Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #include <curses.h>
25 #include <panel.h>
26 #include <locale.h>
27 #include <sha1.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_diff.h"
50 #include "got_opentemp.h"
51 #include "got_utf8.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_path.h"
57 #include "got_worktree.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #ifndef MAX
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #endif
67 #define CTRL(x) ((x) & 0x1f)
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 struct tog_cmd {
74 const char *name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 };
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
98 };
100 enum tog_view_type {
101 TOG_VIEW_DIFF,
102 TOG_VIEW_LOG,
103 TOG_VIEW_BLAME,
104 TOG_VIEW_TREE,
105 TOG_VIEW_REF,
106 };
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
114 int idx;
115 };
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
118 int ncommits;
119 struct commit_queue_head head;
120 };
122 struct tog_color {
123 STAILQ_ENTRY(tog_color) entry;
124 regex_t regex;
125 short colorpair;
126 };
127 STAILQ_HEAD(tog_colors, tog_color);
129 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
130 static struct got_reflist_object_id_map *tog_refs_idmap;
132 static const struct got_error *
133 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
134 struct got_reference* re2)
136 const char *name1 = got_ref_get_name(re1);
137 const char *name2 = got_ref_get_name(re2);
138 int isbackup1, isbackup2;
140 /* Sort backup refs towards the bottom of the list. */
141 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
142 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
143 if (!isbackup1 && isbackup2) {
144 *cmp = -1;
145 return NULL;
146 } else if (isbackup1 && !isbackup2) {
147 *cmp = 1;
148 return NULL;
151 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
152 return NULL;
155 static const struct got_error *
156 tog_load_refs(struct got_repository *repo, int sort_by_date)
158 const struct got_error *err;
160 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
161 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
162 repo);
163 if (err)
164 return err;
166 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
167 repo);
170 static void
171 tog_free_refs(void)
173 if (tog_refs_idmap) {
174 got_reflist_object_id_map_free(tog_refs_idmap);
175 tog_refs_idmap = NULL;
177 got_ref_list_free(&tog_refs);
180 static const struct got_error *
181 add_color(struct tog_colors *colors, const char *pattern,
182 int idx, short color)
184 const struct got_error *err = NULL;
185 struct tog_color *tc;
186 int regerr = 0;
188 if (idx < 1 || idx > COLOR_PAIRS - 1)
189 return NULL;
191 init_pair(idx, color, -1);
193 tc = calloc(1, sizeof(*tc));
194 if (tc == NULL)
195 return got_error_from_errno("calloc");
196 regerr = regcomp(&tc->regex, pattern,
197 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
198 if (regerr) {
199 static char regerr_msg[512];
200 static char err_msg[512];
201 regerror(regerr, &tc->regex, regerr_msg,
202 sizeof(regerr_msg));
203 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
204 regerr_msg);
205 err = got_error_msg(GOT_ERR_REGEX, err_msg);
206 free(tc);
207 return err;
209 tc->colorpair = idx;
210 STAILQ_INSERT_HEAD(colors, tc, entry);
211 return NULL;
214 static void
215 free_colors(struct tog_colors *colors)
217 struct tog_color *tc;
219 while (!STAILQ_EMPTY(colors)) {
220 tc = STAILQ_FIRST(colors);
221 STAILQ_REMOVE_HEAD(colors, entry);
222 regfree(&tc->regex);
223 free(tc);
227 struct tog_color *
228 get_color(struct tog_colors *colors, int colorpair)
230 struct tog_color *tc = NULL;
232 STAILQ_FOREACH(tc, colors, entry) {
233 if (tc->colorpair == colorpair)
234 return tc;
237 return NULL;
240 static int
241 default_color_value(const char *envvar)
243 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
244 return COLOR_MAGENTA;
245 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
246 return COLOR_CYAN;
247 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
248 return COLOR_YELLOW;
249 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
250 return COLOR_GREEN;
251 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
254 return COLOR_MAGENTA;
255 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
256 return COLOR_CYAN;
257 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
258 return COLOR_GREEN;
259 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
260 return COLOR_GREEN;
261 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
262 return COLOR_CYAN;
263 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
264 return COLOR_YELLOW;
265 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
266 return COLOR_GREEN;
267 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
270 return COLOR_YELLOW;
271 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
272 return COLOR_CYAN;
274 return -1;
277 static int
278 get_color_value(const char *envvar)
280 const char *val = getenv(envvar);
282 if (val == NULL)
283 return default_color_value(envvar);
285 if (strcasecmp(val, "black") == 0)
286 return COLOR_BLACK;
287 if (strcasecmp(val, "red") == 0)
288 return COLOR_RED;
289 if (strcasecmp(val, "green") == 0)
290 return COLOR_GREEN;
291 if (strcasecmp(val, "yellow") == 0)
292 return COLOR_YELLOW;
293 if (strcasecmp(val, "blue") == 0)
294 return COLOR_BLUE;
295 if (strcasecmp(val, "magenta") == 0)
296 return COLOR_MAGENTA;
297 if (strcasecmp(val, "cyan") == 0)
298 return COLOR_CYAN;
299 if (strcasecmp(val, "white") == 0)
300 return COLOR_WHITE;
301 if (strcasecmp(val, "default") == 0)
302 return -1;
304 return default_color_value(envvar);
308 struct tog_diff_view_state {
309 struct got_object_id *id1, *id2;
310 const char *label1, *label2;
311 FILE *f, *f1, *f2;
312 int first_displayed_line;
313 int last_displayed_line;
314 int eof;
315 int diff_context;
316 int ignore_whitespace;
317 int force_text_diff;
318 struct got_repository *repo;
319 struct tog_colors colors;
320 size_t nlines;
321 off_t *line_offsets;
322 int matched_line;
323 int selected_line;
325 /* passed from log view; may be NULL */
326 struct tog_view *log_view;
327 };
329 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
331 struct tog_log_thread_args {
332 pthread_cond_t need_commits;
333 pthread_cond_t commit_loaded;
334 int commits_needed;
335 int load_all;
336 struct got_commit_graph *graph;
337 struct commit_queue *commits;
338 const char *in_repo_path;
339 struct got_object_id *start_id;
340 struct got_repository *repo;
341 int *pack_fds;
342 int log_complete;
343 sig_atomic_t *quit;
344 struct commit_queue_entry **first_displayed_entry;
345 struct commit_queue_entry **selected_entry;
346 int *searching;
347 int *search_next_done;
348 regex_t *regex;
349 };
351 struct tog_log_view_state {
352 struct commit_queue commits;
353 struct commit_queue_entry *first_displayed_entry;
354 struct commit_queue_entry *last_displayed_entry;
355 struct commit_queue_entry *selected_entry;
356 int selected;
357 char *in_repo_path;
358 char *head_ref_name;
359 int log_branches;
360 struct got_repository *repo;
361 struct got_object_id *start_id;
362 sig_atomic_t quit;
363 pthread_t thread;
364 struct tog_log_thread_args thread_args;
365 struct commit_queue_entry *matched_entry;
366 struct commit_queue_entry *search_entry;
367 struct tog_colors colors;
368 };
370 #define TOG_COLOR_DIFF_MINUS 1
371 #define TOG_COLOR_DIFF_PLUS 2
372 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
373 #define TOG_COLOR_DIFF_META 4
374 #define TOG_COLOR_TREE_SUBMODULE 5
375 #define TOG_COLOR_TREE_SYMLINK 6
376 #define TOG_COLOR_TREE_DIRECTORY 7
377 #define TOG_COLOR_TREE_EXECUTABLE 8
378 #define TOG_COLOR_COMMIT 9
379 #define TOG_COLOR_AUTHOR 10
380 #define TOG_COLOR_DATE 11
381 #define TOG_COLOR_REFS_HEADS 12
382 #define TOG_COLOR_REFS_TAGS 13
383 #define TOG_COLOR_REFS_REMOTES 14
384 #define TOG_COLOR_REFS_BACKUP 15
386 struct tog_blame_cb_args {
387 struct tog_blame_line *lines; /* one per line */
388 int nlines;
390 struct tog_view *view;
391 struct got_object_id *commit_id;
392 int *quit;
393 };
395 struct tog_blame_thread_args {
396 const char *path;
397 struct got_repository *repo;
398 struct tog_blame_cb_args *cb_args;
399 int *complete;
400 got_cancel_cb cancel_cb;
401 void *cancel_arg;
402 };
404 struct tog_blame {
405 FILE *f;
406 off_t filesize;
407 struct tog_blame_line *lines;
408 int nlines;
409 off_t *line_offsets;
410 pthread_t thread;
411 struct tog_blame_thread_args thread_args;
412 struct tog_blame_cb_args cb_args;
413 const char *path;
414 int *pack_fds;
415 };
417 struct tog_blame_view_state {
418 int first_displayed_line;
419 int last_displayed_line;
420 int selected_line;
421 int blame_complete;
422 int eof;
423 int done;
424 struct got_object_id_queue blamed_commits;
425 struct got_object_qid *blamed_commit;
426 char *path;
427 struct got_repository *repo;
428 struct got_object_id *commit_id;
429 struct tog_blame blame;
430 int matched_line;
431 struct tog_colors colors;
432 };
434 struct tog_parent_tree {
435 TAILQ_ENTRY(tog_parent_tree) entry;
436 struct got_tree_object *tree;
437 struct got_tree_entry *first_displayed_entry;
438 struct got_tree_entry *selected_entry;
439 int selected;
440 };
442 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
444 struct tog_tree_view_state {
445 char *tree_label;
446 struct got_object_id *commit_id;/* commit which this tree belongs to */
447 struct got_tree_object *root; /* the commit's root tree entry */
448 struct got_tree_object *tree; /* currently displayed (sub-)tree */
449 struct got_tree_entry *first_displayed_entry;
450 struct got_tree_entry *last_displayed_entry;
451 struct got_tree_entry *selected_entry;
452 int ndisplayed, selected, show_ids;
453 struct tog_parent_trees parents; /* parent trees of current sub-tree */
454 char *head_ref_name;
455 struct got_repository *repo;
456 struct got_tree_entry *matched_entry;
457 struct tog_colors colors;
458 };
460 struct tog_reflist_entry {
461 TAILQ_ENTRY(tog_reflist_entry) entry;
462 struct got_reference *ref;
463 int idx;
464 };
466 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
468 struct tog_ref_view_state {
469 struct tog_reflist_head refs;
470 struct tog_reflist_entry *first_displayed_entry;
471 struct tog_reflist_entry *last_displayed_entry;
472 struct tog_reflist_entry *selected_entry;
473 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
474 struct got_repository *repo;
475 struct tog_reflist_entry *matched_entry;
476 struct tog_colors colors;
477 };
479 /*
480 * We implement two types of views: parent views and child views.
482 * The 'Tab' key switches focus between a parent view and its child view.
483 * Child views are shown side-by-side to their parent view, provided
484 * there is enough screen estate.
486 * When a new view is opened from within a parent view, this new view
487 * becomes a child view of the parent view, replacing any existing child.
489 * When a new view is opened from within a child view, this new view
490 * becomes a parent view which will obscure the views below until the
491 * user quits the new parent view by typing 'q'.
493 * This list of views contains parent views only.
494 * Child views are only pointed to by their parent view.
495 */
496 TAILQ_HEAD(tog_view_list_head, tog_view);
498 struct tog_view {
499 TAILQ_ENTRY(tog_view) entry;
500 WINDOW *window;
501 PANEL *panel;
502 int nlines, ncols, begin_y, begin_x;
503 int maxx, x; /* max column and current start column */
504 int lines, cols; /* copies of LINES and COLS */
505 int focussed; /* Only set on one parent or child view at a time. */
506 int dying;
507 struct tog_view *parent;
508 struct tog_view *child;
510 /*
511 * This flag is initially set on parent views when a new child view
512 * is created. It gets toggled when the 'Tab' key switches focus
513 * between parent and child.
514 * The flag indicates whether focus should be passed on to our child
515 * view if this parent view gets picked for focus after another parent
516 * view was closed. This prevents child views from losing focus in such
517 * situations.
518 */
519 int focus_child;
521 /* type-specific state */
522 enum tog_view_type type;
523 union {
524 struct tog_diff_view_state diff;
525 struct tog_log_view_state log;
526 struct tog_blame_view_state blame;
527 struct tog_tree_view_state tree;
528 struct tog_ref_view_state ref;
529 } state;
531 const struct got_error *(*show)(struct tog_view *);
532 const struct got_error *(*input)(struct tog_view **,
533 struct tog_view *, int);
534 const struct got_error *(*close)(struct tog_view *);
536 const struct got_error *(*search_start)(struct tog_view *);
537 const struct got_error *(*search_next)(struct tog_view *);
538 int search_started;
539 int searching;
540 #define TOG_SEARCH_FORWARD 1
541 #define TOG_SEARCH_BACKWARD 2
542 int search_next_done;
543 #define TOG_SEARCH_HAVE_MORE 1
544 #define TOG_SEARCH_NO_MORE 2
545 #define TOG_SEARCH_HAVE_NONE 3
546 regex_t regex;
547 regmatch_t regmatch;
548 };
550 static const struct got_error *open_diff_view(struct tog_view *,
551 struct got_object_id *, struct got_object_id *,
552 const char *, const char *, int, int, int, struct tog_view *,
553 struct got_repository *);
554 static const struct got_error *show_diff_view(struct tog_view *);
555 static const struct got_error *input_diff_view(struct tog_view **,
556 struct tog_view *, int);
557 static const struct got_error* close_diff_view(struct tog_view *);
558 static const struct got_error *search_start_diff_view(struct tog_view *);
559 static const struct got_error *search_next_diff_view(struct tog_view *);
561 static const struct got_error *open_log_view(struct tog_view *,
562 struct got_object_id *, struct got_repository *,
563 const char *, const char *, int);
564 static const struct got_error * show_log_view(struct tog_view *);
565 static const struct got_error *input_log_view(struct tog_view **,
566 struct tog_view *, int);
567 static const struct got_error *close_log_view(struct tog_view *);
568 static const struct got_error *search_start_log_view(struct tog_view *);
569 static const struct got_error *search_next_log_view(struct tog_view *);
571 static const struct got_error *open_blame_view(struct tog_view *, char *,
572 struct got_object_id *, struct got_repository *);
573 static const struct got_error *show_blame_view(struct tog_view *);
574 static const struct got_error *input_blame_view(struct tog_view **,
575 struct tog_view *, int);
576 static const struct got_error *close_blame_view(struct tog_view *);
577 static const struct got_error *search_start_blame_view(struct tog_view *);
578 static const struct got_error *search_next_blame_view(struct tog_view *);
580 static const struct got_error *open_tree_view(struct tog_view *,
581 struct got_object_id *, const char *, struct got_repository *);
582 static const struct got_error *show_tree_view(struct tog_view *);
583 static const struct got_error *input_tree_view(struct tog_view **,
584 struct tog_view *, int);
585 static const struct got_error *close_tree_view(struct tog_view *);
586 static const struct got_error *search_start_tree_view(struct tog_view *);
587 static const struct got_error *search_next_tree_view(struct tog_view *);
589 static const struct got_error *open_ref_view(struct tog_view *,
590 struct got_repository *);
591 static const struct got_error *show_ref_view(struct tog_view *);
592 static const struct got_error *input_ref_view(struct tog_view **,
593 struct tog_view *, int);
594 static const struct got_error *close_ref_view(struct tog_view *);
595 static const struct got_error *search_start_ref_view(struct tog_view *);
596 static const struct got_error *search_next_ref_view(struct tog_view *);
598 static volatile sig_atomic_t tog_sigwinch_received;
599 static volatile sig_atomic_t tog_sigpipe_received;
600 static volatile sig_atomic_t tog_sigcont_received;
601 static volatile sig_atomic_t tog_sigint_received;
602 static volatile sig_atomic_t tog_sigterm_received;
604 static void
605 tog_sigwinch(int signo)
607 tog_sigwinch_received = 1;
610 static void
611 tog_sigpipe(int signo)
613 tog_sigpipe_received = 1;
616 static void
617 tog_sigcont(int signo)
619 tog_sigcont_received = 1;
622 static void
623 tog_sigint(int signo)
625 tog_sigint_received = 1;
628 static void
629 tog_sigterm(int signo)
631 tog_sigterm_received = 1;
634 static int
635 tog_fatal_signal_received(void)
637 return (tog_sigpipe_received ||
638 tog_sigint_received || tog_sigint_received);
642 static const struct got_error *
643 view_close(struct tog_view *view)
645 const struct got_error *err = NULL;
647 if (view->child) {
648 view_close(view->child);
649 view->child = NULL;
651 if (view->close)
652 err = view->close(view);
653 if (view->panel)
654 del_panel(view->panel);
655 if (view->window)
656 delwin(view->window);
657 free(view);
658 return err;
661 static struct tog_view *
662 view_open(int nlines, int ncols, int begin_y, int begin_x,
663 enum tog_view_type type)
665 struct tog_view *view = calloc(1, sizeof(*view));
667 if (view == NULL)
668 return NULL;
670 view->type = type;
671 view->lines = LINES;
672 view->cols = COLS;
673 view->nlines = nlines ? nlines : LINES - begin_y;
674 view->ncols = ncols ? ncols : COLS - begin_x;
675 view->begin_y = begin_y;
676 view->begin_x = begin_x;
677 view->window = newwin(nlines, ncols, begin_y, begin_x);
678 if (view->window == NULL) {
679 view_close(view);
680 return NULL;
682 view->panel = new_panel(view->window);
683 if (view->panel == NULL ||
684 set_panel_userptr(view->panel, view) != OK) {
685 view_close(view);
686 return NULL;
689 keypad(view->window, TRUE);
690 return view;
693 static int
694 view_split_begin_x(int begin_x)
696 if (begin_x > 0 || COLS < 120)
697 return 0;
698 return (COLS - MAX(COLS / 2, 80));
701 static const struct got_error *view_resize(struct tog_view *);
703 static const struct got_error *
704 view_splitscreen(struct tog_view *view)
706 const struct got_error *err = NULL;
708 view->begin_y = 0;
709 view->begin_x = view_split_begin_x(0);
710 view->nlines = LINES;
711 view->ncols = COLS - view->begin_x;
712 view->lines = LINES;
713 view->cols = COLS;
714 err = view_resize(view);
715 if (err)
716 return err;
718 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
719 return got_error_from_errno("mvwin");
721 return NULL;
724 static const struct got_error *
725 view_fullscreen(struct tog_view *view)
727 const struct got_error *err = NULL;
729 view->begin_x = 0;
730 view->begin_y = 0;
731 view->nlines = LINES;
732 view->ncols = COLS;
733 view->lines = LINES;
734 view->cols = COLS;
735 err = view_resize(view);
736 if (err)
737 return err;
739 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
740 return got_error_from_errno("mvwin");
742 return NULL;
745 static int
746 view_is_parent_view(struct tog_view *view)
748 return view->parent == NULL;
751 static const struct got_error *
752 view_resize(struct tog_view *view)
754 int nlines, ncols;
756 if (view->lines > LINES)
757 nlines = view->nlines - (view->lines - LINES);
758 else
759 nlines = view->nlines + (LINES - view->lines);
761 if (view->cols > COLS)
762 ncols = view->ncols - (view->cols - COLS);
763 else
764 ncols = view->ncols + (COLS - view->cols);
766 if (view->child) {
767 view->child->begin_x = view_split_begin_x(view->begin_x);
768 if (view->child->begin_x == 0) {
769 ncols = COLS;
771 view_fullscreen(view->child);
772 if (view->child->focussed)
773 show_panel(view->child->panel);
774 else
775 show_panel(view->panel);
776 } else {
777 ncols = view->child->begin_x;
779 view_splitscreen(view->child);
780 show_panel(view->child->panel);
782 } else if (view->parent == NULL)
783 ncols = COLS;
785 if (wresize(view->window, nlines, ncols) == ERR)
786 return got_error_from_errno("wresize");
787 if (replace_panel(view->panel, view->window) == ERR)
788 return got_error_from_errno("replace_panel");
789 wclear(view->window);
791 view->nlines = nlines;
792 view->ncols = ncols;
793 view->lines = LINES;
794 view->cols = COLS;
796 return NULL;
799 static const struct got_error *
800 view_close_child(struct tog_view *view)
802 const struct got_error *err = NULL;
804 if (view->child == NULL)
805 return NULL;
807 err = view_close(view->child);
808 view->child = NULL;
809 return err;
812 static const struct got_error *
813 view_set_child(struct tog_view *view, struct tog_view *child)
815 view->child = child;
816 child->parent = view;
818 return view_resize(view);
821 static int
822 view_is_splitscreen(struct tog_view *view)
824 return view->begin_x > 0;
827 static void
828 tog_resizeterm(void)
830 int cols, lines;
831 struct winsize size;
833 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
834 cols = 80; /* Default */
835 lines = 24;
836 } else {
837 cols = size.ws_col;
838 lines = size.ws_row;
840 resize_term(lines, cols);
843 static const struct got_error *
844 view_search_start(struct tog_view *view)
846 const struct got_error *err = NULL;
847 char pattern[1024];
848 int ret;
850 if (view->search_started) {
851 regfree(&view->regex);
852 view->searching = 0;
853 memset(&view->regmatch, 0, sizeof(view->regmatch));
855 view->search_started = 0;
857 if (view->nlines < 1)
858 return NULL;
860 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
861 wclrtoeol(view->window);
863 nocbreak();
864 echo();
865 ret = wgetnstr(view->window, pattern, sizeof(pattern));
866 cbreak();
867 noecho();
868 if (ret == ERR)
869 return NULL;
871 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
872 err = view->search_start(view);
873 if (err) {
874 regfree(&view->regex);
875 return err;
877 view->search_started = 1;
878 view->searching = TOG_SEARCH_FORWARD;
879 view->search_next_done = 0;
880 view->search_next(view);
883 return NULL;
886 static const struct got_error *
887 view_input(struct tog_view **new, int *done, struct tog_view *view,
888 struct tog_view_list_head *views)
890 const struct got_error *err = NULL;
891 struct tog_view *v;
892 int ch, errcode;
894 *new = NULL;
896 /* Clear "no matches" indicator. */
897 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
898 view->search_next_done == TOG_SEARCH_HAVE_NONE)
899 view->search_next_done = TOG_SEARCH_HAVE_MORE;
901 if (view->searching && !view->search_next_done) {
902 errcode = pthread_mutex_unlock(&tog_mutex);
903 if (errcode)
904 return got_error_set_errno(errcode,
905 "pthread_mutex_unlock");
906 sched_yield();
907 errcode = pthread_mutex_lock(&tog_mutex);
908 if (errcode)
909 return got_error_set_errno(errcode,
910 "pthread_mutex_lock");
911 view->search_next(view);
912 return NULL;
915 nodelay(stdscr, FALSE);
916 /* Allow threads to make progress while we are waiting for input. */
917 errcode = pthread_mutex_unlock(&tog_mutex);
918 if (errcode)
919 return got_error_set_errno(errcode, "pthread_mutex_unlock");
920 ch = wgetch(view->window);
921 errcode = pthread_mutex_lock(&tog_mutex);
922 if (errcode)
923 return got_error_set_errno(errcode, "pthread_mutex_lock");
924 nodelay(stdscr, TRUE);
926 if (tog_sigwinch_received || tog_sigcont_received) {
927 tog_resizeterm();
928 tog_sigwinch_received = 0;
929 tog_sigcont_received = 0;
930 TAILQ_FOREACH(v, views, entry) {
931 err = view_resize(v);
932 if (err)
933 return err;
934 err = v->input(new, v, KEY_RESIZE);
935 if (err)
936 return err;
937 if (v->child) {
938 err = view_resize(v->child);
939 if (err)
940 return err;
941 err = v->child->input(new, v->child,
942 KEY_RESIZE);
943 if (err)
944 return err;
949 switch (ch) {
950 case '\t':
951 if (view->child) {
952 view->focussed = 0;
953 view->child->focussed = 1;
954 view->focus_child = 1;
955 } else if (view->parent) {
956 view->focussed = 0;
957 view->parent->focussed = 1;
958 view->parent->focus_child = 0;
960 break;
961 case 'q':
962 err = view->input(new, view, ch);
963 view->dying = 1;
964 break;
965 case 'Q':
966 *done = 1;
967 break;
968 case 'f':
969 if (view_is_parent_view(view)) {
970 if (view->child == NULL)
971 break;
972 if (view_is_splitscreen(view->child)) {
973 view->focussed = 0;
974 view->child->focussed = 1;
975 err = view_fullscreen(view->child);
976 } else
977 err = view_splitscreen(view->child);
978 if (err)
979 break;
980 err = view->child->input(new, view->child,
981 KEY_RESIZE);
982 } else {
983 if (view_is_splitscreen(view)) {
984 view->parent->focussed = 0;
985 view->focussed = 1;
986 err = view_fullscreen(view);
987 } else {
988 err = view_splitscreen(view);
990 if (err)
991 break;
992 err = view->input(new, view, KEY_RESIZE);
994 break;
995 case KEY_RESIZE:
996 break;
997 case '/':
998 if (view->search_start)
999 view_search_start(view);
1000 else
1001 err = view->input(new, view, ch);
1002 break;
1003 case 'N':
1004 case 'n':
1005 if (view->search_started && view->search_next) {
1006 view->searching = (ch == 'n' ?
1007 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1008 view->search_next_done = 0;
1009 view->search_next(view);
1010 } else
1011 err = view->input(new, view, ch);
1012 break;
1013 default:
1014 err = view->input(new, view, ch);
1015 break;
1018 return err;
1021 void
1022 view_vborder(struct tog_view *view)
1024 PANEL *panel;
1025 const struct tog_view *view_above;
1027 if (view->parent)
1028 return view_vborder(view->parent);
1030 panel = panel_above(view->panel);
1031 if (panel == NULL)
1032 return;
1034 view_above = panel_userptr(panel);
1035 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1036 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1039 int
1040 view_needs_focus_indication(struct tog_view *view)
1042 if (view_is_parent_view(view)) {
1043 if (view->child == NULL || view->child->focussed)
1044 return 0;
1045 if (!view_is_splitscreen(view->child))
1046 return 0;
1047 } else if (!view_is_splitscreen(view))
1048 return 0;
1050 return view->focussed;
1053 static const struct got_error *
1054 view_loop(struct tog_view *view)
1056 const struct got_error *err = NULL;
1057 struct tog_view_list_head views;
1058 struct tog_view *new_view;
1059 int fast_refresh = 10;
1060 int done = 0, errcode;
1062 errcode = pthread_mutex_lock(&tog_mutex);
1063 if (errcode)
1064 return got_error_set_errno(errcode, "pthread_mutex_lock");
1066 TAILQ_INIT(&views);
1067 TAILQ_INSERT_HEAD(&views, view, entry);
1069 view->focussed = 1;
1070 err = view->show(view);
1071 if (err)
1072 return err;
1073 update_panels();
1074 doupdate();
1075 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1076 /* Refresh fast during initialization, then become slower. */
1077 if (fast_refresh && fast_refresh-- == 0)
1078 halfdelay(10); /* switch to once per second */
1080 err = view_input(&new_view, &done, view, &views);
1081 if (err)
1082 break;
1083 if (view->dying) {
1084 struct tog_view *v, *prev = NULL;
1086 if (view_is_parent_view(view))
1087 prev = TAILQ_PREV(view, tog_view_list_head,
1088 entry);
1089 else if (view->parent)
1090 prev = view->parent;
1092 if (view->parent) {
1093 view->parent->child = NULL;
1094 view->parent->focus_child = 0;
1096 err = view_resize(view->parent);
1097 if (err)
1098 break;
1099 } else
1100 TAILQ_REMOVE(&views, view, entry);
1102 err = view_close(view);
1103 if (err)
1104 goto done;
1106 view = NULL;
1107 TAILQ_FOREACH(v, &views, entry) {
1108 if (v->focussed)
1109 break;
1111 if (view == NULL && new_view == NULL) {
1112 /* No view has focus. Try to pick one. */
1113 if (prev)
1114 view = prev;
1115 else if (!TAILQ_EMPTY(&views)) {
1116 view = TAILQ_LAST(&views,
1117 tog_view_list_head);
1119 if (view) {
1120 if (view->focus_child) {
1121 view->child->focussed = 1;
1122 view = view->child;
1123 } else
1124 view->focussed = 1;
1128 if (new_view) {
1129 struct tog_view *v, *t;
1130 /* Only allow one parent view per type. */
1131 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1132 if (v->type != new_view->type)
1133 continue;
1134 TAILQ_REMOVE(&views, v, entry);
1135 err = view_close(v);
1136 if (err)
1137 goto done;
1138 break;
1140 TAILQ_INSERT_TAIL(&views, new_view, entry);
1141 view = new_view;
1143 if (view) {
1144 if (view_is_parent_view(view)) {
1145 if (view->child && view->child->focussed)
1146 view = view->child;
1147 } else {
1148 if (view->parent && view->parent->focussed)
1149 view = view->parent;
1151 show_panel(view->panel);
1152 if (view->child && view_is_splitscreen(view->child))
1153 show_panel(view->child->panel);
1154 if (view->parent && view_is_splitscreen(view)) {
1155 err = view->parent->show(view->parent);
1156 if (err)
1157 goto done;
1159 err = view->show(view);
1160 if (err)
1161 goto done;
1162 if (view->child) {
1163 err = view->child->show(view->child);
1164 if (err)
1165 goto done;
1167 update_panels();
1168 doupdate();
1171 done:
1172 while (!TAILQ_EMPTY(&views)) {
1173 view = TAILQ_FIRST(&views);
1174 TAILQ_REMOVE(&views, view, entry);
1175 view_close(view);
1178 errcode = pthread_mutex_unlock(&tog_mutex);
1179 if (errcode && err == NULL)
1180 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1182 return err;
1185 __dead static void
1186 usage_log(void)
1188 endwin();
1189 fprintf(stderr,
1190 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1191 getprogname());
1192 exit(1);
1195 /* Create newly allocated wide-character string equivalent to a byte string. */
1196 static const struct got_error *
1197 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1199 char *vis = NULL;
1200 const struct got_error *err = NULL;
1202 *ws = NULL;
1203 *wlen = mbstowcs(NULL, s, 0);
1204 if (*wlen == (size_t)-1) {
1205 int vislen;
1206 if (errno != EILSEQ)
1207 return got_error_from_errno("mbstowcs");
1209 /* byte string invalid in current encoding; try to "fix" it */
1210 err = got_mbsavis(&vis, &vislen, s);
1211 if (err)
1212 return err;
1213 *wlen = mbstowcs(NULL, vis, 0);
1214 if (*wlen == (size_t)-1) {
1215 err = got_error_from_errno("mbstowcs"); /* give up */
1216 goto done;
1220 *ws = calloc(*wlen + 1, sizeof(**ws));
1221 if (*ws == NULL) {
1222 err = got_error_from_errno("calloc");
1223 goto done;
1226 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1227 err = got_error_from_errno("mbstowcs");
1228 done:
1229 free(vis);
1230 if (err) {
1231 free(*ws);
1232 *ws = NULL;
1233 *wlen = 0;
1235 return err;
1238 static const struct got_error *
1239 expand_tab(char **ptr, const char *src)
1241 char *dst;
1242 size_t len, n, idx = 0, sz = 0;
1244 *ptr = NULL;
1245 n = len = strlen(src);
1246 dst = malloc(n + 1);
1247 if (dst == NULL)
1248 return got_error_from_errno("malloc");
1250 while (idx < len && src[idx]) {
1251 const char c = src[idx];
1253 if (c == '\t') {
1254 size_t nb = TABSIZE - sz % TABSIZE;
1255 char *p;
1257 p = realloc(dst, n + nb);
1258 if (p == NULL) {
1259 free(dst);
1260 return got_error_from_errno("realloc");
1263 dst = p;
1264 n += nb;
1265 memset(dst + sz, ' ', nb);
1266 sz += nb;
1267 } else
1268 dst[sz++] = src[idx];
1269 ++idx;
1272 dst[sz] = '\0';
1273 *ptr = dst;
1274 return NULL;
1278 * Advance at most n columns from wline starting at offset off.
1279 * Return the index to the first character after the span operation.
1280 * Return the combined column width of all spanned wide character in
1281 * *rcol.
1283 static int
1284 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1286 int width, i, cols = 0;
1288 if (n == 0) {
1289 *rcol = cols;
1290 return off;
1293 for (i = off; wline[i] != L'\0'; ++i) {
1294 if (wline[i] == L'\t')
1295 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1296 else
1297 width = wcwidth(wline[i]);
1299 if (width == -1) {
1300 width = 1;
1301 wline[i] = L'.';
1304 if (cols + width > n)
1305 break;
1306 cols += width;
1309 *rcol = cols;
1310 return i;
1314 * Format a line for display, ensuring that it won't overflow a width limit.
1315 * With scrolling, the width returned refers to the scrolled version of the
1316 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1318 static const struct got_error *
1319 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1320 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1322 const struct got_error *err = NULL;
1323 int cols;
1324 wchar_t *wline = NULL;
1325 char *exstr = NULL;
1326 size_t wlen;
1327 int i, scrollx;
1329 *wlinep = NULL;
1330 *widthp = 0;
1332 if (expand) {
1333 err = expand_tab(&exstr, line);
1334 if (err)
1335 return err;
1338 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1339 free(exstr);
1340 if (err)
1341 return err;
1343 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1345 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1346 wline[wlen - 1] = L'\0';
1347 wlen--;
1349 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1350 wline[wlen - 1] = L'\0';
1351 wlen--;
1354 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1355 wline[i] = L'\0';
1357 if (widthp)
1358 *widthp = cols;
1359 if (scrollxp)
1360 *scrollxp = scrollx;
1361 if (err)
1362 free(wline);
1363 else
1364 *wlinep = wline;
1365 return err;
1368 static const struct got_error*
1369 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1370 struct got_object_id *id, struct got_repository *repo)
1372 static const struct got_error *err = NULL;
1373 struct got_reflist_entry *re;
1374 char *s;
1375 const char *name;
1377 *refs_str = NULL;
1379 TAILQ_FOREACH(re, refs, entry) {
1380 struct got_tag_object *tag = NULL;
1381 struct got_object_id *ref_id;
1382 int cmp;
1384 name = got_ref_get_name(re->ref);
1385 if (strcmp(name, GOT_REF_HEAD) == 0)
1386 continue;
1387 if (strncmp(name, "refs/", 5) == 0)
1388 name += 5;
1389 if (strncmp(name, "got/", 4) == 0 &&
1390 strncmp(name, "got/backup/", 11) != 0)
1391 continue;
1392 if (strncmp(name, "heads/", 6) == 0)
1393 name += 6;
1394 if (strncmp(name, "remotes/", 8) == 0) {
1395 name += 8;
1396 s = strstr(name, "/" GOT_REF_HEAD);
1397 if (s != NULL && s[strlen(s)] == '\0')
1398 continue;
1400 err = got_ref_resolve(&ref_id, repo, re->ref);
1401 if (err)
1402 break;
1403 if (strncmp(name, "tags/", 5) == 0) {
1404 err = got_object_open_as_tag(&tag, repo, ref_id);
1405 if (err) {
1406 if (err->code != GOT_ERR_OBJ_TYPE) {
1407 free(ref_id);
1408 break;
1410 /* Ref points at something other than a tag. */
1411 err = NULL;
1412 tag = NULL;
1415 cmp = got_object_id_cmp(tag ?
1416 got_object_tag_get_object_id(tag) : ref_id, id);
1417 free(ref_id);
1418 if (tag)
1419 got_object_tag_close(tag);
1420 if (cmp != 0)
1421 continue;
1422 s = *refs_str;
1423 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1424 s ? ", " : "", name) == -1) {
1425 err = got_error_from_errno("asprintf");
1426 free(s);
1427 *refs_str = NULL;
1428 break;
1430 free(s);
1433 return err;
1436 static const struct got_error *
1437 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1438 int col_tab_align)
1440 char *smallerthan;
1442 smallerthan = strchr(author, '<');
1443 if (smallerthan && smallerthan[1] != '\0')
1444 author = smallerthan + 1;
1445 author[strcspn(author, "@>")] = '\0';
1446 return format_line(wauthor, author_width, NULL, author, 0, limit,
1447 col_tab_align, 0);
1450 static const struct got_error *
1451 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1452 struct got_object_id *id, const size_t date_display_cols,
1453 int author_display_cols)
1455 struct tog_log_view_state *s = &view->state.log;
1456 const struct got_error *err = NULL;
1457 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1458 char *logmsg0 = NULL, *logmsg = NULL;
1459 char *author = NULL;
1460 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1461 int author_width, logmsg_width;
1462 char *newline, *line = NULL;
1463 int col, limit, scrollx;
1464 const int avail = view->ncols;
1465 struct tm tm;
1466 time_t committer_time;
1467 struct tog_color *tc;
1469 committer_time = got_object_commit_get_committer_time(commit);
1470 if (gmtime_r(&committer_time, &tm) == NULL)
1471 return got_error_from_errno("gmtime_r");
1472 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1473 return got_error(GOT_ERR_NO_SPACE);
1475 if (avail <= date_display_cols)
1476 limit = MIN(sizeof(datebuf) - 1, avail);
1477 else
1478 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1479 tc = get_color(&s->colors, TOG_COLOR_DATE);
1480 if (tc)
1481 wattr_on(view->window,
1482 COLOR_PAIR(tc->colorpair), NULL);
1483 waddnstr(view->window, datebuf, limit);
1484 if (tc)
1485 wattr_off(view->window,
1486 COLOR_PAIR(tc->colorpair), NULL);
1487 col = limit;
1488 if (col > avail)
1489 goto done;
1491 if (avail >= 120) {
1492 char *id_str;
1493 err = got_object_id_str(&id_str, id);
1494 if (err)
1495 goto done;
1496 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1497 if (tc)
1498 wattr_on(view->window,
1499 COLOR_PAIR(tc->colorpair), NULL);
1500 wprintw(view->window, "%.8s ", id_str);
1501 if (tc)
1502 wattr_off(view->window,
1503 COLOR_PAIR(tc->colorpair), NULL);
1504 free(id_str);
1505 col += 9;
1506 if (col > avail)
1507 goto done;
1510 author = strdup(got_object_commit_get_author(commit));
1511 if (author == NULL) {
1512 err = got_error_from_errno("strdup");
1513 goto done;
1515 err = format_author(&wauthor, &author_width, author, avail - col, col);
1516 if (err)
1517 goto done;
1518 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1519 if (tc)
1520 wattr_on(view->window,
1521 COLOR_PAIR(tc->colorpair), NULL);
1522 waddwstr(view->window, wauthor);
1523 if (tc)
1524 wattr_off(view->window,
1525 COLOR_PAIR(tc->colorpair), NULL);
1526 col += author_width;
1527 while (col < avail && author_width < author_display_cols + 2) {
1528 waddch(view->window, ' ');
1529 col++;
1530 author_width++;
1532 if (col > avail)
1533 goto done;
1535 err = got_object_commit_get_logmsg(&logmsg0, commit);
1536 if (err)
1537 goto done;
1538 logmsg = logmsg0;
1539 while (*logmsg == '\n')
1540 logmsg++;
1541 newline = strchr(logmsg, '\n');
1542 if (newline)
1543 *newline = '\0';
1544 limit = avail - col;
1545 if (view->child && limit > 0)
1546 limit--; /* for the border */
1547 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1548 limit, col, 1);
1549 if (err)
1550 goto done;
1551 waddwstr(view->window, &wlogmsg[scrollx]);
1552 col += MAX(logmsg_width, 0);
1553 while (col < avail) {
1554 waddch(view->window, ' ');
1555 col++;
1557 done:
1558 free(logmsg0);
1559 free(wlogmsg);
1560 free(author);
1561 free(wauthor);
1562 free(line);
1563 return err;
1566 static struct commit_queue_entry *
1567 alloc_commit_queue_entry(struct got_commit_object *commit,
1568 struct got_object_id *id)
1570 struct commit_queue_entry *entry;
1572 entry = calloc(1, sizeof(*entry));
1573 if (entry == NULL)
1574 return NULL;
1576 entry->id = id;
1577 entry->commit = commit;
1578 return entry;
1581 static void
1582 pop_commit(struct commit_queue *commits)
1584 struct commit_queue_entry *entry;
1586 entry = TAILQ_FIRST(&commits->head);
1587 TAILQ_REMOVE(&commits->head, entry, entry);
1588 got_object_commit_close(entry->commit);
1589 commits->ncommits--;
1590 /* Don't free entry->id! It is owned by the commit graph. */
1591 free(entry);
1594 static void
1595 free_commits(struct commit_queue *commits)
1597 while (!TAILQ_EMPTY(&commits->head))
1598 pop_commit(commits);
1601 static const struct got_error *
1602 match_commit(int *have_match, struct got_object_id *id,
1603 struct got_commit_object *commit, regex_t *regex)
1605 const struct got_error *err = NULL;
1606 regmatch_t regmatch;
1607 char *id_str = NULL, *logmsg = NULL;
1609 *have_match = 0;
1611 err = got_object_id_str(&id_str, id);
1612 if (err)
1613 return err;
1615 err = got_object_commit_get_logmsg(&logmsg, commit);
1616 if (err)
1617 goto done;
1619 if (regexec(regex, got_object_commit_get_author(commit), 1,
1620 &regmatch, 0) == 0 ||
1621 regexec(regex, got_object_commit_get_committer(commit), 1,
1622 &regmatch, 0) == 0 ||
1623 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1624 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1625 *have_match = 1;
1626 done:
1627 free(id_str);
1628 free(logmsg);
1629 return err;
1632 static const struct got_error *
1633 queue_commits(struct tog_log_thread_args *a)
1635 const struct got_error *err = NULL;
1638 * We keep all commits open throughout the lifetime of the log
1639 * view in order to avoid having to re-fetch commits from disk
1640 * while updating the display.
1642 do {
1643 struct got_object_id *id;
1644 struct got_commit_object *commit;
1645 struct commit_queue_entry *entry;
1646 int errcode;
1648 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1649 NULL, NULL);
1650 if (err || id == NULL)
1651 break;
1653 err = got_object_open_as_commit(&commit, a->repo, id);
1654 if (err)
1655 break;
1656 entry = alloc_commit_queue_entry(commit, id);
1657 if (entry == NULL) {
1658 err = got_error_from_errno("alloc_commit_queue_entry");
1659 break;
1662 errcode = pthread_mutex_lock(&tog_mutex);
1663 if (errcode) {
1664 err = got_error_set_errno(errcode,
1665 "pthread_mutex_lock");
1666 break;
1669 entry->idx = a->commits->ncommits;
1670 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1671 a->commits->ncommits++;
1673 if (*a->searching == TOG_SEARCH_FORWARD &&
1674 !*a->search_next_done) {
1675 int have_match;
1676 err = match_commit(&have_match, id, commit, a->regex);
1677 if (err)
1678 break;
1679 if (have_match)
1680 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1683 errcode = pthread_mutex_unlock(&tog_mutex);
1684 if (errcode && err == NULL)
1685 err = got_error_set_errno(errcode,
1686 "pthread_mutex_unlock");
1687 if (err)
1688 break;
1689 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1691 return err;
1694 static void
1695 select_commit(struct tog_log_view_state *s)
1697 struct commit_queue_entry *entry;
1698 int ncommits = 0;
1700 entry = s->first_displayed_entry;
1701 while (entry) {
1702 if (ncommits == s->selected) {
1703 s->selected_entry = entry;
1704 break;
1706 entry = TAILQ_NEXT(entry, entry);
1707 ncommits++;
1711 static const struct got_error *
1712 draw_commits(struct tog_view *view)
1714 const struct got_error *err = NULL;
1715 struct tog_log_view_state *s = &view->state.log;
1716 struct commit_queue_entry *entry = s->selected_entry;
1717 const int limit = view->nlines;
1718 int width;
1719 int ncommits, author_cols = 4;
1720 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1721 char *refs_str = NULL;
1722 wchar_t *wline;
1723 struct tog_color *tc;
1724 static const size_t date_display_cols = 12;
1726 if (s->selected_entry &&
1727 !(view->searching && view->search_next_done == 0)) {
1728 struct got_reflist_head *refs;
1729 err = got_object_id_str(&id_str, s->selected_entry->id);
1730 if (err)
1731 return err;
1732 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1733 s->selected_entry->id);
1734 if (refs) {
1735 err = build_refs_str(&refs_str, refs,
1736 s->selected_entry->id, s->repo);
1737 if (err)
1738 goto done;
1742 if (s->thread_args.commits_needed == 0)
1743 halfdelay(10); /* disable fast refresh */
1745 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1746 if (asprintf(&ncommits_str, " [%d/%d] %s",
1747 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1748 (view->searching && !view->search_next_done) ?
1749 "searching..." : "loading...") == -1) {
1750 err = got_error_from_errno("asprintf");
1751 goto done;
1753 } else {
1754 const char *search_str = NULL;
1756 if (view->searching) {
1757 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1758 search_str = "no more matches";
1759 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1760 search_str = "no matches found";
1761 else if (!view->search_next_done)
1762 search_str = "searching...";
1765 if (asprintf(&ncommits_str, " [%d/%d] %s",
1766 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1767 search_str ? search_str :
1768 (refs_str ? refs_str : "")) == -1) {
1769 err = got_error_from_errno("asprintf");
1770 goto done;
1774 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1775 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1776 "........................................",
1777 s->in_repo_path, ncommits_str) == -1) {
1778 err = got_error_from_errno("asprintf");
1779 header = NULL;
1780 goto done;
1782 } else if (asprintf(&header, "commit %s%s",
1783 id_str ? id_str : "........................................",
1784 ncommits_str) == -1) {
1785 err = got_error_from_errno("asprintf");
1786 header = NULL;
1787 goto done;
1789 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1790 if (err)
1791 goto done;
1793 werase(view->window);
1795 if (view_needs_focus_indication(view))
1796 wstandout(view->window);
1797 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1798 if (tc)
1799 wattr_on(view->window,
1800 COLOR_PAIR(tc->colorpair), NULL);
1801 waddwstr(view->window, wline);
1802 if (tc)
1803 wattr_off(view->window,
1804 COLOR_PAIR(tc->colorpair), NULL);
1805 while (width < view->ncols) {
1806 waddch(view->window, ' ');
1807 width++;
1809 if (view_needs_focus_indication(view))
1810 wstandend(view->window);
1811 free(wline);
1812 if (limit <= 1)
1813 goto done;
1815 /* Grow author column size if necessary, and set view->maxx. */
1816 entry = s->first_displayed_entry;
1817 ncommits = 0;
1818 view->maxx = 0;
1819 while (entry) {
1820 char *author, *eol, *msg, *msg0;
1821 wchar_t *wauthor, *wmsg;
1822 int width;
1823 if (ncommits >= limit - 1)
1824 break;
1825 author = strdup(got_object_commit_get_author(entry->commit));
1826 if (author == NULL) {
1827 err = got_error_from_errno("strdup");
1828 goto done;
1830 err = format_author(&wauthor, &width, author, COLS,
1831 date_display_cols);
1832 if (author_cols < width)
1833 author_cols = width;
1834 free(wauthor);
1835 free(author);
1836 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1837 if (err)
1838 goto done;
1839 msg = msg0;
1840 while (*msg == '\n')
1841 ++msg;
1842 if ((eol = strchr(msg, '\n')))
1843 *eol = '\0';
1844 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1845 date_display_cols + author_cols, 0);
1846 if (err)
1847 goto done;
1848 view->maxx = MAX(view->maxx, width);
1849 free(msg0);
1850 free(wmsg);
1851 ncommits++;
1852 entry = TAILQ_NEXT(entry, entry);
1855 entry = s->first_displayed_entry;
1856 s->last_displayed_entry = s->first_displayed_entry;
1857 ncommits = 0;
1858 while (entry) {
1859 if (ncommits >= limit - 1)
1860 break;
1861 if (ncommits == s->selected)
1862 wstandout(view->window);
1863 err = draw_commit(view, entry->commit, entry->id,
1864 date_display_cols, author_cols);
1865 if (ncommits == s->selected)
1866 wstandend(view->window);
1867 if (err)
1868 goto done;
1869 ncommits++;
1870 s->last_displayed_entry = entry;
1871 entry = TAILQ_NEXT(entry, entry);
1874 view_vborder(view);
1875 done:
1876 free(id_str);
1877 free(refs_str);
1878 free(ncommits_str);
1879 free(header);
1880 return err;
1883 static void
1884 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1886 struct commit_queue_entry *entry;
1887 int nscrolled = 0;
1889 entry = TAILQ_FIRST(&s->commits.head);
1890 if (s->first_displayed_entry == entry)
1891 return;
1893 entry = s->first_displayed_entry;
1894 while (entry && nscrolled < maxscroll) {
1895 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1896 if (entry) {
1897 s->first_displayed_entry = entry;
1898 nscrolled++;
1903 static const struct got_error *
1904 trigger_log_thread(struct tog_view *view, int wait)
1906 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1907 int errcode;
1909 halfdelay(1); /* fast refresh while loading commits */
1911 while (ta->commits_needed > 0 || ta->load_all) {
1912 if (ta->log_complete)
1913 break;
1915 /* Wake the log thread. */
1916 errcode = pthread_cond_signal(&ta->need_commits);
1917 if (errcode)
1918 return got_error_set_errno(errcode,
1919 "pthread_cond_signal");
1922 * The mutex will be released while the view loop waits
1923 * in wgetch(), at which time the log thread will run.
1925 if (!wait)
1926 break;
1928 /* Display progress update in log view. */
1929 show_log_view(view);
1930 update_panels();
1931 doupdate();
1933 /* Wait right here while next commit is being loaded. */
1934 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1935 if (errcode)
1936 return got_error_set_errno(errcode,
1937 "pthread_cond_wait");
1939 /* Display progress update in log view. */
1940 show_log_view(view);
1941 update_panels();
1942 doupdate();
1945 return NULL;
1948 static const struct got_error *
1949 log_scroll_down(struct tog_view *view, int maxscroll)
1951 struct tog_log_view_state *s = &view->state.log;
1952 const struct got_error *err = NULL;
1953 struct commit_queue_entry *pentry;
1954 int nscrolled = 0, ncommits_needed;
1956 if (s->last_displayed_entry == NULL)
1957 return NULL;
1959 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1960 if (s->commits.ncommits < ncommits_needed &&
1961 !s->thread_args.log_complete) {
1963 * Ask the log thread for required amount of commits.
1965 s->thread_args.commits_needed += maxscroll;
1966 err = trigger_log_thread(view, 1);
1967 if (err)
1968 return err;
1971 do {
1972 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1973 if (pentry == NULL)
1974 break;
1976 s->last_displayed_entry = pentry;
1978 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1979 if (pentry == NULL)
1980 break;
1981 s->first_displayed_entry = pentry;
1982 } while (++nscrolled < maxscroll);
1984 return err;
1987 static const struct got_error *
1988 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1989 struct got_commit_object *commit, struct got_object_id *commit_id,
1990 struct tog_view *log_view, struct got_repository *repo)
1992 const struct got_error *err;
1993 struct got_object_qid *parent_id;
1994 struct tog_view *diff_view;
1996 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1997 if (diff_view == NULL)
1998 return got_error_from_errno("view_open");
2000 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2001 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2002 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2003 if (err == NULL)
2004 *new_view = diff_view;
2005 return err;
2008 static const struct got_error *
2009 tree_view_visit_subtree(struct tog_tree_view_state *s,
2010 struct got_tree_object *subtree)
2012 struct tog_parent_tree *parent;
2014 parent = calloc(1, sizeof(*parent));
2015 if (parent == NULL)
2016 return got_error_from_errno("calloc");
2018 parent->tree = s->tree;
2019 parent->first_displayed_entry = s->first_displayed_entry;
2020 parent->selected_entry = s->selected_entry;
2021 parent->selected = s->selected;
2022 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2023 s->tree = subtree;
2024 s->selected = 0;
2025 s->first_displayed_entry = NULL;
2026 return NULL;
2029 static const struct got_error *
2030 tree_view_walk_path(struct tog_tree_view_state *s,
2031 struct got_commit_object *commit, const char *path)
2033 const struct got_error *err = NULL;
2034 struct got_tree_object *tree = NULL;
2035 const char *p;
2036 char *slash, *subpath = NULL;
2038 /* Walk the path and open corresponding tree objects. */
2039 p = path;
2040 while (*p) {
2041 struct got_tree_entry *te;
2042 struct got_object_id *tree_id;
2043 char *te_name;
2045 while (p[0] == '/')
2046 p++;
2048 /* Ensure the correct subtree entry is selected. */
2049 slash = strchr(p, '/');
2050 if (slash == NULL)
2051 te_name = strdup(p);
2052 else
2053 te_name = strndup(p, slash - p);
2054 if (te_name == NULL) {
2055 err = got_error_from_errno("strndup");
2056 break;
2058 te = got_object_tree_find_entry(s->tree, te_name);
2059 if (te == NULL) {
2060 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2061 free(te_name);
2062 break;
2064 free(te_name);
2065 s->first_displayed_entry = s->selected_entry = te;
2067 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2068 break; /* jump to this file's entry */
2070 slash = strchr(p, '/');
2071 if (slash)
2072 subpath = strndup(path, slash - path);
2073 else
2074 subpath = strdup(path);
2075 if (subpath == NULL) {
2076 err = got_error_from_errno("strdup");
2077 break;
2080 err = got_object_id_by_path(&tree_id, s->repo, commit,
2081 subpath);
2082 if (err)
2083 break;
2085 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2086 free(tree_id);
2087 if (err)
2088 break;
2090 err = tree_view_visit_subtree(s, tree);
2091 if (err) {
2092 got_object_tree_close(tree);
2093 break;
2095 if (slash == NULL)
2096 break;
2097 free(subpath);
2098 subpath = NULL;
2099 p = slash;
2102 free(subpath);
2103 return err;
2106 static const struct got_error *
2107 browse_commit_tree(struct tog_view **new_view, int begin_x,
2108 struct commit_queue_entry *entry, const char *path,
2109 const char *head_ref_name, struct got_repository *repo)
2111 const struct got_error *err = NULL;
2112 struct tog_tree_view_state *s;
2113 struct tog_view *tree_view;
2115 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2116 if (tree_view == NULL)
2117 return got_error_from_errno("view_open");
2119 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2120 if (err)
2121 return err;
2122 s = &tree_view->state.tree;
2124 *new_view = tree_view;
2126 if (got_path_is_root_dir(path))
2127 return NULL;
2129 return tree_view_walk_path(s, entry->commit, path);
2132 static const struct got_error *
2133 block_signals_used_by_main_thread(void)
2135 sigset_t sigset;
2136 int errcode;
2138 if (sigemptyset(&sigset) == -1)
2139 return got_error_from_errno("sigemptyset");
2141 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2142 if (sigaddset(&sigset, SIGWINCH) == -1)
2143 return got_error_from_errno("sigaddset");
2144 if (sigaddset(&sigset, SIGCONT) == -1)
2145 return got_error_from_errno("sigaddset");
2146 if (sigaddset(&sigset, SIGINT) == -1)
2147 return got_error_from_errno("sigaddset");
2148 if (sigaddset(&sigset, SIGTERM) == -1)
2149 return got_error_from_errno("sigaddset");
2151 /* ncurses handles SIGTSTP */
2152 if (sigaddset(&sigset, SIGTSTP) == -1)
2153 return got_error_from_errno("sigaddset");
2155 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2156 if (errcode)
2157 return got_error_set_errno(errcode, "pthread_sigmask");
2159 return NULL;
2162 static void *
2163 log_thread(void *arg)
2165 const struct got_error *err = NULL;
2166 int errcode = 0;
2167 struct tog_log_thread_args *a = arg;
2168 int done = 0;
2170 err = block_signals_used_by_main_thread();
2171 if (err)
2172 return (void *)err;
2174 while (!done && !err && !tog_fatal_signal_received()) {
2175 err = queue_commits(a);
2176 if (err) {
2177 if (err->code != GOT_ERR_ITER_COMPLETED)
2178 return (void *)err;
2179 err = NULL;
2180 done = 1;
2181 } else if (a->commits_needed > 0 && !a->load_all)
2182 a->commits_needed--;
2184 errcode = pthread_mutex_lock(&tog_mutex);
2185 if (errcode) {
2186 err = got_error_set_errno(errcode,
2187 "pthread_mutex_lock");
2188 break;
2189 } else if (*a->quit)
2190 done = 1;
2191 else if (*a->first_displayed_entry == NULL) {
2192 *a->first_displayed_entry =
2193 TAILQ_FIRST(&a->commits->head);
2194 *a->selected_entry = *a->first_displayed_entry;
2197 errcode = pthread_cond_signal(&a->commit_loaded);
2198 if (errcode) {
2199 err = got_error_set_errno(errcode,
2200 "pthread_cond_signal");
2201 pthread_mutex_unlock(&tog_mutex);
2202 break;
2205 if (done)
2206 a->commits_needed = 0;
2207 else {
2208 if (a->commits_needed == 0 && !a->load_all) {
2209 errcode = pthread_cond_wait(&a->need_commits,
2210 &tog_mutex);
2211 if (errcode)
2212 err = got_error_set_errno(errcode,
2213 "pthread_cond_wait");
2214 if (*a->quit)
2215 done = 1;
2219 errcode = pthread_mutex_unlock(&tog_mutex);
2220 if (errcode && err == NULL)
2221 err = got_error_set_errno(errcode,
2222 "pthread_mutex_unlock");
2224 a->log_complete = 1;
2225 return (void *)err;
2228 static const struct got_error *
2229 stop_log_thread(struct tog_log_view_state *s)
2231 const struct got_error *err = NULL;
2232 int errcode;
2234 if (s->thread) {
2235 s->quit = 1;
2236 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2237 if (errcode)
2238 return got_error_set_errno(errcode,
2239 "pthread_cond_signal");
2240 errcode = pthread_mutex_unlock(&tog_mutex);
2241 if (errcode)
2242 return got_error_set_errno(errcode,
2243 "pthread_mutex_unlock");
2244 errcode = pthread_join(s->thread, (void **)&err);
2245 if (errcode)
2246 return got_error_set_errno(errcode, "pthread_join");
2247 errcode = pthread_mutex_lock(&tog_mutex);
2248 if (errcode)
2249 return got_error_set_errno(errcode,
2250 "pthread_mutex_lock");
2251 s->thread = NULL;
2254 if (s->thread_args.repo) {
2255 err = got_repo_close(s->thread_args.repo);
2256 s->thread_args.repo = NULL;
2259 if (s->thread_args.pack_fds) {
2260 const struct got_error *pack_err =
2261 got_repo_pack_fds_close(s->thread_args.pack_fds);
2262 if (err == NULL)
2263 err = pack_err;
2264 s->thread_args.pack_fds = NULL;
2267 if (s->thread_args.graph) {
2268 got_commit_graph_close(s->thread_args.graph);
2269 s->thread_args.graph = NULL;
2272 return err;
2275 static const struct got_error *
2276 close_log_view(struct tog_view *view)
2278 const struct got_error *err = NULL;
2279 struct tog_log_view_state *s = &view->state.log;
2280 int errcode;
2282 err = stop_log_thread(s);
2284 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2285 if (errcode && err == NULL)
2286 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2288 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2289 if (errcode && err == NULL)
2290 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2292 free_commits(&s->commits);
2293 free(s->in_repo_path);
2294 s->in_repo_path = NULL;
2295 free(s->start_id);
2296 s->start_id = NULL;
2297 free(s->head_ref_name);
2298 s->head_ref_name = NULL;
2299 return err;
2302 static const struct got_error *
2303 search_start_log_view(struct tog_view *view)
2305 struct tog_log_view_state *s = &view->state.log;
2307 s->matched_entry = NULL;
2308 s->search_entry = NULL;
2309 return NULL;
2312 static const struct got_error *
2313 search_next_log_view(struct tog_view *view)
2315 const struct got_error *err = NULL;
2316 struct tog_log_view_state *s = &view->state.log;
2317 struct commit_queue_entry *entry;
2319 /* Display progress update in log view. */
2320 show_log_view(view);
2321 update_panels();
2322 doupdate();
2324 if (s->search_entry) {
2325 int errcode, ch;
2326 errcode = pthread_mutex_unlock(&tog_mutex);
2327 if (errcode)
2328 return got_error_set_errno(errcode,
2329 "pthread_mutex_unlock");
2330 ch = wgetch(view->window);
2331 errcode = pthread_mutex_lock(&tog_mutex);
2332 if (errcode)
2333 return got_error_set_errno(errcode,
2334 "pthread_mutex_lock");
2335 if (ch == KEY_BACKSPACE) {
2336 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2337 return NULL;
2339 if (view->searching == TOG_SEARCH_FORWARD)
2340 entry = TAILQ_NEXT(s->search_entry, entry);
2341 else
2342 entry = TAILQ_PREV(s->search_entry,
2343 commit_queue_head, entry);
2344 } else if (s->matched_entry) {
2345 int matched_idx = s->matched_entry->idx;
2346 int selected_idx = s->selected_entry->idx;
2349 * If user has moved cursor after we hit the match, position
2350 * from where we should continue search must be changed.
2352 if (view->searching == TOG_SEARCH_FORWARD)
2353 if (matched_idx > selected_idx)
2354 entry = TAILQ_NEXT(s->selected_entry, entry);
2355 else
2356 entry = TAILQ_NEXT(s->matched_entry, entry);
2357 else
2358 if (matched_idx < selected_idx)
2359 entry = TAILQ_PREV(s->selected_entry,
2360 commit_queue_head, entry);
2361 else
2362 entry = TAILQ_PREV(s->matched_entry,
2363 commit_queue_head, entry);
2364 } else {
2365 entry = s->selected_entry;
2368 while (1) {
2369 int have_match = 0;
2371 if (entry == NULL) {
2372 if (s->thread_args.log_complete ||
2373 view->searching == TOG_SEARCH_BACKWARD) {
2374 view->search_next_done =
2375 (s->matched_entry == NULL ?
2376 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2377 s->search_entry = NULL;
2378 return NULL;
2381 * Poke the log thread for more commits and return,
2382 * allowing the main loop to make progress. Search
2383 * will resume at s->search_entry once we come back.
2385 s->thread_args.commits_needed++;
2386 return trigger_log_thread(view, 0);
2389 err = match_commit(&have_match, entry->id, entry->commit,
2390 &view->regex);
2391 if (err)
2392 break;
2393 if (have_match) {
2394 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2395 s->matched_entry = entry;
2396 break;
2399 s->search_entry = entry;
2400 if (view->searching == TOG_SEARCH_FORWARD)
2401 entry = TAILQ_NEXT(entry, entry);
2402 else
2403 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2406 if (s->matched_entry) {
2407 int cur = s->selected_entry->idx;
2408 while (cur < s->matched_entry->idx) {
2409 err = input_log_view(NULL, view, KEY_DOWN);
2410 if (err)
2411 return err;
2412 cur++;
2414 while (cur > s->matched_entry->idx) {
2415 err = input_log_view(NULL, view, KEY_UP);
2416 if (err)
2417 return err;
2418 cur--;
2422 s->search_entry = NULL;
2424 return NULL;
2427 static const struct got_error *
2428 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2429 struct got_repository *repo, const char *head_ref_name,
2430 const char *in_repo_path, int log_branches)
2432 const struct got_error *err = NULL;
2433 struct tog_log_view_state *s = &view->state.log;
2434 struct got_repository *thread_repo = NULL;
2435 struct got_commit_graph *thread_graph = NULL;
2436 int errcode;
2438 if (in_repo_path != s->in_repo_path) {
2439 free(s->in_repo_path);
2440 s->in_repo_path = strdup(in_repo_path);
2441 if (s->in_repo_path == NULL)
2442 return got_error_from_errno("strdup");
2445 /* The commit queue only contains commits being displayed. */
2446 TAILQ_INIT(&s->commits.head);
2447 s->commits.ncommits = 0;
2449 s->repo = repo;
2450 if (head_ref_name) {
2451 s->head_ref_name = strdup(head_ref_name);
2452 if (s->head_ref_name == NULL) {
2453 err = got_error_from_errno("strdup");
2454 goto done;
2457 s->start_id = got_object_id_dup(start_id);
2458 if (s->start_id == NULL) {
2459 err = got_error_from_errno("got_object_id_dup");
2460 goto done;
2462 s->log_branches = log_branches;
2464 STAILQ_INIT(&s->colors);
2465 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2466 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2467 get_color_value("TOG_COLOR_COMMIT"));
2468 if (err)
2469 goto done;
2470 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2471 get_color_value("TOG_COLOR_AUTHOR"));
2472 if (err) {
2473 free_colors(&s->colors);
2474 goto done;
2476 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2477 get_color_value("TOG_COLOR_DATE"));
2478 if (err) {
2479 free_colors(&s->colors);
2480 goto done;
2484 view->show = show_log_view;
2485 view->input = input_log_view;
2486 view->close = close_log_view;
2487 view->search_start = search_start_log_view;
2488 view->search_next = search_next_log_view;
2490 if (s->thread_args.pack_fds == NULL) {
2491 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2492 if (err)
2493 goto done;
2495 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2496 s->thread_args.pack_fds);
2497 if (err)
2498 goto done;
2499 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2500 !s->log_branches);
2501 if (err)
2502 goto done;
2503 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2504 s->repo, NULL, NULL);
2505 if (err)
2506 goto done;
2508 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2509 if (errcode) {
2510 err = got_error_set_errno(errcode, "pthread_cond_init");
2511 goto done;
2513 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2514 if (errcode) {
2515 err = got_error_set_errno(errcode, "pthread_cond_init");
2516 goto done;
2519 s->thread_args.commits_needed = view->nlines;
2520 s->thread_args.graph = thread_graph;
2521 s->thread_args.commits = &s->commits;
2522 s->thread_args.in_repo_path = s->in_repo_path;
2523 s->thread_args.start_id = s->start_id;
2524 s->thread_args.repo = thread_repo;
2525 s->thread_args.log_complete = 0;
2526 s->thread_args.quit = &s->quit;
2527 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2528 s->thread_args.selected_entry = &s->selected_entry;
2529 s->thread_args.searching = &view->searching;
2530 s->thread_args.search_next_done = &view->search_next_done;
2531 s->thread_args.regex = &view->regex;
2532 done:
2533 if (err)
2534 close_log_view(view);
2535 return err;
2538 static const struct got_error *
2539 show_log_view(struct tog_view *view)
2541 const struct got_error *err;
2542 struct tog_log_view_state *s = &view->state.log;
2544 if (s->thread == NULL) {
2545 int errcode = pthread_create(&s->thread, NULL, log_thread,
2546 &s->thread_args);
2547 if (errcode)
2548 return got_error_set_errno(errcode, "pthread_create");
2549 if (s->thread_args.commits_needed > 0) {
2550 err = trigger_log_thread(view, 1);
2551 if (err)
2552 return err;
2556 return draw_commits(view);
2559 static const struct got_error *
2560 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2562 const struct got_error *err = NULL;
2563 struct tog_log_view_state *s = &view->state.log;
2564 struct tog_view *diff_view = NULL, *tree_view = NULL;
2565 struct tog_view *ref_view = NULL;
2566 struct commit_queue_entry *entry;
2567 int begin_x = 0, n, nscroll = view->nlines - 1;
2569 if (s->thread_args.load_all) {
2570 if (ch == KEY_BACKSPACE)
2571 s->thread_args.load_all = 0;
2572 else if (s->thread_args.log_complete) {
2573 s->thread_args.load_all = 0;
2574 log_scroll_down(view, s->commits.ncommits);
2575 s->selected = MIN(view->nlines - 2,
2576 s->commits.ncommits - 1);
2577 select_commit(s);
2579 return NULL;
2582 switch (ch) {
2583 case 'q':
2584 s->quit = 1;
2585 break;
2586 case '0':
2587 view->x = 0;
2588 break;
2589 case '$':
2590 view->x = MAX(view->maxx - view->ncols / 2, 0);
2591 break;
2592 case KEY_RIGHT:
2593 case 'l':
2594 if (view->x + view->ncols / 2 < view->maxx)
2595 view->x += 2; /* move two columns right */
2596 break;
2597 case KEY_LEFT:
2598 case 'h':
2599 view->x -= MIN(view->x, 2); /* move two columns back */
2600 break;
2601 case 'k':
2602 case KEY_UP:
2603 case '<':
2604 case ',':
2605 case CTRL('p'):
2606 if (s->first_displayed_entry == NULL)
2607 break;
2608 if (s->selected > 0)
2609 s->selected--;
2610 else
2611 log_scroll_up(s, 1);
2612 select_commit(s);
2613 break;
2614 case 'g':
2615 case KEY_HOME:
2616 s->selected = 0;
2617 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2618 select_commit(s);
2619 break;
2620 case CTRL('u'):
2621 case 'u':
2622 nscroll /= 2;
2623 /* FALL THROUGH */
2624 case KEY_PPAGE:
2625 case CTRL('b'):
2626 if (s->first_displayed_entry == NULL)
2627 break;
2628 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2629 s->selected = MAX(0, s->selected - nscroll - 1);
2630 else
2631 log_scroll_up(s, nscroll);
2632 select_commit(s);
2633 break;
2634 case 'j':
2635 case KEY_DOWN:
2636 case '>':
2637 case '.':
2638 case CTRL('n'):
2639 if (s->first_displayed_entry == NULL)
2640 break;
2641 if (s->selected < MIN(view->nlines - 2,
2642 s->commits.ncommits - 1))
2643 s->selected++;
2644 else {
2645 err = log_scroll_down(view, 1);
2646 if (err)
2647 break;
2649 select_commit(s);
2650 break;
2651 case 'G':
2652 case KEY_END: {
2653 /* We don't know yet how many commits, so we're forced to
2654 * traverse them all. */
2655 if (!s->thread_args.log_complete) {
2656 s->thread_args.load_all = 1;
2657 return trigger_log_thread(view, 0);
2660 s->selected = 0;
2661 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2662 for (n = 0; n < view->nlines - 1; n++) {
2663 if (entry == NULL)
2664 break;
2665 s->first_displayed_entry = entry;
2666 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2668 if (n > 0)
2669 s->selected = n - 1;
2670 select_commit(s);
2671 break;
2673 case CTRL('d'):
2674 case 'd':
2675 nscroll /= 2;
2676 /* FALL THROUGH */
2677 case KEY_NPAGE:
2678 case CTRL('f'): {
2679 struct commit_queue_entry *first;
2680 first = s->first_displayed_entry;
2681 if (first == NULL)
2682 break;
2683 err = log_scroll_down(view, nscroll);
2684 if (err)
2685 break;
2686 if (first == s->first_displayed_entry &&
2687 s->selected < MIN(view->nlines - 2,
2688 s->commits.ncommits - 1)) {
2689 /* can't scroll further down */
2690 s->selected += MIN(s->last_displayed_entry->idx -
2691 s->selected_entry->idx, nscroll + 1);
2693 select_commit(s);
2694 break;
2696 case KEY_RESIZE:
2697 if (s->selected > view->nlines - 2)
2698 s->selected = view->nlines - 2;
2699 if (s->selected > s->commits.ncommits - 1)
2700 s->selected = s->commits.ncommits - 1;
2701 select_commit(s);
2702 if (s->commits.ncommits < view->nlines - 1 &&
2703 !s->thread_args.log_complete) {
2704 s->thread_args.commits_needed += (view->nlines - 1) -
2705 s->commits.ncommits;
2706 err = trigger_log_thread(view, 1);
2708 break;
2709 case KEY_ENTER:
2710 case ' ':
2711 case '\r':
2712 if (s->selected_entry == NULL)
2713 break;
2714 if (view_is_parent_view(view))
2715 begin_x = view_split_begin_x(view->begin_x);
2716 err = open_diff_view_for_commit(&diff_view, begin_x,
2717 s->selected_entry->commit, s->selected_entry->id,
2718 view, s->repo);
2719 if (err)
2720 break;
2721 view->focussed = 0;
2722 diff_view->focussed = 1;
2723 if (view_is_parent_view(view)) {
2724 err = view_close_child(view);
2725 if (err)
2726 return err;
2727 err = view_set_child(view, diff_view);
2728 if (err)
2729 return err;
2730 view->focus_child = 1;
2731 } else
2732 *new_view = diff_view;
2733 break;
2734 case 't':
2735 if (s->selected_entry == NULL)
2736 break;
2737 if (view_is_parent_view(view))
2738 begin_x = view_split_begin_x(view->begin_x);
2739 err = browse_commit_tree(&tree_view, begin_x,
2740 s->selected_entry, s->in_repo_path, s->head_ref_name,
2741 s->repo);
2742 if (err)
2743 break;
2744 view->focussed = 0;
2745 tree_view->focussed = 1;
2746 if (view_is_parent_view(view)) {
2747 err = view_close_child(view);
2748 if (err)
2749 return err;
2750 err = view_set_child(view, tree_view);
2751 if (err)
2752 return err;
2753 view->focus_child = 1;
2754 } else
2755 *new_view = tree_view;
2756 break;
2757 case KEY_BACKSPACE:
2758 case CTRL('l'):
2759 case 'B':
2760 if (ch == KEY_BACKSPACE &&
2761 got_path_is_root_dir(s->in_repo_path))
2762 break;
2763 err = stop_log_thread(s);
2764 if (err)
2765 return err;
2766 if (ch == KEY_BACKSPACE) {
2767 char *parent_path;
2768 err = got_path_dirname(&parent_path, s->in_repo_path);
2769 if (err)
2770 return err;
2771 free(s->in_repo_path);
2772 s->in_repo_path = parent_path;
2773 s->thread_args.in_repo_path = s->in_repo_path;
2774 } else if (ch == CTRL('l')) {
2775 struct got_object_id *start_id;
2776 err = got_repo_match_object_id(&start_id, NULL,
2777 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2778 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2779 if (err)
2780 return err;
2781 free(s->start_id);
2782 s->start_id = start_id;
2783 s->thread_args.start_id = s->start_id;
2784 } else /* 'B' */
2785 s->log_branches = !s->log_branches;
2787 if (s->thread_args.pack_fds == NULL) {
2788 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2789 if (err)
2790 return err;
2792 err = got_repo_open(&s->thread_args.repo,
2793 got_repo_get_path(s->repo), NULL,
2794 s->thread_args.pack_fds);
2795 if (err)
2796 return err;
2797 tog_free_refs();
2798 err = tog_load_refs(s->repo, 0);
2799 if (err)
2800 return err;
2801 err = got_commit_graph_open(&s->thread_args.graph,
2802 s->in_repo_path, !s->log_branches);
2803 if (err)
2804 return err;
2805 err = got_commit_graph_iter_start(s->thread_args.graph,
2806 s->start_id, s->repo, NULL, NULL);
2807 if (err)
2808 return err;
2809 free_commits(&s->commits);
2810 s->first_displayed_entry = NULL;
2811 s->last_displayed_entry = NULL;
2812 s->selected_entry = NULL;
2813 s->selected = 0;
2814 s->thread_args.log_complete = 0;
2815 s->quit = 0;
2816 s->thread_args.commits_needed = view->nlines;
2817 s->matched_entry = NULL;
2818 s->search_entry = NULL;
2819 break;
2820 case 'r':
2821 if (view_is_parent_view(view))
2822 begin_x = view_split_begin_x(view->begin_x);
2823 ref_view = view_open(view->nlines, view->ncols,
2824 view->begin_y, begin_x, TOG_VIEW_REF);
2825 if (ref_view == NULL)
2826 return got_error_from_errno("view_open");
2827 err = open_ref_view(ref_view, s->repo);
2828 if (err) {
2829 view_close(ref_view);
2830 return err;
2832 view->focussed = 0;
2833 ref_view->focussed = 1;
2834 if (view_is_parent_view(view)) {
2835 err = view_close_child(view);
2836 if (err)
2837 return err;
2838 err = view_set_child(view, ref_view);
2839 if (err)
2840 return err;
2841 view->focus_child = 1;
2842 } else
2843 *new_view = ref_view;
2844 break;
2845 default:
2846 break;
2849 return err;
2852 static const struct got_error *
2853 apply_unveil(const char *repo_path, const char *worktree_path)
2855 const struct got_error *error;
2857 #ifdef PROFILE
2858 if (unveil("gmon.out", "rwc") != 0)
2859 return got_error_from_errno2("unveil", "gmon.out");
2860 #endif
2861 if (repo_path && unveil(repo_path, "r") != 0)
2862 return got_error_from_errno2("unveil", repo_path);
2864 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2865 return got_error_from_errno2("unveil", worktree_path);
2867 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2868 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2870 error = got_privsep_unveil_exec_helpers();
2871 if (error != NULL)
2872 return error;
2874 if (unveil(NULL, NULL) != 0)
2875 return got_error_from_errno("unveil");
2877 return NULL;
2880 static void
2881 init_curses(void)
2884 * Override default signal handlers before starting ncurses.
2885 * This should prevent ncurses from installing its own
2886 * broken cleanup() signal handler.
2888 signal(SIGWINCH, tog_sigwinch);
2889 signal(SIGPIPE, tog_sigpipe);
2890 signal(SIGCONT, tog_sigcont);
2891 signal(SIGINT, tog_sigint);
2892 signal(SIGTERM, tog_sigterm);
2894 initscr();
2895 cbreak();
2896 halfdelay(1); /* Do fast refresh while initial view is loading. */
2897 noecho();
2898 nonl();
2899 intrflush(stdscr, FALSE);
2900 keypad(stdscr, TRUE);
2901 curs_set(0);
2902 if (getenv("TOG_COLORS") != NULL) {
2903 start_color();
2904 use_default_colors();
2908 static const struct got_error *
2909 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2910 struct got_repository *repo, struct got_worktree *worktree)
2912 const struct got_error *err = NULL;
2914 if (argc == 0) {
2915 *in_repo_path = strdup("/");
2916 if (*in_repo_path == NULL)
2917 return got_error_from_errno("strdup");
2918 return NULL;
2921 if (worktree) {
2922 const char *prefix = got_worktree_get_path_prefix(worktree);
2923 char *p;
2925 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2926 if (err)
2927 return err;
2928 if (asprintf(in_repo_path, "%s%s%s", prefix,
2929 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2930 p) == -1) {
2931 err = got_error_from_errno("asprintf");
2932 *in_repo_path = NULL;
2934 free(p);
2935 } else
2936 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2938 return err;
2941 static const struct got_error *
2942 cmd_log(int argc, char *argv[])
2944 const struct got_error *error;
2945 struct got_repository *repo = NULL;
2946 struct got_worktree *worktree = NULL;
2947 struct got_object_id *start_id = NULL;
2948 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2949 char *start_commit = NULL, *label = NULL;
2950 struct got_reference *ref = NULL;
2951 const char *head_ref_name = NULL;
2952 int ch, log_branches = 0;
2953 struct tog_view *view;
2954 int *pack_fds = NULL;
2956 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2957 switch (ch) {
2958 case 'b':
2959 log_branches = 1;
2960 break;
2961 case 'c':
2962 start_commit = optarg;
2963 break;
2964 case 'r':
2965 repo_path = realpath(optarg, NULL);
2966 if (repo_path == NULL)
2967 return got_error_from_errno2("realpath",
2968 optarg);
2969 break;
2970 default:
2971 usage_log();
2972 /* NOTREACHED */
2976 argc -= optind;
2977 argv += optind;
2979 if (argc > 1)
2980 usage_log();
2982 error = got_repo_pack_fds_open(&pack_fds);
2983 if (error != NULL)
2984 goto done;
2986 if (repo_path == NULL) {
2987 cwd = getcwd(NULL, 0);
2988 if (cwd == NULL)
2989 return got_error_from_errno("getcwd");
2990 error = got_worktree_open(&worktree, cwd);
2991 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2992 goto done;
2993 if (worktree)
2994 repo_path =
2995 strdup(got_worktree_get_repo_path(worktree));
2996 else
2997 repo_path = strdup(cwd);
2998 if (repo_path == NULL) {
2999 error = got_error_from_errno("strdup");
3000 goto done;
3004 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3005 if (error != NULL)
3006 goto done;
3008 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3009 repo, worktree);
3010 if (error)
3011 goto done;
3013 init_curses();
3015 error = apply_unveil(got_repo_get_path(repo),
3016 worktree ? got_worktree_get_root_path(worktree) : NULL);
3017 if (error)
3018 goto done;
3020 /* already loaded by tog_log_with_path()? */
3021 if (TAILQ_EMPTY(&tog_refs)) {
3022 error = tog_load_refs(repo, 0);
3023 if (error)
3024 goto done;
3027 if (start_commit == NULL) {
3028 error = got_repo_match_object_id(&start_id, &label,
3029 worktree ? got_worktree_get_head_ref_name(worktree) :
3030 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3031 if (error)
3032 goto done;
3033 head_ref_name = label;
3034 } else {
3035 error = got_ref_open(&ref, repo, start_commit, 0);
3036 if (error == NULL)
3037 head_ref_name = got_ref_get_name(ref);
3038 else if (error->code != GOT_ERR_NOT_REF)
3039 goto done;
3040 error = got_repo_match_object_id(&start_id, NULL,
3041 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3042 if (error)
3043 goto done;
3046 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3047 if (view == NULL) {
3048 error = got_error_from_errno("view_open");
3049 goto done;
3051 error = open_log_view(view, start_id, repo, head_ref_name,
3052 in_repo_path, log_branches);
3053 if (error)
3054 goto done;
3055 if (worktree) {
3056 /* Release work tree lock. */
3057 got_worktree_close(worktree);
3058 worktree = NULL;
3060 error = view_loop(view);
3061 done:
3062 free(in_repo_path);
3063 free(repo_path);
3064 free(cwd);
3065 free(start_id);
3066 free(label);
3067 if (ref)
3068 got_ref_close(ref);
3069 if (repo) {
3070 const struct got_error *close_err = got_repo_close(repo);
3071 if (error == NULL)
3072 error = close_err;
3074 if (worktree)
3075 got_worktree_close(worktree);
3076 if (pack_fds) {
3077 const struct got_error *pack_err =
3078 got_repo_pack_fds_close(pack_fds);
3079 if (error == NULL)
3080 error = pack_err;
3082 tog_free_refs();
3083 return error;
3086 __dead static void
3087 usage_diff(void)
3089 endwin();
3090 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3091 "[-w] object1 object2\n", getprogname());
3092 exit(1);
3095 static int
3096 match_line(const char *line, regex_t *regex, size_t nmatch,
3097 regmatch_t *regmatch)
3099 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3102 struct tog_color *
3103 match_color(struct tog_colors *colors, const char *line)
3105 struct tog_color *tc = NULL;
3107 STAILQ_FOREACH(tc, colors, entry) {
3108 if (match_line(line, &tc->regex, 0, NULL))
3109 return tc;
3112 return NULL;
3115 static const struct got_error *
3116 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3117 WINDOW *window, int skipcol, regmatch_t *regmatch)
3119 const struct got_error *err = NULL;
3120 char *exstr = NULL;
3121 wchar_t *wline = NULL;
3122 int rme, rms, n, width, scrollx;
3123 int width0 = 0, width1 = 0, width2 = 0;
3124 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3126 *wtotal = 0;
3128 rms = regmatch->rm_so;
3129 rme = regmatch->rm_eo;
3131 err = expand_tab(&exstr, line);
3132 if (err)
3133 return err;
3135 /* Split the line into 3 segments, according to match offsets. */
3136 seg0 = strndup(exstr, rms);
3137 if (seg0 == NULL) {
3138 err = got_error_from_errno("strndup");
3139 goto done;
3141 seg1 = strndup(exstr + rms, rme - rms);
3142 if (seg1 == NULL) {
3143 err = got_error_from_errno("strndup");
3144 goto done;
3146 seg2 = strdup(exstr + rme);
3147 if (seg2 == NULL) {
3148 err = got_error_from_errno("strndup");
3149 goto done;
3152 /* draw up to matched token if we haven't scrolled past it */
3153 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3154 col_tab_align, 1);
3155 if (err)
3156 goto done;
3157 n = MAX(width0 - skipcol, 0);
3158 if (n) {
3159 free(wline);
3160 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3161 wlimit, col_tab_align, 1);
3162 if (err)
3163 goto done;
3164 waddwstr(window, &wline[scrollx]);
3165 wlimit -= width;
3166 *wtotal += width;
3169 if (wlimit > 0) {
3170 int i = 0, w = 0;
3171 size_t wlen;
3173 free(wline);
3174 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3175 col_tab_align, 1);
3176 if (err)
3177 goto done;
3178 wlen = wcslen(wline);
3179 while (i < wlen) {
3180 width = wcwidth(wline[i]);
3181 if (width == -1) {
3182 /* should not happen, tabs are expanded */
3183 err = got_error(GOT_ERR_RANGE);
3184 goto done;
3186 if (width0 + w + width > skipcol)
3187 break;
3188 w += width;
3189 i++;
3191 /* draw (visible part of) matched token (if scrolled into it) */
3192 if (width1 - w > 0) {
3193 wattron(window, A_STANDOUT);
3194 waddwstr(window, &wline[i]);
3195 wattroff(window, A_STANDOUT);
3196 wlimit -= (width1 - w);
3197 *wtotal += (width1 - w);
3201 if (wlimit > 0) { /* draw rest of line */
3202 free(wline);
3203 if (skipcol > width0 + width1) {
3204 err = format_line(&wline, &width2, &scrollx, seg2,
3205 skipcol - (width0 + width1), wlimit,
3206 col_tab_align, 1);
3207 if (err)
3208 goto done;
3209 waddwstr(window, &wline[scrollx]);
3210 } else {
3211 err = format_line(&wline, &width2, NULL, seg2, 0,
3212 wlimit, col_tab_align, 1);
3213 if (err)
3214 goto done;
3215 waddwstr(window, wline);
3217 *wtotal += width2;
3219 done:
3220 free(wline);
3221 free(exstr);
3222 free(seg0);
3223 free(seg1);
3224 free(seg2);
3225 return err;
3228 static const struct got_error *
3229 draw_file(struct tog_view *view, const char *header)
3231 struct tog_diff_view_state *s = &view->state.diff;
3232 regmatch_t *regmatch = &view->regmatch;
3233 const struct got_error *err;
3234 int nprinted = 0;
3235 char *line;
3236 size_t linesize = 0;
3237 ssize_t linelen;
3238 struct tog_color *tc;
3239 wchar_t *wline;
3240 int width;
3241 int max_lines = view->nlines;
3242 int nlines = s->nlines;
3243 off_t line_offset;
3245 line_offset = s->line_offsets[s->first_displayed_line - 1];
3246 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3247 return got_error_from_errno("fseek");
3249 werase(view->window);
3251 if (header) {
3252 if (asprintf(&line, "[%d/%d] %s",
3253 s->first_displayed_line - 1 + s->selected_line, nlines,
3254 header) == -1)
3255 return got_error_from_errno("asprintf");
3256 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3257 0, 0);
3258 free(line);
3259 if (err)
3260 return err;
3262 if (view_needs_focus_indication(view))
3263 wstandout(view->window);
3264 waddwstr(view->window, wline);
3265 free(wline);
3266 wline = NULL;
3267 if (view_needs_focus_indication(view))
3268 wstandend(view->window);
3269 if (width <= view->ncols - 1)
3270 waddch(view->window, '\n');
3272 if (max_lines <= 1)
3273 return NULL;
3274 max_lines--;
3277 s->eof = 0;
3278 view->maxx = 0;
3279 line = NULL;
3280 while (max_lines > 0 && nprinted < max_lines) {
3281 linelen = getline(&line, &linesize, s->f);
3282 if (linelen == -1) {
3283 if (feof(s->f)) {
3284 s->eof = 1;
3285 break;
3287 free(line);
3288 return got_ferror(s->f, GOT_ERR_IO);
3291 /* Set view->maxx based on full line length. */
3292 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3293 view->x ? 1 : 0);
3294 if (err) {
3295 free(line);
3296 return err;
3298 view->maxx = MAX(view->maxx, width);
3299 free(wline);
3300 wline = NULL;
3302 tc = match_color(&s->colors, line);
3303 if (tc)
3304 wattr_on(view->window,
3305 COLOR_PAIR(tc->colorpair), NULL);
3306 if (s->first_displayed_line + nprinted == s->matched_line &&
3307 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3308 err = add_matched_line(&width, line, view->ncols, 0,
3309 view->window, view->x, regmatch);
3310 if (err) {
3311 free(line);
3312 return err;
3314 } else {
3315 int skip;
3316 err = format_line(&wline, &width, &skip, line,
3317 view->x, view->ncols, 0, view->x ? 1 : 0);
3318 if (err) {
3319 free(line);
3320 return err;
3322 waddwstr(view->window, &wline[skip]);
3323 free(wline);
3324 wline = NULL;
3326 if (tc)
3327 wattr_off(view->window,
3328 COLOR_PAIR(tc->colorpair), NULL);
3329 if (width <= view->ncols - 1)
3330 waddch(view->window, '\n');
3331 nprinted++;
3333 free(line);
3334 if (nprinted >= 1)
3335 s->last_displayed_line = s->first_displayed_line +
3336 (nprinted - 1);
3337 else
3338 s->last_displayed_line = s->first_displayed_line;
3340 view_vborder(view);
3342 if (s->eof) {
3343 while (nprinted < view->nlines) {
3344 waddch(view->window, '\n');
3345 nprinted++;
3348 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3349 view->ncols, 0, 0);
3350 if (err) {
3351 return err;
3354 wstandout(view->window);
3355 waddwstr(view->window, wline);
3356 free(wline);
3357 wline = NULL;
3358 wstandend(view->window);
3361 return NULL;
3364 static char *
3365 get_datestr(time_t *time, char *datebuf)
3367 struct tm mytm, *tm;
3368 char *p, *s;
3370 tm = gmtime_r(time, &mytm);
3371 if (tm == NULL)
3372 return NULL;
3373 s = asctime_r(tm, datebuf);
3374 if (s == NULL)
3375 return NULL;
3376 p = strchr(s, '\n');
3377 if (p)
3378 *p = '\0';
3379 return s;
3382 static const struct got_error *
3383 get_changed_paths(struct got_pathlist_head *paths,
3384 struct got_commit_object *commit, struct got_repository *repo)
3386 const struct got_error *err = NULL;
3387 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3388 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3389 struct got_object_qid *qid;
3391 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3392 if (qid != NULL) {
3393 struct got_commit_object *pcommit;
3394 err = got_object_open_as_commit(&pcommit, repo,
3395 &qid->id);
3396 if (err)
3397 return err;
3399 tree_id1 = got_object_id_dup(
3400 got_object_commit_get_tree_id(pcommit));
3401 if (tree_id1 == NULL) {
3402 got_object_commit_close(pcommit);
3403 return got_error_from_errno("got_object_id_dup");
3405 got_object_commit_close(pcommit);
3409 if (tree_id1) {
3410 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3411 if (err)
3412 goto done;
3415 tree_id2 = got_object_commit_get_tree_id(commit);
3416 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3417 if (err)
3418 goto done;
3420 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3421 got_diff_tree_collect_changed_paths, paths, 0);
3422 done:
3423 if (tree1)
3424 got_object_tree_close(tree1);
3425 if (tree2)
3426 got_object_tree_close(tree2);
3427 free(tree_id1);
3428 return err;
3431 static const struct got_error *
3432 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3434 off_t *p;
3436 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3437 if (p == NULL)
3438 return got_error_from_errno("reallocarray");
3439 *line_offsets = p;
3440 (*line_offsets)[*nlines] = off;
3441 (*nlines)++;
3442 return NULL;
3445 static const struct got_error *
3446 write_commit_info(off_t **line_offsets, size_t *nlines,
3447 struct got_object_id *commit_id, struct got_reflist_head *refs,
3448 struct got_repository *repo, FILE *outfile)
3450 const struct got_error *err = NULL;
3451 char datebuf[26], *datestr;
3452 struct got_commit_object *commit;
3453 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3454 time_t committer_time;
3455 const char *author, *committer;
3456 char *refs_str = NULL;
3457 struct got_pathlist_head changed_paths;
3458 struct got_pathlist_entry *pe;
3459 off_t outoff = 0;
3460 int n;
3462 TAILQ_INIT(&changed_paths);
3464 if (refs) {
3465 err = build_refs_str(&refs_str, refs, commit_id, repo);
3466 if (err)
3467 return err;
3470 err = got_object_open_as_commit(&commit, repo, commit_id);
3471 if (err)
3472 return err;
3474 err = got_object_id_str(&id_str, commit_id);
3475 if (err) {
3476 err = got_error_from_errno("got_object_id_str");
3477 goto done;
3480 err = add_line_offset(line_offsets, nlines, 0);
3481 if (err)
3482 goto done;
3484 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3485 refs_str ? refs_str : "", refs_str ? ")" : "");
3486 if (n < 0) {
3487 err = got_error_from_errno("fprintf");
3488 goto done;
3490 outoff += n;
3491 err = add_line_offset(line_offsets, nlines, outoff);
3492 if (err)
3493 goto done;
3495 n = fprintf(outfile, "from: %s\n",
3496 got_object_commit_get_author(commit));
3497 if (n < 0) {
3498 err = got_error_from_errno("fprintf");
3499 goto done;
3501 outoff += n;
3502 err = add_line_offset(line_offsets, nlines, outoff);
3503 if (err)
3504 goto done;
3506 committer_time = got_object_commit_get_committer_time(commit);
3507 datestr = get_datestr(&committer_time, datebuf);
3508 if (datestr) {
3509 n = fprintf(outfile, "date: %s UTC\n", datestr);
3510 if (n < 0) {
3511 err = got_error_from_errno("fprintf");
3512 goto done;
3514 outoff += n;
3515 err = add_line_offset(line_offsets, nlines, outoff);
3516 if (err)
3517 goto done;
3519 author = got_object_commit_get_author(commit);
3520 committer = got_object_commit_get_committer(commit);
3521 if (strcmp(author, committer) != 0) {
3522 n = fprintf(outfile, "via: %s\n", committer);
3523 if (n < 0) {
3524 err = got_error_from_errno("fprintf");
3525 goto done;
3527 outoff += n;
3528 err = add_line_offset(line_offsets, nlines, outoff);
3529 if (err)
3530 goto done;
3532 if (got_object_commit_get_nparents(commit) > 1) {
3533 const struct got_object_id_queue *parent_ids;
3534 struct got_object_qid *qid;
3535 int pn = 1;
3536 parent_ids = got_object_commit_get_parent_ids(commit);
3537 STAILQ_FOREACH(qid, parent_ids, entry) {
3538 err = got_object_id_str(&id_str, &qid->id);
3539 if (err)
3540 goto done;
3541 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3542 if (n < 0) {
3543 err = got_error_from_errno("fprintf");
3544 goto done;
3546 outoff += n;
3547 err = add_line_offset(line_offsets, nlines, outoff);
3548 if (err)
3549 goto done;
3550 free(id_str);
3551 id_str = NULL;
3555 err = got_object_commit_get_logmsg(&logmsg, commit);
3556 if (err)
3557 goto done;
3558 s = logmsg;
3559 while ((line = strsep(&s, "\n")) != NULL) {
3560 n = fprintf(outfile, "%s\n", line);
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;
3571 err = get_changed_paths(&changed_paths, commit, repo);
3572 if (err)
3573 goto done;
3574 TAILQ_FOREACH(pe, &changed_paths, entry) {
3575 struct got_diff_changed_path *cp = pe->data;
3576 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3577 if (n < 0) {
3578 err = got_error_from_errno("fprintf");
3579 goto done;
3581 outoff += n;
3582 err = add_line_offset(line_offsets, nlines, outoff);
3583 if (err)
3584 goto done;
3585 free((char *)pe->path);
3586 free(pe->data);
3589 fputc('\n', outfile);
3590 outoff++;
3591 err = add_line_offset(line_offsets, nlines, outoff);
3592 done:
3593 got_pathlist_free(&changed_paths);
3594 free(id_str);
3595 free(logmsg);
3596 free(refs_str);
3597 got_object_commit_close(commit);
3598 if (err) {
3599 free(*line_offsets);
3600 *line_offsets = NULL;
3601 *nlines = 0;
3603 return err;
3606 static const struct got_error *
3607 create_diff(struct tog_diff_view_state *s)
3609 const struct got_error *err = NULL;
3610 FILE *f = NULL;
3611 int obj_type;
3613 free(s->line_offsets);
3614 s->line_offsets = malloc(sizeof(off_t));
3615 if (s->line_offsets == NULL)
3616 return got_error_from_errno("malloc");
3617 s->nlines = 0;
3619 f = got_opentemp();
3620 if (f == NULL) {
3621 err = got_error_from_errno("got_opentemp");
3622 goto done;
3624 if (s->f && fclose(s->f) == EOF) {
3625 err = got_error_from_errno("fclose");
3626 goto done;
3628 s->f = f;
3630 if (s->id1)
3631 err = got_object_get_type(&obj_type, s->repo, s->id1);
3632 else
3633 err = got_object_get_type(&obj_type, s->repo, s->id2);
3634 if (err)
3635 goto done;
3637 switch (obj_type) {
3638 case GOT_OBJ_TYPE_BLOB:
3639 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3640 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3641 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3642 s->repo, s->f);
3643 break;
3644 case GOT_OBJ_TYPE_TREE:
3645 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3646 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3647 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3648 break;
3649 case GOT_OBJ_TYPE_COMMIT: {
3650 const struct got_object_id_queue *parent_ids;
3651 struct got_object_qid *pid;
3652 struct got_commit_object *commit2;
3653 struct got_reflist_head *refs;
3655 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3656 if (err)
3657 goto done;
3658 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3659 /* Show commit info if we're diffing to a parent/root commit. */
3660 if (s->id1 == NULL) {
3661 err = write_commit_info(&s->line_offsets, &s->nlines,
3662 s->id2, refs, s->repo, s->f);
3663 if (err)
3664 goto done;
3665 } else {
3666 parent_ids = got_object_commit_get_parent_ids(commit2);
3667 STAILQ_FOREACH(pid, parent_ids, entry) {
3668 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3669 err = write_commit_info(
3670 &s->line_offsets, &s->nlines,
3671 s->id2, refs, s->repo, s->f);
3672 if (err)
3673 goto done;
3674 break;
3678 got_object_commit_close(commit2);
3680 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3681 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3682 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3683 break;
3685 default:
3686 err = got_error(GOT_ERR_OBJ_TYPE);
3687 break;
3689 if (err)
3690 goto done;
3691 done:
3692 if (s->f && fflush(s->f) != 0 && err == NULL)
3693 err = got_error_from_errno("fflush");
3694 return err;
3697 static void
3698 diff_view_indicate_progress(struct tog_view *view)
3700 mvwaddstr(view->window, 0, 0, "diffing...");
3701 update_panels();
3702 doupdate();
3705 static const struct got_error *
3706 search_start_diff_view(struct tog_view *view)
3708 struct tog_diff_view_state *s = &view->state.diff;
3710 s->matched_line = 0;
3711 return NULL;
3714 static const struct got_error *
3715 search_next_diff_view(struct tog_view *view)
3717 struct tog_diff_view_state *s = &view->state.diff;
3718 const struct got_error *err = NULL;
3719 int lineno;
3720 char *line = NULL;
3721 size_t linesize = 0;
3722 ssize_t linelen;
3724 if (!view->searching) {
3725 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3726 return NULL;
3729 if (s->matched_line) {
3730 if (view->searching == TOG_SEARCH_FORWARD)
3731 lineno = s->matched_line + 1;
3732 else
3733 lineno = s->matched_line - 1;
3734 } else
3735 lineno = s->first_displayed_line;
3737 while (1) {
3738 off_t offset;
3740 if (lineno <= 0 || lineno > s->nlines) {
3741 if (s->matched_line == 0) {
3742 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3743 break;
3746 if (view->searching == TOG_SEARCH_FORWARD)
3747 lineno = 1;
3748 else
3749 lineno = s->nlines;
3752 offset = s->line_offsets[lineno - 1];
3753 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3754 free(line);
3755 return got_error_from_errno("fseeko");
3757 linelen = getline(&line, &linesize, s->f);
3758 if (linelen != -1) {
3759 char *exstr;
3760 err = expand_tab(&exstr, line);
3761 if (err)
3762 break;
3763 if (match_line(exstr, &view->regex, 1,
3764 &view->regmatch)) {
3765 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3766 s->matched_line = lineno;
3767 free(exstr);
3768 break;
3770 free(exstr);
3772 if (view->searching == TOG_SEARCH_FORWARD)
3773 lineno++;
3774 else
3775 lineno--;
3777 free(line);
3779 if (s->matched_line) {
3780 s->first_displayed_line = s->matched_line;
3781 s->selected_line = 1;
3784 return err;
3787 static const struct got_error *
3788 close_diff_view(struct tog_view *view)
3790 const struct got_error *err = NULL;
3791 struct tog_diff_view_state *s = &view->state.diff;
3793 free(s->id1);
3794 s->id1 = NULL;
3795 free(s->id2);
3796 s->id2 = NULL;
3797 if (s->f && fclose(s->f) == EOF)
3798 err = got_error_from_errno("fclose");
3799 s->f = NULL;
3800 if (s->f1 && fclose(s->f1) == EOF)
3801 err = got_error_from_errno("fclose");
3802 s->f1 = NULL;
3803 if (s->f2 && fclose(s->f2) == EOF)
3804 err = got_error_from_errno("fclose");
3805 s->f2 = NULL;
3806 free_colors(&s->colors);
3807 free(s->line_offsets);
3808 s->line_offsets = NULL;
3809 s->nlines = 0;
3810 return err;
3813 static const struct got_error *
3814 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3815 struct got_object_id *id2, const char *label1, const char *label2,
3816 int diff_context, int ignore_whitespace, int force_text_diff,
3817 struct tog_view *log_view, struct got_repository *repo)
3819 const struct got_error *err;
3820 struct tog_diff_view_state *s = &view->state.diff;
3822 memset(s, 0, sizeof(*s));
3824 if (id1 != NULL && id2 != NULL) {
3825 int type1, type2;
3826 err = got_object_get_type(&type1, repo, id1);
3827 if (err)
3828 return err;
3829 err = got_object_get_type(&type2, repo, id2);
3830 if (err)
3831 return err;
3833 if (type1 != type2)
3834 return got_error(GOT_ERR_OBJ_TYPE);
3836 s->first_displayed_line = 1;
3837 s->last_displayed_line = view->nlines;
3838 s->selected_line = 1;
3839 s->repo = repo;
3840 s->id1 = id1;
3841 s->id2 = id2;
3842 s->label1 = label1;
3843 s->label2 = label2;
3845 if (id1) {
3846 s->id1 = got_object_id_dup(id1);
3847 if (s->id1 == NULL)
3848 return got_error_from_errno("got_object_id_dup");
3849 } else
3850 s->id1 = NULL;
3852 s->id2 = got_object_id_dup(id2);
3853 if (s->id2 == NULL) {
3854 err = got_error_from_errno("got_object_id_dup");
3855 goto done;
3858 s->f1 = got_opentemp();
3859 if (s->f1 == NULL) {
3860 err = got_error_from_errno("got_opentemp");
3861 goto done;
3864 s->f2 = got_opentemp();
3865 if (s->f2 == NULL) {
3866 err = got_error_from_errno("got_opentemp");
3867 goto done;
3870 s->first_displayed_line = 1;
3871 s->last_displayed_line = view->nlines;
3872 s->diff_context = diff_context;
3873 s->ignore_whitespace = ignore_whitespace;
3874 s->force_text_diff = force_text_diff;
3875 s->log_view = log_view;
3876 s->repo = repo;
3878 STAILQ_INIT(&s->colors);
3879 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3880 err = add_color(&s->colors,
3881 "^-", TOG_COLOR_DIFF_MINUS,
3882 get_color_value("TOG_COLOR_DIFF_MINUS"));
3883 if (err)
3884 goto done;
3885 err = add_color(&s->colors, "^\\+",
3886 TOG_COLOR_DIFF_PLUS,
3887 get_color_value("TOG_COLOR_DIFF_PLUS"));
3888 if (err)
3889 goto done;
3890 err = add_color(&s->colors,
3891 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3892 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3893 if (err)
3894 goto done;
3896 err = add_color(&s->colors,
3897 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3898 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3899 get_color_value("TOG_COLOR_DIFF_META"));
3900 if (err)
3901 goto done;
3903 err = add_color(&s->colors,
3904 "^(from|via): ", TOG_COLOR_AUTHOR,
3905 get_color_value("TOG_COLOR_AUTHOR"));
3906 if (err)
3907 goto done;
3909 err = add_color(&s->colors,
3910 "^date: ", TOG_COLOR_DATE,
3911 get_color_value("TOG_COLOR_DATE"));
3912 if (err)
3913 goto done;
3916 if (log_view && view_is_splitscreen(view))
3917 show_log_view(log_view); /* draw vborder */
3918 diff_view_indicate_progress(view);
3920 err = create_diff(s);
3922 view->show = show_diff_view;
3923 view->input = input_diff_view;
3924 view->close = close_diff_view;
3925 view->search_start = search_start_diff_view;
3926 view->search_next = search_next_diff_view;
3927 done:
3928 if (err)
3929 close_diff_view(view);
3930 return err;
3933 static const struct got_error *
3934 show_diff_view(struct tog_view *view)
3936 const struct got_error *err;
3937 struct tog_diff_view_state *s = &view->state.diff;
3938 char *id_str1 = NULL, *id_str2, *header;
3939 const char *label1, *label2;
3941 if (s->id1) {
3942 err = got_object_id_str(&id_str1, s->id1);
3943 if (err)
3944 return err;
3945 label1 = s->label1 ? : id_str1;
3946 } else
3947 label1 = "/dev/null";
3949 err = got_object_id_str(&id_str2, s->id2);
3950 if (err)
3951 return err;
3952 label2 = s->label2 ? : id_str2;
3954 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3955 err = got_error_from_errno("asprintf");
3956 free(id_str1);
3957 free(id_str2);
3958 return err;
3960 free(id_str1);
3961 free(id_str2);
3963 err = draw_file(view, header);
3964 free(header);
3965 return err;
3968 static const struct got_error *
3969 set_selected_commit(struct tog_diff_view_state *s,
3970 struct commit_queue_entry *entry)
3972 const struct got_error *err;
3973 const struct got_object_id_queue *parent_ids;
3974 struct got_commit_object *selected_commit;
3975 struct got_object_qid *pid;
3977 free(s->id2);
3978 s->id2 = got_object_id_dup(entry->id);
3979 if (s->id2 == NULL)
3980 return got_error_from_errno("got_object_id_dup");
3982 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3983 if (err)
3984 return err;
3985 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3986 free(s->id1);
3987 pid = STAILQ_FIRST(parent_ids);
3988 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3989 got_object_commit_close(selected_commit);
3990 return NULL;
3993 static const struct got_error *
3994 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3996 const struct got_error *err = NULL;
3997 struct tog_diff_view_state *s = &view->state.diff;
3998 struct tog_log_view_state *ls;
3999 struct commit_queue_entry *old_selected_entry;
4000 char *line = NULL;
4001 size_t linesize = 0;
4002 ssize_t linelen;
4003 int i, nscroll = view->nlines - 1;
4005 switch (ch) {
4006 case '0':
4007 view->x = 0;
4008 break;
4009 case '$':
4010 view->x = MAX(view->maxx - view->ncols / 3, 0);
4011 break;
4012 case KEY_RIGHT:
4013 case 'l':
4014 if (view->x + view->ncols / 3 < view->maxx)
4015 view->x += 2; /* move two columns right */
4016 break;
4017 case KEY_LEFT:
4018 case 'h':
4019 view->x -= MIN(view->x, 2); /* move two columns back */
4020 break;
4021 case 'a':
4022 case 'w':
4023 if (ch == 'a')
4024 s->force_text_diff = !s->force_text_diff;
4025 if (ch == 'w')
4026 s->ignore_whitespace = !s->ignore_whitespace;
4027 wclear(view->window);
4028 s->first_displayed_line = 1;
4029 s->last_displayed_line = view->nlines;
4030 s->matched_line = 0;
4031 diff_view_indicate_progress(view);
4032 err = create_diff(s);
4033 break;
4034 case 'g':
4035 case KEY_HOME:
4036 s->first_displayed_line = 1;
4037 break;
4038 case 'G':
4039 case KEY_END:
4040 if (s->eof)
4041 break;
4043 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4044 s->eof = 1;
4045 break;
4046 case 'k':
4047 case KEY_UP:
4048 case CTRL('p'):
4049 if (s->first_displayed_line > 1)
4050 s->first_displayed_line--;
4051 break;
4052 case CTRL('u'):
4053 case 'u':
4054 nscroll /= 2;
4055 /* FALL THROUGH */
4056 case KEY_PPAGE:
4057 case CTRL('b'):
4058 if (s->first_displayed_line == 1)
4059 break;
4060 i = 0;
4061 while (i++ < nscroll && s->first_displayed_line > 1)
4062 s->first_displayed_line--;
4063 break;
4064 case 'j':
4065 case KEY_DOWN:
4066 case CTRL('n'):
4067 if (!s->eof)
4068 s->first_displayed_line++;
4069 break;
4070 case CTRL('d'):
4071 case 'd':
4072 nscroll /= 2;
4073 /* FALL THROUGH */
4074 case KEY_NPAGE:
4075 case CTRL('f'):
4076 case ' ':
4077 if (s->eof)
4078 break;
4079 i = 0;
4080 while (!s->eof && i++ < nscroll) {
4081 linelen = getline(&line, &linesize, s->f);
4082 s->first_displayed_line++;
4083 if (linelen == -1) {
4084 if (feof(s->f)) {
4085 s->eof = 1;
4086 } else
4087 err = got_ferror(s->f, GOT_ERR_IO);
4088 break;
4091 free(line);
4092 break;
4093 case '[':
4094 if (s->diff_context > 0) {
4095 s->diff_context--;
4096 s->matched_line = 0;
4097 diff_view_indicate_progress(view);
4098 err = create_diff(s);
4099 if (s->first_displayed_line + view->nlines - 1 >
4100 s->nlines) {
4101 s->first_displayed_line = 1;
4102 s->last_displayed_line = view->nlines;
4105 break;
4106 case ']':
4107 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4108 s->diff_context++;
4109 s->matched_line = 0;
4110 diff_view_indicate_progress(view);
4111 err = create_diff(s);
4113 break;
4114 case '<':
4115 case ',':
4116 if (s->log_view == NULL)
4117 break;
4118 ls = &s->log_view->state.log;
4119 old_selected_entry = ls->selected_entry;
4121 err = input_log_view(NULL, s->log_view, KEY_UP);
4122 if (err)
4123 break;
4125 if (old_selected_entry == ls->selected_entry)
4126 break;
4128 err = set_selected_commit(s, ls->selected_entry);
4129 if (err)
4130 break;
4132 s->first_displayed_line = 1;
4133 s->last_displayed_line = view->nlines;
4134 s->matched_line = 0;
4135 view->x = 0;
4137 diff_view_indicate_progress(view);
4138 err = create_diff(s);
4139 break;
4140 case '>':
4141 case '.':
4142 if (s->log_view == NULL)
4143 break;
4144 ls = &s->log_view->state.log;
4145 old_selected_entry = ls->selected_entry;
4147 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4148 if (err)
4149 break;
4151 if (old_selected_entry == ls->selected_entry)
4152 break;
4154 err = set_selected_commit(s, ls->selected_entry);
4155 if (err)
4156 break;
4158 s->first_displayed_line = 1;
4159 s->last_displayed_line = view->nlines;
4160 s->matched_line = 0;
4161 view->x = 0;
4163 diff_view_indicate_progress(view);
4164 err = create_diff(s);
4165 break;
4166 default:
4167 break;
4170 return err;
4173 static const struct got_error *
4174 cmd_diff(int argc, char *argv[])
4176 const struct got_error *error = NULL;
4177 struct got_repository *repo = NULL;
4178 struct got_worktree *worktree = NULL;
4179 struct got_object_id *id1 = NULL, *id2 = NULL;
4180 char *repo_path = NULL, *cwd = NULL;
4181 char *id_str1 = NULL, *id_str2 = NULL;
4182 char *label1 = NULL, *label2 = NULL;
4183 int diff_context = 3, ignore_whitespace = 0;
4184 int ch, force_text_diff = 0;
4185 const char *errstr;
4186 struct tog_view *view;
4187 int *pack_fds = NULL;
4189 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4190 switch (ch) {
4191 case 'a':
4192 force_text_diff = 1;
4193 break;
4194 case 'C':
4195 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4196 &errstr);
4197 if (errstr != NULL)
4198 errx(1, "number of context lines is %s: %s",
4199 errstr, errstr);
4200 break;
4201 case 'r':
4202 repo_path = realpath(optarg, NULL);
4203 if (repo_path == NULL)
4204 return got_error_from_errno2("realpath",
4205 optarg);
4206 got_path_strip_trailing_slashes(repo_path);
4207 break;
4208 case 'w':
4209 ignore_whitespace = 1;
4210 break;
4211 default:
4212 usage_diff();
4213 /* NOTREACHED */
4217 argc -= optind;
4218 argv += optind;
4220 if (argc == 0) {
4221 usage_diff(); /* TODO show local worktree changes */
4222 } else if (argc == 2) {
4223 id_str1 = argv[0];
4224 id_str2 = argv[1];
4225 } else
4226 usage_diff();
4228 error = got_repo_pack_fds_open(&pack_fds);
4229 if (error)
4230 goto done;
4232 if (repo_path == NULL) {
4233 cwd = getcwd(NULL, 0);
4234 if (cwd == NULL)
4235 return got_error_from_errno("getcwd");
4236 error = got_worktree_open(&worktree, cwd);
4237 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4238 goto done;
4239 if (worktree)
4240 repo_path =
4241 strdup(got_worktree_get_repo_path(worktree));
4242 else
4243 repo_path = strdup(cwd);
4244 if (repo_path == NULL) {
4245 error = got_error_from_errno("strdup");
4246 goto done;
4250 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4251 if (error)
4252 goto done;
4254 init_curses();
4256 error = apply_unveil(got_repo_get_path(repo), NULL);
4257 if (error)
4258 goto done;
4260 error = tog_load_refs(repo, 0);
4261 if (error)
4262 goto done;
4264 error = got_repo_match_object_id(&id1, &label1, id_str1,
4265 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4266 if (error)
4267 goto done;
4269 error = got_repo_match_object_id(&id2, &label2, id_str2,
4270 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4271 if (error)
4272 goto done;
4274 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4275 if (view == NULL) {
4276 error = got_error_from_errno("view_open");
4277 goto done;
4279 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4280 ignore_whitespace, force_text_diff, NULL, repo);
4281 if (error)
4282 goto done;
4283 error = view_loop(view);
4284 done:
4285 free(label1);
4286 free(label2);
4287 free(repo_path);
4288 free(cwd);
4289 if (repo) {
4290 const struct got_error *close_err = got_repo_close(repo);
4291 if (error == NULL)
4292 error = close_err;
4294 if (worktree)
4295 got_worktree_close(worktree);
4296 if (pack_fds) {
4297 const struct got_error *pack_err =
4298 got_repo_pack_fds_close(pack_fds);
4299 if (error == NULL)
4300 error = pack_err;
4302 tog_free_refs();
4303 return error;
4306 __dead static void
4307 usage_blame(void)
4309 endwin();
4310 fprintf(stderr,
4311 "usage: %s blame [-c commit] [-r repository-path] path\n",
4312 getprogname());
4313 exit(1);
4316 struct tog_blame_line {
4317 int annotated;
4318 struct got_object_id *id;
4321 static const struct got_error *
4322 draw_blame(struct tog_view *view)
4324 struct tog_blame_view_state *s = &view->state.blame;
4325 struct tog_blame *blame = &s->blame;
4326 regmatch_t *regmatch = &view->regmatch;
4327 const struct got_error *err;
4328 int lineno = 0, nprinted = 0;
4329 char *line = NULL;
4330 size_t linesize = 0;
4331 ssize_t linelen;
4332 wchar_t *wline;
4333 int width;
4334 struct tog_blame_line *blame_line;
4335 struct got_object_id *prev_id = NULL;
4336 char *id_str;
4337 struct tog_color *tc;
4339 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4340 if (err)
4341 return err;
4343 rewind(blame->f);
4344 werase(view->window);
4346 if (asprintf(&line, "commit %s", id_str) == -1) {
4347 err = got_error_from_errno("asprintf");
4348 free(id_str);
4349 return err;
4352 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4353 free(line);
4354 line = NULL;
4355 if (err)
4356 return err;
4357 if (view_needs_focus_indication(view))
4358 wstandout(view->window);
4359 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4360 if (tc)
4361 wattr_on(view->window,
4362 COLOR_PAIR(tc->colorpair), NULL);
4363 waddwstr(view->window, wline);
4364 if (tc)
4365 wattr_off(view->window,
4366 COLOR_PAIR(tc->colorpair), NULL);
4367 if (view_needs_focus_indication(view))
4368 wstandend(view->window);
4369 free(wline);
4370 wline = NULL;
4371 if (width < view->ncols - 1)
4372 waddch(view->window, '\n');
4374 if (asprintf(&line, "[%d/%d] %s%s",
4375 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4376 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4377 free(id_str);
4378 return got_error_from_errno("asprintf");
4380 free(id_str);
4381 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4382 free(line);
4383 line = NULL;
4384 if (err)
4385 return err;
4386 waddwstr(view->window, wline);
4387 free(wline);
4388 wline = NULL;
4389 if (width < view->ncols - 1)
4390 waddch(view->window, '\n');
4392 s->eof = 0;
4393 view->maxx = 0;
4394 while (nprinted < view->nlines - 2) {
4395 linelen = getline(&line, &linesize, blame->f);
4396 if (linelen == -1) {
4397 if (feof(blame->f)) {
4398 s->eof = 1;
4399 break;
4401 free(line);
4402 return got_ferror(blame->f, GOT_ERR_IO);
4404 if (++lineno < s->first_displayed_line)
4405 continue;
4407 /* Set view->maxx based on full line length. */
4408 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4409 if (err) {
4410 free(line);
4411 return err;
4413 free(wline);
4414 wline = NULL;
4415 view->maxx = MAX(view->maxx, width);
4417 if (view->focussed && nprinted == s->selected_line - 1)
4418 wstandout(view->window);
4420 if (blame->nlines > 0) {
4421 blame_line = &blame->lines[lineno - 1];
4422 if (blame_line->annotated && prev_id &&
4423 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4424 !(view->focussed &&
4425 nprinted == s->selected_line - 1)) {
4426 waddstr(view->window, " ");
4427 } else if (blame_line->annotated) {
4428 char *id_str;
4429 err = got_object_id_str(&id_str,
4430 blame_line->id);
4431 if (err) {
4432 free(line);
4433 return err;
4435 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4436 if (tc)
4437 wattr_on(view->window,
4438 COLOR_PAIR(tc->colorpair), NULL);
4439 wprintw(view->window, "%.8s", id_str);
4440 if (tc)
4441 wattr_off(view->window,
4442 COLOR_PAIR(tc->colorpair), NULL);
4443 free(id_str);
4444 prev_id = blame_line->id;
4445 } else {
4446 waddstr(view->window, "........");
4447 prev_id = NULL;
4449 } else {
4450 waddstr(view->window, "........");
4451 prev_id = NULL;
4454 if (view->focussed && nprinted == s->selected_line - 1)
4455 wstandend(view->window);
4456 waddstr(view->window, " ");
4458 if (view->ncols <= 9) {
4459 width = 9;
4460 } else if (s->first_displayed_line + nprinted ==
4461 s->matched_line &&
4462 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4463 err = add_matched_line(&width, line, view->ncols - 9, 9,
4464 view->window, view->x, regmatch);
4465 if (err) {
4466 free(line);
4467 return err;
4469 width += 9;
4470 } else {
4471 int skip;
4472 err = format_line(&wline, &width, &skip, line,
4473 view->x, view->ncols - 9, 9, 1);
4474 if (err) {
4475 free(line);
4476 return err;
4478 waddwstr(view->window, &wline[skip]);
4479 width += 9;
4480 free(wline);
4481 wline = NULL;
4484 if (width <= view->ncols - 1)
4485 waddch(view->window, '\n');
4486 if (++nprinted == 1)
4487 s->first_displayed_line = lineno;
4489 free(line);
4490 s->last_displayed_line = lineno;
4492 view_vborder(view);
4494 return NULL;
4497 static const struct got_error *
4498 blame_cb(void *arg, int nlines, int lineno,
4499 struct got_commit_object *commit, struct got_object_id *id)
4501 const struct got_error *err = NULL;
4502 struct tog_blame_cb_args *a = arg;
4503 struct tog_blame_line *line;
4504 int errcode;
4506 if (nlines != a->nlines ||
4507 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4508 return got_error(GOT_ERR_RANGE);
4510 errcode = pthread_mutex_lock(&tog_mutex);
4511 if (errcode)
4512 return got_error_set_errno(errcode, "pthread_mutex_lock");
4514 if (*a->quit) { /* user has quit the blame view */
4515 err = got_error(GOT_ERR_ITER_COMPLETED);
4516 goto done;
4519 if (lineno == -1)
4520 goto done; /* no change in this commit */
4522 line = &a->lines[lineno - 1];
4523 if (line->annotated)
4524 goto done;
4526 line->id = got_object_id_dup(id);
4527 if (line->id == NULL) {
4528 err = got_error_from_errno("got_object_id_dup");
4529 goto done;
4531 line->annotated = 1;
4532 done:
4533 errcode = pthread_mutex_unlock(&tog_mutex);
4534 if (errcode)
4535 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4536 return err;
4539 static void *
4540 blame_thread(void *arg)
4542 const struct got_error *err, *close_err;
4543 struct tog_blame_thread_args *ta = arg;
4544 struct tog_blame_cb_args *a = ta->cb_args;
4545 int errcode;
4547 err = block_signals_used_by_main_thread();
4548 if (err)
4549 return (void *)err;
4551 err = got_blame(ta->path, a->commit_id, ta->repo,
4552 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4553 if (err && err->code == GOT_ERR_CANCELLED)
4554 err = NULL;
4556 errcode = pthread_mutex_lock(&tog_mutex);
4557 if (errcode)
4558 return (void *)got_error_set_errno(errcode,
4559 "pthread_mutex_lock");
4561 close_err = got_repo_close(ta->repo);
4562 if (err == NULL)
4563 err = close_err;
4564 ta->repo = NULL;
4565 *ta->complete = 1;
4567 errcode = pthread_mutex_unlock(&tog_mutex);
4568 if (errcode && err == NULL)
4569 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4571 return (void *)err;
4574 static struct got_object_id *
4575 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4576 int first_displayed_line, int selected_line)
4578 struct tog_blame_line *line;
4580 if (nlines <= 0)
4581 return NULL;
4583 line = &lines[first_displayed_line - 1 + selected_line - 1];
4584 if (!line->annotated)
4585 return NULL;
4587 return line->id;
4590 static const struct got_error *
4591 stop_blame(struct tog_blame *blame)
4593 const struct got_error *err = NULL;
4594 int i;
4596 if (blame->thread) {
4597 int errcode;
4598 errcode = pthread_mutex_unlock(&tog_mutex);
4599 if (errcode)
4600 return got_error_set_errno(errcode,
4601 "pthread_mutex_unlock");
4602 errcode = pthread_join(blame->thread, (void **)&err);
4603 if (errcode)
4604 return got_error_set_errno(errcode, "pthread_join");
4605 errcode = pthread_mutex_lock(&tog_mutex);
4606 if (errcode)
4607 return got_error_set_errno(errcode,
4608 "pthread_mutex_lock");
4609 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4610 err = NULL;
4611 blame->thread = NULL;
4613 if (blame->thread_args.repo) {
4614 const struct got_error *close_err;
4615 close_err = got_repo_close(blame->thread_args.repo);
4616 if (err == NULL)
4617 err = close_err;
4618 blame->thread_args.repo = NULL;
4620 if (blame->f) {
4621 if (fclose(blame->f) == EOF && err == NULL)
4622 err = got_error_from_errno("fclose");
4623 blame->f = NULL;
4625 if (blame->lines) {
4626 for (i = 0; i < blame->nlines; i++)
4627 free(blame->lines[i].id);
4628 free(blame->lines);
4629 blame->lines = NULL;
4631 free(blame->cb_args.commit_id);
4632 blame->cb_args.commit_id = NULL;
4633 if (blame->pack_fds) {
4634 const struct got_error *pack_err =
4635 got_repo_pack_fds_close(blame->pack_fds);
4636 if (err == NULL)
4637 err = pack_err;
4638 blame->pack_fds = NULL;
4640 return err;
4643 static const struct got_error *
4644 cancel_blame_view(void *arg)
4646 const struct got_error *err = NULL;
4647 int *done = arg;
4648 int errcode;
4650 errcode = pthread_mutex_lock(&tog_mutex);
4651 if (errcode)
4652 return got_error_set_errno(errcode,
4653 "pthread_mutex_unlock");
4655 if (*done)
4656 err = got_error(GOT_ERR_CANCELLED);
4658 errcode = pthread_mutex_unlock(&tog_mutex);
4659 if (errcode)
4660 return got_error_set_errno(errcode,
4661 "pthread_mutex_lock");
4663 return err;
4666 static const struct got_error *
4667 run_blame(struct tog_view *view)
4669 struct tog_blame_view_state *s = &view->state.blame;
4670 struct tog_blame *blame = &s->blame;
4671 const struct got_error *err = NULL;
4672 struct got_commit_object *commit = NULL;
4673 struct got_blob_object *blob = NULL;
4674 struct got_repository *thread_repo = NULL;
4675 struct got_object_id *obj_id = NULL;
4676 int obj_type;
4677 int *pack_fds = NULL;
4679 err = got_object_open_as_commit(&commit, s->repo,
4680 &s->blamed_commit->id);
4681 if (err)
4682 return err;
4684 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4685 if (err)
4686 goto done;
4688 err = got_object_get_type(&obj_type, s->repo, obj_id);
4689 if (err)
4690 goto done;
4692 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4693 err = got_error(GOT_ERR_OBJ_TYPE);
4694 goto done;
4697 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4698 if (err)
4699 goto done;
4700 blame->f = got_opentemp();
4701 if (blame->f == NULL) {
4702 err = got_error_from_errno("got_opentemp");
4703 goto done;
4705 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4706 &blame->line_offsets, blame->f, blob);
4707 if (err)
4708 goto done;
4709 if (blame->nlines == 0) {
4710 s->blame_complete = 1;
4711 goto done;
4714 /* Don't include \n at EOF in the blame line count. */
4715 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4716 blame->nlines--;
4718 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4719 if (blame->lines == NULL) {
4720 err = got_error_from_errno("calloc");
4721 goto done;
4724 err = got_repo_pack_fds_open(&pack_fds);
4725 if (err)
4726 goto done;
4727 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4728 pack_fds);
4729 if (err)
4730 goto done;
4732 blame->pack_fds = pack_fds;
4733 blame->cb_args.view = view;
4734 blame->cb_args.lines = blame->lines;
4735 blame->cb_args.nlines = blame->nlines;
4736 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4737 if (blame->cb_args.commit_id == NULL) {
4738 err = got_error_from_errno("got_object_id_dup");
4739 goto done;
4741 blame->cb_args.quit = &s->done;
4743 blame->thread_args.path = s->path;
4744 blame->thread_args.repo = thread_repo;
4745 blame->thread_args.cb_args = &blame->cb_args;
4746 blame->thread_args.complete = &s->blame_complete;
4747 blame->thread_args.cancel_cb = cancel_blame_view;
4748 blame->thread_args.cancel_arg = &s->done;
4749 s->blame_complete = 0;
4751 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4752 s->first_displayed_line = 1;
4753 s->last_displayed_line = view->nlines;
4754 s->selected_line = 1;
4756 s->matched_line = 0;
4758 done:
4759 if (commit)
4760 got_object_commit_close(commit);
4761 if (blob)
4762 got_object_blob_close(blob);
4763 free(obj_id);
4764 if (err)
4765 stop_blame(blame);
4766 return err;
4769 static const struct got_error *
4770 open_blame_view(struct tog_view *view, char *path,
4771 struct got_object_id *commit_id, struct got_repository *repo)
4773 const struct got_error *err = NULL;
4774 struct tog_blame_view_state *s = &view->state.blame;
4776 STAILQ_INIT(&s->blamed_commits);
4778 s->path = strdup(path);
4779 if (s->path == NULL)
4780 return got_error_from_errno("strdup");
4782 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4783 if (err) {
4784 free(s->path);
4785 return err;
4788 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4789 s->first_displayed_line = 1;
4790 s->last_displayed_line = view->nlines;
4791 s->selected_line = 1;
4792 s->blame_complete = 0;
4793 s->repo = repo;
4794 s->commit_id = commit_id;
4795 memset(&s->blame, 0, sizeof(s->blame));
4797 STAILQ_INIT(&s->colors);
4798 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4799 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4800 get_color_value("TOG_COLOR_COMMIT"));
4801 if (err)
4802 return err;
4805 view->show = show_blame_view;
4806 view->input = input_blame_view;
4807 view->close = close_blame_view;
4808 view->search_start = search_start_blame_view;
4809 view->search_next = search_next_blame_view;
4811 return run_blame(view);
4814 static const struct got_error *
4815 close_blame_view(struct tog_view *view)
4817 const struct got_error *err = NULL;
4818 struct tog_blame_view_state *s = &view->state.blame;
4820 if (s->blame.thread)
4821 err = stop_blame(&s->blame);
4823 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4824 struct got_object_qid *blamed_commit;
4825 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4826 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4827 got_object_qid_free(blamed_commit);
4830 free(s->path);
4831 free_colors(&s->colors);
4832 return err;
4835 static const struct got_error *
4836 search_start_blame_view(struct tog_view *view)
4838 struct tog_blame_view_state *s = &view->state.blame;
4840 s->matched_line = 0;
4841 return NULL;
4844 static const struct got_error *
4845 search_next_blame_view(struct tog_view *view)
4847 struct tog_blame_view_state *s = &view->state.blame;
4848 const struct got_error *err = NULL;
4849 int lineno;
4850 char *line = NULL;
4851 size_t linesize = 0;
4852 ssize_t linelen;
4854 if (!view->searching) {
4855 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4856 return NULL;
4859 if (s->matched_line) {
4860 if (view->searching == TOG_SEARCH_FORWARD)
4861 lineno = s->matched_line + 1;
4862 else
4863 lineno = s->matched_line - 1;
4864 } else
4865 lineno = s->first_displayed_line - 1 + s->selected_line;
4867 while (1) {
4868 off_t offset;
4870 if (lineno <= 0 || lineno > s->blame.nlines) {
4871 if (s->matched_line == 0) {
4872 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4873 break;
4876 if (view->searching == TOG_SEARCH_FORWARD)
4877 lineno = 1;
4878 else
4879 lineno = s->blame.nlines;
4882 offset = s->blame.line_offsets[lineno - 1];
4883 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4884 free(line);
4885 return got_error_from_errno("fseeko");
4887 linelen = getline(&line, &linesize, s->blame.f);
4888 if (linelen != -1) {
4889 char *exstr;
4890 err = expand_tab(&exstr, line);
4891 if (err)
4892 break;
4893 if (match_line(exstr, &view->regex, 1,
4894 &view->regmatch)) {
4895 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4896 s->matched_line = lineno;
4897 free(exstr);
4898 break;
4900 free(exstr);
4902 if (view->searching == TOG_SEARCH_FORWARD)
4903 lineno++;
4904 else
4905 lineno--;
4907 free(line);
4909 if (s->matched_line) {
4910 s->first_displayed_line = s->matched_line;
4911 s->selected_line = 1;
4914 return err;
4917 static const struct got_error *
4918 show_blame_view(struct tog_view *view)
4920 const struct got_error *err = NULL;
4921 struct tog_blame_view_state *s = &view->state.blame;
4922 int errcode;
4924 if (s->blame.thread == NULL && !s->blame_complete) {
4925 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4926 &s->blame.thread_args);
4927 if (errcode)
4928 return got_error_set_errno(errcode, "pthread_create");
4930 halfdelay(1); /* fast refresh while annotating */
4933 if (s->blame_complete)
4934 halfdelay(10); /* disable fast refresh */
4936 err = draw_blame(view);
4938 view_vborder(view);
4939 return err;
4942 static const struct got_error *
4943 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4945 const struct got_error *err = NULL, *thread_err = NULL;
4946 struct tog_view *diff_view;
4947 struct tog_blame_view_state *s = &view->state.blame;
4948 int begin_x = 0, nscroll = view->nlines - 2;
4950 switch (ch) {
4951 case '0':
4952 view->x = 0;
4953 break;
4954 case '$':
4955 view->x = MAX(view->maxx - view->ncols / 3, 0);
4956 break;
4957 case KEY_RIGHT:
4958 case 'l':
4959 if (view->x + view->ncols / 3 < view->maxx)
4960 view->x += 2; /* move two columns right */
4961 break;
4962 case KEY_LEFT:
4963 case 'h':
4964 view->x -= MIN(view->x, 2); /* move two columns back */
4965 break;
4966 case 'q':
4967 s->done = 1;
4968 break;
4969 case 'g':
4970 case KEY_HOME:
4971 s->selected_line = 1;
4972 s->first_displayed_line = 1;
4973 break;
4974 case 'G':
4975 case KEY_END:
4976 if (s->blame.nlines < view->nlines - 2) {
4977 s->selected_line = s->blame.nlines;
4978 s->first_displayed_line = 1;
4979 } else {
4980 s->selected_line = view->nlines - 2;
4981 s->first_displayed_line = s->blame.nlines -
4982 (view->nlines - 3);
4984 break;
4985 case 'k':
4986 case KEY_UP:
4987 case CTRL('p'):
4988 if (s->selected_line > 1)
4989 s->selected_line--;
4990 else if (s->selected_line == 1 &&
4991 s->first_displayed_line > 1)
4992 s->first_displayed_line--;
4993 break;
4994 case CTRL('u'):
4995 case 'u':
4996 nscroll /= 2;
4997 /* FALL THROUGH */
4998 case KEY_PPAGE:
4999 case CTRL('b'):
5000 if (s->first_displayed_line == 1) {
5001 s->selected_line = MAX(1, s->selected_line - nscroll);
5002 break;
5004 if (s->first_displayed_line > nscroll)
5005 s->first_displayed_line -= nscroll;
5006 else
5007 s->first_displayed_line = 1;
5008 break;
5009 case 'j':
5010 case KEY_DOWN:
5011 case CTRL('n'):
5012 if (s->selected_line < view->nlines - 2 &&
5013 s->first_displayed_line +
5014 s->selected_line <= s->blame.nlines)
5015 s->selected_line++;
5016 else if (s->last_displayed_line <
5017 s->blame.nlines)
5018 s->first_displayed_line++;
5019 break;
5020 case 'b':
5021 case 'p': {
5022 struct got_object_id *id = NULL;
5023 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5024 s->first_displayed_line, s->selected_line);
5025 if (id == NULL)
5026 break;
5027 if (ch == 'p') {
5028 struct got_commit_object *commit, *pcommit;
5029 struct got_object_qid *pid;
5030 struct got_object_id *blob_id = NULL;
5031 int obj_type;
5032 err = got_object_open_as_commit(&commit,
5033 s->repo, id);
5034 if (err)
5035 break;
5036 pid = STAILQ_FIRST(
5037 got_object_commit_get_parent_ids(commit));
5038 if (pid == NULL) {
5039 got_object_commit_close(commit);
5040 break;
5042 /* Check if path history ends here. */
5043 err = got_object_open_as_commit(&pcommit,
5044 s->repo, &pid->id);
5045 if (err)
5046 break;
5047 err = got_object_id_by_path(&blob_id, s->repo,
5048 pcommit, s->path);
5049 got_object_commit_close(pcommit);
5050 if (err) {
5051 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5052 err = NULL;
5053 got_object_commit_close(commit);
5054 break;
5056 err = got_object_get_type(&obj_type, s->repo,
5057 blob_id);
5058 free(blob_id);
5059 /* Can't blame non-blob type objects. */
5060 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5061 got_object_commit_close(commit);
5062 break;
5064 err = got_object_qid_alloc(&s->blamed_commit,
5065 &pid->id);
5066 got_object_commit_close(commit);
5067 } else {
5068 if (got_object_id_cmp(id,
5069 &s->blamed_commit->id) == 0)
5070 break;
5071 err = got_object_qid_alloc(&s->blamed_commit,
5072 id);
5074 if (err)
5075 break;
5076 s->done = 1;
5077 thread_err = stop_blame(&s->blame);
5078 s->done = 0;
5079 if (thread_err)
5080 break;
5081 STAILQ_INSERT_HEAD(&s->blamed_commits,
5082 s->blamed_commit, entry);
5083 err = run_blame(view);
5084 if (err)
5085 break;
5086 break;
5088 case 'B': {
5089 struct got_object_qid *first;
5090 first = STAILQ_FIRST(&s->blamed_commits);
5091 if (!got_object_id_cmp(&first->id, s->commit_id))
5092 break;
5093 s->done = 1;
5094 thread_err = stop_blame(&s->blame);
5095 s->done = 0;
5096 if (thread_err)
5097 break;
5098 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5099 got_object_qid_free(s->blamed_commit);
5100 s->blamed_commit =
5101 STAILQ_FIRST(&s->blamed_commits);
5102 err = run_blame(view);
5103 if (err)
5104 break;
5105 break;
5107 case KEY_ENTER:
5108 case '\r': {
5109 struct got_object_id *id = NULL;
5110 struct got_object_qid *pid;
5111 struct got_commit_object *commit = NULL;
5112 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5113 s->first_displayed_line, s->selected_line);
5114 if (id == NULL)
5115 break;
5116 err = got_object_open_as_commit(&commit, s->repo, id);
5117 if (err)
5118 break;
5119 pid = STAILQ_FIRST(
5120 got_object_commit_get_parent_ids(commit));
5121 if (view_is_parent_view(view))
5122 begin_x = view_split_begin_x(view->begin_x);
5123 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5124 if (diff_view == NULL) {
5125 got_object_commit_close(commit);
5126 err = got_error_from_errno("view_open");
5127 break;
5129 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5130 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5131 got_object_commit_close(commit);
5132 if (err) {
5133 view_close(diff_view);
5134 break;
5136 view->focussed = 0;
5137 diff_view->focussed = 1;
5138 if (view_is_parent_view(view)) {
5139 err = view_close_child(view);
5140 if (err)
5141 break;
5142 err = view_set_child(view, diff_view);
5143 if (err)
5144 break;
5145 view->focus_child = 1;
5146 } else
5147 *new_view = diff_view;
5148 if (err)
5149 break;
5150 break;
5152 case CTRL('d'):
5153 case 'd':
5154 nscroll /= 2;
5155 /* FALL THROUGH */
5156 case KEY_NPAGE:
5157 case CTRL('f'):
5158 case ' ':
5159 if (s->last_displayed_line >= s->blame.nlines &&
5160 s->selected_line >= MIN(s->blame.nlines,
5161 view->nlines - 2)) {
5162 break;
5164 if (s->last_displayed_line >= s->blame.nlines &&
5165 s->selected_line < view->nlines - 2) {
5166 s->selected_line +=
5167 MIN(nscroll, s->last_displayed_line -
5168 s->first_displayed_line - s->selected_line + 1);
5170 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5171 s->first_displayed_line += nscroll;
5172 else
5173 s->first_displayed_line =
5174 s->blame.nlines - (view->nlines - 3);
5175 break;
5176 case KEY_RESIZE:
5177 if (s->selected_line > view->nlines - 2) {
5178 s->selected_line = MIN(s->blame.nlines,
5179 view->nlines - 2);
5181 break;
5182 default:
5183 break;
5185 return thread_err ? thread_err : err;
5188 static const struct got_error *
5189 cmd_blame(int argc, char *argv[])
5191 const struct got_error *error;
5192 struct got_repository *repo = NULL;
5193 struct got_worktree *worktree = NULL;
5194 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5195 char *link_target = NULL;
5196 struct got_object_id *commit_id = NULL;
5197 struct got_commit_object *commit = NULL;
5198 char *commit_id_str = NULL;
5199 int ch;
5200 struct tog_view *view;
5201 int *pack_fds = NULL;
5203 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5204 switch (ch) {
5205 case 'c':
5206 commit_id_str = optarg;
5207 break;
5208 case 'r':
5209 repo_path = realpath(optarg, NULL);
5210 if (repo_path == NULL)
5211 return got_error_from_errno2("realpath",
5212 optarg);
5213 break;
5214 default:
5215 usage_blame();
5216 /* NOTREACHED */
5220 argc -= optind;
5221 argv += optind;
5223 if (argc != 1)
5224 usage_blame();
5226 error = got_repo_pack_fds_open(&pack_fds);
5227 if (error != NULL)
5228 goto done;
5230 if (repo_path == NULL) {
5231 cwd = getcwd(NULL, 0);
5232 if (cwd == NULL)
5233 return got_error_from_errno("getcwd");
5234 error = got_worktree_open(&worktree, cwd);
5235 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5236 goto done;
5237 if (worktree)
5238 repo_path =
5239 strdup(got_worktree_get_repo_path(worktree));
5240 else
5241 repo_path = strdup(cwd);
5242 if (repo_path == NULL) {
5243 error = got_error_from_errno("strdup");
5244 goto done;
5248 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5249 if (error != NULL)
5250 goto done;
5252 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5253 worktree);
5254 if (error)
5255 goto done;
5257 init_curses();
5259 error = apply_unveil(got_repo_get_path(repo), NULL);
5260 if (error)
5261 goto done;
5263 error = tog_load_refs(repo, 0);
5264 if (error)
5265 goto done;
5267 if (commit_id_str == NULL) {
5268 struct got_reference *head_ref;
5269 error = got_ref_open(&head_ref, repo, worktree ?
5270 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5271 if (error != NULL)
5272 goto done;
5273 error = got_ref_resolve(&commit_id, repo, head_ref);
5274 got_ref_close(head_ref);
5275 } else {
5276 error = got_repo_match_object_id(&commit_id, NULL,
5277 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5279 if (error != NULL)
5280 goto done;
5282 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5283 if (view == NULL) {
5284 error = got_error_from_errno("view_open");
5285 goto done;
5288 error = got_object_open_as_commit(&commit, repo, commit_id);
5289 if (error)
5290 goto done;
5292 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5293 commit, repo);
5294 if (error)
5295 goto done;
5297 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5298 commit_id, repo);
5299 if (error)
5300 goto done;
5301 if (worktree) {
5302 /* Release work tree lock. */
5303 got_worktree_close(worktree);
5304 worktree = NULL;
5306 error = view_loop(view);
5307 done:
5308 free(repo_path);
5309 free(in_repo_path);
5310 free(link_target);
5311 free(cwd);
5312 free(commit_id);
5313 if (commit)
5314 got_object_commit_close(commit);
5315 if (worktree)
5316 got_worktree_close(worktree);
5317 if (repo) {
5318 const struct got_error *close_err = got_repo_close(repo);
5319 if (error == NULL)
5320 error = close_err;
5322 if (pack_fds) {
5323 const struct got_error *pack_err =
5324 got_repo_pack_fds_close(pack_fds);
5325 if (error == NULL)
5326 error = pack_err;
5328 tog_free_refs();
5329 return error;
5332 static const struct got_error *
5333 draw_tree_entries(struct tog_view *view, const char *parent_path)
5335 struct tog_tree_view_state *s = &view->state.tree;
5336 const struct got_error *err = NULL;
5337 struct got_tree_entry *te;
5338 wchar_t *wline;
5339 struct tog_color *tc;
5340 int width, n, i, nentries;
5341 int limit = view->nlines;
5343 s->ndisplayed = 0;
5345 werase(view->window);
5347 if (limit == 0)
5348 return NULL;
5350 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5351 0, 0);
5352 if (err)
5353 return err;
5354 if (view_needs_focus_indication(view))
5355 wstandout(view->window);
5356 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5357 if (tc)
5358 wattr_on(view->window,
5359 COLOR_PAIR(tc->colorpair), NULL);
5360 waddwstr(view->window, wline);
5361 if (tc)
5362 wattr_off(view->window,
5363 COLOR_PAIR(tc->colorpair), NULL);
5364 if (view_needs_focus_indication(view))
5365 wstandend(view->window);
5366 free(wline);
5367 wline = NULL;
5368 if (width < view->ncols - 1)
5369 waddch(view->window, '\n');
5370 if (--limit <= 0)
5371 return NULL;
5372 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5373 0, 0);
5374 if (err)
5375 return err;
5376 waddwstr(view->window, wline);
5377 free(wline);
5378 wline = NULL;
5379 if (width < view->ncols - 1)
5380 waddch(view->window, '\n');
5381 if (--limit <= 0)
5382 return NULL;
5383 waddch(view->window, '\n');
5384 if (--limit <= 0)
5385 return NULL;
5387 if (s->first_displayed_entry == NULL) {
5388 te = got_object_tree_get_first_entry(s->tree);
5389 if (s->selected == 0) {
5390 if (view->focussed)
5391 wstandout(view->window);
5392 s->selected_entry = NULL;
5394 waddstr(view->window, " ..\n"); /* parent directory */
5395 if (s->selected == 0 && view->focussed)
5396 wstandend(view->window);
5397 s->ndisplayed++;
5398 if (--limit <= 0)
5399 return NULL;
5400 n = 1;
5401 } else {
5402 n = 0;
5403 te = s->first_displayed_entry;
5406 nentries = got_object_tree_get_nentries(s->tree);
5407 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5408 char *line = NULL, *id_str = NULL, *link_target = NULL;
5409 const char *modestr = "";
5410 mode_t mode;
5412 te = got_object_tree_get_entry(s->tree, i);
5413 mode = got_tree_entry_get_mode(te);
5415 if (s->show_ids) {
5416 err = got_object_id_str(&id_str,
5417 got_tree_entry_get_id(te));
5418 if (err)
5419 return got_error_from_errno(
5420 "got_object_id_str");
5422 if (got_object_tree_entry_is_submodule(te))
5423 modestr = "$";
5424 else if (S_ISLNK(mode)) {
5425 int i;
5427 err = got_tree_entry_get_symlink_target(&link_target,
5428 te, s->repo);
5429 if (err) {
5430 free(id_str);
5431 return err;
5433 for (i = 0; i < strlen(link_target); i++) {
5434 if (!isprint((unsigned char)link_target[i]))
5435 link_target[i] = '?';
5437 modestr = "@";
5439 else if (S_ISDIR(mode))
5440 modestr = "/";
5441 else if (mode & S_IXUSR)
5442 modestr = "*";
5443 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5444 got_tree_entry_get_name(te), modestr,
5445 link_target ? " -> ": "",
5446 link_target ? link_target : "") == -1) {
5447 free(id_str);
5448 free(link_target);
5449 return got_error_from_errno("asprintf");
5451 free(id_str);
5452 free(link_target);
5453 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5454 0, 0);
5455 if (err) {
5456 free(line);
5457 break;
5459 if (n == s->selected) {
5460 if (view->focussed)
5461 wstandout(view->window);
5462 s->selected_entry = te;
5464 tc = match_color(&s->colors, line);
5465 if (tc)
5466 wattr_on(view->window,
5467 COLOR_PAIR(tc->colorpair), NULL);
5468 waddwstr(view->window, wline);
5469 if (tc)
5470 wattr_off(view->window,
5471 COLOR_PAIR(tc->colorpair), NULL);
5472 if (width < view->ncols - 1)
5473 waddch(view->window, '\n');
5474 if (n == s->selected && view->focussed)
5475 wstandend(view->window);
5476 free(line);
5477 free(wline);
5478 wline = NULL;
5479 n++;
5480 s->ndisplayed++;
5481 s->last_displayed_entry = te;
5482 if (--limit <= 0)
5483 break;
5486 return err;
5489 static void
5490 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5492 struct got_tree_entry *te;
5493 int isroot = s->tree == s->root;
5494 int i = 0;
5496 if (s->first_displayed_entry == NULL)
5497 return;
5499 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5500 while (i++ < maxscroll) {
5501 if (te == NULL) {
5502 if (!isroot)
5503 s->first_displayed_entry = NULL;
5504 break;
5506 s->first_displayed_entry = te;
5507 te = got_tree_entry_get_prev(s->tree, te);
5511 static void
5512 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5514 struct got_tree_entry *next, *last;
5515 int n = 0;
5517 if (s->first_displayed_entry)
5518 next = got_tree_entry_get_next(s->tree,
5519 s->first_displayed_entry);
5520 else
5521 next = got_object_tree_get_first_entry(s->tree);
5523 last = s->last_displayed_entry;
5524 while (next && last && n++ < maxscroll) {
5525 last = got_tree_entry_get_next(s->tree, last);
5526 if (last) {
5527 s->first_displayed_entry = next;
5528 next = got_tree_entry_get_next(s->tree, next);
5533 static const struct got_error *
5534 tree_entry_path(char **path, struct tog_parent_trees *parents,
5535 struct got_tree_entry *te)
5537 const struct got_error *err = NULL;
5538 struct tog_parent_tree *pt;
5539 size_t len = 2; /* for leading slash and NUL */
5541 TAILQ_FOREACH(pt, parents, entry)
5542 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5543 + 1 /* slash */;
5544 if (te)
5545 len += strlen(got_tree_entry_get_name(te));
5547 *path = calloc(1, len);
5548 if (path == NULL)
5549 return got_error_from_errno("calloc");
5551 (*path)[0] = '/';
5552 pt = TAILQ_LAST(parents, tog_parent_trees);
5553 while (pt) {
5554 const char *name = got_tree_entry_get_name(pt->selected_entry);
5555 if (strlcat(*path, name, len) >= len) {
5556 err = got_error(GOT_ERR_NO_SPACE);
5557 goto done;
5559 if (strlcat(*path, "/", len) >= len) {
5560 err = got_error(GOT_ERR_NO_SPACE);
5561 goto done;
5563 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5565 if (te) {
5566 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5567 err = got_error(GOT_ERR_NO_SPACE);
5568 goto done;
5571 done:
5572 if (err) {
5573 free(*path);
5574 *path = NULL;
5576 return err;
5579 static const struct got_error *
5580 blame_tree_entry(struct tog_view **new_view, int begin_x,
5581 struct got_tree_entry *te, struct tog_parent_trees *parents,
5582 struct got_object_id *commit_id, struct got_repository *repo)
5584 const struct got_error *err = NULL;
5585 char *path;
5586 struct tog_view *blame_view;
5588 *new_view = NULL;
5590 err = tree_entry_path(&path, parents, te);
5591 if (err)
5592 return err;
5594 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5595 if (blame_view == NULL) {
5596 err = got_error_from_errno("view_open");
5597 goto done;
5600 err = open_blame_view(blame_view, path, commit_id, repo);
5601 if (err) {
5602 if (err->code == GOT_ERR_CANCELLED)
5603 err = NULL;
5604 view_close(blame_view);
5605 } else
5606 *new_view = blame_view;
5607 done:
5608 free(path);
5609 return err;
5612 static const struct got_error *
5613 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5614 struct tog_tree_view_state *s)
5616 struct tog_view *log_view;
5617 const struct got_error *err = NULL;
5618 char *path;
5620 *new_view = NULL;
5622 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5623 if (log_view == NULL)
5624 return got_error_from_errno("view_open");
5626 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5627 if (err)
5628 return err;
5630 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5631 path, 0);
5632 if (err)
5633 view_close(log_view);
5634 else
5635 *new_view = log_view;
5636 free(path);
5637 return err;
5640 static const struct got_error *
5641 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5642 const char *head_ref_name, struct got_repository *repo)
5644 const struct got_error *err = NULL;
5645 char *commit_id_str = NULL;
5646 struct tog_tree_view_state *s = &view->state.tree;
5647 struct got_commit_object *commit = NULL;
5649 TAILQ_INIT(&s->parents);
5650 STAILQ_INIT(&s->colors);
5652 s->commit_id = got_object_id_dup(commit_id);
5653 if (s->commit_id == NULL)
5654 return got_error_from_errno("got_object_id_dup");
5656 err = got_object_open_as_commit(&commit, repo, commit_id);
5657 if (err)
5658 goto done;
5661 * The root is opened here and will be closed when the view is closed.
5662 * Any visited subtrees and their path-wise parents are opened and
5663 * closed on demand.
5665 err = got_object_open_as_tree(&s->root, repo,
5666 got_object_commit_get_tree_id(commit));
5667 if (err)
5668 goto done;
5669 s->tree = s->root;
5671 err = got_object_id_str(&commit_id_str, commit_id);
5672 if (err != NULL)
5673 goto done;
5675 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5676 err = got_error_from_errno("asprintf");
5677 goto done;
5680 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5681 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5682 if (head_ref_name) {
5683 s->head_ref_name = strdup(head_ref_name);
5684 if (s->head_ref_name == NULL) {
5685 err = got_error_from_errno("strdup");
5686 goto done;
5689 s->repo = repo;
5691 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5692 err = add_color(&s->colors, "\\$$",
5693 TOG_COLOR_TREE_SUBMODULE,
5694 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5695 if (err)
5696 goto done;
5697 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5698 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5699 if (err)
5700 goto done;
5701 err = add_color(&s->colors, "/$",
5702 TOG_COLOR_TREE_DIRECTORY,
5703 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5704 if (err)
5705 goto done;
5707 err = add_color(&s->colors, "\\*$",
5708 TOG_COLOR_TREE_EXECUTABLE,
5709 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5710 if (err)
5711 goto done;
5713 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5714 get_color_value("TOG_COLOR_COMMIT"));
5715 if (err)
5716 goto done;
5719 view->show = show_tree_view;
5720 view->input = input_tree_view;
5721 view->close = close_tree_view;
5722 view->search_start = search_start_tree_view;
5723 view->search_next = search_next_tree_view;
5724 done:
5725 free(commit_id_str);
5726 if (commit)
5727 got_object_commit_close(commit);
5728 if (err)
5729 close_tree_view(view);
5730 return err;
5733 static const struct got_error *
5734 close_tree_view(struct tog_view *view)
5736 struct tog_tree_view_state *s = &view->state.tree;
5738 free_colors(&s->colors);
5739 free(s->tree_label);
5740 s->tree_label = NULL;
5741 free(s->commit_id);
5742 s->commit_id = NULL;
5743 free(s->head_ref_name);
5744 s->head_ref_name = NULL;
5745 while (!TAILQ_EMPTY(&s->parents)) {
5746 struct tog_parent_tree *parent;
5747 parent = TAILQ_FIRST(&s->parents);
5748 TAILQ_REMOVE(&s->parents, parent, entry);
5749 if (parent->tree != s->root)
5750 got_object_tree_close(parent->tree);
5751 free(parent);
5754 if (s->tree != NULL && s->tree != s->root)
5755 got_object_tree_close(s->tree);
5756 if (s->root)
5757 got_object_tree_close(s->root);
5758 return NULL;
5761 static const struct got_error *
5762 search_start_tree_view(struct tog_view *view)
5764 struct tog_tree_view_state *s = &view->state.tree;
5766 s->matched_entry = NULL;
5767 return NULL;
5770 static int
5771 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5773 regmatch_t regmatch;
5775 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5776 0) == 0;
5779 static const struct got_error *
5780 search_next_tree_view(struct tog_view *view)
5782 struct tog_tree_view_state *s = &view->state.tree;
5783 struct got_tree_entry *te = NULL;
5785 if (!view->searching) {
5786 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5787 return NULL;
5790 if (s->matched_entry) {
5791 if (view->searching == TOG_SEARCH_FORWARD) {
5792 if (s->selected_entry)
5793 te = got_tree_entry_get_next(s->tree,
5794 s->selected_entry);
5795 else
5796 te = got_object_tree_get_first_entry(s->tree);
5797 } else {
5798 if (s->selected_entry == NULL)
5799 te = got_object_tree_get_last_entry(s->tree);
5800 else
5801 te = got_tree_entry_get_prev(s->tree,
5802 s->selected_entry);
5804 } else {
5805 if (s->selected_entry)
5806 te = s->selected_entry;
5807 else if (view->searching == TOG_SEARCH_FORWARD)
5808 te = got_object_tree_get_first_entry(s->tree);
5809 else
5810 te = got_object_tree_get_last_entry(s->tree);
5813 while (1) {
5814 if (te == NULL) {
5815 if (s->matched_entry == NULL) {
5816 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5817 return NULL;
5819 if (view->searching == TOG_SEARCH_FORWARD)
5820 te = got_object_tree_get_first_entry(s->tree);
5821 else
5822 te = got_object_tree_get_last_entry(s->tree);
5825 if (match_tree_entry(te, &view->regex)) {
5826 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5827 s->matched_entry = te;
5828 break;
5831 if (view->searching == TOG_SEARCH_FORWARD)
5832 te = got_tree_entry_get_next(s->tree, te);
5833 else
5834 te = got_tree_entry_get_prev(s->tree, te);
5837 if (s->matched_entry) {
5838 s->first_displayed_entry = s->matched_entry;
5839 s->selected = 0;
5842 return NULL;
5845 static const struct got_error *
5846 show_tree_view(struct tog_view *view)
5848 const struct got_error *err = NULL;
5849 struct tog_tree_view_state *s = &view->state.tree;
5850 char *parent_path;
5852 err = tree_entry_path(&parent_path, &s->parents, NULL);
5853 if (err)
5854 return err;
5856 err = draw_tree_entries(view, parent_path);
5857 free(parent_path);
5859 view_vborder(view);
5860 return err;
5863 static const struct got_error *
5864 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5866 const struct got_error *err = NULL;
5867 struct tog_tree_view_state *s = &view->state.tree;
5868 struct tog_view *log_view, *ref_view;
5869 struct got_tree_entry *te;
5870 int begin_x = 0, n, nscroll = view->nlines - 3;
5872 switch (ch) {
5873 case 'i':
5874 s->show_ids = !s->show_ids;
5875 break;
5876 case 'l':
5877 if (!s->selected_entry)
5878 break;
5879 if (view_is_parent_view(view))
5880 begin_x = view_split_begin_x(view->begin_x);
5881 err = log_selected_tree_entry(&log_view, begin_x, s);
5882 view->focussed = 0;
5883 log_view->focussed = 1;
5884 if (view_is_parent_view(view)) {
5885 err = view_close_child(view);
5886 if (err)
5887 return err;
5888 err = view_set_child(view, log_view);
5889 if (err)
5890 return err;
5891 view->focus_child = 1;
5892 } else
5893 *new_view = log_view;
5894 break;
5895 case 'r':
5896 if (view_is_parent_view(view))
5897 begin_x = view_split_begin_x(view->begin_x);
5898 ref_view = view_open(view->nlines, view->ncols,
5899 view->begin_y, begin_x, TOG_VIEW_REF);
5900 if (ref_view == NULL)
5901 return got_error_from_errno("view_open");
5902 err = open_ref_view(ref_view, s->repo);
5903 if (err) {
5904 view_close(ref_view);
5905 return err;
5907 view->focussed = 0;
5908 ref_view->focussed = 1;
5909 if (view_is_parent_view(view)) {
5910 err = view_close_child(view);
5911 if (err)
5912 return err;
5913 err = view_set_child(view, ref_view);
5914 if (err)
5915 return err;
5916 view->focus_child = 1;
5917 } else
5918 *new_view = ref_view;
5919 break;
5920 case 'g':
5921 case KEY_HOME:
5922 s->selected = 0;
5923 if (s->tree == s->root)
5924 s->first_displayed_entry =
5925 got_object_tree_get_first_entry(s->tree);
5926 else
5927 s->first_displayed_entry = NULL;
5928 break;
5929 case 'G':
5930 case KEY_END:
5931 s->selected = 0;
5932 te = got_object_tree_get_last_entry(s->tree);
5933 for (n = 0; n < view->nlines - 3; n++) {
5934 if (te == NULL) {
5935 if(s->tree != s->root) {
5936 s->first_displayed_entry = NULL;
5937 n++;
5939 break;
5941 s->first_displayed_entry = te;
5942 te = got_tree_entry_get_prev(s->tree, te);
5944 if (n > 0)
5945 s->selected = n - 1;
5946 break;
5947 case 'k':
5948 case KEY_UP:
5949 case CTRL('p'):
5950 if (s->selected > 0) {
5951 s->selected--;
5952 break;
5954 tree_scroll_up(s, 1);
5955 break;
5956 case CTRL('u'):
5957 case 'u':
5958 nscroll /= 2;
5959 /* FALL THROUGH */
5960 case KEY_PPAGE:
5961 case CTRL('b'):
5962 if (s->tree == s->root) {
5963 if (got_object_tree_get_first_entry(s->tree) ==
5964 s->first_displayed_entry)
5965 s->selected -= MIN(s->selected, nscroll);
5966 } else {
5967 if (s->first_displayed_entry == NULL)
5968 s->selected -= MIN(s->selected, nscroll);
5970 tree_scroll_up(s, MAX(0, nscroll));
5971 break;
5972 case 'j':
5973 case KEY_DOWN:
5974 case CTRL('n'):
5975 if (s->selected < s->ndisplayed - 1) {
5976 s->selected++;
5977 break;
5979 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5980 == NULL)
5981 /* can't scroll any further */
5982 break;
5983 tree_scroll_down(s, 1);
5984 break;
5985 case CTRL('d'):
5986 case 'd':
5987 nscroll /= 2;
5988 /* FALL THROUGH */
5989 case KEY_NPAGE:
5990 case CTRL('f'):
5991 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5992 == NULL) {
5993 /* can't scroll any further; move cursor down */
5994 if (s->selected < s->ndisplayed - 1)
5995 s->selected += MIN(nscroll,
5996 s->ndisplayed - s->selected - 1);
5997 break;
5999 tree_scroll_down(s, nscroll);
6000 break;
6001 case KEY_ENTER:
6002 case '\r':
6003 case KEY_BACKSPACE:
6004 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6005 struct tog_parent_tree *parent;
6006 /* user selected '..' */
6007 if (s->tree == s->root)
6008 break;
6009 parent = TAILQ_FIRST(&s->parents);
6010 TAILQ_REMOVE(&s->parents, parent,
6011 entry);
6012 got_object_tree_close(s->tree);
6013 s->tree = parent->tree;
6014 s->first_displayed_entry =
6015 parent->first_displayed_entry;
6016 s->selected_entry =
6017 parent->selected_entry;
6018 s->selected = parent->selected;
6019 free(parent);
6020 } else if (S_ISDIR(got_tree_entry_get_mode(
6021 s->selected_entry))) {
6022 struct got_tree_object *subtree;
6023 err = got_object_open_as_tree(&subtree, s->repo,
6024 got_tree_entry_get_id(s->selected_entry));
6025 if (err)
6026 break;
6027 err = tree_view_visit_subtree(s, subtree);
6028 if (err) {
6029 got_object_tree_close(subtree);
6030 break;
6032 } else if (S_ISREG(got_tree_entry_get_mode(
6033 s->selected_entry))) {
6034 struct tog_view *blame_view;
6035 int begin_x = view_is_parent_view(view) ?
6036 view_split_begin_x(view->begin_x) : 0;
6038 err = blame_tree_entry(&blame_view, begin_x,
6039 s->selected_entry, &s->parents,
6040 s->commit_id, s->repo);
6041 if (err)
6042 break;
6043 view->focussed = 0;
6044 blame_view->focussed = 1;
6045 if (view_is_parent_view(view)) {
6046 err = view_close_child(view);
6047 if (err)
6048 return err;
6049 err = view_set_child(view, blame_view);
6050 if (err)
6051 return err;
6052 view->focus_child = 1;
6053 } else
6054 *new_view = blame_view;
6056 break;
6057 case KEY_RESIZE:
6058 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6059 s->selected = view->nlines - 4;
6060 break;
6061 default:
6062 break;
6065 return err;
6068 __dead static void
6069 usage_tree(void)
6071 endwin();
6072 fprintf(stderr,
6073 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6074 getprogname());
6075 exit(1);
6078 static const struct got_error *
6079 cmd_tree(int argc, char *argv[])
6081 const struct got_error *error;
6082 struct got_repository *repo = NULL;
6083 struct got_worktree *worktree = NULL;
6084 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6085 struct got_object_id *commit_id = NULL;
6086 struct got_commit_object *commit = NULL;
6087 const char *commit_id_arg = NULL;
6088 char *label = NULL;
6089 struct got_reference *ref = NULL;
6090 const char *head_ref_name = NULL;
6091 int ch;
6092 struct tog_view *view;
6093 int *pack_fds = NULL;
6095 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6096 switch (ch) {
6097 case 'c':
6098 commit_id_arg = optarg;
6099 break;
6100 case 'r':
6101 repo_path = realpath(optarg, NULL);
6102 if (repo_path == NULL)
6103 return got_error_from_errno2("realpath",
6104 optarg);
6105 break;
6106 default:
6107 usage_tree();
6108 /* NOTREACHED */
6112 argc -= optind;
6113 argv += optind;
6115 if (argc > 1)
6116 usage_tree();
6118 error = got_repo_pack_fds_open(&pack_fds);
6119 if (error != NULL)
6120 goto done;
6122 if (repo_path == NULL) {
6123 cwd = getcwd(NULL, 0);
6124 if (cwd == NULL)
6125 return got_error_from_errno("getcwd");
6126 error = got_worktree_open(&worktree, cwd);
6127 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6128 goto done;
6129 if (worktree)
6130 repo_path =
6131 strdup(got_worktree_get_repo_path(worktree));
6132 else
6133 repo_path = strdup(cwd);
6134 if (repo_path == NULL) {
6135 error = got_error_from_errno("strdup");
6136 goto done;
6140 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6141 if (error != NULL)
6142 goto done;
6144 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6145 repo, worktree);
6146 if (error)
6147 goto done;
6149 init_curses();
6151 error = apply_unveil(got_repo_get_path(repo), NULL);
6152 if (error)
6153 goto done;
6155 error = tog_load_refs(repo, 0);
6156 if (error)
6157 goto done;
6159 if (commit_id_arg == NULL) {
6160 error = got_repo_match_object_id(&commit_id, &label,
6161 worktree ? got_worktree_get_head_ref_name(worktree) :
6162 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6163 if (error)
6164 goto done;
6165 head_ref_name = label;
6166 } else {
6167 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6168 if (error == NULL)
6169 head_ref_name = got_ref_get_name(ref);
6170 else if (error->code != GOT_ERR_NOT_REF)
6171 goto done;
6172 error = got_repo_match_object_id(&commit_id, NULL,
6173 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6174 if (error)
6175 goto done;
6178 error = got_object_open_as_commit(&commit, repo, commit_id);
6179 if (error)
6180 goto done;
6182 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6183 if (view == NULL) {
6184 error = got_error_from_errno("view_open");
6185 goto done;
6187 error = open_tree_view(view, commit_id, head_ref_name, repo);
6188 if (error)
6189 goto done;
6190 if (!got_path_is_root_dir(in_repo_path)) {
6191 error = tree_view_walk_path(&view->state.tree, commit,
6192 in_repo_path);
6193 if (error)
6194 goto done;
6197 if (worktree) {
6198 /* Release work tree lock. */
6199 got_worktree_close(worktree);
6200 worktree = NULL;
6202 error = view_loop(view);
6203 done:
6204 free(repo_path);
6205 free(cwd);
6206 free(commit_id);
6207 free(label);
6208 if (ref)
6209 got_ref_close(ref);
6210 if (repo) {
6211 const struct got_error *close_err = got_repo_close(repo);
6212 if (error == NULL)
6213 error = close_err;
6215 if (pack_fds) {
6216 const struct got_error *pack_err =
6217 got_repo_pack_fds_close(pack_fds);
6218 if (error == NULL)
6219 error = pack_err;
6221 tog_free_refs();
6222 return error;
6225 static const struct got_error *
6226 ref_view_load_refs(struct tog_ref_view_state *s)
6228 struct got_reflist_entry *sre;
6229 struct tog_reflist_entry *re;
6231 s->nrefs = 0;
6232 TAILQ_FOREACH(sre, &tog_refs, entry) {
6233 if (strncmp(got_ref_get_name(sre->ref),
6234 "refs/got/", 9) == 0 &&
6235 strncmp(got_ref_get_name(sre->ref),
6236 "refs/got/backup/", 16) != 0)
6237 continue;
6239 re = malloc(sizeof(*re));
6240 if (re == NULL)
6241 return got_error_from_errno("malloc");
6243 re->ref = got_ref_dup(sre->ref);
6244 if (re->ref == NULL)
6245 return got_error_from_errno("got_ref_dup");
6246 re->idx = s->nrefs++;
6247 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6250 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6251 return NULL;
6254 void
6255 ref_view_free_refs(struct tog_ref_view_state *s)
6257 struct tog_reflist_entry *re;
6259 while (!TAILQ_EMPTY(&s->refs)) {
6260 re = TAILQ_FIRST(&s->refs);
6261 TAILQ_REMOVE(&s->refs, re, entry);
6262 got_ref_close(re->ref);
6263 free(re);
6267 static const struct got_error *
6268 open_ref_view(struct tog_view *view, struct got_repository *repo)
6270 const struct got_error *err = NULL;
6271 struct tog_ref_view_state *s = &view->state.ref;
6273 s->selected_entry = 0;
6274 s->repo = repo;
6276 TAILQ_INIT(&s->refs);
6277 STAILQ_INIT(&s->colors);
6279 err = ref_view_load_refs(s);
6280 if (err)
6281 return err;
6283 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6284 err = add_color(&s->colors, "^refs/heads/",
6285 TOG_COLOR_REFS_HEADS,
6286 get_color_value("TOG_COLOR_REFS_HEADS"));
6287 if (err)
6288 goto done;
6290 err = add_color(&s->colors, "^refs/tags/",
6291 TOG_COLOR_REFS_TAGS,
6292 get_color_value("TOG_COLOR_REFS_TAGS"));
6293 if (err)
6294 goto done;
6296 err = add_color(&s->colors, "^refs/remotes/",
6297 TOG_COLOR_REFS_REMOTES,
6298 get_color_value("TOG_COLOR_REFS_REMOTES"));
6299 if (err)
6300 goto done;
6302 err = add_color(&s->colors, "^refs/got/backup/",
6303 TOG_COLOR_REFS_BACKUP,
6304 get_color_value("TOG_COLOR_REFS_BACKUP"));
6305 if (err)
6306 goto done;
6309 view->show = show_ref_view;
6310 view->input = input_ref_view;
6311 view->close = close_ref_view;
6312 view->search_start = search_start_ref_view;
6313 view->search_next = search_next_ref_view;
6314 done:
6315 if (err)
6316 free_colors(&s->colors);
6317 return err;
6320 static const struct got_error *
6321 close_ref_view(struct tog_view *view)
6323 struct tog_ref_view_state *s = &view->state.ref;
6325 ref_view_free_refs(s);
6326 free_colors(&s->colors);
6328 return NULL;
6331 static const struct got_error *
6332 resolve_reflist_entry(struct got_object_id **commit_id,
6333 struct tog_reflist_entry *re, struct got_repository *repo)
6335 const struct got_error *err = NULL;
6336 struct got_object_id *obj_id;
6337 struct got_tag_object *tag = NULL;
6338 int obj_type;
6340 *commit_id = NULL;
6342 err = got_ref_resolve(&obj_id, repo, re->ref);
6343 if (err)
6344 return err;
6346 err = got_object_get_type(&obj_type, repo, obj_id);
6347 if (err)
6348 goto done;
6350 switch (obj_type) {
6351 case GOT_OBJ_TYPE_COMMIT:
6352 *commit_id = obj_id;
6353 break;
6354 case GOT_OBJ_TYPE_TAG:
6355 err = got_object_open_as_tag(&tag, repo, obj_id);
6356 if (err)
6357 goto done;
6358 free(obj_id);
6359 err = got_object_get_type(&obj_type, repo,
6360 got_object_tag_get_object_id(tag));
6361 if (err)
6362 goto done;
6363 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6364 err = got_error(GOT_ERR_OBJ_TYPE);
6365 goto done;
6367 *commit_id = got_object_id_dup(
6368 got_object_tag_get_object_id(tag));
6369 if (*commit_id == NULL) {
6370 err = got_error_from_errno("got_object_id_dup");
6371 goto done;
6373 break;
6374 default:
6375 err = got_error(GOT_ERR_OBJ_TYPE);
6376 break;
6379 done:
6380 if (tag)
6381 got_object_tag_close(tag);
6382 if (err) {
6383 free(*commit_id);
6384 *commit_id = NULL;
6386 return err;
6389 static const struct got_error *
6390 log_ref_entry(struct tog_view **new_view, int begin_x,
6391 struct tog_reflist_entry *re, struct got_repository *repo)
6393 struct tog_view *log_view;
6394 const struct got_error *err = NULL;
6395 struct got_object_id *commit_id = NULL;
6397 *new_view = NULL;
6399 err = resolve_reflist_entry(&commit_id, re, repo);
6400 if (err) {
6401 if (err->code != GOT_ERR_OBJ_TYPE)
6402 return err;
6403 else
6404 return NULL;
6407 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6408 if (log_view == NULL) {
6409 err = got_error_from_errno("view_open");
6410 goto done;
6413 err = open_log_view(log_view, commit_id, repo,
6414 got_ref_get_name(re->ref), "", 0);
6415 done:
6416 if (err)
6417 view_close(log_view);
6418 else
6419 *new_view = log_view;
6420 free(commit_id);
6421 return err;
6424 static void
6425 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6427 struct tog_reflist_entry *re;
6428 int i = 0;
6430 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6431 return;
6433 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6434 while (i++ < maxscroll) {
6435 if (re == NULL)
6436 break;
6437 s->first_displayed_entry = re;
6438 re = TAILQ_PREV(re, tog_reflist_head, entry);
6442 static void
6443 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6445 struct tog_reflist_entry *next, *last;
6446 int n = 0;
6448 if (s->first_displayed_entry)
6449 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6450 else
6451 next = TAILQ_FIRST(&s->refs);
6453 last = s->last_displayed_entry;
6454 while (next && last && n++ < maxscroll) {
6455 last = TAILQ_NEXT(last, entry);
6456 if (last) {
6457 s->first_displayed_entry = next;
6458 next = TAILQ_NEXT(next, entry);
6463 static const struct got_error *
6464 search_start_ref_view(struct tog_view *view)
6466 struct tog_ref_view_state *s = &view->state.ref;
6468 s->matched_entry = NULL;
6469 return NULL;
6472 static int
6473 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6475 regmatch_t regmatch;
6477 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6478 0) == 0;
6481 static const struct got_error *
6482 search_next_ref_view(struct tog_view *view)
6484 struct tog_ref_view_state *s = &view->state.ref;
6485 struct tog_reflist_entry *re = NULL;
6487 if (!view->searching) {
6488 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6489 return NULL;
6492 if (s->matched_entry) {
6493 if (view->searching == TOG_SEARCH_FORWARD) {
6494 if (s->selected_entry)
6495 re = TAILQ_NEXT(s->selected_entry, entry);
6496 else
6497 re = TAILQ_PREV(s->selected_entry,
6498 tog_reflist_head, entry);
6499 } else {
6500 if (s->selected_entry == NULL)
6501 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6502 else
6503 re = TAILQ_PREV(s->selected_entry,
6504 tog_reflist_head, entry);
6506 } else {
6507 if (s->selected_entry)
6508 re = s->selected_entry;
6509 else if (view->searching == TOG_SEARCH_FORWARD)
6510 re = TAILQ_FIRST(&s->refs);
6511 else
6512 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6515 while (1) {
6516 if (re == NULL) {
6517 if (s->matched_entry == NULL) {
6518 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6519 return NULL;
6521 if (view->searching == TOG_SEARCH_FORWARD)
6522 re = TAILQ_FIRST(&s->refs);
6523 else
6524 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6527 if (match_reflist_entry(re, &view->regex)) {
6528 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6529 s->matched_entry = re;
6530 break;
6533 if (view->searching == TOG_SEARCH_FORWARD)
6534 re = TAILQ_NEXT(re, entry);
6535 else
6536 re = TAILQ_PREV(re, tog_reflist_head, entry);
6539 if (s->matched_entry) {
6540 s->first_displayed_entry = s->matched_entry;
6541 s->selected = 0;
6544 return NULL;
6547 static const struct got_error *
6548 show_ref_view(struct tog_view *view)
6550 const struct got_error *err = NULL;
6551 struct tog_ref_view_state *s = &view->state.ref;
6552 struct tog_reflist_entry *re;
6553 char *line = NULL;
6554 wchar_t *wline;
6555 struct tog_color *tc;
6556 int width, n;
6557 int limit = view->nlines;
6559 werase(view->window);
6561 s->ndisplayed = 0;
6563 if (limit == 0)
6564 return NULL;
6566 re = s->first_displayed_entry;
6568 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6569 s->nrefs) == -1)
6570 return got_error_from_errno("asprintf");
6572 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6573 if (err) {
6574 free(line);
6575 return err;
6577 if (view_needs_focus_indication(view))
6578 wstandout(view->window);
6579 waddwstr(view->window, wline);
6580 if (view_needs_focus_indication(view))
6581 wstandend(view->window);
6582 free(wline);
6583 wline = NULL;
6584 free(line);
6585 line = NULL;
6586 if (width < view->ncols - 1)
6587 waddch(view->window, '\n');
6588 if (--limit <= 0)
6589 return NULL;
6591 n = 0;
6592 while (re && limit > 0) {
6593 char *line = NULL;
6594 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6596 if (s->show_date) {
6597 struct got_commit_object *ci;
6598 struct got_tag_object *tag;
6599 struct got_object_id *id;
6600 struct tm tm;
6601 time_t t;
6603 err = got_ref_resolve(&id, s->repo, re->ref);
6604 if (err)
6605 return err;
6606 err = got_object_open_as_tag(&tag, s->repo, id);
6607 if (err) {
6608 if (err->code != GOT_ERR_OBJ_TYPE) {
6609 free(id);
6610 return err;
6612 err = got_object_open_as_commit(&ci, s->repo,
6613 id);
6614 if (err) {
6615 free(id);
6616 return err;
6618 t = got_object_commit_get_committer_time(ci);
6619 got_object_commit_close(ci);
6620 } else {
6621 t = got_object_tag_get_tagger_time(tag);
6622 got_object_tag_close(tag);
6624 free(id);
6625 if (gmtime_r(&t, &tm) == NULL)
6626 return got_error_from_errno("gmtime_r");
6627 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6628 return got_error(GOT_ERR_NO_SPACE);
6630 if (got_ref_is_symbolic(re->ref)) {
6631 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6632 ymd : "", got_ref_get_name(re->ref),
6633 got_ref_get_symref_target(re->ref)) == -1)
6634 return got_error_from_errno("asprintf");
6635 } else if (s->show_ids) {
6636 struct got_object_id *id;
6637 char *id_str;
6638 err = got_ref_resolve(&id, s->repo, re->ref);
6639 if (err)
6640 return err;
6641 err = got_object_id_str(&id_str, id);
6642 if (err) {
6643 free(id);
6644 return err;
6646 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6647 got_ref_get_name(re->ref), id_str) == -1) {
6648 err = got_error_from_errno("asprintf");
6649 free(id);
6650 free(id_str);
6651 return err;
6653 free(id);
6654 free(id_str);
6655 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6656 got_ref_get_name(re->ref)) == -1)
6657 return got_error_from_errno("asprintf");
6659 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6660 0, 0);
6661 if (err) {
6662 free(line);
6663 return err;
6665 if (n == s->selected) {
6666 if (view->focussed)
6667 wstandout(view->window);
6668 s->selected_entry = re;
6670 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6671 if (tc)
6672 wattr_on(view->window,
6673 COLOR_PAIR(tc->colorpair), NULL);
6674 waddwstr(view->window, wline);
6675 if (tc)
6676 wattr_off(view->window,
6677 COLOR_PAIR(tc->colorpair), NULL);
6678 if (width < view->ncols - 1)
6679 waddch(view->window, '\n');
6680 if (n == s->selected && view->focussed)
6681 wstandend(view->window);
6682 free(line);
6683 free(wline);
6684 wline = NULL;
6685 n++;
6686 s->ndisplayed++;
6687 s->last_displayed_entry = re;
6689 limit--;
6690 re = TAILQ_NEXT(re, entry);
6693 view_vborder(view);
6694 return err;
6697 static const struct got_error *
6698 browse_ref_tree(struct tog_view **new_view, int begin_x,
6699 struct tog_reflist_entry *re, struct got_repository *repo)
6701 const struct got_error *err = NULL;
6702 struct got_object_id *commit_id = NULL;
6703 struct tog_view *tree_view;
6705 *new_view = NULL;
6707 err = resolve_reflist_entry(&commit_id, re, repo);
6708 if (err) {
6709 if (err->code != GOT_ERR_OBJ_TYPE)
6710 return err;
6711 else
6712 return NULL;
6716 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6717 if (tree_view == NULL) {
6718 err = got_error_from_errno("view_open");
6719 goto done;
6722 err = open_tree_view(tree_view, commit_id,
6723 got_ref_get_name(re->ref), repo);
6724 if (err)
6725 goto done;
6727 *new_view = tree_view;
6728 done:
6729 free(commit_id);
6730 return err;
6732 static const struct got_error *
6733 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6735 const struct got_error *err = NULL;
6736 struct tog_ref_view_state *s = &view->state.ref;
6737 struct tog_view *log_view, *tree_view;
6738 struct tog_reflist_entry *re;
6739 int begin_x = 0, n, nscroll = view->nlines - 1;
6741 switch (ch) {
6742 case 'i':
6743 s->show_ids = !s->show_ids;
6744 break;
6745 case 'm':
6746 s->show_date = !s->show_date;
6747 break;
6748 case 'o':
6749 s->sort_by_date = !s->sort_by_date;
6750 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6751 got_ref_cmp_by_commit_timestamp_descending :
6752 tog_ref_cmp_by_name, s->repo);
6753 if (err)
6754 break;
6755 got_reflist_object_id_map_free(tog_refs_idmap);
6756 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6757 &tog_refs, s->repo);
6758 if (err)
6759 break;
6760 ref_view_free_refs(s);
6761 err = ref_view_load_refs(s);
6762 break;
6763 case KEY_ENTER:
6764 case '\r':
6765 if (!s->selected_entry)
6766 break;
6767 if (view_is_parent_view(view))
6768 begin_x = view_split_begin_x(view->begin_x);
6769 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6770 s->repo);
6771 view->focussed = 0;
6772 log_view->focussed = 1;
6773 if (view_is_parent_view(view)) {
6774 err = view_close_child(view);
6775 if (err)
6776 return err;
6777 err = view_set_child(view, log_view);
6778 if (err)
6779 return err;
6780 view->focus_child = 1;
6781 } else
6782 *new_view = log_view;
6783 break;
6784 case 't':
6785 if (!s->selected_entry)
6786 break;
6787 if (view_is_parent_view(view))
6788 begin_x = view_split_begin_x(view->begin_x);
6789 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6790 s->repo);
6791 if (err || tree_view == NULL)
6792 break;
6793 view->focussed = 0;
6794 tree_view->focussed = 1;
6795 if (view_is_parent_view(view)) {
6796 err = view_close_child(view);
6797 if (err)
6798 return err;
6799 err = view_set_child(view, tree_view);
6800 if (err)
6801 return err;
6802 view->focus_child = 1;
6803 } else
6804 *new_view = tree_view;
6805 break;
6806 case 'g':
6807 case KEY_HOME:
6808 s->selected = 0;
6809 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6810 break;
6811 case 'G':
6812 case KEY_END:
6813 s->selected = 0;
6814 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6815 for (n = 0; n < view->nlines - 1; n++) {
6816 if (re == NULL)
6817 break;
6818 s->first_displayed_entry = re;
6819 re = TAILQ_PREV(re, tog_reflist_head, entry);
6821 if (n > 0)
6822 s->selected = n - 1;
6823 break;
6824 case 'k':
6825 case KEY_UP:
6826 case CTRL('p'):
6827 if (s->selected > 0) {
6828 s->selected--;
6829 break;
6831 ref_scroll_up(s, 1);
6832 break;
6833 case CTRL('u'):
6834 case 'u':
6835 nscroll /= 2;
6836 /* FALL THROUGH */
6837 case KEY_PPAGE:
6838 case CTRL('b'):
6839 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6840 s->selected -= MIN(nscroll, s->selected);
6841 ref_scroll_up(s, MAX(0, nscroll));
6842 break;
6843 case 'j':
6844 case KEY_DOWN:
6845 case CTRL('n'):
6846 if (s->selected < s->ndisplayed - 1) {
6847 s->selected++;
6848 break;
6850 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6851 /* can't scroll any further */
6852 break;
6853 ref_scroll_down(s, 1);
6854 break;
6855 case CTRL('d'):
6856 case 'd':
6857 nscroll /= 2;
6858 /* FALL THROUGH */
6859 case KEY_NPAGE:
6860 case CTRL('f'):
6861 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6862 /* can't scroll any further; move cursor down */
6863 if (s->selected < s->ndisplayed - 1)
6864 s->selected += MIN(nscroll,
6865 s->ndisplayed - s->selected - 1);
6866 break;
6868 ref_scroll_down(s, nscroll);
6869 break;
6870 case CTRL('l'):
6871 tog_free_refs();
6872 err = tog_load_refs(s->repo, s->sort_by_date);
6873 if (err)
6874 break;
6875 ref_view_free_refs(s);
6876 err = ref_view_load_refs(s);
6877 break;
6878 case KEY_RESIZE:
6879 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6880 s->selected = view->nlines - 2;
6881 break;
6882 default:
6883 break;
6886 return err;
6889 __dead static void
6890 usage_ref(void)
6892 endwin();
6893 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6894 getprogname());
6895 exit(1);
6898 static const struct got_error *
6899 cmd_ref(int argc, char *argv[])
6901 const struct got_error *error;
6902 struct got_repository *repo = NULL;
6903 struct got_worktree *worktree = NULL;
6904 char *cwd = NULL, *repo_path = NULL;
6905 int ch;
6906 struct tog_view *view;
6907 int *pack_fds = NULL;
6909 while ((ch = getopt(argc, argv, "r:")) != -1) {
6910 switch (ch) {
6911 case 'r':
6912 repo_path = realpath(optarg, NULL);
6913 if (repo_path == NULL)
6914 return got_error_from_errno2("realpath",
6915 optarg);
6916 break;
6917 default:
6918 usage_ref();
6919 /* NOTREACHED */
6923 argc -= optind;
6924 argv += optind;
6926 if (argc > 1)
6927 usage_ref();
6929 error = got_repo_pack_fds_open(&pack_fds);
6930 if (error != NULL)
6931 goto done;
6933 if (repo_path == NULL) {
6934 cwd = getcwd(NULL, 0);
6935 if (cwd == NULL)
6936 return got_error_from_errno("getcwd");
6937 error = got_worktree_open(&worktree, cwd);
6938 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6939 goto done;
6940 if (worktree)
6941 repo_path =
6942 strdup(got_worktree_get_repo_path(worktree));
6943 else
6944 repo_path = strdup(cwd);
6945 if (repo_path == NULL) {
6946 error = got_error_from_errno("strdup");
6947 goto done;
6951 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6952 if (error != NULL)
6953 goto done;
6955 init_curses();
6957 error = apply_unveil(got_repo_get_path(repo), NULL);
6958 if (error)
6959 goto done;
6961 error = tog_load_refs(repo, 0);
6962 if (error)
6963 goto done;
6965 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6966 if (view == NULL) {
6967 error = got_error_from_errno("view_open");
6968 goto done;
6971 error = open_ref_view(view, repo);
6972 if (error)
6973 goto done;
6975 if (worktree) {
6976 /* Release work tree lock. */
6977 got_worktree_close(worktree);
6978 worktree = NULL;
6980 error = view_loop(view);
6981 done:
6982 free(repo_path);
6983 free(cwd);
6984 if (repo) {
6985 const struct got_error *close_err = got_repo_close(repo);
6986 if (close_err)
6987 error = close_err;
6989 if (pack_fds) {
6990 const struct got_error *pack_err =
6991 got_repo_pack_fds_close(pack_fds);
6992 if (error == NULL)
6993 error = pack_err;
6995 tog_free_refs();
6996 return error;
6999 static void
7000 list_commands(FILE *fp)
7002 size_t i;
7004 fprintf(fp, "commands:");
7005 for (i = 0; i < nitems(tog_commands); i++) {
7006 const struct tog_cmd *cmd = &tog_commands[i];
7007 fprintf(fp, " %s", cmd->name);
7009 fputc('\n', fp);
7012 __dead static void
7013 usage(int hflag, int status)
7015 FILE *fp = (status == 0) ? stdout : stderr;
7017 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7018 getprogname());
7019 if (hflag) {
7020 fprintf(fp, "lazy usage: %s path\n", getprogname());
7021 list_commands(fp);
7023 exit(status);
7026 static char **
7027 make_argv(int argc, ...)
7029 va_list ap;
7030 char **argv;
7031 int i;
7033 va_start(ap, argc);
7035 argv = calloc(argc, sizeof(char *));
7036 if (argv == NULL)
7037 err(1, "calloc");
7038 for (i = 0; i < argc; i++) {
7039 argv[i] = strdup(va_arg(ap, char *));
7040 if (argv[i] == NULL)
7041 err(1, "strdup");
7044 va_end(ap);
7045 return argv;
7049 * Try to convert 'tog path' into a 'tog log path' command.
7050 * The user could simply have mistyped the command rather than knowingly
7051 * provided a path. So check whether argv[0] can in fact be resolved
7052 * to a path in the HEAD commit and print a special error if not.
7053 * This hack is for mpi@ <3
7055 static const struct got_error *
7056 tog_log_with_path(int argc, char *argv[])
7058 const struct got_error *error = NULL, *close_err;
7059 const struct tog_cmd *cmd = NULL;
7060 struct got_repository *repo = NULL;
7061 struct got_worktree *worktree = NULL;
7062 struct got_object_id *commit_id = NULL, *id = NULL;
7063 struct got_commit_object *commit = NULL;
7064 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7065 char *commit_id_str = NULL, **cmd_argv = NULL;
7066 int *pack_fds = NULL;
7068 cwd = getcwd(NULL, 0);
7069 if (cwd == NULL)
7070 return got_error_from_errno("getcwd");
7072 error = got_repo_pack_fds_open(&pack_fds);
7073 if (error != NULL)
7074 goto done;
7076 error = got_worktree_open(&worktree, cwd);
7077 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7078 goto done;
7080 if (worktree)
7081 repo_path = strdup(got_worktree_get_repo_path(worktree));
7082 else
7083 repo_path = strdup(cwd);
7084 if (repo_path == NULL) {
7085 error = got_error_from_errno("strdup");
7086 goto done;
7089 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7090 if (error != NULL)
7091 goto done;
7093 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7094 repo, worktree);
7095 if (error)
7096 goto done;
7098 error = tog_load_refs(repo, 0);
7099 if (error)
7100 goto done;
7101 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7102 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7103 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7104 if (error)
7105 goto done;
7107 if (worktree) {
7108 got_worktree_close(worktree);
7109 worktree = NULL;
7112 error = got_object_open_as_commit(&commit, repo, commit_id);
7113 if (error)
7114 goto done;
7116 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7117 if (error) {
7118 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7119 goto done;
7120 fprintf(stderr, "%s: '%s' is no known command or path\n",
7121 getprogname(), argv[0]);
7122 usage(1, 1);
7123 /* not reached */
7126 close_err = got_repo_close(repo);
7127 if (error == NULL)
7128 error = close_err;
7129 repo = NULL;
7131 error = got_object_id_str(&commit_id_str, commit_id);
7132 if (error)
7133 goto done;
7135 cmd = &tog_commands[0]; /* log */
7136 argc = 4;
7137 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7138 error = cmd->cmd_main(argc, cmd_argv);
7139 done:
7140 if (repo) {
7141 close_err = got_repo_close(repo);
7142 if (error == NULL)
7143 error = close_err;
7145 if (commit)
7146 got_object_commit_close(commit);
7147 if (worktree)
7148 got_worktree_close(worktree);
7149 if (pack_fds) {
7150 const struct got_error *pack_err =
7151 got_repo_pack_fds_close(pack_fds);
7152 if (error == NULL)
7153 error = pack_err;
7155 free(id);
7156 free(commit_id_str);
7157 free(commit_id);
7158 free(cwd);
7159 free(repo_path);
7160 free(in_repo_path);
7161 if (cmd_argv) {
7162 int i;
7163 for (i = 0; i < argc; i++)
7164 free(cmd_argv[i]);
7165 free(cmd_argv);
7167 tog_free_refs();
7168 return error;
7171 int
7172 main(int argc, char *argv[])
7174 const struct got_error *error = NULL;
7175 const struct tog_cmd *cmd = NULL;
7176 int ch, hflag = 0, Vflag = 0;
7177 char **cmd_argv = NULL;
7178 static const struct option longopts[] = {
7179 { "version", no_argument, NULL, 'V' },
7180 { NULL, 0, NULL, 0}
7183 setlocale(LC_CTYPE, "");
7185 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7186 switch (ch) {
7187 case 'h':
7188 hflag = 1;
7189 break;
7190 case 'V':
7191 Vflag = 1;
7192 break;
7193 default:
7194 usage(hflag, 1);
7195 /* NOTREACHED */
7199 argc -= optind;
7200 argv += optind;
7201 optind = 1;
7202 optreset = 1;
7204 if (Vflag) {
7205 got_version_print_str();
7206 return 0;
7209 #ifndef PROFILE
7210 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7211 NULL) == -1)
7212 err(1, "pledge");
7213 #endif
7215 if (argc == 0) {
7216 if (hflag)
7217 usage(hflag, 0);
7218 /* Build an argument vector which runs a default command. */
7219 cmd = &tog_commands[0];
7220 argc = 1;
7221 cmd_argv = make_argv(argc, cmd->name);
7222 } else {
7223 size_t i;
7225 /* Did the user specify a command? */
7226 for (i = 0; i < nitems(tog_commands); i++) {
7227 if (strncmp(tog_commands[i].name, argv[0],
7228 strlen(argv[0])) == 0) {
7229 cmd = &tog_commands[i];
7230 break;
7235 if (cmd == NULL) {
7236 if (argc != 1)
7237 usage(0, 1);
7238 /* No command specified; try log with a path */
7239 error = tog_log_with_path(argc, argv);
7240 } else {
7241 if (hflag)
7242 cmd->cmd_usage();
7243 else
7244 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7247 endwin();
7248 putchar('\n');
7249 if (cmd_argv) {
7250 int i;
7251 for (i = 0; i < argc; i++)
7252 free(cmd_argv[i]);
7253 free(cmd_argv);
7256 if (error && error->code != GOT_ERR_CANCELLED)
7257 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7258 return 0;