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 /* for ncurses wide-character functions */
24 #include <curses.h>
25 #include <panel.h>
26 #include <locale.h>
27 #include <sha1.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_diff.h"
50 #include "got_opentemp.h"
51 #include "got_utf8.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_path.h"
57 #include "got_worktree.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #ifndef MAX
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #endif
67 #define CTRL(x) ((x) & 0x1f)
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 struct tog_cmd {
74 const char *name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 };
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
98 };
100 enum tog_view_type {
101 TOG_VIEW_DIFF,
102 TOG_VIEW_LOG,
103 TOG_VIEW_BLAME,
104 TOG_VIEW_TREE,
105 TOG_VIEW_REF,
106 };
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
114 int idx;
115 };
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
118 int ncommits;
119 struct commit_queue_head head;
120 };
122 struct tog_color {
123 STAILQ_ENTRY(tog_color) entry;
124 regex_t regex;
125 short colorpair;
126 };
127 STAILQ_HEAD(tog_colors, tog_color);
129 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
130 static struct got_reflist_object_id_map *tog_refs_idmap;
132 static const struct got_error *
133 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
134 struct got_reference* re2)
136 const char *name1 = got_ref_get_name(re1);
137 const char *name2 = got_ref_get_name(re2);
138 int isbackup1, isbackup2;
140 /* Sort backup refs towards the bottom of the list. */
141 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
142 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
143 if (!isbackup1 && isbackup2) {
144 *cmp = -1;
145 return NULL;
146 } else if (isbackup1 && !isbackup2) {
147 *cmp = 1;
148 return NULL;
151 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
152 return NULL;
155 static const struct got_error *
156 tog_load_refs(struct got_repository *repo, int sort_by_date)
158 const struct got_error *err;
160 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
161 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
162 repo);
163 if (err)
164 return err;
166 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
167 repo);
170 static void
171 tog_free_refs(void)
173 if (tog_refs_idmap) {
174 got_reflist_object_id_map_free(tog_refs_idmap);
175 tog_refs_idmap = NULL;
177 got_ref_list_free(&tog_refs);
180 static const struct got_error *
181 add_color(struct tog_colors *colors, const char *pattern,
182 int idx, short color)
184 const struct got_error *err = NULL;
185 struct tog_color *tc;
186 int regerr = 0;
188 if (idx < 1 || idx > COLOR_PAIRS - 1)
189 return NULL;
191 init_pair(idx, color, -1);
193 tc = calloc(1, sizeof(*tc));
194 if (tc == NULL)
195 return got_error_from_errno("calloc");
196 regerr = regcomp(&tc->regex, pattern,
197 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
198 if (regerr) {
199 static char regerr_msg[512];
200 static char err_msg[512];
201 regerror(regerr, &tc->regex, regerr_msg,
202 sizeof(regerr_msg));
203 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
204 regerr_msg);
205 err = got_error_msg(GOT_ERR_REGEX, err_msg);
206 free(tc);
207 return err;
209 tc->colorpair = idx;
210 STAILQ_INSERT_HEAD(colors, tc, entry);
211 return NULL;
214 static void
215 free_colors(struct tog_colors *colors)
217 struct tog_color *tc;
219 while (!STAILQ_EMPTY(colors)) {
220 tc = STAILQ_FIRST(colors);
221 STAILQ_REMOVE_HEAD(colors, entry);
222 regfree(&tc->regex);
223 free(tc);
227 struct tog_color *
228 get_color(struct tog_colors *colors, int colorpair)
230 struct tog_color *tc = NULL;
232 STAILQ_FOREACH(tc, colors, entry) {
233 if (tc->colorpair == colorpair)
234 return tc;
237 return NULL;
240 static int
241 default_color_value(const char *envvar)
243 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
244 return COLOR_MAGENTA;
245 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
246 return COLOR_CYAN;
247 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
248 return COLOR_YELLOW;
249 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
250 return COLOR_GREEN;
251 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
254 return COLOR_MAGENTA;
255 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
256 return COLOR_CYAN;
257 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
258 return COLOR_GREEN;
259 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
260 return COLOR_GREEN;
261 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
262 return COLOR_CYAN;
263 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
264 return COLOR_YELLOW;
265 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
266 return COLOR_GREEN;
267 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
270 return COLOR_YELLOW;
271 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
272 return COLOR_CYAN;
274 return -1;
277 static int
278 get_color_value(const char *envvar)
280 const char *val = getenv(envvar);
282 if (val == NULL)
283 return default_color_value(envvar);
285 if (strcasecmp(val, "black") == 0)
286 return COLOR_BLACK;
287 if (strcasecmp(val, "red") == 0)
288 return COLOR_RED;
289 if (strcasecmp(val, "green") == 0)
290 return COLOR_GREEN;
291 if (strcasecmp(val, "yellow") == 0)
292 return COLOR_YELLOW;
293 if (strcasecmp(val, "blue") == 0)
294 return COLOR_BLUE;
295 if (strcasecmp(val, "magenta") == 0)
296 return COLOR_MAGENTA;
297 if (strcasecmp(val, "cyan") == 0)
298 return COLOR_CYAN;
299 if (strcasecmp(val, "white") == 0)
300 return COLOR_WHITE;
301 if (strcasecmp(val, "default") == 0)
302 return -1;
304 return default_color_value(envvar);
308 struct tog_diff_view_state {
309 struct got_object_id *id1, *id2;
310 const char *label1, *label2;
311 FILE *f, *f1, *f2;
312 int first_displayed_line;
313 int last_displayed_line;
314 int eof;
315 int diff_context;
316 int ignore_whitespace;
317 int force_text_diff;
318 struct got_repository *repo;
319 struct tog_colors colors;
320 size_t nlines;
321 off_t *line_offsets;
322 int matched_line;
323 int selected_line;
325 /* passed from log view; may be NULL */
326 struct tog_view *log_view;
327 };
329 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
331 struct tog_log_thread_args {
332 pthread_cond_t need_commits;
333 pthread_cond_t commit_loaded;
334 int commits_needed;
335 int load_all;
336 struct got_commit_graph *graph;
337 struct commit_queue *commits;
338 const char *in_repo_path;
339 struct got_object_id *start_id;
340 struct got_repository *repo;
341 int *pack_fds;
342 int log_complete;
343 sig_atomic_t *quit;
344 struct commit_queue_entry **first_displayed_entry;
345 struct commit_queue_entry **selected_entry;
346 int *searching;
347 int *search_next_done;
348 regex_t *regex;
349 };
351 struct tog_log_view_state {
352 struct commit_queue commits;
353 struct commit_queue_entry *first_displayed_entry;
354 struct commit_queue_entry *last_displayed_entry;
355 struct commit_queue_entry *selected_entry;
356 int selected;
357 char *in_repo_path;
358 char *head_ref_name;
359 int log_branches;
360 struct got_repository *repo;
361 struct got_object_id *start_id;
362 sig_atomic_t quit;
363 pthread_t thread;
364 struct tog_log_thread_args thread_args;
365 struct commit_queue_entry *matched_entry;
366 struct commit_queue_entry *search_entry;
367 struct tog_colors colors;
368 };
370 #define TOG_COLOR_DIFF_MINUS 1
371 #define TOG_COLOR_DIFF_PLUS 2
372 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
373 #define TOG_COLOR_DIFF_META 4
374 #define TOG_COLOR_TREE_SUBMODULE 5
375 #define TOG_COLOR_TREE_SYMLINK 6
376 #define TOG_COLOR_TREE_DIRECTORY 7
377 #define TOG_COLOR_TREE_EXECUTABLE 8
378 #define TOG_COLOR_COMMIT 9
379 #define TOG_COLOR_AUTHOR 10
380 #define TOG_COLOR_DATE 11
381 #define TOG_COLOR_REFS_HEADS 12
382 #define TOG_COLOR_REFS_TAGS 13
383 #define TOG_COLOR_REFS_REMOTES 14
384 #define TOG_COLOR_REFS_BACKUP 15
386 struct tog_blame_cb_args {
387 struct tog_blame_line *lines; /* one per line */
388 int nlines;
390 struct tog_view *view;
391 struct got_object_id *commit_id;
392 int *quit;
393 };
395 struct tog_blame_thread_args {
396 const char *path;
397 struct got_repository *repo;
398 struct tog_blame_cb_args *cb_args;
399 int *complete;
400 got_cancel_cb cancel_cb;
401 void *cancel_arg;
402 };
404 struct tog_blame {
405 FILE *f;
406 off_t filesize;
407 struct tog_blame_line *lines;
408 int nlines;
409 off_t *line_offsets;
410 pthread_t thread;
411 struct tog_blame_thread_args thread_args;
412 struct tog_blame_cb_args cb_args;
413 const char *path;
414 int *pack_fds;
415 };
417 struct tog_blame_view_state {
418 int first_displayed_line;
419 int last_displayed_line;
420 int selected_line;
421 int blame_complete;
422 int eof;
423 int done;
424 struct got_object_id_queue blamed_commits;
425 struct got_object_qid *blamed_commit;
426 char *path;
427 struct got_repository *repo;
428 struct got_object_id *commit_id;
429 struct tog_blame blame;
430 int matched_line;
431 struct tog_colors colors;
432 };
434 struct tog_parent_tree {
435 TAILQ_ENTRY(tog_parent_tree) entry;
436 struct got_tree_object *tree;
437 struct got_tree_entry *first_displayed_entry;
438 struct got_tree_entry *selected_entry;
439 int selected;
440 };
442 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
444 struct tog_tree_view_state {
445 char *tree_label;
446 struct got_object_id *commit_id;/* commit which this tree belongs to */
447 struct got_tree_object *root; /* the commit's root tree entry */
448 struct got_tree_object *tree; /* currently displayed (sub-)tree */
449 struct got_tree_entry *first_displayed_entry;
450 struct got_tree_entry *last_displayed_entry;
451 struct got_tree_entry *selected_entry;
452 int ndisplayed, selected, show_ids;
453 struct tog_parent_trees parents; /* parent trees of current sub-tree */
454 char *head_ref_name;
455 struct got_repository *repo;
456 struct got_tree_entry *matched_entry;
457 struct tog_colors colors;
458 };
460 struct tog_reflist_entry {
461 TAILQ_ENTRY(tog_reflist_entry) entry;
462 struct got_reference *ref;
463 int idx;
464 };
466 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
468 struct tog_ref_view_state {
469 struct tog_reflist_head refs;
470 struct tog_reflist_entry *first_displayed_entry;
471 struct tog_reflist_entry *last_displayed_entry;
472 struct tog_reflist_entry *selected_entry;
473 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
474 struct got_repository *repo;
475 struct tog_reflist_entry *matched_entry;
476 struct tog_colors colors;
477 };
479 /*
480 * We implement two types of views: parent views and child views.
482 * The 'Tab' key switches focus between a parent view and its child view.
483 * Child views are shown side-by-side to their parent view, provided
484 * there is enough screen estate.
486 * When a new view is opened from within a parent view, this new view
487 * becomes a child view of the parent view, replacing any existing child.
489 * When a new view is opened from within a child view, this new view
490 * becomes a parent view which will obscure the views below until the
491 * user quits the new parent view by typing 'q'.
493 * This list of views contains parent views only.
494 * Child views are only pointed to by their parent view.
495 */
496 TAILQ_HEAD(tog_view_list_head, tog_view);
498 struct tog_view {
499 TAILQ_ENTRY(tog_view) entry;
500 WINDOW *window;
501 PANEL *panel;
502 int nlines, ncols, begin_y, begin_x;
503 int maxx, x; /* max column and current start column */
504 int lines, cols; /* copies of LINES and COLS */
505 int focussed; /* Only set on one parent or child view at a time. */
506 int dying;
507 struct tog_view *parent;
508 struct tog_view *child;
510 /*
511 * This flag is initially set on parent views when a new child view
512 * is created. It gets toggled when the 'Tab' key switches focus
513 * between parent and child.
514 * The flag indicates whether focus should be passed on to our child
515 * view if this parent view gets picked for focus after another parent
516 * view was closed. This prevents child views from losing focus in such
517 * situations.
518 */
519 int focus_child;
521 /* type-specific state */
522 enum tog_view_type type;
523 union {
524 struct tog_diff_view_state diff;
525 struct tog_log_view_state log;
526 struct tog_blame_view_state blame;
527 struct tog_tree_view_state tree;
528 struct tog_ref_view_state ref;
529 } state;
531 const struct got_error *(*show)(struct tog_view *);
532 const struct got_error *(*input)(struct tog_view **,
533 struct tog_view *, int);
534 const struct got_error *(*close)(struct tog_view *);
536 const struct got_error *(*search_start)(struct tog_view *);
537 const struct got_error *(*search_next)(struct tog_view *);
538 int search_started;
539 int searching;
540 #define TOG_SEARCH_FORWARD 1
541 #define TOG_SEARCH_BACKWARD 2
542 int search_next_done;
543 #define TOG_SEARCH_HAVE_MORE 1
544 #define TOG_SEARCH_NO_MORE 2
545 #define TOG_SEARCH_HAVE_NONE 3
546 regex_t regex;
547 regmatch_t regmatch;
548 };
550 static const struct got_error *open_diff_view(struct tog_view *,
551 struct got_object_id *, struct got_object_id *,
552 const char *, const char *, int, int, int, struct tog_view *,
553 struct got_repository *);
554 static const struct got_error *show_diff_view(struct tog_view *);
555 static const struct got_error *input_diff_view(struct tog_view **,
556 struct tog_view *, int);
557 static const struct got_error* close_diff_view(struct tog_view *);
558 static const struct got_error *search_start_diff_view(struct tog_view *);
559 static const struct got_error *search_next_diff_view(struct tog_view *);
561 static const struct got_error *open_log_view(struct tog_view *,
562 struct got_object_id *, struct got_repository *,
563 const char *, const char *, int);
564 static const struct got_error * show_log_view(struct tog_view *);
565 static const struct got_error *input_log_view(struct tog_view **,
566 struct tog_view *, int);
567 static const struct got_error *close_log_view(struct tog_view *);
568 static const struct got_error *search_start_log_view(struct tog_view *);
569 static const struct got_error *search_next_log_view(struct tog_view *);
571 static const struct got_error *open_blame_view(struct tog_view *, char *,
572 struct got_object_id *, struct got_repository *);
573 static const struct got_error *show_blame_view(struct tog_view *);
574 static const struct got_error *input_blame_view(struct tog_view **,
575 struct tog_view *, int);
576 static const struct got_error *close_blame_view(struct tog_view *);
577 static const struct got_error *search_start_blame_view(struct tog_view *);
578 static const struct got_error *search_next_blame_view(struct tog_view *);
580 static const struct got_error *open_tree_view(struct tog_view *,
581 struct got_object_id *, const char *, struct got_repository *);
582 static const struct got_error *show_tree_view(struct tog_view *);
583 static const struct got_error *input_tree_view(struct tog_view **,
584 struct tog_view *, int);
585 static const struct got_error *close_tree_view(struct tog_view *);
586 static const struct got_error *search_start_tree_view(struct tog_view *);
587 static const struct got_error *search_next_tree_view(struct tog_view *);
589 static const struct got_error *open_ref_view(struct tog_view *,
590 struct got_repository *);
591 static const struct got_error *show_ref_view(struct tog_view *);
592 static const struct got_error *input_ref_view(struct tog_view **,
593 struct tog_view *, int);
594 static const struct got_error *close_ref_view(struct tog_view *);
595 static const struct got_error *search_start_ref_view(struct tog_view *);
596 static const struct got_error *search_next_ref_view(struct tog_view *);
598 static volatile sig_atomic_t tog_sigwinch_received;
599 static volatile sig_atomic_t tog_sigpipe_received;
600 static volatile sig_atomic_t tog_sigcont_received;
601 static volatile sig_atomic_t tog_sigint_received;
602 static volatile sig_atomic_t tog_sigterm_received;
604 static void
605 tog_sigwinch(int signo)
607 tog_sigwinch_received = 1;
610 static void
611 tog_sigpipe(int signo)
613 tog_sigpipe_received = 1;
616 static void
617 tog_sigcont(int signo)
619 tog_sigcont_received = 1;
622 static void
623 tog_sigint(int signo)
625 tog_sigint_received = 1;
628 static void
629 tog_sigterm(int signo)
631 tog_sigterm_received = 1;
634 static int
635 tog_fatal_signal_received(void)
637 return (tog_sigpipe_received ||
638 tog_sigint_received || tog_sigint_received);
642 static const struct got_error *
643 view_close(struct tog_view *view)
645 const struct got_error *err = NULL;
647 if (view->child) {
648 view_close(view->child);
649 view->child = NULL;
651 if (view->close)
652 err = view->close(view);
653 if (view->panel)
654 del_panel(view->panel);
655 if (view->window)
656 delwin(view->window);
657 free(view);
658 return err;
661 static struct tog_view *
662 view_open(int nlines, int ncols, int begin_y, int begin_x,
663 enum tog_view_type type)
665 struct tog_view *view = calloc(1, sizeof(*view));
667 if (view == NULL)
668 return NULL;
670 view->type = type;
671 view->lines = LINES;
672 view->cols = COLS;
673 view->nlines = nlines ? nlines : LINES - begin_y;
674 view->ncols = ncols ? ncols : COLS - begin_x;
675 view->begin_y = begin_y;
676 view->begin_x = begin_x;
677 view->window = newwin(nlines, ncols, begin_y, begin_x);
678 if (view->window == NULL) {
679 view_close(view);
680 return NULL;
682 view->panel = new_panel(view->window);
683 if (view->panel == NULL ||
684 set_panel_userptr(view->panel, view) != OK) {
685 view_close(view);
686 return NULL;
689 keypad(view->window, TRUE);
690 return view;
693 static int
694 view_split_begin_x(int begin_x)
696 if (begin_x > 0 || COLS < 120)
697 return 0;
698 return (COLS - MAX(COLS / 2, 80));
701 static const struct got_error *view_resize(struct tog_view *);
703 static const struct got_error *
704 view_splitscreen(struct tog_view *view)
706 const struct got_error *err = NULL;
708 view->begin_y = 0;
709 view->begin_x = view_split_begin_x(0);
710 view->nlines = LINES;
711 view->ncols = COLS - view->begin_x;
712 view->lines = LINES;
713 view->cols = COLS;
714 err = view_resize(view);
715 if (err)
716 return err;
718 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
719 return got_error_from_errno("mvwin");
721 return NULL;
724 static const struct got_error *
725 view_fullscreen(struct tog_view *view)
727 const struct got_error *err = NULL;
729 view->begin_x = 0;
730 view->begin_y = 0;
731 view->nlines = LINES;
732 view->ncols = COLS;
733 view->lines = LINES;
734 view->cols = COLS;
735 err = view_resize(view);
736 if (err)
737 return err;
739 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
740 return got_error_from_errno("mvwin");
742 return NULL;
745 static int
746 view_is_parent_view(struct tog_view *view)
748 return view->parent == NULL;
751 static const struct got_error *
752 view_resize(struct tog_view *view)
754 int nlines, ncols;
756 if (view->lines > LINES)
757 nlines = view->nlines - (view->lines - LINES);
758 else
759 nlines = view->nlines + (LINES - view->lines);
761 if (view->cols > COLS)
762 ncols = view->ncols - (view->cols - COLS);
763 else
764 ncols = view->ncols + (COLS - view->cols);
766 if (view->child) {
767 view->child->begin_x = view_split_begin_x(view->begin_x);
768 if (view->child->begin_x == 0) {
769 ncols = COLS;
771 view_fullscreen(view->child);
772 if (view->child->focussed)
773 show_panel(view->child->panel);
774 else
775 show_panel(view->panel);
776 } else {
777 ncols = view->child->begin_x;
779 view_splitscreen(view->child);
780 show_panel(view->child->panel);
782 } else if (view->parent == NULL)
783 ncols = COLS;
785 if (wresize(view->window, nlines, ncols) == ERR)
786 return got_error_from_errno("wresize");
787 if (replace_panel(view->panel, view->window) == ERR)
788 return got_error_from_errno("replace_panel");
789 wclear(view->window);
791 view->nlines = nlines;
792 view->ncols = ncols;
793 view->lines = LINES;
794 view->cols = COLS;
796 return NULL;
799 static const struct got_error *
800 view_close_child(struct tog_view *view)
802 const struct got_error *err = NULL;
804 if (view->child == NULL)
805 return NULL;
807 err = view_close(view->child);
808 view->child = NULL;
809 return err;
812 static const struct got_error *
813 view_set_child(struct tog_view *view, struct tog_view *child)
815 view->child = child;
816 child->parent = view;
818 return view_resize(view);
821 static int
822 view_is_splitscreen(struct tog_view *view)
824 return view->begin_x > 0;
827 static void
828 tog_resizeterm(void)
830 int cols, lines;
831 struct winsize size;
833 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
834 cols = 80; /* Default */
835 lines = 24;
836 } else {
837 cols = size.ws_col;
838 lines = size.ws_row;
840 resize_term(lines, cols);
843 static const struct got_error *
844 view_search_start(struct tog_view *view)
846 const struct got_error *err = NULL;
847 char pattern[1024];
848 int ret;
850 if (view->search_started) {
851 regfree(&view->regex);
852 view->searching = 0;
853 memset(&view->regmatch, 0, sizeof(view->regmatch));
855 view->search_started = 0;
857 if (view->nlines < 1)
858 return NULL;
860 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
861 wclrtoeol(view->window);
863 nocbreak();
864 echo();
865 ret = wgetnstr(view->window, pattern, sizeof(pattern));
866 cbreak();
867 noecho();
868 if (ret == ERR)
869 return NULL;
871 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
872 err = view->search_start(view);
873 if (err) {
874 regfree(&view->regex);
875 return err;
877 view->search_started = 1;
878 view->searching = TOG_SEARCH_FORWARD;
879 view->search_next_done = 0;
880 view->search_next(view);
883 return NULL;
886 static const struct got_error *
887 view_input(struct tog_view **new, int *done, struct tog_view *view,
888 struct tog_view_list_head *views)
890 const struct got_error *err = NULL;
891 struct tog_view *v;
892 int ch, errcode;
894 *new = NULL;
896 /* Clear "no matches" indicator. */
897 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
898 view->search_next_done == TOG_SEARCH_HAVE_NONE)
899 view->search_next_done = TOG_SEARCH_HAVE_MORE;
901 if (view->searching && !view->search_next_done) {
902 errcode = pthread_mutex_unlock(&tog_mutex);
903 if (errcode)
904 return got_error_set_errno(errcode,
905 "pthread_mutex_unlock");
906 sched_yield();
907 errcode = pthread_mutex_lock(&tog_mutex);
908 if (errcode)
909 return got_error_set_errno(errcode,
910 "pthread_mutex_lock");
911 view->search_next(view);
912 return NULL;
915 nodelay(stdscr, FALSE);
916 /* Allow threads to make progress while we are waiting for input. */
917 errcode = pthread_mutex_unlock(&tog_mutex);
918 if (errcode)
919 return got_error_set_errno(errcode, "pthread_mutex_unlock");
920 ch = wgetch(view->window);
921 errcode = pthread_mutex_lock(&tog_mutex);
922 if (errcode)
923 return got_error_set_errno(errcode, "pthread_mutex_lock");
924 nodelay(stdscr, TRUE);
926 if (tog_sigwinch_received || tog_sigcont_received) {
927 tog_resizeterm();
928 tog_sigwinch_received = 0;
929 tog_sigcont_received = 0;
930 TAILQ_FOREACH(v, views, entry) {
931 err = view_resize(v);
932 if (err)
933 return err;
934 err = v->input(new, v, KEY_RESIZE);
935 if (err)
936 return err;
937 if (v->child) {
938 err = view_resize(v->child);
939 if (err)
940 return err;
941 err = v->child->input(new, v->child,
942 KEY_RESIZE);
943 if (err)
944 return err;
949 switch (ch) {
950 case '\t':
951 if (view->child) {
952 view->focussed = 0;
953 view->child->focussed = 1;
954 view->focus_child = 1;
955 } else if (view->parent) {
956 view->focussed = 0;
957 view->parent->focussed = 1;
958 view->parent->focus_child = 0;
960 break;
961 case 'q':
962 err = view->input(new, view, ch);
963 view->dying = 1;
964 break;
965 case 'Q':
966 *done = 1;
967 break;
968 case 'f':
969 if (view_is_parent_view(view)) {
970 if (view->child == NULL)
971 break;
972 if (view_is_splitscreen(view->child)) {
973 view->focussed = 0;
974 view->child->focussed = 1;
975 err = view_fullscreen(view->child);
976 } else
977 err = view_splitscreen(view->child);
978 if (err)
979 break;
980 err = view->child->input(new, view->child,
981 KEY_RESIZE);
982 } else {
983 if (view_is_splitscreen(view)) {
984 view->parent->focussed = 0;
985 view->focussed = 1;
986 err = view_fullscreen(view);
987 } else {
988 err = view_splitscreen(view);
990 if (err)
991 break;
992 err = view->input(new, view, KEY_RESIZE);
994 break;
995 case KEY_RESIZE:
996 break;
997 case '/':
998 if (view->search_start)
999 view_search_start(view);
1000 else
1001 err = view->input(new, view, ch);
1002 break;
1003 case 'N':
1004 case 'n':
1005 if (view->search_started && view->search_next) {
1006 view->searching = (ch == 'n' ?
1007 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1008 view->search_next_done = 0;
1009 view->search_next(view);
1010 } else
1011 err = view->input(new, view, ch);
1012 break;
1013 default:
1014 err = view->input(new, view, ch);
1015 break;
1018 return err;
1021 void
1022 view_vborder(struct tog_view *view)
1024 PANEL *panel;
1025 const struct tog_view *view_above;
1027 if (view->parent)
1028 return view_vborder(view->parent);
1030 panel = panel_above(view->panel);
1031 if (panel == NULL)
1032 return;
1034 view_above = panel_userptr(panel);
1035 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1036 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1039 int
1040 view_needs_focus_indication(struct tog_view *view)
1042 if (view_is_parent_view(view)) {
1043 if (view->child == NULL || view->child->focussed)
1044 return 0;
1045 if (!view_is_splitscreen(view->child))
1046 return 0;
1047 } else if (!view_is_splitscreen(view))
1048 return 0;
1050 return view->focussed;
1053 static const struct got_error *
1054 view_loop(struct tog_view *view)
1056 const struct got_error *err = NULL;
1057 struct tog_view_list_head views;
1058 struct tog_view *new_view;
1059 int fast_refresh = 10;
1060 int done = 0, errcode;
1062 errcode = pthread_mutex_lock(&tog_mutex);
1063 if (errcode)
1064 return got_error_set_errno(errcode, "pthread_mutex_lock");
1066 TAILQ_INIT(&views);
1067 TAILQ_INSERT_HEAD(&views, view, entry);
1069 view->focussed = 1;
1070 err = view->show(view);
1071 if (err)
1072 return err;
1073 update_panels();
1074 doupdate();
1075 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1076 /* Refresh fast during initialization, then become slower. */
1077 if (fast_refresh && fast_refresh-- == 0)
1078 halfdelay(10); /* switch to once per second */
1080 err = view_input(&new_view, &done, view, &views);
1081 if (err)
1082 break;
1083 if (view->dying) {
1084 struct tog_view *v, *prev = NULL;
1086 if (view_is_parent_view(view))
1087 prev = TAILQ_PREV(view, tog_view_list_head,
1088 entry);
1089 else if (view->parent)
1090 prev = view->parent;
1092 if (view->parent) {
1093 view->parent->child = NULL;
1094 view->parent->focus_child = 0;
1096 err = view_resize(view->parent);
1097 if (err)
1098 break;
1099 } else
1100 TAILQ_REMOVE(&views, view, entry);
1102 err = view_close(view);
1103 if (err)
1104 goto done;
1106 view = NULL;
1107 TAILQ_FOREACH(v, &views, entry) {
1108 if (v->focussed)
1109 break;
1111 if (view == NULL && new_view == NULL) {
1112 /* No view has focus. Try to pick one. */
1113 if (prev)
1114 view = prev;
1115 else if (!TAILQ_EMPTY(&views)) {
1116 view = TAILQ_LAST(&views,
1117 tog_view_list_head);
1119 if (view) {
1120 if (view->focus_child) {
1121 view->child->focussed = 1;
1122 view = view->child;
1123 } else
1124 view->focussed = 1;
1128 if (new_view) {
1129 struct tog_view *v, *t;
1130 /* Only allow one parent view per type. */
1131 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1132 if (v->type != new_view->type)
1133 continue;
1134 TAILQ_REMOVE(&views, v, entry);
1135 err = view_close(v);
1136 if (err)
1137 goto done;
1138 break;
1140 TAILQ_INSERT_TAIL(&views, new_view, entry);
1141 view = new_view;
1143 if (view) {
1144 if (view_is_parent_view(view)) {
1145 if (view->child && view->child->focussed)
1146 view = view->child;
1147 } else {
1148 if (view->parent && view->parent->focussed)
1149 view = view->parent;
1151 show_panel(view->panel);
1152 if (view->child && view_is_splitscreen(view->child))
1153 show_panel(view->child->panel);
1154 if (view->parent && view_is_splitscreen(view)) {
1155 err = view->parent->show(view->parent);
1156 if (err)
1157 goto done;
1159 err = view->show(view);
1160 if (err)
1161 goto done;
1162 if (view->child) {
1163 err = view->child->show(view->child);
1164 if (err)
1165 goto done;
1167 update_panels();
1168 doupdate();
1171 done:
1172 while (!TAILQ_EMPTY(&views)) {
1173 view = TAILQ_FIRST(&views);
1174 TAILQ_REMOVE(&views, view, entry);
1175 view_close(view);
1178 errcode = pthread_mutex_unlock(&tog_mutex);
1179 if (errcode && err == NULL)
1180 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1182 return err;
1185 __dead static void
1186 usage_log(void)
1188 endwin();
1189 fprintf(stderr,
1190 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1191 getprogname());
1192 exit(1);
1195 /* Create newly allocated wide-character string equivalent to a byte string. */
1196 static const struct got_error *
1197 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1199 char *vis = NULL;
1200 const struct got_error *err = NULL;
1202 *ws = NULL;
1203 *wlen = mbstowcs(NULL, s, 0);
1204 if (*wlen == (size_t)-1) {
1205 int vislen;
1206 if (errno != EILSEQ)
1207 return got_error_from_errno("mbstowcs");
1209 /* byte string invalid in current encoding; try to "fix" it */
1210 err = got_mbsavis(&vis, &vislen, s);
1211 if (err)
1212 return err;
1213 *wlen = mbstowcs(NULL, vis, 0);
1214 if (*wlen == (size_t)-1) {
1215 err = got_error_from_errno("mbstowcs"); /* give up */
1216 goto done;
1220 *ws = calloc(*wlen + 1, sizeof(**ws));
1221 if (*ws == NULL) {
1222 err = got_error_from_errno("calloc");
1223 goto done;
1226 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1227 err = got_error_from_errno("mbstowcs");
1228 done:
1229 free(vis);
1230 if (err) {
1231 free(*ws);
1232 *ws = NULL;
1233 *wlen = 0;
1235 return err;
1238 static const struct got_error *
1239 expand_tab(char **ptr, const char *src)
1241 char *dst;
1242 size_t len, n, idx = 0, sz = 0;
1244 *ptr = NULL;
1245 n = len = strlen(src);
1246 dst = malloc(n + 1);
1247 if (dst == NULL)
1248 return got_error_from_errno("malloc");
1250 while (idx < len && src[idx]) {
1251 const char c = src[idx];
1253 if (c == '\t') {
1254 size_t nb = TABSIZE - sz % TABSIZE;
1255 char *p;
1257 p = realloc(dst, n + nb);
1258 if (p == NULL) {
1259 free(dst);
1260 return got_error_from_errno("realloc");
1263 dst = p;
1264 n += nb;
1265 memset(dst + sz, ' ', nb);
1266 sz += nb;
1267 } else
1268 dst[sz++] = src[idx];
1269 ++idx;
1272 dst[sz] = '\0';
1273 *ptr = dst;
1274 return NULL;
1278 * Advance at most n columns from wline starting at offset off.
1279 * Return the index to the first character after the span operation.
1280 * Return the combined column width of all spanned wide character in
1281 * *rcol.
1283 static int
1284 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1286 int width, i, cols = 0;
1288 if (n == 0) {
1289 *rcol = cols;
1290 return off;
1293 for (i = off; wline[i] != L'\0'; ++i) {
1294 if (wline[i] == L'\t')
1295 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1296 else
1297 width = wcwidth(wline[i]);
1299 if (width == -1) {
1300 width = 1;
1301 wline[i] = L'.';
1304 if (cols + width > n)
1305 break;
1306 cols += width;
1309 *rcol = cols;
1310 return i;
1314 * Format a line for display, ensuring that it won't overflow a width limit.
1315 * With scrolling, the width returned refers to the scrolled version of the
1316 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1318 static const struct got_error *
1319 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1320 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1322 const struct got_error *err = NULL;
1323 int cols;
1324 wchar_t *wline = NULL;
1325 char *exstr = NULL;
1326 size_t wlen;
1327 int i, scrollx;
1329 *wlinep = NULL;
1330 *widthp = 0;
1332 if (expand) {
1333 err = expand_tab(&exstr, line);
1334 if (err)
1335 return err;
1338 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1339 free(exstr);
1340 if (err)
1341 return err;
1343 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1345 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1346 wline[wlen - 1] = L'\0';
1347 wlen--;
1349 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1350 wline[wlen - 1] = L'\0';
1351 wlen--;
1354 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1355 wline[i] = L'\0';
1357 if (widthp)
1358 *widthp = cols;
1359 if (scrollxp)
1360 *scrollxp = scrollx;
1361 if (err)
1362 free(wline);
1363 else
1364 *wlinep = wline;
1365 return err;
1368 static const struct got_error*
1369 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1370 struct got_object_id *id, struct got_repository *repo)
1372 static const struct got_error *err = NULL;
1373 struct got_reflist_entry *re;
1374 char *s;
1375 const char *name;
1377 *refs_str = NULL;
1379 TAILQ_FOREACH(re, refs, entry) {
1380 struct got_tag_object *tag = NULL;
1381 struct got_object_id *ref_id;
1382 int cmp;
1384 name = got_ref_get_name(re->ref);
1385 if (strcmp(name, GOT_REF_HEAD) == 0)
1386 continue;
1387 if (strncmp(name, "refs/", 5) == 0)
1388 name += 5;
1389 if (strncmp(name, "got/", 4) == 0 &&
1390 strncmp(name, "got/backup/", 11) != 0)
1391 continue;
1392 if (strncmp(name, "heads/", 6) == 0)
1393 name += 6;
1394 if (strncmp(name, "remotes/", 8) == 0) {
1395 name += 8;
1396 s = strstr(name, "/" GOT_REF_HEAD);
1397 if (s != NULL && s[strlen(s)] == '\0')
1398 continue;
1400 err = got_ref_resolve(&ref_id, repo, re->ref);
1401 if (err)
1402 break;
1403 if (strncmp(name, "tags/", 5) == 0) {
1404 err = got_object_open_as_tag(&tag, repo, ref_id);
1405 if (err) {
1406 if (err->code != GOT_ERR_OBJ_TYPE) {
1407 free(ref_id);
1408 break;
1410 /* Ref points at something other than a tag. */
1411 err = NULL;
1412 tag = NULL;
1415 cmp = got_object_id_cmp(tag ?
1416 got_object_tag_get_object_id(tag) : ref_id, id);
1417 free(ref_id);
1418 if (tag)
1419 got_object_tag_close(tag);
1420 if (cmp != 0)
1421 continue;
1422 s = *refs_str;
1423 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1424 s ? ", " : "", name) == -1) {
1425 err = got_error_from_errno("asprintf");
1426 free(s);
1427 *refs_str = NULL;
1428 break;
1430 free(s);
1433 return err;
1436 static const struct got_error *
1437 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1438 int col_tab_align)
1440 char *smallerthan;
1442 smallerthan = strchr(author, '<');
1443 if (smallerthan && smallerthan[1] != '\0')
1444 author = smallerthan + 1;
1445 author[strcspn(author, "@>")] = '\0';
1446 return format_line(wauthor, author_width, NULL, author, 0, limit,
1447 col_tab_align, 0);
1450 static const struct got_error *
1451 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1452 struct got_object_id *id, const size_t date_display_cols,
1453 int author_display_cols)
1455 struct tog_log_view_state *s = &view->state.log;
1456 const struct got_error *err = NULL;
1457 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1458 char *logmsg0 = NULL, *logmsg = NULL;
1459 char *author = NULL;
1460 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1461 int author_width, logmsg_width;
1462 char *newline, *line = NULL;
1463 int col, limit, scrollx;
1464 const int avail = view->ncols;
1465 struct tm tm;
1466 time_t committer_time;
1467 struct tog_color *tc;
1469 committer_time = got_object_commit_get_committer_time(commit);
1470 if (gmtime_r(&committer_time, &tm) == NULL)
1471 return got_error_from_errno("gmtime_r");
1472 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1473 return got_error(GOT_ERR_NO_SPACE);
1475 if (avail <= date_display_cols)
1476 limit = MIN(sizeof(datebuf) - 1, avail);
1477 else
1478 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1479 tc = get_color(&s->colors, TOG_COLOR_DATE);
1480 if (tc)
1481 wattr_on(view->window,
1482 COLOR_PAIR(tc->colorpair), NULL);
1483 waddnstr(view->window, datebuf, limit);
1484 if (tc)
1485 wattr_off(view->window,
1486 COLOR_PAIR(tc->colorpair), NULL);
1487 col = limit;
1488 if (col > avail)
1489 goto done;
1491 if (avail >= 120) {
1492 char *id_str;
1493 err = got_object_id_str(&id_str, id);
1494 if (err)
1495 goto done;
1496 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1497 if (tc)
1498 wattr_on(view->window,
1499 COLOR_PAIR(tc->colorpair), NULL);
1500 wprintw(view->window, "%.8s ", id_str);
1501 if (tc)
1502 wattr_off(view->window,
1503 COLOR_PAIR(tc->colorpair), NULL);
1504 free(id_str);
1505 col += 9;
1506 if (col > avail)
1507 goto done;
1510 author = strdup(got_object_commit_get_author(commit));
1511 if (author == NULL) {
1512 err = got_error_from_errno("strdup");
1513 goto done;
1515 err = format_author(&wauthor, &author_width, author, avail - col, col);
1516 if (err)
1517 goto done;
1518 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1519 if (tc)
1520 wattr_on(view->window,
1521 COLOR_PAIR(tc->colorpair), NULL);
1522 waddwstr(view->window, wauthor);
1523 if (tc)
1524 wattr_off(view->window,
1525 COLOR_PAIR(tc->colorpair), NULL);
1526 col += author_width;
1527 while (col < avail && author_width < author_display_cols + 2) {
1528 waddch(view->window, ' ');
1529 col++;
1530 author_width++;
1532 if (col > avail)
1533 goto done;
1535 err = got_object_commit_get_logmsg(&logmsg0, commit);
1536 if (err)
1537 goto done;
1538 logmsg = logmsg0;
1539 while (*logmsg == '\n')
1540 logmsg++;
1541 newline = strchr(logmsg, '\n');
1542 if (newline)
1543 *newline = '\0';
1544 limit = avail - col;
1545 if (view->child && limit > 0)
1546 limit--; /* for the border */
1547 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1548 limit, col, 1);
1549 if (err)
1550 goto done;
1551 waddwstr(view->window, &wlogmsg[scrollx]);
1552 col += MAX(logmsg_width, 0);
1553 while (col < avail) {
1554 waddch(view->window, ' ');
1555 col++;
1557 done:
1558 free(logmsg0);
1559 free(wlogmsg);
1560 free(author);
1561 free(wauthor);
1562 free(line);
1563 return err;
1566 static struct commit_queue_entry *
1567 alloc_commit_queue_entry(struct got_commit_object *commit,
1568 struct got_object_id *id)
1570 struct commit_queue_entry *entry;
1572 entry = calloc(1, sizeof(*entry));
1573 if (entry == NULL)
1574 return NULL;
1576 entry->id = id;
1577 entry->commit = commit;
1578 return entry;
1581 static void
1582 pop_commit(struct commit_queue *commits)
1584 struct commit_queue_entry *entry;
1586 entry = TAILQ_FIRST(&commits->head);
1587 TAILQ_REMOVE(&commits->head, entry, entry);
1588 got_object_commit_close(entry->commit);
1589 commits->ncommits--;
1590 /* Don't free entry->id! It is owned by the commit graph. */
1591 free(entry);
1594 static void
1595 free_commits(struct commit_queue *commits)
1597 while (!TAILQ_EMPTY(&commits->head))
1598 pop_commit(commits);
1601 static const struct got_error *
1602 match_commit(int *have_match, struct got_object_id *id,
1603 struct got_commit_object *commit, regex_t *regex)
1605 const struct got_error *err = NULL;
1606 regmatch_t regmatch;
1607 char *id_str = NULL, *logmsg = NULL;
1609 *have_match = 0;
1611 err = got_object_id_str(&id_str, id);
1612 if (err)
1613 return err;
1615 err = got_object_commit_get_logmsg(&logmsg, commit);
1616 if (err)
1617 goto done;
1619 if (regexec(regex, got_object_commit_get_author(commit), 1,
1620 &regmatch, 0) == 0 ||
1621 regexec(regex, got_object_commit_get_committer(commit), 1,
1622 &regmatch, 0) == 0 ||
1623 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1624 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1625 *have_match = 1;
1626 done:
1627 free(id_str);
1628 free(logmsg);
1629 return err;
1632 static const struct got_error *
1633 queue_commits(struct tog_log_thread_args *a)
1635 const struct got_error *err = NULL;
1638 * We keep all commits open throughout the lifetime of the log
1639 * view in order to avoid having to re-fetch commits from disk
1640 * while updating the display.
1642 do {
1643 struct got_object_id *id;
1644 struct got_commit_object *commit;
1645 struct commit_queue_entry *entry;
1646 int errcode;
1648 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1649 NULL, NULL);
1650 if (err || id == NULL)
1651 break;
1653 err = got_object_open_as_commit(&commit, a->repo, id);
1654 if (err)
1655 break;
1656 entry = alloc_commit_queue_entry(commit, id);
1657 if (entry == NULL) {
1658 err = got_error_from_errno("alloc_commit_queue_entry");
1659 break;
1662 errcode = pthread_mutex_lock(&tog_mutex);
1663 if (errcode) {
1664 err = got_error_set_errno(errcode,
1665 "pthread_mutex_lock");
1666 break;
1669 entry->idx = a->commits->ncommits;
1670 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1671 a->commits->ncommits++;
1673 if (*a->searching == TOG_SEARCH_FORWARD &&
1674 !*a->search_next_done) {
1675 int have_match;
1676 err = match_commit(&have_match, id, commit, a->regex);
1677 if (err)
1678 break;
1679 if (have_match)
1680 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1683 errcode = pthread_mutex_unlock(&tog_mutex);
1684 if (errcode && err == NULL)
1685 err = got_error_set_errno(errcode,
1686 "pthread_mutex_unlock");
1687 if (err)
1688 break;
1689 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1691 return err;
1694 static void
1695 select_commit(struct tog_log_view_state *s)
1697 struct commit_queue_entry *entry;
1698 int ncommits = 0;
1700 entry = s->first_displayed_entry;
1701 while (entry) {
1702 if (ncommits == s->selected) {
1703 s->selected_entry = entry;
1704 break;
1706 entry = TAILQ_NEXT(entry, entry);
1707 ncommits++;
1711 static const struct got_error *
1712 draw_commits(struct tog_view *view)
1714 const struct got_error *err = NULL;
1715 struct tog_log_view_state *s = &view->state.log;
1716 struct commit_queue_entry *entry = s->selected_entry;
1717 const int limit = view->nlines;
1718 int width;
1719 int ncommits, author_cols = 4;
1720 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1721 char *refs_str = NULL;
1722 wchar_t *wline;
1723 struct tog_color *tc;
1724 static const size_t date_display_cols = 12;
1726 if (s->selected_entry &&
1727 !(view->searching && view->search_next_done == 0)) {
1728 struct got_reflist_head *refs;
1729 err = got_object_id_str(&id_str, s->selected_entry->id);
1730 if (err)
1731 return err;
1732 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1733 s->selected_entry->id);
1734 if (refs) {
1735 err = build_refs_str(&refs_str, refs,
1736 s->selected_entry->id, s->repo);
1737 if (err)
1738 goto done;
1742 if (s->thread_args.commits_needed == 0)
1743 halfdelay(10); /* disable fast refresh */
1745 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1746 if (asprintf(&ncommits_str, " [%d/%d] %s",
1747 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1748 (view->searching && !view->search_next_done) ?
1749 "searching..." : "loading...") == -1) {
1750 err = got_error_from_errno("asprintf");
1751 goto done;
1753 } else {
1754 const char *search_str = NULL;
1756 if (view->searching) {
1757 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1758 search_str = "no more matches";
1759 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1760 search_str = "no matches found";
1761 else if (!view->search_next_done)
1762 search_str = "searching...";
1765 if (asprintf(&ncommits_str, " [%d/%d] %s",
1766 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1767 search_str ? search_str :
1768 (refs_str ? refs_str : "")) == -1) {
1769 err = got_error_from_errno("asprintf");
1770 goto done;
1774 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1775 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1776 "........................................",
1777 s->in_repo_path, ncommits_str) == -1) {
1778 err = got_error_from_errno("asprintf");
1779 header = NULL;
1780 goto done;
1782 } else if (asprintf(&header, "commit %s%s",
1783 id_str ? id_str : "........................................",
1784 ncommits_str) == -1) {
1785 err = got_error_from_errno("asprintf");
1786 header = NULL;
1787 goto done;
1789 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1790 if (err)
1791 goto done;
1793 werase(view->window);
1795 if (view_needs_focus_indication(view))
1796 wstandout(view->window);
1797 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1798 if (tc)
1799 wattr_on(view->window,
1800 COLOR_PAIR(tc->colorpair), NULL);
1801 waddwstr(view->window, wline);
1802 if (tc)
1803 wattr_off(view->window,
1804 COLOR_PAIR(tc->colorpair), NULL);
1805 while (width < view->ncols) {
1806 waddch(view->window, ' ');
1807 width++;
1809 if (view_needs_focus_indication(view))
1810 wstandend(view->window);
1811 free(wline);
1812 if (limit <= 1)
1813 goto done;
1815 /* Grow author column size if necessary, and set view->maxx. */
1816 entry = s->first_displayed_entry;
1817 ncommits = 0;
1818 view->maxx = 0;
1819 while (entry) {
1820 char *author, *eol, *msg, *msg0;
1821 wchar_t *wauthor, *wmsg;
1822 int width;
1823 if (ncommits >= limit - 1)
1824 break;
1825 author = strdup(got_object_commit_get_author(entry->commit));
1826 if (author == NULL) {
1827 err = got_error_from_errno("strdup");
1828 goto done;
1830 err = format_author(&wauthor, &width, author, COLS,
1831 date_display_cols);
1832 if (author_cols < width)
1833 author_cols = width;
1834 free(wauthor);
1835 free(author);
1836 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1837 if (err)
1838 goto done;
1839 msg = msg0;
1840 while (*msg == '\n')
1841 ++msg;
1842 if ((eol = strchr(msg, '\n')))
1843 *eol = '\0';
1844 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1845 date_display_cols + author_cols, 0);
1846 if (err)
1847 goto done;
1848 view->maxx = MAX(view->maxx, width);
1849 free(msg0);
1850 free(wmsg);
1851 ncommits++;
1852 entry = TAILQ_NEXT(entry, entry);
1855 entry = s->first_displayed_entry;
1856 s->last_displayed_entry = s->first_displayed_entry;
1857 ncommits = 0;
1858 while (entry) {
1859 if (ncommits >= limit - 1)
1860 break;
1861 if (ncommits == s->selected)
1862 wstandout(view->window);
1863 err = draw_commit(view, entry->commit, entry->id,
1864 date_display_cols, author_cols);
1865 if (ncommits == s->selected)
1866 wstandend(view->window);
1867 if (err)
1868 goto done;
1869 ncommits++;
1870 s->last_displayed_entry = entry;
1871 entry = TAILQ_NEXT(entry, entry);
1874 view_vborder(view);
1875 done:
1876 free(id_str);
1877 free(refs_str);
1878 free(ncommits_str);
1879 free(header);
1880 return err;
1883 static void
1884 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1886 struct commit_queue_entry *entry;
1887 int nscrolled = 0;
1889 entry = TAILQ_FIRST(&s->commits.head);
1890 if (s->first_displayed_entry == entry)
1891 return;
1893 entry = s->first_displayed_entry;
1894 while (entry && nscrolled < maxscroll) {
1895 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1896 if (entry) {
1897 s->first_displayed_entry = entry;
1898 nscrolled++;
1903 static const struct got_error *
1904 trigger_log_thread(struct tog_view *view, int wait)
1906 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1907 int errcode;
1909 halfdelay(1); /* fast refresh while loading commits */
1911 while (ta->commits_needed > 0 || ta->load_all) {
1912 if (ta->log_complete)
1913 break;
1915 /* Wake the log thread. */
1916 errcode = pthread_cond_signal(&ta->need_commits);
1917 if (errcode)
1918 return got_error_set_errno(errcode,
1919 "pthread_cond_signal");
1922 * The mutex will be released while the view loop waits
1923 * in wgetch(), at which time the log thread will run.
1925 if (!wait)
1926 break;
1928 /* Display progress update in log view. */
1929 show_log_view(view);
1930 update_panels();
1931 doupdate();
1933 /* Wait right here while next commit is being loaded. */
1934 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1935 if (errcode)
1936 return got_error_set_errno(errcode,
1937 "pthread_cond_wait");
1939 /* Display progress update in log view. */
1940 show_log_view(view);
1941 update_panels();
1942 doupdate();
1945 return NULL;
1948 static const struct got_error *
1949 log_scroll_down(struct tog_view *view, int maxscroll)
1951 struct tog_log_view_state *s = &view->state.log;
1952 const struct got_error *err = NULL;
1953 struct commit_queue_entry *pentry;
1954 int nscrolled = 0, ncommits_needed;
1956 if (s->last_displayed_entry == NULL)
1957 return NULL;
1959 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1960 if (s->commits.ncommits < ncommits_needed &&
1961 !s->thread_args.log_complete) {
1963 * Ask the log thread for required amount of commits.
1965 s->thread_args.commits_needed += maxscroll;
1966 err = trigger_log_thread(view, 1);
1967 if (err)
1968 return err;
1971 do {
1972 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1973 if (pentry == NULL)
1974 break;
1976 s->last_displayed_entry = pentry;
1978 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1979 if (pentry == NULL)
1980 break;
1981 s->first_displayed_entry = pentry;
1982 } while (++nscrolled < maxscroll);
1984 return err;
1987 static const struct got_error *
1988 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1989 struct got_commit_object *commit, struct got_object_id *commit_id,
1990 struct tog_view *log_view, struct got_repository *repo)
1992 const struct got_error *err;
1993 struct got_object_qid *parent_id;
1994 struct tog_view *diff_view;
1996 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1997 if (diff_view == NULL)
1998 return got_error_from_errno("view_open");
2000 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2001 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2002 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2003 if (err == NULL)
2004 *new_view = diff_view;
2005 return err;
2008 static const struct got_error *
2009 tree_view_visit_subtree(struct tog_tree_view_state *s,
2010 struct got_tree_object *subtree)
2012 struct tog_parent_tree *parent;
2014 parent = calloc(1, sizeof(*parent));
2015 if (parent == NULL)
2016 return got_error_from_errno("calloc");
2018 parent->tree = s->tree;
2019 parent->first_displayed_entry = s->first_displayed_entry;
2020 parent->selected_entry = s->selected_entry;
2021 parent->selected = s->selected;
2022 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2023 s->tree = subtree;
2024 s->selected = 0;
2025 s->first_displayed_entry = NULL;
2026 return NULL;
2029 static const struct got_error *
2030 tree_view_walk_path(struct tog_tree_view_state *s,
2031 struct got_commit_object *commit, const char *path)
2033 const struct got_error *err = NULL;
2034 struct got_tree_object *tree = NULL;
2035 const char *p;
2036 char *slash, *subpath = NULL;
2038 /* Walk the path and open corresponding tree objects. */
2039 p = path;
2040 while (*p) {
2041 struct got_tree_entry *te;
2042 struct got_object_id *tree_id;
2043 char *te_name;
2045 while (p[0] == '/')
2046 p++;
2048 /* Ensure the correct subtree entry is selected. */
2049 slash = strchr(p, '/');
2050 if (slash == NULL)
2051 te_name = strdup(p);
2052 else
2053 te_name = strndup(p, slash - p);
2054 if (te_name == NULL) {
2055 err = got_error_from_errno("strndup");
2056 break;
2058 te = got_object_tree_find_entry(s->tree, te_name);
2059 if (te == NULL) {
2060 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2061 free(te_name);
2062 break;
2064 free(te_name);
2065 s->first_displayed_entry = s->selected_entry = te;
2067 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2068 break; /* jump to this file's entry */
2070 slash = strchr(p, '/');
2071 if (slash)
2072 subpath = strndup(path, slash - path);
2073 else
2074 subpath = strdup(path);
2075 if (subpath == NULL) {
2076 err = got_error_from_errno("strdup");
2077 break;
2080 err = got_object_id_by_path(&tree_id, s->repo, commit,
2081 subpath);
2082 if (err)
2083 break;
2085 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2086 free(tree_id);
2087 if (err)
2088 break;
2090 err = tree_view_visit_subtree(s, tree);
2091 if (err) {
2092 got_object_tree_close(tree);
2093 break;
2095 if (slash == NULL)
2096 break;
2097 free(subpath);
2098 subpath = NULL;
2099 p = slash;
2102 free(subpath);
2103 return err;
2106 static const struct got_error *
2107 browse_commit_tree(struct tog_view **new_view, int begin_x,
2108 struct commit_queue_entry *entry, const char *path,
2109 const char *head_ref_name, struct got_repository *repo)
2111 const struct got_error *err = NULL;
2112 struct tog_tree_view_state *s;
2113 struct tog_view *tree_view;
2115 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2116 if (tree_view == NULL)
2117 return got_error_from_errno("view_open");
2119 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2120 if (err)
2121 return err;
2122 s = &tree_view->state.tree;
2124 *new_view = tree_view;
2126 if (got_path_is_root_dir(path))
2127 return NULL;
2129 return tree_view_walk_path(s, entry->commit, path);
2132 static const struct got_error *
2133 block_signals_used_by_main_thread(void)
2135 sigset_t sigset;
2136 int errcode;
2138 if (sigemptyset(&sigset) == -1)
2139 return got_error_from_errno("sigemptyset");
2141 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2142 if (sigaddset(&sigset, SIGWINCH) == -1)
2143 return got_error_from_errno("sigaddset");
2144 if (sigaddset(&sigset, SIGCONT) == -1)
2145 return got_error_from_errno("sigaddset");
2146 if (sigaddset(&sigset, SIGINT) == -1)
2147 return got_error_from_errno("sigaddset");
2148 if (sigaddset(&sigset, SIGTERM) == -1)
2149 return got_error_from_errno("sigaddset");
2151 /* ncurses handles SIGTSTP */
2152 if (sigaddset(&sigset, SIGTSTP) == -1)
2153 return got_error_from_errno("sigaddset");
2155 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2156 if (errcode)
2157 return got_error_set_errno(errcode, "pthread_sigmask");
2159 return NULL;
2162 static void *
2163 log_thread(void *arg)
2165 const struct got_error *err = NULL;
2166 int errcode = 0;
2167 struct tog_log_thread_args *a = arg;
2168 int done = 0;
2170 err = block_signals_used_by_main_thread();
2171 if (err)
2172 return (void *)err;
2174 while (!done && !err && !tog_fatal_signal_received()) {
2175 err = queue_commits(a);
2176 if (err) {
2177 if (err->code != GOT_ERR_ITER_COMPLETED)
2178 return (void *)err;
2179 err = NULL;
2180 done = 1;
2181 } else if (a->commits_needed > 0 && !a->load_all)
2182 a->commits_needed--;
2184 errcode = pthread_mutex_lock(&tog_mutex);
2185 if (errcode) {
2186 err = got_error_set_errno(errcode,
2187 "pthread_mutex_lock");
2188 break;
2189 } else if (*a->quit)
2190 done = 1;
2191 else if (*a->first_displayed_entry == NULL) {
2192 *a->first_displayed_entry =
2193 TAILQ_FIRST(&a->commits->head);
2194 *a->selected_entry = *a->first_displayed_entry;
2197 errcode = pthread_cond_signal(&a->commit_loaded);
2198 if (errcode) {
2199 err = got_error_set_errno(errcode,
2200 "pthread_cond_signal");
2201 pthread_mutex_unlock(&tog_mutex);
2202 break;
2205 if (done)
2206 a->commits_needed = 0;
2207 else {
2208 if (a->commits_needed == 0 && !a->load_all) {
2209 errcode = pthread_cond_wait(&a->need_commits,
2210 &tog_mutex);
2211 if (errcode)
2212 err = got_error_set_errno(errcode,
2213 "pthread_cond_wait");
2214 if (*a->quit)
2215 done = 1;
2219 errcode = pthread_mutex_unlock(&tog_mutex);
2220 if (errcode && err == NULL)
2221 err = got_error_set_errno(errcode,
2222 "pthread_mutex_unlock");
2224 a->log_complete = 1;
2225 return (void *)err;
2228 static const struct got_error *
2229 stop_log_thread(struct tog_log_view_state *s)
2231 const struct got_error *err = NULL;
2232 int errcode;
2234 if (s->thread) {
2235 s->quit = 1;
2236 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2237 if (errcode)
2238 return got_error_set_errno(errcode,
2239 "pthread_cond_signal");
2240 errcode = pthread_mutex_unlock(&tog_mutex);
2241 if (errcode)
2242 return got_error_set_errno(errcode,
2243 "pthread_mutex_unlock");
2244 errcode = pthread_join(s->thread, (void **)&err);
2245 if (errcode)
2246 return got_error_set_errno(errcode, "pthread_join");
2247 errcode = pthread_mutex_lock(&tog_mutex);
2248 if (errcode)
2249 return got_error_set_errno(errcode,
2250 "pthread_mutex_lock");
2251 s->thread = NULL;
2254 if (s->thread_args.repo) {
2255 err = got_repo_close(s->thread_args.repo);
2256 s->thread_args.repo = NULL;
2259 if (s->thread_args.pack_fds) {
2260 const struct got_error *pack_err =
2261 got_repo_pack_fds_close(s->thread_args.pack_fds);
2262 if (err == NULL)
2263 err = pack_err;
2264 s->thread_args.pack_fds = NULL;
2267 if (s->thread_args.graph) {
2268 got_commit_graph_close(s->thread_args.graph);
2269 s->thread_args.graph = NULL;
2272 return err;
2275 static const struct got_error *
2276 close_log_view(struct tog_view *view)
2278 const struct got_error *err = NULL;
2279 struct tog_log_view_state *s = &view->state.log;
2280 int errcode;
2282 err = stop_log_thread(s);
2284 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2285 if (errcode && err == NULL)
2286 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2288 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2289 if (errcode && err == NULL)
2290 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2292 free_commits(&s->commits);
2293 free(s->in_repo_path);
2294 s->in_repo_path = NULL;
2295 free(s->start_id);
2296 s->start_id = NULL;
2297 free(s->head_ref_name);
2298 s->head_ref_name = NULL;
2299 return err;
2302 static const struct got_error *
2303 search_start_log_view(struct tog_view *view)
2305 struct tog_log_view_state *s = &view->state.log;
2307 s->matched_entry = NULL;
2308 s->search_entry = NULL;
2309 return NULL;
2312 static const struct got_error *
2313 search_next_log_view(struct tog_view *view)
2315 const struct got_error *err = NULL;
2316 struct tog_log_view_state *s = &view->state.log;
2317 struct commit_queue_entry *entry;
2319 /* Display progress update in log view. */
2320 show_log_view(view);
2321 update_panels();
2322 doupdate();
2324 if (s->search_entry) {
2325 int errcode, ch;
2326 errcode = pthread_mutex_unlock(&tog_mutex);
2327 if (errcode)
2328 return got_error_set_errno(errcode,
2329 "pthread_mutex_unlock");
2330 ch = wgetch(view->window);
2331 errcode = pthread_mutex_lock(&tog_mutex);
2332 if (errcode)
2333 return got_error_set_errno(errcode,
2334 "pthread_mutex_lock");
2335 if (ch == KEY_BACKSPACE) {
2336 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2337 return NULL;
2339 if (view->searching == TOG_SEARCH_FORWARD)
2340 entry = TAILQ_NEXT(s->search_entry, entry);
2341 else
2342 entry = TAILQ_PREV(s->search_entry,
2343 commit_queue_head, entry);
2344 } else if (s->matched_entry) {
2345 int matched_idx = s->matched_entry->idx;
2346 int selected_idx = s->selected_entry->idx;
2349 * If the user has moved the cursor after we hit a match,
2350 * the position from where we should continue searching
2351 * might have changed.
2353 if (view->searching == TOG_SEARCH_FORWARD) {
2354 if (matched_idx > selected_idx)
2355 entry = TAILQ_NEXT(s->selected_entry, entry);
2356 else
2357 entry = TAILQ_NEXT(s->matched_entry, entry);
2358 } else {
2359 if (matched_idx < selected_idx)
2360 entry = TAILQ_PREV(s->selected_entry,
2361 commit_queue_head, entry);
2362 else
2363 entry = TAILQ_PREV(s->matched_entry,
2364 commit_queue_head, entry);
2366 } else {
2367 entry = s->selected_entry;
2370 while (1) {
2371 int have_match = 0;
2373 if (entry == NULL) {
2374 if (s->thread_args.log_complete ||
2375 view->searching == TOG_SEARCH_BACKWARD) {
2376 view->search_next_done =
2377 (s->matched_entry == NULL ?
2378 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2379 s->search_entry = NULL;
2380 return NULL;
2383 * Poke the log thread for more commits and return,
2384 * allowing the main loop to make progress. Search
2385 * will resume at s->search_entry once we come back.
2387 s->thread_args.commits_needed++;
2388 return trigger_log_thread(view, 0);
2391 err = match_commit(&have_match, entry->id, entry->commit,
2392 &view->regex);
2393 if (err)
2394 break;
2395 if (have_match) {
2396 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2397 s->matched_entry = entry;
2398 break;
2401 s->search_entry = entry;
2402 if (view->searching == TOG_SEARCH_FORWARD)
2403 entry = TAILQ_NEXT(entry, entry);
2404 else
2405 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2408 if (s->matched_entry) {
2409 int cur = s->selected_entry->idx;
2410 while (cur < s->matched_entry->idx) {
2411 err = input_log_view(NULL, view, KEY_DOWN);
2412 if (err)
2413 return err;
2414 cur++;
2416 while (cur > s->matched_entry->idx) {
2417 err = input_log_view(NULL, view, KEY_UP);
2418 if (err)
2419 return err;
2420 cur--;
2424 s->search_entry = NULL;
2426 return NULL;
2429 static const struct got_error *
2430 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2431 struct got_repository *repo, const char *head_ref_name,
2432 const char *in_repo_path, int log_branches)
2434 const struct got_error *err = NULL;
2435 struct tog_log_view_state *s = &view->state.log;
2436 struct got_repository *thread_repo = NULL;
2437 struct got_commit_graph *thread_graph = NULL;
2438 int errcode;
2440 if (in_repo_path != s->in_repo_path) {
2441 free(s->in_repo_path);
2442 s->in_repo_path = strdup(in_repo_path);
2443 if (s->in_repo_path == NULL)
2444 return got_error_from_errno("strdup");
2447 /* The commit queue only contains commits being displayed. */
2448 TAILQ_INIT(&s->commits.head);
2449 s->commits.ncommits = 0;
2451 s->repo = repo;
2452 if (head_ref_name) {
2453 s->head_ref_name = strdup(head_ref_name);
2454 if (s->head_ref_name == NULL) {
2455 err = got_error_from_errno("strdup");
2456 goto done;
2459 s->start_id = got_object_id_dup(start_id);
2460 if (s->start_id == NULL) {
2461 err = got_error_from_errno("got_object_id_dup");
2462 goto done;
2464 s->log_branches = log_branches;
2466 STAILQ_INIT(&s->colors);
2467 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2468 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2469 get_color_value("TOG_COLOR_COMMIT"));
2470 if (err)
2471 goto done;
2472 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2473 get_color_value("TOG_COLOR_AUTHOR"));
2474 if (err) {
2475 free_colors(&s->colors);
2476 goto done;
2478 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2479 get_color_value("TOG_COLOR_DATE"));
2480 if (err) {
2481 free_colors(&s->colors);
2482 goto done;
2486 view->show = show_log_view;
2487 view->input = input_log_view;
2488 view->close = close_log_view;
2489 view->search_start = search_start_log_view;
2490 view->search_next = search_next_log_view;
2492 if (s->thread_args.pack_fds == NULL) {
2493 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2494 if (err)
2495 goto done;
2497 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2498 s->thread_args.pack_fds);
2499 if (err)
2500 goto done;
2501 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2502 !s->log_branches);
2503 if (err)
2504 goto done;
2505 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2506 s->repo, NULL, NULL);
2507 if (err)
2508 goto done;
2510 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2511 if (errcode) {
2512 err = got_error_set_errno(errcode, "pthread_cond_init");
2513 goto done;
2515 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2516 if (errcode) {
2517 err = got_error_set_errno(errcode, "pthread_cond_init");
2518 goto done;
2521 s->thread_args.commits_needed = view->nlines;
2522 s->thread_args.graph = thread_graph;
2523 s->thread_args.commits = &s->commits;
2524 s->thread_args.in_repo_path = s->in_repo_path;
2525 s->thread_args.start_id = s->start_id;
2526 s->thread_args.repo = thread_repo;
2527 s->thread_args.log_complete = 0;
2528 s->thread_args.quit = &s->quit;
2529 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2530 s->thread_args.selected_entry = &s->selected_entry;
2531 s->thread_args.searching = &view->searching;
2532 s->thread_args.search_next_done = &view->search_next_done;
2533 s->thread_args.regex = &view->regex;
2534 done:
2535 if (err)
2536 close_log_view(view);
2537 return err;
2540 static const struct got_error *
2541 show_log_view(struct tog_view *view)
2543 const struct got_error *err;
2544 struct tog_log_view_state *s = &view->state.log;
2546 if (s->thread == NULL) {
2547 int errcode = pthread_create(&s->thread, NULL, log_thread,
2548 &s->thread_args);
2549 if (errcode)
2550 return got_error_set_errno(errcode, "pthread_create");
2551 if (s->thread_args.commits_needed > 0) {
2552 err = trigger_log_thread(view, 1);
2553 if (err)
2554 return err;
2558 return draw_commits(view);
2561 static const struct got_error *
2562 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2564 const struct got_error *err = NULL;
2565 struct tog_log_view_state *s = &view->state.log;
2566 struct tog_view *diff_view = NULL, *tree_view = NULL;
2567 struct tog_view *ref_view = NULL;
2568 struct commit_queue_entry *entry;
2569 int begin_x = 0, n, nscroll = view->nlines - 1;
2571 if (s->thread_args.load_all) {
2572 if (ch == KEY_BACKSPACE)
2573 s->thread_args.load_all = 0;
2574 else if (s->thread_args.log_complete) {
2575 s->thread_args.load_all = 0;
2576 log_scroll_down(view, s->commits.ncommits);
2577 s->selected = MIN(view->nlines - 2,
2578 s->commits.ncommits - 1);
2579 select_commit(s);
2581 return NULL;
2584 switch (ch) {
2585 case 'q':
2586 s->quit = 1;
2587 break;
2588 case '0':
2589 view->x = 0;
2590 break;
2591 case '$':
2592 view->x = MAX(view->maxx - view->ncols / 2, 0);
2593 break;
2594 case KEY_RIGHT:
2595 case 'l':
2596 if (view->x + view->ncols / 2 < view->maxx)
2597 view->x += 2; /* move two columns right */
2598 break;
2599 case KEY_LEFT:
2600 case 'h':
2601 view->x -= MIN(view->x, 2); /* move two columns back */
2602 break;
2603 case 'k':
2604 case KEY_UP:
2605 case '<':
2606 case ',':
2607 case CTRL('p'):
2608 if (s->first_displayed_entry == NULL)
2609 break;
2610 if (s->selected > 0)
2611 s->selected--;
2612 else
2613 log_scroll_up(s, 1);
2614 select_commit(s);
2615 break;
2616 case 'g':
2617 case KEY_HOME:
2618 s->selected = 0;
2619 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2620 select_commit(s);
2621 break;
2622 case CTRL('u'):
2623 case 'u':
2624 nscroll /= 2;
2625 /* FALL THROUGH */
2626 case KEY_PPAGE:
2627 case CTRL('b'):
2628 if (s->first_displayed_entry == NULL)
2629 break;
2630 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2631 s->selected = MAX(0, s->selected - nscroll - 1);
2632 else
2633 log_scroll_up(s, nscroll);
2634 select_commit(s);
2635 break;
2636 case 'j':
2637 case KEY_DOWN:
2638 case '>':
2639 case '.':
2640 case CTRL('n'):
2641 if (s->first_displayed_entry == NULL)
2642 break;
2643 if (s->selected < MIN(view->nlines - 2,
2644 s->commits.ncommits - 1))
2645 s->selected++;
2646 else {
2647 err = log_scroll_down(view, 1);
2648 if (err)
2649 break;
2651 select_commit(s);
2652 break;
2653 case 'G':
2654 case KEY_END: {
2655 /* We don't know yet how many commits, so we're forced to
2656 * traverse them all. */
2657 if (!s->thread_args.log_complete) {
2658 s->thread_args.load_all = 1;
2659 return trigger_log_thread(view, 0);
2662 s->selected = 0;
2663 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2664 for (n = 0; n < view->nlines - 1; n++) {
2665 if (entry == NULL)
2666 break;
2667 s->first_displayed_entry = entry;
2668 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2670 if (n > 0)
2671 s->selected = n - 1;
2672 select_commit(s);
2673 break;
2675 case CTRL('d'):
2676 case 'd':
2677 nscroll /= 2;
2678 /* FALL THROUGH */
2679 case KEY_NPAGE:
2680 case CTRL('f'): {
2681 struct commit_queue_entry *first;
2682 first = s->first_displayed_entry;
2683 if (first == NULL)
2684 break;
2685 err = log_scroll_down(view, nscroll);
2686 if (err)
2687 break;
2688 if (first == s->first_displayed_entry &&
2689 s->selected < MIN(view->nlines - 2,
2690 s->commits.ncommits - 1)) {
2691 /* can't scroll further down */
2692 s->selected += MIN(s->last_displayed_entry->idx -
2693 s->selected_entry->idx, nscroll + 1);
2695 select_commit(s);
2696 break;
2698 case KEY_RESIZE:
2699 if (s->selected > view->nlines - 2)
2700 s->selected = view->nlines - 2;
2701 if (s->selected > s->commits.ncommits - 1)
2702 s->selected = s->commits.ncommits - 1;
2703 select_commit(s);
2704 if (s->commits.ncommits < view->nlines - 1 &&
2705 !s->thread_args.log_complete) {
2706 s->thread_args.commits_needed += (view->nlines - 1) -
2707 s->commits.ncommits;
2708 err = trigger_log_thread(view, 1);
2710 break;
2711 case KEY_ENTER:
2712 case ' ':
2713 case '\r':
2714 if (s->selected_entry == NULL)
2715 break;
2716 if (view_is_parent_view(view))
2717 begin_x = view_split_begin_x(view->begin_x);
2718 err = open_diff_view_for_commit(&diff_view, begin_x,
2719 s->selected_entry->commit, s->selected_entry->id,
2720 view, s->repo);
2721 if (err)
2722 break;
2723 view->focussed = 0;
2724 diff_view->focussed = 1;
2725 if (view_is_parent_view(view)) {
2726 err = view_close_child(view);
2727 if (err)
2728 return err;
2729 err = view_set_child(view, diff_view);
2730 if (err)
2731 return err;
2732 view->focus_child = 1;
2733 } else
2734 *new_view = diff_view;
2735 break;
2736 case 't':
2737 if (s->selected_entry == NULL)
2738 break;
2739 if (view_is_parent_view(view))
2740 begin_x = view_split_begin_x(view->begin_x);
2741 err = browse_commit_tree(&tree_view, begin_x,
2742 s->selected_entry, s->in_repo_path, s->head_ref_name,
2743 s->repo);
2744 if (err)
2745 break;
2746 view->focussed = 0;
2747 tree_view->focussed = 1;
2748 if (view_is_parent_view(view)) {
2749 err = view_close_child(view);
2750 if (err)
2751 return err;
2752 err = view_set_child(view, tree_view);
2753 if (err)
2754 return err;
2755 view->focus_child = 1;
2756 } else
2757 *new_view = tree_view;
2758 break;
2759 case KEY_BACKSPACE:
2760 case CTRL('l'):
2761 case 'B':
2762 if (ch == KEY_BACKSPACE &&
2763 got_path_is_root_dir(s->in_repo_path))
2764 break;
2765 err = stop_log_thread(s);
2766 if (err)
2767 return err;
2768 if (ch == KEY_BACKSPACE) {
2769 char *parent_path;
2770 err = got_path_dirname(&parent_path, s->in_repo_path);
2771 if (err)
2772 return err;
2773 free(s->in_repo_path);
2774 s->in_repo_path = parent_path;
2775 s->thread_args.in_repo_path = s->in_repo_path;
2776 } else if (ch == CTRL('l')) {
2777 struct got_object_id *start_id;
2778 err = got_repo_match_object_id(&start_id, NULL,
2779 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2780 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2781 if (err)
2782 return err;
2783 free(s->start_id);
2784 s->start_id = start_id;
2785 s->thread_args.start_id = s->start_id;
2786 } else /* 'B' */
2787 s->log_branches = !s->log_branches;
2789 if (s->thread_args.pack_fds == NULL) {
2790 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2791 if (err)
2792 return err;
2794 err = got_repo_open(&s->thread_args.repo,
2795 got_repo_get_path(s->repo), NULL,
2796 s->thread_args.pack_fds);
2797 if (err)
2798 return err;
2799 tog_free_refs();
2800 err = tog_load_refs(s->repo, 0);
2801 if (err)
2802 return err;
2803 err = got_commit_graph_open(&s->thread_args.graph,
2804 s->in_repo_path, !s->log_branches);
2805 if (err)
2806 return err;
2807 err = got_commit_graph_iter_start(s->thread_args.graph,
2808 s->start_id, s->repo, NULL, NULL);
2809 if (err)
2810 return err;
2811 free_commits(&s->commits);
2812 s->first_displayed_entry = NULL;
2813 s->last_displayed_entry = NULL;
2814 s->selected_entry = NULL;
2815 s->selected = 0;
2816 s->thread_args.log_complete = 0;
2817 s->quit = 0;
2818 s->thread_args.commits_needed = view->nlines;
2819 s->matched_entry = NULL;
2820 s->search_entry = NULL;
2821 break;
2822 case 'r':
2823 if (view_is_parent_view(view))
2824 begin_x = view_split_begin_x(view->begin_x);
2825 ref_view = view_open(view->nlines, view->ncols,
2826 view->begin_y, begin_x, TOG_VIEW_REF);
2827 if (ref_view == NULL)
2828 return got_error_from_errno("view_open");
2829 err = open_ref_view(ref_view, s->repo);
2830 if (err) {
2831 view_close(ref_view);
2832 return err;
2834 view->focussed = 0;
2835 ref_view->focussed = 1;
2836 if (view_is_parent_view(view)) {
2837 err = view_close_child(view);
2838 if (err)
2839 return err;
2840 err = view_set_child(view, ref_view);
2841 if (err)
2842 return err;
2843 view->focus_child = 1;
2844 } else
2845 *new_view = ref_view;
2846 break;
2847 default:
2848 break;
2851 return err;
2854 static const struct got_error *
2855 apply_unveil(const char *repo_path, const char *worktree_path)
2857 const struct got_error *error;
2859 #ifdef PROFILE
2860 if (unveil("gmon.out", "rwc") != 0)
2861 return got_error_from_errno2("unveil", "gmon.out");
2862 #endif
2863 if (repo_path && unveil(repo_path, "r") != 0)
2864 return got_error_from_errno2("unveil", repo_path);
2866 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2867 return got_error_from_errno2("unveil", worktree_path);
2869 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2870 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2872 error = got_privsep_unveil_exec_helpers();
2873 if (error != NULL)
2874 return error;
2876 if (unveil(NULL, NULL) != 0)
2877 return got_error_from_errno("unveil");
2879 return NULL;
2882 static void
2883 init_curses(void)
2886 * Override default signal handlers before starting ncurses.
2887 * This should prevent ncurses from installing its own
2888 * broken cleanup() signal handler.
2890 signal(SIGWINCH, tog_sigwinch);
2891 signal(SIGPIPE, tog_sigpipe);
2892 signal(SIGCONT, tog_sigcont);
2893 signal(SIGINT, tog_sigint);
2894 signal(SIGTERM, tog_sigterm);
2896 initscr();
2897 cbreak();
2898 halfdelay(1); /* Do fast refresh while initial view is loading. */
2899 noecho();
2900 nonl();
2901 intrflush(stdscr, FALSE);
2902 keypad(stdscr, TRUE);
2903 curs_set(0);
2904 if (getenv("TOG_COLORS") != NULL) {
2905 start_color();
2906 use_default_colors();
2910 static const struct got_error *
2911 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2912 struct got_repository *repo, struct got_worktree *worktree)
2914 const struct got_error *err = NULL;
2916 if (argc == 0) {
2917 *in_repo_path = strdup("/");
2918 if (*in_repo_path == NULL)
2919 return got_error_from_errno("strdup");
2920 return NULL;
2923 if (worktree) {
2924 const char *prefix = got_worktree_get_path_prefix(worktree);
2925 char *p;
2927 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2928 if (err)
2929 return err;
2930 if (asprintf(in_repo_path, "%s%s%s", prefix,
2931 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2932 p) == -1) {
2933 err = got_error_from_errno("asprintf");
2934 *in_repo_path = NULL;
2936 free(p);
2937 } else
2938 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2940 return err;
2943 static const struct got_error *
2944 cmd_log(int argc, char *argv[])
2946 const struct got_error *error;
2947 struct got_repository *repo = NULL;
2948 struct got_worktree *worktree = NULL;
2949 struct got_object_id *start_id = NULL;
2950 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2951 char *start_commit = NULL, *label = NULL;
2952 struct got_reference *ref = NULL;
2953 const char *head_ref_name = NULL;
2954 int ch, log_branches = 0;
2955 struct tog_view *view;
2956 int *pack_fds = NULL;
2958 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2959 switch (ch) {
2960 case 'b':
2961 log_branches = 1;
2962 break;
2963 case 'c':
2964 start_commit = optarg;
2965 break;
2966 case 'r':
2967 repo_path = realpath(optarg, NULL);
2968 if (repo_path == NULL)
2969 return got_error_from_errno2("realpath",
2970 optarg);
2971 break;
2972 default:
2973 usage_log();
2974 /* NOTREACHED */
2978 argc -= optind;
2979 argv += optind;
2981 if (argc > 1)
2982 usage_log();
2984 error = got_repo_pack_fds_open(&pack_fds);
2985 if (error != NULL)
2986 goto done;
2988 if (repo_path == NULL) {
2989 cwd = getcwd(NULL, 0);
2990 if (cwd == NULL)
2991 return got_error_from_errno("getcwd");
2992 error = got_worktree_open(&worktree, cwd);
2993 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2994 goto done;
2995 if (worktree)
2996 repo_path =
2997 strdup(got_worktree_get_repo_path(worktree));
2998 else
2999 repo_path = strdup(cwd);
3000 if (repo_path == NULL) {
3001 error = got_error_from_errno("strdup");
3002 goto done;
3006 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3007 if (error != NULL)
3008 goto done;
3010 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3011 repo, worktree);
3012 if (error)
3013 goto done;
3015 init_curses();
3017 error = apply_unveil(got_repo_get_path(repo),
3018 worktree ? got_worktree_get_root_path(worktree) : NULL);
3019 if (error)
3020 goto done;
3022 /* already loaded by tog_log_with_path()? */
3023 if (TAILQ_EMPTY(&tog_refs)) {
3024 error = tog_load_refs(repo, 0);
3025 if (error)
3026 goto done;
3029 if (start_commit == NULL) {
3030 error = got_repo_match_object_id(&start_id, &label,
3031 worktree ? got_worktree_get_head_ref_name(worktree) :
3032 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3033 if (error)
3034 goto done;
3035 head_ref_name = label;
3036 } else {
3037 error = got_ref_open(&ref, repo, start_commit, 0);
3038 if (error == NULL)
3039 head_ref_name = got_ref_get_name(ref);
3040 else if (error->code != GOT_ERR_NOT_REF)
3041 goto done;
3042 error = got_repo_match_object_id(&start_id, NULL,
3043 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3044 if (error)
3045 goto done;
3048 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3049 if (view == NULL) {
3050 error = got_error_from_errno("view_open");
3051 goto done;
3053 error = open_log_view(view, start_id, repo, head_ref_name,
3054 in_repo_path, log_branches);
3055 if (error)
3056 goto done;
3057 if (worktree) {
3058 /* Release work tree lock. */
3059 got_worktree_close(worktree);
3060 worktree = NULL;
3062 error = view_loop(view);
3063 done:
3064 free(in_repo_path);
3065 free(repo_path);
3066 free(cwd);
3067 free(start_id);
3068 free(label);
3069 if (ref)
3070 got_ref_close(ref);
3071 if (repo) {
3072 const struct got_error *close_err = got_repo_close(repo);
3073 if (error == NULL)
3074 error = close_err;
3076 if (worktree)
3077 got_worktree_close(worktree);
3078 if (pack_fds) {
3079 const struct got_error *pack_err =
3080 got_repo_pack_fds_close(pack_fds);
3081 if (error == NULL)
3082 error = pack_err;
3084 tog_free_refs();
3085 return error;
3088 __dead static void
3089 usage_diff(void)
3091 endwin();
3092 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3093 "[-w] object1 object2\n", getprogname());
3094 exit(1);
3097 static int
3098 match_line(const char *line, regex_t *regex, size_t nmatch,
3099 regmatch_t *regmatch)
3101 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3104 struct tog_color *
3105 match_color(struct tog_colors *colors, const char *line)
3107 struct tog_color *tc = NULL;
3109 STAILQ_FOREACH(tc, colors, entry) {
3110 if (match_line(line, &tc->regex, 0, NULL))
3111 return tc;
3114 return NULL;
3117 static const struct got_error *
3118 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3119 WINDOW *window, int skipcol, regmatch_t *regmatch)
3121 const struct got_error *err = NULL;
3122 char *exstr = NULL;
3123 wchar_t *wline = NULL;
3124 int rme, rms, n, width, scrollx;
3125 int width0 = 0, width1 = 0, width2 = 0;
3126 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3128 *wtotal = 0;
3130 rms = regmatch->rm_so;
3131 rme = regmatch->rm_eo;
3133 err = expand_tab(&exstr, line);
3134 if (err)
3135 return err;
3137 /* Split the line into 3 segments, according to match offsets. */
3138 seg0 = strndup(exstr, rms);
3139 if (seg0 == NULL) {
3140 err = got_error_from_errno("strndup");
3141 goto done;
3143 seg1 = strndup(exstr + rms, rme - rms);
3144 if (seg1 == NULL) {
3145 err = got_error_from_errno("strndup");
3146 goto done;
3148 seg2 = strdup(exstr + rme);
3149 if (seg2 == NULL) {
3150 err = got_error_from_errno("strndup");
3151 goto done;
3154 /* draw up to matched token if we haven't scrolled past it */
3155 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3156 col_tab_align, 1);
3157 if (err)
3158 goto done;
3159 n = MAX(width0 - skipcol, 0);
3160 if (n) {
3161 free(wline);
3162 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3163 wlimit, col_tab_align, 1);
3164 if (err)
3165 goto done;
3166 waddwstr(window, &wline[scrollx]);
3167 wlimit -= width;
3168 *wtotal += width;
3171 if (wlimit > 0) {
3172 int i = 0, w = 0;
3173 size_t wlen;
3175 free(wline);
3176 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3177 col_tab_align, 1);
3178 if (err)
3179 goto done;
3180 wlen = wcslen(wline);
3181 while (i < wlen) {
3182 width = wcwidth(wline[i]);
3183 if (width == -1) {
3184 /* should not happen, tabs are expanded */
3185 err = got_error(GOT_ERR_RANGE);
3186 goto done;
3188 if (width0 + w + width > skipcol)
3189 break;
3190 w += width;
3191 i++;
3193 /* draw (visible part of) matched token (if scrolled into it) */
3194 if (width1 - w > 0) {
3195 wattron(window, A_STANDOUT);
3196 waddwstr(window, &wline[i]);
3197 wattroff(window, A_STANDOUT);
3198 wlimit -= (width1 - w);
3199 *wtotal += (width1 - w);
3203 if (wlimit > 0) { /* draw rest of line */
3204 free(wline);
3205 if (skipcol > width0 + width1) {
3206 err = format_line(&wline, &width2, &scrollx, seg2,
3207 skipcol - (width0 + width1), wlimit,
3208 col_tab_align, 1);
3209 if (err)
3210 goto done;
3211 waddwstr(window, &wline[scrollx]);
3212 } else {
3213 err = format_line(&wline, &width2, NULL, seg2, 0,
3214 wlimit, col_tab_align, 1);
3215 if (err)
3216 goto done;
3217 waddwstr(window, wline);
3219 *wtotal += width2;
3221 done:
3222 free(wline);
3223 free(exstr);
3224 free(seg0);
3225 free(seg1);
3226 free(seg2);
3227 return err;
3230 static const struct got_error *
3231 draw_file(struct tog_view *view, const char *header)
3233 struct tog_diff_view_state *s = &view->state.diff;
3234 regmatch_t *regmatch = &view->regmatch;
3235 const struct got_error *err;
3236 int nprinted = 0;
3237 char *line;
3238 size_t linesize = 0;
3239 ssize_t linelen;
3240 struct tog_color *tc;
3241 wchar_t *wline;
3242 int width;
3243 int max_lines = view->nlines;
3244 int nlines = s->nlines;
3245 off_t line_offset;
3247 line_offset = s->line_offsets[s->first_displayed_line - 1];
3248 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3249 return got_error_from_errno("fseek");
3251 werase(view->window);
3253 if (header) {
3254 if (asprintf(&line, "[%d/%d] %s",
3255 s->first_displayed_line - 1 + s->selected_line, nlines,
3256 header) == -1)
3257 return got_error_from_errno("asprintf");
3258 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3259 0, 0);
3260 free(line);
3261 if (err)
3262 return err;
3264 if (view_needs_focus_indication(view))
3265 wstandout(view->window);
3266 waddwstr(view->window, wline);
3267 free(wline);
3268 wline = NULL;
3269 if (view_needs_focus_indication(view))
3270 wstandend(view->window);
3271 if (width <= view->ncols - 1)
3272 waddch(view->window, '\n');
3274 if (max_lines <= 1)
3275 return NULL;
3276 max_lines--;
3279 s->eof = 0;
3280 view->maxx = 0;
3281 line = NULL;
3282 while (max_lines > 0 && nprinted < max_lines) {
3283 linelen = getline(&line, &linesize, s->f);
3284 if (linelen == -1) {
3285 if (feof(s->f)) {
3286 s->eof = 1;
3287 break;
3289 free(line);
3290 return got_ferror(s->f, GOT_ERR_IO);
3293 /* Set view->maxx based on full line length. */
3294 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3295 view->x ? 1 : 0);
3296 if (err) {
3297 free(line);
3298 return err;
3300 view->maxx = MAX(view->maxx, width);
3301 free(wline);
3302 wline = NULL;
3304 tc = match_color(&s->colors, line);
3305 if (tc)
3306 wattr_on(view->window,
3307 COLOR_PAIR(tc->colorpair), NULL);
3308 if (s->first_displayed_line + nprinted == s->matched_line &&
3309 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3310 err = add_matched_line(&width, line, view->ncols, 0,
3311 view->window, view->x, regmatch);
3312 if (err) {
3313 free(line);
3314 return err;
3316 } else {
3317 int skip;
3318 err = format_line(&wline, &width, &skip, line,
3319 view->x, view->ncols, 0, view->x ? 1 : 0);
3320 if (err) {
3321 free(line);
3322 return err;
3324 waddwstr(view->window, &wline[skip]);
3325 free(wline);
3326 wline = NULL;
3328 if (tc)
3329 wattr_off(view->window,
3330 COLOR_PAIR(tc->colorpair), NULL);
3331 if (width <= view->ncols - 1)
3332 waddch(view->window, '\n');
3333 nprinted++;
3335 free(line);
3336 if (nprinted >= 1)
3337 s->last_displayed_line = s->first_displayed_line +
3338 (nprinted - 1);
3339 else
3340 s->last_displayed_line = s->first_displayed_line;
3342 view_vborder(view);
3344 if (s->eof) {
3345 while (nprinted < view->nlines) {
3346 waddch(view->window, '\n');
3347 nprinted++;
3350 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3351 view->ncols, 0, 0);
3352 if (err) {
3353 return err;
3356 wstandout(view->window);
3357 waddwstr(view->window, wline);
3358 free(wline);
3359 wline = NULL;
3360 wstandend(view->window);
3363 return NULL;
3366 static char *
3367 get_datestr(time_t *time, char *datebuf)
3369 struct tm mytm, *tm;
3370 char *p, *s;
3372 tm = gmtime_r(time, &mytm);
3373 if (tm == NULL)
3374 return NULL;
3375 s = asctime_r(tm, datebuf);
3376 if (s == NULL)
3377 return NULL;
3378 p = strchr(s, '\n');
3379 if (p)
3380 *p = '\0';
3381 return s;
3384 static const struct got_error *
3385 get_changed_paths(struct got_pathlist_head *paths,
3386 struct got_commit_object *commit, struct got_repository *repo)
3388 const struct got_error *err = NULL;
3389 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3390 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3391 struct got_object_qid *qid;
3393 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3394 if (qid != NULL) {
3395 struct got_commit_object *pcommit;
3396 err = got_object_open_as_commit(&pcommit, repo,
3397 &qid->id);
3398 if (err)
3399 return err;
3401 tree_id1 = got_object_id_dup(
3402 got_object_commit_get_tree_id(pcommit));
3403 if (tree_id1 == NULL) {
3404 got_object_commit_close(pcommit);
3405 return got_error_from_errno("got_object_id_dup");
3407 got_object_commit_close(pcommit);
3411 if (tree_id1) {
3412 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3413 if (err)
3414 goto done;
3417 tree_id2 = got_object_commit_get_tree_id(commit);
3418 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3419 if (err)
3420 goto done;
3422 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3423 got_diff_tree_collect_changed_paths, paths, 0);
3424 done:
3425 if (tree1)
3426 got_object_tree_close(tree1);
3427 if (tree2)
3428 got_object_tree_close(tree2);
3429 free(tree_id1);
3430 return err;
3433 static const struct got_error *
3434 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3436 off_t *p;
3438 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3439 if (p == NULL)
3440 return got_error_from_errno("reallocarray");
3441 *line_offsets = p;
3442 (*line_offsets)[*nlines] = off;
3443 (*nlines)++;
3444 return NULL;
3447 static const struct got_error *
3448 write_commit_info(off_t **line_offsets, size_t *nlines,
3449 struct got_object_id *commit_id, struct got_reflist_head *refs,
3450 struct got_repository *repo, FILE *outfile)
3452 const struct got_error *err = NULL;
3453 char datebuf[26], *datestr;
3454 struct got_commit_object *commit;
3455 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3456 time_t committer_time;
3457 const char *author, *committer;
3458 char *refs_str = NULL;
3459 struct got_pathlist_head changed_paths;
3460 struct got_pathlist_entry *pe;
3461 off_t outoff = 0;
3462 int n;
3464 TAILQ_INIT(&changed_paths);
3466 if (refs) {
3467 err = build_refs_str(&refs_str, refs, commit_id, repo);
3468 if (err)
3469 return err;
3472 err = got_object_open_as_commit(&commit, repo, commit_id);
3473 if (err)
3474 return err;
3476 err = got_object_id_str(&id_str, commit_id);
3477 if (err) {
3478 err = got_error_from_errno("got_object_id_str");
3479 goto done;
3482 err = add_line_offset(line_offsets, nlines, 0);
3483 if (err)
3484 goto done;
3486 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3487 refs_str ? refs_str : "", refs_str ? ")" : "");
3488 if (n < 0) {
3489 err = got_error_from_errno("fprintf");
3490 goto done;
3492 outoff += n;
3493 err = add_line_offset(line_offsets, nlines, outoff);
3494 if (err)
3495 goto done;
3497 n = fprintf(outfile, "from: %s\n",
3498 got_object_commit_get_author(commit));
3499 if (n < 0) {
3500 err = got_error_from_errno("fprintf");
3501 goto done;
3503 outoff += n;
3504 err = add_line_offset(line_offsets, nlines, outoff);
3505 if (err)
3506 goto done;
3508 committer_time = got_object_commit_get_committer_time(commit);
3509 datestr = get_datestr(&committer_time, datebuf);
3510 if (datestr) {
3511 n = fprintf(outfile, "date: %s UTC\n", datestr);
3512 if (n < 0) {
3513 err = got_error_from_errno("fprintf");
3514 goto done;
3516 outoff += n;
3517 err = add_line_offset(line_offsets, nlines, outoff);
3518 if (err)
3519 goto done;
3521 author = got_object_commit_get_author(commit);
3522 committer = got_object_commit_get_committer(commit);
3523 if (strcmp(author, committer) != 0) {
3524 n = fprintf(outfile, "via: %s\n", committer);
3525 if (n < 0) {
3526 err = got_error_from_errno("fprintf");
3527 goto done;
3529 outoff += n;
3530 err = add_line_offset(line_offsets, nlines, outoff);
3531 if (err)
3532 goto done;
3534 if (got_object_commit_get_nparents(commit) > 1) {
3535 const struct got_object_id_queue *parent_ids;
3536 struct got_object_qid *qid;
3537 int pn = 1;
3538 parent_ids = got_object_commit_get_parent_ids(commit);
3539 STAILQ_FOREACH(qid, parent_ids, entry) {
3540 err = got_object_id_str(&id_str, &qid->id);
3541 if (err)
3542 goto done;
3543 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3544 if (n < 0) {
3545 err = got_error_from_errno("fprintf");
3546 goto done;
3548 outoff += n;
3549 err = add_line_offset(line_offsets, nlines, outoff);
3550 if (err)
3551 goto done;
3552 free(id_str);
3553 id_str = NULL;
3557 err = got_object_commit_get_logmsg(&logmsg, commit);
3558 if (err)
3559 goto done;
3560 s = logmsg;
3561 while ((line = strsep(&s, "\n")) != NULL) {
3562 n = fprintf(outfile, "%s\n", line);
3563 if (n < 0) {
3564 err = got_error_from_errno("fprintf");
3565 goto done;
3567 outoff += n;
3568 err = add_line_offset(line_offsets, nlines, outoff);
3569 if (err)
3570 goto done;
3573 err = get_changed_paths(&changed_paths, commit, repo);
3574 if (err)
3575 goto done;
3576 TAILQ_FOREACH(pe, &changed_paths, entry) {
3577 struct got_diff_changed_path *cp = pe->data;
3578 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3579 if (n < 0) {
3580 err = got_error_from_errno("fprintf");
3581 goto done;
3583 outoff += n;
3584 err = add_line_offset(line_offsets, nlines, outoff);
3585 if (err)
3586 goto done;
3587 free((char *)pe->path);
3588 free(pe->data);
3591 fputc('\n', outfile);
3592 outoff++;
3593 err = add_line_offset(line_offsets, nlines, outoff);
3594 done:
3595 got_pathlist_free(&changed_paths);
3596 free(id_str);
3597 free(logmsg);
3598 free(refs_str);
3599 got_object_commit_close(commit);
3600 if (err) {
3601 free(*line_offsets);
3602 *line_offsets = NULL;
3603 *nlines = 0;
3605 return err;
3608 static const struct got_error *
3609 create_diff(struct tog_diff_view_state *s)
3611 const struct got_error *err = NULL;
3612 FILE *f = NULL;
3613 int obj_type;
3615 free(s->line_offsets);
3616 s->line_offsets = malloc(sizeof(off_t));
3617 if (s->line_offsets == NULL)
3618 return got_error_from_errno("malloc");
3619 s->nlines = 0;
3621 f = got_opentemp();
3622 if (f == NULL) {
3623 err = got_error_from_errno("got_opentemp");
3624 goto done;
3626 if (s->f && fclose(s->f) == EOF) {
3627 err = got_error_from_errno("fclose");
3628 goto done;
3630 s->f = f;
3632 if (s->id1)
3633 err = got_object_get_type(&obj_type, s->repo, s->id1);
3634 else
3635 err = got_object_get_type(&obj_type, s->repo, s->id2);
3636 if (err)
3637 goto done;
3639 switch (obj_type) {
3640 case GOT_OBJ_TYPE_BLOB:
3641 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3642 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3643 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3644 s->repo, s->f);
3645 break;
3646 case GOT_OBJ_TYPE_TREE:
3647 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3648 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3649 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3650 break;
3651 case GOT_OBJ_TYPE_COMMIT: {
3652 const struct got_object_id_queue *parent_ids;
3653 struct got_object_qid *pid;
3654 struct got_commit_object *commit2;
3655 struct got_reflist_head *refs;
3657 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3658 if (err)
3659 goto done;
3660 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3661 /* Show commit info if we're diffing to a parent/root commit. */
3662 if (s->id1 == NULL) {
3663 err = write_commit_info(&s->line_offsets, &s->nlines,
3664 s->id2, refs, s->repo, s->f);
3665 if (err)
3666 goto done;
3667 } else {
3668 parent_ids = got_object_commit_get_parent_ids(commit2);
3669 STAILQ_FOREACH(pid, parent_ids, entry) {
3670 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3671 err = write_commit_info(
3672 &s->line_offsets, &s->nlines,
3673 s->id2, refs, s->repo, s->f);
3674 if (err)
3675 goto done;
3676 break;
3680 got_object_commit_close(commit2);
3682 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3683 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3684 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3685 break;
3687 default:
3688 err = got_error(GOT_ERR_OBJ_TYPE);
3689 break;
3691 if (err)
3692 goto done;
3693 done:
3694 if (s->f && fflush(s->f) != 0 && err == NULL)
3695 err = got_error_from_errno("fflush");
3696 return err;
3699 static void
3700 diff_view_indicate_progress(struct tog_view *view)
3702 mvwaddstr(view->window, 0, 0, "diffing...");
3703 update_panels();
3704 doupdate();
3707 static const struct got_error *
3708 search_start_diff_view(struct tog_view *view)
3710 struct tog_diff_view_state *s = &view->state.diff;
3712 s->matched_line = 0;
3713 return NULL;
3716 static const struct got_error *
3717 search_next_diff_view(struct tog_view *view)
3719 struct tog_diff_view_state *s = &view->state.diff;
3720 const struct got_error *err = NULL;
3721 int lineno;
3722 char *line = NULL;
3723 size_t linesize = 0;
3724 ssize_t linelen;
3726 if (!view->searching) {
3727 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3728 return NULL;
3731 if (s->matched_line) {
3732 if (view->searching == TOG_SEARCH_FORWARD)
3733 lineno = s->matched_line + 1;
3734 else
3735 lineno = s->matched_line - 1;
3736 } else
3737 lineno = s->first_displayed_line;
3739 while (1) {
3740 off_t offset;
3742 if (lineno <= 0 || lineno > s->nlines) {
3743 if (s->matched_line == 0) {
3744 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3745 break;
3748 if (view->searching == TOG_SEARCH_FORWARD)
3749 lineno = 1;
3750 else
3751 lineno = s->nlines;
3754 offset = s->line_offsets[lineno - 1];
3755 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3756 free(line);
3757 return got_error_from_errno("fseeko");
3759 linelen = getline(&line, &linesize, s->f);
3760 if (linelen != -1) {
3761 char *exstr;
3762 err = expand_tab(&exstr, line);
3763 if (err)
3764 break;
3765 if (match_line(exstr, &view->regex, 1,
3766 &view->regmatch)) {
3767 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3768 s->matched_line = lineno;
3769 free(exstr);
3770 break;
3772 free(exstr);
3774 if (view->searching == TOG_SEARCH_FORWARD)
3775 lineno++;
3776 else
3777 lineno--;
3779 free(line);
3781 if (s->matched_line) {
3782 s->first_displayed_line = s->matched_line;
3783 s->selected_line = 1;
3786 return err;
3789 static const struct got_error *
3790 close_diff_view(struct tog_view *view)
3792 const struct got_error *err = NULL;
3793 struct tog_diff_view_state *s = &view->state.diff;
3795 free(s->id1);
3796 s->id1 = NULL;
3797 free(s->id2);
3798 s->id2 = NULL;
3799 if (s->f && fclose(s->f) == EOF)
3800 err = got_error_from_errno("fclose");
3801 s->f = NULL;
3802 if (s->f1 && fclose(s->f1) == EOF)
3803 err = got_error_from_errno("fclose");
3804 s->f1 = NULL;
3805 if (s->f2 && fclose(s->f2) == EOF)
3806 err = got_error_from_errno("fclose");
3807 s->f2 = NULL;
3808 free_colors(&s->colors);
3809 free(s->line_offsets);
3810 s->line_offsets = NULL;
3811 s->nlines = 0;
3812 return err;
3815 static const struct got_error *
3816 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3817 struct got_object_id *id2, const char *label1, const char *label2,
3818 int diff_context, int ignore_whitespace, int force_text_diff,
3819 struct tog_view *log_view, struct got_repository *repo)
3821 const struct got_error *err;
3822 struct tog_diff_view_state *s = &view->state.diff;
3824 memset(s, 0, sizeof(*s));
3826 if (id1 != NULL && id2 != NULL) {
3827 int type1, type2;
3828 err = got_object_get_type(&type1, repo, id1);
3829 if (err)
3830 return err;
3831 err = got_object_get_type(&type2, repo, id2);
3832 if (err)
3833 return err;
3835 if (type1 != type2)
3836 return got_error(GOT_ERR_OBJ_TYPE);
3838 s->first_displayed_line = 1;
3839 s->last_displayed_line = view->nlines;
3840 s->selected_line = 1;
3841 s->repo = repo;
3842 s->id1 = id1;
3843 s->id2 = id2;
3844 s->label1 = label1;
3845 s->label2 = label2;
3847 if (id1) {
3848 s->id1 = got_object_id_dup(id1);
3849 if (s->id1 == NULL)
3850 return got_error_from_errno("got_object_id_dup");
3851 } else
3852 s->id1 = NULL;
3854 s->id2 = got_object_id_dup(id2);
3855 if (s->id2 == NULL) {
3856 err = got_error_from_errno("got_object_id_dup");
3857 goto done;
3860 s->f1 = got_opentemp();
3861 if (s->f1 == NULL) {
3862 err = got_error_from_errno("got_opentemp");
3863 goto done;
3866 s->f2 = got_opentemp();
3867 if (s->f2 == NULL) {
3868 err = got_error_from_errno("got_opentemp");
3869 goto done;
3872 s->first_displayed_line = 1;
3873 s->last_displayed_line = view->nlines;
3874 s->diff_context = diff_context;
3875 s->ignore_whitespace = ignore_whitespace;
3876 s->force_text_diff = force_text_diff;
3877 s->log_view = log_view;
3878 s->repo = repo;
3880 STAILQ_INIT(&s->colors);
3881 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3882 err = add_color(&s->colors,
3883 "^-", TOG_COLOR_DIFF_MINUS,
3884 get_color_value("TOG_COLOR_DIFF_MINUS"));
3885 if (err)
3886 goto done;
3887 err = add_color(&s->colors, "^\\+",
3888 TOG_COLOR_DIFF_PLUS,
3889 get_color_value("TOG_COLOR_DIFF_PLUS"));
3890 if (err)
3891 goto done;
3892 err = add_color(&s->colors,
3893 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3894 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3895 if (err)
3896 goto done;
3898 err = add_color(&s->colors,
3899 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3900 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3901 get_color_value("TOG_COLOR_DIFF_META"));
3902 if (err)
3903 goto done;
3905 err = add_color(&s->colors,
3906 "^(from|via): ", TOG_COLOR_AUTHOR,
3907 get_color_value("TOG_COLOR_AUTHOR"));
3908 if (err)
3909 goto done;
3911 err = add_color(&s->colors,
3912 "^date: ", TOG_COLOR_DATE,
3913 get_color_value("TOG_COLOR_DATE"));
3914 if (err)
3915 goto done;
3918 if (log_view && view_is_splitscreen(view))
3919 show_log_view(log_view); /* draw vborder */
3920 diff_view_indicate_progress(view);
3922 err = create_diff(s);
3924 view->show = show_diff_view;
3925 view->input = input_diff_view;
3926 view->close = close_diff_view;
3927 view->search_start = search_start_diff_view;
3928 view->search_next = search_next_diff_view;
3929 done:
3930 if (err)
3931 close_diff_view(view);
3932 return err;
3935 static const struct got_error *
3936 show_diff_view(struct tog_view *view)
3938 const struct got_error *err;
3939 struct tog_diff_view_state *s = &view->state.diff;
3940 char *id_str1 = NULL, *id_str2, *header;
3941 const char *label1, *label2;
3943 if (s->id1) {
3944 err = got_object_id_str(&id_str1, s->id1);
3945 if (err)
3946 return err;
3947 label1 = s->label1 ? : id_str1;
3948 } else
3949 label1 = "/dev/null";
3951 err = got_object_id_str(&id_str2, s->id2);
3952 if (err)
3953 return err;
3954 label2 = s->label2 ? : id_str2;
3956 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3957 err = got_error_from_errno("asprintf");
3958 free(id_str1);
3959 free(id_str2);
3960 return err;
3962 free(id_str1);
3963 free(id_str2);
3965 err = draw_file(view, header);
3966 free(header);
3967 return err;
3970 static const struct got_error *
3971 set_selected_commit(struct tog_diff_view_state *s,
3972 struct commit_queue_entry *entry)
3974 const struct got_error *err;
3975 const struct got_object_id_queue *parent_ids;
3976 struct got_commit_object *selected_commit;
3977 struct got_object_qid *pid;
3979 free(s->id2);
3980 s->id2 = got_object_id_dup(entry->id);
3981 if (s->id2 == NULL)
3982 return got_error_from_errno("got_object_id_dup");
3984 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3985 if (err)
3986 return err;
3987 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3988 free(s->id1);
3989 pid = STAILQ_FIRST(parent_ids);
3990 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3991 got_object_commit_close(selected_commit);
3992 return NULL;
3995 static const struct got_error *
3996 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3998 const struct got_error *err = NULL;
3999 struct tog_diff_view_state *s = &view->state.diff;
4000 struct tog_log_view_state *ls;
4001 struct commit_queue_entry *old_selected_entry;
4002 char *line = NULL;
4003 size_t linesize = 0;
4004 ssize_t linelen;
4005 int i, nscroll = view->nlines - 1;
4007 switch (ch) {
4008 case '0':
4009 view->x = 0;
4010 break;
4011 case '$':
4012 view->x = MAX(view->maxx - view->ncols / 3, 0);
4013 break;
4014 case KEY_RIGHT:
4015 case 'l':
4016 if (view->x + view->ncols / 3 < view->maxx)
4017 view->x += 2; /* move two columns right */
4018 break;
4019 case KEY_LEFT:
4020 case 'h':
4021 view->x -= MIN(view->x, 2); /* move two columns back */
4022 break;
4023 case 'a':
4024 case 'w':
4025 if (ch == 'a')
4026 s->force_text_diff = !s->force_text_diff;
4027 if (ch == 'w')
4028 s->ignore_whitespace = !s->ignore_whitespace;
4029 wclear(view->window);
4030 s->first_displayed_line = 1;
4031 s->last_displayed_line = view->nlines;
4032 s->matched_line = 0;
4033 diff_view_indicate_progress(view);
4034 err = create_diff(s);
4035 break;
4036 case 'g':
4037 case KEY_HOME:
4038 s->first_displayed_line = 1;
4039 break;
4040 case 'G':
4041 case KEY_END:
4042 if (s->eof)
4043 break;
4045 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4046 s->eof = 1;
4047 break;
4048 case 'k':
4049 case KEY_UP:
4050 case CTRL('p'):
4051 if (s->first_displayed_line > 1)
4052 s->first_displayed_line--;
4053 break;
4054 case CTRL('u'):
4055 case 'u':
4056 nscroll /= 2;
4057 /* FALL THROUGH */
4058 case KEY_PPAGE:
4059 case CTRL('b'):
4060 if (s->first_displayed_line == 1)
4061 break;
4062 i = 0;
4063 while (i++ < nscroll && s->first_displayed_line > 1)
4064 s->first_displayed_line--;
4065 break;
4066 case 'j':
4067 case KEY_DOWN:
4068 case CTRL('n'):
4069 if (!s->eof)
4070 s->first_displayed_line++;
4071 break;
4072 case CTRL('d'):
4073 case 'd':
4074 nscroll /= 2;
4075 /* FALL THROUGH */
4076 case KEY_NPAGE:
4077 case CTRL('f'):
4078 case ' ':
4079 if (s->eof)
4080 break;
4081 i = 0;
4082 while (!s->eof && i++ < nscroll) {
4083 linelen = getline(&line, &linesize, s->f);
4084 s->first_displayed_line++;
4085 if (linelen == -1) {
4086 if (feof(s->f)) {
4087 s->eof = 1;
4088 } else
4089 err = got_ferror(s->f, GOT_ERR_IO);
4090 break;
4093 free(line);
4094 break;
4095 case '[':
4096 if (s->diff_context > 0) {
4097 s->diff_context--;
4098 s->matched_line = 0;
4099 diff_view_indicate_progress(view);
4100 err = create_diff(s);
4101 if (s->first_displayed_line + view->nlines - 1 >
4102 s->nlines) {
4103 s->first_displayed_line = 1;
4104 s->last_displayed_line = view->nlines;
4107 break;
4108 case ']':
4109 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4110 s->diff_context++;
4111 s->matched_line = 0;
4112 diff_view_indicate_progress(view);
4113 err = create_diff(s);
4115 break;
4116 case '<':
4117 case ',':
4118 if (s->log_view == NULL)
4119 break;
4120 ls = &s->log_view->state.log;
4121 old_selected_entry = ls->selected_entry;
4123 err = input_log_view(NULL, s->log_view, KEY_UP);
4124 if (err)
4125 break;
4127 if (old_selected_entry == ls->selected_entry)
4128 break;
4130 err = set_selected_commit(s, ls->selected_entry);
4131 if (err)
4132 break;
4134 s->first_displayed_line = 1;
4135 s->last_displayed_line = view->nlines;
4136 s->matched_line = 0;
4137 view->x = 0;
4139 diff_view_indicate_progress(view);
4140 err = create_diff(s);
4141 break;
4142 case '>':
4143 case '.':
4144 if (s->log_view == NULL)
4145 break;
4146 ls = &s->log_view->state.log;
4147 old_selected_entry = ls->selected_entry;
4149 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4150 if (err)
4151 break;
4153 if (old_selected_entry == ls->selected_entry)
4154 break;
4156 err = set_selected_commit(s, ls->selected_entry);
4157 if (err)
4158 break;
4160 s->first_displayed_line = 1;
4161 s->last_displayed_line = view->nlines;
4162 s->matched_line = 0;
4163 view->x = 0;
4165 diff_view_indicate_progress(view);
4166 err = create_diff(s);
4167 break;
4168 default:
4169 break;
4172 return err;
4175 static const struct got_error *
4176 cmd_diff(int argc, char *argv[])
4178 const struct got_error *error = NULL;
4179 struct got_repository *repo = NULL;
4180 struct got_worktree *worktree = NULL;
4181 struct got_object_id *id1 = NULL, *id2 = NULL;
4182 char *repo_path = NULL, *cwd = NULL;
4183 char *id_str1 = NULL, *id_str2 = NULL;
4184 char *label1 = NULL, *label2 = NULL;
4185 int diff_context = 3, ignore_whitespace = 0;
4186 int ch, force_text_diff = 0;
4187 const char *errstr;
4188 struct tog_view *view;
4189 int *pack_fds = NULL;
4191 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4192 switch (ch) {
4193 case 'a':
4194 force_text_diff = 1;
4195 break;
4196 case 'C':
4197 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4198 &errstr);
4199 if (errstr != NULL)
4200 errx(1, "number of context lines is %s: %s",
4201 errstr, errstr);
4202 break;
4203 case 'r':
4204 repo_path = realpath(optarg, NULL);
4205 if (repo_path == NULL)
4206 return got_error_from_errno2("realpath",
4207 optarg);
4208 got_path_strip_trailing_slashes(repo_path);
4209 break;
4210 case 'w':
4211 ignore_whitespace = 1;
4212 break;
4213 default:
4214 usage_diff();
4215 /* NOTREACHED */
4219 argc -= optind;
4220 argv += optind;
4222 if (argc == 0) {
4223 usage_diff(); /* TODO show local worktree changes */
4224 } else if (argc == 2) {
4225 id_str1 = argv[0];
4226 id_str2 = argv[1];
4227 } else
4228 usage_diff();
4230 error = got_repo_pack_fds_open(&pack_fds);
4231 if (error)
4232 goto done;
4234 if (repo_path == NULL) {
4235 cwd = getcwd(NULL, 0);
4236 if (cwd == NULL)
4237 return got_error_from_errno("getcwd");
4238 error = got_worktree_open(&worktree, cwd);
4239 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4240 goto done;
4241 if (worktree)
4242 repo_path =
4243 strdup(got_worktree_get_repo_path(worktree));
4244 else
4245 repo_path = strdup(cwd);
4246 if (repo_path == NULL) {
4247 error = got_error_from_errno("strdup");
4248 goto done;
4252 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4253 if (error)
4254 goto done;
4256 init_curses();
4258 error = apply_unveil(got_repo_get_path(repo), NULL);
4259 if (error)
4260 goto done;
4262 error = tog_load_refs(repo, 0);
4263 if (error)
4264 goto done;
4266 error = got_repo_match_object_id(&id1, &label1, id_str1,
4267 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4268 if (error)
4269 goto done;
4271 error = got_repo_match_object_id(&id2, &label2, id_str2,
4272 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4273 if (error)
4274 goto done;
4276 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4277 if (view == NULL) {
4278 error = got_error_from_errno("view_open");
4279 goto done;
4281 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4282 ignore_whitespace, force_text_diff, NULL, repo);
4283 if (error)
4284 goto done;
4285 error = view_loop(view);
4286 done:
4287 free(label1);
4288 free(label2);
4289 free(repo_path);
4290 free(cwd);
4291 if (repo) {
4292 const struct got_error *close_err = got_repo_close(repo);
4293 if (error == NULL)
4294 error = close_err;
4296 if (worktree)
4297 got_worktree_close(worktree);
4298 if (pack_fds) {
4299 const struct got_error *pack_err =
4300 got_repo_pack_fds_close(pack_fds);
4301 if (error == NULL)
4302 error = pack_err;
4304 tog_free_refs();
4305 return error;
4308 __dead static void
4309 usage_blame(void)
4311 endwin();
4312 fprintf(stderr,
4313 "usage: %s blame [-c commit] [-r repository-path] path\n",
4314 getprogname());
4315 exit(1);
4318 struct tog_blame_line {
4319 int annotated;
4320 struct got_object_id *id;
4323 static const struct got_error *
4324 draw_blame(struct tog_view *view)
4326 struct tog_blame_view_state *s = &view->state.blame;
4327 struct tog_blame *blame = &s->blame;
4328 regmatch_t *regmatch = &view->regmatch;
4329 const struct got_error *err;
4330 int lineno = 0, nprinted = 0;
4331 char *line = NULL;
4332 size_t linesize = 0;
4333 ssize_t linelen;
4334 wchar_t *wline;
4335 int width;
4336 struct tog_blame_line *blame_line;
4337 struct got_object_id *prev_id = NULL;
4338 char *id_str;
4339 struct tog_color *tc;
4341 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4342 if (err)
4343 return err;
4345 rewind(blame->f);
4346 werase(view->window);
4348 if (asprintf(&line, "commit %s", id_str) == -1) {
4349 err = got_error_from_errno("asprintf");
4350 free(id_str);
4351 return err;
4354 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4355 free(line);
4356 line = NULL;
4357 if (err)
4358 return err;
4359 if (view_needs_focus_indication(view))
4360 wstandout(view->window);
4361 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4362 if (tc)
4363 wattr_on(view->window,
4364 COLOR_PAIR(tc->colorpair), NULL);
4365 waddwstr(view->window, wline);
4366 if (tc)
4367 wattr_off(view->window,
4368 COLOR_PAIR(tc->colorpair), NULL);
4369 if (view_needs_focus_indication(view))
4370 wstandend(view->window);
4371 free(wline);
4372 wline = NULL;
4373 if (width < view->ncols - 1)
4374 waddch(view->window, '\n');
4376 if (asprintf(&line, "[%d/%d] %s%s",
4377 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4378 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4379 free(id_str);
4380 return got_error_from_errno("asprintf");
4382 free(id_str);
4383 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4384 free(line);
4385 line = NULL;
4386 if (err)
4387 return err;
4388 waddwstr(view->window, wline);
4389 free(wline);
4390 wline = NULL;
4391 if (width < view->ncols - 1)
4392 waddch(view->window, '\n');
4394 s->eof = 0;
4395 view->maxx = 0;
4396 while (nprinted < view->nlines - 2) {
4397 linelen = getline(&line, &linesize, blame->f);
4398 if (linelen == -1) {
4399 if (feof(blame->f)) {
4400 s->eof = 1;
4401 break;
4403 free(line);
4404 return got_ferror(blame->f, GOT_ERR_IO);
4406 if (++lineno < s->first_displayed_line)
4407 continue;
4409 /* Set view->maxx based on full line length. */
4410 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4411 if (err) {
4412 free(line);
4413 return err;
4415 free(wline);
4416 wline = NULL;
4417 view->maxx = MAX(view->maxx, width);
4419 if (view->focussed && nprinted == s->selected_line - 1)
4420 wstandout(view->window);
4422 if (blame->nlines > 0) {
4423 blame_line = &blame->lines[lineno - 1];
4424 if (blame_line->annotated && prev_id &&
4425 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4426 !(view->focussed &&
4427 nprinted == s->selected_line - 1)) {
4428 waddstr(view->window, " ");
4429 } else if (blame_line->annotated) {
4430 char *id_str;
4431 err = got_object_id_str(&id_str,
4432 blame_line->id);
4433 if (err) {
4434 free(line);
4435 return err;
4437 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4438 if (tc)
4439 wattr_on(view->window,
4440 COLOR_PAIR(tc->colorpair), NULL);
4441 wprintw(view->window, "%.8s", id_str);
4442 if (tc)
4443 wattr_off(view->window,
4444 COLOR_PAIR(tc->colorpair), NULL);
4445 free(id_str);
4446 prev_id = blame_line->id;
4447 } else {
4448 waddstr(view->window, "........");
4449 prev_id = NULL;
4451 } else {
4452 waddstr(view->window, "........");
4453 prev_id = NULL;
4456 if (view->focussed && nprinted == s->selected_line - 1)
4457 wstandend(view->window);
4458 waddstr(view->window, " ");
4460 if (view->ncols <= 9) {
4461 width = 9;
4462 } else if (s->first_displayed_line + nprinted ==
4463 s->matched_line &&
4464 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4465 err = add_matched_line(&width, line, view->ncols - 9, 9,
4466 view->window, view->x, regmatch);
4467 if (err) {
4468 free(line);
4469 return err;
4471 width += 9;
4472 } else {
4473 int skip;
4474 err = format_line(&wline, &width, &skip, line,
4475 view->x, view->ncols - 9, 9, 1);
4476 if (err) {
4477 free(line);
4478 return err;
4480 waddwstr(view->window, &wline[skip]);
4481 width += 9;
4482 free(wline);
4483 wline = NULL;
4486 if (width <= view->ncols - 1)
4487 waddch(view->window, '\n');
4488 if (++nprinted == 1)
4489 s->first_displayed_line = lineno;
4491 free(line);
4492 s->last_displayed_line = lineno;
4494 view_vborder(view);
4496 return NULL;
4499 static const struct got_error *
4500 blame_cb(void *arg, int nlines, int lineno,
4501 struct got_commit_object *commit, struct got_object_id *id)
4503 const struct got_error *err = NULL;
4504 struct tog_blame_cb_args *a = arg;
4505 struct tog_blame_line *line;
4506 int errcode;
4508 if (nlines != a->nlines ||
4509 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4510 return got_error(GOT_ERR_RANGE);
4512 errcode = pthread_mutex_lock(&tog_mutex);
4513 if (errcode)
4514 return got_error_set_errno(errcode, "pthread_mutex_lock");
4516 if (*a->quit) { /* user has quit the blame view */
4517 err = got_error(GOT_ERR_ITER_COMPLETED);
4518 goto done;
4521 if (lineno == -1)
4522 goto done; /* no change in this commit */
4524 line = &a->lines[lineno - 1];
4525 if (line->annotated)
4526 goto done;
4528 line->id = got_object_id_dup(id);
4529 if (line->id == NULL) {
4530 err = got_error_from_errno("got_object_id_dup");
4531 goto done;
4533 line->annotated = 1;
4534 done:
4535 errcode = pthread_mutex_unlock(&tog_mutex);
4536 if (errcode)
4537 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4538 return err;
4541 static void *
4542 blame_thread(void *arg)
4544 const struct got_error *err, *close_err;
4545 struct tog_blame_thread_args *ta = arg;
4546 struct tog_blame_cb_args *a = ta->cb_args;
4547 int errcode;
4549 err = block_signals_used_by_main_thread();
4550 if (err)
4551 return (void *)err;
4553 err = got_blame(ta->path, a->commit_id, ta->repo,
4554 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4555 if (err && err->code == GOT_ERR_CANCELLED)
4556 err = NULL;
4558 errcode = pthread_mutex_lock(&tog_mutex);
4559 if (errcode)
4560 return (void *)got_error_set_errno(errcode,
4561 "pthread_mutex_lock");
4563 close_err = got_repo_close(ta->repo);
4564 if (err == NULL)
4565 err = close_err;
4566 ta->repo = NULL;
4567 *ta->complete = 1;
4569 errcode = pthread_mutex_unlock(&tog_mutex);
4570 if (errcode && err == NULL)
4571 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4573 return (void *)err;
4576 static struct got_object_id *
4577 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4578 int first_displayed_line, int selected_line)
4580 struct tog_blame_line *line;
4582 if (nlines <= 0)
4583 return NULL;
4585 line = &lines[first_displayed_line - 1 + selected_line - 1];
4586 if (!line->annotated)
4587 return NULL;
4589 return line->id;
4592 static const struct got_error *
4593 stop_blame(struct tog_blame *blame)
4595 const struct got_error *err = NULL;
4596 int i;
4598 if (blame->thread) {
4599 int errcode;
4600 errcode = pthread_mutex_unlock(&tog_mutex);
4601 if (errcode)
4602 return got_error_set_errno(errcode,
4603 "pthread_mutex_unlock");
4604 errcode = pthread_join(blame->thread, (void **)&err);
4605 if (errcode)
4606 return got_error_set_errno(errcode, "pthread_join");
4607 errcode = pthread_mutex_lock(&tog_mutex);
4608 if (errcode)
4609 return got_error_set_errno(errcode,
4610 "pthread_mutex_lock");
4611 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4612 err = NULL;
4613 blame->thread = NULL;
4615 if (blame->thread_args.repo) {
4616 const struct got_error *close_err;
4617 close_err = got_repo_close(blame->thread_args.repo);
4618 if (err == NULL)
4619 err = close_err;
4620 blame->thread_args.repo = NULL;
4622 if (blame->f) {
4623 if (fclose(blame->f) == EOF && err == NULL)
4624 err = got_error_from_errno("fclose");
4625 blame->f = NULL;
4627 if (blame->lines) {
4628 for (i = 0; i < blame->nlines; i++)
4629 free(blame->lines[i].id);
4630 free(blame->lines);
4631 blame->lines = NULL;
4633 free(blame->cb_args.commit_id);
4634 blame->cb_args.commit_id = NULL;
4635 if (blame->pack_fds) {
4636 const struct got_error *pack_err =
4637 got_repo_pack_fds_close(blame->pack_fds);
4638 if (err == NULL)
4639 err = pack_err;
4640 blame->pack_fds = NULL;
4642 return err;
4645 static const struct got_error *
4646 cancel_blame_view(void *arg)
4648 const struct got_error *err = NULL;
4649 int *done = arg;
4650 int errcode;
4652 errcode = pthread_mutex_lock(&tog_mutex);
4653 if (errcode)
4654 return got_error_set_errno(errcode,
4655 "pthread_mutex_unlock");
4657 if (*done)
4658 err = got_error(GOT_ERR_CANCELLED);
4660 errcode = pthread_mutex_unlock(&tog_mutex);
4661 if (errcode)
4662 return got_error_set_errno(errcode,
4663 "pthread_mutex_lock");
4665 return err;
4668 static const struct got_error *
4669 run_blame(struct tog_view *view)
4671 struct tog_blame_view_state *s = &view->state.blame;
4672 struct tog_blame *blame = &s->blame;
4673 const struct got_error *err = NULL;
4674 struct got_commit_object *commit = NULL;
4675 struct got_blob_object *blob = NULL;
4676 struct got_repository *thread_repo = NULL;
4677 struct got_object_id *obj_id = NULL;
4678 int obj_type;
4679 int *pack_fds = NULL;
4681 err = got_object_open_as_commit(&commit, s->repo,
4682 &s->blamed_commit->id);
4683 if (err)
4684 return err;
4686 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4687 if (err)
4688 goto done;
4690 err = got_object_get_type(&obj_type, s->repo, obj_id);
4691 if (err)
4692 goto done;
4694 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4695 err = got_error(GOT_ERR_OBJ_TYPE);
4696 goto done;
4699 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4700 if (err)
4701 goto done;
4702 blame->f = got_opentemp();
4703 if (blame->f == NULL) {
4704 err = got_error_from_errno("got_opentemp");
4705 goto done;
4707 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4708 &blame->line_offsets, blame->f, blob);
4709 if (err)
4710 goto done;
4711 if (blame->nlines == 0) {
4712 s->blame_complete = 1;
4713 goto done;
4716 /* Don't include \n at EOF in the blame line count. */
4717 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4718 blame->nlines--;
4720 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4721 if (blame->lines == NULL) {
4722 err = got_error_from_errno("calloc");
4723 goto done;
4726 err = got_repo_pack_fds_open(&pack_fds);
4727 if (err)
4728 goto done;
4729 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4730 pack_fds);
4731 if (err)
4732 goto done;
4734 blame->pack_fds = pack_fds;
4735 blame->cb_args.view = view;
4736 blame->cb_args.lines = blame->lines;
4737 blame->cb_args.nlines = blame->nlines;
4738 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4739 if (blame->cb_args.commit_id == NULL) {
4740 err = got_error_from_errno("got_object_id_dup");
4741 goto done;
4743 blame->cb_args.quit = &s->done;
4745 blame->thread_args.path = s->path;
4746 blame->thread_args.repo = thread_repo;
4747 blame->thread_args.cb_args = &blame->cb_args;
4748 blame->thread_args.complete = &s->blame_complete;
4749 blame->thread_args.cancel_cb = cancel_blame_view;
4750 blame->thread_args.cancel_arg = &s->done;
4751 s->blame_complete = 0;
4753 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4754 s->first_displayed_line = 1;
4755 s->last_displayed_line = view->nlines;
4756 s->selected_line = 1;
4758 s->matched_line = 0;
4760 done:
4761 if (commit)
4762 got_object_commit_close(commit);
4763 if (blob)
4764 got_object_blob_close(blob);
4765 free(obj_id);
4766 if (err)
4767 stop_blame(blame);
4768 return err;
4771 static const struct got_error *
4772 open_blame_view(struct tog_view *view, char *path,
4773 struct got_object_id *commit_id, struct got_repository *repo)
4775 const struct got_error *err = NULL;
4776 struct tog_blame_view_state *s = &view->state.blame;
4778 STAILQ_INIT(&s->blamed_commits);
4780 s->path = strdup(path);
4781 if (s->path == NULL)
4782 return got_error_from_errno("strdup");
4784 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4785 if (err) {
4786 free(s->path);
4787 return err;
4790 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4791 s->first_displayed_line = 1;
4792 s->last_displayed_line = view->nlines;
4793 s->selected_line = 1;
4794 s->blame_complete = 0;
4795 s->repo = repo;
4796 s->commit_id = commit_id;
4797 memset(&s->blame, 0, sizeof(s->blame));
4799 STAILQ_INIT(&s->colors);
4800 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4801 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4802 get_color_value("TOG_COLOR_COMMIT"));
4803 if (err)
4804 return err;
4807 view->show = show_blame_view;
4808 view->input = input_blame_view;
4809 view->close = close_blame_view;
4810 view->search_start = search_start_blame_view;
4811 view->search_next = search_next_blame_view;
4813 return run_blame(view);
4816 static const struct got_error *
4817 close_blame_view(struct tog_view *view)
4819 const struct got_error *err = NULL;
4820 struct tog_blame_view_state *s = &view->state.blame;
4822 if (s->blame.thread)
4823 err = stop_blame(&s->blame);
4825 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4826 struct got_object_qid *blamed_commit;
4827 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4828 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4829 got_object_qid_free(blamed_commit);
4832 free(s->path);
4833 free_colors(&s->colors);
4834 return err;
4837 static const struct got_error *
4838 search_start_blame_view(struct tog_view *view)
4840 struct tog_blame_view_state *s = &view->state.blame;
4842 s->matched_line = 0;
4843 return NULL;
4846 static const struct got_error *
4847 search_next_blame_view(struct tog_view *view)
4849 struct tog_blame_view_state *s = &view->state.blame;
4850 const struct got_error *err = NULL;
4851 int lineno;
4852 char *line = NULL;
4853 size_t linesize = 0;
4854 ssize_t linelen;
4856 if (!view->searching) {
4857 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4858 return NULL;
4861 if (s->matched_line) {
4862 if (view->searching == TOG_SEARCH_FORWARD)
4863 lineno = s->matched_line + 1;
4864 else
4865 lineno = s->matched_line - 1;
4866 } else
4867 lineno = s->first_displayed_line - 1 + s->selected_line;
4869 while (1) {
4870 off_t offset;
4872 if (lineno <= 0 || lineno > s->blame.nlines) {
4873 if (s->matched_line == 0) {
4874 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4875 break;
4878 if (view->searching == TOG_SEARCH_FORWARD)
4879 lineno = 1;
4880 else
4881 lineno = s->blame.nlines;
4884 offset = s->blame.line_offsets[lineno - 1];
4885 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4886 free(line);
4887 return got_error_from_errno("fseeko");
4889 linelen = getline(&line, &linesize, s->blame.f);
4890 if (linelen != -1) {
4891 char *exstr;
4892 err = expand_tab(&exstr, line);
4893 if (err)
4894 break;
4895 if (match_line(exstr, &view->regex, 1,
4896 &view->regmatch)) {
4897 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4898 s->matched_line = lineno;
4899 free(exstr);
4900 break;
4902 free(exstr);
4904 if (view->searching == TOG_SEARCH_FORWARD)
4905 lineno++;
4906 else
4907 lineno--;
4909 free(line);
4911 if (s->matched_line) {
4912 s->first_displayed_line = s->matched_line;
4913 s->selected_line = 1;
4916 return err;
4919 static const struct got_error *
4920 show_blame_view(struct tog_view *view)
4922 const struct got_error *err = NULL;
4923 struct tog_blame_view_state *s = &view->state.blame;
4924 int errcode;
4926 if (s->blame.thread == NULL && !s->blame_complete) {
4927 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4928 &s->blame.thread_args);
4929 if (errcode)
4930 return got_error_set_errno(errcode, "pthread_create");
4932 halfdelay(1); /* fast refresh while annotating */
4935 if (s->blame_complete)
4936 halfdelay(10); /* disable fast refresh */
4938 err = draw_blame(view);
4940 view_vborder(view);
4941 return err;
4944 static const struct got_error *
4945 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4947 const struct got_error *err = NULL, *thread_err = NULL;
4948 struct tog_view *diff_view;
4949 struct tog_blame_view_state *s = &view->state.blame;
4950 int begin_x = 0, nscroll = view->nlines - 2;
4952 switch (ch) {
4953 case '0':
4954 view->x = 0;
4955 break;
4956 case '$':
4957 view->x = MAX(view->maxx - view->ncols / 3, 0);
4958 break;
4959 case KEY_RIGHT:
4960 case 'l':
4961 if (view->x + view->ncols / 3 < view->maxx)
4962 view->x += 2; /* move two columns right */
4963 break;
4964 case KEY_LEFT:
4965 case 'h':
4966 view->x -= MIN(view->x, 2); /* move two columns back */
4967 break;
4968 case 'q':
4969 s->done = 1;
4970 break;
4971 case 'g':
4972 case KEY_HOME:
4973 s->selected_line = 1;
4974 s->first_displayed_line = 1;
4975 break;
4976 case 'G':
4977 case KEY_END:
4978 if (s->blame.nlines < view->nlines - 2) {
4979 s->selected_line = s->blame.nlines;
4980 s->first_displayed_line = 1;
4981 } else {
4982 s->selected_line = view->nlines - 2;
4983 s->first_displayed_line = s->blame.nlines -
4984 (view->nlines - 3);
4986 break;
4987 case 'k':
4988 case KEY_UP:
4989 case CTRL('p'):
4990 if (s->selected_line > 1)
4991 s->selected_line--;
4992 else if (s->selected_line == 1 &&
4993 s->first_displayed_line > 1)
4994 s->first_displayed_line--;
4995 break;
4996 case CTRL('u'):
4997 case 'u':
4998 nscroll /= 2;
4999 /* FALL THROUGH */
5000 case KEY_PPAGE:
5001 case CTRL('b'):
5002 if (s->first_displayed_line == 1) {
5003 s->selected_line = MAX(1, s->selected_line - nscroll);
5004 break;
5006 if (s->first_displayed_line > nscroll)
5007 s->first_displayed_line -= nscroll;
5008 else
5009 s->first_displayed_line = 1;
5010 break;
5011 case 'j':
5012 case KEY_DOWN:
5013 case CTRL('n'):
5014 if (s->selected_line < view->nlines - 2 &&
5015 s->first_displayed_line +
5016 s->selected_line <= s->blame.nlines)
5017 s->selected_line++;
5018 else if (s->last_displayed_line <
5019 s->blame.nlines)
5020 s->first_displayed_line++;
5021 break;
5022 case 'b':
5023 case 'p': {
5024 struct got_object_id *id = NULL;
5025 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5026 s->first_displayed_line, s->selected_line);
5027 if (id == NULL)
5028 break;
5029 if (ch == 'p') {
5030 struct got_commit_object *commit, *pcommit;
5031 struct got_object_qid *pid;
5032 struct got_object_id *blob_id = NULL;
5033 int obj_type;
5034 err = got_object_open_as_commit(&commit,
5035 s->repo, id);
5036 if (err)
5037 break;
5038 pid = STAILQ_FIRST(
5039 got_object_commit_get_parent_ids(commit));
5040 if (pid == NULL) {
5041 got_object_commit_close(commit);
5042 break;
5044 /* Check if path history ends here. */
5045 err = got_object_open_as_commit(&pcommit,
5046 s->repo, &pid->id);
5047 if (err)
5048 break;
5049 err = got_object_id_by_path(&blob_id, s->repo,
5050 pcommit, s->path);
5051 got_object_commit_close(pcommit);
5052 if (err) {
5053 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5054 err = NULL;
5055 got_object_commit_close(commit);
5056 break;
5058 err = got_object_get_type(&obj_type, s->repo,
5059 blob_id);
5060 free(blob_id);
5061 /* Can't blame non-blob type objects. */
5062 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5063 got_object_commit_close(commit);
5064 break;
5066 err = got_object_qid_alloc(&s->blamed_commit,
5067 &pid->id);
5068 got_object_commit_close(commit);
5069 } else {
5070 if (got_object_id_cmp(id,
5071 &s->blamed_commit->id) == 0)
5072 break;
5073 err = got_object_qid_alloc(&s->blamed_commit,
5074 id);
5076 if (err)
5077 break;
5078 s->done = 1;
5079 thread_err = stop_blame(&s->blame);
5080 s->done = 0;
5081 if (thread_err)
5082 break;
5083 STAILQ_INSERT_HEAD(&s->blamed_commits,
5084 s->blamed_commit, entry);
5085 err = run_blame(view);
5086 if (err)
5087 break;
5088 break;
5090 case 'B': {
5091 struct got_object_qid *first;
5092 first = STAILQ_FIRST(&s->blamed_commits);
5093 if (!got_object_id_cmp(&first->id, s->commit_id))
5094 break;
5095 s->done = 1;
5096 thread_err = stop_blame(&s->blame);
5097 s->done = 0;
5098 if (thread_err)
5099 break;
5100 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5101 got_object_qid_free(s->blamed_commit);
5102 s->blamed_commit =
5103 STAILQ_FIRST(&s->blamed_commits);
5104 err = run_blame(view);
5105 if (err)
5106 break;
5107 break;
5109 case KEY_ENTER:
5110 case '\r': {
5111 struct got_object_id *id = NULL;
5112 struct got_object_qid *pid;
5113 struct got_commit_object *commit = NULL;
5114 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5115 s->first_displayed_line, s->selected_line);
5116 if (id == NULL)
5117 break;
5118 err = got_object_open_as_commit(&commit, s->repo, id);
5119 if (err)
5120 break;
5121 pid = STAILQ_FIRST(
5122 got_object_commit_get_parent_ids(commit));
5123 if (view_is_parent_view(view))
5124 begin_x = view_split_begin_x(view->begin_x);
5125 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5126 if (diff_view == NULL) {
5127 got_object_commit_close(commit);
5128 err = got_error_from_errno("view_open");
5129 break;
5131 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5132 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5133 got_object_commit_close(commit);
5134 if (err) {
5135 view_close(diff_view);
5136 break;
5138 view->focussed = 0;
5139 diff_view->focussed = 1;
5140 if (view_is_parent_view(view)) {
5141 err = view_close_child(view);
5142 if (err)
5143 break;
5144 err = view_set_child(view, diff_view);
5145 if (err)
5146 break;
5147 view->focus_child = 1;
5148 } else
5149 *new_view = diff_view;
5150 if (err)
5151 break;
5152 break;
5154 case CTRL('d'):
5155 case 'd':
5156 nscroll /= 2;
5157 /* FALL THROUGH */
5158 case KEY_NPAGE:
5159 case CTRL('f'):
5160 case ' ':
5161 if (s->last_displayed_line >= s->blame.nlines &&
5162 s->selected_line >= MIN(s->blame.nlines,
5163 view->nlines - 2)) {
5164 break;
5166 if (s->last_displayed_line >= s->blame.nlines &&
5167 s->selected_line < view->nlines - 2) {
5168 s->selected_line +=
5169 MIN(nscroll, s->last_displayed_line -
5170 s->first_displayed_line - s->selected_line + 1);
5172 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5173 s->first_displayed_line += nscroll;
5174 else
5175 s->first_displayed_line =
5176 s->blame.nlines - (view->nlines - 3);
5177 break;
5178 case KEY_RESIZE:
5179 if (s->selected_line > view->nlines - 2) {
5180 s->selected_line = MIN(s->blame.nlines,
5181 view->nlines - 2);
5183 break;
5184 default:
5185 break;
5187 return thread_err ? thread_err : err;
5190 static const struct got_error *
5191 cmd_blame(int argc, char *argv[])
5193 const struct got_error *error;
5194 struct got_repository *repo = NULL;
5195 struct got_worktree *worktree = NULL;
5196 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5197 char *link_target = NULL;
5198 struct got_object_id *commit_id = NULL;
5199 struct got_commit_object *commit = NULL;
5200 char *commit_id_str = NULL;
5201 int ch;
5202 struct tog_view *view;
5203 int *pack_fds = NULL;
5205 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5206 switch (ch) {
5207 case 'c':
5208 commit_id_str = optarg;
5209 break;
5210 case 'r':
5211 repo_path = realpath(optarg, NULL);
5212 if (repo_path == NULL)
5213 return got_error_from_errno2("realpath",
5214 optarg);
5215 break;
5216 default:
5217 usage_blame();
5218 /* NOTREACHED */
5222 argc -= optind;
5223 argv += optind;
5225 if (argc != 1)
5226 usage_blame();
5228 error = got_repo_pack_fds_open(&pack_fds);
5229 if (error != NULL)
5230 goto done;
5232 if (repo_path == NULL) {
5233 cwd = getcwd(NULL, 0);
5234 if (cwd == NULL)
5235 return got_error_from_errno("getcwd");
5236 error = got_worktree_open(&worktree, cwd);
5237 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5238 goto done;
5239 if (worktree)
5240 repo_path =
5241 strdup(got_worktree_get_repo_path(worktree));
5242 else
5243 repo_path = strdup(cwd);
5244 if (repo_path == NULL) {
5245 error = got_error_from_errno("strdup");
5246 goto done;
5250 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5251 if (error != NULL)
5252 goto done;
5254 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5255 worktree);
5256 if (error)
5257 goto done;
5259 init_curses();
5261 error = apply_unveil(got_repo_get_path(repo), NULL);
5262 if (error)
5263 goto done;
5265 error = tog_load_refs(repo, 0);
5266 if (error)
5267 goto done;
5269 if (commit_id_str == NULL) {
5270 struct got_reference *head_ref;
5271 error = got_ref_open(&head_ref, repo, worktree ?
5272 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5273 if (error != NULL)
5274 goto done;
5275 error = got_ref_resolve(&commit_id, repo, head_ref);
5276 got_ref_close(head_ref);
5277 } else {
5278 error = got_repo_match_object_id(&commit_id, NULL,
5279 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5281 if (error != NULL)
5282 goto done;
5284 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5285 if (view == NULL) {
5286 error = got_error_from_errno("view_open");
5287 goto done;
5290 error = got_object_open_as_commit(&commit, repo, commit_id);
5291 if (error)
5292 goto done;
5294 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5295 commit, repo);
5296 if (error)
5297 goto done;
5299 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5300 commit_id, repo);
5301 if (error)
5302 goto done;
5303 if (worktree) {
5304 /* Release work tree lock. */
5305 got_worktree_close(worktree);
5306 worktree = NULL;
5308 error = view_loop(view);
5309 done:
5310 free(repo_path);
5311 free(in_repo_path);
5312 free(link_target);
5313 free(cwd);
5314 free(commit_id);
5315 if (commit)
5316 got_object_commit_close(commit);
5317 if (worktree)
5318 got_worktree_close(worktree);
5319 if (repo) {
5320 const struct got_error *close_err = got_repo_close(repo);
5321 if (error == NULL)
5322 error = close_err;
5324 if (pack_fds) {
5325 const struct got_error *pack_err =
5326 got_repo_pack_fds_close(pack_fds);
5327 if (error == NULL)
5328 error = pack_err;
5330 tog_free_refs();
5331 return error;
5334 static const struct got_error *
5335 draw_tree_entries(struct tog_view *view, const char *parent_path)
5337 struct tog_tree_view_state *s = &view->state.tree;
5338 const struct got_error *err = NULL;
5339 struct got_tree_entry *te;
5340 wchar_t *wline;
5341 struct tog_color *tc;
5342 int width, n, i, nentries;
5343 int limit = view->nlines;
5345 s->ndisplayed = 0;
5347 werase(view->window);
5349 if (limit == 0)
5350 return NULL;
5352 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5353 0, 0);
5354 if (err)
5355 return err;
5356 if (view_needs_focus_indication(view))
5357 wstandout(view->window);
5358 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5359 if (tc)
5360 wattr_on(view->window,
5361 COLOR_PAIR(tc->colorpair), NULL);
5362 waddwstr(view->window, wline);
5363 if (tc)
5364 wattr_off(view->window,
5365 COLOR_PAIR(tc->colorpair), NULL);
5366 if (view_needs_focus_indication(view))
5367 wstandend(view->window);
5368 free(wline);
5369 wline = NULL;
5370 if (width < view->ncols - 1)
5371 waddch(view->window, '\n');
5372 if (--limit <= 0)
5373 return NULL;
5374 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5375 0, 0);
5376 if (err)
5377 return err;
5378 waddwstr(view->window, wline);
5379 free(wline);
5380 wline = NULL;
5381 if (width < view->ncols - 1)
5382 waddch(view->window, '\n');
5383 if (--limit <= 0)
5384 return NULL;
5385 waddch(view->window, '\n');
5386 if (--limit <= 0)
5387 return NULL;
5389 if (s->first_displayed_entry == NULL) {
5390 te = got_object_tree_get_first_entry(s->tree);
5391 if (s->selected == 0) {
5392 if (view->focussed)
5393 wstandout(view->window);
5394 s->selected_entry = NULL;
5396 waddstr(view->window, " ..\n"); /* parent directory */
5397 if (s->selected == 0 && view->focussed)
5398 wstandend(view->window);
5399 s->ndisplayed++;
5400 if (--limit <= 0)
5401 return NULL;
5402 n = 1;
5403 } else {
5404 n = 0;
5405 te = s->first_displayed_entry;
5408 nentries = got_object_tree_get_nentries(s->tree);
5409 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5410 char *line = NULL, *id_str = NULL, *link_target = NULL;
5411 const char *modestr = "";
5412 mode_t mode;
5414 te = got_object_tree_get_entry(s->tree, i);
5415 mode = got_tree_entry_get_mode(te);
5417 if (s->show_ids) {
5418 err = got_object_id_str(&id_str,
5419 got_tree_entry_get_id(te));
5420 if (err)
5421 return got_error_from_errno(
5422 "got_object_id_str");
5424 if (got_object_tree_entry_is_submodule(te))
5425 modestr = "$";
5426 else if (S_ISLNK(mode)) {
5427 int i;
5429 err = got_tree_entry_get_symlink_target(&link_target,
5430 te, s->repo);
5431 if (err) {
5432 free(id_str);
5433 return err;
5435 for (i = 0; i < strlen(link_target); i++) {
5436 if (!isprint((unsigned char)link_target[i]))
5437 link_target[i] = '?';
5439 modestr = "@";
5441 else if (S_ISDIR(mode))
5442 modestr = "/";
5443 else if (mode & S_IXUSR)
5444 modestr = "*";
5445 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5446 got_tree_entry_get_name(te), modestr,
5447 link_target ? " -> ": "",
5448 link_target ? link_target : "") == -1) {
5449 free(id_str);
5450 free(link_target);
5451 return got_error_from_errno("asprintf");
5453 free(id_str);
5454 free(link_target);
5455 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5456 0, 0);
5457 if (err) {
5458 free(line);
5459 break;
5461 if (n == s->selected) {
5462 if (view->focussed)
5463 wstandout(view->window);
5464 s->selected_entry = te;
5466 tc = match_color(&s->colors, line);
5467 if (tc)
5468 wattr_on(view->window,
5469 COLOR_PAIR(tc->colorpair), NULL);
5470 waddwstr(view->window, wline);
5471 if (tc)
5472 wattr_off(view->window,
5473 COLOR_PAIR(tc->colorpair), NULL);
5474 if (width < view->ncols - 1)
5475 waddch(view->window, '\n');
5476 if (n == s->selected && view->focussed)
5477 wstandend(view->window);
5478 free(line);
5479 free(wline);
5480 wline = NULL;
5481 n++;
5482 s->ndisplayed++;
5483 s->last_displayed_entry = te;
5484 if (--limit <= 0)
5485 break;
5488 return err;
5491 static void
5492 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5494 struct got_tree_entry *te;
5495 int isroot = s->tree == s->root;
5496 int i = 0;
5498 if (s->first_displayed_entry == NULL)
5499 return;
5501 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5502 while (i++ < maxscroll) {
5503 if (te == NULL) {
5504 if (!isroot)
5505 s->first_displayed_entry = NULL;
5506 break;
5508 s->first_displayed_entry = te;
5509 te = got_tree_entry_get_prev(s->tree, te);
5513 static void
5514 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5516 struct got_tree_entry *next, *last;
5517 int n = 0;
5519 if (s->first_displayed_entry)
5520 next = got_tree_entry_get_next(s->tree,
5521 s->first_displayed_entry);
5522 else
5523 next = got_object_tree_get_first_entry(s->tree);
5525 last = s->last_displayed_entry;
5526 while (next && last && n++ < maxscroll) {
5527 last = got_tree_entry_get_next(s->tree, last);
5528 if (last) {
5529 s->first_displayed_entry = next;
5530 next = got_tree_entry_get_next(s->tree, next);
5535 static const struct got_error *
5536 tree_entry_path(char **path, struct tog_parent_trees *parents,
5537 struct got_tree_entry *te)
5539 const struct got_error *err = NULL;
5540 struct tog_parent_tree *pt;
5541 size_t len = 2; /* for leading slash and NUL */
5543 TAILQ_FOREACH(pt, parents, entry)
5544 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5545 + 1 /* slash */;
5546 if (te)
5547 len += strlen(got_tree_entry_get_name(te));
5549 *path = calloc(1, len);
5550 if (path == NULL)
5551 return got_error_from_errno("calloc");
5553 (*path)[0] = '/';
5554 pt = TAILQ_LAST(parents, tog_parent_trees);
5555 while (pt) {
5556 const char *name = got_tree_entry_get_name(pt->selected_entry);
5557 if (strlcat(*path, name, len) >= len) {
5558 err = got_error(GOT_ERR_NO_SPACE);
5559 goto done;
5561 if (strlcat(*path, "/", len) >= len) {
5562 err = got_error(GOT_ERR_NO_SPACE);
5563 goto done;
5565 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5567 if (te) {
5568 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5569 err = got_error(GOT_ERR_NO_SPACE);
5570 goto done;
5573 done:
5574 if (err) {
5575 free(*path);
5576 *path = NULL;
5578 return err;
5581 static const struct got_error *
5582 blame_tree_entry(struct tog_view **new_view, int begin_x,
5583 struct got_tree_entry *te, struct tog_parent_trees *parents,
5584 struct got_object_id *commit_id, struct got_repository *repo)
5586 const struct got_error *err = NULL;
5587 char *path;
5588 struct tog_view *blame_view;
5590 *new_view = NULL;
5592 err = tree_entry_path(&path, parents, te);
5593 if (err)
5594 return err;
5596 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5597 if (blame_view == NULL) {
5598 err = got_error_from_errno("view_open");
5599 goto done;
5602 err = open_blame_view(blame_view, path, commit_id, repo);
5603 if (err) {
5604 if (err->code == GOT_ERR_CANCELLED)
5605 err = NULL;
5606 view_close(blame_view);
5607 } else
5608 *new_view = blame_view;
5609 done:
5610 free(path);
5611 return err;
5614 static const struct got_error *
5615 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5616 struct tog_tree_view_state *s)
5618 struct tog_view *log_view;
5619 const struct got_error *err = NULL;
5620 char *path;
5622 *new_view = NULL;
5624 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5625 if (log_view == NULL)
5626 return got_error_from_errno("view_open");
5628 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5629 if (err)
5630 return err;
5632 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5633 path, 0);
5634 if (err)
5635 view_close(log_view);
5636 else
5637 *new_view = log_view;
5638 free(path);
5639 return err;
5642 static const struct got_error *
5643 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5644 const char *head_ref_name, struct got_repository *repo)
5646 const struct got_error *err = NULL;
5647 char *commit_id_str = NULL;
5648 struct tog_tree_view_state *s = &view->state.tree;
5649 struct got_commit_object *commit = NULL;
5651 TAILQ_INIT(&s->parents);
5652 STAILQ_INIT(&s->colors);
5654 s->commit_id = got_object_id_dup(commit_id);
5655 if (s->commit_id == NULL)
5656 return got_error_from_errno("got_object_id_dup");
5658 err = got_object_open_as_commit(&commit, repo, commit_id);
5659 if (err)
5660 goto done;
5663 * The root is opened here and will be closed when the view is closed.
5664 * Any visited subtrees and their path-wise parents are opened and
5665 * closed on demand.
5667 err = got_object_open_as_tree(&s->root, repo,
5668 got_object_commit_get_tree_id(commit));
5669 if (err)
5670 goto done;
5671 s->tree = s->root;
5673 err = got_object_id_str(&commit_id_str, commit_id);
5674 if (err != NULL)
5675 goto done;
5677 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5678 err = got_error_from_errno("asprintf");
5679 goto done;
5682 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5683 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5684 if (head_ref_name) {
5685 s->head_ref_name = strdup(head_ref_name);
5686 if (s->head_ref_name == NULL) {
5687 err = got_error_from_errno("strdup");
5688 goto done;
5691 s->repo = repo;
5693 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5694 err = add_color(&s->colors, "\\$$",
5695 TOG_COLOR_TREE_SUBMODULE,
5696 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5697 if (err)
5698 goto done;
5699 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5700 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5701 if (err)
5702 goto done;
5703 err = add_color(&s->colors, "/$",
5704 TOG_COLOR_TREE_DIRECTORY,
5705 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5706 if (err)
5707 goto done;
5709 err = add_color(&s->colors, "\\*$",
5710 TOG_COLOR_TREE_EXECUTABLE,
5711 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5712 if (err)
5713 goto done;
5715 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5716 get_color_value("TOG_COLOR_COMMIT"));
5717 if (err)
5718 goto done;
5721 view->show = show_tree_view;
5722 view->input = input_tree_view;
5723 view->close = close_tree_view;
5724 view->search_start = search_start_tree_view;
5725 view->search_next = search_next_tree_view;
5726 done:
5727 free(commit_id_str);
5728 if (commit)
5729 got_object_commit_close(commit);
5730 if (err)
5731 close_tree_view(view);
5732 return err;
5735 static const struct got_error *
5736 close_tree_view(struct tog_view *view)
5738 struct tog_tree_view_state *s = &view->state.tree;
5740 free_colors(&s->colors);
5741 free(s->tree_label);
5742 s->tree_label = NULL;
5743 free(s->commit_id);
5744 s->commit_id = NULL;
5745 free(s->head_ref_name);
5746 s->head_ref_name = NULL;
5747 while (!TAILQ_EMPTY(&s->parents)) {
5748 struct tog_parent_tree *parent;
5749 parent = TAILQ_FIRST(&s->parents);
5750 TAILQ_REMOVE(&s->parents, parent, entry);
5751 if (parent->tree != s->root)
5752 got_object_tree_close(parent->tree);
5753 free(parent);
5756 if (s->tree != NULL && s->tree != s->root)
5757 got_object_tree_close(s->tree);
5758 if (s->root)
5759 got_object_tree_close(s->root);
5760 return NULL;
5763 static const struct got_error *
5764 search_start_tree_view(struct tog_view *view)
5766 struct tog_tree_view_state *s = &view->state.tree;
5768 s->matched_entry = NULL;
5769 return NULL;
5772 static int
5773 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5775 regmatch_t regmatch;
5777 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5778 0) == 0;
5781 static const struct got_error *
5782 search_next_tree_view(struct tog_view *view)
5784 struct tog_tree_view_state *s = &view->state.tree;
5785 struct got_tree_entry *te = NULL;
5787 if (!view->searching) {
5788 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5789 return NULL;
5792 if (s->matched_entry) {
5793 if (view->searching == TOG_SEARCH_FORWARD) {
5794 if (s->selected_entry)
5795 te = got_tree_entry_get_next(s->tree,
5796 s->selected_entry);
5797 else
5798 te = got_object_tree_get_first_entry(s->tree);
5799 } else {
5800 if (s->selected_entry == NULL)
5801 te = got_object_tree_get_last_entry(s->tree);
5802 else
5803 te = got_tree_entry_get_prev(s->tree,
5804 s->selected_entry);
5806 } else {
5807 if (s->selected_entry)
5808 te = s->selected_entry;
5809 else if (view->searching == TOG_SEARCH_FORWARD)
5810 te = got_object_tree_get_first_entry(s->tree);
5811 else
5812 te = got_object_tree_get_last_entry(s->tree);
5815 while (1) {
5816 if (te == NULL) {
5817 if (s->matched_entry == NULL) {
5818 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5819 return NULL;
5821 if (view->searching == TOG_SEARCH_FORWARD)
5822 te = got_object_tree_get_first_entry(s->tree);
5823 else
5824 te = got_object_tree_get_last_entry(s->tree);
5827 if (match_tree_entry(te, &view->regex)) {
5828 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5829 s->matched_entry = te;
5830 break;
5833 if (view->searching == TOG_SEARCH_FORWARD)
5834 te = got_tree_entry_get_next(s->tree, te);
5835 else
5836 te = got_tree_entry_get_prev(s->tree, te);
5839 if (s->matched_entry) {
5840 s->first_displayed_entry = s->matched_entry;
5841 s->selected = 0;
5844 return NULL;
5847 static const struct got_error *
5848 show_tree_view(struct tog_view *view)
5850 const struct got_error *err = NULL;
5851 struct tog_tree_view_state *s = &view->state.tree;
5852 char *parent_path;
5854 err = tree_entry_path(&parent_path, &s->parents, NULL);
5855 if (err)
5856 return err;
5858 err = draw_tree_entries(view, parent_path);
5859 free(parent_path);
5861 view_vborder(view);
5862 return err;
5865 static const struct got_error *
5866 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5868 const struct got_error *err = NULL;
5869 struct tog_tree_view_state *s = &view->state.tree;
5870 struct tog_view *log_view, *ref_view;
5871 struct got_tree_entry *te;
5872 int begin_x = 0, n, nscroll = view->nlines - 3;
5874 switch (ch) {
5875 case 'i':
5876 s->show_ids = !s->show_ids;
5877 break;
5878 case 'l':
5879 if (!s->selected_entry)
5880 break;
5881 if (view_is_parent_view(view))
5882 begin_x = view_split_begin_x(view->begin_x);
5883 err = log_selected_tree_entry(&log_view, begin_x, s);
5884 view->focussed = 0;
5885 log_view->focussed = 1;
5886 if (view_is_parent_view(view)) {
5887 err = view_close_child(view);
5888 if (err)
5889 return err;
5890 err = view_set_child(view, log_view);
5891 if (err)
5892 return err;
5893 view->focus_child = 1;
5894 } else
5895 *new_view = log_view;
5896 break;
5897 case 'r':
5898 if (view_is_parent_view(view))
5899 begin_x = view_split_begin_x(view->begin_x);
5900 ref_view = view_open(view->nlines, view->ncols,
5901 view->begin_y, begin_x, TOG_VIEW_REF);
5902 if (ref_view == NULL)
5903 return got_error_from_errno("view_open");
5904 err = open_ref_view(ref_view, s->repo);
5905 if (err) {
5906 view_close(ref_view);
5907 return err;
5909 view->focussed = 0;
5910 ref_view->focussed = 1;
5911 if (view_is_parent_view(view)) {
5912 err = view_close_child(view);
5913 if (err)
5914 return err;
5915 err = view_set_child(view, ref_view);
5916 if (err)
5917 return err;
5918 view->focus_child = 1;
5919 } else
5920 *new_view = ref_view;
5921 break;
5922 case 'g':
5923 case KEY_HOME:
5924 s->selected = 0;
5925 if (s->tree == s->root)
5926 s->first_displayed_entry =
5927 got_object_tree_get_first_entry(s->tree);
5928 else
5929 s->first_displayed_entry = NULL;
5930 break;
5931 case 'G':
5932 case KEY_END:
5933 s->selected = 0;
5934 te = got_object_tree_get_last_entry(s->tree);
5935 for (n = 0; n < view->nlines - 3; n++) {
5936 if (te == NULL) {
5937 if(s->tree != s->root) {
5938 s->first_displayed_entry = NULL;
5939 n++;
5941 break;
5943 s->first_displayed_entry = te;
5944 te = got_tree_entry_get_prev(s->tree, te);
5946 if (n > 0)
5947 s->selected = n - 1;
5948 break;
5949 case 'k':
5950 case KEY_UP:
5951 case CTRL('p'):
5952 if (s->selected > 0) {
5953 s->selected--;
5954 break;
5956 tree_scroll_up(s, 1);
5957 break;
5958 case CTRL('u'):
5959 case 'u':
5960 nscroll /= 2;
5961 /* FALL THROUGH */
5962 case KEY_PPAGE:
5963 case CTRL('b'):
5964 if (s->tree == s->root) {
5965 if (got_object_tree_get_first_entry(s->tree) ==
5966 s->first_displayed_entry)
5967 s->selected -= MIN(s->selected, nscroll);
5968 } else {
5969 if (s->first_displayed_entry == NULL)
5970 s->selected -= MIN(s->selected, nscroll);
5972 tree_scroll_up(s, MAX(0, nscroll));
5973 break;
5974 case 'j':
5975 case KEY_DOWN:
5976 case CTRL('n'):
5977 if (s->selected < s->ndisplayed - 1) {
5978 s->selected++;
5979 break;
5981 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5982 == NULL)
5983 /* can't scroll any further */
5984 break;
5985 tree_scroll_down(s, 1);
5986 break;
5987 case CTRL('d'):
5988 case 'd':
5989 nscroll /= 2;
5990 /* FALL THROUGH */
5991 case KEY_NPAGE:
5992 case CTRL('f'):
5993 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5994 == NULL) {
5995 /* can't scroll any further; move cursor down */
5996 if (s->selected < s->ndisplayed - 1)
5997 s->selected += MIN(nscroll,
5998 s->ndisplayed - s->selected - 1);
5999 break;
6001 tree_scroll_down(s, nscroll);
6002 break;
6003 case KEY_ENTER:
6004 case '\r':
6005 case KEY_BACKSPACE:
6006 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6007 struct tog_parent_tree *parent;
6008 /* user selected '..' */
6009 if (s->tree == s->root)
6010 break;
6011 parent = TAILQ_FIRST(&s->parents);
6012 TAILQ_REMOVE(&s->parents, parent,
6013 entry);
6014 got_object_tree_close(s->tree);
6015 s->tree = parent->tree;
6016 s->first_displayed_entry =
6017 parent->first_displayed_entry;
6018 s->selected_entry =
6019 parent->selected_entry;
6020 s->selected = parent->selected;
6021 free(parent);
6022 } else if (S_ISDIR(got_tree_entry_get_mode(
6023 s->selected_entry))) {
6024 struct got_tree_object *subtree;
6025 err = got_object_open_as_tree(&subtree, s->repo,
6026 got_tree_entry_get_id(s->selected_entry));
6027 if (err)
6028 break;
6029 err = tree_view_visit_subtree(s, subtree);
6030 if (err) {
6031 got_object_tree_close(subtree);
6032 break;
6034 } else if (S_ISREG(got_tree_entry_get_mode(
6035 s->selected_entry))) {
6036 struct tog_view *blame_view;
6037 int begin_x = view_is_parent_view(view) ?
6038 view_split_begin_x(view->begin_x) : 0;
6040 err = blame_tree_entry(&blame_view, begin_x,
6041 s->selected_entry, &s->parents,
6042 s->commit_id, s->repo);
6043 if (err)
6044 break;
6045 view->focussed = 0;
6046 blame_view->focussed = 1;
6047 if (view_is_parent_view(view)) {
6048 err = view_close_child(view);
6049 if (err)
6050 return err;
6051 err = view_set_child(view, blame_view);
6052 if (err)
6053 return err;
6054 view->focus_child = 1;
6055 } else
6056 *new_view = blame_view;
6058 break;
6059 case KEY_RESIZE:
6060 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6061 s->selected = view->nlines - 4;
6062 break;
6063 default:
6064 break;
6067 return err;
6070 __dead static void
6071 usage_tree(void)
6073 endwin();
6074 fprintf(stderr,
6075 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6076 getprogname());
6077 exit(1);
6080 static const struct got_error *
6081 cmd_tree(int argc, char *argv[])
6083 const struct got_error *error;
6084 struct got_repository *repo = NULL;
6085 struct got_worktree *worktree = NULL;
6086 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6087 struct got_object_id *commit_id = NULL;
6088 struct got_commit_object *commit = NULL;
6089 const char *commit_id_arg = NULL;
6090 char *label = NULL;
6091 struct got_reference *ref = NULL;
6092 const char *head_ref_name = NULL;
6093 int ch;
6094 struct tog_view *view;
6095 int *pack_fds = NULL;
6097 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6098 switch (ch) {
6099 case 'c':
6100 commit_id_arg = optarg;
6101 break;
6102 case 'r':
6103 repo_path = realpath(optarg, NULL);
6104 if (repo_path == NULL)
6105 return got_error_from_errno2("realpath",
6106 optarg);
6107 break;
6108 default:
6109 usage_tree();
6110 /* NOTREACHED */
6114 argc -= optind;
6115 argv += optind;
6117 if (argc > 1)
6118 usage_tree();
6120 error = got_repo_pack_fds_open(&pack_fds);
6121 if (error != NULL)
6122 goto done;
6124 if (repo_path == NULL) {
6125 cwd = getcwd(NULL, 0);
6126 if (cwd == NULL)
6127 return got_error_from_errno("getcwd");
6128 error = got_worktree_open(&worktree, cwd);
6129 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6130 goto done;
6131 if (worktree)
6132 repo_path =
6133 strdup(got_worktree_get_repo_path(worktree));
6134 else
6135 repo_path = strdup(cwd);
6136 if (repo_path == NULL) {
6137 error = got_error_from_errno("strdup");
6138 goto done;
6142 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6143 if (error != NULL)
6144 goto done;
6146 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6147 repo, worktree);
6148 if (error)
6149 goto done;
6151 init_curses();
6153 error = apply_unveil(got_repo_get_path(repo), NULL);
6154 if (error)
6155 goto done;
6157 error = tog_load_refs(repo, 0);
6158 if (error)
6159 goto done;
6161 if (commit_id_arg == NULL) {
6162 error = got_repo_match_object_id(&commit_id, &label,
6163 worktree ? got_worktree_get_head_ref_name(worktree) :
6164 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6165 if (error)
6166 goto done;
6167 head_ref_name = label;
6168 } else {
6169 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6170 if (error == NULL)
6171 head_ref_name = got_ref_get_name(ref);
6172 else if (error->code != GOT_ERR_NOT_REF)
6173 goto done;
6174 error = got_repo_match_object_id(&commit_id, NULL,
6175 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6176 if (error)
6177 goto done;
6180 error = got_object_open_as_commit(&commit, repo, commit_id);
6181 if (error)
6182 goto done;
6184 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6185 if (view == NULL) {
6186 error = got_error_from_errno("view_open");
6187 goto done;
6189 error = open_tree_view(view, commit_id, head_ref_name, repo);
6190 if (error)
6191 goto done;
6192 if (!got_path_is_root_dir(in_repo_path)) {
6193 error = tree_view_walk_path(&view->state.tree, commit,
6194 in_repo_path);
6195 if (error)
6196 goto done;
6199 if (worktree) {
6200 /* Release work tree lock. */
6201 got_worktree_close(worktree);
6202 worktree = NULL;
6204 error = view_loop(view);
6205 done:
6206 free(repo_path);
6207 free(cwd);
6208 free(commit_id);
6209 free(label);
6210 if (ref)
6211 got_ref_close(ref);
6212 if (repo) {
6213 const struct got_error *close_err = got_repo_close(repo);
6214 if (error == NULL)
6215 error = close_err;
6217 if (pack_fds) {
6218 const struct got_error *pack_err =
6219 got_repo_pack_fds_close(pack_fds);
6220 if (error == NULL)
6221 error = pack_err;
6223 tog_free_refs();
6224 return error;
6227 static const struct got_error *
6228 ref_view_load_refs(struct tog_ref_view_state *s)
6230 struct got_reflist_entry *sre;
6231 struct tog_reflist_entry *re;
6233 s->nrefs = 0;
6234 TAILQ_FOREACH(sre, &tog_refs, entry) {
6235 if (strncmp(got_ref_get_name(sre->ref),
6236 "refs/got/", 9) == 0 &&
6237 strncmp(got_ref_get_name(sre->ref),
6238 "refs/got/backup/", 16) != 0)
6239 continue;
6241 re = malloc(sizeof(*re));
6242 if (re == NULL)
6243 return got_error_from_errno("malloc");
6245 re->ref = got_ref_dup(sre->ref);
6246 if (re->ref == NULL)
6247 return got_error_from_errno("got_ref_dup");
6248 re->idx = s->nrefs++;
6249 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6252 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6253 return NULL;
6256 void
6257 ref_view_free_refs(struct tog_ref_view_state *s)
6259 struct tog_reflist_entry *re;
6261 while (!TAILQ_EMPTY(&s->refs)) {
6262 re = TAILQ_FIRST(&s->refs);
6263 TAILQ_REMOVE(&s->refs, re, entry);
6264 got_ref_close(re->ref);
6265 free(re);
6269 static const struct got_error *
6270 open_ref_view(struct tog_view *view, struct got_repository *repo)
6272 const struct got_error *err = NULL;
6273 struct tog_ref_view_state *s = &view->state.ref;
6275 s->selected_entry = 0;
6276 s->repo = repo;
6278 TAILQ_INIT(&s->refs);
6279 STAILQ_INIT(&s->colors);
6281 err = ref_view_load_refs(s);
6282 if (err)
6283 return err;
6285 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6286 err = add_color(&s->colors, "^refs/heads/",
6287 TOG_COLOR_REFS_HEADS,
6288 get_color_value("TOG_COLOR_REFS_HEADS"));
6289 if (err)
6290 goto done;
6292 err = add_color(&s->colors, "^refs/tags/",
6293 TOG_COLOR_REFS_TAGS,
6294 get_color_value("TOG_COLOR_REFS_TAGS"));
6295 if (err)
6296 goto done;
6298 err = add_color(&s->colors, "^refs/remotes/",
6299 TOG_COLOR_REFS_REMOTES,
6300 get_color_value("TOG_COLOR_REFS_REMOTES"));
6301 if (err)
6302 goto done;
6304 err = add_color(&s->colors, "^refs/got/backup/",
6305 TOG_COLOR_REFS_BACKUP,
6306 get_color_value("TOG_COLOR_REFS_BACKUP"));
6307 if (err)
6308 goto done;
6311 view->show = show_ref_view;
6312 view->input = input_ref_view;
6313 view->close = close_ref_view;
6314 view->search_start = search_start_ref_view;
6315 view->search_next = search_next_ref_view;
6316 done:
6317 if (err)
6318 free_colors(&s->colors);
6319 return err;
6322 static const struct got_error *
6323 close_ref_view(struct tog_view *view)
6325 struct tog_ref_view_state *s = &view->state.ref;
6327 ref_view_free_refs(s);
6328 free_colors(&s->colors);
6330 return NULL;
6333 static const struct got_error *
6334 resolve_reflist_entry(struct got_object_id **commit_id,
6335 struct tog_reflist_entry *re, struct got_repository *repo)
6337 const struct got_error *err = NULL;
6338 struct got_object_id *obj_id;
6339 struct got_tag_object *tag = NULL;
6340 int obj_type;
6342 *commit_id = NULL;
6344 err = got_ref_resolve(&obj_id, repo, re->ref);
6345 if (err)
6346 return err;
6348 err = got_object_get_type(&obj_type, repo, obj_id);
6349 if (err)
6350 goto done;
6352 switch (obj_type) {
6353 case GOT_OBJ_TYPE_COMMIT:
6354 *commit_id = obj_id;
6355 break;
6356 case GOT_OBJ_TYPE_TAG:
6357 err = got_object_open_as_tag(&tag, repo, obj_id);
6358 if (err)
6359 goto done;
6360 free(obj_id);
6361 err = got_object_get_type(&obj_type, repo,
6362 got_object_tag_get_object_id(tag));
6363 if (err)
6364 goto done;
6365 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6366 err = got_error(GOT_ERR_OBJ_TYPE);
6367 goto done;
6369 *commit_id = got_object_id_dup(
6370 got_object_tag_get_object_id(tag));
6371 if (*commit_id == NULL) {
6372 err = got_error_from_errno("got_object_id_dup");
6373 goto done;
6375 break;
6376 default:
6377 err = got_error(GOT_ERR_OBJ_TYPE);
6378 break;
6381 done:
6382 if (tag)
6383 got_object_tag_close(tag);
6384 if (err) {
6385 free(*commit_id);
6386 *commit_id = NULL;
6388 return err;
6391 static const struct got_error *
6392 log_ref_entry(struct tog_view **new_view, int begin_x,
6393 struct tog_reflist_entry *re, struct got_repository *repo)
6395 struct tog_view *log_view;
6396 const struct got_error *err = NULL;
6397 struct got_object_id *commit_id = NULL;
6399 *new_view = NULL;
6401 err = resolve_reflist_entry(&commit_id, re, repo);
6402 if (err) {
6403 if (err->code != GOT_ERR_OBJ_TYPE)
6404 return err;
6405 else
6406 return NULL;
6409 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6410 if (log_view == NULL) {
6411 err = got_error_from_errno("view_open");
6412 goto done;
6415 err = open_log_view(log_view, commit_id, repo,
6416 got_ref_get_name(re->ref), "", 0);
6417 done:
6418 if (err)
6419 view_close(log_view);
6420 else
6421 *new_view = log_view;
6422 free(commit_id);
6423 return err;
6426 static void
6427 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6429 struct tog_reflist_entry *re;
6430 int i = 0;
6432 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6433 return;
6435 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6436 while (i++ < maxscroll) {
6437 if (re == NULL)
6438 break;
6439 s->first_displayed_entry = re;
6440 re = TAILQ_PREV(re, tog_reflist_head, entry);
6444 static void
6445 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6447 struct tog_reflist_entry *next, *last;
6448 int n = 0;
6450 if (s->first_displayed_entry)
6451 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6452 else
6453 next = TAILQ_FIRST(&s->refs);
6455 last = s->last_displayed_entry;
6456 while (next && last && n++ < maxscroll) {
6457 last = TAILQ_NEXT(last, entry);
6458 if (last) {
6459 s->first_displayed_entry = next;
6460 next = TAILQ_NEXT(next, entry);
6465 static const struct got_error *
6466 search_start_ref_view(struct tog_view *view)
6468 struct tog_ref_view_state *s = &view->state.ref;
6470 s->matched_entry = NULL;
6471 return NULL;
6474 static int
6475 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6477 regmatch_t regmatch;
6479 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6480 0) == 0;
6483 static const struct got_error *
6484 search_next_ref_view(struct tog_view *view)
6486 struct tog_ref_view_state *s = &view->state.ref;
6487 struct tog_reflist_entry *re = NULL;
6489 if (!view->searching) {
6490 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6491 return NULL;
6494 if (s->matched_entry) {
6495 if (view->searching == TOG_SEARCH_FORWARD) {
6496 if (s->selected_entry)
6497 re = TAILQ_NEXT(s->selected_entry, entry);
6498 else
6499 re = TAILQ_PREV(s->selected_entry,
6500 tog_reflist_head, entry);
6501 } else {
6502 if (s->selected_entry == NULL)
6503 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6504 else
6505 re = TAILQ_PREV(s->selected_entry,
6506 tog_reflist_head, entry);
6508 } else {
6509 if (s->selected_entry)
6510 re = s->selected_entry;
6511 else if (view->searching == TOG_SEARCH_FORWARD)
6512 re = TAILQ_FIRST(&s->refs);
6513 else
6514 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6517 while (1) {
6518 if (re == NULL) {
6519 if (s->matched_entry == NULL) {
6520 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6521 return NULL;
6523 if (view->searching == TOG_SEARCH_FORWARD)
6524 re = TAILQ_FIRST(&s->refs);
6525 else
6526 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6529 if (match_reflist_entry(re, &view->regex)) {
6530 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6531 s->matched_entry = re;
6532 break;
6535 if (view->searching == TOG_SEARCH_FORWARD)
6536 re = TAILQ_NEXT(re, entry);
6537 else
6538 re = TAILQ_PREV(re, tog_reflist_head, entry);
6541 if (s->matched_entry) {
6542 s->first_displayed_entry = s->matched_entry;
6543 s->selected = 0;
6546 return NULL;
6549 static const struct got_error *
6550 show_ref_view(struct tog_view *view)
6552 const struct got_error *err = NULL;
6553 struct tog_ref_view_state *s = &view->state.ref;
6554 struct tog_reflist_entry *re;
6555 char *line = NULL;
6556 wchar_t *wline;
6557 struct tog_color *tc;
6558 int width, n;
6559 int limit = view->nlines;
6561 werase(view->window);
6563 s->ndisplayed = 0;
6565 if (limit == 0)
6566 return NULL;
6568 re = s->first_displayed_entry;
6570 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6571 s->nrefs) == -1)
6572 return got_error_from_errno("asprintf");
6574 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6575 if (err) {
6576 free(line);
6577 return err;
6579 if (view_needs_focus_indication(view))
6580 wstandout(view->window);
6581 waddwstr(view->window, wline);
6582 if (view_needs_focus_indication(view))
6583 wstandend(view->window);
6584 free(wline);
6585 wline = NULL;
6586 free(line);
6587 line = NULL;
6588 if (width < view->ncols - 1)
6589 waddch(view->window, '\n');
6590 if (--limit <= 0)
6591 return NULL;
6593 n = 0;
6594 while (re && limit > 0) {
6595 char *line = NULL;
6596 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6598 if (s->show_date) {
6599 struct got_commit_object *ci;
6600 struct got_tag_object *tag;
6601 struct got_object_id *id;
6602 struct tm tm;
6603 time_t t;
6605 err = got_ref_resolve(&id, s->repo, re->ref);
6606 if (err)
6607 return err;
6608 err = got_object_open_as_tag(&tag, s->repo, id);
6609 if (err) {
6610 if (err->code != GOT_ERR_OBJ_TYPE) {
6611 free(id);
6612 return err;
6614 err = got_object_open_as_commit(&ci, s->repo,
6615 id);
6616 if (err) {
6617 free(id);
6618 return err;
6620 t = got_object_commit_get_committer_time(ci);
6621 got_object_commit_close(ci);
6622 } else {
6623 t = got_object_tag_get_tagger_time(tag);
6624 got_object_tag_close(tag);
6626 free(id);
6627 if (gmtime_r(&t, &tm) == NULL)
6628 return got_error_from_errno("gmtime_r");
6629 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6630 return got_error(GOT_ERR_NO_SPACE);
6632 if (got_ref_is_symbolic(re->ref)) {
6633 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6634 ymd : "", got_ref_get_name(re->ref),
6635 got_ref_get_symref_target(re->ref)) == -1)
6636 return got_error_from_errno("asprintf");
6637 } else if (s->show_ids) {
6638 struct got_object_id *id;
6639 char *id_str;
6640 err = got_ref_resolve(&id, s->repo, re->ref);
6641 if (err)
6642 return err;
6643 err = got_object_id_str(&id_str, id);
6644 if (err) {
6645 free(id);
6646 return err;
6648 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6649 got_ref_get_name(re->ref), id_str) == -1) {
6650 err = got_error_from_errno("asprintf");
6651 free(id);
6652 free(id_str);
6653 return err;
6655 free(id);
6656 free(id_str);
6657 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6658 got_ref_get_name(re->ref)) == -1)
6659 return got_error_from_errno("asprintf");
6661 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6662 0, 0);
6663 if (err) {
6664 free(line);
6665 return err;
6667 if (n == s->selected) {
6668 if (view->focussed)
6669 wstandout(view->window);
6670 s->selected_entry = re;
6672 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6673 if (tc)
6674 wattr_on(view->window,
6675 COLOR_PAIR(tc->colorpair), NULL);
6676 waddwstr(view->window, wline);
6677 if (tc)
6678 wattr_off(view->window,
6679 COLOR_PAIR(tc->colorpair), NULL);
6680 if (width < view->ncols - 1)
6681 waddch(view->window, '\n');
6682 if (n == s->selected && view->focussed)
6683 wstandend(view->window);
6684 free(line);
6685 free(wline);
6686 wline = NULL;
6687 n++;
6688 s->ndisplayed++;
6689 s->last_displayed_entry = re;
6691 limit--;
6692 re = TAILQ_NEXT(re, entry);
6695 view_vborder(view);
6696 return err;
6699 static const struct got_error *
6700 browse_ref_tree(struct tog_view **new_view, int begin_x,
6701 struct tog_reflist_entry *re, struct got_repository *repo)
6703 const struct got_error *err = NULL;
6704 struct got_object_id *commit_id = NULL;
6705 struct tog_view *tree_view;
6707 *new_view = NULL;
6709 err = resolve_reflist_entry(&commit_id, re, repo);
6710 if (err) {
6711 if (err->code != GOT_ERR_OBJ_TYPE)
6712 return err;
6713 else
6714 return NULL;
6718 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6719 if (tree_view == NULL) {
6720 err = got_error_from_errno("view_open");
6721 goto done;
6724 err = open_tree_view(tree_view, commit_id,
6725 got_ref_get_name(re->ref), repo);
6726 if (err)
6727 goto done;
6729 *new_view = tree_view;
6730 done:
6731 free(commit_id);
6732 return err;
6734 static const struct got_error *
6735 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6737 const struct got_error *err = NULL;
6738 struct tog_ref_view_state *s = &view->state.ref;
6739 struct tog_view *log_view, *tree_view;
6740 struct tog_reflist_entry *re;
6741 int begin_x = 0, n, nscroll = view->nlines - 1;
6743 switch (ch) {
6744 case 'i':
6745 s->show_ids = !s->show_ids;
6746 break;
6747 case 'm':
6748 s->show_date = !s->show_date;
6749 break;
6750 case 'o':
6751 s->sort_by_date = !s->sort_by_date;
6752 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6753 got_ref_cmp_by_commit_timestamp_descending :
6754 tog_ref_cmp_by_name, s->repo);
6755 if (err)
6756 break;
6757 got_reflist_object_id_map_free(tog_refs_idmap);
6758 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6759 &tog_refs, s->repo);
6760 if (err)
6761 break;
6762 ref_view_free_refs(s);
6763 err = ref_view_load_refs(s);
6764 break;
6765 case KEY_ENTER:
6766 case '\r':
6767 if (!s->selected_entry)
6768 break;
6769 if (view_is_parent_view(view))
6770 begin_x = view_split_begin_x(view->begin_x);
6771 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6772 s->repo);
6773 view->focussed = 0;
6774 log_view->focussed = 1;
6775 if (view_is_parent_view(view)) {
6776 err = view_close_child(view);
6777 if (err)
6778 return err;
6779 err = view_set_child(view, log_view);
6780 if (err)
6781 return err;
6782 view->focus_child = 1;
6783 } else
6784 *new_view = log_view;
6785 break;
6786 case 't':
6787 if (!s->selected_entry)
6788 break;
6789 if (view_is_parent_view(view))
6790 begin_x = view_split_begin_x(view->begin_x);
6791 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6792 s->repo);
6793 if (err || tree_view == NULL)
6794 break;
6795 view->focussed = 0;
6796 tree_view->focussed = 1;
6797 if (view_is_parent_view(view)) {
6798 err = view_close_child(view);
6799 if (err)
6800 return err;
6801 err = view_set_child(view, tree_view);
6802 if (err)
6803 return err;
6804 view->focus_child = 1;
6805 } else
6806 *new_view = tree_view;
6807 break;
6808 case 'g':
6809 case KEY_HOME:
6810 s->selected = 0;
6811 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6812 break;
6813 case 'G':
6814 case KEY_END:
6815 s->selected = 0;
6816 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6817 for (n = 0; n < view->nlines - 1; n++) {
6818 if (re == NULL)
6819 break;
6820 s->first_displayed_entry = re;
6821 re = TAILQ_PREV(re, tog_reflist_head, entry);
6823 if (n > 0)
6824 s->selected = n - 1;
6825 break;
6826 case 'k':
6827 case KEY_UP:
6828 case CTRL('p'):
6829 if (s->selected > 0) {
6830 s->selected--;
6831 break;
6833 ref_scroll_up(s, 1);
6834 break;
6835 case CTRL('u'):
6836 case 'u':
6837 nscroll /= 2;
6838 /* FALL THROUGH */
6839 case KEY_PPAGE:
6840 case CTRL('b'):
6841 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6842 s->selected -= MIN(nscroll, s->selected);
6843 ref_scroll_up(s, MAX(0, nscroll));
6844 break;
6845 case 'j':
6846 case KEY_DOWN:
6847 case CTRL('n'):
6848 if (s->selected < s->ndisplayed - 1) {
6849 s->selected++;
6850 break;
6852 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6853 /* can't scroll any further */
6854 break;
6855 ref_scroll_down(s, 1);
6856 break;
6857 case CTRL('d'):
6858 case 'd':
6859 nscroll /= 2;
6860 /* FALL THROUGH */
6861 case KEY_NPAGE:
6862 case CTRL('f'):
6863 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6864 /* can't scroll any further; move cursor down */
6865 if (s->selected < s->ndisplayed - 1)
6866 s->selected += MIN(nscroll,
6867 s->ndisplayed - s->selected - 1);
6868 break;
6870 ref_scroll_down(s, nscroll);
6871 break;
6872 case CTRL('l'):
6873 tog_free_refs();
6874 err = tog_load_refs(s->repo, s->sort_by_date);
6875 if (err)
6876 break;
6877 ref_view_free_refs(s);
6878 err = ref_view_load_refs(s);
6879 break;
6880 case KEY_RESIZE:
6881 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6882 s->selected = view->nlines - 2;
6883 break;
6884 default:
6885 break;
6888 return err;
6891 __dead static void
6892 usage_ref(void)
6894 endwin();
6895 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6896 getprogname());
6897 exit(1);
6900 static const struct got_error *
6901 cmd_ref(int argc, char *argv[])
6903 const struct got_error *error;
6904 struct got_repository *repo = NULL;
6905 struct got_worktree *worktree = NULL;
6906 char *cwd = NULL, *repo_path = NULL;
6907 int ch;
6908 struct tog_view *view;
6909 int *pack_fds = NULL;
6911 while ((ch = getopt(argc, argv, "r:")) != -1) {
6912 switch (ch) {
6913 case 'r':
6914 repo_path = realpath(optarg, NULL);
6915 if (repo_path == NULL)
6916 return got_error_from_errno2("realpath",
6917 optarg);
6918 break;
6919 default:
6920 usage_ref();
6921 /* NOTREACHED */
6925 argc -= optind;
6926 argv += optind;
6928 if (argc > 1)
6929 usage_ref();
6931 error = got_repo_pack_fds_open(&pack_fds);
6932 if (error != NULL)
6933 goto done;
6935 if (repo_path == NULL) {
6936 cwd = getcwd(NULL, 0);
6937 if (cwd == NULL)
6938 return got_error_from_errno("getcwd");
6939 error = got_worktree_open(&worktree, cwd);
6940 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6941 goto done;
6942 if (worktree)
6943 repo_path =
6944 strdup(got_worktree_get_repo_path(worktree));
6945 else
6946 repo_path = strdup(cwd);
6947 if (repo_path == NULL) {
6948 error = got_error_from_errno("strdup");
6949 goto done;
6953 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6954 if (error != NULL)
6955 goto done;
6957 init_curses();
6959 error = apply_unveil(got_repo_get_path(repo), NULL);
6960 if (error)
6961 goto done;
6963 error = tog_load_refs(repo, 0);
6964 if (error)
6965 goto done;
6967 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6968 if (view == NULL) {
6969 error = got_error_from_errno("view_open");
6970 goto done;
6973 error = open_ref_view(view, repo);
6974 if (error)
6975 goto done;
6977 if (worktree) {
6978 /* Release work tree lock. */
6979 got_worktree_close(worktree);
6980 worktree = NULL;
6982 error = view_loop(view);
6983 done:
6984 free(repo_path);
6985 free(cwd);
6986 if (repo) {
6987 const struct got_error *close_err = got_repo_close(repo);
6988 if (close_err)
6989 error = close_err;
6991 if (pack_fds) {
6992 const struct got_error *pack_err =
6993 got_repo_pack_fds_close(pack_fds);
6994 if (error == NULL)
6995 error = pack_err;
6997 tog_free_refs();
6998 return error;
7001 static void
7002 list_commands(FILE *fp)
7004 size_t i;
7006 fprintf(fp, "commands:");
7007 for (i = 0; i < nitems(tog_commands); i++) {
7008 const struct tog_cmd *cmd = &tog_commands[i];
7009 fprintf(fp, " %s", cmd->name);
7011 fputc('\n', fp);
7014 __dead static void
7015 usage(int hflag, int status)
7017 FILE *fp = (status == 0) ? stdout : stderr;
7019 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7020 getprogname());
7021 if (hflag) {
7022 fprintf(fp, "lazy usage: %s path\n", getprogname());
7023 list_commands(fp);
7025 exit(status);
7028 static char **
7029 make_argv(int argc, ...)
7031 va_list ap;
7032 char **argv;
7033 int i;
7035 va_start(ap, argc);
7037 argv = calloc(argc, sizeof(char *));
7038 if (argv == NULL)
7039 err(1, "calloc");
7040 for (i = 0; i < argc; i++) {
7041 argv[i] = strdup(va_arg(ap, char *));
7042 if (argv[i] == NULL)
7043 err(1, "strdup");
7046 va_end(ap);
7047 return argv;
7051 * Try to convert 'tog path' into a 'tog log path' command.
7052 * The user could simply have mistyped the command rather than knowingly
7053 * provided a path. So check whether argv[0] can in fact be resolved
7054 * to a path in the HEAD commit and print a special error if not.
7055 * This hack is for mpi@ <3
7057 static const struct got_error *
7058 tog_log_with_path(int argc, char *argv[])
7060 const struct got_error *error = NULL, *close_err;
7061 const struct tog_cmd *cmd = NULL;
7062 struct got_repository *repo = NULL;
7063 struct got_worktree *worktree = NULL;
7064 struct got_object_id *commit_id = NULL, *id = NULL;
7065 struct got_commit_object *commit = NULL;
7066 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7067 char *commit_id_str = NULL, **cmd_argv = NULL;
7068 int *pack_fds = NULL;
7070 cwd = getcwd(NULL, 0);
7071 if (cwd == NULL)
7072 return got_error_from_errno("getcwd");
7074 error = got_repo_pack_fds_open(&pack_fds);
7075 if (error != NULL)
7076 goto done;
7078 error = got_worktree_open(&worktree, cwd);
7079 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7080 goto done;
7082 if (worktree)
7083 repo_path = strdup(got_worktree_get_repo_path(worktree));
7084 else
7085 repo_path = strdup(cwd);
7086 if (repo_path == NULL) {
7087 error = got_error_from_errno("strdup");
7088 goto done;
7091 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7092 if (error != NULL)
7093 goto done;
7095 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7096 repo, worktree);
7097 if (error)
7098 goto done;
7100 error = tog_load_refs(repo, 0);
7101 if (error)
7102 goto done;
7103 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7104 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7105 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7106 if (error)
7107 goto done;
7109 if (worktree) {
7110 got_worktree_close(worktree);
7111 worktree = NULL;
7114 error = got_object_open_as_commit(&commit, repo, commit_id);
7115 if (error)
7116 goto done;
7118 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7119 if (error) {
7120 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7121 goto done;
7122 fprintf(stderr, "%s: '%s' is no known command or path\n",
7123 getprogname(), argv[0]);
7124 usage(1, 1);
7125 /* not reached */
7128 close_err = got_repo_close(repo);
7129 if (error == NULL)
7130 error = close_err;
7131 repo = NULL;
7133 error = got_object_id_str(&commit_id_str, commit_id);
7134 if (error)
7135 goto done;
7137 cmd = &tog_commands[0]; /* log */
7138 argc = 4;
7139 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7140 error = cmd->cmd_main(argc, cmd_argv);
7141 done:
7142 if (repo) {
7143 close_err = got_repo_close(repo);
7144 if (error == NULL)
7145 error = close_err;
7147 if (commit)
7148 got_object_commit_close(commit);
7149 if (worktree)
7150 got_worktree_close(worktree);
7151 if (pack_fds) {
7152 const struct got_error *pack_err =
7153 got_repo_pack_fds_close(pack_fds);
7154 if (error == NULL)
7155 error = pack_err;
7157 free(id);
7158 free(commit_id_str);
7159 free(commit_id);
7160 free(cwd);
7161 free(repo_path);
7162 free(in_repo_path);
7163 if (cmd_argv) {
7164 int i;
7165 for (i = 0; i < argc; i++)
7166 free(cmd_argv[i]);
7167 free(cmd_argv);
7169 tog_free_refs();
7170 return error;
7173 int
7174 main(int argc, char *argv[])
7176 const struct got_error *error = NULL;
7177 const struct tog_cmd *cmd = NULL;
7178 int ch, hflag = 0, Vflag = 0;
7179 char **cmd_argv = NULL;
7180 static const struct option longopts[] = {
7181 { "version", no_argument, NULL, 'V' },
7182 { NULL, 0, NULL, 0}
7185 setlocale(LC_CTYPE, "");
7187 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7188 switch (ch) {
7189 case 'h':
7190 hflag = 1;
7191 break;
7192 case 'V':
7193 Vflag = 1;
7194 break;
7195 default:
7196 usage(hflag, 1);
7197 /* NOTREACHED */
7201 argc -= optind;
7202 argv += optind;
7203 optind = 1;
7204 optreset = 1;
7206 if (Vflag) {
7207 got_version_print_str();
7208 return 0;
7211 #ifndef PROFILE
7212 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7213 NULL) == -1)
7214 err(1, "pledge");
7215 #endif
7217 if (argc == 0) {
7218 if (hflag)
7219 usage(hflag, 0);
7220 /* Build an argument vector which runs a default command. */
7221 cmd = &tog_commands[0];
7222 argc = 1;
7223 cmd_argv = make_argv(argc, cmd->name);
7224 } else {
7225 size_t i;
7227 /* Did the user specify a command? */
7228 for (i = 0; i < nitems(tog_commands); i++) {
7229 if (strncmp(tog_commands[i].name, argv[0],
7230 strlen(argv[0])) == 0) {
7231 cmd = &tog_commands[i];
7232 break;
7237 if (cmd == NULL) {
7238 if (argc != 1)
7239 usage(0, 1);
7240 /* No command specified; try log with a path */
7241 error = tog_log_with_path(argc, argv);
7242 } else {
7243 if (hflag)
7244 cmd->cmd_usage();
7245 else
7246 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7249 endwin();
7250 putchar('\n');
7251 if (cmd_argv) {
7252 int i;
7253 for (i = 0; i < argc; i++)
7254 free(cmd_argv[i]);
7255 free(cmd_argv);
7258 if (error && error->code != GOT_ERR_CANCELLED)
7259 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7260 return 0;