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;
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;
599 static void
600 tog_sigwinch(int signo)
602 tog_sigwinch_received = 1;
605 static void
606 tog_sigpipe(int signo)
608 tog_sigpipe_received = 1;
611 static void
612 tog_sigcont(int signo)
614 tog_sigcont_received = 1;
617 static const struct got_error *
618 view_close(struct tog_view *view)
620 const struct got_error *err = NULL;
622 if (view->child) {
623 view_close(view->child);
624 view->child = NULL;
626 if (view->close)
627 err = view->close(view);
628 if (view->panel)
629 del_panel(view->panel);
630 if (view->window)
631 delwin(view->window);
632 free(view);
633 return err;
636 static struct tog_view *
637 view_open(int nlines, int ncols, int begin_y, int begin_x,
638 enum tog_view_type type)
640 struct tog_view *view = calloc(1, sizeof(*view));
642 if (view == NULL)
643 return NULL;
645 view->type = type;
646 view->lines = LINES;
647 view->cols = COLS;
648 view->nlines = nlines ? nlines : LINES - begin_y;
649 view->ncols = ncols ? ncols : COLS - begin_x;
650 view->begin_y = begin_y;
651 view->begin_x = begin_x;
652 view->window = newwin(nlines, ncols, begin_y, begin_x);
653 if (view->window == NULL) {
654 view_close(view);
655 return NULL;
657 view->panel = new_panel(view->window);
658 if (view->panel == NULL ||
659 set_panel_userptr(view->panel, view) != OK) {
660 view_close(view);
661 return NULL;
664 keypad(view->window, TRUE);
665 return view;
668 static int
669 view_split_begin_x(int begin_x)
671 if (begin_x > 0 || COLS < 120)
672 return 0;
673 return (COLS - MAX(COLS / 2, 80));
676 static const struct got_error *view_resize(struct tog_view *);
678 static const struct got_error *
679 view_splitscreen(struct tog_view *view)
681 const struct got_error *err = NULL;
683 view->begin_y = 0;
684 view->begin_x = view_split_begin_x(0);
685 view->nlines = LINES;
686 view->ncols = COLS - view->begin_x;
687 view->lines = LINES;
688 view->cols = COLS;
689 err = view_resize(view);
690 if (err)
691 return err;
693 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
694 return got_error_from_errno("mvwin");
696 return NULL;
699 static const struct got_error *
700 view_fullscreen(struct tog_view *view)
702 const struct got_error *err = NULL;
704 view->begin_x = 0;
705 view->begin_y = 0;
706 view->nlines = LINES;
707 view->ncols = COLS;
708 view->lines = LINES;
709 view->cols = COLS;
710 err = view_resize(view);
711 if (err)
712 return err;
714 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
715 return got_error_from_errno("mvwin");
717 return NULL;
720 static int
721 view_is_parent_view(struct tog_view *view)
723 return view->parent == NULL;
726 static const struct got_error *
727 view_resize(struct tog_view *view)
729 int nlines, ncols;
731 if (view->lines > LINES)
732 nlines = view->nlines - (view->lines - LINES);
733 else
734 nlines = view->nlines + (LINES - view->lines);
736 if (view->cols > COLS)
737 ncols = view->ncols - (view->cols - COLS);
738 else
739 ncols = view->ncols + (COLS - view->cols);
741 if (wresize(view->window, nlines, ncols) == ERR)
742 return got_error_from_errno("wresize");
743 if (replace_panel(view->panel, view->window) == ERR)
744 return got_error_from_errno("replace_panel");
745 wclear(view->window);
747 view->nlines = nlines;
748 view->ncols = ncols;
749 view->lines = LINES;
750 view->cols = COLS;
752 if (view->child) {
753 view->child->begin_x = view_split_begin_x(view->begin_x);
754 if (view->child->begin_x == 0) {
755 view_fullscreen(view->child);
756 if (view->child->focussed)
757 show_panel(view->child->panel);
758 else
759 show_panel(view->panel);
760 } else {
761 view_splitscreen(view->child);
762 show_panel(view->child->panel);
766 return NULL;
769 static const struct got_error *
770 view_close_child(struct tog_view *view)
772 const struct got_error *err = NULL;
774 if (view->child == NULL)
775 return NULL;
777 err = view_close(view->child);
778 view->child = NULL;
779 return err;
782 static void
783 view_set_child(struct tog_view *view, struct tog_view *child)
785 view->child = child;
786 child->parent = view;
789 static int
790 view_is_splitscreen(struct tog_view *view)
792 return view->begin_x > 0;
795 static void
796 tog_resizeterm(void)
798 int cols, lines;
799 struct winsize size;
801 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
802 cols = 80; /* Default */
803 lines = 24;
804 } else {
805 cols = size.ws_col;
806 lines = size.ws_row;
808 resize_term(lines, cols);
811 static const struct got_error *
812 view_search_start(struct tog_view *view)
814 const struct got_error *err = NULL;
815 char pattern[1024];
816 int ret;
818 if (view->search_started) {
819 regfree(&view->regex);
820 view->searching = 0;
821 memset(&view->regmatch, 0, sizeof(view->regmatch));
823 view->search_started = 0;
825 if (view->nlines < 1)
826 return NULL;
828 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
829 wclrtoeol(view->window);
831 nocbreak();
832 echo();
833 ret = wgetnstr(view->window, pattern, sizeof(pattern));
834 cbreak();
835 noecho();
836 if (ret == ERR)
837 return NULL;
839 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
840 err = view->search_start(view);
841 if (err) {
842 regfree(&view->regex);
843 return err;
845 view->search_started = 1;
846 view->searching = TOG_SEARCH_FORWARD;
847 view->search_next_done = 0;
848 view->search_next(view);
851 return NULL;
854 static const struct got_error *
855 view_input(struct tog_view **new, int *done, struct tog_view *view,
856 struct tog_view_list_head *views)
858 const struct got_error *err = NULL;
859 struct tog_view *v;
860 int ch, errcode;
862 *new = NULL;
864 /* Clear "no matches" indicator. */
865 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
866 view->search_next_done == TOG_SEARCH_HAVE_NONE)
867 view->search_next_done = TOG_SEARCH_HAVE_MORE;
869 if (view->searching && !view->search_next_done) {
870 errcode = pthread_mutex_unlock(&tog_mutex);
871 if (errcode)
872 return got_error_set_errno(errcode,
873 "pthread_mutex_unlock");
874 sched_yield();
875 errcode = pthread_mutex_lock(&tog_mutex);
876 if (errcode)
877 return got_error_set_errno(errcode,
878 "pthread_mutex_lock");
879 view->search_next(view);
880 return NULL;
883 nodelay(stdscr, FALSE);
884 /* Allow threads to make progress while we are waiting for input. */
885 errcode = pthread_mutex_unlock(&tog_mutex);
886 if (errcode)
887 return got_error_set_errno(errcode, "pthread_mutex_unlock");
888 ch = wgetch(view->window);
889 errcode = pthread_mutex_lock(&tog_mutex);
890 if (errcode)
891 return got_error_set_errno(errcode, "pthread_mutex_lock");
892 nodelay(stdscr, TRUE);
894 if (tog_sigwinch_received || tog_sigcont_received) {
895 tog_resizeterm();
896 tog_sigwinch_received = 0;
897 tog_sigcont_received = 0;
898 TAILQ_FOREACH(v, views, entry) {
899 err = view_resize(v);
900 if (err)
901 return err;
902 err = v->input(new, v, KEY_RESIZE);
903 if (err)
904 return err;
905 if (v->child) {
906 err = view_resize(v->child);
907 if (err)
908 return err;
909 err = v->child->input(new, v->child,
910 KEY_RESIZE);
911 if (err)
912 return err;
917 switch (ch) {
918 case '\t':
919 if (view->child) {
920 view->focussed = 0;
921 view->child->focussed = 1;
922 view->focus_child = 1;
923 } else if (view->parent) {
924 view->focussed = 0;
925 view->parent->focussed = 1;
926 view->parent->focus_child = 0;
928 break;
929 case 'q':
930 err = view->input(new, view, ch);
931 view->dying = 1;
932 break;
933 case 'Q':
934 *done = 1;
935 break;
936 case 'f':
937 if (view_is_parent_view(view)) {
938 if (view->child == NULL)
939 break;
940 if (view_is_splitscreen(view->child)) {
941 view->focussed = 0;
942 view->child->focussed = 1;
943 err = view_fullscreen(view->child);
944 } else
945 err = view_splitscreen(view->child);
946 if (err)
947 break;
948 err = view->child->input(new, view->child,
949 KEY_RESIZE);
950 } else {
951 if (view_is_splitscreen(view)) {
952 view->parent->focussed = 0;
953 view->focussed = 1;
954 err = view_fullscreen(view);
955 } else {
956 err = view_splitscreen(view);
958 if (err)
959 break;
960 err = view->input(new, view, KEY_RESIZE);
962 break;
963 case KEY_RESIZE:
964 break;
965 case '/':
966 if (view->search_start)
967 view_search_start(view);
968 else
969 err = view->input(new, view, ch);
970 break;
971 case 'N':
972 case 'n':
973 if (view->search_started && view->search_next) {
974 view->searching = (ch == 'n' ?
975 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
976 view->search_next_done = 0;
977 view->search_next(view);
978 } else
979 err = view->input(new, view, ch);
980 break;
981 default:
982 err = view->input(new, view, ch);
983 break;
986 return err;
989 void
990 view_vborder(struct tog_view *view)
992 PANEL *panel;
993 const struct tog_view *view_above;
995 if (view->parent)
996 return view_vborder(view->parent);
998 panel = panel_above(view->panel);
999 if (panel == NULL)
1000 return;
1002 view_above = panel_userptr(panel);
1003 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1004 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1007 int
1008 view_needs_focus_indication(struct tog_view *view)
1010 if (view_is_parent_view(view)) {
1011 if (view->child == NULL || view->child->focussed)
1012 return 0;
1013 if (!view_is_splitscreen(view->child))
1014 return 0;
1015 } else if (!view_is_splitscreen(view))
1016 return 0;
1018 return view->focussed;
1021 static const struct got_error *
1022 view_loop(struct tog_view *view)
1024 const struct got_error *err = NULL;
1025 struct tog_view_list_head views;
1026 struct tog_view *new_view;
1027 int fast_refresh = 10;
1028 int done = 0, errcode;
1030 errcode = pthread_mutex_lock(&tog_mutex);
1031 if (errcode)
1032 return got_error_set_errno(errcode, "pthread_mutex_lock");
1034 TAILQ_INIT(&views);
1035 TAILQ_INSERT_HEAD(&views, view, entry);
1037 view->focussed = 1;
1038 err = view->show(view);
1039 if (err)
1040 return err;
1041 update_panels();
1042 doupdate();
1043 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1044 /* Refresh fast during initialization, then become slower. */
1045 if (fast_refresh && fast_refresh-- == 0)
1046 halfdelay(10); /* switch to once per second */
1048 err = view_input(&new_view, &done, view, &views);
1049 if (err)
1050 break;
1051 if (view->dying) {
1052 struct tog_view *v, *prev = NULL;
1054 if (view_is_parent_view(view))
1055 prev = TAILQ_PREV(view, tog_view_list_head,
1056 entry);
1057 else if (view->parent)
1058 prev = view->parent;
1060 if (view->parent) {
1061 view->parent->child = NULL;
1062 view->parent->focus_child = 0;
1063 } else
1064 TAILQ_REMOVE(&views, view, entry);
1066 err = view_close(view);
1067 if (err)
1068 goto done;
1070 view = NULL;
1071 TAILQ_FOREACH(v, &views, entry) {
1072 if (v->focussed)
1073 break;
1075 if (view == NULL && new_view == NULL) {
1076 /* No view has focus. Try to pick one. */
1077 if (prev)
1078 view = prev;
1079 else if (!TAILQ_EMPTY(&views)) {
1080 view = TAILQ_LAST(&views,
1081 tog_view_list_head);
1083 if (view) {
1084 if (view->focus_child) {
1085 view->child->focussed = 1;
1086 view = view->child;
1087 } else
1088 view->focussed = 1;
1092 if (new_view) {
1093 struct tog_view *v, *t;
1094 /* Only allow one parent view per type. */
1095 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1096 if (v->type != new_view->type)
1097 continue;
1098 TAILQ_REMOVE(&views, v, entry);
1099 err = view_close(v);
1100 if (err)
1101 goto done;
1102 break;
1104 TAILQ_INSERT_TAIL(&views, new_view, entry);
1105 view = new_view;
1107 if (view) {
1108 if (view_is_parent_view(view)) {
1109 if (view->child && view->child->focussed)
1110 view = view->child;
1111 } else {
1112 if (view->parent && view->parent->focussed)
1113 view = view->parent;
1115 show_panel(view->panel);
1116 if (view->child && view_is_splitscreen(view->child))
1117 show_panel(view->child->panel);
1118 if (view->parent && view_is_splitscreen(view)) {
1119 err = view->parent->show(view->parent);
1120 if (err)
1121 goto done;
1123 err = view->show(view);
1124 if (err)
1125 goto done;
1126 if (view->child) {
1127 err = view->child->show(view->child);
1128 if (err)
1129 goto done;
1131 update_panels();
1132 doupdate();
1135 done:
1136 while (!TAILQ_EMPTY(&views)) {
1137 view = TAILQ_FIRST(&views);
1138 TAILQ_REMOVE(&views, view, entry);
1139 view_close(view);
1142 errcode = pthread_mutex_unlock(&tog_mutex);
1143 if (errcode && err == NULL)
1144 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1146 return err;
1149 __dead static void
1150 usage_log(void)
1152 endwin();
1153 fprintf(stderr,
1154 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1155 getprogname());
1156 exit(1);
1159 /* Create newly allocated wide-character string equivalent to a byte string. */
1160 static const struct got_error *
1161 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1163 char *vis = NULL;
1164 const struct got_error *err = NULL;
1166 *ws = NULL;
1167 *wlen = mbstowcs(NULL, s, 0);
1168 if (*wlen == (size_t)-1) {
1169 int vislen;
1170 if (errno != EILSEQ)
1171 return got_error_from_errno("mbstowcs");
1173 /* byte string invalid in current encoding; try to "fix" it */
1174 err = got_mbsavis(&vis, &vislen, s);
1175 if (err)
1176 return err;
1177 *wlen = mbstowcs(NULL, vis, 0);
1178 if (*wlen == (size_t)-1) {
1179 err = got_error_from_errno("mbstowcs"); /* give up */
1180 goto done;
1184 *ws = calloc(*wlen + 1, sizeof(**ws));
1185 if (*ws == NULL) {
1186 err = got_error_from_errno("calloc");
1187 goto done;
1190 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1191 err = got_error_from_errno("mbstowcs");
1192 done:
1193 free(vis);
1194 if (err) {
1195 free(*ws);
1196 *ws = NULL;
1197 *wlen = 0;
1199 return err;
1202 /* Format a line for display, ensuring that it won't overflow a width limit. */
1203 static const struct got_error *
1204 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1205 int col_tab_align)
1207 const struct got_error *err = NULL;
1208 int cols = 0;
1209 wchar_t *wline = NULL;
1210 size_t wlen;
1211 int i;
1213 *wlinep = NULL;
1214 *widthp = 0;
1216 err = mbs2ws(&wline, &wlen, line);
1217 if (err)
1218 return err;
1220 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1221 wline[wlen - 1] = L'\0';
1222 wlen--;
1224 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1225 wline[wlen - 1] = L'\0';
1226 wlen--;
1229 i = 0;
1230 while (i < wlen) {
1231 int width = wcwidth(wline[i]);
1233 if (width == 0) {
1234 i++;
1235 continue;
1238 if (width == 1 || width == 2) {
1239 if (cols + width > wlimit)
1240 break;
1241 cols += width;
1242 i++;
1243 } else if (width == -1) {
1244 if (wline[i] == L'\t') {
1245 width = TABSIZE -
1246 ((cols + col_tab_align) % TABSIZE);
1247 } else {
1248 width = 1;
1249 wline[i] = L'.';
1251 if (cols + width > wlimit)
1252 break;
1253 cols += width;
1254 i++;
1255 } else {
1256 err = got_error_from_errno("wcwidth");
1257 goto done;
1260 wline[i] = L'\0';
1261 if (widthp)
1262 *widthp = cols;
1263 done:
1264 if (err)
1265 free(wline);
1266 else
1267 *wlinep = wline;
1268 return err;
1271 static const struct got_error*
1272 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1273 struct got_object_id *id, struct got_repository *repo)
1275 static const struct got_error *err = NULL;
1276 struct got_reflist_entry *re;
1277 char *s;
1278 const char *name;
1280 *refs_str = NULL;
1282 TAILQ_FOREACH(re, refs, entry) {
1283 struct got_tag_object *tag = NULL;
1284 struct got_object_id *ref_id;
1285 int cmp;
1287 name = got_ref_get_name(re->ref);
1288 if (strcmp(name, GOT_REF_HEAD) == 0)
1289 continue;
1290 if (strncmp(name, "refs/", 5) == 0)
1291 name += 5;
1292 if (strncmp(name, "got/", 4) == 0 &&
1293 strncmp(name, "got/backup/", 11) != 0)
1294 continue;
1295 if (strncmp(name, "heads/", 6) == 0)
1296 name += 6;
1297 if (strncmp(name, "remotes/", 8) == 0) {
1298 name += 8;
1299 s = strstr(name, "/" GOT_REF_HEAD);
1300 if (s != NULL && s[strlen(s)] == '\0')
1301 continue;
1303 err = got_ref_resolve(&ref_id, repo, re->ref);
1304 if (err)
1305 break;
1306 if (strncmp(name, "tags/", 5) == 0) {
1307 err = got_object_open_as_tag(&tag, repo, ref_id);
1308 if (err) {
1309 if (err->code != GOT_ERR_OBJ_TYPE) {
1310 free(ref_id);
1311 break;
1313 /* Ref points at something other than a tag. */
1314 err = NULL;
1315 tag = NULL;
1318 cmp = got_object_id_cmp(tag ?
1319 got_object_tag_get_object_id(tag) : ref_id, id);
1320 free(ref_id);
1321 if (tag)
1322 got_object_tag_close(tag);
1323 if (cmp != 0)
1324 continue;
1325 s = *refs_str;
1326 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1327 s ? ", " : "", name) == -1) {
1328 err = got_error_from_errno("asprintf");
1329 free(s);
1330 *refs_str = NULL;
1331 break;
1333 free(s);
1336 return err;
1339 static const struct got_error *
1340 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1341 int col_tab_align)
1343 char *smallerthan;
1345 smallerthan = strchr(author, '<');
1346 if (smallerthan && smallerthan[1] != '\0')
1347 author = smallerthan + 1;
1348 author[strcspn(author, "@>")] = '\0';
1349 return format_line(wauthor, author_width, author, limit, col_tab_align);
1352 static const struct got_error *
1353 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1354 struct got_object_id *id, const size_t date_display_cols,
1355 int author_display_cols)
1357 struct tog_log_view_state *s = &view->state.log;
1358 const struct got_error *err = NULL;
1359 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1360 char *logmsg0 = NULL, *logmsg = NULL;
1361 char *author = NULL;
1362 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1363 int author_width, logmsg_width;
1364 char *newline, *line = NULL;
1365 int col, limit;
1366 const int avail = view->ncols;
1367 struct tm tm;
1368 time_t committer_time;
1369 struct tog_color *tc;
1371 committer_time = got_object_commit_get_committer_time(commit);
1372 if (gmtime_r(&committer_time, &tm) == NULL)
1373 return got_error_from_errno("gmtime_r");
1374 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1375 return got_error(GOT_ERR_NO_SPACE);
1377 if (avail <= date_display_cols)
1378 limit = MIN(sizeof(datebuf) - 1, avail);
1379 else
1380 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1381 tc = get_color(&s->colors, TOG_COLOR_DATE);
1382 if (tc)
1383 wattr_on(view->window,
1384 COLOR_PAIR(tc->colorpair), NULL);
1385 waddnstr(view->window, datebuf, limit);
1386 if (tc)
1387 wattr_off(view->window,
1388 COLOR_PAIR(tc->colorpair), NULL);
1389 col = limit;
1390 if (col > avail)
1391 goto done;
1393 if (avail >= 120) {
1394 char *id_str;
1395 err = got_object_id_str(&id_str, id);
1396 if (err)
1397 goto done;
1398 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1399 if (tc)
1400 wattr_on(view->window,
1401 COLOR_PAIR(tc->colorpair), NULL);
1402 wprintw(view->window, "%.8s ", id_str);
1403 if (tc)
1404 wattr_off(view->window,
1405 COLOR_PAIR(tc->colorpair), NULL);
1406 free(id_str);
1407 col += 9;
1408 if (col > avail)
1409 goto done;
1412 author = strdup(got_object_commit_get_author(commit));
1413 if (author == NULL) {
1414 err = got_error_from_errno("strdup");
1415 goto done;
1417 err = format_author(&wauthor, &author_width, author, avail - col, col);
1418 if (err)
1419 goto done;
1420 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1421 if (tc)
1422 wattr_on(view->window,
1423 COLOR_PAIR(tc->colorpair), NULL);
1424 waddwstr(view->window, wauthor);
1425 if (tc)
1426 wattr_off(view->window,
1427 COLOR_PAIR(tc->colorpair), NULL);
1428 col += author_width;
1429 while (col < avail && author_width < author_display_cols + 2) {
1430 waddch(view->window, ' ');
1431 col++;
1432 author_width++;
1434 if (col > avail)
1435 goto done;
1437 err = got_object_commit_get_logmsg(&logmsg0, commit);
1438 if (err)
1439 goto done;
1440 logmsg = logmsg0;
1441 while (*logmsg == '\n')
1442 logmsg++;
1443 newline = strchr(logmsg, '\n');
1444 if (newline)
1445 *newline = '\0';
1446 limit = avail - col;
1447 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1448 if (err)
1449 goto done;
1450 waddwstr(view->window, wlogmsg);
1451 col += logmsg_width;
1452 while (col < avail) {
1453 waddch(view->window, ' ');
1454 col++;
1456 done:
1457 free(logmsg0);
1458 free(wlogmsg);
1459 free(author);
1460 free(wauthor);
1461 free(line);
1462 return err;
1465 static struct commit_queue_entry *
1466 alloc_commit_queue_entry(struct got_commit_object *commit,
1467 struct got_object_id *id)
1469 struct commit_queue_entry *entry;
1471 entry = calloc(1, sizeof(*entry));
1472 if (entry == NULL)
1473 return NULL;
1475 entry->id = id;
1476 entry->commit = commit;
1477 return entry;
1480 static void
1481 pop_commit(struct commit_queue *commits)
1483 struct commit_queue_entry *entry;
1485 entry = TAILQ_FIRST(&commits->head);
1486 TAILQ_REMOVE(&commits->head, entry, entry);
1487 got_object_commit_close(entry->commit);
1488 commits->ncommits--;
1489 /* Don't free entry->id! It is owned by the commit graph. */
1490 free(entry);
1493 static void
1494 free_commits(struct commit_queue *commits)
1496 while (!TAILQ_EMPTY(&commits->head))
1497 pop_commit(commits);
1500 static const struct got_error *
1501 match_commit(int *have_match, struct got_object_id *id,
1502 struct got_commit_object *commit, regex_t *regex)
1504 const struct got_error *err = NULL;
1505 regmatch_t regmatch;
1506 char *id_str = NULL, *logmsg = NULL;
1508 *have_match = 0;
1510 err = got_object_id_str(&id_str, id);
1511 if (err)
1512 return err;
1514 err = got_object_commit_get_logmsg(&logmsg, commit);
1515 if (err)
1516 goto done;
1518 if (regexec(regex, got_object_commit_get_author(commit), 1,
1519 &regmatch, 0) == 0 ||
1520 regexec(regex, got_object_commit_get_committer(commit), 1,
1521 &regmatch, 0) == 0 ||
1522 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1523 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1524 *have_match = 1;
1525 done:
1526 free(id_str);
1527 free(logmsg);
1528 return err;
1531 static const struct got_error *
1532 queue_commits(struct tog_log_thread_args *a)
1534 const struct got_error *err = NULL;
1537 * We keep all commits open throughout the lifetime of the log
1538 * view in order to avoid having to re-fetch commits from disk
1539 * while updating the display.
1541 do {
1542 struct got_object_id *id;
1543 struct got_commit_object *commit;
1544 struct commit_queue_entry *entry;
1545 int errcode;
1547 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1548 NULL, NULL);
1549 if (err || id == NULL)
1550 break;
1552 err = got_object_open_as_commit(&commit, a->repo, id);
1553 if (err)
1554 break;
1555 entry = alloc_commit_queue_entry(commit, id);
1556 if (entry == NULL) {
1557 err = got_error_from_errno("alloc_commit_queue_entry");
1558 break;
1561 errcode = pthread_mutex_lock(&tog_mutex);
1562 if (errcode) {
1563 err = got_error_set_errno(errcode,
1564 "pthread_mutex_lock");
1565 break;
1568 entry->idx = a->commits->ncommits;
1569 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1570 a->commits->ncommits++;
1572 if (*a->searching == TOG_SEARCH_FORWARD &&
1573 !*a->search_next_done) {
1574 int have_match;
1575 err = match_commit(&have_match, id, commit, a->regex);
1576 if (err)
1577 break;
1578 if (have_match)
1579 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1582 errcode = pthread_mutex_unlock(&tog_mutex);
1583 if (errcode && err == NULL)
1584 err = got_error_set_errno(errcode,
1585 "pthread_mutex_unlock");
1586 if (err)
1587 break;
1588 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1590 return err;
1593 static void
1594 select_commit(struct tog_log_view_state *s)
1596 struct commit_queue_entry *entry;
1597 int ncommits = 0;
1599 entry = s->first_displayed_entry;
1600 while (entry) {
1601 if (ncommits == s->selected) {
1602 s->selected_entry = entry;
1603 break;
1605 entry = TAILQ_NEXT(entry, entry);
1606 ncommits++;
1610 static const struct got_error *
1611 draw_commits(struct tog_view *view)
1613 const struct got_error *err = NULL;
1614 struct tog_log_view_state *s = &view->state.log;
1615 struct commit_queue_entry *entry = s->selected_entry;
1616 const int limit = view->nlines;
1617 int width;
1618 int ncommits, author_cols = 4;
1619 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1620 char *refs_str = NULL;
1621 wchar_t *wline;
1622 struct tog_color *tc;
1623 static const size_t date_display_cols = 12;
1625 if (s->selected_entry &&
1626 !(view->searching && view->search_next_done == 0)) {
1627 struct got_reflist_head *refs;
1628 err = got_object_id_str(&id_str, s->selected_entry->id);
1629 if (err)
1630 return err;
1631 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1632 s->selected_entry->id);
1633 if (refs) {
1634 err = build_refs_str(&refs_str, refs,
1635 s->selected_entry->id, s->repo);
1636 if (err)
1637 goto done;
1641 if (s->thread_args.commits_needed == 0)
1642 halfdelay(10); /* disable fast refresh */
1644 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1645 if (asprintf(&ncommits_str, " [%d/%d] %s",
1646 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1647 (view->searching && !view->search_next_done) ?
1648 "searching..." : "loading...") == -1) {
1649 err = got_error_from_errno("asprintf");
1650 goto done;
1652 } else {
1653 const char *search_str = NULL;
1655 if (view->searching) {
1656 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1657 search_str = "no more matches";
1658 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1659 search_str = "no matches found";
1660 else if (!view->search_next_done)
1661 search_str = "searching...";
1664 if (asprintf(&ncommits_str, " [%d/%d] %s",
1665 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1666 search_str ? search_str :
1667 (refs_str ? refs_str : "")) == -1) {
1668 err = got_error_from_errno("asprintf");
1669 goto done;
1673 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1674 if (asprintf(&header, "commit %s %s%s",
1675 id_str ? id_str : "........................................",
1676 s->in_repo_path, ncommits_str) == -1) {
1677 err = got_error_from_errno("asprintf");
1678 header = NULL;
1679 goto done;
1681 } else if (asprintf(&header, "commit %s%s",
1682 id_str ? id_str : "........................................",
1683 ncommits_str) == -1) {
1684 err = got_error_from_errno("asprintf");
1685 header = NULL;
1686 goto done;
1688 err = format_line(&wline, &width, header, view->ncols, 0);
1689 if (err)
1690 goto done;
1692 werase(view->window);
1694 if (view_needs_focus_indication(view))
1695 wstandout(view->window);
1696 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1697 if (tc)
1698 wattr_on(view->window,
1699 COLOR_PAIR(tc->colorpair), NULL);
1700 waddwstr(view->window, wline);
1701 if (tc)
1702 wattr_off(view->window,
1703 COLOR_PAIR(tc->colorpair), NULL);
1704 while (width < view->ncols) {
1705 waddch(view->window, ' ');
1706 width++;
1708 if (view_needs_focus_indication(view))
1709 wstandend(view->window);
1710 free(wline);
1711 if (limit <= 1)
1712 goto done;
1714 /* Grow author column size if necessary. */
1715 entry = s->first_displayed_entry;
1716 ncommits = 0;
1717 while (entry) {
1718 char *author;
1719 wchar_t *wauthor;
1720 int width;
1721 if (ncommits >= limit - 1)
1722 break;
1723 author = strdup(got_object_commit_get_author(entry->commit));
1724 if (author == NULL) {
1725 err = got_error_from_errno("strdup");
1726 goto done;
1728 err = format_author(&wauthor, &width, author, COLS,
1729 date_display_cols);
1730 if (author_cols < width)
1731 author_cols = width;
1732 free(wauthor);
1733 free(author);
1734 ncommits++;
1735 entry = TAILQ_NEXT(entry, entry);
1738 entry = s->first_displayed_entry;
1739 s->last_displayed_entry = s->first_displayed_entry;
1740 ncommits = 0;
1741 while (entry) {
1742 if (ncommits >= limit - 1)
1743 break;
1744 if (ncommits == s->selected)
1745 wstandout(view->window);
1746 err = draw_commit(view, entry->commit, entry->id,
1747 date_display_cols, author_cols);
1748 if (ncommits == s->selected)
1749 wstandend(view->window);
1750 if (err)
1751 goto done;
1752 ncommits++;
1753 s->last_displayed_entry = entry;
1754 entry = TAILQ_NEXT(entry, entry);
1757 view_vborder(view);
1758 done:
1759 free(id_str);
1760 free(refs_str);
1761 free(ncommits_str);
1762 free(header);
1763 return err;
1766 static void
1767 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1769 struct commit_queue_entry *entry;
1770 int nscrolled = 0;
1772 entry = TAILQ_FIRST(&s->commits.head);
1773 if (s->first_displayed_entry == entry)
1774 return;
1776 entry = s->first_displayed_entry;
1777 while (entry && nscrolled < maxscroll) {
1778 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1779 if (entry) {
1780 s->first_displayed_entry = entry;
1781 nscrolled++;
1786 static const struct got_error *
1787 trigger_log_thread(struct tog_view *view, int wait)
1789 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1790 int errcode;
1792 halfdelay(1); /* fast refresh while loading commits */
1794 while (ta->commits_needed > 0 || ta->load_all) {
1795 if (ta->log_complete)
1796 break;
1798 /* Wake the log thread. */
1799 errcode = pthread_cond_signal(&ta->need_commits);
1800 if (errcode)
1801 return got_error_set_errno(errcode,
1802 "pthread_cond_signal");
1805 * The mutex will be released while the view loop waits
1806 * in wgetch(), at which time the log thread will run.
1808 if (!wait)
1809 break;
1811 /* Display progress update in log view. */
1812 show_log_view(view);
1813 update_panels();
1814 doupdate();
1816 /* Wait right here while next commit is being loaded. */
1817 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1818 if (errcode)
1819 return got_error_set_errno(errcode,
1820 "pthread_cond_wait");
1822 /* Display progress update in log view. */
1823 show_log_view(view);
1824 update_panels();
1825 doupdate();
1828 return NULL;
1831 static const struct got_error *
1832 log_scroll_down(struct tog_view *view, int maxscroll)
1834 struct tog_log_view_state *s = &view->state.log;
1835 const struct got_error *err = NULL;
1836 struct commit_queue_entry *pentry;
1837 int nscrolled = 0, ncommits_needed;
1839 if (s->last_displayed_entry == NULL)
1840 return NULL;
1842 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1843 if (s->commits.ncommits < ncommits_needed &&
1844 !s->thread_args.log_complete) {
1846 * Ask the log thread for required amount of commits.
1848 s->thread_args.commits_needed += maxscroll;
1849 err = trigger_log_thread(view, 1);
1850 if (err)
1851 return err;
1854 do {
1855 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1856 if (pentry == NULL)
1857 break;
1859 s->last_displayed_entry = pentry;
1861 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1862 if (pentry == NULL)
1863 break;
1864 s->first_displayed_entry = pentry;
1865 } while (++nscrolled < maxscroll);
1867 return err;
1870 static const struct got_error *
1871 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1872 struct got_commit_object *commit, struct got_object_id *commit_id,
1873 struct tog_view *log_view, struct got_repository *repo)
1875 const struct got_error *err;
1876 struct got_object_qid *parent_id;
1877 struct tog_view *diff_view;
1879 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1880 if (diff_view == NULL)
1881 return got_error_from_errno("view_open");
1883 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1884 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1885 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1886 if (err == NULL)
1887 *new_view = diff_view;
1888 return err;
1891 static const struct got_error *
1892 tree_view_visit_subtree(struct tog_tree_view_state *s,
1893 struct got_tree_object *subtree)
1895 struct tog_parent_tree *parent;
1897 parent = calloc(1, sizeof(*parent));
1898 if (parent == NULL)
1899 return got_error_from_errno("calloc");
1901 parent->tree = s->tree;
1902 parent->first_displayed_entry = s->first_displayed_entry;
1903 parent->selected_entry = s->selected_entry;
1904 parent->selected = s->selected;
1905 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1906 s->tree = subtree;
1907 s->selected = 0;
1908 s->first_displayed_entry = NULL;
1909 return NULL;
1912 static const struct got_error *
1913 tree_view_walk_path(struct tog_tree_view_state *s,
1914 struct got_commit_object *commit, const char *path)
1916 const struct got_error *err = NULL;
1917 struct got_tree_object *tree = NULL;
1918 const char *p;
1919 char *slash, *subpath = NULL;
1921 /* Walk the path and open corresponding tree objects. */
1922 p = path;
1923 while (*p) {
1924 struct got_tree_entry *te;
1925 struct got_object_id *tree_id;
1926 char *te_name;
1928 while (p[0] == '/')
1929 p++;
1931 /* Ensure the correct subtree entry is selected. */
1932 slash = strchr(p, '/');
1933 if (slash == NULL)
1934 te_name = strdup(p);
1935 else
1936 te_name = strndup(p, slash - p);
1937 if (te_name == NULL) {
1938 err = got_error_from_errno("strndup");
1939 break;
1941 te = got_object_tree_find_entry(s->tree, te_name);
1942 if (te == NULL) {
1943 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1944 free(te_name);
1945 break;
1947 free(te_name);
1948 s->first_displayed_entry = s->selected_entry = te;
1950 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1951 break; /* jump to this file's entry */
1953 slash = strchr(p, '/');
1954 if (slash)
1955 subpath = strndup(path, slash - path);
1956 else
1957 subpath = strdup(path);
1958 if (subpath == NULL) {
1959 err = got_error_from_errno("strdup");
1960 break;
1963 err = got_object_id_by_path(&tree_id, s->repo, commit,
1964 subpath);
1965 if (err)
1966 break;
1968 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1969 free(tree_id);
1970 if (err)
1971 break;
1973 err = tree_view_visit_subtree(s, tree);
1974 if (err) {
1975 got_object_tree_close(tree);
1976 break;
1978 if (slash == NULL)
1979 break;
1980 free(subpath);
1981 subpath = NULL;
1982 p = slash;
1985 free(subpath);
1986 return err;
1989 static const struct got_error *
1990 browse_commit_tree(struct tog_view **new_view, int begin_x,
1991 struct commit_queue_entry *entry, const char *path,
1992 const char *head_ref_name, struct got_repository *repo)
1994 const struct got_error *err = NULL;
1995 struct tog_tree_view_state *s;
1996 struct tog_view *tree_view;
1998 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1999 if (tree_view == NULL)
2000 return got_error_from_errno("view_open");
2002 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2003 if (err)
2004 return err;
2005 s = &tree_view->state.tree;
2007 *new_view = tree_view;
2009 if (got_path_is_root_dir(path))
2010 return NULL;
2012 return tree_view_walk_path(s, entry->commit, path);
2015 static const struct got_error *
2016 block_signals_used_by_main_thread(void)
2018 sigset_t sigset;
2019 int errcode;
2021 if (sigemptyset(&sigset) == -1)
2022 return got_error_from_errno("sigemptyset");
2024 /* tog handles SIGWINCH and SIGCONT */
2025 if (sigaddset(&sigset, SIGWINCH) == -1)
2026 return got_error_from_errno("sigaddset");
2027 if (sigaddset(&sigset, SIGCONT) == -1)
2028 return got_error_from_errno("sigaddset");
2030 /* ncurses handles SIGTSTP */
2031 if (sigaddset(&sigset, SIGTSTP) == -1)
2032 return got_error_from_errno("sigaddset");
2034 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2035 if (errcode)
2036 return got_error_set_errno(errcode, "pthread_sigmask");
2038 return NULL;
2041 static void *
2042 log_thread(void *arg)
2044 const struct got_error *err = NULL;
2045 int errcode = 0;
2046 struct tog_log_thread_args *a = arg;
2047 int done = 0;
2049 err = block_signals_used_by_main_thread();
2050 if (err)
2051 return (void *)err;
2053 while (!done && !err && !tog_sigpipe_received) {
2054 err = queue_commits(a);
2055 if (err) {
2056 if (err->code != GOT_ERR_ITER_COMPLETED)
2057 return (void *)err;
2058 err = NULL;
2059 done = 1;
2060 } else if (a->commits_needed > 0 && !a->load_all)
2061 a->commits_needed--;
2063 errcode = pthread_mutex_lock(&tog_mutex);
2064 if (errcode) {
2065 err = got_error_set_errno(errcode,
2066 "pthread_mutex_lock");
2067 break;
2068 } else if (*a->quit)
2069 done = 1;
2070 else if (*a->first_displayed_entry == NULL) {
2071 *a->first_displayed_entry =
2072 TAILQ_FIRST(&a->commits->head);
2073 *a->selected_entry = *a->first_displayed_entry;
2076 errcode = pthread_cond_signal(&a->commit_loaded);
2077 if (errcode) {
2078 err = got_error_set_errno(errcode,
2079 "pthread_cond_signal");
2080 pthread_mutex_unlock(&tog_mutex);
2081 break;
2084 if (done)
2085 a->commits_needed = 0;
2086 else {
2087 if (a->commits_needed == 0 && !a->load_all) {
2088 errcode = pthread_cond_wait(&a->need_commits,
2089 &tog_mutex);
2090 if (errcode)
2091 err = got_error_set_errno(errcode,
2092 "pthread_cond_wait");
2093 if (*a->quit)
2094 done = 1;
2098 errcode = pthread_mutex_unlock(&tog_mutex);
2099 if (errcode && err == NULL)
2100 err = got_error_set_errno(errcode,
2101 "pthread_mutex_unlock");
2103 a->log_complete = 1;
2104 return (void *)err;
2107 static const struct got_error *
2108 stop_log_thread(struct tog_log_view_state *s)
2110 const struct got_error *err = NULL;
2111 int errcode;
2113 if (s->thread) {
2114 s->quit = 1;
2115 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2116 if (errcode)
2117 return got_error_set_errno(errcode,
2118 "pthread_cond_signal");
2119 errcode = pthread_mutex_unlock(&tog_mutex);
2120 if (errcode)
2121 return got_error_set_errno(errcode,
2122 "pthread_mutex_unlock");
2123 errcode = pthread_join(s->thread, (void **)&err);
2124 if (errcode)
2125 return got_error_set_errno(errcode, "pthread_join");
2126 errcode = pthread_mutex_lock(&tog_mutex);
2127 if (errcode)
2128 return got_error_set_errno(errcode,
2129 "pthread_mutex_lock");
2130 s->thread = NULL;
2133 if (s->thread_args.repo) {
2134 err = got_repo_close(s->thread_args.repo);
2135 s->thread_args.repo = NULL;
2138 if (s->thread_args.graph) {
2139 got_commit_graph_close(s->thread_args.graph);
2140 s->thread_args.graph = NULL;
2143 return err;
2146 static const struct got_error *
2147 close_log_view(struct tog_view *view)
2149 const struct got_error *err = NULL;
2150 struct tog_log_view_state *s = &view->state.log;
2151 int errcode;
2153 err = stop_log_thread(s);
2155 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2156 if (errcode && err == NULL)
2157 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2159 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2160 if (errcode && err == NULL)
2161 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2163 free_commits(&s->commits);
2164 free(s->in_repo_path);
2165 s->in_repo_path = NULL;
2166 free(s->start_id);
2167 s->start_id = NULL;
2168 free(s->head_ref_name);
2169 s->head_ref_name = NULL;
2170 return err;
2173 static const struct got_error *
2174 search_start_log_view(struct tog_view *view)
2176 struct tog_log_view_state *s = &view->state.log;
2178 s->matched_entry = NULL;
2179 s->search_entry = NULL;
2180 return NULL;
2183 static const struct got_error *
2184 search_next_log_view(struct tog_view *view)
2186 const struct got_error *err = NULL;
2187 struct tog_log_view_state *s = &view->state.log;
2188 struct commit_queue_entry *entry;
2190 /* Display progress update in log view. */
2191 show_log_view(view);
2192 update_panels();
2193 doupdate();
2195 if (s->search_entry) {
2196 int errcode, ch;
2197 errcode = pthread_mutex_unlock(&tog_mutex);
2198 if (errcode)
2199 return got_error_set_errno(errcode,
2200 "pthread_mutex_unlock");
2201 ch = wgetch(view->window);
2202 errcode = pthread_mutex_lock(&tog_mutex);
2203 if (errcode)
2204 return got_error_set_errno(errcode,
2205 "pthread_mutex_lock");
2206 if (ch == KEY_BACKSPACE) {
2207 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2208 return NULL;
2210 if (view->searching == TOG_SEARCH_FORWARD)
2211 entry = TAILQ_NEXT(s->search_entry, entry);
2212 else
2213 entry = TAILQ_PREV(s->search_entry,
2214 commit_queue_head, entry);
2215 } else if (s->matched_entry) {
2216 if (view->searching == TOG_SEARCH_FORWARD)
2217 entry = TAILQ_NEXT(s->matched_entry, entry);
2218 else
2219 entry = TAILQ_PREV(s->matched_entry,
2220 commit_queue_head, entry);
2221 } else {
2222 entry = s->selected_entry;
2225 while (1) {
2226 int have_match = 0;
2228 if (entry == NULL) {
2229 if (s->thread_args.log_complete ||
2230 view->searching == TOG_SEARCH_BACKWARD) {
2231 view->search_next_done =
2232 (s->matched_entry == NULL ?
2233 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2234 s->search_entry = NULL;
2235 return NULL;
2238 * Poke the log thread for more commits and return,
2239 * allowing the main loop to make progress. Search
2240 * will resume at s->search_entry once we come back.
2242 s->thread_args.commits_needed++;
2243 return trigger_log_thread(view, 0);
2246 err = match_commit(&have_match, entry->id, entry->commit,
2247 &view->regex);
2248 if (err)
2249 break;
2250 if (have_match) {
2251 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2252 s->matched_entry = entry;
2253 break;
2256 s->search_entry = entry;
2257 if (view->searching == TOG_SEARCH_FORWARD)
2258 entry = TAILQ_NEXT(entry, entry);
2259 else
2260 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2263 if (s->matched_entry) {
2264 int cur = s->selected_entry->idx;
2265 while (cur < s->matched_entry->idx) {
2266 err = input_log_view(NULL, view, KEY_DOWN);
2267 if (err)
2268 return err;
2269 cur++;
2271 while (cur > s->matched_entry->idx) {
2272 err = input_log_view(NULL, view, KEY_UP);
2273 if (err)
2274 return err;
2275 cur--;
2279 s->search_entry = NULL;
2281 return NULL;
2284 static const struct got_error *
2285 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2286 struct got_repository *repo, const char *head_ref_name,
2287 const char *in_repo_path, int log_branches)
2289 const struct got_error *err = NULL;
2290 struct tog_log_view_state *s = &view->state.log;
2291 struct got_repository *thread_repo = NULL;
2292 struct got_commit_graph *thread_graph = NULL;
2293 int errcode;
2295 if (in_repo_path != s->in_repo_path) {
2296 free(s->in_repo_path);
2297 s->in_repo_path = strdup(in_repo_path);
2298 if (s->in_repo_path == NULL)
2299 return got_error_from_errno("strdup");
2302 /* The commit queue only contains commits being displayed. */
2303 TAILQ_INIT(&s->commits.head);
2304 s->commits.ncommits = 0;
2306 s->repo = repo;
2307 if (head_ref_name) {
2308 s->head_ref_name = strdup(head_ref_name);
2309 if (s->head_ref_name == NULL) {
2310 err = got_error_from_errno("strdup");
2311 goto done;
2314 s->start_id = got_object_id_dup(start_id);
2315 if (s->start_id == NULL) {
2316 err = got_error_from_errno("got_object_id_dup");
2317 goto done;
2319 s->log_branches = log_branches;
2321 STAILQ_INIT(&s->colors);
2322 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2323 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2324 get_color_value("TOG_COLOR_COMMIT"));
2325 if (err)
2326 goto done;
2327 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2328 get_color_value("TOG_COLOR_AUTHOR"));
2329 if (err) {
2330 free_colors(&s->colors);
2331 goto done;
2333 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2334 get_color_value("TOG_COLOR_DATE"));
2335 if (err) {
2336 free_colors(&s->colors);
2337 goto done;
2341 view->show = show_log_view;
2342 view->input = input_log_view;
2343 view->close = close_log_view;
2344 view->search_start = search_start_log_view;
2345 view->search_next = search_next_log_view;
2347 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2348 if (err)
2349 goto done;
2350 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2351 !s->log_branches);
2352 if (err)
2353 goto done;
2354 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2355 s->repo, NULL, NULL);
2356 if (err)
2357 goto done;
2359 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2360 if (errcode) {
2361 err = got_error_set_errno(errcode, "pthread_cond_init");
2362 goto done;
2364 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2365 if (errcode) {
2366 err = got_error_set_errno(errcode, "pthread_cond_init");
2367 goto done;
2370 s->thread_args.commits_needed = view->nlines;
2371 s->thread_args.graph = thread_graph;
2372 s->thread_args.commits = &s->commits;
2373 s->thread_args.in_repo_path = s->in_repo_path;
2374 s->thread_args.start_id = s->start_id;
2375 s->thread_args.repo = thread_repo;
2376 s->thread_args.log_complete = 0;
2377 s->thread_args.quit = &s->quit;
2378 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2379 s->thread_args.selected_entry = &s->selected_entry;
2380 s->thread_args.searching = &view->searching;
2381 s->thread_args.search_next_done = &view->search_next_done;
2382 s->thread_args.regex = &view->regex;
2383 done:
2384 if (err)
2385 close_log_view(view);
2386 return err;
2389 static const struct got_error *
2390 show_log_view(struct tog_view *view)
2392 const struct got_error *err;
2393 struct tog_log_view_state *s = &view->state.log;
2395 if (s->thread == NULL) {
2396 int errcode = pthread_create(&s->thread, NULL, log_thread,
2397 &s->thread_args);
2398 if (errcode)
2399 return got_error_set_errno(errcode, "pthread_create");
2400 if (s->thread_args.commits_needed > 0) {
2401 err = trigger_log_thread(view, 1);
2402 if (err)
2403 return err;
2407 return draw_commits(view);
2410 static const struct got_error *
2411 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2413 const struct got_error *err = NULL;
2414 struct tog_log_view_state *s = &view->state.log;
2415 struct tog_view *diff_view = NULL, *tree_view = NULL;
2416 struct tog_view *ref_view = NULL;
2417 struct commit_queue_entry *entry;
2418 int begin_x = 0, n;
2420 if (s->thread_args.load_all) {
2421 if (ch == KEY_BACKSPACE)
2422 s->thread_args.load_all = 0;
2423 else if (s->thread_args.log_complete) {
2424 s->thread_args.load_all = 0;
2425 log_scroll_down(view, s->commits.ncommits);
2426 s->selected = MIN(view->nlines - 2,
2427 s->commits.ncommits - 1);
2428 select_commit(s);
2430 return NULL;
2433 switch (ch) {
2434 case 'q':
2435 s->quit = 1;
2436 break;
2437 case 'k':
2438 case KEY_UP:
2439 case '<':
2440 case ',':
2441 case CTRL('p'):
2442 if (s->first_displayed_entry == NULL)
2443 break;
2444 if (s->selected > 0)
2445 s->selected--;
2446 else
2447 log_scroll_up(s, 1);
2448 select_commit(s);
2449 break;
2450 case 'g':
2451 case KEY_HOME:
2452 s->selected = 0;
2453 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2454 select_commit(s);
2455 break;
2456 case KEY_PPAGE:
2457 case CTRL('b'):
2458 if (s->first_displayed_entry == NULL)
2459 break;
2460 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2461 s->selected = 0;
2462 else
2463 log_scroll_up(s, view->nlines - 1);
2464 select_commit(s);
2465 break;
2466 case 'j':
2467 case KEY_DOWN:
2468 case '>':
2469 case '.':
2470 case CTRL('n'):
2471 if (s->first_displayed_entry == NULL)
2472 break;
2473 if (s->selected < MIN(view->nlines - 2,
2474 s->commits.ncommits - 1))
2475 s->selected++;
2476 else {
2477 err = log_scroll_down(view, 1);
2478 if (err)
2479 break;
2481 select_commit(s);
2482 break;
2483 case 'G':
2484 case KEY_END: {
2485 /* We don't know yet how many commits, so we're forced to
2486 * traverse them all. */
2487 if (!s->thread_args.log_complete) {
2488 s->thread_args.load_all = 1;
2489 return trigger_log_thread(view, 0);
2492 s->selected = 0;
2493 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2494 for (n = 0; n < view->nlines - 1; n++) {
2495 if (entry == NULL)
2496 break;
2497 s->first_displayed_entry = entry;
2498 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2500 if (n > 0)
2501 s->selected = n - 1;
2502 select_commit(s);
2503 break;
2505 case KEY_NPAGE:
2506 case CTRL('f'): {
2507 struct commit_queue_entry *first;
2508 first = s->first_displayed_entry;
2509 if (first == NULL)
2510 break;
2511 err = log_scroll_down(view, view->nlines - 1);
2512 if (err)
2513 break;
2514 if (first == s->first_displayed_entry &&
2515 s->selected < MIN(view->nlines - 2,
2516 s->commits.ncommits - 1)) {
2517 /* can't scroll further down */
2518 s->selected = MIN(view->nlines - 2,
2519 s->commits.ncommits - 1);
2521 select_commit(s);
2522 break;
2524 case KEY_RESIZE:
2525 if (s->selected > view->nlines - 2)
2526 s->selected = view->nlines - 2;
2527 if (s->selected > s->commits.ncommits - 1)
2528 s->selected = s->commits.ncommits - 1;
2529 select_commit(s);
2530 if (s->commits.ncommits < view->nlines - 1 &&
2531 !s->thread_args.log_complete) {
2532 s->thread_args.commits_needed += (view->nlines - 1) -
2533 s->commits.ncommits;
2534 err = trigger_log_thread(view, 1);
2536 break;
2537 case KEY_ENTER:
2538 case ' ':
2539 case '\r':
2540 if (s->selected_entry == NULL)
2541 break;
2542 if (view_is_parent_view(view))
2543 begin_x = view_split_begin_x(view->begin_x);
2544 err = open_diff_view_for_commit(&diff_view, begin_x,
2545 s->selected_entry->commit, s->selected_entry->id,
2546 view, s->repo);
2547 if (err)
2548 break;
2549 view->focussed = 0;
2550 diff_view->focussed = 1;
2551 if (view_is_parent_view(view)) {
2552 err = view_close_child(view);
2553 if (err)
2554 return err;
2555 view_set_child(view, diff_view);
2556 view->focus_child = 1;
2557 } else
2558 *new_view = diff_view;
2559 break;
2560 case 't':
2561 if (s->selected_entry == NULL)
2562 break;
2563 if (view_is_parent_view(view))
2564 begin_x = view_split_begin_x(view->begin_x);
2565 err = browse_commit_tree(&tree_view, begin_x,
2566 s->selected_entry, s->in_repo_path, s->head_ref_name,
2567 s->repo);
2568 if (err)
2569 break;
2570 view->focussed = 0;
2571 tree_view->focussed = 1;
2572 if (view_is_parent_view(view)) {
2573 err = view_close_child(view);
2574 if (err)
2575 return err;
2576 view_set_child(view, tree_view);
2577 view->focus_child = 1;
2578 } else
2579 *new_view = tree_view;
2580 break;
2581 case KEY_BACKSPACE:
2582 case CTRL('l'):
2583 case 'B':
2584 if (ch == KEY_BACKSPACE &&
2585 got_path_is_root_dir(s->in_repo_path))
2586 break;
2587 err = stop_log_thread(s);
2588 if (err)
2589 return err;
2590 if (ch == KEY_BACKSPACE) {
2591 char *parent_path;
2592 err = got_path_dirname(&parent_path, s->in_repo_path);
2593 if (err)
2594 return err;
2595 free(s->in_repo_path);
2596 s->in_repo_path = parent_path;
2597 s->thread_args.in_repo_path = s->in_repo_path;
2598 } else if (ch == CTRL('l')) {
2599 struct got_object_id *start_id;
2600 err = got_repo_match_object_id(&start_id, NULL,
2601 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2602 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2603 if (err)
2604 return err;
2605 free(s->start_id);
2606 s->start_id = start_id;
2607 s->thread_args.start_id = s->start_id;
2608 } else /* 'B' */
2609 s->log_branches = !s->log_branches;
2611 err = got_repo_open(&s->thread_args.repo,
2612 got_repo_get_path(s->repo), NULL);
2613 if (err)
2614 return err;
2615 tog_free_refs();
2616 err = tog_load_refs(s->repo, 0);
2617 if (err)
2618 return err;
2619 err = got_commit_graph_open(&s->thread_args.graph,
2620 s->in_repo_path, !s->log_branches);
2621 if (err)
2622 return err;
2623 err = got_commit_graph_iter_start(s->thread_args.graph,
2624 s->start_id, s->repo, NULL, NULL);
2625 if (err)
2626 return err;
2627 free_commits(&s->commits);
2628 s->first_displayed_entry = NULL;
2629 s->last_displayed_entry = NULL;
2630 s->selected_entry = NULL;
2631 s->selected = 0;
2632 s->thread_args.log_complete = 0;
2633 s->quit = 0;
2634 s->thread_args.commits_needed = view->nlines;
2635 break;
2636 case 'r':
2637 if (view_is_parent_view(view))
2638 begin_x = view_split_begin_x(view->begin_x);
2639 ref_view = view_open(view->nlines, view->ncols,
2640 view->begin_y, begin_x, TOG_VIEW_REF);
2641 if (ref_view == NULL)
2642 return got_error_from_errno("view_open");
2643 err = open_ref_view(ref_view, s->repo);
2644 if (err) {
2645 view_close(ref_view);
2646 return err;
2648 view->focussed = 0;
2649 ref_view->focussed = 1;
2650 if (view_is_parent_view(view)) {
2651 err = view_close_child(view);
2652 if (err)
2653 return err;
2654 view_set_child(view, ref_view);
2655 view->focus_child = 1;
2656 } else
2657 *new_view = ref_view;
2658 break;
2659 default:
2660 break;
2663 return err;
2666 static const struct got_error *
2667 apply_unveil(const char *repo_path, const char *worktree_path)
2669 const struct got_error *error;
2671 #ifdef PROFILE
2672 if (unveil("gmon.out", "rwc") != 0)
2673 return got_error_from_errno2("unveil", "gmon.out");
2674 #endif
2675 if (repo_path && unveil(repo_path, "r") != 0)
2676 return got_error_from_errno2("unveil", repo_path);
2678 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2679 return got_error_from_errno2("unveil", worktree_path);
2681 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2682 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2684 error = got_privsep_unveil_exec_helpers();
2685 if (error != NULL)
2686 return error;
2688 if (unveil(NULL, NULL) != 0)
2689 return got_error_from_errno("unveil");
2691 return NULL;
2694 static void
2695 init_curses(void)
2697 initscr();
2698 cbreak();
2699 halfdelay(1); /* Do fast refresh while initial view is loading. */
2700 noecho();
2701 nonl();
2702 intrflush(stdscr, FALSE);
2703 keypad(stdscr, TRUE);
2704 curs_set(0);
2705 if (getenv("TOG_COLORS") != NULL) {
2706 start_color();
2707 use_default_colors();
2709 signal(SIGWINCH, tog_sigwinch);
2710 signal(SIGPIPE, tog_sigpipe);
2711 signal(SIGCONT, tog_sigcont);
2714 static const struct got_error *
2715 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2716 struct got_repository *repo, struct got_worktree *worktree)
2718 const struct got_error *err = NULL;
2720 if (argc == 0) {
2721 *in_repo_path = strdup("/");
2722 if (*in_repo_path == NULL)
2723 return got_error_from_errno("strdup");
2724 return NULL;
2727 if (worktree) {
2728 const char *prefix = got_worktree_get_path_prefix(worktree);
2729 char *p;
2731 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2732 if (err)
2733 return err;
2734 if (asprintf(in_repo_path, "%s%s%s", prefix,
2735 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2736 p) == -1) {
2737 err = got_error_from_errno("asprintf");
2738 *in_repo_path = NULL;
2740 free(p);
2741 } else
2742 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2744 return err;
2747 static const struct got_error *
2748 cmd_log(int argc, char *argv[])
2750 const struct got_error *error;
2751 struct got_repository *repo = NULL;
2752 struct got_worktree *worktree = NULL;
2753 struct got_object_id *start_id = NULL;
2754 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2755 char *start_commit = NULL, *label = NULL;
2756 struct got_reference *ref = NULL;
2757 const char *head_ref_name = NULL;
2758 int ch, log_branches = 0;
2759 struct tog_view *view;
2761 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2762 switch (ch) {
2763 case 'b':
2764 log_branches = 1;
2765 break;
2766 case 'c':
2767 start_commit = optarg;
2768 break;
2769 case 'r':
2770 repo_path = realpath(optarg, NULL);
2771 if (repo_path == NULL)
2772 return got_error_from_errno2("realpath",
2773 optarg);
2774 break;
2775 default:
2776 usage_log();
2777 /* NOTREACHED */
2781 argc -= optind;
2782 argv += optind;
2784 if (argc > 1)
2785 usage_log();
2787 if (repo_path == NULL) {
2788 cwd = getcwd(NULL, 0);
2789 if (cwd == NULL)
2790 return got_error_from_errno("getcwd");
2791 error = got_worktree_open(&worktree, cwd);
2792 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2793 goto done;
2794 if (worktree)
2795 repo_path =
2796 strdup(got_worktree_get_repo_path(worktree));
2797 else
2798 repo_path = strdup(cwd);
2799 if (repo_path == NULL) {
2800 error = got_error_from_errno("strdup");
2801 goto done;
2805 error = got_repo_open(&repo, repo_path, NULL);
2806 if (error != NULL)
2807 goto done;
2809 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2810 repo, worktree);
2811 if (error)
2812 goto done;
2814 init_curses();
2816 error = apply_unveil(got_repo_get_path(repo),
2817 worktree ? got_worktree_get_root_path(worktree) : NULL);
2818 if (error)
2819 goto done;
2821 /* already loaded by tog_log_with_path()? */
2822 if (TAILQ_EMPTY(&tog_refs)) {
2823 error = tog_load_refs(repo, 0);
2824 if (error)
2825 goto done;
2828 if (start_commit == NULL) {
2829 error = got_repo_match_object_id(&start_id, &label,
2830 worktree ? got_worktree_get_head_ref_name(worktree) :
2831 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2832 if (error)
2833 goto done;
2834 head_ref_name = label;
2835 } else {
2836 error = got_ref_open(&ref, repo, start_commit, 0);
2837 if (error == NULL)
2838 head_ref_name = got_ref_get_name(ref);
2839 else if (error->code != GOT_ERR_NOT_REF)
2840 goto done;
2841 error = got_repo_match_object_id(&start_id, NULL,
2842 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2843 if (error)
2844 goto done;
2847 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2848 if (view == NULL) {
2849 error = got_error_from_errno("view_open");
2850 goto done;
2852 error = open_log_view(view, start_id, repo, head_ref_name,
2853 in_repo_path, log_branches);
2854 if (error)
2855 goto done;
2856 if (worktree) {
2857 /* Release work tree lock. */
2858 got_worktree_close(worktree);
2859 worktree = NULL;
2861 error = view_loop(view);
2862 done:
2863 free(in_repo_path);
2864 free(repo_path);
2865 free(cwd);
2866 free(start_id);
2867 free(label);
2868 if (ref)
2869 got_ref_close(ref);
2870 if (repo) {
2871 const struct got_error *close_err = got_repo_close(repo);
2872 if (error == NULL)
2873 error = close_err;
2875 if (worktree)
2876 got_worktree_close(worktree);
2877 tog_free_refs();
2878 return error;
2881 __dead static void
2882 usage_diff(void)
2884 endwin();
2885 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2886 "[-w] object1 object2\n", getprogname());
2887 exit(1);
2890 static int
2891 match_line(const char *line, regex_t *regex, size_t nmatch,
2892 regmatch_t *regmatch)
2894 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2897 struct tog_color *
2898 match_color(struct tog_colors *colors, const char *line)
2900 struct tog_color *tc = NULL;
2902 STAILQ_FOREACH(tc, colors, entry) {
2903 if (match_line(line, &tc->regex, 0, NULL))
2904 return tc;
2907 return NULL;
2910 static const struct got_error *
2911 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2912 WINDOW *window, regmatch_t *regmatch)
2914 const struct got_error *err = NULL;
2915 wchar_t *wline;
2916 int width;
2917 char *s;
2919 *wtotal = 0;
2921 s = strndup(line, regmatch->rm_so);
2922 if (s == NULL)
2923 return got_error_from_errno("strndup");
2925 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2926 if (err) {
2927 free(s);
2928 return err;
2930 waddwstr(window, wline);
2931 free(wline);
2932 free(s);
2933 wlimit -= width;
2934 *wtotal += width;
2936 if (wlimit > 0) {
2937 s = strndup(line + regmatch->rm_so,
2938 regmatch->rm_eo - regmatch->rm_so);
2939 if (s == NULL) {
2940 err = got_error_from_errno("strndup");
2941 free(s);
2942 return err;
2944 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2945 if (err) {
2946 free(s);
2947 return err;
2949 wattr_on(window, A_STANDOUT, NULL);
2950 waddwstr(window, wline);
2951 wattr_off(window, A_STANDOUT, NULL);
2952 free(wline);
2953 free(s);
2954 wlimit -= width;
2955 *wtotal += width;
2958 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2959 err = format_line(&wline, &width,
2960 line + regmatch->rm_eo, wlimit, col_tab_align);
2961 if (err)
2962 return err;
2963 waddwstr(window, wline);
2964 free(wline);
2965 *wtotal += width;
2968 return NULL;
2971 static const struct got_error *
2972 draw_file(struct tog_view *view, const char *header)
2974 struct tog_diff_view_state *s = &view->state.diff;
2975 regmatch_t *regmatch = &view->regmatch;
2976 const struct got_error *err;
2977 int nprinted = 0;
2978 char *line;
2979 size_t linesize = 0;
2980 ssize_t linelen;
2981 struct tog_color *tc;
2982 wchar_t *wline;
2983 int width;
2984 int max_lines = view->nlines;
2985 int nlines = s->nlines;
2986 off_t line_offset;
2988 line_offset = s->line_offsets[s->first_displayed_line - 1];
2989 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2990 return got_error_from_errno("fseek");
2992 werase(view->window);
2994 if (header) {
2995 if (asprintf(&line, "[%d/%d] %s",
2996 s->first_displayed_line - 1 + s->selected_line, nlines,
2997 header) == -1)
2998 return got_error_from_errno("asprintf");
2999 err = format_line(&wline, &width, line, view->ncols, 0);
3000 free(line);
3001 if (err)
3002 return err;
3004 if (view_needs_focus_indication(view))
3005 wstandout(view->window);
3006 waddwstr(view->window, wline);
3007 free(wline);
3008 wline = NULL;
3009 if (view_needs_focus_indication(view))
3010 wstandend(view->window);
3011 if (width <= view->ncols - 1)
3012 waddch(view->window, '\n');
3014 if (max_lines <= 1)
3015 return NULL;
3016 max_lines--;
3019 s->eof = 0;
3020 line = NULL;
3021 while (max_lines > 0 && nprinted < max_lines) {
3022 linelen = getline(&line, &linesize, s->f);
3023 if (linelen == -1) {
3024 if (feof(s->f)) {
3025 s->eof = 1;
3026 break;
3028 free(line);
3029 return got_ferror(s->f, GOT_ERR_IO);
3032 tc = match_color(&s->colors, line);
3033 if (tc)
3034 wattr_on(view->window,
3035 COLOR_PAIR(tc->colorpair), NULL);
3036 if (s->first_displayed_line + nprinted == s->matched_line &&
3037 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3038 err = add_matched_line(&width, line, view->ncols, 0,
3039 view->window, regmatch);
3040 if (err) {
3041 free(line);
3042 return err;
3044 } else {
3045 err = format_line(&wline, &width, line, view->ncols, 0);
3046 if (err) {
3047 free(line);
3048 return err;
3050 waddwstr(view->window, wline);
3051 free(wline);
3052 wline = NULL;
3054 if (tc)
3055 wattr_off(view->window,
3056 COLOR_PAIR(tc->colorpair), NULL);
3057 if (width <= view->ncols - 1)
3058 waddch(view->window, '\n');
3059 nprinted++;
3061 free(line);
3062 if (nprinted >= 1)
3063 s->last_displayed_line = s->first_displayed_line +
3064 (nprinted - 1);
3065 else
3066 s->last_displayed_line = s->first_displayed_line;
3068 view_vborder(view);
3070 if (s->eof) {
3071 while (nprinted < view->nlines) {
3072 waddch(view->window, '\n');
3073 nprinted++;
3076 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3077 if (err) {
3078 return err;
3081 wstandout(view->window);
3082 waddwstr(view->window, wline);
3083 free(wline);
3084 wline = NULL;
3085 wstandend(view->window);
3088 return NULL;
3091 static char *
3092 get_datestr(time_t *time, char *datebuf)
3094 struct tm mytm, *tm;
3095 char *p, *s;
3097 tm = gmtime_r(time, &mytm);
3098 if (tm == NULL)
3099 return NULL;
3100 s = asctime_r(tm, datebuf);
3101 if (s == NULL)
3102 return NULL;
3103 p = strchr(s, '\n');
3104 if (p)
3105 *p = '\0';
3106 return s;
3109 static const struct got_error *
3110 get_changed_paths(struct got_pathlist_head *paths,
3111 struct got_commit_object *commit, struct got_repository *repo)
3113 const struct got_error *err = NULL;
3114 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3115 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3116 struct got_object_qid *qid;
3118 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3119 if (qid != NULL) {
3120 struct got_commit_object *pcommit;
3121 err = got_object_open_as_commit(&pcommit, repo,
3122 &qid->id);
3123 if (err)
3124 return err;
3126 tree_id1 = got_object_id_dup(
3127 got_object_commit_get_tree_id(pcommit));
3128 if (tree_id1 == NULL) {
3129 got_object_commit_close(pcommit);
3130 return got_error_from_errno("got_object_id_dup");
3132 got_object_commit_close(pcommit);
3136 if (tree_id1) {
3137 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3138 if (err)
3139 goto done;
3142 tree_id2 = got_object_commit_get_tree_id(commit);
3143 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3144 if (err)
3145 goto done;
3147 err = got_diff_tree(tree1, tree2, "", "", repo,
3148 got_diff_tree_collect_changed_paths, paths, 0);
3149 done:
3150 if (tree1)
3151 got_object_tree_close(tree1);
3152 if (tree2)
3153 got_object_tree_close(tree2);
3154 free(tree_id1);
3155 return err;
3158 static const struct got_error *
3159 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3161 off_t *p;
3163 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3164 if (p == NULL)
3165 return got_error_from_errno("reallocarray");
3166 *line_offsets = p;
3167 (*line_offsets)[*nlines] = off;
3168 (*nlines)++;
3169 return NULL;
3172 static const struct got_error *
3173 write_commit_info(off_t **line_offsets, size_t *nlines,
3174 struct got_object_id *commit_id, struct got_reflist_head *refs,
3175 struct got_repository *repo, FILE *outfile)
3177 const struct got_error *err = NULL;
3178 char datebuf[26], *datestr;
3179 struct got_commit_object *commit;
3180 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3181 time_t committer_time;
3182 const char *author, *committer;
3183 char *refs_str = NULL;
3184 struct got_pathlist_head changed_paths;
3185 struct got_pathlist_entry *pe;
3186 off_t outoff = 0;
3187 int n;
3189 TAILQ_INIT(&changed_paths);
3191 if (refs) {
3192 err = build_refs_str(&refs_str, refs, commit_id, repo);
3193 if (err)
3194 return err;
3197 err = got_object_open_as_commit(&commit, repo, commit_id);
3198 if (err)
3199 return err;
3201 err = got_object_id_str(&id_str, commit_id);
3202 if (err) {
3203 err = got_error_from_errno("got_object_id_str");
3204 goto done;
3207 err = add_line_offset(line_offsets, nlines, 0);
3208 if (err)
3209 goto done;
3211 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3212 refs_str ? refs_str : "", refs_str ? ")" : "");
3213 if (n < 0) {
3214 err = got_error_from_errno("fprintf");
3215 goto done;
3217 outoff += n;
3218 err = add_line_offset(line_offsets, nlines, outoff);
3219 if (err)
3220 goto done;
3222 n = fprintf(outfile, "from: %s\n",
3223 got_object_commit_get_author(commit));
3224 if (n < 0) {
3225 err = got_error_from_errno("fprintf");
3226 goto done;
3228 outoff += n;
3229 err = add_line_offset(line_offsets, nlines, outoff);
3230 if (err)
3231 goto done;
3233 committer_time = got_object_commit_get_committer_time(commit);
3234 datestr = get_datestr(&committer_time, datebuf);
3235 if (datestr) {
3236 n = fprintf(outfile, "date: %s UTC\n", datestr);
3237 if (n < 0) {
3238 err = got_error_from_errno("fprintf");
3239 goto done;
3241 outoff += n;
3242 err = add_line_offset(line_offsets, nlines, outoff);
3243 if (err)
3244 goto done;
3246 author = got_object_commit_get_author(commit);
3247 committer = got_object_commit_get_committer(commit);
3248 if (strcmp(author, committer) != 0) {
3249 n = fprintf(outfile, "via: %s\n", committer);
3250 if (n < 0) {
3251 err = got_error_from_errno("fprintf");
3252 goto done;
3254 outoff += n;
3255 err = add_line_offset(line_offsets, nlines, outoff);
3256 if (err)
3257 goto done;
3259 if (got_object_commit_get_nparents(commit) > 1) {
3260 const struct got_object_id_queue *parent_ids;
3261 struct got_object_qid *qid;
3262 int pn = 1;
3263 parent_ids = got_object_commit_get_parent_ids(commit);
3264 STAILQ_FOREACH(qid, parent_ids, entry) {
3265 err = got_object_id_str(&id_str, &qid->id);
3266 if (err)
3267 goto done;
3268 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3269 if (n < 0) {
3270 err = got_error_from_errno("fprintf");
3271 goto done;
3273 outoff += n;
3274 err = add_line_offset(line_offsets, nlines, outoff);
3275 if (err)
3276 goto done;
3277 free(id_str);
3278 id_str = NULL;
3282 err = got_object_commit_get_logmsg(&logmsg, commit);
3283 if (err)
3284 goto done;
3285 s = logmsg;
3286 while ((line = strsep(&s, "\n")) != NULL) {
3287 n = fprintf(outfile, "%s\n", line);
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;
3298 err = get_changed_paths(&changed_paths, commit, repo);
3299 if (err)
3300 goto done;
3301 TAILQ_FOREACH(pe, &changed_paths, entry) {
3302 struct got_diff_changed_path *cp = pe->data;
3303 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3304 if (n < 0) {
3305 err = got_error_from_errno("fprintf");
3306 goto done;
3308 outoff += n;
3309 err = add_line_offset(line_offsets, nlines, outoff);
3310 if (err)
3311 goto done;
3312 free((char *)pe->path);
3313 free(pe->data);
3316 fputc('\n', outfile);
3317 outoff++;
3318 err = add_line_offset(line_offsets, nlines, outoff);
3319 done:
3320 got_pathlist_free(&changed_paths);
3321 free(id_str);
3322 free(logmsg);
3323 free(refs_str);
3324 got_object_commit_close(commit);
3325 if (err) {
3326 free(*line_offsets);
3327 *line_offsets = NULL;
3328 *nlines = 0;
3330 return err;
3333 static const struct got_error *
3334 create_diff(struct tog_diff_view_state *s)
3336 const struct got_error *err = NULL;
3337 FILE *f = NULL;
3338 int obj_type;
3340 free(s->line_offsets);
3341 s->line_offsets = malloc(sizeof(off_t));
3342 if (s->line_offsets == NULL)
3343 return got_error_from_errno("malloc");
3344 s->nlines = 0;
3346 f = got_opentemp();
3347 if (f == NULL) {
3348 err = got_error_from_errno("got_opentemp");
3349 goto done;
3351 if (s->f && fclose(s->f) == EOF) {
3352 err = got_error_from_errno("fclose");
3353 goto done;
3355 s->f = f;
3357 if (s->id1)
3358 err = got_object_get_type(&obj_type, s->repo, s->id1);
3359 else
3360 err = got_object_get_type(&obj_type, s->repo, s->id2);
3361 if (err)
3362 goto done;
3364 switch (obj_type) {
3365 case GOT_OBJ_TYPE_BLOB:
3366 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3367 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3368 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3369 break;
3370 case GOT_OBJ_TYPE_TREE:
3371 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3372 s->id1, s->id2, NULL, "", "", s->diff_context,
3373 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3374 break;
3375 case GOT_OBJ_TYPE_COMMIT: {
3376 const struct got_object_id_queue *parent_ids;
3377 struct got_object_qid *pid;
3378 struct got_commit_object *commit2;
3379 struct got_reflist_head *refs;
3381 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3382 if (err)
3383 goto done;
3384 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3385 /* Show commit info if we're diffing to a parent/root commit. */
3386 if (s->id1 == NULL) {
3387 err = write_commit_info(&s->line_offsets, &s->nlines,
3388 s->id2, refs, s->repo, s->f);
3389 if (err)
3390 goto done;
3391 } else {
3392 parent_ids = got_object_commit_get_parent_ids(commit2);
3393 STAILQ_FOREACH(pid, parent_ids, entry) {
3394 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3395 err = write_commit_info(
3396 &s->line_offsets, &s->nlines,
3397 s->id2, refs, s->repo, s->f);
3398 if (err)
3399 goto done;
3400 break;
3404 got_object_commit_close(commit2);
3406 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3407 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3408 s->force_text_diff, s->repo, s->f);
3409 break;
3411 default:
3412 err = got_error(GOT_ERR_OBJ_TYPE);
3413 break;
3415 if (err)
3416 goto done;
3417 done:
3418 if (s->f && fflush(s->f) != 0 && err == NULL)
3419 err = got_error_from_errno("fflush");
3420 return err;
3423 static void
3424 diff_view_indicate_progress(struct tog_view *view)
3426 mvwaddstr(view->window, 0, 0, "diffing...");
3427 update_panels();
3428 doupdate();
3431 static const struct got_error *
3432 search_start_diff_view(struct tog_view *view)
3434 struct tog_diff_view_state *s = &view->state.diff;
3436 s->matched_line = 0;
3437 return NULL;
3440 static const struct got_error *
3441 search_next_diff_view(struct tog_view *view)
3443 struct tog_diff_view_state *s = &view->state.diff;
3444 int lineno;
3445 char *line = NULL;
3446 size_t linesize = 0;
3447 ssize_t linelen;
3449 if (!view->searching) {
3450 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3451 return NULL;
3454 if (s->matched_line) {
3455 if (view->searching == TOG_SEARCH_FORWARD)
3456 lineno = s->matched_line + 1;
3457 else
3458 lineno = s->matched_line - 1;
3459 } else
3460 lineno = s->first_displayed_line;
3462 while (1) {
3463 off_t offset;
3465 if (lineno <= 0 || lineno > s->nlines) {
3466 if (s->matched_line == 0) {
3467 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3468 break;
3471 if (view->searching == TOG_SEARCH_FORWARD)
3472 lineno = 1;
3473 else
3474 lineno = s->nlines;
3477 offset = s->line_offsets[lineno - 1];
3478 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3479 free(line);
3480 return got_error_from_errno("fseeko");
3482 linelen = getline(&line, &linesize, s->f);
3483 if (linelen != -1 &&
3484 match_line(line, &view->regex, 1, &view->regmatch)) {
3485 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3486 s->matched_line = lineno;
3487 break;
3489 if (view->searching == TOG_SEARCH_FORWARD)
3490 lineno++;
3491 else
3492 lineno--;
3494 free(line);
3496 if (s->matched_line) {
3497 s->first_displayed_line = s->matched_line;
3498 s->selected_line = 1;
3501 return NULL;
3504 static const struct got_error *
3505 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3506 struct got_object_id *id2, const char *label1, const char *label2,
3507 int diff_context, int ignore_whitespace, int force_text_diff,
3508 struct tog_view *log_view, struct got_repository *repo)
3510 const struct got_error *err;
3511 struct tog_diff_view_state *s = &view->state.diff;
3513 if (id1 != NULL && id2 != NULL) {
3514 int type1, type2;
3515 err = got_object_get_type(&type1, repo, id1);
3516 if (err)
3517 return err;
3518 err = got_object_get_type(&type2, repo, id2);
3519 if (err)
3520 return err;
3522 if (type1 != type2)
3523 return got_error(GOT_ERR_OBJ_TYPE);
3525 s->first_displayed_line = 1;
3526 s->last_displayed_line = view->nlines;
3527 s->selected_line = 1;
3528 s->repo = repo;
3529 s->id1 = id1;
3530 s->id2 = id2;
3531 s->label1 = label1;
3532 s->label2 = label2;
3534 if (id1) {
3535 s->id1 = got_object_id_dup(id1);
3536 if (s->id1 == NULL)
3537 return got_error_from_errno("got_object_id_dup");
3538 } else
3539 s->id1 = NULL;
3541 s->id2 = got_object_id_dup(id2);
3542 if (s->id2 == NULL) {
3543 free(s->id1);
3544 s->id1 = NULL;
3545 return got_error_from_errno("got_object_id_dup");
3547 s->f = NULL;
3548 s->first_displayed_line = 1;
3549 s->last_displayed_line = view->nlines;
3550 s->diff_context = diff_context;
3551 s->ignore_whitespace = ignore_whitespace;
3552 s->force_text_diff = force_text_diff;
3553 s->log_view = log_view;
3554 s->repo = repo;
3556 STAILQ_INIT(&s->colors);
3557 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3558 err = add_color(&s->colors,
3559 "^-", TOG_COLOR_DIFF_MINUS,
3560 get_color_value("TOG_COLOR_DIFF_MINUS"));
3561 if (err)
3562 return err;
3563 err = add_color(&s->colors, "^\\+",
3564 TOG_COLOR_DIFF_PLUS,
3565 get_color_value("TOG_COLOR_DIFF_PLUS"));
3566 if (err) {
3567 free_colors(&s->colors);
3568 return err;
3570 err = add_color(&s->colors,
3571 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3572 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3573 if (err) {
3574 free_colors(&s->colors);
3575 return err;
3578 err = add_color(&s->colors,
3579 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3580 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3581 get_color_value("TOG_COLOR_DIFF_META"));
3582 if (err) {
3583 free_colors(&s->colors);
3584 return err;
3587 err = add_color(&s->colors,
3588 "^(from|via): ", TOG_COLOR_AUTHOR,
3589 get_color_value("TOG_COLOR_AUTHOR"));
3590 if (err) {
3591 free_colors(&s->colors);
3592 return err;
3595 err = add_color(&s->colors,
3596 "^date: ", TOG_COLOR_DATE,
3597 get_color_value("TOG_COLOR_DATE"));
3598 if (err) {
3599 free_colors(&s->colors);
3600 return err;
3604 if (log_view && view_is_splitscreen(view))
3605 show_log_view(log_view); /* draw vborder */
3606 diff_view_indicate_progress(view);
3608 s->line_offsets = NULL;
3609 s->nlines = 0;
3610 err = create_diff(s);
3611 if (err) {
3612 free(s->id1);
3613 s->id1 = NULL;
3614 free(s->id2);
3615 s->id2 = NULL;
3616 free_colors(&s->colors);
3617 return err;
3620 view->show = show_diff_view;
3621 view->input = input_diff_view;
3622 view->close = close_diff_view;
3623 view->search_start = search_start_diff_view;
3624 view->search_next = search_next_diff_view;
3626 return NULL;
3629 static const struct got_error *
3630 close_diff_view(struct tog_view *view)
3632 const struct got_error *err = NULL;
3633 struct tog_diff_view_state *s = &view->state.diff;
3635 free(s->id1);
3636 s->id1 = NULL;
3637 free(s->id2);
3638 s->id2 = NULL;
3639 if (s->f && fclose(s->f) == EOF)
3640 err = got_error_from_errno("fclose");
3641 free_colors(&s->colors);
3642 free(s->line_offsets);
3643 s->line_offsets = NULL;
3644 s->nlines = 0;
3645 return err;
3648 static const struct got_error *
3649 show_diff_view(struct tog_view *view)
3651 const struct got_error *err;
3652 struct tog_diff_view_state *s = &view->state.diff;
3653 char *id_str1 = NULL, *id_str2, *header;
3654 const char *label1, *label2;
3656 if (s->id1) {
3657 err = got_object_id_str(&id_str1, s->id1);
3658 if (err)
3659 return err;
3660 label1 = s->label1 ? : id_str1;
3661 } else
3662 label1 = "/dev/null";
3664 err = got_object_id_str(&id_str2, s->id2);
3665 if (err)
3666 return err;
3667 label2 = s->label2 ? : id_str2;
3669 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3670 err = got_error_from_errno("asprintf");
3671 free(id_str1);
3672 free(id_str2);
3673 return err;
3675 free(id_str1);
3676 free(id_str2);
3678 err = draw_file(view, header);
3679 free(header);
3680 return err;
3683 static const struct got_error *
3684 set_selected_commit(struct tog_diff_view_state *s,
3685 struct commit_queue_entry *entry)
3687 const struct got_error *err;
3688 const struct got_object_id_queue *parent_ids;
3689 struct got_commit_object *selected_commit;
3690 struct got_object_qid *pid;
3692 free(s->id2);
3693 s->id2 = got_object_id_dup(entry->id);
3694 if (s->id2 == NULL)
3695 return got_error_from_errno("got_object_id_dup");
3697 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3698 if (err)
3699 return err;
3700 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3701 free(s->id1);
3702 pid = STAILQ_FIRST(parent_ids);
3703 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3704 got_object_commit_close(selected_commit);
3705 return NULL;
3708 static const struct got_error *
3709 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3711 const struct got_error *err = NULL;
3712 struct tog_diff_view_state *s = &view->state.diff;
3713 struct tog_log_view_state *ls;
3714 struct commit_queue_entry *old_selected_entry;
3715 char *line = NULL;
3716 size_t linesize = 0;
3717 ssize_t linelen;
3718 int i;
3720 switch (ch) {
3721 case 'a':
3722 case 'w':
3723 if (ch == 'a')
3724 s->force_text_diff = !s->force_text_diff;
3725 if (ch == 'w')
3726 s->ignore_whitespace = !s->ignore_whitespace;
3727 wclear(view->window);
3728 s->first_displayed_line = 1;
3729 s->last_displayed_line = view->nlines;
3730 s->matched_line = 0;
3731 diff_view_indicate_progress(view);
3732 err = create_diff(s);
3733 break;
3734 case 'g':
3735 case KEY_HOME:
3736 s->first_displayed_line = 1;
3737 break;
3738 case 'G':
3739 case KEY_END:
3740 if (s->eof)
3741 break;
3743 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3744 s->eof = 1;
3745 break;
3746 case 'k':
3747 case KEY_UP:
3748 case CTRL('p'):
3749 if (s->first_displayed_line > 1)
3750 s->first_displayed_line--;
3751 break;
3752 case KEY_PPAGE:
3753 case CTRL('b'):
3754 if (s->first_displayed_line == 1)
3755 break;
3756 i = 0;
3757 while (i++ < view->nlines - 1 &&
3758 s->first_displayed_line > 1)
3759 s->first_displayed_line--;
3760 break;
3761 case 'j':
3762 case KEY_DOWN:
3763 case CTRL('n'):
3764 if (!s->eof)
3765 s->first_displayed_line++;
3766 break;
3767 case KEY_NPAGE:
3768 case CTRL('f'):
3769 case ' ':
3770 if (s->eof)
3771 break;
3772 i = 0;
3773 while (!s->eof && i++ < view->nlines - 1) {
3774 linelen = getline(&line, &linesize, s->f);
3775 s->first_displayed_line++;
3776 if (linelen == -1) {
3777 if (feof(s->f)) {
3778 s->eof = 1;
3779 } else
3780 err = got_ferror(s->f, GOT_ERR_IO);
3781 break;
3784 free(line);
3785 break;
3786 case '[':
3787 if (s->diff_context > 0) {
3788 s->diff_context--;
3789 s->matched_line = 0;
3790 diff_view_indicate_progress(view);
3791 err = create_diff(s);
3792 if (s->first_displayed_line + view->nlines - 1 >
3793 s->nlines) {
3794 s->first_displayed_line = 1;
3795 s->last_displayed_line = view->nlines;
3798 break;
3799 case ']':
3800 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3801 s->diff_context++;
3802 s->matched_line = 0;
3803 diff_view_indicate_progress(view);
3804 err = create_diff(s);
3806 break;
3807 case '<':
3808 case ',':
3809 if (s->log_view == NULL)
3810 break;
3811 ls = &s->log_view->state.log;
3812 old_selected_entry = ls->selected_entry;
3814 err = input_log_view(NULL, s->log_view, KEY_UP);
3815 if (err)
3816 break;
3818 if (old_selected_entry == ls->selected_entry)
3819 break;
3821 err = set_selected_commit(s, ls->selected_entry);
3822 if (err)
3823 break;
3825 s->first_displayed_line = 1;
3826 s->last_displayed_line = view->nlines;
3827 s->matched_line = 0;
3829 diff_view_indicate_progress(view);
3830 err = create_diff(s);
3831 break;
3832 case '>':
3833 case '.':
3834 if (s->log_view == NULL)
3835 break;
3836 ls = &s->log_view->state.log;
3837 old_selected_entry = ls->selected_entry;
3839 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3840 if (err)
3841 break;
3843 if (old_selected_entry == ls->selected_entry)
3844 break;
3846 err = set_selected_commit(s, ls->selected_entry);
3847 if (err)
3848 break;
3850 s->first_displayed_line = 1;
3851 s->last_displayed_line = view->nlines;
3852 s->matched_line = 0;
3854 diff_view_indicate_progress(view);
3855 err = create_diff(s);
3856 break;
3857 default:
3858 break;
3861 return err;
3864 static const struct got_error *
3865 cmd_diff(int argc, char *argv[])
3867 const struct got_error *error = NULL;
3868 struct got_repository *repo = NULL;
3869 struct got_worktree *worktree = NULL;
3870 struct got_object_id *id1 = NULL, *id2 = NULL;
3871 char *repo_path = NULL, *cwd = NULL;
3872 char *id_str1 = NULL, *id_str2 = NULL;
3873 char *label1 = NULL, *label2 = NULL;
3874 int diff_context = 3, ignore_whitespace = 0;
3875 int ch, force_text_diff = 0;
3876 const char *errstr;
3877 struct tog_view *view;
3879 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3880 switch (ch) {
3881 case 'a':
3882 force_text_diff = 1;
3883 break;
3884 case 'C':
3885 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3886 &errstr);
3887 if (errstr != NULL)
3888 errx(1, "number of context lines is %s: %s",
3889 errstr, errstr);
3890 break;
3891 case 'r':
3892 repo_path = realpath(optarg, NULL);
3893 if (repo_path == NULL)
3894 return got_error_from_errno2("realpath",
3895 optarg);
3896 got_path_strip_trailing_slashes(repo_path);
3897 break;
3898 case 'w':
3899 ignore_whitespace = 1;
3900 break;
3901 default:
3902 usage_diff();
3903 /* NOTREACHED */
3907 argc -= optind;
3908 argv += optind;
3910 if (argc == 0) {
3911 usage_diff(); /* TODO show local worktree changes */
3912 } else if (argc == 2) {
3913 id_str1 = argv[0];
3914 id_str2 = argv[1];
3915 } else
3916 usage_diff();
3918 if (repo_path == NULL) {
3919 cwd = getcwd(NULL, 0);
3920 if (cwd == NULL)
3921 return got_error_from_errno("getcwd");
3922 error = got_worktree_open(&worktree, cwd);
3923 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3924 goto done;
3925 if (worktree)
3926 repo_path =
3927 strdup(got_worktree_get_repo_path(worktree));
3928 else
3929 repo_path = strdup(cwd);
3930 if (repo_path == NULL) {
3931 error = got_error_from_errno("strdup");
3932 goto done;
3936 error = got_repo_open(&repo, repo_path, NULL);
3937 if (error)
3938 goto done;
3940 init_curses();
3942 error = apply_unveil(got_repo_get_path(repo), NULL);
3943 if (error)
3944 goto done;
3946 error = tog_load_refs(repo, 0);
3947 if (error)
3948 goto done;
3950 error = got_repo_match_object_id(&id1, &label1, id_str1,
3951 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3952 if (error)
3953 goto done;
3955 error = got_repo_match_object_id(&id2, &label2, id_str2,
3956 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3957 if (error)
3958 goto done;
3960 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3961 if (view == NULL) {
3962 error = got_error_from_errno("view_open");
3963 goto done;
3965 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3966 ignore_whitespace, force_text_diff, NULL, repo);
3967 if (error)
3968 goto done;
3969 error = view_loop(view);
3970 done:
3971 free(label1);
3972 free(label2);
3973 free(repo_path);
3974 free(cwd);
3975 if (repo) {
3976 const struct got_error *close_err = got_repo_close(repo);
3977 if (error == NULL)
3978 error = close_err;
3980 if (worktree)
3981 got_worktree_close(worktree);
3982 tog_free_refs();
3983 return error;
3986 __dead static void
3987 usage_blame(void)
3989 endwin();
3990 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3991 getprogname());
3992 exit(1);
3995 struct tog_blame_line {
3996 int annotated;
3997 struct got_object_id *id;
4000 static const struct got_error *
4001 draw_blame(struct tog_view *view)
4003 struct tog_blame_view_state *s = &view->state.blame;
4004 struct tog_blame *blame = &s->blame;
4005 regmatch_t *regmatch = &view->regmatch;
4006 const struct got_error *err;
4007 int lineno = 0, nprinted = 0;
4008 char *line = NULL;
4009 size_t linesize = 0;
4010 ssize_t linelen;
4011 wchar_t *wline;
4012 int width;
4013 struct tog_blame_line *blame_line;
4014 struct got_object_id *prev_id = NULL;
4015 char *id_str;
4016 struct tog_color *tc;
4018 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4019 if (err)
4020 return err;
4022 rewind(blame->f);
4023 werase(view->window);
4025 if (asprintf(&line, "commit %s", id_str) == -1) {
4026 err = got_error_from_errno("asprintf");
4027 free(id_str);
4028 return err;
4031 err = format_line(&wline, &width, line, view->ncols, 0);
4032 free(line);
4033 line = NULL;
4034 if (err)
4035 return err;
4036 if (view_needs_focus_indication(view))
4037 wstandout(view->window);
4038 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4039 if (tc)
4040 wattr_on(view->window,
4041 COLOR_PAIR(tc->colorpair), NULL);
4042 waddwstr(view->window, wline);
4043 if (tc)
4044 wattr_off(view->window,
4045 COLOR_PAIR(tc->colorpair), NULL);
4046 if (view_needs_focus_indication(view))
4047 wstandend(view->window);
4048 free(wline);
4049 wline = NULL;
4050 if (width < view->ncols - 1)
4051 waddch(view->window, '\n');
4053 if (asprintf(&line, "[%d/%d] %s%s",
4054 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4055 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4056 free(id_str);
4057 return got_error_from_errno("asprintf");
4059 free(id_str);
4060 err = format_line(&wline, &width, line, view->ncols, 0);
4061 free(line);
4062 line = NULL;
4063 if (err)
4064 return err;
4065 waddwstr(view->window, wline);
4066 free(wline);
4067 wline = NULL;
4068 if (width < view->ncols - 1)
4069 waddch(view->window, '\n');
4071 s->eof = 0;
4072 while (nprinted < view->nlines - 2) {
4073 linelen = getline(&line, &linesize, blame->f);
4074 if (linelen == -1) {
4075 if (feof(blame->f)) {
4076 s->eof = 1;
4077 break;
4079 free(line);
4080 return got_ferror(blame->f, GOT_ERR_IO);
4082 if (++lineno < s->first_displayed_line)
4083 continue;
4085 if (view->focussed && nprinted == s->selected_line - 1)
4086 wstandout(view->window);
4088 if (blame->nlines > 0) {
4089 blame_line = &blame->lines[lineno - 1];
4090 if (blame_line->annotated && prev_id &&
4091 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4092 !(view->focussed &&
4093 nprinted == s->selected_line - 1)) {
4094 waddstr(view->window, " ");
4095 } else if (blame_line->annotated) {
4096 char *id_str;
4097 err = got_object_id_str(&id_str, blame_line->id);
4098 if (err) {
4099 free(line);
4100 return err;
4102 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4103 if (tc)
4104 wattr_on(view->window,
4105 COLOR_PAIR(tc->colorpair), NULL);
4106 wprintw(view->window, "%.8s", id_str);
4107 if (tc)
4108 wattr_off(view->window,
4109 COLOR_PAIR(tc->colorpair), NULL);
4110 free(id_str);
4111 prev_id = blame_line->id;
4112 } else {
4113 waddstr(view->window, "........");
4114 prev_id = NULL;
4116 } else {
4117 waddstr(view->window, "........");
4118 prev_id = NULL;
4121 if (view->focussed && nprinted == s->selected_line - 1)
4122 wstandend(view->window);
4123 waddstr(view->window, " ");
4125 if (view->ncols <= 9) {
4126 width = 9;
4127 wline = wcsdup(L"");
4128 if (wline == NULL) {
4129 err = got_error_from_errno("wcsdup");
4130 free(line);
4131 return err;
4133 } else if (s->first_displayed_line + nprinted ==
4134 s->matched_line &&
4135 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4136 err = add_matched_line(&width, line, view->ncols - 9, 9,
4137 view->window, regmatch);
4138 if (err) {
4139 free(line);
4140 return err;
4142 width += 9;
4143 } else {
4144 err = format_line(&wline, &width, line,
4145 view->ncols - 9, 9);
4146 waddwstr(view->window, wline);
4147 free(wline);
4148 wline = NULL;
4149 width += 9;
4152 if (width <= view->ncols - 1)
4153 waddch(view->window, '\n');
4154 if (++nprinted == 1)
4155 s->first_displayed_line = lineno;
4157 free(line);
4158 s->last_displayed_line = lineno;
4160 view_vborder(view);
4162 return NULL;
4165 static const struct got_error *
4166 blame_cb(void *arg, int nlines, int lineno,
4167 struct got_commit_object *commit, struct got_object_id *id)
4169 const struct got_error *err = NULL;
4170 struct tog_blame_cb_args *a = arg;
4171 struct tog_blame_line *line;
4172 int errcode;
4174 if (nlines != a->nlines ||
4175 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4176 return got_error(GOT_ERR_RANGE);
4178 errcode = pthread_mutex_lock(&tog_mutex);
4179 if (errcode)
4180 return got_error_set_errno(errcode, "pthread_mutex_lock");
4182 if (*a->quit) { /* user has quit the blame view */
4183 err = got_error(GOT_ERR_ITER_COMPLETED);
4184 goto done;
4187 if (lineno == -1)
4188 goto done; /* no change in this commit */
4190 line = &a->lines[lineno - 1];
4191 if (line->annotated)
4192 goto done;
4194 line->id = got_object_id_dup(id);
4195 if (line->id == NULL) {
4196 err = got_error_from_errno("got_object_id_dup");
4197 goto done;
4199 line->annotated = 1;
4200 done:
4201 errcode = pthread_mutex_unlock(&tog_mutex);
4202 if (errcode)
4203 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4204 return err;
4207 static void *
4208 blame_thread(void *arg)
4210 const struct got_error *err, *close_err;
4211 struct tog_blame_thread_args *ta = arg;
4212 struct tog_blame_cb_args *a = ta->cb_args;
4213 int errcode;
4215 err = block_signals_used_by_main_thread();
4216 if (err)
4217 return (void *)err;
4219 err = got_blame(ta->path, a->commit_id, ta->repo,
4220 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4221 if (err && err->code == GOT_ERR_CANCELLED)
4222 err = NULL;
4224 errcode = pthread_mutex_lock(&tog_mutex);
4225 if (errcode)
4226 return (void *)got_error_set_errno(errcode,
4227 "pthread_mutex_lock");
4229 close_err = got_repo_close(ta->repo);
4230 if (err == NULL)
4231 err = close_err;
4232 ta->repo = NULL;
4233 *ta->complete = 1;
4235 errcode = pthread_mutex_unlock(&tog_mutex);
4236 if (errcode && err == NULL)
4237 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4239 return (void *)err;
4242 static struct got_object_id *
4243 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4244 int first_displayed_line, int selected_line)
4246 struct tog_blame_line *line;
4248 if (nlines <= 0)
4249 return NULL;
4251 line = &lines[first_displayed_line - 1 + selected_line - 1];
4252 if (!line->annotated)
4253 return NULL;
4255 return line->id;
4258 static const struct got_error *
4259 stop_blame(struct tog_blame *blame)
4261 const struct got_error *err = NULL;
4262 int i;
4264 if (blame->thread) {
4265 int errcode;
4266 errcode = pthread_mutex_unlock(&tog_mutex);
4267 if (errcode)
4268 return got_error_set_errno(errcode,
4269 "pthread_mutex_unlock");
4270 errcode = pthread_join(blame->thread, (void **)&err);
4271 if (errcode)
4272 return got_error_set_errno(errcode, "pthread_join");
4273 errcode = pthread_mutex_lock(&tog_mutex);
4274 if (errcode)
4275 return got_error_set_errno(errcode,
4276 "pthread_mutex_lock");
4277 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4278 err = NULL;
4279 blame->thread = NULL;
4281 if (blame->thread_args.repo) {
4282 const struct got_error *close_err;
4283 close_err = got_repo_close(blame->thread_args.repo);
4284 if (err == NULL)
4285 err = close_err;
4286 blame->thread_args.repo = NULL;
4288 if (blame->f) {
4289 if (fclose(blame->f) == EOF && err == NULL)
4290 err = got_error_from_errno("fclose");
4291 blame->f = NULL;
4293 if (blame->lines) {
4294 for (i = 0; i < blame->nlines; i++)
4295 free(blame->lines[i].id);
4296 free(blame->lines);
4297 blame->lines = NULL;
4299 free(blame->cb_args.commit_id);
4300 blame->cb_args.commit_id = NULL;
4302 return err;
4305 static const struct got_error *
4306 cancel_blame_view(void *arg)
4308 const struct got_error *err = NULL;
4309 int *done = arg;
4310 int errcode;
4312 errcode = pthread_mutex_lock(&tog_mutex);
4313 if (errcode)
4314 return got_error_set_errno(errcode,
4315 "pthread_mutex_unlock");
4317 if (*done)
4318 err = got_error(GOT_ERR_CANCELLED);
4320 errcode = pthread_mutex_unlock(&tog_mutex);
4321 if (errcode)
4322 return got_error_set_errno(errcode,
4323 "pthread_mutex_lock");
4325 return err;
4328 static const struct got_error *
4329 run_blame(struct tog_view *view)
4331 struct tog_blame_view_state *s = &view->state.blame;
4332 struct tog_blame *blame = &s->blame;
4333 const struct got_error *err = NULL;
4334 struct got_commit_object *commit = NULL;
4335 struct got_blob_object *blob = NULL;
4336 struct got_repository *thread_repo = NULL;
4337 struct got_object_id *obj_id = NULL;
4338 int obj_type;
4340 err = got_object_open_as_commit(&commit, s->repo,
4341 &s->blamed_commit->id);
4342 if (err)
4343 return err;
4345 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4346 if (err)
4347 goto done;
4349 err = got_object_get_type(&obj_type, s->repo, obj_id);
4350 if (err)
4351 goto done;
4353 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4354 err = got_error(GOT_ERR_OBJ_TYPE);
4355 goto done;
4358 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4359 if (err)
4360 goto done;
4361 blame->f = got_opentemp();
4362 if (blame->f == NULL) {
4363 err = got_error_from_errno("got_opentemp");
4364 goto done;
4366 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4367 &blame->line_offsets, blame->f, blob);
4368 if (err)
4369 goto done;
4370 if (blame->nlines == 0) {
4371 s->blame_complete = 1;
4372 goto done;
4375 /* Don't include \n at EOF in the blame line count. */
4376 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4377 blame->nlines--;
4379 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4380 if (blame->lines == NULL) {
4381 err = got_error_from_errno("calloc");
4382 goto done;
4385 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4386 if (err)
4387 goto done;
4389 blame->cb_args.view = view;
4390 blame->cb_args.lines = blame->lines;
4391 blame->cb_args.nlines = blame->nlines;
4392 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4393 if (blame->cb_args.commit_id == NULL) {
4394 err = got_error_from_errno("got_object_id_dup");
4395 goto done;
4397 blame->cb_args.quit = &s->done;
4399 blame->thread_args.path = s->path;
4400 blame->thread_args.repo = thread_repo;
4401 blame->thread_args.cb_args = &blame->cb_args;
4402 blame->thread_args.complete = &s->blame_complete;
4403 blame->thread_args.cancel_cb = cancel_blame_view;
4404 blame->thread_args.cancel_arg = &s->done;
4405 s->blame_complete = 0;
4407 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4408 s->first_displayed_line = 1;
4409 s->last_displayed_line = view->nlines;
4410 s->selected_line = 1;
4412 s->matched_line = 0;
4414 done:
4415 if (commit)
4416 got_object_commit_close(commit);
4417 if (blob)
4418 got_object_blob_close(blob);
4419 free(obj_id);
4420 if (err)
4421 stop_blame(blame);
4422 return err;
4425 static const struct got_error *
4426 open_blame_view(struct tog_view *view, char *path,
4427 struct got_object_id *commit_id, struct got_repository *repo)
4429 const struct got_error *err = NULL;
4430 struct tog_blame_view_state *s = &view->state.blame;
4432 STAILQ_INIT(&s->blamed_commits);
4434 s->path = strdup(path);
4435 if (s->path == NULL)
4436 return got_error_from_errno("strdup");
4438 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4439 if (err) {
4440 free(s->path);
4441 return err;
4444 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4445 s->first_displayed_line = 1;
4446 s->last_displayed_line = view->nlines;
4447 s->selected_line = 1;
4448 s->blame_complete = 0;
4449 s->repo = repo;
4450 s->commit_id = commit_id;
4451 memset(&s->blame, 0, sizeof(s->blame));
4453 STAILQ_INIT(&s->colors);
4454 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4455 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4456 get_color_value("TOG_COLOR_COMMIT"));
4457 if (err)
4458 return err;
4461 view->show = show_blame_view;
4462 view->input = input_blame_view;
4463 view->close = close_blame_view;
4464 view->search_start = search_start_blame_view;
4465 view->search_next = search_next_blame_view;
4467 return run_blame(view);
4470 static const struct got_error *
4471 close_blame_view(struct tog_view *view)
4473 const struct got_error *err = NULL;
4474 struct tog_blame_view_state *s = &view->state.blame;
4476 if (s->blame.thread)
4477 err = stop_blame(&s->blame);
4479 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4480 struct got_object_qid *blamed_commit;
4481 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4482 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4483 got_object_qid_free(blamed_commit);
4486 free(s->path);
4487 free_colors(&s->colors);
4489 return err;
4492 static const struct got_error *
4493 search_start_blame_view(struct tog_view *view)
4495 struct tog_blame_view_state *s = &view->state.blame;
4497 s->matched_line = 0;
4498 return NULL;
4501 static const struct got_error *
4502 search_next_blame_view(struct tog_view *view)
4504 struct tog_blame_view_state *s = &view->state.blame;
4505 int lineno;
4506 char *line = NULL;
4507 size_t linesize = 0;
4508 ssize_t linelen;
4510 if (!view->searching) {
4511 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4512 return NULL;
4515 if (s->matched_line) {
4516 if (view->searching == TOG_SEARCH_FORWARD)
4517 lineno = s->matched_line + 1;
4518 else
4519 lineno = s->matched_line - 1;
4520 } else
4521 lineno = s->first_displayed_line - 1 + s->selected_line;
4523 while (1) {
4524 off_t offset;
4526 if (lineno <= 0 || lineno > s->blame.nlines) {
4527 if (s->matched_line == 0) {
4528 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4529 break;
4532 if (view->searching == TOG_SEARCH_FORWARD)
4533 lineno = 1;
4534 else
4535 lineno = s->blame.nlines;
4538 offset = s->blame.line_offsets[lineno - 1];
4539 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4540 free(line);
4541 return got_error_from_errno("fseeko");
4543 linelen = getline(&line, &linesize, s->blame.f);
4544 if (linelen != -1 &&
4545 match_line(line, &view->regex, 1, &view->regmatch)) {
4546 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4547 s->matched_line = lineno;
4548 break;
4550 if (view->searching == TOG_SEARCH_FORWARD)
4551 lineno++;
4552 else
4553 lineno--;
4555 free(line);
4557 if (s->matched_line) {
4558 s->first_displayed_line = s->matched_line;
4559 s->selected_line = 1;
4562 return NULL;
4565 static const struct got_error *
4566 show_blame_view(struct tog_view *view)
4568 const struct got_error *err = NULL;
4569 struct tog_blame_view_state *s = &view->state.blame;
4570 int errcode;
4572 if (s->blame.thread == NULL && !s->blame_complete) {
4573 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4574 &s->blame.thread_args);
4575 if (errcode)
4576 return got_error_set_errno(errcode, "pthread_create");
4578 halfdelay(1); /* fast refresh while annotating */
4581 if (s->blame_complete)
4582 halfdelay(10); /* disable fast refresh */
4584 err = draw_blame(view);
4586 view_vborder(view);
4587 return err;
4590 static const struct got_error *
4591 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4593 const struct got_error *err = NULL, *thread_err = NULL;
4594 struct tog_view *diff_view;
4595 struct tog_blame_view_state *s = &view->state.blame;
4596 int begin_x = 0;
4598 switch (ch) {
4599 case 'q':
4600 s->done = 1;
4601 break;
4602 case 'g':
4603 case KEY_HOME:
4604 s->selected_line = 1;
4605 s->first_displayed_line = 1;
4606 break;
4607 case 'G':
4608 case KEY_END:
4609 if (s->blame.nlines < view->nlines - 2) {
4610 s->selected_line = s->blame.nlines;
4611 s->first_displayed_line = 1;
4612 } else {
4613 s->selected_line = view->nlines - 2;
4614 s->first_displayed_line = s->blame.nlines -
4615 (view->nlines - 3);
4617 break;
4618 case 'k':
4619 case KEY_UP:
4620 case CTRL('p'):
4621 if (s->selected_line > 1)
4622 s->selected_line--;
4623 else if (s->selected_line == 1 &&
4624 s->first_displayed_line > 1)
4625 s->first_displayed_line--;
4626 break;
4627 case KEY_PPAGE:
4628 case CTRL('b'):
4629 if (s->first_displayed_line == 1) {
4630 s->selected_line = 1;
4631 break;
4633 if (s->first_displayed_line > view->nlines - 2)
4634 s->first_displayed_line -=
4635 (view->nlines - 2);
4636 else
4637 s->first_displayed_line = 1;
4638 break;
4639 case 'j':
4640 case KEY_DOWN:
4641 case CTRL('n'):
4642 if (s->selected_line < view->nlines - 2 &&
4643 s->first_displayed_line +
4644 s->selected_line <= s->blame.nlines)
4645 s->selected_line++;
4646 else if (s->last_displayed_line <
4647 s->blame.nlines)
4648 s->first_displayed_line++;
4649 break;
4650 case 'b':
4651 case 'p': {
4652 struct got_object_id *id = NULL;
4653 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4654 s->first_displayed_line, s->selected_line);
4655 if (id == NULL)
4656 break;
4657 if (ch == 'p') {
4658 struct got_commit_object *commit, *pcommit;
4659 struct got_object_qid *pid;
4660 struct got_object_id *blob_id = NULL;
4661 int obj_type;
4662 err = got_object_open_as_commit(&commit,
4663 s->repo, id);
4664 if (err)
4665 break;
4666 pid = STAILQ_FIRST(
4667 got_object_commit_get_parent_ids(commit));
4668 if (pid == NULL) {
4669 got_object_commit_close(commit);
4670 break;
4672 /* Check if path history ends here. */
4673 err = got_object_open_as_commit(&pcommit,
4674 s->repo, &pid->id);
4675 if (err)
4676 break;
4677 err = got_object_id_by_path(&blob_id, s->repo,
4678 pcommit, s->path);
4679 got_object_commit_close(pcommit);
4680 if (err) {
4681 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4682 err = NULL;
4683 got_object_commit_close(commit);
4684 break;
4686 err = got_object_get_type(&obj_type, s->repo,
4687 blob_id);
4688 free(blob_id);
4689 /* Can't blame non-blob type objects. */
4690 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4691 got_object_commit_close(commit);
4692 break;
4694 err = got_object_qid_alloc(&s->blamed_commit,
4695 &pid->id);
4696 got_object_commit_close(commit);
4697 } else {
4698 if (got_object_id_cmp(id,
4699 &s->blamed_commit->id) == 0)
4700 break;
4701 err = got_object_qid_alloc(&s->blamed_commit,
4702 id);
4704 if (err)
4705 break;
4706 s->done = 1;
4707 thread_err = stop_blame(&s->blame);
4708 s->done = 0;
4709 if (thread_err)
4710 break;
4711 STAILQ_INSERT_HEAD(&s->blamed_commits,
4712 s->blamed_commit, entry);
4713 err = run_blame(view);
4714 if (err)
4715 break;
4716 break;
4718 case 'B': {
4719 struct got_object_qid *first;
4720 first = STAILQ_FIRST(&s->blamed_commits);
4721 if (!got_object_id_cmp(&first->id, s->commit_id))
4722 break;
4723 s->done = 1;
4724 thread_err = stop_blame(&s->blame);
4725 s->done = 0;
4726 if (thread_err)
4727 break;
4728 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4729 got_object_qid_free(s->blamed_commit);
4730 s->blamed_commit =
4731 STAILQ_FIRST(&s->blamed_commits);
4732 err = run_blame(view);
4733 if (err)
4734 break;
4735 break;
4737 case KEY_ENTER:
4738 case '\r': {
4739 struct got_object_id *id = NULL;
4740 struct got_object_qid *pid;
4741 struct got_commit_object *commit = NULL;
4742 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4743 s->first_displayed_line, s->selected_line);
4744 if (id == NULL)
4745 break;
4746 err = got_object_open_as_commit(&commit, s->repo, id);
4747 if (err)
4748 break;
4749 pid = STAILQ_FIRST(
4750 got_object_commit_get_parent_ids(commit));
4751 if (view_is_parent_view(view))
4752 begin_x = view_split_begin_x(view->begin_x);
4753 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4754 if (diff_view == NULL) {
4755 got_object_commit_close(commit);
4756 err = got_error_from_errno("view_open");
4757 break;
4759 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4760 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4761 got_object_commit_close(commit);
4762 if (err) {
4763 view_close(diff_view);
4764 break;
4766 view->focussed = 0;
4767 diff_view->focussed = 1;
4768 if (view_is_parent_view(view)) {
4769 err = view_close_child(view);
4770 if (err)
4771 break;
4772 view_set_child(view, diff_view);
4773 view->focus_child = 1;
4774 } else
4775 *new_view = diff_view;
4776 if (err)
4777 break;
4778 break;
4780 case KEY_NPAGE:
4781 case CTRL('f'):
4782 case ' ':
4783 if (s->last_displayed_line >= s->blame.nlines &&
4784 s->selected_line >= MIN(s->blame.nlines,
4785 view->nlines - 2)) {
4786 break;
4788 if (s->last_displayed_line >= s->blame.nlines &&
4789 s->selected_line < view->nlines - 2) {
4790 s->selected_line = MIN(s->blame.nlines,
4791 view->nlines - 2);
4792 break;
4794 if (s->last_displayed_line + view->nlines - 2
4795 <= s->blame.nlines)
4796 s->first_displayed_line +=
4797 view->nlines - 2;
4798 else
4799 s->first_displayed_line =
4800 s->blame.nlines -
4801 (view->nlines - 3);
4802 break;
4803 case KEY_RESIZE:
4804 if (s->selected_line > view->nlines - 2) {
4805 s->selected_line = MIN(s->blame.nlines,
4806 view->nlines - 2);
4808 break;
4809 default:
4810 break;
4812 return thread_err ? thread_err : err;
4815 static const struct got_error *
4816 cmd_blame(int argc, char *argv[])
4818 const struct got_error *error;
4819 struct got_repository *repo = NULL;
4820 struct got_worktree *worktree = NULL;
4821 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4822 char *link_target = NULL;
4823 struct got_object_id *commit_id = NULL;
4824 struct got_commit_object *commit = NULL;
4825 char *commit_id_str = NULL;
4826 int ch;
4827 struct tog_view *view;
4829 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4830 switch (ch) {
4831 case 'c':
4832 commit_id_str = optarg;
4833 break;
4834 case 'r':
4835 repo_path = realpath(optarg, NULL);
4836 if (repo_path == NULL)
4837 return got_error_from_errno2("realpath",
4838 optarg);
4839 break;
4840 default:
4841 usage_blame();
4842 /* NOTREACHED */
4846 argc -= optind;
4847 argv += optind;
4849 if (argc != 1)
4850 usage_blame();
4852 if (repo_path == NULL) {
4853 cwd = getcwd(NULL, 0);
4854 if (cwd == NULL)
4855 return got_error_from_errno("getcwd");
4856 error = got_worktree_open(&worktree, cwd);
4857 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4858 goto done;
4859 if (worktree)
4860 repo_path =
4861 strdup(got_worktree_get_repo_path(worktree));
4862 else
4863 repo_path = strdup(cwd);
4864 if (repo_path == NULL) {
4865 error = got_error_from_errno("strdup");
4866 goto done;
4870 error = got_repo_open(&repo, repo_path, NULL);
4871 if (error != NULL)
4872 goto done;
4874 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4875 worktree);
4876 if (error)
4877 goto done;
4879 init_curses();
4881 error = apply_unveil(got_repo_get_path(repo), NULL);
4882 if (error)
4883 goto done;
4885 error = tog_load_refs(repo, 0);
4886 if (error)
4887 goto done;
4889 if (commit_id_str == NULL) {
4890 struct got_reference *head_ref;
4891 error = got_ref_open(&head_ref, repo, worktree ?
4892 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4893 if (error != NULL)
4894 goto done;
4895 error = got_ref_resolve(&commit_id, repo, head_ref);
4896 got_ref_close(head_ref);
4897 } else {
4898 error = got_repo_match_object_id(&commit_id, NULL,
4899 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4901 if (error != NULL)
4902 goto done;
4904 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4905 if (view == NULL) {
4906 error = got_error_from_errno("view_open");
4907 goto done;
4910 error = got_object_open_as_commit(&commit, repo, commit_id);
4911 if (error)
4912 goto done;
4914 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4915 commit, repo);
4916 if (error)
4917 goto done;
4919 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4920 commit_id, repo);
4921 if (error)
4922 goto done;
4923 if (worktree) {
4924 /* Release work tree lock. */
4925 got_worktree_close(worktree);
4926 worktree = NULL;
4928 error = view_loop(view);
4929 done:
4930 free(repo_path);
4931 free(in_repo_path);
4932 free(link_target);
4933 free(cwd);
4934 free(commit_id);
4935 if (commit)
4936 got_object_commit_close(commit);
4937 if (worktree)
4938 got_worktree_close(worktree);
4939 if (repo) {
4940 const struct got_error *close_err = got_repo_close(repo);
4941 if (error == NULL)
4942 error = close_err;
4944 tog_free_refs();
4945 return error;
4948 static const struct got_error *
4949 draw_tree_entries(struct tog_view *view, const char *parent_path)
4951 struct tog_tree_view_state *s = &view->state.tree;
4952 const struct got_error *err = NULL;
4953 struct got_tree_entry *te;
4954 wchar_t *wline;
4955 struct tog_color *tc;
4956 int width, n, i, nentries;
4957 int limit = view->nlines;
4959 s->ndisplayed = 0;
4961 werase(view->window);
4963 if (limit == 0)
4964 return NULL;
4966 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4967 if (err)
4968 return err;
4969 if (view_needs_focus_indication(view))
4970 wstandout(view->window);
4971 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4972 if (tc)
4973 wattr_on(view->window,
4974 COLOR_PAIR(tc->colorpair), NULL);
4975 waddwstr(view->window, wline);
4976 if (tc)
4977 wattr_off(view->window,
4978 COLOR_PAIR(tc->colorpair), NULL);
4979 if (view_needs_focus_indication(view))
4980 wstandend(view->window);
4981 free(wline);
4982 wline = NULL;
4983 if (width < view->ncols - 1)
4984 waddch(view->window, '\n');
4985 if (--limit <= 0)
4986 return NULL;
4987 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4988 if (err)
4989 return err;
4990 waddwstr(view->window, wline);
4991 free(wline);
4992 wline = NULL;
4993 if (width < view->ncols - 1)
4994 waddch(view->window, '\n');
4995 if (--limit <= 0)
4996 return NULL;
4997 waddch(view->window, '\n');
4998 if (--limit <= 0)
4999 return NULL;
5001 if (s->first_displayed_entry == NULL) {
5002 te = got_object_tree_get_first_entry(s->tree);
5003 if (s->selected == 0) {
5004 if (view->focussed)
5005 wstandout(view->window);
5006 s->selected_entry = NULL;
5008 waddstr(view->window, " ..\n"); /* parent directory */
5009 if (s->selected == 0 && view->focussed)
5010 wstandend(view->window);
5011 s->ndisplayed++;
5012 if (--limit <= 0)
5013 return NULL;
5014 n = 1;
5015 } else {
5016 n = 0;
5017 te = s->first_displayed_entry;
5020 nentries = got_object_tree_get_nentries(s->tree);
5021 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5022 char *line = NULL, *id_str = NULL, *link_target = NULL;
5023 const char *modestr = "";
5024 mode_t mode;
5026 te = got_object_tree_get_entry(s->tree, i);
5027 mode = got_tree_entry_get_mode(te);
5029 if (s->show_ids) {
5030 err = got_object_id_str(&id_str,
5031 got_tree_entry_get_id(te));
5032 if (err)
5033 return got_error_from_errno(
5034 "got_object_id_str");
5036 if (got_object_tree_entry_is_submodule(te))
5037 modestr = "$";
5038 else if (S_ISLNK(mode)) {
5039 int i;
5041 err = got_tree_entry_get_symlink_target(&link_target,
5042 te, s->repo);
5043 if (err) {
5044 free(id_str);
5045 return err;
5047 for (i = 0; i < strlen(link_target); i++) {
5048 if (!isprint((unsigned char)link_target[i]))
5049 link_target[i] = '?';
5051 modestr = "@";
5053 else if (S_ISDIR(mode))
5054 modestr = "/";
5055 else if (mode & S_IXUSR)
5056 modestr = "*";
5057 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5058 got_tree_entry_get_name(te), modestr,
5059 link_target ? " -> ": "",
5060 link_target ? link_target : "") == -1) {
5061 free(id_str);
5062 free(link_target);
5063 return got_error_from_errno("asprintf");
5065 free(id_str);
5066 free(link_target);
5067 err = format_line(&wline, &width, line, view->ncols, 0);
5068 if (err) {
5069 free(line);
5070 break;
5072 if (n == s->selected) {
5073 if (view->focussed)
5074 wstandout(view->window);
5075 s->selected_entry = te;
5077 tc = match_color(&s->colors, line);
5078 if (tc)
5079 wattr_on(view->window,
5080 COLOR_PAIR(tc->colorpair), NULL);
5081 waddwstr(view->window, wline);
5082 if (tc)
5083 wattr_off(view->window,
5084 COLOR_PAIR(tc->colorpair), NULL);
5085 if (width < view->ncols - 1)
5086 waddch(view->window, '\n');
5087 if (n == s->selected && view->focussed)
5088 wstandend(view->window);
5089 free(line);
5090 free(wline);
5091 wline = NULL;
5092 n++;
5093 s->ndisplayed++;
5094 s->last_displayed_entry = te;
5095 if (--limit <= 0)
5096 break;
5099 return err;
5102 static void
5103 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5105 struct got_tree_entry *te;
5106 int isroot = s->tree == s->root;
5107 int i = 0;
5109 if (s->first_displayed_entry == NULL)
5110 return;
5112 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5113 while (i++ < maxscroll) {
5114 if (te == NULL) {
5115 if (!isroot)
5116 s->first_displayed_entry = NULL;
5117 break;
5119 s->first_displayed_entry = te;
5120 te = got_tree_entry_get_prev(s->tree, te);
5124 static void
5125 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5127 struct got_tree_entry *next, *last;
5128 int n = 0;
5130 if (s->first_displayed_entry)
5131 next = got_tree_entry_get_next(s->tree,
5132 s->first_displayed_entry);
5133 else
5134 next = got_object_tree_get_first_entry(s->tree);
5136 last = s->last_displayed_entry;
5137 while (next && last && n++ < maxscroll) {
5138 last = got_tree_entry_get_next(s->tree, last);
5139 if (last) {
5140 s->first_displayed_entry = next;
5141 next = got_tree_entry_get_next(s->tree, next);
5146 static const struct got_error *
5147 tree_entry_path(char **path, struct tog_parent_trees *parents,
5148 struct got_tree_entry *te)
5150 const struct got_error *err = NULL;
5151 struct tog_parent_tree *pt;
5152 size_t len = 2; /* for leading slash and NUL */
5154 TAILQ_FOREACH(pt, parents, entry)
5155 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5156 + 1 /* slash */;
5157 if (te)
5158 len += strlen(got_tree_entry_get_name(te));
5160 *path = calloc(1, len);
5161 if (path == NULL)
5162 return got_error_from_errno("calloc");
5164 (*path)[0] = '/';
5165 pt = TAILQ_LAST(parents, tog_parent_trees);
5166 while (pt) {
5167 const char *name = got_tree_entry_get_name(pt->selected_entry);
5168 if (strlcat(*path, name, len) >= len) {
5169 err = got_error(GOT_ERR_NO_SPACE);
5170 goto done;
5172 if (strlcat(*path, "/", len) >= len) {
5173 err = got_error(GOT_ERR_NO_SPACE);
5174 goto done;
5176 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5178 if (te) {
5179 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5180 err = got_error(GOT_ERR_NO_SPACE);
5181 goto done;
5184 done:
5185 if (err) {
5186 free(*path);
5187 *path = NULL;
5189 return err;
5192 static const struct got_error *
5193 blame_tree_entry(struct tog_view **new_view, int begin_x,
5194 struct got_tree_entry *te, struct tog_parent_trees *parents,
5195 struct got_object_id *commit_id, struct got_repository *repo)
5197 const struct got_error *err = NULL;
5198 char *path;
5199 struct tog_view *blame_view;
5201 *new_view = NULL;
5203 err = tree_entry_path(&path, parents, te);
5204 if (err)
5205 return err;
5207 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5208 if (blame_view == NULL) {
5209 err = got_error_from_errno("view_open");
5210 goto done;
5213 err = open_blame_view(blame_view, path, commit_id, repo);
5214 if (err) {
5215 if (err->code == GOT_ERR_CANCELLED)
5216 err = NULL;
5217 view_close(blame_view);
5218 } else
5219 *new_view = blame_view;
5220 done:
5221 free(path);
5222 return err;
5225 static const struct got_error *
5226 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5227 struct tog_tree_view_state *s)
5229 struct tog_view *log_view;
5230 const struct got_error *err = NULL;
5231 char *path;
5233 *new_view = NULL;
5235 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5236 if (log_view == NULL)
5237 return got_error_from_errno("view_open");
5239 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5240 if (err)
5241 return err;
5243 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5244 path, 0);
5245 if (err)
5246 view_close(log_view);
5247 else
5248 *new_view = log_view;
5249 free(path);
5250 return err;
5253 static const struct got_error *
5254 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5255 const char *head_ref_name, struct got_repository *repo)
5257 const struct got_error *err = NULL;
5258 char *commit_id_str = NULL;
5259 struct tog_tree_view_state *s = &view->state.tree;
5260 struct got_commit_object *commit = NULL;
5262 TAILQ_INIT(&s->parents);
5263 STAILQ_INIT(&s->colors);
5265 s->commit_id = got_object_id_dup(commit_id);
5266 if (s->commit_id == NULL)
5267 return got_error_from_errno("got_object_id_dup");
5269 err = got_object_open_as_commit(&commit, repo, commit_id);
5270 if (err)
5271 goto done;
5274 * The root is opened here and will be closed when the view is closed.
5275 * Any visited subtrees and their path-wise parents are opened and
5276 * closed on demand.
5278 err = got_object_open_as_tree(&s->root, repo,
5279 got_object_commit_get_tree_id(commit));
5280 if (err)
5281 goto done;
5282 s->tree = s->root;
5284 err = got_object_id_str(&commit_id_str, commit_id);
5285 if (err != NULL)
5286 goto done;
5288 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5289 err = got_error_from_errno("asprintf");
5290 goto done;
5293 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5294 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5295 if (head_ref_name) {
5296 s->head_ref_name = strdup(head_ref_name);
5297 if (s->head_ref_name == NULL) {
5298 err = got_error_from_errno("strdup");
5299 goto done;
5302 s->repo = repo;
5304 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5305 err = add_color(&s->colors, "\\$$",
5306 TOG_COLOR_TREE_SUBMODULE,
5307 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5308 if (err)
5309 goto done;
5310 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5311 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5312 if (err)
5313 goto done;
5314 err = add_color(&s->colors, "/$",
5315 TOG_COLOR_TREE_DIRECTORY,
5316 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5317 if (err)
5318 goto done;
5320 err = add_color(&s->colors, "\\*$",
5321 TOG_COLOR_TREE_EXECUTABLE,
5322 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5323 if (err)
5324 goto done;
5326 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5327 get_color_value("TOG_COLOR_COMMIT"));
5328 if (err)
5329 goto done;
5332 view->show = show_tree_view;
5333 view->input = input_tree_view;
5334 view->close = close_tree_view;
5335 view->search_start = search_start_tree_view;
5336 view->search_next = search_next_tree_view;
5337 done:
5338 free(commit_id_str);
5339 if (commit)
5340 got_object_commit_close(commit);
5341 if (err)
5342 close_tree_view(view);
5343 return err;
5346 static const struct got_error *
5347 close_tree_view(struct tog_view *view)
5349 struct tog_tree_view_state *s = &view->state.tree;
5351 free_colors(&s->colors);
5352 free(s->tree_label);
5353 s->tree_label = NULL;
5354 free(s->commit_id);
5355 s->commit_id = NULL;
5356 free(s->head_ref_name);
5357 s->head_ref_name = NULL;
5358 while (!TAILQ_EMPTY(&s->parents)) {
5359 struct tog_parent_tree *parent;
5360 parent = TAILQ_FIRST(&s->parents);
5361 TAILQ_REMOVE(&s->parents, parent, entry);
5362 if (parent->tree != s->root)
5363 got_object_tree_close(parent->tree);
5364 free(parent);
5367 if (s->tree != NULL && s->tree != s->root)
5368 got_object_tree_close(s->tree);
5369 if (s->root)
5370 got_object_tree_close(s->root);
5371 return NULL;
5374 static const struct got_error *
5375 search_start_tree_view(struct tog_view *view)
5377 struct tog_tree_view_state *s = &view->state.tree;
5379 s->matched_entry = NULL;
5380 return NULL;
5383 static int
5384 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5386 regmatch_t regmatch;
5388 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5389 0) == 0;
5392 static const struct got_error *
5393 search_next_tree_view(struct tog_view *view)
5395 struct tog_tree_view_state *s = &view->state.tree;
5396 struct got_tree_entry *te = NULL;
5398 if (!view->searching) {
5399 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5400 return NULL;
5403 if (s->matched_entry) {
5404 if (view->searching == TOG_SEARCH_FORWARD) {
5405 if (s->selected_entry)
5406 te = got_tree_entry_get_next(s->tree,
5407 s->selected_entry);
5408 else
5409 te = got_object_tree_get_first_entry(s->tree);
5410 } else {
5411 if (s->selected_entry == NULL)
5412 te = got_object_tree_get_last_entry(s->tree);
5413 else
5414 te = got_tree_entry_get_prev(s->tree,
5415 s->selected_entry);
5417 } else {
5418 if (s->selected_entry)
5419 te = s->selected_entry;
5420 else if (view->searching == TOG_SEARCH_FORWARD)
5421 te = got_object_tree_get_first_entry(s->tree);
5422 else
5423 te = got_object_tree_get_last_entry(s->tree);
5426 while (1) {
5427 if (te == NULL) {
5428 if (s->matched_entry == NULL) {
5429 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5430 return NULL;
5432 if (view->searching == TOG_SEARCH_FORWARD)
5433 te = got_object_tree_get_first_entry(s->tree);
5434 else
5435 te = got_object_tree_get_last_entry(s->tree);
5438 if (match_tree_entry(te, &view->regex)) {
5439 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5440 s->matched_entry = te;
5441 break;
5444 if (view->searching == TOG_SEARCH_FORWARD)
5445 te = got_tree_entry_get_next(s->tree, te);
5446 else
5447 te = got_tree_entry_get_prev(s->tree, te);
5450 if (s->matched_entry) {
5451 s->first_displayed_entry = s->matched_entry;
5452 s->selected = 0;
5455 return NULL;
5458 static const struct got_error *
5459 show_tree_view(struct tog_view *view)
5461 const struct got_error *err = NULL;
5462 struct tog_tree_view_state *s = &view->state.tree;
5463 char *parent_path;
5465 err = tree_entry_path(&parent_path, &s->parents, NULL);
5466 if (err)
5467 return err;
5469 err = draw_tree_entries(view, parent_path);
5470 free(parent_path);
5472 view_vborder(view);
5473 return err;
5476 static const struct got_error *
5477 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5479 const struct got_error *err = NULL;
5480 struct tog_tree_view_state *s = &view->state.tree;
5481 struct tog_view *log_view, *ref_view;
5482 struct got_tree_entry *te;
5483 int begin_x = 0, n;
5485 switch (ch) {
5486 case 'i':
5487 s->show_ids = !s->show_ids;
5488 break;
5489 case 'l':
5490 if (!s->selected_entry)
5491 break;
5492 if (view_is_parent_view(view))
5493 begin_x = view_split_begin_x(view->begin_x);
5494 err = log_selected_tree_entry(&log_view, begin_x, s);
5495 view->focussed = 0;
5496 log_view->focussed = 1;
5497 if (view_is_parent_view(view)) {
5498 err = view_close_child(view);
5499 if (err)
5500 return err;
5501 view_set_child(view, log_view);
5502 view->focus_child = 1;
5503 } else
5504 *new_view = log_view;
5505 break;
5506 case 'r':
5507 if (view_is_parent_view(view))
5508 begin_x = view_split_begin_x(view->begin_x);
5509 ref_view = view_open(view->nlines, view->ncols,
5510 view->begin_y, begin_x, TOG_VIEW_REF);
5511 if (ref_view == NULL)
5512 return got_error_from_errno("view_open");
5513 err = open_ref_view(ref_view, s->repo);
5514 if (err) {
5515 view_close(ref_view);
5516 return err;
5518 view->focussed = 0;
5519 ref_view->focussed = 1;
5520 if (view_is_parent_view(view)) {
5521 err = view_close_child(view);
5522 if (err)
5523 return err;
5524 view_set_child(view, ref_view);
5525 view->focus_child = 1;
5526 } else
5527 *new_view = ref_view;
5528 break;
5529 case 'g':
5530 case KEY_HOME:
5531 s->selected = 0;
5532 if (s->tree == s->root)
5533 s->first_displayed_entry =
5534 got_object_tree_get_first_entry(s->tree);
5535 else
5536 s->first_displayed_entry = NULL;
5537 break;
5538 case 'G':
5539 case KEY_END:
5540 s->selected = 0;
5541 te = got_object_tree_get_last_entry(s->tree);
5542 for (n = 0; n < view->nlines - 3; n++) {
5543 if (te == NULL) {
5544 if(s->tree != s->root) {
5545 s->first_displayed_entry = NULL;
5546 n++;
5548 break;
5550 s->first_displayed_entry = te;
5551 te = got_tree_entry_get_prev(s->tree, te);
5553 if (n > 0)
5554 s->selected = n - 1;
5555 break;
5556 case 'k':
5557 case KEY_UP:
5558 case CTRL('p'):
5559 if (s->selected > 0) {
5560 s->selected--;
5561 break;
5563 tree_scroll_up(s, 1);
5564 break;
5565 case KEY_PPAGE:
5566 case CTRL('b'):
5567 if (s->tree == s->root) {
5568 if (got_object_tree_get_first_entry(s->tree) ==
5569 s->first_displayed_entry)
5570 s->selected = 0;
5571 } else {
5572 if (s->first_displayed_entry == NULL)
5573 s->selected = 0;
5575 tree_scroll_up(s, MAX(0, view->nlines - 3));
5576 break;
5577 case 'j':
5578 case KEY_DOWN:
5579 case CTRL('n'):
5580 if (s->selected < s->ndisplayed - 1) {
5581 s->selected++;
5582 break;
5584 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5585 == NULL)
5586 /* can't scroll any further */
5587 break;
5588 tree_scroll_down(s, 1);
5589 break;
5590 case KEY_NPAGE:
5591 case CTRL('f'):
5592 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5593 == NULL) {
5594 /* can't scroll any further; move cursor down */
5595 if (s->selected < s->ndisplayed - 1)
5596 s->selected = s->ndisplayed - 1;
5597 break;
5599 tree_scroll_down(s, view->nlines - 3);
5600 break;
5601 case KEY_ENTER:
5602 case '\r':
5603 case KEY_BACKSPACE:
5604 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5605 struct tog_parent_tree *parent;
5606 /* user selected '..' */
5607 if (s->tree == s->root)
5608 break;
5609 parent = TAILQ_FIRST(&s->parents);
5610 TAILQ_REMOVE(&s->parents, parent,
5611 entry);
5612 got_object_tree_close(s->tree);
5613 s->tree = parent->tree;
5614 s->first_displayed_entry =
5615 parent->first_displayed_entry;
5616 s->selected_entry =
5617 parent->selected_entry;
5618 s->selected = parent->selected;
5619 free(parent);
5620 } else if (S_ISDIR(got_tree_entry_get_mode(
5621 s->selected_entry))) {
5622 struct got_tree_object *subtree;
5623 err = got_object_open_as_tree(&subtree, s->repo,
5624 got_tree_entry_get_id(s->selected_entry));
5625 if (err)
5626 break;
5627 err = tree_view_visit_subtree(s, subtree);
5628 if (err) {
5629 got_object_tree_close(subtree);
5630 break;
5632 } else if (S_ISREG(got_tree_entry_get_mode(
5633 s->selected_entry))) {
5634 struct tog_view *blame_view;
5635 int begin_x = view_is_parent_view(view) ?
5636 view_split_begin_x(view->begin_x) : 0;
5638 err = blame_tree_entry(&blame_view, begin_x,
5639 s->selected_entry, &s->parents,
5640 s->commit_id, s->repo);
5641 if (err)
5642 break;
5643 view->focussed = 0;
5644 blame_view->focussed = 1;
5645 if (view_is_parent_view(view)) {
5646 err = view_close_child(view);
5647 if (err)
5648 return err;
5649 view_set_child(view, blame_view);
5650 view->focus_child = 1;
5651 } else
5652 *new_view = blame_view;
5654 break;
5655 case KEY_RESIZE:
5656 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5657 s->selected = view->nlines - 4;
5658 break;
5659 default:
5660 break;
5663 return err;
5666 __dead static void
5667 usage_tree(void)
5669 endwin();
5670 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5671 getprogname());
5672 exit(1);
5675 static const struct got_error *
5676 cmd_tree(int argc, char *argv[])
5678 const struct got_error *error;
5679 struct got_repository *repo = NULL;
5680 struct got_worktree *worktree = NULL;
5681 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5682 struct got_object_id *commit_id = NULL;
5683 struct got_commit_object *commit = NULL;
5684 const char *commit_id_arg = NULL;
5685 char *label = NULL;
5686 struct got_reference *ref = NULL;
5687 const char *head_ref_name = NULL;
5688 int ch;
5689 struct tog_view *view;
5691 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5692 switch (ch) {
5693 case 'c':
5694 commit_id_arg = optarg;
5695 break;
5696 case 'r':
5697 repo_path = realpath(optarg, NULL);
5698 if (repo_path == NULL)
5699 return got_error_from_errno2("realpath",
5700 optarg);
5701 break;
5702 default:
5703 usage_tree();
5704 /* NOTREACHED */
5708 argc -= optind;
5709 argv += optind;
5711 if (argc > 1)
5712 usage_tree();
5714 if (repo_path == NULL) {
5715 cwd = getcwd(NULL, 0);
5716 if (cwd == NULL)
5717 return got_error_from_errno("getcwd");
5718 error = got_worktree_open(&worktree, cwd);
5719 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5720 goto done;
5721 if (worktree)
5722 repo_path =
5723 strdup(got_worktree_get_repo_path(worktree));
5724 else
5725 repo_path = strdup(cwd);
5726 if (repo_path == NULL) {
5727 error = got_error_from_errno("strdup");
5728 goto done;
5732 error = got_repo_open(&repo, repo_path, NULL);
5733 if (error != NULL)
5734 goto done;
5736 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5737 repo, worktree);
5738 if (error)
5739 goto done;
5741 init_curses();
5743 error = apply_unveil(got_repo_get_path(repo), NULL);
5744 if (error)
5745 goto done;
5747 error = tog_load_refs(repo, 0);
5748 if (error)
5749 goto done;
5751 if (commit_id_arg == NULL) {
5752 error = got_repo_match_object_id(&commit_id, &label,
5753 worktree ? got_worktree_get_head_ref_name(worktree) :
5754 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5755 if (error)
5756 goto done;
5757 head_ref_name = label;
5758 } else {
5759 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5760 if (error == NULL)
5761 head_ref_name = got_ref_get_name(ref);
5762 else if (error->code != GOT_ERR_NOT_REF)
5763 goto done;
5764 error = got_repo_match_object_id(&commit_id, NULL,
5765 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5766 if (error)
5767 goto done;
5770 error = got_object_open_as_commit(&commit, repo, commit_id);
5771 if (error)
5772 goto done;
5774 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5775 if (view == NULL) {
5776 error = got_error_from_errno("view_open");
5777 goto done;
5779 error = open_tree_view(view, commit_id, head_ref_name, repo);
5780 if (error)
5781 goto done;
5782 if (!got_path_is_root_dir(in_repo_path)) {
5783 error = tree_view_walk_path(&view->state.tree, commit,
5784 in_repo_path);
5785 if (error)
5786 goto done;
5789 if (worktree) {
5790 /* Release work tree lock. */
5791 got_worktree_close(worktree);
5792 worktree = NULL;
5794 error = view_loop(view);
5795 done:
5796 free(repo_path);
5797 free(cwd);
5798 free(commit_id);
5799 free(label);
5800 if (ref)
5801 got_ref_close(ref);
5802 if (repo) {
5803 const struct got_error *close_err = got_repo_close(repo);
5804 if (error == NULL)
5805 error = close_err;
5807 tog_free_refs();
5808 return error;
5811 static const struct got_error *
5812 ref_view_load_refs(struct tog_ref_view_state *s)
5814 struct got_reflist_entry *sre;
5815 struct tog_reflist_entry *re;
5817 s->nrefs = 0;
5818 TAILQ_FOREACH(sre, &tog_refs, entry) {
5819 if (strncmp(got_ref_get_name(sre->ref),
5820 "refs/got/", 9) == 0 &&
5821 strncmp(got_ref_get_name(sre->ref),
5822 "refs/got/backup/", 16) != 0)
5823 continue;
5825 re = malloc(sizeof(*re));
5826 if (re == NULL)
5827 return got_error_from_errno("malloc");
5829 re->ref = got_ref_dup(sre->ref);
5830 if (re->ref == NULL)
5831 return got_error_from_errno("got_ref_dup");
5832 re->idx = s->nrefs++;
5833 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5836 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5837 return NULL;
5840 void
5841 ref_view_free_refs(struct tog_ref_view_state *s)
5843 struct tog_reflist_entry *re;
5845 while (!TAILQ_EMPTY(&s->refs)) {
5846 re = TAILQ_FIRST(&s->refs);
5847 TAILQ_REMOVE(&s->refs, re, entry);
5848 got_ref_close(re->ref);
5849 free(re);
5853 static const struct got_error *
5854 open_ref_view(struct tog_view *view, struct got_repository *repo)
5856 const struct got_error *err = NULL;
5857 struct tog_ref_view_state *s = &view->state.ref;
5859 s->selected_entry = 0;
5860 s->repo = repo;
5862 TAILQ_INIT(&s->refs);
5863 STAILQ_INIT(&s->colors);
5865 err = ref_view_load_refs(s);
5866 if (err)
5867 return err;
5869 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5870 err = add_color(&s->colors, "^refs/heads/",
5871 TOG_COLOR_REFS_HEADS,
5872 get_color_value("TOG_COLOR_REFS_HEADS"));
5873 if (err)
5874 goto done;
5876 err = add_color(&s->colors, "^refs/tags/",
5877 TOG_COLOR_REFS_TAGS,
5878 get_color_value("TOG_COLOR_REFS_TAGS"));
5879 if (err)
5880 goto done;
5882 err = add_color(&s->colors, "^refs/remotes/",
5883 TOG_COLOR_REFS_REMOTES,
5884 get_color_value("TOG_COLOR_REFS_REMOTES"));
5885 if (err)
5886 goto done;
5888 err = add_color(&s->colors, "^refs/got/backup/",
5889 TOG_COLOR_REFS_BACKUP,
5890 get_color_value("TOG_COLOR_REFS_BACKUP"));
5891 if (err)
5892 goto done;
5895 view->show = show_ref_view;
5896 view->input = input_ref_view;
5897 view->close = close_ref_view;
5898 view->search_start = search_start_ref_view;
5899 view->search_next = search_next_ref_view;
5900 done:
5901 if (err)
5902 free_colors(&s->colors);
5903 return err;
5906 static const struct got_error *
5907 close_ref_view(struct tog_view *view)
5909 struct tog_ref_view_state *s = &view->state.ref;
5911 ref_view_free_refs(s);
5912 free_colors(&s->colors);
5914 return NULL;
5917 static const struct got_error *
5918 resolve_reflist_entry(struct got_object_id **commit_id,
5919 struct tog_reflist_entry *re, struct got_repository *repo)
5921 const struct got_error *err = NULL;
5922 struct got_object_id *obj_id;
5923 struct got_tag_object *tag = NULL;
5924 int obj_type;
5926 *commit_id = NULL;
5928 err = got_ref_resolve(&obj_id, repo, re->ref);
5929 if (err)
5930 return err;
5932 err = got_object_get_type(&obj_type, repo, obj_id);
5933 if (err)
5934 goto done;
5936 switch (obj_type) {
5937 case GOT_OBJ_TYPE_COMMIT:
5938 *commit_id = obj_id;
5939 break;
5940 case GOT_OBJ_TYPE_TAG:
5941 err = got_object_open_as_tag(&tag, repo, obj_id);
5942 if (err)
5943 goto done;
5944 free(obj_id);
5945 err = got_object_get_type(&obj_type, repo,
5946 got_object_tag_get_object_id(tag));
5947 if (err)
5948 goto done;
5949 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5950 err = got_error(GOT_ERR_OBJ_TYPE);
5951 goto done;
5953 *commit_id = got_object_id_dup(
5954 got_object_tag_get_object_id(tag));
5955 if (*commit_id == NULL) {
5956 err = got_error_from_errno("got_object_id_dup");
5957 goto done;
5959 break;
5960 default:
5961 err = got_error(GOT_ERR_OBJ_TYPE);
5962 break;
5965 done:
5966 if (tag)
5967 got_object_tag_close(tag);
5968 if (err) {
5969 free(*commit_id);
5970 *commit_id = NULL;
5972 return err;
5975 static const struct got_error *
5976 log_ref_entry(struct tog_view **new_view, int begin_x,
5977 struct tog_reflist_entry *re, struct got_repository *repo)
5979 struct tog_view *log_view;
5980 const struct got_error *err = NULL;
5981 struct got_object_id *commit_id = NULL;
5983 *new_view = NULL;
5985 err = resolve_reflist_entry(&commit_id, re, repo);
5986 if (err) {
5987 if (err->code != GOT_ERR_OBJ_TYPE)
5988 return err;
5989 else
5990 return NULL;
5993 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5994 if (log_view == NULL) {
5995 err = got_error_from_errno("view_open");
5996 goto done;
5999 err = open_log_view(log_view, commit_id, repo,
6000 got_ref_get_name(re->ref), "", 0);
6001 done:
6002 if (err)
6003 view_close(log_view);
6004 else
6005 *new_view = log_view;
6006 free(commit_id);
6007 return err;
6010 static void
6011 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6013 struct tog_reflist_entry *re;
6014 int i = 0;
6016 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6017 return;
6019 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6020 while (i++ < maxscroll) {
6021 if (re == NULL)
6022 break;
6023 s->first_displayed_entry = re;
6024 re = TAILQ_PREV(re, tog_reflist_head, entry);
6028 static void
6029 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6031 struct tog_reflist_entry *next, *last;
6032 int n = 0;
6034 if (s->first_displayed_entry)
6035 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6036 else
6037 next = TAILQ_FIRST(&s->refs);
6039 last = s->last_displayed_entry;
6040 while (next && last && n++ < maxscroll) {
6041 last = TAILQ_NEXT(last, entry);
6042 if (last) {
6043 s->first_displayed_entry = next;
6044 next = TAILQ_NEXT(next, entry);
6049 static const struct got_error *
6050 search_start_ref_view(struct tog_view *view)
6052 struct tog_ref_view_state *s = &view->state.ref;
6054 s->matched_entry = NULL;
6055 return NULL;
6058 static int
6059 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6061 regmatch_t regmatch;
6063 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6064 0) == 0;
6067 static const struct got_error *
6068 search_next_ref_view(struct tog_view *view)
6070 struct tog_ref_view_state *s = &view->state.ref;
6071 struct tog_reflist_entry *re = NULL;
6073 if (!view->searching) {
6074 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6075 return NULL;
6078 if (s->matched_entry) {
6079 if (view->searching == TOG_SEARCH_FORWARD) {
6080 if (s->selected_entry)
6081 re = TAILQ_NEXT(s->selected_entry, entry);
6082 else
6083 re = TAILQ_PREV(s->selected_entry,
6084 tog_reflist_head, entry);
6085 } else {
6086 if (s->selected_entry == NULL)
6087 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6088 else
6089 re = TAILQ_PREV(s->selected_entry,
6090 tog_reflist_head, entry);
6092 } else {
6093 if (s->selected_entry)
6094 re = s->selected_entry;
6095 else if (view->searching == TOG_SEARCH_FORWARD)
6096 re = TAILQ_FIRST(&s->refs);
6097 else
6098 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6101 while (1) {
6102 if (re == NULL) {
6103 if (s->matched_entry == NULL) {
6104 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6105 return NULL;
6107 if (view->searching == TOG_SEARCH_FORWARD)
6108 re = TAILQ_FIRST(&s->refs);
6109 else
6110 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6113 if (match_reflist_entry(re, &view->regex)) {
6114 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6115 s->matched_entry = re;
6116 break;
6119 if (view->searching == TOG_SEARCH_FORWARD)
6120 re = TAILQ_NEXT(re, entry);
6121 else
6122 re = TAILQ_PREV(re, tog_reflist_head, entry);
6125 if (s->matched_entry) {
6126 s->first_displayed_entry = s->matched_entry;
6127 s->selected = 0;
6130 return NULL;
6133 static const struct got_error *
6134 show_ref_view(struct tog_view *view)
6136 const struct got_error *err = NULL;
6137 struct tog_ref_view_state *s = &view->state.ref;
6138 struct tog_reflist_entry *re;
6139 char *line = NULL;
6140 wchar_t *wline;
6141 struct tog_color *tc;
6142 int width, n;
6143 int limit = view->nlines;
6145 werase(view->window);
6147 s->ndisplayed = 0;
6149 if (limit == 0)
6150 return NULL;
6152 re = s->first_displayed_entry;
6154 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6155 s->nrefs) == -1)
6156 return got_error_from_errno("asprintf");
6158 err = format_line(&wline, &width, line, view->ncols, 0);
6159 if (err) {
6160 free(line);
6161 return err;
6163 if (view_needs_focus_indication(view))
6164 wstandout(view->window);
6165 waddwstr(view->window, wline);
6166 if (view_needs_focus_indication(view))
6167 wstandend(view->window);
6168 free(wline);
6169 wline = NULL;
6170 free(line);
6171 line = NULL;
6172 if (width < view->ncols - 1)
6173 waddch(view->window, '\n');
6174 if (--limit <= 0)
6175 return NULL;
6177 n = 0;
6178 while (re && limit > 0) {
6179 char *line = NULL;
6181 if (got_ref_is_symbolic(re->ref)) {
6182 if (asprintf(&line, "%s -> %s",
6183 got_ref_get_name(re->ref),
6184 got_ref_get_symref_target(re->ref)) == -1)
6185 return got_error_from_errno("asprintf");
6186 } else if (s->show_ids) {
6187 struct got_object_id *id;
6188 char *id_str;
6189 err = got_ref_resolve(&id, s->repo, re->ref);
6190 if (err)
6191 return err;
6192 err = got_object_id_str(&id_str, id);
6193 if (err) {
6194 free(id);
6195 return err;
6197 if (asprintf(&line, "%s: %s",
6198 got_ref_get_name(re->ref), id_str) == -1) {
6199 err = got_error_from_errno("asprintf");
6200 free(id);
6201 free(id_str);
6202 return err;
6204 free(id);
6205 free(id_str);
6206 } else {
6207 line = strdup(got_ref_get_name(re->ref));
6208 if (line == NULL)
6209 return got_error_from_errno("strdup");
6212 err = format_line(&wline, &width, line, view->ncols, 0);
6213 if (err) {
6214 free(line);
6215 return err;
6217 if (n == s->selected) {
6218 if (view->focussed)
6219 wstandout(view->window);
6220 s->selected_entry = re;
6222 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6223 if (tc)
6224 wattr_on(view->window,
6225 COLOR_PAIR(tc->colorpair), NULL);
6226 waddwstr(view->window, wline);
6227 if (tc)
6228 wattr_off(view->window,
6229 COLOR_PAIR(tc->colorpair), NULL);
6230 if (width < view->ncols - 1)
6231 waddch(view->window, '\n');
6232 if (n == s->selected && view->focussed)
6233 wstandend(view->window);
6234 free(line);
6235 free(wline);
6236 wline = NULL;
6237 n++;
6238 s->ndisplayed++;
6239 s->last_displayed_entry = re;
6241 limit--;
6242 re = TAILQ_NEXT(re, entry);
6245 view_vborder(view);
6246 return err;
6249 static const struct got_error *
6250 browse_ref_tree(struct tog_view **new_view, int begin_x,
6251 struct tog_reflist_entry *re, struct got_repository *repo)
6253 const struct got_error *err = NULL;
6254 struct got_object_id *commit_id = NULL;
6255 struct tog_view *tree_view;
6257 *new_view = NULL;
6259 err = resolve_reflist_entry(&commit_id, re, repo);
6260 if (err) {
6261 if (err->code != GOT_ERR_OBJ_TYPE)
6262 return err;
6263 else
6264 return NULL;
6268 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6269 if (tree_view == NULL) {
6270 err = got_error_from_errno("view_open");
6271 goto done;
6274 err = open_tree_view(tree_view, commit_id,
6275 got_ref_get_name(re->ref), repo);
6276 if (err)
6277 goto done;
6279 *new_view = tree_view;
6280 done:
6281 free(commit_id);
6282 return err;
6284 static const struct got_error *
6285 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6287 const struct got_error *err = NULL;
6288 struct tog_ref_view_state *s = &view->state.ref;
6289 struct tog_view *log_view, *tree_view;
6290 struct tog_reflist_entry *re;
6291 int begin_x = 0, n;
6293 switch (ch) {
6294 case 'i':
6295 s->show_ids = !s->show_ids;
6296 break;
6297 case 'o':
6298 s->sort_by_date = !s->sort_by_date;
6299 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6300 got_ref_cmp_by_commit_timestamp_descending :
6301 tog_ref_cmp_by_name, s->repo);
6302 if (err)
6303 break;
6304 got_reflist_object_id_map_free(tog_refs_idmap);
6305 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6306 &tog_refs, s->repo);
6307 if (err)
6308 break;
6309 ref_view_free_refs(s);
6310 err = ref_view_load_refs(s);
6311 break;
6312 case KEY_ENTER:
6313 case '\r':
6314 if (!s->selected_entry)
6315 break;
6316 if (view_is_parent_view(view))
6317 begin_x = view_split_begin_x(view->begin_x);
6318 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6319 s->repo);
6320 view->focussed = 0;
6321 log_view->focussed = 1;
6322 if (view_is_parent_view(view)) {
6323 err = view_close_child(view);
6324 if (err)
6325 return err;
6326 view_set_child(view, log_view);
6327 view->focus_child = 1;
6328 } else
6329 *new_view = log_view;
6330 break;
6331 case 't':
6332 if (!s->selected_entry)
6333 break;
6334 if (view_is_parent_view(view))
6335 begin_x = view_split_begin_x(view->begin_x);
6336 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6337 s->repo);
6338 if (err || tree_view == NULL)
6339 break;
6340 view->focussed = 0;
6341 tree_view->focussed = 1;
6342 if (view_is_parent_view(view)) {
6343 err = view_close_child(view);
6344 if (err)
6345 return err;
6346 view_set_child(view, tree_view);
6347 view->focus_child = 1;
6348 } else
6349 *new_view = tree_view;
6350 break;
6351 case 'g':
6352 case KEY_HOME:
6353 s->selected = 0;
6354 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6355 break;
6356 case 'G':
6357 case KEY_END:
6358 s->selected = 0;
6359 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6360 for (n = 0; n < view->nlines - 1; n++) {
6361 if (re == NULL)
6362 break;
6363 s->first_displayed_entry = re;
6364 re = TAILQ_PREV(re, tog_reflist_head, entry);
6366 if (n > 0)
6367 s->selected = n - 1;
6368 break;
6369 case 'k':
6370 case KEY_UP:
6371 case CTRL('p'):
6372 if (s->selected > 0) {
6373 s->selected--;
6374 break;
6376 ref_scroll_up(s, 1);
6377 break;
6378 case KEY_PPAGE:
6379 case CTRL('b'):
6380 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6381 s->selected = 0;
6382 ref_scroll_up(s, MAX(0, view->nlines - 1));
6383 break;
6384 case 'j':
6385 case KEY_DOWN:
6386 case CTRL('n'):
6387 if (s->selected < s->ndisplayed - 1) {
6388 s->selected++;
6389 break;
6391 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6392 /* can't scroll any further */
6393 break;
6394 ref_scroll_down(s, 1);
6395 break;
6396 case KEY_NPAGE:
6397 case CTRL('f'):
6398 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6399 /* can't scroll any further; move cursor down */
6400 if (s->selected < s->ndisplayed - 1)
6401 s->selected = s->ndisplayed - 1;
6402 break;
6404 ref_scroll_down(s, view->nlines - 1);
6405 break;
6406 case CTRL('l'):
6407 tog_free_refs();
6408 err = tog_load_refs(s->repo, s->sort_by_date);
6409 if (err)
6410 break;
6411 ref_view_free_refs(s);
6412 err = ref_view_load_refs(s);
6413 break;
6414 case KEY_RESIZE:
6415 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6416 s->selected = view->nlines - 2;
6417 break;
6418 default:
6419 break;
6422 return err;
6425 __dead static void
6426 usage_ref(void)
6428 endwin();
6429 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6430 getprogname());
6431 exit(1);
6434 static const struct got_error *
6435 cmd_ref(int argc, char *argv[])
6437 const struct got_error *error;
6438 struct got_repository *repo = NULL;
6439 struct got_worktree *worktree = NULL;
6440 char *cwd = NULL, *repo_path = NULL;
6441 int ch;
6442 struct tog_view *view;
6444 while ((ch = getopt(argc, argv, "r:")) != -1) {
6445 switch (ch) {
6446 case 'r':
6447 repo_path = realpath(optarg, NULL);
6448 if (repo_path == NULL)
6449 return got_error_from_errno2("realpath",
6450 optarg);
6451 break;
6452 default:
6453 usage_ref();
6454 /* NOTREACHED */
6458 argc -= optind;
6459 argv += optind;
6461 if (argc > 1)
6462 usage_ref();
6464 if (repo_path == NULL) {
6465 cwd = getcwd(NULL, 0);
6466 if (cwd == NULL)
6467 return got_error_from_errno("getcwd");
6468 error = got_worktree_open(&worktree, cwd);
6469 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6470 goto done;
6471 if (worktree)
6472 repo_path =
6473 strdup(got_worktree_get_repo_path(worktree));
6474 else
6475 repo_path = strdup(cwd);
6476 if (repo_path == NULL) {
6477 error = got_error_from_errno("strdup");
6478 goto done;
6482 error = got_repo_open(&repo, repo_path, NULL);
6483 if (error != NULL)
6484 goto done;
6486 init_curses();
6488 error = apply_unveil(got_repo_get_path(repo), NULL);
6489 if (error)
6490 goto done;
6492 error = tog_load_refs(repo, 0);
6493 if (error)
6494 goto done;
6496 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6497 if (view == NULL) {
6498 error = got_error_from_errno("view_open");
6499 goto done;
6502 error = open_ref_view(view, repo);
6503 if (error)
6504 goto done;
6506 if (worktree) {
6507 /* Release work tree lock. */
6508 got_worktree_close(worktree);
6509 worktree = NULL;
6511 error = view_loop(view);
6512 done:
6513 free(repo_path);
6514 free(cwd);
6515 if (repo) {
6516 const struct got_error *close_err = got_repo_close(repo);
6517 if (close_err)
6518 error = close_err;
6520 tog_free_refs();
6521 return error;
6524 static void
6525 list_commands(FILE *fp)
6527 size_t i;
6529 fprintf(fp, "commands:");
6530 for (i = 0; i < nitems(tog_commands); i++) {
6531 const struct tog_cmd *cmd = &tog_commands[i];
6532 fprintf(fp, " %s", cmd->name);
6534 fputc('\n', fp);
6537 __dead static void
6538 usage(int hflag, int status)
6540 FILE *fp = (status == 0) ? stdout : stderr;
6542 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6543 getprogname());
6544 if (hflag) {
6545 fprintf(fp, "lazy usage: %s path\n", getprogname());
6546 list_commands(fp);
6548 exit(status);
6551 static char **
6552 make_argv(int argc, ...)
6554 va_list ap;
6555 char **argv;
6556 int i;
6558 va_start(ap, argc);
6560 argv = calloc(argc, sizeof(char *));
6561 if (argv == NULL)
6562 err(1, "calloc");
6563 for (i = 0; i < argc; i++) {
6564 argv[i] = strdup(va_arg(ap, char *));
6565 if (argv[i] == NULL)
6566 err(1, "strdup");
6569 va_end(ap);
6570 return argv;
6574 * Try to convert 'tog path' into a 'tog log path' command.
6575 * The user could simply have mistyped the command rather than knowingly
6576 * provided a path. So check whether argv[0] can in fact be resolved
6577 * to a path in the HEAD commit and print a special error if not.
6578 * This hack is for mpi@ <3
6580 static const struct got_error *
6581 tog_log_with_path(int argc, char *argv[])
6583 const struct got_error *error = NULL, *close_err;
6584 const struct tog_cmd *cmd = NULL;
6585 struct got_repository *repo = NULL;
6586 struct got_worktree *worktree = NULL;
6587 struct got_object_id *commit_id = NULL, *id = NULL;
6588 struct got_commit_object *commit = NULL;
6589 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6590 char *commit_id_str = NULL, **cmd_argv = NULL;
6592 cwd = getcwd(NULL, 0);
6593 if (cwd == NULL)
6594 return got_error_from_errno("getcwd");
6596 error = got_worktree_open(&worktree, cwd);
6597 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6598 goto done;
6600 if (worktree)
6601 repo_path = strdup(got_worktree_get_repo_path(worktree));
6602 else
6603 repo_path = strdup(cwd);
6604 if (repo_path == NULL) {
6605 error = got_error_from_errno("strdup");
6606 goto done;
6609 error = got_repo_open(&repo, repo_path, NULL);
6610 if (error != NULL)
6611 goto done;
6613 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6614 repo, worktree);
6615 if (error)
6616 goto done;
6618 error = tog_load_refs(repo, 0);
6619 if (error)
6620 goto done;
6621 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6622 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6623 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6624 if (error)
6625 goto done;
6627 if (worktree) {
6628 got_worktree_close(worktree);
6629 worktree = NULL;
6632 error = got_object_open_as_commit(&commit, repo, commit_id);
6633 if (error)
6634 goto done;
6636 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6637 if (error) {
6638 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6639 goto done;
6640 fprintf(stderr, "%s: '%s' is no known command or path\n",
6641 getprogname(), argv[0]);
6642 usage(1, 1);
6643 /* not reached */
6646 close_err = got_repo_close(repo);
6647 if (error == NULL)
6648 error = close_err;
6649 repo = NULL;
6651 error = got_object_id_str(&commit_id_str, commit_id);
6652 if (error)
6653 goto done;
6655 cmd = &tog_commands[0]; /* log */
6656 argc = 4;
6657 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6658 error = cmd->cmd_main(argc, cmd_argv);
6659 done:
6660 if (repo) {
6661 close_err = got_repo_close(repo);
6662 if (error == NULL)
6663 error = close_err;
6665 if (commit)
6666 got_object_commit_close(commit);
6667 if (worktree)
6668 got_worktree_close(worktree);
6669 free(id);
6670 free(commit_id_str);
6671 free(commit_id);
6672 free(cwd);
6673 free(repo_path);
6674 free(in_repo_path);
6675 if (cmd_argv) {
6676 int i;
6677 for (i = 0; i < argc; i++)
6678 free(cmd_argv[i]);
6679 free(cmd_argv);
6681 tog_free_refs();
6682 return error;
6685 int
6686 main(int argc, char *argv[])
6688 const struct got_error *error = NULL;
6689 const struct tog_cmd *cmd = NULL;
6690 int ch, hflag = 0, Vflag = 0;
6691 char **cmd_argv = NULL;
6692 static const struct option longopts[] = {
6693 { "version", no_argument, NULL, 'V' },
6694 { NULL, 0, NULL, 0}
6697 setlocale(LC_CTYPE, "");
6699 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6700 switch (ch) {
6701 case 'h':
6702 hflag = 1;
6703 break;
6704 case 'V':
6705 Vflag = 1;
6706 break;
6707 default:
6708 usage(hflag, 1);
6709 /* NOTREACHED */
6713 argc -= optind;
6714 argv += optind;
6715 optind = 1;
6716 optreset = 1;
6718 if (Vflag) {
6719 got_version_print_str();
6720 return 0;
6723 #ifndef PROFILE
6724 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6725 NULL) == -1)
6726 err(1, "pledge");
6727 #endif
6729 if (argc == 0) {
6730 if (hflag)
6731 usage(hflag, 0);
6732 /* Build an argument vector which runs a default command. */
6733 cmd = &tog_commands[0];
6734 argc = 1;
6735 cmd_argv = make_argv(argc, cmd->name);
6736 } else {
6737 size_t i;
6739 /* Did the user specify a command? */
6740 for (i = 0; i < nitems(tog_commands); i++) {
6741 if (strncmp(tog_commands[i].name, argv[0],
6742 strlen(argv[0])) == 0) {
6743 cmd = &tog_commands[i];
6744 break;
6749 if (cmd == NULL) {
6750 if (argc != 1)
6751 usage(0, 1);
6752 /* No command specified; try log with a path */
6753 error = tog_log_with_path(argc, argv);
6754 } else {
6755 if (hflag)
6756 cmd->cmd_usage();
6757 else
6758 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6761 endwin();
6762 putchar('\n');
6763 if (cmd_argv) {
6764 int i;
6765 for (i = 0; i < argc; i++)
6766 free(cmd_argv[i]);
6767 free(cmd_argv);
6770 if (error && error->code != GOT_ERR_CANCELLED)
6771 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6772 return 0;