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 if (view->searching == TOG_SEARCH_FORWARD)
3433 lineno = 1;
3434 else
3435 lineno = s->nlines;
3438 while (1) {
3439 off_t offset;
3441 if (lineno <= 0 || lineno > s->nlines) {
3442 if (s->matched_line == 0) {
3443 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3444 break;
3447 if (view->searching == TOG_SEARCH_FORWARD)
3448 lineno = 1;
3449 else
3450 lineno = s->nlines;
3453 offset = s->line_offsets[lineno - 1];
3454 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3455 free(line);
3456 return got_error_from_errno("fseeko");
3458 linelen = getline(&line, &linesize, s->f);
3459 if (linelen != -1 &&
3460 match_line(line, &view->regex, 1, &view->regmatch)) {
3461 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3462 s->matched_line = lineno;
3463 break;
3465 if (view->searching == TOG_SEARCH_FORWARD)
3466 lineno++;
3467 else
3468 lineno--;
3470 free(line);
3472 if (s->matched_line) {
3473 s->first_displayed_line = s->matched_line;
3474 s->selected_line = 1;
3477 return NULL;
3480 static const struct got_error *
3481 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3482 struct got_object_id *id2, const char *label1, const char *label2,
3483 int diff_context, int ignore_whitespace, int force_text_diff,
3484 struct tog_view *log_view, struct got_repository *repo)
3486 const struct got_error *err;
3487 struct tog_diff_view_state *s = &view->state.diff;
3489 if (id1 != NULL && id2 != NULL) {
3490 int type1, type2;
3491 err = got_object_get_type(&type1, repo, id1);
3492 if (err)
3493 return err;
3494 err = got_object_get_type(&type2, repo, id2);
3495 if (err)
3496 return err;
3498 if (type1 != type2)
3499 return got_error(GOT_ERR_OBJ_TYPE);
3501 s->first_displayed_line = 1;
3502 s->last_displayed_line = view->nlines;
3503 s->selected_line = 1;
3504 s->repo = repo;
3505 s->id1 = id1;
3506 s->id2 = id2;
3507 s->label1 = label1;
3508 s->label2 = label2;
3510 if (id1) {
3511 s->id1 = got_object_id_dup(id1);
3512 if (s->id1 == NULL)
3513 return got_error_from_errno("got_object_id_dup");
3514 } else
3515 s->id1 = NULL;
3517 s->id2 = got_object_id_dup(id2);
3518 if (s->id2 == NULL) {
3519 free(s->id1);
3520 s->id1 = NULL;
3521 return got_error_from_errno("got_object_id_dup");
3523 s->f = NULL;
3524 s->first_displayed_line = 1;
3525 s->last_displayed_line = view->nlines;
3526 s->diff_context = diff_context;
3527 s->ignore_whitespace = ignore_whitespace;
3528 s->force_text_diff = force_text_diff;
3529 s->log_view = log_view;
3530 s->repo = repo;
3532 STAILQ_INIT(&s->colors);
3533 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3534 err = add_color(&s->colors,
3535 "^-", TOG_COLOR_DIFF_MINUS,
3536 get_color_value("TOG_COLOR_DIFF_MINUS"));
3537 if (err)
3538 return err;
3539 err = add_color(&s->colors, "^\\+",
3540 TOG_COLOR_DIFF_PLUS,
3541 get_color_value("TOG_COLOR_DIFF_PLUS"));
3542 if (err) {
3543 free_colors(&s->colors);
3544 return err;
3546 err = add_color(&s->colors,
3547 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3548 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3549 if (err) {
3550 free_colors(&s->colors);
3551 return err;
3554 err = add_color(&s->colors,
3555 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3556 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3557 get_color_value("TOG_COLOR_DIFF_META"));
3558 if (err) {
3559 free_colors(&s->colors);
3560 return err;
3563 err = add_color(&s->colors,
3564 "^(from|via): ", TOG_COLOR_AUTHOR,
3565 get_color_value("TOG_COLOR_AUTHOR"));
3566 if (err) {
3567 free_colors(&s->colors);
3568 return err;
3571 err = add_color(&s->colors,
3572 "^date: ", TOG_COLOR_DATE,
3573 get_color_value("TOG_COLOR_DATE"));
3574 if (err) {
3575 free_colors(&s->colors);
3576 return err;
3580 if (log_view && view_is_splitscreen(view))
3581 show_log_view(log_view); /* draw vborder */
3582 diff_view_indicate_progress(view);
3584 s->line_offsets = NULL;
3585 s->nlines = 0;
3586 err = create_diff(s);
3587 if (err) {
3588 free(s->id1);
3589 s->id1 = NULL;
3590 free(s->id2);
3591 s->id2 = NULL;
3592 free_colors(&s->colors);
3593 return err;
3596 view->show = show_diff_view;
3597 view->input = input_diff_view;
3598 view->close = close_diff_view;
3599 view->search_start = search_start_diff_view;
3600 view->search_next = search_next_diff_view;
3602 return NULL;
3605 static const struct got_error *
3606 close_diff_view(struct tog_view *view)
3608 const struct got_error *err = NULL;
3609 struct tog_diff_view_state *s = &view->state.diff;
3611 free(s->id1);
3612 s->id1 = NULL;
3613 free(s->id2);
3614 s->id2 = NULL;
3615 if (s->f && fclose(s->f) == EOF)
3616 err = got_error_from_errno("fclose");
3617 free_colors(&s->colors);
3618 free(s->line_offsets);
3619 s->line_offsets = NULL;
3620 s->nlines = 0;
3621 return err;
3624 static const struct got_error *
3625 show_diff_view(struct tog_view *view)
3627 const struct got_error *err;
3628 struct tog_diff_view_state *s = &view->state.diff;
3629 char *id_str1 = NULL, *id_str2, *header;
3630 const char *label1, *label2;
3632 if (s->id1) {
3633 err = got_object_id_str(&id_str1, s->id1);
3634 if (err)
3635 return err;
3636 label1 = s->label1 ? : id_str1;
3637 } else
3638 label1 = "/dev/null";
3640 err = got_object_id_str(&id_str2, s->id2);
3641 if (err)
3642 return err;
3643 label2 = s->label2 ? : id_str2;
3645 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3646 err = got_error_from_errno("asprintf");
3647 free(id_str1);
3648 free(id_str2);
3649 return err;
3651 free(id_str1);
3652 free(id_str2);
3654 err = draw_file(view, header);
3655 free(header);
3656 return err;
3659 static const struct got_error *
3660 set_selected_commit(struct tog_diff_view_state *s,
3661 struct commit_queue_entry *entry)
3663 const struct got_error *err;
3664 const struct got_object_id_queue *parent_ids;
3665 struct got_commit_object *selected_commit;
3666 struct got_object_qid *pid;
3668 free(s->id2);
3669 s->id2 = got_object_id_dup(entry->id);
3670 if (s->id2 == NULL)
3671 return got_error_from_errno("got_object_id_dup");
3673 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3674 if (err)
3675 return err;
3676 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3677 free(s->id1);
3678 pid = STAILQ_FIRST(parent_ids);
3679 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3680 got_object_commit_close(selected_commit);
3681 return NULL;
3684 static const struct got_error *
3685 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3687 const struct got_error *err = NULL;
3688 struct tog_diff_view_state *s = &view->state.diff;
3689 struct tog_log_view_state *ls;
3690 struct commit_queue_entry *old_selected_entry;
3691 char *line = NULL;
3692 size_t linesize = 0;
3693 ssize_t linelen;
3694 int i;
3696 switch (ch) {
3697 case 'a':
3698 case 'w':
3699 if (ch == 'a')
3700 s->force_text_diff = !s->force_text_diff;
3701 if (ch == 'w')
3702 s->ignore_whitespace = !s->ignore_whitespace;
3703 wclear(view->window);
3704 s->first_displayed_line = 1;
3705 s->last_displayed_line = view->nlines;
3706 diff_view_indicate_progress(view);
3707 err = create_diff(s);
3708 break;
3709 case 'g':
3710 case KEY_HOME:
3711 s->first_displayed_line = 1;
3712 break;
3713 case 'G':
3714 case KEY_END:
3715 if (s->eof)
3716 break;
3718 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3719 s->eof = 1;
3720 break;
3721 case 'k':
3722 case KEY_UP:
3723 case CTRL('p'):
3724 if (s->first_displayed_line > 1)
3725 s->first_displayed_line--;
3726 break;
3727 case KEY_PPAGE:
3728 case CTRL('b'):
3729 if (s->first_displayed_line == 1)
3730 break;
3731 i = 0;
3732 while (i++ < view->nlines - 1 &&
3733 s->first_displayed_line > 1)
3734 s->first_displayed_line--;
3735 break;
3736 case 'j':
3737 case KEY_DOWN:
3738 case CTRL('n'):
3739 if (!s->eof)
3740 s->first_displayed_line++;
3741 break;
3742 case KEY_NPAGE:
3743 case CTRL('f'):
3744 case ' ':
3745 if (s->eof)
3746 break;
3747 i = 0;
3748 while (!s->eof && i++ < view->nlines - 1) {
3749 linelen = getline(&line, &linesize, s->f);
3750 s->first_displayed_line++;
3751 if (linelen == -1) {
3752 if (feof(s->f)) {
3753 s->eof = 1;
3754 } else
3755 err = got_ferror(s->f, GOT_ERR_IO);
3756 break;
3759 free(line);
3760 break;
3761 case '[':
3762 if (s->diff_context > 0) {
3763 s->diff_context--;
3764 diff_view_indicate_progress(view);
3765 err = create_diff(s);
3766 if (s->first_displayed_line + view->nlines - 1 >
3767 s->nlines) {
3768 s->first_displayed_line = 1;
3769 s->last_displayed_line = view->nlines;
3772 break;
3773 case ']':
3774 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3775 s->diff_context++;
3776 diff_view_indicate_progress(view);
3777 err = create_diff(s);
3779 break;
3780 case '<':
3781 case ',':
3782 if (s->log_view == NULL)
3783 break;
3784 ls = &s->log_view->state.log;
3785 old_selected_entry = ls->selected_entry;
3787 err = input_log_view(NULL, s->log_view, KEY_UP);
3788 if (err)
3789 break;
3791 if (old_selected_entry == ls->selected_entry)
3792 break;
3794 err = set_selected_commit(s, ls->selected_entry);
3795 if (err)
3796 break;
3798 s->first_displayed_line = 1;
3799 s->last_displayed_line = view->nlines;
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;
3825 diff_view_indicate_progress(view);
3826 err = create_diff(s);
3827 break;
3828 default:
3829 break;
3832 return err;
3835 static const struct got_error *
3836 cmd_diff(int argc, char *argv[])
3838 const struct got_error *error = NULL;
3839 struct got_repository *repo = NULL;
3840 struct got_worktree *worktree = NULL;
3841 struct got_object_id *id1 = NULL, *id2 = NULL;
3842 char *repo_path = NULL, *cwd = NULL;
3843 char *id_str1 = NULL, *id_str2 = NULL;
3844 char *label1 = NULL, *label2 = NULL;
3845 int diff_context = 3, ignore_whitespace = 0;
3846 int ch, force_text_diff = 0;
3847 const char *errstr;
3848 struct tog_view *view;
3850 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3851 switch (ch) {
3852 case 'a':
3853 force_text_diff = 1;
3854 break;
3855 case 'C':
3856 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3857 &errstr);
3858 if (errstr != NULL)
3859 err(1, "-C option %s", errstr);
3860 break;
3861 case 'r':
3862 repo_path = realpath(optarg, NULL);
3863 if (repo_path == NULL)
3864 return got_error_from_errno2("realpath",
3865 optarg);
3866 got_path_strip_trailing_slashes(repo_path);
3867 break;
3868 case 'w':
3869 ignore_whitespace = 1;
3870 break;
3871 default:
3872 usage_diff();
3873 /* NOTREACHED */
3877 argc -= optind;
3878 argv += optind;
3880 if (argc == 0) {
3881 usage_diff(); /* TODO show local worktree changes */
3882 } else if (argc == 2) {
3883 id_str1 = argv[0];
3884 id_str2 = argv[1];
3885 } else
3886 usage_diff();
3888 if (repo_path == NULL) {
3889 cwd = getcwd(NULL, 0);
3890 if (cwd == NULL)
3891 return got_error_from_errno("getcwd");
3892 error = got_worktree_open(&worktree, cwd);
3893 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3894 goto done;
3895 if (worktree)
3896 repo_path =
3897 strdup(got_worktree_get_repo_path(worktree));
3898 else
3899 repo_path = strdup(cwd);
3900 if (repo_path == NULL) {
3901 error = got_error_from_errno("strdup");
3902 goto done;
3906 error = got_repo_open(&repo, repo_path, NULL);
3907 if (error)
3908 goto done;
3910 init_curses();
3912 error = apply_unveil(got_repo_get_path(repo), NULL);
3913 if (error)
3914 goto done;
3916 error = tog_load_refs(repo, 0);
3917 if (error)
3918 goto done;
3920 error = got_repo_match_object_id(&id1, &label1, id_str1,
3921 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3922 if (error)
3923 goto done;
3925 error = got_repo_match_object_id(&id2, &label2, id_str2,
3926 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3927 if (error)
3928 goto done;
3930 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3931 if (view == NULL) {
3932 error = got_error_from_errno("view_open");
3933 goto done;
3935 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3936 ignore_whitespace, force_text_diff, NULL, repo);
3937 if (error)
3938 goto done;
3939 error = view_loop(view);
3940 done:
3941 free(label1);
3942 free(label2);
3943 free(repo_path);
3944 free(cwd);
3945 if (repo) {
3946 const struct got_error *close_err = got_repo_close(repo);
3947 if (error == NULL)
3948 error = close_err;
3950 if (worktree)
3951 got_worktree_close(worktree);
3952 tog_free_refs();
3953 return error;
3956 __dead static void
3957 usage_blame(void)
3959 endwin();
3960 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3961 getprogname());
3962 exit(1);
3965 struct tog_blame_line {
3966 int annotated;
3967 struct got_object_id *id;
3970 static const struct got_error *
3971 draw_blame(struct tog_view *view)
3973 struct tog_blame_view_state *s = &view->state.blame;
3974 struct tog_blame *blame = &s->blame;
3975 regmatch_t *regmatch = &view->regmatch;
3976 const struct got_error *err;
3977 int lineno = 0, nprinted = 0;
3978 char *line = NULL;
3979 size_t linesize = 0;
3980 ssize_t linelen;
3981 wchar_t *wline;
3982 int width;
3983 struct tog_blame_line *blame_line;
3984 struct got_object_id *prev_id = NULL;
3985 char *id_str;
3986 struct tog_color *tc;
3988 err = got_object_id_str(&id_str, s->blamed_commit->id);
3989 if (err)
3990 return err;
3992 rewind(blame->f);
3993 werase(view->window);
3995 if (asprintf(&line, "commit %s", id_str) == -1) {
3996 err = got_error_from_errno("asprintf");
3997 free(id_str);
3998 return err;
4001 err = format_line(&wline, &width, line, view->ncols, 0);
4002 free(line);
4003 line = NULL;
4004 if (err)
4005 return err;
4006 if (view_needs_focus_indication(view))
4007 wstandout(view->window);
4008 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4009 if (tc)
4010 wattr_on(view->window,
4011 COLOR_PAIR(tc->colorpair), NULL);
4012 waddwstr(view->window, wline);
4013 if (tc)
4014 wattr_off(view->window,
4015 COLOR_PAIR(tc->colorpair), NULL);
4016 if (view_needs_focus_indication(view))
4017 wstandend(view->window);
4018 free(wline);
4019 wline = NULL;
4020 if (width < view->ncols - 1)
4021 waddch(view->window, '\n');
4023 if (asprintf(&line, "[%d/%d] %s%s",
4024 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4025 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4026 free(id_str);
4027 return got_error_from_errno("asprintf");
4029 free(id_str);
4030 err = format_line(&wline, &width, line, view->ncols, 0);
4031 free(line);
4032 line = NULL;
4033 if (err)
4034 return err;
4035 waddwstr(view->window, wline);
4036 free(wline);
4037 wline = NULL;
4038 if (width < view->ncols - 1)
4039 waddch(view->window, '\n');
4041 s->eof = 0;
4042 while (nprinted < view->nlines - 2) {
4043 linelen = getline(&line, &linesize, blame->f);
4044 if (linelen == -1) {
4045 if (feof(blame->f)) {
4046 s->eof = 1;
4047 break;
4049 free(line);
4050 return got_ferror(blame->f, GOT_ERR_IO);
4052 if (++lineno < s->first_displayed_line)
4053 continue;
4055 if (view->focussed && nprinted == s->selected_line - 1)
4056 wstandout(view->window);
4058 if (blame->nlines > 0) {
4059 blame_line = &blame->lines[lineno - 1];
4060 if (blame_line->annotated && prev_id &&
4061 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4062 !(view->focussed &&
4063 nprinted == s->selected_line - 1)) {
4064 waddstr(view->window, " ");
4065 } else if (blame_line->annotated) {
4066 char *id_str;
4067 err = got_object_id_str(&id_str, blame_line->id);
4068 if (err) {
4069 free(line);
4070 return err;
4072 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4073 if (tc)
4074 wattr_on(view->window,
4075 COLOR_PAIR(tc->colorpair), NULL);
4076 wprintw(view->window, "%.8s", id_str);
4077 if (tc)
4078 wattr_off(view->window,
4079 COLOR_PAIR(tc->colorpair), NULL);
4080 free(id_str);
4081 prev_id = blame_line->id;
4082 } else {
4083 waddstr(view->window, "........");
4084 prev_id = NULL;
4086 } else {
4087 waddstr(view->window, "........");
4088 prev_id = NULL;
4091 if (view->focussed && nprinted == s->selected_line - 1)
4092 wstandend(view->window);
4093 waddstr(view->window, " ");
4095 if (view->ncols <= 9) {
4096 width = 9;
4097 wline = wcsdup(L"");
4098 if (wline == NULL) {
4099 err = got_error_from_errno("wcsdup");
4100 free(line);
4101 return err;
4103 } else if (s->first_displayed_line + nprinted ==
4104 s->matched_line &&
4105 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4106 err = add_matched_line(&width, line, view->ncols - 9, 9,
4107 view->window, regmatch);
4108 if (err) {
4109 free(line);
4110 return err;
4112 width += 9;
4113 } else {
4114 err = format_line(&wline, &width, line,
4115 view->ncols - 9, 9);
4116 waddwstr(view->window, wline);
4117 free(wline);
4118 wline = NULL;
4119 width += 9;
4122 if (width <= view->ncols - 1)
4123 waddch(view->window, '\n');
4124 if (++nprinted == 1)
4125 s->first_displayed_line = lineno;
4127 free(line);
4128 s->last_displayed_line = lineno;
4130 view_vborder(view);
4132 return NULL;
4135 static const struct got_error *
4136 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4138 const struct got_error *err = NULL;
4139 struct tog_blame_cb_args *a = arg;
4140 struct tog_blame_line *line;
4141 int errcode;
4143 if (nlines != a->nlines ||
4144 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4145 return got_error(GOT_ERR_RANGE);
4147 errcode = pthread_mutex_lock(&tog_mutex);
4148 if (errcode)
4149 return got_error_set_errno(errcode, "pthread_mutex_lock");
4151 if (*a->quit) { /* user has quit the blame view */
4152 err = got_error(GOT_ERR_ITER_COMPLETED);
4153 goto done;
4156 if (lineno == -1)
4157 goto done; /* no change in this commit */
4159 line = &a->lines[lineno - 1];
4160 if (line->annotated)
4161 goto done;
4163 line->id = got_object_id_dup(id);
4164 if (line->id == NULL) {
4165 err = got_error_from_errno("got_object_id_dup");
4166 goto done;
4168 line->annotated = 1;
4169 done:
4170 errcode = pthread_mutex_unlock(&tog_mutex);
4171 if (errcode)
4172 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4173 return err;
4176 static void *
4177 blame_thread(void *arg)
4179 const struct got_error *err, *close_err;
4180 struct tog_blame_thread_args *ta = arg;
4181 struct tog_blame_cb_args *a = ta->cb_args;
4182 int errcode;
4184 err = block_signals_used_by_main_thread();
4185 if (err)
4186 return (void *)err;
4188 err = got_blame(ta->path, a->commit_id, ta->repo,
4189 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4190 if (err && err->code == GOT_ERR_CANCELLED)
4191 err = NULL;
4193 errcode = pthread_mutex_lock(&tog_mutex);
4194 if (errcode)
4195 return (void *)got_error_set_errno(errcode,
4196 "pthread_mutex_lock");
4198 close_err = got_repo_close(ta->repo);
4199 if (err == NULL)
4200 err = close_err;
4201 ta->repo = NULL;
4202 *ta->complete = 1;
4204 errcode = pthread_mutex_unlock(&tog_mutex);
4205 if (errcode && err == NULL)
4206 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4208 return (void *)err;
4211 static struct got_object_id *
4212 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4213 int first_displayed_line, int selected_line)
4215 struct tog_blame_line *line;
4217 if (nlines <= 0)
4218 return NULL;
4220 line = &lines[first_displayed_line - 1 + selected_line - 1];
4221 if (!line->annotated)
4222 return NULL;
4224 return line->id;
4227 static const struct got_error *
4228 stop_blame(struct tog_blame *blame)
4230 const struct got_error *err = NULL;
4231 int i;
4233 if (blame->thread) {
4234 int errcode;
4235 errcode = pthread_mutex_unlock(&tog_mutex);
4236 if (errcode)
4237 return got_error_set_errno(errcode,
4238 "pthread_mutex_unlock");
4239 errcode = pthread_join(blame->thread, (void **)&err);
4240 if (errcode)
4241 return got_error_set_errno(errcode, "pthread_join");
4242 errcode = pthread_mutex_lock(&tog_mutex);
4243 if (errcode)
4244 return got_error_set_errno(errcode,
4245 "pthread_mutex_lock");
4246 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4247 err = NULL;
4248 blame->thread = NULL;
4250 if (blame->thread_args.repo) {
4251 const struct got_error *close_err;
4252 close_err = got_repo_close(blame->thread_args.repo);
4253 if (err == NULL)
4254 err = close_err;
4255 blame->thread_args.repo = NULL;
4257 if (blame->f) {
4258 if (fclose(blame->f) == EOF && err == NULL)
4259 err = got_error_from_errno("fclose");
4260 blame->f = NULL;
4262 if (blame->lines) {
4263 for (i = 0; i < blame->nlines; i++)
4264 free(blame->lines[i].id);
4265 free(blame->lines);
4266 blame->lines = NULL;
4268 free(blame->cb_args.commit_id);
4269 blame->cb_args.commit_id = NULL;
4271 return err;
4274 static const struct got_error *
4275 cancel_blame_view(void *arg)
4277 const struct got_error *err = NULL;
4278 int *done = arg;
4279 int errcode;
4281 errcode = pthread_mutex_lock(&tog_mutex);
4282 if (errcode)
4283 return got_error_set_errno(errcode,
4284 "pthread_mutex_unlock");
4286 if (*done)
4287 err = got_error(GOT_ERR_CANCELLED);
4289 errcode = pthread_mutex_unlock(&tog_mutex);
4290 if (errcode)
4291 return got_error_set_errno(errcode,
4292 "pthread_mutex_lock");
4294 return err;
4297 static const struct got_error *
4298 run_blame(struct tog_view *view)
4300 struct tog_blame_view_state *s = &view->state.blame;
4301 struct tog_blame *blame = &s->blame;
4302 const struct got_error *err = NULL;
4303 struct got_blob_object *blob = NULL;
4304 struct got_repository *thread_repo = NULL;
4305 struct got_object_id *obj_id = NULL;
4306 int obj_type;
4308 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4309 s->path);
4310 if (err)
4311 return err;
4313 err = got_object_get_type(&obj_type, s->repo, obj_id);
4314 if (err)
4315 goto done;
4317 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4318 err = got_error(GOT_ERR_OBJ_TYPE);
4319 goto done;
4322 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4323 if (err)
4324 goto done;
4325 blame->f = got_opentemp();
4326 if (blame->f == NULL) {
4327 err = got_error_from_errno("got_opentemp");
4328 goto done;
4330 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4331 &blame->line_offsets, blame->f, blob);
4332 if (err)
4333 goto done;
4334 if (blame->nlines == 0) {
4335 s->blame_complete = 1;
4336 goto done;
4339 /* Don't include \n at EOF in the blame line count. */
4340 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4341 blame->nlines--;
4343 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4344 if (blame->lines == NULL) {
4345 err = got_error_from_errno("calloc");
4346 goto done;
4349 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4350 if (err)
4351 goto done;
4353 blame->cb_args.view = view;
4354 blame->cb_args.lines = blame->lines;
4355 blame->cb_args.nlines = blame->nlines;
4356 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4357 if (blame->cb_args.commit_id == NULL) {
4358 err = got_error_from_errno("got_object_id_dup");
4359 goto done;
4361 blame->cb_args.quit = &s->done;
4363 blame->thread_args.path = s->path;
4364 blame->thread_args.repo = thread_repo;
4365 blame->thread_args.cb_args = &blame->cb_args;
4366 blame->thread_args.complete = &s->blame_complete;
4367 blame->thread_args.cancel_cb = cancel_blame_view;
4368 blame->thread_args.cancel_arg = &s->done;
4369 s->blame_complete = 0;
4371 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4372 s->first_displayed_line = 1;
4373 s->last_displayed_line = view->nlines;
4374 s->selected_line = 1;
4377 done:
4378 if (blob)
4379 got_object_blob_close(blob);
4380 free(obj_id);
4381 if (err)
4382 stop_blame(blame);
4383 return err;
4386 static const struct got_error *
4387 open_blame_view(struct tog_view *view, char *path,
4388 struct got_object_id *commit_id, struct got_repository *repo)
4390 const struct got_error *err = NULL;
4391 struct tog_blame_view_state *s = &view->state.blame;
4393 STAILQ_INIT(&s->blamed_commits);
4395 s->path = strdup(path);
4396 if (s->path == NULL)
4397 return got_error_from_errno("strdup");
4399 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4400 if (err) {
4401 free(s->path);
4402 return err;
4405 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4406 s->first_displayed_line = 1;
4407 s->last_displayed_line = view->nlines;
4408 s->selected_line = 1;
4409 s->blame_complete = 0;
4410 s->repo = repo;
4411 s->commit_id = commit_id;
4412 memset(&s->blame, 0, sizeof(s->blame));
4414 STAILQ_INIT(&s->colors);
4415 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4416 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4417 get_color_value("TOG_COLOR_COMMIT"));
4418 if (err)
4419 return err;
4422 view->show = show_blame_view;
4423 view->input = input_blame_view;
4424 view->close = close_blame_view;
4425 view->search_start = search_start_blame_view;
4426 view->search_next = search_next_blame_view;
4428 return run_blame(view);
4431 static const struct got_error *
4432 close_blame_view(struct tog_view *view)
4434 const struct got_error *err = NULL;
4435 struct tog_blame_view_state *s = &view->state.blame;
4437 if (s->blame.thread)
4438 err = stop_blame(&s->blame);
4440 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4441 struct got_object_qid *blamed_commit;
4442 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4443 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4444 got_object_qid_free(blamed_commit);
4447 free(s->path);
4448 free_colors(&s->colors);
4450 return err;
4453 static const struct got_error *
4454 search_start_blame_view(struct tog_view *view)
4456 struct tog_blame_view_state *s = &view->state.blame;
4458 s->matched_line = 0;
4459 return NULL;
4462 static const struct got_error *
4463 search_next_blame_view(struct tog_view *view)
4465 struct tog_blame_view_state *s = &view->state.blame;
4466 int lineno;
4467 char *line = NULL;
4468 size_t linesize = 0;
4469 ssize_t linelen;
4471 if (!view->searching) {
4472 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4473 return NULL;
4476 if (s->matched_line) {
4477 if (view->searching == TOG_SEARCH_FORWARD)
4478 lineno = s->matched_line + 1;
4479 else
4480 lineno = s->matched_line - 1;
4481 } else {
4482 if (view->searching == TOG_SEARCH_FORWARD)
4483 lineno = 1;
4484 else
4485 lineno = s->blame.nlines;
4488 while (1) {
4489 off_t offset;
4491 if (lineno <= 0 || lineno > s->blame.nlines) {
4492 if (s->matched_line == 0) {
4493 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4494 break;
4497 if (view->searching == TOG_SEARCH_FORWARD)
4498 lineno = 1;
4499 else
4500 lineno = s->blame.nlines;
4503 offset = s->blame.line_offsets[lineno - 1];
4504 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4505 free(line);
4506 return got_error_from_errno("fseeko");
4508 linelen = getline(&line, &linesize, s->blame.f);
4509 if (linelen != -1 &&
4510 match_line(line, &view->regex, 1, &view->regmatch)) {
4511 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4512 s->matched_line = lineno;
4513 break;
4515 if (view->searching == TOG_SEARCH_FORWARD)
4516 lineno++;
4517 else
4518 lineno--;
4520 free(line);
4522 if (s->matched_line) {
4523 s->first_displayed_line = s->matched_line;
4524 s->selected_line = 1;
4527 return NULL;
4530 static const struct got_error *
4531 show_blame_view(struct tog_view *view)
4533 const struct got_error *err = NULL;
4534 struct tog_blame_view_state *s = &view->state.blame;
4535 int errcode;
4537 if (s->blame.thread == NULL && !s->blame_complete) {
4538 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4539 &s->blame.thread_args);
4540 if (errcode)
4541 return got_error_set_errno(errcode, "pthread_create");
4543 halfdelay(1); /* fast refresh while annotating */
4546 if (s->blame_complete)
4547 halfdelay(10); /* disable fast refresh */
4549 err = draw_blame(view);
4551 view_vborder(view);
4552 return err;
4555 static const struct got_error *
4556 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4558 const struct got_error *err = NULL, *thread_err = NULL;
4559 struct tog_view *diff_view;
4560 struct tog_blame_view_state *s = &view->state.blame;
4561 int begin_x = 0;
4563 switch (ch) {
4564 case 'q':
4565 s->done = 1;
4566 break;
4567 case 'g':
4568 case KEY_HOME:
4569 s->selected_line = 1;
4570 s->first_displayed_line = 1;
4571 break;
4572 case 'G':
4573 case KEY_END:
4574 if (s->blame.nlines < view->nlines - 2) {
4575 s->selected_line = s->blame.nlines;
4576 s->first_displayed_line = 1;
4577 } else {
4578 s->selected_line = view->nlines - 2;
4579 s->first_displayed_line = s->blame.nlines -
4580 (view->nlines - 3);
4582 break;
4583 case 'k':
4584 case KEY_UP:
4585 case CTRL('p'):
4586 if (s->selected_line > 1)
4587 s->selected_line--;
4588 else if (s->selected_line == 1 &&
4589 s->first_displayed_line > 1)
4590 s->first_displayed_line--;
4591 break;
4592 case KEY_PPAGE:
4593 case CTRL('b'):
4594 if (s->first_displayed_line == 1) {
4595 s->selected_line = 1;
4596 break;
4598 if (s->first_displayed_line > view->nlines - 2)
4599 s->first_displayed_line -=
4600 (view->nlines - 2);
4601 else
4602 s->first_displayed_line = 1;
4603 break;
4604 case 'j':
4605 case KEY_DOWN:
4606 case CTRL('n'):
4607 if (s->selected_line < view->nlines - 2 &&
4608 s->first_displayed_line +
4609 s->selected_line <= s->blame.nlines)
4610 s->selected_line++;
4611 else if (s->last_displayed_line <
4612 s->blame.nlines)
4613 s->first_displayed_line++;
4614 break;
4615 case 'b':
4616 case 'p': {
4617 struct got_object_id *id = NULL;
4618 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4619 s->first_displayed_line, s->selected_line);
4620 if (id == NULL)
4621 break;
4622 if (ch == 'p') {
4623 struct got_commit_object *commit;
4624 struct got_object_qid *pid;
4625 struct got_object_id *blob_id = NULL;
4626 int obj_type;
4627 err = got_object_open_as_commit(&commit,
4628 s->repo, id);
4629 if (err)
4630 break;
4631 pid = STAILQ_FIRST(
4632 got_object_commit_get_parent_ids(commit));
4633 if (pid == NULL) {
4634 got_object_commit_close(commit);
4635 break;
4637 /* Check if path history ends here. */
4638 err = got_object_id_by_path(&blob_id, s->repo,
4639 pid->id, s->path);
4640 if (err) {
4641 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4642 err = NULL;
4643 got_object_commit_close(commit);
4644 break;
4646 err = got_object_get_type(&obj_type, s->repo,
4647 blob_id);
4648 free(blob_id);
4649 /* Can't blame non-blob type objects. */
4650 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4651 got_object_commit_close(commit);
4652 break;
4654 err = got_object_qid_alloc(&s->blamed_commit,
4655 pid->id);
4656 got_object_commit_close(commit);
4657 } else {
4658 if (got_object_id_cmp(id,
4659 s->blamed_commit->id) == 0)
4660 break;
4661 err = got_object_qid_alloc(&s->blamed_commit,
4662 id);
4664 if (err)
4665 break;
4666 s->done = 1;
4667 thread_err = stop_blame(&s->blame);
4668 s->done = 0;
4669 if (thread_err)
4670 break;
4671 STAILQ_INSERT_HEAD(&s->blamed_commits,
4672 s->blamed_commit, entry);
4673 err = run_blame(view);
4674 if (err)
4675 break;
4676 break;
4678 case 'B': {
4679 struct got_object_qid *first;
4680 first = STAILQ_FIRST(&s->blamed_commits);
4681 if (!got_object_id_cmp(first->id, s->commit_id))
4682 break;
4683 s->done = 1;
4684 thread_err = stop_blame(&s->blame);
4685 s->done = 0;
4686 if (thread_err)
4687 break;
4688 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4689 got_object_qid_free(s->blamed_commit);
4690 s->blamed_commit =
4691 STAILQ_FIRST(&s->blamed_commits);
4692 err = run_blame(view);
4693 if (err)
4694 break;
4695 break;
4697 case KEY_ENTER:
4698 case '\r': {
4699 struct got_object_id *id = NULL;
4700 struct got_object_qid *pid;
4701 struct got_commit_object *commit = NULL;
4702 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4703 s->first_displayed_line, s->selected_line);
4704 if (id == NULL)
4705 break;
4706 err = got_object_open_as_commit(&commit, s->repo, id);
4707 if (err)
4708 break;
4709 pid = STAILQ_FIRST(
4710 got_object_commit_get_parent_ids(commit));
4711 if (view_is_parent_view(view))
4712 begin_x = view_split_begin_x(view->begin_x);
4713 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4714 if (diff_view == NULL) {
4715 got_object_commit_close(commit);
4716 err = got_error_from_errno("view_open");
4717 break;
4719 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4720 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4721 got_object_commit_close(commit);
4722 if (err) {
4723 view_close(diff_view);
4724 break;
4726 view->focussed = 0;
4727 diff_view->focussed = 1;
4728 if (view_is_parent_view(view)) {
4729 err = view_close_child(view);
4730 if (err)
4731 break;
4732 view_set_child(view, diff_view);
4733 view->focus_child = 1;
4734 } else
4735 *new_view = diff_view;
4736 if (err)
4737 break;
4738 break;
4740 case KEY_NPAGE:
4741 case CTRL('f'):
4742 case ' ':
4743 if (s->last_displayed_line >= s->blame.nlines &&
4744 s->selected_line >= MIN(s->blame.nlines,
4745 view->nlines - 2)) {
4746 break;
4748 if (s->last_displayed_line >= s->blame.nlines &&
4749 s->selected_line < view->nlines - 2) {
4750 s->selected_line = MIN(s->blame.nlines,
4751 view->nlines - 2);
4752 break;
4754 if (s->last_displayed_line + view->nlines - 2
4755 <= s->blame.nlines)
4756 s->first_displayed_line +=
4757 view->nlines - 2;
4758 else
4759 s->first_displayed_line =
4760 s->blame.nlines -
4761 (view->nlines - 3);
4762 break;
4763 case KEY_RESIZE:
4764 if (s->selected_line > view->nlines - 2) {
4765 s->selected_line = MIN(s->blame.nlines,
4766 view->nlines - 2);
4768 break;
4769 default:
4770 break;
4772 return thread_err ? thread_err : err;
4775 static const struct got_error *
4776 cmd_blame(int argc, char *argv[])
4778 const struct got_error *error;
4779 struct got_repository *repo = NULL;
4780 struct got_worktree *worktree = NULL;
4781 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4782 char *link_target = NULL;
4783 struct got_object_id *commit_id = NULL;
4784 char *commit_id_str = NULL;
4785 int ch;
4786 struct tog_view *view;
4788 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4789 switch (ch) {
4790 case 'c':
4791 commit_id_str = optarg;
4792 break;
4793 case 'r':
4794 repo_path = realpath(optarg, NULL);
4795 if (repo_path == NULL)
4796 return got_error_from_errno2("realpath",
4797 optarg);
4798 break;
4799 default:
4800 usage_blame();
4801 /* NOTREACHED */
4805 argc -= optind;
4806 argv += optind;
4808 if (argc != 1)
4809 usage_blame();
4811 if (repo_path == NULL) {
4812 cwd = getcwd(NULL, 0);
4813 if (cwd == NULL)
4814 return got_error_from_errno("getcwd");
4815 error = got_worktree_open(&worktree, cwd);
4816 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4817 goto done;
4818 if (worktree)
4819 repo_path =
4820 strdup(got_worktree_get_repo_path(worktree));
4821 else
4822 repo_path = strdup(cwd);
4823 if (repo_path == NULL) {
4824 error = got_error_from_errno("strdup");
4825 goto done;
4829 error = got_repo_open(&repo, repo_path, NULL);
4830 if (error != NULL)
4831 goto done;
4833 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4834 worktree);
4835 if (error)
4836 goto done;
4838 init_curses();
4840 error = apply_unveil(got_repo_get_path(repo), NULL);
4841 if (error)
4842 goto done;
4844 error = tog_load_refs(repo, 0);
4845 if (error)
4846 goto done;
4848 if (commit_id_str == NULL) {
4849 struct got_reference *head_ref;
4850 error = got_ref_open(&head_ref, repo, worktree ?
4851 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4852 if (error != NULL)
4853 goto done;
4854 error = got_ref_resolve(&commit_id, repo, head_ref);
4855 got_ref_close(head_ref);
4856 } else {
4857 error = got_repo_match_object_id(&commit_id, NULL,
4858 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4860 if (error != NULL)
4861 goto done;
4863 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4864 if (view == NULL) {
4865 error = got_error_from_errno("view_open");
4866 goto done;
4869 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4870 commit_id, repo);
4871 if (error)
4872 goto done;
4874 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4875 commit_id, repo);
4876 if (error)
4877 goto done;
4878 if (worktree) {
4879 /* Release work tree lock. */
4880 got_worktree_close(worktree);
4881 worktree = NULL;
4883 error = view_loop(view);
4884 done:
4885 free(repo_path);
4886 free(in_repo_path);
4887 free(link_target);
4888 free(cwd);
4889 free(commit_id);
4890 if (worktree)
4891 got_worktree_close(worktree);
4892 if (repo) {
4893 const struct got_error *close_err = got_repo_close(repo);
4894 if (error == NULL)
4895 error = close_err;
4897 tog_free_refs();
4898 return error;
4901 static const struct got_error *
4902 draw_tree_entries(struct tog_view *view, const char *parent_path)
4904 struct tog_tree_view_state *s = &view->state.tree;
4905 const struct got_error *err = NULL;
4906 struct got_tree_entry *te;
4907 wchar_t *wline;
4908 struct tog_color *tc;
4909 int width, n, i, nentries;
4910 int limit = view->nlines;
4912 s->ndisplayed = 0;
4914 werase(view->window);
4916 if (limit == 0)
4917 return NULL;
4919 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4920 if (err)
4921 return err;
4922 if (view_needs_focus_indication(view))
4923 wstandout(view->window);
4924 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4925 if (tc)
4926 wattr_on(view->window,
4927 COLOR_PAIR(tc->colorpair), NULL);
4928 waddwstr(view->window, wline);
4929 if (tc)
4930 wattr_off(view->window,
4931 COLOR_PAIR(tc->colorpair), NULL);
4932 if (view_needs_focus_indication(view))
4933 wstandend(view->window);
4934 free(wline);
4935 wline = NULL;
4936 if (width < view->ncols - 1)
4937 waddch(view->window, '\n');
4938 if (--limit <= 0)
4939 return NULL;
4940 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4941 if (err)
4942 return err;
4943 waddwstr(view->window, wline);
4944 free(wline);
4945 wline = NULL;
4946 if (width < view->ncols - 1)
4947 waddch(view->window, '\n');
4948 if (--limit <= 0)
4949 return NULL;
4950 waddch(view->window, '\n');
4951 if (--limit <= 0)
4952 return NULL;
4954 if (s->first_displayed_entry == NULL) {
4955 te = got_object_tree_get_first_entry(s->tree);
4956 if (s->selected == 0) {
4957 if (view->focussed)
4958 wstandout(view->window);
4959 s->selected_entry = NULL;
4961 waddstr(view->window, " ..\n"); /* parent directory */
4962 if (s->selected == 0 && view->focussed)
4963 wstandend(view->window);
4964 s->ndisplayed++;
4965 if (--limit <= 0)
4966 return NULL;
4967 n = 1;
4968 } else {
4969 n = 0;
4970 te = s->first_displayed_entry;
4973 nentries = got_object_tree_get_nentries(s->tree);
4974 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4975 char *line = NULL, *id_str = NULL, *link_target = NULL;
4976 const char *modestr = "";
4977 mode_t mode;
4979 te = got_object_tree_get_entry(s->tree, i);
4980 mode = got_tree_entry_get_mode(te);
4982 if (s->show_ids) {
4983 err = got_object_id_str(&id_str,
4984 got_tree_entry_get_id(te));
4985 if (err)
4986 return got_error_from_errno(
4987 "got_object_id_str");
4989 if (got_object_tree_entry_is_submodule(te))
4990 modestr = "$";
4991 else if (S_ISLNK(mode)) {
4992 int i;
4994 err = got_tree_entry_get_symlink_target(&link_target,
4995 te, s->repo);
4996 if (err) {
4997 free(id_str);
4998 return err;
5000 for (i = 0; i < strlen(link_target); i++) {
5001 if (!isprint((unsigned char)link_target[i]))
5002 link_target[i] = '?';
5004 modestr = "@";
5006 else if (S_ISDIR(mode))
5007 modestr = "/";
5008 else if (mode & S_IXUSR)
5009 modestr = "*";
5010 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5011 got_tree_entry_get_name(te), modestr,
5012 link_target ? " -> ": "",
5013 link_target ? link_target : "") == -1) {
5014 free(id_str);
5015 free(link_target);
5016 return got_error_from_errno("asprintf");
5018 free(id_str);
5019 free(link_target);
5020 err = format_line(&wline, &width, line, view->ncols, 0);
5021 if (err) {
5022 free(line);
5023 break;
5025 if (n == s->selected) {
5026 if (view->focussed)
5027 wstandout(view->window);
5028 s->selected_entry = te;
5030 tc = match_color(&s->colors, line);
5031 if (tc)
5032 wattr_on(view->window,
5033 COLOR_PAIR(tc->colorpair), NULL);
5034 waddwstr(view->window, wline);
5035 if (tc)
5036 wattr_off(view->window,
5037 COLOR_PAIR(tc->colorpair), NULL);
5038 if (width < view->ncols - 1)
5039 waddch(view->window, '\n');
5040 if (n == s->selected && view->focussed)
5041 wstandend(view->window);
5042 free(line);
5043 free(wline);
5044 wline = NULL;
5045 n++;
5046 s->ndisplayed++;
5047 s->last_displayed_entry = te;
5048 if (--limit <= 0)
5049 break;
5052 return err;
5055 static void
5056 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5058 struct got_tree_entry *te;
5059 int isroot = s->tree == s->root;
5060 int i = 0;
5062 if (s->first_displayed_entry == NULL)
5063 return;
5065 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5066 while (i++ < maxscroll) {
5067 if (te == NULL) {
5068 if (!isroot)
5069 s->first_displayed_entry = NULL;
5070 break;
5072 s->first_displayed_entry = te;
5073 te = got_tree_entry_get_prev(s->tree, te);
5077 static void
5078 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5080 struct got_tree_entry *next, *last;
5081 int n = 0;
5083 if (s->first_displayed_entry)
5084 next = got_tree_entry_get_next(s->tree,
5085 s->first_displayed_entry);
5086 else
5087 next = got_object_tree_get_first_entry(s->tree);
5089 last = s->last_displayed_entry;
5090 while (next && last && n++ < maxscroll) {
5091 last = got_tree_entry_get_next(s->tree, last);
5092 if (last) {
5093 s->first_displayed_entry = next;
5094 next = got_tree_entry_get_next(s->tree, next);
5099 static const struct got_error *
5100 tree_entry_path(char **path, struct tog_parent_trees *parents,
5101 struct got_tree_entry *te)
5103 const struct got_error *err = NULL;
5104 struct tog_parent_tree *pt;
5105 size_t len = 2; /* for leading slash and NUL */
5107 TAILQ_FOREACH(pt, parents, entry)
5108 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5109 + 1 /* slash */;
5110 if (te)
5111 len += strlen(got_tree_entry_get_name(te));
5113 *path = calloc(1, len);
5114 if (path == NULL)
5115 return got_error_from_errno("calloc");
5117 (*path)[0] = '/';
5118 pt = TAILQ_LAST(parents, tog_parent_trees);
5119 while (pt) {
5120 const char *name = got_tree_entry_get_name(pt->selected_entry);
5121 if (strlcat(*path, name, len) >= len) {
5122 err = got_error(GOT_ERR_NO_SPACE);
5123 goto done;
5125 if (strlcat(*path, "/", len) >= len) {
5126 err = got_error(GOT_ERR_NO_SPACE);
5127 goto done;
5129 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5131 if (te) {
5132 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5133 err = got_error(GOT_ERR_NO_SPACE);
5134 goto done;
5137 done:
5138 if (err) {
5139 free(*path);
5140 *path = NULL;
5142 return err;
5145 static const struct got_error *
5146 blame_tree_entry(struct tog_view **new_view, int begin_x,
5147 struct got_tree_entry *te, struct tog_parent_trees *parents,
5148 struct got_object_id *commit_id, struct got_repository *repo)
5150 const struct got_error *err = NULL;
5151 char *path;
5152 struct tog_view *blame_view;
5154 *new_view = NULL;
5156 err = tree_entry_path(&path, parents, te);
5157 if (err)
5158 return err;
5160 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5161 if (blame_view == NULL) {
5162 err = got_error_from_errno("view_open");
5163 goto done;
5166 err = open_blame_view(blame_view, path, commit_id, repo);
5167 if (err) {
5168 if (err->code == GOT_ERR_CANCELLED)
5169 err = NULL;
5170 view_close(blame_view);
5171 } else
5172 *new_view = blame_view;
5173 done:
5174 free(path);
5175 return err;
5178 static const struct got_error *
5179 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5180 struct tog_tree_view_state *s)
5182 struct tog_view *log_view;
5183 const struct got_error *err = NULL;
5184 char *path;
5186 *new_view = NULL;
5188 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5189 if (log_view == NULL)
5190 return got_error_from_errno("view_open");
5192 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5193 if (err)
5194 return err;
5196 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5197 path, 0);
5198 if (err)
5199 view_close(log_view);
5200 else
5201 *new_view = log_view;
5202 free(path);
5203 return err;
5206 static const struct got_error *
5207 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5208 const char *head_ref_name, struct got_repository *repo)
5210 const struct got_error *err = NULL;
5211 char *commit_id_str = NULL;
5212 struct tog_tree_view_state *s = &view->state.tree;
5213 struct got_commit_object *commit = NULL;
5215 TAILQ_INIT(&s->parents);
5216 STAILQ_INIT(&s->colors);
5218 s->commit_id = got_object_id_dup(commit_id);
5219 if (s->commit_id == NULL)
5220 return got_error_from_errno("got_object_id_dup");
5222 err = got_object_open_as_commit(&commit, repo, commit_id);
5223 if (err)
5224 goto done;
5227 * The root is opened here and will be closed when the view is closed.
5228 * Any visited subtrees and their path-wise parents are opened and
5229 * closed on demand.
5231 err = got_object_open_as_tree(&s->root, repo,
5232 got_object_commit_get_tree_id(commit));
5233 if (err)
5234 goto done;
5235 s->tree = s->root;
5237 err = got_object_id_str(&commit_id_str, commit_id);
5238 if (err != NULL)
5239 goto done;
5241 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5242 err = got_error_from_errno("asprintf");
5243 goto done;
5246 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5247 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5248 if (head_ref_name) {
5249 s->head_ref_name = strdup(head_ref_name);
5250 if (s->head_ref_name == NULL) {
5251 err = got_error_from_errno("strdup");
5252 goto done;
5255 s->repo = repo;
5257 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5258 err = add_color(&s->colors, "\\$$",
5259 TOG_COLOR_TREE_SUBMODULE,
5260 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5261 if (err)
5262 goto done;
5263 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5264 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5265 if (err)
5266 goto done;
5267 err = add_color(&s->colors, "/$",
5268 TOG_COLOR_TREE_DIRECTORY,
5269 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5270 if (err)
5271 goto done;
5273 err = add_color(&s->colors, "\\*$",
5274 TOG_COLOR_TREE_EXECUTABLE,
5275 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5276 if (err)
5277 goto done;
5279 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5280 get_color_value("TOG_COLOR_COMMIT"));
5281 if (err)
5282 goto done;
5285 view->show = show_tree_view;
5286 view->input = input_tree_view;
5287 view->close = close_tree_view;
5288 view->search_start = search_start_tree_view;
5289 view->search_next = search_next_tree_view;
5290 done:
5291 free(commit_id_str);
5292 if (commit)
5293 got_object_commit_close(commit);
5294 if (err)
5295 close_tree_view(view);
5296 return err;
5299 static const struct got_error *
5300 close_tree_view(struct tog_view *view)
5302 struct tog_tree_view_state *s = &view->state.tree;
5304 free_colors(&s->colors);
5305 free(s->tree_label);
5306 s->tree_label = NULL;
5307 free(s->commit_id);
5308 s->commit_id = NULL;
5309 free(s->head_ref_name);
5310 s->head_ref_name = NULL;
5311 while (!TAILQ_EMPTY(&s->parents)) {
5312 struct tog_parent_tree *parent;
5313 parent = TAILQ_FIRST(&s->parents);
5314 TAILQ_REMOVE(&s->parents, parent, entry);
5315 if (parent->tree != s->root)
5316 got_object_tree_close(parent->tree);
5317 free(parent);
5320 if (s->tree != NULL && s->tree != s->root)
5321 got_object_tree_close(s->tree);
5322 if (s->root)
5323 got_object_tree_close(s->root);
5324 return NULL;
5327 static const struct got_error *
5328 search_start_tree_view(struct tog_view *view)
5330 struct tog_tree_view_state *s = &view->state.tree;
5332 s->matched_entry = NULL;
5333 return NULL;
5336 static int
5337 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5339 regmatch_t regmatch;
5341 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5342 0) == 0;
5345 static const struct got_error *
5346 search_next_tree_view(struct tog_view *view)
5348 struct tog_tree_view_state *s = &view->state.tree;
5349 struct got_tree_entry *te = NULL;
5351 if (!view->searching) {
5352 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5353 return NULL;
5356 if (s->matched_entry) {
5357 if (view->searching == TOG_SEARCH_FORWARD) {
5358 if (s->selected_entry)
5359 te = got_tree_entry_get_next(s->tree,
5360 s->selected_entry);
5361 else
5362 te = got_object_tree_get_first_entry(s->tree);
5363 } else {
5364 if (s->selected_entry == NULL)
5365 te = got_object_tree_get_last_entry(s->tree);
5366 else
5367 te = got_tree_entry_get_prev(s->tree,
5368 s->selected_entry);
5370 } else {
5371 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 (view->searching == TOG_SEARCH_FORWARD)
6031 re = TAILQ_FIRST(&s->refs);
6032 else
6033 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6036 while (1) {
6037 if (re == NULL) {
6038 if (s->matched_entry == NULL) {
6039 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6040 return NULL;
6042 if (view->searching == TOG_SEARCH_FORWARD)
6043 re = TAILQ_FIRST(&s->refs);
6044 else
6045 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6048 if (match_reflist_entry(re, &view->regex)) {
6049 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6050 s->matched_entry = re;
6051 break;
6054 if (view->searching == TOG_SEARCH_FORWARD)
6055 re = TAILQ_NEXT(re, entry);
6056 else
6057 re = TAILQ_PREV(re, tog_reflist_head, entry);
6060 if (s->matched_entry) {
6061 s->first_displayed_entry = s->matched_entry;
6062 s->selected = 0;
6065 return NULL;
6068 static const struct got_error *
6069 show_ref_view(struct tog_view *view)
6071 const struct got_error *err = NULL;
6072 struct tog_ref_view_state *s = &view->state.ref;
6073 struct tog_reflist_entry *re;
6074 char *line = NULL;
6075 wchar_t *wline;
6076 struct tog_color *tc;
6077 int width, n;
6078 int limit = view->nlines;
6080 werase(view->window);
6082 s->ndisplayed = 0;
6084 if (limit == 0)
6085 return NULL;
6087 re = s->first_displayed_entry;
6089 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6090 s->nrefs) == -1)
6091 return got_error_from_errno("asprintf");
6093 err = format_line(&wline, &width, line, view->ncols, 0);
6094 if (err) {
6095 free(line);
6096 return err;
6098 if (view_needs_focus_indication(view))
6099 wstandout(view->window);
6100 waddwstr(view->window, wline);
6101 if (view_needs_focus_indication(view))
6102 wstandend(view->window);
6103 free(wline);
6104 wline = NULL;
6105 free(line);
6106 line = NULL;
6107 if (width < view->ncols - 1)
6108 waddch(view->window, '\n');
6109 if (--limit <= 0)
6110 return NULL;
6112 n = 0;
6113 while (re && limit > 0) {
6114 char *line = NULL;
6116 if (got_ref_is_symbolic(re->ref)) {
6117 if (asprintf(&line, "%s -> %s",
6118 got_ref_get_name(re->ref),
6119 got_ref_get_symref_target(re->ref)) == -1)
6120 return got_error_from_errno("asprintf");
6121 } else if (s->show_ids) {
6122 struct got_object_id *id;
6123 char *id_str;
6124 err = got_ref_resolve(&id, s->repo, re->ref);
6125 if (err)
6126 return err;
6127 err = got_object_id_str(&id_str, id);
6128 if (err) {
6129 free(id);
6130 return err;
6132 if (asprintf(&line, "%s: %s",
6133 got_ref_get_name(re->ref), id_str) == -1) {
6134 err = got_error_from_errno("asprintf");
6135 free(id);
6136 free(id_str);
6137 return err;
6139 free(id);
6140 free(id_str);
6141 } else {
6142 line = strdup(got_ref_get_name(re->ref));
6143 if (line == NULL)
6144 return got_error_from_errno("strdup");
6147 err = format_line(&wline, &width, line, view->ncols, 0);
6148 if (err) {
6149 free(line);
6150 return err;
6152 if (n == s->selected) {
6153 if (view->focussed)
6154 wstandout(view->window);
6155 s->selected_entry = re;
6157 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6158 if (tc)
6159 wattr_on(view->window,
6160 COLOR_PAIR(tc->colorpair), NULL);
6161 waddwstr(view->window, wline);
6162 if (tc)
6163 wattr_off(view->window,
6164 COLOR_PAIR(tc->colorpair), NULL);
6165 if (width < view->ncols - 1)
6166 waddch(view->window, '\n');
6167 if (n == s->selected && view->focussed)
6168 wstandend(view->window);
6169 free(line);
6170 free(wline);
6171 wline = NULL;
6172 n++;
6173 s->ndisplayed++;
6174 s->last_displayed_entry = re;
6176 limit--;
6177 re = TAILQ_NEXT(re, entry);
6180 view_vborder(view);
6181 return err;
6184 static const struct got_error *
6185 browse_ref_tree(struct tog_view **new_view, int begin_x,
6186 struct tog_reflist_entry *re, struct got_repository *repo)
6188 const struct got_error *err = NULL;
6189 struct got_object_id *commit_id = NULL;
6190 struct tog_view *tree_view;
6192 *new_view = NULL;
6194 err = resolve_reflist_entry(&commit_id, re, repo);
6195 if (err) {
6196 if (err->code != GOT_ERR_OBJ_TYPE)
6197 return err;
6198 else
6199 return NULL;
6203 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6204 if (tree_view == NULL) {
6205 err = got_error_from_errno("view_open");
6206 goto done;
6209 err = open_tree_view(tree_view, commit_id,
6210 got_ref_get_name(re->ref), repo);
6211 if (err)
6212 goto done;
6214 *new_view = tree_view;
6215 done:
6216 free(commit_id);
6217 return err;
6219 static const struct got_error *
6220 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6222 const struct got_error *err = NULL;
6223 struct tog_ref_view_state *s = &view->state.ref;
6224 struct tog_view *log_view, *tree_view;
6225 struct tog_reflist_entry *re;
6226 int begin_x = 0, n;
6228 switch (ch) {
6229 case 'i':
6230 s->show_ids = !s->show_ids;
6231 break;
6232 case 'o':
6233 s->sort_by_date = !s->sort_by_date;
6234 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6235 got_ref_cmp_by_commit_timestamp_descending :
6236 got_ref_cmp_by_name, s->repo);
6237 if (err)
6238 break;
6239 got_reflist_object_id_map_free(tog_refs_idmap);
6240 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6241 &tog_refs, s->repo);
6242 if (err)
6243 break;
6244 ref_view_free_refs(s);
6245 err = ref_view_load_refs(s);
6246 break;
6247 case KEY_ENTER:
6248 case '\r':
6249 if (!s->selected_entry)
6250 break;
6251 if (view_is_parent_view(view))
6252 begin_x = view_split_begin_x(view->begin_x);
6253 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6254 s->repo);
6255 view->focussed = 0;
6256 log_view->focussed = 1;
6257 if (view_is_parent_view(view)) {
6258 err = view_close_child(view);
6259 if (err)
6260 return err;
6261 view_set_child(view, log_view);
6262 view->focus_child = 1;
6263 } else
6264 *new_view = log_view;
6265 break;
6266 case 't':
6267 if (!s->selected_entry)
6268 break;
6269 if (view_is_parent_view(view))
6270 begin_x = view_split_begin_x(view->begin_x);
6271 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6272 s->repo);
6273 if (err || tree_view == NULL)
6274 break;
6275 view->focussed = 0;
6276 tree_view->focussed = 1;
6277 if (view_is_parent_view(view)) {
6278 err = view_close_child(view);
6279 if (err)
6280 return err;
6281 view_set_child(view, tree_view);
6282 view->focus_child = 1;
6283 } else
6284 *new_view = tree_view;
6285 break;
6286 case 'g':
6287 case KEY_HOME:
6288 s->selected = 0;
6289 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6290 break;
6291 case 'G':
6292 case KEY_END:
6293 s->selected = 0;
6294 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6295 for (n = 0; n < view->nlines - 1; n++) {
6296 if (re == NULL)
6297 break;
6298 s->first_displayed_entry = re;
6299 re = TAILQ_PREV(re, tog_reflist_head, entry);
6301 if (n > 0)
6302 s->selected = n - 1;
6303 break;
6304 case 'k':
6305 case KEY_UP:
6306 case CTRL('p'):
6307 if (s->selected > 0) {
6308 s->selected--;
6309 break;
6311 ref_scroll_up(s, 1);
6312 break;
6313 case KEY_PPAGE:
6314 case CTRL('b'):
6315 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6316 s->selected = 0;
6317 ref_scroll_up(s, MAX(0, view->nlines - 1));
6318 break;
6319 case 'j':
6320 case KEY_DOWN:
6321 case CTRL('n'):
6322 if (s->selected < s->ndisplayed - 1) {
6323 s->selected++;
6324 break;
6326 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6327 /* can't scroll any further */
6328 break;
6329 ref_scroll_down(s, 1);
6330 break;
6331 case KEY_NPAGE:
6332 case CTRL('f'):
6333 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6334 /* can't scroll any further; move cursor down */
6335 if (s->selected < s->ndisplayed - 1)
6336 s->selected = s->ndisplayed - 1;
6337 break;
6339 ref_scroll_down(s, view->nlines - 1);
6340 break;
6341 case CTRL('l'):
6342 tog_free_refs();
6343 err = tog_load_refs(s->repo, s->sort_by_date);
6344 if (err)
6345 break;
6346 ref_view_free_refs(s);
6347 err = ref_view_load_refs(s);
6348 break;
6349 case KEY_RESIZE:
6350 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6351 s->selected = view->nlines - 2;
6352 break;
6353 default:
6354 break;
6357 return err;
6360 __dead static void
6361 usage_ref(void)
6363 endwin();
6364 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6365 getprogname());
6366 exit(1);
6369 static const struct got_error *
6370 cmd_ref(int argc, char *argv[])
6372 const struct got_error *error;
6373 struct got_repository *repo = NULL;
6374 struct got_worktree *worktree = NULL;
6375 char *cwd = NULL, *repo_path = NULL;
6376 int ch;
6377 struct tog_view *view;
6379 while ((ch = getopt(argc, argv, "r:")) != -1) {
6380 switch (ch) {
6381 case 'r':
6382 repo_path = realpath(optarg, NULL);
6383 if (repo_path == NULL)
6384 return got_error_from_errno2("realpath",
6385 optarg);
6386 break;
6387 default:
6388 usage_ref();
6389 /* NOTREACHED */
6393 argc -= optind;
6394 argv += optind;
6396 if (argc > 1)
6397 usage_ref();
6399 if (repo_path == NULL) {
6400 cwd = getcwd(NULL, 0);
6401 if (cwd == NULL)
6402 return got_error_from_errno("getcwd");
6403 error = got_worktree_open(&worktree, cwd);
6404 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6405 goto done;
6406 if (worktree)
6407 repo_path =
6408 strdup(got_worktree_get_repo_path(worktree));
6409 else
6410 repo_path = strdup(cwd);
6411 if (repo_path == NULL) {
6412 error = got_error_from_errno("strdup");
6413 goto done;
6417 error = got_repo_open(&repo, repo_path, NULL);
6418 if (error != NULL)
6419 goto done;
6421 init_curses();
6423 error = apply_unveil(got_repo_get_path(repo), NULL);
6424 if (error)
6425 goto done;
6427 error = tog_load_refs(repo, 0);
6428 if (error)
6429 goto done;
6431 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6432 if (view == NULL) {
6433 error = got_error_from_errno("view_open");
6434 goto done;
6437 error = open_ref_view(view, repo);
6438 if (error)
6439 goto done;
6441 if (worktree) {
6442 /* Release work tree lock. */
6443 got_worktree_close(worktree);
6444 worktree = NULL;
6446 error = view_loop(view);
6447 done:
6448 free(repo_path);
6449 free(cwd);
6450 if (repo) {
6451 const struct got_error *close_err = got_repo_close(repo);
6452 if (close_err)
6453 error = close_err;
6455 tog_free_refs();
6456 return error;
6459 static void
6460 list_commands(FILE *fp)
6462 size_t i;
6464 fprintf(fp, "commands:");
6465 for (i = 0; i < nitems(tog_commands); i++) {
6466 struct tog_cmd *cmd = &tog_commands[i];
6467 fprintf(fp, " %s", cmd->name);
6469 fputc('\n', fp);
6472 __dead static void
6473 usage(int hflag, int status)
6475 FILE *fp = (status == 0) ? stdout : stderr;
6477 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6478 getprogname());
6479 if (hflag) {
6480 fprintf(fp, "lazy usage: %s path\n", getprogname());
6481 list_commands(fp);
6483 exit(status);
6486 static char **
6487 make_argv(int argc, ...)
6489 va_list ap;
6490 char **argv;
6491 int i;
6493 va_start(ap, argc);
6495 argv = calloc(argc, sizeof(char *));
6496 if (argv == NULL)
6497 err(1, "calloc");
6498 for (i = 0; i < argc; i++) {
6499 argv[i] = strdup(va_arg(ap, char *));
6500 if (argv[i] == NULL)
6501 err(1, "strdup");
6504 va_end(ap);
6505 return argv;
6509 * Try to convert 'tog path' into a 'tog log path' command.
6510 * The user could simply have mistyped the command rather than knowingly
6511 * provided a path. So check whether argv[0] can in fact be resolved
6512 * to a path in the HEAD commit and print a special error if not.
6513 * This hack is for mpi@ <3
6515 static const struct got_error *
6516 tog_log_with_path(int argc, char *argv[])
6518 const struct got_error *error = NULL, *close_err;
6519 struct tog_cmd *cmd = NULL;
6520 struct got_repository *repo = NULL;
6521 struct got_worktree *worktree = NULL;
6522 struct got_object_id *commit_id = NULL, *id = NULL;
6523 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6524 char *commit_id_str = NULL, **cmd_argv = NULL;
6526 cwd = getcwd(NULL, 0);
6527 if (cwd == NULL)
6528 return got_error_from_errno("getcwd");
6530 error = got_worktree_open(&worktree, cwd);
6531 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6532 goto done;
6534 if (worktree)
6535 repo_path = strdup(got_worktree_get_repo_path(worktree));
6536 else
6537 repo_path = strdup(cwd);
6538 if (repo_path == NULL) {
6539 error = got_error_from_errno("strdup");
6540 goto done;
6543 error = got_repo_open(&repo, repo_path, NULL);
6544 if (error != NULL)
6545 goto done;
6547 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6548 repo, worktree);
6549 if (error)
6550 goto done;
6552 error = tog_load_refs(repo, 0);
6553 if (error)
6554 goto done;
6555 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6556 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6557 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6558 if (error)
6559 goto done;
6561 if (worktree) {
6562 got_worktree_close(worktree);
6563 worktree = NULL;
6566 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6567 if (error) {
6568 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6569 goto done;
6570 fprintf(stderr, "%s: '%s' is no known command or path\n",
6571 getprogname(), argv[0]);
6572 usage(1, 1);
6573 /* not reached */
6576 close_err = got_repo_close(repo);
6577 if (error == NULL)
6578 error = close_err;
6579 repo = NULL;
6581 error = got_object_id_str(&commit_id_str, commit_id);
6582 if (error)
6583 goto done;
6585 cmd = &tog_commands[0]; /* log */
6586 argc = 4;
6587 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6588 error = cmd->cmd_main(argc, cmd_argv);
6589 done:
6590 if (repo) {
6591 close_err = got_repo_close(repo);
6592 if (error == NULL)
6593 error = close_err;
6595 if (worktree)
6596 got_worktree_close(worktree);
6597 free(id);
6598 free(commit_id_str);
6599 free(commit_id);
6600 free(cwd);
6601 free(repo_path);
6602 free(in_repo_path);
6603 if (cmd_argv) {
6604 int i;
6605 for (i = 0; i < argc; i++)
6606 free(cmd_argv[i]);
6607 free(cmd_argv);
6609 tog_free_refs();
6610 return error;
6613 int
6614 main(int argc, char *argv[])
6616 const struct got_error *error = NULL;
6617 struct tog_cmd *cmd = NULL;
6618 int ch, hflag = 0, Vflag = 0;
6619 char **cmd_argv = NULL;
6620 static struct option longopts[] = {
6621 { "version", no_argument, NULL, 'V' },
6622 { NULL, 0, NULL, 0}
6625 setlocale(LC_CTYPE, "");
6627 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6628 switch (ch) {
6629 case 'h':
6630 hflag = 1;
6631 break;
6632 case 'V':
6633 Vflag = 1;
6634 break;
6635 default:
6636 usage(hflag, 1);
6637 /* NOTREACHED */
6641 argc -= optind;
6642 argv += optind;
6643 optind = 1;
6644 optreset = 1;
6646 if (Vflag) {
6647 got_version_print_str();
6648 return 0;
6651 #ifndef PROFILE
6652 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6653 NULL) == -1)
6654 err(1, "pledge");
6655 #endif
6657 if (argc == 0) {
6658 if (hflag)
6659 usage(hflag, 0);
6660 /* Build an argument vector which runs a default command. */
6661 cmd = &tog_commands[0];
6662 argc = 1;
6663 cmd_argv = make_argv(argc, cmd->name);
6664 } else {
6665 size_t i;
6667 /* Did the user specify a command? */
6668 for (i = 0; i < nitems(tog_commands); i++) {
6669 if (strncmp(tog_commands[i].name, argv[0],
6670 strlen(argv[0])) == 0) {
6671 cmd = &tog_commands[i];
6672 break;
6677 if (cmd == NULL) {
6678 if (argc != 1)
6679 usage(0, 1);
6680 /* No command specified; try log with a path */
6681 error = tog_log_with_path(argc, argv);
6682 } else {
6683 if (hflag)
6684 cmd->cmd_usage();
6685 else
6686 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6689 endwin();
6690 putchar('\n');
6691 if (cmd_argv) {
6692 int i;
6693 for (i = 0; i < argc; i++)
6694 free(cmd_argv[i]);
6695 free(cmd_argv);
6698 if (error && error->code != GOT_ERR_CANCELLED)
6699 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6700 return 0;