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);
2365 } else {
2366 entry = s->selected_entry;
2369 while (1) {
2370 int have_match = 0;
2372 if (entry == NULL) {
2373 if (s->thread_args.log_complete ||
2374 view->searching == TOG_SEARCH_BACKWARD) {
2375 view->search_next_done =
2376 (s->matched_entry == NULL ?
2377 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2378 s->search_entry = NULL;
2379 return NULL;
2382 * Poke the log thread for more commits and return,
2383 * allowing the main loop to make progress. Search
2384 * will resume at s->search_entry once we come back.
2386 s->thread_args.commits_needed++;
2387 return trigger_log_thread(view, 0);
2390 err = match_commit(&have_match, entry->id, entry->commit,
2391 &view->regex);
2392 if (err)
2393 break;
2394 if (have_match) {
2395 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2396 s->matched_entry = entry;
2397 break;
2400 s->search_entry = entry;
2401 if (view->searching == TOG_SEARCH_FORWARD)
2402 entry = TAILQ_NEXT(entry, entry);
2403 else
2404 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2407 if (s->matched_entry) {
2408 int cur = s->selected_entry->idx;
2409 while (cur < s->matched_entry->idx) {
2410 err = input_log_view(NULL, view, KEY_DOWN);
2411 if (err)
2412 return err;
2413 cur++;
2415 while (cur > s->matched_entry->idx) {
2416 err = input_log_view(NULL, view, KEY_UP);
2417 if (err)
2418 return err;
2419 cur--;
2423 s->search_entry = NULL;
2425 return NULL;
2428 static const struct got_error *
2429 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2430 struct got_repository *repo, const char *head_ref_name,
2431 const char *in_repo_path, int log_branches)
2433 const struct got_error *err = NULL;
2434 struct tog_log_view_state *s = &view->state.log;
2435 struct got_repository *thread_repo = NULL;
2436 struct got_commit_graph *thread_graph = NULL;
2437 int errcode;
2439 if (in_repo_path != s->in_repo_path) {
2440 free(s->in_repo_path);
2441 s->in_repo_path = strdup(in_repo_path);
2442 if (s->in_repo_path == NULL)
2443 return got_error_from_errno("strdup");
2446 /* The commit queue only contains commits being displayed. */
2447 TAILQ_INIT(&s->commits.head);
2448 s->commits.ncommits = 0;
2450 s->repo = repo;
2451 if (head_ref_name) {
2452 s->head_ref_name = strdup(head_ref_name);
2453 if (s->head_ref_name == NULL) {
2454 err = got_error_from_errno("strdup");
2455 goto done;
2458 s->start_id = got_object_id_dup(start_id);
2459 if (s->start_id == NULL) {
2460 err = got_error_from_errno("got_object_id_dup");
2461 goto done;
2463 s->log_branches = log_branches;
2465 STAILQ_INIT(&s->colors);
2466 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2467 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2468 get_color_value("TOG_COLOR_COMMIT"));
2469 if (err)
2470 goto done;
2471 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2472 get_color_value("TOG_COLOR_AUTHOR"));
2473 if (err) {
2474 free_colors(&s->colors);
2475 goto done;
2477 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2478 get_color_value("TOG_COLOR_DATE"));
2479 if (err) {
2480 free_colors(&s->colors);
2481 goto done;
2485 view->show = show_log_view;
2486 view->input = input_log_view;
2487 view->close = close_log_view;
2488 view->search_start = search_start_log_view;
2489 view->search_next = search_next_log_view;
2491 if (s->thread_args.pack_fds == NULL) {
2492 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2493 if (err)
2494 goto done;
2496 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2497 s->thread_args.pack_fds);
2498 if (err)
2499 goto done;
2500 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2501 !s->log_branches);
2502 if (err)
2503 goto done;
2504 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2505 s->repo, NULL, NULL);
2506 if (err)
2507 goto done;
2509 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2510 if (errcode) {
2511 err = got_error_set_errno(errcode, "pthread_cond_init");
2512 goto done;
2514 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2515 if (errcode) {
2516 err = got_error_set_errno(errcode, "pthread_cond_init");
2517 goto done;
2520 s->thread_args.commits_needed = view->nlines;
2521 s->thread_args.graph = thread_graph;
2522 s->thread_args.commits = &s->commits;
2523 s->thread_args.in_repo_path = s->in_repo_path;
2524 s->thread_args.start_id = s->start_id;
2525 s->thread_args.repo = thread_repo;
2526 s->thread_args.log_complete = 0;
2527 s->thread_args.quit = &s->quit;
2528 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2529 s->thread_args.selected_entry = &s->selected_entry;
2530 s->thread_args.searching = &view->searching;
2531 s->thread_args.search_next_done = &view->search_next_done;
2532 s->thread_args.regex = &view->regex;
2533 done:
2534 if (err)
2535 close_log_view(view);
2536 return err;
2539 static const struct got_error *
2540 show_log_view(struct tog_view *view)
2542 const struct got_error *err;
2543 struct tog_log_view_state *s = &view->state.log;
2545 if (s->thread == NULL) {
2546 int errcode = pthread_create(&s->thread, NULL, log_thread,
2547 &s->thread_args);
2548 if (errcode)
2549 return got_error_set_errno(errcode, "pthread_create");
2550 if (s->thread_args.commits_needed > 0) {
2551 err = trigger_log_thread(view, 1);
2552 if (err)
2553 return err;
2557 return draw_commits(view);
2560 static const struct got_error *
2561 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2563 const struct got_error *err = NULL;
2564 struct tog_log_view_state *s = &view->state.log;
2565 struct tog_view *diff_view = NULL, *tree_view = NULL;
2566 struct tog_view *ref_view = NULL;
2567 struct commit_queue_entry *entry;
2568 int begin_x = 0, n, nscroll = view->nlines - 1;
2570 if (s->thread_args.load_all) {
2571 if (ch == KEY_BACKSPACE)
2572 s->thread_args.load_all = 0;
2573 else if (s->thread_args.log_complete) {
2574 s->thread_args.load_all = 0;
2575 log_scroll_down(view, s->commits.ncommits);
2576 s->selected = MIN(view->nlines - 2,
2577 s->commits.ncommits - 1);
2578 select_commit(s);
2580 return NULL;
2583 switch (ch) {
2584 case 'q':
2585 s->quit = 1;
2586 break;
2587 case '0':
2588 view->x = 0;
2589 break;
2590 case '$':
2591 view->x = MAX(view->maxx - view->ncols / 2, 0);
2592 break;
2593 case KEY_RIGHT:
2594 case 'l':
2595 if (view->x + view->ncols / 2 < view->maxx)
2596 view->x += 2; /* move two columns right */
2597 break;
2598 case KEY_LEFT:
2599 case 'h':
2600 view->x -= MIN(view->x, 2); /* move two columns back */
2601 break;
2602 case 'k':
2603 case KEY_UP:
2604 case '<':
2605 case ',':
2606 case CTRL('p'):
2607 if (s->first_displayed_entry == NULL)
2608 break;
2609 if (s->selected > 0)
2610 s->selected--;
2611 else
2612 log_scroll_up(s, 1);
2613 select_commit(s);
2614 break;
2615 case 'g':
2616 case KEY_HOME:
2617 s->selected = 0;
2618 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2619 select_commit(s);
2620 break;
2621 case CTRL('u'):
2622 case 'u':
2623 nscroll /= 2;
2624 /* FALL THROUGH */
2625 case KEY_PPAGE:
2626 case CTRL('b'):
2627 if (s->first_displayed_entry == NULL)
2628 break;
2629 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2630 s->selected = MAX(0, s->selected - nscroll - 1);
2631 else
2632 log_scroll_up(s, nscroll);
2633 select_commit(s);
2634 break;
2635 case 'j':
2636 case KEY_DOWN:
2637 case '>':
2638 case '.':
2639 case CTRL('n'):
2640 if (s->first_displayed_entry == NULL)
2641 break;
2642 if (s->selected < MIN(view->nlines - 2,
2643 s->commits.ncommits - 1))
2644 s->selected++;
2645 else {
2646 err = log_scroll_down(view, 1);
2647 if (err)
2648 break;
2650 select_commit(s);
2651 break;
2652 case 'G':
2653 case KEY_END: {
2654 /* We don't know yet how many commits, so we're forced to
2655 * traverse them all. */
2656 if (!s->thread_args.log_complete) {
2657 s->thread_args.load_all = 1;
2658 return trigger_log_thread(view, 0);
2661 s->selected = 0;
2662 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2663 for (n = 0; n < view->nlines - 1; n++) {
2664 if (entry == NULL)
2665 break;
2666 s->first_displayed_entry = entry;
2667 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2669 if (n > 0)
2670 s->selected = n - 1;
2671 select_commit(s);
2672 break;
2674 case CTRL('d'):
2675 case 'd':
2676 nscroll /= 2;
2677 /* FALL THROUGH */
2678 case KEY_NPAGE:
2679 case CTRL('f'): {
2680 struct commit_queue_entry *first;
2681 first = s->first_displayed_entry;
2682 if (first == NULL)
2683 break;
2684 err = log_scroll_down(view, nscroll);
2685 if (err)
2686 break;
2687 if (first == s->first_displayed_entry &&
2688 s->selected < MIN(view->nlines - 2,
2689 s->commits.ncommits - 1)) {
2690 /* can't scroll further down */
2691 s->selected += MIN(s->last_displayed_entry->idx -
2692 s->selected_entry->idx, nscroll + 1);
2694 select_commit(s);
2695 break;
2697 case KEY_RESIZE:
2698 if (s->selected > view->nlines - 2)
2699 s->selected = view->nlines - 2;
2700 if (s->selected > s->commits.ncommits - 1)
2701 s->selected = s->commits.ncommits - 1;
2702 select_commit(s);
2703 if (s->commits.ncommits < view->nlines - 1 &&
2704 !s->thread_args.log_complete) {
2705 s->thread_args.commits_needed += (view->nlines - 1) -
2706 s->commits.ncommits;
2707 err = trigger_log_thread(view, 1);
2709 break;
2710 case KEY_ENTER:
2711 case ' ':
2712 case '\r':
2713 if (s->selected_entry == NULL)
2714 break;
2715 if (view_is_parent_view(view))
2716 begin_x = view_split_begin_x(view->begin_x);
2717 err = open_diff_view_for_commit(&diff_view, begin_x,
2718 s->selected_entry->commit, s->selected_entry->id,
2719 view, s->repo);
2720 if (err)
2721 break;
2722 view->focussed = 0;
2723 diff_view->focussed = 1;
2724 if (view_is_parent_view(view)) {
2725 err = view_close_child(view);
2726 if (err)
2727 return err;
2728 err = view_set_child(view, diff_view);
2729 if (err)
2730 return err;
2731 view->focus_child = 1;
2732 } else
2733 *new_view = diff_view;
2734 break;
2735 case 't':
2736 if (s->selected_entry == NULL)
2737 break;
2738 if (view_is_parent_view(view))
2739 begin_x = view_split_begin_x(view->begin_x);
2740 err = browse_commit_tree(&tree_view, begin_x,
2741 s->selected_entry, s->in_repo_path, s->head_ref_name,
2742 s->repo);
2743 if (err)
2744 break;
2745 view->focussed = 0;
2746 tree_view->focussed = 1;
2747 if (view_is_parent_view(view)) {
2748 err = view_close_child(view);
2749 if (err)
2750 return err;
2751 err = view_set_child(view, tree_view);
2752 if (err)
2753 return err;
2754 view->focus_child = 1;
2755 } else
2756 *new_view = tree_view;
2757 break;
2758 case KEY_BACKSPACE:
2759 case CTRL('l'):
2760 case 'B':
2761 if (ch == KEY_BACKSPACE &&
2762 got_path_is_root_dir(s->in_repo_path))
2763 break;
2764 err = stop_log_thread(s);
2765 if (err)
2766 return err;
2767 if (ch == KEY_BACKSPACE) {
2768 char *parent_path;
2769 err = got_path_dirname(&parent_path, s->in_repo_path);
2770 if (err)
2771 return err;
2772 free(s->in_repo_path);
2773 s->in_repo_path = parent_path;
2774 s->thread_args.in_repo_path = s->in_repo_path;
2775 } else if (ch == CTRL('l')) {
2776 struct got_object_id *start_id;
2777 err = got_repo_match_object_id(&start_id, NULL,
2778 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2779 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2780 if (err)
2781 return err;
2782 free(s->start_id);
2783 s->start_id = start_id;
2784 s->thread_args.start_id = s->start_id;
2785 } else /* 'B' */
2786 s->log_branches = !s->log_branches;
2788 if (s->thread_args.pack_fds == NULL) {
2789 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2790 if (err)
2791 return err;
2793 err = got_repo_open(&s->thread_args.repo,
2794 got_repo_get_path(s->repo), NULL,
2795 s->thread_args.pack_fds);
2796 if (err)
2797 return err;
2798 tog_free_refs();
2799 err = tog_load_refs(s->repo, 0);
2800 if (err)
2801 return err;
2802 err = got_commit_graph_open(&s->thread_args.graph,
2803 s->in_repo_path, !s->log_branches);
2804 if (err)
2805 return err;
2806 err = got_commit_graph_iter_start(s->thread_args.graph,
2807 s->start_id, s->repo, NULL, NULL);
2808 if (err)
2809 return err;
2810 free_commits(&s->commits);
2811 s->first_displayed_entry = NULL;
2812 s->last_displayed_entry = NULL;
2813 s->selected_entry = NULL;
2814 s->selected = 0;
2815 s->thread_args.log_complete = 0;
2816 s->quit = 0;
2817 s->thread_args.commits_needed = view->nlines;
2818 s->matched_entry = NULL;
2819 s->search_entry = NULL;
2820 break;
2821 case 'r':
2822 if (view_is_parent_view(view))
2823 begin_x = view_split_begin_x(view->begin_x);
2824 ref_view = view_open(view->nlines, view->ncols,
2825 view->begin_y, begin_x, TOG_VIEW_REF);
2826 if (ref_view == NULL)
2827 return got_error_from_errno("view_open");
2828 err = open_ref_view(ref_view, s->repo);
2829 if (err) {
2830 view_close(ref_view);
2831 return err;
2833 view->focussed = 0;
2834 ref_view->focussed = 1;
2835 if (view_is_parent_view(view)) {
2836 err = view_close_child(view);
2837 if (err)
2838 return err;
2839 err = view_set_child(view, ref_view);
2840 if (err)
2841 return err;
2842 view->focus_child = 1;
2843 } else
2844 *new_view = ref_view;
2845 break;
2846 default:
2847 break;
2850 return err;
2853 static const struct got_error *
2854 apply_unveil(const char *repo_path, const char *worktree_path)
2856 const struct got_error *error;
2858 #ifdef PROFILE
2859 if (unveil("gmon.out", "rwc") != 0)
2860 return got_error_from_errno2("unveil", "gmon.out");
2861 #endif
2862 if (repo_path && unveil(repo_path, "r") != 0)
2863 return got_error_from_errno2("unveil", repo_path);
2865 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2866 return got_error_from_errno2("unveil", worktree_path);
2868 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2869 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2871 error = got_privsep_unveil_exec_helpers();
2872 if (error != NULL)
2873 return error;
2875 if (unveil(NULL, NULL) != 0)
2876 return got_error_from_errno("unveil");
2878 return NULL;
2881 static void
2882 init_curses(void)
2885 * Override default signal handlers before starting ncurses.
2886 * This should prevent ncurses from installing its own
2887 * broken cleanup() signal handler.
2889 signal(SIGWINCH, tog_sigwinch);
2890 signal(SIGPIPE, tog_sigpipe);
2891 signal(SIGCONT, tog_sigcont);
2892 signal(SIGINT, tog_sigint);
2893 signal(SIGTERM, tog_sigterm);
2895 initscr();
2896 cbreak();
2897 halfdelay(1); /* Do fast refresh while initial view is loading. */
2898 noecho();
2899 nonl();
2900 intrflush(stdscr, FALSE);
2901 keypad(stdscr, TRUE);
2902 curs_set(0);
2903 if (getenv("TOG_COLORS") != NULL) {
2904 start_color();
2905 use_default_colors();
2909 static const struct got_error *
2910 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2911 struct got_repository *repo, struct got_worktree *worktree)
2913 const struct got_error *err = NULL;
2915 if (argc == 0) {
2916 *in_repo_path = strdup("/");
2917 if (*in_repo_path == NULL)
2918 return got_error_from_errno("strdup");
2919 return NULL;
2922 if (worktree) {
2923 const char *prefix = got_worktree_get_path_prefix(worktree);
2924 char *p;
2926 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2927 if (err)
2928 return err;
2929 if (asprintf(in_repo_path, "%s%s%s", prefix,
2930 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2931 p) == -1) {
2932 err = got_error_from_errno("asprintf");
2933 *in_repo_path = NULL;
2935 free(p);
2936 } else
2937 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2939 return err;
2942 static const struct got_error *
2943 cmd_log(int argc, char *argv[])
2945 const struct got_error *error;
2946 struct got_repository *repo = NULL;
2947 struct got_worktree *worktree = NULL;
2948 struct got_object_id *start_id = NULL;
2949 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2950 char *start_commit = NULL, *label = NULL;
2951 struct got_reference *ref = NULL;
2952 const char *head_ref_name = NULL;
2953 int ch, log_branches = 0;
2954 struct tog_view *view;
2955 int *pack_fds = NULL;
2957 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2958 switch (ch) {
2959 case 'b':
2960 log_branches = 1;
2961 break;
2962 case 'c':
2963 start_commit = optarg;
2964 break;
2965 case 'r':
2966 repo_path = realpath(optarg, NULL);
2967 if (repo_path == NULL)
2968 return got_error_from_errno2("realpath",
2969 optarg);
2970 break;
2971 default:
2972 usage_log();
2973 /* NOTREACHED */
2977 argc -= optind;
2978 argv += optind;
2980 if (argc > 1)
2981 usage_log();
2983 error = got_repo_pack_fds_open(&pack_fds);
2984 if (error != NULL)
2985 goto done;
2987 if (repo_path == NULL) {
2988 cwd = getcwd(NULL, 0);
2989 if (cwd == NULL)
2990 return got_error_from_errno("getcwd");
2991 error = got_worktree_open(&worktree, cwd);
2992 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2993 goto done;
2994 if (worktree)
2995 repo_path =
2996 strdup(got_worktree_get_repo_path(worktree));
2997 else
2998 repo_path = strdup(cwd);
2999 if (repo_path == NULL) {
3000 error = got_error_from_errno("strdup");
3001 goto done;
3005 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3006 if (error != NULL)
3007 goto done;
3009 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3010 repo, worktree);
3011 if (error)
3012 goto done;
3014 init_curses();
3016 error = apply_unveil(got_repo_get_path(repo),
3017 worktree ? got_worktree_get_root_path(worktree) : NULL);
3018 if (error)
3019 goto done;
3021 /* already loaded by tog_log_with_path()? */
3022 if (TAILQ_EMPTY(&tog_refs)) {
3023 error = tog_load_refs(repo, 0);
3024 if (error)
3025 goto done;
3028 if (start_commit == NULL) {
3029 error = got_repo_match_object_id(&start_id, &label,
3030 worktree ? got_worktree_get_head_ref_name(worktree) :
3031 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3032 if (error)
3033 goto done;
3034 head_ref_name = label;
3035 } else {
3036 error = got_ref_open(&ref, repo, start_commit, 0);
3037 if (error == NULL)
3038 head_ref_name = got_ref_get_name(ref);
3039 else if (error->code != GOT_ERR_NOT_REF)
3040 goto done;
3041 error = got_repo_match_object_id(&start_id, NULL,
3042 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3043 if (error)
3044 goto done;
3047 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3048 if (view == NULL) {
3049 error = got_error_from_errno("view_open");
3050 goto done;
3052 error = open_log_view(view, start_id, repo, head_ref_name,
3053 in_repo_path, log_branches);
3054 if (error)
3055 goto done;
3056 if (worktree) {
3057 /* Release work tree lock. */
3058 got_worktree_close(worktree);
3059 worktree = NULL;
3061 error = view_loop(view);
3062 done:
3063 free(in_repo_path);
3064 free(repo_path);
3065 free(cwd);
3066 free(start_id);
3067 free(label);
3068 if (ref)
3069 got_ref_close(ref);
3070 if (repo) {
3071 const struct got_error *close_err = got_repo_close(repo);
3072 if (error == NULL)
3073 error = close_err;
3075 if (worktree)
3076 got_worktree_close(worktree);
3077 if (pack_fds) {
3078 const struct got_error *pack_err =
3079 got_repo_pack_fds_close(pack_fds);
3080 if (error == NULL)
3081 error = pack_err;
3083 tog_free_refs();
3084 return error;
3087 __dead static void
3088 usage_diff(void)
3090 endwin();
3091 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3092 "[-w] object1 object2\n", getprogname());
3093 exit(1);
3096 static int
3097 match_line(const char *line, regex_t *regex, size_t nmatch,
3098 regmatch_t *regmatch)
3100 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3103 struct tog_color *
3104 match_color(struct tog_colors *colors, const char *line)
3106 struct tog_color *tc = NULL;
3108 STAILQ_FOREACH(tc, colors, entry) {
3109 if (match_line(line, &tc->regex, 0, NULL))
3110 return tc;
3113 return NULL;
3116 static const struct got_error *
3117 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3118 WINDOW *window, int skipcol, regmatch_t *regmatch)
3120 const struct got_error *err = NULL;
3121 char *exstr = NULL;
3122 wchar_t *wline = NULL;
3123 int rme, rms, n, width, scrollx;
3124 int width0 = 0, width1 = 0, width2 = 0;
3125 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3127 *wtotal = 0;
3129 rms = regmatch->rm_so;
3130 rme = regmatch->rm_eo;
3132 err = expand_tab(&exstr, line);
3133 if (err)
3134 return err;
3136 /* Split the line into 3 segments, according to match offsets. */
3137 seg0 = strndup(exstr, rms);
3138 if (seg0 == NULL) {
3139 err = got_error_from_errno("strndup");
3140 goto done;
3142 seg1 = strndup(exstr + rms, rme - rms);
3143 if (seg1 == NULL) {
3144 err = got_error_from_errno("strndup");
3145 goto done;
3147 seg2 = strdup(exstr + rme);
3148 if (seg2 == NULL) {
3149 err = got_error_from_errno("strndup");
3150 goto done;
3153 /* draw up to matched token if we haven't scrolled past it */
3154 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3155 col_tab_align, 1);
3156 if (err)
3157 goto done;
3158 n = MAX(width0 - skipcol, 0);
3159 if (n) {
3160 free(wline);
3161 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3162 wlimit, col_tab_align, 1);
3163 if (err)
3164 goto done;
3165 waddwstr(window, &wline[scrollx]);
3166 wlimit -= width;
3167 *wtotal += width;
3170 if (wlimit > 0) {
3171 int i = 0, w = 0;
3172 size_t wlen;
3174 free(wline);
3175 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3176 col_tab_align, 1);
3177 if (err)
3178 goto done;
3179 wlen = wcslen(wline);
3180 while (i < wlen) {
3181 width = wcwidth(wline[i]);
3182 if (width == -1) {
3183 /* should not happen, tabs are expanded */
3184 err = got_error(GOT_ERR_RANGE);
3185 goto done;
3187 if (width0 + w + width > skipcol)
3188 break;
3189 w += width;
3190 i++;
3192 /* draw (visible part of) matched token (if scrolled into it) */
3193 if (width1 - w > 0) {
3194 wattron(window, A_STANDOUT);
3195 waddwstr(window, &wline[i]);
3196 wattroff(window, A_STANDOUT);
3197 wlimit -= (width1 - w);
3198 *wtotal += (width1 - w);
3202 if (wlimit > 0) { /* draw rest of line */
3203 free(wline);
3204 if (skipcol > width0 + width1) {
3205 err = format_line(&wline, &width2, &scrollx, seg2,
3206 skipcol - (width0 + width1), wlimit,
3207 col_tab_align, 1);
3208 if (err)
3209 goto done;
3210 waddwstr(window, &wline[scrollx]);
3211 } else {
3212 err = format_line(&wline, &width2, NULL, seg2, 0,
3213 wlimit, col_tab_align, 1);
3214 if (err)
3215 goto done;
3216 waddwstr(window, wline);
3218 *wtotal += width2;
3220 done:
3221 free(wline);
3222 free(exstr);
3223 free(seg0);
3224 free(seg1);
3225 free(seg2);
3226 return err;
3229 static const struct got_error *
3230 draw_file(struct tog_view *view, const char *header)
3232 struct tog_diff_view_state *s = &view->state.diff;
3233 regmatch_t *regmatch = &view->regmatch;
3234 const struct got_error *err;
3235 int nprinted = 0;
3236 char *line;
3237 size_t linesize = 0;
3238 ssize_t linelen;
3239 struct tog_color *tc;
3240 wchar_t *wline;
3241 int width;
3242 int max_lines = view->nlines;
3243 int nlines = s->nlines;
3244 off_t line_offset;
3246 line_offset = s->line_offsets[s->first_displayed_line - 1];
3247 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3248 return got_error_from_errno("fseek");
3250 werase(view->window);
3252 if (header) {
3253 if (asprintf(&line, "[%d/%d] %s",
3254 s->first_displayed_line - 1 + s->selected_line, nlines,
3255 header) == -1)
3256 return got_error_from_errno("asprintf");
3257 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3258 0, 0);
3259 free(line);
3260 if (err)
3261 return err;
3263 if (view_needs_focus_indication(view))
3264 wstandout(view->window);
3265 waddwstr(view->window, wline);
3266 free(wline);
3267 wline = NULL;
3268 if (view_needs_focus_indication(view))
3269 wstandend(view->window);
3270 if (width <= view->ncols - 1)
3271 waddch(view->window, '\n');
3273 if (max_lines <= 1)
3274 return NULL;
3275 max_lines--;
3278 s->eof = 0;
3279 view->maxx = 0;
3280 line = NULL;
3281 while (max_lines > 0 && nprinted < max_lines) {
3282 linelen = getline(&line, &linesize, s->f);
3283 if (linelen == -1) {
3284 if (feof(s->f)) {
3285 s->eof = 1;
3286 break;
3288 free(line);
3289 return got_ferror(s->f, GOT_ERR_IO);
3292 /* Set view->maxx based on full line length. */
3293 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3294 view->x ? 1 : 0);
3295 if (err) {
3296 free(line);
3297 return err;
3299 view->maxx = MAX(view->maxx, width);
3300 free(wline);
3301 wline = NULL;
3303 tc = match_color(&s->colors, line);
3304 if (tc)
3305 wattr_on(view->window,
3306 COLOR_PAIR(tc->colorpair), NULL);
3307 if (s->first_displayed_line + nprinted == s->matched_line &&
3308 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3309 err = add_matched_line(&width, line, view->ncols, 0,
3310 view->window, view->x, regmatch);
3311 if (err) {
3312 free(line);
3313 return err;
3315 } else {
3316 int skip;
3317 err = format_line(&wline, &width, &skip, line,
3318 view->x, view->ncols, 0, view->x ? 1 : 0);
3319 if (err) {
3320 free(line);
3321 return err;
3323 waddwstr(view->window, &wline[skip]);
3324 free(wline);
3325 wline = NULL;
3327 if (tc)
3328 wattr_off(view->window,
3329 COLOR_PAIR(tc->colorpair), NULL);
3330 if (width <= view->ncols - 1)
3331 waddch(view->window, '\n');
3332 nprinted++;
3334 free(line);
3335 if (nprinted >= 1)
3336 s->last_displayed_line = s->first_displayed_line +
3337 (nprinted - 1);
3338 else
3339 s->last_displayed_line = s->first_displayed_line;
3341 view_vborder(view);
3343 if (s->eof) {
3344 while (nprinted < view->nlines) {
3345 waddch(view->window, '\n');
3346 nprinted++;
3349 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3350 view->ncols, 0, 0);
3351 if (err) {
3352 return err;
3355 wstandout(view->window);
3356 waddwstr(view->window, wline);
3357 free(wline);
3358 wline = NULL;
3359 wstandend(view->window);
3362 return NULL;
3365 static char *
3366 get_datestr(time_t *time, char *datebuf)
3368 struct tm mytm, *tm;
3369 char *p, *s;
3371 tm = gmtime_r(time, &mytm);
3372 if (tm == NULL)
3373 return NULL;
3374 s = asctime_r(tm, datebuf);
3375 if (s == NULL)
3376 return NULL;
3377 p = strchr(s, '\n');
3378 if (p)
3379 *p = '\0';
3380 return s;
3383 static const struct got_error *
3384 get_changed_paths(struct got_pathlist_head *paths,
3385 struct got_commit_object *commit, struct got_repository *repo)
3387 const struct got_error *err = NULL;
3388 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3389 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3390 struct got_object_qid *qid;
3392 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3393 if (qid != NULL) {
3394 struct got_commit_object *pcommit;
3395 err = got_object_open_as_commit(&pcommit, repo,
3396 &qid->id);
3397 if (err)
3398 return err;
3400 tree_id1 = got_object_id_dup(
3401 got_object_commit_get_tree_id(pcommit));
3402 if (tree_id1 == NULL) {
3403 got_object_commit_close(pcommit);
3404 return got_error_from_errno("got_object_id_dup");
3406 got_object_commit_close(pcommit);
3410 if (tree_id1) {
3411 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3412 if (err)
3413 goto done;
3416 tree_id2 = got_object_commit_get_tree_id(commit);
3417 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3418 if (err)
3419 goto done;
3421 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3422 got_diff_tree_collect_changed_paths, paths, 0);
3423 done:
3424 if (tree1)
3425 got_object_tree_close(tree1);
3426 if (tree2)
3427 got_object_tree_close(tree2);
3428 free(tree_id1);
3429 return err;
3432 static const struct got_error *
3433 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3435 off_t *p;
3437 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3438 if (p == NULL)
3439 return got_error_from_errno("reallocarray");
3440 *line_offsets = p;
3441 (*line_offsets)[*nlines] = off;
3442 (*nlines)++;
3443 return NULL;
3446 static const struct got_error *
3447 write_commit_info(off_t **line_offsets, size_t *nlines,
3448 struct got_object_id *commit_id, struct got_reflist_head *refs,
3449 struct got_repository *repo, FILE *outfile)
3451 const struct got_error *err = NULL;
3452 char datebuf[26], *datestr;
3453 struct got_commit_object *commit;
3454 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3455 time_t committer_time;
3456 const char *author, *committer;
3457 char *refs_str = NULL;
3458 struct got_pathlist_head changed_paths;
3459 struct got_pathlist_entry *pe;
3460 off_t outoff = 0;
3461 int n;
3463 TAILQ_INIT(&changed_paths);
3465 if (refs) {
3466 err = build_refs_str(&refs_str, refs, commit_id, repo);
3467 if (err)
3468 return err;
3471 err = got_object_open_as_commit(&commit, repo, commit_id);
3472 if (err)
3473 return err;
3475 err = got_object_id_str(&id_str, commit_id);
3476 if (err) {
3477 err = got_error_from_errno("got_object_id_str");
3478 goto done;
3481 err = add_line_offset(line_offsets, nlines, 0);
3482 if (err)
3483 goto done;
3485 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3486 refs_str ? refs_str : "", refs_str ? ")" : "");
3487 if (n < 0) {
3488 err = got_error_from_errno("fprintf");
3489 goto done;
3491 outoff += n;
3492 err = add_line_offset(line_offsets, nlines, outoff);
3493 if (err)
3494 goto done;
3496 n = fprintf(outfile, "from: %s\n",
3497 got_object_commit_get_author(commit));
3498 if (n < 0) {
3499 err = got_error_from_errno("fprintf");
3500 goto done;
3502 outoff += n;
3503 err = add_line_offset(line_offsets, nlines, outoff);
3504 if (err)
3505 goto done;
3507 committer_time = got_object_commit_get_committer_time(commit);
3508 datestr = get_datestr(&committer_time, datebuf);
3509 if (datestr) {
3510 n = fprintf(outfile, "date: %s UTC\n", datestr);
3511 if (n < 0) {
3512 err = got_error_from_errno("fprintf");
3513 goto done;
3515 outoff += n;
3516 err = add_line_offset(line_offsets, nlines, outoff);
3517 if (err)
3518 goto done;
3520 author = got_object_commit_get_author(commit);
3521 committer = got_object_commit_get_committer(commit);
3522 if (strcmp(author, committer) != 0) {
3523 n = fprintf(outfile, "via: %s\n", committer);
3524 if (n < 0) {
3525 err = got_error_from_errno("fprintf");
3526 goto done;
3528 outoff += n;
3529 err = add_line_offset(line_offsets, nlines, outoff);
3530 if (err)
3531 goto done;
3533 if (got_object_commit_get_nparents(commit) > 1) {
3534 const struct got_object_id_queue *parent_ids;
3535 struct got_object_qid *qid;
3536 int pn = 1;
3537 parent_ids = got_object_commit_get_parent_ids(commit);
3538 STAILQ_FOREACH(qid, parent_ids, entry) {
3539 err = got_object_id_str(&id_str, &qid->id);
3540 if (err)
3541 goto done;
3542 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3543 if (n < 0) {
3544 err = got_error_from_errno("fprintf");
3545 goto done;
3547 outoff += n;
3548 err = add_line_offset(line_offsets, nlines, outoff);
3549 if (err)
3550 goto done;
3551 free(id_str);
3552 id_str = NULL;
3556 err = got_object_commit_get_logmsg(&logmsg, commit);
3557 if (err)
3558 goto done;
3559 s = logmsg;
3560 while ((line = strsep(&s, "\n")) != NULL) {
3561 n = fprintf(outfile, "%s\n", line);
3562 if (n < 0) {
3563 err = got_error_from_errno("fprintf");
3564 goto done;
3566 outoff += n;
3567 err = add_line_offset(line_offsets, nlines, outoff);
3568 if (err)
3569 goto done;
3572 err = get_changed_paths(&changed_paths, commit, repo);
3573 if (err)
3574 goto done;
3575 TAILQ_FOREACH(pe, &changed_paths, entry) {
3576 struct got_diff_changed_path *cp = pe->data;
3577 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3578 if (n < 0) {
3579 err = got_error_from_errno("fprintf");
3580 goto done;
3582 outoff += n;
3583 err = add_line_offset(line_offsets, nlines, outoff);
3584 if (err)
3585 goto done;
3586 free((char *)pe->path);
3587 free(pe->data);
3590 fputc('\n', outfile);
3591 outoff++;
3592 err = add_line_offset(line_offsets, nlines, outoff);
3593 done:
3594 got_pathlist_free(&changed_paths);
3595 free(id_str);
3596 free(logmsg);
3597 free(refs_str);
3598 got_object_commit_close(commit);
3599 if (err) {
3600 free(*line_offsets);
3601 *line_offsets = NULL;
3602 *nlines = 0;
3604 return err;
3607 static const struct got_error *
3608 create_diff(struct tog_diff_view_state *s)
3610 const struct got_error *err = NULL;
3611 FILE *f = NULL;
3612 int obj_type;
3614 free(s->line_offsets);
3615 s->line_offsets = malloc(sizeof(off_t));
3616 if (s->line_offsets == NULL)
3617 return got_error_from_errno("malloc");
3618 s->nlines = 0;
3620 f = got_opentemp();
3621 if (f == NULL) {
3622 err = got_error_from_errno("got_opentemp");
3623 goto done;
3625 if (s->f && fclose(s->f) == EOF) {
3626 err = got_error_from_errno("fclose");
3627 goto done;
3629 s->f = f;
3631 if (s->id1)
3632 err = got_object_get_type(&obj_type, s->repo, s->id1);
3633 else
3634 err = got_object_get_type(&obj_type, s->repo, s->id2);
3635 if (err)
3636 goto done;
3638 switch (obj_type) {
3639 case GOT_OBJ_TYPE_BLOB:
3640 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3641 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3642 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3643 s->repo, s->f);
3644 break;
3645 case GOT_OBJ_TYPE_TREE:
3646 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3647 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3648 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3649 break;
3650 case GOT_OBJ_TYPE_COMMIT: {
3651 const struct got_object_id_queue *parent_ids;
3652 struct got_object_qid *pid;
3653 struct got_commit_object *commit2;
3654 struct got_reflist_head *refs;
3656 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3657 if (err)
3658 goto done;
3659 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3660 /* Show commit info if we're diffing to a parent/root commit. */
3661 if (s->id1 == NULL) {
3662 err = write_commit_info(&s->line_offsets, &s->nlines,
3663 s->id2, refs, s->repo, s->f);
3664 if (err)
3665 goto done;
3666 } else {
3667 parent_ids = got_object_commit_get_parent_ids(commit2);
3668 STAILQ_FOREACH(pid, parent_ids, entry) {
3669 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3670 err = write_commit_info(
3671 &s->line_offsets, &s->nlines,
3672 s->id2, refs, s->repo, s->f);
3673 if (err)
3674 goto done;
3675 break;
3679 got_object_commit_close(commit2);
3681 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3682 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3683 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3684 break;
3686 default:
3687 err = got_error(GOT_ERR_OBJ_TYPE);
3688 break;
3690 if (err)
3691 goto done;
3692 done:
3693 if (s->f && fflush(s->f) != 0 && err == NULL)
3694 err = got_error_from_errno("fflush");
3695 return err;
3698 static void
3699 diff_view_indicate_progress(struct tog_view *view)
3701 mvwaddstr(view->window, 0, 0, "diffing...");
3702 update_panels();
3703 doupdate();
3706 static const struct got_error *
3707 search_start_diff_view(struct tog_view *view)
3709 struct tog_diff_view_state *s = &view->state.diff;
3711 s->matched_line = 0;
3712 return NULL;
3715 static const struct got_error *
3716 search_next_diff_view(struct tog_view *view)
3718 struct tog_diff_view_state *s = &view->state.diff;
3719 const struct got_error *err = NULL;
3720 int lineno;
3721 char *line = NULL;
3722 size_t linesize = 0;
3723 ssize_t linelen;
3725 if (!view->searching) {
3726 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3727 return NULL;
3730 if (s->matched_line) {
3731 if (view->searching == TOG_SEARCH_FORWARD)
3732 lineno = s->matched_line + 1;
3733 else
3734 lineno = s->matched_line - 1;
3735 } else
3736 lineno = s->first_displayed_line;
3738 while (1) {
3739 off_t offset;
3741 if (lineno <= 0 || lineno > s->nlines) {
3742 if (s->matched_line == 0) {
3743 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3744 break;
3747 if (view->searching == TOG_SEARCH_FORWARD)
3748 lineno = 1;
3749 else
3750 lineno = s->nlines;
3753 offset = s->line_offsets[lineno - 1];
3754 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3755 free(line);
3756 return got_error_from_errno("fseeko");
3758 linelen = getline(&line, &linesize, s->f);
3759 if (linelen != -1) {
3760 char *exstr;
3761 err = expand_tab(&exstr, line);
3762 if (err)
3763 break;
3764 if (match_line(exstr, &view->regex, 1,
3765 &view->regmatch)) {
3766 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3767 s->matched_line = lineno;
3768 free(exstr);
3769 break;
3771 free(exstr);
3773 if (view->searching == TOG_SEARCH_FORWARD)
3774 lineno++;
3775 else
3776 lineno--;
3778 free(line);
3780 if (s->matched_line) {
3781 s->first_displayed_line = s->matched_line;
3782 s->selected_line = 1;
3785 return err;
3788 static const struct got_error *
3789 close_diff_view(struct tog_view *view)
3791 const struct got_error *err = NULL;
3792 struct tog_diff_view_state *s = &view->state.diff;
3794 free(s->id1);
3795 s->id1 = NULL;
3796 free(s->id2);
3797 s->id2 = NULL;
3798 if (s->f && fclose(s->f) == EOF)
3799 err = got_error_from_errno("fclose");
3800 s->f = NULL;
3801 if (s->f1 && fclose(s->f1) == EOF)
3802 err = got_error_from_errno("fclose");
3803 s->f1 = NULL;
3804 if (s->f2 && fclose(s->f2) == EOF)
3805 err = got_error_from_errno("fclose");
3806 s->f2 = NULL;
3807 free_colors(&s->colors);
3808 free(s->line_offsets);
3809 s->line_offsets = NULL;
3810 s->nlines = 0;
3811 return err;
3814 static const struct got_error *
3815 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3816 struct got_object_id *id2, const char *label1, const char *label2,
3817 int diff_context, int ignore_whitespace, int force_text_diff,
3818 struct tog_view *log_view, struct got_repository *repo)
3820 const struct got_error *err;
3821 struct tog_diff_view_state *s = &view->state.diff;
3823 memset(s, 0, sizeof(*s));
3825 if (id1 != NULL && id2 != NULL) {
3826 int type1, type2;
3827 err = got_object_get_type(&type1, repo, id1);
3828 if (err)
3829 return err;
3830 err = got_object_get_type(&type2, repo, id2);
3831 if (err)
3832 return err;
3834 if (type1 != type2)
3835 return got_error(GOT_ERR_OBJ_TYPE);
3837 s->first_displayed_line = 1;
3838 s->last_displayed_line = view->nlines;
3839 s->selected_line = 1;
3840 s->repo = repo;
3841 s->id1 = id1;
3842 s->id2 = id2;
3843 s->label1 = label1;
3844 s->label2 = label2;
3846 if (id1) {
3847 s->id1 = got_object_id_dup(id1);
3848 if (s->id1 == NULL)
3849 return got_error_from_errno("got_object_id_dup");
3850 } else
3851 s->id1 = NULL;
3853 s->id2 = got_object_id_dup(id2);
3854 if (s->id2 == NULL) {
3855 err = got_error_from_errno("got_object_id_dup");
3856 goto done;
3859 s->f1 = got_opentemp();
3860 if (s->f1 == NULL) {
3861 err = got_error_from_errno("got_opentemp");
3862 goto done;
3865 s->f2 = got_opentemp();
3866 if (s->f2 == NULL) {
3867 err = got_error_from_errno("got_opentemp");
3868 goto done;
3871 s->first_displayed_line = 1;
3872 s->last_displayed_line = view->nlines;
3873 s->diff_context = diff_context;
3874 s->ignore_whitespace = ignore_whitespace;
3875 s->force_text_diff = force_text_diff;
3876 s->log_view = log_view;
3877 s->repo = repo;
3879 STAILQ_INIT(&s->colors);
3880 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3881 err = add_color(&s->colors,
3882 "^-", TOG_COLOR_DIFF_MINUS,
3883 get_color_value("TOG_COLOR_DIFF_MINUS"));
3884 if (err)
3885 goto done;
3886 err = add_color(&s->colors, "^\\+",
3887 TOG_COLOR_DIFF_PLUS,
3888 get_color_value("TOG_COLOR_DIFF_PLUS"));
3889 if (err)
3890 goto done;
3891 err = add_color(&s->colors,
3892 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3893 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3894 if (err)
3895 goto done;
3897 err = add_color(&s->colors,
3898 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3899 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3900 get_color_value("TOG_COLOR_DIFF_META"));
3901 if (err)
3902 goto done;
3904 err = add_color(&s->colors,
3905 "^(from|via): ", TOG_COLOR_AUTHOR,
3906 get_color_value("TOG_COLOR_AUTHOR"));
3907 if (err)
3908 goto done;
3910 err = add_color(&s->colors,
3911 "^date: ", TOG_COLOR_DATE,
3912 get_color_value("TOG_COLOR_DATE"));
3913 if (err)
3914 goto done;
3917 if (log_view && view_is_splitscreen(view))
3918 show_log_view(log_view); /* draw vborder */
3919 diff_view_indicate_progress(view);
3921 err = create_diff(s);
3923 view->show = show_diff_view;
3924 view->input = input_diff_view;
3925 view->close = close_diff_view;
3926 view->search_start = search_start_diff_view;
3927 view->search_next = search_next_diff_view;
3928 done:
3929 if (err)
3930 close_diff_view(view);
3931 return err;
3934 static const struct got_error *
3935 show_diff_view(struct tog_view *view)
3937 const struct got_error *err;
3938 struct tog_diff_view_state *s = &view->state.diff;
3939 char *id_str1 = NULL, *id_str2, *header;
3940 const char *label1, *label2;
3942 if (s->id1) {
3943 err = got_object_id_str(&id_str1, s->id1);
3944 if (err)
3945 return err;
3946 label1 = s->label1 ? : id_str1;
3947 } else
3948 label1 = "/dev/null";
3950 err = got_object_id_str(&id_str2, s->id2);
3951 if (err)
3952 return err;
3953 label2 = s->label2 ? : id_str2;
3955 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3956 err = got_error_from_errno("asprintf");
3957 free(id_str1);
3958 free(id_str2);
3959 return err;
3961 free(id_str1);
3962 free(id_str2);
3964 err = draw_file(view, header);
3965 free(header);
3966 return err;
3969 static const struct got_error *
3970 set_selected_commit(struct tog_diff_view_state *s,
3971 struct commit_queue_entry *entry)
3973 const struct got_error *err;
3974 const struct got_object_id_queue *parent_ids;
3975 struct got_commit_object *selected_commit;
3976 struct got_object_qid *pid;
3978 free(s->id2);
3979 s->id2 = got_object_id_dup(entry->id);
3980 if (s->id2 == NULL)
3981 return got_error_from_errno("got_object_id_dup");
3983 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3984 if (err)
3985 return err;
3986 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3987 free(s->id1);
3988 pid = STAILQ_FIRST(parent_ids);
3989 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3990 got_object_commit_close(selected_commit);
3991 return NULL;
3994 static const struct got_error *
3995 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3997 const struct got_error *err = NULL;
3998 struct tog_diff_view_state *s = &view->state.diff;
3999 struct tog_log_view_state *ls;
4000 struct commit_queue_entry *old_selected_entry;
4001 char *line = NULL;
4002 size_t linesize = 0;
4003 ssize_t linelen;
4004 int i, nscroll = view->nlines - 1;
4006 switch (ch) {
4007 case '0':
4008 view->x = 0;
4009 break;
4010 case '$':
4011 view->x = MAX(view->maxx - view->ncols / 3, 0);
4012 break;
4013 case KEY_RIGHT:
4014 case 'l':
4015 if (view->x + view->ncols / 3 < view->maxx)
4016 view->x += 2; /* move two columns right */
4017 break;
4018 case KEY_LEFT:
4019 case 'h':
4020 view->x -= MIN(view->x, 2); /* move two columns back */
4021 break;
4022 case 'a':
4023 case 'w':
4024 if (ch == 'a')
4025 s->force_text_diff = !s->force_text_diff;
4026 if (ch == 'w')
4027 s->ignore_whitespace = !s->ignore_whitespace;
4028 wclear(view->window);
4029 s->first_displayed_line = 1;
4030 s->last_displayed_line = view->nlines;
4031 s->matched_line = 0;
4032 diff_view_indicate_progress(view);
4033 err = create_diff(s);
4034 break;
4035 case 'g':
4036 case KEY_HOME:
4037 s->first_displayed_line = 1;
4038 break;
4039 case 'G':
4040 case KEY_END:
4041 if (s->eof)
4042 break;
4044 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4045 s->eof = 1;
4046 break;
4047 case 'k':
4048 case KEY_UP:
4049 case CTRL('p'):
4050 if (s->first_displayed_line > 1)
4051 s->first_displayed_line--;
4052 break;
4053 case CTRL('u'):
4054 case 'u':
4055 nscroll /= 2;
4056 /* FALL THROUGH */
4057 case KEY_PPAGE:
4058 case CTRL('b'):
4059 if (s->first_displayed_line == 1)
4060 break;
4061 i = 0;
4062 while (i++ < nscroll && s->first_displayed_line > 1)
4063 s->first_displayed_line--;
4064 break;
4065 case 'j':
4066 case KEY_DOWN:
4067 case CTRL('n'):
4068 if (!s->eof)
4069 s->first_displayed_line++;
4070 break;
4071 case CTRL('d'):
4072 case 'd':
4073 nscroll /= 2;
4074 /* FALL THROUGH */
4075 case KEY_NPAGE:
4076 case CTRL('f'):
4077 case ' ':
4078 if (s->eof)
4079 break;
4080 i = 0;
4081 while (!s->eof && i++ < nscroll) {
4082 linelen = getline(&line, &linesize, s->f);
4083 s->first_displayed_line++;
4084 if (linelen == -1) {
4085 if (feof(s->f)) {
4086 s->eof = 1;
4087 } else
4088 err = got_ferror(s->f, GOT_ERR_IO);
4089 break;
4092 free(line);
4093 break;
4094 case '[':
4095 if (s->diff_context > 0) {
4096 s->diff_context--;
4097 s->matched_line = 0;
4098 diff_view_indicate_progress(view);
4099 err = create_diff(s);
4100 if (s->first_displayed_line + view->nlines - 1 >
4101 s->nlines) {
4102 s->first_displayed_line = 1;
4103 s->last_displayed_line = view->nlines;
4106 break;
4107 case ']':
4108 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4109 s->diff_context++;
4110 s->matched_line = 0;
4111 diff_view_indicate_progress(view);
4112 err = create_diff(s);
4114 break;
4115 case '<':
4116 case ',':
4117 if (s->log_view == NULL)
4118 break;
4119 ls = &s->log_view->state.log;
4120 old_selected_entry = ls->selected_entry;
4122 err = input_log_view(NULL, s->log_view, KEY_UP);
4123 if (err)
4124 break;
4126 if (old_selected_entry == ls->selected_entry)
4127 break;
4129 err = set_selected_commit(s, ls->selected_entry);
4130 if (err)
4131 break;
4133 s->first_displayed_line = 1;
4134 s->last_displayed_line = view->nlines;
4135 s->matched_line = 0;
4136 view->x = 0;
4138 diff_view_indicate_progress(view);
4139 err = create_diff(s);
4140 break;
4141 case '>':
4142 case '.':
4143 if (s->log_view == NULL)
4144 break;
4145 ls = &s->log_view->state.log;
4146 old_selected_entry = ls->selected_entry;
4148 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4149 if (err)
4150 break;
4152 if (old_selected_entry == ls->selected_entry)
4153 break;
4155 err = set_selected_commit(s, ls->selected_entry);
4156 if (err)
4157 break;
4159 s->first_displayed_line = 1;
4160 s->last_displayed_line = view->nlines;
4161 s->matched_line = 0;
4162 view->x = 0;
4164 diff_view_indicate_progress(view);
4165 err = create_diff(s);
4166 break;
4167 default:
4168 break;
4171 return err;
4174 static const struct got_error *
4175 cmd_diff(int argc, char *argv[])
4177 const struct got_error *error = NULL;
4178 struct got_repository *repo = NULL;
4179 struct got_worktree *worktree = NULL;
4180 struct got_object_id *id1 = NULL, *id2 = NULL;
4181 char *repo_path = NULL, *cwd = NULL;
4182 char *id_str1 = NULL, *id_str2 = NULL;
4183 char *label1 = NULL, *label2 = NULL;
4184 int diff_context = 3, ignore_whitespace = 0;
4185 int ch, force_text_diff = 0;
4186 const char *errstr;
4187 struct tog_view *view;
4188 int *pack_fds = NULL;
4190 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4191 switch (ch) {
4192 case 'a':
4193 force_text_diff = 1;
4194 break;
4195 case 'C':
4196 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4197 &errstr);
4198 if (errstr != NULL)
4199 errx(1, "number of context lines is %s: %s",
4200 errstr, errstr);
4201 break;
4202 case 'r':
4203 repo_path = realpath(optarg, NULL);
4204 if (repo_path == NULL)
4205 return got_error_from_errno2("realpath",
4206 optarg);
4207 got_path_strip_trailing_slashes(repo_path);
4208 break;
4209 case 'w':
4210 ignore_whitespace = 1;
4211 break;
4212 default:
4213 usage_diff();
4214 /* NOTREACHED */
4218 argc -= optind;
4219 argv += optind;
4221 if (argc == 0) {
4222 usage_diff(); /* TODO show local worktree changes */
4223 } else if (argc == 2) {
4224 id_str1 = argv[0];
4225 id_str2 = argv[1];
4226 } else
4227 usage_diff();
4229 error = got_repo_pack_fds_open(&pack_fds);
4230 if (error)
4231 goto done;
4233 if (repo_path == NULL) {
4234 cwd = getcwd(NULL, 0);
4235 if (cwd == NULL)
4236 return got_error_from_errno("getcwd");
4237 error = got_worktree_open(&worktree, cwd);
4238 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4239 goto done;
4240 if (worktree)
4241 repo_path =
4242 strdup(got_worktree_get_repo_path(worktree));
4243 else
4244 repo_path = strdup(cwd);
4245 if (repo_path == NULL) {
4246 error = got_error_from_errno("strdup");
4247 goto done;
4251 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4252 if (error)
4253 goto done;
4255 init_curses();
4257 error = apply_unveil(got_repo_get_path(repo), NULL);
4258 if (error)
4259 goto done;
4261 error = tog_load_refs(repo, 0);
4262 if (error)
4263 goto done;
4265 error = got_repo_match_object_id(&id1, &label1, id_str1,
4266 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4267 if (error)
4268 goto done;
4270 error = got_repo_match_object_id(&id2, &label2, id_str2,
4271 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4272 if (error)
4273 goto done;
4275 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4276 if (view == NULL) {
4277 error = got_error_from_errno("view_open");
4278 goto done;
4280 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4281 ignore_whitespace, force_text_diff, NULL, repo);
4282 if (error)
4283 goto done;
4284 error = view_loop(view);
4285 done:
4286 free(label1);
4287 free(label2);
4288 free(repo_path);
4289 free(cwd);
4290 if (repo) {
4291 const struct got_error *close_err = got_repo_close(repo);
4292 if (error == NULL)
4293 error = close_err;
4295 if (worktree)
4296 got_worktree_close(worktree);
4297 if (pack_fds) {
4298 const struct got_error *pack_err =
4299 got_repo_pack_fds_close(pack_fds);
4300 if (error == NULL)
4301 error = pack_err;
4303 tog_free_refs();
4304 return error;
4307 __dead static void
4308 usage_blame(void)
4310 endwin();
4311 fprintf(stderr,
4312 "usage: %s blame [-c commit] [-r repository-path] path\n",
4313 getprogname());
4314 exit(1);
4317 struct tog_blame_line {
4318 int annotated;
4319 struct got_object_id *id;
4322 static const struct got_error *
4323 draw_blame(struct tog_view *view)
4325 struct tog_blame_view_state *s = &view->state.blame;
4326 struct tog_blame *blame = &s->blame;
4327 regmatch_t *regmatch = &view->regmatch;
4328 const struct got_error *err;
4329 int lineno = 0, nprinted = 0;
4330 char *line = NULL;
4331 size_t linesize = 0;
4332 ssize_t linelen;
4333 wchar_t *wline;
4334 int width;
4335 struct tog_blame_line *blame_line;
4336 struct got_object_id *prev_id = NULL;
4337 char *id_str;
4338 struct tog_color *tc;
4340 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4341 if (err)
4342 return err;
4344 rewind(blame->f);
4345 werase(view->window);
4347 if (asprintf(&line, "commit %s", id_str) == -1) {
4348 err = got_error_from_errno("asprintf");
4349 free(id_str);
4350 return err;
4353 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4354 free(line);
4355 line = NULL;
4356 if (err)
4357 return err;
4358 if (view_needs_focus_indication(view))
4359 wstandout(view->window);
4360 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4361 if (tc)
4362 wattr_on(view->window,
4363 COLOR_PAIR(tc->colorpair), NULL);
4364 waddwstr(view->window, wline);
4365 if (tc)
4366 wattr_off(view->window,
4367 COLOR_PAIR(tc->colorpair), NULL);
4368 if (view_needs_focus_indication(view))
4369 wstandend(view->window);
4370 free(wline);
4371 wline = NULL;
4372 if (width < view->ncols - 1)
4373 waddch(view->window, '\n');
4375 if (asprintf(&line, "[%d/%d] %s%s",
4376 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4377 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4378 free(id_str);
4379 return got_error_from_errno("asprintf");
4381 free(id_str);
4382 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4383 free(line);
4384 line = NULL;
4385 if (err)
4386 return err;
4387 waddwstr(view->window, wline);
4388 free(wline);
4389 wline = NULL;
4390 if (width < view->ncols - 1)
4391 waddch(view->window, '\n');
4393 s->eof = 0;
4394 view->maxx = 0;
4395 while (nprinted < view->nlines - 2) {
4396 linelen = getline(&line, &linesize, blame->f);
4397 if (linelen == -1) {
4398 if (feof(blame->f)) {
4399 s->eof = 1;
4400 break;
4402 free(line);
4403 return got_ferror(blame->f, GOT_ERR_IO);
4405 if (++lineno < s->first_displayed_line)
4406 continue;
4408 /* Set view->maxx based on full line length. */
4409 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4410 if (err) {
4411 free(line);
4412 return err;
4414 free(wline);
4415 wline = NULL;
4416 view->maxx = MAX(view->maxx, width);
4418 if (view->focussed && nprinted == s->selected_line - 1)
4419 wstandout(view->window);
4421 if (blame->nlines > 0) {
4422 blame_line = &blame->lines[lineno - 1];
4423 if (blame_line->annotated && prev_id &&
4424 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4425 !(view->focussed &&
4426 nprinted == s->selected_line - 1)) {
4427 waddstr(view->window, " ");
4428 } else if (blame_line->annotated) {
4429 char *id_str;
4430 err = got_object_id_str(&id_str,
4431 blame_line->id);
4432 if (err) {
4433 free(line);
4434 return err;
4436 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4437 if (tc)
4438 wattr_on(view->window,
4439 COLOR_PAIR(tc->colorpair), NULL);
4440 wprintw(view->window, "%.8s", id_str);
4441 if (tc)
4442 wattr_off(view->window,
4443 COLOR_PAIR(tc->colorpair), NULL);
4444 free(id_str);
4445 prev_id = blame_line->id;
4446 } else {
4447 waddstr(view->window, "........");
4448 prev_id = NULL;
4450 } else {
4451 waddstr(view->window, "........");
4452 prev_id = NULL;
4455 if (view->focussed && nprinted == s->selected_line - 1)
4456 wstandend(view->window);
4457 waddstr(view->window, " ");
4459 if (view->ncols <= 9) {
4460 width = 9;
4461 } else if (s->first_displayed_line + nprinted ==
4462 s->matched_line &&
4463 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4464 err = add_matched_line(&width, line, view->ncols - 9, 9,
4465 view->window, view->x, regmatch);
4466 if (err) {
4467 free(line);
4468 return err;
4470 width += 9;
4471 } else {
4472 int skip;
4473 err = format_line(&wline, &width, &skip, line,
4474 view->x, view->ncols - 9, 9, 1);
4475 if (err) {
4476 free(line);
4477 return err;
4479 waddwstr(view->window, &wline[skip]);
4480 width += 9;
4481 free(wline);
4482 wline = NULL;
4485 if (width <= view->ncols - 1)
4486 waddch(view->window, '\n');
4487 if (++nprinted == 1)
4488 s->first_displayed_line = lineno;
4490 free(line);
4491 s->last_displayed_line = lineno;
4493 view_vborder(view);
4495 return NULL;
4498 static const struct got_error *
4499 blame_cb(void *arg, int nlines, int lineno,
4500 struct got_commit_object *commit, struct got_object_id *id)
4502 const struct got_error *err = NULL;
4503 struct tog_blame_cb_args *a = arg;
4504 struct tog_blame_line *line;
4505 int errcode;
4507 if (nlines != a->nlines ||
4508 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4509 return got_error(GOT_ERR_RANGE);
4511 errcode = pthread_mutex_lock(&tog_mutex);
4512 if (errcode)
4513 return got_error_set_errno(errcode, "pthread_mutex_lock");
4515 if (*a->quit) { /* user has quit the blame view */
4516 err = got_error(GOT_ERR_ITER_COMPLETED);
4517 goto done;
4520 if (lineno == -1)
4521 goto done; /* no change in this commit */
4523 line = &a->lines[lineno - 1];
4524 if (line->annotated)
4525 goto done;
4527 line->id = got_object_id_dup(id);
4528 if (line->id == NULL) {
4529 err = got_error_from_errno("got_object_id_dup");
4530 goto done;
4532 line->annotated = 1;
4533 done:
4534 errcode = pthread_mutex_unlock(&tog_mutex);
4535 if (errcode)
4536 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4537 return err;
4540 static void *
4541 blame_thread(void *arg)
4543 const struct got_error *err, *close_err;
4544 struct tog_blame_thread_args *ta = arg;
4545 struct tog_blame_cb_args *a = ta->cb_args;
4546 int errcode;
4548 err = block_signals_used_by_main_thread();
4549 if (err)
4550 return (void *)err;
4552 err = got_blame(ta->path, a->commit_id, ta->repo,
4553 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4554 if (err && err->code == GOT_ERR_CANCELLED)
4555 err = NULL;
4557 errcode = pthread_mutex_lock(&tog_mutex);
4558 if (errcode)
4559 return (void *)got_error_set_errno(errcode,
4560 "pthread_mutex_lock");
4562 close_err = got_repo_close(ta->repo);
4563 if (err == NULL)
4564 err = close_err;
4565 ta->repo = NULL;
4566 *ta->complete = 1;
4568 errcode = pthread_mutex_unlock(&tog_mutex);
4569 if (errcode && err == NULL)
4570 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4572 return (void *)err;
4575 static struct got_object_id *
4576 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4577 int first_displayed_line, int selected_line)
4579 struct tog_blame_line *line;
4581 if (nlines <= 0)
4582 return NULL;
4584 line = &lines[first_displayed_line - 1 + selected_line - 1];
4585 if (!line->annotated)
4586 return NULL;
4588 return line->id;
4591 static const struct got_error *
4592 stop_blame(struct tog_blame *blame)
4594 const struct got_error *err = NULL;
4595 int i;
4597 if (blame->thread) {
4598 int errcode;
4599 errcode = pthread_mutex_unlock(&tog_mutex);
4600 if (errcode)
4601 return got_error_set_errno(errcode,
4602 "pthread_mutex_unlock");
4603 errcode = pthread_join(blame->thread, (void **)&err);
4604 if (errcode)
4605 return got_error_set_errno(errcode, "pthread_join");
4606 errcode = pthread_mutex_lock(&tog_mutex);
4607 if (errcode)
4608 return got_error_set_errno(errcode,
4609 "pthread_mutex_lock");
4610 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4611 err = NULL;
4612 blame->thread = NULL;
4614 if (blame->thread_args.repo) {
4615 const struct got_error *close_err;
4616 close_err = got_repo_close(blame->thread_args.repo);
4617 if (err == NULL)
4618 err = close_err;
4619 blame->thread_args.repo = NULL;
4621 if (blame->f) {
4622 if (fclose(blame->f) == EOF && err == NULL)
4623 err = got_error_from_errno("fclose");
4624 blame->f = NULL;
4626 if (blame->lines) {
4627 for (i = 0; i < blame->nlines; i++)
4628 free(blame->lines[i].id);
4629 free(blame->lines);
4630 blame->lines = NULL;
4632 free(blame->cb_args.commit_id);
4633 blame->cb_args.commit_id = NULL;
4634 if (blame->pack_fds) {
4635 const struct got_error *pack_err =
4636 got_repo_pack_fds_close(blame->pack_fds);
4637 if (err == NULL)
4638 err = pack_err;
4639 blame->pack_fds = NULL;
4641 return err;
4644 static const struct got_error *
4645 cancel_blame_view(void *arg)
4647 const struct got_error *err = NULL;
4648 int *done = arg;
4649 int errcode;
4651 errcode = pthread_mutex_lock(&tog_mutex);
4652 if (errcode)
4653 return got_error_set_errno(errcode,
4654 "pthread_mutex_unlock");
4656 if (*done)
4657 err = got_error(GOT_ERR_CANCELLED);
4659 errcode = pthread_mutex_unlock(&tog_mutex);
4660 if (errcode)
4661 return got_error_set_errno(errcode,
4662 "pthread_mutex_lock");
4664 return err;
4667 static const struct got_error *
4668 run_blame(struct tog_view *view)
4670 struct tog_blame_view_state *s = &view->state.blame;
4671 struct tog_blame *blame = &s->blame;
4672 const struct got_error *err = NULL;
4673 struct got_commit_object *commit = NULL;
4674 struct got_blob_object *blob = NULL;
4675 struct got_repository *thread_repo = NULL;
4676 struct got_object_id *obj_id = NULL;
4677 int obj_type;
4678 int *pack_fds = NULL;
4680 err = got_object_open_as_commit(&commit, s->repo,
4681 &s->blamed_commit->id);
4682 if (err)
4683 return err;
4685 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4686 if (err)
4687 goto done;
4689 err = got_object_get_type(&obj_type, s->repo, obj_id);
4690 if (err)
4691 goto done;
4693 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4694 err = got_error(GOT_ERR_OBJ_TYPE);
4695 goto done;
4698 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4699 if (err)
4700 goto done;
4701 blame->f = got_opentemp();
4702 if (blame->f == NULL) {
4703 err = got_error_from_errno("got_opentemp");
4704 goto done;
4706 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4707 &blame->line_offsets, blame->f, blob);
4708 if (err)
4709 goto done;
4710 if (blame->nlines == 0) {
4711 s->blame_complete = 1;
4712 goto done;
4715 /* Don't include \n at EOF in the blame line count. */
4716 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4717 blame->nlines--;
4719 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4720 if (blame->lines == NULL) {
4721 err = got_error_from_errno("calloc");
4722 goto done;
4725 err = got_repo_pack_fds_open(&pack_fds);
4726 if (err)
4727 goto done;
4728 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4729 pack_fds);
4730 if (err)
4731 goto done;
4733 blame->pack_fds = pack_fds;
4734 blame->cb_args.view = view;
4735 blame->cb_args.lines = blame->lines;
4736 blame->cb_args.nlines = blame->nlines;
4737 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4738 if (blame->cb_args.commit_id == NULL) {
4739 err = got_error_from_errno("got_object_id_dup");
4740 goto done;
4742 blame->cb_args.quit = &s->done;
4744 blame->thread_args.path = s->path;
4745 blame->thread_args.repo = thread_repo;
4746 blame->thread_args.cb_args = &blame->cb_args;
4747 blame->thread_args.complete = &s->blame_complete;
4748 blame->thread_args.cancel_cb = cancel_blame_view;
4749 blame->thread_args.cancel_arg = &s->done;
4750 s->blame_complete = 0;
4752 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4753 s->first_displayed_line = 1;
4754 s->last_displayed_line = view->nlines;
4755 s->selected_line = 1;
4757 s->matched_line = 0;
4759 done:
4760 if (commit)
4761 got_object_commit_close(commit);
4762 if (blob)
4763 got_object_blob_close(blob);
4764 free(obj_id);
4765 if (err)
4766 stop_blame(blame);
4767 return err;
4770 static const struct got_error *
4771 open_blame_view(struct tog_view *view, char *path,
4772 struct got_object_id *commit_id, struct got_repository *repo)
4774 const struct got_error *err = NULL;
4775 struct tog_blame_view_state *s = &view->state.blame;
4777 STAILQ_INIT(&s->blamed_commits);
4779 s->path = strdup(path);
4780 if (s->path == NULL)
4781 return got_error_from_errno("strdup");
4783 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4784 if (err) {
4785 free(s->path);
4786 return err;
4789 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4790 s->first_displayed_line = 1;
4791 s->last_displayed_line = view->nlines;
4792 s->selected_line = 1;
4793 s->blame_complete = 0;
4794 s->repo = repo;
4795 s->commit_id = commit_id;
4796 memset(&s->blame, 0, sizeof(s->blame));
4798 STAILQ_INIT(&s->colors);
4799 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4800 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4801 get_color_value("TOG_COLOR_COMMIT"));
4802 if (err)
4803 return err;
4806 view->show = show_blame_view;
4807 view->input = input_blame_view;
4808 view->close = close_blame_view;
4809 view->search_start = search_start_blame_view;
4810 view->search_next = search_next_blame_view;
4812 return run_blame(view);
4815 static const struct got_error *
4816 close_blame_view(struct tog_view *view)
4818 const struct got_error *err = NULL;
4819 struct tog_blame_view_state *s = &view->state.blame;
4821 if (s->blame.thread)
4822 err = stop_blame(&s->blame);
4824 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4825 struct got_object_qid *blamed_commit;
4826 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4827 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4828 got_object_qid_free(blamed_commit);
4831 free(s->path);
4832 free_colors(&s->colors);
4833 return err;
4836 static const struct got_error *
4837 search_start_blame_view(struct tog_view *view)
4839 struct tog_blame_view_state *s = &view->state.blame;
4841 s->matched_line = 0;
4842 return NULL;
4845 static const struct got_error *
4846 search_next_blame_view(struct tog_view *view)
4848 struct tog_blame_view_state *s = &view->state.blame;
4849 const struct got_error *err = NULL;
4850 int lineno;
4851 char *line = NULL;
4852 size_t linesize = 0;
4853 ssize_t linelen;
4855 if (!view->searching) {
4856 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4857 return NULL;
4860 if (s->matched_line) {
4861 if (view->searching == TOG_SEARCH_FORWARD)
4862 lineno = s->matched_line + 1;
4863 else
4864 lineno = s->matched_line - 1;
4865 } else
4866 lineno = s->first_displayed_line - 1 + s->selected_line;
4868 while (1) {
4869 off_t offset;
4871 if (lineno <= 0 || lineno > s->blame.nlines) {
4872 if (s->matched_line == 0) {
4873 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4874 break;
4877 if (view->searching == TOG_SEARCH_FORWARD)
4878 lineno = 1;
4879 else
4880 lineno = s->blame.nlines;
4883 offset = s->blame.line_offsets[lineno - 1];
4884 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4885 free(line);
4886 return got_error_from_errno("fseeko");
4888 linelen = getline(&line, &linesize, s->blame.f);
4889 if (linelen != -1) {
4890 char *exstr;
4891 err = expand_tab(&exstr, line);
4892 if (err)
4893 break;
4894 if (match_line(exstr, &view->regex, 1,
4895 &view->regmatch)) {
4896 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4897 s->matched_line = lineno;
4898 free(exstr);
4899 break;
4901 free(exstr);
4903 if (view->searching == TOG_SEARCH_FORWARD)
4904 lineno++;
4905 else
4906 lineno--;
4908 free(line);
4910 if (s->matched_line) {
4911 s->first_displayed_line = s->matched_line;
4912 s->selected_line = 1;
4915 return err;
4918 static const struct got_error *
4919 show_blame_view(struct tog_view *view)
4921 const struct got_error *err = NULL;
4922 struct tog_blame_view_state *s = &view->state.blame;
4923 int errcode;
4925 if (s->blame.thread == NULL && !s->blame_complete) {
4926 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4927 &s->blame.thread_args);
4928 if (errcode)
4929 return got_error_set_errno(errcode, "pthread_create");
4931 halfdelay(1); /* fast refresh while annotating */
4934 if (s->blame_complete)
4935 halfdelay(10); /* disable fast refresh */
4937 err = draw_blame(view);
4939 view_vborder(view);
4940 return err;
4943 static const struct got_error *
4944 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4946 const struct got_error *err = NULL, *thread_err = NULL;
4947 struct tog_view *diff_view;
4948 struct tog_blame_view_state *s = &view->state.blame;
4949 int begin_x = 0, nscroll = view->nlines - 2;
4951 switch (ch) {
4952 case '0':
4953 view->x = 0;
4954 break;
4955 case '$':
4956 view->x = MAX(view->maxx - view->ncols / 3, 0);
4957 break;
4958 case KEY_RIGHT:
4959 case 'l':
4960 if (view->x + view->ncols / 3 < view->maxx)
4961 view->x += 2; /* move two columns right */
4962 break;
4963 case KEY_LEFT:
4964 case 'h':
4965 view->x -= MIN(view->x, 2); /* move two columns back */
4966 break;
4967 case 'q':
4968 s->done = 1;
4969 break;
4970 case 'g':
4971 case KEY_HOME:
4972 s->selected_line = 1;
4973 s->first_displayed_line = 1;
4974 break;
4975 case 'G':
4976 case KEY_END:
4977 if (s->blame.nlines < view->nlines - 2) {
4978 s->selected_line = s->blame.nlines;
4979 s->first_displayed_line = 1;
4980 } else {
4981 s->selected_line = view->nlines - 2;
4982 s->first_displayed_line = s->blame.nlines -
4983 (view->nlines - 3);
4985 break;
4986 case 'k':
4987 case KEY_UP:
4988 case CTRL('p'):
4989 if (s->selected_line > 1)
4990 s->selected_line--;
4991 else if (s->selected_line == 1 &&
4992 s->first_displayed_line > 1)
4993 s->first_displayed_line--;
4994 break;
4995 case CTRL('u'):
4996 case 'u':
4997 nscroll /= 2;
4998 /* FALL THROUGH */
4999 case KEY_PPAGE:
5000 case CTRL('b'):
5001 if (s->first_displayed_line == 1) {
5002 s->selected_line = MAX(1, s->selected_line - nscroll);
5003 break;
5005 if (s->first_displayed_line > nscroll)
5006 s->first_displayed_line -= nscroll;
5007 else
5008 s->first_displayed_line = 1;
5009 break;
5010 case 'j':
5011 case KEY_DOWN:
5012 case CTRL('n'):
5013 if (s->selected_line < view->nlines - 2 &&
5014 s->first_displayed_line +
5015 s->selected_line <= s->blame.nlines)
5016 s->selected_line++;
5017 else if (s->last_displayed_line <
5018 s->blame.nlines)
5019 s->first_displayed_line++;
5020 break;
5021 case 'b':
5022 case 'p': {
5023 struct got_object_id *id = NULL;
5024 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5025 s->first_displayed_line, s->selected_line);
5026 if (id == NULL)
5027 break;
5028 if (ch == 'p') {
5029 struct got_commit_object *commit, *pcommit;
5030 struct got_object_qid *pid;
5031 struct got_object_id *blob_id = NULL;
5032 int obj_type;
5033 err = got_object_open_as_commit(&commit,
5034 s->repo, id);
5035 if (err)
5036 break;
5037 pid = STAILQ_FIRST(
5038 got_object_commit_get_parent_ids(commit));
5039 if (pid == NULL) {
5040 got_object_commit_close(commit);
5041 break;
5043 /* Check if path history ends here. */
5044 err = got_object_open_as_commit(&pcommit,
5045 s->repo, &pid->id);
5046 if (err)
5047 break;
5048 err = got_object_id_by_path(&blob_id, s->repo,
5049 pcommit, s->path);
5050 got_object_commit_close(pcommit);
5051 if (err) {
5052 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5053 err = NULL;
5054 got_object_commit_close(commit);
5055 break;
5057 err = got_object_get_type(&obj_type, s->repo,
5058 blob_id);
5059 free(blob_id);
5060 /* Can't blame non-blob type objects. */
5061 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5062 got_object_commit_close(commit);
5063 break;
5065 err = got_object_qid_alloc(&s->blamed_commit,
5066 &pid->id);
5067 got_object_commit_close(commit);
5068 } else {
5069 if (got_object_id_cmp(id,
5070 &s->blamed_commit->id) == 0)
5071 break;
5072 err = got_object_qid_alloc(&s->blamed_commit,
5073 id);
5075 if (err)
5076 break;
5077 s->done = 1;
5078 thread_err = stop_blame(&s->blame);
5079 s->done = 0;
5080 if (thread_err)
5081 break;
5082 STAILQ_INSERT_HEAD(&s->blamed_commits,
5083 s->blamed_commit, entry);
5084 err = run_blame(view);
5085 if (err)
5086 break;
5087 break;
5089 case 'B': {
5090 struct got_object_qid *first;
5091 first = STAILQ_FIRST(&s->blamed_commits);
5092 if (!got_object_id_cmp(&first->id, s->commit_id))
5093 break;
5094 s->done = 1;
5095 thread_err = stop_blame(&s->blame);
5096 s->done = 0;
5097 if (thread_err)
5098 break;
5099 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5100 got_object_qid_free(s->blamed_commit);
5101 s->blamed_commit =
5102 STAILQ_FIRST(&s->blamed_commits);
5103 err = run_blame(view);
5104 if (err)
5105 break;
5106 break;
5108 case KEY_ENTER:
5109 case '\r': {
5110 struct got_object_id *id = NULL;
5111 struct got_object_qid *pid;
5112 struct got_commit_object *commit = NULL;
5113 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5114 s->first_displayed_line, s->selected_line);
5115 if (id == NULL)
5116 break;
5117 err = got_object_open_as_commit(&commit, s->repo, id);
5118 if (err)
5119 break;
5120 pid = STAILQ_FIRST(
5121 got_object_commit_get_parent_ids(commit));
5122 if (view_is_parent_view(view))
5123 begin_x = view_split_begin_x(view->begin_x);
5124 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5125 if (diff_view == NULL) {
5126 got_object_commit_close(commit);
5127 err = got_error_from_errno("view_open");
5128 break;
5130 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5131 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5132 got_object_commit_close(commit);
5133 if (err) {
5134 view_close(diff_view);
5135 break;
5137 view->focussed = 0;
5138 diff_view->focussed = 1;
5139 if (view_is_parent_view(view)) {
5140 err = view_close_child(view);
5141 if (err)
5142 break;
5143 err = view_set_child(view, diff_view);
5144 if (err)
5145 break;
5146 view->focus_child = 1;
5147 } else
5148 *new_view = diff_view;
5149 if (err)
5150 break;
5151 break;
5153 case CTRL('d'):
5154 case 'd':
5155 nscroll /= 2;
5156 /* FALL THROUGH */
5157 case KEY_NPAGE:
5158 case CTRL('f'):
5159 case ' ':
5160 if (s->last_displayed_line >= s->blame.nlines &&
5161 s->selected_line >= MIN(s->blame.nlines,
5162 view->nlines - 2)) {
5163 break;
5165 if (s->last_displayed_line >= s->blame.nlines &&
5166 s->selected_line < view->nlines - 2) {
5167 s->selected_line +=
5168 MIN(nscroll, s->last_displayed_line -
5169 s->first_displayed_line - s->selected_line + 1);
5171 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5172 s->first_displayed_line += nscroll;
5173 else
5174 s->first_displayed_line =
5175 s->blame.nlines - (view->nlines - 3);
5176 break;
5177 case KEY_RESIZE:
5178 if (s->selected_line > view->nlines - 2) {
5179 s->selected_line = MIN(s->blame.nlines,
5180 view->nlines - 2);
5182 break;
5183 default:
5184 break;
5186 return thread_err ? thread_err : err;
5189 static const struct got_error *
5190 cmd_blame(int argc, char *argv[])
5192 const struct got_error *error;
5193 struct got_repository *repo = NULL;
5194 struct got_worktree *worktree = NULL;
5195 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5196 char *link_target = NULL;
5197 struct got_object_id *commit_id = NULL;
5198 struct got_commit_object *commit = NULL;
5199 char *commit_id_str = NULL;
5200 int ch;
5201 struct tog_view *view;
5202 int *pack_fds = NULL;
5204 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5205 switch (ch) {
5206 case 'c':
5207 commit_id_str = optarg;
5208 break;
5209 case 'r':
5210 repo_path = realpath(optarg, NULL);
5211 if (repo_path == NULL)
5212 return got_error_from_errno2("realpath",
5213 optarg);
5214 break;
5215 default:
5216 usage_blame();
5217 /* NOTREACHED */
5221 argc -= optind;
5222 argv += optind;
5224 if (argc != 1)
5225 usage_blame();
5227 error = got_repo_pack_fds_open(&pack_fds);
5228 if (error != NULL)
5229 goto done;
5231 if (repo_path == NULL) {
5232 cwd = getcwd(NULL, 0);
5233 if (cwd == NULL)
5234 return got_error_from_errno("getcwd");
5235 error = got_worktree_open(&worktree, cwd);
5236 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5237 goto done;
5238 if (worktree)
5239 repo_path =
5240 strdup(got_worktree_get_repo_path(worktree));
5241 else
5242 repo_path = strdup(cwd);
5243 if (repo_path == NULL) {
5244 error = got_error_from_errno("strdup");
5245 goto done;
5249 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5250 if (error != NULL)
5251 goto done;
5253 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5254 worktree);
5255 if (error)
5256 goto done;
5258 init_curses();
5260 error = apply_unveil(got_repo_get_path(repo), NULL);
5261 if (error)
5262 goto done;
5264 error = tog_load_refs(repo, 0);
5265 if (error)
5266 goto done;
5268 if (commit_id_str == NULL) {
5269 struct got_reference *head_ref;
5270 error = got_ref_open(&head_ref, repo, worktree ?
5271 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5272 if (error != NULL)
5273 goto done;
5274 error = got_ref_resolve(&commit_id, repo, head_ref);
5275 got_ref_close(head_ref);
5276 } else {
5277 error = got_repo_match_object_id(&commit_id, NULL,
5278 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5280 if (error != NULL)
5281 goto done;
5283 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5284 if (view == NULL) {
5285 error = got_error_from_errno("view_open");
5286 goto done;
5289 error = got_object_open_as_commit(&commit, repo, commit_id);
5290 if (error)
5291 goto done;
5293 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5294 commit, repo);
5295 if (error)
5296 goto done;
5298 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5299 commit_id, repo);
5300 if (error)
5301 goto done;
5302 if (worktree) {
5303 /* Release work tree lock. */
5304 got_worktree_close(worktree);
5305 worktree = NULL;
5307 error = view_loop(view);
5308 done:
5309 free(repo_path);
5310 free(in_repo_path);
5311 free(link_target);
5312 free(cwd);
5313 free(commit_id);
5314 if (commit)
5315 got_object_commit_close(commit);
5316 if (worktree)
5317 got_worktree_close(worktree);
5318 if (repo) {
5319 const struct got_error *close_err = got_repo_close(repo);
5320 if (error == NULL)
5321 error = close_err;
5323 if (pack_fds) {
5324 const struct got_error *pack_err =
5325 got_repo_pack_fds_close(pack_fds);
5326 if (error == NULL)
5327 error = pack_err;
5329 tog_free_refs();
5330 return error;
5333 static const struct got_error *
5334 draw_tree_entries(struct tog_view *view, const char *parent_path)
5336 struct tog_tree_view_state *s = &view->state.tree;
5337 const struct got_error *err = NULL;
5338 struct got_tree_entry *te;
5339 wchar_t *wline;
5340 struct tog_color *tc;
5341 int width, n, i, nentries;
5342 int limit = view->nlines;
5344 s->ndisplayed = 0;
5346 werase(view->window);
5348 if (limit == 0)
5349 return NULL;
5351 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5352 0, 0);
5353 if (err)
5354 return err;
5355 if (view_needs_focus_indication(view))
5356 wstandout(view->window);
5357 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5358 if (tc)
5359 wattr_on(view->window,
5360 COLOR_PAIR(tc->colorpair), NULL);
5361 waddwstr(view->window, wline);
5362 if (tc)
5363 wattr_off(view->window,
5364 COLOR_PAIR(tc->colorpair), NULL);
5365 if (view_needs_focus_indication(view))
5366 wstandend(view->window);
5367 free(wline);
5368 wline = NULL;
5369 if (width < view->ncols - 1)
5370 waddch(view->window, '\n');
5371 if (--limit <= 0)
5372 return NULL;
5373 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5374 0, 0);
5375 if (err)
5376 return err;
5377 waddwstr(view->window, wline);
5378 free(wline);
5379 wline = NULL;
5380 if (width < view->ncols - 1)
5381 waddch(view->window, '\n');
5382 if (--limit <= 0)
5383 return NULL;
5384 waddch(view->window, '\n');
5385 if (--limit <= 0)
5386 return NULL;
5388 if (s->first_displayed_entry == NULL) {
5389 te = got_object_tree_get_first_entry(s->tree);
5390 if (s->selected == 0) {
5391 if (view->focussed)
5392 wstandout(view->window);
5393 s->selected_entry = NULL;
5395 waddstr(view->window, " ..\n"); /* parent directory */
5396 if (s->selected == 0 && view->focussed)
5397 wstandend(view->window);
5398 s->ndisplayed++;
5399 if (--limit <= 0)
5400 return NULL;
5401 n = 1;
5402 } else {
5403 n = 0;
5404 te = s->first_displayed_entry;
5407 nentries = got_object_tree_get_nentries(s->tree);
5408 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5409 char *line = NULL, *id_str = NULL, *link_target = NULL;
5410 const char *modestr = "";
5411 mode_t mode;
5413 te = got_object_tree_get_entry(s->tree, i);
5414 mode = got_tree_entry_get_mode(te);
5416 if (s->show_ids) {
5417 err = got_object_id_str(&id_str,
5418 got_tree_entry_get_id(te));
5419 if (err)
5420 return got_error_from_errno(
5421 "got_object_id_str");
5423 if (got_object_tree_entry_is_submodule(te))
5424 modestr = "$";
5425 else if (S_ISLNK(mode)) {
5426 int i;
5428 err = got_tree_entry_get_symlink_target(&link_target,
5429 te, s->repo);
5430 if (err) {
5431 free(id_str);
5432 return err;
5434 for (i = 0; i < strlen(link_target); i++) {
5435 if (!isprint((unsigned char)link_target[i]))
5436 link_target[i] = '?';
5438 modestr = "@";
5440 else if (S_ISDIR(mode))
5441 modestr = "/";
5442 else if (mode & S_IXUSR)
5443 modestr = "*";
5444 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5445 got_tree_entry_get_name(te), modestr,
5446 link_target ? " -> ": "",
5447 link_target ? link_target : "") == -1) {
5448 free(id_str);
5449 free(link_target);
5450 return got_error_from_errno("asprintf");
5452 free(id_str);
5453 free(link_target);
5454 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5455 0, 0);
5456 if (err) {
5457 free(line);
5458 break;
5460 if (n == s->selected) {
5461 if (view->focussed)
5462 wstandout(view->window);
5463 s->selected_entry = te;
5465 tc = match_color(&s->colors, line);
5466 if (tc)
5467 wattr_on(view->window,
5468 COLOR_PAIR(tc->colorpair), NULL);
5469 waddwstr(view->window, wline);
5470 if (tc)
5471 wattr_off(view->window,
5472 COLOR_PAIR(tc->colorpair), NULL);
5473 if (width < view->ncols - 1)
5474 waddch(view->window, '\n');
5475 if (n == s->selected && view->focussed)
5476 wstandend(view->window);
5477 free(line);
5478 free(wline);
5479 wline = NULL;
5480 n++;
5481 s->ndisplayed++;
5482 s->last_displayed_entry = te;
5483 if (--limit <= 0)
5484 break;
5487 return err;
5490 static void
5491 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5493 struct got_tree_entry *te;
5494 int isroot = s->tree == s->root;
5495 int i = 0;
5497 if (s->first_displayed_entry == NULL)
5498 return;
5500 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5501 while (i++ < maxscroll) {
5502 if (te == NULL) {
5503 if (!isroot)
5504 s->first_displayed_entry = NULL;
5505 break;
5507 s->first_displayed_entry = te;
5508 te = got_tree_entry_get_prev(s->tree, te);
5512 static void
5513 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5515 struct got_tree_entry *next, *last;
5516 int n = 0;
5518 if (s->first_displayed_entry)
5519 next = got_tree_entry_get_next(s->tree,
5520 s->first_displayed_entry);
5521 else
5522 next = got_object_tree_get_first_entry(s->tree);
5524 last = s->last_displayed_entry;
5525 while (next && last && n++ < maxscroll) {
5526 last = got_tree_entry_get_next(s->tree, last);
5527 if (last) {
5528 s->first_displayed_entry = next;
5529 next = got_tree_entry_get_next(s->tree, next);
5534 static const struct got_error *
5535 tree_entry_path(char **path, struct tog_parent_trees *parents,
5536 struct got_tree_entry *te)
5538 const struct got_error *err = NULL;
5539 struct tog_parent_tree *pt;
5540 size_t len = 2; /* for leading slash and NUL */
5542 TAILQ_FOREACH(pt, parents, entry)
5543 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5544 + 1 /* slash */;
5545 if (te)
5546 len += strlen(got_tree_entry_get_name(te));
5548 *path = calloc(1, len);
5549 if (path == NULL)
5550 return got_error_from_errno("calloc");
5552 (*path)[0] = '/';
5553 pt = TAILQ_LAST(parents, tog_parent_trees);
5554 while (pt) {
5555 const char *name = got_tree_entry_get_name(pt->selected_entry);
5556 if (strlcat(*path, name, len) >= len) {
5557 err = got_error(GOT_ERR_NO_SPACE);
5558 goto done;
5560 if (strlcat(*path, "/", len) >= len) {
5561 err = got_error(GOT_ERR_NO_SPACE);
5562 goto done;
5564 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5566 if (te) {
5567 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5568 err = got_error(GOT_ERR_NO_SPACE);
5569 goto done;
5572 done:
5573 if (err) {
5574 free(*path);
5575 *path = NULL;
5577 return err;
5580 static const struct got_error *
5581 blame_tree_entry(struct tog_view **new_view, int begin_x,
5582 struct got_tree_entry *te, struct tog_parent_trees *parents,
5583 struct got_object_id *commit_id, struct got_repository *repo)
5585 const struct got_error *err = NULL;
5586 char *path;
5587 struct tog_view *blame_view;
5589 *new_view = NULL;
5591 err = tree_entry_path(&path, parents, te);
5592 if (err)
5593 return err;
5595 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5596 if (blame_view == NULL) {
5597 err = got_error_from_errno("view_open");
5598 goto done;
5601 err = open_blame_view(blame_view, path, commit_id, repo);
5602 if (err) {
5603 if (err->code == GOT_ERR_CANCELLED)
5604 err = NULL;
5605 view_close(blame_view);
5606 } else
5607 *new_view = blame_view;
5608 done:
5609 free(path);
5610 return err;
5613 static const struct got_error *
5614 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5615 struct tog_tree_view_state *s)
5617 struct tog_view *log_view;
5618 const struct got_error *err = NULL;
5619 char *path;
5621 *new_view = NULL;
5623 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5624 if (log_view == NULL)
5625 return got_error_from_errno("view_open");
5627 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5628 if (err)
5629 return err;
5631 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5632 path, 0);
5633 if (err)
5634 view_close(log_view);
5635 else
5636 *new_view = log_view;
5637 free(path);
5638 return err;
5641 static const struct got_error *
5642 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5643 const char *head_ref_name, struct got_repository *repo)
5645 const struct got_error *err = NULL;
5646 char *commit_id_str = NULL;
5647 struct tog_tree_view_state *s = &view->state.tree;
5648 struct got_commit_object *commit = NULL;
5650 TAILQ_INIT(&s->parents);
5651 STAILQ_INIT(&s->colors);
5653 s->commit_id = got_object_id_dup(commit_id);
5654 if (s->commit_id == NULL)
5655 return got_error_from_errno("got_object_id_dup");
5657 err = got_object_open_as_commit(&commit, repo, commit_id);
5658 if (err)
5659 goto done;
5662 * The root is opened here and will be closed when the view is closed.
5663 * Any visited subtrees and their path-wise parents are opened and
5664 * closed on demand.
5666 err = got_object_open_as_tree(&s->root, repo,
5667 got_object_commit_get_tree_id(commit));
5668 if (err)
5669 goto done;
5670 s->tree = s->root;
5672 err = got_object_id_str(&commit_id_str, commit_id);
5673 if (err != NULL)
5674 goto done;
5676 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5677 err = got_error_from_errno("asprintf");
5678 goto done;
5681 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5682 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5683 if (head_ref_name) {
5684 s->head_ref_name = strdup(head_ref_name);
5685 if (s->head_ref_name == NULL) {
5686 err = got_error_from_errno("strdup");
5687 goto done;
5690 s->repo = repo;
5692 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5693 err = add_color(&s->colors, "\\$$",
5694 TOG_COLOR_TREE_SUBMODULE,
5695 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5696 if (err)
5697 goto done;
5698 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5699 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5700 if (err)
5701 goto done;
5702 err = add_color(&s->colors, "/$",
5703 TOG_COLOR_TREE_DIRECTORY,
5704 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5705 if (err)
5706 goto done;
5708 err = add_color(&s->colors, "\\*$",
5709 TOG_COLOR_TREE_EXECUTABLE,
5710 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5711 if (err)
5712 goto done;
5714 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5715 get_color_value("TOG_COLOR_COMMIT"));
5716 if (err)
5717 goto done;
5720 view->show = show_tree_view;
5721 view->input = input_tree_view;
5722 view->close = close_tree_view;
5723 view->search_start = search_start_tree_view;
5724 view->search_next = search_next_tree_view;
5725 done:
5726 free(commit_id_str);
5727 if (commit)
5728 got_object_commit_close(commit);
5729 if (err)
5730 close_tree_view(view);
5731 return err;
5734 static const struct got_error *
5735 close_tree_view(struct tog_view *view)
5737 struct tog_tree_view_state *s = &view->state.tree;
5739 free_colors(&s->colors);
5740 free(s->tree_label);
5741 s->tree_label = NULL;
5742 free(s->commit_id);
5743 s->commit_id = NULL;
5744 free(s->head_ref_name);
5745 s->head_ref_name = NULL;
5746 while (!TAILQ_EMPTY(&s->parents)) {
5747 struct tog_parent_tree *parent;
5748 parent = TAILQ_FIRST(&s->parents);
5749 TAILQ_REMOVE(&s->parents, parent, entry);
5750 if (parent->tree != s->root)
5751 got_object_tree_close(parent->tree);
5752 free(parent);
5755 if (s->tree != NULL && s->tree != s->root)
5756 got_object_tree_close(s->tree);
5757 if (s->root)
5758 got_object_tree_close(s->root);
5759 return NULL;
5762 static const struct got_error *
5763 search_start_tree_view(struct tog_view *view)
5765 struct tog_tree_view_state *s = &view->state.tree;
5767 s->matched_entry = NULL;
5768 return NULL;
5771 static int
5772 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5774 regmatch_t regmatch;
5776 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5777 0) == 0;
5780 static const struct got_error *
5781 search_next_tree_view(struct tog_view *view)
5783 struct tog_tree_view_state *s = &view->state.tree;
5784 struct got_tree_entry *te = NULL;
5786 if (!view->searching) {
5787 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5788 return NULL;
5791 if (s->matched_entry) {
5792 if (view->searching == TOG_SEARCH_FORWARD) {
5793 if (s->selected_entry)
5794 te = got_tree_entry_get_next(s->tree,
5795 s->selected_entry);
5796 else
5797 te = got_object_tree_get_first_entry(s->tree);
5798 } else {
5799 if (s->selected_entry == NULL)
5800 te = got_object_tree_get_last_entry(s->tree);
5801 else
5802 te = got_tree_entry_get_prev(s->tree,
5803 s->selected_entry);
5805 } else {
5806 if (s->selected_entry)
5807 te = s->selected_entry;
5808 else if (view->searching == TOG_SEARCH_FORWARD)
5809 te = got_object_tree_get_first_entry(s->tree);
5810 else
5811 te = got_object_tree_get_last_entry(s->tree);
5814 while (1) {
5815 if (te == NULL) {
5816 if (s->matched_entry == NULL) {
5817 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5818 return NULL;
5820 if (view->searching == TOG_SEARCH_FORWARD)
5821 te = got_object_tree_get_first_entry(s->tree);
5822 else
5823 te = got_object_tree_get_last_entry(s->tree);
5826 if (match_tree_entry(te, &view->regex)) {
5827 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5828 s->matched_entry = te;
5829 break;
5832 if (view->searching == TOG_SEARCH_FORWARD)
5833 te = got_tree_entry_get_next(s->tree, te);
5834 else
5835 te = got_tree_entry_get_prev(s->tree, te);
5838 if (s->matched_entry) {
5839 s->first_displayed_entry = s->matched_entry;
5840 s->selected = 0;
5843 return NULL;
5846 static const struct got_error *
5847 show_tree_view(struct tog_view *view)
5849 const struct got_error *err = NULL;
5850 struct tog_tree_view_state *s = &view->state.tree;
5851 char *parent_path;
5853 err = tree_entry_path(&parent_path, &s->parents, NULL);
5854 if (err)
5855 return err;
5857 err = draw_tree_entries(view, parent_path);
5858 free(parent_path);
5860 view_vborder(view);
5861 return err;
5864 static const struct got_error *
5865 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5867 const struct got_error *err = NULL;
5868 struct tog_tree_view_state *s = &view->state.tree;
5869 struct tog_view *log_view, *ref_view;
5870 struct got_tree_entry *te;
5871 int begin_x = 0, n, nscroll = view->nlines - 3;
5873 switch (ch) {
5874 case 'i':
5875 s->show_ids = !s->show_ids;
5876 break;
5877 case 'l':
5878 if (!s->selected_entry)
5879 break;
5880 if (view_is_parent_view(view))
5881 begin_x = view_split_begin_x(view->begin_x);
5882 err = log_selected_tree_entry(&log_view, begin_x, s);
5883 view->focussed = 0;
5884 log_view->focussed = 1;
5885 if (view_is_parent_view(view)) {
5886 err = view_close_child(view);
5887 if (err)
5888 return err;
5889 err = view_set_child(view, log_view);
5890 if (err)
5891 return err;
5892 view->focus_child = 1;
5893 } else
5894 *new_view = log_view;
5895 break;
5896 case 'r':
5897 if (view_is_parent_view(view))
5898 begin_x = view_split_begin_x(view->begin_x);
5899 ref_view = view_open(view->nlines, view->ncols,
5900 view->begin_y, begin_x, TOG_VIEW_REF);
5901 if (ref_view == NULL)
5902 return got_error_from_errno("view_open");
5903 err = open_ref_view(ref_view, s->repo);
5904 if (err) {
5905 view_close(ref_view);
5906 return err;
5908 view->focussed = 0;
5909 ref_view->focussed = 1;
5910 if (view_is_parent_view(view)) {
5911 err = view_close_child(view);
5912 if (err)
5913 return err;
5914 err = view_set_child(view, ref_view);
5915 if (err)
5916 return err;
5917 view->focus_child = 1;
5918 } else
5919 *new_view = ref_view;
5920 break;
5921 case 'g':
5922 case KEY_HOME:
5923 s->selected = 0;
5924 if (s->tree == s->root)
5925 s->first_displayed_entry =
5926 got_object_tree_get_first_entry(s->tree);
5927 else
5928 s->first_displayed_entry = NULL;
5929 break;
5930 case 'G':
5931 case KEY_END:
5932 s->selected = 0;
5933 te = got_object_tree_get_last_entry(s->tree);
5934 for (n = 0; n < view->nlines - 3; n++) {
5935 if (te == NULL) {
5936 if(s->tree != s->root) {
5937 s->first_displayed_entry = NULL;
5938 n++;
5940 break;
5942 s->first_displayed_entry = te;
5943 te = got_tree_entry_get_prev(s->tree, te);
5945 if (n > 0)
5946 s->selected = n - 1;
5947 break;
5948 case 'k':
5949 case KEY_UP:
5950 case CTRL('p'):
5951 if (s->selected > 0) {
5952 s->selected--;
5953 break;
5955 tree_scroll_up(s, 1);
5956 break;
5957 case CTRL('u'):
5958 case 'u':
5959 nscroll /= 2;
5960 /* FALL THROUGH */
5961 case KEY_PPAGE:
5962 case CTRL('b'):
5963 if (s->tree == s->root) {
5964 if (got_object_tree_get_first_entry(s->tree) ==
5965 s->first_displayed_entry)
5966 s->selected -= MIN(s->selected, nscroll);
5967 } else {
5968 if (s->first_displayed_entry == NULL)
5969 s->selected -= MIN(s->selected, nscroll);
5971 tree_scroll_up(s, MAX(0, nscroll));
5972 break;
5973 case 'j':
5974 case KEY_DOWN:
5975 case CTRL('n'):
5976 if (s->selected < s->ndisplayed - 1) {
5977 s->selected++;
5978 break;
5980 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5981 == NULL)
5982 /* can't scroll any further */
5983 break;
5984 tree_scroll_down(s, 1);
5985 break;
5986 case CTRL('d'):
5987 case 'd':
5988 nscroll /= 2;
5989 /* FALL THROUGH */
5990 case KEY_NPAGE:
5991 case CTRL('f'):
5992 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5993 == NULL) {
5994 /* can't scroll any further; move cursor down */
5995 if (s->selected < s->ndisplayed - 1)
5996 s->selected += MIN(nscroll,
5997 s->ndisplayed - s->selected - 1);
5998 break;
6000 tree_scroll_down(s, nscroll);
6001 break;
6002 case KEY_ENTER:
6003 case '\r':
6004 case KEY_BACKSPACE:
6005 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6006 struct tog_parent_tree *parent;
6007 /* user selected '..' */
6008 if (s->tree == s->root)
6009 break;
6010 parent = TAILQ_FIRST(&s->parents);
6011 TAILQ_REMOVE(&s->parents, parent,
6012 entry);
6013 got_object_tree_close(s->tree);
6014 s->tree = parent->tree;
6015 s->first_displayed_entry =
6016 parent->first_displayed_entry;
6017 s->selected_entry =
6018 parent->selected_entry;
6019 s->selected = parent->selected;
6020 free(parent);
6021 } else if (S_ISDIR(got_tree_entry_get_mode(
6022 s->selected_entry))) {
6023 struct got_tree_object *subtree;
6024 err = got_object_open_as_tree(&subtree, s->repo,
6025 got_tree_entry_get_id(s->selected_entry));
6026 if (err)
6027 break;
6028 err = tree_view_visit_subtree(s, subtree);
6029 if (err) {
6030 got_object_tree_close(subtree);
6031 break;
6033 } else if (S_ISREG(got_tree_entry_get_mode(
6034 s->selected_entry))) {
6035 struct tog_view *blame_view;
6036 int begin_x = view_is_parent_view(view) ?
6037 view_split_begin_x(view->begin_x) : 0;
6039 err = blame_tree_entry(&blame_view, begin_x,
6040 s->selected_entry, &s->parents,
6041 s->commit_id, s->repo);
6042 if (err)
6043 break;
6044 view->focussed = 0;
6045 blame_view->focussed = 1;
6046 if (view_is_parent_view(view)) {
6047 err = view_close_child(view);
6048 if (err)
6049 return err;
6050 err = view_set_child(view, blame_view);
6051 if (err)
6052 return err;
6053 view->focus_child = 1;
6054 } else
6055 *new_view = blame_view;
6057 break;
6058 case KEY_RESIZE:
6059 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6060 s->selected = view->nlines - 4;
6061 break;
6062 default:
6063 break;
6066 return err;
6069 __dead static void
6070 usage_tree(void)
6072 endwin();
6073 fprintf(stderr,
6074 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6075 getprogname());
6076 exit(1);
6079 static const struct got_error *
6080 cmd_tree(int argc, char *argv[])
6082 const struct got_error *error;
6083 struct got_repository *repo = NULL;
6084 struct got_worktree *worktree = NULL;
6085 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6086 struct got_object_id *commit_id = NULL;
6087 struct got_commit_object *commit = NULL;
6088 const char *commit_id_arg = NULL;
6089 char *label = NULL;
6090 struct got_reference *ref = NULL;
6091 const char *head_ref_name = NULL;
6092 int ch;
6093 struct tog_view *view;
6094 int *pack_fds = NULL;
6096 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6097 switch (ch) {
6098 case 'c':
6099 commit_id_arg = optarg;
6100 break;
6101 case 'r':
6102 repo_path = realpath(optarg, NULL);
6103 if (repo_path == NULL)
6104 return got_error_from_errno2("realpath",
6105 optarg);
6106 break;
6107 default:
6108 usage_tree();
6109 /* NOTREACHED */
6113 argc -= optind;
6114 argv += optind;
6116 if (argc > 1)
6117 usage_tree();
6119 error = got_repo_pack_fds_open(&pack_fds);
6120 if (error != NULL)
6121 goto done;
6123 if (repo_path == NULL) {
6124 cwd = getcwd(NULL, 0);
6125 if (cwd == NULL)
6126 return got_error_from_errno("getcwd");
6127 error = got_worktree_open(&worktree, cwd);
6128 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6129 goto done;
6130 if (worktree)
6131 repo_path =
6132 strdup(got_worktree_get_repo_path(worktree));
6133 else
6134 repo_path = strdup(cwd);
6135 if (repo_path == NULL) {
6136 error = got_error_from_errno("strdup");
6137 goto done;
6141 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6142 if (error != NULL)
6143 goto done;
6145 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6146 repo, worktree);
6147 if (error)
6148 goto done;
6150 init_curses();
6152 error = apply_unveil(got_repo_get_path(repo), NULL);
6153 if (error)
6154 goto done;
6156 error = tog_load_refs(repo, 0);
6157 if (error)
6158 goto done;
6160 if (commit_id_arg == NULL) {
6161 error = got_repo_match_object_id(&commit_id, &label,
6162 worktree ? got_worktree_get_head_ref_name(worktree) :
6163 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6164 if (error)
6165 goto done;
6166 head_ref_name = label;
6167 } else {
6168 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6169 if (error == NULL)
6170 head_ref_name = got_ref_get_name(ref);
6171 else if (error->code != GOT_ERR_NOT_REF)
6172 goto done;
6173 error = got_repo_match_object_id(&commit_id, NULL,
6174 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6175 if (error)
6176 goto done;
6179 error = got_object_open_as_commit(&commit, repo, commit_id);
6180 if (error)
6181 goto done;
6183 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6184 if (view == NULL) {
6185 error = got_error_from_errno("view_open");
6186 goto done;
6188 error = open_tree_view(view, commit_id, head_ref_name, repo);
6189 if (error)
6190 goto done;
6191 if (!got_path_is_root_dir(in_repo_path)) {
6192 error = tree_view_walk_path(&view->state.tree, commit,
6193 in_repo_path);
6194 if (error)
6195 goto done;
6198 if (worktree) {
6199 /* Release work tree lock. */
6200 got_worktree_close(worktree);
6201 worktree = NULL;
6203 error = view_loop(view);
6204 done:
6205 free(repo_path);
6206 free(cwd);
6207 free(commit_id);
6208 free(label);
6209 if (ref)
6210 got_ref_close(ref);
6211 if (repo) {
6212 const struct got_error *close_err = got_repo_close(repo);
6213 if (error == NULL)
6214 error = close_err;
6216 if (pack_fds) {
6217 const struct got_error *pack_err =
6218 got_repo_pack_fds_close(pack_fds);
6219 if (error == NULL)
6220 error = pack_err;
6222 tog_free_refs();
6223 return error;
6226 static const struct got_error *
6227 ref_view_load_refs(struct tog_ref_view_state *s)
6229 struct got_reflist_entry *sre;
6230 struct tog_reflist_entry *re;
6232 s->nrefs = 0;
6233 TAILQ_FOREACH(sre, &tog_refs, entry) {
6234 if (strncmp(got_ref_get_name(sre->ref),
6235 "refs/got/", 9) == 0 &&
6236 strncmp(got_ref_get_name(sre->ref),
6237 "refs/got/backup/", 16) != 0)
6238 continue;
6240 re = malloc(sizeof(*re));
6241 if (re == NULL)
6242 return got_error_from_errno("malloc");
6244 re->ref = got_ref_dup(sre->ref);
6245 if (re->ref == NULL)
6246 return got_error_from_errno("got_ref_dup");
6247 re->idx = s->nrefs++;
6248 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6251 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6252 return NULL;
6255 void
6256 ref_view_free_refs(struct tog_ref_view_state *s)
6258 struct tog_reflist_entry *re;
6260 while (!TAILQ_EMPTY(&s->refs)) {
6261 re = TAILQ_FIRST(&s->refs);
6262 TAILQ_REMOVE(&s->refs, re, entry);
6263 got_ref_close(re->ref);
6264 free(re);
6268 static const struct got_error *
6269 open_ref_view(struct tog_view *view, struct got_repository *repo)
6271 const struct got_error *err = NULL;
6272 struct tog_ref_view_state *s = &view->state.ref;
6274 s->selected_entry = 0;
6275 s->repo = repo;
6277 TAILQ_INIT(&s->refs);
6278 STAILQ_INIT(&s->colors);
6280 err = ref_view_load_refs(s);
6281 if (err)
6282 return err;
6284 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6285 err = add_color(&s->colors, "^refs/heads/",
6286 TOG_COLOR_REFS_HEADS,
6287 get_color_value("TOG_COLOR_REFS_HEADS"));
6288 if (err)
6289 goto done;
6291 err = add_color(&s->colors, "^refs/tags/",
6292 TOG_COLOR_REFS_TAGS,
6293 get_color_value("TOG_COLOR_REFS_TAGS"));
6294 if (err)
6295 goto done;
6297 err = add_color(&s->colors, "^refs/remotes/",
6298 TOG_COLOR_REFS_REMOTES,
6299 get_color_value("TOG_COLOR_REFS_REMOTES"));
6300 if (err)
6301 goto done;
6303 err = add_color(&s->colors, "^refs/got/backup/",
6304 TOG_COLOR_REFS_BACKUP,
6305 get_color_value("TOG_COLOR_REFS_BACKUP"));
6306 if (err)
6307 goto done;
6310 view->show = show_ref_view;
6311 view->input = input_ref_view;
6312 view->close = close_ref_view;
6313 view->search_start = search_start_ref_view;
6314 view->search_next = search_next_ref_view;
6315 done:
6316 if (err)
6317 free_colors(&s->colors);
6318 return err;
6321 static const struct got_error *
6322 close_ref_view(struct tog_view *view)
6324 struct tog_ref_view_state *s = &view->state.ref;
6326 ref_view_free_refs(s);
6327 free_colors(&s->colors);
6329 return NULL;
6332 static const struct got_error *
6333 resolve_reflist_entry(struct got_object_id **commit_id,
6334 struct tog_reflist_entry *re, struct got_repository *repo)
6336 const struct got_error *err = NULL;
6337 struct got_object_id *obj_id;
6338 struct got_tag_object *tag = NULL;
6339 int obj_type;
6341 *commit_id = NULL;
6343 err = got_ref_resolve(&obj_id, repo, re->ref);
6344 if (err)
6345 return err;
6347 err = got_object_get_type(&obj_type, repo, obj_id);
6348 if (err)
6349 goto done;
6351 switch (obj_type) {
6352 case GOT_OBJ_TYPE_COMMIT:
6353 *commit_id = obj_id;
6354 break;
6355 case GOT_OBJ_TYPE_TAG:
6356 err = got_object_open_as_tag(&tag, repo, obj_id);
6357 if (err)
6358 goto done;
6359 free(obj_id);
6360 err = got_object_get_type(&obj_type, repo,
6361 got_object_tag_get_object_id(tag));
6362 if (err)
6363 goto done;
6364 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6365 err = got_error(GOT_ERR_OBJ_TYPE);
6366 goto done;
6368 *commit_id = got_object_id_dup(
6369 got_object_tag_get_object_id(tag));
6370 if (*commit_id == NULL) {
6371 err = got_error_from_errno("got_object_id_dup");
6372 goto done;
6374 break;
6375 default:
6376 err = got_error(GOT_ERR_OBJ_TYPE);
6377 break;
6380 done:
6381 if (tag)
6382 got_object_tag_close(tag);
6383 if (err) {
6384 free(*commit_id);
6385 *commit_id = NULL;
6387 return err;
6390 static const struct got_error *
6391 log_ref_entry(struct tog_view **new_view, int begin_x,
6392 struct tog_reflist_entry *re, struct got_repository *repo)
6394 struct tog_view *log_view;
6395 const struct got_error *err = NULL;
6396 struct got_object_id *commit_id = NULL;
6398 *new_view = NULL;
6400 err = resolve_reflist_entry(&commit_id, re, repo);
6401 if (err) {
6402 if (err->code != GOT_ERR_OBJ_TYPE)
6403 return err;
6404 else
6405 return NULL;
6408 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6409 if (log_view == NULL) {
6410 err = got_error_from_errno("view_open");
6411 goto done;
6414 err = open_log_view(log_view, commit_id, repo,
6415 got_ref_get_name(re->ref), "", 0);
6416 done:
6417 if (err)
6418 view_close(log_view);
6419 else
6420 *new_view = log_view;
6421 free(commit_id);
6422 return err;
6425 static void
6426 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6428 struct tog_reflist_entry *re;
6429 int i = 0;
6431 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6432 return;
6434 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6435 while (i++ < maxscroll) {
6436 if (re == NULL)
6437 break;
6438 s->first_displayed_entry = re;
6439 re = TAILQ_PREV(re, tog_reflist_head, entry);
6443 static void
6444 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6446 struct tog_reflist_entry *next, *last;
6447 int n = 0;
6449 if (s->first_displayed_entry)
6450 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6451 else
6452 next = TAILQ_FIRST(&s->refs);
6454 last = s->last_displayed_entry;
6455 while (next && last && n++ < maxscroll) {
6456 last = TAILQ_NEXT(last, entry);
6457 if (last) {
6458 s->first_displayed_entry = next;
6459 next = TAILQ_NEXT(next, entry);
6464 static const struct got_error *
6465 search_start_ref_view(struct tog_view *view)
6467 struct tog_ref_view_state *s = &view->state.ref;
6469 s->matched_entry = NULL;
6470 return NULL;
6473 static int
6474 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6476 regmatch_t regmatch;
6478 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6479 0) == 0;
6482 static const struct got_error *
6483 search_next_ref_view(struct tog_view *view)
6485 struct tog_ref_view_state *s = &view->state.ref;
6486 struct tog_reflist_entry *re = NULL;
6488 if (!view->searching) {
6489 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6490 return NULL;
6493 if (s->matched_entry) {
6494 if (view->searching == TOG_SEARCH_FORWARD) {
6495 if (s->selected_entry)
6496 re = TAILQ_NEXT(s->selected_entry, entry);
6497 else
6498 re = TAILQ_PREV(s->selected_entry,
6499 tog_reflist_head, entry);
6500 } else {
6501 if (s->selected_entry == NULL)
6502 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6503 else
6504 re = TAILQ_PREV(s->selected_entry,
6505 tog_reflist_head, entry);
6507 } else {
6508 if (s->selected_entry)
6509 re = s->selected_entry;
6510 else if (view->searching == TOG_SEARCH_FORWARD)
6511 re = TAILQ_FIRST(&s->refs);
6512 else
6513 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6516 while (1) {
6517 if (re == NULL) {
6518 if (s->matched_entry == NULL) {
6519 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6520 return NULL;
6522 if (view->searching == TOG_SEARCH_FORWARD)
6523 re = TAILQ_FIRST(&s->refs);
6524 else
6525 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6528 if (match_reflist_entry(re, &view->regex)) {
6529 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6530 s->matched_entry = re;
6531 break;
6534 if (view->searching == TOG_SEARCH_FORWARD)
6535 re = TAILQ_NEXT(re, entry);
6536 else
6537 re = TAILQ_PREV(re, tog_reflist_head, entry);
6540 if (s->matched_entry) {
6541 s->first_displayed_entry = s->matched_entry;
6542 s->selected = 0;
6545 return NULL;
6548 static const struct got_error *
6549 show_ref_view(struct tog_view *view)
6551 const struct got_error *err = NULL;
6552 struct tog_ref_view_state *s = &view->state.ref;
6553 struct tog_reflist_entry *re;
6554 char *line = NULL;
6555 wchar_t *wline;
6556 struct tog_color *tc;
6557 int width, n;
6558 int limit = view->nlines;
6560 werase(view->window);
6562 s->ndisplayed = 0;
6564 if (limit == 0)
6565 return NULL;
6567 re = s->first_displayed_entry;
6569 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6570 s->nrefs) == -1)
6571 return got_error_from_errno("asprintf");
6573 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6574 if (err) {
6575 free(line);
6576 return err;
6578 if (view_needs_focus_indication(view))
6579 wstandout(view->window);
6580 waddwstr(view->window, wline);
6581 if (view_needs_focus_indication(view))
6582 wstandend(view->window);
6583 free(wline);
6584 wline = NULL;
6585 free(line);
6586 line = NULL;
6587 if (width < view->ncols - 1)
6588 waddch(view->window, '\n');
6589 if (--limit <= 0)
6590 return NULL;
6592 n = 0;
6593 while (re && limit > 0) {
6594 char *line = NULL;
6595 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6597 if (s->show_date) {
6598 struct got_commit_object *ci;
6599 struct got_tag_object *tag;
6600 struct got_object_id *id;
6601 struct tm tm;
6602 time_t t;
6604 err = got_ref_resolve(&id, s->repo, re->ref);
6605 if (err)
6606 return err;
6607 err = got_object_open_as_tag(&tag, s->repo, id);
6608 if (err) {
6609 if (err->code != GOT_ERR_OBJ_TYPE) {
6610 free(id);
6611 return err;
6613 err = got_object_open_as_commit(&ci, s->repo,
6614 id);
6615 if (err) {
6616 free(id);
6617 return err;
6619 t = got_object_commit_get_committer_time(ci);
6620 got_object_commit_close(ci);
6621 } else {
6622 t = got_object_tag_get_tagger_time(tag);
6623 got_object_tag_close(tag);
6625 free(id);
6626 if (gmtime_r(&t, &tm) == NULL)
6627 return got_error_from_errno("gmtime_r");
6628 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6629 return got_error(GOT_ERR_NO_SPACE);
6631 if (got_ref_is_symbolic(re->ref)) {
6632 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6633 ymd : "", got_ref_get_name(re->ref),
6634 got_ref_get_symref_target(re->ref)) == -1)
6635 return got_error_from_errno("asprintf");
6636 } else if (s->show_ids) {
6637 struct got_object_id *id;
6638 char *id_str;
6639 err = got_ref_resolve(&id, s->repo, re->ref);
6640 if (err)
6641 return err;
6642 err = got_object_id_str(&id_str, id);
6643 if (err) {
6644 free(id);
6645 return err;
6647 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6648 got_ref_get_name(re->ref), id_str) == -1) {
6649 err = got_error_from_errno("asprintf");
6650 free(id);
6651 free(id_str);
6652 return err;
6654 free(id);
6655 free(id_str);
6656 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6657 got_ref_get_name(re->ref)) == -1)
6658 return got_error_from_errno("asprintf");
6660 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6661 0, 0);
6662 if (err) {
6663 free(line);
6664 return err;
6666 if (n == s->selected) {
6667 if (view->focussed)
6668 wstandout(view->window);
6669 s->selected_entry = re;
6671 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6672 if (tc)
6673 wattr_on(view->window,
6674 COLOR_PAIR(tc->colorpair), NULL);
6675 waddwstr(view->window, wline);
6676 if (tc)
6677 wattr_off(view->window,
6678 COLOR_PAIR(tc->colorpair), NULL);
6679 if (width < view->ncols - 1)
6680 waddch(view->window, '\n');
6681 if (n == s->selected && view->focussed)
6682 wstandend(view->window);
6683 free(line);
6684 free(wline);
6685 wline = NULL;
6686 n++;
6687 s->ndisplayed++;
6688 s->last_displayed_entry = re;
6690 limit--;
6691 re = TAILQ_NEXT(re, entry);
6694 view_vborder(view);
6695 return err;
6698 static const struct got_error *
6699 browse_ref_tree(struct tog_view **new_view, int begin_x,
6700 struct tog_reflist_entry *re, struct got_repository *repo)
6702 const struct got_error *err = NULL;
6703 struct got_object_id *commit_id = NULL;
6704 struct tog_view *tree_view;
6706 *new_view = NULL;
6708 err = resolve_reflist_entry(&commit_id, re, repo);
6709 if (err) {
6710 if (err->code != GOT_ERR_OBJ_TYPE)
6711 return err;
6712 else
6713 return NULL;
6717 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6718 if (tree_view == NULL) {
6719 err = got_error_from_errno("view_open");
6720 goto done;
6723 err = open_tree_view(tree_view, commit_id,
6724 got_ref_get_name(re->ref), repo);
6725 if (err)
6726 goto done;
6728 *new_view = tree_view;
6729 done:
6730 free(commit_id);
6731 return err;
6733 static const struct got_error *
6734 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6736 const struct got_error *err = NULL;
6737 struct tog_ref_view_state *s = &view->state.ref;
6738 struct tog_view *log_view, *tree_view;
6739 struct tog_reflist_entry *re;
6740 int begin_x = 0, n, nscroll = view->nlines - 1;
6742 switch (ch) {
6743 case 'i':
6744 s->show_ids = !s->show_ids;
6745 break;
6746 case 'm':
6747 s->show_date = !s->show_date;
6748 break;
6749 case 'o':
6750 s->sort_by_date = !s->sort_by_date;
6751 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6752 got_ref_cmp_by_commit_timestamp_descending :
6753 tog_ref_cmp_by_name, s->repo);
6754 if (err)
6755 break;
6756 got_reflist_object_id_map_free(tog_refs_idmap);
6757 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6758 &tog_refs, s->repo);
6759 if (err)
6760 break;
6761 ref_view_free_refs(s);
6762 err = ref_view_load_refs(s);
6763 break;
6764 case KEY_ENTER:
6765 case '\r':
6766 if (!s->selected_entry)
6767 break;
6768 if (view_is_parent_view(view))
6769 begin_x = view_split_begin_x(view->begin_x);
6770 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6771 s->repo);
6772 view->focussed = 0;
6773 log_view->focussed = 1;
6774 if (view_is_parent_view(view)) {
6775 err = view_close_child(view);
6776 if (err)
6777 return err;
6778 err = view_set_child(view, log_view);
6779 if (err)
6780 return err;
6781 view->focus_child = 1;
6782 } else
6783 *new_view = log_view;
6784 break;
6785 case 't':
6786 if (!s->selected_entry)
6787 break;
6788 if (view_is_parent_view(view))
6789 begin_x = view_split_begin_x(view->begin_x);
6790 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6791 s->repo);
6792 if (err || tree_view == NULL)
6793 break;
6794 view->focussed = 0;
6795 tree_view->focussed = 1;
6796 if (view_is_parent_view(view)) {
6797 err = view_close_child(view);
6798 if (err)
6799 return err;
6800 err = view_set_child(view, tree_view);
6801 if (err)
6802 return err;
6803 view->focus_child = 1;
6804 } else
6805 *new_view = tree_view;
6806 break;
6807 case 'g':
6808 case KEY_HOME:
6809 s->selected = 0;
6810 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6811 break;
6812 case 'G':
6813 case KEY_END:
6814 s->selected = 0;
6815 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6816 for (n = 0; n < view->nlines - 1; n++) {
6817 if (re == NULL)
6818 break;
6819 s->first_displayed_entry = re;
6820 re = TAILQ_PREV(re, tog_reflist_head, entry);
6822 if (n > 0)
6823 s->selected = n - 1;
6824 break;
6825 case 'k':
6826 case KEY_UP:
6827 case CTRL('p'):
6828 if (s->selected > 0) {
6829 s->selected--;
6830 break;
6832 ref_scroll_up(s, 1);
6833 break;
6834 case CTRL('u'):
6835 case 'u':
6836 nscroll /= 2;
6837 /* FALL THROUGH */
6838 case KEY_PPAGE:
6839 case CTRL('b'):
6840 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6841 s->selected -= MIN(nscroll, s->selected);
6842 ref_scroll_up(s, MAX(0, nscroll));
6843 break;
6844 case 'j':
6845 case KEY_DOWN:
6846 case CTRL('n'):
6847 if (s->selected < s->ndisplayed - 1) {
6848 s->selected++;
6849 break;
6851 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6852 /* can't scroll any further */
6853 break;
6854 ref_scroll_down(s, 1);
6855 break;
6856 case CTRL('d'):
6857 case 'd':
6858 nscroll /= 2;
6859 /* FALL THROUGH */
6860 case KEY_NPAGE:
6861 case CTRL('f'):
6862 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6863 /* can't scroll any further; move cursor down */
6864 if (s->selected < s->ndisplayed - 1)
6865 s->selected += MIN(nscroll,
6866 s->ndisplayed - s->selected - 1);
6867 break;
6869 ref_scroll_down(s, nscroll);
6870 break;
6871 case CTRL('l'):
6872 tog_free_refs();
6873 err = tog_load_refs(s->repo, s->sort_by_date);
6874 if (err)
6875 break;
6876 ref_view_free_refs(s);
6877 err = ref_view_load_refs(s);
6878 break;
6879 case KEY_RESIZE:
6880 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6881 s->selected = view->nlines - 2;
6882 break;
6883 default:
6884 break;
6887 return err;
6890 __dead static void
6891 usage_ref(void)
6893 endwin();
6894 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6895 getprogname());
6896 exit(1);
6899 static const struct got_error *
6900 cmd_ref(int argc, char *argv[])
6902 const struct got_error *error;
6903 struct got_repository *repo = NULL;
6904 struct got_worktree *worktree = NULL;
6905 char *cwd = NULL, *repo_path = NULL;
6906 int ch;
6907 struct tog_view *view;
6908 int *pack_fds = NULL;
6910 while ((ch = getopt(argc, argv, "r:")) != -1) {
6911 switch (ch) {
6912 case 'r':
6913 repo_path = realpath(optarg, NULL);
6914 if (repo_path == NULL)
6915 return got_error_from_errno2("realpath",
6916 optarg);
6917 break;
6918 default:
6919 usage_ref();
6920 /* NOTREACHED */
6924 argc -= optind;
6925 argv += optind;
6927 if (argc > 1)
6928 usage_ref();
6930 error = got_repo_pack_fds_open(&pack_fds);
6931 if (error != NULL)
6932 goto done;
6934 if (repo_path == NULL) {
6935 cwd = getcwd(NULL, 0);
6936 if (cwd == NULL)
6937 return got_error_from_errno("getcwd");
6938 error = got_worktree_open(&worktree, cwd);
6939 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6940 goto done;
6941 if (worktree)
6942 repo_path =
6943 strdup(got_worktree_get_repo_path(worktree));
6944 else
6945 repo_path = strdup(cwd);
6946 if (repo_path == NULL) {
6947 error = got_error_from_errno("strdup");
6948 goto done;
6952 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6953 if (error != NULL)
6954 goto done;
6956 init_curses();
6958 error = apply_unveil(got_repo_get_path(repo), NULL);
6959 if (error)
6960 goto done;
6962 error = tog_load_refs(repo, 0);
6963 if (error)
6964 goto done;
6966 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6967 if (view == NULL) {
6968 error = got_error_from_errno("view_open");
6969 goto done;
6972 error = open_ref_view(view, repo);
6973 if (error)
6974 goto done;
6976 if (worktree) {
6977 /* Release work tree lock. */
6978 got_worktree_close(worktree);
6979 worktree = NULL;
6981 error = view_loop(view);
6982 done:
6983 free(repo_path);
6984 free(cwd);
6985 if (repo) {
6986 const struct got_error *close_err = got_repo_close(repo);
6987 if (close_err)
6988 error = close_err;
6990 if (pack_fds) {
6991 const struct got_error *pack_err =
6992 got_repo_pack_fds_close(pack_fds);
6993 if (error == NULL)
6994 error = pack_err;
6996 tog_free_refs();
6997 return error;
7000 static void
7001 list_commands(FILE *fp)
7003 size_t i;
7005 fprintf(fp, "commands:");
7006 for (i = 0; i < nitems(tog_commands); i++) {
7007 const struct tog_cmd *cmd = &tog_commands[i];
7008 fprintf(fp, " %s", cmd->name);
7010 fputc('\n', fp);
7013 __dead static void
7014 usage(int hflag, int status)
7016 FILE *fp = (status == 0) ? stdout : stderr;
7018 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7019 getprogname());
7020 if (hflag) {
7021 fprintf(fp, "lazy usage: %s path\n", getprogname());
7022 list_commands(fp);
7024 exit(status);
7027 static char **
7028 make_argv(int argc, ...)
7030 va_list ap;
7031 char **argv;
7032 int i;
7034 va_start(ap, argc);
7036 argv = calloc(argc, sizeof(char *));
7037 if (argv == NULL)
7038 err(1, "calloc");
7039 for (i = 0; i < argc; i++) {
7040 argv[i] = strdup(va_arg(ap, char *));
7041 if (argv[i] == NULL)
7042 err(1, "strdup");
7045 va_end(ap);
7046 return argv;
7050 * Try to convert 'tog path' into a 'tog log path' command.
7051 * The user could simply have mistyped the command rather than knowingly
7052 * provided a path. So check whether argv[0] can in fact be resolved
7053 * to a path in the HEAD commit and print a special error if not.
7054 * This hack is for mpi@ <3
7056 static const struct got_error *
7057 tog_log_with_path(int argc, char *argv[])
7059 const struct got_error *error = NULL, *close_err;
7060 const struct tog_cmd *cmd = NULL;
7061 struct got_repository *repo = NULL;
7062 struct got_worktree *worktree = NULL;
7063 struct got_object_id *commit_id = NULL, *id = NULL;
7064 struct got_commit_object *commit = NULL;
7065 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7066 char *commit_id_str = NULL, **cmd_argv = NULL;
7067 int *pack_fds = NULL;
7069 cwd = getcwd(NULL, 0);
7070 if (cwd == NULL)
7071 return got_error_from_errno("getcwd");
7073 error = got_repo_pack_fds_open(&pack_fds);
7074 if (error != NULL)
7075 goto done;
7077 error = got_worktree_open(&worktree, cwd);
7078 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7079 goto done;
7081 if (worktree)
7082 repo_path = strdup(got_worktree_get_repo_path(worktree));
7083 else
7084 repo_path = strdup(cwd);
7085 if (repo_path == NULL) {
7086 error = got_error_from_errno("strdup");
7087 goto done;
7090 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7091 if (error != NULL)
7092 goto done;
7094 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7095 repo, worktree);
7096 if (error)
7097 goto done;
7099 error = tog_load_refs(repo, 0);
7100 if (error)
7101 goto done;
7102 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7103 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7104 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7105 if (error)
7106 goto done;
7108 if (worktree) {
7109 got_worktree_close(worktree);
7110 worktree = NULL;
7113 error = got_object_open_as_commit(&commit, repo, commit_id);
7114 if (error)
7115 goto done;
7117 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7118 if (error) {
7119 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7120 goto done;
7121 fprintf(stderr, "%s: '%s' is no known command or path\n",
7122 getprogname(), argv[0]);
7123 usage(1, 1);
7124 /* not reached */
7127 close_err = got_repo_close(repo);
7128 if (error == NULL)
7129 error = close_err;
7130 repo = NULL;
7132 error = got_object_id_str(&commit_id_str, commit_id);
7133 if (error)
7134 goto done;
7136 cmd = &tog_commands[0]; /* log */
7137 argc = 4;
7138 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7139 error = cmd->cmd_main(argc, cmd_argv);
7140 done:
7141 if (repo) {
7142 close_err = got_repo_close(repo);
7143 if (error == NULL)
7144 error = close_err;
7146 if (commit)
7147 got_object_commit_close(commit);
7148 if (worktree)
7149 got_worktree_close(worktree);
7150 if (pack_fds) {
7151 const struct got_error *pack_err =
7152 got_repo_pack_fds_close(pack_fds);
7153 if (error == NULL)
7154 error = pack_err;
7156 free(id);
7157 free(commit_id_str);
7158 free(commit_id);
7159 free(cwd);
7160 free(repo_path);
7161 free(in_repo_path);
7162 if (cmd_argv) {
7163 int i;
7164 for (i = 0; i < argc; i++)
7165 free(cmd_argv[i]);
7166 free(cmd_argv);
7168 tog_free_refs();
7169 return error;
7172 int
7173 main(int argc, char *argv[])
7175 const struct got_error *error = NULL;
7176 const struct tog_cmd *cmd = NULL;
7177 int ch, hflag = 0, Vflag = 0;
7178 char **cmd_argv = NULL;
7179 static const struct option longopts[] = {
7180 { "version", no_argument, NULL, 'V' },
7181 { NULL, 0, NULL, 0}
7184 setlocale(LC_CTYPE, "");
7186 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7187 switch (ch) {
7188 case 'h':
7189 hflag = 1;
7190 break;
7191 case 'V':
7192 Vflag = 1;
7193 break;
7194 default:
7195 usage(hflag, 1);
7196 /* NOTREACHED */
7200 argc -= optind;
7201 argv += optind;
7202 optind = 1;
7203 optreset = 1;
7205 if (Vflag) {
7206 got_version_print_str();
7207 return 0;
7210 #ifndef PROFILE
7211 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7212 NULL) == -1)
7213 err(1, "pledge");
7214 #endif
7216 if (argc == 0) {
7217 if (hflag)
7218 usage(hflag, 0);
7219 /* Build an argument vector which runs a default command. */
7220 cmd = &tog_commands[0];
7221 argc = 1;
7222 cmd_argv = make_argv(argc, cmd->name);
7223 } else {
7224 size_t i;
7226 /* Did the user specify a command? */
7227 for (i = 0; i < nitems(tog_commands); i++) {
7228 if (strncmp(tog_commands[i].name, argv[0],
7229 strlen(argv[0])) == 0) {
7230 cmd = &tog_commands[i];
7231 break;
7236 if (cmd == NULL) {
7237 if (argc != 1)
7238 usage(0, 1);
7239 /* No command specified; try log with a path */
7240 error = tog_log_with_path(argc, argv);
7241 } else {
7242 if (hflag)
7243 cmd->cmd_usage();
7244 else
7245 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7248 endwin();
7249 putchar('\n');
7250 if (cmd_argv) {
7251 int i;
7252 for (i = 0; i < argc; i++)
7253 free(cmd_argv[i]);
7254 free(cmd_argv);
7257 if (error && error->code != GOT_ERR_CANCELLED)
7258 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7259 return 0;