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 if (view->searching == TOG_SEARCH_FORWARD)
2195 entry = TAILQ_FIRST(&s->commits.head);
2196 else
2197 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2200 while (1) {
2201 int have_match = 0;
2203 if (entry == NULL) {
2204 if (s->thread_args.log_complete ||
2205 view->searching == TOG_SEARCH_BACKWARD) {
2206 view->search_next_done =
2207 (s->matched_entry == NULL ?
2208 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2209 s->search_entry = NULL;
2210 return NULL;
2213 * Poke the log thread for more commits and return,
2214 * allowing the main loop to make progress. Search
2215 * will resume at s->search_entry once we come back.
2217 s->thread_args.commits_needed++;
2218 return trigger_log_thread(view, 0);
2221 err = match_commit(&have_match, entry->id, entry->commit,
2222 &view->regex);
2223 if (err)
2224 break;
2225 if (have_match) {
2226 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2227 s->matched_entry = entry;
2228 break;
2231 s->search_entry = entry;
2232 if (view->searching == TOG_SEARCH_FORWARD)
2233 entry = TAILQ_NEXT(entry, entry);
2234 else
2235 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2238 if (s->matched_entry) {
2239 int cur = s->selected_entry->idx;
2240 while (cur < s->matched_entry->idx) {
2241 err = input_log_view(NULL, view, KEY_DOWN);
2242 if (err)
2243 return err;
2244 cur++;
2246 while (cur > s->matched_entry->idx) {
2247 err = input_log_view(NULL, view, KEY_UP);
2248 if (err)
2249 return err;
2250 cur--;
2254 s->search_entry = NULL;
2256 return NULL;
2259 static const struct got_error *
2260 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2261 struct got_repository *repo, const char *head_ref_name,
2262 const char *in_repo_path, int log_branches)
2264 const struct got_error *err = NULL;
2265 struct tog_log_view_state *s = &view->state.log;
2266 struct got_repository *thread_repo = NULL;
2267 struct got_commit_graph *thread_graph = NULL;
2268 int errcode;
2270 if (in_repo_path != s->in_repo_path) {
2271 free(s->in_repo_path);
2272 s->in_repo_path = strdup(in_repo_path);
2273 if (s->in_repo_path == NULL)
2274 return got_error_from_errno("strdup");
2277 /* The commit queue only contains commits being displayed. */
2278 TAILQ_INIT(&s->commits.head);
2279 s->commits.ncommits = 0;
2281 s->repo = repo;
2282 if (head_ref_name) {
2283 s->head_ref_name = strdup(head_ref_name);
2284 if (s->head_ref_name == NULL) {
2285 err = got_error_from_errno("strdup");
2286 goto done;
2289 s->start_id = got_object_id_dup(start_id);
2290 if (s->start_id == NULL) {
2291 err = got_error_from_errno("got_object_id_dup");
2292 goto done;
2294 s->log_branches = log_branches;
2296 STAILQ_INIT(&s->colors);
2297 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2298 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2299 get_color_value("TOG_COLOR_COMMIT"));
2300 if (err)
2301 goto done;
2302 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2303 get_color_value("TOG_COLOR_AUTHOR"));
2304 if (err) {
2305 free_colors(&s->colors);
2306 goto done;
2308 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2309 get_color_value("TOG_COLOR_DATE"));
2310 if (err) {
2311 free_colors(&s->colors);
2312 goto done;
2316 view->show = show_log_view;
2317 view->input = input_log_view;
2318 view->close = close_log_view;
2319 view->search_start = search_start_log_view;
2320 view->search_next = search_next_log_view;
2322 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2323 if (err)
2324 goto done;
2325 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2326 !s->log_branches);
2327 if (err)
2328 goto done;
2329 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2330 s->repo, NULL, NULL);
2331 if (err)
2332 goto done;
2334 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2335 if (errcode) {
2336 err = got_error_set_errno(errcode, "pthread_cond_init");
2337 goto done;
2339 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2340 if (errcode) {
2341 err = got_error_set_errno(errcode, "pthread_cond_init");
2342 goto done;
2345 s->thread_args.commits_needed = view->nlines;
2346 s->thread_args.graph = thread_graph;
2347 s->thread_args.commits = &s->commits;
2348 s->thread_args.in_repo_path = s->in_repo_path;
2349 s->thread_args.start_id = s->start_id;
2350 s->thread_args.repo = thread_repo;
2351 s->thread_args.log_complete = 0;
2352 s->thread_args.quit = &s->quit;
2353 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2354 s->thread_args.selected_entry = &s->selected_entry;
2355 s->thread_args.searching = &view->searching;
2356 s->thread_args.search_next_done = &view->search_next_done;
2357 s->thread_args.regex = &view->regex;
2358 done:
2359 if (err)
2360 close_log_view(view);
2361 return err;
2364 static const struct got_error *
2365 show_log_view(struct tog_view *view)
2367 const struct got_error *err;
2368 struct tog_log_view_state *s = &view->state.log;
2370 if (s->thread == NULL) {
2371 int errcode = pthread_create(&s->thread, NULL, log_thread,
2372 &s->thread_args);
2373 if (errcode)
2374 return got_error_set_errno(errcode, "pthread_create");
2375 if (s->thread_args.commits_needed > 0) {
2376 err = trigger_log_thread(view, 1);
2377 if (err)
2378 return err;
2382 return draw_commits(view);
2385 static const struct got_error *
2386 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2388 const struct got_error *err = NULL;
2389 struct tog_log_view_state *s = &view->state.log;
2390 struct tog_view *diff_view = NULL, *tree_view = NULL;
2391 struct tog_view *ref_view = NULL;
2392 struct commit_queue_entry *entry;
2393 int begin_x = 0, n;
2395 if (s->thread_args.load_all) {
2396 if (ch == KEY_BACKSPACE)
2397 s->thread_args.load_all = 0;
2398 else if (s->thread_args.log_complete) {
2399 s->thread_args.load_all = 0;
2400 log_scroll_down(view, s->commits.ncommits);
2401 s->selected = MIN(view->nlines - 2,
2402 s->commits.ncommits - 1);
2403 select_commit(s);
2405 return NULL;
2408 switch (ch) {
2409 case 'q':
2410 s->quit = 1;
2411 break;
2412 case 'k':
2413 case KEY_UP:
2414 case '<':
2415 case ',':
2416 case CTRL('p'):
2417 if (s->first_displayed_entry == NULL)
2418 break;
2419 if (s->selected > 0)
2420 s->selected--;
2421 else
2422 log_scroll_up(s, 1);
2423 select_commit(s);
2424 break;
2425 case 'g':
2426 case KEY_HOME:
2427 s->selected = 0;
2428 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2429 select_commit(s);
2430 break;
2431 case KEY_PPAGE:
2432 case CTRL('b'):
2433 if (s->first_displayed_entry == NULL)
2434 break;
2435 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2436 s->selected = 0;
2437 else
2438 log_scroll_up(s, view->nlines - 1);
2439 select_commit(s);
2440 break;
2441 case 'j':
2442 case KEY_DOWN:
2443 case '>':
2444 case '.':
2445 case CTRL('n'):
2446 if (s->first_displayed_entry == NULL)
2447 break;
2448 if (s->selected < MIN(view->nlines - 2,
2449 s->commits.ncommits - 1))
2450 s->selected++;
2451 else {
2452 err = log_scroll_down(view, 1);
2453 if (err)
2454 break;
2456 select_commit(s);
2457 break;
2458 case 'G':
2459 case KEY_END: {
2460 /* We don't know yet how many commits, so we're forced to
2461 * traverse them all. */
2462 if (!s->thread_args.log_complete) {
2463 s->thread_args.load_all = 1;
2464 return trigger_log_thread(view, 0);
2467 s->selected = 0;
2468 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2469 for (n = 0; n < view->nlines - 1; n++) {
2470 if (entry == NULL)
2471 break;
2472 s->first_displayed_entry = entry;
2473 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2475 if (n > 0)
2476 s->selected = n - 1;
2477 select_commit(s);
2478 break;
2480 case KEY_NPAGE:
2481 case CTRL('f'): {
2482 struct commit_queue_entry *first;
2483 first = s->first_displayed_entry;
2484 if (first == NULL)
2485 break;
2486 err = log_scroll_down(view, view->nlines - 1);
2487 if (err)
2488 break;
2489 if (first == s->first_displayed_entry &&
2490 s->selected < MIN(view->nlines - 2,
2491 s->commits.ncommits - 1)) {
2492 /* can't scroll further down */
2493 s->selected = MIN(view->nlines - 2,
2494 s->commits.ncommits - 1);
2496 select_commit(s);
2497 break;
2499 case KEY_RESIZE:
2500 if (s->selected > view->nlines - 2)
2501 s->selected = view->nlines - 2;
2502 if (s->selected > s->commits.ncommits - 1)
2503 s->selected = s->commits.ncommits - 1;
2504 select_commit(s);
2505 if (s->commits.ncommits < view->nlines - 1 &&
2506 !s->thread_args.log_complete) {
2507 s->thread_args.commits_needed += (view->nlines - 1) -
2508 s->commits.ncommits;
2509 err = trigger_log_thread(view, 1);
2511 break;
2512 case KEY_ENTER:
2513 case ' ':
2514 case '\r':
2515 if (s->selected_entry == NULL)
2516 break;
2517 if (view_is_parent_view(view))
2518 begin_x = view_split_begin_x(view->begin_x);
2519 err = open_diff_view_for_commit(&diff_view, begin_x,
2520 s->selected_entry->commit, s->selected_entry->id,
2521 view, s->repo);
2522 if (err)
2523 break;
2524 view->focussed = 0;
2525 diff_view->focussed = 1;
2526 if (view_is_parent_view(view)) {
2527 err = view_close_child(view);
2528 if (err)
2529 return err;
2530 view_set_child(view, diff_view);
2531 view->focus_child = 1;
2532 } else
2533 *new_view = diff_view;
2534 break;
2535 case 't':
2536 if (s->selected_entry == NULL)
2537 break;
2538 if (view_is_parent_view(view))
2539 begin_x = view_split_begin_x(view->begin_x);
2540 err = browse_commit_tree(&tree_view, begin_x,
2541 s->selected_entry, s->in_repo_path, s->head_ref_name,
2542 s->repo);
2543 if (err)
2544 break;
2545 view->focussed = 0;
2546 tree_view->focussed = 1;
2547 if (view_is_parent_view(view)) {
2548 err = view_close_child(view);
2549 if (err)
2550 return err;
2551 view_set_child(view, tree_view);
2552 view->focus_child = 1;
2553 } else
2554 *new_view = tree_view;
2555 break;
2556 case KEY_BACKSPACE:
2557 case CTRL('l'):
2558 case 'B':
2559 if (ch == KEY_BACKSPACE &&
2560 got_path_is_root_dir(s->in_repo_path))
2561 break;
2562 err = stop_log_thread(s);
2563 if (err)
2564 return err;
2565 if (ch == KEY_BACKSPACE) {
2566 char *parent_path;
2567 err = got_path_dirname(&parent_path, s->in_repo_path);
2568 if (err)
2569 return err;
2570 free(s->in_repo_path);
2571 s->in_repo_path = parent_path;
2572 s->thread_args.in_repo_path = s->in_repo_path;
2573 } else if (ch == CTRL('l')) {
2574 struct got_object_id *start_id;
2575 err = got_repo_match_object_id(&start_id, NULL,
2576 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2577 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2578 if (err)
2579 return err;
2580 free(s->start_id);
2581 s->start_id = start_id;
2582 s->thread_args.start_id = s->start_id;
2583 } else /* 'B' */
2584 s->log_branches = !s->log_branches;
2586 err = got_repo_open(&s->thread_args.repo,
2587 got_repo_get_path(s->repo), NULL);
2588 if (err)
2589 return err;
2590 tog_free_refs();
2591 err = tog_load_refs(s->repo, 0);
2592 if (err)
2593 return err;
2594 err = got_commit_graph_open(&s->thread_args.graph,
2595 s->in_repo_path, !s->log_branches);
2596 if (err)
2597 return err;
2598 err = got_commit_graph_iter_start(s->thread_args.graph,
2599 s->start_id, s->repo, NULL, NULL);
2600 if (err)
2601 return err;
2602 free_commits(&s->commits);
2603 s->first_displayed_entry = NULL;
2604 s->last_displayed_entry = NULL;
2605 s->selected_entry = NULL;
2606 s->selected = 0;
2607 s->thread_args.log_complete = 0;
2608 s->quit = 0;
2609 s->thread_args.commits_needed = view->nlines;
2610 break;
2611 case 'r':
2612 if (view_is_parent_view(view))
2613 begin_x = view_split_begin_x(view->begin_x);
2614 ref_view = view_open(view->nlines, view->ncols,
2615 view->begin_y, begin_x, TOG_VIEW_REF);
2616 if (ref_view == NULL)
2617 return got_error_from_errno("view_open");
2618 err = open_ref_view(ref_view, s->repo);
2619 if (err) {
2620 view_close(ref_view);
2621 return err;
2623 view->focussed = 0;
2624 ref_view->focussed = 1;
2625 if (view_is_parent_view(view)) {
2626 err = view_close_child(view);
2627 if (err)
2628 return err;
2629 view_set_child(view, ref_view);
2630 view->focus_child = 1;
2631 } else
2632 *new_view = ref_view;
2633 break;
2634 default:
2635 break;
2638 return err;
2641 static const struct got_error *
2642 apply_unveil(const char *repo_path, const char *worktree_path)
2644 const struct got_error *error;
2646 #ifdef PROFILE
2647 if (unveil("gmon.out", "rwc") != 0)
2648 return got_error_from_errno2("unveil", "gmon.out");
2649 #endif
2650 if (repo_path && unveil(repo_path, "r") != 0)
2651 return got_error_from_errno2("unveil", repo_path);
2653 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2654 return got_error_from_errno2("unveil", worktree_path);
2656 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2657 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2659 error = got_privsep_unveil_exec_helpers();
2660 if (error != NULL)
2661 return error;
2663 if (unveil(NULL, NULL) != 0)
2664 return got_error_from_errno("unveil");
2666 return NULL;
2669 static void
2670 init_curses(void)
2672 initscr();
2673 cbreak();
2674 halfdelay(1); /* Do fast refresh while initial view is loading. */
2675 noecho();
2676 nonl();
2677 intrflush(stdscr, FALSE);
2678 keypad(stdscr, TRUE);
2679 curs_set(0);
2680 if (getenv("TOG_COLORS") != NULL) {
2681 start_color();
2682 use_default_colors();
2684 signal(SIGWINCH, tog_sigwinch);
2685 signal(SIGPIPE, tog_sigpipe);
2686 signal(SIGCONT, tog_sigcont);
2689 static const struct got_error *
2690 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2691 struct got_repository *repo, struct got_worktree *worktree)
2693 const struct got_error *err = NULL;
2695 if (argc == 0) {
2696 *in_repo_path = strdup("/");
2697 if (*in_repo_path == NULL)
2698 return got_error_from_errno("strdup");
2699 return NULL;
2702 if (worktree) {
2703 const char *prefix = got_worktree_get_path_prefix(worktree);
2704 char *p;
2706 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2707 if (err)
2708 return err;
2709 if (asprintf(in_repo_path, "%s%s%s", prefix,
2710 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2711 p) == -1) {
2712 err = got_error_from_errno("asprintf");
2713 *in_repo_path = NULL;
2715 free(p);
2716 } else
2717 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2719 return err;
2722 static const struct got_error *
2723 cmd_log(int argc, char *argv[])
2725 const struct got_error *error;
2726 struct got_repository *repo = NULL;
2727 struct got_worktree *worktree = NULL;
2728 struct got_object_id *start_id = NULL;
2729 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2730 char *start_commit = NULL, *label = NULL;
2731 struct got_reference *ref = NULL;
2732 const char *head_ref_name = NULL;
2733 int ch, log_branches = 0;
2734 struct tog_view *view;
2736 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2737 switch (ch) {
2738 case 'b':
2739 log_branches = 1;
2740 break;
2741 case 'c':
2742 start_commit = optarg;
2743 break;
2744 case 'r':
2745 repo_path = realpath(optarg, NULL);
2746 if (repo_path == NULL)
2747 return got_error_from_errno2("realpath",
2748 optarg);
2749 break;
2750 default:
2751 usage_log();
2752 /* NOTREACHED */
2756 argc -= optind;
2757 argv += optind;
2759 if (argc > 1)
2760 usage_log();
2762 if (repo_path == NULL) {
2763 cwd = getcwd(NULL, 0);
2764 if (cwd == NULL)
2765 return got_error_from_errno("getcwd");
2766 error = got_worktree_open(&worktree, cwd);
2767 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2768 goto done;
2769 if (worktree)
2770 repo_path =
2771 strdup(got_worktree_get_repo_path(worktree));
2772 else
2773 repo_path = strdup(cwd);
2774 if (repo_path == NULL) {
2775 error = got_error_from_errno("strdup");
2776 goto done;
2780 error = got_repo_open(&repo, repo_path, NULL);
2781 if (error != NULL)
2782 goto done;
2784 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2785 repo, worktree);
2786 if (error)
2787 goto done;
2789 init_curses();
2791 error = apply_unveil(got_repo_get_path(repo),
2792 worktree ? got_worktree_get_root_path(worktree) : NULL);
2793 if (error)
2794 goto done;
2796 /* already loaded by tog_log_with_path()? */
2797 if (TAILQ_EMPTY(&tog_refs)) {
2798 error = tog_load_refs(repo, 0);
2799 if (error)
2800 goto done;
2803 if (start_commit == NULL) {
2804 error = got_repo_match_object_id(&start_id, &label,
2805 worktree ? got_worktree_get_head_ref_name(worktree) :
2806 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2807 if (error)
2808 goto done;
2809 head_ref_name = label;
2810 } else {
2811 error = got_ref_open(&ref, repo, start_commit, 0);
2812 if (error == NULL)
2813 head_ref_name = got_ref_get_name(ref);
2814 else if (error->code != GOT_ERR_NOT_REF)
2815 goto done;
2816 error = got_repo_match_object_id(&start_id, NULL,
2817 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2818 if (error)
2819 goto done;
2822 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2823 if (view == NULL) {
2824 error = got_error_from_errno("view_open");
2825 goto done;
2827 error = open_log_view(view, start_id, repo, head_ref_name,
2828 in_repo_path, log_branches);
2829 if (error)
2830 goto done;
2831 if (worktree) {
2832 /* Release work tree lock. */
2833 got_worktree_close(worktree);
2834 worktree = NULL;
2836 error = view_loop(view);
2837 done:
2838 free(in_repo_path);
2839 free(repo_path);
2840 free(cwd);
2841 free(start_id);
2842 free(label);
2843 if (ref)
2844 got_ref_close(ref);
2845 if (repo) {
2846 const struct got_error *close_err = got_repo_close(repo);
2847 if (error == NULL)
2848 error = close_err;
2850 if (worktree)
2851 got_worktree_close(worktree);
2852 tog_free_refs();
2853 return error;
2856 __dead static void
2857 usage_diff(void)
2859 endwin();
2860 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2861 "[-w] object1 object2\n", getprogname());
2862 exit(1);
2865 static int
2866 match_line(const char *line, regex_t *regex, size_t nmatch,
2867 regmatch_t *regmatch)
2869 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2872 struct tog_color *
2873 match_color(struct tog_colors *colors, const char *line)
2875 struct tog_color *tc = NULL;
2877 STAILQ_FOREACH(tc, colors, entry) {
2878 if (match_line(line, &tc->regex, 0, NULL))
2879 return tc;
2882 return NULL;
2885 static const struct got_error *
2886 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2887 WINDOW *window, regmatch_t *regmatch)
2889 const struct got_error *err = NULL;
2890 wchar_t *wline;
2891 int width;
2892 char *s;
2894 *wtotal = 0;
2896 s = strndup(line, regmatch->rm_so);
2897 if (s == NULL)
2898 return got_error_from_errno("strndup");
2900 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2901 if (err) {
2902 free(s);
2903 return err;
2905 waddwstr(window, wline);
2906 free(wline);
2907 free(s);
2908 wlimit -= width;
2909 *wtotal += width;
2911 if (wlimit > 0) {
2912 s = strndup(line + regmatch->rm_so,
2913 regmatch->rm_eo - regmatch->rm_so);
2914 if (s == NULL) {
2915 err = got_error_from_errno("strndup");
2916 free(s);
2917 return err;
2919 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2920 if (err) {
2921 free(s);
2922 return err;
2924 wattr_on(window, A_STANDOUT, NULL);
2925 waddwstr(window, wline);
2926 wattr_off(window, A_STANDOUT, NULL);
2927 free(wline);
2928 free(s);
2929 wlimit -= width;
2930 *wtotal += width;
2933 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2934 err = format_line(&wline, &width,
2935 line + regmatch->rm_eo, wlimit, col_tab_align);
2936 if (err)
2937 return err;
2938 waddwstr(window, wline);
2939 free(wline);
2940 *wtotal += width;
2943 return NULL;
2946 static const struct got_error *
2947 draw_file(struct tog_view *view, const char *header)
2949 struct tog_diff_view_state *s = &view->state.diff;
2950 regmatch_t *regmatch = &view->regmatch;
2951 const struct got_error *err;
2952 int nprinted = 0;
2953 char *line;
2954 size_t linesize = 0;
2955 ssize_t linelen;
2956 struct tog_color *tc;
2957 wchar_t *wline;
2958 int width;
2959 int max_lines = view->nlines;
2960 int nlines = s->nlines;
2961 off_t line_offset;
2963 line_offset = s->line_offsets[s->first_displayed_line - 1];
2964 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2965 return got_error_from_errno("fseek");
2967 werase(view->window);
2969 if (header) {
2970 if (asprintf(&line, "[%d/%d] %s",
2971 s->first_displayed_line - 1 + s->selected_line, nlines,
2972 header) == -1)
2973 return got_error_from_errno("asprintf");
2974 err = format_line(&wline, &width, line, view->ncols, 0);
2975 free(line);
2976 if (err)
2977 return err;
2979 if (view_needs_focus_indication(view))
2980 wstandout(view->window);
2981 waddwstr(view->window, wline);
2982 free(wline);
2983 wline = NULL;
2984 if (view_needs_focus_indication(view))
2985 wstandend(view->window);
2986 if (width <= view->ncols - 1)
2987 waddch(view->window, '\n');
2989 if (max_lines <= 1)
2990 return NULL;
2991 max_lines--;
2994 s->eof = 0;
2995 line = NULL;
2996 while (max_lines > 0 && nprinted < max_lines) {
2997 linelen = getline(&line, &linesize, s->f);
2998 if (linelen == -1) {
2999 if (feof(s->f)) {
3000 s->eof = 1;
3001 break;
3003 free(line);
3004 return got_ferror(s->f, GOT_ERR_IO);
3007 tc = match_color(&s->colors, line);
3008 if (tc)
3009 wattr_on(view->window,
3010 COLOR_PAIR(tc->colorpair), NULL);
3011 if (s->first_displayed_line + nprinted == s->matched_line &&
3012 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3013 err = add_matched_line(&width, line, view->ncols, 0,
3014 view->window, regmatch);
3015 if (err) {
3016 free(line);
3017 return err;
3019 } else {
3020 err = format_line(&wline, &width, line, view->ncols, 0);
3021 if (err) {
3022 free(line);
3023 return err;
3025 waddwstr(view->window, wline);
3026 free(wline);
3027 wline = NULL;
3029 if (tc)
3030 wattr_off(view->window,
3031 COLOR_PAIR(tc->colorpair), NULL);
3032 if (width <= view->ncols - 1)
3033 waddch(view->window, '\n');
3034 nprinted++;
3036 free(line);
3037 if (nprinted >= 1)
3038 s->last_displayed_line = s->first_displayed_line +
3039 (nprinted - 1);
3040 else
3041 s->last_displayed_line = s->first_displayed_line;
3043 view_vborder(view);
3045 if (s->eof) {
3046 while (nprinted < view->nlines) {
3047 waddch(view->window, '\n');
3048 nprinted++;
3051 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3052 if (err) {
3053 return err;
3056 wstandout(view->window);
3057 waddwstr(view->window, wline);
3058 free(wline);
3059 wline = NULL;
3060 wstandend(view->window);
3063 return NULL;
3066 static char *
3067 get_datestr(time_t *time, char *datebuf)
3069 struct tm mytm, *tm;
3070 char *p, *s;
3072 tm = gmtime_r(time, &mytm);
3073 if (tm == NULL)
3074 return NULL;
3075 s = asctime_r(tm, datebuf);
3076 if (s == NULL)
3077 return NULL;
3078 p = strchr(s, '\n');
3079 if (p)
3080 *p = '\0';
3081 return s;
3084 static const struct got_error *
3085 get_changed_paths(struct got_pathlist_head *paths,
3086 struct got_commit_object *commit, struct got_repository *repo)
3088 const struct got_error *err = NULL;
3089 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3090 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3091 struct got_object_qid *qid;
3093 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3094 if (qid != NULL) {
3095 struct got_commit_object *pcommit;
3096 err = got_object_open_as_commit(&pcommit, repo,
3097 qid->id);
3098 if (err)
3099 return err;
3101 tree_id1 = got_object_id_dup(
3102 got_object_commit_get_tree_id(pcommit));
3103 if (tree_id1 == NULL) {
3104 got_object_commit_close(pcommit);
3105 return got_error_from_errno("got_object_id_dup");
3107 got_object_commit_close(pcommit);
3111 if (tree_id1) {
3112 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3113 if (err)
3114 goto done;
3117 tree_id2 = got_object_commit_get_tree_id(commit);
3118 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3119 if (err)
3120 goto done;
3122 err = got_diff_tree(tree1, tree2, "", "", repo,
3123 got_diff_tree_collect_changed_paths, paths, 0);
3124 done:
3125 if (tree1)
3126 got_object_tree_close(tree1);
3127 if (tree2)
3128 got_object_tree_close(tree2);
3129 free(tree_id1);
3130 return err;
3133 static const struct got_error *
3134 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3136 off_t *p;
3138 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3139 if (p == NULL)
3140 return got_error_from_errno("reallocarray");
3141 *line_offsets = p;
3142 (*line_offsets)[*nlines] = off;
3143 (*nlines)++;
3144 return NULL;
3147 static const struct got_error *
3148 write_commit_info(off_t **line_offsets, size_t *nlines,
3149 struct got_object_id *commit_id, struct got_reflist_head *refs,
3150 struct got_repository *repo, FILE *outfile)
3152 const struct got_error *err = NULL;
3153 char datebuf[26], *datestr;
3154 struct got_commit_object *commit;
3155 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3156 time_t committer_time;
3157 const char *author, *committer;
3158 char *refs_str = NULL;
3159 struct got_pathlist_head changed_paths;
3160 struct got_pathlist_entry *pe;
3161 off_t outoff = 0;
3162 int n;
3164 TAILQ_INIT(&changed_paths);
3166 if (refs) {
3167 err = build_refs_str(&refs_str, refs, commit_id, repo);
3168 if (err)
3169 return err;
3172 err = got_object_open_as_commit(&commit, repo, commit_id);
3173 if (err)
3174 return err;
3176 err = got_object_id_str(&id_str, commit_id);
3177 if (err) {
3178 err = got_error_from_errno("got_object_id_str");
3179 goto done;
3182 err = add_line_offset(line_offsets, nlines, 0);
3183 if (err)
3184 goto done;
3186 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3187 refs_str ? refs_str : "", refs_str ? ")" : "");
3188 if (n < 0) {
3189 err = got_error_from_errno("fprintf");
3190 goto done;
3192 outoff += n;
3193 err = add_line_offset(line_offsets, nlines, outoff);
3194 if (err)
3195 goto done;
3197 n = fprintf(outfile, "from: %s\n",
3198 got_object_commit_get_author(commit));
3199 if (n < 0) {
3200 err = got_error_from_errno("fprintf");
3201 goto done;
3203 outoff += n;
3204 err = add_line_offset(line_offsets, nlines, outoff);
3205 if (err)
3206 goto done;
3208 committer_time = got_object_commit_get_committer_time(commit);
3209 datestr = get_datestr(&committer_time, datebuf);
3210 if (datestr) {
3211 n = fprintf(outfile, "date: %s UTC\n", datestr);
3212 if (n < 0) {
3213 err = got_error_from_errno("fprintf");
3214 goto done;
3216 outoff += n;
3217 err = add_line_offset(line_offsets, nlines, outoff);
3218 if (err)
3219 goto done;
3221 author = got_object_commit_get_author(commit);
3222 committer = got_object_commit_get_committer(commit);
3223 if (strcmp(author, committer) != 0) {
3224 n = fprintf(outfile, "via: %s\n", committer);
3225 if (n < 0) {
3226 err = got_error_from_errno("fprintf");
3227 goto done;
3229 outoff += n;
3230 err = add_line_offset(line_offsets, nlines, outoff);
3231 if (err)
3232 goto done;
3234 if (got_object_commit_get_nparents(commit) > 1) {
3235 const struct got_object_id_queue *parent_ids;
3236 struct got_object_qid *qid;
3237 int pn = 1;
3238 parent_ids = got_object_commit_get_parent_ids(commit);
3239 STAILQ_FOREACH(qid, parent_ids, entry) {
3240 err = got_object_id_str(&id_str, qid->id);
3241 if (err)
3242 goto done;
3243 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3244 if (n < 0) {
3245 err = got_error_from_errno("fprintf");
3246 goto done;
3248 outoff += n;
3249 err = add_line_offset(line_offsets, nlines, outoff);
3250 if (err)
3251 goto done;
3252 free(id_str);
3253 id_str = NULL;
3257 err = got_object_commit_get_logmsg(&logmsg, commit);
3258 if (err)
3259 goto done;
3260 s = logmsg;
3261 while ((line = strsep(&s, "\n")) != NULL) {
3262 n = fprintf(outfile, "%s\n", line);
3263 if (n < 0) {
3264 err = got_error_from_errno("fprintf");
3265 goto done;
3267 outoff += n;
3268 err = add_line_offset(line_offsets, nlines, outoff);
3269 if (err)
3270 goto done;
3273 err = get_changed_paths(&changed_paths, commit, repo);
3274 if (err)
3275 goto done;
3276 TAILQ_FOREACH(pe, &changed_paths, entry) {
3277 struct got_diff_changed_path *cp = pe->data;
3278 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3279 if (n < 0) {
3280 err = got_error_from_errno("fprintf");
3281 goto done;
3283 outoff += n;
3284 err = add_line_offset(line_offsets, nlines, outoff);
3285 if (err)
3286 goto done;
3287 free((char *)pe->path);
3288 free(pe->data);
3291 fputc('\n', outfile);
3292 outoff++;
3293 err = add_line_offset(line_offsets, nlines, outoff);
3294 done:
3295 got_pathlist_free(&changed_paths);
3296 free(id_str);
3297 free(logmsg);
3298 free(refs_str);
3299 got_object_commit_close(commit);
3300 if (err) {
3301 free(*line_offsets);
3302 *line_offsets = NULL;
3303 *nlines = 0;
3305 return err;
3308 static const struct got_error *
3309 create_diff(struct tog_diff_view_state *s)
3311 const struct got_error *err = NULL;
3312 FILE *f = NULL;
3313 int obj_type;
3315 free(s->line_offsets);
3316 s->line_offsets = malloc(sizeof(off_t));
3317 if (s->line_offsets == NULL)
3318 return got_error_from_errno("malloc");
3319 s->nlines = 0;
3321 f = got_opentemp();
3322 if (f == NULL) {
3323 err = got_error_from_errno("got_opentemp");
3324 goto done;
3326 if (s->f && fclose(s->f) == EOF) {
3327 err = got_error_from_errno("fclose");
3328 goto done;
3330 s->f = f;
3332 if (s->id1)
3333 err = got_object_get_type(&obj_type, s->repo, s->id1);
3334 else
3335 err = got_object_get_type(&obj_type, s->repo, s->id2);
3336 if (err)
3337 goto done;
3339 switch (obj_type) {
3340 case GOT_OBJ_TYPE_BLOB:
3341 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3342 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3343 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3344 break;
3345 case GOT_OBJ_TYPE_TREE:
3346 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3347 s->id1, s->id2, NULL, "", "", s->diff_context,
3348 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3349 break;
3350 case GOT_OBJ_TYPE_COMMIT: {
3351 const struct got_object_id_queue *parent_ids;
3352 struct got_object_qid *pid;
3353 struct got_commit_object *commit2;
3354 struct got_reflist_head *refs;
3356 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3357 if (err)
3358 goto done;
3359 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3360 /* Show commit info if we're diffing to a parent/root commit. */
3361 if (s->id1 == NULL) {
3362 err = write_commit_info(&s->line_offsets, &s->nlines,
3363 s->id2, refs, s->repo, s->f);
3364 if (err)
3365 goto done;
3366 } else {
3367 parent_ids = got_object_commit_get_parent_ids(commit2);
3368 STAILQ_FOREACH(pid, parent_ids, entry) {
3369 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3370 err = write_commit_info(
3371 &s->line_offsets, &s->nlines,
3372 s->id2, refs, s->repo, s->f);
3373 if (err)
3374 goto done;
3375 break;
3379 got_object_commit_close(commit2);
3381 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3382 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3383 s->force_text_diff, s->repo, s->f);
3384 break;
3386 default:
3387 err = got_error(GOT_ERR_OBJ_TYPE);
3388 break;
3390 if (err)
3391 goto done;
3392 done:
3393 if (s->f && fflush(s->f) != 0 && err == NULL)
3394 err = got_error_from_errno("fflush");
3395 return err;
3398 static void
3399 diff_view_indicate_progress(struct tog_view *view)
3401 mvwaddstr(view->window, 0, 0, "diffing...");
3402 update_panels();
3403 doupdate();
3406 static const struct got_error *
3407 search_start_diff_view(struct tog_view *view)
3409 struct tog_diff_view_state *s = &view->state.diff;
3411 s->matched_line = 0;
3412 return NULL;
3415 static const struct got_error *
3416 search_next_diff_view(struct tog_view *view)
3418 struct tog_diff_view_state *s = &view->state.diff;
3419 int lineno;
3420 char *line = NULL;
3421 size_t linesize = 0;
3422 ssize_t linelen;
3424 if (!view->searching) {
3425 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3426 return NULL;
3429 if (s->matched_line) {
3430 if (view->searching == TOG_SEARCH_FORWARD)
3431 lineno = s->matched_line + 1;
3432 else
3433 lineno = s->matched_line - 1;
3434 } else {
3435 if (view->searching == TOG_SEARCH_FORWARD)
3436 lineno = 1;
3437 else
3438 lineno = s->nlines;
3441 while (1) {
3442 off_t offset;
3444 if (lineno <= 0 || lineno > s->nlines) {
3445 if (s->matched_line == 0) {
3446 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3447 break;
3450 if (view->searching == TOG_SEARCH_FORWARD)
3451 lineno = 1;
3452 else
3453 lineno = s->nlines;
3456 offset = s->line_offsets[lineno - 1];
3457 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3458 free(line);
3459 return got_error_from_errno("fseeko");
3461 linelen = getline(&line, &linesize, s->f);
3462 if (linelen != -1 &&
3463 match_line(line, &view->regex, 1, &view->regmatch)) {
3464 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3465 s->matched_line = lineno;
3466 break;
3468 if (view->searching == TOG_SEARCH_FORWARD)
3469 lineno++;
3470 else
3471 lineno--;
3473 free(line);
3475 if (s->matched_line) {
3476 s->first_displayed_line = s->matched_line;
3477 s->selected_line = 1;
3480 return NULL;
3483 static const struct got_error *
3484 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3485 struct got_object_id *id2, const char *label1, const char *label2,
3486 int diff_context, int ignore_whitespace, int force_text_diff,
3487 struct tog_view *log_view, struct got_repository *repo)
3489 const struct got_error *err;
3490 struct tog_diff_view_state *s = &view->state.diff;
3492 if (id1 != NULL && id2 != NULL) {
3493 int type1, type2;
3494 err = got_object_get_type(&type1, repo, id1);
3495 if (err)
3496 return err;
3497 err = got_object_get_type(&type2, repo, id2);
3498 if (err)
3499 return err;
3501 if (type1 != type2)
3502 return got_error(GOT_ERR_OBJ_TYPE);
3504 s->first_displayed_line = 1;
3505 s->last_displayed_line = view->nlines;
3506 s->selected_line = 1;
3507 s->repo = repo;
3508 s->id1 = id1;
3509 s->id2 = id2;
3510 s->label1 = label1;
3511 s->label2 = label2;
3513 if (id1) {
3514 s->id1 = got_object_id_dup(id1);
3515 if (s->id1 == NULL)
3516 return got_error_from_errno("got_object_id_dup");
3517 } else
3518 s->id1 = NULL;
3520 s->id2 = got_object_id_dup(id2);
3521 if (s->id2 == NULL) {
3522 free(s->id1);
3523 s->id1 = NULL;
3524 return got_error_from_errno("got_object_id_dup");
3526 s->f = NULL;
3527 s->first_displayed_line = 1;
3528 s->last_displayed_line = view->nlines;
3529 s->diff_context = diff_context;
3530 s->ignore_whitespace = ignore_whitespace;
3531 s->force_text_diff = force_text_diff;
3532 s->log_view = log_view;
3533 s->repo = repo;
3535 STAILQ_INIT(&s->colors);
3536 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3537 err = add_color(&s->colors,
3538 "^-", TOG_COLOR_DIFF_MINUS,
3539 get_color_value("TOG_COLOR_DIFF_MINUS"));
3540 if (err)
3541 return err;
3542 err = add_color(&s->colors, "^\\+",
3543 TOG_COLOR_DIFF_PLUS,
3544 get_color_value("TOG_COLOR_DIFF_PLUS"));
3545 if (err) {
3546 free_colors(&s->colors);
3547 return err;
3549 err = add_color(&s->colors,
3550 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3551 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3552 if (err) {
3553 free_colors(&s->colors);
3554 return err;
3557 err = add_color(&s->colors,
3558 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3559 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3560 get_color_value("TOG_COLOR_DIFF_META"));
3561 if (err) {
3562 free_colors(&s->colors);
3563 return err;
3566 err = add_color(&s->colors,
3567 "^(from|via): ", TOG_COLOR_AUTHOR,
3568 get_color_value("TOG_COLOR_AUTHOR"));
3569 if (err) {
3570 free_colors(&s->colors);
3571 return err;
3574 err = add_color(&s->colors,
3575 "^date: ", TOG_COLOR_DATE,
3576 get_color_value("TOG_COLOR_DATE"));
3577 if (err) {
3578 free_colors(&s->colors);
3579 return err;
3583 if (log_view && view_is_splitscreen(view))
3584 show_log_view(log_view); /* draw vborder */
3585 diff_view_indicate_progress(view);
3587 s->line_offsets = NULL;
3588 s->nlines = 0;
3589 err = create_diff(s);
3590 if (err) {
3591 free(s->id1);
3592 s->id1 = NULL;
3593 free(s->id2);
3594 s->id2 = NULL;
3595 free_colors(&s->colors);
3596 return err;
3599 view->show = show_diff_view;
3600 view->input = input_diff_view;
3601 view->close = close_diff_view;
3602 view->search_start = search_start_diff_view;
3603 view->search_next = search_next_diff_view;
3605 return NULL;
3608 static const struct got_error *
3609 close_diff_view(struct tog_view *view)
3611 const struct got_error *err = NULL;
3612 struct tog_diff_view_state *s = &view->state.diff;
3614 free(s->id1);
3615 s->id1 = NULL;
3616 free(s->id2);
3617 s->id2 = NULL;
3618 if (s->f && fclose(s->f) == EOF)
3619 err = got_error_from_errno("fclose");
3620 free_colors(&s->colors);
3621 free(s->line_offsets);
3622 s->line_offsets = NULL;
3623 s->nlines = 0;
3624 return err;
3627 static const struct got_error *
3628 show_diff_view(struct tog_view *view)
3630 const struct got_error *err;
3631 struct tog_diff_view_state *s = &view->state.diff;
3632 char *id_str1 = NULL, *id_str2, *header;
3633 const char *label1, *label2;
3635 if (s->id1) {
3636 err = got_object_id_str(&id_str1, s->id1);
3637 if (err)
3638 return err;
3639 label1 = s->label1 ? : id_str1;
3640 } else
3641 label1 = "/dev/null";
3643 err = got_object_id_str(&id_str2, s->id2);
3644 if (err)
3645 return err;
3646 label2 = s->label2 ? : id_str2;
3648 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3649 err = got_error_from_errno("asprintf");
3650 free(id_str1);
3651 free(id_str2);
3652 return err;
3654 free(id_str1);
3655 free(id_str2);
3657 err = draw_file(view, header);
3658 free(header);
3659 return err;
3662 static const struct got_error *
3663 set_selected_commit(struct tog_diff_view_state *s,
3664 struct commit_queue_entry *entry)
3666 const struct got_error *err;
3667 const struct got_object_id_queue *parent_ids;
3668 struct got_commit_object *selected_commit;
3669 struct got_object_qid *pid;
3671 free(s->id2);
3672 s->id2 = got_object_id_dup(entry->id);
3673 if (s->id2 == NULL)
3674 return got_error_from_errno("got_object_id_dup");
3676 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3677 if (err)
3678 return err;
3679 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3680 free(s->id1);
3681 pid = STAILQ_FIRST(parent_ids);
3682 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3683 got_object_commit_close(selected_commit);
3684 return NULL;
3687 static const struct got_error *
3688 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3690 const struct got_error *err = NULL;
3691 struct tog_diff_view_state *s = &view->state.diff;
3692 struct tog_log_view_state *ls;
3693 struct commit_queue_entry *old_selected_entry;
3694 char *line = NULL;
3695 size_t linesize = 0;
3696 ssize_t linelen;
3697 int i;
3699 switch (ch) {
3700 case 'a':
3701 case 'w':
3702 if (ch == 'a')
3703 s->force_text_diff = !s->force_text_diff;
3704 if (ch == 'w')
3705 s->ignore_whitespace = !s->ignore_whitespace;
3706 wclear(view->window);
3707 s->first_displayed_line = 1;
3708 s->last_displayed_line = view->nlines;
3709 diff_view_indicate_progress(view);
3710 err = create_diff(s);
3711 break;
3712 case 'g':
3713 case KEY_HOME:
3714 s->first_displayed_line = 1;
3715 break;
3716 case 'G':
3717 case KEY_END:
3718 if (s->eof)
3719 break;
3721 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3722 s->eof = 1;
3723 break;
3724 case 'k':
3725 case KEY_UP:
3726 case CTRL('p'):
3727 if (s->first_displayed_line > 1)
3728 s->first_displayed_line--;
3729 break;
3730 case KEY_PPAGE:
3731 case CTRL('b'):
3732 if (s->first_displayed_line == 1)
3733 break;
3734 i = 0;
3735 while (i++ < view->nlines - 1 &&
3736 s->first_displayed_line > 1)
3737 s->first_displayed_line--;
3738 break;
3739 case 'j':
3740 case KEY_DOWN:
3741 case CTRL('n'):
3742 if (!s->eof)
3743 s->first_displayed_line++;
3744 break;
3745 case KEY_NPAGE:
3746 case CTRL('f'):
3747 case ' ':
3748 if (s->eof)
3749 break;
3750 i = 0;
3751 while (!s->eof && i++ < view->nlines - 1) {
3752 linelen = getline(&line, &linesize, s->f);
3753 s->first_displayed_line++;
3754 if (linelen == -1) {
3755 if (feof(s->f)) {
3756 s->eof = 1;
3757 } else
3758 err = got_ferror(s->f, GOT_ERR_IO);
3759 break;
3762 free(line);
3763 break;
3764 case '[':
3765 if (s->diff_context > 0) {
3766 s->diff_context--;
3767 diff_view_indicate_progress(view);
3768 err = create_diff(s);
3769 if (s->first_displayed_line + view->nlines - 1 >
3770 s->nlines) {
3771 s->first_displayed_line = 1;
3772 s->last_displayed_line = view->nlines;
3775 break;
3776 case ']':
3777 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3778 s->diff_context++;
3779 diff_view_indicate_progress(view);
3780 err = create_diff(s);
3782 break;
3783 case '<':
3784 case ',':
3785 if (s->log_view == NULL)
3786 break;
3787 ls = &s->log_view->state.log;
3788 old_selected_entry = ls->selected_entry;
3790 err = input_log_view(NULL, s->log_view, KEY_UP);
3791 if (err)
3792 break;
3794 if (old_selected_entry == ls->selected_entry)
3795 break;
3797 err = set_selected_commit(s, ls->selected_entry);
3798 if (err)
3799 break;
3801 s->first_displayed_line = 1;
3802 s->last_displayed_line = view->nlines;
3804 diff_view_indicate_progress(view);
3805 err = create_diff(s);
3806 break;
3807 case '>':
3808 case '.':
3809 if (s->log_view == NULL)
3810 break;
3811 ls = &s->log_view->state.log;
3812 old_selected_entry = ls->selected_entry;
3814 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3815 if (err)
3816 break;
3818 if (old_selected_entry == ls->selected_entry)
3819 break;
3821 err = set_selected_commit(s, ls->selected_entry);
3822 if (err)
3823 break;
3825 s->first_displayed_line = 1;
3826 s->last_displayed_line = view->nlines;
3828 diff_view_indicate_progress(view);
3829 err = create_diff(s);
3830 break;
3831 default:
3832 break;
3835 return err;
3838 static const struct got_error *
3839 cmd_diff(int argc, char *argv[])
3841 const struct got_error *error = NULL;
3842 struct got_repository *repo = NULL;
3843 struct got_worktree *worktree = NULL;
3844 struct got_object_id *id1 = NULL, *id2 = NULL;
3845 char *repo_path = NULL, *cwd = NULL;
3846 char *id_str1 = NULL, *id_str2 = NULL;
3847 char *label1 = NULL, *label2 = NULL;
3848 int diff_context = 3, ignore_whitespace = 0;
3849 int ch, force_text_diff = 0;
3850 const char *errstr;
3851 struct tog_view *view;
3853 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3854 switch (ch) {
3855 case 'a':
3856 force_text_diff = 1;
3857 break;
3858 case 'C':
3859 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3860 &errstr);
3861 if (errstr != NULL)
3862 err(1, "-C option %s", errstr);
3863 break;
3864 case 'r':
3865 repo_path = realpath(optarg, NULL);
3866 if (repo_path == NULL)
3867 return got_error_from_errno2("realpath",
3868 optarg);
3869 got_path_strip_trailing_slashes(repo_path);
3870 break;
3871 case 'w':
3872 ignore_whitespace = 1;
3873 break;
3874 default:
3875 usage_diff();
3876 /* NOTREACHED */
3880 argc -= optind;
3881 argv += optind;
3883 if (argc == 0) {
3884 usage_diff(); /* TODO show local worktree changes */
3885 } else if (argc == 2) {
3886 id_str1 = argv[0];
3887 id_str2 = argv[1];
3888 } else
3889 usage_diff();
3891 if (repo_path == NULL) {
3892 cwd = getcwd(NULL, 0);
3893 if (cwd == NULL)
3894 return got_error_from_errno("getcwd");
3895 error = got_worktree_open(&worktree, cwd);
3896 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3897 goto done;
3898 if (worktree)
3899 repo_path =
3900 strdup(got_worktree_get_repo_path(worktree));
3901 else
3902 repo_path = strdup(cwd);
3903 if (repo_path == NULL) {
3904 error = got_error_from_errno("strdup");
3905 goto done;
3909 error = got_repo_open(&repo, repo_path, NULL);
3910 if (error)
3911 goto done;
3913 init_curses();
3915 error = apply_unveil(got_repo_get_path(repo), NULL);
3916 if (error)
3917 goto done;
3919 error = tog_load_refs(repo, 0);
3920 if (error)
3921 goto done;
3923 error = got_repo_match_object_id(&id1, &label1, id_str1,
3924 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3925 if (error)
3926 goto done;
3928 error = got_repo_match_object_id(&id2, &label2, id_str2,
3929 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3930 if (error)
3931 goto done;
3933 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3934 if (view == NULL) {
3935 error = got_error_from_errno("view_open");
3936 goto done;
3938 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3939 ignore_whitespace, force_text_diff, NULL, repo);
3940 if (error)
3941 goto done;
3942 error = view_loop(view);
3943 done:
3944 free(label1);
3945 free(label2);
3946 free(repo_path);
3947 free(cwd);
3948 if (repo) {
3949 const struct got_error *close_err = got_repo_close(repo);
3950 if (error == NULL)
3951 error = close_err;
3953 if (worktree)
3954 got_worktree_close(worktree);
3955 tog_free_refs();
3956 return error;
3959 __dead static void
3960 usage_blame(void)
3962 endwin();
3963 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3964 getprogname());
3965 exit(1);
3968 struct tog_blame_line {
3969 int annotated;
3970 struct got_object_id *id;
3973 static const struct got_error *
3974 draw_blame(struct tog_view *view)
3976 struct tog_blame_view_state *s = &view->state.blame;
3977 struct tog_blame *blame = &s->blame;
3978 regmatch_t *regmatch = &view->regmatch;
3979 const struct got_error *err;
3980 int lineno = 0, nprinted = 0;
3981 char *line = NULL;
3982 size_t linesize = 0;
3983 ssize_t linelen;
3984 wchar_t *wline;
3985 int width;
3986 struct tog_blame_line *blame_line;
3987 struct got_object_id *prev_id = NULL;
3988 char *id_str;
3989 struct tog_color *tc;
3991 err = got_object_id_str(&id_str, s->blamed_commit->id);
3992 if (err)
3993 return err;
3995 rewind(blame->f);
3996 werase(view->window);
3998 if (asprintf(&line, "commit %s", id_str) == -1) {
3999 err = got_error_from_errno("asprintf");
4000 free(id_str);
4001 return err;
4004 err = format_line(&wline, &width, line, view->ncols, 0);
4005 free(line);
4006 line = NULL;
4007 if (err)
4008 return err;
4009 if (view_needs_focus_indication(view))
4010 wstandout(view->window);
4011 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4012 if (tc)
4013 wattr_on(view->window,
4014 COLOR_PAIR(tc->colorpair), NULL);
4015 waddwstr(view->window, wline);
4016 if (tc)
4017 wattr_off(view->window,
4018 COLOR_PAIR(tc->colorpair), NULL);
4019 if (view_needs_focus_indication(view))
4020 wstandend(view->window);
4021 free(wline);
4022 wline = NULL;
4023 if (width < view->ncols - 1)
4024 waddch(view->window, '\n');
4026 if (asprintf(&line, "[%d/%d] %s%s",
4027 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4028 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4029 free(id_str);
4030 return got_error_from_errno("asprintf");
4032 free(id_str);
4033 err = format_line(&wline, &width, line, view->ncols, 0);
4034 free(line);
4035 line = NULL;
4036 if (err)
4037 return err;
4038 waddwstr(view->window, wline);
4039 free(wline);
4040 wline = NULL;
4041 if (width < view->ncols - 1)
4042 waddch(view->window, '\n');
4044 s->eof = 0;
4045 while (nprinted < view->nlines - 2) {
4046 linelen = getline(&line, &linesize, blame->f);
4047 if (linelen == -1) {
4048 if (feof(blame->f)) {
4049 s->eof = 1;
4050 break;
4052 free(line);
4053 return got_ferror(blame->f, GOT_ERR_IO);
4055 if (++lineno < s->first_displayed_line)
4056 continue;
4058 if (view->focussed && nprinted == s->selected_line - 1)
4059 wstandout(view->window);
4061 if (blame->nlines > 0) {
4062 blame_line = &blame->lines[lineno - 1];
4063 if (blame_line->annotated && prev_id &&
4064 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4065 !(view->focussed &&
4066 nprinted == s->selected_line - 1)) {
4067 waddstr(view->window, " ");
4068 } else if (blame_line->annotated) {
4069 char *id_str;
4070 err = got_object_id_str(&id_str, blame_line->id);
4071 if (err) {
4072 free(line);
4073 return err;
4075 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4076 if (tc)
4077 wattr_on(view->window,
4078 COLOR_PAIR(tc->colorpair), NULL);
4079 wprintw(view->window, "%.8s", id_str);
4080 if (tc)
4081 wattr_off(view->window,
4082 COLOR_PAIR(tc->colorpair), NULL);
4083 free(id_str);
4084 prev_id = blame_line->id;
4085 } else {
4086 waddstr(view->window, "........");
4087 prev_id = NULL;
4089 } else {
4090 waddstr(view->window, "........");
4091 prev_id = NULL;
4094 if (view->focussed && nprinted == s->selected_line - 1)
4095 wstandend(view->window);
4096 waddstr(view->window, " ");
4098 if (view->ncols <= 9) {
4099 width = 9;
4100 wline = wcsdup(L"");
4101 if (wline == NULL) {
4102 err = got_error_from_errno("wcsdup");
4103 free(line);
4104 return err;
4106 } else if (s->first_displayed_line + nprinted ==
4107 s->matched_line &&
4108 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4109 err = add_matched_line(&width, line, view->ncols - 9, 9,
4110 view->window, regmatch);
4111 if (err) {
4112 free(line);
4113 return err;
4115 width += 9;
4116 } else {
4117 err = format_line(&wline, &width, line,
4118 view->ncols - 9, 9);
4119 waddwstr(view->window, wline);
4120 free(wline);
4121 wline = NULL;
4122 width += 9;
4125 if (width <= view->ncols - 1)
4126 waddch(view->window, '\n');
4127 if (++nprinted == 1)
4128 s->first_displayed_line = lineno;
4130 free(line);
4131 s->last_displayed_line = lineno;
4133 view_vborder(view);
4135 return NULL;
4138 static const struct got_error *
4139 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4141 const struct got_error *err = NULL;
4142 struct tog_blame_cb_args *a = arg;
4143 struct tog_blame_line *line;
4144 int errcode;
4146 if (nlines != a->nlines ||
4147 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4148 return got_error(GOT_ERR_RANGE);
4150 errcode = pthread_mutex_lock(&tog_mutex);
4151 if (errcode)
4152 return got_error_set_errno(errcode, "pthread_mutex_lock");
4154 if (*a->quit) { /* user has quit the blame view */
4155 err = got_error(GOT_ERR_ITER_COMPLETED);
4156 goto done;
4159 if (lineno == -1)
4160 goto done; /* no change in this commit */
4162 line = &a->lines[lineno - 1];
4163 if (line->annotated)
4164 goto done;
4166 line->id = got_object_id_dup(id);
4167 if (line->id == NULL) {
4168 err = got_error_from_errno("got_object_id_dup");
4169 goto done;
4171 line->annotated = 1;
4172 done:
4173 errcode = pthread_mutex_unlock(&tog_mutex);
4174 if (errcode)
4175 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4176 return err;
4179 static void *
4180 blame_thread(void *arg)
4182 const struct got_error *err, *close_err;
4183 struct tog_blame_thread_args *ta = arg;
4184 struct tog_blame_cb_args *a = ta->cb_args;
4185 int errcode;
4187 err = block_signals_used_by_main_thread();
4188 if (err)
4189 return (void *)err;
4191 err = got_blame(ta->path, a->commit_id, ta->repo,
4192 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4193 if (err && err->code == GOT_ERR_CANCELLED)
4194 err = NULL;
4196 errcode = pthread_mutex_lock(&tog_mutex);
4197 if (errcode)
4198 return (void *)got_error_set_errno(errcode,
4199 "pthread_mutex_lock");
4201 close_err = got_repo_close(ta->repo);
4202 if (err == NULL)
4203 err = close_err;
4204 ta->repo = NULL;
4205 *ta->complete = 1;
4207 errcode = pthread_mutex_unlock(&tog_mutex);
4208 if (errcode && err == NULL)
4209 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4211 return (void *)err;
4214 static struct got_object_id *
4215 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4216 int first_displayed_line, int selected_line)
4218 struct tog_blame_line *line;
4220 if (nlines <= 0)
4221 return NULL;
4223 line = &lines[first_displayed_line - 1 + selected_line - 1];
4224 if (!line->annotated)
4225 return NULL;
4227 return line->id;
4230 static const struct got_error *
4231 stop_blame(struct tog_blame *blame)
4233 const struct got_error *err = NULL;
4234 int i;
4236 if (blame->thread) {
4237 int errcode;
4238 errcode = pthread_mutex_unlock(&tog_mutex);
4239 if (errcode)
4240 return got_error_set_errno(errcode,
4241 "pthread_mutex_unlock");
4242 errcode = pthread_join(blame->thread, (void **)&err);
4243 if (errcode)
4244 return got_error_set_errno(errcode, "pthread_join");
4245 errcode = pthread_mutex_lock(&tog_mutex);
4246 if (errcode)
4247 return got_error_set_errno(errcode,
4248 "pthread_mutex_lock");
4249 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4250 err = NULL;
4251 blame->thread = NULL;
4253 if (blame->thread_args.repo) {
4254 const struct got_error *close_err;
4255 close_err = got_repo_close(blame->thread_args.repo);
4256 if (err == NULL)
4257 err = close_err;
4258 blame->thread_args.repo = NULL;
4260 if (blame->f) {
4261 if (fclose(blame->f) == EOF && err == NULL)
4262 err = got_error_from_errno("fclose");
4263 blame->f = NULL;
4265 if (blame->lines) {
4266 for (i = 0; i < blame->nlines; i++)
4267 free(blame->lines[i].id);
4268 free(blame->lines);
4269 blame->lines = NULL;
4271 free(blame->cb_args.commit_id);
4272 blame->cb_args.commit_id = NULL;
4274 return err;
4277 static const struct got_error *
4278 cancel_blame_view(void *arg)
4280 const struct got_error *err = NULL;
4281 int *done = arg;
4282 int errcode;
4284 errcode = pthread_mutex_lock(&tog_mutex);
4285 if (errcode)
4286 return got_error_set_errno(errcode,
4287 "pthread_mutex_unlock");
4289 if (*done)
4290 err = got_error(GOT_ERR_CANCELLED);
4292 errcode = pthread_mutex_unlock(&tog_mutex);
4293 if (errcode)
4294 return got_error_set_errno(errcode,
4295 "pthread_mutex_lock");
4297 return err;
4300 static const struct got_error *
4301 run_blame(struct tog_view *view)
4303 struct tog_blame_view_state *s = &view->state.blame;
4304 struct tog_blame *blame = &s->blame;
4305 const struct got_error *err = NULL;
4306 struct got_blob_object *blob = NULL;
4307 struct got_repository *thread_repo = NULL;
4308 struct got_object_id *obj_id = NULL;
4309 int obj_type;
4311 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4312 s->path);
4313 if (err)
4314 return err;
4316 err = got_object_get_type(&obj_type, s->repo, obj_id);
4317 if (err)
4318 goto done;
4320 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4321 err = got_error(GOT_ERR_OBJ_TYPE);
4322 goto done;
4325 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4326 if (err)
4327 goto done;
4328 blame->f = got_opentemp();
4329 if (blame->f == NULL) {
4330 err = got_error_from_errno("got_opentemp");
4331 goto done;
4333 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4334 &blame->line_offsets, blame->f, blob);
4335 if (err)
4336 goto done;
4337 if (blame->nlines == 0) {
4338 s->blame_complete = 1;
4339 goto done;
4342 /* Don't include \n at EOF in the blame line count. */
4343 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4344 blame->nlines--;
4346 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4347 if (blame->lines == NULL) {
4348 err = got_error_from_errno("calloc");
4349 goto done;
4352 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4353 if (err)
4354 goto done;
4356 blame->cb_args.view = view;
4357 blame->cb_args.lines = blame->lines;
4358 blame->cb_args.nlines = blame->nlines;
4359 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4360 if (blame->cb_args.commit_id == NULL) {
4361 err = got_error_from_errno("got_object_id_dup");
4362 goto done;
4364 blame->cb_args.quit = &s->done;
4366 blame->thread_args.path = s->path;
4367 blame->thread_args.repo = thread_repo;
4368 blame->thread_args.cb_args = &blame->cb_args;
4369 blame->thread_args.complete = &s->blame_complete;
4370 blame->thread_args.cancel_cb = cancel_blame_view;
4371 blame->thread_args.cancel_arg = &s->done;
4372 s->blame_complete = 0;
4374 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4375 s->first_displayed_line = 1;
4376 s->last_displayed_line = view->nlines;
4377 s->selected_line = 1;
4380 done:
4381 if (blob)
4382 got_object_blob_close(blob);
4383 free(obj_id);
4384 if (err)
4385 stop_blame(blame);
4386 return err;
4389 static const struct got_error *
4390 open_blame_view(struct tog_view *view, char *path,
4391 struct got_object_id *commit_id, struct got_repository *repo)
4393 const struct got_error *err = NULL;
4394 struct tog_blame_view_state *s = &view->state.blame;
4396 STAILQ_INIT(&s->blamed_commits);
4398 s->path = strdup(path);
4399 if (s->path == NULL)
4400 return got_error_from_errno("strdup");
4402 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4403 if (err) {
4404 free(s->path);
4405 return err;
4408 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4409 s->first_displayed_line = 1;
4410 s->last_displayed_line = view->nlines;
4411 s->selected_line = 1;
4412 s->blame_complete = 0;
4413 s->repo = repo;
4414 s->commit_id = commit_id;
4415 memset(&s->blame, 0, sizeof(s->blame));
4417 STAILQ_INIT(&s->colors);
4418 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4419 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4420 get_color_value("TOG_COLOR_COMMIT"));
4421 if (err)
4422 return err;
4425 view->show = show_blame_view;
4426 view->input = input_blame_view;
4427 view->close = close_blame_view;
4428 view->search_start = search_start_blame_view;
4429 view->search_next = search_next_blame_view;
4431 return run_blame(view);
4434 static const struct got_error *
4435 close_blame_view(struct tog_view *view)
4437 const struct got_error *err = NULL;
4438 struct tog_blame_view_state *s = &view->state.blame;
4440 if (s->blame.thread)
4441 err = stop_blame(&s->blame);
4443 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4444 struct got_object_qid *blamed_commit;
4445 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4446 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4447 got_object_qid_free(blamed_commit);
4450 free(s->path);
4451 free_colors(&s->colors);
4453 return err;
4456 static const struct got_error *
4457 search_start_blame_view(struct tog_view *view)
4459 struct tog_blame_view_state *s = &view->state.blame;
4461 s->matched_line = 0;
4462 return NULL;
4465 static const struct got_error *
4466 search_next_blame_view(struct tog_view *view)
4468 struct tog_blame_view_state *s = &view->state.blame;
4469 int lineno;
4470 char *line = NULL;
4471 size_t linesize = 0;
4472 ssize_t linelen;
4474 if (!view->searching) {
4475 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4476 return NULL;
4479 if (s->matched_line) {
4480 if (view->searching == TOG_SEARCH_FORWARD)
4481 lineno = s->matched_line + 1;
4482 else
4483 lineno = s->matched_line - 1;
4484 } else {
4485 if (view->searching == TOG_SEARCH_FORWARD)
4486 lineno = 1;
4487 else
4488 lineno = s->blame.nlines;
4491 while (1) {
4492 off_t offset;
4494 if (lineno <= 0 || lineno > s->blame.nlines) {
4495 if (s->matched_line == 0) {
4496 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4497 break;
4500 if (view->searching == TOG_SEARCH_FORWARD)
4501 lineno = 1;
4502 else
4503 lineno = s->blame.nlines;
4506 offset = s->blame.line_offsets[lineno - 1];
4507 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4508 free(line);
4509 return got_error_from_errno("fseeko");
4511 linelen = getline(&line, &linesize, s->blame.f);
4512 if (linelen != -1 &&
4513 match_line(line, &view->regex, 1, &view->regmatch)) {
4514 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4515 s->matched_line = lineno;
4516 break;
4518 if (view->searching == TOG_SEARCH_FORWARD)
4519 lineno++;
4520 else
4521 lineno--;
4523 free(line);
4525 if (s->matched_line) {
4526 s->first_displayed_line = s->matched_line;
4527 s->selected_line = 1;
4530 return NULL;
4533 static const struct got_error *
4534 show_blame_view(struct tog_view *view)
4536 const struct got_error *err = NULL;
4537 struct tog_blame_view_state *s = &view->state.blame;
4538 int errcode;
4540 if (s->blame.thread == NULL && !s->blame_complete) {
4541 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4542 &s->blame.thread_args);
4543 if (errcode)
4544 return got_error_set_errno(errcode, "pthread_create");
4546 halfdelay(1); /* fast refresh while annotating */
4549 if (s->blame_complete)
4550 halfdelay(10); /* disable fast refresh */
4552 err = draw_blame(view);
4554 view_vborder(view);
4555 return err;
4558 static const struct got_error *
4559 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4561 const struct got_error *err = NULL, *thread_err = NULL;
4562 struct tog_view *diff_view;
4563 struct tog_blame_view_state *s = &view->state.blame;
4564 int begin_x = 0;
4566 switch (ch) {
4567 case 'q':
4568 s->done = 1;
4569 break;
4570 case 'g':
4571 case KEY_HOME:
4572 s->selected_line = 1;
4573 s->first_displayed_line = 1;
4574 break;
4575 case 'G':
4576 case KEY_END:
4577 if (s->blame.nlines < view->nlines - 2) {
4578 s->selected_line = s->blame.nlines;
4579 s->first_displayed_line = 1;
4580 } else {
4581 s->selected_line = view->nlines - 2;
4582 s->first_displayed_line = s->blame.nlines -
4583 (view->nlines - 3);
4585 break;
4586 case 'k':
4587 case KEY_UP:
4588 case CTRL('p'):
4589 if (s->selected_line > 1)
4590 s->selected_line--;
4591 else if (s->selected_line == 1 &&
4592 s->first_displayed_line > 1)
4593 s->first_displayed_line--;
4594 break;
4595 case KEY_PPAGE:
4596 case CTRL('b'):
4597 if (s->first_displayed_line == 1) {
4598 s->selected_line = 1;
4599 break;
4601 if (s->first_displayed_line > view->nlines - 2)
4602 s->first_displayed_line -=
4603 (view->nlines - 2);
4604 else
4605 s->first_displayed_line = 1;
4606 break;
4607 case 'j':
4608 case KEY_DOWN:
4609 case CTRL('n'):
4610 if (s->selected_line < view->nlines - 2 &&
4611 s->first_displayed_line +
4612 s->selected_line <= s->blame.nlines)
4613 s->selected_line++;
4614 else if (s->last_displayed_line <
4615 s->blame.nlines)
4616 s->first_displayed_line++;
4617 break;
4618 case 'b':
4619 case 'p': {
4620 struct got_object_id *id = NULL;
4621 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4622 s->first_displayed_line, s->selected_line);
4623 if (id == NULL)
4624 break;
4625 if (ch == 'p') {
4626 struct got_commit_object *commit;
4627 struct got_object_qid *pid;
4628 struct got_object_id *blob_id = NULL;
4629 int obj_type;
4630 err = got_object_open_as_commit(&commit,
4631 s->repo, id);
4632 if (err)
4633 break;
4634 pid = STAILQ_FIRST(
4635 got_object_commit_get_parent_ids(commit));
4636 if (pid == NULL) {
4637 got_object_commit_close(commit);
4638 break;
4640 /* Check if path history ends here. */
4641 err = got_object_id_by_path(&blob_id, s->repo,
4642 pid->id, s->path);
4643 if (err) {
4644 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4645 err = NULL;
4646 got_object_commit_close(commit);
4647 break;
4649 err = got_object_get_type(&obj_type, s->repo,
4650 blob_id);
4651 free(blob_id);
4652 /* Can't blame non-blob type objects. */
4653 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4654 got_object_commit_close(commit);
4655 break;
4657 err = got_object_qid_alloc(&s->blamed_commit,
4658 pid->id);
4659 got_object_commit_close(commit);
4660 } else {
4661 if (got_object_id_cmp(id,
4662 s->blamed_commit->id) == 0)
4663 break;
4664 err = got_object_qid_alloc(&s->blamed_commit,
4665 id);
4667 if (err)
4668 break;
4669 s->done = 1;
4670 thread_err = stop_blame(&s->blame);
4671 s->done = 0;
4672 if (thread_err)
4673 break;
4674 STAILQ_INSERT_HEAD(&s->blamed_commits,
4675 s->blamed_commit, entry);
4676 err = run_blame(view);
4677 if (err)
4678 break;
4679 break;
4681 case 'B': {
4682 struct got_object_qid *first;
4683 first = STAILQ_FIRST(&s->blamed_commits);
4684 if (!got_object_id_cmp(first->id, s->commit_id))
4685 break;
4686 s->done = 1;
4687 thread_err = stop_blame(&s->blame);
4688 s->done = 0;
4689 if (thread_err)
4690 break;
4691 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4692 got_object_qid_free(s->blamed_commit);
4693 s->blamed_commit =
4694 STAILQ_FIRST(&s->blamed_commits);
4695 err = run_blame(view);
4696 if (err)
4697 break;
4698 break;
4700 case KEY_ENTER:
4701 case '\r': {
4702 struct got_object_id *id = NULL;
4703 struct got_object_qid *pid;
4704 struct got_commit_object *commit = NULL;
4705 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4706 s->first_displayed_line, s->selected_line);
4707 if (id == NULL)
4708 break;
4709 err = got_object_open_as_commit(&commit, s->repo, id);
4710 if (err)
4711 break;
4712 pid = STAILQ_FIRST(
4713 got_object_commit_get_parent_ids(commit));
4714 if (view_is_parent_view(view))
4715 begin_x = view_split_begin_x(view->begin_x);
4716 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4717 if (diff_view == NULL) {
4718 got_object_commit_close(commit);
4719 err = got_error_from_errno("view_open");
4720 break;
4722 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4723 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4724 got_object_commit_close(commit);
4725 if (err) {
4726 view_close(diff_view);
4727 break;
4729 view->focussed = 0;
4730 diff_view->focussed = 1;
4731 if (view_is_parent_view(view)) {
4732 err = view_close_child(view);
4733 if (err)
4734 break;
4735 view_set_child(view, diff_view);
4736 view->focus_child = 1;
4737 } else
4738 *new_view = diff_view;
4739 if (err)
4740 break;
4741 break;
4743 case KEY_NPAGE:
4744 case CTRL('f'):
4745 case ' ':
4746 if (s->last_displayed_line >= s->blame.nlines &&
4747 s->selected_line >= MIN(s->blame.nlines,
4748 view->nlines - 2)) {
4749 break;
4751 if (s->last_displayed_line >= s->blame.nlines &&
4752 s->selected_line < view->nlines - 2) {
4753 s->selected_line = MIN(s->blame.nlines,
4754 view->nlines - 2);
4755 break;
4757 if (s->last_displayed_line + view->nlines - 2
4758 <= s->blame.nlines)
4759 s->first_displayed_line +=
4760 view->nlines - 2;
4761 else
4762 s->first_displayed_line =
4763 s->blame.nlines -
4764 (view->nlines - 3);
4765 break;
4766 case KEY_RESIZE:
4767 if (s->selected_line > view->nlines - 2) {
4768 s->selected_line = MIN(s->blame.nlines,
4769 view->nlines - 2);
4771 break;
4772 default:
4773 break;
4775 return thread_err ? thread_err : err;
4778 static const struct got_error *
4779 cmd_blame(int argc, char *argv[])
4781 const struct got_error *error;
4782 struct got_repository *repo = NULL;
4783 struct got_worktree *worktree = NULL;
4784 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4785 char *link_target = NULL;
4786 struct got_object_id *commit_id = NULL;
4787 char *commit_id_str = NULL;
4788 int ch;
4789 struct tog_view *view;
4791 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4792 switch (ch) {
4793 case 'c':
4794 commit_id_str = optarg;
4795 break;
4796 case 'r':
4797 repo_path = realpath(optarg, NULL);
4798 if (repo_path == NULL)
4799 return got_error_from_errno2("realpath",
4800 optarg);
4801 break;
4802 default:
4803 usage_blame();
4804 /* NOTREACHED */
4808 argc -= optind;
4809 argv += optind;
4811 if (argc != 1)
4812 usage_blame();
4814 if (repo_path == NULL) {
4815 cwd = getcwd(NULL, 0);
4816 if (cwd == NULL)
4817 return got_error_from_errno("getcwd");
4818 error = got_worktree_open(&worktree, cwd);
4819 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4820 goto done;
4821 if (worktree)
4822 repo_path =
4823 strdup(got_worktree_get_repo_path(worktree));
4824 else
4825 repo_path = strdup(cwd);
4826 if (repo_path == NULL) {
4827 error = got_error_from_errno("strdup");
4828 goto done;
4832 error = got_repo_open(&repo, repo_path, NULL);
4833 if (error != NULL)
4834 goto done;
4836 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4837 worktree);
4838 if (error)
4839 goto done;
4841 init_curses();
4843 error = apply_unveil(got_repo_get_path(repo), NULL);
4844 if (error)
4845 goto done;
4847 error = tog_load_refs(repo, 0);
4848 if (error)
4849 goto done;
4851 if (commit_id_str == NULL) {
4852 struct got_reference *head_ref;
4853 error = got_ref_open(&head_ref, repo, worktree ?
4854 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4855 if (error != NULL)
4856 goto done;
4857 error = got_ref_resolve(&commit_id, repo, head_ref);
4858 got_ref_close(head_ref);
4859 } else {
4860 error = got_repo_match_object_id(&commit_id, NULL,
4861 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4863 if (error != NULL)
4864 goto done;
4866 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4867 if (view == NULL) {
4868 error = got_error_from_errno("view_open");
4869 goto done;
4872 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4873 commit_id, repo);
4874 if (error)
4875 goto done;
4877 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4878 commit_id, repo);
4879 if (error)
4880 goto done;
4881 if (worktree) {
4882 /* Release work tree lock. */
4883 got_worktree_close(worktree);
4884 worktree = NULL;
4886 error = view_loop(view);
4887 done:
4888 free(repo_path);
4889 free(in_repo_path);
4890 free(link_target);
4891 free(cwd);
4892 free(commit_id);
4893 if (worktree)
4894 got_worktree_close(worktree);
4895 if (repo) {
4896 const struct got_error *close_err = got_repo_close(repo);
4897 if (error == NULL)
4898 error = close_err;
4900 tog_free_refs();
4901 return error;
4904 static const struct got_error *
4905 draw_tree_entries(struct tog_view *view, const char *parent_path)
4907 struct tog_tree_view_state *s = &view->state.tree;
4908 const struct got_error *err = NULL;
4909 struct got_tree_entry *te;
4910 wchar_t *wline;
4911 struct tog_color *tc;
4912 int width, n, i, nentries;
4913 int limit = view->nlines;
4915 s->ndisplayed = 0;
4917 werase(view->window);
4919 if (limit == 0)
4920 return NULL;
4922 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4923 if (err)
4924 return err;
4925 if (view_needs_focus_indication(view))
4926 wstandout(view->window);
4927 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4928 if (tc)
4929 wattr_on(view->window,
4930 COLOR_PAIR(tc->colorpair), NULL);
4931 waddwstr(view->window, wline);
4932 if (tc)
4933 wattr_off(view->window,
4934 COLOR_PAIR(tc->colorpair), NULL);
4935 if (view_needs_focus_indication(view))
4936 wstandend(view->window);
4937 free(wline);
4938 wline = NULL;
4939 if (width < view->ncols - 1)
4940 waddch(view->window, '\n');
4941 if (--limit <= 0)
4942 return NULL;
4943 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4944 if (err)
4945 return err;
4946 waddwstr(view->window, wline);
4947 free(wline);
4948 wline = NULL;
4949 if (width < view->ncols - 1)
4950 waddch(view->window, '\n');
4951 if (--limit <= 0)
4952 return NULL;
4953 waddch(view->window, '\n');
4954 if (--limit <= 0)
4955 return NULL;
4957 if (s->first_displayed_entry == NULL) {
4958 te = got_object_tree_get_first_entry(s->tree);
4959 if (s->selected == 0) {
4960 if (view->focussed)
4961 wstandout(view->window);
4962 s->selected_entry = NULL;
4964 waddstr(view->window, " ..\n"); /* parent directory */
4965 if (s->selected == 0 && view->focussed)
4966 wstandend(view->window);
4967 s->ndisplayed++;
4968 if (--limit <= 0)
4969 return NULL;
4970 n = 1;
4971 } else {
4972 n = 0;
4973 te = s->first_displayed_entry;
4976 nentries = got_object_tree_get_nentries(s->tree);
4977 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4978 char *line = NULL, *id_str = NULL, *link_target = NULL;
4979 const char *modestr = "";
4980 mode_t mode;
4982 te = got_object_tree_get_entry(s->tree, i);
4983 mode = got_tree_entry_get_mode(te);
4985 if (s->show_ids) {
4986 err = got_object_id_str(&id_str,
4987 got_tree_entry_get_id(te));
4988 if (err)
4989 return got_error_from_errno(
4990 "got_object_id_str");
4992 if (got_object_tree_entry_is_submodule(te))
4993 modestr = "$";
4994 else if (S_ISLNK(mode)) {
4995 int i;
4997 err = got_tree_entry_get_symlink_target(&link_target,
4998 te, s->repo);
4999 if (err) {
5000 free(id_str);
5001 return err;
5003 for (i = 0; i < strlen(link_target); i++) {
5004 if (!isprint((unsigned char)link_target[i]))
5005 link_target[i] = '?';
5007 modestr = "@";
5009 else if (S_ISDIR(mode))
5010 modestr = "/";
5011 else if (mode & S_IXUSR)
5012 modestr = "*";
5013 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5014 got_tree_entry_get_name(te), modestr,
5015 link_target ? " -> ": "",
5016 link_target ? link_target : "") == -1) {
5017 free(id_str);
5018 free(link_target);
5019 return got_error_from_errno("asprintf");
5021 free(id_str);
5022 free(link_target);
5023 err = format_line(&wline, &width, line, view->ncols, 0);
5024 if (err) {
5025 free(line);
5026 break;
5028 if (n == s->selected) {
5029 if (view->focussed)
5030 wstandout(view->window);
5031 s->selected_entry = te;
5033 tc = match_color(&s->colors, line);
5034 if (tc)
5035 wattr_on(view->window,
5036 COLOR_PAIR(tc->colorpair), NULL);
5037 waddwstr(view->window, wline);
5038 if (tc)
5039 wattr_off(view->window,
5040 COLOR_PAIR(tc->colorpair), NULL);
5041 if (width < view->ncols - 1)
5042 waddch(view->window, '\n');
5043 if (n == s->selected && view->focussed)
5044 wstandend(view->window);
5045 free(line);
5046 free(wline);
5047 wline = NULL;
5048 n++;
5049 s->ndisplayed++;
5050 s->last_displayed_entry = te;
5051 if (--limit <= 0)
5052 break;
5055 return err;
5058 static void
5059 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5061 struct got_tree_entry *te;
5062 int isroot = s->tree == s->root;
5063 int i = 0;
5065 if (s->first_displayed_entry == NULL)
5066 return;
5068 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5069 while (i++ < maxscroll) {
5070 if (te == NULL) {
5071 if (!isroot)
5072 s->first_displayed_entry = NULL;
5073 break;
5075 s->first_displayed_entry = te;
5076 te = got_tree_entry_get_prev(s->tree, te);
5080 static void
5081 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5083 struct got_tree_entry *next, *last;
5084 int n = 0;
5086 if (s->first_displayed_entry)
5087 next = got_tree_entry_get_next(s->tree,
5088 s->first_displayed_entry);
5089 else
5090 next = got_object_tree_get_first_entry(s->tree);
5092 last = s->last_displayed_entry;
5093 while (next && last && n++ < maxscroll) {
5094 last = got_tree_entry_get_next(s->tree, last);
5095 if (last) {
5096 s->first_displayed_entry = next;
5097 next = got_tree_entry_get_next(s->tree, next);
5102 static const struct got_error *
5103 tree_entry_path(char **path, struct tog_parent_trees *parents,
5104 struct got_tree_entry *te)
5106 const struct got_error *err = NULL;
5107 struct tog_parent_tree *pt;
5108 size_t len = 2; /* for leading slash and NUL */
5110 TAILQ_FOREACH(pt, parents, entry)
5111 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5112 + 1 /* slash */;
5113 if (te)
5114 len += strlen(got_tree_entry_get_name(te));
5116 *path = calloc(1, len);
5117 if (path == NULL)
5118 return got_error_from_errno("calloc");
5120 (*path)[0] = '/';
5121 pt = TAILQ_LAST(parents, tog_parent_trees);
5122 while (pt) {
5123 const char *name = got_tree_entry_get_name(pt->selected_entry);
5124 if (strlcat(*path, name, len) >= len) {
5125 err = got_error(GOT_ERR_NO_SPACE);
5126 goto done;
5128 if (strlcat(*path, "/", len) >= len) {
5129 err = got_error(GOT_ERR_NO_SPACE);
5130 goto done;
5132 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5134 if (te) {
5135 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5136 err = got_error(GOT_ERR_NO_SPACE);
5137 goto done;
5140 done:
5141 if (err) {
5142 free(*path);
5143 *path = NULL;
5145 return err;
5148 static const struct got_error *
5149 blame_tree_entry(struct tog_view **new_view, int begin_x,
5150 struct got_tree_entry *te, struct tog_parent_trees *parents,
5151 struct got_object_id *commit_id, struct got_repository *repo)
5153 const struct got_error *err = NULL;
5154 char *path;
5155 struct tog_view *blame_view;
5157 *new_view = NULL;
5159 err = tree_entry_path(&path, parents, te);
5160 if (err)
5161 return err;
5163 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5164 if (blame_view == NULL) {
5165 err = got_error_from_errno("view_open");
5166 goto done;
5169 err = open_blame_view(blame_view, path, commit_id, repo);
5170 if (err) {
5171 if (err->code == GOT_ERR_CANCELLED)
5172 err = NULL;
5173 view_close(blame_view);
5174 } else
5175 *new_view = blame_view;
5176 done:
5177 free(path);
5178 return err;
5181 static const struct got_error *
5182 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5183 struct tog_tree_view_state *s)
5185 struct tog_view *log_view;
5186 const struct got_error *err = NULL;
5187 char *path;
5189 *new_view = NULL;
5191 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5192 if (log_view == NULL)
5193 return got_error_from_errno("view_open");
5195 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5196 if (err)
5197 return err;
5199 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5200 path, 0);
5201 if (err)
5202 view_close(log_view);
5203 else
5204 *new_view = log_view;
5205 free(path);
5206 return err;
5209 static const struct got_error *
5210 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5211 const char *head_ref_name, struct got_repository *repo)
5213 const struct got_error *err = NULL;
5214 char *commit_id_str = NULL;
5215 struct tog_tree_view_state *s = &view->state.tree;
5216 struct got_commit_object *commit = NULL;
5218 TAILQ_INIT(&s->parents);
5219 STAILQ_INIT(&s->colors);
5221 s->commit_id = got_object_id_dup(commit_id);
5222 if (s->commit_id == NULL)
5223 return got_error_from_errno("got_object_id_dup");
5225 err = got_object_open_as_commit(&commit, repo, commit_id);
5226 if (err)
5227 goto done;
5230 * The root is opened here and will be closed when the view is closed.
5231 * Any visited subtrees and their path-wise parents are opened and
5232 * closed on demand.
5234 err = got_object_open_as_tree(&s->root, repo,
5235 got_object_commit_get_tree_id(commit));
5236 if (err)
5237 goto done;
5238 s->tree = s->root;
5240 err = got_object_id_str(&commit_id_str, commit_id);
5241 if (err != NULL)
5242 goto done;
5244 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5245 err = got_error_from_errno("asprintf");
5246 goto done;
5249 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5250 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5251 if (head_ref_name) {
5252 s->head_ref_name = strdup(head_ref_name);
5253 if (s->head_ref_name == NULL) {
5254 err = got_error_from_errno("strdup");
5255 goto done;
5258 s->repo = repo;
5260 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5261 err = add_color(&s->colors, "\\$$",
5262 TOG_COLOR_TREE_SUBMODULE,
5263 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5264 if (err)
5265 goto done;
5266 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5267 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5268 if (err)
5269 goto done;
5270 err = add_color(&s->colors, "/$",
5271 TOG_COLOR_TREE_DIRECTORY,
5272 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5273 if (err)
5274 goto done;
5276 err = add_color(&s->colors, "\\*$",
5277 TOG_COLOR_TREE_EXECUTABLE,
5278 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5279 if (err)
5280 goto done;
5282 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5283 get_color_value("TOG_COLOR_COMMIT"));
5284 if (err)
5285 goto done;
5288 view->show = show_tree_view;
5289 view->input = input_tree_view;
5290 view->close = close_tree_view;
5291 view->search_start = search_start_tree_view;
5292 view->search_next = search_next_tree_view;
5293 done:
5294 free(commit_id_str);
5295 if (commit)
5296 got_object_commit_close(commit);
5297 if (err)
5298 close_tree_view(view);
5299 return err;
5302 static const struct got_error *
5303 close_tree_view(struct tog_view *view)
5305 struct tog_tree_view_state *s = &view->state.tree;
5307 free_colors(&s->colors);
5308 free(s->tree_label);
5309 s->tree_label = NULL;
5310 free(s->commit_id);
5311 s->commit_id = NULL;
5312 free(s->head_ref_name);
5313 s->head_ref_name = NULL;
5314 while (!TAILQ_EMPTY(&s->parents)) {
5315 struct tog_parent_tree *parent;
5316 parent = TAILQ_FIRST(&s->parents);
5317 TAILQ_REMOVE(&s->parents, parent, entry);
5318 if (parent->tree != s->root)
5319 got_object_tree_close(parent->tree);
5320 free(parent);
5323 if (s->tree != NULL && s->tree != s->root)
5324 got_object_tree_close(s->tree);
5325 if (s->root)
5326 got_object_tree_close(s->root);
5327 return NULL;
5330 static const struct got_error *
5331 search_start_tree_view(struct tog_view *view)
5333 struct tog_tree_view_state *s = &view->state.tree;
5335 s->matched_entry = NULL;
5336 return NULL;
5339 static int
5340 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5342 regmatch_t regmatch;
5344 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5345 0) == 0;
5348 static const struct got_error *
5349 search_next_tree_view(struct tog_view *view)
5351 struct tog_tree_view_state *s = &view->state.tree;
5352 struct got_tree_entry *te = NULL;
5354 if (!view->searching) {
5355 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5356 return NULL;
5359 if (s->matched_entry) {
5360 if (view->searching == TOG_SEARCH_FORWARD) {
5361 if (s->selected_entry)
5362 te = got_tree_entry_get_next(s->tree,
5363 s->selected_entry);
5364 else
5365 te = got_object_tree_get_first_entry(s->tree);
5366 } else {
5367 if (s->selected_entry == NULL)
5368 te = got_object_tree_get_last_entry(s->tree);
5369 else
5370 te = got_tree_entry_get_prev(s->tree,
5371 s->selected_entry);
5373 } else {
5374 if (view->searching == TOG_SEARCH_FORWARD)
5375 te = got_object_tree_get_first_entry(s->tree);
5376 else
5377 te = got_object_tree_get_last_entry(s->tree);
5380 while (1) {
5381 if (te == NULL) {
5382 if (s->matched_entry == NULL) {
5383 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5384 return NULL;
5386 if (view->searching == TOG_SEARCH_FORWARD)
5387 te = got_object_tree_get_first_entry(s->tree);
5388 else
5389 te = got_object_tree_get_last_entry(s->tree);
5392 if (match_tree_entry(te, &view->regex)) {
5393 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5394 s->matched_entry = te;
5395 break;
5398 if (view->searching == TOG_SEARCH_FORWARD)
5399 te = got_tree_entry_get_next(s->tree, te);
5400 else
5401 te = got_tree_entry_get_prev(s->tree, te);
5404 if (s->matched_entry) {
5405 s->first_displayed_entry = s->matched_entry;
5406 s->selected = 0;
5409 return NULL;
5412 static const struct got_error *
5413 show_tree_view(struct tog_view *view)
5415 const struct got_error *err = NULL;
5416 struct tog_tree_view_state *s = &view->state.tree;
5417 char *parent_path;
5419 err = tree_entry_path(&parent_path, &s->parents, NULL);
5420 if (err)
5421 return err;
5423 err = draw_tree_entries(view, parent_path);
5424 free(parent_path);
5426 view_vborder(view);
5427 return err;
5430 static const struct got_error *
5431 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5433 const struct got_error *err = NULL;
5434 struct tog_tree_view_state *s = &view->state.tree;
5435 struct tog_view *log_view, *ref_view;
5436 struct got_tree_entry *te;
5437 int begin_x = 0, n;
5439 switch (ch) {
5440 case 'i':
5441 s->show_ids = !s->show_ids;
5442 break;
5443 case 'l':
5444 if (!s->selected_entry)
5445 break;
5446 if (view_is_parent_view(view))
5447 begin_x = view_split_begin_x(view->begin_x);
5448 err = log_selected_tree_entry(&log_view, begin_x, s);
5449 view->focussed = 0;
5450 log_view->focussed = 1;
5451 if (view_is_parent_view(view)) {
5452 err = view_close_child(view);
5453 if (err)
5454 return err;
5455 view_set_child(view, log_view);
5456 view->focus_child = 1;
5457 } else
5458 *new_view = log_view;
5459 break;
5460 case 'r':
5461 if (view_is_parent_view(view))
5462 begin_x = view_split_begin_x(view->begin_x);
5463 ref_view = view_open(view->nlines, view->ncols,
5464 view->begin_y, begin_x, TOG_VIEW_REF);
5465 if (ref_view == NULL)
5466 return got_error_from_errno("view_open");
5467 err = open_ref_view(ref_view, s->repo);
5468 if (err) {
5469 view_close(ref_view);
5470 return err;
5472 view->focussed = 0;
5473 ref_view->focussed = 1;
5474 if (view_is_parent_view(view)) {
5475 err = view_close_child(view);
5476 if (err)
5477 return err;
5478 view_set_child(view, ref_view);
5479 view->focus_child = 1;
5480 } else
5481 *new_view = ref_view;
5482 break;
5483 case 'g':
5484 case KEY_HOME:
5485 s->selected = 0;
5486 if (s->tree == s->root)
5487 s->first_displayed_entry =
5488 got_object_tree_get_first_entry(s->tree);
5489 else
5490 s->first_displayed_entry = NULL;
5491 break;
5492 case 'G':
5493 case KEY_END:
5494 s->selected = 0;
5495 te = got_object_tree_get_last_entry(s->tree);
5496 for (n = 0; n < view->nlines - 3; n++) {
5497 if (te == NULL) {
5498 if(s->tree != s->root) {
5499 s->first_displayed_entry = NULL;
5500 n++;
5502 break;
5504 s->first_displayed_entry = te;
5505 te = got_tree_entry_get_prev(s->tree, te);
5507 if (n > 0)
5508 s->selected = n - 1;
5509 break;
5510 case 'k':
5511 case KEY_UP:
5512 case CTRL('p'):
5513 if (s->selected > 0) {
5514 s->selected--;
5515 break;
5517 tree_scroll_up(s, 1);
5518 break;
5519 case KEY_PPAGE:
5520 case CTRL('b'):
5521 if (s->tree == s->root) {
5522 if (got_object_tree_get_first_entry(s->tree) ==
5523 s->first_displayed_entry)
5524 s->selected = 0;
5525 } else {
5526 if (s->first_displayed_entry == NULL)
5527 s->selected = 0;
5529 tree_scroll_up(s, MAX(0, view->nlines - 3));
5530 break;
5531 case 'j':
5532 case KEY_DOWN:
5533 case CTRL('n'):
5534 if (s->selected < s->ndisplayed - 1) {
5535 s->selected++;
5536 break;
5538 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5539 == NULL)
5540 /* can't scroll any further */
5541 break;
5542 tree_scroll_down(s, 1);
5543 break;
5544 case KEY_NPAGE:
5545 case CTRL('f'):
5546 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5547 == NULL) {
5548 /* can't scroll any further; move cursor down */
5549 if (s->selected < s->ndisplayed - 1)
5550 s->selected = s->ndisplayed - 1;
5551 break;
5553 tree_scroll_down(s, view->nlines - 3);
5554 break;
5555 case KEY_ENTER:
5556 case '\r':
5557 case KEY_BACKSPACE:
5558 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5559 struct tog_parent_tree *parent;
5560 /* user selected '..' */
5561 if (s->tree == s->root)
5562 break;
5563 parent = TAILQ_FIRST(&s->parents);
5564 TAILQ_REMOVE(&s->parents, parent,
5565 entry);
5566 got_object_tree_close(s->tree);
5567 s->tree = parent->tree;
5568 s->first_displayed_entry =
5569 parent->first_displayed_entry;
5570 s->selected_entry =
5571 parent->selected_entry;
5572 s->selected = parent->selected;
5573 free(parent);
5574 } else if (S_ISDIR(got_tree_entry_get_mode(
5575 s->selected_entry))) {
5576 struct got_tree_object *subtree;
5577 err = got_object_open_as_tree(&subtree, s->repo,
5578 got_tree_entry_get_id(s->selected_entry));
5579 if (err)
5580 break;
5581 err = tree_view_visit_subtree(s, subtree);
5582 if (err) {
5583 got_object_tree_close(subtree);
5584 break;
5586 } else if (S_ISREG(got_tree_entry_get_mode(
5587 s->selected_entry))) {
5588 struct tog_view *blame_view;
5589 int begin_x = view_is_parent_view(view) ?
5590 view_split_begin_x(view->begin_x) : 0;
5592 err = blame_tree_entry(&blame_view, begin_x,
5593 s->selected_entry, &s->parents,
5594 s->commit_id, s->repo);
5595 if (err)
5596 break;
5597 view->focussed = 0;
5598 blame_view->focussed = 1;
5599 if (view_is_parent_view(view)) {
5600 err = view_close_child(view);
5601 if (err)
5602 return err;
5603 view_set_child(view, blame_view);
5604 view->focus_child = 1;
5605 } else
5606 *new_view = blame_view;
5608 break;
5609 case KEY_RESIZE:
5610 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5611 s->selected = view->nlines - 4;
5612 break;
5613 default:
5614 break;
5617 return err;
5620 __dead static void
5621 usage_tree(void)
5623 endwin();
5624 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5625 getprogname());
5626 exit(1);
5629 static const struct got_error *
5630 cmd_tree(int argc, char *argv[])
5632 const struct got_error *error;
5633 struct got_repository *repo = NULL;
5634 struct got_worktree *worktree = NULL;
5635 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5636 struct got_object_id *commit_id = NULL;
5637 const char *commit_id_arg = NULL;
5638 char *label = NULL;
5639 struct got_reference *ref = NULL;
5640 const char *head_ref_name = NULL;
5641 int ch;
5642 struct tog_view *view;
5644 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5645 switch (ch) {
5646 case 'c':
5647 commit_id_arg = optarg;
5648 break;
5649 case 'r':
5650 repo_path = realpath(optarg, NULL);
5651 if (repo_path == NULL)
5652 return got_error_from_errno2("realpath",
5653 optarg);
5654 break;
5655 default:
5656 usage_tree();
5657 /* NOTREACHED */
5661 argc -= optind;
5662 argv += optind;
5664 if (argc > 1)
5665 usage_tree();
5667 if (repo_path == NULL) {
5668 cwd = getcwd(NULL, 0);
5669 if (cwd == NULL)
5670 return got_error_from_errno("getcwd");
5671 error = got_worktree_open(&worktree, cwd);
5672 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5673 goto done;
5674 if (worktree)
5675 repo_path =
5676 strdup(got_worktree_get_repo_path(worktree));
5677 else
5678 repo_path = strdup(cwd);
5679 if (repo_path == NULL) {
5680 error = got_error_from_errno("strdup");
5681 goto done;
5685 error = got_repo_open(&repo, repo_path, NULL);
5686 if (error != NULL)
5687 goto done;
5689 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5690 repo, worktree);
5691 if (error)
5692 goto done;
5694 init_curses();
5696 error = apply_unveil(got_repo_get_path(repo), NULL);
5697 if (error)
5698 goto done;
5700 error = tog_load_refs(repo, 0);
5701 if (error)
5702 goto done;
5704 if (commit_id_arg == NULL) {
5705 error = got_repo_match_object_id(&commit_id, &label,
5706 worktree ? got_worktree_get_head_ref_name(worktree) :
5707 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5708 if (error)
5709 goto done;
5710 head_ref_name = label;
5711 } else {
5712 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5713 if (error == NULL)
5714 head_ref_name = got_ref_get_name(ref);
5715 else if (error->code != GOT_ERR_NOT_REF)
5716 goto done;
5717 error = got_repo_match_object_id(&commit_id, NULL,
5718 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5719 if (error)
5720 goto done;
5723 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5724 if (view == NULL) {
5725 error = got_error_from_errno("view_open");
5726 goto done;
5728 error = open_tree_view(view, commit_id, head_ref_name, repo);
5729 if (error)
5730 goto done;
5731 if (!got_path_is_root_dir(in_repo_path)) {
5732 error = tree_view_walk_path(&view->state.tree, commit_id,
5733 in_repo_path);
5734 if (error)
5735 goto done;
5738 if (worktree) {
5739 /* Release work tree lock. */
5740 got_worktree_close(worktree);
5741 worktree = NULL;
5743 error = view_loop(view);
5744 done:
5745 free(repo_path);
5746 free(cwd);
5747 free(commit_id);
5748 free(label);
5749 if (ref)
5750 got_ref_close(ref);
5751 if (repo) {
5752 const struct got_error *close_err = got_repo_close(repo);
5753 if (error == NULL)
5754 error = close_err;
5756 tog_free_refs();
5757 return error;
5760 static const struct got_error *
5761 ref_view_load_refs(struct tog_ref_view_state *s)
5763 struct got_reflist_entry *sre;
5764 struct tog_reflist_entry *re;
5766 s->nrefs = 0;
5767 TAILQ_FOREACH(sre, &tog_refs, entry) {
5768 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5769 continue;
5771 re = malloc(sizeof(*re));
5772 if (re == NULL)
5773 return got_error_from_errno("malloc");
5775 re->ref = got_ref_dup(sre->ref);
5776 if (re->ref == NULL)
5777 return got_error_from_errno("got_ref_dup");
5778 re->idx = s->nrefs++;
5779 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5782 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5783 return NULL;
5786 void
5787 ref_view_free_refs(struct tog_ref_view_state *s)
5789 struct tog_reflist_entry *re;
5791 while (!TAILQ_EMPTY(&s->refs)) {
5792 re = TAILQ_FIRST(&s->refs);
5793 TAILQ_REMOVE(&s->refs, re, entry);
5794 got_ref_close(re->ref);
5795 free(re);
5799 static const struct got_error *
5800 open_ref_view(struct tog_view *view, struct got_repository *repo)
5802 const struct got_error *err = NULL;
5803 struct tog_ref_view_state *s = &view->state.ref;
5805 s->selected_entry = 0;
5806 s->repo = repo;
5808 TAILQ_INIT(&s->refs);
5809 STAILQ_INIT(&s->colors);
5811 err = ref_view_load_refs(s);
5812 if (err)
5813 return err;
5815 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5816 err = add_color(&s->colors, "^refs/heads/",
5817 TOG_COLOR_REFS_HEADS,
5818 get_color_value("TOG_COLOR_REFS_HEADS"));
5819 if (err)
5820 goto done;
5822 err = add_color(&s->colors, "^refs/tags/",
5823 TOG_COLOR_REFS_TAGS,
5824 get_color_value("TOG_COLOR_REFS_TAGS"));
5825 if (err)
5826 goto done;
5828 err = add_color(&s->colors, "^refs/remotes/",
5829 TOG_COLOR_REFS_REMOTES,
5830 get_color_value("TOG_COLOR_REFS_REMOTES"));
5831 if (err)
5832 goto done;
5835 view->show = show_ref_view;
5836 view->input = input_ref_view;
5837 view->close = close_ref_view;
5838 view->search_start = search_start_ref_view;
5839 view->search_next = search_next_ref_view;
5840 done:
5841 if (err)
5842 free_colors(&s->colors);
5843 return err;
5846 static const struct got_error *
5847 close_ref_view(struct tog_view *view)
5849 struct tog_ref_view_state *s = &view->state.ref;
5851 ref_view_free_refs(s);
5852 free_colors(&s->colors);
5854 return NULL;
5857 static const struct got_error *
5858 resolve_reflist_entry(struct got_object_id **commit_id,
5859 struct tog_reflist_entry *re, struct got_repository *repo)
5861 const struct got_error *err = NULL;
5862 struct got_object_id *obj_id;
5863 struct got_tag_object *tag = NULL;
5864 int obj_type;
5866 *commit_id = NULL;
5868 err = got_ref_resolve(&obj_id, repo, re->ref);
5869 if (err)
5870 return err;
5872 err = got_object_get_type(&obj_type, repo, obj_id);
5873 if (err)
5874 goto done;
5876 switch (obj_type) {
5877 case GOT_OBJ_TYPE_COMMIT:
5878 *commit_id = obj_id;
5879 break;
5880 case GOT_OBJ_TYPE_TAG:
5881 err = got_object_open_as_tag(&tag, repo, obj_id);
5882 if (err)
5883 goto done;
5884 free(obj_id);
5885 err = got_object_get_type(&obj_type, repo,
5886 got_object_tag_get_object_id(tag));
5887 if (err)
5888 goto done;
5889 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5890 err = got_error(GOT_ERR_OBJ_TYPE);
5891 goto done;
5893 *commit_id = got_object_id_dup(
5894 got_object_tag_get_object_id(tag));
5895 if (*commit_id == NULL) {
5896 err = got_error_from_errno("got_object_id_dup");
5897 goto done;
5899 break;
5900 default:
5901 err = got_error(GOT_ERR_OBJ_TYPE);
5902 break;
5905 done:
5906 if (tag)
5907 got_object_tag_close(tag);
5908 if (err) {
5909 free(*commit_id);
5910 *commit_id = NULL;
5912 return err;
5915 static const struct got_error *
5916 log_ref_entry(struct tog_view **new_view, int begin_x,
5917 struct tog_reflist_entry *re, struct got_repository *repo)
5919 struct tog_view *log_view;
5920 const struct got_error *err = NULL;
5921 struct got_object_id *commit_id = NULL;
5923 *new_view = NULL;
5925 err = resolve_reflist_entry(&commit_id, re, repo);
5926 if (err) {
5927 if (err->code != GOT_ERR_OBJ_TYPE)
5928 return err;
5929 else
5930 return NULL;
5933 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5934 if (log_view == NULL) {
5935 err = got_error_from_errno("view_open");
5936 goto done;
5939 err = open_log_view(log_view, commit_id, repo,
5940 got_ref_get_name(re->ref), "", 0);
5941 done:
5942 if (err)
5943 view_close(log_view);
5944 else
5945 *new_view = log_view;
5946 free(commit_id);
5947 return err;
5950 static void
5951 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5953 struct tog_reflist_entry *re;
5954 int i = 0;
5956 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5957 return;
5959 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5960 while (i++ < maxscroll) {
5961 if (re == NULL)
5962 break;
5963 s->first_displayed_entry = re;
5964 re = TAILQ_PREV(re, tog_reflist_head, entry);
5968 static void
5969 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5971 struct tog_reflist_entry *next, *last;
5972 int n = 0;
5974 if (s->first_displayed_entry)
5975 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5976 else
5977 next = TAILQ_FIRST(&s->refs);
5979 last = s->last_displayed_entry;
5980 while (next && last && n++ < maxscroll) {
5981 last = TAILQ_NEXT(last, entry);
5982 if (last) {
5983 s->first_displayed_entry = next;
5984 next = TAILQ_NEXT(next, entry);
5989 static const struct got_error *
5990 search_start_ref_view(struct tog_view *view)
5992 struct tog_ref_view_state *s = &view->state.ref;
5994 s->matched_entry = NULL;
5995 return NULL;
5998 static int
5999 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6001 regmatch_t regmatch;
6003 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6004 0) == 0;
6007 static const struct got_error *
6008 search_next_ref_view(struct tog_view *view)
6010 struct tog_ref_view_state *s = &view->state.ref;
6011 struct tog_reflist_entry *re = NULL;
6013 if (!view->searching) {
6014 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6015 return NULL;
6018 if (s->matched_entry) {
6019 if (view->searching == TOG_SEARCH_FORWARD) {
6020 if (s->selected_entry)
6021 re = TAILQ_NEXT(s->selected_entry, entry);
6022 else
6023 re = TAILQ_PREV(s->selected_entry,
6024 tog_reflist_head, entry);
6025 } else {
6026 if (s->selected_entry == NULL)
6027 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6028 else
6029 re = TAILQ_PREV(s->selected_entry,
6030 tog_reflist_head, entry);
6032 } else {
6033 if (view->searching == TOG_SEARCH_FORWARD)
6034 re = TAILQ_FIRST(&s->refs);
6035 else
6036 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6039 while (1) {
6040 if (re == NULL) {
6041 if (s->matched_entry == NULL) {
6042 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6043 return NULL;
6045 if (view->searching == TOG_SEARCH_FORWARD)
6046 re = TAILQ_FIRST(&s->refs);
6047 else
6048 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6051 if (match_reflist_entry(re, &view->regex)) {
6052 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6053 s->matched_entry = re;
6054 break;
6057 if (view->searching == TOG_SEARCH_FORWARD)
6058 re = TAILQ_NEXT(re, entry);
6059 else
6060 re = TAILQ_PREV(re, tog_reflist_head, entry);
6063 if (s->matched_entry) {
6064 s->first_displayed_entry = s->matched_entry;
6065 s->selected = 0;
6068 return NULL;
6071 static const struct got_error *
6072 show_ref_view(struct tog_view *view)
6074 const struct got_error *err = NULL;
6075 struct tog_ref_view_state *s = &view->state.ref;
6076 struct tog_reflist_entry *re;
6077 char *line = NULL;
6078 wchar_t *wline;
6079 struct tog_color *tc;
6080 int width, n;
6081 int limit = view->nlines;
6083 werase(view->window);
6085 s->ndisplayed = 0;
6087 if (limit == 0)
6088 return NULL;
6090 re = s->first_displayed_entry;
6092 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6093 s->nrefs) == -1)
6094 return got_error_from_errno("asprintf");
6096 err = format_line(&wline, &width, line, view->ncols, 0);
6097 if (err) {
6098 free(line);
6099 return err;
6101 if (view_needs_focus_indication(view))
6102 wstandout(view->window);
6103 waddwstr(view->window, wline);
6104 if (view_needs_focus_indication(view))
6105 wstandend(view->window);
6106 free(wline);
6107 wline = NULL;
6108 free(line);
6109 line = NULL;
6110 if (width < view->ncols - 1)
6111 waddch(view->window, '\n');
6112 if (--limit <= 0)
6113 return NULL;
6115 n = 0;
6116 while (re && limit > 0) {
6117 char *line = NULL;
6119 if (got_ref_is_symbolic(re->ref)) {
6120 if (asprintf(&line, "%s -> %s",
6121 got_ref_get_name(re->ref),
6122 got_ref_get_symref_target(re->ref)) == -1)
6123 return got_error_from_errno("asprintf");
6124 } else if (s->show_ids) {
6125 struct got_object_id *id;
6126 char *id_str;
6127 err = got_ref_resolve(&id, s->repo, re->ref);
6128 if (err)
6129 return err;
6130 err = got_object_id_str(&id_str, id);
6131 if (err) {
6132 free(id);
6133 return err;
6135 if (asprintf(&line, "%s: %s",
6136 got_ref_get_name(re->ref), id_str) == -1) {
6137 err = got_error_from_errno("asprintf");
6138 free(id);
6139 free(id_str);
6140 return err;
6142 free(id);
6143 free(id_str);
6144 } else {
6145 line = strdup(got_ref_get_name(re->ref));
6146 if (line == NULL)
6147 return got_error_from_errno("strdup");
6150 err = format_line(&wline, &width, line, view->ncols, 0);
6151 if (err) {
6152 free(line);
6153 return err;
6155 if (n == s->selected) {
6156 if (view->focussed)
6157 wstandout(view->window);
6158 s->selected_entry = re;
6160 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6161 if (tc)
6162 wattr_on(view->window,
6163 COLOR_PAIR(tc->colorpair), NULL);
6164 waddwstr(view->window, wline);
6165 if (tc)
6166 wattr_off(view->window,
6167 COLOR_PAIR(tc->colorpair), NULL);
6168 if (width < view->ncols - 1)
6169 waddch(view->window, '\n');
6170 if (n == s->selected && view->focussed)
6171 wstandend(view->window);
6172 free(line);
6173 free(wline);
6174 wline = NULL;
6175 n++;
6176 s->ndisplayed++;
6177 s->last_displayed_entry = re;
6179 limit--;
6180 re = TAILQ_NEXT(re, entry);
6183 view_vborder(view);
6184 return err;
6187 static const struct got_error *
6188 browse_ref_tree(struct tog_view **new_view, int begin_x,
6189 struct tog_reflist_entry *re, struct got_repository *repo)
6191 const struct got_error *err = NULL;
6192 struct got_object_id *commit_id = NULL;
6193 struct tog_view *tree_view;
6195 *new_view = NULL;
6197 err = resolve_reflist_entry(&commit_id, re, repo);
6198 if (err) {
6199 if (err->code != GOT_ERR_OBJ_TYPE)
6200 return err;
6201 else
6202 return NULL;
6206 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6207 if (tree_view == NULL) {
6208 err = got_error_from_errno("view_open");
6209 goto done;
6212 err = open_tree_view(tree_view, commit_id,
6213 got_ref_get_name(re->ref), repo);
6214 if (err)
6215 goto done;
6217 *new_view = tree_view;
6218 done:
6219 free(commit_id);
6220 return err;
6222 static const struct got_error *
6223 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6225 const struct got_error *err = NULL;
6226 struct tog_ref_view_state *s = &view->state.ref;
6227 struct tog_view *log_view, *tree_view;
6228 struct tog_reflist_entry *re;
6229 int begin_x = 0, n;
6231 switch (ch) {
6232 case 'i':
6233 s->show_ids = !s->show_ids;
6234 break;
6235 case 'o':
6236 s->sort_by_date = !s->sort_by_date;
6237 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6238 got_ref_cmp_by_commit_timestamp_descending :
6239 got_ref_cmp_by_name, s->repo);
6240 if (err)
6241 break;
6242 got_reflist_object_id_map_free(tog_refs_idmap);
6243 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6244 &tog_refs, s->repo);
6245 if (err)
6246 break;
6247 ref_view_free_refs(s);
6248 err = ref_view_load_refs(s);
6249 break;
6250 case KEY_ENTER:
6251 case '\r':
6252 if (!s->selected_entry)
6253 break;
6254 if (view_is_parent_view(view))
6255 begin_x = view_split_begin_x(view->begin_x);
6256 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6257 s->repo);
6258 view->focussed = 0;
6259 log_view->focussed = 1;
6260 if (view_is_parent_view(view)) {
6261 err = view_close_child(view);
6262 if (err)
6263 return err;
6264 view_set_child(view, log_view);
6265 view->focus_child = 1;
6266 } else
6267 *new_view = log_view;
6268 break;
6269 case 't':
6270 if (!s->selected_entry)
6271 break;
6272 if (view_is_parent_view(view))
6273 begin_x = view_split_begin_x(view->begin_x);
6274 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6275 s->repo);
6276 if (err || tree_view == NULL)
6277 break;
6278 view->focussed = 0;
6279 tree_view->focussed = 1;
6280 if (view_is_parent_view(view)) {
6281 err = view_close_child(view);
6282 if (err)
6283 return err;
6284 view_set_child(view, tree_view);
6285 view->focus_child = 1;
6286 } else
6287 *new_view = tree_view;
6288 break;
6289 case 'g':
6290 case KEY_HOME:
6291 s->selected = 0;
6292 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6293 break;
6294 case 'G':
6295 case KEY_END:
6296 s->selected = 0;
6297 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6298 for (n = 0; n < view->nlines - 1; n++) {
6299 if (re == NULL)
6300 break;
6301 s->first_displayed_entry = re;
6302 re = TAILQ_PREV(re, tog_reflist_head, entry);
6304 if (n > 0)
6305 s->selected = n - 1;
6306 break;
6307 case 'k':
6308 case KEY_UP:
6309 case CTRL('p'):
6310 if (s->selected > 0) {
6311 s->selected--;
6312 break;
6314 ref_scroll_up(s, 1);
6315 break;
6316 case KEY_PPAGE:
6317 case CTRL('b'):
6318 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6319 s->selected = 0;
6320 ref_scroll_up(s, MAX(0, view->nlines - 1));
6321 break;
6322 case 'j':
6323 case KEY_DOWN:
6324 case CTRL('n'):
6325 if (s->selected < s->ndisplayed - 1) {
6326 s->selected++;
6327 break;
6329 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6330 /* can't scroll any further */
6331 break;
6332 ref_scroll_down(s, 1);
6333 break;
6334 case KEY_NPAGE:
6335 case CTRL('f'):
6336 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6337 /* can't scroll any further; move cursor down */
6338 if (s->selected < s->ndisplayed - 1)
6339 s->selected = s->ndisplayed - 1;
6340 break;
6342 ref_scroll_down(s, view->nlines - 1);
6343 break;
6344 case CTRL('l'):
6345 tog_free_refs();
6346 err = tog_load_refs(s->repo, s->sort_by_date);
6347 if (err)
6348 break;
6349 ref_view_free_refs(s);
6350 err = ref_view_load_refs(s);
6351 break;
6352 case KEY_RESIZE:
6353 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6354 s->selected = view->nlines - 2;
6355 break;
6356 default:
6357 break;
6360 return err;
6363 __dead static void
6364 usage_ref(void)
6366 endwin();
6367 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6368 getprogname());
6369 exit(1);
6372 static const struct got_error *
6373 cmd_ref(int argc, char *argv[])
6375 const struct got_error *error;
6376 struct got_repository *repo = NULL;
6377 struct got_worktree *worktree = NULL;
6378 char *cwd = NULL, *repo_path = NULL;
6379 int ch;
6380 struct tog_view *view;
6382 while ((ch = getopt(argc, argv, "r:")) != -1) {
6383 switch (ch) {
6384 case 'r':
6385 repo_path = realpath(optarg, NULL);
6386 if (repo_path == NULL)
6387 return got_error_from_errno2("realpath",
6388 optarg);
6389 break;
6390 default:
6391 usage_ref();
6392 /* NOTREACHED */
6396 argc -= optind;
6397 argv += optind;
6399 if (argc > 1)
6400 usage_ref();
6402 if (repo_path == NULL) {
6403 cwd = getcwd(NULL, 0);
6404 if (cwd == NULL)
6405 return got_error_from_errno("getcwd");
6406 error = got_worktree_open(&worktree, cwd);
6407 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6408 goto done;
6409 if (worktree)
6410 repo_path =
6411 strdup(got_worktree_get_repo_path(worktree));
6412 else
6413 repo_path = strdup(cwd);
6414 if (repo_path == NULL) {
6415 error = got_error_from_errno("strdup");
6416 goto done;
6420 error = got_repo_open(&repo, repo_path, NULL);
6421 if (error != NULL)
6422 goto done;
6424 init_curses();
6426 error = apply_unveil(got_repo_get_path(repo), NULL);
6427 if (error)
6428 goto done;
6430 error = tog_load_refs(repo, 0);
6431 if (error)
6432 goto done;
6434 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6435 if (view == NULL) {
6436 error = got_error_from_errno("view_open");
6437 goto done;
6440 error = open_ref_view(view, repo);
6441 if (error)
6442 goto done;
6444 if (worktree) {
6445 /* Release work tree lock. */
6446 got_worktree_close(worktree);
6447 worktree = NULL;
6449 error = view_loop(view);
6450 done:
6451 free(repo_path);
6452 free(cwd);
6453 if (repo) {
6454 const struct got_error *close_err = got_repo_close(repo);
6455 if (close_err)
6456 error = close_err;
6458 tog_free_refs();
6459 return error;
6462 static void
6463 list_commands(FILE *fp)
6465 size_t i;
6467 fprintf(fp, "commands:");
6468 for (i = 0; i < nitems(tog_commands); i++) {
6469 struct tog_cmd *cmd = &tog_commands[i];
6470 fprintf(fp, " %s", cmd->name);
6472 fputc('\n', fp);
6475 __dead static void
6476 usage(int hflag, int status)
6478 FILE *fp = (status == 0) ? stdout : stderr;
6480 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6481 getprogname());
6482 if (hflag) {
6483 fprintf(fp, "lazy usage: %s path\n", getprogname());
6484 list_commands(fp);
6486 exit(status);
6489 static char **
6490 make_argv(int argc, ...)
6492 va_list ap;
6493 char **argv;
6494 int i;
6496 va_start(ap, argc);
6498 argv = calloc(argc, sizeof(char *));
6499 if (argv == NULL)
6500 err(1, "calloc");
6501 for (i = 0; i < argc; i++) {
6502 argv[i] = strdup(va_arg(ap, char *));
6503 if (argv[i] == NULL)
6504 err(1, "strdup");
6507 va_end(ap);
6508 return argv;
6512 * Try to convert 'tog path' into a 'tog log path' command.
6513 * The user could simply have mistyped the command rather than knowingly
6514 * provided a path. So check whether argv[0] can in fact be resolved
6515 * to a path in the HEAD commit and print a special error if not.
6516 * This hack is for mpi@ <3
6518 static const struct got_error *
6519 tog_log_with_path(int argc, char *argv[])
6521 const struct got_error *error = NULL, *close_err;
6522 struct tog_cmd *cmd = NULL;
6523 struct got_repository *repo = NULL;
6524 struct got_worktree *worktree = NULL;
6525 struct got_object_id *commit_id = NULL, *id = NULL;
6526 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6527 char *commit_id_str = NULL, **cmd_argv = NULL;
6529 cwd = getcwd(NULL, 0);
6530 if (cwd == NULL)
6531 return got_error_from_errno("getcwd");
6533 error = got_worktree_open(&worktree, cwd);
6534 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6535 goto done;
6537 if (worktree)
6538 repo_path = strdup(got_worktree_get_repo_path(worktree));
6539 else
6540 repo_path = strdup(cwd);
6541 if (repo_path == NULL) {
6542 error = got_error_from_errno("strdup");
6543 goto done;
6546 error = got_repo_open(&repo, repo_path, NULL);
6547 if (error != NULL)
6548 goto done;
6550 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6551 repo, worktree);
6552 if (error)
6553 goto done;
6555 error = tog_load_refs(repo, 0);
6556 if (error)
6557 goto done;
6558 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6559 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6560 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6561 if (error)
6562 goto done;
6564 if (worktree) {
6565 got_worktree_close(worktree);
6566 worktree = NULL;
6569 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6570 if (error) {
6571 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6572 goto done;
6573 fprintf(stderr, "%s: '%s' is no known command or path\n",
6574 getprogname(), argv[0]);
6575 usage(1, 1);
6576 /* not reached */
6579 close_err = got_repo_close(repo);
6580 if (error == NULL)
6581 error = close_err;
6582 repo = NULL;
6584 error = got_object_id_str(&commit_id_str, commit_id);
6585 if (error)
6586 goto done;
6588 cmd = &tog_commands[0]; /* log */
6589 argc = 4;
6590 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6591 error = cmd->cmd_main(argc, cmd_argv);
6592 done:
6593 if (repo) {
6594 close_err = got_repo_close(repo);
6595 if (error == NULL)
6596 error = close_err;
6598 if (worktree)
6599 got_worktree_close(worktree);
6600 free(id);
6601 free(commit_id_str);
6602 free(commit_id);
6603 free(cwd);
6604 free(repo_path);
6605 free(in_repo_path);
6606 if (cmd_argv) {
6607 int i;
6608 for (i = 0; i < argc; i++)
6609 free(cmd_argv[i]);
6610 free(cmd_argv);
6612 tog_free_refs();
6613 return error;
6616 int
6617 main(int argc, char *argv[])
6619 const struct got_error *error = NULL;
6620 struct tog_cmd *cmd = NULL;
6621 int ch, hflag = 0, Vflag = 0;
6622 char **cmd_argv = NULL;
6623 static struct option longopts[] = {
6624 { "version", no_argument, NULL, 'V' },
6625 { NULL, 0, NULL, 0}
6628 setlocale(LC_CTYPE, "");
6630 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6631 switch (ch) {
6632 case 'h':
6633 hflag = 1;
6634 break;
6635 case 'V':
6636 Vflag = 1;
6637 break;
6638 default:
6639 usage(hflag, 1);
6640 /* NOTREACHED */
6644 argc -= optind;
6645 argv += optind;
6646 optind = 1;
6647 optreset = 1;
6649 if (Vflag) {
6650 got_version_print_str();
6651 return 0;
6654 #ifndef PROFILE
6655 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6656 NULL) == -1)
6657 err(1, "pledge");
6658 #endif
6660 if (argc == 0) {
6661 if (hflag)
6662 usage(hflag, 0);
6663 /* Build an argument vector which runs a default command. */
6664 cmd = &tog_commands[0];
6665 argc = 1;
6666 cmd_argv = make_argv(argc, cmd->name);
6667 } else {
6668 size_t i;
6670 /* Did the user specify a command? */
6671 for (i = 0; i < nitems(tog_commands); i++) {
6672 if (strncmp(tog_commands[i].name, argv[0],
6673 strlen(argv[0])) == 0) {
6674 cmd = &tog_commands[i];
6675 break;
6680 if (cmd == NULL) {
6681 if (argc != 1)
6682 usage(0, 1);
6683 /* No command specified; try log with a path */
6684 error = tog_log_with_path(argc, argv);
6685 } else {
6686 if (hflag)
6687 cmd->cmd_usage();
6688 else
6689 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6692 endwin();
6693 putchar('\n');
6694 if (cmd_argv) {
6695 int i;
6696 for (i = 0; i < argc; i++)
6697 free(cmd_argv[i]);
6698 free(cmd_argv);
6701 if (error && error->code != GOT_ERR_CANCELLED)
6702 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6703 return 0;