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 <signal.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <getopt.h>
32 #include <string.h>
33 #include <err.h>
34 #include <unistd.h>
35 #include <limits.h>
36 #include <wchar.h>
37 #include <time.h>
38 #include <pthread.h>
39 #include <libgen.h>
40 #include <regex.h>
41 #include <sched.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_diff.h"
49 #include "got_opentemp.h"
50 #include "got_utf8.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
55 #include "got_path.h"
56 #include "got_worktree.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #ifndef MAX
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 #endif
66 #define CTRL(x) ((x) & 0x1f)
68 #ifndef nitems
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #endif
72 struct tog_cmd {
73 const char *name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 };
78 __dead static void usage(int, int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
83 __dead static void usage_ref(void);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
89 static const struct got_error* cmd_ref(int, char *[]);
91 static struct tog_cmd tog_commands[] = {
92 { "log", cmd_log, usage_log },
93 { "diff", cmd_diff, usage_diff },
94 { "blame", cmd_blame, usage_blame },
95 { "tree", cmd_tree, usage_tree },
96 { "ref", cmd_ref, usage_ref },
97 };
99 enum tog_view_type {
100 TOG_VIEW_DIFF,
101 TOG_VIEW_LOG,
102 TOG_VIEW_BLAME,
103 TOG_VIEW_TREE,
104 TOG_VIEW_REF,
105 };
107 #define TOG_EOF_STRING "(END)"
109 struct commit_queue_entry {
110 TAILQ_ENTRY(commit_queue_entry) entry;
111 struct got_object_id *id;
112 struct got_commit_object *commit;
113 int idx;
114 };
115 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 struct commit_queue {
117 int ncommits;
118 struct commit_queue_head head;
119 };
121 struct tog_color {
122 STAILQ_ENTRY(tog_color) entry;
123 regex_t regex;
124 short colorpair;
125 };
126 STAILQ_HEAD(tog_colors, tog_color);
128 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
129 static struct got_reflist_object_id_map *tog_refs_idmap;
131 static const struct got_error *
132 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
133 struct got_reference* re2)
135 const char *name1 = got_ref_get_name(re1);
136 const char *name2 = got_ref_get_name(re2);
137 int isbackup1, isbackup2;
139 /* Sort backup refs towards the bottom of the list. */
140 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
141 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
142 if (!isbackup1 && isbackup2) {
143 *cmp = -1;
144 return NULL;
145 } else if (isbackup1 && !isbackup2) {
146 *cmp = 1;
147 return NULL;
150 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
151 return NULL;
154 static const struct got_error *
155 tog_load_refs(struct got_repository *repo, int sort_by_date)
157 const struct got_error *err;
159 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
160 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
161 repo);
162 if (err)
163 return err;
165 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
166 repo);
169 static void
170 tog_free_refs(void)
172 if (tog_refs_idmap) {
173 got_reflist_object_id_map_free(tog_refs_idmap);
174 tog_refs_idmap = NULL;
176 got_ref_list_free(&tog_refs);
179 static const struct got_error *
180 add_color(struct tog_colors *colors, const char *pattern,
181 int idx, short color)
183 const struct got_error *err = NULL;
184 struct tog_color *tc;
185 int regerr = 0;
187 if (idx < 1 || idx > COLOR_PAIRS - 1)
188 return NULL;
190 init_pair(idx, color, -1);
192 tc = calloc(1, sizeof(*tc));
193 if (tc == NULL)
194 return got_error_from_errno("calloc");
195 regerr = regcomp(&tc->regex, pattern,
196 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
197 if (regerr) {
198 static char regerr_msg[512];
199 static char err_msg[512];
200 regerror(regerr, &tc->regex, regerr_msg,
201 sizeof(regerr_msg));
202 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
203 regerr_msg);
204 err = got_error_msg(GOT_ERR_REGEX, err_msg);
205 free(tc);
206 return err;
208 tc->colorpair = idx;
209 STAILQ_INSERT_HEAD(colors, tc, entry);
210 return NULL;
213 static void
214 free_colors(struct tog_colors *colors)
216 struct tog_color *tc;
218 while (!STAILQ_EMPTY(colors)) {
219 tc = STAILQ_FIRST(colors);
220 STAILQ_REMOVE_HEAD(colors, entry);
221 regfree(&tc->regex);
222 free(tc);
226 struct tog_color *
227 get_color(struct tog_colors *colors, int colorpair)
229 struct tog_color *tc = NULL;
231 STAILQ_FOREACH(tc, colors, entry) {
232 if (tc->colorpair == colorpair)
233 return tc;
236 return NULL;
239 static int
240 default_color_value(const char *envvar)
242 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
243 return COLOR_MAGENTA;
244 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
245 return COLOR_CYAN;
246 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
247 return COLOR_YELLOW;
248 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
249 return COLOR_GREEN;
250 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
251 return COLOR_MAGENTA;
252 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
253 return COLOR_MAGENTA;
254 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
255 return COLOR_CYAN;
256 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
257 return COLOR_GREEN;
258 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
259 return COLOR_GREEN;
260 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
261 return COLOR_CYAN;
262 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
263 return COLOR_YELLOW;
264 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
265 return COLOR_GREEN;
266 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
267 return COLOR_MAGENTA;
268 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
269 return COLOR_YELLOW;
270 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
271 return COLOR_CYAN;
273 return -1;
276 static int
277 get_color_value(const char *envvar)
279 const char *val = getenv(envvar);
281 if (val == NULL)
282 return default_color_value(envvar);
284 if (strcasecmp(val, "black") == 0)
285 return COLOR_BLACK;
286 if (strcasecmp(val, "red") == 0)
287 return COLOR_RED;
288 if (strcasecmp(val, "green") == 0)
289 return COLOR_GREEN;
290 if (strcasecmp(val, "yellow") == 0)
291 return COLOR_YELLOW;
292 if (strcasecmp(val, "blue") == 0)
293 return COLOR_BLUE;
294 if (strcasecmp(val, "magenta") == 0)
295 return COLOR_MAGENTA;
296 if (strcasecmp(val, "cyan") == 0)
297 return COLOR_CYAN;
298 if (strcasecmp(val, "white") == 0)
299 return COLOR_WHITE;
300 if (strcasecmp(val, "default") == 0)
301 return -1;
303 return default_color_value(envvar);
307 struct tog_diff_view_state {
308 struct got_object_id *id1, *id2;
309 const char *label1, *label2;
310 FILE *f;
311 int first_displayed_line;
312 int last_displayed_line;
313 int eof;
314 int diff_context;
315 int ignore_whitespace;
316 int force_text_diff;
317 struct got_repository *repo;
318 struct tog_colors colors;
319 size_t nlines;
320 off_t *line_offsets;
321 int matched_line;
322 int selected_line;
324 /* passed from log view; may be NULL */
325 struct tog_view *log_view;
326 };
328 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
330 struct tog_log_thread_args {
331 pthread_cond_t need_commits;
332 pthread_cond_t commit_loaded;
333 int commits_needed;
334 int load_all;
335 struct got_commit_graph *graph;
336 struct commit_queue *commits;
337 const char *in_repo_path;
338 struct got_object_id *start_id;
339 struct got_repository *repo;
340 int log_complete;
341 sig_atomic_t *quit;
342 struct commit_queue_entry **first_displayed_entry;
343 struct commit_queue_entry **selected_entry;
344 int *searching;
345 int *search_next_done;
346 regex_t *regex;
347 };
349 struct tog_log_view_state {
350 struct commit_queue commits;
351 struct commit_queue_entry *first_displayed_entry;
352 struct commit_queue_entry *last_displayed_entry;
353 struct commit_queue_entry *selected_entry;
354 int selected;
355 char *in_repo_path;
356 char *head_ref_name;
357 int log_branches;
358 struct got_repository *repo;
359 struct got_object_id *start_id;
360 sig_atomic_t quit;
361 pthread_t thread;
362 struct tog_log_thread_args thread_args;
363 struct commit_queue_entry *matched_entry;
364 struct commit_queue_entry *search_entry;
365 struct tog_colors colors;
366 };
368 #define TOG_COLOR_DIFF_MINUS 1
369 #define TOG_COLOR_DIFF_PLUS 2
370 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
371 #define TOG_COLOR_DIFF_META 4
372 #define TOG_COLOR_TREE_SUBMODULE 5
373 #define TOG_COLOR_TREE_SYMLINK 6
374 #define TOG_COLOR_TREE_DIRECTORY 7
375 #define TOG_COLOR_TREE_EXECUTABLE 8
376 #define TOG_COLOR_COMMIT 9
377 #define TOG_COLOR_AUTHOR 10
378 #define TOG_COLOR_DATE 11
379 #define TOG_COLOR_REFS_HEADS 12
380 #define TOG_COLOR_REFS_TAGS 13
381 #define TOG_COLOR_REFS_REMOTES 14
382 #define TOG_COLOR_REFS_BACKUP 15
384 struct tog_blame_cb_args {
385 struct tog_blame_line *lines; /* one per line */
386 int nlines;
388 struct tog_view *view;
389 struct got_object_id *commit_id;
390 int *quit;
391 };
393 struct tog_blame_thread_args {
394 const char *path;
395 struct got_repository *repo;
396 struct tog_blame_cb_args *cb_args;
397 int *complete;
398 got_cancel_cb cancel_cb;
399 void *cancel_arg;
400 };
402 struct tog_blame {
403 FILE *f;
404 off_t filesize;
405 struct tog_blame_line *lines;
406 int nlines;
407 off_t *line_offsets;
408 pthread_t thread;
409 struct tog_blame_thread_args thread_args;
410 struct tog_blame_cb_args cb_args;
411 const char *path;
412 };
414 struct tog_blame_view_state {
415 int first_displayed_line;
416 int last_displayed_line;
417 int selected_line;
418 int blame_complete;
419 int eof;
420 int done;
421 struct got_object_id_queue blamed_commits;
422 struct got_object_qid *blamed_commit;
423 char *path;
424 struct got_repository *repo;
425 struct got_object_id *commit_id;
426 struct tog_blame blame;
427 int matched_line;
428 struct tog_colors colors;
429 };
431 struct tog_parent_tree {
432 TAILQ_ENTRY(tog_parent_tree) entry;
433 struct got_tree_object *tree;
434 struct got_tree_entry *first_displayed_entry;
435 struct got_tree_entry *selected_entry;
436 int selected;
437 };
439 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
441 struct tog_tree_view_state {
442 char *tree_label;
443 struct got_object_id *commit_id;/* commit which this tree belongs to */
444 struct got_tree_object *root; /* the commit's root tree entry */
445 struct got_tree_object *tree; /* currently displayed (sub-)tree */
446 struct got_tree_entry *first_displayed_entry;
447 struct got_tree_entry *last_displayed_entry;
448 struct got_tree_entry *selected_entry;
449 int ndisplayed, selected, show_ids;
450 struct tog_parent_trees parents; /* parent trees of current sub-tree */
451 char *head_ref_name;
452 struct got_repository *repo;
453 struct got_tree_entry *matched_entry;
454 struct tog_colors colors;
455 };
457 struct tog_reflist_entry {
458 TAILQ_ENTRY(tog_reflist_entry) entry;
459 struct got_reference *ref;
460 int idx;
461 };
463 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
465 struct tog_ref_view_state {
466 struct tog_reflist_head refs;
467 struct tog_reflist_entry *first_displayed_entry;
468 struct tog_reflist_entry *last_displayed_entry;
469 struct tog_reflist_entry *selected_entry;
470 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
471 struct got_repository *repo;
472 struct tog_reflist_entry *matched_entry;
473 struct tog_colors colors;
474 };
476 /*
477 * We implement two types of views: parent views and child views.
479 * The 'Tab' key switches focus between a parent view and its child view.
480 * Child views are shown side-by-side to their parent view, provided
481 * there is enough screen estate.
483 * When a new view is opened from within a parent view, this new view
484 * becomes a child view of the parent view, replacing any existing child.
486 * When a new view is opened from within a child view, this new view
487 * becomes a parent view which will obscure the views below until the
488 * user quits the new parent view by typing 'q'.
490 * This list of views contains parent views only.
491 * Child views are only pointed to by their parent view.
492 */
493 TAILQ_HEAD(tog_view_list_head, tog_view);
495 struct tog_view {
496 TAILQ_ENTRY(tog_view) entry;
497 WINDOW *window;
498 PANEL *panel;
499 int nlines, ncols, begin_y, begin_x;
500 int lines, cols; /* copies of LINES and COLS */
501 int focussed; /* Only set on one parent or child view at a time. */
502 int dying;
503 struct tog_view *parent;
504 struct tog_view *child;
506 /*
507 * This flag is initially set on parent views when a new child view
508 * is created. It gets toggled when the 'Tab' key switches focus
509 * between parent and child.
510 * The flag indicates whether focus should be passed on to our child
511 * view if this parent view gets picked for focus after another parent
512 * view was closed. This prevents child views from losing focus in such
513 * situations.
514 */
515 int focus_child;
517 /* type-specific state */
518 enum tog_view_type type;
519 union {
520 struct tog_diff_view_state diff;
521 struct tog_log_view_state log;
522 struct tog_blame_view_state blame;
523 struct tog_tree_view_state tree;
524 struct tog_ref_view_state ref;
525 } state;
527 const struct got_error *(*show)(struct tog_view *);
528 const struct got_error *(*input)(struct tog_view **,
529 struct tog_view *, int);
530 const struct got_error *(*close)(struct tog_view *);
532 const struct got_error *(*search_start)(struct tog_view *);
533 const struct got_error *(*search_next)(struct tog_view *);
534 int search_started;
535 int searching;
536 #define TOG_SEARCH_FORWARD 1
537 #define TOG_SEARCH_BACKWARD 2
538 int search_next_done;
539 #define TOG_SEARCH_HAVE_MORE 1
540 #define TOG_SEARCH_NO_MORE 2
541 #define TOG_SEARCH_HAVE_NONE 3
542 regex_t regex;
543 regmatch_t regmatch;
544 };
546 static const struct got_error *open_diff_view(struct tog_view *,
547 struct got_object_id *, struct got_object_id *,
548 const char *, const char *, int, int, int, struct tog_view *,
549 struct got_repository *);
550 static const struct got_error *show_diff_view(struct tog_view *);
551 static const struct got_error *input_diff_view(struct tog_view **,
552 struct tog_view *, int);
553 static const struct got_error* close_diff_view(struct tog_view *);
554 static const struct got_error *search_start_diff_view(struct tog_view *);
555 static const struct got_error *search_next_diff_view(struct tog_view *);
557 static const struct got_error *open_log_view(struct tog_view *,
558 struct got_object_id *, struct got_repository *,
559 const char *, const char *, int);
560 static const struct got_error * show_log_view(struct tog_view *);
561 static const struct got_error *input_log_view(struct tog_view **,
562 struct tog_view *, int);
563 static const struct got_error *close_log_view(struct tog_view *);
564 static const struct got_error *search_start_log_view(struct tog_view *);
565 static const struct got_error *search_next_log_view(struct tog_view *);
567 static const struct got_error *open_blame_view(struct tog_view *, char *,
568 struct got_object_id *, struct got_repository *);
569 static const struct got_error *show_blame_view(struct tog_view *);
570 static const struct got_error *input_blame_view(struct tog_view **,
571 struct tog_view *, int);
572 static const struct got_error *close_blame_view(struct tog_view *);
573 static const struct got_error *search_start_blame_view(struct tog_view *);
574 static const struct got_error *search_next_blame_view(struct tog_view *);
576 static const struct got_error *open_tree_view(struct tog_view *,
577 struct got_object_id *, const char *, struct got_repository *);
578 static const struct got_error *show_tree_view(struct tog_view *);
579 static const struct got_error *input_tree_view(struct tog_view **,
580 struct tog_view *, int);
581 static const struct got_error *close_tree_view(struct tog_view *);
582 static const struct got_error *search_start_tree_view(struct tog_view *);
583 static const struct got_error *search_next_tree_view(struct tog_view *);
585 static const struct got_error *open_ref_view(struct tog_view *,
586 struct got_repository *);
587 static const struct got_error *show_ref_view(struct tog_view *);
588 static const struct got_error *input_ref_view(struct tog_view **,
589 struct tog_view *, int);
590 static const struct got_error *close_ref_view(struct tog_view *);
591 static const struct got_error *search_start_ref_view(struct tog_view *);
592 static const struct got_error *search_next_ref_view(struct tog_view *);
594 static volatile sig_atomic_t tog_sigwinch_received;
595 static volatile sig_atomic_t tog_sigpipe_received;
596 static volatile sig_atomic_t tog_sigcont_received;
598 static void
599 tog_sigwinch(int signo)
601 tog_sigwinch_received = 1;
604 static void
605 tog_sigpipe(int signo)
607 tog_sigpipe_received = 1;
610 static void
611 tog_sigcont(int signo)
613 tog_sigcont_received = 1;
616 static const struct got_error *
617 view_close(struct tog_view *view)
619 const struct got_error *err = NULL;
621 if (view->child) {
622 view_close(view->child);
623 view->child = NULL;
625 if (view->close)
626 err = view->close(view);
627 if (view->panel)
628 del_panel(view->panel);
629 if (view->window)
630 delwin(view->window);
631 free(view);
632 return err;
635 static struct tog_view *
636 view_open(int nlines, int ncols, int begin_y, int begin_x,
637 enum tog_view_type type)
639 struct tog_view *view = calloc(1, sizeof(*view));
641 if (view == NULL)
642 return NULL;
644 view->type = type;
645 view->lines = LINES;
646 view->cols = COLS;
647 view->nlines = nlines ? nlines : LINES - begin_y;
648 view->ncols = ncols ? ncols : COLS - begin_x;
649 view->begin_y = begin_y;
650 view->begin_x = begin_x;
651 view->window = newwin(nlines, ncols, begin_y, begin_x);
652 if (view->window == NULL) {
653 view_close(view);
654 return NULL;
656 view->panel = new_panel(view->window);
657 if (view->panel == NULL ||
658 set_panel_userptr(view->panel, view) != OK) {
659 view_close(view);
660 return NULL;
663 keypad(view->window, TRUE);
664 return view;
667 static int
668 view_split_begin_x(int begin_x)
670 if (begin_x > 0 || COLS < 120)
671 return 0;
672 return (COLS - MAX(COLS / 2, 80));
675 static const struct got_error *view_resize(struct tog_view *);
677 static const struct got_error *
678 view_splitscreen(struct tog_view *view)
680 const struct got_error *err = NULL;
682 view->begin_y = 0;
683 view->begin_x = view_split_begin_x(0);
684 view->nlines = LINES;
685 view->ncols = COLS - view->begin_x;
686 view->lines = LINES;
687 view->cols = COLS;
688 err = view_resize(view);
689 if (err)
690 return err;
692 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
693 return got_error_from_errno("mvwin");
695 return NULL;
698 static const struct got_error *
699 view_fullscreen(struct tog_view *view)
701 const struct got_error *err = NULL;
703 view->begin_x = 0;
704 view->begin_y = 0;
705 view->nlines = LINES;
706 view->ncols = COLS;
707 view->lines = LINES;
708 view->cols = COLS;
709 err = view_resize(view);
710 if (err)
711 return err;
713 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
714 return got_error_from_errno("mvwin");
716 return NULL;
719 static int
720 view_is_parent_view(struct tog_view *view)
722 return view->parent == NULL;
725 static const struct got_error *
726 view_resize(struct tog_view *view)
728 int nlines, ncols;
730 if (view->lines > LINES)
731 nlines = view->nlines - (view->lines - LINES);
732 else
733 nlines = view->nlines + (LINES - view->lines);
735 if (view->cols > COLS)
736 ncols = view->ncols - (view->cols - COLS);
737 else
738 ncols = view->ncols + (COLS - view->cols);
740 if (wresize(view->window, nlines, ncols) == ERR)
741 return got_error_from_errno("wresize");
742 if (replace_panel(view->panel, view->window) == ERR)
743 return got_error_from_errno("replace_panel");
744 wclear(view->window);
746 view->nlines = nlines;
747 view->ncols = ncols;
748 view->lines = LINES;
749 view->cols = COLS;
751 if (view->child) {
752 view->child->begin_x = view_split_begin_x(view->begin_x);
753 if (view->child->begin_x == 0) {
754 view_fullscreen(view->child);
755 if (view->child->focussed)
756 show_panel(view->child->panel);
757 else
758 show_panel(view->panel);
759 } else {
760 view_splitscreen(view->child);
761 show_panel(view->child->panel);
765 return NULL;
768 static const struct got_error *
769 view_close_child(struct tog_view *view)
771 const struct got_error *err = NULL;
773 if (view->child == NULL)
774 return NULL;
776 err = view_close(view->child);
777 view->child = NULL;
778 return err;
781 static void
782 view_set_child(struct tog_view *view, struct tog_view *child)
784 view->child = child;
785 child->parent = view;
788 static int
789 view_is_splitscreen(struct tog_view *view)
791 return view->begin_x > 0;
794 static void
795 tog_resizeterm(void)
797 int cols, lines;
798 struct winsize size;
800 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
801 cols = 80; /* Default */
802 lines = 24;
803 } else {
804 cols = size.ws_col;
805 lines = size.ws_row;
807 resize_term(lines, cols);
810 static const struct got_error *
811 view_search_start(struct tog_view *view)
813 const struct got_error *err = NULL;
814 char pattern[1024];
815 int ret;
817 if (view->search_started) {
818 regfree(&view->regex);
819 view->searching = 0;
820 memset(&view->regmatch, 0, sizeof(view->regmatch));
822 view->search_started = 0;
824 if (view->nlines < 1)
825 return NULL;
827 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
828 wclrtoeol(view->window);
830 nocbreak();
831 echo();
832 ret = wgetnstr(view->window, pattern, sizeof(pattern));
833 cbreak();
834 noecho();
835 if (ret == ERR)
836 return NULL;
838 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
839 err = view->search_start(view);
840 if (err) {
841 regfree(&view->regex);
842 return err;
844 view->search_started = 1;
845 view->searching = TOG_SEARCH_FORWARD;
846 view->search_next_done = 0;
847 view->search_next(view);
850 return NULL;
853 static const struct got_error *
854 view_input(struct tog_view **new, int *done, struct tog_view *view,
855 struct tog_view_list_head *views)
857 const struct got_error *err = NULL;
858 struct tog_view *v;
859 int ch, errcode;
861 *new = NULL;
863 /* Clear "no matches" indicator. */
864 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
865 view->search_next_done == TOG_SEARCH_HAVE_NONE)
866 view->search_next_done = TOG_SEARCH_HAVE_MORE;
868 if (view->searching && !view->search_next_done) {
869 errcode = pthread_mutex_unlock(&tog_mutex);
870 if (errcode)
871 return got_error_set_errno(errcode,
872 "pthread_mutex_unlock");
873 sched_yield();
874 errcode = pthread_mutex_lock(&tog_mutex);
875 if (errcode)
876 return got_error_set_errno(errcode,
877 "pthread_mutex_lock");
878 view->search_next(view);
879 return NULL;
882 nodelay(stdscr, FALSE);
883 /* Allow threads to make progress while we are waiting for input. */
884 errcode = pthread_mutex_unlock(&tog_mutex);
885 if (errcode)
886 return got_error_set_errno(errcode, "pthread_mutex_unlock");
887 ch = wgetch(view->window);
888 errcode = pthread_mutex_lock(&tog_mutex);
889 if (errcode)
890 return got_error_set_errno(errcode, "pthread_mutex_lock");
891 nodelay(stdscr, TRUE);
893 if (tog_sigwinch_received || tog_sigcont_received) {
894 tog_resizeterm();
895 tog_sigwinch_received = 0;
896 tog_sigcont_received = 0;
897 TAILQ_FOREACH(v, views, entry) {
898 err = view_resize(v);
899 if (err)
900 return err;
901 err = v->input(new, v, KEY_RESIZE);
902 if (err)
903 return err;
904 if (v->child) {
905 err = view_resize(v->child);
906 if (err)
907 return err;
908 err = v->child->input(new, v->child,
909 KEY_RESIZE);
910 if (err)
911 return err;
916 switch (ch) {
917 case '\t':
918 if (view->child) {
919 view->focussed = 0;
920 view->child->focussed = 1;
921 view->focus_child = 1;
922 } else if (view->parent) {
923 view->focussed = 0;
924 view->parent->focussed = 1;
925 view->parent->focus_child = 0;
927 break;
928 case 'q':
929 err = view->input(new, view, ch);
930 view->dying = 1;
931 break;
932 case 'Q':
933 *done = 1;
934 break;
935 case 'f':
936 if (view_is_parent_view(view)) {
937 if (view->child == NULL)
938 break;
939 if (view_is_splitscreen(view->child)) {
940 view->focussed = 0;
941 view->child->focussed = 1;
942 err = view_fullscreen(view->child);
943 } else
944 err = view_splitscreen(view->child);
945 if (err)
946 break;
947 err = view->child->input(new, view->child,
948 KEY_RESIZE);
949 } else {
950 if (view_is_splitscreen(view)) {
951 view->parent->focussed = 0;
952 view->focussed = 1;
953 err = view_fullscreen(view);
954 } else {
955 err = view_splitscreen(view);
957 if (err)
958 break;
959 err = view->input(new, view, KEY_RESIZE);
961 break;
962 case KEY_RESIZE:
963 break;
964 case '/':
965 if (view->search_start)
966 view_search_start(view);
967 else
968 err = view->input(new, view, ch);
969 break;
970 case 'N':
971 case 'n':
972 if (view->search_started && view->search_next) {
973 view->searching = (ch == 'n' ?
974 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
975 view->search_next_done = 0;
976 view->search_next(view);
977 } else
978 err = view->input(new, view, ch);
979 break;
980 default:
981 err = view->input(new, view, ch);
982 break;
985 return err;
988 void
989 view_vborder(struct tog_view *view)
991 PANEL *panel;
992 const struct tog_view *view_above;
994 if (view->parent)
995 return view_vborder(view->parent);
997 panel = panel_above(view->panel);
998 if (panel == NULL)
999 return;
1001 view_above = panel_userptr(panel);
1002 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1003 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1006 int
1007 view_needs_focus_indication(struct tog_view *view)
1009 if (view_is_parent_view(view)) {
1010 if (view->child == NULL || view->child->focussed)
1011 return 0;
1012 if (!view_is_splitscreen(view->child))
1013 return 0;
1014 } else if (!view_is_splitscreen(view))
1015 return 0;
1017 return view->focussed;
1020 static const struct got_error *
1021 view_loop(struct tog_view *view)
1023 const struct got_error *err = NULL;
1024 struct tog_view_list_head views;
1025 struct tog_view *new_view;
1026 int fast_refresh = 10;
1027 int done = 0, errcode;
1029 errcode = pthread_mutex_lock(&tog_mutex);
1030 if (errcode)
1031 return got_error_set_errno(errcode, "pthread_mutex_lock");
1033 TAILQ_INIT(&views);
1034 TAILQ_INSERT_HEAD(&views, view, entry);
1036 view->focussed = 1;
1037 err = view->show(view);
1038 if (err)
1039 return err;
1040 update_panels();
1041 doupdate();
1042 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1043 /* Refresh fast during initialization, then become slower. */
1044 if (fast_refresh && fast_refresh-- == 0)
1045 halfdelay(10); /* switch to once per second */
1047 err = view_input(&new_view, &done, view, &views);
1048 if (err)
1049 break;
1050 if (view->dying) {
1051 struct tog_view *v, *prev = NULL;
1053 if (view_is_parent_view(view))
1054 prev = TAILQ_PREV(view, tog_view_list_head,
1055 entry);
1056 else if (view->parent)
1057 prev = view->parent;
1059 if (view->parent) {
1060 view->parent->child = NULL;
1061 view->parent->focus_child = 0;
1062 } else
1063 TAILQ_REMOVE(&views, view, entry);
1065 err = view_close(view);
1066 if (err)
1067 goto done;
1069 view = NULL;
1070 TAILQ_FOREACH(v, &views, entry) {
1071 if (v->focussed)
1072 break;
1074 if (view == NULL && new_view == NULL) {
1075 /* No view has focus. Try to pick one. */
1076 if (prev)
1077 view = prev;
1078 else if (!TAILQ_EMPTY(&views)) {
1079 view = TAILQ_LAST(&views,
1080 tog_view_list_head);
1082 if (view) {
1083 if (view->focus_child) {
1084 view->child->focussed = 1;
1085 view = view->child;
1086 } else
1087 view->focussed = 1;
1091 if (new_view) {
1092 struct tog_view *v, *t;
1093 /* Only allow one parent view per type. */
1094 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1095 if (v->type != new_view->type)
1096 continue;
1097 TAILQ_REMOVE(&views, v, entry);
1098 err = view_close(v);
1099 if (err)
1100 goto done;
1101 break;
1103 TAILQ_INSERT_TAIL(&views, new_view, entry);
1104 view = new_view;
1106 if (view) {
1107 if (view_is_parent_view(view)) {
1108 if (view->child && view->child->focussed)
1109 view = view->child;
1110 } else {
1111 if (view->parent && view->parent->focussed)
1112 view = view->parent;
1114 show_panel(view->panel);
1115 if (view->child && view_is_splitscreen(view->child))
1116 show_panel(view->child->panel);
1117 if (view->parent && view_is_splitscreen(view)) {
1118 err = view->parent->show(view->parent);
1119 if (err)
1120 goto done;
1122 err = view->show(view);
1123 if (err)
1124 goto done;
1125 if (view->child) {
1126 err = view->child->show(view->child);
1127 if (err)
1128 goto done;
1130 update_panels();
1131 doupdate();
1134 done:
1135 while (!TAILQ_EMPTY(&views)) {
1136 view = TAILQ_FIRST(&views);
1137 TAILQ_REMOVE(&views, view, entry);
1138 view_close(view);
1141 errcode = pthread_mutex_unlock(&tog_mutex);
1142 if (errcode && err == NULL)
1143 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1145 return err;
1148 __dead static void
1149 usage_log(void)
1151 endwin();
1152 fprintf(stderr,
1153 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1154 getprogname());
1155 exit(1);
1158 /* Create newly allocated wide-character string equivalent to a byte string. */
1159 static const struct got_error *
1160 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1162 char *vis = NULL;
1163 const struct got_error *err = NULL;
1165 *ws = NULL;
1166 *wlen = mbstowcs(NULL, s, 0);
1167 if (*wlen == (size_t)-1) {
1168 int vislen;
1169 if (errno != EILSEQ)
1170 return got_error_from_errno("mbstowcs");
1172 /* byte string invalid in current encoding; try to "fix" it */
1173 err = got_mbsavis(&vis, &vislen, s);
1174 if (err)
1175 return err;
1176 *wlen = mbstowcs(NULL, vis, 0);
1177 if (*wlen == (size_t)-1) {
1178 err = got_error_from_errno("mbstowcs"); /* give up */
1179 goto done;
1183 *ws = calloc(*wlen + 1, sizeof(**ws));
1184 if (*ws == NULL) {
1185 err = got_error_from_errno("calloc");
1186 goto done;
1189 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1190 err = got_error_from_errno("mbstowcs");
1191 done:
1192 free(vis);
1193 if (err) {
1194 free(*ws);
1195 *ws = NULL;
1196 *wlen = 0;
1198 return err;
1201 /* Format a line for display, ensuring that it won't overflow a width limit. */
1202 static const struct got_error *
1203 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1204 int col_tab_align)
1206 const struct got_error *err = NULL;
1207 int cols = 0;
1208 wchar_t *wline = NULL;
1209 size_t wlen;
1210 int i;
1212 *wlinep = NULL;
1213 *widthp = 0;
1215 err = mbs2ws(&wline, &wlen, line);
1216 if (err)
1217 return err;
1219 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1220 wline[wlen - 1] = L'\0';
1221 wlen--;
1223 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1224 wline[wlen - 1] = L'\0';
1225 wlen--;
1228 i = 0;
1229 while (i < wlen) {
1230 int width = wcwidth(wline[i]);
1232 if (width == 0) {
1233 i++;
1234 continue;
1237 if (width == 1 || width == 2) {
1238 if (cols + width > wlimit)
1239 break;
1240 cols += width;
1241 i++;
1242 } else if (width == -1) {
1243 if (wline[i] == L'\t') {
1244 width = TABSIZE -
1245 ((cols + col_tab_align) % TABSIZE);
1246 } else {
1247 width = 1;
1248 wline[i] = L'.';
1250 if (cols + width > wlimit)
1251 break;
1252 cols += width;
1253 i++;
1254 } else {
1255 err = got_error_from_errno("wcwidth");
1256 goto done;
1259 wline[i] = L'\0';
1260 if (widthp)
1261 *widthp = cols;
1262 done:
1263 if (err)
1264 free(wline);
1265 else
1266 *wlinep = wline;
1267 return err;
1270 static const struct got_error*
1271 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1272 struct got_object_id *id, struct got_repository *repo)
1274 static const struct got_error *err = NULL;
1275 struct got_reflist_entry *re;
1276 char *s;
1277 const char *name;
1279 *refs_str = NULL;
1281 TAILQ_FOREACH(re, refs, entry) {
1282 struct got_tag_object *tag = NULL;
1283 struct got_object_id *ref_id;
1284 int cmp;
1286 name = got_ref_get_name(re->ref);
1287 if (strcmp(name, GOT_REF_HEAD) == 0)
1288 continue;
1289 if (strncmp(name, "refs/", 5) == 0)
1290 name += 5;
1291 if (strncmp(name, "got/", 4) == 0 &&
1292 strncmp(name, "got/backup/", 11) != 0)
1293 continue;
1294 if (strncmp(name, "heads/", 6) == 0)
1295 name += 6;
1296 if (strncmp(name, "remotes/", 8) == 0) {
1297 name += 8;
1298 s = strstr(name, "/" GOT_REF_HEAD);
1299 if (s != NULL && s[strlen(s)] == '\0')
1300 continue;
1302 err = got_ref_resolve(&ref_id, repo, re->ref);
1303 if (err)
1304 break;
1305 if (strncmp(name, "tags/", 5) == 0) {
1306 err = got_object_open_as_tag(&tag, repo, ref_id);
1307 if (err) {
1308 if (err->code != GOT_ERR_OBJ_TYPE) {
1309 free(ref_id);
1310 break;
1312 /* Ref points at something other than a tag. */
1313 err = NULL;
1314 tag = NULL;
1317 cmp = got_object_id_cmp(tag ?
1318 got_object_tag_get_object_id(tag) : ref_id, id);
1319 free(ref_id);
1320 if (tag)
1321 got_object_tag_close(tag);
1322 if (cmp != 0)
1323 continue;
1324 s = *refs_str;
1325 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1326 s ? ", " : "", name) == -1) {
1327 err = got_error_from_errno("asprintf");
1328 free(s);
1329 *refs_str = NULL;
1330 break;
1332 free(s);
1335 return err;
1338 static const struct got_error *
1339 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1340 int col_tab_align)
1342 char *smallerthan;
1344 smallerthan = strchr(author, '<');
1345 if (smallerthan && smallerthan[1] != '\0')
1346 author = smallerthan + 1;
1347 author[strcspn(author, "@>")] = '\0';
1348 return format_line(wauthor, author_width, author, limit, col_tab_align);
1351 static const struct got_error *
1352 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1353 struct got_object_id *id, const size_t date_display_cols,
1354 int author_display_cols)
1356 struct tog_log_view_state *s = &view->state.log;
1357 const struct got_error *err = NULL;
1358 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1359 char *logmsg0 = NULL, *logmsg = NULL;
1360 char *author = NULL;
1361 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1362 int author_width, logmsg_width;
1363 char *newline, *line = NULL;
1364 int col, limit;
1365 const int avail = view->ncols;
1366 struct tm tm;
1367 time_t committer_time;
1368 struct tog_color *tc;
1370 committer_time = got_object_commit_get_committer_time(commit);
1371 if (gmtime_r(&committer_time, &tm) == NULL)
1372 return got_error_from_errno("gmtime_r");
1373 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1374 return got_error(GOT_ERR_NO_SPACE);
1376 if (avail <= date_display_cols)
1377 limit = MIN(sizeof(datebuf) - 1, avail);
1378 else
1379 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1380 tc = get_color(&s->colors, TOG_COLOR_DATE);
1381 if (tc)
1382 wattr_on(view->window,
1383 COLOR_PAIR(tc->colorpair), NULL);
1384 waddnstr(view->window, datebuf, limit);
1385 if (tc)
1386 wattr_off(view->window,
1387 COLOR_PAIR(tc->colorpair), NULL);
1388 col = limit;
1389 if (col > avail)
1390 goto done;
1392 if (avail >= 120) {
1393 char *id_str;
1394 err = got_object_id_str(&id_str, id);
1395 if (err)
1396 goto done;
1397 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1398 if (tc)
1399 wattr_on(view->window,
1400 COLOR_PAIR(tc->colorpair), NULL);
1401 wprintw(view->window, "%.8s ", id_str);
1402 if (tc)
1403 wattr_off(view->window,
1404 COLOR_PAIR(tc->colorpair), NULL);
1405 free(id_str);
1406 col += 9;
1407 if (col > avail)
1408 goto done;
1411 author = strdup(got_object_commit_get_author(commit));
1412 if (author == NULL) {
1413 err = got_error_from_errno("strdup");
1414 goto done;
1416 err = format_author(&wauthor, &author_width, author, avail - col, col);
1417 if (err)
1418 goto done;
1419 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1420 if (tc)
1421 wattr_on(view->window,
1422 COLOR_PAIR(tc->colorpair), NULL);
1423 waddwstr(view->window, wauthor);
1424 if (tc)
1425 wattr_off(view->window,
1426 COLOR_PAIR(tc->colorpair), NULL);
1427 col += author_width;
1428 while (col < avail && author_width < author_display_cols + 2) {
1429 waddch(view->window, ' ');
1430 col++;
1431 author_width++;
1433 if (col > avail)
1434 goto done;
1436 err = got_object_commit_get_logmsg(&logmsg0, commit);
1437 if (err)
1438 goto done;
1439 logmsg = logmsg0;
1440 while (*logmsg == '\n')
1441 logmsg++;
1442 newline = strchr(logmsg, '\n');
1443 if (newline)
1444 *newline = '\0';
1445 limit = avail - col;
1446 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1447 if (err)
1448 goto done;
1449 waddwstr(view->window, wlogmsg);
1450 col += logmsg_width;
1451 while (col < avail) {
1452 waddch(view->window, ' ');
1453 col++;
1455 done:
1456 free(logmsg0);
1457 free(wlogmsg);
1458 free(author);
1459 free(wauthor);
1460 free(line);
1461 return err;
1464 static struct commit_queue_entry *
1465 alloc_commit_queue_entry(struct got_commit_object *commit,
1466 struct got_object_id *id)
1468 struct commit_queue_entry *entry;
1470 entry = calloc(1, sizeof(*entry));
1471 if (entry == NULL)
1472 return NULL;
1474 entry->id = id;
1475 entry->commit = commit;
1476 return entry;
1479 static void
1480 pop_commit(struct commit_queue *commits)
1482 struct commit_queue_entry *entry;
1484 entry = TAILQ_FIRST(&commits->head);
1485 TAILQ_REMOVE(&commits->head, entry, entry);
1486 got_object_commit_close(entry->commit);
1487 commits->ncommits--;
1488 /* Don't free entry->id! It is owned by the commit graph. */
1489 free(entry);
1492 static void
1493 free_commits(struct commit_queue *commits)
1495 while (!TAILQ_EMPTY(&commits->head))
1496 pop_commit(commits);
1499 static const struct got_error *
1500 match_commit(int *have_match, struct got_object_id *id,
1501 struct got_commit_object *commit, regex_t *regex)
1503 const struct got_error *err = NULL;
1504 regmatch_t regmatch;
1505 char *id_str = NULL, *logmsg = NULL;
1507 *have_match = 0;
1509 err = got_object_id_str(&id_str, id);
1510 if (err)
1511 return err;
1513 err = got_object_commit_get_logmsg(&logmsg, commit);
1514 if (err)
1515 goto done;
1517 if (regexec(regex, got_object_commit_get_author(commit), 1,
1518 &regmatch, 0) == 0 ||
1519 regexec(regex, got_object_commit_get_committer(commit), 1,
1520 &regmatch, 0) == 0 ||
1521 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1522 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1523 *have_match = 1;
1524 done:
1525 free(id_str);
1526 free(logmsg);
1527 return err;
1530 static const struct got_error *
1531 queue_commits(struct tog_log_thread_args *a)
1533 const struct got_error *err = NULL;
1536 * We keep all commits open throughout the lifetime of the log
1537 * view in order to avoid having to re-fetch commits from disk
1538 * while updating the display.
1540 do {
1541 struct got_object_id *id;
1542 struct got_commit_object *commit;
1543 struct commit_queue_entry *entry;
1544 int errcode;
1546 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1547 NULL, NULL);
1548 if (err || id == NULL)
1549 break;
1551 err = got_object_open_as_commit(&commit, a->repo, id);
1552 if (err)
1553 break;
1554 entry = alloc_commit_queue_entry(commit, id);
1555 if (entry == NULL) {
1556 err = got_error_from_errno("alloc_commit_queue_entry");
1557 break;
1560 errcode = pthread_mutex_lock(&tog_mutex);
1561 if (errcode) {
1562 err = got_error_set_errno(errcode,
1563 "pthread_mutex_lock");
1564 break;
1567 entry->idx = a->commits->ncommits;
1568 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1569 a->commits->ncommits++;
1571 if (*a->searching == TOG_SEARCH_FORWARD &&
1572 !*a->search_next_done) {
1573 int have_match;
1574 err = match_commit(&have_match, id, commit, a->regex);
1575 if (err)
1576 break;
1577 if (have_match)
1578 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1581 errcode = pthread_mutex_unlock(&tog_mutex);
1582 if (errcode && err == NULL)
1583 err = got_error_set_errno(errcode,
1584 "pthread_mutex_unlock");
1585 if (err)
1586 break;
1587 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1589 return err;
1592 static void
1593 select_commit(struct tog_log_view_state *s)
1595 struct commit_queue_entry *entry;
1596 int ncommits = 0;
1598 entry = s->first_displayed_entry;
1599 while (entry) {
1600 if (ncommits == s->selected) {
1601 s->selected_entry = entry;
1602 break;
1604 entry = TAILQ_NEXT(entry, entry);
1605 ncommits++;
1609 static const struct got_error *
1610 draw_commits(struct tog_view *view)
1612 const struct got_error *err = NULL;
1613 struct tog_log_view_state *s = &view->state.log;
1614 struct commit_queue_entry *entry = s->selected_entry;
1615 const int limit = view->nlines;
1616 int width;
1617 int ncommits, author_cols = 4;
1618 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1619 char *refs_str = NULL;
1620 wchar_t *wline;
1621 struct tog_color *tc;
1622 static const size_t date_display_cols = 12;
1624 if (s->selected_entry &&
1625 !(view->searching && view->search_next_done == 0)) {
1626 struct got_reflist_head *refs;
1627 err = got_object_id_str(&id_str, s->selected_entry->id);
1628 if (err)
1629 return err;
1630 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1631 s->selected_entry->id);
1632 if (refs) {
1633 err = build_refs_str(&refs_str, refs,
1634 s->selected_entry->id, s->repo);
1635 if (err)
1636 goto done;
1640 if (s->thread_args.commits_needed == 0)
1641 halfdelay(10); /* disable fast refresh */
1643 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1644 if (asprintf(&ncommits_str, " [%d/%d] %s",
1645 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1646 (view->searching && !view->search_next_done) ?
1647 "searching..." : "loading...") == -1) {
1648 err = got_error_from_errno("asprintf");
1649 goto done;
1651 } else {
1652 const char *search_str = NULL;
1654 if (view->searching) {
1655 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1656 search_str = "no more matches";
1657 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1658 search_str = "no matches found";
1659 else if (!view->search_next_done)
1660 search_str = "searching...";
1663 if (asprintf(&ncommits_str, " [%d/%d] %s",
1664 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1665 search_str ? search_str :
1666 (refs_str ? refs_str : "")) == -1) {
1667 err = got_error_from_errno("asprintf");
1668 goto done;
1672 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1673 if (asprintf(&header, "commit %s %s%s",
1674 id_str ? id_str : "........................................",
1675 s->in_repo_path, ncommits_str) == -1) {
1676 err = got_error_from_errno("asprintf");
1677 header = NULL;
1678 goto done;
1680 } else if (asprintf(&header, "commit %s%s",
1681 id_str ? id_str : "........................................",
1682 ncommits_str) == -1) {
1683 err = got_error_from_errno("asprintf");
1684 header = NULL;
1685 goto done;
1687 err = format_line(&wline, &width, header, view->ncols, 0);
1688 if (err)
1689 goto done;
1691 werase(view->window);
1693 if (view_needs_focus_indication(view))
1694 wstandout(view->window);
1695 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1696 if (tc)
1697 wattr_on(view->window,
1698 COLOR_PAIR(tc->colorpair), NULL);
1699 waddwstr(view->window, wline);
1700 if (tc)
1701 wattr_off(view->window,
1702 COLOR_PAIR(tc->colorpair), NULL);
1703 while (width < view->ncols) {
1704 waddch(view->window, ' ');
1705 width++;
1707 if (view_needs_focus_indication(view))
1708 wstandend(view->window);
1709 free(wline);
1710 if (limit <= 1)
1711 goto done;
1713 /* Grow author column size if necessary. */
1714 entry = s->first_displayed_entry;
1715 ncommits = 0;
1716 while (entry) {
1717 char *author;
1718 wchar_t *wauthor;
1719 int width;
1720 if (ncommits >= limit - 1)
1721 break;
1722 author = strdup(got_object_commit_get_author(entry->commit));
1723 if (author == NULL) {
1724 err = got_error_from_errno("strdup");
1725 goto done;
1727 err = format_author(&wauthor, &width, author, COLS,
1728 date_display_cols);
1729 if (author_cols < width)
1730 author_cols = width;
1731 free(wauthor);
1732 free(author);
1733 ncommits++;
1734 entry = TAILQ_NEXT(entry, entry);
1737 entry = s->first_displayed_entry;
1738 s->last_displayed_entry = s->first_displayed_entry;
1739 ncommits = 0;
1740 while (entry) {
1741 if (ncommits >= limit - 1)
1742 break;
1743 if (ncommits == s->selected)
1744 wstandout(view->window);
1745 err = draw_commit(view, entry->commit, entry->id,
1746 date_display_cols, author_cols);
1747 if (ncommits == s->selected)
1748 wstandend(view->window);
1749 if (err)
1750 goto done;
1751 ncommits++;
1752 s->last_displayed_entry = entry;
1753 entry = TAILQ_NEXT(entry, entry);
1756 view_vborder(view);
1757 done:
1758 free(id_str);
1759 free(refs_str);
1760 free(ncommits_str);
1761 free(header);
1762 return err;
1765 static void
1766 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1768 struct commit_queue_entry *entry;
1769 int nscrolled = 0;
1771 entry = TAILQ_FIRST(&s->commits.head);
1772 if (s->first_displayed_entry == entry)
1773 return;
1775 entry = s->first_displayed_entry;
1776 while (entry && nscrolled < maxscroll) {
1777 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1778 if (entry) {
1779 s->first_displayed_entry = entry;
1780 nscrolled++;
1785 static const struct got_error *
1786 trigger_log_thread(struct tog_view *view, int wait)
1788 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1789 int errcode;
1791 halfdelay(1); /* fast refresh while loading commits */
1793 while (ta->commits_needed > 0 || ta->load_all) {
1794 if (ta->log_complete)
1795 break;
1797 /* Wake the log thread. */
1798 errcode = pthread_cond_signal(&ta->need_commits);
1799 if (errcode)
1800 return got_error_set_errno(errcode,
1801 "pthread_cond_signal");
1804 * The mutex will be released while the view loop waits
1805 * in wgetch(), at which time the log thread will run.
1807 if (!wait)
1808 break;
1810 /* Display progress update in log view. */
1811 show_log_view(view);
1812 update_panels();
1813 doupdate();
1815 /* Wait right here while next commit is being loaded. */
1816 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1817 if (errcode)
1818 return got_error_set_errno(errcode,
1819 "pthread_cond_wait");
1821 /* Display progress update in log view. */
1822 show_log_view(view);
1823 update_panels();
1824 doupdate();
1827 return NULL;
1830 static const struct got_error *
1831 log_scroll_down(struct tog_view *view, int maxscroll)
1833 struct tog_log_view_state *s = &view->state.log;
1834 const struct got_error *err = NULL;
1835 struct commit_queue_entry *pentry;
1836 int nscrolled = 0, ncommits_needed;
1838 if (s->last_displayed_entry == NULL)
1839 return NULL;
1841 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1842 if (s->commits.ncommits < ncommits_needed &&
1843 !s->thread_args.log_complete) {
1845 * Ask the log thread for required amount of commits.
1847 s->thread_args.commits_needed += maxscroll;
1848 err = trigger_log_thread(view, 1);
1849 if (err)
1850 return err;
1853 do {
1854 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1855 if (pentry == NULL)
1856 break;
1858 s->last_displayed_entry = pentry;
1860 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1861 if (pentry == NULL)
1862 break;
1863 s->first_displayed_entry = pentry;
1864 } while (++nscrolled < maxscroll);
1866 return err;
1869 static const struct got_error *
1870 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1871 struct got_commit_object *commit, struct got_object_id *commit_id,
1872 struct tog_view *log_view, struct got_repository *repo)
1874 const struct got_error *err;
1875 struct got_object_qid *parent_id;
1876 struct tog_view *diff_view;
1878 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1879 if (diff_view == NULL)
1880 return got_error_from_errno("view_open");
1882 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1883 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1884 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1885 if (err == NULL)
1886 *new_view = diff_view;
1887 return err;
1890 static const struct got_error *
1891 tree_view_visit_subtree(struct tog_tree_view_state *s,
1892 struct got_tree_object *subtree)
1894 struct tog_parent_tree *parent;
1896 parent = calloc(1, sizeof(*parent));
1897 if (parent == NULL)
1898 return got_error_from_errno("calloc");
1900 parent->tree = s->tree;
1901 parent->first_displayed_entry = s->first_displayed_entry;
1902 parent->selected_entry = s->selected_entry;
1903 parent->selected = s->selected;
1904 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1905 s->tree = subtree;
1906 s->selected = 0;
1907 s->first_displayed_entry = NULL;
1908 return NULL;
1911 static const struct got_error *
1912 tree_view_walk_path(struct tog_tree_view_state *s,
1913 struct got_object_id *commit_id, const char *path)
1915 const struct got_error *err = NULL;
1916 struct got_tree_object *tree = NULL;
1917 const char *p;
1918 char *slash, *subpath = NULL;
1920 /* Walk the path and open corresponding tree objects. */
1921 p = path;
1922 while (*p) {
1923 struct got_tree_entry *te;
1924 struct got_object_id *tree_id;
1925 char *te_name;
1927 while (p[0] == '/')
1928 p++;
1930 /* Ensure the correct subtree entry is selected. */
1931 slash = strchr(p, '/');
1932 if (slash == NULL)
1933 te_name = strdup(p);
1934 else
1935 te_name = strndup(p, slash - p);
1936 if (te_name == NULL) {
1937 err = got_error_from_errno("strndup");
1938 break;
1940 te = got_object_tree_find_entry(s->tree, te_name);
1941 if (te == NULL) {
1942 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1943 free(te_name);
1944 break;
1946 free(te_name);
1947 s->first_displayed_entry = s->selected_entry = te;
1949 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1950 break; /* jump to this file's entry */
1952 slash = strchr(p, '/');
1953 if (slash)
1954 subpath = strndup(path, slash - path);
1955 else
1956 subpath = strdup(path);
1957 if (subpath == NULL) {
1958 err = got_error_from_errno("strdup");
1959 break;
1962 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1963 subpath);
1964 if (err)
1965 break;
1967 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1968 free(tree_id);
1969 if (err)
1970 break;
1972 err = tree_view_visit_subtree(s, tree);
1973 if (err) {
1974 got_object_tree_close(tree);
1975 break;
1977 if (slash == NULL)
1978 break;
1979 free(subpath);
1980 subpath = NULL;
1981 p = slash;
1984 free(subpath);
1985 return err;
1988 static const struct got_error *
1989 browse_commit_tree(struct tog_view **new_view, int begin_x,
1990 struct commit_queue_entry *entry, const char *path,
1991 const char *head_ref_name, struct got_repository *repo)
1993 const struct got_error *err = NULL;
1994 struct tog_tree_view_state *s;
1995 struct tog_view *tree_view;
1997 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1998 if (tree_view == NULL)
1999 return got_error_from_errno("view_open");
2001 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2002 if (err)
2003 return err;
2004 s = &tree_view->state.tree;
2006 *new_view = tree_view;
2008 if (got_path_is_root_dir(path))
2009 return NULL;
2011 return tree_view_walk_path(s, entry->id, path);
2014 static const struct got_error *
2015 block_signals_used_by_main_thread(void)
2017 sigset_t sigset;
2018 int errcode;
2020 if (sigemptyset(&sigset) == -1)
2021 return got_error_from_errno("sigemptyset");
2023 /* tog handles SIGWINCH and SIGCONT */
2024 if (sigaddset(&sigset, SIGWINCH) == -1)
2025 return got_error_from_errno("sigaddset");
2026 if (sigaddset(&sigset, SIGCONT) == -1)
2027 return got_error_from_errno("sigaddset");
2029 /* ncurses handles SIGTSTP */
2030 if (sigaddset(&sigset, SIGTSTP) == -1)
2031 return got_error_from_errno("sigaddset");
2033 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2034 if (errcode)
2035 return got_error_set_errno(errcode, "pthread_sigmask");
2037 return NULL;
2040 static void *
2041 log_thread(void *arg)
2043 const struct got_error *err = NULL;
2044 int errcode = 0;
2045 struct tog_log_thread_args *a = arg;
2046 int done = 0;
2048 err = block_signals_used_by_main_thread();
2049 if (err)
2050 return (void *)err;
2052 while (!done && !err && !tog_sigpipe_received) {
2053 err = queue_commits(a);
2054 if (err) {
2055 if (err->code != GOT_ERR_ITER_COMPLETED)
2056 return (void *)err;
2057 err = NULL;
2058 done = 1;
2059 } else if (a->commits_needed > 0 && !a->load_all)
2060 a->commits_needed--;
2062 errcode = pthread_mutex_lock(&tog_mutex);
2063 if (errcode) {
2064 err = got_error_set_errno(errcode,
2065 "pthread_mutex_lock");
2066 break;
2067 } else if (*a->quit)
2068 done = 1;
2069 else if (*a->first_displayed_entry == NULL) {
2070 *a->first_displayed_entry =
2071 TAILQ_FIRST(&a->commits->head);
2072 *a->selected_entry = *a->first_displayed_entry;
2075 errcode = pthread_cond_signal(&a->commit_loaded);
2076 if (errcode) {
2077 err = got_error_set_errno(errcode,
2078 "pthread_cond_signal");
2079 pthread_mutex_unlock(&tog_mutex);
2080 break;
2083 if (done)
2084 a->commits_needed = 0;
2085 else {
2086 if (a->commits_needed == 0 && !a->load_all) {
2087 errcode = pthread_cond_wait(&a->need_commits,
2088 &tog_mutex);
2089 if (errcode)
2090 err = got_error_set_errno(errcode,
2091 "pthread_cond_wait");
2092 if (*a->quit)
2093 done = 1;
2097 errcode = pthread_mutex_unlock(&tog_mutex);
2098 if (errcode && err == NULL)
2099 err = got_error_set_errno(errcode,
2100 "pthread_mutex_unlock");
2102 a->log_complete = 1;
2103 return (void *)err;
2106 static const struct got_error *
2107 stop_log_thread(struct tog_log_view_state *s)
2109 const struct got_error *err = NULL;
2110 int errcode;
2112 if (s->thread) {
2113 s->quit = 1;
2114 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2115 if (errcode)
2116 return got_error_set_errno(errcode,
2117 "pthread_cond_signal");
2118 errcode = pthread_mutex_unlock(&tog_mutex);
2119 if (errcode)
2120 return got_error_set_errno(errcode,
2121 "pthread_mutex_unlock");
2122 errcode = pthread_join(s->thread, (void **)&err);
2123 if (errcode)
2124 return got_error_set_errno(errcode, "pthread_join");
2125 errcode = pthread_mutex_lock(&tog_mutex);
2126 if (errcode)
2127 return got_error_set_errno(errcode,
2128 "pthread_mutex_lock");
2129 s->thread = NULL;
2132 if (s->thread_args.repo) {
2133 err = got_repo_close(s->thread_args.repo);
2134 s->thread_args.repo = NULL;
2137 if (s->thread_args.graph) {
2138 got_commit_graph_close(s->thread_args.graph);
2139 s->thread_args.graph = NULL;
2142 return err;
2145 static const struct got_error *
2146 close_log_view(struct tog_view *view)
2148 const struct got_error *err = NULL;
2149 struct tog_log_view_state *s = &view->state.log;
2150 int errcode;
2152 err = stop_log_thread(s);
2154 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2155 if (errcode && err == NULL)
2156 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2158 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2159 if (errcode && err == NULL)
2160 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2162 free_commits(&s->commits);
2163 free(s->in_repo_path);
2164 s->in_repo_path = NULL;
2165 free(s->start_id);
2166 s->start_id = NULL;
2167 free(s->head_ref_name);
2168 s->head_ref_name = NULL;
2169 return err;
2172 static const struct got_error *
2173 search_start_log_view(struct tog_view *view)
2175 struct tog_log_view_state *s = &view->state.log;
2177 s->matched_entry = NULL;
2178 s->search_entry = NULL;
2179 return NULL;
2182 static const struct got_error *
2183 search_next_log_view(struct tog_view *view)
2185 const struct got_error *err = NULL;
2186 struct tog_log_view_state *s = &view->state.log;
2187 struct commit_queue_entry *entry;
2189 /* Display progress update in log view. */
2190 show_log_view(view);
2191 update_panels();
2192 doupdate();
2194 if (s->search_entry) {
2195 int errcode, ch;
2196 errcode = pthread_mutex_unlock(&tog_mutex);
2197 if (errcode)
2198 return got_error_set_errno(errcode,
2199 "pthread_mutex_unlock");
2200 ch = wgetch(view->window);
2201 errcode = pthread_mutex_lock(&tog_mutex);
2202 if (errcode)
2203 return got_error_set_errno(errcode,
2204 "pthread_mutex_lock");
2205 if (ch == KEY_BACKSPACE) {
2206 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2207 return NULL;
2209 if (view->searching == TOG_SEARCH_FORWARD)
2210 entry = TAILQ_NEXT(s->search_entry, entry);
2211 else
2212 entry = TAILQ_PREV(s->search_entry,
2213 commit_queue_head, entry);
2214 } else if (s->matched_entry) {
2215 if (view->searching == TOG_SEARCH_FORWARD)
2216 entry = TAILQ_NEXT(s->matched_entry, entry);
2217 else
2218 entry = TAILQ_PREV(s->matched_entry,
2219 commit_queue_head, entry);
2220 } else {
2221 entry = s->selected_entry;
2224 while (1) {
2225 int have_match = 0;
2227 if (entry == NULL) {
2228 if (s->thread_args.log_complete ||
2229 view->searching == TOG_SEARCH_BACKWARD) {
2230 view->search_next_done =
2231 (s->matched_entry == NULL ?
2232 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2233 s->search_entry = NULL;
2234 return NULL;
2237 * Poke the log thread for more commits and return,
2238 * allowing the main loop to make progress. Search
2239 * will resume at s->search_entry once we come back.
2241 s->thread_args.commits_needed++;
2242 return trigger_log_thread(view, 0);
2245 err = match_commit(&have_match, entry->id, entry->commit,
2246 &view->regex);
2247 if (err)
2248 break;
2249 if (have_match) {
2250 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2251 s->matched_entry = entry;
2252 break;
2255 s->search_entry = entry;
2256 if (view->searching == TOG_SEARCH_FORWARD)
2257 entry = TAILQ_NEXT(entry, entry);
2258 else
2259 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2262 if (s->matched_entry) {
2263 int cur = s->selected_entry->idx;
2264 while (cur < s->matched_entry->idx) {
2265 err = input_log_view(NULL, view, KEY_DOWN);
2266 if (err)
2267 return err;
2268 cur++;
2270 while (cur > s->matched_entry->idx) {
2271 err = input_log_view(NULL, view, KEY_UP);
2272 if (err)
2273 return err;
2274 cur--;
2278 s->search_entry = NULL;
2280 return NULL;
2283 static const struct got_error *
2284 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2285 struct got_repository *repo, const char *head_ref_name,
2286 const char *in_repo_path, int log_branches)
2288 const struct got_error *err = NULL;
2289 struct tog_log_view_state *s = &view->state.log;
2290 struct got_repository *thread_repo = NULL;
2291 struct got_commit_graph *thread_graph = NULL;
2292 int errcode;
2294 if (in_repo_path != s->in_repo_path) {
2295 free(s->in_repo_path);
2296 s->in_repo_path = strdup(in_repo_path);
2297 if (s->in_repo_path == NULL)
2298 return got_error_from_errno("strdup");
2301 /* The commit queue only contains commits being displayed. */
2302 TAILQ_INIT(&s->commits.head);
2303 s->commits.ncommits = 0;
2305 s->repo = repo;
2306 if (head_ref_name) {
2307 s->head_ref_name = strdup(head_ref_name);
2308 if (s->head_ref_name == NULL) {
2309 err = got_error_from_errno("strdup");
2310 goto done;
2313 s->start_id = got_object_id_dup(start_id);
2314 if (s->start_id == NULL) {
2315 err = got_error_from_errno("got_object_id_dup");
2316 goto done;
2318 s->log_branches = log_branches;
2320 STAILQ_INIT(&s->colors);
2321 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2322 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2323 get_color_value("TOG_COLOR_COMMIT"));
2324 if (err)
2325 goto done;
2326 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2327 get_color_value("TOG_COLOR_AUTHOR"));
2328 if (err) {
2329 free_colors(&s->colors);
2330 goto done;
2332 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2333 get_color_value("TOG_COLOR_DATE"));
2334 if (err) {
2335 free_colors(&s->colors);
2336 goto done;
2340 view->show = show_log_view;
2341 view->input = input_log_view;
2342 view->close = close_log_view;
2343 view->search_start = search_start_log_view;
2344 view->search_next = search_next_log_view;
2346 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2347 if (err)
2348 goto done;
2349 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2350 !s->log_branches);
2351 if (err)
2352 goto done;
2353 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2354 s->repo, NULL, NULL);
2355 if (err)
2356 goto done;
2358 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2359 if (errcode) {
2360 err = got_error_set_errno(errcode, "pthread_cond_init");
2361 goto done;
2363 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2364 if (errcode) {
2365 err = got_error_set_errno(errcode, "pthread_cond_init");
2366 goto done;
2369 s->thread_args.commits_needed = view->nlines;
2370 s->thread_args.graph = thread_graph;
2371 s->thread_args.commits = &s->commits;
2372 s->thread_args.in_repo_path = s->in_repo_path;
2373 s->thread_args.start_id = s->start_id;
2374 s->thread_args.repo = thread_repo;
2375 s->thread_args.log_complete = 0;
2376 s->thread_args.quit = &s->quit;
2377 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2378 s->thread_args.selected_entry = &s->selected_entry;
2379 s->thread_args.searching = &view->searching;
2380 s->thread_args.search_next_done = &view->search_next_done;
2381 s->thread_args.regex = &view->regex;
2382 done:
2383 if (err)
2384 close_log_view(view);
2385 return err;
2388 static const struct got_error *
2389 show_log_view(struct tog_view *view)
2391 const struct got_error *err;
2392 struct tog_log_view_state *s = &view->state.log;
2394 if (s->thread == NULL) {
2395 int errcode = pthread_create(&s->thread, NULL, log_thread,
2396 &s->thread_args);
2397 if (errcode)
2398 return got_error_set_errno(errcode, "pthread_create");
2399 if (s->thread_args.commits_needed > 0) {
2400 err = trigger_log_thread(view, 1);
2401 if (err)
2402 return err;
2406 return draw_commits(view);
2409 static const struct got_error *
2410 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2412 const struct got_error *err = NULL;
2413 struct tog_log_view_state *s = &view->state.log;
2414 struct tog_view *diff_view = NULL, *tree_view = NULL;
2415 struct tog_view *ref_view = NULL;
2416 struct commit_queue_entry *entry;
2417 int begin_x = 0, n;
2419 if (s->thread_args.load_all) {
2420 if (ch == KEY_BACKSPACE)
2421 s->thread_args.load_all = 0;
2422 else if (s->thread_args.log_complete) {
2423 s->thread_args.load_all = 0;
2424 log_scroll_down(view, s->commits.ncommits);
2425 s->selected = MIN(view->nlines - 2,
2426 s->commits.ncommits - 1);
2427 select_commit(s);
2429 return NULL;
2432 switch (ch) {
2433 case 'q':
2434 s->quit = 1;
2435 break;
2436 case 'k':
2437 case KEY_UP:
2438 case '<':
2439 case ',':
2440 case CTRL('p'):
2441 if (s->first_displayed_entry == NULL)
2442 break;
2443 if (s->selected > 0)
2444 s->selected--;
2445 else
2446 log_scroll_up(s, 1);
2447 select_commit(s);
2448 break;
2449 case 'g':
2450 case KEY_HOME:
2451 s->selected = 0;
2452 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2453 select_commit(s);
2454 break;
2455 case KEY_PPAGE:
2456 case CTRL('b'):
2457 if (s->first_displayed_entry == NULL)
2458 break;
2459 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2460 s->selected = 0;
2461 else
2462 log_scroll_up(s, view->nlines - 1);
2463 select_commit(s);
2464 break;
2465 case 'j':
2466 case KEY_DOWN:
2467 case '>':
2468 case '.':
2469 case CTRL('n'):
2470 if (s->first_displayed_entry == NULL)
2471 break;
2472 if (s->selected < MIN(view->nlines - 2,
2473 s->commits.ncommits - 1))
2474 s->selected++;
2475 else {
2476 err = log_scroll_down(view, 1);
2477 if (err)
2478 break;
2480 select_commit(s);
2481 break;
2482 case 'G':
2483 case KEY_END: {
2484 /* We don't know yet how many commits, so we're forced to
2485 * traverse them all. */
2486 if (!s->thread_args.log_complete) {
2487 s->thread_args.load_all = 1;
2488 return trigger_log_thread(view, 0);
2491 s->selected = 0;
2492 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2493 for (n = 0; n < view->nlines - 1; n++) {
2494 if (entry == NULL)
2495 break;
2496 s->first_displayed_entry = entry;
2497 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2499 if (n > 0)
2500 s->selected = n - 1;
2501 select_commit(s);
2502 break;
2504 case KEY_NPAGE:
2505 case CTRL('f'): {
2506 struct commit_queue_entry *first;
2507 first = s->first_displayed_entry;
2508 if (first == NULL)
2509 break;
2510 err = log_scroll_down(view, view->nlines - 1);
2511 if (err)
2512 break;
2513 if (first == s->first_displayed_entry &&
2514 s->selected < MIN(view->nlines - 2,
2515 s->commits.ncommits - 1)) {
2516 /* can't scroll further down */
2517 s->selected = MIN(view->nlines - 2,
2518 s->commits.ncommits - 1);
2520 select_commit(s);
2521 break;
2523 case KEY_RESIZE:
2524 if (s->selected > view->nlines - 2)
2525 s->selected = view->nlines - 2;
2526 if (s->selected > s->commits.ncommits - 1)
2527 s->selected = s->commits.ncommits - 1;
2528 select_commit(s);
2529 if (s->commits.ncommits < view->nlines - 1 &&
2530 !s->thread_args.log_complete) {
2531 s->thread_args.commits_needed += (view->nlines - 1) -
2532 s->commits.ncommits;
2533 err = trigger_log_thread(view, 1);
2535 break;
2536 case KEY_ENTER:
2537 case ' ':
2538 case '\r':
2539 if (s->selected_entry == NULL)
2540 break;
2541 if (view_is_parent_view(view))
2542 begin_x = view_split_begin_x(view->begin_x);
2543 err = open_diff_view_for_commit(&diff_view, begin_x,
2544 s->selected_entry->commit, s->selected_entry->id,
2545 view, s->repo);
2546 if (err)
2547 break;
2548 view->focussed = 0;
2549 diff_view->focussed = 1;
2550 if (view_is_parent_view(view)) {
2551 err = view_close_child(view);
2552 if (err)
2553 return err;
2554 view_set_child(view, diff_view);
2555 view->focus_child = 1;
2556 } else
2557 *new_view = diff_view;
2558 break;
2559 case 't':
2560 if (s->selected_entry == NULL)
2561 break;
2562 if (view_is_parent_view(view))
2563 begin_x = view_split_begin_x(view->begin_x);
2564 err = browse_commit_tree(&tree_view, begin_x,
2565 s->selected_entry, s->in_repo_path, s->head_ref_name,
2566 s->repo);
2567 if (err)
2568 break;
2569 view->focussed = 0;
2570 tree_view->focussed = 1;
2571 if (view_is_parent_view(view)) {
2572 err = view_close_child(view);
2573 if (err)
2574 return err;
2575 view_set_child(view, tree_view);
2576 view->focus_child = 1;
2577 } else
2578 *new_view = tree_view;
2579 break;
2580 case KEY_BACKSPACE:
2581 case CTRL('l'):
2582 case 'B':
2583 if (ch == KEY_BACKSPACE &&
2584 got_path_is_root_dir(s->in_repo_path))
2585 break;
2586 err = stop_log_thread(s);
2587 if (err)
2588 return err;
2589 if (ch == KEY_BACKSPACE) {
2590 char *parent_path;
2591 err = got_path_dirname(&parent_path, s->in_repo_path);
2592 if (err)
2593 return err;
2594 free(s->in_repo_path);
2595 s->in_repo_path = parent_path;
2596 s->thread_args.in_repo_path = s->in_repo_path;
2597 } else if (ch == CTRL('l')) {
2598 struct got_object_id *start_id;
2599 err = got_repo_match_object_id(&start_id, NULL,
2600 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2601 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2602 if (err)
2603 return err;
2604 free(s->start_id);
2605 s->start_id = start_id;
2606 s->thread_args.start_id = s->start_id;
2607 } else /* 'B' */
2608 s->log_branches = !s->log_branches;
2610 err = got_repo_open(&s->thread_args.repo,
2611 got_repo_get_path(s->repo), NULL);
2612 if (err)
2613 return err;
2614 tog_free_refs();
2615 err = tog_load_refs(s->repo, 0);
2616 if (err)
2617 return err;
2618 err = got_commit_graph_open(&s->thread_args.graph,
2619 s->in_repo_path, !s->log_branches);
2620 if (err)
2621 return err;
2622 err = got_commit_graph_iter_start(s->thread_args.graph,
2623 s->start_id, s->repo, NULL, NULL);
2624 if (err)
2625 return err;
2626 free_commits(&s->commits);
2627 s->first_displayed_entry = NULL;
2628 s->last_displayed_entry = NULL;
2629 s->selected_entry = NULL;
2630 s->selected = 0;
2631 s->thread_args.log_complete = 0;
2632 s->quit = 0;
2633 s->thread_args.commits_needed = view->nlines;
2634 break;
2635 case 'r':
2636 if (view_is_parent_view(view))
2637 begin_x = view_split_begin_x(view->begin_x);
2638 ref_view = view_open(view->nlines, view->ncols,
2639 view->begin_y, begin_x, TOG_VIEW_REF);
2640 if (ref_view == NULL)
2641 return got_error_from_errno("view_open");
2642 err = open_ref_view(ref_view, s->repo);
2643 if (err) {
2644 view_close(ref_view);
2645 return err;
2647 view->focussed = 0;
2648 ref_view->focussed = 1;
2649 if (view_is_parent_view(view)) {
2650 err = view_close_child(view);
2651 if (err)
2652 return err;
2653 view_set_child(view, ref_view);
2654 view->focus_child = 1;
2655 } else
2656 *new_view = ref_view;
2657 break;
2658 default:
2659 break;
2662 return err;
2665 static const struct got_error *
2666 apply_unveil(const char *repo_path, const char *worktree_path)
2668 const struct got_error *error;
2670 #ifdef PROFILE
2671 if (unveil("gmon.out", "rwc") != 0)
2672 return got_error_from_errno2("unveil", "gmon.out");
2673 #endif
2674 if (repo_path && unveil(repo_path, "r") != 0)
2675 return got_error_from_errno2("unveil", repo_path);
2677 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2678 return got_error_from_errno2("unveil", worktree_path);
2680 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2681 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2683 error = got_privsep_unveil_exec_helpers();
2684 if (error != NULL)
2685 return error;
2687 if (unveil(NULL, NULL) != 0)
2688 return got_error_from_errno("unveil");
2690 return NULL;
2693 static void
2694 init_curses(void)
2696 initscr();
2697 cbreak();
2698 halfdelay(1); /* Do fast refresh while initial view is loading. */
2699 noecho();
2700 nonl();
2701 intrflush(stdscr, FALSE);
2702 keypad(stdscr, TRUE);
2703 curs_set(0);
2704 if (getenv("TOG_COLORS") != NULL) {
2705 start_color();
2706 use_default_colors();
2708 signal(SIGWINCH, tog_sigwinch);
2709 signal(SIGPIPE, tog_sigpipe);
2710 signal(SIGCONT, tog_sigcont);
2713 static const struct got_error *
2714 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2715 struct got_repository *repo, struct got_worktree *worktree)
2717 const struct got_error *err = NULL;
2719 if (argc == 0) {
2720 *in_repo_path = strdup("/");
2721 if (*in_repo_path == NULL)
2722 return got_error_from_errno("strdup");
2723 return NULL;
2726 if (worktree) {
2727 const char *prefix = got_worktree_get_path_prefix(worktree);
2728 char *p;
2730 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2731 if (err)
2732 return err;
2733 if (asprintf(in_repo_path, "%s%s%s", prefix,
2734 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2735 p) == -1) {
2736 err = got_error_from_errno("asprintf");
2737 *in_repo_path = NULL;
2739 free(p);
2740 } else
2741 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2743 return err;
2746 static const struct got_error *
2747 cmd_log(int argc, char *argv[])
2749 const struct got_error *error;
2750 struct got_repository *repo = NULL;
2751 struct got_worktree *worktree = NULL;
2752 struct got_object_id *start_id = NULL;
2753 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2754 char *start_commit = NULL, *label = NULL;
2755 struct got_reference *ref = NULL;
2756 const char *head_ref_name = NULL;
2757 int ch, log_branches = 0;
2758 struct tog_view *view;
2760 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2761 switch (ch) {
2762 case 'b':
2763 log_branches = 1;
2764 break;
2765 case 'c':
2766 start_commit = optarg;
2767 break;
2768 case 'r':
2769 repo_path = realpath(optarg, NULL);
2770 if (repo_path == NULL)
2771 return got_error_from_errno2("realpath",
2772 optarg);
2773 break;
2774 default:
2775 usage_log();
2776 /* NOTREACHED */
2780 argc -= optind;
2781 argv += optind;
2783 if (argc > 1)
2784 usage_log();
2786 if (repo_path == NULL) {
2787 cwd = getcwd(NULL, 0);
2788 if (cwd == NULL)
2789 return got_error_from_errno("getcwd");
2790 error = got_worktree_open(&worktree, cwd);
2791 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2792 goto done;
2793 if (worktree)
2794 repo_path =
2795 strdup(got_worktree_get_repo_path(worktree));
2796 else
2797 repo_path = strdup(cwd);
2798 if (repo_path == NULL) {
2799 error = got_error_from_errno("strdup");
2800 goto done;
2804 error = got_repo_open(&repo, repo_path, NULL);
2805 if (error != NULL)
2806 goto done;
2808 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2809 repo, worktree);
2810 if (error)
2811 goto done;
2813 init_curses();
2815 error = apply_unveil(got_repo_get_path(repo),
2816 worktree ? got_worktree_get_root_path(worktree) : NULL);
2817 if (error)
2818 goto done;
2820 /* already loaded by tog_log_with_path()? */
2821 if (TAILQ_EMPTY(&tog_refs)) {
2822 error = tog_load_refs(repo, 0);
2823 if (error)
2824 goto done;
2827 if (start_commit == NULL) {
2828 error = got_repo_match_object_id(&start_id, &label,
2829 worktree ? got_worktree_get_head_ref_name(worktree) :
2830 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2831 if (error)
2832 goto done;
2833 head_ref_name = label;
2834 } else {
2835 error = got_ref_open(&ref, repo, start_commit, 0);
2836 if (error == NULL)
2837 head_ref_name = got_ref_get_name(ref);
2838 else if (error->code != GOT_ERR_NOT_REF)
2839 goto done;
2840 error = got_repo_match_object_id(&start_id, NULL,
2841 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2842 if (error)
2843 goto done;
2846 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2847 if (view == NULL) {
2848 error = got_error_from_errno("view_open");
2849 goto done;
2851 error = open_log_view(view, start_id, repo, head_ref_name,
2852 in_repo_path, log_branches);
2853 if (error)
2854 goto done;
2855 if (worktree) {
2856 /* Release work tree lock. */
2857 got_worktree_close(worktree);
2858 worktree = NULL;
2860 error = view_loop(view);
2861 done:
2862 free(in_repo_path);
2863 free(repo_path);
2864 free(cwd);
2865 free(start_id);
2866 free(label);
2867 if (ref)
2868 got_ref_close(ref);
2869 if (repo) {
2870 const struct got_error *close_err = got_repo_close(repo);
2871 if (error == NULL)
2872 error = close_err;
2874 if (worktree)
2875 got_worktree_close(worktree);
2876 tog_free_refs();
2877 return error;
2880 __dead static void
2881 usage_diff(void)
2883 endwin();
2884 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2885 "[-w] object1 object2\n", getprogname());
2886 exit(1);
2889 static int
2890 match_line(const char *line, regex_t *regex, size_t nmatch,
2891 regmatch_t *regmatch)
2893 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2896 struct tog_color *
2897 match_color(struct tog_colors *colors, const char *line)
2899 struct tog_color *tc = NULL;
2901 STAILQ_FOREACH(tc, colors, entry) {
2902 if (match_line(line, &tc->regex, 0, NULL))
2903 return tc;
2906 return NULL;
2909 static const struct got_error *
2910 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2911 WINDOW *window, regmatch_t *regmatch)
2913 const struct got_error *err = NULL;
2914 wchar_t *wline;
2915 int width;
2916 char *s;
2918 *wtotal = 0;
2920 s = strndup(line, regmatch->rm_so);
2921 if (s == NULL)
2922 return got_error_from_errno("strndup");
2924 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2925 if (err) {
2926 free(s);
2927 return err;
2929 waddwstr(window, wline);
2930 free(wline);
2931 free(s);
2932 wlimit -= width;
2933 *wtotal += width;
2935 if (wlimit > 0) {
2936 s = strndup(line + regmatch->rm_so,
2937 regmatch->rm_eo - regmatch->rm_so);
2938 if (s == NULL) {
2939 err = got_error_from_errno("strndup");
2940 free(s);
2941 return err;
2943 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2944 if (err) {
2945 free(s);
2946 return err;
2948 wattr_on(window, A_STANDOUT, NULL);
2949 waddwstr(window, wline);
2950 wattr_off(window, A_STANDOUT, NULL);
2951 free(wline);
2952 free(s);
2953 wlimit -= width;
2954 *wtotal += width;
2957 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2958 err = format_line(&wline, &width,
2959 line + regmatch->rm_eo, wlimit, col_tab_align);
2960 if (err)
2961 return err;
2962 waddwstr(window, wline);
2963 free(wline);
2964 *wtotal += width;
2967 return NULL;
2970 static const struct got_error *
2971 draw_file(struct tog_view *view, const char *header)
2973 struct tog_diff_view_state *s = &view->state.diff;
2974 regmatch_t *regmatch = &view->regmatch;
2975 const struct got_error *err;
2976 int nprinted = 0;
2977 char *line;
2978 size_t linesize = 0;
2979 ssize_t linelen;
2980 struct tog_color *tc;
2981 wchar_t *wline;
2982 int width;
2983 int max_lines = view->nlines;
2984 int nlines = s->nlines;
2985 off_t line_offset;
2987 line_offset = s->line_offsets[s->first_displayed_line - 1];
2988 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2989 return got_error_from_errno("fseek");
2991 werase(view->window);
2993 if (header) {
2994 if (asprintf(&line, "[%d/%d] %s",
2995 s->first_displayed_line - 1 + s->selected_line, nlines,
2996 header) == -1)
2997 return got_error_from_errno("asprintf");
2998 err = format_line(&wline, &width, line, view->ncols, 0);
2999 free(line);
3000 if (err)
3001 return err;
3003 if (view_needs_focus_indication(view))
3004 wstandout(view->window);
3005 waddwstr(view->window, wline);
3006 free(wline);
3007 wline = NULL;
3008 if (view_needs_focus_indication(view))
3009 wstandend(view->window);
3010 if (width <= view->ncols - 1)
3011 waddch(view->window, '\n');
3013 if (max_lines <= 1)
3014 return NULL;
3015 max_lines--;
3018 s->eof = 0;
3019 line = NULL;
3020 while (max_lines > 0 && nprinted < max_lines) {
3021 linelen = getline(&line, &linesize, s->f);
3022 if (linelen == -1) {
3023 if (feof(s->f)) {
3024 s->eof = 1;
3025 break;
3027 free(line);
3028 return got_ferror(s->f, GOT_ERR_IO);
3031 tc = match_color(&s->colors, line);
3032 if (tc)
3033 wattr_on(view->window,
3034 COLOR_PAIR(tc->colorpair), NULL);
3035 if (s->first_displayed_line + nprinted == s->matched_line &&
3036 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3037 err = add_matched_line(&width, line, view->ncols, 0,
3038 view->window, regmatch);
3039 if (err) {
3040 free(line);
3041 return err;
3043 } else {
3044 err = format_line(&wline, &width, line, view->ncols, 0);
3045 if (err) {
3046 free(line);
3047 return err;
3049 waddwstr(view->window, wline);
3050 free(wline);
3051 wline = NULL;
3053 if (tc)
3054 wattr_off(view->window,
3055 COLOR_PAIR(tc->colorpair), NULL);
3056 if (width <= view->ncols - 1)
3057 waddch(view->window, '\n');
3058 nprinted++;
3060 free(line);
3061 if (nprinted >= 1)
3062 s->last_displayed_line = s->first_displayed_line +
3063 (nprinted - 1);
3064 else
3065 s->last_displayed_line = s->first_displayed_line;
3067 view_vborder(view);
3069 if (s->eof) {
3070 while (nprinted < view->nlines) {
3071 waddch(view->window, '\n');
3072 nprinted++;
3075 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3076 if (err) {
3077 return err;
3080 wstandout(view->window);
3081 waddwstr(view->window, wline);
3082 free(wline);
3083 wline = NULL;
3084 wstandend(view->window);
3087 return NULL;
3090 static char *
3091 get_datestr(time_t *time, char *datebuf)
3093 struct tm mytm, *tm;
3094 char *p, *s;
3096 tm = gmtime_r(time, &mytm);
3097 if (tm == NULL)
3098 return NULL;
3099 s = asctime_r(tm, datebuf);
3100 if (s == NULL)
3101 return NULL;
3102 p = strchr(s, '\n');
3103 if (p)
3104 *p = '\0';
3105 return s;
3108 static const struct got_error *
3109 get_changed_paths(struct got_pathlist_head *paths,
3110 struct got_commit_object *commit, struct got_repository *repo)
3112 const struct got_error *err = NULL;
3113 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3114 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3115 struct got_object_qid *qid;
3117 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3118 if (qid != NULL) {
3119 struct got_commit_object *pcommit;
3120 err = got_object_open_as_commit(&pcommit, repo,
3121 qid->id);
3122 if (err)
3123 return err;
3125 tree_id1 = got_object_id_dup(
3126 got_object_commit_get_tree_id(pcommit));
3127 if (tree_id1 == NULL) {
3128 got_object_commit_close(pcommit);
3129 return got_error_from_errno("got_object_id_dup");
3131 got_object_commit_close(pcommit);
3135 if (tree_id1) {
3136 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3137 if (err)
3138 goto done;
3141 tree_id2 = got_object_commit_get_tree_id(commit);
3142 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3143 if (err)
3144 goto done;
3146 err = got_diff_tree(tree1, tree2, "", "", repo,
3147 got_diff_tree_collect_changed_paths, paths, 0);
3148 done:
3149 if (tree1)
3150 got_object_tree_close(tree1);
3151 if (tree2)
3152 got_object_tree_close(tree2);
3153 free(tree_id1);
3154 return err;
3157 static const struct got_error *
3158 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3160 off_t *p;
3162 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3163 if (p == NULL)
3164 return got_error_from_errno("reallocarray");
3165 *line_offsets = p;
3166 (*line_offsets)[*nlines] = off;
3167 (*nlines)++;
3168 return NULL;
3171 static const struct got_error *
3172 write_commit_info(off_t **line_offsets, size_t *nlines,
3173 struct got_object_id *commit_id, struct got_reflist_head *refs,
3174 struct got_repository *repo, FILE *outfile)
3176 const struct got_error *err = NULL;
3177 char datebuf[26], *datestr;
3178 struct got_commit_object *commit;
3179 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3180 time_t committer_time;
3181 const char *author, *committer;
3182 char *refs_str = NULL;
3183 struct got_pathlist_head changed_paths;
3184 struct got_pathlist_entry *pe;
3185 off_t outoff = 0;
3186 int n;
3188 TAILQ_INIT(&changed_paths);
3190 if (refs) {
3191 err = build_refs_str(&refs_str, refs, commit_id, repo);
3192 if (err)
3193 return err;
3196 err = got_object_open_as_commit(&commit, repo, commit_id);
3197 if (err)
3198 return err;
3200 err = got_object_id_str(&id_str, commit_id);
3201 if (err) {
3202 err = got_error_from_errno("got_object_id_str");
3203 goto done;
3206 err = add_line_offset(line_offsets, nlines, 0);
3207 if (err)
3208 goto done;
3210 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3211 refs_str ? refs_str : "", refs_str ? ")" : "");
3212 if (n < 0) {
3213 err = got_error_from_errno("fprintf");
3214 goto done;
3216 outoff += n;
3217 err = add_line_offset(line_offsets, nlines, outoff);
3218 if (err)
3219 goto done;
3221 n = fprintf(outfile, "from: %s\n",
3222 got_object_commit_get_author(commit));
3223 if (n < 0) {
3224 err = got_error_from_errno("fprintf");
3225 goto done;
3227 outoff += n;
3228 err = add_line_offset(line_offsets, nlines, outoff);
3229 if (err)
3230 goto done;
3232 committer_time = got_object_commit_get_committer_time(commit);
3233 datestr = get_datestr(&committer_time, datebuf);
3234 if (datestr) {
3235 n = fprintf(outfile, "date: %s UTC\n", datestr);
3236 if (n < 0) {
3237 err = got_error_from_errno("fprintf");
3238 goto done;
3240 outoff += n;
3241 err = add_line_offset(line_offsets, nlines, outoff);
3242 if (err)
3243 goto done;
3245 author = got_object_commit_get_author(commit);
3246 committer = got_object_commit_get_committer(commit);
3247 if (strcmp(author, committer) != 0) {
3248 n = fprintf(outfile, "via: %s\n", committer);
3249 if (n < 0) {
3250 err = got_error_from_errno("fprintf");
3251 goto done;
3253 outoff += n;
3254 err = add_line_offset(line_offsets, nlines, outoff);
3255 if (err)
3256 goto done;
3258 if (got_object_commit_get_nparents(commit) > 1) {
3259 const struct got_object_id_queue *parent_ids;
3260 struct got_object_qid *qid;
3261 int pn = 1;
3262 parent_ids = got_object_commit_get_parent_ids(commit);
3263 STAILQ_FOREACH(qid, parent_ids, entry) {
3264 err = got_object_id_str(&id_str, qid->id);
3265 if (err)
3266 goto done;
3267 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3268 if (n < 0) {
3269 err = got_error_from_errno("fprintf");
3270 goto done;
3272 outoff += n;
3273 err = add_line_offset(line_offsets, nlines, outoff);
3274 if (err)
3275 goto done;
3276 free(id_str);
3277 id_str = NULL;
3281 err = got_object_commit_get_logmsg(&logmsg, commit);
3282 if (err)
3283 goto done;
3284 s = logmsg;
3285 while ((line = strsep(&s, "\n")) != NULL) {
3286 n = fprintf(outfile, "%s\n", line);
3287 if (n < 0) {
3288 err = got_error_from_errno("fprintf");
3289 goto done;
3291 outoff += n;
3292 err = add_line_offset(line_offsets, nlines, outoff);
3293 if (err)
3294 goto done;
3297 err = get_changed_paths(&changed_paths, commit, repo);
3298 if (err)
3299 goto done;
3300 TAILQ_FOREACH(pe, &changed_paths, entry) {
3301 struct got_diff_changed_path *cp = pe->data;
3302 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3303 if (n < 0) {
3304 err = got_error_from_errno("fprintf");
3305 goto done;
3307 outoff += n;
3308 err = add_line_offset(line_offsets, nlines, outoff);
3309 if (err)
3310 goto done;
3311 free((char *)pe->path);
3312 free(pe->data);
3315 fputc('\n', outfile);
3316 outoff++;
3317 err = add_line_offset(line_offsets, nlines, outoff);
3318 done:
3319 got_pathlist_free(&changed_paths);
3320 free(id_str);
3321 free(logmsg);
3322 free(refs_str);
3323 got_object_commit_close(commit);
3324 if (err) {
3325 free(*line_offsets);
3326 *line_offsets = NULL;
3327 *nlines = 0;
3329 return err;
3332 static const struct got_error *
3333 create_diff(struct tog_diff_view_state *s)
3335 const struct got_error *err = NULL;
3336 FILE *f = NULL;
3337 int obj_type;
3339 free(s->line_offsets);
3340 s->line_offsets = malloc(sizeof(off_t));
3341 if (s->line_offsets == NULL)
3342 return got_error_from_errno("malloc");
3343 s->nlines = 0;
3345 f = got_opentemp();
3346 if (f == NULL) {
3347 err = got_error_from_errno("got_opentemp");
3348 goto done;
3350 if (s->f && fclose(s->f) == EOF) {
3351 err = got_error_from_errno("fclose");
3352 goto done;
3354 s->f = f;
3356 if (s->id1)
3357 err = got_object_get_type(&obj_type, s->repo, s->id1);
3358 else
3359 err = got_object_get_type(&obj_type, s->repo, s->id2);
3360 if (err)
3361 goto done;
3363 switch (obj_type) {
3364 case GOT_OBJ_TYPE_BLOB:
3365 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3366 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3367 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3368 break;
3369 case GOT_OBJ_TYPE_TREE:
3370 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3371 s->id1, s->id2, NULL, "", "", s->diff_context,
3372 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3373 break;
3374 case GOT_OBJ_TYPE_COMMIT: {
3375 const struct got_object_id_queue *parent_ids;
3376 struct got_object_qid *pid;
3377 struct got_commit_object *commit2;
3378 struct got_reflist_head *refs;
3380 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3381 if (err)
3382 goto done;
3383 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3384 /* Show commit info if we're diffing to a parent/root commit. */
3385 if (s->id1 == NULL) {
3386 err = write_commit_info(&s->line_offsets, &s->nlines,
3387 s->id2, refs, s->repo, s->f);
3388 if (err)
3389 goto done;
3390 } else {
3391 parent_ids = got_object_commit_get_parent_ids(commit2);
3392 STAILQ_FOREACH(pid, parent_ids, entry) {
3393 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3394 err = write_commit_info(
3395 &s->line_offsets, &s->nlines,
3396 s->id2, refs, s->repo, s->f);
3397 if (err)
3398 goto done;
3399 break;
3403 got_object_commit_close(commit2);
3405 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3406 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3407 s->force_text_diff, s->repo, s->f);
3408 break;
3410 default:
3411 err = got_error(GOT_ERR_OBJ_TYPE);
3412 break;
3414 if (err)
3415 goto done;
3416 done:
3417 if (s->f && fflush(s->f) != 0 && err == NULL)
3418 err = got_error_from_errno("fflush");
3419 return err;
3422 static void
3423 diff_view_indicate_progress(struct tog_view *view)
3425 mvwaddstr(view->window, 0, 0, "diffing...");
3426 update_panels();
3427 doupdate();
3430 static const struct got_error *
3431 search_start_diff_view(struct tog_view *view)
3433 struct tog_diff_view_state *s = &view->state.diff;
3435 s->matched_line = 0;
3436 return NULL;
3439 static const struct got_error *
3440 search_next_diff_view(struct tog_view *view)
3442 struct tog_diff_view_state *s = &view->state.diff;
3443 int lineno;
3444 char *line = NULL;
3445 size_t linesize = 0;
3446 ssize_t linelen;
3448 if (!view->searching) {
3449 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3450 return NULL;
3453 if (s->matched_line) {
3454 if (view->searching == TOG_SEARCH_FORWARD)
3455 lineno = s->matched_line + 1;
3456 else
3457 lineno = s->matched_line - 1;
3458 } else
3459 lineno = s->first_displayed_line;
3461 while (1) {
3462 off_t offset;
3464 if (lineno <= 0 || lineno > s->nlines) {
3465 if (s->matched_line == 0) {
3466 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3467 break;
3470 if (view->searching == TOG_SEARCH_FORWARD)
3471 lineno = 1;
3472 else
3473 lineno = s->nlines;
3476 offset = s->line_offsets[lineno - 1];
3477 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3478 free(line);
3479 return got_error_from_errno("fseeko");
3481 linelen = getline(&line, &linesize, s->f);
3482 if (linelen != -1 &&
3483 match_line(line, &view->regex, 1, &view->regmatch)) {
3484 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3485 s->matched_line = lineno;
3486 break;
3488 if (view->searching == TOG_SEARCH_FORWARD)
3489 lineno++;
3490 else
3491 lineno--;
3493 free(line);
3495 if (s->matched_line) {
3496 s->first_displayed_line = s->matched_line;
3497 s->selected_line = 1;
3500 return NULL;
3503 static const struct got_error *
3504 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3505 struct got_object_id *id2, const char *label1, const char *label2,
3506 int diff_context, int ignore_whitespace, int force_text_diff,
3507 struct tog_view *log_view, struct got_repository *repo)
3509 const struct got_error *err;
3510 struct tog_diff_view_state *s = &view->state.diff;
3512 if (id1 != NULL && id2 != NULL) {
3513 int type1, type2;
3514 err = got_object_get_type(&type1, repo, id1);
3515 if (err)
3516 return err;
3517 err = got_object_get_type(&type2, repo, id2);
3518 if (err)
3519 return err;
3521 if (type1 != type2)
3522 return got_error(GOT_ERR_OBJ_TYPE);
3524 s->first_displayed_line = 1;
3525 s->last_displayed_line = view->nlines;
3526 s->selected_line = 1;
3527 s->repo = repo;
3528 s->id1 = id1;
3529 s->id2 = id2;
3530 s->label1 = label1;
3531 s->label2 = label2;
3533 if (id1) {
3534 s->id1 = got_object_id_dup(id1);
3535 if (s->id1 == NULL)
3536 return got_error_from_errno("got_object_id_dup");
3537 } else
3538 s->id1 = NULL;
3540 s->id2 = got_object_id_dup(id2);
3541 if (s->id2 == NULL) {
3542 free(s->id1);
3543 s->id1 = NULL;
3544 return got_error_from_errno("got_object_id_dup");
3546 s->f = NULL;
3547 s->first_displayed_line = 1;
3548 s->last_displayed_line = view->nlines;
3549 s->diff_context = diff_context;
3550 s->ignore_whitespace = ignore_whitespace;
3551 s->force_text_diff = force_text_diff;
3552 s->log_view = log_view;
3553 s->repo = repo;
3555 STAILQ_INIT(&s->colors);
3556 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3557 err = add_color(&s->colors,
3558 "^-", TOG_COLOR_DIFF_MINUS,
3559 get_color_value("TOG_COLOR_DIFF_MINUS"));
3560 if (err)
3561 return err;
3562 err = add_color(&s->colors, "^\\+",
3563 TOG_COLOR_DIFF_PLUS,
3564 get_color_value("TOG_COLOR_DIFF_PLUS"));
3565 if (err) {
3566 free_colors(&s->colors);
3567 return err;
3569 err = add_color(&s->colors,
3570 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3571 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3572 if (err) {
3573 free_colors(&s->colors);
3574 return err;
3577 err = add_color(&s->colors,
3578 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3579 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3580 get_color_value("TOG_COLOR_DIFF_META"));
3581 if (err) {
3582 free_colors(&s->colors);
3583 return err;
3586 err = add_color(&s->colors,
3587 "^(from|via): ", TOG_COLOR_AUTHOR,
3588 get_color_value("TOG_COLOR_AUTHOR"));
3589 if (err) {
3590 free_colors(&s->colors);
3591 return err;
3594 err = add_color(&s->colors,
3595 "^date: ", TOG_COLOR_DATE,
3596 get_color_value("TOG_COLOR_DATE"));
3597 if (err) {
3598 free_colors(&s->colors);
3599 return err;
3603 if (log_view && view_is_splitscreen(view))
3604 show_log_view(log_view); /* draw vborder */
3605 diff_view_indicate_progress(view);
3607 s->line_offsets = NULL;
3608 s->nlines = 0;
3609 err = create_diff(s);
3610 if (err) {
3611 free(s->id1);
3612 s->id1 = NULL;
3613 free(s->id2);
3614 s->id2 = NULL;
3615 free_colors(&s->colors);
3616 return err;
3619 view->show = show_diff_view;
3620 view->input = input_diff_view;
3621 view->close = close_diff_view;
3622 view->search_start = search_start_diff_view;
3623 view->search_next = search_next_diff_view;
3625 return NULL;
3628 static const struct got_error *
3629 close_diff_view(struct tog_view *view)
3631 const struct got_error *err = NULL;
3632 struct tog_diff_view_state *s = &view->state.diff;
3634 free(s->id1);
3635 s->id1 = NULL;
3636 free(s->id2);
3637 s->id2 = NULL;
3638 if (s->f && fclose(s->f) == EOF)
3639 err = got_error_from_errno("fclose");
3640 free_colors(&s->colors);
3641 free(s->line_offsets);
3642 s->line_offsets = NULL;
3643 s->nlines = 0;
3644 return err;
3647 static const struct got_error *
3648 show_diff_view(struct tog_view *view)
3650 const struct got_error *err;
3651 struct tog_diff_view_state *s = &view->state.diff;
3652 char *id_str1 = NULL, *id_str2, *header;
3653 const char *label1, *label2;
3655 if (s->id1) {
3656 err = got_object_id_str(&id_str1, s->id1);
3657 if (err)
3658 return err;
3659 label1 = s->label1 ? : id_str1;
3660 } else
3661 label1 = "/dev/null";
3663 err = got_object_id_str(&id_str2, s->id2);
3664 if (err)
3665 return err;
3666 label2 = s->label2 ? : id_str2;
3668 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3669 err = got_error_from_errno("asprintf");
3670 free(id_str1);
3671 free(id_str2);
3672 return err;
3674 free(id_str1);
3675 free(id_str2);
3677 err = draw_file(view, header);
3678 free(header);
3679 return err;
3682 static const struct got_error *
3683 set_selected_commit(struct tog_diff_view_state *s,
3684 struct commit_queue_entry *entry)
3686 const struct got_error *err;
3687 const struct got_object_id_queue *parent_ids;
3688 struct got_commit_object *selected_commit;
3689 struct got_object_qid *pid;
3691 free(s->id2);
3692 s->id2 = got_object_id_dup(entry->id);
3693 if (s->id2 == NULL)
3694 return got_error_from_errno("got_object_id_dup");
3696 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3697 if (err)
3698 return err;
3699 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3700 free(s->id1);
3701 pid = STAILQ_FIRST(parent_ids);
3702 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3703 got_object_commit_close(selected_commit);
3704 return NULL;
3707 static const struct got_error *
3708 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3710 const struct got_error *err = NULL;
3711 struct tog_diff_view_state *s = &view->state.diff;
3712 struct tog_log_view_state *ls;
3713 struct commit_queue_entry *old_selected_entry;
3714 char *line = NULL;
3715 size_t linesize = 0;
3716 ssize_t linelen;
3717 int i;
3719 switch (ch) {
3720 case 'a':
3721 case 'w':
3722 if (ch == 'a')
3723 s->force_text_diff = !s->force_text_diff;
3724 if (ch == 'w')
3725 s->ignore_whitespace = !s->ignore_whitespace;
3726 wclear(view->window);
3727 s->first_displayed_line = 1;
3728 s->last_displayed_line = view->nlines;
3729 s->matched_line = 0;
3730 diff_view_indicate_progress(view);
3731 err = create_diff(s);
3732 break;
3733 case 'g':
3734 case KEY_HOME:
3735 s->first_displayed_line = 1;
3736 break;
3737 case 'G':
3738 case KEY_END:
3739 if (s->eof)
3740 break;
3742 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3743 s->eof = 1;
3744 break;
3745 case 'k':
3746 case KEY_UP:
3747 case CTRL('p'):
3748 if (s->first_displayed_line > 1)
3749 s->first_displayed_line--;
3750 break;
3751 case KEY_PPAGE:
3752 case CTRL('b'):
3753 if (s->first_displayed_line == 1)
3754 break;
3755 i = 0;
3756 while (i++ < view->nlines - 1 &&
3757 s->first_displayed_line > 1)
3758 s->first_displayed_line--;
3759 break;
3760 case 'j':
3761 case KEY_DOWN:
3762 case CTRL('n'):
3763 if (!s->eof)
3764 s->first_displayed_line++;
3765 break;
3766 case KEY_NPAGE:
3767 case CTRL('f'):
3768 case ' ':
3769 if (s->eof)
3770 break;
3771 i = 0;
3772 while (!s->eof && i++ < view->nlines - 1) {
3773 linelen = getline(&line, &linesize, s->f);
3774 s->first_displayed_line++;
3775 if (linelen == -1) {
3776 if (feof(s->f)) {
3777 s->eof = 1;
3778 } else
3779 err = got_ferror(s->f, GOT_ERR_IO);
3780 break;
3783 free(line);
3784 break;
3785 case '[':
3786 if (s->diff_context > 0) {
3787 s->diff_context--;
3788 s->matched_line = 0;
3789 diff_view_indicate_progress(view);
3790 err = create_diff(s);
3791 if (s->first_displayed_line + view->nlines - 1 >
3792 s->nlines) {
3793 s->first_displayed_line = 1;
3794 s->last_displayed_line = view->nlines;
3797 break;
3798 case ']':
3799 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3800 s->diff_context++;
3801 s->matched_line = 0;
3802 diff_view_indicate_progress(view);
3803 err = create_diff(s);
3805 break;
3806 case '<':
3807 case ',':
3808 if (s->log_view == NULL)
3809 break;
3810 ls = &s->log_view->state.log;
3811 old_selected_entry = ls->selected_entry;
3813 err = input_log_view(NULL, s->log_view, KEY_UP);
3814 if (err)
3815 break;
3817 if (old_selected_entry == ls->selected_entry)
3818 break;
3820 err = set_selected_commit(s, ls->selected_entry);
3821 if (err)
3822 break;
3824 s->first_displayed_line = 1;
3825 s->last_displayed_line = view->nlines;
3826 s->matched_line = 0;
3828 diff_view_indicate_progress(view);
3829 err = create_diff(s);
3830 break;
3831 case '>':
3832 case '.':
3833 if (s->log_view == NULL)
3834 break;
3835 ls = &s->log_view->state.log;
3836 old_selected_entry = ls->selected_entry;
3838 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3839 if (err)
3840 break;
3842 if (old_selected_entry == ls->selected_entry)
3843 break;
3845 err = set_selected_commit(s, ls->selected_entry);
3846 if (err)
3847 break;
3849 s->first_displayed_line = 1;
3850 s->last_displayed_line = view->nlines;
3851 s->matched_line = 0;
3853 diff_view_indicate_progress(view);
3854 err = create_diff(s);
3855 break;
3856 default:
3857 break;
3860 return err;
3863 static const struct got_error *
3864 cmd_diff(int argc, char *argv[])
3866 const struct got_error *error = NULL;
3867 struct got_repository *repo = NULL;
3868 struct got_worktree *worktree = NULL;
3869 struct got_object_id *id1 = NULL, *id2 = NULL;
3870 char *repo_path = NULL, *cwd = NULL;
3871 char *id_str1 = NULL, *id_str2 = NULL;
3872 char *label1 = NULL, *label2 = NULL;
3873 int diff_context = 3, ignore_whitespace = 0;
3874 int ch, force_text_diff = 0;
3875 const char *errstr;
3876 struct tog_view *view;
3878 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3879 switch (ch) {
3880 case 'a':
3881 force_text_diff = 1;
3882 break;
3883 case 'C':
3884 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3885 &errstr);
3886 if (errstr != NULL)
3887 err(1, "-C option %s", errstr);
3888 break;
3889 case 'r':
3890 repo_path = realpath(optarg, NULL);
3891 if (repo_path == NULL)
3892 return got_error_from_errno2("realpath",
3893 optarg);
3894 got_path_strip_trailing_slashes(repo_path);
3895 break;
3896 case 'w':
3897 ignore_whitespace = 1;
3898 break;
3899 default:
3900 usage_diff();
3901 /* NOTREACHED */
3905 argc -= optind;
3906 argv += optind;
3908 if (argc == 0) {
3909 usage_diff(); /* TODO show local worktree changes */
3910 } else if (argc == 2) {
3911 id_str1 = argv[0];
3912 id_str2 = argv[1];
3913 } else
3914 usage_diff();
3916 if (repo_path == NULL) {
3917 cwd = getcwd(NULL, 0);
3918 if (cwd == NULL)
3919 return got_error_from_errno("getcwd");
3920 error = got_worktree_open(&worktree, cwd);
3921 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3922 goto done;
3923 if (worktree)
3924 repo_path =
3925 strdup(got_worktree_get_repo_path(worktree));
3926 else
3927 repo_path = strdup(cwd);
3928 if (repo_path == NULL) {
3929 error = got_error_from_errno("strdup");
3930 goto done;
3934 error = got_repo_open(&repo, repo_path, NULL);
3935 if (error)
3936 goto done;
3938 init_curses();
3940 error = apply_unveil(got_repo_get_path(repo), NULL);
3941 if (error)
3942 goto done;
3944 error = tog_load_refs(repo, 0);
3945 if (error)
3946 goto done;
3948 error = got_repo_match_object_id(&id1, &label1, id_str1,
3949 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3950 if (error)
3951 goto done;
3953 error = got_repo_match_object_id(&id2, &label2, id_str2,
3954 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3955 if (error)
3956 goto done;
3958 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3959 if (view == NULL) {
3960 error = got_error_from_errno("view_open");
3961 goto done;
3963 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3964 ignore_whitespace, force_text_diff, NULL, repo);
3965 if (error)
3966 goto done;
3967 error = view_loop(view);
3968 done:
3969 free(label1);
3970 free(label2);
3971 free(repo_path);
3972 free(cwd);
3973 if (repo) {
3974 const struct got_error *close_err = got_repo_close(repo);
3975 if (error == NULL)
3976 error = close_err;
3978 if (worktree)
3979 got_worktree_close(worktree);
3980 tog_free_refs();
3981 return error;
3984 __dead static void
3985 usage_blame(void)
3987 endwin();
3988 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3989 getprogname());
3990 exit(1);
3993 struct tog_blame_line {
3994 int annotated;
3995 struct got_object_id *id;
3998 static const struct got_error *
3999 draw_blame(struct tog_view *view)
4001 struct tog_blame_view_state *s = &view->state.blame;
4002 struct tog_blame *blame = &s->blame;
4003 regmatch_t *regmatch = &view->regmatch;
4004 const struct got_error *err;
4005 int lineno = 0, nprinted = 0;
4006 char *line = NULL;
4007 size_t linesize = 0;
4008 ssize_t linelen;
4009 wchar_t *wline;
4010 int width;
4011 struct tog_blame_line *blame_line;
4012 struct got_object_id *prev_id = NULL;
4013 char *id_str;
4014 struct tog_color *tc;
4016 err = got_object_id_str(&id_str, s->blamed_commit->id);
4017 if (err)
4018 return err;
4020 rewind(blame->f);
4021 werase(view->window);
4023 if (asprintf(&line, "commit %s", id_str) == -1) {
4024 err = got_error_from_errno("asprintf");
4025 free(id_str);
4026 return err;
4029 err = format_line(&wline, &width, line, view->ncols, 0);
4030 free(line);
4031 line = NULL;
4032 if (err)
4033 return err;
4034 if (view_needs_focus_indication(view))
4035 wstandout(view->window);
4036 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4037 if (tc)
4038 wattr_on(view->window,
4039 COLOR_PAIR(tc->colorpair), NULL);
4040 waddwstr(view->window, wline);
4041 if (tc)
4042 wattr_off(view->window,
4043 COLOR_PAIR(tc->colorpair), NULL);
4044 if (view_needs_focus_indication(view))
4045 wstandend(view->window);
4046 free(wline);
4047 wline = NULL;
4048 if (width < view->ncols - 1)
4049 waddch(view->window, '\n');
4051 if (asprintf(&line, "[%d/%d] %s%s",
4052 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4053 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4054 free(id_str);
4055 return got_error_from_errno("asprintf");
4057 free(id_str);
4058 err = format_line(&wline, &width, line, view->ncols, 0);
4059 free(line);
4060 line = NULL;
4061 if (err)
4062 return err;
4063 waddwstr(view->window, wline);
4064 free(wline);
4065 wline = NULL;
4066 if (width < view->ncols - 1)
4067 waddch(view->window, '\n');
4069 s->eof = 0;
4070 while (nprinted < view->nlines - 2) {
4071 linelen = getline(&line, &linesize, blame->f);
4072 if (linelen == -1) {
4073 if (feof(blame->f)) {
4074 s->eof = 1;
4075 break;
4077 free(line);
4078 return got_ferror(blame->f, GOT_ERR_IO);
4080 if (++lineno < s->first_displayed_line)
4081 continue;
4083 if (view->focussed && nprinted == s->selected_line - 1)
4084 wstandout(view->window);
4086 if (blame->nlines > 0) {
4087 blame_line = &blame->lines[lineno - 1];
4088 if (blame_line->annotated && prev_id &&
4089 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4090 !(view->focussed &&
4091 nprinted == s->selected_line - 1)) {
4092 waddstr(view->window, " ");
4093 } else if (blame_line->annotated) {
4094 char *id_str;
4095 err = got_object_id_str(&id_str, blame_line->id);
4096 if (err) {
4097 free(line);
4098 return err;
4100 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4101 if (tc)
4102 wattr_on(view->window,
4103 COLOR_PAIR(tc->colorpair), NULL);
4104 wprintw(view->window, "%.8s", id_str);
4105 if (tc)
4106 wattr_off(view->window,
4107 COLOR_PAIR(tc->colorpair), NULL);
4108 free(id_str);
4109 prev_id = blame_line->id;
4110 } else {
4111 waddstr(view->window, "........");
4112 prev_id = NULL;
4114 } else {
4115 waddstr(view->window, "........");
4116 prev_id = NULL;
4119 if (view->focussed && nprinted == s->selected_line - 1)
4120 wstandend(view->window);
4121 waddstr(view->window, " ");
4123 if (view->ncols <= 9) {
4124 width = 9;
4125 wline = wcsdup(L"");
4126 if (wline == NULL) {
4127 err = got_error_from_errno("wcsdup");
4128 free(line);
4129 return err;
4131 } else if (s->first_displayed_line + nprinted ==
4132 s->matched_line &&
4133 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4134 err = add_matched_line(&width, line, view->ncols - 9, 9,
4135 view->window, regmatch);
4136 if (err) {
4137 free(line);
4138 return err;
4140 width += 9;
4141 } else {
4142 err = format_line(&wline, &width, line,
4143 view->ncols - 9, 9);
4144 waddwstr(view->window, wline);
4145 free(wline);
4146 wline = NULL;
4147 width += 9;
4150 if (width <= view->ncols - 1)
4151 waddch(view->window, '\n');
4152 if (++nprinted == 1)
4153 s->first_displayed_line = lineno;
4155 free(line);
4156 s->last_displayed_line = lineno;
4158 view_vborder(view);
4160 return NULL;
4163 static const struct got_error *
4164 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4166 const struct got_error *err = NULL;
4167 struct tog_blame_cb_args *a = arg;
4168 struct tog_blame_line *line;
4169 int errcode;
4171 if (nlines != a->nlines ||
4172 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4173 return got_error(GOT_ERR_RANGE);
4175 errcode = pthread_mutex_lock(&tog_mutex);
4176 if (errcode)
4177 return got_error_set_errno(errcode, "pthread_mutex_lock");
4179 if (*a->quit) { /* user has quit the blame view */
4180 err = got_error(GOT_ERR_ITER_COMPLETED);
4181 goto done;
4184 if (lineno == -1)
4185 goto done; /* no change in this commit */
4187 line = &a->lines[lineno - 1];
4188 if (line->annotated)
4189 goto done;
4191 line->id = got_object_id_dup(id);
4192 if (line->id == NULL) {
4193 err = got_error_from_errno("got_object_id_dup");
4194 goto done;
4196 line->annotated = 1;
4197 done:
4198 errcode = pthread_mutex_unlock(&tog_mutex);
4199 if (errcode)
4200 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4201 return err;
4204 static void *
4205 blame_thread(void *arg)
4207 const struct got_error *err, *close_err;
4208 struct tog_blame_thread_args *ta = arg;
4209 struct tog_blame_cb_args *a = ta->cb_args;
4210 int errcode;
4212 err = block_signals_used_by_main_thread();
4213 if (err)
4214 return (void *)err;
4216 err = got_blame(ta->path, a->commit_id, ta->repo,
4217 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4218 if (err && err->code == GOT_ERR_CANCELLED)
4219 err = NULL;
4221 errcode = pthread_mutex_lock(&tog_mutex);
4222 if (errcode)
4223 return (void *)got_error_set_errno(errcode,
4224 "pthread_mutex_lock");
4226 close_err = got_repo_close(ta->repo);
4227 if (err == NULL)
4228 err = close_err;
4229 ta->repo = NULL;
4230 *ta->complete = 1;
4232 errcode = pthread_mutex_unlock(&tog_mutex);
4233 if (errcode && err == NULL)
4234 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4236 return (void *)err;
4239 static struct got_object_id *
4240 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4241 int first_displayed_line, int selected_line)
4243 struct tog_blame_line *line;
4245 if (nlines <= 0)
4246 return NULL;
4248 line = &lines[first_displayed_line - 1 + selected_line - 1];
4249 if (!line->annotated)
4250 return NULL;
4252 return line->id;
4255 static const struct got_error *
4256 stop_blame(struct tog_blame *blame)
4258 const struct got_error *err = NULL;
4259 int i;
4261 if (blame->thread) {
4262 int errcode;
4263 errcode = pthread_mutex_unlock(&tog_mutex);
4264 if (errcode)
4265 return got_error_set_errno(errcode,
4266 "pthread_mutex_unlock");
4267 errcode = pthread_join(blame->thread, (void **)&err);
4268 if (errcode)
4269 return got_error_set_errno(errcode, "pthread_join");
4270 errcode = pthread_mutex_lock(&tog_mutex);
4271 if (errcode)
4272 return got_error_set_errno(errcode,
4273 "pthread_mutex_lock");
4274 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4275 err = NULL;
4276 blame->thread = NULL;
4278 if (blame->thread_args.repo) {
4279 const struct got_error *close_err;
4280 close_err = got_repo_close(blame->thread_args.repo);
4281 if (err == NULL)
4282 err = close_err;
4283 blame->thread_args.repo = NULL;
4285 if (blame->f) {
4286 if (fclose(blame->f) == EOF && err == NULL)
4287 err = got_error_from_errno("fclose");
4288 blame->f = NULL;
4290 if (blame->lines) {
4291 for (i = 0; i < blame->nlines; i++)
4292 free(blame->lines[i].id);
4293 free(blame->lines);
4294 blame->lines = NULL;
4296 free(blame->cb_args.commit_id);
4297 blame->cb_args.commit_id = NULL;
4299 return err;
4302 static const struct got_error *
4303 cancel_blame_view(void *arg)
4305 const struct got_error *err = NULL;
4306 int *done = arg;
4307 int errcode;
4309 errcode = pthread_mutex_lock(&tog_mutex);
4310 if (errcode)
4311 return got_error_set_errno(errcode,
4312 "pthread_mutex_unlock");
4314 if (*done)
4315 err = got_error(GOT_ERR_CANCELLED);
4317 errcode = pthread_mutex_unlock(&tog_mutex);
4318 if (errcode)
4319 return got_error_set_errno(errcode,
4320 "pthread_mutex_lock");
4322 return err;
4325 static const struct got_error *
4326 run_blame(struct tog_view *view)
4328 struct tog_blame_view_state *s = &view->state.blame;
4329 struct tog_blame *blame = &s->blame;
4330 const struct got_error *err = NULL;
4331 struct got_blob_object *blob = NULL;
4332 struct got_repository *thread_repo = NULL;
4333 struct got_object_id *obj_id = NULL;
4334 int obj_type;
4336 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4337 s->path);
4338 if (err)
4339 return err;
4341 err = got_object_get_type(&obj_type, s->repo, obj_id);
4342 if (err)
4343 goto done;
4345 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4346 err = got_error(GOT_ERR_OBJ_TYPE);
4347 goto done;
4350 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4351 if (err)
4352 goto done;
4353 blame->f = got_opentemp();
4354 if (blame->f == NULL) {
4355 err = got_error_from_errno("got_opentemp");
4356 goto done;
4358 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4359 &blame->line_offsets, blame->f, blob);
4360 if (err)
4361 goto done;
4362 if (blame->nlines == 0) {
4363 s->blame_complete = 1;
4364 goto done;
4367 /* Don't include \n at EOF in the blame line count. */
4368 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4369 blame->nlines--;
4371 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4372 if (blame->lines == NULL) {
4373 err = got_error_from_errno("calloc");
4374 goto done;
4377 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4378 if (err)
4379 goto done;
4381 blame->cb_args.view = view;
4382 blame->cb_args.lines = blame->lines;
4383 blame->cb_args.nlines = blame->nlines;
4384 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4385 if (blame->cb_args.commit_id == NULL) {
4386 err = got_error_from_errno("got_object_id_dup");
4387 goto done;
4389 blame->cb_args.quit = &s->done;
4391 blame->thread_args.path = s->path;
4392 blame->thread_args.repo = thread_repo;
4393 blame->thread_args.cb_args = &blame->cb_args;
4394 blame->thread_args.complete = &s->blame_complete;
4395 blame->thread_args.cancel_cb = cancel_blame_view;
4396 blame->thread_args.cancel_arg = &s->done;
4397 s->blame_complete = 0;
4399 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4400 s->first_displayed_line = 1;
4401 s->last_displayed_line = view->nlines;
4402 s->selected_line = 1;
4404 s->matched_line = 0;
4406 done:
4407 if (blob)
4408 got_object_blob_close(blob);
4409 free(obj_id);
4410 if (err)
4411 stop_blame(blame);
4412 return err;
4415 static const struct got_error *
4416 open_blame_view(struct tog_view *view, char *path,
4417 struct got_object_id *commit_id, struct got_repository *repo)
4419 const struct got_error *err = NULL;
4420 struct tog_blame_view_state *s = &view->state.blame;
4422 STAILQ_INIT(&s->blamed_commits);
4424 s->path = strdup(path);
4425 if (s->path == NULL)
4426 return got_error_from_errno("strdup");
4428 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4429 if (err) {
4430 free(s->path);
4431 return err;
4434 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4435 s->first_displayed_line = 1;
4436 s->last_displayed_line = view->nlines;
4437 s->selected_line = 1;
4438 s->blame_complete = 0;
4439 s->repo = repo;
4440 s->commit_id = commit_id;
4441 memset(&s->blame, 0, sizeof(s->blame));
4443 STAILQ_INIT(&s->colors);
4444 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4445 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4446 get_color_value("TOG_COLOR_COMMIT"));
4447 if (err)
4448 return err;
4451 view->show = show_blame_view;
4452 view->input = input_blame_view;
4453 view->close = close_blame_view;
4454 view->search_start = search_start_blame_view;
4455 view->search_next = search_next_blame_view;
4457 return run_blame(view);
4460 static const struct got_error *
4461 close_blame_view(struct tog_view *view)
4463 const struct got_error *err = NULL;
4464 struct tog_blame_view_state *s = &view->state.blame;
4466 if (s->blame.thread)
4467 err = stop_blame(&s->blame);
4469 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4470 struct got_object_qid *blamed_commit;
4471 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4472 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4473 got_object_qid_free(blamed_commit);
4476 free(s->path);
4477 free_colors(&s->colors);
4479 return err;
4482 static const struct got_error *
4483 search_start_blame_view(struct tog_view *view)
4485 struct tog_blame_view_state *s = &view->state.blame;
4487 s->matched_line = 0;
4488 return NULL;
4491 static const struct got_error *
4492 search_next_blame_view(struct tog_view *view)
4494 struct tog_blame_view_state *s = &view->state.blame;
4495 int lineno;
4496 char *line = NULL;
4497 size_t linesize = 0;
4498 ssize_t linelen;
4500 if (!view->searching) {
4501 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4502 return NULL;
4505 if (s->matched_line) {
4506 if (view->searching == TOG_SEARCH_FORWARD)
4507 lineno = s->matched_line + 1;
4508 else
4509 lineno = s->matched_line - 1;
4510 } else
4511 lineno = s->first_displayed_line - 1 + s->selected_line;
4513 while (1) {
4514 off_t offset;
4516 if (lineno <= 0 || lineno > s->blame.nlines) {
4517 if (s->matched_line == 0) {
4518 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4519 break;
4522 if (view->searching == TOG_SEARCH_FORWARD)
4523 lineno = 1;
4524 else
4525 lineno = s->blame.nlines;
4528 offset = s->blame.line_offsets[lineno - 1];
4529 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4530 free(line);
4531 return got_error_from_errno("fseeko");
4533 linelen = getline(&line, &linesize, s->blame.f);
4534 if (linelen != -1 &&
4535 match_line(line, &view->regex, 1, &view->regmatch)) {
4536 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4537 s->matched_line = lineno;
4538 break;
4540 if (view->searching == TOG_SEARCH_FORWARD)
4541 lineno++;
4542 else
4543 lineno--;
4545 free(line);
4547 if (s->matched_line) {
4548 s->first_displayed_line = s->matched_line;
4549 s->selected_line = 1;
4552 return NULL;
4555 static const struct got_error *
4556 show_blame_view(struct tog_view *view)
4558 const struct got_error *err = NULL;
4559 struct tog_blame_view_state *s = &view->state.blame;
4560 int errcode;
4562 if (s->blame.thread == NULL && !s->blame_complete) {
4563 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4564 &s->blame.thread_args);
4565 if (errcode)
4566 return got_error_set_errno(errcode, "pthread_create");
4568 halfdelay(1); /* fast refresh while annotating */
4571 if (s->blame_complete)
4572 halfdelay(10); /* disable fast refresh */
4574 err = draw_blame(view);
4576 view_vborder(view);
4577 return err;
4580 static const struct got_error *
4581 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4583 const struct got_error *err = NULL, *thread_err = NULL;
4584 struct tog_view *diff_view;
4585 struct tog_blame_view_state *s = &view->state.blame;
4586 int begin_x = 0;
4588 switch (ch) {
4589 case 'q':
4590 s->done = 1;
4591 break;
4592 case 'g':
4593 case KEY_HOME:
4594 s->selected_line = 1;
4595 s->first_displayed_line = 1;
4596 break;
4597 case 'G':
4598 case KEY_END:
4599 if (s->blame.nlines < view->nlines - 2) {
4600 s->selected_line = s->blame.nlines;
4601 s->first_displayed_line = 1;
4602 } else {
4603 s->selected_line = view->nlines - 2;
4604 s->first_displayed_line = s->blame.nlines -
4605 (view->nlines - 3);
4607 break;
4608 case 'k':
4609 case KEY_UP:
4610 case CTRL('p'):
4611 if (s->selected_line > 1)
4612 s->selected_line--;
4613 else if (s->selected_line == 1 &&
4614 s->first_displayed_line > 1)
4615 s->first_displayed_line--;
4616 break;
4617 case KEY_PPAGE:
4618 case CTRL('b'):
4619 if (s->first_displayed_line == 1) {
4620 s->selected_line = 1;
4621 break;
4623 if (s->first_displayed_line > view->nlines - 2)
4624 s->first_displayed_line -=
4625 (view->nlines - 2);
4626 else
4627 s->first_displayed_line = 1;
4628 break;
4629 case 'j':
4630 case KEY_DOWN:
4631 case CTRL('n'):
4632 if (s->selected_line < view->nlines - 2 &&
4633 s->first_displayed_line +
4634 s->selected_line <= s->blame.nlines)
4635 s->selected_line++;
4636 else if (s->last_displayed_line <
4637 s->blame.nlines)
4638 s->first_displayed_line++;
4639 break;
4640 case 'b':
4641 case 'p': {
4642 struct got_object_id *id = NULL;
4643 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4644 s->first_displayed_line, s->selected_line);
4645 if (id == NULL)
4646 break;
4647 if (ch == 'p') {
4648 struct got_commit_object *commit;
4649 struct got_object_qid *pid;
4650 struct got_object_id *blob_id = NULL;
4651 int obj_type;
4652 err = got_object_open_as_commit(&commit,
4653 s->repo, id);
4654 if (err)
4655 break;
4656 pid = STAILQ_FIRST(
4657 got_object_commit_get_parent_ids(commit));
4658 if (pid == NULL) {
4659 got_object_commit_close(commit);
4660 break;
4662 /* Check if path history ends here. */
4663 err = got_object_id_by_path(&blob_id, s->repo,
4664 pid->id, s->path);
4665 if (err) {
4666 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4667 err = NULL;
4668 got_object_commit_close(commit);
4669 break;
4671 err = got_object_get_type(&obj_type, s->repo,
4672 blob_id);
4673 free(blob_id);
4674 /* Can't blame non-blob type objects. */
4675 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4676 got_object_commit_close(commit);
4677 break;
4679 err = got_object_qid_alloc(&s->blamed_commit,
4680 pid->id);
4681 got_object_commit_close(commit);
4682 } else {
4683 if (got_object_id_cmp(id,
4684 s->blamed_commit->id) == 0)
4685 break;
4686 err = got_object_qid_alloc(&s->blamed_commit,
4687 id);
4689 if (err)
4690 break;
4691 s->done = 1;
4692 thread_err = stop_blame(&s->blame);
4693 s->done = 0;
4694 if (thread_err)
4695 break;
4696 STAILQ_INSERT_HEAD(&s->blamed_commits,
4697 s->blamed_commit, entry);
4698 err = run_blame(view);
4699 if (err)
4700 break;
4701 break;
4703 case 'B': {
4704 struct got_object_qid *first;
4705 first = STAILQ_FIRST(&s->blamed_commits);
4706 if (!got_object_id_cmp(first->id, s->commit_id))
4707 break;
4708 s->done = 1;
4709 thread_err = stop_blame(&s->blame);
4710 s->done = 0;
4711 if (thread_err)
4712 break;
4713 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4714 got_object_qid_free(s->blamed_commit);
4715 s->blamed_commit =
4716 STAILQ_FIRST(&s->blamed_commits);
4717 err = run_blame(view);
4718 if (err)
4719 break;
4720 break;
4722 case KEY_ENTER:
4723 case '\r': {
4724 struct got_object_id *id = NULL;
4725 struct got_object_qid *pid;
4726 struct got_commit_object *commit = NULL;
4727 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4728 s->first_displayed_line, s->selected_line);
4729 if (id == NULL)
4730 break;
4731 err = got_object_open_as_commit(&commit, s->repo, id);
4732 if (err)
4733 break;
4734 pid = STAILQ_FIRST(
4735 got_object_commit_get_parent_ids(commit));
4736 if (view_is_parent_view(view))
4737 begin_x = view_split_begin_x(view->begin_x);
4738 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4739 if (diff_view == NULL) {
4740 got_object_commit_close(commit);
4741 err = got_error_from_errno("view_open");
4742 break;
4744 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4745 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4746 got_object_commit_close(commit);
4747 if (err) {
4748 view_close(diff_view);
4749 break;
4751 view->focussed = 0;
4752 diff_view->focussed = 1;
4753 if (view_is_parent_view(view)) {
4754 err = view_close_child(view);
4755 if (err)
4756 break;
4757 view_set_child(view, diff_view);
4758 view->focus_child = 1;
4759 } else
4760 *new_view = diff_view;
4761 if (err)
4762 break;
4763 break;
4765 case KEY_NPAGE:
4766 case CTRL('f'):
4767 case ' ':
4768 if (s->last_displayed_line >= s->blame.nlines &&
4769 s->selected_line >= MIN(s->blame.nlines,
4770 view->nlines - 2)) {
4771 break;
4773 if (s->last_displayed_line >= s->blame.nlines &&
4774 s->selected_line < view->nlines - 2) {
4775 s->selected_line = MIN(s->blame.nlines,
4776 view->nlines - 2);
4777 break;
4779 if (s->last_displayed_line + view->nlines - 2
4780 <= s->blame.nlines)
4781 s->first_displayed_line +=
4782 view->nlines - 2;
4783 else
4784 s->first_displayed_line =
4785 s->blame.nlines -
4786 (view->nlines - 3);
4787 break;
4788 case KEY_RESIZE:
4789 if (s->selected_line > view->nlines - 2) {
4790 s->selected_line = MIN(s->blame.nlines,
4791 view->nlines - 2);
4793 break;
4794 default:
4795 break;
4797 return thread_err ? thread_err : err;
4800 static const struct got_error *
4801 cmd_blame(int argc, char *argv[])
4803 const struct got_error *error;
4804 struct got_repository *repo = NULL;
4805 struct got_worktree *worktree = NULL;
4806 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4807 char *link_target = NULL;
4808 struct got_object_id *commit_id = NULL;
4809 char *commit_id_str = NULL;
4810 int ch;
4811 struct tog_view *view;
4813 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4814 switch (ch) {
4815 case 'c':
4816 commit_id_str = optarg;
4817 break;
4818 case 'r':
4819 repo_path = realpath(optarg, NULL);
4820 if (repo_path == NULL)
4821 return got_error_from_errno2("realpath",
4822 optarg);
4823 break;
4824 default:
4825 usage_blame();
4826 /* NOTREACHED */
4830 argc -= optind;
4831 argv += optind;
4833 if (argc != 1)
4834 usage_blame();
4836 if (repo_path == NULL) {
4837 cwd = getcwd(NULL, 0);
4838 if (cwd == NULL)
4839 return got_error_from_errno("getcwd");
4840 error = got_worktree_open(&worktree, cwd);
4841 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4842 goto done;
4843 if (worktree)
4844 repo_path =
4845 strdup(got_worktree_get_repo_path(worktree));
4846 else
4847 repo_path = strdup(cwd);
4848 if (repo_path == NULL) {
4849 error = got_error_from_errno("strdup");
4850 goto done;
4854 error = got_repo_open(&repo, repo_path, NULL);
4855 if (error != NULL)
4856 goto done;
4858 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4859 worktree);
4860 if (error)
4861 goto done;
4863 init_curses();
4865 error = apply_unveil(got_repo_get_path(repo), NULL);
4866 if (error)
4867 goto done;
4869 error = tog_load_refs(repo, 0);
4870 if (error)
4871 goto done;
4873 if (commit_id_str == NULL) {
4874 struct got_reference *head_ref;
4875 error = got_ref_open(&head_ref, repo, worktree ?
4876 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4877 if (error != NULL)
4878 goto done;
4879 error = got_ref_resolve(&commit_id, repo, head_ref);
4880 got_ref_close(head_ref);
4881 } else {
4882 error = got_repo_match_object_id(&commit_id, NULL,
4883 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4885 if (error != NULL)
4886 goto done;
4888 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4889 if (view == NULL) {
4890 error = got_error_from_errno("view_open");
4891 goto done;
4894 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4895 commit_id, repo);
4896 if (error)
4897 goto done;
4899 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4900 commit_id, repo);
4901 if (error)
4902 goto done;
4903 if (worktree) {
4904 /* Release work tree lock. */
4905 got_worktree_close(worktree);
4906 worktree = NULL;
4908 error = view_loop(view);
4909 done:
4910 free(repo_path);
4911 free(in_repo_path);
4912 free(link_target);
4913 free(cwd);
4914 free(commit_id);
4915 if (worktree)
4916 got_worktree_close(worktree);
4917 if (repo) {
4918 const struct got_error *close_err = got_repo_close(repo);
4919 if (error == NULL)
4920 error = close_err;
4922 tog_free_refs();
4923 return error;
4926 static const struct got_error *
4927 draw_tree_entries(struct tog_view *view, const char *parent_path)
4929 struct tog_tree_view_state *s = &view->state.tree;
4930 const struct got_error *err = NULL;
4931 struct got_tree_entry *te;
4932 wchar_t *wline;
4933 struct tog_color *tc;
4934 int width, n, i, nentries;
4935 int limit = view->nlines;
4937 s->ndisplayed = 0;
4939 werase(view->window);
4941 if (limit == 0)
4942 return NULL;
4944 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4945 if (err)
4946 return err;
4947 if (view_needs_focus_indication(view))
4948 wstandout(view->window);
4949 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4950 if (tc)
4951 wattr_on(view->window,
4952 COLOR_PAIR(tc->colorpair), NULL);
4953 waddwstr(view->window, wline);
4954 if (tc)
4955 wattr_off(view->window,
4956 COLOR_PAIR(tc->colorpair), NULL);
4957 if (view_needs_focus_indication(view))
4958 wstandend(view->window);
4959 free(wline);
4960 wline = NULL;
4961 if (width < view->ncols - 1)
4962 waddch(view->window, '\n');
4963 if (--limit <= 0)
4964 return NULL;
4965 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4966 if (err)
4967 return err;
4968 waddwstr(view->window, wline);
4969 free(wline);
4970 wline = NULL;
4971 if (width < view->ncols - 1)
4972 waddch(view->window, '\n');
4973 if (--limit <= 0)
4974 return NULL;
4975 waddch(view->window, '\n');
4976 if (--limit <= 0)
4977 return NULL;
4979 if (s->first_displayed_entry == NULL) {
4980 te = got_object_tree_get_first_entry(s->tree);
4981 if (s->selected == 0) {
4982 if (view->focussed)
4983 wstandout(view->window);
4984 s->selected_entry = NULL;
4986 waddstr(view->window, " ..\n"); /* parent directory */
4987 if (s->selected == 0 && view->focussed)
4988 wstandend(view->window);
4989 s->ndisplayed++;
4990 if (--limit <= 0)
4991 return NULL;
4992 n = 1;
4993 } else {
4994 n = 0;
4995 te = s->first_displayed_entry;
4998 nentries = got_object_tree_get_nentries(s->tree);
4999 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5000 char *line = NULL, *id_str = NULL, *link_target = NULL;
5001 const char *modestr = "";
5002 mode_t mode;
5004 te = got_object_tree_get_entry(s->tree, i);
5005 mode = got_tree_entry_get_mode(te);
5007 if (s->show_ids) {
5008 err = got_object_id_str(&id_str,
5009 got_tree_entry_get_id(te));
5010 if (err)
5011 return got_error_from_errno(
5012 "got_object_id_str");
5014 if (got_object_tree_entry_is_submodule(te))
5015 modestr = "$";
5016 else if (S_ISLNK(mode)) {
5017 int i;
5019 err = got_tree_entry_get_symlink_target(&link_target,
5020 te, s->repo);
5021 if (err) {
5022 free(id_str);
5023 return err;
5025 for (i = 0; i < strlen(link_target); i++) {
5026 if (!isprint((unsigned char)link_target[i]))
5027 link_target[i] = '?';
5029 modestr = "@";
5031 else if (S_ISDIR(mode))
5032 modestr = "/";
5033 else if (mode & S_IXUSR)
5034 modestr = "*";
5035 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5036 got_tree_entry_get_name(te), modestr,
5037 link_target ? " -> ": "",
5038 link_target ? link_target : "") == -1) {
5039 free(id_str);
5040 free(link_target);
5041 return got_error_from_errno("asprintf");
5043 free(id_str);
5044 free(link_target);
5045 err = format_line(&wline, &width, line, view->ncols, 0);
5046 if (err) {
5047 free(line);
5048 break;
5050 if (n == s->selected) {
5051 if (view->focussed)
5052 wstandout(view->window);
5053 s->selected_entry = te;
5055 tc = match_color(&s->colors, line);
5056 if (tc)
5057 wattr_on(view->window,
5058 COLOR_PAIR(tc->colorpair), NULL);
5059 waddwstr(view->window, wline);
5060 if (tc)
5061 wattr_off(view->window,
5062 COLOR_PAIR(tc->colorpair), NULL);
5063 if (width < view->ncols - 1)
5064 waddch(view->window, '\n');
5065 if (n == s->selected && view->focussed)
5066 wstandend(view->window);
5067 free(line);
5068 free(wline);
5069 wline = NULL;
5070 n++;
5071 s->ndisplayed++;
5072 s->last_displayed_entry = te;
5073 if (--limit <= 0)
5074 break;
5077 return err;
5080 static void
5081 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5083 struct got_tree_entry *te;
5084 int isroot = s->tree == s->root;
5085 int i = 0;
5087 if (s->first_displayed_entry == NULL)
5088 return;
5090 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5091 while (i++ < maxscroll) {
5092 if (te == NULL) {
5093 if (!isroot)
5094 s->first_displayed_entry = NULL;
5095 break;
5097 s->first_displayed_entry = te;
5098 te = got_tree_entry_get_prev(s->tree, te);
5102 static void
5103 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5105 struct got_tree_entry *next, *last;
5106 int n = 0;
5108 if (s->first_displayed_entry)
5109 next = got_tree_entry_get_next(s->tree,
5110 s->first_displayed_entry);
5111 else
5112 next = got_object_tree_get_first_entry(s->tree);
5114 last = s->last_displayed_entry;
5115 while (next && last && n++ < maxscroll) {
5116 last = got_tree_entry_get_next(s->tree, last);
5117 if (last) {
5118 s->first_displayed_entry = next;
5119 next = got_tree_entry_get_next(s->tree, next);
5124 static const struct got_error *
5125 tree_entry_path(char **path, struct tog_parent_trees *parents,
5126 struct got_tree_entry *te)
5128 const struct got_error *err = NULL;
5129 struct tog_parent_tree *pt;
5130 size_t len = 2; /* for leading slash and NUL */
5132 TAILQ_FOREACH(pt, parents, entry)
5133 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5134 + 1 /* slash */;
5135 if (te)
5136 len += strlen(got_tree_entry_get_name(te));
5138 *path = calloc(1, len);
5139 if (path == NULL)
5140 return got_error_from_errno("calloc");
5142 (*path)[0] = '/';
5143 pt = TAILQ_LAST(parents, tog_parent_trees);
5144 while (pt) {
5145 const char *name = got_tree_entry_get_name(pt->selected_entry);
5146 if (strlcat(*path, name, len) >= len) {
5147 err = got_error(GOT_ERR_NO_SPACE);
5148 goto done;
5150 if (strlcat(*path, "/", len) >= len) {
5151 err = got_error(GOT_ERR_NO_SPACE);
5152 goto done;
5154 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5156 if (te) {
5157 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5158 err = got_error(GOT_ERR_NO_SPACE);
5159 goto done;
5162 done:
5163 if (err) {
5164 free(*path);
5165 *path = NULL;
5167 return err;
5170 static const struct got_error *
5171 blame_tree_entry(struct tog_view **new_view, int begin_x,
5172 struct got_tree_entry *te, struct tog_parent_trees *parents,
5173 struct got_object_id *commit_id, struct got_repository *repo)
5175 const struct got_error *err = NULL;
5176 char *path;
5177 struct tog_view *blame_view;
5179 *new_view = NULL;
5181 err = tree_entry_path(&path, parents, te);
5182 if (err)
5183 return err;
5185 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5186 if (blame_view == NULL) {
5187 err = got_error_from_errno("view_open");
5188 goto done;
5191 err = open_blame_view(blame_view, path, commit_id, repo);
5192 if (err) {
5193 if (err->code == GOT_ERR_CANCELLED)
5194 err = NULL;
5195 view_close(blame_view);
5196 } else
5197 *new_view = blame_view;
5198 done:
5199 free(path);
5200 return err;
5203 static const struct got_error *
5204 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5205 struct tog_tree_view_state *s)
5207 struct tog_view *log_view;
5208 const struct got_error *err = NULL;
5209 char *path;
5211 *new_view = NULL;
5213 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5214 if (log_view == NULL)
5215 return got_error_from_errno("view_open");
5217 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5218 if (err)
5219 return err;
5221 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5222 path, 0);
5223 if (err)
5224 view_close(log_view);
5225 else
5226 *new_view = log_view;
5227 free(path);
5228 return err;
5231 static const struct got_error *
5232 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5233 const char *head_ref_name, struct got_repository *repo)
5235 const struct got_error *err = NULL;
5236 char *commit_id_str = NULL;
5237 struct tog_tree_view_state *s = &view->state.tree;
5238 struct got_commit_object *commit = NULL;
5240 TAILQ_INIT(&s->parents);
5241 STAILQ_INIT(&s->colors);
5243 s->commit_id = got_object_id_dup(commit_id);
5244 if (s->commit_id == NULL)
5245 return got_error_from_errno("got_object_id_dup");
5247 err = got_object_open_as_commit(&commit, repo, commit_id);
5248 if (err)
5249 goto done;
5252 * The root is opened here and will be closed when the view is closed.
5253 * Any visited subtrees and their path-wise parents are opened and
5254 * closed on demand.
5256 err = got_object_open_as_tree(&s->root, repo,
5257 got_object_commit_get_tree_id(commit));
5258 if (err)
5259 goto done;
5260 s->tree = s->root;
5262 err = got_object_id_str(&commit_id_str, commit_id);
5263 if (err != NULL)
5264 goto done;
5266 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5267 err = got_error_from_errno("asprintf");
5268 goto done;
5271 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5272 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5273 if (head_ref_name) {
5274 s->head_ref_name = strdup(head_ref_name);
5275 if (s->head_ref_name == NULL) {
5276 err = got_error_from_errno("strdup");
5277 goto done;
5280 s->repo = repo;
5282 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5283 err = add_color(&s->colors, "\\$$",
5284 TOG_COLOR_TREE_SUBMODULE,
5285 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5286 if (err)
5287 goto done;
5288 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5289 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5290 if (err)
5291 goto done;
5292 err = add_color(&s->colors, "/$",
5293 TOG_COLOR_TREE_DIRECTORY,
5294 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5295 if (err)
5296 goto done;
5298 err = add_color(&s->colors, "\\*$",
5299 TOG_COLOR_TREE_EXECUTABLE,
5300 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5301 if (err)
5302 goto done;
5304 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5305 get_color_value("TOG_COLOR_COMMIT"));
5306 if (err)
5307 goto done;
5310 view->show = show_tree_view;
5311 view->input = input_tree_view;
5312 view->close = close_tree_view;
5313 view->search_start = search_start_tree_view;
5314 view->search_next = search_next_tree_view;
5315 done:
5316 free(commit_id_str);
5317 if (commit)
5318 got_object_commit_close(commit);
5319 if (err)
5320 close_tree_view(view);
5321 return err;
5324 static const struct got_error *
5325 close_tree_view(struct tog_view *view)
5327 struct tog_tree_view_state *s = &view->state.tree;
5329 free_colors(&s->colors);
5330 free(s->tree_label);
5331 s->tree_label = NULL;
5332 free(s->commit_id);
5333 s->commit_id = NULL;
5334 free(s->head_ref_name);
5335 s->head_ref_name = NULL;
5336 while (!TAILQ_EMPTY(&s->parents)) {
5337 struct tog_parent_tree *parent;
5338 parent = TAILQ_FIRST(&s->parents);
5339 TAILQ_REMOVE(&s->parents, parent, entry);
5340 if (parent->tree != s->root)
5341 got_object_tree_close(parent->tree);
5342 free(parent);
5345 if (s->tree != NULL && s->tree != s->root)
5346 got_object_tree_close(s->tree);
5347 if (s->root)
5348 got_object_tree_close(s->root);
5349 return NULL;
5352 static const struct got_error *
5353 search_start_tree_view(struct tog_view *view)
5355 struct tog_tree_view_state *s = &view->state.tree;
5357 s->matched_entry = NULL;
5358 return NULL;
5361 static int
5362 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5364 regmatch_t regmatch;
5366 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5367 0) == 0;
5370 static const struct got_error *
5371 search_next_tree_view(struct tog_view *view)
5373 struct tog_tree_view_state *s = &view->state.tree;
5374 struct got_tree_entry *te = NULL;
5376 if (!view->searching) {
5377 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5378 return NULL;
5381 if (s->matched_entry) {
5382 if (view->searching == TOG_SEARCH_FORWARD) {
5383 if (s->selected_entry)
5384 te = got_tree_entry_get_next(s->tree,
5385 s->selected_entry);
5386 else
5387 te = got_object_tree_get_first_entry(s->tree);
5388 } else {
5389 if (s->selected_entry == NULL)
5390 te = got_object_tree_get_last_entry(s->tree);
5391 else
5392 te = got_tree_entry_get_prev(s->tree,
5393 s->selected_entry);
5395 } else {
5396 if (s->selected_entry)
5397 te = s->selected_entry;
5398 else if (view->searching == TOG_SEARCH_FORWARD)
5399 te = got_object_tree_get_first_entry(s->tree);
5400 else
5401 te = got_object_tree_get_last_entry(s->tree);
5404 while (1) {
5405 if (te == NULL) {
5406 if (s->matched_entry == NULL) {
5407 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5408 return NULL;
5410 if (view->searching == TOG_SEARCH_FORWARD)
5411 te = got_object_tree_get_first_entry(s->tree);
5412 else
5413 te = got_object_tree_get_last_entry(s->tree);
5416 if (match_tree_entry(te, &view->regex)) {
5417 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5418 s->matched_entry = te;
5419 break;
5422 if (view->searching == TOG_SEARCH_FORWARD)
5423 te = got_tree_entry_get_next(s->tree, te);
5424 else
5425 te = got_tree_entry_get_prev(s->tree, te);
5428 if (s->matched_entry) {
5429 s->first_displayed_entry = s->matched_entry;
5430 s->selected = 0;
5433 return NULL;
5436 static const struct got_error *
5437 show_tree_view(struct tog_view *view)
5439 const struct got_error *err = NULL;
5440 struct tog_tree_view_state *s = &view->state.tree;
5441 char *parent_path;
5443 err = tree_entry_path(&parent_path, &s->parents, NULL);
5444 if (err)
5445 return err;
5447 err = draw_tree_entries(view, parent_path);
5448 free(parent_path);
5450 view_vborder(view);
5451 return err;
5454 static const struct got_error *
5455 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5457 const struct got_error *err = NULL;
5458 struct tog_tree_view_state *s = &view->state.tree;
5459 struct tog_view *log_view, *ref_view;
5460 struct got_tree_entry *te;
5461 int begin_x = 0, n;
5463 switch (ch) {
5464 case 'i':
5465 s->show_ids = !s->show_ids;
5466 break;
5467 case 'l':
5468 if (!s->selected_entry)
5469 break;
5470 if (view_is_parent_view(view))
5471 begin_x = view_split_begin_x(view->begin_x);
5472 err = log_selected_tree_entry(&log_view, begin_x, s);
5473 view->focussed = 0;
5474 log_view->focussed = 1;
5475 if (view_is_parent_view(view)) {
5476 err = view_close_child(view);
5477 if (err)
5478 return err;
5479 view_set_child(view, log_view);
5480 view->focus_child = 1;
5481 } else
5482 *new_view = log_view;
5483 break;
5484 case 'r':
5485 if (view_is_parent_view(view))
5486 begin_x = view_split_begin_x(view->begin_x);
5487 ref_view = view_open(view->nlines, view->ncols,
5488 view->begin_y, begin_x, TOG_VIEW_REF);
5489 if (ref_view == NULL)
5490 return got_error_from_errno("view_open");
5491 err = open_ref_view(ref_view, s->repo);
5492 if (err) {
5493 view_close(ref_view);
5494 return err;
5496 view->focussed = 0;
5497 ref_view->focussed = 1;
5498 if (view_is_parent_view(view)) {
5499 err = view_close_child(view);
5500 if (err)
5501 return err;
5502 view_set_child(view, ref_view);
5503 view->focus_child = 1;
5504 } else
5505 *new_view = ref_view;
5506 break;
5507 case 'g':
5508 case KEY_HOME:
5509 s->selected = 0;
5510 if (s->tree == s->root)
5511 s->first_displayed_entry =
5512 got_object_tree_get_first_entry(s->tree);
5513 else
5514 s->first_displayed_entry = NULL;
5515 break;
5516 case 'G':
5517 case KEY_END:
5518 s->selected = 0;
5519 te = got_object_tree_get_last_entry(s->tree);
5520 for (n = 0; n < view->nlines - 3; n++) {
5521 if (te == NULL) {
5522 if(s->tree != s->root) {
5523 s->first_displayed_entry = NULL;
5524 n++;
5526 break;
5528 s->first_displayed_entry = te;
5529 te = got_tree_entry_get_prev(s->tree, te);
5531 if (n > 0)
5532 s->selected = n - 1;
5533 break;
5534 case 'k':
5535 case KEY_UP:
5536 case CTRL('p'):
5537 if (s->selected > 0) {
5538 s->selected--;
5539 break;
5541 tree_scroll_up(s, 1);
5542 break;
5543 case KEY_PPAGE:
5544 case CTRL('b'):
5545 if (s->tree == s->root) {
5546 if (got_object_tree_get_first_entry(s->tree) ==
5547 s->first_displayed_entry)
5548 s->selected = 0;
5549 } else {
5550 if (s->first_displayed_entry == NULL)
5551 s->selected = 0;
5553 tree_scroll_up(s, MAX(0, view->nlines - 3));
5554 break;
5555 case 'j':
5556 case KEY_DOWN:
5557 case CTRL('n'):
5558 if (s->selected < s->ndisplayed - 1) {
5559 s->selected++;
5560 break;
5562 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5563 == NULL)
5564 /* can't scroll any further */
5565 break;
5566 tree_scroll_down(s, 1);
5567 break;
5568 case KEY_NPAGE:
5569 case CTRL('f'):
5570 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5571 == NULL) {
5572 /* can't scroll any further; move cursor down */
5573 if (s->selected < s->ndisplayed - 1)
5574 s->selected = s->ndisplayed - 1;
5575 break;
5577 tree_scroll_down(s, view->nlines - 3);
5578 break;
5579 case KEY_ENTER:
5580 case '\r':
5581 case KEY_BACKSPACE:
5582 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5583 struct tog_parent_tree *parent;
5584 /* user selected '..' */
5585 if (s->tree == s->root)
5586 break;
5587 parent = TAILQ_FIRST(&s->parents);
5588 TAILQ_REMOVE(&s->parents, parent,
5589 entry);
5590 got_object_tree_close(s->tree);
5591 s->tree = parent->tree;
5592 s->first_displayed_entry =
5593 parent->first_displayed_entry;
5594 s->selected_entry =
5595 parent->selected_entry;
5596 s->selected = parent->selected;
5597 free(parent);
5598 } else if (S_ISDIR(got_tree_entry_get_mode(
5599 s->selected_entry))) {
5600 struct got_tree_object *subtree;
5601 err = got_object_open_as_tree(&subtree, s->repo,
5602 got_tree_entry_get_id(s->selected_entry));
5603 if (err)
5604 break;
5605 err = tree_view_visit_subtree(s, subtree);
5606 if (err) {
5607 got_object_tree_close(subtree);
5608 break;
5610 } else if (S_ISREG(got_tree_entry_get_mode(
5611 s->selected_entry))) {
5612 struct tog_view *blame_view;
5613 int begin_x = view_is_parent_view(view) ?
5614 view_split_begin_x(view->begin_x) : 0;
5616 err = blame_tree_entry(&blame_view, begin_x,
5617 s->selected_entry, &s->parents,
5618 s->commit_id, s->repo);
5619 if (err)
5620 break;
5621 view->focussed = 0;
5622 blame_view->focussed = 1;
5623 if (view_is_parent_view(view)) {
5624 err = view_close_child(view);
5625 if (err)
5626 return err;
5627 view_set_child(view, blame_view);
5628 view->focus_child = 1;
5629 } else
5630 *new_view = blame_view;
5632 break;
5633 case KEY_RESIZE:
5634 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5635 s->selected = view->nlines - 4;
5636 break;
5637 default:
5638 break;
5641 return err;
5644 __dead static void
5645 usage_tree(void)
5647 endwin();
5648 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5649 getprogname());
5650 exit(1);
5653 static const struct got_error *
5654 cmd_tree(int argc, char *argv[])
5656 const struct got_error *error;
5657 struct got_repository *repo = NULL;
5658 struct got_worktree *worktree = NULL;
5659 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5660 struct got_object_id *commit_id = NULL;
5661 const char *commit_id_arg = NULL;
5662 char *label = NULL;
5663 struct got_reference *ref = NULL;
5664 const char *head_ref_name = NULL;
5665 int ch;
5666 struct tog_view *view;
5668 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5669 switch (ch) {
5670 case 'c':
5671 commit_id_arg = optarg;
5672 break;
5673 case 'r':
5674 repo_path = realpath(optarg, NULL);
5675 if (repo_path == NULL)
5676 return got_error_from_errno2("realpath",
5677 optarg);
5678 break;
5679 default:
5680 usage_tree();
5681 /* NOTREACHED */
5685 argc -= optind;
5686 argv += optind;
5688 if (argc > 1)
5689 usage_tree();
5691 if (repo_path == NULL) {
5692 cwd = getcwd(NULL, 0);
5693 if (cwd == NULL)
5694 return got_error_from_errno("getcwd");
5695 error = got_worktree_open(&worktree, cwd);
5696 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5697 goto done;
5698 if (worktree)
5699 repo_path =
5700 strdup(got_worktree_get_repo_path(worktree));
5701 else
5702 repo_path = strdup(cwd);
5703 if (repo_path == NULL) {
5704 error = got_error_from_errno("strdup");
5705 goto done;
5709 error = got_repo_open(&repo, repo_path, NULL);
5710 if (error != NULL)
5711 goto done;
5713 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5714 repo, worktree);
5715 if (error)
5716 goto done;
5718 init_curses();
5720 error = apply_unveil(got_repo_get_path(repo), NULL);
5721 if (error)
5722 goto done;
5724 error = tog_load_refs(repo, 0);
5725 if (error)
5726 goto done;
5728 if (commit_id_arg == NULL) {
5729 error = got_repo_match_object_id(&commit_id, &label,
5730 worktree ? got_worktree_get_head_ref_name(worktree) :
5731 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5732 if (error)
5733 goto done;
5734 head_ref_name = label;
5735 } else {
5736 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5737 if (error == NULL)
5738 head_ref_name = got_ref_get_name(ref);
5739 else if (error->code != GOT_ERR_NOT_REF)
5740 goto done;
5741 error = got_repo_match_object_id(&commit_id, NULL,
5742 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5743 if (error)
5744 goto done;
5747 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5748 if (view == NULL) {
5749 error = got_error_from_errno("view_open");
5750 goto done;
5752 error = open_tree_view(view, commit_id, head_ref_name, repo);
5753 if (error)
5754 goto done;
5755 if (!got_path_is_root_dir(in_repo_path)) {
5756 error = tree_view_walk_path(&view->state.tree, commit_id,
5757 in_repo_path);
5758 if (error)
5759 goto done;
5762 if (worktree) {
5763 /* Release work tree lock. */
5764 got_worktree_close(worktree);
5765 worktree = NULL;
5767 error = view_loop(view);
5768 done:
5769 free(repo_path);
5770 free(cwd);
5771 free(commit_id);
5772 free(label);
5773 if (ref)
5774 got_ref_close(ref);
5775 if (repo) {
5776 const struct got_error *close_err = got_repo_close(repo);
5777 if (error == NULL)
5778 error = close_err;
5780 tog_free_refs();
5781 return error;
5784 static const struct got_error *
5785 ref_view_load_refs(struct tog_ref_view_state *s)
5787 struct got_reflist_entry *sre;
5788 struct tog_reflist_entry *re;
5790 s->nrefs = 0;
5791 TAILQ_FOREACH(sre, &tog_refs, entry) {
5792 if (strncmp(got_ref_get_name(sre->ref),
5793 "refs/got/", 9) == 0 &&
5794 strncmp(got_ref_get_name(sre->ref),
5795 "refs/got/backup/", 16) != 0)
5796 continue;
5798 re = malloc(sizeof(*re));
5799 if (re == NULL)
5800 return got_error_from_errno("malloc");
5802 re->ref = got_ref_dup(sre->ref);
5803 if (re->ref == NULL)
5804 return got_error_from_errno("got_ref_dup");
5805 re->idx = s->nrefs++;
5806 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5809 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5810 return NULL;
5813 void
5814 ref_view_free_refs(struct tog_ref_view_state *s)
5816 struct tog_reflist_entry *re;
5818 while (!TAILQ_EMPTY(&s->refs)) {
5819 re = TAILQ_FIRST(&s->refs);
5820 TAILQ_REMOVE(&s->refs, re, entry);
5821 got_ref_close(re->ref);
5822 free(re);
5826 static const struct got_error *
5827 open_ref_view(struct tog_view *view, struct got_repository *repo)
5829 const struct got_error *err = NULL;
5830 struct tog_ref_view_state *s = &view->state.ref;
5832 s->selected_entry = 0;
5833 s->repo = repo;
5835 TAILQ_INIT(&s->refs);
5836 STAILQ_INIT(&s->colors);
5838 err = ref_view_load_refs(s);
5839 if (err)
5840 return err;
5842 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5843 err = add_color(&s->colors, "^refs/heads/",
5844 TOG_COLOR_REFS_HEADS,
5845 get_color_value("TOG_COLOR_REFS_HEADS"));
5846 if (err)
5847 goto done;
5849 err = add_color(&s->colors, "^refs/tags/",
5850 TOG_COLOR_REFS_TAGS,
5851 get_color_value("TOG_COLOR_REFS_TAGS"));
5852 if (err)
5853 goto done;
5855 err = add_color(&s->colors, "^refs/remotes/",
5856 TOG_COLOR_REFS_REMOTES,
5857 get_color_value("TOG_COLOR_REFS_REMOTES"));
5858 if (err)
5859 goto done;
5861 err = add_color(&s->colors, "^refs/got/backup/",
5862 TOG_COLOR_REFS_BACKUP,
5863 get_color_value("TOG_COLOR_REFS_BACKUP"));
5864 if (err)
5865 goto done;
5868 view->show = show_ref_view;
5869 view->input = input_ref_view;
5870 view->close = close_ref_view;
5871 view->search_start = search_start_ref_view;
5872 view->search_next = search_next_ref_view;
5873 done:
5874 if (err)
5875 free_colors(&s->colors);
5876 return err;
5879 static const struct got_error *
5880 close_ref_view(struct tog_view *view)
5882 struct tog_ref_view_state *s = &view->state.ref;
5884 ref_view_free_refs(s);
5885 free_colors(&s->colors);
5887 return NULL;
5890 static const struct got_error *
5891 resolve_reflist_entry(struct got_object_id **commit_id,
5892 struct tog_reflist_entry *re, struct got_repository *repo)
5894 const struct got_error *err = NULL;
5895 struct got_object_id *obj_id;
5896 struct got_tag_object *tag = NULL;
5897 int obj_type;
5899 *commit_id = NULL;
5901 err = got_ref_resolve(&obj_id, repo, re->ref);
5902 if (err)
5903 return err;
5905 err = got_object_get_type(&obj_type, repo, obj_id);
5906 if (err)
5907 goto done;
5909 switch (obj_type) {
5910 case GOT_OBJ_TYPE_COMMIT:
5911 *commit_id = obj_id;
5912 break;
5913 case GOT_OBJ_TYPE_TAG:
5914 err = got_object_open_as_tag(&tag, repo, obj_id);
5915 if (err)
5916 goto done;
5917 free(obj_id);
5918 err = got_object_get_type(&obj_type, repo,
5919 got_object_tag_get_object_id(tag));
5920 if (err)
5921 goto done;
5922 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5923 err = got_error(GOT_ERR_OBJ_TYPE);
5924 goto done;
5926 *commit_id = got_object_id_dup(
5927 got_object_tag_get_object_id(tag));
5928 if (*commit_id == NULL) {
5929 err = got_error_from_errno("got_object_id_dup");
5930 goto done;
5932 break;
5933 default:
5934 err = got_error(GOT_ERR_OBJ_TYPE);
5935 break;
5938 done:
5939 if (tag)
5940 got_object_tag_close(tag);
5941 if (err) {
5942 free(*commit_id);
5943 *commit_id = NULL;
5945 return err;
5948 static const struct got_error *
5949 log_ref_entry(struct tog_view **new_view, int begin_x,
5950 struct tog_reflist_entry *re, struct got_repository *repo)
5952 struct tog_view *log_view;
5953 const struct got_error *err = NULL;
5954 struct got_object_id *commit_id = NULL;
5956 *new_view = NULL;
5958 err = resolve_reflist_entry(&commit_id, re, repo);
5959 if (err) {
5960 if (err->code != GOT_ERR_OBJ_TYPE)
5961 return err;
5962 else
5963 return NULL;
5966 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5967 if (log_view == NULL) {
5968 err = got_error_from_errno("view_open");
5969 goto done;
5972 err = open_log_view(log_view, commit_id, repo,
5973 got_ref_get_name(re->ref), "", 0);
5974 done:
5975 if (err)
5976 view_close(log_view);
5977 else
5978 *new_view = log_view;
5979 free(commit_id);
5980 return err;
5983 static void
5984 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5986 struct tog_reflist_entry *re;
5987 int i = 0;
5989 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5990 return;
5992 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5993 while (i++ < maxscroll) {
5994 if (re == NULL)
5995 break;
5996 s->first_displayed_entry = re;
5997 re = TAILQ_PREV(re, tog_reflist_head, entry);
6001 static void
6002 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6004 struct tog_reflist_entry *next, *last;
6005 int n = 0;
6007 if (s->first_displayed_entry)
6008 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6009 else
6010 next = TAILQ_FIRST(&s->refs);
6012 last = s->last_displayed_entry;
6013 while (next && last && n++ < maxscroll) {
6014 last = TAILQ_NEXT(last, entry);
6015 if (last) {
6016 s->first_displayed_entry = next;
6017 next = TAILQ_NEXT(next, entry);
6022 static const struct got_error *
6023 search_start_ref_view(struct tog_view *view)
6025 struct tog_ref_view_state *s = &view->state.ref;
6027 s->matched_entry = NULL;
6028 return NULL;
6031 static int
6032 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6034 regmatch_t regmatch;
6036 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6037 0) == 0;
6040 static const struct got_error *
6041 search_next_ref_view(struct tog_view *view)
6043 struct tog_ref_view_state *s = &view->state.ref;
6044 struct tog_reflist_entry *re = NULL;
6046 if (!view->searching) {
6047 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6048 return NULL;
6051 if (s->matched_entry) {
6052 if (view->searching == TOG_SEARCH_FORWARD) {
6053 if (s->selected_entry)
6054 re = TAILQ_NEXT(s->selected_entry, entry);
6055 else
6056 re = TAILQ_PREV(s->selected_entry,
6057 tog_reflist_head, entry);
6058 } else {
6059 if (s->selected_entry == NULL)
6060 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6061 else
6062 re = TAILQ_PREV(s->selected_entry,
6063 tog_reflist_head, entry);
6065 } else {
6066 if (s->selected_entry)
6067 re = s->selected_entry;
6068 else if (view->searching == TOG_SEARCH_FORWARD)
6069 re = TAILQ_FIRST(&s->refs);
6070 else
6071 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6074 while (1) {
6075 if (re == NULL) {
6076 if (s->matched_entry == NULL) {
6077 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6078 return NULL;
6080 if (view->searching == TOG_SEARCH_FORWARD)
6081 re = TAILQ_FIRST(&s->refs);
6082 else
6083 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6086 if (match_reflist_entry(re, &view->regex)) {
6087 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6088 s->matched_entry = re;
6089 break;
6092 if (view->searching == TOG_SEARCH_FORWARD)
6093 re = TAILQ_NEXT(re, entry);
6094 else
6095 re = TAILQ_PREV(re, tog_reflist_head, entry);
6098 if (s->matched_entry) {
6099 s->first_displayed_entry = s->matched_entry;
6100 s->selected = 0;
6103 return NULL;
6106 static const struct got_error *
6107 show_ref_view(struct tog_view *view)
6109 const struct got_error *err = NULL;
6110 struct tog_ref_view_state *s = &view->state.ref;
6111 struct tog_reflist_entry *re;
6112 char *line = NULL;
6113 wchar_t *wline;
6114 struct tog_color *tc;
6115 int width, n;
6116 int limit = view->nlines;
6118 werase(view->window);
6120 s->ndisplayed = 0;
6122 if (limit == 0)
6123 return NULL;
6125 re = s->first_displayed_entry;
6127 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6128 s->nrefs) == -1)
6129 return got_error_from_errno("asprintf");
6131 err = format_line(&wline, &width, line, view->ncols, 0);
6132 if (err) {
6133 free(line);
6134 return err;
6136 if (view_needs_focus_indication(view))
6137 wstandout(view->window);
6138 waddwstr(view->window, wline);
6139 if (view_needs_focus_indication(view))
6140 wstandend(view->window);
6141 free(wline);
6142 wline = NULL;
6143 free(line);
6144 line = NULL;
6145 if (width < view->ncols - 1)
6146 waddch(view->window, '\n');
6147 if (--limit <= 0)
6148 return NULL;
6150 n = 0;
6151 while (re && limit > 0) {
6152 char *line = NULL;
6154 if (got_ref_is_symbolic(re->ref)) {
6155 if (asprintf(&line, "%s -> %s",
6156 got_ref_get_name(re->ref),
6157 got_ref_get_symref_target(re->ref)) == -1)
6158 return got_error_from_errno("asprintf");
6159 } else if (s->show_ids) {
6160 struct got_object_id *id;
6161 char *id_str;
6162 err = got_ref_resolve(&id, s->repo, re->ref);
6163 if (err)
6164 return err;
6165 err = got_object_id_str(&id_str, id);
6166 if (err) {
6167 free(id);
6168 return err;
6170 if (asprintf(&line, "%s: %s",
6171 got_ref_get_name(re->ref), id_str) == -1) {
6172 err = got_error_from_errno("asprintf");
6173 free(id);
6174 free(id_str);
6175 return err;
6177 free(id);
6178 free(id_str);
6179 } else {
6180 line = strdup(got_ref_get_name(re->ref));
6181 if (line == NULL)
6182 return got_error_from_errno("strdup");
6185 err = format_line(&wline, &width, line, view->ncols, 0);
6186 if (err) {
6187 free(line);
6188 return err;
6190 if (n == s->selected) {
6191 if (view->focussed)
6192 wstandout(view->window);
6193 s->selected_entry = re;
6195 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6196 if (tc)
6197 wattr_on(view->window,
6198 COLOR_PAIR(tc->colorpair), NULL);
6199 waddwstr(view->window, wline);
6200 if (tc)
6201 wattr_off(view->window,
6202 COLOR_PAIR(tc->colorpair), NULL);
6203 if (width < view->ncols - 1)
6204 waddch(view->window, '\n');
6205 if (n == s->selected && view->focussed)
6206 wstandend(view->window);
6207 free(line);
6208 free(wline);
6209 wline = NULL;
6210 n++;
6211 s->ndisplayed++;
6212 s->last_displayed_entry = re;
6214 limit--;
6215 re = TAILQ_NEXT(re, entry);
6218 view_vborder(view);
6219 return err;
6222 static const struct got_error *
6223 browse_ref_tree(struct tog_view **new_view, int begin_x,
6224 struct tog_reflist_entry *re, struct got_repository *repo)
6226 const struct got_error *err = NULL;
6227 struct got_object_id *commit_id = NULL;
6228 struct tog_view *tree_view;
6230 *new_view = NULL;
6232 err = resolve_reflist_entry(&commit_id, re, repo);
6233 if (err) {
6234 if (err->code != GOT_ERR_OBJ_TYPE)
6235 return err;
6236 else
6237 return NULL;
6241 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6242 if (tree_view == NULL) {
6243 err = got_error_from_errno("view_open");
6244 goto done;
6247 err = open_tree_view(tree_view, commit_id,
6248 got_ref_get_name(re->ref), repo);
6249 if (err)
6250 goto done;
6252 *new_view = tree_view;
6253 done:
6254 free(commit_id);
6255 return err;
6257 static const struct got_error *
6258 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6260 const struct got_error *err = NULL;
6261 struct tog_ref_view_state *s = &view->state.ref;
6262 struct tog_view *log_view, *tree_view;
6263 struct tog_reflist_entry *re;
6264 int begin_x = 0, n;
6266 switch (ch) {
6267 case 'i':
6268 s->show_ids = !s->show_ids;
6269 break;
6270 case 'o':
6271 s->sort_by_date = !s->sort_by_date;
6272 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6273 got_ref_cmp_by_commit_timestamp_descending :
6274 tog_ref_cmp_by_name, s->repo);
6275 if (err)
6276 break;
6277 got_reflist_object_id_map_free(tog_refs_idmap);
6278 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6279 &tog_refs, s->repo);
6280 if (err)
6281 break;
6282 ref_view_free_refs(s);
6283 err = ref_view_load_refs(s);
6284 break;
6285 case KEY_ENTER:
6286 case '\r':
6287 if (!s->selected_entry)
6288 break;
6289 if (view_is_parent_view(view))
6290 begin_x = view_split_begin_x(view->begin_x);
6291 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6292 s->repo);
6293 view->focussed = 0;
6294 log_view->focussed = 1;
6295 if (view_is_parent_view(view)) {
6296 err = view_close_child(view);
6297 if (err)
6298 return err;
6299 view_set_child(view, log_view);
6300 view->focus_child = 1;
6301 } else
6302 *new_view = log_view;
6303 break;
6304 case 't':
6305 if (!s->selected_entry)
6306 break;
6307 if (view_is_parent_view(view))
6308 begin_x = view_split_begin_x(view->begin_x);
6309 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6310 s->repo);
6311 if (err || tree_view == NULL)
6312 break;
6313 view->focussed = 0;
6314 tree_view->focussed = 1;
6315 if (view_is_parent_view(view)) {
6316 err = view_close_child(view);
6317 if (err)
6318 return err;
6319 view_set_child(view, tree_view);
6320 view->focus_child = 1;
6321 } else
6322 *new_view = tree_view;
6323 break;
6324 case 'g':
6325 case KEY_HOME:
6326 s->selected = 0;
6327 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6328 break;
6329 case 'G':
6330 case KEY_END:
6331 s->selected = 0;
6332 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6333 for (n = 0; n < view->nlines - 1; n++) {
6334 if (re == NULL)
6335 break;
6336 s->first_displayed_entry = re;
6337 re = TAILQ_PREV(re, tog_reflist_head, entry);
6339 if (n > 0)
6340 s->selected = n - 1;
6341 break;
6342 case 'k':
6343 case KEY_UP:
6344 case CTRL('p'):
6345 if (s->selected > 0) {
6346 s->selected--;
6347 break;
6349 ref_scroll_up(s, 1);
6350 break;
6351 case KEY_PPAGE:
6352 case CTRL('b'):
6353 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6354 s->selected = 0;
6355 ref_scroll_up(s, MAX(0, view->nlines - 1));
6356 break;
6357 case 'j':
6358 case KEY_DOWN:
6359 case CTRL('n'):
6360 if (s->selected < s->ndisplayed - 1) {
6361 s->selected++;
6362 break;
6364 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6365 /* can't scroll any further */
6366 break;
6367 ref_scroll_down(s, 1);
6368 break;
6369 case KEY_NPAGE:
6370 case CTRL('f'):
6371 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6372 /* can't scroll any further; move cursor down */
6373 if (s->selected < s->ndisplayed - 1)
6374 s->selected = s->ndisplayed - 1;
6375 break;
6377 ref_scroll_down(s, view->nlines - 1);
6378 break;
6379 case CTRL('l'):
6380 tog_free_refs();
6381 err = tog_load_refs(s->repo, s->sort_by_date);
6382 if (err)
6383 break;
6384 ref_view_free_refs(s);
6385 err = ref_view_load_refs(s);
6386 break;
6387 case KEY_RESIZE:
6388 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6389 s->selected = view->nlines - 2;
6390 break;
6391 default:
6392 break;
6395 return err;
6398 __dead static void
6399 usage_ref(void)
6401 endwin();
6402 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6403 getprogname());
6404 exit(1);
6407 static const struct got_error *
6408 cmd_ref(int argc, char *argv[])
6410 const struct got_error *error;
6411 struct got_repository *repo = NULL;
6412 struct got_worktree *worktree = NULL;
6413 char *cwd = NULL, *repo_path = NULL;
6414 int ch;
6415 struct tog_view *view;
6417 while ((ch = getopt(argc, argv, "r:")) != -1) {
6418 switch (ch) {
6419 case 'r':
6420 repo_path = realpath(optarg, NULL);
6421 if (repo_path == NULL)
6422 return got_error_from_errno2("realpath",
6423 optarg);
6424 break;
6425 default:
6426 usage_ref();
6427 /* NOTREACHED */
6431 argc -= optind;
6432 argv += optind;
6434 if (argc > 1)
6435 usage_ref();
6437 if (repo_path == NULL) {
6438 cwd = getcwd(NULL, 0);
6439 if (cwd == NULL)
6440 return got_error_from_errno("getcwd");
6441 error = got_worktree_open(&worktree, cwd);
6442 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6443 goto done;
6444 if (worktree)
6445 repo_path =
6446 strdup(got_worktree_get_repo_path(worktree));
6447 else
6448 repo_path = strdup(cwd);
6449 if (repo_path == NULL) {
6450 error = got_error_from_errno("strdup");
6451 goto done;
6455 error = got_repo_open(&repo, repo_path, NULL);
6456 if (error != NULL)
6457 goto done;
6459 init_curses();
6461 error = apply_unveil(got_repo_get_path(repo), NULL);
6462 if (error)
6463 goto done;
6465 error = tog_load_refs(repo, 0);
6466 if (error)
6467 goto done;
6469 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6470 if (view == NULL) {
6471 error = got_error_from_errno("view_open");
6472 goto done;
6475 error = open_ref_view(view, repo);
6476 if (error)
6477 goto done;
6479 if (worktree) {
6480 /* Release work tree lock. */
6481 got_worktree_close(worktree);
6482 worktree = NULL;
6484 error = view_loop(view);
6485 done:
6486 free(repo_path);
6487 free(cwd);
6488 if (repo) {
6489 const struct got_error *close_err = got_repo_close(repo);
6490 if (close_err)
6491 error = close_err;
6493 tog_free_refs();
6494 return error;
6497 static void
6498 list_commands(FILE *fp)
6500 size_t i;
6502 fprintf(fp, "commands:");
6503 for (i = 0; i < nitems(tog_commands); i++) {
6504 struct tog_cmd *cmd = &tog_commands[i];
6505 fprintf(fp, " %s", cmd->name);
6507 fputc('\n', fp);
6510 __dead static void
6511 usage(int hflag, int status)
6513 FILE *fp = (status == 0) ? stdout : stderr;
6515 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6516 getprogname());
6517 if (hflag) {
6518 fprintf(fp, "lazy usage: %s path\n", getprogname());
6519 list_commands(fp);
6521 exit(status);
6524 static char **
6525 make_argv(int argc, ...)
6527 va_list ap;
6528 char **argv;
6529 int i;
6531 va_start(ap, argc);
6533 argv = calloc(argc, sizeof(char *));
6534 if (argv == NULL)
6535 err(1, "calloc");
6536 for (i = 0; i < argc; i++) {
6537 argv[i] = strdup(va_arg(ap, char *));
6538 if (argv[i] == NULL)
6539 err(1, "strdup");
6542 va_end(ap);
6543 return argv;
6547 * Try to convert 'tog path' into a 'tog log path' command.
6548 * The user could simply have mistyped the command rather than knowingly
6549 * provided a path. So check whether argv[0] can in fact be resolved
6550 * to a path in the HEAD commit and print a special error if not.
6551 * This hack is for mpi@ <3
6553 static const struct got_error *
6554 tog_log_with_path(int argc, char *argv[])
6556 const struct got_error *error = NULL, *close_err;
6557 struct tog_cmd *cmd = NULL;
6558 struct got_repository *repo = NULL;
6559 struct got_worktree *worktree = NULL;
6560 struct got_object_id *commit_id = NULL, *id = NULL;
6561 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6562 char *commit_id_str = NULL, **cmd_argv = NULL;
6564 cwd = getcwd(NULL, 0);
6565 if (cwd == NULL)
6566 return got_error_from_errno("getcwd");
6568 error = got_worktree_open(&worktree, cwd);
6569 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6570 goto done;
6572 if (worktree)
6573 repo_path = strdup(got_worktree_get_repo_path(worktree));
6574 else
6575 repo_path = strdup(cwd);
6576 if (repo_path == NULL) {
6577 error = got_error_from_errno("strdup");
6578 goto done;
6581 error = got_repo_open(&repo, repo_path, NULL);
6582 if (error != NULL)
6583 goto done;
6585 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6586 repo, worktree);
6587 if (error)
6588 goto done;
6590 error = tog_load_refs(repo, 0);
6591 if (error)
6592 goto done;
6593 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6594 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6595 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6596 if (error)
6597 goto done;
6599 if (worktree) {
6600 got_worktree_close(worktree);
6601 worktree = NULL;
6604 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6605 if (error) {
6606 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6607 goto done;
6608 fprintf(stderr, "%s: '%s' is no known command or path\n",
6609 getprogname(), argv[0]);
6610 usage(1, 1);
6611 /* not reached */
6614 close_err = got_repo_close(repo);
6615 if (error == NULL)
6616 error = close_err;
6617 repo = NULL;
6619 error = got_object_id_str(&commit_id_str, commit_id);
6620 if (error)
6621 goto done;
6623 cmd = &tog_commands[0]; /* log */
6624 argc = 4;
6625 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6626 error = cmd->cmd_main(argc, cmd_argv);
6627 done:
6628 if (repo) {
6629 close_err = got_repo_close(repo);
6630 if (error == NULL)
6631 error = close_err;
6633 if (worktree)
6634 got_worktree_close(worktree);
6635 free(id);
6636 free(commit_id_str);
6637 free(commit_id);
6638 free(cwd);
6639 free(repo_path);
6640 free(in_repo_path);
6641 if (cmd_argv) {
6642 int i;
6643 for (i = 0; i < argc; i++)
6644 free(cmd_argv[i]);
6645 free(cmd_argv);
6647 tog_free_refs();
6648 return error;
6651 int
6652 main(int argc, char *argv[])
6654 const struct got_error *error = NULL;
6655 struct tog_cmd *cmd = NULL;
6656 int ch, hflag = 0, Vflag = 0;
6657 char **cmd_argv = NULL;
6658 static struct option longopts[] = {
6659 { "version", no_argument, NULL, 'V' },
6660 { NULL, 0, NULL, 0}
6663 setlocale(LC_CTYPE, "");
6665 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6666 switch (ch) {
6667 case 'h':
6668 hflag = 1;
6669 break;
6670 case 'V':
6671 Vflag = 1;
6672 break;
6673 default:
6674 usage(hflag, 1);
6675 /* NOTREACHED */
6679 argc -= optind;
6680 argv += optind;
6681 optind = 1;
6682 optreset = 1;
6684 if (Vflag) {
6685 got_version_print_str();
6686 return 0;
6689 #ifndef PROFILE
6690 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6691 NULL) == -1)
6692 err(1, "pledge");
6693 #endif
6695 if (argc == 0) {
6696 if (hflag)
6697 usage(hflag, 0);
6698 /* Build an argument vector which runs a default command. */
6699 cmd = &tog_commands[0];
6700 argc = 1;
6701 cmd_argv = make_argv(argc, cmd->name);
6702 } else {
6703 size_t i;
6705 /* Did the user specify a command? */
6706 for (i = 0; i < nitems(tog_commands); i++) {
6707 if (strncmp(tog_commands[i].name, argv[0],
6708 strlen(argv[0])) == 0) {
6709 cmd = &tog_commands[i];
6710 break;
6715 if (cmd == NULL) {
6716 if (argc != 1)
6717 usage(0, 1);
6718 /* No command specified; try log with a path */
6719 error = tog_log_with_path(argc, argv);
6720 } else {
6721 if (hflag)
6722 cmd->cmd_usage();
6723 else
6724 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6727 endwin();
6728 putchar('\n');
6729 if (cmd_argv) {
6730 int i;
6731 for (i = 0; i < argc; i++)
6732 free(cmd_argv[i]);
6733 free(cmd_argv);
6736 if (error && error->code != GOT_ERR_CANCELLED)
6737 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6738 return 0;