move encode_delta() in pack_create.c to eliminate a forward declaration
[got-portable.git] / tog / tog.c
blob15773adb5cfcef6d2e7a96584702408d8908643d
1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/stat.h>
18 #include <sys/ioctl.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #if defined(__FreeBSD__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #endif
25 #include <curses.h>
26 #include <panel.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
51 #include "got_diff.h"
52 #include "got_opentemp.h"
53 #include "got_utf8.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_path.h"
59 #include "got_worktree.h"
61 //#define update_panels() (0)
62 //#define doupdate() (0)
64 #ifndef MIN
65 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #endif
68 #ifndef MAX
69 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 #endif
72 #ifndef CTRL
73 #define CTRL(x) ((x) & 0x1f)
74 #endif
76 #ifndef nitems
77 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
78 #endif
80 struct tog_cmd {
81 const char *name;
82 const struct got_error *(*cmd_main)(int, char *[]);
83 void (*cmd_usage)(void);
86 __dead static void usage(int, int);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_ref(void);
93 static const struct got_error* cmd_log(int, char *[]);
94 static const struct got_error* cmd_diff(int, char *[]);
95 static const struct got_error* cmd_blame(int, char *[]);
96 static const struct got_error* cmd_tree(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
99 static struct tog_cmd tog_commands[] = {
100 { "log", cmd_log, usage_log },
101 { "diff", cmd_diff, usage_diff },
102 { "blame", cmd_blame, usage_blame },
103 { "tree", cmd_tree, usage_tree },
104 { "ref", cmd_ref, usage_ref },
107 enum tog_view_type {
108 TOG_VIEW_DIFF,
109 TOG_VIEW_LOG,
110 TOG_VIEW_BLAME,
111 TOG_VIEW_TREE,
112 TOG_VIEW_REF,
115 #define TOG_EOF_STRING "(END)"
117 struct commit_queue_entry {
118 TAILQ_ENTRY(commit_queue_entry) entry;
119 struct got_object_id *id;
120 struct got_commit_object *commit;
121 int idx;
123 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 struct commit_queue {
125 int ncommits;
126 struct commit_queue_head head;
129 struct tog_color {
130 STAILQ_ENTRY(tog_color) entry;
131 regex_t regex;
132 short colorpair;
134 STAILQ_HEAD(tog_colors, tog_color);
136 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static const struct got_error *
140 tog_load_refs(struct got_repository *repo)
142 const struct got_error *err;
144 err = got_ref_list(&tog_refs, repo, NULL, got_ref_cmp_by_name, NULL);
145 if (err)
146 return err;
148 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
149 repo);
152 static void
153 tog_free_refs(void)
155 if (tog_refs_idmap) {
156 got_reflist_object_id_map_free(tog_refs_idmap);
157 tog_refs_idmap = NULL;
159 got_ref_list_free(&tog_refs);
162 static const struct got_error *
163 add_color(struct tog_colors *colors, const char *pattern,
164 int idx, short color)
166 const struct got_error *err = NULL;
167 struct tog_color *tc;
168 int regerr = 0;
170 if (idx < 1 || idx > COLOR_PAIRS - 1)
171 return NULL;
173 init_pair(idx, color, -1);
175 tc = calloc(1, sizeof(*tc));
176 if (tc == NULL)
177 return got_error_from_errno("calloc");
178 regerr = regcomp(&tc->regex, pattern,
179 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
180 if (regerr) {
181 static char regerr_msg[512];
182 static char err_msg[512];
183 regerror(regerr, &tc->regex, regerr_msg,
184 sizeof(regerr_msg));
185 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
186 regerr_msg);
187 err = got_error_msg(GOT_ERR_REGEX, err_msg);
188 free(tc);
189 return err;
191 tc->colorpair = idx;
192 STAILQ_INSERT_HEAD(colors, tc, entry);
193 return NULL;
196 static void
197 free_colors(struct tog_colors *colors)
199 struct tog_color *tc;
201 while (!STAILQ_EMPTY(colors)) {
202 tc = STAILQ_FIRST(colors);
203 STAILQ_REMOVE_HEAD(colors, entry);
204 regfree(&tc->regex);
205 free(tc);
209 struct tog_color *
210 get_color(struct tog_colors *colors, int colorpair)
212 struct tog_color *tc = NULL;
214 STAILQ_FOREACH(tc, colors, entry) {
215 if (tc->colorpair == colorpair)
216 return tc;
219 return NULL;
222 static int
223 default_color_value(const char *envvar)
225 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
226 return COLOR_MAGENTA;
227 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
228 return COLOR_CYAN;
229 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
230 return COLOR_YELLOW;
231 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
232 return COLOR_GREEN;
233 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
234 return COLOR_MAGENTA;
235 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
236 return COLOR_MAGENTA;
237 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
238 return COLOR_CYAN;
239 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
240 return COLOR_GREEN;
241 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
242 return COLOR_GREEN;
243 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
244 return COLOR_CYAN;
245 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
246 return COLOR_YELLOW;
247 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
248 return COLOR_GREEN;
249 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
250 return COLOR_MAGENTA;
251 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
252 return COLOR_YELLOW;
254 return -1;
257 static int
258 get_color_value(const char *envvar)
260 const char *val = getenv(envvar);
262 if (val == NULL)
263 return default_color_value(envvar);
265 if (strcasecmp(val, "black") == 0)
266 return COLOR_BLACK;
267 if (strcasecmp(val, "red") == 0)
268 return COLOR_RED;
269 if (strcasecmp(val, "green") == 0)
270 return COLOR_GREEN;
271 if (strcasecmp(val, "yellow") == 0)
272 return COLOR_YELLOW;
273 if (strcasecmp(val, "blue") == 0)
274 return COLOR_BLUE;
275 if (strcasecmp(val, "magenta") == 0)
276 return COLOR_MAGENTA;
277 if (strcasecmp(val, "cyan") == 0)
278 return COLOR_CYAN;
279 if (strcasecmp(val, "white") == 0)
280 return COLOR_WHITE;
281 if (strcasecmp(val, "default") == 0)
282 return -1;
284 return default_color_value(envvar);
288 struct tog_diff_view_state {
289 struct got_object_id *id1, *id2;
290 const char *label1, *label2;
291 FILE *f;
292 int first_displayed_line;
293 int last_displayed_line;
294 int eof;
295 int diff_context;
296 int ignore_whitespace;
297 int force_text_diff;
298 struct got_repository *repo;
299 struct tog_colors colors;
300 size_t nlines;
301 off_t *line_offsets;
302 int matched_line;
303 int selected_line;
305 /* passed from log view; may be NULL */
306 struct tog_view *log_view;
309 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
311 struct tog_log_thread_args {
312 pthread_cond_t need_commits;
313 pthread_cond_t commit_loaded;
314 int commits_needed;
315 int load_all;
316 struct got_commit_graph *graph;
317 struct commit_queue *commits;
318 const char *in_repo_path;
319 struct got_object_id *start_id;
320 struct got_repository *repo;
321 int log_complete;
322 sig_atomic_t *quit;
323 struct commit_queue_entry **first_displayed_entry;
324 struct commit_queue_entry **selected_entry;
325 int *searching;
326 int *search_next_done;
327 regex_t *regex;
330 struct tog_log_view_state {
331 struct commit_queue commits;
332 struct commit_queue_entry *first_displayed_entry;
333 struct commit_queue_entry *last_displayed_entry;
334 struct commit_queue_entry *selected_entry;
335 int selected;
336 char *in_repo_path;
337 char *head_ref_name;
338 int log_branches;
339 struct got_repository *repo;
340 struct got_object_id *start_id;
341 sig_atomic_t quit;
342 pthread_t thread;
343 struct tog_log_thread_args thread_args;
344 struct commit_queue_entry *matched_entry;
345 struct commit_queue_entry *search_entry;
346 struct tog_colors colors;
349 #define TOG_COLOR_DIFF_MINUS 1
350 #define TOG_COLOR_DIFF_PLUS 2
351 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
352 #define TOG_COLOR_DIFF_META 4
353 #define TOG_COLOR_TREE_SUBMODULE 5
354 #define TOG_COLOR_TREE_SYMLINK 6
355 #define TOG_COLOR_TREE_DIRECTORY 7
356 #define TOG_COLOR_TREE_EXECUTABLE 8
357 #define TOG_COLOR_COMMIT 9
358 #define TOG_COLOR_AUTHOR 10
359 #define TOG_COLOR_DATE 11
360 #define TOG_COLOR_REFS_HEADS 12
361 #define TOG_COLOR_REFS_TAGS 13
362 #define TOG_COLOR_REFS_REMOTES 14
364 struct tog_blame_cb_args {
365 struct tog_blame_line *lines; /* one per line */
366 int nlines;
368 struct tog_view *view;
369 struct got_object_id *commit_id;
370 int *quit;
373 struct tog_blame_thread_args {
374 const char *path;
375 struct got_repository *repo;
376 struct tog_blame_cb_args *cb_args;
377 int *complete;
378 got_cancel_cb cancel_cb;
379 void *cancel_arg;
382 struct tog_blame {
383 FILE *f;
384 off_t filesize;
385 struct tog_blame_line *lines;
386 int nlines;
387 off_t *line_offsets;
388 pthread_t thread;
389 struct tog_blame_thread_args thread_args;
390 struct tog_blame_cb_args cb_args;
391 const char *path;
394 struct tog_blame_view_state {
395 int first_displayed_line;
396 int last_displayed_line;
397 int selected_line;
398 int blame_complete;
399 int eof;
400 int done;
401 struct got_object_id_queue blamed_commits;
402 struct got_object_qid *blamed_commit;
403 char *path;
404 struct got_repository *repo;
405 struct got_object_id *commit_id;
406 struct tog_blame blame;
407 int matched_line;
408 struct tog_colors colors;
411 struct tog_parent_tree {
412 TAILQ_ENTRY(tog_parent_tree) entry;
413 struct got_tree_object *tree;
414 struct got_tree_entry *first_displayed_entry;
415 struct got_tree_entry *selected_entry;
416 int selected;
419 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
421 struct tog_tree_view_state {
422 char *tree_label;
423 struct got_object_id *commit_id;/* commit which this tree belongs to */
424 struct got_tree_object *root; /* the commit's root tree entry */
425 struct got_tree_object *tree; /* currently displayed (sub-)tree */
426 struct got_tree_entry *first_displayed_entry;
427 struct got_tree_entry *last_displayed_entry;
428 struct got_tree_entry *selected_entry;
429 int ndisplayed, selected, show_ids;
430 struct tog_parent_trees parents; /* parent trees of current sub-tree */
431 char *head_ref_name;
432 struct got_repository *repo;
433 struct got_tree_entry *matched_entry;
434 struct tog_colors colors;
437 struct tog_reflist_entry {
438 TAILQ_ENTRY(tog_reflist_entry) entry;
439 struct got_reference *ref;
440 int idx;
443 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
445 struct tog_ref_view_state {
446 struct tog_reflist_head refs;
447 struct tog_reflist_entry *first_displayed_entry;
448 struct tog_reflist_entry *last_displayed_entry;
449 struct tog_reflist_entry *selected_entry;
450 int nrefs, ndisplayed, selected, show_ids;
451 struct got_repository *repo;
452 struct tog_reflist_entry *matched_entry;
453 struct tog_colors colors;
457 * We implement two types of views: parent views and child views.
459 * The 'Tab' key switches focus between a parent view and its child view.
460 * Child views are shown side-by-side to their parent view, provided
461 * there is enough screen estate.
463 * When a new view is opened from within a parent view, this new view
464 * becomes a child view of the parent view, replacing any existing child.
466 * When a new view is opened from within a child view, this new view
467 * becomes a parent view which will obscure the views below until the
468 * user quits the new parent view by typing 'q'.
470 * This list of views contains parent views only.
471 * Child views are only pointed to by their parent view.
473 TAILQ_HEAD(tog_view_list_head, tog_view);
475 struct tog_view {
476 TAILQ_ENTRY(tog_view) entry;
477 WINDOW *window;
478 PANEL *panel;
479 int nlines, ncols, begin_y, begin_x;
480 int lines, cols; /* copies of LINES and COLS */
481 int focussed; /* Only set on one parent or child view at a time. */
482 int dying;
483 struct tog_view *parent;
484 struct tog_view *child;
487 * This flag is initially set on parent views when a new child view
488 * is created. It gets toggled when the 'Tab' key switches focus
489 * between parent and child.
490 * The flag indicates whether focus should be passed on to our child
491 * view if this parent view gets picked for focus after another parent
492 * view was closed. This prevents child views from losing focus in such
493 * situations.
495 int focus_child;
497 /* type-specific state */
498 enum tog_view_type type;
499 union {
500 struct tog_diff_view_state diff;
501 struct tog_log_view_state log;
502 struct tog_blame_view_state blame;
503 struct tog_tree_view_state tree;
504 struct tog_ref_view_state ref;
505 } state;
507 const struct got_error *(*show)(struct tog_view *);
508 const struct got_error *(*input)(struct tog_view **,
509 struct tog_view *, int);
510 const struct got_error *(*close)(struct tog_view *);
512 const struct got_error *(*search_start)(struct tog_view *);
513 const struct got_error *(*search_next)(struct tog_view *);
514 int search_started;
515 int searching;
516 #define TOG_SEARCH_FORWARD 1
517 #define TOG_SEARCH_BACKWARD 2
518 int search_next_done;
519 #define TOG_SEARCH_HAVE_MORE 1
520 #define TOG_SEARCH_NO_MORE 2
521 #define TOG_SEARCH_HAVE_NONE 3
522 regex_t regex;
523 regmatch_t regmatch;
526 static const struct got_error *open_diff_view(struct tog_view *,
527 struct got_object_id *, struct got_object_id *,
528 const char *, const char *, int, int, int, struct tog_view *,
529 struct got_repository *);
530 static const struct got_error *show_diff_view(struct tog_view *);
531 static const struct got_error *input_diff_view(struct tog_view **,
532 struct tog_view *, int);
533 static const struct got_error* close_diff_view(struct tog_view *);
534 static const struct got_error *search_start_diff_view(struct tog_view *);
535 static const struct got_error *search_next_diff_view(struct tog_view *);
537 static const struct got_error *open_log_view(struct tog_view *,
538 struct got_object_id *, struct got_repository *,
539 const char *, const char *, int);
540 static const struct got_error * show_log_view(struct tog_view *);
541 static const struct got_error *input_log_view(struct tog_view **,
542 struct tog_view *, int);
543 static const struct got_error *close_log_view(struct tog_view *);
544 static const struct got_error *search_start_log_view(struct tog_view *);
545 static const struct got_error *search_next_log_view(struct tog_view *);
547 static const struct got_error *open_blame_view(struct tog_view *, char *,
548 struct got_object_id *, struct got_repository *);
549 static const struct got_error *show_blame_view(struct tog_view *);
550 static const struct got_error *input_blame_view(struct tog_view **,
551 struct tog_view *, int);
552 static const struct got_error *close_blame_view(struct tog_view *);
553 static const struct got_error *search_start_blame_view(struct tog_view *);
554 static const struct got_error *search_next_blame_view(struct tog_view *);
556 static const struct got_error *open_tree_view(struct tog_view *,
557 struct got_object_id *, const char *, struct got_repository *);
558 static const struct got_error *show_tree_view(struct tog_view *);
559 static const struct got_error *input_tree_view(struct tog_view **,
560 struct tog_view *, int);
561 static const struct got_error *close_tree_view(struct tog_view *);
562 static const struct got_error *search_start_tree_view(struct tog_view *);
563 static const struct got_error *search_next_tree_view(struct tog_view *);
565 static const struct got_error *open_ref_view(struct tog_view *,
566 struct got_repository *);
567 static const struct got_error *show_ref_view(struct tog_view *);
568 static const struct got_error *input_ref_view(struct tog_view **,
569 struct tog_view *, int);
570 static const struct got_error *close_ref_view(struct tog_view *);
571 static const struct got_error *search_start_ref_view(struct tog_view *);
572 static const struct got_error *search_next_ref_view(struct tog_view *);
574 static volatile sig_atomic_t tog_sigwinch_received;
575 static volatile sig_atomic_t tog_sigpipe_received;
576 static volatile sig_atomic_t tog_sigcont_received;
578 static void
579 tog_sigwinch(int signo)
581 tog_sigwinch_received = 1;
584 static void
585 tog_sigpipe(int signo)
587 tog_sigpipe_received = 1;
590 static void
591 tog_sigcont(int signo)
593 tog_sigcont_received = 1;
596 static const struct got_error *
597 view_close(struct tog_view *view)
599 const struct got_error *err = NULL;
601 if (view->child) {
602 view_close(view->child);
603 view->child = NULL;
605 if (view->close)
606 err = view->close(view);
607 if (view->panel)
608 del_panel(view->panel);
609 if (view->window)
610 delwin(view->window);
611 free(view);
612 return err;
615 static struct tog_view *
616 view_open(int nlines, int ncols, int begin_y, int begin_x,
617 enum tog_view_type type)
619 struct tog_view *view = calloc(1, sizeof(*view));
621 if (view == NULL)
622 return NULL;
624 view->type = type;
625 view->lines = LINES;
626 view->cols = COLS;
627 view->nlines = nlines ? nlines : LINES - begin_y;
628 view->ncols = ncols ? ncols : COLS - begin_x;
629 view->begin_y = begin_y;
630 view->begin_x = begin_x;
631 view->window = newwin(nlines, ncols, begin_y, begin_x);
632 if (view->window == NULL) {
633 view_close(view);
634 return NULL;
636 view->panel = new_panel(view->window);
637 if (view->panel == NULL ||
638 set_panel_userptr(view->panel, view) != OK) {
639 view_close(view);
640 return NULL;
643 keypad(view->window, TRUE);
644 return view;
647 static int
648 view_split_begin_x(int begin_x)
650 if (begin_x > 0 || COLS < 120)
651 return 0;
652 return (COLS - MAX(COLS / 2, 80));
655 static const struct got_error *view_resize(struct tog_view *);
657 static const struct got_error *
658 view_splitscreen(struct tog_view *view)
660 const struct got_error *err = NULL;
662 view->begin_y = 0;
663 view->begin_x = view_split_begin_x(0);
664 view->nlines = LINES;
665 view->ncols = COLS - view->begin_x;
666 view->lines = LINES;
667 view->cols = COLS;
668 err = view_resize(view);
669 if (err)
670 return err;
672 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
673 return got_error_from_errno("mvwin");
675 return NULL;
678 static const struct got_error *
679 view_fullscreen(struct tog_view *view)
681 const struct got_error *err = NULL;
683 view->begin_x = 0;
684 view->begin_y = 0;
685 view->nlines = LINES;
686 view->ncols = COLS;
687 view->lines = LINES;
688 view->cols = COLS;
689 err = view_resize(view);
690 if (err)
691 return err;
693 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
694 return got_error_from_errno("mvwin");
696 return NULL;
699 static int
700 view_is_parent_view(struct tog_view *view)
702 return view->parent == NULL;
705 static const struct got_error *
706 view_resize(struct tog_view *view)
708 int nlines, ncols;
710 if (view->lines > LINES)
711 nlines = view->nlines - (view->lines - LINES);
712 else
713 nlines = view->nlines + (LINES - view->lines);
715 if (view->cols > COLS)
716 ncols = view->ncols - (view->cols - COLS);
717 else
718 ncols = view->ncols + (COLS - view->cols);
720 if (wresize(view->window, nlines, ncols) == ERR)
721 return got_error_from_errno("wresize");
722 if (replace_panel(view->panel, view->window) == ERR)
723 return got_error_from_errno("replace_panel");
724 wclear(view->window);
726 view->nlines = nlines;
727 view->ncols = ncols;
728 view->lines = LINES;
729 view->cols = COLS;
731 if (view->child) {
732 view->child->begin_x = view_split_begin_x(view->begin_x);
733 if (view->child->begin_x == 0) {
734 view_fullscreen(view->child);
735 if (view->child->focussed)
736 show_panel(view->child->panel);
737 else
738 show_panel(view->panel);
739 } else {
740 view_splitscreen(view->child);
741 show_panel(view->child->panel);
745 return NULL;
748 static const struct got_error *
749 view_close_child(struct tog_view *view)
751 const struct got_error *err = NULL;
753 if (view->child == NULL)
754 return NULL;
756 err = view_close(view->child);
757 view->child = NULL;
758 return err;
761 static void
762 view_set_child(struct tog_view *view, struct tog_view *child)
764 view->child = child;
765 child->parent = view;
768 static int
769 view_is_splitscreen(struct tog_view *view)
771 return view->begin_x > 0;
774 static void
775 tog_resizeterm(void)
777 int cols, lines;
778 struct winsize size;
780 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
781 cols = 80; /* Default */
782 lines = 24;
783 } else {
784 cols = size.ws_col;
785 lines = size.ws_row;
787 resize_term(lines, cols);
790 static const struct got_error *
791 view_search_start(struct tog_view *view)
793 const struct got_error *err = NULL;
794 char pattern[1024];
795 int ret;
797 if (view->search_started) {
798 regfree(&view->regex);
799 view->searching = 0;
800 memset(&view->regmatch, 0, sizeof(view->regmatch));
802 view->search_started = 0;
804 if (view->nlines < 1)
805 return NULL;
807 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
808 wclrtoeol(view->window);
810 nocbreak();
811 echo();
812 ret = wgetnstr(view->window, pattern, sizeof(pattern));
813 cbreak();
814 noecho();
815 if (ret == ERR)
816 return NULL;
818 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
819 err = view->search_start(view);
820 if (err) {
821 regfree(&view->regex);
822 return err;
824 view->search_started = 1;
825 view->searching = TOG_SEARCH_FORWARD;
826 view->search_next_done = 0;
827 view->search_next(view);
830 return NULL;
833 static const struct got_error *
834 view_input(struct tog_view **new, int *done, struct tog_view *view,
835 struct tog_view_list_head *views)
837 const struct got_error *err = NULL;
838 struct tog_view *v;
839 int ch, errcode;
841 *new = NULL;
843 /* Clear "no matches" indicator. */
844 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
845 view->search_next_done == TOG_SEARCH_HAVE_NONE)
846 view->search_next_done = TOG_SEARCH_HAVE_MORE;
848 if (view->searching && !view->search_next_done) {
849 errcode = pthread_mutex_unlock(&tog_mutex);
850 if (errcode)
851 return got_error_set_errno(errcode,
852 "pthread_mutex_unlock");
853 sched_yield();
854 errcode = pthread_mutex_lock(&tog_mutex);
855 if (errcode)
856 return got_error_set_errno(errcode,
857 "pthread_mutex_lock");
858 view->search_next(view);
859 return NULL;
862 nodelay(stdscr, FALSE);
863 /* Allow threads to make progress while we are waiting for input. */
864 errcode = pthread_mutex_unlock(&tog_mutex);
865 if (errcode)
866 return got_error_set_errno(errcode, "pthread_mutex_unlock");
867 ch = wgetch(view->window);
868 errcode = pthread_mutex_lock(&tog_mutex);
869 if (errcode)
870 return got_error_set_errno(errcode, "pthread_mutex_lock");
871 nodelay(stdscr, TRUE);
873 if (tog_sigwinch_received || tog_sigcont_received) {
874 tog_resizeterm();
875 tog_sigwinch_received = 0;
876 tog_sigcont_received = 0;
877 TAILQ_FOREACH(v, views, entry) {
878 err = view_resize(v);
879 if (err)
880 return err;
881 err = v->input(new, v, KEY_RESIZE);
882 if (err)
883 return err;
884 if (v->child) {
885 err = view_resize(v->child);
886 if (err)
887 return err;
888 err = v->child->input(new, v->child,
889 KEY_RESIZE);
890 if (err)
891 return err;
896 switch (ch) {
897 case '\t':
898 if (view->child) {
899 view->focussed = 0;
900 view->child->focussed = 1;
901 view->focus_child = 1;
902 } else if (view->parent) {
903 view->focussed = 0;
904 view->parent->focussed = 1;
905 view->parent->focus_child = 0;
907 break;
908 case 'q':
909 err = view->input(new, view, ch);
910 view->dying = 1;
911 break;
912 case 'Q':
913 *done = 1;
914 break;
915 case 'f':
916 if (view_is_parent_view(view)) {
917 if (view->child == NULL)
918 break;
919 if (view_is_splitscreen(view->child)) {
920 view->focussed = 0;
921 view->child->focussed = 1;
922 err = view_fullscreen(view->child);
923 } else
924 err = view_splitscreen(view->child);
925 if (err)
926 break;
927 err = view->child->input(new, view->child,
928 KEY_RESIZE);
929 } else {
930 if (view_is_splitscreen(view)) {
931 view->parent->focussed = 0;
932 view->focussed = 1;
933 err = view_fullscreen(view);
934 } else {
935 err = view_splitscreen(view);
937 if (err)
938 break;
939 err = view->input(new, view, KEY_RESIZE);
941 break;
942 case KEY_RESIZE:
943 break;
944 case '/':
945 if (view->search_start)
946 view_search_start(view);
947 else
948 err = view->input(new, view, ch);
949 break;
950 case 'N':
951 case 'n':
952 if (view->search_started && view->search_next) {
953 view->searching = (ch == 'n' ?
954 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
955 view->search_next_done = 0;
956 view->search_next(view);
957 } else
958 err = view->input(new, view, ch);
959 break;
960 default:
961 err = view->input(new, view, ch);
962 break;
965 return err;
968 void
969 view_vborder(struct tog_view *view)
971 PANEL *panel;
972 const struct tog_view *view_above;
974 if (view->parent)
975 return view_vborder(view->parent);
977 panel = panel_above(view->panel);
978 if (panel == NULL)
979 return;
981 view_above = panel_userptr(panel);
982 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
983 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
987 view_needs_focus_indication(struct tog_view *view)
989 if (view_is_parent_view(view)) {
990 if (view->child == NULL || view->child->focussed)
991 return 0;
992 if (!view_is_splitscreen(view->child))
993 return 0;
994 } else if (!view_is_splitscreen(view))
995 return 0;
997 return view->focussed;
1000 static const struct got_error *
1001 view_loop(struct tog_view *view)
1003 const struct got_error *err = NULL;
1004 struct tog_view_list_head views;
1005 struct tog_view *new_view;
1006 int fast_refresh = 10;
1007 int done = 0, errcode;
1009 errcode = pthread_mutex_lock(&tog_mutex);
1010 if (errcode)
1011 return got_error_set_errno(errcode, "pthread_mutex_lock");
1013 TAILQ_INIT(&views);
1014 TAILQ_INSERT_HEAD(&views, view, entry);
1016 view->focussed = 1;
1017 err = view->show(view);
1018 if (err)
1019 return err;
1020 update_panels();
1021 doupdate();
1022 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1023 /* Refresh fast during initialization, then become slower. */
1024 if (fast_refresh && fast_refresh-- == 0)
1025 halfdelay(10); /* switch to once per second */
1027 err = view_input(&new_view, &done, view, &views);
1028 if (err)
1029 break;
1030 if (view->dying) {
1031 struct tog_view *v, *prev = NULL;
1033 if (view_is_parent_view(view))
1034 prev = TAILQ_PREV(view, tog_view_list_head,
1035 entry);
1036 else if (view->parent)
1037 prev = view->parent;
1039 if (view->parent) {
1040 view->parent->child = NULL;
1041 view->parent->focus_child = 0;
1042 } else
1043 TAILQ_REMOVE(&views, view, entry);
1045 err = view_close(view);
1046 if (err)
1047 goto done;
1049 view = NULL;
1050 TAILQ_FOREACH(v, &views, entry) {
1051 if (v->focussed)
1052 break;
1054 if (view == NULL && new_view == NULL) {
1055 /* No view has focus. Try to pick one. */
1056 if (prev)
1057 view = prev;
1058 else if (!TAILQ_EMPTY(&views)) {
1059 view = TAILQ_LAST(&views,
1060 tog_view_list_head);
1062 if (view) {
1063 if (view->focus_child) {
1064 view->child->focussed = 1;
1065 view = view->child;
1066 } else
1067 view->focussed = 1;
1071 if (new_view) {
1072 struct tog_view *v, *t;
1073 /* Only allow one parent view per type. */
1074 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1075 if (v->type != new_view->type)
1076 continue;
1077 TAILQ_REMOVE(&views, v, entry);
1078 err = view_close(v);
1079 if (err)
1080 goto done;
1081 break;
1083 TAILQ_INSERT_TAIL(&views, new_view, entry);
1084 view = new_view;
1086 if (view) {
1087 if (view_is_parent_view(view)) {
1088 if (view->child && view->child->focussed)
1089 view = view->child;
1090 } else {
1091 if (view->parent && view->parent->focussed)
1092 view = view->parent;
1094 show_panel(view->panel);
1095 if (view->child && view_is_splitscreen(view->child))
1096 show_panel(view->child->panel);
1097 if (view->parent && view_is_splitscreen(view)) {
1098 err = view->parent->show(view->parent);
1099 if (err)
1100 goto done;
1102 err = view->show(view);
1103 if (err)
1104 goto done;
1105 if (view->child) {
1106 err = view->child->show(view->child);
1107 if (err)
1108 goto done;
1110 update_panels();
1111 doupdate();
1114 done:
1115 while (!TAILQ_EMPTY(&views)) {
1116 view = TAILQ_FIRST(&views);
1117 TAILQ_REMOVE(&views, view, entry);
1118 view_close(view);
1121 errcode = pthread_mutex_unlock(&tog_mutex);
1122 if (errcode && err == NULL)
1123 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1125 return err;
1128 __dead static void
1129 usage_log(void)
1131 endwin();
1132 fprintf(stderr,
1133 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1134 getprogname());
1135 exit(1);
1138 /* Create newly allocated wide-character string equivalent to a byte string. */
1139 static const struct got_error *
1140 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1142 char *vis = NULL;
1143 const struct got_error *err = NULL;
1145 *ws = NULL;
1146 *wlen = mbstowcs(NULL, s, 0);
1147 if (*wlen == (size_t)-1) {
1148 int vislen;
1149 if (errno != EILSEQ)
1150 return got_error_from_errno("mbstowcs");
1152 /* byte string invalid in current encoding; try to "fix" it */
1153 err = got_mbsavis(&vis, &vislen, s);
1154 if (err)
1155 return err;
1156 *wlen = mbstowcs(NULL, vis, 0);
1157 if (*wlen == (size_t)-1) {
1158 err = got_error_from_errno("mbstowcs"); /* give up */
1159 goto done;
1163 *ws = calloc(*wlen + 1, sizeof(**ws));
1164 if (*ws == NULL) {
1165 err = got_error_from_errno("calloc");
1166 goto done;
1169 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1170 err = got_error_from_errno("mbstowcs");
1171 done:
1172 free(vis);
1173 if (err) {
1174 free(*ws);
1175 *ws = NULL;
1176 *wlen = 0;
1178 return err;
1181 /* Format a line for display, ensuring that it won't overflow a width limit. */
1182 static const struct got_error *
1183 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1184 int col_tab_align)
1186 const struct got_error *err = NULL;
1187 int cols = 0;
1188 wchar_t *wline = NULL;
1189 size_t wlen;
1190 int i;
1192 *wlinep = NULL;
1193 *widthp = 0;
1195 err = mbs2ws(&wline, &wlen, line);
1196 if (err)
1197 return err;
1199 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1200 wline[wlen - 1] = L'\0';
1201 wlen--;
1203 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1204 wline[wlen - 1] = L'\0';
1205 wlen--;
1208 i = 0;
1209 while (i < wlen) {
1210 int width = wcwidth(wline[i]);
1212 if (width == 0) {
1213 i++;
1214 continue;
1217 if (width == 1 || width == 2) {
1218 if (cols + width > wlimit)
1219 break;
1220 cols += width;
1221 i++;
1222 } else if (width == -1) {
1223 if (wline[i] == L'\t') {
1224 width = TABSIZE -
1225 ((cols + col_tab_align) % TABSIZE);
1226 } else {
1227 width = 1;
1228 wline[i] = L'.';
1230 if (cols + width > wlimit)
1231 break;
1232 cols += width;
1233 i++;
1234 } else {
1235 err = got_error_from_errno("wcwidth");
1236 goto done;
1239 wline[i] = L'\0';
1240 if (widthp)
1241 *widthp = cols;
1242 done:
1243 if (err)
1244 free(wline);
1245 else
1246 *wlinep = wline;
1247 return err;
1250 static const struct got_error*
1251 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1252 struct got_object_id *id, struct got_repository *repo)
1254 static const struct got_error *err = NULL;
1255 struct got_reflist_entry *re;
1256 char *s;
1257 const char *name;
1259 *refs_str = NULL;
1261 TAILQ_FOREACH(re, refs, entry) {
1262 struct got_tag_object *tag = NULL;
1263 struct got_object_id *ref_id;
1264 int cmp;
1266 name = got_ref_get_name(re->ref);
1267 if (strcmp(name, GOT_REF_HEAD) == 0)
1268 continue;
1269 if (strncmp(name, "refs/", 5) == 0)
1270 name += 5;
1271 if (strncmp(name, "got/", 4) == 0)
1272 continue;
1273 if (strncmp(name, "heads/", 6) == 0)
1274 name += 6;
1275 if (strncmp(name, "remotes/", 8) == 0) {
1276 name += 8;
1277 s = strstr(name, "/" GOT_REF_HEAD);
1278 if (s != NULL && s[strlen(s)] == '\0')
1279 continue;
1281 err = got_ref_resolve(&ref_id, repo, re->ref);
1282 if (err)
1283 break;
1284 if (strncmp(name, "tags/", 5) == 0) {
1285 err = got_object_open_as_tag(&tag, repo, ref_id);
1286 if (err) {
1287 if (err->code != GOT_ERR_OBJ_TYPE) {
1288 free(ref_id);
1289 break;
1291 /* Ref points at something other than a tag. */
1292 err = NULL;
1293 tag = NULL;
1296 cmp = got_object_id_cmp(tag ?
1297 got_object_tag_get_object_id(tag) : ref_id, id);
1298 free(ref_id);
1299 if (tag)
1300 got_object_tag_close(tag);
1301 if (cmp != 0)
1302 continue;
1303 s = *refs_str;
1304 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1305 s ? ", " : "", name) == -1) {
1306 err = got_error_from_errno("asprintf");
1307 free(s);
1308 *refs_str = NULL;
1309 break;
1311 free(s);
1314 return err;
1317 static const struct got_error *
1318 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1319 int col_tab_align)
1321 char *smallerthan;
1323 smallerthan = strchr(author, '<');
1324 if (smallerthan && smallerthan[1] != '\0')
1325 author = smallerthan + 1;
1326 author[strcspn(author, "@>")] = '\0';
1327 return format_line(wauthor, author_width, author, limit, col_tab_align);
1330 static const struct got_error *
1331 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1332 struct got_object_id *id, const size_t date_display_cols,
1333 int author_display_cols)
1335 struct tog_log_view_state *s = &view->state.log;
1336 const struct got_error *err = NULL;
1337 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1338 char *logmsg0 = NULL, *logmsg = NULL;
1339 char *author = NULL;
1340 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1341 int author_width, logmsg_width;
1342 char *newline, *line = NULL;
1343 int col, limit;
1344 const int avail = view->ncols;
1345 struct tm tm;
1346 time_t committer_time;
1347 struct tog_color *tc;
1349 committer_time = got_object_commit_get_committer_time(commit);
1350 if (gmtime_r(&committer_time, &tm) == NULL)
1351 return got_error_from_errno("gmtime_r");
1352 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1353 return got_error(GOT_ERR_NO_SPACE);
1355 if (avail <= date_display_cols)
1356 limit = MIN(sizeof(datebuf) - 1, avail);
1357 else
1358 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1359 tc = get_color(&s->colors, TOG_COLOR_DATE);
1360 if (tc)
1361 wattr_on(view->window,
1362 COLOR_PAIR(tc->colorpair), NULL);
1363 waddnstr(view->window, datebuf, limit);
1364 if (tc)
1365 wattr_off(view->window,
1366 COLOR_PAIR(tc->colorpair), NULL);
1367 col = limit;
1368 if (col > avail)
1369 goto done;
1371 if (avail >= 120) {
1372 char *id_str;
1373 err = got_object_id_str(&id_str, id);
1374 if (err)
1375 goto done;
1376 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1377 if (tc)
1378 wattr_on(view->window,
1379 COLOR_PAIR(tc->colorpair), NULL);
1380 wprintw(view->window, "%.8s ", id_str);
1381 if (tc)
1382 wattr_off(view->window,
1383 COLOR_PAIR(tc->colorpair), NULL);
1384 free(id_str);
1385 col += 9;
1386 if (col > avail)
1387 goto done;
1390 author = strdup(got_object_commit_get_author(commit));
1391 if (author == NULL) {
1392 err = got_error_from_errno("strdup");
1393 goto done;
1395 err = format_author(&wauthor, &author_width, author, avail - col, col);
1396 if (err)
1397 goto done;
1398 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1399 if (tc)
1400 wattr_on(view->window,
1401 COLOR_PAIR(tc->colorpair), NULL);
1402 waddwstr(view->window, wauthor);
1403 if (tc)
1404 wattr_off(view->window,
1405 COLOR_PAIR(tc->colorpair), NULL);
1406 col += author_width;
1407 while (col < avail && author_width < author_display_cols + 2) {
1408 waddch(view->window, ' ');
1409 col++;
1410 author_width++;
1412 if (col > avail)
1413 goto done;
1415 err = got_object_commit_get_logmsg(&logmsg0, commit);
1416 if (err)
1417 goto done;
1418 logmsg = logmsg0;
1419 while (*logmsg == '\n')
1420 logmsg++;
1421 newline = strchr(logmsg, '\n');
1422 if (newline)
1423 *newline = '\0';
1424 limit = avail - col;
1425 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1426 if (err)
1427 goto done;
1428 waddwstr(view->window, wlogmsg);
1429 col += logmsg_width;
1430 while (col < avail) {
1431 waddch(view->window, ' ');
1432 col++;
1434 done:
1435 free(logmsg0);
1436 free(wlogmsg);
1437 free(author);
1438 free(wauthor);
1439 free(line);
1440 return err;
1443 static struct commit_queue_entry *
1444 alloc_commit_queue_entry(struct got_commit_object *commit,
1445 struct got_object_id *id)
1447 struct commit_queue_entry *entry;
1449 entry = calloc(1, sizeof(*entry));
1450 if (entry == NULL)
1451 return NULL;
1453 entry->id = id;
1454 entry->commit = commit;
1455 return entry;
1458 static void
1459 pop_commit(struct commit_queue *commits)
1461 struct commit_queue_entry *entry;
1463 entry = TAILQ_FIRST(&commits->head);
1464 TAILQ_REMOVE(&commits->head, entry, entry);
1465 got_object_commit_close(entry->commit);
1466 commits->ncommits--;
1467 /* Don't free entry->id! It is owned by the commit graph. */
1468 free(entry);
1471 static void
1472 free_commits(struct commit_queue *commits)
1474 while (!TAILQ_EMPTY(&commits->head))
1475 pop_commit(commits);
1478 static const struct got_error *
1479 match_commit(int *have_match, struct got_object_id *id,
1480 struct got_commit_object *commit, regex_t *regex)
1482 const struct got_error *err = NULL;
1483 regmatch_t regmatch;
1484 char *id_str = NULL, *logmsg = NULL;
1486 *have_match = 0;
1488 err = got_object_id_str(&id_str, id);
1489 if (err)
1490 return err;
1492 err = got_object_commit_get_logmsg(&logmsg, commit);
1493 if (err)
1494 goto done;
1496 if (regexec(regex, got_object_commit_get_author(commit), 1,
1497 &regmatch, 0) == 0 ||
1498 regexec(regex, got_object_commit_get_committer(commit), 1,
1499 &regmatch, 0) == 0 ||
1500 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1501 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1502 *have_match = 1;
1503 done:
1504 free(id_str);
1505 free(logmsg);
1506 return err;
1509 static const struct got_error *
1510 queue_commits(struct tog_log_thread_args *a)
1512 const struct got_error *err = NULL;
1515 * We keep all commits open throughout the lifetime of the log
1516 * view in order to avoid having to re-fetch commits from disk
1517 * while updating the display.
1519 do {
1520 struct got_object_id *id;
1521 struct got_commit_object *commit;
1522 struct commit_queue_entry *entry;
1523 int errcode;
1525 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1526 NULL, NULL);
1527 if (err || id == NULL)
1528 break;
1530 err = got_object_open_as_commit(&commit, a->repo, id);
1531 if (err)
1532 break;
1533 entry = alloc_commit_queue_entry(commit, id);
1534 if (entry == NULL) {
1535 err = got_error_from_errno("alloc_commit_queue_entry");
1536 break;
1539 errcode = pthread_mutex_lock(&tog_mutex);
1540 if (errcode) {
1541 err = got_error_set_errno(errcode,
1542 "pthread_mutex_lock");
1543 break;
1546 entry->idx = a->commits->ncommits;
1547 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1548 a->commits->ncommits++;
1550 if (*a->searching == TOG_SEARCH_FORWARD &&
1551 !*a->search_next_done) {
1552 int have_match;
1553 err = match_commit(&have_match, id, commit, a->regex);
1554 if (err)
1555 break;
1556 if (have_match)
1557 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1560 errcode = pthread_mutex_unlock(&tog_mutex);
1561 if (errcode && err == NULL)
1562 err = got_error_set_errno(errcode,
1563 "pthread_mutex_unlock");
1564 if (err)
1565 break;
1566 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1568 return err;
1571 static void
1572 select_commit(struct tog_log_view_state *s)
1574 struct commit_queue_entry *entry;
1575 int ncommits = 0;
1577 entry = s->first_displayed_entry;
1578 while (entry) {
1579 if (ncommits == s->selected) {
1580 s->selected_entry = entry;
1581 break;
1583 entry = TAILQ_NEXT(entry, entry);
1584 ncommits++;
1588 static const struct got_error *
1589 draw_commits(struct tog_view *view)
1591 const struct got_error *err = NULL;
1592 struct tog_log_view_state *s = &view->state.log;
1593 struct commit_queue_entry *entry = s->selected_entry;
1594 const int limit = view->nlines;
1595 int width;
1596 int ncommits, author_cols = 4;
1597 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1598 char *refs_str = NULL;
1599 wchar_t *wline;
1600 struct tog_color *tc;
1601 static const size_t date_display_cols = 12;
1603 if (s->selected_entry &&
1604 !(view->searching && view->search_next_done == 0)) {
1605 struct got_reflist_head *refs;
1606 err = got_object_id_str(&id_str, s->selected_entry->id);
1607 if (err)
1608 return err;
1609 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1610 s->selected_entry->id);
1611 if (refs) {
1612 err = build_refs_str(&refs_str, refs,
1613 s->selected_entry->id, s->repo);
1614 if (err)
1615 goto done;
1619 if (s->thread_args.commits_needed == 0)
1620 halfdelay(10); /* disable fast refresh */
1622 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1623 if (asprintf(&ncommits_str, " [%d/%d] %s",
1624 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1625 (view->searching && !view->search_next_done) ?
1626 "searching..." : "loading...") == -1) {
1627 err = got_error_from_errno("asprintf");
1628 goto done;
1630 } else {
1631 const char *search_str = NULL;
1633 if (view->searching) {
1634 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1635 search_str = "no more matches";
1636 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1637 search_str = "no matches found";
1638 else if (!view->search_next_done)
1639 search_str = "searching...";
1642 if (asprintf(&ncommits_str, " [%d/%d] %s",
1643 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1644 search_str ? search_str :
1645 (refs_str ? refs_str : "")) == -1) {
1646 err = got_error_from_errno("asprintf");
1647 goto done;
1651 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1652 if (asprintf(&header, "commit %s %s%s",
1653 id_str ? id_str : "........................................",
1654 s->in_repo_path, ncommits_str) == -1) {
1655 err = got_error_from_errno("asprintf");
1656 header = NULL;
1657 goto done;
1659 } else if (asprintf(&header, "commit %s%s",
1660 id_str ? id_str : "........................................",
1661 ncommits_str) == -1) {
1662 err = got_error_from_errno("asprintf");
1663 header = NULL;
1664 goto done;
1666 err = format_line(&wline, &width, header, view->ncols, 0);
1667 if (err)
1668 goto done;
1670 werase(view->window);
1672 if (view_needs_focus_indication(view))
1673 wstandout(view->window);
1674 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1675 if (tc)
1676 wattr_on(view->window,
1677 COLOR_PAIR(tc->colorpair), NULL);
1678 waddwstr(view->window, wline);
1679 if (tc)
1680 wattr_off(view->window,
1681 COLOR_PAIR(tc->colorpair), NULL);
1682 while (width < view->ncols) {
1683 waddch(view->window, ' ');
1684 width++;
1686 if (view_needs_focus_indication(view))
1687 wstandend(view->window);
1688 free(wline);
1689 if (limit <= 1)
1690 goto done;
1692 /* Grow author column size if necessary. */
1693 entry = s->first_displayed_entry;
1694 ncommits = 0;
1695 while (entry) {
1696 char *author;
1697 wchar_t *wauthor;
1698 int width;
1699 if (ncommits >= limit - 1)
1700 break;
1701 author = strdup(got_object_commit_get_author(entry->commit));
1702 if (author == NULL) {
1703 err = got_error_from_errno("strdup");
1704 goto done;
1706 err = format_author(&wauthor, &width, author, COLS,
1707 date_display_cols);
1708 if (author_cols < width)
1709 author_cols = width;
1710 free(wauthor);
1711 free(author);
1712 ncommits++;
1713 entry = TAILQ_NEXT(entry, entry);
1716 entry = s->first_displayed_entry;
1717 s->last_displayed_entry = s->first_displayed_entry;
1718 ncommits = 0;
1719 while (entry) {
1720 if (ncommits >= limit - 1)
1721 break;
1722 if (ncommits == s->selected)
1723 wstandout(view->window);
1724 err = draw_commit(view, entry->commit, entry->id,
1725 date_display_cols, author_cols);
1726 if (ncommits == s->selected)
1727 wstandend(view->window);
1728 if (err)
1729 goto done;
1730 ncommits++;
1731 s->last_displayed_entry = entry;
1732 entry = TAILQ_NEXT(entry, entry);
1735 view_vborder(view);
1736 update_panels();
1737 doupdate();
1738 done:
1739 free(id_str);
1740 free(refs_str);
1741 free(ncommits_str);
1742 free(header);
1743 return err;
1746 static void
1747 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1749 struct commit_queue_entry *entry;
1750 int nscrolled = 0;
1752 entry = TAILQ_FIRST(&s->commits.head);
1753 if (s->first_displayed_entry == entry)
1754 return;
1756 entry = s->first_displayed_entry;
1757 while (entry && nscrolled < maxscroll) {
1758 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1759 if (entry) {
1760 s->first_displayed_entry = entry;
1761 nscrolled++;
1766 static const struct got_error *
1767 trigger_log_thread(struct tog_view *view, int wait)
1769 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1770 int errcode;
1772 halfdelay(1); /* fast refresh while loading commits */
1774 while (ta->commits_needed > 0 || ta->load_all) {
1775 if (ta->log_complete)
1776 break;
1778 /* Wake the log thread. */
1779 errcode = pthread_cond_signal(&ta->need_commits);
1780 if (errcode)
1781 return got_error_set_errno(errcode,
1782 "pthread_cond_signal");
1785 * The mutex will be released while the view loop waits
1786 * in wgetch(), at which time the log thread will run.
1788 if (!wait)
1789 break;
1791 /* Display progress update in log view. */
1792 show_log_view(view);
1793 update_panels();
1794 doupdate();
1796 /* Wait right here while next commit is being loaded. */
1797 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1798 if (errcode)
1799 return got_error_set_errno(errcode,
1800 "pthread_cond_wait");
1802 /* Display progress update in log view. */
1803 show_log_view(view);
1804 update_panels();
1805 doupdate();
1808 return NULL;
1811 static const struct got_error *
1812 log_scroll_down(struct tog_view *view, int maxscroll)
1814 struct tog_log_view_state *s = &view->state.log;
1815 const struct got_error *err = NULL;
1816 struct commit_queue_entry *pentry;
1817 int nscrolled = 0, ncommits_needed;
1819 if (s->last_displayed_entry == NULL)
1820 return NULL;
1822 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1823 if (s->commits.ncommits < ncommits_needed &&
1824 !s->thread_args.log_complete) {
1826 * Ask the log thread for required amount of commits.
1828 s->thread_args.commits_needed += maxscroll;
1829 err = trigger_log_thread(view, 1);
1830 if (err)
1831 return err;
1834 do {
1835 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1836 if (pentry == NULL)
1837 break;
1839 s->last_displayed_entry = pentry;
1841 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1842 if (pentry == NULL)
1843 break;
1844 s->first_displayed_entry = pentry;
1845 } while (++nscrolled < maxscroll);
1847 return err;
1850 static const struct got_error *
1851 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1852 struct got_commit_object *commit, struct got_object_id *commit_id,
1853 struct tog_view *log_view, struct got_repository *repo)
1855 const struct got_error *err;
1856 struct got_object_qid *parent_id;
1857 struct tog_view *diff_view;
1859 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1860 if (diff_view == NULL)
1861 return got_error_from_errno("view_open");
1863 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1864 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1865 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1866 if (err == NULL)
1867 *new_view = diff_view;
1868 return err;
1871 static const struct got_error *
1872 tree_view_visit_subtree(struct tog_tree_view_state *s,
1873 struct got_tree_object *subtree)
1875 struct tog_parent_tree *parent;
1877 parent = calloc(1, sizeof(*parent));
1878 if (parent == NULL)
1879 return got_error_from_errno("calloc");
1881 parent->tree = s->tree;
1882 parent->first_displayed_entry = s->first_displayed_entry;
1883 parent->selected_entry = s->selected_entry;
1884 parent->selected = s->selected;
1885 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1886 s->tree = subtree;
1887 s->selected = 0;
1888 s->first_displayed_entry = NULL;
1889 return NULL;
1892 static const struct got_error *
1893 tree_view_walk_path(struct tog_tree_view_state *s,
1894 struct got_object_id *commit_id, const char *path)
1896 const struct got_error *err = NULL;
1897 struct got_tree_object *tree = NULL;
1898 const char *p;
1899 char *slash, *subpath = NULL;
1901 /* Walk the path and open corresponding tree objects. */
1902 p = path;
1903 while (*p) {
1904 struct got_tree_entry *te;
1905 struct got_object_id *tree_id;
1906 char *te_name;
1908 while (p[0] == '/')
1909 p++;
1911 /* Ensure the correct subtree entry is selected. */
1912 slash = strchr(p, '/');
1913 if (slash == NULL)
1914 te_name = strdup(p);
1915 else
1916 te_name = strndup(p, slash - p);
1917 if (te_name == NULL) {
1918 err = got_error_from_errno("strndup");
1919 break;
1921 te = got_object_tree_find_entry(s->tree, te_name);
1922 if (te == NULL) {
1923 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1924 free(te_name);
1925 break;
1927 free(te_name);
1928 s->first_displayed_entry = s->selected_entry = te;
1930 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1931 break; /* jump to this file's entry */
1933 slash = strchr(p, '/');
1934 if (slash)
1935 subpath = strndup(path, slash - path);
1936 else
1937 subpath = strdup(path);
1938 if (subpath == NULL) {
1939 err = got_error_from_errno("strdup");
1940 break;
1943 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1944 subpath);
1945 if (err)
1946 break;
1948 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1949 free(tree_id);
1950 if (err)
1951 break;
1953 err = tree_view_visit_subtree(s, tree);
1954 if (err) {
1955 got_object_tree_close(tree);
1956 break;
1958 if (slash == NULL)
1959 break;
1960 free(subpath);
1961 subpath = NULL;
1962 p = slash;
1965 free(subpath);
1966 return err;
1969 static const struct got_error *
1970 browse_commit_tree(struct tog_view **new_view, int begin_x,
1971 struct commit_queue_entry *entry, const char *path,
1972 const char *head_ref_name, struct got_repository *repo)
1974 const struct got_error *err = NULL;
1975 struct tog_tree_view_state *s;
1976 struct tog_view *tree_view;
1978 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1979 if (tree_view == NULL)
1980 return got_error_from_errno("view_open");
1982 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1983 if (err)
1984 return err;
1985 s = &tree_view->state.tree;
1987 *new_view = tree_view;
1989 if (got_path_is_root_dir(path))
1990 return NULL;
1992 return tree_view_walk_path(s, entry->id, path);
1995 static const struct got_error *
1996 block_signals_used_by_main_thread(void)
1998 sigset_t sigset;
1999 int errcode;
2001 if (sigemptyset(&sigset) == -1)
2002 return got_error_from_errno("sigemptyset");
2004 /* tog handles SIGWINCH and SIGCONT */
2005 if (sigaddset(&sigset, SIGWINCH) == -1)
2006 return got_error_from_errno("sigaddset");
2007 if (sigaddset(&sigset, SIGCONT) == -1)
2008 return got_error_from_errno("sigaddset");
2010 /* ncurses handles SIGTSTP */
2011 if (sigaddset(&sigset, SIGTSTP) == -1)
2012 return got_error_from_errno("sigaddset");
2014 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2015 if (errcode)
2016 return got_error_set_errno(errcode, "pthread_sigmask");
2018 return NULL;
2021 static void *
2022 log_thread(void *arg)
2024 const struct got_error *err = NULL;
2025 int errcode = 0;
2026 struct tog_log_thread_args *a = arg;
2027 int done = 0;
2029 err = block_signals_used_by_main_thread();
2030 if (err)
2031 return (void *)err;
2033 while (!done && !err && !tog_sigpipe_received) {
2034 err = queue_commits(a);
2035 if (err) {
2036 if (err->code != GOT_ERR_ITER_COMPLETED)
2037 return (void *)err;
2038 err = NULL;
2039 done = 1;
2040 } else if (a->commits_needed > 0 && !a->load_all)
2041 a->commits_needed--;
2043 errcode = pthread_mutex_lock(&tog_mutex);
2044 if (errcode) {
2045 err = got_error_set_errno(errcode,
2046 "pthread_mutex_lock");
2047 break;
2048 } else if (*a->quit)
2049 done = 1;
2050 else if (*a->first_displayed_entry == NULL) {
2051 *a->first_displayed_entry =
2052 TAILQ_FIRST(&a->commits->head);
2053 *a->selected_entry = *a->first_displayed_entry;
2056 errcode = pthread_cond_signal(&a->commit_loaded);
2057 if (errcode) {
2058 err = got_error_set_errno(errcode,
2059 "pthread_cond_signal");
2060 pthread_mutex_unlock(&tog_mutex);
2061 break;
2064 if (done)
2065 a->commits_needed = 0;
2066 else {
2067 if (a->commits_needed == 0 && !a->load_all) {
2068 errcode = pthread_cond_wait(&a->need_commits,
2069 &tog_mutex);
2070 if (errcode)
2071 err = got_error_set_errno(errcode,
2072 "pthread_cond_wait");
2073 if (*a->quit)
2074 done = 1;
2078 errcode = pthread_mutex_unlock(&tog_mutex);
2079 if (errcode && err == NULL)
2080 err = got_error_set_errno(errcode,
2081 "pthread_mutex_unlock");
2083 a->log_complete = 1;
2084 return (void *)err;
2087 static const struct got_error *
2088 stop_log_thread(struct tog_log_view_state *s)
2090 const struct got_error *err = NULL;
2091 int errcode;
2093 if (s->thread) {
2094 s->quit = 1;
2095 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2096 if (errcode)
2097 return got_error_set_errno(errcode,
2098 "pthread_cond_signal");
2099 errcode = pthread_mutex_unlock(&tog_mutex);
2100 if (errcode)
2101 return got_error_set_errno(errcode,
2102 "pthread_mutex_unlock");
2103 errcode = pthread_join(s->thread, (void **)&err);
2104 if (errcode)
2105 return got_error_set_errno(errcode, "pthread_join");
2106 errcode = pthread_mutex_lock(&tog_mutex);
2107 if (errcode)
2108 return got_error_set_errno(errcode,
2109 "pthread_mutex_lock");
2110 s->thread = 0; //NULL;
2113 if (s->thread_args.repo) {
2114 err = got_repo_close(s->thread_args.repo);
2115 s->thread_args.repo = NULL;
2118 if (s->thread_args.graph) {
2119 got_commit_graph_close(s->thread_args.graph);
2120 s->thread_args.graph = NULL;
2123 return err;
2126 static const struct got_error *
2127 close_log_view(struct tog_view *view)
2129 const struct got_error *err = NULL;
2130 struct tog_log_view_state *s = &view->state.log;
2131 int errcode;
2133 err = stop_log_thread(s);
2135 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2136 if (errcode && err == NULL)
2137 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2139 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2140 if (errcode && err == NULL)
2141 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2143 free_commits(&s->commits);
2144 free(s->in_repo_path);
2145 s->in_repo_path = NULL;
2146 free(s->start_id);
2147 s->start_id = NULL;
2148 free(s->head_ref_name);
2149 s->head_ref_name = NULL;
2150 return err;
2153 static const struct got_error *
2154 search_start_log_view(struct tog_view *view)
2156 struct tog_log_view_state *s = &view->state.log;
2158 s->matched_entry = NULL;
2159 s->search_entry = NULL;
2160 return NULL;
2163 static const struct got_error *
2164 search_next_log_view(struct tog_view *view)
2166 const struct got_error *err = NULL;
2167 struct tog_log_view_state *s = &view->state.log;
2168 struct commit_queue_entry *entry;
2170 /* Display progress update in log view. */
2171 show_log_view(view);
2172 update_panels();
2173 doupdate();
2175 if (s->search_entry) {
2176 int errcode, ch;
2177 errcode = pthread_mutex_unlock(&tog_mutex);
2178 if (errcode)
2179 return got_error_set_errno(errcode,
2180 "pthread_mutex_unlock");
2181 ch = wgetch(view->window);
2182 errcode = pthread_mutex_lock(&tog_mutex);
2183 if (errcode)
2184 return got_error_set_errno(errcode,
2185 "pthread_mutex_lock");
2186 if (ch == KEY_BACKSPACE) {
2187 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2188 return NULL;
2190 if (view->searching == TOG_SEARCH_FORWARD)
2191 entry = TAILQ_NEXT(s->search_entry, entry);
2192 else
2193 entry = TAILQ_PREV(s->search_entry,
2194 commit_queue_head, entry);
2195 } else if (s->matched_entry) {
2196 if (view->searching == TOG_SEARCH_FORWARD)
2197 entry = TAILQ_NEXT(s->matched_entry, entry);
2198 else
2199 entry = TAILQ_PREV(s->matched_entry,
2200 commit_queue_head, entry);
2201 } else {
2202 if (view->searching == TOG_SEARCH_FORWARD)
2203 entry = TAILQ_FIRST(&s->commits.head);
2204 else
2205 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2208 while (1) {
2209 int have_match = 0;
2211 if (entry == NULL) {
2212 if (s->thread_args.log_complete ||
2213 view->searching == TOG_SEARCH_BACKWARD) {
2214 view->search_next_done =
2215 (s->matched_entry == NULL ?
2216 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2217 s->search_entry = NULL;
2218 return NULL;
2221 * Poke the log thread for more commits and return,
2222 * allowing the main loop to make progress. Search
2223 * will resume at s->search_entry once we come back.
2225 s->thread_args.commits_needed++;
2226 return trigger_log_thread(view, 0);
2229 err = match_commit(&have_match, entry->id, entry->commit,
2230 &view->regex);
2231 if (err)
2232 break;
2233 if (have_match) {
2234 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2235 s->matched_entry = entry;
2236 break;
2239 s->search_entry = entry;
2240 if (view->searching == TOG_SEARCH_FORWARD)
2241 entry = TAILQ_NEXT(entry, entry);
2242 else
2243 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2246 if (s->matched_entry) {
2247 int cur = s->selected_entry->idx;
2248 while (cur < s->matched_entry->idx) {
2249 err = input_log_view(NULL, view, KEY_DOWN);
2250 if (err)
2251 return err;
2252 cur++;
2254 while (cur > s->matched_entry->idx) {
2255 err = input_log_view(NULL, view, KEY_UP);
2256 if (err)
2257 return err;
2258 cur--;
2262 s->search_entry = NULL;
2264 return NULL;
2267 static const struct got_error *
2268 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2269 struct got_repository *repo, const char *head_ref_name,
2270 const char *in_repo_path, int log_branches)
2272 const struct got_error *err = NULL;
2273 struct tog_log_view_state *s = &view->state.log;
2274 struct got_repository *thread_repo = NULL;
2275 struct got_commit_graph *thread_graph = NULL;
2276 int errcode;
2278 if (in_repo_path != s->in_repo_path) {
2279 free(s->in_repo_path);
2280 s->in_repo_path = strdup(in_repo_path);
2281 if (s->in_repo_path == NULL)
2282 return got_error_from_errno("strdup");
2285 /* The commit queue only contains commits being displayed. */
2286 TAILQ_INIT(&s->commits.head);
2287 s->commits.ncommits = 0;
2289 s->repo = repo;
2290 if (head_ref_name) {
2291 s->head_ref_name = strdup(head_ref_name);
2292 if (s->head_ref_name == NULL) {
2293 err = got_error_from_errno("strdup");
2294 goto done;
2297 s->start_id = got_object_id_dup(start_id);
2298 if (s->start_id == NULL) {
2299 err = got_error_from_errno("got_object_id_dup");
2300 goto done;
2302 s->log_branches = log_branches;
2304 STAILQ_INIT(&s->colors);
2305 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2306 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2307 get_color_value("TOG_COLOR_COMMIT"));
2308 if (err)
2309 goto done;
2310 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2311 get_color_value("TOG_COLOR_AUTHOR"));
2312 if (err) {
2313 free_colors(&s->colors);
2314 goto done;
2316 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2317 get_color_value("TOG_COLOR_DATE"));
2318 if (err) {
2319 free_colors(&s->colors);
2320 goto done;
2324 view->show = show_log_view;
2325 view->input = input_log_view;
2326 view->close = close_log_view;
2327 view->search_start = search_start_log_view;
2328 view->search_next = search_next_log_view;
2330 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2331 if (err)
2332 goto done;
2333 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2334 !s->log_branches);
2335 if (err)
2336 goto done;
2337 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2338 s->repo, NULL, NULL);
2339 if (err)
2340 goto done;
2342 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2343 if (errcode) {
2344 err = got_error_set_errno(errcode, "pthread_cond_init");
2345 goto done;
2347 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2348 if (errcode) {
2349 err = got_error_set_errno(errcode, "pthread_cond_init");
2350 goto done;
2353 s->thread_args.commits_needed = view->nlines;
2354 s->thread_args.graph = thread_graph;
2355 s->thread_args.commits = &s->commits;
2356 s->thread_args.in_repo_path = s->in_repo_path;
2357 s->thread_args.start_id = s->start_id;
2358 s->thread_args.repo = thread_repo;
2359 s->thread_args.log_complete = 0;
2360 s->thread_args.quit = &s->quit;
2361 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2362 s->thread_args.selected_entry = &s->selected_entry;
2363 s->thread_args.searching = &view->searching;
2364 s->thread_args.search_next_done = &view->search_next_done;
2365 s->thread_args.regex = &view->regex;
2366 done:
2367 if (err)
2368 close_log_view(view);
2369 return err;
2372 static const struct got_error *
2373 show_log_view(struct tog_view *view)
2375 const struct got_error *err;
2376 struct tog_log_view_state *s = &view->state.log;
2378 if (s->thread == 0) { //NULL) {
2379 int errcode = pthread_create(&s->thread, NULL, log_thread,
2380 &s->thread_args);
2381 if (errcode)
2382 return got_error_set_errno(errcode, "pthread_create");
2383 if (s->thread_args.commits_needed > 0) {
2384 err = trigger_log_thread(view, 1);
2385 if (err)
2386 return err;
2390 return draw_commits(view);
2393 static const struct got_error *
2394 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2396 const struct got_error *err = NULL;
2397 struct tog_log_view_state *s = &view->state.log;
2398 struct tog_view *diff_view = NULL, *tree_view = NULL;
2399 struct tog_view *ref_view = NULL;
2400 struct commit_queue_entry *entry;
2401 int begin_x = 0, n;
2403 if (s->thread_args.load_all) {
2404 if (ch == KEY_BACKSPACE)
2405 s->thread_args.load_all = 0;
2406 else if (s->thread_args.log_complete) {
2407 s->thread_args.load_all = 0;
2408 log_scroll_down(view, s->commits.ncommits);
2409 s->selected = MIN(view->nlines - 2,
2410 s->commits.ncommits - 1);
2411 select_commit(s);
2413 return NULL;
2416 switch (ch) {
2417 case 'q':
2418 s->quit = 1;
2419 break;
2420 case 'k':
2421 case KEY_UP:
2422 case '<':
2423 case ',':
2424 if (s->first_displayed_entry == NULL)
2425 break;
2426 if (s->selected > 0)
2427 s->selected--;
2428 else
2429 log_scroll_up(s, 1);
2430 select_commit(s);
2431 break;
2432 case 'g':
2433 case KEY_HOME:
2434 s->selected = 0;
2435 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2436 select_commit(s);
2437 break;
2438 case KEY_PPAGE:
2439 case CTRL('b'):
2440 if (s->first_displayed_entry == NULL)
2441 break;
2442 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2443 s->selected = 0;
2444 else
2445 log_scroll_up(s, view->nlines - 1);
2446 select_commit(s);
2447 break;
2448 case 'j':
2449 case KEY_DOWN:
2450 case '>':
2451 case '.':
2452 if (s->first_displayed_entry == NULL)
2453 break;
2454 if (s->selected < MIN(view->nlines - 2,
2455 s->commits.ncommits - 1))
2456 s->selected++;
2457 else {
2458 err = log_scroll_down(view, 1);
2459 if (err)
2460 break;
2462 select_commit(s);
2463 break;
2464 case 'G':
2465 case KEY_END: {
2466 /* We don't know yet how many commits, so we're forced to
2467 * traverse them all. */
2468 if (!s->thread_args.log_complete) {
2469 s->thread_args.load_all = 1;
2470 return trigger_log_thread(view, 0);
2473 s->selected = 0;
2474 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2475 for (n = 0; n < view->nlines - 1; n++) {
2476 if (entry == NULL)
2477 break;
2478 s->first_displayed_entry = entry;
2479 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2481 if (n > 0)
2482 s->selected = n - 1;
2483 select_commit(s);
2484 break;
2486 case KEY_NPAGE:
2487 case CTRL('f'): {
2488 struct commit_queue_entry *first;
2489 first = s->first_displayed_entry;
2490 if (first == NULL)
2491 break;
2492 err = log_scroll_down(view, view->nlines - 1);
2493 if (err)
2494 break;
2495 if (first == s->first_displayed_entry &&
2496 s->selected < MIN(view->nlines - 2,
2497 s->commits.ncommits - 1)) {
2498 /* can't scroll further down */
2499 s->selected = MIN(view->nlines - 2,
2500 s->commits.ncommits - 1);
2502 select_commit(s);
2503 break;
2505 case KEY_RESIZE:
2506 if (s->selected > view->nlines - 2)
2507 s->selected = view->nlines - 2;
2508 if (s->selected > s->commits.ncommits - 1)
2509 s->selected = s->commits.ncommits - 1;
2510 select_commit(s);
2511 if (s->commits.ncommits < view->nlines - 1 &&
2512 !s->thread_args.log_complete) {
2513 s->thread_args.commits_needed += (view->nlines - 1) -
2514 s->commits.ncommits;
2515 err = trigger_log_thread(view, 1);
2517 break;
2518 case KEY_ENTER:
2519 case ' ':
2520 case '\r':
2521 if (s->selected_entry == NULL)
2522 break;
2523 if (view_is_parent_view(view))
2524 begin_x = view_split_begin_x(view->begin_x);
2525 err = open_diff_view_for_commit(&diff_view, begin_x,
2526 s->selected_entry->commit, s->selected_entry->id,
2527 view, s->repo);
2528 if (err)
2529 break;
2530 view->focussed = 0;
2531 diff_view->focussed = 1;
2532 if (view_is_parent_view(view)) {
2533 err = view_close_child(view);
2534 if (err)
2535 return err;
2536 view_set_child(view, diff_view);
2537 view->focus_child = 1;
2538 } else
2539 *new_view = diff_view;
2540 break;
2541 case 't':
2542 if (s->selected_entry == NULL)
2543 break;
2544 if (view_is_parent_view(view))
2545 begin_x = view_split_begin_x(view->begin_x);
2546 err = browse_commit_tree(&tree_view, begin_x,
2547 s->selected_entry, s->in_repo_path, s->head_ref_name,
2548 s->repo);
2549 if (err)
2550 break;
2551 view->focussed = 0;
2552 tree_view->focussed = 1;
2553 if (view_is_parent_view(view)) {
2554 err = view_close_child(view);
2555 if (err)
2556 return err;
2557 view_set_child(view, tree_view);
2558 view->focus_child = 1;
2559 } else
2560 *new_view = tree_view;
2561 break;
2562 case KEY_BACKSPACE:
2563 case CTRL('l'):
2564 case 'B':
2565 if (ch == KEY_BACKSPACE &&
2566 got_path_is_root_dir(s->in_repo_path))
2567 break;
2568 err = stop_log_thread(s);
2569 if (err)
2570 return err;
2571 if (ch == KEY_BACKSPACE) {
2572 char *parent_path;
2573 err = got_path_dirname(&parent_path, s->in_repo_path);
2574 if (err)
2575 return err;
2576 free(s->in_repo_path);
2577 s->in_repo_path = parent_path;
2578 s->thread_args.in_repo_path = s->in_repo_path;
2579 } else if (ch == CTRL('l')) {
2580 struct got_object_id *start_id;
2581 err = got_repo_match_object_id(&start_id, NULL,
2582 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2583 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2584 if (err)
2585 return err;
2586 free(s->start_id);
2587 s->start_id = start_id;
2588 s->thread_args.start_id = s->start_id;
2589 } else /* 'B' */
2590 s->log_branches = !s->log_branches;
2592 err = got_repo_open(&s->thread_args.repo,
2593 got_repo_get_path(s->repo), NULL);
2594 if (err)
2595 return err;
2596 tog_free_refs();
2597 err = tog_load_refs(s->repo);
2598 if (err)
2599 return err;
2600 err = got_commit_graph_open(&s->thread_args.graph,
2601 s->in_repo_path, !s->log_branches);
2602 if (err)
2603 return err;
2604 err = got_commit_graph_iter_start(s->thread_args.graph,
2605 s->start_id, s->repo, NULL, NULL);
2606 if (err)
2607 return err;
2608 free_commits(&s->commits);
2609 s->first_displayed_entry = NULL;
2610 s->last_displayed_entry = NULL;
2611 s->selected_entry = NULL;
2612 s->selected = 0;
2613 s->thread_args.log_complete = 0;
2614 s->quit = 0;
2615 s->thread_args.commits_needed = view->nlines;
2616 break;
2617 case 'r':
2618 if (view_is_parent_view(view))
2619 begin_x = view_split_begin_x(view->begin_x);
2620 ref_view = view_open(view->nlines, view->ncols,
2621 view->begin_y, begin_x, TOG_VIEW_REF);
2622 if (ref_view == NULL)
2623 return got_error_from_errno("view_open");
2624 err = open_ref_view(ref_view, s->repo);
2625 if (err) {
2626 view_close(ref_view);
2627 return err;
2629 view->focussed = 0;
2630 ref_view->focussed = 1;
2631 if (view_is_parent_view(view)) {
2632 err = view_close_child(view);
2633 if (err)
2634 return err;
2635 view_set_child(view, ref_view);
2636 view->focus_child = 1;
2637 } else
2638 *new_view = ref_view;
2639 break;
2640 default:
2641 break;
2644 return err;
2647 static const struct got_error *
2648 apply_unveil(const char *repo_path, const char *worktree_path)
2650 const struct got_error *error;
2652 #ifdef PROFILE
2653 if (unveil("gmon.out", "rwc") != 0)
2654 return got_error_from_errno2("unveil", "gmon.out");
2655 #endif
2656 if (repo_path && unveil(repo_path, "r") != 0)
2657 return got_error_from_errno2("unveil", repo_path);
2659 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2660 return got_error_from_errno2("unveil", worktree_path);
2662 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2663 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2665 error = got_privsep_unveil_exec_helpers();
2666 if (error != NULL)
2667 return error;
2669 if (unveil(NULL, NULL) != 0)
2670 return got_error_from_errno("unveil");
2672 return NULL;
2675 static void
2676 init_curses(void)
2678 initscr();
2679 cbreak();
2680 halfdelay(1); /* Do fast refresh while initial view is loading. */
2681 noecho();
2682 nonl();
2683 intrflush(stdscr, FALSE);
2684 keypad(stdscr, TRUE);
2685 curs_set(0);
2686 if (getenv("TOG_COLORS") != NULL) {
2687 start_color();
2688 use_default_colors();
2690 signal(SIGWINCH, tog_sigwinch);
2691 signal(SIGPIPE, tog_sigpipe);
2692 signal(SIGCONT, tog_sigcont);
2695 static const struct got_error *
2696 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2697 struct got_repository *repo, struct got_worktree *worktree)
2699 const struct got_error *err = NULL;
2701 if (argc == 0) {
2702 *in_repo_path = strdup("/");
2703 if (*in_repo_path == NULL)
2704 return got_error_from_errno("strdup");
2705 return NULL;
2708 if (worktree) {
2709 const char *prefix = got_worktree_get_path_prefix(worktree);
2710 char *p;
2712 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2713 if (err)
2714 return err;
2715 if (asprintf(in_repo_path, "%s%s%s", prefix,
2716 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2717 p) == -1) {
2718 err = got_error_from_errno("asprintf");
2719 *in_repo_path = NULL;
2721 free(p);
2722 } else
2723 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2725 return err;
2728 static const struct got_error *
2729 cmd_log(int argc, char *argv[])
2731 const struct got_error *error;
2732 struct got_repository *repo = NULL;
2733 struct got_worktree *worktree = NULL;
2734 struct got_object_id *start_id = NULL;
2735 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2736 char *start_commit = NULL, *label = NULL;
2737 struct got_reference *ref = NULL;
2738 const char *head_ref_name = NULL;
2739 int ch, log_branches = 0;
2740 struct tog_view *view;
2742 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2743 switch (ch) {
2744 case 'b':
2745 log_branches = 1;
2746 break;
2747 case 'c':
2748 start_commit = optarg;
2749 break;
2750 case 'r':
2751 repo_path = realpath(optarg, NULL);
2752 if (repo_path == NULL)
2753 return got_error_from_errno2("realpath",
2754 optarg);
2755 break;
2756 default:
2757 usage_log();
2758 /* NOTREACHED */
2762 argc -= optind;
2763 argv += optind;
2765 if (argc > 1)
2766 usage_log();
2768 if (repo_path == NULL) {
2769 cwd = getcwd(NULL, 0);
2770 if (cwd == NULL)
2771 return got_error_from_errno("getcwd");
2772 error = got_worktree_open(&worktree, cwd);
2773 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2774 goto done;
2775 if (worktree)
2776 repo_path =
2777 strdup(got_worktree_get_repo_path(worktree));
2778 else
2779 repo_path = strdup(cwd);
2780 if (repo_path == NULL) {
2781 error = got_error_from_errno("strdup");
2782 goto done;
2786 error = got_repo_open(&repo, repo_path, NULL);
2787 if (error != NULL)
2788 goto done;
2790 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2791 repo, worktree);
2792 if (error)
2793 goto done;
2795 init_curses();
2797 error = apply_unveil(got_repo_get_path(repo),
2798 worktree ? got_worktree_get_root_path(worktree) : NULL);
2799 if (error)
2800 goto done;
2802 /* already loaded by tog_log_with_path()? */
2803 if (TAILQ_EMPTY(&tog_refs)) {
2804 error = tog_load_refs(repo);
2805 if (error)
2806 goto done;
2809 if (start_commit == NULL) {
2810 error = got_repo_match_object_id(&start_id, &label,
2811 worktree ? got_worktree_get_head_ref_name(worktree) :
2812 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2813 if (error)
2814 goto done;
2815 head_ref_name = label;
2816 } else {
2817 error = got_ref_open(&ref, repo, start_commit, 0);
2818 if (error == NULL)
2819 head_ref_name = got_ref_get_name(ref);
2820 else if (error->code != GOT_ERR_NOT_REF)
2821 goto done;
2822 error = got_repo_match_object_id(&start_id, NULL,
2823 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2824 if (error)
2825 goto done;
2828 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2829 if (view == NULL) {
2830 error = got_error_from_errno("view_open");
2831 goto done;
2833 error = open_log_view(view, start_id, repo, head_ref_name,
2834 in_repo_path, log_branches);
2835 if (error)
2836 goto done;
2837 if (worktree) {
2838 /* Release work tree lock. */
2839 got_worktree_close(worktree);
2840 worktree = NULL;
2842 error = view_loop(view);
2843 done:
2844 free(in_repo_path);
2845 free(repo_path);
2846 free(cwd);
2847 free(start_id);
2848 free(label);
2849 if (ref)
2850 got_ref_close(ref);
2851 if (repo) {
2852 const struct got_error *close_err = got_repo_close(repo);
2853 if (error == NULL)
2854 error = close_err;
2856 if (worktree)
2857 got_worktree_close(worktree);
2858 tog_free_refs();
2859 return error;
2862 __dead static void
2863 usage_diff(void)
2865 endwin();
2866 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2867 "[-w] object1 object2\n", getprogname());
2868 exit(1);
2871 static int
2872 match_line(const char *line, regex_t *regex, size_t nmatch,
2873 regmatch_t *regmatch)
2875 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2878 struct tog_color *
2879 match_color(struct tog_colors *colors, const char *line)
2881 struct tog_color *tc = NULL;
2883 STAILQ_FOREACH(tc, colors, entry) {
2884 if (match_line(line, &tc->regex, 0, NULL))
2885 return tc;
2888 return NULL;
2891 static const struct got_error *
2892 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2893 WINDOW *window, regmatch_t *regmatch)
2895 const struct got_error *err = NULL;
2896 wchar_t *wline;
2897 int width;
2898 char *s;
2900 *wtotal = 0;
2902 s = strndup(line, regmatch->rm_so);
2903 if (s == NULL)
2904 return got_error_from_errno("strndup");
2906 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2907 if (err) {
2908 free(s);
2909 return err;
2911 waddwstr(window, wline);
2912 free(wline);
2913 free(s);
2914 wlimit -= width;
2915 *wtotal += width;
2917 if (wlimit > 0) {
2918 s = strndup(line + regmatch->rm_so,
2919 regmatch->rm_eo - regmatch->rm_so);
2920 if (s == NULL) {
2921 err = got_error_from_errno("strndup");
2922 free(s);
2923 return err;
2925 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2926 if (err) {
2927 free(s);
2928 return err;
2930 wattr_on(window, A_STANDOUT, NULL);
2931 waddwstr(window, wline);
2932 wattr_off(window, A_STANDOUT, NULL);
2933 free(wline);
2934 free(s);
2935 wlimit -= width;
2936 *wtotal += width;
2939 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2940 err = format_line(&wline, &width,
2941 line + regmatch->rm_eo, wlimit, col_tab_align);
2942 if (err)
2943 return err;
2944 waddwstr(window, wline);
2945 free(wline);
2946 *wtotal += width;
2949 return NULL;
2952 static const struct got_error *
2953 draw_file(struct tog_view *view, const char *header)
2955 struct tog_diff_view_state *s = &view->state.diff;
2956 regmatch_t *regmatch = &view->regmatch;
2957 const struct got_error *err;
2958 int nprinted = 0;
2959 char *line;
2960 size_t linesize = 0;
2961 ssize_t linelen;
2962 struct tog_color *tc;
2963 wchar_t *wline;
2964 int width;
2965 int max_lines = view->nlines;
2966 int nlines = s->nlines;
2967 off_t line_offset;
2969 line_offset = s->line_offsets[s->first_displayed_line - 1];
2970 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2971 return got_error_from_errno("fseek");
2973 werase(view->window);
2975 if (header) {
2976 if (asprintf(&line, "[%d/%d] %s",
2977 s->first_displayed_line - 1 + s->selected_line, nlines,
2978 header) == -1)
2979 return got_error_from_errno("asprintf");
2980 err = format_line(&wline, &width, line, view->ncols, 0);
2981 free(line);
2982 if (err)
2983 return err;
2985 if (view_needs_focus_indication(view))
2986 wstandout(view->window);
2987 waddwstr(view->window, wline);
2988 free(wline);
2989 wline = NULL;
2990 if (view_needs_focus_indication(view))
2991 wstandend(view->window);
2992 if (width <= view->ncols - 1)
2993 waddch(view->window, '\n');
2995 if (max_lines <= 1)
2996 return NULL;
2997 max_lines--;
3000 s->eof = 0;
3001 line = NULL;
3002 while (max_lines > 0 && nprinted < max_lines) {
3003 linelen = getline(&line, &linesize, s->f);
3004 if (linelen == -1) {
3005 if (feof(s->f)) {
3006 s->eof = 1;
3007 break;
3009 free(line);
3010 return got_ferror(s->f, GOT_ERR_IO);
3013 tc = match_color(&s->colors, line);
3014 if (tc)
3015 wattr_on(view->window,
3016 COLOR_PAIR(tc->colorpair), NULL);
3017 if (s->first_displayed_line + nprinted == s->matched_line &&
3018 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3019 err = add_matched_line(&width, line, view->ncols, 0,
3020 view->window, regmatch);
3021 if (err) {
3022 free(line);
3023 return err;
3025 } else {
3026 err = format_line(&wline, &width, line, view->ncols, 0);
3027 if (err) {
3028 free(line);
3029 return err;
3031 waddwstr(view->window, wline);
3032 free(wline);
3033 wline = NULL;
3035 if (tc)
3036 wattr_off(view->window,
3037 COLOR_PAIR(tc->colorpair), NULL);
3038 if (width <= view->ncols - 1)
3039 waddch(view->window, '\n');
3040 nprinted++;
3042 free(line);
3043 if (nprinted >= 1)
3044 s->last_displayed_line = s->first_displayed_line +
3045 (nprinted - 1);
3046 else
3047 s->last_displayed_line = s->first_displayed_line;
3049 view_vborder(view);
3051 if (s->eof) {
3052 while (nprinted < view->nlines) {
3053 waddch(view->window, '\n');
3054 nprinted++;
3057 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3058 if (err) {
3059 return err;
3062 wstandout(view->window);
3063 waddwstr(view->window, wline);
3064 free(wline);
3065 wline = NULL;
3066 wstandend(view->window);
3069 return NULL;
3072 static char *
3073 get_datestr(time_t *time, char *datebuf)
3075 struct tm mytm, *tm;
3076 char *p, *s;
3078 tm = gmtime_r(time, &mytm);
3079 if (tm == NULL)
3080 return NULL;
3081 s = asctime_r(tm, datebuf);
3082 if (s == NULL)
3083 return NULL;
3084 p = strchr(s, '\n');
3085 if (p)
3086 *p = '\0';
3087 return s;
3090 static const struct got_error *
3091 get_changed_paths(struct got_pathlist_head *paths,
3092 struct got_commit_object *commit, struct got_repository *repo)
3094 const struct got_error *err = NULL;
3095 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3096 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3097 struct got_object_qid *qid;
3099 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3100 if (qid != NULL) {
3101 struct got_commit_object *pcommit;
3102 err = got_object_open_as_commit(&pcommit, repo,
3103 qid->id);
3104 if (err)
3105 return err;
3107 tree_id1 = got_object_id_dup(
3108 got_object_commit_get_tree_id(pcommit));
3109 if (tree_id1 == NULL) {
3110 got_object_commit_close(pcommit);
3111 return got_error_from_errno("got_object_id_dup");
3113 got_object_commit_close(pcommit);
3117 if (tree_id1) {
3118 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3119 if (err)
3120 goto done;
3123 tree_id2 = got_object_commit_get_tree_id(commit);
3124 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3125 if (err)
3126 goto done;
3128 err = got_diff_tree(tree1, tree2, "", "", repo,
3129 got_diff_tree_collect_changed_paths, paths, 0);
3130 done:
3131 if (tree1)
3132 got_object_tree_close(tree1);
3133 if (tree2)
3134 got_object_tree_close(tree2);
3135 free(tree_id1);
3136 return err;
3139 static const struct got_error *
3140 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3142 off_t *p;
3144 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3145 if (p == NULL)
3146 return got_error_from_errno("reallocarray");
3147 *line_offsets = p;
3148 (*line_offsets)[*nlines] = off;
3149 (*nlines)++;
3150 return NULL;
3153 static const struct got_error *
3154 write_commit_info(off_t **line_offsets, size_t *nlines,
3155 struct got_object_id *commit_id, struct got_reflist_head *refs,
3156 struct got_repository *repo, FILE *outfile)
3158 const struct got_error *err = NULL;
3159 char datebuf[26], *datestr;
3160 struct got_commit_object *commit;
3161 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3162 time_t committer_time;
3163 const char *author, *committer;
3164 char *refs_str = NULL;
3165 struct got_pathlist_head changed_paths;
3166 struct got_pathlist_entry *pe;
3167 off_t outoff = 0;
3168 int n;
3170 TAILQ_INIT(&changed_paths);
3172 if (refs) {
3173 err = build_refs_str(&refs_str, refs, commit_id, repo);
3174 if (err)
3175 return err;
3178 err = got_object_open_as_commit(&commit, repo, commit_id);
3179 if (err)
3180 return err;
3182 err = got_object_id_str(&id_str, commit_id);
3183 if (err) {
3184 err = got_error_from_errno("got_object_id_str");
3185 goto done;
3188 err = add_line_offset(line_offsets, nlines, 0);
3189 if (err)
3190 goto done;
3192 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3193 refs_str ? refs_str : "", refs_str ? ")" : "");
3194 if (n < 0) {
3195 err = got_error_from_errno("fprintf");
3196 goto done;
3198 outoff += n;
3199 err = add_line_offset(line_offsets, nlines, outoff);
3200 if (err)
3201 goto done;
3203 n = fprintf(outfile, "from: %s\n",
3204 got_object_commit_get_author(commit));
3205 if (n < 0) {
3206 err = got_error_from_errno("fprintf");
3207 goto done;
3209 outoff += n;
3210 err = add_line_offset(line_offsets, nlines, outoff);
3211 if (err)
3212 goto done;
3214 committer_time = got_object_commit_get_committer_time(commit);
3215 datestr = get_datestr(&committer_time, datebuf);
3216 if (datestr) {
3217 n = fprintf(outfile, "date: %s UTC\n", datestr);
3218 if (n < 0) {
3219 err = got_error_from_errno("fprintf");
3220 goto done;
3222 outoff += n;
3223 err = add_line_offset(line_offsets, nlines, outoff);
3224 if (err)
3225 goto done;
3227 author = got_object_commit_get_author(commit);
3228 committer = got_object_commit_get_committer(commit);
3229 if (strcmp(author, committer) != 0) {
3230 n = fprintf(outfile, "via: %s\n", committer);
3231 if (n < 0) {
3232 err = got_error_from_errno("fprintf");
3233 goto done;
3235 outoff += n;
3236 err = add_line_offset(line_offsets, nlines, outoff);
3237 if (err)
3238 goto done;
3240 if (got_object_commit_get_nparents(commit) > 1) {
3241 const struct got_object_id_queue *parent_ids;
3242 struct got_object_qid *qid;
3243 int pn = 1;
3244 parent_ids = got_object_commit_get_parent_ids(commit);
3245 STAILQ_FOREACH(qid, parent_ids, entry) {
3246 err = got_object_id_str(&id_str, qid->id);
3247 if (err)
3248 goto done;
3249 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3250 if (n < 0) {
3251 err = got_error_from_errno("fprintf");
3252 goto done;
3254 outoff += n;
3255 err = add_line_offset(line_offsets, nlines, outoff);
3256 if (err)
3257 goto done;
3258 free(id_str);
3259 id_str = NULL;
3263 err = got_object_commit_get_logmsg(&logmsg, commit);
3264 if (err)
3265 goto done;
3266 s = logmsg;
3267 while ((line = strsep(&s, "\n")) != NULL) {
3268 n = fprintf(outfile, "%s\n", line);
3269 if (n < 0) {
3270 err = got_error_from_errno("fprintf");
3271 goto done;
3273 outoff += n;
3274 err = add_line_offset(line_offsets, nlines, outoff);
3275 if (err)
3276 goto done;
3279 err = get_changed_paths(&changed_paths, commit, repo);
3280 if (err)
3281 goto done;
3282 TAILQ_FOREACH(pe, &changed_paths, entry) {
3283 struct got_diff_changed_path *cp = pe->data;
3284 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3285 if (n < 0) {
3286 err = got_error_from_errno("fprintf");
3287 goto done;
3289 outoff += n;
3290 err = add_line_offset(line_offsets, nlines, outoff);
3291 if (err)
3292 goto done;
3293 free((char *)pe->path);
3294 free(pe->data);
3297 fputc('\n', outfile);
3298 outoff++;
3299 err = add_line_offset(line_offsets, nlines, outoff);
3300 done:
3301 got_pathlist_free(&changed_paths);
3302 free(id_str);
3303 free(logmsg);
3304 free(refs_str);
3305 got_object_commit_close(commit);
3306 if (err) {
3307 free(*line_offsets);
3308 *line_offsets = NULL;
3309 *nlines = 0;
3311 return err;
3314 static const struct got_error *
3315 create_diff(struct tog_diff_view_state *s)
3317 const struct got_error *err = NULL;
3318 FILE *f = NULL;
3319 int obj_type;
3321 free(s->line_offsets);
3322 s->line_offsets = malloc(sizeof(off_t));
3323 if (s->line_offsets == NULL)
3324 return got_error_from_errno("malloc");
3325 s->nlines = 0;
3327 f = got_opentemp();
3328 if (f == NULL) {
3329 err = got_error_from_errno("got_opentemp");
3330 goto done;
3332 if (s->f && fclose(s->f) == EOF) {
3333 err = got_error_from_errno("fclose");
3334 goto done;
3336 s->f = f;
3338 if (s->id1)
3339 err = got_object_get_type(&obj_type, s->repo, s->id1);
3340 else
3341 err = got_object_get_type(&obj_type, s->repo, s->id2);
3342 if (err)
3343 goto done;
3345 switch (obj_type) {
3346 case GOT_OBJ_TYPE_BLOB:
3347 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3348 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3349 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3350 break;
3351 case GOT_OBJ_TYPE_TREE:
3352 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3353 s->id1, s->id2, NULL, "", "", s->diff_context,
3354 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3355 break;
3356 case GOT_OBJ_TYPE_COMMIT: {
3357 const struct got_object_id_queue *parent_ids;
3358 struct got_object_qid *pid;
3359 struct got_commit_object *commit2;
3360 struct got_reflist_head *refs;
3362 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3363 if (err)
3364 goto done;
3365 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3366 /* Show commit info if we're diffing to a parent/root commit. */
3367 if (s->id1 == NULL) {
3368 err = write_commit_info(&s->line_offsets, &s->nlines,
3369 s->id2, refs, s->repo, s->f);
3370 if (err)
3371 goto done;
3372 } else {
3373 parent_ids = got_object_commit_get_parent_ids(commit2);
3374 STAILQ_FOREACH(pid, parent_ids, entry) {
3375 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3376 err = write_commit_info(
3377 &s->line_offsets, &s->nlines,
3378 s->id2, refs, s->repo, s->f);
3379 if (err)
3380 goto done;
3381 break;
3385 got_object_commit_close(commit2);
3387 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3388 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3389 s->force_text_diff, s->repo, s->f);
3390 break;
3392 default:
3393 err = got_error(GOT_ERR_OBJ_TYPE);
3394 break;
3396 if (err)
3397 goto done;
3398 done:
3399 if (s->f && fflush(s->f) != 0 && err == NULL)
3400 err = got_error_from_errno("fflush");
3401 return err;
3404 static void
3405 diff_view_indicate_progress(struct tog_view *view)
3407 mvwaddstr(view->window, 0, 0, "diffing...");
3408 update_panels();
3409 doupdate();
3412 static const struct got_error *
3413 search_start_diff_view(struct tog_view *view)
3415 struct tog_diff_view_state *s = &view->state.diff;
3417 s->matched_line = 0;
3418 return NULL;
3421 static const struct got_error *
3422 search_next_diff_view(struct tog_view *view)
3424 struct tog_diff_view_state *s = &view->state.diff;
3425 int lineno;
3426 char *line = NULL;
3427 size_t linesize = 0;
3428 ssize_t linelen;
3430 if (!view->searching) {
3431 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3432 return NULL;
3435 if (s->matched_line) {
3436 if (view->searching == TOG_SEARCH_FORWARD)
3437 lineno = s->matched_line + 1;
3438 else
3439 lineno = s->matched_line - 1;
3440 } else {
3441 if (view->searching == TOG_SEARCH_FORWARD)
3442 lineno = 1;
3443 else
3444 lineno = s->nlines;
3447 while (1) {
3448 off_t offset;
3450 if (lineno <= 0 || lineno > s->nlines) {
3451 if (s->matched_line == 0) {
3452 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3453 break;
3456 if (view->searching == TOG_SEARCH_FORWARD)
3457 lineno = 1;
3458 else
3459 lineno = s->nlines;
3462 offset = s->line_offsets[lineno - 1];
3463 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3464 free(line);
3465 return got_error_from_errno("fseeko");
3467 linelen = getline(&line, &linesize, s->f);
3468 if (linelen != -1 &&
3469 match_line(line, &view->regex, 1, &view->regmatch)) {
3470 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3471 s->matched_line = lineno;
3472 break;
3474 if (view->searching == TOG_SEARCH_FORWARD)
3475 lineno++;
3476 else
3477 lineno--;
3479 free(line);
3481 if (s->matched_line) {
3482 s->first_displayed_line = s->matched_line;
3483 s->selected_line = 1;
3486 return NULL;
3489 static const struct got_error *
3490 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3491 struct got_object_id *id2, const char *label1, const char *label2,
3492 int diff_context, int ignore_whitespace, int force_text_diff,
3493 struct tog_view *log_view, struct got_repository *repo)
3495 const struct got_error *err;
3496 struct tog_diff_view_state *s = &view->state.diff;
3498 if (id1 != NULL && id2 != NULL) {
3499 int type1, type2;
3500 err = got_object_get_type(&type1, repo, id1);
3501 if (err)
3502 return err;
3503 err = got_object_get_type(&type2, repo, id2);
3504 if (err)
3505 return err;
3507 if (type1 != type2)
3508 return got_error(GOT_ERR_OBJ_TYPE);
3510 s->first_displayed_line = 1;
3511 s->last_displayed_line = view->nlines;
3512 s->selected_line = 1;
3513 s->repo = repo;
3514 s->id1 = id1;
3515 s->id2 = id2;
3516 s->label1 = label1;
3517 s->label2 = label2;
3519 if (id1) {
3520 s->id1 = got_object_id_dup(id1);
3521 if (s->id1 == NULL)
3522 return got_error_from_errno("got_object_id_dup");
3523 } else
3524 s->id1 = NULL;
3526 s->id2 = got_object_id_dup(id2);
3527 if (s->id2 == NULL) {
3528 free(s->id1);
3529 s->id1 = NULL;
3530 return got_error_from_errno("got_object_id_dup");
3532 s->f = NULL;
3533 s->first_displayed_line = 1;
3534 s->last_displayed_line = view->nlines;
3535 s->diff_context = diff_context;
3536 s->ignore_whitespace = ignore_whitespace;
3537 s->force_text_diff = force_text_diff;
3538 s->log_view = log_view;
3539 s->repo = repo;
3541 STAILQ_INIT(&s->colors);
3542 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3543 err = add_color(&s->colors,
3544 "^-", TOG_COLOR_DIFF_MINUS,
3545 get_color_value("TOG_COLOR_DIFF_MINUS"));
3546 if (err)
3547 return err;
3548 err = add_color(&s->colors, "^\\+",
3549 TOG_COLOR_DIFF_PLUS,
3550 get_color_value("TOG_COLOR_DIFF_PLUS"));
3551 if (err) {
3552 free_colors(&s->colors);
3553 return err;
3555 err = add_color(&s->colors,
3556 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3557 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3558 if (err) {
3559 free_colors(&s->colors);
3560 return err;
3563 err = add_color(&s->colors,
3564 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3565 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3566 get_color_value("TOG_COLOR_DIFF_META"));
3567 if (err) {
3568 free_colors(&s->colors);
3569 return err;
3572 err = add_color(&s->colors,
3573 "^(from|via): ", TOG_COLOR_AUTHOR,
3574 get_color_value("TOG_COLOR_AUTHOR"));
3575 if (err) {
3576 free_colors(&s->colors);
3577 return err;
3580 err = add_color(&s->colors,
3581 "^date: ", TOG_COLOR_DATE,
3582 get_color_value("TOG_COLOR_DATE"));
3583 if (err) {
3584 free_colors(&s->colors);
3585 return err;
3589 if (log_view && view_is_splitscreen(view))
3590 show_log_view(log_view); /* draw vborder */
3591 diff_view_indicate_progress(view);
3593 s->line_offsets = NULL;
3594 s->nlines = 0;
3595 err = create_diff(s);
3596 if (err) {
3597 free(s->id1);
3598 s->id1 = NULL;
3599 free(s->id2);
3600 s->id2 = NULL;
3601 free_colors(&s->colors);
3602 return err;
3605 view->show = show_diff_view;
3606 view->input = input_diff_view;
3607 view->close = close_diff_view;
3608 view->search_start = search_start_diff_view;
3609 view->search_next = search_next_diff_view;
3611 return NULL;
3614 static const struct got_error *
3615 close_diff_view(struct tog_view *view)
3617 const struct got_error *err = NULL;
3618 struct tog_diff_view_state *s = &view->state.diff;
3620 free(s->id1);
3621 s->id1 = NULL;
3622 free(s->id2);
3623 s->id2 = NULL;
3624 if (s->f && fclose(s->f) == EOF)
3625 err = got_error_from_errno("fclose");
3626 free_colors(&s->colors);
3627 free(s->line_offsets);
3628 s->line_offsets = NULL;
3629 s->nlines = 0;
3630 return err;
3633 static const struct got_error *
3634 show_diff_view(struct tog_view *view)
3636 const struct got_error *err;
3637 struct tog_diff_view_state *s = &view->state.diff;
3638 char *id_str1 = NULL, *id_str2, *header;
3639 const char *label1, *label2;
3641 if (s->id1) {
3642 err = got_object_id_str(&id_str1, s->id1);
3643 if (err)
3644 return err;
3645 label1 = s->label1 ? : id_str1;
3646 } else
3647 label1 = "/dev/null";
3649 err = got_object_id_str(&id_str2, s->id2);
3650 if (err)
3651 return err;
3652 label2 = s->label2 ? : id_str2;
3654 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3655 err = got_error_from_errno("asprintf");
3656 free(id_str1);
3657 free(id_str2);
3658 return err;
3660 free(id_str1);
3661 free(id_str2);
3663 err = draw_file(view, header);
3664 free(header);
3665 return err;
3668 static const struct got_error *
3669 set_selected_commit(struct tog_diff_view_state *s,
3670 struct commit_queue_entry *entry)
3672 const struct got_error *err;
3673 const struct got_object_id_queue *parent_ids;
3674 struct got_commit_object *selected_commit;
3675 struct got_object_qid *pid;
3677 free(s->id2);
3678 s->id2 = got_object_id_dup(entry->id);
3679 if (s->id2 == NULL)
3680 return got_error_from_errno("got_object_id_dup");
3682 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3683 if (err)
3684 return err;
3685 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3686 free(s->id1);
3687 pid = STAILQ_FIRST(parent_ids);
3688 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3689 got_object_commit_close(selected_commit);
3690 return NULL;
3693 static const struct got_error *
3694 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3696 const struct got_error *err = NULL;
3697 struct tog_diff_view_state *s = &view->state.diff;
3698 struct tog_log_view_state *ls;
3699 struct commit_queue_entry *old_selected_entry;
3700 char *line = NULL;
3701 size_t linesize = 0;
3702 ssize_t linelen;
3703 int i;
3705 switch (ch) {
3706 case 'a':
3707 case 'w':
3708 if (ch == 'a')
3709 s->force_text_diff = !s->force_text_diff;
3710 if (ch == 'w')
3711 s->ignore_whitespace = !s->ignore_whitespace;
3712 wclear(view->window);
3713 s->first_displayed_line = 1;
3714 s->last_displayed_line = view->nlines;
3715 diff_view_indicate_progress(view);
3716 err = create_diff(s);
3717 break;
3718 case 'g':
3719 case KEY_HOME:
3720 s->first_displayed_line = 1;
3721 break;
3722 case 'G':
3723 case KEY_END:
3724 if (s->eof)
3725 break;
3727 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3728 s->eof = 1;
3729 break;
3730 case 'k':
3731 case KEY_UP:
3732 if (s->first_displayed_line > 1)
3733 s->first_displayed_line--;
3734 break;
3735 case KEY_PPAGE:
3736 case CTRL('b'):
3737 if (s->first_displayed_line == 1)
3738 break;
3739 i = 0;
3740 while (i++ < view->nlines - 1 &&
3741 s->first_displayed_line > 1)
3742 s->first_displayed_line--;
3743 break;
3744 case 'j':
3745 case KEY_DOWN:
3746 if (!s->eof)
3747 s->first_displayed_line++;
3748 break;
3749 case KEY_NPAGE:
3750 case CTRL('f'):
3751 case ' ':
3752 if (s->eof)
3753 break;
3754 i = 0;
3755 while (!s->eof && i++ < view->nlines - 1) {
3756 linelen = getline(&line, &linesize, s->f);
3757 s->first_displayed_line++;
3758 if (linelen == -1) {
3759 if (feof(s->f)) {
3760 s->eof = 1;
3761 } else
3762 err = got_ferror(s->f, GOT_ERR_IO);
3763 break;
3766 free(line);
3767 break;
3768 case '[':
3769 if (s->diff_context > 0) {
3770 s->diff_context--;
3771 diff_view_indicate_progress(view);
3772 err = create_diff(s);
3773 if (s->first_displayed_line + view->nlines - 1 >
3774 s->nlines) {
3775 s->first_displayed_line = 1;
3776 s->last_displayed_line = view->nlines;
3779 break;
3780 case ']':
3781 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3782 s->diff_context++;
3783 diff_view_indicate_progress(view);
3784 err = create_diff(s);
3786 break;
3787 case '<':
3788 case ',':
3789 if (s->log_view == NULL)
3790 break;
3791 ls = &s->log_view->state.log;
3792 old_selected_entry = ls->selected_entry;
3794 err = input_log_view(NULL, s->log_view, KEY_UP);
3795 if (err)
3796 break;
3798 if (old_selected_entry == ls->selected_entry)
3799 break;
3801 err = set_selected_commit(s, ls->selected_entry);
3802 if (err)
3803 break;
3805 s->first_displayed_line = 1;
3806 s->last_displayed_line = view->nlines;
3808 diff_view_indicate_progress(view);
3809 err = create_diff(s);
3810 break;
3811 case '>':
3812 case '.':
3813 if (s->log_view == NULL)
3814 break;
3815 ls = &s->log_view->state.log;
3816 old_selected_entry = ls->selected_entry;
3818 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3819 if (err)
3820 break;
3822 if (old_selected_entry == ls->selected_entry)
3823 break;
3825 err = set_selected_commit(s, ls->selected_entry);
3826 if (err)
3827 break;
3829 s->first_displayed_line = 1;
3830 s->last_displayed_line = view->nlines;
3832 diff_view_indicate_progress(view);
3833 err = create_diff(s);
3834 break;
3835 default:
3836 break;
3839 return err;
3842 static const struct got_error *
3843 cmd_diff(int argc, char *argv[])
3845 const struct got_error *error = NULL;
3846 struct got_repository *repo = NULL;
3847 struct got_worktree *worktree = NULL;
3848 struct got_object_id *id1 = NULL, *id2 = NULL;
3849 char *repo_path = NULL, *cwd = NULL;
3850 char *id_str1 = NULL, *id_str2 = NULL;
3851 char *label1 = NULL, *label2 = NULL;
3852 int diff_context = 3, ignore_whitespace = 0;
3853 int ch, force_text_diff = 0;
3854 const char *errstr;
3855 struct tog_view *view;
3857 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3858 switch (ch) {
3859 case 'a':
3860 force_text_diff = 1;
3861 break;
3862 case 'C':
3863 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3864 &errstr);
3865 if (errstr != NULL)
3866 err(1, "-C option %s", errstr);
3867 break;
3868 case 'r':
3869 repo_path = realpath(optarg, NULL);
3870 if (repo_path == NULL)
3871 return got_error_from_errno2("realpath",
3872 optarg);
3873 got_path_strip_trailing_slashes(repo_path);
3874 break;
3875 case 'w':
3876 ignore_whitespace = 1;
3877 break;
3878 default:
3879 usage_diff();
3880 /* NOTREACHED */
3884 argc -= optind;
3885 argv += optind;
3887 if (argc == 0) {
3888 usage_diff(); /* TODO show local worktree changes */
3889 } else if (argc == 2) {
3890 id_str1 = argv[0];
3891 id_str2 = argv[1];
3892 } else
3893 usage_diff();
3895 if (repo_path == NULL) {
3896 cwd = getcwd(NULL, 0);
3897 if (cwd == NULL)
3898 return got_error_from_errno("getcwd");
3899 error = got_worktree_open(&worktree, cwd);
3900 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3901 goto done;
3902 if (worktree)
3903 repo_path =
3904 strdup(got_worktree_get_repo_path(worktree));
3905 else
3906 repo_path = strdup(cwd);
3907 if (repo_path == NULL) {
3908 error = got_error_from_errno("strdup");
3909 goto done;
3913 error = got_repo_open(&repo, repo_path, NULL);
3914 if (error)
3915 goto done;
3917 init_curses();
3919 error = apply_unveil(got_repo_get_path(repo), NULL);
3920 if (error)
3921 goto done;
3923 error = tog_load_refs(repo);
3924 if (error)
3925 goto done;
3927 error = got_repo_match_object_id(&id1, &label1, id_str1,
3928 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3929 if (error)
3930 goto done;
3932 error = got_repo_match_object_id(&id2, &label2, id_str2,
3933 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3934 if (error)
3935 goto done;
3937 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3938 if (view == NULL) {
3939 error = got_error_from_errno("view_open");
3940 goto done;
3942 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3943 ignore_whitespace, force_text_diff, NULL, repo);
3944 if (error)
3945 goto done;
3946 error = view_loop(view);
3947 done:
3948 free(label1);
3949 free(label2);
3950 free(repo_path);
3951 free(cwd);
3952 if (repo) {
3953 const struct got_error *close_err = got_repo_close(repo);
3954 if (error == NULL)
3955 error = close_err;
3957 if (worktree)
3958 got_worktree_close(worktree);
3959 tog_free_refs();
3960 return error;
3963 __dead static void
3964 usage_blame(void)
3966 endwin();
3967 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3968 getprogname());
3969 exit(1);
3972 struct tog_blame_line {
3973 int annotated;
3974 struct got_object_id *id;
3977 static const struct got_error *
3978 draw_blame(struct tog_view *view)
3980 struct tog_blame_view_state *s = &view->state.blame;
3981 struct tog_blame *blame = &s->blame;
3982 regmatch_t *regmatch = &view->regmatch;
3983 const struct got_error *err;
3984 int lineno = 0, nprinted = 0;
3985 char *line = NULL;
3986 size_t linesize = 0;
3987 ssize_t linelen;
3988 wchar_t *wline;
3989 int width;
3990 struct tog_blame_line *blame_line;
3991 struct got_object_id *prev_id = NULL;
3992 char *id_str;
3993 struct tog_color *tc;
3995 err = got_object_id_str(&id_str, s->blamed_commit->id);
3996 if (err)
3997 return err;
3999 rewind(blame->f);
4000 werase(view->window);
4002 if (asprintf(&line, "commit %s", id_str) == -1) {
4003 err = got_error_from_errno("asprintf");
4004 free(id_str);
4005 return err;
4008 err = format_line(&wline, &width, line, view->ncols, 0);
4009 free(line);
4010 line = NULL;
4011 if (err)
4012 return err;
4013 if (view_needs_focus_indication(view))
4014 wstandout(view->window);
4015 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4016 if (tc)
4017 wattr_on(view->window,
4018 COLOR_PAIR(tc->colorpair), NULL);
4019 waddwstr(view->window, wline);
4020 if (tc)
4021 wattr_off(view->window,
4022 COLOR_PAIR(tc->colorpair), NULL);
4023 if (view_needs_focus_indication(view))
4024 wstandend(view->window);
4025 free(wline);
4026 wline = NULL;
4027 if (width < view->ncols - 1)
4028 waddch(view->window, '\n');
4030 if (asprintf(&line, "[%d/%d] %s%s",
4031 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4032 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4033 free(id_str);
4034 return got_error_from_errno("asprintf");
4036 free(id_str);
4037 err = format_line(&wline, &width, line, view->ncols, 0);
4038 free(line);
4039 line = NULL;
4040 if (err)
4041 return err;
4042 waddwstr(view->window, wline);
4043 free(wline);
4044 wline = NULL;
4045 if (width < view->ncols - 1)
4046 waddch(view->window, '\n');
4048 s->eof = 0;
4049 while (nprinted < view->nlines - 2) {
4050 linelen = getline(&line, &linesize, blame->f);
4051 if (linelen == -1) {
4052 if (feof(blame->f)) {
4053 s->eof = 1;
4054 break;
4056 free(line);
4057 return got_ferror(blame->f, GOT_ERR_IO);
4059 if (++lineno < s->first_displayed_line)
4060 continue;
4062 if (view->focussed && nprinted == s->selected_line - 1)
4063 wstandout(view->window);
4065 if (blame->nlines > 0) {
4066 blame_line = &blame->lines[lineno - 1];
4067 if (blame_line->annotated && prev_id &&
4068 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4069 !(view->focussed &&
4070 nprinted == s->selected_line - 1)) {
4071 waddstr(view->window, " ");
4072 } else if (blame_line->annotated) {
4073 char *id_str;
4074 err = got_object_id_str(&id_str, blame_line->id);
4075 if (err) {
4076 free(line);
4077 return err;
4079 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4080 if (tc)
4081 wattr_on(view->window,
4082 COLOR_PAIR(tc->colorpair), NULL);
4083 wprintw(view->window, "%.8s", id_str);
4084 if (tc)
4085 wattr_off(view->window,
4086 COLOR_PAIR(tc->colorpair), NULL);
4087 free(id_str);
4088 prev_id = blame_line->id;
4089 } else {
4090 waddstr(view->window, "........");
4091 prev_id = NULL;
4093 } else {
4094 waddstr(view->window, "........");
4095 prev_id = NULL;
4098 if (view->focussed && nprinted == s->selected_line - 1)
4099 wstandend(view->window);
4100 waddstr(view->window, " ");
4102 if (view->ncols <= 9) {
4103 width = 9;
4104 wline = wcsdup(L"");
4105 if (wline == NULL) {
4106 err = got_error_from_errno("wcsdup");
4107 free(line);
4108 return err;
4110 } else if (s->first_displayed_line + nprinted ==
4111 s->matched_line &&
4112 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4113 err = add_matched_line(&width, line, view->ncols - 9, 9,
4114 view->window, regmatch);
4115 if (err) {
4116 free(line);
4117 return err;
4119 width += 9;
4120 } else {
4121 err = format_line(&wline, &width, line,
4122 view->ncols - 9, 9);
4123 waddwstr(view->window, wline);
4124 free(wline);
4125 wline = NULL;
4126 width += 9;
4129 if (width <= view->ncols - 1)
4130 waddch(view->window, '\n');
4131 if (++nprinted == 1)
4132 s->first_displayed_line = lineno;
4134 free(line);
4135 s->last_displayed_line = lineno;
4137 view_vborder(view);
4139 return NULL;
4142 static const struct got_error *
4143 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4145 const struct got_error *err = NULL;
4146 struct tog_blame_cb_args *a = arg;
4147 struct tog_blame_line *line;
4148 int errcode;
4150 if (nlines != a->nlines ||
4151 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4152 return got_error(GOT_ERR_RANGE);
4154 errcode = pthread_mutex_lock(&tog_mutex);
4155 if (errcode)
4156 return got_error_set_errno(errcode, "pthread_mutex_lock");
4158 if (*a->quit) { /* user has quit the blame view */
4159 err = got_error(GOT_ERR_ITER_COMPLETED);
4160 goto done;
4163 if (lineno == -1)
4164 goto done; /* no change in this commit */
4166 line = &a->lines[lineno - 1];
4167 if (line->annotated)
4168 goto done;
4170 line->id = got_object_id_dup(id);
4171 if (line->id == NULL) {
4172 err = got_error_from_errno("got_object_id_dup");
4173 goto done;
4175 line->annotated = 1;
4176 done:
4177 errcode = pthread_mutex_unlock(&tog_mutex);
4178 if (errcode)
4179 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4180 return err;
4183 static void *
4184 blame_thread(void *arg)
4186 const struct got_error *err, *close_err;
4187 struct tog_blame_thread_args *ta = arg;
4188 struct tog_blame_cb_args *a = ta->cb_args;
4189 int errcode;
4191 err = block_signals_used_by_main_thread();
4192 if (err)
4193 return (void *)err;
4195 err = got_blame(ta->path, a->commit_id, ta->repo,
4196 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4197 if (err && err->code == GOT_ERR_CANCELLED)
4198 err = NULL;
4200 errcode = pthread_mutex_lock(&tog_mutex);
4201 if (errcode)
4202 return (void *)got_error_set_errno(errcode,
4203 "pthread_mutex_lock");
4205 close_err = got_repo_close(ta->repo);
4206 if (err == NULL)
4207 err = close_err;
4208 ta->repo = NULL;
4209 *ta->complete = 1;
4211 errcode = pthread_mutex_unlock(&tog_mutex);
4212 if (errcode && err == NULL)
4213 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4215 return (void *)err;
4218 static struct got_object_id *
4219 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4220 int first_displayed_line, int selected_line)
4222 struct tog_blame_line *line;
4224 if (nlines <= 0)
4225 return NULL;
4227 line = &lines[first_displayed_line - 1 + selected_line - 1];
4228 if (!line->annotated)
4229 return NULL;
4231 return line->id;
4234 static const struct got_error *
4235 stop_blame(struct tog_blame *blame)
4237 const struct got_error *err = NULL;
4238 int i;
4240 if (blame->thread) {
4241 int errcode;
4242 errcode = pthread_mutex_unlock(&tog_mutex);
4243 if (errcode)
4244 return got_error_set_errno(errcode,
4245 "pthread_mutex_unlock");
4246 errcode = pthread_join(blame->thread, (void **)&err);
4247 if (errcode)
4248 return got_error_set_errno(errcode, "pthread_join");
4249 errcode = pthread_mutex_lock(&tog_mutex);
4250 if (errcode)
4251 return got_error_set_errno(errcode,
4252 "pthread_mutex_lock");
4253 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4254 err = NULL;
4255 blame->thread = 0; //NULL;
4257 if (blame->thread_args.repo) {
4258 const struct got_error *close_err;
4259 close_err = got_repo_close(blame->thread_args.repo);
4260 if (err == NULL)
4261 err = close_err;
4262 blame->thread_args.repo = NULL;
4264 if (blame->f) {
4265 if (fclose(blame->f) == EOF && err == NULL)
4266 err = got_error_from_errno("fclose");
4267 blame->f = NULL;
4269 if (blame->lines) {
4270 for (i = 0; i < blame->nlines; i++)
4271 free(blame->lines[i].id);
4272 free(blame->lines);
4273 blame->lines = NULL;
4275 free(blame->cb_args.commit_id);
4276 blame->cb_args.commit_id = NULL;
4278 return err;
4281 static const struct got_error *
4282 cancel_blame_view(void *arg)
4284 const struct got_error *err = NULL;
4285 int *done = arg;
4286 int errcode;
4288 errcode = pthread_mutex_lock(&tog_mutex);
4289 if (errcode)
4290 return got_error_set_errno(errcode,
4291 "pthread_mutex_unlock");
4293 if (*done)
4294 err = got_error(GOT_ERR_CANCELLED);
4296 errcode = pthread_mutex_unlock(&tog_mutex);
4297 if (errcode)
4298 return got_error_set_errno(errcode,
4299 "pthread_mutex_lock");
4301 return err;
4304 static const struct got_error *
4305 run_blame(struct tog_view *view)
4307 struct tog_blame_view_state *s = &view->state.blame;
4308 struct tog_blame *blame = &s->blame;
4309 const struct got_error *err = NULL;
4310 struct got_blob_object *blob = NULL;
4311 struct got_repository *thread_repo = NULL;
4312 struct got_object_id *obj_id = NULL;
4313 int obj_type;
4315 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4316 s->path);
4317 if (err)
4318 return err;
4320 err = got_object_get_type(&obj_type, s->repo, obj_id);
4321 if (err)
4322 goto done;
4324 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4325 err = got_error(GOT_ERR_OBJ_TYPE);
4326 goto done;
4329 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4330 if (err)
4331 goto done;
4332 blame->f = got_opentemp();
4333 if (blame->f == NULL) {
4334 err = got_error_from_errno("got_opentemp");
4335 goto done;
4337 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4338 &blame->line_offsets, blame->f, blob);
4339 if (err)
4340 goto done;
4341 if (blame->nlines == 0) {
4342 s->blame_complete = 1;
4343 goto done;
4346 /* Don't include \n at EOF in the blame line count. */
4347 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4348 blame->nlines--;
4350 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4351 if (blame->lines == NULL) {
4352 err = got_error_from_errno("calloc");
4353 goto done;
4356 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4357 if (err)
4358 goto done;
4360 blame->cb_args.view = view;
4361 blame->cb_args.lines = blame->lines;
4362 blame->cb_args.nlines = blame->nlines;
4363 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4364 if (blame->cb_args.commit_id == NULL) {
4365 err = got_error_from_errno("got_object_id_dup");
4366 goto done;
4368 blame->cb_args.quit = &s->done;
4370 blame->thread_args.path = s->path;
4371 blame->thread_args.repo = thread_repo;
4372 blame->thread_args.cb_args = &blame->cb_args;
4373 blame->thread_args.complete = &s->blame_complete;
4374 blame->thread_args.cancel_cb = cancel_blame_view;
4375 blame->thread_args.cancel_arg = &s->done;
4376 s->blame_complete = 0;
4378 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4379 s->first_displayed_line = 1;
4380 s->last_displayed_line = view->nlines;
4381 s->selected_line = 1;
4384 done:
4385 if (blob)
4386 got_object_blob_close(blob);
4387 free(obj_id);
4388 if (err)
4389 stop_blame(blame);
4390 return err;
4393 static const struct got_error *
4394 open_blame_view(struct tog_view *view, char *path,
4395 struct got_object_id *commit_id, struct got_repository *repo)
4397 const struct got_error *err = NULL;
4398 struct tog_blame_view_state *s = &view->state.blame;
4400 STAILQ_INIT(&s->blamed_commits);
4402 s->path = strdup(path);
4403 if (s->path == NULL)
4404 return got_error_from_errno("strdup");
4406 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4407 if (err) {
4408 free(s->path);
4409 return err;
4412 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4413 s->first_displayed_line = 1;
4414 s->last_displayed_line = view->nlines;
4415 s->selected_line = 1;
4416 s->blame_complete = 0;
4417 s->repo = repo;
4418 s->commit_id = commit_id;
4419 memset(&s->blame, 0, sizeof(s->blame));
4421 STAILQ_INIT(&s->colors);
4422 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4423 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4424 get_color_value("TOG_COLOR_COMMIT"));
4425 if (err)
4426 return err;
4429 view->show = show_blame_view;
4430 view->input = input_blame_view;
4431 view->close = close_blame_view;
4432 view->search_start = search_start_blame_view;
4433 view->search_next = search_next_blame_view;
4435 return run_blame(view);
4438 static const struct got_error *
4439 close_blame_view(struct tog_view *view)
4441 const struct got_error *err = NULL;
4442 struct tog_blame_view_state *s = &view->state.blame;
4444 if (s->blame.thread)
4445 err = stop_blame(&s->blame);
4447 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4448 struct got_object_qid *blamed_commit;
4449 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4450 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4451 got_object_qid_free(blamed_commit);
4454 free(s->path);
4455 free_colors(&s->colors);
4457 return err;
4460 static const struct got_error *
4461 search_start_blame_view(struct tog_view *view)
4463 struct tog_blame_view_state *s = &view->state.blame;
4465 s->matched_line = 0;
4466 return NULL;
4469 static const struct got_error *
4470 search_next_blame_view(struct tog_view *view)
4472 struct tog_blame_view_state *s = &view->state.blame;
4473 int lineno;
4474 char *line = NULL;
4475 size_t linesize = 0;
4476 ssize_t linelen;
4478 if (!view->searching) {
4479 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4480 return NULL;
4483 if (s->matched_line) {
4484 if (view->searching == TOG_SEARCH_FORWARD)
4485 lineno = s->matched_line + 1;
4486 else
4487 lineno = s->matched_line - 1;
4488 } else {
4489 if (view->searching == TOG_SEARCH_FORWARD)
4490 lineno = 1;
4491 else
4492 lineno = s->blame.nlines;
4495 while (1) {
4496 off_t offset;
4498 if (lineno <= 0 || lineno > s->blame.nlines) {
4499 if (s->matched_line == 0) {
4500 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4501 break;
4504 if (view->searching == TOG_SEARCH_FORWARD)
4505 lineno = 1;
4506 else
4507 lineno = s->blame.nlines;
4510 offset = s->blame.line_offsets[lineno - 1];
4511 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4512 free(line);
4513 return got_error_from_errno("fseeko");
4515 linelen = getline(&line, &linesize, s->blame.f);
4516 if (linelen != -1 &&
4517 match_line(line, &view->regex, 1, &view->regmatch)) {
4518 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4519 s->matched_line = lineno;
4520 break;
4522 if (view->searching == TOG_SEARCH_FORWARD)
4523 lineno++;
4524 else
4525 lineno--;
4527 free(line);
4529 if (s->matched_line) {
4530 s->first_displayed_line = s->matched_line;
4531 s->selected_line = 1;
4534 return NULL;
4537 static const struct got_error *
4538 show_blame_view(struct tog_view *view)
4540 const struct got_error *err = NULL;
4541 struct tog_blame_view_state *s = &view->state.blame;
4542 int errcode;
4544 if (s->blame.thread == 0 && !s->blame_complete) {
4545 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4546 &s->blame.thread_args);
4547 if (errcode)
4548 return got_error_set_errno(errcode, "pthread_create");
4550 halfdelay(1); /* fast refresh while annotating */
4553 if (s->blame_complete)
4554 halfdelay(10); /* disable fast refresh */
4556 err = draw_blame(view);
4558 view_vborder(view);
4559 return err;
4562 static const struct got_error *
4563 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4565 const struct got_error *err = NULL, *thread_err = NULL;
4566 struct tog_view *diff_view;
4567 struct tog_blame_view_state *s = &view->state.blame;
4568 int begin_x = 0;
4570 switch (ch) {
4571 case 'q':
4572 s->done = 1;
4573 break;
4574 case 'g':
4575 case KEY_HOME:
4576 s->selected_line = 1;
4577 s->first_displayed_line = 1;
4578 break;
4579 case 'G':
4580 case KEY_END:
4581 if (s->blame.nlines < view->nlines - 2) {
4582 s->selected_line = s->blame.nlines;
4583 s->first_displayed_line = 1;
4584 } else {
4585 s->selected_line = view->nlines - 2;
4586 s->first_displayed_line = s->blame.nlines -
4587 (view->nlines - 3);
4589 break;
4590 case 'k':
4591 case KEY_UP:
4592 if (s->selected_line > 1)
4593 s->selected_line--;
4594 else if (s->selected_line == 1 &&
4595 s->first_displayed_line > 1)
4596 s->first_displayed_line--;
4597 break;
4598 case KEY_PPAGE:
4599 case CTRL('b'):
4600 if (s->first_displayed_line == 1) {
4601 s->selected_line = 1;
4602 break;
4604 if (s->first_displayed_line > view->nlines - 2)
4605 s->first_displayed_line -=
4606 (view->nlines - 2);
4607 else
4608 s->first_displayed_line = 1;
4609 break;
4610 case 'j':
4611 case KEY_DOWN:
4612 if (s->selected_line < view->nlines - 2 &&
4613 s->first_displayed_line +
4614 s->selected_line <= s->blame.nlines)
4615 s->selected_line++;
4616 else if (s->last_displayed_line <
4617 s->blame.nlines)
4618 s->first_displayed_line++;
4619 break;
4620 case 'b':
4621 case 'p': {
4622 struct got_object_id *id = NULL;
4623 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4624 s->first_displayed_line, s->selected_line);
4625 if (id == NULL)
4626 break;
4627 if (ch == 'p') {
4628 struct got_commit_object *commit;
4629 struct got_object_qid *pid;
4630 struct got_object_id *blob_id = NULL;
4631 int obj_type;
4632 err = got_object_open_as_commit(&commit,
4633 s->repo, id);
4634 if (err)
4635 break;
4636 pid = STAILQ_FIRST(
4637 got_object_commit_get_parent_ids(commit));
4638 if (pid == NULL) {
4639 got_object_commit_close(commit);
4640 break;
4642 /* Check if path history ends here. */
4643 err = got_object_id_by_path(&blob_id, s->repo,
4644 pid->id, s->path);
4645 if (err) {
4646 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4647 err = NULL;
4648 got_object_commit_close(commit);
4649 break;
4651 err = got_object_get_type(&obj_type, s->repo,
4652 blob_id);
4653 free(blob_id);
4654 /* Can't blame non-blob type objects. */
4655 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4656 got_object_commit_close(commit);
4657 break;
4659 err = got_object_qid_alloc(&s->blamed_commit,
4660 pid->id);
4661 got_object_commit_close(commit);
4662 } else {
4663 if (got_object_id_cmp(id,
4664 s->blamed_commit->id) == 0)
4665 break;
4666 err = got_object_qid_alloc(&s->blamed_commit,
4667 id);
4669 if (err)
4670 break;
4671 s->done = 1;
4672 thread_err = stop_blame(&s->blame);
4673 s->done = 0;
4674 if (thread_err)
4675 break;
4676 STAILQ_INSERT_HEAD(&s->blamed_commits,
4677 s->blamed_commit, entry);
4678 err = run_blame(view);
4679 if (err)
4680 break;
4681 break;
4683 case 'B': {
4684 struct got_object_qid *first;
4685 first = STAILQ_FIRST(&s->blamed_commits);
4686 if (!got_object_id_cmp(first->id, s->commit_id))
4687 break;
4688 s->done = 1;
4689 thread_err = stop_blame(&s->blame);
4690 s->done = 0;
4691 if (thread_err)
4692 break;
4693 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4694 got_object_qid_free(s->blamed_commit);
4695 s->blamed_commit =
4696 STAILQ_FIRST(&s->blamed_commits);
4697 err = run_blame(view);
4698 if (err)
4699 break;
4700 break;
4702 case KEY_ENTER:
4703 case '\r': {
4704 struct got_object_id *id = NULL;
4705 struct got_object_qid *pid;
4706 struct got_commit_object *commit = NULL;
4707 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4708 s->first_displayed_line, s->selected_line);
4709 if (id == NULL)
4710 break;
4711 err = got_object_open_as_commit(&commit, s->repo, id);
4712 if (err)
4713 break;
4714 pid = STAILQ_FIRST(
4715 got_object_commit_get_parent_ids(commit));
4716 if (view_is_parent_view(view))
4717 begin_x = view_split_begin_x(view->begin_x);
4718 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4719 if (diff_view == NULL) {
4720 got_object_commit_close(commit);
4721 err = got_error_from_errno("view_open");
4722 break;
4724 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4725 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4726 got_object_commit_close(commit);
4727 if (err) {
4728 view_close(diff_view);
4729 break;
4731 view->focussed = 0;
4732 diff_view->focussed = 1;
4733 if (view_is_parent_view(view)) {
4734 err = view_close_child(view);
4735 if (err)
4736 break;
4737 view_set_child(view, diff_view);
4738 view->focus_child = 1;
4739 } else
4740 *new_view = diff_view;
4741 if (err)
4742 break;
4743 break;
4745 case KEY_NPAGE:
4746 case CTRL('f'):
4747 case ' ':
4748 if (s->last_displayed_line >= s->blame.nlines &&
4749 s->selected_line >= MIN(s->blame.nlines,
4750 view->nlines - 2)) {
4751 break;
4753 if (s->last_displayed_line >= s->blame.nlines &&
4754 s->selected_line < view->nlines - 2) {
4755 s->selected_line = MIN(s->blame.nlines,
4756 view->nlines - 2);
4757 break;
4759 if (s->last_displayed_line + view->nlines - 2
4760 <= s->blame.nlines)
4761 s->first_displayed_line +=
4762 view->nlines - 2;
4763 else
4764 s->first_displayed_line =
4765 s->blame.nlines -
4766 (view->nlines - 3);
4767 break;
4768 case KEY_RESIZE:
4769 if (s->selected_line > view->nlines - 2) {
4770 s->selected_line = MIN(s->blame.nlines,
4771 view->nlines - 2);
4773 break;
4774 default:
4775 break;
4777 return thread_err ? thread_err : err;
4780 static const struct got_error *
4781 cmd_blame(int argc, char *argv[])
4783 const struct got_error *error;
4784 struct got_repository *repo = NULL;
4785 struct got_worktree *worktree = NULL;
4786 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4787 char *link_target = NULL;
4788 struct got_object_id *commit_id = NULL;
4789 char *commit_id_str = NULL;
4790 int ch;
4791 struct tog_view *view;
4793 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4794 switch (ch) {
4795 case 'c':
4796 commit_id_str = optarg;
4797 break;
4798 case 'r':
4799 repo_path = realpath(optarg, NULL);
4800 if (repo_path == NULL)
4801 return got_error_from_errno2("realpath",
4802 optarg);
4803 break;
4804 default:
4805 usage_blame();
4806 /* NOTREACHED */
4810 argc -= optind;
4811 argv += optind;
4813 if (argc != 1)
4814 usage_blame();
4816 if (repo_path == NULL) {
4817 cwd = getcwd(NULL, 0);
4818 if (cwd == NULL)
4819 return got_error_from_errno("getcwd");
4820 error = got_worktree_open(&worktree, cwd);
4821 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4822 goto done;
4823 if (worktree)
4824 repo_path =
4825 strdup(got_worktree_get_repo_path(worktree));
4826 else
4827 repo_path = strdup(cwd);
4828 if (repo_path == NULL) {
4829 error = got_error_from_errno("strdup");
4830 goto done;
4834 error = got_repo_open(&repo, repo_path, NULL);
4835 if (error != NULL)
4836 goto done;
4838 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4839 worktree);
4840 if (error)
4841 goto done;
4843 init_curses();
4845 error = apply_unveil(got_repo_get_path(repo), NULL);
4846 if (error)
4847 goto done;
4849 error = tog_load_refs(repo);
4850 if (error)
4851 goto done;
4853 if (commit_id_str == NULL) {
4854 struct got_reference *head_ref;
4855 error = got_ref_open(&head_ref, repo, worktree ?
4856 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4857 if (error != NULL)
4858 goto done;
4859 error = got_ref_resolve(&commit_id, repo, head_ref);
4860 got_ref_close(head_ref);
4861 } else {
4862 error = got_repo_match_object_id(&commit_id, NULL,
4863 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4865 if (error != NULL)
4866 goto done;
4868 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4869 if (view == NULL) {
4870 error = got_error_from_errno("view_open");
4871 goto done;
4874 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4875 commit_id, repo);
4876 if (error)
4877 goto done;
4879 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4880 commit_id, repo);
4881 if (error)
4882 goto done;
4883 if (worktree) {
4884 /* Release work tree lock. */
4885 got_worktree_close(worktree);
4886 worktree = NULL;
4888 error = view_loop(view);
4889 done:
4890 free(repo_path);
4891 free(in_repo_path);
4892 free(link_target);
4893 free(cwd);
4894 free(commit_id);
4895 if (worktree)
4896 got_worktree_close(worktree);
4897 if (repo) {
4898 const struct got_error *close_err = got_repo_close(repo);
4899 if (error == NULL)
4900 error = close_err;
4902 tog_free_refs();
4903 return error;
4906 static const struct got_error *
4907 draw_tree_entries(struct tog_view *view, const char *parent_path)
4909 struct tog_tree_view_state *s = &view->state.tree;
4910 const struct got_error *err = NULL;
4911 struct got_tree_entry *te;
4912 wchar_t *wline;
4913 struct tog_color *tc;
4914 int width, n, i, nentries;
4915 int limit = view->nlines;
4917 s->ndisplayed = 0;
4919 werase(view->window);
4921 if (limit == 0)
4922 return NULL;
4924 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4925 if (err)
4926 return err;
4927 if (view_needs_focus_indication(view))
4928 wstandout(view->window);
4929 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4930 if (tc)
4931 wattr_on(view->window,
4932 COLOR_PAIR(tc->colorpair), NULL);
4933 waddwstr(view->window, wline);
4934 if (tc)
4935 wattr_off(view->window,
4936 COLOR_PAIR(tc->colorpair), NULL);
4937 if (view_needs_focus_indication(view))
4938 wstandend(view->window);
4939 free(wline);
4940 wline = NULL;
4941 if (width < view->ncols - 1)
4942 waddch(view->window, '\n');
4943 if (--limit <= 0)
4944 return NULL;
4945 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4946 if (err)
4947 return err;
4948 waddwstr(view->window, wline);
4949 free(wline);
4950 wline = NULL;
4951 if (width < view->ncols - 1)
4952 waddch(view->window, '\n');
4953 if (--limit <= 0)
4954 return NULL;
4955 waddch(view->window, '\n');
4956 if (--limit <= 0)
4957 return NULL;
4959 if (s->first_displayed_entry == NULL) {
4960 te = got_object_tree_get_first_entry(s->tree);
4961 if (s->selected == 0) {
4962 if (view->focussed)
4963 wstandout(view->window);
4964 s->selected_entry = NULL;
4966 waddstr(view->window, " ..\n"); /* parent directory */
4967 if (s->selected == 0 && view->focussed)
4968 wstandend(view->window);
4969 s->ndisplayed++;
4970 if (--limit <= 0)
4971 return NULL;
4972 n = 1;
4973 } else {
4974 n = 0;
4975 te = s->first_displayed_entry;
4978 nentries = got_object_tree_get_nentries(s->tree);
4979 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4980 char *line = NULL, *id_str = NULL, *link_target = NULL;
4981 const char *modestr = "";
4982 mode_t mode;
4984 te = got_object_tree_get_entry(s->tree, i);
4985 mode = got_tree_entry_get_mode(te);
4987 if (s->show_ids) {
4988 err = got_object_id_str(&id_str,
4989 got_tree_entry_get_id(te));
4990 if (err)
4991 return got_error_from_errno(
4992 "got_object_id_str");
4994 if (got_object_tree_entry_is_submodule(te))
4995 modestr = "$";
4996 else if (S_ISLNK(mode)) {
4997 int i;
4999 err = got_tree_entry_get_symlink_target(&link_target,
5000 te, s->repo);
5001 if (err) {
5002 free(id_str);
5003 return err;
5005 for (i = 0; i < strlen(link_target); i++) {
5006 if (!isprint((unsigned char)link_target[i]))
5007 link_target[i] = '?';
5009 modestr = "@";
5011 else if (S_ISDIR(mode))
5012 modestr = "/";
5013 else if (mode & S_IXUSR)
5014 modestr = "*";
5015 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5016 got_tree_entry_get_name(te), modestr,
5017 link_target ? " -> ": "",
5018 link_target ? link_target : "") == -1) {
5019 free(id_str);
5020 free(link_target);
5021 return got_error_from_errno("asprintf");
5023 free(id_str);
5024 free(link_target);
5025 err = format_line(&wline, &width, line, view->ncols, 0);
5026 if (err) {
5027 free(line);
5028 break;
5030 if (n == s->selected) {
5031 if (view->focussed)
5032 wstandout(view->window);
5033 s->selected_entry = te;
5035 tc = match_color(&s->colors, line);
5036 if (tc)
5037 wattr_on(view->window,
5038 COLOR_PAIR(tc->colorpair), NULL);
5039 waddwstr(view->window, wline);
5040 if (tc)
5041 wattr_off(view->window,
5042 COLOR_PAIR(tc->colorpair), NULL);
5043 if (width < view->ncols - 1)
5044 waddch(view->window, '\n');
5045 if (n == s->selected && view->focussed)
5046 wstandend(view->window);
5047 free(line);
5048 free(wline);
5049 wline = NULL;
5050 n++;
5051 s->ndisplayed++;
5052 s->last_displayed_entry = te;
5053 if (--limit <= 0)
5054 break;
5057 return err;
5060 static void
5061 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5063 struct got_tree_entry *te;
5064 int isroot = s->tree == s->root;
5065 int i = 0;
5067 if (s->first_displayed_entry == NULL)
5068 return;
5070 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5071 while (i++ < maxscroll) {
5072 if (te == NULL) {
5073 if (!isroot)
5074 s->first_displayed_entry = NULL;
5075 break;
5077 s->first_displayed_entry = te;
5078 te = got_tree_entry_get_prev(s->tree, te);
5082 static void
5083 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5085 struct got_tree_entry *next, *last;
5086 int n = 0;
5088 if (s->first_displayed_entry)
5089 next = got_tree_entry_get_next(s->tree,
5090 s->first_displayed_entry);
5091 else
5092 next = got_object_tree_get_first_entry(s->tree);
5094 last = s->last_displayed_entry;
5095 while (next && last && n++ < maxscroll) {
5096 last = got_tree_entry_get_next(s->tree, last);
5097 if (last) {
5098 s->first_displayed_entry = next;
5099 next = got_tree_entry_get_next(s->tree, next);
5104 static const struct got_error *
5105 tree_entry_path(char **path, struct tog_parent_trees *parents,
5106 struct got_tree_entry *te)
5108 const struct got_error *err = NULL;
5109 struct tog_parent_tree *pt;
5110 size_t len = 2; /* for leading slash and NUL */
5112 TAILQ_FOREACH(pt, parents, entry)
5113 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5114 + 1 /* slash */;
5115 if (te)
5116 len += strlen(got_tree_entry_get_name(te));
5118 *path = calloc(1, len);
5119 if (path == NULL)
5120 return got_error_from_errno("calloc");
5122 (*path)[0] = '/';
5123 pt = TAILQ_LAST(parents, tog_parent_trees);
5124 while (pt) {
5125 const char *name = got_tree_entry_get_name(pt->selected_entry);
5126 if (strlcat(*path, name, len) >= len) {
5127 err = got_error(GOT_ERR_NO_SPACE);
5128 goto done;
5130 if (strlcat(*path, "/", len) >= len) {
5131 err = got_error(GOT_ERR_NO_SPACE);
5132 goto done;
5134 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5136 if (te) {
5137 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5138 err = got_error(GOT_ERR_NO_SPACE);
5139 goto done;
5142 done:
5143 if (err) {
5144 free(*path);
5145 *path = NULL;
5147 return err;
5150 static const struct got_error *
5151 blame_tree_entry(struct tog_view **new_view, int begin_x,
5152 struct got_tree_entry *te, struct tog_parent_trees *parents,
5153 struct got_object_id *commit_id, struct got_repository *repo)
5155 const struct got_error *err = NULL;
5156 char *path;
5157 struct tog_view *blame_view;
5159 *new_view = NULL;
5161 err = tree_entry_path(&path, parents, te);
5162 if (err)
5163 return err;
5165 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5166 if (blame_view == NULL) {
5167 err = got_error_from_errno("view_open");
5168 goto done;
5171 err = open_blame_view(blame_view, path, commit_id, repo);
5172 if (err) {
5173 if (err->code == GOT_ERR_CANCELLED)
5174 err = NULL;
5175 view_close(blame_view);
5176 } else
5177 *new_view = blame_view;
5178 done:
5179 free(path);
5180 return err;
5183 static const struct got_error *
5184 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5185 struct tog_tree_view_state *s)
5187 struct tog_view *log_view;
5188 const struct got_error *err = NULL;
5189 char *path;
5191 *new_view = NULL;
5193 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5194 if (log_view == NULL)
5195 return got_error_from_errno("view_open");
5197 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5198 if (err)
5199 return err;
5201 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5202 path, 0);
5203 if (err)
5204 view_close(log_view);
5205 else
5206 *new_view = log_view;
5207 free(path);
5208 return err;
5211 static const struct got_error *
5212 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5213 const char *head_ref_name, struct got_repository *repo)
5215 const struct got_error *err = NULL;
5216 char *commit_id_str = NULL;
5217 struct tog_tree_view_state *s = &view->state.tree;
5218 struct got_commit_object *commit = NULL;
5220 TAILQ_INIT(&s->parents);
5221 STAILQ_INIT(&s->colors);
5223 s->commit_id = got_object_id_dup(commit_id);
5224 if (s->commit_id == NULL)
5225 return got_error_from_errno("got_object_id_dup");
5227 err = got_object_open_as_commit(&commit, repo, commit_id);
5228 if (err)
5229 goto done;
5232 * The root is opened here and will be closed when the view is closed.
5233 * Any visited subtrees and their path-wise parents are opened and
5234 * closed on demand.
5236 err = got_object_open_as_tree(&s->root, repo,
5237 got_object_commit_get_tree_id(commit));
5238 if (err)
5239 goto done;
5240 s->tree = s->root;
5242 err = got_object_id_str(&commit_id_str, commit_id);
5243 if (err != NULL)
5244 goto done;
5246 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5247 err = got_error_from_errno("asprintf");
5248 goto done;
5251 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5252 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5253 if (head_ref_name) {
5254 s->head_ref_name = strdup(head_ref_name);
5255 if (s->head_ref_name == NULL) {
5256 err = got_error_from_errno("strdup");
5257 goto done;
5260 s->repo = repo;
5262 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5263 err = add_color(&s->colors, "\\$$",
5264 TOG_COLOR_TREE_SUBMODULE,
5265 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5266 if (err)
5267 goto done;
5268 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5269 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5270 if (err)
5271 goto done;
5272 err = add_color(&s->colors, "/$",
5273 TOG_COLOR_TREE_DIRECTORY,
5274 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5275 if (err)
5276 goto done;
5278 err = add_color(&s->colors, "\\*$",
5279 TOG_COLOR_TREE_EXECUTABLE,
5280 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5281 if (err)
5282 goto done;
5284 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5285 get_color_value("TOG_COLOR_COMMIT"));
5286 if (err)
5287 goto done;
5290 view->show = show_tree_view;
5291 view->input = input_tree_view;
5292 view->close = close_tree_view;
5293 view->search_start = search_start_tree_view;
5294 view->search_next = search_next_tree_view;
5295 done:
5296 free(commit_id_str);
5297 if (commit)
5298 got_object_commit_close(commit);
5299 if (err)
5300 close_tree_view(view);
5301 return err;
5304 static const struct got_error *
5305 close_tree_view(struct tog_view *view)
5307 struct tog_tree_view_state *s = &view->state.tree;
5309 free_colors(&s->colors);
5310 free(s->tree_label);
5311 s->tree_label = NULL;
5312 free(s->commit_id);
5313 s->commit_id = NULL;
5314 free(s->head_ref_name);
5315 s->head_ref_name = NULL;
5316 while (!TAILQ_EMPTY(&s->parents)) {
5317 struct tog_parent_tree *parent;
5318 parent = TAILQ_FIRST(&s->parents);
5319 TAILQ_REMOVE(&s->parents, parent, entry);
5320 if (parent->tree != s->root)
5321 got_object_tree_close(parent->tree);
5322 free(parent);
5325 if (s->tree != NULL && s->tree != s->root)
5326 got_object_tree_close(s->tree);
5327 if (s->root)
5328 got_object_tree_close(s->root);
5329 return NULL;
5332 static const struct got_error *
5333 search_start_tree_view(struct tog_view *view)
5335 struct tog_tree_view_state *s = &view->state.tree;
5337 s->matched_entry = NULL;
5338 return NULL;
5341 static int
5342 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5344 regmatch_t regmatch;
5346 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5347 0) == 0;
5350 static const struct got_error *
5351 search_next_tree_view(struct tog_view *view)
5353 struct tog_tree_view_state *s = &view->state.tree;
5354 struct got_tree_entry *te = NULL;
5356 if (!view->searching) {
5357 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5358 return NULL;
5361 if (s->matched_entry) {
5362 if (view->searching == TOG_SEARCH_FORWARD) {
5363 if (s->selected_entry)
5364 te = got_tree_entry_get_next(s->tree,
5365 s->selected_entry);
5366 else
5367 te = got_object_tree_get_first_entry(s->tree);
5368 } else {
5369 if (s->selected_entry == NULL)
5370 te = got_object_tree_get_last_entry(s->tree);
5371 else
5372 te = got_tree_entry_get_prev(s->tree,
5373 s->selected_entry);
5375 } else {
5376 if (view->searching == TOG_SEARCH_FORWARD)
5377 te = got_object_tree_get_first_entry(s->tree);
5378 else
5379 te = got_object_tree_get_last_entry(s->tree);
5382 while (1) {
5383 if (te == NULL) {
5384 if (s->matched_entry == NULL) {
5385 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5386 return NULL;
5388 if (view->searching == TOG_SEARCH_FORWARD)
5389 te = got_object_tree_get_first_entry(s->tree);
5390 else
5391 te = got_object_tree_get_last_entry(s->tree);
5394 if (match_tree_entry(te, &view->regex)) {
5395 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5396 s->matched_entry = te;
5397 break;
5400 if (view->searching == TOG_SEARCH_FORWARD)
5401 te = got_tree_entry_get_next(s->tree, te);
5402 else
5403 te = got_tree_entry_get_prev(s->tree, te);
5406 if (s->matched_entry) {
5407 s->first_displayed_entry = s->matched_entry;
5408 s->selected = 0;
5411 return NULL;
5414 static const struct got_error *
5415 show_tree_view(struct tog_view *view)
5417 const struct got_error *err = NULL;
5418 struct tog_tree_view_state *s = &view->state.tree;
5419 char *parent_path;
5421 err = tree_entry_path(&parent_path, &s->parents, NULL);
5422 if (err)
5423 return err;
5425 err = draw_tree_entries(view, parent_path);
5426 free(parent_path);
5428 view_vborder(view);
5429 return err;
5432 static const struct got_error *
5433 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5435 const struct got_error *err = NULL;
5436 struct tog_tree_view_state *s = &view->state.tree;
5437 struct tog_view *log_view, *ref_view;
5438 struct got_tree_entry *te;
5439 int begin_x = 0, n;
5441 switch (ch) {
5442 case 'i':
5443 s->show_ids = !s->show_ids;
5444 break;
5445 case 'l':
5446 if (!s->selected_entry)
5447 break;
5448 if (view_is_parent_view(view))
5449 begin_x = view_split_begin_x(view->begin_x);
5450 err = log_selected_tree_entry(&log_view, begin_x, s);
5451 view->focussed = 0;
5452 log_view->focussed = 1;
5453 if (view_is_parent_view(view)) {
5454 err = view_close_child(view);
5455 if (err)
5456 return err;
5457 view_set_child(view, log_view);
5458 view->focus_child = 1;
5459 } else
5460 *new_view = log_view;
5461 break;
5462 case 'r':
5463 if (view_is_parent_view(view))
5464 begin_x = view_split_begin_x(view->begin_x);
5465 ref_view = view_open(view->nlines, view->ncols,
5466 view->begin_y, begin_x, TOG_VIEW_REF);
5467 if (ref_view == NULL)
5468 return got_error_from_errno("view_open");
5469 err = open_ref_view(ref_view, s->repo);
5470 if (err) {
5471 view_close(ref_view);
5472 return err;
5474 view->focussed = 0;
5475 ref_view->focussed = 1;
5476 if (view_is_parent_view(view)) {
5477 err = view_close_child(view);
5478 if (err)
5479 return err;
5480 view_set_child(view, ref_view);
5481 view->focus_child = 1;
5482 } else
5483 *new_view = ref_view;
5484 break;
5485 case 'g':
5486 case KEY_HOME:
5487 s->selected = 0;
5488 if (s->tree == s->root)
5489 s->first_displayed_entry =
5490 got_object_tree_get_first_entry(s->tree);
5491 else
5492 s->first_displayed_entry = NULL;
5493 break;
5494 case 'G':
5495 case KEY_END:
5496 s->selected = 0;
5497 te = got_object_tree_get_last_entry(s->tree);
5498 for (n = 0; n < view->nlines - 3; n++) {
5499 if (te == NULL) {
5500 if(s->tree != s->root) {
5501 s->first_displayed_entry = NULL;
5502 n++;
5504 break;
5506 s->first_displayed_entry = te;
5507 te = got_tree_entry_get_prev(s->tree, te);
5509 if (n > 0)
5510 s->selected = n - 1;
5511 break;
5512 case 'k':
5513 case KEY_UP:
5514 if (s->selected > 0) {
5515 s->selected--;
5516 break;
5518 tree_scroll_up(s, 1);
5519 break;
5520 case KEY_PPAGE:
5521 case CTRL('b'):
5522 if (s->tree == s->root) {
5523 if (got_object_tree_get_first_entry(s->tree) ==
5524 s->first_displayed_entry)
5525 s->selected = 0;
5526 } else {
5527 if (s->first_displayed_entry == NULL)
5528 s->selected = 0;
5530 tree_scroll_up(s, MAX(0, view->nlines - 3));
5531 break;
5532 case 'j':
5533 case KEY_DOWN:
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);
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 KEY_ENTER:
6236 case '\r':
6237 if (!s->selected_entry)
6238 break;
6239 if (view_is_parent_view(view))
6240 begin_x = view_split_begin_x(view->begin_x);
6241 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6242 s->repo);
6243 view->focussed = 0;
6244 log_view->focussed = 1;
6245 if (view_is_parent_view(view)) {
6246 err = view_close_child(view);
6247 if (err)
6248 return err;
6249 view_set_child(view, log_view);
6250 view->focus_child = 1;
6251 } else
6252 *new_view = log_view;
6253 break;
6254 case 't':
6255 if (!s->selected_entry)
6256 break;
6257 if (view_is_parent_view(view))
6258 begin_x = view_split_begin_x(view->begin_x);
6259 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6260 s->repo);
6261 if (err || tree_view == NULL)
6262 break;
6263 view->focussed = 0;
6264 tree_view->focussed = 1;
6265 if (view_is_parent_view(view)) {
6266 err = view_close_child(view);
6267 if (err)
6268 return err;
6269 view_set_child(view, tree_view);
6270 view->focus_child = 1;
6271 } else
6272 *new_view = tree_view;
6273 break;
6274 case 'g':
6275 case KEY_HOME:
6276 s->selected = 0;
6277 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6278 break;
6279 case 'G':
6280 case KEY_END:
6281 s->selected = 0;
6282 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6283 for (n = 0; n < view->nlines - 1; n++) {
6284 if (re == NULL)
6285 break;
6286 s->first_displayed_entry = re;
6287 re = TAILQ_PREV(re, tog_reflist_head, entry);
6289 if (n > 0)
6290 s->selected = n - 1;
6291 break;
6292 case 'k':
6293 case KEY_UP:
6294 if (s->selected > 0) {
6295 s->selected--;
6296 break;
6298 ref_scroll_up(s, 1);
6299 break;
6300 case KEY_PPAGE:
6301 case CTRL('b'):
6302 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6303 s->selected = 0;
6304 ref_scroll_up(s, MAX(0, view->nlines - 1));
6305 break;
6306 case 'j':
6307 case KEY_DOWN:
6308 if (s->selected < s->ndisplayed - 1) {
6309 s->selected++;
6310 break;
6312 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6313 /* can't scroll any further */
6314 break;
6315 ref_scroll_down(s, 1);
6316 break;
6317 case KEY_NPAGE:
6318 case CTRL('f'):
6319 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6320 /* can't scroll any further; move cursor down */
6321 if (s->selected < s->ndisplayed - 1)
6322 s->selected = s->ndisplayed - 1;
6323 break;
6325 ref_scroll_down(s, view->nlines - 1);
6326 break;
6327 case CTRL('l'):
6328 tog_free_refs();
6329 err = tog_load_refs(s->repo);
6330 if (err)
6331 break;
6332 ref_view_free_refs(s);
6333 err = ref_view_load_refs(s);
6334 break;
6335 case KEY_RESIZE:
6336 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6337 s->selected = view->nlines - 2;
6338 break;
6339 default:
6340 break;
6343 return err;
6346 __dead static void
6347 usage_ref(void)
6349 endwin();
6350 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6351 getprogname());
6352 exit(1);
6355 static const struct got_error *
6356 cmd_ref(int argc, char *argv[])
6358 const struct got_error *error;
6359 struct got_repository *repo = NULL;
6360 struct got_worktree *worktree = NULL;
6361 char *cwd = NULL, *repo_path = NULL;
6362 int ch;
6363 struct tog_view *view;
6365 while ((ch = getopt(argc, argv, "r:")) != -1) {
6366 switch (ch) {
6367 case 'r':
6368 repo_path = realpath(optarg, NULL);
6369 if (repo_path == NULL)
6370 return got_error_from_errno2("realpath",
6371 optarg);
6372 break;
6373 default:
6374 usage_ref();
6375 /* NOTREACHED */
6379 argc -= optind;
6380 argv += optind;
6382 if (argc > 1)
6383 usage_ref();
6385 if (repo_path == NULL) {
6386 cwd = getcwd(NULL, 0);
6387 if (cwd == NULL)
6388 return got_error_from_errno("getcwd");
6389 error = got_worktree_open(&worktree, cwd);
6390 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6391 goto done;
6392 if (worktree)
6393 repo_path =
6394 strdup(got_worktree_get_repo_path(worktree));
6395 else
6396 repo_path = strdup(cwd);
6397 if (repo_path == NULL) {
6398 error = got_error_from_errno("strdup");
6399 goto done;
6403 error = got_repo_open(&repo, repo_path, NULL);
6404 if (error != NULL)
6405 goto done;
6407 init_curses();
6409 error = apply_unveil(got_repo_get_path(repo), NULL);
6410 if (error)
6411 goto done;
6413 error = tog_load_refs(repo);
6414 if (error)
6415 goto done;
6417 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6418 if (view == NULL) {
6419 error = got_error_from_errno("view_open");
6420 goto done;
6423 error = open_ref_view(view, repo);
6424 if (error)
6425 goto done;
6427 if (worktree) {
6428 /* Release work tree lock. */
6429 got_worktree_close(worktree);
6430 worktree = NULL;
6432 error = view_loop(view);
6433 done:
6434 free(repo_path);
6435 free(cwd);
6436 if (repo) {
6437 const struct got_error *close_err = got_repo_close(repo);
6438 if (close_err)
6439 error = close_err;
6441 tog_free_refs();
6442 return error;
6445 static void
6446 list_commands(FILE *fp)
6448 size_t i;
6450 fprintf(fp, "commands:");
6451 for (i = 0; i < nitems(tog_commands); i++) {
6452 struct tog_cmd *cmd = &tog_commands[i];
6453 fprintf(fp, " %s", cmd->name);
6455 fputc('\n', fp);
6458 __dead static void
6459 usage(int hflag, int status)
6461 FILE *fp = (status == 0) ? stdout : stderr;
6463 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6464 getprogname());
6465 if (hflag) {
6466 fprintf(fp, "lazy usage: %s path\n", getprogname());
6467 list_commands(fp);
6469 exit(status);
6472 static char **
6473 make_argv(int argc, ...)
6475 va_list ap;
6476 char **argv;
6477 int i;
6479 va_start(ap, argc);
6481 argv = calloc(argc, sizeof(char *));
6482 if (argv == NULL)
6483 err(1, "calloc");
6484 for (i = 0; i < argc; i++) {
6485 argv[i] = strdup(va_arg(ap, char *));
6486 if (argv[i] == NULL)
6487 err(1, "strdup");
6490 va_end(ap);
6491 return argv;
6495 * Try to convert 'tog path' into a 'tog log path' command.
6496 * The user could simply have mistyped the command rather than knowingly
6497 * provided a path. So check whether argv[0] can in fact be resolved
6498 * to a path in the HEAD commit and print a special error if not.
6499 * This hack is for mpi@ <3
6501 static const struct got_error *
6502 tog_log_with_path(int argc, char *argv[])
6504 const struct got_error *error = NULL, *close_err;
6505 struct tog_cmd *cmd = NULL;
6506 struct got_repository *repo = NULL;
6507 struct got_worktree *worktree = NULL;
6508 struct got_object_id *commit_id = NULL, *id = NULL;
6509 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6510 char *commit_id_str = NULL, **cmd_argv = NULL;
6512 cwd = getcwd(NULL, 0);
6513 if (cwd == NULL)
6514 return got_error_from_errno("getcwd");
6516 error = got_worktree_open(&worktree, cwd);
6517 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6518 goto done;
6520 if (worktree)
6521 repo_path = strdup(got_worktree_get_repo_path(worktree));
6522 else
6523 repo_path = strdup(cwd);
6524 if (repo_path == NULL) {
6525 error = got_error_from_errno("strdup");
6526 goto done;
6529 error = got_repo_open(&repo, repo_path, NULL);
6530 if (error != NULL)
6531 goto done;
6533 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6534 repo, worktree);
6535 if (error)
6536 goto done;
6538 error = tog_load_refs(repo);
6539 if (error)
6540 goto done;
6541 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6542 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6543 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6544 if (error)
6545 goto done;
6547 if (worktree) {
6548 got_worktree_close(worktree);
6549 worktree = NULL;
6552 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6553 if (error) {
6554 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6555 goto done;
6556 fprintf(stderr, "%s: '%s' is no known command or path\n",
6557 getprogname(), argv[0]);
6558 usage(1, 1);
6559 /* not reached */
6562 close_err = got_repo_close(repo);
6563 if (error == NULL)
6564 error = close_err;
6565 repo = NULL;
6567 error = got_object_id_str(&commit_id_str, commit_id);
6568 if (error)
6569 goto done;
6571 cmd = &tog_commands[0]; /* log */
6572 argc = 4;
6573 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6574 error = cmd->cmd_main(argc, cmd_argv);
6575 done:
6576 if (repo) {
6577 close_err = got_repo_close(repo);
6578 if (error == NULL)
6579 error = close_err;
6581 if (worktree)
6582 got_worktree_close(worktree);
6583 free(id);
6584 free(commit_id_str);
6585 free(commit_id);
6586 free(cwd);
6587 free(repo_path);
6588 free(in_repo_path);
6589 if (cmd_argv) {
6590 int i;
6591 for (i = 0; i < argc; i++)
6592 free(cmd_argv[i]);
6593 free(cmd_argv);
6595 tog_free_refs();
6596 return error;
6600 main(int argc, char *argv[])
6602 const struct got_error *error = NULL;
6603 struct tog_cmd *cmd = NULL;
6604 int ch, hflag = 0, Vflag = 0;
6605 char **cmd_argv = NULL;
6606 static struct option longopts[] = {
6607 { "version", no_argument, NULL, 'V' },
6608 { NULL, 0, NULL, 0}
6611 setlocale(LC_CTYPE, "");
6613 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6614 switch (ch) {
6615 case 'h':
6616 hflag = 1;
6617 break;
6618 case 'V':
6619 Vflag = 1;
6620 break;
6621 default:
6622 usage(hflag, 1);
6623 /* NOTREACHED */
6627 argc -= optind;
6628 argv += optind;
6629 optind = 1;
6630 optreset = 1;
6632 if (Vflag) {
6633 got_version_print_str();
6634 return 0;
6637 #ifndef PROFILE
6638 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6639 NULL) == -1)
6640 err(1, "pledge");
6641 #endif
6643 if (argc == 0) {
6644 if (hflag)
6645 usage(hflag, 0);
6646 /* Build an argument vector which runs a default command. */
6647 cmd = &tog_commands[0];
6648 argc = 1;
6649 cmd_argv = make_argv(argc, cmd->name);
6650 } else {
6651 size_t i;
6653 /* Did the user specify a command? */
6654 for (i = 0; i < nitems(tog_commands); i++) {
6655 if (strncmp(tog_commands[i].name, argv[0],
6656 strlen(argv[0])) == 0) {
6657 cmd = &tog_commands[i];
6658 break;
6663 if (cmd == NULL) {
6664 if (argc != 1)
6665 usage(0, 1);
6666 /* No command specified; try log with a path */
6667 error = tog_log_with_path(argc, argv);
6668 } else {
6669 if (hflag)
6670 cmd->cmd_usage();
6671 else
6672 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6675 endwin();
6676 putchar('\n');
6677 if (cmd_argv) {
6678 int i;
6679 for (i = 0; i < argc; i++)
6680 free(cmd_argv[i]);
6681 free(cmd_argv);
6684 if (error && error->code != GOT_ERR_CANCELLED)
6685 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6686 return 0;