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 SIMPLEQ_ENTRY(tog_color) entry;
123 regex_t regex;
124 short colorpair;
125 };
126 SIMPLEQ_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 SIMPLEQ_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 (!SIMPLEQ_EMPTY(colors)) {
194 tc = SIMPLEQ_FIRST(colors);
195 SIMPLEQ_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 SIMPLEQ_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_tree_object *root;
415 struct got_tree_object *tree;
416 struct got_tree_entry *first_displayed_entry;
417 struct got_tree_entry *last_displayed_entry;
418 struct got_tree_entry *selected_entry;
419 int ndisplayed, selected, show_ids;
420 struct tog_parent_trees parents;
421 struct got_object_id *commit_id;
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 searching;
506 #define TOG_SEARCH_FORWARD 1
507 #define TOG_SEARCH_BACKWARD 2
508 int search_next_done;
509 #define TOG_SEARCH_HAVE_MORE 1
510 #define TOG_SEARCH_NO_MORE 2
511 #define TOG_SEARCH_HAVE_NONE 3
512 regex_t regex;
513 regmatch_t regmatch;
514 };
516 static const struct got_error *open_diff_view(struct tog_view *,
517 struct got_object_id *, struct got_object_id *,
518 const char *, const char *, int, int, int, struct tog_view *,
519 struct got_repository *);
520 static const struct got_error *show_diff_view(struct tog_view *);
521 static const struct got_error *input_diff_view(struct tog_view **,
522 struct tog_view *, int);
523 static const struct got_error* close_diff_view(struct tog_view *);
524 static const struct got_error *search_start_diff_view(struct tog_view *);
525 static const struct got_error *search_next_diff_view(struct tog_view *);
527 static const struct got_error *open_log_view(struct tog_view *,
528 struct got_object_id *, struct got_repository *,
529 const char *, const char *, int);
530 static const struct got_error * show_log_view(struct tog_view *);
531 static const struct got_error *input_log_view(struct tog_view **,
532 struct tog_view *, int);
533 static const struct got_error *close_log_view(struct tog_view *);
534 static const struct got_error *search_start_log_view(struct tog_view *);
535 static const struct got_error *search_next_log_view(struct tog_view *);
537 static const struct got_error *open_blame_view(struct tog_view *, char *,
538 struct got_object_id *, struct got_repository *);
539 static const struct got_error *show_blame_view(struct tog_view *);
540 static const struct got_error *input_blame_view(struct tog_view **,
541 struct tog_view *, int);
542 static const struct got_error *close_blame_view(struct tog_view *);
543 static const struct got_error *search_start_blame_view(struct tog_view *);
544 static const struct got_error *search_next_blame_view(struct tog_view *);
546 static const struct got_error *open_tree_view(struct tog_view *,
547 struct got_tree_object *, struct got_object_id *, const char *,
548 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->nlines < 1)
789 return NULL;
791 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
792 wclrtoeol(view->window);
794 nocbreak();
795 echo();
796 ret = wgetnstr(view->window, pattern, sizeof(pattern));
797 cbreak();
798 noecho();
799 if (ret == ERR)
800 return NULL;
802 if (view->searching) {
803 regfree(&view->regex);
804 view->searching = 0;
807 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
808 err = view->search_start(view);
809 if (err) {
810 regfree(&view->regex);
811 return err;
813 view->searching = TOG_SEARCH_FORWARD;
814 view->search_next_done = 0;
815 view->search_next(view);
818 return NULL;
821 static const struct got_error *
822 view_input(struct tog_view **new, int *done, struct tog_view *view,
823 struct tog_view_list_head *views)
825 const struct got_error *err = NULL;
826 struct tog_view *v;
827 int ch, errcode;
829 *new = NULL;
831 /* Clear "no matches" indicator. */
832 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
833 view->search_next_done == TOG_SEARCH_HAVE_NONE)
834 view->search_next_done = TOG_SEARCH_HAVE_MORE;
836 if (view->searching && !view->search_next_done) {
837 errcode = pthread_mutex_unlock(&tog_mutex);
838 if (errcode)
839 return got_error_set_errno(errcode,
840 "pthread_mutex_unlock");
841 pthread_yield();
842 errcode = pthread_mutex_lock(&tog_mutex);
843 if (errcode)
844 return got_error_set_errno(errcode,
845 "pthread_mutex_lock");
846 view->search_next(view);
847 return NULL;
850 nodelay(stdscr, FALSE);
851 /* Allow threads to make progress while we are waiting for input. */
852 errcode = pthread_mutex_unlock(&tog_mutex);
853 if (errcode)
854 return got_error_set_errno(errcode, "pthread_mutex_unlock");
855 ch = wgetch(view->window);
856 errcode = pthread_mutex_lock(&tog_mutex);
857 if (errcode)
858 return got_error_set_errno(errcode, "pthread_mutex_lock");
859 nodelay(stdscr, TRUE);
861 if (tog_sigwinch_received || tog_sigcont_received) {
862 tog_resizeterm();
863 tog_sigwinch_received = 0;
864 tog_sigcont_received = 0;
865 TAILQ_FOREACH(v, views, entry) {
866 err = view_resize(v);
867 if (err)
868 return err;
869 err = v->input(new, v, KEY_RESIZE);
870 if (err)
871 return err;
872 if (v->child) {
873 err = view_resize(v->child);
874 if (err)
875 return err;
876 err = v->child->input(new, v->child,
877 KEY_RESIZE);
878 if (err)
879 return err;
884 switch (ch) {
885 case ERR:
886 break;
887 case '\t':
888 if (view->child) {
889 view->focussed = 0;
890 view->child->focussed = 1;
891 view->focus_child = 1;
892 } else if (view->parent) {
893 view->focussed = 0;
894 view->parent->focussed = 1;
895 view->parent->focus_child = 0;
897 break;
898 case 'q':
899 err = view->input(new, view, ch);
900 view->dying = 1;
901 break;
902 case 'Q':
903 *done = 1;
904 break;
905 case 'f':
906 if (view_is_parent_view(view)) {
907 if (view->child == NULL)
908 break;
909 if (view_is_splitscreen(view->child)) {
910 view->focussed = 0;
911 view->child->focussed = 1;
912 err = view_fullscreen(view->child);
913 } else
914 err = view_splitscreen(view->child);
915 if (err)
916 break;
917 err = view->child->input(new, view->child,
918 KEY_RESIZE);
919 } else {
920 if (view_is_splitscreen(view)) {
921 view->parent->focussed = 0;
922 view->focussed = 1;
923 err = view_fullscreen(view);
924 } else {
925 err = view_splitscreen(view);
927 if (err)
928 break;
929 err = view->input(new, view, KEY_RESIZE);
931 break;
932 case KEY_RESIZE:
933 break;
934 case '/':
935 if (view->search_start)
936 view_search_start(view);
937 else
938 err = view->input(new, view, ch);
939 break;
940 case 'N':
941 case 'n':
942 if (view->search_next) {
943 view->searching = (ch == 'n' ?
944 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
945 view->search_next_done = 0;
946 view->search_next(view);
947 } else
948 err = view->input(new, view, ch);
949 break;
950 default:
951 err = view->input(new, view, ch);
952 break;
955 return err;
958 void
959 view_vborder(struct tog_view *view)
961 PANEL *panel;
962 const struct tog_view *view_above;
964 if (view->parent)
965 return view_vborder(view->parent);
967 panel = panel_above(view->panel);
968 if (panel == NULL)
969 return;
971 view_above = panel_userptr(panel);
972 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
973 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
976 int
977 view_needs_focus_indication(struct tog_view *view)
979 if (view_is_parent_view(view)) {
980 if (view->child == NULL || view->child->focussed)
981 return 0;
982 if (!view_is_splitscreen(view->child))
983 return 0;
984 } else if (!view_is_splitscreen(view))
985 return 0;
987 return view->focussed;
990 static const struct got_error *
991 view_loop(struct tog_view *view)
993 const struct got_error *err = NULL;
994 struct tog_view_list_head views;
995 struct tog_view *new_view;
996 int fast_refresh = 10;
997 int done = 0, errcode;
999 errcode = pthread_mutex_lock(&tog_mutex);
1000 if (errcode)
1001 return got_error_set_errno(errcode, "pthread_mutex_lock");
1003 TAILQ_INIT(&views);
1004 TAILQ_INSERT_HEAD(&views, view, entry);
1006 view->focussed = 1;
1007 err = view->show(view);
1008 if (err)
1009 return err;
1010 update_panels();
1011 doupdate();
1012 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1013 /* Refresh fast during initialization, then become slower. */
1014 if (fast_refresh && fast_refresh-- == 0)
1015 halfdelay(10); /* switch to once per second */
1017 err = view_input(&new_view, &done, view, &views);
1018 if (err)
1019 break;
1020 if (view->dying) {
1021 struct tog_view *v, *prev = NULL;
1023 if (view_is_parent_view(view))
1024 prev = TAILQ_PREV(view, tog_view_list_head,
1025 entry);
1026 else if (view->parent)
1027 prev = view->parent;
1029 if (view->parent) {
1030 view->parent->child = NULL;
1031 view->parent->focus_child = 0;
1032 } else
1033 TAILQ_REMOVE(&views, view, entry);
1035 err = view_close(view);
1036 if (err)
1037 goto done;
1039 view = NULL;
1040 TAILQ_FOREACH(v, &views, entry) {
1041 if (v->focussed)
1042 break;
1044 if (view == NULL && new_view == NULL) {
1045 /* No view has focus. Try to pick one. */
1046 if (prev)
1047 view = prev;
1048 else if (!TAILQ_EMPTY(&views)) {
1049 view = TAILQ_LAST(&views,
1050 tog_view_list_head);
1052 if (view) {
1053 if (view->focus_child) {
1054 view->child->focussed = 1;
1055 view = view->child;
1056 } else
1057 view->focussed = 1;
1061 if (new_view) {
1062 struct tog_view *v, *t;
1063 /* Only allow one parent view per type. */
1064 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1065 if (v->type != new_view->type)
1066 continue;
1067 TAILQ_REMOVE(&views, v, entry);
1068 err = view_close(v);
1069 if (err)
1070 goto done;
1071 break;
1073 TAILQ_INSERT_TAIL(&views, new_view, entry);
1074 view = new_view;
1076 if (view) {
1077 if (view_is_parent_view(view)) {
1078 if (view->child && view->child->focussed)
1079 view = view->child;
1080 } else {
1081 if (view->parent && view->parent->focussed)
1082 view = view->parent;
1084 show_panel(view->panel);
1085 if (view->child && view_is_splitscreen(view->child))
1086 show_panel(view->child->panel);
1087 if (view->parent && view_is_splitscreen(view)) {
1088 err = view->parent->show(view->parent);
1089 if (err)
1090 goto done;
1092 err = view->show(view);
1093 if (err)
1094 goto done;
1095 if (view->child) {
1096 err = view->child->show(view->child);
1097 if (err)
1098 goto done;
1100 update_panels();
1101 doupdate();
1104 done:
1105 while (!TAILQ_EMPTY(&views)) {
1106 view = TAILQ_FIRST(&views);
1107 TAILQ_REMOVE(&views, view, entry);
1108 view_close(view);
1111 errcode = pthread_mutex_unlock(&tog_mutex);
1112 if (errcode && err == NULL)
1113 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1115 return err;
1118 __dead static void
1119 usage_log(void)
1121 endwin();
1122 fprintf(stderr,
1123 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1124 getprogname());
1125 exit(1);
1128 /* Create newly allocated wide-character string equivalent to a byte string. */
1129 static const struct got_error *
1130 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1132 char *vis = NULL;
1133 const struct got_error *err = NULL;
1135 *ws = NULL;
1136 *wlen = mbstowcs(NULL, s, 0);
1137 if (*wlen == (size_t)-1) {
1138 int vislen;
1139 if (errno != EILSEQ)
1140 return got_error_from_errno("mbstowcs");
1142 /* byte string invalid in current encoding; try to "fix" it */
1143 err = got_mbsavis(&vis, &vislen, s);
1144 if (err)
1145 return err;
1146 *wlen = mbstowcs(NULL, vis, 0);
1147 if (*wlen == (size_t)-1) {
1148 err = got_error_from_errno("mbstowcs"); /* give up */
1149 goto done;
1153 *ws = calloc(*wlen + 1, sizeof(**ws));
1154 if (*ws == NULL) {
1155 err = got_error_from_errno("calloc");
1156 goto done;
1159 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1160 err = got_error_from_errno("mbstowcs");
1161 done:
1162 free(vis);
1163 if (err) {
1164 free(*ws);
1165 *ws = NULL;
1166 *wlen = 0;
1168 return err;
1171 /* Format a line for display, ensuring that it won't overflow a width limit. */
1172 static const struct got_error *
1173 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1174 int col_tab_align)
1176 const struct got_error *err = NULL;
1177 int cols = 0;
1178 wchar_t *wline = NULL;
1179 size_t wlen;
1180 int i;
1182 *wlinep = NULL;
1183 *widthp = 0;
1185 err = mbs2ws(&wline, &wlen, line);
1186 if (err)
1187 return err;
1189 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1190 wline[wlen - 1] = L'\0';
1191 wlen--;
1193 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1194 wline[wlen - 1] = L'\0';
1195 wlen--;
1198 i = 0;
1199 while (i < wlen) {
1200 int width = wcwidth(wline[i]);
1202 if (width == 0) {
1203 i++;
1204 continue;
1207 if (width == 1 || width == 2) {
1208 if (cols + width > wlimit)
1209 break;
1210 cols += width;
1211 i++;
1212 } else if (width == -1) {
1213 if (wline[i] == L'\t') {
1214 width = TABSIZE -
1215 ((cols + col_tab_align) % TABSIZE);
1216 } else {
1217 width = 1;
1218 wline[i] = L'.';
1220 if (cols + width > wlimit)
1221 break;
1222 cols += width;
1223 i++;
1224 } else {
1225 err = got_error_from_errno("wcwidth");
1226 goto done;
1229 wline[i] = L'\0';
1230 if (widthp)
1231 *widthp = cols;
1232 done:
1233 if (err)
1234 free(wline);
1235 else
1236 *wlinep = wline;
1237 return err;
1240 static const struct got_error*
1241 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1242 struct got_object_id *id, struct got_repository *repo)
1244 static const struct got_error *err = NULL;
1245 struct got_reflist_entry *re;
1246 char *s;
1247 const char *name;
1249 *refs_str = NULL;
1251 TAILQ_FOREACH(re, refs, entry) {
1252 struct got_tag_object *tag = NULL;
1253 struct got_object_id *ref_id;
1254 int cmp;
1256 name = got_ref_get_name(re->ref);
1257 if (strcmp(name, GOT_REF_HEAD) == 0)
1258 continue;
1259 if (strncmp(name, "refs/", 5) == 0)
1260 name += 5;
1261 if (strncmp(name, "got/", 4) == 0)
1262 continue;
1263 if (strncmp(name, "heads/", 6) == 0)
1264 name += 6;
1265 if (strncmp(name, "remotes/", 8) == 0) {
1266 name += 8;
1267 s = strstr(name, "/" GOT_REF_HEAD);
1268 if (s != NULL && s[strlen(s)] == '\0')
1269 continue;
1271 err = got_ref_resolve(&ref_id, repo, re->ref);
1272 if (err)
1273 break;
1274 if (strncmp(name, "tags/", 5) == 0) {
1275 err = got_object_open_as_tag(&tag, repo, ref_id);
1276 if (err) {
1277 if (err->code != GOT_ERR_OBJ_TYPE) {
1278 free(ref_id);
1279 break;
1281 /* Ref points at something other than a tag. */
1282 err = NULL;
1283 tag = NULL;
1286 cmp = got_object_id_cmp(tag ?
1287 got_object_tag_get_object_id(tag) : ref_id, id);
1288 free(ref_id);
1289 if (tag)
1290 got_object_tag_close(tag);
1291 if (cmp != 0)
1292 continue;
1293 s = *refs_str;
1294 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1295 s ? ", " : "", name) == -1) {
1296 err = got_error_from_errno("asprintf");
1297 free(s);
1298 *refs_str = NULL;
1299 break;
1301 free(s);
1304 return err;
1307 static const struct got_error *
1308 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1309 int col_tab_align)
1311 char *smallerthan, *at;
1313 smallerthan = strchr(author, '<');
1314 if (smallerthan && smallerthan[1] != '\0')
1315 author = smallerthan + 1;
1316 at = strchr(author, '@');
1317 if (at)
1318 *at = '\0';
1319 return format_line(wauthor, author_width, author, limit, col_tab_align);
1322 static const struct got_error *
1323 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1324 struct got_object_id *id, const size_t date_display_cols,
1325 int author_display_cols)
1327 struct tog_log_view_state *s = &view->state.log;
1328 const struct got_error *err = NULL;
1329 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1330 char *logmsg0 = NULL, *logmsg = NULL;
1331 char *author = NULL;
1332 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1333 int author_width, logmsg_width;
1334 char *newline, *line = NULL;
1335 int col, limit;
1336 const int avail = view->ncols;
1337 struct tm tm;
1338 time_t committer_time;
1339 struct tog_color *tc;
1341 committer_time = got_object_commit_get_committer_time(commit);
1342 if (localtime_r(&committer_time, &tm) == NULL)
1343 return got_error_from_errno("localtime_r");
1344 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1345 >= sizeof(datebuf))
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 = SIMPLEQ_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 got_tree_object *tree;
1967 struct tog_tree_view_state *s;
1968 struct tog_view *tree_view;
1970 err = got_object_open_as_tree(&tree, repo,
1971 got_object_commit_get_tree_id(entry->commit));
1972 if (err)
1973 return err;
1975 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1976 if (tree_view == NULL)
1977 return got_error_from_errno("view_open");
1979 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1980 if (err) {
1981 got_object_tree_close(tree);
1982 return err;
1984 s = &tree_view->state.tree;
1986 *new_view = tree_view;
1988 if (got_path_is_root_dir(path))
1989 return NULL;
1991 return tree_view_walk_path(s, entry->id, path);
1994 static const struct got_error *
1995 block_signals_used_by_main_thread(void)
1997 sigset_t sigset;
1998 int errcode;
2000 if (sigemptyset(&sigset) == -1)
2001 return got_error_from_errno("sigemptyset");
2003 /* tog handles SIGWINCH and SIGCONT */
2004 if (sigaddset(&sigset, SIGWINCH) == -1)
2005 return got_error_from_errno("sigaddset");
2006 if (sigaddset(&sigset, SIGCONT) == -1)
2007 return got_error_from_errno("sigaddset");
2009 /* ncurses handles SIGTSTP */
2010 if (sigaddset(&sigset, SIGTSTP) == -1)
2011 return got_error_from_errno("sigaddset");
2013 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2014 if (errcode)
2015 return got_error_set_errno(errcode, "pthread_sigmask");
2017 return NULL;
2020 static void *
2021 log_thread(void *arg)
2023 const struct got_error *err = NULL;
2024 int errcode = 0;
2025 struct tog_log_thread_args *a = arg;
2026 int done = 0;
2028 err = block_signals_used_by_main_thread();
2029 if (err)
2030 return (void *)err;
2032 while (!done && !err && !tog_sigpipe_received) {
2033 err = queue_commits(a);
2034 if (err) {
2035 if (err->code != GOT_ERR_ITER_COMPLETED)
2036 return (void *)err;
2037 err = NULL;
2038 done = 1;
2039 } else if (a->commits_needed > 0)
2040 a->commits_needed--;
2042 errcode = pthread_mutex_lock(&tog_mutex);
2043 if (errcode) {
2044 err = got_error_set_errno(errcode,
2045 "pthread_mutex_lock");
2046 break;
2047 } else if (*a->quit)
2048 done = 1;
2049 else if (*a->first_displayed_entry == NULL) {
2050 *a->first_displayed_entry =
2051 TAILQ_FIRST(&a->commits->head);
2052 *a->selected_entry = *a->first_displayed_entry;
2055 errcode = pthread_cond_signal(&a->commit_loaded);
2056 if (errcode) {
2057 err = got_error_set_errno(errcode,
2058 "pthread_cond_signal");
2059 pthread_mutex_unlock(&tog_mutex);
2060 break;
2063 if (done)
2064 a->commits_needed = 0;
2065 else {
2066 if (a->commits_needed == 0) {
2067 errcode = pthread_cond_wait(&a->need_commits,
2068 &tog_mutex);
2069 if (errcode)
2070 err = got_error_set_errno(errcode,
2071 "pthread_cond_wait");
2072 if (*a->quit)
2073 done = 1;
2077 errcode = pthread_mutex_unlock(&tog_mutex);
2078 if (errcode && err == NULL)
2079 err = got_error_set_errno(errcode,
2080 "pthread_mutex_unlock");
2082 a->log_complete = 1;
2083 return (void *)err;
2086 static const struct got_error *
2087 stop_log_thread(struct tog_log_view_state *s)
2089 const struct got_error *err = NULL;
2090 int errcode;
2092 if (s->thread) {
2093 s->quit = 1;
2094 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2095 if (errcode)
2096 return got_error_set_errno(errcode,
2097 "pthread_cond_signal");
2098 errcode = pthread_mutex_unlock(&tog_mutex);
2099 if (errcode)
2100 return got_error_set_errno(errcode,
2101 "pthread_mutex_unlock");
2102 errcode = pthread_join(s->thread, (void **)&err);
2103 if (errcode)
2104 return got_error_set_errno(errcode, "pthread_join");
2105 errcode = pthread_mutex_lock(&tog_mutex);
2106 if (errcode)
2107 return got_error_set_errno(errcode,
2108 "pthread_mutex_lock");
2109 s->thread = NULL;
2112 if (s->thread_args.repo) {
2113 got_repo_close(s->thread_args.repo);
2114 s->thread_args.repo = NULL;
2117 if (s->thread_args.graph) {
2118 got_commit_graph_close(s->thread_args.graph);
2119 s->thread_args.graph = NULL;
2122 return err;
2125 static const struct got_error *
2126 close_log_view(struct tog_view *view)
2128 const struct got_error *err = NULL;
2129 struct tog_log_view_state *s = &view->state.log;
2130 int errcode;
2132 err = stop_log_thread(s);
2134 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2135 if (errcode && err == NULL)
2136 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2138 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2139 if (errcode && err == NULL)
2140 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2142 free_commits(&s->commits);
2143 free(s->in_repo_path);
2144 s->in_repo_path = NULL;
2145 free(s->start_id);
2146 s->start_id = NULL;
2147 free(s->head_ref_name);
2148 s->head_ref_name = NULL;
2149 return err;
2152 static const struct got_error *
2153 search_start_log_view(struct tog_view *view)
2155 struct tog_log_view_state *s = &view->state.log;
2157 s->matched_entry = NULL;
2158 s->search_entry = NULL;
2159 return NULL;
2162 static const struct got_error *
2163 search_next_log_view(struct tog_view *view)
2165 const struct got_error *err = NULL;
2166 struct tog_log_view_state *s = &view->state.log;
2167 struct commit_queue_entry *entry;
2169 /* Display progress update in log view. */
2170 show_log_view(view);
2171 update_panels();
2172 doupdate();
2174 if (s->search_entry) {
2175 int errcode, ch;
2176 errcode = pthread_mutex_unlock(&tog_mutex);
2177 if (errcode)
2178 return got_error_set_errno(errcode,
2179 "pthread_mutex_unlock");
2180 ch = wgetch(view->window);
2181 errcode = pthread_mutex_lock(&tog_mutex);
2182 if (errcode)
2183 return got_error_set_errno(errcode,
2184 "pthread_mutex_lock");
2185 if (ch == KEY_BACKSPACE) {
2186 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2187 return NULL;
2189 if (view->searching == TOG_SEARCH_FORWARD)
2190 entry = TAILQ_NEXT(s->search_entry, entry);
2191 else
2192 entry = TAILQ_PREV(s->search_entry,
2193 commit_queue_head, entry);
2194 } else if (s->matched_entry) {
2195 if (view->searching == TOG_SEARCH_FORWARD)
2196 entry = TAILQ_NEXT(s->matched_entry, entry);
2197 else
2198 entry = TAILQ_PREV(s->matched_entry,
2199 commit_queue_head, entry);
2200 } else {
2201 if (view->searching == TOG_SEARCH_FORWARD)
2202 entry = TAILQ_FIRST(&s->commits.head);
2203 else
2204 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2207 while (1) {
2208 int have_match = 0;
2210 if (entry == NULL) {
2211 if (s->thread_args.log_complete ||
2212 view->searching == TOG_SEARCH_BACKWARD) {
2213 view->search_next_done =
2214 (s->matched_entry == NULL ?
2215 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2216 s->search_entry = NULL;
2217 return NULL;
2220 * Poke the log thread for more commits and return,
2221 * allowing the main loop to make progress. Search
2222 * will resume at s->search_entry once we come back.
2224 s->thread_args.commits_needed++;
2225 return trigger_log_thread(view, 0);
2228 err = match_commit(&have_match, entry->id, entry->commit,
2229 &view->regex);
2230 if (err)
2231 break;
2232 if (have_match) {
2233 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2234 s->matched_entry = entry;
2235 break;
2238 s->search_entry = entry;
2239 if (view->searching == TOG_SEARCH_FORWARD)
2240 entry = TAILQ_NEXT(entry, entry);
2241 else
2242 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2245 if (s->matched_entry) {
2246 int cur = s->selected_entry->idx;
2247 while (cur < s->matched_entry->idx) {
2248 err = input_log_view(NULL, view, KEY_DOWN);
2249 if (err)
2250 return err;
2251 cur++;
2253 while (cur > s->matched_entry->idx) {
2254 err = input_log_view(NULL, view, KEY_UP);
2255 if (err)
2256 return err;
2257 cur--;
2261 s->search_entry = NULL;
2263 return NULL;
2266 static const struct got_error *
2267 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2268 struct got_repository *repo, const char *head_ref_name,
2269 const char *in_repo_path, int log_branches)
2271 const struct got_error *err = NULL;
2272 struct tog_log_view_state *s = &view->state.log;
2273 struct got_repository *thread_repo = NULL;
2274 struct got_commit_graph *thread_graph = NULL;
2275 int errcode;
2277 if (in_repo_path != s->in_repo_path) {
2278 free(s->in_repo_path);
2279 s->in_repo_path = strdup(in_repo_path);
2280 if (s->in_repo_path == NULL)
2281 return got_error_from_errno("strdup");
2284 /* The commit queue only contains commits being displayed. */
2285 TAILQ_INIT(&s->commits.head);
2286 s->commits.ncommits = 0;
2288 s->repo = repo;
2289 if (head_ref_name) {
2290 s->head_ref_name = strdup(head_ref_name);
2291 if (s->head_ref_name == NULL) {
2292 err = got_error_from_errno("strdup");
2293 goto done;
2296 s->start_id = got_object_id_dup(start_id);
2297 if (s->start_id == NULL) {
2298 err = got_error_from_errno("got_object_id_dup");
2299 goto done;
2301 s->log_branches = log_branches;
2303 SIMPLEQ_INIT(&s->colors);
2304 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2305 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2306 get_color_value("TOG_COLOR_COMMIT"));
2307 if (err)
2308 goto done;
2309 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2310 get_color_value("TOG_COLOR_AUTHOR"));
2311 if (err) {
2312 free_colors(&s->colors);
2313 goto done;
2315 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2316 get_color_value("TOG_COLOR_DATE"));
2317 if (err) {
2318 free_colors(&s->colors);
2319 goto done;
2323 view->show = show_log_view;
2324 view->input = input_log_view;
2325 view->close = close_log_view;
2326 view->search_start = search_start_log_view;
2327 view->search_next = search_next_log_view;
2329 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2330 if (err)
2331 goto done;
2332 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2333 !s->log_branches);
2334 if (err)
2335 goto done;
2336 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2337 s->repo, NULL, NULL);
2338 if (err)
2339 goto done;
2341 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2342 if (errcode) {
2343 err = got_error_set_errno(errcode, "pthread_cond_init");
2344 goto done;
2346 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2347 if (errcode) {
2348 err = got_error_set_errno(errcode, "pthread_cond_init");
2349 goto done;
2352 s->thread_args.commits_needed = view->nlines;
2353 s->thread_args.graph = thread_graph;
2354 s->thread_args.commits = &s->commits;
2355 s->thread_args.in_repo_path = s->in_repo_path;
2356 s->thread_args.start_id = s->start_id;
2357 s->thread_args.repo = thread_repo;
2358 s->thread_args.log_complete = 0;
2359 s->thread_args.quit = &s->quit;
2360 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2361 s->thread_args.selected_entry = &s->selected_entry;
2362 s->thread_args.searching = &view->searching;
2363 s->thread_args.search_next_done = &view->search_next_done;
2364 s->thread_args.regex = &view->regex;
2365 done:
2366 if (err)
2367 close_log_view(view);
2368 return err;
2371 static const struct got_error *
2372 show_log_view(struct tog_view *view)
2374 const struct got_error *err;
2375 struct tog_log_view_state *s = &view->state.log;
2377 if (s->thread == NULL) {
2378 int errcode = pthread_create(&s->thread, NULL, log_thread,
2379 &s->thread_args);
2380 if (errcode)
2381 return got_error_set_errno(errcode, "pthread_create");
2382 if (s->thread_args.commits_needed > 0) {
2383 err = trigger_log_thread(view, 1);
2384 if (err)
2385 return err;
2389 return draw_commits(view);
2392 static const struct got_error *
2393 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2395 const struct got_error *err = NULL;
2396 struct tog_log_view_state *s = &view->state.log;
2397 struct tog_view *diff_view = NULL, *tree_view = NULL;
2398 struct tog_view *ref_view = NULL;
2399 int begin_x = 0;
2401 switch (ch) {
2402 case 'q':
2403 s->quit = 1;
2404 break;
2405 case 'k':
2406 case KEY_UP:
2407 case '<':
2408 case ',':
2409 if (s->first_displayed_entry == NULL)
2410 break;
2411 if (s->selected > 0)
2412 s->selected--;
2413 else
2414 log_scroll_up(s, 1);
2415 select_commit(s);
2416 break;
2417 case KEY_PPAGE:
2418 case CTRL('b'):
2419 if (s->first_displayed_entry == NULL)
2420 break;
2421 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2422 s->selected = 0;
2423 else
2424 log_scroll_up(s, view->nlines - 1);
2425 select_commit(s);
2426 break;
2427 case 'j':
2428 case KEY_DOWN:
2429 case '>':
2430 case '.':
2431 if (s->first_displayed_entry == NULL)
2432 break;
2433 if (s->selected < MIN(view->nlines - 2,
2434 s->commits.ncommits - 1))
2435 s->selected++;
2436 else {
2437 err = log_scroll_down(view, 1);
2438 if (err)
2439 break;
2441 select_commit(s);
2442 break;
2443 case KEY_NPAGE:
2444 case CTRL('f'): {
2445 struct commit_queue_entry *first;
2446 first = s->first_displayed_entry;
2447 if (first == NULL)
2448 break;
2449 err = log_scroll_down(view, view->nlines - 1);
2450 if (err)
2451 break;
2452 if (first == s->first_displayed_entry &&
2453 s->selected < MIN(view->nlines - 2,
2454 s->commits.ncommits - 1)) {
2455 /* can't scroll further down */
2456 s->selected = MIN(view->nlines - 2,
2457 s->commits.ncommits - 1);
2459 select_commit(s);
2460 break;
2462 case KEY_RESIZE:
2463 if (s->selected > view->nlines - 2)
2464 s->selected = view->nlines - 2;
2465 if (s->selected > s->commits.ncommits - 1)
2466 s->selected = s->commits.ncommits - 1;
2467 select_commit(s);
2468 if (s->commits.ncommits < view->nlines - 1 &&
2469 !s->thread_args.log_complete) {
2470 s->thread_args.commits_needed += (view->nlines - 1) -
2471 s->commits.ncommits;
2472 err = trigger_log_thread(view, 1);
2474 break;
2475 case KEY_ENTER:
2476 case ' ':
2477 case '\r':
2478 if (s->selected_entry == NULL)
2479 break;
2480 if (view_is_parent_view(view))
2481 begin_x = view_split_begin_x(view->begin_x);
2482 err = open_diff_view_for_commit(&diff_view, begin_x,
2483 s->selected_entry->commit, s->selected_entry->id,
2484 view, s->repo);
2485 if (err)
2486 break;
2487 view->focussed = 0;
2488 diff_view->focussed = 1;
2489 if (view_is_parent_view(view)) {
2490 err = view_close_child(view);
2491 if (err)
2492 return err;
2493 view_set_child(view, diff_view);
2494 view->focus_child = 1;
2495 } else
2496 *new_view = diff_view;
2497 break;
2498 case 't':
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 = browse_commit_tree(&tree_view, begin_x,
2504 s->selected_entry, s->in_repo_path, s->head_ref_name,
2505 s->repo);
2506 if (err)
2507 break;
2508 view->focussed = 0;
2509 tree_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, tree_view);
2515 view->focus_child = 1;
2516 } else
2517 *new_view = tree_view;
2518 break;
2519 case KEY_BACKSPACE:
2520 case CTRL('l'):
2521 case 'B':
2522 if (ch == KEY_BACKSPACE &&
2523 got_path_is_root_dir(s->in_repo_path))
2524 break;
2525 err = stop_log_thread(s);
2526 if (err)
2527 return err;
2528 if (ch == KEY_BACKSPACE) {
2529 char *parent_path;
2530 err = got_path_dirname(&parent_path, s->in_repo_path);
2531 if (err)
2532 return err;
2533 free(s->in_repo_path);
2534 s->in_repo_path = parent_path;
2535 s->thread_args.in_repo_path = s->in_repo_path;
2536 } else if (ch == CTRL('l')) {
2537 struct got_object_id *start_id;
2538 err = got_repo_match_object_id(&start_id, NULL,
2539 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2540 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2541 if (err)
2542 return err;
2543 free(s->start_id);
2544 s->start_id = start_id;
2545 s->thread_args.start_id = s->start_id;
2546 } else /* 'B' */
2547 s->log_branches = !s->log_branches;
2549 err = got_repo_open(&s->thread_args.repo,
2550 got_repo_get_path(s->repo), NULL);
2551 if (err)
2552 return err;
2553 tog_free_refs();
2554 err = tog_load_refs(s->repo);
2555 if (err)
2556 return err;
2557 err = got_commit_graph_open(&s->thread_args.graph,
2558 s->in_repo_path, !s->log_branches);
2559 if (err)
2560 return err;
2561 err = got_commit_graph_iter_start(s->thread_args.graph,
2562 s->start_id, s->repo, NULL, NULL);
2563 if (err)
2564 return err;
2565 free_commits(&s->commits);
2566 s->first_displayed_entry = NULL;
2567 s->last_displayed_entry = NULL;
2568 s->selected_entry = NULL;
2569 s->selected = 0;
2570 s->thread_args.log_complete = 0;
2571 s->quit = 0;
2572 s->thread_args.commits_needed = view->nlines;
2573 break;
2574 case 'r':
2575 if (view_is_parent_view(view))
2576 begin_x = view_split_begin_x(view->begin_x);
2577 ref_view = view_open(view->nlines, view->ncols,
2578 view->begin_y, begin_x, TOG_VIEW_REF);
2579 if (ref_view == NULL)
2580 return got_error_from_errno("view_open");
2581 err = open_ref_view(ref_view, s->repo);
2582 if (err) {
2583 view_close(ref_view);
2584 return err;
2586 view->focussed = 0;
2587 ref_view->focussed = 1;
2588 if (view_is_parent_view(view)) {
2589 err = view_close_child(view);
2590 if (err)
2591 return err;
2592 view_set_child(view, ref_view);
2593 view->focus_child = 1;
2594 } else
2595 *new_view = ref_view;
2596 break;
2597 default:
2598 break;
2601 return err;
2604 static const struct got_error *
2605 apply_unveil(const char *repo_path, const char *worktree_path)
2607 const struct got_error *error;
2609 #ifdef PROFILE
2610 if (unveil("gmon.out", "rwc") != 0)
2611 return got_error_from_errno2("unveil", "gmon.out");
2612 #endif
2613 if (repo_path && unveil(repo_path, "r") != 0)
2614 return got_error_from_errno2("unveil", repo_path);
2616 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2617 return got_error_from_errno2("unveil", worktree_path);
2619 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2620 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2622 error = got_privsep_unveil_exec_helpers();
2623 if (error != NULL)
2624 return error;
2626 if (unveil(NULL, NULL) != 0)
2627 return got_error_from_errno("unveil");
2629 return NULL;
2632 static void
2633 init_curses(void)
2635 initscr();
2636 cbreak();
2637 halfdelay(1); /* Do fast refresh while initial view is loading. */
2638 noecho();
2639 nonl();
2640 intrflush(stdscr, FALSE);
2641 keypad(stdscr, TRUE);
2642 curs_set(0);
2643 if (getenv("TOG_COLORS") != NULL) {
2644 start_color();
2645 use_default_colors();
2647 signal(SIGWINCH, tog_sigwinch);
2648 signal(SIGPIPE, tog_sigpipe);
2649 signal(SIGCONT, tog_sigcont);
2652 static const struct got_error *
2653 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2654 struct got_repository *repo, struct got_worktree *worktree)
2656 const struct got_error *err = NULL;
2658 if (argc == 0) {
2659 *in_repo_path = strdup("/");
2660 if (*in_repo_path == NULL)
2661 return got_error_from_errno("strdup");
2662 return NULL;
2665 if (worktree) {
2666 const char *prefix = got_worktree_get_path_prefix(worktree);
2667 char *p;
2669 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2670 if (err)
2671 return err;
2672 if (asprintf(in_repo_path, "%s%s%s", prefix,
2673 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2674 p) == -1) {
2675 err = got_error_from_errno("asprintf");
2676 *in_repo_path = NULL;
2678 free(p);
2679 } else
2680 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2682 return err;
2685 static const struct got_error *
2686 cmd_log(int argc, char *argv[])
2688 const struct got_error *error;
2689 struct got_repository *repo = NULL;
2690 struct got_worktree *worktree = NULL;
2691 struct got_object_id *start_id = NULL;
2692 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2693 char *start_commit = NULL, *label = NULL;
2694 struct got_reference *ref = NULL;
2695 const char *head_ref_name = NULL;
2696 int ch, log_branches = 0;
2697 struct tog_view *view;
2699 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2700 switch (ch) {
2701 case 'b':
2702 log_branches = 1;
2703 break;
2704 case 'c':
2705 start_commit = optarg;
2706 break;
2707 case 'r':
2708 repo_path = realpath(optarg, NULL);
2709 if (repo_path == NULL)
2710 return got_error_from_errno2("realpath",
2711 optarg);
2712 break;
2713 default:
2714 usage_log();
2715 /* NOTREACHED */
2719 argc -= optind;
2720 argv += optind;
2722 if (argc > 1)
2723 usage_log();
2725 if (repo_path == NULL) {
2726 cwd = getcwd(NULL, 0);
2727 if (cwd == NULL)
2728 return got_error_from_errno("getcwd");
2729 error = got_worktree_open(&worktree, cwd);
2730 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2731 goto done;
2732 if (worktree)
2733 repo_path =
2734 strdup(got_worktree_get_repo_path(worktree));
2735 else
2736 repo_path = strdup(cwd);
2737 if (repo_path == NULL) {
2738 error = got_error_from_errno("strdup");
2739 goto done;
2743 error = got_repo_open(&repo, repo_path, NULL);
2744 if (error != NULL)
2745 goto done;
2747 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2748 repo, worktree);
2749 if (error)
2750 goto done;
2752 init_curses();
2754 error = apply_unveil(got_repo_get_path(repo),
2755 worktree ? got_worktree_get_root_path(worktree) : NULL);
2756 if (error)
2757 goto done;
2759 /* already loaded by tog_log_with_path()? */
2760 if (TAILQ_EMPTY(&tog_refs)) {
2761 error = tog_load_refs(repo);
2762 if (error)
2763 goto done;
2766 if (start_commit == NULL) {
2767 error = got_repo_match_object_id(&start_id, &label,
2768 worktree ? got_worktree_get_head_ref_name(worktree) :
2769 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2770 if (error)
2771 goto done;
2772 head_ref_name = label;
2773 } else {
2774 error = got_ref_open(&ref, repo, start_commit, 0);
2775 if (error == NULL)
2776 head_ref_name = got_ref_get_name(ref);
2777 else if (error->code != GOT_ERR_NOT_REF)
2778 goto done;
2779 error = got_repo_match_object_id(&start_id, NULL,
2780 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2781 if (error)
2782 goto done;
2785 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2786 if (view == NULL) {
2787 error = got_error_from_errno("view_open");
2788 goto done;
2790 error = open_log_view(view, start_id, repo, head_ref_name,
2791 in_repo_path, log_branches);
2792 if (error)
2793 goto done;
2794 if (worktree) {
2795 /* Release work tree lock. */
2796 got_worktree_close(worktree);
2797 worktree = NULL;
2799 error = view_loop(view);
2800 done:
2801 free(in_repo_path);
2802 free(repo_path);
2803 free(cwd);
2804 free(start_id);
2805 free(label);
2806 if (ref)
2807 got_ref_close(ref);
2808 if (repo)
2809 got_repo_close(repo);
2810 if (worktree)
2811 got_worktree_close(worktree);
2812 tog_free_refs();
2813 return error;
2816 __dead static void
2817 usage_diff(void)
2819 endwin();
2820 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2821 "[-w] object1 object2\n", getprogname());
2822 exit(1);
2825 static int
2826 match_line(const char *line, regex_t *regex, size_t nmatch,
2827 regmatch_t *regmatch)
2829 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2832 struct tog_color *
2833 match_color(struct tog_colors *colors, const char *line)
2835 struct tog_color *tc = NULL;
2837 SIMPLEQ_FOREACH(tc, colors, entry) {
2838 if (match_line(line, &tc->regex, 0, NULL))
2839 return tc;
2842 return NULL;
2845 static const struct got_error *
2846 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2847 WINDOW *window, regmatch_t *regmatch)
2849 const struct got_error *err = NULL;
2850 wchar_t *wline;
2851 int width;
2852 char *s;
2854 *wtotal = 0;
2856 s = strndup(line, regmatch->rm_so);
2857 if (s == NULL)
2858 return got_error_from_errno("strndup");
2860 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2861 if (err) {
2862 free(s);
2863 return err;
2865 waddwstr(window, wline);
2866 free(wline);
2867 free(s);
2868 wlimit -= width;
2869 *wtotal += width;
2871 if (wlimit > 0) {
2872 s = strndup(line + regmatch->rm_so,
2873 regmatch->rm_eo - regmatch->rm_so);
2874 if (s == NULL) {
2875 err = got_error_from_errno("strndup");
2876 free(s);
2877 return err;
2879 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2880 if (err) {
2881 free(s);
2882 return err;
2884 wattr_on(window, A_STANDOUT, NULL);
2885 waddwstr(window, wline);
2886 wattr_off(window, A_STANDOUT, NULL);
2887 free(wline);
2888 free(s);
2889 wlimit -= width;
2890 *wtotal += width;
2893 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2894 err = format_line(&wline, &width,
2895 line + regmatch->rm_eo, wlimit, col_tab_align);
2896 if (err)
2897 return err;
2898 waddwstr(window, wline);
2899 free(wline);
2900 *wtotal += width;
2903 return NULL;
2906 static const struct got_error *
2907 draw_file(struct tog_view *view, const char *header)
2909 struct tog_diff_view_state *s = &view->state.diff;
2910 regmatch_t *regmatch = &view->regmatch;
2911 const struct got_error *err;
2912 int nprinted = 0;
2913 char *line;
2914 size_t linesize = 0;
2915 ssize_t linelen;
2916 struct tog_color *tc;
2917 wchar_t *wline;
2918 int width;
2919 int max_lines = view->nlines;
2920 int nlines = s->nlines;
2921 off_t line_offset;
2923 line_offset = s->line_offsets[s->first_displayed_line - 1];
2924 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2925 return got_error_from_errno("fseek");
2927 werase(view->window);
2929 if (header) {
2930 if (asprintf(&line, "[%d/%d] %s",
2931 s->first_displayed_line - 1 + s->selected_line, nlines,
2932 header) == -1)
2933 return got_error_from_errno("asprintf");
2934 err = format_line(&wline, &width, line, view->ncols, 0);
2935 free(line);
2936 if (err)
2937 return err;
2939 if (view_needs_focus_indication(view))
2940 wstandout(view->window);
2941 waddwstr(view->window, wline);
2942 free(wline);
2943 wline = NULL;
2944 if (view_needs_focus_indication(view))
2945 wstandend(view->window);
2946 if (width <= view->ncols - 1)
2947 waddch(view->window, '\n');
2949 if (max_lines <= 1)
2950 return NULL;
2951 max_lines--;
2954 s->eof = 0;
2955 line = NULL;
2956 while (max_lines > 0 && nprinted < max_lines) {
2957 linelen = getline(&line, &linesize, s->f);
2958 if (linelen == -1) {
2959 if (feof(s->f)) {
2960 s->eof = 1;
2961 break;
2963 free(line);
2964 return got_ferror(s->f, GOT_ERR_IO);
2967 tc = match_color(&s->colors, line);
2968 if (tc)
2969 wattr_on(view->window,
2970 COLOR_PAIR(tc->colorpair), NULL);
2971 if (s->first_displayed_line + nprinted == s->matched_line &&
2972 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2973 err = add_matched_line(&width, line, view->ncols, 0,
2974 view->window, regmatch);
2975 if (err) {
2976 free(line);
2977 return err;
2979 } else {
2980 err = format_line(&wline, &width, line, view->ncols, 0);
2981 if (err) {
2982 free(line);
2983 return err;
2985 waddwstr(view->window, wline);
2986 free(wline);
2987 wline = NULL;
2989 if (tc)
2990 wattr_off(view->window,
2991 COLOR_PAIR(tc->colorpair), NULL);
2992 if (width <= view->ncols - 1)
2993 waddch(view->window, '\n');
2994 nprinted++;
2996 free(line);
2997 if (nprinted >= 1)
2998 s->last_displayed_line = s->first_displayed_line +
2999 (nprinted - 1);
3000 else
3001 s->last_displayed_line = s->first_displayed_line;
3003 view_vborder(view);
3005 if (s->eof) {
3006 while (nprinted < view->nlines) {
3007 waddch(view->window, '\n');
3008 nprinted++;
3011 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3012 if (err) {
3013 return err;
3016 wstandout(view->window);
3017 waddwstr(view->window, wline);
3018 free(wline);
3019 wline = NULL;
3020 wstandend(view->window);
3023 return NULL;
3026 static char *
3027 get_datestr(time_t *time, char *datebuf)
3029 struct tm mytm, *tm;
3030 char *p, *s;
3032 tm = gmtime_r(time, &mytm);
3033 if (tm == NULL)
3034 return NULL;
3035 s = asctime_r(tm, datebuf);
3036 if (s == NULL)
3037 return NULL;
3038 p = strchr(s, '\n');
3039 if (p)
3040 *p = '\0';
3041 return s;
3044 static const struct got_error *
3045 get_changed_paths(struct got_pathlist_head *paths,
3046 struct got_commit_object *commit, struct got_repository *repo)
3048 const struct got_error *err = NULL;
3049 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3050 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3051 struct got_object_qid *qid;
3053 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3054 if (qid != NULL) {
3055 struct got_commit_object *pcommit;
3056 err = got_object_open_as_commit(&pcommit, repo,
3057 qid->id);
3058 if (err)
3059 return err;
3061 tree_id1 = got_object_commit_get_tree_id(pcommit);
3062 got_object_commit_close(pcommit);
3066 if (tree_id1) {
3067 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3068 if (err)
3069 goto done;
3072 tree_id2 = got_object_commit_get_tree_id(commit);
3073 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3074 if (err)
3075 goto done;
3077 err = got_diff_tree(tree1, tree2, "", "", repo,
3078 got_diff_tree_collect_changed_paths, paths, 0);
3079 done:
3080 if (tree1)
3081 got_object_tree_close(tree1);
3082 if (tree2)
3083 got_object_tree_close(tree2);
3084 return err;
3087 static const struct got_error *
3088 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3090 off_t *p;
3092 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3093 if (p == NULL)
3094 return got_error_from_errno("reallocarray");
3095 *line_offsets = p;
3096 (*line_offsets)[*nlines] = off;
3097 (*nlines)++;
3098 return NULL;
3101 static const struct got_error *
3102 write_commit_info(off_t **line_offsets, size_t *nlines,
3103 struct got_object_id *commit_id, struct got_reflist_head *refs,
3104 struct got_repository *repo, FILE *outfile)
3106 const struct got_error *err = NULL;
3107 char datebuf[26], *datestr;
3108 struct got_commit_object *commit;
3109 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3110 time_t committer_time;
3111 const char *author, *committer;
3112 char *refs_str = NULL;
3113 struct got_pathlist_head changed_paths;
3114 struct got_pathlist_entry *pe;
3115 off_t outoff = 0;
3116 int n;
3118 TAILQ_INIT(&changed_paths);
3120 if (refs) {
3121 err = build_refs_str(&refs_str, refs, commit_id, repo);
3122 if (err)
3123 return err;
3126 err = got_object_open_as_commit(&commit, repo, commit_id);
3127 if (err)
3128 return err;
3130 err = got_object_id_str(&id_str, commit_id);
3131 if (err) {
3132 err = got_error_from_errno("got_object_id_str");
3133 goto done;
3136 err = add_line_offset(line_offsets, nlines, 0);
3137 if (err)
3138 goto done;
3140 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3141 refs_str ? refs_str : "", refs_str ? ")" : "");
3142 if (n < 0) {
3143 err = got_error_from_errno("fprintf");
3144 goto done;
3146 outoff += n;
3147 err = add_line_offset(line_offsets, nlines, outoff);
3148 if (err)
3149 goto done;
3151 n = fprintf(outfile, "from: %s\n",
3152 got_object_commit_get_author(commit));
3153 if (n < 0) {
3154 err = got_error_from_errno("fprintf");
3155 goto done;
3157 outoff += n;
3158 err = add_line_offset(line_offsets, nlines, outoff);
3159 if (err)
3160 goto done;
3162 committer_time = got_object_commit_get_committer_time(commit);
3163 datestr = get_datestr(&committer_time, datebuf);
3164 if (datestr) {
3165 n = fprintf(outfile, "date: %s UTC\n", datestr);
3166 if (n < 0) {
3167 err = got_error_from_errno("fprintf");
3168 goto done;
3170 outoff += n;
3171 err = add_line_offset(line_offsets, nlines, outoff);
3172 if (err)
3173 goto done;
3175 author = got_object_commit_get_author(commit);
3176 committer = got_object_commit_get_committer(commit);
3177 if (strcmp(author, committer) != 0) {
3178 n = fprintf(outfile, "via: %s\n", committer);
3179 if (n < 0) {
3180 err = got_error_from_errno("fprintf");
3181 goto done;
3183 outoff += n;
3184 err = add_line_offset(line_offsets, nlines, outoff);
3185 if (err)
3186 goto done;
3188 err = got_object_commit_get_logmsg(&logmsg, commit);
3189 if (err)
3190 goto done;
3191 s = logmsg;
3192 while ((line = strsep(&s, "\n")) != NULL) {
3193 n = fprintf(outfile, "%s\n", line);
3194 if (n < 0) {
3195 err = got_error_from_errno("fprintf");
3196 goto done;
3198 outoff += n;
3199 err = add_line_offset(line_offsets, nlines, outoff);
3200 if (err)
3201 goto done;
3204 err = get_changed_paths(&changed_paths, commit, repo);
3205 if (err)
3206 goto done;
3207 TAILQ_FOREACH(pe, &changed_paths, entry) {
3208 struct got_diff_changed_path *cp = pe->data;
3209 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3210 if (n < 0) {
3211 err = got_error_from_errno("fprintf");
3212 goto done;
3214 outoff += n;
3215 err = add_line_offset(line_offsets, nlines, outoff);
3216 if (err)
3217 goto done;
3218 free((char *)pe->path);
3219 free(pe->data);
3222 fputc('\n', outfile);
3223 outoff++;
3224 err = add_line_offset(line_offsets, nlines, outoff);
3225 done:
3226 got_pathlist_free(&changed_paths);
3227 free(id_str);
3228 free(logmsg);
3229 free(refs_str);
3230 got_object_commit_close(commit);
3231 if (err) {
3232 free(*line_offsets);
3233 *line_offsets = NULL;
3234 *nlines = 0;
3236 return err;
3239 static const struct got_error *
3240 create_diff(struct tog_diff_view_state *s)
3242 const struct got_error *err = NULL;
3243 FILE *f = NULL;
3244 int obj_type;
3246 free(s->line_offsets);
3247 s->line_offsets = malloc(sizeof(off_t));
3248 if (s->line_offsets == NULL)
3249 return got_error_from_errno("malloc");
3250 s->nlines = 0;
3252 f = got_opentemp();
3253 if (f == NULL) {
3254 err = got_error_from_errno("got_opentemp");
3255 goto done;
3257 if (s->f && fclose(s->f) != 0) {
3258 err = got_error_from_errno("fclose");
3259 goto done;
3261 s->f = f;
3263 if (s->id1)
3264 err = got_object_get_type(&obj_type, s->repo, s->id1);
3265 else
3266 err = got_object_get_type(&obj_type, s->repo, s->id2);
3267 if (err)
3268 goto done;
3270 switch (obj_type) {
3271 case GOT_OBJ_TYPE_BLOB:
3272 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3273 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3274 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3275 break;
3276 case GOT_OBJ_TYPE_TREE:
3277 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3278 s->id1, s->id2, "", "", s->diff_context,
3279 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3280 break;
3281 case GOT_OBJ_TYPE_COMMIT: {
3282 const struct got_object_id_queue *parent_ids;
3283 struct got_object_qid *pid;
3284 struct got_commit_object *commit2;
3285 struct got_reflist_head *refs;
3287 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3288 if (err)
3289 goto done;
3290 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3291 /* Show commit info if we're diffing to a parent/root commit. */
3292 if (s->id1 == NULL) {
3293 err = write_commit_info(&s->line_offsets, &s->nlines,
3294 s->id2, refs, s->repo, s->f);
3295 if (err)
3296 goto done;
3297 } else {
3298 parent_ids = got_object_commit_get_parent_ids(commit2);
3299 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3300 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3301 err = write_commit_info(
3302 &s->line_offsets, &s->nlines,
3303 s->id2, refs, s->repo, s->f);
3304 if (err)
3305 goto done;
3306 break;
3310 got_object_commit_close(commit2);
3312 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3313 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3314 s->force_text_diff, s->repo, s->f);
3315 break;
3317 default:
3318 err = got_error(GOT_ERR_OBJ_TYPE);
3319 break;
3321 if (err)
3322 goto done;
3323 done:
3324 if (s->f && fflush(s->f) != 0 && err == NULL)
3325 err = got_error_from_errno("fflush");
3326 return err;
3329 static void
3330 diff_view_indicate_progress(struct tog_view *view)
3332 mvwaddstr(view->window, 0, 0, "diffing...");
3333 update_panels();
3334 doupdate();
3337 static const struct got_error *
3338 search_start_diff_view(struct tog_view *view)
3340 struct tog_diff_view_state *s = &view->state.diff;
3342 s->matched_line = 0;
3343 return NULL;
3346 static const struct got_error *
3347 search_next_diff_view(struct tog_view *view)
3349 struct tog_diff_view_state *s = &view->state.diff;
3350 int lineno;
3351 char *line = NULL;
3352 size_t linesize = 0;
3353 ssize_t linelen;
3355 if (!view->searching) {
3356 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3357 return NULL;
3360 if (s->matched_line) {
3361 if (view->searching == TOG_SEARCH_FORWARD)
3362 lineno = s->matched_line + 1;
3363 else
3364 lineno = s->matched_line - 1;
3365 } else {
3366 if (view->searching == TOG_SEARCH_FORWARD)
3367 lineno = 1;
3368 else
3369 lineno = s->nlines;
3372 while (1) {
3373 off_t offset;
3375 if (lineno <= 0 || lineno > s->nlines) {
3376 if (s->matched_line == 0) {
3377 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3378 break;
3381 if (view->searching == TOG_SEARCH_FORWARD)
3382 lineno = 1;
3383 else
3384 lineno = s->nlines;
3387 offset = s->line_offsets[lineno - 1];
3388 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3389 free(line);
3390 return got_error_from_errno("fseeko");
3392 linelen = getline(&line, &linesize, s->f);
3393 if (linelen != -1 &&
3394 match_line(line, &view->regex, 1, &view->regmatch)) {
3395 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3396 s->matched_line = lineno;
3397 break;
3399 if (view->searching == TOG_SEARCH_FORWARD)
3400 lineno++;
3401 else
3402 lineno--;
3404 free(line);
3406 if (s->matched_line) {
3407 s->first_displayed_line = s->matched_line;
3408 s->selected_line = 1;
3411 return NULL;
3414 static const struct got_error *
3415 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3416 struct got_object_id *id2, const char *label1, const char *label2,
3417 int diff_context, int ignore_whitespace, int force_text_diff,
3418 struct tog_view *log_view, struct got_repository *repo)
3420 const struct got_error *err;
3421 struct tog_diff_view_state *s = &view->state.diff;
3423 if (id1 != NULL && id2 != NULL) {
3424 int type1, type2;
3425 err = got_object_get_type(&type1, repo, id1);
3426 if (err)
3427 return err;
3428 err = got_object_get_type(&type2, repo, id2);
3429 if (err)
3430 return err;
3432 if (type1 != type2)
3433 return got_error(GOT_ERR_OBJ_TYPE);
3435 s->first_displayed_line = 1;
3436 s->last_displayed_line = view->nlines;
3437 s->selected_line = 1;
3438 s->repo = repo;
3439 s->id1 = id1;
3440 s->id2 = id2;
3441 s->label1 = label1;
3442 s->label2 = label2;
3444 if (id1) {
3445 s->id1 = got_object_id_dup(id1);
3446 if (s->id1 == NULL)
3447 return got_error_from_errno("got_object_id_dup");
3448 } else
3449 s->id1 = NULL;
3451 s->id2 = got_object_id_dup(id2);
3452 if (s->id2 == NULL) {
3453 free(s->id1);
3454 s->id1 = NULL;
3455 return got_error_from_errno("got_object_id_dup");
3457 s->f = NULL;
3458 s->first_displayed_line = 1;
3459 s->last_displayed_line = view->nlines;
3460 s->diff_context = diff_context;
3461 s->ignore_whitespace = ignore_whitespace;
3462 s->force_text_diff = force_text_diff;
3463 s->log_view = log_view;
3464 s->repo = repo;
3466 SIMPLEQ_INIT(&s->colors);
3467 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3468 err = add_color(&s->colors,
3469 "^-", TOG_COLOR_DIFF_MINUS,
3470 get_color_value("TOG_COLOR_DIFF_MINUS"));
3471 if (err)
3472 return err;
3473 err = add_color(&s->colors, "^\\+",
3474 TOG_COLOR_DIFF_PLUS,
3475 get_color_value("TOG_COLOR_DIFF_PLUS"));
3476 if (err) {
3477 free_colors(&s->colors);
3478 return err;
3480 err = add_color(&s->colors,
3481 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3482 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3483 if (err) {
3484 free_colors(&s->colors);
3485 return err;
3488 err = add_color(&s->colors,
3489 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3490 TOG_COLOR_DIFF_META,
3491 get_color_value("TOG_COLOR_DIFF_META"));
3492 if (err) {
3493 free_colors(&s->colors);
3494 return err;
3497 err = add_color(&s->colors,
3498 "^(from|via): ", TOG_COLOR_AUTHOR,
3499 get_color_value("TOG_COLOR_AUTHOR"));
3500 if (err) {
3501 free_colors(&s->colors);
3502 return err;
3505 err = add_color(&s->colors,
3506 "^date: ", TOG_COLOR_DATE,
3507 get_color_value("TOG_COLOR_DATE"));
3508 if (err) {
3509 free_colors(&s->colors);
3510 return err;
3514 if (log_view && view_is_splitscreen(view))
3515 show_log_view(log_view); /* draw vborder */
3516 diff_view_indicate_progress(view);
3518 s->line_offsets = NULL;
3519 s->nlines = 0;
3520 err = create_diff(s);
3521 if (err) {
3522 free(s->id1);
3523 s->id1 = NULL;
3524 free(s->id2);
3525 s->id2 = NULL;
3526 free_colors(&s->colors);
3527 return err;
3530 view->show = show_diff_view;
3531 view->input = input_diff_view;
3532 view->close = close_diff_view;
3533 view->search_start = search_start_diff_view;
3534 view->search_next = search_next_diff_view;
3536 return NULL;
3539 static const struct got_error *
3540 close_diff_view(struct tog_view *view)
3542 const struct got_error *err = NULL;
3543 struct tog_diff_view_state *s = &view->state.diff;
3545 free(s->id1);
3546 s->id1 = NULL;
3547 free(s->id2);
3548 s->id2 = NULL;
3549 if (s->f && fclose(s->f) == EOF)
3550 err = got_error_from_errno("fclose");
3551 free_colors(&s->colors);
3552 free(s->line_offsets);
3553 s->line_offsets = NULL;
3554 s->nlines = 0;
3555 return err;
3558 static const struct got_error *
3559 show_diff_view(struct tog_view *view)
3561 const struct got_error *err;
3562 struct tog_diff_view_state *s = &view->state.diff;
3563 char *id_str1 = NULL, *id_str2, *header;
3564 const char *label1, *label2;
3566 if (s->id1) {
3567 err = got_object_id_str(&id_str1, s->id1);
3568 if (err)
3569 return err;
3570 label1 = s->label1 ? : id_str1;
3571 } else
3572 label1 = "/dev/null";
3574 err = got_object_id_str(&id_str2, s->id2);
3575 if (err)
3576 return err;
3577 label2 = s->label2 ? : id_str2;
3579 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3580 err = got_error_from_errno("asprintf");
3581 free(id_str1);
3582 free(id_str2);
3583 return err;
3585 free(id_str1);
3586 free(id_str2);
3588 return draw_file(view, header);
3591 static const struct got_error *
3592 set_selected_commit(struct tog_diff_view_state *s,
3593 struct commit_queue_entry *entry)
3595 const struct got_error *err;
3596 const struct got_object_id_queue *parent_ids;
3597 struct got_commit_object *selected_commit;
3598 struct got_object_qid *pid;
3600 free(s->id2);
3601 s->id2 = got_object_id_dup(entry->id);
3602 if (s->id2 == NULL)
3603 return got_error_from_errno("got_object_id_dup");
3605 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3606 if (err)
3607 return err;
3608 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3609 free(s->id1);
3610 pid = SIMPLEQ_FIRST(parent_ids);
3611 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3612 got_object_commit_close(selected_commit);
3613 return NULL;
3616 static const struct got_error *
3617 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3619 const struct got_error *err = NULL;
3620 struct tog_diff_view_state *s = &view->state.diff;
3621 struct tog_log_view_state *ls;
3622 struct commit_queue_entry *old_selected_entry;
3623 char *line = NULL;
3624 size_t linesize = 0;
3625 ssize_t linelen;
3626 int i;
3628 switch (ch) {
3629 case 'a':
3630 case 'w':
3631 if (ch == 'a')
3632 s->force_text_diff = !s->force_text_diff;
3633 if (ch == 'w')
3634 s->ignore_whitespace = !s->ignore_whitespace;
3635 wclear(view->window);
3636 s->first_displayed_line = 1;
3637 s->last_displayed_line = view->nlines;
3638 diff_view_indicate_progress(view);
3639 err = create_diff(s);
3640 break;
3641 case 'k':
3642 case KEY_UP:
3643 if (s->first_displayed_line > 1)
3644 s->first_displayed_line--;
3645 break;
3646 case KEY_PPAGE:
3647 case CTRL('b'):
3648 if (s->first_displayed_line == 1)
3649 break;
3650 i = 0;
3651 while (i++ < view->nlines - 1 &&
3652 s->first_displayed_line > 1)
3653 s->first_displayed_line--;
3654 break;
3655 case 'j':
3656 case KEY_DOWN:
3657 if (!s->eof)
3658 s->first_displayed_line++;
3659 break;
3660 case KEY_NPAGE:
3661 case CTRL('f'):
3662 case ' ':
3663 if (s->eof)
3664 break;
3665 i = 0;
3666 while (!s->eof && i++ < view->nlines - 1) {
3667 linelen = getline(&line, &linesize, s->f);
3668 s->first_displayed_line++;
3669 if (linelen == -1) {
3670 if (feof(s->f)) {
3671 s->eof = 1;
3672 } else
3673 err = got_ferror(s->f, GOT_ERR_IO);
3674 break;
3677 free(line);
3678 break;
3679 case '[':
3680 if (s->diff_context > 0) {
3681 s->diff_context--;
3682 diff_view_indicate_progress(view);
3683 err = create_diff(s);
3684 if (s->first_displayed_line + view->nlines - 1 >
3685 s->nlines) {
3686 s->first_displayed_line = 1;
3687 s->last_displayed_line = view->nlines;
3690 break;
3691 case ']':
3692 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3693 s->diff_context++;
3694 diff_view_indicate_progress(view);
3695 err = create_diff(s);
3697 break;
3698 case '<':
3699 case ',':
3700 if (s->log_view == NULL)
3701 break;
3702 ls = &s->log_view->state.log;
3703 old_selected_entry = ls->selected_entry;
3705 err = input_log_view(NULL, s->log_view, KEY_UP);
3706 if (err)
3707 break;
3709 if (old_selected_entry == ls->selected_entry)
3710 break;
3712 err = set_selected_commit(s, ls->selected_entry);
3713 if (err)
3714 break;
3716 s->first_displayed_line = 1;
3717 s->last_displayed_line = view->nlines;
3719 diff_view_indicate_progress(view);
3720 err = create_diff(s);
3721 break;
3722 case '>':
3723 case '.':
3724 if (s->log_view == NULL)
3725 break;
3726 ls = &s->log_view->state.log;
3727 old_selected_entry = ls->selected_entry;
3729 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3730 if (err)
3731 break;
3733 if (old_selected_entry == ls->selected_entry)
3734 break;
3736 err = set_selected_commit(s, ls->selected_entry);
3737 if (err)
3738 break;
3740 s->first_displayed_line = 1;
3741 s->last_displayed_line = view->nlines;
3743 diff_view_indicate_progress(view);
3744 err = create_diff(s);
3745 break;
3746 default:
3747 break;
3750 return err;
3753 static const struct got_error *
3754 cmd_diff(int argc, char *argv[])
3756 const struct got_error *error = NULL;
3757 struct got_repository *repo = NULL;
3758 struct got_worktree *worktree = NULL;
3759 struct got_object_id *id1 = NULL, *id2 = NULL;
3760 char *repo_path = NULL, *cwd = NULL;
3761 char *id_str1 = NULL, *id_str2 = NULL;
3762 char *label1 = NULL, *label2 = NULL;
3763 int diff_context = 3, ignore_whitespace = 0;
3764 int ch, force_text_diff = 0;
3765 const char *errstr;
3766 struct tog_view *view;
3768 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3769 switch (ch) {
3770 case 'a':
3771 force_text_diff = 1;
3772 break;
3773 case 'C':
3774 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3775 &errstr);
3776 if (errstr != NULL)
3777 err(1, "-C option %s", errstr);
3778 break;
3779 case 'r':
3780 repo_path = realpath(optarg, NULL);
3781 if (repo_path == NULL)
3782 return got_error_from_errno2("realpath",
3783 optarg);
3784 got_path_strip_trailing_slashes(repo_path);
3785 break;
3786 case 'w':
3787 ignore_whitespace = 1;
3788 break;
3789 default:
3790 usage_diff();
3791 /* NOTREACHED */
3795 argc -= optind;
3796 argv += optind;
3798 if (argc == 0) {
3799 usage_diff(); /* TODO show local worktree changes */
3800 } else if (argc == 2) {
3801 id_str1 = argv[0];
3802 id_str2 = argv[1];
3803 } else
3804 usage_diff();
3806 if (repo_path == NULL) {
3807 cwd = getcwd(NULL, 0);
3808 if (cwd == NULL)
3809 return got_error_from_errno("getcwd");
3810 error = got_worktree_open(&worktree, cwd);
3811 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3812 goto done;
3813 if (worktree)
3814 repo_path =
3815 strdup(got_worktree_get_repo_path(worktree));
3816 else
3817 repo_path = strdup(cwd);
3818 if (repo_path == NULL) {
3819 error = got_error_from_errno("strdup");
3820 goto done;
3824 error = got_repo_open(&repo, repo_path, NULL);
3825 if (error)
3826 goto done;
3828 init_curses();
3830 error = apply_unveil(got_repo_get_path(repo), NULL);
3831 if (error)
3832 goto done;
3834 error = tog_load_refs(repo);
3835 if (error)
3836 goto done;
3838 error = got_repo_match_object_id(&id1, &label1, id_str1,
3839 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3840 if (error)
3841 goto done;
3843 error = got_repo_match_object_id(&id2, &label2, id_str2,
3844 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3845 if (error)
3846 goto done;
3848 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3849 if (view == NULL) {
3850 error = got_error_from_errno("view_open");
3851 goto done;
3853 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3854 ignore_whitespace, force_text_diff, NULL, repo);
3855 if (error)
3856 goto done;
3857 error = view_loop(view);
3858 done:
3859 free(label1);
3860 free(label2);
3861 free(repo_path);
3862 free(cwd);
3863 if (repo)
3864 got_repo_close(repo);
3865 if (worktree)
3866 got_worktree_close(worktree);
3867 tog_free_refs();
3868 return error;
3871 __dead static void
3872 usage_blame(void)
3874 endwin();
3875 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3876 getprogname());
3877 exit(1);
3880 struct tog_blame_line {
3881 int annotated;
3882 struct got_object_id *id;
3885 static const struct got_error *
3886 draw_blame(struct tog_view *view)
3888 struct tog_blame_view_state *s = &view->state.blame;
3889 struct tog_blame *blame = &s->blame;
3890 regmatch_t *regmatch = &view->regmatch;
3891 const struct got_error *err;
3892 int lineno = 0, nprinted = 0;
3893 char *line = NULL;
3894 size_t linesize = 0;
3895 ssize_t linelen;
3896 wchar_t *wline;
3897 int width;
3898 struct tog_blame_line *blame_line;
3899 struct got_object_id *prev_id = NULL;
3900 char *id_str;
3901 struct tog_color *tc;
3903 err = got_object_id_str(&id_str, s->blamed_commit->id);
3904 if (err)
3905 return err;
3907 rewind(blame->f);
3908 werase(view->window);
3910 if (asprintf(&line, "commit %s", id_str) == -1) {
3911 err = got_error_from_errno("asprintf");
3912 free(id_str);
3913 return err;
3916 err = format_line(&wline, &width, line, view->ncols, 0);
3917 free(line);
3918 line = NULL;
3919 if (err)
3920 return err;
3921 if (view_needs_focus_indication(view))
3922 wstandout(view->window);
3923 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3924 if (tc)
3925 wattr_on(view->window,
3926 COLOR_PAIR(tc->colorpair), NULL);
3927 waddwstr(view->window, wline);
3928 if (tc)
3929 wattr_off(view->window,
3930 COLOR_PAIR(tc->colorpair), NULL);
3931 if (view_needs_focus_indication(view))
3932 wstandend(view->window);
3933 free(wline);
3934 wline = NULL;
3935 if (width < view->ncols - 1)
3936 waddch(view->window, '\n');
3938 if (asprintf(&line, "[%d/%d] %s%s",
3939 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3940 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3941 free(id_str);
3942 return got_error_from_errno("asprintf");
3944 free(id_str);
3945 err = format_line(&wline, &width, line, view->ncols, 0);
3946 free(line);
3947 line = NULL;
3948 if (err)
3949 return err;
3950 waddwstr(view->window, wline);
3951 free(wline);
3952 wline = NULL;
3953 if (width < view->ncols - 1)
3954 waddch(view->window, '\n');
3956 s->eof = 0;
3957 while (nprinted < view->nlines - 2) {
3958 linelen = getline(&line, &linesize, blame->f);
3959 if (linelen == -1) {
3960 if (feof(blame->f)) {
3961 s->eof = 1;
3962 break;
3964 free(line);
3965 return got_ferror(blame->f, GOT_ERR_IO);
3967 if (++lineno < s->first_displayed_line)
3968 continue;
3970 if (view->focussed && nprinted == s->selected_line - 1)
3971 wstandout(view->window);
3973 if (blame->nlines > 0) {
3974 blame_line = &blame->lines[lineno - 1];
3975 if (blame_line->annotated && prev_id &&
3976 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3977 !(view->focussed &&
3978 nprinted == s->selected_line - 1)) {
3979 waddstr(view->window, " ");
3980 } else if (blame_line->annotated) {
3981 char *id_str;
3982 err = got_object_id_str(&id_str, blame_line->id);
3983 if (err) {
3984 free(line);
3985 return err;
3987 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3988 if (tc)
3989 wattr_on(view->window,
3990 COLOR_PAIR(tc->colorpair), NULL);
3991 wprintw(view->window, "%.8s", id_str);
3992 if (tc)
3993 wattr_off(view->window,
3994 COLOR_PAIR(tc->colorpair), NULL);
3995 free(id_str);
3996 prev_id = blame_line->id;
3997 } else {
3998 waddstr(view->window, "........");
3999 prev_id = NULL;
4001 } else {
4002 waddstr(view->window, "........");
4003 prev_id = NULL;
4006 if (view->focussed && nprinted == s->selected_line - 1)
4007 wstandend(view->window);
4008 waddstr(view->window, " ");
4010 if (view->ncols <= 9) {
4011 width = 9;
4012 wline = wcsdup(L"");
4013 if (wline == NULL) {
4014 err = got_error_from_errno("wcsdup");
4015 free(line);
4016 return err;
4018 } else if (s->first_displayed_line + nprinted ==
4019 s->matched_line &&
4020 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4021 err = add_matched_line(&width, line, view->ncols - 9, 9,
4022 view->window, regmatch);
4023 if (err) {
4024 free(line);
4025 return err;
4027 width += 9;
4028 } else {
4029 err = format_line(&wline, &width, line,
4030 view->ncols - 9, 9);
4031 waddwstr(view->window, wline);
4032 free(wline);
4033 wline = NULL;
4034 width += 9;
4037 if (width <= view->ncols - 1)
4038 waddch(view->window, '\n');
4039 if (++nprinted == 1)
4040 s->first_displayed_line = lineno;
4042 free(line);
4043 s->last_displayed_line = lineno;
4045 view_vborder(view);
4047 return NULL;
4050 static const struct got_error *
4051 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4053 const struct got_error *err = NULL;
4054 struct tog_blame_cb_args *a = arg;
4055 struct tog_blame_line *line;
4056 int errcode;
4058 if (nlines != a->nlines ||
4059 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4060 return got_error(GOT_ERR_RANGE);
4062 errcode = pthread_mutex_lock(&tog_mutex);
4063 if (errcode)
4064 return got_error_set_errno(errcode, "pthread_mutex_lock");
4066 if (*a->quit) { /* user has quit the blame view */
4067 err = got_error(GOT_ERR_ITER_COMPLETED);
4068 goto done;
4071 if (lineno == -1)
4072 goto done; /* no change in this commit */
4074 line = &a->lines[lineno - 1];
4075 if (line->annotated)
4076 goto done;
4078 line->id = got_object_id_dup(id);
4079 if (line->id == NULL) {
4080 err = got_error_from_errno("got_object_id_dup");
4081 goto done;
4083 line->annotated = 1;
4084 done:
4085 errcode = pthread_mutex_unlock(&tog_mutex);
4086 if (errcode)
4087 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4088 return err;
4091 static void *
4092 blame_thread(void *arg)
4094 const struct got_error *err;
4095 struct tog_blame_thread_args *ta = arg;
4096 struct tog_blame_cb_args *a = ta->cb_args;
4097 int errcode;
4099 err = block_signals_used_by_main_thread();
4100 if (err)
4101 return (void *)err;
4103 err = got_blame(ta->path, a->commit_id, ta->repo,
4104 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4105 if (err && err->code == GOT_ERR_CANCELLED)
4106 err = NULL;
4108 errcode = pthread_mutex_lock(&tog_mutex);
4109 if (errcode)
4110 return (void *)got_error_set_errno(errcode,
4111 "pthread_mutex_lock");
4113 got_repo_close(ta->repo);
4114 ta->repo = NULL;
4115 *ta->complete = 1;
4117 errcode = pthread_mutex_unlock(&tog_mutex);
4118 if (errcode && err == NULL)
4119 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4121 return (void *)err;
4124 static struct got_object_id *
4125 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4126 int first_displayed_line, int selected_line)
4128 struct tog_blame_line *line;
4130 if (nlines <= 0)
4131 return NULL;
4133 line = &lines[first_displayed_line - 1 + selected_line - 1];
4134 if (!line->annotated)
4135 return NULL;
4137 return line->id;
4140 static const struct got_error *
4141 stop_blame(struct tog_blame *blame)
4143 const struct got_error *err = NULL;
4144 int i;
4146 if (blame->thread) {
4147 int errcode;
4148 errcode = pthread_mutex_unlock(&tog_mutex);
4149 if (errcode)
4150 return got_error_set_errno(errcode,
4151 "pthread_mutex_unlock");
4152 errcode = pthread_join(blame->thread, (void **)&err);
4153 if (errcode)
4154 return got_error_set_errno(errcode, "pthread_join");
4155 errcode = pthread_mutex_lock(&tog_mutex);
4156 if (errcode)
4157 return got_error_set_errno(errcode,
4158 "pthread_mutex_lock");
4159 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4160 err = NULL;
4161 blame->thread = NULL;
4163 if (blame->thread_args.repo) {
4164 got_repo_close(blame->thread_args.repo);
4165 blame->thread_args.repo = NULL;
4167 if (blame->f) {
4168 if (fclose(blame->f) != 0 && err == NULL)
4169 err = got_error_from_errno("fclose");
4170 blame->f = NULL;
4172 if (blame->lines) {
4173 for (i = 0; i < blame->nlines; i++)
4174 free(blame->lines[i].id);
4175 free(blame->lines);
4176 blame->lines = NULL;
4178 free(blame->cb_args.commit_id);
4179 blame->cb_args.commit_id = NULL;
4181 return err;
4184 static const struct got_error *
4185 cancel_blame_view(void *arg)
4187 const struct got_error *err = NULL;
4188 int *done = arg;
4189 int errcode;
4191 errcode = pthread_mutex_lock(&tog_mutex);
4192 if (errcode)
4193 return got_error_set_errno(errcode,
4194 "pthread_mutex_unlock");
4196 if (*done)
4197 err = got_error(GOT_ERR_CANCELLED);
4199 errcode = pthread_mutex_unlock(&tog_mutex);
4200 if (errcode)
4201 return got_error_set_errno(errcode,
4202 "pthread_mutex_lock");
4204 return err;
4207 static const struct got_error *
4208 run_blame(struct tog_view *view)
4210 struct tog_blame_view_state *s = &view->state.blame;
4211 struct tog_blame *blame = &s->blame;
4212 const struct got_error *err = NULL;
4213 struct got_blob_object *blob = NULL;
4214 struct got_repository *thread_repo = NULL;
4215 struct got_object_id *obj_id = NULL;
4216 int obj_type;
4218 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4219 s->path);
4220 if (err)
4221 return err;
4223 err = got_object_get_type(&obj_type, s->repo, obj_id);
4224 if (err)
4225 goto done;
4227 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4228 err = got_error(GOT_ERR_OBJ_TYPE);
4229 goto done;
4232 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4233 if (err)
4234 goto done;
4235 blame->f = got_opentemp();
4236 if (blame->f == NULL) {
4237 err = got_error_from_errno("got_opentemp");
4238 goto done;
4240 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4241 &blame->line_offsets, blame->f, blob);
4242 if (err || blame->nlines == 0)
4243 goto done;
4245 /* Don't include \n at EOF in the blame line count. */
4246 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4247 blame->nlines--;
4249 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4250 if (blame->lines == NULL) {
4251 err = got_error_from_errno("calloc");
4252 goto done;
4255 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4256 if (err)
4257 goto done;
4259 blame->cb_args.view = view;
4260 blame->cb_args.lines = blame->lines;
4261 blame->cb_args.nlines = blame->nlines;
4262 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4263 if (blame->cb_args.commit_id == NULL) {
4264 err = got_error_from_errno("got_object_id_dup");
4265 goto done;
4267 blame->cb_args.quit = &s->done;
4269 blame->thread_args.path = s->path;
4270 blame->thread_args.repo = thread_repo;
4271 blame->thread_args.cb_args = &blame->cb_args;
4272 blame->thread_args.complete = &s->blame_complete;
4273 blame->thread_args.cancel_cb = cancel_blame_view;
4274 blame->thread_args.cancel_arg = &s->done;
4275 s->blame_complete = 0;
4277 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4278 s->first_displayed_line = 1;
4279 s->last_displayed_line = view->nlines;
4280 s->selected_line = 1;
4283 done:
4284 if (blob)
4285 got_object_blob_close(blob);
4286 free(obj_id);
4287 if (err)
4288 stop_blame(blame);
4289 return err;
4292 static const struct got_error *
4293 open_blame_view(struct tog_view *view, char *path,
4294 struct got_object_id *commit_id, struct got_repository *repo)
4296 const struct got_error *err = NULL;
4297 struct tog_blame_view_state *s = &view->state.blame;
4299 SIMPLEQ_INIT(&s->blamed_commits);
4301 s->path = strdup(path);
4302 if (s->path == NULL)
4303 return got_error_from_errno("strdup");
4305 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4306 if (err) {
4307 free(s->path);
4308 return err;
4311 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4312 s->first_displayed_line = 1;
4313 s->last_displayed_line = view->nlines;
4314 s->selected_line = 1;
4315 s->blame_complete = 0;
4316 s->repo = repo;
4317 s->commit_id = commit_id;
4318 memset(&s->blame, 0, sizeof(s->blame));
4320 SIMPLEQ_INIT(&s->colors);
4321 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4322 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4323 get_color_value("TOG_COLOR_COMMIT"));
4324 if (err)
4325 return err;
4328 view->show = show_blame_view;
4329 view->input = input_blame_view;
4330 view->close = close_blame_view;
4331 view->search_start = search_start_blame_view;
4332 view->search_next = search_next_blame_view;
4334 return run_blame(view);
4337 static const struct got_error *
4338 close_blame_view(struct tog_view *view)
4340 const struct got_error *err = NULL;
4341 struct tog_blame_view_state *s = &view->state.blame;
4343 if (s->blame.thread)
4344 err = stop_blame(&s->blame);
4346 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4347 struct got_object_qid *blamed_commit;
4348 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4349 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4350 got_object_qid_free(blamed_commit);
4353 free(s->path);
4354 free_colors(&s->colors);
4356 return err;
4359 static const struct got_error *
4360 search_start_blame_view(struct tog_view *view)
4362 struct tog_blame_view_state *s = &view->state.blame;
4364 s->matched_line = 0;
4365 return NULL;
4368 static const struct got_error *
4369 search_next_blame_view(struct tog_view *view)
4371 struct tog_blame_view_state *s = &view->state.blame;
4372 int lineno;
4373 char *line = NULL;
4374 size_t linesize = 0;
4375 ssize_t linelen;
4377 if (!view->searching) {
4378 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4379 return NULL;
4382 if (s->matched_line) {
4383 if (view->searching == TOG_SEARCH_FORWARD)
4384 lineno = s->matched_line + 1;
4385 else
4386 lineno = s->matched_line - 1;
4387 } else {
4388 if (view->searching == TOG_SEARCH_FORWARD)
4389 lineno = 1;
4390 else
4391 lineno = s->blame.nlines;
4394 while (1) {
4395 off_t offset;
4397 if (lineno <= 0 || lineno > s->blame.nlines) {
4398 if (s->matched_line == 0) {
4399 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4400 break;
4403 if (view->searching == TOG_SEARCH_FORWARD)
4404 lineno = 1;
4405 else
4406 lineno = s->blame.nlines;
4409 offset = s->blame.line_offsets[lineno - 1];
4410 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4411 free(line);
4412 return got_error_from_errno("fseeko");
4414 linelen = getline(&line, &linesize, s->blame.f);
4415 if (linelen != -1 &&
4416 match_line(line, &view->regex, 1, &view->regmatch)) {
4417 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4418 s->matched_line = lineno;
4419 break;
4421 if (view->searching == TOG_SEARCH_FORWARD)
4422 lineno++;
4423 else
4424 lineno--;
4426 free(line);
4428 if (s->matched_line) {
4429 s->first_displayed_line = s->matched_line;
4430 s->selected_line = 1;
4433 return NULL;
4436 static const struct got_error *
4437 show_blame_view(struct tog_view *view)
4439 const struct got_error *err = NULL;
4440 struct tog_blame_view_state *s = &view->state.blame;
4441 int errcode;
4443 if (s->blame.thread == NULL) {
4444 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4445 &s->blame.thread_args);
4446 if (errcode)
4447 return got_error_set_errno(errcode, "pthread_create");
4449 halfdelay(1); /* fast refresh while annotating */
4452 if (s->blame_complete)
4453 halfdelay(10); /* disable fast refresh */
4455 err = draw_blame(view);
4457 view_vborder(view);
4458 return err;
4461 static const struct got_error *
4462 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4464 const struct got_error *err = NULL, *thread_err = NULL;
4465 struct tog_view *diff_view;
4466 struct tog_blame_view_state *s = &view->state.blame;
4467 int begin_x = 0;
4469 switch (ch) {
4470 case 'q':
4471 s->done = 1;
4472 break;
4473 case 'k':
4474 case KEY_UP:
4475 if (s->selected_line > 1)
4476 s->selected_line--;
4477 else if (s->selected_line == 1 &&
4478 s->first_displayed_line > 1)
4479 s->first_displayed_line--;
4480 break;
4481 case KEY_PPAGE:
4482 case CTRL('b'):
4483 if (s->first_displayed_line == 1) {
4484 s->selected_line = 1;
4485 break;
4487 if (s->first_displayed_line > view->nlines - 2)
4488 s->first_displayed_line -=
4489 (view->nlines - 2);
4490 else
4491 s->first_displayed_line = 1;
4492 break;
4493 case 'j':
4494 case KEY_DOWN:
4495 if (s->selected_line < view->nlines - 2 &&
4496 s->first_displayed_line +
4497 s->selected_line <= s->blame.nlines)
4498 s->selected_line++;
4499 else if (s->last_displayed_line <
4500 s->blame.nlines)
4501 s->first_displayed_line++;
4502 break;
4503 case 'b':
4504 case 'p': {
4505 struct got_object_id *id = NULL;
4506 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4507 s->first_displayed_line, s->selected_line);
4508 if (id == NULL)
4509 break;
4510 if (ch == 'p') {
4511 struct got_commit_object *commit;
4512 struct got_object_qid *pid;
4513 struct got_object_id *blob_id = NULL;
4514 int obj_type;
4515 err = got_object_open_as_commit(&commit,
4516 s->repo, id);
4517 if (err)
4518 break;
4519 pid = SIMPLEQ_FIRST(
4520 got_object_commit_get_parent_ids(commit));
4521 if (pid == NULL) {
4522 got_object_commit_close(commit);
4523 break;
4525 /* Check if path history ends here. */
4526 err = got_object_id_by_path(&blob_id, s->repo,
4527 pid->id, s->path);
4528 if (err) {
4529 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4530 err = NULL;
4531 got_object_commit_close(commit);
4532 break;
4534 err = got_object_get_type(&obj_type, s->repo,
4535 blob_id);
4536 free(blob_id);
4537 /* Can't blame non-blob type objects. */
4538 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4539 got_object_commit_close(commit);
4540 break;
4542 err = got_object_qid_alloc(&s->blamed_commit,
4543 pid->id);
4544 got_object_commit_close(commit);
4545 } else {
4546 if (got_object_id_cmp(id,
4547 s->blamed_commit->id) == 0)
4548 break;
4549 err = got_object_qid_alloc(&s->blamed_commit,
4550 id);
4552 if (err)
4553 break;
4554 s->done = 1;
4555 thread_err = stop_blame(&s->blame);
4556 s->done = 0;
4557 if (thread_err)
4558 break;
4559 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4560 s->blamed_commit, entry);
4561 err = run_blame(view);
4562 if (err)
4563 break;
4564 break;
4566 case 'B': {
4567 struct got_object_qid *first;
4568 first = SIMPLEQ_FIRST(&s->blamed_commits);
4569 if (!got_object_id_cmp(first->id, s->commit_id))
4570 break;
4571 s->done = 1;
4572 thread_err = stop_blame(&s->blame);
4573 s->done = 0;
4574 if (thread_err)
4575 break;
4576 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4577 got_object_qid_free(s->blamed_commit);
4578 s->blamed_commit =
4579 SIMPLEQ_FIRST(&s->blamed_commits);
4580 err = run_blame(view);
4581 if (err)
4582 break;
4583 break;
4585 case KEY_ENTER:
4586 case '\r': {
4587 struct got_object_id *id = NULL;
4588 struct got_object_qid *pid;
4589 struct got_commit_object *commit = NULL;
4590 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4591 s->first_displayed_line, s->selected_line);
4592 if (id == NULL)
4593 break;
4594 err = got_object_open_as_commit(&commit, s->repo, id);
4595 if (err)
4596 break;
4597 pid = SIMPLEQ_FIRST(
4598 got_object_commit_get_parent_ids(commit));
4599 if (view_is_parent_view(view))
4600 begin_x = view_split_begin_x(view->begin_x);
4601 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4602 if (diff_view == NULL) {
4603 got_object_commit_close(commit);
4604 err = got_error_from_errno("view_open");
4605 break;
4607 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4608 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4609 got_object_commit_close(commit);
4610 if (err) {
4611 view_close(diff_view);
4612 break;
4614 view->focussed = 0;
4615 diff_view->focussed = 1;
4616 if (view_is_parent_view(view)) {
4617 err = view_close_child(view);
4618 if (err)
4619 break;
4620 view_set_child(view, diff_view);
4621 view->focus_child = 1;
4622 } else
4623 *new_view = diff_view;
4624 if (err)
4625 break;
4626 break;
4628 case KEY_NPAGE:
4629 case CTRL('f'):
4630 case ' ':
4631 if (s->last_displayed_line >= s->blame.nlines &&
4632 s->selected_line >= MIN(s->blame.nlines,
4633 view->nlines - 2)) {
4634 break;
4636 if (s->last_displayed_line >= s->blame.nlines &&
4637 s->selected_line < view->nlines - 2) {
4638 s->selected_line = MIN(s->blame.nlines,
4639 view->nlines - 2);
4640 break;
4642 if (s->last_displayed_line + view->nlines - 2
4643 <= s->blame.nlines)
4644 s->first_displayed_line +=
4645 view->nlines - 2;
4646 else
4647 s->first_displayed_line =
4648 s->blame.nlines -
4649 (view->nlines - 3);
4650 break;
4651 case KEY_RESIZE:
4652 if (s->selected_line > view->nlines - 2) {
4653 s->selected_line = MIN(s->blame.nlines,
4654 view->nlines - 2);
4656 break;
4657 default:
4658 break;
4660 return thread_err ? thread_err : err;
4663 static const struct got_error *
4664 cmd_blame(int argc, char *argv[])
4666 const struct got_error *error;
4667 struct got_repository *repo = NULL;
4668 struct got_worktree *worktree = NULL;
4669 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4670 char *link_target = NULL;
4671 struct got_object_id *commit_id = NULL;
4672 char *commit_id_str = NULL;
4673 int ch;
4674 struct tog_view *view;
4676 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4677 switch (ch) {
4678 case 'c':
4679 commit_id_str = optarg;
4680 break;
4681 case 'r':
4682 repo_path = realpath(optarg, NULL);
4683 if (repo_path == NULL)
4684 return got_error_from_errno2("realpath",
4685 optarg);
4686 break;
4687 default:
4688 usage_blame();
4689 /* NOTREACHED */
4693 argc -= optind;
4694 argv += optind;
4696 if (argc != 1)
4697 usage_blame();
4699 if (repo_path == NULL) {
4700 cwd = getcwd(NULL, 0);
4701 if (cwd == NULL)
4702 return got_error_from_errno("getcwd");
4703 error = got_worktree_open(&worktree, cwd);
4704 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4705 goto done;
4706 if (worktree)
4707 repo_path =
4708 strdup(got_worktree_get_repo_path(worktree));
4709 else
4710 repo_path = strdup(cwd);
4711 if (repo_path == NULL) {
4712 error = got_error_from_errno("strdup");
4713 goto done;
4717 error = got_repo_open(&repo, repo_path, NULL);
4718 if (error != NULL)
4719 goto done;
4721 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4722 worktree);
4723 if (error)
4724 goto done;
4726 init_curses();
4728 error = apply_unveil(got_repo_get_path(repo), NULL);
4729 if (error)
4730 goto done;
4732 error = tog_load_refs(repo);
4733 if (error)
4734 goto done;
4736 if (commit_id_str == NULL) {
4737 struct got_reference *head_ref;
4738 error = got_ref_open(&head_ref, repo, worktree ?
4739 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4740 if (error != NULL)
4741 goto done;
4742 error = got_ref_resolve(&commit_id, repo, head_ref);
4743 got_ref_close(head_ref);
4744 } else {
4745 error = got_repo_match_object_id(&commit_id, NULL,
4746 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4748 if (error != NULL)
4749 goto done;
4751 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4752 if (view == NULL) {
4753 error = got_error_from_errno("view_open");
4754 goto done;
4757 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4758 commit_id, repo);
4759 if (error)
4760 goto done;
4762 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4763 commit_id, repo);
4764 if (error)
4765 goto done;
4766 if (worktree) {
4767 /* Release work tree lock. */
4768 got_worktree_close(worktree);
4769 worktree = NULL;
4771 error = view_loop(view);
4772 done:
4773 free(repo_path);
4774 free(in_repo_path);
4775 free(link_target);
4776 free(cwd);
4777 free(commit_id);
4778 if (worktree)
4779 got_worktree_close(worktree);
4780 if (repo)
4781 got_repo_close(repo);
4782 tog_free_refs();
4783 return error;
4786 static const struct got_error *
4787 draw_tree_entries(struct tog_view *view, const char *parent_path)
4789 struct tog_tree_view_state *s = &view->state.tree;
4790 const struct got_error *err = NULL;
4791 struct got_tree_entry *te;
4792 wchar_t *wline;
4793 struct tog_color *tc;
4794 int width, n, i, nentries;
4795 int limit = view->nlines;
4797 s->ndisplayed = 0;
4799 werase(view->window);
4801 if (limit == 0)
4802 return NULL;
4804 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4805 if (err)
4806 return err;
4807 if (view_needs_focus_indication(view))
4808 wstandout(view->window);
4809 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4810 if (tc)
4811 wattr_on(view->window,
4812 COLOR_PAIR(tc->colorpair), NULL);
4813 waddwstr(view->window, wline);
4814 if (tc)
4815 wattr_off(view->window,
4816 COLOR_PAIR(tc->colorpair), NULL);
4817 if (view_needs_focus_indication(view))
4818 wstandend(view->window);
4819 free(wline);
4820 wline = NULL;
4821 if (width < view->ncols - 1)
4822 waddch(view->window, '\n');
4823 if (--limit <= 0)
4824 return NULL;
4825 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4826 if (err)
4827 return err;
4828 waddwstr(view->window, wline);
4829 free(wline);
4830 wline = NULL;
4831 if (width < view->ncols - 1)
4832 waddch(view->window, '\n');
4833 if (--limit <= 0)
4834 return NULL;
4835 waddch(view->window, '\n');
4836 if (--limit <= 0)
4837 return NULL;
4839 if (s->first_displayed_entry == NULL) {
4840 te = got_object_tree_get_first_entry(s->tree);
4841 if (s->selected == 0) {
4842 if (view->focussed)
4843 wstandout(view->window);
4844 s->selected_entry = NULL;
4846 waddstr(view->window, " ..\n"); /* parent directory */
4847 if (s->selected == 0 && view->focussed)
4848 wstandend(view->window);
4849 s->ndisplayed++;
4850 if (--limit <= 0)
4851 return NULL;
4852 n = 1;
4853 } else {
4854 n = 0;
4855 te = s->first_displayed_entry;
4858 nentries = got_object_tree_get_nentries(s->tree);
4859 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4860 char *line = NULL, *id_str = NULL, *link_target = NULL;
4861 const char *modestr = "";
4862 mode_t mode;
4864 te = got_object_tree_get_entry(s->tree, i);
4865 mode = got_tree_entry_get_mode(te);
4867 if (s->show_ids) {
4868 err = got_object_id_str(&id_str,
4869 got_tree_entry_get_id(te));
4870 if (err)
4871 return got_error_from_errno(
4872 "got_object_id_str");
4874 if (got_object_tree_entry_is_submodule(te))
4875 modestr = "$";
4876 else if (S_ISLNK(mode)) {
4877 int i;
4879 err = got_tree_entry_get_symlink_target(&link_target,
4880 te, s->repo);
4881 if (err) {
4882 free(id_str);
4883 return err;
4885 for (i = 0; i < strlen(link_target); i++) {
4886 if (!isprint((unsigned char)link_target[i]))
4887 link_target[i] = '?';
4889 modestr = "@";
4891 else if (S_ISDIR(mode))
4892 modestr = "/";
4893 else if (mode & S_IXUSR)
4894 modestr = "*";
4895 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4896 got_tree_entry_get_name(te), modestr,
4897 link_target ? " -> ": "",
4898 link_target ? link_target : "") == -1) {
4899 free(id_str);
4900 free(link_target);
4901 return got_error_from_errno("asprintf");
4903 free(id_str);
4904 free(link_target);
4905 err = format_line(&wline, &width, line, view->ncols, 0);
4906 if (err) {
4907 free(line);
4908 break;
4910 if (n == s->selected) {
4911 if (view->focussed)
4912 wstandout(view->window);
4913 s->selected_entry = te;
4915 tc = match_color(&s->colors, line);
4916 if (tc)
4917 wattr_on(view->window,
4918 COLOR_PAIR(tc->colorpair), NULL);
4919 waddwstr(view->window, wline);
4920 if (tc)
4921 wattr_off(view->window,
4922 COLOR_PAIR(tc->colorpair), NULL);
4923 if (width < view->ncols - 1)
4924 waddch(view->window, '\n');
4925 if (n == s->selected && view->focussed)
4926 wstandend(view->window);
4927 free(line);
4928 free(wline);
4929 wline = NULL;
4930 n++;
4931 s->ndisplayed++;
4932 s->last_displayed_entry = te;
4933 if (--limit <= 0)
4934 break;
4937 return err;
4940 static void
4941 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4943 struct got_tree_entry *te;
4944 int isroot = s->tree == s->root;
4945 int i = 0;
4947 if (s->first_displayed_entry == NULL)
4948 return;
4950 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4951 while (i++ < maxscroll) {
4952 if (te == NULL) {
4953 if (!isroot)
4954 s->first_displayed_entry = NULL;
4955 break;
4957 s->first_displayed_entry = te;
4958 te = got_tree_entry_get_prev(s->tree, te);
4962 static void
4963 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4965 struct got_tree_entry *next, *last;
4966 int n = 0;
4968 if (s->first_displayed_entry)
4969 next = got_tree_entry_get_next(s->tree,
4970 s->first_displayed_entry);
4971 else
4972 next = got_object_tree_get_first_entry(s->tree);
4974 last = s->last_displayed_entry;
4975 while (next && last && n++ < maxscroll) {
4976 last = got_tree_entry_get_next(s->tree, last);
4977 if (last) {
4978 s->first_displayed_entry = next;
4979 next = got_tree_entry_get_next(s->tree, next);
4984 static const struct got_error *
4985 tree_entry_path(char **path, struct tog_parent_trees *parents,
4986 struct got_tree_entry *te)
4988 const struct got_error *err = NULL;
4989 struct tog_parent_tree *pt;
4990 size_t len = 2; /* for leading slash and NUL */
4992 TAILQ_FOREACH(pt, parents, entry)
4993 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4994 + 1 /* slash */;
4995 if (te)
4996 len += strlen(got_tree_entry_get_name(te));
4998 *path = calloc(1, len);
4999 if (path == NULL)
5000 return got_error_from_errno("calloc");
5002 (*path)[0] = '/';
5003 pt = TAILQ_LAST(parents, tog_parent_trees);
5004 while (pt) {
5005 const char *name = got_tree_entry_get_name(pt->selected_entry);
5006 if (strlcat(*path, name, len) >= len) {
5007 err = got_error(GOT_ERR_NO_SPACE);
5008 goto done;
5010 if (strlcat(*path, "/", len) >= len) {
5011 err = got_error(GOT_ERR_NO_SPACE);
5012 goto done;
5014 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5016 if (te) {
5017 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5018 err = got_error(GOT_ERR_NO_SPACE);
5019 goto done;
5022 done:
5023 if (err) {
5024 free(*path);
5025 *path = NULL;
5027 return err;
5030 static const struct got_error *
5031 blame_tree_entry(struct tog_view **new_view, int begin_x,
5032 struct got_tree_entry *te, struct tog_parent_trees *parents,
5033 struct got_object_id *commit_id, struct got_repository *repo)
5035 const struct got_error *err = NULL;
5036 char *path;
5037 struct tog_view *blame_view;
5039 *new_view = NULL;
5041 err = tree_entry_path(&path, parents, te);
5042 if (err)
5043 return err;
5045 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5046 if (blame_view == NULL) {
5047 err = got_error_from_errno("view_open");
5048 goto done;
5051 err = open_blame_view(blame_view, path, commit_id, repo);
5052 if (err) {
5053 if (err->code == GOT_ERR_CANCELLED)
5054 err = NULL;
5055 view_close(blame_view);
5056 } else
5057 *new_view = blame_view;
5058 done:
5059 free(path);
5060 return err;
5063 static const struct got_error *
5064 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5065 struct tog_tree_view_state *s)
5067 struct tog_view *log_view;
5068 const struct got_error *err = NULL;
5069 char *path;
5071 *new_view = NULL;
5073 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5074 if (log_view == NULL)
5075 return got_error_from_errno("view_open");
5077 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5078 if (err)
5079 return err;
5081 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5082 path, 0);
5083 if (err)
5084 view_close(log_view);
5085 else
5086 *new_view = log_view;
5087 free(path);
5088 return err;
5091 static const struct got_error *
5092 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5093 struct got_object_id *commit_id, const char *head_ref_name,
5094 struct got_repository *repo)
5096 const struct got_error *err = NULL;
5097 char *commit_id_str = NULL;
5098 struct tog_tree_view_state *s = &view->state.tree;
5100 TAILQ_INIT(&s->parents);
5102 err = got_object_id_str(&commit_id_str, commit_id);
5103 if (err != NULL)
5104 goto done;
5106 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5107 err = got_error_from_errno("asprintf");
5108 goto done;
5111 s->root = s->tree = root;
5112 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5113 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5114 s->commit_id = got_object_id_dup(commit_id);
5115 if (s->commit_id == NULL) {
5116 err = got_error_from_errno("got_object_id_dup");
5117 goto done;
5119 if (head_ref_name) {
5120 s->head_ref_name = strdup(head_ref_name);
5121 if (s->head_ref_name == NULL) {
5122 err = got_error_from_errno("strdup");
5123 goto done;
5126 s->repo = repo;
5128 SIMPLEQ_INIT(&s->colors);
5130 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5131 err = add_color(&s->colors, "\\$$",
5132 TOG_COLOR_TREE_SUBMODULE,
5133 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5134 if (err)
5135 goto done;
5136 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5137 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5138 if (err) {
5139 free_colors(&s->colors);
5140 goto done;
5142 err = add_color(&s->colors, "/$",
5143 TOG_COLOR_TREE_DIRECTORY,
5144 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5145 if (err) {
5146 free_colors(&s->colors);
5147 goto done;
5150 err = add_color(&s->colors, "\\*$",
5151 TOG_COLOR_TREE_EXECUTABLE,
5152 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5153 if (err) {
5154 free_colors(&s->colors);
5155 goto done;
5158 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5159 get_color_value("TOG_COLOR_COMMIT"));
5160 if (err) {
5161 free_colors(&s->colors);
5162 goto done;
5166 view->show = show_tree_view;
5167 view->input = input_tree_view;
5168 view->close = close_tree_view;
5169 view->search_start = search_start_tree_view;
5170 view->search_next = search_next_tree_view;
5171 done:
5172 free(commit_id_str);
5173 if (err) {
5174 free(s->tree_label);
5175 s->tree_label = NULL;
5177 return err;
5180 static const struct got_error *
5181 close_tree_view(struct tog_view *view)
5183 struct tog_tree_view_state *s = &view->state.tree;
5185 free_colors(&s->colors);
5186 free(s->tree_label);
5187 s->tree_label = NULL;
5188 free(s->commit_id);
5189 s->commit_id = NULL;
5190 free(s->head_ref_name);
5191 s->head_ref_name = NULL;
5192 while (!TAILQ_EMPTY(&s->parents)) {
5193 struct tog_parent_tree *parent;
5194 parent = TAILQ_FIRST(&s->parents);
5195 TAILQ_REMOVE(&s->parents, parent, entry);
5196 free(parent);
5199 if (s->tree != s->root)
5200 got_object_tree_close(s->tree);
5201 got_object_tree_close(s->root);
5202 return NULL;
5205 static const struct got_error *
5206 search_start_tree_view(struct tog_view *view)
5208 struct tog_tree_view_state *s = &view->state.tree;
5210 s->matched_entry = NULL;
5211 return NULL;
5214 static int
5215 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5217 regmatch_t regmatch;
5219 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5220 0) == 0;
5223 static const struct got_error *
5224 search_next_tree_view(struct tog_view *view)
5226 struct tog_tree_view_state *s = &view->state.tree;
5227 struct got_tree_entry *te = NULL;
5229 if (!view->searching) {
5230 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5231 return NULL;
5234 if (s->matched_entry) {
5235 if (view->searching == TOG_SEARCH_FORWARD) {
5236 if (s->selected_entry)
5237 te = got_tree_entry_get_next(s->tree,
5238 s->selected_entry);
5239 else
5240 te = got_object_tree_get_first_entry(s->tree);
5241 } else {
5242 if (s->selected_entry == NULL)
5243 te = got_object_tree_get_last_entry(s->tree);
5244 else
5245 te = got_tree_entry_get_prev(s->tree,
5246 s->selected_entry);
5248 } else {
5249 if (view->searching == TOG_SEARCH_FORWARD)
5250 te = got_object_tree_get_first_entry(s->tree);
5251 else
5252 te = got_object_tree_get_last_entry(s->tree);
5255 while (1) {
5256 if (te == NULL) {
5257 if (s->matched_entry == NULL) {
5258 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5259 return NULL;
5261 if (view->searching == TOG_SEARCH_FORWARD)
5262 te = got_object_tree_get_first_entry(s->tree);
5263 else
5264 te = got_object_tree_get_last_entry(s->tree);
5267 if (match_tree_entry(te, &view->regex)) {
5268 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5269 s->matched_entry = te;
5270 break;
5273 if (view->searching == TOG_SEARCH_FORWARD)
5274 te = got_tree_entry_get_next(s->tree, te);
5275 else
5276 te = got_tree_entry_get_prev(s->tree, te);
5279 if (s->matched_entry) {
5280 s->first_displayed_entry = s->matched_entry;
5281 s->selected = 0;
5284 return NULL;
5287 static const struct got_error *
5288 show_tree_view(struct tog_view *view)
5290 const struct got_error *err = NULL;
5291 struct tog_tree_view_state *s = &view->state.tree;
5292 char *parent_path;
5294 err = tree_entry_path(&parent_path, &s->parents, NULL);
5295 if (err)
5296 return err;
5298 err = draw_tree_entries(view, parent_path);
5299 free(parent_path);
5301 view_vborder(view);
5302 return err;
5305 static const struct got_error *
5306 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5308 const struct got_error *err = NULL;
5309 struct tog_tree_view_state *s = &view->state.tree;
5310 struct tog_view *log_view, *ref_view;
5311 int begin_x = 0;
5313 switch (ch) {
5314 case 'i':
5315 s->show_ids = !s->show_ids;
5316 break;
5317 case 'l':
5318 if (!s->selected_entry)
5319 break;
5320 if (view_is_parent_view(view))
5321 begin_x = view_split_begin_x(view->begin_x);
5322 err = log_selected_tree_entry(&log_view, begin_x, s);
5323 view->focussed = 0;
5324 log_view->focussed = 1;
5325 if (view_is_parent_view(view)) {
5326 err = view_close_child(view);
5327 if (err)
5328 return err;
5329 view_set_child(view, log_view);
5330 view->focus_child = 1;
5331 } else
5332 *new_view = log_view;
5333 break;
5334 case 'r':
5335 if (view_is_parent_view(view))
5336 begin_x = view_split_begin_x(view->begin_x);
5337 ref_view = view_open(view->nlines, view->ncols,
5338 view->begin_y, begin_x, TOG_VIEW_REF);
5339 if (ref_view == NULL)
5340 return got_error_from_errno("view_open");
5341 err = open_ref_view(ref_view, s->repo);
5342 if (err) {
5343 view_close(ref_view);
5344 return err;
5346 view->focussed = 0;
5347 ref_view->focussed = 1;
5348 if (view_is_parent_view(view)) {
5349 err = view_close_child(view);
5350 if (err)
5351 return err;
5352 view_set_child(view, ref_view);
5353 view->focus_child = 1;
5354 } else
5355 *new_view = ref_view;
5356 break;
5357 case 'k':
5358 case KEY_UP:
5359 if (s->selected > 0) {
5360 s->selected--;
5361 break;
5363 tree_scroll_up(s, 1);
5364 break;
5365 case KEY_PPAGE:
5366 case CTRL('b'):
5367 if (s->tree == s->root) {
5368 if (got_object_tree_get_first_entry(s->tree) ==
5369 s->first_displayed_entry)
5370 s->selected = 0;
5371 } else {
5372 if (s->first_displayed_entry == NULL)
5373 s->selected = 0;
5375 tree_scroll_up(s, MAX(0, view->nlines - 3));
5376 break;
5377 case 'j':
5378 case KEY_DOWN:
5379 if (s->selected < s->ndisplayed - 1) {
5380 s->selected++;
5381 break;
5383 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5384 == NULL)
5385 /* can't scroll any further */
5386 break;
5387 tree_scroll_down(s, 1);
5388 break;
5389 case KEY_NPAGE:
5390 case CTRL('f'):
5391 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5392 == NULL) {
5393 /* can't scroll any further; move cursor down */
5394 if (s->selected < s->ndisplayed - 1)
5395 s->selected = s->ndisplayed - 1;
5396 break;
5398 tree_scroll_down(s, view->nlines - 3);
5399 break;
5400 case KEY_ENTER:
5401 case '\r':
5402 case KEY_BACKSPACE:
5403 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5404 struct tog_parent_tree *parent;
5405 /* user selected '..' */
5406 if (s->tree == s->root)
5407 break;
5408 parent = TAILQ_FIRST(&s->parents);
5409 TAILQ_REMOVE(&s->parents, parent,
5410 entry);
5411 got_object_tree_close(s->tree);
5412 s->tree = parent->tree;
5413 s->first_displayed_entry =
5414 parent->first_displayed_entry;
5415 s->selected_entry =
5416 parent->selected_entry;
5417 s->selected = parent->selected;
5418 free(parent);
5419 } else if (S_ISDIR(got_tree_entry_get_mode(
5420 s->selected_entry))) {
5421 struct got_tree_object *subtree;
5422 err = got_object_open_as_tree(&subtree, s->repo,
5423 got_tree_entry_get_id(s->selected_entry));
5424 if (err)
5425 break;
5426 err = tree_view_visit_subtree(s, subtree);
5427 if (err) {
5428 got_object_tree_close(subtree);
5429 break;
5431 } else if (S_ISREG(got_tree_entry_get_mode(
5432 s->selected_entry))) {
5433 struct tog_view *blame_view;
5434 int begin_x = view_is_parent_view(view) ?
5435 view_split_begin_x(view->begin_x) : 0;
5437 err = blame_tree_entry(&blame_view, begin_x,
5438 s->selected_entry, &s->parents,
5439 s->commit_id, s->repo);
5440 if (err)
5441 break;
5442 view->focussed = 0;
5443 blame_view->focussed = 1;
5444 if (view_is_parent_view(view)) {
5445 err = view_close_child(view);
5446 if (err)
5447 return err;
5448 view_set_child(view, blame_view);
5449 view->focus_child = 1;
5450 } else
5451 *new_view = blame_view;
5453 break;
5454 case KEY_RESIZE:
5455 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5456 s->selected = view->nlines - 4;
5457 break;
5458 default:
5459 break;
5462 return err;
5465 __dead static void
5466 usage_tree(void)
5468 endwin();
5469 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5470 getprogname());
5471 exit(1);
5474 static const struct got_error *
5475 cmd_tree(int argc, char *argv[])
5477 const struct got_error *error;
5478 struct got_repository *repo = NULL;
5479 struct got_worktree *worktree = NULL;
5480 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5481 struct got_object_id *commit_id = NULL;
5482 const char *commit_id_arg = NULL;
5483 char *label = NULL;
5484 struct got_commit_object *commit = NULL;
5485 struct got_tree_object *tree = NULL;
5486 struct got_reference *ref = NULL;
5487 const char *head_ref_name = NULL;
5488 int ch;
5489 struct tog_view *view;
5491 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5492 switch (ch) {
5493 case 'c':
5494 commit_id_arg = optarg;
5495 break;
5496 case 'r':
5497 repo_path = realpath(optarg, NULL);
5498 if (repo_path == NULL)
5499 return got_error_from_errno2("realpath",
5500 optarg);
5501 break;
5502 default:
5503 usage_tree();
5504 /* NOTREACHED */
5508 argc -= optind;
5509 argv += optind;
5511 if (argc > 1)
5512 usage_tree();
5514 if (repo_path == NULL) {
5515 cwd = getcwd(NULL, 0);
5516 if (cwd == NULL)
5517 return got_error_from_errno("getcwd");
5518 error = got_worktree_open(&worktree, cwd);
5519 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5520 goto done;
5521 if (worktree)
5522 repo_path =
5523 strdup(got_worktree_get_repo_path(worktree));
5524 else
5525 repo_path = strdup(cwd);
5526 if (repo_path == NULL) {
5527 error = got_error_from_errno("strdup");
5528 goto done;
5532 error = got_repo_open(&repo, repo_path, NULL);
5533 if (error != NULL)
5534 goto done;
5536 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5537 repo, worktree);
5538 if (error)
5539 goto done;
5541 init_curses();
5543 error = apply_unveil(got_repo_get_path(repo), NULL);
5544 if (error)
5545 goto done;
5547 error = tog_load_refs(repo);
5548 if (error)
5549 goto done;
5551 if (commit_id_arg == NULL) {
5552 error = got_repo_match_object_id(&commit_id, &label,
5553 worktree ? got_worktree_get_head_ref_name(worktree) :
5554 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5555 if (error)
5556 goto done;
5557 head_ref_name = label;
5558 } else {
5559 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5560 if (error == NULL)
5561 head_ref_name = got_ref_get_name(ref);
5562 else if (error->code != GOT_ERR_NOT_REF)
5563 goto done;
5564 error = got_repo_match_object_id(&commit_id, NULL,
5565 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5566 if (error)
5567 goto done;
5570 error = got_object_open_as_commit(&commit, repo, commit_id);
5571 if (error)
5572 goto done;
5574 error = got_object_open_as_tree(&tree, repo,
5575 got_object_commit_get_tree_id(commit));
5576 if (error)
5577 goto done;
5579 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5580 if (view == NULL) {
5581 error = got_error_from_errno("view_open");
5582 goto done;
5584 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5585 if (error)
5586 goto done;
5587 if (!got_path_is_root_dir(in_repo_path)) {
5588 error = tree_view_walk_path(&view->state.tree, commit_id,
5589 in_repo_path);
5590 if (error)
5591 goto done;
5594 if (worktree) {
5595 /* Release work tree lock. */
5596 got_worktree_close(worktree);
5597 worktree = NULL;
5599 error = view_loop(view);
5600 done:
5601 free(repo_path);
5602 free(cwd);
5603 free(commit_id);
5604 free(label);
5605 if (ref)
5606 got_ref_close(ref);
5607 if (commit)
5608 got_object_commit_close(commit);
5609 if (tree)
5610 got_object_tree_close(tree);
5611 if (repo)
5612 got_repo_close(repo);
5613 tog_free_refs();
5614 return error;
5617 static const struct got_error *
5618 ref_view_load_refs(struct tog_ref_view_state *s)
5620 struct got_reflist_entry *sre;
5621 struct tog_reflist_entry *re;
5623 s->nrefs = 0;
5624 TAILQ_FOREACH(sre, &tog_refs, entry) {
5625 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5626 continue;
5628 re = malloc(sizeof(*re));
5629 if (re == NULL)
5630 return got_error_from_errno("malloc");
5632 re->ref = got_ref_dup(sre->ref);
5633 if (re->ref == NULL)
5634 return got_error_from_errno("got_ref_dup");
5635 re->idx = s->nrefs++;
5636 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5639 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5640 return NULL;
5643 void
5644 ref_view_free_refs(struct tog_ref_view_state *s)
5646 struct tog_reflist_entry *re;
5648 while (!TAILQ_EMPTY(&s->refs)) {
5649 re = TAILQ_FIRST(&s->refs);
5650 TAILQ_REMOVE(&s->refs, re, entry);
5651 got_ref_close(re->ref);
5652 free(re);
5656 static const struct got_error *
5657 open_ref_view(struct tog_view *view, struct got_repository *repo)
5659 const struct got_error *err = NULL;
5660 struct tog_ref_view_state *s = &view->state.ref;
5662 s->selected_entry = 0;
5663 s->repo = repo;
5665 TAILQ_INIT(&s->refs);
5666 SIMPLEQ_INIT(&s->colors);
5668 err = ref_view_load_refs(s);
5669 if (err)
5670 return err;
5672 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5673 err = add_color(&s->colors, "^refs/heads/",
5674 TOG_COLOR_REFS_HEADS,
5675 get_color_value("TOG_COLOR_REFS_HEADS"));
5676 if (err)
5677 goto done;
5679 err = add_color(&s->colors, "^refs/tags/",
5680 TOG_COLOR_REFS_TAGS,
5681 get_color_value("TOG_COLOR_REFS_TAGS"));
5682 if (err)
5683 goto done;
5685 err = add_color(&s->colors, "^refs/remotes/",
5686 TOG_COLOR_REFS_REMOTES,
5687 get_color_value("TOG_COLOR_REFS_REMOTES"));
5688 if (err)
5689 goto done;
5692 view->show = show_ref_view;
5693 view->input = input_ref_view;
5694 view->close = close_ref_view;
5695 view->search_start = search_start_ref_view;
5696 view->search_next = search_next_ref_view;
5697 done:
5698 if (err)
5699 free_colors(&s->colors);
5700 return err;
5703 static const struct got_error *
5704 close_ref_view(struct tog_view *view)
5706 struct tog_ref_view_state *s = &view->state.ref;
5708 ref_view_free_refs(s);
5709 free_colors(&s->colors);
5711 return NULL;
5714 static const struct got_error *
5715 resolve_reflist_entry(struct got_object_id **commit_id,
5716 struct tog_reflist_entry *re, struct got_repository *repo)
5718 const struct got_error *err = NULL;
5719 struct got_object_id *obj_id;
5720 struct got_tag_object *tag = NULL;
5721 int obj_type;
5723 *commit_id = NULL;
5725 err = got_ref_resolve(&obj_id, repo, re->ref);
5726 if (err)
5727 return err;
5729 err = got_object_get_type(&obj_type, repo, obj_id);
5730 if (err)
5731 goto done;
5733 switch (obj_type) {
5734 case GOT_OBJ_TYPE_COMMIT:
5735 *commit_id = obj_id;
5736 break;
5737 case GOT_OBJ_TYPE_TAG:
5738 err = got_object_open_as_tag(&tag, repo, obj_id);
5739 if (err)
5740 goto done;
5741 free(obj_id);
5742 err = got_object_get_type(&obj_type, repo,
5743 got_object_tag_get_object_id(tag));
5744 if (err)
5745 goto done;
5746 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5747 err = got_error(GOT_ERR_OBJ_TYPE);
5748 goto done;
5750 *commit_id = got_object_id_dup(
5751 got_object_tag_get_object_id(tag));
5752 if (*commit_id == NULL) {
5753 err = got_error_from_errno("got_object_id_dup");
5754 goto done;
5756 break;
5757 default:
5758 err = got_error(GOT_ERR_OBJ_TYPE);
5759 break;
5762 done:
5763 if (tag)
5764 got_object_tag_close(tag);
5765 if (err) {
5766 free(*commit_id);
5767 *commit_id = NULL;
5769 return err;
5772 static const struct got_error *
5773 log_ref_entry(struct tog_view **new_view, int begin_x,
5774 struct tog_reflist_entry *re, struct got_repository *repo)
5776 struct tog_view *log_view;
5777 const struct got_error *err = NULL;
5778 struct got_object_id *commit_id = NULL;
5780 *new_view = NULL;
5782 err = resolve_reflist_entry(&commit_id, re, repo);
5783 if (err) {
5784 if (err->code != GOT_ERR_OBJ_TYPE)
5785 return err;
5786 else
5787 return NULL;
5790 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5791 if (log_view == NULL) {
5792 err = got_error_from_errno("view_open");
5793 goto done;
5796 err = open_log_view(log_view, commit_id, repo,
5797 got_ref_get_name(re->ref), "", 0);
5798 done:
5799 if (err)
5800 view_close(log_view);
5801 else
5802 *new_view = log_view;
5803 free(commit_id);
5804 return err;
5807 static void
5808 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5810 struct tog_reflist_entry *re;
5811 int i = 0;
5813 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5814 return;
5816 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5817 while (i++ < maxscroll) {
5818 if (re == NULL)
5819 break;
5820 s->first_displayed_entry = re;
5821 re = TAILQ_PREV(re, tog_reflist_head, entry);
5825 static void
5826 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5828 struct tog_reflist_entry *next, *last;
5829 int n = 0;
5831 if (s->first_displayed_entry)
5832 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5833 else
5834 next = TAILQ_FIRST(&s->refs);
5836 last = s->last_displayed_entry;
5837 while (next && last && n++ < maxscroll) {
5838 last = TAILQ_NEXT(last, entry);
5839 if (last) {
5840 s->first_displayed_entry = next;
5841 next = TAILQ_NEXT(next, entry);
5846 static const struct got_error *
5847 search_start_ref_view(struct tog_view *view)
5849 struct tog_ref_view_state *s = &view->state.ref;
5851 s->matched_entry = NULL;
5852 return NULL;
5855 static int
5856 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5858 regmatch_t regmatch;
5860 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5861 0) == 0;
5864 static const struct got_error *
5865 search_next_ref_view(struct tog_view *view)
5867 struct tog_ref_view_state *s = &view->state.ref;
5868 struct tog_reflist_entry *re = NULL;
5870 if (!view->searching) {
5871 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5872 return NULL;
5875 if (s->matched_entry) {
5876 if (view->searching == TOG_SEARCH_FORWARD) {
5877 if (s->selected_entry)
5878 re = TAILQ_NEXT(s->selected_entry, entry);
5879 else
5880 re = TAILQ_PREV(s->selected_entry,
5881 tog_reflist_head, entry);
5882 } else {
5883 if (s->selected_entry == NULL)
5884 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5885 else
5886 re = TAILQ_PREV(s->selected_entry,
5887 tog_reflist_head, entry);
5889 } else {
5890 if (view->searching == TOG_SEARCH_FORWARD)
5891 re = TAILQ_FIRST(&s->refs);
5892 else
5893 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5896 while (1) {
5897 if (re == NULL) {
5898 if (s->matched_entry == NULL) {
5899 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5900 return NULL;
5902 if (view->searching == TOG_SEARCH_FORWARD)
5903 re = TAILQ_FIRST(&s->refs);
5904 else
5905 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5908 if (match_reflist_entry(re, &view->regex)) {
5909 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5910 s->matched_entry = re;
5911 break;
5914 if (view->searching == TOG_SEARCH_FORWARD)
5915 re = TAILQ_NEXT(re, entry);
5916 else
5917 re = TAILQ_PREV(re, tog_reflist_head, entry);
5920 if (s->matched_entry) {
5921 s->first_displayed_entry = s->matched_entry;
5922 s->selected = 0;
5925 return NULL;
5928 static const struct got_error *
5929 show_ref_view(struct tog_view *view)
5931 const struct got_error *err = NULL;
5932 struct tog_ref_view_state *s = &view->state.ref;
5933 struct tog_reflist_entry *re;
5934 char *line = NULL;
5935 wchar_t *wline;
5936 struct tog_color *tc;
5937 int width, n;
5938 int limit = view->nlines;
5940 werase(view->window);
5942 s->ndisplayed = 0;
5944 if (limit == 0)
5945 return NULL;
5947 re = s->first_displayed_entry;
5949 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5950 s->nrefs) == -1)
5951 return got_error_from_errno("asprintf");
5953 err = format_line(&wline, &width, line, view->ncols, 0);
5954 if (err) {
5955 free(line);
5956 return err;
5958 if (view_needs_focus_indication(view))
5959 wstandout(view->window);
5960 waddwstr(view->window, wline);
5961 if (view_needs_focus_indication(view))
5962 wstandend(view->window);
5963 free(wline);
5964 wline = NULL;
5965 free(line);
5966 line = NULL;
5967 if (width < view->ncols - 1)
5968 waddch(view->window, '\n');
5969 if (--limit <= 0)
5970 return NULL;
5972 n = 0;
5973 while (re && limit > 0) {
5974 char *line = NULL;
5976 if (got_ref_is_symbolic(re->ref)) {
5977 if (asprintf(&line, "%s -> %s",
5978 got_ref_get_name(re->ref),
5979 got_ref_get_symref_target(re->ref)) == -1)
5980 return got_error_from_errno("asprintf");
5981 } else if (s->show_ids) {
5982 struct got_object_id *id;
5983 char *id_str;
5984 err = got_ref_resolve(&id, s->repo, re->ref);
5985 if (err)
5986 return err;
5987 err = got_object_id_str(&id_str, id);
5988 if (err) {
5989 free(id);
5990 return err;
5992 if (asprintf(&line, "%s: %s",
5993 got_ref_get_name(re->ref), id_str) == -1) {
5994 err = got_error_from_errno("asprintf");
5995 free(id);
5996 free(id_str);
5997 return err;
5999 free(id);
6000 free(id_str);
6001 } else {
6002 line = strdup(got_ref_get_name(re->ref));
6003 if (line == NULL)
6004 return got_error_from_errno("strdup");
6007 err = format_line(&wline, &width, line, view->ncols, 0);
6008 if (err) {
6009 free(line);
6010 return err;
6012 if (n == s->selected) {
6013 if (view->focussed)
6014 wstandout(view->window);
6015 s->selected_entry = re;
6017 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6018 if (tc)
6019 wattr_on(view->window,
6020 COLOR_PAIR(tc->colorpair), NULL);
6021 waddwstr(view->window, wline);
6022 if (tc)
6023 wattr_off(view->window,
6024 COLOR_PAIR(tc->colorpair), NULL);
6025 if (width < view->ncols - 1)
6026 waddch(view->window, '\n');
6027 if (n == s->selected && view->focussed)
6028 wstandend(view->window);
6029 free(line);
6030 free(wline);
6031 wline = NULL;
6032 n++;
6033 s->ndisplayed++;
6034 s->last_displayed_entry = re;
6036 limit--;
6037 re = TAILQ_NEXT(re, entry);
6040 view_vborder(view);
6041 return err;
6044 static const struct got_error *
6045 browse_ref_tree(struct tog_view **new_view, int begin_x,
6046 struct tog_reflist_entry *re, struct got_repository *repo)
6048 const struct got_error *err = NULL;
6049 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6050 struct got_tree_object *tree = NULL;
6051 struct tog_view *tree_view;
6053 *new_view = NULL;
6055 err = resolve_reflist_entry(&commit_id, re, repo);
6056 if (err) {
6057 if (err->code != GOT_ERR_OBJ_TYPE)
6058 return err;
6059 else
6060 return NULL;
6063 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6064 if (err)
6065 goto done;
6067 err = got_object_open_as_tree(&tree, repo, tree_id);
6068 if (err)
6069 goto done;
6071 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6072 if (tree_view == NULL) {
6073 err = got_error_from_errno("view_open");
6074 goto done;
6077 err = open_tree_view(tree_view, tree, commit_id,
6078 got_ref_get_name(re->ref), repo);
6079 if (err)
6080 goto done;
6082 *new_view = tree_view;
6083 done:
6084 free(commit_id);
6085 free(tree_id);
6086 if (err) {
6087 if (tree)
6088 got_object_tree_close(tree);
6090 return err;
6092 static const struct got_error *
6093 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6095 const struct got_error *err = NULL;
6096 struct tog_ref_view_state *s = &view->state.ref;
6097 struct tog_view *log_view, *tree_view;
6098 int begin_x = 0;
6100 switch (ch) {
6101 case 'i':
6102 s->show_ids = !s->show_ids;
6103 break;
6104 case KEY_ENTER:
6105 case '\r':
6106 if (!s->selected_entry)
6107 break;
6108 if (view_is_parent_view(view))
6109 begin_x = view_split_begin_x(view->begin_x);
6110 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6111 s->repo);
6112 view->focussed = 0;
6113 log_view->focussed = 1;
6114 if (view_is_parent_view(view)) {
6115 err = view_close_child(view);
6116 if (err)
6117 return err;
6118 view_set_child(view, log_view);
6119 view->focus_child = 1;
6120 } else
6121 *new_view = log_view;
6122 break;
6123 case 't':
6124 if (!s->selected_entry)
6125 break;
6126 if (view_is_parent_view(view))
6127 begin_x = view_split_begin_x(view->begin_x);
6128 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6129 s->repo);
6130 if (err || tree_view == NULL)
6131 break;
6132 view->focussed = 0;
6133 tree_view->focussed = 1;
6134 if (view_is_parent_view(view)) {
6135 err = view_close_child(view);
6136 if (err)
6137 return err;
6138 view_set_child(view, tree_view);
6139 view->focus_child = 1;
6140 } else
6141 *new_view = tree_view;
6142 break;
6143 case 'k':
6144 case KEY_UP:
6145 if (s->selected > 0) {
6146 s->selected--;
6147 break;
6149 ref_scroll_up(s, 1);
6150 break;
6151 case KEY_PPAGE:
6152 case CTRL('b'):
6153 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6154 s->selected = 0;
6155 ref_scroll_up(s, MAX(0, view->nlines - 1));
6156 break;
6157 case 'j':
6158 case KEY_DOWN:
6159 if (s->selected < s->ndisplayed - 1) {
6160 s->selected++;
6161 break;
6163 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6164 /* can't scroll any further */
6165 break;
6166 ref_scroll_down(s, 1);
6167 break;
6168 case KEY_NPAGE:
6169 case CTRL('f'):
6170 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6171 /* can't scroll any further; move cursor down */
6172 if (s->selected < s->ndisplayed - 1)
6173 s->selected = s->ndisplayed - 1;
6174 break;
6176 ref_scroll_down(s, view->nlines - 1);
6177 break;
6178 case CTRL('l'):
6179 tog_free_refs();
6180 err = tog_load_refs(s->repo);
6181 if (err)
6182 break;
6183 ref_view_free_refs(s);
6184 err = ref_view_load_refs(s);
6185 break;
6186 case KEY_RESIZE:
6187 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6188 s->selected = view->nlines - 2;
6189 break;
6190 default:
6191 break;
6194 return err;
6197 __dead static void
6198 usage_ref(void)
6200 endwin();
6201 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6202 getprogname());
6203 exit(1);
6206 static const struct got_error *
6207 cmd_ref(int argc, char *argv[])
6209 const struct got_error *error;
6210 struct got_repository *repo = NULL;
6211 struct got_worktree *worktree = NULL;
6212 char *cwd = NULL, *repo_path = NULL;
6213 int ch;
6214 struct tog_view *view;
6216 while ((ch = getopt(argc, argv, "r:")) != -1) {
6217 switch (ch) {
6218 case 'r':
6219 repo_path = realpath(optarg, NULL);
6220 if (repo_path == NULL)
6221 return got_error_from_errno2("realpath",
6222 optarg);
6223 break;
6224 default:
6225 usage_ref();
6226 /* NOTREACHED */
6230 argc -= optind;
6231 argv += optind;
6233 if (argc > 1)
6234 usage_ref();
6236 if (repo_path == NULL) {
6237 cwd = getcwd(NULL, 0);
6238 if (cwd == NULL)
6239 return got_error_from_errno("getcwd");
6240 error = got_worktree_open(&worktree, cwd);
6241 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6242 goto done;
6243 if (worktree)
6244 repo_path =
6245 strdup(got_worktree_get_repo_path(worktree));
6246 else
6247 repo_path = strdup(cwd);
6248 if (repo_path == NULL) {
6249 error = got_error_from_errno("strdup");
6250 goto done;
6254 error = got_repo_open(&repo, repo_path, NULL);
6255 if (error != NULL)
6256 goto done;
6258 init_curses();
6260 error = apply_unveil(got_repo_get_path(repo), NULL);
6261 if (error)
6262 goto done;
6264 error = tog_load_refs(repo);
6265 if (error)
6266 goto done;
6268 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6269 if (view == NULL) {
6270 error = got_error_from_errno("view_open");
6271 goto done;
6274 error = open_ref_view(view, repo);
6275 if (error)
6276 goto done;
6278 if (worktree) {
6279 /* Release work tree lock. */
6280 got_worktree_close(worktree);
6281 worktree = NULL;
6283 error = view_loop(view);
6284 done:
6285 free(repo_path);
6286 free(cwd);
6287 if (repo)
6288 got_repo_close(repo);
6289 tog_free_refs();
6290 return error;
6293 static void
6294 list_commands(FILE *fp)
6296 size_t i;
6298 fprintf(fp, "commands:");
6299 for (i = 0; i < nitems(tog_commands); i++) {
6300 struct tog_cmd *cmd = &tog_commands[i];
6301 fprintf(fp, " %s", cmd->name);
6303 fputc('\n', fp);
6306 __dead static void
6307 usage(int hflag, int status)
6309 FILE *fp = (status == 0) ? stdout : stderr;
6311 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6312 getprogname());
6313 if (hflag) {
6314 fprintf(fp, "lazy usage: %s path\n", getprogname());
6315 list_commands(fp);
6317 exit(status);
6320 static char **
6321 make_argv(int argc, ...)
6323 va_list ap;
6324 char **argv;
6325 int i;
6327 va_start(ap, argc);
6329 argv = calloc(argc, sizeof(char *));
6330 if (argv == NULL)
6331 err(1, "calloc");
6332 for (i = 0; i < argc; i++) {
6333 argv[i] = strdup(va_arg(ap, char *));
6334 if (argv[i] == NULL)
6335 err(1, "strdup");
6338 va_end(ap);
6339 return argv;
6343 * Try to convert 'tog path' into a 'tog log path' command.
6344 * The user could simply have mistyped the command rather than knowingly
6345 * provided a path. So check whether argv[0] can in fact be resolved
6346 * to a path in the HEAD commit and print a special error if not.
6347 * This hack is for mpi@ <3
6349 static const struct got_error *
6350 tog_log_with_path(int argc, char *argv[])
6352 const struct got_error *error = NULL;
6353 struct tog_cmd *cmd = NULL;
6354 struct got_repository *repo = NULL;
6355 struct got_worktree *worktree = NULL;
6356 struct got_object_id *commit_id = NULL, *id = NULL;
6357 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6358 char *commit_id_str = NULL, **cmd_argv = NULL;
6360 cwd = getcwd(NULL, 0);
6361 if (cwd == NULL)
6362 return got_error_from_errno("getcwd");
6364 error = got_worktree_open(&worktree, cwd);
6365 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6366 goto done;
6368 if (worktree)
6369 repo_path = strdup(got_worktree_get_repo_path(worktree));
6370 else
6371 repo_path = strdup(cwd);
6372 if (repo_path == NULL) {
6373 error = got_error_from_errno("strdup");
6374 goto done;
6377 error = got_repo_open(&repo, repo_path, NULL);
6378 if (error != NULL)
6379 goto done;
6381 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6382 repo, worktree);
6383 if (error)
6384 goto done;
6386 error = tog_load_refs(repo);
6387 if (error)
6388 goto done;
6389 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6390 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6391 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6392 if (error)
6393 goto done;
6395 if (worktree) {
6396 got_worktree_close(worktree);
6397 worktree = NULL;
6400 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6401 if (error) {
6402 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6403 goto done;
6404 fprintf(stderr, "%s: '%s' is no known command or path\n",
6405 getprogname(), argv[0]);
6406 usage(1, 1);
6407 /* not reached */
6410 got_repo_close(repo);
6411 repo = NULL;
6413 error = got_object_id_str(&commit_id_str, commit_id);
6414 if (error)
6415 goto done;
6417 cmd = &tog_commands[0]; /* log */
6418 argc = 4;
6419 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6420 error = cmd->cmd_main(argc, cmd_argv);
6421 done:
6422 if (repo)
6423 got_repo_close(repo);
6424 if (worktree)
6425 got_worktree_close(worktree);
6426 free(id);
6427 free(commit_id_str);
6428 free(commit_id);
6429 free(cwd);
6430 free(repo_path);
6431 free(in_repo_path);
6432 if (cmd_argv) {
6433 int i;
6434 for (i = 0; i < argc; i++)
6435 free(cmd_argv[i]);
6436 free(cmd_argv);
6438 tog_free_refs();
6439 return error;
6442 int
6443 main(int argc, char *argv[])
6445 const struct got_error *error = NULL;
6446 struct tog_cmd *cmd = NULL;
6447 int ch, hflag = 0, Vflag = 0;
6448 char **cmd_argv = NULL;
6449 static struct option longopts[] = {
6450 { "version", no_argument, NULL, 'V' },
6451 { NULL, 0, NULL, 0}
6454 setlocale(LC_CTYPE, "");
6456 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6457 switch (ch) {
6458 case 'h':
6459 hflag = 1;
6460 break;
6461 case 'V':
6462 Vflag = 1;
6463 break;
6464 default:
6465 usage(hflag, 1);
6466 /* NOTREACHED */
6470 argc -= optind;
6471 argv += optind;
6472 optind = 1;
6473 optreset = 1;
6475 if (Vflag) {
6476 got_version_print_str();
6477 return 0;
6480 #ifndef PROFILE
6481 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6482 NULL) == -1)
6483 err(1, "pledge");
6484 #endif
6486 if (argc == 0) {
6487 if (hflag)
6488 usage(hflag, 0);
6489 /* Build an argument vector which runs a default command. */
6490 cmd = &tog_commands[0];
6491 argc = 1;
6492 cmd_argv = make_argv(argc, cmd->name);
6493 } else {
6494 size_t i;
6496 /* Did the user specify a command? */
6497 for (i = 0; i < nitems(tog_commands); i++) {
6498 if (strncmp(tog_commands[i].name, argv[0],
6499 strlen(argv[0])) == 0) {
6500 cmd = &tog_commands[i];
6501 break;
6506 if (cmd == NULL) {
6507 if (argc != 1)
6508 usage(0, 1);
6509 /* No command specified; try log with a path */
6510 error = tog_log_with_path(argc, argv);
6511 } else {
6512 if (hflag)
6513 cmd->cmd_usage();
6514 else
6515 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6518 endwin();
6519 putchar('\n');
6520 if (cmd_argv) {
6521 int i;
6522 for (i = 0; i < argc; i++)
6523 free(cmd_argv[i]);
6524 free(cmd_argv);
6527 if (error && error->code != GOT_ERR_CANCELLED)
6528 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6529 return 0;