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/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED
24 #include <curses.h>
25 #undef _XOPEN_SOURCE_EXTENDED
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>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_diff.h"
49 #include "got_opentemp.h"
50 #include "got_utf8.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
55 #include "got_path.h"
56 #include "got_worktree.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #ifndef MAX
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 #endif
66 #define CTRL(x) ((x) & 0x1f)
68 #ifndef nitems
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #endif
72 struct tog_cmd {
73 const char *name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 };
78 __dead static void usage(int, int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
83 __dead static void usage_ref(void);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
89 static const struct got_error* cmd_ref(int, char *[]);
91 static struct tog_cmd tog_commands[] = {
92 { "log", cmd_log, usage_log },
93 { "diff", cmd_diff, usage_diff },
94 { "blame", cmd_blame, usage_blame },
95 { "tree", cmd_tree, usage_tree },
96 { "ref", cmd_ref, usage_ref },
97 };
99 enum tog_view_type {
100 TOG_VIEW_DIFF,
101 TOG_VIEW_LOG,
102 TOG_VIEW_BLAME,
103 TOG_VIEW_TREE,
104 TOG_VIEW_REF,
105 };
107 #define TOG_EOF_STRING "(END)"
109 struct commit_queue_entry {
110 TAILQ_ENTRY(commit_queue_entry) entry;
111 struct got_object_id *id;
112 struct got_commit_object *commit;
113 int idx;
114 };
115 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 struct commit_queue {
117 int ncommits;
118 struct commit_queue_head head;
119 };
121 struct tog_color {
122 STAILQ_ENTRY(tog_color) entry;
123 regex_t regex;
124 short colorpair;
125 };
126 STAILQ_HEAD(tog_colors, tog_color);
128 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
129 static struct got_reflist_object_id_map *tog_refs_idmap;
131 static const struct got_error *
132 tog_load_refs(struct got_repository *repo)
134 const struct got_error *err;
136 err = got_ref_list(&tog_refs, repo, NULL, got_ref_cmp_by_name, NULL);
137 if (err)
138 return err;
140 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
141 repo);
144 static void
145 tog_free_refs(void)
147 if (tog_refs_idmap) {
148 got_reflist_object_id_map_free(tog_refs_idmap);
149 tog_refs_idmap = NULL;
151 got_ref_list_free(&tog_refs);
154 static const struct got_error *
155 add_color(struct tog_colors *colors, const char *pattern,
156 int idx, short color)
158 const struct got_error *err = NULL;
159 struct tog_color *tc;
160 int regerr = 0;
162 if (idx < 1 || idx > COLOR_PAIRS - 1)
163 return NULL;
165 init_pair(idx, color, -1);
167 tc = calloc(1, sizeof(*tc));
168 if (tc == NULL)
169 return got_error_from_errno("calloc");
170 regerr = regcomp(&tc->regex, pattern,
171 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
172 if (regerr) {
173 static char regerr_msg[512];
174 static char err_msg[512];
175 regerror(regerr, &tc->regex, regerr_msg,
176 sizeof(regerr_msg));
177 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
178 regerr_msg);
179 err = got_error_msg(GOT_ERR_REGEX, err_msg);
180 free(tc);
181 return err;
183 tc->colorpair = idx;
184 STAILQ_INSERT_HEAD(colors, tc, entry);
185 return NULL;
188 static void
189 free_colors(struct tog_colors *colors)
191 struct tog_color *tc;
193 while (!STAILQ_EMPTY(colors)) {
194 tc = STAILQ_FIRST(colors);
195 STAILQ_REMOVE_HEAD(colors, entry);
196 regfree(&tc->regex);
197 free(tc);
201 struct tog_color *
202 get_color(struct tog_colors *colors, int colorpair)
204 struct tog_color *tc = NULL;
206 STAILQ_FOREACH(tc, colors, entry) {
207 if (tc->colorpair == colorpair)
208 return tc;
211 return NULL;
214 static int
215 default_color_value(const char *envvar)
217 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
218 return COLOR_MAGENTA;
219 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
220 return COLOR_CYAN;
221 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
222 return COLOR_YELLOW;
223 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
224 return COLOR_GREEN;
225 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
226 return COLOR_MAGENTA;
227 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
228 return COLOR_MAGENTA;
229 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
230 return COLOR_CYAN;
231 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
232 return COLOR_GREEN;
233 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
234 return COLOR_GREEN;
235 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
236 return COLOR_CYAN;
237 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
238 return COLOR_YELLOW;
239 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
240 return COLOR_GREEN;
241 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
242 return COLOR_MAGENTA;
243 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
244 return COLOR_YELLOW;
246 return -1;
249 static int
250 get_color_value(const char *envvar)
252 const char *val = getenv(envvar);
254 if (val == NULL)
255 return default_color_value(envvar);
257 if (strcasecmp(val, "black") == 0)
258 return COLOR_BLACK;
259 if (strcasecmp(val, "red") == 0)
260 return COLOR_RED;
261 if (strcasecmp(val, "green") == 0)
262 return COLOR_GREEN;
263 if (strcasecmp(val, "yellow") == 0)
264 return COLOR_YELLOW;
265 if (strcasecmp(val, "blue") == 0)
266 return COLOR_BLUE;
267 if (strcasecmp(val, "magenta") == 0)
268 return COLOR_MAGENTA;
269 if (strcasecmp(val, "cyan") == 0)
270 return COLOR_CYAN;
271 if (strcasecmp(val, "white") == 0)
272 return COLOR_WHITE;
273 if (strcasecmp(val, "default") == 0)
274 return -1;
276 return default_color_value(envvar);
280 struct tog_diff_view_state {
281 struct got_object_id *id1, *id2;
282 const char *label1, *label2;
283 FILE *f;
284 int first_displayed_line;
285 int last_displayed_line;
286 int eof;
287 int diff_context;
288 int ignore_whitespace;
289 int force_text_diff;
290 struct got_repository *repo;
291 struct tog_colors colors;
292 size_t nlines;
293 off_t *line_offsets;
294 int matched_line;
295 int selected_line;
297 /* passed from log view; may be NULL */
298 struct tog_view *log_view;
299 };
301 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
303 struct tog_log_thread_args {
304 pthread_cond_t need_commits;
305 pthread_cond_t commit_loaded;
306 int commits_needed;
307 struct got_commit_graph *graph;
308 struct commit_queue *commits;
309 const char *in_repo_path;
310 struct got_object_id *start_id;
311 struct got_repository *repo;
312 int log_complete;
313 sig_atomic_t *quit;
314 struct commit_queue_entry **first_displayed_entry;
315 struct commit_queue_entry **selected_entry;
316 int *searching;
317 int *search_next_done;
318 regex_t *regex;
319 };
321 struct tog_log_view_state {
322 struct commit_queue commits;
323 struct commit_queue_entry *first_displayed_entry;
324 struct commit_queue_entry *last_displayed_entry;
325 struct commit_queue_entry *selected_entry;
326 int selected;
327 char *in_repo_path;
328 char *head_ref_name;
329 int log_branches;
330 struct got_repository *repo;
331 struct got_object_id *start_id;
332 sig_atomic_t quit;
333 pthread_t thread;
334 struct tog_log_thread_args thread_args;
335 struct commit_queue_entry *matched_entry;
336 struct commit_queue_entry *search_entry;
337 struct tog_colors colors;
338 };
340 #define TOG_COLOR_DIFF_MINUS 1
341 #define TOG_COLOR_DIFF_PLUS 2
342 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
343 #define TOG_COLOR_DIFF_META 4
344 #define TOG_COLOR_TREE_SUBMODULE 5
345 #define TOG_COLOR_TREE_SYMLINK 6
346 #define TOG_COLOR_TREE_DIRECTORY 7
347 #define TOG_COLOR_TREE_EXECUTABLE 8
348 #define TOG_COLOR_COMMIT 9
349 #define TOG_COLOR_AUTHOR 10
350 #define TOG_COLOR_DATE 11
351 #define TOG_COLOR_REFS_HEADS 12
352 #define TOG_COLOR_REFS_TAGS 13
353 #define TOG_COLOR_REFS_REMOTES 14
355 struct tog_blame_cb_args {
356 struct tog_blame_line *lines; /* one per line */
357 int nlines;
359 struct tog_view *view;
360 struct got_object_id *commit_id;
361 int *quit;
362 };
364 struct tog_blame_thread_args {
365 const char *path;
366 struct got_repository *repo;
367 struct tog_blame_cb_args *cb_args;
368 int *complete;
369 got_cancel_cb cancel_cb;
370 void *cancel_arg;
371 };
373 struct tog_blame {
374 FILE *f;
375 off_t filesize;
376 struct tog_blame_line *lines;
377 int nlines;
378 off_t *line_offsets;
379 pthread_t thread;
380 struct tog_blame_thread_args thread_args;
381 struct tog_blame_cb_args cb_args;
382 const char *path;
383 };
385 struct tog_blame_view_state {
386 int first_displayed_line;
387 int last_displayed_line;
388 int selected_line;
389 int blame_complete;
390 int eof;
391 int done;
392 struct got_object_id_queue blamed_commits;
393 struct got_object_qid *blamed_commit;
394 char *path;
395 struct got_repository *repo;
396 struct got_object_id *commit_id;
397 struct tog_blame blame;
398 int matched_line;
399 struct tog_colors colors;
400 };
402 struct tog_parent_tree {
403 TAILQ_ENTRY(tog_parent_tree) entry;
404 struct got_tree_object *tree;
405 struct got_tree_entry *first_displayed_entry;
406 struct got_tree_entry *selected_entry;
407 int selected;
408 };
410 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
412 struct tog_tree_view_state {
413 char *tree_label;
414 struct got_object_id *commit_id;/* commit which this tree belongs to */
415 struct got_tree_object *root; /* the commit's root tree entry */
416 struct got_tree_object *tree; /* currently displayed (sub-)tree */
417 struct got_tree_entry *first_displayed_entry;
418 struct got_tree_entry *last_displayed_entry;
419 struct got_tree_entry *selected_entry;
420 int ndisplayed, selected, show_ids;
421 struct tog_parent_trees parents; /* parent trees of current sub-tree */
422 char *head_ref_name;
423 struct got_repository *repo;
424 struct got_tree_entry *matched_entry;
425 struct tog_colors colors;
426 };
428 struct tog_reflist_entry {
429 TAILQ_ENTRY(tog_reflist_entry) entry;
430 struct got_reference *ref;
431 int idx;
432 };
434 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
436 struct tog_ref_view_state {
437 struct tog_reflist_head refs;
438 struct tog_reflist_entry *first_displayed_entry;
439 struct tog_reflist_entry *last_displayed_entry;
440 struct tog_reflist_entry *selected_entry;
441 int nrefs, ndisplayed, selected, show_ids;
442 struct got_repository *repo;
443 struct tog_reflist_entry *matched_entry;
444 struct tog_colors colors;
445 };
447 /*
448 * We implement two types of views: parent views and child views.
450 * The 'Tab' key switches focus between a parent view and its child view.
451 * Child views are shown side-by-side to their parent view, provided
452 * there is enough screen estate.
454 * When a new view is opened from within a parent view, this new view
455 * becomes a child view of the parent view, replacing any existing child.
457 * When a new view is opened from within a child view, this new view
458 * becomes a parent view which will obscure the views below until the
459 * user quits the new parent view by typing 'q'.
461 * This list of views contains parent views only.
462 * Child views are only pointed to by their parent view.
463 */
464 TAILQ_HEAD(tog_view_list_head, tog_view);
466 struct tog_view {
467 TAILQ_ENTRY(tog_view) entry;
468 WINDOW *window;
469 PANEL *panel;
470 int nlines, ncols, begin_y, begin_x;
471 int lines, cols; /* copies of LINES and COLS */
472 int focussed; /* Only set on one parent or child view at a time. */
473 int dying;
474 struct tog_view *parent;
475 struct tog_view *child;
477 /*
478 * This flag is initially set on parent views when a new child view
479 * is created. It gets toggled when the 'Tab' key switches focus
480 * between parent and child.
481 * The flag indicates whether focus should be passed on to our child
482 * view if this parent view gets picked for focus after another parent
483 * view was closed. This prevents child views from losing focus in such
484 * situations.
485 */
486 int focus_child;
488 /* type-specific state */
489 enum tog_view_type type;
490 union {
491 struct tog_diff_view_state diff;
492 struct tog_log_view_state log;
493 struct tog_blame_view_state blame;
494 struct tog_tree_view_state tree;
495 struct tog_ref_view_state ref;
496 } state;
498 const struct got_error *(*show)(struct tog_view *);
499 const struct got_error *(*input)(struct tog_view **,
500 struct tog_view *, int);
501 const struct got_error *(*close)(struct tog_view *);
503 const struct got_error *(*search_start)(struct tog_view *);
504 const struct got_error *(*search_next)(struct tog_view *);
505 int search_started;
506 int searching;
507 #define TOG_SEARCH_FORWARD 1
508 #define TOG_SEARCH_BACKWARD 2
509 int search_next_done;
510 #define TOG_SEARCH_HAVE_MORE 1
511 #define TOG_SEARCH_NO_MORE 2
512 #define TOG_SEARCH_HAVE_NONE 3
513 regex_t regex;
514 regmatch_t regmatch;
515 };
517 static const struct got_error *open_diff_view(struct tog_view *,
518 struct got_object_id *, struct got_object_id *,
519 const char *, const char *, int, int, int, struct tog_view *,
520 struct got_repository *);
521 static const struct got_error *show_diff_view(struct tog_view *);
522 static const struct got_error *input_diff_view(struct tog_view **,
523 struct tog_view *, int);
524 static const struct got_error* close_diff_view(struct tog_view *);
525 static const struct got_error *search_start_diff_view(struct tog_view *);
526 static const struct got_error *search_next_diff_view(struct tog_view *);
528 static const struct got_error *open_log_view(struct tog_view *,
529 struct got_object_id *, struct got_repository *,
530 const char *, const char *, int);
531 static const struct got_error * show_log_view(struct tog_view *);
532 static const struct got_error *input_log_view(struct tog_view **,
533 struct tog_view *, int);
534 static const struct got_error *close_log_view(struct tog_view *);
535 static const struct got_error *search_start_log_view(struct tog_view *);
536 static const struct got_error *search_next_log_view(struct tog_view *);
538 static const struct got_error *open_blame_view(struct tog_view *, char *,
539 struct got_object_id *, struct got_repository *);
540 static const struct got_error *show_blame_view(struct tog_view *);
541 static const struct got_error *input_blame_view(struct tog_view **,
542 struct tog_view *, int);
543 static const struct got_error *close_blame_view(struct tog_view *);
544 static const struct got_error *search_start_blame_view(struct tog_view *);
545 static const struct got_error *search_next_blame_view(struct tog_view *);
547 static const struct got_error *open_tree_view(struct tog_view *,
548 struct got_object_id *, const char *, struct got_repository *);
549 static const struct got_error *show_tree_view(struct tog_view *);
550 static const struct got_error *input_tree_view(struct tog_view **,
551 struct tog_view *, int);
552 static const struct got_error *close_tree_view(struct tog_view *);
553 static const struct got_error *search_start_tree_view(struct tog_view *);
554 static const struct got_error *search_next_tree_view(struct tog_view *);
556 static const struct got_error *open_ref_view(struct tog_view *,
557 struct got_repository *);
558 static const struct got_error *show_ref_view(struct tog_view *);
559 static const struct got_error *input_ref_view(struct tog_view **,
560 struct tog_view *, int);
561 static const struct got_error *close_ref_view(struct tog_view *);
562 static const struct got_error *search_start_ref_view(struct tog_view *);
563 static const struct got_error *search_next_ref_view(struct tog_view *);
565 static volatile sig_atomic_t tog_sigwinch_received;
566 static volatile sig_atomic_t tog_sigpipe_received;
567 static volatile sig_atomic_t tog_sigcont_received;
569 static void
570 tog_sigwinch(int signo)
572 tog_sigwinch_received = 1;
575 static void
576 tog_sigpipe(int signo)
578 tog_sigpipe_received = 1;
581 static void
582 tog_sigcont(int signo)
584 tog_sigcont_received = 1;
587 static const struct got_error *
588 view_close(struct tog_view *view)
590 const struct got_error *err = NULL;
592 if (view->child) {
593 view_close(view->child);
594 view->child = NULL;
596 if (view->close)
597 err = view->close(view);
598 if (view->panel)
599 del_panel(view->panel);
600 if (view->window)
601 delwin(view->window);
602 free(view);
603 return err;
606 static struct tog_view *
607 view_open(int nlines, int ncols, int begin_y, int begin_x,
608 enum tog_view_type type)
610 struct tog_view *view = calloc(1, sizeof(*view));
612 if (view == NULL)
613 return NULL;
615 view->type = type;
616 view->lines = LINES;
617 view->cols = COLS;
618 view->nlines = nlines ? nlines : LINES - begin_y;
619 view->ncols = ncols ? ncols : COLS - begin_x;
620 view->begin_y = begin_y;
621 view->begin_x = begin_x;
622 view->window = newwin(nlines, ncols, begin_y, begin_x);
623 if (view->window == NULL) {
624 view_close(view);
625 return NULL;
627 view->panel = new_panel(view->window);
628 if (view->panel == NULL ||
629 set_panel_userptr(view->panel, view) != OK) {
630 view_close(view);
631 return NULL;
634 keypad(view->window, TRUE);
635 return view;
638 static int
639 view_split_begin_x(int begin_x)
641 if (begin_x > 0 || COLS < 120)
642 return 0;
643 return (COLS - MAX(COLS / 2, 80));
646 static const struct got_error *view_resize(struct tog_view *);
648 static const struct got_error *
649 view_splitscreen(struct tog_view *view)
651 const struct got_error *err = NULL;
653 view->begin_y = 0;
654 view->begin_x = view_split_begin_x(0);
655 view->nlines = LINES;
656 view->ncols = COLS - view->begin_x;
657 view->lines = LINES;
658 view->cols = COLS;
659 err = view_resize(view);
660 if (err)
661 return err;
663 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
664 return got_error_from_errno("mvwin");
666 return NULL;
669 static const struct got_error *
670 view_fullscreen(struct tog_view *view)
672 const struct got_error *err = NULL;
674 view->begin_x = 0;
675 view->begin_y = 0;
676 view->nlines = LINES;
677 view->ncols = COLS;
678 view->lines = LINES;
679 view->cols = COLS;
680 err = view_resize(view);
681 if (err)
682 return err;
684 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
685 return got_error_from_errno("mvwin");
687 return NULL;
690 static int
691 view_is_parent_view(struct tog_view *view)
693 return view->parent == NULL;
696 static const struct got_error *
697 view_resize(struct tog_view *view)
699 int nlines, ncols;
701 if (view->lines > LINES)
702 nlines = view->nlines - (view->lines - LINES);
703 else
704 nlines = view->nlines + (LINES - view->lines);
706 if (view->cols > COLS)
707 ncols = view->ncols - (view->cols - COLS);
708 else
709 ncols = view->ncols + (COLS - view->cols);
711 if (wresize(view->window, nlines, ncols) == ERR)
712 return got_error_from_errno("wresize");
713 if (replace_panel(view->panel, view->window) == ERR)
714 return got_error_from_errno("replace_panel");
715 wclear(view->window);
717 view->nlines = nlines;
718 view->ncols = ncols;
719 view->lines = LINES;
720 view->cols = COLS;
722 if (view->child) {
723 view->child->begin_x = view_split_begin_x(view->begin_x);
724 if (view->child->begin_x == 0) {
725 view_fullscreen(view->child);
726 if (view->child->focussed)
727 show_panel(view->child->panel);
728 else
729 show_panel(view->panel);
730 } else {
731 view_splitscreen(view->child);
732 show_panel(view->child->panel);
736 return NULL;
739 static const struct got_error *
740 view_close_child(struct tog_view *view)
742 const struct got_error *err = NULL;
744 if (view->child == NULL)
745 return NULL;
747 err = view_close(view->child);
748 view->child = NULL;
749 return err;
752 static void
753 view_set_child(struct tog_view *view, struct tog_view *child)
755 view->child = child;
756 child->parent = view;
759 static int
760 view_is_splitscreen(struct tog_view *view)
762 return view->begin_x > 0;
765 static void
766 tog_resizeterm(void)
768 int cols, lines;
769 struct winsize size;
771 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
772 cols = 80; /* Default */
773 lines = 24;
774 } else {
775 cols = size.ws_col;
776 lines = size.ws_row;
778 resize_term(lines, cols);
781 static const struct got_error *
782 view_search_start(struct tog_view *view)
784 const struct got_error *err = NULL;
785 char pattern[1024];
786 int ret;
788 if (view->search_started) {
789 regfree(&view->regex);
790 view->searching = 0;
791 memset(&view->regmatch, 0, sizeof(view->regmatch));
793 view->search_started = 0;
795 if (view->nlines < 1)
796 return NULL;
798 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
799 wclrtoeol(view->window);
801 nocbreak();
802 echo();
803 ret = wgetnstr(view->window, pattern, sizeof(pattern));
804 cbreak();
805 noecho();
806 if (ret == ERR)
807 return NULL;
809 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
810 err = view->search_start(view);
811 if (err) {
812 regfree(&view->regex);
813 return err;
815 view->search_started = 1;
816 view->searching = TOG_SEARCH_FORWARD;
817 view->search_next_done = 0;
818 view->search_next(view);
821 return NULL;
824 static const struct got_error *
825 view_input(struct tog_view **new, int *done, struct tog_view *view,
826 struct tog_view_list_head *views)
828 const struct got_error *err = NULL;
829 struct tog_view *v;
830 int ch, errcode;
832 *new = NULL;
834 /* Clear "no matches" indicator. */
835 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
836 view->search_next_done == TOG_SEARCH_HAVE_NONE)
837 view->search_next_done = TOG_SEARCH_HAVE_MORE;
839 if (view->searching && !view->search_next_done) {
840 errcode = pthread_mutex_unlock(&tog_mutex);
841 if (errcode)
842 return got_error_set_errno(errcode,
843 "pthread_mutex_unlock");
844 pthread_yield();
845 errcode = pthread_mutex_lock(&tog_mutex);
846 if (errcode)
847 return got_error_set_errno(errcode,
848 "pthread_mutex_lock");
849 view->search_next(view);
850 return NULL;
853 nodelay(stdscr, FALSE);
854 /* Allow threads to make progress while we are waiting for input. */
855 errcode = pthread_mutex_unlock(&tog_mutex);
856 if (errcode)
857 return got_error_set_errno(errcode, "pthread_mutex_unlock");
858 ch = wgetch(view->window);
859 errcode = pthread_mutex_lock(&tog_mutex);
860 if (errcode)
861 return got_error_set_errno(errcode, "pthread_mutex_lock");
862 nodelay(stdscr, TRUE);
864 if (tog_sigwinch_received || tog_sigcont_received) {
865 tog_resizeterm();
866 tog_sigwinch_received = 0;
867 tog_sigcont_received = 0;
868 TAILQ_FOREACH(v, views, entry) {
869 err = view_resize(v);
870 if (err)
871 return err;
872 err = v->input(new, v, KEY_RESIZE);
873 if (err)
874 return err;
875 if (v->child) {
876 err = view_resize(v->child);
877 if (err)
878 return err;
879 err = v->child->input(new, v->child,
880 KEY_RESIZE);
881 if (err)
882 return err;
887 switch (ch) {
888 case ERR:
889 break;
890 case '\t':
891 if (view->child) {
892 view->focussed = 0;
893 view->child->focussed = 1;
894 view->focus_child = 1;
895 } else if (view->parent) {
896 view->focussed = 0;
897 view->parent->focussed = 1;
898 view->parent->focus_child = 0;
900 break;
901 case 'q':
902 err = view->input(new, view, ch);
903 view->dying = 1;
904 break;
905 case 'Q':
906 *done = 1;
907 break;
908 case 'f':
909 if (view_is_parent_view(view)) {
910 if (view->child == NULL)
911 break;
912 if (view_is_splitscreen(view->child)) {
913 view->focussed = 0;
914 view->child->focussed = 1;
915 err = view_fullscreen(view->child);
916 } else
917 err = view_splitscreen(view->child);
918 if (err)
919 break;
920 err = view->child->input(new, view->child,
921 KEY_RESIZE);
922 } else {
923 if (view_is_splitscreen(view)) {
924 view->parent->focussed = 0;
925 view->focussed = 1;
926 err = view_fullscreen(view);
927 } else {
928 err = view_splitscreen(view);
930 if (err)
931 break;
932 err = view->input(new, view, KEY_RESIZE);
934 break;
935 case KEY_RESIZE:
936 break;
937 case '/':
938 if (view->search_start)
939 view_search_start(view);
940 else
941 err = view->input(new, view, ch);
942 break;
943 case 'N':
944 case 'n':
945 if (view->search_started && view->search_next) {
946 view->searching = (ch == 'n' ?
947 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
948 view->search_next_done = 0;
949 view->search_next(view);
950 } else
951 err = view->input(new, view, ch);
952 break;
953 default:
954 err = view->input(new, view, ch);
955 break;
958 return err;
961 void
962 view_vborder(struct tog_view *view)
964 PANEL *panel;
965 const struct tog_view *view_above;
967 if (view->parent)
968 return view_vborder(view->parent);
970 panel = panel_above(view->panel);
971 if (panel == NULL)
972 return;
974 view_above = panel_userptr(panel);
975 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
976 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
979 int
980 view_needs_focus_indication(struct tog_view *view)
982 if (view_is_parent_view(view)) {
983 if (view->child == NULL || view->child->focussed)
984 return 0;
985 if (!view_is_splitscreen(view->child))
986 return 0;
987 } else if (!view_is_splitscreen(view))
988 return 0;
990 return view->focussed;
993 static const struct got_error *
994 view_loop(struct tog_view *view)
996 const struct got_error *err = NULL;
997 struct tog_view_list_head views;
998 struct tog_view *new_view;
999 int fast_refresh = 10;
1000 int done = 0, errcode;
1002 errcode = pthread_mutex_lock(&tog_mutex);
1003 if (errcode)
1004 return got_error_set_errno(errcode, "pthread_mutex_lock");
1006 TAILQ_INIT(&views);
1007 TAILQ_INSERT_HEAD(&views, view, entry);
1009 view->focussed = 1;
1010 err = view->show(view);
1011 if (err)
1012 return err;
1013 update_panels();
1014 doupdate();
1015 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1016 /* Refresh fast during initialization, then become slower. */
1017 if (fast_refresh && fast_refresh-- == 0)
1018 halfdelay(10); /* switch to once per second */
1020 err = view_input(&new_view, &done, view, &views);
1021 if (err)
1022 break;
1023 if (view->dying) {
1024 struct tog_view *v, *prev = NULL;
1026 if (view_is_parent_view(view))
1027 prev = TAILQ_PREV(view, tog_view_list_head,
1028 entry);
1029 else if (view->parent)
1030 prev = view->parent;
1032 if (view->parent) {
1033 view->parent->child = NULL;
1034 view->parent->focus_child = 0;
1035 } else
1036 TAILQ_REMOVE(&views, view, entry);
1038 err = view_close(view);
1039 if (err)
1040 goto done;
1042 view = NULL;
1043 TAILQ_FOREACH(v, &views, entry) {
1044 if (v->focussed)
1045 break;
1047 if (view == NULL && new_view == NULL) {
1048 /* No view has focus. Try to pick one. */
1049 if (prev)
1050 view = prev;
1051 else if (!TAILQ_EMPTY(&views)) {
1052 view = TAILQ_LAST(&views,
1053 tog_view_list_head);
1055 if (view) {
1056 if (view->focus_child) {
1057 view->child->focussed = 1;
1058 view = view->child;
1059 } else
1060 view->focussed = 1;
1064 if (new_view) {
1065 struct tog_view *v, *t;
1066 /* Only allow one parent view per type. */
1067 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1068 if (v->type != new_view->type)
1069 continue;
1070 TAILQ_REMOVE(&views, v, entry);
1071 err = view_close(v);
1072 if (err)
1073 goto done;
1074 break;
1076 TAILQ_INSERT_TAIL(&views, new_view, entry);
1077 view = new_view;
1079 if (view) {
1080 if (view_is_parent_view(view)) {
1081 if (view->child && view->child->focussed)
1082 view = view->child;
1083 } else {
1084 if (view->parent && view->parent->focussed)
1085 view = view->parent;
1087 show_panel(view->panel);
1088 if (view->child && view_is_splitscreen(view->child))
1089 show_panel(view->child->panel);
1090 if (view->parent && view_is_splitscreen(view)) {
1091 err = view->parent->show(view->parent);
1092 if (err)
1093 goto done;
1095 err = view->show(view);
1096 if (err)
1097 goto done;
1098 if (view->child) {
1099 err = view->child->show(view->child);
1100 if (err)
1101 goto done;
1103 update_panels();
1104 doupdate();
1107 done:
1108 while (!TAILQ_EMPTY(&views)) {
1109 view = TAILQ_FIRST(&views);
1110 TAILQ_REMOVE(&views, view, entry);
1111 view_close(view);
1114 errcode = pthread_mutex_unlock(&tog_mutex);
1115 if (errcode && err == NULL)
1116 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1118 return err;
1121 __dead static void
1122 usage_log(void)
1124 endwin();
1125 fprintf(stderr,
1126 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1127 getprogname());
1128 exit(1);
1131 /* Create newly allocated wide-character string equivalent to a byte string. */
1132 static const struct got_error *
1133 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1135 char *vis = NULL;
1136 const struct got_error *err = NULL;
1138 *ws = NULL;
1139 *wlen = mbstowcs(NULL, s, 0);
1140 if (*wlen == (size_t)-1) {
1141 int vislen;
1142 if (errno != EILSEQ)
1143 return got_error_from_errno("mbstowcs");
1145 /* byte string invalid in current encoding; try to "fix" it */
1146 err = got_mbsavis(&vis, &vislen, s);
1147 if (err)
1148 return err;
1149 *wlen = mbstowcs(NULL, vis, 0);
1150 if (*wlen == (size_t)-1) {
1151 err = got_error_from_errno("mbstowcs"); /* give up */
1152 goto done;
1156 *ws = calloc(*wlen + 1, sizeof(**ws));
1157 if (*ws == NULL) {
1158 err = got_error_from_errno("calloc");
1159 goto done;
1162 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1163 err = got_error_from_errno("mbstowcs");
1164 done:
1165 free(vis);
1166 if (err) {
1167 free(*ws);
1168 *ws = NULL;
1169 *wlen = 0;
1171 return err;
1174 /* Format a line for display, ensuring that it won't overflow a width limit. */
1175 static const struct got_error *
1176 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1177 int col_tab_align)
1179 const struct got_error *err = NULL;
1180 int cols = 0;
1181 wchar_t *wline = NULL;
1182 size_t wlen;
1183 int i;
1185 *wlinep = NULL;
1186 *widthp = 0;
1188 err = mbs2ws(&wline, &wlen, line);
1189 if (err)
1190 return err;
1192 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1193 wline[wlen - 1] = L'\0';
1194 wlen--;
1196 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1197 wline[wlen - 1] = L'\0';
1198 wlen--;
1201 i = 0;
1202 while (i < wlen) {
1203 int width = wcwidth(wline[i]);
1205 if (width == 0) {
1206 i++;
1207 continue;
1210 if (width == 1 || width == 2) {
1211 if (cols + width > wlimit)
1212 break;
1213 cols += width;
1214 i++;
1215 } else if (width == -1) {
1216 if (wline[i] == L'\t') {
1217 width = TABSIZE -
1218 ((cols + col_tab_align) % TABSIZE);
1219 } else {
1220 width = 1;
1221 wline[i] = L'.';
1223 if (cols + width > wlimit)
1224 break;
1225 cols += width;
1226 i++;
1227 } else {
1228 err = got_error_from_errno("wcwidth");
1229 goto done;
1232 wline[i] = L'\0';
1233 if (widthp)
1234 *widthp = cols;
1235 done:
1236 if (err)
1237 free(wline);
1238 else
1239 *wlinep = wline;
1240 return err;
1243 static const struct got_error*
1244 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1245 struct got_object_id *id, struct got_repository *repo)
1247 static const struct got_error *err = NULL;
1248 struct got_reflist_entry *re;
1249 char *s;
1250 const char *name;
1252 *refs_str = NULL;
1254 TAILQ_FOREACH(re, refs, entry) {
1255 struct got_tag_object *tag = NULL;
1256 struct got_object_id *ref_id;
1257 int cmp;
1259 name = got_ref_get_name(re->ref);
1260 if (strcmp(name, GOT_REF_HEAD) == 0)
1261 continue;
1262 if (strncmp(name, "refs/", 5) == 0)
1263 name += 5;
1264 if (strncmp(name, "got/", 4) == 0)
1265 continue;
1266 if (strncmp(name, "heads/", 6) == 0)
1267 name += 6;
1268 if (strncmp(name, "remotes/", 8) == 0) {
1269 name += 8;
1270 s = strstr(name, "/" GOT_REF_HEAD);
1271 if (s != NULL && s[strlen(s)] == '\0')
1272 continue;
1274 err = got_ref_resolve(&ref_id, repo, re->ref);
1275 if (err)
1276 break;
1277 if (strncmp(name, "tags/", 5) == 0) {
1278 err = got_object_open_as_tag(&tag, repo, ref_id);
1279 if (err) {
1280 if (err->code != GOT_ERR_OBJ_TYPE) {
1281 free(ref_id);
1282 break;
1284 /* Ref points at something other than a tag. */
1285 err = NULL;
1286 tag = NULL;
1289 cmp = got_object_id_cmp(tag ?
1290 got_object_tag_get_object_id(tag) : ref_id, id);
1291 free(ref_id);
1292 if (tag)
1293 got_object_tag_close(tag);
1294 if (cmp != 0)
1295 continue;
1296 s = *refs_str;
1297 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1298 s ? ", " : "", name) == -1) {
1299 err = got_error_from_errno("asprintf");
1300 free(s);
1301 *refs_str = NULL;
1302 break;
1304 free(s);
1307 return err;
1310 static const struct got_error *
1311 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1312 int col_tab_align)
1314 char *smallerthan;
1316 smallerthan = strchr(author, '<');
1317 if (smallerthan && smallerthan[1] != '\0')
1318 author = smallerthan + 1;
1319 author[strcspn(author, "@>")] = '\0';
1320 return format_line(wauthor, author_width, author, limit, col_tab_align);
1323 static const struct got_error *
1324 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1325 struct got_object_id *id, const size_t date_display_cols,
1326 int author_display_cols)
1328 struct tog_log_view_state *s = &view->state.log;
1329 const struct got_error *err = NULL;
1330 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1331 char *logmsg0 = NULL, *logmsg = NULL;
1332 char *author = NULL;
1333 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1334 int author_width, logmsg_width;
1335 char *newline, *line = NULL;
1336 int col, limit;
1337 const int avail = view->ncols;
1338 struct tm tm;
1339 time_t committer_time;
1340 struct tog_color *tc;
1342 committer_time = got_object_commit_get_committer_time(commit);
1343 if (gmtime_r(&committer_time, &tm) == NULL)
1344 return got_error_from_errno("gmtime_r");
1345 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1346 return got_error(GOT_ERR_NO_SPACE);
1348 if (avail <= date_display_cols)
1349 limit = MIN(sizeof(datebuf) - 1, avail);
1350 else
1351 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1352 tc = get_color(&s->colors, TOG_COLOR_DATE);
1353 if (tc)
1354 wattr_on(view->window,
1355 COLOR_PAIR(tc->colorpair), NULL);
1356 waddnstr(view->window, datebuf, limit);
1357 if (tc)
1358 wattr_off(view->window,
1359 COLOR_PAIR(tc->colorpair), NULL);
1360 col = limit;
1361 if (col > avail)
1362 goto done;
1364 if (avail >= 120) {
1365 char *id_str;
1366 err = got_object_id_str(&id_str, id);
1367 if (err)
1368 goto done;
1369 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1370 if (tc)
1371 wattr_on(view->window,
1372 COLOR_PAIR(tc->colorpair), NULL);
1373 wprintw(view->window, "%.8s ", id_str);
1374 if (tc)
1375 wattr_off(view->window,
1376 COLOR_PAIR(tc->colorpair), NULL);
1377 free(id_str);
1378 col += 9;
1379 if (col > avail)
1380 goto done;
1383 author = strdup(got_object_commit_get_author(commit));
1384 if (author == NULL) {
1385 err = got_error_from_errno("strdup");
1386 goto done;
1388 err = format_author(&wauthor, &author_width, author, avail - col, col);
1389 if (err)
1390 goto done;
1391 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1392 if (tc)
1393 wattr_on(view->window,
1394 COLOR_PAIR(tc->colorpair), NULL);
1395 waddwstr(view->window, wauthor);
1396 if (tc)
1397 wattr_off(view->window,
1398 COLOR_PAIR(tc->colorpair), NULL);
1399 col += author_width;
1400 while (col < avail && author_width < author_display_cols + 2) {
1401 waddch(view->window, ' ');
1402 col++;
1403 author_width++;
1405 if (col > avail)
1406 goto done;
1408 err = got_object_commit_get_logmsg(&logmsg0, commit);
1409 if (err)
1410 goto done;
1411 logmsg = logmsg0;
1412 while (*logmsg == '\n')
1413 logmsg++;
1414 newline = strchr(logmsg, '\n');
1415 if (newline)
1416 *newline = '\0';
1417 limit = avail - col;
1418 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1419 if (err)
1420 goto done;
1421 waddwstr(view->window, wlogmsg);
1422 col += logmsg_width;
1423 while (col < avail) {
1424 waddch(view->window, ' ');
1425 col++;
1427 done:
1428 free(logmsg0);
1429 free(wlogmsg);
1430 free(author);
1431 free(wauthor);
1432 free(line);
1433 return err;
1436 static struct commit_queue_entry *
1437 alloc_commit_queue_entry(struct got_commit_object *commit,
1438 struct got_object_id *id)
1440 struct commit_queue_entry *entry;
1442 entry = calloc(1, sizeof(*entry));
1443 if (entry == NULL)
1444 return NULL;
1446 entry->id = id;
1447 entry->commit = commit;
1448 return entry;
1451 static void
1452 pop_commit(struct commit_queue *commits)
1454 struct commit_queue_entry *entry;
1456 entry = TAILQ_FIRST(&commits->head);
1457 TAILQ_REMOVE(&commits->head, entry, entry);
1458 got_object_commit_close(entry->commit);
1459 commits->ncommits--;
1460 /* Don't free entry->id! It is owned by the commit graph. */
1461 free(entry);
1464 static void
1465 free_commits(struct commit_queue *commits)
1467 while (!TAILQ_EMPTY(&commits->head))
1468 pop_commit(commits);
1471 static const struct got_error *
1472 match_commit(int *have_match, struct got_object_id *id,
1473 struct got_commit_object *commit, regex_t *regex)
1475 const struct got_error *err = NULL;
1476 regmatch_t regmatch;
1477 char *id_str = NULL, *logmsg = NULL;
1479 *have_match = 0;
1481 err = got_object_id_str(&id_str, id);
1482 if (err)
1483 return err;
1485 err = got_object_commit_get_logmsg(&logmsg, commit);
1486 if (err)
1487 goto done;
1489 if (regexec(regex, got_object_commit_get_author(commit), 1,
1490 &regmatch, 0) == 0 ||
1491 regexec(regex, got_object_commit_get_committer(commit), 1,
1492 &regmatch, 0) == 0 ||
1493 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1494 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1495 *have_match = 1;
1496 done:
1497 free(id_str);
1498 free(logmsg);
1499 return err;
1502 static const struct got_error *
1503 queue_commits(struct tog_log_thread_args *a)
1505 const struct got_error *err = NULL;
1508 * We keep all commits open throughout the lifetime of the log
1509 * view in order to avoid having to re-fetch commits from disk
1510 * while updating the display.
1512 do {
1513 struct got_object_id *id;
1514 struct got_commit_object *commit;
1515 struct commit_queue_entry *entry;
1516 int errcode;
1518 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1519 NULL, NULL);
1520 if (err || id == NULL)
1521 break;
1523 err = got_object_open_as_commit(&commit, a->repo, id);
1524 if (err)
1525 break;
1526 entry = alloc_commit_queue_entry(commit, id);
1527 if (entry == NULL) {
1528 err = got_error_from_errno("alloc_commit_queue_entry");
1529 break;
1532 errcode = pthread_mutex_lock(&tog_mutex);
1533 if (errcode) {
1534 err = got_error_set_errno(errcode,
1535 "pthread_mutex_lock");
1536 break;
1539 entry->idx = a->commits->ncommits;
1540 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1541 a->commits->ncommits++;
1543 if (*a->searching == TOG_SEARCH_FORWARD &&
1544 !*a->search_next_done) {
1545 int have_match;
1546 err = match_commit(&have_match, id, commit, a->regex);
1547 if (err)
1548 break;
1549 if (have_match)
1550 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1553 errcode = pthread_mutex_unlock(&tog_mutex);
1554 if (errcode && err == NULL)
1555 err = got_error_set_errno(errcode,
1556 "pthread_mutex_unlock");
1557 if (err)
1558 break;
1559 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1561 return err;
1564 static void
1565 select_commit(struct tog_log_view_state *s)
1567 struct commit_queue_entry *entry;
1568 int ncommits = 0;
1570 entry = s->first_displayed_entry;
1571 while (entry) {
1572 if (ncommits == s->selected) {
1573 s->selected_entry = entry;
1574 break;
1576 entry = TAILQ_NEXT(entry, entry);
1577 ncommits++;
1581 static const struct got_error *
1582 draw_commits(struct tog_view *view)
1584 const struct got_error *err = NULL;
1585 struct tog_log_view_state *s = &view->state.log;
1586 struct commit_queue_entry *entry = s->selected_entry;
1587 const int limit = view->nlines;
1588 int width;
1589 int ncommits, author_cols = 4;
1590 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1591 char *refs_str = NULL;
1592 wchar_t *wline;
1593 struct tog_color *tc;
1594 static const size_t date_display_cols = 12;
1596 if (s->selected_entry &&
1597 !(view->searching && view->search_next_done == 0)) {
1598 struct got_reflist_head *refs;
1599 err = got_object_id_str(&id_str, s->selected_entry->id);
1600 if (err)
1601 return err;
1602 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1603 s->selected_entry->id);
1604 if (refs) {
1605 err = build_refs_str(&refs_str, refs,
1606 s->selected_entry->id, s->repo);
1607 if (err)
1608 goto done;
1612 if (s->thread_args.commits_needed == 0)
1613 halfdelay(10); /* disable fast refresh */
1615 if (s->thread_args.commits_needed > 0) {
1616 if (asprintf(&ncommits_str, " [%d/%d] %s",
1617 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1618 (view->searching && !view->search_next_done) ?
1619 "searching..." : "loading...") == -1) {
1620 err = got_error_from_errno("asprintf");
1621 goto done;
1623 } else {
1624 const char *search_str = NULL;
1626 if (view->searching) {
1627 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1628 search_str = "no more matches";
1629 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1630 search_str = "no matches found";
1631 else if (!view->search_next_done)
1632 search_str = "searching...";
1635 if (asprintf(&ncommits_str, " [%d/%d] %s",
1636 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1637 search_str ? search_str :
1638 (refs_str ? refs_str : "")) == -1) {
1639 err = got_error_from_errno("asprintf");
1640 goto done;
1644 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1645 if (asprintf(&header, "commit %s %s%s",
1646 id_str ? id_str : "........................................",
1647 s->in_repo_path, ncommits_str) == -1) {
1648 err = got_error_from_errno("asprintf");
1649 header = NULL;
1650 goto done;
1652 } else if (asprintf(&header, "commit %s%s",
1653 id_str ? id_str : "........................................",
1654 ncommits_str) == -1) {
1655 err = got_error_from_errno("asprintf");
1656 header = NULL;
1657 goto done;
1659 err = format_line(&wline, &width, header, view->ncols, 0);
1660 if (err)
1661 goto done;
1663 werase(view->window);
1665 if (view_needs_focus_indication(view))
1666 wstandout(view->window);
1667 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1668 if (tc)
1669 wattr_on(view->window,
1670 COLOR_PAIR(tc->colorpair), NULL);
1671 waddwstr(view->window, wline);
1672 if (tc)
1673 wattr_off(view->window,
1674 COLOR_PAIR(tc->colorpair), NULL);
1675 while (width < view->ncols) {
1676 waddch(view->window, ' ');
1677 width++;
1679 if (view_needs_focus_indication(view))
1680 wstandend(view->window);
1681 free(wline);
1682 if (limit <= 1)
1683 goto done;
1685 /* Grow author column size if necessary. */
1686 entry = s->first_displayed_entry;
1687 ncommits = 0;
1688 while (entry) {
1689 char *author;
1690 wchar_t *wauthor;
1691 int width;
1692 if (ncommits >= limit - 1)
1693 break;
1694 author = strdup(got_object_commit_get_author(entry->commit));
1695 if (author == NULL) {
1696 err = got_error_from_errno("strdup");
1697 goto done;
1699 err = format_author(&wauthor, &width, author, COLS,
1700 date_display_cols);
1701 if (author_cols < width)
1702 author_cols = width;
1703 free(wauthor);
1704 free(author);
1705 ncommits++;
1706 entry = TAILQ_NEXT(entry, entry);
1709 entry = s->first_displayed_entry;
1710 s->last_displayed_entry = s->first_displayed_entry;
1711 ncommits = 0;
1712 while (entry) {
1713 if (ncommits >= limit - 1)
1714 break;
1715 if (ncommits == s->selected)
1716 wstandout(view->window);
1717 err = draw_commit(view, entry->commit, entry->id,
1718 date_display_cols, author_cols);
1719 if (ncommits == s->selected)
1720 wstandend(view->window);
1721 if (err)
1722 goto done;
1723 ncommits++;
1724 s->last_displayed_entry = entry;
1725 entry = TAILQ_NEXT(entry, entry);
1728 view_vborder(view);
1729 done:
1730 free(id_str);
1731 free(refs_str);
1732 free(ncommits_str);
1733 free(header);
1734 return err;
1737 static void
1738 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1740 struct commit_queue_entry *entry;
1741 int nscrolled = 0;
1743 entry = TAILQ_FIRST(&s->commits.head);
1744 if (s->first_displayed_entry == entry)
1745 return;
1747 entry = s->first_displayed_entry;
1748 while (entry && nscrolled < maxscroll) {
1749 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1750 if (entry) {
1751 s->first_displayed_entry = entry;
1752 nscrolled++;
1757 static const struct got_error *
1758 trigger_log_thread(struct tog_view *view, int wait)
1760 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1761 int errcode;
1763 halfdelay(1); /* fast refresh while loading commits */
1765 while (ta->commits_needed > 0) {
1766 if (ta->log_complete)
1767 break;
1769 /* Wake the log thread. */
1770 errcode = pthread_cond_signal(&ta->need_commits);
1771 if (errcode)
1772 return got_error_set_errno(errcode,
1773 "pthread_cond_signal");
1776 * The mutex will be released while the view loop waits
1777 * in wgetch(), at which time the log thread will run.
1779 if (!wait)
1780 break;
1782 /* Display progress update in log view. */
1783 show_log_view(view);
1784 update_panels();
1785 doupdate();
1787 /* Wait right here while next commit is being loaded. */
1788 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1789 if (errcode)
1790 return got_error_set_errno(errcode,
1791 "pthread_cond_wait");
1793 /* Display progress update in log view. */
1794 show_log_view(view);
1795 update_panels();
1796 doupdate();
1799 return NULL;
1802 static const struct got_error *
1803 log_scroll_down(struct tog_view *view, int maxscroll)
1805 struct tog_log_view_state *s = &view->state.log;
1806 const struct got_error *err = NULL;
1807 struct commit_queue_entry *pentry;
1808 int nscrolled = 0, ncommits_needed;
1810 if (s->last_displayed_entry == NULL)
1811 return NULL;
1813 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1814 if (s->commits.ncommits < ncommits_needed &&
1815 !s->thread_args.log_complete) {
1817 * Ask the log thread for required amount of commits.
1819 s->thread_args.commits_needed += maxscroll;
1820 err = trigger_log_thread(view, 1);
1821 if (err)
1822 return err;
1825 do {
1826 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1827 if (pentry == NULL)
1828 break;
1830 s->last_displayed_entry = pentry;
1832 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1833 if (pentry == NULL)
1834 break;
1835 s->first_displayed_entry = pentry;
1836 } while (++nscrolled < maxscroll);
1838 return err;
1841 static const struct got_error *
1842 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1843 struct got_commit_object *commit, struct got_object_id *commit_id,
1844 struct tog_view *log_view, struct got_repository *repo)
1846 const struct got_error *err;
1847 struct got_object_qid *parent_id;
1848 struct tog_view *diff_view;
1850 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1851 if (diff_view == NULL)
1852 return got_error_from_errno("view_open");
1854 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1855 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1856 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1857 if (err == NULL)
1858 *new_view = diff_view;
1859 return err;
1862 static const struct got_error *
1863 tree_view_visit_subtree(struct tog_tree_view_state *s,
1864 struct got_tree_object *subtree)
1866 struct tog_parent_tree *parent;
1868 parent = calloc(1, sizeof(*parent));
1869 if (parent == NULL)
1870 return got_error_from_errno("calloc");
1872 parent->tree = s->tree;
1873 parent->first_displayed_entry = s->first_displayed_entry;
1874 parent->selected_entry = s->selected_entry;
1875 parent->selected = s->selected;
1876 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1877 s->tree = subtree;
1878 s->selected = 0;
1879 s->first_displayed_entry = NULL;
1880 return NULL;
1883 static const struct got_error *
1884 tree_view_walk_path(struct tog_tree_view_state *s,
1885 struct got_object_id *commit_id, const char *path)
1887 const struct got_error *err = NULL;
1888 struct got_tree_object *tree = NULL;
1889 const char *p;
1890 char *slash, *subpath = NULL;
1892 /* Walk the path and open corresponding tree objects. */
1893 p = path;
1894 while (*p) {
1895 struct got_tree_entry *te;
1896 struct got_object_id *tree_id;
1897 char *te_name;
1899 while (p[0] == '/')
1900 p++;
1902 /* Ensure the correct subtree entry is selected. */
1903 slash = strchr(p, '/');
1904 if (slash == NULL)
1905 te_name = strdup(p);
1906 else
1907 te_name = strndup(p, slash - p);
1908 if (te_name == NULL) {
1909 err = got_error_from_errno("strndup");
1910 break;
1912 te = got_object_tree_find_entry(s->tree, te_name);
1913 if (te == NULL) {
1914 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1915 free(te_name);
1916 break;
1918 free(te_name);
1919 s->first_displayed_entry = s->selected_entry = te;
1921 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1922 break; /* jump to this file's entry */
1924 slash = strchr(p, '/');
1925 if (slash)
1926 subpath = strndup(path, slash - path);
1927 else
1928 subpath = strdup(path);
1929 if (subpath == NULL) {
1930 err = got_error_from_errno("strdup");
1931 break;
1934 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1935 subpath);
1936 if (err)
1937 break;
1939 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1940 free(tree_id);
1941 if (err)
1942 break;
1944 err = tree_view_visit_subtree(s, tree);
1945 if (err) {
1946 got_object_tree_close(tree);
1947 break;
1949 if (slash == NULL)
1950 break;
1951 free(subpath);
1952 subpath = NULL;
1953 p = slash;
1956 free(subpath);
1957 return err;
1960 static const struct got_error *
1961 browse_commit_tree(struct tog_view **new_view, int begin_x,
1962 struct commit_queue_entry *entry, const char *path,
1963 const char *head_ref_name, struct got_repository *repo)
1965 const struct got_error *err = NULL;
1966 struct tog_tree_view_state *s;
1967 struct tog_view *tree_view;
1969 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1970 if (tree_view == NULL)
1971 return got_error_from_errno("view_open");
1973 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1974 if (err)
1975 return err;
1976 s = &tree_view->state.tree;
1978 *new_view = tree_view;
1980 if (got_path_is_root_dir(path))
1981 return NULL;
1983 return tree_view_walk_path(s, entry->id, path);
1986 static const struct got_error *
1987 block_signals_used_by_main_thread(void)
1989 sigset_t sigset;
1990 int errcode;
1992 if (sigemptyset(&sigset) == -1)
1993 return got_error_from_errno("sigemptyset");
1995 /* tog handles SIGWINCH and SIGCONT */
1996 if (sigaddset(&sigset, SIGWINCH) == -1)
1997 return got_error_from_errno("sigaddset");
1998 if (sigaddset(&sigset, SIGCONT) == -1)
1999 return got_error_from_errno("sigaddset");
2001 /* ncurses handles SIGTSTP */
2002 if (sigaddset(&sigset, SIGTSTP) == -1)
2003 return got_error_from_errno("sigaddset");
2005 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2006 if (errcode)
2007 return got_error_set_errno(errcode, "pthread_sigmask");
2009 return NULL;
2012 static void *
2013 log_thread(void *arg)
2015 const struct got_error *err = NULL;
2016 int errcode = 0;
2017 struct tog_log_thread_args *a = arg;
2018 int done = 0;
2020 err = block_signals_used_by_main_thread();
2021 if (err)
2022 return (void *)err;
2024 while (!done && !err && !tog_sigpipe_received) {
2025 err = queue_commits(a);
2026 if (err) {
2027 if (err->code != GOT_ERR_ITER_COMPLETED)
2028 return (void *)err;
2029 err = NULL;
2030 done = 1;
2031 } else if (a->commits_needed > 0)
2032 a->commits_needed--;
2034 errcode = pthread_mutex_lock(&tog_mutex);
2035 if (errcode) {
2036 err = got_error_set_errno(errcode,
2037 "pthread_mutex_lock");
2038 break;
2039 } else if (*a->quit)
2040 done = 1;
2041 else if (*a->first_displayed_entry == NULL) {
2042 *a->first_displayed_entry =
2043 TAILQ_FIRST(&a->commits->head);
2044 *a->selected_entry = *a->first_displayed_entry;
2047 errcode = pthread_cond_signal(&a->commit_loaded);
2048 if (errcode) {
2049 err = got_error_set_errno(errcode,
2050 "pthread_cond_signal");
2051 pthread_mutex_unlock(&tog_mutex);
2052 break;
2055 if (done)
2056 a->commits_needed = 0;
2057 else {
2058 if (a->commits_needed == 0) {
2059 errcode = pthread_cond_wait(&a->need_commits,
2060 &tog_mutex);
2061 if (errcode)
2062 err = got_error_set_errno(errcode,
2063 "pthread_cond_wait");
2064 if (*a->quit)
2065 done = 1;
2069 errcode = pthread_mutex_unlock(&tog_mutex);
2070 if (errcode && err == NULL)
2071 err = got_error_set_errno(errcode,
2072 "pthread_mutex_unlock");
2074 a->log_complete = 1;
2075 return (void *)err;
2078 static const struct got_error *
2079 stop_log_thread(struct tog_log_view_state *s)
2081 const struct got_error *err = NULL;
2082 int errcode;
2084 if (s->thread) {
2085 s->quit = 1;
2086 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2087 if (errcode)
2088 return got_error_set_errno(errcode,
2089 "pthread_cond_signal");
2090 errcode = pthread_mutex_unlock(&tog_mutex);
2091 if (errcode)
2092 return got_error_set_errno(errcode,
2093 "pthread_mutex_unlock");
2094 errcode = pthread_join(s->thread, (void **)&err);
2095 if (errcode)
2096 return got_error_set_errno(errcode, "pthread_join");
2097 errcode = pthread_mutex_lock(&tog_mutex);
2098 if (errcode)
2099 return got_error_set_errno(errcode,
2100 "pthread_mutex_lock");
2101 s->thread = NULL;
2104 if (s->thread_args.repo) {
2105 err = got_repo_close(s->thread_args.repo);
2106 s->thread_args.repo = NULL;
2109 if (s->thread_args.graph) {
2110 got_commit_graph_close(s->thread_args.graph);
2111 s->thread_args.graph = NULL;
2114 return err;
2117 static const struct got_error *
2118 close_log_view(struct tog_view *view)
2120 const struct got_error *err = NULL;
2121 struct tog_log_view_state *s = &view->state.log;
2122 int errcode;
2124 err = stop_log_thread(s);
2126 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2127 if (errcode && err == NULL)
2128 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2130 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2131 if (errcode && err == NULL)
2132 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2134 free_commits(&s->commits);
2135 free(s->in_repo_path);
2136 s->in_repo_path = NULL;
2137 free(s->start_id);
2138 s->start_id = NULL;
2139 free(s->head_ref_name);
2140 s->head_ref_name = NULL;
2141 return err;
2144 static const struct got_error *
2145 search_start_log_view(struct tog_view *view)
2147 struct tog_log_view_state *s = &view->state.log;
2149 s->matched_entry = NULL;
2150 s->search_entry = NULL;
2151 return NULL;
2154 static const struct got_error *
2155 search_next_log_view(struct tog_view *view)
2157 const struct got_error *err = NULL;
2158 struct tog_log_view_state *s = &view->state.log;
2159 struct commit_queue_entry *entry;
2161 /* Display progress update in log view. */
2162 show_log_view(view);
2163 update_panels();
2164 doupdate();
2166 if (s->search_entry) {
2167 int errcode, ch;
2168 errcode = pthread_mutex_unlock(&tog_mutex);
2169 if (errcode)
2170 return got_error_set_errno(errcode,
2171 "pthread_mutex_unlock");
2172 ch = wgetch(view->window);
2173 errcode = pthread_mutex_lock(&tog_mutex);
2174 if (errcode)
2175 return got_error_set_errno(errcode,
2176 "pthread_mutex_lock");
2177 if (ch == KEY_BACKSPACE) {
2178 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2179 return NULL;
2181 if (view->searching == TOG_SEARCH_FORWARD)
2182 entry = TAILQ_NEXT(s->search_entry, entry);
2183 else
2184 entry = TAILQ_PREV(s->search_entry,
2185 commit_queue_head, entry);
2186 } else if (s->matched_entry) {
2187 if (view->searching == TOG_SEARCH_FORWARD)
2188 entry = TAILQ_NEXT(s->matched_entry, entry);
2189 else
2190 entry = TAILQ_PREV(s->matched_entry,
2191 commit_queue_head, entry);
2192 } else {
2193 if (view->searching == TOG_SEARCH_FORWARD)
2194 entry = TAILQ_FIRST(&s->commits.head);
2195 else
2196 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2199 while (1) {
2200 int have_match = 0;
2202 if (entry == NULL) {
2203 if (s->thread_args.log_complete ||
2204 view->searching == TOG_SEARCH_BACKWARD) {
2205 view->search_next_done =
2206 (s->matched_entry == NULL ?
2207 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2208 s->search_entry = NULL;
2209 return NULL;
2212 * Poke the log thread for more commits and return,
2213 * allowing the main loop to make progress. Search
2214 * will resume at s->search_entry once we come back.
2216 s->thread_args.commits_needed++;
2217 return trigger_log_thread(view, 0);
2220 err = match_commit(&have_match, entry->id, entry->commit,
2221 &view->regex);
2222 if (err)
2223 break;
2224 if (have_match) {
2225 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2226 s->matched_entry = entry;
2227 break;
2230 s->search_entry = entry;
2231 if (view->searching == TOG_SEARCH_FORWARD)
2232 entry = TAILQ_NEXT(entry, entry);
2233 else
2234 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2237 if (s->matched_entry) {
2238 int cur = s->selected_entry->idx;
2239 while (cur < s->matched_entry->idx) {
2240 err = input_log_view(NULL, view, KEY_DOWN);
2241 if (err)
2242 return err;
2243 cur++;
2245 while (cur > s->matched_entry->idx) {
2246 err = input_log_view(NULL, view, KEY_UP);
2247 if (err)
2248 return err;
2249 cur--;
2253 s->search_entry = NULL;
2255 return NULL;
2258 static const struct got_error *
2259 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2260 struct got_repository *repo, const char *head_ref_name,
2261 const char *in_repo_path, int log_branches)
2263 const struct got_error *err = NULL;
2264 struct tog_log_view_state *s = &view->state.log;
2265 struct got_repository *thread_repo = NULL;
2266 struct got_commit_graph *thread_graph = NULL;
2267 int errcode;
2269 if (in_repo_path != s->in_repo_path) {
2270 free(s->in_repo_path);
2271 s->in_repo_path = strdup(in_repo_path);
2272 if (s->in_repo_path == NULL)
2273 return got_error_from_errno("strdup");
2276 /* The commit queue only contains commits being displayed. */
2277 TAILQ_INIT(&s->commits.head);
2278 s->commits.ncommits = 0;
2280 s->repo = repo;
2281 if (head_ref_name) {
2282 s->head_ref_name = strdup(head_ref_name);
2283 if (s->head_ref_name == NULL) {
2284 err = got_error_from_errno("strdup");
2285 goto done;
2288 s->start_id = got_object_id_dup(start_id);
2289 if (s->start_id == NULL) {
2290 err = got_error_from_errno("got_object_id_dup");
2291 goto done;
2293 s->log_branches = log_branches;
2295 STAILQ_INIT(&s->colors);
2296 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2297 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2298 get_color_value("TOG_COLOR_COMMIT"));
2299 if (err)
2300 goto done;
2301 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2302 get_color_value("TOG_COLOR_AUTHOR"));
2303 if (err) {
2304 free_colors(&s->colors);
2305 goto done;
2307 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2308 get_color_value("TOG_COLOR_DATE"));
2309 if (err) {
2310 free_colors(&s->colors);
2311 goto done;
2315 view->show = show_log_view;
2316 view->input = input_log_view;
2317 view->close = close_log_view;
2318 view->search_start = search_start_log_view;
2319 view->search_next = search_next_log_view;
2321 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2322 if (err)
2323 goto done;
2324 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2325 !s->log_branches);
2326 if (err)
2327 goto done;
2328 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2329 s->repo, NULL, NULL);
2330 if (err)
2331 goto done;
2333 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2334 if (errcode) {
2335 err = got_error_set_errno(errcode, "pthread_cond_init");
2336 goto done;
2338 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2339 if (errcode) {
2340 err = got_error_set_errno(errcode, "pthread_cond_init");
2341 goto done;
2344 s->thread_args.commits_needed = view->nlines;
2345 s->thread_args.graph = thread_graph;
2346 s->thread_args.commits = &s->commits;
2347 s->thread_args.in_repo_path = s->in_repo_path;
2348 s->thread_args.start_id = s->start_id;
2349 s->thread_args.repo = thread_repo;
2350 s->thread_args.log_complete = 0;
2351 s->thread_args.quit = &s->quit;
2352 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2353 s->thread_args.selected_entry = &s->selected_entry;
2354 s->thread_args.searching = &view->searching;
2355 s->thread_args.search_next_done = &view->search_next_done;
2356 s->thread_args.regex = &view->regex;
2357 done:
2358 if (err)
2359 close_log_view(view);
2360 return err;
2363 static const struct got_error *
2364 show_log_view(struct tog_view *view)
2366 const struct got_error *err;
2367 struct tog_log_view_state *s = &view->state.log;
2369 if (s->thread == NULL) {
2370 int errcode = pthread_create(&s->thread, NULL, log_thread,
2371 &s->thread_args);
2372 if (errcode)
2373 return got_error_set_errno(errcode, "pthread_create");
2374 if (s->thread_args.commits_needed > 0) {
2375 err = trigger_log_thread(view, 1);
2376 if (err)
2377 return err;
2381 return draw_commits(view);
2384 static const struct got_error *
2385 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2387 const struct got_error *err = NULL;
2388 struct tog_log_view_state *s = &view->state.log;
2389 struct tog_view *diff_view = NULL, *tree_view = NULL;
2390 struct tog_view *ref_view = NULL;
2391 int begin_x = 0;
2393 switch (ch) {
2394 case 'q':
2395 s->quit = 1;
2396 break;
2397 case 'k':
2398 case KEY_UP:
2399 case '<':
2400 case ',':
2401 if (s->first_displayed_entry == NULL)
2402 break;
2403 if (s->selected > 0)
2404 s->selected--;
2405 else
2406 log_scroll_up(s, 1);
2407 select_commit(s);
2408 break;
2409 case 'g':
2410 case CTRL('u'):
2411 case KEY_HOME:
2412 if (s->first_displayed_entry == NULL)
2413 break;
2415 s->selected = 0;
2416 log_scroll_up(s, s->commits.ncommits);
2417 select_commit(s);
2418 break;
2419 case KEY_PPAGE:
2420 case CTRL('b'):
2421 if (s->first_displayed_entry == NULL)
2422 break;
2423 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2424 s->selected = 0;
2425 else
2426 log_scroll_up(s, view->nlines - 1);
2427 select_commit(s);
2428 break;
2429 case 'j':
2430 case KEY_DOWN:
2431 case '>':
2432 case '.':
2433 if (s->first_displayed_entry == NULL)
2434 break;
2435 if (s->selected < MIN(view->nlines - 2,
2436 s->commits.ncommits - 1))
2437 s->selected++;
2438 else {
2439 err = log_scroll_down(view, 1);
2440 if (err)
2441 break;
2443 select_commit(s);
2444 break;
2445 case 'G':
2446 case KEY_END: {
2447 /* We don't know yet how many commits, so we're forced to
2448 * traverse them all. */
2449 while (1) {
2450 if (s->thread_args.log_complete)
2451 break;
2453 s->thread_args.commits_needed++;
2454 err = trigger_log_thread(view, 1);
2455 if (err)
2456 return err;
2459 log_scroll_down(view, s->commits.ncommits);
2460 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2461 select_commit(s);
2462 break;
2464 case KEY_NPAGE:
2465 case CTRL('f'): {
2466 struct commit_queue_entry *first;
2467 first = s->first_displayed_entry;
2468 if (first == NULL)
2469 break;
2470 err = log_scroll_down(view, view->nlines - 1);
2471 if (err)
2472 break;
2473 if (first == s->first_displayed_entry &&
2474 s->selected < MIN(view->nlines - 2,
2475 s->commits.ncommits - 1)) {
2476 /* can't scroll further down */
2477 s->selected = MIN(view->nlines - 2,
2478 s->commits.ncommits - 1);
2480 select_commit(s);
2481 break;
2483 case KEY_RESIZE:
2484 if (s->selected > view->nlines - 2)
2485 s->selected = view->nlines - 2;
2486 if (s->selected > s->commits.ncommits - 1)
2487 s->selected = s->commits.ncommits - 1;
2488 select_commit(s);
2489 if (s->commits.ncommits < view->nlines - 1 &&
2490 !s->thread_args.log_complete) {
2491 s->thread_args.commits_needed += (view->nlines - 1) -
2492 s->commits.ncommits;
2493 err = trigger_log_thread(view, 1);
2495 break;
2496 case KEY_ENTER:
2497 case ' ':
2498 case '\r':
2499 if (s->selected_entry == NULL)
2500 break;
2501 if (view_is_parent_view(view))
2502 begin_x = view_split_begin_x(view->begin_x);
2503 err = open_diff_view_for_commit(&diff_view, begin_x,
2504 s->selected_entry->commit, s->selected_entry->id,
2505 view, s->repo);
2506 if (err)
2507 break;
2508 view->focussed = 0;
2509 diff_view->focussed = 1;
2510 if (view_is_parent_view(view)) {
2511 err = view_close_child(view);
2512 if (err)
2513 return err;
2514 view_set_child(view, diff_view);
2515 view->focus_child = 1;
2516 } else
2517 *new_view = diff_view;
2518 break;
2519 case 't':
2520 if (s->selected_entry == NULL)
2521 break;
2522 if (view_is_parent_view(view))
2523 begin_x = view_split_begin_x(view->begin_x);
2524 err = browse_commit_tree(&tree_view, begin_x,
2525 s->selected_entry, s->in_repo_path, s->head_ref_name,
2526 s->repo);
2527 if (err)
2528 break;
2529 view->focussed = 0;
2530 tree_view->focussed = 1;
2531 if (view_is_parent_view(view)) {
2532 err = view_close_child(view);
2533 if (err)
2534 return err;
2535 view_set_child(view, tree_view);
2536 view->focus_child = 1;
2537 } else
2538 *new_view = tree_view;
2539 break;
2540 case KEY_BACKSPACE:
2541 case CTRL('l'):
2542 case 'B':
2543 if (ch == KEY_BACKSPACE &&
2544 got_path_is_root_dir(s->in_repo_path))
2545 break;
2546 err = stop_log_thread(s);
2547 if (err)
2548 return err;
2549 if (ch == KEY_BACKSPACE) {
2550 char *parent_path;
2551 err = got_path_dirname(&parent_path, s->in_repo_path);
2552 if (err)
2553 return err;
2554 free(s->in_repo_path);
2555 s->in_repo_path = parent_path;
2556 s->thread_args.in_repo_path = s->in_repo_path;
2557 } else if (ch == CTRL('l')) {
2558 struct got_object_id *start_id;
2559 err = got_repo_match_object_id(&start_id, NULL,
2560 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2561 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2562 if (err)
2563 return err;
2564 free(s->start_id);
2565 s->start_id = start_id;
2566 s->thread_args.start_id = s->start_id;
2567 } else /* 'B' */
2568 s->log_branches = !s->log_branches;
2570 err = got_repo_open(&s->thread_args.repo,
2571 got_repo_get_path(s->repo), NULL);
2572 if (err)
2573 return err;
2574 tog_free_refs();
2575 err = tog_load_refs(s->repo);
2576 if (err)
2577 return err;
2578 err = got_commit_graph_open(&s->thread_args.graph,
2579 s->in_repo_path, !s->log_branches);
2580 if (err)
2581 return err;
2582 err = got_commit_graph_iter_start(s->thread_args.graph,
2583 s->start_id, s->repo, NULL, NULL);
2584 if (err)
2585 return err;
2586 free_commits(&s->commits);
2587 s->first_displayed_entry = NULL;
2588 s->last_displayed_entry = NULL;
2589 s->selected_entry = NULL;
2590 s->selected = 0;
2591 s->thread_args.log_complete = 0;
2592 s->quit = 0;
2593 s->thread_args.commits_needed = view->nlines;
2594 break;
2595 case 'r':
2596 if (view_is_parent_view(view))
2597 begin_x = view_split_begin_x(view->begin_x);
2598 ref_view = view_open(view->nlines, view->ncols,
2599 view->begin_y, begin_x, TOG_VIEW_REF);
2600 if (ref_view == NULL)
2601 return got_error_from_errno("view_open");
2602 err = open_ref_view(ref_view, s->repo);
2603 if (err) {
2604 view_close(ref_view);
2605 return err;
2607 view->focussed = 0;
2608 ref_view->focussed = 1;
2609 if (view_is_parent_view(view)) {
2610 err = view_close_child(view);
2611 if (err)
2612 return err;
2613 view_set_child(view, ref_view);
2614 view->focus_child = 1;
2615 } else
2616 *new_view = ref_view;
2617 break;
2618 default:
2619 break;
2622 return err;
2625 static const struct got_error *
2626 apply_unveil(const char *repo_path, const char *worktree_path)
2628 const struct got_error *error;
2630 #ifdef PROFILE
2631 if (unveil("gmon.out", "rwc") != 0)
2632 return got_error_from_errno2("unveil", "gmon.out");
2633 #endif
2634 if (repo_path && unveil(repo_path, "r") != 0)
2635 return got_error_from_errno2("unveil", repo_path);
2637 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2638 return got_error_from_errno2("unveil", worktree_path);
2640 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2641 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2643 error = got_privsep_unveil_exec_helpers();
2644 if (error != NULL)
2645 return error;
2647 if (unveil(NULL, NULL) != 0)
2648 return got_error_from_errno("unveil");
2650 return NULL;
2653 static void
2654 init_curses(void)
2656 initscr();
2657 cbreak();
2658 halfdelay(1); /* Do fast refresh while initial view is loading. */
2659 noecho();
2660 nonl();
2661 intrflush(stdscr, FALSE);
2662 keypad(stdscr, TRUE);
2663 curs_set(0);
2664 if (getenv("TOG_COLORS") != NULL) {
2665 start_color();
2666 use_default_colors();
2668 signal(SIGWINCH, tog_sigwinch);
2669 signal(SIGPIPE, tog_sigpipe);
2670 signal(SIGCONT, tog_sigcont);
2673 static const struct got_error *
2674 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2675 struct got_repository *repo, struct got_worktree *worktree)
2677 const struct got_error *err = NULL;
2679 if (argc == 0) {
2680 *in_repo_path = strdup("/");
2681 if (*in_repo_path == NULL)
2682 return got_error_from_errno("strdup");
2683 return NULL;
2686 if (worktree) {
2687 const char *prefix = got_worktree_get_path_prefix(worktree);
2688 char *p;
2690 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2691 if (err)
2692 return err;
2693 if (asprintf(in_repo_path, "%s%s%s", prefix,
2694 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2695 p) == -1) {
2696 err = got_error_from_errno("asprintf");
2697 *in_repo_path = NULL;
2699 free(p);
2700 } else
2701 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2703 return err;
2706 static const struct got_error *
2707 cmd_log(int argc, char *argv[])
2709 const struct got_error *error;
2710 struct got_repository *repo = NULL;
2711 struct got_worktree *worktree = NULL;
2712 struct got_object_id *start_id = NULL;
2713 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2714 char *start_commit = NULL, *label = NULL;
2715 struct got_reference *ref = NULL;
2716 const char *head_ref_name = NULL;
2717 int ch, log_branches = 0;
2718 struct tog_view *view;
2720 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2721 switch (ch) {
2722 case 'b':
2723 log_branches = 1;
2724 break;
2725 case 'c':
2726 start_commit = optarg;
2727 break;
2728 case 'r':
2729 repo_path = realpath(optarg, NULL);
2730 if (repo_path == NULL)
2731 return got_error_from_errno2("realpath",
2732 optarg);
2733 break;
2734 default:
2735 usage_log();
2736 /* NOTREACHED */
2740 argc -= optind;
2741 argv += optind;
2743 if (argc > 1)
2744 usage_log();
2746 if (repo_path == NULL) {
2747 cwd = getcwd(NULL, 0);
2748 if (cwd == NULL)
2749 return got_error_from_errno("getcwd");
2750 error = got_worktree_open(&worktree, cwd);
2751 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2752 goto done;
2753 if (worktree)
2754 repo_path =
2755 strdup(got_worktree_get_repo_path(worktree));
2756 else
2757 repo_path = strdup(cwd);
2758 if (repo_path == NULL) {
2759 error = got_error_from_errno("strdup");
2760 goto done;
2764 error = got_repo_open(&repo, repo_path, NULL);
2765 if (error != NULL)
2766 goto done;
2768 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2769 repo, worktree);
2770 if (error)
2771 goto done;
2773 init_curses();
2775 error = apply_unveil(got_repo_get_path(repo),
2776 worktree ? got_worktree_get_root_path(worktree) : NULL);
2777 if (error)
2778 goto done;
2780 /* already loaded by tog_log_with_path()? */
2781 if (TAILQ_EMPTY(&tog_refs)) {
2782 error = tog_load_refs(repo);
2783 if (error)
2784 goto done;
2787 if (start_commit == NULL) {
2788 error = got_repo_match_object_id(&start_id, &label,
2789 worktree ? got_worktree_get_head_ref_name(worktree) :
2790 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2791 if (error)
2792 goto done;
2793 head_ref_name = label;
2794 } else {
2795 error = got_ref_open(&ref, repo, start_commit, 0);
2796 if (error == NULL)
2797 head_ref_name = got_ref_get_name(ref);
2798 else if (error->code != GOT_ERR_NOT_REF)
2799 goto done;
2800 error = got_repo_match_object_id(&start_id, NULL,
2801 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2802 if (error)
2803 goto done;
2806 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2807 if (view == NULL) {
2808 error = got_error_from_errno("view_open");
2809 goto done;
2811 error = open_log_view(view, start_id, repo, head_ref_name,
2812 in_repo_path, log_branches);
2813 if (error)
2814 goto done;
2815 if (worktree) {
2816 /* Release work tree lock. */
2817 got_worktree_close(worktree);
2818 worktree = NULL;
2820 error = view_loop(view);
2821 done:
2822 free(in_repo_path);
2823 free(repo_path);
2824 free(cwd);
2825 free(start_id);
2826 free(label);
2827 if (ref)
2828 got_ref_close(ref);
2829 if (repo) {
2830 const struct got_error *close_err = got_repo_close(repo);
2831 if (error == NULL)
2832 error = close_err;
2834 if (worktree)
2835 got_worktree_close(worktree);
2836 tog_free_refs();
2837 return error;
2840 __dead static void
2841 usage_diff(void)
2843 endwin();
2844 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2845 "[-w] object1 object2\n", getprogname());
2846 exit(1);
2849 static int
2850 match_line(const char *line, regex_t *regex, size_t nmatch,
2851 regmatch_t *regmatch)
2853 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2856 struct tog_color *
2857 match_color(struct tog_colors *colors, const char *line)
2859 struct tog_color *tc = NULL;
2861 STAILQ_FOREACH(tc, colors, entry) {
2862 if (match_line(line, &tc->regex, 0, NULL))
2863 return tc;
2866 return NULL;
2869 static const struct got_error *
2870 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2871 WINDOW *window, regmatch_t *regmatch)
2873 const struct got_error *err = NULL;
2874 wchar_t *wline;
2875 int width;
2876 char *s;
2878 *wtotal = 0;
2880 s = strndup(line, regmatch->rm_so);
2881 if (s == NULL)
2882 return got_error_from_errno("strndup");
2884 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2885 if (err) {
2886 free(s);
2887 return err;
2889 waddwstr(window, wline);
2890 free(wline);
2891 free(s);
2892 wlimit -= width;
2893 *wtotal += width;
2895 if (wlimit > 0) {
2896 s = strndup(line + regmatch->rm_so,
2897 regmatch->rm_eo - regmatch->rm_so);
2898 if (s == NULL) {
2899 err = got_error_from_errno("strndup");
2900 free(s);
2901 return err;
2903 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2904 if (err) {
2905 free(s);
2906 return err;
2908 wattr_on(window, A_STANDOUT, NULL);
2909 waddwstr(window, wline);
2910 wattr_off(window, A_STANDOUT, NULL);
2911 free(wline);
2912 free(s);
2913 wlimit -= width;
2914 *wtotal += width;
2917 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2918 err = format_line(&wline, &width,
2919 line + regmatch->rm_eo, wlimit, col_tab_align);
2920 if (err)
2921 return err;
2922 waddwstr(window, wline);
2923 free(wline);
2924 *wtotal += width;
2927 return NULL;
2930 static const struct got_error *
2931 draw_file(struct tog_view *view, const char *header)
2933 struct tog_diff_view_state *s = &view->state.diff;
2934 regmatch_t *regmatch = &view->regmatch;
2935 const struct got_error *err;
2936 int nprinted = 0;
2937 char *line;
2938 size_t linesize = 0;
2939 ssize_t linelen;
2940 struct tog_color *tc;
2941 wchar_t *wline;
2942 int width;
2943 int max_lines = view->nlines;
2944 int nlines = s->nlines;
2945 off_t line_offset;
2947 line_offset = s->line_offsets[s->first_displayed_line - 1];
2948 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2949 return got_error_from_errno("fseek");
2951 werase(view->window);
2953 if (header) {
2954 if (asprintf(&line, "[%d/%d] %s",
2955 s->first_displayed_line - 1 + s->selected_line, nlines,
2956 header) == -1)
2957 return got_error_from_errno("asprintf");
2958 err = format_line(&wline, &width, line, view->ncols, 0);
2959 free(line);
2960 if (err)
2961 return err;
2963 if (view_needs_focus_indication(view))
2964 wstandout(view->window);
2965 waddwstr(view->window, wline);
2966 free(wline);
2967 wline = NULL;
2968 if (view_needs_focus_indication(view))
2969 wstandend(view->window);
2970 if (width <= view->ncols - 1)
2971 waddch(view->window, '\n');
2973 if (max_lines <= 1)
2974 return NULL;
2975 max_lines--;
2978 s->eof = 0;
2979 line = NULL;
2980 while (max_lines > 0 && nprinted < max_lines) {
2981 linelen = getline(&line, &linesize, s->f);
2982 if (linelen == -1) {
2983 if (feof(s->f)) {
2984 s->eof = 1;
2985 break;
2987 free(line);
2988 return got_ferror(s->f, GOT_ERR_IO);
2991 tc = match_color(&s->colors, line);
2992 if (tc)
2993 wattr_on(view->window,
2994 COLOR_PAIR(tc->colorpair), NULL);
2995 if (s->first_displayed_line + nprinted == s->matched_line &&
2996 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2997 err = add_matched_line(&width, line, view->ncols, 0,
2998 view->window, regmatch);
2999 if (err) {
3000 free(line);
3001 return err;
3003 } else {
3004 err = format_line(&wline, &width, line, view->ncols, 0);
3005 if (err) {
3006 free(line);
3007 return err;
3009 waddwstr(view->window, wline);
3010 free(wline);
3011 wline = NULL;
3013 if (tc)
3014 wattr_off(view->window,
3015 COLOR_PAIR(tc->colorpair), NULL);
3016 if (width <= view->ncols - 1)
3017 waddch(view->window, '\n');
3018 nprinted++;
3020 free(line);
3021 if (nprinted >= 1)
3022 s->last_displayed_line = s->first_displayed_line +
3023 (nprinted - 1);
3024 else
3025 s->last_displayed_line = s->first_displayed_line;
3027 view_vborder(view);
3029 if (s->eof) {
3030 while (nprinted < view->nlines) {
3031 waddch(view->window, '\n');
3032 nprinted++;
3035 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3036 if (err) {
3037 return err;
3040 wstandout(view->window);
3041 waddwstr(view->window, wline);
3042 free(wline);
3043 wline = NULL;
3044 wstandend(view->window);
3047 return NULL;
3050 static char *
3051 get_datestr(time_t *time, char *datebuf)
3053 struct tm mytm, *tm;
3054 char *p, *s;
3056 tm = gmtime_r(time, &mytm);
3057 if (tm == NULL)
3058 return NULL;
3059 s = asctime_r(tm, datebuf);
3060 if (s == NULL)
3061 return NULL;
3062 p = strchr(s, '\n');
3063 if (p)
3064 *p = '\0';
3065 return s;
3068 static const struct got_error *
3069 get_changed_paths(struct got_pathlist_head *paths,
3070 struct got_commit_object *commit, struct got_repository *repo)
3072 const struct got_error *err = NULL;
3073 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3074 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3075 struct got_object_qid *qid;
3077 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3078 if (qid != NULL) {
3079 struct got_commit_object *pcommit;
3080 err = got_object_open_as_commit(&pcommit, repo,
3081 qid->id);
3082 if (err)
3083 return err;
3085 tree_id1 = got_object_id_dup(
3086 got_object_commit_get_tree_id(pcommit));
3087 if (tree_id1 == NULL) {
3088 got_object_commit_close(pcommit);
3089 return got_error_from_errno("got_object_id_dup");
3091 got_object_commit_close(pcommit);
3095 if (tree_id1) {
3096 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3097 if (err)
3098 goto done;
3101 tree_id2 = got_object_commit_get_tree_id(commit);
3102 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3103 if (err)
3104 goto done;
3106 err = got_diff_tree(tree1, tree2, "", "", repo,
3107 got_diff_tree_collect_changed_paths, paths, 0);
3108 done:
3109 if (tree1)
3110 got_object_tree_close(tree1);
3111 if (tree2)
3112 got_object_tree_close(tree2);
3113 free(tree_id1);
3114 return err;
3117 static const struct got_error *
3118 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3120 off_t *p;
3122 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3123 if (p == NULL)
3124 return got_error_from_errno("reallocarray");
3125 *line_offsets = p;
3126 (*line_offsets)[*nlines] = off;
3127 (*nlines)++;
3128 return NULL;
3131 static const struct got_error *
3132 write_commit_info(off_t **line_offsets, size_t *nlines,
3133 struct got_object_id *commit_id, struct got_reflist_head *refs,
3134 struct got_repository *repo, FILE *outfile)
3136 const struct got_error *err = NULL;
3137 char datebuf[26], *datestr;
3138 struct got_commit_object *commit;
3139 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3140 time_t committer_time;
3141 const char *author, *committer;
3142 char *refs_str = NULL;
3143 struct got_pathlist_head changed_paths;
3144 struct got_pathlist_entry *pe;
3145 off_t outoff = 0;
3146 int n;
3148 TAILQ_INIT(&changed_paths);
3150 if (refs) {
3151 err = build_refs_str(&refs_str, refs, commit_id, repo);
3152 if (err)
3153 return err;
3156 err = got_object_open_as_commit(&commit, repo, commit_id);
3157 if (err)
3158 return err;
3160 err = got_object_id_str(&id_str, commit_id);
3161 if (err) {
3162 err = got_error_from_errno("got_object_id_str");
3163 goto done;
3166 err = add_line_offset(line_offsets, nlines, 0);
3167 if (err)
3168 goto done;
3170 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3171 refs_str ? refs_str : "", refs_str ? ")" : "");
3172 if (n < 0) {
3173 err = got_error_from_errno("fprintf");
3174 goto done;
3176 outoff += n;
3177 err = add_line_offset(line_offsets, nlines, outoff);
3178 if (err)
3179 goto done;
3181 n = fprintf(outfile, "from: %s\n",
3182 got_object_commit_get_author(commit));
3183 if (n < 0) {
3184 err = got_error_from_errno("fprintf");
3185 goto done;
3187 outoff += n;
3188 err = add_line_offset(line_offsets, nlines, outoff);
3189 if (err)
3190 goto done;
3192 committer_time = got_object_commit_get_committer_time(commit);
3193 datestr = get_datestr(&committer_time, datebuf);
3194 if (datestr) {
3195 n = fprintf(outfile, "date: %s UTC\n", datestr);
3196 if (n < 0) {
3197 err = got_error_from_errno("fprintf");
3198 goto done;
3200 outoff += n;
3201 err = add_line_offset(line_offsets, nlines, outoff);
3202 if (err)
3203 goto done;
3205 author = got_object_commit_get_author(commit);
3206 committer = got_object_commit_get_committer(commit);
3207 if (strcmp(author, committer) != 0) {
3208 n = fprintf(outfile, "via: %s\n", committer);
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 err = got_object_commit_get_logmsg(&logmsg, commit);
3219 if (err)
3220 goto done;
3221 s = logmsg;
3222 while ((line = strsep(&s, "\n")) != NULL) {
3223 n = fprintf(outfile, "%s\n", line);
3224 if (n < 0) {
3225 err = got_error_from_errno("fprintf");
3226 goto done;
3228 outoff += n;
3229 err = add_line_offset(line_offsets, nlines, outoff);
3230 if (err)
3231 goto done;
3234 err = get_changed_paths(&changed_paths, commit, repo);
3235 if (err)
3236 goto done;
3237 TAILQ_FOREACH(pe, &changed_paths, entry) {
3238 struct got_diff_changed_path *cp = pe->data;
3239 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3240 if (n < 0) {
3241 err = got_error_from_errno("fprintf");
3242 goto done;
3244 outoff += n;
3245 err = add_line_offset(line_offsets, nlines, outoff);
3246 if (err)
3247 goto done;
3248 free((char *)pe->path);
3249 free(pe->data);
3252 fputc('\n', outfile);
3253 outoff++;
3254 err = add_line_offset(line_offsets, nlines, outoff);
3255 done:
3256 got_pathlist_free(&changed_paths);
3257 free(id_str);
3258 free(logmsg);
3259 free(refs_str);
3260 got_object_commit_close(commit);
3261 if (err) {
3262 free(*line_offsets);
3263 *line_offsets = NULL;
3264 *nlines = 0;
3266 return err;
3269 static const struct got_error *
3270 create_diff(struct tog_diff_view_state *s)
3272 const struct got_error *err = NULL;
3273 FILE *f = NULL;
3274 int obj_type;
3276 free(s->line_offsets);
3277 s->line_offsets = malloc(sizeof(off_t));
3278 if (s->line_offsets == NULL)
3279 return got_error_from_errno("malloc");
3280 s->nlines = 0;
3282 f = got_opentemp();
3283 if (f == NULL) {
3284 err = got_error_from_errno("got_opentemp");
3285 goto done;
3287 if (s->f && fclose(s->f) == EOF) {
3288 err = got_error_from_errno("fclose");
3289 goto done;
3291 s->f = f;
3293 if (s->id1)
3294 err = got_object_get_type(&obj_type, s->repo, s->id1);
3295 else
3296 err = got_object_get_type(&obj_type, s->repo, s->id2);
3297 if (err)
3298 goto done;
3300 switch (obj_type) {
3301 case GOT_OBJ_TYPE_BLOB:
3302 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3303 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3304 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3305 break;
3306 case GOT_OBJ_TYPE_TREE:
3307 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3308 s->id1, s->id2, "", "", s->diff_context,
3309 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3310 break;
3311 case GOT_OBJ_TYPE_COMMIT: {
3312 const struct got_object_id_queue *parent_ids;
3313 struct got_object_qid *pid;
3314 struct got_commit_object *commit2;
3315 struct got_reflist_head *refs;
3317 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3318 if (err)
3319 goto done;
3320 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3321 /* Show commit info if we're diffing to a parent/root commit. */
3322 if (s->id1 == NULL) {
3323 err = write_commit_info(&s->line_offsets, &s->nlines,
3324 s->id2, refs, s->repo, s->f);
3325 if (err)
3326 goto done;
3327 } else {
3328 parent_ids = got_object_commit_get_parent_ids(commit2);
3329 STAILQ_FOREACH(pid, parent_ids, entry) {
3330 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3331 err = write_commit_info(
3332 &s->line_offsets, &s->nlines,
3333 s->id2, refs, s->repo, s->f);
3334 if (err)
3335 goto done;
3336 break;
3340 got_object_commit_close(commit2);
3342 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3343 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3344 s->force_text_diff, s->repo, s->f);
3345 break;
3347 default:
3348 err = got_error(GOT_ERR_OBJ_TYPE);
3349 break;
3351 if (err)
3352 goto done;
3353 done:
3354 if (s->f && fflush(s->f) != 0 && err == NULL)
3355 err = got_error_from_errno("fflush");
3356 return err;
3359 static void
3360 diff_view_indicate_progress(struct tog_view *view)
3362 mvwaddstr(view->window, 0, 0, "diffing...");
3363 update_panels();
3364 doupdate();
3367 static const struct got_error *
3368 search_start_diff_view(struct tog_view *view)
3370 struct tog_diff_view_state *s = &view->state.diff;
3372 s->matched_line = 0;
3373 return NULL;
3376 static const struct got_error *
3377 search_next_diff_view(struct tog_view *view)
3379 struct tog_diff_view_state *s = &view->state.diff;
3380 int lineno;
3381 char *line = NULL;
3382 size_t linesize = 0;
3383 ssize_t linelen;
3385 if (!view->searching) {
3386 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3387 return NULL;
3390 if (s->matched_line) {
3391 if (view->searching == TOG_SEARCH_FORWARD)
3392 lineno = s->matched_line + 1;
3393 else
3394 lineno = s->matched_line - 1;
3395 } else {
3396 if (view->searching == TOG_SEARCH_FORWARD)
3397 lineno = 1;
3398 else
3399 lineno = s->nlines;
3402 while (1) {
3403 off_t offset;
3405 if (lineno <= 0 || lineno > s->nlines) {
3406 if (s->matched_line == 0) {
3407 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3408 break;
3411 if (view->searching == TOG_SEARCH_FORWARD)
3412 lineno = 1;
3413 else
3414 lineno = s->nlines;
3417 offset = s->line_offsets[lineno - 1];
3418 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3419 free(line);
3420 return got_error_from_errno("fseeko");
3422 linelen = getline(&line, &linesize, s->f);
3423 if (linelen != -1 &&
3424 match_line(line, &view->regex, 1, &view->regmatch)) {
3425 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3426 s->matched_line = lineno;
3427 break;
3429 if (view->searching == TOG_SEARCH_FORWARD)
3430 lineno++;
3431 else
3432 lineno--;
3434 free(line);
3436 if (s->matched_line) {
3437 s->first_displayed_line = s->matched_line;
3438 s->selected_line = 1;
3441 return NULL;
3444 static const struct got_error *
3445 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3446 struct got_object_id *id2, const char *label1, const char *label2,
3447 int diff_context, int ignore_whitespace, int force_text_diff,
3448 struct tog_view *log_view, struct got_repository *repo)
3450 const struct got_error *err;
3451 struct tog_diff_view_state *s = &view->state.diff;
3453 if (id1 != NULL && id2 != NULL) {
3454 int type1, type2;
3455 err = got_object_get_type(&type1, repo, id1);
3456 if (err)
3457 return err;
3458 err = got_object_get_type(&type2, repo, id2);
3459 if (err)
3460 return err;
3462 if (type1 != type2)
3463 return got_error(GOT_ERR_OBJ_TYPE);
3465 s->first_displayed_line = 1;
3466 s->last_displayed_line = view->nlines;
3467 s->selected_line = 1;
3468 s->repo = repo;
3469 s->id1 = id1;
3470 s->id2 = id2;
3471 s->label1 = label1;
3472 s->label2 = label2;
3474 if (id1) {
3475 s->id1 = got_object_id_dup(id1);
3476 if (s->id1 == NULL)
3477 return got_error_from_errno("got_object_id_dup");
3478 } else
3479 s->id1 = NULL;
3481 s->id2 = got_object_id_dup(id2);
3482 if (s->id2 == NULL) {
3483 free(s->id1);
3484 s->id1 = NULL;
3485 return got_error_from_errno("got_object_id_dup");
3487 s->f = NULL;
3488 s->first_displayed_line = 1;
3489 s->last_displayed_line = view->nlines;
3490 s->diff_context = diff_context;
3491 s->ignore_whitespace = ignore_whitespace;
3492 s->force_text_diff = force_text_diff;
3493 s->log_view = log_view;
3494 s->repo = repo;
3496 STAILQ_INIT(&s->colors);
3497 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3498 err = add_color(&s->colors,
3499 "^-", TOG_COLOR_DIFF_MINUS,
3500 get_color_value("TOG_COLOR_DIFF_MINUS"));
3501 if (err)
3502 return err;
3503 err = add_color(&s->colors, "^\\+",
3504 TOG_COLOR_DIFF_PLUS,
3505 get_color_value("TOG_COLOR_DIFF_PLUS"));
3506 if (err) {
3507 free_colors(&s->colors);
3508 return err;
3510 err = add_color(&s->colors,
3511 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3512 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3513 if (err) {
3514 free_colors(&s->colors);
3515 return err;
3518 err = add_color(&s->colors,
3519 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3520 TOG_COLOR_DIFF_META,
3521 get_color_value("TOG_COLOR_DIFF_META"));
3522 if (err) {
3523 free_colors(&s->colors);
3524 return err;
3527 err = add_color(&s->colors,
3528 "^(from|via): ", TOG_COLOR_AUTHOR,
3529 get_color_value("TOG_COLOR_AUTHOR"));
3530 if (err) {
3531 free_colors(&s->colors);
3532 return err;
3535 err = add_color(&s->colors,
3536 "^date: ", TOG_COLOR_DATE,
3537 get_color_value("TOG_COLOR_DATE"));
3538 if (err) {
3539 free_colors(&s->colors);
3540 return err;
3544 if (log_view && view_is_splitscreen(view))
3545 show_log_view(log_view); /* draw vborder */
3546 diff_view_indicate_progress(view);
3548 s->line_offsets = NULL;
3549 s->nlines = 0;
3550 err = create_diff(s);
3551 if (err) {
3552 free(s->id1);
3553 s->id1 = NULL;
3554 free(s->id2);
3555 s->id2 = NULL;
3556 free_colors(&s->colors);
3557 return err;
3560 view->show = show_diff_view;
3561 view->input = input_diff_view;
3562 view->close = close_diff_view;
3563 view->search_start = search_start_diff_view;
3564 view->search_next = search_next_diff_view;
3566 return NULL;
3569 static const struct got_error *
3570 close_diff_view(struct tog_view *view)
3572 const struct got_error *err = NULL;
3573 struct tog_diff_view_state *s = &view->state.diff;
3575 free(s->id1);
3576 s->id1 = NULL;
3577 free(s->id2);
3578 s->id2 = NULL;
3579 if (s->f && fclose(s->f) == EOF)
3580 err = got_error_from_errno("fclose");
3581 free_colors(&s->colors);
3582 free(s->line_offsets);
3583 s->line_offsets = NULL;
3584 s->nlines = 0;
3585 return err;
3588 static const struct got_error *
3589 show_diff_view(struct tog_view *view)
3591 const struct got_error *err;
3592 struct tog_diff_view_state *s = &view->state.diff;
3593 char *id_str1 = NULL, *id_str2, *header;
3594 const char *label1, *label2;
3596 if (s->id1) {
3597 err = got_object_id_str(&id_str1, s->id1);
3598 if (err)
3599 return err;
3600 label1 = s->label1 ? : id_str1;
3601 } else
3602 label1 = "/dev/null";
3604 err = got_object_id_str(&id_str2, s->id2);
3605 if (err)
3606 return err;
3607 label2 = s->label2 ? : id_str2;
3609 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3610 err = got_error_from_errno("asprintf");
3611 free(id_str1);
3612 free(id_str2);
3613 return err;
3615 free(id_str1);
3616 free(id_str2);
3618 err = draw_file(view, header);
3619 free(header);
3620 return err;
3623 static const struct got_error *
3624 set_selected_commit(struct tog_diff_view_state *s,
3625 struct commit_queue_entry *entry)
3627 const struct got_error *err;
3628 const struct got_object_id_queue *parent_ids;
3629 struct got_commit_object *selected_commit;
3630 struct got_object_qid *pid;
3632 free(s->id2);
3633 s->id2 = got_object_id_dup(entry->id);
3634 if (s->id2 == NULL)
3635 return got_error_from_errno("got_object_id_dup");
3637 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3638 if (err)
3639 return err;
3640 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3641 free(s->id1);
3642 pid = STAILQ_FIRST(parent_ids);
3643 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3644 got_object_commit_close(selected_commit);
3645 return NULL;
3648 static const struct got_error *
3649 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3651 const struct got_error *err = NULL;
3652 struct tog_diff_view_state *s = &view->state.diff;
3653 struct tog_log_view_state *ls;
3654 struct commit_queue_entry *old_selected_entry;
3655 char *line = NULL;
3656 size_t linesize = 0;
3657 ssize_t linelen;
3658 int i;
3660 switch (ch) {
3661 case 'a':
3662 case 'w':
3663 if (ch == 'a')
3664 s->force_text_diff = !s->force_text_diff;
3665 if (ch == 'w')
3666 s->ignore_whitespace = !s->ignore_whitespace;
3667 wclear(view->window);
3668 s->first_displayed_line = 1;
3669 s->last_displayed_line = view->nlines;
3670 diff_view_indicate_progress(view);
3671 err = create_diff(s);
3672 break;
3673 case 'g':
3674 case CTRL('u'):
3675 case KEY_HOME:
3676 s->first_displayed_line = 1;
3677 break;
3678 case 'G':
3679 case KEY_END:
3680 if (s->eof)
3681 break;
3683 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3684 s->eof = 1;
3685 break;
3686 case 'k':
3687 case KEY_UP:
3688 if (s->first_displayed_line > 1)
3689 s->first_displayed_line--;
3690 break;
3691 case KEY_PPAGE:
3692 case CTRL('b'):
3693 if (s->first_displayed_line == 1)
3694 break;
3695 i = 0;
3696 while (i++ < view->nlines - 1 &&
3697 s->first_displayed_line > 1)
3698 s->first_displayed_line--;
3699 break;
3700 case 'j':
3701 case KEY_DOWN:
3702 if (!s->eof)
3703 s->first_displayed_line++;
3704 break;
3705 case KEY_NPAGE:
3706 case CTRL('f'):
3707 case ' ':
3708 if (s->eof)
3709 break;
3710 i = 0;
3711 while (!s->eof && i++ < view->nlines - 1) {
3712 linelen = getline(&line, &linesize, s->f);
3713 s->first_displayed_line++;
3714 if (linelen == -1) {
3715 if (feof(s->f)) {
3716 s->eof = 1;
3717 } else
3718 err = got_ferror(s->f, GOT_ERR_IO);
3719 break;
3722 free(line);
3723 break;
3724 case '[':
3725 if (s->diff_context > 0) {
3726 s->diff_context--;
3727 diff_view_indicate_progress(view);
3728 err = create_diff(s);
3729 if (s->first_displayed_line + view->nlines - 1 >
3730 s->nlines) {
3731 s->first_displayed_line = 1;
3732 s->last_displayed_line = view->nlines;
3735 break;
3736 case ']':
3737 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3738 s->diff_context++;
3739 diff_view_indicate_progress(view);
3740 err = create_diff(s);
3742 break;
3743 case '<':
3744 case ',':
3745 if (s->log_view == NULL)
3746 break;
3747 ls = &s->log_view->state.log;
3748 old_selected_entry = ls->selected_entry;
3750 err = input_log_view(NULL, s->log_view, KEY_UP);
3751 if (err)
3752 break;
3754 if (old_selected_entry == ls->selected_entry)
3755 break;
3757 err = set_selected_commit(s, ls->selected_entry);
3758 if (err)
3759 break;
3761 s->first_displayed_line = 1;
3762 s->last_displayed_line = view->nlines;
3764 diff_view_indicate_progress(view);
3765 err = create_diff(s);
3766 break;
3767 case '>':
3768 case '.':
3769 if (s->log_view == NULL)
3770 break;
3771 ls = &s->log_view->state.log;
3772 old_selected_entry = ls->selected_entry;
3774 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3775 if (err)
3776 break;
3778 if (old_selected_entry == ls->selected_entry)
3779 break;
3781 err = set_selected_commit(s, ls->selected_entry);
3782 if (err)
3783 break;
3785 s->first_displayed_line = 1;
3786 s->last_displayed_line = view->nlines;
3788 diff_view_indicate_progress(view);
3789 err = create_diff(s);
3790 break;
3791 default:
3792 break;
3795 return err;
3798 static const struct got_error *
3799 cmd_diff(int argc, char *argv[])
3801 const struct got_error *error = NULL;
3802 struct got_repository *repo = NULL;
3803 struct got_worktree *worktree = NULL;
3804 struct got_object_id *id1 = NULL, *id2 = NULL;
3805 char *repo_path = NULL, *cwd = NULL;
3806 char *id_str1 = NULL, *id_str2 = NULL;
3807 char *label1 = NULL, *label2 = NULL;
3808 int diff_context = 3, ignore_whitespace = 0;
3809 int ch, force_text_diff = 0;
3810 const char *errstr;
3811 struct tog_view *view;
3813 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3814 switch (ch) {
3815 case 'a':
3816 force_text_diff = 1;
3817 break;
3818 case 'C':
3819 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3820 &errstr);
3821 if (errstr != NULL)
3822 err(1, "-C option %s", errstr);
3823 break;
3824 case 'r':
3825 repo_path = realpath(optarg, NULL);
3826 if (repo_path == NULL)
3827 return got_error_from_errno2("realpath",
3828 optarg);
3829 got_path_strip_trailing_slashes(repo_path);
3830 break;
3831 case 'w':
3832 ignore_whitespace = 1;
3833 break;
3834 default:
3835 usage_diff();
3836 /* NOTREACHED */
3840 argc -= optind;
3841 argv += optind;
3843 if (argc == 0) {
3844 usage_diff(); /* TODO show local worktree changes */
3845 } else if (argc == 2) {
3846 id_str1 = argv[0];
3847 id_str2 = argv[1];
3848 } else
3849 usage_diff();
3851 if (repo_path == NULL) {
3852 cwd = getcwd(NULL, 0);
3853 if (cwd == NULL)
3854 return got_error_from_errno("getcwd");
3855 error = got_worktree_open(&worktree, cwd);
3856 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3857 goto done;
3858 if (worktree)
3859 repo_path =
3860 strdup(got_worktree_get_repo_path(worktree));
3861 else
3862 repo_path = strdup(cwd);
3863 if (repo_path == NULL) {
3864 error = got_error_from_errno("strdup");
3865 goto done;
3869 error = got_repo_open(&repo, repo_path, NULL);
3870 if (error)
3871 goto done;
3873 init_curses();
3875 error = apply_unveil(got_repo_get_path(repo), NULL);
3876 if (error)
3877 goto done;
3879 error = tog_load_refs(repo);
3880 if (error)
3881 goto done;
3883 error = got_repo_match_object_id(&id1, &label1, id_str1,
3884 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3885 if (error)
3886 goto done;
3888 error = got_repo_match_object_id(&id2, &label2, id_str2,
3889 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3890 if (error)
3891 goto done;
3893 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3894 if (view == NULL) {
3895 error = got_error_from_errno("view_open");
3896 goto done;
3898 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3899 ignore_whitespace, force_text_diff, NULL, repo);
3900 if (error)
3901 goto done;
3902 error = view_loop(view);
3903 done:
3904 free(label1);
3905 free(label2);
3906 free(repo_path);
3907 free(cwd);
3908 if (repo) {
3909 const struct got_error *close_err = got_repo_close(repo);
3910 if (error == NULL)
3911 error = close_err;
3913 if (worktree)
3914 got_worktree_close(worktree);
3915 tog_free_refs();
3916 return error;
3919 __dead static void
3920 usage_blame(void)
3922 endwin();
3923 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3924 getprogname());
3925 exit(1);
3928 struct tog_blame_line {
3929 int annotated;
3930 struct got_object_id *id;
3933 static const struct got_error *
3934 draw_blame(struct tog_view *view)
3936 struct tog_blame_view_state *s = &view->state.blame;
3937 struct tog_blame *blame = &s->blame;
3938 regmatch_t *regmatch = &view->regmatch;
3939 const struct got_error *err;
3940 int lineno = 0, nprinted = 0;
3941 char *line = NULL;
3942 size_t linesize = 0;
3943 ssize_t linelen;
3944 wchar_t *wline;
3945 int width;
3946 struct tog_blame_line *blame_line;
3947 struct got_object_id *prev_id = NULL;
3948 char *id_str;
3949 struct tog_color *tc;
3951 err = got_object_id_str(&id_str, s->blamed_commit->id);
3952 if (err)
3953 return err;
3955 rewind(blame->f);
3956 werase(view->window);
3958 if (asprintf(&line, "commit %s", id_str) == -1) {
3959 err = got_error_from_errno("asprintf");
3960 free(id_str);
3961 return err;
3964 err = format_line(&wline, &width, line, view->ncols, 0);
3965 free(line);
3966 line = NULL;
3967 if (err)
3968 return err;
3969 if (view_needs_focus_indication(view))
3970 wstandout(view->window);
3971 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3972 if (tc)
3973 wattr_on(view->window,
3974 COLOR_PAIR(tc->colorpair), NULL);
3975 waddwstr(view->window, wline);
3976 if (tc)
3977 wattr_off(view->window,
3978 COLOR_PAIR(tc->colorpair), NULL);
3979 if (view_needs_focus_indication(view))
3980 wstandend(view->window);
3981 free(wline);
3982 wline = NULL;
3983 if (width < view->ncols - 1)
3984 waddch(view->window, '\n');
3986 if (asprintf(&line, "[%d/%d] %s%s",
3987 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3988 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3989 free(id_str);
3990 return got_error_from_errno("asprintf");
3992 free(id_str);
3993 err = format_line(&wline, &width, line, view->ncols, 0);
3994 free(line);
3995 line = NULL;
3996 if (err)
3997 return err;
3998 waddwstr(view->window, wline);
3999 free(wline);
4000 wline = NULL;
4001 if (width < view->ncols - 1)
4002 waddch(view->window, '\n');
4004 s->eof = 0;
4005 while (nprinted < view->nlines - 2) {
4006 linelen = getline(&line, &linesize, blame->f);
4007 if (linelen == -1) {
4008 if (feof(blame->f)) {
4009 s->eof = 1;
4010 break;
4012 free(line);
4013 return got_ferror(blame->f, GOT_ERR_IO);
4015 if (++lineno < s->first_displayed_line)
4016 continue;
4018 if (view->focussed && nprinted == s->selected_line - 1)
4019 wstandout(view->window);
4021 if (blame->nlines > 0) {
4022 blame_line = &blame->lines[lineno - 1];
4023 if (blame_line->annotated && prev_id &&
4024 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4025 !(view->focussed &&
4026 nprinted == s->selected_line - 1)) {
4027 waddstr(view->window, " ");
4028 } else if (blame_line->annotated) {
4029 char *id_str;
4030 err = got_object_id_str(&id_str, blame_line->id);
4031 if (err) {
4032 free(line);
4033 return err;
4035 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4036 if (tc)
4037 wattr_on(view->window,
4038 COLOR_PAIR(tc->colorpair), NULL);
4039 wprintw(view->window, "%.8s", id_str);
4040 if (tc)
4041 wattr_off(view->window,
4042 COLOR_PAIR(tc->colorpair), NULL);
4043 free(id_str);
4044 prev_id = blame_line->id;
4045 } else {
4046 waddstr(view->window, "........");
4047 prev_id = NULL;
4049 } else {
4050 waddstr(view->window, "........");
4051 prev_id = NULL;
4054 if (view->focussed && nprinted == s->selected_line - 1)
4055 wstandend(view->window);
4056 waddstr(view->window, " ");
4058 if (view->ncols <= 9) {
4059 width = 9;
4060 wline = wcsdup(L"");
4061 if (wline == NULL) {
4062 err = got_error_from_errno("wcsdup");
4063 free(line);
4064 return err;
4066 } else if (s->first_displayed_line + nprinted ==
4067 s->matched_line &&
4068 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4069 err = add_matched_line(&width, line, view->ncols - 9, 9,
4070 view->window, regmatch);
4071 if (err) {
4072 free(line);
4073 return err;
4075 width += 9;
4076 } else {
4077 err = format_line(&wline, &width, line,
4078 view->ncols - 9, 9);
4079 waddwstr(view->window, wline);
4080 free(wline);
4081 wline = NULL;
4082 width += 9;
4085 if (width <= view->ncols - 1)
4086 waddch(view->window, '\n');
4087 if (++nprinted == 1)
4088 s->first_displayed_line = lineno;
4090 free(line);
4091 s->last_displayed_line = lineno;
4093 view_vborder(view);
4095 return NULL;
4098 static const struct got_error *
4099 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4101 const struct got_error *err = NULL;
4102 struct tog_blame_cb_args *a = arg;
4103 struct tog_blame_line *line;
4104 int errcode;
4106 if (nlines != a->nlines ||
4107 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4108 return got_error(GOT_ERR_RANGE);
4110 errcode = pthread_mutex_lock(&tog_mutex);
4111 if (errcode)
4112 return got_error_set_errno(errcode, "pthread_mutex_lock");
4114 if (*a->quit) { /* user has quit the blame view */
4115 err = got_error(GOT_ERR_ITER_COMPLETED);
4116 goto done;
4119 if (lineno == -1)
4120 goto done; /* no change in this commit */
4122 line = &a->lines[lineno - 1];
4123 if (line->annotated)
4124 goto done;
4126 line->id = got_object_id_dup(id);
4127 if (line->id == NULL) {
4128 err = got_error_from_errno("got_object_id_dup");
4129 goto done;
4131 line->annotated = 1;
4132 done:
4133 errcode = pthread_mutex_unlock(&tog_mutex);
4134 if (errcode)
4135 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4136 return err;
4139 static void *
4140 blame_thread(void *arg)
4142 const struct got_error *err, *close_err;
4143 struct tog_blame_thread_args *ta = arg;
4144 struct tog_blame_cb_args *a = ta->cb_args;
4145 int errcode;
4147 err = block_signals_used_by_main_thread();
4148 if (err)
4149 return (void *)err;
4151 err = got_blame(ta->path, a->commit_id, ta->repo,
4152 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4153 if (err && err->code == GOT_ERR_CANCELLED)
4154 err = NULL;
4156 errcode = pthread_mutex_lock(&tog_mutex);
4157 if (errcode)
4158 return (void *)got_error_set_errno(errcode,
4159 "pthread_mutex_lock");
4161 close_err = got_repo_close(ta->repo);
4162 if (err == NULL)
4163 err = close_err;
4164 ta->repo = NULL;
4165 *ta->complete = 1;
4167 errcode = pthread_mutex_unlock(&tog_mutex);
4168 if (errcode && err == NULL)
4169 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4171 return (void *)err;
4174 static struct got_object_id *
4175 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4176 int first_displayed_line, int selected_line)
4178 struct tog_blame_line *line;
4180 if (nlines <= 0)
4181 return NULL;
4183 line = &lines[first_displayed_line - 1 + selected_line - 1];
4184 if (!line->annotated)
4185 return NULL;
4187 return line->id;
4190 static const struct got_error *
4191 stop_blame(struct tog_blame *blame)
4193 const struct got_error *err = NULL;
4194 int i;
4196 if (blame->thread) {
4197 int errcode;
4198 errcode = pthread_mutex_unlock(&tog_mutex);
4199 if (errcode)
4200 return got_error_set_errno(errcode,
4201 "pthread_mutex_unlock");
4202 errcode = pthread_join(blame->thread, (void **)&err);
4203 if (errcode)
4204 return got_error_set_errno(errcode, "pthread_join");
4205 errcode = pthread_mutex_lock(&tog_mutex);
4206 if (errcode)
4207 return got_error_set_errno(errcode,
4208 "pthread_mutex_lock");
4209 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4210 err = NULL;
4211 blame->thread = NULL;
4213 if (blame->thread_args.repo) {
4214 const struct got_error *close_err;
4215 close_err = got_repo_close(blame->thread_args.repo);
4216 if (err == NULL)
4217 err = close_err;
4218 blame->thread_args.repo = NULL;
4220 if (blame->f) {
4221 if (fclose(blame->f) == EOF && err == NULL)
4222 err = got_error_from_errno("fclose");
4223 blame->f = NULL;
4225 if (blame->lines) {
4226 for (i = 0; i < blame->nlines; i++)
4227 free(blame->lines[i].id);
4228 free(blame->lines);
4229 blame->lines = NULL;
4231 free(blame->cb_args.commit_id);
4232 blame->cb_args.commit_id = NULL;
4234 return err;
4237 static const struct got_error *
4238 cancel_blame_view(void *arg)
4240 const struct got_error *err = NULL;
4241 int *done = arg;
4242 int errcode;
4244 errcode = pthread_mutex_lock(&tog_mutex);
4245 if (errcode)
4246 return got_error_set_errno(errcode,
4247 "pthread_mutex_unlock");
4249 if (*done)
4250 err = got_error(GOT_ERR_CANCELLED);
4252 errcode = pthread_mutex_unlock(&tog_mutex);
4253 if (errcode)
4254 return got_error_set_errno(errcode,
4255 "pthread_mutex_lock");
4257 return err;
4260 static const struct got_error *
4261 run_blame(struct tog_view *view)
4263 struct tog_blame_view_state *s = &view->state.blame;
4264 struct tog_blame *blame = &s->blame;
4265 const struct got_error *err = NULL;
4266 struct got_blob_object *blob = NULL;
4267 struct got_repository *thread_repo = NULL;
4268 struct got_object_id *obj_id = NULL;
4269 int obj_type;
4271 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4272 s->path);
4273 if (err)
4274 return err;
4276 err = got_object_get_type(&obj_type, s->repo, obj_id);
4277 if (err)
4278 goto done;
4280 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4281 err = got_error(GOT_ERR_OBJ_TYPE);
4282 goto done;
4285 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4286 if (err)
4287 goto done;
4288 blame->f = got_opentemp();
4289 if (blame->f == NULL) {
4290 err = got_error_from_errno("got_opentemp");
4291 goto done;
4293 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4294 &blame->line_offsets, blame->f, blob);
4295 if (err)
4296 goto done;
4297 if (blame->nlines == 0) {
4298 s->blame_complete = 1;
4299 goto done;
4302 /* Don't include \n at EOF in the blame line count. */
4303 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4304 blame->nlines--;
4306 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4307 if (blame->lines == NULL) {
4308 err = got_error_from_errno("calloc");
4309 goto done;
4312 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4313 if (err)
4314 goto done;
4316 blame->cb_args.view = view;
4317 blame->cb_args.lines = blame->lines;
4318 blame->cb_args.nlines = blame->nlines;
4319 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4320 if (blame->cb_args.commit_id == NULL) {
4321 err = got_error_from_errno("got_object_id_dup");
4322 goto done;
4324 blame->cb_args.quit = &s->done;
4326 blame->thread_args.path = s->path;
4327 blame->thread_args.repo = thread_repo;
4328 blame->thread_args.cb_args = &blame->cb_args;
4329 blame->thread_args.complete = &s->blame_complete;
4330 blame->thread_args.cancel_cb = cancel_blame_view;
4331 blame->thread_args.cancel_arg = &s->done;
4332 s->blame_complete = 0;
4334 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4335 s->first_displayed_line = 1;
4336 s->last_displayed_line = view->nlines;
4337 s->selected_line = 1;
4340 done:
4341 if (blob)
4342 got_object_blob_close(blob);
4343 free(obj_id);
4344 if (err)
4345 stop_blame(blame);
4346 return err;
4349 static const struct got_error *
4350 open_blame_view(struct tog_view *view, char *path,
4351 struct got_object_id *commit_id, struct got_repository *repo)
4353 const struct got_error *err = NULL;
4354 struct tog_blame_view_state *s = &view->state.blame;
4356 STAILQ_INIT(&s->blamed_commits);
4358 s->path = strdup(path);
4359 if (s->path == NULL)
4360 return got_error_from_errno("strdup");
4362 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4363 if (err) {
4364 free(s->path);
4365 return err;
4368 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4369 s->first_displayed_line = 1;
4370 s->last_displayed_line = view->nlines;
4371 s->selected_line = 1;
4372 s->blame_complete = 0;
4373 s->repo = repo;
4374 s->commit_id = commit_id;
4375 memset(&s->blame, 0, sizeof(s->blame));
4377 STAILQ_INIT(&s->colors);
4378 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4379 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4380 get_color_value("TOG_COLOR_COMMIT"));
4381 if (err)
4382 return err;
4385 view->show = show_blame_view;
4386 view->input = input_blame_view;
4387 view->close = close_blame_view;
4388 view->search_start = search_start_blame_view;
4389 view->search_next = search_next_blame_view;
4391 return run_blame(view);
4394 static const struct got_error *
4395 close_blame_view(struct tog_view *view)
4397 const struct got_error *err = NULL;
4398 struct tog_blame_view_state *s = &view->state.blame;
4400 if (s->blame.thread)
4401 err = stop_blame(&s->blame);
4403 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4404 struct got_object_qid *blamed_commit;
4405 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4406 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4407 got_object_qid_free(blamed_commit);
4410 free(s->path);
4411 free_colors(&s->colors);
4413 return err;
4416 static const struct got_error *
4417 search_start_blame_view(struct tog_view *view)
4419 struct tog_blame_view_state *s = &view->state.blame;
4421 s->matched_line = 0;
4422 return NULL;
4425 static const struct got_error *
4426 search_next_blame_view(struct tog_view *view)
4428 struct tog_blame_view_state *s = &view->state.blame;
4429 int lineno;
4430 char *line = NULL;
4431 size_t linesize = 0;
4432 ssize_t linelen;
4434 if (!view->searching) {
4435 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4436 return NULL;
4439 if (s->matched_line) {
4440 if (view->searching == TOG_SEARCH_FORWARD)
4441 lineno = s->matched_line + 1;
4442 else
4443 lineno = s->matched_line - 1;
4444 } else {
4445 if (view->searching == TOG_SEARCH_FORWARD)
4446 lineno = 1;
4447 else
4448 lineno = s->blame.nlines;
4451 while (1) {
4452 off_t offset;
4454 if (lineno <= 0 || lineno > s->blame.nlines) {
4455 if (s->matched_line == 0) {
4456 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4457 break;
4460 if (view->searching == TOG_SEARCH_FORWARD)
4461 lineno = 1;
4462 else
4463 lineno = s->blame.nlines;
4466 offset = s->blame.line_offsets[lineno - 1];
4467 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4468 free(line);
4469 return got_error_from_errno("fseeko");
4471 linelen = getline(&line, &linesize, s->blame.f);
4472 if (linelen != -1 &&
4473 match_line(line, &view->regex, 1, &view->regmatch)) {
4474 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4475 s->matched_line = lineno;
4476 break;
4478 if (view->searching == TOG_SEARCH_FORWARD)
4479 lineno++;
4480 else
4481 lineno--;
4483 free(line);
4485 if (s->matched_line) {
4486 s->first_displayed_line = s->matched_line;
4487 s->selected_line = 1;
4490 return NULL;
4493 static const struct got_error *
4494 show_blame_view(struct tog_view *view)
4496 const struct got_error *err = NULL;
4497 struct tog_blame_view_state *s = &view->state.blame;
4498 int errcode;
4500 if (s->blame.thread == NULL && !s->blame_complete) {
4501 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4502 &s->blame.thread_args);
4503 if (errcode)
4504 return got_error_set_errno(errcode, "pthread_create");
4506 halfdelay(1); /* fast refresh while annotating */
4509 if (s->blame_complete)
4510 halfdelay(10); /* disable fast refresh */
4512 err = draw_blame(view);
4514 view_vborder(view);
4515 return err;
4518 static const struct got_error *
4519 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4521 const struct got_error *err = NULL, *thread_err = NULL;
4522 struct tog_view *diff_view;
4523 struct tog_blame_view_state *s = &view->state.blame;
4524 int begin_x = 0;
4526 switch (ch) {
4527 case 'q':
4528 s->done = 1;
4529 break;
4530 case 'k':
4531 case KEY_UP:
4532 if (s->selected_line > 1)
4533 s->selected_line--;
4534 else if (s->selected_line == 1 &&
4535 s->first_displayed_line > 1)
4536 s->first_displayed_line--;
4537 break;
4538 case KEY_PPAGE:
4539 case CTRL('b'):
4540 if (s->first_displayed_line == 1) {
4541 s->selected_line = 1;
4542 break;
4544 if (s->first_displayed_line > view->nlines - 2)
4545 s->first_displayed_line -=
4546 (view->nlines - 2);
4547 else
4548 s->first_displayed_line = 1;
4549 break;
4550 case 'j':
4551 case KEY_DOWN:
4552 if (s->selected_line < view->nlines - 2 &&
4553 s->first_displayed_line +
4554 s->selected_line <= s->blame.nlines)
4555 s->selected_line++;
4556 else if (s->last_displayed_line <
4557 s->blame.nlines)
4558 s->first_displayed_line++;
4559 break;
4560 case 'b':
4561 case 'p': {
4562 struct got_object_id *id = NULL;
4563 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4564 s->first_displayed_line, s->selected_line);
4565 if (id == NULL)
4566 break;
4567 if (ch == 'p') {
4568 struct got_commit_object *commit;
4569 struct got_object_qid *pid;
4570 struct got_object_id *blob_id = NULL;
4571 int obj_type;
4572 err = got_object_open_as_commit(&commit,
4573 s->repo, id);
4574 if (err)
4575 break;
4576 pid = STAILQ_FIRST(
4577 got_object_commit_get_parent_ids(commit));
4578 if (pid == NULL) {
4579 got_object_commit_close(commit);
4580 break;
4582 /* Check if path history ends here. */
4583 err = got_object_id_by_path(&blob_id, s->repo,
4584 pid->id, s->path);
4585 if (err) {
4586 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4587 err = NULL;
4588 got_object_commit_close(commit);
4589 break;
4591 err = got_object_get_type(&obj_type, s->repo,
4592 blob_id);
4593 free(blob_id);
4594 /* Can't blame non-blob type objects. */
4595 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4596 got_object_commit_close(commit);
4597 break;
4599 err = got_object_qid_alloc(&s->blamed_commit,
4600 pid->id);
4601 got_object_commit_close(commit);
4602 } else {
4603 if (got_object_id_cmp(id,
4604 s->blamed_commit->id) == 0)
4605 break;
4606 err = got_object_qid_alloc(&s->blamed_commit,
4607 id);
4609 if (err)
4610 break;
4611 s->done = 1;
4612 thread_err = stop_blame(&s->blame);
4613 s->done = 0;
4614 if (thread_err)
4615 break;
4616 STAILQ_INSERT_HEAD(&s->blamed_commits,
4617 s->blamed_commit, entry);
4618 err = run_blame(view);
4619 if (err)
4620 break;
4621 break;
4623 case 'B': {
4624 struct got_object_qid *first;
4625 first = STAILQ_FIRST(&s->blamed_commits);
4626 if (!got_object_id_cmp(first->id, s->commit_id))
4627 break;
4628 s->done = 1;
4629 thread_err = stop_blame(&s->blame);
4630 s->done = 0;
4631 if (thread_err)
4632 break;
4633 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4634 got_object_qid_free(s->blamed_commit);
4635 s->blamed_commit =
4636 STAILQ_FIRST(&s->blamed_commits);
4637 err = run_blame(view);
4638 if (err)
4639 break;
4640 break;
4642 case KEY_ENTER:
4643 case '\r': {
4644 struct got_object_id *id = NULL;
4645 struct got_object_qid *pid;
4646 struct got_commit_object *commit = NULL;
4647 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4648 s->first_displayed_line, s->selected_line);
4649 if (id == NULL)
4650 break;
4651 err = got_object_open_as_commit(&commit, s->repo, id);
4652 if (err)
4653 break;
4654 pid = STAILQ_FIRST(
4655 got_object_commit_get_parent_ids(commit));
4656 if (view_is_parent_view(view))
4657 begin_x = view_split_begin_x(view->begin_x);
4658 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4659 if (diff_view == NULL) {
4660 got_object_commit_close(commit);
4661 err = got_error_from_errno("view_open");
4662 break;
4664 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4665 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4666 got_object_commit_close(commit);
4667 if (err) {
4668 view_close(diff_view);
4669 break;
4671 view->focussed = 0;
4672 diff_view->focussed = 1;
4673 if (view_is_parent_view(view)) {
4674 err = view_close_child(view);
4675 if (err)
4676 break;
4677 view_set_child(view, diff_view);
4678 view->focus_child = 1;
4679 } else
4680 *new_view = diff_view;
4681 if (err)
4682 break;
4683 break;
4685 case KEY_NPAGE:
4686 case CTRL('f'):
4687 case ' ':
4688 if (s->last_displayed_line >= s->blame.nlines &&
4689 s->selected_line >= MIN(s->blame.nlines,
4690 view->nlines - 2)) {
4691 break;
4693 if (s->last_displayed_line >= s->blame.nlines &&
4694 s->selected_line < view->nlines - 2) {
4695 s->selected_line = MIN(s->blame.nlines,
4696 view->nlines - 2);
4697 break;
4699 if (s->last_displayed_line + view->nlines - 2
4700 <= s->blame.nlines)
4701 s->first_displayed_line +=
4702 view->nlines - 2;
4703 else
4704 s->first_displayed_line =
4705 s->blame.nlines -
4706 (view->nlines - 3);
4707 break;
4708 case KEY_RESIZE:
4709 if (s->selected_line > view->nlines - 2) {
4710 s->selected_line = MIN(s->blame.nlines,
4711 view->nlines - 2);
4713 break;
4714 default:
4715 break;
4717 return thread_err ? thread_err : err;
4720 static const struct got_error *
4721 cmd_blame(int argc, char *argv[])
4723 const struct got_error *error;
4724 struct got_repository *repo = NULL;
4725 struct got_worktree *worktree = NULL;
4726 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4727 char *link_target = NULL;
4728 struct got_object_id *commit_id = NULL;
4729 char *commit_id_str = NULL;
4730 int ch;
4731 struct tog_view *view;
4733 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4734 switch (ch) {
4735 case 'c':
4736 commit_id_str = optarg;
4737 break;
4738 case 'r':
4739 repo_path = realpath(optarg, NULL);
4740 if (repo_path == NULL)
4741 return got_error_from_errno2("realpath",
4742 optarg);
4743 break;
4744 default:
4745 usage_blame();
4746 /* NOTREACHED */
4750 argc -= optind;
4751 argv += optind;
4753 if (argc != 1)
4754 usage_blame();
4756 if (repo_path == NULL) {
4757 cwd = getcwd(NULL, 0);
4758 if (cwd == NULL)
4759 return got_error_from_errno("getcwd");
4760 error = got_worktree_open(&worktree, cwd);
4761 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4762 goto done;
4763 if (worktree)
4764 repo_path =
4765 strdup(got_worktree_get_repo_path(worktree));
4766 else
4767 repo_path = strdup(cwd);
4768 if (repo_path == NULL) {
4769 error = got_error_from_errno("strdup");
4770 goto done;
4774 error = got_repo_open(&repo, repo_path, NULL);
4775 if (error != NULL)
4776 goto done;
4778 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4779 worktree);
4780 if (error)
4781 goto done;
4783 init_curses();
4785 error = apply_unveil(got_repo_get_path(repo), NULL);
4786 if (error)
4787 goto done;
4789 error = tog_load_refs(repo);
4790 if (error)
4791 goto done;
4793 if (commit_id_str == NULL) {
4794 struct got_reference *head_ref;
4795 error = got_ref_open(&head_ref, repo, worktree ?
4796 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4797 if (error != NULL)
4798 goto done;
4799 error = got_ref_resolve(&commit_id, repo, head_ref);
4800 got_ref_close(head_ref);
4801 } else {
4802 error = got_repo_match_object_id(&commit_id, NULL,
4803 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4805 if (error != NULL)
4806 goto done;
4808 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4809 if (view == NULL) {
4810 error = got_error_from_errno("view_open");
4811 goto done;
4814 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4815 commit_id, repo);
4816 if (error)
4817 goto done;
4819 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4820 commit_id, repo);
4821 if (error)
4822 goto done;
4823 if (worktree) {
4824 /* Release work tree lock. */
4825 got_worktree_close(worktree);
4826 worktree = NULL;
4828 error = view_loop(view);
4829 done:
4830 free(repo_path);
4831 free(in_repo_path);
4832 free(link_target);
4833 free(cwd);
4834 free(commit_id);
4835 if (worktree)
4836 got_worktree_close(worktree);
4837 if (repo) {
4838 const struct got_error *close_err = got_repo_close(repo);
4839 if (error == NULL)
4840 error = close_err;
4842 tog_free_refs();
4843 return error;
4846 static const struct got_error *
4847 draw_tree_entries(struct tog_view *view, const char *parent_path)
4849 struct tog_tree_view_state *s = &view->state.tree;
4850 const struct got_error *err = NULL;
4851 struct got_tree_entry *te;
4852 wchar_t *wline;
4853 struct tog_color *tc;
4854 int width, n, i, nentries;
4855 int limit = view->nlines;
4857 s->ndisplayed = 0;
4859 werase(view->window);
4861 if (limit == 0)
4862 return NULL;
4864 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4865 if (err)
4866 return err;
4867 if (view_needs_focus_indication(view))
4868 wstandout(view->window);
4869 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4870 if (tc)
4871 wattr_on(view->window,
4872 COLOR_PAIR(tc->colorpair), NULL);
4873 waddwstr(view->window, wline);
4874 if (tc)
4875 wattr_off(view->window,
4876 COLOR_PAIR(tc->colorpair), NULL);
4877 if (view_needs_focus_indication(view))
4878 wstandend(view->window);
4879 free(wline);
4880 wline = NULL;
4881 if (width < view->ncols - 1)
4882 waddch(view->window, '\n');
4883 if (--limit <= 0)
4884 return NULL;
4885 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4886 if (err)
4887 return err;
4888 waddwstr(view->window, wline);
4889 free(wline);
4890 wline = NULL;
4891 if (width < view->ncols - 1)
4892 waddch(view->window, '\n');
4893 if (--limit <= 0)
4894 return NULL;
4895 waddch(view->window, '\n');
4896 if (--limit <= 0)
4897 return NULL;
4899 if (s->first_displayed_entry == NULL) {
4900 te = got_object_tree_get_first_entry(s->tree);
4901 if (s->selected == 0) {
4902 if (view->focussed)
4903 wstandout(view->window);
4904 s->selected_entry = NULL;
4906 waddstr(view->window, " ..\n"); /* parent directory */
4907 if (s->selected == 0 && view->focussed)
4908 wstandend(view->window);
4909 s->ndisplayed++;
4910 if (--limit <= 0)
4911 return NULL;
4912 n = 1;
4913 } else {
4914 n = 0;
4915 te = s->first_displayed_entry;
4918 nentries = got_object_tree_get_nentries(s->tree);
4919 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4920 char *line = NULL, *id_str = NULL, *link_target = NULL;
4921 const char *modestr = "";
4922 mode_t mode;
4924 te = got_object_tree_get_entry(s->tree, i);
4925 mode = got_tree_entry_get_mode(te);
4927 if (s->show_ids) {
4928 err = got_object_id_str(&id_str,
4929 got_tree_entry_get_id(te));
4930 if (err)
4931 return got_error_from_errno(
4932 "got_object_id_str");
4934 if (got_object_tree_entry_is_submodule(te))
4935 modestr = "$";
4936 else if (S_ISLNK(mode)) {
4937 int i;
4939 err = got_tree_entry_get_symlink_target(&link_target,
4940 te, s->repo);
4941 if (err) {
4942 free(id_str);
4943 return err;
4945 for (i = 0; i < strlen(link_target); i++) {
4946 if (!isprint((unsigned char)link_target[i]))
4947 link_target[i] = '?';
4949 modestr = "@";
4951 else if (S_ISDIR(mode))
4952 modestr = "/";
4953 else if (mode & S_IXUSR)
4954 modestr = "*";
4955 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4956 got_tree_entry_get_name(te), modestr,
4957 link_target ? " -> ": "",
4958 link_target ? link_target : "") == -1) {
4959 free(id_str);
4960 free(link_target);
4961 return got_error_from_errno("asprintf");
4963 free(id_str);
4964 free(link_target);
4965 err = format_line(&wline, &width, line, view->ncols, 0);
4966 if (err) {
4967 free(line);
4968 break;
4970 if (n == s->selected) {
4971 if (view->focussed)
4972 wstandout(view->window);
4973 s->selected_entry = te;
4975 tc = match_color(&s->colors, line);
4976 if (tc)
4977 wattr_on(view->window,
4978 COLOR_PAIR(tc->colorpair), NULL);
4979 waddwstr(view->window, wline);
4980 if (tc)
4981 wattr_off(view->window,
4982 COLOR_PAIR(tc->colorpair), NULL);
4983 if (width < view->ncols - 1)
4984 waddch(view->window, '\n');
4985 if (n == s->selected && view->focussed)
4986 wstandend(view->window);
4987 free(line);
4988 free(wline);
4989 wline = NULL;
4990 n++;
4991 s->ndisplayed++;
4992 s->last_displayed_entry = te;
4993 if (--limit <= 0)
4994 break;
4997 return err;
5000 static void
5001 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5003 struct got_tree_entry *te;
5004 int isroot = s->tree == s->root;
5005 int i = 0;
5007 if (s->first_displayed_entry == NULL)
5008 return;
5010 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5011 while (i++ < maxscroll) {
5012 if (te == NULL) {
5013 if (!isroot)
5014 s->first_displayed_entry = NULL;
5015 break;
5017 s->first_displayed_entry = te;
5018 te = got_tree_entry_get_prev(s->tree, te);
5022 static void
5023 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5025 struct got_tree_entry *next, *last;
5026 int n = 0;
5028 if (s->first_displayed_entry)
5029 next = got_tree_entry_get_next(s->tree,
5030 s->first_displayed_entry);
5031 else
5032 next = got_object_tree_get_first_entry(s->tree);
5034 last = s->last_displayed_entry;
5035 while (next && last && n++ < maxscroll) {
5036 last = got_tree_entry_get_next(s->tree, last);
5037 if (last) {
5038 s->first_displayed_entry = next;
5039 next = got_tree_entry_get_next(s->tree, next);
5044 static const struct got_error *
5045 tree_entry_path(char **path, struct tog_parent_trees *parents,
5046 struct got_tree_entry *te)
5048 const struct got_error *err = NULL;
5049 struct tog_parent_tree *pt;
5050 size_t len = 2; /* for leading slash and NUL */
5052 TAILQ_FOREACH(pt, parents, entry)
5053 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5054 + 1 /* slash */;
5055 if (te)
5056 len += strlen(got_tree_entry_get_name(te));
5058 *path = calloc(1, len);
5059 if (path == NULL)
5060 return got_error_from_errno("calloc");
5062 (*path)[0] = '/';
5063 pt = TAILQ_LAST(parents, tog_parent_trees);
5064 while (pt) {
5065 const char *name = got_tree_entry_get_name(pt->selected_entry);
5066 if (strlcat(*path, name, len) >= len) {
5067 err = got_error(GOT_ERR_NO_SPACE);
5068 goto done;
5070 if (strlcat(*path, "/", len) >= len) {
5071 err = got_error(GOT_ERR_NO_SPACE);
5072 goto done;
5074 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5076 if (te) {
5077 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5078 err = got_error(GOT_ERR_NO_SPACE);
5079 goto done;
5082 done:
5083 if (err) {
5084 free(*path);
5085 *path = NULL;
5087 return err;
5090 static const struct got_error *
5091 blame_tree_entry(struct tog_view **new_view, int begin_x,
5092 struct got_tree_entry *te, struct tog_parent_trees *parents,
5093 struct got_object_id *commit_id, struct got_repository *repo)
5095 const struct got_error *err = NULL;
5096 char *path;
5097 struct tog_view *blame_view;
5099 *new_view = NULL;
5101 err = tree_entry_path(&path, parents, te);
5102 if (err)
5103 return err;
5105 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5106 if (blame_view == NULL) {
5107 err = got_error_from_errno("view_open");
5108 goto done;
5111 err = open_blame_view(blame_view, path, commit_id, repo);
5112 if (err) {
5113 if (err->code == GOT_ERR_CANCELLED)
5114 err = NULL;
5115 view_close(blame_view);
5116 } else
5117 *new_view = blame_view;
5118 done:
5119 free(path);
5120 return err;
5123 static const struct got_error *
5124 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5125 struct tog_tree_view_state *s)
5127 struct tog_view *log_view;
5128 const struct got_error *err = NULL;
5129 char *path;
5131 *new_view = NULL;
5133 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5134 if (log_view == NULL)
5135 return got_error_from_errno("view_open");
5137 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5138 if (err)
5139 return err;
5141 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5142 path, 0);
5143 if (err)
5144 view_close(log_view);
5145 else
5146 *new_view = log_view;
5147 free(path);
5148 return err;
5151 static const struct got_error *
5152 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5153 const char *head_ref_name, struct got_repository *repo)
5155 const struct got_error *err = NULL;
5156 char *commit_id_str = NULL;
5157 struct tog_tree_view_state *s = &view->state.tree;
5158 struct got_commit_object *commit = NULL;
5160 TAILQ_INIT(&s->parents);
5161 STAILQ_INIT(&s->colors);
5163 s->commit_id = got_object_id_dup(commit_id);
5164 if (s->commit_id == NULL)
5165 return got_error_from_errno("got_object_id_dup");
5167 err = got_object_open_as_commit(&commit, repo, commit_id);
5168 if (err)
5169 goto done;
5172 * The root is opened here and will be closed when the view is closed.
5173 * Any visited subtrees and their path-wise parents are opened and
5174 * closed on demand.
5176 err = got_object_open_as_tree(&s->root, repo,
5177 got_object_commit_get_tree_id(commit));
5178 if (err)
5179 goto done;
5180 s->tree = s->root;
5182 err = got_object_id_str(&commit_id_str, commit_id);
5183 if (err != NULL)
5184 goto done;
5186 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5187 err = got_error_from_errno("asprintf");
5188 goto done;
5191 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5192 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5193 if (head_ref_name) {
5194 s->head_ref_name = strdup(head_ref_name);
5195 if (s->head_ref_name == NULL) {
5196 err = got_error_from_errno("strdup");
5197 goto done;
5200 s->repo = repo;
5202 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5203 err = add_color(&s->colors, "\\$$",
5204 TOG_COLOR_TREE_SUBMODULE,
5205 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5206 if (err)
5207 goto done;
5208 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5209 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5210 if (err)
5211 goto done;
5212 err = add_color(&s->colors, "/$",
5213 TOG_COLOR_TREE_DIRECTORY,
5214 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5215 if (err)
5216 goto done;
5218 err = add_color(&s->colors, "\\*$",
5219 TOG_COLOR_TREE_EXECUTABLE,
5220 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5221 if (err)
5222 goto done;
5224 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5225 get_color_value("TOG_COLOR_COMMIT"));
5226 if (err)
5227 goto done;
5230 view->show = show_tree_view;
5231 view->input = input_tree_view;
5232 view->close = close_tree_view;
5233 view->search_start = search_start_tree_view;
5234 view->search_next = search_next_tree_view;
5235 done:
5236 free(commit_id_str);
5237 if (commit)
5238 got_object_commit_close(commit);
5239 if (err)
5240 close_tree_view(view);
5241 return err;
5244 static const struct got_error *
5245 close_tree_view(struct tog_view *view)
5247 struct tog_tree_view_state *s = &view->state.tree;
5249 free_colors(&s->colors);
5250 free(s->tree_label);
5251 s->tree_label = NULL;
5252 free(s->commit_id);
5253 s->commit_id = NULL;
5254 free(s->head_ref_name);
5255 s->head_ref_name = NULL;
5256 while (!TAILQ_EMPTY(&s->parents)) {
5257 struct tog_parent_tree *parent;
5258 parent = TAILQ_FIRST(&s->parents);
5259 TAILQ_REMOVE(&s->parents, parent, entry);
5260 if (parent->tree != s->root)
5261 got_object_tree_close(parent->tree);
5262 free(parent);
5265 if (s->tree != NULL && s->tree != s->root)
5266 got_object_tree_close(s->tree);
5267 if (s->root)
5268 got_object_tree_close(s->root);
5269 return NULL;
5272 static const struct got_error *
5273 search_start_tree_view(struct tog_view *view)
5275 struct tog_tree_view_state *s = &view->state.tree;
5277 s->matched_entry = NULL;
5278 return NULL;
5281 static int
5282 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5284 regmatch_t regmatch;
5286 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5287 0) == 0;
5290 static const struct got_error *
5291 search_next_tree_view(struct tog_view *view)
5293 struct tog_tree_view_state *s = &view->state.tree;
5294 struct got_tree_entry *te = NULL;
5296 if (!view->searching) {
5297 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5298 return NULL;
5301 if (s->matched_entry) {
5302 if (view->searching == TOG_SEARCH_FORWARD) {
5303 if (s->selected_entry)
5304 te = got_tree_entry_get_next(s->tree,
5305 s->selected_entry);
5306 else
5307 te = got_object_tree_get_first_entry(s->tree);
5308 } else {
5309 if (s->selected_entry == NULL)
5310 te = got_object_tree_get_last_entry(s->tree);
5311 else
5312 te = got_tree_entry_get_prev(s->tree,
5313 s->selected_entry);
5315 } else {
5316 if (view->searching == TOG_SEARCH_FORWARD)
5317 te = got_object_tree_get_first_entry(s->tree);
5318 else
5319 te = got_object_tree_get_last_entry(s->tree);
5322 while (1) {
5323 if (te == NULL) {
5324 if (s->matched_entry == NULL) {
5325 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5326 return NULL;
5328 if (view->searching == TOG_SEARCH_FORWARD)
5329 te = got_object_tree_get_first_entry(s->tree);
5330 else
5331 te = got_object_tree_get_last_entry(s->tree);
5334 if (match_tree_entry(te, &view->regex)) {
5335 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5336 s->matched_entry = te;
5337 break;
5340 if (view->searching == TOG_SEARCH_FORWARD)
5341 te = got_tree_entry_get_next(s->tree, te);
5342 else
5343 te = got_tree_entry_get_prev(s->tree, te);
5346 if (s->matched_entry) {
5347 s->first_displayed_entry = s->matched_entry;
5348 s->selected = 0;
5351 return NULL;
5354 static const struct got_error *
5355 show_tree_view(struct tog_view *view)
5357 const struct got_error *err = NULL;
5358 struct tog_tree_view_state *s = &view->state.tree;
5359 char *parent_path;
5361 err = tree_entry_path(&parent_path, &s->parents, NULL);
5362 if (err)
5363 return err;
5365 err = draw_tree_entries(view, parent_path);
5366 free(parent_path);
5368 view_vborder(view);
5369 return err;
5372 static const struct got_error *
5373 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5375 const struct got_error *err = NULL;
5376 struct tog_tree_view_state *s = &view->state.tree;
5377 struct tog_view *log_view, *ref_view;
5378 int begin_x = 0;
5380 switch (ch) {
5381 case 'i':
5382 s->show_ids = !s->show_ids;
5383 break;
5384 case 'l':
5385 if (!s->selected_entry)
5386 break;
5387 if (view_is_parent_view(view))
5388 begin_x = view_split_begin_x(view->begin_x);
5389 err = log_selected_tree_entry(&log_view, begin_x, s);
5390 view->focussed = 0;
5391 log_view->focussed = 1;
5392 if (view_is_parent_view(view)) {
5393 err = view_close_child(view);
5394 if (err)
5395 return err;
5396 view_set_child(view, log_view);
5397 view->focus_child = 1;
5398 } else
5399 *new_view = log_view;
5400 break;
5401 case 'r':
5402 if (view_is_parent_view(view))
5403 begin_x = view_split_begin_x(view->begin_x);
5404 ref_view = view_open(view->nlines, view->ncols,
5405 view->begin_y, begin_x, TOG_VIEW_REF);
5406 if (ref_view == NULL)
5407 return got_error_from_errno("view_open");
5408 err = open_ref_view(ref_view, s->repo);
5409 if (err) {
5410 view_close(ref_view);
5411 return err;
5413 view->focussed = 0;
5414 ref_view->focussed = 1;
5415 if (view_is_parent_view(view)) {
5416 err = view_close_child(view);
5417 if (err)
5418 return err;
5419 view_set_child(view, ref_view);
5420 view->focus_child = 1;
5421 } else
5422 *new_view = ref_view;
5423 break;
5424 case 'k':
5425 case KEY_UP:
5426 if (s->selected > 0) {
5427 s->selected--;
5428 break;
5430 tree_scroll_up(s, 1);
5431 break;
5432 case KEY_PPAGE:
5433 case CTRL('b'):
5434 if (s->tree == s->root) {
5435 if (got_object_tree_get_first_entry(s->tree) ==
5436 s->first_displayed_entry)
5437 s->selected = 0;
5438 } else {
5439 if (s->first_displayed_entry == NULL)
5440 s->selected = 0;
5442 tree_scroll_up(s, MAX(0, view->nlines - 3));
5443 break;
5444 case 'j':
5445 case KEY_DOWN:
5446 if (s->selected < s->ndisplayed - 1) {
5447 s->selected++;
5448 break;
5450 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5451 == NULL)
5452 /* can't scroll any further */
5453 break;
5454 tree_scroll_down(s, 1);
5455 break;
5456 case KEY_NPAGE:
5457 case CTRL('f'):
5458 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5459 == NULL) {
5460 /* can't scroll any further; move cursor down */
5461 if (s->selected < s->ndisplayed - 1)
5462 s->selected = s->ndisplayed - 1;
5463 break;
5465 tree_scroll_down(s, view->nlines - 3);
5466 break;
5467 case KEY_ENTER:
5468 case '\r':
5469 case KEY_BACKSPACE:
5470 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5471 struct tog_parent_tree *parent;
5472 /* user selected '..' */
5473 if (s->tree == s->root)
5474 break;
5475 parent = TAILQ_FIRST(&s->parents);
5476 TAILQ_REMOVE(&s->parents, parent,
5477 entry);
5478 got_object_tree_close(s->tree);
5479 s->tree = parent->tree;
5480 s->first_displayed_entry =
5481 parent->first_displayed_entry;
5482 s->selected_entry =
5483 parent->selected_entry;
5484 s->selected = parent->selected;
5485 free(parent);
5486 } else if (S_ISDIR(got_tree_entry_get_mode(
5487 s->selected_entry))) {
5488 struct got_tree_object *subtree;
5489 err = got_object_open_as_tree(&subtree, s->repo,
5490 got_tree_entry_get_id(s->selected_entry));
5491 if (err)
5492 break;
5493 err = tree_view_visit_subtree(s, subtree);
5494 if (err) {
5495 got_object_tree_close(subtree);
5496 break;
5498 } else if (S_ISREG(got_tree_entry_get_mode(
5499 s->selected_entry))) {
5500 struct tog_view *blame_view;
5501 int begin_x = view_is_parent_view(view) ?
5502 view_split_begin_x(view->begin_x) : 0;
5504 err = blame_tree_entry(&blame_view, begin_x,
5505 s->selected_entry, &s->parents,
5506 s->commit_id, s->repo);
5507 if (err)
5508 break;
5509 view->focussed = 0;
5510 blame_view->focussed = 1;
5511 if (view_is_parent_view(view)) {
5512 err = view_close_child(view);
5513 if (err)
5514 return err;
5515 view_set_child(view, blame_view);
5516 view->focus_child = 1;
5517 } else
5518 *new_view = blame_view;
5520 break;
5521 case KEY_RESIZE:
5522 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5523 s->selected = view->nlines - 4;
5524 break;
5525 default:
5526 break;
5529 return err;
5532 __dead static void
5533 usage_tree(void)
5535 endwin();
5536 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5537 getprogname());
5538 exit(1);
5541 static const struct got_error *
5542 cmd_tree(int argc, char *argv[])
5544 const struct got_error *error;
5545 struct got_repository *repo = NULL;
5546 struct got_worktree *worktree = NULL;
5547 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5548 struct got_object_id *commit_id = NULL;
5549 const char *commit_id_arg = NULL;
5550 char *label = NULL;
5551 struct got_reference *ref = NULL;
5552 const char *head_ref_name = NULL;
5553 int ch;
5554 struct tog_view *view;
5556 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5557 switch (ch) {
5558 case 'c':
5559 commit_id_arg = optarg;
5560 break;
5561 case 'r':
5562 repo_path = realpath(optarg, NULL);
5563 if (repo_path == NULL)
5564 return got_error_from_errno2("realpath",
5565 optarg);
5566 break;
5567 default:
5568 usage_tree();
5569 /* NOTREACHED */
5573 argc -= optind;
5574 argv += optind;
5576 if (argc > 1)
5577 usage_tree();
5579 if (repo_path == NULL) {
5580 cwd = getcwd(NULL, 0);
5581 if (cwd == NULL)
5582 return got_error_from_errno("getcwd");
5583 error = got_worktree_open(&worktree, cwd);
5584 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5585 goto done;
5586 if (worktree)
5587 repo_path =
5588 strdup(got_worktree_get_repo_path(worktree));
5589 else
5590 repo_path = strdup(cwd);
5591 if (repo_path == NULL) {
5592 error = got_error_from_errno("strdup");
5593 goto done;
5597 error = got_repo_open(&repo, repo_path, NULL);
5598 if (error != NULL)
5599 goto done;
5601 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5602 repo, worktree);
5603 if (error)
5604 goto done;
5606 init_curses();
5608 error = apply_unveil(got_repo_get_path(repo), NULL);
5609 if (error)
5610 goto done;
5612 error = tog_load_refs(repo);
5613 if (error)
5614 goto done;
5616 if (commit_id_arg == NULL) {
5617 error = got_repo_match_object_id(&commit_id, &label,
5618 worktree ? got_worktree_get_head_ref_name(worktree) :
5619 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5620 if (error)
5621 goto done;
5622 head_ref_name = label;
5623 } else {
5624 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5625 if (error == NULL)
5626 head_ref_name = got_ref_get_name(ref);
5627 else if (error->code != GOT_ERR_NOT_REF)
5628 goto done;
5629 error = got_repo_match_object_id(&commit_id, NULL,
5630 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5631 if (error)
5632 goto done;
5635 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5636 if (view == NULL) {
5637 error = got_error_from_errno("view_open");
5638 goto done;
5640 error = open_tree_view(view, commit_id, head_ref_name, repo);
5641 if (error)
5642 goto done;
5643 if (!got_path_is_root_dir(in_repo_path)) {
5644 error = tree_view_walk_path(&view->state.tree, commit_id,
5645 in_repo_path);
5646 if (error)
5647 goto done;
5650 if (worktree) {
5651 /* Release work tree lock. */
5652 got_worktree_close(worktree);
5653 worktree = NULL;
5655 error = view_loop(view);
5656 done:
5657 free(repo_path);
5658 free(cwd);
5659 free(commit_id);
5660 free(label);
5661 if (ref)
5662 got_ref_close(ref);
5663 if (repo) {
5664 const struct got_error *close_err = got_repo_close(repo);
5665 if (error == NULL)
5666 error = close_err;
5668 tog_free_refs();
5669 return error;
5672 static const struct got_error *
5673 ref_view_load_refs(struct tog_ref_view_state *s)
5675 struct got_reflist_entry *sre;
5676 struct tog_reflist_entry *re;
5678 s->nrefs = 0;
5679 TAILQ_FOREACH(sre, &tog_refs, entry) {
5680 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5681 continue;
5683 re = malloc(sizeof(*re));
5684 if (re == NULL)
5685 return got_error_from_errno("malloc");
5687 re->ref = got_ref_dup(sre->ref);
5688 if (re->ref == NULL)
5689 return got_error_from_errno("got_ref_dup");
5690 re->idx = s->nrefs++;
5691 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5694 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5695 return NULL;
5698 void
5699 ref_view_free_refs(struct tog_ref_view_state *s)
5701 struct tog_reflist_entry *re;
5703 while (!TAILQ_EMPTY(&s->refs)) {
5704 re = TAILQ_FIRST(&s->refs);
5705 TAILQ_REMOVE(&s->refs, re, entry);
5706 got_ref_close(re->ref);
5707 free(re);
5711 static const struct got_error *
5712 open_ref_view(struct tog_view *view, struct got_repository *repo)
5714 const struct got_error *err = NULL;
5715 struct tog_ref_view_state *s = &view->state.ref;
5717 s->selected_entry = 0;
5718 s->repo = repo;
5720 TAILQ_INIT(&s->refs);
5721 STAILQ_INIT(&s->colors);
5723 err = ref_view_load_refs(s);
5724 if (err)
5725 return err;
5727 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5728 err = add_color(&s->colors, "^refs/heads/",
5729 TOG_COLOR_REFS_HEADS,
5730 get_color_value("TOG_COLOR_REFS_HEADS"));
5731 if (err)
5732 goto done;
5734 err = add_color(&s->colors, "^refs/tags/",
5735 TOG_COLOR_REFS_TAGS,
5736 get_color_value("TOG_COLOR_REFS_TAGS"));
5737 if (err)
5738 goto done;
5740 err = add_color(&s->colors, "^refs/remotes/",
5741 TOG_COLOR_REFS_REMOTES,
5742 get_color_value("TOG_COLOR_REFS_REMOTES"));
5743 if (err)
5744 goto done;
5747 view->show = show_ref_view;
5748 view->input = input_ref_view;
5749 view->close = close_ref_view;
5750 view->search_start = search_start_ref_view;
5751 view->search_next = search_next_ref_view;
5752 done:
5753 if (err)
5754 free_colors(&s->colors);
5755 return err;
5758 static const struct got_error *
5759 close_ref_view(struct tog_view *view)
5761 struct tog_ref_view_state *s = &view->state.ref;
5763 ref_view_free_refs(s);
5764 free_colors(&s->colors);
5766 return NULL;
5769 static const struct got_error *
5770 resolve_reflist_entry(struct got_object_id **commit_id,
5771 struct tog_reflist_entry *re, struct got_repository *repo)
5773 const struct got_error *err = NULL;
5774 struct got_object_id *obj_id;
5775 struct got_tag_object *tag = NULL;
5776 int obj_type;
5778 *commit_id = NULL;
5780 err = got_ref_resolve(&obj_id, repo, re->ref);
5781 if (err)
5782 return err;
5784 err = got_object_get_type(&obj_type, repo, obj_id);
5785 if (err)
5786 goto done;
5788 switch (obj_type) {
5789 case GOT_OBJ_TYPE_COMMIT:
5790 *commit_id = obj_id;
5791 break;
5792 case GOT_OBJ_TYPE_TAG:
5793 err = got_object_open_as_tag(&tag, repo, obj_id);
5794 if (err)
5795 goto done;
5796 free(obj_id);
5797 err = got_object_get_type(&obj_type, repo,
5798 got_object_tag_get_object_id(tag));
5799 if (err)
5800 goto done;
5801 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5802 err = got_error(GOT_ERR_OBJ_TYPE);
5803 goto done;
5805 *commit_id = got_object_id_dup(
5806 got_object_tag_get_object_id(tag));
5807 if (*commit_id == NULL) {
5808 err = got_error_from_errno("got_object_id_dup");
5809 goto done;
5811 break;
5812 default:
5813 err = got_error(GOT_ERR_OBJ_TYPE);
5814 break;
5817 done:
5818 if (tag)
5819 got_object_tag_close(tag);
5820 if (err) {
5821 free(*commit_id);
5822 *commit_id = NULL;
5824 return err;
5827 static const struct got_error *
5828 log_ref_entry(struct tog_view **new_view, int begin_x,
5829 struct tog_reflist_entry *re, struct got_repository *repo)
5831 struct tog_view *log_view;
5832 const struct got_error *err = NULL;
5833 struct got_object_id *commit_id = NULL;
5835 *new_view = NULL;
5837 err = resolve_reflist_entry(&commit_id, re, repo);
5838 if (err) {
5839 if (err->code != GOT_ERR_OBJ_TYPE)
5840 return err;
5841 else
5842 return NULL;
5845 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5846 if (log_view == NULL) {
5847 err = got_error_from_errno("view_open");
5848 goto done;
5851 err = open_log_view(log_view, commit_id, repo,
5852 got_ref_get_name(re->ref), "", 0);
5853 done:
5854 if (err)
5855 view_close(log_view);
5856 else
5857 *new_view = log_view;
5858 free(commit_id);
5859 return err;
5862 static void
5863 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5865 struct tog_reflist_entry *re;
5866 int i = 0;
5868 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5869 return;
5871 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5872 while (i++ < maxscroll) {
5873 if (re == NULL)
5874 break;
5875 s->first_displayed_entry = re;
5876 re = TAILQ_PREV(re, tog_reflist_head, entry);
5880 static void
5881 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5883 struct tog_reflist_entry *next, *last;
5884 int n = 0;
5886 if (s->first_displayed_entry)
5887 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5888 else
5889 next = TAILQ_FIRST(&s->refs);
5891 last = s->last_displayed_entry;
5892 while (next && last && n++ < maxscroll) {
5893 last = TAILQ_NEXT(last, entry);
5894 if (last) {
5895 s->first_displayed_entry = next;
5896 next = TAILQ_NEXT(next, entry);
5901 static const struct got_error *
5902 search_start_ref_view(struct tog_view *view)
5904 struct tog_ref_view_state *s = &view->state.ref;
5906 s->matched_entry = NULL;
5907 return NULL;
5910 static int
5911 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5913 regmatch_t regmatch;
5915 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5916 0) == 0;
5919 static const struct got_error *
5920 search_next_ref_view(struct tog_view *view)
5922 struct tog_ref_view_state *s = &view->state.ref;
5923 struct tog_reflist_entry *re = NULL;
5925 if (!view->searching) {
5926 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5927 return NULL;
5930 if (s->matched_entry) {
5931 if (view->searching == TOG_SEARCH_FORWARD) {
5932 if (s->selected_entry)
5933 re = TAILQ_NEXT(s->selected_entry, entry);
5934 else
5935 re = TAILQ_PREV(s->selected_entry,
5936 tog_reflist_head, entry);
5937 } else {
5938 if (s->selected_entry == NULL)
5939 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5940 else
5941 re = TAILQ_PREV(s->selected_entry,
5942 tog_reflist_head, entry);
5944 } else {
5945 if (view->searching == TOG_SEARCH_FORWARD)
5946 re = TAILQ_FIRST(&s->refs);
5947 else
5948 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5951 while (1) {
5952 if (re == NULL) {
5953 if (s->matched_entry == NULL) {
5954 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5955 return NULL;
5957 if (view->searching == TOG_SEARCH_FORWARD)
5958 re = TAILQ_FIRST(&s->refs);
5959 else
5960 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5963 if (match_reflist_entry(re, &view->regex)) {
5964 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5965 s->matched_entry = re;
5966 break;
5969 if (view->searching == TOG_SEARCH_FORWARD)
5970 re = TAILQ_NEXT(re, entry);
5971 else
5972 re = TAILQ_PREV(re, tog_reflist_head, entry);
5975 if (s->matched_entry) {
5976 s->first_displayed_entry = s->matched_entry;
5977 s->selected = 0;
5980 return NULL;
5983 static const struct got_error *
5984 show_ref_view(struct tog_view *view)
5986 const struct got_error *err = NULL;
5987 struct tog_ref_view_state *s = &view->state.ref;
5988 struct tog_reflist_entry *re;
5989 char *line = NULL;
5990 wchar_t *wline;
5991 struct tog_color *tc;
5992 int width, n;
5993 int limit = view->nlines;
5995 werase(view->window);
5997 s->ndisplayed = 0;
5999 if (limit == 0)
6000 return NULL;
6002 re = s->first_displayed_entry;
6004 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6005 s->nrefs) == -1)
6006 return got_error_from_errno("asprintf");
6008 err = format_line(&wline, &width, line, view->ncols, 0);
6009 if (err) {
6010 free(line);
6011 return err;
6013 if (view_needs_focus_indication(view))
6014 wstandout(view->window);
6015 waddwstr(view->window, wline);
6016 if (view_needs_focus_indication(view))
6017 wstandend(view->window);
6018 free(wline);
6019 wline = NULL;
6020 free(line);
6021 line = NULL;
6022 if (width < view->ncols - 1)
6023 waddch(view->window, '\n');
6024 if (--limit <= 0)
6025 return NULL;
6027 n = 0;
6028 while (re && limit > 0) {
6029 char *line = NULL;
6031 if (got_ref_is_symbolic(re->ref)) {
6032 if (asprintf(&line, "%s -> %s",
6033 got_ref_get_name(re->ref),
6034 got_ref_get_symref_target(re->ref)) == -1)
6035 return got_error_from_errno("asprintf");
6036 } else if (s->show_ids) {
6037 struct got_object_id *id;
6038 char *id_str;
6039 err = got_ref_resolve(&id, s->repo, re->ref);
6040 if (err)
6041 return err;
6042 err = got_object_id_str(&id_str, id);
6043 if (err) {
6044 free(id);
6045 return err;
6047 if (asprintf(&line, "%s: %s",
6048 got_ref_get_name(re->ref), id_str) == -1) {
6049 err = got_error_from_errno("asprintf");
6050 free(id);
6051 free(id_str);
6052 return err;
6054 free(id);
6055 free(id_str);
6056 } else {
6057 line = strdup(got_ref_get_name(re->ref));
6058 if (line == NULL)
6059 return got_error_from_errno("strdup");
6062 err = format_line(&wline, &width, line, view->ncols, 0);
6063 if (err) {
6064 free(line);
6065 return err;
6067 if (n == s->selected) {
6068 if (view->focussed)
6069 wstandout(view->window);
6070 s->selected_entry = re;
6072 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6073 if (tc)
6074 wattr_on(view->window,
6075 COLOR_PAIR(tc->colorpair), NULL);
6076 waddwstr(view->window, wline);
6077 if (tc)
6078 wattr_off(view->window,
6079 COLOR_PAIR(tc->colorpair), NULL);
6080 if (width < view->ncols - 1)
6081 waddch(view->window, '\n');
6082 if (n == s->selected && view->focussed)
6083 wstandend(view->window);
6084 free(line);
6085 free(wline);
6086 wline = NULL;
6087 n++;
6088 s->ndisplayed++;
6089 s->last_displayed_entry = re;
6091 limit--;
6092 re = TAILQ_NEXT(re, entry);
6095 view_vborder(view);
6096 return err;
6099 static const struct got_error *
6100 browse_ref_tree(struct tog_view **new_view, int begin_x,
6101 struct tog_reflist_entry *re, struct got_repository *repo)
6103 const struct got_error *err = NULL;
6104 struct got_object_id *commit_id = NULL;
6105 struct tog_view *tree_view;
6107 *new_view = NULL;
6109 err = resolve_reflist_entry(&commit_id, re, repo);
6110 if (err) {
6111 if (err->code != GOT_ERR_OBJ_TYPE)
6112 return err;
6113 else
6114 return NULL;
6118 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6119 if (tree_view == NULL) {
6120 err = got_error_from_errno("view_open");
6121 goto done;
6124 err = open_tree_view(tree_view, commit_id,
6125 got_ref_get_name(re->ref), repo);
6126 if (err)
6127 goto done;
6129 *new_view = tree_view;
6130 done:
6131 free(commit_id);
6132 return err;
6134 static const struct got_error *
6135 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6137 const struct got_error *err = NULL;
6138 struct tog_ref_view_state *s = &view->state.ref;
6139 struct tog_view *log_view, *tree_view;
6140 int begin_x = 0;
6142 switch (ch) {
6143 case 'i':
6144 s->show_ids = !s->show_ids;
6145 break;
6146 case KEY_ENTER:
6147 case '\r':
6148 if (!s->selected_entry)
6149 break;
6150 if (view_is_parent_view(view))
6151 begin_x = view_split_begin_x(view->begin_x);
6152 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6153 s->repo);
6154 view->focussed = 0;
6155 log_view->focussed = 1;
6156 if (view_is_parent_view(view)) {
6157 err = view_close_child(view);
6158 if (err)
6159 return err;
6160 view_set_child(view, log_view);
6161 view->focus_child = 1;
6162 } else
6163 *new_view = log_view;
6164 break;
6165 case 't':
6166 if (!s->selected_entry)
6167 break;
6168 if (view_is_parent_view(view))
6169 begin_x = view_split_begin_x(view->begin_x);
6170 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6171 s->repo);
6172 if (err || tree_view == NULL)
6173 break;
6174 view->focussed = 0;
6175 tree_view->focussed = 1;
6176 if (view_is_parent_view(view)) {
6177 err = view_close_child(view);
6178 if (err)
6179 return err;
6180 view_set_child(view, tree_view);
6181 view->focus_child = 1;
6182 } else
6183 *new_view = tree_view;
6184 break;
6185 case 'k':
6186 case KEY_UP:
6187 if (s->selected > 0) {
6188 s->selected--;
6189 break;
6191 ref_scroll_up(s, 1);
6192 break;
6193 case KEY_PPAGE:
6194 case CTRL('b'):
6195 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6196 s->selected = 0;
6197 ref_scroll_up(s, MAX(0, view->nlines - 1));
6198 break;
6199 case 'j':
6200 case KEY_DOWN:
6201 if (s->selected < s->ndisplayed - 1) {
6202 s->selected++;
6203 break;
6205 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6206 /* can't scroll any further */
6207 break;
6208 ref_scroll_down(s, 1);
6209 break;
6210 case KEY_NPAGE:
6211 case CTRL('f'):
6212 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6213 /* can't scroll any further; move cursor down */
6214 if (s->selected < s->ndisplayed - 1)
6215 s->selected = s->ndisplayed - 1;
6216 break;
6218 ref_scroll_down(s, view->nlines - 1);
6219 break;
6220 case CTRL('l'):
6221 tog_free_refs();
6222 err = tog_load_refs(s->repo);
6223 if (err)
6224 break;
6225 ref_view_free_refs(s);
6226 err = ref_view_load_refs(s);
6227 break;
6228 case KEY_RESIZE:
6229 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6230 s->selected = view->nlines - 2;
6231 break;
6232 default:
6233 break;
6236 return err;
6239 __dead static void
6240 usage_ref(void)
6242 endwin();
6243 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6244 getprogname());
6245 exit(1);
6248 static const struct got_error *
6249 cmd_ref(int argc, char *argv[])
6251 const struct got_error *error;
6252 struct got_repository *repo = NULL;
6253 struct got_worktree *worktree = NULL;
6254 char *cwd = NULL, *repo_path = NULL;
6255 int ch;
6256 struct tog_view *view;
6258 while ((ch = getopt(argc, argv, "r:")) != -1) {
6259 switch (ch) {
6260 case 'r':
6261 repo_path = realpath(optarg, NULL);
6262 if (repo_path == NULL)
6263 return got_error_from_errno2("realpath",
6264 optarg);
6265 break;
6266 default:
6267 usage_ref();
6268 /* NOTREACHED */
6272 argc -= optind;
6273 argv += optind;
6275 if (argc > 1)
6276 usage_ref();
6278 if (repo_path == NULL) {
6279 cwd = getcwd(NULL, 0);
6280 if (cwd == NULL)
6281 return got_error_from_errno("getcwd");
6282 error = got_worktree_open(&worktree, cwd);
6283 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6284 goto done;
6285 if (worktree)
6286 repo_path =
6287 strdup(got_worktree_get_repo_path(worktree));
6288 else
6289 repo_path = strdup(cwd);
6290 if (repo_path == NULL) {
6291 error = got_error_from_errno("strdup");
6292 goto done;
6296 error = got_repo_open(&repo, repo_path, NULL);
6297 if (error != NULL)
6298 goto done;
6300 init_curses();
6302 error = apply_unveil(got_repo_get_path(repo), NULL);
6303 if (error)
6304 goto done;
6306 error = tog_load_refs(repo);
6307 if (error)
6308 goto done;
6310 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6311 if (view == NULL) {
6312 error = got_error_from_errno("view_open");
6313 goto done;
6316 error = open_ref_view(view, repo);
6317 if (error)
6318 goto done;
6320 if (worktree) {
6321 /* Release work tree lock. */
6322 got_worktree_close(worktree);
6323 worktree = NULL;
6325 error = view_loop(view);
6326 done:
6327 free(repo_path);
6328 free(cwd);
6329 if (repo) {
6330 const struct got_error *close_err = got_repo_close(repo);
6331 if (close_err)
6332 error = close_err;
6334 tog_free_refs();
6335 return error;
6338 static void
6339 list_commands(FILE *fp)
6341 size_t i;
6343 fprintf(fp, "commands:");
6344 for (i = 0; i < nitems(tog_commands); i++) {
6345 struct tog_cmd *cmd = &tog_commands[i];
6346 fprintf(fp, " %s", cmd->name);
6348 fputc('\n', fp);
6351 __dead static void
6352 usage(int hflag, int status)
6354 FILE *fp = (status == 0) ? stdout : stderr;
6356 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6357 getprogname());
6358 if (hflag) {
6359 fprintf(fp, "lazy usage: %s path\n", getprogname());
6360 list_commands(fp);
6362 exit(status);
6365 static char **
6366 make_argv(int argc, ...)
6368 va_list ap;
6369 char **argv;
6370 int i;
6372 va_start(ap, argc);
6374 argv = calloc(argc, sizeof(char *));
6375 if (argv == NULL)
6376 err(1, "calloc");
6377 for (i = 0; i < argc; i++) {
6378 argv[i] = strdup(va_arg(ap, char *));
6379 if (argv[i] == NULL)
6380 err(1, "strdup");
6383 va_end(ap);
6384 return argv;
6388 * Try to convert 'tog path' into a 'tog log path' command.
6389 * The user could simply have mistyped the command rather than knowingly
6390 * provided a path. So check whether argv[0] can in fact be resolved
6391 * to a path in the HEAD commit and print a special error if not.
6392 * This hack is for mpi@ <3
6394 static const struct got_error *
6395 tog_log_with_path(int argc, char *argv[])
6397 const struct got_error *error = NULL, *close_err;
6398 struct tog_cmd *cmd = NULL;
6399 struct got_repository *repo = NULL;
6400 struct got_worktree *worktree = NULL;
6401 struct got_object_id *commit_id = NULL, *id = NULL;
6402 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6403 char *commit_id_str = NULL, **cmd_argv = NULL;
6405 cwd = getcwd(NULL, 0);
6406 if (cwd == NULL)
6407 return got_error_from_errno("getcwd");
6409 error = got_worktree_open(&worktree, cwd);
6410 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6411 goto done;
6413 if (worktree)
6414 repo_path = strdup(got_worktree_get_repo_path(worktree));
6415 else
6416 repo_path = strdup(cwd);
6417 if (repo_path == NULL) {
6418 error = got_error_from_errno("strdup");
6419 goto done;
6422 error = got_repo_open(&repo, repo_path, NULL);
6423 if (error != NULL)
6424 goto done;
6426 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6427 repo, worktree);
6428 if (error)
6429 goto done;
6431 error = tog_load_refs(repo);
6432 if (error)
6433 goto done;
6434 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6435 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6436 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6437 if (error)
6438 goto done;
6440 if (worktree) {
6441 got_worktree_close(worktree);
6442 worktree = NULL;
6445 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6446 if (error) {
6447 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6448 goto done;
6449 fprintf(stderr, "%s: '%s' is no known command or path\n",
6450 getprogname(), argv[0]);
6451 usage(1, 1);
6452 /* not reached */
6455 close_err = got_repo_close(repo);
6456 if (error == NULL)
6457 error = close_err;
6458 repo = NULL;
6460 error = got_object_id_str(&commit_id_str, commit_id);
6461 if (error)
6462 goto done;
6464 cmd = &tog_commands[0]; /* log */
6465 argc = 4;
6466 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6467 error = cmd->cmd_main(argc, cmd_argv);
6468 done:
6469 if (repo) {
6470 close_err = got_repo_close(repo);
6471 if (error == NULL)
6472 error = close_err;
6474 if (worktree)
6475 got_worktree_close(worktree);
6476 free(id);
6477 free(commit_id_str);
6478 free(commit_id);
6479 free(cwd);
6480 free(repo_path);
6481 free(in_repo_path);
6482 if (cmd_argv) {
6483 int i;
6484 for (i = 0; i < argc; i++)
6485 free(cmd_argv[i]);
6486 free(cmd_argv);
6488 tog_free_refs();
6489 return error;
6492 int
6493 main(int argc, char *argv[])
6495 const struct got_error *error = NULL;
6496 struct tog_cmd *cmd = NULL;
6497 int ch, hflag = 0, Vflag = 0;
6498 char **cmd_argv = NULL;
6499 static struct option longopts[] = {
6500 { "version", no_argument, NULL, 'V' },
6501 { NULL, 0, NULL, 0}
6504 setlocale(LC_CTYPE, "");
6506 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6507 switch (ch) {
6508 case 'h':
6509 hflag = 1;
6510 break;
6511 case 'V':
6512 Vflag = 1;
6513 break;
6514 default:
6515 usage(hflag, 1);
6516 /* NOTREACHED */
6520 argc -= optind;
6521 argv += optind;
6522 optind = 1;
6523 optreset = 1;
6525 if (Vflag) {
6526 got_version_print_str();
6527 return 0;
6530 #ifndef PROFILE
6531 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6532 NULL) == -1)
6533 err(1, "pledge");
6534 #endif
6536 if (argc == 0) {
6537 if (hflag)
6538 usage(hflag, 0);
6539 /* Build an argument vector which runs a default command. */
6540 cmd = &tog_commands[0];
6541 argc = 1;
6542 cmd_argv = make_argv(argc, cmd->name);
6543 } else {
6544 size_t i;
6546 /* Did the user specify a command? */
6547 for (i = 0; i < nitems(tog_commands); i++) {
6548 if (strncmp(tog_commands[i].name, argv[0],
6549 strlen(argv[0])) == 0) {
6550 cmd = &tog_commands[i];
6551 break;
6556 if (cmd == NULL) {
6557 if (argc != 1)
6558 usage(0, 1);
6559 /* No command specified; try log with a path */
6560 error = tog_log_with_path(argc, argv);
6561 } else {
6562 if (hflag)
6563 cmd->cmd_usage();
6564 else
6565 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6568 endwin();
6569 putchar('\n');
6570 if (cmd_argv) {
6571 int i;
6572 for (i = 0; i < argc; i++)
6573 free(cmd_argv[i]);
6574 free(cmd_argv);
6577 if (error && error->code != GOT_ERR_CANCELLED)
6578 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6579 return 0;