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 #include <curses.h>
23 #include <panel.h>
24 #include <locale.h>
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <limits.h>
34 #include <wchar.h>
35 #include <time.h>
36 #include <pthread.h>
37 #include <libgen.h>
38 #include <regex.h>
39 #include <sched.h>
41 #include "got_compat.h"
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_diff.h"
49 #include "got_opentemp.h"
50 #include "got_utf8.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
55 #include "got_path.h"
56 #include "got_worktree.h"
58 //#define update_panels() (0)
59 //#define doupdate() (0)
61 #ifndef MIN
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #endif
65 #ifndef MAX
66 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #endif
69 #ifndef CTRL
70 #define CTRL(x) ((x) & 0x1f)
71 #endif
73 #ifndef nitems
74 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 #endif
77 struct tog_cmd {
78 const char *name;
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
81 };
83 __dead static void usage(int, int);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_ref(void);
90 static const struct got_error* cmd_log(int, char *[]);
91 static const struct got_error* cmd_diff(int, char *[]);
92 static const struct got_error* cmd_blame(int, char *[]);
93 static const struct got_error* cmd_tree(int, char *[]);
94 static const struct got_error* cmd_ref(int, char *[]);
96 static struct tog_cmd tog_commands[] = {
97 { "log", cmd_log, usage_log },
98 { "diff", cmd_diff, usage_diff },
99 { "blame", cmd_blame, usage_blame },
100 { "tree", cmd_tree, usage_tree },
101 { "ref", cmd_ref, usage_ref },
102 };
104 enum tog_view_type {
105 TOG_VIEW_DIFF,
106 TOG_VIEW_LOG,
107 TOG_VIEW_BLAME,
108 TOG_VIEW_TREE,
109 TOG_VIEW_REF,
110 };
112 #define TOG_EOF_STRING "(END)"
114 struct commit_queue_entry {
115 TAILQ_ENTRY(commit_queue_entry) entry;
116 struct got_object_id *id;
117 struct got_commit_object *commit;
118 int idx;
119 };
120 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
121 struct commit_queue {
122 int ncommits;
123 struct commit_queue_head head;
124 };
126 struct tog_color {
127 STAILQ_ENTRY(tog_color) entry;
128 regex_t regex;
129 short colorpair;
130 };
131 STAILQ_HEAD(tog_colors, tog_color);
133 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
134 static struct got_reflist_object_id_map *tog_refs_idmap;
136 static const struct got_error *
137 tog_load_refs(struct got_repository *repo)
139 const struct got_error *err;
141 err = got_ref_list(&tog_refs, repo, NULL, got_ref_cmp_by_name, NULL);
142 if (err)
143 return err;
145 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
146 repo);
149 static void
150 tog_free_refs(void)
152 if (tog_refs_idmap) {
153 got_reflist_object_id_map_free(tog_refs_idmap);
154 tog_refs_idmap = NULL;
156 got_ref_list_free(&tog_refs);
159 static const struct got_error *
160 add_color(struct tog_colors *colors, const char *pattern,
161 int idx, short color)
163 const struct got_error *err = NULL;
164 struct tog_color *tc;
165 int regerr = 0;
167 if (idx < 1 || idx > COLOR_PAIRS - 1)
168 return NULL;
170 init_pair(idx, color, -1);
172 tc = calloc(1, sizeof(*tc));
173 if (tc == NULL)
174 return got_error_from_errno("calloc");
175 regerr = regcomp(&tc->regex, pattern,
176 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
177 if (regerr) {
178 static char regerr_msg[512];
179 static char err_msg[512];
180 regerror(regerr, &tc->regex, regerr_msg,
181 sizeof(regerr_msg));
182 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
183 regerr_msg);
184 err = got_error_msg(GOT_ERR_REGEX, err_msg);
185 free(tc);
186 return err;
188 tc->colorpair = idx;
189 STAILQ_INSERT_HEAD(colors, tc, entry);
190 return NULL;
193 static void
194 free_colors(struct tog_colors *colors)
196 struct tog_color *tc;
198 while (!STAILQ_EMPTY(colors)) {
199 tc = STAILQ_FIRST(colors);
200 STAILQ_REMOVE_HEAD(colors, entry);
201 regfree(&tc->regex);
202 free(tc);
206 struct tog_color *
207 get_color(struct tog_colors *colors, int colorpair)
209 struct tog_color *tc = NULL;
211 STAILQ_FOREACH(tc, colors, entry) {
212 if (tc->colorpair == colorpair)
213 return tc;
216 return NULL;
219 static int
220 default_color_value(const char *envvar)
222 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
223 return COLOR_MAGENTA;
224 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
225 return COLOR_CYAN;
226 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
227 return COLOR_YELLOW;
228 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
229 return COLOR_GREEN;
230 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
231 return COLOR_MAGENTA;
232 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
233 return COLOR_MAGENTA;
234 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
235 return COLOR_CYAN;
236 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
237 return COLOR_GREEN;
238 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
239 return COLOR_GREEN;
240 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
241 return COLOR_CYAN;
242 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
243 return COLOR_YELLOW;
244 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
245 return COLOR_GREEN;
246 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
247 return COLOR_MAGENTA;
248 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
249 return COLOR_YELLOW;
251 return -1;
254 static int
255 get_color_value(const char *envvar)
257 const char *val = getenv(envvar);
259 if (val == NULL)
260 return default_color_value(envvar);
262 if (strcasecmp(val, "black") == 0)
263 return COLOR_BLACK;
264 if (strcasecmp(val, "red") == 0)
265 return COLOR_RED;
266 if (strcasecmp(val, "green") == 0)
267 return COLOR_GREEN;
268 if (strcasecmp(val, "yellow") == 0)
269 return COLOR_YELLOW;
270 if (strcasecmp(val, "blue") == 0)
271 return COLOR_BLUE;
272 if (strcasecmp(val, "magenta") == 0)
273 return COLOR_MAGENTA;
274 if (strcasecmp(val, "cyan") == 0)
275 return COLOR_CYAN;
276 if (strcasecmp(val, "white") == 0)
277 return COLOR_WHITE;
278 if (strcasecmp(val, "default") == 0)
279 return -1;
281 return default_color_value(envvar);
285 struct tog_diff_view_state {
286 struct got_object_id *id1, *id2;
287 const char *label1, *label2;
288 FILE *f;
289 int first_displayed_line;
290 int last_displayed_line;
291 int eof;
292 int diff_context;
293 int ignore_whitespace;
294 int force_text_diff;
295 struct got_repository *repo;
296 struct tog_colors colors;
297 size_t nlines;
298 off_t *line_offsets;
299 int matched_line;
300 int selected_line;
302 /* passed from log view; may be NULL */
303 struct tog_view *log_view;
304 };
306 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
308 struct tog_log_thread_args {
309 pthread_cond_t need_commits;
310 pthread_cond_t commit_loaded;
311 int commits_needed;
312 int load_all;
313 struct got_commit_graph *graph;
314 struct commit_queue *commits;
315 const char *in_repo_path;
316 struct got_object_id *start_id;
317 struct got_repository *repo;
318 int log_complete;
319 sig_atomic_t *quit;
320 struct commit_queue_entry **first_displayed_entry;
321 struct commit_queue_entry **selected_entry;
322 int *searching;
323 int *search_next_done;
324 regex_t *regex;
325 };
327 struct tog_log_view_state {
328 struct commit_queue commits;
329 struct commit_queue_entry *first_displayed_entry;
330 struct commit_queue_entry *last_displayed_entry;
331 struct commit_queue_entry *selected_entry;
332 int selected;
333 char *in_repo_path;
334 char *head_ref_name;
335 int log_branches;
336 struct got_repository *repo;
337 struct got_object_id *start_id;
338 sig_atomic_t quit;
339 pthread_t thread;
340 struct tog_log_thread_args thread_args;
341 struct commit_queue_entry *matched_entry;
342 struct commit_queue_entry *search_entry;
343 struct tog_colors colors;
344 };
346 #define TOG_COLOR_DIFF_MINUS 1
347 #define TOG_COLOR_DIFF_PLUS 2
348 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
349 #define TOG_COLOR_DIFF_META 4
350 #define TOG_COLOR_TREE_SUBMODULE 5
351 #define TOG_COLOR_TREE_SYMLINK 6
352 #define TOG_COLOR_TREE_DIRECTORY 7
353 #define TOG_COLOR_TREE_EXECUTABLE 8
354 #define TOG_COLOR_COMMIT 9
355 #define TOG_COLOR_AUTHOR 10
356 #define TOG_COLOR_DATE 11
357 #define TOG_COLOR_REFS_HEADS 12
358 #define TOG_COLOR_REFS_TAGS 13
359 #define TOG_COLOR_REFS_REMOTES 14
361 struct tog_blame_cb_args {
362 struct tog_blame_line *lines; /* one per line */
363 int nlines;
365 struct tog_view *view;
366 struct got_object_id *commit_id;
367 int *quit;
368 };
370 struct tog_blame_thread_args {
371 const char *path;
372 struct got_repository *repo;
373 struct tog_blame_cb_args *cb_args;
374 int *complete;
375 got_cancel_cb cancel_cb;
376 void *cancel_arg;
377 };
379 struct tog_blame {
380 FILE *f;
381 off_t filesize;
382 struct tog_blame_line *lines;
383 int nlines;
384 off_t *line_offsets;
385 pthread_t thread;
386 struct tog_blame_thread_args thread_args;
387 struct tog_blame_cb_args cb_args;
388 const char *path;
389 };
391 struct tog_blame_view_state {
392 int first_displayed_line;
393 int last_displayed_line;
394 int selected_line;
395 int blame_complete;
396 int eof;
397 int done;
398 struct got_object_id_queue blamed_commits;
399 struct got_object_qid *blamed_commit;
400 char *path;
401 struct got_repository *repo;
402 struct got_object_id *commit_id;
403 struct tog_blame blame;
404 int matched_line;
405 struct tog_colors colors;
406 };
408 struct tog_parent_tree {
409 TAILQ_ENTRY(tog_parent_tree) entry;
410 struct got_tree_object *tree;
411 struct got_tree_entry *first_displayed_entry;
412 struct got_tree_entry *selected_entry;
413 int selected;
414 };
416 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
418 struct tog_tree_view_state {
419 char *tree_label;
420 struct got_object_id *commit_id;/* commit which this tree belongs to */
421 struct got_tree_object *root; /* the commit's root tree entry */
422 struct got_tree_object *tree; /* currently displayed (sub-)tree */
423 struct got_tree_entry *first_displayed_entry;
424 struct got_tree_entry *last_displayed_entry;
425 struct got_tree_entry *selected_entry;
426 int ndisplayed, selected, show_ids;
427 struct tog_parent_trees parents; /* parent trees of current sub-tree */
428 char *head_ref_name;
429 struct got_repository *repo;
430 struct got_tree_entry *matched_entry;
431 struct tog_colors colors;
432 };
434 struct tog_reflist_entry {
435 TAILQ_ENTRY(tog_reflist_entry) entry;
436 struct got_reference *ref;
437 int idx;
438 };
440 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
442 struct tog_ref_view_state {
443 struct tog_reflist_head refs;
444 struct tog_reflist_entry *first_displayed_entry;
445 struct tog_reflist_entry *last_displayed_entry;
446 struct tog_reflist_entry *selected_entry;
447 int nrefs, ndisplayed, selected, show_ids;
448 struct got_repository *repo;
449 struct tog_reflist_entry *matched_entry;
450 struct tog_colors colors;
451 };
453 /*
454 * We implement two types of views: parent views and child views.
456 * The 'Tab' key switches focus between a parent view and its child view.
457 * Child views are shown side-by-side to their parent view, provided
458 * there is enough screen estate.
460 * When a new view is opened from within a parent view, this new view
461 * becomes a child view of the parent view, replacing any existing child.
463 * When a new view is opened from within a child view, this new view
464 * becomes a parent view which will obscure the views below until the
465 * user quits the new parent view by typing 'q'.
467 * This list of views contains parent views only.
468 * Child views are only pointed to by their parent view.
469 */
470 TAILQ_HEAD(tog_view_list_head, tog_view);
472 struct tog_view {
473 TAILQ_ENTRY(tog_view) entry;
474 WINDOW *window;
475 PANEL *panel;
476 int nlines, ncols, begin_y, begin_x;
477 int lines, cols; /* copies of LINES and COLS */
478 int focussed; /* Only set on one parent or child view at a time. */
479 int dying;
480 struct tog_view *parent;
481 struct tog_view *child;
483 /*
484 * This flag is initially set on parent views when a new child view
485 * is created. It gets toggled when the 'Tab' key switches focus
486 * between parent and child.
487 * The flag indicates whether focus should be passed on to our child
488 * view if this parent view gets picked for focus after another parent
489 * view was closed. This prevents child views from losing focus in such
490 * situations.
491 */
492 int focus_child;
494 /* type-specific state */
495 enum tog_view_type type;
496 union {
497 struct tog_diff_view_state diff;
498 struct tog_log_view_state log;
499 struct tog_blame_view_state blame;
500 struct tog_tree_view_state tree;
501 struct tog_ref_view_state ref;
502 } state;
504 const struct got_error *(*show)(struct tog_view *);
505 const struct got_error *(*input)(struct tog_view **,
506 struct tog_view *, int);
507 const struct got_error *(*close)(struct tog_view *);
509 const struct got_error *(*search_start)(struct tog_view *);
510 const struct got_error *(*search_next)(struct tog_view *);
511 int search_started;
512 int searching;
513 #define TOG_SEARCH_FORWARD 1
514 #define TOG_SEARCH_BACKWARD 2
515 int search_next_done;
516 #define TOG_SEARCH_HAVE_MORE 1
517 #define TOG_SEARCH_NO_MORE 2
518 #define TOG_SEARCH_HAVE_NONE 3
519 regex_t regex;
520 regmatch_t regmatch;
521 };
523 static const struct got_error *open_diff_view(struct tog_view *,
524 struct got_object_id *, struct got_object_id *,
525 const char *, const char *, int, int, int, struct tog_view *,
526 struct got_repository *);
527 static const struct got_error *show_diff_view(struct tog_view *);
528 static const struct got_error *input_diff_view(struct tog_view **,
529 struct tog_view *, int);
530 static const struct got_error* close_diff_view(struct tog_view *);
531 static const struct got_error *search_start_diff_view(struct tog_view *);
532 static const struct got_error *search_next_diff_view(struct tog_view *);
534 static const struct got_error *open_log_view(struct tog_view *,
535 struct got_object_id *, struct got_repository *,
536 const char *, const char *, int);
537 static const struct got_error * show_log_view(struct tog_view *);
538 static const struct got_error *input_log_view(struct tog_view **,
539 struct tog_view *, int);
540 static const struct got_error *close_log_view(struct tog_view *);
541 static const struct got_error *search_start_log_view(struct tog_view *);
542 static const struct got_error *search_next_log_view(struct tog_view *);
544 static const struct got_error *open_blame_view(struct tog_view *, char *,
545 struct got_object_id *, struct got_repository *);
546 static const struct got_error *show_blame_view(struct tog_view *);
547 static const struct got_error *input_blame_view(struct tog_view **,
548 struct tog_view *, int);
549 static const struct got_error *close_blame_view(struct tog_view *);
550 static const struct got_error *search_start_blame_view(struct tog_view *);
551 static const struct got_error *search_next_blame_view(struct tog_view *);
553 static const struct got_error *open_tree_view(struct tog_view *,
554 struct got_object_id *, const char *, struct got_repository *);
555 static const struct got_error *show_tree_view(struct tog_view *);
556 static const struct got_error *input_tree_view(struct tog_view **,
557 struct tog_view *, int);
558 static const struct got_error *close_tree_view(struct tog_view *);
559 static const struct got_error *search_start_tree_view(struct tog_view *);
560 static const struct got_error *search_next_tree_view(struct tog_view *);
562 static const struct got_error *open_ref_view(struct tog_view *,
563 struct got_repository *);
564 static const struct got_error *show_ref_view(struct tog_view *);
565 static const struct got_error *input_ref_view(struct tog_view **,
566 struct tog_view *, int);
567 static const struct got_error *close_ref_view(struct tog_view *);
568 static const struct got_error *search_start_ref_view(struct tog_view *);
569 static const struct got_error *search_next_ref_view(struct tog_view *);
571 static volatile sig_atomic_t tog_sigwinch_received;
572 static volatile sig_atomic_t tog_sigpipe_received;
573 static volatile sig_atomic_t tog_sigcont_received;
575 static void
576 tog_sigwinch(int signo)
578 tog_sigwinch_received = 1;
581 static void
582 tog_sigpipe(int signo)
584 tog_sigpipe_received = 1;
587 static void
588 tog_sigcont(int signo)
590 tog_sigcont_received = 1;
593 static const struct got_error *
594 view_close(struct tog_view *view)
596 const struct got_error *err = NULL;
598 if (view->child) {
599 view_close(view->child);
600 view->child = NULL;
602 if (view->close)
603 err = view->close(view);
604 if (view->panel)
605 del_panel(view->panel);
606 if (view->window)
607 delwin(view->window);
608 free(view);
609 return err;
612 static struct tog_view *
613 view_open(int nlines, int ncols, int begin_y, int begin_x,
614 enum tog_view_type type)
616 struct tog_view *view = calloc(1, sizeof(*view));
618 if (view == NULL)
619 return NULL;
621 view->type = type;
622 view->lines = LINES;
623 view->cols = COLS;
624 view->nlines = nlines ? nlines : LINES - begin_y;
625 view->ncols = ncols ? ncols : COLS - begin_x;
626 view->begin_y = begin_y;
627 view->begin_x = begin_x;
628 view->window = newwin(nlines, ncols, begin_y, begin_x);
629 if (view->window == NULL) {
630 view_close(view);
631 return NULL;
633 view->panel = new_panel(view->window);
634 if (view->panel == NULL ||
635 set_panel_userptr(view->panel, view) != OK) {
636 view_close(view);
637 return NULL;
640 keypad(view->window, TRUE);
641 return view;
644 static int
645 view_split_begin_x(int begin_x)
647 if (begin_x > 0 || COLS < 120)
648 return 0;
649 return (COLS - MAX(COLS / 2, 80));
652 static const struct got_error *view_resize(struct tog_view *);
654 static const struct got_error *
655 view_splitscreen(struct tog_view *view)
657 const struct got_error *err = NULL;
659 view->begin_y = 0;
660 view->begin_x = view_split_begin_x(0);
661 view->nlines = LINES;
662 view->ncols = COLS - view->begin_x;
663 view->lines = LINES;
664 view->cols = COLS;
665 err = view_resize(view);
666 if (err)
667 return err;
669 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
670 return got_error_from_errno("mvwin");
672 return NULL;
675 static const struct got_error *
676 view_fullscreen(struct tog_view *view)
678 const struct got_error *err = NULL;
680 view->begin_x = 0;
681 view->begin_y = 0;
682 view->nlines = LINES;
683 view->ncols = COLS;
684 view->lines = LINES;
685 view->cols = COLS;
686 err = view_resize(view);
687 if (err)
688 return err;
690 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
691 return got_error_from_errno("mvwin");
693 return NULL;
696 static int
697 view_is_parent_view(struct tog_view *view)
699 return view->parent == NULL;
702 static const struct got_error *
703 view_resize(struct tog_view *view)
705 int nlines, ncols;
707 if (view->lines > LINES)
708 nlines = view->nlines - (view->lines - LINES);
709 else
710 nlines = view->nlines + (LINES - view->lines);
712 if (view->cols > COLS)
713 ncols = view->ncols - (view->cols - COLS);
714 else
715 ncols = view->ncols + (COLS - view->cols);
717 if (wresize(view->window, nlines, ncols) == ERR)
718 return got_error_from_errno("wresize");
719 if (replace_panel(view->panel, view->window) == ERR)
720 return got_error_from_errno("replace_panel");
721 wclear(view->window);
723 view->nlines = nlines;
724 view->ncols = ncols;
725 view->lines = LINES;
726 view->cols = COLS;
728 if (view->child) {
729 view->child->begin_x = view_split_begin_x(view->begin_x);
730 if (view->child->begin_x == 0) {
731 view_fullscreen(view->child);
732 if (view->child->focussed)
733 show_panel(view->child->panel);
734 else
735 show_panel(view->panel);
736 } else {
737 view_splitscreen(view->child);
738 show_panel(view->child->panel);
742 return NULL;
745 static const struct got_error *
746 view_close_child(struct tog_view *view)
748 const struct got_error *err = NULL;
750 if (view->child == NULL)
751 return NULL;
753 err = view_close(view->child);
754 view->child = NULL;
755 return err;
758 static void
759 view_set_child(struct tog_view *view, struct tog_view *child)
761 view->child = child;
762 child->parent = view;
765 static int
766 view_is_splitscreen(struct tog_view *view)
768 return view->begin_x > 0;
771 static void
772 tog_resizeterm(void)
774 int cols, lines;
775 struct winsize size;
777 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
778 cols = 80; /* Default */
779 lines = 24;
780 } else {
781 cols = size.ws_col;
782 lines = size.ws_row;
784 resize_term(lines, cols);
787 static const struct got_error *
788 view_search_start(struct tog_view *view)
790 const struct got_error *err = NULL;
791 char pattern[1024];
792 int ret;
794 if (view->search_started) {
795 regfree(&view->regex);
796 view->searching = 0;
797 memset(&view->regmatch, 0, sizeof(view->regmatch));
799 view->search_started = 0;
801 if (view->nlines < 1)
802 return NULL;
804 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
805 wclrtoeol(view->window);
807 nocbreak();
808 echo();
809 ret = wgetnstr(view->window, pattern, sizeof(pattern));
810 cbreak();
811 noecho();
812 if (ret == ERR)
813 return NULL;
815 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
816 err = view->search_start(view);
817 if (err) {
818 regfree(&view->regex);
819 return err;
821 view->search_started = 1;
822 view->searching = TOG_SEARCH_FORWARD;
823 view->search_next_done = 0;
824 view->search_next(view);
827 return NULL;
830 static const struct got_error *
831 view_input(struct tog_view **new, int *done, struct tog_view *view,
832 struct tog_view_list_head *views)
834 const struct got_error *err = NULL;
835 struct tog_view *v;
836 int ch, errcode;
838 *new = NULL;
840 /* Clear "no matches" indicator. */
841 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
842 view->search_next_done == TOG_SEARCH_HAVE_NONE)
843 view->search_next_done = TOG_SEARCH_HAVE_MORE;
845 if (view->searching && !view->search_next_done) {
846 errcode = pthread_mutex_unlock(&tog_mutex);
847 if (errcode)
848 return got_error_set_errno(errcode,
849 "pthread_mutex_unlock");
850 sched_yield();
851 errcode = pthread_mutex_lock(&tog_mutex);
852 if (errcode)
853 return got_error_set_errno(errcode,
854 "pthread_mutex_lock");
855 view->search_next(view);
856 return NULL;
859 nodelay(stdscr, FALSE);
860 /* Allow threads to make progress while we are waiting for input. */
861 errcode = pthread_mutex_unlock(&tog_mutex);
862 if (errcode)
863 return got_error_set_errno(errcode, "pthread_mutex_unlock");
864 ch = wgetch(view->window);
865 errcode = pthread_mutex_lock(&tog_mutex);
866 if (errcode)
867 return got_error_set_errno(errcode, "pthread_mutex_lock");
868 nodelay(stdscr, TRUE);
870 if (tog_sigwinch_received || tog_sigcont_received) {
871 tog_resizeterm();
872 tog_sigwinch_received = 0;
873 tog_sigcont_received = 0;
874 TAILQ_FOREACH(v, views, entry) {
875 err = view_resize(v);
876 if (err)
877 return err;
878 err = v->input(new, v, KEY_RESIZE);
879 if (err)
880 return err;
881 if (v->child) {
882 err = view_resize(v->child);
883 if (err)
884 return err;
885 err = v->child->input(new, v->child,
886 KEY_RESIZE);
887 if (err)
888 return err;
893 switch (ch) {
894 case '\t':
895 if (view->child) {
896 view->focussed = 0;
897 view->child->focussed = 1;
898 view->focus_child = 1;
899 } else if (view->parent) {
900 view->focussed = 0;
901 view->parent->focussed = 1;
902 view->parent->focus_child = 0;
904 break;
905 case 'q':
906 err = view->input(new, view, ch);
907 view->dying = 1;
908 break;
909 case 'Q':
910 *done = 1;
911 break;
912 case 'f':
913 if (view_is_parent_view(view)) {
914 if (view->child == NULL)
915 break;
916 if (view_is_splitscreen(view->child)) {
917 view->focussed = 0;
918 view->child->focussed = 1;
919 err = view_fullscreen(view->child);
920 } else
921 err = view_splitscreen(view->child);
922 if (err)
923 break;
924 err = view->child->input(new, view->child,
925 KEY_RESIZE);
926 } else {
927 if (view_is_splitscreen(view)) {
928 view->parent->focussed = 0;
929 view->focussed = 1;
930 err = view_fullscreen(view);
931 } else {
932 err = view_splitscreen(view);
934 if (err)
935 break;
936 err = view->input(new, view, KEY_RESIZE);
938 break;
939 case KEY_RESIZE:
940 break;
941 case '/':
942 if (view->search_start)
943 view_search_start(view);
944 else
945 err = view->input(new, view, ch);
946 break;
947 case 'N':
948 case 'n':
949 if (view->search_started && view->search_next) {
950 view->searching = (ch == 'n' ?
951 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
952 view->search_next_done = 0;
953 view->search_next(view);
954 } else
955 err = view->input(new, view, ch);
956 break;
957 default:
958 err = view->input(new, view, ch);
959 break;
962 return err;
965 void
966 view_vborder(struct tog_view *view)
968 PANEL *panel;
969 const struct tog_view *view_above;
971 if (view->parent)
972 return view_vborder(view->parent);
974 panel = panel_above(view->panel);
975 if (panel == NULL)
976 return;
978 view_above = panel_userptr(panel);
979 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
980 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
983 int
984 view_needs_focus_indication(struct tog_view *view)
986 if (view_is_parent_view(view)) {
987 if (view->child == NULL || view->child->focussed)
988 return 0;
989 if (!view_is_splitscreen(view->child))
990 return 0;
991 } else if (!view_is_splitscreen(view))
992 return 0;
994 return view->focussed;
997 static const struct got_error *
998 view_loop(struct tog_view *view)
1000 const struct got_error *err = NULL;
1001 struct tog_view_list_head views;
1002 struct tog_view *new_view;
1003 int fast_refresh = 10;
1004 int done = 0, errcode;
1006 errcode = pthread_mutex_lock(&tog_mutex);
1007 if (errcode)
1008 return got_error_set_errno(errcode, "pthread_mutex_lock");
1010 TAILQ_INIT(&views);
1011 TAILQ_INSERT_HEAD(&views, view, entry);
1013 view->focussed = 1;
1014 err = view->show(view);
1015 if (err)
1016 return err;
1017 update_panels();
1018 doupdate();
1019 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1020 /* Refresh fast during initialization, then become slower. */
1021 if (fast_refresh && fast_refresh-- == 0)
1022 halfdelay(10); /* switch to once per second */
1024 err = view_input(&new_view, &done, view, &views);
1025 if (err)
1026 break;
1027 if (view->dying) {
1028 struct tog_view *v, *prev = NULL;
1030 if (view_is_parent_view(view))
1031 prev = TAILQ_PREV(view, tog_view_list_head,
1032 entry);
1033 else if (view->parent)
1034 prev = view->parent;
1036 if (view->parent) {
1037 view->parent->child = NULL;
1038 view->parent->focus_child = 0;
1039 } else
1040 TAILQ_REMOVE(&views, view, entry);
1042 err = view_close(view);
1043 if (err)
1044 goto done;
1046 view = NULL;
1047 TAILQ_FOREACH(v, &views, entry) {
1048 if (v->focussed)
1049 break;
1051 if (view == NULL && new_view == NULL) {
1052 /* No view has focus. Try to pick one. */
1053 if (prev)
1054 view = prev;
1055 else if (!TAILQ_EMPTY(&views)) {
1056 view = TAILQ_LAST(&views,
1057 tog_view_list_head);
1059 if (view) {
1060 if (view->focus_child) {
1061 view->child->focussed = 1;
1062 view = view->child;
1063 } else
1064 view->focussed = 1;
1068 if (new_view) {
1069 struct tog_view *v, *t;
1070 /* Only allow one parent view per type. */
1071 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1072 if (v->type != new_view->type)
1073 continue;
1074 TAILQ_REMOVE(&views, v, entry);
1075 err = view_close(v);
1076 if (err)
1077 goto done;
1078 break;
1080 TAILQ_INSERT_TAIL(&views, new_view, entry);
1081 view = new_view;
1083 if (view) {
1084 if (view_is_parent_view(view)) {
1085 if (view->child && view->child->focussed)
1086 view = view->child;
1087 } else {
1088 if (view->parent && view->parent->focussed)
1089 view = view->parent;
1091 show_panel(view->panel);
1092 if (view->child && view_is_splitscreen(view->child))
1093 show_panel(view->child->panel);
1094 if (view->parent && view_is_splitscreen(view)) {
1095 err = view->parent->show(view->parent);
1096 if (err)
1097 goto done;
1099 err = view->show(view);
1100 if (err)
1101 goto done;
1102 if (view->child) {
1103 err = view->child->show(view->child);
1104 if (err)
1105 goto done;
1107 update_panels();
1108 doupdate();
1111 done:
1112 while (!TAILQ_EMPTY(&views)) {
1113 view = TAILQ_FIRST(&views);
1114 TAILQ_REMOVE(&views, view, entry);
1115 view_close(view);
1118 errcode = pthread_mutex_unlock(&tog_mutex);
1119 if (errcode && err == NULL)
1120 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1122 return err;
1125 __dead static void
1126 usage_log(void)
1128 endwin();
1129 fprintf(stderr,
1130 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1131 getprogname());
1132 exit(1);
1135 /* Create newly allocated wide-character string equivalent to a byte string. */
1136 static const struct got_error *
1137 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1139 char *vis = NULL;
1140 const struct got_error *err = NULL;
1142 *ws = NULL;
1143 *wlen = mbstowcs(NULL, s, 0);
1144 if (*wlen == (size_t)-1) {
1145 int vislen;
1146 if (errno != EILSEQ)
1147 return got_error_from_errno("mbstowcs");
1149 /* byte string invalid in current encoding; try to "fix" it */
1150 err = got_mbsavis(&vis, &vislen, s);
1151 if (err)
1152 return err;
1153 *wlen = mbstowcs(NULL, vis, 0);
1154 if (*wlen == (size_t)-1) {
1155 err = got_error_from_errno("mbstowcs"); /* give up */
1156 goto done;
1160 *ws = calloc(*wlen + 1, sizeof(**ws));
1161 if (*ws == NULL) {
1162 err = got_error_from_errno("calloc");
1163 goto done;
1166 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1167 err = got_error_from_errno("mbstowcs");
1168 done:
1169 free(vis);
1170 if (err) {
1171 free(*ws);
1172 *ws = NULL;
1173 *wlen = 0;
1175 return err;
1178 /* Format a line for display, ensuring that it won't overflow a width limit. */
1179 static const struct got_error *
1180 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1181 int col_tab_align)
1183 const struct got_error *err = NULL;
1184 int cols = 0;
1185 wchar_t *wline = NULL;
1186 size_t wlen;
1187 int i;
1189 *wlinep = NULL;
1190 *widthp = 0;
1192 err = mbs2ws(&wline, &wlen, line);
1193 if (err)
1194 return err;
1196 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1197 wline[wlen - 1] = L'\0';
1198 wlen--;
1200 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1201 wline[wlen - 1] = L'\0';
1202 wlen--;
1205 i = 0;
1206 while (i < wlen) {
1207 int width = wcwidth(wline[i]);
1209 if (width == 0) {
1210 i++;
1211 continue;
1214 if (width == 1 || width == 2) {
1215 if (cols + width > wlimit)
1216 break;
1217 cols += width;
1218 i++;
1219 } else if (width == -1) {
1220 if (wline[i] == L'\t') {
1221 width = TABSIZE -
1222 ((cols + col_tab_align) % TABSIZE);
1223 } else {
1224 width = 1;
1225 wline[i] = L'.';
1227 if (cols + width > wlimit)
1228 break;
1229 cols += width;
1230 i++;
1231 } else {
1232 err = got_error_from_errno("wcwidth");
1233 goto done;
1236 wline[i] = L'\0';
1237 if (widthp)
1238 *widthp = cols;
1239 done:
1240 if (err)
1241 free(wline);
1242 else
1243 *wlinep = wline;
1244 return err;
1247 static const struct got_error*
1248 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1249 struct got_object_id *id, struct got_repository *repo)
1251 static const struct got_error *err = NULL;
1252 struct got_reflist_entry *re;
1253 char *s;
1254 const char *name;
1256 *refs_str = NULL;
1258 TAILQ_FOREACH(re, refs, entry) {
1259 struct got_tag_object *tag = NULL;
1260 struct got_object_id *ref_id;
1261 int cmp;
1263 name = got_ref_get_name(re->ref);
1264 if (strcmp(name, GOT_REF_HEAD) == 0)
1265 continue;
1266 if (strncmp(name, "refs/", 5) == 0)
1267 name += 5;
1268 if (strncmp(name, "got/", 4) == 0)
1269 continue;
1270 if (strncmp(name, "heads/", 6) == 0)
1271 name += 6;
1272 if (strncmp(name, "remotes/", 8) == 0) {
1273 name += 8;
1274 s = strstr(name, "/" GOT_REF_HEAD);
1275 if (s != NULL && s[strlen(s)] == '\0')
1276 continue;
1278 err = got_ref_resolve(&ref_id, repo, re->ref);
1279 if (err)
1280 break;
1281 if (strncmp(name, "tags/", 5) == 0) {
1282 err = got_object_open_as_tag(&tag, repo, ref_id);
1283 if (err) {
1284 if (err->code != GOT_ERR_OBJ_TYPE) {
1285 free(ref_id);
1286 break;
1288 /* Ref points at something other than a tag. */
1289 err = NULL;
1290 tag = NULL;
1293 cmp = got_object_id_cmp(tag ?
1294 got_object_tag_get_object_id(tag) : ref_id, id);
1295 free(ref_id);
1296 if (tag)
1297 got_object_tag_close(tag);
1298 if (cmp != 0)
1299 continue;
1300 s = *refs_str;
1301 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1302 s ? ", " : "", name) == -1) {
1303 err = got_error_from_errno("asprintf");
1304 free(s);
1305 *refs_str = NULL;
1306 break;
1308 free(s);
1311 return err;
1314 static const struct got_error *
1315 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1316 int col_tab_align)
1318 char *smallerthan;
1320 smallerthan = strchr(author, '<');
1321 if (smallerthan && smallerthan[1] != '\0')
1322 author = smallerthan + 1;
1323 author[strcspn(author, "@>")] = '\0';
1324 return format_line(wauthor, author_width, author, limit, col_tab_align);
1327 static const struct got_error *
1328 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1329 struct got_object_id *id, const size_t date_display_cols,
1330 int author_display_cols)
1332 struct tog_log_view_state *s = &view->state.log;
1333 const struct got_error *err = NULL;
1334 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1335 char *logmsg0 = NULL, *logmsg = NULL;
1336 char *author = NULL;
1337 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1338 int author_width, logmsg_width;
1339 char *newline, *line = NULL;
1340 int col, limit;
1341 const int avail = view->ncols;
1342 struct tm tm;
1343 time_t committer_time;
1344 struct tog_color *tc;
1346 committer_time = got_object_commit_get_committer_time(commit);
1347 if (gmtime_r(&committer_time, &tm) == NULL)
1348 return got_error_from_errno("gmtime_r");
1349 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1350 return got_error(GOT_ERR_NO_SPACE);
1352 if (avail <= date_display_cols)
1353 limit = MIN(sizeof(datebuf) - 1, avail);
1354 else
1355 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1356 tc = get_color(&s->colors, TOG_COLOR_DATE);
1357 if (tc)
1358 wattr_on(view->window,
1359 COLOR_PAIR(tc->colorpair), NULL);
1360 waddnstr(view->window, datebuf, limit);
1361 if (tc)
1362 wattr_off(view->window,
1363 COLOR_PAIR(tc->colorpair), NULL);
1364 col = limit;
1365 if (col > avail)
1366 goto done;
1368 if (avail >= 120) {
1369 char *id_str;
1370 err = got_object_id_str(&id_str, id);
1371 if (err)
1372 goto done;
1373 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1374 if (tc)
1375 wattr_on(view->window,
1376 COLOR_PAIR(tc->colorpair), NULL);
1377 wprintw(view->window, "%.8s ", id_str);
1378 if (tc)
1379 wattr_off(view->window,
1380 COLOR_PAIR(tc->colorpair), NULL);
1381 free(id_str);
1382 col += 9;
1383 if (col > avail)
1384 goto done;
1387 author = strdup(got_object_commit_get_author(commit));
1388 if (author == NULL) {
1389 err = got_error_from_errno("strdup");
1390 goto done;
1392 err = format_author(&wauthor, &author_width, author, avail - col, col);
1393 if (err)
1394 goto done;
1395 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1396 if (tc)
1397 wattr_on(view->window,
1398 COLOR_PAIR(tc->colorpair), NULL);
1399 waddwstr(view->window, wauthor);
1400 if (tc)
1401 wattr_off(view->window,
1402 COLOR_PAIR(tc->colorpair), NULL);
1403 col += author_width;
1404 while (col < avail && author_width < author_display_cols + 2) {
1405 waddch(view->window, ' ');
1406 col++;
1407 author_width++;
1409 if (col > avail)
1410 goto done;
1412 err = got_object_commit_get_logmsg(&logmsg0, commit);
1413 if (err)
1414 goto done;
1415 logmsg = logmsg0;
1416 while (*logmsg == '\n')
1417 logmsg++;
1418 newline = strchr(logmsg, '\n');
1419 if (newline)
1420 *newline = '\0';
1421 limit = avail - col;
1422 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1423 if (err)
1424 goto done;
1425 waddwstr(view->window, wlogmsg);
1426 col += logmsg_width;
1427 while (col < avail) {
1428 waddch(view->window, ' ');
1429 col++;
1431 done:
1432 free(logmsg0);
1433 free(wlogmsg);
1434 free(author);
1435 free(wauthor);
1436 free(line);
1437 return err;
1440 static struct commit_queue_entry *
1441 alloc_commit_queue_entry(struct got_commit_object *commit,
1442 struct got_object_id *id)
1444 struct commit_queue_entry *entry;
1446 entry = calloc(1, sizeof(*entry));
1447 if (entry == NULL)
1448 return NULL;
1450 entry->id = id;
1451 entry->commit = commit;
1452 return entry;
1455 static void
1456 pop_commit(struct commit_queue *commits)
1458 struct commit_queue_entry *entry;
1460 entry = TAILQ_FIRST(&commits->head);
1461 TAILQ_REMOVE(&commits->head, entry, entry);
1462 got_object_commit_close(entry->commit);
1463 commits->ncommits--;
1464 /* Don't free entry->id! It is owned by the commit graph. */
1465 free(entry);
1468 static void
1469 free_commits(struct commit_queue *commits)
1471 while (!TAILQ_EMPTY(&commits->head))
1472 pop_commit(commits);
1475 static const struct got_error *
1476 match_commit(int *have_match, struct got_object_id *id,
1477 struct got_commit_object *commit, regex_t *regex)
1479 const struct got_error *err = NULL;
1480 regmatch_t regmatch;
1481 char *id_str = NULL, *logmsg = NULL;
1483 *have_match = 0;
1485 err = got_object_id_str(&id_str, id);
1486 if (err)
1487 return err;
1489 err = got_object_commit_get_logmsg(&logmsg, commit);
1490 if (err)
1491 goto done;
1493 if (regexec(regex, got_object_commit_get_author(commit), 1,
1494 &regmatch, 0) == 0 ||
1495 regexec(regex, got_object_commit_get_committer(commit), 1,
1496 &regmatch, 0) == 0 ||
1497 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1498 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1499 *have_match = 1;
1500 done:
1501 free(id_str);
1502 free(logmsg);
1503 return err;
1506 static const struct got_error *
1507 queue_commits(struct tog_log_thread_args *a)
1509 const struct got_error *err = NULL;
1512 * We keep all commits open throughout the lifetime of the log
1513 * view in order to avoid having to re-fetch commits from disk
1514 * while updating the display.
1516 do {
1517 struct got_object_id *id;
1518 struct got_commit_object *commit;
1519 struct commit_queue_entry *entry;
1520 int errcode;
1522 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1523 NULL, NULL);
1524 if (err || id == NULL)
1525 break;
1527 err = got_object_open_as_commit(&commit, a->repo, id);
1528 if (err)
1529 break;
1530 entry = alloc_commit_queue_entry(commit, id);
1531 if (entry == NULL) {
1532 err = got_error_from_errno("alloc_commit_queue_entry");
1533 break;
1536 errcode = pthread_mutex_lock(&tog_mutex);
1537 if (errcode) {
1538 err = got_error_set_errno(errcode,
1539 "pthread_mutex_lock");
1540 break;
1543 entry->idx = a->commits->ncommits;
1544 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1545 a->commits->ncommits++;
1547 if (*a->searching == TOG_SEARCH_FORWARD &&
1548 !*a->search_next_done) {
1549 int have_match;
1550 err = match_commit(&have_match, id, commit, a->regex);
1551 if (err)
1552 break;
1553 if (have_match)
1554 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1557 errcode = pthread_mutex_unlock(&tog_mutex);
1558 if (errcode && err == NULL)
1559 err = got_error_set_errno(errcode,
1560 "pthread_mutex_unlock");
1561 if (err)
1562 break;
1563 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1565 return err;
1568 static void
1569 select_commit(struct tog_log_view_state *s)
1571 struct commit_queue_entry *entry;
1572 int ncommits = 0;
1574 entry = s->first_displayed_entry;
1575 while (entry) {
1576 if (ncommits == s->selected) {
1577 s->selected_entry = entry;
1578 break;
1580 entry = TAILQ_NEXT(entry, entry);
1581 ncommits++;
1585 static const struct got_error *
1586 draw_commits(struct tog_view *view)
1588 const struct got_error *err = NULL;
1589 struct tog_log_view_state *s = &view->state.log;
1590 struct commit_queue_entry *entry = s->selected_entry;
1591 const int limit = view->nlines;
1592 int width;
1593 int ncommits, author_cols = 4;
1594 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1595 char *refs_str = NULL;
1596 wchar_t *wline;
1597 struct tog_color *tc;
1598 static const size_t date_display_cols = 12;
1600 if (s->selected_entry &&
1601 !(view->searching && view->search_next_done == 0)) {
1602 struct got_reflist_head *refs;
1603 err = got_object_id_str(&id_str, s->selected_entry->id);
1604 if (err)
1605 return err;
1606 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1607 s->selected_entry->id);
1608 if (refs) {
1609 err = build_refs_str(&refs_str, refs,
1610 s->selected_entry->id, s->repo);
1611 if (err)
1612 goto done;
1616 if (s->thread_args.commits_needed == 0)
1617 halfdelay(10); /* disable fast refresh */
1619 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1620 if (asprintf(&ncommits_str, " [%d/%d] %s",
1621 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1622 (view->searching && !view->search_next_done) ?
1623 "searching..." : "loading...") == -1) {
1624 err = got_error_from_errno("asprintf");
1625 goto done;
1627 } else {
1628 const char *search_str = NULL;
1630 if (view->searching) {
1631 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1632 search_str = "no more matches";
1633 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1634 search_str = "no matches found";
1635 else if (!view->search_next_done)
1636 search_str = "searching...";
1639 if (asprintf(&ncommits_str, " [%d/%d] %s",
1640 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1641 search_str ? search_str :
1642 (refs_str ? refs_str : "")) == -1) {
1643 err = got_error_from_errno("asprintf");
1644 goto done;
1648 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1649 if (asprintf(&header, "commit %s %s%s",
1650 id_str ? id_str : "........................................",
1651 s->in_repo_path, ncommits_str) == -1) {
1652 err = got_error_from_errno("asprintf");
1653 header = NULL;
1654 goto done;
1656 } else if (asprintf(&header, "commit %s%s",
1657 id_str ? id_str : "........................................",
1658 ncommits_str) == -1) {
1659 err = got_error_from_errno("asprintf");
1660 header = NULL;
1661 goto done;
1663 err = format_line(&wline, &width, header, view->ncols, 0);
1664 if (err)
1665 goto done;
1667 werase(view->window);
1669 if (view_needs_focus_indication(view))
1670 wstandout(view->window);
1671 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1672 if (tc)
1673 wattr_on(view->window,
1674 COLOR_PAIR(tc->colorpair), NULL);
1675 waddwstr(view->window, wline);
1676 if (tc)
1677 wattr_off(view->window,
1678 COLOR_PAIR(tc->colorpair), NULL);
1679 while (width < view->ncols) {
1680 waddch(view->window, ' ');
1681 width++;
1683 if (view_needs_focus_indication(view))
1684 wstandend(view->window);
1685 free(wline);
1686 if (limit <= 1)
1687 goto done;
1689 /* Grow author column size if necessary. */
1690 entry = s->first_displayed_entry;
1691 ncommits = 0;
1692 while (entry) {
1693 char *author;
1694 wchar_t *wauthor;
1695 int width;
1696 if (ncommits >= limit - 1)
1697 break;
1698 author = strdup(got_object_commit_get_author(entry->commit));
1699 if (author == NULL) {
1700 err = got_error_from_errno("strdup");
1701 goto done;
1703 err = format_author(&wauthor, &width, author, COLS,
1704 date_display_cols);
1705 if (author_cols < width)
1706 author_cols = width;
1707 free(wauthor);
1708 free(author);
1709 ncommits++;
1710 entry = TAILQ_NEXT(entry, entry);
1713 entry = s->first_displayed_entry;
1714 s->last_displayed_entry = s->first_displayed_entry;
1715 ncommits = 0;
1716 while (entry) {
1717 if (ncommits >= limit - 1)
1718 break;
1719 if (ncommits == s->selected)
1720 wstandout(view->window);
1721 err = draw_commit(view, entry->commit, entry->id,
1722 date_display_cols, author_cols);
1723 if (ncommits == s->selected)
1724 wstandend(view->window);
1725 if (err)
1726 goto done;
1727 ncommits++;
1728 s->last_displayed_entry = entry;
1729 entry = TAILQ_NEXT(entry, entry);
1732 view_vborder(view);
1733 update_panels();
1734 doupdate();
1735 done:
1736 free(id_str);
1737 free(refs_str);
1738 free(ncommits_str);
1739 free(header);
1740 return err;
1743 static void
1744 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1746 struct commit_queue_entry *entry;
1747 int nscrolled = 0;
1749 entry = TAILQ_FIRST(&s->commits.head);
1750 if (s->first_displayed_entry == entry)
1751 return;
1753 entry = s->first_displayed_entry;
1754 while (entry && nscrolled < maxscroll) {
1755 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1756 if (entry) {
1757 s->first_displayed_entry = entry;
1758 nscrolled++;
1763 static const struct got_error *
1764 trigger_log_thread(struct tog_view *view, int wait)
1766 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1767 int errcode;
1769 halfdelay(1); /* fast refresh while loading commits */
1771 while (ta->commits_needed > 0 || ta->load_all) {
1772 if (ta->log_complete)
1773 break;
1775 /* Wake the log thread. */
1776 errcode = pthread_cond_signal(&ta->need_commits);
1777 if (errcode)
1778 return got_error_set_errno(errcode,
1779 "pthread_cond_signal");
1782 * The mutex will be released while the view loop waits
1783 * in wgetch(), at which time the log thread will run.
1785 if (!wait)
1786 break;
1788 /* Display progress update in log view. */
1789 show_log_view(view);
1790 update_panels();
1791 doupdate();
1793 /* Wait right here while next commit is being loaded. */
1794 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1795 if (errcode)
1796 return got_error_set_errno(errcode,
1797 "pthread_cond_wait");
1799 /* Display progress update in log view. */
1800 show_log_view(view);
1801 update_panels();
1802 doupdate();
1805 return NULL;
1808 static const struct got_error *
1809 log_scroll_down(struct tog_view *view, int maxscroll)
1811 struct tog_log_view_state *s = &view->state.log;
1812 const struct got_error *err = NULL;
1813 struct commit_queue_entry *pentry;
1814 int nscrolled = 0, ncommits_needed;
1816 if (s->last_displayed_entry == NULL)
1817 return NULL;
1819 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1820 if (s->commits.ncommits < ncommits_needed &&
1821 !s->thread_args.log_complete) {
1823 * Ask the log thread for required amount of commits.
1825 s->thread_args.commits_needed += maxscroll;
1826 err = trigger_log_thread(view, 1);
1827 if (err)
1828 return err;
1831 do {
1832 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1833 if (pentry == NULL)
1834 break;
1836 s->last_displayed_entry = pentry;
1838 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1839 if (pentry == NULL)
1840 break;
1841 s->first_displayed_entry = pentry;
1842 } while (++nscrolled < maxscroll);
1844 return err;
1847 static const struct got_error *
1848 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1849 struct got_commit_object *commit, struct got_object_id *commit_id,
1850 struct tog_view *log_view, struct got_repository *repo)
1852 const struct got_error *err;
1853 struct got_object_qid *parent_id;
1854 struct tog_view *diff_view;
1856 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1857 if (diff_view == NULL)
1858 return got_error_from_errno("view_open");
1860 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1861 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1862 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1863 if (err == NULL)
1864 *new_view = diff_view;
1865 return err;
1868 static const struct got_error *
1869 tree_view_visit_subtree(struct tog_tree_view_state *s,
1870 struct got_tree_object *subtree)
1872 struct tog_parent_tree *parent;
1874 parent = calloc(1, sizeof(*parent));
1875 if (parent == NULL)
1876 return got_error_from_errno("calloc");
1878 parent->tree = s->tree;
1879 parent->first_displayed_entry = s->first_displayed_entry;
1880 parent->selected_entry = s->selected_entry;
1881 parent->selected = s->selected;
1882 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1883 s->tree = subtree;
1884 s->selected = 0;
1885 s->first_displayed_entry = NULL;
1886 return NULL;
1889 static const struct got_error *
1890 tree_view_walk_path(struct tog_tree_view_state *s,
1891 struct got_object_id *commit_id, const char *path)
1893 const struct got_error *err = NULL;
1894 struct got_tree_object *tree = NULL;
1895 const char *p;
1896 char *slash, *subpath = NULL;
1898 /* Walk the path and open corresponding tree objects. */
1899 p = path;
1900 while (*p) {
1901 struct got_tree_entry *te;
1902 struct got_object_id *tree_id;
1903 char *te_name;
1905 while (p[0] == '/')
1906 p++;
1908 /* Ensure the correct subtree entry is selected. */
1909 slash = strchr(p, '/');
1910 if (slash == NULL)
1911 te_name = strdup(p);
1912 else
1913 te_name = strndup(p, slash - p);
1914 if (te_name == NULL) {
1915 err = got_error_from_errno("strndup");
1916 break;
1918 te = got_object_tree_find_entry(s->tree, te_name);
1919 if (te == NULL) {
1920 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1921 free(te_name);
1922 break;
1924 free(te_name);
1925 s->first_displayed_entry = s->selected_entry = te;
1927 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1928 break; /* jump to this file's entry */
1930 slash = strchr(p, '/');
1931 if (slash)
1932 subpath = strndup(path, slash - path);
1933 else
1934 subpath = strdup(path);
1935 if (subpath == NULL) {
1936 err = got_error_from_errno("strdup");
1937 break;
1940 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1941 subpath);
1942 if (err)
1943 break;
1945 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1946 free(tree_id);
1947 if (err)
1948 break;
1950 err = tree_view_visit_subtree(s, tree);
1951 if (err) {
1952 got_object_tree_close(tree);
1953 break;
1955 if (slash == NULL)
1956 break;
1957 free(subpath);
1958 subpath = NULL;
1959 p = slash;
1962 free(subpath);
1963 return err;
1966 static const struct got_error *
1967 browse_commit_tree(struct tog_view **new_view, int begin_x,
1968 struct commit_queue_entry *entry, const char *path,
1969 const char *head_ref_name, struct got_repository *repo)
1971 const struct got_error *err = NULL;
1972 struct tog_tree_view_state *s;
1973 struct tog_view *tree_view;
1975 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1976 if (tree_view == NULL)
1977 return got_error_from_errno("view_open");
1979 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1980 if (err)
1981 return err;
1982 s = &tree_view->state.tree;
1984 *new_view = tree_view;
1986 if (got_path_is_root_dir(path))
1987 return NULL;
1989 return tree_view_walk_path(s, entry->id, path);
1992 static const struct got_error *
1993 block_signals_used_by_main_thread(void)
1995 sigset_t sigset;
1996 int errcode;
1998 if (sigemptyset(&sigset) == -1)
1999 return got_error_from_errno("sigemptyset");
2001 /* tog handles SIGWINCH and SIGCONT */
2002 if (sigaddset(&sigset, SIGWINCH) == -1)
2003 return got_error_from_errno("sigaddset");
2004 if (sigaddset(&sigset, SIGCONT) == -1)
2005 return got_error_from_errno("sigaddset");
2007 /* ncurses handles SIGTSTP */
2008 if (sigaddset(&sigset, SIGTSTP) == -1)
2009 return got_error_from_errno("sigaddset");
2011 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2012 if (errcode)
2013 return got_error_set_errno(errcode, "pthread_sigmask");
2015 return NULL;
2018 static void *
2019 log_thread(void *arg)
2021 const struct got_error *err = NULL;
2022 int errcode = 0;
2023 struct tog_log_thread_args *a = arg;
2024 int done = 0;
2026 err = block_signals_used_by_main_thread();
2027 if (err)
2028 return (void *)err;
2030 while (!done && !err && !tog_sigpipe_received) {
2031 err = queue_commits(a);
2032 if (err) {
2033 if (err->code != GOT_ERR_ITER_COMPLETED)
2034 return (void *)err;
2035 err = NULL;
2036 done = 1;
2037 } else if (a->commits_needed > 0 && !a->load_all)
2038 a->commits_needed--;
2040 errcode = pthread_mutex_lock(&tog_mutex);
2041 if (errcode) {
2042 err = got_error_set_errno(errcode,
2043 "pthread_mutex_lock");
2044 break;
2045 } else if (*a->quit)
2046 done = 1;
2047 else if (*a->first_displayed_entry == NULL) {
2048 *a->first_displayed_entry =
2049 TAILQ_FIRST(&a->commits->head);
2050 *a->selected_entry = *a->first_displayed_entry;
2053 errcode = pthread_cond_signal(&a->commit_loaded);
2054 if (errcode) {
2055 err = got_error_set_errno(errcode,
2056 "pthread_cond_signal");
2057 pthread_mutex_unlock(&tog_mutex);
2058 break;
2061 if (done)
2062 a->commits_needed = 0;
2063 else {
2064 if (a->commits_needed == 0 && !a->load_all) {
2065 errcode = pthread_cond_wait(&a->need_commits,
2066 &tog_mutex);
2067 if (errcode)
2068 err = got_error_set_errno(errcode,
2069 "pthread_cond_wait");
2070 if (*a->quit)
2071 done = 1;
2075 errcode = pthread_mutex_unlock(&tog_mutex);
2076 if (errcode && err == NULL)
2077 err = got_error_set_errno(errcode,
2078 "pthread_mutex_unlock");
2080 a->log_complete = 1;
2081 return (void *)err;
2084 static const struct got_error *
2085 stop_log_thread(struct tog_log_view_state *s)
2087 const struct got_error *err = NULL;
2088 int errcode;
2090 if (s->thread) {
2091 s->quit = 1;
2092 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2093 if (errcode)
2094 return got_error_set_errno(errcode,
2095 "pthread_cond_signal");
2096 errcode = pthread_mutex_unlock(&tog_mutex);
2097 if (errcode)
2098 return got_error_set_errno(errcode,
2099 "pthread_mutex_unlock");
2100 errcode = pthread_join(s->thread, (void **)&err);
2101 if (errcode)
2102 return got_error_set_errno(errcode, "pthread_join");
2103 errcode = pthread_mutex_lock(&tog_mutex);
2104 if (errcode)
2105 return got_error_set_errno(errcode,
2106 "pthread_mutex_lock");
2107 s->thread = 0; //NULL;
2110 if (s->thread_args.repo) {
2111 err = got_repo_close(s->thread_args.repo);
2112 s->thread_args.repo = NULL;
2115 if (s->thread_args.graph) {
2116 got_commit_graph_close(s->thread_args.graph);
2117 s->thread_args.graph = NULL;
2120 return err;
2123 static const struct got_error *
2124 close_log_view(struct tog_view *view)
2126 const struct got_error *err = NULL;
2127 struct tog_log_view_state *s = &view->state.log;
2128 int errcode;
2130 err = stop_log_thread(s);
2132 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2133 if (errcode && err == NULL)
2134 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2136 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2137 if (errcode && err == NULL)
2138 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2140 free_commits(&s->commits);
2141 free(s->in_repo_path);
2142 s->in_repo_path = NULL;
2143 free(s->start_id);
2144 s->start_id = NULL;
2145 free(s->head_ref_name);
2146 s->head_ref_name = NULL;
2147 return err;
2150 static const struct got_error *
2151 search_start_log_view(struct tog_view *view)
2153 struct tog_log_view_state *s = &view->state.log;
2155 s->matched_entry = NULL;
2156 s->search_entry = NULL;
2157 return NULL;
2160 static const struct got_error *
2161 search_next_log_view(struct tog_view *view)
2163 const struct got_error *err = NULL;
2164 struct tog_log_view_state *s = &view->state.log;
2165 struct commit_queue_entry *entry;
2167 /* Display progress update in log view. */
2168 show_log_view(view);
2169 update_panels();
2170 doupdate();
2172 if (s->search_entry) {
2173 int errcode, ch;
2174 errcode = pthread_mutex_unlock(&tog_mutex);
2175 if (errcode)
2176 return got_error_set_errno(errcode,
2177 "pthread_mutex_unlock");
2178 ch = wgetch(view->window);
2179 errcode = pthread_mutex_lock(&tog_mutex);
2180 if (errcode)
2181 return got_error_set_errno(errcode,
2182 "pthread_mutex_lock");
2183 if (ch == KEY_BACKSPACE) {
2184 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2185 return NULL;
2187 if (view->searching == TOG_SEARCH_FORWARD)
2188 entry = TAILQ_NEXT(s->search_entry, entry);
2189 else
2190 entry = TAILQ_PREV(s->search_entry,
2191 commit_queue_head, entry);
2192 } else if (s->matched_entry) {
2193 if (view->searching == TOG_SEARCH_FORWARD)
2194 entry = TAILQ_NEXT(s->matched_entry, entry);
2195 else
2196 entry = TAILQ_PREV(s->matched_entry,
2197 commit_queue_head, entry);
2198 } else {
2199 if (view->searching == TOG_SEARCH_FORWARD)
2200 entry = TAILQ_FIRST(&s->commits.head);
2201 else
2202 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2205 while (1) {
2206 int have_match = 0;
2208 if (entry == NULL) {
2209 if (s->thread_args.log_complete ||
2210 view->searching == TOG_SEARCH_BACKWARD) {
2211 view->search_next_done =
2212 (s->matched_entry == NULL ?
2213 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2214 s->search_entry = NULL;
2215 return NULL;
2218 * Poke the log thread for more commits and return,
2219 * allowing the main loop to make progress. Search
2220 * will resume at s->search_entry once we come back.
2222 s->thread_args.commits_needed++;
2223 return trigger_log_thread(view, 0);
2226 err = match_commit(&have_match, entry->id, entry->commit,
2227 &view->regex);
2228 if (err)
2229 break;
2230 if (have_match) {
2231 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2232 s->matched_entry = entry;
2233 break;
2236 s->search_entry = entry;
2237 if (view->searching == TOG_SEARCH_FORWARD)
2238 entry = TAILQ_NEXT(entry, entry);
2239 else
2240 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2243 if (s->matched_entry) {
2244 int cur = s->selected_entry->idx;
2245 while (cur < s->matched_entry->idx) {
2246 err = input_log_view(NULL, view, KEY_DOWN);
2247 if (err)
2248 return err;
2249 cur++;
2251 while (cur > s->matched_entry->idx) {
2252 err = input_log_view(NULL, view, KEY_UP);
2253 if (err)
2254 return err;
2255 cur--;
2259 s->search_entry = NULL;
2261 return NULL;
2264 static const struct got_error *
2265 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2266 struct got_repository *repo, const char *head_ref_name,
2267 const char *in_repo_path, int log_branches)
2269 const struct got_error *err = NULL;
2270 struct tog_log_view_state *s = &view->state.log;
2271 struct got_repository *thread_repo = NULL;
2272 struct got_commit_graph *thread_graph = NULL;
2273 int errcode;
2275 if (in_repo_path != s->in_repo_path) {
2276 free(s->in_repo_path);
2277 s->in_repo_path = strdup(in_repo_path);
2278 if (s->in_repo_path == NULL)
2279 return got_error_from_errno("strdup");
2282 /* The commit queue only contains commits being displayed. */
2283 TAILQ_INIT(&s->commits.head);
2284 s->commits.ncommits = 0;
2286 s->repo = repo;
2287 if (head_ref_name) {
2288 s->head_ref_name = strdup(head_ref_name);
2289 if (s->head_ref_name == NULL) {
2290 err = got_error_from_errno("strdup");
2291 goto done;
2294 s->start_id = got_object_id_dup(start_id);
2295 if (s->start_id == NULL) {
2296 err = got_error_from_errno("got_object_id_dup");
2297 goto done;
2299 s->log_branches = log_branches;
2301 STAILQ_INIT(&s->colors);
2302 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2303 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2304 get_color_value("TOG_COLOR_COMMIT"));
2305 if (err)
2306 goto done;
2307 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2308 get_color_value("TOG_COLOR_AUTHOR"));
2309 if (err) {
2310 free_colors(&s->colors);
2311 goto done;
2313 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2314 get_color_value("TOG_COLOR_DATE"));
2315 if (err) {
2316 free_colors(&s->colors);
2317 goto done;
2321 view->show = show_log_view;
2322 view->input = input_log_view;
2323 view->close = close_log_view;
2324 view->search_start = search_start_log_view;
2325 view->search_next = search_next_log_view;
2327 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2328 if (err)
2329 goto done;
2330 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2331 !s->log_branches);
2332 if (err)
2333 goto done;
2334 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2335 s->repo, NULL, NULL);
2336 if (err)
2337 goto done;
2339 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2340 if (errcode) {
2341 err = got_error_set_errno(errcode, "pthread_cond_init");
2342 goto done;
2344 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2345 if (errcode) {
2346 err = got_error_set_errno(errcode, "pthread_cond_init");
2347 goto done;
2350 s->thread_args.commits_needed = view->nlines;
2351 s->thread_args.graph = thread_graph;
2352 s->thread_args.commits = &s->commits;
2353 s->thread_args.in_repo_path = s->in_repo_path;
2354 s->thread_args.start_id = s->start_id;
2355 s->thread_args.repo = thread_repo;
2356 s->thread_args.log_complete = 0;
2357 s->thread_args.quit = &s->quit;
2358 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2359 s->thread_args.selected_entry = &s->selected_entry;
2360 s->thread_args.searching = &view->searching;
2361 s->thread_args.search_next_done = &view->search_next_done;
2362 s->thread_args.regex = &view->regex;
2363 done:
2364 if (err)
2365 close_log_view(view);
2366 return err;
2369 static const struct got_error *
2370 show_log_view(struct tog_view *view)
2372 const struct got_error *err;
2373 struct tog_log_view_state *s = &view->state.log;
2375 if (s->thread == 0) { //NULL) {
2376 int errcode = pthread_create(&s->thread, NULL, log_thread,
2377 &s->thread_args);
2378 if (errcode)
2379 return got_error_set_errno(errcode, "pthread_create");
2380 if (s->thread_args.commits_needed > 0) {
2381 err = trigger_log_thread(view, 1);
2382 if (err)
2383 return err;
2387 return draw_commits(view);
2390 static const struct got_error *
2391 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2393 const struct got_error *err = NULL;
2394 struct tog_log_view_state *s = &view->state.log;
2395 struct tog_view *diff_view = NULL, *tree_view = NULL;
2396 struct tog_view *ref_view = NULL;
2397 struct commit_queue_entry *entry;
2398 int begin_x = 0, n;
2400 if (s->thread_args.load_all) {
2401 if (ch == KEY_BACKSPACE)
2402 s->thread_args.load_all = 0;
2403 else if (s->thread_args.log_complete) {
2404 s->thread_args.load_all = 0;
2405 log_scroll_down(view, s->commits.ncommits);
2406 s->selected = MIN(view->nlines - 2,
2407 s->commits.ncommits - 1);
2408 select_commit(s);
2410 return NULL;
2413 switch (ch) {
2414 case 'q':
2415 s->quit = 1;
2416 break;
2417 case 'k':
2418 case KEY_UP:
2419 case '<':
2420 case ',':
2421 if (s->first_displayed_entry == NULL)
2422 break;
2423 if (s->selected > 0)
2424 s->selected--;
2425 else
2426 log_scroll_up(s, 1);
2427 select_commit(s);
2428 break;
2429 case 'g':
2430 case KEY_HOME:
2431 s->selected = 0;
2432 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2433 select_commit(s);
2434 break;
2435 case KEY_PPAGE:
2436 case CTRL('b'):
2437 if (s->first_displayed_entry == NULL)
2438 break;
2439 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2440 s->selected = 0;
2441 else
2442 log_scroll_up(s, view->nlines - 1);
2443 select_commit(s);
2444 break;
2445 case 'j':
2446 case KEY_DOWN:
2447 case '>':
2448 case '.':
2449 if (s->first_displayed_entry == NULL)
2450 break;
2451 if (s->selected < MIN(view->nlines - 2,
2452 s->commits.ncommits - 1))
2453 s->selected++;
2454 else {
2455 err = log_scroll_down(view, 1);
2456 if (err)
2457 break;
2459 select_commit(s);
2460 break;
2461 case 'G':
2462 case KEY_END: {
2463 /* We don't know yet how many commits, so we're forced to
2464 * traverse them all. */
2465 if (!s->thread_args.log_complete) {
2466 s->thread_args.load_all = 1;
2467 return trigger_log_thread(view, 0);
2470 s->selected = 0;
2471 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2472 for (n = 0; n < view->nlines - 1; n++) {
2473 if (entry == NULL)
2474 break;
2475 s->first_displayed_entry = entry;
2476 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2478 if (n > 0)
2479 s->selected = n - 1;
2480 select_commit(s);
2481 break;
2483 case KEY_NPAGE:
2484 case CTRL('f'): {
2485 struct commit_queue_entry *first;
2486 first = s->first_displayed_entry;
2487 if (first == NULL)
2488 break;
2489 err = log_scroll_down(view, view->nlines - 1);
2490 if (err)
2491 break;
2492 if (first == s->first_displayed_entry &&
2493 s->selected < MIN(view->nlines - 2,
2494 s->commits.ncommits - 1)) {
2495 /* can't scroll further down */
2496 s->selected = MIN(view->nlines - 2,
2497 s->commits.ncommits - 1);
2499 select_commit(s);
2500 break;
2502 case KEY_RESIZE:
2503 if (s->selected > view->nlines - 2)
2504 s->selected = view->nlines - 2;
2505 if (s->selected > s->commits.ncommits - 1)
2506 s->selected = s->commits.ncommits - 1;
2507 select_commit(s);
2508 if (s->commits.ncommits < view->nlines - 1 &&
2509 !s->thread_args.log_complete) {
2510 s->thread_args.commits_needed += (view->nlines - 1) -
2511 s->commits.ncommits;
2512 err = trigger_log_thread(view, 1);
2514 break;
2515 case KEY_ENTER:
2516 case ' ':
2517 case '\r':
2518 if (s->selected_entry == NULL)
2519 break;
2520 if (view_is_parent_view(view))
2521 begin_x = view_split_begin_x(view->begin_x);
2522 err = open_diff_view_for_commit(&diff_view, begin_x,
2523 s->selected_entry->commit, s->selected_entry->id,
2524 view, s->repo);
2525 if (err)
2526 break;
2527 view->focussed = 0;
2528 diff_view->focussed = 1;
2529 if (view_is_parent_view(view)) {
2530 err = view_close_child(view);
2531 if (err)
2532 return err;
2533 view_set_child(view, diff_view);
2534 view->focus_child = 1;
2535 } else
2536 *new_view = diff_view;
2537 break;
2538 case 't':
2539 if (s->selected_entry == NULL)
2540 break;
2541 if (view_is_parent_view(view))
2542 begin_x = view_split_begin_x(view->begin_x);
2543 err = browse_commit_tree(&tree_view, begin_x,
2544 s->selected_entry, s->in_repo_path, s->head_ref_name,
2545 s->repo);
2546 if (err)
2547 break;
2548 view->focussed = 0;
2549 tree_view->focussed = 1;
2550 if (view_is_parent_view(view)) {
2551 err = view_close_child(view);
2552 if (err)
2553 return err;
2554 view_set_child(view, tree_view);
2555 view->focus_child = 1;
2556 } else
2557 *new_view = tree_view;
2558 break;
2559 case KEY_BACKSPACE:
2560 case CTRL('l'):
2561 case 'B':
2562 if (ch == KEY_BACKSPACE &&
2563 got_path_is_root_dir(s->in_repo_path))
2564 break;
2565 err = stop_log_thread(s);
2566 if (err)
2567 return err;
2568 if (ch == KEY_BACKSPACE) {
2569 char *parent_path;
2570 err = got_path_dirname(&parent_path, s->in_repo_path);
2571 if (err)
2572 return err;
2573 free(s->in_repo_path);
2574 s->in_repo_path = parent_path;
2575 s->thread_args.in_repo_path = s->in_repo_path;
2576 } else if (ch == CTRL('l')) {
2577 struct got_object_id *start_id;
2578 err = got_repo_match_object_id(&start_id, NULL,
2579 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2580 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2581 if (err)
2582 return err;
2583 free(s->start_id);
2584 s->start_id = start_id;
2585 s->thread_args.start_id = s->start_id;
2586 } else /* 'B' */
2587 s->log_branches = !s->log_branches;
2589 err = got_repo_open(&s->thread_args.repo,
2590 got_repo_get_path(s->repo), NULL);
2591 if (err)
2592 return err;
2593 tog_free_refs();
2594 err = tog_load_refs(s->repo);
2595 if (err)
2596 return err;
2597 err = got_commit_graph_open(&s->thread_args.graph,
2598 s->in_repo_path, !s->log_branches);
2599 if (err)
2600 return err;
2601 err = got_commit_graph_iter_start(s->thread_args.graph,
2602 s->start_id, s->repo, NULL, NULL);
2603 if (err)
2604 return err;
2605 free_commits(&s->commits);
2606 s->first_displayed_entry = NULL;
2607 s->last_displayed_entry = NULL;
2608 s->selected_entry = NULL;
2609 s->selected = 0;
2610 s->thread_args.log_complete = 0;
2611 s->quit = 0;
2612 s->thread_args.commits_needed = view->nlines;
2613 break;
2614 case 'r':
2615 if (view_is_parent_view(view))
2616 begin_x = view_split_begin_x(view->begin_x);
2617 ref_view = view_open(view->nlines, view->ncols,
2618 view->begin_y, begin_x, TOG_VIEW_REF);
2619 if (ref_view == NULL)
2620 return got_error_from_errno("view_open");
2621 err = open_ref_view(ref_view, s->repo);
2622 if (err) {
2623 view_close(ref_view);
2624 return err;
2626 view->focussed = 0;
2627 ref_view->focussed = 1;
2628 if (view_is_parent_view(view)) {
2629 err = view_close_child(view);
2630 if (err)
2631 return err;
2632 view_set_child(view, ref_view);
2633 view->focus_child = 1;
2634 } else
2635 *new_view = ref_view;
2636 break;
2637 default:
2638 break;
2641 return err;
2644 static const struct got_error *
2645 apply_unveil(const char *repo_path, const char *worktree_path)
2647 const struct got_error *error;
2649 #ifdef PROFILE
2650 if (unveil("gmon.out", "rwc") != 0)
2651 return got_error_from_errno2("unveil", "gmon.out");
2652 #endif
2653 if (repo_path && unveil(repo_path, "r") != 0)
2654 return got_error_from_errno2("unveil", repo_path);
2656 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2657 return got_error_from_errno2("unveil", worktree_path);
2659 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2660 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2662 error = got_privsep_unveil_exec_helpers();
2663 if (error != NULL)
2664 return error;
2666 if (unveil(NULL, NULL) != 0)
2667 return got_error_from_errno("unveil");
2669 return NULL;
2672 static void
2673 init_curses(void)
2675 initscr();
2676 cbreak();
2677 halfdelay(1); /* Do fast refresh while initial view is loading. */
2678 noecho();
2679 nonl();
2680 intrflush(stdscr, FALSE);
2681 keypad(stdscr, TRUE);
2682 curs_set(0);
2683 if (getenv("TOG_COLORS") != NULL) {
2684 start_color();
2685 use_default_colors();
2687 signal(SIGWINCH, tog_sigwinch);
2688 signal(SIGPIPE, tog_sigpipe);
2689 signal(SIGCONT, tog_sigcont);
2692 static const struct got_error *
2693 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2694 struct got_repository *repo, struct got_worktree *worktree)
2696 const struct got_error *err = NULL;
2698 if (argc == 0) {
2699 *in_repo_path = strdup("/");
2700 if (*in_repo_path == NULL)
2701 return got_error_from_errno("strdup");
2702 return NULL;
2705 if (worktree) {
2706 const char *prefix = got_worktree_get_path_prefix(worktree);
2707 char *p;
2709 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2710 if (err)
2711 return err;
2712 if (asprintf(in_repo_path, "%s%s%s", prefix,
2713 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2714 p) == -1) {
2715 err = got_error_from_errno("asprintf");
2716 *in_repo_path = NULL;
2718 free(p);
2719 } else
2720 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2722 return err;
2725 static const struct got_error *
2726 cmd_log(int argc, char *argv[])
2728 const struct got_error *error;
2729 struct got_repository *repo = NULL;
2730 struct got_worktree *worktree = NULL;
2731 struct got_object_id *start_id = NULL;
2732 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2733 char *start_commit = NULL, *label = NULL;
2734 struct got_reference *ref = NULL;
2735 const char *head_ref_name = NULL;
2736 int ch, log_branches = 0;
2737 struct tog_view *view;
2739 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2740 switch (ch) {
2741 case 'b':
2742 log_branches = 1;
2743 break;
2744 case 'c':
2745 start_commit = optarg;
2746 break;
2747 case 'r':
2748 repo_path = realpath(optarg, NULL);
2749 if (repo_path == NULL)
2750 return got_error_from_errno2("realpath",
2751 optarg);
2752 break;
2753 default:
2754 usage_log();
2755 /* NOTREACHED */
2759 argc -= optind;
2760 argv += optind;
2762 if (argc > 1)
2763 usage_log();
2765 if (repo_path == NULL) {
2766 cwd = getcwd(NULL, 0);
2767 if (cwd == NULL)
2768 return got_error_from_errno("getcwd");
2769 error = got_worktree_open(&worktree, cwd);
2770 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2771 goto done;
2772 if (worktree)
2773 repo_path =
2774 strdup(got_worktree_get_repo_path(worktree));
2775 else
2776 repo_path = strdup(cwd);
2777 if (repo_path == NULL) {
2778 error = got_error_from_errno("strdup");
2779 goto done;
2783 error = got_repo_open(&repo, repo_path, NULL);
2784 if (error != NULL)
2785 goto done;
2787 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2788 repo, worktree);
2789 if (error)
2790 goto done;
2792 init_curses();
2794 error = apply_unveil(got_repo_get_path(repo),
2795 worktree ? got_worktree_get_root_path(worktree) : NULL);
2796 if (error)
2797 goto done;
2799 /* already loaded by tog_log_with_path()? */
2800 if (TAILQ_EMPTY(&tog_refs)) {
2801 error = tog_load_refs(repo);
2802 if (error)
2803 goto done;
2806 if (start_commit == NULL) {
2807 error = got_repo_match_object_id(&start_id, &label,
2808 worktree ? got_worktree_get_head_ref_name(worktree) :
2809 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2810 if (error)
2811 goto done;
2812 head_ref_name = label;
2813 } else {
2814 error = got_ref_open(&ref, repo, start_commit, 0);
2815 if (error == NULL)
2816 head_ref_name = got_ref_get_name(ref);
2817 else if (error->code != GOT_ERR_NOT_REF)
2818 goto done;
2819 error = got_repo_match_object_id(&start_id, NULL,
2820 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2821 if (error)
2822 goto done;
2825 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2826 if (view == NULL) {
2827 error = got_error_from_errno("view_open");
2828 goto done;
2830 error = open_log_view(view, start_id, repo, head_ref_name,
2831 in_repo_path, log_branches);
2832 if (error)
2833 goto done;
2834 if (worktree) {
2835 /* Release work tree lock. */
2836 got_worktree_close(worktree);
2837 worktree = NULL;
2839 error = view_loop(view);
2840 done:
2841 free(in_repo_path);
2842 free(repo_path);
2843 free(cwd);
2844 free(start_id);
2845 free(label);
2846 if (ref)
2847 got_ref_close(ref);
2848 if (repo) {
2849 const struct got_error *close_err = got_repo_close(repo);
2850 if (error == NULL)
2851 error = close_err;
2853 if (worktree)
2854 got_worktree_close(worktree);
2855 tog_free_refs();
2856 return error;
2859 __dead static void
2860 usage_diff(void)
2862 endwin();
2863 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2864 "[-w] object1 object2\n", getprogname());
2865 exit(1);
2868 static int
2869 match_line(const char *line, regex_t *regex, size_t nmatch,
2870 regmatch_t *regmatch)
2872 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2875 struct tog_color *
2876 match_color(struct tog_colors *colors, const char *line)
2878 struct tog_color *tc = NULL;
2880 STAILQ_FOREACH(tc, colors, entry) {
2881 if (match_line(line, &tc->regex, 0, NULL))
2882 return tc;
2885 return NULL;
2888 static const struct got_error *
2889 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2890 WINDOW *window, regmatch_t *regmatch)
2892 const struct got_error *err = NULL;
2893 wchar_t *wline;
2894 int width;
2895 char *s;
2897 *wtotal = 0;
2899 s = strndup(line, regmatch->rm_so);
2900 if (s == NULL)
2901 return got_error_from_errno("strndup");
2903 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2904 if (err) {
2905 free(s);
2906 return err;
2908 waddwstr(window, wline);
2909 free(wline);
2910 free(s);
2911 wlimit -= width;
2912 *wtotal += width;
2914 if (wlimit > 0) {
2915 s = strndup(line + regmatch->rm_so,
2916 regmatch->rm_eo - regmatch->rm_so);
2917 if (s == NULL) {
2918 err = got_error_from_errno("strndup");
2919 free(s);
2920 return err;
2922 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2923 if (err) {
2924 free(s);
2925 return err;
2927 wattr_on(window, A_STANDOUT, NULL);
2928 waddwstr(window, wline);
2929 wattr_off(window, A_STANDOUT, NULL);
2930 free(wline);
2931 free(s);
2932 wlimit -= width;
2933 *wtotal += width;
2936 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2937 err = format_line(&wline, &width,
2938 line + regmatch->rm_eo, wlimit, col_tab_align);
2939 if (err)
2940 return err;
2941 waddwstr(window, wline);
2942 free(wline);
2943 *wtotal += width;
2946 return NULL;
2949 static const struct got_error *
2950 draw_file(struct tog_view *view, const char *header)
2952 struct tog_diff_view_state *s = &view->state.diff;
2953 regmatch_t *regmatch = &view->regmatch;
2954 const struct got_error *err;
2955 int nprinted = 0;
2956 char *line;
2957 size_t linesize = 0;
2958 ssize_t linelen;
2959 struct tog_color *tc;
2960 wchar_t *wline;
2961 int width;
2962 int max_lines = view->nlines;
2963 int nlines = s->nlines;
2964 off_t line_offset;
2966 line_offset = s->line_offsets[s->first_displayed_line - 1];
2967 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2968 return got_error_from_errno("fseek");
2970 werase(view->window);
2972 if (header) {
2973 if (asprintf(&line, "[%d/%d] %s",
2974 s->first_displayed_line - 1 + s->selected_line, nlines,
2975 header) == -1)
2976 return got_error_from_errno("asprintf");
2977 err = format_line(&wline, &width, line, view->ncols, 0);
2978 free(line);
2979 if (err)
2980 return err;
2982 if (view_needs_focus_indication(view))
2983 wstandout(view->window);
2984 waddwstr(view->window, wline);
2985 free(wline);
2986 wline = NULL;
2987 if (view_needs_focus_indication(view))
2988 wstandend(view->window);
2989 if (width <= view->ncols - 1)
2990 waddch(view->window, '\n');
2992 if (max_lines <= 1)
2993 return NULL;
2994 max_lines--;
2997 s->eof = 0;
2998 line = NULL;
2999 while (max_lines > 0 && nprinted < max_lines) {
3000 linelen = getline(&line, &linesize, s->f);
3001 if (linelen == -1) {
3002 if (feof(s->f)) {
3003 s->eof = 1;
3004 break;
3006 free(line);
3007 return got_ferror(s->f, GOT_ERR_IO);
3010 tc = match_color(&s->colors, line);
3011 if (tc)
3012 wattr_on(view->window,
3013 COLOR_PAIR(tc->colorpair), NULL);
3014 if (s->first_displayed_line + nprinted == s->matched_line &&
3015 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3016 err = add_matched_line(&width, line, view->ncols, 0,
3017 view->window, regmatch);
3018 if (err) {
3019 free(line);
3020 return err;
3022 } else {
3023 err = format_line(&wline, &width, line, view->ncols, 0);
3024 if (err) {
3025 free(line);
3026 return err;
3028 waddwstr(view->window, wline);
3029 free(wline);
3030 wline = NULL;
3032 if (tc)
3033 wattr_off(view->window,
3034 COLOR_PAIR(tc->colorpair), NULL);
3035 if (width <= view->ncols - 1)
3036 waddch(view->window, '\n');
3037 nprinted++;
3039 free(line);
3040 if (nprinted >= 1)
3041 s->last_displayed_line = s->first_displayed_line +
3042 (nprinted - 1);
3043 else
3044 s->last_displayed_line = s->first_displayed_line;
3046 view_vborder(view);
3048 if (s->eof) {
3049 while (nprinted < view->nlines) {
3050 waddch(view->window, '\n');
3051 nprinted++;
3054 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3055 if (err) {
3056 return err;
3059 wstandout(view->window);
3060 waddwstr(view->window, wline);
3061 free(wline);
3062 wline = NULL;
3063 wstandend(view->window);
3066 return NULL;
3069 static char *
3070 get_datestr(time_t *time, char *datebuf)
3072 struct tm mytm, *tm;
3073 char *p, *s;
3075 tm = gmtime_r(time, &mytm);
3076 if (tm == NULL)
3077 return NULL;
3078 s = asctime_r(tm, datebuf);
3079 if (s == NULL)
3080 return NULL;
3081 p = strchr(s, '\n');
3082 if (p)
3083 *p = '\0';
3084 return s;
3087 static const struct got_error *
3088 get_changed_paths(struct got_pathlist_head *paths,
3089 struct got_commit_object *commit, struct got_repository *repo)
3091 const struct got_error *err = NULL;
3092 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3093 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3094 struct got_object_qid *qid;
3096 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3097 if (qid != NULL) {
3098 struct got_commit_object *pcommit;
3099 err = got_object_open_as_commit(&pcommit, repo,
3100 qid->id);
3101 if (err)
3102 return err;
3104 tree_id1 = got_object_id_dup(
3105 got_object_commit_get_tree_id(pcommit));
3106 if (tree_id1 == NULL) {
3107 got_object_commit_close(pcommit);
3108 return got_error_from_errno("got_object_id_dup");
3110 got_object_commit_close(pcommit);
3114 if (tree_id1) {
3115 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3116 if (err)
3117 goto done;
3120 tree_id2 = got_object_commit_get_tree_id(commit);
3121 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3122 if (err)
3123 goto done;
3125 err = got_diff_tree(tree1, tree2, "", "", repo,
3126 got_diff_tree_collect_changed_paths, paths, 0);
3127 done:
3128 if (tree1)
3129 got_object_tree_close(tree1);
3130 if (tree2)
3131 got_object_tree_close(tree2);
3132 free(tree_id1);
3133 return err;
3136 static const struct got_error *
3137 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3139 off_t *p;
3141 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3142 if (p == NULL)
3143 return got_error_from_errno("reallocarray");
3144 *line_offsets = p;
3145 (*line_offsets)[*nlines] = off;
3146 (*nlines)++;
3147 return NULL;
3150 static const struct got_error *
3151 write_commit_info(off_t **line_offsets, size_t *nlines,
3152 struct got_object_id *commit_id, struct got_reflist_head *refs,
3153 struct got_repository *repo, FILE *outfile)
3155 const struct got_error *err = NULL;
3156 char datebuf[26], *datestr;
3157 struct got_commit_object *commit;
3158 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3159 time_t committer_time;
3160 const char *author, *committer;
3161 char *refs_str = NULL;
3162 struct got_pathlist_head changed_paths;
3163 struct got_pathlist_entry *pe;
3164 off_t outoff = 0;
3165 int n;
3167 TAILQ_INIT(&changed_paths);
3169 if (refs) {
3170 err = build_refs_str(&refs_str, refs, commit_id, repo);
3171 if (err)
3172 return err;
3175 err = got_object_open_as_commit(&commit, repo, commit_id);
3176 if (err)
3177 return err;
3179 err = got_object_id_str(&id_str, commit_id);
3180 if (err) {
3181 err = got_error_from_errno("got_object_id_str");
3182 goto done;
3185 err = add_line_offset(line_offsets, nlines, 0);
3186 if (err)
3187 goto done;
3189 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3190 refs_str ? refs_str : "", refs_str ? ")" : "");
3191 if (n < 0) {
3192 err = got_error_from_errno("fprintf");
3193 goto done;
3195 outoff += n;
3196 err = add_line_offset(line_offsets, nlines, outoff);
3197 if (err)
3198 goto done;
3200 n = fprintf(outfile, "from: %s\n",
3201 got_object_commit_get_author(commit));
3202 if (n < 0) {
3203 err = got_error_from_errno("fprintf");
3204 goto done;
3206 outoff += n;
3207 err = add_line_offset(line_offsets, nlines, outoff);
3208 if (err)
3209 goto done;
3211 committer_time = got_object_commit_get_committer_time(commit);
3212 datestr = get_datestr(&committer_time, datebuf);
3213 if (datestr) {
3214 n = fprintf(outfile, "date: %s UTC\n", datestr);
3215 if (n < 0) {
3216 err = got_error_from_errno("fprintf");
3217 goto done;
3219 outoff += n;
3220 err = add_line_offset(line_offsets, nlines, outoff);
3221 if (err)
3222 goto done;
3224 author = got_object_commit_get_author(commit);
3225 committer = got_object_commit_get_committer(commit);
3226 if (strcmp(author, committer) != 0) {
3227 n = fprintf(outfile, "via: %s\n", committer);
3228 if (n < 0) {
3229 err = got_error_from_errno("fprintf");
3230 goto done;
3232 outoff += n;
3233 err = add_line_offset(line_offsets, nlines, outoff);
3234 if (err)
3235 goto done;
3237 if (got_object_commit_get_nparents(commit) > 1) {
3238 const struct got_object_id_queue *parent_ids;
3239 struct got_object_qid *qid;
3240 int pn = 1;
3241 parent_ids = got_object_commit_get_parent_ids(commit);
3242 STAILQ_FOREACH(qid, parent_ids, entry) {
3243 err = got_object_id_str(&id_str, qid->id);
3244 if (err)
3245 goto done;
3246 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3247 if (n < 0) {
3248 err = got_error_from_errno("fprintf");
3249 goto done;
3251 outoff += n;
3252 err = add_line_offset(line_offsets, nlines, outoff);
3253 if (err)
3254 goto done;
3255 free(id_str);
3256 id_str = NULL;
3260 err = got_object_commit_get_logmsg(&logmsg, commit);
3261 if (err)
3262 goto done;
3263 s = logmsg;
3264 while ((line = strsep(&s, "\n")) != NULL) {
3265 n = fprintf(outfile, "%s\n", line);
3266 if (n < 0) {
3267 err = got_error_from_errno("fprintf");
3268 goto done;
3270 outoff += n;
3271 err = add_line_offset(line_offsets, nlines, outoff);
3272 if (err)
3273 goto done;
3276 err = get_changed_paths(&changed_paths, commit, repo);
3277 if (err)
3278 goto done;
3279 TAILQ_FOREACH(pe, &changed_paths, entry) {
3280 struct got_diff_changed_path *cp = pe->data;
3281 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3282 if (n < 0) {
3283 err = got_error_from_errno("fprintf");
3284 goto done;
3286 outoff += n;
3287 err = add_line_offset(line_offsets, nlines, outoff);
3288 if (err)
3289 goto done;
3290 free((char *)pe->path);
3291 free(pe->data);
3294 fputc('\n', outfile);
3295 outoff++;
3296 err = add_line_offset(line_offsets, nlines, outoff);
3297 done:
3298 got_pathlist_free(&changed_paths);
3299 free(id_str);
3300 free(logmsg);
3301 free(refs_str);
3302 got_object_commit_close(commit);
3303 if (err) {
3304 free(*line_offsets);
3305 *line_offsets = NULL;
3306 *nlines = 0;
3308 return err;
3311 static const struct got_error *
3312 create_diff(struct tog_diff_view_state *s)
3314 const struct got_error *err = NULL;
3315 FILE *f = NULL;
3316 int obj_type;
3318 free(s->line_offsets);
3319 s->line_offsets = malloc(sizeof(off_t));
3320 if (s->line_offsets == NULL)
3321 return got_error_from_errno("malloc");
3322 s->nlines = 0;
3324 f = got_opentemp();
3325 if (f == NULL) {
3326 err = got_error_from_errno("got_opentemp");
3327 goto done;
3329 if (s->f && fclose(s->f) == EOF) {
3330 err = got_error_from_errno("fclose");
3331 goto done;
3333 s->f = f;
3335 if (s->id1)
3336 err = got_object_get_type(&obj_type, s->repo, s->id1);
3337 else
3338 err = got_object_get_type(&obj_type, s->repo, s->id2);
3339 if (err)
3340 goto done;
3342 switch (obj_type) {
3343 case GOT_OBJ_TYPE_BLOB:
3344 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3345 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3346 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3347 break;
3348 case GOT_OBJ_TYPE_TREE:
3349 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3350 s->id1, s->id2, "", "", s->diff_context,
3351 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3352 break;
3353 case GOT_OBJ_TYPE_COMMIT: {
3354 const struct got_object_id_queue *parent_ids;
3355 struct got_object_qid *pid;
3356 struct got_commit_object *commit2;
3357 struct got_reflist_head *refs;
3359 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3360 if (err)
3361 goto done;
3362 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3363 /* Show commit info if we're diffing to a parent/root commit. */
3364 if (s->id1 == NULL) {
3365 err = write_commit_info(&s->line_offsets, &s->nlines,
3366 s->id2, refs, s->repo, s->f);
3367 if (err)
3368 goto done;
3369 } else {
3370 parent_ids = got_object_commit_get_parent_ids(commit2);
3371 STAILQ_FOREACH(pid, parent_ids, entry) {
3372 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3373 err = write_commit_info(
3374 &s->line_offsets, &s->nlines,
3375 s->id2, refs, s->repo, s->f);
3376 if (err)
3377 goto done;
3378 break;
3382 got_object_commit_close(commit2);
3384 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3385 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3386 s->force_text_diff, s->repo, s->f);
3387 break;
3389 default:
3390 err = got_error(GOT_ERR_OBJ_TYPE);
3391 break;
3393 if (err)
3394 goto done;
3395 done:
3396 if (s->f && fflush(s->f) != 0 && err == NULL)
3397 err = got_error_from_errno("fflush");
3398 return err;
3401 static void
3402 diff_view_indicate_progress(struct tog_view *view)
3404 mvwaddstr(view->window, 0, 0, "diffing...");
3405 update_panels();
3406 doupdate();
3409 static const struct got_error *
3410 search_start_diff_view(struct tog_view *view)
3412 struct tog_diff_view_state *s = &view->state.diff;
3414 s->matched_line = 0;
3415 return NULL;
3418 static const struct got_error *
3419 search_next_diff_view(struct tog_view *view)
3421 struct tog_diff_view_state *s = &view->state.diff;
3422 int lineno;
3423 char *line = NULL;
3424 size_t linesize = 0;
3425 ssize_t linelen;
3427 if (!view->searching) {
3428 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3429 return NULL;
3432 if (s->matched_line) {
3433 if (view->searching == TOG_SEARCH_FORWARD)
3434 lineno = s->matched_line + 1;
3435 else
3436 lineno = s->matched_line - 1;
3437 } else {
3438 if (view->searching == TOG_SEARCH_FORWARD)
3439 lineno = 1;
3440 else
3441 lineno = s->nlines;
3444 while (1) {
3445 off_t offset;
3447 if (lineno <= 0 || lineno > s->nlines) {
3448 if (s->matched_line == 0) {
3449 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3450 break;
3453 if (view->searching == TOG_SEARCH_FORWARD)
3454 lineno = 1;
3455 else
3456 lineno = s->nlines;
3459 offset = s->line_offsets[lineno - 1];
3460 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3461 free(line);
3462 return got_error_from_errno("fseeko");
3464 linelen = getline(&line, &linesize, s->f);
3465 if (linelen != -1 &&
3466 match_line(line, &view->regex, 1, &view->regmatch)) {
3467 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3468 s->matched_line = lineno;
3469 break;
3471 if (view->searching == TOG_SEARCH_FORWARD)
3472 lineno++;
3473 else
3474 lineno--;
3476 free(line);
3478 if (s->matched_line) {
3479 s->first_displayed_line = s->matched_line;
3480 s->selected_line = 1;
3483 return NULL;
3486 static const struct got_error *
3487 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3488 struct got_object_id *id2, const char *label1, const char *label2,
3489 int diff_context, int ignore_whitespace, int force_text_diff,
3490 struct tog_view *log_view, struct got_repository *repo)
3492 const struct got_error *err;
3493 struct tog_diff_view_state *s = &view->state.diff;
3495 if (id1 != NULL && id2 != NULL) {
3496 int type1, type2;
3497 err = got_object_get_type(&type1, repo, id1);
3498 if (err)
3499 return err;
3500 err = got_object_get_type(&type2, repo, id2);
3501 if (err)
3502 return err;
3504 if (type1 != type2)
3505 return got_error(GOT_ERR_OBJ_TYPE);
3507 s->first_displayed_line = 1;
3508 s->last_displayed_line = view->nlines;
3509 s->selected_line = 1;
3510 s->repo = repo;
3511 s->id1 = id1;
3512 s->id2 = id2;
3513 s->label1 = label1;
3514 s->label2 = label2;
3516 if (id1) {
3517 s->id1 = got_object_id_dup(id1);
3518 if (s->id1 == NULL)
3519 return got_error_from_errno("got_object_id_dup");
3520 } else
3521 s->id1 = NULL;
3523 s->id2 = got_object_id_dup(id2);
3524 if (s->id2 == NULL) {
3525 free(s->id1);
3526 s->id1 = NULL;
3527 return got_error_from_errno("got_object_id_dup");
3529 s->f = NULL;
3530 s->first_displayed_line = 1;
3531 s->last_displayed_line = view->nlines;
3532 s->diff_context = diff_context;
3533 s->ignore_whitespace = ignore_whitespace;
3534 s->force_text_diff = force_text_diff;
3535 s->log_view = log_view;
3536 s->repo = repo;
3538 STAILQ_INIT(&s->colors);
3539 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3540 err = add_color(&s->colors,
3541 "^-", TOG_COLOR_DIFF_MINUS,
3542 get_color_value("TOG_COLOR_DIFF_MINUS"));
3543 if (err)
3544 return err;
3545 err = add_color(&s->colors, "^\\+",
3546 TOG_COLOR_DIFF_PLUS,
3547 get_color_value("TOG_COLOR_DIFF_PLUS"));
3548 if (err) {
3549 free_colors(&s->colors);
3550 return err;
3552 err = add_color(&s->colors,
3553 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3554 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3555 if (err) {
3556 free_colors(&s->colors);
3557 return err;
3560 err = add_color(&s->colors,
3561 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3562 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3563 get_color_value("TOG_COLOR_DIFF_META"));
3564 if (err) {
3565 free_colors(&s->colors);
3566 return err;
3569 err = add_color(&s->colors,
3570 "^(from|via): ", TOG_COLOR_AUTHOR,
3571 get_color_value("TOG_COLOR_AUTHOR"));
3572 if (err) {
3573 free_colors(&s->colors);
3574 return err;
3577 err = add_color(&s->colors,
3578 "^date: ", TOG_COLOR_DATE,
3579 get_color_value("TOG_COLOR_DATE"));
3580 if (err) {
3581 free_colors(&s->colors);
3582 return err;
3586 if (log_view && view_is_splitscreen(view))
3587 show_log_view(log_view); /* draw vborder */
3588 diff_view_indicate_progress(view);
3590 s->line_offsets = NULL;
3591 s->nlines = 0;
3592 err = create_diff(s);
3593 if (err) {
3594 free(s->id1);
3595 s->id1 = NULL;
3596 free(s->id2);
3597 s->id2 = NULL;
3598 free_colors(&s->colors);
3599 return err;
3602 view->show = show_diff_view;
3603 view->input = input_diff_view;
3604 view->close = close_diff_view;
3605 view->search_start = search_start_diff_view;
3606 view->search_next = search_next_diff_view;
3608 return NULL;
3611 static const struct got_error *
3612 close_diff_view(struct tog_view *view)
3614 const struct got_error *err = NULL;
3615 struct tog_diff_view_state *s = &view->state.diff;
3617 free(s->id1);
3618 s->id1 = NULL;
3619 free(s->id2);
3620 s->id2 = NULL;
3621 if (s->f && fclose(s->f) == EOF)
3622 err = got_error_from_errno("fclose");
3623 free_colors(&s->colors);
3624 free(s->line_offsets);
3625 s->line_offsets = NULL;
3626 s->nlines = 0;
3627 return err;
3630 static const struct got_error *
3631 show_diff_view(struct tog_view *view)
3633 const struct got_error *err;
3634 struct tog_diff_view_state *s = &view->state.diff;
3635 char *id_str1 = NULL, *id_str2, *header;
3636 const char *label1, *label2;
3638 if (s->id1) {
3639 err = got_object_id_str(&id_str1, s->id1);
3640 if (err)
3641 return err;
3642 label1 = s->label1 ? : id_str1;
3643 } else
3644 label1 = "/dev/null";
3646 err = got_object_id_str(&id_str2, s->id2);
3647 if (err)
3648 return err;
3649 label2 = s->label2 ? : id_str2;
3651 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3652 err = got_error_from_errno("asprintf");
3653 free(id_str1);
3654 free(id_str2);
3655 return err;
3657 free(id_str1);
3658 free(id_str2);
3660 err = draw_file(view, header);
3661 free(header);
3662 return err;
3665 static const struct got_error *
3666 set_selected_commit(struct tog_diff_view_state *s,
3667 struct commit_queue_entry *entry)
3669 const struct got_error *err;
3670 const struct got_object_id_queue *parent_ids;
3671 struct got_commit_object *selected_commit;
3672 struct got_object_qid *pid;
3674 free(s->id2);
3675 s->id2 = got_object_id_dup(entry->id);
3676 if (s->id2 == NULL)
3677 return got_error_from_errno("got_object_id_dup");
3679 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3680 if (err)
3681 return err;
3682 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3683 free(s->id1);
3684 pid = STAILQ_FIRST(parent_ids);
3685 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3686 got_object_commit_close(selected_commit);
3687 return NULL;
3690 static const struct got_error *
3691 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3693 const struct got_error *err = NULL;
3694 struct tog_diff_view_state *s = &view->state.diff;
3695 struct tog_log_view_state *ls;
3696 struct commit_queue_entry *old_selected_entry;
3697 char *line = NULL;
3698 size_t linesize = 0;
3699 ssize_t linelen;
3700 int i;
3702 switch (ch) {
3703 case 'a':
3704 case 'w':
3705 if (ch == 'a')
3706 s->force_text_diff = !s->force_text_diff;
3707 if (ch == 'w')
3708 s->ignore_whitespace = !s->ignore_whitespace;
3709 wclear(view->window);
3710 s->first_displayed_line = 1;
3711 s->last_displayed_line = view->nlines;
3712 diff_view_indicate_progress(view);
3713 err = create_diff(s);
3714 break;
3715 case 'g':
3716 case KEY_HOME:
3717 s->first_displayed_line = 1;
3718 break;
3719 case 'G':
3720 case KEY_END:
3721 if (s->eof)
3722 break;
3724 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3725 s->eof = 1;
3726 break;
3727 case 'k':
3728 case KEY_UP:
3729 if (s->first_displayed_line > 1)
3730 s->first_displayed_line--;
3731 break;
3732 case KEY_PPAGE:
3733 case CTRL('b'):
3734 if (s->first_displayed_line == 1)
3735 break;
3736 i = 0;
3737 while (i++ < view->nlines - 1 &&
3738 s->first_displayed_line > 1)
3739 s->first_displayed_line--;
3740 break;
3741 case 'j':
3742 case KEY_DOWN:
3743 if (!s->eof)
3744 s->first_displayed_line++;
3745 break;
3746 case KEY_NPAGE:
3747 case CTRL('f'):
3748 case ' ':
3749 if (s->eof)
3750 break;
3751 i = 0;
3752 while (!s->eof && i++ < view->nlines - 1) {
3753 linelen = getline(&line, &linesize, s->f);
3754 s->first_displayed_line++;
3755 if (linelen == -1) {
3756 if (feof(s->f)) {
3757 s->eof = 1;
3758 } else
3759 err = got_ferror(s->f, GOT_ERR_IO);
3760 break;
3763 free(line);
3764 break;
3765 case '[':
3766 if (s->diff_context > 0) {
3767 s->diff_context--;
3768 diff_view_indicate_progress(view);
3769 err = create_diff(s);
3770 if (s->first_displayed_line + view->nlines - 1 >
3771 s->nlines) {
3772 s->first_displayed_line = 1;
3773 s->last_displayed_line = view->nlines;
3776 break;
3777 case ']':
3778 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3779 s->diff_context++;
3780 diff_view_indicate_progress(view);
3781 err = create_diff(s);
3783 break;
3784 case '<':
3785 case ',':
3786 if (s->log_view == NULL)
3787 break;
3788 ls = &s->log_view->state.log;
3789 old_selected_entry = ls->selected_entry;
3791 err = input_log_view(NULL, s->log_view, KEY_UP);
3792 if (err)
3793 break;
3795 if (old_selected_entry == ls->selected_entry)
3796 break;
3798 err = set_selected_commit(s, ls->selected_entry);
3799 if (err)
3800 break;
3802 s->first_displayed_line = 1;
3803 s->last_displayed_line = view->nlines;
3805 diff_view_indicate_progress(view);
3806 err = create_diff(s);
3807 break;
3808 case '>':
3809 case '.':
3810 if (s->log_view == NULL)
3811 break;
3812 ls = &s->log_view->state.log;
3813 old_selected_entry = ls->selected_entry;
3815 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3816 if (err)
3817 break;
3819 if (old_selected_entry == ls->selected_entry)
3820 break;
3822 err = set_selected_commit(s, ls->selected_entry);
3823 if (err)
3824 break;
3826 s->first_displayed_line = 1;
3827 s->last_displayed_line = view->nlines;
3829 diff_view_indicate_progress(view);
3830 err = create_diff(s);
3831 break;
3832 default:
3833 break;
3836 return err;
3839 static const struct got_error *
3840 cmd_diff(int argc, char *argv[])
3842 const struct got_error *error = NULL;
3843 struct got_repository *repo = NULL;
3844 struct got_worktree *worktree = NULL;
3845 struct got_object_id *id1 = NULL, *id2 = NULL;
3846 char *repo_path = NULL, *cwd = NULL;
3847 char *id_str1 = NULL, *id_str2 = NULL;
3848 char *label1 = NULL, *label2 = NULL;
3849 int diff_context = 3, ignore_whitespace = 0;
3850 int ch, force_text_diff = 0;
3851 const char *errstr;
3852 struct tog_view *view;
3854 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3855 switch (ch) {
3856 case 'a':
3857 force_text_diff = 1;
3858 break;
3859 case 'C':
3860 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3861 &errstr);
3862 if (errstr != NULL)
3863 err(1, "-C option %s", errstr);
3864 break;
3865 case 'r':
3866 repo_path = realpath(optarg, NULL);
3867 if (repo_path == NULL)
3868 return got_error_from_errno2("realpath",
3869 optarg);
3870 got_path_strip_trailing_slashes(repo_path);
3871 break;
3872 case 'w':
3873 ignore_whitespace = 1;
3874 break;
3875 default:
3876 usage_diff();
3877 /* NOTREACHED */
3881 argc -= optind;
3882 argv += optind;
3884 if (argc == 0) {
3885 usage_diff(); /* TODO show local worktree changes */
3886 } else if (argc == 2) {
3887 id_str1 = argv[0];
3888 id_str2 = argv[1];
3889 } else
3890 usage_diff();
3892 if (repo_path == NULL) {
3893 cwd = getcwd(NULL, 0);
3894 if (cwd == NULL)
3895 return got_error_from_errno("getcwd");
3896 error = got_worktree_open(&worktree, cwd);
3897 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3898 goto done;
3899 if (worktree)
3900 repo_path =
3901 strdup(got_worktree_get_repo_path(worktree));
3902 else
3903 repo_path = strdup(cwd);
3904 if (repo_path == NULL) {
3905 error = got_error_from_errno("strdup");
3906 goto done;
3910 error = got_repo_open(&repo, repo_path, NULL);
3911 if (error)
3912 goto done;
3914 init_curses();
3916 error = apply_unveil(got_repo_get_path(repo), NULL);
3917 if (error)
3918 goto done;
3920 error = tog_load_refs(repo);
3921 if (error)
3922 goto done;
3924 error = got_repo_match_object_id(&id1, &label1, id_str1,
3925 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3926 if (error)
3927 goto done;
3929 error = got_repo_match_object_id(&id2, &label2, id_str2,
3930 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3931 if (error)
3932 goto done;
3934 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3935 if (view == NULL) {
3936 error = got_error_from_errno("view_open");
3937 goto done;
3939 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3940 ignore_whitespace, force_text_diff, NULL, repo);
3941 if (error)
3942 goto done;
3943 error = view_loop(view);
3944 done:
3945 free(label1);
3946 free(label2);
3947 free(repo_path);
3948 free(cwd);
3949 if (repo) {
3950 const struct got_error *close_err = got_repo_close(repo);
3951 if (error == NULL)
3952 error = close_err;
3954 if (worktree)
3955 got_worktree_close(worktree);
3956 tog_free_refs();
3957 return error;
3960 __dead static void
3961 usage_blame(void)
3963 endwin();
3964 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3965 getprogname());
3966 exit(1);
3969 struct tog_blame_line {
3970 int annotated;
3971 struct got_object_id *id;
3974 static const struct got_error *
3975 draw_blame(struct tog_view *view)
3977 struct tog_blame_view_state *s = &view->state.blame;
3978 struct tog_blame *blame = &s->blame;
3979 regmatch_t *regmatch = &view->regmatch;
3980 const struct got_error *err;
3981 int lineno = 0, nprinted = 0;
3982 char *line = NULL;
3983 size_t linesize = 0;
3984 ssize_t linelen;
3985 wchar_t *wline;
3986 int width;
3987 struct tog_blame_line *blame_line;
3988 struct got_object_id *prev_id = NULL;
3989 char *id_str;
3990 struct tog_color *tc;
3992 err = got_object_id_str(&id_str, s->blamed_commit->id);
3993 if (err)
3994 return err;
3996 rewind(blame->f);
3997 werase(view->window);
3999 if (asprintf(&line, "commit %s", id_str) == -1) {
4000 err = got_error_from_errno("asprintf");
4001 free(id_str);
4002 return err;
4005 err = format_line(&wline, &width, line, view->ncols, 0);
4006 free(line);
4007 line = NULL;
4008 if (err)
4009 return err;
4010 if (view_needs_focus_indication(view))
4011 wstandout(view->window);
4012 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4013 if (tc)
4014 wattr_on(view->window,
4015 COLOR_PAIR(tc->colorpair), NULL);
4016 waddwstr(view->window, wline);
4017 if (tc)
4018 wattr_off(view->window,
4019 COLOR_PAIR(tc->colorpair), NULL);
4020 if (view_needs_focus_indication(view))
4021 wstandend(view->window);
4022 free(wline);
4023 wline = NULL;
4024 if (width < view->ncols - 1)
4025 waddch(view->window, '\n');
4027 if (asprintf(&line, "[%d/%d] %s%s",
4028 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4029 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4030 free(id_str);
4031 return got_error_from_errno("asprintf");
4033 free(id_str);
4034 err = format_line(&wline, &width, line, view->ncols, 0);
4035 free(line);
4036 line = NULL;
4037 if (err)
4038 return err;
4039 waddwstr(view->window, wline);
4040 free(wline);
4041 wline = NULL;
4042 if (width < view->ncols - 1)
4043 waddch(view->window, '\n');
4045 s->eof = 0;
4046 while (nprinted < view->nlines - 2) {
4047 linelen = getline(&line, &linesize, blame->f);
4048 if (linelen == -1) {
4049 if (feof(blame->f)) {
4050 s->eof = 1;
4051 break;
4053 free(line);
4054 return got_ferror(blame->f, GOT_ERR_IO);
4056 if (++lineno < s->first_displayed_line)
4057 continue;
4059 if (view->focussed && nprinted == s->selected_line - 1)
4060 wstandout(view->window);
4062 if (blame->nlines > 0) {
4063 blame_line = &blame->lines[lineno - 1];
4064 if (blame_line->annotated && prev_id &&
4065 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4066 !(view->focussed &&
4067 nprinted == s->selected_line - 1)) {
4068 waddstr(view->window, " ");
4069 } else if (blame_line->annotated) {
4070 char *id_str;
4071 err = got_object_id_str(&id_str, blame_line->id);
4072 if (err) {
4073 free(line);
4074 return err;
4076 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4077 if (tc)
4078 wattr_on(view->window,
4079 COLOR_PAIR(tc->colorpair), NULL);
4080 wprintw(view->window, "%.8s", id_str);
4081 if (tc)
4082 wattr_off(view->window,
4083 COLOR_PAIR(tc->colorpair), NULL);
4084 free(id_str);
4085 prev_id = blame_line->id;
4086 } else {
4087 waddstr(view->window, "........");
4088 prev_id = NULL;
4090 } else {
4091 waddstr(view->window, "........");
4092 prev_id = NULL;
4095 if (view->focussed && nprinted == s->selected_line - 1)
4096 wstandend(view->window);
4097 waddstr(view->window, " ");
4099 if (view->ncols <= 9) {
4100 width = 9;
4101 wline = wcsdup(L"");
4102 if (wline == NULL) {
4103 err = got_error_from_errno("wcsdup");
4104 free(line);
4105 return err;
4107 } else if (s->first_displayed_line + nprinted ==
4108 s->matched_line &&
4109 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4110 err = add_matched_line(&width, line, view->ncols - 9, 9,
4111 view->window, regmatch);
4112 if (err) {
4113 free(line);
4114 return err;
4116 width += 9;
4117 } else {
4118 err = format_line(&wline, &width, line,
4119 view->ncols - 9, 9);
4120 waddwstr(view->window, wline);
4121 free(wline);
4122 wline = NULL;
4123 width += 9;
4126 if (width <= view->ncols - 1)
4127 waddch(view->window, '\n');
4128 if (++nprinted == 1)
4129 s->first_displayed_line = lineno;
4131 free(line);
4132 s->last_displayed_line = lineno;
4134 view_vborder(view);
4136 return NULL;
4139 static const struct got_error *
4140 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4142 const struct got_error *err = NULL;
4143 struct tog_blame_cb_args *a = arg;
4144 struct tog_blame_line *line;
4145 int errcode;
4147 if (nlines != a->nlines ||
4148 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4149 return got_error(GOT_ERR_RANGE);
4151 errcode = pthread_mutex_lock(&tog_mutex);
4152 if (errcode)
4153 return got_error_set_errno(errcode, "pthread_mutex_lock");
4155 if (*a->quit) { /* user has quit the blame view */
4156 err = got_error(GOT_ERR_ITER_COMPLETED);
4157 goto done;
4160 if (lineno == -1)
4161 goto done; /* no change in this commit */
4163 line = &a->lines[lineno - 1];
4164 if (line->annotated)
4165 goto done;
4167 line->id = got_object_id_dup(id);
4168 if (line->id == NULL) {
4169 err = got_error_from_errno("got_object_id_dup");
4170 goto done;
4172 line->annotated = 1;
4173 done:
4174 errcode = pthread_mutex_unlock(&tog_mutex);
4175 if (errcode)
4176 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4177 return err;
4180 static void *
4181 blame_thread(void *arg)
4183 const struct got_error *err, *close_err;
4184 struct tog_blame_thread_args *ta = arg;
4185 struct tog_blame_cb_args *a = ta->cb_args;
4186 int errcode;
4188 err = block_signals_used_by_main_thread();
4189 if (err)
4190 return (void *)err;
4192 err = got_blame(ta->path, a->commit_id, ta->repo,
4193 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4194 if (err && err->code == GOT_ERR_CANCELLED)
4195 err = NULL;
4197 errcode = pthread_mutex_lock(&tog_mutex);
4198 if (errcode)
4199 return (void *)got_error_set_errno(errcode,
4200 "pthread_mutex_lock");
4202 close_err = got_repo_close(ta->repo);
4203 if (err == NULL)
4204 err = close_err;
4205 ta->repo = NULL;
4206 *ta->complete = 1;
4208 errcode = pthread_mutex_unlock(&tog_mutex);
4209 if (errcode && err == NULL)
4210 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4212 return (void *)err;
4215 static struct got_object_id *
4216 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4217 int first_displayed_line, int selected_line)
4219 struct tog_blame_line *line;
4221 if (nlines <= 0)
4222 return NULL;
4224 line = &lines[first_displayed_line - 1 + selected_line - 1];
4225 if (!line->annotated)
4226 return NULL;
4228 return line->id;
4231 static const struct got_error *
4232 stop_blame(struct tog_blame *blame)
4234 const struct got_error *err = NULL;
4235 int i;
4237 if (blame->thread) {
4238 int errcode;
4239 errcode = pthread_mutex_unlock(&tog_mutex);
4240 if (errcode)
4241 return got_error_set_errno(errcode,
4242 "pthread_mutex_unlock");
4243 errcode = pthread_join(blame->thread, (void **)&err);
4244 if (errcode)
4245 return got_error_set_errno(errcode, "pthread_join");
4246 errcode = pthread_mutex_lock(&tog_mutex);
4247 if (errcode)
4248 return got_error_set_errno(errcode,
4249 "pthread_mutex_lock");
4250 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4251 err = NULL;
4252 blame->thread = 0; //NULL;
4254 if (blame->thread_args.repo) {
4255 const struct got_error *close_err;
4256 close_err = got_repo_close(blame->thread_args.repo);
4257 if (err == NULL)
4258 err = close_err;
4259 blame->thread_args.repo = NULL;
4261 if (blame->f) {
4262 if (fclose(blame->f) == EOF && err == NULL)
4263 err = got_error_from_errno("fclose");
4264 blame->f = NULL;
4266 if (blame->lines) {
4267 for (i = 0; i < blame->nlines; i++)
4268 free(blame->lines[i].id);
4269 free(blame->lines);
4270 blame->lines = NULL;
4272 free(blame->cb_args.commit_id);
4273 blame->cb_args.commit_id = NULL;
4275 return err;
4278 static const struct got_error *
4279 cancel_blame_view(void *arg)
4281 const struct got_error *err = NULL;
4282 int *done = arg;
4283 int errcode;
4285 errcode = pthread_mutex_lock(&tog_mutex);
4286 if (errcode)
4287 return got_error_set_errno(errcode,
4288 "pthread_mutex_unlock");
4290 if (*done)
4291 err = got_error(GOT_ERR_CANCELLED);
4293 errcode = pthread_mutex_unlock(&tog_mutex);
4294 if (errcode)
4295 return got_error_set_errno(errcode,
4296 "pthread_mutex_lock");
4298 return err;
4301 static const struct got_error *
4302 run_blame(struct tog_view *view)
4304 struct tog_blame_view_state *s = &view->state.blame;
4305 struct tog_blame *blame = &s->blame;
4306 const struct got_error *err = NULL;
4307 struct got_blob_object *blob = NULL;
4308 struct got_repository *thread_repo = NULL;
4309 struct got_object_id *obj_id = NULL;
4310 int obj_type;
4312 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4313 s->path);
4314 if (err)
4315 return err;
4317 err = got_object_get_type(&obj_type, s->repo, obj_id);
4318 if (err)
4319 goto done;
4321 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4322 err = got_error(GOT_ERR_OBJ_TYPE);
4323 goto done;
4326 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4327 if (err)
4328 goto done;
4329 blame->f = got_opentemp();
4330 if (blame->f == NULL) {
4331 err = got_error_from_errno("got_opentemp");
4332 goto done;
4334 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4335 &blame->line_offsets, blame->f, blob);
4336 if (err)
4337 goto done;
4338 if (blame->nlines == 0) {
4339 s->blame_complete = 1;
4340 goto done;
4343 /* Don't include \n at EOF in the blame line count. */
4344 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4345 blame->nlines--;
4347 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4348 if (blame->lines == NULL) {
4349 err = got_error_from_errno("calloc");
4350 goto done;
4353 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4354 if (err)
4355 goto done;
4357 blame->cb_args.view = view;
4358 blame->cb_args.lines = blame->lines;
4359 blame->cb_args.nlines = blame->nlines;
4360 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4361 if (blame->cb_args.commit_id == NULL) {
4362 err = got_error_from_errno("got_object_id_dup");
4363 goto done;
4365 blame->cb_args.quit = &s->done;
4367 blame->thread_args.path = s->path;
4368 blame->thread_args.repo = thread_repo;
4369 blame->thread_args.cb_args = &blame->cb_args;
4370 blame->thread_args.complete = &s->blame_complete;
4371 blame->thread_args.cancel_cb = cancel_blame_view;
4372 blame->thread_args.cancel_arg = &s->done;
4373 s->blame_complete = 0;
4375 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4376 s->first_displayed_line = 1;
4377 s->last_displayed_line = view->nlines;
4378 s->selected_line = 1;
4381 done:
4382 if (blob)
4383 got_object_blob_close(blob);
4384 free(obj_id);
4385 if (err)
4386 stop_blame(blame);
4387 return err;
4390 static const struct got_error *
4391 open_blame_view(struct tog_view *view, char *path,
4392 struct got_object_id *commit_id, struct got_repository *repo)
4394 const struct got_error *err = NULL;
4395 struct tog_blame_view_state *s = &view->state.blame;
4397 STAILQ_INIT(&s->blamed_commits);
4399 s->path = strdup(path);
4400 if (s->path == NULL)
4401 return got_error_from_errno("strdup");
4403 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4404 if (err) {
4405 free(s->path);
4406 return err;
4409 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4410 s->first_displayed_line = 1;
4411 s->last_displayed_line = view->nlines;
4412 s->selected_line = 1;
4413 s->blame_complete = 0;
4414 s->repo = repo;
4415 s->commit_id = commit_id;
4416 memset(&s->blame, 0, sizeof(s->blame));
4418 STAILQ_INIT(&s->colors);
4419 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4420 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4421 get_color_value("TOG_COLOR_COMMIT"));
4422 if (err)
4423 return err;
4426 view->show = show_blame_view;
4427 view->input = input_blame_view;
4428 view->close = close_blame_view;
4429 view->search_start = search_start_blame_view;
4430 view->search_next = search_next_blame_view;
4432 return run_blame(view);
4435 static const struct got_error *
4436 close_blame_view(struct tog_view *view)
4438 const struct got_error *err = NULL;
4439 struct tog_blame_view_state *s = &view->state.blame;
4441 if (s->blame.thread)
4442 err = stop_blame(&s->blame);
4444 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4445 struct got_object_qid *blamed_commit;
4446 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4447 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4448 got_object_qid_free(blamed_commit);
4451 free(s->path);
4452 free_colors(&s->colors);
4454 return err;
4457 static const struct got_error *
4458 search_start_blame_view(struct tog_view *view)
4460 struct tog_blame_view_state *s = &view->state.blame;
4462 s->matched_line = 0;
4463 return NULL;
4466 static const struct got_error *
4467 search_next_blame_view(struct tog_view *view)
4469 struct tog_blame_view_state *s = &view->state.blame;
4470 int lineno;
4471 char *line = NULL;
4472 size_t linesize = 0;
4473 ssize_t linelen;
4475 if (!view->searching) {
4476 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4477 return NULL;
4480 if (s->matched_line) {
4481 if (view->searching == TOG_SEARCH_FORWARD)
4482 lineno = s->matched_line + 1;
4483 else
4484 lineno = s->matched_line - 1;
4485 } else {
4486 if (view->searching == TOG_SEARCH_FORWARD)
4487 lineno = 1;
4488 else
4489 lineno = s->blame.nlines;
4492 while (1) {
4493 off_t offset;
4495 if (lineno <= 0 || lineno > s->blame.nlines) {
4496 if (s->matched_line == 0) {
4497 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4498 break;
4501 if (view->searching == TOG_SEARCH_FORWARD)
4502 lineno = 1;
4503 else
4504 lineno = s->blame.nlines;
4507 offset = s->blame.line_offsets[lineno - 1];
4508 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4509 free(line);
4510 return got_error_from_errno("fseeko");
4512 linelen = getline(&line, &linesize, s->blame.f);
4513 if (linelen != -1 &&
4514 match_line(line, &view->regex, 1, &view->regmatch)) {
4515 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4516 s->matched_line = lineno;
4517 break;
4519 if (view->searching == TOG_SEARCH_FORWARD)
4520 lineno++;
4521 else
4522 lineno--;
4524 free(line);
4526 if (s->matched_line) {
4527 s->first_displayed_line = s->matched_line;
4528 s->selected_line = 1;
4531 return NULL;
4534 static const struct got_error *
4535 show_blame_view(struct tog_view *view)
4537 const struct got_error *err = NULL;
4538 struct tog_blame_view_state *s = &view->state.blame;
4539 int errcode;
4541 if (s->blame.thread == 0 && !s->blame_complete) {
4542 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4543 &s->blame.thread_args);
4544 if (errcode)
4545 return got_error_set_errno(errcode, "pthread_create");
4547 halfdelay(1); /* fast refresh while annotating */
4550 if (s->blame_complete)
4551 halfdelay(10); /* disable fast refresh */
4553 err = draw_blame(view);
4555 view_vborder(view);
4556 return err;
4559 static const struct got_error *
4560 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4562 const struct got_error *err = NULL, *thread_err = NULL;
4563 struct tog_view *diff_view;
4564 struct tog_blame_view_state *s = &view->state.blame;
4565 int begin_x = 0;
4567 switch (ch) {
4568 case 'q':
4569 s->done = 1;
4570 break;
4571 case 'g':
4572 case KEY_HOME:
4573 s->selected_line = 1;
4574 s->first_displayed_line = 1;
4575 break;
4576 case 'G':
4577 case KEY_END:
4578 if (s->blame.nlines < view->nlines - 2) {
4579 s->selected_line = s->blame.nlines;
4580 s->first_displayed_line = 1;
4581 } else {
4582 s->selected_line = view->nlines - 2;
4583 s->first_displayed_line = s->blame.nlines -
4584 (view->nlines - 3);
4586 break;
4587 case 'k':
4588 case KEY_UP:
4589 if (s->selected_line > 1)
4590 s->selected_line--;
4591 else if (s->selected_line == 1 &&
4592 s->first_displayed_line > 1)
4593 s->first_displayed_line--;
4594 break;
4595 case KEY_PPAGE:
4596 case CTRL('b'):
4597 if (s->first_displayed_line == 1) {
4598 s->selected_line = 1;
4599 break;
4601 if (s->first_displayed_line > view->nlines - 2)
4602 s->first_displayed_line -=
4603 (view->nlines - 2);
4604 else
4605 s->first_displayed_line = 1;
4606 break;
4607 case 'j':
4608 case KEY_DOWN:
4609 if (s->selected_line < view->nlines - 2 &&
4610 s->first_displayed_line +
4611 s->selected_line <= s->blame.nlines)
4612 s->selected_line++;
4613 else if (s->last_displayed_line <
4614 s->blame.nlines)
4615 s->first_displayed_line++;
4616 break;
4617 case 'b':
4618 case 'p': {
4619 struct got_object_id *id = NULL;
4620 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4621 s->first_displayed_line, s->selected_line);
4622 if (id == NULL)
4623 break;
4624 if (ch == 'p') {
4625 struct got_commit_object *commit;
4626 struct got_object_qid *pid;
4627 struct got_object_id *blob_id = NULL;
4628 int obj_type;
4629 err = got_object_open_as_commit(&commit,
4630 s->repo, id);
4631 if (err)
4632 break;
4633 pid = STAILQ_FIRST(
4634 got_object_commit_get_parent_ids(commit));
4635 if (pid == NULL) {
4636 got_object_commit_close(commit);
4637 break;
4639 /* Check if path history ends here. */
4640 err = got_object_id_by_path(&blob_id, s->repo,
4641 pid->id, s->path);
4642 if (err) {
4643 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4644 err = NULL;
4645 got_object_commit_close(commit);
4646 break;
4648 err = got_object_get_type(&obj_type, s->repo,
4649 blob_id);
4650 free(blob_id);
4651 /* Can't blame non-blob type objects. */
4652 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4653 got_object_commit_close(commit);
4654 break;
4656 err = got_object_qid_alloc(&s->blamed_commit,
4657 pid->id);
4658 got_object_commit_close(commit);
4659 } else {
4660 if (got_object_id_cmp(id,
4661 s->blamed_commit->id) == 0)
4662 break;
4663 err = got_object_qid_alloc(&s->blamed_commit,
4664 id);
4666 if (err)
4667 break;
4668 s->done = 1;
4669 thread_err = stop_blame(&s->blame);
4670 s->done = 0;
4671 if (thread_err)
4672 break;
4673 STAILQ_INSERT_HEAD(&s->blamed_commits,
4674 s->blamed_commit, entry);
4675 err = run_blame(view);
4676 if (err)
4677 break;
4678 break;
4680 case 'B': {
4681 struct got_object_qid *first;
4682 first = STAILQ_FIRST(&s->blamed_commits);
4683 if (!got_object_id_cmp(first->id, s->commit_id))
4684 break;
4685 s->done = 1;
4686 thread_err = stop_blame(&s->blame);
4687 s->done = 0;
4688 if (thread_err)
4689 break;
4690 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4691 got_object_qid_free(s->blamed_commit);
4692 s->blamed_commit =
4693 STAILQ_FIRST(&s->blamed_commits);
4694 err = run_blame(view);
4695 if (err)
4696 break;
4697 break;
4699 case KEY_ENTER:
4700 case '\r': {
4701 struct got_object_id *id = NULL;
4702 struct got_object_qid *pid;
4703 struct got_commit_object *commit = NULL;
4704 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4705 s->first_displayed_line, s->selected_line);
4706 if (id == NULL)
4707 break;
4708 err = got_object_open_as_commit(&commit, s->repo, id);
4709 if (err)
4710 break;
4711 pid = STAILQ_FIRST(
4712 got_object_commit_get_parent_ids(commit));
4713 if (view_is_parent_view(view))
4714 begin_x = view_split_begin_x(view->begin_x);
4715 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4716 if (diff_view == NULL) {
4717 got_object_commit_close(commit);
4718 err = got_error_from_errno("view_open");
4719 break;
4721 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4722 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4723 got_object_commit_close(commit);
4724 if (err) {
4725 view_close(diff_view);
4726 break;
4728 view->focussed = 0;
4729 diff_view->focussed = 1;
4730 if (view_is_parent_view(view)) {
4731 err = view_close_child(view);
4732 if (err)
4733 break;
4734 view_set_child(view, diff_view);
4735 view->focus_child = 1;
4736 } else
4737 *new_view = diff_view;
4738 if (err)
4739 break;
4740 break;
4742 case KEY_NPAGE:
4743 case CTRL('f'):
4744 case ' ':
4745 if (s->last_displayed_line >= s->blame.nlines &&
4746 s->selected_line >= MIN(s->blame.nlines,
4747 view->nlines - 2)) {
4748 break;
4750 if (s->last_displayed_line >= s->blame.nlines &&
4751 s->selected_line < view->nlines - 2) {
4752 s->selected_line = MIN(s->blame.nlines,
4753 view->nlines - 2);
4754 break;
4756 if (s->last_displayed_line + view->nlines - 2
4757 <= s->blame.nlines)
4758 s->first_displayed_line +=
4759 view->nlines - 2;
4760 else
4761 s->first_displayed_line =
4762 s->blame.nlines -
4763 (view->nlines - 3);
4764 break;
4765 case KEY_RESIZE:
4766 if (s->selected_line > view->nlines - 2) {
4767 s->selected_line = MIN(s->blame.nlines,
4768 view->nlines - 2);
4770 break;
4771 default:
4772 break;
4774 return thread_err ? thread_err : err;
4777 static const struct got_error *
4778 cmd_blame(int argc, char *argv[])
4780 const struct got_error *error;
4781 struct got_repository *repo = NULL;
4782 struct got_worktree *worktree = NULL;
4783 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4784 char *link_target = NULL;
4785 struct got_object_id *commit_id = NULL;
4786 char *commit_id_str = NULL;
4787 int ch;
4788 struct tog_view *view;
4790 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4791 switch (ch) {
4792 case 'c':
4793 commit_id_str = optarg;
4794 break;
4795 case 'r':
4796 repo_path = realpath(optarg, NULL);
4797 if (repo_path == NULL)
4798 return got_error_from_errno2("realpath",
4799 optarg);
4800 break;
4801 default:
4802 usage_blame();
4803 /* NOTREACHED */
4807 argc -= optind;
4808 argv += optind;
4810 if (argc != 1)
4811 usage_blame();
4813 if (repo_path == NULL) {
4814 cwd = getcwd(NULL, 0);
4815 if (cwd == NULL)
4816 return got_error_from_errno("getcwd");
4817 error = got_worktree_open(&worktree, cwd);
4818 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4819 goto done;
4820 if (worktree)
4821 repo_path =
4822 strdup(got_worktree_get_repo_path(worktree));
4823 else
4824 repo_path = strdup(cwd);
4825 if (repo_path == NULL) {
4826 error = got_error_from_errno("strdup");
4827 goto done;
4831 error = got_repo_open(&repo, repo_path, NULL);
4832 if (error != NULL)
4833 goto done;
4835 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4836 worktree);
4837 if (error)
4838 goto done;
4840 init_curses();
4842 error = apply_unveil(got_repo_get_path(repo), NULL);
4843 if (error)
4844 goto done;
4846 error = tog_load_refs(repo);
4847 if (error)
4848 goto done;
4850 if (commit_id_str == NULL) {
4851 struct got_reference *head_ref;
4852 error = got_ref_open(&head_ref, repo, worktree ?
4853 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4854 if (error != NULL)
4855 goto done;
4856 error = got_ref_resolve(&commit_id, repo, head_ref);
4857 got_ref_close(head_ref);
4858 } else {
4859 error = got_repo_match_object_id(&commit_id, NULL,
4860 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4862 if (error != NULL)
4863 goto done;
4865 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4866 if (view == NULL) {
4867 error = got_error_from_errno("view_open");
4868 goto done;
4871 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4872 commit_id, repo);
4873 if (error)
4874 goto done;
4876 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4877 commit_id, repo);
4878 if (error)
4879 goto done;
4880 if (worktree) {
4881 /* Release work tree lock. */
4882 got_worktree_close(worktree);
4883 worktree = NULL;
4885 error = view_loop(view);
4886 done:
4887 free(repo_path);
4888 free(in_repo_path);
4889 free(link_target);
4890 free(cwd);
4891 free(commit_id);
4892 if (worktree)
4893 got_worktree_close(worktree);
4894 if (repo) {
4895 const struct got_error *close_err = got_repo_close(repo);
4896 if (error == NULL)
4897 error = close_err;
4899 tog_free_refs();
4900 return error;
4903 static const struct got_error *
4904 draw_tree_entries(struct tog_view *view, const char *parent_path)
4906 struct tog_tree_view_state *s = &view->state.tree;
4907 const struct got_error *err = NULL;
4908 struct got_tree_entry *te;
4909 wchar_t *wline;
4910 struct tog_color *tc;
4911 int width, n, i, nentries;
4912 int limit = view->nlines;
4914 s->ndisplayed = 0;
4916 werase(view->window);
4918 if (limit == 0)
4919 return NULL;
4921 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4922 if (err)
4923 return err;
4924 if (view_needs_focus_indication(view))
4925 wstandout(view->window);
4926 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4927 if (tc)
4928 wattr_on(view->window,
4929 COLOR_PAIR(tc->colorpair), NULL);
4930 waddwstr(view->window, wline);
4931 if (tc)
4932 wattr_off(view->window,
4933 COLOR_PAIR(tc->colorpair), NULL);
4934 if (view_needs_focus_indication(view))
4935 wstandend(view->window);
4936 free(wline);
4937 wline = NULL;
4938 if (width < view->ncols - 1)
4939 waddch(view->window, '\n');
4940 if (--limit <= 0)
4941 return NULL;
4942 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4943 if (err)
4944 return err;
4945 waddwstr(view->window, wline);
4946 free(wline);
4947 wline = NULL;
4948 if (width < view->ncols - 1)
4949 waddch(view->window, '\n');
4950 if (--limit <= 0)
4951 return NULL;
4952 waddch(view->window, '\n');
4953 if (--limit <= 0)
4954 return NULL;
4956 if (s->first_displayed_entry == NULL) {
4957 te = got_object_tree_get_first_entry(s->tree);
4958 if (s->selected == 0) {
4959 if (view->focussed)
4960 wstandout(view->window);
4961 s->selected_entry = NULL;
4963 waddstr(view->window, " ..\n"); /* parent directory */
4964 if (s->selected == 0 && view->focussed)
4965 wstandend(view->window);
4966 s->ndisplayed++;
4967 if (--limit <= 0)
4968 return NULL;
4969 n = 1;
4970 } else {
4971 n = 0;
4972 te = s->first_displayed_entry;
4975 nentries = got_object_tree_get_nentries(s->tree);
4976 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4977 char *line = NULL, *id_str = NULL, *link_target = NULL;
4978 const char *modestr = "";
4979 mode_t mode;
4981 te = got_object_tree_get_entry(s->tree, i);
4982 mode = got_tree_entry_get_mode(te);
4984 if (s->show_ids) {
4985 err = got_object_id_str(&id_str,
4986 got_tree_entry_get_id(te));
4987 if (err)
4988 return got_error_from_errno(
4989 "got_object_id_str");
4991 if (got_object_tree_entry_is_submodule(te))
4992 modestr = "$";
4993 else if (S_ISLNK(mode)) {
4994 int i;
4996 err = got_tree_entry_get_symlink_target(&link_target,
4997 te, s->repo);
4998 if (err) {
4999 free(id_str);
5000 return err;
5002 for (i = 0; i < strlen(link_target); i++) {
5003 if (!isprint((unsigned char)link_target[i]))
5004 link_target[i] = '?';
5006 modestr = "@";
5008 else if (S_ISDIR(mode))
5009 modestr = "/";
5010 else if (mode & S_IXUSR)
5011 modestr = "*";
5012 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5013 got_tree_entry_get_name(te), modestr,
5014 link_target ? " -> ": "",
5015 link_target ? link_target : "") == -1) {
5016 free(id_str);
5017 free(link_target);
5018 return got_error_from_errno("asprintf");
5020 free(id_str);
5021 free(link_target);
5022 err = format_line(&wline, &width, line, view->ncols, 0);
5023 if (err) {
5024 free(line);
5025 break;
5027 if (n == s->selected) {
5028 if (view->focussed)
5029 wstandout(view->window);
5030 s->selected_entry = te;
5032 tc = match_color(&s->colors, line);
5033 if (tc)
5034 wattr_on(view->window,
5035 COLOR_PAIR(tc->colorpair), NULL);
5036 waddwstr(view->window, wline);
5037 if (tc)
5038 wattr_off(view->window,
5039 COLOR_PAIR(tc->colorpair), NULL);
5040 if (width < view->ncols - 1)
5041 waddch(view->window, '\n');
5042 if (n == s->selected && view->focussed)
5043 wstandend(view->window);
5044 free(line);
5045 free(wline);
5046 wline = NULL;
5047 n++;
5048 s->ndisplayed++;
5049 s->last_displayed_entry = te;
5050 if (--limit <= 0)
5051 break;
5054 return err;
5057 static void
5058 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5060 struct got_tree_entry *te;
5061 int isroot = s->tree == s->root;
5062 int i = 0;
5064 if (s->first_displayed_entry == NULL)
5065 return;
5067 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5068 while (i++ < maxscroll) {
5069 if (te == NULL) {
5070 if (!isroot)
5071 s->first_displayed_entry = NULL;
5072 break;
5074 s->first_displayed_entry = te;
5075 te = got_tree_entry_get_prev(s->tree, te);
5079 static void
5080 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5082 struct got_tree_entry *next, *last;
5083 int n = 0;
5085 if (s->first_displayed_entry)
5086 next = got_tree_entry_get_next(s->tree,
5087 s->first_displayed_entry);
5088 else
5089 next = got_object_tree_get_first_entry(s->tree);
5091 last = s->last_displayed_entry;
5092 while (next && last && n++ < maxscroll) {
5093 last = got_tree_entry_get_next(s->tree, last);
5094 if (last) {
5095 s->first_displayed_entry = next;
5096 next = got_tree_entry_get_next(s->tree, next);
5101 static const struct got_error *
5102 tree_entry_path(char **path, struct tog_parent_trees *parents,
5103 struct got_tree_entry *te)
5105 const struct got_error *err = NULL;
5106 struct tog_parent_tree *pt;
5107 size_t len = 2; /* for leading slash and NUL */
5109 TAILQ_FOREACH(pt, parents, entry)
5110 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5111 + 1 /* slash */;
5112 if (te)
5113 len += strlen(got_tree_entry_get_name(te));
5115 *path = calloc(1, len);
5116 if (path == NULL)
5117 return got_error_from_errno("calloc");
5119 (*path)[0] = '/';
5120 pt = TAILQ_LAST(parents, tog_parent_trees);
5121 while (pt) {
5122 const char *name = got_tree_entry_get_name(pt->selected_entry);
5123 if (strlcat(*path, name, len) >= len) {
5124 err = got_error(GOT_ERR_NO_SPACE);
5125 goto done;
5127 if (strlcat(*path, "/", len) >= len) {
5128 err = got_error(GOT_ERR_NO_SPACE);
5129 goto done;
5131 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5133 if (te) {
5134 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5135 err = got_error(GOT_ERR_NO_SPACE);
5136 goto done;
5139 done:
5140 if (err) {
5141 free(*path);
5142 *path = NULL;
5144 return err;
5147 static const struct got_error *
5148 blame_tree_entry(struct tog_view **new_view, int begin_x,
5149 struct got_tree_entry *te, struct tog_parent_trees *parents,
5150 struct got_object_id *commit_id, struct got_repository *repo)
5152 const struct got_error *err = NULL;
5153 char *path;
5154 struct tog_view *blame_view;
5156 *new_view = NULL;
5158 err = tree_entry_path(&path, parents, te);
5159 if (err)
5160 return err;
5162 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5163 if (blame_view == NULL) {
5164 err = got_error_from_errno("view_open");
5165 goto done;
5168 err = open_blame_view(blame_view, path, commit_id, repo);
5169 if (err) {
5170 if (err->code == GOT_ERR_CANCELLED)
5171 err = NULL;
5172 view_close(blame_view);
5173 } else
5174 *new_view = blame_view;
5175 done:
5176 free(path);
5177 return err;
5180 static const struct got_error *
5181 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5182 struct tog_tree_view_state *s)
5184 struct tog_view *log_view;
5185 const struct got_error *err = NULL;
5186 char *path;
5188 *new_view = NULL;
5190 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5191 if (log_view == NULL)
5192 return got_error_from_errno("view_open");
5194 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5195 if (err)
5196 return err;
5198 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5199 path, 0);
5200 if (err)
5201 view_close(log_view);
5202 else
5203 *new_view = log_view;
5204 free(path);
5205 return err;
5208 static const struct got_error *
5209 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5210 const char *head_ref_name, struct got_repository *repo)
5212 const struct got_error *err = NULL;
5213 char *commit_id_str = NULL;
5214 struct tog_tree_view_state *s = &view->state.tree;
5215 struct got_commit_object *commit = NULL;
5217 TAILQ_INIT(&s->parents);
5218 STAILQ_INIT(&s->colors);
5220 s->commit_id = got_object_id_dup(commit_id);
5221 if (s->commit_id == NULL)
5222 return got_error_from_errno("got_object_id_dup");
5224 err = got_object_open_as_commit(&commit, repo, commit_id);
5225 if (err)
5226 goto done;
5229 * The root is opened here and will be closed when the view is closed.
5230 * Any visited subtrees and their path-wise parents are opened and
5231 * closed on demand.
5233 err = got_object_open_as_tree(&s->root, repo,
5234 got_object_commit_get_tree_id(commit));
5235 if (err)
5236 goto done;
5237 s->tree = s->root;
5239 err = got_object_id_str(&commit_id_str, commit_id);
5240 if (err != NULL)
5241 goto done;
5243 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5244 err = got_error_from_errno("asprintf");
5245 goto done;
5248 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5249 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5250 if (head_ref_name) {
5251 s->head_ref_name = strdup(head_ref_name);
5252 if (s->head_ref_name == NULL) {
5253 err = got_error_from_errno("strdup");
5254 goto done;
5257 s->repo = repo;
5259 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5260 err = add_color(&s->colors, "\\$$",
5261 TOG_COLOR_TREE_SUBMODULE,
5262 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5263 if (err)
5264 goto done;
5265 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5266 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5267 if (err)
5268 goto done;
5269 err = add_color(&s->colors, "/$",
5270 TOG_COLOR_TREE_DIRECTORY,
5271 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5272 if (err)
5273 goto done;
5275 err = add_color(&s->colors, "\\*$",
5276 TOG_COLOR_TREE_EXECUTABLE,
5277 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5278 if (err)
5279 goto done;
5281 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5282 get_color_value("TOG_COLOR_COMMIT"));
5283 if (err)
5284 goto done;
5287 view->show = show_tree_view;
5288 view->input = input_tree_view;
5289 view->close = close_tree_view;
5290 view->search_start = search_start_tree_view;
5291 view->search_next = search_next_tree_view;
5292 done:
5293 free(commit_id_str);
5294 if (commit)
5295 got_object_commit_close(commit);
5296 if (err)
5297 close_tree_view(view);
5298 return err;
5301 static const struct got_error *
5302 close_tree_view(struct tog_view *view)
5304 struct tog_tree_view_state *s = &view->state.tree;
5306 free_colors(&s->colors);
5307 free(s->tree_label);
5308 s->tree_label = NULL;
5309 free(s->commit_id);
5310 s->commit_id = NULL;
5311 free(s->head_ref_name);
5312 s->head_ref_name = NULL;
5313 while (!TAILQ_EMPTY(&s->parents)) {
5314 struct tog_parent_tree *parent;
5315 parent = TAILQ_FIRST(&s->parents);
5316 TAILQ_REMOVE(&s->parents, parent, entry);
5317 if (parent->tree != s->root)
5318 got_object_tree_close(parent->tree);
5319 free(parent);
5322 if (s->tree != NULL && s->tree != s->root)
5323 got_object_tree_close(s->tree);
5324 if (s->root)
5325 got_object_tree_close(s->root);
5326 return NULL;
5329 static const struct got_error *
5330 search_start_tree_view(struct tog_view *view)
5332 struct tog_tree_view_state *s = &view->state.tree;
5334 s->matched_entry = NULL;
5335 return NULL;
5338 static int
5339 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5341 regmatch_t regmatch;
5343 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5344 0) == 0;
5347 static const struct got_error *
5348 search_next_tree_view(struct tog_view *view)
5350 struct tog_tree_view_state *s = &view->state.tree;
5351 struct got_tree_entry *te = NULL;
5353 if (!view->searching) {
5354 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5355 return NULL;
5358 if (s->matched_entry) {
5359 if (view->searching == TOG_SEARCH_FORWARD) {
5360 if (s->selected_entry)
5361 te = got_tree_entry_get_next(s->tree,
5362 s->selected_entry);
5363 else
5364 te = got_object_tree_get_first_entry(s->tree);
5365 } else {
5366 if (s->selected_entry == NULL)
5367 te = got_object_tree_get_last_entry(s->tree);
5368 else
5369 te = got_tree_entry_get_prev(s->tree,
5370 s->selected_entry);
5372 } else {
5373 if (view->searching == TOG_SEARCH_FORWARD)
5374 te = got_object_tree_get_first_entry(s->tree);
5375 else
5376 te = got_object_tree_get_last_entry(s->tree);
5379 while (1) {
5380 if (te == NULL) {
5381 if (s->matched_entry == NULL) {
5382 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5383 return NULL;
5385 if (view->searching == TOG_SEARCH_FORWARD)
5386 te = got_object_tree_get_first_entry(s->tree);
5387 else
5388 te = got_object_tree_get_last_entry(s->tree);
5391 if (match_tree_entry(te, &view->regex)) {
5392 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5393 s->matched_entry = te;
5394 break;
5397 if (view->searching == TOG_SEARCH_FORWARD)
5398 te = got_tree_entry_get_next(s->tree, te);
5399 else
5400 te = got_tree_entry_get_prev(s->tree, te);
5403 if (s->matched_entry) {
5404 s->first_displayed_entry = s->matched_entry;
5405 s->selected = 0;
5408 return NULL;
5411 static const struct got_error *
5412 show_tree_view(struct tog_view *view)
5414 const struct got_error *err = NULL;
5415 struct tog_tree_view_state *s = &view->state.tree;
5416 char *parent_path;
5418 err = tree_entry_path(&parent_path, &s->parents, NULL);
5419 if (err)
5420 return err;
5422 err = draw_tree_entries(view, parent_path);
5423 free(parent_path);
5425 view_vborder(view);
5426 return err;
5429 static const struct got_error *
5430 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5432 const struct got_error *err = NULL;
5433 struct tog_tree_view_state *s = &view->state.tree;
5434 struct tog_view *log_view, *ref_view;
5435 struct got_tree_entry *te;
5436 int begin_x = 0, n;
5438 switch (ch) {
5439 case 'i':
5440 s->show_ids = !s->show_ids;
5441 break;
5442 case 'l':
5443 if (!s->selected_entry)
5444 break;
5445 if (view_is_parent_view(view))
5446 begin_x = view_split_begin_x(view->begin_x);
5447 err = log_selected_tree_entry(&log_view, begin_x, s);
5448 view->focussed = 0;
5449 log_view->focussed = 1;
5450 if (view_is_parent_view(view)) {
5451 err = view_close_child(view);
5452 if (err)
5453 return err;
5454 view_set_child(view, log_view);
5455 view->focus_child = 1;
5456 } else
5457 *new_view = log_view;
5458 break;
5459 case 'r':
5460 if (view_is_parent_view(view))
5461 begin_x = view_split_begin_x(view->begin_x);
5462 ref_view = view_open(view->nlines, view->ncols,
5463 view->begin_y, begin_x, TOG_VIEW_REF);
5464 if (ref_view == NULL)
5465 return got_error_from_errno("view_open");
5466 err = open_ref_view(ref_view, s->repo);
5467 if (err) {
5468 view_close(ref_view);
5469 return err;
5471 view->focussed = 0;
5472 ref_view->focussed = 1;
5473 if (view_is_parent_view(view)) {
5474 err = view_close_child(view);
5475 if (err)
5476 return err;
5477 view_set_child(view, ref_view);
5478 view->focus_child = 1;
5479 } else
5480 *new_view = ref_view;
5481 break;
5482 case 'g':
5483 case KEY_HOME:
5484 s->selected = 0;
5485 if (s->tree == s->root)
5486 s->first_displayed_entry =
5487 got_object_tree_get_first_entry(s->tree);
5488 else
5489 s->first_displayed_entry = NULL;
5490 break;
5491 case 'G':
5492 case KEY_END:
5493 s->selected = 0;
5494 te = got_object_tree_get_last_entry(s->tree);
5495 for (n = 0; n < view->nlines - 3; n++) {
5496 if (te == NULL) {
5497 if(s->tree != s->root) {
5498 s->first_displayed_entry = NULL;
5499 n++;
5501 break;
5503 s->first_displayed_entry = te;
5504 te = got_tree_entry_get_prev(s->tree, te);
5506 if (n > 0)
5507 s->selected = n - 1;
5508 break;
5509 case 'k':
5510 case KEY_UP:
5511 if (s->selected > 0) {
5512 s->selected--;
5513 break;
5515 tree_scroll_up(s, 1);
5516 break;
5517 case KEY_PPAGE:
5518 case CTRL('b'):
5519 if (s->tree == s->root) {
5520 if (got_object_tree_get_first_entry(s->tree) ==
5521 s->first_displayed_entry)
5522 s->selected = 0;
5523 } else {
5524 if (s->first_displayed_entry == NULL)
5525 s->selected = 0;
5527 tree_scroll_up(s, MAX(0, view->nlines - 3));
5528 break;
5529 case 'j':
5530 case KEY_DOWN:
5531 if (s->selected < s->ndisplayed - 1) {
5532 s->selected++;
5533 break;
5535 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5536 == NULL)
5537 /* can't scroll any further */
5538 break;
5539 tree_scroll_down(s, 1);
5540 break;
5541 case KEY_NPAGE:
5542 case CTRL('f'):
5543 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5544 == NULL) {
5545 /* can't scroll any further; move cursor down */
5546 if (s->selected < s->ndisplayed - 1)
5547 s->selected = s->ndisplayed - 1;
5548 break;
5550 tree_scroll_down(s, view->nlines - 3);
5551 break;
5552 case KEY_ENTER:
5553 case '\r':
5554 case KEY_BACKSPACE:
5555 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5556 struct tog_parent_tree *parent;
5557 /* user selected '..' */
5558 if (s->tree == s->root)
5559 break;
5560 parent = TAILQ_FIRST(&s->parents);
5561 TAILQ_REMOVE(&s->parents, parent,
5562 entry);
5563 got_object_tree_close(s->tree);
5564 s->tree = parent->tree;
5565 s->first_displayed_entry =
5566 parent->first_displayed_entry;
5567 s->selected_entry =
5568 parent->selected_entry;
5569 s->selected = parent->selected;
5570 free(parent);
5571 } else if (S_ISDIR(got_tree_entry_get_mode(
5572 s->selected_entry))) {
5573 struct got_tree_object *subtree;
5574 err = got_object_open_as_tree(&subtree, s->repo,
5575 got_tree_entry_get_id(s->selected_entry));
5576 if (err)
5577 break;
5578 err = tree_view_visit_subtree(s, subtree);
5579 if (err) {
5580 got_object_tree_close(subtree);
5581 break;
5583 } else if (S_ISREG(got_tree_entry_get_mode(
5584 s->selected_entry))) {
5585 struct tog_view *blame_view;
5586 int begin_x = view_is_parent_view(view) ?
5587 view_split_begin_x(view->begin_x) : 0;
5589 err = blame_tree_entry(&blame_view, begin_x,
5590 s->selected_entry, &s->parents,
5591 s->commit_id, s->repo);
5592 if (err)
5593 break;
5594 view->focussed = 0;
5595 blame_view->focussed = 1;
5596 if (view_is_parent_view(view)) {
5597 err = view_close_child(view);
5598 if (err)
5599 return err;
5600 view_set_child(view, blame_view);
5601 view->focus_child = 1;
5602 } else
5603 *new_view = blame_view;
5605 break;
5606 case KEY_RESIZE:
5607 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5608 s->selected = view->nlines - 4;
5609 break;
5610 default:
5611 break;
5614 return err;
5617 __dead static void
5618 usage_tree(void)
5620 endwin();
5621 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5622 getprogname());
5623 exit(1);
5626 static const struct got_error *
5627 cmd_tree(int argc, char *argv[])
5629 const struct got_error *error;
5630 struct got_repository *repo = NULL;
5631 struct got_worktree *worktree = NULL;
5632 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5633 struct got_object_id *commit_id = NULL;
5634 const char *commit_id_arg = NULL;
5635 char *label = NULL;
5636 struct got_reference *ref = NULL;
5637 const char *head_ref_name = NULL;
5638 int ch;
5639 struct tog_view *view;
5641 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5642 switch (ch) {
5643 case 'c':
5644 commit_id_arg = optarg;
5645 break;
5646 case 'r':
5647 repo_path = realpath(optarg, NULL);
5648 if (repo_path == NULL)
5649 return got_error_from_errno2("realpath",
5650 optarg);
5651 break;
5652 default:
5653 usage_tree();
5654 /* NOTREACHED */
5658 argc -= optind;
5659 argv += optind;
5661 if (argc > 1)
5662 usage_tree();
5664 if (repo_path == NULL) {
5665 cwd = getcwd(NULL, 0);
5666 if (cwd == NULL)
5667 return got_error_from_errno("getcwd");
5668 error = got_worktree_open(&worktree, cwd);
5669 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5670 goto done;
5671 if (worktree)
5672 repo_path =
5673 strdup(got_worktree_get_repo_path(worktree));
5674 else
5675 repo_path = strdup(cwd);
5676 if (repo_path == NULL) {
5677 error = got_error_from_errno("strdup");
5678 goto done;
5682 error = got_repo_open(&repo, repo_path, NULL);
5683 if (error != NULL)
5684 goto done;
5686 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5687 repo, worktree);
5688 if (error)
5689 goto done;
5691 init_curses();
5693 error = apply_unveil(got_repo_get_path(repo), NULL);
5694 if (error)
5695 goto done;
5697 error = tog_load_refs(repo);
5698 if (error)
5699 goto done;
5701 if (commit_id_arg == NULL) {
5702 error = got_repo_match_object_id(&commit_id, &label,
5703 worktree ? got_worktree_get_head_ref_name(worktree) :
5704 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5705 if (error)
5706 goto done;
5707 head_ref_name = label;
5708 } else {
5709 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5710 if (error == NULL)
5711 head_ref_name = got_ref_get_name(ref);
5712 else if (error->code != GOT_ERR_NOT_REF)
5713 goto done;
5714 error = got_repo_match_object_id(&commit_id, NULL,
5715 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5716 if (error)
5717 goto done;
5720 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5721 if (view == NULL) {
5722 error = got_error_from_errno("view_open");
5723 goto done;
5725 error = open_tree_view(view, commit_id, head_ref_name, repo);
5726 if (error)
5727 goto done;
5728 if (!got_path_is_root_dir(in_repo_path)) {
5729 error = tree_view_walk_path(&view->state.tree, commit_id,
5730 in_repo_path);
5731 if (error)
5732 goto done;
5735 if (worktree) {
5736 /* Release work tree lock. */
5737 got_worktree_close(worktree);
5738 worktree = NULL;
5740 error = view_loop(view);
5741 done:
5742 free(repo_path);
5743 free(cwd);
5744 free(commit_id);
5745 free(label);
5746 if (ref)
5747 got_ref_close(ref);
5748 if (repo) {
5749 const struct got_error *close_err = got_repo_close(repo);
5750 if (error == NULL)
5751 error = close_err;
5753 tog_free_refs();
5754 return error;
5757 static const struct got_error *
5758 ref_view_load_refs(struct tog_ref_view_state *s)
5760 struct got_reflist_entry *sre;
5761 struct tog_reflist_entry *re;
5763 s->nrefs = 0;
5764 TAILQ_FOREACH(sre, &tog_refs, entry) {
5765 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5766 continue;
5768 re = malloc(sizeof(*re));
5769 if (re == NULL)
5770 return got_error_from_errno("malloc");
5772 re->ref = got_ref_dup(sre->ref);
5773 if (re->ref == NULL)
5774 return got_error_from_errno("got_ref_dup");
5775 re->idx = s->nrefs++;
5776 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5779 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5780 return NULL;
5783 void
5784 ref_view_free_refs(struct tog_ref_view_state *s)
5786 struct tog_reflist_entry *re;
5788 while (!TAILQ_EMPTY(&s->refs)) {
5789 re = TAILQ_FIRST(&s->refs);
5790 TAILQ_REMOVE(&s->refs, re, entry);
5791 got_ref_close(re->ref);
5792 free(re);
5796 static const struct got_error *
5797 open_ref_view(struct tog_view *view, struct got_repository *repo)
5799 const struct got_error *err = NULL;
5800 struct tog_ref_view_state *s = &view->state.ref;
5802 s->selected_entry = 0;
5803 s->repo = repo;
5805 TAILQ_INIT(&s->refs);
5806 STAILQ_INIT(&s->colors);
5808 err = ref_view_load_refs(s);
5809 if (err)
5810 return err;
5812 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5813 err = add_color(&s->colors, "^refs/heads/",
5814 TOG_COLOR_REFS_HEADS,
5815 get_color_value("TOG_COLOR_REFS_HEADS"));
5816 if (err)
5817 goto done;
5819 err = add_color(&s->colors, "^refs/tags/",
5820 TOG_COLOR_REFS_TAGS,
5821 get_color_value("TOG_COLOR_REFS_TAGS"));
5822 if (err)
5823 goto done;
5825 err = add_color(&s->colors, "^refs/remotes/",
5826 TOG_COLOR_REFS_REMOTES,
5827 get_color_value("TOG_COLOR_REFS_REMOTES"));
5828 if (err)
5829 goto done;
5832 view->show = show_ref_view;
5833 view->input = input_ref_view;
5834 view->close = close_ref_view;
5835 view->search_start = search_start_ref_view;
5836 view->search_next = search_next_ref_view;
5837 done:
5838 if (err)
5839 free_colors(&s->colors);
5840 return err;
5843 static const struct got_error *
5844 close_ref_view(struct tog_view *view)
5846 struct tog_ref_view_state *s = &view->state.ref;
5848 ref_view_free_refs(s);
5849 free_colors(&s->colors);
5851 return NULL;
5854 static const struct got_error *
5855 resolve_reflist_entry(struct got_object_id **commit_id,
5856 struct tog_reflist_entry *re, struct got_repository *repo)
5858 const struct got_error *err = NULL;
5859 struct got_object_id *obj_id;
5860 struct got_tag_object *tag = NULL;
5861 int obj_type;
5863 *commit_id = NULL;
5865 err = got_ref_resolve(&obj_id, repo, re->ref);
5866 if (err)
5867 return err;
5869 err = got_object_get_type(&obj_type, repo, obj_id);
5870 if (err)
5871 goto done;
5873 switch (obj_type) {
5874 case GOT_OBJ_TYPE_COMMIT:
5875 *commit_id = obj_id;
5876 break;
5877 case GOT_OBJ_TYPE_TAG:
5878 err = got_object_open_as_tag(&tag, repo, obj_id);
5879 if (err)
5880 goto done;
5881 free(obj_id);
5882 err = got_object_get_type(&obj_type, repo,
5883 got_object_tag_get_object_id(tag));
5884 if (err)
5885 goto done;
5886 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5887 err = got_error(GOT_ERR_OBJ_TYPE);
5888 goto done;
5890 *commit_id = got_object_id_dup(
5891 got_object_tag_get_object_id(tag));
5892 if (*commit_id == NULL) {
5893 err = got_error_from_errno("got_object_id_dup");
5894 goto done;
5896 break;
5897 default:
5898 err = got_error(GOT_ERR_OBJ_TYPE);
5899 break;
5902 done:
5903 if (tag)
5904 got_object_tag_close(tag);
5905 if (err) {
5906 free(*commit_id);
5907 *commit_id = NULL;
5909 return err;
5912 static const struct got_error *
5913 log_ref_entry(struct tog_view **new_view, int begin_x,
5914 struct tog_reflist_entry *re, struct got_repository *repo)
5916 struct tog_view *log_view;
5917 const struct got_error *err = NULL;
5918 struct got_object_id *commit_id = NULL;
5920 *new_view = NULL;
5922 err = resolve_reflist_entry(&commit_id, re, repo);
5923 if (err) {
5924 if (err->code != GOT_ERR_OBJ_TYPE)
5925 return err;
5926 else
5927 return NULL;
5930 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5931 if (log_view == NULL) {
5932 err = got_error_from_errno("view_open");
5933 goto done;
5936 err = open_log_view(log_view, commit_id, repo,
5937 got_ref_get_name(re->ref), "", 0);
5938 done:
5939 if (err)
5940 view_close(log_view);
5941 else
5942 *new_view = log_view;
5943 free(commit_id);
5944 return err;
5947 static void
5948 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5950 struct tog_reflist_entry *re;
5951 int i = 0;
5953 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5954 return;
5956 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5957 while (i++ < maxscroll) {
5958 if (re == NULL)
5959 break;
5960 s->first_displayed_entry = re;
5961 re = TAILQ_PREV(re, tog_reflist_head, entry);
5965 static void
5966 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5968 struct tog_reflist_entry *next, *last;
5969 int n = 0;
5971 if (s->first_displayed_entry)
5972 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5973 else
5974 next = TAILQ_FIRST(&s->refs);
5976 last = s->last_displayed_entry;
5977 while (next && last && n++ < maxscroll) {
5978 last = TAILQ_NEXT(last, entry);
5979 if (last) {
5980 s->first_displayed_entry = next;
5981 next = TAILQ_NEXT(next, entry);
5986 static const struct got_error *
5987 search_start_ref_view(struct tog_view *view)
5989 struct tog_ref_view_state *s = &view->state.ref;
5991 s->matched_entry = NULL;
5992 return NULL;
5995 static int
5996 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5998 regmatch_t regmatch;
6000 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6001 0) == 0;
6004 static const struct got_error *
6005 search_next_ref_view(struct tog_view *view)
6007 struct tog_ref_view_state *s = &view->state.ref;
6008 struct tog_reflist_entry *re = NULL;
6010 if (!view->searching) {
6011 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6012 return NULL;
6015 if (s->matched_entry) {
6016 if (view->searching == TOG_SEARCH_FORWARD) {
6017 if (s->selected_entry)
6018 re = TAILQ_NEXT(s->selected_entry, entry);
6019 else
6020 re = TAILQ_PREV(s->selected_entry,
6021 tog_reflist_head, entry);
6022 } else {
6023 if (s->selected_entry == NULL)
6024 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6025 else
6026 re = TAILQ_PREV(s->selected_entry,
6027 tog_reflist_head, entry);
6029 } else {
6030 if (view->searching == TOG_SEARCH_FORWARD)
6031 re = TAILQ_FIRST(&s->refs);
6032 else
6033 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6036 while (1) {
6037 if (re == NULL) {
6038 if (s->matched_entry == NULL) {
6039 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6040 return NULL;
6042 if (view->searching == TOG_SEARCH_FORWARD)
6043 re = TAILQ_FIRST(&s->refs);
6044 else
6045 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6048 if (match_reflist_entry(re, &view->regex)) {
6049 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6050 s->matched_entry = re;
6051 break;
6054 if (view->searching == TOG_SEARCH_FORWARD)
6055 re = TAILQ_NEXT(re, entry);
6056 else
6057 re = TAILQ_PREV(re, tog_reflist_head, entry);
6060 if (s->matched_entry) {
6061 s->first_displayed_entry = s->matched_entry;
6062 s->selected = 0;
6065 return NULL;
6068 static const struct got_error *
6069 show_ref_view(struct tog_view *view)
6071 const struct got_error *err = NULL;
6072 struct tog_ref_view_state *s = &view->state.ref;
6073 struct tog_reflist_entry *re;
6074 char *line = NULL;
6075 wchar_t *wline;
6076 struct tog_color *tc;
6077 int width, n;
6078 int limit = view->nlines;
6080 werase(view->window);
6082 s->ndisplayed = 0;
6084 if (limit == 0)
6085 return NULL;
6087 re = s->first_displayed_entry;
6089 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6090 s->nrefs) == -1)
6091 return got_error_from_errno("asprintf");
6093 err = format_line(&wline, &width, line, view->ncols, 0);
6094 if (err) {
6095 free(line);
6096 return err;
6098 if (view_needs_focus_indication(view))
6099 wstandout(view->window);
6100 waddwstr(view->window, wline);
6101 if (view_needs_focus_indication(view))
6102 wstandend(view->window);
6103 free(wline);
6104 wline = NULL;
6105 free(line);
6106 line = NULL;
6107 if (width < view->ncols - 1)
6108 waddch(view->window, '\n');
6109 if (--limit <= 0)
6110 return NULL;
6112 n = 0;
6113 while (re && limit > 0) {
6114 char *line = NULL;
6116 if (got_ref_is_symbolic(re->ref)) {
6117 if (asprintf(&line, "%s -> %s",
6118 got_ref_get_name(re->ref),
6119 got_ref_get_symref_target(re->ref)) == -1)
6120 return got_error_from_errno("asprintf");
6121 } else if (s->show_ids) {
6122 struct got_object_id *id;
6123 char *id_str;
6124 err = got_ref_resolve(&id, s->repo, re->ref);
6125 if (err)
6126 return err;
6127 err = got_object_id_str(&id_str, id);
6128 if (err) {
6129 free(id);
6130 return err;
6132 if (asprintf(&line, "%s: %s",
6133 got_ref_get_name(re->ref), id_str) == -1) {
6134 err = got_error_from_errno("asprintf");
6135 free(id);
6136 free(id_str);
6137 return err;
6139 free(id);
6140 free(id_str);
6141 } else {
6142 line = strdup(got_ref_get_name(re->ref));
6143 if (line == NULL)
6144 return got_error_from_errno("strdup");
6147 err = format_line(&wline, &width, line, view->ncols, 0);
6148 if (err) {
6149 free(line);
6150 return err;
6152 if (n == s->selected) {
6153 if (view->focussed)
6154 wstandout(view->window);
6155 s->selected_entry = re;
6157 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6158 if (tc)
6159 wattr_on(view->window,
6160 COLOR_PAIR(tc->colorpair), NULL);
6161 waddwstr(view->window, wline);
6162 if (tc)
6163 wattr_off(view->window,
6164 COLOR_PAIR(tc->colorpair), NULL);
6165 if (width < view->ncols - 1)
6166 waddch(view->window, '\n');
6167 if (n == s->selected && view->focussed)
6168 wstandend(view->window);
6169 free(line);
6170 free(wline);
6171 wline = NULL;
6172 n++;
6173 s->ndisplayed++;
6174 s->last_displayed_entry = re;
6176 limit--;
6177 re = TAILQ_NEXT(re, entry);
6180 view_vborder(view);
6181 return err;
6184 static const struct got_error *
6185 browse_ref_tree(struct tog_view **new_view, int begin_x,
6186 struct tog_reflist_entry *re, struct got_repository *repo)
6188 const struct got_error *err = NULL;
6189 struct got_object_id *commit_id = NULL;
6190 struct tog_view *tree_view;
6192 *new_view = NULL;
6194 err = resolve_reflist_entry(&commit_id, re, repo);
6195 if (err) {
6196 if (err->code != GOT_ERR_OBJ_TYPE)
6197 return err;
6198 else
6199 return NULL;
6203 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6204 if (tree_view == NULL) {
6205 err = got_error_from_errno("view_open");
6206 goto done;
6209 err = open_tree_view(tree_view, commit_id,
6210 got_ref_get_name(re->ref), repo);
6211 if (err)
6212 goto done;
6214 *new_view = tree_view;
6215 done:
6216 free(commit_id);
6217 return err;
6219 static const struct got_error *
6220 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6222 const struct got_error *err = NULL;
6223 struct tog_ref_view_state *s = &view->state.ref;
6224 struct tog_view *log_view, *tree_view;
6225 struct tog_reflist_entry *re;
6226 int begin_x = 0, n;
6228 switch (ch) {
6229 case 'i':
6230 s->show_ids = !s->show_ids;
6231 break;
6232 case KEY_ENTER:
6233 case '\r':
6234 if (!s->selected_entry)
6235 break;
6236 if (view_is_parent_view(view))
6237 begin_x = view_split_begin_x(view->begin_x);
6238 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6239 s->repo);
6240 view->focussed = 0;
6241 log_view->focussed = 1;
6242 if (view_is_parent_view(view)) {
6243 err = view_close_child(view);
6244 if (err)
6245 return err;
6246 view_set_child(view, log_view);
6247 view->focus_child = 1;
6248 } else
6249 *new_view = log_view;
6250 break;
6251 case 't':
6252 if (!s->selected_entry)
6253 break;
6254 if (view_is_parent_view(view))
6255 begin_x = view_split_begin_x(view->begin_x);
6256 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6257 s->repo);
6258 if (err || tree_view == NULL)
6259 break;
6260 view->focussed = 0;
6261 tree_view->focussed = 1;
6262 if (view_is_parent_view(view)) {
6263 err = view_close_child(view);
6264 if (err)
6265 return err;
6266 view_set_child(view, tree_view);
6267 view->focus_child = 1;
6268 } else
6269 *new_view = tree_view;
6270 break;
6271 case 'g':
6272 case KEY_HOME:
6273 s->selected = 0;
6274 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6275 break;
6276 case 'G':
6277 case KEY_END:
6278 s->selected = 0;
6279 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6280 for (n = 0; n < view->nlines - 1; n++) {
6281 if (re == NULL)
6282 break;
6283 s->first_displayed_entry = re;
6284 re = TAILQ_PREV(re, tog_reflist_head, entry);
6286 if (n > 0)
6287 s->selected = n - 1;
6288 break;
6289 case 'k':
6290 case KEY_UP:
6291 if (s->selected > 0) {
6292 s->selected--;
6293 break;
6295 ref_scroll_up(s, 1);
6296 break;
6297 case KEY_PPAGE:
6298 case CTRL('b'):
6299 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6300 s->selected = 0;
6301 ref_scroll_up(s, MAX(0, view->nlines - 1));
6302 break;
6303 case 'j':
6304 case KEY_DOWN:
6305 if (s->selected < s->ndisplayed - 1) {
6306 s->selected++;
6307 break;
6309 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6310 /* can't scroll any further */
6311 break;
6312 ref_scroll_down(s, 1);
6313 break;
6314 case KEY_NPAGE:
6315 case CTRL('f'):
6316 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6317 /* can't scroll any further; move cursor down */
6318 if (s->selected < s->ndisplayed - 1)
6319 s->selected = s->ndisplayed - 1;
6320 break;
6322 ref_scroll_down(s, view->nlines - 1);
6323 break;
6324 case CTRL('l'):
6325 tog_free_refs();
6326 err = tog_load_refs(s->repo);
6327 if (err)
6328 break;
6329 ref_view_free_refs(s);
6330 err = ref_view_load_refs(s);
6331 break;
6332 case KEY_RESIZE:
6333 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6334 s->selected = view->nlines - 2;
6335 break;
6336 default:
6337 break;
6340 return err;
6343 __dead static void
6344 usage_ref(void)
6346 endwin();
6347 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6348 getprogname());
6349 exit(1);
6352 static const struct got_error *
6353 cmd_ref(int argc, char *argv[])
6355 const struct got_error *error;
6356 struct got_repository *repo = NULL;
6357 struct got_worktree *worktree = NULL;
6358 char *cwd = NULL, *repo_path = NULL;
6359 int ch;
6360 struct tog_view *view;
6362 while ((ch = getopt(argc, argv, "r:")) != -1) {
6363 switch (ch) {
6364 case 'r':
6365 repo_path = realpath(optarg, NULL);
6366 if (repo_path == NULL)
6367 return got_error_from_errno2("realpath",
6368 optarg);
6369 break;
6370 default:
6371 usage_ref();
6372 /* NOTREACHED */
6376 argc -= optind;
6377 argv += optind;
6379 if (argc > 1)
6380 usage_ref();
6382 if (repo_path == NULL) {
6383 cwd = getcwd(NULL, 0);
6384 if (cwd == NULL)
6385 return got_error_from_errno("getcwd");
6386 error = got_worktree_open(&worktree, cwd);
6387 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6388 goto done;
6389 if (worktree)
6390 repo_path =
6391 strdup(got_worktree_get_repo_path(worktree));
6392 else
6393 repo_path = strdup(cwd);
6394 if (repo_path == NULL) {
6395 error = got_error_from_errno("strdup");
6396 goto done;
6400 error = got_repo_open(&repo, repo_path, NULL);
6401 if (error != NULL)
6402 goto done;
6404 init_curses();
6406 error = apply_unveil(got_repo_get_path(repo), NULL);
6407 if (error)
6408 goto done;
6410 error = tog_load_refs(repo);
6411 if (error)
6412 goto done;
6414 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6415 if (view == NULL) {
6416 error = got_error_from_errno("view_open");
6417 goto done;
6420 error = open_ref_view(view, repo);
6421 if (error)
6422 goto done;
6424 if (worktree) {
6425 /* Release work tree lock. */
6426 got_worktree_close(worktree);
6427 worktree = NULL;
6429 error = view_loop(view);
6430 done:
6431 free(repo_path);
6432 free(cwd);
6433 if (repo) {
6434 const struct got_error *close_err = got_repo_close(repo);
6435 if (close_err)
6436 error = close_err;
6438 tog_free_refs();
6439 return error;
6442 static void
6443 list_commands(FILE *fp)
6445 size_t i;
6447 fprintf(fp, "commands:");
6448 for (i = 0; i < nitems(tog_commands); i++) {
6449 struct tog_cmd *cmd = &tog_commands[i];
6450 fprintf(fp, " %s", cmd->name);
6452 fputc('\n', fp);
6455 __dead static void
6456 usage(int hflag, int status)
6458 FILE *fp = (status == 0) ? stdout : stderr;
6460 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6461 getprogname());
6462 if (hflag) {
6463 fprintf(fp, "lazy usage: %s path\n", getprogname());
6464 list_commands(fp);
6466 exit(status);
6469 static char **
6470 make_argv(int argc, ...)
6472 va_list ap;
6473 char **argv;
6474 int i;
6476 va_start(ap, argc);
6478 argv = calloc(argc, sizeof(char *));
6479 if (argv == NULL)
6480 err(1, "calloc");
6481 for (i = 0; i < argc; i++) {
6482 argv[i] = strdup(va_arg(ap, char *));
6483 if (argv[i] == NULL)
6484 err(1, "strdup");
6487 va_end(ap);
6488 return argv;
6492 * Try to convert 'tog path' into a 'tog log path' command.
6493 * The user could simply have mistyped the command rather than knowingly
6494 * provided a path. So check whether argv[0] can in fact be resolved
6495 * to a path in the HEAD commit and print a special error if not.
6496 * This hack is for mpi@ <3
6498 static const struct got_error *
6499 tog_log_with_path(int argc, char *argv[])
6501 const struct got_error *error = NULL, *close_err;
6502 struct tog_cmd *cmd = NULL;
6503 struct got_repository *repo = NULL;
6504 struct got_worktree *worktree = NULL;
6505 struct got_object_id *commit_id = NULL, *id = NULL;
6506 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6507 char *commit_id_str = NULL, **cmd_argv = NULL;
6509 cwd = getcwd(NULL, 0);
6510 if (cwd == NULL)
6511 return got_error_from_errno("getcwd");
6513 error = got_worktree_open(&worktree, cwd);
6514 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6515 goto done;
6517 if (worktree)
6518 repo_path = strdup(got_worktree_get_repo_path(worktree));
6519 else
6520 repo_path = strdup(cwd);
6521 if (repo_path == NULL) {
6522 error = got_error_from_errno("strdup");
6523 goto done;
6526 error = got_repo_open(&repo, repo_path, NULL);
6527 if (error != NULL)
6528 goto done;
6530 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6531 repo, worktree);
6532 if (error)
6533 goto done;
6535 error = tog_load_refs(repo);
6536 if (error)
6537 goto done;
6538 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6539 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6540 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6541 if (error)
6542 goto done;
6544 if (worktree) {
6545 got_worktree_close(worktree);
6546 worktree = NULL;
6549 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6550 if (error) {
6551 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6552 goto done;
6553 fprintf(stderr, "%s: '%s' is no known command or path\n",
6554 getprogname(), argv[0]);
6555 usage(1, 1);
6556 /* not reached */
6559 close_err = got_repo_close(repo);
6560 if (error == NULL)
6561 error = close_err;
6562 repo = NULL;
6564 error = got_object_id_str(&commit_id_str, commit_id);
6565 if (error)
6566 goto done;
6568 cmd = &tog_commands[0]; /* log */
6569 argc = 4;
6570 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6571 error = cmd->cmd_main(argc, cmd_argv);
6572 done:
6573 if (repo) {
6574 close_err = got_repo_close(repo);
6575 if (error == NULL)
6576 error = close_err;
6578 if (worktree)
6579 got_worktree_close(worktree);
6580 free(id);
6581 free(commit_id_str);
6582 free(commit_id);
6583 free(cwd);
6584 free(repo_path);
6585 free(in_repo_path);
6586 if (cmd_argv) {
6587 int i;
6588 for (i = 0; i < argc; i++)
6589 free(cmd_argv[i]);
6590 free(cmd_argv);
6592 tog_free_refs();
6593 return error;
6596 int
6597 main(int argc, char *argv[])
6599 const struct got_error *error = NULL;
6600 struct tog_cmd *cmd = NULL;
6601 int ch, hflag = 0, Vflag = 0;
6602 char **cmd_argv = NULL;
6603 static struct option longopts[] = {
6604 { "version", no_argument, NULL, 'V' },
6605 { NULL, 0, NULL, 0}
6608 setlocale(LC_CTYPE, "");
6610 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6611 switch (ch) {
6612 case 'h':
6613 hflag = 1;
6614 break;
6615 case 'V':
6616 Vflag = 1;
6617 break;
6618 default:
6619 usage(hflag, 1);
6620 /* NOTREACHED */
6624 argc -= optind;
6625 argv += optind;
6626 optind = 1;
6627 optreset = 1;
6629 if (Vflag) {
6630 got_version_print_str();
6631 return 0;
6634 #ifndef PROFILE
6635 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6636 NULL) == -1)
6637 err(1, "pledge");
6638 #endif
6640 if (argc == 0) {
6641 if (hflag)
6642 usage(hflag, 0);
6643 /* Build an argument vector which runs a default command. */
6644 cmd = &tog_commands[0];
6645 argc = 1;
6646 cmd_argv = make_argv(argc, cmd->name);
6647 } else {
6648 size_t i;
6650 /* Did the user specify a command? */
6651 for (i = 0; i < nitems(tog_commands); i++) {
6652 if (strncmp(tog_commands[i].name, argv[0],
6653 strlen(argv[0])) == 0) {
6654 cmd = &tog_commands[i];
6655 break;
6660 if (cmd == NULL) {
6661 if (argc != 1)
6662 usage(0, 1);
6663 /* No command specified; try log with a path */
6664 error = tog_log_with_path(argc, argv);
6665 } else {
6666 if (hflag)
6667 cmd->cmd_usage();
6668 else
6669 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6672 endwin();
6673 putchar('\n');
6674 if (cmd_argv) {
6675 int i;
6676 for (i = 0; i < argc; i++)
6677 free(cmd_argv[i]);
6678 free(cmd_argv);
6681 if (error && error->code != GOT_ERR_CANCELLED)
6682 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6683 return 0;