Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #include <curses.h>
25 #include <panel.h>
26 #include <locale.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <getopt.h>
32 #include <string.h>
33 #include <err.h>
34 #include <unistd.h>
35 #include <limits.h>
36 #include <wchar.h>
37 #include <time.h>
38 #include <pthread.h>
39 #include <libgen.h>
40 #include <regex.h>
41 #include <sched.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_diff.h"
49 #include "got_opentemp.h"
50 #include "got_utf8.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
55 #include "got_path.h"
56 #include "got_worktree.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #ifndef MAX
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 #endif
66 #define CTRL(x) ((x) & 0x1f)
68 #ifndef nitems
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #endif
72 struct tog_cmd {
73 const char *name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 };
78 __dead static void usage(int, int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
83 __dead static void usage_ref(void);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
89 static const struct got_error* cmd_ref(int, char *[]);
91 static struct tog_cmd tog_commands[] = {
92 { "log", cmd_log, usage_log },
93 { "diff", cmd_diff, usage_diff },
94 { "blame", cmd_blame, usage_blame },
95 { "tree", cmd_tree, usage_tree },
96 { "ref", cmd_ref, usage_ref },
97 };
99 enum tog_view_type {
100 TOG_VIEW_DIFF,
101 TOG_VIEW_LOG,
102 TOG_VIEW_BLAME,
103 TOG_VIEW_TREE,
104 TOG_VIEW_REF,
105 };
107 #define TOG_EOF_STRING "(END)"
109 struct commit_queue_entry {
110 TAILQ_ENTRY(commit_queue_entry) entry;
111 struct got_object_id *id;
112 struct got_commit_object *commit;
113 int idx;
114 };
115 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 struct commit_queue {
117 int ncommits;
118 struct commit_queue_head head;
119 };
121 struct tog_color {
122 STAILQ_ENTRY(tog_color) entry;
123 regex_t regex;
124 short colorpair;
125 };
126 STAILQ_HEAD(tog_colors, tog_color);
128 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
129 static struct got_reflist_object_id_map *tog_refs_idmap;
131 static const struct got_error *
132 tog_load_refs(struct got_repository *repo, int sort_by_date)
134 const struct got_error *err;
136 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
137 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
138 repo);
139 if (err)
140 return err;
142 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
143 repo);
146 static void
147 tog_free_refs(void)
149 if (tog_refs_idmap) {
150 got_reflist_object_id_map_free(tog_refs_idmap);
151 tog_refs_idmap = NULL;
153 got_ref_list_free(&tog_refs);
156 static const struct got_error *
157 add_color(struct tog_colors *colors, const char *pattern,
158 int idx, short color)
160 const struct got_error *err = NULL;
161 struct tog_color *tc;
162 int regerr = 0;
164 if (idx < 1 || idx > COLOR_PAIRS - 1)
165 return NULL;
167 init_pair(idx, color, -1);
169 tc = calloc(1, sizeof(*tc));
170 if (tc == NULL)
171 return got_error_from_errno("calloc");
172 regerr = regcomp(&tc->regex, pattern,
173 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
174 if (regerr) {
175 static char regerr_msg[512];
176 static char err_msg[512];
177 regerror(regerr, &tc->regex, regerr_msg,
178 sizeof(regerr_msg));
179 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
180 regerr_msg);
181 err = got_error_msg(GOT_ERR_REGEX, err_msg);
182 free(tc);
183 return err;
185 tc->colorpair = idx;
186 STAILQ_INSERT_HEAD(colors, tc, entry);
187 return NULL;
190 static void
191 free_colors(struct tog_colors *colors)
193 struct tog_color *tc;
195 while (!STAILQ_EMPTY(colors)) {
196 tc = STAILQ_FIRST(colors);
197 STAILQ_REMOVE_HEAD(colors, entry);
198 regfree(&tc->regex);
199 free(tc);
203 struct tog_color *
204 get_color(struct tog_colors *colors, int colorpair)
206 struct tog_color *tc = NULL;
208 STAILQ_FOREACH(tc, colors, entry) {
209 if (tc->colorpair == colorpair)
210 return tc;
213 return NULL;
216 static int
217 default_color_value(const char *envvar)
219 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
220 return COLOR_MAGENTA;
221 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
222 return COLOR_CYAN;
223 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
224 return COLOR_YELLOW;
225 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
226 return COLOR_GREEN;
227 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
228 return COLOR_MAGENTA;
229 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
230 return COLOR_MAGENTA;
231 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
232 return COLOR_CYAN;
233 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
234 return COLOR_GREEN;
235 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
236 return COLOR_GREEN;
237 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
238 return COLOR_CYAN;
239 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
240 return COLOR_YELLOW;
241 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
242 return COLOR_GREEN;
243 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
244 return COLOR_MAGENTA;
245 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
246 return COLOR_YELLOW;
248 return -1;
251 static int
252 get_color_value(const char *envvar)
254 const char *val = getenv(envvar);
256 if (val == NULL)
257 return default_color_value(envvar);
259 if (strcasecmp(val, "black") == 0)
260 return COLOR_BLACK;
261 if (strcasecmp(val, "red") == 0)
262 return COLOR_RED;
263 if (strcasecmp(val, "green") == 0)
264 return COLOR_GREEN;
265 if (strcasecmp(val, "yellow") == 0)
266 return COLOR_YELLOW;
267 if (strcasecmp(val, "blue") == 0)
268 return COLOR_BLUE;
269 if (strcasecmp(val, "magenta") == 0)
270 return COLOR_MAGENTA;
271 if (strcasecmp(val, "cyan") == 0)
272 return COLOR_CYAN;
273 if (strcasecmp(val, "white") == 0)
274 return COLOR_WHITE;
275 if (strcasecmp(val, "default") == 0)
276 return -1;
278 return default_color_value(envvar);
282 struct tog_diff_view_state {
283 struct got_object_id *id1, *id2;
284 const char *label1, *label2;
285 FILE *f;
286 int first_displayed_line;
287 int last_displayed_line;
288 int eof;
289 int diff_context;
290 int ignore_whitespace;
291 int force_text_diff;
292 struct got_repository *repo;
293 struct tog_colors colors;
294 size_t nlines;
295 off_t *line_offsets;
296 int matched_line;
297 int selected_line;
299 /* passed from log view; may be NULL */
300 struct tog_view *log_view;
301 };
303 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
305 struct tog_log_thread_args {
306 pthread_cond_t need_commits;
307 pthread_cond_t commit_loaded;
308 int commits_needed;
309 int load_all;
310 struct got_commit_graph *graph;
311 struct commit_queue *commits;
312 const char *in_repo_path;
313 struct got_object_id *start_id;
314 struct got_repository *repo;
315 int log_complete;
316 sig_atomic_t *quit;
317 struct commit_queue_entry **first_displayed_entry;
318 struct commit_queue_entry **selected_entry;
319 int *searching;
320 int *search_next_done;
321 regex_t *regex;
322 };
324 struct tog_log_view_state {
325 struct commit_queue commits;
326 struct commit_queue_entry *first_displayed_entry;
327 struct commit_queue_entry *last_displayed_entry;
328 struct commit_queue_entry *selected_entry;
329 int selected;
330 char *in_repo_path;
331 char *head_ref_name;
332 int log_branches;
333 struct got_repository *repo;
334 struct got_object_id *start_id;
335 sig_atomic_t quit;
336 pthread_t thread;
337 struct tog_log_thread_args thread_args;
338 struct commit_queue_entry *matched_entry;
339 struct commit_queue_entry *search_entry;
340 struct tog_colors colors;
341 };
343 #define TOG_COLOR_DIFF_MINUS 1
344 #define TOG_COLOR_DIFF_PLUS 2
345 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
346 #define TOG_COLOR_DIFF_META 4
347 #define TOG_COLOR_TREE_SUBMODULE 5
348 #define TOG_COLOR_TREE_SYMLINK 6
349 #define TOG_COLOR_TREE_DIRECTORY 7
350 #define TOG_COLOR_TREE_EXECUTABLE 8
351 #define TOG_COLOR_COMMIT 9
352 #define TOG_COLOR_AUTHOR 10
353 #define TOG_COLOR_DATE 11
354 #define TOG_COLOR_REFS_HEADS 12
355 #define TOG_COLOR_REFS_TAGS 13
356 #define TOG_COLOR_REFS_REMOTES 14
358 struct tog_blame_cb_args {
359 struct tog_blame_line *lines; /* one per line */
360 int nlines;
362 struct tog_view *view;
363 struct got_object_id *commit_id;
364 int *quit;
365 };
367 struct tog_blame_thread_args {
368 const char *path;
369 struct got_repository *repo;
370 struct tog_blame_cb_args *cb_args;
371 int *complete;
372 got_cancel_cb cancel_cb;
373 void *cancel_arg;
374 };
376 struct tog_blame {
377 FILE *f;
378 off_t filesize;
379 struct tog_blame_line *lines;
380 int nlines;
381 off_t *line_offsets;
382 pthread_t thread;
383 struct tog_blame_thread_args thread_args;
384 struct tog_blame_cb_args cb_args;
385 const char *path;
386 };
388 struct tog_blame_view_state {
389 int first_displayed_line;
390 int last_displayed_line;
391 int selected_line;
392 int blame_complete;
393 int eof;
394 int done;
395 struct got_object_id_queue blamed_commits;
396 struct got_object_qid *blamed_commit;
397 char *path;
398 struct got_repository *repo;
399 struct got_object_id *commit_id;
400 struct tog_blame blame;
401 int matched_line;
402 struct tog_colors colors;
403 };
405 struct tog_parent_tree {
406 TAILQ_ENTRY(tog_parent_tree) entry;
407 struct got_tree_object *tree;
408 struct got_tree_entry *first_displayed_entry;
409 struct got_tree_entry *selected_entry;
410 int selected;
411 };
413 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
415 struct tog_tree_view_state {
416 char *tree_label;
417 struct got_object_id *commit_id;/* commit which this tree belongs to */
418 struct got_tree_object *root; /* the commit's root tree entry */
419 struct got_tree_object *tree; /* currently displayed (sub-)tree */
420 struct got_tree_entry *first_displayed_entry;
421 struct got_tree_entry *last_displayed_entry;
422 struct got_tree_entry *selected_entry;
423 int ndisplayed, selected, show_ids;
424 struct tog_parent_trees parents; /* parent trees of current sub-tree */
425 char *head_ref_name;
426 struct got_repository *repo;
427 struct got_tree_entry *matched_entry;
428 struct tog_colors colors;
429 };
431 struct tog_reflist_entry {
432 TAILQ_ENTRY(tog_reflist_entry) entry;
433 struct got_reference *ref;
434 int idx;
435 };
437 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
439 struct tog_ref_view_state {
440 struct tog_reflist_head refs;
441 struct tog_reflist_entry *first_displayed_entry;
442 struct tog_reflist_entry *last_displayed_entry;
443 struct tog_reflist_entry *selected_entry;
444 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
445 struct got_repository *repo;
446 struct tog_reflist_entry *matched_entry;
447 struct tog_colors colors;
448 };
450 /*
451 * We implement two types of views: parent views and child views.
453 * The 'Tab' key switches focus between a parent view and its child view.
454 * Child views are shown side-by-side to their parent view, provided
455 * there is enough screen estate.
457 * When a new view is opened from within a parent view, this new view
458 * becomes a child view of the parent view, replacing any existing child.
460 * When a new view is opened from within a child view, this new view
461 * becomes a parent view which will obscure the views below until the
462 * user quits the new parent view by typing 'q'.
464 * This list of views contains parent views only.
465 * Child views are only pointed to by their parent view.
466 */
467 TAILQ_HEAD(tog_view_list_head, tog_view);
469 struct tog_view {
470 TAILQ_ENTRY(tog_view) entry;
471 WINDOW *window;
472 PANEL *panel;
473 int nlines, ncols, begin_y, begin_x;
474 int lines, cols; /* copies of LINES and COLS */
475 int focussed; /* Only set on one parent or child view at a time. */
476 int dying;
477 struct tog_view *parent;
478 struct tog_view *child;
480 /*
481 * This flag is initially set on parent views when a new child view
482 * is created. It gets toggled when the 'Tab' key switches focus
483 * between parent and child.
484 * The flag indicates whether focus should be passed on to our child
485 * view if this parent view gets picked for focus after another parent
486 * view was closed. This prevents child views from losing focus in such
487 * situations.
488 */
489 int focus_child;
491 /* type-specific state */
492 enum tog_view_type type;
493 union {
494 struct tog_diff_view_state diff;
495 struct tog_log_view_state log;
496 struct tog_blame_view_state blame;
497 struct tog_tree_view_state tree;
498 struct tog_ref_view_state ref;
499 } state;
501 const struct got_error *(*show)(struct tog_view *);
502 const struct got_error *(*input)(struct tog_view **,
503 struct tog_view *, int);
504 const struct got_error *(*close)(struct tog_view *);
506 const struct got_error *(*search_start)(struct tog_view *);
507 const struct got_error *(*search_next)(struct tog_view *);
508 int search_started;
509 int searching;
510 #define TOG_SEARCH_FORWARD 1
511 #define TOG_SEARCH_BACKWARD 2
512 int search_next_done;
513 #define TOG_SEARCH_HAVE_MORE 1
514 #define TOG_SEARCH_NO_MORE 2
515 #define TOG_SEARCH_HAVE_NONE 3
516 regex_t regex;
517 regmatch_t regmatch;
518 };
520 static const struct got_error *open_diff_view(struct tog_view *,
521 struct got_object_id *, struct got_object_id *,
522 const char *, const char *, int, int, int, struct tog_view *,
523 struct got_repository *);
524 static const struct got_error *show_diff_view(struct tog_view *);
525 static const struct got_error *input_diff_view(struct tog_view **,
526 struct tog_view *, int);
527 static const struct got_error* close_diff_view(struct tog_view *);
528 static const struct got_error *search_start_diff_view(struct tog_view *);
529 static const struct got_error *search_next_diff_view(struct tog_view *);
531 static const struct got_error *open_log_view(struct tog_view *,
532 struct got_object_id *, struct got_repository *,
533 const char *, const char *, int);
534 static const struct got_error * show_log_view(struct tog_view *);
535 static const struct got_error *input_log_view(struct tog_view **,
536 struct tog_view *, int);
537 static const struct got_error *close_log_view(struct tog_view *);
538 static const struct got_error *search_start_log_view(struct tog_view *);
539 static const struct got_error *search_next_log_view(struct tog_view *);
541 static const struct got_error *open_blame_view(struct tog_view *, char *,
542 struct got_object_id *, struct got_repository *);
543 static const struct got_error *show_blame_view(struct tog_view *);
544 static const struct got_error *input_blame_view(struct tog_view **,
545 struct tog_view *, int);
546 static const struct got_error *close_blame_view(struct tog_view *);
547 static const struct got_error *search_start_blame_view(struct tog_view *);
548 static const struct got_error *search_next_blame_view(struct tog_view *);
550 static const struct got_error *open_tree_view(struct tog_view *,
551 struct got_object_id *, const char *, struct got_repository *);
552 static const struct got_error *show_tree_view(struct tog_view *);
553 static const struct got_error *input_tree_view(struct tog_view **,
554 struct tog_view *, int);
555 static const struct got_error *close_tree_view(struct tog_view *);
556 static const struct got_error *search_start_tree_view(struct tog_view *);
557 static const struct got_error *search_next_tree_view(struct tog_view *);
559 static const struct got_error *open_ref_view(struct tog_view *,
560 struct got_repository *);
561 static const struct got_error *show_ref_view(struct tog_view *);
562 static const struct got_error *input_ref_view(struct tog_view **,
563 struct tog_view *, int);
564 static const struct got_error *close_ref_view(struct tog_view *);
565 static const struct got_error *search_start_ref_view(struct tog_view *);
566 static const struct got_error *search_next_ref_view(struct tog_view *);
568 static volatile sig_atomic_t tog_sigwinch_received;
569 static volatile sig_atomic_t tog_sigpipe_received;
570 static volatile sig_atomic_t tog_sigcont_received;
572 static void
573 tog_sigwinch(int signo)
575 tog_sigwinch_received = 1;
578 static void
579 tog_sigpipe(int signo)
581 tog_sigpipe_received = 1;
584 static void
585 tog_sigcont(int signo)
587 tog_sigcont_received = 1;
590 static const struct got_error *
591 view_close(struct tog_view *view)
593 const struct got_error *err = NULL;
595 if (view->child) {
596 view_close(view->child);
597 view->child = NULL;
599 if (view->close)
600 err = view->close(view);
601 if (view->panel)
602 del_panel(view->panel);
603 if (view->window)
604 delwin(view->window);
605 free(view);
606 return err;
609 static struct tog_view *
610 view_open(int nlines, int ncols, int begin_y, int begin_x,
611 enum tog_view_type type)
613 struct tog_view *view = calloc(1, sizeof(*view));
615 if (view == NULL)
616 return NULL;
618 view->type = type;
619 view->lines = LINES;
620 view->cols = COLS;
621 view->nlines = nlines ? nlines : LINES - begin_y;
622 view->ncols = ncols ? ncols : COLS - begin_x;
623 view->begin_y = begin_y;
624 view->begin_x = begin_x;
625 view->window = newwin(nlines, ncols, begin_y, begin_x);
626 if (view->window == NULL) {
627 view_close(view);
628 return NULL;
630 view->panel = new_panel(view->window);
631 if (view->panel == NULL ||
632 set_panel_userptr(view->panel, view) != OK) {
633 view_close(view);
634 return NULL;
637 keypad(view->window, TRUE);
638 return view;
641 static int
642 view_split_begin_x(int begin_x)
644 if (begin_x > 0 || COLS < 120)
645 return 0;
646 return (COLS - MAX(COLS / 2, 80));
649 static const struct got_error *view_resize(struct tog_view *);
651 static const struct got_error *
652 view_splitscreen(struct tog_view *view)
654 const struct got_error *err = NULL;
656 view->begin_y = 0;
657 view->begin_x = view_split_begin_x(0);
658 view->nlines = LINES;
659 view->ncols = COLS - view->begin_x;
660 view->lines = LINES;
661 view->cols = COLS;
662 err = view_resize(view);
663 if (err)
664 return err;
666 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
667 return got_error_from_errno("mvwin");
669 return NULL;
672 static const struct got_error *
673 view_fullscreen(struct tog_view *view)
675 const struct got_error *err = NULL;
677 view->begin_x = 0;
678 view->begin_y = 0;
679 view->nlines = LINES;
680 view->ncols = COLS;
681 view->lines = LINES;
682 view->cols = COLS;
683 err = view_resize(view);
684 if (err)
685 return err;
687 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
688 return got_error_from_errno("mvwin");
690 return NULL;
693 static int
694 view_is_parent_view(struct tog_view *view)
696 return view->parent == NULL;
699 static const struct got_error *
700 view_resize(struct tog_view *view)
702 int nlines, ncols;
704 if (view->lines > LINES)
705 nlines = view->nlines - (view->lines - LINES);
706 else
707 nlines = view->nlines + (LINES - view->lines);
709 if (view->cols > COLS)
710 ncols = view->ncols - (view->cols - COLS);
711 else
712 ncols = view->ncols + (COLS - view->cols);
714 if (wresize(view->window, nlines, ncols) == ERR)
715 return got_error_from_errno("wresize");
716 if (replace_panel(view->panel, view->window) == ERR)
717 return got_error_from_errno("replace_panel");
718 wclear(view->window);
720 view->nlines = nlines;
721 view->ncols = ncols;
722 view->lines = LINES;
723 view->cols = COLS;
725 if (view->child) {
726 view->child->begin_x = view_split_begin_x(view->begin_x);
727 if (view->child->begin_x == 0) {
728 view_fullscreen(view->child);
729 if (view->child->focussed)
730 show_panel(view->child->panel);
731 else
732 show_panel(view->panel);
733 } else {
734 view_splitscreen(view->child);
735 show_panel(view->child->panel);
739 return NULL;
742 static const struct got_error *
743 view_close_child(struct tog_view *view)
745 const struct got_error *err = NULL;
747 if (view->child == NULL)
748 return NULL;
750 err = view_close(view->child);
751 view->child = NULL;
752 return err;
755 static void
756 view_set_child(struct tog_view *view, struct tog_view *child)
758 view->child = child;
759 child->parent = view;
762 static int
763 view_is_splitscreen(struct tog_view *view)
765 return view->begin_x > 0;
768 static void
769 tog_resizeterm(void)
771 int cols, lines;
772 struct winsize size;
774 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
775 cols = 80; /* Default */
776 lines = 24;
777 } else {
778 cols = size.ws_col;
779 lines = size.ws_row;
781 resize_term(lines, cols);
784 static const struct got_error *
785 view_search_start(struct tog_view *view)
787 const struct got_error *err = NULL;
788 char pattern[1024];
789 int ret;
791 if (view->search_started) {
792 regfree(&view->regex);
793 view->searching = 0;
794 memset(&view->regmatch, 0, sizeof(view->regmatch));
796 view->search_started = 0;
798 if (view->nlines < 1)
799 return NULL;
801 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
802 wclrtoeol(view->window);
804 nocbreak();
805 echo();
806 ret = wgetnstr(view->window, pattern, sizeof(pattern));
807 cbreak();
808 noecho();
809 if (ret == ERR)
810 return NULL;
812 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
813 err = view->search_start(view);
814 if (err) {
815 regfree(&view->regex);
816 return err;
818 view->search_started = 1;
819 view->searching = TOG_SEARCH_FORWARD;
820 view->search_next_done = 0;
821 view->search_next(view);
824 return NULL;
827 static const struct got_error *
828 view_input(struct tog_view **new, int *done, struct tog_view *view,
829 struct tog_view_list_head *views)
831 const struct got_error *err = NULL;
832 struct tog_view *v;
833 int ch, errcode;
835 *new = NULL;
837 /* Clear "no matches" indicator. */
838 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
839 view->search_next_done == TOG_SEARCH_HAVE_NONE)
840 view->search_next_done = TOG_SEARCH_HAVE_MORE;
842 if (view->searching && !view->search_next_done) {
843 errcode = pthread_mutex_unlock(&tog_mutex);
844 if (errcode)
845 return got_error_set_errno(errcode,
846 "pthread_mutex_unlock");
847 sched_yield();
848 errcode = pthread_mutex_lock(&tog_mutex);
849 if (errcode)
850 return got_error_set_errno(errcode,
851 "pthread_mutex_lock");
852 view->search_next(view);
853 return NULL;
856 nodelay(stdscr, FALSE);
857 /* Allow threads to make progress while we are waiting for input. */
858 errcode = pthread_mutex_unlock(&tog_mutex);
859 if (errcode)
860 return got_error_set_errno(errcode, "pthread_mutex_unlock");
861 ch = wgetch(view->window);
862 errcode = pthread_mutex_lock(&tog_mutex);
863 if (errcode)
864 return got_error_set_errno(errcode, "pthread_mutex_lock");
865 nodelay(stdscr, TRUE);
867 if (tog_sigwinch_received || tog_sigcont_received) {
868 tog_resizeterm();
869 tog_sigwinch_received = 0;
870 tog_sigcont_received = 0;
871 TAILQ_FOREACH(v, views, entry) {
872 err = view_resize(v);
873 if (err)
874 return err;
875 err = v->input(new, v, KEY_RESIZE);
876 if (err)
877 return err;
878 if (v->child) {
879 err = view_resize(v->child);
880 if (err)
881 return err;
882 err = v->child->input(new, v->child,
883 KEY_RESIZE);
884 if (err)
885 return err;
890 switch (ch) {
891 case '\t':
892 if (view->child) {
893 view->focussed = 0;
894 view->child->focussed = 1;
895 view->focus_child = 1;
896 } else if (view->parent) {
897 view->focussed = 0;
898 view->parent->focussed = 1;
899 view->parent->focus_child = 0;
901 break;
902 case 'q':
903 err = view->input(new, view, ch);
904 view->dying = 1;
905 break;
906 case 'Q':
907 *done = 1;
908 break;
909 case 'f':
910 if (view_is_parent_view(view)) {
911 if (view->child == NULL)
912 break;
913 if (view_is_splitscreen(view->child)) {
914 view->focussed = 0;
915 view->child->focussed = 1;
916 err = view_fullscreen(view->child);
917 } else
918 err = view_splitscreen(view->child);
919 if (err)
920 break;
921 err = view->child->input(new, view->child,
922 KEY_RESIZE);
923 } else {
924 if (view_is_splitscreen(view)) {
925 view->parent->focussed = 0;
926 view->focussed = 1;
927 err = view_fullscreen(view);
928 } else {
929 err = view_splitscreen(view);
931 if (err)
932 break;
933 err = view->input(new, view, KEY_RESIZE);
935 break;
936 case KEY_RESIZE:
937 break;
938 case '/':
939 if (view->search_start)
940 view_search_start(view);
941 else
942 err = view->input(new, view, ch);
943 break;
944 case 'N':
945 case 'n':
946 if (view->search_started && view->search_next) {
947 view->searching = (ch == 'n' ?
948 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
949 view->search_next_done = 0;
950 view->search_next(view);
951 } else
952 err = view->input(new, view, ch);
953 break;
954 default:
955 err = view->input(new, view, ch);
956 break;
959 return err;
962 void
963 view_vborder(struct tog_view *view)
965 PANEL *panel;
966 const struct tog_view *view_above;
968 if (view->parent)
969 return view_vborder(view->parent);
971 panel = panel_above(view->panel);
972 if (panel == NULL)
973 return;
975 view_above = panel_userptr(panel);
976 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
977 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
980 int
981 view_needs_focus_indication(struct tog_view *view)
983 if (view_is_parent_view(view)) {
984 if (view->child == NULL || view->child->focussed)
985 return 0;
986 if (!view_is_splitscreen(view->child))
987 return 0;
988 } else if (!view_is_splitscreen(view))
989 return 0;
991 return view->focussed;
994 static const struct got_error *
995 view_loop(struct tog_view *view)
997 const struct got_error *err = NULL;
998 struct tog_view_list_head views;
999 struct tog_view *new_view;
1000 int fast_refresh = 10;
1001 int done = 0, errcode;
1003 errcode = pthread_mutex_lock(&tog_mutex);
1004 if (errcode)
1005 return got_error_set_errno(errcode, "pthread_mutex_lock");
1007 TAILQ_INIT(&views);
1008 TAILQ_INSERT_HEAD(&views, view, entry);
1010 view->focussed = 1;
1011 err = view->show(view);
1012 if (err)
1013 return err;
1014 update_panels();
1015 doupdate();
1016 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1017 /* Refresh fast during initialization, then become slower. */
1018 if (fast_refresh && fast_refresh-- == 0)
1019 halfdelay(10); /* switch to once per second */
1021 err = view_input(&new_view, &done, view, &views);
1022 if (err)
1023 break;
1024 if (view->dying) {
1025 struct tog_view *v, *prev = NULL;
1027 if (view_is_parent_view(view))
1028 prev = TAILQ_PREV(view, tog_view_list_head,
1029 entry);
1030 else if (view->parent)
1031 prev = view->parent;
1033 if (view->parent) {
1034 view->parent->child = NULL;
1035 view->parent->focus_child = 0;
1036 } else
1037 TAILQ_REMOVE(&views, view, entry);
1039 err = view_close(view);
1040 if (err)
1041 goto done;
1043 view = NULL;
1044 TAILQ_FOREACH(v, &views, entry) {
1045 if (v->focussed)
1046 break;
1048 if (view == NULL && new_view == NULL) {
1049 /* No view has focus. Try to pick one. */
1050 if (prev)
1051 view = prev;
1052 else if (!TAILQ_EMPTY(&views)) {
1053 view = TAILQ_LAST(&views,
1054 tog_view_list_head);
1056 if (view) {
1057 if (view->focus_child) {
1058 view->child->focussed = 1;
1059 view = view->child;
1060 } else
1061 view->focussed = 1;
1065 if (new_view) {
1066 struct tog_view *v, *t;
1067 /* Only allow one parent view per type. */
1068 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1069 if (v->type != new_view->type)
1070 continue;
1071 TAILQ_REMOVE(&views, v, entry);
1072 err = view_close(v);
1073 if (err)
1074 goto done;
1075 break;
1077 TAILQ_INSERT_TAIL(&views, new_view, entry);
1078 view = new_view;
1080 if (view) {
1081 if (view_is_parent_view(view)) {
1082 if (view->child && view->child->focussed)
1083 view = view->child;
1084 } else {
1085 if (view->parent && view->parent->focussed)
1086 view = view->parent;
1088 show_panel(view->panel);
1089 if (view->child && view_is_splitscreen(view->child))
1090 show_panel(view->child->panel);
1091 if (view->parent && view_is_splitscreen(view)) {
1092 err = view->parent->show(view->parent);
1093 if (err)
1094 goto done;
1096 err = view->show(view);
1097 if (err)
1098 goto done;
1099 if (view->child) {
1100 err = view->child->show(view->child);
1101 if (err)
1102 goto done;
1104 update_panels();
1105 doupdate();
1108 done:
1109 while (!TAILQ_EMPTY(&views)) {
1110 view = TAILQ_FIRST(&views);
1111 TAILQ_REMOVE(&views, view, entry);
1112 view_close(view);
1115 errcode = pthread_mutex_unlock(&tog_mutex);
1116 if (errcode && err == NULL)
1117 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1119 return err;
1122 __dead static void
1123 usage_log(void)
1125 endwin();
1126 fprintf(stderr,
1127 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1128 getprogname());
1129 exit(1);
1132 /* Create newly allocated wide-character string equivalent to a byte string. */
1133 static const struct got_error *
1134 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1136 char *vis = NULL;
1137 const struct got_error *err = NULL;
1139 *ws = NULL;
1140 *wlen = mbstowcs(NULL, s, 0);
1141 if (*wlen == (size_t)-1) {
1142 int vislen;
1143 if (errno != EILSEQ)
1144 return got_error_from_errno("mbstowcs");
1146 /* byte string invalid in current encoding; try to "fix" it */
1147 err = got_mbsavis(&vis, &vislen, s);
1148 if (err)
1149 return err;
1150 *wlen = mbstowcs(NULL, vis, 0);
1151 if (*wlen == (size_t)-1) {
1152 err = got_error_from_errno("mbstowcs"); /* give up */
1153 goto done;
1157 *ws = calloc(*wlen + 1, sizeof(**ws));
1158 if (*ws == NULL) {
1159 err = got_error_from_errno("calloc");
1160 goto done;
1163 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1164 err = got_error_from_errno("mbstowcs");
1165 done:
1166 free(vis);
1167 if (err) {
1168 free(*ws);
1169 *ws = NULL;
1170 *wlen = 0;
1172 return err;
1175 /* Format a line for display, ensuring that it won't overflow a width limit. */
1176 static const struct got_error *
1177 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1178 int col_tab_align)
1180 const struct got_error *err = NULL;
1181 int cols = 0;
1182 wchar_t *wline = NULL;
1183 size_t wlen;
1184 int i;
1186 *wlinep = NULL;
1187 *widthp = 0;
1189 err = mbs2ws(&wline, &wlen, line);
1190 if (err)
1191 return err;
1193 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1194 wline[wlen - 1] = L'\0';
1195 wlen--;
1197 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1198 wline[wlen - 1] = L'\0';
1199 wlen--;
1202 i = 0;
1203 while (i < wlen) {
1204 int width = wcwidth(wline[i]);
1206 if (width == 0) {
1207 i++;
1208 continue;
1211 if (width == 1 || width == 2) {
1212 if (cols + width > wlimit)
1213 break;
1214 cols += width;
1215 i++;
1216 } else if (width == -1) {
1217 if (wline[i] == L'\t') {
1218 width = TABSIZE -
1219 ((cols + col_tab_align) % TABSIZE);
1220 } else {
1221 width = 1;
1222 wline[i] = L'.';
1224 if (cols + width > wlimit)
1225 break;
1226 cols += width;
1227 i++;
1228 } else {
1229 err = got_error_from_errno("wcwidth");
1230 goto done;
1233 wline[i] = L'\0';
1234 if (widthp)
1235 *widthp = cols;
1236 done:
1237 if (err)
1238 free(wline);
1239 else
1240 *wlinep = wline;
1241 return err;
1244 static const struct got_error*
1245 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1246 struct got_object_id *id, struct got_repository *repo)
1248 static const struct got_error *err = NULL;
1249 struct got_reflist_entry *re;
1250 char *s;
1251 const char *name;
1253 *refs_str = NULL;
1255 TAILQ_FOREACH(re, refs, entry) {
1256 struct got_tag_object *tag = NULL;
1257 struct got_object_id *ref_id;
1258 int cmp;
1260 name = got_ref_get_name(re->ref);
1261 if (strcmp(name, GOT_REF_HEAD) == 0)
1262 continue;
1263 if (strncmp(name, "refs/", 5) == 0)
1264 name += 5;
1265 if (strncmp(name, "got/", 4) == 0)
1266 continue;
1267 if (strncmp(name, "heads/", 6) == 0)
1268 name += 6;
1269 if (strncmp(name, "remotes/", 8) == 0) {
1270 name += 8;
1271 s = strstr(name, "/" GOT_REF_HEAD);
1272 if (s != NULL && s[strlen(s)] == '\0')
1273 continue;
1275 err = got_ref_resolve(&ref_id, repo, re->ref);
1276 if (err)
1277 break;
1278 if (strncmp(name, "tags/", 5) == 0) {
1279 err = got_object_open_as_tag(&tag, repo, ref_id);
1280 if (err) {
1281 if (err->code != GOT_ERR_OBJ_TYPE) {
1282 free(ref_id);
1283 break;
1285 /* Ref points at something other than a tag. */
1286 err = NULL;
1287 tag = NULL;
1290 cmp = got_object_id_cmp(tag ?
1291 got_object_tag_get_object_id(tag) : ref_id, id);
1292 free(ref_id);
1293 if (tag)
1294 got_object_tag_close(tag);
1295 if (cmp != 0)
1296 continue;
1297 s = *refs_str;
1298 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1299 s ? ", " : "", name) == -1) {
1300 err = got_error_from_errno("asprintf");
1301 free(s);
1302 *refs_str = NULL;
1303 break;
1305 free(s);
1308 return err;
1311 static const struct got_error *
1312 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1313 int col_tab_align)
1315 char *smallerthan;
1317 smallerthan = strchr(author, '<');
1318 if (smallerthan && smallerthan[1] != '\0')
1319 author = smallerthan + 1;
1320 author[strcspn(author, "@>")] = '\0';
1321 return format_line(wauthor, author_width, author, limit, col_tab_align);
1324 static const struct got_error *
1325 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1326 struct got_object_id *id, const size_t date_display_cols,
1327 int author_display_cols)
1329 struct tog_log_view_state *s = &view->state.log;
1330 const struct got_error *err = NULL;
1331 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1332 char *logmsg0 = NULL, *logmsg = NULL;
1333 char *author = NULL;
1334 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1335 int author_width, logmsg_width;
1336 char *newline, *line = NULL;
1337 int col, limit;
1338 const int avail = view->ncols;
1339 struct tm tm;
1340 time_t committer_time;
1341 struct tog_color *tc;
1343 committer_time = got_object_commit_get_committer_time(commit);
1344 if (gmtime_r(&committer_time, &tm) == NULL)
1345 return got_error_from_errno("gmtime_r");
1346 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1347 return got_error(GOT_ERR_NO_SPACE);
1349 if (avail <= date_display_cols)
1350 limit = MIN(sizeof(datebuf) - 1, avail);
1351 else
1352 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1353 tc = get_color(&s->colors, TOG_COLOR_DATE);
1354 if (tc)
1355 wattr_on(view->window,
1356 COLOR_PAIR(tc->colorpair), NULL);
1357 waddnstr(view->window, datebuf, limit);
1358 if (tc)
1359 wattr_off(view->window,
1360 COLOR_PAIR(tc->colorpair), NULL);
1361 col = limit;
1362 if (col > avail)
1363 goto done;
1365 if (avail >= 120) {
1366 char *id_str;
1367 err = got_object_id_str(&id_str, id);
1368 if (err)
1369 goto done;
1370 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1371 if (tc)
1372 wattr_on(view->window,
1373 COLOR_PAIR(tc->colorpair), NULL);
1374 wprintw(view->window, "%.8s ", id_str);
1375 if (tc)
1376 wattr_off(view->window,
1377 COLOR_PAIR(tc->colorpair), NULL);
1378 free(id_str);
1379 col += 9;
1380 if (col > avail)
1381 goto done;
1384 author = strdup(got_object_commit_get_author(commit));
1385 if (author == NULL) {
1386 err = got_error_from_errno("strdup");
1387 goto done;
1389 err = format_author(&wauthor, &author_width, author, avail - col, col);
1390 if (err)
1391 goto done;
1392 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1393 if (tc)
1394 wattr_on(view->window,
1395 COLOR_PAIR(tc->colorpair), NULL);
1396 waddwstr(view->window, wauthor);
1397 if (tc)
1398 wattr_off(view->window,
1399 COLOR_PAIR(tc->colorpair), NULL);
1400 col += author_width;
1401 while (col < avail && author_width < author_display_cols + 2) {
1402 waddch(view->window, ' ');
1403 col++;
1404 author_width++;
1406 if (col > avail)
1407 goto done;
1409 err = got_object_commit_get_logmsg(&logmsg0, commit);
1410 if (err)
1411 goto done;
1412 logmsg = logmsg0;
1413 while (*logmsg == '\n')
1414 logmsg++;
1415 newline = strchr(logmsg, '\n');
1416 if (newline)
1417 *newline = '\0';
1418 limit = avail - col;
1419 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1420 if (err)
1421 goto done;
1422 waddwstr(view->window, wlogmsg);
1423 col += logmsg_width;
1424 while (col < avail) {
1425 waddch(view->window, ' ');
1426 col++;
1428 done:
1429 free(logmsg0);
1430 free(wlogmsg);
1431 free(author);
1432 free(wauthor);
1433 free(line);
1434 return err;
1437 static struct commit_queue_entry *
1438 alloc_commit_queue_entry(struct got_commit_object *commit,
1439 struct got_object_id *id)
1441 struct commit_queue_entry *entry;
1443 entry = calloc(1, sizeof(*entry));
1444 if (entry == NULL)
1445 return NULL;
1447 entry->id = id;
1448 entry->commit = commit;
1449 return entry;
1452 static void
1453 pop_commit(struct commit_queue *commits)
1455 struct commit_queue_entry *entry;
1457 entry = TAILQ_FIRST(&commits->head);
1458 TAILQ_REMOVE(&commits->head, entry, entry);
1459 got_object_commit_close(entry->commit);
1460 commits->ncommits--;
1461 /* Don't free entry->id! It is owned by the commit graph. */
1462 free(entry);
1465 static void
1466 free_commits(struct commit_queue *commits)
1468 while (!TAILQ_EMPTY(&commits->head))
1469 pop_commit(commits);
1472 static const struct got_error *
1473 match_commit(int *have_match, struct got_object_id *id,
1474 struct got_commit_object *commit, regex_t *regex)
1476 const struct got_error *err = NULL;
1477 regmatch_t regmatch;
1478 char *id_str = NULL, *logmsg = NULL;
1480 *have_match = 0;
1482 err = got_object_id_str(&id_str, id);
1483 if (err)
1484 return err;
1486 err = got_object_commit_get_logmsg(&logmsg, commit);
1487 if (err)
1488 goto done;
1490 if (regexec(regex, got_object_commit_get_author(commit), 1,
1491 &regmatch, 0) == 0 ||
1492 regexec(regex, got_object_commit_get_committer(commit), 1,
1493 &regmatch, 0) == 0 ||
1494 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1495 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1496 *have_match = 1;
1497 done:
1498 free(id_str);
1499 free(logmsg);
1500 return err;
1503 static const struct got_error *
1504 queue_commits(struct tog_log_thread_args *a)
1506 const struct got_error *err = NULL;
1509 * We keep all commits open throughout the lifetime of the log
1510 * view in order to avoid having to re-fetch commits from disk
1511 * while updating the display.
1513 do {
1514 struct got_object_id *id;
1515 struct got_commit_object *commit;
1516 struct commit_queue_entry *entry;
1517 int errcode;
1519 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1520 NULL, NULL);
1521 if (err || id == NULL)
1522 break;
1524 err = got_object_open_as_commit(&commit, a->repo, id);
1525 if (err)
1526 break;
1527 entry = alloc_commit_queue_entry(commit, id);
1528 if (entry == NULL) {
1529 err = got_error_from_errno("alloc_commit_queue_entry");
1530 break;
1533 errcode = pthread_mutex_lock(&tog_mutex);
1534 if (errcode) {
1535 err = got_error_set_errno(errcode,
1536 "pthread_mutex_lock");
1537 break;
1540 entry->idx = a->commits->ncommits;
1541 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1542 a->commits->ncommits++;
1544 if (*a->searching == TOG_SEARCH_FORWARD &&
1545 !*a->search_next_done) {
1546 int have_match;
1547 err = match_commit(&have_match, id, commit, a->regex);
1548 if (err)
1549 break;
1550 if (have_match)
1551 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1554 errcode = pthread_mutex_unlock(&tog_mutex);
1555 if (errcode && err == NULL)
1556 err = got_error_set_errno(errcode,
1557 "pthread_mutex_unlock");
1558 if (err)
1559 break;
1560 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1562 return err;
1565 static void
1566 select_commit(struct tog_log_view_state *s)
1568 struct commit_queue_entry *entry;
1569 int ncommits = 0;
1571 entry = s->first_displayed_entry;
1572 while (entry) {
1573 if (ncommits == s->selected) {
1574 s->selected_entry = entry;
1575 break;
1577 entry = TAILQ_NEXT(entry, entry);
1578 ncommits++;
1582 static const struct got_error *
1583 draw_commits(struct tog_view *view)
1585 const struct got_error *err = NULL;
1586 struct tog_log_view_state *s = &view->state.log;
1587 struct commit_queue_entry *entry = s->selected_entry;
1588 const int limit = view->nlines;
1589 int width;
1590 int ncommits, author_cols = 4;
1591 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1592 char *refs_str = NULL;
1593 wchar_t *wline;
1594 struct tog_color *tc;
1595 static const size_t date_display_cols = 12;
1597 if (s->selected_entry &&
1598 !(view->searching && view->search_next_done == 0)) {
1599 struct got_reflist_head *refs;
1600 err = got_object_id_str(&id_str, s->selected_entry->id);
1601 if (err)
1602 return err;
1603 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1604 s->selected_entry->id);
1605 if (refs) {
1606 err = build_refs_str(&refs_str, refs,
1607 s->selected_entry->id, s->repo);
1608 if (err)
1609 goto done;
1613 if (s->thread_args.commits_needed == 0)
1614 halfdelay(10); /* disable fast refresh */
1616 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1617 if (asprintf(&ncommits_str, " [%d/%d] %s",
1618 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1619 (view->searching && !view->search_next_done) ?
1620 "searching..." : "loading...") == -1) {
1621 err = got_error_from_errno("asprintf");
1622 goto done;
1624 } else {
1625 const char *search_str = NULL;
1627 if (view->searching) {
1628 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1629 search_str = "no more matches";
1630 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1631 search_str = "no matches found";
1632 else if (!view->search_next_done)
1633 search_str = "searching...";
1636 if (asprintf(&ncommits_str, " [%d/%d] %s",
1637 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1638 search_str ? search_str :
1639 (refs_str ? refs_str : "")) == -1) {
1640 err = got_error_from_errno("asprintf");
1641 goto done;
1645 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1646 if (asprintf(&header, "commit %s %s%s",
1647 id_str ? id_str : "........................................",
1648 s->in_repo_path, ncommits_str) == -1) {
1649 err = got_error_from_errno("asprintf");
1650 header = NULL;
1651 goto done;
1653 } else if (asprintf(&header, "commit %s%s",
1654 id_str ? id_str : "........................................",
1655 ncommits_str) == -1) {
1656 err = got_error_from_errno("asprintf");
1657 header = NULL;
1658 goto done;
1660 err = format_line(&wline, &width, header, view->ncols, 0);
1661 if (err)
1662 goto done;
1664 werase(view->window);
1666 if (view_needs_focus_indication(view))
1667 wstandout(view->window);
1668 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1669 if (tc)
1670 wattr_on(view->window,
1671 COLOR_PAIR(tc->colorpair), NULL);
1672 waddwstr(view->window, wline);
1673 if (tc)
1674 wattr_off(view->window,
1675 COLOR_PAIR(tc->colorpair), NULL);
1676 while (width < view->ncols) {
1677 waddch(view->window, ' ');
1678 width++;
1680 if (view_needs_focus_indication(view))
1681 wstandend(view->window);
1682 free(wline);
1683 if (limit <= 1)
1684 goto done;
1686 /* Grow author column size if necessary. */
1687 entry = s->first_displayed_entry;
1688 ncommits = 0;
1689 while (entry) {
1690 char *author;
1691 wchar_t *wauthor;
1692 int width;
1693 if (ncommits >= limit - 1)
1694 break;
1695 author = strdup(got_object_commit_get_author(entry->commit));
1696 if (author == NULL) {
1697 err = got_error_from_errno("strdup");
1698 goto done;
1700 err = format_author(&wauthor, &width, author, COLS,
1701 date_display_cols);
1702 if (author_cols < width)
1703 author_cols = width;
1704 free(wauthor);
1705 free(author);
1706 ncommits++;
1707 entry = TAILQ_NEXT(entry, entry);
1710 entry = s->first_displayed_entry;
1711 s->last_displayed_entry = s->first_displayed_entry;
1712 ncommits = 0;
1713 while (entry) {
1714 if (ncommits >= limit - 1)
1715 break;
1716 if (ncommits == s->selected)
1717 wstandout(view->window);
1718 err = draw_commit(view, entry->commit, entry->id,
1719 date_display_cols, author_cols);
1720 if (ncommits == s->selected)
1721 wstandend(view->window);
1722 if (err)
1723 goto done;
1724 ncommits++;
1725 s->last_displayed_entry = entry;
1726 entry = TAILQ_NEXT(entry, entry);
1729 view_vborder(view);
1730 done:
1731 free(id_str);
1732 free(refs_str);
1733 free(ncommits_str);
1734 free(header);
1735 return err;
1738 static void
1739 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1741 struct commit_queue_entry *entry;
1742 int nscrolled = 0;
1744 entry = TAILQ_FIRST(&s->commits.head);
1745 if (s->first_displayed_entry == entry)
1746 return;
1748 entry = s->first_displayed_entry;
1749 while (entry && nscrolled < maxscroll) {
1750 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1751 if (entry) {
1752 s->first_displayed_entry = entry;
1753 nscrolled++;
1758 static const struct got_error *
1759 trigger_log_thread(struct tog_view *view, int wait)
1761 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1762 int errcode;
1764 halfdelay(1); /* fast refresh while loading commits */
1766 while (ta->commits_needed > 0 || ta->load_all) {
1767 if (ta->log_complete)
1768 break;
1770 /* Wake the log thread. */
1771 errcode = pthread_cond_signal(&ta->need_commits);
1772 if (errcode)
1773 return got_error_set_errno(errcode,
1774 "pthread_cond_signal");
1777 * The mutex will be released while the view loop waits
1778 * in wgetch(), at which time the log thread will run.
1780 if (!wait)
1781 break;
1783 /* Display progress update in log view. */
1784 show_log_view(view);
1785 update_panels();
1786 doupdate();
1788 /* Wait right here while next commit is being loaded. */
1789 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1790 if (errcode)
1791 return got_error_set_errno(errcode,
1792 "pthread_cond_wait");
1794 /* Display progress update in log view. */
1795 show_log_view(view);
1796 update_panels();
1797 doupdate();
1800 return NULL;
1803 static const struct got_error *
1804 log_scroll_down(struct tog_view *view, int maxscroll)
1806 struct tog_log_view_state *s = &view->state.log;
1807 const struct got_error *err = NULL;
1808 struct commit_queue_entry *pentry;
1809 int nscrolled = 0, ncommits_needed;
1811 if (s->last_displayed_entry == NULL)
1812 return NULL;
1814 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1815 if (s->commits.ncommits < ncommits_needed &&
1816 !s->thread_args.log_complete) {
1818 * Ask the log thread for required amount of commits.
1820 s->thread_args.commits_needed += maxscroll;
1821 err = trigger_log_thread(view, 1);
1822 if (err)
1823 return err;
1826 do {
1827 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1828 if (pentry == NULL)
1829 break;
1831 s->last_displayed_entry = pentry;
1833 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1834 if (pentry == NULL)
1835 break;
1836 s->first_displayed_entry = pentry;
1837 } while (++nscrolled < maxscroll);
1839 return err;
1842 static const struct got_error *
1843 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1844 struct got_commit_object *commit, struct got_object_id *commit_id,
1845 struct tog_view *log_view, struct got_repository *repo)
1847 const struct got_error *err;
1848 struct got_object_qid *parent_id;
1849 struct tog_view *diff_view;
1851 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1852 if (diff_view == NULL)
1853 return got_error_from_errno("view_open");
1855 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1856 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1857 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1858 if (err == NULL)
1859 *new_view = diff_view;
1860 return err;
1863 static const struct got_error *
1864 tree_view_visit_subtree(struct tog_tree_view_state *s,
1865 struct got_tree_object *subtree)
1867 struct tog_parent_tree *parent;
1869 parent = calloc(1, sizeof(*parent));
1870 if (parent == NULL)
1871 return got_error_from_errno("calloc");
1873 parent->tree = s->tree;
1874 parent->first_displayed_entry = s->first_displayed_entry;
1875 parent->selected_entry = s->selected_entry;
1876 parent->selected = s->selected;
1877 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1878 s->tree = subtree;
1879 s->selected = 0;
1880 s->first_displayed_entry = NULL;
1881 return NULL;
1884 static const struct got_error *
1885 tree_view_walk_path(struct tog_tree_view_state *s,
1886 struct got_object_id *commit_id, const char *path)
1888 const struct got_error *err = NULL;
1889 struct got_tree_object *tree = NULL;
1890 const char *p;
1891 char *slash, *subpath = NULL;
1893 /* Walk the path and open corresponding tree objects. */
1894 p = path;
1895 while (*p) {
1896 struct got_tree_entry *te;
1897 struct got_object_id *tree_id;
1898 char *te_name;
1900 while (p[0] == '/')
1901 p++;
1903 /* Ensure the correct subtree entry is selected. */
1904 slash = strchr(p, '/');
1905 if (slash == NULL)
1906 te_name = strdup(p);
1907 else
1908 te_name = strndup(p, slash - p);
1909 if (te_name == NULL) {
1910 err = got_error_from_errno("strndup");
1911 break;
1913 te = got_object_tree_find_entry(s->tree, te_name);
1914 if (te == NULL) {
1915 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1916 free(te_name);
1917 break;
1919 free(te_name);
1920 s->first_displayed_entry = s->selected_entry = te;
1922 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1923 break; /* jump to this file's entry */
1925 slash = strchr(p, '/');
1926 if (slash)
1927 subpath = strndup(path, slash - path);
1928 else
1929 subpath = strdup(path);
1930 if (subpath == NULL) {
1931 err = got_error_from_errno("strdup");
1932 break;
1935 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1936 subpath);
1937 if (err)
1938 break;
1940 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1941 free(tree_id);
1942 if (err)
1943 break;
1945 err = tree_view_visit_subtree(s, tree);
1946 if (err) {
1947 got_object_tree_close(tree);
1948 break;
1950 if (slash == NULL)
1951 break;
1952 free(subpath);
1953 subpath = NULL;
1954 p = slash;
1957 free(subpath);
1958 return err;
1961 static const struct got_error *
1962 browse_commit_tree(struct tog_view **new_view, int begin_x,
1963 struct commit_queue_entry *entry, const char *path,
1964 const char *head_ref_name, struct got_repository *repo)
1966 const struct got_error *err = NULL;
1967 struct tog_tree_view_state *s;
1968 struct tog_view *tree_view;
1970 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1971 if (tree_view == NULL)
1972 return got_error_from_errno("view_open");
1974 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1975 if (err)
1976 return err;
1977 s = &tree_view->state.tree;
1979 *new_view = tree_view;
1981 if (got_path_is_root_dir(path))
1982 return NULL;
1984 return tree_view_walk_path(s, entry->id, path);
1987 static const struct got_error *
1988 block_signals_used_by_main_thread(void)
1990 sigset_t sigset;
1991 int errcode;
1993 if (sigemptyset(&sigset) == -1)
1994 return got_error_from_errno("sigemptyset");
1996 /* tog handles SIGWINCH and SIGCONT */
1997 if (sigaddset(&sigset, SIGWINCH) == -1)
1998 return got_error_from_errno("sigaddset");
1999 if (sigaddset(&sigset, SIGCONT) == -1)
2000 return got_error_from_errno("sigaddset");
2002 /* ncurses handles SIGTSTP */
2003 if (sigaddset(&sigset, SIGTSTP) == -1)
2004 return got_error_from_errno("sigaddset");
2006 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2007 if (errcode)
2008 return got_error_set_errno(errcode, "pthread_sigmask");
2010 return NULL;
2013 static void *
2014 log_thread(void *arg)
2016 const struct got_error *err = NULL;
2017 int errcode = 0;
2018 struct tog_log_thread_args *a = arg;
2019 int done = 0;
2021 err = block_signals_used_by_main_thread();
2022 if (err)
2023 return (void *)err;
2025 while (!done && !err && !tog_sigpipe_received) {
2026 err = queue_commits(a);
2027 if (err) {
2028 if (err->code != GOT_ERR_ITER_COMPLETED)
2029 return (void *)err;
2030 err = NULL;
2031 done = 1;
2032 } else if (a->commits_needed > 0 && !a->load_all)
2033 a->commits_needed--;
2035 errcode = pthread_mutex_lock(&tog_mutex);
2036 if (errcode) {
2037 err = got_error_set_errno(errcode,
2038 "pthread_mutex_lock");
2039 break;
2040 } else if (*a->quit)
2041 done = 1;
2042 else if (*a->first_displayed_entry == NULL) {
2043 *a->first_displayed_entry =
2044 TAILQ_FIRST(&a->commits->head);
2045 *a->selected_entry = *a->first_displayed_entry;
2048 errcode = pthread_cond_signal(&a->commit_loaded);
2049 if (errcode) {
2050 err = got_error_set_errno(errcode,
2051 "pthread_cond_signal");
2052 pthread_mutex_unlock(&tog_mutex);
2053 break;
2056 if (done)
2057 a->commits_needed = 0;
2058 else {
2059 if (a->commits_needed == 0 && !a->load_all) {
2060 errcode = pthread_cond_wait(&a->need_commits,
2061 &tog_mutex);
2062 if (errcode)
2063 err = got_error_set_errno(errcode,
2064 "pthread_cond_wait");
2065 if (*a->quit)
2066 done = 1;
2070 errcode = pthread_mutex_unlock(&tog_mutex);
2071 if (errcode && err == NULL)
2072 err = got_error_set_errno(errcode,
2073 "pthread_mutex_unlock");
2075 a->log_complete = 1;
2076 return (void *)err;
2079 static const struct got_error *
2080 stop_log_thread(struct tog_log_view_state *s)
2082 const struct got_error *err = NULL;
2083 int errcode;
2085 if (s->thread) {
2086 s->quit = 1;
2087 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2088 if (errcode)
2089 return got_error_set_errno(errcode,
2090 "pthread_cond_signal");
2091 errcode = pthread_mutex_unlock(&tog_mutex);
2092 if (errcode)
2093 return got_error_set_errno(errcode,
2094 "pthread_mutex_unlock");
2095 errcode = pthread_join(s->thread, (void **)&err);
2096 if (errcode)
2097 return got_error_set_errno(errcode, "pthread_join");
2098 errcode = pthread_mutex_lock(&tog_mutex);
2099 if (errcode)
2100 return got_error_set_errno(errcode,
2101 "pthread_mutex_lock");
2102 s->thread = NULL;
2105 if (s->thread_args.repo) {
2106 err = got_repo_close(s->thread_args.repo);
2107 s->thread_args.repo = NULL;
2110 if (s->thread_args.graph) {
2111 got_commit_graph_close(s->thread_args.graph);
2112 s->thread_args.graph = NULL;
2115 return err;
2118 static const struct got_error *
2119 close_log_view(struct tog_view *view)
2121 const struct got_error *err = NULL;
2122 struct tog_log_view_state *s = &view->state.log;
2123 int errcode;
2125 err = stop_log_thread(s);
2127 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2128 if (errcode && err == NULL)
2129 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2131 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2132 if (errcode && err == NULL)
2133 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2135 free_commits(&s->commits);
2136 free(s->in_repo_path);
2137 s->in_repo_path = NULL;
2138 free(s->start_id);
2139 s->start_id = NULL;
2140 free(s->head_ref_name);
2141 s->head_ref_name = NULL;
2142 return err;
2145 static const struct got_error *
2146 search_start_log_view(struct tog_view *view)
2148 struct tog_log_view_state *s = &view->state.log;
2150 s->matched_entry = NULL;
2151 s->search_entry = NULL;
2152 return NULL;
2155 static const struct got_error *
2156 search_next_log_view(struct tog_view *view)
2158 const struct got_error *err = NULL;
2159 struct tog_log_view_state *s = &view->state.log;
2160 struct commit_queue_entry *entry;
2162 /* Display progress update in log view. */
2163 show_log_view(view);
2164 update_panels();
2165 doupdate();
2167 if (s->search_entry) {
2168 int errcode, ch;
2169 errcode = pthread_mutex_unlock(&tog_mutex);
2170 if (errcode)
2171 return got_error_set_errno(errcode,
2172 "pthread_mutex_unlock");
2173 ch = wgetch(view->window);
2174 errcode = pthread_mutex_lock(&tog_mutex);
2175 if (errcode)
2176 return got_error_set_errno(errcode,
2177 "pthread_mutex_lock");
2178 if (ch == KEY_BACKSPACE) {
2179 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2180 return NULL;
2182 if (view->searching == TOG_SEARCH_FORWARD)
2183 entry = TAILQ_NEXT(s->search_entry, entry);
2184 else
2185 entry = TAILQ_PREV(s->search_entry,
2186 commit_queue_head, entry);
2187 } else if (s->matched_entry) {
2188 if (view->searching == TOG_SEARCH_FORWARD)
2189 entry = TAILQ_NEXT(s->matched_entry, entry);
2190 else
2191 entry = TAILQ_PREV(s->matched_entry,
2192 commit_queue_head, entry);
2193 } else {
2194 entry = s->selected_entry;
2197 while (1) {
2198 int have_match = 0;
2200 if (entry == NULL) {
2201 if (s->thread_args.log_complete ||
2202 view->searching == TOG_SEARCH_BACKWARD) {
2203 view->search_next_done =
2204 (s->matched_entry == NULL ?
2205 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2206 s->search_entry = NULL;
2207 return NULL;
2210 * Poke the log thread for more commits and return,
2211 * allowing the main loop to make progress. Search
2212 * will resume at s->search_entry once we come back.
2214 s->thread_args.commits_needed++;
2215 return trigger_log_thread(view, 0);
2218 err = match_commit(&have_match, entry->id, entry->commit,
2219 &view->regex);
2220 if (err)
2221 break;
2222 if (have_match) {
2223 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2224 s->matched_entry = entry;
2225 break;
2228 s->search_entry = entry;
2229 if (view->searching == TOG_SEARCH_FORWARD)
2230 entry = TAILQ_NEXT(entry, entry);
2231 else
2232 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2235 if (s->matched_entry) {
2236 int cur = s->selected_entry->idx;
2237 while (cur < s->matched_entry->idx) {
2238 err = input_log_view(NULL, view, KEY_DOWN);
2239 if (err)
2240 return err;
2241 cur++;
2243 while (cur > s->matched_entry->idx) {
2244 err = input_log_view(NULL, view, KEY_UP);
2245 if (err)
2246 return err;
2247 cur--;
2251 s->search_entry = NULL;
2253 return NULL;
2256 static const struct got_error *
2257 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2258 struct got_repository *repo, const char *head_ref_name,
2259 const char *in_repo_path, int log_branches)
2261 const struct got_error *err = NULL;
2262 struct tog_log_view_state *s = &view->state.log;
2263 struct got_repository *thread_repo = NULL;
2264 struct got_commit_graph *thread_graph = NULL;
2265 int errcode;
2267 if (in_repo_path != s->in_repo_path) {
2268 free(s->in_repo_path);
2269 s->in_repo_path = strdup(in_repo_path);
2270 if (s->in_repo_path == NULL)
2271 return got_error_from_errno("strdup");
2274 /* The commit queue only contains commits being displayed. */
2275 TAILQ_INIT(&s->commits.head);
2276 s->commits.ncommits = 0;
2278 s->repo = repo;
2279 if (head_ref_name) {
2280 s->head_ref_name = strdup(head_ref_name);
2281 if (s->head_ref_name == NULL) {
2282 err = got_error_from_errno("strdup");
2283 goto done;
2286 s->start_id = got_object_id_dup(start_id);
2287 if (s->start_id == NULL) {
2288 err = got_error_from_errno("got_object_id_dup");
2289 goto done;
2291 s->log_branches = log_branches;
2293 STAILQ_INIT(&s->colors);
2294 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2295 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2296 get_color_value("TOG_COLOR_COMMIT"));
2297 if (err)
2298 goto done;
2299 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2300 get_color_value("TOG_COLOR_AUTHOR"));
2301 if (err) {
2302 free_colors(&s->colors);
2303 goto done;
2305 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2306 get_color_value("TOG_COLOR_DATE"));
2307 if (err) {
2308 free_colors(&s->colors);
2309 goto done;
2313 view->show = show_log_view;
2314 view->input = input_log_view;
2315 view->close = close_log_view;
2316 view->search_start = search_start_log_view;
2317 view->search_next = search_next_log_view;
2319 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2320 if (err)
2321 goto done;
2322 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2323 !s->log_branches);
2324 if (err)
2325 goto done;
2326 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2327 s->repo, NULL, NULL);
2328 if (err)
2329 goto done;
2331 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2332 if (errcode) {
2333 err = got_error_set_errno(errcode, "pthread_cond_init");
2334 goto done;
2336 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2337 if (errcode) {
2338 err = got_error_set_errno(errcode, "pthread_cond_init");
2339 goto done;
2342 s->thread_args.commits_needed = view->nlines;
2343 s->thread_args.graph = thread_graph;
2344 s->thread_args.commits = &s->commits;
2345 s->thread_args.in_repo_path = s->in_repo_path;
2346 s->thread_args.start_id = s->start_id;
2347 s->thread_args.repo = thread_repo;
2348 s->thread_args.log_complete = 0;
2349 s->thread_args.quit = &s->quit;
2350 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2351 s->thread_args.selected_entry = &s->selected_entry;
2352 s->thread_args.searching = &view->searching;
2353 s->thread_args.search_next_done = &view->search_next_done;
2354 s->thread_args.regex = &view->regex;
2355 done:
2356 if (err)
2357 close_log_view(view);
2358 return err;
2361 static const struct got_error *
2362 show_log_view(struct tog_view *view)
2364 const struct got_error *err;
2365 struct tog_log_view_state *s = &view->state.log;
2367 if (s->thread == NULL) {
2368 int errcode = pthread_create(&s->thread, NULL, log_thread,
2369 &s->thread_args);
2370 if (errcode)
2371 return got_error_set_errno(errcode, "pthread_create");
2372 if (s->thread_args.commits_needed > 0) {
2373 err = trigger_log_thread(view, 1);
2374 if (err)
2375 return err;
2379 return draw_commits(view);
2382 static const struct got_error *
2383 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2385 const struct got_error *err = NULL;
2386 struct tog_log_view_state *s = &view->state.log;
2387 struct tog_view *diff_view = NULL, *tree_view = NULL;
2388 struct tog_view *ref_view = NULL;
2389 struct commit_queue_entry *entry;
2390 int begin_x = 0, n;
2392 if (s->thread_args.load_all) {
2393 if (ch == KEY_BACKSPACE)
2394 s->thread_args.load_all = 0;
2395 else if (s->thread_args.log_complete) {
2396 s->thread_args.load_all = 0;
2397 log_scroll_down(view, s->commits.ncommits);
2398 s->selected = MIN(view->nlines - 2,
2399 s->commits.ncommits - 1);
2400 select_commit(s);
2402 return NULL;
2405 switch (ch) {
2406 case 'q':
2407 s->quit = 1;
2408 break;
2409 case 'k':
2410 case KEY_UP:
2411 case '<':
2412 case ',':
2413 case CTRL('p'):
2414 if (s->first_displayed_entry == NULL)
2415 break;
2416 if (s->selected > 0)
2417 s->selected--;
2418 else
2419 log_scroll_up(s, 1);
2420 select_commit(s);
2421 break;
2422 case 'g':
2423 case KEY_HOME:
2424 s->selected = 0;
2425 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2426 select_commit(s);
2427 break;
2428 case KEY_PPAGE:
2429 case CTRL('b'):
2430 if (s->first_displayed_entry == NULL)
2431 break;
2432 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2433 s->selected = 0;
2434 else
2435 log_scroll_up(s, view->nlines - 1);
2436 select_commit(s);
2437 break;
2438 case 'j':
2439 case KEY_DOWN:
2440 case '>':
2441 case '.':
2442 case CTRL('n'):
2443 if (s->first_displayed_entry == NULL)
2444 break;
2445 if (s->selected < MIN(view->nlines - 2,
2446 s->commits.ncommits - 1))
2447 s->selected++;
2448 else {
2449 err = log_scroll_down(view, 1);
2450 if (err)
2451 break;
2453 select_commit(s);
2454 break;
2455 case 'G':
2456 case KEY_END: {
2457 /* We don't know yet how many commits, so we're forced to
2458 * traverse them all. */
2459 if (!s->thread_args.log_complete) {
2460 s->thread_args.load_all = 1;
2461 return trigger_log_thread(view, 0);
2464 s->selected = 0;
2465 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2466 for (n = 0; n < view->nlines - 1; n++) {
2467 if (entry == NULL)
2468 break;
2469 s->first_displayed_entry = entry;
2470 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2472 if (n > 0)
2473 s->selected = n - 1;
2474 select_commit(s);
2475 break;
2477 case KEY_NPAGE:
2478 case CTRL('f'): {
2479 struct commit_queue_entry *first;
2480 first = s->first_displayed_entry;
2481 if (first == NULL)
2482 break;
2483 err = log_scroll_down(view, view->nlines - 1);
2484 if (err)
2485 break;
2486 if (first == s->first_displayed_entry &&
2487 s->selected < MIN(view->nlines - 2,
2488 s->commits.ncommits - 1)) {
2489 /* can't scroll further down */
2490 s->selected = MIN(view->nlines - 2,
2491 s->commits.ncommits - 1);
2493 select_commit(s);
2494 break;
2496 case KEY_RESIZE:
2497 if (s->selected > view->nlines - 2)
2498 s->selected = view->nlines - 2;
2499 if (s->selected > s->commits.ncommits - 1)
2500 s->selected = s->commits.ncommits - 1;
2501 select_commit(s);
2502 if (s->commits.ncommits < view->nlines - 1 &&
2503 !s->thread_args.log_complete) {
2504 s->thread_args.commits_needed += (view->nlines - 1) -
2505 s->commits.ncommits;
2506 err = trigger_log_thread(view, 1);
2508 break;
2509 case KEY_ENTER:
2510 case ' ':
2511 case '\r':
2512 if (s->selected_entry == NULL)
2513 break;
2514 if (view_is_parent_view(view))
2515 begin_x = view_split_begin_x(view->begin_x);
2516 err = open_diff_view_for_commit(&diff_view, begin_x,
2517 s->selected_entry->commit, s->selected_entry->id,
2518 view, s->repo);
2519 if (err)
2520 break;
2521 view->focussed = 0;
2522 diff_view->focussed = 1;
2523 if (view_is_parent_view(view)) {
2524 err = view_close_child(view);
2525 if (err)
2526 return err;
2527 view_set_child(view, diff_view);
2528 view->focus_child = 1;
2529 } else
2530 *new_view = diff_view;
2531 break;
2532 case 't':
2533 if (s->selected_entry == NULL)
2534 break;
2535 if (view_is_parent_view(view))
2536 begin_x = view_split_begin_x(view->begin_x);
2537 err = browse_commit_tree(&tree_view, begin_x,
2538 s->selected_entry, s->in_repo_path, s->head_ref_name,
2539 s->repo);
2540 if (err)
2541 break;
2542 view->focussed = 0;
2543 tree_view->focussed = 1;
2544 if (view_is_parent_view(view)) {
2545 err = view_close_child(view);
2546 if (err)
2547 return err;
2548 view_set_child(view, tree_view);
2549 view->focus_child = 1;
2550 } else
2551 *new_view = tree_view;
2552 break;
2553 case KEY_BACKSPACE:
2554 case CTRL('l'):
2555 case 'B':
2556 if (ch == KEY_BACKSPACE &&
2557 got_path_is_root_dir(s->in_repo_path))
2558 break;
2559 err = stop_log_thread(s);
2560 if (err)
2561 return err;
2562 if (ch == KEY_BACKSPACE) {
2563 char *parent_path;
2564 err = got_path_dirname(&parent_path, s->in_repo_path);
2565 if (err)
2566 return err;
2567 free(s->in_repo_path);
2568 s->in_repo_path = parent_path;
2569 s->thread_args.in_repo_path = s->in_repo_path;
2570 } else if (ch == CTRL('l')) {
2571 struct got_object_id *start_id;
2572 err = got_repo_match_object_id(&start_id, NULL,
2573 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2574 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2575 if (err)
2576 return err;
2577 free(s->start_id);
2578 s->start_id = start_id;
2579 s->thread_args.start_id = s->start_id;
2580 } else /* 'B' */
2581 s->log_branches = !s->log_branches;
2583 err = got_repo_open(&s->thread_args.repo,
2584 got_repo_get_path(s->repo), NULL);
2585 if (err)
2586 return err;
2587 tog_free_refs();
2588 err = tog_load_refs(s->repo, 0);
2589 if (err)
2590 return err;
2591 err = got_commit_graph_open(&s->thread_args.graph,
2592 s->in_repo_path, !s->log_branches);
2593 if (err)
2594 return err;
2595 err = got_commit_graph_iter_start(s->thread_args.graph,
2596 s->start_id, s->repo, NULL, NULL);
2597 if (err)
2598 return err;
2599 free_commits(&s->commits);
2600 s->first_displayed_entry = NULL;
2601 s->last_displayed_entry = NULL;
2602 s->selected_entry = NULL;
2603 s->selected = 0;
2604 s->thread_args.log_complete = 0;
2605 s->quit = 0;
2606 s->thread_args.commits_needed = view->nlines;
2607 break;
2608 case 'r':
2609 if (view_is_parent_view(view))
2610 begin_x = view_split_begin_x(view->begin_x);
2611 ref_view = view_open(view->nlines, view->ncols,
2612 view->begin_y, begin_x, TOG_VIEW_REF);
2613 if (ref_view == NULL)
2614 return got_error_from_errno("view_open");
2615 err = open_ref_view(ref_view, s->repo);
2616 if (err) {
2617 view_close(ref_view);
2618 return err;
2620 view->focussed = 0;
2621 ref_view->focussed = 1;
2622 if (view_is_parent_view(view)) {
2623 err = view_close_child(view);
2624 if (err)
2625 return err;
2626 view_set_child(view, ref_view);
2627 view->focus_child = 1;
2628 } else
2629 *new_view = ref_view;
2630 break;
2631 default:
2632 break;
2635 return err;
2638 static const struct got_error *
2639 apply_unveil(const char *repo_path, const char *worktree_path)
2641 const struct got_error *error;
2643 #ifdef PROFILE
2644 if (unveil("gmon.out", "rwc") != 0)
2645 return got_error_from_errno2("unveil", "gmon.out");
2646 #endif
2647 if (repo_path && unveil(repo_path, "r") != 0)
2648 return got_error_from_errno2("unveil", repo_path);
2650 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2651 return got_error_from_errno2("unveil", worktree_path);
2653 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2654 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2656 error = got_privsep_unveil_exec_helpers();
2657 if (error != NULL)
2658 return error;
2660 if (unveil(NULL, NULL) != 0)
2661 return got_error_from_errno("unveil");
2663 return NULL;
2666 static void
2667 init_curses(void)
2669 initscr();
2670 cbreak();
2671 halfdelay(1); /* Do fast refresh while initial view is loading. */
2672 noecho();
2673 nonl();
2674 intrflush(stdscr, FALSE);
2675 keypad(stdscr, TRUE);
2676 curs_set(0);
2677 if (getenv("TOG_COLORS") != NULL) {
2678 start_color();
2679 use_default_colors();
2681 signal(SIGWINCH, tog_sigwinch);
2682 signal(SIGPIPE, tog_sigpipe);
2683 signal(SIGCONT, tog_sigcont);
2686 static const struct got_error *
2687 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2688 struct got_repository *repo, struct got_worktree *worktree)
2690 const struct got_error *err = NULL;
2692 if (argc == 0) {
2693 *in_repo_path = strdup("/");
2694 if (*in_repo_path == NULL)
2695 return got_error_from_errno("strdup");
2696 return NULL;
2699 if (worktree) {
2700 const char *prefix = got_worktree_get_path_prefix(worktree);
2701 char *p;
2703 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2704 if (err)
2705 return err;
2706 if (asprintf(in_repo_path, "%s%s%s", prefix,
2707 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2708 p) == -1) {
2709 err = got_error_from_errno("asprintf");
2710 *in_repo_path = NULL;
2712 free(p);
2713 } else
2714 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2716 return err;
2719 static const struct got_error *
2720 cmd_log(int argc, char *argv[])
2722 const struct got_error *error;
2723 struct got_repository *repo = NULL;
2724 struct got_worktree *worktree = NULL;
2725 struct got_object_id *start_id = NULL;
2726 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2727 char *start_commit = NULL, *label = NULL;
2728 struct got_reference *ref = NULL;
2729 const char *head_ref_name = NULL;
2730 int ch, log_branches = 0;
2731 struct tog_view *view;
2733 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2734 switch (ch) {
2735 case 'b':
2736 log_branches = 1;
2737 break;
2738 case 'c':
2739 start_commit = optarg;
2740 break;
2741 case 'r':
2742 repo_path = realpath(optarg, NULL);
2743 if (repo_path == NULL)
2744 return got_error_from_errno2("realpath",
2745 optarg);
2746 break;
2747 default:
2748 usage_log();
2749 /* NOTREACHED */
2753 argc -= optind;
2754 argv += optind;
2756 if (argc > 1)
2757 usage_log();
2759 if (repo_path == NULL) {
2760 cwd = getcwd(NULL, 0);
2761 if (cwd == NULL)
2762 return got_error_from_errno("getcwd");
2763 error = got_worktree_open(&worktree, cwd);
2764 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2765 goto done;
2766 if (worktree)
2767 repo_path =
2768 strdup(got_worktree_get_repo_path(worktree));
2769 else
2770 repo_path = strdup(cwd);
2771 if (repo_path == NULL) {
2772 error = got_error_from_errno("strdup");
2773 goto done;
2777 error = got_repo_open(&repo, repo_path, NULL);
2778 if (error != NULL)
2779 goto done;
2781 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2782 repo, worktree);
2783 if (error)
2784 goto done;
2786 init_curses();
2788 error = apply_unveil(got_repo_get_path(repo),
2789 worktree ? got_worktree_get_root_path(worktree) : NULL);
2790 if (error)
2791 goto done;
2793 /* already loaded by tog_log_with_path()? */
2794 if (TAILQ_EMPTY(&tog_refs)) {
2795 error = tog_load_refs(repo, 0);
2796 if (error)
2797 goto done;
2800 if (start_commit == NULL) {
2801 error = got_repo_match_object_id(&start_id, &label,
2802 worktree ? got_worktree_get_head_ref_name(worktree) :
2803 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2804 if (error)
2805 goto done;
2806 head_ref_name = label;
2807 } else {
2808 error = got_ref_open(&ref, repo, start_commit, 0);
2809 if (error == NULL)
2810 head_ref_name = got_ref_get_name(ref);
2811 else if (error->code != GOT_ERR_NOT_REF)
2812 goto done;
2813 error = got_repo_match_object_id(&start_id, NULL,
2814 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2815 if (error)
2816 goto done;
2819 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2820 if (view == NULL) {
2821 error = got_error_from_errno("view_open");
2822 goto done;
2824 error = open_log_view(view, start_id, repo, head_ref_name,
2825 in_repo_path, log_branches);
2826 if (error)
2827 goto done;
2828 if (worktree) {
2829 /* Release work tree lock. */
2830 got_worktree_close(worktree);
2831 worktree = NULL;
2833 error = view_loop(view);
2834 done:
2835 free(in_repo_path);
2836 free(repo_path);
2837 free(cwd);
2838 free(start_id);
2839 free(label);
2840 if (ref)
2841 got_ref_close(ref);
2842 if (repo) {
2843 const struct got_error *close_err = got_repo_close(repo);
2844 if (error == NULL)
2845 error = close_err;
2847 if (worktree)
2848 got_worktree_close(worktree);
2849 tog_free_refs();
2850 return error;
2853 __dead static void
2854 usage_diff(void)
2856 endwin();
2857 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2858 "[-w] object1 object2\n", getprogname());
2859 exit(1);
2862 static int
2863 match_line(const char *line, regex_t *regex, size_t nmatch,
2864 regmatch_t *regmatch)
2866 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2869 struct tog_color *
2870 match_color(struct tog_colors *colors, const char *line)
2872 struct tog_color *tc = NULL;
2874 STAILQ_FOREACH(tc, colors, entry) {
2875 if (match_line(line, &tc->regex, 0, NULL))
2876 return tc;
2879 return NULL;
2882 static const struct got_error *
2883 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2884 WINDOW *window, regmatch_t *regmatch)
2886 const struct got_error *err = NULL;
2887 wchar_t *wline;
2888 int width;
2889 char *s;
2891 *wtotal = 0;
2893 s = strndup(line, regmatch->rm_so);
2894 if (s == NULL)
2895 return got_error_from_errno("strndup");
2897 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2898 if (err) {
2899 free(s);
2900 return err;
2902 waddwstr(window, wline);
2903 free(wline);
2904 free(s);
2905 wlimit -= width;
2906 *wtotal += width;
2908 if (wlimit > 0) {
2909 s = strndup(line + regmatch->rm_so,
2910 regmatch->rm_eo - regmatch->rm_so);
2911 if (s == NULL) {
2912 err = got_error_from_errno("strndup");
2913 free(s);
2914 return err;
2916 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2917 if (err) {
2918 free(s);
2919 return err;
2921 wattr_on(window, A_STANDOUT, NULL);
2922 waddwstr(window, wline);
2923 wattr_off(window, A_STANDOUT, NULL);
2924 free(wline);
2925 free(s);
2926 wlimit -= width;
2927 *wtotal += width;
2930 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2931 err = format_line(&wline, &width,
2932 line + regmatch->rm_eo, wlimit, col_tab_align);
2933 if (err)
2934 return err;
2935 waddwstr(window, wline);
2936 free(wline);
2937 *wtotal += width;
2940 return NULL;
2943 static const struct got_error *
2944 draw_file(struct tog_view *view, const char *header)
2946 struct tog_diff_view_state *s = &view->state.diff;
2947 regmatch_t *regmatch = &view->regmatch;
2948 const struct got_error *err;
2949 int nprinted = 0;
2950 char *line;
2951 size_t linesize = 0;
2952 ssize_t linelen;
2953 struct tog_color *tc;
2954 wchar_t *wline;
2955 int width;
2956 int max_lines = view->nlines;
2957 int nlines = s->nlines;
2958 off_t line_offset;
2960 line_offset = s->line_offsets[s->first_displayed_line - 1];
2961 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2962 return got_error_from_errno("fseek");
2964 werase(view->window);
2966 if (header) {
2967 if (asprintf(&line, "[%d/%d] %s",
2968 s->first_displayed_line - 1 + s->selected_line, nlines,
2969 header) == -1)
2970 return got_error_from_errno("asprintf");
2971 err = format_line(&wline, &width, line, view->ncols, 0);
2972 free(line);
2973 if (err)
2974 return err;
2976 if (view_needs_focus_indication(view))
2977 wstandout(view->window);
2978 waddwstr(view->window, wline);
2979 free(wline);
2980 wline = NULL;
2981 if (view_needs_focus_indication(view))
2982 wstandend(view->window);
2983 if (width <= view->ncols - 1)
2984 waddch(view->window, '\n');
2986 if (max_lines <= 1)
2987 return NULL;
2988 max_lines--;
2991 s->eof = 0;
2992 line = NULL;
2993 while (max_lines > 0 && nprinted < max_lines) {
2994 linelen = getline(&line, &linesize, s->f);
2995 if (linelen == -1) {
2996 if (feof(s->f)) {
2997 s->eof = 1;
2998 break;
3000 free(line);
3001 return got_ferror(s->f, GOT_ERR_IO);
3004 tc = match_color(&s->colors, line);
3005 if (tc)
3006 wattr_on(view->window,
3007 COLOR_PAIR(tc->colorpair), NULL);
3008 if (s->first_displayed_line + nprinted == s->matched_line &&
3009 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3010 err = add_matched_line(&width, line, view->ncols, 0,
3011 view->window, regmatch);
3012 if (err) {
3013 free(line);
3014 return err;
3016 } else {
3017 err = format_line(&wline, &width, line, view->ncols, 0);
3018 if (err) {
3019 free(line);
3020 return err;
3022 waddwstr(view->window, wline);
3023 free(wline);
3024 wline = NULL;
3026 if (tc)
3027 wattr_off(view->window,
3028 COLOR_PAIR(tc->colorpair), NULL);
3029 if (width <= view->ncols - 1)
3030 waddch(view->window, '\n');
3031 nprinted++;
3033 free(line);
3034 if (nprinted >= 1)
3035 s->last_displayed_line = s->first_displayed_line +
3036 (nprinted - 1);
3037 else
3038 s->last_displayed_line = s->first_displayed_line;
3040 view_vborder(view);
3042 if (s->eof) {
3043 while (nprinted < view->nlines) {
3044 waddch(view->window, '\n');
3045 nprinted++;
3048 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3049 if (err) {
3050 return err;
3053 wstandout(view->window);
3054 waddwstr(view->window, wline);
3055 free(wline);
3056 wline = NULL;
3057 wstandend(view->window);
3060 return NULL;
3063 static char *
3064 get_datestr(time_t *time, char *datebuf)
3066 struct tm mytm, *tm;
3067 char *p, *s;
3069 tm = gmtime_r(time, &mytm);
3070 if (tm == NULL)
3071 return NULL;
3072 s = asctime_r(tm, datebuf);
3073 if (s == NULL)
3074 return NULL;
3075 p = strchr(s, '\n');
3076 if (p)
3077 *p = '\0';
3078 return s;
3081 static const struct got_error *
3082 get_changed_paths(struct got_pathlist_head *paths,
3083 struct got_commit_object *commit, struct got_repository *repo)
3085 const struct got_error *err = NULL;
3086 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3087 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3088 struct got_object_qid *qid;
3090 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3091 if (qid != NULL) {
3092 struct got_commit_object *pcommit;
3093 err = got_object_open_as_commit(&pcommit, repo,
3094 qid->id);
3095 if (err)
3096 return err;
3098 tree_id1 = got_object_id_dup(
3099 got_object_commit_get_tree_id(pcommit));
3100 if (tree_id1 == NULL) {
3101 got_object_commit_close(pcommit);
3102 return got_error_from_errno("got_object_id_dup");
3104 got_object_commit_close(pcommit);
3108 if (tree_id1) {
3109 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3110 if (err)
3111 goto done;
3114 tree_id2 = got_object_commit_get_tree_id(commit);
3115 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3116 if (err)
3117 goto done;
3119 err = got_diff_tree(tree1, tree2, "", "", repo,
3120 got_diff_tree_collect_changed_paths, paths, 0);
3121 done:
3122 if (tree1)
3123 got_object_tree_close(tree1);
3124 if (tree2)
3125 got_object_tree_close(tree2);
3126 free(tree_id1);
3127 return err;
3130 static const struct got_error *
3131 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3133 off_t *p;
3135 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3136 if (p == NULL)
3137 return got_error_from_errno("reallocarray");
3138 *line_offsets = p;
3139 (*line_offsets)[*nlines] = off;
3140 (*nlines)++;
3141 return NULL;
3144 static const struct got_error *
3145 write_commit_info(off_t **line_offsets, size_t *nlines,
3146 struct got_object_id *commit_id, struct got_reflist_head *refs,
3147 struct got_repository *repo, FILE *outfile)
3149 const struct got_error *err = NULL;
3150 char datebuf[26], *datestr;
3151 struct got_commit_object *commit;
3152 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3153 time_t committer_time;
3154 const char *author, *committer;
3155 char *refs_str = NULL;
3156 struct got_pathlist_head changed_paths;
3157 struct got_pathlist_entry *pe;
3158 off_t outoff = 0;
3159 int n;
3161 TAILQ_INIT(&changed_paths);
3163 if (refs) {
3164 err = build_refs_str(&refs_str, refs, commit_id, repo);
3165 if (err)
3166 return err;
3169 err = got_object_open_as_commit(&commit, repo, commit_id);
3170 if (err)
3171 return err;
3173 err = got_object_id_str(&id_str, commit_id);
3174 if (err) {
3175 err = got_error_from_errno("got_object_id_str");
3176 goto done;
3179 err = add_line_offset(line_offsets, nlines, 0);
3180 if (err)
3181 goto done;
3183 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3184 refs_str ? refs_str : "", refs_str ? ")" : "");
3185 if (n < 0) {
3186 err = got_error_from_errno("fprintf");
3187 goto done;
3189 outoff += n;
3190 err = add_line_offset(line_offsets, nlines, outoff);
3191 if (err)
3192 goto done;
3194 n = fprintf(outfile, "from: %s\n",
3195 got_object_commit_get_author(commit));
3196 if (n < 0) {
3197 err = got_error_from_errno("fprintf");
3198 goto done;
3200 outoff += n;
3201 err = add_line_offset(line_offsets, nlines, outoff);
3202 if (err)
3203 goto done;
3205 committer_time = got_object_commit_get_committer_time(commit);
3206 datestr = get_datestr(&committer_time, datebuf);
3207 if (datestr) {
3208 n = fprintf(outfile, "date: %s UTC\n", datestr);
3209 if (n < 0) {
3210 err = got_error_from_errno("fprintf");
3211 goto done;
3213 outoff += n;
3214 err = add_line_offset(line_offsets, nlines, outoff);
3215 if (err)
3216 goto done;
3218 author = got_object_commit_get_author(commit);
3219 committer = got_object_commit_get_committer(commit);
3220 if (strcmp(author, committer) != 0) {
3221 n = fprintf(outfile, "via: %s\n", committer);
3222 if (n < 0) {
3223 err = got_error_from_errno("fprintf");
3224 goto done;
3226 outoff += n;
3227 err = add_line_offset(line_offsets, nlines, outoff);
3228 if (err)
3229 goto done;
3231 if (got_object_commit_get_nparents(commit) > 1) {
3232 const struct got_object_id_queue *parent_ids;
3233 struct got_object_qid *qid;
3234 int pn = 1;
3235 parent_ids = got_object_commit_get_parent_ids(commit);
3236 STAILQ_FOREACH(qid, parent_ids, entry) {
3237 err = got_object_id_str(&id_str, qid->id);
3238 if (err)
3239 goto done;
3240 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3241 if (n < 0) {
3242 err = got_error_from_errno("fprintf");
3243 goto done;
3245 outoff += n;
3246 err = add_line_offset(line_offsets, nlines, outoff);
3247 if (err)
3248 goto done;
3249 free(id_str);
3250 id_str = NULL;
3254 err = got_object_commit_get_logmsg(&logmsg, commit);
3255 if (err)
3256 goto done;
3257 s = logmsg;
3258 while ((line = strsep(&s, "\n")) != NULL) {
3259 n = fprintf(outfile, "%s\n", line);
3260 if (n < 0) {
3261 err = got_error_from_errno("fprintf");
3262 goto done;
3264 outoff += n;
3265 err = add_line_offset(line_offsets, nlines, outoff);
3266 if (err)
3267 goto done;
3270 err = get_changed_paths(&changed_paths, commit, repo);
3271 if (err)
3272 goto done;
3273 TAILQ_FOREACH(pe, &changed_paths, entry) {
3274 struct got_diff_changed_path *cp = pe->data;
3275 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3276 if (n < 0) {
3277 err = got_error_from_errno("fprintf");
3278 goto done;
3280 outoff += n;
3281 err = add_line_offset(line_offsets, nlines, outoff);
3282 if (err)
3283 goto done;
3284 free((char *)pe->path);
3285 free(pe->data);
3288 fputc('\n', outfile);
3289 outoff++;
3290 err = add_line_offset(line_offsets, nlines, outoff);
3291 done:
3292 got_pathlist_free(&changed_paths);
3293 free(id_str);
3294 free(logmsg);
3295 free(refs_str);
3296 got_object_commit_close(commit);
3297 if (err) {
3298 free(*line_offsets);
3299 *line_offsets = NULL;
3300 *nlines = 0;
3302 return err;
3305 static const struct got_error *
3306 create_diff(struct tog_diff_view_state *s)
3308 const struct got_error *err = NULL;
3309 FILE *f = NULL;
3310 int obj_type;
3312 free(s->line_offsets);
3313 s->line_offsets = malloc(sizeof(off_t));
3314 if (s->line_offsets == NULL)
3315 return got_error_from_errno("malloc");
3316 s->nlines = 0;
3318 f = got_opentemp();
3319 if (f == NULL) {
3320 err = got_error_from_errno("got_opentemp");
3321 goto done;
3323 if (s->f && fclose(s->f) == EOF) {
3324 err = got_error_from_errno("fclose");
3325 goto done;
3327 s->f = f;
3329 if (s->id1)
3330 err = got_object_get_type(&obj_type, s->repo, s->id1);
3331 else
3332 err = got_object_get_type(&obj_type, s->repo, s->id2);
3333 if (err)
3334 goto done;
3336 switch (obj_type) {
3337 case GOT_OBJ_TYPE_BLOB:
3338 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3339 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3340 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3341 break;
3342 case GOT_OBJ_TYPE_TREE:
3343 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3344 s->id1, s->id2, NULL, "", "", s->diff_context,
3345 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3346 break;
3347 case GOT_OBJ_TYPE_COMMIT: {
3348 const struct got_object_id_queue *parent_ids;
3349 struct got_object_qid *pid;
3350 struct got_commit_object *commit2;
3351 struct got_reflist_head *refs;
3353 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3354 if (err)
3355 goto done;
3356 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3357 /* Show commit info if we're diffing to a parent/root commit. */
3358 if (s->id1 == NULL) {
3359 err = write_commit_info(&s->line_offsets, &s->nlines,
3360 s->id2, refs, s->repo, s->f);
3361 if (err)
3362 goto done;
3363 } else {
3364 parent_ids = got_object_commit_get_parent_ids(commit2);
3365 STAILQ_FOREACH(pid, parent_ids, entry) {
3366 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3367 err = write_commit_info(
3368 &s->line_offsets, &s->nlines,
3369 s->id2, refs, s->repo, s->f);
3370 if (err)
3371 goto done;
3372 break;
3376 got_object_commit_close(commit2);
3378 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3379 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3380 s->force_text_diff, s->repo, s->f);
3381 break;
3383 default:
3384 err = got_error(GOT_ERR_OBJ_TYPE);
3385 break;
3387 if (err)
3388 goto done;
3389 done:
3390 if (s->f && fflush(s->f) != 0 && err == NULL)
3391 err = got_error_from_errno("fflush");
3392 return err;
3395 static void
3396 diff_view_indicate_progress(struct tog_view *view)
3398 mvwaddstr(view->window, 0, 0, "diffing...");
3399 update_panels();
3400 doupdate();
3403 static const struct got_error *
3404 search_start_diff_view(struct tog_view *view)
3406 struct tog_diff_view_state *s = &view->state.diff;
3408 s->matched_line = 0;
3409 return NULL;
3412 static const struct got_error *
3413 search_next_diff_view(struct tog_view *view)
3415 struct tog_diff_view_state *s = &view->state.diff;
3416 int lineno;
3417 char *line = NULL;
3418 size_t linesize = 0;
3419 ssize_t linelen;
3421 if (!view->searching) {
3422 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3423 return NULL;
3426 if (s->matched_line) {
3427 if (view->searching == TOG_SEARCH_FORWARD)
3428 lineno = s->matched_line + 1;
3429 else
3430 lineno = s->matched_line - 1;
3431 } else
3432 lineno = s->first_displayed_line;
3434 while (1) {
3435 off_t offset;
3437 if (lineno <= 0 || lineno > s->nlines) {
3438 if (s->matched_line == 0) {
3439 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3440 break;
3443 if (view->searching == TOG_SEARCH_FORWARD)
3444 lineno = 1;
3445 else
3446 lineno = s->nlines;
3449 offset = s->line_offsets[lineno - 1];
3450 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3451 free(line);
3452 return got_error_from_errno("fseeko");
3454 linelen = getline(&line, &linesize, s->f);
3455 if (linelen != -1 &&
3456 match_line(line, &view->regex, 1, &view->regmatch)) {
3457 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3458 s->matched_line = lineno;
3459 break;
3461 if (view->searching == TOG_SEARCH_FORWARD)
3462 lineno++;
3463 else
3464 lineno--;
3466 free(line);
3468 if (s->matched_line) {
3469 s->first_displayed_line = s->matched_line;
3470 s->selected_line = 1;
3473 return NULL;
3476 static const struct got_error *
3477 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3478 struct got_object_id *id2, const char *label1, const char *label2,
3479 int diff_context, int ignore_whitespace, int force_text_diff,
3480 struct tog_view *log_view, struct got_repository *repo)
3482 const struct got_error *err;
3483 struct tog_diff_view_state *s = &view->state.diff;
3485 if (id1 != NULL && id2 != NULL) {
3486 int type1, type2;
3487 err = got_object_get_type(&type1, repo, id1);
3488 if (err)
3489 return err;
3490 err = got_object_get_type(&type2, repo, id2);
3491 if (err)
3492 return err;
3494 if (type1 != type2)
3495 return got_error(GOT_ERR_OBJ_TYPE);
3497 s->first_displayed_line = 1;
3498 s->last_displayed_line = view->nlines;
3499 s->selected_line = 1;
3500 s->repo = repo;
3501 s->id1 = id1;
3502 s->id2 = id2;
3503 s->label1 = label1;
3504 s->label2 = label2;
3506 if (id1) {
3507 s->id1 = got_object_id_dup(id1);
3508 if (s->id1 == NULL)
3509 return got_error_from_errno("got_object_id_dup");
3510 } else
3511 s->id1 = NULL;
3513 s->id2 = got_object_id_dup(id2);
3514 if (s->id2 == NULL) {
3515 free(s->id1);
3516 s->id1 = NULL;
3517 return got_error_from_errno("got_object_id_dup");
3519 s->f = NULL;
3520 s->first_displayed_line = 1;
3521 s->last_displayed_line = view->nlines;
3522 s->diff_context = diff_context;
3523 s->ignore_whitespace = ignore_whitespace;
3524 s->force_text_diff = force_text_diff;
3525 s->log_view = log_view;
3526 s->repo = repo;
3528 STAILQ_INIT(&s->colors);
3529 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3530 err = add_color(&s->colors,
3531 "^-", TOG_COLOR_DIFF_MINUS,
3532 get_color_value("TOG_COLOR_DIFF_MINUS"));
3533 if (err)
3534 return err;
3535 err = add_color(&s->colors, "^\\+",
3536 TOG_COLOR_DIFF_PLUS,
3537 get_color_value("TOG_COLOR_DIFF_PLUS"));
3538 if (err) {
3539 free_colors(&s->colors);
3540 return err;
3542 err = add_color(&s->colors,
3543 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3544 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3545 if (err) {
3546 free_colors(&s->colors);
3547 return err;
3550 err = add_color(&s->colors,
3551 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3552 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3553 get_color_value("TOG_COLOR_DIFF_META"));
3554 if (err) {
3555 free_colors(&s->colors);
3556 return err;
3559 err = add_color(&s->colors,
3560 "^(from|via): ", TOG_COLOR_AUTHOR,
3561 get_color_value("TOG_COLOR_AUTHOR"));
3562 if (err) {
3563 free_colors(&s->colors);
3564 return err;
3567 err = add_color(&s->colors,
3568 "^date: ", TOG_COLOR_DATE,
3569 get_color_value("TOG_COLOR_DATE"));
3570 if (err) {
3571 free_colors(&s->colors);
3572 return err;
3576 if (log_view && view_is_splitscreen(view))
3577 show_log_view(log_view); /* draw vborder */
3578 diff_view_indicate_progress(view);
3580 s->line_offsets = NULL;
3581 s->nlines = 0;
3582 err = create_diff(s);
3583 if (err) {
3584 free(s->id1);
3585 s->id1 = NULL;
3586 free(s->id2);
3587 s->id2 = NULL;
3588 free_colors(&s->colors);
3589 return err;
3592 view->show = show_diff_view;
3593 view->input = input_diff_view;
3594 view->close = close_diff_view;
3595 view->search_start = search_start_diff_view;
3596 view->search_next = search_next_diff_view;
3598 return NULL;
3601 static const struct got_error *
3602 close_diff_view(struct tog_view *view)
3604 const struct got_error *err = NULL;
3605 struct tog_diff_view_state *s = &view->state.diff;
3607 free(s->id1);
3608 s->id1 = NULL;
3609 free(s->id2);
3610 s->id2 = NULL;
3611 if (s->f && fclose(s->f) == EOF)
3612 err = got_error_from_errno("fclose");
3613 free_colors(&s->colors);
3614 free(s->line_offsets);
3615 s->line_offsets = NULL;
3616 s->nlines = 0;
3617 return err;
3620 static const struct got_error *
3621 show_diff_view(struct tog_view *view)
3623 const struct got_error *err;
3624 struct tog_diff_view_state *s = &view->state.diff;
3625 char *id_str1 = NULL, *id_str2, *header;
3626 const char *label1, *label2;
3628 if (s->id1) {
3629 err = got_object_id_str(&id_str1, s->id1);
3630 if (err)
3631 return err;
3632 label1 = s->label1 ? : id_str1;
3633 } else
3634 label1 = "/dev/null";
3636 err = got_object_id_str(&id_str2, s->id2);
3637 if (err)
3638 return err;
3639 label2 = s->label2 ? : id_str2;
3641 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3642 err = got_error_from_errno("asprintf");
3643 free(id_str1);
3644 free(id_str2);
3645 return err;
3647 free(id_str1);
3648 free(id_str2);
3650 err = draw_file(view, header);
3651 free(header);
3652 return err;
3655 static const struct got_error *
3656 set_selected_commit(struct tog_diff_view_state *s,
3657 struct commit_queue_entry *entry)
3659 const struct got_error *err;
3660 const struct got_object_id_queue *parent_ids;
3661 struct got_commit_object *selected_commit;
3662 struct got_object_qid *pid;
3664 free(s->id2);
3665 s->id2 = got_object_id_dup(entry->id);
3666 if (s->id2 == NULL)
3667 return got_error_from_errno("got_object_id_dup");
3669 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3670 if (err)
3671 return err;
3672 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3673 free(s->id1);
3674 pid = STAILQ_FIRST(parent_ids);
3675 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3676 got_object_commit_close(selected_commit);
3677 return NULL;
3680 static const struct got_error *
3681 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3683 const struct got_error *err = NULL;
3684 struct tog_diff_view_state *s = &view->state.diff;
3685 struct tog_log_view_state *ls;
3686 struct commit_queue_entry *old_selected_entry;
3687 char *line = NULL;
3688 size_t linesize = 0;
3689 ssize_t linelen;
3690 int i;
3692 switch (ch) {
3693 case 'a':
3694 case 'w':
3695 if (ch == 'a')
3696 s->force_text_diff = !s->force_text_diff;
3697 if (ch == 'w')
3698 s->ignore_whitespace = !s->ignore_whitespace;
3699 wclear(view->window);
3700 s->first_displayed_line = 1;
3701 s->last_displayed_line = view->nlines;
3702 s->matched_line = 0;
3703 diff_view_indicate_progress(view);
3704 err = create_diff(s);
3705 break;
3706 case 'g':
3707 case KEY_HOME:
3708 s->first_displayed_line = 1;
3709 break;
3710 case 'G':
3711 case KEY_END:
3712 if (s->eof)
3713 break;
3715 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3716 s->eof = 1;
3717 break;
3718 case 'k':
3719 case KEY_UP:
3720 case CTRL('p'):
3721 if (s->first_displayed_line > 1)
3722 s->first_displayed_line--;
3723 break;
3724 case KEY_PPAGE:
3725 case CTRL('b'):
3726 if (s->first_displayed_line == 1)
3727 break;
3728 i = 0;
3729 while (i++ < view->nlines - 1 &&
3730 s->first_displayed_line > 1)
3731 s->first_displayed_line--;
3732 break;
3733 case 'j':
3734 case KEY_DOWN:
3735 case CTRL('n'):
3736 if (!s->eof)
3737 s->first_displayed_line++;
3738 break;
3739 case KEY_NPAGE:
3740 case CTRL('f'):
3741 case ' ':
3742 if (s->eof)
3743 break;
3744 i = 0;
3745 while (!s->eof && i++ < view->nlines - 1) {
3746 linelen = getline(&line, &linesize, s->f);
3747 s->first_displayed_line++;
3748 if (linelen == -1) {
3749 if (feof(s->f)) {
3750 s->eof = 1;
3751 } else
3752 err = got_ferror(s->f, GOT_ERR_IO);
3753 break;
3756 free(line);
3757 break;
3758 case '[':
3759 if (s->diff_context > 0) {
3760 s->diff_context--;
3761 s->matched_line = 0;
3762 diff_view_indicate_progress(view);
3763 err = create_diff(s);
3764 if (s->first_displayed_line + view->nlines - 1 >
3765 s->nlines) {
3766 s->first_displayed_line = 1;
3767 s->last_displayed_line = view->nlines;
3770 break;
3771 case ']':
3772 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3773 s->diff_context++;
3774 s->matched_line = 0;
3775 diff_view_indicate_progress(view);
3776 err = create_diff(s);
3778 break;
3779 case '<':
3780 case ',':
3781 if (s->log_view == NULL)
3782 break;
3783 ls = &s->log_view->state.log;
3784 old_selected_entry = ls->selected_entry;
3786 err = input_log_view(NULL, s->log_view, KEY_UP);
3787 if (err)
3788 break;
3790 if (old_selected_entry == ls->selected_entry)
3791 break;
3793 err = set_selected_commit(s, ls->selected_entry);
3794 if (err)
3795 break;
3797 s->first_displayed_line = 1;
3798 s->last_displayed_line = view->nlines;
3799 s->matched_line = 0;
3801 diff_view_indicate_progress(view);
3802 err = create_diff(s);
3803 break;
3804 case '>':
3805 case '.':
3806 if (s->log_view == NULL)
3807 break;
3808 ls = &s->log_view->state.log;
3809 old_selected_entry = ls->selected_entry;
3811 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3812 if (err)
3813 break;
3815 if (old_selected_entry == ls->selected_entry)
3816 break;
3818 err = set_selected_commit(s, ls->selected_entry);
3819 if (err)
3820 break;
3822 s->first_displayed_line = 1;
3823 s->last_displayed_line = view->nlines;
3824 s->matched_line = 0;
3826 diff_view_indicate_progress(view);
3827 err = create_diff(s);
3828 break;
3829 default:
3830 break;
3833 return err;
3836 static const struct got_error *
3837 cmd_diff(int argc, char *argv[])
3839 const struct got_error *error = NULL;
3840 struct got_repository *repo = NULL;
3841 struct got_worktree *worktree = NULL;
3842 struct got_object_id *id1 = NULL, *id2 = NULL;
3843 char *repo_path = NULL, *cwd = NULL;
3844 char *id_str1 = NULL, *id_str2 = NULL;
3845 char *label1 = NULL, *label2 = NULL;
3846 int diff_context = 3, ignore_whitespace = 0;
3847 int ch, force_text_diff = 0;
3848 const char *errstr;
3849 struct tog_view *view;
3851 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3852 switch (ch) {
3853 case 'a':
3854 force_text_diff = 1;
3855 break;
3856 case 'C':
3857 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3858 &errstr);
3859 if (errstr != NULL)
3860 err(1, "-C option %s", errstr);
3861 break;
3862 case 'r':
3863 repo_path = realpath(optarg, NULL);
3864 if (repo_path == NULL)
3865 return got_error_from_errno2("realpath",
3866 optarg);
3867 got_path_strip_trailing_slashes(repo_path);
3868 break;
3869 case 'w':
3870 ignore_whitespace = 1;
3871 break;
3872 default:
3873 usage_diff();
3874 /* NOTREACHED */
3878 argc -= optind;
3879 argv += optind;
3881 if (argc == 0) {
3882 usage_diff(); /* TODO show local worktree changes */
3883 } else if (argc == 2) {
3884 id_str1 = argv[0];
3885 id_str2 = argv[1];
3886 } else
3887 usage_diff();
3889 if (repo_path == NULL) {
3890 cwd = getcwd(NULL, 0);
3891 if (cwd == NULL)
3892 return got_error_from_errno("getcwd");
3893 error = got_worktree_open(&worktree, cwd);
3894 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3895 goto done;
3896 if (worktree)
3897 repo_path =
3898 strdup(got_worktree_get_repo_path(worktree));
3899 else
3900 repo_path = strdup(cwd);
3901 if (repo_path == NULL) {
3902 error = got_error_from_errno("strdup");
3903 goto done;
3907 error = got_repo_open(&repo, repo_path, NULL);
3908 if (error)
3909 goto done;
3911 init_curses();
3913 error = apply_unveil(got_repo_get_path(repo), NULL);
3914 if (error)
3915 goto done;
3917 error = tog_load_refs(repo, 0);
3918 if (error)
3919 goto done;
3921 error = got_repo_match_object_id(&id1, &label1, id_str1,
3922 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3923 if (error)
3924 goto done;
3926 error = got_repo_match_object_id(&id2, &label2, id_str2,
3927 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3928 if (error)
3929 goto done;
3931 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3932 if (view == NULL) {
3933 error = got_error_from_errno("view_open");
3934 goto done;
3936 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3937 ignore_whitespace, force_text_diff, NULL, repo);
3938 if (error)
3939 goto done;
3940 error = view_loop(view);
3941 done:
3942 free(label1);
3943 free(label2);
3944 free(repo_path);
3945 free(cwd);
3946 if (repo) {
3947 const struct got_error *close_err = got_repo_close(repo);
3948 if (error == NULL)
3949 error = close_err;
3951 if (worktree)
3952 got_worktree_close(worktree);
3953 tog_free_refs();
3954 return error;
3957 __dead static void
3958 usage_blame(void)
3960 endwin();
3961 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3962 getprogname());
3963 exit(1);
3966 struct tog_blame_line {
3967 int annotated;
3968 struct got_object_id *id;
3971 static const struct got_error *
3972 draw_blame(struct tog_view *view)
3974 struct tog_blame_view_state *s = &view->state.blame;
3975 struct tog_blame *blame = &s->blame;
3976 regmatch_t *regmatch = &view->regmatch;
3977 const struct got_error *err;
3978 int lineno = 0, nprinted = 0;
3979 char *line = NULL;
3980 size_t linesize = 0;
3981 ssize_t linelen;
3982 wchar_t *wline;
3983 int width;
3984 struct tog_blame_line *blame_line;
3985 struct got_object_id *prev_id = NULL;
3986 char *id_str;
3987 struct tog_color *tc;
3989 err = got_object_id_str(&id_str, s->blamed_commit->id);
3990 if (err)
3991 return err;
3993 rewind(blame->f);
3994 werase(view->window);
3996 if (asprintf(&line, "commit %s", id_str) == -1) {
3997 err = got_error_from_errno("asprintf");
3998 free(id_str);
3999 return err;
4002 err = format_line(&wline, &width, line, view->ncols, 0);
4003 free(line);
4004 line = NULL;
4005 if (err)
4006 return err;
4007 if (view_needs_focus_indication(view))
4008 wstandout(view->window);
4009 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4010 if (tc)
4011 wattr_on(view->window,
4012 COLOR_PAIR(tc->colorpair), NULL);
4013 waddwstr(view->window, wline);
4014 if (tc)
4015 wattr_off(view->window,
4016 COLOR_PAIR(tc->colorpair), NULL);
4017 if (view_needs_focus_indication(view))
4018 wstandend(view->window);
4019 free(wline);
4020 wline = NULL;
4021 if (width < view->ncols - 1)
4022 waddch(view->window, '\n');
4024 if (asprintf(&line, "[%d/%d] %s%s",
4025 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4026 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4027 free(id_str);
4028 return got_error_from_errno("asprintf");
4030 free(id_str);
4031 err = format_line(&wline, &width, line, view->ncols, 0);
4032 free(line);
4033 line = NULL;
4034 if (err)
4035 return err;
4036 waddwstr(view->window, wline);
4037 free(wline);
4038 wline = NULL;
4039 if (width < view->ncols - 1)
4040 waddch(view->window, '\n');
4042 s->eof = 0;
4043 while (nprinted < view->nlines - 2) {
4044 linelen = getline(&line, &linesize, blame->f);
4045 if (linelen == -1) {
4046 if (feof(blame->f)) {
4047 s->eof = 1;
4048 break;
4050 free(line);
4051 return got_ferror(blame->f, GOT_ERR_IO);
4053 if (++lineno < s->first_displayed_line)
4054 continue;
4056 if (view->focussed && nprinted == s->selected_line - 1)
4057 wstandout(view->window);
4059 if (blame->nlines > 0) {
4060 blame_line = &blame->lines[lineno - 1];
4061 if (blame_line->annotated && prev_id &&
4062 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4063 !(view->focussed &&
4064 nprinted == s->selected_line - 1)) {
4065 waddstr(view->window, " ");
4066 } else if (blame_line->annotated) {
4067 char *id_str;
4068 err = got_object_id_str(&id_str, blame_line->id);
4069 if (err) {
4070 free(line);
4071 return err;
4073 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4074 if (tc)
4075 wattr_on(view->window,
4076 COLOR_PAIR(tc->colorpair), NULL);
4077 wprintw(view->window, "%.8s", id_str);
4078 if (tc)
4079 wattr_off(view->window,
4080 COLOR_PAIR(tc->colorpair), NULL);
4081 free(id_str);
4082 prev_id = blame_line->id;
4083 } else {
4084 waddstr(view->window, "........");
4085 prev_id = NULL;
4087 } else {
4088 waddstr(view->window, "........");
4089 prev_id = NULL;
4092 if (view->focussed && nprinted == s->selected_line - 1)
4093 wstandend(view->window);
4094 waddstr(view->window, " ");
4096 if (view->ncols <= 9) {
4097 width = 9;
4098 wline = wcsdup(L"");
4099 if (wline == NULL) {
4100 err = got_error_from_errno("wcsdup");
4101 free(line);
4102 return err;
4104 } else if (s->first_displayed_line + nprinted ==
4105 s->matched_line &&
4106 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4107 err = add_matched_line(&width, line, view->ncols - 9, 9,
4108 view->window, regmatch);
4109 if (err) {
4110 free(line);
4111 return err;
4113 width += 9;
4114 } else {
4115 err = format_line(&wline, &width, line,
4116 view->ncols - 9, 9);
4117 waddwstr(view->window, wline);
4118 free(wline);
4119 wline = NULL;
4120 width += 9;
4123 if (width <= view->ncols - 1)
4124 waddch(view->window, '\n');
4125 if (++nprinted == 1)
4126 s->first_displayed_line = lineno;
4128 free(line);
4129 s->last_displayed_line = lineno;
4131 view_vborder(view);
4133 return NULL;
4136 static const struct got_error *
4137 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4139 const struct got_error *err = NULL;
4140 struct tog_blame_cb_args *a = arg;
4141 struct tog_blame_line *line;
4142 int errcode;
4144 if (nlines != a->nlines ||
4145 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4146 return got_error(GOT_ERR_RANGE);
4148 errcode = pthread_mutex_lock(&tog_mutex);
4149 if (errcode)
4150 return got_error_set_errno(errcode, "pthread_mutex_lock");
4152 if (*a->quit) { /* user has quit the blame view */
4153 err = got_error(GOT_ERR_ITER_COMPLETED);
4154 goto done;
4157 if (lineno == -1)
4158 goto done; /* no change in this commit */
4160 line = &a->lines[lineno - 1];
4161 if (line->annotated)
4162 goto done;
4164 line->id = got_object_id_dup(id);
4165 if (line->id == NULL) {
4166 err = got_error_from_errno("got_object_id_dup");
4167 goto done;
4169 line->annotated = 1;
4170 done:
4171 errcode = pthread_mutex_unlock(&tog_mutex);
4172 if (errcode)
4173 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4174 return err;
4177 static void *
4178 blame_thread(void *arg)
4180 const struct got_error *err, *close_err;
4181 struct tog_blame_thread_args *ta = arg;
4182 struct tog_blame_cb_args *a = ta->cb_args;
4183 int errcode;
4185 err = block_signals_used_by_main_thread();
4186 if (err)
4187 return (void *)err;
4189 err = got_blame(ta->path, a->commit_id, ta->repo,
4190 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4191 if (err && err->code == GOT_ERR_CANCELLED)
4192 err = NULL;
4194 errcode = pthread_mutex_lock(&tog_mutex);
4195 if (errcode)
4196 return (void *)got_error_set_errno(errcode,
4197 "pthread_mutex_lock");
4199 close_err = got_repo_close(ta->repo);
4200 if (err == NULL)
4201 err = close_err;
4202 ta->repo = NULL;
4203 *ta->complete = 1;
4205 errcode = pthread_mutex_unlock(&tog_mutex);
4206 if (errcode && err == NULL)
4207 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4209 return (void *)err;
4212 static struct got_object_id *
4213 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4214 int first_displayed_line, int selected_line)
4216 struct tog_blame_line *line;
4218 if (nlines <= 0)
4219 return NULL;
4221 line = &lines[first_displayed_line - 1 + selected_line - 1];
4222 if (!line->annotated)
4223 return NULL;
4225 return line->id;
4228 static const struct got_error *
4229 stop_blame(struct tog_blame *blame)
4231 const struct got_error *err = NULL;
4232 int i;
4234 if (blame->thread) {
4235 int errcode;
4236 errcode = pthread_mutex_unlock(&tog_mutex);
4237 if (errcode)
4238 return got_error_set_errno(errcode,
4239 "pthread_mutex_unlock");
4240 errcode = pthread_join(blame->thread, (void **)&err);
4241 if (errcode)
4242 return got_error_set_errno(errcode, "pthread_join");
4243 errcode = pthread_mutex_lock(&tog_mutex);
4244 if (errcode)
4245 return got_error_set_errno(errcode,
4246 "pthread_mutex_lock");
4247 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4248 err = NULL;
4249 blame->thread = NULL;
4251 if (blame->thread_args.repo) {
4252 const struct got_error *close_err;
4253 close_err = got_repo_close(blame->thread_args.repo);
4254 if (err == NULL)
4255 err = close_err;
4256 blame->thread_args.repo = NULL;
4258 if (blame->f) {
4259 if (fclose(blame->f) == EOF && err == NULL)
4260 err = got_error_from_errno("fclose");
4261 blame->f = NULL;
4263 if (blame->lines) {
4264 for (i = 0; i < blame->nlines; i++)
4265 free(blame->lines[i].id);
4266 free(blame->lines);
4267 blame->lines = NULL;
4269 free(blame->cb_args.commit_id);
4270 blame->cb_args.commit_id = NULL;
4272 return err;
4275 static const struct got_error *
4276 cancel_blame_view(void *arg)
4278 const struct got_error *err = NULL;
4279 int *done = arg;
4280 int errcode;
4282 errcode = pthread_mutex_lock(&tog_mutex);
4283 if (errcode)
4284 return got_error_set_errno(errcode,
4285 "pthread_mutex_unlock");
4287 if (*done)
4288 err = got_error(GOT_ERR_CANCELLED);
4290 errcode = pthread_mutex_unlock(&tog_mutex);
4291 if (errcode)
4292 return got_error_set_errno(errcode,
4293 "pthread_mutex_lock");
4295 return err;
4298 static const struct got_error *
4299 run_blame(struct tog_view *view)
4301 struct tog_blame_view_state *s = &view->state.blame;
4302 struct tog_blame *blame = &s->blame;
4303 const struct got_error *err = NULL;
4304 struct got_blob_object *blob = NULL;
4305 struct got_repository *thread_repo = NULL;
4306 struct got_object_id *obj_id = NULL;
4307 int obj_type;
4309 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4310 s->path);
4311 if (err)
4312 return err;
4314 err = got_object_get_type(&obj_type, s->repo, obj_id);
4315 if (err)
4316 goto done;
4318 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4319 err = got_error(GOT_ERR_OBJ_TYPE);
4320 goto done;
4323 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4324 if (err)
4325 goto done;
4326 blame->f = got_opentemp();
4327 if (blame->f == NULL) {
4328 err = got_error_from_errno("got_opentemp");
4329 goto done;
4331 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4332 &blame->line_offsets, blame->f, blob);
4333 if (err)
4334 goto done;
4335 if (blame->nlines == 0) {
4336 s->blame_complete = 1;
4337 goto done;
4340 /* Don't include \n at EOF in the blame line count. */
4341 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4342 blame->nlines--;
4344 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4345 if (blame->lines == NULL) {
4346 err = got_error_from_errno("calloc");
4347 goto done;
4350 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4351 if (err)
4352 goto done;
4354 blame->cb_args.view = view;
4355 blame->cb_args.lines = blame->lines;
4356 blame->cb_args.nlines = blame->nlines;
4357 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4358 if (blame->cb_args.commit_id == NULL) {
4359 err = got_error_from_errno("got_object_id_dup");
4360 goto done;
4362 blame->cb_args.quit = &s->done;
4364 blame->thread_args.path = s->path;
4365 blame->thread_args.repo = thread_repo;
4366 blame->thread_args.cb_args = &blame->cb_args;
4367 blame->thread_args.complete = &s->blame_complete;
4368 blame->thread_args.cancel_cb = cancel_blame_view;
4369 blame->thread_args.cancel_arg = &s->done;
4370 s->blame_complete = 0;
4372 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4373 s->first_displayed_line = 1;
4374 s->last_displayed_line = view->nlines;
4375 s->selected_line = 1;
4377 s->matched_line = 0;
4379 done:
4380 if (blob)
4381 got_object_blob_close(blob);
4382 free(obj_id);
4383 if (err)
4384 stop_blame(blame);
4385 return err;
4388 static const struct got_error *
4389 open_blame_view(struct tog_view *view, char *path,
4390 struct got_object_id *commit_id, struct got_repository *repo)
4392 const struct got_error *err = NULL;
4393 struct tog_blame_view_state *s = &view->state.blame;
4395 STAILQ_INIT(&s->blamed_commits);
4397 s->path = strdup(path);
4398 if (s->path == NULL)
4399 return got_error_from_errno("strdup");
4401 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4402 if (err) {
4403 free(s->path);
4404 return err;
4407 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4408 s->first_displayed_line = 1;
4409 s->last_displayed_line = view->nlines;
4410 s->selected_line = 1;
4411 s->blame_complete = 0;
4412 s->repo = repo;
4413 s->commit_id = commit_id;
4414 memset(&s->blame, 0, sizeof(s->blame));
4416 STAILQ_INIT(&s->colors);
4417 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4418 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4419 get_color_value("TOG_COLOR_COMMIT"));
4420 if (err)
4421 return err;
4424 view->show = show_blame_view;
4425 view->input = input_blame_view;
4426 view->close = close_blame_view;
4427 view->search_start = search_start_blame_view;
4428 view->search_next = search_next_blame_view;
4430 return run_blame(view);
4433 static const struct got_error *
4434 close_blame_view(struct tog_view *view)
4436 const struct got_error *err = NULL;
4437 struct tog_blame_view_state *s = &view->state.blame;
4439 if (s->blame.thread)
4440 err = stop_blame(&s->blame);
4442 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4443 struct got_object_qid *blamed_commit;
4444 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4445 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4446 got_object_qid_free(blamed_commit);
4449 free(s->path);
4450 free_colors(&s->colors);
4452 return err;
4455 static const struct got_error *
4456 search_start_blame_view(struct tog_view *view)
4458 struct tog_blame_view_state *s = &view->state.blame;
4460 s->matched_line = 0;
4461 return NULL;
4464 static const struct got_error *
4465 search_next_blame_view(struct tog_view *view)
4467 struct tog_blame_view_state *s = &view->state.blame;
4468 int lineno;
4469 char *line = NULL;
4470 size_t linesize = 0;
4471 ssize_t linelen;
4473 if (!view->searching) {
4474 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4475 return NULL;
4478 if (s->matched_line) {
4479 if (view->searching == TOG_SEARCH_FORWARD)
4480 lineno = s->matched_line + 1;
4481 else
4482 lineno = s->matched_line - 1;
4483 } else
4484 lineno = s->first_displayed_line - 1 + s->selected_line;
4486 while (1) {
4487 off_t offset;
4489 if (lineno <= 0 || lineno > s->blame.nlines) {
4490 if (s->matched_line == 0) {
4491 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4492 break;
4495 if (view->searching == TOG_SEARCH_FORWARD)
4496 lineno = 1;
4497 else
4498 lineno = s->blame.nlines;
4501 offset = s->blame.line_offsets[lineno - 1];
4502 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4503 free(line);
4504 return got_error_from_errno("fseeko");
4506 linelen = getline(&line, &linesize, s->blame.f);
4507 if (linelen != -1 &&
4508 match_line(line, &view->regex, 1, &view->regmatch)) {
4509 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4510 s->matched_line = lineno;
4511 break;
4513 if (view->searching == TOG_SEARCH_FORWARD)
4514 lineno++;
4515 else
4516 lineno--;
4518 free(line);
4520 if (s->matched_line) {
4521 s->first_displayed_line = s->matched_line;
4522 s->selected_line = 1;
4525 return NULL;
4528 static const struct got_error *
4529 show_blame_view(struct tog_view *view)
4531 const struct got_error *err = NULL;
4532 struct tog_blame_view_state *s = &view->state.blame;
4533 int errcode;
4535 if (s->blame.thread == NULL && !s->blame_complete) {
4536 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4537 &s->blame.thread_args);
4538 if (errcode)
4539 return got_error_set_errno(errcode, "pthread_create");
4541 halfdelay(1); /* fast refresh while annotating */
4544 if (s->blame_complete)
4545 halfdelay(10); /* disable fast refresh */
4547 err = draw_blame(view);
4549 view_vborder(view);
4550 return err;
4553 static const struct got_error *
4554 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4556 const struct got_error *err = NULL, *thread_err = NULL;
4557 struct tog_view *diff_view;
4558 struct tog_blame_view_state *s = &view->state.blame;
4559 int begin_x = 0;
4561 switch (ch) {
4562 case 'q':
4563 s->done = 1;
4564 break;
4565 case 'g':
4566 case KEY_HOME:
4567 s->selected_line = 1;
4568 s->first_displayed_line = 1;
4569 break;
4570 case 'G':
4571 case KEY_END:
4572 if (s->blame.nlines < view->nlines - 2) {
4573 s->selected_line = s->blame.nlines;
4574 s->first_displayed_line = 1;
4575 } else {
4576 s->selected_line = view->nlines - 2;
4577 s->first_displayed_line = s->blame.nlines -
4578 (view->nlines - 3);
4580 break;
4581 case 'k':
4582 case KEY_UP:
4583 case CTRL('p'):
4584 if (s->selected_line > 1)
4585 s->selected_line--;
4586 else if (s->selected_line == 1 &&
4587 s->first_displayed_line > 1)
4588 s->first_displayed_line--;
4589 break;
4590 case KEY_PPAGE:
4591 case CTRL('b'):
4592 if (s->first_displayed_line == 1) {
4593 s->selected_line = 1;
4594 break;
4596 if (s->first_displayed_line > view->nlines - 2)
4597 s->first_displayed_line -=
4598 (view->nlines - 2);
4599 else
4600 s->first_displayed_line = 1;
4601 break;
4602 case 'j':
4603 case KEY_DOWN:
4604 case CTRL('n'):
4605 if (s->selected_line < view->nlines - 2 &&
4606 s->first_displayed_line +
4607 s->selected_line <= s->blame.nlines)
4608 s->selected_line++;
4609 else if (s->last_displayed_line <
4610 s->blame.nlines)
4611 s->first_displayed_line++;
4612 break;
4613 case 'b':
4614 case 'p': {
4615 struct got_object_id *id = NULL;
4616 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4617 s->first_displayed_line, s->selected_line);
4618 if (id == NULL)
4619 break;
4620 if (ch == 'p') {
4621 struct got_commit_object *commit;
4622 struct got_object_qid *pid;
4623 struct got_object_id *blob_id = NULL;
4624 int obj_type;
4625 err = got_object_open_as_commit(&commit,
4626 s->repo, id);
4627 if (err)
4628 break;
4629 pid = STAILQ_FIRST(
4630 got_object_commit_get_parent_ids(commit));
4631 if (pid == NULL) {
4632 got_object_commit_close(commit);
4633 break;
4635 /* Check if path history ends here. */
4636 err = got_object_id_by_path(&blob_id, s->repo,
4637 pid->id, s->path);
4638 if (err) {
4639 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4640 err = NULL;
4641 got_object_commit_close(commit);
4642 break;
4644 err = got_object_get_type(&obj_type, s->repo,
4645 blob_id);
4646 free(blob_id);
4647 /* Can't blame non-blob type objects. */
4648 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4649 got_object_commit_close(commit);
4650 break;
4652 err = got_object_qid_alloc(&s->blamed_commit,
4653 pid->id);
4654 got_object_commit_close(commit);
4655 } else {
4656 if (got_object_id_cmp(id,
4657 s->blamed_commit->id) == 0)
4658 break;
4659 err = got_object_qid_alloc(&s->blamed_commit,
4660 id);
4662 if (err)
4663 break;
4664 s->done = 1;
4665 thread_err = stop_blame(&s->blame);
4666 s->done = 0;
4667 if (thread_err)
4668 break;
4669 STAILQ_INSERT_HEAD(&s->blamed_commits,
4670 s->blamed_commit, entry);
4671 err = run_blame(view);
4672 if (err)
4673 break;
4674 break;
4676 case 'B': {
4677 struct got_object_qid *first;
4678 first = STAILQ_FIRST(&s->blamed_commits);
4679 if (!got_object_id_cmp(first->id, s->commit_id))
4680 break;
4681 s->done = 1;
4682 thread_err = stop_blame(&s->blame);
4683 s->done = 0;
4684 if (thread_err)
4685 break;
4686 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4687 got_object_qid_free(s->blamed_commit);
4688 s->blamed_commit =
4689 STAILQ_FIRST(&s->blamed_commits);
4690 err = run_blame(view);
4691 if (err)
4692 break;
4693 break;
4695 case KEY_ENTER:
4696 case '\r': {
4697 struct got_object_id *id = NULL;
4698 struct got_object_qid *pid;
4699 struct got_commit_object *commit = NULL;
4700 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4701 s->first_displayed_line, s->selected_line);
4702 if (id == NULL)
4703 break;
4704 err = got_object_open_as_commit(&commit, s->repo, id);
4705 if (err)
4706 break;
4707 pid = STAILQ_FIRST(
4708 got_object_commit_get_parent_ids(commit));
4709 if (view_is_parent_view(view))
4710 begin_x = view_split_begin_x(view->begin_x);
4711 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4712 if (diff_view == NULL) {
4713 got_object_commit_close(commit);
4714 err = got_error_from_errno("view_open");
4715 break;
4717 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4718 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4719 got_object_commit_close(commit);
4720 if (err) {
4721 view_close(diff_view);
4722 break;
4724 view->focussed = 0;
4725 diff_view->focussed = 1;
4726 if (view_is_parent_view(view)) {
4727 err = view_close_child(view);
4728 if (err)
4729 break;
4730 view_set_child(view, diff_view);
4731 view->focus_child = 1;
4732 } else
4733 *new_view = diff_view;
4734 if (err)
4735 break;
4736 break;
4738 case KEY_NPAGE:
4739 case CTRL('f'):
4740 case ' ':
4741 if (s->last_displayed_line >= s->blame.nlines &&
4742 s->selected_line >= MIN(s->blame.nlines,
4743 view->nlines - 2)) {
4744 break;
4746 if (s->last_displayed_line >= s->blame.nlines &&
4747 s->selected_line < view->nlines - 2) {
4748 s->selected_line = MIN(s->blame.nlines,
4749 view->nlines - 2);
4750 break;
4752 if (s->last_displayed_line + view->nlines - 2
4753 <= s->blame.nlines)
4754 s->first_displayed_line +=
4755 view->nlines - 2;
4756 else
4757 s->first_displayed_line =
4758 s->blame.nlines -
4759 (view->nlines - 3);
4760 break;
4761 case KEY_RESIZE:
4762 if (s->selected_line > view->nlines - 2) {
4763 s->selected_line = MIN(s->blame.nlines,
4764 view->nlines - 2);
4766 break;
4767 default:
4768 break;
4770 return thread_err ? thread_err : err;
4773 static const struct got_error *
4774 cmd_blame(int argc, char *argv[])
4776 const struct got_error *error;
4777 struct got_repository *repo = NULL;
4778 struct got_worktree *worktree = NULL;
4779 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4780 char *link_target = NULL;
4781 struct got_object_id *commit_id = NULL;
4782 char *commit_id_str = NULL;
4783 int ch;
4784 struct tog_view *view;
4786 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4787 switch (ch) {
4788 case 'c':
4789 commit_id_str = optarg;
4790 break;
4791 case 'r':
4792 repo_path = realpath(optarg, NULL);
4793 if (repo_path == NULL)
4794 return got_error_from_errno2("realpath",
4795 optarg);
4796 break;
4797 default:
4798 usage_blame();
4799 /* NOTREACHED */
4803 argc -= optind;
4804 argv += optind;
4806 if (argc != 1)
4807 usage_blame();
4809 if (repo_path == NULL) {
4810 cwd = getcwd(NULL, 0);
4811 if (cwd == NULL)
4812 return got_error_from_errno("getcwd");
4813 error = got_worktree_open(&worktree, cwd);
4814 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4815 goto done;
4816 if (worktree)
4817 repo_path =
4818 strdup(got_worktree_get_repo_path(worktree));
4819 else
4820 repo_path = strdup(cwd);
4821 if (repo_path == NULL) {
4822 error = got_error_from_errno("strdup");
4823 goto done;
4827 error = got_repo_open(&repo, repo_path, NULL);
4828 if (error != NULL)
4829 goto done;
4831 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4832 worktree);
4833 if (error)
4834 goto done;
4836 init_curses();
4838 error = apply_unveil(got_repo_get_path(repo), NULL);
4839 if (error)
4840 goto done;
4842 error = tog_load_refs(repo, 0);
4843 if (error)
4844 goto done;
4846 if (commit_id_str == NULL) {
4847 struct got_reference *head_ref;
4848 error = got_ref_open(&head_ref, repo, worktree ?
4849 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4850 if (error != NULL)
4851 goto done;
4852 error = got_ref_resolve(&commit_id, repo, head_ref);
4853 got_ref_close(head_ref);
4854 } else {
4855 error = got_repo_match_object_id(&commit_id, NULL,
4856 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4858 if (error != NULL)
4859 goto done;
4861 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4862 if (view == NULL) {
4863 error = got_error_from_errno("view_open");
4864 goto done;
4867 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4868 commit_id, repo);
4869 if (error)
4870 goto done;
4872 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4873 commit_id, repo);
4874 if (error)
4875 goto done;
4876 if (worktree) {
4877 /* Release work tree lock. */
4878 got_worktree_close(worktree);
4879 worktree = NULL;
4881 error = view_loop(view);
4882 done:
4883 free(repo_path);
4884 free(in_repo_path);
4885 free(link_target);
4886 free(cwd);
4887 free(commit_id);
4888 if (worktree)
4889 got_worktree_close(worktree);
4890 if (repo) {
4891 const struct got_error *close_err = got_repo_close(repo);
4892 if (error == NULL)
4893 error = close_err;
4895 tog_free_refs();
4896 return error;
4899 static const struct got_error *
4900 draw_tree_entries(struct tog_view *view, const char *parent_path)
4902 struct tog_tree_view_state *s = &view->state.tree;
4903 const struct got_error *err = NULL;
4904 struct got_tree_entry *te;
4905 wchar_t *wline;
4906 struct tog_color *tc;
4907 int width, n, i, nentries;
4908 int limit = view->nlines;
4910 s->ndisplayed = 0;
4912 werase(view->window);
4914 if (limit == 0)
4915 return NULL;
4917 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4918 if (err)
4919 return err;
4920 if (view_needs_focus_indication(view))
4921 wstandout(view->window);
4922 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4923 if (tc)
4924 wattr_on(view->window,
4925 COLOR_PAIR(tc->colorpair), NULL);
4926 waddwstr(view->window, wline);
4927 if (tc)
4928 wattr_off(view->window,
4929 COLOR_PAIR(tc->colorpair), NULL);
4930 if (view_needs_focus_indication(view))
4931 wstandend(view->window);
4932 free(wline);
4933 wline = NULL;
4934 if (width < view->ncols - 1)
4935 waddch(view->window, '\n');
4936 if (--limit <= 0)
4937 return NULL;
4938 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4939 if (err)
4940 return err;
4941 waddwstr(view->window, wline);
4942 free(wline);
4943 wline = NULL;
4944 if (width < view->ncols - 1)
4945 waddch(view->window, '\n');
4946 if (--limit <= 0)
4947 return NULL;
4948 waddch(view->window, '\n');
4949 if (--limit <= 0)
4950 return NULL;
4952 if (s->first_displayed_entry == NULL) {
4953 te = got_object_tree_get_first_entry(s->tree);
4954 if (s->selected == 0) {
4955 if (view->focussed)
4956 wstandout(view->window);
4957 s->selected_entry = NULL;
4959 waddstr(view->window, " ..\n"); /* parent directory */
4960 if (s->selected == 0 && view->focussed)
4961 wstandend(view->window);
4962 s->ndisplayed++;
4963 if (--limit <= 0)
4964 return NULL;
4965 n = 1;
4966 } else {
4967 n = 0;
4968 te = s->first_displayed_entry;
4971 nentries = got_object_tree_get_nentries(s->tree);
4972 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4973 char *line = NULL, *id_str = NULL, *link_target = NULL;
4974 const char *modestr = "";
4975 mode_t mode;
4977 te = got_object_tree_get_entry(s->tree, i);
4978 mode = got_tree_entry_get_mode(te);
4980 if (s->show_ids) {
4981 err = got_object_id_str(&id_str,
4982 got_tree_entry_get_id(te));
4983 if (err)
4984 return got_error_from_errno(
4985 "got_object_id_str");
4987 if (got_object_tree_entry_is_submodule(te))
4988 modestr = "$";
4989 else if (S_ISLNK(mode)) {
4990 int i;
4992 err = got_tree_entry_get_symlink_target(&link_target,
4993 te, s->repo);
4994 if (err) {
4995 free(id_str);
4996 return err;
4998 for (i = 0; i < strlen(link_target); i++) {
4999 if (!isprint((unsigned char)link_target[i]))
5000 link_target[i] = '?';
5002 modestr = "@";
5004 else if (S_ISDIR(mode))
5005 modestr = "/";
5006 else if (mode & S_IXUSR)
5007 modestr = "*";
5008 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5009 got_tree_entry_get_name(te), modestr,
5010 link_target ? " -> ": "",
5011 link_target ? link_target : "") == -1) {
5012 free(id_str);
5013 free(link_target);
5014 return got_error_from_errno("asprintf");
5016 free(id_str);
5017 free(link_target);
5018 err = format_line(&wline, &width, line, view->ncols, 0);
5019 if (err) {
5020 free(line);
5021 break;
5023 if (n == s->selected) {
5024 if (view->focussed)
5025 wstandout(view->window);
5026 s->selected_entry = te;
5028 tc = match_color(&s->colors, line);
5029 if (tc)
5030 wattr_on(view->window,
5031 COLOR_PAIR(tc->colorpair), NULL);
5032 waddwstr(view->window, wline);
5033 if (tc)
5034 wattr_off(view->window,
5035 COLOR_PAIR(tc->colorpair), NULL);
5036 if (width < view->ncols - 1)
5037 waddch(view->window, '\n');
5038 if (n == s->selected && view->focussed)
5039 wstandend(view->window);
5040 free(line);
5041 free(wline);
5042 wline = NULL;
5043 n++;
5044 s->ndisplayed++;
5045 s->last_displayed_entry = te;
5046 if (--limit <= 0)
5047 break;
5050 return err;
5053 static void
5054 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5056 struct got_tree_entry *te;
5057 int isroot = s->tree == s->root;
5058 int i = 0;
5060 if (s->first_displayed_entry == NULL)
5061 return;
5063 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5064 while (i++ < maxscroll) {
5065 if (te == NULL) {
5066 if (!isroot)
5067 s->first_displayed_entry = NULL;
5068 break;
5070 s->first_displayed_entry = te;
5071 te = got_tree_entry_get_prev(s->tree, te);
5075 static void
5076 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5078 struct got_tree_entry *next, *last;
5079 int n = 0;
5081 if (s->first_displayed_entry)
5082 next = got_tree_entry_get_next(s->tree,
5083 s->first_displayed_entry);
5084 else
5085 next = got_object_tree_get_first_entry(s->tree);
5087 last = s->last_displayed_entry;
5088 while (next && last && n++ < maxscroll) {
5089 last = got_tree_entry_get_next(s->tree, last);
5090 if (last) {
5091 s->first_displayed_entry = next;
5092 next = got_tree_entry_get_next(s->tree, next);
5097 static const struct got_error *
5098 tree_entry_path(char **path, struct tog_parent_trees *parents,
5099 struct got_tree_entry *te)
5101 const struct got_error *err = NULL;
5102 struct tog_parent_tree *pt;
5103 size_t len = 2; /* for leading slash and NUL */
5105 TAILQ_FOREACH(pt, parents, entry)
5106 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5107 + 1 /* slash */;
5108 if (te)
5109 len += strlen(got_tree_entry_get_name(te));
5111 *path = calloc(1, len);
5112 if (path == NULL)
5113 return got_error_from_errno("calloc");
5115 (*path)[0] = '/';
5116 pt = TAILQ_LAST(parents, tog_parent_trees);
5117 while (pt) {
5118 const char *name = got_tree_entry_get_name(pt->selected_entry);
5119 if (strlcat(*path, name, len) >= len) {
5120 err = got_error(GOT_ERR_NO_SPACE);
5121 goto done;
5123 if (strlcat(*path, "/", len) >= len) {
5124 err = got_error(GOT_ERR_NO_SPACE);
5125 goto done;
5127 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5129 if (te) {
5130 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5131 err = got_error(GOT_ERR_NO_SPACE);
5132 goto done;
5135 done:
5136 if (err) {
5137 free(*path);
5138 *path = NULL;
5140 return err;
5143 static const struct got_error *
5144 blame_tree_entry(struct tog_view **new_view, int begin_x,
5145 struct got_tree_entry *te, struct tog_parent_trees *parents,
5146 struct got_object_id *commit_id, struct got_repository *repo)
5148 const struct got_error *err = NULL;
5149 char *path;
5150 struct tog_view *blame_view;
5152 *new_view = NULL;
5154 err = tree_entry_path(&path, parents, te);
5155 if (err)
5156 return err;
5158 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5159 if (blame_view == NULL) {
5160 err = got_error_from_errno("view_open");
5161 goto done;
5164 err = open_blame_view(blame_view, path, commit_id, repo);
5165 if (err) {
5166 if (err->code == GOT_ERR_CANCELLED)
5167 err = NULL;
5168 view_close(blame_view);
5169 } else
5170 *new_view = blame_view;
5171 done:
5172 free(path);
5173 return err;
5176 static const struct got_error *
5177 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5178 struct tog_tree_view_state *s)
5180 struct tog_view *log_view;
5181 const struct got_error *err = NULL;
5182 char *path;
5184 *new_view = NULL;
5186 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5187 if (log_view == NULL)
5188 return got_error_from_errno("view_open");
5190 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5191 if (err)
5192 return err;
5194 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5195 path, 0);
5196 if (err)
5197 view_close(log_view);
5198 else
5199 *new_view = log_view;
5200 free(path);
5201 return err;
5204 static const struct got_error *
5205 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5206 const char *head_ref_name, struct got_repository *repo)
5208 const struct got_error *err = NULL;
5209 char *commit_id_str = NULL;
5210 struct tog_tree_view_state *s = &view->state.tree;
5211 struct got_commit_object *commit = NULL;
5213 TAILQ_INIT(&s->parents);
5214 STAILQ_INIT(&s->colors);
5216 s->commit_id = got_object_id_dup(commit_id);
5217 if (s->commit_id == NULL)
5218 return got_error_from_errno("got_object_id_dup");
5220 err = got_object_open_as_commit(&commit, repo, commit_id);
5221 if (err)
5222 goto done;
5225 * The root is opened here and will be closed when the view is closed.
5226 * Any visited subtrees and their path-wise parents are opened and
5227 * closed on demand.
5229 err = got_object_open_as_tree(&s->root, repo,
5230 got_object_commit_get_tree_id(commit));
5231 if (err)
5232 goto done;
5233 s->tree = s->root;
5235 err = got_object_id_str(&commit_id_str, commit_id);
5236 if (err != NULL)
5237 goto done;
5239 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5240 err = got_error_from_errno("asprintf");
5241 goto done;
5244 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5245 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5246 if (head_ref_name) {
5247 s->head_ref_name = strdup(head_ref_name);
5248 if (s->head_ref_name == NULL) {
5249 err = got_error_from_errno("strdup");
5250 goto done;
5253 s->repo = repo;
5255 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5256 err = add_color(&s->colors, "\\$$",
5257 TOG_COLOR_TREE_SUBMODULE,
5258 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5259 if (err)
5260 goto done;
5261 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5262 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5263 if (err)
5264 goto done;
5265 err = add_color(&s->colors, "/$",
5266 TOG_COLOR_TREE_DIRECTORY,
5267 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5268 if (err)
5269 goto done;
5271 err = add_color(&s->colors, "\\*$",
5272 TOG_COLOR_TREE_EXECUTABLE,
5273 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5274 if (err)
5275 goto done;
5277 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5278 get_color_value("TOG_COLOR_COMMIT"));
5279 if (err)
5280 goto done;
5283 view->show = show_tree_view;
5284 view->input = input_tree_view;
5285 view->close = close_tree_view;
5286 view->search_start = search_start_tree_view;
5287 view->search_next = search_next_tree_view;
5288 done:
5289 free(commit_id_str);
5290 if (commit)
5291 got_object_commit_close(commit);
5292 if (err)
5293 close_tree_view(view);
5294 return err;
5297 static const struct got_error *
5298 close_tree_view(struct tog_view *view)
5300 struct tog_tree_view_state *s = &view->state.tree;
5302 free_colors(&s->colors);
5303 free(s->tree_label);
5304 s->tree_label = NULL;
5305 free(s->commit_id);
5306 s->commit_id = NULL;
5307 free(s->head_ref_name);
5308 s->head_ref_name = NULL;
5309 while (!TAILQ_EMPTY(&s->parents)) {
5310 struct tog_parent_tree *parent;
5311 parent = TAILQ_FIRST(&s->parents);
5312 TAILQ_REMOVE(&s->parents, parent, entry);
5313 if (parent->tree != s->root)
5314 got_object_tree_close(parent->tree);
5315 free(parent);
5318 if (s->tree != NULL && s->tree != s->root)
5319 got_object_tree_close(s->tree);
5320 if (s->root)
5321 got_object_tree_close(s->root);
5322 return NULL;
5325 static const struct got_error *
5326 search_start_tree_view(struct tog_view *view)
5328 struct tog_tree_view_state *s = &view->state.tree;
5330 s->matched_entry = NULL;
5331 return NULL;
5334 static int
5335 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5337 regmatch_t regmatch;
5339 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5340 0) == 0;
5343 static const struct got_error *
5344 search_next_tree_view(struct tog_view *view)
5346 struct tog_tree_view_state *s = &view->state.tree;
5347 struct got_tree_entry *te = NULL;
5349 if (!view->searching) {
5350 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5351 return NULL;
5354 if (s->matched_entry) {
5355 if (view->searching == TOG_SEARCH_FORWARD) {
5356 if (s->selected_entry)
5357 te = got_tree_entry_get_next(s->tree,
5358 s->selected_entry);
5359 else
5360 te = got_object_tree_get_first_entry(s->tree);
5361 } else {
5362 if (s->selected_entry == NULL)
5363 te = got_object_tree_get_last_entry(s->tree);
5364 else
5365 te = got_tree_entry_get_prev(s->tree,
5366 s->selected_entry);
5368 } else {
5369 if (s->selected_entry)
5370 te = s->selected_entry;
5371 else if (view->searching == TOG_SEARCH_FORWARD)
5372 te = got_object_tree_get_first_entry(s->tree);
5373 else
5374 te = got_object_tree_get_last_entry(s->tree);
5377 while (1) {
5378 if (te == NULL) {
5379 if (s->matched_entry == NULL) {
5380 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5381 return NULL;
5383 if (view->searching == TOG_SEARCH_FORWARD)
5384 te = got_object_tree_get_first_entry(s->tree);
5385 else
5386 te = got_object_tree_get_last_entry(s->tree);
5389 if (match_tree_entry(te, &view->regex)) {
5390 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5391 s->matched_entry = te;
5392 break;
5395 if (view->searching == TOG_SEARCH_FORWARD)
5396 te = got_tree_entry_get_next(s->tree, te);
5397 else
5398 te = got_tree_entry_get_prev(s->tree, te);
5401 if (s->matched_entry) {
5402 s->first_displayed_entry = s->matched_entry;
5403 s->selected = 0;
5406 return NULL;
5409 static const struct got_error *
5410 show_tree_view(struct tog_view *view)
5412 const struct got_error *err = NULL;
5413 struct tog_tree_view_state *s = &view->state.tree;
5414 char *parent_path;
5416 err = tree_entry_path(&parent_path, &s->parents, NULL);
5417 if (err)
5418 return err;
5420 err = draw_tree_entries(view, parent_path);
5421 free(parent_path);
5423 view_vborder(view);
5424 return err;
5427 static const struct got_error *
5428 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5430 const struct got_error *err = NULL;
5431 struct tog_tree_view_state *s = &view->state.tree;
5432 struct tog_view *log_view, *ref_view;
5433 struct got_tree_entry *te;
5434 int begin_x = 0, n;
5436 switch (ch) {
5437 case 'i':
5438 s->show_ids = !s->show_ids;
5439 break;
5440 case 'l':
5441 if (!s->selected_entry)
5442 break;
5443 if (view_is_parent_view(view))
5444 begin_x = view_split_begin_x(view->begin_x);
5445 err = log_selected_tree_entry(&log_view, begin_x, s);
5446 view->focussed = 0;
5447 log_view->focussed = 1;
5448 if (view_is_parent_view(view)) {
5449 err = view_close_child(view);
5450 if (err)
5451 return err;
5452 view_set_child(view, log_view);
5453 view->focus_child = 1;
5454 } else
5455 *new_view = log_view;
5456 break;
5457 case 'r':
5458 if (view_is_parent_view(view))
5459 begin_x = view_split_begin_x(view->begin_x);
5460 ref_view = view_open(view->nlines, view->ncols,
5461 view->begin_y, begin_x, TOG_VIEW_REF);
5462 if (ref_view == NULL)
5463 return got_error_from_errno("view_open");
5464 err = open_ref_view(ref_view, s->repo);
5465 if (err) {
5466 view_close(ref_view);
5467 return err;
5469 view->focussed = 0;
5470 ref_view->focussed = 1;
5471 if (view_is_parent_view(view)) {
5472 err = view_close_child(view);
5473 if (err)
5474 return err;
5475 view_set_child(view, ref_view);
5476 view->focus_child = 1;
5477 } else
5478 *new_view = ref_view;
5479 break;
5480 case 'g':
5481 case KEY_HOME:
5482 s->selected = 0;
5483 if (s->tree == s->root)
5484 s->first_displayed_entry =
5485 got_object_tree_get_first_entry(s->tree);
5486 else
5487 s->first_displayed_entry = NULL;
5488 break;
5489 case 'G':
5490 case KEY_END:
5491 s->selected = 0;
5492 te = got_object_tree_get_last_entry(s->tree);
5493 for (n = 0; n < view->nlines - 3; n++) {
5494 if (te == NULL) {
5495 if(s->tree != s->root) {
5496 s->first_displayed_entry = NULL;
5497 n++;
5499 break;
5501 s->first_displayed_entry = te;
5502 te = got_tree_entry_get_prev(s->tree, te);
5504 if (n > 0)
5505 s->selected = n - 1;
5506 break;
5507 case 'k':
5508 case KEY_UP:
5509 case CTRL('p'):
5510 if (s->selected > 0) {
5511 s->selected--;
5512 break;
5514 tree_scroll_up(s, 1);
5515 break;
5516 case KEY_PPAGE:
5517 case CTRL('b'):
5518 if (s->tree == s->root) {
5519 if (got_object_tree_get_first_entry(s->tree) ==
5520 s->first_displayed_entry)
5521 s->selected = 0;
5522 } else {
5523 if (s->first_displayed_entry == NULL)
5524 s->selected = 0;
5526 tree_scroll_up(s, MAX(0, view->nlines - 3));
5527 break;
5528 case 'j':
5529 case KEY_DOWN:
5530 case CTRL('n'):
5531 if (s->selected < s->ndisplayed - 1) {
5532 s->selected++;
5533 break;
5535 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5536 == NULL)
5537 /* can't scroll any further */
5538 break;
5539 tree_scroll_down(s, 1);
5540 break;
5541 case KEY_NPAGE:
5542 case CTRL('f'):
5543 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5544 == NULL) {
5545 /* can't scroll any further; move cursor down */
5546 if (s->selected < s->ndisplayed - 1)
5547 s->selected = s->ndisplayed - 1;
5548 break;
5550 tree_scroll_down(s, view->nlines - 3);
5551 break;
5552 case KEY_ENTER:
5553 case '\r':
5554 case KEY_BACKSPACE:
5555 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5556 struct tog_parent_tree *parent;
5557 /* user selected '..' */
5558 if (s->tree == s->root)
5559 break;
5560 parent = TAILQ_FIRST(&s->parents);
5561 TAILQ_REMOVE(&s->parents, parent,
5562 entry);
5563 got_object_tree_close(s->tree);
5564 s->tree = parent->tree;
5565 s->first_displayed_entry =
5566 parent->first_displayed_entry;
5567 s->selected_entry =
5568 parent->selected_entry;
5569 s->selected = parent->selected;
5570 free(parent);
5571 } else if (S_ISDIR(got_tree_entry_get_mode(
5572 s->selected_entry))) {
5573 struct got_tree_object *subtree;
5574 err = got_object_open_as_tree(&subtree, s->repo,
5575 got_tree_entry_get_id(s->selected_entry));
5576 if (err)
5577 break;
5578 err = tree_view_visit_subtree(s, subtree);
5579 if (err) {
5580 got_object_tree_close(subtree);
5581 break;
5583 } else if (S_ISREG(got_tree_entry_get_mode(
5584 s->selected_entry))) {
5585 struct tog_view *blame_view;
5586 int begin_x = view_is_parent_view(view) ?
5587 view_split_begin_x(view->begin_x) : 0;
5589 err = blame_tree_entry(&blame_view, begin_x,
5590 s->selected_entry, &s->parents,
5591 s->commit_id, s->repo);
5592 if (err)
5593 break;
5594 view->focussed = 0;
5595 blame_view->focussed = 1;
5596 if (view_is_parent_view(view)) {
5597 err = view_close_child(view);
5598 if (err)
5599 return err;
5600 view_set_child(view, blame_view);
5601 view->focus_child = 1;
5602 } else
5603 *new_view = blame_view;
5605 break;
5606 case KEY_RESIZE:
5607 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5608 s->selected = view->nlines - 4;
5609 break;
5610 default:
5611 break;
5614 return err;
5617 __dead static void
5618 usage_tree(void)
5620 endwin();
5621 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5622 getprogname());
5623 exit(1);
5626 static const struct got_error *
5627 cmd_tree(int argc, char *argv[])
5629 const struct got_error *error;
5630 struct got_repository *repo = NULL;
5631 struct got_worktree *worktree = NULL;
5632 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5633 struct got_object_id *commit_id = NULL;
5634 const char *commit_id_arg = NULL;
5635 char *label = NULL;
5636 struct got_reference *ref = NULL;
5637 const char *head_ref_name = NULL;
5638 int ch;
5639 struct tog_view *view;
5641 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5642 switch (ch) {
5643 case 'c':
5644 commit_id_arg = optarg;
5645 break;
5646 case 'r':
5647 repo_path = realpath(optarg, NULL);
5648 if (repo_path == NULL)
5649 return got_error_from_errno2("realpath",
5650 optarg);
5651 break;
5652 default:
5653 usage_tree();
5654 /* NOTREACHED */
5658 argc -= optind;
5659 argv += optind;
5661 if (argc > 1)
5662 usage_tree();
5664 if (repo_path == NULL) {
5665 cwd = getcwd(NULL, 0);
5666 if (cwd == NULL)
5667 return got_error_from_errno("getcwd");
5668 error = got_worktree_open(&worktree, cwd);
5669 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5670 goto done;
5671 if (worktree)
5672 repo_path =
5673 strdup(got_worktree_get_repo_path(worktree));
5674 else
5675 repo_path = strdup(cwd);
5676 if (repo_path == NULL) {
5677 error = got_error_from_errno("strdup");
5678 goto done;
5682 error = got_repo_open(&repo, repo_path, NULL);
5683 if (error != NULL)
5684 goto done;
5686 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5687 repo, worktree);
5688 if (error)
5689 goto done;
5691 init_curses();
5693 error = apply_unveil(got_repo_get_path(repo), NULL);
5694 if (error)
5695 goto done;
5697 error = tog_load_refs(repo, 0);
5698 if (error)
5699 goto done;
5701 if (commit_id_arg == NULL) {
5702 error = got_repo_match_object_id(&commit_id, &label,
5703 worktree ? got_worktree_get_head_ref_name(worktree) :
5704 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5705 if (error)
5706 goto done;
5707 head_ref_name = label;
5708 } else {
5709 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5710 if (error == NULL)
5711 head_ref_name = got_ref_get_name(ref);
5712 else if (error->code != GOT_ERR_NOT_REF)
5713 goto done;
5714 error = got_repo_match_object_id(&commit_id, NULL,
5715 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5716 if (error)
5717 goto done;
5720 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5721 if (view == NULL) {
5722 error = got_error_from_errno("view_open");
5723 goto done;
5725 error = open_tree_view(view, commit_id, head_ref_name, repo);
5726 if (error)
5727 goto done;
5728 if (!got_path_is_root_dir(in_repo_path)) {
5729 error = tree_view_walk_path(&view->state.tree, commit_id,
5730 in_repo_path);
5731 if (error)
5732 goto done;
5735 if (worktree) {
5736 /* Release work tree lock. */
5737 got_worktree_close(worktree);
5738 worktree = NULL;
5740 error = view_loop(view);
5741 done:
5742 free(repo_path);
5743 free(cwd);
5744 free(commit_id);
5745 free(label);
5746 if (ref)
5747 got_ref_close(ref);
5748 if (repo) {
5749 const struct got_error *close_err = got_repo_close(repo);
5750 if (error == NULL)
5751 error = close_err;
5753 tog_free_refs();
5754 return error;
5757 static const struct got_error *
5758 ref_view_load_refs(struct tog_ref_view_state *s)
5760 struct got_reflist_entry *sre;
5761 struct tog_reflist_entry *re;
5763 s->nrefs = 0;
5764 TAILQ_FOREACH(sre, &tog_refs, entry) {
5765 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5766 continue;
5768 re = malloc(sizeof(*re));
5769 if (re == NULL)
5770 return got_error_from_errno("malloc");
5772 re->ref = got_ref_dup(sre->ref);
5773 if (re->ref == NULL)
5774 return got_error_from_errno("got_ref_dup");
5775 re->idx = s->nrefs++;
5776 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5779 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5780 return NULL;
5783 void
5784 ref_view_free_refs(struct tog_ref_view_state *s)
5786 struct tog_reflist_entry *re;
5788 while (!TAILQ_EMPTY(&s->refs)) {
5789 re = TAILQ_FIRST(&s->refs);
5790 TAILQ_REMOVE(&s->refs, re, entry);
5791 got_ref_close(re->ref);
5792 free(re);
5796 static const struct got_error *
5797 open_ref_view(struct tog_view *view, struct got_repository *repo)
5799 const struct got_error *err = NULL;
5800 struct tog_ref_view_state *s = &view->state.ref;
5802 s->selected_entry = 0;
5803 s->repo = repo;
5805 TAILQ_INIT(&s->refs);
5806 STAILQ_INIT(&s->colors);
5808 err = ref_view_load_refs(s);
5809 if (err)
5810 return err;
5812 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5813 err = add_color(&s->colors, "^refs/heads/",
5814 TOG_COLOR_REFS_HEADS,
5815 get_color_value("TOG_COLOR_REFS_HEADS"));
5816 if (err)
5817 goto done;
5819 err = add_color(&s->colors, "^refs/tags/",
5820 TOG_COLOR_REFS_TAGS,
5821 get_color_value("TOG_COLOR_REFS_TAGS"));
5822 if (err)
5823 goto done;
5825 err = add_color(&s->colors, "^refs/remotes/",
5826 TOG_COLOR_REFS_REMOTES,
5827 get_color_value("TOG_COLOR_REFS_REMOTES"));
5828 if (err)
5829 goto done;
5832 view->show = show_ref_view;
5833 view->input = input_ref_view;
5834 view->close = close_ref_view;
5835 view->search_start = search_start_ref_view;
5836 view->search_next = search_next_ref_view;
5837 done:
5838 if (err)
5839 free_colors(&s->colors);
5840 return err;
5843 static const struct got_error *
5844 close_ref_view(struct tog_view *view)
5846 struct tog_ref_view_state *s = &view->state.ref;
5848 ref_view_free_refs(s);
5849 free_colors(&s->colors);
5851 return NULL;
5854 static const struct got_error *
5855 resolve_reflist_entry(struct got_object_id **commit_id,
5856 struct tog_reflist_entry *re, struct got_repository *repo)
5858 const struct got_error *err = NULL;
5859 struct got_object_id *obj_id;
5860 struct got_tag_object *tag = NULL;
5861 int obj_type;
5863 *commit_id = NULL;
5865 err = got_ref_resolve(&obj_id, repo, re->ref);
5866 if (err)
5867 return err;
5869 err = got_object_get_type(&obj_type, repo, obj_id);
5870 if (err)
5871 goto done;
5873 switch (obj_type) {
5874 case GOT_OBJ_TYPE_COMMIT:
5875 *commit_id = obj_id;
5876 break;
5877 case GOT_OBJ_TYPE_TAG:
5878 err = got_object_open_as_tag(&tag, repo, obj_id);
5879 if (err)
5880 goto done;
5881 free(obj_id);
5882 err = got_object_get_type(&obj_type, repo,
5883 got_object_tag_get_object_id(tag));
5884 if (err)
5885 goto done;
5886 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5887 err = got_error(GOT_ERR_OBJ_TYPE);
5888 goto done;
5890 *commit_id = got_object_id_dup(
5891 got_object_tag_get_object_id(tag));
5892 if (*commit_id == NULL) {
5893 err = got_error_from_errno("got_object_id_dup");
5894 goto done;
5896 break;
5897 default:
5898 err = got_error(GOT_ERR_OBJ_TYPE);
5899 break;
5902 done:
5903 if (tag)
5904 got_object_tag_close(tag);
5905 if (err) {
5906 free(*commit_id);
5907 *commit_id = NULL;
5909 return err;
5912 static const struct got_error *
5913 log_ref_entry(struct tog_view **new_view, int begin_x,
5914 struct tog_reflist_entry *re, struct got_repository *repo)
5916 struct tog_view *log_view;
5917 const struct got_error *err = NULL;
5918 struct got_object_id *commit_id = NULL;
5920 *new_view = NULL;
5922 err = resolve_reflist_entry(&commit_id, re, repo);
5923 if (err) {
5924 if (err->code != GOT_ERR_OBJ_TYPE)
5925 return err;
5926 else
5927 return NULL;
5930 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5931 if (log_view == NULL) {
5932 err = got_error_from_errno("view_open");
5933 goto done;
5936 err = open_log_view(log_view, commit_id, repo,
5937 got_ref_get_name(re->ref), "", 0);
5938 done:
5939 if (err)
5940 view_close(log_view);
5941 else
5942 *new_view = log_view;
5943 free(commit_id);
5944 return err;
5947 static void
5948 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5950 struct tog_reflist_entry *re;
5951 int i = 0;
5953 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5954 return;
5956 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5957 while (i++ < maxscroll) {
5958 if (re == NULL)
5959 break;
5960 s->first_displayed_entry = re;
5961 re = TAILQ_PREV(re, tog_reflist_head, entry);
5965 static void
5966 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5968 struct tog_reflist_entry *next, *last;
5969 int n = 0;
5971 if (s->first_displayed_entry)
5972 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5973 else
5974 next = TAILQ_FIRST(&s->refs);
5976 last = s->last_displayed_entry;
5977 while (next && last && n++ < maxscroll) {
5978 last = TAILQ_NEXT(last, entry);
5979 if (last) {
5980 s->first_displayed_entry = next;
5981 next = TAILQ_NEXT(next, entry);
5986 static const struct got_error *
5987 search_start_ref_view(struct tog_view *view)
5989 struct tog_ref_view_state *s = &view->state.ref;
5991 s->matched_entry = NULL;
5992 return NULL;
5995 static int
5996 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5998 regmatch_t regmatch;
6000 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6001 0) == 0;
6004 static const struct got_error *
6005 search_next_ref_view(struct tog_view *view)
6007 struct tog_ref_view_state *s = &view->state.ref;
6008 struct tog_reflist_entry *re = NULL;
6010 if (!view->searching) {
6011 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6012 return NULL;
6015 if (s->matched_entry) {
6016 if (view->searching == TOG_SEARCH_FORWARD) {
6017 if (s->selected_entry)
6018 re = TAILQ_NEXT(s->selected_entry, entry);
6019 else
6020 re = TAILQ_PREV(s->selected_entry,
6021 tog_reflist_head, entry);
6022 } else {
6023 if (s->selected_entry == NULL)
6024 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6025 else
6026 re = TAILQ_PREV(s->selected_entry,
6027 tog_reflist_head, entry);
6029 } else {
6030 if (s->selected_entry)
6031 re = s->selected_entry;
6032 else if (view->searching == TOG_SEARCH_FORWARD)
6033 re = TAILQ_FIRST(&s->refs);
6034 else
6035 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6038 while (1) {
6039 if (re == NULL) {
6040 if (s->matched_entry == NULL) {
6041 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6042 return NULL;
6044 if (view->searching == TOG_SEARCH_FORWARD)
6045 re = TAILQ_FIRST(&s->refs);
6046 else
6047 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6050 if (match_reflist_entry(re, &view->regex)) {
6051 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6052 s->matched_entry = re;
6053 break;
6056 if (view->searching == TOG_SEARCH_FORWARD)
6057 re = TAILQ_NEXT(re, entry);
6058 else
6059 re = TAILQ_PREV(re, tog_reflist_head, entry);
6062 if (s->matched_entry) {
6063 s->first_displayed_entry = s->matched_entry;
6064 s->selected = 0;
6067 return NULL;
6070 static const struct got_error *
6071 show_ref_view(struct tog_view *view)
6073 const struct got_error *err = NULL;
6074 struct tog_ref_view_state *s = &view->state.ref;
6075 struct tog_reflist_entry *re;
6076 char *line = NULL;
6077 wchar_t *wline;
6078 struct tog_color *tc;
6079 int width, n;
6080 int limit = view->nlines;
6082 werase(view->window);
6084 s->ndisplayed = 0;
6086 if (limit == 0)
6087 return NULL;
6089 re = s->first_displayed_entry;
6091 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6092 s->nrefs) == -1)
6093 return got_error_from_errno("asprintf");
6095 err = format_line(&wline, &width, line, view->ncols, 0);
6096 if (err) {
6097 free(line);
6098 return err;
6100 if (view_needs_focus_indication(view))
6101 wstandout(view->window);
6102 waddwstr(view->window, wline);
6103 if (view_needs_focus_indication(view))
6104 wstandend(view->window);
6105 free(wline);
6106 wline = NULL;
6107 free(line);
6108 line = NULL;
6109 if (width < view->ncols - 1)
6110 waddch(view->window, '\n');
6111 if (--limit <= 0)
6112 return NULL;
6114 n = 0;
6115 while (re && limit > 0) {
6116 char *line = NULL;
6118 if (got_ref_is_symbolic(re->ref)) {
6119 if (asprintf(&line, "%s -> %s",
6120 got_ref_get_name(re->ref),
6121 got_ref_get_symref_target(re->ref)) == -1)
6122 return got_error_from_errno("asprintf");
6123 } else if (s->show_ids) {
6124 struct got_object_id *id;
6125 char *id_str;
6126 err = got_ref_resolve(&id, s->repo, re->ref);
6127 if (err)
6128 return err;
6129 err = got_object_id_str(&id_str, id);
6130 if (err) {
6131 free(id);
6132 return err;
6134 if (asprintf(&line, "%s: %s",
6135 got_ref_get_name(re->ref), id_str) == -1) {
6136 err = got_error_from_errno("asprintf");
6137 free(id);
6138 free(id_str);
6139 return err;
6141 free(id);
6142 free(id_str);
6143 } else {
6144 line = strdup(got_ref_get_name(re->ref));
6145 if (line == NULL)
6146 return got_error_from_errno("strdup");
6149 err = format_line(&wline, &width, line, view->ncols, 0);
6150 if (err) {
6151 free(line);
6152 return err;
6154 if (n == s->selected) {
6155 if (view->focussed)
6156 wstandout(view->window);
6157 s->selected_entry = re;
6159 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6160 if (tc)
6161 wattr_on(view->window,
6162 COLOR_PAIR(tc->colorpair), NULL);
6163 waddwstr(view->window, wline);
6164 if (tc)
6165 wattr_off(view->window,
6166 COLOR_PAIR(tc->colorpair), NULL);
6167 if (width < view->ncols - 1)
6168 waddch(view->window, '\n');
6169 if (n == s->selected && view->focussed)
6170 wstandend(view->window);
6171 free(line);
6172 free(wline);
6173 wline = NULL;
6174 n++;
6175 s->ndisplayed++;
6176 s->last_displayed_entry = re;
6178 limit--;
6179 re = TAILQ_NEXT(re, entry);
6182 view_vborder(view);
6183 return err;
6186 static const struct got_error *
6187 browse_ref_tree(struct tog_view **new_view, int begin_x,
6188 struct tog_reflist_entry *re, struct got_repository *repo)
6190 const struct got_error *err = NULL;
6191 struct got_object_id *commit_id = NULL;
6192 struct tog_view *tree_view;
6194 *new_view = NULL;
6196 err = resolve_reflist_entry(&commit_id, re, repo);
6197 if (err) {
6198 if (err->code != GOT_ERR_OBJ_TYPE)
6199 return err;
6200 else
6201 return NULL;
6205 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6206 if (tree_view == NULL) {
6207 err = got_error_from_errno("view_open");
6208 goto done;
6211 err = open_tree_view(tree_view, commit_id,
6212 got_ref_get_name(re->ref), repo);
6213 if (err)
6214 goto done;
6216 *new_view = tree_view;
6217 done:
6218 free(commit_id);
6219 return err;
6221 static const struct got_error *
6222 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6224 const struct got_error *err = NULL;
6225 struct tog_ref_view_state *s = &view->state.ref;
6226 struct tog_view *log_view, *tree_view;
6227 struct tog_reflist_entry *re;
6228 int begin_x = 0, n;
6230 switch (ch) {
6231 case 'i':
6232 s->show_ids = !s->show_ids;
6233 break;
6234 case 'o':
6235 s->sort_by_date = !s->sort_by_date;
6236 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6237 got_ref_cmp_by_commit_timestamp_descending :
6238 got_ref_cmp_by_name, s->repo);
6239 if (err)
6240 break;
6241 got_reflist_object_id_map_free(tog_refs_idmap);
6242 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6243 &tog_refs, s->repo);
6244 if (err)
6245 break;
6246 ref_view_free_refs(s);
6247 err = ref_view_load_refs(s);
6248 break;
6249 case KEY_ENTER:
6250 case '\r':
6251 if (!s->selected_entry)
6252 break;
6253 if (view_is_parent_view(view))
6254 begin_x = view_split_begin_x(view->begin_x);
6255 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6256 s->repo);
6257 view->focussed = 0;
6258 log_view->focussed = 1;
6259 if (view_is_parent_view(view)) {
6260 err = view_close_child(view);
6261 if (err)
6262 return err;
6263 view_set_child(view, log_view);
6264 view->focus_child = 1;
6265 } else
6266 *new_view = log_view;
6267 break;
6268 case 't':
6269 if (!s->selected_entry)
6270 break;
6271 if (view_is_parent_view(view))
6272 begin_x = view_split_begin_x(view->begin_x);
6273 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6274 s->repo);
6275 if (err || tree_view == NULL)
6276 break;
6277 view->focussed = 0;
6278 tree_view->focussed = 1;
6279 if (view_is_parent_view(view)) {
6280 err = view_close_child(view);
6281 if (err)
6282 return err;
6283 view_set_child(view, tree_view);
6284 view->focus_child = 1;
6285 } else
6286 *new_view = tree_view;
6287 break;
6288 case 'g':
6289 case KEY_HOME:
6290 s->selected = 0;
6291 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6292 break;
6293 case 'G':
6294 case KEY_END:
6295 s->selected = 0;
6296 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6297 for (n = 0; n < view->nlines - 1; n++) {
6298 if (re == NULL)
6299 break;
6300 s->first_displayed_entry = re;
6301 re = TAILQ_PREV(re, tog_reflist_head, entry);
6303 if (n > 0)
6304 s->selected = n - 1;
6305 break;
6306 case 'k':
6307 case KEY_UP:
6308 case CTRL('p'):
6309 if (s->selected > 0) {
6310 s->selected--;
6311 break;
6313 ref_scroll_up(s, 1);
6314 break;
6315 case KEY_PPAGE:
6316 case CTRL('b'):
6317 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6318 s->selected = 0;
6319 ref_scroll_up(s, MAX(0, view->nlines - 1));
6320 break;
6321 case 'j':
6322 case KEY_DOWN:
6323 case CTRL('n'):
6324 if (s->selected < s->ndisplayed - 1) {
6325 s->selected++;
6326 break;
6328 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6329 /* can't scroll any further */
6330 break;
6331 ref_scroll_down(s, 1);
6332 break;
6333 case KEY_NPAGE:
6334 case CTRL('f'):
6335 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6336 /* can't scroll any further; move cursor down */
6337 if (s->selected < s->ndisplayed - 1)
6338 s->selected = s->ndisplayed - 1;
6339 break;
6341 ref_scroll_down(s, view->nlines - 1);
6342 break;
6343 case CTRL('l'):
6344 tog_free_refs();
6345 err = tog_load_refs(s->repo, s->sort_by_date);
6346 if (err)
6347 break;
6348 ref_view_free_refs(s);
6349 err = ref_view_load_refs(s);
6350 break;
6351 case KEY_RESIZE:
6352 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6353 s->selected = view->nlines - 2;
6354 break;
6355 default:
6356 break;
6359 return err;
6362 __dead static void
6363 usage_ref(void)
6365 endwin();
6366 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6367 getprogname());
6368 exit(1);
6371 static const struct got_error *
6372 cmd_ref(int argc, char *argv[])
6374 const struct got_error *error;
6375 struct got_repository *repo = NULL;
6376 struct got_worktree *worktree = NULL;
6377 char *cwd = NULL, *repo_path = NULL;
6378 int ch;
6379 struct tog_view *view;
6381 while ((ch = getopt(argc, argv, "r:")) != -1) {
6382 switch (ch) {
6383 case 'r':
6384 repo_path = realpath(optarg, NULL);
6385 if (repo_path == NULL)
6386 return got_error_from_errno2("realpath",
6387 optarg);
6388 break;
6389 default:
6390 usage_ref();
6391 /* NOTREACHED */
6395 argc -= optind;
6396 argv += optind;
6398 if (argc > 1)
6399 usage_ref();
6401 if (repo_path == NULL) {
6402 cwd = getcwd(NULL, 0);
6403 if (cwd == NULL)
6404 return got_error_from_errno("getcwd");
6405 error = got_worktree_open(&worktree, cwd);
6406 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6407 goto done;
6408 if (worktree)
6409 repo_path =
6410 strdup(got_worktree_get_repo_path(worktree));
6411 else
6412 repo_path = strdup(cwd);
6413 if (repo_path == NULL) {
6414 error = got_error_from_errno("strdup");
6415 goto done;
6419 error = got_repo_open(&repo, repo_path, NULL);
6420 if (error != NULL)
6421 goto done;
6423 init_curses();
6425 error = apply_unveil(got_repo_get_path(repo), NULL);
6426 if (error)
6427 goto done;
6429 error = tog_load_refs(repo, 0);
6430 if (error)
6431 goto done;
6433 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6434 if (view == NULL) {
6435 error = got_error_from_errno("view_open");
6436 goto done;
6439 error = open_ref_view(view, repo);
6440 if (error)
6441 goto done;
6443 if (worktree) {
6444 /* Release work tree lock. */
6445 got_worktree_close(worktree);
6446 worktree = NULL;
6448 error = view_loop(view);
6449 done:
6450 free(repo_path);
6451 free(cwd);
6452 if (repo) {
6453 const struct got_error *close_err = got_repo_close(repo);
6454 if (close_err)
6455 error = close_err;
6457 tog_free_refs();
6458 return error;
6461 static void
6462 list_commands(FILE *fp)
6464 size_t i;
6466 fprintf(fp, "commands:");
6467 for (i = 0; i < nitems(tog_commands); i++) {
6468 struct tog_cmd *cmd = &tog_commands[i];
6469 fprintf(fp, " %s", cmd->name);
6471 fputc('\n', fp);
6474 __dead static void
6475 usage(int hflag, int status)
6477 FILE *fp = (status == 0) ? stdout : stderr;
6479 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6480 getprogname());
6481 if (hflag) {
6482 fprintf(fp, "lazy usage: %s path\n", getprogname());
6483 list_commands(fp);
6485 exit(status);
6488 static char **
6489 make_argv(int argc, ...)
6491 va_list ap;
6492 char **argv;
6493 int i;
6495 va_start(ap, argc);
6497 argv = calloc(argc, sizeof(char *));
6498 if (argv == NULL)
6499 err(1, "calloc");
6500 for (i = 0; i < argc; i++) {
6501 argv[i] = strdup(va_arg(ap, char *));
6502 if (argv[i] == NULL)
6503 err(1, "strdup");
6506 va_end(ap);
6507 return argv;
6511 * Try to convert 'tog path' into a 'tog log path' command.
6512 * The user could simply have mistyped the command rather than knowingly
6513 * provided a path. So check whether argv[0] can in fact be resolved
6514 * to a path in the HEAD commit and print a special error if not.
6515 * This hack is for mpi@ <3
6517 static const struct got_error *
6518 tog_log_with_path(int argc, char *argv[])
6520 const struct got_error *error = NULL, *close_err;
6521 struct tog_cmd *cmd = NULL;
6522 struct got_repository *repo = NULL;
6523 struct got_worktree *worktree = NULL;
6524 struct got_object_id *commit_id = NULL, *id = NULL;
6525 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6526 char *commit_id_str = NULL, **cmd_argv = NULL;
6528 cwd = getcwd(NULL, 0);
6529 if (cwd == NULL)
6530 return got_error_from_errno("getcwd");
6532 error = got_worktree_open(&worktree, cwd);
6533 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6534 goto done;
6536 if (worktree)
6537 repo_path = strdup(got_worktree_get_repo_path(worktree));
6538 else
6539 repo_path = strdup(cwd);
6540 if (repo_path == NULL) {
6541 error = got_error_from_errno("strdup");
6542 goto done;
6545 error = got_repo_open(&repo, repo_path, NULL);
6546 if (error != NULL)
6547 goto done;
6549 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6550 repo, worktree);
6551 if (error)
6552 goto done;
6554 error = tog_load_refs(repo, 0);
6555 if (error)
6556 goto done;
6557 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6558 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6559 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6560 if (error)
6561 goto done;
6563 if (worktree) {
6564 got_worktree_close(worktree);
6565 worktree = NULL;
6568 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6569 if (error) {
6570 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6571 goto done;
6572 fprintf(stderr, "%s: '%s' is no known command or path\n",
6573 getprogname(), argv[0]);
6574 usage(1, 1);
6575 /* not reached */
6578 close_err = got_repo_close(repo);
6579 if (error == NULL)
6580 error = close_err;
6581 repo = NULL;
6583 error = got_object_id_str(&commit_id_str, commit_id);
6584 if (error)
6585 goto done;
6587 cmd = &tog_commands[0]; /* log */
6588 argc = 4;
6589 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6590 error = cmd->cmd_main(argc, cmd_argv);
6591 done:
6592 if (repo) {
6593 close_err = got_repo_close(repo);
6594 if (error == NULL)
6595 error = close_err;
6597 if (worktree)
6598 got_worktree_close(worktree);
6599 free(id);
6600 free(commit_id_str);
6601 free(commit_id);
6602 free(cwd);
6603 free(repo_path);
6604 free(in_repo_path);
6605 if (cmd_argv) {
6606 int i;
6607 for (i = 0; i < argc; i++)
6608 free(cmd_argv[i]);
6609 free(cmd_argv);
6611 tog_free_refs();
6612 return error;
6615 int
6616 main(int argc, char *argv[])
6618 const struct got_error *error = NULL;
6619 struct tog_cmd *cmd = NULL;
6620 int ch, hflag = 0, Vflag = 0;
6621 char **cmd_argv = NULL;
6622 static struct option longopts[] = {
6623 { "version", no_argument, NULL, 'V' },
6624 { NULL, 0, NULL, 0}
6627 setlocale(LC_CTYPE, "");
6629 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6630 switch (ch) {
6631 case 'h':
6632 hflag = 1;
6633 break;
6634 case 'V':
6635 Vflag = 1;
6636 break;
6637 default:
6638 usage(hflag, 1);
6639 /* NOTREACHED */
6643 argc -= optind;
6644 argv += optind;
6645 optind = 1;
6646 optreset = 1;
6648 if (Vflag) {
6649 got_version_print_str();
6650 return 0;
6653 #ifndef PROFILE
6654 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6655 NULL) == -1)
6656 err(1, "pledge");
6657 #endif
6659 if (argc == 0) {
6660 if (hflag)
6661 usage(hflag, 0);
6662 /* Build an argument vector which runs a default command. */
6663 cmd = &tog_commands[0];
6664 argc = 1;
6665 cmd_argv = make_argv(argc, cmd->name);
6666 } else {
6667 size_t i;
6669 /* Did the user specify a command? */
6670 for (i = 0; i < nitems(tog_commands); i++) {
6671 if (strncmp(tog_commands[i].name, argv[0],
6672 strlen(argv[0])) == 0) {
6673 cmd = &tog_commands[i];
6674 break;
6679 if (cmd == NULL) {
6680 if (argc != 1)
6681 usage(0, 1);
6682 /* No command specified; try log with a path */
6683 error = tog_log_with_path(argc, argv);
6684 } else {
6685 if (hflag)
6686 cmd->cmd_usage();
6687 else
6688 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6691 endwin();
6692 putchar('\n');
6693 if (cmd_argv) {
6694 int i;
6695 for (i = 0; i < argc; i++)
6696 free(cmd_argv[i]);
6697 free(cmd_argv);
6700 if (error && error->code != GOT_ERR_CANCELLED)
6701 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6702 return 0;