tog: make blame view work
[got-portable.git] / tog / tog.c
blob88e42baaf5d6e7a213d94fdf05adf31d18c574f0
1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/stat.h>
18 #include <sys/ioctl.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <curses.h>
23 #include <panel.h>
24 #include <locale.h>
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <limits.h>
34 #include <wchar.h>
35 #include <time.h>
36 #include <pthread.h>
37 #include <libgen.h>
38 #include <regex.h>
39 #include <sched.h>
41 #include "got_compat.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 //#define update_panels() (0)
59 //#define doupdate() (0)
61 #ifndef MIN
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #endif
65 #ifndef MAX
66 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #endif
69 #ifndef CTRL
70 #define CTRL(x) ((x) & 0x1f)
71 #endif
73 #ifndef nitems
74 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 #endif
77 struct tog_cmd {
78 const char *name;
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
83 __dead static void usage(int, int);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_ref(void);
90 static const struct got_error* cmd_log(int, char *[]);
91 static const struct got_error* cmd_diff(int, char *[]);
92 static const struct got_error* cmd_blame(int, char *[]);
93 static const struct got_error* cmd_tree(int, char *[]);
94 static const struct got_error* cmd_ref(int, char *[]);
96 static struct tog_cmd tog_commands[] = {
97 { "log", cmd_log, usage_log },
98 { "diff", cmd_diff, usage_diff },
99 { "blame", cmd_blame, usage_blame },
100 { "tree", cmd_tree, usage_tree },
101 { "ref", cmd_ref, usage_ref },
104 enum tog_view_type {
105 TOG_VIEW_DIFF,
106 TOG_VIEW_LOG,
107 TOG_VIEW_BLAME,
108 TOG_VIEW_TREE,
109 TOG_VIEW_REF,
112 #define TOG_EOF_STRING "(END)"
114 struct commit_queue_entry {
115 TAILQ_ENTRY(commit_queue_entry) entry;
116 struct got_object_id *id;
117 struct got_commit_object *commit;
118 int idx;
120 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
121 struct commit_queue {
122 int ncommits;
123 struct commit_queue_head head;
126 struct tog_color {
127 STAILQ_ENTRY(tog_color) entry;
128 regex_t regex;
129 short colorpair;
131 STAILQ_HEAD(tog_colors, tog_color);
133 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
134 static struct got_reflist_object_id_map *tog_refs_idmap;
136 static const struct got_error *
137 tog_load_refs(struct got_repository *repo)
139 const struct got_error *err;
141 err = got_ref_list(&tog_refs, repo, NULL, got_ref_cmp_by_name, NULL);
142 if (err)
143 return err;
145 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
146 repo);
149 static void
150 tog_free_refs(void)
152 if (tog_refs_idmap) {
153 got_reflist_object_id_map_free(tog_refs_idmap);
154 tog_refs_idmap = NULL;
156 got_ref_list_free(&tog_refs);
159 static const struct got_error *
160 add_color(struct tog_colors *colors, const char *pattern,
161 int idx, short color)
163 const struct got_error *err = NULL;
164 struct tog_color *tc;
165 int regerr = 0;
167 if (idx < 1 || idx > COLOR_PAIRS - 1)
168 return NULL;
170 init_pair(idx, color, -1);
172 tc = calloc(1, sizeof(*tc));
173 if (tc == NULL)
174 return got_error_from_errno("calloc");
175 regerr = regcomp(&tc->regex, pattern,
176 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
177 if (regerr) {
178 static char regerr_msg[512];
179 static char err_msg[512];
180 regerror(regerr, &tc->regex, regerr_msg,
181 sizeof(regerr_msg));
182 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
183 regerr_msg);
184 err = got_error_msg(GOT_ERR_REGEX, err_msg);
185 free(tc);
186 return err;
188 tc->colorpair = idx;
189 STAILQ_INSERT_HEAD(colors, tc, entry);
190 return NULL;
193 static void
194 free_colors(struct tog_colors *colors)
196 struct tog_color *tc;
198 while (!STAILQ_EMPTY(colors)) {
199 tc = STAILQ_FIRST(colors);
200 STAILQ_REMOVE_HEAD(colors, entry);
201 regfree(&tc->regex);
202 free(tc);
206 struct tog_color *
207 get_color(struct tog_colors *colors, int colorpair)
209 struct tog_color *tc = NULL;
211 STAILQ_FOREACH(tc, colors, entry) {
212 if (tc->colorpair == colorpair)
213 return tc;
216 return NULL;
219 static int
220 default_color_value(const char *envvar)
222 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
223 return COLOR_MAGENTA;
224 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
225 return COLOR_CYAN;
226 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
227 return COLOR_YELLOW;
228 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
229 return COLOR_GREEN;
230 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
231 return COLOR_MAGENTA;
232 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
233 return COLOR_MAGENTA;
234 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
235 return COLOR_CYAN;
236 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
237 return COLOR_GREEN;
238 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
239 return COLOR_GREEN;
240 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
241 return COLOR_CYAN;
242 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
243 return COLOR_YELLOW;
244 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
245 return COLOR_GREEN;
246 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
247 return COLOR_MAGENTA;
248 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
249 return COLOR_YELLOW;
251 return -1;
254 static int
255 get_color_value(const char *envvar)
257 const char *val = getenv(envvar);
259 if (val == NULL)
260 return default_color_value(envvar);
262 if (strcasecmp(val, "black") == 0)
263 return COLOR_BLACK;
264 if (strcasecmp(val, "red") == 0)
265 return COLOR_RED;
266 if (strcasecmp(val, "green") == 0)
267 return COLOR_GREEN;
268 if (strcasecmp(val, "yellow") == 0)
269 return COLOR_YELLOW;
270 if (strcasecmp(val, "blue") == 0)
271 return COLOR_BLUE;
272 if (strcasecmp(val, "magenta") == 0)
273 return COLOR_MAGENTA;
274 if (strcasecmp(val, "cyan") == 0)
275 return COLOR_CYAN;
276 if (strcasecmp(val, "white") == 0)
277 return COLOR_WHITE;
278 if (strcasecmp(val, "default") == 0)
279 return -1;
281 return default_color_value(envvar);
285 struct tog_diff_view_state {
286 struct got_object_id *id1, *id2;
287 const char *label1, *label2;
288 FILE *f;
289 int first_displayed_line;
290 int last_displayed_line;
291 int eof;
292 int diff_context;
293 int ignore_whitespace;
294 int force_text_diff;
295 struct got_repository *repo;
296 struct tog_colors colors;
297 size_t nlines;
298 off_t *line_offsets;
299 int matched_line;
300 int selected_line;
302 /* passed from log view; may be NULL */
303 struct tog_view *log_view;
306 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
308 struct tog_log_thread_args {
309 pthread_cond_t need_commits;
310 pthread_cond_t commit_loaded;
311 int commits_needed;
312 int load_all;
313 struct got_commit_graph *graph;
314 struct commit_queue *commits;
315 const char *in_repo_path;
316 struct got_object_id *start_id;
317 struct got_repository *repo;
318 int log_complete;
319 sig_atomic_t *quit;
320 struct commit_queue_entry **first_displayed_entry;
321 struct commit_queue_entry **selected_entry;
322 int *searching;
323 int *search_next_done;
324 regex_t *regex;
327 struct tog_log_view_state {
328 struct commit_queue commits;
329 struct commit_queue_entry *first_displayed_entry;
330 struct commit_queue_entry *last_displayed_entry;
331 struct commit_queue_entry *selected_entry;
332 int selected;
333 char *in_repo_path;
334 char *head_ref_name;
335 int log_branches;
336 struct got_repository *repo;
337 struct got_object_id *start_id;
338 sig_atomic_t quit;
339 pthread_t thread;
340 struct tog_log_thread_args thread_args;
341 struct commit_queue_entry *matched_entry;
342 struct commit_queue_entry *search_entry;
343 struct tog_colors colors;
346 #define TOG_COLOR_DIFF_MINUS 1
347 #define TOG_COLOR_DIFF_PLUS 2
348 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
349 #define TOG_COLOR_DIFF_META 4
350 #define TOG_COLOR_TREE_SUBMODULE 5
351 #define TOG_COLOR_TREE_SYMLINK 6
352 #define TOG_COLOR_TREE_DIRECTORY 7
353 #define TOG_COLOR_TREE_EXECUTABLE 8
354 #define TOG_COLOR_COMMIT 9
355 #define TOG_COLOR_AUTHOR 10
356 #define TOG_COLOR_DATE 11
357 #define TOG_COLOR_REFS_HEADS 12
358 #define TOG_COLOR_REFS_TAGS 13
359 #define TOG_COLOR_REFS_REMOTES 14
361 struct tog_blame_cb_args {
362 struct tog_blame_line *lines; /* one per line */
363 int nlines;
365 struct tog_view *view;
366 struct got_object_id *commit_id;
367 int *quit;
370 struct tog_blame_thread_args {
371 const char *path;
372 struct got_repository *repo;
373 struct tog_blame_cb_args *cb_args;
374 int *complete;
375 got_cancel_cb cancel_cb;
376 void *cancel_arg;
379 struct tog_blame {
380 FILE *f;
381 off_t filesize;
382 struct tog_blame_line *lines;
383 int nlines;
384 off_t *line_offsets;
385 pthread_t thread;
386 struct tog_blame_thread_args thread_args;
387 struct tog_blame_cb_args cb_args;
388 const char *path;
391 struct tog_blame_view_state {
392 int first_displayed_line;
393 int last_displayed_line;
394 int selected_line;
395 int blame_complete;
396 int eof;
397 int done;
398 struct got_object_id_queue blamed_commits;
399 struct got_object_qid *blamed_commit;
400 char *path;
401 struct got_repository *repo;
402 struct got_object_id *commit_id;
403 struct tog_blame blame;
404 int matched_line;
405 struct tog_colors colors;
408 struct tog_parent_tree {
409 TAILQ_ENTRY(tog_parent_tree) entry;
410 struct got_tree_object *tree;
411 struct got_tree_entry *first_displayed_entry;
412 struct got_tree_entry *selected_entry;
413 int selected;
416 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
418 struct tog_tree_view_state {
419 char *tree_label;
420 struct got_object_id *commit_id;/* commit which this tree belongs to */
421 struct got_tree_object *root; /* the commit's root tree entry */
422 struct got_tree_object *tree; /* currently displayed (sub-)tree */
423 struct got_tree_entry *first_displayed_entry;
424 struct got_tree_entry *last_displayed_entry;
425 struct got_tree_entry *selected_entry;
426 int ndisplayed, selected, show_ids;
427 struct tog_parent_trees parents; /* parent trees of current sub-tree */
428 char *head_ref_name;
429 struct got_repository *repo;
430 struct got_tree_entry *matched_entry;
431 struct tog_colors colors;
434 struct tog_reflist_entry {
435 TAILQ_ENTRY(tog_reflist_entry) entry;
436 struct got_reference *ref;
437 int idx;
440 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
442 struct tog_ref_view_state {
443 struct tog_reflist_head refs;
444 struct tog_reflist_entry *first_displayed_entry;
445 struct tog_reflist_entry *last_displayed_entry;
446 struct tog_reflist_entry *selected_entry;
447 int nrefs, ndisplayed, selected, show_ids;
448 struct got_repository *repo;
449 struct tog_reflist_entry *matched_entry;
450 struct tog_colors colors;
454 * We implement two types of views: parent views and child views.
456 * The 'Tab' key switches focus between a parent view and its child view.
457 * Child views are shown side-by-side to their parent view, provided
458 * there is enough screen estate.
460 * When a new view is opened from within a parent view, this new view
461 * becomes a child view of the parent view, replacing any existing child.
463 * When a new view is opened from within a child view, this new view
464 * becomes a parent view which will obscure the views below until the
465 * user quits the new parent view by typing 'q'.
467 * This list of views contains parent views only.
468 * Child views are only pointed to by their parent view.
470 TAILQ_HEAD(tog_view_list_head, tog_view);
472 struct tog_view {
473 TAILQ_ENTRY(tog_view) entry;
474 WINDOW *window;
475 PANEL *panel;
476 int nlines, ncols, begin_y, begin_x;
477 int lines, cols; /* copies of LINES and COLS */
478 int focussed; /* Only set on one parent or child view at a time. */
479 int dying;
480 struct tog_view *parent;
481 struct tog_view *child;
484 * This flag is initially set on parent views when a new child view
485 * is created. It gets toggled when the 'Tab' key switches focus
486 * between parent and child.
487 * The flag indicates whether focus should be passed on to our child
488 * view if this parent view gets picked for focus after another parent
489 * view was closed. This prevents child views from losing focus in such
490 * situations.
492 int focus_child;
494 /* type-specific state */
495 enum tog_view_type type;
496 union {
497 struct tog_diff_view_state diff;
498 struct tog_log_view_state log;
499 struct tog_blame_view_state blame;
500 struct tog_tree_view_state tree;
501 struct tog_ref_view_state ref;
502 } state;
504 const struct got_error *(*show)(struct tog_view *);
505 const struct got_error *(*input)(struct tog_view **,
506 struct tog_view *, int);
507 const struct got_error *(*close)(struct tog_view *);
509 const struct got_error *(*search_start)(struct tog_view *);
510 const struct got_error *(*search_next)(struct tog_view *);
511 int search_started;
512 int searching;
513 #define TOG_SEARCH_FORWARD 1
514 #define TOG_SEARCH_BACKWARD 2
515 int search_next_done;
516 #define TOG_SEARCH_HAVE_MORE 1
517 #define TOG_SEARCH_NO_MORE 2
518 #define TOG_SEARCH_HAVE_NONE 3
519 regex_t regex;
520 regmatch_t regmatch;
523 static const struct got_error *open_diff_view(struct tog_view *,
524 struct got_object_id *, struct got_object_id *,
525 const char *, const char *, int, int, int, struct tog_view *,
526 struct got_repository *);
527 static const struct got_error *show_diff_view(struct tog_view *);
528 static const struct got_error *input_diff_view(struct tog_view **,
529 struct tog_view *, int);
530 static const struct got_error* close_diff_view(struct tog_view *);
531 static const struct got_error *search_start_diff_view(struct tog_view *);
532 static const struct got_error *search_next_diff_view(struct tog_view *);
534 static const struct got_error *open_log_view(struct tog_view *,
535 struct got_object_id *, struct got_repository *,
536 const char *, const char *, int);
537 static const struct got_error * show_log_view(struct tog_view *);
538 static const struct got_error *input_log_view(struct tog_view **,
539 struct tog_view *, int);
540 static const struct got_error *close_log_view(struct tog_view *);
541 static const struct got_error *search_start_log_view(struct tog_view *);
542 static const struct got_error *search_next_log_view(struct tog_view *);
544 static const struct got_error *open_blame_view(struct tog_view *, char *,
545 struct got_object_id *, struct got_repository *);
546 static const struct got_error *show_blame_view(struct tog_view *);
547 static const struct got_error *input_blame_view(struct tog_view **,
548 struct tog_view *, int);
549 static const struct got_error *close_blame_view(struct tog_view *);
550 static const struct got_error *search_start_blame_view(struct tog_view *);
551 static const struct got_error *search_next_blame_view(struct tog_view *);
553 static const struct got_error *open_tree_view(struct tog_view *,
554 struct got_object_id *, const char *, struct got_repository *);
555 static const struct got_error *show_tree_view(struct tog_view *);
556 static const struct got_error *input_tree_view(struct tog_view **,
557 struct tog_view *, int);
558 static const struct got_error *close_tree_view(struct tog_view *);
559 static const struct got_error *search_start_tree_view(struct tog_view *);
560 static const struct got_error *search_next_tree_view(struct tog_view *);
562 static const struct got_error *open_ref_view(struct tog_view *,
563 struct got_repository *);
564 static const struct got_error *show_ref_view(struct tog_view *);
565 static const struct got_error *input_ref_view(struct tog_view **,
566 struct tog_view *, int);
567 static const struct got_error *close_ref_view(struct tog_view *);
568 static const struct got_error *search_start_ref_view(struct tog_view *);
569 static const struct got_error *search_next_ref_view(struct tog_view *);
571 static volatile sig_atomic_t tog_sigwinch_received;
572 static volatile sig_atomic_t tog_sigpipe_received;
573 static volatile sig_atomic_t tog_sigcont_received;
575 static void
576 tog_sigwinch(int signo)
578 tog_sigwinch_received = 1;
581 static void
582 tog_sigpipe(int signo)
584 tog_sigpipe_received = 1;
587 static void
588 tog_sigcont(int signo)
590 tog_sigcont_received = 1;
593 static const struct got_error *
594 view_close(struct tog_view *view)
596 const struct got_error *err = NULL;
598 if (view->child) {
599 view_close(view->child);
600 view->child = NULL;
602 if (view->close)
603 err = view->close(view);
604 if (view->panel)
605 del_panel(view->panel);
606 if (view->window)
607 delwin(view->window);
608 free(view);
609 return err;
612 static struct tog_view *
613 view_open(int nlines, int ncols, int begin_y, int begin_x,
614 enum tog_view_type type)
616 struct tog_view *view = calloc(1, sizeof(*view));
618 if (view == NULL)
619 return NULL;
621 view->type = type;
622 view->lines = LINES;
623 view->cols = COLS;
624 view->nlines = nlines ? nlines : LINES - begin_y;
625 view->ncols = ncols ? ncols : COLS - begin_x;
626 view->begin_y = begin_y;
627 view->begin_x = begin_x;
628 view->window = newwin(nlines, ncols, begin_y, begin_x);
629 if (view->window == NULL) {
630 view_close(view);
631 return NULL;
633 view->panel = new_panel(view->window);
634 if (view->panel == NULL ||
635 set_panel_userptr(view->panel, view) != OK) {
636 view_close(view);
637 return NULL;
640 keypad(view->window, TRUE);
641 return view;
644 static int
645 view_split_begin_x(int begin_x)
647 if (begin_x > 0 || COLS < 120)
648 return 0;
649 return (COLS - MAX(COLS / 2, 80));
652 static const struct got_error *view_resize(struct tog_view *);
654 static const struct got_error *
655 view_splitscreen(struct tog_view *view)
657 const struct got_error *err = NULL;
659 view->begin_y = 0;
660 view->begin_x = view_split_begin_x(0);
661 view->nlines = LINES;
662 view->ncols = COLS - view->begin_x;
663 view->lines = LINES;
664 view->cols = COLS;
665 err = view_resize(view);
666 if (err)
667 return err;
669 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
670 return got_error_from_errno("mvwin");
672 return NULL;
675 static const struct got_error *
676 view_fullscreen(struct tog_view *view)
678 const struct got_error *err = NULL;
680 view->begin_x = 0;
681 view->begin_y = 0;
682 view->nlines = LINES;
683 view->ncols = COLS;
684 view->lines = LINES;
685 view->cols = COLS;
686 err = view_resize(view);
687 if (err)
688 return err;
690 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
691 return got_error_from_errno("mvwin");
693 return NULL;
696 static int
697 view_is_parent_view(struct tog_view *view)
699 return view->parent == NULL;
702 static const struct got_error *
703 view_resize(struct tog_view *view)
705 int nlines, ncols;
707 if (view->lines > LINES)
708 nlines = view->nlines - (view->lines - LINES);
709 else
710 nlines = view->nlines + (LINES - view->lines);
712 if (view->cols > COLS)
713 ncols = view->ncols - (view->cols - COLS);
714 else
715 ncols = view->ncols + (COLS - view->cols);
717 if (wresize(view->window, nlines, ncols) == ERR)
718 return got_error_from_errno("wresize");
719 if (replace_panel(view->panel, view->window) == ERR)
720 return got_error_from_errno("replace_panel");
721 wclear(view->window);
723 view->nlines = nlines;
724 view->ncols = ncols;
725 view->lines = LINES;
726 view->cols = COLS;
728 if (view->child) {
729 view->child->begin_x = view_split_begin_x(view->begin_x);
730 if (view->child->begin_x == 0) {
731 view_fullscreen(view->child);
732 if (view->child->focussed)
733 show_panel(view->child->panel);
734 else
735 show_panel(view->panel);
736 } else {
737 view_splitscreen(view->child);
738 show_panel(view->child->panel);
742 return NULL;
745 static const struct got_error *
746 view_close_child(struct tog_view *view)
748 const struct got_error *err = NULL;
750 if (view->child == NULL)
751 return NULL;
753 err = view_close(view->child);
754 view->child = NULL;
755 return err;
758 static void
759 view_set_child(struct tog_view *view, struct tog_view *child)
761 view->child = child;
762 child->parent = view;
765 static int
766 view_is_splitscreen(struct tog_view *view)
768 return view->begin_x > 0;
771 static void
772 tog_resizeterm(void)
774 int cols, lines;
775 struct winsize size;
777 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
778 cols = 80; /* Default */
779 lines = 24;
780 } else {
781 cols = size.ws_col;
782 lines = size.ws_row;
784 resize_term(lines, cols);
787 static const struct got_error *
788 view_search_start(struct tog_view *view)
790 const struct got_error *err = NULL;
791 char pattern[1024];
792 int ret;
794 if (view->search_started) {
795 regfree(&view->regex);
796 view->searching = 0;
797 memset(&view->regmatch, 0, sizeof(view->regmatch));
799 view->search_started = 0;
801 if (view->nlines < 1)
802 return NULL;
804 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
805 wclrtoeol(view->window);
807 nocbreak();
808 echo();
809 ret = wgetnstr(view->window, pattern, sizeof(pattern));
810 cbreak();
811 noecho();
812 if (ret == ERR)
813 return NULL;
815 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
816 err = view->search_start(view);
817 if (err) {
818 regfree(&view->regex);
819 return err;
821 view->search_started = 1;
822 view->searching = TOG_SEARCH_FORWARD;
823 view->search_next_done = 0;
824 view->search_next(view);
827 return NULL;
830 static const struct got_error *
831 view_input(struct tog_view **new, int *done, struct tog_view *view,
832 struct tog_view_list_head *views)
834 const struct got_error *err = NULL;
835 struct tog_view *v;
836 int ch, errcode;
838 *new = NULL;
840 /* Clear "no matches" indicator. */
841 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
842 view->search_next_done == TOG_SEARCH_HAVE_NONE)
843 view->search_next_done = TOG_SEARCH_HAVE_MORE;
845 if (view->searching && !view->search_next_done) {
846 errcode = pthread_mutex_unlock(&tog_mutex);
847 if (errcode)
848 return got_error_set_errno(errcode,
849 "pthread_mutex_unlock");
850 sched_yield();
851 errcode = pthread_mutex_lock(&tog_mutex);
852 if (errcode)
853 return got_error_set_errno(errcode,
854 "pthread_mutex_lock");
855 view->search_next(view);
856 return NULL;
859 nodelay(stdscr, FALSE);
860 /* Allow threads to make progress while we are waiting for input. */
861 errcode = pthread_mutex_unlock(&tog_mutex);
862 if (errcode)
863 return got_error_set_errno(errcode, "pthread_mutex_unlock");
864 ch = wgetch(view->window);
865 errcode = pthread_mutex_lock(&tog_mutex);
866 if (errcode)
867 return got_error_set_errno(errcode, "pthread_mutex_lock");
868 nodelay(stdscr, TRUE);
870 if (tog_sigwinch_received || tog_sigcont_received) {
871 tog_resizeterm();
872 tog_sigwinch_received = 0;
873 tog_sigcont_received = 0;
874 TAILQ_FOREACH(v, views, entry) {
875 err = view_resize(v);
876 if (err)
877 return err;
878 err = v->input(new, v, KEY_RESIZE);
879 if (err)
880 return err;
881 if (v->child) {
882 err = view_resize(v->child);
883 if (err)
884 return err;
885 err = v->child->input(new, v->child,
886 KEY_RESIZE);
887 if (err)
888 return err;
893 switch (ch) {
894 case '\t':
895 if (view->child) {
896 view->focussed = 0;
897 view->child->focussed = 1;
898 view->focus_child = 1;
899 } else if (view->parent) {
900 view->focussed = 0;
901 view->parent->focussed = 1;
902 view->parent->focus_child = 0;
904 break;
905 case 'q':
906 err = view->input(new, view, ch);
907 view->dying = 1;
908 break;
909 case 'Q':
910 *done = 1;
911 break;
912 case 'f':
913 if (view_is_parent_view(view)) {
914 if (view->child == NULL)
915 break;
916 if (view_is_splitscreen(view->child)) {
917 view->focussed = 0;
918 view->child->focussed = 1;
919 err = view_fullscreen(view->child);
920 } else
921 err = view_splitscreen(view->child);
922 if (err)
923 break;
924 err = view->child->input(new, view->child,
925 KEY_RESIZE);
926 } else {
927 if (view_is_splitscreen(view)) {
928 view->parent->focussed = 0;
929 view->focussed = 1;
930 err = view_fullscreen(view);
931 } else {
932 err = view_splitscreen(view);
934 if (err)
935 break;
936 err = view->input(new, view, KEY_RESIZE);
938 break;
939 case KEY_RESIZE:
940 break;
941 case '/':
942 if (view->search_start)
943 view_search_start(view);
944 else
945 err = view->input(new, view, ch);
946 break;
947 case 'N':
948 case 'n':
949 if (view->search_started && view->search_next) {
950 view->searching = (ch == 'n' ?
951 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
952 view->search_next_done = 0;
953 view->search_next(view);
954 } else
955 err = view->input(new, view, ch);
956 break;
957 default:
958 err = view->input(new, view, ch);
959 break;
962 return err;
965 void
966 view_vborder(struct tog_view *view)
968 PANEL *panel;
969 const struct tog_view *view_above;
971 if (view->parent)
972 return view_vborder(view->parent);
974 panel = panel_above(view->panel);
975 if (panel == NULL)
976 return;
978 view_above = panel_userptr(panel);
979 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
980 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
984 view_needs_focus_indication(struct tog_view *view)
986 if (view_is_parent_view(view)) {
987 if (view->child == NULL || view->child->focussed)
988 return 0;
989 if (!view_is_splitscreen(view->child))
990 return 0;
991 } else if (!view_is_splitscreen(view))
992 return 0;
994 return view->focussed;
997 static const struct got_error *
998 view_loop(struct tog_view *view)
1000 const struct got_error *err = NULL;
1001 struct tog_view_list_head views;
1002 struct tog_view *new_view;
1003 int fast_refresh = 10;
1004 int done = 0, errcode;
1006 errcode = pthread_mutex_lock(&tog_mutex);
1007 if (errcode)
1008 return got_error_set_errno(errcode, "pthread_mutex_lock");
1010 TAILQ_INIT(&views);
1011 TAILQ_INSERT_HEAD(&views, view, entry);
1013 view->focussed = 1;
1014 err = view->show(view);
1015 if (err)
1016 return err;
1017 update_panels();
1018 doupdate();
1019 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1020 /* Refresh fast during initialization, then become slower. */
1021 if (fast_refresh && fast_refresh-- == 0)
1022 halfdelay(10); /* switch to once per second */
1024 err = view_input(&new_view, &done, view, &views);
1025 if (err)
1026 break;
1027 if (view->dying) {
1028 struct tog_view *v, *prev = NULL;
1030 if (view_is_parent_view(view))
1031 prev = TAILQ_PREV(view, tog_view_list_head,
1032 entry);
1033 else if (view->parent)
1034 prev = view->parent;
1036 if (view->parent) {
1037 view->parent->child = NULL;
1038 view->parent->focus_child = 0;
1039 } else
1040 TAILQ_REMOVE(&views, view, entry);
1042 err = view_close(view);
1043 if (err)
1044 goto done;
1046 view = NULL;
1047 TAILQ_FOREACH(v, &views, entry) {
1048 if (v->focussed)
1049 break;
1051 if (view == NULL && new_view == NULL) {
1052 /* No view has focus. Try to pick one. */
1053 if (prev)
1054 view = prev;
1055 else if (!TAILQ_EMPTY(&views)) {
1056 view = TAILQ_LAST(&views,
1057 tog_view_list_head);
1059 if (view) {
1060 if (view->focus_child) {
1061 view->child->focussed = 1;
1062 view = view->child;
1063 } else
1064 view->focussed = 1;
1068 if (new_view) {
1069 struct tog_view *v, *t;
1070 /* Only allow one parent view per type. */
1071 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1072 if (v->type != new_view->type)
1073 continue;
1074 TAILQ_REMOVE(&views, v, entry);
1075 err = view_close(v);
1076 if (err)
1077 goto done;
1078 break;
1080 TAILQ_INSERT_TAIL(&views, new_view, entry);
1081 view = new_view;
1083 if (view) {
1084 if (view_is_parent_view(view)) {
1085 if (view->child && view->child->focussed)
1086 view = view->child;
1087 } else {
1088 if (view->parent && view->parent->focussed)
1089 view = view->parent;
1091 show_panel(view->panel);
1092 if (view->child && view_is_splitscreen(view->child))
1093 show_panel(view->child->panel);
1094 if (view->parent && view_is_splitscreen(view)) {
1095 err = view->parent->show(view->parent);
1096 if (err)
1097 goto done;
1099 err = view->show(view);
1100 if (err)
1101 goto done;
1102 if (view->child) {
1103 err = view->child->show(view->child);
1104 if (err)
1105 goto done;
1107 update_panels();
1108 doupdate();
1111 done:
1112 while (!TAILQ_EMPTY(&views)) {
1113 view = TAILQ_FIRST(&views);
1114 TAILQ_REMOVE(&views, view, entry);
1115 view_close(view);
1118 errcode = pthread_mutex_unlock(&tog_mutex);
1119 if (errcode && err == NULL)
1120 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1122 return err;
1125 __dead static void
1126 usage_log(void)
1128 endwin();
1129 fprintf(stderr,
1130 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1131 getprogname());
1132 exit(1);
1135 /* Create newly allocated wide-character string equivalent to a byte string. */
1136 static const struct got_error *
1137 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1139 char *vis = NULL;
1140 const struct got_error *err = NULL;
1142 *ws = NULL;
1143 *wlen = mbstowcs(NULL, s, 0);
1144 if (*wlen == (size_t)-1) {
1145 int vislen;
1146 if (errno != EILSEQ)
1147 return got_error_from_errno("mbstowcs");
1149 /* byte string invalid in current encoding; try to "fix" it */
1150 err = got_mbsavis(&vis, &vislen, s);
1151 if (err)
1152 return err;
1153 *wlen = mbstowcs(NULL, vis, 0);
1154 if (*wlen == (size_t)-1) {
1155 err = got_error_from_errno("mbstowcs"); /* give up */
1156 goto done;
1160 *ws = calloc(*wlen + 1, sizeof(**ws));
1161 if (*ws == NULL) {
1162 err = got_error_from_errno("calloc");
1163 goto done;
1166 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1167 err = got_error_from_errno("mbstowcs");
1168 done:
1169 free(vis);
1170 if (err) {
1171 free(*ws);
1172 *ws = NULL;
1173 *wlen = 0;
1175 return err;
1178 /* Format a line for display, ensuring that it won't overflow a width limit. */
1179 static const struct got_error *
1180 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1181 int col_tab_align)
1183 const struct got_error *err = NULL;
1184 int cols = 0;
1185 wchar_t *wline = NULL;
1186 size_t wlen;
1187 int i;
1189 *wlinep = NULL;
1190 *widthp = 0;
1192 err = mbs2ws(&wline, &wlen, line);
1193 if (err)
1194 return err;
1196 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1197 wline[wlen - 1] = L'\0';
1198 wlen--;
1200 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1201 wline[wlen - 1] = L'\0';
1202 wlen--;
1205 i = 0;
1206 while (i < wlen) {
1207 int width = wcwidth(wline[i]);
1209 if (width == 0) {
1210 i++;
1211 continue;
1214 if (width == 1 || width == 2) {
1215 if (cols + width > wlimit)
1216 break;
1217 cols += width;
1218 i++;
1219 } else if (width == -1) {
1220 if (wline[i] == L'\t') {
1221 width = TABSIZE -
1222 ((cols + col_tab_align) % TABSIZE);
1223 } else {
1224 width = 1;
1225 wline[i] = L'.';
1227 if (cols + width > wlimit)
1228 break;
1229 cols += width;
1230 i++;
1231 } else {
1232 err = got_error_from_errno("wcwidth");
1233 goto done;
1236 wline[i] = L'\0';
1237 if (widthp)
1238 *widthp = cols;
1239 done:
1240 if (err)
1241 free(wline);
1242 else
1243 *wlinep = wline;
1244 return err;
1247 static const struct got_error*
1248 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1249 struct got_object_id *id, struct got_repository *repo)
1251 static const struct got_error *err = NULL;
1252 struct got_reflist_entry *re;
1253 char *s;
1254 const char *name;
1256 *refs_str = NULL;
1258 TAILQ_FOREACH(re, refs, entry) {
1259 struct got_tag_object *tag = NULL;
1260 struct got_object_id *ref_id;
1261 int cmp;
1263 name = got_ref_get_name(re->ref);
1264 if (strcmp(name, GOT_REF_HEAD) == 0)
1265 continue;
1266 if (strncmp(name, "refs/", 5) == 0)
1267 name += 5;
1268 if (strncmp(name, "got/", 4) == 0)
1269 continue;
1270 if (strncmp(name, "heads/", 6) == 0)
1271 name += 6;
1272 if (strncmp(name, "remotes/", 8) == 0) {
1273 name += 8;
1274 s = strstr(name, "/" GOT_REF_HEAD);
1275 if (s != NULL && s[strlen(s)] == '\0')
1276 continue;
1278 err = got_ref_resolve(&ref_id, repo, re->ref);
1279 if (err)
1280 break;
1281 if (strncmp(name, "tags/", 5) == 0) {
1282 err = got_object_open_as_tag(&tag, repo, ref_id);
1283 if (err) {
1284 if (err->code != GOT_ERR_OBJ_TYPE) {
1285 free(ref_id);
1286 break;
1288 /* Ref points at something other than a tag. */
1289 err = NULL;
1290 tag = NULL;
1293 cmp = got_object_id_cmp(tag ?
1294 got_object_tag_get_object_id(tag) : ref_id, id);
1295 free(ref_id);
1296 if (tag)
1297 got_object_tag_close(tag);
1298 if (cmp != 0)
1299 continue;
1300 s = *refs_str;
1301 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1302 s ? ", " : "", name) == -1) {
1303 err = got_error_from_errno("asprintf");
1304 free(s);
1305 *refs_str = NULL;
1306 break;
1308 free(s);
1311 return err;
1314 static const struct got_error *
1315 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1316 int col_tab_align)
1318 char *smallerthan;
1320 smallerthan = strchr(author, '<');
1321 if (smallerthan && smallerthan[1] != '\0')
1322 author = smallerthan + 1;
1323 author[strcspn(author, "@>")] = '\0';
1324 return format_line(wauthor, author_width, author, limit, col_tab_align);
1327 static const struct got_error *
1328 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1329 struct got_object_id *id, const size_t date_display_cols,
1330 int author_display_cols)
1332 struct tog_log_view_state *s = &view->state.log;
1333 const struct got_error *err = NULL;
1334 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1335 char *logmsg0 = NULL, *logmsg = NULL;
1336 char *author = NULL;
1337 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1338 int author_width, logmsg_width;
1339 char *newline, *line = NULL;
1340 int col, limit;
1341 const int avail = view->ncols;
1342 struct tm tm;
1343 time_t committer_time;
1344 struct tog_color *tc;
1346 committer_time = got_object_commit_get_committer_time(commit);
1347 if (gmtime_r(&committer_time, &tm) == NULL)
1348 return got_error_from_errno("gmtime_r");
1349 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1350 return got_error(GOT_ERR_NO_SPACE);
1352 if (avail <= date_display_cols)
1353 limit = MIN(sizeof(datebuf) - 1, avail);
1354 else
1355 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1356 tc = get_color(&s->colors, TOG_COLOR_DATE);
1357 if (tc)
1358 wattr_on(view->window,
1359 COLOR_PAIR(tc->colorpair), NULL);
1360 waddnstr(view->window, datebuf, limit);
1361 if (tc)
1362 wattr_off(view->window,
1363 COLOR_PAIR(tc->colorpair), NULL);
1364 col = limit;
1365 if (col > avail)
1366 goto done;
1368 if (avail >= 120) {
1369 char *id_str;
1370 err = got_object_id_str(&id_str, id);
1371 if (err)
1372 goto done;
1373 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1374 if (tc)
1375 wattr_on(view->window,
1376 COLOR_PAIR(tc->colorpair), NULL);
1377 wprintw(view->window, "%.8s ", id_str);
1378 if (tc)
1379 wattr_off(view->window,
1380 COLOR_PAIR(tc->colorpair), NULL);
1381 free(id_str);
1382 col += 9;
1383 if (col > avail)
1384 goto done;
1387 author = strdup(got_object_commit_get_author(commit));
1388 if (author == NULL) {
1389 err = got_error_from_errno("strdup");
1390 goto done;
1392 err = format_author(&wauthor, &author_width, author, avail - col, col);
1393 if (err)
1394 goto done;
1395 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1396 if (tc)
1397 wattr_on(view->window,
1398 COLOR_PAIR(tc->colorpair), NULL);
1399 waddwstr(view->window, wauthor);
1400 if (tc)
1401 wattr_off(view->window,
1402 COLOR_PAIR(tc->colorpair), NULL);
1403 col += author_width;
1404 while (col < avail && author_width < author_display_cols + 2) {
1405 waddch(view->window, ' ');
1406 col++;
1407 author_width++;
1409 if (col > avail)
1410 goto done;
1412 err = got_object_commit_get_logmsg(&logmsg0, commit);
1413 if (err)
1414 goto done;
1415 logmsg = logmsg0;
1416 while (*logmsg == '\n')
1417 logmsg++;
1418 newline = strchr(logmsg, '\n');
1419 if (newline)
1420 *newline = '\0';
1421 limit = avail - col;
1422 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1423 if (err)
1424 goto done;
1425 waddwstr(view->window, wlogmsg);
1426 col += logmsg_width;
1427 while (col < avail) {
1428 waddch(view->window, ' ');
1429 col++;
1431 done:
1432 free(logmsg0);
1433 free(wlogmsg);
1434 free(author);
1435 free(wauthor);
1436 free(line);
1437 return err;
1440 static struct commit_queue_entry *
1441 alloc_commit_queue_entry(struct got_commit_object *commit,
1442 struct got_object_id *id)
1444 struct commit_queue_entry *entry;
1446 entry = calloc(1, sizeof(*entry));
1447 if (entry == NULL)
1448 return NULL;
1450 entry->id = id;
1451 entry->commit = commit;
1452 return entry;
1455 static void
1456 pop_commit(struct commit_queue *commits)
1458 struct commit_queue_entry *entry;
1460 entry = TAILQ_FIRST(&commits->head);
1461 TAILQ_REMOVE(&commits->head, entry, entry);
1462 got_object_commit_close(entry->commit);
1463 commits->ncommits--;
1464 /* Don't free entry->id! It is owned by the commit graph. */
1465 free(entry);
1468 static void
1469 free_commits(struct commit_queue *commits)
1471 while (!TAILQ_EMPTY(&commits->head))
1472 pop_commit(commits);
1475 static const struct got_error *
1476 match_commit(int *have_match, struct got_object_id *id,
1477 struct got_commit_object *commit, regex_t *regex)
1479 const struct got_error *err = NULL;
1480 regmatch_t regmatch;
1481 char *id_str = NULL, *logmsg = NULL;
1483 *have_match = 0;
1485 err = got_object_id_str(&id_str, id);
1486 if (err)
1487 return err;
1489 err = got_object_commit_get_logmsg(&logmsg, commit);
1490 if (err)
1491 goto done;
1493 if (regexec(regex, got_object_commit_get_author(commit), 1,
1494 &regmatch, 0) == 0 ||
1495 regexec(regex, got_object_commit_get_committer(commit), 1,
1496 &regmatch, 0) == 0 ||
1497 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1498 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1499 *have_match = 1;
1500 done:
1501 free(id_str);
1502 free(logmsg);
1503 return err;
1506 static const struct got_error *
1507 queue_commits(struct tog_log_thread_args *a)
1509 const struct got_error *err = NULL;
1512 * We keep all commits open throughout the lifetime of the log
1513 * view in order to avoid having to re-fetch commits from disk
1514 * while updating the display.
1516 do {
1517 struct got_object_id *id;
1518 struct got_commit_object *commit;
1519 struct commit_queue_entry *entry;
1520 int errcode;
1522 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1523 NULL, NULL);
1524 if (err || id == NULL)
1525 break;
1527 err = got_object_open_as_commit(&commit, a->repo, id);
1528 if (err)
1529 break;
1530 entry = alloc_commit_queue_entry(commit, id);
1531 if (entry == NULL) {
1532 err = got_error_from_errno("alloc_commit_queue_entry");
1533 break;
1536 errcode = pthread_mutex_lock(&tog_mutex);
1537 if (errcode) {
1538 err = got_error_set_errno(errcode,
1539 "pthread_mutex_lock");
1540 break;
1543 entry->idx = a->commits->ncommits;
1544 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1545 a->commits->ncommits++;
1547 if (*a->searching == TOG_SEARCH_FORWARD &&
1548 !*a->search_next_done) {
1549 int have_match;
1550 err = match_commit(&have_match, id, commit, a->regex);
1551 if (err)
1552 break;
1553 if (have_match)
1554 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1557 errcode = pthread_mutex_unlock(&tog_mutex);
1558 if (errcode && err == NULL)
1559 err = got_error_set_errno(errcode,
1560 "pthread_mutex_unlock");
1561 if (err)
1562 break;
1563 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1565 return err;
1568 static void
1569 select_commit(struct tog_log_view_state *s)
1571 struct commit_queue_entry *entry;
1572 int ncommits = 0;
1574 entry = s->first_displayed_entry;
1575 while (entry) {
1576 if (ncommits == s->selected) {
1577 s->selected_entry = entry;
1578 break;
1580 entry = TAILQ_NEXT(entry, entry);
1581 ncommits++;
1585 static const struct got_error *
1586 draw_commits(struct tog_view *view)
1588 const struct got_error *err = NULL;
1589 struct tog_log_view_state *s = &view->state.log;
1590 struct commit_queue_entry *entry = s->selected_entry;
1591 const int limit = view->nlines;
1592 int width;
1593 int ncommits, author_cols = 4;
1594 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1595 char *refs_str = NULL;
1596 wchar_t *wline;
1597 struct tog_color *tc;
1598 static const size_t date_display_cols = 12;
1600 if (s->selected_entry &&
1601 !(view->searching && view->search_next_done == 0)) {
1602 struct got_reflist_head *refs;
1603 err = got_object_id_str(&id_str, s->selected_entry->id);
1604 if (err)
1605 return err;
1606 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1607 s->selected_entry->id);
1608 if (refs) {
1609 err = build_refs_str(&refs_str, refs,
1610 s->selected_entry->id, s->repo);
1611 if (err)
1612 goto done;
1616 if (s->thread_args.commits_needed == 0)
1617 halfdelay(10); /* disable fast refresh */
1619 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1620 if (asprintf(&ncommits_str, " [%d/%d] %s",
1621 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1622 (view->searching && !view->search_next_done) ?
1623 "searching..." : "loading...") == -1) {
1624 err = got_error_from_errno("asprintf");
1625 goto done;
1627 } else {
1628 const char *search_str = NULL;
1630 if (view->searching) {
1631 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1632 search_str = "no more matches";
1633 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1634 search_str = "no matches found";
1635 else if (!view->search_next_done)
1636 search_str = "searching...";
1639 if (asprintf(&ncommits_str, " [%d/%d] %s",
1640 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1641 search_str ? search_str :
1642 (refs_str ? refs_str : "")) == -1) {
1643 err = got_error_from_errno("asprintf");
1644 goto done;
1648 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1649 if (asprintf(&header, "commit %s %s%s",
1650 id_str ? id_str : "........................................",
1651 s->in_repo_path, ncommits_str) == -1) {
1652 err = got_error_from_errno("asprintf");
1653 header = NULL;
1654 goto done;
1656 } else if (asprintf(&header, "commit %s%s",
1657 id_str ? id_str : "........................................",
1658 ncommits_str) == -1) {
1659 err = got_error_from_errno("asprintf");
1660 header = NULL;
1661 goto done;
1663 err = format_line(&wline, &width, header, view->ncols, 0);
1664 if (err)
1665 goto done;
1667 werase(view->window);
1669 if (view_needs_focus_indication(view))
1670 wstandout(view->window);
1671 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1672 if (tc)
1673 wattr_on(view->window,
1674 COLOR_PAIR(tc->colorpair), NULL);
1675 waddwstr(view->window, wline);
1676 if (tc)
1677 wattr_off(view->window,
1678 COLOR_PAIR(tc->colorpair), NULL);
1679 while (width < view->ncols) {
1680 waddch(view->window, ' ');
1681 width++;
1683 if (view_needs_focus_indication(view))
1684 wstandend(view->window);
1685 free(wline);
1686 if (limit <= 1)
1687 goto done;
1689 /* Grow author column size if necessary. */
1690 entry = s->first_displayed_entry;
1691 ncommits = 0;
1692 while (entry) {
1693 char *author;
1694 wchar_t *wauthor;
1695 int width;
1696 if (ncommits >= limit - 1)
1697 break;
1698 author = strdup(got_object_commit_get_author(entry->commit));
1699 if (author == NULL) {
1700 err = got_error_from_errno("strdup");
1701 goto done;
1703 err = format_author(&wauthor, &width, author, COLS,
1704 date_display_cols);
1705 if (author_cols < width)
1706 author_cols = width;
1707 free(wauthor);
1708 free(author);
1709 ncommits++;
1710 entry = TAILQ_NEXT(entry, entry);
1713 entry = s->first_displayed_entry;
1714 s->last_displayed_entry = s->first_displayed_entry;
1715 ncommits = 0;
1716 while (entry) {
1717 if (ncommits >= limit - 1)
1718 break;
1719 if (ncommits == s->selected)
1720 wstandout(view->window);
1721 err = draw_commit(view, entry->commit, entry->id,
1722 date_display_cols, author_cols);
1723 if (ncommits == s->selected)
1724 wstandend(view->window);
1725 if (err)
1726 goto done;
1727 ncommits++;
1728 s->last_displayed_entry = entry;
1729 entry = TAILQ_NEXT(entry, entry);
1732 view_vborder(view);
1733 update_panels();
1734 doupdate();
1735 done:
1736 free(id_str);
1737 free(refs_str);
1738 free(ncommits_str);
1739 free(header);
1740 return err;
1743 static void
1744 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1746 struct commit_queue_entry *entry;
1747 int nscrolled = 0;
1749 entry = TAILQ_FIRST(&s->commits.head);
1750 if (s->first_displayed_entry == entry)
1751 return;
1753 entry = s->first_displayed_entry;
1754 while (entry && nscrolled < maxscroll) {
1755 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1756 if (entry) {
1757 s->first_displayed_entry = entry;
1758 nscrolled++;
1763 static const struct got_error *
1764 trigger_log_thread(struct tog_view *view, int wait)
1766 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1767 int errcode;
1769 halfdelay(1); /* fast refresh while loading commits */
1771 while (ta->commits_needed > 0 || ta->load_all) {
1772 if (ta->log_complete)
1773 break;
1775 /* Wake the log thread. */
1776 errcode = pthread_cond_signal(&ta->need_commits);
1777 if (errcode)
1778 return got_error_set_errno(errcode,
1779 "pthread_cond_signal");
1782 * The mutex will be released while the view loop waits
1783 * in wgetch(), at which time the log thread will run.
1785 if (!wait)
1786 break;
1788 /* Display progress update in log view. */
1789 show_log_view(view);
1790 update_panels();
1791 doupdate();
1793 /* Wait right here while next commit is being loaded. */
1794 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1795 if (errcode)
1796 return got_error_set_errno(errcode,
1797 "pthread_cond_wait");
1799 /* Display progress update in log view. */
1800 show_log_view(view);
1801 update_panels();
1802 doupdate();
1805 return NULL;
1808 static const struct got_error *
1809 log_scroll_down(struct tog_view *view, int maxscroll)
1811 struct tog_log_view_state *s = &view->state.log;
1812 const struct got_error *err = NULL;
1813 struct commit_queue_entry *pentry;
1814 int nscrolled = 0, ncommits_needed;
1816 if (s->last_displayed_entry == NULL)
1817 return NULL;
1819 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1820 if (s->commits.ncommits < ncommits_needed &&
1821 !s->thread_args.log_complete) {
1823 * Ask the log thread for required amount of commits.
1825 s->thread_args.commits_needed += maxscroll;
1826 err = trigger_log_thread(view, 1);
1827 if (err)
1828 return err;
1831 do {
1832 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1833 if (pentry == NULL)
1834 break;
1836 s->last_displayed_entry = pentry;
1838 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1839 if (pentry == NULL)
1840 break;
1841 s->first_displayed_entry = pentry;
1842 } while (++nscrolled < maxscroll);
1844 return err;
1847 static const struct got_error *
1848 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1849 struct got_commit_object *commit, struct got_object_id *commit_id,
1850 struct tog_view *log_view, struct got_repository *repo)
1852 const struct got_error *err;
1853 struct got_object_qid *parent_id;
1854 struct tog_view *diff_view;
1856 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1857 if (diff_view == NULL)
1858 return got_error_from_errno("view_open");
1860 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1861 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1862 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1863 if (err == NULL)
1864 *new_view = diff_view;
1865 return err;
1868 static const struct got_error *
1869 tree_view_visit_subtree(struct tog_tree_view_state *s,
1870 struct got_tree_object *subtree)
1872 struct tog_parent_tree *parent;
1874 parent = calloc(1, sizeof(*parent));
1875 if (parent == NULL)
1876 return got_error_from_errno("calloc");
1878 parent->tree = s->tree;
1879 parent->first_displayed_entry = s->first_displayed_entry;
1880 parent->selected_entry = s->selected_entry;
1881 parent->selected = s->selected;
1882 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1883 s->tree = subtree;
1884 s->selected = 0;
1885 s->first_displayed_entry = NULL;
1886 return NULL;
1889 static const struct got_error *
1890 tree_view_walk_path(struct tog_tree_view_state *s,
1891 struct got_object_id *commit_id, const char *path)
1893 const struct got_error *err = NULL;
1894 struct got_tree_object *tree = NULL;
1895 const char *p;
1896 char *slash, *subpath = NULL;
1898 /* Walk the path and open corresponding tree objects. */
1899 p = path;
1900 while (*p) {
1901 struct got_tree_entry *te;
1902 struct got_object_id *tree_id;
1903 char *te_name;
1905 while (p[0] == '/')
1906 p++;
1908 /* Ensure the correct subtree entry is selected. */
1909 slash = strchr(p, '/');
1910 if (slash == NULL)
1911 te_name = strdup(p);
1912 else
1913 te_name = strndup(p, slash - p);
1914 if (te_name == NULL) {
1915 err = got_error_from_errno("strndup");
1916 break;
1918 te = got_object_tree_find_entry(s->tree, te_name);
1919 if (te == NULL) {
1920 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1921 free(te_name);
1922 break;
1924 free(te_name);
1925 s->first_displayed_entry = s->selected_entry = te;
1927 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1928 break; /* jump to this file's entry */
1930 slash = strchr(p, '/');
1931 if (slash)
1932 subpath = strndup(path, slash - path);
1933 else
1934 subpath = strdup(path);
1935 if (subpath == NULL) {
1936 err = got_error_from_errno("strdup");
1937 break;
1940 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1941 subpath);
1942 if (err)
1943 break;
1945 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1946 free(tree_id);
1947 if (err)
1948 break;
1950 err = tree_view_visit_subtree(s, tree);
1951 if (err) {
1952 got_object_tree_close(tree);
1953 break;
1955 if (slash == NULL)
1956 break;
1957 free(subpath);
1958 subpath = NULL;
1959 p = slash;
1962 free(subpath);
1963 return err;
1966 static const struct got_error *
1967 browse_commit_tree(struct tog_view **new_view, int begin_x,
1968 struct commit_queue_entry *entry, const char *path,
1969 const char *head_ref_name, struct got_repository *repo)
1971 const struct got_error *err = NULL;
1972 struct tog_tree_view_state *s;
1973 struct tog_view *tree_view;
1975 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1976 if (tree_view == NULL)
1977 return got_error_from_errno("view_open");
1979 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1980 if (err)
1981 return err;
1982 s = &tree_view->state.tree;
1984 *new_view = tree_view;
1986 if (got_path_is_root_dir(path))
1987 return NULL;
1989 return tree_view_walk_path(s, entry->id, path);
1992 static const struct got_error *
1993 block_signals_used_by_main_thread(void)
1995 sigset_t sigset;
1996 int errcode;
1998 if (sigemptyset(&sigset) == -1)
1999 return got_error_from_errno("sigemptyset");
2001 /* tog handles SIGWINCH and SIGCONT */
2002 if (sigaddset(&sigset, SIGWINCH) == -1)
2003 return got_error_from_errno("sigaddset");
2004 if (sigaddset(&sigset, SIGCONT) == -1)
2005 return got_error_from_errno("sigaddset");
2007 /* ncurses handles SIGTSTP */
2008 if (sigaddset(&sigset, SIGTSTP) == -1)
2009 return got_error_from_errno("sigaddset");
2011 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2012 if (errcode)
2013 return got_error_set_errno(errcode, "pthread_sigmask");
2015 return NULL;
2018 static void *
2019 log_thread(void *arg)
2021 const struct got_error *err = NULL;
2022 int errcode = 0;
2023 struct tog_log_thread_args *a = arg;
2024 int done = 0;
2026 err = block_signals_used_by_main_thread();
2027 if (err)
2028 return (void *)err;
2030 while (!done && !err && !tog_sigpipe_received) {
2031 err = queue_commits(a);
2032 if (err) {
2033 if (err->code != GOT_ERR_ITER_COMPLETED)
2034 return (void *)err;
2035 err = NULL;
2036 done = 1;
2037 } else if (a->commits_needed > 0 && !a->load_all)
2038 a->commits_needed--;
2040 errcode = pthread_mutex_lock(&tog_mutex);
2041 if (errcode) {
2042 err = got_error_set_errno(errcode,
2043 "pthread_mutex_lock");
2044 break;
2045 } else if (*a->quit)
2046 done = 1;
2047 else if (*a->first_displayed_entry == NULL) {
2048 *a->first_displayed_entry =
2049 TAILQ_FIRST(&a->commits->head);
2050 *a->selected_entry = *a->first_displayed_entry;
2053 errcode = pthread_cond_signal(&a->commit_loaded);
2054 if (errcode) {
2055 err = got_error_set_errno(errcode,
2056 "pthread_cond_signal");
2057 pthread_mutex_unlock(&tog_mutex);
2058 break;
2061 if (done)
2062 a->commits_needed = 0;
2063 else {
2064 if (a->commits_needed == 0 && !a->load_all) {
2065 errcode = pthread_cond_wait(&a->need_commits,
2066 &tog_mutex);
2067 if (errcode)
2068 err = got_error_set_errno(errcode,
2069 "pthread_cond_wait");
2070 if (*a->quit)
2071 done = 1;
2075 errcode = pthread_mutex_unlock(&tog_mutex);
2076 if (errcode && err == NULL)
2077 err = got_error_set_errno(errcode,
2078 "pthread_mutex_unlock");
2080 a->log_complete = 1;
2081 return (void *)err;
2084 static const struct got_error *
2085 stop_log_thread(struct tog_log_view_state *s)
2087 const struct got_error *err = NULL;
2088 int errcode;
2090 if (s->thread) {
2091 s->quit = 1;
2092 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2093 if (errcode)
2094 return got_error_set_errno(errcode,
2095 "pthread_cond_signal");
2096 errcode = pthread_mutex_unlock(&tog_mutex);
2097 if (errcode)
2098 return got_error_set_errno(errcode,
2099 "pthread_mutex_unlock");
2100 errcode = pthread_join(s->thread, (void **)&err);
2101 if (errcode)
2102 return got_error_set_errno(errcode, "pthread_join");
2103 errcode = pthread_mutex_lock(&tog_mutex);
2104 if (errcode)
2105 return got_error_set_errno(errcode,
2106 "pthread_mutex_lock");
2107 s->thread = 0; //NULL;
2110 if (s->thread_args.repo) {
2111 err = got_repo_close(s->thread_args.repo);
2112 s->thread_args.repo = NULL;
2115 if (s->thread_args.graph) {
2116 got_commit_graph_close(s->thread_args.graph);
2117 s->thread_args.graph = NULL;
2120 return err;
2123 static const struct got_error *
2124 close_log_view(struct tog_view *view)
2126 const struct got_error *err = NULL;
2127 struct tog_log_view_state *s = &view->state.log;
2128 int errcode;
2130 err = stop_log_thread(s);
2132 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2133 if (errcode && err == NULL)
2134 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2136 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2137 if (errcode && err == NULL)
2138 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2140 free_commits(&s->commits);
2141 free(s->in_repo_path);
2142 s->in_repo_path = NULL;
2143 free(s->start_id);
2144 s->start_id = NULL;
2145 free(s->head_ref_name);
2146 s->head_ref_name = NULL;
2147 return err;
2150 static const struct got_error *
2151 search_start_log_view(struct tog_view *view)
2153 struct tog_log_view_state *s = &view->state.log;
2155 s->matched_entry = NULL;
2156 s->search_entry = NULL;
2157 return NULL;
2160 static const struct got_error *
2161 search_next_log_view(struct tog_view *view)
2163 const struct got_error *err = NULL;
2164 struct tog_log_view_state *s = &view->state.log;
2165 struct commit_queue_entry *entry;
2167 /* Display progress update in log view. */
2168 show_log_view(view);
2169 update_panels();
2170 doupdate();
2172 if (s->search_entry) {
2173 int errcode, ch;
2174 errcode = pthread_mutex_unlock(&tog_mutex);
2175 if (errcode)
2176 return got_error_set_errno(errcode,
2177 "pthread_mutex_unlock");
2178 ch = wgetch(view->window);
2179 errcode = pthread_mutex_lock(&tog_mutex);
2180 if (errcode)
2181 return got_error_set_errno(errcode,
2182 "pthread_mutex_lock");
2183 if (ch == KEY_BACKSPACE) {
2184 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2185 return NULL;
2187 if (view->searching == TOG_SEARCH_FORWARD)
2188 entry = TAILQ_NEXT(s->search_entry, entry);
2189 else
2190 entry = TAILQ_PREV(s->search_entry,
2191 commit_queue_head, entry);
2192 } else if (s->matched_entry) {
2193 if (view->searching == TOG_SEARCH_FORWARD)
2194 entry = TAILQ_NEXT(s->matched_entry, entry);
2195 else
2196 entry = TAILQ_PREV(s->matched_entry,
2197 commit_queue_head, entry);
2198 } else {
2199 if (view->searching == TOG_SEARCH_FORWARD)
2200 entry = TAILQ_FIRST(&s->commits.head);
2201 else
2202 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2205 while (1) {
2206 int have_match = 0;
2208 if (entry == NULL) {
2209 if (s->thread_args.log_complete ||
2210 view->searching == TOG_SEARCH_BACKWARD) {
2211 view->search_next_done =
2212 (s->matched_entry == NULL ?
2213 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2214 s->search_entry = NULL;
2215 return NULL;
2218 * Poke the log thread for more commits and return,
2219 * allowing the main loop to make progress. Search
2220 * will resume at s->search_entry once we come back.
2222 s->thread_args.commits_needed++;
2223 return trigger_log_thread(view, 0);
2226 err = match_commit(&have_match, entry->id, entry->commit,
2227 &view->regex);
2228 if (err)
2229 break;
2230 if (have_match) {
2231 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2232 s->matched_entry = entry;
2233 break;
2236 s->search_entry = entry;
2237 if (view->searching == TOG_SEARCH_FORWARD)
2238 entry = TAILQ_NEXT(entry, entry);
2239 else
2240 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2243 if (s->matched_entry) {
2244 int cur = s->selected_entry->idx;
2245 while (cur < s->matched_entry->idx) {
2246 err = input_log_view(NULL, view, KEY_DOWN);
2247 if (err)
2248 return err;
2249 cur++;
2251 while (cur > s->matched_entry->idx) {
2252 err = input_log_view(NULL, view, KEY_UP);
2253 if (err)
2254 return err;
2255 cur--;
2259 s->search_entry = NULL;
2261 return NULL;
2264 static const struct got_error *
2265 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2266 struct got_repository *repo, const char *head_ref_name,
2267 const char *in_repo_path, int log_branches)
2269 const struct got_error *err = NULL;
2270 struct tog_log_view_state *s = &view->state.log;
2271 struct got_repository *thread_repo = NULL;
2272 struct got_commit_graph *thread_graph = NULL;
2273 int errcode;
2275 if (in_repo_path != s->in_repo_path) {
2276 free(s->in_repo_path);
2277 s->in_repo_path = strdup(in_repo_path);
2278 if (s->in_repo_path == NULL)
2279 return got_error_from_errno("strdup");
2282 /* The commit queue only contains commits being displayed. */
2283 TAILQ_INIT(&s->commits.head);
2284 s->commits.ncommits = 0;
2286 s->repo = repo;
2287 if (head_ref_name) {
2288 s->head_ref_name = strdup(head_ref_name);
2289 if (s->head_ref_name == NULL) {
2290 err = got_error_from_errno("strdup");
2291 goto done;
2294 s->start_id = got_object_id_dup(start_id);
2295 if (s->start_id == NULL) {
2296 err = got_error_from_errno("got_object_id_dup");
2297 goto done;
2299 s->log_branches = log_branches;
2301 STAILQ_INIT(&s->colors);
2302 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2303 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2304 get_color_value("TOG_COLOR_COMMIT"));
2305 if (err)
2306 goto done;
2307 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2308 get_color_value("TOG_COLOR_AUTHOR"));
2309 if (err) {
2310 free_colors(&s->colors);
2311 goto done;
2313 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2314 get_color_value("TOG_COLOR_DATE"));
2315 if (err) {
2316 free_colors(&s->colors);
2317 goto done;
2321 view->show = show_log_view;
2322 view->input = input_log_view;
2323 view->close = close_log_view;
2324 view->search_start = search_start_log_view;
2325 view->search_next = search_next_log_view;
2327 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2328 if (err)
2329 goto done;
2330 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2331 !s->log_branches);
2332 if (err)
2333 goto done;
2334 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2335 s->repo, NULL, NULL);
2336 if (err)
2337 goto done;
2339 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2340 if (errcode) {
2341 err = got_error_set_errno(errcode, "pthread_cond_init");
2342 goto done;
2344 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2345 if (errcode) {
2346 err = got_error_set_errno(errcode, "pthread_cond_init");
2347 goto done;
2350 s->thread_args.commits_needed = view->nlines;
2351 s->thread_args.graph = thread_graph;
2352 s->thread_args.commits = &s->commits;
2353 s->thread_args.in_repo_path = s->in_repo_path;
2354 s->thread_args.start_id = s->start_id;
2355 s->thread_args.repo = thread_repo;
2356 s->thread_args.log_complete = 0;
2357 s->thread_args.quit = &s->quit;
2358 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2359 s->thread_args.selected_entry = &s->selected_entry;
2360 s->thread_args.searching = &view->searching;
2361 s->thread_args.search_next_done = &view->search_next_done;
2362 s->thread_args.regex = &view->regex;
2363 done:
2364 if (err)
2365 close_log_view(view);
2366 return err;
2369 static const struct got_error *
2370 show_log_view(struct tog_view *view)
2372 const struct got_error *err;
2373 struct tog_log_view_state *s = &view->state.log;
2375 if (s->thread == 0) { //NULL) {
2376 int errcode = pthread_create(&s->thread, NULL, log_thread,
2377 &s->thread_args);
2378 if (errcode)
2379 return got_error_set_errno(errcode, "pthread_create");
2380 if (s->thread_args.commits_needed > 0) {
2381 err = trigger_log_thread(view, 1);
2382 if (err)
2383 return err;
2387 return draw_commits(view);
2390 static const struct got_error *
2391 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2393 const struct got_error *err = NULL;
2394 struct tog_log_view_state *s = &view->state.log;
2395 struct tog_view *diff_view = NULL, *tree_view = NULL;
2396 struct tog_view *ref_view = NULL;
2397 struct commit_queue_entry *entry;
2398 int begin_x = 0, n;
2400 if (s->thread_args.load_all) {
2401 if (ch == KEY_BACKSPACE)
2402 s->thread_args.load_all = 0;
2403 else if (s->thread_args.log_complete) {
2404 s->thread_args.load_all = 0;
2405 log_scroll_down(view, s->commits.ncommits);
2406 s->selected = MIN(view->nlines - 2,
2407 s->commits.ncommits - 1);
2408 select_commit(s);
2410 return NULL;
2413 switch (ch) {
2414 case 'q':
2415 s->quit = 1;
2416 break;
2417 case 'k':
2418 case KEY_UP:
2419 case '<':
2420 case ',':
2421 if (s->first_displayed_entry == NULL)
2422 break;
2423 if (s->selected > 0)
2424 s->selected--;
2425 else
2426 log_scroll_up(s, 1);
2427 select_commit(s);
2428 break;
2429 case 'g':
2430 case KEY_HOME:
2431 s->selected = 0;
2432 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2433 select_commit(s);
2434 break;
2435 case KEY_PPAGE:
2436 case CTRL('b'):
2437 if (s->first_displayed_entry == NULL)
2438 break;
2439 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2440 s->selected = 0;
2441 else
2442 log_scroll_up(s, view->nlines - 1);
2443 select_commit(s);
2444 break;
2445 case 'j':
2446 case KEY_DOWN:
2447 case '>':
2448 case '.':
2449 if (s->first_displayed_entry == NULL)
2450 break;
2451 if (s->selected < MIN(view->nlines - 2,
2452 s->commits.ncommits - 1))
2453 s->selected++;
2454 else {
2455 err = log_scroll_down(view, 1);
2456 if (err)
2457 break;
2459 select_commit(s);
2460 break;
2461 case 'G':
2462 case KEY_END: {
2463 /* We don't know yet how many commits, so we're forced to
2464 * traverse them all. */
2465 if (!s->thread_args.log_complete) {
2466 s->thread_args.load_all = 1;
2467 return trigger_log_thread(view, 0);
2470 s->selected = 0;
2471 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2472 for (n = 0; n < view->nlines - 1; n++) {
2473 if (entry == NULL)
2474 break;
2475 s->first_displayed_entry = entry;
2476 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2478 if (n > 0)
2479 s->selected = n - 1;
2480 select_commit(s);
2481 break;
2483 case KEY_NPAGE:
2484 case CTRL('f'): {
2485 struct commit_queue_entry *first;
2486 first = s->first_displayed_entry;
2487 if (first == NULL)
2488 break;
2489 err = log_scroll_down(view, view->nlines - 1);
2490 if (err)
2491 break;
2492 if (first == s->first_displayed_entry &&
2493 s->selected < MIN(view->nlines - 2,
2494 s->commits.ncommits - 1)) {
2495 /* can't scroll further down */
2496 s->selected = MIN(view->nlines - 2,
2497 s->commits.ncommits - 1);
2499 select_commit(s);
2500 break;
2502 case KEY_RESIZE:
2503 if (s->selected > view->nlines - 2)
2504 s->selected = view->nlines - 2;
2505 if (s->selected > s->commits.ncommits - 1)
2506 s->selected = s->commits.ncommits - 1;
2507 select_commit(s);
2508 if (s->commits.ncommits < view->nlines - 1 &&
2509 !s->thread_args.log_complete) {
2510 s->thread_args.commits_needed += (view->nlines - 1) -
2511 s->commits.ncommits;
2512 err = trigger_log_thread(view, 1);
2514 break;
2515 case KEY_ENTER:
2516 case ' ':
2517 case '\r':
2518 if (s->selected_entry == NULL)
2519 break;
2520 if (view_is_parent_view(view))
2521 begin_x = view_split_begin_x(view->begin_x);
2522 err = open_diff_view_for_commit(&diff_view, begin_x,
2523 s->selected_entry->commit, s->selected_entry->id,
2524 view, s->repo);
2525 if (err)
2526 break;
2527 view->focussed = 0;
2528 diff_view->focussed = 1;
2529 if (view_is_parent_view(view)) {
2530 err = view_close_child(view);
2531 if (err)
2532 return err;
2533 view_set_child(view, diff_view);
2534 view->focus_child = 1;
2535 } else
2536 *new_view = diff_view;
2537 break;
2538 case 't':
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 = browse_commit_tree(&tree_view, begin_x,
2544 s->selected_entry, s->in_repo_path, s->head_ref_name,
2545 s->repo);
2546 if (err)
2547 break;
2548 view->focussed = 0;
2549 tree_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, tree_view);
2555 view->focus_child = 1;
2556 } else
2557 *new_view = tree_view;
2558 break;
2559 case KEY_BACKSPACE:
2560 case CTRL('l'):
2561 case 'B':
2562 if (ch == KEY_BACKSPACE &&
2563 got_path_is_root_dir(s->in_repo_path))
2564 break;
2565 err = stop_log_thread(s);
2566 if (err)
2567 return err;
2568 if (ch == KEY_BACKSPACE) {
2569 char *parent_path;
2570 err = got_path_dirname(&parent_path, s->in_repo_path);
2571 if (err)
2572 return err;
2573 free(s->in_repo_path);
2574 s->in_repo_path = parent_path;
2575 s->thread_args.in_repo_path = s->in_repo_path;
2576 } else if (ch == CTRL('l')) {
2577 struct got_object_id *start_id;
2578 err = got_repo_match_object_id(&start_id, NULL,
2579 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2580 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2581 if (err)
2582 return err;
2583 free(s->start_id);
2584 s->start_id = start_id;
2585 s->thread_args.start_id = s->start_id;
2586 } else /* 'B' */
2587 s->log_branches = !s->log_branches;
2589 err = got_repo_open(&s->thread_args.repo,
2590 got_repo_get_path(s->repo), NULL);
2591 if (err)
2592 return err;
2593 tog_free_refs();
2594 err = tog_load_refs(s->repo);
2595 if (err)
2596 return err;
2597 err = got_commit_graph_open(&s->thread_args.graph,
2598 s->in_repo_path, !s->log_branches);
2599 if (err)
2600 return err;
2601 err = got_commit_graph_iter_start(s->thread_args.graph,
2602 s->start_id, s->repo, NULL, NULL);
2603 if (err)
2604 return err;
2605 free_commits(&s->commits);
2606 s->first_displayed_entry = NULL;
2607 s->last_displayed_entry = NULL;
2608 s->selected_entry = NULL;
2609 s->selected = 0;
2610 s->thread_args.log_complete = 0;
2611 s->quit = 0;
2612 s->thread_args.commits_needed = view->nlines;
2613 break;
2614 case 'r':
2615 if (view_is_parent_view(view))
2616 begin_x = view_split_begin_x(view->begin_x);
2617 ref_view = view_open(view->nlines, view->ncols,
2618 view->begin_y, begin_x, TOG_VIEW_REF);
2619 if (ref_view == NULL)
2620 return got_error_from_errno("view_open");
2621 err = open_ref_view(ref_view, s->repo);
2622 if (err) {
2623 view_close(ref_view);
2624 return err;
2626 view->focussed = 0;
2627 ref_view->focussed = 1;
2628 if (view_is_parent_view(view)) {
2629 err = view_close_child(view);
2630 if (err)
2631 return err;
2632 view_set_child(view, ref_view);
2633 view->focus_child = 1;
2634 } else
2635 *new_view = ref_view;
2636 break;
2637 default:
2638 break;
2641 return err;
2644 static const struct got_error *
2645 apply_unveil(const char *repo_path, const char *worktree_path)
2647 const struct got_error *error;
2649 #ifdef PROFILE
2650 if (unveil("gmon.out", "rwc") != 0)
2651 return got_error_from_errno2("unveil", "gmon.out");
2652 #endif
2653 if (repo_path && unveil(repo_path, "r") != 0)
2654 return got_error_from_errno2("unveil", repo_path);
2656 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2657 return got_error_from_errno2("unveil", worktree_path);
2659 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2660 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2662 error = got_privsep_unveil_exec_helpers();
2663 if (error != NULL)
2664 return error;
2666 if (unveil(NULL, NULL) != 0)
2667 return got_error_from_errno("unveil");
2669 return NULL;
2672 static void
2673 init_curses(void)
2675 initscr();
2676 cbreak();
2677 halfdelay(1); /* Do fast refresh while initial view is loading. */
2678 noecho();
2679 nonl();
2680 intrflush(stdscr, FALSE);
2681 keypad(stdscr, TRUE);
2682 curs_set(0);
2683 if (getenv("TOG_COLORS") != NULL) {
2684 start_color();
2685 use_default_colors();
2687 signal(SIGWINCH, tog_sigwinch);
2688 signal(SIGPIPE, tog_sigpipe);
2689 signal(SIGCONT, tog_sigcont);
2692 static const struct got_error *
2693 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2694 struct got_repository *repo, struct got_worktree *worktree)
2696 const struct got_error *err = NULL;
2698 if (argc == 0) {
2699 *in_repo_path = strdup("/");
2700 if (*in_repo_path == NULL)
2701 return got_error_from_errno("strdup");
2702 return NULL;
2705 if (worktree) {
2706 const char *prefix = got_worktree_get_path_prefix(worktree);
2707 char *p;
2709 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2710 if (err)
2711 return err;
2712 if (asprintf(in_repo_path, "%s%s%s", prefix,
2713 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2714 p) == -1) {
2715 err = got_error_from_errno("asprintf");
2716 *in_repo_path = NULL;
2718 free(p);
2719 } else
2720 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2722 return err;
2725 static const struct got_error *
2726 cmd_log(int argc, char *argv[])
2728 const struct got_error *error;
2729 struct got_repository *repo = NULL;
2730 struct got_worktree *worktree = NULL;
2731 struct got_object_id *start_id = NULL;
2732 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2733 char *start_commit = NULL, *label = NULL;
2734 struct got_reference *ref = NULL;
2735 const char *head_ref_name = NULL;
2736 int ch, log_branches = 0;
2737 struct tog_view *view;
2739 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2740 switch (ch) {
2741 case 'b':
2742 log_branches = 1;
2743 break;
2744 case 'c':
2745 start_commit = optarg;
2746 break;
2747 case 'r':
2748 repo_path = realpath(optarg, NULL);
2749 if (repo_path == NULL)
2750 return got_error_from_errno2("realpath",
2751 optarg);
2752 break;
2753 default:
2754 usage_log();
2755 /* NOTREACHED */
2759 argc -= optind;
2760 argv += optind;
2762 if (argc > 1)
2763 usage_log();
2765 if (repo_path == NULL) {
2766 cwd = getcwd(NULL, 0);
2767 if (cwd == NULL)
2768 return got_error_from_errno("getcwd");
2769 error = got_worktree_open(&worktree, cwd);
2770 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2771 goto done;
2772 if (worktree)
2773 repo_path =
2774 strdup(got_worktree_get_repo_path(worktree));
2775 else
2776 repo_path = strdup(cwd);
2777 if (repo_path == NULL) {
2778 error = got_error_from_errno("strdup");
2779 goto done;
2783 error = got_repo_open(&repo, repo_path, NULL);
2784 if (error != NULL)
2785 goto done;
2787 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2788 repo, worktree);
2789 if (error)
2790 goto done;
2792 init_curses();
2794 error = apply_unveil(got_repo_get_path(repo),
2795 worktree ? got_worktree_get_root_path(worktree) : NULL);
2796 if (error)
2797 goto done;
2799 /* already loaded by tog_log_with_path()? */
2800 if (TAILQ_EMPTY(&tog_refs)) {
2801 error = tog_load_refs(repo);
2802 if (error)
2803 goto done;
2806 if (start_commit == NULL) {
2807 error = got_repo_match_object_id(&start_id, &label,
2808 worktree ? got_worktree_get_head_ref_name(worktree) :
2809 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2810 if (error)
2811 goto done;
2812 head_ref_name = label;
2813 } else {
2814 error = got_ref_open(&ref, repo, start_commit, 0);
2815 if (error == NULL)
2816 head_ref_name = got_ref_get_name(ref);
2817 else if (error->code != GOT_ERR_NOT_REF)
2818 goto done;
2819 error = got_repo_match_object_id(&start_id, NULL,
2820 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2821 if (error)
2822 goto done;
2825 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2826 if (view == NULL) {
2827 error = got_error_from_errno("view_open");
2828 goto done;
2830 error = open_log_view(view, start_id, repo, head_ref_name,
2831 in_repo_path, log_branches);
2832 if (error)
2833 goto done;
2834 if (worktree) {
2835 /* Release work tree lock. */
2836 got_worktree_close(worktree);
2837 worktree = NULL;
2839 error = view_loop(view);
2840 done:
2841 free(in_repo_path);
2842 free(repo_path);
2843 free(cwd);
2844 free(start_id);
2845 free(label);
2846 if (ref)
2847 got_ref_close(ref);
2848 if (repo) {
2849 const struct got_error *close_err = got_repo_close(repo);
2850 if (error == NULL)
2851 error = close_err;
2853 if (worktree)
2854 got_worktree_close(worktree);
2855 tog_free_refs();
2856 return error;
2859 __dead static void
2860 usage_diff(void)
2862 endwin();
2863 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2864 "[-w] object1 object2\n", getprogname());
2865 exit(1);
2868 static int
2869 match_line(const char *line, regex_t *regex, size_t nmatch,
2870 regmatch_t *regmatch)
2872 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2875 struct tog_color *
2876 match_color(struct tog_colors *colors, const char *line)
2878 struct tog_color *tc = NULL;
2880 STAILQ_FOREACH(tc, colors, entry) {
2881 if (match_line(line, &tc->regex, 0, NULL))
2882 return tc;
2885 return NULL;
2888 static const struct got_error *
2889 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2890 WINDOW *window, regmatch_t *regmatch)
2892 const struct got_error *err = NULL;
2893 wchar_t *wline;
2894 int width;
2895 char *s;
2897 *wtotal = 0;
2899 s = strndup(line, regmatch->rm_so);
2900 if (s == NULL)
2901 return got_error_from_errno("strndup");
2903 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2904 if (err) {
2905 free(s);
2906 return err;
2908 waddwstr(window, wline);
2909 free(wline);
2910 free(s);
2911 wlimit -= width;
2912 *wtotal += width;
2914 if (wlimit > 0) {
2915 s = strndup(line + regmatch->rm_so,
2916 regmatch->rm_eo - regmatch->rm_so);
2917 if (s == NULL) {
2918 err = got_error_from_errno("strndup");
2919 free(s);
2920 return err;
2922 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2923 if (err) {
2924 free(s);
2925 return err;
2927 wattr_on(window, A_STANDOUT, NULL);
2928 waddwstr(window, wline);
2929 wattr_off(window, A_STANDOUT, NULL);
2930 free(wline);
2931 free(s);
2932 wlimit -= width;
2933 *wtotal += width;
2936 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2937 err = format_line(&wline, &width,
2938 line + regmatch->rm_eo, wlimit, col_tab_align);
2939 if (err)
2940 return err;
2941 waddwstr(window, wline);
2942 free(wline);
2943 *wtotal += width;
2946 return NULL;
2949 static const struct got_error *
2950 draw_file(struct tog_view *view, const char *header)
2952 struct tog_diff_view_state *s = &view->state.diff;
2953 regmatch_t *regmatch = &view->regmatch;
2954 const struct got_error *err;
2955 int nprinted = 0;
2956 char *line;
2957 size_t linesize = 0;
2958 ssize_t linelen;
2959 struct tog_color *tc;
2960 wchar_t *wline;
2961 int width;
2962 int max_lines = view->nlines;
2963 int nlines = s->nlines;
2964 off_t line_offset;
2966 line_offset = s->line_offsets[s->first_displayed_line - 1];
2967 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2968 return got_error_from_errno("fseek");
2970 werase(view->window);
2972 if (header) {
2973 if (asprintf(&line, "[%d/%d] %s",
2974 s->first_displayed_line - 1 + s->selected_line, nlines,
2975 header) == -1)
2976 return got_error_from_errno("asprintf");
2977 err = format_line(&wline, &width, line, view->ncols, 0);
2978 free(line);
2979 if (err)
2980 return err;
2982 if (view_needs_focus_indication(view))
2983 wstandout(view->window);
2984 waddwstr(view->window, wline);
2985 free(wline);
2986 wline = NULL;
2987 if (view_needs_focus_indication(view))
2988 wstandend(view->window);
2989 if (width <= view->ncols - 1)
2990 waddch(view->window, '\n');
2992 if (max_lines <= 1)
2993 return NULL;
2994 max_lines--;
2997 s->eof = 0;
2998 line = NULL;
2999 while (max_lines > 0 && nprinted < max_lines) {
3000 linelen = getline(&line, &linesize, s->f);
3001 if (linelen == -1) {
3002 if (feof(s->f)) {
3003 s->eof = 1;
3004 break;
3006 free(line);
3007 return got_ferror(s->f, GOT_ERR_IO);
3010 tc = match_color(&s->colors, line);
3011 if (tc)
3012 wattr_on(view->window,
3013 COLOR_PAIR(tc->colorpair), NULL);
3014 if (s->first_displayed_line + nprinted == s->matched_line &&
3015 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3016 err = add_matched_line(&width, line, view->ncols, 0,
3017 view->window, regmatch);
3018 if (err) {
3019 free(line);
3020 return err;
3022 } else {
3023 err = format_line(&wline, &width, line, view->ncols, 0);
3024 if (err) {
3025 free(line);
3026 return err;
3028 waddwstr(view->window, wline);
3029 free(wline);
3030 wline = NULL;
3032 if (tc)
3033 wattr_off(view->window,
3034 COLOR_PAIR(tc->colorpair), NULL);
3035 if (width <= view->ncols - 1)
3036 waddch(view->window, '\n');
3037 nprinted++;
3039 free(line);
3040 if (nprinted >= 1)
3041 s->last_displayed_line = s->first_displayed_line +
3042 (nprinted - 1);
3043 else
3044 s->last_displayed_line = s->first_displayed_line;
3046 view_vborder(view);
3048 if (s->eof) {
3049 while (nprinted < view->nlines) {
3050 waddch(view->window, '\n');
3051 nprinted++;
3054 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3055 if (err) {
3056 return err;
3059 wstandout(view->window);
3060 waddwstr(view->window, wline);
3061 free(wline);
3062 wline = NULL;
3063 wstandend(view->window);
3066 return NULL;
3069 static char *
3070 get_datestr(time_t *time, char *datebuf)
3072 struct tm mytm, *tm;
3073 char *p, *s;
3075 tm = gmtime_r(time, &mytm);
3076 if (tm == NULL)
3077 return NULL;
3078 s = asctime_r(tm, datebuf);
3079 if (s == NULL)
3080 return NULL;
3081 p = strchr(s, '\n');
3082 if (p)
3083 *p = '\0';
3084 return s;
3087 static const struct got_error *
3088 get_changed_paths(struct got_pathlist_head *paths,
3089 struct got_commit_object *commit, struct got_repository *repo)
3091 const struct got_error *err = NULL;
3092 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3093 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3094 struct got_object_qid *qid;
3096 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3097 if (qid != NULL) {
3098 struct got_commit_object *pcommit;
3099 err = got_object_open_as_commit(&pcommit, repo,
3100 qid->id);
3101 if (err)
3102 return err;
3104 tree_id1 = got_object_id_dup(
3105 got_object_commit_get_tree_id(pcommit));
3106 if (tree_id1 == NULL) {
3107 got_object_commit_close(pcommit);
3108 return got_error_from_errno("got_object_id_dup");
3110 got_object_commit_close(pcommit);
3114 if (tree_id1) {
3115 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3116 if (err)
3117 goto done;
3120 tree_id2 = got_object_commit_get_tree_id(commit);
3121 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3122 if (err)
3123 goto done;
3125 err = got_diff_tree(tree1, tree2, "", "", repo,
3126 got_diff_tree_collect_changed_paths, paths, 0);
3127 done:
3128 if (tree1)
3129 got_object_tree_close(tree1);
3130 if (tree2)
3131 got_object_tree_close(tree2);
3132 free(tree_id1);
3133 return err;
3136 static const struct got_error *
3137 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3139 off_t *p;
3141 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3142 if (p == NULL)
3143 return got_error_from_errno("reallocarray");
3144 *line_offsets = p;
3145 (*line_offsets)[*nlines] = off;
3146 (*nlines)++;
3147 return NULL;
3150 static const struct got_error *
3151 write_commit_info(off_t **line_offsets, size_t *nlines,
3152 struct got_object_id *commit_id, struct got_reflist_head *refs,
3153 struct got_repository *repo, FILE *outfile)
3155 const struct got_error *err = NULL;
3156 char datebuf[26], *datestr;
3157 struct got_commit_object *commit;
3158 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3159 time_t committer_time;
3160 const char *author, *committer;
3161 char *refs_str = NULL;
3162 struct got_pathlist_head changed_paths;
3163 struct got_pathlist_entry *pe;
3164 off_t outoff = 0;
3165 int n;
3167 TAILQ_INIT(&changed_paths);
3169 if (refs) {
3170 err = build_refs_str(&refs_str, refs, commit_id, repo);
3171 if (err)
3172 return err;
3175 err = got_object_open_as_commit(&commit, repo, commit_id);
3176 if (err)
3177 return err;
3179 err = got_object_id_str(&id_str, commit_id);
3180 if (err) {
3181 err = got_error_from_errno("got_object_id_str");
3182 goto done;
3185 err = add_line_offset(line_offsets, nlines, 0);
3186 if (err)
3187 goto done;
3189 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3190 refs_str ? refs_str : "", refs_str ? ")" : "");
3191 if (n < 0) {
3192 err = got_error_from_errno("fprintf");
3193 goto done;
3195 outoff += n;
3196 err = add_line_offset(line_offsets, nlines, outoff);
3197 if (err)
3198 goto done;
3200 n = fprintf(outfile, "from: %s\n",
3201 got_object_commit_get_author(commit));
3202 if (n < 0) {
3203 err = got_error_from_errno("fprintf");
3204 goto done;
3206 outoff += n;
3207 err = add_line_offset(line_offsets, nlines, outoff);
3208 if (err)
3209 goto done;
3211 committer_time = got_object_commit_get_committer_time(commit);
3212 datestr = get_datestr(&committer_time, datebuf);
3213 if (datestr) {
3214 n = fprintf(outfile, "date: %s UTC\n", datestr);
3215 if (n < 0) {
3216 err = got_error_from_errno("fprintf");
3217 goto done;
3219 outoff += n;
3220 err = add_line_offset(line_offsets, nlines, outoff);
3221 if (err)
3222 goto done;
3224 author = got_object_commit_get_author(commit);
3225 committer = got_object_commit_get_committer(commit);
3226 if (strcmp(author, committer) != 0) {
3227 n = fprintf(outfile, "via: %s\n", committer);
3228 if (n < 0) {
3229 err = got_error_from_errno("fprintf");
3230 goto done;
3232 outoff += n;
3233 err = add_line_offset(line_offsets, nlines, outoff);
3234 if (err)
3235 goto done;
3237 err = got_object_commit_get_logmsg(&logmsg, commit);
3238 if (err)
3239 goto done;
3240 s = logmsg;
3241 while ((line = strsep(&s, "\n")) != NULL) {
3242 n = fprintf(outfile, "%s\n", line);
3243 if (n < 0) {
3244 err = got_error_from_errno("fprintf");
3245 goto done;
3247 outoff += n;
3248 err = add_line_offset(line_offsets, nlines, outoff);
3249 if (err)
3250 goto done;
3253 err = get_changed_paths(&changed_paths, commit, repo);
3254 if (err)
3255 goto done;
3256 TAILQ_FOREACH(pe, &changed_paths, entry) {
3257 struct got_diff_changed_path *cp = pe->data;
3258 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3259 if (n < 0) {
3260 err = got_error_from_errno("fprintf");
3261 goto done;
3263 outoff += n;
3264 err = add_line_offset(line_offsets, nlines, outoff);
3265 if (err)
3266 goto done;
3267 free((char *)pe->path);
3268 free(pe->data);
3271 fputc('\n', outfile);
3272 outoff++;
3273 err = add_line_offset(line_offsets, nlines, outoff);
3274 done:
3275 got_pathlist_free(&changed_paths);
3276 free(id_str);
3277 free(logmsg);
3278 free(refs_str);
3279 got_object_commit_close(commit);
3280 if (err) {
3281 free(*line_offsets);
3282 *line_offsets = NULL;
3283 *nlines = 0;
3285 return err;
3288 static const struct got_error *
3289 create_diff(struct tog_diff_view_state *s)
3291 const struct got_error *err = NULL;
3292 FILE *f = NULL;
3293 int obj_type;
3295 free(s->line_offsets);
3296 s->line_offsets = malloc(sizeof(off_t));
3297 if (s->line_offsets == NULL)
3298 return got_error_from_errno("malloc");
3299 s->nlines = 0;
3301 f = got_opentemp();
3302 if (f == NULL) {
3303 err = got_error_from_errno("got_opentemp");
3304 goto done;
3306 if (s->f && fclose(s->f) == EOF) {
3307 err = got_error_from_errno("fclose");
3308 goto done;
3310 s->f = f;
3312 if (s->id1)
3313 err = got_object_get_type(&obj_type, s->repo, s->id1);
3314 else
3315 err = got_object_get_type(&obj_type, s->repo, s->id2);
3316 if (err)
3317 goto done;
3319 switch (obj_type) {
3320 case GOT_OBJ_TYPE_BLOB:
3321 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3322 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3323 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3324 break;
3325 case GOT_OBJ_TYPE_TREE:
3326 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3327 s->id1, s->id2, "", "", s->diff_context,
3328 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3329 break;
3330 case GOT_OBJ_TYPE_COMMIT: {
3331 const struct got_object_id_queue *parent_ids;
3332 struct got_object_qid *pid;
3333 struct got_commit_object *commit2;
3334 struct got_reflist_head *refs;
3336 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3337 if (err)
3338 goto done;
3339 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3340 /* Show commit info if we're diffing to a parent/root commit. */
3341 if (s->id1 == NULL) {
3342 err = write_commit_info(&s->line_offsets, &s->nlines,
3343 s->id2, refs, s->repo, s->f);
3344 if (err)
3345 goto done;
3346 } else {
3347 parent_ids = got_object_commit_get_parent_ids(commit2);
3348 STAILQ_FOREACH(pid, parent_ids, entry) {
3349 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3350 err = write_commit_info(
3351 &s->line_offsets, &s->nlines,
3352 s->id2, refs, s->repo, s->f);
3353 if (err)
3354 goto done;
3355 break;
3359 got_object_commit_close(commit2);
3361 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3362 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3363 s->force_text_diff, s->repo, s->f);
3364 break;
3366 default:
3367 err = got_error(GOT_ERR_OBJ_TYPE);
3368 break;
3370 if (err)
3371 goto done;
3372 done:
3373 if (s->f && fflush(s->f) != 0 && err == NULL)
3374 err = got_error_from_errno("fflush");
3375 return err;
3378 static void
3379 diff_view_indicate_progress(struct tog_view *view)
3381 mvwaddstr(view->window, 0, 0, "diffing...");
3382 update_panels();
3383 doupdate();
3386 static const struct got_error *
3387 search_start_diff_view(struct tog_view *view)
3389 struct tog_diff_view_state *s = &view->state.diff;
3391 s->matched_line = 0;
3392 return NULL;
3395 static const struct got_error *
3396 search_next_diff_view(struct tog_view *view)
3398 struct tog_diff_view_state *s = &view->state.diff;
3399 int lineno;
3400 char *line = NULL;
3401 size_t linesize = 0;
3402 ssize_t linelen;
3404 if (!view->searching) {
3405 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3406 return NULL;
3409 if (s->matched_line) {
3410 if (view->searching == TOG_SEARCH_FORWARD)
3411 lineno = s->matched_line + 1;
3412 else
3413 lineno = s->matched_line - 1;
3414 } else {
3415 if (view->searching == TOG_SEARCH_FORWARD)
3416 lineno = 1;
3417 else
3418 lineno = s->nlines;
3421 while (1) {
3422 off_t offset;
3424 if (lineno <= 0 || lineno > s->nlines) {
3425 if (s->matched_line == 0) {
3426 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3427 break;
3430 if (view->searching == TOG_SEARCH_FORWARD)
3431 lineno = 1;
3432 else
3433 lineno = s->nlines;
3436 offset = s->line_offsets[lineno - 1];
3437 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3438 free(line);
3439 return got_error_from_errno("fseeko");
3441 linelen = getline(&line, &linesize, s->f);
3442 if (linelen != -1 &&
3443 match_line(line, &view->regex, 1, &view->regmatch)) {
3444 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3445 s->matched_line = lineno;
3446 break;
3448 if (view->searching == TOG_SEARCH_FORWARD)
3449 lineno++;
3450 else
3451 lineno--;
3453 free(line);
3455 if (s->matched_line) {
3456 s->first_displayed_line = s->matched_line;
3457 s->selected_line = 1;
3460 return NULL;
3463 static const struct got_error *
3464 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3465 struct got_object_id *id2, const char *label1, const char *label2,
3466 int diff_context, int ignore_whitespace, int force_text_diff,
3467 struct tog_view *log_view, struct got_repository *repo)
3469 const struct got_error *err;
3470 struct tog_diff_view_state *s = &view->state.diff;
3472 if (id1 != NULL && id2 != NULL) {
3473 int type1, type2;
3474 err = got_object_get_type(&type1, repo, id1);
3475 if (err)
3476 return err;
3477 err = got_object_get_type(&type2, repo, id2);
3478 if (err)
3479 return err;
3481 if (type1 != type2)
3482 return got_error(GOT_ERR_OBJ_TYPE);
3484 s->first_displayed_line = 1;
3485 s->last_displayed_line = view->nlines;
3486 s->selected_line = 1;
3487 s->repo = repo;
3488 s->id1 = id1;
3489 s->id2 = id2;
3490 s->label1 = label1;
3491 s->label2 = label2;
3493 if (id1) {
3494 s->id1 = got_object_id_dup(id1);
3495 if (s->id1 == NULL)
3496 return got_error_from_errno("got_object_id_dup");
3497 } else
3498 s->id1 = NULL;
3500 s->id2 = got_object_id_dup(id2);
3501 if (s->id2 == NULL) {
3502 free(s->id1);
3503 s->id1 = NULL;
3504 return got_error_from_errno("got_object_id_dup");
3506 s->f = NULL;
3507 s->first_displayed_line = 1;
3508 s->last_displayed_line = view->nlines;
3509 s->diff_context = diff_context;
3510 s->ignore_whitespace = ignore_whitespace;
3511 s->force_text_diff = force_text_diff;
3512 s->log_view = log_view;
3513 s->repo = repo;
3515 STAILQ_INIT(&s->colors);
3516 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3517 err = add_color(&s->colors,
3518 "^-", TOG_COLOR_DIFF_MINUS,
3519 get_color_value("TOG_COLOR_DIFF_MINUS"));
3520 if (err)
3521 return err;
3522 err = add_color(&s->colors, "^\\+",
3523 TOG_COLOR_DIFF_PLUS,
3524 get_color_value("TOG_COLOR_DIFF_PLUS"));
3525 if (err) {
3526 free_colors(&s->colors);
3527 return err;
3529 err = add_color(&s->colors,
3530 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3531 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3532 if (err) {
3533 free_colors(&s->colors);
3534 return err;
3537 err = add_color(&s->colors,
3538 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3539 TOG_COLOR_DIFF_META,
3540 get_color_value("TOG_COLOR_DIFF_META"));
3541 if (err) {
3542 free_colors(&s->colors);
3543 return err;
3546 err = add_color(&s->colors,
3547 "^(from|via): ", TOG_COLOR_AUTHOR,
3548 get_color_value("TOG_COLOR_AUTHOR"));
3549 if (err) {
3550 free_colors(&s->colors);
3551 return err;
3554 err = add_color(&s->colors,
3555 "^date: ", TOG_COLOR_DATE,
3556 get_color_value("TOG_COLOR_DATE"));
3557 if (err) {
3558 free_colors(&s->colors);
3559 return err;
3563 if (log_view && view_is_splitscreen(view))
3564 show_log_view(log_view); /* draw vborder */
3565 diff_view_indicate_progress(view);
3567 s->line_offsets = NULL;
3568 s->nlines = 0;
3569 err = create_diff(s);
3570 if (err) {
3571 free(s->id1);
3572 s->id1 = NULL;
3573 free(s->id2);
3574 s->id2 = NULL;
3575 free_colors(&s->colors);
3576 return err;
3579 view->show = show_diff_view;
3580 view->input = input_diff_view;
3581 view->close = close_diff_view;
3582 view->search_start = search_start_diff_view;
3583 view->search_next = search_next_diff_view;
3585 return NULL;
3588 static const struct got_error *
3589 close_diff_view(struct tog_view *view)
3591 const struct got_error *err = NULL;
3592 struct tog_diff_view_state *s = &view->state.diff;
3594 free(s->id1);
3595 s->id1 = NULL;
3596 free(s->id2);
3597 s->id2 = NULL;
3598 if (s->f && fclose(s->f) == EOF)
3599 err = got_error_from_errno("fclose");
3600 free_colors(&s->colors);
3601 free(s->line_offsets);
3602 s->line_offsets = NULL;
3603 s->nlines = 0;
3604 return err;
3607 static const struct got_error *
3608 show_diff_view(struct tog_view *view)
3610 const struct got_error *err;
3611 struct tog_diff_view_state *s = &view->state.diff;
3612 char *id_str1 = NULL, *id_str2, *header;
3613 const char *label1, *label2;
3615 if (s->id1) {
3616 err = got_object_id_str(&id_str1, s->id1);
3617 if (err)
3618 return err;
3619 label1 = s->label1 ? : id_str1;
3620 } else
3621 label1 = "/dev/null";
3623 err = got_object_id_str(&id_str2, s->id2);
3624 if (err)
3625 return err;
3626 label2 = s->label2 ? : id_str2;
3628 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3629 err = got_error_from_errno("asprintf");
3630 free(id_str1);
3631 free(id_str2);
3632 return err;
3634 free(id_str1);
3635 free(id_str2);
3637 err = draw_file(view, header);
3638 free(header);
3639 return err;
3642 static const struct got_error *
3643 set_selected_commit(struct tog_diff_view_state *s,
3644 struct commit_queue_entry *entry)
3646 const struct got_error *err;
3647 const struct got_object_id_queue *parent_ids;
3648 struct got_commit_object *selected_commit;
3649 struct got_object_qid *pid;
3651 free(s->id2);
3652 s->id2 = got_object_id_dup(entry->id);
3653 if (s->id2 == NULL)
3654 return got_error_from_errno("got_object_id_dup");
3656 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3657 if (err)
3658 return err;
3659 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3660 free(s->id1);
3661 pid = STAILQ_FIRST(parent_ids);
3662 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3663 got_object_commit_close(selected_commit);
3664 return NULL;
3667 static const struct got_error *
3668 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3670 const struct got_error *err = NULL;
3671 struct tog_diff_view_state *s = &view->state.diff;
3672 struct tog_log_view_state *ls;
3673 struct commit_queue_entry *old_selected_entry;
3674 char *line = NULL;
3675 size_t linesize = 0;
3676 ssize_t linelen;
3677 int i;
3679 switch (ch) {
3680 case 'a':
3681 case 'w':
3682 if (ch == 'a')
3683 s->force_text_diff = !s->force_text_diff;
3684 if (ch == 'w')
3685 s->ignore_whitespace = !s->ignore_whitespace;
3686 wclear(view->window);
3687 s->first_displayed_line = 1;
3688 s->last_displayed_line = view->nlines;
3689 diff_view_indicate_progress(view);
3690 err = create_diff(s);
3691 break;
3692 case 'g':
3693 case KEY_HOME:
3694 s->first_displayed_line = 1;
3695 break;
3696 case 'G':
3697 case KEY_END:
3698 if (s->eof)
3699 break;
3701 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3702 s->eof = 1;
3703 break;
3704 case 'k':
3705 case KEY_UP:
3706 if (s->first_displayed_line > 1)
3707 s->first_displayed_line--;
3708 break;
3709 case KEY_PPAGE:
3710 case CTRL('b'):
3711 if (s->first_displayed_line == 1)
3712 break;
3713 i = 0;
3714 while (i++ < view->nlines - 1 &&
3715 s->first_displayed_line > 1)
3716 s->first_displayed_line--;
3717 break;
3718 case 'j':
3719 case KEY_DOWN:
3720 if (!s->eof)
3721 s->first_displayed_line++;
3722 break;
3723 case KEY_NPAGE:
3724 case CTRL('f'):
3725 case ' ':
3726 if (s->eof)
3727 break;
3728 i = 0;
3729 while (!s->eof && i++ < view->nlines - 1) {
3730 linelen = getline(&line, &linesize, s->f);
3731 s->first_displayed_line++;
3732 if (linelen == -1) {
3733 if (feof(s->f)) {
3734 s->eof = 1;
3735 } else
3736 err = got_ferror(s->f, GOT_ERR_IO);
3737 break;
3740 free(line);
3741 break;
3742 case '[':
3743 if (s->diff_context > 0) {
3744 s->diff_context--;
3745 diff_view_indicate_progress(view);
3746 err = create_diff(s);
3747 if (s->first_displayed_line + view->nlines - 1 >
3748 s->nlines) {
3749 s->first_displayed_line = 1;
3750 s->last_displayed_line = view->nlines;
3753 break;
3754 case ']':
3755 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3756 s->diff_context++;
3757 diff_view_indicate_progress(view);
3758 err = create_diff(s);
3760 break;
3761 case '<':
3762 case ',':
3763 if (s->log_view == NULL)
3764 break;
3765 ls = &s->log_view->state.log;
3766 old_selected_entry = ls->selected_entry;
3768 err = input_log_view(NULL, s->log_view, KEY_UP);
3769 if (err)
3770 break;
3772 if (old_selected_entry == ls->selected_entry)
3773 break;
3775 err = set_selected_commit(s, ls->selected_entry);
3776 if (err)
3777 break;
3779 s->first_displayed_line = 1;
3780 s->last_displayed_line = view->nlines;
3782 diff_view_indicate_progress(view);
3783 err = create_diff(s);
3784 break;
3785 case '>':
3786 case '.':
3787 if (s->log_view == NULL)
3788 break;
3789 ls = &s->log_view->state.log;
3790 old_selected_entry = ls->selected_entry;
3792 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3793 if (err)
3794 break;
3796 if (old_selected_entry == ls->selected_entry)
3797 break;
3799 err = set_selected_commit(s, ls->selected_entry);
3800 if (err)
3801 break;
3803 s->first_displayed_line = 1;
3804 s->last_displayed_line = view->nlines;
3806 diff_view_indicate_progress(view);
3807 err = create_diff(s);
3808 break;
3809 default:
3810 break;
3813 return err;
3816 static const struct got_error *
3817 cmd_diff(int argc, char *argv[])
3819 const struct got_error *error = NULL;
3820 struct got_repository *repo = NULL;
3821 struct got_worktree *worktree = NULL;
3822 struct got_object_id *id1 = NULL, *id2 = NULL;
3823 char *repo_path = NULL, *cwd = NULL;
3824 char *id_str1 = NULL, *id_str2 = NULL;
3825 char *label1 = NULL, *label2 = NULL;
3826 int diff_context = 3, ignore_whitespace = 0;
3827 int ch, force_text_diff = 0;
3828 const char *errstr;
3829 struct tog_view *view;
3831 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3832 switch (ch) {
3833 case 'a':
3834 force_text_diff = 1;
3835 break;
3836 case 'C':
3837 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3838 &errstr);
3839 if (errstr != NULL)
3840 err(1, "-C option %s", errstr);
3841 break;
3842 case 'r':
3843 repo_path = realpath(optarg, NULL);
3844 if (repo_path == NULL)
3845 return got_error_from_errno2("realpath",
3846 optarg);
3847 got_path_strip_trailing_slashes(repo_path);
3848 break;
3849 case 'w':
3850 ignore_whitespace = 1;
3851 break;
3852 default:
3853 usage_diff();
3854 /* NOTREACHED */
3858 argc -= optind;
3859 argv += optind;
3861 if (argc == 0) {
3862 usage_diff(); /* TODO show local worktree changes */
3863 } else if (argc == 2) {
3864 id_str1 = argv[0];
3865 id_str2 = argv[1];
3866 } else
3867 usage_diff();
3869 if (repo_path == NULL) {
3870 cwd = getcwd(NULL, 0);
3871 if (cwd == NULL)
3872 return got_error_from_errno("getcwd");
3873 error = got_worktree_open(&worktree, cwd);
3874 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3875 goto done;
3876 if (worktree)
3877 repo_path =
3878 strdup(got_worktree_get_repo_path(worktree));
3879 else
3880 repo_path = strdup(cwd);
3881 if (repo_path == NULL) {
3882 error = got_error_from_errno("strdup");
3883 goto done;
3887 error = got_repo_open(&repo, repo_path, NULL);
3888 if (error)
3889 goto done;
3891 init_curses();
3893 error = apply_unveil(got_repo_get_path(repo), NULL);
3894 if (error)
3895 goto done;
3897 error = tog_load_refs(repo);
3898 if (error)
3899 goto done;
3901 error = got_repo_match_object_id(&id1, &label1, id_str1,
3902 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3903 if (error)
3904 goto done;
3906 error = got_repo_match_object_id(&id2, &label2, id_str2,
3907 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3908 if (error)
3909 goto done;
3911 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3912 if (view == NULL) {
3913 error = got_error_from_errno("view_open");
3914 goto done;
3916 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3917 ignore_whitespace, force_text_diff, NULL, repo);
3918 if (error)
3919 goto done;
3920 error = view_loop(view);
3921 done:
3922 free(label1);
3923 free(label2);
3924 free(repo_path);
3925 free(cwd);
3926 if (repo) {
3927 const struct got_error *close_err = got_repo_close(repo);
3928 if (error == NULL)
3929 error = close_err;
3931 if (worktree)
3932 got_worktree_close(worktree);
3933 tog_free_refs();
3934 return error;
3937 __dead static void
3938 usage_blame(void)
3940 endwin();
3941 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3942 getprogname());
3943 exit(1);
3946 struct tog_blame_line {
3947 int annotated;
3948 struct got_object_id *id;
3951 static const struct got_error *
3952 draw_blame(struct tog_view *view)
3954 struct tog_blame_view_state *s = &view->state.blame;
3955 struct tog_blame *blame = &s->blame;
3956 regmatch_t *regmatch = &view->regmatch;
3957 const struct got_error *err;
3958 int lineno = 0, nprinted = 0;
3959 char *line = NULL;
3960 size_t linesize = 0;
3961 ssize_t linelen;
3962 wchar_t *wline;
3963 int width;
3964 struct tog_blame_line *blame_line;
3965 struct got_object_id *prev_id = NULL;
3966 char *id_str;
3967 struct tog_color *tc;
3969 err = got_object_id_str(&id_str, s->blamed_commit->id);
3970 if (err)
3971 return err;
3973 rewind(blame->f);
3974 werase(view->window);
3976 if (asprintf(&line, "commit %s", id_str) == -1) {
3977 err = got_error_from_errno("asprintf");
3978 free(id_str);
3979 return err;
3982 err = format_line(&wline, &width, line, view->ncols, 0);
3983 free(line);
3984 line = NULL;
3985 if (err)
3986 return err;
3987 if (view_needs_focus_indication(view))
3988 wstandout(view->window);
3989 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3990 if (tc)
3991 wattr_on(view->window,
3992 COLOR_PAIR(tc->colorpair), NULL);
3993 waddwstr(view->window, wline);
3994 if (tc)
3995 wattr_off(view->window,
3996 COLOR_PAIR(tc->colorpair), NULL);
3997 if (view_needs_focus_indication(view))
3998 wstandend(view->window);
3999 free(wline);
4000 wline = NULL;
4001 if (width < view->ncols - 1)
4002 waddch(view->window, '\n');
4004 if (asprintf(&line, "[%d/%d] %s%s",
4005 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4006 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4007 free(id_str);
4008 return got_error_from_errno("asprintf");
4010 free(id_str);
4011 err = format_line(&wline, &width, line, view->ncols, 0);
4012 free(line);
4013 line = NULL;
4014 if (err)
4015 return err;
4016 waddwstr(view->window, wline);
4017 free(wline);
4018 wline = NULL;
4019 if (width < view->ncols - 1)
4020 waddch(view->window, '\n');
4022 s->eof = 0;
4023 while (nprinted < view->nlines - 2) {
4024 linelen = getline(&line, &linesize, blame->f);
4025 if (linelen == -1) {
4026 if (feof(blame->f)) {
4027 s->eof = 1;
4028 break;
4030 free(line);
4031 return got_ferror(blame->f, GOT_ERR_IO);
4033 if (++lineno < s->first_displayed_line)
4034 continue;
4036 if (view->focussed && nprinted == s->selected_line - 1)
4037 wstandout(view->window);
4039 if (blame->nlines > 0) {
4040 blame_line = &blame->lines[lineno - 1];
4041 if (blame_line->annotated && prev_id &&
4042 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4043 !(view->focussed &&
4044 nprinted == s->selected_line - 1)) {
4045 waddstr(view->window, " ");
4046 } else if (blame_line->annotated) {
4047 char *id_str;
4048 err = got_object_id_str(&id_str, blame_line->id);
4049 if (err) {
4050 free(line);
4051 return err;
4053 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4054 if (tc)
4055 wattr_on(view->window,
4056 COLOR_PAIR(tc->colorpair), NULL);
4057 wprintw(view->window, "%.8s", id_str);
4058 if (tc)
4059 wattr_off(view->window,
4060 COLOR_PAIR(tc->colorpair), NULL);
4061 free(id_str);
4062 prev_id = blame_line->id;
4063 } else {
4064 waddstr(view->window, "........");
4065 prev_id = NULL;
4067 } else {
4068 waddstr(view->window, "........");
4069 prev_id = NULL;
4072 if (view->focussed && nprinted == s->selected_line - 1)
4073 wstandend(view->window);
4074 waddstr(view->window, " ");
4076 if (view->ncols <= 9) {
4077 width = 9;
4078 wline = wcsdup(L"");
4079 if (wline == NULL) {
4080 err = got_error_from_errno("wcsdup");
4081 free(line);
4082 return err;
4084 } else if (s->first_displayed_line + nprinted ==
4085 s->matched_line &&
4086 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4087 err = add_matched_line(&width, line, view->ncols - 9, 9,
4088 view->window, regmatch);
4089 if (err) {
4090 free(line);
4091 return err;
4093 width += 9;
4094 } else {
4095 err = format_line(&wline, &width, line,
4096 view->ncols - 9, 9);
4097 waddwstr(view->window, wline);
4098 free(wline);
4099 wline = NULL;
4100 width += 9;
4103 if (width <= view->ncols - 1)
4104 waddch(view->window, '\n');
4105 if (++nprinted == 1)
4106 s->first_displayed_line = lineno;
4108 free(line);
4109 s->last_displayed_line = lineno;
4111 view_vborder(view);
4113 return NULL;
4116 static const struct got_error *
4117 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4119 const struct got_error *err = NULL;
4120 struct tog_blame_cb_args *a = arg;
4121 struct tog_blame_line *line;
4122 int errcode;
4124 if (nlines != a->nlines ||
4125 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4126 return got_error(GOT_ERR_RANGE);
4128 errcode = pthread_mutex_lock(&tog_mutex);
4129 if (errcode)
4130 return got_error_set_errno(errcode, "pthread_mutex_lock");
4132 if (*a->quit) { /* user has quit the blame view */
4133 err = got_error(GOT_ERR_ITER_COMPLETED);
4134 goto done;
4137 if (lineno == -1)
4138 goto done; /* no change in this commit */
4140 line = &a->lines[lineno - 1];
4141 if (line->annotated)
4142 goto done;
4144 line->id = got_object_id_dup(id);
4145 if (line->id == NULL) {
4146 err = got_error_from_errno("got_object_id_dup");
4147 goto done;
4149 line->annotated = 1;
4150 done:
4151 errcode = pthread_mutex_unlock(&tog_mutex);
4152 if (errcode)
4153 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4154 return err;
4157 static void *
4158 blame_thread(void *arg)
4160 const struct got_error *err, *close_err;
4161 struct tog_blame_thread_args *ta = arg;
4162 struct tog_blame_cb_args *a = ta->cb_args;
4163 int errcode;
4165 err = block_signals_used_by_main_thread();
4166 if (err)
4167 return (void *)err;
4169 err = got_blame(ta->path, a->commit_id, ta->repo,
4170 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4171 if (err && err->code == GOT_ERR_CANCELLED)
4172 err = NULL;
4174 errcode = pthread_mutex_lock(&tog_mutex);
4175 if (errcode)
4176 return (void *)got_error_set_errno(errcode,
4177 "pthread_mutex_lock");
4179 close_err = got_repo_close(ta->repo);
4180 if (err == NULL)
4181 err = close_err;
4182 ta->repo = NULL;
4183 *ta->complete = 1;
4185 errcode = pthread_mutex_unlock(&tog_mutex);
4186 if (errcode && err == NULL)
4187 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4189 return (void *)err;
4192 static struct got_object_id *
4193 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4194 int first_displayed_line, int selected_line)
4196 struct tog_blame_line *line;
4198 if (nlines <= 0)
4199 return NULL;
4201 line = &lines[first_displayed_line - 1 + selected_line - 1];
4202 if (!line->annotated)
4203 return NULL;
4205 return line->id;
4208 static const struct got_error *
4209 stop_blame(struct tog_blame *blame)
4211 const struct got_error *err = NULL;
4212 int i;
4214 if (blame->thread) {
4215 int errcode;
4216 errcode = pthread_mutex_unlock(&tog_mutex);
4217 if (errcode)
4218 return got_error_set_errno(errcode,
4219 "pthread_mutex_unlock");
4220 errcode = pthread_join(blame->thread, (void **)&err);
4221 if (errcode)
4222 return got_error_set_errno(errcode, "pthread_join");
4223 errcode = pthread_mutex_lock(&tog_mutex);
4224 if (errcode)
4225 return got_error_set_errno(errcode,
4226 "pthread_mutex_lock");
4227 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4228 err = NULL;
4229 blame->thread = 0; //NULL;
4231 if (blame->thread_args.repo) {
4232 const struct got_error *close_err;
4233 close_err = got_repo_close(blame->thread_args.repo);
4234 if (err == NULL)
4235 err = close_err;
4236 blame->thread_args.repo = NULL;
4238 if (blame->f) {
4239 if (fclose(blame->f) == EOF && err == NULL)
4240 err = got_error_from_errno("fclose");
4241 blame->f = NULL;
4243 if (blame->lines) {
4244 for (i = 0; i < blame->nlines; i++)
4245 free(blame->lines[i].id);
4246 free(blame->lines);
4247 blame->lines = NULL;
4249 free(blame->cb_args.commit_id);
4250 blame->cb_args.commit_id = NULL;
4252 return err;
4255 static const struct got_error *
4256 cancel_blame_view(void *arg)
4258 const struct got_error *err = NULL;
4259 int *done = arg;
4260 int errcode;
4262 errcode = pthread_mutex_lock(&tog_mutex);
4263 if (errcode)
4264 return got_error_set_errno(errcode,
4265 "pthread_mutex_unlock");
4267 if (*done)
4268 err = got_error(GOT_ERR_CANCELLED);
4270 errcode = pthread_mutex_unlock(&tog_mutex);
4271 if (errcode)
4272 return got_error_set_errno(errcode,
4273 "pthread_mutex_lock");
4275 return err;
4278 static const struct got_error *
4279 run_blame(struct tog_view *view)
4281 struct tog_blame_view_state *s = &view->state.blame;
4282 struct tog_blame *blame = &s->blame;
4283 const struct got_error *err = NULL;
4284 struct got_blob_object *blob = NULL;
4285 struct got_repository *thread_repo = NULL;
4286 struct got_object_id *obj_id = NULL;
4287 int obj_type;
4289 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4290 s->path);
4291 if (err)
4292 return err;
4294 err = got_object_get_type(&obj_type, s->repo, obj_id);
4295 if (err)
4296 goto done;
4298 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4299 err = got_error(GOT_ERR_OBJ_TYPE);
4300 goto done;
4303 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4304 if (err)
4305 goto done;
4306 blame->f = got_opentemp();
4307 if (blame->f == NULL) {
4308 err = got_error_from_errno("got_opentemp");
4309 goto done;
4311 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4312 &blame->line_offsets, blame->f, blob);
4313 if (err)
4314 goto done;
4315 if (blame->nlines == 0) {
4316 s->blame_complete = 1;
4317 goto done;
4320 /* Don't include \n at EOF in the blame line count. */
4321 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4322 blame->nlines--;
4324 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4325 if (blame->lines == NULL) {
4326 err = got_error_from_errno("calloc");
4327 goto done;
4330 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4331 if (err)
4332 goto done;
4334 blame->cb_args.view = view;
4335 blame->cb_args.lines = blame->lines;
4336 blame->cb_args.nlines = blame->nlines;
4337 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4338 if (blame->cb_args.commit_id == NULL) {
4339 err = got_error_from_errno("got_object_id_dup");
4340 goto done;
4342 blame->cb_args.quit = &s->done;
4344 blame->thread_args.path = s->path;
4345 blame->thread_args.repo = thread_repo;
4346 blame->thread_args.cb_args = &blame->cb_args;
4347 blame->thread_args.complete = &s->blame_complete;
4348 blame->thread_args.cancel_cb = cancel_blame_view;
4349 blame->thread_args.cancel_arg = &s->done;
4350 s->blame_complete = 0;
4352 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4353 s->first_displayed_line = 1;
4354 s->last_displayed_line = view->nlines;
4355 s->selected_line = 1;
4358 done:
4359 if (blob)
4360 got_object_blob_close(blob);
4361 free(obj_id);
4362 if (err)
4363 stop_blame(blame);
4364 return err;
4367 static const struct got_error *
4368 open_blame_view(struct tog_view *view, char *path,
4369 struct got_object_id *commit_id, struct got_repository *repo)
4371 const struct got_error *err = NULL;
4372 struct tog_blame_view_state *s = &view->state.blame;
4374 STAILQ_INIT(&s->blamed_commits);
4376 s->path = strdup(path);
4377 if (s->path == NULL)
4378 return got_error_from_errno("strdup");
4380 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4381 if (err) {
4382 free(s->path);
4383 return err;
4386 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4387 s->first_displayed_line = 1;
4388 s->last_displayed_line = view->nlines;
4389 s->selected_line = 1;
4390 s->blame_complete = 0;
4391 s->repo = repo;
4392 s->commit_id = commit_id;
4393 memset(&s->blame, 0, sizeof(s->blame));
4395 STAILQ_INIT(&s->colors);
4396 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4397 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4398 get_color_value("TOG_COLOR_COMMIT"));
4399 if (err)
4400 return err;
4403 view->show = show_blame_view;
4404 view->input = input_blame_view;
4405 view->close = close_blame_view;
4406 view->search_start = search_start_blame_view;
4407 view->search_next = search_next_blame_view;
4409 return run_blame(view);
4412 static const struct got_error *
4413 close_blame_view(struct tog_view *view)
4415 const struct got_error *err = NULL;
4416 struct tog_blame_view_state *s = &view->state.blame;
4418 if (s->blame.thread)
4419 err = stop_blame(&s->blame);
4421 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4422 struct got_object_qid *blamed_commit;
4423 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4424 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4425 got_object_qid_free(blamed_commit);
4428 free(s->path);
4429 free_colors(&s->colors);
4431 return err;
4434 static const struct got_error *
4435 search_start_blame_view(struct tog_view *view)
4437 struct tog_blame_view_state *s = &view->state.blame;
4439 s->matched_line = 0;
4440 return NULL;
4443 static const struct got_error *
4444 search_next_blame_view(struct tog_view *view)
4446 struct tog_blame_view_state *s = &view->state.blame;
4447 int lineno;
4448 char *line = NULL;
4449 size_t linesize = 0;
4450 ssize_t linelen;
4452 if (!view->searching) {
4453 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4454 return NULL;
4457 if (s->matched_line) {
4458 if (view->searching == TOG_SEARCH_FORWARD)
4459 lineno = s->matched_line + 1;
4460 else
4461 lineno = s->matched_line - 1;
4462 } else {
4463 if (view->searching == TOG_SEARCH_FORWARD)
4464 lineno = 1;
4465 else
4466 lineno = s->blame.nlines;
4469 while (1) {
4470 off_t offset;
4472 if (lineno <= 0 || lineno > s->blame.nlines) {
4473 if (s->matched_line == 0) {
4474 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4475 break;
4478 if (view->searching == TOG_SEARCH_FORWARD)
4479 lineno = 1;
4480 else
4481 lineno = s->blame.nlines;
4484 offset = s->blame.line_offsets[lineno - 1];
4485 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4486 free(line);
4487 return got_error_from_errno("fseeko");
4489 linelen = getline(&line, &linesize, s->blame.f);
4490 if (linelen != -1 &&
4491 match_line(line, &view->regex, 1, &view->regmatch)) {
4492 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4493 s->matched_line = lineno;
4494 break;
4496 if (view->searching == TOG_SEARCH_FORWARD)
4497 lineno++;
4498 else
4499 lineno--;
4501 free(line);
4503 if (s->matched_line) {
4504 s->first_displayed_line = s->matched_line;
4505 s->selected_line = 1;
4508 return NULL;
4511 static const struct got_error *
4512 show_blame_view(struct tog_view *view)
4514 const struct got_error *err = NULL;
4515 struct tog_blame_view_state *s = &view->state.blame;
4516 int errcode;
4518 if (s->blame.thread == 0 && !s->blame_complete) {
4519 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4520 &s->blame.thread_args);
4521 if (errcode)
4522 return got_error_set_errno(errcode, "pthread_create");
4524 halfdelay(1); /* fast refresh while annotating */
4527 if (s->blame_complete)
4528 halfdelay(10); /* disable fast refresh */
4530 err = draw_blame(view);
4532 view_vborder(view);
4533 return err;
4536 static const struct got_error *
4537 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4539 const struct got_error *err = NULL, *thread_err = NULL;
4540 struct tog_view *diff_view;
4541 struct tog_blame_view_state *s = &view->state.blame;
4542 int begin_x = 0;
4544 switch (ch) {
4545 case 'q':
4546 s->done = 1;
4547 break;
4548 case 'g':
4549 case KEY_HOME:
4550 s->selected_line = 1;
4551 s->first_displayed_line = 1;
4552 break;
4553 case 'G':
4554 case KEY_END:
4555 if (s->blame.nlines < view->nlines - 2) {
4556 s->selected_line = s->blame.nlines;
4557 s->first_displayed_line = 1;
4558 } else {
4559 s->selected_line = view->nlines - 2;
4560 s->first_displayed_line = s->blame.nlines -
4561 (view->nlines - 3);
4563 break;
4564 case 'k':
4565 case KEY_UP:
4566 if (s->selected_line > 1)
4567 s->selected_line--;
4568 else if (s->selected_line == 1 &&
4569 s->first_displayed_line > 1)
4570 s->first_displayed_line--;
4571 break;
4572 case KEY_PPAGE:
4573 case CTRL('b'):
4574 if (s->first_displayed_line == 1) {
4575 s->selected_line = 1;
4576 break;
4578 if (s->first_displayed_line > view->nlines - 2)
4579 s->first_displayed_line -=
4580 (view->nlines - 2);
4581 else
4582 s->first_displayed_line = 1;
4583 break;
4584 case 'j':
4585 case KEY_DOWN:
4586 if (s->selected_line < view->nlines - 2 &&
4587 s->first_displayed_line +
4588 s->selected_line <= s->blame.nlines)
4589 s->selected_line++;
4590 else if (s->last_displayed_line <
4591 s->blame.nlines)
4592 s->first_displayed_line++;
4593 break;
4594 case 'b':
4595 case 'p': {
4596 struct got_object_id *id = NULL;
4597 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4598 s->first_displayed_line, s->selected_line);
4599 if (id == NULL)
4600 break;
4601 if (ch == 'p') {
4602 struct got_commit_object *commit;
4603 struct got_object_qid *pid;
4604 struct got_object_id *blob_id = NULL;
4605 int obj_type;
4606 err = got_object_open_as_commit(&commit,
4607 s->repo, id);
4608 if (err)
4609 break;
4610 pid = STAILQ_FIRST(
4611 got_object_commit_get_parent_ids(commit));
4612 if (pid == NULL) {
4613 got_object_commit_close(commit);
4614 break;
4616 /* Check if path history ends here. */
4617 err = got_object_id_by_path(&blob_id, s->repo,
4618 pid->id, s->path);
4619 if (err) {
4620 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4621 err = NULL;
4622 got_object_commit_close(commit);
4623 break;
4625 err = got_object_get_type(&obj_type, s->repo,
4626 blob_id);
4627 free(blob_id);
4628 /* Can't blame non-blob type objects. */
4629 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4630 got_object_commit_close(commit);
4631 break;
4633 err = got_object_qid_alloc(&s->blamed_commit,
4634 pid->id);
4635 got_object_commit_close(commit);
4636 } else {
4637 if (got_object_id_cmp(id,
4638 s->blamed_commit->id) == 0)
4639 break;
4640 err = got_object_qid_alloc(&s->blamed_commit,
4641 id);
4643 if (err)
4644 break;
4645 s->done = 1;
4646 thread_err = stop_blame(&s->blame);
4647 s->done = 0;
4648 if (thread_err)
4649 break;
4650 STAILQ_INSERT_HEAD(&s->blamed_commits,
4651 s->blamed_commit, entry);
4652 err = run_blame(view);
4653 if (err)
4654 break;
4655 break;
4657 case 'B': {
4658 struct got_object_qid *first;
4659 first = STAILQ_FIRST(&s->blamed_commits);
4660 if (!got_object_id_cmp(first->id, s->commit_id))
4661 break;
4662 s->done = 1;
4663 thread_err = stop_blame(&s->blame);
4664 s->done = 0;
4665 if (thread_err)
4666 break;
4667 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4668 got_object_qid_free(s->blamed_commit);
4669 s->blamed_commit =
4670 STAILQ_FIRST(&s->blamed_commits);
4671 err = run_blame(view);
4672 if (err)
4673 break;
4674 break;
4676 case KEY_ENTER:
4677 case '\r': {
4678 struct got_object_id *id = NULL;
4679 struct got_object_qid *pid;
4680 struct got_commit_object *commit = NULL;
4681 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4682 s->first_displayed_line, s->selected_line);
4683 if (id == NULL)
4684 break;
4685 err = got_object_open_as_commit(&commit, s->repo, id);
4686 if (err)
4687 break;
4688 pid = STAILQ_FIRST(
4689 got_object_commit_get_parent_ids(commit));
4690 if (view_is_parent_view(view))
4691 begin_x = view_split_begin_x(view->begin_x);
4692 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4693 if (diff_view == NULL) {
4694 got_object_commit_close(commit);
4695 err = got_error_from_errno("view_open");
4696 break;
4698 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4699 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4700 got_object_commit_close(commit);
4701 if (err) {
4702 view_close(diff_view);
4703 break;
4705 view->focussed = 0;
4706 diff_view->focussed = 1;
4707 if (view_is_parent_view(view)) {
4708 err = view_close_child(view);
4709 if (err)
4710 break;
4711 view_set_child(view, diff_view);
4712 view->focus_child = 1;
4713 } else
4714 *new_view = diff_view;
4715 if (err)
4716 break;
4717 break;
4719 case KEY_NPAGE:
4720 case CTRL('f'):
4721 case ' ':
4722 if (s->last_displayed_line >= s->blame.nlines &&
4723 s->selected_line >= MIN(s->blame.nlines,
4724 view->nlines - 2)) {
4725 break;
4727 if (s->last_displayed_line >= s->blame.nlines &&
4728 s->selected_line < view->nlines - 2) {
4729 s->selected_line = MIN(s->blame.nlines,
4730 view->nlines - 2);
4731 break;
4733 if (s->last_displayed_line + view->nlines - 2
4734 <= s->blame.nlines)
4735 s->first_displayed_line +=
4736 view->nlines - 2;
4737 else
4738 s->first_displayed_line =
4739 s->blame.nlines -
4740 (view->nlines - 3);
4741 break;
4742 case KEY_RESIZE:
4743 if (s->selected_line > view->nlines - 2) {
4744 s->selected_line = MIN(s->blame.nlines,
4745 view->nlines - 2);
4747 break;
4748 default:
4749 break;
4751 return thread_err ? thread_err : err;
4754 static const struct got_error *
4755 cmd_blame(int argc, char *argv[])
4757 const struct got_error *error;
4758 struct got_repository *repo = NULL;
4759 struct got_worktree *worktree = NULL;
4760 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4761 char *link_target = NULL;
4762 struct got_object_id *commit_id = NULL;
4763 char *commit_id_str = NULL;
4764 int ch;
4765 struct tog_view *view;
4767 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4768 switch (ch) {
4769 case 'c':
4770 commit_id_str = optarg;
4771 break;
4772 case 'r':
4773 repo_path = realpath(optarg, NULL);
4774 if (repo_path == NULL)
4775 return got_error_from_errno2("realpath",
4776 optarg);
4777 break;
4778 default:
4779 usage_blame();
4780 /* NOTREACHED */
4784 argc -= optind;
4785 argv += optind;
4787 if (argc != 1)
4788 usage_blame();
4790 if (repo_path == NULL) {
4791 cwd = getcwd(NULL, 0);
4792 if (cwd == NULL)
4793 return got_error_from_errno("getcwd");
4794 error = got_worktree_open(&worktree, cwd);
4795 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4796 goto done;
4797 if (worktree)
4798 repo_path =
4799 strdup(got_worktree_get_repo_path(worktree));
4800 else
4801 repo_path = strdup(cwd);
4802 if (repo_path == NULL) {
4803 error = got_error_from_errno("strdup");
4804 goto done;
4808 error = got_repo_open(&repo, repo_path, NULL);
4809 if (error != NULL)
4810 goto done;
4812 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4813 worktree);
4814 if (error)
4815 goto done;
4817 init_curses();
4819 error = apply_unveil(got_repo_get_path(repo), NULL);
4820 if (error)
4821 goto done;
4823 error = tog_load_refs(repo);
4824 if (error)
4825 goto done;
4827 if (commit_id_str == NULL) {
4828 struct got_reference *head_ref;
4829 error = got_ref_open(&head_ref, repo, worktree ?
4830 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4831 if (error != NULL)
4832 goto done;
4833 error = got_ref_resolve(&commit_id, repo, head_ref);
4834 got_ref_close(head_ref);
4835 } else {
4836 error = got_repo_match_object_id(&commit_id, NULL,
4837 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4839 if (error != NULL)
4840 goto done;
4842 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4843 if (view == NULL) {
4844 error = got_error_from_errno("view_open");
4845 goto done;
4848 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4849 commit_id, repo);
4850 if (error)
4851 goto done;
4853 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4854 commit_id, repo);
4855 if (error)
4856 goto done;
4857 if (worktree) {
4858 /* Release work tree lock. */
4859 got_worktree_close(worktree);
4860 worktree = NULL;
4862 error = view_loop(view);
4863 done:
4864 free(repo_path);
4865 free(in_repo_path);
4866 free(link_target);
4867 free(cwd);
4868 free(commit_id);
4869 if (worktree)
4870 got_worktree_close(worktree);
4871 if (repo) {
4872 const struct got_error *close_err = got_repo_close(repo);
4873 if (error == NULL)
4874 error = close_err;
4876 tog_free_refs();
4877 return error;
4880 static const struct got_error *
4881 draw_tree_entries(struct tog_view *view, const char *parent_path)
4883 struct tog_tree_view_state *s = &view->state.tree;
4884 const struct got_error *err = NULL;
4885 struct got_tree_entry *te;
4886 wchar_t *wline;
4887 struct tog_color *tc;
4888 int width, n, i, nentries;
4889 int limit = view->nlines;
4891 s->ndisplayed = 0;
4893 werase(view->window);
4895 if (limit == 0)
4896 return NULL;
4898 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4899 if (err)
4900 return err;
4901 if (view_needs_focus_indication(view))
4902 wstandout(view->window);
4903 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4904 if (tc)
4905 wattr_on(view->window,
4906 COLOR_PAIR(tc->colorpair), NULL);
4907 waddwstr(view->window, wline);
4908 if (tc)
4909 wattr_off(view->window,
4910 COLOR_PAIR(tc->colorpair), NULL);
4911 if (view_needs_focus_indication(view))
4912 wstandend(view->window);
4913 free(wline);
4914 wline = NULL;
4915 if (width < view->ncols - 1)
4916 waddch(view->window, '\n');
4917 if (--limit <= 0)
4918 return NULL;
4919 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4920 if (err)
4921 return err;
4922 waddwstr(view->window, wline);
4923 free(wline);
4924 wline = NULL;
4925 if (width < view->ncols - 1)
4926 waddch(view->window, '\n');
4927 if (--limit <= 0)
4928 return NULL;
4929 waddch(view->window, '\n');
4930 if (--limit <= 0)
4931 return NULL;
4933 if (s->first_displayed_entry == NULL) {
4934 te = got_object_tree_get_first_entry(s->tree);
4935 if (s->selected == 0) {
4936 if (view->focussed)
4937 wstandout(view->window);
4938 s->selected_entry = NULL;
4940 waddstr(view->window, " ..\n"); /* parent directory */
4941 if (s->selected == 0 && view->focussed)
4942 wstandend(view->window);
4943 s->ndisplayed++;
4944 if (--limit <= 0)
4945 return NULL;
4946 n = 1;
4947 } else {
4948 n = 0;
4949 te = s->first_displayed_entry;
4952 nentries = got_object_tree_get_nentries(s->tree);
4953 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4954 char *line = NULL, *id_str = NULL, *link_target = NULL;
4955 const char *modestr = "";
4956 mode_t mode;
4958 te = got_object_tree_get_entry(s->tree, i);
4959 mode = got_tree_entry_get_mode(te);
4961 if (s->show_ids) {
4962 err = got_object_id_str(&id_str,
4963 got_tree_entry_get_id(te));
4964 if (err)
4965 return got_error_from_errno(
4966 "got_object_id_str");
4968 if (got_object_tree_entry_is_submodule(te))
4969 modestr = "$";
4970 else if (S_ISLNK(mode)) {
4971 int i;
4973 err = got_tree_entry_get_symlink_target(&link_target,
4974 te, s->repo);
4975 if (err) {
4976 free(id_str);
4977 return err;
4979 for (i = 0; i < strlen(link_target); i++) {
4980 if (!isprint((unsigned char)link_target[i]))
4981 link_target[i] = '?';
4983 modestr = "@";
4985 else if (S_ISDIR(mode))
4986 modestr = "/";
4987 else if (mode & S_IXUSR)
4988 modestr = "*";
4989 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4990 got_tree_entry_get_name(te), modestr,
4991 link_target ? " -> ": "",
4992 link_target ? link_target : "") == -1) {
4993 free(id_str);
4994 free(link_target);
4995 return got_error_from_errno("asprintf");
4997 free(id_str);
4998 free(link_target);
4999 err = format_line(&wline, &width, line, view->ncols, 0);
5000 if (err) {
5001 free(line);
5002 break;
5004 if (n == s->selected) {
5005 if (view->focussed)
5006 wstandout(view->window);
5007 s->selected_entry = te;
5009 tc = match_color(&s->colors, line);
5010 if (tc)
5011 wattr_on(view->window,
5012 COLOR_PAIR(tc->colorpair), NULL);
5013 waddwstr(view->window, wline);
5014 if (tc)
5015 wattr_off(view->window,
5016 COLOR_PAIR(tc->colorpair), NULL);
5017 if (width < view->ncols - 1)
5018 waddch(view->window, '\n');
5019 if (n == s->selected && view->focussed)
5020 wstandend(view->window);
5021 free(line);
5022 free(wline);
5023 wline = NULL;
5024 n++;
5025 s->ndisplayed++;
5026 s->last_displayed_entry = te;
5027 if (--limit <= 0)
5028 break;
5031 return err;
5034 static void
5035 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5037 struct got_tree_entry *te;
5038 int isroot = s->tree == s->root;
5039 int i = 0;
5041 if (s->first_displayed_entry == NULL)
5042 return;
5044 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5045 while (i++ < maxscroll) {
5046 if (te == NULL) {
5047 if (!isroot)
5048 s->first_displayed_entry = NULL;
5049 break;
5051 s->first_displayed_entry = te;
5052 te = got_tree_entry_get_prev(s->tree, te);
5056 static void
5057 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5059 struct got_tree_entry *next, *last;
5060 int n = 0;
5062 if (s->first_displayed_entry)
5063 next = got_tree_entry_get_next(s->tree,
5064 s->first_displayed_entry);
5065 else
5066 next = got_object_tree_get_first_entry(s->tree);
5068 last = s->last_displayed_entry;
5069 while (next && last && n++ < maxscroll) {
5070 last = got_tree_entry_get_next(s->tree, last);
5071 if (last) {
5072 s->first_displayed_entry = next;
5073 next = got_tree_entry_get_next(s->tree, next);
5078 static const struct got_error *
5079 tree_entry_path(char **path, struct tog_parent_trees *parents,
5080 struct got_tree_entry *te)
5082 const struct got_error *err = NULL;
5083 struct tog_parent_tree *pt;
5084 size_t len = 2; /* for leading slash and NUL */
5086 TAILQ_FOREACH(pt, parents, entry)
5087 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5088 + 1 /* slash */;
5089 if (te)
5090 len += strlen(got_tree_entry_get_name(te));
5092 *path = calloc(1, len);
5093 if (path == NULL)
5094 return got_error_from_errno("calloc");
5096 (*path)[0] = '/';
5097 pt = TAILQ_LAST(parents, tog_parent_trees);
5098 while (pt) {
5099 const char *name = got_tree_entry_get_name(pt->selected_entry);
5100 if (strlcat(*path, name, len) >= len) {
5101 err = got_error(GOT_ERR_NO_SPACE);
5102 goto done;
5104 if (strlcat(*path, "/", len) >= len) {
5105 err = got_error(GOT_ERR_NO_SPACE);
5106 goto done;
5108 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5110 if (te) {
5111 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5112 err = got_error(GOT_ERR_NO_SPACE);
5113 goto done;
5116 done:
5117 if (err) {
5118 free(*path);
5119 *path = NULL;
5121 return err;
5124 static const struct got_error *
5125 blame_tree_entry(struct tog_view **new_view, int begin_x,
5126 struct got_tree_entry *te, struct tog_parent_trees *parents,
5127 struct got_object_id *commit_id, struct got_repository *repo)
5129 const struct got_error *err = NULL;
5130 char *path;
5131 struct tog_view *blame_view;
5133 *new_view = NULL;
5135 err = tree_entry_path(&path, parents, te);
5136 if (err)
5137 return err;
5139 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5140 if (blame_view == NULL) {
5141 err = got_error_from_errno("view_open");
5142 goto done;
5145 err = open_blame_view(blame_view, path, commit_id, repo);
5146 if (err) {
5147 if (err->code == GOT_ERR_CANCELLED)
5148 err = NULL;
5149 view_close(blame_view);
5150 } else
5151 *new_view = blame_view;
5152 done:
5153 free(path);
5154 return err;
5157 static const struct got_error *
5158 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5159 struct tog_tree_view_state *s)
5161 struct tog_view *log_view;
5162 const struct got_error *err = NULL;
5163 char *path;
5165 *new_view = NULL;
5167 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5168 if (log_view == NULL)
5169 return got_error_from_errno("view_open");
5171 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5172 if (err)
5173 return err;
5175 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5176 path, 0);
5177 if (err)
5178 view_close(log_view);
5179 else
5180 *new_view = log_view;
5181 free(path);
5182 return err;
5185 static const struct got_error *
5186 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5187 const char *head_ref_name, struct got_repository *repo)
5189 const struct got_error *err = NULL;
5190 char *commit_id_str = NULL;
5191 struct tog_tree_view_state *s = &view->state.tree;
5192 struct got_commit_object *commit = NULL;
5194 TAILQ_INIT(&s->parents);
5195 STAILQ_INIT(&s->colors);
5197 s->commit_id = got_object_id_dup(commit_id);
5198 if (s->commit_id == NULL)
5199 return got_error_from_errno("got_object_id_dup");
5201 err = got_object_open_as_commit(&commit, repo, commit_id);
5202 if (err)
5203 goto done;
5206 * The root is opened here and will be closed when the view is closed.
5207 * Any visited subtrees and their path-wise parents are opened and
5208 * closed on demand.
5210 err = got_object_open_as_tree(&s->root, repo,
5211 got_object_commit_get_tree_id(commit));
5212 if (err)
5213 goto done;
5214 s->tree = s->root;
5216 err = got_object_id_str(&commit_id_str, commit_id);
5217 if (err != NULL)
5218 goto done;
5220 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5221 err = got_error_from_errno("asprintf");
5222 goto done;
5225 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5226 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5227 if (head_ref_name) {
5228 s->head_ref_name = strdup(head_ref_name);
5229 if (s->head_ref_name == NULL) {
5230 err = got_error_from_errno("strdup");
5231 goto done;
5234 s->repo = repo;
5236 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5237 err = add_color(&s->colors, "\\$$",
5238 TOG_COLOR_TREE_SUBMODULE,
5239 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5240 if (err)
5241 goto done;
5242 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5243 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5244 if (err)
5245 goto done;
5246 err = add_color(&s->colors, "/$",
5247 TOG_COLOR_TREE_DIRECTORY,
5248 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5249 if (err)
5250 goto done;
5252 err = add_color(&s->colors, "\\*$",
5253 TOG_COLOR_TREE_EXECUTABLE,
5254 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5255 if (err)
5256 goto done;
5258 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5259 get_color_value("TOG_COLOR_COMMIT"));
5260 if (err)
5261 goto done;
5264 view->show = show_tree_view;
5265 view->input = input_tree_view;
5266 view->close = close_tree_view;
5267 view->search_start = search_start_tree_view;
5268 view->search_next = search_next_tree_view;
5269 done:
5270 free(commit_id_str);
5271 if (commit)
5272 got_object_commit_close(commit);
5273 if (err)
5274 close_tree_view(view);
5275 return err;
5278 static const struct got_error *
5279 close_tree_view(struct tog_view *view)
5281 struct tog_tree_view_state *s = &view->state.tree;
5283 free_colors(&s->colors);
5284 free(s->tree_label);
5285 s->tree_label = NULL;
5286 free(s->commit_id);
5287 s->commit_id = NULL;
5288 free(s->head_ref_name);
5289 s->head_ref_name = NULL;
5290 while (!TAILQ_EMPTY(&s->parents)) {
5291 struct tog_parent_tree *parent;
5292 parent = TAILQ_FIRST(&s->parents);
5293 TAILQ_REMOVE(&s->parents, parent, entry);
5294 if (parent->tree != s->root)
5295 got_object_tree_close(parent->tree);
5296 free(parent);
5299 if (s->tree != NULL && s->tree != s->root)
5300 got_object_tree_close(s->tree);
5301 if (s->root)
5302 got_object_tree_close(s->root);
5303 return NULL;
5306 static const struct got_error *
5307 search_start_tree_view(struct tog_view *view)
5309 struct tog_tree_view_state *s = &view->state.tree;
5311 s->matched_entry = NULL;
5312 return NULL;
5315 static int
5316 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5318 regmatch_t regmatch;
5320 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5321 0) == 0;
5324 static const struct got_error *
5325 search_next_tree_view(struct tog_view *view)
5327 struct tog_tree_view_state *s = &view->state.tree;
5328 struct got_tree_entry *te = NULL;
5330 if (!view->searching) {
5331 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5332 return NULL;
5335 if (s->matched_entry) {
5336 if (view->searching == TOG_SEARCH_FORWARD) {
5337 if (s->selected_entry)
5338 te = got_tree_entry_get_next(s->tree,
5339 s->selected_entry);
5340 else
5341 te = got_object_tree_get_first_entry(s->tree);
5342 } else {
5343 if (s->selected_entry == NULL)
5344 te = got_object_tree_get_last_entry(s->tree);
5345 else
5346 te = got_tree_entry_get_prev(s->tree,
5347 s->selected_entry);
5349 } else {
5350 if (view->searching == TOG_SEARCH_FORWARD)
5351 te = got_object_tree_get_first_entry(s->tree);
5352 else
5353 te = got_object_tree_get_last_entry(s->tree);
5356 while (1) {
5357 if (te == NULL) {
5358 if (s->matched_entry == NULL) {
5359 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5360 return NULL;
5362 if (view->searching == TOG_SEARCH_FORWARD)
5363 te = got_object_tree_get_first_entry(s->tree);
5364 else
5365 te = got_object_tree_get_last_entry(s->tree);
5368 if (match_tree_entry(te, &view->regex)) {
5369 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5370 s->matched_entry = te;
5371 break;
5374 if (view->searching == TOG_SEARCH_FORWARD)
5375 te = got_tree_entry_get_next(s->tree, te);
5376 else
5377 te = got_tree_entry_get_prev(s->tree, te);
5380 if (s->matched_entry) {
5381 s->first_displayed_entry = s->matched_entry;
5382 s->selected = 0;
5385 return NULL;
5388 static const struct got_error *
5389 show_tree_view(struct tog_view *view)
5391 const struct got_error *err = NULL;
5392 struct tog_tree_view_state *s = &view->state.tree;
5393 char *parent_path;
5395 err = tree_entry_path(&parent_path, &s->parents, NULL);
5396 if (err)
5397 return err;
5399 err = draw_tree_entries(view, parent_path);
5400 free(parent_path);
5402 view_vborder(view);
5403 return err;
5406 static const struct got_error *
5407 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5409 const struct got_error *err = NULL;
5410 struct tog_tree_view_state *s = &view->state.tree;
5411 struct tog_view *log_view, *ref_view;
5412 struct got_tree_entry *te;
5413 int begin_x = 0, n;
5415 switch (ch) {
5416 case 'i':
5417 s->show_ids = !s->show_ids;
5418 break;
5419 case 'l':
5420 if (!s->selected_entry)
5421 break;
5422 if (view_is_parent_view(view))
5423 begin_x = view_split_begin_x(view->begin_x);
5424 err = log_selected_tree_entry(&log_view, begin_x, s);
5425 view->focussed = 0;
5426 log_view->focussed = 1;
5427 if (view_is_parent_view(view)) {
5428 err = view_close_child(view);
5429 if (err)
5430 return err;
5431 view_set_child(view, log_view);
5432 view->focus_child = 1;
5433 } else
5434 *new_view = log_view;
5435 break;
5436 case 'r':
5437 if (view_is_parent_view(view))
5438 begin_x = view_split_begin_x(view->begin_x);
5439 ref_view = view_open(view->nlines, view->ncols,
5440 view->begin_y, begin_x, TOG_VIEW_REF);
5441 if (ref_view == NULL)
5442 return got_error_from_errno("view_open");
5443 err = open_ref_view(ref_view, s->repo);
5444 if (err) {
5445 view_close(ref_view);
5446 return err;
5448 view->focussed = 0;
5449 ref_view->focussed = 1;
5450 if (view_is_parent_view(view)) {
5451 err = view_close_child(view);
5452 if (err)
5453 return err;
5454 view_set_child(view, ref_view);
5455 view->focus_child = 1;
5456 } else
5457 *new_view = ref_view;
5458 break;
5459 case 'g':
5460 case KEY_HOME:
5461 s->selected = 0;
5462 if (s->tree == s->root)
5463 s->first_displayed_entry =
5464 got_object_tree_get_first_entry(s->tree);
5465 else
5466 s->first_displayed_entry = NULL;
5467 break;
5468 case 'G':
5469 case KEY_END:
5470 s->selected = 0;
5471 te = got_object_tree_get_last_entry(s->tree);
5472 for (n = 0; n < view->nlines - 3; n++) {
5473 if (te == NULL) {
5474 if(s->tree != s->root) {
5475 s->first_displayed_entry = NULL;
5476 n++;
5478 break;
5480 s->first_displayed_entry = te;
5481 te = got_tree_entry_get_prev(s->tree, te);
5483 if (n > 0)
5484 s->selected = n - 1;
5485 break;
5486 case 'k':
5487 case KEY_UP:
5488 if (s->selected > 0) {
5489 s->selected--;
5490 break;
5492 tree_scroll_up(s, 1);
5493 break;
5494 case KEY_PPAGE:
5495 case CTRL('b'):
5496 if (s->tree == s->root) {
5497 if (got_object_tree_get_first_entry(s->tree) ==
5498 s->first_displayed_entry)
5499 s->selected = 0;
5500 } else {
5501 if (s->first_displayed_entry == NULL)
5502 s->selected = 0;
5504 tree_scroll_up(s, MAX(0, view->nlines - 3));
5505 break;
5506 case 'j':
5507 case KEY_DOWN:
5508 if (s->selected < s->ndisplayed - 1) {
5509 s->selected++;
5510 break;
5512 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5513 == NULL)
5514 /* can't scroll any further */
5515 break;
5516 tree_scroll_down(s, 1);
5517 break;
5518 case KEY_NPAGE:
5519 case CTRL('f'):
5520 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5521 == NULL) {
5522 /* can't scroll any further; move cursor down */
5523 if (s->selected < s->ndisplayed - 1)
5524 s->selected = s->ndisplayed - 1;
5525 break;
5527 tree_scroll_down(s, view->nlines - 3);
5528 break;
5529 case KEY_ENTER:
5530 case '\r':
5531 case KEY_BACKSPACE:
5532 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5533 struct tog_parent_tree *parent;
5534 /* user selected '..' */
5535 if (s->tree == s->root)
5536 break;
5537 parent = TAILQ_FIRST(&s->parents);
5538 TAILQ_REMOVE(&s->parents, parent,
5539 entry);
5540 got_object_tree_close(s->tree);
5541 s->tree = parent->tree;
5542 s->first_displayed_entry =
5543 parent->first_displayed_entry;
5544 s->selected_entry =
5545 parent->selected_entry;
5546 s->selected = parent->selected;
5547 free(parent);
5548 } else if (S_ISDIR(got_tree_entry_get_mode(
5549 s->selected_entry))) {
5550 struct got_tree_object *subtree;
5551 err = got_object_open_as_tree(&subtree, s->repo,
5552 got_tree_entry_get_id(s->selected_entry));
5553 if (err)
5554 break;
5555 err = tree_view_visit_subtree(s, subtree);
5556 if (err) {
5557 got_object_tree_close(subtree);
5558 break;
5560 } else if (S_ISREG(got_tree_entry_get_mode(
5561 s->selected_entry))) {
5562 struct tog_view *blame_view;
5563 int begin_x = view_is_parent_view(view) ?
5564 view_split_begin_x(view->begin_x) : 0;
5566 err = blame_tree_entry(&blame_view, begin_x,
5567 s->selected_entry, &s->parents,
5568 s->commit_id, s->repo);
5569 if (err)
5570 break;
5571 view->focussed = 0;
5572 blame_view->focussed = 1;
5573 if (view_is_parent_view(view)) {
5574 err = view_close_child(view);
5575 if (err)
5576 return err;
5577 view_set_child(view, blame_view);
5578 view->focus_child = 1;
5579 } else
5580 *new_view = blame_view;
5582 break;
5583 case KEY_RESIZE:
5584 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5585 s->selected = view->nlines - 4;
5586 break;
5587 default:
5588 break;
5591 return err;
5594 __dead static void
5595 usage_tree(void)
5597 endwin();
5598 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5599 getprogname());
5600 exit(1);
5603 static const struct got_error *
5604 cmd_tree(int argc, char *argv[])
5606 const struct got_error *error;
5607 struct got_repository *repo = NULL;
5608 struct got_worktree *worktree = NULL;
5609 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5610 struct got_object_id *commit_id = NULL;
5611 const char *commit_id_arg = NULL;
5612 char *label = NULL;
5613 struct got_reference *ref = NULL;
5614 const char *head_ref_name = NULL;
5615 int ch;
5616 struct tog_view *view;
5618 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5619 switch (ch) {
5620 case 'c':
5621 commit_id_arg = optarg;
5622 break;
5623 case 'r':
5624 repo_path = realpath(optarg, NULL);
5625 if (repo_path == NULL)
5626 return got_error_from_errno2("realpath",
5627 optarg);
5628 break;
5629 default:
5630 usage_tree();
5631 /* NOTREACHED */
5635 argc -= optind;
5636 argv += optind;
5638 if (argc > 1)
5639 usage_tree();
5641 if (repo_path == NULL) {
5642 cwd = getcwd(NULL, 0);
5643 if (cwd == NULL)
5644 return got_error_from_errno("getcwd");
5645 error = got_worktree_open(&worktree, cwd);
5646 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5647 goto done;
5648 if (worktree)
5649 repo_path =
5650 strdup(got_worktree_get_repo_path(worktree));
5651 else
5652 repo_path = strdup(cwd);
5653 if (repo_path == NULL) {
5654 error = got_error_from_errno("strdup");
5655 goto done;
5659 error = got_repo_open(&repo, repo_path, NULL);
5660 if (error != NULL)
5661 goto done;
5663 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5664 repo, worktree);
5665 if (error)
5666 goto done;
5668 init_curses();
5670 error = apply_unveil(got_repo_get_path(repo), NULL);
5671 if (error)
5672 goto done;
5674 error = tog_load_refs(repo);
5675 if (error)
5676 goto done;
5678 if (commit_id_arg == NULL) {
5679 error = got_repo_match_object_id(&commit_id, &label,
5680 worktree ? got_worktree_get_head_ref_name(worktree) :
5681 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5682 if (error)
5683 goto done;
5684 head_ref_name = label;
5685 } else {
5686 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5687 if (error == NULL)
5688 head_ref_name = got_ref_get_name(ref);
5689 else if (error->code != GOT_ERR_NOT_REF)
5690 goto done;
5691 error = got_repo_match_object_id(&commit_id, NULL,
5692 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5693 if (error)
5694 goto done;
5697 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5698 if (view == NULL) {
5699 error = got_error_from_errno("view_open");
5700 goto done;
5702 error = open_tree_view(view, commit_id, head_ref_name, repo);
5703 if (error)
5704 goto done;
5705 if (!got_path_is_root_dir(in_repo_path)) {
5706 error = tree_view_walk_path(&view->state.tree, commit_id,
5707 in_repo_path);
5708 if (error)
5709 goto done;
5712 if (worktree) {
5713 /* Release work tree lock. */
5714 got_worktree_close(worktree);
5715 worktree = NULL;
5717 error = view_loop(view);
5718 done:
5719 free(repo_path);
5720 free(cwd);
5721 free(commit_id);
5722 free(label);
5723 if (ref)
5724 got_ref_close(ref);
5725 if (repo) {
5726 const struct got_error *close_err = got_repo_close(repo);
5727 if (error == NULL)
5728 error = close_err;
5730 tog_free_refs();
5731 return error;
5734 static const struct got_error *
5735 ref_view_load_refs(struct tog_ref_view_state *s)
5737 struct got_reflist_entry *sre;
5738 struct tog_reflist_entry *re;
5740 s->nrefs = 0;
5741 TAILQ_FOREACH(sre, &tog_refs, entry) {
5742 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5743 continue;
5745 re = malloc(sizeof(*re));
5746 if (re == NULL)
5747 return got_error_from_errno("malloc");
5749 re->ref = got_ref_dup(sre->ref);
5750 if (re->ref == NULL)
5751 return got_error_from_errno("got_ref_dup");
5752 re->idx = s->nrefs++;
5753 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5756 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5757 return NULL;
5760 void
5761 ref_view_free_refs(struct tog_ref_view_state *s)
5763 struct tog_reflist_entry *re;
5765 while (!TAILQ_EMPTY(&s->refs)) {
5766 re = TAILQ_FIRST(&s->refs);
5767 TAILQ_REMOVE(&s->refs, re, entry);
5768 got_ref_close(re->ref);
5769 free(re);
5773 static const struct got_error *
5774 open_ref_view(struct tog_view *view, struct got_repository *repo)
5776 const struct got_error *err = NULL;
5777 struct tog_ref_view_state *s = &view->state.ref;
5779 s->selected_entry = 0;
5780 s->repo = repo;
5782 TAILQ_INIT(&s->refs);
5783 STAILQ_INIT(&s->colors);
5785 err = ref_view_load_refs(s);
5786 if (err)
5787 return err;
5789 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5790 err = add_color(&s->colors, "^refs/heads/",
5791 TOG_COLOR_REFS_HEADS,
5792 get_color_value("TOG_COLOR_REFS_HEADS"));
5793 if (err)
5794 goto done;
5796 err = add_color(&s->colors, "^refs/tags/",
5797 TOG_COLOR_REFS_TAGS,
5798 get_color_value("TOG_COLOR_REFS_TAGS"));
5799 if (err)
5800 goto done;
5802 err = add_color(&s->colors, "^refs/remotes/",
5803 TOG_COLOR_REFS_REMOTES,
5804 get_color_value("TOG_COLOR_REFS_REMOTES"));
5805 if (err)
5806 goto done;
5809 view->show = show_ref_view;
5810 view->input = input_ref_view;
5811 view->close = close_ref_view;
5812 view->search_start = search_start_ref_view;
5813 view->search_next = search_next_ref_view;
5814 done:
5815 if (err)
5816 free_colors(&s->colors);
5817 return err;
5820 static const struct got_error *
5821 close_ref_view(struct tog_view *view)
5823 struct tog_ref_view_state *s = &view->state.ref;
5825 ref_view_free_refs(s);
5826 free_colors(&s->colors);
5828 return NULL;
5831 static const struct got_error *
5832 resolve_reflist_entry(struct got_object_id **commit_id,
5833 struct tog_reflist_entry *re, struct got_repository *repo)
5835 const struct got_error *err = NULL;
5836 struct got_object_id *obj_id;
5837 struct got_tag_object *tag = NULL;
5838 int obj_type;
5840 *commit_id = NULL;
5842 err = got_ref_resolve(&obj_id, repo, re->ref);
5843 if (err)
5844 return err;
5846 err = got_object_get_type(&obj_type, repo, obj_id);
5847 if (err)
5848 goto done;
5850 switch (obj_type) {
5851 case GOT_OBJ_TYPE_COMMIT:
5852 *commit_id = obj_id;
5853 break;
5854 case GOT_OBJ_TYPE_TAG:
5855 err = got_object_open_as_tag(&tag, repo, obj_id);
5856 if (err)
5857 goto done;
5858 free(obj_id);
5859 err = got_object_get_type(&obj_type, repo,
5860 got_object_tag_get_object_id(tag));
5861 if (err)
5862 goto done;
5863 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5864 err = got_error(GOT_ERR_OBJ_TYPE);
5865 goto done;
5867 *commit_id = got_object_id_dup(
5868 got_object_tag_get_object_id(tag));
5869 if (*commit_id == NULL) {
5870 err = got_error_from_errno("got_object_id_dup");
5871 goto done;
5873 break;
5874 default:
5875 err = got_error(GOT_ERR_OBJ_TYPE);
5876 break;
5879 done:
5880 if (tag)
5881 got_object_tag_close(tag);
5882 if (err) {
5883 free(*commit_id);
5884 *commit_id = NULL;
5886 return err;
5889 static const struct got_error *
5890 log_ref_entry(struct tog_view **new_view, int begin_x,
5891 struct tog_reflist_entry *re, struct got_repository *repo)
5893 struct tog_view *log_view;
5894 const struct got_error *err = NULL;
5895 struct got_object_id *commit_id = NULL;
5897 *new_view = NULL;
5899 err = resolve_reflist_entry(&commit_id, re, repo);
5900 if (err) {
5901 if (err->code != GOT_ERR_OBJ_TYPE)
5902 return err;
5903 else
5904 return NULL;
5907 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5908 if (log_view == NULL) {
5909 err = got_error_from_errno("view_open");
5910 goto done;
5913 err = open_log_view(log_view, commit_id, repo,
5914 got_ref_get_name(re->ref), "", 0);
5915 done:
5916 if (err)
5917 view_close(log_view);
5918 else
5919 *new_view = log_view;
5920 free(commit_id);
5921 return err;
5924 static void
5925 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5927 struct tog_reflist_entry *re;
5928 int i = 0;
5930 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5931 return;
5933 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5934 while (i++ < maxscroll) {
5935 if (re == NULL)
5936 break;
5937 s->first_displayed_entry = re;
5938 re = TAILQ_PREV(re, tog_reflist_head, entry);
5942 static void
5943 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5945 struct tog_reflist_entry *next, *last;
5946 int n = 0;
5948 if (s->first_displayed_entry)
5949 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5950 else
5951 next = TAILQ_FIRST(&s->refs);
5953 last = s->last_displayed_entry;
5954 while (next && last && n++ < maxscroll) {
5955 last = TAILQ_NEXT(last, entry);
5956 if (last) {
5957 s->first_displayed_entry = next;
5958 next = TAILQ_NEXT(next, entry);
5963 static const struct got_error *
5964 search_start_ref_view(struct tog_view *view)
5966 struct tog_ref_view_state *s = &view->state.ref;
5968 s->matched_entry = NULL;
5969 return NULL;
5972 static int
5973 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5975 regmatch_t regmatch;
5977 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5978 0) == 0;
5981 static const struct got_error *
5982 search_next_ref_view(struct tog_view *view)
5984 struct tog_ref_view_state *s = &view->state.ref;
5985 struct tog_reflist_entry *re = NULL;
5987 if (!view->searching) {
5988 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5989 return NULL;
5992 if (s->matched_entry) {
5993 if (view->searching == TOG_SEARCH_FORWARD) {
5994 if (s->selected_entry)
5995 re = TAILQ_NEXT(s->selected_entry, entry);
5996 else
5997 re = TAILQ_PREV(s->selected_entry,
5998 tog_reflist_head, entry);
5999 } else {
6000 if (s->selected_entry == NULL)
6001 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6002 else
6003 re = TAILQ_PREV(s->selected_entry,
6004 tog_reflist_head, entry);
6006 } else {
6007 if (view->searching == TOG_SEARCH_FORWARD)
6008 re = TAILQ_FIRST(&s->refs);
6009 else
6010 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6013 while (1) {
6014 if (re == NULL) {
6015 if (s->matched_entry == NULL) {
6016 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6017 return NULL;
6019 if (view->searching == TOG_SEARCH_FORWARD)
6020 re = TAILQ_FIRST(&s->refs);
6021 else
6022 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6025 if (match_reflist_entry(re, &view->regex)) {
6026 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6027 s->matched_entry = re;
6028 break;
6031 if (view->searching == TOG_SEARCH_FORWARD)
6032 re = TAILQ_NEXT(re, entry);
6033 else
6034 re = TAILQ_PREV(re, tog_reflist_head, entry);
6037 if (s->matched_entry) {
6038 s->first_displayed_entry = s->matched_entry;
6039 s->selected = 0;
6042 return NULL;
6045 static const struct got_error *
6046 show_ref_view(struct tog_view *view)
6048 const struct got_error *err = NULL;
6049 struct tog_ref_view_state *s = &view->state.ref;
6050 struct tog_reflist_entry *re;
6051 char *line = NULL;
6052 wchar_t *wline;
6053 struct tog_color *tc;
6054 int width, n;
6055 int limit = view->nlines;
6057 werase(view->window);
6059 s->ndisplayed = 0;
6061 if (limit == 0)
6062 return NULL;
6064 re = s->first_displayed_entry;
6066 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6067 s->nrefs) == -1)
6068 return got_error_from_errno("asprintf");
6070 err = format_line(&wline, &width, line, view->ncols, 0);
6071 if (err) {
6072 free(line);
6073 return err;
6075 if (view_needs_focus_indication(view))
6076 wstandout(view->window);
6077 waddwstr(view->window, wline);
6078 if (view_needs_focus_indication(view))
6079 wstandend(view->window);
6080 free(wline);
6081 wline = NULL;
6082 free(line);
6083 line = NULL;
6084 if (width < view->ncols - 1)
6085 waddch(view->window, '\n');
6086 if (--limit <= 0)
6087 return NULL;
6089 n = 0;
6090 while (re && limit > 0) {
6091 char *line = NULL;
6093 if (got_ref_is_symbolic(re->ref)) {
6094 if (asprintf(&line, "%s -> %s",
6095 got_ref_get_name(re->ref),
6096 got_ref_get_symref_target(re->ref)) == -1)
6097 return got_error_from_errno("asprintf");
6098 } else if (s->show_ids) {
6099 struct got_object_id *id;
6100 char *id_str;
6101 err = got_ref_resolve(&id, s->repo, re->ref);
6102 if (err)
6103 return err;
6104 err = got_object_id_str(&id_str, id);
6105 if (err) {
6106 free(id);
6107 return err;
6109 if (asprintf(&line, "%s: %s",
6110 got_ref_get_name(re->ref), id_str) == -1) {
6111 err = got_error_from_errno("asprintf");
6112 free(id);
6113 free(id_str);
6114 return err;
6116 free(id);
6117 free(id_str);
6118 } else {
6119 line = strdup(got_ref_get_name(re->ref));
6120 if (line == NULL)
6121 return got_error_from_errno("strdup");
6124 err = format_line(&wline, &width, line, view->ncols, 0);
6125 if (err) {
6126 free(line);
6127 return err;
6129 if (n == s->selected) {
6130 if (view->focussed)
6131 wstandout(view->window);
6132 s->selected_entry = re;
6134 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6135 if (tc)
6136 wattr_on(view->window,
6137 COLOR_PAIR(tc->colorpair), NULL);
6138 waddwstr(view->window, wline);
6139 if (tc)
6140 wattr_off(view->window,
6141 COLOR_PAIR(tc->colorpair), NULL);
6142 if (width < view->ncols - 1)
6143 waddch(view->window, '\n');
6144 if (n == s->selected && view->focussed)
6145 wstandend(view->window);
6146 free(line);
6147 free(wline);
6148 wline = NULL;
6149 n++;
6150 s->ndisplayed++;
6151 s->last_displayed_entry = re;
6153 limit--;
6154 re = TAILQ_NEXT(re, entry);
6157 view_vborder(view);
6158 return err;
6161 static const struct got_error *
6162 browse_ref_tree(struct tog_view **new_view, int begin_x,
6163 struct tog_reflist_entry *re, struct got_repository *repo)
6165 const struct got_error *err = NULL;
6166 struct got_object_id *commit_id = NULL;
6167 struct tog_view *tree_view;
6169 *new_view = NULL;
6171 err = resolve_reflist_entry(&commit_id, re, repo);
6172 if (err) {
6173 if (err->code != GOT_ERR_OBJ_TYPE)
6174 return err;
6175 else
6176 return NULL;
6180 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6181 if (tree_view == NULL) {
6182 err = got_error_from_errno("view_open");
6183 goto done;
6186 err = open_tree_view(tree_view, commit_id,
6187 got_ref_get_name(re->ref), repo);
6188 if (err)
6189 goto done;
6191 *new_view = tree_view;
6192 done:
6193 free(commit_id);
6194 return err;
6196 static const struct got_error *
6197 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6199 const struct got_error *err = NULL;
6200 struct tog_ref_view_state *s = &view->state.ref;
6201 struct tog_view *log_view, *tree_view;
6202 struct tog_reflist_entry *re;
6203 int begin_x = 0, n;
6205 switch (ch) {
6206 case 'i':
6207 s->show_ids = !s->show_ids;
6208 break;
6209 case KEY_ENTER:
6210 case '\r':
6211 if (!s->selected_entry)
6212 break;
6213 if (view_is_parent_view(view))
6214 begin_x = view_split_begin_x(view->begin_x);
6215 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6216 s->repo);
6217 view->focussed = 0;
6218 log_view->focussed = 1;
6219 if (view_is_parent_view(view)) {
6220 err = view_close_child(view);
6221 if (err)
6222 return err;
6223 view_set_child(view, log_view);
6224 view->focus_child = 1;
6225 } else
6226 *new_view = log_view;
6227 break;
6228 case 't':
6229 if (!s->selected_entry)
6230 break;
6231 if (view_is_parent_view(view))
6232 begin_x = view_split_begin_x(view->begin_x);
6233 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6234 s->repo);
6235 if (err || tree_view == NULL)
6236 break;
6237 view->focussed = 0;
6238 tree_view->focussed = 1;
6239 if (view_is_parent_view(view)) {
6240 err = view_close_child(view);
6241 if (err)
6242 return err;
6243 view_set_child(view, tree_view);
6244 view->focus_child = 1;
6245 } else
6246 *new_view = tree_view;
6247 break;
6248 case 'g':
6249 case KEY_HOME:
6250 s->selected = 0;
6251 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6252 break;
6253 case 'G':
6254 case KEY_END:
6255 s->selected = 0;
6256 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6257 for (n = 0; n < view->nlines - 1; n++) {
6258 if (re == NULL)
6259 break;
6260 s->first_displayed_entry = re;
6261 re = TAILQ_PREV(re, tog_reflist_head, entry);
6263 if (n > 0)
6264 s->selected = n - 1;
6265 break;
6266 case 'k':
6267 case KEY_UP:
6268 if (s->selected > 0) {
6269 s->selected--;
6270 break;
6272 ref_scroll_up(s, 1);
6273 break;
6274 case KEY_PPAGE:
6275 case CTRL('b'):
6276 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6277 s->selected = 0;
6278 ref_scroll_up(s, MAX(0, view->nlines - 1));
6279 break;
6280 case 'j':
6281 case KEY_DOWN:
6282 if (s->selected < s->ndisplayed - 1) {
6283 s->selected++;
6284 break;
6286 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6287 /* can't scroll any further */
6288 break;
6289 ref_scroll_down(s, 1);
6290 break;
6291 case KEY_NPAGE:
6292 case CTRL('f'):
6293 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6294 /* can't scroll any further; move cursor down */
6295 if (s->selected < s->ndisplayed - 1)
6296 s->selected = s->ndisplayed - 1;
6297 break;
6299 ref_scroll_down(s, view->nlines - 1);
6300 break;
6301 case CTRL('l'):
6302 tog_free_refs();
6303 err = tog_load_refs(s->repo);
6304 if (err)
6305 break;
6306 ref_view_free_refs(s);
6307 err = ref_view_load_refs(s);
6308 break;
6309 case KEY_RESIZE:
6310 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6311 s->selected = view->nlines - 2;
6312 break;
6313 default:
6314 break;
6317 return err;
6320 __dead static void
6321 usage_ref(void)
6323 endwin();
6324 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6325 getprogname());
6326 exit(1);
6329 static const struct got_error *
6330 cmd_ref(int argc, char *argv[])
6332 const struct got_error *error;
6333 struct got_repository *repo = NULL;
6334 struct got_worktree *worktree = NULL;
6335 char *cwd = NULL, *repo_path = NULL;
6336 int ch;
6337 struct tog_view *view;
6339 while ((ch = getopt(argc, argv, "r:")) != -1) {
6340 switch (ch) {
6341 case 'r':
6342 repo_path = realpath(optarg, NULL);
6343 if (repo_path == NULL)
6344 return got_error_from_errno2("realpath",
6345 optarg);
6346 break;
6347 default:
6348 usage_ref();
6349 /* NOTREACHED */
6353 argc -= optind;
6354 argv += optind;
6356 if (argc > 1)
6357 usage_ref();
6359 if (repo_path == NULL) {
6360 cwd = getcwd(NULL, 0);
6361 if (cwd == NULL)
6362 return got_error_from_errno("getcwd");
6363 error = got_worktree_open(&worktree, cwd);
6364 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6365 goto done;
6366 if (worktree)
6367 repo_path =
6368 strdup(got_worktree_get_repo_path(worktree));
6369 else
6370 repo_path = strdup(cwd);
6371 if (repo_path == NULL) {
6372 error = got_error_from_errno("strdup");
6373 goto done;
6377 error = got_repo_open(&repo, repo_path, NULL);
6378 if (error != NULL)
6379 goto done;
6381 init_curses();
6383 error = apply_unveil(got_repo_get_path(repo), NULL);
6384 if (error)
6385 goto done;
6387 error = tog_load_refs(repo);
6388 if (error)
6389 goto done;
6391 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6392 if (view == NULL) {
6393 error = got_error_from_errno("view_open");
6394 goto done;
6397 error = open_ref_view(view, repo);
6398 if (error)
6399 goto done;
6401 if (worktree) {
6402 /* Release work tree lock. */
6403 got_worktree_close(worktree);
6404 worktree = NULL;
6406 error = view_loop(view);
6407 done:
6408 free(repo_path);
6409 free(cwd);
6410 if (repo) {
6411 const struct got_error *close_err = got_repo_close(repo);
6412 if (close_err)
6413 error = close_err;
6415 tog_free_refs();
6416 return error;
6419 static void
6420 list_commands(FILE *fp)
6422 size_t i;
6424 fprintf(fp, "commands:");
6425 for (i = 0; i < nitems(tog_commands); i++) {
6426 struct tog_cmd *cmd = &tog_commands[i];
6427 fprintf(fp, " %s", cmd->name);
6429 fputc('\n', fp);
6432 __dead static void
6433 usage(int hflag, int status)
6435 FILE *fp = (status == 0) ? stdout : stderr;
6437 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6438 getprogname());
6439 if (hflag) {
6440 fprintf(fp, "lazy usage: %s path\n", getprogname());
6441 list_commands(fp);
6443 exit(status);
6446 static char **
6447 make_argv(int argc, ...)
6449 va_list ap;
6450 char **argv;
6451 int i;
6453 va_start(ap, argc);
6455 argv = calloc(argc, sizeof(char *));
6456 if (argv == NULL)
6457 err(1, "calloc");
6458 for (i = 0; i < argc; i++) {
6459 argv[i] = strdup(va_arg(ap, char *));
6460 if (argv[i] == NULL)
6461 err(1, "strdup");
6464 va_end(ap);
6465 return argv;
6469 * Try to convert 'tog path' into a 'tog log path' command.
6470 * The user could simply have mistyped the command rather than knowingly
6471 * provided a path. So check whether argv[0] can in fact be resolved
6472 * to a path in the HEAD commit and print a special error if not.
6473 * This hack is for mpi@ <3
6475 static const struct got_error *
6476 tog_log_with_path(int argc, char *argv[])
6478 const struct got_error *error = NULL, *close_err;
6479 struct tog_cmd *cmd = NULL;
6480 struct got_repository *repo = NULL;
6481 struct got_worktree *worktree = NULL;
6482 struct got_object_id *commit_id = NULL, *id = NULL;
6483 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6484 char *commit_id_str = NULL, **cmd_argv = NULL;
6486 cwd = getcwd(NULL, 0);
6487 if (cwd == NULL)
6488 return got_error_from_errno("getcwd");
6490 error = got_worktree_open(&worktree, cwd);
6491 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6492 goto done;
6494 if (worktree)
6495 repo_path = strdup(got_worktree_get_repo_path(worktree));
6496 else
6497 repo_path = strdup(cwd);
6498 if (repo_path == NULL) {
6499 error = got_error_from_errno("strdup");
6500 goto done;
6503 error = got_repo_open(&repo, repo_path, NULL);
6504 if (error != NULL)
6505 goto done;
6507 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6508 repo, worktree);
6509 if (error)
6510 goto done;
6512 error = tog_load_refs(repo);
6513 if (error)
6514 goto done;
6515 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6516 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6517 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6518 if (error)
6519 goto done;
6521 if (worktree) {
6522 got_worktree_close(worktree);
6523 worktree = NULL;
6526 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6527 if (error) {
6528 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6529 goto done;
6530 fprintf(stderr, "%s: '%s' is no known command or path\n",
6531 getprogname(), argv[0]);
6532 usage(1, 1);
6533 /* not reached */
6536 close_err = got_repo_close(repo);
6537 if (error == NULL)
6538 error = close_err;
6539 repo = NULL;
6541 error = got_object_id_str(&commit_id_str, commit_id);
6542 if (error)
6543 goto done;
6545 cmd = &tog_commands[0]; /* log */
6546 argc = 4;
6547 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6548 error = cmd->cmd_main(argc, cmd_argv);
6549 done:
6550 if (repo) {
6551 close_err = got_repo_close(repo);
6552 if (error == NULL)
6553 error = close_err;
6555 if (worktree)
6556 got_worktree_close(worktree);
6557 free(id);
6558 free(commit_id_str);
6559 free(commit_id);
6560 free(cwd);
6561 free(repo_path);
6562 free(in_repo_path);
6563 if (cmd_argv) {
6564 int i;
6565 for (i = 0; i < argc; i++)
6566 free(cmd_argv[i]);
6567 free(cmd_argv);
6569 tog_free_refs();
6570 return error;
6574 main(int argc, char *argv[])
6576 const struct got_error *error = NULL;
6577 struct tog_cmd *cmd = NULL;
6578 int ch, hflag = 0, Vflag = 0;
6579 char **cmd_argv = NULL;
6580 static struct option longopts[] = {
6581 { "version", no_argument, NULL, 'V' },
6582 { NULL, 0, NULL, 0}
6585 setlocale(LC_CTYPE, "");
6587 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6588 switch (ch) {
6589 case 'h':
6590 hflag = 1;
6591 break;
6592 case 'V':
6593 Vflag = 1;
6594 break;
6595 default:
6596 usage(hflag, 1);
6597 /* NOTREACHED */
6601 argc -= optind;
6602 argv += optind;
6603 optind = 1;
6604 optreset = 1;
6606 if (Vflag) {
6607 got_version_print_str();
6608 return 0;
6611 #ifndef PROFILE
6612 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6613 NULL) == -1)
6614 err(1, "pledge");
6615 #endif
6617 if (argc == 0) {
6618 if (hflag)
6619 usage(hflag, 0);
6620 /* Build an argument vector which runs a default command. */
6621 cmd = &tog_commands[0];
6622 argc = 1;
6623 cmd_argv = make_argv(argc, cmd->name);
6624 } else {
6625 size_t i;
6627 /* Did the user specify a command? */
6628 for (i = 0; i < nitems(tog_commands); i++) {
6629 if (strncmp(tog_commands[i].name, argv[0],
6630 strlen(argv[0])) == 0) {
6631 cmd = &tog_commands[i];
6632 break;
6637 if (cmd == NULL) {
6638 if (argc != 1)
6639 usage(0, 1);
6640 /* No command specified; try log with a path */
6641 error = tog_log_with_path(argc, argv);
6642 } else {
6643 if (hflag)
6644 cmd->cmd_usage();
6645 else
6646 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6649 endwin();
6650 putchar('\n');
6651 if (cmd_argv) {
6652 int i;
6653 for (i = 0; i < argc; i++)
6654 free(cmd_argv[i]);
6655 free(cmd_argv);
6658 if (error && error->code != GOT_ERR_CANCELLED)
6659 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6660 return 0;