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 } else
3834 s->id1 = NULL;
3836 s->id2 = got_object_id_dup(id2);
3837 if (s->id2 == NULL) {
3838 err = got_error_from_errno("got_object_id_dup");
3839 goto done;
3842 s->f1 = got_opentemp();
3843 if (s->f1 == NULL) {
3844 err = got_error_from_errno("got_opentemp");
3845 goto done;
3848 s->f2 = got_opentemp();
3849 if (s->f2 == NULL) {
3850 err = got_error_from_errno("got_opentemp");
3851 goto done;
3854 s->first_displayed_line = 1;
3855 s->last_displayed_line = view->nlines;
3856 s->diff_context = diff_context;
3857 s->ignore_whitespace = ignore_whitespace;
3858 s->force_text_diff = force_text_diff;
3859 s->log_view = log_view;
3860 s->repo = repo;
3862 STAILQ_INIT(&s->colors);
3863 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3864 err = add_color(&s->colors,
3865 "^-", TOG_COLOR_DIFF_MINUS,
3866 get_color_value("TOG_COLOR_DIFF_MINUS"));
3867 if (err)
3868 goto done;
3869 err = add_color(&s->colors, "^\\+",
3870 TOG_COLOR_DIFF_PLUS,
3871 get_color_value("TOG_COLOR_DIFF_PLUS"));
3872 if (err)
3873 goto done;
3874 err = add_color(&s->colors,
3875 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3876 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3877 if (err)
3878 goto done;
3880 err = add_color(&s->colors,
3881 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3882 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3883 get_color_value("TOG_COLOR_DIFF_META"));
3884 if (err)
3885 goto done;
3887 err = add_color(&s->colors,
3888 "^(from|via): ", TOG_COLOR_AUTHOR,
3889 get_color_value("TOG_COLOR_AUTHOR"));
3890 if (err)
3891 goto done;
3893 err = add_color(&s->colors,
3894 "^date: ", TOG_COLOR_DATE,
3895 get_color_value("TOG_COLOR_DATE"));
3896 if (err)
3897 goto done;
3900 if (log_view && view_is_splitscreen(view))
3901 show_log_view(log_view); /* draw vborder */
3902 diff_view_indicate_progress(view);
3904 err = create_diff(s);
3906 view->show = show_diff_view;
3907 view->input = input_diff_view;
3908 view->close = close_diff_view;
3909 view->search_start = search_start_diff_view;
3910 view->search_next = search_next_diff_view;
3911 done:
3912 if (err)
3913 close_diff_view(view);
3914 return err;
3917 static const struct got_error *
3918 show_diff_view(struct tog_view *view)
3920 const struct got_error *err;
3921 struct tog_diff_view_state *s = &view->state.diff;
3922 char *id_str1 = NULL, *id_str2, *header;
3923 const char *label1, *label2;
3925 if (s->id1) {
3926 err = got_object_id_str(&id_str1, s->id1);
3927 if (err)
3928 return err;
3929 label1 = s->label1 ? : id_str1;
3930 } else
3931 label1 = "/dev/null";
3933 err = got_object_id_str(&id_str2, s->id2);
3934 if (err)
3935 return err;
3936 label2 = s->label2 ? : id_str2;
3938 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3939 err = got_error_from_errno("asprintf");
3940 free(id_str1);
3941 free(id_str2);
3942 return err;
3944 free(id_str1);
3945 free(id_str2);
3947 err = draw_file(view, header);
3948 free(header);
3949 return err;
3952 static const struct got_error *
3953 set_selected_commit(struct tog_diff_view_state *s,
3954 struct commit_queue_entry *entry)
3956 const struct got_error *err;
3957 const struct got_object_id_queue *parent_ids;
3958 struct got_commit_object *selected_commit;
3959 struct got_object_qid *pid;
3961 free(s->id2);
3962 s->id2 = got_object_id_dup(entry->id);
3963 if (s->id2 == NULL)
3964 return got_error_from_errno("got_object_id_dup");
3966 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3967 if (err)
3968 return err;
3969 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3970 free(s->id1);
3971 pid = STAILQ_FIRST(parent_ids);
3972 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3973 got_object_commit_close(selected_commit);
3974 return NULL;
3977 static const struct got_error *
3978 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3980 const struct got_error *err = NULL;
3981 struct tog_diff_view_state *s = &view->state.diff;
3982 struct tog_log_view_state *ls;
3983 struct commit_queue_entry *old_selected_entry;
3984 char *line = NULL;
3985 size_t linesize = 0;
3986 ssize_t linelen;
3987 int i, nscroll = view->nlines - 1;
3989 switch (ch) {
3990 case '0':
3991 view->x = 0;
3992 break;
3993 case '$':
3994 view->x = MAX(view->maxx - view->ncols / 3, 0);
3995 break;
3996 case KEY_RIGHT:
3997 case 'l':
3998 if (view->x + view->ncols / 3 < view->maxx)
3999 view->x += 2; /* move two columns right */
4000 break;
4001 case KEY_LEFT:
4002 case 'h':
4003 view->x -= MIN(view->x, 2); /* move two columns back */
4004 break;
4005 case 'a':
4006 case 'w':
4007 if (ch == 'a')
4008 s->force_text_diff = !s->force_text_diff;
4009 if (ch == 'w')
4010 s->ignore_whitespace = !s->ignore_whitespace;
4011 wclear(view->window);
4012 s->first_displayed_line = 1;
4013 s->last_displayed_line = view->nlines;
4014 s->matched_line = 0;
4015 diff_view_indicate_progress(view);
4016 err = create_diff(s);
4017 break;
4018 case 'g':
4019 case KEY_HOME:
4020 s->first_displayed_line = 1;
4021 break;
4022 case 'G':
4023 case KEY_END:
4024 if (s->eof)
4025 break;
4027 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4028 s->eof = 1;
4029 break;
4030 case 'k':
4031 case KEY_UP:
4032 case CTRL('p'):
4033 if (s->first_displayed_line > 1)
4034 s->first_displayed_line--;
4035 break;
4036 case CTRL('u'):
4037 case 'u':
4038 nscroll /= 2;
4039 /* FALL THROUGH */
4040 case KEY_PPAGE:
4041 case CTRL('b'):
4042 if (s->first_displayed_line == 1)
4043 break;
4044 i = 0;
4045 while (i++ < nscroll && s->first_displayed_line > 1)
4046 s->first_displayed_line--;
4047 break;
4048 case 'j':
4049 case KEY_DOWN:
4050 case CTRL('n'):
4051 if (!s->eof)
4052 s->first_displayed_line++;
4053 break;
4054 case CTRL('d'):
4055 case 'd':
4056 nscroll /= 2;
4057 /* FALL THROUGH */
4058 case KEY_NPAGE:
4059 case CTRL('f'):
4060 case ' ':
4061 if (s->eof)
4062 break;
4063 i = 0;
4064 while (!s->eof && i++ < nscroll) {
4065 linelen = getline(&line, &linesize, s->f);
4066 s->first_displayed_line++;
4067 if (linelen == -1) {
4068 if (feof(s->f)) {
4069 s->eof = 1;
4070 } else
4071 err = got_ferror(s->f, GOT_ERR_IO);
4072 break;
4075 free(line);
4076 break;
4077 case '[':
4078 if (s->diff_context > 0) {
4079 s->diff_context--;
4080 s->matched_line = 0;
4081 diff_view_indicate_progress(view);
4082 err = create_diff(s);
4083 if (s->first_displayed_line + view->nlines - 1 >
4084 s->nlines) {
4085 s->first_displayed_line = 1;
4086 s->last_displayed_line = view->nlines;
4089 break;
4090 case ']':
4091 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4092 s->diff_context++;
4093 s->matched_line = 0;
4094 diff_view_indicate_progress(view);
4095 err = create_diff(s);
4097 break;
4098 case '<':
4099 case ',':
4100 if (s->log_view == NULL)
4101 break;
4102 ls = &s->log_view->state.log;
4103 old_selected_entry = ls->selected_entry;
4105 err = input_log_view(NULL, s->log_view, KEY_UP);
4106 if (err)
4107 break;
4109 if (old_selected_entry == ls->selected_entry)
4110 break;
4112 err = set_selected_commit(s, ls->selected_entry);
4113 if (err)
4114 break;
4116 s->first_displayed_line = 1;
4117 s->last_displayed_line = view->nlines;
4118 s->matched_line = 0;
4119 view->x = 0;
4121 diff_view_indicate_progress(view);
4122 err = create_diff(s);
4123 break;
4124 case '>':
4125 case '.':
4126 if (s->log_view == NULL)
4127 break;
4128 ls = &s->log_view->state.log;
4129 old_selected_entry = ls->selected_entry;
4131 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4132 if (err)
4133 break;
4135 if (old_selected_entry == ls->selected_entry)
4136 break;
4138 err = set_selected_commit(s, ls->selected_entry);
4139 if (err)
4140 break;
4142 s->first_displayed_line = 1;
4143 s->last_displayed_line = view->nlines;
4144 s->matched_line = 0;
4145 view->x = 0;
4147 diff_view_indicate_progress(view);
4148 err = create_diff(s);
4149 break;
4150 default:
4151 break;
4154 return err;
4157 static const struct got_error *
4158 cmd_diff(int argc, char *argv[])
4160 const struct got_error *error = NULL;
4161 struct got_repository *repo = NULL;
4162 struct got_worktree *worktree = NULL;
4163 struct got_object_id *id1 = NULL, *id2 = NULL;
4164 char *repo_path = NULL, *cwd = NULL;
4165 char *id_str1 = NULL, *id_str2 = NULL;
4166 char *label1 = NULL, *label2 = NULL;
4167 int diff_context = 3, ignore_whitespace = 0;
4168 int ch, force_text_diff = 0;
4169 const char *errstr;
4170 struct tog_view *view;
4171 int *pack_fds = NULL;
4173 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4174 switch (ch) {
4175 case 'a':
4176 force_text_diff = 1;
4177 break;
4178 case 'C':
4179 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4180 &errstr);
4181 if (errstr != NULL)
4182 errx(1, "number of context lines is %s: %s",
4183 errstr, errstr);
4184 break;
4185 case 'r':
4186 repo_path = realpath(optarg, NULL);
4187 if (repo_path == NULL)
4188 return got_error_from_errno2("realpath",
4189 optarg);
4190 got_path_strip_trailing_slashes(repo_path);
4191 break;
4192 case 'w':
4193 ignore_whitespace = 1;
4194 break;
4195 default:
4196 usage_diff();
4197 /* NOTREACHED */
4201 argc -= optind;
4202 argv += optind;
4204 if (argc == 0) {
4205 usage_diff(); /* TODO show local worktree changes */
4206 } else if (argc == 2) {
4207 id_str1 = argv[0];
4208 id_str2 = argv[1];
4209 } else
4210 usage_diff();
4212 error = got_repo_pack_fds_open(&pack_fds);
4213 if (error)
4214 goto done;
4216 if (repo_path == NULL) {
4217 cwd = getcwd(NULL, 0);
4218 if (cwd == NULL)
4219 return got_error_from_errno("getcwd");
4220 error = got_worktree_open(&worktree, cwd);
4221 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4222 goto done;
4223 if (worktree)
4224 repo_path =
4225 strdup(got_worktree_get_repo_path(worktree));
4226 else
4227 repo_path = strdup(cwd);
4228 if (repo_path == NULL) {
4229 error = got_error_from_errno("strdup");
4230 goto done;
4234 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4235 if (error)
4236 goto done;
4238 init_curses();
4240 error = apply_unveil(got_repo_get_path(repo), NULL);
4241 if (error)
4242 goto done;
4244 error = tog_load_refs(repo, 0);
4245 if (error)
4246 goto done;
4248 error = got_repo_match_object_id(&id1, &label1, id_str1,
4249 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4250 if (error)
4251 goto done;
4253 error = got_repo_match_object_id(&id2, &label2, id_str2,
4254 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4255 if (error)
4256 goto done;
4258 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4259 if (view == NULL) {
4260 error = got_error_from_errno("view_open");
4261 goto done;
4263 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4264 ignore_whitespace, force_text_diff, NULL, repo);
4265 if (error)
4266 goto done;
4267 error = view_loop(view);
4268 done:
4269 free(label1);
4270 free(label2);
4271 free(repo_path);
4272 free(cwd);
4273 if (repo) {
4274 const struct got_error *close_err = got_repo_close(repo);
4275 if (error == NULL)
4276 error = close_err;
4278 if (worktree)
4279 got_worktree_close(worktree);
4280 if (pack_fds) {
4281 const struct got_error *pack_err =
4282 got_repo_pack_fds_close(pack_fds);
4283 if (error == NULL)
4284 error = pack_err;
4286 tog_free_refs();
4287 return error;
4290 __dead static void
4291 usage_blame(void)
4293 endwin();
4294 fprintf(stderr,
4295 "usage: %s blame [-c commit] [-r repository-path] path\n",
4296 getprogname());
4297 exit(1);
4300 struct tog_blame_line {
4301 int annotated;
4302 struct got_object_id *id;
4305 static const struct got_error *
4306 draw_blame(struct tog_view *view)
4308 struct tog_blame_view_state *s = &view->state.blame;
4309 struct tog_blame *blame = &s->blame;
4310 regmatch_t *regmatch = &view->regmatch;
4311 const struct got_error *err;
4312 int lineno = 0, nprinted = 0;
4313 char *line = NULL;
4314 size_t linesize = 0;
4315 ssize_t linelen;
4316 wchar_t *wline;
4317 int width;
4318 struct tog_blame_line *blame_line;
4319 struct got_object_id *prev_id = NULL;
4320 char *id_str;
4321 struct tog_color *tc;
4323 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4324 if (err)
4325 return err;
4327 rewind(blame->f);
4328 werase(view->window);
4330 if (asprintf(&line, "commit %s", id_str) == -1) {
4331 err = got_error_from_errno("asprintf");
4332 free(id_str);
4333 return err;
4336 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4337 free(line);
4338 line = NULL;
4339 if (err)
4340 return err;
4341 if (view_needs_focus_indication(view))
4342 wstandout(view->window);
4343 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4344 if (tc)
4345 wattr_on(view->window,
4346 COLOR_PAIR(tc->colorpair), NULL);
4347 waddwstr(view->window, wline);
4348 if (tc)
4349 wattr_off(view->window,
4350 COLOR_PAIR(tc->colorpair), NULL);
4351 if (view_needs_focus_indication(view))
4352 wstandend(view->window);
4353 free(wline);
4354 wline = NULL;
4355 if (width < view->ncols - 1)
4356 waddch(view->window, '\n');
4358 if (asprintf(&line, "[%d/%d] %s%s",
4359 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4360 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4361 free(id_str);
4362 return got_error_from_errno("asprintf");
4364 free(id_str);
4365 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4366 free(line);
4367 line = NULL;
4368 if (err)
4369 return err;
4370 waddwstr(view->window, wline);
4371 free(wline);
4372 wline = NULL;
4373 if (width < view->ncols - 1)
4374 waddch(view->window, '\n');
4376 s->eof = 0;
4377 view->maxx = 0;
4378 while (nprinted < view->nlines - 2) {
4379 linelen = getline(&line, &linesize, blame->f);
4380 if (linelen == -1) {
4381 if (feof(blame->f)) {
4382 s->eof = 1;
4383 break;
4385 free(line);
4386 return got_ferror(blame->f, GOT_ERR_IO);
4388 if (++lineno < s->first_displayed_line)
4389 continue;
4391 /* Set view->maxx based on full line length. */
4392 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4393 if (err) {
4394 free(line);
4395 return err;
4397 free(wline);
4398 wline = NULL;
4399 view->maxx = MAX(view->maxx, width);
4401 if (view->focussed && nprinted == s->selected_line - 1)
4402 wstandout(view->window);
4404 if (blame->nlines > 0) {
4405 blame_line = &blame->lines[lineno - 1];
4406 if (blame_line->annotated && prev_id &&
4407 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4408 !(view->focussed &&
4409 nprinted == s->selected_line - 1)) {
4410 waddstr(view->window, " ");
4411 } else if (blame_line->annotated) {
4412 char *id_str;
4413 err = got_object_id_str(&id_str,
4414 blame_line->id);
4415 if (err) {
4416 free(line);
4417 return err;
4419 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4420 if (tc)
4421 wattr_on(view->window,
4422 COLOR_PAIR(tc->colorpair), NULL);
4423 wprintw(view->window, "%.8s", id_str);
4424 if (tc)
4425 wattr_off(view->window,
4426 COLOR_PAIR(tc->colorpair), NULL);
4427 free(id_str);
4428 prev_id = blame_line->id;
4429 } else {
4430 waddstr(view->window, "........");
4431 prev_id = NULL;
4433 } else {
4434 waddstr(view->window, "........");
4435 prev_id = NULL;
4438 if (view->focussed && nprinted == s->selected_line - 1)
4439 wstandend(view->window);
4440 waddstr(view->window, " ");
4442 if (view->ncols <= 9) {
4443 width = 9;
4444 } else if (s->first_displayed_line + nprinted ==
4445 s->matched_line &&
4446 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4447 err = add_matched_line(&width, line, view->ncols - 9, 9,
4448 view->window, view->x, regmatch);
4449 if (err) {
4450 free(line);
4451 return err;
4453 width += 9;
4454 } else {
4455 int skip;
4456 err = format_line(&wline, &width, &skip, line,
4457 view->x, view->ncols - 9, 9, 1);
4458 if (err) {
4459 free(line);
4460 return err;
4462 waddwstr(view->window, &wline[skip]);
4463 width += 9;
4464 free(wline);
4465 wline = NULL;
4468 if (width <= view->ncols - 1)
4469 waddch(view->window, '\n');
4470 if (++nprinted == 1)
4471 s->first_displayed_line = lineno;
4473 free(line);
4474 s->last_displayed_line = lineno;
4476 view_vborder(view);
4478 return NULL;
4481 static const struct got_error *
4482 blame_cb(void *arg, int nlines, int lineno,
4483 struct got_commit_object *commit, struct got_object_id *id)
4485 const struct got_error *err = NULL;
4486 struct tog_blame_cb_args *a = arg;
4487 struct tog_blame_line *line;
4488 int errcode;
4490 if (nlines != a->nlines ||
4491 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4492 return got_error(GOT_ERR_RANGE);
4494 errcode = pthread_mutex_lock(&tog_mutex);
4495 if (errcode)
4496 return got_error_set_errno(errcode, "pthread_mutex_lock");
4498 if (*a->quit) { /* user has quit the blame view */
4499 err = got_error(GOT_ERR_ITER_COMPLETED);
4500 goto done;
4503 if (lineno == -1)
4504 goto done; /* no change in this commit */
4506 line = &a->lines[lineno - 1];
4507 if (line->annotated)
4508 goto done;
4510 line->id = got_object_id_dup(id);
4511 if (line->id == NULL) {
4512 err = got_error_from_errno("got_object_id_dup");
4513 goto done;
4515 line->annotated = 1;
4516 done:
4517 errcode = pthread_mutex_unlock(&tog_mutex);
4518 if (errcode)
4519 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4520 return err;
4523 static void *
4524 blame_thread(void *arg)
4526 const struct got_error *err, *close_err;
4527 struct tog_blame_thread_args *ta = arg;
4528 struct tog_blame_cb_args *a = ta->cb_args;
4529 int errcode;
4531 err = block_signals_used_by_main_thread();
4532 if (err)
4533 return (void *)err;
4535 err = got_blame(ta->path, a->commit_id, ta->repo,
4536 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4537 if (err && err->code == GOT_ERR_CANCELLED)
4538 err = NULL;
4540 errcode = pthread_mutex_lock(&tog_mutex);
4541 if (errcode)
4542 return (void *)got_error_set_errno(errcode,
4543 "pthread_mutex_lock");
4545 close_err = got_repo_close(ta->repo);
4546 if (err == NULL)
4547 err = close_err;
4548 ta->repo = NULL;
4549 *ta->complete = 1;
4551 errcode = pthread_mutex_unlock(&tog_mutex);
4552 if (errcode && err == NULL)
4553 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4555 return (void *)err;
4558 static struct got_object_id *
4559 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4560 int first_displayed_line, int selected_line)
4562 struct tog_blame_line *line;
4564 if (nlines <= 0)
4565 return NULL;
4567 line = &lines[first_displayed_line - 1 + selected_line - 1];
4568 if (!line->annotated)
4569 return NULL;
4571 return line->id;
4574 static const struct got_error *
4575 stop_blame(struct tog_blame *blame)
4577 const struct got_error *err = NULL;
4578 int i;
4580 if (blame->thread) {
4581 int errcode;
4582 errcode = pthread_mutex_unlock(&tog_mutex);
4583 if (errcode)
4584 return got_error_set_errno(errcode,
4585 "pthread_mutex_unlock");
4586 errcode = pthread_join(blame->thread, (void **)&err);
4587 if (errcode)
4588 return got_error_set_errno(errcode, "pthread_join");
4589 errcode = pthread_mutex_lock(&tog_mutex);
4590 if (errcode)
4591 return got_error_set_errno(errcode,
4592 "pthread_mutex_lock");
4593 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4594 err = NULL;
4595 blame->thread = NULL;
4597 if (blame->thread_args.repo) {
4598 const struct got_error *close_err;
4599 close_err = got_repo_close(blame->thread_args.repo);
4600 if (err == NULL)
4601 err = close_err;
4602 blame->thread_args.repo = NULL;
4604 if (blame->f) {
4605 if (fclose(blame->f) == EOF && err == NULL)
4606 err = got_error_from_errno("fclose");
4607 blame->f = NULL;
4609 if (blame->lines) {
4610 for (i = 0; i < blame->nlines; i++)
4611 free(blame->lines[i].id);
4612 free(blame->lines);
4613 blame->lines = NULL;
4615 free(blame->cb_args.commit_id);
4616 blame->cb_args.commit_id = NULL;
4617 if (blame->pack_fds) {
4618 const struct got_error *pack_err =
4619 got_repo_pack_fds_close(blame->pack_fds);
4620 if (err == NULL)
4621 err = pack_err;
4622 blame->pack_fds = NULL;
4624 return err;
4627 static const struct got_error *
4628 cancel_blame_view(void *arg)
4630 const struct got_error *err = NULL;
4631 int *done = arg;
4632 int errcode;
4634 errcode = pthread_mutex_lock(&tog_mutex);
4635 if (errcode)
4636 return got_error_set_errno(errcode,
4637 "pthread_mutex_unlock");
4639 if (*done)
4640 err = got_error(GOT_ERR_CANCELLED);
4642 errcode = pthread_mutex_unlock(&tog_mutex);
4643 if (errcode)
4644 return got_error_set_errno(errcode,
4645 "pthread_mutex_lock");
4647 return err;
4650 static const struct got_error *
4651 run_blame(struct tog_view *view)
4653 struct tog_blame_view_state *s = &view->state.blame;
4654 struct tog_blame *blame = &s->blame;
4655 const struct got_error *err = NULL;
4656 struct got_commit_object *commit = NULL;
4657 struct got_blob_object *blob = NULL;
4658 struct got_repository *thread_repo = NULL;
4659 struct got_object_id *obj_id = NULL;
4660 int obj_type;
4661 int *pack_fds = NULL;
4663 err = got_object_open_as_commit(&commit, s->repo,
4664 &s->blamed_commit->id);
4665 if (err)
4666 return err;
4668 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4669 if (err)
4670 goto done;
4672 err = got_object_get_type(&obj_type, s->repo, obj_id);
4673 if (err)
4674 goto done;
4676 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4677 err = got_error(GOT_ERR_OBJ_TYPE);
4678 goto done;
4681 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4682 if (err)
4683 goto done;
4684 blame->f = got_opentemp();
4685 if (blame->f == NULL) {
4686 err = got_error_from_errno("got_opentemp");
4687 goto done;
4689 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4690 &blame->line_offsets, blame->f, blob);
4691 if (err)
4692 goto done;
4693 if (blame->nlines == 0) {
4694 s->blame_complete = 1;
4695 goto done;
4698 /* Don't include \n at EOF in the blame line count. */
4699 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4700 blame->nlines--;
4702 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4703 if (blame->lines == NULL) {
4704 err = got_error_from_errno("calloc");
4705 goto done;
4708 err = got_repo_pack_fds_open(&pack_fds);
4709 if (err)
4710 goto done;
4711 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4712 pack_fds);
4713 if (err)
4714 goto done;
4716 blame->pack_fds = pack_fds;
4717 blame->cb_args.view = view;
4718 blame->cb_args.lines = blame->lines;
4719 blame->cb_args.nlines = blame->nlines;
4720 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4721 if (blame->cb_args.commit_id == NULL) {
4722 err = got_error_from_errno("got_object_id_dup");
4723 goto done;
4725 blame->cb_args.quit = &s->done;
4727 blame->thread_args.path = s->path;
4728 blame->thread_args.repo = thread_repo;
4729 blame->thread_args.cb_args = &blame->cb_args;
4730 blame->thread_args.complete = &s->blame_complete;
4731 blame->thread_args.cancel_cb = cancel_blame_view;
4732 blame->thread_args.cancel_arg = &s->done;
4733 s->blame_complete = 0;
4735 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4736 s->first_displayed_line = 1;
4737 s->last_displayed_line = view->nlines;
4738 s->selected_line = 1;
4740 s->matched_line = 0;
4742 done:
4743 if (commit)
4744 got_object_commit_close(commit);
4745 if (blob)
4746 got_object_blob_close(blob);
4747 free(obj_id);
4748 if (err)
4749 stop_blame(blame);
4750 return err;
4753 static const struct got_error *
4754 open_blame_view(struct tog_view *view, char *path,
4755 struct got_object_id *commit_id, struct got_repository *repo)
4757 const struct got_error *err = NULL;
4758 struct tog_blame_view_state *s = &view->state.blame;
4760 STAILQ_INIT(&s->blamed_commits);
4762 s->path = strdup(path);
4763 if (s->path == NULL)
4764 return got_error_from_errno("strdup");
4766 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4767 if (err) {
4768 free(s->path);
4769 return err;
4772 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4773 s->first_displayed_line = 1;
4774 s->last_displayed_line = view->nlines;
4775 s->selected_line = 1;
4776 s->blame_complete = 0;
4777 s->repo = repo;
4778 s->commit_id = commit_id;
4779 memset(&s->blame, 0, sizeof(s->blame));
4781 STAILQ_INIT(&s->colors);
4782 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4783 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4784 get_color_value("TOG_COLOR_COMMIT"));
4785 if (err)
4786 return err;
4789 view->show = show_blame_view;
4790 view->input = input_blame_view;
4791 view->close = close_blame_view;
4792 view->search_start = search_start_blame_view;
4793 view->search_next = search_next_blame_view;
4795 return run_blame(view);
4798 static const struct got_error *
4799 close_blame_view(struct tog_view *view)
4801 const struct got_error *err = NULL;
4802 struct tog_blame_view_state *s = &view->state.blame;
4804 if (s->blame.thread)
4805 err = stop_blame(&s->blame);
4807 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4808 struct got_object_qid *blamed_commit;
4809 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4810 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4811 got_object_qid_free(blamed_commit);
4814 free(s->path);
4815 free_colors(&s->colors);
4816 return err;
4819 static const struct got_error *
4820 search_start_blame_view(struct tog_view *view)
4822 struct tog_blame_view_state *s = &view->state.blame;
4824 s->matched_line = 0;
4825 return NULL;
4828 static const struct got_error *
4829 search_next_blame_view(struct tog_view *view)
4831 struct tog_blame_view_state *s = &view->state.blame;
4832 const struct got_error *err = NULL;
4833 int lineno;
4834 char *line = NULL;
4835 size_t linesize = 0;
4836 ssize_t linelen;
4838 if (!view->searching) {
4839 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4840 return NULL;
4843 if (s->matched_line) {
4844 if (view->searching == TOG_SEARCH_FORWARD)
4845 lineno = s->matched_line + 1;
4846 else
4847 lineno = s->matched_line - 1;
4848 } else
4849 lineno = s->first_displayed_line - 1 + s->selected_line;
4851 while (1) {
4852 off_t offset;
4854 if (lineno <= 0 || lineno > s->blame.nlines) {
4855 if (s->matched_line == 0) {
4856 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4857 break;
4860 if (view->searching == TOG_SEARCH_FORWARD)
4861 lineno = 1;
4862 else
4863 lineno = s->blame.nlines;
4866 offset = s->blame.line_offsets[lineno - 1];
4867 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4868 free(line);
4869 return got_error_from_errno("fseeko");
4871 linelen = getline(&line, &linesize, s->blame.f);
4872 if (linelen != -1) {
4873 char *exstr;
4874 err = expand_tab(&exstr, line);
4875 if (err)
4876 break;
4877 if (match_line(exstr, &view->regex, 1,
4878 &view->regmatch)) {
4879 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4880 s->matched_line = lineno;
4881 free(exstr);
4882 break;
4884 free(exstr);
4886 if (view->searching == TOG_SEARCH_FORWARD)
4887 lineno++;
4888 else
4889 lineno--;
4891 free(line);
4893 if (s->matched_line) {
4894 s->first_displayed_line = s->matched_line;
4895 s->selected_line = 1;
4898 return err;
4901 static const struct got_error *
4902 show_blame_view(struct tog_view *view)
4904 const struct got_error *err = NULL;
4905 struct tog_blame_view_state *s = &view->state.blame;
4906 int errcode;
4908 if (s->blame.thread == NULL && !s->blame_complete) {
4909 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4910 &s->blame.thread_args);
4911 if (errcode)
4912 return got_error_set_errno(errcode, "pthread_create");
4914 halfdelay(1); /* fast refresh while annotating */
4917 if (s->blame_complete)
4918 halfdelay(10); /* disable fast refresh */
4920 err = draw_blame(view);
4922 view_vborder(view);
4923 return err;
4926 static const struct got_error *
4927 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4929 const struct got_error *err = NULL, *thread_err = NULL;
4930 struct tog_view *diff_view;
4931 struct tog_blame_view_state *s = &view->state.blame;
4932 int begin_x = 0, nscroll = view->nlines - 2;
4934 switch (ch) {
4935 case '0':
4936 view->x = 0;
4937 break;
4938 case '$':
4939 view->x = MAX(view->maxx - view->ncols / 3, 0);
4940 break;
4941 case KEY_RIGHT:
4942 case 'l':
4943 if (view->x + view->ncols / 3 < view->maxx)
4944 view->x += 2; /* move two columns right */
4945 break;
4946 case KEY_LEFT:
4947 case 'h':
4948 view->x -= MIN(view->x, 2); /* move two columns back */
4949 break;
4950 case 'q':
4951 s->done = 1;
4952 break;
4953 case 'g':
4954 case KEY_HOME:
4955 s->selected_line = 1;
4956 s->first_displayed_line = 1;
4957 break;
4958 case 'G':
4959 case KEY_END:
4960 if (s->blame.nlines < view->nlines - 2) {
4961 s->selected_line = s->blame.nlines;
4962 s->first_displayed_line = 1;
4963 } else {
4964 s->selected_line = view->nlines - 2;
4965 s->first_displayed_line = s->blame.nlines -
4966 (view->nlines - 3);
4968 break;
4969 case 'k':
4970 case KEY_UP:
4971 case CTRL('p'):
4972 if (s->selected_line > 1)
4973 s->selected_line--;
4974 else if (s->selected_line == 1 &&
4975 s->first_displayed_line > 1)
4976 s->first_displayed_line--;
4977 break;
4978 case CTRL('u'):
4979 case 'u':
4980 nscroll /= 2;
4981 /* FALL THROUGH */
4982 case KEY_PPAGE:
4983 case CTRL('b'):
4984 if (s->first_displayed_line == 1) {
4985 s->selected_line = MAX(1, s->selected_line - nscroll);
4986 break;
4988 if (s->first_displayed_line > nscroll)
4989 s->first_displayed_line -= nscroll;
4990 else
4991 s->first_displayed_line = 1;
4992 break;
4993 case 'j':
4994 case KEY_DOWN:
4995 case CTRL('n'):
4996 if (s->selected_line < view->nlines - 2 &&
4997 s->first_displayed_line +
4998 s->selected_line <= s->blame.nlines)
4999 s->selected_line++;
5000 else if (s->last_displayed_line <
5001 s->blame.nlines)
5002 s->first_displayed_line++;
5003 break;
5004 case 'b':
5005 case 'p': {
5006 struct got_object_id *id = NULL;
5007 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5008 s->first_displayed_line, s->selected_line);
5009 if (id == NULL)
5010 break;
5011 if (ch == 'p') {
5012 struct got_commit_object *commit, *pcommit;
5013 struct got_object_qid *pid;
5014 struct got_object_id *blob_id = NULL;
5015 int obj_type;
5016 err = got_object_open_as_commit(&commit,
5017 s->repo, id);
5018 if (err)
5019 break;
5020 pid = STAILQ_FIRST(
5021 got_object_commit_get_parent_ids(commit));
5022 if (pid == NULL) {
5023 got_object_commit_close(commit);
5024 break;
5026 /* Check if path history ends here. */
5027 err = got_object_open_as_commit(&pcommit,
5028 s->repo, &pid->id);
5029 if (err)
5030 break;
5031 err = got_object_id_by_path(&blob_id, s->repo,
5032 pcommit, s->path);
5033 got_object_commit_close(pcommit);
5034 if (err) {
5035 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5036 err = NULL;
5037 got_object_commit_close(commit);
5038 break;
5040 err = got_object_get_type(&obj_type, s->repo,
5041 blob_id);
5042 free(blob_id);
5043 /* Can't blame non-blob type objects. */
5044 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5045 got_object_commit_close(commit);
5046 break;
5048 err = got_object_qid_alloc(&s->blamed_commit,
5049 &pid->id);
5050 got_object_commit_close(commit);
5051 } else {
5052 if (got_object_id_cmp(id,
5053 &s->blamed_commit->id) == 0)
5054 break;
5055 err = got_object_qid_alloc(&s->blamed_commit,
5056 id);
5058 if (err)
5059 break;
5060 s->done = 1;
5061 thread_err = stop_blame(&s->blame);
5062 s->done = 0;
5063 if (thread_err)
5064 break;
5065 STAILQ_INSERT_HEAD(&s->blamed_commits,
5066 s->blamed_commit, entry);
5067 err = run_blame(view);
5068 if (err)
5069 break;
5070 break;
5072 case 'B': {
5073 struct got_object_qid *first;
5074 first = STAILQ_FIRST(&s->blamed_commits);
5075 if (!got_object_id_cmp(&first->id, s->commit_id))
5076 break;
5077 s->done = 1;
5078 thread_err = stop_blame(&s->blame);
5079 s->done = 0;
5080 if (thread_err)
5081 break;
5082 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5083 got_object_qid_free(s->blamed_commit);
5084 s->blamed_commit =
5085 STAILQ_FIRST(&s->blamed_commits);
5086 err = run_blame(view);
5087 if (err)
5088 break;
5089 break;
5091 case KEY_ENTER:
5092 case '\r': {
5093 struct got_object_id *id = NULL;
5094 struct got_object_qid *pid;
5095 struct got_commit_object *commit = NULL;
5096 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5097 s->first_displayed_line, s->selected_line);
5098 if (id == NULL)
5099 break;
5100 err = got_object_open_as_commit(&commit, s->repo, id);
5101 if (err)
5102 break;
5103 pid = STAILQ_FIRST(
5104 got_object_commit_get_parent_ids(commit));
5105 if (view_is_parent_view(view))
5106 begin_x = view_split_begin_x(view->begin_x);
5107 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5108 if (diff_view == NULL) {
5109 got_object_commit_close(commit);
5110 err = got_error_from_errno("view_open");
5111 break;
5113 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5114 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5115 got_object_commit_close(commit);
5116 if (err) {
5117 view_close(diff_view);
5118 break;
5120 view->focussed = 0;
5121 diff_view->focussed = 1;
5122 if (view_is_parent_view(view)) {
5123 err = view_close_child(view);
5124 if (err)
5125 break;
5126 err = view_set_child(view, diff_view);
5127 if (err)
5128 break;
5129 view->focus_child = 1;
5130 } else
5131 *new_view = diff_view;
5132 if (err)
5133 break;
5134 break;
5136 case CTRL('d'):
5137 case 'd':
5138 nscroll /= 2;
5139 /* FALL THROUGH */
5140 case KEY_NPAGE:
5141 case CTRL('f'):
5142 case ' ':
5143 if (s->last_displayed_line >= s->blame.nlines &&
5144 s->selected_line >= MIN(s->blame.nlines,
5145 view->nlines - 2)) {
5146 break;
5148 if (s->last_displayed_line >= s->blame.nlines &&
5149 s->selected_line < view->nlines - 2) {
5150 s->selected_line +=
5151 MIN(nscroll, s->last_displayed_line -
5152 s->first_displayed_line - s->selected_line + 1);
5154 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5155 s->first_displayed_line += nscroll;
5156 else
5157 s->first_displayed_line =
5158 s->blame.nlines - (view->nlines - 3);
5159 break;
5160 case KEY_RESIZE:
5161 if (s->selected_line > view->nlines - 2) {
5162 s->selected_line = MIN(s->blame.nlines,
5163 view->nlines - 2);
5165 break;
5166 default:
5167 break;
5169 return thread_err ? thread_err : err;
5172 static const struct got_error *
5173 cmd_blame(int argc, char *argv[])
5175 const struct got_error *error;
5176 struct got_repository *repo = NULL;
5177 struct got_worktree *worktree = NULL;
5178 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5179 char *link_target = NULL;
5180 struct got_object_id *commit_id = NULL;
5181 struct got_commit_object *commit = NULL;
5182 char *commit_id_str = NULL;
5183 int ch;
5184 struct tog_view *view;
5185 int *pack_fds = NULL;
5187 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5188 switch (ch) {
5189 case 'c':
5190 commit_id_str = optarg;
5191 break;
5192 case 'r':
5193 repo_path = realpath(optarg, NULL);
5194 if (repo_path == NULL)
5195 return got_error_from_errno2("realpath",
5196 optarg);
5197 break;
5198 default:
5199 usage_blame();
5200 /* NOTREACHED */
5204 argc -= optind;
5205 argv += optind;
5207 if (argc != 1)
5208 usage_blame();
5210 error = got_repo_pack_fds_open(&pack_fds);
5211 if (error != NULL)
5212 goto done;
5214 if (repo_path == NULL) {
5215 cwd = getcwd(NULL, 0);
5216 if (cwd == NULL)
5217 return got_error_from_errno("getcwd");
5218 error = got_worktree_open(&worktree, cwd);
5219 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5220 goto done;
5221 if (worktree)
5222 repo_path =
5223 strdup(got_worktree_get_repo_path(worktree));
5224 else
5225 repo_path = strdup(cwd);
5226 if (repo_path == NULL) {
5227 error = got_error_from_errno("strdup");
5228 goto done;
5232 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5233 if (error != NULL)
5234 goto done;
5236 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5237 worktree);
5238 if (error)
5239 goto done;
5241 init_curses();
5243 error = apply_unveil(got_repo_get_path(repo), NULL);
5244 if (error)
5245 goto done;
5247 error = tog_load_refs(repo, 0);
5248 if (error)
5249 goto done;
5251 if (commit_id_str == NULL) {
5252 struct got_reference *head_ref;
5253 error = got_ref_open(&head_ref, repo, worktree ?
5254 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5255 if (error != NULL)
5256 goto done;
5257 error = got_ref_resolve(&commit_id, repo, head_ref);
5258 got_ref_close(head_ref);
5259 } else {
5260 error = got_repo_match_object_id(&commit_id, NULL,
5261 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5263 if (error != NULL)
5264 goto done;
5266 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5267 if (view == NULL) {
5268 error = got_error_from_errno("view_open");
5269 goto done;
5272 error = got_object_open_as_commit(&commit, repo, commit_id);
5273 if (error)
5274 goto done;
5276 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5277 commit, repo);
5278 if (error)
5279 goto done;
5281 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5282 commit_id, repo);
5283 if (error)
5284 goto done;
5285 if (worktree) {
5286 /* Release work tree lock. */
5287 got_worktree_close(worktree);
5288 worktree = NULL;
5290 error = view_loop(view);
5291 done:
5292 free(repo_path);
5293 free(in_repo_path);
5294 free(link_target);
5295 free(cwd);
5296 free(commit_id);
5297 if (commit)
5298 got_object_commit_close(commit);
5299 if (worktree)
5300 got_worktree_close(worktree);
5301 if (repo) {
5302 const struct got_error *close_err = got_repo_close(repo);
5303 if (error == NULL)
5304 error = close_err;
5306 if (pack_fds) {
5307 const struct got_error *pack_err =
5308 got_repo_pack_fds_close(pack_fds);
5309 if (error == NULL)
5310 error = pack_err;
5312 tog_free_refs();
5313 return error;
5316 static const struct got_error *
5317 draw_tree_entries(struct tog_view *view, const char *parent_path)
5319 struct tog_tree_view_state *s = &view->state.tree;
5320 const struct got_error *err = NULL;
5321 struct got_tree_entry *te;
5322 wchar_t *wline;
5323 struct tog_color *tc;
5324 int width, n, i, nentries;
5325 int limit = view->nlines;
5327 s->ndisplayed = 0;
5329 werase(view->window);
5331 if (limit == 0)
5332 return NULL;
5334 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5335 0, 0);
5336 if (err)
5337 return err;
5338 if (view_needs_focus_indication(view))
5339 wstandout(view->window);
5340 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5341 if (tc)
5342 wattr_on(view->window,
5343 COLOR_PAIR(tc->colorpair), NULL);
5344 waddwstr(view->window, wline);
5345 if (tc)
5346 wattr_off(view->window,
5347 COLOR_PAIR(tc->colorpair), NULL);
5348 if (view_needs_focus_indication(view))
5349 wstandend(view->window);
5350 free(wline);
5351 wline = NULL;
5352 if (width < view->ncols - 1)
5353 waddch(view->window, '\n');
5354 if (--limit <= 0)
5355 return NULL;
5356 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5357 0, 0);
5358 if (err)
5359 return err;
5360 waddwstr(view->window, wline);
5361 free(wline);
5362 wline = NULL;
5363 if (width < view->ncols - 1)
5364 waddch(view->window, '\n');
5365 if (--limit <= 0)
5366 return NULL;
5367 waddch(view->window, '\n');
5368 if (--limit <= 0)
5369 return NULL;
5371 if (s->first_displayed_entry == NULL) {
5372 te = got_object_tree_get_first_entry(s->tree);
5373 if (s->selected == 0) {
5374 if (view->focussed)
5375 wstandout(view->window);
5376 s->selected_entry = NULL;
5378 waddstr(view->window, " ..\n"); /* parent directory */
5379 if (s->selected == 0 && view->focussed)
5380 wstandend(view->window);
5381 s->ndisplayed++;
5382 if (--limit <= 0)
5383 return NULL;
5384 n = 1;
5385 } else {
5386 n = 0;
5387 te = s->first_displayed_entry;
5390 nentries = got_object_tree_get_nentries(s->tree);
5391 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5392 char *line = NULL, *id_str = NULL, *link_target = NULL;
5393 const char *modestr = "";
5394 mode_t mode;
5396 te = got_object_tree_get_entry(s->tree, i);
5397 mode = got_tree_entry_get_mode(te);
5399 if (s->show_ids) {
5400 err = got_object_id_str(&id_str,
5401 got_tree_entry_get_id(te));
5402 if (err)
5403 return got_error_from_errno(
5404 "got_object_id_str");
5406 if (got_object_tree_entry_is_submodule(te))
5407 modestr = "$";
5408 else if (S_ISLNK(mode)) {
5409 int i;
5411 err = got_tree_entry_get_symlink_target(&link_target,
5412 te, s->repo);
5413 if (err) {
5414 free(id_str);
5415 return err;
5417 for (i = 0; i < strlen(link_target); i++) {
5418 if (!isprint((unsigned char)link_target[i]))
5419 link_target[i] = '?';
5421 modestr = "@";
5423 else if (S_ISDIR(mode))
5424 modestr = "/";
5425 else if (mode & S_IXUSR)
5426 modestr = "*";
5427 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5428 got_tree_entry_get_name(te), modestr,
5429 link_target ? " -> ": "",
5430 link_target ? link_target : "") == -1) {
5431 free(id_str);
5432 free(link_target);
5433 return got_error_from_errno("asprintf");
5435 free(id_str);
5436 free(link_target);
5437 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5438 0, 0);
5439 if (err) {
5440 free(line);
5441 break;
5443 if (n == s->selected) {
5444 if (view->focussed)
5445 wstandout(view->window);
5446 s->selected_entry = te;
5448 tc = match_color(&s->colors, line);
5449 if (tc)
5450 wattr_on(view->window,
5451 COLOR_PAIR(tc->colorpair), NULL);
5452 waddwstr(view->window, wline);
5453 if (tc)
5454 wattr_off(view->window,
5455 COLOR_PAIR(tc->colorpair), NULL);
5456 if (width < view->ncols - 1)
5457 waddch(view->window, '\n');
5458 if (n == s->selected && view->focussed)
5459 wstandend(view->window);
5460 free(line);
5461 free(wline);
5462 wline = NULL;
5463 n++;
5464 s->ndisplayed++;
5465 s->last_displayed_entry = te;
5466 if (--limit <= 0)
5467 break;
5470 return err;
5473 static void
5474 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5476 struct got_tree_entry *te;
5477 int isroot = s->tree == s->root;
5478 int i = 0;
5480 if (s->first_displayed_entry == NULL)
5481 return;
5483 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5484 while (i++ < maxscroll) {
5485 if (te == NULL) {
5486 if (!isroot)
5487 s->first_displayed_entry = NULL;
5488 break;
5490 s->first_displayed_entry = te;
5491 te = got_tree_entry_get_prev(s->tree, te);
5495 static void
5496 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5498 struct got_tree_entry *next, *last;
5499 int n = 0;
5501 if (s->first_displayed_entry)
5502 next = got_tree_entry_get_next(s->tree,
5503 s->first_displayed_entry);
5504 else
5505 next = got_object_tree_get_first_entry(s->tree);
5507 last = s->last_displayed_entry;
5508 while (next && last && n++ < maxscroll) {
5509 last = got_tree_entry_get_next(s->tree, last);
5510 if (last) {
5511 s->first_displayed_entry = next;
5512 next = got_tree_entry_get_next(s->tree, next);
5517 static const struct got_error *
5518 tree_entry_path(char **path, struct tog_parent_trees *parents,
5519 struct got_tree_entry *te)
5521 const struct got_error *err = NULL;
5522 struct tog_parent_tree *pt;
5523 size_t len = 2; /* for leading slash and NUL */
5525 TAILQ_FOREACH(pt, parents, entry)
5526 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5527 + 1 /* slash */;
5528 if (te)
5529 len += strlen(got_tree_entry_get_name(te));
5531 *path = calloc(1, len);
5532 if (path == NULL)
5533 return got_error_from_errno("calloc");
5535 (*path)[0] = '/';
5536 pt = TAILQ_LAST(parents, tog_parent_trees);
5537 while (pt) {
5538 const char *name = got_tree_entry_get_name(pt->selected_entry);
5539 if (strlcat(*path, name, len) >= len) {
5540 err = got_error(GOT_ERR_NO_SPACE);
5541 goto done;
5543 if (strlcat(*path, "/", len) >= len) {
5544 err = got_error(GOT_ERR_NO_SPACE);
5545 goto done;
5547 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5549 if (te) {
5550 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5551 err = got_error(GOT_ERR_NO_SPACE);
5552 goto done;
5555 done:
5556 if (err) {
5557 free(*path);
5558 *path = NULL;
5560 return err;
5563 static const struct got_error *
5564 blame_tree_entry(struct tog_view **new_view, int begin_x,
5565 struct got_tree_entry *te, struct tog_parent_trees *parents,
5566 struct got_object_id *commit_id, struct got_repository *repo)
5568 const struct got_error *err = NULL;
5569 char *path;
5570 struct tog_view *blame_view;
5572 *new_view = NULL;
5574 err = tree_entry_path(&path, parents, te);
5575 if (err)
5576 return err;
5578 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5579 if (blame_view == NULL) {
5580 err = got_error_from_errno("view_open");
5581 goto done;
5584 err = open_blame_view(blame_view, path, commit_id, repo);
5585 if (err) {
5586 if (err->code == GOT_ERR_CANCELLED)
5587 err = NULL;
5588 view_close(blame_view);
5589 } else
5590 *new_view = blame_view;
5591 done:
5592 free(path);
5593 return err;
5596 static const struct got_error *
5597 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5598 struct tog_tree_view_state *s)
5600 struct tog_view *log_view;
5601 const struct got_error *err = NULL;
5602 char *path;
5604 *new_view = NULL;
5606 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5607 if (log_view == NULL)
5608 return got_error_from_errno("view_open");
5610 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5611 if (err)
5612 return err;
5614 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5615 path, 0);
5616 if (err)
5617 view_close(log_view);
5618 else
5619 *new_view = log_view;
5620 free(path);
5621 return err;
5624 static const struct got_error *
5625 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5626 const char *head_ref_name, struct got_repository *repo)
5628 const struct got_error *err = NULL;
5629 char *commit_id_str = NULL;
5630 struct tog_tree_view_state *s = &view->state.tree;
5631 struct got_commit_object *commit = NULL;
5633 TAILQ_INIT(&s->parents);
5634 STAILQ_INIT(&s->colors);
5636 s->commit_id = got_object_id_dup(commit_id);
5637 if (s->commit_id == NULL)
5638 return got_error_from_errno("got_object_id_dup");
5640 err = got_object_open_as_commit(&commit, repo, commit_id);
5641 if (err)
5642 goto done;
5645 * The root is opened here and will be closed when the view is closed.
5646 * Any visited subtrees and their path-wise parents are opened and
5647 * closed on demand.
5649 err = got_object_open_as_tree(&s->root, repo,
5650 got_object_commit_get_tree_id(commit));
5651 if (err)
5652 goto done;
5653 s->tree = s->root;
5655 err = got_object_id_str(&commit_id_str, commit_id);
5656 if (err != NULL)
5657 goto done;
5659 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5660 err = got_error_from_errno("asprintf");
5661 goto done;
5664 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5665 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5666 if (head_ref_name) {
5667 s->head_ref_name = strdup(head_ref_name);
5668 if (s->head_ref_name == NULL) {
5669 err = got_error_from_errno("strdup");
5670 goto done;
5673 s->repo = repo;
5675 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5676 err = add_color(&s->colors, "\\$$",
5677 TOG_COLOR_TREE_SUBMODULE,
5678 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5679 if (err)
5680 goto done;
5681 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5682 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5683 if (err)
5684 goto done;
5685 err = add_color(&s->colors, "/$",
5686 TOG_COLOR_TREE_DIRECTORY,
5687 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5688 if (err)
5689 goto done;
5691 err = add_color(&s->colors, "\\*$",
5692 TOG_COLOR_TREE_EXECUTABLE,
5693 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5694 if (err)
5695 goto done;
5697 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5698 get_color_value("TOG_COLOR_COMMIT"));
5699 if (err)
5700 goto done;
5703 view->show = show_tree_view;
5704 view->input = input_tree_view;
5705 view->close = close_tree_view;
5706 view->search_start = search_start_tree_view;
5707 view->search_next = search_next_tree_view;
5708 done:
5709 free(commit_id_str);
5710 if (commit)
5711 got_object_commit_close(commit);
5712 if (err)
5713 close_tree_view(view);
5714 return err;
5717 static const struct got_error *
5718 close_tree_view(struct tog_view *view)
5720 struct tog_tree_view_state *s = &view->state.tree;
5722 free_colors(&s->colors);
5723 free(s->tree_label);
5724 s->tree_label = NULL;
5725 free(s->commit_id);
5726 s->commit_id = NULL;
5727 free(s->head_ref_name);
5728 s->head_ref_name = NULL;
5729 while (!TAILQ_EMPTY(&s->parents)) {
5730 struct tog_parent_tree *parent;
5731 parent = TAILQ_FIRST(&s->parents);
5732 TAILQ_REMOVE(&s->parents, parent, entry);
5733 if (parent->tree != s->root)
5734 got_object_tree_close(parent->tree);
5735 free(parent);
5738 if (s->tree != NULL && s->tree != s->root)
5739 got_object_tree_close(s->tree);
5740 if (s->root)
5741 got_object_tree_close(s->root);
5742 return NULL;
5745 static const struct got_error *
5746 search_start_tree_view(struct tog_view *view)
5748 struct tog_tree_view_state *s = &view->state.tree;
5750 s->matched_entry = NULL;
5751 return NULL;
5754 static int
5755 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5757 regmatch_t regmatch;
5759 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5760 0) == 0;
5763 static const struct got_error *
5764 search_next_tree_view(struct tog_view *view)
5766 struct tog_tree_view_state *s = &view->state.tree;
5767 struct got_tree_entry *te = NULL;
5769 if (!view->searching) {
5770 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5771 return NULL;
5774 if (s->matched_entry) {
5775 if (view->searching == TOG_SEARCH_FORWARD) {
5776 if (s->selected_entry)
5777 te = got_tree_entry_get_next(s->tree,
5778 s->selected_entry);
5779 else
5780 te = got_object_tree_get_first_entry(s->tree);
5781 } else {
5782 if (s->selected_entry == NULL)
5783 te = got_object_tree_get_last_entry(s->tree);
5784 else
5785 te = got_tree_entry_get_prev(s->tree,
5786 s->selected_entry);
5788 } else {
5789 if (s->selected_entry)
5790 te = s->selected_entry;
5791 else if (view->searching == TOG_SEARCH_FORWARD)
5792 te = got_object_tree_get_first_entry(s->tree);
5793 else
5794 te = got_object_tree_get_last_entry(s->tree);
5797 while (1) {
5798 if (te == NULL) {
5799 if (s->matched_entry == NULL) {
5800 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5801 return NULL;
5803 if (view->searching == TOG_SEARCH_FORWARD)
5804 te = got_object_tree_get_first_entry(s->tree);
5805 else
5806 te = got_object_tree_get_last_entry(s->tree);
5809 if (match_tree_entry(te, &view->regex)) {
5810 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5811 s->matched_entry = te;
5812 break;
5815 if (view->searching == TOG_SEARCH_FORWARD)
5816 te = got_tree_entry_get_next(s->tree, te);
5817 else
5818 te = got_tree_entry_get_prev(s->tree, te);
5821 if (s->matched_entry) {
5822 s->first_displayed_entry = s->matched_entry;
5823 s->selected = 0;
5826 return NULL;
5829 static const struct got_error *
5830 show_tree_view(struct tog_view *view)
5832 const struct got_error *err = NULL;
5833 struct tog_tree_view_state *s = &view->state.tree;
5834 char *parent_path;
5836 err = tree_entry_path(&parent_path, &s->parents, NULL);
5837 if (err)
5838 return err;
5840 err = draw_tree_entries(view, parent_path);
5841 free(parent_path);
5843 view_vborder(view);
5844 return err;
5847 static const struct got_error *
5848 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5850 const struct got_error *err = NULL;
5851 struct tog_tree_view_state *s = &view->state.tree;
5852 struct tog_view *log_view, *ref_view;
5853 struct got_tree_entry *te;
5854 int begin_x = 0, n, nscroll = view->nlines - 3;
5856 switch (ch) {
5857 case 'i':
5858 s->show_ids = !s->show_ids;
5859 break;
5860 case 'l':
5861 if (!s->selected_entry)
5862 break;
5863 if (view_is_parent_view(view))
5864 begin_x = view_split_begin_x(view->begin_x);
5865 err = log_selected_tree_entry(&log_view, begin_x, s);
5866 view->focussed = 0;
5867 log_view->focussed = 1;
5868 if (view_is_parent_view(view)) {
5869 err = view_close_child(view);
5870 if (err)
5871 return err;
5872 err = view_set_child(view, log_view);
5873 if (err)
5874 return err;
5875 view->focus_child = 1;
5876 } else
5877 *new_view = log_view;
5878 break;
5879 case 'r':
5880 if (view_is_parent_view(view))
5881 begin_x = view_split_begin_x(view->begin_x);
5882 ref_view = view_open(view->nlines, view->ncols,
5883 view->begin_y, begin_x, TOG_VIEW_REF);
5884 if (ref_view == NULL)
5885 return got_error_from_errno("view_open");
5886 err = open_ref_view(ref_view, s->repo);
5887 if (err) {
5888 view_close(ref_view);
5889 return err;
5891 view->focussed = 0;
5892 ref_view->focussed = 1;
5893 if (view_is_parent_view(view)) {
5894 err = view_close_child(view);
5895 if (err)
5896 return err;
5897 err = view_set_child(view, ref_view);
5898 if (err)
5899 return err;
5900 view->focus_child = 1;
5901 } else
5902 *new_view = ref_view;
5903 break;
5904 case 'g':
5905 case KEY_HOME:
5906 s->selected = 0;
5907 if (s->tree == s->root)
5908 s->first_displayed_entry =
5909 got_object_tree_get_first_entry(s->tree);
5910 else
5911 s->first_displayed_entry = NULL;
5912 break;
5913 case 'G':
5914 case KEY_END:
5915 s->selected = 0;
5916 te = got_object_tree_get_last_entry(s->tree);
5917 for (n = 0; n < view->nlines - 3; n++) {
5918 if (te == NULL) {
5919 if(s->tree != s->root) {
5920 s->first_displayed_entry = NULL;
5921 n++;
5923 break;
5925 s->first_displayed_entry = te;
5926 te = got_tree_entry_get_prev(s->tree, te);
5928 if (n > 0)
5929 s->selected = n - 1;
5930 break;
5931 case 'k':
5932 case KEY_UP:
5933 case CTRL('p'):
5934 if (s->selected > 0) {
5935 s->selected--;
5936 break;
5938 tree_scroll_up(s, 1);
5939 break;
5940 case CTRL('u'):
5941 case 'u':
5942 nscroll /= 2;
5943 /* FALL THROUGH */
5944 case KEY_PPAGE:
5945 case CTRL('b'):
5946 if (s->tree == s->root) {
5947 if (got_object_tree_get_first_entry(s->tree) ==
5948 s->first_displayed_entry)
5949 s->selected -= MIN(s->selected, nscroll);
5950 } else {
5951 if (s->first_displayed_entry == NULL)
5952 s->selected -= MIN(s->selected, nscroll);
5954 tree_scroll_up(s, MAX(0, nscroll));
5955 break;
5956 case 'j':
5957 case KEY_DOWN:
5958 case CTRL('n'):
5959 if (s->selected < s->ndisplayed - 1) {
5960 s->selected++;
5961 break;
5963 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5964 == NULL)
5965 /* can't scroll any further */
5966 break;
5967 tree_scroll_down(s, 1);
5968 break;
5969 case CTRL('d'):
5970 case 'd':
5971 nscroll /= 2;
5972 /* FALL THROUGH */
5973 case KEY_NPAGE:
5974 case CTRL('f'):
5975 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5976 == NULL) {
5977 /* can't scroll any further; move cursor down */
5978 if (s->selected < s->ndisplayed - 1)
5979 s->selected += MIN(nscroll,
5980 s->ndisplayed - s->selected - 1);
5981 break;
5983 tree_scroll_down(s, nscroll);
5984 break;
5985 case KEY_ENTER:
5986 case '\r':
5987 case KEY_BACKSPACE:
5988 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5989 struct tog_parent_tree *parent;
5990 /* user selected '..' */
5991 if (s->tree == s->root)
5992 break;
5993 parent = TAILQ_FIRST(&s->parents);
5994 TAILQ_REMOVE(&s->parents, parent,
5995 entry);
5996 got_object_tree_close(s->tree);
5997 s->tree = parent->tree;
5998 s->first_displayed_entry =
5999 parent->first_displayed_entry;
6000 s->selected_entry =
6001 parent->selected_entry;
6002 s->selected = parent->selected;
6003 free(parent);
6004 } else if (S_ISDIR(got_tree_entry_get_mode(
6005 s->selected_entry))) {
6006 struct got_tree_object *subtree;
6007 err = got_object_open_as_tree(&subtree, s->repo,
6008 got_tree_entry_get_id(s->selected_entry));
6009 if (err)
6010 break;
6011 err = tree_view_visit_subtree(s, subtree);
6012 if (err) {
6013 got_object_tree_close(subtree);
6014 break;
6016 } else if (S_ISREG(got_tree_entry_get_mode(
6017 s->selected_entry))) {
6018 struct tog_view *blame_view;
6019 int begin_x = view_is_parent_view(view) ?
6020 view_split_begin_x(view->begin_x) : 0;
6022 err = blame_tree_entry(&blame_view, begin_x,
6023 s->selected_entry, &s->parents,
6024 s->commit_id, s->repo);
6025 if (err)
6026 break;
6027 view->focussed = 0;
6028 blame_view->focussed = 1;
6029 if (view_is_parent_view(view)) {
6030 err = view_close_child(view);
6031 if (err)
6032 return err;
6033 err = view_set_child(view, blame_view);
6034 if (err)
6035 return err;
6036 view->focus_child = 1;
6037 } else
6038 *new_view = blame_view;
6040 break;
6041 case KEY_RESIZE:
6042 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6043 s->selected = view->nlines - 4;
6044 break;
6045 default:
6046 break;
6049 return err;
6052 __dead static void
6053 usage_tree(void)
6055 endwin();
6056 fprintf(stderr,
6057 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6058 getprogname());
6059 exit(1);
6062 static const struct got_error *
6063 cmd_tree(int argc, char *argv[])
6065 const struct got_error *error;
6066 struct got_repository *repo = NULL;
6067 struct got_worktree *worktree = NULL;
6068 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6069 struct got_object_id *commit_id = NULL;
6070 struct got_commit_object *commit = NULL;
6071 const char *commit_id_arg = NULL;
6072 char *label = NULL;
6073 struct got_reference *ref = NULL;
6074 const char *head_ref_name = NULL;
6075 int ch;
6076 struct tog_view *view;
6077 int *pack_fds = NULL;
6079 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6080 switch (ch) {
6081 case 'c':
6082 commit_id_arg = optarg;
6083 break;
6084 case 'r':
6085 repo_path = realpath(optarg, NULL);
6086 if (repo_path == NULL)
6087 return got_error_from_errno2("realpath",
6088 optarg);
6089 break;
6090 default:
6091 usage_tree();
6092 /* NOTREACHED */
6096 argc -= optind;
6097 argv += optind;
6099 if (argc > 1)
6100 usage_tree();
6102 error = got_repo_pack_fds_open(&pack_fds);
6103 if (error != NULL)
6104 goto done;
6106 if (repo_path == NULL) {
6107 cwd = getcwd(NULL, 0);
6108 if (cwd == NULL)
6109 return got_error_from_errno("getcwd");
6110 error = got_worktree_open(&worktree, cwd);
6111 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6112 goto done;
6113 if (worktree)
6114 repo_path =
6115 strdup(got_worktree_get_repo_path(worktree));
6116 else
6117 repo_path = strdup(cwd);
6118 if (repo_path == NULL) {
6119 error = got_error_from_errno("strdup");
6120 goto done;
6124 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6125 if (error != NULL)
6126 goto done;
6128 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6129 repo, worktree);
6130 if (error)
6131 goto done;
6133 init_curses();
6135 error = apply_unveil(got_repo_get_path(repo), NULL);
6136 if (error)
6137 goto done;
6139 error = tog_load_refs(repo, 0);
6140 if (error)
6141 goto done;
6143 if (commit_id_arg == NULL) {
6144 error = got_repo_match_object_id(&commit_id, &label,
6145 worktree ? got_worktree_get_head_ref_name(worktree) :
6146 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6147 if (error)
6148 goto done;
6149 head_ref_name = label;
6150 } else {
6151 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6152 if (error == NULL)
6153 head_ref_name = got_ref_get_name(ref);
6154 else if (error->code != GOT_ERR_NOT_REF)
6155 goto done;
6156 error = got_repo_match_object_id(&commit_id, NULL,
6157 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6158 if (error)
6159 goto done;
6162 error = got_object_open_as_commit(&commit, repo, commit_id);
6163 if (error)
6164 goto done;
6166 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6167 if (view == NULL) {
6168 error = got_error_from_errno("view_open");
6169 goto done;
6171 error = open_tree_view(view, commit_id, head_ref_name, repo);
6172 if (error)
6173 goto done;
6174 if (!got_path_is_root_dir(in_repo_path)) {
6175 error = tree_view_walk_path(&view->state.tree, commit,
6176 in_repo_path);
6177 if (error)
6178 goto done;
6181 if (worktree) {
6182 /* Release work tree lock. */
6183 got_worktree_close(worktree);
6184 worktree = NULL;
6186 error = view_loop(view);
6187 done:
6188 free(repo_path);
6189 free(cwd);
6190 free(commit_id);
6191 free(label);
6192 if (ref)
6193 got_ref_close(ref);
6194 if (repo) {
6195 const struct got_error *close_err = got_repo_close(repo);
6196 if (error == NULL)
6197 error = close_err;
6199 if (pack_fds) {
6200 const struct got_error *pack_err =
6201 got_repo_pack_fds_close(pack_fds);
6202 if (error == NULL)
6203 error = pack_err;
6205 tog_free_refs();
6206 return error;
6209 static const struct got_error *
6210 ref_view_load_refs(struct tog_ref_view_state *s)
6212 struct got_reflist_entry *sre;
6213 struct tog_reflist_entry *re;
6215 s->nrefs = 0;
6216 TAILQ_FOREACH(sre, &tog_refs, entry) {
6217 if (strncmp(got_ref_get_name(sre->ref),
6218 "refs/got/", 9) == 0 &&
6219 strncmp(got_ref_get_name(sre->ref),
6220 "refs/got/backup/", 16) != 0)
6221 continue;
6223 re = malloc(sizeof(*re));
6224 if (re == NULL)
6225 return got_error_from_errno("malloc");
6227 re->ref = got_ref_dup(sre->ref);
6228 if (re->ref == NULL)
6229 return got_error_from_errno("got_ref_dup");
6230 re->idx = s->nrefs++;
6231 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6234 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6235 return NULL;
6238 void
6239 ref_view_free_refs(struct tog_ref_view_state *s)
6241 struct tog_reflist_entry *re;
6243 while (!TAILQ_EMPTY(&s->refs)) {
6244 re = TAILQ_FIRST(&s->refs);
6245 TAILQ_REMOVE(&s->refs, re, entry);
6246 got_ref_close(re->ref);
6247 free(re);
6251 static const struct got_error *
6252 open_ref_view(struct tog_view *view, struct got_repository *repo)
6254 const struct got_error *err = NULL;
6255 struct tog_ref_view_state *s = &view->state.ref;
6257 s->selected_entry = 0;
6258 s->repo = repo;
6260 TAILQ_INIT(&s->refs);
6261 STAILQ_INIT(&s->colors);
6263 err = ref_view_load_refs(s);
6264 if (err)
6265 return err;
6267 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6268 err = add_color(&s->colors, "^refs/heads/",
6269 TOG_COLOR_REFS_HEADS,
6270 get_color_value("TOG_COLOR_REFS_HEADS"));
6271 if (err)
6272 goto done;
6274 err = add_color(&s->colors, "^refs/tags/",
6275 TOG_COLOR_REFS_TAGS,
6276 get_color_value("TOG_COLOR_REFS_TAGS"));
6277 if (err)
6278 goto done;
6280 err = add_color(&s->colors, "^refs/remotes/",
6281 TOG_COLOR_REFS_REMOTES,
6282 get_color_value("TOG_COLOR_REFS_REMOTES"));
6283 if (err)
6284 goto done;
6286 err = add_color(&s->colors, "^refs/got/backup/",
6287 TOG_COLOR_REFS_BACKUP,
6288 get_color_value("TOG_COLOR_REFS_BACKUP"));
6289 if (err)
6290 goto done;
6293 view->show = show_ref_view;
6294 view->input = input_ref_view;
6295 view->close = close_ref_view;
6296 view->search_start = search_start_ref_view;
6297 view->search_next = search_next_ref_view;
6298 done:
6299 if (err)
6300 free_colors(&s->colors);
6301 return err;
6304 static const struct got_error *
6305 close_ref_view(struct tog_view *view)
6307 struct tog_ref_view_state *s = &view->state.ref;
6309 ref_view_free_refs(s);
6310 free_colors(&s->colors);
6312 return NULL;
6315 static const struct got_error *
6316 resolve_reflist_entry(struct got_object_id **commit_id,
6317 struct tog_reflist_entry *re, struct got_repository *repo)
6319 const struct got_error *err = NULL;
6320 struct got_object_id *obj_id;
6321 struct got_tag_object *tag = NULL;
6322 int obj_type;
6324 *commit_id = NULL;
6326 err = got_ref_resolve(&obj_id, repo, re->ref);
6327 if (err)
6328 return err;
6330 err = got_object_get_type(&obj_type, repo, obj_id);
6331 if (err)
6332 goto done;
6334 switch (obj_type) {
6335 case GOT_OBJ_TYPE_COMMIT:
6336 *commit_id = obj_id;
6337 break;
6338 case GOT_OBJ_TYPE_TAG:
6339 err = got_object_open_as_tag(&tag, repo, obj_id);
6340 if (err)
6341 goto done;
6342 free(obj_id);
6343 err = got_object_get_type(&obj_type, repo,
6344 got_object_tag_get_object_id(tag));
6345 if (err)
6346 goto done;
6347 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6348 err = got_error(GOT_ERR_OBJ_TYPE);
6349 goto done;
6351 *commit_id = got_object_id_dup(
6352 got_object_tag_get_object_id(tag));
6353 if (*commit_id == NULL) {
6354 err = got_error_from_errno("got_object_id_dup");
6355 goto done;
6357 break;
6358 default:
6359 err = got_error(GOT_ERR_OBJ_TYPE);
6360 break;
6363 done:
6364 if (tag)
6365 got_object_tag_close(tag);
6366 if (err) {
6367 free(*commit_id);
6368 *commit_id = NULL;
6370 return err;
6373 static const struct got_error *
6374 log_ref_entry(struct tog_view **new_view, int begin_x,
6375 struct tog_reflist_entry *re, struct got_repository *repo)
6377 struct tog_view *log_view;
6378 const struct got_error *err = NULL;
6379 struct got_object_id *commit_id = NULL;
6381 *new_view = NULL;
6383 err = resolve_reflist_entry(&commit_id, re, repo);
6384 if (err) {
6385 if (err->code != GOT_ERR_OBJ_TYPE)
6386 return err;
6387 else
6388 return NULL;
6391 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6392 if (log_view == NULL) {
6393 err = got_error_from_errno("view_open");
6394 goto done;
6397 err = open_log_view(log_view, commit_id, repo,
6398 got_ref_get_name(re->ref), "", 0);
6399 done:
6400 if (err)
6401 view_close(log_view);
6402 else
6403 *new_view = log_view;
6404 free(commit_id);
6405 return err;
6408 static void
6409 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6411 struct tog_reflist_entry *re;
6412 int i = 0;
6414 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6415 return;
6417 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6418 while (i++ < maxscroll) {
6419 if (re == NULL)
6420 break;
6421 s->first_displayed_entry = re;
6422 re = TAILQ_PREV(re, tog_reflist_head, entry);
6426 static void
6427 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6429 struct tog_reflist_entry *next, *last;
6430 int n = 0;
6432 if (s->first_displayed_entry)
6433 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6434 else
6435 next = TAILQ_FIRST(&s->refs);
6437 last = s->last_displayed_entry;
6438 while (next && last && n++ < maxscroll) {
6439 last = TAILQ_NEXT(last, entry);
6440 if (last) {
6441 s->first_displayed_entry = next;
6442 next = TAILQ_NEXT(next, entry);
6447 static const struct got_error *
6448 search_start_ref_view(struct tog_view *view)
6450 struct tog_ref_view_state *s = &view->state.ref;
6452 s->matched_entry = NULL;
6453 return NULL;
6456 static int
6457 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6459 regmatch_t regmatch;
6461 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6462 0) == 0;
6465 static const struct got_error *
6466 search_next_ref_view(struct tog_view *view)
6468 struct tog_ref_view_state *s = &view->state.ref;
6469 struct tog_reflist_entry *re = NULL;
6471 if (!view->searching) {
6472 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6473 return NULL;
6476 if (s->matched_entry) {
6477 if (view->searching == TOG_SEARCH_FORWARD) {
6478 if (s->selected_entry)
6479 re = TAILQ_NEXT(s->selected_entry, entry);
6480 else
6481 re = TAILQ_PREV(s->selected_entry,
6482 tog_reflist_head, entry);
6483 } else {
6484 if (s->selected_entry == NULL)
6485 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6486 else
6487 re = TAILQ_PREV(s->selected_entry,
6488 tog_reflist_head, entry);
6490 } else {
6491 if (s->selected_entry)
6492 re = s->selected_entry;
6493 else if (view->searching == TOG_SEARCH_FORWARD)
6494 re = TAILQ_FIRST(&s->refs);
6495 else
6496 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6499 while (1) {
6500 if (re == NULL) {
6501 if (s->matched_entry == NULL) {
6502 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6503 return NULL;
6505 if (view->searching == TOG_SEARCH_FORWARD)
6506 re = TAILQ_FIRST(&s->refs);
6507 else
6508 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6511 if (match_reflist_entry(re, &view->regex)) {
6512 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6513 s->matched_entry = re;
6514 break;
6517 if (view->searching == TOG_SEARCH_FORWARD)
6518 re = TAILQ_NEXT(re, entry);
6519 else
6520 re = TAILQ_PREV(re, tog_reflist_head, entry);
6523 if (s->matched_entry) {
6524 s->first_displayed_entry = s->matched_entry;
6525 s->selected = 0;
6528 return NULL;
6531 static const struct got_error *
6532 show_ref_view(struct tog_view *view)
6534 const struct got_error *err = NULL;
6535 struct tog_ref_view_state *s = &view->state.ref;
6536 struct tog_reflist_entry *re;
6537 char *line = NULL;
6538 wchar_t *wline;
6539 struct tog_color *tc;
6540 int width, n;
6541 int limit = view->nlines;
6543 werase(view->window);
6545 s->ndisplayed = 0;
6547 if (limit == 0)
6548 return NULL;
6550 re = s->first_displayed_entry;
6552 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6553 s->nrefs) == -1)
6554 return got_error_from_errno("asprintf");
6556 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6557 if (err) {
6558 free(line);
6559 return err;
6561 if (view_needs_focus_indication(view))
6562 wstandout(view->window);
6563 waddwstr(view->window, wline);
6564 if (view_needs_focus_indication(view))
6565 wstandend(view->window);
6566 free(wline);
6567 wline = NULL;
6568 free(line);
6569 line = NULL;
6570 if (width < view->ncols - 1)
6571 waddch(view->window, '\n');
6572 if (--limit <= 0)
6573 return NULL;
6575 n = 0;
6576 while (re && limit > 0) {
6577 char *line = NULL;
6578 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6580 if (s->show_date) {
6581 struct got_commit_object *ci;
6582 struct got_tag_object *tag;
6583 struct got_object_id *id;
6584 struct tm tm;
6585 time_t t;
6587 err = got_ref_resolve(&id, s->repo, re->ref);
6588 if (err)
6589 return err;
6590 err = got_object_open_as_tag(&tag, s->repo, id);
6591 if (err) {
6592 if (err->code != GOT_ERR_OBJ_TYPE) {
6593 free(id);
6594 return err;
6596 err = got_object_open_as_commit(&ci, s->repo,
6597 id);
6598 if (err) {
6599 free(id);
6600 return err;
6602 t = got_object_commit_get_committer_time(ci);
6603 got_object_commit_close(ci);
6604 } else {
6605 t = got_object_tag_get_tagger_time(tag);
6606 got_object_tag_close(tag);
6608 free(id);
6609 if (gmtime_r(&t, &tm) == NULL)
6610 return got_error_from_errno("gmtime_r");
6611 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6612 return got_error(GOT_ERR_NO_SPACE);
6614 if (got_ref_is_symbolic(re->ref)) {
6615 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6616 ymd : "", got_ref_get_name(re->ref),
6617 got_ref_get_symref_target(re->ref)) == -1)
6618 return got_error_from_errno("asprintf");
6619 } else if (s->show_ids) {
6620 struct got_object_id *id;
6621 char *id_str;
6622 err = got_ref_resolve(&id, s->repo, re->ref);
6623 if (err)
6624 return err;
6625 err = got_object_id_str(&id_str, id);
6626 if (err) {
6627 free(id);
6628 return err;
6630 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6631 got_ref_get_name(re->ref), id_str) == -1) {
6632 err = got_error_from_errno("asprintf");
6633 free(id);
6634 free(id_str);
6635 return err;
6637 free(id);
6638 free(id_str);
6639 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6640 got_ref_get_name(re->ref)) == -1)
6641 return got_error_from_errno("asprintf");
6643 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6644 0, 0);
6645 if (err) {
6646 free(line);
6647 return err;
6649 if (n == s->selected) {
6650 if (view->focussed)
6651 wstandout(view->window);
6652 s->selected_entry = re;
6654 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6655 if (tc)
6656 wattr_on(view->window,
6657 COLOR_PAIR(tc->colorpair), NULL);
6658 waddwstr(view->window, wline);
6659 if (tc)
6660 wattr_off(view->window,
6661 COLOR_PAIR(tc->colorpair), NULL);
6662 if (width < view->ncols - 1)
6663 waddch(view->window, '\n');
6664 if (n == s->selected && view->focussed)
6665 wstandend(view->window);
6666 free(line);
6667 free(wline);
6668 wline = NULL;
6669 n++;
6670 s->ndisplayed++;
6671 s->last_displayed_entry = re;
6673 limit--;
6674 re = TAILQ_NEXT(re, entry);
6677 view_vborder(view);
6678 return err;
6681 static const struct got_error *
6682 browse_ref_tree(struct tog_view **new_view, int begin_x,
6683 struct tog_reflist_entry *re, struct got_repository *repo)
6685 const struct got_error *err = NULL;
6686 struct got_object_id *commit_id = NULL;
6687 struct tog_view *tree_view;
6689 *new_view = NULL;
6691 err = resolve_reflist_entry(&commit_id, re, repo);
6692 if (err) {
6693 if (err->code != GOT_ERR_OBJ_TYPE)
6694 return err;
6695 else
6696 return NULL;
6700 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6701 if (tree_view == NULL) {
6702 err = got_error_from_errno("view_open");
6703 goto done;
6706 err = open_tree_view(tree_view, commit_id,
6707 got_ref_get_name(re->ref), repo);
6708 if (err)
6709 goto done;
6711 *new_view = tree_view;
6712 done:
6713 free(commit_id);
6714 return err;
6716 static const struct got_error *
6717 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6719 const struct got_error *err = NULL;
6720 struct tog_ref_view_state *s = &view->state.ref;
6721 struct tog_view *log_view, *tree_view;
6722 struct tog_reflist_entry *re;
6723 int begin_x = 0, n, nscroll = view->nlines - 1;
6725 switch (ch) {
6726 case 'i':
6727 s->show_ids = !s->show_ids;
6728 break;
6729 case 'm':
6730 s->show_date = !s->show_date;
6731 break;
6732 case 'o':
6733 s->sort_by_date = !s->sort_by_date;
6734 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6735 got_ref_cmp_by_commit_timestamp_descending :
6736 tog_ref_cmp_by_name, s->repo);
6737 if (err)
6738 break;
6739 got_reflist_object_id_map_free(tog_refs_idmap);
6740 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6741 &tog_refs, s->repo);
6742 if (err)
6743 break;
6744 ref_view_free_refs(s);
6745 err = ref_view_load_refs(s);
6746 break;
6747 case KEY_ENTER:
6748 case '\r':
6749 if (!s->selected_entry)
6750 break;
6751 if (view_is_parent_view(view))
6752 begin_x = view_split_begin_x(view->begin_x);
6753 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6754 s->repo);
6755 view->focussed = 0;
6756 log_view->focussed = 1;
6757 if (view_is_parent_view(view)) {
6758 err = view_close_child(view);
6759 if (err)
6760 return err;
6761 err = view_set_child(view, log_view);
6762 if (err)
6763 return err;
6764 view->focus_child = 1;
6765 } else
6766 *new_view = log_view;
6767 break;
6768 case 't':
6769 if (!s->selected_entry)
6770 break;
6771 if (view_is_parent_view(view))
6772 begin_x = view_split_begin_x(view->begin_x);
6773 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6774 s->repo);
6775 if (err || tree_view == NULL)
6776 break;
6777 view->focussed = 0;
6778 tree_view->focussed = 1;
6779 if (view_is_parent_view(view)) {
6780 err = view_close_child(view);
6781 if (err)
6782 return err;
6783 err = view_set_child(view, tree_view);
6784 if (err)
6785 return err;
6786 view->focus_child = 1;
6787 } else
6788 *new_view = tree_view;
6789 break;
6790 case 'g':
6791 case KEY_HOME:
6792 s->selected = 0;
6793 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6794 break;
6795 case 'G':
6796 case KEY_END:
6797 s->selected = 0;
6798 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6799 for (n = 0; n < view->nlines - 1; n++) {
6800 if (re == NULL)
6801 break;
6802 s->first_displayed_entry = re;
6803 re = TAILQ_PREV(re, tog_reflist_head, entry);
6805 if (n > 0)
6806 s->selected = n - 1;
6807 break;
6808 case 'k':
6809 case KEY_UP:
6810 case CTRL('p'):
6811 if (s->selected > 0) {
6812 s->selected--;
6813 break;
6815 ref_scroll_up(s, 1);
6816 break;
6817 case CTRL('u'):
6818 case 'u':
6819 nscroll /= 2;
6820 /* FALL THROUGH */
6821 case KEY_PPAGE:
6822 case CTRL('b'):
6823 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6824 s->selected -= MIN(nscroll, s->selected);
6825 ref_scroll_up(s, MAX(0, nscroll));
6826 break;
6827 case 'j':
6828 case KEY_DOWN:
6829 case CTRL('n'):
6830 if (s->selected < s->ndisplayed - 1) {
6831 s->selected++;
6832 break;
6834 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6835 /* can't scroll any further */
6836 break;
6837 ref_scroll_down(s, 1);
6838 break;
6839 case CTRL('d'):
6840 case 'd':
6841 nscroll /= 2;
6842 /* FALL THROUGH */
6843 case KEY_NPAGE:
6844 case CTRL('f'):
6845 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6846 /* can't scroll any further; move cursor down */
6847 if (s->selected < s->ndisplayed - 1)
6848 s->selected += MIN(nscroll,
6849 s->ndisplayed - s->selected - 1);
6850 break;
6852 ref_scroll_down(s, nscroll);
6853 break;
6854 case CTRL('l'):
6855 tog_free_refs();
6856 err = tog_load_refs(s->repo, s->sort_by_date);
6857 if (err)
6858 break;
6859 ref_view_free_refs(s);
6860 err = ref_view_load_refs(s);
6861 break;
6862 case KEY_RESIZE:
6863 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6864 s->selected = view->nlines - 2;
6865 break;
6866 default:
6867 break;
6870 return err;
6873 __dead static void
6874 usage_ref(void)
6876 endwin();
6877 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6878 getprogname());
6879 exit(1);
6882 static const struct got_error *
6883 cmd_ref(int argc, char *argv[])
6885 const struct got_error *error;
6886 struct got_repository *repo = NULL;
6887 struct got_worktree *worktree = NULL;
6888 char *cwd = NULL, *repo_path = NULL;
6889 int ch;
6890 struct tog_view *view;
6891 int *pack_fds = NULL;
6893 while ((ch = getopt(argc, argv, "r:")) != -1) {
6894 switch (ch) {
6895 case 'r':
6896 repo_path = realpath(optarg, NULL);
6897 if (repo_path == NULL)
6898 return got_error_from_errno2("realpath",
6899 optarg);
6900 break;
6901 default:
6902 usage_ref();
6903 /* NOTREACHED */
6907 argc -= optind;
6908 argv += optind;
6910 if (argc > 1)
6911 usage_ref();
6913 error = got_repo_pack_fds_open(&pack_fds);
6914 if (error != NULL)
6915 goto done;
6917 if (repo_path == NULL) {
6918 cwd = getcwd(NULL, 0);
6919 if (cwd == NULL)
6920 return got_error_from_errno("getcwd");
6921 error = got_worktree_open(&worktree, cwd);
6922 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6923 goto done;
6924 if (worktree)
6925 repo_path =
6926 strdup(got_worktree_get_repo_path(worktree));
6927 else
6928 repo_path = strdup(cwd);
6929 if (repo_path == NULL) {
6930 error = got_error_from_errno("strdup");
6931 goto done;
6935 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6936 if (error != NULL)
6937 goto done;
6939 init_curses();
6941 error = apply_unveil(got_repo_get_path(repo), NULL);
6942 if (error)
6943 goto done;
6945 error = tog_load_refs(repo, 0);
6946 if (error)
6947 goto done;
6949 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6950 if (view == NULL) {
6951 error = got_error_from_errno("view_open");
6952 goto done;
6955 error = open_ref_view(view, repo);
6956 if (error)
6957 goto done;
6959 if (worktree) {
6960 /* Release work tree lock. */
6961 got_worktree_close(worktree);
6962 worktree = NULL;
6964 error = view_loop(view);
6965 done:
6966 free(repo_path);
6967 free(cwd);
6968 if (repo) {
6969 const struct got_error *close_err = got_repo_close(repo);
6970 if (close_err)
6971 error = close_err;
6973 if (pack_fds) {
6974 const struct got_error *pack_err =
6975 got_repo_pack_fds_close(pack_fds);
6976 if (error == NULL)
6977 error = pack_err;
6979 tog_free_refs();
6980 return error;
6983 static void
6984 list_commands(FILE *fp)
6986 size_t i;
6988 fprintf(fp, "commands:");
6989 for (i = 0; i < nitems(tog_commands); i++) {
6990 const struct tog_cmd *cmd = &tog_commands[i];
6991 fprintf(fp, " %s", cmd->name);
6993 fputc('\n', fp);
6996 __dead static void
6997 usage(int hflag, int status)
6999 FILE *fp = (status == 0) ? stdout : stderr;
7001 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7002 getprogname());
7003 if (hflag) {
7004 fprintf(fp, "lazy usage: %s path\n", getprogname());
7005 list_commands(fp);
7007 exit(status);
7010 static char **
7011 make_argv(int argc, ...)
7013 va_list ap;
7014 char **argv;
7015 int i;
7017 va_start(ap, argc);
7019 argv = calloc(argc, sizeof(char *));
7020 if (argv == NULL)
7021 err(1, "calloc");
7022 for (i = 0; i < argc; i++) {
7023 argv[i] = strdup(va_arg(ap, char *));
7024 if (argv[i] == NULL)
7025 err(1, "strdup");
7028 va_end(ap);
7029 return argv;
7033 * Try to convert 'tog path' into a 'tog log path' command.
7034 * The user could simply have mistyped the command rather than knowingly
7035 * provided a path. So check whether argv[0] can in fact be resolved
7036 * to a path in the HEAD commit and print a special error if not.
7037 * This hack is for mpi@ <3
7039 static const struct got_error *
7040 tog_log_with_path(int argc, char *argv[])
7042 const struct got_error *error = NULL, *close_err;
7043 const struct tog_cmd *cmd = NULL;
7044 struct got_repository *repo = NULL;
7045 struct got_worktree *worktree = NULL;
7046 struct got_object_id *commit_id = NULL, *id = NULL;
7047 struct got_commit_object *commit = NULL;
7048 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7049 char *commit_id_str = NULL, **cmd_argv = NULL;
7050 int *pack_fds = NULL;
7052 cwd = getcwd(NULL, 0);
7053 if (cwd == NULL)
7054 return got_error_from_errno("getcwd");
7056 error = got_repo_pack_fds_open(&pack_fds);
7057 if (error != NULL)
7058 goto done;
7060 error = got_worktree_open(&worktree, cwd);
7061 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7062 goto done;
7064 if (worktree)
7065 repo_path = strdup(got_worktree_get_repo_path(worktree));
7066 else
7067 repo_path = strdup(cwd);
7068 if (repo_path == NULL) {
7069 error = got_error_from_errno("strdup");
7070 goto done;
7073 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7074 if (error != NULL)
7075 goto done;
7077 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7078 repo, worktree);
7079 if (error)
7080 goto done;
7082 error = tog_load_refs(repo, 0);
7083 if (error)
7084 goto done;
7085 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7086 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7087 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7088 if (error)
7089 goto done;
7091 if (worktree) {
7092 got_worktree_close(worktree);
7093 worktree = NULL;
7096 error = got_object_open_as_commit(&commit, repo, commit_id);
7097 if (error)
7098 goto done;
7100 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7101 if (error) {
7102 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7103 goto done;
7104 fprintf(stderr, "%s: '%s' is no known command or path\n",
7105 getprogname(), argv[0]);
7106 usage(1, 1);
7107 /* not reached */
7110 close_err = got_repo_close(repo);
7111 if (error == NULL)
7112 error = close_err;
7113 repo = NULL;
7115 error = got_object_id_str(&commit_id_str, commit_id);
7116 if (error)
7117 goto done;
7119 cmd = &tog_commands[0]; /* log */
7120 argc = 4;
7121 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7122 error = cmd->cmd_main(argc, cmd_argv);
7123 done:
7124 if (repo) {
7125 close_err = got_repo_close(repo);
7126 if (error == NULL)
7127 error = close_err;
7129 if (commit)
7130 got_object_commit_close(commit);
7131 if (worktree)
7132 got_worktree_close(worktree);
7133 if (pack_fds) {
7134 const struct got_error *pack_err =
7135 got_repo_pack_fds_close(pack_fds);
7136 if (error == NULL)
7137 error = pack_err;
7139 free(id);
7140 free(commit_id_str);
7141 free(commit_id);
7142 free(cwd);
7143 free(repo_path);
7144 free(in_repo_path);
7145 if (cmd_argv) {
7146 int i;
7147 for (i = 0; i < argc; i++)
7148 free(cmd_argv[i]);
7149 free(cmd_argv);
7151 tog_free_refs();
7152 return error;
7155 int
7156 main(int argc, char *argv[])
7158 const struct got_error *error = NULL;
7159 const struct tog_cmd *cmd = NULL;
7160 int ch, hflag = 0, Vflag = 0;
7161 char **cmd_argv = NULL;
7162 static const struct option longopts[] = {
7163 { "version", no_argument, NULL, 'V' },
7164 { NULL, 0, NULL, 0}
7167 setlocale(LC_CTYPE, "");
7169 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7170 switch (ch) {
7171 case 'h':
7172 hflag = 1;
7173 break;
7174 case 'V':
7175 Vflag = 1;
7176 break;
7177 default:
7178 usage(hflag, 1);
7179 /* NOTREACHED */
7183 argc -= optind;
7184 argv += optind;
7185 optind = 1;
7186 optreset = 1;
7188 if (Vflag) {
7189 got_version_print_str();
7190 return 0;
7193 #ifndef PROFILE
7194 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7195 NULL) == -1)
7196 err(1, "pledge");
7197 #endif
7199 if (argc == 0) {
7200 if (hflag)
7201 usage(hflag, 0);
7202 /* Build an argument vector which runs a default command. */
7203 cmd = &tog_commands[0];
7204 argc = 1;
7205 cmd_argv = make_argv(argc, cmd->name);
7206 } else {
7207 size_t i;
7209 /* Did the user specify a command? */
7210 for (i = 0; i < nitems(tog_commands); i++) {
7211 if (strncmp(tog_commands[i].name, argv[0],
7212 strlen(argv[0])) == 0) {
7213 cmd = &tog_commands[i];
7214 break;
7219 if (cmd == NULL) {
7220 if (argc != 1)
7221 usage(0, 1);
7222 /* No command specified; try log with a path */
7223 error = tog_log_with_path(argc, argv);
7224 } else {
7225 if (hflag)
7226 cmd->cmd_usage();
7227 else
7228 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7231 endwin();
7232 putchar('\n');
7233 if (cmd_argv) {
7234 int i;
7235 for (i = 0; i < argc; i++)
7236 free(cmd_argv[i]);
7237 free(cmd_argv);
7240 if (error && error->code != GOT_ERR_CANCELLED)
7241 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7242 return 0;