Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/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);
84 };
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 },
105 };
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,
113 };
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;
122 };
123 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 struct commit_queue {
125 int ncommits;
126 struct commit_queue_head head;
127 };
129 struct tog_color {
130 STAILQ_ENTRY(tog_color) entry;
131 regex_t regex;
132 short colorpair;
133 };
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;
307 };
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;
328 };
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;
347 };
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;
371 };
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;
380 };
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;
392 };
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;
409 };
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;
417 };
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;
435 };
437 struct tog_reflist_entry {
438 TAILQ_ENTRY(tog_reflist_entry) entry;
439 struct got_reference *ref;
440 int idx;
441 };
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;
454 };
456 /*
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.
472 */
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;
486 /*
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.
494 */
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;
524 };
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);
986 int
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 case CTRL('p'):
2425 if (s->first_displayed_entry == NULL)
2426 break;
2427 if (s->selected > 0)
2428 s->selected--;
2429 else
2430 log_scroll_up(s, 1);
2431 select_commit(s);
2432 break;
2433 case 'g':
2434 case KEY_HOME:
2435 s->selected = 0;
2436 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2437 select_commit(s);
2438 break;
2439 case KEY_PPAGE:
2440 case CTRL('b'):
2441 if (s->first_displayed_entry == NULL)
2442 break;
2443 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2444 s->selected = 0;
2445 else
2446 log_scroll_up(s, view->nlines - 1);
2447 select_commit(s);
2448 break;
2449 case 'j':
2450 case KEY_DOWN:
2451 case '>':
2452 case '.':
2453 case CTRL('n'):
2454 if (s->first_displayed_entry == NULL)
2455 break;
2456 if (s->selected < MIN(view->nlines - 2,
2457 s->commits.ncommits - 1))
2458 s->selected++;
2459 else {
2460 err = log_scroll_down(view, 1);
2461 if (err)
2462 break;
2464 select_commit(s);
2465 break;
2466 case 'G':
2467 case KEY_END: {
2468 /* We don't know yet how many commits, so we're forced to
2469 * traverse them all. */
2470 if (!s->thread_args.log_complete) {
2471 s->thread_args.load_all = 1;
2472 return trigger_log_thread(view, 0);
2475 s->selected = 0;
2476 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2477 for (n = 0; n < view->nlines - 1; n++) {
2478 if (entry == NULL)
2479 break;
2480 s->first_displayed_entry = entry;
2481 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2483 if (n > 0)
2484 s->selected = n - 1;
2485 select_commit(s);
2486 break;
2488 case KEY_NPAGE:
2489 case CTRL('f'): {
2490 struct commit_queue_entry *first;
2491 first = s->first_displayed_entry;
2492 if (first == NULL)
2493 break;
2494 err = log_scroll_down(view, view->nlines - 1);
2495 if (err)
2496 break;
2497 if (first == s->first_displayed_entry &&
2498 s->selected < MIN(view->nlines - 2,
2499 s->commits.ncommits - 1)) {
2500 /* can't scroll further down */
2501 s->selected = MIN(view->nlines - 2,
2502 s->commits.ncommits - 1);
2504 select_commit(s);
2505 break;
2507 case KEY_RESIZE:
2508 if (s->selected > view->nlines - 2)
2509 s->selected = view->nlines - 2;
2510 if (s->selected > s->commits.ncommits - 1)
2511 s->selected = s->commits.ncommits - 1;
2512 select_commit(s);
2513 if (s->commits.ncommits < view->nlines - 1 &&
2514 !s->thread_args.log_complete) {
2515 s->thread_args.commits_needed += (view->nlines - 1) -
2516 s->commits.ncommits;
2517 err = trigger_log_thread(view, 1);
2519 break;
2520 case KEY_ENTER:
2521 case ' ':
2522 case '\r':
2523 if (s->selected_entry == NULL)
2524 break;
2525 if (view_is_parent_view(view))
2526 begin_x = view_split_begin_x(view->begin_x);
2527 err = open_diff_view_for_commit(&diff_view, begin_x,
2528 s->selected_entry->commit, s->selected_entry->id,
2529 view, s->repo);
2530 if (err)
2531 break;
2532 view->focussed = 0;
2533 diff_view->focussed = 1;
2534 if (view_is_parent_view(view)) {
2535 err = view_close_child(view);
2536 if (err)
2537 return err;
2538 view_set_child(view, diff_view);
2539 view->focus_child = 1;
2540 } else
2541 *new_view = diff_view;
2542 break;
2543 case 't':
2544 if (s->selected_entry == NULL)
2545 break;
2546 if (view_is_parent_view(view))
2547 begin_x = view_split_begin_x(view->begin_x);
2548 err = browse_commit_tree(&tree_view, begin_x,
2549 s->selected_entry, s->in_repo_path, s->head_ref_name,
2550 s->repo);
2551 if (err)
2552 break;
2553 view->focussed = 0;
2554 tree_view->focussed = 1;
2555 if (view_is_parent_view(view)) {
2556 err = view_close_child(view);
2557 if (err)
2558 return err;
2559 view_set_child(view, tree_view);
2560 view->focus_child = 1;
2561 } else
2562 *new_view = tree_view;
2563 break;
2564 case KEY_BACKSPACE:
2565 case CTRL('l'):
2566 case 'B':
2567 if (ch == KEY_BACKSPACE &&
2568 got_path_is_root_dir(s->in_repo_path))
2569 break;
2570 err = stop_log_thread(s);
2571 if (err)
2572 return err;
2573 if (ch == KEY_BACKSPACE) {
2574 char *parent_path;
2575 err = got_path_dirname(&parent_path, s->in_repo_path);
2576 if (err)
2577 return err;
2578 free(s->in_repo_path);
2579 s->in_repo_path = parent_path;
2580 s->thread_args.in_repo_path = s->in_repo_path;
2581 } else if (ch == CTRL('l')) {
2582 struct got_object_id *start_id;
2583 err = got_repo_match_object_id(&start_id, NULL,
2584 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2585 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2586 if (err)
2587 return err;
2588 free(s->start_id);
2589 s->start_id = start_id;
2590 s->thread_args.start_id = s->start_id;
2591 } else /* 'B' */
2592 s->log_branches = !s->log_branches;
2594 err = got_repo_open(&s->thread_args.repo,
2595 got_repo_get_path(s->repo), NULL);
2596 if (err)
2597 return err;
2598 tog_free_refs();
2599 err = tog_load_refs(s->repo);
2600 if (err)
2601 return err;
2602 err = got_commit_graph_open(&s->thread_args.graph,
2603 s->in_repo_path, !s->log_branches);
2604 if (err)
2605 return err;
2606 err = got_commit_graph_iter_start(s->thread_args.graph,
2607 s->start_id, s->repo, NULL, NULL);
2608 if (err)
2609 return err;
2610 free_commits(&s->commits);
2611 s->first_displayed_entry = NULL;
2612 s->last_displayed_entry = NULL;
2613 s->selected_entry = NULL;
2614 s->selected = 0;
2615 s->thread_args.log_complete = 0;
2616 s->quit = 0;
2617 s->thread_args.commits_needed = view->nlines;
2618 break;
2619 case 'r':
2620 if (view_is_parent_view(view))
2621 begin_x = view_split_begin_x(view->begin_x);
2622 ref_view = view_open(view->nlines, view->ncols,
2623 view->begin_y, begin_x, TOG_VIEW_REF);
2624 if (ref_view == NULL)
2625 return got_error_from_errno("view_open");
2626 err = open_ref_view(ref_view, s->repo);
2627 if (err) {
2628 view_close(ref_view);
2629 return err;
2631 view->focussed = 0;
2632 ref_view->focussed = 1;
2633 if (view_is_parent_view(view)) {
2634 err = view_close_child(view);
2635 if (err)
2636 return err;
2637 view_set_child(view, ref_view);
2638 view->focus_child = 1;
2639 } else
2640 *new_view = ref_view;
2641 break;
2642 default:
2643 break;
2646 return err;
2649 static const struct got_error *
2650 apply_unveil(const char *repo_path, const char *worktree_path)
2652 const struct got_error *error;
2654 #ifdef PROFILE
2655 if (unveil("gmon.out", "rwc") != 0)
2656 return got_error_from_errno2("unveil", "gmon.out");
2657 #endif
2658 if (repo_path && unveil(repo_path, "r") != 0)
2659 return got_error_from_errno2("unveil", repo_path);
2661 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2662 return got_error_from_errno2("unveil", worktree_path);
2664 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2665 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2667 error = got_privsep_unveil_exec_helpers();
2668 if (error != NULL)
2669 return error;
2671 if (unveil(NULL, NULL) != 0)
2672 return got_error_from_errno("unveil");
2674 return NULL;
2677 static void
2678 init_curses(void)
2680 initscr();
2681 cbreak();
2682 halfdelay(1); /* Do fast refresh while initial view is loading. */
2683 noecho();
2684 nonl();
2685 intrflush(stdscr, FALSE);
2686 keypad(stdscr, TRUE);
2687 curs_set(0);
2688 if (getenv("TOG_COLORS") != NULL) {
2689 start_color();
2690 use_default_colors();
2692 signal(SIGWINCH, tog_sigwinch);
2693 signal(SIGPIPE, tog_sigpipe);
2694 signal(SIGCONT, tog_sigcont);
2697 static const struct got_error *
2698 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2699 struct got_repository *repo, struct got_worktree *worktree)
2701 const struct got_error *err = NULL;
2703 if (argc == 0) {
2704 *in_repo_path = strdup("/");
2705 if (*in_repo_path == NULL)
2706 return got_error_from_errno("strdup");
2707 return NULL;
2710 if (worktree) {
2711 const char *prefix = got_worktree_get_path_prefix(worktree);
2712 char *p;
2714 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2715 if (err)
2716 return err;
2717 if (asprintf(in_repo_path, "%s%s%s", prefix,
2718 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2719 p) == -1) {
2720 err = got_error_from_errno("asprintf");
2721 *in_repo_path = NULL;
2723 free(p);
2724 } else
2725 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2727 return err;
2730 static const struct got_error *
2731 cmd_log(int argc, char *argv[])
2733 const struct got_error *error;
2734 struct got_repository *repo = NULL;
2735 struct got_worktree *worktree = NULL;
2736 struct got_object_id *start_id = NULL;
2737 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2738 char *start_commit = NULL, *label = NULL;
2739 struct got_reference *ref = NULL;
2740 const char *head_ref_name = NULL;
2741 int ch, log_branches = 0;
2742 struct tog_view *view;
2744 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2745 switch (ch) {
2746 case 'b':
2747 log_branches = 1;
2748 break;
2749 case 'c':
2750 start_commit = optarg;
2751 break;
2752 case 'r':
2753 repo_path = realpath(optarg, NULL);
2754 if (repo_path == NULL)
2755 return got_error_from_errno2("realpath",
2756 optarg);
2757 break;
2758 default:
2759 usage_log();
2760 /* NOTREACHED */
2764 argc -= optind;
2765 argv += optind;
2767 if (argc > 1)
2768 usage_log();
2770 if (repo_path == NULL) {
2771 cwd = getcwd(NULL, 0);
2772 if (cwd == NULL)
2773 return got_error_from_errno("getcwd");
2774 error = got_worktree_open(&worktree, cwd);
2775 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2776 goto done;
2777 if (worktree)
2778 repo_path =
2779 strdup(got_worktree_get_repo_path(worktree));
2780 else
2781 repo_path = strdup(cwd);
2782 if (repo_path == NULL) {
2783 error = got_error_from_errno("strdup");
2784 goto done;
2788 error = got_repo_open(&repo, repo_path, NULL);
2789 if (error != NULL)
2790 goto done;
2792 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2793 repo, worktree);
2794 if (error)
2795 goto done;
2797 init_curses();
2799 error = apply_unveil(got_repo_get_path(repo),
2800 worktree ? got_worktree_get_root_path(worktree) : NULL);
2801 if (error)
2802 goto done;
2804 /* already loaded by tog_log_with_path()? */
2805 if (TAILQ_EMPTY(&tog_refs)) {
2806 error = tog_load_refs(repo);
2807 if (error)
2808 goto done;
2811 if (start_commit == NULL) {
2812 error = got_repo_match_object_id(&start_id, &label,
2813 worktree ? got_worktree_get_head_ref_name(worktree) :
2814 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2815 if (error)
2816 goto done;
2817 head_ref_name = label;
2818 } else {
2819 error = got_ref_open(&ref, repo, start_commit, 0);
2820 if (error == NULL)
2821 head_ref_name = got_ref_get_name(ref);
2822 else if (error->code != GOT_ERR_NOT_REF)
2823 goto done;
2824 error = got_repo_match_object_id(&start_id, NULL,
2825 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2826 if (error)
2827 goto done;
2830 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2831 if (view == NULL) {
2832 error = got_error_from_errno("view_open");
2833 goto done;
2835 error = open_log_view(view, start_id, repo, head_ref_name,
2836 in_repo_path, log_branches);
2837 if (error)
2838 goto done;
2839 if (worktree) {
2840 /* Release work tree lock. */
2841 got_worktree_close(worktree);
2842 worktree = NULL;
2844 error = view_loop(view);
2845 done:
2846 free(in_repo_path);
2847 free(repo_path);
2848 free(cwd);
2849 free(start_id);
2850 free(label);
2851 if (ref)
2852 got_ref_close(ref);
2853 if (repo) {
2854 const struct got_error *close_err = got_repo_close(repo);
2855 if (error == NULL)
2856 error = close_err;
2858 if (worktree)
2859 got_worktree_close(worktree);
2860 tog_free_refs();
2861 return error;
2864 __dead static void
2865 usage_diff(void)
2867 endwin();
2868 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2869 "[-w] object1 object2\n", getprogname());
2870 exit(1);
2873 static int
2874 match_line(const char *line, regex_t *regex, size_t nmatch,
2875 regmatch_t *regmatch)
2877 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2880 struct tog_color *
2881 match_color(struct tog_colors *colors, const char *line)
2883 struct tog_color *tc = NULL;
2885 STAILQ_FOREACH(tc, colors, entry) {
2886 if (match_line(line, &tc->regex, 0, NULL))
2887 return tc;
2890 return NULL;
2893 static const struct got_error *
2894 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2895 WINDOW *window, regmatch_t *regmatch)
2897 const struct got_error *err = NULL;
2898 wchar_t *wline;
2899 int width;
2900 char *s;
2902 *wtotal = 0;
2904 s = strndup(line, regmatch->rm_so);
2905 if (s == NULL)
2906 return got_error_from_errno("strndup");
2908 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2909 if (err) {
2910 free(s);
2911 return err;
2913 waddwstr(window, wline);
2914 free(wline);
2915 free(s);
2916 wlimit -= width;
2917 *wtotal += width;
2919 if (wlimit > 0) {
2920 s = strndup(line + regmatch->rm_so,
2921 regmatch->rm_eo - regmatch->rm_so);
2922 if (s == NULL) {
2923 err = got_error_from_errno("strndup");
2924 free(s);
2925 return err;
2927 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2928 if (err) {
2929 free(s);
2930 return err;
2932 wattr_on(window, A_STANDOUT, NULL);
2933 waddwstr(window, wline);
2934 wattr_off(window, A_STANDOUT, NULL);
2935 free(wline);
2936 free(s);
2937 wlimit -= width;
2938 *wtotal += width;
2941 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2942 err = format_line(&wline, &width,
2943 line + regmatch->rm_eo, wlimit, col_tab_align);
2944 if (err)
2945 return err;
2946 waddwstr(window, wline);
2947 free(wline);
2948 *wtotal += width;
2951 return NULL;
2954 static const struct got_error *
2955 draw_file(struct tog_view *view, const char *header)
2957 struct tog_diff_view_state *s = &view->state.diff;
2958 regmatch_t *regmatch = &view->regmatch;
2959 const struct got_error *err;
2960 int nprinted = 0;
2961 char *line;
2962 size_t linesize = 0;
2963 ssize_t linelen;
2964 struct tog_color *tc;
2965 wchar_t *wline;
2966 int width;
2967 int max_lines = view->nlines;
2968 int nlines = s->nlines;
2969 off_t line_offset;
2971 line_offset = s->line_offsets[s->first_displayed_line - 1];
2972 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2973 return got_error_from_errno("fseek");
2975 werase(view->window);
2977 if (header) {
2978 if (asprintf(&line, "[%d/%d] %s",
2979 s->first_displayed_line - 1 + s->selected_line, nlines,
2980 header) == -1)
2981 return got_error_from_errno("asprintf");
2982 err = format_line(&wline, &width, line, view->ncols, 0);
2983 free(line);
2984 if (err)
2985 return err;
2987 if (view_needs_focus_indication(view))
2988 wstandout(view->window);
2989 waddwstr(view->window, wline);
2990 free(wline);
2991 wline = NULL;
2992 if (view_needs_focus_indication(view))
2993 wstandend(view->window);
2994 if (width <= view->ncols - 1)
2995 waddch(view->window, '\n');
2997 if (max_lines <= 1)
2998 return NULL;
2999 max_lines--;
3002 s->eof = 0;
3003 line = NULL;
3004 while (max_lines > 0 && nprinted < max_lines) {
3005 linelen = getline(&line, &linesize, s->f);
3006 if (linelen == -1) {
3007 if (feof(s->f)) {
3008 s->eof = 1;
3009 break;
3011 free(line);
3012 return got_ferror(s->f, GOT_ERR_IO);
3015 tc = match_color(&s->colors, line);
3016 if (tc)
3017 wattr_on(view->window,
3018 COLOR_PAIR(tc->colorpair), NULL);
3019 if (s->first_displayed_line + nprinted == s->matched_line &&
3020 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3021 err = add_matched_line(&width, line, view->ncols, 0,
3022 view->window, regmatch);
3023 if (err) {
3024 free(line);
3025 return err;
3027 } else {
3028 err = format_line(&wline, &width, line, view->ncols, 0);
3029 if (err) {
3030 free(line);
3031 return err;
3033 waddwstr(view->window, wline);
3034 free(wline);
3035 wline = NULL;
3037 if (tc)
3038 wattr_off(view->window,
3039 COLOR_PAIR(tc->colorpair), NULL);
3040 if (width <= view->ncols - 1)
3041 waddch(view->window, '\n');
3042 nprinted++;
3044 free(line);
3045 if (nprinted >= 1)
3046 s->last_displayed_line = s->first_displayed_line +
3047 (nprinted - 1);
3048 else
3049 s->last_displayed_line = s->first_displayed_line;
3051 view_vborder(view);
3053 if (s->eof) {
3054 while (nprinted < view->nlines) {
3055 waddch(view->window, '\n');
3056 nprinted++;
3059 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3060 if (err) {
3061 return err;
3064 wstandout(view->window);
3065 waddwstr(view->window, wline);
3066 free(wline);
3067 wline = NULL;
3068 wstandend(view->window);
3071 return NULL;
3074 static char *
3075 get_datestr(time_t *time, char *datebuf)
3077 struct tm mytm, *tm;
3078 char *p, *s;
3080 tm = gmtime_r(time, &mytm);
3081 if (tm == NULL)
3082 return NULL;
3083 s = asctime_r(tm, datebuf);
3084 if (s == NULL)
3085 return NULL;
3086 p = strchr(s, '\n');
3087 if (p)
3088 *p = '\0';
3089 return s;
3092 static const struct got_error *
3093 get_changed_paths(struct got_pathlist_head *paths,
3094 struct got_commit_object *commit, struct got_repository *repo)
3096 const struct got_error *err = NULL;
3097 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3098 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3099 struct got_object_qid *qid;
3101 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3102 if (qid != NULL) {
3103 struct got_commit_object *pcommit;
3104 err = got_object_open_as_commit(&pcommit, repo,
3105 qid->id);
3106 if (err)
3107 return err;
3109 tree_id1 = got_object_id_dup(
3110 got_object_commit_get_tree_id(pcommit));
3111 if (tree_id1 == NULL) {
3112 got_object_commit_close(pcommit);
3113 return got_error_from_errno("got_object_id_dup");
3115 got_object_commit_close(pcommit);
3119 if (tree_id1) {
3120 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3121 if (err)
3122 goto done;
3125 tree_id2 = got_object_commit_get_tree_id(commit);
3126 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3127 if (err)
3128 goto done;
3130 err = got_diff_tree(tree1, tree2, "", "", repo,
3131 got_diff_tree_collect_changed_paths, paths, 0);
3132 done:
3133 if (tree1)
3134 got_object_tree_close(tree1);
3135 if (tree2)
3136 got_object_tree_close(tree2);
3137 free(tree_id1);
3138 return err;
3141 static const struct got_error *
3142 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3144 off_t *p;
3146 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3147 if (p == NULL)
3148 return got_error_from_errno("reallocarray");
3149 *line_offsets = p;
3150 (*line_offsets)[*nlines] = off;
3151 (*nlines)++;
3152 return NULL;
3155 static const struct got_error *
3156 write_commit_info(off_t **line_offsets, size_t *nlines,
3157 struct got_object_id *commit_id, struct got_reflist_head *refs,
3158 struct got_repository *repo, FILE *outfile)
3160 const struct got_error *err = NULL;
3161 char datebuf[26], *datestr;
3162 struct got_commit_object *commit;
3163 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3164 time_t committer_time;
3165 const char *author, *committer;
3166 char *refs_str = NULL;
3167 struct got_pathlist_head changed_paths;
3168 struct got_pathlist_entry *pe;
3169 off_t outoff = 0;
3170 int n;
3172 TAILQ_INIT(&changed_paths);
3174 if (refs) {
3175 err = build_refs_str(&refs_str, refs, commit_id, repo);
3176 if (err)
3177 return err;
3180 err = got_object_open_as_commit(&commit, repo, commit_id);
3181 if (err)
3182 return err;
3184 err = got_object_id_str(&id_str, commit_id);
3185 if (err) {
3186 err = got_error_from_errno("got_object_id_str");
3187 goto done;
3190 err = add_line_offset(line_offsets, nlines, 0);
3191 if (err)
3192 goto done;
3194 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3195 refs_str ? refs_str : "", refs_str ? ")" : "");
3196 if (n < 0) {
3197 err = got_error_from_errno("fprintf");
3198 goto done;
3200 outoff += n;
3201 err = add_line_offset(line_offsets, nlines, outoff);
3202 if (err)
3203 goto done;
3205 n = fprintf(outfile, "from: %s\n",
3206 got_object_commit_get_author(commit));
3207 if (n < 0) {
3208 err = got_error_from_errno("fprintf");
3209 goto done;
3211 outoff += n;
3212 err = add_line_offset(line_offsets, nlines, outoff);
3213 if (err)
3214 goto done;
3216 committer_time = got_object_commit_get_committer_time(commit);
3217 datestr = get_datestr(&committer_time, datebuf);
3218 if (datestr) {
3219 n = fprintf(outfile, "date: %s UTC\n", datestr);
3220 if (n < 0) {
3221 err = got_error_from_errno("fprintf");
3222 goto done;
3224 outoff += n;
3225 err = add_line_offset(line_offsets, nlines, outoff);
3226 if (err)
3227 goto done;
3229 author = got_object_commit_get_author(commit);
3230 committer = got_object_commit_get_committer(commit);
3231 if (strcmp(author, committer) != 0) {
3232 n = fprintf(outfile, "via: %s\n", committer);
3233 if (n < 0) {
3234 err = got_error_from_errno("fprintf");
3235 goto done;
3237 outoff += n;
3238 err = add_line_offset(line_offsets, nlines, outoff);
3239 if (err)
3240 goto done;
3242 if (got_object_commit_get_nparents(commit) > 1) {
3243 const struct got_object_id_queue *parent_ids;
3244 struct got_object_qid *qid;
3245 int pn = 1;
3246 parent_ids = got_object_commit_get_parent_ids(commit);
3247 STAILQ_FOREACH(qid, parent_ids, entry) {
3248 err = got_object_id_str(&id_str, qid->id);
3249 if (err)
3250 goto done;
3251 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3252 if (n < 0) {
3253 err = got_error_from_errno("fprintf");
3254 goto done;
3256 outoff += n;
3257 err = add_line_offset(line_offsets, nlines, outoff);
3258 if (err)
3259 goto done;
3260 free(id_str);
3261 id_str = NULL;
3265 err = got_object_commit_get_logmsg(&logmsg, commit);
3266 if (err)
3267 goto done;
3268 s = logmsg;
3269 while ((line = strsep(&s, "\n")) != NULL) {
3270 n = fprintf(outfile, "%s\n", line);
3271 if (n < 0) {
3272 err = got_error_from_errno("fprintf");
3273 goto done;
3275 outoff += n;
3276 err = add_line_offset(line_offsets, nlines, outoff);
3277 if (err)
3278 goto done;
3281 err = get_changed_paths(&changed_paths, commit, repo);
3282 if (err)
3283 goto done;
3284 TAILQ_FOREACH(pe, &changed_paths, entry) {
3285 struct got_diff_changed_path *cp = pe->data;
3286 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3287 if (n < 0) {
3288 err = got_error_from_errno("fprintf");
3289 goto done;
3291 outoff += n;
3292 err = add_line_offset(line_offsets, nlines, outoff);
3293 if (err)
3294 goto done;
3295 free((char *)pe->path);
3296 free(pe->data);
3299 fputc('\n', outfile);
3300 outoff++;
3301 err = add_line_offset(line_offsets, nlines, outoff);
3302 done:
3303 got_pathlist_free(&changed_paths);
3304 free(id_str);
3305 free(logmsg);
3306 free(refs_str);
3307 got_object_commit_close(commit);
3308 if (err) {
3309 free(*line_offsets);
3310 *line_offsets = NULL;
3311 *nlines = 0;
3313 return err;
3316 static const struct got_error *
3317 create_diff(struct tog_diff_view_state *s)
3319 const struct got_error *err = NULL;
3320 FILE *f = NULL;
3321 int obj_type;
3323 free(s->line_offsets);
3324 s->line_offsets = malloc(sizeof(off_t));
3325 if (s->line_offsets == NULL)
3326 return got_error_from_errno("malloc");
3327 s->nlines = 0;
3329 f = got_opentemp();
3330 if (f == NULL) {
3331 err = got_error_from_errno("got_opentemp");
3332 goto done;
3334 if (s->f && fclose(s->f) == EOF) {
3335 err = got_error_from_errno("fclose");
3336 goto done;
3338 s->f = f;
3340 if (s->id1)
3341 err = got_object_get_type(&obj_type, s->repo, s->id1);
3342 else
3343 err = got_object_get_type(&obj_type, s->repo, s->id2);
3344 if (err)
3345 goto done;
3347 switch (obj_type) {
3348 case GOT_OBJ_TYPE_BLOB:
3349 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3350 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3351 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3352 break;
3353 case GOT_OBJ_TYPE_TREE:
3354 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3355 s->id1, s->id2, NULL, "", "", s->diff_context,
3356 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3357 break;
3358 case GOT_OBJ_TYPE_COMMIT: {
3359 const struct got_object_id_queue *parent_ids;
3360 struct got_object_qid *pid;
3361 struct got_commit_object *commit2;
3362 struct got_reflist_head *refs;
3364 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3365 if (err)
3366 goto done;
3367 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3368 /* Show commit info if we're diffing to a parent/root commit. */
3369 if (s->id1 == NULL) {
3370 err = write_commit_info(&s->line_offsets, &s->nlines,
3371 s->id2, refs, s->repo, s->f);
3372 if (err)
3373 goto done;
3374 } else {
3375 parent_ids = got_object_commit_get_parent_ids(commit2);
3376 STAILQ_FOREACH(pid, parent_ids, entry) {
3377 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3378 err = write_commit_info(
3379 &s->line_offsets, &s->nlines,
3380 s->id2, refs, s->repo, s->f);
3381 if (err)
3382 goto done;
3383 break;
3387 got_object_commit_close(commit2);
3389 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3390 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3391 s->force_text_diff, s->repo, s->f);
3392 break;
3394 default:
3395 err = got_error(GOT_ERR_OBJ_TYPE);
3396 break;
3398 if (err)
3399 goto done;
3400 done:
3401 if (s->f && fflush(s->f) != 0 && err == NULL)
3402 err = got_error_from_errno("fflush");
3403 return err;
3406 static void
3407 diff_view_indicate_progress(struct tog_view *view)
3409 mvwaddstr(view->window, 0, 0, "diffing...");
3410 update_panels();
3411 doupdate();
3414 static const struct got_error *
3415 search_start_diff_view(struct tog_view *view)
3417 struct tog_diff_view_state *s = &view->state.diff;
3419 s->matched_line = 0;
3420 return NULL;
3423 static const struct got_error *
3424 search_next_diff_view(struct tog_view *view)
3426 struct tog_diff_view_state *s = &view->state.diff;
3427 int lineno;
3428 char *line = NULL;
3429 size_t linesize = 0;
3430 ssize_t linelen;
3432 if (!view->searching) {
3433 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3434 return NULL;
3437 if (s->matched_line) {
3438 if (view->searching == TOG_SEARCH_FORWARD)
3439 lineno = s->matched_line + 1;
3440 else
3441 lineno = s->matched_line - 1;
3442 } else {
3443 if (view->searching == TOG_SEARCH_FORWARD)
3444 lineno = 1;
3445 else
3446 lineno = s->nlines;
3449 while (1) {
3450 off_t offset;
3452 if (lineno <= 0 || lineno > s->nlines) {
3453 if (s->matched_line == 0) {
3454 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3455 break;
3458 if (view->searching == TOG_SEARCH_FORWARD)
3459 lineno = 1;
3460 else
3461 lineno = s->nlines;
3464 offset = s->line_offsets[lineno - 1];
3465 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3466 free(line);
3467 return got_error_from_errno("fseeko");
3469 linelen = getline(&line, &linesize, s->f);
3470 if (linelen != -1 &&
3471 match_line(line, &view->regex, 1, &view->regmatch)) {
3472 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3473 s->matched_line = lineno;
3474 break;
3476 if (view->searching == TOG_SEARCH_FORWARD)
3477 lineno++;
3478 else
3479 lineno--;
3481 free(line);
3483 if (s->matched_line) {
3484 s->first_displayed_line = s->matched_line;
3485 s->selected_line = 1;
3488 return NULL;
3491 static const struct got_error *
3492 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3493 struct got_object_id *id2, const char *label1, const char *label2,
3494 int diff_context, int ignore_whitespace, int force_text_diff,
3495 struct tog_view *log_view, struct got_repository *repo)
3497 const struct got_error *err;
3498 struct tog_diff_view_state *s = &view->state.diff;
3500 if (id1 != NULL && id2 != NULL) {
3501 int type1, type2;
3502 err = got_object_get_type(&type1, repo, id1);
3503 if (err)
3504 return err;
3505 err = got_object_get_type(&type2, repo, id2);
3506 if (err)
3507 return err;
3509 if (type1 != type2)
3510 return got_error(GOT_ERR_OBJ_TYPE);
3512 s->first_displayed_line = 1;
3513 s->last_displayed_line = view->nlines;
3514 s->selected_line = 1;
3515 s->repo = repo;
3516 s->id1 = id1;
3517 s->id2 = id2;
3518 s->label1 = label1;
3519 s->label2 = label2;
3521 if (id1) {
3522 s->id1 = got_object_id_dup(id1);
3523 if (s->id1 == NULL)
3524 return got_error_from_errno("got_object_id_dup");
3525 } else
3526 s->id1 = NULL;
3528 s->id2 = got_object_id_dup(id2);
3529 if (s->id2 == NULL) {
3530 free(s->id1);
3531 s->id1 = NULL;
3532 return got_error_from_errno("got_object_id_dup");
3534 s->f = NULL;
3535 s->first_displayed_line = 1;
3536 s->last_displayed_line = view->nlines;
3537 s->diff_context = diff_context;
3538 s->ignore_whitespace = ignore_whitespace;
3539 s->force_text_diff = force_text_diff;
3540 s->log_view = log_view;
3541 s->repo = repo;
3543 STAILQ_INIT(&s->colors);
3544 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3545 err = add_color(&s->colors,
3546 "^-", TOG_COLOR_DIFF_MINUS,
3547 get_color_value("TOG_COLOR_DIFF_MINUS"));
3548 if (err)
3549 return err;
3550 err = add_color(&s->colors, "^\\+",
3551 TOG_COLOR_DIFF_PLUS,
3552 get_color_value("TOG_COLOR_DIFF_PLUS"));
3553 if (err) {
3554 free_colors(&s->colors);
3555 return err;
3557 err = add_color(&s->colors,
3558 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3559 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3560 if (err) {
3561 free_colors(&s->colors);
3562 return err;
3565 err = add_color(&s->colors,
3566 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3567 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3568 get_color_value("TOG_COLOR_DIFF_META"));
3569 if (err) {
3570 free_colors(&s->colors);
3571 return err;
3574 err = add_color(&s->colors,
3575 "^(from|via): ", TOG_COLOR_AUTHOR,
3576 get_color_value("TOG_COLOR_AUTHOR"));
3577 if (err) {
3578 free_colors(&s->colors);
3579 return err;
3582 err = add_color(&s->colors,
3583 "^date: ", TOG_COLOR_DATE,
3584 get_color_value("TOG_COLOR_DATE"));
3585 if (err) {
3586 free_colors(&s->colors);
3587 return err;
3591 if (log_view && view_is_splitscreen(view))
3592 show_log_view(log_view); /* draw vborder */
3593 diff_view_indicate_progress(view);
3595 s->line_offsets = NULL;
3596 s->nlines = 0;
3597 err = create_diff(s);
3598 if (err) {
3599 free(s->id1);
3600 s->id1 = NULL;
3601 free(s->id2);
3602 s->id2 = NULL;
3603 free_colors(&s->colors);
3604 return err;
3607 view->show = show_diff_view;
3608 view->input = input_diff_view;
3609 view->close = close_diff_view;
3610 view->search_start = search_start_diff_view;
3611 view->search_next = search_next_diff_view;
3613 return NULL;
3616 static const struct got_error *
3617 close_diff_view(struct tog_view *view)
3619 const struct got_error *err = NULL;
3620 struct tog_diff_view_state *s = &view->state.diff;
3622 free(s->id1);
3623 s->id1 = NULL;
3624 free(s->id2);
3625 s->id2 = NULL;
3626 if (s->f && fclose(s->f) == EOF)
3627 err = got_error_from_errno("fclose");
3628 free_colors(&s->colors);
3629 free(s->line_offsets);
3630 s->line_offsets = NULL;
3631 s->nlines = 0;
3632 return err;
3635 static const struct got_error *
3636 show_diff_view(struct tog_view *view)
3638 const struct got_error *err;
3639 struct tog_diff_view_state *s = &view->state.diff;
3640 char *id_str1 = NULL, *id_str2, *header;
3641 const char *label1, *label2;
3643 if (s->id1) {
3644 err = got_object_id_str(&id_str1, s->id1);
3645 if (err)
3646 return err;
3647 label1 = s->label1 ? : id_str1;
3648 } else
3649 label1 = "/dev/null";
3651 err = got_object_id_str(&id_str2, s->id2);
3652 if (err)
3653 return err;
3654 label2 = s->label2 ? : id_str2;
3656 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3657 err = got_error_from_errno("asprintf");
3658 free(id_str1);
3659 free(id_str2);
3660 return err;
3662 free(id_str1);
3663 free(id_str2);
3665 err = draw_file(view, header);
3666 free(header);
3667 return err;
3670 static const struct got_error *
3671 set_selected_commit(struct tog_diff_view_state *s,
3672 struct commit_queue_entry *entry)
3674 const struct got_error *err;
3675 const struct got_object_id_queue *parent_ids;
3676 struct got_commit_object *selected_commit;
3677 struct got_object_qid *pid;
3679 free(s->id2);
3680 s->id2 = got_object_id_dup(entry->id);
3681 if (s->id2 == NULL)
3682 return got_error_from_errno("got_object_id_dup");
3684 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3685 if (err)
3686 return err;
3687 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3688 free(s->id1);
3689 pid = STAILQ_FIRST(parent_ids);
3690 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3691 got_object_commit_close(selected_commit);
3692 return NULL;
3695 static const struct got_error *
3696 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3698 const struct got_error *err = NULL;
3699 struct tog_diff_view_state *s = &view->state.diff;
3700 struct tog_log_view_state *ls;
3701 struct commit_queue_entry *old_selected_entry;
3702 char *line = NULL;
3703 size_t linesize = 0;
3704 ssize_t linelen;
3705 int i;
3707 switch (ch) {
3708 case 'a':
3709 case 'w':
3710 if (ch == 'a')
3711 s->force_text_diff = !s->force_text_diff;
3712 if (ch == 'w')
3713 s->ignore_whitespace = !s->ignore_whitespace;
3714 wclear(view->window);
3715 s->first_displayed_line = 1;
3716 s->last_displayed_line = view->nlines;
3717 diff_view_indicate_progress(view);
3718 err = create_diff(s);
3719 break;
3720 case 'g':
3721 case KEY_HOME:
3722 s->first_displayed_line = 1;
3723 break;
3724 case 'G':
3725 case KEY_END:
3726 if (s->eof)
3727 break;
3729 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3730 s->eof = 1;
3731 break;
3732 case 'k':
3733 case KEY_UP:
3734 case CTRL('p'):
3735 if (s->first_displayed_line > 1)
3736 s->first_displayed_line--;
3737 break;
3738 case KEY_PPAGE:
3739 case CTRL('b'):
3740 if (s->first_displayed_line == 1)
3741 break;
3742 i = 0;
3743 while (i++ < view->nlines - 1 &&
3744 s->first_displayed_line > 1)
3745 s->first_displayed_line--;
3746 break;
3747 case 'j':
3748 case KEY_DOWN:
3749 case CTRL('n'):
3750 if (!s->eof)
3751 s->first_displayed_line++;
3752 break;
3753 case KEY_NPAGE:
3754 case CTRL('f'):
3755 case ' ':
3756 if (s->eof)
3757 break;
3758 i = 0;
3759 while (!s->eof && i++ < view->nlines - 1) {
3760 linelen = getline(&line, &linesize, s->f);
3761 s->first_displayed_line++;
3762 if (linelen == -1) {
3763 if (feof(s->f)) {
3764 s->eof = 1;
3765 } else
3766 err = got_ferror(s->f, GOT_ERR_IO);
3767 break;
3770 free(line);
3771 break;
3772 case '[':
3773 if (s->diff_context > 0) {
3774 s->diff_context--;
3775 diff_view_indicate_progress(view);
3776 err = create_diff(s);
3777 if (s->first_displayed_line + view->nlines - 1 >
3778 s->nlines) {
3779 s->first_displayed_line = 1;
3780 s->last_displayed_line = view->nlines;
3783 break;
3784 case ']':
3785 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3786 s->diff_context++;
3787 diff_view_indicate_progress(view);
3788 err = create_diff(s);
3790 break;
3791 case '<':
3792 case ',':
3793 if (s->log_view == NULL)
3794 break;
3795 ls = &s->log_view->state.log;
3796 old_selected_entry = ls->selected_entry;
3798 err = input_log_view(NULL, s->log_view, KEY_UP);
3799 if (err)
3800 break;
3802 if (old_selected_entry == ls->selected_entry)
3803 break;
3805 err = set_selected_commit(s, ls->selected_entry);
3806 if (err)
3807 break;
3809 s->first_displayed_line = 1;
3810 s->last_displayed_line = view->nlines;
3812 diff_view_indicate_progress(view);
3813 err = create_diff(s);
3814 break;
3815 case '>':
3816 case '.':
3817 if (s->log_view == NULL)
3818 break;
3819 ls = &s->log_view->state.log;
3820 old_selected_entry = ls->selected_entry;
3822 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3823 if (err)
3824 break;
3826 if (old_selected_entry == ls->selected_entry)
3827 break;
3829 err = set_selected_commit(s, ls->selected_entry);
3830 if (err)
3831 break;
3833 s->first_displayed_line = 1;
3834 s->last_displayed_line = view->nlines;
3836 diff_view_indicate_progress(view);
3837 err = create_diff(s);
3838 break;
3839 default:
3840 break;
3843 return err;
3846 static const struct got_error *
3847 cmd_diff(int argc, char *argv[])
3849 const struct got_error *error = NULL;
3850 struct got_repository *repo = NULL;
3851 struct got_worktree *worktree = NULL;
3852 struct got_object_id *id1 = NULL, *id2 = NULL;
3853 char *repo_path = NULL, *cwd = NULL;
3854 char *id_str1 = NULL, *id_str2 = NULL;
3855 char *label1 = NULL, *label2 = NULL;
3856 int diff_context = 3, ignore_whitespace = 0;
3857 int ch, force_text_diff = 0;
3858 const char *errstr;
3859 struct tog_view *view;
3861 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3862 switch (ch) {
3863 case 'a':
3864 force_text_diff = 1;
3865 break;
3866 case 'C':
3867 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3868 &errstr);
3869 if (errstr != NULL)
3870 err(1, "-C option %s", errstr);
3871 break;
3872 case 'r':
3873 repo_path = realpath(optarg, NULL);
3874 if (repo_path == NULL)
3875 return got_error_from_errno2("realpath",
3876 optarg);
3877 got_path_strip_trailing_slashes(repo_path);
3878 break;
3879 case 'w':
3880 ignore_whitespace = 1;
3881 break;
3882 default:
3883 usage_diff();
3884 /* NOTREACHED */
3888 argc -= optind;
3889 argv += optind;
3891 if (argc == 0) {
3892 usage_diff(); /* TODO show local worktree changes */
3893 } else if (argc == 2) {
3894 id_str1 = argv[0];
3895 id_str2 = argv[1];
3896 } else
3897 usage_diff();
3899 if (repo_path == NULL) {
3900 cwd = getcwd(NULL, 0);
3901 if (cwd == NULL)
3902 return got_error_from_errno("getcwd");
3903 error = got_worktree_open(&worktree, cwd);
3904 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3905 goto done;
3906 if (worktree)
3907 repo_path =
3908 strdup(got_worktree_get_repo_path(worktree));
3909 else
3910 repo_path = strdup(cwd);
3911 if (repo_path == NULL) {
3912 error = got_error_from_errno("strdup");
3913 goto done;
3917 error = got_repo_open(&repo, repo_path, NULL);
3918 if (error)
3919 goto done;
3921 init_curses();
3923 error = apply_unveil(got_repo_get_path(repo), NULL);
3924 if (error)
3925 goto done;
3927 error = tog_load_refs(repo);
3928 if (error)
3929 goto done;
3931 error = got_repo_match_object_id(&id1, &label1, id_str1,
3932 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3933 if (error)
3934 goto done;
3936 error = got_repo_match_object_id(&id2, &label2, id_str2,
3937 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3938 if (error)
3939 goto done;
3941 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3942 if (view == NULL) {
3943 error = got_error_from_errno("view_open");
3944 goto done;
3946 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3947 ignore_whitespace, force_text_diff, NULL, repo);
3948 if (error)
3949 goto done;
3950 error = view_loop(view);
3951 done:
3952 free(label1);
3953 free(label2);
3954 free(repo_path);
3955 free(cwd);
3956 if (repo) {
3957 const struct got_error *close_err = got_repo_close(repo);
3958 if (error == NULL)
3959 error = close_err;
3961 if (worktree)
3962 got_worktree_close(worktree);
3963 tog_free_refs();
3964 return error;
3967 __dead static void
3968 usage_blame(void)
3970 endwin();
3971 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3972 getprogname());
3973 exit(1);
3976 struct tog_blame_line {
3977 int annotated;
3978 struct got_object_id *id;
3981 static const struct got_error *
3982 draw_blame(struct tog_view *view)
3984 struct tog_blame_view_state *s = &view->state.blame;
3985 struct tog_blame *blame = &s->blame;
3986 regmatch_t *regmatch = &view->regmatch;
3987 const struct got_error *err;
3988 int lineno = 0, nprinted = 0;
3989 char *line = NULL;
3990 size_t linesize = 0;
3991 ssize_t linelen;
3992 wchar_t *wline;
3993 int width;
3994 struct tog_blame_line *blame_line;
3995 struct got_object_id *prev_id = NULL;
3996 char *id_str;
3997 struct tog_color *tc;
3999 err = got_object_id_str(&id_str, s->blamed_commit->id);
4000 if (err)
4001 return err;
4003 rewind(blame->f);
4004 werase(view->window);
4006 if (asprintf(&line, "commit %s", id_str) == -1) {
4007 err = got_error_from_errno("asprintf");
4008 free(id_str);
4009 return err;
4012 err = format_line(&wline, &width, line, view->ncols, 0);
4013 free(line);
4014 line = NULL;
4015 if (err)
4016 return err;
4017 if (view_needs_focus_indication(view))
4018 wstandout(view->window);
4019 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4020 if (tc)
4021 wattr_on(view->window,
4022 COLOR_PAIR(tc->colorpair), NULL);
4023 waddwstr(view->window, wline);
4024 if (tc)
4025 wattr_off(view->window,
4026 COLOR_PAIR(tc->colorpair), NULL);
4027 if (view_needs_focus_indication(view))
4028 wstandend(view->window);
4029 free(wline);
4030 wline = NULL;
4031 if (width < view->ncols - 1)
4032 waddch(view->window, '\n');
4034 if (asprintf(&line, "[%d/%d] %s%s",
4035 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4036 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4037 free(id_str);
4038 return got_error_from_errno("asprintf");
4040 free(id_str);
4041 err = format_line(&wline, &width, line, view->ncols, 0);
4042 free(line);
4043 line = NULL;
4044 if (err)
4045 return err;
4046 waddwstr(view->window, wline);
4047 free(wline);
4048 wline = NULL;
4049 if (width < view->ncols - 1)
4050 waddch(view->window, '\n');
4052 s->eof = 0;
4053 while (nprinted < view->nlines - 2) {
4054 linelen = getline(&line, &linesize, blame->f);
4055 if (linelen == -1) {
4056 if (feof(blame->f)) {
4057 s->eof = 1;
4058 break;
4060 free(line);
4061 return got_ferror(blame->f, GOT_ERR_IO);
4063 if (++lineno < s->first_displayed_line)
4064 continue;
4066 if (view->focussed && nprinted == s->selected_line - 1)
4067 wstandout(view->window);
4069 if (blame->nlines > 0) {
4070 blame_line = &blame->lines[lineno - 1];
4071 if (blame_line->annotated && prev_id &&
4072 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4073 !(view->focussed &&
4074 nprinted == s->selected_line - 1)) {
4075 waddstr(view->window, " ");
4076 } else if (blame_line->annotated) {
4077 char *id_str;
4078 err = got_object_id_str(&id_str, blame_line->id);
4079 if (err) {
4080 free(line);
4081 return err;
4083 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4084 if (tc)
4085 wattr_on(view->window,
4086 COLOR_PAIR(tc->colorpair), NULL);
4087 wprintw(view->window, "%.8s", id_str);
4088 if (tc)
4089 wattr_off(view->window,
4090 COLOR_PAIR(tc->colorpair), NULL);
4091 free(id_str);
4092 prev_id = blame_line->id;
4093 } else {
4094 waddstr(view->window, "........");
4095 prev_id = NULL;
4097 } else {
4098 waddstr(view->window, "........");
4099 prev_id = NULL;
4102 if (view->focussed && nprinted == s->selected_line - 1)
4103 wstandend(view->window);
4104 waddstr(view->window, " ");
4106 if (view->ncols <= 9) {
4107 width = 9;
4108 wline = wcsdup(L"");
4109 if (wline == NULL) {
4110 err = got_error_from_errno("wcsdup");
4111 free(line);
4112 return err;
4114 } else if (s->first_displayed_line + nprinted ==
4115 s->matched_line &&
4116 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4117 err = add_matched_line(&width, line, view->ncols - 9, 9,
4118 view->window, regmatch);
4119 if (err) {
4120 free(line);
4121 return err;
4123 width += 9;
4124 } else {
4125 err = format_line(&wline, &width, line,
4126 view->ncols - 9, 9);
4127 waddwstr(view->window, wline);
4128 free(wline);
4129 wline = NULL;
4130 width += 9;
4133 if (width <= view->ncols - 1)
4134 waddch(view->window, '\n');
4135 if (++nprinted == 1)
4136 s->first_displayed_line = lineno;
4138 free(line);
4139 s->last_displayed_line = lineno;
4141 view_vborder(view);
4143 return NULL;
4146 static const struct got_error *
4147 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4149 const struct got_error *err = NULL;
4150 struct tog_blame_cb_args *a = arg;
4151 struct tog_blame_line *line;
4152 int errcode;
4154 if (nlines != a->nlines ||
4155 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4156 return got_error(GOT_ERR_RANGE);
4158 errcode = pthread_mutex_lock(&tog_mutex);
4159 if (errcode)
4160 return got_error_set_errno(errcode, "pthread_mutex_lock");
4162 if (*a->quit) { /* user has quit the blame view */
4163 err = got_error(GOT_ERR_ITER_COMPLETED);
4164 goto done;
4167 if (lineno == -1)
4168 goto done; /* no change in this commit */
4170 line = &a->lines[lineno - 1];
4171 if (line->annotated)
4172 goto done;
4174 line->id = got_object_id_dup(id);
4175 if (line->id == NULL) {
4176 err = got_error_from_errno("got_object_id_dup");
4177 goto done;
4179 line->annotated = 1;
4180 done:
4181 errcode = pthread_mutex_unlock(&tog_mutex);
4182 if (errcode)
4183 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4184 return err;
4187 static void *
4188 blame_thread(void *arg)
4190 const struct got_error *err, *close_err;
4191 struct tog_blame_thread_args *ta = arg;
4192 struct tog_blame_cb_args *a = ta->cb_args;
4193 int errcode;
4195 err = block_signals_used_by_main_thread();
4196 if (err)
4197 return (void *)err;
4199 err = got_blame(ta->path, a->commit_id, ta->repo,
4200 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4201 if (err && err->code == GOT_ERR_CANCELLED)
4202 err = NULL;
4204 errcode = pthread_mutex_lock(&tog_mutex);
4205 if (errcode)
4206 return (void *)got_error_set_errno(errcode,
4207 "pthread_mutex_lock");
4209 close_err = got_repo_close(ta->repo);
4210 if (err == NULL)
4211 err = close_err;
4212 ta->repo = NULL;
4213 *ta->complete = 1;
4215 errcode = pthread_mutex_unlock(&tog_mutex);
4216 if (errcode && err == NULL)
4217 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4219 return (void *)err;
4222 static struct got_object_id *
4223 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4224 int first_displayed_line, int selected_line)
4226 struct tog_blame_line *line;
4228 if (nlines <= 0)
4229 return NULL;
4231 line = &lines[first_displayed_line - 1 + selected_line - 1];
4232 if (!line->annotated)
4233 return NULL;
4235 return line->id;
4238 static const struct got_error *
4239 stop_blame(struct tog_blame *blame)
4241 const struct got_error *err = NULL;
4242 int i;
4244 if (blame->thread) {
4245 int errcode;
4246 errcode = pthread_mutex_unlock(&tog_mutex);
4247 if (errcode)
4248 return got_error_set_errno(errcode,
4249 "pthread_mutex_unlock");
4250 errcode = pthread_join(blame->thread, (void **)&err);
4251 if (errcode)
4252 return got_error_set_errno(errcode, "pthread_join");
4253 errcode = pthread_mutex_lock(&tog_mutex);
4254 if (errcode)
4255 return got_error_set_errno(errcode,
4256 "pthread_mutex_lock");
4257 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4258 err = NULL;
4259 blame->thread = 0; //NULL;
4261 if (blame->thread_args.repo) {
4262 const struct got_error *close_err;
4263 close_err = got_repo_close(blame->thread_args.repo);
4264 if (err == NULL)
4265 err = close_err;
4266 blame->thread_args.repo = NULL;
4268 if (blame->f) {
4269 if (fclose(blame->f) == EOF && err == NULL)
4270 err = got_error_from_errno("fclose");
4271 blame->f = NULL;
4273 if (blame->lines) {
4274 for (i = 0; i < blame->nlines; i++)
4275 free(blame->lines[i].id);
4276 free(blame->lines);
4277 blame->lines = NULL;
4279 free(blame->cb_args.commit_id);
4280 blame->cb_args.commit_id = NULL;
4282 return err;
4285 static const struct got_error *
4286 cancel_blame_view(void *arg)
4288 const struct got_error *err = NULL;
4289 int *done = arg;
4290 int errcode;
4292 errcode = pthread_mutex_lock(&tog_mutex);
4293 if (errcode)
4294 return got_error_set_errno(errcode,
4295 "pthread_mutex_unlock");
4297 if (*done)
4298 err = got_error(GOT_ERR_CANCELLED);
4300 errcode = pthread_mutex_unlock(&tog_mutex);
4301 if (errcode)
4302 return got_error_set_errno(errcode,
4303 "pthread_mutex_lock");
4305 return err;
4308 static const struct got_error *
4309 run_blame(struct tog_view *view)
4311 struct tog_blame_view_state *s = &view->state.blame;
4312 struct tog_blame *blame = &s->blame;
4313 const struct got_error *err = NULL;
4314 struct got_blob_object *blob = NULL;
4315 struct got_repository *thread_repo = NULL;
4316 struct got_object_id *obj_id = NULL;
4317 int obj_type;
4319 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4320 s->path);
4321 if (err)
4322 return err;
4324 err = got_object_get_type(&obj_type, s->repo, obj_id);
4325 if (err)
4326 goto done;
4328 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4329 err = got_error(GOT_ERR_OBJ_TYPE);
4330 goto done;
4333 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4334 if (err)
4335 goto done;
4336 blame->f = got_opentemp();
4337 if (blame->f == NULL) {
4338 err = got_error_from_errno("got_opentemp");
4339 goto done;
4341 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4342 &blame->line_offsets, blame->f, blob);
4343 if (err)
4344 goto done;
4345 if (blame->nlines == 0) {
4346 s->blame_complete = 1;
4347 goto done;
4350 /* Don't include \n at EOF in the blame line count. */
4351 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4352 blame->nlines--;
4354 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4355 if (blame->lines == NULL) {
4356 err = got_error_from_errno("calloc");
4357 goto done;
4360 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4361 if (err)
4362 goto done;
4364 blame->cb_args.view = view;
4365 blame->cb_args.lines = blame->lines;
4366 blame->cb_args.nlines = blame->nlines;
4367 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4368 if (blame->cb_args.commit_id == NULL) {
4369 err = got_error_from_errno("got_object_id_dup");
4370 goto done;
4372 blame->cb_args.quit = &s->done;
4374 blame->thread_args.path = s->path;
4375 blame->thread_args.repo = thread_repo;
4376 blame->thread_args.cb_args = &blame->cb_args;
4377 blame->thread_args.complete = &s->blame_complete;
4378 blame->thread_args.cancel_cb = cancel_blame_view;
4379 blame->thread_args.cancel_arg = &s->done;
4380 s->blame_complete = 0;
4382 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4383 s->first_displayed_line = 1;
4384 s->last_displayed_line = view->nlines;
4385 s->selected_line = 1;
4388 done:
4389 if (blob)
4390 got_object_blob_close(blob);
4391 free(obj_id);
4392 if (err)
4393 stop_blame(blame);
4394 return err;
4397 static const struct got_error *
4398 open_blame_view(struct tog_view *view, char *path,
4399 struct got_object_id *commit_id, struct got_repository *repo)
4401 const struct got_error *err = NULL;
4402 struct tog_blame_view_state *s = &view->state.blame;
4404 STAILQ_INIT(&s->blamed_commits);
4406 s->path = strdup(path);
4407 if (s->path == NULL)
4408 return got_error_from_errno("strdup");
4410 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4411 if (err) {
4412 free(s->path);
4413 return err;
4416 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4417 s->first_displayed_line = 1;
4418 s->last_displayed_line = view->nlines;
4419 s->selected_line = 1;
4420 s->blame_complete = 0;
4421 s->repo = repo;
4422 s->commit_id = commit_id;
4423 memset(&s->blame, 0, sizeof(s->blame));
4425 STAILQ_INIT(&s->colors);
4426 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4427 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4428 get_color_value("TOG_COLOR_COMMIT"));
4429 if (err)
4430 return err;
4433 view->show = show_blame_view;
4434 view->input = input_blame_view;
4435 view->close = close_blame_view;
4436 view->search_start = search_start_blame_view;
4437 view->search_next = search_next_blame_view;
4439 return run_blame(view);
4442 static const struct got_error *
4443 close_blame_view(struct tog_view *view)
4445 const struct got_error *err = NULL;
4446 struct tog_blame_view_state *s = &view->state.blame;
4448 if (s->blame.thread)
4449 err = stop_blame(&s->blame);
4451 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4452 struct got_object_qid *blamed_commit;
4453 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4454 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4455 got_object_qid_free(blamed_commit);
4458 free(s->path);
4459 free_colors(&s->colors);
4461 return err;
4464 static const struct got_error *
4465 search_start_blame_view(struct tog_view *view)
4467 struct tog_blame_view_state *s = &view->state.blame;
4469 s->matched_line = 0;
4470 return NULL;
4473 static const struct got_error *
4474 search_next_blame_view(struct tog_view *view)
4476 struct tog_blame_view_state *s = &view->state.blame;
4477 int lineno;
4478 char *line = NULL;
4479 size_t linesize = 0;
4480 ssize_t linelen;
4482 if (!view->searching) {
4483 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4484 return NULL;
4487 if (s->matched_line) {
4488 if (view->searching == TOG_SEARCH_FORWARD)
4489 lineno = s->matched_line + 1;
4490 else
4491 lineno = s->matched_line - 1;
4492 } else {
4493 if (view->searching == TOG_SEARCH_FORWARD)
4494 lineno = 1;
4495 else
4496 lineno = s->blame.nlines;
4499 while (1) {
4500 off_t offset;
4502 if (lineno <= 0 || lineno > s->blame.nlines) {
4503 if (s->matched_line == 0) {
4504 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4505 break;
4508 if (view->searching == TOG_SEARCH_FORWARD)
4509 lineno = 1;
4510 else
4511 lineno = s->blame.nlines;
4514 offset = s->blame.line_offsets[lineno - 1];
4515 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4516 free(line);
4517 return got_error_from_errno("fseeko");
4519 linelen = getline(&line, &linesize, s->blame.f);
4520 if (linelen != -1 &&
4521 match_line(line, &view->regex, 1, &view->regmatch)) {
4522 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4523 s->matched_line = lineno;
4524 break;
4526 if (view->searching == TOG_SEARCH_FORWARD)
4527 lineno++;
4528 else
4529 lineno--;
4531 free(line);
4533 if (s->matched_line) {
4534 s->first_displayed_line = s->matched_line;
4535 s->selected_line = 1;
4538 return NULL;
4541 static const struct got_error *
4542 show_blame_view(struct tog_view *view)
4544 const struct got_error *err = NULL;
4545 struct tog_blame_view_state *s = &view->state.blame;
4546 int errcode;
4548 if (s->blame.thread == 0 && !s->blame_complete) {
4549 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4550 &s->blame.thread_args);
4551 if (errcode)
4552 return got_error_set_errno(errcode, "pthread_create");
4554 halfdelay(1); /* fast refresh while annotating */
4557 if (s->blame_complete)
4558 halfdelay(10); /* disable fast refresh */
4560 err = draw_blame(view);
4562 view_vborder(view);
4563 return err;
4566 static const struct got_error *
4567 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4569 const struct got_error *err = NULL, *thread_err = NULL;
4570 struct tog_view *diff_view;
4571 struct tog_blame_view_state *s = &view->state.blame;
4572 int begin_x = 0;
4574 switch (ch) {
4575 case 'q':
4576 s->done = 1;
4577 break;
4578 case 'g':
4579 case KEY_HOME:
4580 s->selected_line = 1;
4581 s->first_displayed_line = 1;
4582 break;
4583 case 'G':
4584 case KEY_END:
4585 if (s->blame.nlines < view->nlines - 2) {
4586 s->selected_line = s->blame.nlines;
4587 s->first_displayed_line = 1;
4588 } else {
4589 s->selected_line = view->nlines - 2;
4590 s->first_displayed_line = s->blame.nlines -
4591 (view->nlines - 3);
4593 break;
4594 case 'k':
4595 case KEY_UP:
4596 case CTRL('p'):
4597 if (s->selected_line > 1)
4598 s->selected_line--;
4599 else if (s->selected_line == 1 &&
4600 s->first_displayed_line > 1)
4601 s->first_displayed_line--;
4602 break;
4603 case KEY_PPAGE:
4604 case CTRL('b'):
4605 if (s->first_displayed_line == 1) {
4606 s->selected_line = 1;
4607 break;
4609 if (s->first_displayed_line > view->nlines - 2)
4610 s->first_displayed_line -=
4611 (view->nlines - 2);
4612 else
4613 s->first_displayed_line = 1;
4614 break;
4615 case 'j':
4616 case KEY_DOWN:
4617 case CTRL('n'):
4618 if (s->selected_line < view->nlines - 2 &&
4619 s->first_displayed_line +
4620 s->selected_line <= s->blame.nlines)
4621 s->selected_line++;
4622 else if (s->last_displayed_line <
4623 s->blame.nlines)
4624 s->first_displayed_line++;
4625 break;
4626 case 'b':
4627 case 'p': {
4628 struct got_object_id *id = NULL;
4629 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4630 s->first_displayed_line, s->selected_line);
4631 if (id == NULL)
4632 break;
4633 if (ch == 'p') {
4634 struct got_commit_object *commit;
4635 struct got_object_qid *pid;
4636 struct got_object_id *blob_id = NULL;
4637 int obj_type;
4638 err = got_object_open_as_commit(&commit,
4639 s->repo, id);
4640 if (err)
4641 break;
4642 pid = STAILQ_FIRST(
4643 got_object_commit_get_parent_ids(commit));
4644 if (pid == NULL) {
4645 got_object_commit_close(commit);
4646 break;
4648 /* Check if path history ends here. */
4649 err = got_object_id_by_path(&blob_id, s->repo,
4650 pid->id, s->path);
4651 if (err) {
4652 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4653 err = NULL;
4654 got_object_commit_close(commit);
4655 break;
4657 err = got_object_get_type(&obj_type, s->repo,
4658 blob_id);
4659 free(blob_id);
4660 /* Can't blame non-blob type objects. */
4661 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4662 got_object_commit_close(commit);
4663 break;
4665 err = got_object_qid_alloc(&s->blamed_commit,
4666 pid->id);
4667 got_object_commit_close(commit);
4668 } else {
4669 if (got_object_id_cmp(id,
4670 s->blamed_commit->id) == 0)
4671 break;
4672 err = got_object_qid_alloc(&s->blamed_commit,
4673 id);
4675 if (err)
4676 break;
4677 s->done = 1;
4678 thread_err = stop_blame(&s->blame);
4679 s->done = 0;
4680 if (thread_err)
4681 break;
4682 STAILQ_INSERT_HEAD(&s->blamed_commits,
4683 s->blamed_commit, entry);
4684 err = run_blame(view);
4685 if (err)
4686 break;
4687 break;
4689 case 'B': {
4690 struct got_object_qid *first;
4691 first = STAILQ_FIRST(&s->blamed_commits);
4692 if (!got_object_id_cmp(first->id, s->commit_id))
4693 break;
4694 s->done = 1;
4695 thread_err = stop_blame(&s->blame);
4696 s->done = 0;
4697 if (thread_err)
4698 break;
4699 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4700 got_object_qid_free(s->blamed_commit);
4701 s->blamed_commit =
4702 STAILQ_FIRST(&s->blamed_commits);
4703 err = run_blame(view);
4704 if (err)
4705 break;
4706 break;
4708 case KEY_ENTER:
4709 case '\r': {
4710 struct got_object_id *id = NULL;
4711 struct got_object_qid *pid;
4712 struct got_commit_object *commit = NULL;
4713 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4714 s->first_displayed_line, s->selected_line);
4715 if (id == NULL)
4716 break;
4717 err = got_object_open_as_commit(&commit, s->repo, id);
4718 if (err)
4719 break;
4720 pid = STAILQ_FIRST(
4721 got_object_commit_get_parent_ids(commit));
4722 if (view_is_parent_view(view))
4723 begin_x = view_split_begin_x(view->begin_x);
4724 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4725 if (diff_view == NULL) {
4726 got_object_commit_close(commit);
4727 err = got_error_from_errno("view_open");
4728 break;
4730 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4731 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4732 got_object_commit_close(commit);
4733 if (err) {
4734 view_close(diff_view);
4735 break;
4737 view->focussed = 0;
4738 diff_view->focussed = 1;
4739 if (view_is_parent_view(view)) {
4740 err = view_close_child(view);
4741 if (err)
4742 break;
4743 view_set_child(view, diff_view);
4744 view->focus_child = 1;
4745 } else
4746 *new_view = diff_view;
4747 if (err)
4748 break;
4749 break;
4751 case KEY_NPAGE:
4752 case CTRL('f'):
4753 case ' ':
4754 if (s->last_displayed_line >= s->blame.nlines &&
4755 s->selected_line >= MIN(s->blame.nlines,
4756 view->nlines - 2)) {
4757 break;
4759 if (s->last_displayed_line >= s->blame.nlines &&
4760 s->selected_line < view->nlines - 2) {
4761 s->selected_line = MIN(s->blame.nlines,
4762 view->nlines - 2);
4763 break;
4765 if (s->last_displayed_line + view->nlines - 2
4766 <= s->blame.nlines)
4767 s->first_displayed_line +=
4768 view->nlines - 2;
4769 else
4770 s->first_displayed_line =
4771 s->blame.nlines -
4772 (view->nlines - 3);
4773 break;
4774 case KEY_RESIZE:
4775 if (s->selected_line > view->nlines - 2) {
4776 s->selected_line = MIN(s->blame.nlines,
4777 view->nlines - 2);
4779 break;
4780 default:
4781 break;
4783 return thread_err ? thread_err : err;
4786 static const struct got_error *
4787 cmd_blame(int argc, char *argv[])
4789 const struct got_error *error;
4790 struct got_repository *repo = NULL;
4791 struct got_worktree *worktree = NULL;
4792 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4793 char *link_target = NULL;
4794 struct got_object_id *commit_id = NULL;
4795 char *commit_id_str = NULL;
4796 int ch;
4797 struct tog_view *view;
4799 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4800 switch (ch) {
4801 case 'c':
4802 commit_id_str = optarg;
4803 break;
4804 case 'r':
4805 repo_path = realpath(optarg, NULL);
4806 if (repo_path == NULL)
4807 return got_error_from_errno2("realpath",
4808 optarg);
4809 break;
4810 default:
4811 usage_blame();
4812 /* NOTREACHED */
4816 argc -= optind;
4817 argv += optind;
4819 if (argc != 1)
4820 usage_blame();
4822 if (repo_path == NULL) {
4823 cwd = getcwd(NULL, 0);
4824 if (cwd == NULL)
4825 return got_error_from_errno("getcwd");
4826 error = got_worktree_open(&worktree, cwd);
4827 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4828 goto done;
4829 if (worktree)
4830 repo_path =
4831 strdup(got_worktree_get_repo_path(worktree));
4832 else
4833 repo_path = strdup(cwd);
4834 if (repo_path == NULL) {
4835 error = got_error_from_errno("strdup");
4836 goto done;
4840 error = got_repo_open(&repo, repo_path, NULL);
4841 if (error != NULL)
4842 goto done;
4844 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4845 worktree);
4846 if (error)
4847 goto done;
4849 init_curses();
4851 error = apply_unveil(got_repo_get_path(repo), NULL);
4852 if (error)
4853 goto done;
4855 error = tog_load_refs(repo);
4856 if (error)
4857 goto done;
4859 if (commit_id_str == NULL) {
4860 struct got_reference *head_ref;
4861 error = got_ref_open(&head_ref, repo, worktree ?
4862 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4863 if (error != NULL)
4864 goto done;
4865 error = got_ref_resolve(&commit_id, repo, head_ref);
4866 got_ref_close(head_ref);
4867 } else {
4868 error = got_repo_match_object_id(&commit_id, NULL,
4869 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4871 if (error != NULL)
4872 goto done;
4874 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4875 if (view == NULL) {
4876 error = got_error_from_errno("view_open");
4877 goto done;
4880 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4881 commit_id, repo);
4882 if (error)
4883 goto done;
4885 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4886 commit_id, repo);
4887 if (error)
4888 goto done;
4889 if (worktree) {
4890 /* Release work tree lock. */
4891 got_worktree_close(worktree);
4892 worktree = NULL;
4894 error = view_loop(view);
4895 done:
4896 free(repo_path);
4897 free(in_repo_path);
4898 free(link_target);
4899 free(cwd);
4900 free(commit_id);
4901 if (worktree)
4902 got_worktree_close(worktree);
4903 if (repo) {
4904 const struct got_error *close_err = got_repo_close(repo);
4905 if (error == NULL)
4906 error = close_err;
4908 tog_free_refs();
4909 return error;
4912 static const struct got_error *
4913 draw_tree_entries(struct tog_view *view, const char *parent_path)
4915 struct tog_tree_view_state *s = &view->state.tree;
4916 const struct got_error *err = NULL;
4917 struct got_tree_entry *te;
4918 wchar_t *wline;
4919 struct tog_color *tc;
4920 int width, n, i, nentries;
4921 int limit = view->nlines;
4923 s->ndisplayed = 0;
4925 werase(view->window);
4927 if (limit == 0)
4928 return NULL;
4930 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4931 if (err)
4932 return err;
4933 if (view_needs_focus_indication(view))
4934 wstandout(view->window);
4935 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4936 if (tc)
4937 wattr_on(view->window,
4938 COLOR_PAIR(tc->colorpair), NULL);
4939 waddwstr(view->window, wline);
4940 if (tc)
4941 wattr_off(view->window,
4942 COLOR_PAIR(tc->colorpair), NULL);
4943 if (view_needs_focus_indication(view))
4944 wstandend(view->window);
4945 free(wline);
4946 wline = NULL;
4947 if (width < view->ncols - 1)
4948 waddch(view->window, '\n');
4949 if (--limit <= 0)
4950 return NULL;
4951 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4952 if (err)
4953 return err;
4954 waddwstr(view->window, wline);
4955 free(wline);
4956 wline = NULL;
4957 if (width < view->ncols - 1)
4958 waddch(view->window, '\n');
4959 if (--limit <= 0)
4960 return NULL;
4961 waddch(view->window, '\n');
4962 if (--limit <= 0)
4963 return NULL;
4965 if (s->first_displayed_entry == NULL) {
4966 te = got_object_tree_get_first_entry(s->tree);
4967 if (s->selected == 0) {
4968 if (view->focussed)
4969 wstandout(view->window);
4970 s->selected_entry = NULL;
4972 waddstr(view->window, " ..\n"); /* parent directory */
4973 if (s->selected == 0 && view->focussed)
4974 wstandend(view->window);
4975 s->ndisplayed++;
4976 if (--limit <= 0)
4977 return NULL;
4978 n = 1;
4979 } else {
4980 n = 0;
4981 te = s->first_displayed_entry;
4984 nentries = got_object_tree_get_nentries(s->tree);
4985 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4986 char *line = NULL, *id_str = NULL, *link_target = NULL;
4987 const char *modestr = "";
4988 mode_t mode;
4990 te = got_object_tree_get_entry(s->tree, i);
4991 mode = got_tree_entry_get_mode(te);
4993 if (s->show_ids) {
4994 err = got_object_id_str(&id_str,
4995 got_tree_entry_get_id(te));
4996 if (err)
4997 return got_error_from_errno(
4998 "got_object_id_str");
5000 if (got_object_tree_entry_is_submodule(te))
5001 modestr = "$";
5002 else if (S_ISLNK(mode)) {
5003 int i;
5005 err = got_tree_entry_get_symlink_target(&link_target,
5006 te, s->repo);
5007 if (err) {
5008 free(id_str);
5009 return err;
5011 for (i = 0; i < strlen(link_target); i++) {
5012 if (!isprint((unsigned char)link_target[i]))
5013 link_target[i] = '?';
5015 modestr = "@";
5017 else if (S_ISDIR(mode))
5018 modestr = "/";
5019 else if (mode & S_IXUSR)
5020 modestr = "*";
5021 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5022 got_tree_entry_get_name(te), modestr,
5023 link_target ? " -> ": "",
5024 link_target ? link_target : "") == -1) {
5025 free(id_str);
5026 free(link_target);
5027 return got_error_from_errno("asprintf");
5029 free(id_str);
5030 free(link_target);
5031 err = format_line(&wline, &width, line, view->ncols, 0);
5032 if (err) {
5033 free(line);
5034 break;
5036 if (n == s->selected) {
5037 if (view->focussed)
5038 wstandout(view->window);
5039 s->selected_entry = te;
5041 tc = match_color(&s->colors, line);
5042 if (tc)
5043 wattr_on(view->window,
5044 COLOR_PAIR(tc->colorpair), NULL);
5045 waddwstr(view->window, wline);
5046 if (tc)
5047 wattr_off(view->window,
5048 COLOR_PAIR(tc->colorpair), NULL);
5049 if (width < view->ncols - 1)
5050 waddch(view->window, '\n');
5051 if (n == s->selected && view->focussed)
5052 wstandend(view->window);
5053 free(line);
5054 free(wline);
5055 wline = NULL;
5056 n++;
5057 s->ndisplayed++;
5058 s->last_displayed_entry = te;
5059 if (--limit <= 0)
5060 break;
5063 return err;
5066 static void
5067 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5069 struct got_tree_entry *te;
5070 int isroot = s->tree == s->root;
5071 int i = 0;
5073 if (s->first_displayed_entry == NULL)
5074 return;
5076 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5077 while (i++ < maxscroll) {
5078 if (te == NULL) {
5079 if (!isroot)
5080 s->first_displayed_entry = NULL;
5081 break;
5083 s->first_displayed_entry = te;
5084 te = got_tree_entry_get_prev(s->tree, te);
5088 static void
5089 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5091 struct got_tree_entry *next, *last;
5092 int n = 0;
5094 if (s->first_displayed_entry)
5095 next = got_tree_entry_get_next(s->tree,
5096 s->first_displayed_entry);
5097 else
5098 next = got_object_tree_get_first_entry(s->tree);
5100 last = s->last_displayed_entry;
5101 while (next && last && n++ < maxscroll) {
5102 last = got_tree_entry_get_next(s->tree, last);
5103 if (last) {
5104 s->first_displayed_entry = next;
5105 next = got_tree_entry_get_next(s->tree, next);
5110 static const struct got_error *
5111 tree_entry_path(char **path, struct tog_parent_trees *parents,
5112 struct got_tree_entry *te)
5114 const struct got_error *err = NULL;
5115 struct tog_parent_tree *pt;
5116 size_t len = 2; /* for leading slash and NUL */
5118 TAILQ_FOREACH(pt, parents, entry)
5119 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5120 + 1 /* slash */;
5121 if (te)
5122 len += strlen(got_tree_entry_get_name(te));
5124 *path = calloc(1, len);
5125 if (path == NULL)
5126 return got_error_from_errno("calloc");
5128 (*path)[0] = '/';
5129 pt = TAILQ_LAST(parents, tog_parent_trees);
5130 while (pt) {
5131 const char *name = got_tree_entry_get_name(pt->selected_entry);
5132 if (strlcat(*path, name, len) >= len) {
5133 err = got_error(GOT_ERR_NO_SPACE);
5134 goto done;
5136 if (strlcat(*path, "/", len) >= len) {
5137 err = got_error(GOT_ERR_NO_SPACE);
5138 goto done;
5140 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5142 if (te) {
5143 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5144 err = got_error(GOT_ERR_NO_SPACE);
5145 goto done;
5148 done:
5149 if (err) {
5150 free(*path);
5151 *path = NULL;
5153 return err;
5156 static const struct got_error *
5157 blame_tree_entry(struct tog_view **new_view, int begin_x,
5158 struct got_tree_entry *te, struct tog_parent_trees *parents,
5159 struct got_object_id *commit_id, struct got_repository *repo)
5161 const struct got_error *err = NULL;
5162 char *path;
5163 struct tog_view *blame_view;
5165 *new_view = NULL;
5167 err = tree_entry_path(&path, parents, te);
5168 if (err)
5169 return err;
5171 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5172 if (blame_view == NULL) {
5173 err = got_error_from_errno("view_open");
5174 goto done;
5177 err = open_blame_view(blame_view, path, commit_id, repo);
5178 if (err) {
5179 if (err->code == GOT_ERR_CANCELLED)
5180 err = NULL;
5181 view_close(blame_view);
5182 } else
5183 *new_view = blame_view;
5184 done:
5185 free(path);
5186 return err;
5189 static const struct got_error *
5190 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5191 struct tog_tree_view_state *s)
5193 struct tog_view *log_view;
5194 const struct got_error *err = NULL;
5195 char *path;
5197 *new_view = NULL;
5199 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5200 if (log_view == NULL)
5201 return got_error_from_errno("view_open");
5203 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5204 if (err)
5205 return err;
5207 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5208 path, 0);
5209 if (err)
5210 view_close(log_view);
5211 else
5212 *new_view = log_view;
5213 free(path);
5214 return err;
5217 static const struct got_error *
5218 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5219 const char *head_ref_name, struct got_repository *repo)
5221 const struct got_error *err = NULL;
5222 char *commit_id_str = NULL;
5223 struct tog_tree_view_state *s = &view->state.tree;
5224 struct got_commit_object *commit = NULL;
5226 TAILQ_INIT(&s->parents);
5227 STAILQ_INIT(&s->colors);
5229 s->commit_id = got_object_id_dup(commit_id);
5230 if (s->commit_id == NULL)
5231 return got_error_from_errno("got_object_id_dup");
5233 err = got_object_open_as_commit(&commit, repo, commit_id);
5234 if (err)
5235 goto done;
5238 * The root is opened here and will be closed when the view is closed.
5239 * Any visited subtrees and their path-wise parents are opened and
5240 * closed on demand.
5242 err = got_object_open_as_tree(&s->root, repo,
5243 got_object_commit_get_tree_id(commit));
5244 if (err)
5245 goto done;
5246 s->tree = s->root;
5248 err = got_object_id_str(&commit_id_str, commit_id);
5249 if (err != NULL)
5250 goto done;
5252 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5253 err = got_error_from_errno("asprintf");
5254 goto done;
5257 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5258 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5259 if (head_ref_name) {
5260 s->head_ref_name = strdup(head_ref_name);
5261 if (s->head_ref_name == NULL) {
5262 err = got_error_from_errno("strdup");
5263 goto done;
5266 s->repo = repo;
5268 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5269 err = add_color(&s->colors, "\\$$",
5270 TOG_COLOR_TREE_SUBMODULE,
5271 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5272 if (err)
5273 goto done;
5274 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5275 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5276 if (err)
5277 goto done;
5278 err = add_color(&s->colors, "/$",
5279 TOG_COLOR_TREE_DIRECTORY,
5280 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5281 if (err)
5282 goto done;
5284 err = add_color(&s->colors, "\\*$",
5285 TOG_COLOR_TREE_EXECUTABLE,
5286 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5287 if (err)
5288 goto done;
5290 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5291 get_color_value("TOG_COLOR_COMMIT"));
5292 if (err)
5293 goto done;
5296 view->show = show_tree_view;
5297 view->input = input_tree_view;
5298 view->close = close_tree_view;
5299 view->search_start = search_start_tree_view;
5300 view->search_next = search_next_tree_view;
5301 done:
5302 free(commit_id_str);
5303 if (commit)
5304 got_object_commit_close(commit);
5305 if (err)
5306 close_tree_view(view);
5307 return err;
5310 static const struct got_error *
5311 close_tree_view(struct tog_view *view)
5313 struct tog_tree_view_state *s = &view->state.tree;
5315 free_colors(&s->colors);
5316 free(s->tree_label);
5317 s->tree_label = NULL;
5318 free(s->commit_id);
5319 s->commit_id = NULL;
5320 free(s->head_ref_name);
5321 s->head_ref_name = NULL;
5322 while (!TAILQ_EMPTY(&s->parents)) {
5323 struct tog_parent_tree *parent;
5324 parent = TAILQ_FIRST(&s->parents);
5325 TAILQ_REMOVE(&s->parents, parent, entry);
5326 if (parent->tree != s->root)
5327 got_object_tree_close(parent->tree);
5328 free(parent);
5331 if (s->tree != NULL && s->tree != s->root)
5332 got_object_tree_close(s->tree);
5333 if (s->root)
5334 got_object_tree_close(s->root);
5335 return NULL;
5338 static const struct got_error *
5339 search_start_tree_view(struct tog_view *view)
5341 struct tog_tree_view_state *s = &view->state.tree;
5343 s->matched_entry = NULL;
5344 return NULL;
5347 static int
5348 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5350 regmatch_t regmatch;
5352 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5353 0) == 0;
5356 static const struct got_error *
5357 search_next_tree_view(struct tog_view *view)
5359 struct tog_tree_view_state *s = &view->state.tree;
5360 struct got_tree_entry *te = NULL;
5362 if (!view->searching) {
5363 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5364 return NULL;
5367 if (s->matched_entry) {
5368 if (view->searching == TOG_SEARCH_FORWARD) {
5369 if (s->selected_entry)
5370 te = got_tree_entry_get_next(s->tree,
5371 s->selected_entry);
5372 else
5373 te = got_object_tree_get_first_entry(s->tree);
5374 } else {
5375 if (s->selected_entry == NULL)
5376 te = got_object_tree_get_last_entry(s->tree);
5377 else
5378 te = got_tree_entry_get_prev(s->tree,
5379 s->selected_entry);
5381 } else {
5382 if (view->searching == TOG_SEARCH_FORWARD)
5383 te = got_object_tree_get_first_entry(s->tree);
5384 else
5385 te = got_object_tree_get_last_entry(s->tree);
5388 while (1) {
5389 if (te == NULL) {
5390 if (s->matched_entry == NULL) {
5391 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5392 return NULL;
5394 if (view->searching == TOG_SEARCH_FORWARD)
5395 te = got_object_tree_get_first_entry(s->tree);
5396 else
5397 te = got_object_tree_get_last_entry(s->tree);
5400 if (match_tree_entry(te, &view->regex)) {
5401 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5402 s->matched_entry = te;
5403 break;
5406 if (view->searching == TOG_SEARCH_FORWARD)
5407 te = got_tree_entry_get_next(s->tree, te);
5408 else
5409 te = got_tree_entry_get_prev(s->tree, te);
5412 if (s->matched_entry) {
5413 s->first_displayed_entry = s->matched_entry;
5414 s->selected = 0;
5417 return NULL;
5420 static const struct got_error *
5421 show_tree_view(struct tog_view *view)
5423 const struct got_error *err = NULL;
5424 struct tog_tree_view_state *s = &view->state.tree;
5425 char *parent_path;
5427 err = tree_entry_path(&parent_path, &s->parents, NULL);
5428 if (err)
5429 return err;
5431 err = draw_tree_entries(view, parent_path);
5432 free(parent_path);
5434 view_vborder(view);
5435 return err;
5438 static const struct got_error *
5439 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5441 const struct got_error *err = NULL;
5442 struct tog_tree_view_state *s = &view->state.tree;
5443 struct tog_view *log_view, *ref_view;
5444 struct got_tree_entry *te;
5445 int begin_x = 0, n;
5447 switch (ch) {
5448 case 'i':
5449 s->show_ids = !s->show_ids;
5450 break;
5451 case 'l':
5452 if (!s->selected_entry)
5453 break;
5454 if (view_is_parent_view(view))
5455 begin_x = view_split_begin_x(view->begin_x);
5456 err = log_selected_tree_entry(&log_view, begin_x, s);
5457 view->focussed = 0;
5458 log_view->focussed = 1;
5459 if (view_is_parent_view(view)) {
5460 err = view_close_child(view);
5461 if (err)
5462 return err;
5463 view_set_child(view, log_view);
5464 view->focus_child = 1;
5465 } else
5466 *new_view = log_view;
5467 break;
5468 case 'r':
5469 if (view_is_parent_view(view))
5470 begin_x = view_split_begin_x(view->begin_x);
5471 ref_view = view_open(view->nlines, view->ncols,
5472 view->begin_y, begin_x, TOG_VIEW_REF);
5473 if (ref_view == NULL)
5474 return got_error_from_errno("view_open");
5475 err = open_ref_view(ref_view, s->repo);
5476 if (err) {
5477 view_close(ref_view);
5478 return err;
5480 view->focussed = 0;
5481 ref_view->focussed = 1;
5482 if (view_is_parent_view(view)) {
5483 err = view_close_child(view);
5484 if (err)
5485 return err;
5486 view_set_child(view, ref_view);
5487 view->focus_child = 1;
5488 } else
5489 *new_view = ref_view;
5490 break;
5491 case 'g':
5492 case KEY_HOME:
5493 s->selected = 0;
5494 if (s->tree == s->root)
5495 s->first_displayed_entry =
5496 got_object_tree_get_first_entry(s->tree);
5497 else
5498 s->first_displayed_entry = NULL;
5499 break;
5500 case 'G':
5501 case KEY_END:
5502 s->selected = 0;
5503 te = got_object_tree_get_last_entry(s->tree);
5504 for (n = 0; n < view->nlines - 3; n++) {
5505 if (te == NULL) {
5506 if(s->tree != s->root) {
5507 s->first_displayed_entry = NULL;
5508 n++;
5510 break;
5512 s->first_displayed_entry = te;
5513 te = got_tree_entry_get_prev(s->tree, te);
5515 if (n > 0)
5516 s->selected = n - 1;
5517 break;
5518 case 'k':
5519 case KEY_UP:
5520 case CTRL('p'):
5521 if (s->selected > 0) {
5522 s->selected--;
5523 break;
5525 tree_scroll_up(s, 1);
5526 break;
5527 case KEY_PPAGE:
5528 case CTRL('b'):
5529 if (s->tree == s->root) {
5530 if (got_object_tree_get_first_entry(s->tree) ==
5531 s->first_displayed_entry)
5532 s->selected = 0;
5533 } else {
5534 if (s->first_displayed_entry == NULL)
5535 s->selected = 0;
5537 tree_scroll_up(s, MAX(0, view->nlines - 3));
5538 break;
5539 case 'j':
5540 case KEY_DOWN:
5541 case CTRL('n'):
5542 if (s->selected < s->ndisplayed - 1) {
5543 s->selected++;
5544 break;
5546 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5547 == NULL)
5548 /* can't scroll any further */
5549 break;
5550 tree_scroll_down(s, 1);
5551 break;
5552 case KEY_NPAGE:
5553 case CTRL('f'):
5554 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5555 == NULL) {
5556 /* can't scroll any further; move cursor down */
5557 if (s->selected < s->ndisplayed - 1)
5558 s->selected = s->ndisplayed - 1;
5559 break;
5561 tree_scroll_down(s, view->nlines - 3);
5562 break;
5563 case KEY_ENTER:
5564 case '\r':
5565 case KEY_BACKSPACE:
5566 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5567 struct tog_parent_tree *parent;
5568 /* user selected '..' */
5569 if (s->tree == s->root)
5570 break;
5571 parent = TAILQ_FIRST(&s->parents);
5572 TAILQ_REMOVE(&s->parents, parent,
5573 entry);
5574 got_object_tree_close(s->tree);
5575 s->tree = parent->tree;
5576 s->first_displayed_entry =
5577 parent->first_displayed_entry;
5578 s->selected_entry =
5579 parent->selected_entry;
5580 s->selected = parent->selected;
5581 free(parent);
5582 } else if (S_ISDIR(got_tree_entry_get_mode(
5583 s->selected_entry))) {
5584 struct got_tree_object *subtree;
5585 err = got_object_open_as_tree(&subtree, s->repo,
5586 got_tree_entry_get_id(s->selected_entry));
5587 if (err)
5588 break;
5589 err = tree_view_visit_subtree(s, subtree);
5590 if (err) {
5591 got_object_tree_close(subtree);
5592 break;
5594 } else if (S_ISREG(got_tree_entry_get_mode(
5595 s->selected_entry))) {
5596 struct tog_view *blame_view;
5597 int begin_x = view_is_parent_view(view) ?
5598 view_split_begin_x(view->begin_x) : 0;
5600 err = blame_tree_entry(&blame_view, begin_x,
5601 s->selected_entry, &s->parents,
5602 s->commit_id, s->repo);
5603 if (err)
5604 break;
5605 view->focussed = 0;
5606 blame_view->focussed = 1;
5607 if (view_is_parent_view(view)) {
5608 err = view_close_child(view);
5609 if (err)
5610 return err;
5611 view_set_child(view, blame_view);
5612 view->focus_child = 1;
5613 } else
5614 *new_view = blame_view;
5616 break;
5617 case KEY_RESIZE:
5618 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5619 s->selected = view->nlines - 4;
5620 break;
5621 default:
5622 break;
5625 return err;
5628 __dead static void
5629 usage_tree(void)
5631 endwin();
5632 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5633 getprogname());
5634 exit(1);
5637 static const struct got_error *
5638 cmd_tree(int argc, char *argv[])
5640 const struct got_error *error;
5641 struct got_repository *repo = NULL;
5642 struct got_worktree *worktree = NULL;
5643 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5644 struct got_object_id *commit_id = NULL;
5645 const char *commit_id_arg = NULL;
5646 char *label = NULL;
5647 struct got_reference *ref = NULL;
5648 const char *head_ref_name = NULL;
5649 int ch;
5650 struct tog_view *view;
5652 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5653 switch (ch) {
5654 case 'c':
5655 commit_id_arg = optarg;
5656 break;
5657 case 'r':
5658 repo_path = realpath(optarg, NULL);
5659 if (repo_path == NULL)
5660 return got_error_from_errno2("realpath",
5661 optarg);
5662 break;
5663 default:
5664 usage_tree();
5665 /* NOTREACHED */
5669 argc -= optind;
5670 argv += optind;
5672 if (argc > 1)
5673 usage_tree();
5675 if (repo_path == NULL) {
5676 cwd = getcwd(NULL, 0);
5677 if (cwd == NULL)
5678 return got_error_from_errno("getcwd");
5679 error = got_worktree_open(&worktree, cwd);
5680 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5681 goto done;
5682 if (worktree)
5683 repo_path =
5684 strdup(got_worktree_get_repo_path(worktree));
5685 else
5686 repo_path = strdup(cwd);
5687 if (repo_path == NULL) {
5688 error = got_error_from_errno("strdup");
5689 goto done;
5693 error = got_repo_open(&repo, repo_path, NULL);
5694 if (error != NULL)
5695 goto done;
5697 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5698 repo, worktree);
5699 if (error)
5700 goto done;
5702 init_curses();
5704 error = apply_unveil(got_repo_get_path(repo), NULL);
5705 if (error)
5706 goto done;
5708 error = tog_load_refs(repo);
5709 if (error)
5710 goto done;
5712 if (commit_id_arg == NULL) {
5713 error = got_repo_match_object_id(&commit_id, &label,
5714 worktree ? got_worktree_get_head_ref_name(worktree) :
5715 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5716 if (error)
5717 goto done;
5718 head_ref_name = label;
5719 } else {
5720 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5721 if (error == NULL)
5722 head_ref_name = got_ref_get_name(ref);
5723 else if (error->code != GOT_ERR_NOT_REF)
5724 goto done;
5725 error = got_repo_match_object_id(&commit_id, NULL,
5726 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5727 if (error)
5728 goto done;
5731 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5732 if (view == NULL) {
5733 error = got_error_from_errno("view_open");
5734 goto done;
5736 error = open_tree_view(view, commit_id, head_ref_name, repo);
5737 if (error)
5738 goto done;
5739 if (!got_path_is_root_dir(in_repo_path)) {
5740 error = tree_view_walk_path(&view->state.tree, commit_id,
5741 in_repo_path);
5742 if (error)
5743 goto done;
5746 if (worktree) {
5747 /* Release work tree lock. */
5748 got_worktree_close(worktree);
5749 worktree = NULL;
5751 error = view_loop(view);
5752 done:
5753 free(repo_path);
5754 free(cwd);
5755 free(commit_id);
5756 free(label);
5757 if (ref)
5758 got_ref_close(ref);
5759 if (repo) {
5760 const struct got_error *close_err = got_repo_close(repo);
5761 if (error == NULL)
5762 error = close_err;
5764 tog_free_refs();
5765 return error;
5768 static const struct got_error *
5769 ref_view_load_refs(struct tog_ref_view_state *s)
5771 struct got_reflist_entry *sre;
5772 struct tog_reflist_entry *re;
5774 s->nrefs = 0;
5775 TAILQ_FOREACH(sre, &tog_refs, entry) {
5776 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5777 continue;
5779 re = malloc(sizeof(*re));
5780 if (re == NULL)
5781 return got_error_from_errno("malloc");
5783 re->ref = got_ref_dup(sre->ref);
5784 if (re->ref == NULL)
5785 return got_error_from_errno("got_ref_dup");
5786 re->idx = s->nrefs++;
5787 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5790 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5791 return NULL;
5794 void
5795 ref_view_free_refs(struct tog_ref_view_state *s)
5797 struct tog_reflist_entry *re;
5799 while (!TAILQ_EMPTY(&s->refs)) {
5800 re = TAILQ_FIRST(&s->refs);
5801 TAILQ_REMOVE(&s->refs, re, entry);
5802 got_ref_close(re->ref);
5803 free(re);
5807 static const struct got_error *
5808 open_ref_view(struct tog_view *view, struct got_repository *repo)
5810 const struct got_error *err = NULL;
5811 struct tog_ref_view_state *s = &view->state.ref;
5813 s->selected_entry = 0;
5814 s->repo = repo;
5816 TAILQ_INIT(&s->refs);
5817 STAILQ_INIT(&s->colors);
5819 err = ref_view_load_refs(s);
5820 if (err)
5821 return err;
5823 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5824 err = add_color(&s->colors, "^refs/heads/",
5825 TOG_COLOR_REFS_HEADS,
5826 get_color_value("TOG_COLOR_REFS_HEADS"));
5827 if (err)
5828 goto done;
5830 err = add_color(&s->colors, "^refs/tags/",
5831 TOG_COLOR_REFS_TAGS,
5832 get_color_value("TOG_COLOR_REFS_TAGS"));
5833 if (err)
5834 goto done;
5836 err = add_color(&s->colors, "^refs/remotes/",
5837 TOG_COLOR_REFS_REMOTES,
5838 get_color_value("TOG_COLOR_REFS_REMOTES"));
5839 if (err)
5840 goto done;
5843 view->show = show_ref_view;
5844 view->input = input_ref_view;
5845 view->close = close_ref_view;
5846 view->search_start = search_start_ref_view;
5847 view->search_next = search_next_ref_view;
5848 done:
5849 if (err)
5850 free_colors(&s->colors);
5851 return err;
5854 static const struct got_error *
5855 close_ref_view(struct tog_view *view)
5857 struct tog_ref_view_state *s = &view->state.ref;
5859 ref_view_free_refs(s);
5860 free_colors(&s->colors);
5862 return NULL;
5865 static const struct got_error *
5866 resolve_reflist_entry(struct got_object_id **commit_id,
5867 struct tog_reflist_entry *re, struct got_repository *repo)
5869 const struct got_error *err = NULL;
5870 struct got_object_id *obj_id;
5871 struct got_tag_object *tag = NULL;
5872 int obj_type;
5874 *commit_id = NULL;
5876 err = got_ref_resolve(&obj_id, repo, re->ref);
5877 if (err)
5878 return err;
5880 err = got_object_get_type(&obj_type, repo, obj_id);
5881 if (err)
5882 goto done;
5884 switch (obj_type) {
5885 case GOT_OBJ_TYPE_COMMIT:
5886 *commit_id = obj_id;
5887 break;
5888 case GOT_OBJ_TYPE_TAG:
5889 err = got_object_open_as_tag(&tag, repo, obj_id);
5890 if (err)
5891 goto done;
5892 free(obj_id);
5893 err = got_object_get_type(&obj_type, repo,
5894 got_object_tag_get_object_id(tag));
5895 if (err)
5896 goto done;
5897 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5898 err = got_error(GOT_ERR_OBJ_TYPE);
5899 goto done;
5901 *commit_id = got_object_id_dup(
5902 got_object_tag_get_object_id(tag));
5903 if (*commit_id == NULL) {
5904 err = got_error_from_errno("got_object_id_dup");
5905 goto done;
5907 break;
5908 default:
5909 err = got_error(GOT_ERR_OBJ_TYPE);
5910 break;
5913 done:
5914 if (tag)
5915 got_object_tag_close(tag);
5916 if (err) {
5917 free(*commit_id);
5918 *commit_id = NULL;
5920 return err;
5923 static const struct got_error *
5924 log_ref_entry(struct tog_view **new_view, int begin_x,
5925 struct tog_reflist_entry *re, struct got_repository *repo)
5927 struct tog_view *log_view;
5928 const struct got_error *err = NULL;
5929 struct got_object_id *commit_id = NULL;
5931 *new_view = NULL;
5933 err = resolve_reflist_entry(&commit_id, re, repo);
5934 if (err) {
5935 if (err->code != GOT_ERR_OBJ_TYPE)
5936 return err;
5937 else
5938 return NULL;
5941 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5942 if (log_view == NULL) {
5943 err = got_error_from_errno("view_open");
5944 goto done;
5947 err = open_log_view(log_view, commit_id, repo,
5948 got_ref_get_name(re->ref), "", 0);
5949 done:
5950 if (err)
5951 view_close(log_view);
5952 else
5953 *new_view = log_view;
5954 free(commit_id);
5955 return err;
5958 static void
5959 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5961 struct tog_reflist_entry *re;
5962 int i = 0;
5964 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5965 return;
5967 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5968 while (i++ < maxscroll) {
5969 if (re == NULL)
5970 break;
5971 s->first_displayed_entry = re;
5972 re = TAILQ_PREV(re, tog_reflist_head, entry);
5976 static void
5977 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5979 struct tog_reflist_entry *next, *last;
5980 int n = 0;
5982 if (s->first_displayed_entry)
5983 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5984 else
5985 next = TAILQ_FIRST(&s->refs);
5987 last = s->last_displayed_entry;
5988 while (next && last && n++ < maxscroll) {
5989 last = TAILQ_NEXT(last, entry);
5990 if (last) {
5991 s->first_displayed_entry = next;
5992 next = TAILQ_NEXT(next, entry);
5997 static const struct got_error *
5998 search_start_ref_view(struct tog_view *view)
6000 struct tog_ref_view_state *s = &view->state.ref;
6002 s->matched_entry = NULL;
6003 return NULL;
6006 static int
6007 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6009 regmatch_t regmatch;
6011 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6012 0) == 0;
6015 static const struct got_error *
6016 search_next_ref_view(struct tog_view *view)
6018 struct tog_ref_view_state *s = &view->state.ref;
6019 struct tog_reflist_entry *re = NULL;
6021 if (!view->searching) {
6022 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6023 return NULL;
6026 if (s->matched_entry) {
6027 if (view->searching == TOG_SEARCH_FORWARD) {
6028 if (s->selected_entry)
6029 re = TAILQ_NEXT(s->selected_entry, entry);
6030 else
6031 re = TAILQ_PREV(s->selected_entry,
6032 tog_reflist_head, entry);
6033 } else {
6034 if (s->selected_entry == NULL)
6035 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6036 else
6037 re = TAILQ_PREV(s->selected_entry,
6038 tog_reflist_head, entry);
6040 } else {
6041 if (view->searching == TOG_SEARCH_FORWARD)
6042 re = TAILQ_FIRST(&s->refs);
6043 else
6044 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6047 while (1) {
6048 if (re == NULL) {
6049 if (s->matched_entry == NULL) {
6050 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6051 return NULL;
6053 if (view->searching == TOG_SEARCH_FORWARD)
6054 re = TAILQ_FIRST(&s->refs);
6055 else
6056 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6059 if (match_reflist_entry(re, &view->regex)) {
6060 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6061 s->matched_entry = re;
6062 break;
6065 if (view->searching == TOG_SEARCH_FORWARD)
6066 re = TAILQ_NEXT(re, entry);
6067 else
6068 re = TAILQ_PREV(re, tog_reflist_head, entry);
6071 if (s->matched_entry) {
6072 s->first_displayed_entry = s->matched_entry;
6073 s->selected = 0;
6076 return NULL;
6079 static const struct got_error *
6080 show_ref_view(struct tog_view *view)
6082 const struct got_error *err = NULL;
6083 struct tog_ref_view_state *s = &view->state.ref;
6084 struct tog_reflist_entry *re;
6085 char *line = NULL;
6086 wchar_t *wline;
6087 struct tog_color *tc;
6088 int width, n;
6089 int limit = view->nlines;
6091 werase(view->window);
6093 s->ndisplayed = 0;
6095 if (limit == 0)
6096 return NULL;
6098 re = s->first_displayed_entry;
6100 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6101 s->nrefs) == -1)
6102 return got_error_from_errno("asprintf");
6104 err = format_line(&wline, &width, line, view->ncols, 0);
6105 if (err) {
6106 free(line);
6107 return err;
6109 if (view_needs_focus_indication(view))
6110 wstandout(view->window);
6111 waddwstr(view->window, wline);
6112 if (view_needs_focus_indication(view))
6113 wstandend(view->window);
6114 free(wline);
6115 wline = NULL;
6116 free(line);
6117 line = NULL;
6118 if (width < view->ncols - 1)
6119 waddch(view->window, '\n');
6120 if (--limit <= 0)
6121 return NULL;
6123 n = 0;
6124 while (re && limit > 0) {
6125 char *line = NULL;
6127 if (got_ref_is_symbolic(re->ref)) {
6128 if (asprintf(&line, "%s -> %s",
6129 got_ref_get_name(re->ref),
6130 got_ref_get_symref_target(re->ref)) == -1)
6131 return got_error_from_errno("asprintf");
6132 } else if (s->show_ids) {
6133 struct got_object_id *id;
6134 char *id_str;
6135 err = got_ref_resolve(&id, s->repo, re->ref);
6136 if (err)
6137 return err;
6138 err = got_object_id_str(&id_str, id);
6139 if (err) {
6140 free(id);
6141 return err;
6143 if (asprintf(&line, "%s: %s",
6144 got_ref_get_name(re->ref), id_str) == -1) {
6145 err = got_error_from_errno("asprintf");
6146 free(id);
6147 free(id_str);
6148 return err;
6150 free(id);
6151 free(id_str);
6152 } else {
6153 line = strdup(got_ref_get_name(re->ref));
6154 if (line == NULL)
6155 return got_error_from_errno("strdup");
6158 err = format_line(&wline, &width, line, view->ncols, 0);
6159 if (err) {
6160 free(line);
6161 return err;
6163 if (n == s->selected) {
6164 if (view->focussed)
6165 wstandout(view->window);
6166 s->selected_entry = re;
6168 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6169 if (tc)
6170 wattr_on(view->window,
6171 COLOR_PAIR(tc->colorpair), NULL);
6172 waddwstr(view->window, wline);
6173 if (tc)
6174 wattr_off(view->window,
6175 COLOR_PAIR(tc->colorpair), NULL);
6176 if (width < view->ncols - 1)
6177 waddch(view->window, '\n');
6178 if (n == s->selected && view->focussed)
6179 wstandend(view->window);
6180 free(line);
6181 free(wline);
6182 wline = NULL;
6183 n++;
6184 s->ndisplayed++;
6185 s->last_displayed_entry = re;
6187 limit--;
6188 re = TAILQ_NEXT(re, entry);
6191 view_vborder(view);
6192 return err;
6195 static const struct got_error *
6196 browse_ref_tree(struct tog_view **new_view, int begin_x,
6197 struct tog_reflist_entry *re, struct got_repository *repo)
6199 const struct got_error *err = NULL;
6200 struct got_object_id *commit_id = NULL;
6201 struct tog_view *tree_view;
6203 *new_view = NULL;
6205 err = resolve_reflist_entry(&commit_id, re, repo);
6206 if (err) {
6207 if (err->code != GOT_ERR_OBJ_TYPE)
6208 return err;
6209 else
6210 return NULL;
6214 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6215 if (tree_view == NULL) {
6216 err = got_error_from_errno("view_open");
6217 goto done;
6220 err = open_tree_view(tree_view, commit_id,
6221 got_ref_get_name(re->ref), repo);
6222 if (err)
6223 goto done;
6225 *new_view = tree_view;
6226 done:
6227 free(commit_id);
6228 return err;
6230 static const struct got_error *
6231 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6233 const struct got_error *err = NULL;
6234 struct tog_ref_view_state *s = &view->state.ref;
6235 struct tog_view *log_view, *tree_view;
6236 struct tog_reflist_entry *re;
6237 int begin_x = 0, n;
6239 switch (ch) {
6240 case 'i':
6241 s->show_ids = !s->show_ids;
6242 break;
6243 case KEY_ENTER:
6244 case '\r':
6245 if (!s->selected_entry)
6246 break;
6247 if (view_is_parent_view(view))
6248 begin_x = view_split_begin_x(view->begin_x);
6249 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6250 s->repo);
6251 view->focussed = 0;
6252 log_view->focussed = 1;
6253 if (view_is_parent_view(view)) {
6254 err = view_close_child(view);
6255 if (err)
6256 return err;
6257 view_set_child(view, log_view);
6258 view->focus_child = 1;
6259 } else
6260 *new_view = log_view;
6261 break;
6262 case 't':
6263 if (!s->selected_entry)
6264 break;
6265 if (view_is_parent_view(view))
6266 begin_x = view_split_begin_x(view->begin_x);
6267 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6268 s->repo);
6269 if (err || tree_view == NULL)
6270 break;
6271 view->focussed = 0;
6272 tree_view->focussed = 1;
6273 if (view_is_parent_view(view)) {
6274 err = view_close_child(view);
6275 if (err)
6276 return err;
6277 view_set_child(view, tree_view);
6278 view->focus_child = 1;
6279 } else
6280 *new_view = tree_view;
6281 break;
6282 case 'g':
6283 case KEY_HOME:
6284 s->selected = 0;
6285 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6286 break;
6287 case 'G':
6288 case KEY_END:
6289 s->selected = 0;
6290 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6291 for (n = 0; n < view->nlines - 1; n++) {
6292 if (re == NULL)
6293 break;
6294 s->first_displayed_entry = re;
6295 re = TAILQ_PREV(re, tog_reflist_head, entry);
6297 if (n > 0)
6298 s->selected = n - 1;
6299 break;
6300 case 'k':
6301 case KEY_UP:
6302 case CTRL('p'):
6303 if (s->selected > 0) {
6304 s->selected--;
6305 break;
6307 ref_scroll_up(s, 1);
6308 break;
6309 case KEY_PPAGE:
6310 case CTRL('b'):
6311 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6312 s->selected = 0;
6313 ref_scroll_up(s, MAX(0, view->nlines - 1));
6314 break;
6315 case 'j':
6316 case KEY_DOWN:
6317 case CTRL('n'):
6318 if (s->selected < s->ndisplayed - 1) {
6319 s->selected++;
6320 break;
6322 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6323 /* can't scroll any further */
6324 break;
6325 ref_scroll_down(s, 1);
6326 break;
6327 case KEY_NPAGE:
6328 case CTRL('f'):
6329 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6330 /* can't scroll any further; move cursor down */
6331 if (s->selected < s->ndisplayed - 1)
6332 s->selected = s->ndisplayed - 1;
6333 break;
6335 ref_scroll_down(s, view->nlines - 1);
6336 break;
6337 case CTRL('l'):
6338 tog_free_refs();
6339 err = tog_load_refs(s->repo);
6340 if (err)
6341 break;
6342 ref_view_free_refs(s);
6343 err = ref_view_load_refs(s);
6344 break;
6345 case KEY_RESIZE:
6346 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6347 s->selected = view->nlines - 2;
6348 break;
6349 default:
6350 break;
6353 return err;
6356 __dead static void
6357 usage_ref(void)
6359 endwin();
6360 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6361 getprogname());
6362 exit(1);
6365 static const struct got_error *
6366 cmd_ref(int argc, char *argv[])
6368 const struct got_error *error;
6369 struct got_repository *repo = NULL;
6370 struct got_worktree *worktree = NULL;
6371 char *cwd = NULL, *repo_path = NULL;
6372 int ch;
6373 struct tog_view *view;
6375 while ((ch = getopt(argc, argv, "r:")) != -1) {
6376 switch (ch) {
6377 case 'r':
6378 repo_path = realpath(optarg, NULL);
6379 if (repo_path == NULL)
6380 return got_error_from_errno2("realpath",
6381 optarg);
6382 break;
6383 default:
6384 usage_ref();
6385 /* NOTREACHED */
6389 argc -= optind;
6390 argv += optind;
6392 if (argc > 1)
6393 usage_ref();
6395 if (repo_path == NULL) {
6396 cwd = getcwd(NULL, 0);
6397 if (cwd == NULL)
6398 return got_error_from_errno("getcwd");
6399 error = got_worktree_open(&worktree, cwd);
6400 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6401 goto done;
6402 if (worktree)
6403 repo_path =
6404 strdup(got_worktree_get_repo_path(worktree));
6405 else
6406 repo_path = strdup(cwd);
6407 if (repo_path == NULL) {
6408 error = got_error_from_errno("strdup");
6409 goto done;
6413 error = got_repo_open(&repo, repo_path, NULL);
6414 if (error != NULL)
6415 goto done;
6417 init_curses();
6419 error = apply_unveil(got_repo_get_path(repo), NULL);
6420 if (error)
6421 goto done;
6423 error = tog_load_refs(repo);
6424 if (error)
6425 goto done;
6427 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6428 if (view == NULL) {
6429 error = got_error_from_errno("view_open");
6430 goto done;
6433 error = open_ref_view(view, repo);
6434 if (error)
6435 goto done;
6437 if (worktree) {
6438 /* Release work tree lock. */
6439 got_worktree_close(worktree);
6440 worktree = NULL;
6442 error = view_loop(view);
6443 done:
6444 free(repo_path);
6445 free(cwd);
6446 if (repo) {
6447 const struct got_error *close_err = got_repo_close(repo);
6448 if (close_err)
6449 error = close_err;
6451 tog_free_refs();
6452 return error;
6455 static void
6456 list_commands(FILE *fp)
6458 size_t i;
6460 fprintf(fp, "commands:");
6461 for (i = 0; i < nitems(tog_commands); i++) {
6462 struct tog_cmd *cmd = &tog_commands[i];
6463 fprintf(fp, " %s", cmd->name);
6465 fputc('\n', fp);
6468 __dead static void
6469 usage(int hflag, int status)
6471 FILE *fp = (status == 0) ? stdout : stderr;
6473 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6474 getprogname());
6475 if (hflag) {
6476 fprintf(fp, "lazy usage: %s path\n", getprogname());
6477 list_commands(fp);
6479 exit(status);
6482 static char **
6483 make_argv(int argc, ...)
6485 va_list ap;
6486 char **argv;
6487 int i;
6489 va_start(ap, argc);
6491 argv = calloc(argc, sizeof(char *));
6492 if (argv == NULL)
6493 err(1, "calloc");
6494 for (i = 0; i < argc; i++) {
6495 argv[i] = strdup(va_arg(ap, char *));
6496 if (argv[i] == NULL)
6497 err(1, "strdup");
6500 va_end(ap);
6501 return argv;
6505 * Try to convert 'tog path' into a 'tog log path' command.
6506 * The user could simply have mistyped the command rather than knowingly
6507 * provided a path. So check whether argv[0] can in fact be resolved
6508 * to a path in the HEAD commit and print a special error if not.
6509 * This hack is for mpi@ <3
6511 static const struct got_error *
6512 tog_log_with_path(int argc, char *argv[])
6514 const struct got_error *error = NULL, *close_err;
6515 struct tog_cmd *cmd = NULL;
6516 struct got_repository *repo = NULL;
6517 struct got_worktree *worktree = NULL;
6518 struct got_object_id *commit_id = NULL, *id = NULL;
6519 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6520 char *commit_id_str = NULL, **cmd_argv = NULL;
6522 cwd = getcwd(NULL, 0);
6523 if (cwd == NULL)
6524 return got_error_from_errno("getcwd");
6526 error = got_worktree_open(&worktree, cwd);
6527 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6528 goto done;
6530 if (worktree)
6531 repo_path = strdup(got_worktree_get_repo_path(worktree));
6532 else
6533 repo_path = strdup(cwd);
6534 if (repo_path == NULL) {
6535 error = got_error_from_errno("strdup");
6536 goto done;
6539 error = got_repo_open(&repo, repo_path, NULL);
6540 if (error != NULL)
6541 goto done;
6543 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6544 repo, worktree);
6545 if (error)
6546 goto done;
6548 error = tog_load_refs(repo);
6549 if (error)
6550 goto done;
6551 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6552 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6553 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6554 if (error)
6555 goto done;
6557 if (worktree) {
6558 got_worktree_close(worktree);
6559 worktree = NULL;
6562 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6563 if (error) {
6564 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6565 goto done;
6566 fprintf(stderr, "%s: '%s' is no known command or path\n",
6567 getprogname(), argv[0]);
6568 usage(1, 1);
6569 /* not reached */
6572 close_err = got_repo_close(repo);
6573 if (error == NULL)
6574 error = close_err;
6575 repo = NULL;
6577 error = got_object_id_str(&commit_id_str, commit_id);
6578 if (error)
6579 goto done;
6581 cmd = &tog_commands[0]; /* log */
6582 argc = 4;
6583 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6584 error = cmd->cmd_main(argc, cmd_argv);
6585 done:
6586 if (repo) {
6587 close_err = got_repo_close(repo);
6588 if (error == NULL)
6589 error = close_err;
6591 if (worktree)
6592 got_worktree_close(worktree);
6593 free(id);
6594 free(commit_id_str);
6595 free(commit_id);
6596 free(cwd);
6597 free(repo_path);
6598 free(in_repo_path);
6599 if (cmd_argv) {
6600 int i;
6601 for (i = 0; i < argc; i++)
6602 free(cmd_argv[i]);
6603 free(cmd_argv);
6605 tog_free_refs();
6606 return error;
6609 int
6610 main(int argc, char *argv[])
6612 const struct got_error *error = NULL;
6613 struct tog_cmd *cmd = NULL;
6614 int ch, hflag = 0, Vflag = 0;
6615 char **cmd_argv = NULL;
6616 static struct option longopts[] = {
6617 { "version", no_argument, NULL, 'V' },
6618 { NULL, 0, NULL, 0}
6621 setlocale(LC_CTYPE, "");
6623 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6624 switch (ch) {
6625 case 'h':
6626 hflag = 1;
6627 break;
6628 case 'V':
6629 Vflag = 1;
6630 break;
6631 default:
6632 usage(hflag, 1);
6633 /* NOTREACHED */
6637 argc -= optind;
6638 argv += optind;
6639 optind = 1;
6640 optreset = 1;
6642 if (Vflag) {
6643 got_version_print_str();
6644 return 0;
6647 #ifndef PROFILE
6648 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6649 NULL) == -1)
6650 err(1, "pledge");
6651 #endif
6653 if (argc == 0) {
6654 if (hflag)
6655 usage(hflag, 0);
6656 /* Build an argument vector which runs a default command. */
6657 cmd = &tog_commands[0];
6658 argc = 1;
6659 cmd_argv = make_argv(argc, cmd->name);
6660 } else {
6661 size_t i;
6663 /* Did the user specify a command? */
6664 for (i = 0; i < nitems(tog_commands); i++) {
6665 if (strncmp(tog_commands[i].name, argv[0],
6666 strlen(argv[0])) == 0) {
6667 cmd = &tog_commands[i];
6668 break;
6673 if (cmd == NULL) {
6674 if (argc != 1)
6675 usage(0, 1);
6676 /* No command specified; try log with a path */
6677 error = tog_log_with_path(argc, argv);
6678 } else {
6679 if (hflag)
6680 cmd->cmd_usage();
6681 else
6682 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6685 endwin();
6686 putchar('\n');
6687 if (cmd_argv) {
6688 int i;
6689 for (i = 0; i < argc; i++)
6690 free(cmd_argv[i]);
6691 free(cmd_argv);
6694 if (error && error->code != GOT_ERR_CANCELLED)
6695 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6696 return 0;