Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/ioctl.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #if defined(__FreeBSD__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #endif
25 #include <curses.h>
26 #include <panel.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
51 #include "got_diff.h"
52 #include "got_opentemp.h"
53 #include "got_utf8.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_path.h"
59 #include "got_worktree.h"
61 //#define update_panels() (0)
62 //#define doupdate() (0)
64 #ifndef MIN
65 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #endif
68 #ifndef MAX
69 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 #endif
72 #ifndef CTRL
73 #define CTRL(x) ((x) & 0x1f)
74 #endif
76 #ifndef nitems
77 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
78 #endif
80 struct tog_cmd {
81 const char *name;
82 const struct got_error *(*cmd_main)(int, char *[]);
83 void (*cmd_usage)(void);
84 };
86 __dead static void usage(int, int);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_ref(void);
93 static const struct got_error* cmd_log(int, char *[]);
94 static const struct got_error* cmd_diff(int, char *[]);
95 static const struct got_error* cmd_blame(int, char *[]);
96 static const struct got_error* cmd_tree(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
99 static struct tog_cmd tog_commands[] = {
100 { "log", cmd_log, usage_log },
101 { "diff", cmd_diff, usage_diff },
102 { "blame", cmd_blame, usage_blame },
103 { "tree", cmd_tree, usage_tree },
104 { "ref", cmd_ref, usage_ref },
105 };
107 enum tog_view_type {
108 TOG_VIEW_DIFF,
109 TOG_VIEW_LOG,
110 TOG_VIEW_BLAME,
111 TOG_VIEW_TREE,
112 TOG_VIEW_REF,
113 };
115 #define TOG_EOF_STRING "(END)"
117 struct commit_queue_entry {
118 TAILQ_ENTRY(commit_queue_entry) entry;
119 struct got_object_id *id;
120 struct got_commit_object *commit;
121 int idx;
122 };
123 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 struct commit_queue {
125 int ncommits;
126 struct commit_queue_head head;
127 };
129 struct tog_color {
130 STAILQ_ENTRY(tog_color) entry;
131 regex_t regex;
132 short colorpair;
133 };
134 STAILQ_HEAD(tog_colors, tog_color);
136 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static const struct got_error *
140 tog_load_refs(struct got_repository *repo, int sort_by_date)
142 const struct got_error *err;
144 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
145 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
146 repo);
147 if (err)
148 return err;
150 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
151 repo);
154 static void
155 tog_free_refs(void)
157 if (tog_refs_idmap) {
158 got_reflist_object_id_map_free(tog_refs_idmap);
159 tog_refs_idmap = NULL;
161 got_ref_list_free(&tog_refs);
164 static const struct got_error *
165 add_color(struct tog_colors *colors, const char *pattern,
166 int idx, short color)
168 const struct got_error *err = NULL;
169 struct tog_color *tc;
170 int regerr = 0;
172 if (idx < 1 || idx > COLOR_PAIRS - 1)
173 return NULL;
175 init_pair(idx, color, -1);
177 tc = calloc(1, sizeof(*tc));
178 if (tc == NULL)
179 return got_error_from_errno("calloc");
180 regerr = regcomp(&tc->regex, pattern,
181 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
182 if (regerr) {
183 static char regerr_msg[512];
184 static char err_msg[512];
185 regerror(regerr, &tc->regex, regerr_msg,
186 sizeof(regerr_msg));
187 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
188 regerr_msg);
189 err = got_error_msg(GOT_ERR_REGEX, err_msg);
190 free(tc);
191 return err;
193 tc->colorpair = idx;
194 STAILQ_INSERT_HEAD(colors, tc, entry);
195 return NULL;
198 static void
199 free_colors(struct tog_colors *colors)
201 struct tog_color *tc;
203 while (!STAILQ_EMPTY(colors)) {
204 tc = STAILQ_FIRST(colors);
205 STAILQ_REMOVE_HEAD(colors, entry);
206 regfree(&tc->regex);
207 free(tc);
211 struct tog_color *
212 get_color(struct tog_colors *colors, int colorpair)
214 struct tog_color *tc = NULL;
216 STAILQ_FOREACH(tc, colors, entry) {
217 if (tc->colorpair == colorpair)
218 return tc;
221 return NULL;
224 static int
225 default_color_value(const char *envvar)
227 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
228 return COLOR_MAGENTA;
229 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
230 return COLOR_CYAN;
231 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
232 return COLOR_YELLOW;
233 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
234 return COLOR_GREEN;
235 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
236 return COLOR_MAGENTA;
237 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
238 return COLOR_MAGENTA;
239 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
240 return COLOR_CYAN;
241 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
242 return COLOR_GREEN;
243 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
244 return COLOR_GREEN;
245 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
246 return COLOR_CYAN;
247 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
248 return COLOR_YELLOW;
249 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
250 return COLOR_GREEN;
251 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
254 return COLOR_YELLOW;
256 return -1;
259 static int
260 get_color_value(const char *envvar)
262 const char *val = getenv(envvar);
264 if (val == NULL)
265 return default_color_value(envvar);
267 if (strcasecmp(val, "black") == 0)
268 return COLOR_BLACK;
269 if (strcasecmp(val, "red") == 0)
270 return COLOR_RED;
271 if (strcasecmp(val, "green") == 0)
272 return COLOR_GREEN;
273 if (strcasecmp(val, "yellow") == 0)
274 return COLOR_YELLOW;
275 if (strcasecmp(val, "blue") == 0)
276 return COLOR_BLUE;
277 if (strcasecmp(val, "magenta") == 0)
278 return COLOR_MAGENTA;
279 if (strcasecmp(val, "cyan") == 0)
280 return COLOR_CYAN;
281 if (strcasecmp(val, "white") == 0)
282 return COLOR_WHITE;
283 if (strcasecmp(val, "default") == 0)
284 return -1;
286 return default_color_value(envvar);
290 struct tog_diff_view_state {
291 struct got_object_id *id1, *id2;
292 const char *label1, *label2;
293 FILE *f;
294 int first_displayed_line;
295 int last_displayed_line;
296 int eof;
297 int diff_context;
298 int ignore_whitespace;
299 int force_text_diff;
300 struct got_repository *repo;
301 struct tog_colors colors;
302 size_t nlines;
303 off_t *line_offsets;
304 int matched_line;
305 int selected_line;
307 /* passed from log view; may be NULL */
308 struct tog_view *log_view;
309 };
311 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
313 struct tog_log_thread_args {
314 pthread_cond_t need_commits;
315 pthread_cond_t commit_loaded;
316 int commits_needed;
317 int load_all;
318 struct got_commit_graph *graph;
319 struct commit_queue *commits;
320 const char *in_repo_path;
321 struct got_object_id *start_id;
322 struct got_repository *repo;
323 int log_complete;
324 sig_atomic_t *quit;
325 struct commit_queue_entry **first_displayed_entry;
326 struct commit_queue_entry **selected_entry;
327 int *searching;
328 int *search_next_done;
329 regex_t *regex;
330 };
332 struct tog_log_view_state {
333 struct commit_queue commits;
334 struct commit_queue_entry *first_displayed_entry;
335 struct commit_queue_entry *last_displayed_entry;
336 struct commit_queue_entry *selected_entry;
337 int selected;
338 char *in_repo_path;
339 char *head_ref_name;
340 int log_branches;
341 struct got_repository *repo;
342 struct got_object_id *start_id;
343 sig_atomic_t quit;
344 pthread_t thread;
345 struct tog_log_thread_args thread_args;
346 struct commit_queue_entry *matched_entry;
347 struct commit_queue_entry *search_entry;
348 struct tog_colors colors;
349 };
351 #define TOG_COLOR_DIFF_MINUS 1
352 #define TOG_COLOR_DIFF_PLUS 2
353 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
354 #define TOG_COLOR_DIFF_META 4
355 #define TOG_COLOR_TREE_SUBMODULE 5
356 #define TOG_COLOR_TREE_SYMLINK 6
357 #define TOG_COLOR_TREE_DIRECTORY 7
358 #define TOG_COLOR_TREE_EXECUTABLE 8
359 #define TOG_COLOR_COMMIT 9
360 #define TOG_COLOR_AUTHOR 10
361 #define TOG_COLOR_DATE 11
362 #define TOG_COLOR_REFS_HEADS 12
363 #define TOG_COLOR_REFS_TAGS 13
364 #define TOG_COLOR_REFS_REMOTES 14
366 struct tog_blame_cb_args {
367 struct tog_blame_line *lines; /* one per line */
368 int nlines;
370 struct tog_view *view;
371 struct got_object_id *commit_id;
372 int *quit;
373 };
375 struct tog_blame_thread_args {
376 const char *path;
377 struct got_repository *repo;
378 struct tog_blame_cb_args *cb_args;
379 int *complete;
380 got_cancel_cb cancel_cb;
381 void *cancel_arg;
382 };
384 struct tog_blame {
385 FILE *f;
386 off_t filesize;
387 struct tog_blame_line *lines;
388 int nlines;
389 off_t *line_offsets;
390 pthread_t thread;
391 struct tog_blame_thread_args thread_args;
392 struct tog_blame_cb_args cb_args;
393 const char *path;
394 };
396 struct tog_blame_view_state {
397 int first_displayed_line;
398 int last_displayed_line;
399 int selected_line;
400 int blame_complete;
401 int eof;
402 int done;
403 struct got_object_id_queue blamed_commits;
404 struct got_object_qid *blamed_commit;
405 char *path;
406 struct got_repository *repo;
407 struct got_object_id *commit_id;
408 struct tog_blame blame;
409 int matched_line;
410 struct tog_colors colors;
411 };
413 struct tog_parent_tree {
414 TAILQ_ENTRY(tog_parent_tree) entry;
415 struct got_tree_object *tree;
416 struct got_tree_entry *first_displayed_entry;
417 struct got_tree_entry *selected_entry;
418 int selected;
419 };
421 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
423 struct tog_tree_view_state {
424 char *tree_label;
425 struct got_object_id *commit_id;/* commit which this tree belongs to */
426 struct got_tree_object *root; /* the commit's root tree entry */
427 struct got_tree_object *tree; /* currently displayed (sub-)tree */
428 struct got_tree_entry *first_displayed_entry;
429 struct got_tree_entry *last_displayed_entry;
430 struct got_tree_entry *selected_entry;
431 int ndisplayed, selected, show_ids;
432 struct tog_parent_trees parents; /* parent trees of current sub-tree */
433 char *head_ref_name;
434 struct got_repository *repo;
435 struct got_tree_entry *matched_entry;
436 struct tog_colors colors;
437 };
439 struct tog_reflist_entry {
440 TAILQ_ENTRY(tog_reflist_entry) entry;
441 struct got_reference *ref;
442 int idx;
443 };
445 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
447 struct tog_ref_view_state {
448 struct tog_reflist_head refs;
449 struct tog_reflist_entry *first_displayed_entry;
450 struct tog_reflist_entry *last_displayed_entry;
451 struct tog_reflist_entry *selected_entry;
452 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
453 struct got_repository *repo;
454 struct tog_reflist_entry *matched_entry;
455 struct tog_colors colors;
456 };
458 /*
459 * We implement two types of views: parent views and child views.
461 * The 'Tab' key switches focus between a parent view and its child view.
462 * Child views are shown side-by-side to their parent view, provided
463 * there is enough screen estate.
465 * When a new view is opened from within a parent view, this new view
466 * becomes a child view of the parent view, replacing any existing child.
468 * When a new view is opened from within a child view, this new view
469 * becomes a parent view which will obscure the views below until the
470 * user quits the new parent view by typing 'q'.
472 * This list of views contains parent views only.
473 * Child views are only pointed to by their parent view.
474 */
475 TAILQ_HEAD(tog_view_list_head, tog_view);
477 struct tog_view {
478 TAILQ_ENTRY(tog_view) entry;
479 WINDOW *window;
480 PANEL *panel;
481 int nlines, ncols, begin_y, begin_x;
482 int lines, cols; /* copies of LINES and COLS */
483 int focussed; /* Only set on one parent or child view at a time. */
484 int dying;
485 struct tog_view *parent;
486 struct tog_view *child;
488 /*
489 * This flag is initially set on parent views when a new child view
490 * is created. It gets toggled when the 'Tab' key switches focus
491 * between parent and child.
492 * The flag indicates whether focus should be passed on to our child
493 * view if this parent view gets picked for focus after another parent
494 * view was closed. This prevents child views from losing focus in such
495 * situations.
496 */
497 int focus_child;
499 /* type-specific state */
500 enum tog_view_type type;
501 union {
502 struct tog_diff_view_state diff;
503 struct tog_log_view_state log;
504 struct tog_blame_view_state blame;
505 struct tog_tree_view_state tree;
506 struct tog_ref_view_state ref;
507 } state;
509 const struct got_error *(*show)(struct tog_view *);
510 const struct got_error *(*input)(struct tog_view **,
511 struct tog_view *, int);
512 const struct got_error *(*close)(struct tog_view *);
514 const struct got_error *(*search_start)(struct tog_view *);
515 const struct got_error *(*search_next)(struct tog_view *);
516 int search_started;
517 int searching;
518 #define TOG_SEARCH_FORWARD 1
519 #define TOG_SEARCH_BACKWARD 2
520 int search_next_done;
521 #define TOG_SEARCH_HAVE_MORE 1
522 #define TOG_SEARCH_NO_MORE 2
523 #define TOG_SEARCH_HAVE_NONE 3
524 regex_t regex;
525 regmatch_t regmatch;
526 };
528 static const struct got_error *open_diff_view(struct tog_view *,
529 struct got_object_id *, struct got_object_id *,
530 const char *, const char *, int, int, int, struct tog_view *,
531 struct got_repository *);
532 static const struct got_error *show_diff_view(struct tog_view *);
533 static const struct got_error *input_diff_view(struct tog_view **,
534 struct tog_view *, int);
535 static const struct got_error* close_diff_view(struct tog_view *);
536 static const struct got_error *search_start_diff_view(struct tog_view *);
537 static const struct got_error *search_next_diff_view(struct tog_view *);
539 static const struct got_error *open_log_view(struct tog_view *,
540 struct got_object_id *, struct got_repository *,
541 const char *, const char *, int);
542 static const struct got_error * show_log_view(struct tog_view *);
543 static const struct got_error *input_log_view(struct tog_view **,
544 struct tog_view *, int);
545 static const struct got_error *close_log_view(struct tog_view *);
546 static const struct got_error *search_start_log_view(struct tog_view *);
547 static const struct got_error *search_next_log_view(struct tog_view *);
549 static const struct got_error *open_blame_view(struct tog_view *, char *,
550 struct got_object_id *, struct got_repository *);
551 static const struct got_error *show_blame_view(struct tog_view *);
552 static const struct got_error *input_blame_view(struct tog_view **,
553 struct tog_view *, int);
554 static const struct got_error *close_blame_view(struct tog_view *);
555 static const struct got_error *search_start_blame_view(struct tog_view *);
556 static const struct got_error *search_next_blame_view(struct tog_view *);
558 static const struct got_error *open_tree_view(struct tog_view *,
559 struct got_object_id *, const char *, struct got_repository *);
560 static const struct got_error *show_tree_view(struct tog_view *);
561 static const struct got_error *input_tree_view(struct tog_view **,
562 struct tog_view *, int);
563 static const struct got_error *close_tree_view(struct tog_view *);
564 static const struct got_error *search_start_tree_view(struct tog_view *);
565 static const struct got_error *search_next_tree_view(struct tog_view *);
567 static const struct got_error *open_ref_view(struct tog_view *,
568 struct got_repository *);
569 static const struct got_error *show_ref_view(struct tog_view *);
570 static const struct got_error *input_ref_view(struct tog_view **,
571 struct tog_view *, int);
572 static const struct got_error *close_ref_view(struct tog_view *);
573 static const struct got_error *search_start_ref_view(struct tog_view *);
574 static const struct got_error *search_next_ref_view(struct tog_view *);
576 static volatile sig_atomic_t tog_sigwinch_received;
577 static volatile sig_atomic_t tog_sigpipe_received;
578 static volatile sig_atomic_t tog_sigcont_received;
580 static void
581 tog_sigwinch(int signo)
583 tog_sigwinch_received = 1;
586 static void
587 tog_sigpipe(int signo)
589 tog_sigpipe_received = 1;
592 static void
593 tog_sigcont(int signo)
595 tog_sigcont_received = 1;
598 static const struct got_error *
599 view_close(struct tog_view *view)
601 const struct got_error *err = NULL;
603 if (view->child) {
604 view_close(view->child);
605 view->child = NULL;
607 if (view->close)
608 err = view->close(view);
609 if (view->panel)
610 del_panel(view->panel);
611 if (view->window)
612 delwin(view->window);
613 free(view);
614 return err;
617 static struct tog_view *
618 view_open(int nlines, int ncols, int begin_y, int begin_x,
619 enum tog_view_type type)
621 struct tog_view *view = calloc(1, sizeof(*view));
623 if (view == NULL)
624 return NULL;
626 view->type = type;
627 view->lines = LINES;
628 view->cols = COLS;
629 view->nlines = nlines ? nlines : LINES - begin_y;
630 view->ncols = ncols ? ncols : COLS - begin_x;
631 view->begin_y = begin_y;
632 view->begin_x = begin_x;
633 view->window = newwin(nlines, ncols, begin_y, begin_x);
634 if (view->window == NULL) {
635 view_close(view);
636 return NULL;
638 view->panel = new_panel(view->window);
639 if (view->panel == NULL ||
640 set_panel_userptr(view->panel, view) != OK) {
641 view_close(view);
642 return NULL;
645 keypad(view->window, TRUE);
646 return view;
649 static int
650 view_split_begin_x(int begin_x)
652 if (begin_x > 0 || COLS < 120)
653 return 0;
654 return (COLS - MAX(COLS / 2, 80));
657 static const struct got_error *view_resize(struct tog_view *);
659 static const struct got_error *
660 view_splitscreen(struct tog_view *view)
662 const struct got_error *err = NULL;
664 view->begin_y = 0;
665 view->begin_x = view_split_begin_x(0);
666 view->nlines = LINES;
667 view->ncols = COLS - view->begin_x;
668 view->lines = LINES;
669 view->cols = COLS;
670 err = view_resize(view);
671 if (err)
672 return err;
674 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
675 return got_error_from_errno("mvwin");
677 return NULL;
680 static const struct got_error *
681 view_fullscreen(struct tog_view *view)
683 const struct got_error *err = NULL;
685 view->begin_x = 0;
686 view->begin_y = 0;
687 view->nlines = LINES;
688 view->ncols = COLS;
689 view->lines = LINES;
690 view->cols = COLS;
691 err = view_resize(view);
692 if (err)
693 return err;
695 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
696 return got_error_from_errno("mvwin");
698 return NULL;
701 static int
702 view_is_parent_view(struct tog_view *view)
704 return view->parent == NULL;
707 static const struct got_error *
708 view_resize(struct tog_view *view)
710 int nlines, ncols;
712 if (view->lines > LINES)
713 nlines = view->nlines - (view->lines - LINES);
714 else
715 nlines = view->nlines + (LINES - view->lines);
717 if (view->cols > COLS)
718 ncols = view->ncols - (view->cols - COLS);
719 else
720 ncols = view->ncols + (COLS - view->cols);
722 if (wresize(view->window, nlines, ncols) == ERR)
723 return got_error_from_errno("wresize");
724 if (replace_panel(view->panel, view->window) == ERR)
725 return got_error_from_errno("replace_panel");
726 wclear(view->window);
728 view->nlines = nlines;
729 view->ncols = ncols;
730 view->lines = LINES;
731 view->cols = COLS;
733 if (view->child) {
734 view->child->begin_x = view_split_begin_x(view->begin_x);
735 if (view->child->begin_x == 0) {
736 view_fullscreen(view->child);
737 if (view->child->focussed)
738 show_panel(view->child->panel);
739 else
740 show_panel(view->panel);
741 } else {
742 view_splitscreen(view->child);
743 show_panel(view->child->panel);
747 return NULL;
750 static const struct got_error *
751 view_close_child(struct tog_view *view)
753 const struct got_error *err = NULL;
755 if (view->child == NULL)
756 return NULL;
758 err = view_close(view->child);
759 view->child = NULL;
760 return err;
763 static void
764 view_set_child(struct tog_view *view, struct tog_view *child)
766 view->child = child;
767 child->parent = view;
770 static int
771 view_is_splitscreen(struct tog_view *view)
773 return view->begin_x > 0;
776 static void
777 tog_resizeterm(void)
779 int cols, lines;
780 struct winsize size;
782 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
783 cols = 80; /* Default */
784 lines = 24;
785 } else {
786 cols = size.ws_col;
787 lines = size.ws_row;
789 resize_term(lines, cols);
792 static const struct got_error *
793 view_search_start(struct tog_view *view)
795 const struct got_error *err = NULL;
796 char pattern[1024];
797 int ret;
799 if (view->search_started) {
800 regfree(&view->regex);
801 view->searching = 0;
802 memset(&view->regmatch, 0, sizeof(view->regmatch));
804 view->search_started = 0;
806 if (view->nlines < 1)
807 return NULL;
809 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
810 wclrtoeol(view->window);
812 nocbreak();
813 echo();
814 ret = wgetnstr(view->window, pattern, sizeof(pattern));
815 cbreak();
816 noecho();
817 if (ret == ERR)
818 return NULL;
820 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
821 err = view->search_start(view);
822 if (err) {
823 regfree(&view->regex);
824 return err;
826 view->search_started = 1;
827 view->searching = TOG_SEARCH_FORWARD;
828 view->search_next_done = 0;
829 view->search_next(view);
832 return NULL;
835 static const struct got_error *
836 view_input(struct tog_view **new, int *done, struct tog_view *view,
837 struct tog_view_list_head *views)
839 const struct got_error *err = NULL;
840 struct tog_view *v;
841 int ch, errcode;
843 *new = NULL;
845 /* Clear "no matches" indicator. */
846 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
847 view->search_next_done == TOG_SEARCH_HAVE_NONE)
848 view->search_next_done = TOG_SEARCH_HAVE_MORE;
850 if (view->searching && !view->search_next_done) {
851 errcode = pthread_mutex_unlock(&tog_mutex);
852 if (errcode)
853 return got_error_set_errno(errcode,
854 "pthread_mutex_unlock");
855 sched_yield();
856 errcode = pthread_mutex_lock(&tog_mutex);
857 if (errcode)
858 return got_error_set_errno(errcode,
859 "pthread_mutex_lock");
860 view->search_next(view);
861 return NULL;
864 nodelay(stdscr, FALSE);
865 /* Allow threads to make progress while we are waiting for input. */
866 errcode = pthread_mutex_unlock(&tog_mutex);
867 if (errcode)
868 return got_error_set_errno(errcode, "pthread_mutex_unlock");
869 ch = wgetch(view->window);
870 errcode = pthread_mutex_lock(&tog_mutex);
871 if (errcode)
872 return got_error_set_errno(errcode, "pthread_mutex_lock");
873 nodelay(stdscr, TRUE);
875 if (tog_sigwinch_received || tog_sigcont_received) {
876 tog_resizeterm();
877 tog_sigwinch_received = 0;
878 tog_sigcont_received = 0;
879 TAILQ_FOREACH(v, views, entry) {
880 err = view_resize(v);
881 if (err)
882 return err;
883 err = v->input(new, v, KEY_RESIZE);
884 if (err)
885 return err;
886 if (v->child) {
887 err = view_resize(v->child);
888 if (err)
889 return err;
890 err = v->child->input(new, v->child,
891 KEY_RESIZE);
892 if (err)
893 return err;
898 switch (ch) {
899 case '\t':
900 if (view->child) {
901 view->focussed = 0;
902 view->child->focussed = 1;
903 view->focus_child = 1;
904 } else if (view->parent) {
905 view->focussed = 0;
906 view->parent->focussed = 1;
907 view->parent->focus_child = 0;
909 break;
910 case 'q':
911 err = view->input(new, view, ch);
912 view->dying = 1;
913 break;
914 case 'Q':
915 *done = 1;
916 break;
917 case 'f':
918 if (view_is_parent_view(view)) {
919 if (view->child == NULL)
920 break;
921 if (view_is_splitscreen(view->child)) {
922 view->focussed = 0;
923 view->child->focussed = 1;
924 err = view_fullscreen(view->child);
925 } else
926 err = view_splitscreen(view->child);
927 if (err)
928 break;
929 err = view->child->input(new, view->child,
930 KEY_RESIZE);
931 } else {
932 if (view_is_splitscreen(view)) {
933 view->parent->focussed = 0;
934 view->focussed = 1;
935 err = view_fullscreen(view);
936 } else {
937 err = view_splitscreen(view);
939 if (err)
940 break;
941 err = view->input(new, view, KEY_RESIZE);
943 break;
944 case KEY_RESIZE:
945 break;
946 case '/':
947 if (view->search_start)
948 view_search_start(view);
949 else
950 err = view->input(new, view, ch);
951 break;
952 case 'N':
953 case 'n':
954 if (view->search_started && view->search_next) {
955 view->searching = (ch == 'n' ?
956 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
957 view->search_next_done = 0;
958 view->search_next(view);
959 } else
960 err = view->input(new, view, ch);
961 break;
962 default:
963 err = view->input(new, view, ch);
964 break;
967 return err;
970 void
971 view_vborder(struct tog_view *view)
973 PANEL *panel;
974 const struct tog_view *view_above;
976 if (view->parent)
977 return view_vborder(view->parent);
979 panel = panel_above(view->panel);
980 if (panel == NULL)
981 return;
983 view_above = panel_userptr(panel);
984 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
985 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
988 int
989 view_needs_focus_indication(struct tog_view *view)
991 if (view_is_parent_view(view)) {
992 if (view->child == NULL || view->child->focussed)
993 return 0;
994 if (!view_is_splitscreen(view->child))
995 return 0;
996 } else if (!view_is_splitscreen(view))
997 return 0;
999 return view->focussed;
1002 static const struct got_error *
1003 view_loop(struct tog_view *view)
1005 const struct got_error *err = NULL;
1006 struct tog_view_list_head views;
1007 struct tog_view *new_view;
1008 int fast_refresh = 10;
1009 int done = 0, errcode;
1011 errcode = pthread_mutex_lock(&tog_mutex);
1012 if (errcode)
1013 return got_error_set_errno(errcode, "pthread_mutex_lock");
1015 TAILQ_INIT(&views);
1016 TAILQ_INSERT_HEAD(&views, view, entry);
1018 view->focussed = 1;
1019 err = view->show(view);
1020 if (err)
1021 return err;
1022 update_panels();
1023 doupdate();
1024 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1025 /* Refresh fast during initialization, then become slower. */
1026 if (fast_refresh && fast_refresh-- == 0)
1027 halfdelay(10); /* switch to once per second */
1029 err = view_input(&new_view, &done, view, &views);
1030 if (err)
1031 break;
1032 if (view->dying) {
1033 struct tog_view *v, *prev = NULL;
1035 if (view_is_parent_view(view))
1036 prev = TAILQ_PREV(view, tog_view_list_head,
1037 entry);
1038 else if (view->parent)
1039 prev = view->parent;
1041 if (view->parent) {
1042 view->parent->child = NULL;
1043 view->parent->focus_child = 0;
1044 } else
1045 TAILQ_REMOVE(&views, view, entry);
1047 err = view_close(view);
1048 if (err)
1049 goto done;
1051 view = NULL;
1052 TAILQ_FOREACH(v, &views, entry) {
1053 if (v->focussed)
1054 break;
1056 if (view == NULL && new_view == NULL) {
1057 /* No view has focus. Try to pick one. */
1058 if (prev)
1059 view = prev;
1060 else if (!TAILQ_EMPTY(&views)) {
1061 view = TAILQ_LAST(&views,
1062 tog_view_list_head);
1064 if (view) {
1065 if (view->focus_child) {
1066 view->child->focussed = 1;
1067 view = view->child;
1068 } else
1069 view->focussed = 1;
1073 if (new_view) {
1074 struct tog_view *v, *t;
1075 /* Only allow one parent view per type. */
1076 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1077 if (v->type != new_view->type)
1078 continue;
1079 TAILQ_REMOVE(&views, v, entry);
1080 err = view_close(v);
1081 if (err)
1082 goto done;
1083 break;
1085 TAILQ_INSERT_TAIL(&views, new_view, entry);
1086 view = new_view;
1088 if (view) {
1089 if (view_is_parent_view(view)) {
1090 if (view->child && view->child->focussed)
1091 view = view->child;
1092 } else {
1093 if (view->parent && view->parent->focussed)
1094 view = view->parent;
1096 show_panel(view->panel);
1097 if (view->child && view_is_splitscreen(view->child))
1098 show_panel(view->child->panel);
1099 if (view->parent && view_is_splitscreen(view)) {
1100 err = view->parent->show(view->parent);
1101 if (err)
1102 goto done;
1104 err = view->show(view);
1105 if (err)
1106 goto done;
1107 if (view->child) {
1108 err = view->child->show(view->child);
1109 if (err)
1110 goto done;
1112 update_panels();
1113 doupdate();
1116 done:
1117 while (!TAILQ_EMPTY(&views)) {
1118 view = TAILQ_FIRST(&views);
1119 TAILQ_REMOVE(&views, view, entry);
1120 view_close(view);
1123 errcode = pthread_mutex_unlock(&tog_mutex);
1124 if (errcode && err == NULL)
1125 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1127 return err;
1130 __dead static void
1131 usage_log(void)
1133 endwin();
1134 fprintf(stderr,
1135 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1136 getprogname());
1137 exit(1);
1140 /* Create newly allocated wide-character string equivalent to a byte string. */
1141 static const struct got_error *
1142 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1144 char *vis = NULL;
1145 const struct got_error *err = NULL;
1147 *ws = NULL;
1148 *wlen = mbstowcs(NULL, s, 0);
1149 if (*wlen == (size_t)-1) {
1150 int vislen;
1151 if (errno != EILSEQ)
1152 return got_error_from_errno("mbstowcs");
1154 /* byte string invalid in current encoding; try to "fix" it */
1155 err = got_mbsavis(&vis, &vislen, s);
1156 if (err)
1157 return err;
1158 *wlen = mbstowcs(NULL, vis, 0);
1159 if (*wlen == (size_t)-1) {
1160 err = got_error_from_errno("mbstowcs"); /* give up */
1161 goto done;
1165 *ws = calloc(*wlen + 1, sizeof(**ws));
1166 if (*ws == NULL) {
1167 err = got_error_from_errno("calloc");
1168 goto done;
1171 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1172 err = got_error_from_errno("mbstowcs");
1173 done:
1174 free(vis);
1175 if (err) {
1176 free(*ws);
1177 *ws = NULL;
1178 *wlen = 0;
1180 return err;
1183 /* Format a line for display, ensuring that it won't overflow a width limit. */
1184 static const struct got_error *
1185 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1186 int col_tab_align)
1188 const struct got_error *err = NULL;
1189 int cols = 0;
1190 wchar_t *wline = NULL;
1191 size_t wlen;
1192 int i;
1194 *wlinep = NULL;
1195 *widthp = 0;
1197 err = mbs2ws(&wline, &wlen, line);
1198 if (err)
1199 return err;
1201 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1202 wline[wlen - 1] = L'\0';
1203 wlen--;
1205 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1206 wline[wlen - 1] = L'\0';
1207 wlen--;
1210 i = 0;
1211 while (i < wlen) {
1212 int width = wcwidth(wline[i]);
1214 if (width == 0) {
1215 i++;
1216 continue;
1219 if (width == 1 || width == 2) {
1220 if (cols + width > wlimit)
1221 break;
1222 cols += width;
1223 i++;
1224 } else if (width == -1) {
1225 if (wline[i] == L'\t') {
1226 width = TABSIZE -
1227 ((cols + col_tab_align) % TABSIZE);
1228 } else {
1229 width = 1;
1230 wline[i] = L'.';
1232 if (cols + width > wlimit)
1233 break;
1234 cols += width;
1235 i++;
1236 } else {
1237 err = got_error_from_errno("wcwidth");
1238 goto done;
1241 wline[i] = L'\0';
1242 if (widthp)
1243 *widthp = cols;
1244 done:
1245 if (err)
1246 free(wline);
1247 else
1248 *wlinep = wline;
1249 return err;
1252 static const struct got_error*
1253 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1254 struct got_object_id *id, struct got_repository *repo)
1256 static const struct got_error *err = NULL;
1257 struct got_reflist_entry *re;
1258 char *s;
1259 const char *name;
1261 *refs_str = NULL;
1263 TAILQ_FOREACH(re, refs, entry) {
1264 struct got_tag_object *tag = NULL;
1265 struct got_object_id *ref_id;
1266 int cmp;
1268 name = got_ref_get_name(re->ref);
1269 if (strcmp(name, GOT_REF_HEAD) == 0)
1270 continue;
1271 if (strncmp(name, "refs/", 5) == 0)
1272 name += 5;
1273 if (strncmp(name, "got/", 4) == 0)
1274 continue;
1275 if (strncmp(name, "heads/", 6) == 0)
1276 name += 6;
1277 if (strncmp(name, "remotes/", 8) == 0) {
1278 name += 8;
1279 s = strstr(name, "/" GOT_REF_HEAD);
1280 if (s != NULL && s[strlen(s)] == '\0')
1281 continue;
1283 err = got_ref_resolve(&ref_id, repo, re->ref);
1284 if (err)
1285 break;
1286 if (strncmp(name, "tags/", 5) == 0) {
1287 err = got_object_open_as_tag(&tag, repo, ref_id);
1288 if (err) {
1289 if (err->code != GOT_ERR_OBJ_TYPE) {
1290 free(ref_id);
1291 break;
1293 /* Ref points at something other than a tag. */
1294 err = NULL;
1295 tag = NULL;
1298 cmp = got_object_id_cmp(tag ?
1299 got_object_tag_get_object_id(tag) : ref_id, id);
1300 free(ref_id);
1301 if (tag)
1302 got_object_tag_close(tag);
1303 if (cmp != 0)
1304 continue;
1305 s = *refs_str;
1306 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1307 s ? ", " : "", name) == -1) {
1308 err = got_error_from_errno("asprintf");
1309 free(s);
1310 *refs_str = NULL;
1311 break;
1313 free(s);
1316 return err;
1319 static const struct got_error *
1320 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1321 int col_tab_align)
1323 char *smallerthan;
1325 smallerthan = strchr(author, '<');
1326 if (smallerthan && smallerthan[1] != '\0')
1327 author = smallerthan + 1;
1328 author[strcspn(author, "@>")] = '\0';
1329 return format_line(wauthor, author_width, author, limit, col_tab_align);
1332 static const struct got_error *
1333 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1334 struct got_object_id *id, const size_t date_display_cols,
1335 int author_display_cols)
1337 struct tog_log_view_state *s = &view->state.log;
1338 const struct got_error *err = NULL;
1339 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1340 char *logmsg0 = NULL, *logmsg = NULL;
1341 char *author = NULL;
1342 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1343 int author_width, logmsg_width;
1344 char *newline, *line = NULL;
1345 int col, limit;
1346 const int avail = view->ncols;
1347 struct tm tm;
1348 time_t committer_time;
1349 struct tog_color *tc;
1351 committer_time = got_object_commit_get_committer_time(commit);
1352 if (gmtime_r(&committer_time, &tm) == NULL)
1353 return got_error_from_errno("gmtime_r");
1354 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1355 return got_error(GOT_ERR_NO_SPACE);
1357 if (avail <= date_display_cols)
1358 limit = MIN(sizeof(datebuf) - 1, avail);
1359 else
1360 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1361 tc = get_color(&s->colors, TOG_COLOR_DATE);
1362 if (tc)
1363 wattr_on(view->window,
1364 COLOR_PAIR(tc->colorpair), NULL);
1365 waddnstr(view->window, datebuf, limit);
1366 if (tc)
1367 wattr_off(view->window,
1368 COLOR_PAIR(tc->colorpair), NULL);
1369 col = limit;
1370 if (col > avail)
1371 goto done;
1373 if (avail >= 120) {
1374 char *id_str;
1375 err = got_object_id_str(&id_str, id);
1376 if (err)
1377 goto done;
1378 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1379 if (tc)
1380 wattr_on(view->window,
1381 COLOR_PAIR(tc->colorpair), NULL);
1382 wprintw(view->window, "%.8s ", id_str);
1383 if (tc)
1384 wattr_off(view->window,
1385 COLOR_PAIR(tc->colorpair), NULL);
1386 free(id_str);
1387 col += 9;
1388 if (col > avail)
1389 goto done;
1392 author = strdup(got_object_commit_get_author(commit));
1393 if (author == NULL) {
1394 err = got_error_from_errno("strdup");
1395 goto done;
1397 err = format_author(&wauthor, &author_width, author, avail - col, col);
1398 if (err)
1399 goto done;
1400 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1401 if (tc)
1402 wattr_on(view->window,
1403 COLOR_PAIR(tc->colorpair), NULL);
1404 waddwstr(view->window, wauthor);
1405 if (tc)
1406 wattr_off(view->window,
1407 COLOR_PAIR(tc->colorpair), NULL);
1408 col += author_width;
1409 while (col < avail && author_width < author_display_cols + 2) {
1410 waddch(view->window, ' ');
1411 col++;
1412 author_width++;
1414 if (col > avail)
1415 goto done;
1417 err = got_object_commit_get_logmsg(&logmsg0, commit);
1418 if (err)
1419 goto done;
1420 logmsg = logmsg0;
1421 while (*logmsg == '\n')
1422 logmsg++;
1423 newline = strchr(logmsg, '\n');
1424 if (newline)
1425 *newline = '\0';
1426 limit = avail - col;
1427 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1428 if (err)
1429 goto done;
1430 waddwstr(view->window, wlogmsg);
1431 col += logmsg_width;
1432 while (col < avail) {
1433 waddch(view->window, ' ');
1434 col++;
1436 done:
1437 free(logmsg0);
1438 free(wlogmsg);
1439 free(author);
1440 free(wauthor);
1441 free(line);
1442 return err;
1445 static struct commit_queue_entry *
1446 alloc_commit_queue_entry(struct got_commit_object *commit,
1447 struct got_object_id *id)
1449 struct commit_queue_entry *entry;
1451 entry = calloc(1, sizeof(*entry));
1452 if (entry == NULL)
1453 return NULL;
1455 entry->id = id;
1456 entry->commit = commit;
1457 return entry;
1460 static void
1461 pop_commit(struct commit_queue *commits)
1463 struct commit_queue_entry *entry;
1465 entry = TAILQ_FIRST(&commits->head);
1466 TAILQ_REMOVE(&commits->head, entry, entry);
1467 got_object_commit_close(entry->commit);
1468 commits->ncommits--;
1469 /* Don't free entry->id! It is owned by the commit graph. */
1470 free(entry);
1473 static void
1474 free_commits(struct commit_queue *commits)
1476 while (!TAILQ_EMPTY(&commits->head))
1477 pop_commit(commits);
1480 static const struct got_error *
1481 match_commit(int *have_match, struct got_object_id *id,
1482 struct got_commit_object *commit, regex_t *regex)
1484 const struct got_error *err = NULL;
1485 regmatch_t regmatch;
1486 char *id_str = NULL, *logmsg = NULL;
1488 *have_match = 0;
1490 err = got_object_id_str(&id_str, id);
1491 if (err)
1492 return err;
1494 err = got_object_commit_get_logmsg(&logmsg, commit);
1495 if (err)
1496 goto done;
1498 if (regexec(regex, got_object_commit_get_author(commit), 1,
1499 &regmatch, 0) == 0 ||
1500 regexec(regex, got_object_commit_get_committer(commit), 1,
1501 &regmatch, 0) == 0 ||
1502 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1503 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1504 *have_match = 1;
1505 done:
1506 free(id_str);
1507 free(logmsg);
1508 return err;
1511 static const struct got_error *
1512 queue_commits(struct tog_log_thread_args *a)
1514 const struct got_error *err = NULL;
1517 * We keep all commits open throughout the lifetime of the log
1518 * view in order to avoid having to re-fetch commits from disk
1519 * while updating the display.
1521 do {
1522 struct got_object_id *id;
1523 struct got_commit_object *commit;
1524 struct commit_queue_entry *entry;
1525 int errcode;
1527 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1528 NULL, NULL);
1529 if (err || id == NULL)
1530 break;
1532 err = got_object_open_as_commit(&commit, a->repo, id);
1533 if (err)
1534 break;
1535 entry = alloc_commit_queue_entry(commit, id);
1536 if (entry == NULL) {
1537 err = got_error_from_errno("alloc_commit_queue_entry");
1538 break;
1541 errcode = pthread_mutex_lock(&tog_mutex);
1542 if (errcode) {
1543 err = got_error_set_errno(errcode,
1544 "pthread_mutex_lock");
1545 break;
1548 entry->idx = a->commits->ncommits;
1549 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1550 a->commits->ncommits++;
1552 if (*a->searching == TOG_SEARCH_FORWARD &&
1553 !*a->search_next_done) {
1554 int have_match;
1555 err = match_commit(&have_match, id, commit, a->regex);
1556 if (err)
1557 break;
1558 if (have_match)
1559 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1562 errcode = pthread_mutex_unlock(&tog_mutex);
1563 if (errcode && err == NULL)
1564 err = got_error_set_errno(errcode,
1565 "pthread_mutex_unlock");
1566 if (err)
1567 break;
1568 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1570 return err;
1573 static void
1574 select_commit(struct tog_log_view_state *s)
1576 struct commit_queue_entry *entry;
1577 int ncommits = 0;
1579 entry = s->first_displayed_entry;
1580 while (entry) {
1581 if (ncommits == s->selected) {
1582 s->selected_entry = entry;
1583 break;
1585 entry = TAILQ_NEXT(entry, entry);
1586 ncommits++;
1590 static const struct got_error *
1591 draw_commits(struct tog_view *view)
1593 const struct got_error *err = NULL;
1594 struct tog_log_view_state *s = &view->state.log;
1595 struct commit_queue_entry *entry = s->selected_entry;
1596 const int limit = view->nlines;
1597 int width;
1598 int ncommits, author_cols = 4;
1599 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1600 char *refs_str = NULL;
1601 wchar_t *wline;
1602 struct tog_color *tc;
1603 static const size_t date_display_cols = 12;
1605 if (s->selected_entry &&
1606 !(view->searching && view->search_next_done == 0)) {
1607 struct got_reflist_head *refs;
1608 err = got_object_id_str(&id_str, s->selected_entry->id);
1609 if (err)
1610 return err;
1611 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1612 s->selected_entry->id);
1613 if (refs) {
1614 err = build_refs_str(&refs_str, refs,
1615 s->selected_entry->id, s->repo);
1616 if (err)
1617 goto done;
1621 if (s->thread_args.commits_needed == 0)
1622 halfdelay(10); /* disable fast refresh */
1624 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1625 if (asprintf(&ncommits_str, " [%d/%d] %s",
1626 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1627 (view->searching && !view->search_next_done) ?
1628 "searching..." : "loading...") == -1) {
1629 err = got_error_from_errno("asprintf");
1630 goto done;
1632 } else {
1633 const char *search_str = NULL;
1635 if (view->searching) {
1636 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1637 search_str = "no more matches";
1638 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1639 search_str = "no matches found";
1640 else if (!view->search_next_done)
1641 search_str = "searching...";
1644 if (asprintf(&ncommits_str, " [%d/%d] %s",
1645 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1646 search_str ? search_str :
1647 (refs_str ? refs_str : "")) == -1) {
1648 err = got_error_from_errno("asprintf");
1649 goto done;
1653 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1654 if (asprintf(&header, "commit %s %s%s",
1655 id_str ? id_str : "........................................",
1656 s->in_repo_path, ncommits_str) == -1) {
1657 err = got_error_from_errno("asprintf");
1658 header = NULL;
1659 goto done;
1661 } else if (asprintf(&header, "commit %s%s",
1662 id_str ? id_str : "........................................",
1663 ncommits_str) == -1) {
1664 err = got_error_from_errno("asprintf");
1665 header = NULL;
1666 goto done;
1668 err = format_line(&wline, &width, header, view->ncols, 0);
1669 if (err)
1670 goto done;
1672 werase(view->window);
1674 if (view_needs_focus_indication(view))
1675 wstandout(view->window);
1676 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1677 if (tc)
1678 wattr_on(view->window,
1679 COLOR_PAIR(tc->colorpair), NULL);
1680 waddwstr(view->window, wline);
1681 if (tc)
1682 wattr_off(view->window,
1683 COLOR_PAIR(tc->colorpair), NULL);
1684 while (width < view->ncols) {
1685 waddch(view->window, ' ');
1686 width++;
1688 if (view_needs_focus_indication(view))
1689 wstandend(view->window);
1690 free(wline);
1691 if (limit <= 1)
1692 goto done;
1694 /* Grow author column size if necessary. */
1695 entry = s->first_displayed_entry;
1696 ncommits = 0;
1697 while (entry) {
1698 char *author;
1699 wchar_t *wauthor;
1700 int width;
1701 if (ncommits >= limit - 1)
1702 break;
1703 author = strdup(got_object_commit_get_author(entry->commit));
1704 if (author == NULL) {
1705 err = got_error_from_errno("strdup");
1706 goto done;
1708 err = format_author(&wauthor, &width, author, COLS,
1709 date_display_cols);
1710 if (author_cols < width)
1711 author_cols = width;
1712 free(wauthor);
1713 free(author);
1714 ncommits++;
1715 entry = TAILQ_NEXT(entry, entry);
1718 entry = s->first_displayed_entry;
1719 s->last_displayed_entry = s->first_displayed_entry;
1720 ncommits = 0;
1721 while (entry) {
1722 if (ncommits >= limit - 1)
1723 break;
1724 if (ncommits == s->selected)
1725 wstandout(view->window);
1726 err = draw_commit(view, entry->commit, entry->id,
1727 date_display_cols, author_cols);
1728 if (ncommits == s->selected)
1729 wstandend(view->window);
1730 if (err)
1731 goto done;
1732 ncommits++;
1733 s->last_displayed_entry = entry;
1734 entry = TAILQ_NEXT(entry, entry);
1737 view_vborder(view);
1738 update_panels();
1739 doupdate();
1740 done:
1741 free(id_str);
1742 free(refs_str);
1743 free(ncommits_str);
1744 free(header);
1745 return err;
1748 static void
1749 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1751 struct commit_queue_entry *entry;
1752 int nscrolled = 0;
1754 entry = TAILQ_FIRST(&s->commits.head);
1755 if (s->first_displayed_entry == entry)
1756 return;
1758 entry = s->first_displayed_entry;
1759 while (entry && nscrolled < maxscroll) {
1760 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1761 if (entry) {
1762 s->first_displayed_entry = entry;
1763 nscrolled++;
1768 static const struct got_error *
1769 trigger_log_thread(struct tog_view *view, int wait)
1771 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1772 int errcode;
1774 halfdelay(1); /* fast refresh while loading commits */
1776 while (ta->commits_needed > 0 || ta->load_all) {
1777 if (ta->log_complete)
1778 break;
1780 /* Wake the log thread. */
1781 errcode = pthread_cond_signal(&ta->need_commits);
1782 if (errcode)
1783 return got_error_set_errno(errcode,
1784 "pthread_cond_signal");
1787 * The mutex will be released while the view loop waits
1788 * in wgetch(), at which time the log thread will run.
1790 if (!wait)
1791 break;
1793 /* Display progress update in log view. */
1794 show_log_view(view);
1795 update_panels();
1796 doupdate();
1798 /* Wait right here while next commit is being loaded. */
1799 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1800 if (errcode)
1801 return got_error_set_errno(errcode,
1802 "pthread_cond_wait");
1804 /* Display progress update in log view. */
1805 show_log_view(view);
1806 update_panels();
1807 doupdate();
1810 return NULL;
1813 static const struct got_error *
1814 log_scroll_down(struct tog_view *view, int maxscroll)
1816 struct tog_log_view_state *s = &view->state.log;
1817 const struct got_error *err = NULL;
1818 struct commit_queue_entry *pentry;
1819 int nscrolled = 0, ncommits_needed;
1821 if (s->last_displayed_entry == NULL)
1822 return NULL;
1824 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1825 if (s->commits.ncommits < ncommits_needed &&
1826 !s->thread_args.log_complete) {
1828 * Ask the log thread for required amount of commits.
1830 s->thread_args.commits_needed += maxscroll;
1831 err = trigger_log_thread(view, 1);
1832 if (err)
1833 return err;
1836 do {
1837 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1838 if (pentry == NULL)
1839 break;
1841 s->last_displayed_entry = pentry;
1843 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1844 if (pentry == NULL)
1845 break;
1846 s->first_displayed_entry = pentry;
1847 } while (++nscrolled < maxscroll);
1849 return err;
1852 static const struct got_error *
1853 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1854 struct got_commit_object *commit, struct got_object_id *commit_id,
1855 struct tog_view *log_view, struct got_repository *repo)
1857 const struct got_error *err;
1858 struct got_object_qid *parent_id;
1859 struct tog_view *diff_view;
1861 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1862 if (diff_view == NULL)
1863 return got_error_from_errno("view_open");
1865 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1866 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1867 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1868 if (err == NULL)
1869 *new_view = diff_view;
1870 return err;
1873 static const struct got_error *
1874 tree_view_visit_subtree(struct tog_tree_view_state *s,
1875 struct got_tree_object *subtree)
1877 struct tog_parent_tree *parent;
1879 parent = calloc(1, sizeof(*parent));
1880 if (parent == NULL)
1881 return got_error_from_errno("calloc");
1883 parent->tree = s->tree;
1884 parent->first_displayed_entry = s->first_displayed_entry;
1885 parent->selected_entry = s->selected_entry;
1886 parent->selected = s->selected;
1887 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1888 s->tree = subtree;
1889 s->selected = 0;
1890 s->first_displayed_entry = NULL;
1891 return NULL;
1894 static const struct got_error *
1895 tree_view_walk_path(struct tog_tree_view_state *s,
1896 struct got_object_id *commit_id, const char *path)
1898 const struct got_error *err = NULL;
1899 struct got_tree_object *tree = NULL;
1900 const char *p;
1901 char *slash, *subpath = NULL;
1903 /* Walk the path and open corresponding tree objects. */
1904 p = path;
1905 while (*p) {
1906 struct got_tree_entry *te;
1907 struct got_object_id *tree_id;
1908 char *te_name;
1910 while (p[0] == '/')
1911 p++;
1913 /* Ensure the correct subtree entry is selected. */
1914 slash = strchr(p, '/');
1915 if (slash == NULL)
1916 te_name = strdup(p);
1917 else
1918 te_name = strndup(p, slash - p);
1919 if (te_name == NULL) {
1920 err = got_error_from_errno("strndup");
1921 break;
1923 te = got_object_tree_find_entry(s->tree, te_name);
1924 if (te == NULL) {
1925 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1926 free(te_name);
1927 break;
1929 free(te_name);
1930 s->first_displayed_entry = s->selected_entry = te;
1932 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1933 break; /* jump to this file's entry */
1935 slash = strchr(p, '/');
1936 if (slash)
1937 subpath = strndup(path, slash - path);
1938 else
1939 subpath = strdup(path);
1940 if (subpath == NULL) {
1941 err = got_error_from_errno("strdup");
1942 break;
1945 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1946 subpath);
1947 if (err)
1948 break;
1950 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1951 free(tree_id);
1952 if (err)
1953 break;
1955 err = tree_view_visit_subtree(s, tree);
1956 if (err) {
1957 got_object_tree_close(tree);
1958 break;
1960 if (slash == NULL)
1961 break;
1962 free(subpath);
1963 subpath = NULL;
1964 p = slash;
1967 free(subpath);
1968 return err;
1971 static const struct got_error *
1972 browse_commit_tree(struct tog_view **new_view, int begin_x,
1973 struct commit_queue_entry *entry, const char *path,
1974 const char *head_ref_name, struct got_repository *repo)
1976 const struct got_error *err = NULL;
1977 struct tog_tree_view_state *s;
1978 struct tog_view *tree_view;
1980 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1981 if (tree_view == NULL)
1982 return got_error_from_errno("view_open");
1984 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1985 if (err)
1986 return err;
1987 s = &tree_view->state.tree;
1989 *new_view = tree_view;
1991 if (got_path_is_root_dir(path))
1992 return NULL;
1994 return tree_view_walk_path(s, entry->id, path);
1997 static const struct got_error *
1998 block_signals_used_by_main_thread(void)
2000 sigset_t sigset;
2001 int errcode;
2003 if (sigemptyset(&sigset) == -1)
2004 return got_error_from_errno("sigemptyset");
2006 /* tog handles SIGWINCH and SIGCONT */
2007 if (sigaddset(&sigset, SIGWINCH) == -1)
2008 return got_error_from_errno("sigaddset");
2009 if (sigaddset(&sigset, SIGCONT) == -1)
2010 return got_error_from_errno("sigaddset");
2012 /* ncurses handles SIGTSTP */
2013 if (sigaddset(&sigset, SIGTSTP) == -1)
2014 return got_error_from_errno("sigaddset");
2016 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2017 if (errcode)
2018 return got_error_set_errno(errcode, "pthread_sigmask");
2020 return NULL;
2023 static void *
2024 log_thread(void *arg)
2026 const struct got_error *err = NULL;
2027 int errcode = 0;
2028 struct tog_log_thread_args *a = arg;
2029 int done = 0;
2031 err = block_signals_used_by_main_thread();
2032 if (err)
2033 return (void *)err;
2035 while (!done && !err && !tog_sigpipe_received) {
2036 err = queue_commits(a);
2037 if (err) {
2038 if (err->code != GOT_ERR_ITER_COMPLETED)
2039 return (void *)err;
2040 err = NULL;
2041 done = 1;
2042 } else if (a->commits_needed > 0 && !a->load_all)
2043 a->commits_needed--;
2045 errcode = pthread_mutex_lock(&tog_mutex);
2046 if (errcode) {
2047 err = got_error_set_errno(errcode,
2048 "pthread_mutex_lock");
2049 break;
2050 } else if (*a->quit)
2051 done = 1;
2052 else if (*a->first_displayed_entry == NULL) {
2053 *a->first_displayed_entry =
2054 TAILQ_FIRST(&a->commits->head);
2055 *a->selected_entry = *a->first_displayed_entry;
2058 errcode = pthread_cond_signal(&a->commit_loaded);
2059 if (errcode) {
2060 err = got_error_set_errno(errcode,
2061 "pthread_cond_signal");
2062 pthread_mutex_unlock(&tog_mutex);
2063 break;
2066 if (done)
2067 a->commits_needed = 0;
2068 else {
2069 if (a->commits_needed == 0 && !a->load_all) {
2070 errcode = pthread_cond_wait(&a->need_commits,
2071 &tog_mutex);
2072 if (errcode)
2073 err = got_error_set_errno(errcode,
2074 "pthread_cond_wait");
2075 if (*a->quit)
2076 done = 1;
2080 errcode = pthread_mutex_unlock(&tog_mutex);
2081 if (errcode && err == NULL)
2082 err = got_error_set_errno(errcode,
2083 "pthread_mutex_unlock");
2085 a->log_complete = 1;
2086 return (void *)err;
2089 static const struct got_error *
2090 stop_log_thread(struct tog_log_view_state *s)
2092 const struct got_error *err = NULL;
2093 int errcode;
2095 if (s->thread) {
2096 s->quit = 1;
2097 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2098 if (errcode)
2099 return got_error_set_errno(errcode,
2100 "pthread_cond_signal");
2101 errcode = pthread_mutex_unlock(&tog_mutex);
2102 if (errcode)
2103 return got_error_set_errno(errcode,
2104 "pthread_mutex_unlock");
2105 errcode = pthread_join(s->thread, (void **)&err);
2106 if (errcode)
2107 return got_error_set_errno(errcode, "pthread_join");
2108 errcode = pthread_mutex_lock(&tog_mutex);
2109 if (errcode)
2110 return got_error_set_errno(errcode,
2111 "pthread_mutex_lock");
2112 s->thread = 0; //NULL;
2115 if (s->thread_args.repo) {
2116 err = got_repo_close(s->thread_args.repo);
2117 s->thread_args.repo = NULL;
2120 if (s->thread_args.graph) {
2121 got_commit_graph_close(s->thread_args.graph);
2122 s->thread_args.graph = NULL;
2125 return err;
2128 static const struct got_error *
2129 close_log_view(struct tog_view *view)
2131 const struct got_error *err = NULL;
2132 struct tog_log_view_state *s = &view->state.log;
2133 int errcode;
2135 err = stop_log_thread(s);
2137 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2138 if (errcode && err == NULL)
2139 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2141 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2142 if (errcode && err == NULL)
2143 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2145 free_commits(&s->commits);
2146 free(s->in_repo_path);
2147 s->in_repo_path = NULL;
2148 free(s->start_id);
2149 s->start_id = NULL;
2150 free(s->head_ref_name);
2151 s->head_ref_name = NULL;
2152 return err;
2155 static const struct got_error *
2156 search_start_log_view(struct tog_view *view)
2158 struct tog_log_view_state *s = &view->state.log;
2160 s->matched_entry = NULL;
2161 s->search_entry = NULL;
2162 return NULL;
2165 static const struct got_error *
2166 search_next_log_view(struct tog_view *view)
2168 const struct got_error *err = NULL;
2169 struct tog_log_view_state *s = &view->state.log;
2170 struct commit_queue_entry *entry;
2172 /* Display progress update in log view. */
2173 show_log_view(view);
2174 update_panels();
2175 doupdate();
2177 if (s->search_entry) {
2178 int errcode, ch;
2179 errcode = pthread_mutex_unlock(&tog_mutex);
2180 if (errcode)
2181 return got_error_set_errno(errcode,
2182 "pthread_mutex_unlock");
2183 ch = wgetch(view->window);
2184 errcode = pthread_mutex_lock(&tog_mutex);
2185 if (errcode)
2186 return got_error_set_errno(errcode,
2187 "pthread_mutex_lock");
2188 if (ch == KEY_BACKSPACE) {
2189 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2190 return NULL;
2192 if (view->searching == TOG_SEARCH_FORWARD)
2193 entry = TAILQ_NEXT(s->search_entry, entry);
2194 else
2195 entry = TAILQ_PREV(s->search_entry,
2196 commit_queue_head, entry);
2197 } else if (s->matched_entry) {
2198 if (view->searching == TOG_SEARCH_FORWARD)
2199 entry = TAILQ_NEXT(s->matched_entry, entry);
2200 else
2201 entry = TAILQ_PREV(s->matched_entry,
2202 commit_queue_head, entry);
2203 } else {
2204 entry = s->selected_entry;
2207 while (1) {
2208 int have_match = 0;
2210 if (entry == NULL) {
2211 if (s->thread_args.log_complete ||
2212 view->searching == TOG_SEARCH_BACKWARD) {
2213 view->search_next_done =
2214 (s->matched_entry == NULL ?
2215 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2216 s->search_entry = NULL;
2217 return NULL;
2220 * Poke the log thread for more commits and return,
2221 * allowing the main loop to make progress. Search
2222 * will resume at s->search_entry once we come back.
2224 s->thread_args.commits_needed++;
2225 return trigger_log_thread(view, 0);
2228 err = match_commit(&have_match, entry->id, entry->commit,
2229 &view->regex);
2230 if (err)
2231 break;
2232 if (have_match) {
2233 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2234 s->matched_entry = entry;
2235 break;
2238 s->search_entry = entry;
2239 if (view->searching == TOG_SEARCH_FORWARD)
2240 entry = TAILQ_NEXT(entry, entry);
2241 else
2242 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2245 if (s->matched_entry) {
2246 int cur = s->selected_entry->idx;
2247 while (cur < s->matched_entry->idx) {
2248 err = input_log_view(NULL, view, KEY_DOWN);
2249 if (err)
2250 return err;
2251 cur++;
2253 while (cur > s->matched_entry->idx) {
2254 err = input_log_view(NULL, view, KEY_UP);
2255 if (err)
2256 return err;
2257 cur--;
2261 s->search_entry = NULL;
2263 return NULL;
2266 static const struct got_error *
2267 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2268 struct got_repository *repo, const char *head_ref_name,
2269 const char *in_repo_path, int log_branches)
2271 const struct got_error *err = NULL;
2272 struct tog_log_view_state *s = &view->state.log;
2273 struct got_repository *thread_repo = NULL;
2274 struct got_commit_graph *thread_graph = NULL;
2275 int errcode;
2277 if (in_repo_path != s->in_repo_path) {
2278 free(s->in_repo_path);
2279 s->in_repo_path = strdup(in_repo_path);
2280 if (s->in_repo_path == NULL)
2281 return got_error_from_errno("strdup");
2284 /* The commit queue only contains commits being displayed. */
2285 TAILQ_INIT(&s->commits.head);
2286 s->commits.ncommits = 0;
2288 s->repo = repo;
2289 if (head_ref_name) {
2290 s->head_ref_name = strdup(head_ref_name);
2291 if (s->head_ref_name == NULL) {
2292 err = got_error_from_errno("strdup");
2293 goto done;
2296 s->start_id = got_object_id_dup(start_id);
2297 if (s->start_id == NULL) {
2298 err = got_error_from_errno("got_object_id_dup");
2299 goto done;
2301 s->log_branches = log_branches;
2303 STAILQ_INIT(&s->colors);
2304 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2305 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2306 get_color_value("TOG_COLOR_COMMIT"));
2307 if (err)
2308 goto done;
2309 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2310 get_color_value("TOG_COLOR_AUTHOR"));
2311 if (err) {
2312 free_colors(&s->colors);
2313 goto done;
2315 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2316 get_color_value("TOG_COLOR_DATE"));
2317 if (err) {
2318 free_colors(&s->colors);
2319 goto done;
2323 view->show = show_log_view;
2324 view->input = input_log_view;
2325 view->close = close_log_view;
2326 view->search_start = search_start_log_view;
2327 view->search_next = search_next_log_view;
2329 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2330 if (err)
2331 goto done;
2332 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2333 !s->log_branches);
2334 if (err)
2335 goto done;
2336 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2337 s->repo, NULL, NULL);
2338 if (err)
2339 goto done;
2341 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2342 if (errcode) {
2343 err = got_error_set_errno(errcode, "pthread_cond_init");
2344 goto done;
2346 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2347 if (errcode) {
2348 err = got_error_set_errno(errcode, "pthread_cond_init");
2349 goto done;
2352 s->thread_args.commits_needed = view->nlines;
2353 s->thread_args.graph = thread_graph;
2354 s->thread_args.commits = &s->commits;
2355 s->thread_args.in_repo_path = s->in_repo_path;
2356 s->thread_args.start_id = s->start_id;
2357 s->thread_args.repo = thread_repo;
2358 s->thread_args.log_complete = 0;
2359 s->thread_args.quit = &s->quit;
2360 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2361 s->thread_args.selected_entry = &s->selected_entry;
2362 s->thread_args.searching = &view->searching;
2363 s->thread_args.search_next_done = &view->search_next_done;
2364 s->thread_args.regex = &view->regex;
2365 done:
2366 if (err)
2367 close_log_view(view);
2368 return err;
2371 static const struct got_error *
2372 show_log_view(struct tog_view *view)
2374 const struct got_error *err;
2375 struct tog_log_view_state *s = &view->state.log;
2377 if (s->thread == 0) { //NULL) {
2378 int errcode = pthread_create(&s->thread, NULL, log_thread,
2379 &s->thread_args);
2380 if (errcode)
2381 return got_error_set_errno(errcode, "pthread_create");
2382 if (s->thread_args.commits_needed > 0) {
2383 err = trigger_log_thread(view, 1);
2384 if (err)
2385 return err;
2389 return draw_commits(view);
2392 static const struct got_error *
2393 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2395 const struct got_error *err = NULL;
2396 struct tog_log_view_state *s = &view->state.log;
2397 struct tog_view *diff_view = NULL, *tree_view = NULL;
2398 struct tog_view *ref_view = NULL;
2399 struct commit_queue_entry *entry;
2400 int begin_x = 0, n;
2402 if (s->thread_args.load_all) {
2403 if (ch == KEY_BACKSPACE)
2404 s->thread_args.load_all = 0;
2405 else if (s->thread_args.log_complete) {
2406 s->thread_args.load_all = 0;
2407 log_scroll_down(view, s->commits.ncommits);
2408 s->selected = MIN(view->nlines - 2,
2409 s->commits.ncommits - 1);
2410 select_commit(s);
2412 return NULL;
2415 switch (ch) {
2416 case 'q':
2417 s->quit = 1;
2418 break;
2419 case 'k':
2420 case KEY_UP:
2421 case '<':
2422 case ',':
2423 case CTRL('p'):
2424 if (s->first_displayed_entry == NULL)
2425 break;
2426 if (s->selected > 0)
2427 s->selected--;
2428 else
2429 log_scroll_up(s, 1);
2430 select_commit(s);
2431 break;
2432 case 'g':
2433 case KEY_HOME:
2434 s->selected = 0;
2435 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2436 select_commit(s);
2437 break;
2438 case KEY_PPAGE:
2439 case CTRL('b'):
2440 if (s->first_displayed_entry == NULL)
2441 break;
2442 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2443 s->selected = 0;
2444 else
2445 log_scroll_up(s, view->nlines - 1);
2446 select_commit(s);
2447 break;
2448 case 'j':
2449 case KEY_DOWN:
2450 case '>':
2451 case '.':
2452 case CTRL('n'):
2453 if (s->first_displayed_entry == NULL)
2454 break;
2455 if (s->selected < MIN(view->nlines - 2,
2456 s->commits.ncommits - 1))
2457 s->selected++;
2458 else {
2459 err = log_scroll_down(view, 1);
2460 if (err)
2461 break;
2463 select_commit(s);
2464 break;
2465 case 'G':
2466 case KEY_END: {
2467 /* We don't know yet how many commits, so we're forced to
2468 * traverse them all. */
2469 if (!s->thread_args.log_complete) {
2470 s->thread_args.load_all = 1;
2471 return trigger_log_thread(view, 0);
2474 s->selected = 0;
2475 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2476 for (n = 0; n < view->nlines - 1; n++) {
2477 if (entry == NULL)
2478 break;
2479 s->first_displayed_entry = entry;
2480 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2482 if (n > 0)
2483 s->selected = n - 1;
2484 select_commit(s);
2485 break;
2487 case KEY_NPAGE:
2488 case CTRL('f'): {
2489 struct commit_queue_entry *first;
2490 first = s->first_displayed_entry;
2491 if (first == NULL)
2492 break;
2493 err = log_scroll_down(view, view->nlines - 1);
2494 if (err)
2495 break;
2496 if (first == s->first_displayed_entry &&
2497 s->selected < MIN(view->nlines - 2,
2498 s->commits.ncommits - 1)) {
2499 /* can't scroll further down */
2500 s->selected = MIN(view->nlines - 2,
2501 s->commits.ncommits - 1);
2503 select_commit(s);
2504 break;
2506 case KEY_RESIZE:
2507 if (s->selected > view->nlines - 2)
2508 s->selected = view->nlines - 2;
2509 if (s->selected > s->commits.ncommits - 1)
2510 s->selected = s->commits.ncommits - 1;
2511 select_commit(s);
2512 if (s->commits.ncommits < view->nlines - 1 &&
2513 !s->thread_args.log_complete) {
2514 s->thread_args.commits_needed += (view->nlines - 1) -
2515 s->commits.ncommits;
2516 err = trigger_log_thread(view, 1);
2518 break;
2519 case KEY_ENTER:
2520 case ' ':
2521 case '\r':
2522 if (s->selected_entry == NULL)
2523 break;
2524 if (view_is_parent_view(view))
2525 begin_x = view_split_begin_x(view->begin_x);
2526 err = open_diff_view_for_commit(&diff_view, begin_x,
2527 s->selected_entry->commit, s->selected_entry->id,
2528 view, s->repo);
2529 if (err)
2530 break;
2531 view->focussed = 0;
2532 diff_view->focussed = 1;
2533 if (view_is_parent_view(view)) {
2534 err = view_close_child(view);
2535 if (err)
2536 return err;
2537 view_set_child(view, diff_view);
2538 view->focus_child = 1;
2539 } else
2540 *new_view = diff_view;
2541 break;
2542 case 't':
2543 if (s->selected_entry == NULL)
2544 break;
2545 if (view_is_parent_view(view))
2546 begin_x = view_split_begin_x(view->begin_x);
2547 err = browse_commit_tree(&tree_view, begin_x,
2548 s->selected_entry, s->in_repo_path, s->head_ref_name,
2549 s->repo);
2550 if (err)
2551 break;
2552 view->focussed = 0;
2553 tree_view->focussed = 1;
2554 if (view_is_parent_view(view)) {
2555 err = view_close_child(view);
2556 if (err)
2557 return err;
2558 view_set_child(view, tree_view);
2559 view->focus_child = 1;
2560 } else
2561 *new_view = tree_view;
2562 break;
2563 case KEY_BACKSPACE:
2564 case CTRL('l'):
2565 case 'B':
2566 if (ch == KEY_BACKSPACE &&
2567 got_path_is_root_dir(s->in_repo_path))
2568 break;
2569 err = stop_log_thread(s);
2570 if (err)
2571 return err;
2572 if (ch == KEY_BACKSPACE) {
2573 char *parent_path;
2574 err = got_path_dirname(&parent_path, s->in_repo_path);
2575 if (err)
2576 return err;
2577 free(s->in_repo_path);
2578 s->in_repo_path = parent_path;
2579 s->thread_args.in_repo_path = s->in_repo_path;
2580 } else if (ch == CTRL('l')) {
2581 struct got_object_id *start_id;
2582 err = got_repo_match_object_id(&start_id, NULL,
2583 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2584 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2585 if (err)
2586 return err;
2587 free(s->start_id);
2588 s->start_id = start_id;
2589 s->thread_args.start_id = s->start_id;
2590 } else /* 'B' */
2591 s->log_branches = !s->log_branches;
2593 err = got_repo_open(&s->thread_args.repo,
2594 got_repo_get_path(s->repo), NULL);
2595 if (err)
2596 return err;
2597 tog_free_refs();
2598 err = tog_load_refs(s->repo, 0);
2599 if (err)
2600 return err;
2601 err = got_commit_graph_open(&s->thread_args.graph,
2602 s->in_repo_path, !s->log_branches);
2603 if (err)
2604 return err;
2605 err = got_commit_graph_iter_start(s->thread_args.graph,
2606 s->start_id, s->repo, NULL, NULL);
2607 if (err)
2608 return err;
2609 free_commits(&s->commits);
2610 s->first_displayed_entry = NULL;
2611 s->last_displayed_entry = NULL;
2612 s->selected_entry = NULL;
2613 s->selected = 0;
2614 s->thread_args.log_complete = 0;
2615 s->quit = 0;
2616 s->thread_args.commits_needed = view->nlines;
2617 break;
2618 case 'r':
2619 if (view_is_parent_view(view))
2620 begin_x = view_split_begin_x(view->begin_x);
2621 ref_view = view_open(view->nlines, view->ncols,
2622 view->begin_y, begin_x, TOG_VIEW_REF);
2623 if (ref_view == NULL)
2624 return got_error_from_errno("view_open");
2625 err = open_ref_view(ref_view, s->repo);
2626 if (err) {
2627 view_close(ref_view);
2628 return err;
2630 view->focussed = 0;
2631 ref_view->focussed = 1;
2632 if (view_is_parent_view(view)) {
2633 err = view_close_child(view);
2634 if (err)
2635 return err;
2636 view_set_child(view, ref_view);
2637 view->focus_child = 1;
2638 } else
2639 *new_view = ref_view;
2640 break;
2641 default:
2642 break;
2645 return err;
2648 static const struct got_error *
2649 apply_unveil(const char *repo_path, const char *worktree_path)
2651 const struct got_error *error;
2653 #ifdef PROFILE
2654 if (unveil("gmon.out", "rwc") != 0)
2655 return got_error_from_errno2("unveil", "gmon.out");
2656 #endif
2657 if (repo_path && unveil(repo_path, "r") != 0)
2658 return got_error_from_errno2("unveil", repo_path);
2660 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2661 return got_error_from_errno2("unveil", worktree_path);
2663 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2664 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2666 error = got_privsep_unveil_exec_helpers();
2667 if (error != NULL)
2668 return error;
2670 if (unveil(NULL, NULL) != 0)
2671 return got_error_from_errno("unveil");
2673 return NULL;
2676 static void
2677 init_curses(void)
2679 initscr();
2680 cbreak();
2681 halfdelay(1); /* Do fast refresh while initial view is loading. */
2682 noecho();
2683 nonl();
2684 intrflush(stdscr, FALSE);
2685 keypad(stdscr, TRUE);
2686 curs_set(0);
2687 if (getenv("TOG_COLORS") != NULL) {
2688 start_color();
2689 use_default_colors();
2691 signal(SIGWINCH, tog_sigwinch);
2692 signal(SIGPIPE, tog_sigpipe);
2693 signal(SIGCONT, tog_sigcont);
2696 static const struct got_error *
2697 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2698 struct got_repository *repo, struct got_worktree *worktree)
2700 const struct got_error *err = NULL;
2702 if (argc == 0) {
2703 *in_repo_path = strdup("/");
2704 if (*in_repo_path == NULL)
2705 return got_error_from_errno("strdup");
2706 return NULL;
2709 if (worktree) {
2710 const char *prefix = got_worktree_get_path_prefix(worktree);
2711 char *p;
2713 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2714 if (err)
2715 return err;
2716 if (asprintf(in_repo_path, "%s%s%s", prefix,
2717 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2718 p) == -1) {
2719 err = got_error_from_errno("asprintf");
2720 *in_repo_path = NULL;
2722 free(p);
2723 } else
2724 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2726 return err;
2729 static const struct got_error *
2730 cmd_log(int argc, char *argv[])
2732 const struct got_error *error;
2733 struct got_repository *repo = NULL;
2734 struct got_worktree *worktree = NULL;
2735 struct got_object_id *start_id = NULL;
2736 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2737 char *start_commit = NULL, *label = NULL;
2738 struct got_reference *ref = NULL;
2739 const char *head_ref_name = NULL;
2740 int ch, log_branches = 0;
2741 struct tog_view *view;
2743 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2744 switch (ch) {
2745 case 'b':
2746 log_branches = 1;
2747 break;
2748 case 'c':
2749 start_commit = optarg;
2750 break;
2751 case 'r':
2752 repo_path = realpath(optarg, NULL);
2753 if (repo_path == NULL)
2754 return got_error_from_errno2("realpath",
2755 optarg);
2756 break;
2757 default:
2758 usage_log();
2759 /* NOTREACHED */
2763 argc -= optind;
2764 argv += optind;
2766 if (argc > 1)
2767 usage_log();
2769 if (repo_path == NULL) {
2770 cwd = getcwd(NULL, 0);
2771 if (cwd == NULL)
2772 return got_error_from_errno("getcwd");
2773 error = got_worktree_open(&worktree, cwd);
2774 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2775 goto done;
2776 if (worktree)
2777 repo_path =
2778 strdup(got_worktree_get_repo_path(worktree));
2779 else
2780 repo_path = strdup(cwd);
2781 if (repo_path == NULL) {
2782 error = got_error_from_errno("strdup");
2783 goto done;
2787 error = got_repo_open(&repo, repo_path, NULL);
2788 if (error != NULL)
2789 goto done;
2791 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2792 repo, worktree);
2793 if (error)
2794 goto done;
2796 init_curses();
2798 error = apply_unveil(got_repo_get_path(repo),
2799 worktree ? got_worktree_get_root_path(worktree) : NULL);
2800 if (error)
2801 goto done;
2803 /* already loaded by tog_log_with_path()? */
2804 if (TAILQ_EMPTY(&tog_refs)) {
2805 error = tog_load_refs(repo, 0);
2806 if (error)
2807 goto done;
2810 if (start_commit == NULL) {
2811 error = got_repo_match_object_id(&start_id, &label,
2812 worktree ? got_worktree_get_head_ref_name(worktree) :
2813 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2814 if (error)
2815 goto done;
2816 head_ref_name = label;
2817 } else {
2818 error = got_ref_open(&ref, repo, start_commit, 0);
2819 if (error == NULL)
2820 head_ref_name = got_ref_get_name(ref);
2821 else if (error->code != GOT_ERR_NOT_REF)
2822 goto done;
2823 error = got_repo_match_object_id(&start_id, NULL,
2824 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2825 if (error)
2826 goto done;
2829 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2830 if (view == NULL) {
2831 error = got_error_from_errno("view_open");
2832 goto done;
2834 error = open_log_view(view, start_id, repo, head_ref_name,
2835 in_repo_path, log_branches);
2836 if (error)
2837 goto done;
2838 if (worktree) {
2839 /* Release work tree lock. */
2840 got_worktree_close(worktree);
2841 worktree = NULL;
2843 error = view_loop(view);
2844 done:
2845 free(in_repo_path);
2846 free(repo_path);
2847 free(cwd);
2848 free(start_id);
2849 free(label);
2850 if (ref)
2851 got_ref_close(ref);
2852 if (repo) {
2853 const struct got_error *close_err = got_repo_close(repo);
2854 if (error == NULL)
2855 error = close_err;
2857 if (worktree)
2858 got_worktree_close(worktree);
2859 tog_free_refs();
2860 return error;
2863 __dead static void
2864 usage_diff(void)
2866 endwin();
2867 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2868 "[-w] object1 object2\n", getprogname());
2869 exit(1);
2872 static int
2873 match_line(const char *line, regex_t *regex, size_t nmatch,
2874 regmatch_t *regmatch)
2876 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2879 struct tog_color *
2880 match_color(struct tog_colors *colors, const char *line)
2882 struct tog_color *tc = NULL;
2884 STAILQ_FOREACH(tc, colors, entry) {
2885 if (match_line(line, &tc->regex, 0, NULL))
2886 return tc;
2889 return NULL;
2892 static const struct got_error *
2893 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2894 WINDOW *window, regmatch_t *regmatch)
2896 const struct got_error *err = NULL;
2897 wchar_t *wline;
2898 int width;
2899 char *s;
2901 *wtotal = 0;
2903 s = strndup(line, regmatch->rm_so);
2904 if (s == NULL)
2905 return got_error_from_errno("strndup");
2907 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2908 if (err) {
2909 free(s);
2910 return err;
2912 waddwstr(window, wline);
2913 free(wline);
2914 free(s);
2915 wlimit -= width;
2916 *wtotal += width;
2918 if (wlimit > 0) {
2919 s = strndup(line + regmatch->rm_so,
2920 regmatch->rm_eo - regmatch->rm_so);
2921 if (s == NULL) {
2922 err = got_error_from_errno("strndup");
2923 free(s);
2924 return err;
2926 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2927 if (err) {
2928 free(s);
2929 return err;
2931 wattr_on(window, A_STANDOUT, NULL);
2932 waddwstr(window, wline);
2933 wattr_off(window, A_STANDOUT, NULL);
2934 free(wline);
2935 free(s);
2936 wlimit -= width;
2937 *wtotal += width;
2940 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2941 err = format_line(&wline, &width,
2942 line + regmatch->rm_eo, wlimit, col_tab_align);
2943 if (err)
2944 return err;
2945 waddwstr(window, wline);
2946 free(wline);
2947 *wtotal += width;
2950 return NULL;
2953 static const struct got_error *
2954 draw_file(struct tog_view *view, const char *header)
2956 struct tog_diff_view_state *s = &view->state.diff;
2957 regmatch_t *regmatch = &view->regmatch;
2958 const struct got_error *err;
2959 int nprinted = 0;
2960 char *line;
2961 size_t linesize = 0;
2962 ssize_t linelen;
2963 struct tog_color *tc;
2964 wchar_t *wline;
2965 int width;
2966 int max_lines = view->nlines;
2967 int nlines = s->nlines;
2968 off_t line_offset;
2970 line_offset = s->line_offsets[s->first_displayed_line - 1];
2971 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2972 return got_error_from_errno("fseek");
2974 werase(view->window);
2976 if (header) {
2977 if (asprintf(&line, "[%d/%d] %s",
2978 s->first_displayed_line - 1 + s->selected_line, nlines,
2979 header) == -1)
2980 return got_error_from_errno("asprintf");
2981 err = format_line(&wline, &width, line, view->ncols, 0);
2982 free(line);
2983 if (err)
2984 return err;
2986 if (view_needs_focus_indication(view))
2987 wstandout(view->window);
2988 waddwstr(view->window, wline);
2989 free(wline);
2990 wline = NULL;
2991 if (view_needs_focus_indication(view))
2992 wstandend(view->window);
2993 if (width <= view->ncols - 1)
2994 waddch(view->window, '\n');
2996 if (max_lines <= 1)
2997 return NULL;
2998 max_lines--;
3001 s->eof = 0;
3002 line = NULL;
3003 while (max_lines > 0 && nprinted < max_lines) {
3004 linelen = getline(&line, &linesize, s->f);
3005 if (linelen == -1) {
3006 if (feof(s->f)) {
3007 s->eof = 1;
3008 break;
3010 free(line);
3011 return got_ferror(s->f, GOT_ERR_IO);
3014 tc = match_color(&s->colors, line);
3015 if (tc)
3016 wattr_on(view->window,
3017 COLOR_PAIR(tc->colorpair), NULL);
3018 if (s->first_displayed_line + nprinted == s->matched_line &&
3019 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3020 err = add_matched_line(&width, line, view->ncols, 0,
3021 view->window, regmatch);
3022 if (err) {
3023 free(line);
3024 return err;
3026 } else {
3027 err = format_line(&wline, &width, line, view->ncols, 0);
3028 if (err) {
3029 free(line);
3030 return err;
3032 waddwstr(view->window, wline);
3033 free(wline);
3034 wline = NULL;
3036 if (tc)
3037 wattr_off(view->window,
3038 COLOR_PAIR(tc->colorpair), NULL);
3039 if (width <= view->ncols - 1)
3040 waddch(view->window, '\n');
3041 nprinted++;
3043 free(line);
3044 if (nprinted >= 1)
3045 s->last_displayed_line = s->first_displayed_line +
3046 (nprinted - 1);
3047 else
3048 s->last_displayed_line = s->first_displayed_line;
3050 view_vborder(view);
3052 if (s->eof) {
3053 while (nprinted < view->nlines) {
3054 waddch(view->window, '\n');
3055 nprinted++;
3058 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3059 if (err) {
3060 return err;
3063 wstandout(view->window);
3064 waddwstr(view->window, wline);
3065 free(wline);
3066 wline = NULL;
3067 wstandend(view->window);
3070 return NULL;
3073 static char *
3074 get_datestr(time_t *time, char *datebuf)
3076 struct tm mytm, *tm;
3077 char *p, *s;
3079 tm = gmtime_r(time, &mytm);
3080 if (tm == NULL)
3081 return NULL;
3082 s = asctime_r(tm, datebuf);
3083 if (s == NULL)
3084 return NULL;
3085 p = strchr(s, '\n');
3086 if (p)
3087 *p = '\0';
3088 return s;
3091 static const struct got_error *
3092 get_changed_paths(struct got_pathlist_head *paths,
3093 struct got_commit_object *commit, struct got_repository *repo)
3095 const struct got_error *err = NULL;
3096 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3097 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3098 struct got_object_qid *qid;
3100 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3101 if (qid != NULL) {
3102 struct got_commit_object *pcommit;
3103 err = got_object_open_as_commit(&pcommit, repo,
3104 qid->id);
3105 if (err)
3106 return err;
3108 tree_id1 = got_object_id_dup(
3109 got_object_commit_get_tree_id(pcommit));
3110 if (tree_id1 == NULL) {
3111 got_object_commit_close(pcommit);
3112 return got_error_from_errno("got_object_id_dup");
3114 got_object_commit_close(pcommit);
3118 if (tree_id1) {
3119 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3120 if (err)
3121 goto done;
3124 tree_id2 = got_object_commit_get_tree_id(commit);
3125 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3126 if (err)
3127 goto done;
3129 err = got_diff_tree(tree1, tree2, "", "", repo,
3130 got_diff_tree_collect_changed_paths, paths, 0);
3131 done:
3132 if (tree1)
3133 got_object_tree_close(tree1);
3134 if (tree2)
3135 got_object_tree_close(tree2);
3136 free(tree_id1);
3137 return err;
3140 static const struct got_error *
3141 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3143 off_t *p;
3145 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3146 if (p == NULL)
3147 return got_error_from_errno("reallocarray");
3148 *line_offsets = p;
3149 (*line_offsets)[*nlines] = off;
3150 (*nlines)++;
3151 return NULL;
3154 static const struct got_error *
3155 write_commit_info(off_t **line_offsets, size_t *nlines,
3156 struct got_object_id *commit_id, struct got_reflist_head *refs,
3157 struct got_repository *repo, FILE *outfile)
3159 const struct got_error *err = NULL;
3160 char datebuf[26], *datestr;
3161 struct got_commit_object *commit;
3162 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3163 time_t committer_time;
3164 const char *author, *committer;
3165 char *refs_str = NULL;
3166 struct got_pathlist_head changed_paths;
3167 struct got_pathlist_entry *pe;
3168 off_t outoff = 0;
3169 int n;
3171 TAILQ_INIT(&changed_paths);
3173 if (refs) {
3174 err = build_refs_str(&refs_str, refs, commit_id, repo);
3175 if (err)
3176 return err;
3179 err = got_object_open_as_commit(&commit, repo, commit_id);
3180 if (err)
3181 return err;
3183 err = got_object_id_str(&id_str, commit_id);
3184 if (err) {
3185 err = got_error_from_errno("got_object_id_str");
3186 goto done;
3189 err = add_line_offset(line_offsets, nlines, 0);
3190 if (err)
3191 goto done;
3193 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3194 refs_str ? refs_str : "", refs_str ? ")" : "");
3195 if (n < 0) {
3196 err = got_error_from_errno("fprintf");
3197 goto done;
3199 outoff += n;
3200 err = add_line_offset(line_offsets, nlines, outoff);
3201 if (err)
3202 goto done;
3204 n = fprintf(outfile, "from: %s\n",
3205 got_object_commit_get_author(commit));
3206 if (n < 0) {
3207 err = got_error_from_errno("fprintf");
3208 goto done;
3210 outoff += n;
3211 err = add_line_offset(line_offsets, nlines, outoff);
3212 if (err)
3213 goto done;
3215 committer_time = got_object_commit_get_committer_time(commit);
3216 datestr = get_datestr(&committer_time, datebuf);
3217 if (datestr) {
3218 n = fprintf(outfile, "date: %s UTC\n", datestr);
3219 if (n < 0) {
3220 err = got_error_from_errno("fprintf");
3221 goto done;
3223 outoff += n;
3224 err = add_line_offset(line_offsets, nlines, outoff);
3225 if (err)
3226 goto done;
3228 author = got_object_commit_get_author(commit);
3229 committer = got_object_commit_get_committer(commit);
3230 if (strcmp(author, committer) != 0) {
3231 n = fprintf(outfile, "via: %s\n", committer);
3232 if (n < 0) {
3233 err = got_error_from_errno("fprintf");
3234 goto done;
3236 outoff += n;
3237 err = add_line_offset(line_offsets, nlines, outoff);
3238 if (err)
3239 goto done;
3241 if (got_object_commit_get_nparents(commit) > 1) {
3242 const struct got_object_id_queue *parent_ids;
3243 struct got_object_qid *qid;
3244 int pn = 1;
3245 parent_ids = got_object_commit_get_parent_ids(commit);
3246 STAILQ_FOREACH(qid, parent_ids, entry) {
3247 err = got_object_id_str(&id_str, qid->id);
3248 if (err)
3249 goto done;
3250 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3251 if (n < 0) {
3252 err = got_error_from_errno("fprintf");
3253 goto done;
3255 outoff += n;
3256 err = add_line_offset(line_offsets, nlines, outoff);
3257 if (err)
3258 goto done;
3259 free(id_str);
3260 id_str = NULL;
3264 err = got_object_commit_get_logmsg(&logmsg, commit);
3265 if (err)
3266 goto done;
3267 s = logmsg;
3268 while ((line = strsep(&s, "\n")) != NULL) {
3269 n = fprintf(outfile, "%s\n", line);
3270 if (n < 0) {
3271 err = got_error_from_errno("fprintf");
3272 goto done;
3274 outoff += n;
3275 err = add_line_offset(line_offsets, nlines, outoff);
3276 if (err)
3277 goto done;
3280 err = get_changed_paths(&changed_paths, commit, repo);
3281 if (err)
3282 goto done;
3283 TAILQ_FOREACH(pe, &changed_paths, entry) {
3284 struct got_diff_changed_path *cp = pe->data;
3285 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3286 if (n < 0) {
3287 err = got_error_from_errno("fprintf");
3288 goto done;
3290 outoff += n;
3291 err = add_line_offset(line_offsets, nlines, outoff);
3292 if (err)
3293 goto done;
3294 free((char *)pe->path);
3295 free(pe->data);
3298 fputc('\n', outfile);
3299 outoff++;
3300 err = add_line_offset(line_offsets, nlines, outoff);
3301 done:
3302 got_pathlist_free(&changed_paths);
3303 free(id_str);
3304 free(logmsg);
3305 free(refs_str);
3306 got_object_commit_close(commit);
3307 if (err) {
3308 free(*line_offsets);
3309 *line_offsets = NULL;
3310 *nlines = 0;
3312 return err;
3315 static const struct got_error *
3316 create_diff(struct tog_diff_view_state *s)
3318 const struct got_error *err = NULL;
3319 FILE *f = NULL;
3320 int obj_type;
3322 free(s->line_offsets);
3323 s->line_offsets = malloc(sizeof(off_t));
3324 if (s->line_offsets == NULL)
3325 return got_error_from_errno("malloc");
3326 s->nlines = 0;
3328 f = got_opentemp();
3329 if (f == NULL) {
3330 err = got_error_from_errno("got_opentemp");
3331 goto done;
3333 if (s->f && fclose(s->f) == EOF) {
3334 err = got_error_from_errno("fclose");
3335 goto done;
3337 s->f = f;
3339 if (s->id1)
3340 err = got_object_get_type(&obj_type, s->repo, s->id1);
3341 else
3342 err = got_object_get_type(&obj_type, s->repo, s->id2);
3343 if (err)
3344 goto done;
3346 switch (obj_type) {
3347 case GOT_OBJ_TYPE_BLOB:
3348 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3349 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3350 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3351 break;
3352 case GOT_OBJ_TYPE_TREE:
3353 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3354 s->id1, s->id2, NULL, "", "", s->diff_context,
3355 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3356 break;
3357 case GOT_OBJ_TYPE_COMMIT: {
3358 const struct got_object_id_queue *parent_ids;
3359 struct got_object_qid *pid;
3360 struct got_commit_object *commit2;
3361 struct got_reflist_head *refs;
3363 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3364 if (err)
3365 goto done;
3366 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3367 /* Show commit info if we're diffing to a parent/root commit. */
3368 if (s->id1 == NULL) {
3369 err = write_commit_info(&s->line_offsets, &s->nlines,
3370 s->id2, refs, s->repo, s->f);
3371 if (err)
3372 goto done;
3373 } else {
3374 parent_ids = got_object_commit_get_parent_ids(commit2);
3375 STAILQ_FOREACH(pid, parent_ids, entry) {
3376 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3377 err = write_commit_info(
3378 &s->line_offsets, &s->nlines,
3379 s->id2, refs, s->repo, s->f);
3380 if (err)
3381 goto done;
3382 break;
3386 got_object_commit_close(commit2);
3388 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3389 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3390 s->force_text_diff, s->repo, s->f);
3391 break;
3393 default:
3394 err = got_error(GOT_ERR_OBJ_TYPE);
3395 break;
3397 if (err)
3398 goto done;
3399 done:
3400 if (s->f && fflush(s->f) != 0 && err == NULL)
3401 err = got_error_from_errno("fflush");
3402 return err;
3405 static void
3406 diff_view_indicate_progress(struct tog_view *view)
3408 mvwaddstr(view->window, 0, 0, "diffing...");
3409 update_panels();
3410 doupdate();
3413 static const struct got_error *
3414 search_start_diff_view(struct tog_view *view)
3416 struct tog_diff_view_state *s = &view->state.diff;
3418 s->matched_line = 0;
3419 return NULL;
3422 static const struct got_error *
3423 search_next_diff_view(struct tog_view *view)
3425 struct tog_diff_view_state *s = &view->state.diff;
3426 int lineno;
3427 char *line = NULL;
3428 size_t linesize = 0;
3429 ssize_t linelen;
3431 if (!view->searching) {
3432 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3433 return NULL;
3436 if (s->matched_line) {
3437 if (view->searching == TOG_SEARCH_FORWARD)
3438 lineno = s->matched_line + 1;
3439 else
3440 lineno = s->matched_line - 1;
3441 } else
3442 lineno = s->first_displayed_line;
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 s->matched_line = 0;
3713 diff_view_indicate_progress(view);
3714 err = create_diff(s);
3715 break;
3716 case 'g':
3717 case KEY_HOME:
3718 s->first_displayed_line = 1;
3719 break;
3720 case 'G':
3721 case KEY_END:
3722 if (s->eof)
3723 break;
3725 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3726 s->eof = 1;
3727 break;
3728 case 'k':
3729 case KEY_UP:
3730 case CTRL('p'):
3731 if (s->first_displayed_line > 1)
3732 s->first_displayed_line--;
3733 break;
3734 case KEY_PPAGE:
3735 case CTRL('b'):
3736 if (s->first_displayed_line == 1)
3737 break;
3738 i = 0;
3739 while (i++ < view->nlines - 1 &&
3740 s->first_displayed_line > 1)
3741 s->first_displayed_line--;
3742 break;
3743 case 'j':
3744 case KEY_DOWN:
3745 case CTRL('n'):
3746 if (!s->eof)
3747 s->first_displayed_line++;
3748 break;
3749 case KEY_NPAGE:
3750 case CTRL('f'):
3751 case ' ':
3752 if (s->eof)
3753 break;
3754 i = 0;
3755 while (!s->eof && i++ < view->nlines - 1) {
3756 linelen = getline(&line, &linesize, s->f);
3757 s->first_displayed_line++;
3758 if (linelen == -1) {
3759 if (feof(s->f)) {
3760 s->eof = 1;
3761 } else
3762 err = got_ferror(s->f, GOT_ERR_IO);
3763 break;
3766 free(line);
3767 break;
3768 case '[':
3769 if (s->diff_context > 0) {
3770 s->diff_context--;
3771 s->matched_line = 0;
3772 diff_view_indicate_progress(view);
3773 err = create_diff(s);
3774 if (s->first_displayed_line + view->nlines - 1 >
3775 s->nlines) {
3776 s->first_displayed_line = 1;
3777 s->last_displayed_line = view->nlines;
3780 break;
3781 case ']':
3782 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3783 s->diff_context++;
3784 s->matched_line = 0;
3785 diff_view_indicate_progress(view);
3786 err = create_diff(s);
3788 break;
3789 case '<':
3790 case ',':
3791 if (s->log_view == NULL)
3792 break;
3793 ls = &s->log_view->state.log;
3794 old_selected_entry = ls->selected_entry;
3796 err = input_log_view(NULL, s->log_view, KEY_UP);
3797 if (err)
3798 break;
3800 if (old_selected_entry == ls->selected_entry)
3801 break;
3803 err = set_selected_commit(s, ls->selected_entry);
3804 if (err)
3805 break;
3807 s->first_displayed_line = 1;
3808 s->last_displayed_line = view->nlines;
3809 s->matched_line = 0;
3811 diff_view_indicate_progress(view);
3812 err = create_diff(s);
3813 break;
3814 case '>':
3815 case '.':
3816 if (s->log_view == NULL)
3817 break;
3818 ls = &s->log_view->state.log;
3819 old_selected_entry = ls->selected_entry;
3821 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3822 if (err)
3823 break;
3825 if (old_selected_entry == ls->selected_entry)
3826 break;
3828 err = set_selected_commit(s, ls->selected_entry);
3829 if (err)
3830 break;
3832 s->first_displayed_line = 1;
3833 s->last_displayed_line = view->nlines;
3834 s->matched_line = 0;
3836 diff_view_indicate_progress(view);
3837 err = create_diff(s);
3838 break;
3839 default:
3840 break;
3843 return err;
3846 static const struct got_error *
3847 cmd_diff(int argc, char *argv[])
3849 const struct got_error *error = NULL;
3850 struct got_repository *repo = NULL;
3851 struct got_worktree *worktree = NULL;
3852 struct got_object_id *id1 = NULL, *id2 = NULL;
3853 char *repo_path = NULL, *cwd = NULL;
3854 char *id_str1 = NULL, *id_str2 = NULL;
3855 char *label1 = NULL, *label2 = NULL;
3856 int diff_context = 3, ignore_whitespace = 0;
3857 int ch, force_text_diff = 0;
3858 const char *errstr;
3859 struct tog_view *view;
3861 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3862 switch (ch) {
3863 case 'a':
3864 force_text_diff = 1;
3865 break;
3866 case 'C':
3867 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3868 &errstr);
3869 if (errstr != NULL)
3870 err(1, "-C option %s", errstr);
3871 break;
3872 case 'r':
3873 repo_path = realpath(optarg, NULL);
3874 if (repo_path == NULL)
3875 return got_error_from_errno2("realpath",
3876 optarg);
3877 got_path_strip_trailing_slashes(repo_path);
3878 break;
3879 case 'w':
3880 ignore_whitespace = 1;
3881 break;
3882 default:
3883 usage_diff();
3884 /* NOTREACHED */
3888 argc -= optind;
3889 argv += optind;
3891 if (argc == 0) {
3892 usage_diff(); /* TODO show local worktree changes */
3893 } else if (argc == 2) {
3894 id_str1 = argv[0];
3895 id_str2 = argv[1];
3896 } else
3897 usage_diff();
3899 if (repo_path == NULL) {
3900 cwd = getcwd(NULL, 0);
3901 if (cwd == NULL)
3902 return got_error_from_errno("getcwd");
3903 error = got_worktree_open(&worktree, cwd);
3904 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3905 goto done;
3906 if (worktree)
3907 repo_path =
3908 strdup(got_worktree_get_repo_path(worktree));
3909 else
3910 repo_path = strdup(cwd);
3911 if (repo_path == NULL) {
3912 error = got_error_from_errno("strdup");
3913 goto done;
3917 error = got_repo_open(&repo, repo_path, NULL);
3918 if (error)
3919 goto done;
3921 init_curses();
3923 error = apply_unveil(got_repo_get_path(repo), NULL);
3924 if (error)
3925 goto done;
3927 error = tog_load_refs(repo, 0);
3928 if (error)
3929 goto done;
3931 error = got_repo_match_object_id(&id1, &label1, id_str1,
3932 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3933 if (error)
3934 goto done;
3936 error = got_repo_match_object_id(&id2, &label2, id_str2,
3937 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3938 if (error)
3939 goto done;
3941 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3942 if (view == NULL) {
3943 error = got_error_from_errno("view_open");
3944 goto done;
3946 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3947 ignore_whitespace, force_text_diff, NULL, repo);
3948 if (error)
3949 goto done;
3950 error = view_loop(view);
3951 done:
3952 free(label1);
3953 free(label2);
3954 free(repo_path);
3955 free(cwd);
3956 if (repo) {
3957 const struct got_error *close_err = got_repo_close(repo);
3958 if (error == NULL)
3959 error = close_err;
3961 if (worktree)
3962 got_worktree_close(worktree);
3963 tog_free_refs();
3964 return error;
3967 __dead static void
3968 usage_blame(void)
3970 endwin();
3971 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3972 getprogname());
3973 exit(1);
3976 struct tog_blame_line {
3977 int annotated;
3978 struct got_object_id *id;
3981 static const struct got_error *
3982 draw_blame(struct tog_view *view)
3984 struct tog_blame_view_state *s = &view->state.blame;
3985 struct tog_blame *blame = &s->blame;
3986 regmatch_t *regmatch = &view->regmatch;
3987 const struct got_error *err;
3988 int lineno = 0, nprinted = 0;
3989 char *line = NULL;
3990 size_t linesize = 0;
3991 ssize_t linelen;
3992 wchar_t *wline;
3993 int width;
3994 struct tog_blame_line *blame_line;
3995 struct got_object_id *prev_id = NULL;
3996 char *id_str;
3997 struct tog_color *tc;
3999 err = got_object_id_str(&id_str, s->blamed_commit->id);
4000 if (err)
4001 return err;
4003 rewind(blame->f);
4004 werase(view->window);
4006 if (asprintf(&line, "commit %s", id_str) == -1) {
4007 err = got_error_from_errno("asprintf");
4008 free(id_str);
4009 return err;
4012 err = format_line(&wline, &width, line, view->ncols, 0);
4013 free(line);
4014 line = NULL;
4015 if (err)
4016 return err;
4017 if (view_needs_focus_indication(view))
4018 wstandout(view->window);
4019 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4020 if (tc)
4021 wattr_on(view->window,
4022 COLOR_PAIR(tc->colorpair), NULL);
4023 waddwstr(view->window, wline);
4024 if (tc)
4025 wattr_off(view->window,
4026 COLOR_PAIR(tc->colorpair), NULL);
4027 if (view_needs_focus_indication(view))
4028 wstandend(view->window);
4029 free(wline);
4030 wline = NULL;
4031 if (width < view->ncols - 1)
4032 waddch(view->window, '\n');
4034 if (asprintf(&line, "[%d/%d] %s%s",
4035 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4036 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4037 free(id_str);
4038 return got_error_from_errno("asprintf");
4040 free(id_str);
4041 err = format_line(&wline, &width, line, view->ncols, 0);
4042 free(line);
4043 line = NULL;
4044 if (err)
4045 return err;
4046 waddwstr(view->window, wline);
4047 free(wline);
4048 wline = NULL;
4049 if (width < view->ncols - 1)
4050 waddch(view->window, '\n');
4052 s->eof = 0;
4053 while (nprinted < view->nlines - 2) {
4054 linelen = getline(&line, &linesize, blame->f);
4055 if (linelen == -1) {
4056 if (feof(blame->f)) {
4057 s->eof = 1;
4058 break;
4060 free(line);
4061 return got_ferror(blame->f, GOT_ERR_IO);
4063 if (++lineno < s->first_displayed_line)
4064 continue;
4066 if (view->focussed && nprinted == s->selected_line - 1)
4067 wstandout(view->window);
4069 if (blame->nlines > 0) {
4070 blame_line = &blame->lines[lineno - 1];
4071 if (blame_line->annotated && prev_id &&
4072 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4073 !(view->focussed &&
4074 nprinted == s->selected_line - 1)) {
4075 waddstr(view->window, " ");
4076 } else if (blame_line->annotated) {
4077 char *id_str;
4078 err = got_object_id_str(&id_str, blame_line->id);
4079 if (err) {
4080 free(line);
4081 return err;
4083 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4084 if (tc)
4085 wattr_on(view->window,
4086 COLOR_PAIR(tc->colorpair), NULL);
4087 wprintw(view->window, "%.8s", id_str);
4088 if (tc)
4089 wattr_off(view->window,
4090 COLOR_PAIR(tc->colorpair), NULL);
4091 free(id_str);
4092 prev_id = blame_line->id;
4093 } else {
4094 waddstr(view->window, "........");
4095 prev_id = NULL;
4097 } else {
4098 waddstr(view->window, "........");
4099 prev_id = NULL;
4102 if (view->focussed && nprinted == s->selected_line - 1)
4103 wstandend(view->window);
4104 waddstr(view->window, " ");
4106 if (view->ncols <= 9) {
4107 width = 9;
4108 wline = wcsdup(L"");
4109 if (wline == NULL) {
4110 err = got_error_from_errno("wcsdup");
4111 free(line);
4112 return err;
4114 } else if (s->first_displayed_line + nprinted ==
4115 s->matched_line &&
4116 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4117 err = add_matched_line(&width, line, view->ncols - 9, 9,
4118 view->window, regmatch);
4119 if (err) {
4120 free(line);
4121 return err;
4123 width += 9;
4124 } else {
4125 err = format_line(&wline, &width, line,
4126 view->ncols - 9, 9);
4127 waddwstr(view->window, wline);
4128 free(wline);
4129 wline = NULL;
4130 width += 9;
4133 if (width <= view->ncols - 1)
4134 waddch(view->window, '\n');
4135 if (++nprinted == 1)
4136 s->first_displayed_line = lineno;
4138 free(line);
4139 s->last_displayed_line = lineno;
4141 view_vborder(view);
4143 return NULL;
4146 static const struct got_error *
4147 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4149 const struct got_error *err = NULL;
4150 struct tog_blame_cb_args *a = arg;
4151 struct tog_blame_line *line;
4152 int errcode;
4154 if (nlines != a->nlines ||
4155 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4156 return got_error(GOT_ERR_RANGE);
4158 errcode = pthread_mutex_lock(&tog_mutex);
4159 if (errcode)
4160 return got_error_set_errno(errcode, "pthread_mutex_lock");
4162 if (*a->quit) { /* user has quit the blame view */
4163 err = got_error(GOT_ERR_ITER_COMPLETED);
4164 goto done;
4167 if (lineno == -1)
4168 goto done; /* no change in this commit */
4170 line = &a->lines[lineno - 1];
4171 if (line->annotated)
4172 goto done;
4174 line->id = got_object_id_dup(id);
4175 if (line->id == NULL) {
4176 err = got_error_from_errno("got_object_id_dup");
4177 goto done;
4179 line->annotated = 1;
4180 done:
4181 errcode = pthread_mutex_unlock(&tog_mutex);
4182 if (errcode)
4183 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4184 return err;
4187 static void *
4188 blame_thread(void *arg)
4190 const struct got_error *err, *close_err;
4191 struct tog_blame_thread_args *ta = arg;
4192 struct tog_blame_cb_args *a = ta->cb_args;
4193 int errcode;
4195 err = block_signals_used_by_main_thread();
4196 if (err)
4197 return (void *)err;
4199 err = got_blame(ta->path, a->commit_id, ta->repo,
4200 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4201 if (err && err->code == GOT_ERR_CANCELLED)
4202 err = NULL;
4204 errcode = pthread_mutex_lock(&tog_mutex);
4205 if (errcode)
4206 return (void *)got_error_set_errno(errcode,
4207 "pthread_mutex_lock");
4209 close_err = got_repo_close(ta->repo);
4210 if (err == NULL)
4211 err = close_err;
4212 ta->repo = NULL;
4213 *ta->complete = 1;
4215 errcode = pthread_mutex_unlock(&tog_mutex);
4216 if (errcode && err == NULL)
4217 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4219 return (void *)err;
4222 static struct got_object_id *
4223 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4224 int first_displayed_line, int selected_line)
4226 struct tog_blame_line *line;
4228 if (nlines <= 0)
4229 return NULL;
4231 line = &lines[first_displayed_line - 1 + selected_line - 1];
4232 if (!line->annotated)
4233 return NULL;
4235 return line->id;
4238 static const struct got_error *
4239 stop_blame(struct tog_blame *blame)
4241 const struct got_error *err = NULL;
4242 int i;
4244 if (blame->thread) {
4245 int errcode;
4246 errcode = pthread_mutex_unlock(&tog_mutex);
4247 if (errcode)
4248 return got_error_set_errno(errcode,
4249 "pthread_mutex_unlock");
4250 errcode = pthread_join(blame->thread, (void **)&err);
4251 if (errcode)
4252 return got_error_set_errno(errcode, "pthread_join");
4253 errcode = pthread_mutex_lock(&tog_mutex);
4254 if (errcode)
4255 return got_error_set_errno(errcode,
4256 "pthread_mutex_lock");
4257 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4258 err = NULL;
4259 blame->thread = 0; //NULL;
4261 if (blame->thread_args.repo) {
4262 const struct got_error *close_err;
4263 close_err = got_repo_close(blame->thread_args.repo);
4264 if (err == NULL)
4265 err = close_err;
4266 blame->thread_args.repo = NULL;
4268 if (blame->f) {
4269 if (fclose(blame->f) == EOF && err == NULL)
4270 err = got_error_from_errno("fclose");
4271 blame->f = NULL;
4273 if (blame->lines) {
4274 for (i = 0; i < blame->nlines; i++)
4275 free(blame->lines[i].id);
4276 free(blame->lines);
4277 blame->lines = NULL;
4279 free(blame->cb_args.commit_id);
4280 blame->cb_args.commit_id = NULL;
4282 return err;
4285 static const struct got_error *
4286 cancel_blame_view(void *arg)
4288 const struct got_error *err = NULL;
4289 int *done = arg;
4290 int errcode;
4292 errcode = pthread_mutex_lock(&tog_mutex);
4293 if (errcode)
4294 return got_error_set_errno(errcode,
4295 "pthread_mutex_unlock");
4297 if (*done)
4298 err = got_error(GOT_ERR_CANCELLED);
4300 errcode = pthread_mutex_unlock(&tog_mutex);
4301 if (errcode)
4302 return got_error_set_errno(errcode,
4303 "pthread_mutex_lock");
4305 return err;
4308 static const struct got_error *
4309 run_blame(struct tog_view *view)
4311 struct tog_blame_view_state *s = &view->state.blame;
4312 struct tog_blame *blame = &s->blame;
4313 const struct got_error *err = NULL;
4314 struct got_blob_object *blob = NULL;
4315 struct got_repository *thread_repo = NULL;
4316 struct got_object_id *obj_id = NULL;
4317 int obj_type;
4319 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4320 s->path);
4321 if (err)
4322 return err;
4324 err = got_object_get_type(&obj_type, s->repo, obj_id);
4325 if (err)
4326 goto done;
4328 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4329 err = got_error(GOT_ERR_OBJ_TYPE);
4330 goto done;
4333 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4334 if (err)
4335 goto done;
4336 blame->f = got_opentemp();
4337 if (blame->f == NULL) {
4338 err = got_error_from_errno("got_opentemp");
4339 goto done;
4341 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4342 &blame->line_offsets, blame->f, blob);
4343 if (err)
4344 goto done;
4345 if (blame->nlines == 0) {
4346 s->blame_complete = 1;
4347 goto done;
4350 /* Don't include \n at EOF in the blame line count. */
4351 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4352 blame->nlines--;
4354 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4355 if (blame->lines == NULL) {
4356 err = got_error_from_errno("calloc");
4357 goto done;
4360 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4361 if (err)
4362 goto done;
4364 blame->cb_args.view = view;
4365 blame->cb_args.lines = blame->lines;
4366 blame->cb_args.nlines = blame->nlines;
4367 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4368 if (blame->cb_args.commit_id == NULL) {
4369 err = got_error_from_errno("got_object_id_dup");
4370 goto done;
4372 blame->cb_args.quit = &s->done;
4374 blame->thread_args.path = s->path;
4375 blame->thread_args.repo = thread_repo;
4376 blame->thread_args.cb_args = &blame->cb_args;
4377 blame->thread_args.complete = &s->blame_complete;
4378 blame->thread_args.cancel_cb = cancel_blame_view;
4379 blame->thread_args.cancel_arg = &s->done;
4380 s->blame_complete = 0;
4382 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4383 s->first_displayed_line = 1;
4384 s->last_displayed_line = view->nlines;
4385 s->selected_line = 1;
4387 s->matched_line = 0;
4389 done:
4390 if (blob)
4391 got_object_blob_close(blob);
4392 free(obj_id);
4393 if (err)
4394 stop_blame(blame);
4395 return err;
4398 static const struct got_error *
4399 open_blame_view(struct tog_view *view, char *path,
4400 struct got_object_id *commit_id, struct got_repository *repo)
4402 const struct got_error *err = NULL;
4403 struct tog_blame_view_state *s = &view->state.blame;
4405 STAILQ_INIT(&s->blamed_commits);
4407 s->path = strdup(path);
4408 if (s->path == NULL)
4409 return got_error_from_errno("strdup");
4411 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4412 if (err) {
4413 free(s->path);
4414 return err;
4417 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4418 s->first_displayed_line = 1;
4419 s->last_displayed_line = view->nlines;
4420 s->selected_line = 1;
4421 s->blame_complete = 0;
4422 s->repo = repo;
4423 s->commit_id = commit_id;
4424 memset(&s->blame, 0, sizeof(s->blame));
4426 STAILQ_INIT(&s->colors);
4427 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4428 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4429 get_color_value("TOG_COLOR_COMMIT"));
4430 if (err)
4431 return err;
4434 view->show = show_blame_view;
4435 view->input = input_blame_view;
4436 view->close = close_blame_view;
4437 view->search_start = search_start_blame_view;
4438 view->search_next = search_next_blame_view;
4440 return run_blame(view);
4443 static const struct got_error *
4444 close_blame_view(struct tog_view *view)
4446 const struct got_error *err = NULL;
4447 struct tog_blame_view_state *s = &view->state.blame;
4449 if (s->blame.thread)
4450 err = stop_blame(&s->blame);
4452 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4453 struct got_object_qid *blamed_commit;
4454 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4455 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4456 got_object_qid_free(blamed_commit);
4459 free(s->path);
4460 free_colors(&s->colors);
4462 return err;
4465 static const struct got_error *
4466 search_start_blame_view(struct tog_view *view)
4468 struct tog_blame_view_state *s = &view->state.blame;
4470 s->matched_line = 0;
4471 return NULL;
4474 static const struct got_error *
4475 search_next_blame_view(struct tog_view *view)
4477 struct tog_blame_view_state *s = &view->state.blame;
4478 int lineno;
4479 char *line = NULL;
4480 size_t linesize = 0;
4481 ssize_t linelen;
4483 if (!view->searching) {
4484 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4485 return NULL;
4488 if (s->matched_line) {
4489 if (view->searching == TOG_SEARCH_FORWARD)
4490 lineno = s->matched_line + 1;
4491 else
4492 lineno = s->matched_line - 1;
4493 } else
4494 lineno = s->first_displayed_line - 1 + s->selected_line;
4496 while (1) {
4497 off_t offset;
4499 if (lineno <= 0 || lineno > s->blame.nlines) {
4500 if (s->matched_line == 0) {
4501 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4502 break;
4505 if (view->searching == TOG_SEARCH_FORWARD)
4506 lineno = 1;
4507 else
4508 lineno = s->blame.nlines;
4511 offset = s->blame.line_offsets[lineno - 1];
4512 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4513 free(line);
4514 return got_error_from_errno("fseeko");
4516 linelen = getline(&line, &linesize, s->blame.f);
4517 if (linelen != -1 &&
4518 match_line(line, &view->regex, 1, &view->regmatch)) {
4519 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4520 s->matched_line = lineno;
4521 break;
4523 if (view->searching == TOG_SEARCH_FORWARD)
4524 lineno++;
4525 else
4526 lineno--;
4528 free(line);
4530 if (s->matched_line) {
4531 s->first_displayed_line = s->matched_line;
4532 s->selected_line = 1;
4535 return NULL;
4538 static const struct got_error *
4539 show_blame_view(struct tog_view *view)
4541 const struct got_error *err = NULL;
4542 struct tog_blame_view_state *s = &view->state.blame;
4543 int errcode;
4545 if (s->blame.thread == 0 && !s->blame_complete) {
4546 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4547 &s->blame.thread_args);
4548 if (errcode)
4549 return got_error_set_errno(errcode, "pthread_create");
4551 halfdelay(1); /* fast refresh while annotating */
4554 if (s->blame_complete)
4555 halfdelay(10); /* disable fast refresh */
4557 err = draw_blame(view);
4559 view_vborder(view);
4560 return err;
4563 static const struct got_error *
4564 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4566 const struct got_error *err = NULL, *thread_err = NULL;
4567 struct tog_view *diff_view;
4568 struct tog_blame_view_state *s = &view->state.blame;
4569 int begin_x = 0;
4571 switch (ch) {
4572 case 'q':
4573 s->done = 1;
4574 break;
4575 case 'g':
4576 case KEY_HOME:
4577 s->selected_line = 1;
4578 s->first_displayed_line = 1;
4579 break;
4580 case 'G':
4581 case KEY_END:
4582 if (s->blame.nlines < view->nlines - 2) {
4583 s->selected_line = s->blame.nlines;
4584 s->first_displayed_line = 1;
4585 } else {
4586 s->selected_line = view->nlines - 2;
4587 s->first_displayed_line = s->blame.nlines -
4588 (view->nlines - 3);
4590 break;
4591 case 'k':
4592 case KEY_UP:
4593 case CTRL('p'):
4594 if (s->selected_line > 1)
4595 s->selected_line--;
4596 else if (s->selected_line == 1 &&
4597 s->first_displayed_line > 1)
4598 s->first_displayed_line--;
4599 break;
4600 case KEY_PPAGE:
4601 case CTRL('b'):
4602 if (s->first_displayed_line == 1) {
4603 s->selected_line = 1;
4604 break;
4606 if (s->first_displayed_line > view->nlines - 2)
4607 s->first_displayed_line -=
4608 (view->nlines - 2);
4609 else
4610 s->first_displayed_line = 1;
4611 break;
4612 case 'j':
4613 case KEY_DOWN:
4614 case CTRL('n'):
4615 if (s->selected_line < view->nlines - 2 &&
4616 s->first_displayed_line +
4617 s->selected_line <= s->blame.nlines)
4618 s->selected_line++;
4619 else if (s->last_displayed_line <
4620 s->blame.nlines)
4621 s->first_displayed_line++;
4622 break;
4623 case 'b':
4624 case 'p': {
4625 struct got_object_id *id = NULL;
4626 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4627 s->first_displayed_line, s->selected_line);
4628 if (id == NULL)
4629 break;
4630 if (ch == 'p') {
4631 struct got_commit_object *commit;
4632 struct got_object_qid *pid;
4633 struct got_object_id *blob_id = NULL;
4634 int obj_type;
4635 err = got_object_open_as_commit(&commit,
4636 s->repo, id);
4637 if (err)
4638 break;
4639 pid = STAILQ_FIRST(
4640 got_object_commit_get_parent_ids(commit));
4641 if (pid == NULL) {
4642 got_object_commit_close(commit);
4643 break;
4645 /* Check if path history ends here. */
4646 err = got_object_id_by_path(&blob_id, s->repo,
4647 pid->id, s->path);
4648 if (err) {
4649 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4650 err = NULL;
4651 got_object_commit_close(commit);
4652 break;
4654 err = got_object_get_type(&obj_type, s->repo,
4655 blob_id);
4656 free(blob_id);
4657 /* Can't blame non-blob type objects. */
4658 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4659 got_object_commit_close(commit);
4660 break;
4662 err = got_object_qid_alloc(&s->blamed_commit,
4663 pid->id);
4664 got_object_commit_close(commit);
4665 } else {
4666 if (got_object_id_cmp(id,
4667 s->blamed_commit->id) == 0)
4668 break;
4669 err = got_object_qid_alloc(&s->blamed_commit,
4670 id);
4672 if (err)
4673 break;
4674 s->done = 1;
4675 thread_err = stop_blame(&s->blame);
4676 s->done = 0;
4677 if (thread_err)
4678 break;
4679 STAILQ_INSERT_HEAD(&s->blamed_commits,
4680 s->blamed_commit, entry);
4681 err = run_blame(view);
4682 if (err)
4683 break;
4684 break;
4686 case 'B': {
4687 struct got_object_qid *first;
4688 first = STAILQ_FIRST(&s->blamed_commits);
4689 if (!got_object_id_cmp(first->id, s->commit_id))
4690 break;
4691 s->done = 1;
4692 thread_err = stop_blame(&s->blame);
4693 s->done = 0;
4694 if (thread_err)
4695 break;
4696 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4697 got_object_qid_free(s->blamed_commit);
4698 s->blamed_commit =
4699 STAILQ_FIRST(&s->blamed_commits);
4700 err = run_blame(view);
4701 if (err)
4702 break;
4703 break;
4705 case KEY_ENTER:
4706 case '\r': {
4707 struct got_object_id *id = NULL;
4708 struct got_object_qid *pid;
4709 struct got_commit_object *commit = NULL;
4710 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4711 s->first_displayed_line, s->selected_line);
4712 if (id == NULL)
4713 break;
4714 err = got_object_open_as_commit(&commit, s->repo, id);
4715 if (err)
4716 break;
4717 pid = STAILQ_FIRST(
4718 got_object_commit_get_parent_ids(commit));
4719 if (view_is_parent_view(view))
4720 begin_x = view_split_begin_x(view->begin_x);
4721 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4722 if (diff_view == NULL) {
4723 got_object_commit_close(commit);
4724 err = got_error_from_errno("view_open");
4725 break;
4727 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4728 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4729 got_object_commit_close(commit);
4730 if (err) {
4731 view_close(diff_view);
4732 break;
4734 view->focussed = 0;
4735 diff_view->focussed = 1;
4736 if (view_is_parent_view(view)) {
4737 err = view_close_child(view);
4738 if (err)
4739 break;
4740 view_set_child(view, diff_view);
4741 view->focus_child = 1;
4742 } else
4743 *new_view = diff_view;
4744 if (err)
4745 break;
4746 break;
4748 case KEY_NPAGE:
4749 case CTRL('f'):
4750 case ' ':
4751 if (s->last_displayed_line >= s->blame.nlines &&
4752 s->selected_line >= MIN(s->blame.nlines,
4753 view->nlines - 2)) {
4754 break;
4756 if (s->last_displayed_line >= s->blame.nlines &&
4757 s->selected_line < view->nlines - 2) {
4758 s->selected_line = MIN(s->blame.nlines,
4759 view->nlines - 2);
4760 break;
4762 if (s->last_displayed_line + view->nlines - 2
4763 <= s->blame.nlines)
4764 s->first_displayed_line +=
4765 view->nlines - 2;
4766 else
4767 s->first_displayed_line =
4768 s->blame.nlines -
4769 (view->nlines - 3);
4770 break;
4771 case KEY_RESIZE:
4772 if (s->selected_line > view->nlines - 2) {
4773 s->selected_line = MIN(s->blame.nlines,
4774 view->nlines - 2);
4776 break;
4777 default:
4778 break;
4780 return thread_err ? thread_err : err;
4783 static const struct got_error *
4784 cmd_blame(int argc, char *argv[])
4786 const struct got_error *error;
4787 struct got_repository *repo = NULL;
4788 struct got_worktree *worktree = NULL;
4789 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4790 char *link_target = NULL;
4791 struct got_object_id *commit_id = NULL;
4792 char *commit_id_str = NULL;
4793 int ch;
4794 struct tog_view *view;
4796 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4797 switch (ch) {
4798 case 'c':
4799 commit_id_str = optarg;
4800 break;
4801 case 'r':
4802 repo_path = realpath(optarg, NULL);
4803 if (repo_path == NULL)
4804 return got_error_from_errno2("realpath",
4805 optarg);
4806 break;
4807 default:
4808 usage_blame();
4809 /* NOTREACHED */
4813 argc -= optind;
4814 argv += optind;
4816 if (argc != 1)
4817 usage_blame();
4819 if (repo_path == NULL) {
4820 cwd = getcwd(NULL, 0);
4821 if (cwd == NULL)
4822 return got_error_from_errno("getcwd");
4823 error = got_worktree_open(&worktree, cwd);
4824 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4825 goto done;
4826 if (worktree)
4827 repo_path =
4828 strdup(got_worktree_get_repo_path(worktree));
4829 else
4830 repo_path = strdup(cwd);
4831 if (repo_path == NULL) {
4832 error = got_error_from_errno("strdup");
4833 goto done;
4837 error = got_repo_open(&repo, repo_path, NULL);
4838 if (error != NULL)
4839 goto done;
4841 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4842 worktree);
4843 if (error)
4844 goto done;
4846 init_curses();
4848 error = apply_unveil(got_repo_get_path(repo), NULL);
4849 if (error)
4850 goto done;
4852 error = tog_load_refs(repo, 0);
4853 if (error)
4854 goto done;
4856 if (commit_id_str == NULL) {
4857 struct got_reference *head_ref;
4858 error = got_ref_open(&head_ref, repo, worktree ?
4859 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4860 if (error != NULL)
4861 goto done;
4862 error = got_ref_resolve(&commit_id, repo, head_ref);
4863 got_ref_close(head_ref);
4864 } else {
4865 error = got_repo_match_object_id(&commit_id, NULL,
4866 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4868 if (error != NULL)
4869 goto done;
4871 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4872 if (view == NULL) {
4873 error = got_error_from_errno("view_open");
4874 goto done;
4877 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4878 commit_id, repo);
4879 if (error)
4880 goto done;
4882 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4883 commit_id, repo);
4884 if (error)
4885 goto done;
4886 if (worktree) {
4887 /* Release work tree lock. */
4888 got_worktree_close(worktree);
4889 worktree = NULL;
4891 error = view_loop(view);
4892 done:
4893 free(repo_path);
4894 free(in_repo_path);
4895 free(link_target);
4896 free(cwd);
4897 free(commit_id);
4898 if (worktree)
4899 got_worktree_close(worktree);
4900 if (repo) {
4901 const struct got_error *close_err = got_repo_close(repo);
4902 if (error == NULL)
4903 error = close_err;
4905 tog_free_refs();
4906 return error;
4909 static const struct got_error *
4910 draw_tree_entries(struct tog_view *view, const char *parent_path)
4912 struct tog_tree_view_state *s = &view->state.tree;
4913 const struct got_error *err = NULL;
4914 struct got_tree_entry *te;
4915 wchar_t *wline;
4916 struct tog_color *tc;
4917 int width, n, i, nentries;
4918 int limit = view->nlines;
4920 s->ndisplayed = 0;
4922 werase(view->window);
4924 if (limit == 0)
4925 return NULL;
4927 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4928 if (err)
4929 return err;
4930 if (view_needs_focus_indication(view))
4931 wstandout(view->window);
4932 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4933 if (tc)
4934 wattr_on(view->window,
4935 COLOR_PAIR(tc->colorpair), NULL);
4936 waddwstr(view->window, wline);
4937 if (tc)
4938 wattr_off(view->window,
4939 COLOR_PAIR(tc->colorpair), NULL);
4940 if (view_needs_focus_indication(view))
4941 wstandend(view->window);
4942 free(wline);
4943 wline = NULL;
4944 if (width < view->ncols - 1)
4945 waddch(view->window, '\n');
4946 if (--limit <= 0)
4947 return NULL;
4948 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4949 if (err)
4950 return err;
4951 waddwstr(view->window, wline);
4952 free(wline);
4953 wline = NULL;
4954 if (width < view->ncols - 1)
4955 waddch(view->window, '\n');
4956 if (--limit <= 0)
4957 return NULL;
4958 waddch(view->window, '\n');
4959 if (--limit <= 0)
4960 return NULL;
4962 if (s->first_displayed_entry == NULL) {
4963 te = got_object_tree_get_first_entry(s->tree);
4964 if (s->selected == 0) {
4965 if (view->focussed)
4966 wstandout(view->window);
4967 s->selected_entry = NULL;
4969 waddstr(view->window, " ..\n"); /* parent directory */
4970 if (s->selected == 0 && view->focussed)
4971 wstandend(view->window);
4972 s->ndisplayed++;
4973 if (--limit <= 0)
4974 return NULL;
4975 n = 1;
4976 } else {
4977 n = 0;
4978 te = s->first_displayed_entry;
4981 nentries = got_object_tree_get_nentries(s->tree);
4982 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4983 char *line = NULL, *id_str = NULL, *link_target = NULL;
4984 const char *modestr = "";
4985 mode_t mode;
4987 te = got_object_tree_get_entry(s->tree, i);
4988 mode = got_tree_entry_get_mode(te);
4990 if (s->show_ids) {
4991 err = got_object_id_str(&id_str,
4992 got_tree_entry_get_id(te));
4993 if (err)
4994 return got_error_from_errno(
4995 "got_object_id_str");
4997 if (got_object_tree_entry_is_submodule(te))
4998 modestr = "$";
4999 else if (S_ISLNK(mode)) {
5000 int i;
5002 err = got_tree_entry_get_symlink_target(&link_target,
5003 te, s->repo);
5004 if (err) {
5005 free(id_str);
5006 return err;
5008 for (i = 0; i < strlen(link_target); i++) {
5009 if (!isprint((unsigned char)link_target[i]))
5010 link_target[i] = '?';
5012 modestr = "@";
5014 else if (S_ISDIR(mode))
5015 modestr = "/";
5016 else if (mode & S_IXUSR)
5017 modestr = "*";
5018 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5019 got_tree_entry_get_name(te), modestr,
5020 link_target ? " -> ": "",
5021 link_target ? link_target : "") == -1) {
5022 free(id_str);
5023 free(link_target);
5024 return got_error_from_errno("asprintf");
5026 free(id_str);
5027 free(link_target);
5028 err = format_line(&wline, &width, line, view->ncols, 0);
5029 if (err) {
5030 free(line);
5031 break;
5033 if (n == s->selected) {
5034 if (view->focussed)
5035 wstandout(view->window);
5036 s->selected_entry = te;
5038 tc = match_color(&s->colors, line);
5039 if (tc)
5040 wattr_on(view->window,
5041 COLOR_PAIR(tc->colorpair), NULL);
5042 waddwstr(view->window, wline);
5043 if (tc)
5044 wattr_off(view->window,
5045 COLOR_PAIR(tc->colorpair), NULL);
5046 if (width < view->ncols - 1)
5047 waddch(view->window, '\n');
5048 if (n == s->selected && view->focussed)
5049 wstandend(view->window);
5050 free(line);
5051 free(wline);
5052 wline = NULL;
5053 n++;
5054 s->ndisplayed++;
5055 s->last_displayed_entry = te;
5056 if (--limit <= 0)
5057 break;
5060 return err;
5063 static void
5064 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5066 struct got_tree_entry *te;
5067 int isroot = s->tree == s->root;
5068 int i = 0;
5070 if (s->first_displayed_entry == NULL)
5071 return;
5073 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5074 while (i++ < maxscroll) {
5075 if (te == NULL) {
5076 if (!isroot)
5077 s->first_displayed_entry = NULL;
5078 break;
5080 s->first_displayed_entry = te;
5081 te = got_tree_entry_get_prev(s->tree, te);
5085 static void
5086 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5088 struct got_tree_entry *next, *last;
5089 int n = 0;
5091 if (s->first_displayed_entry)
5092 next = got_tree_entry_get_next(s->tree,
5093 s->first_displayed_entry);
5094 else
5095 next = got_object_tree_get_first_entry(s->tree);
5097 last = s->last_displayed_entry;
5098 while (next && last && n++ < maxscroll) {
5099 last = got_tree_entry_get_next(s->tree, last);
5100 if (last) {
5101 s->first_displayed_entry = next;
5102 next = got_tree_entry_get_next(s->tree, next);
5107 static const struct got_error *
5108 tree_entry_path(char **path, struct tog_parent_trees *parents,
5109 struct got_tree_entry *te)
5111 const struct got_error *err = NULL;
5112 struct tog_parent_tree *pt;
5113 size_t len = 2; /* for leading slash and NUL */
5115 TAILQ_FOREACH(pt, parents, entry)
5116 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5117 + 1 /* slash */;
5118 if (te)
5119 len += strlen(got_tree_entry_get_name(te));
5121 *path = calloc(1, len);
5122 if (path == NULL)
5123 return got_error_from_errno("calloc");
5125 (*path)[0] = '/';
5126 pt = TAILQ_LAST(parents, tog_parent_trees);
5127 while (pt) {
5128 const char *name = got_tree_entry_get_name(pt->selected_entry);
5129 if (strlcat(*path, name, len) >= len) {
5130 err = got_error(GOT_ERR_NO_SPACE);
5131 goto done;
5133 if (strlcat(*path, "/", len) >= len) {
5134 err = got_error(GOT_ERR_NO_SPACE);
5135 goto done;
5137 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5139 if (te) {
5140 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5141 err = got_error(GOT_ERR_NO_SPACE);
5142 goto done;
5145 done:
5146 if (err) {
5147 free(*path);
5148 *path = NULL;
5150 return err;
5153 static const struct got_error *
5154 blame_tree_entry(struct tog_view **new_view, int begin_x,
5155 struct got_tree_entry *te, struct tog_parent_trees *parents,
5156 struct got_object_id *commit_id, struct got_repository *repo)
5158 const struct got_error *err = NULL;
5159 char *path;
5160 struct tog_view *blame_view;
5162 *new_view = NULL;
5164 err = tree_entry_path(&path, parents, te);
5165 if (err)
5166 return err;
5168 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5169 if (blame_view == NULL) {
5170 err = got_error_from_errno("view_open");
5171 goto done;
5174 err = open_blame_view(blame_view, path, commit_id, repo);
5175 if (err) {
5176 if (err->code == GOT_ERR_CANCELLED)
5177 err = NULL;
5178 view_close(blame_view);
5179 } else
5180 *new_view = blame_view;
5181 done:
5182 free(path);
5183 return err;
5186 static const struct got_error *
5187 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5188 struct tog_tree_view_state *s)
5190 struct tog_view *log_view;
5191 const struct got_error *err = NULL;
5192 char *path;
5194 *new_view = NULL;
5196 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5197 if (log_view == NULL)
5198 return got_error_from_errno("view_open");
5200 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5201 if (err)
5202 return err;
5204 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5205 path, 0);
5206 if (err)
5207 view_close(log_view);
5208 else
5209 *new_view = log_view;
5210 free(path);
5211 return err;
5214 static const struct got_error *
5215 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5216 const char *head_ref_name, struct got_repository *repo)
5218 const struct got_error *err = NULL;
5219 char *commit_id_str = NULL;
5220 struct tog_tree_view_state *s = &view->state.tree;
5221 struct got_commit_object *commit = NULL;
5223 TAILQ_INIT(&s->parents);
5224 STAILQ_INIT(&s->colors);
5226 s->commit_id = got_object_id_dup(commit_id);
5227 if (s->commit_id == NULL)
5228 return got_error_from_errno("got_object_id_dup");
5230 err = got_object_open_as_commit(&commit, repo, commit_id);
5231 if (err)
5232 goto done;
5235 * The root is opened here and will be closed when the view is closed.
5236 * Any visited subtrees and their path-wise parents are opened and
5237 * closed on demand.
5239 err = got_object_open_as_tree(&s->root, repo,
5240 got_object_commit_get_tree_id(commit));
5241 if (err)
5242 goto done;
5243 s->tree = s->root;
5245 err = got_object_id_str(&commit_id_str, commit_id);
5246 if (err != NULL)
5247 goto done;
5249 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5250 err = got_error_from_errno("asprintf");
5251 goto done;
5254 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5255 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5256 if (head_ref_name) {
5257 s->head_ref_name = strdup(head_ref_name);
5258 if (s->head_ref_name == NULL) {
5259 err = got_error_from_errno("strdup");
5260 goto done;
5263 s->repo = repo;
5265 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5266 err = add_color(&s->colors, "\\$$",
5267 TOG_COLOR_TREE_SUBMODULE,
5268 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5269 if (err)
5270 goto done;
5271 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5272 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5273 if (err)
5274 goto done;
5275 err = add_color(&s->colors, "/$",
5276 TOG_COLOR_TREE_DIRECTORY,
5277 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5278 if (err)
5279 goto done;
5281 err = add_color(&s->colors, "\\*$",
5282 TOG_COLOR_TREE_EXECUTABLE,
5283 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5284 if (err)
5285 goto done;
5287 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5288 get_color_value("TOG_COLOR_COMMIT"));
5289 if (err)
5290 goto done;
5293 view->show = show_tree_view;
5294 view->input = input_tree_view;
5295 view->close = close_tree_view;
5296 view->search_start = search_start_tree_view;
5297 view->search_next = search_next_tree_view;
5298 done:
5299 free(commit_id_str);
5300 if (commit)
5301 got_object_commit_close(commit);
5302 if (err)
5303 close_tree_view(view);
5304 return err;
5307 static const struct got_error *
5308 close_tree_view(struct tog_view *view)
5310 struct tog_tree_view_state *s = &view->state.tree;
5312 free_colors(&s->colors);
5313 free(s->tree_label);
5314 s->tree_label = NULL;
5315 free(s->commit_id);
5316 s->commit_id = NULL;
5317 free(s->head_ref_name);
5318 s->head_ref_name = NULL;
5319 while (!TAILQ_EMPTY(&s->parents)) {
5320 struct tog_parent_tree *parent;
5321 parent = TAILQ_FIRST(&s->parents);
5322 TAILQ_REMOVE(&s->parents, parent, entry);
5323 if (parent->tree != s->root)
5324 got_object_tree_close(parent->tree);
5325 free(parent);
5328 if (s->tree != NULL && s->tree != s->root)
5329 got_object_tree_close(s->tree);
5330 if (s->root)
5331 got_object_tree_close(s->root);
5332 return NULL;
5335 static const struct got_error *
5336 search_start_tree_view(struct tog_view *view)
5338 struct tog_tree_view_state *s = &view->state.tree;
5340 s->matched_entry = NULL;
5341 return NULL;
5344 static int
5345 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5347 regmatch_t regmatch;
5349 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5350 0) == 0;
5353 static const struct got_error *
5354 search_next_tree_view(struct tog_view *view)
5356 struct tog_tree_view_state *s = &view->state.tree;
5357 struct got_tree_entry *te = NULL;
5359 if (!view->searching) {
5360 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5361 return NULL;
5364 if (s->matched_entry) {
5365 if (view->searching == TOG_SEARCH_FORWARD) {
5366 if (s->selected_entry)
5367 te = got_tree_entry_get_next(s->tree,
5368 s->selected_entry);
5369 else
5370 te = got_object_tree_get_first_entry(s->tree);
5371 } else {
5372 if (s->selected_entry == NULL)
5373 te = got_object_tree_get_last_entry(s->tree);
5374 else
5375 te = got_tree_entry_get_prev(s->tree,
5376 s->selected_entry);
5378 } else {
5379 if (s->selected_entry)
5380 te = s->selected_entry;
5381 else if (view->searching == TOG_SEARCH_FORWARD)
5382 te = got_object_tree_get_first_entry(s->tree);
5383 else
5384 te = got_object_tree_get_last_entry(s->tree);
5387 while (1) {
5388 if (te == NULL) {
5389 if (s->matched_entry == NULL) {
5390 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5391 return NULL;
5393 if (view->searching == TOG_SEARCH_FORWARD)
5394 te = got_object_tree_get_first_entry(s->tree);
5395 else
5396 te = got_object_tree_get_last_entry(s->tree);
5399 if (match_tree_entry(te, &view->regex)) {
5400 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5401 s->matched_entry = te;
5402 break;
5405 if (view->searching == TOG_SEARCH_FORWARD)
5406 te = got_tree_entry_get_next(s->tree, te);
5407 else
5408 te = got_tree_entry_get_prev(s->tree, te);
5411 if (s->matched_entry) {
5412 s->first_displayed_entry = s->matched_entry;
5413 s->selected = 0;
5416 return NULL;
5419 static const struct got_error *
5420 show_tree_view(struct tog_view *view)
5422 const struct got_error *err = NULL;
5423 struct tog_tree_view_state *s = &view->state.tree;
5424 char *parent_path;
5426 err = tree_entry_path(&parent_path, &s->parents, NULL);
5427 if (err)
5428 return err;
5430 err = draw_tree_entries(view, parent_path);
5431 free(parent_path);
5433 view_vborder(view);
5434 return err;
5437 static const struct got_error *
5438 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5440 const struct got_error *err = NULL;
5441 struct tog_tree_view_state *s = &view->state.tree;
5442 struct tog_view *log_view, *ref_view;
5443 struct got_tree_entry *te;
5444 int begin_x = 0, n;
5446 switch (ch) {
5447 case 'i':
5448 s->show_ids = !s->show_ids;
5449 break;
5450 case 'l':
5451 if (!s->selected_entry)
5452 break;
5453 if (view_is_parent_view(view))
5454 begin_x = view_split_begin_x(view->begin_x);
5455 err = log_selected_tree_entry(&log_view, begin_x, s);
5456 view->focussed = 0;
5457 log_view->focussed = 1;
5458 if (view_is_parent_view(view)) {
5459 err = view_close_child(view);
5460 if (err)
5461 return err;
5462 view_set_child(view, log_view);
5463 view->focus_child = 1;
5464 } else
5465 *new_view = log_view;
5466 break;
5467 case 'r':
5468 if (view_is_parent_view(view))
5469 begin_x = view_split_begin_x(view->begin_x);
5470 ref_view = view_open(view->nlines, view->ncols,
5471 view->begin_y, begin_x, TOG_VIEW_REF);
5472 if (ref_view == NULL)
5473 return got_error_from_errno("view_open");
5474 err = open_ref_view(ref_view, s->repo);
5475 if (err) {
5476 view_close(ref_view);
5477 return err;
5479 view->focussed = 0;
5480 ref_view->focussed = 1;
5481 if (view_is_parent_view(view)) {
5482 err = view_close_child(view);
5483 if (err)
5484 return err;
5485 view_set_child(view, ref_view);
5486 view->focus_child = 1;
5487 } else
5488 *new_view = ref_view;
5489 break;
5490 case 'g':
5491 case KEY_HOME:
5492 s->selected = 0;
5493 if (s->tree == s->root)
5494 s->first_displayed_entry =
5495 got_object_tree_get_first_entry(s->tree);
5496 else
5497 s->first_displayed_entry = NULL;
5498 break;
5499 case 'G':
5500 case KEY_END:
5501 s->selected = 0;
5502 te = got_object_tree_get_last_entry(s->tree);
5503 for (n = 0; n < view->nlines - 3; n++) {
5504 if (te == NULL) {
5505 if(s->tree != s->root) {
5506 s->first_displayed_entry = NULL;
5507 n++;
5509 break;
5511 s->first_displayed_entry = te;
5512 te = got_tree_entry_get_prev(s->tree, te);
5514 if (n > 0)
5515 s->selected = n - 1;
5516 break;
5517 case 'k':
5518 case KEY_UP:
5519 case CTRL('p'):
5520 if (s->selected > 0) {
5521 s->selected--;
5522 break;
5524 tree_scroll_up(s, 1);
5525 break;
5526 case KEY_PPAGE:
5527 case CTRL('b'):
5528 if (s->tree == s->root) {
5529 if (got_object_tree_get_first_entry(s->tree) ==
5530 s->first_displayed_entry)
5531 s->selected = 0;
5532 } else {
5533 if (s->first_displayed_entry == NULL)
5534 s->selected = 0;
5536 tree_scroll_up(s, MAX(0, view->nlines - 3));
5537 break;
5538 case 'j':
5539 case KEY_DOWN:
5540 case CTRL('n'):
5541 if (s->selected < s->ndisplayed - 1) {
5542 s->selected++;
5543 break;
5545 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5546 == NULL)
5547 /* can't scroll any further */
5548 break;
5549 tree_scroll_down(s, 1);
5550 break;
5551 case KEY_NPAGE:
5552 case CTRL('f'):
5553 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5554 == NULL) {
5555 /* can't scroll any further; move cursor down */
5556 if (s->selected < s->ndisplayed - 1)
5557 s->selected = s->ndisplayed - 1;
5558 break;
5560 tree_scroll_down(s, view->nlines - 3);
5561 break;
5562 case KEY_ENTER:
5563 case '\r':
5564 case KEY_BACKSPACE:
5565 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5566 struct tog_parent_tree *parent;
5567 /* user selected '..' */
5568 if (s->tree == s->root)
5569 break;
5570 parent = TAILQ_FIRST(&s->parents);
5571 TAILQ_REMOVE(&s->parents, parent,
5572 entry);
5573 got_object_tree_close(s->tree);
5574 s->tree = parent->tree;
5575 s->first_displayed_entry =
5576 parent->first_displayed_entry;
5577 s->selected_entry =
5578 parent->selected_entry;
5579 s->selected = parent->selected;
5580 free(parent);
5581 } else if (S_ISDIR(got_tree_entry_get_mode(
5582 s->selected_entry))) {
5583 struct got_tree_object *subtree;
5584 err = got_object_open_as_tree(&subtree, s->repo,
5585 got_tree_entry_get_id(s->selected_entry));
5586 if (err)
5587 break;
5588 err = tree_view_visit_subtree(s, subtree);
5589 if (err) {
5590 got_object_tree_close(subtree);
5591 break;
5593 } else if (S_ISREG(got_tree_entry_get_mode(
5594 s->selected_entry))) {
5595 struct tog_view *blame_view;
5596 int begin_x = view_is_parent_view(view) ?
5597 view_split_begin_x(view->begin_x) : 0;
5599 err = blame_tree_entry(&blame_view, begin_x,
5600 s->selected_entry, &s->parents,
5601 s->commit_id, s->repo);
5602 if (err)
5603 break;
5604 view->focussed = 0;
5605 blame_view->focussed = 1;
5606 if (view_is_parent_view(view)) {
5607 err = view_close_child(view);
5608 if (err)
5609 return err;
5610 view_set_child(view, blame_view);
5611 view->focus_child = 1;
5612 } else
5613 *new_view = blame_view;
5615 break;
5616 case KEY_RESIZE:
5617 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5618 s->selected = view->nlines - 4;
5619 break;
5620 default:
5621 break;
5624 return err;
5627 __dead static void
5628 usage_tree(void)
5630 endwin();
5631 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5632 getprogname());
5633 exit(1);
5636 static const struct got_error *
5637 cmd_tree(int argc, char *argv[])
5639 const struct got_error *error;
5640 struct got_repository *repo = NULL;
5641 struct got_worktree *worktree = NULL;
5642 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5643 struct got_object_id *commit_id = NULL;
5644 const char *commit_id_arg = NULL;
5645 char *label = NULL;
5646 struct got_reference *ref = NULL;
5647 const char *head_ref_name = NULL;
5648 int ch;
5649 struct tog_view *view;
5651 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5652 switch (ch) {
5653 case 'c':
5654 commit_id_arg = optarg;
5655 break;
5656 case 'r':
5657 repo_path = realpath(optarg, NULL);
5658 if (repo_path == NULL)
5659 return got_error_from_errno2("realpath",
5660 optarg);
5661 break;
5662 default:
5663 usage_tree();
5664 /* NOTREACHED */
5668 argc -= optind;
5669 argv += optind;
5671 if (argc > 1)
5672 usage_tree();
5674 if (repo_path == NULL) {
5675 cwd = getcwd(NULL, 0);
5676 if (cwd == NULL)
5677 return got_error_from_errno("getcwd");
5678 error = got_worktree_open(&worktree, cwd);
5679 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5680 goto done;
5681 if (worktree)
5682 repo_path =
5683 strdup(got_worktree_get_repo_path(worktree));
5684 else
5685 repo_path = strdup(cwd);
5686 if (repo_path == NULL) {
5687 error = got_error_from_errno("strdup");
5688 goto done;
5692 error = got_repo_open(&repo, repo_path, NULL);
5693 if (error != NULL)
5694 goto done;
5696 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5697 repo, worktree);
5698 if (error)
5699 goto done;
5701 init_curses();
5703 error = apply_unveil(got_repo_get_path(repo), NULL);
5704 if (error)
5705 goto done;
5707 error = tog_load_refs(repo, 0);
5708 if (error)
5709 goto done;
5711 if (commit_id_arg == NULL) {
5712 error = got_repo_match_object_id(&commit_id, &label,
5713 worktree ? got_worktree_get_head_ref_name(worktree) :
5714 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5715 if (error)
5716 goto done;
5717 head_ref_name = label;
5718 } else {
5719 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5720 if (error == NULL)
5721 head_ref_name = got_ref_get_name(ref);
5722 else if (error->code != GOT_ERR_NOT_REF)
5723 goto done;
5724 error = got_repo_match_object_id(&commit_id, NULL,
5725 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5726 if (error)
5727 goto done;
5730 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5731 if (view == NULL) {
5732 error = got_error_from_errno("view_open");
5733 goto done;
5735 error = open_tree_view(view, commit_id, head_ref_name, repo);
5736 if (error)
5737 goto done;
5738 if (!got_path_is_root_dir(in_repo_path)) {
5739 error = tree_view_walk_path(&view->state.tree, commit_id,
5740 in_repo_path);
5741 if (error)
5742 goto done;
5745 if (worktree) {
5746 /* Release work tree lock. */
5747 got_worktree_close(worktree);
5748 worktree = NULL;
5750 error = view_loop(view);
5751 done:
5752 free(repo_path);
5753 free(cwd);
5754 free(commit_id);
5755 free(label);
5756 if (ref)
5757 got_ref_close(ref);
5758 if (repo) {
5759 const struct got_error *close_err = got_repo_close(repo);
5760 if (error == NULL)
5761 error = close_err;
5763 tog_free_refs();
5764 return error;
5767 static const struct got_error *
5768 ref_view_load_refs(struct tog_ref_view_state *s)
5770 struct got_reflist_entry *sre;
5771 struct tog_reflist_entry *re;
5773 s->nrefs = 0;
5774 TAILQ_FOREACH(sre, &tog_refs, entry) {
5775 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5776 continue;
5778 re = malloc(sizeof(*re));
5779 if (re == NULL)
5780 return got_error_from_errno("malloc");
5782 re->ref = got_ref_dup(sre->ref);
5783 if (re->ref == NULL)
5784 return got_error_from_errno("got_ref_dup");
5785 re->idx = s->nrefs++;
5786 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5789 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5790 return NULL;
5793 void
5794 ref_view_free_refs(struct tog_ref_view_state *s)
5796 struct tog_reflist_entry *re;
5798 while (!TAILQ_EMPTY(&s->refs)) {
5799 re = TAILQ_FIRST(&s->refs);
5800 TAILQ_REMOVE(&s->refs, re, entry);
5801 got_ref_close(re->ref);
5802 free(re);
5806 static const struct got_error *
5807 open_ref_view(struct tog_view *view, struct got_repository *repo)
5809 const struct got_error *err = NULL;
5810 struct tog_ref_view_state *s = &view->state.ref;
5812 s->selected_entry = 0;
5813 s->repo = repo;
5815 TAILQ_INIT(&s->refs);
5816 STAILQ_INIT(&s->colors);
5818 err = ref_view_load_refs(s);
5819 if (err)
5820 return err;
5822 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5823 err = add_color(&s->colors, "^refs/heads/",
5824 TOG_COLOR_REFS_HEADS,
5825 get_color_value("TOG_COLOR_REFS_HEADS"));
5826 if (err)
5827 goto done;
5829 err = add_color(&s->colors, "^refs/tags/",
5830 TOG_COLOR_REFS_TAGS,
5831 get_color_value("TOG_COLOR_REFS_TAGS"));
5832 if (err)
5833 goto done;
5835 err = add_color(&s->colors, "^refs/remotes/",
5836 TOG_COLOR_REFS_REMOTES,
5837 get_color_value("TOG_COLOR_REFS_REMOTES"));
5838 if (err)
5839 goto done;
5842 view->show = show_ref_view;
5843 view->input = input_ref_view;
5844 view->close = close_ref_view;
5845 view->search_start = search_start_ref_view;
5846 view->search_next = search_next_ref_view;
5847 done:
5848 if (err)
5849 free_colors(&s->colors);
5850 return err;
5853 static const struct got_error *
5854 close_ref_view(struct tog_view *view)
5856 struct tog_ref_view_state *s = &view->state.ref;
5858 ref_view_free_refs(s);
5859 free_colors(&s->colors);
5861 return NULL;
5864 static const struct got_error *
5865 resolve_reflist_entry(struct got_object_id **commit_id,
5866 struct tog_reflist_entry *re, struct got_repository *repo)
5868 const struct got_error *err = NULL;
5869 struct got_object_id *obj_id;
5870 struct got_tag_object *tag = NULL;
5871 int obj_type;
5873 *commit_id = NULL;
5875 err = got_ref_resolve(&obj_id, repo, re->ref);
5876 if (err)
5877 return err;
5879 err = got_object_get_type(&obj_type, repo, obj_id);
5880 if (err)
5881 goto done;
5883 switch (obj_type) {
5884 case GOT_OBJ_TYPE_COMMIT:
5885 *commit_id = obj_id;
5886 break;
5887 case GOT_OBJ_TYPE_TAG:
5888 err = got_object_open_as_tag(&tag, repo, obj_id);
5889 if (err)
5890 goto done;
5891 free(obj_id);
5892 err = got_object_get_type(&obj_type, repo,
5893 got_object_tag_get_object_id(tag));
5894 if (err)
5895 goto done;
5896 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5897 err = got_error(GOT_ERR_OBJ_TYPE);
5898 goto done;
5900 *commit_id = got_object_id_dup(
5901 got_object_tag_get_object_id(tag));
5902 if (*commit_id == NULL) {
5903 err = got_error_from_errno("got_object_id_dup");
5904 goto done;
5906 break;
5907 default:
5908 err = got_error(GOT_ERR_OBJ_TYPE);
5909 break;
5912 done:
5913 if (tag)
5914 got_object_tag_close(tag);
5915 if (err) {
5916 free(*commit_id);
5917 *commit_id = NULL;
5919 return err;
5922 static const struct got_error *
5923 log_ref_entry(struct tog_view **new_view, int begin_x,
5924 struct tog_reflist_entry *re, struct got_repository *repo)
5926 struct tog_view *log_view;
5927 const struct got_error *err = NULL;
5928 struct got_object_id *commit_id = NULL;
5930 *new_view = NULL;
5932 err = resolve_reflist_entry(&commit_id, re, repo);
5933 if (err) {
5934 if (err->code != GOT_ERR_OBJ_TYPE)
5935 return err;
5936 else
5937 return NULL;
5940 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5941 if (log_view == NULL) {
5942 err = got_error_from_errno("view_open");
5943 goto done;
5946 err = open_log_view(log_view, commit_id, repo,
5947 got_ref_get_name(re->ref), "", 0);
5948 done:
5949 if (err)
5950 view_close(log_view);
5951 else
5952 *new_view = log_view;
5953 free(commit_id);
5954 return err;
5957 static void
5958 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5960 struct tog_reflist_entry *re;
5961 int i = 0;
5963 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5964 return;
5966 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5967 while (i++ < maxscroll) {
5968 if (re == NULL)
5969 break;
5970 s->first_displayed_entry = re;
5971 re = TAILQ_PREV(re, tog_reflist_head, entry);
5975 static void
5976 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5978 struct tog_reflist_entry *next, *last;
5979 int n = 0;
5981 if (s->first_displayed_entry)
5982 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5983 else
5984 next = TAILQ_FIRST(&s->refs);
5986 last = s->last_displayed_entry;
5987 while (next && last && n++ < maxscroll) {
5988 last = TAILQ_NEXT(last, entry);
5989 if (last) {
5990 s->first_displayed_entry = next;
5991 next = TAILQ_NEXT(next, entry);
5996 static const struct got_error *
5997 search_start_ref_view(struct tog_view *view)
5999 struct tog_ref_view_state *s = &view->state.ref;
6001 s->matched_entry = NULL;
6002 return NULL;
6005 static int
6006 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6008 regmatch_t regmatch;
6010 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6011 0) == 0;
6014 static const struct got_error *
6015 search_next_ref_view(struct tog_view *view)
6017 struct tog_ref_view_state *s = &view->state.ref;
6018 struct tog_reflist_entry *re = NULL;
6020 if (!view->searching) {
6021 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6022 return NULL;
6025 if (s->matched_entry) {
6026 if (view->searching == TOG_SEARCH_FORWARD) {
6027 if (s->selected_entry)
6028 re = TAILQ_NEXT(s->selected_entry, entry);
6029 else
6030 re = TAILQ_PREV(s->selected_entry,
6031 tog_reflist_head, entry);
6032 } else {
6033 if (s->selected_entry == NULL)
6034 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6035 else
6036 re = TAILQ_PREV(s->selected_entry,
6037 tog_reflist_head, entry);
6039 } else {
6040 if (s->selected_entry)
6041 re = s->selected_entry;
6042 else if (view->searching == TOG_SEARCH_FORWARD)
6043 re = TAILQ_FIRST(&s->refs);
6044 else
6045 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6048 while (1) {
6049 if (re == NULL) {
6050 if (s->matched_entry == NULL) {
6051 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6052 return NULL;
6054 if (view->searching == TOG_SEARCH_FORWARD)
6055 re = TAILQ_FIRST(&s->refs);
6056 else
6057 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6060 if (match_reflist_entry(re, &view->regex)) {
6061 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6062 s->matched_entry = re;
6063 break;
6066 if (view->searching == TOG_SEARCH_FORWARD)
6067 re = TAILQ_NEXT(re, entry);
6068 else
6069 re = TAILQ_PREV(re, tog_reflist_head, entry);
6072 if (s->matched_entry) {
6073 s->first_displayed_entry = s->matched_entry;
6074 s->selected = 0;
6077 return NULL;
6080 static const struct got_error *
6081 show_ref_view(struct tog_view *view)
6083 const struct got_error *err = NULL;
6084 struct tog_ref_view_state *s = &view->state.ref;
6085 struct tog_reflist_entry *re;
6086 char *line = NULL;
6087 wchar_t *wline;
6088 struct tog_color *tc;
6089 int width, n;
6090 int limit = view->nlines;
6092 werase(view->window);
6094 s->ndisplayed = 0;
6096 if (limit == 0)
6097 return NULL;
6099 re = s->first_displayed_entry;
6101 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6102 s->nrefs) == -1)
6103 return got_error_from_errno("asprintf");
6105 err = format_line(&wline, &width, line, view->ncols, 0);
6106 if (err) {
6107 free(line);
6108 return err;
6110 if (view_needs_focus_indication(view))
6111 wstandout(view->window);
6112 waddwstr(view->window, wline);
6113 if (view_needs_focus_indication(view))
6114 wstandend(view->window);
6115 free(wline);
6116 wline = NULL;
6117 free(line);
6118 line = NULL;
6119 if (width < view->ncols - 1)
6120 waddch(view->window, '\n');
6121 if (--limit <= 0)
6122 return NULL;
6124 n = 0;
6125 while (re && limit > 0) {
6126 char *line = NULL;
6128 if (got_ref_is_symbolic(re->ref)) {
6129 if (asprintf(&line, "%s -> %s",
6130 got_ref_get_name(re->ref),
6131 got_ref_get_symref_target(re->ref)) == -1)
6132 return got_error_from_errno("asprintf");
6133 } else if (s->show_ids) {
6134 struct got_object_id *id;
6135 char *id_str;
6136 err = got_ref_resolve(&id, s->repo, re->ref);
6137 if (err)
6138 return err;
6139 err = got_object_id_str(&id_str, id);
6140 if (err) {
6141 free(id);
6142 return err;
6144 if (asprintf(&line, "%s: %s",
6145 got_ref_get_name(re->ref), id_str) == -1) {
6146 err = got_error_from_errno("asprintf");
6147 free(id);
6148 free(id_str);
6149 return err;
6151 free(id);
6152 free(id_str);
6153 } else {
6154 line = strdup(got_ref_get_name(re->ref));
6155 if (line == NULL)
6156 return got_error_from_errno("strdup");
6159 err = format_line(&wline, &width, line, view->ncols, 0);
6160 if (err) {
6161 free(line);
6162 return err;
6164 if (n == s->selected) {
6165 if (view->focussed)
6166 wstandout(view->window);
6167 s->selected_entry = re;
6169 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6170 if (tc)
6171 wattr_on(view->window,
6172 COLOR_PAIR(tc->colorpair), NULL);
6173 waddwstr(view->window, wline);
6174 if (tc)
6175 wattr_off(view->window,
6176 COLOR_PAIR(tc->colorpair), NULL);
6177 if (width < view->ncols - 1)
6178 waddch(view->window, '\n');
6179 if (n == s->selected && view->focussed)
6180 wstandend(view->window);
6181 free(line);
6182 free(wline);
6183 wline = NULL;
6184 n++;
6185 s->ndisplayed++;
6186 s->last_displayed_entry = re;
6188 limit--;
6189 re = TAILQ_NEXT(re, entry);
6192 view_vborder(view);
6193 return err;
6196 static const struct got_error *
6197 browse_ref_tree(struct tog_view **new_view, int begin_x,
6198 struct tog_reflist_entry *re, struct got_repository *repo)
6200 const struct got_error *err = NULL;
6201 struct got_object_id *commit_id = NULL;
6202 struct tog_view *tree_view;
6204 *new_view = NULL;
6206 err = resolve_reflist_entry(&commit_id, re, repo);
6207 if (err) {
6208 if (err->code != GOT_ERR_OBJ_TYPE)
6209 return err;
6210 else
6211 return NULL;
6215 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6216 if (tree_view == NULL) {
6217 err = got_error_from_errno("view_open");
6218 goto done;
6221 err = open_tree_view(tree_view, commit_id,
6222 got_ref_get_name(re->ref), repo);
6223 if (err)
6224 goto done;
6226 *new_view = tree_view;
6227 done:
6228 free(commit_id);
6229 return err;
6231 static const struct got_error *
6232 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6234 const struct got_error *err = NULL;
6235 struct tog_ref_view_state *s = &view->state.ref;
6236 struct tog_view *log_view, *tree_view;
6237 struct tog_reflist_entry *re;
6238 int begin_x = 0, n;
6240 switch (ch) {
6241 case 'i':
6242 s->show_ids = !s->show_ids;
6243 break;
6244 case 'o':
6245 s->sort_by_date = !s->sort_by_date;
6246 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6247 got_ref_cmp_by_commit_timestamp_descending :
6248 got_ref_cmp_by_name, s->repo);
6249 if (err)
6250 break;
6251 got_reflist_object_id_map_free(tog_refs_idmap);
6252 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6253 &tog_refs, s->repo);
6254 if (err)
6255 break;
6256 ref_view_free_refs(s);
6257 err = ref_view_load_refs(s);
6258 break;
6259 case KEY_ENTER:
6260 case '\r':
6261 if (!s->selected_entry)
6262 break;
6263 if (view_is_parent_view(view))
6264 begin_x = view_split_begin_x(view->begin_x);
6265 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6266 s->repo);
6267 view->focussed = 0;
6268 log_view->focussed = 1;
6269 if (view_is_parent_view(view)) {
6270 err = view_close_child(view);
6271 if (err)
6272 return err;
6273 view_set_child(view, log_view);
6274 view->focus_child = 1;
6275 } else
6276 *new_view = log_view;
6277 break;
6278 case 't':
6279 if (!s->selected_entry)
6280 break;
6281 if (view_is_parent_view(view))
6282 begin_x = view_split_begin_x(view->begin_x);
6283 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6284 s->repo);
6285 if (err || tree_view == NULL)
6286 break;
6287 view->focussed = 0;
6288 tree_view->focussed = 1;
6289 if (view_is_parent_view(view)) {
6290 err = view_close_child(view);
6291 if (err)
6292 return err;
6293 view_set_child(view, tree_view);
6294 view->focus_child = 1;
6295 } else
6296 *new_view = tree_view;
6297 break;
6298 case 'g':
6299 case KEY_HOME:
6300 s->selected = 0;
6301 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6302 break;
6303 case 'G':
6304 case KEY_END:
6305 s->selected = 0;
6306 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6307 for (n = 0; n < view->nlines - 1; n++) {
6308 if (re == NULL)
6309 break;
6310 s->first_displayed_entry = re;
6311 re = TAILQ_PREV(re, tog_reflist_head, entry);
6313 if (n > 0)
6314 s->selected = n - 1;
6315 break;
6316 case 'k':
6317 case KEY_UP:
6318 case CTRL('p'):
6319 if (s->selected > 0) {
6320 s->selected--;
6321 break;
6323 ref_scroll_up(s, 1);
6324 break;
6325 case KEY_PPAGE:
6326 case CTRL('b'):
6327 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6328 s->selected = 0;
6329 ref_scroll_up(s, MAX(0, view->nlines - 1));
6330 break;
6331 case 'j':
6332 case KEY_DOWN:
6333 case CTRL('n'):
6334 if (s->selected < s->ndisplayed - 1) {
6335 s->selected++;
6336 break;
6338 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6339 /* can't scroll any further */
6340 break;
6341 ref_scroll_down(s, 1);
6342 break;
6343 case KEY_NPAGE:
6344 case CTRL('f'):
6345 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6346 /* can't scroll any further; move cursor down */
6347 if (s->selected < s->ndisplayed - 1)
6348 s->selected = s->ndisplayed - 1;
6349 break;
6351 ref_scroll_down(s, view->nlines - 1);
6352 break;
6353 case CTRL('l'):
6354 tog_free_refs();
6355 err = tog_load_refs(s->repo, s->sort_by_date);
6356 if (err)
6357 break;
6358 ref_view_free_refs(s);
6359 err = ref_view_load_refs(s);
6360 break;
6361 case KEY_RESIZE:
6362 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6363 s->selected = view->nlines - 2;
6364 break;
6365 default:
6366 break;
6369 return err;
6372 __dead static void
6373 usage_ref(void)
6375 endwin();
6376 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6377 getprogname());
6378 exit(1);
6381 static const struct got_error *
6382 cmd_ref(int argc, char *argv[])
6384 const struct got_error *error;
6385 struct got_repository *repo = NULL;
6386 struct got_worktree *worktree = NULL;
6387 char *cwd = NULL, *repo_path = NULL;
6388 int ch;
6389 struct tog_view *view;
6391 while ((ch = getopt(argc, argv, "r:")) != -1) {
6392 switch (ch) {
6393 case 'r':
6394 repo_path = realpath(optarg, NULL);
6395 if (repo_path == NULL)
6396 return got_error_from_errno2("realpath",
6397 optarg);
6398 break;
6399 default:
6400 usage_ref();
6401 /* NOTREACHED */
6405 argc -= optind;
6406 argv += optind;
6408 if (argc > 1)
6409 usage_ref();
6411 if (repo_path == NULL) {
6412 cwd = getcwd(NULL, 0);
6413 if (cwd == NULL)
6414 return got_error_from_errno("getcwd");
6415 error = got_worktree_open(&worktree, cwd);
6416 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6417 goto done;
6418 if (worktree)
6419 repo_path =
6420 strdup(got_worktree_get_repo_path(worktree));
6421 else
6422 repo_path = strdup(cwd);
6423 if (repo_path == NULL) {
6424 error = got_error_from_errno("strdup");
6425 goto done;
6429 error = got_repo_open(&repo, repo_path, NULL);
6430 if (error != NULL)
6431 goto done;
6433 init_curses();
6435 error = apply_unveil(got_repo_get_path(repo), NULL);
6436 if (error)
6437 goto done;
6439 error = tog_load_refs(repo, 0);
6440 if (error)
6441 goto done;
6443 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6444 if (view == NULL) {
6445 error = got_error_from_errno("view_open");
6446 goto done;
6449 error = open_ref_view(view, repo);
6450 if (error)
6451 goto done;
6453 if (worktree) {
6454 /* Release work tree lock. */
6455 got_worktree_close(worktree);
6456 worktree = NULL;
6458 error = view_loop(view);
6459 done:
6460 free(repo_path);
6461 free(cwd);
6462 if (repo) {
6463 const struct got_error *close_err = got_repo_close(repo);
6464 if (close_err)
6465 error = close_err;
6467 tog_free_refs();
6468 return error;
6471 static void
6472 list_commands(FILE *fp)
6474 size_t i;
6476 fprintf(fp, "commands:");
6477 for (i = 0; i < nitems(tog_commands); i++) {
6478 struct tog_cmd *cmd = &tog_commands[i];
6479 fprintf(fp, " %s", cmd->name);
6481 fputc('\n', fp);
6484 __dead static void
6485 usage(int hflag, int status)
6487 FILE *fp = (status == 0) ? stdout : stderr;
6489 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6490 getprogname());
6491 if (hflag) {
6492 fprintf(fp, "lazy usage: %s path\n", getprogname());
6493 list_commands(fp);
6495 exit(status);
6498 static char **
6499 make_argv(int argc, ...)
6501 va_list ap;
6502 char **argv;
6503 int i;
6505 va_start(ap, argc);
6507 argv = calloc(argc, sizeof(char *));
6508 if (argv == NULL)
6509 err(1, "calloc");
6510 for (i = 0; i < argc; i++) {
6511 argv[i] = strdup(va_arg(ap, char *));
6512 if (argv[i] == NULL)
6513 err(1, "strdup");
6516 va_end(ap);
6517 return argv;
6521 * Try to convert 'tog path' into a 'tog log path' command.
6522 * The user could simply have mistyped the command rather than knowingly
6523 * provided a path. So check whether argv[0] can in fact be resolved
6524 * to a path in the HEAD commit and print a special error if not.
6525 * This hack is for mpi@ <3
6527 static const struct got_error *
6528 tog_log_with_path(int argc, char *argv[])
6530 const struct got_error *error = NULL, *close_err;
6531 struct tog_cmd *cmd = NULL;
6532 struct got_repository *repo = NULL;
6533 struct got_worktree *worktree = NULL;
6534 struct got_object_id *commit_id = NULL, *id = NULL;
6535 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6536 char *commit_id_str = NULL, **cmd_argv = NULL;
6538 cwd = getcwd(NULL, 0);
6539 if (cwd == NULL)
6540 return got_error_from_errno("getcwd");
6542 error = got_worktree_open(&worktree, cwd);
6543 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6544 goto done;
6546 if (worktree)
6547 repo_path = strdup(got_worktree_get_repo_path(worktree));
6548 else
6549 repo_path = strdup(cwd);
6550 if (repo_path == NULL) {
6551 error = got_error_from_errno("strdup");
6552 goto done;
6555 error = got_repo_open(&repo, repo_path, NULL);
6556 if (error != NULL)
6557 goto done;
6559 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6560 repo, worktree);
6561 if (error)
6562 goto done;
6564 error = tog_load_refs(repo, 0);
6565 if (error)
6566 goto done;
6567 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6568 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6569 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6570 if (error)
6571 goto done;
6573 if (worktree) {
6574 got_worktree_close(worktree);
6575 worktree = NULL;
6578 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6579 if (error) {
6580 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6581 goto done;
6582 fprintf(stderr, "%s: '%s' is no known command or path\n",
6583 getprogname(), argv[0]);
6584 usage(1, 1);
6585 /* not reached */
6588 close_err = got_repo_close(repo);
6589 if (error == NULL)
6590 error = close_err;
6591 repo = NULL;
6593 error = got_object_id_str(&commit_id_str, commit_id);
6594 if (error)
6595 goto done;
6597 cmd = &tog_commands[0]; /* log */
6598 argc = 4;
6599 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6600 error = cmd->cmd_main(argc, cmd_argv);
6601 done:
6602 if (repo) {
6603 close_err = got_repo_close(repo);
6604 if (error == NULL)
6605 error = close_err;
6607 if (worktree)
6608 got_worktree_close(worktree);
6609 free(id);
6610 free(commit_id_str);
6611 free(commit_id);
6612 free(cwd);
6613 free(repo_path);
6614 free(in_repo_path);
6615 if (cmd_argv) {
6616 int i;
6617 for (i = 0; i < argc; i++)
6618 free(cmd_argv[i]);
6619 free(cmd_argv);
6621 tog_free_refs();
6622 return error;
6625 int
6626 main(int argc, char *argv[])
6628 const struct got_error *error = NULL;
6629 struct tog_cmd *cmd = NULL;
6630 int ch, hflag = 0, Vflag = 0;
6631 char **cmd_argv = NULL;
6632 static struct option longopts[] = {
6633 { "version", no_argument, NULL, 'V' },
6634 { NULL, 0, NULL, 0}
6637 setlocale(LC_CTYPE, "");
6639 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6640 switch (ch) {
6641 case 'h':
6642 hflag = 1;
6643 break;
6644 case 'V':
6645 Vflag = 1;
6646 break;
6647 default:
6648 usage(hflag, 1);
6649 /* NOTREACHED */
6653 argc -= optind;
6654 argv += optind;
6655 optind = 1;
6656 optreset = 1;
6658 if (Vflag) {
6659 got_version_print_str();
6660 return 0;
6663 #ifndef PROFILE
6664 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6665 NULL) == -1)
6666 err(1, "pledge");
6667 #endif
6669 if (argc == 0) {
6670 if (hflag)
6671 usage(hflag, 0);
6672 /* Build an argument vector which runs a default command. */
6673 cmd = &tog_commands[0];
6674 argc = 1;
6675 cmd_argv = make_argv(argc, cmd->name);
6676 } else {
6677 size_t i;
6679 /* Did the user specify a command? */
6680 for (i = 0; i < nitems(tog_commands); i++) {
6681 if (strncmp(tog_commands[i].name, argv[0],
6682 strlen(argv[0])) == 0) {
6683 cmd = &tog_commands[i];
6684 break;
6689 if (cmd == NULL) {
6690 if (argc != 1)
6691 usage(0, 1);
6692 /* No command specified; try log with a path */
6693 error = tog_log_with_path(argc, argv);
6694 } else {
6695 if (hflag)
6696 cmd->cmd_usage();
6697 else
6698 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6701 endwin();
6702 putchar('\n');
6703 if (cmd_argv) {
6704 int i;
6705 for (i = 0; i < argc; i++)
6706 free(cmd_argv[i]);
6707 free(cmd_argv);
6710 if (error && error->code != GOT_ERR_CANCELLED)
6711 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6712 return 0;