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 if (view->searching == TOG_SEARCH_FORWARD)
2205 entry = TAILQ_FIRST(&s->commits.head);
2206 else
2207 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2210 while (1) {
2211 int have_match = 0;
2213 if (entry == NULL) {
2214 if (s->thread_args.log_complete ||
2215 view->searching == TOG_SEARCH_BACKWARD) {
2216 view->search_next_done =
2217 (s->matched_entry == NULL ?
2218 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2219 s->search_entry = NULL;
2220 return NULL;
2223 * Poke the log thread for more commits and return,
2224 * allowing the main loop to make progress. Search
2225 * will resume at s->search_entry once we come back.
2227 s->thread_args.commits_needed++;
2228 return trigger_log_thread(view, 0);
2231 err = match_commit(&have_match, entry->id, entry->commit,
2232 &view->regex);
2233 if (err)
2234 break;
2235 if (have_match) {
2236 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2237 s->matched_entry = entry;
2238 break;
2241 s->search_entry = entry;
2242 if (view->searching == TOG_SEARCH_FORWARD)
2243 entry = TAILQ_NEXT(entry, entry);
2244 else
2245 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2248 if (s->matched_entry) {
2249 int cur = s->selected_entry->idx;
2250 while (cur < s->matched_entry->idx) {
2251 err = input_log_view(NULL, view, KEY_DOWN);
2252 if (err)
2253 return err;
2254 cur++;
2256 while (cur > s->matched_entry->idx) {
2257 err = input_log_view(NULL, view, KEY_UP);
2258 if (err)
2259 return err;
2260 cur--;
2264 s->search_entry = NULL;
2266 return NULL;
2269 static const struct got_error *
2270 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2271 struct got_repository *repo, const char *head_ref_name,
2272 const char *in_repo_path, int log_branches)
2274 const struct got_error *err = NULL;
2275 struct tog_log_view_state *s = &view->state.log;
2276 struct got_repository *thread_repo = NULL;
2277 struct got_commit_graph *thread_graph = NULL;
2278 int errcode;
2280 if (in_repo_path != s->in_repo_path) {
2281 free(s->in_repo_path);
2282 s->in_repo_path = strdup(in_repo_path);
2283 if (s->in_repo_path == NULL)
2284 return got_error_from_errno("strdup");
2287 /* The commit queue only contains commits being displayed. */
2288 TAILQ_INIT(&s->commits.head);
2289 s->commits.ncommits = 0;
2291 s->repo = repo;
2292 if (head_ref_name) {
2293 s->head_ref_name = strdup(head_ref_name);
2294 if (s->head_ref_name == NULL) {
2295 err = got_error_from_errno("strdup");
2296 goto done;
2299 s->start_id = got_object_id_dup(start_id);
2300 if (s->start_id == NULL) {
2301 err = got_error_from_errno("got_object_id_dup");
2302 goto done;
2304 s->log_branches = log_branches;
2306 STAILQ_INIT(&s->colors);
2307 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2308 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2309 get_color_value("TOG_COLOR_COMMIT"));
2310 if (err)
2311 goto done;
2312 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2313 get_color_value("TOG_COLOR_AUTHOR"));
2314 if (err) {
2315 free_colors(&s->colors);
2316 goto done;
2318 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2319 get_color_value("TOG_COLOR_DATE"));
2320 if (err) {
2321 free_colors(&s->colors);
2322 goto done;
2326 view->show = show_log_view;
2327 view->input = input_log_view;
2328 view->close = close_log_view;
2329 view->search_start = search_start_log_view;
2330 view->search_next = search_next_log_view;
2332 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2333 if (err)
2334 goto done;
2335 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2336 !s->log_branches);
2337 if (err)
2338 goto done;
2339 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2340 s->repo, NULL, NULL);
2341 if (err)
2342 goto done;
2344 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2345 if (errcode) {
2346 err = got_error_set_errno(errcode, "pthread_cond_init");
2347 goto done;
2349 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2350 if (errcode) {
2351 err = got_error_set_errno(errcode, "pthread_cond_init");
2352 goto done;
2355 s->thread_args.commits_needed = view->nlines;
2356 s->thread_args.graph = thread_graph;
2357 s->thread_args.commits = &s->commits;
2358 s->thread_args.in_repo_path = s->in_repo_path;
2359 s->thread_args.start_id = s->start_id;
2360 s->thread_args.repo = thread_repo;
2361 s->thread_args.log_complete = 0;
2362 s->thread_args.quit = &s->quit;
2363 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2364 s->thread_args.selected_entry = &s->selected_entry;
2365 s->thread_args.searching = &view->searching;
2366 s->thread_args.search_next_done = &view->search_next_done;
2367 s->thread_args.regex = &view->regex;
2368 done:
2369 if (err)
2370 close_log_view(view);
2371 return err;
2374 static const struct got_error *
2375 show_log_view(struct tog_view *view)
2377 const struct got_error *err;
2378 struct tog_log_view_state *s = &view->state.log;
2380 if (s->thread == 0) { //NULL) {
2381 int errcode = pthread_create(&s->thread, NULL, log_thread,
2382 &s->thread_args);
2383 if (errcode)
2384 return got_error_set_errno(errcode, "pthread_create");
2385 if (s->thread_args.commits_needed > 0) {
2386 err = trigger_log_thread(view, 1);
2387 if (err)
2388 return err;
2392 return draw_commits(view);
2395 static const struct got_error *
2396 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2398 const struct got_error *err = NULL;
2399 struct tog_log_view_state *s = &view->state.log;
2400 struct tog_view *diff_view = NULL, *tree_view = NULL;
2401 struct tog_view *ref_view = NULL;
2402 struct commit_queue_entry *entry;
2403 int begin_x = 0, n;
2405 if (s->thread_args.load_all) {
2406 if (ch == KEY_BACKSPACE)
2407 s->thread_args.load_all = 0;
2408 else if (s->thread_args.log_complete) {
2409 s->thread_args.load_all = 0;
2410 log_scroll_down(view, s->commits.ncommits);
2411 s->selected = MIN(view->nlines - 2,
2412 s->commits.ncommits - 1);
2413 select_commit(s);
2415 return NULL;
2418 switch (ch) {
2419 case 'q':
2420 s->quit = 1;
2421 break;
2422 case 'k':
2423 case KEY_UP:
2424 case '<':
2425 case ',':
2426 case CTRL('p'):
2427 if (s->first_displayed_entry == NULL)
2428 break;
2429 if (s->selected > 0)
2430 s->selected--;
2431 else
2432 log_scroll_up(s, 1);
2433 select_commit(s);
2434 break;
2435 case 'g':
2436 case KEY_HOME:
2437 s->selected = 0;
2438 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2439 select_commit(s);
2440 break;
2441 case KEY_PPAGE:
2442 case CTRL('b'):
2443 if (s->first_displayed_entry == NULL)
2444 break;
2445 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2446 s->selected = 0;
2447 else
2448 log_scroll_up(s, view->nlines - 1);
2449 select_commit(s);
2450 break;
2451 case 'j':
2452 case KEY_DOWN:
2453 case '>':
2454 case '.':
2455 case CTRL('n'):
2456 if (s->first_displayed_entry == NULL)
2457 break;
2458 if (s->selected < MIN(view->nlines - 2,
2459 s->commits.ncommits - 1))
2460 s->selected++;
2461 else {
2462 err = log_scroll_down(view, 1);
2463 if (err)
2464 break;
2466 select_commit(s);
2467 break;
2468 case 'G':
2469 case KEY_END: {
2470 /* We don't know yet how many commits, so we're forced to
2471 * traverse them all. */
2472 if (!s->thread_args.log_complete) {
2473 s->thread_args.load_all = 1;
2474 return trigger_log_thread(view, 0);
2477 s->selected = 0;
2478 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2479 for (n = 0; n < view->nlines - 1; n++) {
2480 if (entry == NULL)
2481 break;
2482 s->first_displayed_entry = entry;
2483 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2485 if (n > 0)
2486 s->selected = n - 1;
2487 select_commit(s);
2488 break;
2490 case KEY_NPAGE:
2491 case CTRL('f'): {
2492 struct commit_queue_entry *first;
2493 first = s->first_displayed_entry;
2494 if (first == NULL)
2495 break;
2496 err = log_scroll_down(view, view->nlines - 1);
2497 if (err)
2498 break;
2499 if (first == s->first_displayed_entry &&
2500 s->selected < MIN(view->nlines - 2,
2501 s->commits.ncommits - 1)) {
2502 /* can't scroll further down */
2503 s->selected = MIN(view->nlines - 2,
2504 s->commits.ncommits - 1);
2506 select_commit(s);
2507 break;
2509 case KEY_RESIZE:
2510 if (s->selected > view->nlines - 2)
2511 s->selected = view->nlines - 2;
2512 if (s->selected > s->commits.ncommits - 1)
2513 s->selected = s->commits.ncommits - 1;
2514 select_commit(s);
2515 if (s->commits.ncommits < view->nlines - 1 &&
2516 !s->thread_args.log_complete) {
2517 s->thread_args.commits_needed += (view->nlines - 1) -
2518 s->commits.ncommits;
2519 err = trigger_log_thread(view, 1);
2521 break;
2522 case KEY_ENTER:
2523 case ' ':
2524 case '\r':
2525 if (s->selected_entry == NULL)
2526 break;
2527 if (view_is_parent_view(view))
2528 begin_x = view_split_begin_x(view->begin_x);
2529 err = open_diff_view_for_commit(&diff_view, begin_x,
2530 s->selected_entry->commit, s->selected_entry->id,
2531 view, s->repo);
2532 if (err)
2533 break;
2534 view->focussed = 0;
2535 diff_view->focussed = 1;
2536 if (view_is_parent_view(view)) {
2537 err = view_close_child(view);
2538 if (err)
2539 return err;
2540 view_set_child(view, diff_view);
2541 view->focus_child = 1;
2542 } else
2543 *new_view = diff_view;
2544 break;
2545 case 't':
2546 if (s->selected_entry == NULL)
2547 break;
2548 if (view_is_parent_view(view))
2549 begin_x = view_split_begin_x(view->begin_x);
2550 err = browse_commit_tree(&tree_view, begin_x,
2551 s->selected_entry, s->in_repo_path, s->head_ref_name,
2552 s->repo);
2553 if (err)
2554 break;
2555 view->focussed = 0;
2556 tree_view->focussed = 1;
2557 if (view_is_parent_view(view)) {
2558 err = view_close_child(view);
2559 if (err)
2560 return err;
2561 view_set_child(view, tree_view);
2562 view->focus_child = 1;
2563 } else
2564 *new_view = tree_view;
2565 break;
2566 case KEY_BACKSPACE:
2567 case CTRL('l'):
2568 case 'B':
2569 if (ch == KEY_BACKSPACE &&
2570 got_path_is_root_dir(s->in_repo_path))
2571 break;
2572 err = stop_log_thread(s);
2573 if (err)
2574 return err;
2575 if (ch == KEY_BACKSPACE) {
2576 char *parent_path;
2577 err = got_path_dirname(&parent_path, s->in_repo_path);
2578 if (err)
2579 return err;
2580 free(s->in_repo_path);
2581 s->in_repo_path = parent_path;
2582 s->thread_args.in_repo_path = s->in_repo_path;
2583 } else if (ch == CTRL('l')) {
2584 struct got_object_id *start_id;
2585 err = got_repo_match_object_id(&start_id, NULL,
2586 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2587 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2588 if (err)
2589 return err;
2590 free(s->start_id);
2591 s->start_id = start_id;
2592 s->thread_args.start_id = s->start_id;
2593 } else /* 'B' */
2594 s->log_branches = !s->log_branches;
2596 err = got_repo_open(&s->thread_args.repo,
2597 got_repo_get_path(s->repo), NULL);
2598 if (err)
2599 return err;
2600 tog_free_refs();
2601 err = tog_load_refs(s->repo, 0);
2602 if (err)
2603 return err;
2604 err = got_commit_graph_open(&s->thread_args.graph,
2605 s->in_repo_path, !s->log_branches);
2606 if (err)
2607 return err;
2608 err = got_commit_graph_iter_start(s->thread_args.graph,
2609 s->start_id, s->repo, NULL, NULL);
2610 if (err)
2611 return err;
2612 free_commits(&s->commits);
2613 s->first_displayed_entry = NULL;
2614 s->last_displayed_entry = NULL;
2615 s->selected_entry = NULL;
2616 s->selected = 0;
2617 s->thread_args.log_complete = 0;
2618 s->quit = 0;
2619 s->thread_args.commits_needed = view->nlines;
2620 break;
2621 case 'r':
2622 if (view_is_parent_view(view))
2623 begin_x = view_split_begin_x(view->begin_x);
2624 ref_view = view_open(view->nlines, view->ncols,
2625 view->begin_y, begin_x, TOG_VIEW_REF);
2626 if (ref_view == NULL)
2627 return got_error_from_errno("view_open");
2628 err = open_ref_view(ref_view, s->repo);
2629 if (err) {
2630 view_close(ref_view);
2631 return err;
2633 view->focussed = 0;
2634 ref_view->focussed = 1;
2635 if (view_is_parent_view(view)) {
2636 err = view_close_child(view);
2637 if (err)
2638 return err;
2639 view_set_child(view, ref_view);
2640 view->focus_child = 1;
2641 } else
2642 *new_view = ref_view;
2643 break;
2644 default:
2645 break;
2648 return err;
2651 static const struct got_error *
2652 apply_unveil(const char *repo_path, const char *worktree_path)
2654 const struct got_error *error;
2656 #ifdef PROFILE
2657 if (unveil("gmon.out", "rwc") != 0)
2658 return got_error_from_errno2("unveil", "gmon.out");
2659 #endif
2660 if (repo_path && unveil(repo_path, "r") != 0)
2661 return got_error_from_errno2("unveil", repo_path);
2663 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2664 return got_error_from_errno2("unveil", worktree_path);
2666 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2667 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2669 error = got_privsep_unveil_exec_helpers();
2670 if (error != NULL)
2671 return error;
2673 if (unveil(NULL, NULL) != 0)
2674 return got_error_from_errno("unveil");
2676 return NULL;
2679 static void
2680 init_curses(void)
2682 initscr();
2683 cbreak();
2684 halfdelay(1); /* Do fast refresh while initial view is loading. */
2685 noecho();
2686 nonl();
2687 intrflush(stdscr, FALSE);
2688 keypad(stdscr, TRUE);
2689 curs_set(0);
2690 if (getenv("TOG_COLORS") != NULL) {
2691 start_color();
2692 use_default_colors();
2694 signal(SIGWINCH, tog_sigwinch);
2695 signal(SIGPIPE, tog_sigpipe);
2696 signal(SIGCONT, tog_sigcont);
2699 static const struct got_error *
2700 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2701 struct got_repository *repo, struct got_worktree *worktree)
2703 const struct got_error *err = NULL;
2705 if (argc == 0) {
2706 *in_repo_path = strdup("/");
2707 if (*in_repo_path == NULL)
2708 return got_error_from_errno("strdup");
2709 return NULL;
2712 if (worktree) {
2713 const char *prefix = got_worktree_get_path_prefix(worktree);
2714 char *p;
2716 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2717 if (err)
2718 return err;
2719 if (asprintf(in_repo_path, "%s%s%s", prefix,
2720 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2721 p) == -1) {
2722 err = got_error_from_errno("asprintf");
2723 *in_repo_path = NULL;
2725 free(p);
2726 } else
2727 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2729 return err;
2732 static const struct got_error *
2733 cmd_log(int argc, char *argv[])
2735 const struct got_error *error;
2736 struct got_repository *repo = NULL;
2737 struct got_worktree *worktree = NULL;
2738 struct got_object_id *start_id = NULL;
2739 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2740 char *start_commit = NULL, *label = NULL;
2741 struct got_reference *ref = NULL;
2742 const char *head_ref_name = NULL;
2743 int ch, log_branches = 0;
2744 struct tog_view *view;
2746 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2747 switch (ch) {
2748 case 'b':
2749 log_branches = 1;
2750 break;
2751 case 'c':
2752 start_commit = optarg;
2753 break;
2754 case 'r':
2755 repo_path = realpath(optarg, NULL);
2756 if (repo_path == NULL)
2757 return got_error_from_errno2("realpath",
2758 optarg);
2759 break;
2760 default:
2761 usage_log();
2762 /* NOTREACHED */
2766 argc -= optind;
2767 argv += optind;
2769 if (argc > 1)
2770 usage_log();
2772 if (repo_path == NULL) {
2773 cwd = getcwd(NULL, 0);
2774 if (cwd == NULL)
2775 return got_error_from_errno("getcwd");
2776 error = got_worktree_open(&worktree, cwd);
2777 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2778 goto done;
2779 if (worktree)
2780 repo_path =
2781 strdup(got_worktree_get_repo_path(worktree));
2782 else
2783 repo_path = strdup(cwd);
2784 if (repo_path == NULL) {
2785 error = got_error_from_errno("strdup");
2786 goto done;
2790 error = got_repo_open(&repo, repo_path, NULL);
2791 if (error != NULL)
2792 goto done;
2794 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2795 repo, worktree);
2796 if (error)
2797 goto done;
2799 init_curses();
2801 error = apply_unveil(got_repo_get_path(repo),
2802 worktree ? got_worktree_get_root_path(worktree) : NULL);
2803 if (error)
2804 goto done;
2806 /* already loaded by tog_log_with_path()? */
2807 if (TAILQ_EMPTY(&tog_refs)) {
2808 error = tog_load_refs(repo, 0);
2809 if (error)
2810 goto done;
2813 if (start_commit == NULL) {
2814 error = got_repo_match_object_id(&start_id, &label,
2815 worktree ? got_worktree_get_head_ref_name(worktree) :
2816 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2817 if (error)
2818 goto done;
2819 head_ref_name = label;
2820 } else {
2821 error = got_ref_open(&ref, repo, start_commit, 0);
2822 if (error == NULL)
2823 head_ref_name = got_ref_get_name(ref);
2824 else if (error->code != GOT_ERR_NOT_REF)
2825 goto done;
2826 error = got_repo_match_object_id(&start_id, NULL,
2827 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2828 if (error)
2829 goto done;
2832 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2833 if (view == NULL) {
2834 error = got_error_from_errno("view_open");
2835 goto done;
2837 error = open_log_view(view, start_id, repo, head_ref_name,
2838 in_repo_path, log_branches);
2839 if (error)
2840 goto done;
2841 if (worktree) {
2842 /* Release work tree lock. */
2843 got_worktree_close(worktree);
2844 worktree = NULL;
2846 error = view_loop(view);
2847 done:
2848 free(in_repo_path);
2849 free(repo_path);
2850 free(cwd);
2851 free(start_id);
2852 free(label);
2853 if (ref)
2854 got_ref_close(ref);
2855 if (repo) {
2856 const struct got_error *close_err = got_repo_close(repo);
2857 if (error == NULL)
2858 error = close_err;
2860 if (worktree)
2861 got_worktree_close(worktree);
2862 tog_free_refs();
2863 return error;
2866 __dead static void
2867 usage_diff(void)
2869 endwin();
2870 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2871 "[-w] object1 object2\n", getprogname());
2872 exit(1);
2875 static int
2876 match_line(const char *line, regex_t *regex, size_t nmatch,
2877 regmatch_t *regmatch)
2879 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2882 struct tog_color *
2883 match_color(struct tog_colors *colors, const char *line)
2885 struct tog_color *tc = NULL;
2887 STAILQ_FOREACH(tc, colors, entry) {
2888 if (match_line(line, &tc->regex, 0, NULL))
2889 return tc;
2892 return NULL;
2895 static const struct got_error *
2896 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2897 WINDOW *window, regmatch_t *regmatch)
2899 const struct got_error *err = NULL;
2900 wchar_t *wline;
2901 int width;
2902 char *s;
2904 *wtotal = 0;
2906 s = strndup(line, regmatch->rm_so);
2907 if (s == NULL)
2908 return got_error_from_errno("strndup");
2910 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2911 if (err) {
2912 free(s);
2913 return err;
2915 waddwstr(window, wline);
2916 free(wline);
2917 free(s);
2918 wlimit -= width;
2919 *wtotal += width;
2921 if (wlimit > 0) {
2922 s = strndup(line + regmatch->rm_so,
2923 regmatch->rm_eo - regmatch->rm_so);
2924 if (s == NULL) {
2925 err = got_error_from_errno("strndup");
2926 free(s);
2927 return err;
2929 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2930 if (err) {
2931 free(s);
2932 return err;
2934 wattr_on(window, A_STANDOUT, NULL);
2935 waddwstr(window, wline);
2936 wattr_off(window, A_STANDOUT, NULL);
2937 free(wline);
2938 free(s);
2939 wlimit -= width;
2940 *wtotal += width;
2943 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2944 err = format_line(&wline, &width,
2945 line + regmatch->rm_eo, wlimit, col_tab_align);
2946 if (err)
2947 return err;
2948 waddwstr(window, wline);
2949 free(wline);
2950 *wtotal += width;
2953 return NULL;
2956 static const struct got_error *
2957 draw_file(struct tog_view *view, const char *header)
2959 struct tog_diff_view_state *s = &view->state.diff;
2960 regmatch_t *regmatch = &view->regmatch;
2961 const struct got_error *err;
2962 int nprinted = 0;
2963 char *line;
2964 size_t linesize = 0;
2965 ssize_t linelen;
2966 struct tog_color *tc;
2967 wchar_t *wline;
2968 int width;
2969 int max_lines = view->nlines;
2970 int nlines = s->nlines;
2971 off_t line_offset;
2973 line_offset = s->line_offsets[s->first_displayed_line - 1];
2974 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2975 return got_error_from_errno("fseek");
2977 werase(view->window);
2979 if (header) {
2980 if (asprintf(&line, "[%d/%d] %s",
2981 s->first_displayed_line - 1 + s->selected_line, nlines,
2982 header) == -1)
2983 return got_error_from_errno("asprintf");
2984 err = format_line(&wline, &width, line, view->ncols, 0);
2985 free(line);
2986 if (err)
2987 return err;
2989 if (view_needs_focus_indication(view))
2990 wstandout(view->window);
2991 waddwstr(view->window, wline);
2992 free(wline);
2993 wline = NULL;
2994 if (view_needs_focus_indication(view))
2995 wstandend(view->window);
2996 if (width <= view->ncols - 1)
2997 waddch(view->window, '\n');
2999 if (max_lines <= 1)
3000 return NULL;
3001 max_lines--;
3004 s->eof = 0;
3005 line = NULL;
3006 while (max_lines > 0 && nprinted < max_lines) {
3007 linelen = getline(&line, &linesize, s->f);
3008 if (linelen == -1) {
3009 if (feof(s->f)) {
3010 s->eof = 1;
3011 break;
3013 free(line);
3014 return got_ferror(s->f, GOT_ERR_IO);
3017 tc = match_color(&s->colors, line);
3018 if (tc)
3019 wattr_on(view->window,
3020 COLOR_PAIR(tc->colorpair), NULL);
3021 if (s->first_displayed_line + nprinted == s->matched_line &&
3022 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3023 err = add_matched_line(&width, line, view->ncols, 0,
3024 view->window, regmatch);
3025 if (err) {
3026 free(line);
3027 return err;
3029 } else {
3030 err = format_line(&wline, &width, line, view->ncols, 0);
3031 if (err) {
3032 free(line);
3033 return err;
3035 waddwstr(view->window, wline);
3036 free(wline);
3037 wline = NULL;
3039 if (tc)
3040 wattr_off(view->window,
3041 COLOR_PAIR(tc->colorpair), NULL);
3042 if (width <= view->ncols - 1)
3043 waddch(view->window, '\n');
3044 nprinted++;
3046 free(line);
3047 if (nprinted >= 1)
3048 s->last_displayed_line = s->first_displayed_line +
3049 (nprinted - 1);
3050 else
3051 s->last_displayed_line = s->first_displayed_line;
3053 view_vborder(view);
3055 if (s->eof) {
3056 while (nprinted < view->nlines) {
3057 waddch(view->window, '\n');
3058 nprinted++;
3061 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3062 if (err) {
3063 return err;
3066 wstandout(view->window);
3067 waddwstr(view->window, wline);
3068 free(wline);
3069 wline = NULL;
3070 wstandend(view->window);
3073 return NULL;
3076 static char *
3077 get_datestr(time_t *time, char *datebuf)
3079 struct tm mytm, *tm;
3080 char *p, *s;
3082 tm = gmtime_r(time, &mytm);
3083 if (tm == NULL)
3084 return NULL;
3085 s = asctime_r(tm, datebuf);
3086 if (s == NULL)
3087 return NULL;
3088 p = strchr(s, '\n');
3089 if (p)
3090 *p = '\0';
3091 return s;
3094 static const struct got_error *
3095 get_changed_paths(struct got_pathlist_head *paths,
3096 struct got_commit_object *commit, struct got_repository *repo)
3098 const struct got_error *err = NULL;
3099 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3100 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3101 struct got_object_qid *qid;
3103 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3104 if (qid != NULL) {
3105 struct got_commit_object *pcommit;
3106 err = got_object_open_as_commit(&pcommit, repo,
3107 qid->id);
3108 if (err)
3109 return err;
3111 tree_id1 = got_object_id_dup(
3112 got_object_commit_get_tree_id(pcommit));
3113 if (tree_id1 == NULL) {
3114 got_object_commit_close(pcommit);
3115 return got_error_from_errno("got_object_id_dup");
3117 got_object_commit_close(pcommit);
3121 if (tree_id1) {
3122 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3123 if (err)
3124 goto done;
3127 tree_id2 = got_object_commit_get_tree_id(commit);
3128 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3129 if (err)
3130 goto done;
3132 err = got_diff_tree(tree1, tree2, "", "", repo,
3133 got_diff_tree_collect_changed_paths, paths, 0);
3134 done:
3135 if (tree1)
3136 got_object_tree_close(tree1);
3137 if (tree2)
3138 got_object_tree_close(tree2);
3139 free(tree_id1);
3140 return err;
3143 static const struct got_error *
3144 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3146 off_t *p;
3148 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3149 if (p == NULL)
3150 return got_error_from_errno("reallocarray");
3151 *line_offsets = p;
3152 (*line_offsets)[*nlines] = off;
3153 (*nlines)++;
3154 return NULL;
3157 static const struct got_error *
3158 write_commit_info(off_t **line_offsets, size_t *nlines,
3159 struct got_object_id *commit_id, struct got_reflist_head *refs,
3160 struct got_repository *repo, FILE *outfile)
3162 const struct got_error *err = NULL;
3163 char datebuf[26], *datestr;
3164 struct got_commit_object *commit;
3165 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3166 time_t committer_time;
3167 const char *author, *committer;
3168 char *refs_str = NULL;
3169 struct got_pathlist_head changed_paths;
3170 struct got_pathlist_entry *pe;
3171 off_t outoff = 0;
3172 int n;
3174 TAILQ_INIT(&changed_paths);
3176 if (refs) {
3177 err = build_refs_str(&refs_str, refs, commit_id, repo);
3178 if (err)
3179 return err;
3182 err = got_object_open_as_commit(&commit, repo, commit_id);
3183 if (err)
3184 return err;
3186 err = got_object_id_str(&id_str, commit_id);
3187 if (err) {
3188 err = got_error_from_errno("got_object_id_str");
3189 goto done;
3192 err = add_line_offset(line_offsets, nlines, 0);
3193 if (err)
3194 goto done;
3196 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3197 refs_str ? refs_str : "", refs_str ? ")" : "");
3198 if (n < 0) {
3199 err = got_error_from_errno("fprintf");
3200 goto done;
3202 outoff += n;
3203 err = add_line_offset(line_offsets, nlines, outoff);
3204 if (err)
3205 goto done;
3207 n = fprintf(outfile, "from: %s\n",
3208 got_object_commit_get_author(commit));
3209 if (n < 0) {
3210 err = got_error_from_errno("fprintf");
3211 goto done;
3213 outoff += n;
3214 err = add_line_offset(line_offsets, nlines, outoff);
3215 if (err)
3216 goto done;
3218 committer_time = got_object_commit_get_committer_time(commit);
3219 datestr = get_datestr(&committer_time, datebuf);
3220 if (datestr) {
3221 n = fprintf(outfile, "date: %s UTC\n", datestr);
3222 if (n < 0) {
3223 err = got_error_from_errno("fprintf");
3224 goto done;
3226 outoff += n;
3227 err = add_line_offset(line_offsets, nlines, outoff);
3228 if (err)
3229 goto done;
3231 author = got_object_commit_get_author(commit);
3232 committer = got_object_commit_get_committer(commit);
3233 if (strcmp(author, committer) != 0) {
3234 n = fprintf(outfile, "via: %s\n", committer);
3235 if (n < 0) {
3236 err = got_error_from_errno("fprintf");
3237 goto done;
3239 outoff += n;
3240 err = add_line_offset(line_offsets, nlines, outoff);
3241 if (err)
3242 goto done;
3244 if (got_object_commit_get_nparents(commit) > 1) {
3245 const struct got_object_id_queue *parent_ids;
3246 struct got_object_qid *qid;
3247 int pn = 1;
3248 parent_ids = got_object_commit_get_parent_ids(commit);
3249 STAILQ_FOREACH(qid, parent_ids, entry) {
3250 err = got_object_id_str(&id_str, qid->id);
3251 if (err)
3252 goto done;
3253 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3254 if (n < 0) {
3255 err = got_error_from_errno("fprintf");
3256 goto done;
3258 outoff += n;
3259 err = add_line_offset(line_offsets, nlines, outoff);
3260 if (err)
3261 goto done;
3262 free(id_str);
3263 id_str = NULL;
3267 err = got_object_commit_get_logmsg(&logmsg, commit);
3268 if (err)
3269 goto done;
3270 s = logmsg;
3271 while ((line = strsep(&s, "\n")) != NULL) {
3272 n = fprintf(outfile, "%s\n", line);
3273 if (n < 0) {
3274 err = got_error_from_errno("fprintf");
3275 goto done;
3277 outoff += n;
3278 err = add_line_offset(line_offsets, nlines, outoff);
3279 if (err)
3280 goto done;
3283 err = get_changed_paths(&changed_paths, commit, repo);
3284 if (err)
3285 goto done;
3286 TAILQ_FOREACH(pe, &changed_paths, entry) {
3287 struct got_diff_changed_path *cp = pe->data;
3288 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3289 if (n < 0) {
3290 err = got_error_from_errno("fprintf");
3291 goto done;
3293 outoff += n;
3294 err = add_line_offset(line_offsets, nlines, outoff);
3295 if (err)
3296 goto done;
3297 free((char *)pe->path);
3298 free(pe->data);
3301 fputc('\n', outfile);
3302 outoff++;
3303 err = add_line_offset(line_offsets, nlines, outoff);
3304 done:
3305 got_pathlist_free(&changed_paths);
3306 free(id_str);
3307 free(logmsg);
3308 free(refs_str);
3309 got_object_commit_close(commit);
3310 if (err) {
3311 free(*line_offsets);
3312 *line_offsets = NULL;
3313 *nlines = 0;
3315 return err;
3318 static const struct got_error *
3319 create_diff(struct tog_diff_view_state *s)
3321 const struct got_error *err = NULL;
3322 FILE *f = NULL;
3323 int obj_type;
3325 free(s->line_offsets);
3326 s->line_offsets = malloc(sizeof(off_t));
3327 if (s->line_offsets == NULL)
3328 return got_error_from_errno("malloc");
3329 s->nlines = 0;
3331 f = got_opentemp();
3332 if (f == NULL) {
3333 err = got_error_from_errno("got_opentemp");
3334 goto done;
3336 if (s->f && fclose(s->f) == EOF) {
3337 err = got_error_from_errno("fclose");
3338 goto done;
3340 s->f = f;
3342 if (s->id1)
3343 err = got_object_get_type(&obj_type, s->repo, s->id1);
3344 else
3345 err = got_object_get_type(&obj_type, s->repo, s->id2);
3346 if (err)
3347 goto done;
3349 switch (obj_type) {
3350 case GOT_OBJ_TYPE_BLOB:
3351 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3352 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3353 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3354 break;
3355 case GOT_OBJ_TYPE_TREE:
3356 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3357 s->id1, s->id2, NULL, "", "", s->diff_context,
3358 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3359 break;
3360 case GOT_OBJ_TYPE_COMMIT: {
3361 const struct got_object_id_queue *parent_ids;
3362 struct got_object_qid *pid;
3363 struct got_commit_object *commit2;
3364 struct got_reflist_head *refs;
3366 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3367 if (err)
3368 goto done;
3369 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3370 /* Show commit info if we're diffing to a parent/root commit. */
3371 if (s->id1 == NULL) {
3372 err = write_commit_info(&s->line_offsets, &s->nlines,
3373 s->id2, refs, s->repo, s->f);
3374 if (err)
3375 goto done;
3376 } else {
3377 parent_ids = got_object_commit_get_parent_ids(commit2);
3378 STAILQ_FOREACH(pid, parent_ids, entry) {
3379 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3380 err = write_commit_info(
3381 &s->line_offsets, &s->nlines,
3382 s->id2, refs, s->repo, s->f);
3383 if (err)
3384 goto done;
3385 break;
3389 got_object_commit_close(commit2);
3391 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3392 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3393 s->force_text_diff, s->repo, s->f);
3394 break;
3396 default:
3397 err = got_error(GOT_ERR_OBJ_TYPE);
3398 break;
3400 if (err)
3401 goto done;
3402 done:
3403 if (s->f && fflush(s->f) != 0 && err == NULL)
3404 err = got_error_from_errno("fflush");
3405 return err;
3408 static void
3409 diff_view_indicate_progress(struct tog_view *view)
3411 mvwaddstr(view->window, 0, 0, "diffing...");
3412 update_panels();
3413 doupdate();
3416 static const struct got_error *
3417 search_start_diff_view(struct tog_view *view)
3419 struct tog_diff_view_state *s = &view->state.diff;
3421 s->matched_line = 0;
3422 return NULL;
3425 static const struct got_error *
3426 search_next_diff_view(struct tog_view *view)
3428 struct tog_diff_view_state *s = &view->state.diff;
3429 int lineno;
3430 char *line = NULL;
3431 size_t linesize = 0;
3432 ssize_t linelen;
3434 if (!view->searching) {
3435 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3436 return NULL;
3439 if (s->matched_line) {
3440 if (view->searching == TOG_SEARCH_FORWARD)
3441 lineno = s->matched_line + 1;
3442 else
3443 lineno = s->matched_line - 1;
3444 } else {
3445 if (view->searching == TOG_SEARCH_FORWARD)
3446 lineno = 1;
3447 else
3448 lineno = s->nlines;
3451 while (1) {
3452 off_t offset;
3454 if (lineno <= 0 || lineno > s->nlines) {
3455 if (s->matched_line == 0) {
3456 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3457 break;
3460 if (view->searching == TOG_SEARCH_FORWARD)
3461 lineno = 1;
3462 else
3463 lineno = s->nlines;
3466 offset = s->line_offsets[lineno - 1];
3467 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3468 free(line);
3469 return got_error_from_errno("fseeko");
3471 linelen = getline(&line, &linesize, s->f);
3472 if (linelen != -1 &&
3473 match_line(line, &view->regex, 1, &view->regmatch)) {
3474 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3475 s->matched_line = lineno;
3476 break;
3478 if (view->searching == TOG_SEARCH_FORWARD)
3479 lineno++;
3480 else
3481 lineno--;
3483 free(line);
3485 if (s->matched_line) {
3486 s->first_displayed_line = s->matched_line;
3487 s->selected_line = 1;
3490 return NULL;
3493 static const struct got_error *
3494 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3495 struct got_object_id *id2, const char *label1, const char *label2,
3496 int diff_context, int ignore_whitespace, int force_text_diff,
3497 struct tog_view *log_view, struct got_repository *repo)
3499 const struct got_error *err;
3500 struct tog_diff_view_state *s = &view->state.diff;
3502 if (id1 != NULL && id2 != NULL) {
3503 int type1, type2;
3504 err = got_object_get_type(&type1, repo, id1);
3505 if (err)
3506 return err;
3507 err = got_object_get_type(&type2, repo, id2);
3508 if (err)
3509 return err;
3511 if (type1 != type2)
3512 return got_error(GOT_ERR_OBJ_TYPE);
3514 s->first_displayed_line = 1;
3515 s->last_displayed_line = view->nlines;
3516 s->selected_line = 1;
3517 s->repo = repo;
3518 s->id1 = id1;
3519 s->id2 = id2;
3520 s->label1 = label1;
3521 s->label2 = label2;
3523 if (id1) {
3524 s->id1 = got_object_id_dup(id1);
3525 if (s->id1 == NULL)
3526 return got_error_from_errno("got_object_id_dup");
3527 } else
3528 s->id1 = NULL;
3530 s->id2 = got_object_id_dup(id2);
3531 if (s->id2 == NULL) {
3532 free(s->id1);
3533 s->id1 = NULL;
3534 return got_error_from_errno("got_object_id_dup");
3536 s->f = NULL;
3537 s->first_displayed_line = 1;
3538 s->last_displayed_line = view->nlines;
3539 s->diff_context = diff_context;
3540 s->ignore_whitespace = ignore_whitespace;
3541 s->force_text_diff = force_text_diff;
3542 s->log_view = log_view;
3543 s->repo = repo;
3545 STAILQ_INIT(&s->colors);
3546 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3547 err = add_color(&s->colors,
3548 "^-", TOG_COLOR_DIFF_MINUS,
3549 get_color_value("TOG_COLOR_DIFF_MINUS"));
3550 if (err)
3551 return err;
3552 err = add_color(&s->colors, "^\\+",
3553 TOG_COLOR_DIFF_PLUS,
3554 get_color_value("TOG_COLOR_DIFF_PLUS"));
3555 if (err) {
3556 free_colors(&s->colors);
3557 return err;
3559 err = add_color(&s->colors,
3560 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3561 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3562 if (err) {
3563 free_colors(&s->colors);
3564 return err;
3567 err = add_color(&s->colors,
3568 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3569 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3570 get_color_value("TOG_COLOR_DIFF_META"));
3571 if (err) {
3572 free_colors(&s->colors);
3573 return err;
3576 err = add_color(&s->colors,
3577 "^(from|via): ", TOG_COLOR_AUTHOR,
3578 get_color_value("TOG_COLOR_AUTHOR"));
3579 if (err) {
3580 free_colors(&s->colors);
3581 return err;
3584 err = add_color(&s->colors,
3585 "^date: ", TOG_COLOR_DATE,
3586 get_color_value("TOG_COLOR_DATE"));
3587 if (err) {
3588 free_colors(&s->colors);
3589 return err;
3593 if (log_view && view_is_splitscreen(view))
3594 show_log_view(log_view); /* draw vborder */
3595 diff_view_indicate_progress(view);
3597 s->line_offsets = NULL;
3598 s->nlines = 0;
3599 err = create_diff(s);
3600 if (err) {
3601 free(s->id1);
3602 s->id1 = NULL;
3603 free(s->id2);
3604 s->id2 = NULL;
3605 free_colors(&s->colors);
3606 return err;
3609 view->show = show_diff_view;
3610 view->input = input_diff_view;
3611 view->close = close_diff_view;
3612 view->search_start = search_start_diff_view;
3613 view->search_next = search_next_diff_view;
3615 return NULL;
3618 static const struct got_error *
3619 close_diff_view(struct tog_view *view)
3621 const struct got_error *err = NULL;
3622 struct tog_diff_view_state *s = &view->state.diff;
3624 free(s->id1);
3625 s->id1 = NULL;
3626 free(s->id2);
3627 s->id2 = NULL;
3628 if (s->f && fclose(s->f) == EOF)
3629 err = got_error_from_errno("fclose");
3630 free_colors(&s->colors);
3631 free(s->line_offsets);
3632 s->line_offsets = NULL;
3633 s->nlines = 0;
3634 return err;
3637 static const struct got_error *
3638 show_diff_view(struct tog_view *view)
3640 const struct got_error *err;
3641 struct tog_diff_view_state *s = &view->state.diff;
3642 char *id_str1 = NULL, *id_str2, *header;
3643 const char *label1, *label2;
3645 if (s->id1) {
3646 err = got_object_id_str(&id_str1, s->id1);
3647 if (err)
3648 return err;
3649 label1 = s->label1 ? : id_str1;
3650 } else
3651 label1 = "/dev/null";
3653 err = got_object_id_str(&id_str2, s->id2);
3654 if (err)
3655 return err;
3656 label2 = s->label2 ? : id_str2;
3658 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3659 err = got_error_from_errno("asprintf");
3660 free(id_str1);
3661 free(id_str2);
3662 return err;
3664 free(id_str1);
3665 free(id_str2);
3667 err = draw_file(view, header);
3668 free(header);
3669 return err;
3672 static const struct got_error *
3673 set_selected_commit(struct tog_diff_view_state *s,
3674 struct commit_queue_entry *entry)
3676 const struct got_error *err;
3677 const struct got_object_id_queue *parent_ids;
3678 struct got_commit_object *selected_commit;
3679 struct got_object_qid *pid;
3681 free(s->id2);
3682 s->id2 = got_object_id_dup(entry->id);
3683 if (s->id2 == NULL)
3684 return got_error_from_errno("got_object_id_dup");
3686 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3687 if (err)
3688 return err;
3689 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3690 free(s->id1);
3691 pid = STAILQ_FIRST(parent_ids);
3692 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3693 got_object_commit_close(selected_commit);
3694 return NULL;
3697 static const struct got_error *
3698 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3700 const struct got_error *err = NULL;
3701 struct tog_diff_view_state *s = &view->state.diff;
3702 struct tog_log_view_state *ls;
3703 struct commit_queue_entry *old_selected_entry;
3704 char *line = NULL;
3705 size_t linesize = 0;
3706 ssize_t linelen;
3707 int i;
3709 switch (ch) {
3710 case 'a':
3711 case 'w':
3712 if (ch == 'a')
3713 s->force_text_diff = !s->force_text_diff;
3714 if (ch == 'w')
3715 s->ignore_whitespace = !s->ignore_whitespace;
3716 wclear(view->window);
3717 s->first_displayed_line = 1;
3718 s->last_displayed_line = view->nlines;
3719 diff_view_indicate_progress(view);
3720 err = create_diff(s);
3721 break;
3722 case 'g':
3723 case KEY_HOME:
3724 s->first_displayed_line = 1;
3725 break;
3726 case 'G':
3727 case KEY_END:
3728 if (s->eof)
3729 break;
3731 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3732 s->eof = 1;
3733 break;
3734 case 'k':
3735 case KEY_UP:
3736 case CTRL('p'):
3737 if (s->first_displayed_line > 1)
3738 s->first_displayed_line--;
3739 break;
3740 case KEY_PPAGE:
3741 case CTRL('b'):
3742 if (s->first_displayed_line == 1)
3743 break;
3744 i = 0;
3745 while (i++ < view->nlines - 1 &&
3746 s->first_displayed_line > 1)
3747 s->first_displayed_line--;
3748 break;
3749 case 'j':
3750 case KEY_DOWN:
3751 case CTRL('n'):
3752 if (!s->eof)
3753 s->first_displayed_line++;
3754 break;
3755 case KEY_NPAGE:
3756 case CTRL('f'):
3757 case ' ':
3758 if (s->eof)
3759 break;
3760 i = 0;
3761 while (!s->eof && i++ < view->nlines - 1) {
3762 linelen = getline(&line, &linesize, s->f);
3763 s->first_displayed_line++;
3764 if (linelen == -1) {
3765 if (feof(s->f)) {
3766 s->eof = 1;
3767 } else
3768 err = got_ferror(s->f, GOT_ERR_IO);
3769 break;
3772 free(line);
3773 break;
3774 case '[':
3775 if (s->diff_context > 0) {
3776 s->diff_context--;
3777 diff_view_indicate_progress(view);
3778 err = create_diff(s);
3779 if (s->first_displayed_line + view->nlines - 1 >
3780 s->nlines) {
3781 s->first_displayed_line = 1;
3782 s->last_displayed_line = view->nlines;
3785 break;
3786 case ']':
3787 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3788 s->diff_context++;
3789 diff_view_indicate_progress(view);
3790 err = create_diff(s);
3792 break;
3793 case '<':
3794 case ',':
3795 if (s->log_view == NULL)
3796 break;
3797 ls = &s->log_view->state.log;
3798 old_selected_entry = ls->selected_entry;
3800 err = input_log_view(NULL, s->log_view, KEY_UP);
3801 if (err)
3802 break;
3804 if (old_selected_entry == ls->selected_entry)
3805 break;
3807 err = set_selected_commit(s, ls->selected_entry);
3808 if (err)
3809 break;
3811 s->first_displayed_line = 1;
3812 s->last_displayed_line = view->nlines;
3814 diff_view_indicate_progress(view);
3815 err = create_diff(s);
3816 break;
3817 case '>':
3818 case '.':
3819 if (s->log_view == NULL)
3820 break;
3821 ls = &s->log_view->state.log;
3822 old_selected_entry = ls->selected_entry;
3824 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3825 if (err)
3826 break;
3828 if (old_selected_entry == ls->selected_entry)
3829 break;
3831 err = set_selected_commit(s, ls->selected_entry);
3832 if (err)
3833 break;
3835 s->first_displayed_line = 1;
3836 s->last_displayed_line = view->nlines;
3838 diff_view_indicate_progress(view);
3839 err = create_diff(s);
3840 break;
3841 default:
3842 break;
3845 return err;
3848 static const struct got_error *
3849 cmd_diff(int argc, char *argv[])
3851 const struct got_error *error = NULL;
3852 struct got_repository *repo = NULL;
3853 struct got_worktree *worktree = NULL;
3854 struct got_object_id *id1 = NULL, *id2 = NULL;
3855 char *repo_path = NULL, *cwd = NULL;
3856 char *id_str1 = NULL, *id_str2 = NULL;
3857 char *label1 = NULL, *label2 = NULL;
3858 int diff_context = 3, ignore_whitespace = 0;
3859 int ch, force_text_diff = 0;
3860 const char *errstr;
3861 struct tog_view *view;
3863 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3864 switch (ch) {
3865 case 'a':
3866 force_text_diff = 1;
3867 break;
3868 case 'C':
3869 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3870 &errstr);
3871 if (errstr != NULL)
3872 err(1, "-C option %s", errstr);
3873 break;
3874 case 'r':
3875 repo_path = realpath(optarg, NULL);
3876 if (repo_path == NULL)
3877 return got_error_from_errno2("realpath",
3878 optarg);
3879 got_path_strip_trailing_slashes(repo_path);
3880 break;
3881 case 'w':
3882 ignore_whitespace = 1;
3883 break;
3884 default:
3885 usage_diff();
3886 /* NOTREACHED */
3890 argc -= optind;
3891 argv += optind;
3893 if (argc == 0) {
3894 usage_diff(); /* TODO show local worktree changes */
3895 } else if (argc == 2) {
3896 id_str1 = argv[0];
3897 id_str2 = argv[1];
3898 } else
3899 usage_diff();
3901 if (repo_path == NULL) {
3902 cwd = getcwd(NULL, 0);
3903 if (cwd == NULL)
3904 return got_error_from_errno("getcwd");
3905 error = got_worktree_open(&worktree, cwd);
3906 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3907 goto done;
3908 if (worktree)
3909 repo_path =
3910 strdup(got_worktree_get_repo_path(worktree));
3911 else
3912 repo_path = strdup(cwd);
3913 if (repo_path == NULL) {
3914 error = got_error_from_errno("strdup");
3915 goto done;
3919 error = got_repo_open(&repo, repo_path, NULL);
3920 if (error)
3921 goto done;
3923 init_curses();
3925 error = apply_unveil(got_repo_get_path(repo), NULL);
3926 if (error)
3927 goto done;
3929 error = tog_load_refs(repo, 0);
3930 if (error)
3931 goto done;
3933 error = got_repo_match_object_id(&id1, &label1, id_str1,
3934 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3935 if (error)
3936 goto done;
3938 error = got_repo_match_object_id(&id2, &label2, id_str2,
3939 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3940 if (error)
3941 goto done;
3943 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3944 if (view == NULL) {
3945 error = got_error_from_errno("view_open");
3946 goto done;
3948 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3949 ignore_whitespace, force_text_diff, NULL, repo);
3950 if (error)
3951 goto done;
3952 error = view_loop(view);
3953 done:
3954 free(label1);
3955 free(label2);
3956 free(repo_path);
3957 free(cwd);
3958 if (repo) {
3959 const struct got_error *close_err = got_repo_close(repo);
3960 if (error == NULL)
3961 error = close_err;
3963 if (worktree)
3964 got_worktree_close(worktree);
3965 tog_free_refs();
3966 return error;
3969 __dead static void
3970 usage_blame(void)
3972 endwin();
3973 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3974 getprogname());
3975 exit(1);
3978 struct tog_blame_line {
3979 int annotated;
3980 struct got_object_id *id;
3983 static const struct got_error *
3984 draw_blame(struct tog_view *view)
3986 struct tog_blame_view_state *s = &view->state.blame;
3987 struct tog_blame *blame = &s->blame;
3988 regmatch_t *regmatch = &view->regmatch;
3989 const struct got_error *err;
3990 int lineno = 0, nprinted = 0;
3991 char *line = NULL;
3992 size_t linesize = 0;
3993 ssize_t linelen;
3994 wchar_t *wline;
3995 int width;
3996 struct tog_blame_line *blame_line;
3997 struct got_object_id *prev_id = NULL;
3998 char *id_str;
3999 struct tog_color *tc;
4001 err = got_object_id_str(&id_str, s->blamed_commit->id);
4002 if (err)
4003 return err;
4005 rewind(blame->f);
4006 werase(view->window);
4008 if (asprintf(&line, "commit %s", id_str) == -1) {
4009 err = got_error_from_errno("asprintf");
4010 free(id_str);
4011 return err;
4014 err = format_line(&wline, &width, line, view->ncols, 0);
4015 free(line);
4016 line = NULL;
4017 if (err)
4018 return err;
4019 if (view_needs_focus_indication(view))
4020 wstandout(view->window);
4021 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4022 if (tc)
4023 wattr_on(view->window,
4024 COLOR_PAIR(tc->colorpair), NULL);
4025 waddwstr(view->window, wline);
4026 if (tc)
4027 wattr_off(view->window,
4028 COLOR_PAIR(tc->colorpair), NULL);
4029 if (view_needs_focus_indication(view))
4030 wstandend(view->window);
4031 free(wline);
4032 wline = NULL;
4033 if (width < view->ncols - 1)
4034 waddch(view->window, '\n');
4036 if (asprintf(&line, "[%d/%d] %s%s",
4037 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4038 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4039 free(id_str);
4040 return got_error_from_errno("asprintf");
4042 free(id_str);
4043 err = format_line(&wline, &width, line, view->ncols, 0);
4044 free(line);
4045 line = NULL;
4046 if (err)
4047 return err;
4048 waddwstr(view->window, wline);
4049 free(wline);
4050 wline = NULL;
4051 if (width < view->ncols - 1)
4052 waddch(view->window, '\n');
4054 s->eof = 0;
4055 while (nprinted < view->nlines - 2) {
4056 linelen = getline(&line, &linesize, blame->f);
4057 if (linelen == -1) {
4058 if (feof(blame->f)) {
4059 s->eof = 1;
4060 break;
4062 free(line);
4063 return got_ferror(blame->f, GOT_ERR_IO);
4065 if (++lineno < s->first_displayed_line)
4066 continue;
4068 if (view->focussed && nprinted == s->selected_line - 1)
4069 wstandout(view->window);
4071 if (blame->nlines > 0) {
4072 blame_line = &blame->lines[lineno - 1];
4073 if (blame_line->annotated && prev_id &&
4074 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4075 !(view->focussed &&
4076 nprinted == s->selected_line - 1)) {
4077 waddstr(view->window, " ");
4078 } else if (blame_line->annotated) {
4079 char *id_str;
4080 err = got_object_id_str(&id_str, blame_line->id);
4081 if (err) {
4082 free(line);
4083 return err;
4085 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4086 if (tc)
4087 wattr_on(view->window,
4088 COLOR_PAIR(tc->colorpair), NULL);
4089 wprintw(view->window, "%.8s", id_str);
4090 if (tc)
4091 wattr_off(view->window,
4092 COLOR_PAIR(tc->colorpair), NULL);
4093 free(id_str);
4094 prev_id = blame_line->id;
4095 } else {
4096 waddstr(view->window, "........");
4097 prev_id = NULL;
4099 } else {
4100 waddstr(view->window, "........");
4101 prev_id = NULL;
4104 if (view->focussed && nprinted == s->selected_line - 1)
4105 wstandend(view->window);
4106 waddstr(view->window, " ");
4108 if (view->ncols <= 9) {
4109 width = 9;
4110 wline = wcsdup(L"");
4111 if (wline == NULL) {
4112 err = got_error_from_errno("wcsdup");
4113 free(line);
4114 return err;
4116 } else if (s->first_displayed_line + nprinted ==
4117 s->matched_line &&
4118 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4119 err = add_matched_line(&width, line, view->ncols - 9, 9,
4120 view->window, regmatch);
4121 if (err) {
4122 free(line);
4123 return err;
4125 width += 9;
4126 } else {
4127 err = format_line(&wline, &width, line,
4128 view->ncols - 9, 9);
4129 waddwstr(view->window, wline);
4130 free(wline);
4131 wline = NULL;
4132 width += 9;
4135 if (width <= view->ncols - 1)
4136 waddch(view->window, '\n');
4137 if (++nprinted == 1)
4138 s->first_displayed_line = lineno;
4140 free(line);
4141 s->last_displayed_line = lineno;
4143 view_vborder(view);
4145 return NULL;
4148 static const struct got_error *
4149 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4151 const struct got_error *err = NULL;
4152 struct tog_blame_cb_args *a = arg;
4153 struct tog_blame_line *line;
4154 int errcode;
4156 if (nlines != a->nlines ||
4157 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4158 return got_error(GOT_ERR_RANGE);
4160 errcode = pthread_mutex_lock(&tog_mutex);
4161 if (errcode)
4162 return got_error_set_errno(errcode, "pthread_mutex_lock");
4164 if (*a->quit) { /* user has quit the blame view */
4165 err = got_error(GOT_ERR_ITER_COMPLETED);
4166 goto done;
4169 if (lineno == -1)
4170 goto done; /* no change in this commit */
4172 line = &a->lines[lineno - 1];
4173 if (line->annotated)
4174 goto done;
4176 line->id = got_object_id_dup(id);
4177 if (line->id == NULL) {
4178 err = got_error_from_errno("got_object_id_dup");
4179 goto done;
4181 line->annotated = 1;
4182 done:
4183 errcode = pthread_mutex_unlock(&tog_mutex);
4184 if (errcode)
4185 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4186 return err;
4189 static void *
4190 blame_thread(void *arg)
4192 const struct got_error *err, *close_err;
4193 struct tog_blame_thread_args *ta = arg;
4194 struct tog_blame_cb_args *a = ta->cb_args;
4195 int errcode;
4197 err = block_signals_used_by_main_thread();
4198 if (err)
4199 return (void *)err;
4201 err = got_blame(ta->path, a->commit_id, ta->repo,
4202 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4203 if (err && err->code == GOT_ERR_CANCELLED)
4204 err = NULL;
4206 errcode = pthread_mutex_lock(&tog_mutex);
4207 if (errcode)
4208 return (void *)got_error_set_errno(errcode,
4209 "pthread_mutex_lock");
4211 close_err = got_repo_close(ta->repo);
4212 if (err == NULL)
4213 err = close_err;
4214 ta->repo = NULL;
4215 *ta->complete = 1;
4217 errcode = pthread_mutex_unlock(&tog_mutex);
4218 if (errcode && err == NULL)
4219 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4221 return (void *)err;
4224 static struct got_object_id *
4225 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4226 int first_displayed_line, int selected_line)
4228 struct tog_blame_line *line;
4230 if (nlines <= 0)
4231 return NULL;
4233 line = &lines[first_displayed_line - 1 + selected_line - 1];
4234 if (!line->annotated)
4235 return NULL;
4237 return line->id;
4240 static const struct got_error *
4241 stop_blame(struct tog_blame *blame)
4243 const struct got_error *err = NULL;
4244 int i;
4246 if (blame->thread) {
4247 int errcode;
4248 errcode = pthread_mutex_unlock(&tog_mutex);
4249 if (errcode)
4250 return got_error_set_errno(errcode,
4251 "pthread_mutex_unlock");
4252 errcode = pthread_join(blame->thread, (void **)&err);
4253 if (errcode)
4254 return got_error_set_errno(errcode, "pthread_join");
4255 errcode = pthread_mutex_lock(&tog_mutex);
4256 if (errcode)
4257 return got_error_set_errno(errcode,
4258 "pthread_mutex_lock");
4259 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4260 err = NULL;
4261 blame->thread = 0; //NULL;
4263 if (blame->thread_args.repo) {
4264 const struct got_error *close_err;
4265 close_err = got_repo_close(blame->thread_args.repo);
4266 if (err == NULL)
4267 err = close_err;
4268 blame->thread_args.repo = NULL;
4270 if (blame->f) {
4271 if (fclose(blame->f) == EOF && err == NULL)
4272 err = got_error_from_errno("fclose");
4273 blame->f = NULL;
4275 if (blame->lines) {
4276 for (i = 0; i < blame->nlines; i++)
4277 free(blame->lines[i].id);
4278 free(blame->lines);
4279 blame->lines = NULL;
4281 free(blame->cb_args.commit_id);
4282 blame->cb_args.commit_id = NULL;
4284 return err;
4287 static const struct got_error *
4288 cancel_blame_view(void *arg)
4290 const struct got_error *err = NULL;
4291 int *done = arg;
4292 int errcode;
4294 errcode = pthread_mutex_lock(&tog_mutex);
4295 if (errcode)
4296 return got_error_set_errno(errcode,
4297 "pthread_mutex_unlock");
4299 if (*done)
4300 err = got_error(GOT_ERR_CANCELLED);
4302 errcode = pthread_mutex_unlock(&tog_mutex);
4303 if (errcode)
4304 return got_error_set_errno(errcode,
4305 "pthread_mutex_lock");
4307 return err;
4310 static const struct got_error *
4311 run_blame(struct tog_view *view)
4313 struct tog_blame_view_state *s = &view->state.blame;
4314 struct tog_blame *blame = &s->blame;
4315 const struct got_error *err = NULL;
4316 struct got_blob_object *blob = NULL;
4317 struct got_repository *thread_repo = NULL;
4318 struct got_object_id *obj_id = NULL;
4319 int obj_type;
4321 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4322 s->path);
4323 if (err)
4324 return err;
4326 err = got_object_get_type(&obj_type, s->repo, obj_id);
4327 if (err)
4328 goto done;
4330 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4331 err = got_error(GOT_ERR_OBJ_TYPE);
4332 goto done;
4335 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4336 if (err)
4337 goto done;
4338 blame->f = got_opentemp();
4339 if (blame->f == NULL) {
4340 err = got_error_from_errno("got_opentemp");
4341 goto done;
4343 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4344 &blame->line_offsets, blame->f, blob);
4345 if (err)
4346 goto done;
4347 if (blame->nlines == 0) {
4348 s->blame_complete = 1;
4349 goto done;
4352 /* Don't include \n at EOF in the blame line count. */
4353 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4354 blame->nlines--;
4356 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4357 if (blame->lines == NULL) {
4358 err = got_error_from_errno("calloc");
4359 goto done;
4362 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4363 if (err)
4364 goto done;
4366 blame->cb_args.view = view;
4367 blame->cb_args.lines = blame->lines;
4368 blame->cb_args.nlines = blame->nlines;
4369 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4370 if (blame->cb_args.commit_id == NULL) {
4371 err = got_error_from_errno("got_object_id_dup");
4372 goto done;
4374 blame->cb_args.quit = &s->done;
4376 blame->thread_args.path = s->path;
4377 blame->thread_args.repo = thread_repo;
4378 blame->thread_args.cb_args = &blame->cb_args;
4379 blame->thread_args.complete = &s->blame_complete;
4380 blame->thread_args.cancel_cb = cancel_blame_view;
4381 blame->thread_args.cancel_arg = &s->done;
4382 s->blame_complete = 0;
4384 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4385 s->first_displayed_line = 1;
4386 s->last_displayed_line = view->nlines;
4387 s->selected_line = 1;
4390 done:
4391 if (blob)
4392 got_object_blob_close(blob);
4393 free(obj_id);
4394 if (err)
4395 stop_blame(blame);
4396 return err;
4399 static const struct got_error *
4400 open_blame_view(struct tog_view *view, char *path,
4401 struct got_object_id *commit_id, struct got_repository *repo)
4403 const struct got_error *err = NULL;
4404 struct tog_blame_view_state *s = &view->state.blame;
4406 STAILQ_INIT(&s->blamed_commits);
4408 s->path = strdup(path);
4409 if (s->path == NULL)
4410 return got_error_from_errno("strdup");
4412 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4413 if (err) {
4414 free(s->path);
4415 return err;
4418 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4419 s->first_displayed_line = 1;
4420 s->last_displayed_line = view->nlines;
4421 s->selected_line = 1;
4422 s->blame_complete = 0;
4423 s->repo = repo;
4424 s->commit_id = commit_id;
4425 memset(&s->blame, 0, sizeof(s->blame));
4427 STAILQ_INIT(&s->colors);
4428 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4429 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4430 get_color_value("TOG_COLOR_COMMIT"));
4431 if (err)
4432 return err;
4435 view->show = show_blame_view;
4436 view->input = input_blame_view;
4437 view->close = close_blame_view;
4438 view->search_start = search_start_blame_view;
4439 view->search_next = search_next_blame_view;
4441 return run_blame(view);
4444 static const struct got_error *
4445 close_blame_view(struct tog_view *view)
4447 const struct got_error *err = NULL;
4448 struct tog_blame_view_state *s = &view->state.blame;
4450 if (s->blame.thread)
4451 err = stop_blame(&s->blame);
4453 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4454 struct got_object_qid *blamed_commit;
4455 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4456 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4457 got_object_qid_free(blamed_commit);
4460 free(s->path);
4461 free_colors(&s->colors);
4463 return err;
4466 static const struct got_error *
4467 search_start_blame_view(struct tog_view *view)
4469 struct tog_blame_view_state *s = &view->state.blame;
4471 s->matched_line = 0;
4472 return NULL;
4475 static const struct got_error *
4476 search_next_blame_view(struct tog_view *view)
4478 struct tog_blame_view_state *s = &view->state.blame;
4479 int lineno;
4480 char *line = NULL;
4481 size_t linesize = 0;
4482 ssize_t linelen;
4484 if (!view->searching) {
4485 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4486 return NULL;
4489 if (s->matched_line) {
4490 if (view->searching == TOG_SEARCH_FORWARD)
4491 lineno = s->matched_line + 1;
4492 else
4493 lineno = s->matched_line - 1;
4494 } else {
4495 if (view->searching == TOG_SEARCH_FORWARD)
4496 lineno = 1;
4497 else
4498 lineno = s->blame.nlines;
4501 while (1) {
4502 off_t offset;
4504 if (lineno <= 0 || lineno > s->blame.nlines) {
4505 if (s->matched_line == 0) {
4506 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4507 break;
4510 if (view->searching == TOG_SEARCH_FORWARD)
4511 lineno = 1;
4512 else
4513 lineno = s->blame.nlines;
4516 offset = s->blame.line_offsets[lineno - 1];
4517 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4518 free(line);
4519 return got_error_from_errno("fseeko");
4521 linelen = getline(&line, &linesize, s->blame.f);
4522 if (linelen != -1 &&
4523 match_line(line, &view->regex, 1, &view->regmatch)) {
4524 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4525 s->matched_line = lineno;
4526 break;
4528 if (view->searching == TOG_SEARCH_FORWARD)
4529 lineno++;
4530 else
4531 lineno--;
4533 free(line);
4535 if (s->matched_line) {
4536 s->first_displayed_line = s->matched_line;
4537 s->selected_line = 1;
4540 return NULL;
4543 static const struct got_error *
4544 show_blame_view(struct tog_view *view)
4546 const struct got_error *err = NULL;
4547 struct tog_blame_view_state *s = &view->state.blame;
4548 int errcode;
4550 if (s->blame.thread == 0 && !s->blame_complete) {
4551 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4552 &s->blame.thread_args);
4553 if (errcode)
4554 return got_error_set_errno(errcode, "pthread_create");
4556 halfdelay(1); /* fast refresh while annotating */
4559 if (s->blame_complete)
4560 halfdelay(10); /* disable fast refresh */
4562 err = draw_blame(view);
4564 view_vborder(view);
4565 return err;
4568 static const struct got_error *
4569 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4571 const struct got_error *err = NULL, *thread_err = NULL;
4572 struct tog_view *diff_view;
4573 struct tog_blame_view_state *s = &view->state.blame;
4574 int begin_x = 0;
4576 switch (ch) {
4577 case 'q':
4578 s->done = 1;
4579 break;
4580 case 'g':
4581 case KEY_HOME:
4582 s->selected_line = 1;
4583 s->first_displayed_line = 1;
4584 break;
4585 case 'G':
4586 case KEY_END:
4587 if (s->blame.nlines < view->nlines - 2) {
4588 s->selected_line = s->blame.nlines;
4589 s->first_displayed_line = 1;
4590 } else {
4591 s->selected_line = view->nlines - 2;
4592 s->first_displayed_line = s->blame.nlines -
4593 (view->nlines - 3);
4595 break;
4596 case 'k':
4597 case KEY_UP:
4598 case CTRL('p'):
4599 if (s->selected_line > 1)
4600 s->selected_line--;
4601 else if (s->selected_line == 1 &&
4602 s->first_displayed_line > 1)
4603 s->first_displayed_line--;
4604 break;
4605 case KEY_PPAGE:
4606 case CTRL('b'):
4607 if (s->first_displayed_line == 1) {
4608 s->selected_line = 1;
4609 break;
4611 if (s->first_displayed_line > view->nlines - 2)
4612 s->first_displayed_line -=
4613 (view->nlines - 2);
4614 else
4615 s->first_displayed_line = 1;
4616 break;
4617 case 'j':
4618 case KEY_DOWN:
4619 case CTRL('n'):
4620 if (s->selected_line < view->nlines - 2 &&
4621 s->first_displayed_line +
4622 s->selected_line <= s->blame.nlines)
4623 s->selected_line++;
4624 else if (s->last_displayed_line <
4625 s->blame.nlines)
4626 s->first_displayed_line++;
4627 break;
4628 case 'b':
4629 case 'p': {
4630 struct got_object_id *id = NULL;
4631 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4632 s->first_displayed_line, s->selected_line);
4633 if (id == NULL)
4634 break;
4635 if (ch == 'p') {
4636 struct got_commit_object *commit;
4637 struct got_object_qid *pid;
4638 struct got_object_id *blob_id = NULL;
4639 int obj_type;
4640 err = got_object_open_as_commit(&commit,
4641 s->repo, id);
4642 if (err)
4643 break;
4644 pid = STAILQ_FIRST(
4645 got_object_commit_get_parent_ids(commit));
4646 if (pid == NULL) {
4647 got_object_commit_close(commit);
4648 break;
4650 /* Check if path history ends here. */
4651 err = got_object_id_by_path(&blob_id, s->repo,
4652 pid->id, s->path);
4653 if (err) {
4654 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4655 err = NULL;
4656 got_object_commit_close(commit);
4657 break;
4659 err = got_object_get_type(&obj_type, s->repo,
4660 blob_id);
4661 free(blob_id);
4662 /* Can't blame non-blob type objects. */
4663 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4664 got_object_commit_close(commit);
4665 break;
4667 err = got_object_qid_alloc(&s->blamed_commit,
4668 pid->id);
4669 got_object_commit_close(commit);
4670 } else {
4671 if (got_object_id_cmp(id,
4672 s->blamed_commit->id) == 0)
4673 break;
4674 err = got_object_qid_alloc(&s->blamed_commit,
4675 id);
4677 if (err)
4678 break;
4679 s->done = 1;
4680 thread_err = stop_blame(&s->blame);
4681 s->done = 0;
4682 if (thread_err)
4683 break;
4684 STAILQ_INSERT_HEAD(&s->blamed_commits,
4685 s->blamed_commit, entry);
4686 err = run_blame(view);
4687 if (err)
4688 break;
4689 break;
4691 case 'B': {
4692 struct got_object_qid *first;
4693 first = STAILQ_FIRST(&s->blamed_commits);
4694 if (!got_object_id_cmp(first->id, s->commit_id))
4695 break;
4696 s->done = 1;
4697 thread_err = stop_blame(&s->blame);
4698 s->done = 0;
4699 if (thread_err)
4700 break;
4701 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4702 got_object_qid_free(s->blamed_commit);
4703 s->blamed_commit =
4704 STAILQ_FIRST(&s->blamed_commits);
4705 err = run_blame(view);
4706 if (err)
4707 break;
4708 break;
4710 case KEY_ENTER:
4711 case '\r': {
4712 struct got_object_id *id = NULL;
4713 struct got_object_qid *pid;
4714 struct got_commit_object *commit = NULL;
4715 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4716 s->first_displayed_line, s->selected_line);
4717 if (id == NULL)
4718 break;
4719 err = got_object_open_as_commit(&commit, s->repo, id);
4720 if (err)
4721 break;
4722 pid = STAILQ_FIRST(
4723 got_object_commit_get_parent_ids(commit));
4724 if (view_is_parent_view(view))
4725 begin_x = view_split_begin_x(view->begin_x);
4726 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4727 if (diff_view == NULL) {
4728 got_object_commit_close(commit);
4729 err = got_error_from_errno("view_open");
4730 break;
4732 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4733 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4734 got_object_commit_close(commit);
4735 if (err) {
4736 view_close(diff_view);
4737 break;
4739 view->focussed = 0;
4740 diff_view->focussed = 1;
4741 if (view_is_parent_view(view)) {
4742 err = view_close_child(view);
4743 if (err)
4744 break;
4745 view_set_child(view, diff_view);
4746 view->focus_child = 1;
4747 } else
4748 *new_view = diff_view;
4749 if (err)
4750 break;
4751 break;
4753 case KEY_NPAGE:
4754 case CTRL('f'):
4755 case ' ':
4756 if (s->last_displayed_line >= s->blame.nlines &&
4757 s->selected_line >= MIN(s->blame.nlines,
4758 view->nlines - 2)) {
4759 break;
4761 if (s->last_displayed_line >= s->blame.nlines &&
4762 s->selected_line < view->nlines - 2) {
4763 s->selected_line = MIN(s->blame.nlines,
4764 view->nlines - 2);
4765 break;
4767 if (s->last_displayed_line + view->nlines - 2
4768 <= s->blame.nlines)
4769 s->first_displayed_line +=
4770 view->nlines - 2;
4771 else
4772 s->first_displayed_line =
4773 s->blame.nlines -
4774 (view->nlines - 3);
4775 break;
4776 case KEY_RESIZE:
4777 if (s->selected_line > view->nlines - 2) {
4778 s->selected_line = MIN(s->blame.nlines,
4779 view->nlines - 2);
4781 break;
4782 default:
4783 break;
4785 return thread_err ? thread_err : err;
4788 static const struct got_error *
4789 cmd_blame(int argc, char *argv[])
4791 const struct got_error *error;
4792 struct got_repository *repo = NULL;
4793 struct got_worktree *worktree = NULL;
4794 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4795 char *link_target = NULL;
4796 struct got_object_id *commit_id = NULL;
4797 char *commit_id_str = NULL;
4798 int ch;
4799 struct tog_view *view;
4801 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4802 switch (ch) {
4803 case 'c':
4804 commit_id_str = optarg;
4805 break;
4806 case 'r':
4807 repo_path = realpath(optarg, NULL);
4808 if (repo_path == NULL)
4809 return got_error_from_errno2("realpath",
4810 optarg);
4811 break;
4812 default:
4813 usage_blame();
4814 /* NOTREACHED */
4818 argc -= optind;
4819 argv += optind;
4821 if (argc != 1)
4822 usage_blame();
4824 if (repo_path == NULL) {
4825 cwd = getcwd(NULL, 0);
4826 if (cwd == NULL)
4827 return got_error_from_errno("getcwd");
4828 error = got_worktree_open(&worktree, cwd);
4829 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4830 goto done;
4831 if (worktree)
4832 repo_path =
4833 strdup(got_worktree_get_repo_path(worktree));
4834 else
4835 repo_path = strdup(cwd);
4836 if (repo_path == NULL) {
4837 error = got_error_from_errno("strdup");
4838 goto done;
4842 error = got_repo_open(&repo, repo_path, NULL);
4843 if (error != NULL)
4844 goto done;
4846 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4847 worktree);
4848 if (error)
4849 goto done;
4851 init_curses();
4853 error = apply_unveil(got_repo_get_path(repo), NULL);
4854 if (error)
4855 goto done;
4857 error = tog_load_refs(repo, 0);
4858 if (error)
4859 goto done;
4861 if (commit_id_str == NULL) {
4862 struct got_reference *head_ref;
4863 error = got_ref_open(&head_ref, repo, worktree ?
4864 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4865 if (error != NULL)
4866 goto done;
4867 error = got_ref_resolve(&commit_id, repo, head_ref);
4868 got_ref_close(head_ref);
4869 } else {
4870 error = got_repo_match_object_id(&commit_id, NULL,
4871 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4873 if (error != NULL)
4874 goto done;
4876 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4877 if (view == NULL) {
4878 error = got_error_from_errno("view_open");
4879 goto done;
4882 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4883 commit_id, repo);
4884 if (error)
4885 goto done;
4887 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4888 commit_id, repo);
4889 if (error)
4890 goto done;
4891 if (worktree) {
4892 /* Release work tree lock. */
4893 got_worktree_close(worktree);
4894 worktree = NULL;
4896 error = view_loop(view);
4897 done:
4898 free(repo_path);
4899 free(in_repo_path);
4900 free(link_target);
4901 free(cwd);
4902 free(commit_id);
4903 if (worktree)
4904 got_worktree_close(worktree);
4905 if (repo) {
4906 const struct got_error *close_err = got_repo_close(repo);
4907 if (error == NULL)
4908 error = close_err;
4910 tog_free_refs();
4911 return error;
4914 static const struct got_error *
4915 draw_tree_entries(struct tog_view *view, const char *parent_path)
4917 struct tog_tree_view_state *s = &view->state.tree;
4918 const struct got_error *err = NULL;
4919 struct got_tree_entry *te;
4920 wchar_t *wline;
4921 struct tog_color *tc;
4922 int width, n, i, nentries;
4923 int limit = view->nlines;
4925 s->ndisplayed = 0;
4927 werase(view->window);
4929 if (limit == 0)
4930 return NULL;
4932 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4933 if (err)
4934 return err;
4935 if (view_needs_focus_indication(view))
4936 wstandout(view->window);
4937 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4938 if (tc)
4939 wattr_on(view->window,
4940 COLOR_PAIR(tc->colorpair), NULL);
4941 waddwstr(view->window, wline);
4942 if (tc)
4943 wattr_off(view->window,
4944 COLOR_PAIR(tc->colorpair), NULL);
4945 if (view_needs_focus_indication(view))
4946 wstandend(view->window);
4947 free(wline);
4948 wline = NULL;
4949 if (width < view->ncols - 1)
4950 waddch(view->window, '\n');
4951 if (--limit <= 0)
4952 return NULL;
4953 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4954 if (err)
4955 return err;
4956 waddwstr(view->window, wline);
4957 free(wline);
4958 wline = NULL;
4959 if (width < view->ncols - 1)
4960 waddch(view->window, '\n');
4961 if (--limit <= 0)
4962 return NULL;
4963 waddch(view->window, '\n');
4964 if (--limit <= 0)
4965 return NULL;
4967 if (s->first_displayed_entry == NULL) {
4968 te = got_object_tree_get_first_entry(s->tree);
4969 if (s->selected == 0) {
4970 if (view->focussed)
4971 wstandout(view->window);
4972 s->selected_entry = NULL;
4974 waddstr(view->window, " ..\n"); /* parent directory */
4975 if (s->selected == 0 && view->focussed)
4976 wstandend(view->window);
4977 s->ndisplayed++;
4978 if (--limit <= 0)
4979 return NULL;
4980 n = 1;
4981 } else {
4982 n = 0;
4983 te = s->first_displayed_entry;
4986 nentries = got_object_tree_get_nentries(s->tree);
4987 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4988 char *line = NULL, *id_str = NULL, *link_target = NULL;
4989 const char *modestr = "";
4990 mode_t mode;
4992 te = got_object_tree_get_entry(s->tree, i);
4993 mode = got_tree_entry_get_mode(te);
4995 if (s->show_ids) {
4996 err = got_object_id_str(&id_str,
4997 got_tree_entry_get_id(te));
4998 if (err)
4999 return got_error_from_errno(
5000 "got_object_id_str");
5002 if (got_object_tree_entry_is_submodule(te))
5003 modestr = "$";
5004 else if (S_ISLNK(mode)) {
5005 int i;
5007 err = got_tree_entry_get_symlink_target(&link_target,
5008 te, s->repo);
5009 if (err) {
5010 free(id_str);
5011 return err;
5013 for (i = 0; i < strlen(link_target); i++) {
5014 if (!isprint((unsigned char)link_target[i]))
5015 link_target[i] = '?';
5017 modestr = "@";
5019 else if (S_ISDIR(mode))
5020 modestr = "/";
5021 else if (mode & S_IXUSR)
5022 modestr = "*";
5023 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5024 got_tree_entry_get_name(te), modestr,
5025 link_target ? " -> ": "",
5026 link_target ? link_target : "") == -1) {
5027 free(id_str);
5028 free(link_target);
5029 return got_error_from_errno("asprintf");
5031 free(id_str);
5032 free(link_target);
5033 err = format_line(&wline, &width, line, view->ncols, 0);
5034 if (err) {
5035 free(line);
5036 break;
5038 if (n == s->selected) {
5039 if (view->focussed)
5040 wstandout(view->window);
5041 s->selected_entry = te;
5043 tc = match_color(&s->colors, line);
5044 if (tc)
5045 wattr_on(view->window,
5046 COLOR_PAIR(tc->colorpair), NULL);
5047 waddwstr(view->window, wline);
5048 if (tc)
5049 wattr_off(view->window,
5050 COLOR_PAIR(tc->colorpair), NULL);
5051 if (width < view->ncols - 1)
5052 waddch(view->window, '\n');
5053 if (n == s->selected && view->focussed)
5054 wstandend(view->window);
5055 free(line);
5056 free(wline);
5057 wline = NULL;
5058 n++;
5059 s->ndisplayed++;
5060 s->last_displayed_entry = te;
5061 if (--limit <= 0)
5062 break;
5065 return err;
5068 static void
5069 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5071 struct got_tree_entry *te;
5072 int isroot = s->tree == s->root;
5073 int i = 0;
5075 if (s->first_displayed_entry == NULL)
5076 return;
5078 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5079 while (i++ < maxscroll) {
5080 if (te == NULL) {
5081 if (!isroot)
5082 s->first_displayed_entry = NULL;
5083 break;
5085 s->first_displayed_entry = te;
5086 te = got_tree_entry_get_prev(s->tree, te);
5090 static void
5091 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5093 struct got_tree_entry *next, *last;
5094 int n = 0;
5096 if (s->first_displayed_entry)
5097 next = got_tree_entry_get_next(s->tree,
5098 s->first_displayed_entry);
5099 else
5100 next = got_object_tree_get_first_entry(s->tree);
5102 last = s->last_displayed_entry;
5103 while (next && last && n++ < maxscroll) {
5104 last = got_tree_entry_get_next(s->tree, last);
5105 if (last) {
5106 s->first_displayed_entry = next;
5107 next = got_tree_entry_get_next(s->tree, next);
5112 static const struct got_error *
5113 tree_entry_path(char **path, struct tog_parent_trees *parents,
5114 struct got_tree_entry *te)
5116 const struct got_error *err = NULL;
5117 struct tog_parent_tree *pt;
5118 size_t len = 2; /* for leading slash and NUL */
5120 TAILQ_FOREACH(pt, parents, entry)
5121 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5122 + 1 /* slash */;
5123 if (te)
5124 len += strlen(got_tree_entry_get_name(te));
5126 *path = calloc(1, len);
5127 if (path == NULL)
5128 return got_error_from_errno("calloc");
5130 (*path)[0] = '/';
5131 pt = TAILQ_LAST(parents, tog_parent_trees);
5132 while (pt) {
5133 const char *name = got_tree_entry_get_name(pt->selected_entry);
5134 if (strlcat(*path, name, len) >= len) {
5135 err = got_error(GOT_ERR_NO_SPACE);
5136 goto done;
5138 if (strlcat(*path, "/", len) >= len) {
5139 err = got_error(GOT_ERR_NO_SPACE);
5140 goto done;
5142 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5144 if (te) {
5145 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5146 err = got_error(GOT_ERR_NO_SPACE);
5147 goto done;
5150 done:
5151 if (err) {
5152 free(*path);
5153 *path = NULL;
5155 return err;
5158 static const struct got_error *
5159 blame_tree_entry(struct tog_view **new_view, int begin_x,
5160 struct got_tree_entry *te, struct tog_parent_trees *parents,
5161 struct got_object_id *commit_id, struct got_repository *repo)
5163 const struct got_error *err = NULL;
5164 char *path;
5165 struct tog_view *blame_view;
5167 *new_view = NULL;
5169 err = tree_entry_path(&path, parents, te);
5170 if (err)
5171 return err;
5173 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5174 if (blame_view == NULL) {
5175 err = got_error_from_errno("view_open");
5176 goto done;
5179 err = open_blame_view(blame_view, path, commit_id, repo);
5180 if (err) {
5181 if (err->code == GOT_ERR_CANCELLED)
5182 err = NULL;
5183 view_close(blame_view);
5184 } else
5185 *new_view = blame_view;
5186 done:
5187 free(path);
5188 return err;
5191 static const struct got_error *
5192 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5193 struct tog_tree_view_state *s)
5195 struct tog_view *log_view;
5196 const struct got_error *err = NULL;
5197 char *path;
5199 *new_view = NULL;
5201 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5202 if (log_view == NULL)
5203 return got_error_from_errno("view_open");
5205 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5206 if (err)
5207 return err;
5209 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5210 path, 0);
5211 if (err)
5212 view_close(log_view);
5213 else
5214 *new_view = log_view;
5215 free(path);
5216 return err;
5219 static const struct got_error *
5220 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5221 const char *head_ref_name, struct got_repository *repo)
5223 const struct got_error *err = NULL;
5224 char *commit_id_str = NULL;
5225 struct tog_tree_view_state *s = &view->state.tree;
5226 struct got_commit_object *commit = NULL;
5228 TAILQ_INIT(&s->parents);
5229 STAILQ_INIT(&s->colors);
5231 s->commit_id = got_object_id_dup(commit_id);
5232 if (s->commit_id == NULL)
5233 return got_error_from_errno("got_object_id_dup");
5235 err = got_object_open_as_commit(&commit, repo, commit_id);
5236 if (err)
5237 goto done;
5240 * The root is opened here and will be closed when the view is closed.
5241 * Any visited subtrees and their path-wise parents are opened and
5242 * closed on demand.
5244 err = got_object_open_as_tree(&s->root, repo,
5245 got_object_commit_get_tree_id(commit));
5246 if (err)
5247 goto done;
5248 s->tree = s->root;
5250 err = got_object_id_str(&commit_id_str, commit_id);
5251 if (err != NULL)
5252 goto done;
5254 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5255 err = got_error_from_errno("asprintf");
5256 goto done;
5259 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5260 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5261 if (head_ref_name) {
5262 s->head_ref_name = strdup(head_ref_name);
5263 if (s->head_ref_name == NULL) {
5264 err = got_error_from_errno("strdup");
5265 goto done;
5268 s->repo = repo;
5270 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5271 err = add_color(&s->colors, "\\$$",
5272 TOG_COLOR_TREE_SUBMODULE,
5273 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5274 if (err)
5275 goto done;
5276 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5277 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5278 if (err)
5279 goto done;
5280 err = add_color(&s->colors, "/$",
5281 TOG_COLOR_TREE_DIRECTORY,
5282 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5283 if (err)
5284 goto done;
5286 err = add_color(&s->colors, "\\*$",
5287 TOG_COLOR_TREE_EXECUTABLE,
5288 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5289 if (err)
5290 goto done;
5292 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5293 get_color_value("TOG_COLOR_COMMIT"));
5294 if (err)
5295 goto done;
5298 view->show = show_tree_view;
5299 view->input = input_tree_view;
5300 view->close = close_tree_view;
5301 view->search_start = search_start_tree_view;
5302 view->search_next = search_next_tree_view;
5303 done:
5304 free(commit_id_str);
5305 if (commit)
5306 got_object_commit_close(commit);
5307 if (err)
5308 close_tree_view(view);
5309 return err;
5312 static const struct got_error *
5313 close_tree_view(struct tog_view *view)
5315 struct tog_tree_view_state *s = &view->state.tree;
5317 free_colors(&s->colors);
5318 free(s->tree_label);
5319 s->tree_label = NULL;
5320 free(s->commit_id);
5321 s->commit_id = NULL;
5322 free(s->head_ref_name);
5323 s->head_ref_name = NULL;
5324 while (!TAILQ_EMPTY(&s->parents)) {
5325 struct tog_parent_tree *parent;
5326 parent = TAILQ_FIRST(&s->parents);
5327 TAILQ_REMOVE(&s->parents, parent, entry);
5328 if (parent->tree != s->root)
5329 got_object_tree_close(parent->tree);
5330 free(parent);
5333 if (s->tree != NULL && s->tree != s->root)
5334 got_object_tree_close(s->tree);
5335 if (s->root)
5336 got_object_tree_close(s->root);
5337 return NULL;
5340 static const struct got_error *
5341 search_start_tree_view(struct tog_view *view)
5343 struct tog_tree_view_state *s = &view->state.tree;
5345 s->matched_entry = NULL;
5346 return NULL;
5349 static int
5350 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5352 regmatch_t regmatch;
5354 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5355 0) == 0;
5358 static const struct got_error *
5359 search_next_tree_view(struct tog_view *view)
5361 struct tog_tree_view_state *s = &view->state.tree;
5362 struct got_tree_entry *te = NULL;
5364 if (!view->searching) {
5365 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5366 return NULL;
5369 if (s->matched_entry) {
5370 if (view->searching == TOG_SEARCH_FORWARD) {
5371 if (s->selected_entry)
5372 te = got_tree_entry_get_next(s->tree,
5373 s->selected_entry);
5374 else
5375 te = got_object_tree_get_first_entry(s->tree);
5376 } else {
5377 if (s->selected_entry == NULL)
5378 te = got_object_tree_get_last_entry(s->tree);
5379 else
5380 te = got_tree_entry_get_prev(s->tree,
5381 s->selected_entry);
5383 } else {
5384 if (view->searching == TOG_SEARCH_FORWARD)
5385 te = got_object_tree_get_first_entry(s->tree);
5386 else
5387 te = got_object_tree_get_last_entry(s->tree);
5390 while (1) {
5391 if (te == NULL) {
5392 if (s->matched_entry == NULL) {
5393 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5394 return NULL;
5396 if (view->searching == TOG_SEARCH_FORWARD)
5397 te = got_object_tree_get_first_entry(s->tree);
5398 else
5399 te = got_object_tree_get_last_entry(s->tree);
5402 if (match_tree_entry(te, &view->regex)) {
5403 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5404 s->matched_entry = te;
5405 break;
5408 if (view->searching == TOG_SEARCH_FORWARD)
5409 te = got_tree_entry_get_next(s->tree, te);
5410 else
5411 te = got_tree_entry_get_prev(s->tree, te);
5414 if (s->matched_entry) {
5415 s->first_displayed_entry = s->matched_entry;
5416 s->selected = 0;
5419 return NULL;
5422 static const struct got_error *
5423 show_tree_view(struct tog_view *view)
5425 const struct got_error *err = NULL;
5426 struct tog_tree_view_state *s = &view->state.tree;
5427 char *parent_path;
5429 err = tree_entry_path(&parent_path, &s->parents, NULL);
5430 if (err)
5431 return err;
5433 err = draw_tree_entries(view, parent_path);
5434 free(parent_path);
5436 view_vborder(view);
5437 return err;
5440 static const struct got_error *
5441 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5443 const struct got_error *err = NULL;
5444 struct tog_tree_view_state *s = &view->state.tree;
5445 struct tog_view *log_view, *ref_view;
5446 struct got_tree_entry *te;
5447 int begin_x = 0, n;
5449 switch (ch) {
5450 case 'i':
5451 s->show_ids = !s->show_ids;
5452 break;
5453 case 'l':
5454 if (!s->selected_entry)
5455 break;
5456 if (view_is_parent_view(view))
5457 begin_x = view_split_begin_x(view->begin_x);
5458 err = log_selected_tree_entry(&log_view, begin_x, s);
5459 view->focussed = 0;
5460 log_view->focussed = 1;
5461 if (view_is_parent_view(view)) {
5462 err = view_close_child(view);
5463 if (err)
5464 return err;
5465 view_set_child(view, log_view);
5466 view->focus_child = 1;
5467 } else
5468 *new_view = log_view;
5469 break;
5470 case 'r':
5471 if (view_is_parent_view(view))
5472 begin_x = view_split_begin_x(view->begin_x);
5473 ref_view = view_open(view->nlines, view->ncols,
5474 view->begin_y, begin_x, TOG_VIEW_REF);
5475 if (ref_view == NULL)
5476 return got_error_from_errno("view_open");
5477 err = open_ref_view(ref_view, s->repo);
5478 if (err) {
5479 view_close(ref_view);
5480 return err;
5482 view->focussed = 0;
5483 ref_view->focussed = 1;
5484 if (view_is_parent_view(view)) {
5485 err = view_close_child(view);
5486 if (err)
5487 return err;
5488 view_set_child(view, ref_view);
5489 view->focus_child = 1;
5490 } else
5491 *new_view = ref_view;
5492 break;
5493 case 'g':
5494 case KEY_HOME:
5495 s->selected = 0;
5496 if (s->tree == s->root)
5497 s->first_displayed_entry =
5498 got_object_tree_get_first_entry(s->tree);
5499 else
5500 s->first_displayed_entry = NULL;
5501 break;
5502 case 'G':
5503 case KEY_END:
5504 s->selected = 0;
5505 te = got_object_tree_get_last_entry(s->tree);
5506 for (n = 0; n < view->nlines - 3; n++) {
5507 if (te == NULL) {
5508 if(s->tree != s->root) {
5509 s->first_displayed_entry = NULL;
5510 n++;
5512 break;
5514 s->first_displayed_entry = te;
5515 te = got_tree_entry_get_prev(s->tree, te);
5517 if (n > 0)
5518 s->selected = n - 1;
5519 break;
5520 case 'k':
5521 case KEY_UP:
5522 case CTRL('p'):
5523 if (s->selected > 0) {
5524 s->selected--;
5525 break;
5527 tree_scroll_up(s, 1);
5528 break;
5529 case KEY_PPAGE:
5530 case CTRL('b'):
5531 if (s->tree == s->root) {
5532 if (got_object_tree_get_first_entry(s->tree) ==
5533 s->first_displayed_entry)
5534 s->selected = 0;
5535 } else {
5536 if (s->first_displayed_entry == NULL)
5537 s->selected = 0;
5539 tree_scroll_up(s, MAX(0, view->nlines - 3));
5540 break;
5541 case 'j':
5542 case KEY_DOWN:
5543 case CTRL('n'):
5544 if (s->selected < s->ndisplayed - 1) {
5545 s->selected++;
5546 break;
5548 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5549 == NULL)
5550 /* can't scroll any further */
5551 break;
5552 tree_scroll_down(s, 1);
5553 break;
5554 case KEY_NPAGE:
5555 case CTRL('f'):
5556 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5557 == NULL) {
5558 /* can't scroll any further; move cursor down */
5559 if (s->selected < s->ndisplayed - 1)
5560 s->selected = s->ndisplayed - 1;
5561 break;
5563 tree_scroll_down(s, view->nlines - 3);
5564 break;
5565 case KEY_ENTER:
5566 case '\r':
5567 case KEY_BACKSPACE:
5568 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5569 struct tog_parent_tree *parent;
5570 /* user selected '..' */
5571 if (s->tree == s->root)
5572 break;
5573 parent = TAILQ_FIRST(&s->parents);
5574 TAILQ_REMOVE(&s->parents, parent,
5575 entry);
5576 got_object_tree_close(s->tree);
5577 s->tree = parent->tree;
5578 s->first_displayed_entry =
5579 parent->first_displayed_entry;
5580 s->selected_entry =
5581 parent->selected_entry;
5582 s->selected = parent->selected;
5583 free(parent);
5584 } else if (S_ISDIR(got_tree_entry_get_mode(
5585 s->selected_entry))) {
5586 struct got_tree_object *subtree;
5587 err = got_object_open_as_tree(&subtree, s->repo,
5588 got_tree_entry_get_id(s->selected_entry));
5589 if (err)
5590 break;
5591 err = tree_view_visit_subtree(s, subtree);
5592 if (err) {
5593 got_object_tree_close(subtree);
5594 break;
5596 } else if (S_ISREG(got_tree_entry_get_mode(
5597 s->selected_entry))) {
5598 struct tog_view *blame_view;
5599 int begin_x = view_is_parent_view(view) ?
5600 view_split_begin_x(view->begin_x) : 0;
5602 err = blame_tree_entry(&blame_view, begin_x,
5603 s->selected_entry, &s->parents,
5604 s->commit_id, s->repo);
5605 if (err)
5606 break;
5607 view->focussed = 0;
5608 blame_view->focussed = 1;
5609 if (view_is_parent_view(view)) {
5610 err = view_close_child(view);
5611 if (err)
5612 return err;
5613 view_set_child(view, blame_view);
5614 view->focus_child = 1;
5615 } else
5616 *new_view = blame_view;
5618 break;
5619 case KEY_RESIZE:
5620 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5621 s->selected = view->nlines - 4;
5622 break;
5623 default:
5624 break;
5627 return err;
5630 __dead static void
5631 usage_tree(void)
5633 endwin();
5634 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5635 getprogname());
5636 exit(1);
5639 static const struct got_error *
5640 cmd_tree(int argc, char *argv[])
5642 const struct got_error *error;
5643 struct got_repository *repo = NULL;
5644 struct got_worktree *worktree = NULL;
5645 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5646 struct got_object_id *commit_id = NULL;
5647 const char *commit_id_arg = NULL;
5648 char *label = NULL;
5649 struct got_reference *ref = NULL;
5650 const char *head_ref_name = NULL;
5651 int ch;
5652 struct tog_view *view;
5654 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5655 switch (ch) {
5656 case 'c':
5657 commit_id_arg = optarg;
5658 break;
5659 case 'r':
5660 repo_path = realpath(optarg, NULL);
5661 if (repo_path == NULL)
5662 return got_error_from_errno2("realpath",
5663 optarg);
5664 break;
5665 default:
5666 usage_tree();
5667 /* NOTREACHED */
5671 argc -= optind;
5672 argv += optind;
5674 if (argc > 1)
5675 usage_tree();
5677 if (repo_path == NULL) {
5678 cwd = getcwd(NULL, 0);
5679 if (cwd == NULL)
5680 return got_error_from_errno("getcwd");
5681 error = got_worktree_open(&worktree, cwd);
5682 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5683 goto done;
5684 if (worktree)
5685 repo_path =
5686 strdup(got_worktree_get_repo_path(worktree));
5687 else
5688 repo_path = strdup(cwd);
5689 if (repo_path == NULL) {
5690 error = got_error_from_errno("strdup");
5691 goto done;
5695 error = got_repo_open(&repo, repo_path, NULL);
5696 if (error != NULL)
5697 goto done;
5699 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5700 repo, worktree);
5701 if (error)
5702 goto done;
5704 init_curses();
5706 error = apply_unveil(got_repo_get_path(repo), NULL);
5707 if (error)
5708 goto done;
5710 error = tog_load_refs(repo, 0);
5711 if (error)
5712 goto done;
5714 if (commit_id_arg == NULL) {
5715 error = got_repo_match_object_id(&commit_id, &label,
5716 worktree ? got_worktree_get_head_ref_name(worktree) :
5717 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5718 if (error)
5719 goto done;
5720 head_ref_name = label;
5721 } else {
5722 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5723 if (error == NULL)
5724 head_ref_name = got_ref_get_name(ref);
5725 else if (error->code != GOT_ERR_NOT_REF)
5726 goto done;
5727 error = got_repo_match_object_id(&commit_id, NULL,
5728 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5729 if (error)
5730 goto done;
5733 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5734 if (view == NULL) {
5735 error = got_error_from_errno("view_open");
5736 goto done;
5738 error = open_tree_view(view, commit_id, head_ref_name, repo);
5739 if (error)
5740 goto done;
5741 if (!got_path_is_root_dir(in_repo_path)) {
5742 error = tree_view_walk_path(&view->state.tree, commit_id,
5743 in_repo_path);
5744 if (error)
5745 goto done;
5748 if (worktree) {
5749 /* Release work tree lock. */
5750 got_worktree_close(worktree);
5751 worktree = NULL;
5753 error = view_loop(view);
5754 done:
5755 free(repo_path);
5756 free(cwd);
5757 free(commit_id);
5758 free(label);
5759 if (ref)
5760 got_ref_close(ref);
5761 if (repo) {
5762 const struct got_error *close_err = got_repo_close(repo);
5763 if (error == NULL)
5764 error = close_err;
5766 tog_free_refs();
5767 return error;
5770 static const struct got_error *
5771 ref_view_load_refs(struct tog_ref_view_state *s)
5773 struct got_reflist_entry *sre;
5774 struct tog_reflist_entry *re;
5776 s->nrefs = 0;
5777 TAILQ_FOREACH(sre, &tog_refs, entry) {
5778 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5779 continue;
5781 re = malloc(sizeof(*re));
5782 if (re == NULL)
5783 return got_error_from_errno("malloc");
5785 re->ref = got_ref_dup(sre->ref);
5786 if (re->ref == NULL)
5787 return got_error_from_errno("got_ref_dup");
5788 re->idx = s->nrefs++;
5789 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5792 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5793 return NULL;
5796 void
5797 ref_view_free_refs(struct tog_ref_view_state *s)
5799 struct tog_reflist_entry *re;
5801 while (!TAILQ_EMPTY(&s->refs)) {
5802 re = TAILQ_FIRST(&s->refs);
5803 TAILQ_REMOVE(&s->refs, re, entry);
5804 got_ref_close(re->ref);
5805 free(re);
5809 static const struct got_error *
5810 open_ref_view(struct tog_view *view, struct got_repository *repo)
5812 const struct got_error *err = NULL;
5813 struct tog_ref_view_state *s = &view->state.ref;
5815 s->selected_entry = 0;
5816 s->repo = repo;
5818 TAILQ_INIT(&s->refs);
5819 STAILQ_INIT(&s->colors);
5821 err = ref_view_load_refs(s);
5822 if (err)
5823 return err;
5825 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5826 err = add_color(&s->colors, "^refs/heads/",
5827 TOG_COLOR_REFS_HEADS,
5828 get_color_value("TOG_COLOR_REFS_HEADS"));
5829 if (err)
5830 goto done;
5832 err = add_color(&s->colors, "^refs/tags/",
5833 TOG_COLOR_REFS_TAGS,
5834 get_color_value("TOG_COLOR_REFS_TAGS"));
5835 if (err)
5836 goto done;
5838 err = add_color(&s->colors, "^refs/remotes/",
5839 TOG_COLOR_REFS_REMOTES,
5840 get_color_value("TOG_COLOR_REFS_REMOTES"));
5841 if (err)
5842 goto done;
5845 view->show = show_ref_view;
5846 view->input = input_ref_view;
5847 view->close = close_ref_view;
5848 view->search_start = search_start_ref_view;
5849 view->search_next = search_next_ref_view;
5850 done:
5851 if (err)
5852 free_colors(&s->colors);
5853 return err;
5856 static const struct got_error *
5857 close_ref_view(struct tog_view *view)
5859 struct tog_ref_view_state *s = &view->state.ref;
5861 ref_view_free_refs(s);
5862 free_colors(&s->colors);
5864 return NULL;
5867 static const struct got_error *
5868 resolve_reflist_entry(struct got_object_id **commit_id,
5869 struct tog_reflist_entry *re, struct got_repository *repo)
5871 const struct got_error *err = NULL;
5872 struct got_object_id *obj_id;
5873 struct got_tag_object *tag = NULL;
5874 int obj_type;
5876 *commit_id = NULL;
5878 err = got_ref_resolve(&obj_id, repo, re->ref);
5879 if (err)
5880 return err;
5882 err = got_object_get_type(&obj_type, repo, obj_id);
5883 if (err)
5884 goto done;
5886 switch (obj_type) {
5887 case GOT_OBJ_TYPE_COMMIT:
5888 *commit_id = obj_id;
5889 break;
5890 case GOT_OBJ_TYPE_TAG:
5891 err = got_object_open_as_tag(&tag, repo, obj_id);
5892 if (err)
5893 goto done;
5894 free(obj_id);
5895 err = got_object_get_type(&obj_type, repo,
5896 got_object_tag_get_object_id(tag));
5897 if (err)
5898 goto done;
5899 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5900 err = got_error(GOT_ERR_OBJ_TYPE);
5901 goto done;
5903 *commit_id = got_object_id_dup(
5904 got_object_tag_get_object_id(tag));
5905 if (*commit_id == NULL) {
5906 err = got_error_from_errno("got_object_id_dup");
5907 goto done;
5909 break;
5910 default:
5911 err = got_error(GOT_ERR_OBJ_TYPE);
5912 break;
5915 done:
5916 if (tag)
5917 got_object_tag_close(tag);
5918 if (err) {
5919 free(*commit_id);
5920 *commit_id = NULL;
5922 return err;
5925 static const struct got_error *
5926 log_ref_entry(struct tog_view **new_view, int begin_x,
5927 struct tog_reflist_entry *re, struct got_repository *repo)
5929 struct tog_view *log_view;
5930 const struct got_error *err = NULL;
5931 struct got_object_id *commit_id = NULL;
5933 *new_view = NULL;
5935 err = resolve_reflist_entry(&commit_id, re, repo);
5936 if (err) {
5937 if (err->code != GOT_ERR_OBJ_TYPE)
5938 return err;
5939 else
5940 return NULL;
5943 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5944 if (log_view == NULL) {
5945 err = got_error_from_errno("view_open");
5946 goto done;
5949 err = open_log_view(log_view, commit_id, repo,
5950 got_ref_get_name(re->ref), "", 0);
5951 done:
5952 if (err)
5953 view_close(log_view);
5954 else
5955 *new_view = log_view;
5956 free(commit_id);
5957 return err;
5960 static void
5961 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5963 struct tog_reflist_entry *re;
5964 int i = 0;
5966 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5967 return;
5969 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5970 while (i++ < maxscroll) {
5971 if (re == NULL)
5972 break;
5973 s->first_displayed_entry = re;
5974 re = TAILQ_PREV(re, tog_reflist_head, entry);
5978 static void
5979 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5981 struct tog_reflist_entry *next, *last;
5982 int n = 0;
5984 if (s->first_displayed_entry)
5985 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5986 else
5987 next = TAILQ_FIRST(&s->refs);
5989 last = s->last_displayed_entry;
5990 while (next && last && n++ < maxscroll) {
5991 last = TAILQ_NEXT(last, entry);
5992 if (last) {
5993 s->first_displayed_entry = next;
5994 next = TAILQ_NEXT(next, entry);
5999 static const struct got_error *
6000 search_start_ref_view(struct tog_view *view)
6002 struct tog_ref_view_state *s = &view->state.ref;
6004 s->matched_entry = NULL;
6005 return NULL;
6008 static int
6009 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6011 regmatch_t regmatch;
6013 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6014 0) == 0;
6017 static const struct got_error *
6018 search_next_ref_view(struct tog_view *view)
6020 struct tog_ref_view_state *s = &view->state.ref;
6021 struct tog_reflist_entry *re = NULL;
6023 if (!view->searching) {
6024 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6025 return NULL;
6028 if (s->matched_entry) {
6029 if (view->searching == TOG_SEARCH_FORWARD) {
6030 if (s->selected_entry)
6031 re = TAILQ_NEXT(s->selected_entry, entry);
6032 else
6033 re = TAILQ_PREV(s->selected_entry,
6034 tog_reflist_head, entry);
6035 } else {
6036 if (s->selected_entry == NULL)
6037 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6038 else
6039 re = TAILQ_PREV(s->selected_entry,
6040 tog_reflist_head, entry);
6042 } else {
6043 if (view->searching == TOG_SEARCH_FORWARD)
6044 re = TAILQ_FIRST(&s->refs);
6045 else
6046 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6049 while (1) {
6050 if (re == NULL) {
6051 if (s->matched_entry == NULL) {
6052 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6053 return NULL;
6055 if (view->searching == TOG_SEARCH_FORWARD)
6056 re = TAILQ_FIRST(&s->refs);
6057 else
6058 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6061 if (match_reflist_entry(re, &view->regex)) {
6062 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6063 s->matched_entry = re;
6064 break;
6067 if (view->searching == TOG_SEARCH_FORWARD)
6068 re = TAILQ_NEXT(re, entry);
6069 else
6070 re = TAILQ_PREV(re, tog_reflist_head, entry);
6073 if (s->matched_entry) {
6074 s->first_displayed_entry = s->matched_entry;
6075 s->selected = 0;
6078 return NULL;
6081 static const struct got_error *
6082 show_ref_view(struct tog_view *view)
6084 const struct got_error *err = NULL;
6085 struct tog_ref_view_state *s = &view->state.ref;
6086 struct tog_reflist_entry *re;
6087 char *line = NULL;
6088 wchar_t *wline;
6089 struct tog_color *tc;
6090 int width, n;
6091 int limit = view->nlines;
6093 werase(view->window);
6095 s->ndisplayed = 0;
6097 if (limit == 0)
6098 return NULL;
6100 re = s->first_displayed_entry;
6102 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6103 s->nrefs) == -1)
6104 return got_error_from_errno("asprintf");
6106 err = format_line(&wline, &width, line, view->ncols, 0);
6107 if (err) {
6108 free(line);
6109 return err;
6111 if (view_needs_focus_indication(view))
6112 wstandout(view->window);
6113 waddwstr(view->window, wline);
6114 if (view_needs_focus_indication(view))
6115 wstandend(view->window);
6116 free(wline);
6117 wline = NULL;
6118 free(line);
6119 line = NULL;
6120 if (width < view->ncols - 1)
6121 waddch(view->window, '\n');
6122 if (--limit <= 0)
6123 return NULL;
6125 n = 0;
6126 while (re && limit > 0) {
6127 char *line = NULL;
6129 if (got_ref_is_symbolic(re->ref)) {
6130 if (asprintf(&line, "%s -> %s",
6131 got_ref_get_name(re->ref),
6132 got_ref_get_symref_target(re->ref)) == -1)
6133 return got_error_from_errno("asprintf");
6134 } else if (s->show_ids) {
6135 struct got_object_id *id;
6136 char *id_str;
6137 err = got_ref_resolve(&id, s->repo, re->ref);
6138 if (err)
6139 return err;
6140 err = got_object_id_str(&id_str, id);
6141 if (err) {
6142 free(id);
6143 return err;
6145 if (asprintf(&line, "%s: %s",
6146 got_ref_get_name(re->ref), id_str) == -1) {
6147 err = got_error_from_errno("asprintf");
6148 free(id);
6149 free(id_str);
6150 return err;
6152 free(id);
6153 free(id_str);
6154 } else {
6155 line = strdup(got_ref_get_name(re->ref));
6156 if (line == NULL)
6157 return got_error_from_errno("strdup");
6160 err = format_line(&wline, &width, line, view->ncols, 0);
6161 if (err) {
6162 free(line);
6163 return err;
6165 if (n == s->selected) {
6166 if (view->focussed)
6167 wstandout(view->window);
6168 s->selected_entry = re;
6170 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6171 if (tc)
6172 wattr_on(view->window,
6173 COLOR_PAIR(tc->colorpair), NULL);
6174 waddwstr(view->window, wline);
6175 if (tc)
6176 wattr_off(view->window,
6177 COLOR_PAIR(tc->colorpair), NULL);
6178 if (width < view->ncols - 1)
6179 waddch(view->window, '\n');
6180 if (n == s->selected && view->focussed)
6181 wstandend(view->window);
6182 free(line);
6183 free(wline);
6184 wline = NULL;
6185 n++;
6186 s->ndisplayed++;
6187 s->last_displayed_entry = re;
6189 limit--;
6190 re = TAILQ_NEXT(re, entry);
6193 view_vborder(view);
6194 return err;
6197 static const struct got_error *
6198 browse_ref_tree(struct tog_view **new_view, int begin_x,
6199 struct tog_reflist_entry *re, struct got_repository *repo)
6201 const struct got_error *err = NULL;
6202 struct got_object_id *commit_id = NULL;
6203 struct tog_view *tree_view;
6205 *new_view = NULL;
6207 err = resolve_reflist_entry(&commit_id, re, repo);
6208 if (err) {
6209 if (err->code != GOT_ERR_OBJ_TYPE)
6210 return err;
6211 else
6212 return NULL;
6216 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6217 if (tree_view == NULL) {
6218 err = got_error_from_errno("view_open");
6219 goto done;
6222 err = open_tree_view(tree_view, commit_id,
6223 got_ref_get_name(re->ref), repo);
6224 if (err)
6225 goto done;
6227 *new_view = tree_view;
6228 done:
6229 free(commit_id);
6230 return err;
6232 static const struct got_error *
6233 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6235 const struct got_error *err = NULL;
6236 struct tog_ref_view_state *s = &view->state.ref;
6237 struct tog_view *log_view, *tree_view;
6238 struct tog_reflist_entry *re;
6239 int begin_x = 0, n;
6241 switch (ch) {
6242 case 'i':
6243 s->show_ids = !s->show_ids;
6244 break;
6245 case 'o':
6246 s->sort_by_date = !s->sort_by_date;
6247 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6248 got_ref_cmp_by_commit_timestamp_descending :
6249 got_ref_cmp_by_name, s->repo);
6250 if (err)
6251 break;
6252 got_reflist_object_id_map_free(tog_refs_idmap);
6253 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6254 &tog_refs, s->repo);
6255 if (err)
6256 break;
6257 ref_view_free_refs(s);
6258 err = ref_view_load_refs(s);
6259 break;
6260 case KEY_ENTER:
6261 case '\r':
6262 if (!s->selected_entry)
6263 break;
6264 if (view_is_parent_view(view))
6265 begin_x = view_split_begin_x(view->begin_x);
6266 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6267 s->repo);
6268 view->focussed = 0;
6269 log_view->focussed = 1;
6270 if (view_is_parent_view(view)) {
6271 err = view_close_child(view);
6272 if (err)
6273 return err;
6274 view_set_child(view, log_view);
6275 view->focus_child = 1;
6276 } else
6277 *new_view = log_view;
6278 break;
6279 case 't':
6280 if (!s->selected_entry)
6281 break;
6282 if (view_is_parent_view(view))
6283 begin_x = view_split_begin_x(view->begin_x);
6284 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6285 s->repo);
6286 if (err || tree_view == NULL)
6287 break;
6288 view->focussed = 0;
6289 tree_view->focussed = 1;
6290 if (view_is_parent_view(view)) {
6291 err = view_close_child(view);
6292 if (err)
6293 return err;
6294 view_set_child(view, tree_view);
6295 view->focus_child = 1;
6296 } else
6297 *new_view = tree_view;
6298 break;
6299 case 'g':
6300 case KEY_HOME:
6301 s->selected = 0;
6302 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6303 break;
6304 case 'G':
6305 case KEY_END:
6306 s->selected = 0;
6307 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6308 for (n = 0; n < view->nlines - 1; n++) {
6309 if (re == NULL)
6310 break;
6311 s->first_displayed_entry = re;
6312 re = TAILQ_PREV(re, tog_reflist_head, entry);
6314 if (n > 0)
6315 s->selected = n - 1;
6316 break;
6317 case 'k':
6318 case KEY_UP:
6319 case CTRL('p'):
6320 if (s->selected > 0) {
6321 s->selected--;
6322 break;
6324 ref_scroll_up(s, 1);
6325 break;
6326 case KEY_PPAGE:
6327 case CTRL('b'):
6328 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6329 s->selected = 0;
6330 ref_scroll_up(s, MAX(0, view->nlines - 1));
6331 break;
6332 case 'j':
6333 case KEY_DOWN:
6334 case CTRL('n'):
6335 if (s->selected < s->ndisplayed - 1) {
6336 s->selected++;
6337 break;
6339 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6340 /* can't scroll any further */
6341 break;
6342 ref_scroll_down(s, 1);
6343 break;
6344 case KEY_NPAGE:
6345 case CTRL('f'):
6346 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6347 /* can't scroll any further; move cursor down */
6348 if (s->selected < s->ndisplayed - 1)
6349 s->selected = s->ndisplayed - 1;
6350 break;
6352 ref_scroll_down(s, view->nlines - 1);
6353 break;
6354 case CTRL('l'):
6355 tog_free_refs();
6356 err = tog_load_refs(s->repo, s->sort_by_date);
6357 if (err)
6358 break;
6359 ref_view_free_refs(s);
6360 err = ref_view_load_refs(s);
6361 break;
6362 case KEY_RESIZE:
6363 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6364 s->selected = view->nlines - 2;
6365 break;
6366 default:
6367 break;
6370 return err;
6373 __dead static void
6374 usage_ref(void)
6376 endwin();
6377 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6378 getprogname());
6379 exit(1);
6382 static const struct got_error *
6383 cmd_ref(int argc, char *argv[])
6385 const struct got_error *error;
6386 struct got_repository *repo = NULL;
6387 struct got_worktree *worktree = NULL;
6388 char *cwd = NULL, *repo_path = NULL;
6389 int ch;
6390 struct tog_view *view;
6392 while ((ch = getopt(argc, argv, "r:")) != -1) {
6393 switch (ch) {
6394 case 'r':
6395 repo_path = realpath(optarg, NULL);
6396 if (repo_path == NULL)
6397 return got_error_from_errno2("realpath",
6398 optarg);
6399 break;
6400 default:
6401 usage_ref();
6402 /* NOTREACHED */
6406 argc -= optind;
6407 argv += optind;
6409 if (argc > 1)
6410 usage_ref();
6412 if (repo_path == NULL) {
6413 cwd = getcwd(NULL, 0);
6414 if (cwd == NULL)
6415 return got_error_from_errno("getcwd");
6416 error = got_worktree_open(&worktree, cwd);
6417 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6418 goto done;
6419 if (worktree)
6420 repo_path =
6421 strdup(got_worktree_get_repo_path(worktree));
6422 else
6423 repo_path = strdup(cwd);
6424 if (repo_path == NULL) {
6425 error = got_error_from_errno("strdup");
6426 goto done;
6430 error = got_repo_open(&repo, repo_path, NULL);
6431 if (error != NULL)
6432 goto done;
6434 init_curses();
6436 error = apply_unveil(got_repo_get_path(repo), NULL);
6437 if (error)
6438 goto done;
6440 error = tog_load_refs(repo, 0);
6441 if (error)
6442 goto done;
6444 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6445 if (view == NULL) {
6446 error = got_error_from_errno("view_open");
6447 goto done;
6450 error = open_ref_view(view, repo);
6451 if (error)
6452 goto done;
6454 if (worktree) {
6455 /* Release work tree lock. */
6456 got_worktree_close(worktree);
6457 worktree = NULL;
6459 error = view_loop(view);
6460 done:
6461 free(repo_path);
6462 free(cwd);
6463 if (repo) {
6464 const struct got_error *close_err = got_repo_close(repo);
6465 if (close_err)
6466 error = close_err;
6468 tog_free_refs();
6469 return error;
6472 static void
6473 list_commands(FILE *fp)
6475 size_t i;
6477 fprintf(fp, "commands:");
6478 for (i = 0; i < nitems(tog_commands); i++) {
6479 struct tog_cmd *cmd = &tog_commands[i];
6480 fprintf(fp, " %s", cmd->name);
6482 fputc('\n', fp);
6485 __dead static void
6486 usage(int hflag, int status)
6488 FILE *fp = (status == 0) ? stdout : stderr;
6490 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6491 getprogname());
6492 if (hflag) {
6493 fprintf(fp, "lazy usage: %s path\n", getprogname());
6494 list_commands(fp);
6496 exit(status);
6499 static char **
6500 make_argv(int argc, ...)
6502 va_list ap;
6503 char **argv;
6504 int i;
6506 va_start(ap, argc);
6508 argv = calloc(argc, sizeof(char *));
6509 if (argv == NULL)
6510 err(1, "calloc");
6511 for (i = 0; i < argc; i++) {
6512 argv[i] = strdup(va_arg(ap, char *));
6513 if (argv[i] == NULL)
6514 err(1, "strdup");
6517 va_end(ap);
6518 return argv;
6522 * Try to convert 'tog path' into a 'tog log path' command.
6523 * The user could simply have mistyped the command rather than knowingly
6524 * provided a path. So check whether argv[0] can in fact be resolved
6525 * to a path in the HEAD commit and print a special error if not.
6526 * This hack is for mpi@ <3
6528 static const struct got_error *
6529 tog_log_with_path(int argc, char *argv[])
6531 const struct got_error *error = NULL, *close_err;
6532 struct tog_cmd *cmd = NULL;
6533 struct got_repository *repo = NULL;
6534 struct got_worktree *worktree = NULL;
6535 struct got_object_id *commit_id = NULL, *id = NULL;
6536 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6537 char *commit_id_str = NULL, **cmd_argv = NULL;
6539 cwd = getcwd(NULL, 0);
6540 if (cwd == NULL)
6541 return got_error_from_errno("getcwd");
6543 error = got_worktree_open(&worktree, cwd);
6544 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6545 goto done;
6547 if (worktree)
6548 repo_path = strdup(got_worktree_get_repo_path(worktree));
6549 else
6550 repo_path = strdup(cwd);
6551 if (repo_path == NULL) {
6552 error = got_error_from_errno("strdup");
6553 goto done;
6556 error = got_repo_open(&repo, repo_path, NULL);
6557 if (error != NULL)
6558 goto done;
6560 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6561 repo, worktree);
6562 if (error)
6563 goto done;
6565 error = tog_load_refs(repo, 0);
6566 if (error)
6567 goto done;
6568 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6569 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6570 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6571 if (error)
6572 goto done;
6574 if (worktree) {
6575 got_worktree_close(worktree);
6576 worktree = NULL;
6579 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6580 if (error) {
6581 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6582 goto done;
6583 fprintf(stderr, "%s: '%s' is no known command or path\n",
6584 getprogname(), argv[0]);
6585 usage(1, 1);
6586 /* not reached */
6589 close_err = got_repo_close(repo);
6590 if (error == NULL)
6591 error = close_err;
6592 repo = NULL;
6594 error = got_object_id_str(&commit_id_str, commit_id);
6595 if (error)
6596 goto done;
6598 cmd = &tog_commands[0]; /* log */
6599 argc = 4;
6600 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6601 error = cmd->cmd_main(argc, cmd_argv);
6602 done:
6603 if (repo) {
6604 close_err = got_repo_close(repo);
6605 if (error == NULL)
6606 error = close_err;
6608 if (worktree)
6609 got_worktree_close(worktree);
6610 free(id);
6611 free(commit_id_str);
6612 free(commit_id);
6613 free(cwd);
6614 free(repo_path);
6615 free(in_repo_path);
6616 if (cmd_argv) {
6617 int i;
6618 for (i = 0; i < argc; i++)
6619 free(cmd_argv[i]);
6620 free(cmd_argv);
6622 tog_free_refs();
6623 return error;
6626 int
6627 main(int argc, char *argv[])
6629 const struct got_error *error = NULL;
6630 struct tog_cmd *cmd = NULL;
6631 int ch, hflag = 0, Vflag = 0;
6632 char **cmd_argv = NULL;
6633 static struct option longopts[] = {
6634 { "version", no_argument, NULL, 'V' },
6635 { NULL, 0, NULL, 0}
6638 setlocale(LC_CTYPE, "");
6640 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6641 switch (ch) {
6642 case 'h':
6643 hflag = 1;
6644 break;
6645 case 'V':
6646 Vflag = 1;
6647 break;
6648 default:
6649 usage(hflag, 1);
6650 /* NOTREACHED */
6654 argc -= optind;
6655 argv += optind;
6656 optind = 1;
6657 optreset = 1;
6659 if (Vflag) {
6660 got_version_print_str();
6661 return 0;
6664 #ifndef PROFILE
6665 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6666 NULL) == -1)
6667 err(1, "pledge");
6668 #endif
6670 if (argc == 0) {
6671 if (hflag)
6672 usage(hflag, 0);
6673 /* Build an argument vector which runs a default command. */
6674 cmd = &tog_commands[0];
6675 argc = 1;
6676 cmd_argv = make_argv(argc, cmd->name);
6677 } else {
6678 size_t i;
6680 /* Did the user specify a command? */
6681 for (i = 0; i < nitems(tog_commands); i++) {
6682 if (strncmp(tog_commands[i].name, argv[0],
6683 strlen(argv[0])) == 0) {
6684 cmd = &tog_commands[i];
6685 break;
6690 if (cmd == NULL) {
6691 if (argc != 1)
6692 usage(0, 1);
6693 /* No command specified; try log with a path */
6694 error = tog_log_with_path(argc, argv);
6695 } else {
6696 if (hflag)
6697 cmd->cmd_usage();
6698 else
6699 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6702 endwin();
6703 putchar('\n');
6704 if (cmd_argv) {
6705 int i;
6706 for (i = 0; i < argc; i++)
6707 free(cmd_argv[i]);
6708 free(cmd_argv);
6711 if (error && error->code != GOT_ERR_CANCELLED)
6712 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6713 return 0;