Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #include <curses.h>
25 #include <panel.h>
26 #include <locale.h>
27 #include <sha1.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_diff.h"
50 #include "got_opentemp.h"
51 #include "got_utf8.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_path.h"
57 #include "got_worktree.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #ifndef MAX
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #endif
67 #define CTRL(x) ((x) & 0x1f)
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 struct tog_cmd {
74 const char *name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 };
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
98 };
100 enum tog_view_type {
101 TOG_VIEW_DIFF,
102 TOG_VIEW_LOG,
103 TOG_VIEW_BLAME,
104 TOG_VIEW_TREE,
105 TOG_VIEW_REF,
106 };
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
114 int idx;
115 };
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
118 int ncommits;
119 struct commit_queue_head head;
120 };
122 struct tog_color {
123 STAILQ_ENTRY(tog_color) entry;
124 regex_t regex;
125 short colorpair;
126 };
127 STAILQ_HEAD(tog_colors, tog_color);
129 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
130 static struct got_reflist_object_id_map *tog_refs_idmap;
132 static const struct got_error *
133 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
134 struct got_reference* re2)
136 const char *name1 = got_ref_get_name(re1);
137 const char *name2 = got_ref_get_name(re2);
138 int isbackup1, isbackup2;
140 /* Sort backup refs towards the bottom of the list. */
141 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
142 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
143 if (!isbackup1 && isbackup2) {
144 *cmp = -1;
145 return NULL;
146 } else if (isbackup1 && !isbackup2) {
147 *cmp = 1;
148 return NULL;
151 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
152 return NULL;
155 static const struct got_error *
156 tog_load_refs(struct got_repository *repo, int sort_by_date)
158 const struct got_error *err;
160 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
161 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
162 repo);
163 if (err)
164 return err;
166 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
167 repo);
170 static void
171 tog_free_refs(void)
173 if (tog_refs_idmap) {
174 got_reflist_object_id_map_free(tog_refs_idmap);
175 tog_refs_idmap = NULL;
177 got_ref_list_free(&tog_refs);
180 static const struct got_error *
181 add_color(struct tog_colors *colors, const char *pattern,
182 int idx, short color)
184 const struct got_error *err = NULL;
185 struct tog_color *tc;
186 int regerr = 0;
188 if (idx < 1 || idx > COLOR_PAIRS - 1)
189 return NULL;
191 init_pair(idx, color, -1);
193 tc = calloc(1, sizeof(*tc));
194 if (tc == NULL)
195 return got_error_from_errno("calloc");
196 regerr = regcomp(&tc->regex, pattern,
197 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
198 if (regerr) {
199 static char regerr_msg[512];
200 static char err_msg[512];
201 regerror(regerr, &tc->regex, regerr_msg,
202 sizeof(regerr_msg));
203 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
204 regerr_msg);
205 err = got_error_msg(GOT_ERR_REGEX, err_msg);
206 free(tc);
207 return err;
209 tc->colorpair = idx;
210 STAILQ_INSERT_HEAD(colors, tc, entry);
211 return NULL;
214 static void
215 free_colors(struct tog_colors *colors)
217 struct tog_color *tc;
219 while (!STAILQ_EMPTY(colors)) {
220 tc = STAILQ_FIRST(colors);
221 STAILQ_REMOVE_HEAD(colors, entry);
222 regfree(&tc->regex);
223 free(tc);
227 struct tog_color *
228 get_color(struct tog_colors *colors, int colorpair)
230 struct tog_color *tc = NULL;
232 STAILQ_FOREACH(tc, colors, entry) {
233 if (tc->colorpair == colorpair)
234 return tc;
237 return NULL;
240 static int
241 default_color_value(const char *envvar)
243 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
244 return COLOR_MAGENTA;
245 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
246 return COLOR_CYAN;
247 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
248 return COLOR_YELLOW;
249 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
250 return COLOR_GREEN;
251 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
254 return COLOR_MAGENTA;
255 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
256 return COLOR_CYAN;
257 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
258 return COLOR_GREEN;
259 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
260 return COLOR_GREEN;
261 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
262 return COLOR_CYAN;
263 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
264 return COLOR_YELLOW;
265 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
266 return COLOR_GREEN;
267 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
270 return COLOR_YELLOW;
271 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
272 return COLOR_CYAN;
274 return -1;
277 static int
278 get_color_value(const char *envvar)
280 const char *val = getenv(envvar);
282 if (val == NULL)
283 return default_color_value(envvar);
285 if (strcasecmp(val, "black") == 0)
286 return COLOR_BLACK;
287 if (strcasecmp(val, "red") == 0)
288 return COLOR_RED;
289 if (strcasecmp(val, "green") == 0)
290 return COLOR_GREEN;
291 if (strcasecmp(val, "yellow") == 0)
292 return COLOR_YELLOW;
293 if (strcasecmp(val, "blue") == 0)
294 return COLOR_BLUE;
295 if (strcasecmp(val, "magenta") == 0)
296 return COLOR_MAGENTA;
297 if (strcasecmp(val, "cyan") == 0)
298 return COLOR_CYAN;
299 if (strcasecmp(val, "white") == 0)
300 return COLOR_WHITE;
301 if (strcasecmp(val, "default") == 0)
302 return -1;
304 return default_color_value(envvar);
308 struct tog_diff_view_state {
309 struct got_object_id *id1, *id2;
310 const char *label1, *label2;
311 FILE *f, *f1, *f2;
312 int first_displayed_line;
313 int last_displayed_line;
314 int eof;
315 int diff_context;
316 int ignore_whitespace;
317 int force_text_diff;
318 struct got_repository *repo;
319 struct tog_colors colors;
320 size_t nlines;
321 off_t *line_offsets;
322 int matched_line;
323 int selected_line;
325 /* passed from log view; may be NULL */
326 struct tog_view *log_view;
327 };
329 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
331 struct tog_log_thread_args {
332 pthread_cond_t need_commits;
333 pthread_cond_t commit_loaded;
334 int commits_needed;
335 int load_all;
336 struct got_commit_graph *graph;
337 struct commit_queue *commits;
338 const char *in_repo_path;
339 struct got_object_id *start_id;
340 struct got_repository *repo;
341 int log_complete;
342 sig_atomic_t *quit;
343 struct commit_queue_entry **first_displayed_entry;
344 struct commit_queue_entry **selected_entry;
345 int *searching;
346 int *search_next_done;
347 regex_t *regex;
348 };
350 struct tog_log_view_state {
351 struct commit_queue commits;
352 struct commit_queue_entry *first_displayed_entry;
353 struct commit_queue_entry *last_displayed_entry;
354 struct commit_queue_entry *selected_entry;
355 int selected;
356 char *in_repo_path;
357 char *head_ref_name;
358 int log_branches;
359 struct got_repository *repo;
360 struct got_object_id *start_id;
361 sig_atomic_t quit;
362 pthread_t thread;
363 struct tog_log_thread_args thread_args;
364 struct commit_queue_entry *matched_entry;
365 struct commit_queue_entry *search_entry;
366 struct tog_colors colors;
367 };
369 #define TOG_COLOR_DIFF_MINUS 1
370 #define TOG_COLOR_DIFF_PLUS 2
371 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
372 #define TOG_COLOR_DIFF_META 4
373 #define TOG_COLOR_TREE_SUBMODULE 5
374 #define TOG_COLOR_TREE_SYMLINK 6
375 #define TOG_COLOR_TREE_DIRECTORY 7
376 #define TOG_COLOR_TREE_EXECUTABLE 8
377 #define TOG_COLOR_COMMIT 9
378 #define TOG_COLOR_AUTHOR 10
379 #define TOG_COLOR_DATE 11
380 #define TOG_COLOR_REFS_HEADS 12
381 #define TOG_COLOR_REFS_TAGS 13
382 #define TOG_COLOR_REFS_REMOTES 14
383 #define TOG_COLOR_REFS_BACKUP 15
385 struct tog_blame_cb_args {
386 struct tog_blame_line *lines; /* one per line */
387 int nlines;
389 struct tog_view *view;
390 struct got_object_id *commit_id;
391 int *quit;
392 };
394 struct tog_blame_thread_args {
395 const char *path;
396 struct got_repository *repo;
397 struct tog_blame_cb_args *cb_args;
398 int *complete;
399 got_cancel_cb cancel_cb;
400 void *cancel_arg;
401 };
403 struct tog_blame {
404 FILE *f;
405 off_t filesize;
406 struct tog_blame_line *lines;
407 int nlines;
408 off_t *line_offsets;
409 pthread_t thread;
410 struct tog_blame_thread_args thread_args;
411 struct tog_blame_cb_args cb_args;
412 const char *path;
413 };
415 struct tog_blame_view_state {
416 int first_displayed_line;
417 int last_displayed_line;
418 int selected_line;
419 int blame_complete;
420 int eof;
421 int done;
422 struct got_object_id_queue blamed_commits;
423 struct got_object_qid *blamed_commit;
424 char *path;
425 struct got_repository *repo;
426 struct got_object_id *commit_id;
427 struct tog_blame blame;
428 int matched_line;
429 struct tog_colors colors;
430 };
432 struct tog_parent_tree {
433 TAILQ_ENTRY(tog_parent_tree) entry;
434 struct got_tree_object *tree;
435 struct got_tree_entry *first_displayed_entry;
436 struct got_tree_entry *selected_entry;
437 int selected;
438 };
440 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
442 struct tog_tree_view_state {
443 char *tree_label;
444 struct got_object_id *commit_id;/* commit which this tree belongs to */
445 struct got_tree_object *root; /* the commit's root tree entry */
446 struct got_tree_object *tree; /* currently displayed (sub-)tree */
447 struct got_tree_entry *first_displayed_entry;
448 struct got_tree_entry *last_displayed_entry;
449 struct got_tree_entry *selected_entry;
450 int ndisplayed, selected, show_ids;
451 struct tog_parent_trees parents; /* parent trees of current sub-tree */
452 char *head_ref_name;
453 struct got_repository *repo;
454 struct got_tree_entry *matched_entry;
455 struct tog_colors colors;
456 };
458 struct tog_reflist_entry {
459 TAILQ_ENTRY(tog_reflist_entry) entry;
460 struct got_reference *ref;
461 int idx;
462 };
464 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
466 struct tog_ref_view_state {
467 struct tog_reflist_head refs;
468 struct tog_reflist_entry *first_displayed_entry;
469 struct tog_reflist_entry *last_displayed_entry;
470 struct tog_reflist_entry *selected_entry;
471 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
472 struct got_repository *repo;
473 struct tog_reflist_entry *matched_entry;
474 struct tog_colors colors;
475 };
477 /*
478 * We implement two types of views: parent views and child views.
480 * The 'Tab' key switches focus between a parent view and its child view.
481 * Child views are shown side-by-side to their parent view, provided
482 * there is enough screen estate.
484 * When a new view is opened from within a parent view, this new view
485 * becomes a child view of the parent view, replacing any existing child.
487 * When a new view is opened from within a child view, this new view
488 * becomes a parent view which will obscure the views below until the
489 * user quits the new parent view by typing 'q'.
491 * This list of views contains parent views only.
492 * Child views are only pointed to by their parent view.
493 */
494 TAILQ_HEAD(tog_view_list_head, tog_view);
496 struct tog_view {
497 TAILQ_ENTRY(tog_view) entry;
498 WINDOW *window;
499 PANEL *panel;
500 int nlines, ncols, begin_y, begin_x;
501 int lines, cols; /* copies of LINES and COLS */
502 int focussed; /* Only set on one parent or child view at a time. */
503 int dying;
504 struct tog_view *parent;
505 struct tog_view *child;
507 /*
508 * This flag is initially set on parent views when a new child view
509 * is created. It gets toggled when the 'Tab' key switches focus
510 * between parent and child.
511 * The flag indicates whether focus should be passed on to our child
512 * view if this parent view gets picked for focus after another parent
513 * view was closed. This prevents child views from losing focus in such
514 * situations.
515 */
516 int focus_child;
518 /* type-specific state */
519 enum tog_view_type type;
520 union {
521 struct tog_diff_view_state diff;
522 struct tog_log_view_state log;
523 struct tog_blame_view_state blame;
524 struct tog_tree_view_state tree;
525 struct tog_ref_view_state ref;
526 } state;
528 const struct got_error *(*show)(struct tog_view *);
529 const struct got_error *(*input)(struct tog_view **,
530 struct tog_view *, int);
531 const struct got_error *(*close)(struct tog_view *);
533 const struct got_error *(*search_start)(struct tog_view *);
534 const struct got_error *(*search_next)(struct tog_view *);
535 int search_started;
536 int searching;
537 #define TOG_SEARCH_FORWARD 1
538 #define TOG_SEARCH_BACKWARD 2
539 int search_next_done;
540 #define TOG_SEARCH_HAVE_MORE 1
541 #define TOG_SEARCH_NO_MORE 2
542 #define TOG_SEARCH_HAVE_NONE 3
543 regex_t regex;
544 regmatch_t regmatch;
545 };
547 static const struct got_error *open_diff_view(struct tog_view *,
548 struct got_object_id *, struct got_object_id *,
549 const char *, const char *, int, int, int, struct tog_view *,
550 struct got_repository *);
551 static const struct got_error *show_diff_view(struct tog_view *);
552 static const struct got_error *input_diff_view(struct tog_view **,
553 struct tog_view *, int);
554 static const struct got_error* close_diff_view(struct tog_view *);
555 static const struct got_error *search_start_diff_view(struct tog_view *);
556 static const struct got_error *search_next_diff_view(struct tog_view *);
558 static const struct got_error *open_log_view(struct tog_view *,
559 struct got_object_id *, struct got_repository *,
560 const char *, const char *, int);
561 static const struct got_error * show_log_view(struct tog_view *);
562 static const struct got_error *input_log_view(struct tog_view **,
563 struct tog_view *, int);
564 static const struct got_error *close_log_view(struct tog_view *);
565 static const struct got_error *search_start_log_view(struct tog_view *);
566 static const struct got_error *search_next_log_view(struct tog_view *);
568 static const struct got_error *open_blame_view(struct tog_view *, char *,
569 struct got_object_id *, struct got_repository *);
570 static const struct got_error *show_blame_view(struct tog_view *);
571 static const struct got_error *input_blame_view(struct tog_view **,
572 struct tog_view *, int);
573 static const struct got_error *close_blame_view(struct tog_view *);
574 static const struct got_error *search_start_blame_view(struct tog_view *);
575 static const struct got_error *search_next_blame_view(struct tog_view *);
577 static const struct got_error *open_tree_view(struct tog_view *,
578 struct got_object_id *, const char *, struct got_repository *);
579 static const struct got_error *show_tree_view(struct tog_view *);
580 static const struct got_error *input_tree_view(struct tog_view **,
581 struct tog_view *, int);
582 static const struct got_error *close_tree_view(struct tog_view *);
583 static const struct got_error *search_start_tree_view(struct tog_view *);
584 static const struct got_error *search_next_tree_view(struct tog_view *);
586 static const struct got_error *open_ref_view(struct tog_view *,
587 struct got_repository *);
588 static const struct got_error *show_ref_view(struct tog_view *);
589 static const struct got_error *input_ref_view(struct tog_view **,
590 struct tog_view *, int);
591 static const struct got_error *close_ref_view(struct tog_view *);
592 static const struct got_error *search_start_ref_view(struct tog_view *);
593 static const struct got_error *search_next_ref_view(struct tog_view *);
595 static volatile sig_atomic_t tog_sigwinch_received;
596 static volatile sig_atomic_t tog_sigpipe_received;
597 static volatile sig_atomic_t tog_sigcont_received;
598 static volatile sig_atomic_t tog_sigint_received;
599 static volatile sig_atomic_t tog_sigterm_received;
601 static void
602 tog_sigwinch(int signo)
604 tog_sigwinch_received = 1;
607 static void
608 tog_sigpipe(int signo)
610 tog_sigpipe_received = 1;
613 static void
614 tog_sigcont(int signo)
616 tog_sigcont_received = 1;
619 static void
620 tog_sigint(int signo)
622 tog_sigint_received = 1;
625 static void
626 tog_sigterm(int signo)
628 tog_sigterm_received = 1;
631 static int
632 tog_fatal_signal_received()
634 return (tog_sigpipe_received ||
635 tog_sigint_received || tog_sigint_received);
639 static const struct got_error *
640 view_close(struct tog_view *view)
642 const struct got_error *err = NULL;
644 if (view->child) {
645 view_close(view->child);
646 view->child = NULL;
648 if (view->close)
649 err = view->close(view);
650 if (view->panel)
651 del_panel(view->panel);
652 if (view->window)
653 delwin(view->window);
654 free(view);
655 return err;
658 static struct tog_view *
659 view_open(int nlines, int ncols, int begin_y, int begin_x,
660 enum tog_view_type type)
662 struct tog_view *view = calloc(1, sizeof(*view));
664 if (view == NULL)
665 return NULL;
667 view->type = type;
668 view->lines = LINES;
669 view->cols = COLS;
670 view->nlines = nlines ? nlines : LINES - begin_y;
671 view->ncols = ncols ? ncols : COLS - begin_x;
672 view->begin_y = begin_y;
673 view->begin_x = begin_x;
674 view->window = newwin(nlines, ncols, begin_y, begin_x);
675 if (view->window == NULL) {
676 view_close(view);
677 return NULL;
679 view->panel = new_panel(view->window);
680 if (view->panel == NULL ||
681 set_panel_userptr(view->panel, view) != OK) {
682 view_close(view);
683 return NULL;
686 keypad(view->window, TRUE);
687 return view;
690 static int
691 view_split_begin_x(int begin_x)
693 if (begin_x > 0 || COLS < 120)
694 return 0;
695 return (COLS - MAX(COLS / 2, 80));
698 static const struct got_error *view_resize(struct tog_view *);
700 static const struct got_error *
701 view_splitscreen(struct tog_view *view)
703 const struct got_error *err = NULL;
705 view->begin_y = 0;
706 view->begin_x = view_split_begin_x(0);
707 view->nlines = LINES;
708 view->ncols = COLS - view->begin_x;
709 view->lines = LINES;
710 view->cols = COLS;
711 err = view_resize(view);
712 if (err)
713 return err;
715 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
716 return got_error_from_errno("mvwin");
718 return NULL;
721 static const struct got_error *
722 view_fullscreen(struct tog_view *view)
724 const struct got_error *err = NULL;
726 view->begin_x = 0;
727 view->begin_y = 0;
728 view->nlines = LINES;
729 view->ncols = COLS;
730 view->lines = LINES;
731 view->cols = COLS;
732 err = view_resize(view);
733 if (err)
734 return err;
736 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
737 return got_error_from_errno("mvwin");
739 return NULL;
742 static int
743 view_is_parent_view(struct tog_view *view)
745 return view->parent == NULL;
748 static const struct got_error *
749 view_resize(struct tog_view *view)
751 int nlines, ncols;
753 if (view->lines > LINES)
754 nlines = view->nlines - (view->lines - LINES);
755 else
756 nlines = view->nlines + (LINES - view->lines);
758 if (view->cols > COLS)
759 ncols = view->ncols - (view->cols - COLS);
760 else
761 ncols = view->ncols + (COLS - view->cols);
763 if (wresize(view->window, nlines, ncols) == ERR)
764 return got_error_from_errno("wresize");
765 if (replace_panel(view->panel, view->window) == ERR)
766 return got_error_from_errno("replace_panel");
767 wclear(view->window);
769 view->nlines = nlines;
770 view->ncols = ncols;
771 view->lines = LINES;
772 view->cols = COLS;
774 if (view->child) {
775 view->child->begin_x = view_split_begin_x(view->begin_x);
776 if (view->child->begin_x == 0) {
777 view_fullscreen(view->child);
778 if (view->child->focussed)
779 show_panel(view->child->panel);
780 else
781 show_panel(view->panel);
782 } else {
783 view_splitscreen(view->child);
784 show_panel(view->child->panel);
788 return NULL;
791 static const struct got_error *
792 view_close_child(struct tog_view *view)
794 const struct got_error *err = NULL;
796 if (view->child == NULL)
797 return NULL;
799 err = view_close(view->child);
800 view->child = NULL;
801 return err;
804 static void
805 view_set_child(struct tog_view *view, struct tog_view *child)
807 view->child = child;
808 child->parent = view;
811 static int
812 view_is_splitscreen(struct tog_view *view)
814 return view->begin_x > 0;
817 static void
818 tog_resizeterm(void)
820 int cols, lines;
821 struct winsize size;
823 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
824 cols = 80; /* Default */
825 lines = 24;
826 } else {
827 cols = size.ws_col;
828 lines = size.ws_row;
830 resize_term(lines, cols);
833 static const struct got_error *
834 view_search_start(struct tog_view *view)
836 const struct got_error *err = NULL;
837 char pattern[1024];
838 int ret;
840 if (view->search_started) {
841 regfree(&view->regex);
842 view->searching = 0;
843 memset(&view->regmatch, 0, sizeof(view->regmatch));
845 view->search_started = 0;
847 if (view->nlines < 1)
848 return NULL;
850 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
851 wclrtoeol(view->window);
853 nocbreak();
854 echo();
855 ret = wgetnstr(view->window, pattern, sizeof(pattern));
856 cbreak();
857 noecho();
858 if (ret == ERR)
859 return NULL;
861 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
862 err = view->search_start(view);
863 if (err) {
864 regfree(&view->regex);
865 return err;
867 view->search_started = 1;
868 view->searching = TOG_SEARCH_FORWARD;
869 view->search_next_done = 0;
870 view->search_next(view);
873 return NULL;
876 static const struct got_error *
877 view_input(struct tog_view **new, int *done, struct tog_view *view,
878 struct tog_view_list_head *views)
880 const struct got_error *err = NULL;
881 struct tog_view *v;
882 int ch, errcode;
884 *new = NULL;
886 /* Clear "no matches" indicator. */
887 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
888 view->search_next_done == TOG_SEARCH_HAVE_NONE)
889 view->search_next_done = TOG_SEARCH_HAVE_MORE;
891 if (view->searching && !view->search_next_done) {
892 errcode = pthread_mutex_unlock(&tog_mutex);
893 if (errcode)
894 return got_error_set_errno(errcode,
895 "pthread_mutex_unlock");
896 sched_yield();
897 errcode = pthread_mutex_lock(&tog_mutex);
898 if (errcode)
899 return got_error_set_errno(errcode,
900 "pthread_mutex_lock");
901 view->search_next(view);
902 return NULL;
905 nodelay(stdscr, FALSE);
906 /* Allow threads to make progress while we are waiting for input. */
907 errcode = pthread_mutex_unlock(&tog_mutex);
908 if (errcode)
909 return got_error_set_errno(errcode, "pthread_mutex_unlock");
910 ch = wgetch(view->window);
911 errcode = pthread_mutex_lock(&tog_mutex);
912 if (errcode)
913 return got_error_set_errno(errcode, "pthread_mutex_lock");
914 nodelay(stdscr, TRUE);
916 if (tog_sigwinch_received || tog_sigcont_received) {
917 tog_resizeterm();
918 tog_sigwinch_received = 0;
919 tog_sigcont_received = 0;
920 TAILQ_FOREACH(v, views, entry) {
921 err = view_resize(v);
922 if (err)
923 return err;
924 err = v->input(new, v, KEY_RESIZE);
925 if (err)
926 return err;
927 if (v->child) {
928 err = view_resize(v->child);
929 if (err)
930 return err;
931 err = v->child->input(new, v->child,
932 KEY_RESIZE);
933 if (err)
934 return err;
939 switch (ch) {
940 case '\t':
941 if (view->child) {
942 view->focussed = 0;
943 view->child->focussed = 1;
944 view->focus_child = 1;
945 } else if (view->parent) {
946 view->focussed = 0;
947 view->parent->focussed = 1;
948 view->parent->focus_child = 0;
950 break;
951 case 'q':
952 err = view->input(new, view, ch);
953 view->dying = 1;
954 break;
955 case 'Q':
956 *done = 1;
957 break;
958 case 'f':
959 if (view_is_parent_view(view)) {
960 if (view->child == NULL)
961 break;
962 if (view_is_splitscreen(view->child)) {
963 view->focussed = 0;
964 view->child->focussed = 1;
965 err = view_fullscreen(view->child);
966 } else
967 err = view_splitscreen(view->child);
968 if (err)
969 break;
970 err = view->child->input(new, view->child,
971 KEY_RESIZE);
972 } else {
973 if (view_is_splitscreen(view)) {
974 view->parent->focussed = 0;
975 view->focussed = 1;
976 err = view_fullscreen(view);
977 } else {
978 err = view_splitscreen(view);
980 if (err)
981 break;
982 err = view->input(new, view, KEY_RESIZE);
984 break;
985 case KEY_RESIZE:
986 break;
987 case '/':
988 if (view->search_start)
989 view_search_start(view);
990 else
991 err = view->input(new, view, ch);
992 break;
993 case 'N':
994 case 'n':
995 if (view->search_started && view->search_next) {
996 view->searching = (ch == 'n' ?
997 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
998 view->search_next_done = 0;
999 view->search_next(view);
1000 } else
1001 err = view->input(new, view, ch);
1002 break;
1003 default:
1004 err = view->input(new, view, ch);
1005 break;
1008 return err;
1011 void
1012 view_vborder(struct tog_view *view)
1014 PANEL *panel;
1015 const struct tog_view *view_above;
1017 if (view->parent)
1018 return view_vborder(view->parent);
1020 panel = panel_above(view->panel);
1021 if (panel == NULL)
1022 return;
1024 view_above = panel_userptr(panel);
1025 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1026 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1029 int
1030 view_needs_focus_indication(struct tog_view *view)
1032 if (view_is_parent_view(view)) {
1033 if (view->child == NULL || view->child->focussed)
1034 return 0;
1035 if (!view_is_splitscreen(view->child))
1036 return 0;
1037 } else if (!view_is_splitscreen(view))
1038 return 0;
1040 return view->focussed;
1043 static const struct got_error *
1044 view_loop(struct tog_view *view)
1046 const struct got_error *err = NULL;
1047 struct tog_view_list_head views;
1048 struct tog_view *new_view;
1049 int fast_refresh = 10;
1050 int done = 0, errcode;
1052 errcode = pthread_mutex_lock(&tog_mutex);
1053 if (errcode)
1054 return got_error_set_errno(errcode, "pthread_mutex_lock");
1056 TAILQ_INIT(&views);
1057 TAILQ_INSERT_HEAD(&views, view, entry);
1059 view->focussed = 1;
1060 err = view->show(view);
1061 if (err)
1062 return err;
1063 update_panels();
1064 doupdate();
1065 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1066 /* Refresh fast during initialization, then become slower. */
1067 if (fast_refresh && fast_refresh-- == 0)
1068 halfdelay(10); /* switch to once per second */
1070 err = view_input(&new_view, &done, view, &views);
1071 if (err)
1072 break;
1073 if (view->dying) {
1074 struct tog_view *v, *prev = NULL;
1076 if (view_is_parent_view(view))
1077 prev = TAILQ_PREV(view, tog_view_list_head,
1078 entry);
1079 else if (view->parent)
1080 prev = view->parent;
1082 if (view->parent) {
1083 view->parent->child = NULL;
1084 view->parent->focus_child = 0;
1085 } else
1086 TAILQ_REMOVE(&views, view, entry);
1088 err = view_close(view);
1089 if (err)
1090 goto done;
1092 view = NULL;
1093 TAILQ_FOREACH(v, &views, entry) {
1094 if (v->focussed)
1095 break;
1097 if (view == NULL && new_view == NULL) {
1098 /* No view has focus. Try to pick one. */
1099 if (prev)
1100 view = prev;
1101 else if (!TAILQ_EMPTY(&views)) {
1102 view = TAILQ_LAST(&views,
1103 tog_view_list_head);
1105 if (view) {
1106 if (view->focus_child) {
1107 view->child->focussed = 1;
1108 view = view->child;
1109 } else
1110 view->focussed = 1;
1114 if (new_view) {
1115 struct tog_view *v, *t;
1116 /* Only allow one parent view per type. */
1117 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1118 if (v->type != new_view->type)
1119 continue;
1120 TAILQ_REMOVE(&views, v, entry);
1121 err = view_close(v);
1122 if (err)
1123 goto done;
1124 break;
1126 TAILQ_INSERT_TAIL(&views, new_view, entry);
1127 view = new_view;
1129 if (view) {
1130 if (view_is_parent_view(view)) {
1131 if (view->child && view->child->focussed)
1132 view = view->child;
1133 } else {
1134 if (view->parent && view->parent->focussed)
1135 view = view->parent;
1137 show_panel(view->panel);
1138 if (view->child && view_is_splitscreen(view->child))
1139 show_panel(view->child->panel);
1140 if (view->parent && view_is_splitscreen(view)) {
1141 err = view->parent->show(view->parent);
1142 if (err)
1143 goto done;
1145 err = view->show(view);
1146 if (err)
1147 goto done;
1148 if (view->child) {
1149 err = view->child->show(view->child);
1150 if (err)
1151 goto done;
1153 update_panels();
1154 doupdate();
1157 done:
1158 while (!TAILQ_EMPTY(&views)) {
1159 view = TAILQ_FIRST(&views);
1160 TAILQ_REMOVE(&views, view, entry);
1161 view_close(view);
1164 errcode = pthread_mutex_unlock(&tog_mutex);
1165 if (errcode && err == NULL)
1166 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1168 return err;
1171 __dead static void
1172 usage_log(void)
1174 endwin();
1175 fprintf(stderr,
1176 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1177 getprogname());
1178 exit(1);
1181 /* Create newly allocated wide-character string equivalent to a byte string. */
1182 static const struct got_error *
1183 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1185 char *vis = NULL;
1186 const struct got_error *err = NULL;
1188 *ws = NULL;
1189 *wlen = mbstowcs(NULL, s, 0);
1190 if (*wlen == (size_t)-1) {
1191 int vislen;
1192 if (errno != EILSEQ)
1193 return got_error_from_errno("mbstowcs");
1195 /* byte string invalid in current encoding; try to "fix" it */
1196 err = got_mbsavis(&vis, &vislen, s);
1197 if (err)
1198 return err;
1199 *wlen = mbstowcs(NULL, vis, 0);
1200 if (*wlen == (size_t)-1) {
1201 err = got_error_from_errno("mbstowcs"); /* give up */
1202 goto done;
1206 *ws = calloc(*wlen + 1, sizeof(**ws));
1207 if (*ws == NULL) {
1208 err = got_error_from_errno("calloc");
1209 goto done;
1212 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1213 err = got_error_from_errno("mbstowcs");
1214 done:
1215 free(vis);
1216 if (err) {
1217 free(*ws);
1218 *ws = NULL;
1219 *wlen = 0;
1221 return err;
1224 /* Format a line for display, ensuring that it won't overflow a width limit. */
1225 static const struct got_error *
1226 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1227 int col_tab_align)
1229 const struct got_error *err = NULL;
1230 int cols = 0;
1231 wchar_t *wline = NULL;
1232 size_t wlen;
1233 int i;
1235 *wlinep = NULL;
1236 *widthp = 0;
1238 err = mbs2ws(&wline, &wlen, line);
1239 if (err)
1240 return err;
1242 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1243 wline[wlen - 1] = L'\0';
1244 wlen--;
1246 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1247 wline[wlen - 1] = L'\0';
1248 wlen--;
1251 i = 0;
1252 while (i < wlen) {
1253 int width = wcwidth(wline[i]);
1255 if (width == 0) {
1256 i++;
1257 continue;
1260 if (width == 1 || width == 2) {
1261 if (cols + width > wlimit)
1262 break;
1263 cols += width;
1264 i++;
1265 } else if (width == -1) {
1266 if (wline[i] == L'\t') {
1267 width = TABSIZE -
1268 ((cols + col_tab_align) % TABSIZE);
1269 } else {
1270 width = 1;
1271 wline[i] = L'.';
1273 if (cols + width > wlimit)
1274 break;
1275 cols += width;
1276 i++;
1277 } else {
1278 err = got_error_from_errno("wcwidth");
1279 goto done;
1282 wline[i] = L'\0';
1283 if (widthp)
1284 *widthp = cols;
1285 done:
1286 if (err)
1287 free(wline);
1288 else
1289 *wlinep = wline;
1290 return err;
1293 static const struct got_error*
1294 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1295 struct got_object_id *id, struct got_repository *repo)
1297 static const struct got_error *err = NULL;
1298 struct got_reflist_entry *re;
1299 char *s;
1300 const char *name;
1302 *refs_str = NULL;
1304 TAILQ_FOREACH(re, refs, entry) {
1305 struct got_tag_object *tag = NULL;
1306 struct got_object_id *ref_id;
1307 int cmp;
1309 name = got_ref_get_name(re->ref);
1310 if (strcmp(name, GOT_REF_HEAD) == 0)
1311 continue;
1312 if (strncmp(name, "refs/", 5) == 0)
1313 name += 5;
1314 if (strncmp(name, "got/", 4) == 0 &&
1315 strncmp(name, "got/backup/", 11) != 0)
1316 continue;
1317 if (strncmp(name, "heads/", 6) == 0)
1318 name += 6;
1319 if (strncmp(name, "remotes/", 8) == 0) {
1320 name += 8;
1321 s = strstr(name, "/" GOT_REF_HEAD);
1322 if (s != NULL && s[strlen(s)] == '\0')
1323 continue;
1325 err = got_ref_resolve(&ref_id, repo, re->ref);
1326 if (err)
1327 break;
1328 if (strncmp(name, "tags/", 5) == 0) {
1329 err = got_object_open_as_tag(&tag, repo, ref_id);
1330 if (err) {
1331 if (err->code != GOT_ERR_OBJ_TYPE) {
1332 free(ref_id);
1333 break;
1335 /* Ref points at something other than a tag. */
1336 err = NULL;
1337 tag = NULL;
1340 cmp = got_object_id_cmp(tag ?
1341 got_object_tag_get_object_id(tag) : ref_id, id);
1342 free(ref_id);
1343 if (tag)
1344 got_object_tag_close(tag);
1345 if (cmp != 0)
1346 continue;
1347 s = *refs_str;
1348 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1349 s ? ", " : "", name) == -1) {
1350 err = got_error_from_errno("asprintf");
1351 free(s);
1352 *refs_str = NULL;
1353 break;
1355 free(s);
1358 return err;
1361 static const struct got_error *
1362 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1363 int col_tab_align)
1365 char *smallerthan;
1367 smallerthan = strchr(author, '<');
1368 if (smallerthan && smallerthan[1] != '\0')
1369 author = smallerthan + 1;
1370 author[strcspn(author, "@>")] = '\0';
1371 return format_line(wauthor, author_width, author, limit, col_tab_align);
1374 static const struct got_error *
1375 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1376 struct got_object_id *id, const size_t date_display_cols,
1377 int author_display_cols)
1379 struct tog_log_view_state *s = &view->state.log;
1380 const struct got_error *err = NULL;
1381 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1382 char *logmsg0 = NULL, *logmsg = NULL;
1383 char *author = NULL;
1384 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1385 int author_width, logmsg_width;
1386 char *newline, *line = NULL;
1387 int col, limit;
1388 const int avail = view->ncols;
1389 struct tm tm;
1390 time_t committer_time;
1391 struct tog_color *tc;
1393 committer_time = got_object_commit_get_committer_time(commit);
1394 if (gmtime_r(&committer_time, &tm) == NULL)
1395 return got_error_from_errno("gmtime_r");
1396 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1397 return got_error(GOT_ERR_NO_SPACE);
1399 if (avail <= date_display_cols)
1400 limit = MIN(sizeof(datebuf) - 1, avail);
1401 else
1402 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1403 tc = get_color(&s->colors, TOG_COLOR_DATE);
1404 if (tc)
1405 wattr_on(view->window,
1406 COLOR_PAIR(tc->colorpair), NULL);
1407 waddnstr(view->window, datebuf, limit);
1408 if (tc)
1409 wattr_off(view->window,
1410 COLOR_PAIR(tc->colorpair), NULL);
1411 col = limit;
1412 if (col > avail)
1413 goto done;
1415 if (avail >= 120) {
1416 char *id_str;
1417 err = got_object_id_str(&id_str, id);
1418 if (err)
1419 goto done;
1420 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1421 if (tc)
1422 wattr_on(view->window,
1423 COLOR_PAIR(tc->colorpair), NULL);
1424 wprintw(view->window, "%.8s ", id_str);
1425 if (tc)
1426 wattr_off(view->window,
1427 COLOR_PAIR(tc->colorpair), NULL);
1428 free(id_str);
1429 col += 9;
1430 if (col > avail)
1431 goto done;
1434 author = strdup(got_object_commit_get_author(commit));
1435 if (author == NULL) {
1436 err = got_error_from_errno("strdup");
1437 goto done;
1439 err = format_author(&wauthor, &author_width, author, avail - col, col);
1440 if (err)
1441 goto done;
1442 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1443 if (tc)
1444 wattr_on(view->window,
1445 COLOR_PAIR(tc->colorpair), NULL);
1446 waddwstr(view->window, wauthor);
1447 if (tc)
1448 wattr_off(view->window,
1449 COLOR_PAIR(tc->colorpair), NULL);
1450 col += author_width;
1451 while (col < avail && author_width < author_display_cols + 2) {
1452 waddch(view->window, ' ');
1453 col++;
1454 author_width++;
1456 if (col > avail)
1457 goto done;
1459 err = got_object_commit_get_logmsg(&logmsg0, commit);
1460 if (err)
1461 goto done;
1462 logmsg = logmsg0;
1463 while (*logmsg == '\n')
1464 logmsg++;
1465 newline = strchr(logmsg, '\n');
1466 if (newline)
1467 *newline = '\0';
1468 limit = avail - col;
1469 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1470 if (err)
1471 goto done;
1472 waddwstr(view->window, wlogmsg);
1473 col += logmsg_width;
1474 while (col < avail) {
1475 waddch(view->window, ' ');
1476 col++;
1478 done:
1479 free(logmsg0);
1480 free(wlogmsg);
1481 free(author);
1482 free(wauthor);
1483 free(line);
1484 return err;
1487 static struct commit_queue_entry *
1488 alloc_commit_queue_entry(struct got_commit_object *commit,
1489 struct got_object_id *id)
1491 struct commit_queue_entry *entry;
1493 entry = calloc(1, sizeof(*entry));
1494 if (entry == NULL)
1495 return NULL;
1497 entry->id = id;
1498 entry->commit = commit;
1499 return entry;
1502 static void
1503 pop_commit(struct commit_queue *commits)
1505 struct commit_queue_entry *entry;
1507 entry = TAILQ_FIRST(&commits->head);
1508 TAILQ_REMOVE(&commits->head, entry, entry);
1509 got_object_commit_close(entry->commit);
1510 commits->ncommits--;
1511 /* Don't free entry->id! It is owned by the commit graph. */
1512 free(entry);
1515 static void
1516 free_commits(struct commit_queue *commits)
1518 while (!TAILQ_EMPTY(&commits->head))
1519 pop_commit(commits);
1522 static const struct got_error *
1523 match_commit(int *have_match, struct got_object_id *id,
1524 struct got_commit_object *commit, regex_t *regex)
1526 const struct got_error *err = NULL;
1527 regmatch_t regmatch;
1528 char *id_str = NULL, *logmsg = NULL;
1530 *have_match = 0;
1532 err = got_object_id_str(&id_str, id);
1533 if (err)
1534 return err;
1536 err = got_object_commit_get_logmsg(&logmsg, commit);
1537 if (err)
1538 goto done;
1540 if (regexec(regex, got_object_commit_get_author(commit), 1,
1541 &regmatch, 0) == 0 ||
1542 regexec(regex, got_object_commit_get_committer(commit), 1,
1543 &regmatch, 0) == 0 ||
1544 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1545 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1546 *have_match = 1;
1547 done:
1548 free(id_str);
1549 free(logmsg);
1550 return err;
1553 static const struct got_error *
1554 queue_commits(struct tog_log_thread_args *a)
1556 const struct got_error *err = NULL;
1559 * We keep all commits open throughout the lifetime of the log
1560 * view in order to avoid having to re-fetch commits from disk
1561 * while updating the display.
1563 do {
1564 struct got_object_id *id;
1565 struct got_commit_object *commit;
1566 struct commit_queue_entry *entry;
1567 int errcode;
1569 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1570 NULL, NULL);
1571 if (err || id == NULL)
1572 break;
1574 err = got_object_open_as_commit(&commit, a->repo, id);
1575 if (err)
1576 break;
1577 entry = alloc_commit_queue_entry(commit, id);
1578 if (entry == NULL) {
1579 err = got_error_from_errno("alloc_commit_queue_entry");
1580 break;
1583 errcode = pthread_mutex_lock(&tog_mutex);
1584 if (errcode) {
1585 err = got_error_set_errno(errcode,
1586 "pthread_mutex_lock");
1587 break;
1590 entry->idx = a->commits->ncommits;
1591 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1592 a->commits->ncommits++;
1594 if (*a->searching == TOG_SEARCH_FORWARD &&
1595 !*a->search_next_done) {
1596 int have_match;
1597 err = match_commit(&have_match, id, commit, a->regex);
1598 if (err)
1599 break;
1600 if (have_match)
1601 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1604 errcode = pthread_mutex_unlock(&tog_mutex);
1605 if (errcode && err == NULL)
1606 err = got_error_set_errno(errcode,
1607 "pthread_mutex_unlock");
1608 if (err)
1609 break;
1610 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1612 return err;
1615 static void
1616 select_commit(struct tog_log_view_state *s)
1618 struct commit_queue_entry *entry;
1619 int ncommits = 0;
1621 entry = s->first_displayed_entry;
1622 while (entry) {
1623 if (ncommits == s->selected) {
1624 s->selected_entry = entry;
1625 break;
1627 entry = TAILQ_NEXT(entry, entry);
1628 ncommits++;
1632 static const struct got_error *
1633 draw_commits(struct tog_view *view)
1635 const struct got_error *err = NULL;
1636 struct tog_log_view_state *s = &view->state.log;
1637 struct commit_queue_entry *entry = s->selected_entry;
1638 const int limit = view->nlines;
1639 int width;
1640 int ncommits, author_cols = 4;
1641 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1642 char *refs_str = NULL;
1643 wchar_t *wline;
1644 struct tog_color *tc;
1645 static const size_t date_display_cols = 12;
1647 if (s->selected_entry &&
1648 !(view->searching && view->search_next_done == 0)) {
1649 struct got_reflist_head *refs;
1650 err = got_object_id_str(&id_str, s->selected_entry->id);
1651 if (err)
1652 return err;
1653 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1654 s->selected_entry->id);
1655 if (refs) {
1656 err = build_refs_str(&refs_str, refs,
1657 s->selected_entry->id, s->repo);
1658 if (err)
1659 goto done;
1663 if (s->thread_args.commits_needed == 0)
1664 halfdelay(10); /* disable fast refresh */
1666 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1667 if (asprintf(&ncommits_str, " [%d/%d] %s",
1668 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1669 (view->searching && !view->search_next_done) ?
1670 "searching..." : "loading...") == -1) {
1671 err = got_error_from_errno("asprintf");
1672 goto done;
1674 } else {
1675 const char *search_str = NULL;
1677 if (view->searching) {
1678 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1679 search_str = "no more matches";
1680 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1681 search_str = "no matches found";
1682 else if (!view->search_next_done)
1683 search_str = "searching...";
1686 if (asprintf(&ncommits_str, " [%d/%d] %s",
1687 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1688 search_str ? search_str :
1689 (refs_str ? refs_str : "")) == -1) {
1690 err = got_error_from_errno("asprintf");
1691 goto done;
1695 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1696 if (asprintf(&header, "commit %s %s%s",
1697 id_str ? id_str : "........................................",
1698 s->in_repo_path, ncommits_str) == -1) {
1699 err = got_error_from_errno("asprintf");
1700 header = NULL;
1701 goto done;
1703 } else if (asprintf(&header, "commit %s%s",
1704 id_str ? id_str : "........................................",
1705 ncommits_str) == -1) {
1706 err = got_error_from_errno("asprintf");
1707 header = NULL;
1708 goto done;
1710 err = format_line(&wline, &width, header, view->ncols, 0);
1711 if (err)
1712 goto done;
1714 werase(view->window);
1716 if (view_needs_focus_indication(view))
1717 wstandout(view->window);
1718 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1719 if (tc)
1720 wattr_on(view->window,
1721 COLOR_PAIR(tc->colorpair), NULL);
1722 waddwstr(view->window, wline);
1723 if (tc)
1724 wattr_off(view->window,
1725 COLOR_PAIR(tc->colorpair), NULL);
1726 while (width < view->ncols) {
1727 waddch(view->window, ' ');
1728 width++;
1730 if (view_needs_focus_indication(view))
1731 wstandend(view->window);
1732 free(wline);
1733 if (limit <= 1)
1734 goto done;
1736 /* Grow author column size if necessary. */
1737 entry = s->first_displayed_entry;
1738 ncommits = 0;
1739 while (entry) {
1740 char *author;
1741 wchar_t *wauthor;
1742 int width;
1743 if (ncommits >= limit - 1)
1744 break;
1745 author = strdup(got_object_commit_get_author(entry->commit));
1746 if (author == NULL) {
1747 err = got_error_from_errno("strdup");
1748 goto done;
1750 err = format_author(&wauthor, &width, author, COLS,
1751 date_display_cols);
1752 if (author_cols < width)
1753 author_cols = width;
1754 free(wauthor);
1755 free(author);
1756 ncommits++;
1757 entry = TAILQ_NEXT(entry, entry);
1760 entry = s->first_displayed_entry;
1761 s->last_displayed_entry = s->first_displayed_entry;
1762 ncommits = 0;
1763 while (entry) {
1764 if (ncommits >= limit - 1)
1765 break;
1766 if (ncommits == s->selected)
1767 wstandout(view->window);
1768 err = draw_commit(view, entry->commit, entry->id,
1769 date_display_cols, author_cols);
1770 if (ncommits == s->selected)
1771 wstandend(view->window);
1772 if (err)
1773 goto done;
1774 ncommits++;
1775 s->last_displayed_entry = entry;
1776 entry = TAILQ_NEXT(entry, entry);
1779 view_vborder(view);
1780 done:
1781 free(id_str);
1782 free(refs_str);
1783 free(ncommits_str);
1784 free(header);
1785 return err;
1788 static void
1789 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1791 struct commit_queue_entry *entry;
1792 int nscrolled = 0;
1794 entry = TAILQ_FIRST(&s->commits.head);
1795 if (s->first_displayed_entry == entry)
1796 return;
1798 entry = s->first_displayed_entry;
1799 while (entry && nscrolled < maxscroll) {
1800 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1801 if (entry) {
1802 s->first_displayed_entry = entry;
1803 nscrolled++;
1808 static const struct got_error *
1809 trigger_log_thread(struct tog_view *view, int wait)
1811 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1812 int errcode;
1814 halfdelay(1); /* fast refresh while loading commits */
1816 while (ta->commits_needed > 0 || ta->load_all) {
1817 if (ta->log_complete)
1818 break;
1820 /* Wake the log thread. */
1821 errcode = pthread_cond_signal(&ta->need_commits);
1822 if (errcode)
1823 return got_error_set_errno(errcode,
1824 "pthread_cond_signal");
1827 * The mutex will be released while the view loop waits
1828 * in wgetch(), at which time the log thread will run.
1830 if (!wait)
1831 break;
1833 /* Display progress update in log view. */
1834 show_log_view(view);
1835 update_panels();
1836 doupdate();
1838 /* Wait right here while next commit is being loaded. */
1839 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1840 if (errcode)
1841 return got_error_set_errno(errcode,
1842 "pthread_cond_wait");
1844 /* Display progress update in log view. */
1845 show_log_view(view);
1846 update_panels();
1847 doupdate();
1850 return NULL;
1853 static const struct got_error *
1854 log_scroll_down(struct tog_view *view, int maxscroll)
1856 struct tog_log_view_state *s = &view->state.log;
1857 const struct got_error *err = NULL;
1858 struct commit_queue_entry *pentry;
1859 int nscrolled = 0, ncommits_needed;
1861 if (s->last_displayed_entry == NULL)
1862 return NULL;
1864 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1865 if (s->commits.ncommits < ncommits_needed &&
1866 !s->thread_args.log_complete) {
1868 * Ask the log thread for required amount of commits.
1870 s->thread_args.commits_needed += maxscroll;
1871 err = trigger_log_thread(view, 1);
1872 if (err)
1873 return err;
1876 do {
1877 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1878 if (pentry == NULL)
1879 break;
1881 s->last_displayed_entry = pentry;
1883 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1884 if (pentry == NULL)
1885 break;
1886 s->first_displayed_entry = pentry;
1887 } while (++nscrolled < maxscroll);
1889 return err;
1892 static const struct got_error *
1893 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1894 struct got_commit_object *commit, struct got_object_id *commit_id,
1895 struct tog_view *log_view, struct got_repository *repo)
1897 const struct got_error *err;
1898 struct got_object_qid *parent_id;
1899 struct tog_view *diff_view;
1901 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1902 if (diff_view == NULL)
1903 return got_error_from_errno("view_open");
1905 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1906 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1907 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1908 if (err == NULL)
1909 *new_view = diff_view;
1910 return err;
1913 static const struct got_error *
1914 tree_view_visit_subtree(struct tog_tree_view_state *s,
1915 struct got_tree_object *subtree)
1917 struct tog_parent_tree *parent;
1919 parent = calloc(1, sizeof(*parent));
1920 if (parent == NULL)
1921 return got_error_from_errno("calloc");
1923 parent->tree = s->tree;
1924 parent->first_displayed_entry = s->first_displayed_entry;
1925 parent->selected_entry = s->selected_entry;
1926 parent->selected = s->selected;
1927 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1928 s->tree = subtree;
1929 s->selected = 0;
1930 s->first_displayed_entry = NULL;
1931 return NULL;
1934 static const struct got_error *
1935 tree_view_walk_path(struct tog_tree_view_state *s,
1936 struct got_commit_object *commit, const char *path)
1938 const struct got_error *err = NULL;
1939 struct got_tree_object *tree = NULL;
1940 const char *p;
1941 char *slash, *subpath = NULL;
1943 /* Walk the path and open corresponding tree objects. */
1944 p = path;
1945 while (*p) {
1946 struct got_tree_entry *te;
1947 struct got_object_id *tree_id;
1948 char *te_name;
1950 while (p[0] == '/')
1951 p++;
1953 /* Ensure the correct subtree entry is selected. */
1954 slash = strchr(p, '/');
1955 if (slash == NULL)
1956 te_name = strdup(p);
1957 else
1958 te_name = strndup(p, slash - p);
1959 if (te_name == NULL) {
1960 err = got_error_from_errno("strndup");
1961 break;
1963 te = got_object_tree_find_entry(s->tree, te_name);
1964 if (te == NULL) {
1965 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1966 free(te_name);
1967 break;
1969 free(te_name);
1970 s->first_displayed_entry = s->selected_entry = te;
1972 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1973 break; /* jump to this file's entry */
1975 slash = strchr(p, '/');
1976 if (slash)
1977 subpath = strndup(path, slash - path);
1978 else
1979 subpath = strdup(path);
1980 if (subpath == NULL) {
1981 err = got_error_from_errno("strdup");
1982 break;
1985 err = got_object_id_by_path(&tree_id, s->repo, commit,
1986 subpath);
1987 if (err)
1988 break;
1990 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1991 free(tree_id);
1992 if (err)
1993 break;
1995 err = tree_view_visit_subtree(s, tree);
1996 if (err) {
1997 got_object_tree_close(tree);
1998 break;
2000 if (slash == NULL)
2001 break;
2002 free(subpath);
2003 subpath = NULL;
2004 p = slash;
2007 free(subpath);
2008 return err;
2011 static const struct got_error *
2012 browse_commit_tree(struct tog_view **new_view, int begin_x,
2013 struct commit_queue_entry *entry, const char *path,
2014 const char *head_ref_name, struct got_repository *repo)
2016 const struct got_error *err = NULL;
2017 struct tog_tree_view_state *s;
2018 struct tog_view *tree_view;
2020 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2021 if (tree_view == NULL)
2022 return got_error_from_errno("view_open");
2024 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2025 if (err)
2026 return err;
2027 s = &tree_view->state.tree;
2029 *new_view = tree_view;
2031 if (got_path_is_root_dir(path))
2032 return NULL;
2034 return tree_view_walk_path(s, entry->commit, path);
2037 static const struct got_error *
2038 block_signals_used_by_main_thread(void)
2040 sigset_t sigset;
2041 int errcode;
2043 if (sigemptyset(&sigset) == -1)
2044 return got_error_from_errno("sigemptyset");
2046 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2047 if (sigaddset(&sigset, SIGWINCH) == -1)
2048 return got_error_from_errno("sigaddset");
2049 if (sigaddset(&sigset, SIGCONT) == -1)
2050 return got_error_from_errno("sigaddset");
2051 if (sigaddset(&sigset, SIGINT) == -1)
2052 return got_error_from_errno("sigaddset");
2053 if (sigaddset(&sigset, SIGTERM) == -1)
2054 return got_error_from_errno("sigaddset");
2056 /* ncurses handles SIGTSTP */
2057 if (sigaddset(&sigset, SIGTSTP) == -1)
2058 return got_error_from_errno("sigaddset");
2060 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2061 if (errcode)
2062 return got_error_set_errno(errcode, "pthread_sigmask");
2064 return NULL;
2067 static void *
2068 log_thread(void *arg)
2070 const struct got_error *err = NULL;
2071 int errcode = 0;
2072 struct tog_log_thread_args *a = arg;
2073 int done = 0;
2075 err = block_signals_used_by_main_thread();
2076 if (err)
2077 return (void *)err;
2079 while (!done && !err && !tog_fatal_signal_received()) {
2080 err = queue_commits(a);
2081 if (err) {
2082 if (err->code != GOT_ERR_ITER_COMPLETED)
2083 return (void *)err;
2084 err = NULL;
2085 done = 1;
2086 } else if (a->commits_needed > 0 && !a->load_all)
2087 a->commits_needed--;
2089 errcode = pthread_mutex_lock(&tog_mutex);
2090 if (errcode) {
2091 err = got_error_set_errno(errcode,
2092 "pthread_mutex_lock");
2093 break;
2094 } else if (*a->quit)
2095 done = 1;
2096 else if (*a->first_displayed_entry == NULL) {
2097 *a->first_displayed_entry =
2098 TAILQ_FIRST(&a->commits->head);
2099 *a->selected_entry = *a->first_displayed_entry;
2102 errcode = pthread_cond_signal(&a->commit_loaded);
2103 if (errcode) {
2104 err = got_error_set_errno(errcode,
2105 "pthread_cond_signal");
2106 pthread_mutex_unlock(&tog_mutex);
2107 break;
2110 if (done)
2111 a->commits_needed = 0;
2112 else {
2113 if (a->commits_needed == 0 && !a->load_all) {
2114 errcode = pthread_cond_wait(&a->need_commits,
2115 &tog_mutex);
2116 if (errcode)
2117 err = got_error_set_errno(errcode,
2118 "pthread_cond_wait");
2119 if (*a->quit)
2120 done = 1;
2124 errcode = pthread_mutex_unlock(&tog_mutex);
2125 if (errcode && err == NULL)
2126 err = got_error_set_errno(errcode,
2127 "pthread_mutex_unlock");
2129 a->log_complete = 1;
2130 return (void *)err;
2133 static const struct got_error *
2134 stop_log_thread(struct tog_log_view_state *s)
2136 const struct got_error *err = NULL;
2137 int errcode;
2139 if (s->thread) {
2140 s->quit = 1;
2141 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2142 if (errcode)
2143 return got_error_set_errno(errcode,
2144 "pthread_cond_signal");
2145 errcode = pthread_mutex_unlock(&tog_mutex);
2146 if (errcode)
2147 return got_error_set_errno(errcode,
2148 "pthread_mutex_unlock");
2149 errcode = pthread_join(s->thread, (void **)&err);
2150 if (errcode)
2151 return got_error_set_errno(errcode, "pthread_join");
2152 errcode = pthread_mutex_lock(&tog_mutex);
2153 if (errcode)
2154 return got_error_set_errno(errcode,
2155 "pthread_mutex_lock");
2156 s->thread = NULL;
2159 if (s->thread_args.repo) {
2160 err = got_repo_close(s->thread_args.repo);
2161 s->thread_args.repo = NULL;
2164 if (s->thread_args.graph) {
2165 got_commit_graph_close(s->thread_args.graph);
2166 s->thread_args.graph = NULL;
2169 return err;
2172 static const struct got_error *
2173 close_log_view(struct tog_view *view)
2175 const struct got_error *err = NULL;
2176 struct tog_log_view_state *s = &view->state.log;
2177 int errcode;
2179 err = stop_log_thread(s);
2181 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2182 if (errcode && err == NULL)
2183 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2185 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2186 if (errcode && err == NULL)
2187 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2189 free_commits(&s->commits);
2190 free(s->in_repo_path);
2191 s->in_repo_path = NULL;
2192 free(s->start_id);
2193 s->start_id = NULL;
2194 free(s->head_ref_name);
2195 s->head_ref_name = NULL;
2196 return err;
2199 static const struct got_error *
2200 search_start_log_view(struct tog_view *view)
2202 struct tog_log_view_state *s = &view->state.log;
2204 s->matched_entry = NULL;
2205 s->search_entry = NULL;
2206 return NULL;
2209 static const struct got_error *
2210 search_next_log_view(struct tog_view *view)
2212 const struct got_error *err = NULL;
2213 struct tog_log_view_state *s = &view->state.log;
2214 struct commit_queue_entry *entry;
2216 /* Display progress update in log view. */
2217 show_log_view(view);
2218 update_panels();
2219 doupdate();
2221 if (s->search_entry) {
2222 int errcode, ch;
2223 errcode = pthread_mutex_unlock(&tog_mutex);
2224 if (errcode)
2225 return got_error_set_errno(errcode,
2226 "pthread_mutex_unlock");
2227 ch = wgetch(view->window);
2228 errcode = pthread_mutex_lock(&tog_mutex);
2229 if (errcode)
2230 return got_error_set_errno(errcode,
2231 "pthread_mutex_lock");
2232 if (ch == KEY_BACKSPACE) {
2233 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2234 return NULL;
2236 if (view->searching == TOG_SEARCH_FORWARD)
2237 entry = TAILQ_NEXT(s->search_entry, entry);
2238 else
2239 entry = TAILQ_PREV(s->search_entry,
2240 commit_queue_head, entry);
2241 } else if (s->matched_entry) {
2242 if (view->searching == TOG_SEARCH_FORWARD)
2243 entry = TAILQ_NEXT(s->matched_entry, entry);
2244 else
2245 entry = TAILQ_PREV(s->matched_entry,
2246 commit_queue_head, entry);
2247 } else {
2248 entry = s->selected_entry;
2251 while (1) {
2252 int have_match = 0;
2254 if (entry == NULL) {
2255 if (s->thread_args.log_complete ||
2256 view->searching == TOG_SEARCH_BACKWARD) {
2257 view->search_next_done =
2258 (s->matched_entry == NULL ?
2259 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2260 s->search_entry = NULL;
2261 return NULL;
2264 * Poke the log thread for more commits and return,
2265 * allowing the main loop to make progress. Search
2266 * will resume at s->search_entry once we come back.
2268 s->thread_args.commits_needed++;
2269 return trigger_log_thread(view, 0);
2272 err = match_commit(&have_match, entry->id, entry->commit,
2273 &view->regex);
2274 if (err)
2275 break;
2276 if (have_match) {
2277 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2278 s->matched_entry = entry;
2279 break;
2282 s->search_entry = entry;
2283 if (view->searching == TOG_SEARCH_FORWARD)
2284 entry = TAILQ_NEXT(entry, entry);
2285 else
2286 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2289 if (s->matched_entry) {
2290 int cur = s->selected_entry->idx;
2291 while (cur < s->matched_entry->idx) {
2292 err = input_log_view(NULL, view, KEY_DOWN);
2293 if (err)
2294 return err;
2295 cur++;
2297 while (cur > s->matched_entry->idx) {
2298 err = input_log_view(NULL, view, KEY_UP);
2299 if (err)
2300 return err;
2301 cur--;
2305 s->search_entry = NULL;
2307 return NULL;
2310 static const struct got_error *
2311 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2312 struct got_repository *repo, const char *head_ref_name,
2313 const char *in_repo_path, int log_branches)
2315 const struct got_error *err = NULL;
2316 struct tog_log_view_state *s = &view->state.log;
2317 struct got_repository *thread_repo = NULL;
2318 struct got_commit_graph *thread_graph = NULL;
2319 int errcode;
2321 if (in_repo_path != s->in_repo_path) {
2322 free(s->in_repo_path);
2323 s->in_repo_path = strdup(in_repo_path);
2324 if (s->in_repo_path == NULL)
2325 return got_error_from_errno("strdup");
2328 /* The commit queue only contains commits being displayed. */
2329 TAILQ_INIT(&s->commits.head);
2330 s->commits.ncommits = 0;
2332 s->repo = repo;
2333 if (head_ref_name) {
2334 s->head_ref_name = strdup(head_ref_name);
2335 if (s->head_ref_name == NULL) {
2336 err = got_error_from_errno("strdup");
2337 goto done;
2340 s->start_id = got_object_id_dup(start_id);
2341 if (s->start_id == NULL) {
2342 err = got_error_from_errno("got_object_id_dup");
2343 goto done;
2345 s->log_branches = log_branches;
2347 STAILQ_INIT(&s->colors);
2348 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2349 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2350 get_color_value("TOG_COLOR_COMMIT"));
2351 if (err)
2352 goto done;
2353 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2354 get_color_value("TOG_COLOR_AUTHOR"));
2355 if (err) {
2356 free_colors(&s->colors);
2357 goto done;
2359 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2360 get_color_value("TOG_COLOR_DATE"));
2361 if (err) {
2362 free_colors(&s->colors);
2363 goto done;
2367 view->show = show_log_view;
2368 view->input = input_log_view;
2369 view->close = close_log_view;
2370 view->search_start = search_start_log_view;
2371 view->search_next = search_next_log_view;
2373 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2374 if (err)
2375 goto done;
2376 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2377 !s->log_branches);
2378 if (err)
2379 goto done;
2380 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2381 s->repo, NULL, NULL);
2382 if (err)
2383 goto done;
2385 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2386 if (errcode) {
2387 err = got_error_set_errno(errcode, "pthread_cond_init");
2388 goto done;
2390 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2391 if (errcode) {
2392 err = got_error_set_errno(errcode, "pthread_cond_init");
2393 goto done;
2396 s->thread_args.commits_needed = view->nlines;
2397 s->thread_args.graph = thread_graph;
2398 s->thread_args.commits = &s->commits;
2399 s->thread_args.in_repo_path = s->in_repo_path;
2400 s->thread_args.start_id = s->start_id;
2401 s->thread_args.repo = thread_repo;
2402 s->thread_args.log_complete = 0;
2403 s->thread_args.quit = &s->quit;
2404 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2405 s->thread_args.selected_entry = &s->selected_entry;
2406 s->thread_args.searching = &view->searching;
2407 s->thread_args.search_next_done = &view->search_next_done;
2408 s->thread_args.regex = &view->regex;
2409 done:
2410 if (err)
2411 close_log_view(view);
2412 return err;
2415 static const struct got_error *
2416 show_log_view(struct tog_view *view)
2418 const struct got_error *err;
2419 struct tog_log_view_state *s = &view->state.log;
2421 if (s->thread == NULL) {
2422 int errcode = pthread_create(&s->thread, NULL, log_thread,
2423 &s->thread_args);
2424 if (errcode)
2425 return got_error_set_errno(errcode, "pthread_create");
2426 if (s->thread_args.commits_needed > 0) {
2427 err = trigger_log_thread(view, 1);
2428 if (err)
2429 return err;
2433 return draw_commits(view);
2436 static const struct got_error *
2437 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2439 const struct got_error *err = NULL;
2440 struct tog_log_view_state *s = &view->state.log;
2441 struct tog_view *diff_view = NULL, *tree_view = NULL;
2442 struct tog_view *ref_view = NULL;
2443 struct commit_queue_entry *entry;
2444 int begin_x = 0, n;
2446 if (s->thread_args.load_all) {
2447 if (ch == KEY_BACKSPACE)
2448 s->thread_args.load_all = 0;
2449 else if (s->thread_args.log_complete) {
2450 s->thread_args.load_all = 0;
2451 log_scroll_down(view, s->commits.ncommits);
2452 s->selected = MIN(view->nlines - 2,
2453 s->commits.ncommits - 1);
2454 select_commit(s);
2456 return NULL;
2459 switch (ch) {
2460 case 'q':
2461 s->quit = 1;
2462 break;
2463 case 'k':
2464 case KEY_UP:
2465 case '<':
2466 case ',':
2467 case CTRL('p'):
2468 if (s->first_displayed_entry == NULL)
2469 break;
2470 if (s->selected > 0)
2471 s->selected--;
2472 else
2473 log_scroll_up(s, 1);
2474 select_commit(s);
2475 break;
2476 case 'g':
2477 case KEY_HOME:
2478 s->selected = 0;
2479 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2480 select_commit(s);
2481 break;
2482 case KEY_PPAGE:
2483 case CTRL('b'):
2484 case CTRL('u'):
2485 case 'u':
2486 if (s->first_displayed_entry == NULL)
2487 break;
2488 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2489 s->selected = 0;
2490 else
2491 log_scroll_up(s, view->nlines - 1);
2492 select_commit(s);
2493 break;
2494 case 'j':
2495 case KEY_DOWN:
2496 case '>':
2497 case '.':
2498 case CTRL('n'):
2499 if (s->first_displayed_entry == NULL)
2500 break;
2501 if (s->selected < MIN(view->nlines - 2,
2502 s->commits.ncommits - 1))
2503 s->selected++;
2504 else {
2505 err = log_scroll_down(view, 1);
2506 if (err)
2507 break;
2509 select_commit(s);
2510 break;
2511 case 'G':
2512 case KEY_END: {
2513 /* We don't know yet how many commits, so we're forced to
2514 * traverse them all. */
2515 if (!s->thread_args.log_complete) {
2516 s->thread_args.load_all = 1;
2517 return trigger_log_thread(view, 0);
2520 s->selected = 0;
2521 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2522 for (n = 0; n < view->nlines - 1; n++) {
2523 if (entry == NULL)
2524 break;
2525 s->first_displayed_entry = entry;
2526 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2528 if (n > 0)
2529 s->selected = n - 1;
2530 select_commit(s);
2531 break;
2533 case KEY_NPAGE:
2534 case CTRL('f'):
2535 case CTRL('d'):
2536 case 'd': {
2537 struct commit_queue_entry *first;
2538 first = s->first_displayed_entry;
2539 if (first == NULL)
2540 break;
2541 err = log_scroll_down(view, view->nlines - 1);
2542 if (err)
2543 break;
2544 if (first == s->first_displayed_entry &&
2545 s->selected < MIN(view->nlines - 2,
2546 s->commits.ncommits - 1)) {
2547 /* can't scroll further down */
2548 s->selected = MIN(view->nlines - 2,
2549 s->commits.ncommits - 1);
2551 select_commit(s);
2552 break;
2554 case KEY_RESIZE:
2555 if (s->selected > view->nlines - 2)
2556 s->selected = view->nlines - 2;
2557 if (s->selected > s->commits.ncommits - 1)
2558 s->selected = s->commits.ncommits - 1;
2559 select_commit(s);
2560 if (s->commits.ncommits < view->nlines - 1 &&
2561 !s->thread_args.log_complete) {
2562 s->thread_args.commits_needed += (view->nlines - 1) -
2563 s->commits.ncommits;
2564 err = trigger_log_thread(view, 1);
2566 break;
2567 case KEY_ENTER:
2568 case ' ':
2569 case '\r':
2570 if (s->selected_entry == NULL)
2571 break;
2572 if (view_is_parent_view(view))
2573 begin_x = view_split_begin_x(view->begin_x);
2574 err = open_diff_view_for_commit(&diff_view, begin_x,
2575 s->selected_entry->commit, s->selected_entry->id,
2576 view, s->repo);
2577 if (err)
2578 break;
2579 view->focussed = 0;
2580 diff_view->focussed = 1;
2581 if (view_is_parent_view(view)) {
2582 err = view_close_child(view);
2583 if (err)
2584 return err;
2585 view_set_child(view, diff_view);
2586 view->focus_child = 1;
2587 } else
2588 *new_view = diff_view;
2589 break;
2590 case 't':
2591 if (s->selected_entry == NULL)
2592 break;
2593 if (view_is_parent_view(view))
2594 begin_x = view_split_begin_x(view->begin_x);
2595 err = browse_commit_tree(&tree_view, begin_x,
2596 s->selected_entry, s->in_repo_path, s->head_ref_name,
2597 s->repo);
2598 if (err)
2599 break;
2600 view->focussed = 0;
2601 tree_view->focussed = 1;
2602 if (view_is_parent_view(view)) {
2603 err = view_close_child(view);
2604 if (err)
2605 return err;
2606 view_set_child(view, tree_view);
2607 view->focus_child = 1;
2608 } else
2609 *new_view = tree_view;
2610 break;
2611 case KEY_BACKSPACE:
2612 case CTRL('l'):
2613 case 'B':
2614 if (ch == KEY_BACKSPACE &&
2615 got_path_is_root_dir(s->in_repo_path))
2616 break;
2617 err = stop_log_thread(s);
2618 if (err)
2619 return err;
2620 if (ch == KEY_BACKSPACE) {
2621 char *parent_path;
2622 err = got_path_dirname(&parent_path, s->in_repo_path);
2623 if (err)
2624 return err;
2625 free(s->in_repo_path);
2626 s->in_repo_path = parent_path;
2627 s->thread_args.in_repo_path = s->in_repo_path;
2628 } else if (ch == CTRL('l')) {
2629 struct got_object_id *start_id;
2630 err = got_repo_match_object_id(&start_id, NULL,
2631 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2632 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2633 if (err)
2634 return err;
2635 free(s->start_id);
2636 s->start_id = start_id;
2637 s->thread_args.start_id = s->start_id;
2638 } else /* 'B' */
2639 s->log_branches = !s->log_branches;
2641 err = got_repo_open(&s->thread_args.repo,
2642 got_repo_get_path(s->repo), NULL);
2643 if (err)
2644 return err;
2645 tog_free_refs();
2646 err = tog_load_refs(s->repo, 0);
2647 if (err)
2648 return err;
2649 err = got_commit_graph_open(&s->thread_args.graph,
2650 s->in_repo_path, !s->log_branches);
2651 if (err)
2652 return err;
2653 err = got_commit_graph_iter_start(s->thread_args.graph,
2654 s->start_id, s->repo, NULL, NULL);
2655 if (err)
2656 return err;
2657 free_commits(&s->commits);
2658 s->first_displayed_entry = NULL;
2659 s->last_displayed_entry = NULL;
2660 s->selected_entry = NULL;
2661 s->selected = 0;
2662 s->thread_args.log_complete = 0;
2663 s->quit = 0;
2664 s->thread_args.commits_needed = view->nlines;
2665 break;
2666 case 'r':
2667 if (view_is_parent_view(view))
2668 begin_x = view_split_begin_x(view->begin_x);
2669 ref_view = view_open(view->nlines, view->ncols,
2670 view->begin_y, begin_x, TOG_VIEW_REF);
2671 if (ref_view == NULL)
2672 return got_error_from_errno("view_open");
2673 err = open_ref_view(ref_view, s->repo);
2674 if (err) {
2675 view_close(ref_view);
2676 return err;
2678 view->focussed = 0;
2679 ref_view->focussed = 1;
2680 if (view_is_parent_view(view)) {
2681 err = view_close_child(view);
2682 if (err)
2683 return err;
2684 view_set_child(view, ref_view);
2685 view->focus_child = 1;
2686 } else
2687 *new_view = ref_view;
2688 break;
2689 default:
2690 break;
2693 return err;
2696 static const struct got_error *
2697 apply_unveil(const char *repo_path, const char *worktree_path)
2699 const struct got_error *error;
2701 #ifdef PROFILE
2702 if (unveil("gmon.out", "rwc") != 0)
2703 return got_error_from_errno2("unveil", "gmon.out");
2704 #endif
2705 if (repo_path && unveil(repo_path, "r") != 0)
2706 return got_error_from_errno2("unveil", repo_path);
2708 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2709 return got_error_from_errno2("unveil", worktree_path);
2711 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2712 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2714 error = got_privsep_unveil_exec_helpers();
2715 if (error != NULL)
2716 return error;
2718 if (unveil(NULL, NULL) != 0)
2719 return got_error_from_errno("unveil");
2721 return NULL;
2724 static void
2725 init_curses(void)
2728 * Override default signal handlers before starting ncurses.
2729 * This should prevent ncurses from installing its own
2730 * broken cleanup() signal handler.
2732 signal(SIGWINCH, tog_sigwinch);
2733 signal(SIGPIPE, tog_sigpipe);
2734 signal(SIGCONT, tog_sigcont);
2735 signal(SIGINT, tog_sigint);
2736 signal(SIGTERM, tog_sigterm);
2738 initscr();
2739 cbreak();
2740 halfdelay(1); /* Do fast refresh while initial view is loading. */
2741 noecho();
2742 nonl();
2743 intrflush(stdscr, FALSE);
2744 keypad(stdscr, TRUE);
2745 curs_set(0);
2746 if (getenv("TOG_COLORS") != NULL) {
2747 start_color();
2748 use_default_colors();
2752 static const struct got_error *
2753 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2754 struct got_repository *repo, struct got_worktree *worktree)
2756 const struct got_error *err = NULL;
2758 if (argc == 0) {
2759 *in_repo_path = strdup("/");
2760 if (*in_repo_path == NULL)
2761 return got_error_from_errno("strdup");
2762 return NULL;
2765 if (worktree) {
2766 const char *prefix = got_worktree_get_path_prefix(worktree);
2767 char *p;
2769 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2770 if (err)
2771 return err;
2772 if (asprintf(in_repo_path, "%s%s%s", prefix,
2773 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2774 p) == -1) {
2775 err = got_error_from_errno("asprintf");
2776 *in_repo_path = NULL;
2778 free(p);
2779 } else
2780 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2782 return err;
2785 static const struct got_error *
2786 cmd_log(int argc, char *argv[])
2788 const struct got_error *error;
2789 struct got_repository *repo = NULL;
2790 struct got_worktree *worktree = NULL;
2791 struct got_object_id *start_id = NULL;
2792 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2793 char *start_commit = NULL, *label = NULL;
2794 struct got_reference *ref = NULL;
2795 const char *head_ref_name = NULL;
2796 int ch, log_branches = 0;
2797 struct tog_view *view;
2799 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2800 switch (ch) {
2801 case 'b':
2802 log_branches = 1;
2803 break;
2804 case 'c':
2805 start_commit = optarg;
2806 break;
2807 case 'r':
2808 repo_path = realpath(optarg, NULL);
2809 if (repo_path == NULL)
2810 return got_error_from_errno2("realpath",
2811 optarg);
2812 break;
2813 default:
2814 usage_log();
2815 /* NOTREACHED */
2819 argc -= optind;
2820 argv += optind;
2822 if (argc > 1)
2823 usage_log();
2825 if (repo_path == NULL) {
2826 cwd = getcwd(NULL, 0);
2827 if (cwd == NULL)
2828 return got_error_from_errno("getcwd");
2829 error = got_worktree_open(&worktree, cwd);
2830 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2831 goto done;
2832 if (worktree)
2833 repo_path =
2834 strdup(got_worktree_get_repo_path(worktree));
2835 else
2836 repo_path = strdup(cwd);
2837 if (repo_path == NULL) {
2838 error = got_error_from_errno("strdup");
2839 goto done;
2843 error = got_repo_open(&repo, repo_path, NULL);
2844 if (error != NULL)
2845 goto done;
2847 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2848 repo, worktree);
2849 if (error)
2850 goto done;
2852 init_curses();
2854 error = apply_unveil(got_repo_get_path(repo),
2855 worktree ? got_worktree_get_root_path(worktree) : NULL);
2856 if (error)
2857 goto done;
2859 /* already loaded by tog_log_with_path()? */
2860 if (TAILQ_EMPTY(&tog_refs)) {
2861 error = tog_load_refs(repo, 0);
2862 if (error)
2863 goto done;
2866 if (start_commit == NULL) {
2867 error = got_repo_match_object_id(&start_id, &label,
2868 worktree ? got_worktree_get_head_ref_name(worktree) :
2869 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2870 if (error)
2871 goto done;
2872 head_ref_name = label;
2873 } else {
2874 error = got_ref_open(&ref, repo, start_commit, 0);
2875 if (error == NULL)
2876 head_ref_name = got_ref_get_name(ref);
2877 else if (error->code != GOT_ERR_NOT_REF)
2878 goto done;
2879 error = got_repo_match_object_id(&start_id, NULL,
2880 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2881 if (error)
2882 goto done;
2885 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2886 if (view == NULL) {
2887 error = got_error_from_errno("view_open");
2888 goto done;
2890 error = open_log_view(view, start_id, repo, head_ref_name,
2891 in_repo_path, log_branches);
2892 if (error)
2893 goto done;
2894 if (worktree) {
2895 /* Release work tree lock. */
2896 got_worktree_close(worktree);
2897 worktree = NULL;
2899 error = view_loop(view);
2900 done:
2901 free(in_repo_path);
2902 free(repo_path);
2903 free(cwd);
2904 free(start_id);
2905 free(label);
2906 if (ref)
2907 got_ref_close(ref);
2908 if (repo) {
2909 const struct got_error *close_err = got_repo_close(repo);
2910 if (error == NULL)
2911 error = close_err;
2913 if (worktree)
2914 got_worktree_close(worktree);
2915 tog_free_refs();
2916 return error;
2919 __dead static void
2920 usage_diff(void)
2922 endwin();
2923 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2924 "[-w] object1 object2\n", getprogname());
2925 exit(1);
2928 static int
2929 match_line(const char *line, regex_t *regex, size_t nmatch,
2930 regmatch_t *regmatch)
2932 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2935 struct tog_color *
2936 match_color(struct tog_colors *colors, const char *line)
2938 struct tog_color *tc = NULL;
2940 STAILQ_FOREACH(tc, colors, entry) {
2941 if (match_line(line, &tc->regex, 0, NULL))
2942 return tc;
2945 return NULL;
2948 static const struct got_error *
2949 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2950 WINDOW *window, regmatch_t *regmatch)
2952 const struct got_error *err = NULL;
2953 wchar_t *wline;
2954 int width;
2955 char *s;
2957 *wtotal = 0;
2959 s = strndup(line, regmatch->rm_so);
2960 if (s == NULL)
2961 return got_error_from_errno("strndup");
2963 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2964 if (err) {
2965 free(s);
2966 return err;
2968 waddwstr(window, wline);
2969 free(wline);
2970 free(s);
2971 wlimit -= width;
2972 *wtotal += width;
2974 if (wlimit > 0) {
2975 s = strndup(line + regmatch->rm_so,
2976 regmatch->rm_eo - regmatch->rm_so);
2977 if (s == NULL) {
2978 err = got_error_from_errno("strndup");
2979 free(s);
2980 return err;
2982 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2983 if (err) {
2984 free(s);
2985 return err;
2987 wattr_on(window, A_STANDOUT, NULL);
2988 waddwstr(window, wline);
2989 wattr_off(window, A_STANDOUT, NULL);
2990 free(wline);
2991 free(s);
2992 wlimit -= width;
2993 *wtotal += width;
2996 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2997 err = format_line(&wline, &width,
2998 line + regmatch->rm_eo, wlimit, col_tab_align);
2999 if (err)
3000 return err;
3001 waddwstr(window, wline);
3002 free(wline);
3003 *wtotal += width;
3006 return NULL;
3009 static const struct got_error *
3010 draw_file(struct tog_view *view, const char *header)
3012 struct tog_diff_view_state *s = &view->state.diff;
3013 regmatch_t *regmatch = &view->regmatch;
3014 const struct got_error *err;
3015 int nprinted = 0;
3016 char *line;
3017 size_t linesize = 0;
3018 ssize_t linelen;
3019 struct tog_color *tc;
3020 wchar_t *wline;
3021 int width;
3022 int max_lines = view->nlines;
3023 int nlines = s->nlines;
3024 off_t line_offset;
3026 line_offset = s->line_offsets[s->first_displayed_line - 1];
3027 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3028 return got_error_from_errno("fseek");
3030 werase(view->window);
3032 if (header) {
3033 if (asprintf(&line, "[%d/%d] %s",
3034 s->first_displayed_line - 1 + s->selected_line, nlines,
3035 header) == -1)
3036 return got_error_from_errno("asprintf");
3037 err = format_line(&wline, &width, line, view->ncols, 0);
3038 free(line);
3039 if (err)
3040 return err;
3042 if (view_needs_focus_indication(view))
3043 wstandout(view->window);
3044 waddwstr(view->window, wline);
3045 free(wline);
3046 wline = NULL;
3047 if (view_needs_focus_indication(view))
3048 wstandend(view->window);
3049 if (width <= view->ncols - 1)
3050 waddch(view->window, '\n');
3052 if (max_lines <= 1)
3053 return NULL;
3054 max_lines--;
3057 s->eof = 0;
3058 line = NULL;
3059 while (max_lines > 0 && nprinted < max_lines) {
3060 linelen = getline(&line, &linesize, s->f);
3061 if (linelen == -1) {
3062 if (feof(s->f)) {
3063 s->eof = 1;
3064 break;
3066 free(line);
3067 return got_ferror(s->f, GOT_ERR_IO);
3070 tc = match_color(&s->colors, line);
3071 if (tc)
3072 wattr_on(view->window,
3073 COLOR_PAIR(tc->colorpair), NULL);
3074 if (s->first_displayed_line + nprinted == s->matched_line &&
3075 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3076 err = add_matched_line(&width, line, view->ncols, 0,
3077 view->window, regmatch);
3078 if (err) {
3079 free(line);
3080 return err;
3082 } else {
3083 err = format_line(&wline, &width, line, view->ncols, 0);
3084 if (err) {
3085 free(line);
3086 return err;
3088 waddwstr(view->window, wline);
3089 free(wline);
3090 wline = NULL;
3092 if (tc)
3093 wattr_off(view->window,
3094 COLOR_PAIR(tc->colorpair), NULL);
3095 if (width <= view->ncols - 1)
3096 waddch(view->window, '\n');
3097 nprinted++;
3099 free(line);
3100 if (nprinted >= 1)
3101 s->last_displayed_line = s->first_displayed_line +
3102 (nprinted - 1);
3103 else
3104 s->last_displayed_line = s->first_displayed_line;
3106 view_vborder(view);
3108 if (s->eof) {
3109 while (nprinted < view->nlines) {
3110 waddch(view->window, '\n');
3111 nprinted++;
3114 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3115 if (err) {
3116 return err;
3119 wstandout(view->window);
3120 waddwstr(view->window, wline);
3121 free(wline);
3122 wline = NULL;
3123 wstandend(view->window);
3126 return NULL;
3129 static char *
3130 get_datestr(time_t *time, char *datebuf)
3132 struct tm mytm, *tm;
3133 char *p, *s;
3135 tm = gmtime_r(time, &mytm);
3136 if (tm == NULL)
3137 return NULL;
3138 s = asctime_r(tm, datebuf);
3139 if (s == NULL)
3140 return NULL;
3141 p = strchr(s, '\n');
3142 if (p)
3143 *p = '\0';
3144 return s;
3147 static const struct got_error *
3148 get_changed_paths(struct got_pathlist_head *paths,
3149 struct got_commit_object *commit, struct got_repository *repo)
3151 const struct got_error *err = NULL;
3152 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3153 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3154 struct got_object_qid *qid;
3156 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3157 if (qid != NULL) {
3158 struct got_commit_object *pcommit;
3159 err = got_object_open_as_commit(&pcommit, repo,
3160 &qid->id);
3161 if (err)
3162 return err;
3164 tree_id1 = got_object_id_dup(
3165 got_object_commit_get_tree_id(pcommit));
3166 if (tree_id1 == NULL) {
3167 got_object_commit_close(pcommit);
3168 return got_error_from_errno("got_object_id_dup");
3170 got_object_commit_close(pcommit);
3174 if (tree_id1) {
3175 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3176 if (err)
3177 goto done;
3180 tree_id2 = got_object_commit_get_tree_id(commit);
3181 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3182 if (err)
3183 goto done;
3185 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3186 got_diff_tree_collect_changed_paths, paths, 0);
3187 done:
3188 if (tree1)
3189 got_object_tree_close(tree1);
3190 if (tree2)
3191 got_object_tree_close(tree2);
3192 free(tree_id1);
3193 return err;
3196 static const struct got_error *
3197 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3199 off_t *p;
3201 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3202 if (p == NULL)
3203 return got_error_from_errno("reallocarray");
3204 *line_offsets = p;
3205 (*line_offsets)[*nlines] = off;
3206 (*nlines)++;
3207 return NULL;
3210 static const struct got_error *
3211 write_commit_info(off_t **line_offsets, size_t *nlines,
3212 struct got_object_id *commit_id, struct got_reflist_head *refs,
3213 struct got_repository *repo, FILE *outfile)
3215 const struct got_error *err = NULL;
3216 char datebuf[26], *datestr;
3217 struct got_commit_object *commit;
3218 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3219 time_t committer_time;
3220 const char *author, *committer;
3221 char *refs_str = NULL;
3222 struct got_pathlist_head changed_paths;
3223 struct got_pathlist_entry *pe;
3224 off_t outoff = 0;
3225 int n;
3227 TAILQ_INIT(&changed_paths);
3229 if (refs) {
3230 err = build_refs_str(&refs_str, refs, commit_id, repo);
3231 if (err)
3232 return err;
3235 err = got_object_open_as_commit(&commit, repo, commit_id);
3236 if (err)
3237 return err;
3239 err = got_object_id_str(&id_str, commit_id);
3240 if (err) {
3241 err = got_error_from_errno("got_object_id_str");
3242 goto done;
3245 err = add_line_offset(line_offsets, nlines, 0);
3246 if (err)
3247 goto done;
3249 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3250 refs_str ? refs_str : "", refs_str ? ")" : "");
3251 if (n < 0) {
3252 err = got_error_from_errno("fprintf");
3253 goto done;
3255 outoff += n;
3256 err = add_line_offset(line_offsets, nlines, outoff);
3257 if (err)
3258 goto done;
3260 n = fprintf(outfile, "from: %s\n",
3261 got_object_commit_get_author(commit));
3262 if (n < 0) {
3263 err = got_error_from_errno("fprintf");
3264 goto done;
3266 outoff += n;
3267 err = add_line_offset(line_offsets, nlines, outoff);
3268 if (err)
3269 goto done;
3271 committer_time = got_object_commit_get_committer_time(commit);
3272 datestr = get_datestr(&committer_time, datebuf);
3273 if (datestr) {
3274 n = fprintf(outfile, "date: %s UTC\n", datestr);
3275 if (n < 0) {
3276 err = got_error_from_errno("fprintf");
3277 goto done;
3279 outoff += n;
3280 err = add_line_offset(line_offsets, nlines, outoff);
3281 if (err)
3282 goto done;
3284 author = got_object_commit_get_author(commit);
3285 committer = got_object_commit_get_committer(commit);
3286 if (strcmp(author, committer) != 0) {
3287 n = fprintf(outfile, "via: %s\n", committer);
3288 if (n < 0) {
3289 err = got_error_from_errno("fprintf");
3290 goto done;
3292 outoff += n;
3293 err = add_line_offset(line_offsets, nlines, outoff);
3294 if (err)
3295 goto done;
3297 if (got_object_commit_get_nparents(commit) > 1) {
3298 const struct got_object_id_queue *parent_ids;
3299 struct got_object_qid *qid;
3300 int pn = 1;
3301 parent_ids = got_object_commit_get_parent_ids(commit);
3302 STAILQ_FOREACH(qid, parent_ids, entry) {
3303 err = got_object_id_str(&id_str, &qid->id);
3304 if (err)
3305 goto done;
3306 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
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;
3315 free(id_str);
3316 id_str = NULL;
3320 err = got_object_commit_get_logmsg(&logmsg, commit);
3321 if (err)
3322 goto done;
3323 s = logmsg;
3324 while ((line = strsep(&s, "\n")) != NULL) {
3325 n = fprintf(outfile, "%s\n", line);
3326 if (n < 0) {
3327 err = got_error_from_errno("fprintf");
3328 goto done;
3330 outoff += n;
3331 err = add_line_offset(line_offsets, nlines, outoff);
3332 if (err)
3333 goto done;
3336 err = get_changed_paths(&changed_paths, commit, repo);
3337 if (err)
3338 goto done;
3339 TAILQ_FOREACH(pe, &changed_paths, entry) {
3340 struct got_diff_changed_path *cp = pe->data;
3341 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3342 if (n < 0) {
3343 err = got_error_from_errno("fprintf");
3344 goto done;
3346 outoff += n;
3347 err = add_line_offset(line_offsets, nlines, outoff);
3348 if (err)
3349 goto done;
3350 free((char *)pe->path);
3351 free(pe->data);
3354 fputc('\n', outfile);
3355 outoff++;
3356 err = add_line_offset(line_offsets, nlines, outoff);
3357 done:
3358 got_pathlist_free(&changed_paths);
3359 free(id_str);
3360 free(logmsg);
3361 free(refs_str);
3362 got_object_commit_close(commit);
3363 if (err) {
3364 free(*line_offsets);
3365 *line_offsets = NULL;
3366 *nlines = 0;
3368 return err;
3371 static const struct got_error *
3372 create_diff(struct tog_diff_view_state *s)
3374 const struct got_error *err = NULL;
3375 FILE *f = NULL;
3376 int obj_type;
3378 free(s->line_offsets);
3379 s->line_offsets = malloc(sizeof(off_t));
3380 if (s->line_offsets == NULL)
3381 return got_error_from_errno("malloc");
3382 s->nlines = 0;
3384 f = got_opentemp();
3385 if (f == NULL) {
3386 err = got_error_from_errno("got_opentemp");
3387 goto done;
3389 if (s->f && fclose(s->f) == EOF) {
3390 err = got_error_from_errno("fclose");
3391 goto done;
3393 s->f = f;
3395 if (s->id1)
3396 err = got_object_get_type(&obj_type, s->repo, s->id1);
3397 else
3398 err = got_object_get_type(&obj_type, s->repo, s->id2);
3399 if (err)
3400 goto done;
3402 switch (obj_type) {
3403 case GOT_OBJ_TYPE_BLOB:
3404 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3405 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3406 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3407 s->repo, s->f);
3408 break;
3409 case GOT_OBJ_TYPE_TREE:
3410 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3411 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3412 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3413 break;
3414 case GOT_OBJ_TYPE_COMMIT: {
3415 const struct got_object_id_queue *parent_ids;
3416 struct got_object_qid *pid;
3417 struct got_commit_object *commit2;
3418 struct got_reflist_head *refs;
3420 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3421 if (err)
3422 goto done;
3423 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3424 /* Show commit info if we're diffing to a parent/root commit. */
3425 if (s->id1 == NULL) {
3426 err = write_commit_info(&s->line_offsets, &s->nlines,
3427 s->id2, refs, s->repo, s->f);
3428 if (err)
3429 goto done;
3430 } else {
3431 parent_ids = got_object_commit_get_parent_ids(commit2);
3432 STAILQ_FOREACH(pid, parent_ids, entry) {
3433 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3434 err = write_commit_info(
3435 &s->line_offsets, &s->nlines,
3436 s->id2, refs, s->repo, s->f);
3437 if (err)
3438 goto done;
3439 break;
3443 got_object_commit_close(commit2);
3445 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3446 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3447 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3448 break;
3450 default:
3451 err = got_error(GOT_ERR_OBJ_TYPE);
3452 break;
3454 if (err)
3455 goto done;
3456 done:
3457 if (s->f && fflush(s->f) != 0 && err == NULL)
3458 err = got_error_from_errno("fflush");
3459 return err;
3462 static void
3463 diff_view_indicate_progress(struct tog_view *view)
3465 mvwaddstr(view->window, 0, 0, "diffing...");
3466 update_panels();
3467 doupdate();
3470 static const struct got_error *
3471 search_start_diff_view(struct tog_view *view)
3473 struct tog_diff_view_state *s = &view->state.diff;
3475 s->matched_line = 0;
3476 return NULL;
3479 static const struct got_error *
3480 search_next_diff_view(struct tog_view *view)
3482 struct tog_diff_view_state *s = &view->state.diff;
3483 int lineno;
3484 char *line = NULL;
3485 size_t linesize = 0;
3486 ssize_t linelen;
3488 if (!view->searching) {
3489 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3490 return NULL;
3493 if (s->matched_line) {
3494 if (view->searching == TOG_SEARCH_FORWARD)
3495 lineno = s->matched_line + 1;
3496 else
3497 lineno = s->matched_line - 1;
3498 } else
3499 lineno = s->first_displayed_line;
3501 while (1) {
3502 off_t offset;
3504 if (lineno <= 0 || lineno > s->nlines) {
3505 if (s->matched_line == 0) {
3506 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3507 break;
3510 if (view->searching == TOG_SEARCH_FORWARD)
3511 lineno = 1;
3512 else
3513 lineno = s->nlines;
3516 offset = s->line_offsets[lineno - 1];
3517 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3518 free(line);
3519 return got_error_from_errno("fseeko");
3521 linelen = getline(&line, &linesize, s->f);
3522 if (linelen != -1 &&
3523 match_line(line, &view->regex, 1, &view->regmatch)) {
3524 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3525 s->matched_line = lineno;
3526 break;
3528 if (view->searching == TOG_SEARCH_FORWARD)
3529 lineno++;
3530 else
3531 lineno--;
3533 free(line);
3535 if (s->matched_line) {
3536 s->first_displayed_line = s->matched_line;
3537 s->selected_line = 1;
3540 return NULL;
3543 static const struct got_error *
3544 close_diff_view(struct tog_view *view)
3546 const struct got_error *err = NULL;
3547 struct tog_diff_view_state *s = &view->state.diff;
3549 free(s->id1);
3550 s->id1 = NULL;
3551 free(s->id2);
3552 s->id2 = NULL;
3553 if (s->f && fclose(s->f) == EOF)
3554 err = got_error_from_errno("fclose");
3555 s->f = NULL;
3556 if (s->f1 && fclose(s->f1) == EOF)
3557 err = got_error_from_errno("fclose");
3558 s->f1 = NULL;
3559 if (s->f2 && fclose(s->f2) == EOF)
3560 err = got_error_from_errno("fclose");
3561 s->f2 = NULL;
3562 free_colors(&s->colors);
3563 free(s->line_offsets);
3564 s->line_offsets = NULL;
3565 s->nlines = 0;
3566 return err;
3569 static const struct got_error *
3570 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3571 struct got_object_id *id2, const char *label1, const char *label2,
3572 int diff_context, int ignore_whitespace, int force_text_diff,
3573 struct tog_view *log_view, struct got_repository *repo)
3575 const struct got_error *err;
3576 struct tog_diff_view_state *s = &view->state.diff;
3578 memset(s, 0, sizeof(*s));
3580 if (id1 != NULL && id2 != NULL) {
3581 int type1, type2;
3582 err = got_object_get_type(&type1, repo, id1);
3583 if (err)
3584 return err;
3585 err = got_object_get_type(&type2, repo, id2);
3586 if (err)
3587 return err;
3589 if (type1 != type2)
3590 return got_error(GOT_ERR_OBJ_TYPE);
3592 s->first_displayed_line = 1;
3593 s->last_displayed_line = view->nlines;
3594 s->selected_line = 1;
3595 s->repo = repo;
3596 s->id1 = id1;
3597 s->id2 = id2;
3598 s->label1 = label1;
3599 s->label2 = label2;
3601 if (id1) {
3602 s->id1 = got_object_id_dup(id1);
3603 if (s->id1 == NULL)
3604 return got_error_from_errno("got_object_id_dup");
3605 s->f1 = got_opentemp();
3606 if (s->f1 == NULL) {
3607 err = got_error_from_errno("got_opentemp");
3608 goto done;
3610 } else
3611 s->id1 = NULL;
3613 s->id2 = got_object_id_dup(id2);
3614 if (s->id2 == NULL) {
3615 err = got_error_from_errno("got_object_id_dup");
3616 goto done;
3619 s->f2 = got_opentemp();
3620 if (s->f2 == NULL) {
3621 err = got_error_from_errno("got_opentemp");
3622 goto done;
3625 s->first_displayed_line = 1;
3626 s->last_displayed_line = view->nlines;
3627 s->diff_context = diff_context;
3628 s->ignore_whitespace = ignore_whitespace;
3629 s->force_text_diff = force_text_diff;
3630 s->log_view = log_view;
3631 s->repo = repo;
3633 STAILQ_INIT(&s->colors);
3634 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3635 err = add_color(&s->colors,
3636 "^-", TOG_COLOR_DIFF_MINUS,
3637 get_color_value("TOG_COLOR_DIFF_MINUS"));
3638 if (err)
3639 goto done;
3640 err = add_color(&s->colors, "^\\+",
3641 TOG_COLOR_DIFF_PLUS,
3642 get_color_value("TOG_COLOR_DIFF_PLUS"));
3643 if (err)
3644 goto done;
3645 err = add_color(&s->colors,
3646 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3647 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3648 if (err)
3649 goto done;
3651 err = add_color(&s->colors,
3652 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3653 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3654 get_color_value("TOG_COLOR_DIFF_META"));
3655 if (err)
3656 goto done;
3658 err = add_color(&s->colors,
3659 "^(from|via): ", TOG_COLOR_AUTHOR,
3660 get_color_value("TOG_COLOR_AUTHOR"));
3661 if (err)
3662 goto done;
3664 err = add_color(&s->colors,
3665 "^date: ", TOG_COLOR_DATE,
3666 get_color_value("TOG_COLOR_DATE"));
3667 if (err)
3668 goto done;
3671 if (log_view && view_is_splitscreen(view))
3672 show_log_view(log_view); /* draw vborder */
3673 diff_view_indicate_progress(view);
3675 err = create_diff(s);
3677 view->show = show_diff_view;
3678 view->input = input_diff_view;
3679 view->close = close_diff_view;
3680 view->search_start = search_start_diff_view;
3681 view->search_next = search_next_diff_view;
3682 done:
3683 if (err)
3684 close_diff_view(view);
3685 return err;
3688 static const struct got_error *
3689 show_diff_view(struct tog_view *view)
3691 const struct got_error *err;
3692 struct tog_diff_view_state *s = &view->state.diff;
3693 char *id_str1 = NULL, *id_str2, *header;
3694 const char *label1, *label2;
3696 if (s->id1) {
3697 err = got_object_id_str(&id_str1, s->id1);
3698 if (err)
3699 return err;
3700 label1 = s->label1 ? : id_str1;
3701 } else
3702 label1 = "/dev/null";
3704 err = got_object_id_str(&id_str2, s->id2);
3705 if (err)
3706 return err;
3707 label2 = s->label2 ? : id_str2;
3709 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3710 err = got_error_from_errno("asprintf");
3711 free(id_str1);
3712 free(id_str2);
3713 return err;
3715 free(id_str1);
3716 free(id_str2);
3718 err = draw_file(view, header);
3719 free(header);
3720 return err;
3723 static const struct got_error *
3724 set_selected_commit(struct tog_diff_view_state *s,
3725 struct commit_queue_entry *entry)
3727 const struct got_error *err;
3728 const struct got_object_id_queue *parent_ids;
3729 struct got_commit_object *selected_commit;
3730 struct got_object_qid *pid;
3732 free(s->id2);
3733 s->id2 = got_object_id_dup(entry->id);
3734 if (s->id2 == NULL)
3735 return got_error_from_errno("got_object_id_dup");
3737 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3738 if (err)
3739 return err;
3740 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3741 free(s->id1);
3742 pid = STAILQ_FIRST(parent_ids);
3743 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3744 got_object_commit_close(selected_commit);
3745 return NULL;
3748 static const struct got_error *
3749 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3751 const struct got_error *err = NULL;
3752 struct tog_diff_view_state *s = &view->state.diff;
3753 struct tog_log_view_state *ls;
3754 struct commit_queue_entry *old_selected_entry;
3755 char *line = NULL;
3756 size_t linesize = 0;
3757 ssize_t linelen;
3758 int i;
3760 switch (ch) {
3761 case 'a':
3762 case 'w':
3763 if (ch == 'a')
3764 s->force_text_diff = !s->force_text_diff;
3765 if (ch == 'w')
3766 s->ignore_whitespace = !s->ignore_whitespace;
3767 wclear(view->window);
3768 s->first_displayed_line = 1;
3769 s->last_displayed_line = view->nlines;
3770 s->matched_line = 0;
3771 diff_view_indicate_progress(view);
3772 err = create_diff(s);
3773 break;
3774 case 'g':
3775 case KEY_HOME:
3776 s->first_displayed_line = 1;
3777 break;
3778 case 'G':
3779 case KEY_END:
3780 if (s->eof)
3781 break;
3783 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3784 s->eof = 1;
3785 break;
3786 case 'k':
3787 case KEY_UP:
3788 case CTRL('p'):
3789 if (s->first_displayed_line > 1)
3790 s->first_displayed_line--;
3791 break;
3792 case KEY_PPAGE:
3793 case CTRL('b'):
3794 case CTRL('u'):
3795 case 'u':
3796 if (s->first_displayed_line == 1)
3797 break;
3798 i = 0;
3799 while (i++ < view->nlines - 1 &&
3800 s->first_displayed_line > 1)
3801 s->first_displayed_line--;
3802 break;
3803 case 'j':
3804 case KEY_DOWN:
3805 case CTRL('n'):
3806 if (!s->eof)
3807 s->first_displayed_line++;
3808 break;
3809 case KEY_NPAGE:
3810 case CTRL('f'):
3811 case ' ':
3812 case CTRL('d'):
3813 case 'd':
3814 if (s->eof)
3815 break;
3816 i = 0;
3817 while (!s->eof && i++ < view->nlines - 1) {
3818 linelen = getline(&line, &linesize, s->f);
3819 s->first_displayed_line++;
3820 if (linelen == -1) {
3821 if (feof(s->f)) {
3822 s->eof = 1;
3823 } else
3824 err = got_ferror(s->f, GOT_ERR_IO);
3825 break;
3828 free(line);
3829 break;
3830 case '[':
3831 if (s->diff_context > 0) {
3832 s->diff_context--;
3833 s->matched_line = 0;
3834 diff_view_indicate_progress(view);
3835 err = create_diff(s);
3836 if (s->first_displayed_line + view->nlines - 1 >
3837 s->nlines) {
3838 s->first_displayed_line = 1;
3839 s->last_displayed_line = view->nlines;
3842 break;
3843 case ']':
3844 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3845 s->diff_context++;
3846 s->matched_line = 0;
3847 diff_view_indicate_progress(view);
3848 err = create_diff(s);
3850 break;
3851 case '<':
3852 case ',':
3853 if (s->log_view == NULL)
3854 break;
3855 ls = &s->log_view->state.log;
3856 old_selected_entry = ls->selected_entry;
3858 err = input_log_view(NULL, s->log_view, KEY_UP);
3859 if (err)
3860 break;
3862 if (old_selected_entry == ls->selected_entry)
3863 break;
3865 err = set_selected_commit(s, ls->selected_entry);
3866 if (err)
3867 break;
3869 s->first_displayed_line = 1;
3870 s->last_displayed_line = view->nlines;
3871 s->matched_line = 0;
3873 diff_view_indicate_progress(view);
3874 err = create_diff(s);
3875 break;
3876 case '>':
3877 case '.':
3878 if (s->log_view == NULL)
3879 break;
3880 ls = &s->log_view->state.log;
3881 old_selected_entry = ls->selected_entry;
3883 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3884 if (err)
3885 break;
3887 if (old_selected_entry == ls->selected_entry)
3888 break;
3890 err = set_selected_commit(s, ls->selected_entry);
3891 if (err)
3892 break;
3894 s->first_displayed_line = 1;
3895 s->last_displayed_line = view->nlines;
3896 s->matched_line = 0;
3898 diff_view_indicate_progress(view);
3899 err = create_diff(s);
3900 break;
3901 default:
3902 break;
3905 return err;
3908 static const struct got_error *
3909 cmd_diff(int argc, char *argv[])
3911 const struct got_error *error = NULL;
3912 struct got_repository *repo = NULL;
3913 struct got_worktree *worktree = NULL;
3914 struct got_object_id *id1 = NULL, *id2 = NULL;
3915 char *repo_path = NULL, *cwd = NULL;
3916 char *id_str1 = NULL, *id_str2 = NULL;
3917 char *label1 = NULL, *label2 = NULL;
3918 int diff_context = 3, ignore_whitespace = 0;
3919 int ch, force_text_diff = 0;
3920 const char *errstr;
3921 struct tog_view *view;
3923 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3924 switch (ch) {
3925 case 'a':
3926 force_text_diff = 1;
3927 break;
3928 case 'C':
3929 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3930 &errstr);
3931 if (errstr != NULL)
3932 errx(1, "number of context lines is %s: %s",
3933 errstr, errstr);
3934 break;
3935 case 'r':
3936 repo_path = realpath(optarg, NULL);
3937 if (repo_path == NULL)
3938 return got_error_from_errno2("realpath",
3939 optarg);
3940 got_path_strip_trailing_slashes(repo_path);
3941 break;
3942 case 'w':
3943 ignore_whitespace = 1;
3944 break;
3945 default:
3946 usage_diff();
3947 /* NOTREACHED */
3951 argc -= optind;
3952 argv += optind;
3954 if (argc == 0) {
3955 usage_diff(); /* TODO show local worktree changes */
3956 } else if (argc == 2) {
3957 id_str1 = argv[0];
3958 id_str2 = argv[1];
3959 } else
3960 usage_diff();
3962 if (repo_path == NULL) {
3963 cwd = getcwd(NULL, 0);
3964 if (cwd == NULL)
3965 return got_error_from_errno("getcwd");
3966 error = got_worktree_open(&worktree, cwd);
3967 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3968 goto done;
3969 if (worktree)
3970 repo_path =
3971 strdup(got_worktree_get_repo_path(worktree));
3972 else
3973 repo_path = strdup(cwd);
3974 if (repo_path == NULL) {
3975 error = got_error_from_errno("strdup");
3976 goto done;
3980 error = got_repo_open(&repo, repo_path, NULL);
3981 if (error)
3982 goto done;
3984 init_curses();
3986 error = apply_unveil(got_repo_get_path(repo), NULL);
3987 if (error)
3988 goto done;
3990 error = tog_load_refs(repo, 0);
3991 if (error)
3992 goto done;
3994 error = got_repo_match_object_id(&id1, &label1, id_str1,
3995 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3996 if (error)
3997 goto done;
3999 error = got_repo_match_object_id(&id2, &label2, id_str2,
4000 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4001 if (error)
4002 goto done;
4004 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4005 if (view == NULL) {
4006 error = got_error_from_errno("view_open");
4007 goto done;
4009 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4010 ignore_whitespace, force_text_diff, NULL, repo);
4011 if (error)
4012 goto done;
4013 error = view_loop(view);
4014 done:
4015 free(label1);
4016 free(label2);
4017 free(repo_path);
4018 free(cwd);
4019 if (repo) {
4020 const struct got_error *close_err = got_repo_close(repo);
4021 if (error == NULL)
4022 error = close_err;
4024 if (worktree)
4025 got_worktree_close(worktree);
4026 tog_free_refs();
4027 return error;
4030 __dead static void
4031 usage_blame(void)
4033 endwin();
4034 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4035 getprogname());
4036 exit(1);
4039 struct tog_blame_line {
4040 int annotated;
4041 struct got_object_id *id;
4044 static const struct got_error *
4045 draw_blame(struct tog_view *view)
4047 struct tog_blame_view_state *s = &view->state.blame;
4048 struct tog_blame *blame = &s->blame;
4049 regmatch_t *regmatch = &view->regmatch;
4050 const struct got_error *err;
4051 int lineno = 0, nprinted = 0;
4052 char *line = NULL;
4053 size_t linesize = 0;
4054 ssize_t linelen;
4055 wchar_t *wline;
4056 int width;
4057 struct tog_blame_line *blame_line;
4058 struct got_object_id *prev_id = NULL;
4059 char *id_str;
4060 struct tog_color *tc;
4062 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4063 if (err)
4064 return err;
4066 rewind(blame->f);
4067 werase(view->window);
4069 if (asprintf(&line, "commit %s", id_str) == -1) {
4070 err = got_error_from_errno("asprintf");
4071 free(id_str);
4072 return err;
4075 err = format_line(&wline, &width, line, view->ncols, 0);
4076 free(line);
4077 line = NULL;
4078 if (err)
4079 return err;
4080 if (view_needs_focus_indication(view))
4081 wstandout(view->window);
4082 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4083 if (tc)
4084 wattr_on(view->window,
4085 COLOR_PAIR(tc->colorpair), NULL);
4086 waddwstr(view->window, wline);
4087 if (tc)
4088 wattr_off(view->window,
4089 COLOR_PAIR(tc->colorpair), NULL);
4090 if (view_needs_focus_indication(view))
4091 wstandend(view->window);
4092 free(wline);
4093 wline = NULL;
4094 if (width < view->ncols - 1)
4095 waddch(view->window, '\n');
4097 if (asprintf(&line, "[%d/%d] %s%s",
4098 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4099 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4100 free(id_str);
4101 return got_error_from_errno("asprintf");
4103 free(id_str);
4104 err = format_line(&wline, &width, line, view->ncols, 0);
4105 free(line);
4106 line = NULL;
4107 if (err)
4108 return err;
4109 waddwstr(view->window, wline);
4110 free(wline);
4111 wline = NULL;
4112 if (width < view->ncols - 1)
4113 waddch(view->window, '\n');
4115 s->eof = 0;
4116 while (nprinted < view->nlines - 2) {
4117 linelen = getline(&line, &linesize, blame->f);
4118 if (linelen == -1) {
4119 if (feof(blame->f)) {
4120 s->eof = 1;
4121 break;
4123 free(line);
4124 return got_ferror(blame->f, GOT_ERR_IO);
4126 if (++lineno < s->first_displayed_line)
4127 continue;
4129 if (view->focussed && nprinted == s->selected_line - 1)
4130 wstandout(view->window);
4132 if (blame->nlines > 0) {
4133 blame_line = &blame->lines[lineno - 1];
4134 if (blame_line->annotated && prev_id &&
4135 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4136 !(view->focussed &&
4137 nprinted == s->selected_line - 1)) {
4138 waddstr(view->window, " ");
4139 } else if (blame_line->annotated) {
4140 char *id_str;
4141 err = got_object_id_str(&id_str, blame_line->id);
4142 if (err) {
4143 free(line);
4144 return err;
4146 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4147 if (tc)
4148 wattr_on(view->window,
4149 COLOR_PAIR(tc->colorpair), NULL);
4150 wprintw(view->window, "%.8s", id_str);
4151 if (tc)
4152 wattr_off(view->window,
4153 COLOR_PAIR(tc->colorpair), NULL);
4154 free(id_str);
4155 prev_id = blame_line->id;
4156 } else {
4157 waddstr(view->window, "........");
4158 prev_id = NULL;
4160 } else {
4161 waddstr(view->window, "........");
4162 prev_id = NULL;
4165 if (view->focussed && nprinted == s->selected_line - 1)
4166 wstandend(view->window);
4167 waddstr(view->window, " ");
4169 if (view->ncols <= 9) {
4170 width = 9;
4171 wline = wcsdup(L"");
4172 if (wline == NULL) {
4173 err = got_error_from_errno("wcsdup");
4174 free(line);
4175 return err;
4177 } else if (s->first_displayed_line + nprinted ==
4178 s->matched_line &&
4179 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4180 err = add_matched_line(&width, line, view->ncols - 9, 9,
4181 view->window, regmatch);
4182 if (err) {
4183 free(line);
4184 return err;
4186 width += 9;
4187 } else {
4188 err = format_line(&wline, &width, line,
4189 view->ncols - 9, 9);
4190 waddwstr(view->window, wline);
4191 free(wline);
4192 wline = NULL;
4193 width += 9;
4196 if (width <= view->ncols - 1)
4197 waddch(view->window, '\n');
4198 if (++nprinted == 1)
4199 s->first_displayed_line = lineno;
4201 free(line);
4202 s->last_displayed_line = lineno;
4204 view_vborder(view);
4206 return NULL;
4209 static const struct got_error *
4210 blame_cb(void *arg, int nlines, int lineno,
4211 struct got_commit_object *commit, struct got_object_id *id)
4213 const struct got_error *err = NULL;
4214 struct tog_blame_cb_args *a = arg;
4215 struct tog_blame_line *line;
4216 int errcode;
4218 if (nlines != a->nlines ||
4219 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4220 return got_error(GOT_ERR_RANGE);
4222 errcode = pthread_mutex_lock(&tog_mutex);
4223 if (errcode)
4224 return got_error_set_errno(errcode, "pthread_mutex_lock");
4226 if (*a->quit) { /* user has quit the blame view */
4227 err = got_error(GOT_ERR_ITER_COMPLETED);
4228 goto done;
4231 if (lineno == -1)
4232 goto done; /* no change in this commit */
4234 line = &a->lines[lineno - 1];
4235 if (line->annotated)
4236 goto done;
4238 line->id = got_object_id_dup(id);
4239 if (line->id == NULL) {
4240 err = got_error_from_errno("got_object_id_dup");
4241 goto done;
4243 line->annotated = 1;
4244 done:
4245 errcode = pthread_mutex_unlock(&tog_mutex);
4246 if (errcode)
4247 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4248 return err;
4251 static void *
4252 blame_thread(void *arg)
4254 const struct got_error *err, *close_err;
4255 struct tog_blame_thread_args *ta = arg;
4256 struct tog_blame_cb_args *a = ta->cb_args;
4257 int errcode;
4259 err = block_signals_used_by_main_thread();
4260 if (err)
4261 return (void *)err;
4263 err = got_blame(ta->path, a->commit_id, ta->repo,
4264 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4265 if (err && err->code == GOT_ERR_CANCELLED)
4266 err = NULL;
4268 errcode = pthread_mutex_lock(&tog_mutex);
4269 if (errcode)
4270 return (void *)got_error_set_errno(errcode,
4271 "pthread_mutex_lock");
4273 close_err = got_repo_close(ta->repo);
4274 if (err == NULL)
4275 err = close_err;
4276 ta->repo = NULL;
4277 *ta->complete = 1;
4279 errcode = pthread_mutex_unlock(&tog_mutex);
4280 if (errcode && err == NULL)
4281 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4283 return (void *)err;
4286 static struct got_object_id *
4287 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4288 int first_displayed_line, int selected_line)
4290 struct tog_blame_line *line;
4292 if (nlines <= 0)
4293 return NULL;
4295 line = &lines[first_displayed_line - 1 + selected_line - 1];
4296 if (!line->annotated)
4297 return NULL;
4299 return line->id;
4302 static const struct got_error *
4303 stop_blame(struct tog_blame *blame)
4305 const struct got_error *err = NULL;
4306 int i;
4308 if (blame->thread) {
4309 int errcode;
4310 errcode = pthread_mutex_unlock(&tog_mutex);
4311 if (errcode)
4312 return got_error_set_errno(errcode,
4313 "pthread_mutex_unlock");
4314 errcode = pthread_join(blame->thread, (void **)&err);
4315 if (errcode)
4316 return got_error_set_errno(errcode, "pthread_join");
4317 errcode = pthread_mutex_lock(&tog_mutex);
4318 if (errcode)
4319 return got_error_set_errno(errcode,
4320 "pthread_mutex_lock");
4321 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4322 err = NULL;
4323 blame->thread = NULL;
4325 if (blame->thread_args.repo) {
4326 const struct got_error *close_err;
4327 close_err = got_repo_close(blame->thread_args.repo);
4328 if (err == NULL)
4329 err = close_err;
4330 blame->thread_args.repo = NULL;
4332 if (blame->f) {
4333 if (fclose(blame->f) == EOF && err == NULL)
4334 err = got_error_from_errno("fclose");
4335 blame->f = NULL;
4337 if (blame->lines) {
4338 for (i = 0; i < blame->nlines; i++)
4339 free(blame->lines[i].id);
4340 free(blame->lines);
4341 blame->lines = NULL;
4343 free(blame->cb_args.commit_id);
4344 blame->cb_args.commit_id = NULL;
4346 return err;
4349 static const struct got_error *
4350 cancel_blame_view(void *arg)
4352 const struct got_error *err = NULL;
4353 int *done = arg;
4354 int errcode;
4356 errcode = pthread_mutex_lock(&tog_mutex);
4357 if (errcode)
4358 return got_error_set_errno(errcode,
4359 "pthread_mutex_unlock");
4361 if (*done)
4362 err = got_error(GOT_ERR_CANCELLED);
4364 errcode = pthread_mutex_unlock(&tog_mutex);
4365 if (errcode)
4366 return got_error_set_errno(errcode,
4367 "pthread_mutex_lock");
4369 return err;
4372 static const struct got_error *
4373 run_blame(struct tog_view *view)
4375 struct tog_blame_view_state *s = &view->state.blame;
4376 struct tog_blame *blame = &s->blame;
4377 const struct got_error *err = NULL;
4378 struct got_commit_object *commit = NULL;
4379 struct got_blob_object *blob = NULL;
4380 struct got_repository *thread_repo = NULL;
4381 struct got_object_id *obj_id = NULL;
4382 int obj_type;
4384 err = got_object_open_as_commit(&commit, s->repo,
4385 &s->blamed_commit->id);
4386 if (err)
4387 return err;
4389 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4390 if (err)
4391 goto done;
4393 err = got_object_get_type(&obj_type, s->repo, obj_id);
4394 if (err)
4395 goto done;
4397 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4398 err = got_error(GOT_ERR_OBJ_TYPE);
4399 goto done;
4402 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4403 if (err)
4404 goto done;
4405 blame->f = got_opentemp();
4406 if (blame->f == NULL) {
4407 err = got_error_from_errno("got_opentemp");
4408 goto done;
4410 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4411 &blame->line_offsets, blame->f, blob);
4412 if (err)
4413 goto done;
4414 if (blame->nlines == 0) {
4415 s->blame_complete = 1;
4416 goto done;
4419 /* Don't include \n at EOF in the blame line count. */
4420 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4421 blame->nlines--;
4423 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4424 if (blame->lines == NULL) {
4425 err = got_error_from_errno("calloc");
4426 goto done;
4429 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4430 if (err)
4431 goto done;
4433 blame->cb_args.view = view;
4434 blame->cb_args.lines = blame->lines;
4435 blame->cb_args.nlines = blame->nlines;
4436 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4437 if (blame->cb_args.commit_id == NULL) {
4438 err = got_error_from_errno("got_object_id_dup");
4439 goto done;
4441 blame->cb_args.quit = &s->done;
4443 blame->thread_args.path = s->path;
4444 blame->thread_args.repo = thread_repo;
4445 blame->thread_args.cb_args = &blame->cb_args;
4446 blame->thread_args.complete = &s->blame_complete;
4447 blame->thread_args.cancel_cb = cancel_blame_view;
4448 blame->thread_args.cancel_arg = &s->done;
4449 s->blame_complete = 0;
4451 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4452 s->first_displayed_line = 1;
4453 s->last_displayed_line = view->nlines;
4454 s->selected_line = 1;
4456 s->matched_line = 0;
4458 done:
4459 if (commit)
4460 got_object_commit_close(commit);
4461 if (blob)
4462 got_object_blob_close(blob);
4463 free(obj_id);
4464 if (err)
4465 stop_blame(blame);
4466 return err;
4469 static const struct got_error *
4470 open_blame_view(struct tog_view *view, char *path,
4471 struct got_object_id *commit_id, struct got_repository *repo)
4473 const struct got_error *err = NULL;
4474 struct tog_blame_view_state *s = &view->state.blame;
4476 STAILQ_INIT(&s->blamed_commits);
4478 s->path = strdup(path);
4479 if (s->path == NULL)
4480 return got_error_from_errno("strdup");
4482 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4483 if (err) {
4484 free(s->path);
4485 return err;
4488 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4489 s->first_displayed_line = 1;
4490 s->last_displayed_line = view->nlines;
4491 s->selected_line = 1;
4492 s->blame_complete = 0;
4493 s->repo = repo;
4494 s->commit_id = commit_id;
4495 memset(&s->blame, 0, sizeof(s->blame));
4497 STAILQ_INIT(&s->colors);
4498 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4499 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4500 get_color_value("TOG_COLOR_COMMIT"));
4501 if (err)
4502 return err;
4505 view->show = show_blame_view;
4506 view->input = input_blame_view;
4507 view->close = close_blame_view;
4508 view->search_start = search_start_blame_view;
4509 view->search_next = search_next_blame_view;
4511 return run_blame(view);
4514 static const struct got_error *
4515 close_blame_view(struct tog_view *view)
4517 const struct got_error *err = NULL;
4518 struct tog_blame_view_state *s = &view->state.blame;
4520 if (s->blame.thread)
4521 err = stop_blame(&s->blame);
4523 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4524 struct got_object_qid *blamed_commit;
4525 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4526 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4527 got_object_qid_free(blamed_commit);
4530 free(s->path);
4531 free_colors(&s->colors);
4533 return err;
4536 static const struct got_error *
4537 search_start_blame_view(struct tog_view *view)
4539 struct tog_blame_view_state *s = &view->state.blame;
4541 s->matched_line = 0;
4542 return NULL;
4545 static const struct got_error *
4546 search_next_blame_view(struct tog_view *view)
4548 struct tog_blame_view_state *s = &view->state.blame;
4549 int lineno;
4550 char *line = NULL;
4551 size_t linesize = 0;
4552 ssize_t linelen;
4554 if (!view->searching) {
4555 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4556 return NULL;
4559 if (s->matched_line) {
4560 if (view->searching == TOG_SEARCH_FORWARD)
4561 lineno = s->matched_line + 1;
4562 else
4563 lineno = s->matched_line - 1;
4564 } else
4565 lineno = s->first_displayed_line - 1 + s->selected_line;
4567 while (1) {
4568 off_t offset;
4570 if (lineno <= 0 || lineno > s->blame.nlines) {
4571 if (s->matched_line == 0) {
4572 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4573 break;
4576 if (view->searching == TOG_SEARCH_FORWARD)
4577 lineno = 1;
4578 else
4579 lineno = s->blame.nlines;
4582 offset = s->blame.line_offsets[lineno - 1];
4583 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4584 free(line);
4585 return got_error_from_errno("fseeko");
4587 linelen = getline(&line, &linesize, s->blame.f);
4588 if (linelen != -1 &&
4589 match_line(line, &view->regex, 1, &view->regmatch)) {
4590 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4591 s->matched_line = lineno;
4592 break;
4594 if (view->searching == TOG_SEARCH_FORWARD)
4595 lineno++;
4596 else
4597 lineno--;
4599 free(line);
4601 if (s->matched_line) {
4602 s->first_displayed_line = s->matched_line;
4603 s->selected_line = 1;
4606 return NULL;
4609 static const struct got_error *
4610 show_blame_view(struct tog_view *view)
4612 const struct got_error *err = NULL;
4613 struct tog_blame_view_state *s = &view->state.blame;
4614 int errcode;
4616 if (s->blame.thread == NULL && !s->blame_complete) {
4617 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4618 &s->blame.thread_args);
4619 if (errcode)
4620 return got_error_set_errno(errcode, "pthread_create");
4622 halfdelay(1); /* fast refresh while annotating */
4625 if (s->blame_complete)
4626 halfdelay(10); /* disable fast refresh */
4628 err = draw_blame(view);
4630 view_vborder(view);
4631 return err;
4634 static const struct got_error *
4635 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4637 const struct got_error *err = NULL, *thread_err = NULL;
4638 struct tog_view *diff_view;
4639 struct tog_blame_view_state *s = &view->state.blame;
4640 int begin_x = 0;
4642 switch (ch) {
4643 case 'q':
4644 s->done = 1;
4645 break;
4646 case 'g':
4647 case KEY_HOME:
4648 s->selected_line = 1;
4649 s->first_displayed_line = 1;
4650 break;
4651 case 'G':
4652 case KEY_END:
4653 if (s->blame.nlines < view->nlines - 2) {
4654 s->selected_line = s->blame.nlines;
4655 s->first_displayed_line = 1;
4656 } else {
4657 s->selected_line = view->nlines - 2;
4658 s->first_displayed_line = s->blame.nlines -
4659 (view->nlines - 3);
4661 break;
4662 case 'k':
4663 case KEY_UP:
4664 case CTRL('p'):
4665 if (s->selected_line > 1)
4666 s->selected_line--;
4667 else if (s->selected_line == 1 &&
4668 s->first_displayed_line > 1)
4669 s->first_displayed_line--;
4670 break;
4671 case KEY_PPAGE:
4672 case CTRL('b'):
4673 case CTRL('u'):
4674 case 'u':
4675 if (s->first_displayed_line == 1) {
4676 s->selected_line = 1;
4677 break;
4679 if (s->first_displayed_line > view->nlines - 2)
4680 s->first_displayed_line -=
4681 (view->nlines - 2);
4682 else
4683 s->first_displayed_line = 1;
4684 break;
4685 case 'j':
4686 case KEY_DOWN:
4687 case CTRL('n'):
4688 if (s->selected_line < view->nlines - 2 &&
4689 s->first_displayed_line +
4690 s->selected_line <= s->blame.nlines)
4691 s->selected_line++;
4692 else if (s->last_displayed_line <
4693 s->blame.nlines)
4694 s->first_displayed_line++;
4695 break;
4696 case 'b':
4697 case 'p': {
4698 struct got_object_id *id = NULL;
4699 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4700 s->first_displayed_line, s->selected_line);
4701 if (id == NULL)
4702 break;
4703 if (ch == 'p') {
4704 struct got_commit_object *commit, *pcommit;
4705 struct got_object_qid *pid;
4706 struct got_object_id *blob_id = NULL;
4707 int obj_type;
4708 err = got_object_open_as_commit(&commit,
4709 s->repo, id);
4710 if (err)
4711 break;
4712 pid = STAILQ_FIRST(
4713 got_object_commit_get_parent_ids(commit));
4714 if (pid == NULL) {
4715 got_object_commit_close(commit);
4716 break;
4718 /* Check if path history ends here. */
4719 err = got_object_open_as_commit(&pcommit,
4720 s->repo, &pid->id);
4721 if (err)
4722 break;
4723 err = got_object_id_by_path(&blob_id, s->repo,
4724 pcommit, s->path);
4725 got_object_commit_close(pcommit);
4726 if (err) {
4727 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4728 err = NULL;
4729 got_object_commit_close(commit);
4730 break;
4732 err = got_object_get_type(&obj_type, s->repo,
4733 blob_id);
4734 free(blob_id);
4735 /* Can't blame non-blob type objects. */
4736 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4737 got_object_commit_close(commit);
4738 break;
4740 err = got_object_qid_alloc(&s->blamed_commit,
4741 &pid->id);
4742 got_object_commit_close(commit);
4743 } else {
4744 if (got_object_id_cmp(id,
4745 &s->blamed_commit->id) == 0)
4746 break;
4747 err = got_object_qid_alloc(&s->blamed_commit,
4748 id);
4750 if (err)
4751 break;
4752 s->done = 1;
4753 thread_err = stop_blame(&s->blame);
4754 s->done = 0;
4755 if (thread_err)
4756 break;
4757 STAILQ_INSERT_HEAD(&s->blamed_commits,
4758 s->blamed_commit, entry);
4759 err = run_blame(view);
4760 if (err)
4761 break;
4762 break;
4764 case 'B': {
4765 struct got_object_qid *first;
4766 first = STAILQ_FIRST(&s->blamed_commits);
4767 if (!got_object_id_cmp(&first->id, s->commit_id))
4768 break;
4769 s->done = 1;
4770 thread_err = stop_blame(&s->blame);
4771 s->done = 0;
4772 if (thread_err)
4773 break;
4774 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4775 got_object_qid_free(s->blamed_commit);
4776 s->blamed_commit =
4777 STAILQ_FIRST(&s->blamed_commits);
4778 err = run_blame(view);
4779 if (err)
4780 break;
4781 break;
4783 case KEY_ENTER:
4784 case '\r': {
4785 struct got_object_id *id = NULL;
4786 struct got_object_qid *pid;
4787 struct got_commit_object *commit = NULL;
4788 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4789 s->first_displayed_line, s->selected_line);
4790 if (id == NULL)
4791 break;
4792 err = got_object_open_as_commit(&commit, s->repo, id);
4793 if (err)
4794 break;
4795 pid = STAILQ_FIRST(
4796 got_object_commit_get_parent_ids(commit));
4797 if (view_is_parent_view(view))
4798 begin_x = view_split_begin_x(view->begin_x);
4799 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4800 if (diff_view == NULL) {
4801 got_object_commit_close(commit);
4802 err = got_error_from_errno("view_open");
4803 break;
4805 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4806 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4807 got_object_commit_close(commit);
4808 if (err) {
4809 view_close(diff_view);
4810 break;
4812 view->focussed = 0;
4813 diff_view->focussed = 1;
4814 if (view_is_parent_view(view)) {
4815 err = view_close_child(view);
4816 if (err)
4817 break;
4818 view_set_child(view, diff_view);
4819 view->focus_child = 1;
4820 } else
4821 *new_view = diff_view;
4822 if (err)
4823 break;
4824 break;
4826 case KEY_NPAGE:
4827 case CTRL('f'):
4828 case ' ':
4829 case CTRL('d'):
4830 case 'd':
4831 if (s->last_displayed_line >= s->blame.nlines &&
4832 s->selected_line >= MIN(s->blame.nlines,
4833 view->nlines - 2)) {
4834 break;
4836 if (s->last_displayed_line >= s->blame.nlines &&
4837 s->selected_line < view->nlines - 2) {
4838 s->selected_line = MIN(s->blame.nlines,
4839 view->nlines - 2);
4840 break;
4842 if (s->last_displayed_line + view->nlines - 2
4843 <= s->blame.nlines)
4844 s->first_displayed_line +=
4845 view->nlines - 2;
4846 else
4847 s->first_displayed_line =
4848 s->blame.nlines -
4849 (view->nlines - 3);
4850 break;
4851 case KEY_RESIZE:
4852 if (s->selected_line > view->nlines - 2) {
4853 s->selected_line = MIN(s->blame.nlines,
4854 view->nlines - 2);
4856 break;
4857 default:
4858 break;
4860 return thread_err ? thread_err : err;
4863 static const struct got_error *
4864 cmd_blame(int argc, char *argv[])
4866 const struct got_error *error;
4867 struct got_repository *repo = NULL;
4868 struct got_worktree *worktree = NULL;
4869 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4870 char *link_target = NULL;
4871 struct got_object_id *commit_id = NULL;
4872 struct got_commit_object *commit = NULL;
4873 char *commit_id_str = NULL;
4874 int ch;
4875 struct tog_view *view;
4877 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4878 switch (ch) {
4879 case 'c':
4880 commit_id_str = optarg;
4881 break;
4882 case 'r':
4883 repo_path = realpath(optarg, NULL);
4884 if (repo_path == NULL)
4885 return got_error_from_errno2("realpath",
4886 optarg);
4887 break;
4888 default:
4889 usage_blame();
4890 /* NOTREACHED */
4894 argc -= optind;
4895 argv += optind;
4897 if (argc != 1)
4898 usage_blame();
4900 if (repo_path == NULL) {
4901 cwd = getcwd(NULL, 0);
4902 if (cwd == NULL)
4903 return got_error_from_errno("getcwd");
4904 error = got_worktree_open(&worktree, cwd);
4905 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4906 goto done;
4907 if (worktree)
4908 repo_path =
4909 strdup(got_worktree_get_repo_path(worktree));
4910 else
4911 repo_path = strdup(cwd);
4912 if (repo_path == NULL) {
4913 error = got_error_from_errno("strdup");
4914 goto done;
4918 error = got_repo_open(&repo, repo_path, NULL);
4919 if (error != NULL)
4920 goto done;
4922 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4923 worktree);
4924 if (error)
4925 goto done;
4927 init_curses();
4929 error = apply_unveil(got_repo_get_path(repo), NULL);
4930 if (error)
4931 goto done;
4933 error = tog_load_refs(repo, 0);
4934 if (error)
4935 goto done;
4937 if (commit_id_str == NULL) {
4938 struct got_reference *head_ref;
4939 error = got_ref_open(&head_ref, repo, worktree ?
4940 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4941 if (error != NULL)
4942 goto done;
4943 error = got_ref_resolve(&commit_id, repo, head_ref);
4944 got_ref_close(head_ref);
4945 } else {
4946 error = got_repo_match_object_id(&commit_id, NULL,
4947 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4949 if (error != NULL)
4950 goto done;
4952 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4953 if (view == NULL) {
4954 error = got_error_from_errno("view_open");
4955 goto done;
4958 error = got_object_open_as_commit(&commit, repo, commit_id);
4959 if (error)
4960 goto done;
4962 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4963 commit, repo);
4964 if (error)
4965 goto done;
4967 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4968 commit_id, repo);
4969 if (error)
4970 goto done;
4971 if (worktree) {
4972 /* Release work tree lock. */
4973 got_worktree_close(worktree);
4974 worktree = NULL;
4976 error = view_loop(view);
4977 done:
4978 free(repo_path);
4979 free(in_repo_path);
4980 free(link_target);
4981 free(cwd);
4982 free(commit_id);
4983 if (commit)
4984 got_object_commit_close(commit);
4985 if (worktree)
4986 got_worktree_close(worktree);
4987 if (repo) {
4988 const struct got_error *close_err = got_repo_close(repo);
4989 if (error == NULL)
4990 error = close_err;
4992 tog_free_refs();
4993 return error;
4996 static const struct got_error *
4997 draw_tree_entries(struct tog_view *view, const char *parent_path)
4999 struct tog_tree_view_state *s = &view->state.tree;
5000 const struct got_error *err = NULL;
5001 struct got_tree_entry *te;
5002 wchar_t *wline;
5003 struct tog_color *tc;
5004 int width, n, i, nentries;
5005 int limit = view->nlines;
5007 s->ndisplayed = 0;
5009 werase(view->window);
5011 if (limit == 0)
5012 return NULL;
5014 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5015 if (err)
5016 return err;
5017 if (view_needs_focus_indication(view))
5018 wstandout(view->window);
5019 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5020 if (tc)
5021 wattr_on(view->window,
5022 COLOR_PAIR(tc->colorpair), NULL);
5023 waddwstr(view->window, wline);
5024 if (tc)
5025 wattr_off(view->window,
5026 COLOR_PAIR(tc->colorpair), NULL);
5027 if (view_needs_focus_indication(view))
5028 wstandend(view->window);
5029 free(wline);
5030 wline = NULL;
5031 if (width < view->ncols - 1)
5032 waddch(view->window, '\n');
5033 if (--limit <= 0)
5034 return NULL;
5035 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5036 if (err)
5037 return err;
5038 waddwstr(view->window, wline);
5039 free(wline);
5040 wline = NULL;
5041 if (width < view->ncols - 1)
5042 waddch(view->window, '\n');
5043 if (--limit <= 0)
5044 return NULL;
5045 waddch(view->window, '\n');
5046 if (--limit <= 0)
5047 return NULL;
5049 if (s->first_displayed_entry == NULL) {
5050 te = got_object_tree_get_first_entry(s->tree);
5051 if (s->selected == 0) {
5052 if (view->focussed)
5053 wstandout(view->window);
5054 s->selected_entry = NULL;
5056 waddstr(view->window, " ..\n"); /* parent directory */
5057 if (s->selected == 0 && view->focussed)
5058 wstandend(view->window);
5059 s->ndisplayed++;
5060 if (--limit <= 0)
5061 return NULL;
5062 n = 1;
5063 } else {
5064 n = 0;
5065 te = s->first_displayed_entry;
5068 nentries = got_object_tree_get_nentries(s->tree);
5069 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5070 char *line = NULL, *id_str = NULL, *link_target = NULL;
5071 const char *modestr = "";
5072 mode_t mode;
5074 te = got_object_tree_get_entry(s->tree, i);
5075 mode = got_tree_entry_get_mode(te);
5077 if (s->show_ids) {
5078 err = got_object_id_str(&id_str,
5079 got_tree_entry_get_id(te));
5080 if (err)
5081 return got_error_from_errno(
5082 "got_object_id_str");
5084 if (got_object_tree_entry_is_submodule(te))
5085 modestr = "$";
5086 else if (S_ISLNK(mode)) {
5087 int i;
5089 err = got_tree_entry_get_symlink_target(&link_target,
5090 te, s->repo);
5091 if (err) {
5092 free(id_str);
5093 return err;
5095 for (i = 0; i < strlen(link_target); i++) {
5096 if (!isprint((unsigned char)link_target[i]))
5097 link_target[i] = '?';
5099 modestr = "@";
5101 else if (S_ISDIR(mode))
5102 modestr = "/";
5103 else if (mode & S_IXUSR)
5104 modestr = "*";
5105 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5106 got_tree_entry_get_name(te), modestr,
5107 link_target ? " -> ": "",
5108 link_target ? link_target : "") == -1) {
5109 free(id_str);
5110 free(link_target);
5111 return got_error_from_errno("asprintf");
5113 free(id_str);
5114 free(link_target);
5115 err = format_line(&wline, &width, line, view->ncols, 0);
5116 if (err) {
5117 free(line);
5118 break;
5120 if (n == s->selected) {
5121 if (view->focussed)
5122 wstandout(view->window);
5123 s->selected_entry = te;
5125 tc = match_color(&s->colors, line);
5126 if (tc)
5127 wattr_on(view->window,
5128 COLOR_PAIR(tc->colorpair), NULL);
5129 waddwstr(view->window, wline);
5130 if (tc)
5131 wattr_off(view->window,
5132 COLOR_PAIR(tc->colorpair), NULL);
5133 if (width < view->ncols - 1)
5134 waddch(view->window, '\n');
5135 if (n == s->selected && view->focussed)
5136 wstandend(view->window);
5137 free(line);
5138 free(wline);
5139 wline = NULL;
5140 n++;
5141 s->ndisplayed++;
5142 s->last_displayed_entry = te;
5143 if (--limit <= 0)
5144 break;
5147 return err;
5150 static void
5151 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5153 struct got_tree_entry *te;
5154 int isroot = s->tree == s->root;
5155 int i = 0;
5157 if (s->first_displayed_entry == NULL)
5158 return;
5160 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5161 while (i++ < maxscroll) {
5162 if (te == NULL) {
5163 if (!isroot)
5164 s->first_displayed_entry = NULL;
5165 break;
5167 s->first_displayed_entry = te;
5168 te = got_tree_entry_get_prev(s->tree, te);
5172 static void
5173 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5175 struct got_tree_entry *next, *last;
5176 int n = 0;
5178 if (s->first_displayed_entry)
5179 next = got_tree_entry_get_next(s->tree,
5180 s->first_displayed_entry);
5181 else
5182 next = got_object_tree_get_first_entry(s->tree);
5184 last = s->last_displayed_entry;
5185 while (next && last && n++ < maxscroll) {
5186 last = got_tree_entry_get_next(s->tree, last);
5187 if (last) {
5188 s->first_displayed_entry = next;
5189 next = got_tree_entry_get_next(s->tree, next);
5194 static const struct got_error *
5195 tree_entry_path(char **path, struct tog_parent_trees *parents,
5196 struct got_tree_entry *te)
5198 const struct got_error *err = NULL;
5199 struct tog_parent_tree *pt;
5200 size_t len = 2; /* for leading slash and NUL */
5202 TAILQ_FOREACH(pt, parents, entry)
5203 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5204 + 1 /* slash */;
5205 if (te)
5206 len += strlen(got_tree_entry_get_name(te));
5208 *path = calloc(1, len);
5209 if (path == NULL)
5210 return got_error_from_errno("calloc");
5212 (*path)[0] = '/';
5213 pt = TAILQ_LAST(parents, tog_parent_trees);
5214 while (pt) {
5215 const char *name = got_tree_entry_get_name(pt->selected_entry);
5216 if (strlcat(*path, name, len) >= len) {
5217 err = got_error(GOT_ERR_NO_SPACE);
5218 goto done;
5220 if (strlcat(*path, "/", len) >= len) {
5221 err = got_error(GOT_ERR_NO_SPACE);
5222 goto done;
5224 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5226 if (te) {
5227 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5228 err = got_error(GOT_ERR_NO_SPACE);
5229 goto done;
5232 done:
5233 if (err) {
5234 free(*path);
5235 *path = NULL;
5237 return err;
5240 static const struct got_error *
5241 blame_tree_entry(struct tog_view **new_view, int begin_x,
5242 struct got_tree_entry *te, struct tog_parent_trees *parents,
5243 struct got_object_id *commit_id, struct got_repository *repo)
5245 const struct got_error *err = NULL;
5246 char *path;
5247 struct tog_view *blame_view;
5249 *new_view = NULL;
5251 err = tree_entry_path(&path, parents, te);
5252 if (err)
5253 return err;
5255 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5256 if (blame_view == NULL) {
5257 err = got_error_from_errno("view_open");
5258 goto done;
5261 err = open_blame_view(blame_view, path, commit_id, repo);
5262 if (err) {
5263 if (err->code == GOT_ERR_CANCELLED)
5264 err = NULL;
5265 view_close(blame_view);
5266 } else
5267 *new_view = blame_view;
5268 done:
5269 free(path);
5270 return err;
5273 static const struct got_error *
5274 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5275 struct tog_tree_view_state *s)
5277 struct tog_view *log_view;
5278 const struct got_error *err = NULL;
5279 char *path;
5281 *new_view = NULL;
5283 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5284 if (log_view == NULL)
5285 return got_error_from_errno("view_open");
5287 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5288 if (err)
5289 return err;
5291 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5292 path, 0);
5293 if (err)
5294 view_close(log_view);
5295 else
5296 *new_view = log_view;
5297 free(path);
5298 return err;
5301 static const struct got_error *
5302 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5303 const char *head_ref_name, struct got_repository *repo)
5305 const struct got_error *err = NULL;
5306 char *commit_id_str = NULL;
5307 struct tog_tree_view_state *s = &view->state.tree;
5308 struct got_commit_object *commit = NULL;
5310 TAILQ_INIT(&s->parents);
5311 STAILQ_INIT(&s->colors);
5313 s->commit_id = got_object_id_dup(commit_id);
5314 if (s->commit_id == NULL)
5315 return got_error_from_errno("got_object_id_dup");
5317 err = got_object_open_as_commit(&commit, repo, commit_id);
5318 if (err)
5319 goto done;
5322 * The root is opened here and will be closed when the view is closed.
5323 * Any visited subtrees and their path-wise parents are opened and
5324 * closed on demand.
5326 err = got_object_open_as_tree(&s->root, repo,
5327 got_object_commit_get_tree_id(commit));
5328 if (err)
5329 goto done;
5330 s->tree = s->root;
5332 err = got_object_id_str(&commit_id_str, commit_id);
5333 if (err != NULL)
5334 goto done;
5336 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5337 err = got_error_from_errno("asprintf");
5338 goto done;
5341 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5342 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5343 if (head_ref_name) {
5344 s->head_ref_name = strdup(head_ref_name);
5345 if (s->head_ref_name == NULL) {
5346 err = got_error_from_errno("strdup");
5347 goto done;
5350 s->repo = repo;
5352 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5353 err = add_color(&s->colors, "\\$$",
5354 TOG_COLOR_TREE_SUBMODULE,
5355 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5356 if (err)
5357 goto done;
5358 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5359 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5360 if (err)
5361 goto done;
5362 err = add_color(&s->colors, "/$",
5363 TOG_COLOR_TREE_DIRECTORY,
5364 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5365 if (err)
5366 goto done;
5368 err = add_color(&s->colors, "\\*$",
5369 TOG_COLOR_TREE_EXECUTABLE,
5370 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5371 if (err)
5372 goto done;
5374 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5375 get_color_value("TOG_COLOR_COMMIT"));
5376 if (err)
5377 goto done;
5380 view->show = show_tree_view;
5381 view->input = input_tree_view;
5382 view->close = close_tree_view;
5383 view->search_start = search_start_tree_view;
5384 view->search_next = search_next_tree_view;
5385 done:
5386 free(commit_id_str);
5387 if (commit)
5388 got_object_commit_close(commit);
5389 if (err)
5390 close_tree_view(view);
5391 return err;
5394 static const struct got_error *
5395 close_tree_view(struct tog_view *view)
5397 struct tog_tree_view_state *s = &view->state.tree;
5399 free_colors(&s->colors);
5400 free(s->tree_label);
5401 s->tree_label = NULL;
5402 free(s->commit_id);
5403 s->commit_id = NULL;
5404 free(s->head_ref_name);
5405 s->head_ref_name = NULL;
5406 while (!TAILQ_EMPTY(&s->parents)) {
5407 struct tog_parent_tree *parent;
5408 parent = TAILQ_FIRST(&s->parents);
5409 TAILQ_REMOVE(&s->parents, parent, entry);
5410 if (parent->tree != s->root)
5411 got_object_tree_close(parent->tree);
5412 free(parent);
5415 if (s->tree != NULL && s->tree != s->root)
5416 got_object_tree_close(s->tree);
5417 if (s->root)
5418 got_object_tree_close(s->root);
5419 return NULL;
5422 static const struct got_error *
5423 search_start_tree_view(struct tog_view *view)
5425 struct tog_tree_view_state *s = &view->state.tree;
5427 s->matched_entry = NULL;
5428 return NULL;
5431 static int
5432 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5434 regmatch_t regmatch;
5436 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5437 0) == 0;
5440 static const struct got_error *
5441 search_next_tree_view(struct tog_view *view)
5443 struct tog_tree_view_state *s = &view->state.tree;
5444 struct got_tree_entry *te = NULL;
5446 if (!view->searching) {
5447 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5448 return NULL;
5451 if (s->matched_entry) {
5452 if (view->searching == TOG_SEARCH_FORWARD) {
5453 if (s->selected_entry)
5454 te = got_tree_entry_get_next(s->tree,
5455 s->selected_entry);
5456 else
5457 te = got_object_tree_get_first_entry(s->tree);
5458 } else {
5459 if (s->selected_entry == NULL)
5460 te = got_object_tree_get_last_entry(s->tree);
5461 else
5462 te = got_tree_entry_get_prev(s->tree,
5463 s->selected_entry);
5465 } else {
5466 if (s->selected_entry)
5467 te = s->selected_entry;
5468 else if (view->searching == TOG_SEARCH_FORWARD)
5469 te = got_object_tree_get_first_entry(s->tree);
5470 else
5471 te = got_object_tree_get_last_entry(s->tree);
5474 while (1) {
5475 if (te == NULL) {
5476 if (s->matched_entry == NULL) {
5477 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5478 return NULL;
5480 if (view->searching == TOG_SEARCH_FORWARD)
5481 te = got_object_tree_get_first_entry(s->tree);
5482 else
5483 te = got_object_tree_get_last_entry(s->tree);
5486 if (match_tree_entry(te, &view->regex)) {
5487 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5488 s->matched_entry = te;
5489 break;
5492 if (view->searching == TOG_SEARCH_FORWARD)
5493 te = got_tree_entry_get_next(s->tree, te);
5494 else
5495 te = got_tree_entry_get_prev(s->tree, te);
5498 if (s->matched_entry) {
5499 s->first_displayed_entry = s->matched_entry;
5500 s->selected = 0;
5503 return NULL;
5506 static const struct got_error *
5507 show_tree_view(struct tog_view *view)
5509 const struct got_error *err = NULL;
5510 struct tog_tree_view_state *s = &view->state.tree;
5511 char *parent_path;
5513 err = tree_entry_path(&parent_path, &s->parents, NULL);
5514 if (err)
5515 return err;
5517 err = draw_tree_entries(view, parent_path);
5518 free(parent_path);
5520 view_vborder(view);
5521 return err;
5524 static const struct got_error *
5525 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5527 const struct got_error *err = NULL;
5528 struct tog_tree_view_state *s = &view->state.tree;
5529 struct tog_view *log_view, *ref_view;
5530 struct got_tree_entry *te;
5531 int begin_x = 0, n;
5533 switch (ch) {
5534 case 'i':
5535 s->show_ids = !s->show_ids;
5536 break;
5537 case 'l':
5538 if (!s->selected_entry)
5539 break;
5540 if (view_is_parent_view(view))
5541 begin_x = view_split_begin_x(view->begin_x);
5542 err = log_selected_tree_entry(&log_view, begin_x, s);
5543 view->focussed = 0;
5544 log_view->focussed = 1;
5545 if (view_is_parent_view(view)) {
5546 err = view_close_child(view);
5547 if (err)
5548 return err;
5549 view_set_child(view, log_view);
5550 view->focus_child = 1;
5551 } else
5552 *new_view = log_view;
5553 break;
5554 case 'r':
5555 if (view_is_parent_view(view))
5556 begin_x = view_split_begin_x(view->begin_x);
5557 ref_view = view_open(view->nlines, view->ncols,
5558 view->begin_y, begin_x, TOG_VIEW_REF);
5559 if (ref_view == NULL)
5560 return got_error_from_errno("view_open");
5561 err = open_ref_view(ref_view, s->repo);
5562 if (err) {
5563 view_close(ref_view);
5564 return err;
5566 view->focussed = 0;
5567 ref_view->focussed = 1;
5568 if (view_is_parent_view(view)) {
5569 err = view_close_child(view);
5570 if (err)
5571 return err;
5572 view_set_child(view, ref_view);
5573 view->focus_child = 1;
5574 } else
5575 *new_view = ref_view;
5576 break;
5577 case 'g':
5578 case KEY_HOME:
5579 s->selected = 0;
5580 if (s->tree == s->root)
5581 s->first_displayed_entry =
5582 got_object_tree_get_first_entry(s->tree);
5583 else
5584 s->first_displayed_entry = NULL;
5585 break;
5586 case 'G':
5587 case KEY_END:
5588 s->selected = 0;
5589 te = got_object_tree_get_last_entry(s->tree);
5590 for (n = 0; n < view->nlines - 3; n++) {
5591 if (te == NULL) {
5592 if(s->tree != s->root) {
5593 s->first_displayed_entry = NULL;
5594 n++;
5596 break;
5598 s->first_displayed_entry = te;
5599 te = got_tree_entry_get_prev(s->tree, te);
5601 if (n > 0)
5602 s->selected = n - 1;
5603 break;
5604 case 'k':
5605 case KEY_UP:
5606 case CTRL('p'):
5607 if (s->selected > 0) {
5608 s->selected--;
5609 break;
5611 tree_scroll_up(s, 1);
5612 break;
5613 case KEY_PPAGE:
5614 case CTRL('b'):
5615 case CTRL('u'):
5616 case 'u':
5617 if (s->tree == s->root) {
5618 if (got_object_tree_get_first_entry(s->tree) ==
5619 s->first_displayed_entry)
5620 s->selected = 0;
5621 } else {
5622 if (s->first_displayed_entry == NULL)
5623 s->selected = 0;
5625 tree_scroll_up(s, MAX(0, view->nlines - 3));
5626 break;
5627 case 'j':
5628 case KEY_DOWN:
5629 case CTRL('n'):
5630 if (s->selected < s->ndisplayed - 1) {
5631 s->selected++;
5632 break;
5634 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5635 == NULL)
5636 /* can't scroll any further */
5637 break;
5638 tree_scroll_down(s, 1);
5639 break;
5640 case KEY_NPAGE:
5641 case CTRL('f'):
5642 case CTRL('d'):
5643 case 'd':
5644 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5645 == NULL) {
5646 /* can't scroll any further; move cursor down */
5647 if (s->selected < s->ndisplayed - 1)
5648 s->selected = s->ndisplayed - 1;
5649 break;
5651 tree_scroll_down(s, view->nlines - 3);
5652 break;
5653 case KEY_ENTER:
5654 case '\r':
5655 case KEY_BACKSPACE:
5656 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5657 struct tog_parent_tree *parent;
5658 /* user selected '..' */
5659 if (s->tree == s->root)
5660 break;
5661 parent = TAILQ_FIRST(&s->parents);
5662 TAILQ_REMOVE(&s->parents, parent,
5663 entry);
5664 got_object_tree_close(s->tree);
5665 s->tree = parent->tree;
5666 s->first_displayed_entry =
5667 parent->first_displayed_entry;
5668 s->selected_entry =
5669 parent->selected_entry;
5670 s->selected = parent->selected;
5671 free(parent);
5672 } else if (S_ISDIR(got_tree_entry_get_mode(
5673 s->selected_entry))) {
5674 struct got_tree_object *subtree;
5675 err = got_object_open_as_tree(&subtree, s->repo,
5676 got_tree_entry_get_id(s->selected_entry));
5677 if (err)
5678 break;
5679 err = tree_view_visit_subtree(s, subtree);
5680 if (err) {
5681 got_object_tree_close(subtree);
5682 break;
5684 } else if (S_ISREG(got_tree_entry_get_mode(
5685 s->selected_entry))) {
5686 struct tog_view *blame_view;
5687 int begin_x = view_is_parent_view(view) ?
5688 view_split_begin_x(view->begin_x) : 0;
5690 err = blame_tree_entry(&blame_view, begin_x,
5691 s->selected_entry, &s->parents,
5692 s->commit_id, s->repo);
5693 if (err)
5694 break;
5695 view->focussed = 0;
5696 blame_view->focussed = 1;
5697 if (view_is_parent_view(view)) {
5698 err = view_close_child(view);
5699 if (err)
5700 return err;
5701 view_set_child(view, blame_view);
5702 view->focus_child = 1;
5703 } else
5704 *new_view = blame_view;
5706 break;
5707 case KEY_RESIZE:
5708 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5709 s->selected = view->nlines - 4;
5710 break;
5711 default:
5712 break;
5715 return err;
5718 __dead static void
5719 usage_tree(void)
5721 endwin();
5722 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5723 getprogname());
5724 exit(1);
5727 static const struct got_error *
5728 cmd_tree(int argc, char *argv[])
5730 const struct got_error *error;
5731 struct got_repository *repo = NULL;
5732 struct got_worktree *worktree = NULL;
5733 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5734 struct got_object_id *commit_id = NULL;
5735 struct got_commit_object *commit = NULL;
5736 const char *commit_id_arg = NULL;
5737 char *label = NULL;
5738 struct got_reference *ref = NULL;
5739 const char *head_ref_name = NULL;
5740 int ch;
5741 struct tog_view *view;
5743 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5744 switch (ch) {
5745 case 'c':
5746 commit_id_arg = optarg;
5747 break;
5748 case 'r':
5749 repo_path = realpath(optarg, NULL);
5750 if (repo_path == NULL)
5751 return got_error_from_errno2("realpath",
5752 optarg);
5753 break;
5754 default:
5755 usage_tree();
5756 /* NOTREACHED */
5760 argc -= optind;
5761 argv += optind;
5763 if (argc > 1)
5764 usage_tree();
5766 if (repo_path == NULL) {
5767 cwd = getcwd(NULL, 0);
5768 if (cwd == NULL)
5769 return got_error_from_errno("getcwd");
5770 error = got_worktree_open(&worktree, cwd);
5771 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5772 goto done;
5773 if (worktree)
5774 repo_path =
5775 strdup(got_worktree_get_repo_path(worktree));
5776 else
5777 repo_path = strdup(cwd);
5778 if (repo_path == NULL) {
5779 error = got_error_from_errno("strdup");
5780 goto done;
5784 error = got_repo_open(&repo, repo_path, NULL);
5785 if (error != NULL)
5786 goto done;
5788 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5789 repo, worktree);
5790 if (error)
5791 goto done;
5793 init_curses();
5795 error = apply_unveil(got_repo_get_path(repo), NULL);
5796 if (error)
5797 goto done;
5799 error = tog_load_refs(repo, 0);
5800 if (error)
5801 goto done;
5803 if (commit_id_arg == NULL) {
5804 error = got_repo_match_object_id(&commit_id, &label,
5805 worktree ? got_worktree_get_head_ref_name(worktree) :
5806 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5807 if (error)
5808 goto done;
5809 head_ref_name = label;
5810 } else {
5811 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5812 if (error == NULL)
5813 head_ref_name = got_ref_get_name(ref);
5814 else if (error->code != GOT_ERR_NOT_REF)
5815 goto done;
5816 error = got_repo_match_object_id(&commit_id, NULL,
5817 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5818 if (error)
5819 goto done;
5822 error = got_object_open_as_commit(&commit, repo, commit_id);
5823 if (error)
5824 goto done;
5826 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5827 if (view == NULL) {
5828 error = got_error_from_errno("view_open");
5829 goto done;
5831 error = open_tree_view(view, commit_id, head_ref_name, repo);
5832 if (error)
5833 goto done;
5834 if (!got_path_is_root_dir(in_repo_path)) {
5835 error = tree_view_walk_path(&view->state.tree, commit,
5836 in_repo_path);
5837 if (error)
5838 goto done;
5841 if (worktree) {
5842 /* Release work tree lock. */
5843 got_worktree_close(worktree);
5844 worktree = NULL;
5846 error = view_loop(view);
5847 done:
5848 free(repo_path);
5849 free(cwd);
5850 free(commit_id);
5851 free(label);
5852 if (ref)
5853 got_ref_close(ref);
5854 if (repo) {
5855 const struct got_error *close_err = got_repo_close(repo);
5856 if (error == NULL)
5857 error = close_err;
5859 tog_free_refs();
5860 return error;
5863 static const struct got_error *
5864 ref_view_load_refs(struct tog_ref_view_state *s)
5866 struct got_reflist_entry *sre;
5867 struct tog_reflist_entry *re;
5869 s->nrefs = 0;
5870 TAILQ_FOREACH(sre, &tog_refs, entry) {
5871 if (strncmp(got_ref_get_name(sre->ref),
5872 "refs/got/", 9) == 0 &&
5873 strncmp(got_ref_get_name(sre->ref),
5874 "refs/got/backup/", 16) != 0)
5875 continue;
5877 re = malloc(sizeof(*re));
5878 if (re == NULL)
5879 return got_error_from_errno("malloc");
5881 re->ref = got_ref_dup(sre->ref);
5882 if (re->ref == NULL)
5883 return got_error_from_errno("got_ref_dup");
5884 re->idx = s->nrefs++;
5885 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5888 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5889 return NULL;
5892 void
5893 ref_view_free_refs(struct tog_ref_view_state *s)
5895 struct tog_reflist_entry *re;
5897 while (!TAILQ_EMPTY(&s->refs)) {
5898 re = TAILQ_FIRST(&s->refs);
5899 TAILQ_REMOVE(&s->refs, re, entry);
5900 got_ref_close(re->ref);
5901 free(re);
5905 static const struct got_error *
5906 open_ref_view(struct tog_view *view, struct got_repository *repo)
5908 const struct got_error *err = NULL;
5909 struct tog_ref_view_state *s = &view->state.ref;
5911 s->selected_entry = 0;
5912 s->repo = repo;
5914 TAILQ_INIT(&s->refs);
5915 STAILQ_INIT(&s->colors);
5917 err = ref_view_load_refs(s);
5918 if (err)
5919 return err;
5921 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5922 err = add_color(&s->colors, "^refs/heads/",
5923 TOG_COLOR_REFS_HEADS,
5924 get_color_value("TOG_COLOR_REFS_HEADS"));
5925 if (err)
5926 goto done;
5928 err = add_color(&s->colors, "^refs/tags/",
5929 TOG_COLOR_REFS_TAGS,
5930 get_color_value("TOG_COLOR_REFS_TAGS"));
5931 if (err)
5932 goto done;
5934 err = add_color(&s->colors, "^refs/remotes/",
5935 TOG_COLOR_REFS_REMOTES,
5936 get_color_value("TOG_COLOR_REFS_REMOTES"));
5937 if (err)
5938 goto done;
5940 err = add_color(&s->colors, "^refs/got/backup/",
5941 TOG_COLOR_REFS_BACKUP,
5942 get_color_value("TOG_COLOR_REFS_BACKUP"));
5943 if (err)
5944 goto done;
5947 view->show = show_ref_view;
5948 view->input = input_ref_view;
5949 view->close = close_ref_view;
5950 view->search_start = search_start_ref_view;
5951 view->search_next = search_next_ref_view;
5952 done:
5953 if (err)
5954 free_colors(&s->colors);
5955 return err;
5958 static const struct got_error *
5959 close_ref_view(struct tog_view *view)
5961 struct tog_ref_view_state *s = &view->state.ref;
5963 ref_view_free_refs(s);
5964 free_colors(&s->colors);
5966 return NULL;
5969 static const struct got_error *
5970 resolve_reflist_entry(struct got_object_id **commit_id,
5971 struct tog_reflist_entry *re, struct got_repository *repo)
5973 const struct got_error *err = NULL;
5974 struct got_object_id *obj_id;
5975 struct got_tag_object *tag = NULL;
5976 int obj_type;
5978 *commit_id = NULL;
5980 err = got_ref_resolve(&obj_id, repo, re->ref);
5981 if (err)
5982 return err;
5984 err = got_object_get_type(&obj_type, repo, obj_id);
5985 if (err)
5986 goto done;
5988 switch (obj_type) {
5989 case GOT_OBJ_TYPE_COMMIT:
5990 *commit_id = obj_id;
5991 break;
5992 case GOT_OBJ_TYPE_TAG:
5993 err = got_object_open_as_tag(&tag, repo, obj_id);
5994 if (err)
5995 goto done;
5996 free(obj_id);
5997 err = got_object_get_type(&obj_type, repo,
5998 got_object_tag_get_object_id(tag));
5999 if (err)
6000 goto done;
6001 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6002 err = got_error(GOT_ERR_OBJ_TYPE);
6003 goto done;
6005 *commit_id = got_object_id_dup(
6006 got_object_tag_get_object_id(tag));
6007 if (*commit_id == NULL) {
6008 err = got_error_from_errno("got_object_id_dup");
6009 goto done;
6011 break;
6012 default:
6013 err = got_error(GOT_ERR_OBJ_TYPE);
6014 break;
6017 done:
6018 if (tag)
6019 got_object_tag_close(tag);
6020 if (err) {
6021 free(*commit_id);
6022 *commit_id = NULL;
6024 return err;
6027 static const struct got_error *
6028 log_ref_entry(struct tog_view **new_view, int begin_x,
6029 struct tog_reflist_entry *re, struct got_repository *repo)
6031 struct tog_view *log_view;
6032 const struct got_error *err = NULL;
6033 struct got_object_id *commit_id = NULL;
6035 *new_view = NULL;
6037 err = resolve_reflist_entry(&commit_id, re, repo);
6038 if (err) {
6039 if (err->code != GOT_ERR_OBJ_TYPE)
6040 return err;
6041 else
6042 return NULL;
6045 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6046 if (log_view == NULL) {
6047 err = got_error_from_errno("view_open");
6048 goto done;
6051 err = open_log_view(log_view, commit_id, repo,
6052 got_ref_get_name(re->ref), "", 0);
6053 done:
6054 if (err)
6055 view_close(log_view);
6056 else
6057 *new_view = log_view;
6058 free(commit_id);
6059 return err;
6062 static void
6063 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6065 struct tog_reflist_entry *re;
6066 int i = 0;
6068 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6069 return;
6071 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6072 while (i++ < maxscroll) {
6073 if (re == NULL)
6074 break;
6075 s->first_displayed_entry = re;
6076 re = TAILQ_PREV(re, tog_reflist_head, entry);
6080 static void
6081 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6083 struct tog_reflist_entry *next, *last;
6084 int n = 0;
6086 if (s->first_displayed_entry)
6087 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6088 else
6089 next = TAILQ_FIRST(&s->refs);
6091 last = s->last_displayed_entry;
6092 while (next && last && n++ < maxscroll) {
6093 last = TAILQ_NEXT(last, entry);
6094 if (last) {
6095 s->first_displayed_entry = next;
6096 next = TAILQ_NEXT(next, entry);
6101 static const struct got_error *
6102 search_start_ref_view(struct tog_view *view)
6104 struct tog_ref_view_state *s = &view->state.ref;
6106 s->matched_entry = NULL;
6107 return NULL;
6110 static int
6111 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6113 regmatch_t regmatch;
6115 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6116 0) == 0;
6119 static const struct got_error *
6120 search_next_ref_view(struct tog_view *view)
6122 struct tog_ref_view_state *s = &view->state.ref;
6123 struct tog_reflist_entry *re = NULL;
6125 if (!view->searching) {
6126 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6127 return NULL;
6130 if (s->matched_entry) {
6131 if (view->searching == TOG_SEARCH_FORWARD) {
6132 if (s->selected_entry)
6133 re = TAILQ_NEXT(s->selected_entry, entry);
6134 else
6135 re = TAILQ_PREV(s->selected_entry,
6136 tog_reflist_head, entry);
6137 } else {
6138 if (s->selected_entry == NULL)
6139 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6140 else
6141 re = TAILQ_PREV(s->selected_entry,
6142 tog_reflist_head, entry);
6144 } else {
6145 if (s->selected_entry)
6146 re = s->selected_entry;
6147 else if (view->searching == TOG_SEARCH_FORWARD)
6148 re = TAILQ_FIRST(&s->refs);
6149 else
6150 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6153 while (1) {
6154 if (re == NULL) {
6155 if (s->matched_entry == NULL) {
6156 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6157 return NULL;
6159 if (view->searching == TOG_SEARCH_FORWARD)
6160 re = TAILQ_FIRST(&s->refs);
6161 else
6162 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6165 if (match_reflist_entry(re, &view->regex)) {
6166 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6167 s->matched_entry = re;
6168 break;
6171 if (view->searching == TOG_SEARCH_FORWARD)
6172 re = TAILQ_NEXT(re, entry);
6173 else
6174 re = TAILQ_PREV(re, tog_reflist_head, entry);
6177 if (s->matched_entry) {
6178 s->first_displayed_entry = s->matched_entry;
6179 s->selected = 0;
6182 return NULL;
6185 static const struct got_error *
6186 show_ref_view(struct tog_view *view)
6188 const struct got_error *err = NULL;
6189 struct tog_ref_view_state *s = &view->state.ref;
6190 struct tog_reflist_entry *re;
6191 char *line = NULL;
6192 wchar_t *wline;
6193 struct tog_color *tc;
6194 int width, n;
6195 int limit = view->nlines;
6197 werase(view->window);
6199 s->ndisplayed = 0;
6201 if (limit == 0)
6202 return NULL;
6204 re = s->first_displayed_entry;
6206 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6207 s->nrefs) == -1)
6208 return got_error_from_errno("asprintf");
6210 err = format_line(&wline, &width, line, view->ncols, 0);
6211 if (err) {
6212 free(line);
6213 return err;
6215 if (view_needs_focus_indication(view))
6216 wstandout(view->window);
6217 waddwstr(view->window, wline);
6218 if (view_needs_focus_indication(view))
6219 wstandend(view->window);
6220 free(wline);
6221 wline = NULL;
6222 free(line);
6223 line = NULL;
6224 if (width < view->ncols - 1)
6225 waddch(view->window, '\n');
6226 if (--limit <= 0)
6227 return NULL;
6229 n = 0;
6230 while (re && limit > 0) {
6231 char *line = NULL;
6233 if (got_ref_is_symbolic(re->ref)) {
6234 if (asprintf(&line, "%s -> %s",
6235 got_ref_get_name(re->ref),
6236 got_ref_get_symref_target(re->ref)) == -1)
6237 return got_error_from_errno("asprintf");
6238 } else if (s->show_ids) {
6239 struct got_object_id *id;
6240 char *id_str;
6241 err = got_ref_resolve(&id, s->repo, re->ref);
6242 if (err)
6243 return err;
6244 err = got_object_id_str(&id_str, id);
6245 if (err) {
6246 free(id);
6247 return err;
6249 if (asprintf(&line, "%s: %s",
6250 got_ref_get_name(re->ref), id_str) == -1) {
6251 err = got_error_from_errno("asprintf");
6252 free(id);
6253 free(id_str);
6254 return err;
6256 free(id);
6257 free(id_str);
6258 } else {
6259 line = strdup(got_ref_get_name(re->ref));
6260 if (line == NULL)
6261 return got_error_from_errno("strdup");
6264 err = format_line(&wline, &width, line, view->ncols, 0);
6265 if (err) {
6266 free(line);
6267 return err;
6269 if (n == s->selected) {
6270 if (view->focussed)
6271 wstandout(view->window);
6272 s->selected_entry = re;
6274 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6275 if (tc)
6276 wattr_on(view->window,
6277 COLOR_PAIR(tc->colorpair), NULL);
6278 waddwstr(view->window, wline);
6279 if (tc)
6280 wattr_off(view->window,
6281 COLOR_PAIR(tc->colorpair), NULL);
6282 if (width < view->ncols - 1)
6283 waddch(view->window, '\n');
6284 if (n == s->selected && view->focussed)
6285 wstandend(view->window);
6286 free(line);
6287 free(wline);
6288 wline = NULL;
6289 n++;
6290 s->ndisplayed++;
6291 s->last_displayed_entry = re;
6293 limit--;
6294 re = TAILQ_NEXT(re, entry);
6297 view_vborder(view);
6298 return err;
6301 static const struct got_error *
6302 browse_ref_tree(struct tog_view **new_view, int begin_x,
6303 struct tog_reflist_entry *re, struct got_repository *repo)
6305 const struct got_error *err = NULL;
6306 struct got_object_id *commit_id = NULL;
6307 struct tog_view *tree_view;
6309 *new_view = NULL;
6311 err = resolve_reflist_entry(&commit_id, re, repo);
6312 if (err) {
6313 if (err->code != GOT_ERR_OBJ_TYPE)
6314 return err;
6315 else
6316 return NULL;
6320 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6321 if (tree_view == NULL) {
6322 err = got_error_from_errno("view_open");
6323 goto done;
6326 err = open_tree_view(tree_view, commit_id,
6327 got_ref_get_name(re->ref), repo);
6328 if (err)
6329 goto done;
6331 *new_view = tree_view;
6332 done:
6333 free(commit_id);
6334 return err;
6336 static const struct got_error *
6337 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6339 const struct got_error *err = NULL;
6340 struct tog_ref_view_state *s = &view->state.ref;
6341 struct tog_view *log_view, *tree_view;
6342 struct tog_reflist_entry *re;
6343 int begin_x = 0, n;
6345 switch (ch) {
6346 case 'i':
6347 s->show_ids = !s->show_ids;
6348 break;
6349 case 'o':
6350 s->sort_by_date = !s->sort_by_date;
6351 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6352 got_ref_cmp_by_commit_timestamp_descending :
6353 tog_ref_cmp_by_name, s->repo);
6354 if (err)
6355 break;
6356 got_reflist_object_id_map_free(tog_refs_idmap);
6357 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6358 &tog_refs, s->repo);
6359 if (err)
6360 break;
6361 ref_view_free_refs(s);
6362 err = ref_view_load_refs(s);
6363 break;
6364 case KEY_ENTER:
6365 case '\r':
6366 if (!s->selected_entry)
6367 break;
6368 if (view_is_parent_view(view))
6369 begin_x = view_split_begin_x(view->begin_x);
6370 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6371 s->repo);
6372 view->focussed = 0;
6373 log_view->focussed = 1;
6374 if (view_is_parent_view(view)) {
6375 err = view_close_child(view);
6376 if (err)
6377 return err;
6378 view_set_child(view, log_view);
6379 view->focus_child = 1;
6380 } else
6381 *new_view = log_view;
6382 break;
6383 case 't':
6384 if (!s->selected_entry)
6385 break;
6386 if (view_is_parent_view(view))
6387 begin_x = view_split_begin_x(view->begin_x);
6388 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6389 s->repo);
6390 if (err || tree_view == NULL)
6391 break;
6392 view->focussed = 0;
6393 tree_view->focussed = 1;
6394 if (view_is_parent_view(view)) {
6395 err = view_close_child(view);
6396 if (err)
6397 return err;
6398 view_set_child(view, tree_view);
6399 view->focus_child = 1;
6400 } else
6401 *new_view = tree_view;
6402 break;
6403 case 'g':
6404 case KEY_HOME:
6405 s->selected = 0;
6406 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6407 break;
6408 case 'G':
6409 case KEY_END:
6410 s->selected = 0;
6411 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6412 for (n = 0; n < view->nlines - 1; n++) {
6413 if (re == NULL)
6414 break;
6415 s->first_displayed_entry = re;
6416 re = TAILQ_PREV(re, tog_reflist_head, entry);
6418 if (n > 0)
6419 s->selected = n - 1;
6420 break;
6421 case 'k':
6422 case KEY_UP:
6423 case CTRL('p'):
6424 if (s->selected > 0) {
6425 s->selected--;
6426 break;
6428 ref_scroll_up(s, 1);
6429 break;
6430 case KEY_PPAGE:
6431 case CTRL('b'):
6432 case CTRL('u'):
6433 case 'u':
6434 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6435 s->selected = 0;
6436 ref_scroll_up(s, MAX(0, view->nlines - 1));
6437 break;
6438 case 'j':
6439 case KEY_DOWN:
6440 case CTRL('n'):
6441 if (s->selected < s->ndisplayed - 1) {
6442 s->selected++;
6443 break;
6445 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6446 /* can't scroll any further */
6447 break;
6448 ref_scroll_down(s, 1);
6449 break;
6450 case KEY_NPAGE:
6451 case CTRL('f'):
6452 case CTRL('d'):
6453 case 'd':
6454 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6455 /* can't scroll any further; move cursor down */
6456 if (s->selected < s->ndisplayed - 1)
6457 s->selected = s->ndisplayed - 1;
6458 break;
6460 ref_scroll_down(s, view->nlines - 1);
6461 break;
6462 case CTRL('l'):
6463 tog_free_refs();
6464 err = tog_load_refs(s->repo, s->sort_by_date);
6465 if (err)
6466 break;
6467 ref_view_free_refs(s);
6468 err = ref_view_load_refs(s);
6469 break;
6470 case KEY_RESIZE:
6471 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6472 s->selected = view->nlines - 2;
6473 break;
6474 default:
6475 break;
6478 return err;
6481 __dead static void
6482 usage_ref(void)
6484 endwin();
6485 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6486 getprogname());
6487 exit(1);
6490 static const struct got_error *
6491 cmd_ref(int argc, char *argv[])
6493 const struct got_error *error;
6494 struct got_repository *repo = NULL;
6495 struct got_worktree *worktree = NULL;
6496 char *cwd = NULL, *repo_path = NULL;
6497 int ch;
6498 struct tog_view *view;
6500 while ((ch = getopt(argc, argv, "r:")) != -1) {
6501 switch (ch) {
6502 case 'r':
6503 repo_path = realpath(optarg, NULL);
6504 if (repo_path == NULL)
6505 return got_error_from_errno2("realpath",
6506 optarg);
6507 break;
6508 default:
6509 usage_ref();
6510 /* NOTREACHED */
6514 argc -= optind;
6515 argv += optind;
6517 if (argc > 1)
6518 usage_ref();
6520 if (repo_path == NULL) {
6521 cwd = getcwd(NULL, 0);
6522 if (cwd == NULL)
6523 return got_error_from_errno("getcwd");
6524 error = got_worktree_open(&worktree, cwd);
6525 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6526 goto done;
6527 if (worktree)
6528 repo_path =
6529 strdup(got_worktree_get_repo_path(worktree));
6530 else
6531 repo_path = strdup(cwd);
6532 if (repo_path == NULL) {
6533 error = got_error_from_errno("strdup");
6534 goto done;
6538 error = got_repo_open(&repo, repo_path, NULL);
6539 if (error != NULL)
6540 goto done;
6542 init_curses();
6544 error = apply_unveil(got_repo_get_path(repo), NULL);
6545 if (error)
6546 goto done;
6548 error = tog_load_refs(repo, 0);
6549 if (error)
6550 goto done;
6552 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6553 if (view == NULL) {
6554 error = got_error_from_errno("view_open");
6555 goto done;
6558 error = open_ref_view(view, repo);
6559 if (error)
6560 goto done;
6562 if (worktree) {
6563 /* Release work tree lock. */
6564 got_worktree_close(worktree);
6565 worktree = NULL;
6567 error = view_loop(view);
6568 done:
6569 free(repo_path);
6570 free(cwd);
6571 if (repo) {
6572 const struct got_error *close_err = got_repo_close(repo);
6573 if (close_err)
6574 error = close_err;
6576 tog_free_refs();
6577 return error;
6580 static void
6581 list_commands(FILE *fp)
6583 size_t i;
6585 fprintf(fp, "commands:");
6586 for (i = 0; i < nitems(tog_commands); i++) {
6587 const struct tog_cmd *cmd = &tog_commands[i];
6588 fprintf(fp, " %s", cmd->name);
6590 fputc('\n', fp);
6593 __dead static void
6594 usage(int hflag, int status)
6596 FILE *fp = (status == 0) ? stdout : stderr;
6598 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6599 getprogname());
6600 if (hflag) {
6601 fprintf(fp, "lazy usage: %s path\n", getprogname());
6602 list_commands(fp);
6604 exit(status);
6607 static char **
6608 make_argv(int argc, ...)
6610 va_list ap;
6611 char **argv;
6612 int i;
6614 va_start(ap, argc);
6616 argv = calloc(argc, sizeof(char *));
6617 if (argv == NULL)
6618 err(1, "calloc");
6619 for (i = 0; i < argc; i++) {
6620 argv[i] = strdup(va_arg(ap, char *));
6621 if (argv[i] == NULL)
6622 err(1, "strdup");
6625 va_end(ap);
6626 return argv;
6630 * Try to convert 'tog path' into a 'tog log path' command.
6631 * The user could simply have mistyped the command rather than knowingly
6632 * provided a path. So check whether argv[0] can in fact be resolved
6633 * to a path in the HEAD commit and print a special error if not.
6634 * This hack is for mpi@ <3
6636 static const struct got_error *
6637 tog_log_with_path(int argc, char *argv[])
6639 const struct got_error *error = NULL, *close_err;
6640 const struct tog_cmd *cmd = NULL;
6641 struct got_repository *repo = NULL;
6642 struct got_worktree *worktree = NULL;
6643 struct got_object_id *commit_id = NULL, *id = NULL;
6644 struct got_commit_object *commit = NULL;
6645 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6646 char *commit_id_str = NULL, **cmd_argv = NULL;
6648 cwd = getcwd(NULL, 0);
6649 if (cwd == NULL)
6650 return got_error_from_errno("getcwd");
6652 error = got_worktree_open(&worktree, cwd);
6653 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6654 goto done;
6656 if (worktree)
6657 repo_path = strdup(got_worktree_get_repo_path(worktree));
6658 else
6659 repo_path = strdup(cwd);
6660 if (repo_path == NULL) {
6661 error = got_error_from_errno("strdup");
6662 goto done;
6665 error = got_repo_open(&repo, repo_path, NULL);
6666 if (error != NULL)
6667 goto done;
6669 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6670 repo, worktree);
6671 if (error)
6672 goto done;
6674 error = tog_load_refs(repo, 0);
6675 if (error)
6676 goto done;
6677 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6678 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6679 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6680 if (error)
6681 goto done;
6683 if (worktree) {
6684 got_worktree_close(worktree);
6685 worktree = NULL;
6688 error = got_object_open_as_commit(&commit, repo, commit_id);
6689 if (error)
6690 goto done;
6692 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6693 if (error) {
6694 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6695 goto done;
6696 fprintf(stderr, "%s: '%s' is no known command or path\n",
6697 getprogname(), argv[0]);
6698 usage(1, 1);
6699 /* not reached */
6702 close_err = got_repo_close(repo);
6703 if (error == NULL)
6704 error = close_err;
6705 repo = NULL;
6707 error = got_object_id_str(&commit_id_str, commit_id);
6708 if (error)
6709 goto done;
6711 cmd = &tog_commands[0]; /* log */
6712 argc = 4;
6713 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6714 error = cmd->cmd_main(argc, cmd_argv);
6715 done:
6716 if (repo) {
6717 close_err = got_repo_close(repo);
6718 if (error == NULL)
6719 error = close_err;
6721 if (commit)
6722 got_object_commit_close(commit);
6723 if (worktree)
6724 got_worktree_close(worktree);
6725 free(id);
6726 free(commit_id_str);
6727 free(commit_id);
6728 free(cwd);
6729 free(repo_path);
6730 free(in_repo_path);
6731 if (cmd_argv) {
6732 int i;
6733 for (i = 0; i < argc; i++)
6734 free(cmd_argv[i]);
6735 free(cmd_argv);
6737 tog_free_refs();
6738 return error;
6741 int
6742 main(int argc, char *argv[])
6744 const struct got_error *error = NULL;
6745 const struct tog_cmd *cmd = NULL;
6746 int ch, hflag = 0, Vflag = 0;
6747 char **cmd_argv = NULL;
6748 static const struct option longopts[] = {
6749 { "version", no_argument, NULL, 'V' },
6750 { NULL, 0, NULL, 0}
6753 setlocale(LC_CTYPE, "");
6755 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6756 switch (ch) {
6757 case 'h':
6758 hflag = 1;
6759 break;
6760 case 'V':
6761 Vflag = 1;
6762 break;
6763 default:
6764 usage(hflag, 1);
6765 /* NOTREACHED */
6769 argc -= optind;
6770 argv += optind;
6771 optind = 1;
6772 optreset = 1;
6774 if (Vflag) {
6775 got_version_print_str();
6776 return 0;
6779 #ifndef PROFILE
6780 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6781 NULL) == -1)
6782 err(1, "pledge");
6783 #endif
6785 if (argc == 0) {
6786 if (hflag)
6787 usage(hflag, 0);
6788 /* Build an argument vector which runs a default command. */
6789 cmd = &tog_commands[0];
6790 argc = 1;
6791 cmd_argv = make_argv(argc, cmd->name);
6792 } else {
6793 size_t i;
6795 /* Did the user specify a command? */
6796 for (i = 0; i < nitems(tog_commands); i++) {
6797 if (strncmp(tog_commands[i].name, argv[0],
6798 strlen(argv[0])) == 0) {
6799 cmd = &tog_commands[i];
6800 break;
6805 if (cmd == NULL) {
6806 if (argc != 1)
6807 usage(0, 1);
6808 /* No command specified; try log with a path */
6809 error = tog_log_with_path(argc, argv);
6810 } else {
6811 if (hflag)
6812 cmd->cmd_usage();
6813 else
6814 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6817 endwin();
6818 putchar('\n');
6819 if (cmd_argv) {
6820 int i;
6821 for (i = 0; i < argc; i++)
6822 free(cmd_argv[i]);
6823 free(cmd_argv);
6826 if (error && error->code != GOT_ERR_CANCELLED)
6827 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6828 return 0;