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 if (view->searching == TOG_SEARCH_FORWARD)
2346 entry = TAILQ_NEXT(s->matched_entry, entry);
2347 else
2348 entry = TAILQ_PREV(s->matched_entry,
2349 commit_queue_head, entry);
2350 } else {
2351 entry = s->selected_entry;
2354 while (1) {
2355 int have_match = 0;
2357 if (entry == NULL) {
2358 if (s->thread_args.log_complete ||
2359 view->searching == TOG_SEARCH_BACKWARD) {
2360 view->search_next_done =
2361 (s->matched_entry == NULL ?
2362 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2363 s->search_entry = NULL;
2364 return NULL;
2367 * Poke the log thread for more commits and return,
2368 * allowing the main loop to make progress. Search
2369 * will resume at s->search_entry once we come back.
2371 s->thread_args.commits_needed++;
2372 return trigger_log_thread(view, 0);
2375 err = match_commit(&have_match, entry->id, entry->commit,
2376 &view->regex);
2377 if (err)
2378 break;
2379 if (have_match) {
2380 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2381 s->matched_entry = entry;
2382 break;
2385 s->search_entry = entry;
2386 if (view->searching == TOG_SEARCH_FORWARD)
2387 entry = TAILQ_NEXT(entry, entry);
2388 else
2389 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2392 if (s->matched_entry) {
2393 int cur = s->selected_entry->idx;
2394 while (cur < s->matched_entry->idx) {
2395 err = input_log_view(NULL, view, KEY_DOWN);
2396 if (err)
2397 return err;
2398 cur++;
2400 while (cur > s->matched_entry->idx) {
2401 err = input_log_view(NULL, view, KEY_UP);
2402 if (err)
2403 return err;
2404 cur--;
2408 s->search_entry = NULL;
2410 return NULL;
2413 static const struct got_error *
2414 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2415 struct got_repository *repo, const char *head_ref_name,
2416 const char *in_repo_path, int log_branches)
2418 const struct got_error *err = NULL;
2419 struct tog_log_view_state *s = &view->state.log;
2420 struct got_repository *thread_repo = NULL;
2421 struct got_commit_graph *thread_graph = NULL;
2422 int errcode;
2424 if (in_repo_path != s->in_repo_path) {
2425 free(s->in_repo_path);
2426 s->in_repo_path = strdup(in_repo_path);
2427 if (s->in_repo_path == NULL)
2428 return got_error_from_errno("strdup");
2431 /* The commit queue only contains commits being displayed. */
2432 TAILQ_INIT(&s->commits.head);
2433 s->commits.ncommits = 0;
2435 s->repo = repo;
2436 if (head_ref_name) {
2437 s->head_ref_name = strdup(head_ref_name);
2438 if (s->head_ref_name == NULL) {
2439 err = got_error_from_errno("strdup");
2440 goto done;
2443 s->start_id = got_object_id_dup(start_id);
2444 if (s->start_id == NULL) {
2445 err = got_error_from_errno("got_object_id_dup");
2446 goto done;
2448 s->log_branches = log_branches;
2450 STAILQ_INIT(&s->colors);
2451 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2452 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2453 get_color_value("TOG_COLOR_COMMIT"));
2454 if (err)
2455 goto done;
2456 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2457 get_color_value("TOG_COLOR_AUTHOR"));
2458 if (err) {
2459 free_colors(&s->colors);
2460 goto done;
2462 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2463 get_color_value("TOG_COLOR_DATE"));
2464 if (err) {
2465 free_colors(&s->colors);
2466 goto done;
2470 view->show = show_log_view;
2471 view->input = input_log_view;
2472 view->close = close_log_view;
2473 view->search_start = search_start_log_view;
2474 view->search_next = search_next_log_view;
2476 if (s->thread_args.pack_fds == NULL) {
2477 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2478 if (err)
2479 goto done;
2481 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2482 s->thread_args.pack_fds);
2483 if (err)
2484 goto done;
2485 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2486 !s->log_branches);
2487 if (err)
2488 goto done;
2489 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2490 s->repo, NULL, NULL);
2491 if (err)
2492 goto done;
2494 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2495 if (errcode) {
2496 err = got_error_set_errno(errcode, "pthread_cond_init");
2497 goto done;
2499 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2500 if (errcode) {
2501 err = got_error_set_errno(errcode, "pthread_cond_init");
2502 goto done;
2505 s->thread_args.commits_needed = view->nlines;
2506 s->thread_args.graph = thread_graph;
2507 s->thread_args.commits = &s->commits;
2508 s->thread_args.in_repo_path = s->in_repo_path;
2509 s->thread_args.start_id = s->start_id;
2510 s->thread_args.repo = thread_repo;
2511 s->thread_args.log_complete = 0;
2512 s->thread_args.quit = &s->quit;
2513 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2514 s->thread_args.selected_entry = &s->selected_entry;
2515 s->thread_args.searching = &view->searching;
2516 s->thread_args.search_next_done = &view->search_next_done;
2517 s->thread_args.regex = &view->regex;
2518 done:
2519 if (err)
2520 close_log_view(view);
2521 return err;
2524 static const struct got_error *
2525 show_log_view(struct tog_view *view)
2527 const struct got_error *err;
2528 struct tog_log_view_state *s = &view->state.log;
2530 if (s->thread == NULL) {
2531 int errcode = pthread_create(&s->thread, NULL, log_thread,
2532 &s->thread_args);
2533 if (errcode)
2534 return got_error_set_errno(errcode, "pthread_create");
2535 if (s->thread_args.commits_needed > 0) {
2536 err = trigger_log_thread(view, 1);
2537 if (err)
2538 return err;
2542 return draw_commits(view);
2545 static const struct got_error *
2546 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2548 const struct got_error *err = NULL;
2549 struct tog_log_view_state *s = &view->state.log;
2550 struct tog_view *diff_view = NULL, *tree_view = NULL;
2551 struct tog_view *ref_view = NULL;
2552 struct commit_queue_entry *entry;
2553 int begin_x = 0, n, nscroll = view->nlines - 1;
2555 if (s->thread_args.load_all) {
2556 if (ch == KEY_BACKSPACE)
2557 s->thread_args.load_all = 0;
2558 else if (s->thread_args.log_complete) {
2559 s->thread_args.load_all = 0;
2560 log_scroll_down(view, s->commits.ncommits);
2561 s->selected = MIN(view->nlines - 2,
2562 s->commits.ncommits - 1);
2563 select_commit(s);
2565 return NULL;
2568 switch (ch) {
2569 case 'q':
2570 s->quit = 1;
2571 break;
2572 case '0':
2573 view->x = 0;
2574 break;
2575 case '$':
2576 view->x = MAX(view->maxx - view->ncols / 2, 0);
2577 break;
2578 case KEY_RIGHT:
2579 case 'l':
2580 if (view->x + view->ncols / 2 < view->maxx)
2581 view->x += 2; /* move two columns right */
2582 break;
2583 case KEY_LEFT:
2584 case 'h':
2585 view->x -= MIN(view->x, 2); /* move two columns back */
2586 break;
2587 case 'k':
2588 case KEY_UP:
2589 case '<':
2590 case ',':
2591 case CTRL('p'):
2592 if (s->first_displayed_entry == NULL)
2593 break;
2594 if (s->selected > 0)
2595 s->selected--;
2596 else
2597 log_scroll_up(s, 1);
2598 select_commit(s);
2599 break;
2600 case 'g':
2601 case KEY_HOME:
2602 s->selected = 0;
2603 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2604 select_commit(s);
2605 break;
2606 case CTRL('u'):
2607 case 'u':
2608 nscroll /= 2;
2609 /* FALL THROUGH */
2610 case KEY_PPAGE:
2611 case CTRL('b'):
2612 if (s->first_displayed_entry == NULL)
2613 break;
2614 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2615 s->selected = MAX(0, s->selected - nscroll - 1);
2616 else
2617 log_scroll_up(s, nscroll);
2618 select_commit(s);
2619 break;
2620 case 'j':
2621 case KEY_DOWN:
2622 case '>':
2623 case '.':
2624 case CTRL('n'):
2625 if (s->first_displayed_entry == NULL)
2626 break;
2627 if (s->selected < MIN(view->nlines - 2,
2628 s->commits.ncommits - 1))
2629 s->selected++;
2630 else {
2631 err = log_scroll_down(view, 1);
2632 if (err)
2633 break;
2635 select_commit(s);
2636 break;
2637 case 'G':
2638 case KEY_END: {
2639 /* We don't know yet how many commits, so we're forced to
2640 * traverse them all. */
2641 if (!s->thread_args.log_complete) {
2642 s->thread_args.load_all = 1;
2643 return trigger_log_thread(view, 0);
2646 s->selected = 0;
2647 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2648 for (n = 0; n < view->nlines - 1; n++) {
2649 if (entry == NULL)
2650 break;
2651 s->first_displayed_entry = entry;
2652 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2654 if (n > 0)
2655 s->selected = n - 1;
2656 select_commit(s);
2657 break;
2659 case CTRL('d'):
2660 case 'd':
2661 nscroll /= 2;
2662 /* FALL THROUGH */
2663 case KEY_NPAGE:
2664 case CTRL('f'): {
2665 struct commit_queue_entry *first;
2666 first = s->first_displayed_entry;
2667 if (first == NULL)
2668 break;
2669 err = log_scroll_down(view, nscroll);
2670 if (err)
2671 break;
2672 if (first == s->first_displayed_entry &&
2673 s->selected < MIN(view->nlines - 2,
2674 s->commits.ncommits - 1)) {
2675 /* can't scroll further down */
2676 s->selected += MIN(s->last_displayed_entry->idx -
2677 s->selected_entry->idx, nscroll + 1);
2679 select_commit(s);
2680 break;
2682 case KEY_RESIZE:
2683 if (s->selected > view->nlines - 2)
2684 s->selected = view->nlines - 2;
2685 if (s->selected > s->commits.ncommits - 1)
2686 s->selected = s->commits.ncommits - 1;
2687 select_commit(s);
2688 if (s->commits.ncommits < view->nlines - 1 &&
2689 !s->thread_args.log_complete) {
2690 s->thread_args.commits_needed += (view->nlines - 1) -
2691 s->commits.ncommits;
2692 err = trigger_log_thread(view, 1);
2694 break;
2695 case KEY_ENTER:
2696 case ' ':
2697 case '\r':
2698 if (s->selected_entry == NULL)
2699 break;
2700 if (view_is_parent_view(view))
2701 begin_x = view_split_begin_x(view->begin_x);
2702 err = open_diff_view_for_commit(&diff_view, begin_x,
2703 s->selected_entry->commit, s->selected_entry->id,
2704 view, s->repo);
2705 if (err)
2706 break;
2707 view->focussed = 0;
2708 diff_view->focussed = 1;
2709 if (view_is_parent_view(view)) {
2710 err = view_close_child(view);
2711 if (err)
2712 return err;
2713 err = view_set_child(view, diff_view);
2714 if (err)
2715 return err;
2716 view->focus_child = 1;
2717 } else
2718 *new_view = diff_view;
2719 break;
2720 case 't':
2721 if (s->selected_entry == NULL)
2722 break;
2723 if (view_is_parent_view(view))
2724 begin_x = view_split_begin_x(view->begin_x);
2725 err = browse_commit_tree(&tree_view, begin_x,
2726 s->selected_entry, s->in_repo_path, s->head_ref_name,
2727 s->repo);
2728 if (err)
2729 break;
2730 view->focussed = 0;
2731 tree_view->focussed = 1;
2732 if (view_is_parent_view(view)) {
2733 err = view_close_child(view);
2734 if (err)
2735 return err;
2736 err = view_set_child(view, tree_view);
2737 if (err)
2738 return err;
2739 view->focus_child = 1;
2740 } else
2741 *new_view = tree_view;
2742 break;
2743 case KEY_BACKSPACE:
2744 case CTRL('l'):
2745 case 'B':
2746 if (ch == KEY_BACKSPACE &&
2747 got_path_is_root_dir(s->in_repo_path))
2748 break;
2749 err = stop_log_thread(s);
2750 if (err)
2751 return err;
2752 if (ch == KEY_BACKSPACE) {
2753 char *parent_path;
2754 err = got_path_dirname(&parent_path, s->in_repo_path);
2755 if (err)
2756 return err;
2757 free(s->in_repo_path);
2758 s->in_repo_path = parent_path;
2759 s->thread_args.in_repo_path = s->in_repo_path;
2760 } else if (ch == CTRL('l')) {
2761 struct got_object_id *start_id;
2762 err = got_repo_match_object_id(&start_id, NULL,
2763 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2764 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2765 if (err)
2766 return err;
2767 free(s->start_id);
2768 s->start_id = start_id;
2769 s->thread_args.start_id = s->start_id;
2770 } else /* 'B' */
2771 s->log_branches = !s->log_branches;
2773 if (s->thread_args.pack_fds == NULL) {
2774 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2775 if (err)
2776 return err;
2778 err = got_repo_open(&s->thread_args.repo,
2779 got_repo_get_path(s->repo), NULL,
2780 s->thread_args.pack_fds);
2781 if (err)
2782 return err;
2783 tog_free_refs();
2784 err = tog_load_refs(s->repo, 0);
2785 if (err)
2786 return err;
2787 err = got_commit_graph_open(&s->thread_args.graph,
2788 s->in_repo_path, !s->log_branches);
2789 if (err)
2790 return err;
2791 err = got_commit_graph_iter_start(s->thread_args.graph,
2792 s->start_id, s->repo, NULL, NULL);
2793 if (err)
2794 return err;
2795 free_commits(&s->commits);
2796 s->first_displayed_entry = NULL;
2797 s->last_displayed_entry = NULL;
2798 s->selected_entry = NULL;
2799 s->selected = 0;
2800 s->thread_args.log_complete = 0;
2801 s->quit = 0;
2802 s->thread_args.commits_needed = view->nlines;
2803 break;
2804 case 'r':
2805 if (view_is_parent_view(view))
2806 begin_x = view_split_begin_x(view->begin_x);
2807 ref_view = view_open(view->nlines, view->ncols,
2808 view->begin_y, begin_x, TOG_VIEW_REF);
2809 if (ref_view == NULL)
2810 return got_error_from_errno("view_open");
2811 err = open_ref_view(ref_view, s->repo);
2812 if (err) {
2813 view_close(ref_view);
2814 return err;
2816 view->focussed = 0;
2817 ref_view->focussed = 1;
2818 if (view_is_parent_view(view)) {
2819 err = view_close_child(view);
2820 if (err)
2821 return err;
2822 err = view_set_child(view, ref_view);
2823 if (err)
2824 return err;
2825 view->focus_child = 1;
2826 } else
2827 *new_view = ref_view;
2828 break;
2829 default:
2830 break;
2833 return err;
2836 static const struct got_error *
2837 apply_unveil(const char *repo_path, const char *worktree_path)
2839 const struct got_error *error;
2841 #ifdef PROFILE
2842 if (unveil("gmon.out", "rwc") != 0)
2843 return got_error_from_errno2("unveil", "gmon.out");
2844 #endif
2845 if (repo_path && unveil(repo_path, "r") != 0)
2846 return got_error_from_errno2("unveil", repo_path);
2848 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2849 return got_error_from_errno2("unveil", worktree_path);
2851 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2852 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2854 error = got_privsep_unveil_exec_helpers();
2855 if (error != NULL)
2856 return error;
2858 if (unveil(NULL, NULL) != 0)
2859 return got_error_from_errno("unveil");
2861 return NULL;
2864 static void
2865 init_curses(void)
2868 * Override default signal handlers before starting ncurses.
2869 * This should prevent ncurses from installing its own
2870 * broken cleanup() signal handler.
2872 signal(SIGWINCH, tog_sigwinch);
2873 signal(SIGPIPE, tog_sigpipe);
2874 signal(SIGCONT, tog_sigcont);
2875 signal(SIGINT, tog_sigint);
2876 signal(SIGTERM, tog_sigterm);
2878 initscr();
2879 cbreak();
2880 halfdelay(1); /* Do fast refresh while initial view is loading. */
2881 noecho();
2882 nonl();
2883 intrflush(stdscr, FALSE);
2884 keypad(stdscr, TRUE);
2885 curs_set(0);
2886 if (getenv("TOG_COLORS") != NULL) {
2887 start_color();
2888 use_default_colors();
2892 static const struct got_error *
2893 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2894 struct got_repository *repo, struct got_worktree *worktree)
2896 const struct got_error *err = NULL;
2898 if (argc == 0) {
2899 *in_repo_path = strdup("/");
2900 if (*in_repo_path == NULL)
2901 return got_error_from_errno("strdup");
2902 return NULL;
2905 if (worktree) {
2906 const char *prefix = got_worktree_get_path_prefix(worktree);
2907 char *p;
2909 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2910 if (err)
2911 return err;
2912 if (asprintf(in_repo_path, "%s%s%s", prefix,
2913 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2914 p) == -1) {
2915 err = got_error_from_errno("asprintf");
2916 *in_repo_path = NULL;
2918 free(p);
2919 } else
2920 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2922 return err;
2925 static const struct got_error *
2926 cmd_log(int argc, char *argv[])
2928 const struct got_error *error;
2929 struct got_repository *repo = NULL;
2930 struct got_worktree *worktree = NULL;
2931 struct got_object_id *start_id = NULL;
2932 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2933 char *start_commit = NULL, *label = NULL;
2934 struct got_reference *ref = NULL;
2935 const char *head_ref_name = NULL;
2936 int ch, log_branches = 0;
2937 struct tog_view *view;
2938 int *pack_fds = NULL;
2940 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2941 switch (ch) {
2942 case 'b':
2943 log_branches = 1;
2944 break;
2945 case 'c':
2946 start_commit = optarg;
2947 break;
2948 case 'r':
2949 repo_path = realpath(optarg, NULL);
2950 if (repo_path == NULL)
2951 return got_error_from_errno2("realpath",
2952 optarg);
2953 break;
2954 default:
2955 usage_log();
2956 /* NOTREACHED */
2960 argc -= optind;
2961 argv += optind;
2963 if (argc > 1)
2964 usage_log();
2966 error = got_repo_pack_fds_open(&pack_fds);
2967 if (error != NULL)
2968 goto done;
2970 if (repo_path == NULL) {
2971 cwd = getcwd(NULL, 0);
2972 if (cwd == NULL)
2973 return got_error_from_errno("getcwd");
2974 error = got_worktree_open(&worktree, cwd);
2975 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2976 goto done;
2977 if (worktree)
2978 repo_path =
2979 strdup(got_worktree_get_repo_path(worktree));
2980 else
2981 repo_path = strdup(cwd);
2982 if (repo_path == NULL) {
2983 error = got_error_from_errno("strdup");
2984 goto done;
2988 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2989 if (error != NULL)
2990 goto done;
2992 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2993 repo, worktree);
2994 if (error)
2995 goto done;
2997 init_curses();
2999 error = apply_unveil(got_repo_get_path(repo),
3000 worktree ? got_worktree_get_root_path(worktree) : NULL);
3001 if (error)
3002 goto done;
3004 /* already loaded by tog_log_with_path()? */
3005 if (TAILQ_EMPTY(&tog_refs)) {
3006 error = tog_load_refs(repo, 0);
3007 if (error)
3008 goto done;
3011 if (start_commit == NULL) {
3012 error = got_repo_match_object_id(&start_id, &label,
3013 worktree ? got_worktree_get_head_ref_name(worktree) :
3014 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3015 if (error)
3016 goto done;
3017 head_ref_name = label;
3018 } else {
3019 error = got_ref_open(&ref, repo, start_commit, 0);
3020 if (error == NULL)
3021 head_ref_name = got_ref_get_name(ref);
3022 else if (error->code != GOT_ERR_NOT_REF)
3023 goto done;
3024 error = got_repo_match_object_id(&start_id, NULL,
3025 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3026 if (error)
3027 goto done;
3030 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3031 if (view == NULL) {
3032 error = got_error_from_errno("view_open");
3033 goto done;
3035 error = open_log_view(view, start_id, repo, head_ref_name,
3036 in_repo_path, log_branches);
3037 if (error)
3038 goto done;
3039 if (worktree) {
3040 /* Release work tree lock. */
3041 got_worktree_close(worktree);
3042 worktree = NULL;
3044 error = view_loop(view);
3045 done:
3046 free(in_repo_path);
3047 free(repo_path);
3048 free(cwd);
3049 free(start_id);
3050 free(label);
3051 if (ref)
3052 got_ref_close(ref);
3053 if (repo) {
3054 const struct got_error *close_err = got_repo_close(repo);
3055 if (error == NULL)
3056 error = close_err;
3058 if (worktree)
3059 got_worktree_close(worktree);
3060 if (pack_fds) {
3061 const struct got_error *pack_err =
3062 got_repo_pack_fds_close(pack_fds);
3063 if (error == NULL)
3064 error = pack_err;
3066 tog_free_refs();
3067 return error;
3070 __dead static void
3071 usage_diff(void)
3073 endwin();
3074 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3075 "[-w] object1 object2\n", getprogname());
3076 exit(1);
3079 static int
3080 match_line(const char *line, regex_t *regex, size_t nmatch,
3081 regmatch_t *regmatch)
3083 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3086 struct tog_color *
3087 match_color(struct tog_colors *colors, const char *line)
3089 struct tog_color *tc = NULL;
3091 STAILQ_FOREACH(tc, colors, entry) {
3092 if (match_line(line, &tc->regex, 0, NULL))
3093 return tc;
3096 return NULL;
3099 static const struct got_error *
3100 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3101 WINDOW *window, int skipcol, regmatch_t *regmatch)
3103 const struct got_error *err = NULL;
3104 char *exstr = NULL;
3105 wchar_t *wline = NULL;
3106 int rme, rms, n, width, scrollx;
3107 int width0 = 0, width1 = 0, width2 = 0;
3108 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3110 *wtotal = 0;
3112 rms = regmatch->rm_so;
3113 rme = regmatch->rm_eo;
3115 err = expand_tab(&exstr, line);
3116 if (err)
3117 return err;
3119 /* Split the line into 3 segments, according to match offsets. */
3120 seg0 = strndup(exstr, rms);
3121 if (seg0 == NULL) {
3122 err = got_error_from_errno("strndup");
3123 goto done;
3125 seg1 = strndup(exstr + rms, rme - rms);
3126 if (seg1 == NULL) {
3127 err = got_error_from_errno("strndup");
3128 goto done;
3130 seg2 = strdup(exstr + rme);
3131 if (seg2 == NULL) {
3132 err = got_error_from_errno("strndup");
3133 goto done;
3136 /* draw up to matched token if we haven't scrolled past it */
3137 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3138 col_tab_align, 1);
3139 if (err)
3140 goto done;
3141 n = MAX(width0 - skipcol, 0);
3142 if (n) {
3143 free(wline);
3144 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3145 wlimit, col_tab_align, 1);
3146 if (err)
3147 goto done;
3148 waddwstr(window, &wline[scrollx]);
3149 wlimit -= width;
3150 *wtotal += width;
3153 if (wlimit > 0) {
3154 int i = 0, w = 0;
3155 size_t wlen;
3157 free(wline);
3158 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3159 col_tab_align, 1);
3160 if (err)
3161 goto done;
3162 wlen = wcslen(wline);
3163 while (i < wlen) {
3164 width = wcwidth(wline[i]);
3165 if (width == -1) {
3166 /* should not happen, tabs are expanded */
3167 err = got_error(GOT_ERR_RANGE);
3168 goto done;
3170 if (width0 + w + width > skipcol)
3171 break;
3172 w += width;
3173 i++;
3175 /* draw (visible part of) matched token (if scrolled into it) */
3176 if (width1 - w > 0) {
3177 wattron(window, A_STANDOUT);
3178 waddwstr(window, &wline[i]);
3179 wattroff(window, A_STANDOUT);
3180 wlimit -= (width1 - w);
3181 *wtotal += (width1 - w);
3185 if (wlimit > 0) { /* draw rest of line */
3186 free(wline);
3187 if (skipcol > width0 + width1) {
3188 err = format_line(&wline, &width2, &scrollx, seg2,
3189 skipcol - (width0 + width1), wlimit,
3190 col_tab_align, 1);
3191 if (err)
3192 goto done;
3193 waddwstr(window, &wline[scrollx]);
3194 } else {
3195 err = format_line(&wline, &width2, NULL, seg2, 0,
3196 wlimit, col_tab_align, 1);
3197 if (err)
3198 goto done;
3199 waddwstr(window, wline);
3201 *wtotal += width2;
3203 done:
3204 free(wline);
3205 free(exstr);
3206 free(seg0);
3207 free(seg1);
3208 free(seg2);
3209 return err;
3212 static const struct got_error *
3213 draw_file(struct tog_view *view, const char *header)
3215 struct tog_diff_view_state *s = &view->state.diff;
3216 regmatch_t *regmatch = &view->regmatch;
3217 const struct got_error *err;
3218 int nprinted = 0;
3219 char *line;
3220 size_t linesize = 0;
3221 ssize_t linelen;
3222 struct tog_color *tc;
3223 wchar_t *wline;
3224 int width;
3225 int max_lines = view->nlines;
3226 int nlines = s->nlines;
3227 off_t line_offset;
3229 line_offset = s->line_offsets[s->first_displayed_line - 1];
3230 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3231 return got_error_from_errno("fseek");
3233 werase(view->window);
3235 if (header) {
3236 if (asprintf(&line, "[%d/%d] %s",
3237 s->first_displayed_line - 1 + s->selected_line, nlines,
3238 header) == -1)
3239 return got_error_from_errno("asprintf");
3240 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3241 0, 0);
3242 free(line);
3243 if (err)
3244 return err;
3246 if (view_needs_focus_indication(view))
3247 wstandout(view->window);
3248 waddwstr(view->window, wline);
3249 free(wline);
3250 wline = NULL;
3251 if (view_needs_focus_indication(view))
3252 wstandend(view->window);
3253 if (width <= view->ncols - 1)
3254 waddch(view->window, '\n');
3256 if (max_lines <= 1)
3257 return NULL;
3258 max_lines--;
3261 s->eof = 0;
3262 view->maxx = 0;
3263 line = NULL;
3264 while (max_lines > 0 && nprinted < max_lines) {
3265 linelen = getline(&line, &linesize, s->f);
3266 if (linelen == -1) {
3267 if (feof(s->f)) {
3268 s->eof = 1;
3269 break;
3271 free(line);
3272 return got_ferror(s->f, GOT_ERR_IO);
3275 /* Set view->maxx based on full line length. */
3276 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3277 view->x ? 1 : 0);
3278 if (err) {
3279 free(line);
3280 return err;
3282 view->maxx = MAX(view->maxx, width);
3283 free(wline);
3284 wline = NULL;
3286 tc = match_color(&s->colors, line);
3287 if (tc)
3288 wattr_on(view->window,
3289 COLOR_PAIR(tc->colorpair), NULL);
3290 if (s->first_displayed_line + nprinted == s->matched_line &&
3291 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3292 err = add_matched_line(&width, line, view->ncols, 0,
3293 view->window, view->x, regmatch);
3294 if (err) {
3295 free(line);
3296 return err;
3298 } else {
3299 int skip;
3300 err = format_line(&wline, &width, &skip, line,
3301 view->x, view->ncols, 0, view->x ? 1 : 0);
3302 if (err) {
3303 free(line);
3304 return err;
3306 waddwstr(view->window, &wline[skip]);
3307 free(wline);
3308 wline = NULL;
3310 if (tc)
3311 wattr_off(view->window,
3312 COLOR_PAIR(tc->colorpair), NULL);
3313 if (width <= view->ncols - 1)
3314 waddch(view->window, '\n');
3315 nprinted++;
3317 free(line);
3318 if (nprinted >= 1)
3319 s->last_displayed_line = s->first_displayed_line +
3320 (nprinted - 1);
3321 else
3322 s->last_displayed_line = s->first_displayed_line;
3324 view_vborder(view);
3326 if (s->eof) {
3327 while (nprinted < view->nlines) {
3328 waddch(view->window, '\n');
3329 nprinted++;
3332 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3333 view->ncols, 0, 0);
3334 if (err) {
3335 return err;
3338 wstandout(view->window);
3339 waddwstr(view->window, wline);
3340 free(wline);
3341 wline = NULL;
3342 wstandend(view->window);
3345 return NULL;
3348 static char *
3349 get_datestr(time_t *time, char *datebuf)
3351 struct tm mytm, *tm;
3352 char *p, *s;
3354 tm = gmtime_r(time, &mytm);
3355 if (tm == NULL)
3356 return NULL;
3357 s = asctime_r(tm, datebuf);
3358 if (s == NULL)
3359 return NULL;
3360 p = strchr(s, '\n');
3361 if (p)
3362 *p = '\0';
3363 return s;
3366 static const struct got_error *
3367 get_changed_paths(struct got_pathlist_head *paths,
3368 struct got_commit_object *commit, struct got_repository *repo)
3370 const struct got_error *err = NULL;
3371 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3372 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3373 struct got_object_qid *qid;
3375 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3376 if (qid != NULL) {
3377 struct got_commit_object *pcommit;
3378 err = got_object_open_as_commit(&pcommit, repo,
3379 &qid->id);
3380 if (err)
3381 return err;
3383 tree_id1 = got_object_id_dup(
3384 got_object_commit_get_tree_id(pcommit));
3385 if (tree_id1 == NULL) {
3386 got_object_commit_close(pcommit);
3387 return got_error_from_errno("got_object_id_dup");
3389 got_object_commit_close(pcommit);
3393 if (tree_id1) {
3394 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3395 if (err)
3396 goto done;
3399 tree_id2 = got_object_commit_get_tree_id(commit);
3400 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3401 if (err)
3402 goto done;
3404 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3405 got_diff_tree_collect_changed_paths, paths, 0);
3406 done:
3407 if (tree1)
3408 got_object_tree_close(tree1);
3409 if (tree2)
3410 got_object_tree_close(tree2);
3411 free(tree_id1);
3412 return err;
3415 static const struct got_error *
3416 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3418 off_t *p;
3420 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3421 if (p == NULL)
3422 return got_error_from_errno("reallocarray");
3423 *line_offsets = p;
3424 (*line_offsets)[*nlines] = off;
3425 (*nlines)++;
3426 return NULL;
3429 static const struct got_error *
3430 write_commit_info(off_t **line_offsets, size_t *nlines,
3431 struct got_object_id *commit_id, struct got_reflist_head *refs,
3432 struct got_repository *repo, FILE *outfile)
3434 const struct got_error *err = NULL;
3435 char datebuf[26], *datestr;
3436 struct got_commit_object *commit;
3437 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3438 time_t committer_time;
3439 const char *author, *committer;
3440 char *refs_str = NULL;
3441 struct got_pathlist_head changed_paths;
3442 struct got_pathlist_entry *pe;
3443 off_t outoff = 0;
3444 int n;
3446 TAILQ_INIT(&changed_paths);
3448 if (refs) {
3449 err = build_refs_str(&refs_str, refs, commit_id, repo);
3450 if (err)
3451 return err;
3454 err = got_object_open_as_commit(&commit, repo, commit_id);
3455 if (err)
3456 return err;
3458 err = got_object_id_str(&id_str, commit_id);
3459 if (err) {
3460 err = got_error_from_errno("got_object_id_str");
3461 goto done;
3464 err = add_line_offset(line_offsets, nlines, 0);
3465 if (err)
3466 goto done;
3468 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3469 refs_str ? refs_str : "", refs_str ? ")" : "");
3470 if (n < 0) {
3471 err = got_error_from_errno("fprintf");
3472 goto done;
3474 outoff += n;
3475 err = add_line_offset(line_offsets, nlines, outoff);
3476 if (err)
3477 goto done;
3479 n = fprintf(outfile, "from: %s\n",
3480 got_object_commit_get_author(commit));
3481 if (n < 0) {
3482 err = got_error_from_errno("fprintf");
3483 goto done;
3485 outoff += n;
3486 err = add_line_offset(line_offsets, nlines, outoff);
3487 if (err)
3488 goto done;
3490 committer_time = got_object_commit_get_committer_time(commit);
3491 datestr = get_datestr(&committer_time, datebuf);
3492 if (datestr) {
3493 n = fprintf(outfile, "date: %s UTC\n", datestr);
3494 if (n < 0) {
3495 err = got_error_from_errno("fprintf");
3496 goto done;
3498 outoff += n;
3499 err = add_line_offset(line_offsets, nlines, outoff);
3500 if (err)
3501 goto done;
3503 author = got_object_commit_get_author(commit);
3504 committer = got_object_commit_get_committer(commit);
3505 if (strcmp(author, committer) != 0) {
3506 n = fprintf(outfile, "via: %s\n", committer);
3507 if (n < 0) {
3508 err = got_error_from_errno("fprintf");
3509 goto done;
3511 outoff += n;
3512 err = add_line_offset(line_offsets, nlines, outoff);
3513 if (err)
3514 goto done;
3516 if (got_object_commit_get_nparents(commit) > 1) {
3517 const struct got_object_id_queue *parent_ids;
3518 struct got_object_qid *qid;
3519 int pn = 1;
3520 parent_ids = got_object_commit_get_parent_ids(commit);
3521 STAILQ_FOREACH(qid, parent_ids, entry) {
3522 err = got_object_id_str(&id_str, &qid->id);
3523 if (err)
3524 goto done;
3525 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3526 if (n < 0) {
3527 err = got_error_from_errno("fprintf");
3528 goto done;
3530 outoff += n;
3531 err = add_line_offset(line_offsets, nlines, outoff);
3532 if (err)
3533 goto done;
3534 free(id_str);
3535 id_str = NULL;
3539 err = got_object_commit_get_logmsg(&logmsg, commit);
3540 if (err)
3541 goto done;
3542 s = logmsg;
3543 while ((line = strsep(&s, "\n")) != NULL) {
3544 n = fprintf(outfile, "%s\n", line);
3545 if (n < 0) {
3546 err = got_error_from_errno("fprintf");
3547 goto done;
3549 outoff += n;
3550 err = add_line_offset(line_offsets, nlines, outoff);
3551 if (err)
3552 goto done;
3555 err = get_changed_paths(&changed_paths, commit, repo);
3556 if (err)
3557 goto done;
3558 TAILQ_FOREACH(pe, &changed_paths, entry) {
3559 struct got_diff_changed_path *cp = pe->data;
3560 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3561 if (n < 0) {
3562 err = got_error_from_errno("fprintf");
3563 goto done;
3565 outoff += n;
3566 err = add_line_offset(line_offsets, nlines, outoff);
3567 if (err)
3568 goto done;
3569 free((char *)pe->path);
3570 free(pe->data);
3573 fputc('\n', outfile);
3574 outoff++;
3575 err = add_line_offset(line_offsets, nlines, outoff);
3576 done:
3577 got_pathlist_free(&changed_paths);
3578 free(id_str);
3579 free(logmsg);
3580 free(refs_str);
3581 got_object_commit_close(commit);
3582 if (err) {
3583 free(*line_offsets);
3584 *line_offsets = NULL;
3585 *nlines = 0;
3587 return err;
3590 static const struct got_error *
3591 create_diff(struct tog_diff_view_state *s)
3593 const struct got_error *err = NULL;
3594 FILE *f = NULL;
3595 int obj_type;
3597 free(s->line_offsets);
3598 s->line_offsets = malloc(sizeof(off_t));
3599 if (s->line_offsets == NULL)
3600 return got_error_from_errno("malloc");
3601 s->nlines = 0;
3603 f = got_opentemp();
3604 if (f == NULL) {
3605 err = got_error_from_errno("got_opentemp");
3606 goto done;
3608 if (s->f && fclose(s->f) == EOF) {
3609 err = got_error_from_errno("fclose");
3610 goto done;
3612 s->f = f;
3614 if (s->id1)
3615 err = got_object_get_type(&obj_type, s->repo, s->id1);
3616 else
3617 err = got_object_get_type(&obj_type, s->repo, s->id2);
3618 if (err)
3619 goto done;
3621 switch (obj_type) {
3622 case GOT_OBJ_TYPE_BLOB:
3623 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3624 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3625 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3626 s->repo, s->f);
3627 break;
3628 case GOT_OBJ_TYPE_TREE:
3629 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3630 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3631 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3632 break;
3633 case GOT_OBJ_TYPE_COMMIT: {
3634 const struct got_object_id_queue *parent_ids;
3635 struct got_object_qid *pid;
3636 struct got_commit_object *commit2;
3637 struct got_reflist_head *refs;
3639 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3640 if (err)
3641 goto done;
3642 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3643 /* Show commit info if we're diffing to a parent/root commit. */
3644 if (s->id1 == NULL) {
3645 err = write_commit_info(&s->line_offsets, &s->nlines,
3646 s->id2, refs, s->repo, s->f);
3647 if (err)
3648 goto done;
3649 } else {
3650 parent_ids = got_object_commit_get_parent_ids(commit2);
3651 STAILQ_FOREACH(pid, parent_ids, entry) {
3652 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3653 err = write_commit_info(
3654 &s->line_offsets, &s->nlines,
3655 s->id2, refs, s->repo, s->f);
3656 if (err)
3657 goto done;
3658 break;
3662 got_object_commit_close(commit2);
3664 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3665 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3666 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3667 break;
3669 default:
3670 err = got_error(GOT_ERR_OBJ_TYPE);
3671 break;
3673 if (err)
3674 goto done;
3675 done:
3676 if (s->f && fflush(s->f) != 0 && err == NULL)
3677 err = got_error_from_errno("fflush");
3678 return err;
3681 static void
3682 diff_view_indicate_progress(struct tog_view *view)
3684 mvwaddstr(view->window, 0, 0, "diffing...");
3685 update_panels();
3686 doupdate();
3689 static const struct got_error *
3690 search_start_diff_view(struct tog_view *view)
3692 struct tog_diff_view_state *s = &view->state.diff;
3694 s->matched_line = 0;
3695 return NULL;
3698 static const struct got_error *
3699 search_next_diff_view(struct tog_view *view)
3701 struct tog_diff_view_state *s = &view->state.diff;
3702 const struct got_error *err = NULL;
3703 int lineno;
3704 char *line = NULL;
3705 size_t linesize = 0;
3706 ssize_t linelen;
3708 if (!view->searching) {
3709 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3710 return NULL;
3713 if (s->matched_line) {
3714 if (view->searching == TOG_SEARCH_FORWARD)
3715 lineno = s->matched_line + 1;
3716 else
3717 lineno = s->matched_line - 1;
3718 } else
3719 lineno = s->first_displayed_line;
3721 while (1) {
3722 off_t offset;
3724 if (lineno <= 0 || lineno > s->nlines) {
3725 if (s->matched_line == 0) {
3726 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3727 break;
3730 if (view->searching == TOG_SEARCH_FORWARD)
3731 lineno = 1;
3732 else
3733 lineno = s->nlines;
3736 offset = s->line_offsets[lineno - 1];
3737 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3738 free(line);
3739 return got_error_from_errno("fseeko");
3741 linelen = getline(&line, &linesize, s->f);
3742 if (linelen != -1) {
3743 char *exstr;
3744 err = expand_tab(&exstr, line);
3745 if (err)
3746 break;
3747 if (match_line(exstr, &view->regex, 1,
3748 &view->regmatch)) {
3749 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3750 s->matched_line = lineno;
3751 free(exstr);
3752 break;
3754 free(exstr);
3756 if (view->searching == TOG_SEARCH_FORWARD)
3757 lineno++;
3758 else
3759 lineno--;
3761 free(line);
3763 if (s->matched_line) {
3764 s->first_displayed_line = s->matched_line;
3765 s->selected_line = 1;
3768 return err;
3771 static const struct got_error *
3772 close_diff_view(struct tog_view *view)
3774 const struct got_error *err = NULL;
3775 struct tog_diff_view_state *s = &view->state.diff;
3777 free(s->id1);
3778 s->id1 = NULL;
3779 free(s->id2);
3780 s->id2 = NULL;
3781 if (s->f && fclose(s->f) == EOF)
3782 err = got_error_from_errno("fclose");
3783 s->f = NULL;
3784 if (s->f1 && fclose(s->f1) == EOF)
3785 err = got_error_from_errno("fclose");
3786 s->f1 = NULL;
3787 if (s->f2 && fclose(s->f2) == EOF)
3788 err = got_error_from_errno("fclose");
3789 s->f2 = NULL;
3790 free_colors(&s->colors);
3791 free(s->line_offsets);
3792 s->line_offsets = NULL;
3793 s->nlines = 0;
3794 return err;
3797 static const struct got_error *
3798 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3799 struct got_object_id *id2, const char *label1, const char *label2,
3800 int diff_context, int ignore_whitespace, int force_text_diff,
3801 struct tog_view *log_view, struct got_repository *repo)
3803 const struct got_error *err;
3804 struct tog_diff_view_state *s = &view->state.diff;
3806 memset(s, 0, sizeof(*s));
3808 if (id1 != NULL && id2 != NULL) {
3809 int type1, type2;
3810 err = got_object_get_type(&type1, repo, id1);
3811 if (err)
3812 return err;
3813 err = got_object_get_type(&type2, repo, id2);
3814 if (err)
3815 return err;
3817 if (type1 != type2)
3818 return got_error(GOT_ERR_OBJ_TYPE);
3820 s->first_displayed_line = 1;
3821 s->last_displayed_line = view->nlines;
3822 s->selected_line = 1;
3823 s->repo = repo;
3824 s->id1 = id1;
3825 s->id2 = id2;
3826 s->label1 = label1;
3827 s->label2 = label2;
3829 if (id1) {
3830 s->id1 = got_object_id_dup(id1);
3831 if (s->id1 == NULL)
3832 return got_error_from_errno("got_object_id_dup");
3833 s->f1 = got_opentemp();
3834 if (s->f1 == NULL) {
3835 err = got_error_from_errno("got_opentemp");
3836 goto done;
3838 } else
3839 s->id1 = NULL;
3841 s->id2 = got_object_id_dup(id2);
3842 if (s->id2 == NULL) {
3843 err = got_error_from_errno("got_object_id_dup");
3844 goto done;
3847 s->f2 = got_opentemp();
3848 if (s->f2 == NULL) {
3849 err = got_error_from_errno("got_opentemp");
3850 goto done;
3853 s->first_displayed_line = 1;
3854 s->last_displayed_line = view->nlines;
3855 s->diff_context = diff_context;
3856 s->ignore_whitespace = ignore_whitespace;
3857 s->force_text_diff = force_text_diff;
3858 s->log_view = log_view;
3859 s->repo = repo;
3861 STAILQ_INIT(&s->colors);
3862 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3863 err = add_color(&s->colors,
3864 "^-", TOG_COLOR_DIFF_MINUS,
3865 get_color_value("TOG_COLOR_DIFF_MINUS"));
3866 if (err)
3867 goto done;
3868 err = add_color(&s->colors, "^\\+",
3869 TOG_COLOR_DIFF_PLUS,
3870 get_color_value("TOG_COLOR_DIFF_PLUS"));
3871 if (err)
3872 goto done;
3873 err = add_color(&s->colors,
3874 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3875 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3876 if (err)
3877 goto done;
3879 err = add_color(&s->colors,
3880 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3881 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3882 get_color_value("TOG_COLOR_DIFF_META"));
3883 if (err)
3884 goto done;
3886 err = add_color(&s->colors,
3887 "^(from|via): ", TOG_COLOR_AUTHOR,
3888 get_color_value("TOG_COLOR_AUTHOR"));
3889 if (err)
3890 goto done;
3892 err = add_color(&s->colors,
3893 "^date: ", TOG_COLOR_DATE,
3894 get_color_value("TOG_COLOR_DATE"));
3895 if (err)
3896 goto done;
3899 if (log_view && view_is_splitscreen(view))
3900 show_log_view(log_view); /* draw vborder */
3901 diff_view_indicate_progress(view);
3903 err = create_diff(s);
3905 view->show = show_diff_view;
3906 view->input = input_diff_view;
3907 view->close = close_diff_view;
3908 view->search_start = search_start_diff_view;
3909 view->search_next = search_next_diff_view;
3910 done:
3911 if (err)
3912 close_diff_view(view);
3913 return err;
3916 static const struct got_error *
3917 show_diff_view(struct tog_view *view)
3919 const struct got_error *err;
3920 struct tog_diff_view_state *s = &view->state.diff;
3921 char *id_str1 = NULL, *id_str2, *header;
3922 const char *label1, *label2;
3924 if (s->id1) {
3925 err = got_object_id_str(&id_str1, s->id1);
3926 if (err)
3927 return err;
3928 label1 = s->label1 ? : id_str1;
3929 } else
3930 label1 = "/dev/null";
3932 err = got_object_id_str(&id_str2, s->id2);
3933 if (err)
3934 return err;
3935 label2 = s->label2 ? : id_str2;
3937 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3938 err = got_error_from_errno("asprintf");
3939 free(id_str1);
3940 free(id_str2);
3941 return err;
3943 free(id_str1);
3944 free(id_str2);
3946 err = draw_file(view, header);
3947 free(header);
3948 return err;
3951 static const struct got_error *
3952 set_selected_commit(struct tog_diff_view_state *s,
3953 struct commit_queue_entry *entry)
3955 const struct got_error *err;
3956 const struct got_object_id_queue *parent_ids;
3957 struct got_commit_object *selected_commit;
3958 struct got_object_qid *pid;
3960 free(s->id2);
3961 s->id2 = got_object_id_dup(entry->id);
3962 if (s->id2 == NULL)
3963 return got_error_from_errno("got_object_id_dup");
3965 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3966 if (err)
3967 return err;
3968 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3969 free(s->id1);
3970 pid = STAILQ_FIRST(parent_ids);
3971 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3972 got_object_commit_close(selected_commit);
3973 return NULL;
3976 static const struct got_error *
3977 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3979 const struct got_error *err = NULL;
3980 struct tog_diff_view_state *s = &view->state.diff;
3981 struct tog_log_view_state *ls;
3982 struct commit_queue_entry *old_selected_entry;
3983 char *line = NULL;
3984 size_t linesize = 0;
3985 ssize_t linelen;
3986 int i, nscroll = view->nlines - 1;
3988 switch (ch) {
3989 case '0':
3990 view->x = 0;
3991 break;
3992 case '$':
3993 view->x = MAX(view->maxx - view->ncols / 3, 0);
3994 break;
3995 case KEY_RIGHT:
3996 case 'l':
3997 if (view->x + view->ncols / 3 < view->maxx)
3998 view->x += 2; /* move two columns right */
3999 break;
4000 case KEY_LEFT:
4001 case 'h':
4002 view->x -= MIN(view->x, 2); /* move two columns back */
4003 break;
4004 case 'a':
4005 case 'w':
4006 if (ch == 'a')
4007 s->force_text_diff = !s->force_text_diff;
4008 if (ch == 'w')
4009 s->ignore_whitespace = !s->ignore_whitespace;
4010 wclear(view->window);
4011 s->first_displayed_line = 1;
4012 s->last_displayed_line = view->nlines;
4013 s->matched_line = 0;
4014 diff_view_indicate_progress(view);
4015 err = create_diff(s);
4016 break;
4017 case 'g':
4018 case KEY_HOME:
4019 s->first_displayed_line = 1;
4020 break;
4021 case 'G':
4022 case KEY_END:
4023 if (s->eof)
4024 break;
4026 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4027 s->eof = 1;
4028 break;
4029 case 'k':
4030 case KEY_UP:
4031 case CTRL('p'):
4032 if (s->first_displayed_line > 1)
4033 s->first_displayed_line--;
4034 break;
4035 case CTRL('u'):
4036 case 'u':
4037 nscroll /= 2;
4038 /* FALL THROUGH */
4039 case KEY_PPAGE:
4040 case CTRL('b'):
4041 if (s->first_displayed_line == 1)
4042 break;
4043 i = 0;
4044 while (i++ < nscroll && s->first_displayed_line > 1)
4045 s->first_displayed_line--;
4046 break;
4047 case 'j':
4048 case KEY_DOWN:
4049 case CTRL('n'):
4050 if (!s->eof)
4051 s->first_displayed_line++;
4052 break;
4053 case CTRL('d'):
4054 case 'd':
4055 nscroll /= 2;
4056 /* FALL THROUGH */
4057 case KEY_NPAGE:
4058 case CTRL('f'):
4059 case ' ':
4060 if (s->eof)
4061 break;
4062 i = 0;
4063 while (!s->eof && i++ < nscroll) {
4064 linelen = getline(&line, &linesize, s->f);
4065 s->first_displayed_line++;
4066 if (linelen == -1) {
4067 if (feof(s->f)) {
4068 s->eof = 1;
4069 } else
4070 err = got_ferror(s->f, GOT_ERR_IO);
4071 break;
4074 free(line);
4075 break;
4076 case '[':
4077 if (s->diff_context > 0) {
4078 s->diff_context--;
4079 s->matched_line = 0;
4080 diff_view_indicate_progress(view);
4081 err = create_diff(s);
4082 if (s->first_displayed_line + view->nlines - 1 >
4083 s->nlines) {
4084 s->first_displayed_line = 1;
4085 s->last_displayed_line = view->nlines;
4088 break;
4089 case ']':
4090 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4091 s->diff_context++;
4092 s->matched_line = 0;
4093 diff_view_indicate_progress(view);
4094 err = create_diff(s);
4096 break;
4097 case '<':
4098 case ',':
4099 if (s->log_view == NULL)
4100 break;
4101 ls = &s->log_view->state.log;
4102 old_selected_entry = ls->selected_entry;
4104 err = input_log_view(NULL, s->log_view, KEY_UP);
4105 if (err)
4106 break;
4108 if (old_selected_entry == ls->selected_entry)
4109 break;
4111 err = set_selected_commit(s, ls->selected_entry);
4112 if (err)
4113 break;
4115 s->first_displayed_line = 1;
4116 s->last_displayed_line = view->nlines;
4117 s->matched_line = 0;
4118 view->x = 0;
4120 diff_view_indicate_progress(view);
4121 err = create_diff(s);
4122 break;
4123 case '>':
4124 case '.':
4125 if (s->log_view == NULL)
4126 break;
4127 ls = &s->log_view->state.log;
4128 old_selected_entry = ls->selected_entry;
4130 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4131 if (err)
4132 break;
4134 if (old_selected_entry == ls->selected_entry)
4135 break;
4137 err = set_selected_commit(s, ls->selected_entry);
4138 if (err)
4139 break;
4141 s->first_displayed_line = 1;
4142 s->last_displayed_line = view->nlines;
4143 s->matched_line = 0;
4144 view->x = 0;
4146 diff_view_indicate_progress(view);
4147 err = create_diff(s);
4148 break;
4149 default:
4150 break;
4153 return err;
4156 static const struct got_error *
4157 cmd_diff(int argc, char *argv[])
4159 const struct got_error *error = NULL;
4160 struct got_repository *repo = NULL;
4161 struct got_worktree *worktree = NULL;
4162 struct got_object_id *id1 = NULL, *id2 = NULL;
4163 char *repo_path = NULL, *cwd = NULL;
4164 char *id_str1 = NULL, *id_str2 = NULL;
4165 char *label1 = NULL, *label2 = NULL;
4166 int diff_context = 3, ignore_whitespace = 0;
4167 int ch, force_text_diff = 0;
4168 const char *errstr;
4169 struct tog_view *view;
4170 int *pack_fds = NULL;
4172 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4173 switch (ch) {
4174 case 'a':
4175 force_text_diff = 1;
4176 break;
4177 case 'C':
4178 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4179 &errstr);
4180 if (errstr != NULL)
4181 errx(1, "number of context lines is %s: %s",
4182 errstr, errstr);
4183 break;
4184 case 'r':
4185 repo_path = realpath(optarg, NULL);
4186 if (repo_path == NULL)
4187 return got_error_from_errno2("realpath",
4188 optarg);
4189 got_path_strip_trailing_slashes(repo_path);
4190 break;
4191 case 'w':
4192 ignore_whitespace = 1;
4193 break;
4194 default:
4195 usage_diff();
4196 /* NOTREACHED */
4200 argc -= optind;
4201 argv += optind;
4203 if (argc == 0) {
4204 usage_diff(); /* TODO show local worktree changes */
4205 } else if (argc == 2) {
4206 id_str1 = argv[0];
4207 id_str2 = argv[1];
4208 } else
4209 usage_diff();
4211 error = got_repo_pack_fds_open(&pack_fds);
4212 if (error)
4213 goto done;
4215 if (repo_path == NULL) {
4216 cwd = getcwd(NULL, 0);
4217 if (cwd == NULL)
4218 return got_error_from_errno("getcwd");
4219 error = got_worktree_open(&worktree, cwd);
4220 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4221 goto done;
4222 if (worktree)
4223 repo_path =
4224 strdup(got_worktree_get_repo_path(worktree));
4225 else
4226 repo_path = strdup(cwd);
4227 if (repo_path == NULL) {
4228 error = got_error_from_errno("strdup");
4229 goto done;
4233 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4234 if (error)
4235 goto done;
4237 init_curses();
4239 error = apply_unveil(got_repo_get_path(repo), NULL);
4240 if (error)
4241 goto done;
4243 error = tog_load_refs(repo, 0);
4244 if (error)
4245 goto done;
4247 error = got_repo_match_object_id(&id1, &label1, id_str1,
4248 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4249 if (error)
4250 goto done;
4252 error = got_repo_match_object_id(&id2, &label2, id_str2,
4253 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4254 if (error)
4255 goto done;
4257 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4258 if (view == NULL) {
4259 error = got_error_from_errno("view_open");
4260 goto done;
4262 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4263 ignore_whitespace, force_text_diff, NULL, repo);
4264 if (error)
4265 goto done;
4266 error = view_loop(view);
4267 done:
4268 free(label1);
4269 free(label2);
4270 free(repo_path);
4271 free(cwd);
4272 if (repo) {
4273 const struct got_error *close_err = got_repo_close(repo);
4274 if (error == NULL)
4275 error = close_err;
4277 if (worktree)
4278 got_worktree_close(worktree);
4279 if (pack_fds) {
4280 const struct got_error *pack_err =
4281 got_repo_pack_fds_close(pack_fds);
4282 if (error == NULL)
4283 error = pack_err;
4285 tog_free_refs();
4286 return error;
4289 __dead static void
4290 usage_blame(void)
4292 endwin();
4293 fprintf(stderr,
4294 "usage: %s blame [-c commit] [-r repository-path] path\n",
4295 getprogname());
4296 exit(1);
4299 struct tog_blame_line {
4300 int annotated;
4301 struct got_object_id *id;
4304 static const struct got_error *
4305 draw_blame(struct tog_view *view)
4307 struct tog_blame_view_state *s = &view->state.blame;
4308 struct tog_blame *blame = &s->blame;
4309 regmatch_t *regmatch = &view->regmatch;
4310 const struct got_error *err;
4311 int lineno = 0, nprinted = 0;
4312 char *line = NULL;
4313 size_t linesize = 0;
4314 ssize_t linelen;
4315 wchar_t *wline;
4316 int width;
4317 struct tog_blame_line *blame_line;
4318 struct got_object_id *prev_id = NULL;
4319 char *id_str;
4320 struct tog_color *tc;
4322 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4323 if (err)
4324 return err;
4326 rewind(blame->f);
4327 werase(view->window);
4329 if (asprintf(&line, "commit %s", id_str) == -1) {
4330 err = got_error_from_errno("asprintf");
4331 free(id_str);
4332 return err;
4335 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4336 free(line);
4337 line = NULL;
4338 if (err)
4339 return err;
4340 if (view_needs_focus_indication(view))
4341 wstandout(view->window);
4342 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4343 if (tc)
4344 wattr_on(view->window,
4345 COLOR_PAIR(tc->colorpair), NULL);
4346 waddwstr(view->window, wline);
4347 if (tc)
4348 wattr_off(view->window,
4349 COLOR_PAIR(tc->colorpair), NULL);
4350 if (view_needs_focus_indication(view))
4351 wstandend(view->window);
4352 free(wline);
4353 wline = NULL;
4354 if (width < view->ncols - 1)
4355 waddch(view->window, '\n');
4357 if (asprintf(&line, "[%d/%d] %s%s",
4358 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4359 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4360 free(id_str);
4361 return got_error_from_errno("asprintf");
4363 free(id_str);
4364 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4365 free(line);
4366 line = NULL;
4367 if (err)
4368 return err;
4369 waddwstr(view->window, wline);
4370 free(wline);
4371 wline = NULL;
4372 if (width < view->ncols - 1)
4373 waddch(view->window, '\n');
4375 s->eof = 0;
4376 view->maxx = 0;
4377 while (nprinted < view->nlines - 2) {
4378 linelen = getline(&line, &linesize, blame->f);
4379 if (linelen == -1) {
4380 if (feof(blame->f)) {
4381 s->eof = 1;
4382 break;
4384 free(line);
4385 return got_ferror(blame->f, GOT_ERR_IO);
4387 if (++lineno < s->first_displayed_line)
4388 continue;
4390 /* Set view->maxx based on full line length. */
4391 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4392 if (err) {
4393 free(line);
4394 return err;
4396 free(wline);
4397 wline = NULL;
4398 view->maxx = MAX(view->maxx, width);
4400 if (view->focussed && nprinted == s->selected_line - 1)
4401 wstandout(view->window);
4403 if (blame->nlines > 0) {
4404 blame_line = &blame->lines[lineno - 1];
4405 if (blame_line->annotated && prev_id &&
4406 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4407 !(view->focussed &&
4408 nprinted == s->selected_line - 1)) {
4409 waddstr(view->window, " ");
4410 } else if (blame_line->annotated) {
4411 char *id_str;
4412 err = got_object_id_str(&id_str,
4413 blame_line->id);
4414 if (err) {
4415 free(line);
4416 return err;
4418 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4419 if (tc)
4420 wattr_on(view->window,
4421 COLOR_PAIR(tc->colorpair), NULL);
4422 wprintw(view->window, "%.8s", id_str);
4423 if (tc)
4424 wattr_off(view->window,
4425 COLOR_PAIR(tc->colorpair), NULL);
4426 free(id_str);
4427 prev_id = blame_line->id;
4428 } else {
4429 waddstr(view->window, "........");
4430 prev_id = NULL;
4432 } else {
4433 waddstr(view->window, "........");
4434 prev_id = NULL;
4437 if (view->focussed && nprinted == s->selected_line - 1)
4438 wstandend(view->window);
4439 waddstr(view->window, " ");
4441 if (view->ncols <= 9) {
4442 width = 9;
4443 } else if (s->first_displayed_line + nprinted ==
4444 s->matched_line &&
4445 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4446 err = add_matched_line(&width, line, view->ncols - 9, 9,
4447 view->window, view->x, regmatch);
4448 if (err) {
4449 free(line);
4450 return err;
4452 width += 9;
4453 } else {
4454 int skip;
4455 err = format_line(&wline, &width, &skip, line,
4456 view->x, view->ncols - 9, 9, 1);
4457 if (err) {
4458 free(line);
4459 return err;
4461 waddwstr(view->window, &wline[skip]);
4462 width += 9;
4463 free(wline);
4464 wline = NULL;
4467 if (width <= view->ncols - 1)
4468 waddch(view->window, '\n');
4469 if (++nprinted == 1)
4470 s->first_displayed_line = lineno;
4472 free(line);
4473 s->last_displayed_line = lineno;
4475 view_vborder(view);
4477 return NULL;
4480 static const struct got_error *
4481 blame_cb(void *arg, int nlines, int lineno,
4482 struct got_commit_object *commit, struct got_object_id *id)
4484 const struct got_error *err = NULL;
4485 struct tog_blame_cb_args *a = arg;
4486 struct tog_blame_line *line;
4487 int errcode;
4489 if (nlines != a->nlines ||
4490 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4491 return got_error(GOT_ERR_RANGE);
4493 errcode = pthread_mutex_lock(&tog_mutex);
4494 if (errcode)
4495 return got_error_set_errno(errcode, "pthread_mutex_lock");
4497 if (*a->quit) { /* user has quit the blame view */
4498 err = got_error(GOT_ERR_ITER_COMPLETED);
4499 goto done;
4502 if (lineno == -1)
4503 goto done; /* no change in this commit */
4505 line = &a->lines[lineno - 1];
4506 if (line->annotated)
4507 goto done;
4509 line->id = got_object_id_dup(id);
4510 if (line->id == NULL) {
4511 err = got_error_from_errno("got_object_id_dup");
4512 goto done;
4514 line->annotated = 1;
4515 done:
4516 errcode = pthread_mutex_unlock(&tog_mutex);
4517 if (errcode)
4518 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4519 return err;
4522 static void *
4523 blame_thread(void *arg)
4525 const struct got_error *err, *close_err;
4526 struct tog_blame_thread_args *ta = arg;
4527 struct tog_blame_cb_args *a = ta->cb_args;
4528 int errcode;
4530 err = block_signals_used_by_main_thread();
4531 if (err)
4532 return (void *)err;
4534 err = got_blame(ta->path, a->commit_id, ta->repo,
4535 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4536 if (err && err->code == GOT_ERR_CANCELLED)
4537 err = NULL;
4539 errcode = pthread_mutex_lock(&tog_mutex);
4540 if (errcode)
4541 return (void *)got_error_set_errno(errcode,
4542 "pthread_mutex_lock");
4544 close_err = got_repo_close(ta->repo);
4545 if (err == NULL)
4546 err = close_err;
4547 ta->repo = NULL;
4548 *ta->complete = 1;
4550 errcode = pthread_mutex_unlock(&tog_mutex);
4551 if (errcode && err == NULL)
4552 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4554 return (void *)err;
4557 static struct got_object_id *
4558 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4559 int first_displayed_line, int selected_line)
4561 struct tog_blame_line *line;
4563 if (nlines <= 0)
4564 return NULL;
4566 line = &lines[first_displayed_line - 1 + selected_line - 1];
4567 if (!line->annotated)
4568 return NULL;
4570 return line->id;
4573 static const struct got_error *
4574 stop_blame(struct tog_blame *blame)
4576 const struct got_error *err = NULL;
4577 int i;
4579 if (blame->thread) {
4580 int errcode;
4581 errcode = pthread_mutex_unlock(&tog_mutex);
4582 if (errcode)
4583 return got_error_set_errno(errcode,
4584 "pthread_mutex_unlock");
4585 errcode = pthread_join(blame->thread, (void **)&err);
4586 if (errcode)
4587 return got_error_set_errno(errcode, "pthread_join");
4588 errcode = pthread_mutex_lock(&tog_mutex);
4589 if (errcode)
4590 return got_error_set_errno(errcode,
4591 "pthread_mutex_lock");
4592 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4593 err = NULL;
4594 blame->thread = NULL;
4596 if (blame->thread_args.repo) {
4597 const struct got_error *close_err;
4598 close_err = got_repo_close(blame->thread_args.repo);
4599 if (err == NULL)
4600 err = close_err;
4601 blame->thread_args.repo = NULL;
4603 if (blame->f) {
4604 if (fclose(blame->f) == EOF && err == NULL)
4605 err = got_error_from_errno("fclose");
4606 blame->f = NULL;
4608 if (blame->lines) {
4609 for (i = 0; i < blame->nlines; i++)
4610 free(blame->lines[i].id);
4611 free(blame->lines);
4612 blame->lines = NULL;
4614 free(blame->cb_args.commit_id);
4615 blame->cb_args.commit_id = NULL;
4616 if (blame->pack_fds) {
4617 const struct got_error *pack_err =
4618 got_repo_pack_fds_close(blame->pack_fds);
4619 if (err == NULL)
4620 err = pack_err;
4621 blame->pack_fds = NULL;
4623 return err;
4626 static const struct got_error *
4627 cancel_blame_view(void *arg)
4629 const struct got_error *err = NULL;
4630 int *done = arg;
4631 int errcode;
4633 errcode = pthread_mutex_lock(&tog_mutex);
4634 if (errcode)
4635 return got_error_set_errno(errcode,
4636 "pthread_mutex_unlock");
4638 if (*done)
4639 err = got_error(GOT_ERR_CANCELLED);
4641 errcode = pthread_mutex_unlock(&tog_mutex);
4642 if (errcode)
4643 return got_error_set_errno(errcode,
4644 "pthread_mutex_lock");
4646 return err;
4649 static const struct got_error *
4650 run_blame(struct tog_view *view)
4652 struct tog_blame_view_state *s = &view->state.blame;
4653 struct tog_blame *blame = &s->blame;
4654 const struct got_error *err = NULL;
4655 struct got_commit_object *commit = NULL;
4656 struct got_blob_object *blob = NULL;
4657 struct got_repository *thread_repo = NULL;
4658 struct got_object_id *obj_id = NULL;
4659 int obj_type;
4660 int *pack_fds = NULL;
4662 err = got_object_open_as_commit(&commit, s->repo,
4663 &s->blamed_commit->id);
4664 if (err)
4665 return err;
4667 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4668 if (err)
4669 goto done;
4671 err = got_object_get_type(&obj_type, s->repo, obj_id);
4672 if (err)
4673 goto done;
4675 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4676 err = got_error(GOT_ERR_OBJ_TYPE);
4677 goto done;
4680 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4681 if (err)
4682 goto done;
4683 blame->f = got_opentemp();
4684 if (blame->f == NULL) {
4685 err = got_error_from_errno("got_opentemp");
4686 goto done;
4688 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4689 &blame->line_offsets, blame->f, blob);
4690 if (err)
4691 goto done;
4692 if (blame->nlines == 0) {
4693 s->blame_complete = 1;
4694 goto done;
4697 /* Don't include \n at EOF in the blame line count. */
4698 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4699 blame->nlines--;
4701 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4702 if (blame->lines == NULL) {
4703 err = got_error_from_errno("calloc");
4704 goto done;
4707 err = got_repo_pack_fds_open(&pack_fds);
4708 if (err)
4709 goto done;
4710 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4711 pack_fds);
4712 if (err)
4713 goto done;
4715 blame->pack_fds = pack_fds;
4716 blame->cb_args.view = view;
4717 blame->cb_args.lines = blame->lines;
4718 blame->cb_args.nlines = blame->nlines;
4719 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4720 if (blame->cb_args.commit_id == NULL) {
4721 err = got_error_from_errno("got_object_id_dup");
4722 goto done;
4724 blame->cb_args.quit = &s->done;
4726 blame->thread_args.path = s->path;
4727 blame->thread_args.repo = thread_repo;
4728 blame->thread_args.cb_args = &blame->cb_args;
4729 blame->thread_args.complete = &s->blame_complete;
4730 blame->thread_args.cancel_cb = cancel_blame_view;
4731 blame->thread_args.cancel_arg = &s->done;
4732 s->blame_complete = 0;
4734 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4735 s->first_displayed_line = 1;
4736 s->last_displayed_line = view->nlines;
4737 s->selected_line = 1;
4739 s->matched_line = 0;
4741 done:
4742 if (commit)
4743 got_object_commit_close(commit);
4744 if (blob)
4745 got_object_blob_close(blob);
4746 free(obj_id);
4747 if (err)
4748 stop_blame(blame);
4749 return err;
4752 static const struct got_error *
4753 open_blame_view(struct tog_view *view, char *path,
4754 struct got_object_id *commit_id, struct got_repository *repo)
4756 const struct got_error *err = NULL;
4757 struct tog_blame_view_state *s = &view->state.blame;
4759 STAILQ_INIT(&s->blamed_commits);
4761 s->path = strdup(path);
4762 if (s->path == NULL)
4763 return got_error_from_errno("strdup");
4765 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4766 if (err) {
4767 free(s->path);
4768 return err;
4771 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4772 s->first_displayed_line = 1;
4773 s->last_displayed_line = view->nlines;
4774 s->selected_line = 1;
4775 s->blame_complete = 0;
4776 s->repo = repo;
4777 s->commit_id = commit_id;
4778 memset(&s->blame, 0, sizeof(s->blame));
4780 STAILQ_INIT(&s->colors);
4781 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4782 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4783 get_color_value("TOG_COLOR_COMMIT"));
4784 if (err)
4785 return err;
4788 view->show = show_blame_view;
4789 view->input = input_blame_view;
4790 view->close = close_blame_view;
4791 view->search_start = search_start_blame_view;
4792 view->search_next = search_next_blame_view;
4794 return run_blame(view);
4797 static const struct got_error *
4798 close_blame_view(struct tog_view *view)
4800 const struct got_error *err = NULL;
4801 struct tog_blame_view_state *s = &view->state.blame;
4803 if (s->blame.thread)
4804 err = stop_blame(&s->blame);
4806 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4807 struct got_object_qid *blamed_commit;
4808 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4809 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4810 got_object_qid_free(blamed_commit);
4813 free(s->path);
4814 free_colors(&s->colors);
4815 return err;
4818 static const struct got_error *
4819 search_start_blame_view(struct tog_view *view)
4821 struct tog_blame_view_state *s = &view->state.blame;
4823 s->matched_line = 0;
4824 return NULL;
4827 static const struct got_error *
4828 search_next_blame_view(struct tog_view *view)
4830 struct tog_blame_view_state *s = &view->state.blame;
4831 const struct got_error *err = NULL;
4832 int lineno;
4833 char *line = NULL;
4834 size_t linesize = 0;
4835 ssize_t linelen;
4837 if (!view->searching) {
4838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4839 return NULL;
4842 if (s->matched_line) {
4843 if (view->searching == TOG_SEARCH_FORWARD)
4844 lineno = s->matched_line + 1;
4845 else
4846 lineno = s->matched_line - 1;
4847 } else
4848 lineno = s->first_displayed_line - 1 + s->selected_line;
4850 while (1) {
4851 off_t offset;
4853 if (lineno <= 0 || lineno > s->blame.nlines) {
4854 if (s->matched_line == 0) {
4855 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4856 break;
4859 if (view->searching == TOG_SEARCH_FORWARD)
4860 lineno = 1;
4861 else
4862 lineno = s->blame.nlines;
4865 offset = s->blame.line_offsets[lineno - 1];
4866 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4867 free(line);
4868 return got_error_from_errno("fseeko");
4870 linelen = getline(&line, &linesize, s->blame.f);
4871 if (linelen != -1) {
4872 char *exstr;
4873 err = expand_tab(&exstr, line);
4874 if (err)
4875 break;
4876 if (match_line(exstr, &view->regex, 1,
4877 &view->regmatch)) {
4878 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4879 s->matched_line = lineno;
4880 free(exstr);
4881 break;
4883 free(exstr);
4885 if (view->searching == TOG_SEARCH_FORWARD)
4886 lineno++;
4887 else
4888 lineno--;
4890 free(line);
4892 if (s->matched_line) {
4893 s->first_displayed_line = s->matched_line;
4894 s->selected_line = 1;
4897 return err;
4900 static const struct got_error *
4901 show_blame_view(struct tog_view *view)
4903 const struct got_error *err = NULL;
4904 struct tog_blame_view_state *s = &view->state.blame;
4905 int errcode;
4907 if (s->blame.thread == NULL && !s->blame_complete) {
4908 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4909 &s->blame.thread_args);
4910 if (errcode)
4911 return got_error_set_errno(errcode, "pthread_create");
4913 halfdelay(1); /* fast refresh while annotating */
4916 if (s->blame_complete)
4917 halfdelay(10); /* disable fast refresh */
4919 err = draw_blame(view);
4921 view_vborder(view);
4922 return err;
4925 static const struct got_error *
4926 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4928 const struct got_error *err = NULL, *thread_err = NULL;
4929 struct tog_view *diff_view;
4930 struct tog_blame_view_state *s = &view->state.blame;
4931 int begin_x = 0, nscroll = view->nlines - 2;
4933 switch (ch) {
4934 case '0':
4935 view->x = 0;
4936 break;
4937 case '$':
4938 view->x = MAX(view->maxx - view->ncols / 3, 0);
4939 break;
4940 case KEY_RIGHT:
4941 case 'l':
4942 if (view->x + view->ncols / 3 < view->maxx)
4943 view->x += 2; /* move two columns right */
4944 break;
4945 case KEY_LEFT:
4946 case 'h':
4947 view->x -= MIN(view->x, 2); /* move two columns back */
4948 break;
4949 case 'q':
4950 s->done = 1;
4951 break;
4952 case 'g':
4953 case KEY_HOME:
4954 s->selected_line = 1;
4955 s->first_displayed_line = 1;
4956 break;
4957 case 'G':
4958 case KEY_END:
4959 if (s->blame.nlines < view->nlines - 2) {
4960 s->selected_line = s->blame.nlines;
4961 s->first_displayed_line = 1;
4962 } else {
4963 s->selected_line = view->nlines - 2;
4964 s->first_displayed_line = s->blame.nlines -
4965 (view->nlines - 3);
4967 break;
4968 case 'k':
4969 case KEY_UP:
4970 case CTRL('p'):
4971 if (s->selected_line > 1)
4972 s->selected_line--;
4973 else if (s->selected_line == 1 &&
4974 s->first_displayed_line > 1)
4975 s->first_displayed_line--;
4976 break;
4977 case CTRL('u'):
4978 case 'u':
4979 nscroll /= 2;
4980 /* FALL THROUGH */
4981 case KEY_PPAGE:
4982 case CTRL('b'):
4983 if (s->first_displayed_line == 1) {
4984 s->selected_line = MAX(1, s->selected_line - nscroll);
4985 break;
4987 if (s->first_displayed_line > nscroll)
4988 s->first_displayed_line -= nscroll;
4989 else
4990 s->first_displayed_line = 1;
4991 break;
4992 case 'j':
4993 case KEY_DOWN:
4994 case CTRL('n'):
4995 if (s->selected_line < view->nlines - 2 &&
4996 s->first_displayed_line +
4997 s->selected_line <= s->blame.nlines)
4998 s->selected_line++;
4999 else if (s->last_displayed_line <
5000 s->blame.nlines)
5001 s->first_displayed_line++;
5002 break;
5003 case 'b':
5004 case 'p': {
5005 struct got_object_id *id = NULL;
5006 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5007 s->first_displayed_line, s->selected_line);
5008 if (id == NULL)
5009 break;
5010 if (ch == 'p') {
5011 struct got_commit_object *commit, *pcommit;
5012 struct got_object_qid *pid;
5013 struct got_object_id *blob_id = NULL;
5014 int obj_type;
5015 err = got_object_open_as_commit(&commit,
5016 s->repo, id);
5017 if (err)
5018 break;
5019 pid = STAILQ_FIRST(
5020 got_object_commit_get_parent_ids(commit));
5021 if (pid == NULL) {
5022 got_object_commit_close(commit);
5023 break;
5025 /* Check if path history ends here. */
5026 err = got_object_open_as_commit(&pcommit,
5027 s->repo, &pid->id);
5028 if (err)
5029 break;
5030 err = got_object_id_by_path(&blob_id, s->repo,
5031 pcommit, s->path);
5032 got_object_commit_close(pcommit);
5033 if (err) {
5034 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5035 err = NULL;
5036 got_object_commit_close(commit);
5037 break;
5039 err = got_object_get_type(&obj_type, s->repo,
5040 blob_id);
5041 free(blob_id);
5042 /* Can't blame non-blob type objects. */
5043 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5044 got_object_commit_close(commit);
5045 break;
5047 err = got_object_qid_alloc(&s->blamed_commit,
5048 &pid->id);
5049 got_object_commit_close(commit);
5050 } else {
5051 if (got_object_id_cmp(id,
5052 &s->blamed_commit->id) == 0)
5053 break;
5054 err = got_object_qid_alloc(&s->blamed_commit,
5055 id);
5057 if (err)
5058 break;
5059 s->done = 1;
5060 thread_err = stop_blame(&s->blame);
5061 s->done = 0;
5062 if (thread_err)
5063 break;
5064 STAILQ_INSERT_HEAD(&s->blamed_commits,
5065 s->blamed_commit, entry);
5066 err = run_blame(view);
5067 if (err)
5068 break;
5069 break;
5071 case 'B': {
5072 struct got_object_qid *first;
5073 first = STAILQ_FIRST(&s->blamed_commits);
5074 if (!got_object_id_cmp(&first->id, s->commit_id))
5075 break;
5076 s->done = 1;
5077 thread_err = stop_blame(&s->blame);
5078 s->done = 0;
5079 if (thread_err)
5080 break;
5081 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5082 got_object_qid_free(s->blamed_commit);
5083 s->blamed_commit =
5084 STAILQ_FIRST(&s->blamed_commits);
5085 err = run_blame(view);
5086 if (err)
5087 break;
5088 break;
5090 case KEY_ENTER:
5091 case '\r': {
5092 struct got_object_id *id = NULL;
5093 struct got_object_qid *pid;
5094 struct got_commit_object *commit = NULL;
5095 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5096 s->first_displayed_line, s->selected_line);
5097 if (id == NULL)
5098 break;
5099 err = got_object_open_as_commit(&commit, s->repo, id);
5100 if (err)
5101 break;
5102 pid = STAILQ_FIRST(
5103 got_object_commit_get_parent_ids(commit));
5104 if (view_is_parent_view(view))
5105 begin_x = view_split_begin_x(view->begin_x);
5106 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5107 if (diff_view == NULL) {
5108 got_object_commit_close(commit);
5109 err = got_error_from_errno("view_open");
5110 break;
5112 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5113 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5114 got_object_commit_close(commit);
5115 if (err) {
5116 view_close(diff_view);
5117 break;
5119 view->focussed = 0;
5120 diff_view->focussed = 1;
5121 if (view_is_parent_view(view)) {
5122 err = view_close_child(view);
5123 if (err)
5124 break;
5125 err = view_set_child(view, diff_view);
5126 if (err)
5127 break;
5128 view->focus_child = 1;
5129 } else
5130 *new_view = diff_view;
5131 if (err)
5132 break;
5133 break;
5135 case CTRL('d'):
5136 case 'd':
5137 nscroll /= 2;
5138 /* FALL THROUGH */
5139 case KEY_NPAGE:
5140 case CTRL('f'):
5141 case ' ':
5142 if (s->last_displayed_line >= s->blame.nlines &&
5143 s->selected_line >= MIN(s->blame.nlines,
5144 view->nlines - 2)) {
5145 break;
5147 if (s->last_displayed_line >= s->blame.nlines &&
5148 s->selected_line < view->nlines - 2) {
5149 s->selected_line +=
5150 MIN(nscroll, s->last_displayed_line -
5151 s->first_displayed_line - s->selected_line + 1);
5153 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5154 s->first_displayed_line += nscroll;
5155 else
5156 s->first_displayed_line =
5157 s->blame.nlines - (view->nlines - 3);
5158 break;
5159 case KEY_RESIZE:
5160 if (s->selected_line > view->nlines - 2) {
5161 s->selected_line = MIN(s->blame.nlines,
5162 view->nlines - 2);
5164 break;
5165 default:
5166 break;
5168 return thread_err ? thread_err : err;
5171 static const struct got_error *
5172 cmd_blame(int argc, char *argv[])
5174 const struct got_error *error;
5175 struct got_repository *repo = NULL;
5176 struct got_worktree *worktree = NULL;
5177 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5178 char *link_target = NULL;
5179 struct got_object_id *commit_id = NULL;
5180 struct got_commit_object *commit = NULL;
5181 char *commit_id_str = NULL;
5182 int ch;
5183 struct tog_view *view;
5184 int *pack_fds = NULL;
5186 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5187 switch (ch) {
5188 case 'c':
5189 commit_id_str = optarg;
5190 break;
5191 case 'r':
5192 repo_path = realpath(optarg, NULL);
5193 if (repo_path == NULL)
5194 return got_error_from_errno2("realpath",
5195 optarg);
5196 break;
5197 default:
5198 usage_blame();
5199 /* NOTREACHED */
5203 argc -= optind;
5204 argv += optind;
5206 if (argc != 1)
5207 usage_blame();
5209 error = got_repo_pack_fds_open(&pack_fds);
5210 if (error != NULL)
5211 goto done;
5213 if (repo_path == NULL) {
5214 cwd = getcwd(NULL, 0);
5215 if (cwd == NULL)
5216 return got_error_from_errno("getcwd");
5217 error = got_worktree_open(&worktree, cwd);
5218 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5219 goto done;
5220 if (worktree)
5221 repo_path =
5222 strdup(got_worktree_get_repo_path(worktree));
5223 else
5224 repo_path = strdup(cwd);
5225 if (repo_path == NULL) {
5226 error = got_error_from_errno("strdup");
5227 goto done;
5231 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5232 if (error != NULL)
5233 goto done;
5235 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5236 worktree);
5237 if (error)
5238 goto done;
5240 init_curses();
5242 error = apply_unveil(got_repo_get_path(repo), NULL);
5243 if (error)
5244 goto done;
5246 error = tog_load_refs(repo, 0);
5247 if (error)
5248 goto done;
5250 if (commit_id_str == NULL) {
5251 struct got_reference *head_ref;
5252 error = got_ref_open(&head_ref, repo, worktree ?
5253 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5254 if (error != NULL)
5255 goto done;
5256 error = got_ref_resolve(&commit_id, repo, head_ref);
5257 got_ref_close(head_ref);
5258 } else {
5259 error = got_repo_match_object_id(&commit_id, NULL,
5260 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5262 if (error != NULL)
5263 goto done;
5265 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5266 if (view == NULL) {
5267 error = got_error_from_errno("view_open");
5268 goto done;
5271 error = got_object_open_as_commit(&commit, repo, commit_id);
5272 if (error)
5273 goto done;
5275 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5276 commit, repo);
5277 if (error)
5278 goto done;
5280 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5281 commit_id, repo);
5282 if (error)
5283 goto done;
5284 if (worktree) {
5285 /* Release work tree lock. */
5286 got_worktree_close(worktree);
5287 worktree = NULL;
5289 error = view_loop(view);
5290 done:
5291 free(repo_path);
5292 free(in_repo_path);
5293 free(link_target);
5294 free(cwd);
5295 free(commit_id);
5296 if (commit)
5297 got_object_commit_close(commit);
5298 if (worktree)
5299 got_worktree_close(worktree);
5300 if (repo) {
5301 const struct got_error *close_err = got_repo_close(repo);
5302 if (error == NULL)
5303 error = close_err;
5305 if (pack_fds) {
5306 const struct got_error *pack_err =
5307 got_repo_pack_fds_close(pack_fds);
5308 if (error == NULL)
5309 error = pack_err;
5311 tog_free_refs();
5312 return error;
5315 static const struct got_error *
5316 draw_tree_entries(struct tog_view *view, const char *parent_path)
5318 struct tog_tree_view_state *s = &view->state.tree;
5319 const struct got_error *err = NULL;
5320 struct got_tree_entry *te;
5321 wchar_t *wline;
5322 struct tog_color *tc;
5323 int width, n, i, nentries;
5324 int limit = view->nlines;
5326 s->ndisplayed = 0;
5328 werase(view->window);
5330 if (limit == 0)
5331 return NULL;
5333 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5334 0, 0);
5335 if (err)
5336 return err;
5337 if (view_needs_focus_indication(view))
5338 wstandout(view->window);
5339 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5340 if (tc)
5341 wattr_on(view->window,
5342 COLOR_PAIR(tc->colorpair), NULL);
5343 waddwstr(view->window, wline);
5344 if (tc)
5345 wattr_off(view->window,
5346 COLOR_PAIR(tc->colorpair), NULL);
5347 if (view_needs_focus_indication(view))
5348 wstandend(view->window);
5349 free(wline);
5350 wline = NULL;
5351 if (width < view->ncols - 1)
5352 waddch(view->window, '\n');
5353 if (--limit <= 0)
5354 return NULL;
5355 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5356 0, 0);
5357 if (err)
5358 return err;
5359 waddwstr(view->window, wline);
5360 free(wline);
5361 wline = NULL;
5362 if (width < view->ncols - 1)
5363 waddch(view->window, '\n');
5364 if (--limit <= 0)
5365 return NULL;
5366 waddch(view->window, '\n');
5367 if (--limit <= 0)
5368 return NULL;
5370 if (s->first_displayed_entry == NULL) {
5371 te = got_object_tree_get_first_entry(s->tree);
5372 if (s->selected == 0) {
5373 if (view->focussed)
5374 wstandout(view->window);
5375 s->selected_entry = NULL;
5377 waddstr(view->window, " ..\n"); /* parent directory */
5378 if (s->selected == 0 && view->focussed)
5379 wstandend(view->window);
5380 s->ndisplayed++;
5381 if (--limit <= 0)
5382 return NULL;
5383 n = 1;
5384 } else {
5385 n = 0;
5386 te = s->first_displayed_entry;
5389 nentries = got_object_tree_get_nentries(s->tree);
5390 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5391 char *line = NULL, *id_str = NULL, *link_target = NULL;
5392 const char *modestr = "";
5393 mode_t mode;
5395 te = got_object_tree_get_entry(s->tree, i);
5396 mode = got_tree_entry_get_mode(te);
5398 if (s->show_ids) {
5399 err = got_object_id_str(&id_str,
5400 got_tree_entry_get_id(te));
5401 if (err)
5402 return got_error_from_errno(
5403 "got_object_id_str");
5405 if (got_object_tree_entry_is_submodule(te))
5406 modestr = "$";
5407 else if (S_ISLNK(mode)) {
5408 int i;
5410 err = got_tree_entry_get_symlink_target(&link_target,
5411 te, s->repo);
5412 if (err) {
5413 free(id_str);
5414 return err;
5416 for (i = 0; i < strlen(link_target); i++) {
5417 if (!isprint((unsigned char)link_target[i]))
5418 link_target[i] = '?';
5420 modestr = "@";
5422 else if (S_ISDIR(mode))
5423 modestr = "/";
5424 else if (mode & S_IXUSR)
5425 modestr = "*";
5426 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5427 got_tree_entry_get_name(te), modestr,
5428 link_target ? " -> ": "",
5429 link_target ? link_target : "") == -1) {
5430 free(id_str);
5431 free(link_target);
5432 return got_error_from_errno("asprintf");
5434 free(id_str);
5435 free(link_target);
5436 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5437 0, 0);
5438 if (err) {
5439 free(line);
5440 break;
5442 if (n == s->selected) {
5443 if (view->focussed)
5444 wstandout(view->window);
5445 s->selected_entry = te;
5447 tc = match_color(&s->colors, line);
5448 if (tc)
5449 wattr_on(view->window,
5450 COLOR_PAIR(tc->colorpair), NULL);
5451 waddwstr(view->window, wline);
5452 if (tc)
5453 wattr_off(view->window,
5454 COLOR_PAIR(tc->colorpair), NULL);
5455 if (width < view->ncols - 1)
5456 waddch(view->window, '\n');
5457 if (n == s->selected && view->focussed)
5458 wstandend(view->window);
5459 free(line);
5460 free(wline);
5461 wline = NULL;
5462 n++;
5463 s->ndisplayed++;
5464 s->last_displayed_entry = te;
5465 if (--limit <= 0)
5466 break;
5469 return err;
5472 static void
5473 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5475 struct got_tree_entry *te;
5476 int isroot = s->tree == s->root;
5477 int i = 0;
5479 if (s->first_displayed_entry == NULL)
5480 return;
5482 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5483 while (i++ < maxscroll) {
5484 if (te == NULL) {
5485 if (!isroot)
5486 s->first_displayed_entry = NULL;
5487 break;
5489 s->first_displayed_entry = te;
5490 te = got_tree_entry_get_prev(s->tree, te);
5494 static void
5495 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5497 struct got_tree_entry *next, *last;
5498 int n = 0;
5500 if (s->first_displayed_entry)
5501 next = got_tree_entry_get_next(s->tree,
5502 s->first_displayed_entry);
5503 else
5504 next = got_object_tree_get_first_entry(s->tree);
5506 last = s->last_displayed_entry;
5507 while (next && last && n++ < maxscroll) {
5508 last = got_tree_entry_get_next(s->tree, last);
5509 if (last) {
5510 s->first_displayed_entry = next;
5511 next = got_tree_entry_get_next(s->tree, next);
5516 static const struct got_error *
5517 tree_entry_path(char **path, struct tog_parent_trees *parents,
5518 struct got_tree_entry *te)
5520 const struct got_error *err = NULL;
5521 struct tog_parent_tree *pt;
5522 size_t len = 2; /* for leading slash and NUL */
5524 TAILQ_FOREACH(pt, parents, entry)
5525 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5526 + 1 /* slash */;
5527 if (te)
5528 len += strlen(got_tree_entry_get_name(te));
5530 *path = calloc(1, len);
5531 if (path == NULL)
5532 return got_error_from_errno("calloc");
5534 (*path)[0] = '/';
5535 pt = TAILQ_LAST(parents, tog_parent_trees);
5536 while (pt) {
5537 const char *name = got_tree_entry_get_name(pt->selected_entry);
5538 if (strlcat(*path, name, len) >= len) {
5539 err = got_error(GOT_ERR_NO_SPACE);
5540 goto done;
5542 if (strlcat(*path, "/", len) >= len) {
5543 err = got_error(GOT_ERR_NO_SPACE);
5544 goto done;
5546 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5548 if (te) {
5549 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5550 err = got_error(GOT_ERR_NO_SPACE);
5551 goto done;
5554 done:
5555 if (err) {
5556 free(*path);
5557 *path = NULL;
5559 return err;
5562 static const struct got_error *
5563 blame_tree_entry(struct tog_view **new_view, int begin_x,
5564 struct got_tree_entry *te, struct tog_parent_trees *parents,
5565 struct got_object_id *commit_id, struct got_repository *repo)
5567 const struct got_error *err = NULL;
5568 char *path;
5569 struct tog_view *blame_view;
5571 *new_view = NULL;
5573 err = tree_entry_path(&path, parents, te);
5574 if (err)
5575 return err;
5577 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5578 if (blame_view == NULL) {
5579 err = got_error_from_errno("view_open");
5580 goto done;
5583 err = open_blame_view(blame_view, path, commit_id, repo);
5584 if (err) {
5585 if (err->code == GOT_ERR_CANCELLED)
5586 err = NULL;
5587 view_close(blame_view);
5588 } else
5589 *new_view = blame_view;
5590 done:
5591 free(path);
5592 return err;
5595 static const struct got_error *
5596 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5597 struct tog_tree_view_state *s)
5599 struct tog_view *log_view;
5600 const struct got_error *err = NULL;
5601 char *path;
5603 *new_view = NULL;
5605 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5606 if (log_view == NULL)
5607 return got_error_from_errno("view_open");
5609 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5610 if (err)
5611 return err;
5613 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5614 path, 0);
5615 if (err)
5616 view_close(log_view);
5617 else
5618 *new_view = log_view;
5619 free(path);
5620 return err;
5623 static const struct got_error *
5624 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5625 const char *head_ref_name, struct got_repository *repo)
5627 const struct got_error *err = NULL;
5628 char *commit_id_str = NULL;
5629 struct tog_tree_view_state *s = &view->state.tree;
5630 struct got_commit_object *commit = NULL;
5632 TAILQ_INIT(&s->parents);
5633 STAILQ_INIT(&s->colors);
5635 s->commit_id = got_object_id_dup(commit_id);
5636 if (s->commit_id == NULL)
5637 return got_error_from_errno("got_object_id_dup");
5639 err = got_object_open_as_commit(&commit, repo, commit_id);
5640 if (err)
5641 goto done;
5644 * The root is opened here and will be closed when the view is closed.
5645 * Any visited subtrees and their path-wise parents are opened and
5646 * closed on demand.
5648 err = got_object_open_as_tree(&s->root, repo,
5649 got_object_commit_get_tree_id(commit));
5650 if (err)
5651 goto done;
5652 s->tree = s->root;
5654 err = got_object_id_str(&commit_id_str, commit_id);
5655 if (err != NULL)
5656 goto done;
5658 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5659 err = got_error_from_errno("asprintf");
5660 goto done;
5663 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5664 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5665 if (head_ref_name) {
5666 s->head_ref_name = strdup(head_ref_name);
5667 if (s->head_ref_name == NULL) {
5668 err = got_error_from_errno("strdup");
5669 goto done;
5672 s->repo = repo;
5674 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5675 err = add_color(&s->colors, "\\$$",
5676 TOG_COLOR_TREE_SUBMODULE,
5677 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5678 if (err)
5679 goto done;
5680 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5681 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5682 if (err)
5683 goto done;
5684 err = add_color(&s->colors, "/$",
5685 TOG_COLOR_TREE_DIRECTORY,
5686 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5687 if (err)
5688 goto done;
5690 err = add_color(&s->colors, "\\*$",
5691 TOG_COLOR_TREE_EXECUTABLE,
5692 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5693 if (err)
5694 goto done;
5696 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5697 get_color_value("TOG_COLOR_COMMIT"));
5698 if (err)
5699 goto done;
5702 view->show = show_tree_view;
5703 view->input = input_tree_view;
5704 view->close = close_tree_view;
5705 view->search_start = search_start_tree_view;
5706 view->search_next = search_next_tree_view;
5707 done:
5708 free(commit_id_str);
5709 if (commit)
5710 got_object_commit_close(commit);
5711 if (err)
5712 close_tree_view(view);
5713 return err;
5716 static const struct got_error *
5717 close_tree_view(struct tog_view *view)
5719 struct tog_tree_view_state *s = &view->state.tree;
5721 free_colors(&s->colors);
5722 free(s->tree_label);
5723 s->tree_label = NULL;
5724 free(s->commit_id);
5725 s->commit_id = NULL;
5726 free(s->head_ref_name);
5727 s->head_ref_name = NULL;
5728 while (!TAILQ_EMPTY(&s->parents)) {
5729 struct tog_parent_tree *parent;
5730 parent = TAILQ_FIRST(&s->parents);
5731 TAILQ_REMOVE(&s->parents, parent, entry);
5732 if (parent->tree != s->root)
5733 got_object_tree_close(parent->tree);
5734 free(parent);
5737 if (s->tree != NULL && s->tree != s->root)
5738 got_object_tree_close(s->tree);
5739 if (s->root)
5740 got_object_tree_close(s->root);
5741 return NULL;
5744 static const struct got_error *
5745 search_start_tree_view(struct tog_view *view)
5747 struct tog_tree_view_state *s = &view->state.tree;
5749 s->matched_entry = NULL;
5750 return NULL;
5753 static int
5754 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5756 regmatch_t regmatch;
5758 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5759 0) == 0;
5762 static const struct got_error *
5763 search_next_tree_view(struct tog_view *view)
5765 struct tog_tree_view_state *s = &view->state.tree;
5766 struct got_tree_entry *te = NULL;
5768 if (!view->searching) {
5769 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5770 return NULL;
5773 if (s->matched_entry) {
5774 if (view->searching == TOG_SEARCH_FORWARD) {
5775 if (s->selected_entry)
5776 te = got_tree_entry_get_next(s->tree,
5777 s->selected_entry);
5778 else
5779 te = got_object_tree_get_first_entry(s->tree);
5780 } else {
5781 if (s->selected_entry == NULL)
5782 te = got_object_tree_get_last_entry(s->tree);
5783 else
5784 te = got_tree_entry_get_prev(s->tree,
5785 s->selected_entry);
5787 } else {
5788 if (s->selected_entry)
5789 te = s->selected_entry;
5790 else if (view->searching == TOG_SEARCH_FORWARD)
5791 te = got_object_tree_get_first_entry(s->tree);
5792 else
5793 te = got_object_tree_get_last_entry(s->tree);
5796 while (1) {
5797 if (te == NULL) {
5798 if (s->matched_entry == NULL) {
5799 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5800 return NULL;
5802 if (view->searching == TOG_SEARCH_FORWARD)
5803 te = got_object_tree_get_first_entry(s->tree);
5804 else
5805 te = got_object_tree_get_last_entry(s->tree);
5808 if (match_tree_entry(te, &view->regex)) {
5809 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5810 s->matched_entry = te;
5811 break;
5814 if (view->searching == TOG_SEARCH_FORWARD)
5815 te = got_tree_entry_get_next(s->tree, te);
5816 else
5817 te = got_tree_entry_get_prev(s->tree, te);
5820 if (s->matched_entry) {
5821 s->first_displayed_entry = s->matched_entry;
5822 s->selected = 0;
5825 return NULL;
5828 static const struct got_error *
5829 show_tree_view(struct tog_view *view)
5831 const struct got_error *err = NULL;
5832 struct tog_tree_view_state *s = &view->state.tree;
5833 char *parent_path;
5835 err = tree_entry_path(&parent_path, &s->parents, NULL);
5836 if (err)
5837 return err;
5839 err = draw_tree_entries(view, parent_path);
5840 free(parent_path);
5842 view_vborder(view);
5843 return err;
5846 static const struct got_error *
5847 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5849 const struct got_error *err = NULL;
5850 struct tog_tree_view_state *s = &view->state.tree;
5851 struct tog_view *log_view, *ref_view;
5852 struct got_tree_entry *te;
5853 int begin_x = 0, n, nscroll = view->nlines - 3;
5855 switch (ch) {
5856 case 'i':
5857 s->show_ids = !s->show_ids;
5858 break;
5859 case 'l':
5860 if (!s->selected_entry)
5861 break;
5862 if (view_is_parent_view(view))
5863 begin_x = view_split_begin_x(view->begin_x);
5864 err = log_selected_tree_entry(&log_view, begin_x, s);
5865 view->focussed = 0;
5866 log_view->focussed = 1;
5867 if (view_is_parent_view(view)) {
5868 err = view_close_child(view);
5869 if (err)
5870 return err;
5871 err = view_set_child(view, log_view);
5872 if (err)
5873 return err;
5874 view->focus_child = 1;
5875 } else
5876 *new_view = log_view;
5877 break;
5878 case 'r':
5879 if (view_is_parent_view(view))
5880 begin_x = view_split_begin_x(view->begin_x);
5881 ref_view = view_open(view->nlines, view->ncols,
5882 view->begin_y, begin_x, TOG_VIEW_REF);
5883 if (ref_view == NULL)
5884 return got_error_from_errno("view_open");
5885 err = open_ref_view(ref_view, s->repo);
5886 if (err) {
5887 view_close(ref_view);
5888 return err;
5890 view->focussed = 0;
5891 ref_view->focussed = 1;
5892 if (view_is_parent_view(view)) {
5893 err = view_close_child(view);
5894 if (err)
5895 return err;
5896 err = view_set_child(view, ref_view);
5897 if (err)
5898 return err;
5899 view->focus_child = 1;
5900 } else
5901 *new_view = ref_view;
5902 break;
5903 case 'g':
5904 case KEY_HOME:
5905 s->selected = 0;
5906 if (s->tree == s->root)
5907 s->first_displayed_entry =
5908 got_object_tree_get_first_entry(s->tree);
5909 else
5910 s->first_displayed_entry = NULL;
5911 break;
5912 case 'G':
5913 case KEY_END:
5914 s->selected = 0;
5915 te = got_object_tree_get_last_entry(s->tree);
5916 for (n = 0; n < view->nlines - 3; n++) {
5917 if (te == NULL) {
5918 if(s->tree != s->root) {
5919 s->first_displayed_entry = NULL;
5920 n++;
5922 break;
5924 s->first_displayed_entry = te;
5925 te = got_tree_entry_get_prev(s->tree, te);
5927 if (n > 0)
5928 s->selected = n - 1;
5929 break;
5930 case 'k':
5931 case KEY_UP:
5932 case CTRL('p'):
5933 if (s->selected > 0) {
5934 s->selected--;
5935 break;
5937 tree_scroll_up(s, 1);
5938 break;
5939 case CTRL('u'):
5940 case 'u':
5941 nscroll /= 2;
5942 /* FALL THROUGH */
5943 case KEY_PPAGE:
5944 case CTRL('b'):
5945 if (s->tree == s->root) {
5946 if (got_object_tree_get_first_entry(s->tree) ==
5947 s->first_displayed_entry)
5948 s->selected -= MIN(s->selected, nscroll);
5949 } else {
5950 if (s->first_displayed_entry == NULL)
5951 s->selected -= MIN(s->selected, nscroll);
5953 tree_scroll_up(s, MAX(0, nscroll));
5954 break;
5955 case 'j':
5956 case KEY_DOWN:
5957 case CTRL('n'):
5958 if (s->selected < s->ndisplayed - 1) {
5959 s->selected++;
5960 break;
5962 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5963 == NULL)
5964 /* can't scroll any further */
5965 break;
5966 tree_scroll_down(s, 1);
5967 break;
5968 case CTRL('d'):
5969 case 'd':
5970 nscroll /= 2;
5971 /* FALL THROUGH */
5972 case KEY_NPAGE:
5973 case CTRL('f'):
5974 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5975 == NULL) {
5976 /* can't scroll any further; move cursor down */
5977 if (s->selected < s->ndisplayed - 1)
5978 s->selected += MIN(nscroll,
5979 s->ndisplayed - s->selected - 1);
5980 break;
5982 tree_scroll_down(s, nscroll);
5983 break;
5984 case KEY_ENTER:
5985 case '\r':
5986 case KEY_BACKSPACE:
5987 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5988 struct tog_parent_tree *parent;
5989 /* user selected '..' */
5990 if (s->tree == s->root)
5991 break;
5992 parent = TAILQ_FIRST(&s->parents);
5993 TAILQ_REMOVE(&s->parents, parent,
5994 entry);
5995 got_object_tree_close(s->tree);
5996 s->tree = parent->tree;
5997 s->first_displayed_entry =
5998 parent->first_displayed_entry;
5999 s->selected_entry =
6000 parent->selected_entry;
6001 s->selected = parent->selected;
6002 free(parent);
6003 } else if (S_ISDIR(got_tree_entry_get_mode(
6004 s->selected_entry))) {
6005 struct got_tree_object *subtree;
6006 err = got_object_open_as_tree(&subtree, s->repo,
6007 got_tree_entry_get_id(s->selected_entry));
6008 if (err)
6009 break;
6010 err = tree_view_visit_subtree(s, subtree);
6011 if (err) {
6012 got_object_tree_close(subtree);
6013 break;
6015 } else if (S_ISREG(got_tree_entry_get_mode(
6016 s->selected_entry))) {
6017 struct tog_view *blame_view;
6018 int begin_x = view_is_parent_view(view) ?
6019 view_split_begin_x(view->begin_x) : 0;
6021 err = blame_tree_entry(&blame_view, begin_x,
6022 s->selected_entry, &s->parents,
6023 s->commit_id, s->repo);
6024 if (err)
6025 break;
6026 view->focussed = 0;
6027 blame_view->focussed = 1;
6028 if (view_is_parent_view(view)) {
6029 err = view_close_child(view);
6030 if (err)
6031 return err;
6032 err = view_set_child(view, blame_view);
6033 if (err)
6034 return err;
6035 view->focus_child = 1;
6036 } else
6037 *new_view = blame_view;
6039 break;
6040 case KEY_RESIZE:
6041 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6042 s->selected = view->nlines - 4;
6043 break;
6044 default:
6045 break;
6048 return err;
6051 __dead static void
6052 usage_tree(void)
6054 endwin();
6055 fprintf(stderr,
6056 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6057 getprogname());
6058 exit(1);
6061 static const struct got_error *
6062 cmd_tree(int argc, char *argv[])
6064 const struct got_error *error;
6065 struct got_repository *repo = NULL;
6066 struct got_worktree *worktree = NULL;
6067 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6068 struct got_object_id *commit_id = NULL;
6069 struct got_commit_object *commit = NULL;
6070 const char *commit_id_arg = NULL;
6071 char *label = NULL;
6072 struct got_reference *ref = NULL;
6073 const char *head_ref_name = NULL;
6074 int ch;
6075 struct tog_view *view;
6076 int *pack_fds = NULL;
6078 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6079 switch (ch) {
6080 case 'c':
6081 commit_id_arg = optarg;
6082 break;
6083 case 'r':
6084 repo_path = realpath(optarg, NULL);
6085 if (repo_path == NULL)
6086 return got_error_from_errno2("realpath",
6087 optarg);
6088 break;
6089 default:
6090 usage_tree();
6091 /* NOTREACHED */
6095 argc -= optind;
6096 argv += optind;
6098 if (argc > 1)
6099 usage_tree();
6101 error = got_repo_pack_fds_open(&pack_fds);
6102 if (error != NULL)
6103 goto done;
6105 if (repo_path == NULL) {
6106 cwd = getcwd(NULL, 0);
6107 if (cwd == NULL)
6108 return got_error_from_errno("getcwd");
6109 error = got_worktree_open(&worktree, cwd);
6110 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6111 goto done;
6112 if (worktree)
6113 repo_path =
6114 strdup(got_worktree_get_repo_path(worktree));
6115 else
6116 repo_path = strdup(cwd);
6117 if (repo_path == NULL) {
6118 error = got_error_from_errno("strdup");
6119 goto done;
6123 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6124 if (error != NULL)
6125 goto done;
6127 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6128 repo, worktree);
6129 if (error)
6130 goto done;
6132 init_curses();
6134 error = apply_unveil(got_repo_get_path(repo), NULL);
6135 if (error)
6136 goto done;
6138 error = tog_load_refs(repo, 0);
6139 if (error)
6140 goto done;
6142 if (commit_id_arg == NULL) {
6143 error = got_repo_match_object_id(&commit_id, &label,
6144 worktree ? got_worktree_get_head_ref_name(worktree) :
6145 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6146 if (error)
6147 goto done;
6148 head_ref_name = label;
6149 } else {
6150 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6151 if (error == NULL)
6152 head_ref_name = got_ref_get_name(ref);
6153 else if (error->code != GOT_ERR_NOT_REF)
6154 goto done;
6155 error = got_repo_match_object_id(&commit_id, NULL,
6156 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6157 if (error)
6158 goto done;
6161 error = got_object_open_as_commit(&commit, repo, commit_id);
6162 if (error)
6163 goto done;
6165 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6166 if (view == NULL) {
6167 error = got_error_from_errno("view_open");
6168 goto done;
6170 error = open_tree_view(view, commit_id, head_ref_name, repo);
6171 if (error)
6172 goto done;
6173 if (!got_path_is_root_dir(in_repo_path)) {
6174 error = tree_view_walk_path(&view->state.tree, commit,
6175 in_repo_path);
6176 if (error)
6177 goto done;
6180 if (worktree) {
6181 /* Release work tree lock. */
6182 got_worktree_close(worktree);
6183 worktree = NULL;
6185 error = view_loop(view);
6186 done:
6187 free(repo_path);
6188 free(cwd);
6189 free(commit_id);
6190 free(label);
6191 if (ref)
6192 got_ref_close(ref);
6193 if (repo) {
6194 const struct got_error *close_err = got_repo_close(repo);
6195 if (error == NULL)
6196 error = close_err;
6198 if (pack_fds) {
6199 const struct got_error *pack_err =
6200 got_repo_pack_fds_close(pack_fds);
6201 if (error == NULL)
6202 error = pack_err;
6204 tog_free_refs();
6205 return error;
6208 static const struct got_error *
6209 ref_view_load_refs(struct tog_ref_view_state *s)
6211 struct got_reflist_entry *sre;
6212 struct tog_reflist_entry *re;
6214 s->nrefs = 0;
6215 TAILQ_FOREACH(sre, &tog_refs, entry) {
6216 if (strncmp(got_ref_get_name(sre->ref),
6217 "refs/got/", 9) == 0 &&
6218 strncmp(got_ref_get_name(sre->ref),
6219 "refs/got/backup/", 16) != 0)
6220 continue;
6222 re = malloc(sizeof(*re));
6223 if (re == NULL)
6224 return got_error_from_errno("malloc");
6226 re->ref = got_ref_dup(sre->ref);
6227 if (re->ref == NULL)
6228 return got_error_from_errno("got_ref_dup");
6229 re->idx = s->nrefs++;
6230 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6233 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6234 return NULL;
6237 void
6238 ref_view_free_refs(struct tog_ref_view_state *s)
6240 struct tog_reflist_entry *re;
6242 while (!TAILQ_EMPTY(&s->refs)) {
6243 re = TAILQ_FIRST(&s->refs);
6244 TAILQ_REMOVE(&s->refs, re, entry);
6245 got_ref_close(re->ref);
6246 free(re);
6250 static const struct got_error *
6251 open_ref_view(struct tog_view *view, struct got_repository *repo)
6253 const struct got_error *err = NULL;
6254 struct tog_ref_view_state *s = &view->state.ref;
6256 s->selected_entry = 0;
6257 s->repo = repo;
6259 TAILQ_INIT(&s->refs);
6260 STAILQ_INIT(&s->colors);
6262 err = ref_view_load_refs(s);
6263 if (err)
6264 return err;
6266 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6267 err = add_color(&s->colors, "^refs/heads/",
6268 TOG_COLOR_REFS_HEADS,
6269 get_color_value("TOG_COLOR_REFS_HEADS"));
6270 if (err)
6271 goto done;
6273 err = add_color(&s->colors, "^refs/tags/",
6274 TOG_COLOR_REFS_TAGS,
6275 get_color_value("TOG_COLOR_REFS_TAGS"));
6276 if (err)
6277 goto done;
6279 err = add_color(&s->colors, "^refs/remotes/",
6280 TOG_COLOR_REFS_REMOTES,
6281 get_color_value("TOG_COLOR_REFS_REMOTES"));
6282 if (err)
6283 goto done;
6285 err = add_color(&s->colors, "^refs/got/backup/",
6286 TOG_COLOR_REFS_BACKUP,
6287 get_color_value("TOG_COLOR_REFS_BACKUP"));
6288 if (err)
6289 goto done;
6292 view->show = show_ref_view;
6293 view->input = input_ref_view;
6294 view->close = close_ref_view;
6295 view->search_start = search_start_ref_view;
6296 view->search_next = search_next_ref_view;
6297 done:
6298 if (err)
6299 free_colors(&s->colors);
6300 return err;
6303 static const struct got_error *
6304 close_ref_view(struct tog_view *view)
6306 struct tog_ref_view_state *s = &view->state.ref;
6308 ref_view_free_refs(s);
6309 free_colors(&s->colors);
6311 return NULL;
6314 static const struct got_error *
6315 resolve_reflist_entry(struct got_object_id **commit_id,
6316 struct tog_reflist_entry *re, struct got_repository *repo)
6318 const struct got_error *err = NULL;
6319 struct got_object_id *obj_id;
6320 struct got_tag_object *tag = NULL;
6321 int obj_type;
6323 *commit_id = NULL;
6325 err = got_ref_resolve(&obj_id, repo, re->ref);
6326 if (err)
6327 return err;
6329 err = got_object_get_type(&obj_type, repo, obj_id);
6330 if (err)
6331 goto done;
6333 switch (obj_type) {
6334 case GOT_OBJ_TYPE_COMMIT:
6335 *commit_id = obj_id;
6336 break;
6337 case GOT_OBJ_TYPE_TAG:
6338 err = got_object_open_as_tag(&tag, repo, obj_id);
6339 if (err)
6340 goto done;
6341 free(obj_id);
6342 err = got_object_get_type(&obj_type, repo,
6343 got_object_tag_get_object_id(tag));
6344 if (err)
6345 goto done;
6346 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6347 err = got_error(GOT_ERR_OBJ_TYPE);
6348 goto done;
6350 *commit_id = got_object_id_dup(
6351 got_object_tag_get_object_id(tag));
6352 if (*commit_id == NULL) {
6353 err = got_error_from_errno("got_object_id_dup");
6354 goto done;
6356 break;
6357 default:
6358 err = got_error(GOT_ERR_OBJ_TYPE);
6359 break;
6362 done:
6363 if (tag)
6364 got_object_tag_close(tag);
6365 if (err) {
6366 free(*commit_id);
6367 *commit_id = NULL;
6369 return err;
6372 static const struct got_error *
6373 log_ref_entry(struct tog_view **new_view, int begin_x,
6374 struct tog_reflist_entry *re, struct got_repository *repo)
6376 struct tog_view *log_view;
6377 const struct got_error *err = NULL;
6378 struct got_object_id *commit_id = NULL;
6380 *new_view = NULL;
6382 err = resolve_reflist_entry(&commit_id, re, repo);
6383 if (err) {
6384 if (err->code != GOT_ERR_OBJ_TYPE)
6385 return err;
6386 else
6387 return NULL;
6390 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6391 if (log_view == NULL) {
6392 err = got_error_from_errno("view_open");
6393 goto done;
6396 err = open_log_view(log_view, commit_id, repo,
6397 got_ref_get_name(re->ref), "", 0);
6398 done:
6399 if (err)
6400 view_close(log_view);
6401 else
6402 *new_view = log_view;
6403 free(commit_id);
6404 return err;
6407 static void
6408 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6410 struct tog_reflist_entry *re;
6411 int i = 0;
6413 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6414 return;
6416 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6417 while (i++ < maxscroll) {
6418 if (re == NULL)
6419 break;
6420 s->first_displayed_entry = re;
6421 re = TAILQ_PREV(re, tog_reflist_head, entry);
6425 static void
6426 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6428 struct tog_reflist_entry *next, *last;
6429 int n = 0;
6431 if (s->first_displayed_entry)
6432 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6433 else
6434 next = TAILQ_FIRST(&s->refs);
6436 last = s->last_displayed_entry;
6437 while (next && last && n++ < maxscroll) {
6438 last = TAILQ_NEXT(last, entry);
6439 if (last) {
6440 s->first_displayed_entry = next;
6441 next = TAILQ_NEXT(next, entry);
6446 static const struct got_error *
6447 search_start_ref_view(struct tog_view *view)
6449 struct tog_ref_view_state *s = &view->state.ref;
6451 s->matched_entry = NULL;
6452 return NULL;
6455 static int
6456 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6458 regmatch_t regmatch;
6460 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6461 0) == 0;
6464 static const struct got_error *
6465 search_next_ref_view(struct tog_view *view)
6467 struct tog_ref_view_state *s = &view->state.ref;
6468 struct tog_reflist_entry *re = NULL;
6470 if (!view->searching) {
6471 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6472 return NULL;
6475 if (s->matched_entry) {
6476 if (view->searching == TOG_SEARCH_FORWARD) {
6477 if (s->selected_entry)
6478 re = TAILQ_NEXT(s->selected_entry, entry);
6479 else
6480 re = TAILQ_PREV(s->selected_entry,
6481 tog_reflist_head, entry);
6482 } else {
6483 if (s->selected_entry == NULL)
6484 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6485 else
6486 re = TAILQ_PREV(s->selected_entry,
6487 tog_reflist_head, entry);
6489 } else {
6490 if (s->selected_entry)
6491 re = s->selected_entry;
6492 else if (view->searching == TOG_SEARCH_FORWARD)
6493 re = TAILQ_FIRST(&s->refs);
6494 else
6495 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6498 while (1) {
6499 if (re == NULL) {
6500 if (s->matched_entry == NULL) {
6501 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6502 return NULL;
6504 if (view->searching == TOG_SEARCH_FORWARD)
6505 re = TAILQ_FIRST(&s->refs);
6506 else
6507 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6510 if (match_reflist_entry(re, &view->regex)) {
6511 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6512 s->matched_entry = re;
6513 break;
6516 if (view->searching == TOG_SEARCH_FORWARD)
6517 re = TAILQ_NEXT(re, entry);
6518 else
6519 re = TAILQ_PREV(re, tog_reflist_head, entry);
6522 if (s->matched_entry) {
6523 s->first_displayed_entry = s->matched_entry;
6524 s->selected = 0;
6527 return NULL;
6530 static const struct got_error *
6531 show_ref_view(struct tog_view *view)
6533 const struct got_error *err = NULL;
6534 struct tog_ref_view_state *s = &view->state.ref;
6535 struct tog_reflist_entry *re;
6536 char *line = NULL;
6537 wchar_t *wline;
6538 struct tog_color *tc;
6539 int width, n;
6540 int limit = view->nlines;
6542 werase(view->window);
6544 s->ndisplayed = 0;
6546 if (limit == 0)
6547 return NULL;
6549 re = s->first_displayed_entry;
6551 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6552 s->nrefs) == -1)
6553 return got_error_from_errno("asprintf");
6555 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6556 if (err) {
6557 free(line);
6558 return err;
6560 if (view_needs_focus_indication(view))
6561 wstandout(view->window);
6562 waddwstr(view->window, wline);
6563 if (view_needs_focus_indication(view))
6564 wstandend(view->window);
6565 free(wline);
6566 wline = NULL;
6567 free(line);
6568 line = NULL;
6569 if (width < view->ncols - 1)
6570 waddch(view->window, '\n');
6571 if (--limit <= 0)
6572 return NULL;
6574 n = 0;
6575 while (re && limit > 0) {
6576 char *line = NULL;
6577 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6579 if (s->show_date) {
6580 struct got_commit_object *ci;
6581 struct got_tag_object *tag;
6582 struct got_object_id *id;
6583 struct tm tm;
6584 time_t t;
6586 err = got_ref_resolve(&id, s->repo, re->ref);
6587 if (err)
6588 return err;
6589 err = got_object_open_as_tag(&tag, s->repo, id);
6590 if (err) {
6591 if (err->code != GOT_ERR_OBJ_TYPE) {
6592 free(id);
6593 return err;
6595 err = got_object_open_as_commit(&ci, s->repo,
6596 id);
6597 if (err) {
6598 free(id);
6599 return err;
6601 t = got_object_commit_get_committer_time(ci);
6602 got_object_commit_close(ci);
6603 } else {
6604 t = got_object_tag_get_tagger_time(tag);
6605 got_object_tag_close(tag);
6607 free(id);
6608 if (gmtime_r(&t, &tm) == NULL)
6609 return got_error_from_errno("gmtime_r");
6610 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6611 return got_error(GOT_ERR_NO_SPACE);
6613 if (got_ref_is_symbolic(re->ref)) {
6614 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6615 ymd : "", got_ref_get_name(re->ref),
6616 got_ref_get_symref_target(re->ref)) == -1)
6617 return got_error_from_errno("asprintf");
6618 } else if (s->show_ids) {
6619 struct got_object_id *id;
6620 char *id_str;
6621 err = got_ref_resolve(&id, s->repo, re->ref);
6622 if (err)
6623 return err;
6624 err = got_object_id_str(&id_str, id);
6625 if (err) {
6626 free(id);
6627 return err;
6629 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6630 got_ref_get_name(re->ref), id_str) == -1) {
6631 err = got_error_from_errno("asprintf");
6632 free(id);
6633 free(id_str);
6634 return err;
6636 free(id);
6637 free(id_str);
6638 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6639 got_ref_get_name(re->ref)) == -1)
6640 return got_error_from_errno("asprintf");
6642 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6643 0, 0);
6644 if (err) {
6645 free(line);
6646 return err;
6648 if (n == s->selected) {
6649 if (view->focussed)
6650 wstandout(view->window);
6651 s->selected_entry = re;
6653 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6654 if (tc)
6655 wattr_on(view->window,
6656 COLOR_PAIR(tc->colorpair), NULL);
6657 waddwstr(view->window, wline);
6658 if (tc)
6659 wattr_off(view->window,
6660 COLOR_PAIR(tc->colorpair), NULL);
6661 if (width < view->ncols - 1)
6662 waddch(view->window, '\n');
6663 if (n == s->selected && view->focussed)
6664 wstandend(view->window);
6665 free(line);
6666 free(wline);
6667 wline = NULL;
6668 n++;
6669 s->ndisplayed++;
6670 s->last_displayed_entry = re;
6672 limit--;
6673 re = TAILQ_NEXT(re, entry);
6676 view_vborder(view);
6677 return err;
6680 static const struct got_error *
6681 browse_ref_tree(struct tog_view **new_view, int begin_x,
6682 struct tog_reflist_entry *re, struct got_repository *repo)
6684 const struct got_error *err = NULL;
6685 struct got_object_id *commit_id = NULL;
6686 struct tog_view *tree_view;
6688 *new_view = NULL;
6690 err = resolve_reflist_entry(&commit_id, re, repo);
6691 if (err) {
6692 if (err->code != GOT_ERR_OBJ_TYPE)
6693 return err;
6694 else
6695 return NULL;
6699 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6700 if (tree_view == NULL) {
6701 err = got_error_from_errno("view_open");
6702 goto done;
6705 err = open_tree_view(tree_view, commit_id,
6706 got_ref_get_name(re->ref), repo);
6707 if (err)
6708 goto done;
6710 *new_view = tree_view;
6711 done:
6712 free(commit_id);
6713 return err;
6715 static const struct got_error *
6716 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6718 const struct got_error *err = NULL;
6719 struct tog_ref_view_state *s = &view->state.ref;
6720 struct tog_view *log_view, *tree_view;
6721 struct tog_reflist_entry *re;
6722 int begin_x = 0, n, nscroll = view->nlines - 1;
6724 switch (ch) {
6725 case 'i':
6726 s->show_ids = !s->show_ids;
6727 break;
6728 case 'm':
6729 s->show_date = !s->show_date;
6730 break;
6731 case 'o':
6732 s->sort_by_date = !s->sort_by_date;
6733 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6734 got_ref_cmp_by_commit_timestamp_descending :
6735 tog_ref_cmp_by_name, s->repo);
6736 if (err)
6737 break;
6738 got_reflist_object_id_map_free(tog_refs_idmap);
6739 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6740 &tog_refs, s->repo);
6741 if (err)
6742 break;
6743 ref_view_free_refs(s);
6744 err = ref_view_load_refs(s);
6745 break;
6746 case KEY_ENTER:
6747 case '\r':
6748 if (!s->selected_entry)
6749 break;
6750 if (view_is_parent_view(view))
6751 begin_x = view_split_begin_x(view->begin_x);
6752 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6753 s->repo);
6754 view->focussed = 0;
6755 log_view->focussed = 1;
6756 if (view_is_parent_view(view)) {
6757 err = view_close_child(view);
6758 if (err)
6759 return err;
6760 err = view_set_child(view, log_view);
6761 if (err)
6762 return err;
6763 view->focus_child = 1;
6764 } else
6765 *new_view = log_view;
6766 break;
6767 case 't':
6768 if (!s->selected_entry)
6769 break;
6770 if (view_is_parent_view(view))
6771 begin_x = view_split_begin_x(view->begin_x);
6772 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6773 s->repo);
6774 if (err || tree_view == NULL)
6775 break;
6776 view->focussed = 0;
6777 tree_view->focussed = 1;
6778 if (view_is_parent_view(view)) {
6779 err = view_close_child(view);
6780 if (err)
6781 return err;
6782 err = view_set_child(view, tree_view);
6783 if (err)
6784 return err;
6785 view->focus_child = 1;
6786 } else
6787 *new_view = tree_view;
6788 break;
6789 case 'g':
6790 case KEY_HOME:
6791 s->selected = 0;
6792 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6793 break;
6794 case 'G':
6795 case KEY_END:
6796 s->selected = 0;
6797 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6798 for (n = 0; n < view->nlines - 1; n++) {
6799 if (re == NULL)
6800 break;
6801 s->first_displayed_entry = re;
6802 re = TAILQ_PREV(re, tog_reflist_head, entry);
6804 if (n > 0)
6805 s->selected = n - 1;
6806 break;
6807 case 'k':
6808 case KEY_UP:
6809 case CTRL('p'):
6810 if (s->selected > 0) {
6811 s->selected--;
6812 break;
6814 ref_scroll_up(s, 1);
6815 break;
6816 case CTRL('u'):
6817 case 'u':
6818 nscroll /= 2;
6819 /* FALL THROUGH */
6820 case KEY_PPAGE:
6821 case CTRL('b'):
6822 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6823 s->selected -= MIN(nscroll, s->selected);
6824 ref_scroll_up(s, MAX(0, nscroll));
6825 break;
6826 case 'j':
6827 case KEY_DOWN:
6828 case CTRL('n'):
6829 if (s->selected < s->ndisplayed - 1) {
6830 s->selected++;
6831 break;
6833 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6834 /* can't scroll any further */
6835 break;
6836 ref_scroll_down(s, 1);
6837 break;
6838 case CTRL('d'):
6839 case 'd':
6840 nscroll /= 2;
6841 /* FALL THROUGH */
6842 case KEY_NPAGE:
6843 case CTRL('f'):
6844 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6845 /* can't scroll any further; move cursor down */
6846 if (s->selected < s->ndisplayed - 1)
6847 s->selected += MIN(nscroll,
6848 s->ndisplayed - s->selected - 1);
6849 break;
6851 ref_scroll_down(s, nscroll);
6852 break;
6853 case CTRL('l'):
6854 tog_free_refs();
6855 err = tog_load_refs(s->repo, s->sort_by_date);
6856 if (err)
6857 break;
6858 ref_view_free_refs(s);
6859 err = ref_view_load_refs(s);
6860 break;
6861 case KEY_RESIZE:
6862 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6863 s->selected = view->nlines - 2;
6864 break;
6865 default:
6866 break;
6869 return err;
6872 __dead static void
6873 usage_ref(void)
6875 endwin();
6876 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6877 getprogname());
6878 exit(1);
6881 static const struct got_error *
6882 cmd_ref(int argc, char *argv[])
6884 const struct got_error *error;
6885 struct got_repository *repo = NULL;
6886 struct got_worktree *worktree = NULL;
6887 char *cwd = NULL, *repo_path = NULL;
6888 int ch;
6889 struct tog_view *view;
6890 int *pack_fds = NULL;
6892 while ((ch = getopt(argc, argv, "r:")) != -1) {
6893 switch (ch) {
6894 case 'r':
6895 repo_path = realpath(optarg, NULL);
6896 if (repo_path == NULL)
6897 return got_error_from_errno2("realpath",
6898 optarg);
6899 break;
6900 default:
6901 usage_ref();
6902 /* NOTREACHED */
6906 argc -= optind;
6907 argv += optind;
6909 if (argc > 1)
6910 usage_ref();
6912 error = got_repo_pack_fds_open(&pack_fds);
6913 if (error != NULL)
6914 goto done;
6916 if (repo_path == NULL) {
6917 cwd = getcwd(NULL, 0);
6918 if (cwd == NULL)
6919 return got_error_from_errno("getcwd");
6920 error = got_worktree_open(&worktree, cwd);
6921 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6922 goto done;
6923 if (worktree)
6924 repo_path =
6925 strdup(got_worktree_get_repo_path(worktree));
6926 else
6927 repo_path = strdup(cwd);
6928 if (repo_path == NULL) {
6929 error = got_error_from_errno("strdup");
6930 goto done;
6934 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6935 if (error != NULL)
6936 goto done;
6938 init_curses();
6940 error = apply_unveil(got_repo_get_path(repo), NULL);
6941 if (error)
6942 goto done;
6944 error = tog_load_refs(repo, 0);
6945 if (error)
6946 goto done;
6948 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6949 if (view == NULL) {
6950 error = got_error_from_errno("view_open");
6951 goto done;
6954 error = open_ref_view(view, repo);
6955 if (error)
6956 goto done;
6958 if (worktree) {
6959 /* Release work tree lock. */
6960 got_worktree_close(worktree);
6961 worktree = NULL;
6963 error = view_loop(view);
6964 done:
6965 free(repo_path);
6966 free(cwd);
6967 if (repo) {
6968 const struct got_error *close_err = got_repo_close(repo);
6969 if (close_err)
6970 error = close_err;
6972 if (pack_fds) {
6973 const struct got_error *pack_err =
6974 got_repo_pack_fds_close(pack_fds);
6975 if (error == NULL)
6976 error = pack_err;
6978 tog_free_refs();
6979 return error;
6982 static void
6983 list_commands(FILE *fp)
6985 size_t i;
6987 fprintf(fp, "commands:");
6988 for (i = 0; i < nitems(tog_commands); i++) {
6989 const struct tog_cmd *cmd = &tog_commands[i];
6990 fprintf(fp, " %s", cmd->name);
6992 fputc('\n', fp);
6995 __dead static void
6996 usage(int hflag, int status)
6998 FILE *fp = (status == 0) ? stdout : stderr;
7000 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7001 getprogname());
7002 if (hflag) {
7003 fprintf(fp, "lazy usage: %s path\n", getprogname());
7004 list_commands(fp);
7006 exit(status);
7009 static char **
7010 make_argv(int argc, ...)
7012 va_list ap;
7013 char **argv;
7014 int i;
7016 va_start(ap, argc);
7018 argv = calloc(argc, sizeof(char *));
7019 if (argv == NULL)
7020 err(1, "calloc");
7021 for (i = 0; i < argc; i++) {
7022 argv[i] = strdup(va_arg(ap, char *));
7023 if (argv[i] == NULL)
7024 err(1, "strdup");
7027 va_end(ap);
7028 return argv;
7032 * Try to convert 'tog path' into a 'tog log path' command.
7033 * The user could simply have mistyped the command rather than knowingly
7034 * provided a path. So check whether argv[0] can in fact be resolved
7035 * to a path in the HEAD commit and print a special error if not.
7036 * This hack is for mpi@ <3
7038 static const struct got_error *
7039 tog_log_with_path(int argc, char *argv[])
7041 const struct got_error *error = NULL, *close_err;
7042 const struct tog_cmd *cmd = NULL;
7043 struct got_repository *repo = NULL;
7044 struct got_worktree *worktree = NULL;
7045 struct got_object_id *commit_id = NULL, *id = NULL;
7046 struct got_commit_object *commit = NULL;
7047 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7048 char *commit_id_str = NULL, **cmd_argv = NULL;
7049 int *pack_fds = NULL;
7051 cwd = getcwd(NULL, 0);
7052 if (cwd == NULL)
7053 return got_error_from_errno("getcwd");
7055 error = got_repo_pack_fds_open(&pack_fds);
7056 if (error != NULL)
7057 goto done;
7059 error = got_worktree_open(&worktree, cwd);
7060 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7061 goto done;
7063 if (worktree)
7064 repo_path = strdup(got_worktree_get_repo_path(worktree));
7065 else
7066 repo_path = strdup(cwd);
7067 if (repo_path == NULL) {
7068 error = got_error_from_errno("strdup");
7069 goto done;
7072 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7073 if (error != NULL)
7074 goto done;
7076 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7077 repo, worktree);
7078 if (error)
7079 goto done;
7081 error = tog_load_refs(repo, 0);
7082 if (error)
7083 goto done;
7084 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7085 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7086 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7087 if (error)
7088 goto done;
7090 if (worktree) {
7091 got_worktree_close(worktree);
7092 worktree = NULL;
7095 error = got_object_open_as_commit(&commit, repo, commit_id);
7096 if (error)
7097 goto done;
7099 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7100 if (error) {
7101 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7102 goto done;
7103 fprintf(stderr, "%s: '%s' is no known command or path\n",
7104 getprogname(), argv[0]);
7105 usage(1, 1);
7106 /* not reached */
7109 close_err = got_repo_close(repo);
7110 if (error == NULL)
7111 error = close_err;
7112 repo = NULL;
7114 error = got_object_id_str(&commit_id_str, commit_id);
7115 if (error)
7116 goto done;
7118 cmd = &tog_commands[0]; /* log */
7119 argc = 4;
7120 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7121 error = cmd->cmd_main(argc, cmd_argv);
7122 done:
7123 if (repo) {
7124 close_err = got_repo_close(repo);
7125 if (error == NULL)
7126 error = close_err;
7128 if (commit)
7129 got_object_commit_close(commit);
7130 if (worktree)
7131 got_worktree_close(worktree);
7132 if (pack_fds) {
7133 const struct got_error *pack_err =
7134 got_repo_pack_fds_close(pack_fds);
7135 if (error == NULL)
7136 error = pack_err;
7138 free(id);
7139 free(commit_id_str);
7140 free(commit_id);
7141 free(cwd);
7142 free(repo_path);
7143 free(in_repo_path);
7144 if (cmd_argv) {
7145 int i;
7146 for (i = 0; i < argc; i++)
7147 free(cmd_argv[i]);
7148 free(cmd_argv);
7150 tog_free_refs();
7151 return error;
7154 int
7155 main(int argc, char *argv[])
7157 const struct got_error *error = NULL;
7158 const struct tog_cmd *cmd = NULL;
7159 int ch, hflag = 0, Vflag = 0;
7160 char **cmd_argv = NULL;
7161 static const struct option longopts[] = {
7162 { "version", no_argument, NULL, 'V' },
7163 { NULL, 0, NULL, 0}
7166 setlocale(LC_CTYPE, "");
7168 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7169 switch (ch) {
7170 case 'h':
7171 hflag = 1;
7172 break;
7173 case 'V':
7174 Vflag = 1;
7175 break;
7176 default:
7177 usage(hflag, 1);
7178 /* NOTREACHED */
7182 argc -= optind;
7183 argv += optind;
7184 optind = 1;
7185 optreset = 1;
7187 if (Vflag) {
7188 got_version_print_str();
7189 return 0;
7192 #ifndef PROFILE
7193 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7194 NULL) == -1)
7195 err(1, "pledge");
7196 #endif
7198 if (argc == 0) {
7199 if (hflag)
7200 usage(hflag, 0);
7201 /* Build an argument vector which runs a default command. */
7202 cmd = &tog_commands[0];
7203 argc = 1;
7204 cmd_argv = make_argv(argc, cmd->name);
7205 } else {
7206 size_t i;
7208 /* Did the user specify a command? */
7209 for (i = 0; i < nitems(tog_commands); i++) {
7210 if (strncmp(tog_commands[i].name, argv[0],
7211 strlen(argv[0])) == 0) {
7212 cmd = &tog_commands[i];
7213 break;
7218 if (cmd == NULL) {
7219 if (argc != 1)
7220 usage(0, 1);
7221 /* No command specified; try log with a path */
7222 error = tog_log_with_path(argc, argv);
7223 } else {
7224 if (hflag)
7225 cmd->cmd_usage();
7226 else
7227 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7230 endwin();
7231 putchar('\n');
7232 if (cmd_argv) {
7233 int i;
7234 for (i = 0; i < argc; i++)
7235 free(cmd_argv[i]);
7236 free(cmd_argv);
7239 if (error && error->code != GOT_ERR_CANCELLED)
7240 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7241 return 0;