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 const 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_commit_object *commit, 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,
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->commit, 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 errx(1, "number of context lines is %s: %s",
3888 errstr, errstr);
3889 break;
3890 case 'r':
3891 repo_path = realpath(optarg, NULL);
3892 if (repo_path == NULL)
3893 return got_error_from_errno2("realpath",
3894 optarg);
3895 got_path_strip_trailing_slashes(repo_path);
3896 break;
3897 case 'w':
3898 ignore_whitespace = 1;
3899 break;
3900 default:
3901 usage_diff();
3902 /* NOTREACHED */
3906 argc -= optind;
3907 argv += optind;
3909 if (argc == 0) {
3910 usage_diff(); /* TODO show local worktree changes */
3911 } else if (argc == 2) {
3912 id_str1 = argv[0];
3913 id_str2 = argv[1];
3914 } else
3915 usage_diff();
3917 if (repo_path == NULL) {
3918 cwd = getcwd(NULL, 0);
3919 if (cwd == NULL)
3920 return got_error_from_errno("getcwd");
3921 error = got_worktree_open(&worktree, cwd);
3922 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3923 goto done;
3924 if (worktree)
3925 repo_path =
3926 strdup(got_worktree_get_repo_path(worktree));
3927 else
3928 repo_path = strdup(cwd);
3929 if (repo_path == NULL) {
3930 error = got_error_from_errno("strdup");
3931 goto done;
3935 error = got_repo_open(&repo, repo_path, NULL);
3936 if (error)
3937 goto done;
3939 init_curses();
3941 error = apply_unveil(got_repo_get_path(repo), NULL);
3942 if (error)
3943 goto done;
3945 error = tog_load_refs(repo, 0);
3946 if (error)
3947 goto done;
3949 error = got_repo_match_object_id(&id1, &label1, id_str1,
3950 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3951 if (error)
3952 goto done;
3954 error = got_repo_match_object_id(&id2, &label2, id_str2,
3955 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3956 if (error)
3957 goto done;
3959 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3960 if (view == NULL) {
3961 error = got_error_from_errno("view_open");
3962 goto done;
3964 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3965 ignore_whitespace, force_text_diff, NULL, repo);
3966 if (error)
3967 goto done;
3968 error = view_loop(view);
3969 done:
3970 free(label1);
3971 free(label2);
3972 free(repo_path);
3973 free(cwd);
3974 if (repo) {
3975 const struct got_error *close_err = got_repo_close(repo);
3976 if (error == NULL)
3977 error = close_err;
3979 if (worktree)
3980 got_worktree_close(worktree);
3981 tog_free_refs();
3982 return error;
3985 __dead static void
3986 usage_blame(void)
3988 endwin();
3989 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3990 getprogname());
3991 exit(1);
3994 struct tog_blame_line {
3995 int annotated;
3996 struct got_object_id *id;
3999 static const struct got_error *
4000 draw_blame(struct tog_view *view)
4002 struct tog_blame_view_state *s = &view->state.blame;
4003 struct tog_blame *blame = &s->blame;
4004 regmatch_t *regmatch = &view->regmatch;
4005 const struct got_error *err;
4006 int lineno = 0, nprinted = 0;
4007 char *line = NULL;
4008 size_t linesize = 0;
4009 ssize_t linelen;
4010 wchar_t *wline;
4011 int width;
4012 struct tog_blame_line *blame_line;
4013 struct got_object_id *prev_id = NULL;
4014 char *id_str;
4015 struct tog_color *tc;
4017 err = got_object_id_str(&id_str, s->blamed_commit->id);
4018 if (err)
4019 return err;
4021 rewind(blame->f);
4022 werase(view->window);
4024 if (asprintf(&line, "commit %s", id_str) == -1) {
4025 err = got_error_from_errno("asprintf");
4026 free(id_str);
4027 return err;
4030 err = format_line(&wline, &width, line, view->ncols, 0);
4031 free(line);
4032 line = NULL;
4033 if (err)
4034 return err;
4035 if (view_needs_focus_indication(view))
4036 wstandout(view->window);
4037 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4038 if (tc)
4039 wattr_on(view->window,
4040 COLOR_PAIR(tc->colorpair), NULL);
4041 waddwstr(view->window, wline);
4042 if (tc)
4043 wattr_off(view->window,
4044 COLOR_PAIR(tc->colorpair), NULL);
4045 if (view_needs_focus_indication(view))
4046 wstandend(view->window);
4047 free(wline);
4048 wline = NULL;
4049 if (width < view->ncols - 1)
4050 waddch(view->window, '\n');
4052 if (asprintf(&line, "[%d/%d] %s%s",
4053 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4054 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4055 free(id_str);
4056 return got_error_from_errno("asprintf");
4058 free(id_str);
4059 err = format_line(&wline, &width, line, view->ncols, 0);
4060 free(line);
4061 line = NULL;
4062 if (err)
4063 return err;
4064 waddwstr(view->window, wline);
4065 free(wline);
4066 wline = NULL;
4067 if (width < view->ncols - 1)
4068 waddch(view->window, '\n');
4070 s->eof = 0;
4071 while (nprinted < view->nlines - 2) {
4072 linelen = getline(&line, &linesize, blame->f);
4073 if (linelen == -1) {
4074 if (feof(blame->f)) {
4075 s->eof = 1;
4076 break;
4078 free(line);
4079 return got_ferror(blame->f, GOT_ERR_IO);
4081 if (++lineno < s->first_displayed_line)
4082 continue;
4084 if (view->focussed && nprinted == s->selected_line - 1)
4085 wstandout(view->window);
4087 if (blame->nlines > 0) {
4088 blame_line = &blame->lines[lineno - 1];
4089 if (blame_line->annotated && prev_id &&
4090 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4091 !(view->focussed &&
4092 nprinted == s->selected_line - 1)) {
4093 waddstr(view->window, " ");
4094 } else if (blame_line->annotated) {
4095 char *id_str;
4096 err = got_object_id_str(&id_str, blame_line->id);
4097 if (err) {
4098 free(line);
4099 return err;
4101 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4102 if (tc)
4103 wattr_on(view->window,
4104 COLOR_PAIR(tc->colorpair), NULL);
4105 wprintw(view->window, "%.8s", id_str);
4106 if (tc)
4107 wattr_off(view->window,
4108 COLOR_PAIR(tc->colorpair), NULL);
4109 free(id_str);
4110 prev_id = blame_line->id;
4111 } else {
4112 waddstr(view->window, "........");
4113 prev_id = NULL;
4115 } else {
4116 waddstr(view->window, "........");
4117 prev_id = NULL;
4120 if (view->focussed && nprinted == s->selected_line - 1)
4121 wstandend(view->window);
4122 waddstr(view->window, " ");
4124 if (view->ncols <= 9) {
4125 width = 9;
4126 wline = wcsdup(L"");
4127 if (wline == NULL) {
4128 err = got_error_from_errno("wcsdup");
4129 free(line);
4130 return err;
4132 } else if (s->first_displayed_line + nprinted ==
4133 s->matched_line &&
4134 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4135 err = add_matched_line(&width, line, view->ncols - 9, 9,
4136 view->window, regmatch);
4137 if (err) {
4138 free(line);
4139 return err;
4141 width += 9;
4142 } else {
4143 err = format_line(&wline, &width, line,
4144 view->ncols - 9, 9);
4145 waddwstr(view->window, wline);
4146 free(wline);
4147 wline = NULL;
4148 width += 9;
4151 if (width <= view->ncols - 1)
4152 waddch(view->window, '\n');
4153 if (++nprinted == 1)
4154 s->first_displayed_line = lineno;
4156 free(line);
4157 s->last_displayed_line = lineno;
4159 view_vborder(view);
4161 return NULL;
4164 static const struct got_error *
4165 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4167 const struct got_error *err = NULL;
4168 struct tog_blame_cb_args *a = arg;
4169 struct tog_blame_line *line;
4170 int errcode;
4172 if (nlines != a->nlines ||
4173 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4174 return got_error(GOT_ERR_RANGE);
4176 errcode = pthread_mutex_lock(&tog_mutex);
4177 if (errcode)
4178 return got_error_set_errno(errcode, "pthread_mutex_lock");
4180 if (*a->quit) { /* user has quit the blame view */
4181 err = got_error(GOT_ERR_ITER_COMPLETED);
4182 goto done;
4185 if (lineno == -1)
4186 goto done; /* no change in this commit */
4188 line = &a->lines[lineno - 1];
4189 if (line->annotated)
4190 goto done;
4192 line->id = got_object_id_dup(id);
4193 if (line->id == NULL) {
4194 err = got_error_from_errno("got_object_id_dup");
4195 goto done;
4197 line->annotated = 1;
4198 done:
4199 errcode = pthread_mutex_unlock(&tog_mutex);
4200 if (errcode)
4201 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4202 return err;
4205 static void *
4206 blame_thread(void *arg)
4208 const struct got_error *err, *close_err;
4209 struct tog_blame_thread_args *ta = arg;
4210 struct tog_blame_cb_args *a = ta->cb_args;
4211 int errcode;
4213 err = block_signals_used_by_main_thread();
4214 if (err)
4215 return (void *)err;
4217 err = got_blame(ta->path, a->commit_id, ta->repo,
4218 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4219 if (err && err->code == GOT_ERR_CANCELLED)
4220 err = NULL;
4222 errcode = pthread_mutex_lock(&tog_mutex);
4223 if (errcode)
4224 return (void *)got_error_set_errno(errcode,
4225 "pthread_mutex_lock");
4227 close_err = got_repo_close(ta->repo);
4228 if (err == NULL)
4229 err = close_err;
4230 ta->repo = NULL;
4231 *ta->complete = 1;
4233 errcode = pthread_mutex_unlock(&tog_mutex);
4234 if (errcode && err == NULL)
4235 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4237 return (void *)err;
4240 static struct got_object_id *
4241 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4242 int first_displayed_line, int selected_line)
4244 struct tog_blame_line *line;
4246 if (nlines <= 0)
4247 return NULL;
4249 line = &lines[first_displayed_line - 1 + selected_line - 1];
4250 if (!line->annotated)
4251 return NULL;
4253 return line->id;
4256 static const struct got_error *
4257 stop_blame(struct tog_blame *blame)
4259 const struct got_error *err = NULL;
4260 int i;
4262 if (blame->thread) {
4263 int errcode;
4264 errcode = pthread_mutex_unlock(&tog_mutex);
4265 if (errcode)
4266 return got_error_set_errno(errcode,
4267 "pthread_mutex_unlock");
4268 errcode = pthread_join(blame->thread, (void **)&err);
4269 if (errcode)
4270 return got_error_set_errno(errcode, "pthread_join");
4271 errcode = pthread_mutex_lock(&tog_mutex);
4272 if (errcode)
4273 return got_error_set_errno(errcode,
4274 "pthread_mutex_lock");
4275 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4276 err = NULL;
4277 blame->thread = NULL;
4279 if (blame->thread_args.repo) {
4280 const struct got_error *close_err;
4281 close_err = got_repo_close(blame->thread_args.repo);
4282 if (err == NULL)
4283 err = close_err;
4284 blame->thread_args.repo = NULL;
4286 if (blame->f) {
4287 if (fclose(blame->f) == EOF && err == NULL)
4288 err = got_error_from_errno("fclose");
4289 blame->f = NULL;
4291 if (blame->lines) {
4292 for (i = 0; i < blame->nlines; i++)
4293 free(blame->lines[i].id);
4294 free(blame->lines);
4295 blame->lines = NULL;
4297 free(blame->cb_args.commit_id);
4298 blame->cb_args.commit_id = NULL;
4300 return err;
4303 static const struct got_error *
4304 cancel_blame_view(void *arg)
4306 const struct got_error *err = NULL;
4307 int *done = arg;
4308 int errcode;
4310 errcode = pthread_mutex_lock(&tog_mutex);
4311 if (errcode)
4312 return got_error_set_errno(errcode,
4313 "pthread_mutex_unlock");
4315 if (*done)
4316 err = got_error(GOT_ERR_CANCELLED);
4318 errcode = pthread_mutex_unlock(&tog_mutex);
4319 if (errcode)
4320 return got_error_set_errno(errcode,
4321 "pthread_mutex_lock");
4323 return err;
4326 static const struct got_error *
4327 run_blame(struct tog_view *view)
4329 struct tog_blame_view_state *s = &view->state.blame;
4330 struct tog_blame *blame = &s->blame;
4331 const struct got_error *err = NULL;
4332 struct got_commit_object *commit = NULL;
4333 struct got_blob_object *blob = NULL;
4334 struct got_repository *thread_repo = NULL;
4335 struct got_object_id *obj_id = NULL;
4336 int obj_type;
4338 err = got_object_open_as_commit(&commit, s->repo,
4339 s->blamed_commit->id);
4340 if (err)
4341 return err;
4343 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4344 if (err)
4345 goto done;
4347 err = got_object_get_type(&obj_type, s->repo, obj_id);
4348 if (err)
4349 goto done;
4351 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4352 err = got_error(GOT_ERR_OBJ_TYPE);
4353 goto done;
4356 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4357 if (err)
4358 goto done;
4359 blame->f = got_opentemp();
4360 if (blame->f == NULL) {
4361 err = got_error_from_errno("got_opentemp");
4362 goto done;
4364 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4365 &blame->line_offsets, blame->f, blob);
4366 if (err)
4367 goto done;
4368 if (blame->nlines == 0) {
4369 s->blame_complete = 1;
4370 goto done;
4373 /* Don't include \n at EOF in the blame line count. */
4374 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4375 blame->nlines--;
4377 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4378 if (blame->lines == NULL) {
4379 err = got_error_from_errno("calloc");
4380 goto done;
4383 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4384 if (err)
4385 goto done;
4387 blame->cb_args.view = view;
4388 blame->cb_args.lines = blame->lines;
4389 blame->cb_args.nlines = blame->nlines;
4390 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4391 if (blame->cb_args.commit_id == NULL) {
4392 err = got_error_from_errno("got_object_id_dup");
4393 goto done;
4395 blame->cb_args.quit = &s->done;
4397 blame->thread_args.path = s->path;
4398 blame->thread_args.repo = thread_repo;
4399 blame->thread_args.cb_args = &blame->cb_args;
4400 blame->thread_args.complete = &s->blame_complete;
4401 blame->thread_args.cancel_cb = cancel_blame_view;
4402 blame->thread_args.cancel_arg = &s->done;
4403 s->blame_complete = 0;
4405 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4406 s->first_displayed_line = 1;
4407 s->last_displayed_line = view->nlines;
4408 s->selected_line = 1;
4410 s->matched_line = 0;
4412 done:
4413 if (commit)
4414 got_object_commit_close(commit);
4415 if (blob)
4416 got_object_blob_close(blob);
4417 free(obj_id);
4418 if (err)
4419 stop_blame(blame);
4420 return err;
4423 static const struct got_error *
4424 open_blame_view(struct tog_view *view, char *path,
4425 struct got_object_id *commit_id, struct got_repository *repo)
4427 const struct got_error *err = NULL;
4428 struct tog_blame_view_state *s = &view->state.blame;
4430 STAILQ_INIT(&s->blamed_commits);
4432 s->path = strdup(path);
4433 if (s->path == NULL)
4434 return got_error_from_errno("strdup");
4436 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4437 if (err) {
4438 free(s->path);
4439 return err;
4442 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4443 s->first_displayed_line = 1;
4444 s->last_displayed_line = view->nlines;
4445 s->selected_line = 1;
4446 s->blame_complete = 0;
4447 s->repo = repo;
4448 s->commit_id = commit_id;
4449 memset(&s->blame, 0, sizeof(s->blame));
4451 STAILQ_INIT(&s->colors);
4452 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4453 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4454 get_color_value("TOG_COLOR_COMMIT"));
4455 if (err)
4456 return err;
4459 view->show = show_blame_view;
4460 view->input = input_blame_view;
4461 view->close = close_blame_view;
4462 view->search_start = search_start_blame_view;
4463 view->search_next = search_next_blame_view;
4465 return run_blame(view);
4468 static const struct got_error *
4469 close_blame_view(struct tog_view *view)
4471 const struct got_error *err = NULL;
4472 struct tog_blame_view_state *s = &view->state.blame;
4474 if (s->blame.thread)
4475 err = stop_blame(&s->blame);
4477 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4478 struct got_object_qid *blamed_commit;
4479 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4480 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4481 got_object_qid_free(blamed_commit);
4484 free(s->path);
4485 free_colors(&s->colors);
4487 return err;
4490 static const struct got_error *
4491 search_start_blame_view(struct tog_view *view)
4493 struct tog_blame_view_state *s = &view->state.blame;
4495 s->matched_line = 0;
4496 return NULL;
4499 static const struct got_error *
4500 search_next_blame_view(struct tog_view *view)
4502 struct tog_blame_view_state *s = &view->state.blame;
4503 int lineno;
4504 char *line = NULL;
4505 size_t linesize = 0;
4506 ssize_t linelen;
4508 if (!view->searching) {
4509 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4510 return NULL;
4513 if (s->matched_line) {
4514 if (view->searching == TOG_SEARCH_FORWARD)
4515 lineno = s->matched_line + 1;
4516 else
4517 lineno = s->matched_line - 1;
4518 } else
4519 lineno = s->first_displayed_line - 1 + s->selected_line;
4521 while (1) {
4522 off_t offset;
4524 if (lineno <= 0 || lineno > s->blame.nlines) {
4525 if (s->matched_line == 0) {
4526 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4527 break;
4530 if (view->searching == TOG_SEARCH_FORWARD)
4531 lineno = 1;
4532 else
4533 lineno = s->blame.nlines;
4536 offset = s->blame.line_offsets[lineno - 1];
4537 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4538 free(line);
4539 return got_error_from_errno("fseeko");
4541 linelen = getline(&line, &linesize, s->blame.f);
4542 if (linelen != -1 &&
4543 match_line(line, &view->regex, 1, &view->regmatch)) {
4544 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4545 s->matched_line = lineno;
4546 break;
4548 if (view->searching == TOG_SEARCH_FORWARD)
4549 lineno++;
4550 else
4551 lineno--;
4553 free(line);
4555 if (s->matched_line) {
4556 s->first_displayed_line = s->matched_line;
4557 s->selected_line = 1;
4560 return NULL;
4563 static const struct got_error *
4564 show_blame_view(struct tog_view *view)
4566 const struct got_error *err = NULL;
4567 struct tog_blame_view_state *s = &view->state.blame;
4568 int errcode;
4570 if (s->blame.thread == NULL && !s->blame_complete) {
4571 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4572 &s->blame.thread_args);
4573 if (errcode)
4574 return got_error_set_errno(errcode, "pthread_create");
4576 halfdelay(1); /* fast refresh while annotating */
4579 if (s->blame_complete)
4580 halfdelay(10); /* disable fast refresh */
4582 err = draw_blame(view);
4584 view_vborder(view);
4585 return err;
4588 static const struct got_error *
4589 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4591 const struct got_error *err = NULL, *thread_err = NULL;
4592 struct tog_view *diff_view;
4593 struct tog_blame_view_state *s = &view->state.blame;
4594 int begin_x = 0;
4596 switch (ch) {
4597 case 'q':
4598 s->done = 1;
4599 break;
4600 case 'g':
4601 case KEY_HOME:
4602 s->selected_line = 1;
4603 s->first_displayed_line = 1;
4604 break;
4605 case 'G':
4606 case KEY_END:
4607 if (s->blame.nlines < view->nlines - 2) {
4608 s->selected_line = s->blame.nlines;
4609 s->first_displayed_line = 1;
4610 } else {
4611 s->selected_line = view->nlines - 2;
4612 s->first_displayed_line = s->blame.nlines -
4613 (view->nlines - 3);
4615 break;
4616 case 'k':
4617 case KEY_UP:
4618 case CTRL('p'):
4619 if (s->selected_line > 1)
4620 s->selected_line--;
4621 else if (s->selected_line == 1 &&
4622 s->first_displayed_line > 1)
4623 s->first_displayed_line--;
4624 break;
4625 case KEY_PPAGE:
4626 case CTRL('b'):
4627 if (s->first_displayed_line == 1) {
4628 s->selected_line = 1;
4629 break;
4631 if (s->first_displayed_line > view->nlines - 2)
4632 s->first_displayed_line -=
4633 (view->nlines - 2);
4634 else
4635 s->first_displayed_line = 1;
4636 break;
4637 case 'j':
4638 case KEY_DOWN:
4639 case CTRL('n'):
4640 if (s->selected_line < view->nlines - 2 &&
4641 s->first_displayed_line +
4642 s->selected_line <= s->blame.nlines)
4643 s->selected_line++;
4644 else if (s->last_displayed_line <
4645 s->blame.nlines)
4646 s->first_displayed_line++;
4647 break;
4648 case 'b':
4649 case 'p': {
4650 struct got_object_id *id = NULL;
4651 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4652 s->first_displayed_line, s->selected_line);
4653 if (id == NULL)
4654 break;
4655 if (ch == 'p') {
4656 struct got_commit_object *commit, *pcommit;
4657 struct got_object_qid *pid;
4658 struct got_object_id *blob_id = NULL;
4659 int obj_type;
4660 err = got_object_open_as_commit(&commit,
4661 s->repo, id);
4662 if (err)
4663 break;
4664 pid = STAILQ_FIRST(
4665 got_object_commit_get_parent_ids(commit));
4666 if (pid == NULL) {
4667 got_object_commit_close(commit);
4668 break;
4670 /* Check if path history ends here. */
4671 err = got_object_open_as_commit(&pcommit,
4672 s->repo, pid->id);
4673 if (err)
4674 break;
4675 err = got_object_id_by_path(&blob_id, s->repo,
4676 pcommit, s->path);
4677 got_object_commit_close(pcommit);
4678 if (err) {
4679 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4680 err = NULL;
4681 got_object_commit_close(commit);
4682 break;
4684 err = got_object_get_type(&obj_type, s->repo,
4685 blob_id);
4686 free(blob_id);
4687 /* Can't blame non-blob type objects. */
4688 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4689 got_object_commit_close(commit);
4690 break;
4692 err = got_object_qid_alloc(&s->blamed_commit,
4693 pid->id);
4694 got_object_commit_close(commit);
4695 } else {
4696 if (got_object_id_cmp(id,
4697 s->blamed_commit->id) == 0)
4698 break;
4699 err = got_object_qid_alloc(&s->blamed_commit,
4700 id);
4702 if (err)
4703 break;
4704 s->done = 1;
4705 thread_err = stop_blame(&s->blame);
4706 s->done = 0;
4707 if (thread_err)
4708 break;
4709 STAILQ_INSERT_HEAD(&s->blamed_commits,
4710 s->blamed_commit, entry);
4711 err = run_blame(view);
4712 if (err)
4713 break;
4714 break;
4716 case 'B': {
4717 struct got_object_qid *first;
4718 first = STAILQ_FIRST(&s->blamed_commits);
4719 if (!got_object_id_cmp(first->id, s->commit_id))
4720 break;
4721 s->done = 1;
4722 thread_err = stop_blame(&s->blame);
4723 s->done = 0;
4724 if (thread_err)
4725 break;
4726 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4727 got_object_qid_free(s->blamed_commit);
4728 s->blamed_commit =
4729 STAILQ_FIRST(&s->blamed_commits);
4730 err = run_blame(view);
4731 if (err)
4732 break;
4733 break;
4735 case KEY_ENTER:
4736 case '\r': {
4737 struct got_object_id *id = NULL;
4738 struct got_object_qid *pid;
4739 struct got_commit_object *commit = NULL;
4740 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4741 s->first_displayed_line, s->selected_line);
4742 if (id == NULL)
4743 break;
4744 err = got_object_open_as_commit(&commit, s->repo, id);
4745 if (err)
4746 break;
4747 pid = STAILQ_FIRST(
4748 got_object_commit_get_parent_ids(commit));
4749 if (view_is_parent_view(view))
4750 begin_x = view_split_begin_x(view->begin_x);
4751 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4752 if (diff_view == NULL) {
4753 got_object_commit_close(commit);
4754 err = got_error_from_errno("view_open");
4755 break;
4757 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4758 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4759 got_object_commit_close(commit);
4760 if (err) {
4761 view_close(diff_view);
4762 break;
4764 view->focussed = 0;
4765 diff_view->focussed = 1;
4766 if (view_is_parent_view(view)) {
4767 err = view_close_child(view);
4768 if (err)
4769 break;
4770 view_set_child(view, diff_view);
4771 view->focus_child = 1;
4772 } else
4773 *new_view = diff_view;
4774 if (err)
4775 break;
4776 break;
4778 case KEY_NPAGE:
4779 case CTRL('f'):
4780 case ' ':
4781 if (s->last_displayed_line >= s->blame.nlines &&
4782 s->selected_line >= MIN(s->blame.nlines,
4783 view->nlines - 2)) {
4784 break;
4786 if (s->last_displayed_line >= s->blame.nlines &&
4787 s->selected_line < view->nlines - 2) {
4788 s->selected_line = MIN(s->blame.nlines,
4789 view->nlines - 2);
4790 break;
4792 if (s->last_displayed_line + view->nlines - 2
4793 <= s->blame.nlines)
4794 s->first_displayed_line +=
4795 view->nlines - 2;
4796 else
4797 s->first_displayed_line =
4798 s->blame.nlines -
4799 (view->nlines - 3);
4800 break;
4801 case KEY_RESIZE:
4802 if (s->selected_line > view->nlines - 2) {
4803 s->selected_line = MIN(s->blame.nlines,
4804 view->nlines - 2);
4806 break;
4807 default:
4808 break;
4810 return thread_err ? thread_err : err;
4813 static const struct got_error *
4814 cmd_blame(int argc, char *argv[])
4816 const struct got_error *error;
4817 struct got_repository *repo = NULL;
4818 struct got_worktree *worktree = NULL;
4819 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4820 char *link_target = NULL;
4821 struct got_object_id *commit_id = NULL;
4822 struct got_commit_object *commit = NULL;
4823 char *commit_id_str = NULL;
4824 int ch;
4825 struct tog_view *view;
4827 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4828 switch (ch) {
4829 case 'c':
4830 commit_id_str = optarg;
4831 break;
4832 case 'r':
4833 repo_path = realpath(optarg, NULL);
4834 if (repo_path == NULL)
4835 return got_error_from_errno2("realpath",
4836 optarg);
4837 break;
4838 default:
4839 usage_blame();
4840 /* NOTREACHED */
4844 argc -= optind;
4845 argv += optind;
4847 if (argc != 1)
4848 usage_blame();
4850 if (repo_path == NULL) {
4851 cwd = getcwd(NULL, 0);
4852 if (cwd == NULL)
4853 return got_error_from_errno("getcwd");
4854 error = got_worktree_open(&worktree, cwd);
4855 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4856 goto done;
4857 if (worktree)
4858 repo_path =
4859 strdup(got_worktree_get_repo_path(worktree));
4860 else
4861 repo_path = strdup(cwd);
4862 if (repo_path == NULL) {
4863 error = got_error_from_errno("strdup");
4864 goto done;
4868 error = got_repo_open(&repo, repo_path, NULL);
4869 if (error != NULL)
4870 goto done;
4872 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4873 worktree);
4874 if (error)
4875 goto done;
4877 init_curses();
4879 error = apply_unveil(got_repo_get_path(repo), NULL);
4880 if (error)
4881 goto done;
4883 error = tog_load_refs(repo, 0);
4884 if (error)
4885 goto done;
4887 if (commit_id_str == NULL) {
4888 struct got_reference *head_ref;
4889 error = got_ref_open(&head_ref, repo, worktree ?
4890 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4891 if (error != NULL)
4892 goto done;
4893 error = got_ref_resolve(&commit_id, repo, head_ref);
4894 got_ref_close(head_ref);
4895 } else {
4896 error = got_repo_match_object_id(&commit_id, NULL,
4897 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4899 if (error != NULL)
4900 goto done;
4902 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4903 if (view == NULL) {
4904 error = got_error_from_errno("view_open");
4905 goto done;
4908 error = got_object_open_as_commit(&commit, repo, commit_id);
4909 if (error)
4910 goto done;
4912 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4913 commit, repo);
4914 if (error)
4915 goto done;
4917 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4918 commit_id, repo);
4919 if (error)
4920 goto done;
4921 if (worktree) {
4922 /* Release work tree lock. */
4923 got_worktree_close(worktree);
4924 worktree = NULL;
4926 error = view_loop(view);
4927 done:
4928 free(repo_path);
4929 free(in_repo_path);
4930 free(link_target);
4931 free(cwd);
4932 free(commit_id);
4933 if (commit)
4934 got_object_commit_close(commit);
4935 if (worktree)
4936 got_worktree_close(worktree);
4937 if (repo) {
4938 const struct got_error *close_err = got_repo_close(repo);
4939 if (error == NULL)
4940 error = close_err;
4942 tog_free_refs();
4943 return error;
4946 static const struct got_error *
4947 draw_tree_entries(struct tog_view *view, const char *parent_path)
4949 struct tog_tree_view_state *s = &view->state.tree;
4950 const struct got_error *err = NULL;
4951 struct got_tree_entry *te;
4952 wchar_t *wline;
4953 struct tog_color *tc;
4954 int width, n, i, nentries;
4955 int limit = view->nlines;
4957 s->ndisplayed = 0;
4959 werase(view->window);
4961 if (limit == 0)
4962 return NULL;
4964 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4965 if (err)
4966 return err;
4967 if (view_needs_focus_indication(view))
4968 wstandout(view->window);
4969 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4970 if (tc)
4971 wattr_on(view->window,
4972 COLOR_PAIR(tc->colorpair), NULL);
4973 waddwstr(view->window, wline);
4974 if (tc)
4975 wattr_off(view->window,
4976 COLOR_PAIR(tc->colorpair), NULL);
4977 if (view_needs_focus_indication(view))
4978 wstandend(view->window);
4979 free(wline);
4980 wline = NULL;
4981 if (width < view->ncols - 1)
4982 waddch(view->window, '\n');
4983 if (--limit <= 0)
4984 return NULL;
4985 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4986 if (err)
4987 return err;
4988 waddwstr(view->window, wline);
4989 free(wline);
4990 wline = NULL;
4991 if (width < view->ncols - 1)
4992 waddch(view->window, '\n');
4993 if (--limit <= 0)
4994 return NULL;
4995 waddch(view->window, '\n');
4996 if (--limit <= 0)
4997 return NULL;
4999 if (s->first_displayed_entry == NULL) {
5000 te = got_object_tree_get_first_entry(s->tree);
5001 if (s->selected == 0) {
5002 if (view->focussed)
5003 wstandout(view->window);
5004 s->selected_entry = NULL;
5006 waddstr(view->window, " ..\n"); /* parent directory */
5007 if (s->selected == 0 && view->focussed)
5008 wstandend(view->window);
5009 s->ndisplayed++;
5010 if (--limit <= 0)
5011 return NULL;
5012 n = 1;
5013 } else {
5014 n = 0;
5015 te = s->first_displayed_entry;
5018 nentries = got_object_tree_get_nentries(s->tree);
5019 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5020 char *line = NULL, *id_str = NULL, *link_target = NULL;
5021 const char *modestr = "";
5022 mode_t mode;
5024 te = got_object_tree_get_entry(s->tree, i);
5025 mode = got_tree_entry_get_mode(te);
5027 if (s->show_ids) {
5028 err = got_object_id_str(&id_str,
5029 got_tree_entry_get_id(te));
5030 if (err)
5031 return got_error_from_errno(
5032 "got_object_id_str");
5034 if (got_object_tree_entry_is_submodule(te))
5035 modestr = "$";
5036 else if (S_ISLNK(mode)) {
5037 int i;
5039 err = got_tree_entry_get_symlink_target(&link_target,
5040 te, s->repo);
5041 if (err) {
5042 free(id_str);
5043 return err;
5045 for (i = 0; i < strlen(link_target); i++) {
5046 if (!isprint((unsigned char)link_target[i]))
5047 link_target[i] = '?';
5049 modestr = "@";
5051 else if (S_ISDIR(mode))
5052 modestr = "/";
5053 else if (mode & S_IXUSR)
5054 modestr = "*";
5055 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5056 got_tree_entry_get_name(te), modestr,
5057 link_target ? " -> ": "",
5058 link_target ? link_target : "") == -1) {
5059 free(id_str);
5060 free(link_target);
5061 return got_error_from_errno("asprintf");
5063 free(id_str);
5064 free(link_target);
5065 err = format_line(&wline, &width, line, view->ncols, 0);
5066 if (err) {
5067 free(line);
5068 break;
5070 if (n == s->selected) {
5071 if (view->focussed)
5072 wstandout(view->window);
5073 s->selected_entry = te;
5075 tc = match_color(&s->colors, line);
5076 if (tc)
5077 wattr_on(view->window,
5078 COLOR_PAIR(tc->colorpair), NULL);
5079 waddwstr(view->window, wline);
5080 if (tc)
5081 wattr_off(view->window,
5082 COLOR_PAIR(tc->colorpair), NULL);
5083 if (width < view->ncols - 1)
5084 waddch(view->window, '\n');
5085 if (n == s->selected && view->focussed)
5086 wstandend(view->window);
5087 free(line);
5088 free(wline);
5089 wline = NULL;
5090 n++;
5091 s->ndisplayed++;
5092 s->last_displayed_entry = te;
5093 if (--limit <= 0)
5094 break;
5097 return err;
5100 static void
5101 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5103 struct got_tree_entry *te;
5104 int isroot = s->tree == s->root;
5105 int i = 0;
5107 if (s->first_displayed_entry == NULL)
5108 return;
5110 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5111 while (i++ < maxscroll) {
5112 if (te == NULL) {
5113 if (!isroot)
5114 s->first_displayed_entry = NULL;
5115 break;
5117 s->first_displayed_entry = te;
5118 te = got_tree_entry_get_prev(s->tree, te);
5122 static void
5123 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5125 struct got_tree_entry *next, *last;
5126 int n = 0;
5128 if (s->first_displayed_entry)
5129 next = got_tree_entry_get_next(s->tree,
5130 s->first_displayed_entry);
5131 else
5132 next = got_object_tree_get_first_entry(s->tree);
5134 last = s->last_displayed_entry;
5135 while (next && last && n++ < maxscroll) {
5136 last = got_tree_entry_get_next(s->tree, last);
5137 if (last) {
5138 s->first_displayed_entry = next;
5139 next = got_tree_entry_get_next(s->tree, next);
5144 static const struct got_error *
5145 tree_entry_path(char **path, struct tog_parent_trees *parents,
5146 struct got_tree_entry *te)
5148 const struct got_error *err = NULL;
5149 struct tog_parent_tree *pt;
5150 size_t len = 2; /* for leading slash and NUL */
5152 TAILQ_FOREACH(pt, parents, entry)
5153 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5154 + 1 /* slash */;
5155 if (te)
5156 len += strlen(got_tree_entry_get_name(te));
5158 *path = calloc(1, len);
5159 if (path == NULL)
5160 return got_error_from_errno("calloc");
5162 (*path)[0] = '/';
5163 pt = TAILQ_LAST(parents, tog_parent_trees);
5164 while (pt) {
5165 const char *name = got_tree_entry_get_name(pt->selected_entry);
5166 if (strlcat(*path, name, len) >= len) {
5167 err = got_error(GOT_ERR_NO_SPACE);
5168 goto done;
5170 if (strlcat(*path, "/", len) >= len) {
5171 err = got_error(GOT_ERR_NO_SPACE);
5172 goto done;
5174 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5176 if (te) {
5177 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5178 err = got_error(GOT_ERR_NO_SPACE);
5179 goto done;
5182 done:
5183 if (err) {
5184 free(*path);
5185 *path = NULL;
5187 return err;
5190 static const struct got_error *
5191 blame_tree_entry(struct tog_view **new_view, int begin_x,
5192 struct got_tree_entry *te, struct tog_parent_trees *parents,
5193 struct got_object_id *commit_id, struct got_repository *repo)
5195 const struct got_error *err = NULL;
5196 char *path;
5197 struct tog_view *blame_view;
5199 *new_view = NULL;
5201 err = tree_entry_path(&path, parents, te);
5202 if (err)
5203 return err;
5205 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5206 if (blame_view == NULL) {
5207 err = got_error_from_errno("view_open");
5208 goto done;
5211 err = open_blame_view(blame_view, path, commit_id, repo);
5212 if (err) {
5213 if (err->code == GOT_ERR_CANCELLED)
5214 err = NULL;
5215 view_close(blame_view);
5216 } else
5217 *new_view = blame_view;
5218 done:
5219 free(path);
5220 return err;
5223 static const struct got_error *
5224 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5225 struct tog_tree_view_state *s)
5227 struct tog_view *log_view;
5228 const struct got_error *err = NULL;
5229 char *path;
5231 *new_view = NULL;
5233 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5234 if (log_view == NULL)
5235 return got_error_from_errno("view_open");
5237 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5238 if (err)
5239 return err;
5241 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5242 path, 0);
5243 if (err)
5244 view_close(log_view);
5245 else
5246 *new_view = log_view;
5247 free(path);
5248 return err;
5251 static const struct got_error *
5252 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5253 const char *head_ref_name, struct got_repository *repo)
5255 const struct got_error *err = NULL;
5256 char *commit_id_str = NULL;
5257 struct tog_tree_view_state *s = &view->state.tree;
5258 struct got_commit_object *commit = NULL;
5260 TAILQ_INIT(&s->parents);
5261 STAILQ_INIT(&s->colors);
5263 s->commit_id = got_object_id_dup(commit_id);
5264 if (s->commit_id == NULL)
5265 return got_error_from_errno("got_object_id_dup");
5267 err = got_object_open_as_commit(&commit, repo, commit_id);
5268 if (err)
5269 goto done;
5272 * The root is opened here and will be closed when the view is closed.
5273 * Any visited subtrees and their path-wise parents are opened and
5274 * closed on demand.
5276 err = got_object_open_as_tree(&s->root, repo,
5277 got_object_commit_get_tree_id(commit));
5278 if (err)
5279 goto done;
5280 s->tree = s->root;
5282 err = got_object_id_str(&commit_id_str, commit_id);
5283 if (err != NULL)
5284 goto done;
5286 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5287 err = got_error_from_errno("asprintf");
5288 goto done;
5291 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5292 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5293 if (head_ref_name) {
5294 s->head_ref_name = strdup(head_ref_name);
5295 if (s->head_ref_name == NULL) {
5296 err = got_error_from_errno("strdup");
5297 goto done;
5300 s->repo = repo;
5302 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5303 err = add_color(&s->colors, "\\$$",
5304 TOG_COLOR_TREE_SUBMODULE,
5305 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5306 if (err)
5307 goto done;
5308 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5309 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5310 if (err)
5311 goto done;
5312 err = add_color(&s->colors, "/$",
5313 TOG_COLOR_TREE_DIRECTORY,
5314 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5315 if (err)
5316 goto done;
5318 err = add_color(&s->colors, "\\*$",
5319 TOG_COLOR_TREE_EXECUTABLE,
5320 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5321 if (err)
5322 goto done;
5324 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5325 get_color_value("TOG_COLOR_COMMIT"));
5326 if (err)
5327 goto done;
5330 view->show = show_tree_view;
5331 view->input = input_tree_view;
5332 view->close = close_tree_view;
5333 view->search_start = search_start_tree_view;
5334 view->search_next = search_next_tree_view;
5335 done:
5336 free(commit_id_str);
5337 if (commit)
5338 got_object_commit_close(commit);
5339 if (err)
5340 close_tree_view(view);
5341 return err;
5344 static const struct got_error *
5345 close_tree_view(struct tog_view *view)
5347 struct tog_tree_view_state *s = &view->state.tree;
5349 free_colors(&s->colors);
5350 free(s->tree_label);
5351 s->tree_label = NULL;
5352 free(s->commit_id);
5353 s->commit_id = NULL;
5354 free(s->head_ref_name);
5355 s->head_ref_name = NULL;
5356 while (!TAILQ_EMPTY(&s->parents)) {
5357 struct tog_parent_tree *parent;
5358 parent = TAILQ_FIRST(&s->parents);
5359 TAILQ_REMOVE(&s->parents, parent, entry);
5360 if (parent->tree != s->root)
5361 got_object_tree_close(parent->tree);
5362 free(parent);
5365 if (s->tree != NULL && s->tree != s->root)
5366 got_object_tree_close(s->tree);
5367 if (s->root)
5368 got_object_tree_close(s->root);
5369 return NULL;
5372 static const struct got_error *
5373 search_start_tree_view(struct tog_view *view)
5375 struct tog_tree_view_state *s = &view->state.tree;
5377 s->matched_entry = NULL;
5378 return NULL;
5381 static int
5382 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5384 regmatch_t regmatch;
5386 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5387 0) == 0;
5390 static const struct got_error *
5391 search_next_tree_view(struct tog_view *view)
5393 struct tog_tree_view_state *s = &view->state.tree;
5394 struct got_tree_entry *te = NULL;
5396 if (!view->searching) {
5397 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5398 return NULL;
5401 if (s->matched_entry) {
5402 if (view->searching == TOG_SEARCH_FORWARD) {
5403 if (s->selected_entry)
5404 te = got_tree_entry_get_next(s->tree,
5405 s->selected_entry);
5406 else
5407 te = got_object_tree_get_first_entry(s->tree);
5408 } else {
5409 if (s->selected_entry == NULL)
5410 te = got_object_tree_get_last_entry(s->tree);
5411 else
5412 te = got_tree_entry_get_prev(s->tree,
5413 s->selected_entry);
5415 } else {
5416 if (s->selected_entry)
5417 te = s->selected_entry;
5418 else if (view->searching == TOG_SEARCH_FORWARD)
5419 te = got_object_tree_get_first_entry(s->tree);
5420 else
5421 te = got_object_tree_get_last_entry(s->tree);
5424 while (1) {
5425 if (te == NULL) {
5426 if (s->matched_entry == NULL) {
5427 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5428 return NULL;
5430 if (view->searching == TOG_SEARCH_FORWARD)
5431 te = got_object_tree_get_first_entry(s->tree);
5432 else
5433 te = got_object_tree_get_last_entry(s->tree);
5436 if (match_tree_entry(te, &view->regex)) {
5437 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5438 s->matched_entry = te;
5439 break;
5442 if (view->searching == TOG_SEARCH_FORWARD)
5443 te = got_tree_entry_get_next(s->tree, te);
5444 else
5445 te = got_tree_entry_get_prev(s->tree, te);
5448 if (s->matched_entry) {
5449 s->first_displayed_entry = s->matched_entry;
5450 s->selected = 0;
5453 return NULL;
5456 static const struct got_error *
5457 show_tree_view(struct tog_view *view)
5459 const struct got_error *err = NULL;
5460 struct tog_tree_view_state *s = &view->state.tree;
5461 char *parent_path;
5463 err = tree_entry_path(&parent_path, &s->parents, NULL);
5464 if (err)
5465 return err;
5467 err = draw_tree_entries(view, parent_path);
5468 free(parent_path);
5470 view_vborder(view);
5471 return err;
5474 static const struct got_error *
5475 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5477 const struct got_error *err = NULL;
5478 struct tog_tree_view_state *s = &view->state.tree;
5479 struct tog_view *log_view, *ref_view;
5480 struct got_tree_entry *te;
5481 int begin_x = 0, n;
5483 switch (ch) {
5484 case 'i':
5485 s->show_ids = !s->show_ids;
5486 break;
5487 case 'l':
5488 if (!s->selected_entry)
5489 break;
5490 if (view_is_parent_view(view))
5491 begin_x = view_split_begin_x(view->begin_x);
5492 err = log_selected_tree_entry(&log_view, begin_x, s);
5493 view->focussed = 0;
5494 log_view->focussed = 1;
5495 if (view_is_parent_view(view)) {
5496 err = view_close_child(view);
5497 if (err)
5498 return err;
5499 view_set_child(view, log_view);
5500 view->focus_child = 1;
5501 } else
5502 *new_view = log_view;
5503 break;
5504 case 'r':
5505 if (view_is_parent_view(view))
5506 begin_x = view_split_begin_x(view->begin_x);
5507 ref_view = view_open(view->nlines, view->ncols,
5508 view->begin_y, begin_x, TOG_VIEW_REF);
5509 if (ref_view == NULL)
5510 return got_error_from_errno("view_open");
5511 err = open_ref_view(ref_view, s->repo);
5512 if (err) {
5513 view_close(ref_view);
5514 return err;
5516 view->focussed = 0;
5517 ref_view->focussed = 1;
5518 if (view_is_parent_view(view)) {
5519 err = view_close_child(view);
5520 if (err)
5521 return err;
5522 view_set_child(view, ref_view);
5523 view->focus_child = 1;
5524 } else
5525 *new_view = ref_view;
5526 break;
5527 case 'g':
5528 case KEY_HOME:
5529 s->selected = 0;
5530 if (s->tree == s->root)
5531 s->first_displayed_entry =
5532 got_object_tree_get_first_entry(s->tree);
5533 else
5534 s->first_displayed_entry = NULL;
5535 break;
5536 case 'G':
5537 case KEY_END:
5538 s->selected = 0;
5539 te = got_object_tree_get_last_entry(s->tree);
5540 for (n = 0; n < view->nlines - 3; n++) {
5541 if (te == NULL) {
5542 if(s->tree != s->root) {
5543 s->first_displayed_entry = NULL;
5544 n++;
5546 break;
5548 s->first_displayed_entry = te;
5549 te = got_tree_entry_get_prev(s->tree, te);
5551 if (n > 0)
5552 s->selected = n - 1;
5553 break;
5554 case 'k':
5555 case KEY_UP:
5556 case CTRL('p'):
5557 if (s->selected > 0) {
5558 s->selected--;
5559 break;
5561 tree_scroll_up(s, 1);
5562 break;
5563 case KEY_PPAGE:
5564 case CTRL('b'):
5565 if (s->tree == s->root) {
5566 if (got_object_tree_get_first_entry(s->tree) ==
5567 s->first_displayed_entry)
5568 s->selected = 0;
5569 } else {
5570 if (s->first_displayed_entry == NULL)
5571 s->selected = 0;
5573 tree_scroll_up(s, MAX(0, view->nlines - 3));
5574 break;
5575 case 'j':
5576 case KEY_DOWN:
5577 case CTRL('n'):
5578 if (s->selected < s->ndisplayed - 1) {
5579 s->selected++;
5580 break;
5582 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5583 == NULL)
5584 /* can't scroll any further */
5585 break;
5586 tree_scroll_down(s, 1);
5587 break;
5588 case KEY_NPAGE:
5589 case CTRL('f'):
5590 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5591 == NULL) {
5592 /* can't scroll any further; move cursor down */
5593 if (s->selected < s->ndisplayed - 1)
5594 s->selected = s->ndisplayed - 1;
5595 break;
5597 tree_scroll_down(s, view->nlines - 3);
5598 break;
5599 case KEY_ENTER:
5600 case '\r':
5601 case KEY_BACKSPACE:
5602 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5603 struct tog_parent_tree *parent;
5604 /* user selected '..' */
5605 if (s->tree == s->root)
5606 break;
5607 parent = TAILQ_FIRST(&s->parents);
5608 TAILQ_REMOVE(&s->parents, parent,
5609 entry);
5610 got_object_tree_close(s->tree);
5611 s->tree = parent->tree;
5612 s->first_displayed_entry =
5613 parent->first_displayed_entry;
5614 s->selected_entry =
5615 parent->selected_entry;
5616 s->selected = parent->selected;
5617 free(parent);
5618 } else if (S_ISDIR(got_tree_entry_get_mode(
5619 s->selected_entry))) {
5620 struct got_tree_object *subtree;
5621 err = got_object_open_as_tree(&subtree, s->repo,
5622 got_tree_entry_get_id(s->selected_entry));
5623 if (err)
5624 break;
5625 err = tree_view_visit_subtree(s, subtree);
5626 if (err) {
5627 got_object_tree_close(subtree);
5628 break;
5630 } else if (S_ISREG(got_tree_entry_get_mode(
5631 s->selected_entry))) {
5632 struct tog_view *blame_view;
5633 int begin_x = view_is_parent_view(view) ?
5634 view_split_begin_x(view->begin_x) : 0;
5636 err = blame_tree_entry(&blame_view, begin_x,
5637 s->selected_entry, &s->parents,
5638 s->commit_id, s->repo);
5639 if (err)
5640 break;
5641 view->focussed = 0;
5642 blame_view->focussed = 1;
5643 if (view_is_parent_view(view)) {
5644 err = view_close_child(view);
5645 if (err)
5646 return err;
5647 view_set_child(view, blame_view);
5648 view->focus_child = 1;
5649 } else
5650 *new_view = blame_view;
5652 break;
5653 case KEY_RESIZE:
5654 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5655 s->selected = view->nlines - 4;
5656 break;
5657 default:
5658 break;
5661 return err;
5664 __dead static void
5665 usage_tree(void)
5667 endwin();
5668 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5669 getprogname());
5670 exit(1);
5673 static const struct got_error *
5674 cmd_tree(int argc, char *argv[])
5676 const struct got_error *error;
5677 struct got_repository *repo = NULL;
5678 struct got_worktree *worktree = NULL;
5679 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5680 struct got_object_id *commit_id = NULL;
5681 struct got_commit_object *commit = NULL;
5682 const char *commit_id_arg = NULL;
5683 char *label = NULL;
5684 struct got_reference *ref = NULL;
5685 const char *head_ref_name = NULL;
5686 int ch;
5687 struct tog_view *view;
5689 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5690 switch (ch) {
5691 case 'c':
5692 commit_id_arg = optarg;
5693 break;
5694 case 'r':
5695 repo_path = realpath(optarg, NULL);
5696 if (repo_path == NULL)
5697 return got_error_from_errno2("realpath",
5698 optarg);
5699 break;
5700 default:
5701 usage_tree();
5702 /* NOTREACHED */
5706 argc -= optind;
5707 argv += optind;
5709 if (argc > 1)
5710 usage_tree();
5712 if (repo_path == NULL) {
5713 cwd = getcwd(NULL, 0);
5714 if (cwd == NULL)
5715 return got_error_from_errno("getcwd");
5716 error = got_worktree_open(&worktree, cwd);
5717 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5718 goto done;
5719 if (worktree)
5720 repo_path =
5721 strdup(got_worktree_get_repo_path(worktree));
5722 else
5723 repo_path = strdup(cwd);
5724 if (repo_path == NULL) {
5725 error = got_error_from_errno("strdup");
5726 goto done;
5730 error = got_repo_open(&repo, repo_path, NULL);
5731 if (error != NULL)
5732 goto done;
5734 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5735 repo, worktree);
5736 if (error)
5737 goto done;
5739 init_curses();
5741 error = apply_unveil(got_repo_get_path(repo), NULL);
5742 if (error)
5743 goto done;
5745 error = tog_load_refs(repo, 0);
5746 if (error)
5747 goto done;
5749 if (commit_id_arg == NULL) {
5750 error = got_repo_match_object_id(&commit_id, &label,
5751 worktree ? got_worktree_get_head_ref_name(worktree) :
5752 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5753 if (error)
5754 goto done;
5755 head_ref_name = label;
5756 } else {
5757 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5758 if (error == NULL)
5759 head_ref_name = got_ref_get_name(ref);
5760 else if (error->code != GOT_ERR_NOT_REF)
5761 goto done;
5762 error = got_repo_match_object_id(&commit_id, NULL,
5763 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5764 if (error)
5765 goto done;
5768 error = got_object_open_as_commit(&commit, repo, commit_id);
5769 if (error)
5770 goto done;
5772 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5773 if (view == NULL) {
5774 error = got_error_from_errno("view_open");
5775 goto done;
5777 error = open_tree_view(view, commit_id, head_ref_name, repo);
5778 if (error)
5779 goto done;
5780 if (!got_path_is_root_dir(in_repo_path)) {
5781 error = tree_view_walk_path(&view->state.tree, commit,
5782 in_repo_path);
5783 if (error)
5784 goto done;
5787 if (worktree) {
5788 /* Release work tree lock. */
5789 got_worktree_close(worktree);
5790 worktree = NULL;
5792 error = view_loop(view);
5793 done:
5794 free(repo_path);
5795 free(cwd);
5796 free(commit_id);
5797 free(label);
5798 if (ref)
5799 got_ref_close(ref);
5800 if (repo) {
5801 const struct got_error *close_err = got_repo_close(repo);
5802 if (error == NULL)
5803 error = close_err;
5805 tog_free_refs();
5806 return error;
5809 static const struct got_error *
5810 ref_view_load_refs(struct tog_ref_view_state *s)
5812 struct got_reflist_entry *sre;
5813 struct tog_reflist_entry *re;
5815 s->nrefs = 0;
5816 TAILQ_FOREACH(sre, &tog_refs, entry) {
5817 if (strncmp(got_ref_get_name(sre->ref),
5818 "refs/got/", 9) == 0 &&
5819 strncmp(got_ref_get_name(sre->ref),
5820 "refs/got/backup/", 16) != 0)
5821 continue;
5823 re = malloc(sizeof(*re));
5824 if (re == NULL)
5825 return got_error_from_errno("malloc");
5827 re->ref = got_ref_dup(sre->ref);
5828 if (re->ref == NULL)
5829 return got_error_from_errno("got_ref_dup");
5830 re->idx = s->nrefs++;
5831 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5834 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5835 return NULL;
5838 void
5839 ref_view_free_refs(struct tog_ref_view_state *s)
5841 struct tog_reflist_entry *re;
5843 while (!TAILQ_EMPTY(&s->refs)) {
5844 re = TAILQ_FIRST(&s->refs);
5845 TAILQ_REMOVE(&s->refs, re, entry);
5846 got_ref_close(re->ref);
5847 free(re);
5851 static const struct got_error *
5852 open_ref_view(struct tog_view *view, struct got_repository *repo)
5854 const struct got_error *err = NULL;
5855 struct tog_ref_view_state *s = &view->state.ref;
5857 s->selected_entry = 0;
5858 s->repo = repo;
5860 TAILQ_INIT(&s->refs);
5861 STAILQ_INIT(&s->colors);
5863 err = ref_view_load_refs(s);
5864 if (err)
5865 return err;
5867 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5868 err = add_color(&s->colors, "^refs/heads/",
5869 TOG_COLOR_REFS_HEADS,
5870 get_color_value("TOG_COLOR_REFS_HEADS"));
5871 if (err)
5872 goto done;
5874 err = add_color(&s->colors, "^refs/tags/",
5875 TOG_COLOR_REFS_TAGS,
5876 get_color_value("TOG_COLOR_REFS_TAGS"));
5877 if (err)
5878 goto done;
5880 err = add_color(&s->colors, "^refs/remotes/",
5881 TOG_COLOR_REFS_REMOTES,
5882 get_color_value("TOG_COLOR_REFS_REMOTES"));
5883 if (err)
5884 goto done;
5886 err = add_color(&s->colors, "^refs/got/backup/",
5887 TOG_COLOR_REFS_BACKUP,
5888 get_color_value("TOG_COLOR_REFS_BACKUP"));
5889 if (err)
5890 goto done;
5893 view->show = show_ref_view;
5894 view->input = input_ref_view;
5895 view->close = close_ref_view;
5896 view->search_start = search_start_ref_view;
5897 view->search_next = search_next_ref_view;
5898 done:
5899 if (err)
5900 free_colors(&s->colors);
5901 return err;
5904 static const struct got_error *
5905 close_ref_view(struct tog_view *view)
5907 struct tog_ref_view_state *s = &view->state.ref;
5909 ref_view_free_refs(s);
5910 free_colors(&s->colors);
5912 return NULL;
5915 static const struct got_error *
5916 resolve_reflist_entry(struct got_object_id **commit_id,
5917 struct tog_reflist_entry *re, struct got_repository *repo)
5919 const struct got_error *err = NULL;
5920 struct got_object_id *obj_id;
5921 struct got_tag_object *tag = NULL;
5922 int obj_type;
5924 *commit_id = NULL;
5926 err = got_ref_resolve(&obj_id, repo, re->ref);
5927 if (err)
5928 return err;
5930 err = got_object_get_type(&obj_type, repo, obj_id);
5931 if (err)
5932 goto done;
5934 switch (obj_type) {
5935 case GOT_OBJ_TYPE_COMMIT:
5936 *commit_id = obj_id;
5937 break;
5938 case GOT_OBJ_TYPE_TAG:
5939 err = got_object_open_as_tag(&tag, repo, obj_id);
5940 if (err)
5941 goto done;
5942 free(obj_id);
5943 err = got_object_get_type(&obj_type, repo,
5944 got_object_tag_get_object_id(tag));
5945 if (err)
5946 goto done;
5947 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5948 err = got_error(GOT_ERR_OBJ_TYPE);
5949 goto done;
5951 *commit_id = got_object_id_dup(
5952 got_object_tag_get_object_id(tag));
5953 if (*commit_id == NULL) {
5954 err = got_error_from_errno("got_object_id_dup");
5955 goto done;
5957 break;
5958 default:
5959 err = got_error(GOT_ERR_OBJ_TYPE);
5960 break;
5963 done:
5964 if (tag)
5965 got_object_tag_close(tag);
5966 if (err) {
5967 free(*commit_id);
5968 *commit_id = NULL;
5970 return err;
5973 static const struct got_error *
5974 log_ref_entry(struct tog_view **new_view, int begin_x,
5975 struct tog_reflist_entry *re, struct got_repository *repo)
5977 struct tog_view *log_view;
5978 const struct got_error *err = NULL;
5979 struct got_object_id *commit_id = NULL;
5981 *new_view = NULL;
5983 err = resolve_reflist_entry(&commit_id, re, repo);
5984 if (err) {
5985 if (err->code != GOT_ERR_OBJ_TYPE)
5986 return err;
5987 else
5988 return NULL;
5991 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5992 if (log_view == NULL) {
5993 err = got_error_from_errno("view_open");
5994 goto done;
5997 err = open_log_view(log_view, commit_id, repo,
5998 got_ref_get_name(re->ref), "", 0);
5999 done:
6000 if (err)
6001 view_close(log_view);
6002 else
6003 *new_view = log_view;
6004 free(commit_id);
6005 return err;
6008 static void
6009 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6011 struct tog_reflist_entry *re;
6012 int i = 0;
6014 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6015 return;
6017 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6018 while (i++ < maxscroll) {
6019 if (re == NULL)
6020 break;
6021 s->first_displayed_entry = re;
6022 re = TAILQ_PREV(re, tog_reflist_head, entry);
6026 static void
6027 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6029 struct tog_reflist_entry *next, *last;
6030 int n = 0;
6032 if (s->first_displayed_entry)
6033 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6034 else
6035 next = TAILQ_FIRST(&s->refs);
6037 last = s->last_displayed_entry;
6038 while (next && last && n++ < maxscroll) {
6039 last = TAILQ_NEXT(last, entry);
6040 if (last) {
6041 s->first_displayed_entry = next;
6042 next = TAILQ_NEXT(next, entry);
6047 static const struct got_error *
6048 search_start_ref_view(struct tog_view *view)
6050 struct tog_ref_view_state *s = &view->state.ref;
6052 s->matched_entry = NULL;
6053 return NULL;
6056 static int
6057 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6059 regmatch_t regmatch;
6061 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6062 0) == 0;
6065 static const struct got_error *
6066 search_next_ref_view(struct tog_view *view)
6068 struct tog_ref_view_state *s = &view->state.ref;
6069 struct tog_reflist_entry *re = NULL;
6071 if (!view->searching) {
6072 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6073 return NULL;
6076 if (s->matched_entry) {
6077 if (view->searching == TOG_SEARCH_FORWARD) {
6078 if (s->selected_entry)
6079 re = TAILQ_NEXT(s->selected_entry, entry);
6080 else
6081 re = TAILQ_PREV(s->selected_entry,
6082 tog_reflist_head, entry);
6083 } else {
6084 if (s->selected_entry == NULL)
6085 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6086 else
6087 re = TAILQ_PREV(s->selected_entry,
6088 tog_reflist_head, entry);
6090 } else {
6091 if (s->selected_entry)
6092 re = s->selected_entry;
6093 else if (view->searching == TOG_SEARCH_FORWARD)
6094 re = TAILQ_FIRST(&s->refs);
6095 else
6096 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6099 while (1) {
6100 if (re == NULL) {
6101 if (s->matched_entry == NULL) {
6102 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6103 return NULL;
6105 if (view->searching == TOG_SEARCH_FORWARD)
6106 re = TAILQ_FIRST(&s->refs);
6107 else
6108 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6111 if (match_reflist_entry(re, &view->regex)) {
6112 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6113 s->matched_entry = re;
6114 break;
6117 if (view->searching == TOG_SEARCH_FORWARD)
6118 re = TAILQ_NEXT(re, entry);
6119 else
6120 re = TAILQ_PREV(re, tog_reflist_head, entry);
6123 if (s->matched_entry) {
6124 s->first_displayed_entry = s->matched_entry;
6125 s->selected = 0;
6128 return NULL;
6131 static const struct got_error *
6132 show_ref_view(struct tog_view *view)
6134 const struct got_error *err = NULL;
6135 struct tog_ref_view_state *s = &view->state.ref;
6136 struct tog_reflist_entry *re;
6137 char *line = NULL;
6138 wchar_t *wline;
6139 struct tog_color *tc;
6140 int width, n;
6141 int limit = view->nlines;
6143 werase(view->window);
6145 s->ndisplayed = 0;
6147 if (limit == 0)
6148 return NULL;
6150 re = s->first_displayed_entry;
6152 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6153 s->nrefs) == -1)
6154 return got_error_from_errno("asprintf");
6156 err = format_line(&wline, &width, line, view->ncols, 0);
6157 if (err) {
6158 free(line);
6159 return err;
6161 if (view_needs_focus_indication(view))
6162 wstandout(view->window);
6163 waddwstr(view->window, wline);
6164 if (view_needs_focus_indication(view))
6165 wstandend(view->window);
6166 free(wline);
6167 wline = NULL;
6168 free(line);
6169 line = NULL;
6170 if (width < view->ncols - 1)
6171 waddch(view->window, '\n');
6172 if (--limit <= 0)
6173 return NULL;
6175 n = 0;
6176 while (re && limit > 0) {
6177 char *line = NULL;
6179 if (got_ref_is_symbolic(re->ref)) {
6180 if (asprintf(&line, "%s -> %s",
6181 got_ref_get_name(re->ref),
6182 got_ref_get_symref_target(re->ref)) == -1)
6183 return got_error_from_errno("asprintf");
6184 } else if (s->show_ids) {
6185 struct got_object_id *id;
6186 char *id_str;
6187 err = got_ref_resolve(&id, s->repo, re->ref);
6188 if (err)
6189 return err;
6190 err = got_object_id_str(&id_str, id);
6191 if (err) {
6192 free(id);
6193 return err;
6195 if (asprintf(&line, "%s: %s",
6196 got_ref_get_name(re->ref), id_str) == -1) {
6197 err = got_error_from_errno("asprintf");
6198 free(id);
6199 free(id_str);
6200 return err;
6202 free(id);
6203 free(id_str);
6204 } else {
6205 line = strdup(got_ref_get_name(re->ref));
6206 if (line == NULL)
6207 return got_error_from_errno("strdup");
6210 err = format_line(&wline, &width, line, view->ncols, 0);
6211 if (err) {
6212 free(line);
6213 return err;
6215 if (n == s->selected) {
6216 if (view->focussed)
6217 wstandout(view->window);
6218 s->selected_entry = re;
6220 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6221 if (tc)
6222 wattr_on(view->window,
6223 COLOR_PAIR(tc->colorpair), NULL);
6224 waddwstr(view->window, wline);
6225 if (tc)
6226 wattr_off(view->window,
6227 COLOR_PAIR(tc->colorpair), NULL);
6228 if (width < view->ncols - 1)
6229 waddch(view->window, '\n');
6230 if (n == s->selected && view->focussed)
6231 wstandend(view->window);
6232 free(line);
6233 free(wline);
6234 wline = NULL;
6235 n++;
6236 s->ndisplayed++;
6237 s->last_displayed_entry = re;
6239 limit--;
6240 re = TAILQ_NEXT(re, entry);
6243 view_vborder(view);
6244 return err;
6247 static const struct got_error *
6248 browse_ref_tree(struct tog_view **new_view, int begin_x,
6249 struct tog_reflist_entry *re, struct got_repository *repo)
6251 const struct got_error *err = NULL;
6252 struct got_object_id *commit_id = NULL;
6253 struct tog_view *tree_view;
6255 *new_view = NULL;
6257 err = resolve_reflist_entry(&commit_id, re, repo);
6258 if (err) {
6259 if (err->code != GOT_ERR_OBJ_TYPE)
6260 return err;
6261 else
6262 return NULL;
6266 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6267 if (tree_view == NULL) {
6268 err = got_error_from_errno("view_open");
6269 goto done;
6272 err = open_tree_view(tree_view, commit_id,
6273 got_ref_get_name(re->ref), repo);
6274 if (err)
6275 goto done;
6277 *new_view = tree_view;
6278 done:
6279 free(commit_id);
6280 return err;
6282 static const struct got_error *
6283 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6285 const struct got_error *err = NULL;
6286 struct tog_ref_view_state *s = &view->state.ref;
6287 struct tog_view *log_view, *tree_view;
6288 struct tog_reflist_entry *re;
6289 int begin_x = 0, n;
6291 switch (ch) {
6292 case 'i':
6293 s->show_ids = !s->show_ids;
6294 break;
6295 case 'o':
6296 s->sort_by_date = !s->sort_by_date;
6297 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6298 got_ref_cmp_by_commit_timestamp_descending :
6299 tog_ref_cmp_by_name, s->repo);
6300 if (err)
6301 break;
6302 got_reflist_object_id_map_free(tog_refs_idmap);
6303 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6304 &tog_refs, s->repo);
6305 if (err)
6306 break;
6307 ref_view_free_refs(s);
6308 err = ref_view_load_refs(s);
6309 break;
6310 case KEY_ENTER:
6311 case '\r':
6312 if (!s->selected_entry)
6313 break;
6314 if (view_is_parent_view(view))
6315 begin_x = view_split_begin_x(view->begin_x);
6316 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6317 s->repo);
6318 view->focussed = 0;
6319 log_view->focussed = 1;
6320 if (view_is_parent_view(view)) {
6321 err = view_close_child(view);
6322 if (err)
6323 return err;
6324 view_set_child(view, log_view);
6325 view->focus_child = 1;
6326 } else
6327 *new_view = log_view;
6328 break;
6329 case 't':
6330 if (!s->selected_entry)
6331 break;
6332 if (view_is_parent_view(view))
6333 begin_x = view_split_begin_x(view->begin_x);
6334 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6335 s->repo);
6336 if (err || tree_view == NULL)
6337 break;
6338 view->focussed = 0;
6339 tree_view->focussed = 1;
6340 if (view_is_parent_view(view)) {
6341 err = view_close_child(view);
6342 if (err)
6343 return err;
6344 view_set_child(view, tree_view);
6345 view->focus_child = 1;
6346 } else
6347 *new_view = tree_view;
6348 break;
6349 case 'g':
6350 case KEY_HOME:
6351 s->selected = 0;
6352 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6353 break;
6354 case 'G':
6355 case KEY_END:
6356 s->selected = 0;
6357 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6358 for (n = 0; n < view->nlines - 1; n++) {
6359 if (re == NULL)
6360 break;
6361 s->first_displayed_entry = re;
6362 re = TAILQ_PREV(re, tog_reflist_head, entry);
6364 if (n > 0)
6365 s->selected = n - 1;
6366 break;
6367 case 'k':
6368 case KEY_UP:
6369 case CTRL('p'):
6370 if (s->selected > 0) {
6371 s->selected--;
6372 break;
6374 ref_scroll_up(s, 1);
6375 break;
6376 case KEY_PPAGE:
6377 case CTRL('b'):
6378 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6379 s->selected = 0;
6380 ref_scroll_up(s, MAX(0, view->nlines - 1));
6381 break;
6382 case 'j':
6383 case KEY_DOWN:
6384 case CTRL('n'):
6385 if (s->selected < s->ndisplayed - 1) {
6386 s->selected++;
6387 break;
6389 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6390 /* can't scroll any further */
6391 break;
6392 ref_scroll_down(s, 1);
6393 break;
6394 case KEY_NPAGE:
6395 case CTRL('f'):
6396 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6397 /* can't scroll any further; move cursor down */
6398 if (s->selected < s->ndisplayed - 1)
6399 s->selected = s->ndisplayed - 1;
6400 break;
6402 ref_scroll_down(s, view->nlines - 1);
6403 break;
6404 case CTRL('l'):
6405 tog_free_refs();
6406 err = tog_load_refs(s->repo, s->sort_by_date);
6407 if (err)
6408 break;
6409 ref_view_free_refs(s);
6410 err = ref_view_load_refs(s);
6411 break;
6412 case KEY_RESIZE:
6413 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6414 s->selected = view->nlines - 2;
6415 break;
6416 default:
6417 break;
6420 return err;
6423 __dead static void
6424 usage_ref(void)
6426 endwin();
6427 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6428 getprogname());
6429 exit(1);
6432 static const struct got_error *
6433 cmd_ref(int argc, char *argv[])
6435 const struct got_error *error;
6436 struct got_repository *repo = NULL;
6437 struct got_worktree *worktree = NULL;
6438 char *cwd = NULL, *repo_path = NULL;
6439 int ch;
6440 struct tog_view *view;
6442 while ((ch = getopt(argc, argv, "r:")) != -1) {
6443 switch (ch) {
6444 case 'r':
6445 repo_path = realpath(optarg, NULL);
6446 if (repo_path == NULL)
6447 return got_error_from_errno2("realpath",
6448 optarg);
6449 break;
6450 default:
6451 usage_ref();
6452 /* NOTREACHED */
6456 argc -= optind;
6457 argv += optind;
6459 if (argc > 1)
6460 usage_ref();
6462 if (repo_path == NULL) {
6463 cwd = getcwd(NULL, 0);
6464 if (cwd == NULL)
6465 return got_error_from_errno("getcwd");
6466 error = got_worktree_open(&worktree, cwd);
6467 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6468 goto done;
6469 if (worktree)
6470 repo_path =
6471 strdup(got_worktree_get_repo_path(worktree));
6472 else
6473 repo_path = strdup(cwd);
6474 if (repo_path == NULL) {
6475 error = got_error_from_errno("strdup");
6476 goto done;
6480 error = got_repo_open(&repo, repo_path, NULL);
6481 if (error != NULL)
6482 goto done;
6484 init_curses();
6486 error = apply_unveil(got_repo_get_path(repo), NULL);
6487 if (error)
6488 goto done;
6490 error = tog_load_refs(repo, 0);
6491 if (error)
6492 goto done;
6494 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6495 if (view == NULL) {
6496 error = got_error_from_errno("view_open");
6497 goto done;
6500 error = open_ref_view(view, repo);
6501 if (error)
6502 goto done;
6504 if (worktree) {
6505 /* Release work tree lock. */
6506 got_worktree_close(worktree);
6507 worktree = NULL;
6509 error = view_loop(view);
6510 done:
6511 free(repo_path);
6512 free(cwd);
6513 if (repo) {
6514 const struct got_error *close_err = got_repo_close(repo);
6515 if (close_err)
6516 error = close_err;
6518 tog_free_refs();
6519 return error;
6522 static void
6523 list_commands(FILE *fp)
6525 size_t i;
6527 fprintf(fp, "commands:");
6528 for (i = 0; i < nitems(tog_commands); i++) {
6529 const struct tog_cmd *cmd = &tog_commands[i];
6530 fprintf(fp, " %s", cmd->name);
6532 fputc('\n', fp);
6535 __dead static void
6536 usage(int hflag, int status)
6538 FILE *fp = (status == 0) ? stdout : stderr;
6540 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6541 getprogname());
6542 if (hflag) {
6543 fprintf(fp, "lazy usage: %s path\n", getprogname());
6544 list_commands(fp);
6546 exit(status);
6549 static char **
6550 make_argv(int argc, ...)
6552 va_list ap;
6553 char **argv;
6554 int i;
6556 va_start(ap, argc);
6558 argv = calloc(argc, sizeof(char *));
6559 if (argv == NULL)
6560 err(1, "calloc");
6561 for (i = 0; i < argc; i++) {
6562 argv[i] = strdup(va_arg(ap, char *));
6563 if (argv[i] == NULL)
6564 err(1, "strdup");
6567 va_end(ap);
6568 return argv;
6572 * Try to convert 'tog path' into a 'tog log path' command.
6573 * The user could simply have mistyped the command rather than knowingly
6574 * provided a path. So check whether argv[0] can in fact be resolved
6575 * to a path in the HEAD commit and print a special error if not.
6576 * This hack is for mpi@ <3
6578 static const struct got_error *
6579 tog_log_with_path(int argc, char *argv[])
6581 const struct got_error *error = NULL, *close_err;
6582 const struct tog_cmd *cmd = NULL;
6583 struct got_repository *repo = NULL;
6584 struct got_worktree *worktree = NULL;
6585 struct got_object_id *commit_id = NULL, *id = NULL;
6586 struct got_commit_object *commit = NULL;
6587 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6588 char *commit_id_str = NULL, **cmd_argv = NULL;
6590 cwd = getcwd(NULL, 0);
6591 if (cwd == NULL)
6592 return got_error_from_errno("getcwd");
6594 error = got_worktree_open(&worktree, cwd);
6595 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6596 goto done;
6598 if (worktree)
6599 repo_path = strdup(got_worktree_get_repo_path(worktree));
6600 else
6601 repo_path = strdup(cwd);
6602 if (repo_path == NULL) {
6603 error = got_error_from_errno("strdup");
6604 goto done;
6607 error = got_repo_open(&repo, repo_path, NULL);
6608 if (error != NULL)
6609 goto done;
6611 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6612 repo, worktree);
6613 if (error)
6614 goto done;
6616 error = tog_load_refs(repo, 0);
6617 if (error)
6618 goto done;
6619 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6620 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6621 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6622 if (error)
6623 goto done;
6625 if (worktree) {
6626 got_worktree_close(worktree);
6627 worktree = NULL;
6630 error = got_object_open_as_commit(&commit, repo, commit_id);
6631 if (error)
6632 goto done;
6634 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6635 if (error) {
6636 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6637 goto done;
6638 fprintf(stderr, "%s: '%s' is no known command or path\n",
6639 getprogname(), argv[0]);
6640 usage(1, 1);
6641 /* not reached */
6644 close_err = got_repo_close(repo);
6645 if (error == NULL)
6646 error = close_err;
6647 repo = NULL;
6649 error = got_object_id_str(&commit_id_str, commit_id);
6650 if (error)
6651 goto done;
6653 cmd = &tog_commands[0]; /* log */
6654 argc = 4;
6655 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6656 error = cmd->cmd_main(argc, cmd_argv);
6657 done:
6658 if (repo) {
6659 close_err = got_repo_close(repo);
6660 if (error == NULL)
6661 error = close_err;
6663 if (commit)
6664 got_object_commit_close(commit);
6665 if (worktree)
6666 got_worktree_close(worktree);
6667 free(id);
6668 free(commit_id_str);
6669 free(commit_id);
6670 free(cwd);
6671 free(repo_path);
6672 free(in_repo_path);
6673 if (cmd_argv) {
6674 int i;
6675 for (i = 0; i < argc; i++)
6676 free(cmd_argv[i]);
6677 free(cmd_argv);
6679 tog_free_refs();
6680 return error;
6683 int
6684 main(int argc, char *argv[])
6686 const struct got_error *error = NULL;
6687 const struct tog_cmd *cmd = NULL;
6688 int ch, hflag = 0, Vflag = 0;
6689 char **cmd_argv = NULL;
6690 static const struct option longopts[] = {
6691 { "version", no_argument, NULL, 'V' },
6692 { NULL, 0, NULL, 0}
6695 setlocale(LC_CTYPE, "");
6697 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6698 switch (ch) {
6699 case 'h':
6700 hflag = 1;
6701 break;
6702 case 'V':
6703 Vflag = 1;
6704 break;
6705 default:
6706 usage(hflag, 1);
6707 /* NOTREACHED */
6711 argc -= optind;
6712 argv += optind;
6713 optind = 1;
6714 optreset = 1;
6716 if (Vflag) {
6717 got_version_print_str();
6718 return 0;
6721 #ifndef PROFILE
6722 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6723 NULL) == -1)
6724 err(1, "pledge");
6725 #endif
6727 if (argc == 0) {
6728 if (hflag)
6729 usage(hflag, 0);
6730 /* Build an argument vector which runs a default command. */
6731 cmd = &tog_commands[0];
6732 argc = 1;
6733 cmd_argv = make_argv(argc, cmd->name);
6734 } else {
6735 size_t i;
6737 /* Did the user specify a command? */
6738 for (i = 0; i < nitems(tog_commands); i++) {
6739 if (strncmp(tog_commands[i].name, argv[0],
6740 strlen(argv[0])) == 0) {
6741 cmd = &tog_commands[i];
6742 break;
6747 if (cmd == NULL) {
6748 if (argc != 1)
6749 usage(0, 1);
6750 /* No command specified; try log with a path */
6751 error = tog_log_with_path(argc, argv);
6752 } else {
6753 if (hflag)
6754 cmd->cmd_usage();
6755 else
6756 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6759 endwin();
6760 putchar('\n');
6761 if (cmd_argv) {
6762 int i;
6763 for (i = 0; i < argc; i++)
6764 free(cmd_argv[i]);
6765 free(cmd_argv);
6768 if (error && error->code != GOT_ERR_CANCELLED)
6769 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6770 return 0;