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()
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 (wresize(view->window, nlines, ncols) == ERR)
767 return got_error_from_errno("wresize");
768 if (replace_panel(view->panel, view->window) == ERR)
769 return got_error_from_errno("replace_panel");
770 wclear(view->window);
772 view->nlines = nlines;
773 view->ncols = ncols;
774 view->lines = LINES;
775 view->cols = COLS;
777 if (view->child) {
778 view->child->begin_x = view_split_begin_x(view->begin_x);
779 if (view->child->begin_x == 0) {
780 view_fullscreen(view->child);
781 if (view->child->focussed)
782 show_panel(view->child->panel);
783 else
784 show_panel(view->panel);
785 } else {
786 view_splitscreen(view->child);
787 show_panel(view->child->panel);
791 return NULL;
794 static const struct got_error *
795 view_close_child(struct tog_view *view)
797 const struct got_error *err = NULL;
799 if (view->child == NULL)
800 return NULL;
802 err = view_close(view->child);
803 view->child = NULL;
804 return err;
807 static void
808 view_set_child(struct tog_view *view, struct tog_view *child)
810 view->child = child;
811 child->parent = view;
814 static int
815 view_is_splitscreen(struct tog_view *view)
817 return view->begin_x > 0;
820 static void
821 tog_resizeterm(void)
823 int cols, lines;
824 struct winsize size;
826 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
827 cols = 80; /* Default */
828 lines = 24;
829 } else {
830 cols = size.ws_col;
831 lines = size.ws_row;
833 resize_term(lines, cols);
836 static const struct got_error *
837 view_search_start(struct tog_view *view)
839 const struct got_error *err = NULL;
840 char pattern[1024];
841 int ret;
843 if (view->search_started) {
844 regfree(&view->regex);
845 view->searching = 0;
846 memset(&view->regmatch, 0, sizeof(view->regmatch));
848 view->search_started = 0;
850 if (view->nlines < 1)
851 return NULL;
853 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
854 wclrtoeol(view->window);
856 nocbreak();
857 echo();
858 ret = wgetnstr(view->window, pattern, sizeof(pattern));
859 cbreak();
860 noecho();
861 if (ret == ERR)
862 return NULL;
864 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
865 err = view->search_start(view);
866 if (err) {
867 regfree(&view->regex);
868 return err;
870 view->search_started = 1;
871 view->searching = TOG_SEARCH_FORWARD;
872 view->search_next_done = 0;
873 view->search_next(view);
876 return NULL;
879 static const struct got_error *
880 view_input(struct tog_view **new, int *done, struct tog_view *view,
881 struct tog_view_list_head *views)
883 const struct got_error *err = NULL;
884 struct tog_view *v;
885 int ch, errcode;
887 *new = NULL;
889 /* Clear "no matches" indicator. */
890 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
891 view->search_next_done == TOG_SEARCH_HAVE_NONE)
892 view->search_next_done = TOG_SEARCH_HAVE_MORE;
894 if (view->searching && !view->search_next_done) {
895 errcode = pthread_mutex_unlock(&tog_mutex);
896 if (errcode)
897 return got_error_set_errno(errcode,
898 "pthread_mutex_unlock");
899 sched_yield();
900 errcode = pthread_mutex_lock(&tog_mutex);
901 if (errcode)
902 return got_error_set_errno(errcode,
903 "pthread_mutex_lock");
904 view->search_next(view);
905 return NULL;
908 nodelay(stdscr, FALSE);
909 /* Allow threads to make progress while we are waiting for input. */
910 errcode = pthread_mutex_unlock(&tog_mutex);
911 if (errcode)
912 return got_error_set_errno(errcode, "pthread_mutex_unlock");
913 ch = wgetch(view->window);
914 errcode = pthread_mutex_lock(&tog_mutex);
915 if (errcode)
916 return got_error_set_errno(errcode, "pthread_mutex_lock");
917 nodelay(stdscr, TRUE);
919 if (tog_sigwinch_received || tog_sigcont_received) {
920 tog_resizeterm();
921 tog_sigwinch_received = 0;
922 tog_sigcont_received = 0;
923 TAILQ_FOREACH(v, views, entry) {
924 err = view_resize(v);
925 if (err)
926 return err;
927 err = v->input(new, v, KEY_RESIZE);
928 if (err)
929 return err;
930 if (v->child) {
931 err = view_resize(v->child);
932 if (err)
933 return err;
934 err = v->child->input(new, v->child,
935 KEY_RESIZE);
936 if (err)
937 return err;
942 switch (ch) {
943 case '\t':
944 if (view->child) {
945 view->focussed = 0;
946 view->child->focussed = 1;
947 view->focus_child = 1;
948 } else if (view->parent) {
949 view->focussed = 0;
950 view->parent->focussed = 1;
951 view->parent->focus_child = 0;
953 break;
954 case 'q':
955 err = view->input(new, view, ch);
956 view->dying = 1;
957 break;
958 case 'Q':
959 *done = 1;
960 break;
961 case 'f':
962 if (view_is_parent_view(view)) {
963 if (view->child == NULL)
964 break;
965 if (view_is_splitscreen(view->child)) {
966 view->focussed = 0;
967 view->child->focussed = 1;
968 err = view_fullscreen(view->child);
969 } else
970 err = view_splitscreen(view->child);
971 if (err)
972 break;
973 err = view->child->input(new, view->child,
974 KEY_RESIZE);
975 } else {
976 if (view_is_splitscreen(view)) {
977 view->parent->focussed = 0;
978 view->focussed = 1;
979 err = view_fullscreen(view);
980 } else {
981 err = view_splitscreen(view);
983 if (err)
984 break;
985 err = view->input(new, view, KEY_RESIZE);
987 break;
988 case KEY_RESIZE:
989 break;
990 case '/':
991 if (view->search_start)
992 view_search_start(view);
993 else
994 err = view->input(new, view, ch);
995 break;
996 case 'N':
997 case 'n':
998 if (view->search_started && view->search_next) {
999 view->searching = (ch == 'n' ?
1000 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1001 view->search_next_done = 0;
1002 view->search_next(view);
1003 } else
1004 err = view->input(new, view, ch);
1005 break;
1006 default:
1007 err = view->input(new, view, ch);
1008 break;
1011 return err;
1014 void
1015 view_vborder(struct tog_view *view)
1017 PANEL *panel;
1018 const struct tog_view *view_above;
1020 if (view->parent)
1021 return view_vborder(view->parent);
1023 panel = panel_above(view->panel);
1024 if (panel == NULL)
1025 return;
1027 view_above = panel_userptr(panel);
1028 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1029 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1032 int
1033 view_needs_focus_indication(struct tog_view *view)
1035 if (view_is_parent_view(view)) {
1036 if (view->child == NULL || view->child->focussed)
1037 return 0;
1038 if (!view_is_splitscreen(view->child))
1039 return 0;
1040 } else if (!view_is_splitscreen(view))
1041 return 0;
1043 return view->focussed;
1046 static const struct got_error *
1047 view_loop(struct tog_view *view)
1049 const struct got_error *err = NULL;
1050 struct tog_view_list_head views;
1051 struct tog_view *new_view;
1052 int fast_refresh = 10;
1053 int done = 0, errcode;
1055 errcode = pthread_mutex_lock(&tog_mutex);
1056 if (errcode)
1057 return got_error_set_errno(errcode, "pthread_mutex_lock");
1059 TAILQ_INIT(&views);
1060 TAILQ_INSERT_HEAD(&views, view, entry);
1062 view->focussed = 1;
1063 err = view->show(view);
1064 if (err)
1065 return err;
1066 update_panels();
1067 doupdate();
1068 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1069 /* Refresh fast during initialization, then become slower. */
1070 if (fast_refresh && fast_refresh-- == 0)
1071 halfdelay(10); /* switch to once per second */
1073 err = view_input(&new_view, &done, view, &views);
1074 if (err)
1075 break;
1076 if (view->dying) {
1077 struct tog_view *v, *prev = NULL;
1079 if (view_is_parent_view(view))
1080 prev = TAILQ_PREV(view, tog_view_list_head,
1081 entry);
1082 else if (view->parent)
1083 prev = view->parent;
1085 if (view->parent) {
1086 view->parent->child = NULL;
1087 view->parent->focus_child = 0;
1088 } else
1089 TAILQ_REMOVE(&views, view, entry);
1091 err = view_close(view);
1092 if (err)
1093 goto done;
1095 view = NULL;
1096 TAILQ_FOREACH(v, &views, entry) {
1097 if (v->focussed)
1098 break;
1100 if (view == NULL && new_view == NULL) {
1101 /* No view has focus. Try to pick one. */
1102 if (prev)
1103 view = prev;
1104 else if (!TAILQ_EMPTY(&views)) {
1105 view = TAILQ_LAST(&views,
1106 tog_view_list_head);
1108 if (view) {
1109 if (view->focus_child) {
1110 view->child->focussed = 1;
1111 view = view->child;
1112 } else
1113 view->focussed = 1;
1117 if (new_view) {
1118 struct tog_view *v, *t;
1119 /* Only allow one parent view per type. */
1120 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1121 if (v->type != new_view->type)
1122 continue;
1123 TAILQ_REMOVE(&views, v, entry);
1124 err = view_close(v);
1125 if (err)
1126 goto done;
1127 break;
1129 TAILQ_INSERT_TAIL(&views, new_view, entry);
1130 view = new_view;
1132 if (view) {
1133 if (view_is_parent_view(view)) {
1134 if (view->child && view->child->focussed)
1135 view = view->child;
1136 } else {
1137 if (view->parent && view->parent->focussed)
1138 view = view->parent;
1140 show_panel(view->panel);
1141 if (view->child && view_is_splitscreen(view->child))
1142 show_panel(view->child->panel);
1143 if (view->parent && view_is_splitscreen(view)) {
1144 err = view->parent->show(view->parent);
1145 if (err)
1146 goto done;
1148 err = view->show(view);
1149 if (err)
1150 goto done;
1151 if (view->child) {
1152 err = view->child->show(view->child);
1153 if (err)
1154 goto done;
1156 update_panels();
1157 doupdate();
1160 done:
1161 while (!TAILQ_EMPTY(&views)) {
1162 view = TAILQ_FIRST(&views);
1163 TAILQ_REMOVE(&views, view, entry);
1164 view_close(view);
1167 errcode = pthread_mutex_unlock(&tog_mutex);
1168 if (errcode && err == NULL)
1169 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1171 return err;
1174 __dead static void
1175 usage_log(void)
1177 endwin();
1178 fprintf(stderr,
1179 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1180 getprogname());
1181 exit(1);
1184 /* Create newly allocated wide-character string equivalent to a byte string. */
1185 static const struct got_error *
1186 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1188 char *vis = NULL;
1189 const struct got_error *err = NULL;
1191 *ws = NULL;
1192 *wlen = mbstowcs(NULL, s, 0);
1193 if (*wlen == (size_t)-1) {
1194 int vislen;
1195 if (errno != EILSEQ)
1196 return got_error_from_errno("mbstowcs");
1198 /* byte string invalid in current encoding; try to "fix" it */
1199 err = got_mbsavis(&vis, &vislen, s);
1200 if (err)
1201 return err;
1202 *wlen = mbstowcs(NULL, vis, 0);
1203 if (*wlen == (size_t)-1) {
1204 err = got_error_from_errno("mbstowcs"); /* give up */
1205 goto done;
1209 *ws = calloc(*wlen + 1, sizeof(**ws));
1210 if (*ws == NULL) {
1211 err = got_error_from_errno("calloc");
1212 goto done;
1215 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1216 err = got_error_from_errno("mbstowcs");
1217 done:
1218 free(vis);
1219 if (err) {
1220 free(*ws);
1221 *ws = NULL;
1222 *wlen = 0;
1224 return err;
1227 static const struct got_error *
1228 expand_tab(char **ptr, const char *src)
1230 char *dst;
1231 size_t len, n, idx = 0, sz = 0;
1233 *ptr = NULL;
1234 n = len = strlen(src);
1235 dst = malloc(n + 1);
1236 if (dst == NULL)
1237 return got_error_from_errno("malloc");
1239 while (idx < len && src[idx]) {
1240 const char c = src[idx];
1242 if (c == '\t') {
1243 size_t nb = TABSIZE - sz % TABSIZE;
1244 char *p;
1246 p = realloc(dst, n + nb);
1247 if (p == NULL) {
1248 free(dst);
1249 return got_error_from_errno("realloc");
1252 dst = p;
1253 n += nb;
1254 memset(dst + sz, ' ', nb);
1255 sz += nb;
1256 } else
1257 dst[sz++] = src[idx];
1258 ++idx;
1261 dst[sz] = '\0';
1262 *ptr = dst;
1263 return NULL;
1267 * Skip leading nscroll columns of a wide character string.
1268 * Returns the index to the first character of the scrolled string.
1270 static const struct got_error *
1271 scroll_wline(int *scrollx , wchar_t *wline, int nscroll,
1272 int col_tab_align)
1274 int cols = 0;
1275 size_t wlen = wcslen(wline);
1276 int i = 0;
1278 *scrollx = 0;
1280 while (i < wlen && cols < nscroll) {
1281 int width = wcwidth(wline[i]);
1283 if (width == 0) {
1284 i++;
1285 continue;
1288 if (width == 1 || width == 2) {
1289 if (cols + width > nscroll)
1290 break;
1291 cols += width;
1292 i++;
1293 } else if (width == -1) {
1294 if (wline[i] == L'\t') {
1295 width = TABSIZE -
1296 ((cols + col_tab_align) % TABSIZE);
1297 } else {
1298 width = 1;
1299 wline[i] = L'.';
1301 if (cols + width > nscroll)
1302 break;
1303 cols += width;
1304 i++;
1305 } else
1306 return got_error_from_errno("wcwidth");
1309 *scrollx = i;
1310 return NULL;
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 = 0;
1324 wchar_t *wline = NULL;
1325 char *exstr = NULL;
1326 size_t wlen;
1327 int i, scrollx = 0;
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 err = scroll_wline(&scrollx, wline, nscroll, col_tab_align);
1344 if (err)
1345 goto done;
1347 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1348 wline[wlen - 1] = L'\0';
1349 wlen--;
1351 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1352 wline[wlen - 1] = L'\0';
1353 wlen--;
1356 i = scrollx;
1357 while (i < wlen) {
1358 int width = wcwidth(wline[i]);
1360 if (width == 0) {
1361 i++;
1362 continue;
1365 if (width == 1 || width == 2) {
1366 if (cols + width > wlimit)
1367 break;
1368 cols += width;
1369 i++;
1370 } else if (width == -1) {
1371 if (wline[i] == L'\t') {
1372 width = TABSIZE -
1373 ((cols + col_tab_align) % TABSIZE);
1374 } else {
1375 width = 1;
1376 wline[i] = L'.';
1378 if (cols + width > wlimit)
1379 break;
1380 cols += width;
1381 i++;
1382 } else {
1383 err = got_error_from_errno("wcwidth");
1384 goto done;
1387 wline[i] = L'\0';
1388 if (widthp)
1389 *widthp = cols;
1390 if (scrollxp)
1391 *scrollxp = scrollx;
1392 done:
1393 if (err)
1394 free(wline);
1395 else
1396 *wlinep = wline;
1397 return err;
1400 static const struct got_error*
1401 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1402 struct got_object_id *id, struct got_repository *repo)
1404 static const struct got_error *err = NULL;
1405 struct got_reflist_entry *re;
1406 char *s;
1407 const char *name;
1409 *refs_str = NULL;
1411 TAILQ_FOREACH(re, refs, entry) {
1412 struct got_tag_object *tag = NULL;
1413 struct got_object_id *ref_id;
1414 int cmp;
1416 name = got_ref_get_name(re->ref);
1417 if (strcmp(name, GOT_REF_HEAD) == 0)
1418 continue;
1419 if (strncmp(name, "refs/", 5) == 0)
1420 name += 5;
1421 if (strncmp(name, "got/", 4) == 0 &&
1422 strncmp(name, "got/backup/", 11) != 0)
1423 continue;
1424 if (strncmp(name, "heads/", 6) == 0)
1425 name += 6;
1426 if (strncmp(name, "remotes/", 8) == 0) {
1427 name += 8;
1428 s = strstr(name, "/" GOT_REF_HEAD);
1429 if (s != NULL && s[strlen(s)] == '\0')
1430 continue;
1432 err = got_ref_resolve(&ref_id, repo, re->ref);
1433 if (err)
1434 break;
1435 if (strncmp(name, "tags/", 5) == 0) {
1436 err = got_object_open_as_tag(&tag, repo, ref_id);
1437 if (err) {
1438 if (err->code != GOT_ERR_OBJ_TYPE) {
1439 free(ref_id);
1440 break;
1442 /* Ref points at something other than a tag. */
1443 err = NULL;
1444 tag = NULL;
1447 cmp = got_object_id_cmp(tag ?
1448 got_object_tag_get_object_id(tag) : ref_id, id);
1449 free(ref_id);
1450 if (tag)
1451 got_object_tag_close(tag);
1452 if (cmp != 0)
1453 continue;
1454 s = *refs_str;
1455 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1456 s ? ", " : "", name) == -1) {
1457 err = got_error_from_errno("asprintf");
1458 free(s);
1459 *refs_str = NULL;
1460 break;
1462 free(s);
1465 return err;
1468 static const struct got_error *
1469 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1470 int col_tab_align)
1472 char *smallerthan;
1474 smallerthan = strchr(author, '<');
1475 if (smallerthan && smallerthan[1] != '\0')
1476 author = smallerthan + 1;
1477 author[strcspn(author, "@>")] = '\0';
1478 return format_line(wauthor, author_width, NULL, author, 0, limit,
1479 col_tab_align, 0);
1482 static const struct got_error *
1483 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1484 struct got_object_id *id, const size_t date_display_cols,
1485 int author_display_cols)
1487 struct tog_log_view_state *s = &view->state.log;
1488 const struct got_error *err = NULL;
1489 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1490 char *logmsg0 = NULL, *logmsg = NULL;
1491 char *author = NULL;
1492 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1493 int author_width, logmsg_width;
1494 char *newline, *line = NULL;
1495 int col, limit, scrollx;
1496 const int avail = view->ncols;
1497 struct tm tm;
1498 time_t committer_time;
1499 struct tog_color *tc;
1501 committer_time = got_object_commit_get_committer_time(commit);
1502 if (gmtime_r(&committer_time, &tm) == NULL)
1503 return got_error_from_errno("gmtime_r");
1504 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1505 return got_error(GOT_ERR_NO_SPACE);
1507 if (avail <= date_display_cols)
1508 limit = MIN(sizeof(datebuf) - 1, avail);
1509 else
1510 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1511 tc = get_color(&s->colors, TOG_COLOR_DATE);
1512 if (tc)
1513 wattr_on(view->window,
1514 COLOR_PAIR(tc->colorpair), NULL);
1515 waddnstr(view->window, datebuf, limit);
1516 if (tc)
1517 wattr_off(view->window,
1518 COLOR_PAIR(tc->colorpair), NULL);
1519 col = limit;
1520 if (col > avail)
1521 goto done;
1523 if (avail >= 120) {
1524 char *id_str;
1525 err = got_object_id_str(&id_str, id);
1526 if (err)
1527 goto done;
1528 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1529 if (tc)
1530 wattr_on(view->window,
1531 COLOR_PAIR(tc->colorpair), NULL);
1532 wprintw(view->window, "%.8s ", id_str);
1533 if (tc)
1534 wattr_off(view->window,
1535 COLOR_PAIR(tc->colorpair), NULL);
1536 free(id_str);
1537 col += 9;
1538 if (col > avail)
1539 goto done;
1542 author = strdup(got_object_commit_get_author(commit));
1543 if (author == NULL) {
1544 err = got_error_from_errno("strdup");
1545 goto done;
1547 err = format_author(&wauthor, &author_width, author, avail - col, col);
1548 if (err)
1549 goto done;
1550 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1551 if (tc)
1552 wattr_on(view->window,
1553 COLOR_PAIR(tc->colorpair), NULL);
1554 waddwstr(view->window, wauthor);
1555 if (tc)
1556 wattr_off(view->window,
1557 COLOR_PAIR(tc->colorpair), NULL);
1558 col += author_width;
1559 while (col < avail && author_width < author_display_cols + 2) {
1560 waddch(view->window, ' ');
1561 col++;
1562 author_width++;
1564 if (col > avail)
1565 goto done;
1567 err = got_object_commit_get_logmsg(&logmsg0, commit);
1568 if (err)
1569 goto done;
1570 logmsg = logmsg0;
1571 while (*logmsg == '\n')
1572 logmsg++;
1573 newline = strchr(logmsg, '\n');
1574 if (newline)
1575 *newline = '\0';
1576 limit = avail - col;
1577 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1578 limit, col, 1);
1579 if (err)
1580 goto done;
1581 waddwstr(view->window, &wlogmsg[scrollx]);
1582 col += MAX(logmsg_width, 0);
1583 while (col < avail) {
1584 waddch(view->window, ' ');
1585 col++;
1587 done:
1588 free(logmsg0);
1589 free(wlogmsg);
1590 free(author);
1591 free(wauthor);
1592 free(line);
1593 return err;
1596 static struct commit_queue_entry *
1597 alloc_commit_queue_entry(struct got_commit_object *commit,
1598 struct got_object_id *id)
1600 struct commit_queue_entry *entry;
1602 entry = calloc(1, sizeof(*entry));
1603 if (entry == NULL)
1604 return NULL;
1606 entry->id = id;
1607 entry->commit = commit;
1608 return entry;
1611 static void
1612 pop_commit(struct commit_queue *commits)
1614 struct commit_queue_entry *entry;
1616 entry = TAILQ_FIRST(&commits->head);
1617 TAILQ_REMOVE(&commits->head, entry, entry);
1618 got_object_commit_close(entry->commit);
1619 commits->ncommits--;
1620 /* Don't free entry->id! It is owned by the commit graph. */
1621 free(entry);
1624 static void
1625 free_commits(struct commit_queue *commits)
1627 while (!TAILQ_EMPTY(&commits->head))
1628 pop_commit(commits);
1631 static const struct got_error *
1632 match_commit(int *have_match, struct got_object_id *id,
1633 struct got_commit_object *commit, regex_t *regex)
1635 const struct got_error *err = NULL;
1636 regmatch_t regmatch;
1637 char *id_str = NULL, *logmsg = NULL;
1639 *have_match = 0;
1641 err = got_object_id_str(&id_str, id);
1642 if (err)
1643 return err;
1645 err = got_object_commit_get_logmsg(&logmsg, commit);
1646 if (err)
1647 goto done;
1649 if (regexec(regex, got_object_commit_get_author(commit), 1,
1650 &regmatch, 0) == 0 ||
1651 regexec(regex, got_object_commit_get_committer(commit), 1,
1652 &regmatch, 0) == 0 ||
1653 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1654 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1655 *have_match = 1;
1656 done:
1657 free(id_str);
1658 free(logmsg);
1659 return err;
1662 static const struct got_error *
1663 queue_commits(struct tog_log_thread_args *a)
1665 const struct got_error *err = NULL;
1668 * We keep all commits open throughout the lifetime of the log
1669 * view in order to avoid having to re-fetch commits from disk
1670 * while updating the display.
1672 do {
1673 struct got_object_id *id;
1674 struct got_commit_object *commit;
1675 struct commit_queue_entry *entry;
1676 int errcode;
1678 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1679 NULL, NULL);
1680 if (err || id == NULL)
1681 break;
1683 err = got_object_open_as_commit(&commit, a->repo, id);
1684 if (err)
1685 break;
1686 entry = alloc_commit_queue_entry(commit, id);
1687 if (entry == NULL) {
1688 err = got_error_from_errno("alloc_commit_queue_entry");
1689 break;
1692 errcode = pthread_mutex_lock(&tog_mutex);
1693 if (errcode) {
1694 err = got_error_set_errno(errcode,
1695 "pthread_mutex_lock");
1696 break;
1699 entry->idx = a->commits->ncommits;
1700 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1701 a->commits->ncommits++;
1703 if (*a->searching == TOG_SEARCH_FORWARD &&
1704 !*a->search_next_done) {
1705 int have_match;
1706 err = match_commit(&have_match, id, commit, a->regex);
1707 if (err)
1708 break;
1709 if (have_match)
1710 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1713 errcode = pthread_mutex_unlock(&tog_mutex);
1714 if (errcode && err == NULL)
1715 err = got_error_set_errno(errcode,
1716 "pthread_mutex_unlock");
1717 if (err)
1718 break;
1719 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1721 return err;
1724 static void
1725 select_commit(struct tog_log_view_state *s)
1727 struct commit_queue_entry *entry;
1728 int ncommits = 0;
1730 entry = s->first_displayed_entry;
1731 while (entry) {
1732 if (ncommits == s->selected) {
1733 s->selected_entry = entry;
1734 break;
1736 entry = TAILQ_NEXT(entry, entry);
1737 ncommits++;
1741 static const struct got_error *
1742 draw_commits(struct tog_view *view)
1744 const struct got_error *err = NULL;
1745 struct tog_log_view_state *s = &view->state.log;
1746 struct commit_queue_entry *entry = s->selected_entry;
1747 const int limit = view->nlines;
1748 int width;
1749 int ncommits, author_cols = 4;
1750 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1751 char *refs_str = NULL;
1752 wchar_t *wline;
1753 struct tog_color *tc;
1754 static const size_t date_display_cols = 12;
1756 if (s->selected_entry &&
1757 !(view->searching && view->search_next_done == 0)) {
1758 struct got_reflist_head *refs;
1759 err = got_object_id_str(&id_str, s->selected_entry->id);
1760 if (err)
1761 return err;
1762 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1763 s->selected_entry->id);
1764 if (refs) {
1765 err = build_refs_str(&refs_str, refs,
1766 s->selected_entry->id, s->repo);
1767 if (err)
1768 goto done;
1772 if (s->thread_args.commits_needed == 0)
1773 halfdelay(10); /* disable fast refresh */
1775 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1776 if (asprintf(&ncommits_str, " [%d/%d] %s",
1777 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1778 (view->searching && !view->search_next_done) ?
1779 "searching..." : "loading...") == -1) {
1780 err = got_error_from_errno("asprintf");
1781 goto done;
1783 } else {
1784 const char *search_str = NULL;
1786 if (view->searching) {
1787 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1788 search_str = "no more matches";
1789 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1790 search_str = "no matches found";
1791 else if (!view->search_next_done)
1792 search_str = "searching...";
1795 if (asprintf(&ncommits_str, " [%d/%d] %s",
1796 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1797 search_str ? search_str :
1798 (refs_str ? refs_str : "")) == -1) {
1799 err = got_error_from_errno("asprintf");
1800 goto done;
1804 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1805 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1806 "........................................",
1807 s->in_repo_path, ncommits_str) == -1) {
1808 err = got_error_from_errno("asprintf");
1809 header = NULL;
1810 goto done;
1812 } else if (asprintf(&header, "commit %s%s",
1813 id_str ? id_str : "........................................",
1814 ncommits_str) == -1) {
1815 err = got_error_from_errno("asprintf");
1816 header = NULL;
1817 goto done;
1819 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1820 if (err)
1821 goto done;
1823 werase(view->window);
1825 if (view_needs_focus_indication(view))
1826 wstandout(view->window);
1827 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1828 if (tc)
1829 wattr_on(view->window,
1830 COLOR_PAIR(tc->colorpair), NULL);
1831 waddwstr(view->window, wline);
1832 if (tc)
1833 wattr_off(view->window,
1834 COLOR_PAIR(tc->colorpair), NULL);
1835 while (width < view->ncols) {
1836 waddch(view->window, ' ');
1837 width++;
1839 if (view_needs_focus_indication(view))
1840 wstandend(view->window);
1841 free(wline);
1842 if (limit <= 1)
1843 goto done;
1845 /* Grow author column size if necessary, and set view->maxx. */
1846 entry = s->first_displayed_entry;
1847 ncommits = 0;
1848 view->maxx = 0;
1849 while (entry) {
1850 char *author, *eol, *msg, *msg0;
1851 wchar_t *wauthor, *wmsg;
1852 int width;
1853 if (ncommits >= limit - 1)
1854 break;
1855 author = strdup(got_object_commit_get_author(entry->commit));
1856 if (author == NULL) {
1857 err = got_error_from_errno("strdup");
1858 goto done;
1860 err = format_author(&wauthor, &width, author, COLS,
1861 date_display_cols);
1862 if (author_cols < width)
1863 author_cols = width;
1864 free(wauthor);
1865 free(author);
1866 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1867 if (err)
1868 goto done;
1869 msg = msg0;
1870 while (*msg == '\n')
1871 ++msg;
1872 if ((eol = strchr(msg, '\n')))
1873 *eol = '\0';
1874 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1875 date_display_cols + author_cols, 0);
1876 if (err)
1877 goto done;
1878 view->maxx = MAX(view->maxx, width);
1879 free(msg0);
1880 free(wmsg);
1881 ncommits++;
1882 entry = TAILQ_NEXT(entry, entry);
1885 entry = s->first_displayed_entry;
1886 s->last_displayed_entry = s->first_displayed_entry;
1887 ncommits = 0;
1888 while (entry) {
1889 if (ncommits >= limit - 1)
1890 break;
1891 if (ncommits == s->selected)
1892 wstandout(view->window);
1893 err = draw_commit(view, entry->commit, entry->id,
1894 date_display_cols, author_cols);
1895 if (ncommits == s->selected)
1896 wstandend(view->window);
1897 if (err)
1898 goto done;
1899 ncommits++;
1900 s->last_displayed_entry = entry;
1901 entry = TAILQ_NEXT(entry, entry);
1904 view_vborder(view);
1905 done:
1906 free(id_str);
1907 free(refs_str);
1908 free(ncommits_str);
1909 free(header);
1910 return err;
1913 static void
1914 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1916 struct commit_queue_entry *entry;
1917 int nscrolled = 0;
1919 entry = TAILQ_FIRST(&s->commits.head);
1920 if (s->first_displayed_entry == entry)
1921 return;
1923 entry = s->first_displayed_entry;
1924 while (entry && nscrolled < maxscroll) {
1925 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1926 if (entry) {
1927 s->first_displayed_entry = entry;
1928 nscrolled++;
1933 static const struct got_error *
1934 trigger_log_thread(struct tog_view *view, int wait)
1936 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1937 int errcode;
1939 halfdelay(1); /* fast refresh while loading commits */
1941 while (ta->commits_needed > 0 || ta->load_all) {
1942 if (ta->log_complete)
1943 break;
1945 /* Wake the log thread. */
1946 errcode = pthread_cond_signal(&ta->need_commits);
1947 if (errcode)
1948 return got_error_set_errno(errcode,
1949 "pthread_cond_signal");
1952 * The mutex will be released while the view loop waits
1953 * in wgetch(), at which time the log thread will run.
1955 if (!wait)
1956 break;
1958 /* Display progress update in log view. */
1959 show_log_view(view);
1960 update_panels();
1961 doupdate();
1963 /* Wait right here while next commit is being loaded. */
1964 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1965 if (errcode)
1966 return got_error_set_errno(errcode,
1967 "pthread_cond_wait");
1969 /* Display progress update in log view. */
1970 show_log_view(view);
1971 update_panels();
1972 doupdate();
1975 return NULL;
1978 static const struct got_error *
1979 log_scroll_down(struct tog_view *view, int maxscroll)
1981 struct tog_log_view_state *s = &view->state.log;
1982 const struct got_error *err = NULL;
1983 struct commit_queue_entry *pentry;
1984 int nscrolled = 0, ncommits_needed;
1986 if (s->last_displayed_entry == NULL)
1987 return NULL;
1989 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1990 if (s->commits.ncommits < ncommits_needed &&
1991 !s->thread_args.log_complete) {
1993 * Ask the log thread for required amount of commits.
1995 s->thread_args.commits_needed += maxscroll;
1996 err = trigger_log_thread(view, 1);
1997 if (err)
1998 return err;
2001 do {
2002 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2003 if (pentry == NULL)
2004 break;
2006 s->last_displayed_entry = pentry;
2008 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2009 if (pentry == NULL)
2010 break;
2011 s->first_displayed_entry = pentry;
2012 } while (++nscrolled < maxscroll);
2014 return err;
2017 static const struct got_error *
2018 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2019 struct got_commit_object *commit, struct got_object_id *commit_id,
2020 struct tog_view *log_view, struct got_repository *repo)
2022 const struct got_error *err;
2023 struct got_object_qid *parent_id;
2024 struct tog_view *diff_view;
2026 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2027 if (diff_view == NULL)
2028 return got_error_from_errno("view_open");
2030 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2031 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2032 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2033 if (err == NULL)
2034 *new_view = diff_view;
2035 return err;
2038 static const struct got_error *
2039 tree_view_visit_subtree(struct tog_tree_view_state *s,
2040 struct got_tree_object *subtree)
2042 struct tog_parent_tree *parent;
2044 parent = calloc(1, sizeof(*parent));
2045 if (parent == NULL)
2046 return got_error_from_errno("calloc");
2048 parent->tree = s->tree;
2049 parent->first_displayed_entry = s->first_displayed_entry;
2050 parent->selected_entry = s->selected_entry;
2051 parent->selected = s->selected;
2052 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2053 s->tree = subtree;
2054 s->selected = 0;
2055 s->first_displayed_entry = NULL;
2056 return NULL;
2059 static const struct got_error *
2060 tree_view_walk_path(struct tog_tree_view_state *s,
2061 struct got_commit_object *commit, const char *path)
2063 const struct got_error *err = NULL;
2064 struct got_tree_object *tree = NULL;
2065 const char *p;
2066 char *slash, *subpath = NULL;
2068 /* Walk the path and open corresponding tree objects. */
2069 p = path;
2070 while (*p) {
2071 struct got_tree_entry *te;
2072 struct got_object_id *tree_id;
2073 char *te_name;
2075 while (p[0] == '/')
2076 p++;
2078 /* Ensure the correct subtree entry is selected. */
2079 slash = strchr(p, '/');
2080 if (slash == NULL)
2081 te_name = strdup(p);
2082 else
2083 te_name = strndup(p, slash - p);
2084 if (te_name == NULL) {
2085 err = got_error_from_errno("strndup");
2086 break;
2088 te = got_object_tree_find_entry(s->tree, te_name);
2089 if (te == NULL) {
2090 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2091 free(te_name);
2092 break;
2094 free(te_name);
2095 s->first_displayed_entry = s->selected_entry = te;
2097 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2098 break; /* jump to this file's entry */
2100 slash = strchr(p, '/');
2101 if (slash)
2102 subpath = strndup(path, slash - path);
2103 else
2104 subpath = strdup(path);
2105 if (subpath == NULL) {
2106 err = got_error_from_errno("strdup");
2107 break;
2110 err = got_object_id_by_path(&tree_id, s->repo, commit,
2111 subpath);
2112 if (err)
2113 break;
2115 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2116 free(tree_id);
2117 if (err)
2118 break;
2120 err = tree_view_visit_subtree(s, tree);
2121 if (err) {
2122 got_object_tree_close(tree);
2123 break;
2125 if (slash == NULL)
2126 break;
2127 free(subpath);
2128 subpath = NULL;
2129 p = slash;
2132 free(subpath);
2133 return err;
2136 static const struct got_error *
2137 browse_commit_tree(struct tog_view **new_view, int begin_x,
2138 struct commit_queue_entry *entry, const char *path,
2139 const char *head_ref_name, struct got_repository *repo)
2141 const struct got_error *err = NULL;
2142 struct tog_tree_view_state *s;
2143 struct tog_view *tree_view;
2145 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2146 if (tree_view == NULL)
2147 return got_error_from_errno("view_open");
2149 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2150 if (err)
2151 return err;
2152 s = &tree_view->state.tree;
2154 *new_view = tree_view;
2156 if (got_path_is_root_dir(path))
2157 return NULL;
2159 return tree_view_walk_path(s, entry->commit, path);
2162 static const struct got_error *
2163 block_signals_used_by_main_thread(void)
2165 sigset_t sigset;
2166 int errcode;
2168 if (sigemptyset(&sigset) == -1)
2169 return got_error_from_errno("sigemptyset");
2171 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2172 if (sigaddset(&sigset, SIGWINCH) == -1)
2173 return got_error_from_errno("sigaddset");
2174 if (sigaddset(&sigset, SIGCONT) == -1)
2175 return got_error_from_errno("sigaddset");
2176 if (sigaddset(&sigset, SIGINT) == -1)
2177 return got_error_from_errno("sigaddset");
2178 if (sigaddset(&sigset, SIGTERM) == -1)
2179 return got_error_from_errno("sigaddset");
2181 /* ncurses handles SIGTSTP */
2182 if (sigaddset(&sigset, SIGTSTP) == -1)
2183 return got_error_from_errno("sigaddset");
2185 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2186 if (errcode)
2187 return got_error_set_errno(errcode, "pthread_sigmask");
2189 return NULL;
2192 static void *
2193 log_thread(void *arg)
2195 const struct got_error *err = NULL;
2196 int errcode = 0;
2197 struct tog_log_thread_args *a = arg;
2198 int done = 0;
2200 err = block_signals_used_by_main_thread();
2201 if (err)
2202 return (void *)err;
2204 while (!done && !err && !tog_fatal_signal_received()) {
2205 err = queue_commits(a);
2206 if (err) {
2207 if (err->code != GOT_ERR_ITER_COMPLETED)
2208 return (void *)err;
2209 err = NULL;
2210 done = 1;
2211 } else if (a->commits_needed > 0 && !a->load_all)
2212 a->commits_needed--;
2214 errcode = pthread_mutex_lock(&tog_mutex);
2215 if (errcode) {
2216 err = got_error_set_errno(errcode,
2217 "pthread_mutex_lock");
2218 break;
2219 } else if (*a->quit)
2220 done = 1;
2221 else if (*a->first_displayed_entry == NULL) {
2222 *a->first_displayed_entry =
2223 TAILQ_FIRST(&a->commits->head);
2224 *a->selected_entry = *a->first_displayed_entry;
2227 errcode = pthread_cond_signal(&a->commit_loaded);
2228 if (errcode) {
2229 err = got_error_set_errno(errcode,
2230 "pthread_cond_signal");
2231 pthread_mutex_unlock(&tog_mutex);
2232 break;
2235 if (done)
2236 a->commits_needed = 0;
2237 else {
2238 if (a->commits_needed == 0 && !a->load_all) {
2239 errcode = pthread_cond_wait(&a->need_commits,
2240 &tog_mutex);
2241 if (errcode)
2242 err = got_error_set_errno(errcode,
2243 "pthread_cond_wait");
2244 if (*a->quit)
2245 done = 1;
2249 errcode = pthread_mutex_unlock(&tog_mutex);
2250 if (errcode && err == NULL)
2251 err = got_error_set_errno(errcode,
2252 "pthread_mutex_unlock");
2254 a->log_complete = 1;
2255 return (void *)err;
2258 static const struct got_error *
2259 stop_log_thread(struct tog_log_view_state *s)
2261 const struct got_error *err = NULL;
2262 int errcode;
2264 if (s->thread) {
2265 s->quit = 1;
2266 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2267 if (errcode)
2268 return got_error_set_errno(errcode,
2269 "pthread_cond_signal");
2270 errcode = pthread_mutex_unlock(&tog_mutex);
2271 if (errcode)
2272 return got_error_set_errno(errcode,
2273 "pthread_mutex_unlock");
2274 errcode = pthread_join(s->thread, (void **)&err);
2275 if (errcode)
2276 return got_error_set_errno(errcode, "pthread_join");
2277 errcode = pthread_mutex_lock(&tog_mutex);
2278 if (errcode)
2279 return got_error_set_errno(errcode,
2280 "pthread_mutex_lock");
2281 s->thread = NULL;
2284 if (s->thread_args.repo) {
2285 err = got_repo_close(s->thread_args.repo);
2286 s->thread_args.repo = NULL;
2289 if (s->thread_args.pack_fds) {
2290 const struct got_error *pack_err =
2291 got_repo_pack_fds_close(s->thread_args.pack_fds);
2292 if (err == NULL)
2293 err = pack_err;
2294 s->thread_args.pack_fds = NULL;
2297 if (s->thread_args.graph) {
2298 got_commit_graph_close(s->thread_args.graph);
2299 s->thread_args.graph = NULL;
2302 return err;
2305 static const struct got_error *
2306 close_log_view(struct tog_view *view)
2308 const struct got_error *err = NULL;
2309 struct tog_log_view_state *s = &view->state.log;
2310 int errcode;
2312 err = stop_log_thread(s);
2314 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2315 if (errcode && err == NULL)
2316 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2318 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2319 if (errcode && err == NULL)
2320 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2322 free_commits(&s->commits);
2323 free(s->in_repo_path);
2324 s->in_repo_path = NULL;
2325 free(s->start_id);
2326 s->start_id = NULL;
2327 free(s->head_ref_name);
2328 s->head_ref_name = NULL;
2329 return err;
2332 static const struct got_error *
2333 search_start_log_view(struct tog_view *view)
2335 struct tog_log_view_state *s = &view->state.log;
2337 s->matched_entry = NULL;
2338 s->search_entry = NULL;
2339 return NULL;
2342 static const struct got_error *
2343 search_next_log_view(struct tog_view *view)
2345 const struct got_error *err = NULL;
2346 struct tog_log_view_state *s = &view->state.log;
2347 struct commit_queue_entry *entry;
2349 /* Display progress update in log view. */
2350 show_log_view(view);
2351 update_panels();
2352 doupdate();
2354 if (s->search_entry) {
2355 int errcode, ch;
2356 errcode = pthread_mutex_unlock(&tog_mutex);
2357 if (errcode)
2358 return got_error_set_errno(errcode,
2359 "pthread_mutex_unlock");
2360 ch = wgetch(view->window);
2361 errcode = pthread_mutex_lock(&tog_mutex);
2362 if (errcode)
2363 return got_error_set_errno(errcode,
2364 "pthread_mutex_lock");
2365 if (ch == KEY_BACKSPACE) {
2366 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2367 return NULL;
2369 if (view->searching == TOG_SEARCH_FORWARD)
2370 entry = TAILQ_NEXT(s->search_entry, entry);
2371 else
2372 entry = TAILQ_PREV(s->search_entry,
2373 commit_queue_head, entry);
2374 } else if (s->matched_entry) {
2375 if (view->searching == TOG_SEARCH_FORWARD)
2376 entry = TAILQ_NEXT(s->matched_entry, entry);
2377 else
2378 entry = TAILQ_PREV(s->matched_entry,
2379 commit_queue_head, entry);
2380 } else {
2381 entry = s->selected_entry;
2384 while (1) {
2385 int have_match = 0;
2387 if (entry == NULL) {
2388 if (s->thread_args.log_complete ||
2389 view->searching == TOG_SEARCH_BACKWARD) {
2390 view->search_next_done =
2391 (s->matched_entry == NULL ?
2392 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2393 s->search_entry = NULL;
2394 return NULL;
2397 * Poke the log thread for more commits and return,
2398 * allowing the main loop to make progress. Search
2399 * will resume at s->search_entry once we come back.
2401 s->thread_args.commits_needed++;
2402 return trigger_log_thread(view, 0);
2405 err = match_commit(&have_match, entry->id, entry->commit,
2406 &view->regex);
2407 if (err)
2408 break;
2409 if (have_match) {
2410 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2411 s->matched_entry = entry;
2412 break;
2415 s->search_entry = entry;
2416 if (view->searching == TOG_SEARCH_FORWARD)
2417 entry = TAILQ_NEXT(entry, entry);
2418 else
2419 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2422 if (s->matched_entry) {
2423 int cur = s->selected_entry->idx;
2424 while (cur < s->matched_entry->idx) {
2425 err = input_log_view(NULL, view, KEY_DOWN);
2426 if (err)
2427 return err;
2428 cur++;
2430 while (cur > s->matched_entry->idx) {
2431 err = input_log_view(NULL, view, KEY_UP);
2432 if (err)
2433 return err;
2434 cur--;
2438 s->search_entry = NULL;
2440 return NULL;
2443 static const struct got_error *
2444 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2445 struct got_repository *repo, const char *head_ref_name,
2446 const char *in_repo_path, int log_branches)
2448 const struct got_error *err = NULL;
2449 struct tog_log_view_state *s = &view->state.log;
2450 struct got_repository *thread_repo = NULL;
2451 struct got_commit_graph *thread_graph = NULL;
2452 int errcode;
2454 if (in_repo_path != s->in_repo_path) {
2455 free(s->in_repo_path);
2456 s->in_repo_path = strdup(in_repo_path);
2457 if (s->in_repo_path == NULL)
2458 return got_error_from_errno("strdup");
2461 /* The commit queue only contains commits being displayed. */
2462 TAILQ_INIT(&s->commits.head);
2463 s->commits.ncommits = 0;
2465 s->repo = repo;
2466 if (head_ref_name) {
2467 s->head_ref_name = strdup(head_ref_name);
2468 if (s->head_ref_name == NULL) {
2469 err = got_error_from_errno("strdup");
2470 goto done;
2473 s->start_id = got_object_id_dup(start_id);
2474 if (s->start_id == NULL) {
2475 err = got_error_from_errno("got_object_id_dup");
2476 goto done;
2478 s->log_branches = log_branches;
2480 STAILQ_INIT(&s->colors);
2481 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2482 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2483 get_color_value("TOG_COLOR_COMMIT"));
2484 if (err)
2485 goto done;
2486 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2487 get_color_value("TOG_COLOR_AUTHOR"));
2488 if (err) {
2489 free_colors(&s->colors);
2490 goto done;
2492 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2493 get_color_value("TOG_COLOR_DATE"));
2494 if (err) {
2495 free_colors(&s->colors);
2496 goto done;
2500 view->show = show_log_view;
2501 view->input = input_log_view;
2502 view->close = close_log_view;
2503 view->search_start = search_start_log_view;
2504 view->search_next = search_next_log_view;
2506 if (s->thread_args.pack_fds == NULL) {
2507 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2508 if (err)
2509 goto done;
2511 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2512 s->thread_args.pack_fds);
2513 if (err)
2514 goto done;
2515 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2516 !s->log_branches);
2517 if (err)
2518 goto done;
2519 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2520 s->repo, NULL, NULL);
2521 if (err)
2522 goto done;
2524 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2525 if (errcode) {
2526 err = got_error_set_errno(errcode, "pthread_cond_init");
2527 goto done;
2529 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2530 if (errcode) {
2531 err = got_error_set_errno(errcode, "pthread_cond_init");
2532 goto done;
2535 s->thread_args.commits_needed = view->nlines;
2536 s->thread_args.graph = thread_graph;
2537 s->thread_args.commits = &s->commits;
2538 s->thread_args.in_repo_path = s->in_repo_path;
2539 s->thread_args.start_id = s->start_id;
2540 s->thread_args.repo = thread_repo;
2541 s->thread_args.log_complete = 0;
2542 s->thread_args.quit = &s->quit;
2543 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2544 s->thread_args.selected_entry = &s->selected_entry;
2545 s->thread_args.searching = &view->searching;
2546 s->thread_args.search_next_done = &view->search_next_done;
2547 s->thread_args.regex = &view->regex;
2548 done:
2549 if (err)
2550 close_log_view(view);
2551 return err;
2554 static const struct got_error *
2555 show_log_view(struct tog_view *view)
2557 const struct got_error *err;
2558 struct tog_log_view_state *s = &view->state.log;
2560 if (s->thread == NULL) {
2561 int errcode = pthread_create(&s->thread, NULL, log_thread,
2562 &s->thread_args);
2563 if (errcode)
2564 return got_error_set_errno(errcode, "pthread_create");
2565 if (s->thread_args.commits_needed > 0) {
2566 err = trigger_log_thread(view, 1);
2567 if (err)
2568 return err;
2572 return draw_commits(view);
2575 static const struct got_error *
2576 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2578 const struct got_error *err = NULL;
2579 struct tog_log_view_state *s = &view->state.log;
2580 struct tog_view *diff_view = NULL, *tree_view = NULL;
2581 struct tog_view *ref_view = NULL;
2582 struct commit_queue_entry *entry;
2583 int begin_x = 0, n, nscroll = view->nlines - 1;
2585 if (s->thread_args.load_all) {
2586 if (ch == KEY_BACKSPACE)
2587 s->thread_args.load_all = 0;
2588 else if (s->thread_args.log_complete) {
2589 s->thread_args.load_all = 0;
2590 log_scroll_down(view, s->commits.ncommits);
2591 s->selected = MIN(view->nlines - 2,
2592 s->commits.ncommits - 1);
2593 select_commit(s);
2595 return NULL;
2598 switch (ch) {
2599 case 'q':
2600 s->quit = 1;
2601 break;
2602 case '0':
2603 view->x = 0;
2604 break;
2605 case '$':
2606 view->x = MAX(view->maxx - view->ncols / 2, 0);
2607 break;
2608 case KEY_RIGHT:
2609 case 'l':
2610 if (view->x + view->ncols / 2 < view->maxx)
2611 view->x += 2; /* move two columns right */
2612 break;
2613 case KEY_LEFT:
2614 case 'h':
2615 view->x -= MIN(view->x, 2); /* move two columns back */
2616 break;
2617 case 'k':
2618 case KEY_UP:
2619 case '<':
2620 case ',':
2621 case CTRL('p'):
2622 if (s->first_displayed_entry == NULL)
2623 break;
2624 if (s->selected > 0)
2625 s->selected--;
2626 else
2627 log_scroll_up(s, 1);
2628 select_commit(s);
2629 break;
2630 case 'g':
2631 case KEY_HOME:
2632 s->selected = 0;
2633 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2634 select_commit(s);
2635 break;
2636 case CTRL('u'):
2637 case 'u':
2638 nscroll /= 2;
2639 /* FALL THROUGH */
2640 case KEY_PPAGE:
2641 case CTRL('b'):
2642 if (s->first_displayed_entry == NULL)
2643 break;
2644 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2645 s->selected = MAX(0, s->selected - nscroll - 1);
2646 else
2647 log_scroll_up(s, nscroll);
2648 select_commit(s);
2649 break;
2650 case 'j':
2651 case KEY_DOWN:
2652 case '>':
2653 case '.':
2654 case CTRL('n'):
2655 if (s->first_displayed_entry == NULL)
2656 break;
2657 if (s->selected < MIN(view->nlines - 2,
2658 s->commits.ncommits - 1))
2659 s->selected++;
2660 else {
2661 err = log_scroll_down(view, 1);
2662 if (err)
2663 break;
2665 select_commit(s);
2666 break;
2667 case 'G':
2668 case KEY_END: {
2669 /* We don't know yet how many commits, so we're forced to
2670 * traverse them all. */
2671 if (!s->thread_args.log_complete) {
2672 s->thread_args.load_all = 1;
2673 return trigger_log_thread(view, 0);
2676 s->selected = 0;
2677 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2678 for (n = 0; n < view->nlines - 1; n++) {
2679 if (entry == NULL)
2680 break;
2681 s->first_displayed_entry = entry;
2682 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2684 if (n > 0)
2685 s->selected = n - 1;
2686 select_commit(s);
2687 break;
2689 case CTRL('d'):
2690 case 'd':
2691 nscroll /= 2;
2692 /* FALL THROUGH */
2693 case KEY_NPAGE:
2694 case CTRL('f'): {
2695 struct commit_queue_entry *first;
2696 first = s->first_displayed_entry;
2697 if (first == NULL)
2698 break;
2699 err = log_scroll_down(view, nscroll);
2700 if (err)
2701 break;
2702 if (first == s->first_displayed_entry &&
2703 s->selected < MIN(view->nlines - 2,
2704 s->commits.ncommits - 1)) {
2705 /* can't scroll further down */
2706 s->selected += MIN(s->last_displayed_entry->idx -
2707 s->selected_entry->idx, nscroll + 1);
2709 select_commit(s);
2710 break;
2712 case KEY_RESIZE:
2713 if (s->selected > view->nlines - 2)
2714 s->selected = view->nlines - 2;
2715 if (s->selected > s->commits.ncommits - 1)
2716 s->selected = s->commits.ncommits - 1;
2717 select_commit(s);
2718 if (s->commits.ncommits < view->nlines - 1 &&
2719 !s->thread_args.log_complete) {
2720 s->thread_args.commits_needed += (view->nlines - 1) -
2721 s->commits.ncommits;
2722 err = trigger_log_thread(view, 1);
2724 break;
2725 case KEY_ENTER:
2726 case ' ':
2727 case '\r':
2728 if (s->selected_entry == NULL)
2729 break;
2730 if (view_is_parent_view(view))
2731 begin_x = view_split_begin_x(view->begin_x);
2732 err = open_diff_view_for_commit(&diff_view, begin_x,
2733 s->selected_entry->commit, s->selected_entry->id,
2734 view, s->repo);
2735 if (err)
2736 break;
2737 view->focussed = 0;
2738 diff_view->focussed = 1;
2739 if (view_is_parent_view(view)) {
2740 err = view_close_child(view);
2741 if (err)
2742 return err;
2743 view_set_child(view, diff_view);
2744 view->focus_child = 1;
2745 } else
2746 *new_view = diff_view;
2747 break;
2748 case 't':
2749 if (s->selected_entry == NULL)
2750 break;
2751 if (view_is_parent_view(view))
2752 begin_x = view_split_begin_x(view->begin_x);
2753 err = browse_commit_tree(&tree_view, begin_x,
2754 s->selected_entry, s->in_repo_path, s->head_ref_name,
2755 s->repo);
2756 if (err)
2757 break;
2758 view->focussed = 0;
2759 tree_view->focussed = 1;
2760 if (view_is_parent_view(view)) {
2761 err = view_close_child(view);
2762 if (err)
2763 return err;
2764 view_set_child(view, tree_view);
2765 view->focus_child = 1;
2766 } else
2767 *new_view = tree_view;
2768 break;
2769 case KEY_BACKSPACE:
2770 case CTRL('l'):
2771 case 'B':
2772 if (ch == KEY_BACKSPACE &&
2773 got_path_is_root_dir(s->in_repo_path))
2774 break;
2775 err = stop_log_thread(s);
2776 if (err)
2777 return err;
2778 if (ch == KEY_BACKSPACE) {
2779 char *parent_path;
2780 err = got_path_dirname(&parent_path, s->in_repo_path);
2781 if (err)
2782 return err;
2783 free(s->in_repo_path);
2784 s->in_repo_path = parent_path;
2785 s->thread_args.in_repo_path = s->in_repo_path;
2786 } else if (ch == CTRL('l')) {
2787 struct got_object_id *start_id;
2788 err = got_repo_match_object_id(&start_id, NULL,
2789 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2790 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2791 if (err)
2792 return err;
2793 free(s->start_id);
2794 s->start_id = start_id;
2795 s->thread_args.start_id = s->start_id;
2796 } else /* 'B' */
2797 s->log_branches = !s->log_branches;
2799 if (s->thread_args.pack_fds == NULL) {
2800 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2801 if (err)
2802 return err;
2804 err = got_repo_open(&s->thread_args.repo,
2805 got_repo_get_path(s->repo), NULL,
2806 s->thread_args.pack_fds);
2807 if (err)
2808 return err;
2809 tog_free_refs();
2810 err = tog_load_refs(s->repo, 0);
2811 if (err)
2812 return err;
2813 err = got_commit_graph_open(&s->thread_args.graph,
2814 s->in_repo_path, !s->log_branches);
2815 if (err)
2816 return err;
2817 err = got_commit_graph_iter_start(s->thread_args.graph,
2818 s->start_id, s->repo, NULL, NULL);
2819 if (err)
2820 return err;
2821 free_commits(&s->commits);
2822 s->first_displayed_entry = NULL;
2823 s->last_displayed_entry = NULL;
2824 s->selected_entry = NULL;
2825 s->selected = 0;
2826 s->thread_args.log_complete = 0;
2827 s->quit = 0;
2828 s->thread_args.commits_needed = view->nlines;
2829 break;
2830 case 'r':
2831 if (view_is_parent_view(view))
2832 begin_x = view_split_begin_x(view->begin_x);
2833 ref_view = view_open(view->nlines, view->ncols,
2834 view->begin_y, begin_x, TOG_VIEW_REF);
2835 if (ref_view == NULL)
2836 return got_error_from_errno("view_open");
2837 err = open_ref_view(ref_view, s->repo);
2838 if (err) {
2839 view_close(ref_view);
2840 return err;
2842 view->focussed = 0;
2843 ref_view->focussed = 1;
2844 if (view_is_parent_view(view)) {
2845 err = view_close_child(view);
2846 if (err)
2847 return err;
2848 view_set_child(view, ref_view);
2849 view->focus_child = 1;
2850 } else
2851 *new_view = ref_view;
2852 break;
2853 default:
2854 break;
2857 return err;
2860 static const struct got_error *
2861 apply_unveil(const char *repo_path, const char *worktree_path)
2863 const struct got_error *error;
2865 #ifdef PROFILE
2866 if (unveil("gmon.out", "rwc") != 0)
2867 return got_error_from_errno2("unveil", "gmon.out");
2868 #endif
2869 if (repo_path && unveil(repo_path, "r") != 0)
2870 return got_error_from_errno2("unveil", repo_path);
2872 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2873 return got_error_from_errno2("unveil", worktree_path);
2875 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2876 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2878 error = got_privsep_unveil_exec_helpers();
2879 if (error != NULL)
2880 return error;
2882 if (unveil(NULL, NULL) != 0)
2883 return got_error_from_errno("unveil");
2885 return NULL;
2888 static void
2889 init_curses(void)
2892 * Override default signal handlers before starting ncurses.
2893 * This should prevent ncurses from installing its own
2894 * broken cleanup() signal handler.
2896 signal(SIGWINCH, tog_sigwinch);
2897 signal(SIGPIPE, tog_sigpipe);
2898 signal(SIGCONT, tog_sigcont);
2899 signal(SIGINT, tog_sigint);
2900 signal(SIGTERM, tog_sigterm);
2902 initscr();
2903 cbreak();
2904 halfdelay(1); /* Do fast refresh while initial view is loading. */
2905 noecho();
2906 nonl();
2907 intrflush(stdscr, FALSE);
2908 keypad(stdscr, TRUE);
2909 curs_set(0);
2910 if (getenv("TOG_COLORS") != NULL) {
2911 start_color();
2912 use_default_colors();
2916 static const struct got_error *
2917 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2918 struct got_repository *repo, struct got_worktree *worktree)
2920 const struct got_error *err = NULL;
2922 if (argc == 0) {
2923 *in_repo_path = strdup("/");
2924 if (*in_repo_path == NULL)
2925 return got_error_from_errno("strdup");
2926 return NULL;
2929 if (worktree) {
2930 const char *prefix = got_worktree_get_path_prefix(worktree);
2931 char *p;
2933 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2934 if (err)
2935 return err;
2936 if (asprintf(in_repo_path, "%s%s%s", prefix,
2937 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2938 p) == -1) {
2939 err = got_error_from_errno("asprintf");
2940 *in_repo_path = NULL;
2942 free(p);
2943 } else
2944 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2946 return err;
2949 static const struct got_error *
2950 cmd_log(int argc, char *argv[])
2952 const struct got_error *error;
2953 struct got_repository *repo = NULL;
2954 struct got_worktree *worktree = NULL;
2955 struct got_object_id *start_id = NULL;
2956 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2957 char *start_commit = NULL, *label = NULL;
2958 struct got_reference *ref = NULL;
2959 const char *head_ref_name = NULL;
2960 int ch, log_branches = 0;
2961 struct tog_view *view;
2962 int *pack_fds = NULL;
2964 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2965 switch (ch) {
2966 case 'b':
2967 log_branches = 1;
2968 break;
2969 case 'c':
2970 start_commit = optarg;
2971 break;
2972 case 'r':
2973 repo_path = realpath(optarg, NULL);
2974 if (repo_path == NULL)
2975 return got_error_from_errno2("realpath",
2976 optarg);
2977 break;
2978 default:
2979 usage_log();
2980 /* NOTREACHED */
2984 argc -= optind;
2985 argv += optind;
2987 if (argc > 1)
2988 usage_log();
2990 error = got_repo_pack_fds_open(&pack_fds);
2991 if (error != NULL)
2992 goto done;
2994 if (repo_path == NULL) {
2995 cwd = getcwd(NULL, 0);
2996 if (cwd == NULL)
2997 return got_error_from_errno("getcwd");
2998 error = got_worktree_open(&worktree, cwd);
2999 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3000 goto done;
3001 if (worktree)
3002 repo_path =
3003 strdup(got_worktree_get_repo_path(worktree));
3004 else
3005 repo_path = strdup(cwd);
3006 if (repo_path == NULL) {
3007 error = got_error_from_errno("strdup");
3008 goto done;
3012 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3013 if (error != NULL)
3014 goto done;
3016 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3017 repo, worktree);
3018 if (error)
3019 goto done;
3021 init_curses();
3023 error = apply_unveil(got_repo_get_path(repo),
3024 worktree ? got_worktree_get_root_path(worktree) : NULL);
3025 if (error)
3026 goto done;
3028 /* already loaded by tog_log_with_path()? */
3029 if (TAILQ_EMPTY(&tog_refs)) {
3030 error = tog_load_refs(repo, 0);
3031 if (error)
3032 goto done;
3035 if (start_commit == NULL) {
3036 error = got_repo_match_object_id(&start_id, &label,
3037 worktree ? got_worktree_get_head_ref_name(worktree) :
3038 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3039 if (error)
3040 goto done;
3041 head_ref_name = label;
3042 } else {
3043 error = got_ref_open(&ref, repo, start_commit, 0);
3044 if (error == NULL)
3045 head_ref_name = got_ref_get_name(ref);
3046 else if (error->code != GOT_ERR_NOT_REF)
3047 goto done;
3048 error = got_repo_match_object_id(&start_id, NULL,
3049 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3050 if (error)
3051 goto done;
3054 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3055 if (view == NULL) {
3056 error = got_error_from_errno("view_open");
3057 goto done;
3059 error = open_log_view(view, start_id, repo, head_ref_name,
3060 in_repo_path, log_branches);
3061 if (error)
3062 goto done;
3063 if (worktree) {
3064 /* Release work tree lock. */
3065 got_worktree_close(worktree);
3066 worktree = NULL;
3068 error = view_loop(view);
3069 done:
3070 free(in_repo_path);
3071 free(repo_path);
3072 free(cwd);
3073 free(start_id);
3074 free(label);
3075 if (ref)
3076 got_ref_close(ref);
3077 if (repo) {
3078 const struct got_error *close_err = got_repo_close(repo);
3079 if (error == NULL)
3080 error = close_err;
3082 if (worktree)
3083 got_worktree_close(worktree);
3084 if (pack_fds) {
3085 const struct got_error *pack_err =
3086 got_repo_pack_fds_close(pack_fds);
3087 if (error == NULL)
3088 error = pack_err;
3090 tog_free_refs();
3091 return error;
3094 __dead static void
3095 usage_diff(void)
3097 endwin();
3098 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3099 "[-w] object1 object2\n", getprogname());
3100 exit(1);
3103 static int
3104 match_line(const char *line, regex_t *regex, size_t nmatch,
3105 regmatch_t *regmatch)
3107 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3110 struct tog_color *
3111 match_color(struct tog_colors *colors, const char *line)
3113 struct tog_color *tc = NULL;
3115 STAILQ_FOREACH(tc, colors, entry) {
3116 if (match_line(line, &tc->regex, 0, NULL))
3117 return tc;
3120 return NULL;
3123 static const struct got_error *
3124 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3125 WINDOW *window, int skip, regmatch_t *regmatch)
3127 const struct got_error *err = NULL;
3128 wchar_t *wline;
3129 int rme, rms, n, width;
3131 *wtotal = 0;
3132 rms = regmatch->rm_so;
3133 rme = regmatch->rm_eo;
3135 err = format_line(&wline, &width, NULL, line, 0, wlimit + skip,
3136 col_tab_align, 1);
3137 if (err)
3138 return err;
3140 /* draw up to matched token if we haven't scrolled past it */
3141 n = MAX(rms - skip, 0);
3142 if (n) {
3143 waddnwstr(window, wline + skip, n);
3144 wlimit -= n;
3145 *wtotal += n;
3148 if (wlimit > 0) {
3149 int len = rme - rms;
3150 n = 0;
3151 if (skip > rms) {
3152 n = skip - rms;
3153 len = MAX(len - n, 0);
3155 /* draw (visible part of) matched token (if scrolled into it) */
3156 if (len) {
3157 wattron(window, A_STANDOUT);
3158 waddnwstr(window, wline + rms + n, len);
3159 wattroff(window, A_STANDOUT);
3160 wlimit -= len;
3161 *wtotal += len;
3165 if (wlimit > 0 && skip < width) { /* draw rest of line */
3166 n = 0;
3167 if (skip > rme)
3168 n = MIN(skip - rme, width - rme);
3169 waddnwstr(window, wline + rme + n, wlimit);
3172 *wtotal = width;
3173 free(wline);
3174 return NULL;
3177 static const struct got_error *
3178 draw_file(struct tog_view *view, const char *header)
3180 struct tog_diff_view_state *s = &view->state.diff;
3181 regmatch_t *regmatch = &view->regmatch;
3182 const struct got_error *err;
3183 int nprinted = 0;
3184 char *line;
3185 size_t linesize = 0;
3186 ssize_t linelen;
3187 struct tog_color *tc;
3188 wchar_t *wline;
3189 int width;
3190 int max_lines = view->nlines;
3191 int nlines = s->nlines;
3192 off_t line_offset;
3194 line_offset = s->line_offsets[s->first_displayed_line - 1];
3195 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3196 return got_error_from_errno("fseek");
3198 werase(view->window);
3200 if (header) {
3201 if (asprintf(&line, "[%d/%d] %s",
3202 s->first_displayed_line - 1 + s->selected_line, nlines,
3203 header) == -1)
3204 return got_error_from_errno("asprintf");
3205 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3206 0, 0);
3207 free(line);
3208 if (err)
3209 return err;
3211 if (view_needs_focus_indication(view))
3212 wstandout(view->window);
3213 waddwstr(view->window, wline);
3214 free(wline);
3215 wline = NULL;
3216 if (view_needs_focus_indication(view))
3217 wstandend(view->window);
3218 if (width <= view->ncols - 1)
3219 waddch(view->window, '\n');
3221 if (max_lines <= 1)
3222 return NULL;
3223 max_lines--;
3226 s->eof = 0;
3227 view->maxx = 0;
3228 line = NULL;
3229 while (max_lines > 0 && nprinted < max_lines) {
3230 linelen = getline(&line, &linesize, s->f);
3231 if (linelen == -1) {
3232 if (feof(s->f)) {
3233 s->eof = 1;
3234 break;
3236 free(line);
3237 return got_ferror(s->f, GOT_ERR_IO);
3240 tc = match_color(&s->colors, line);
3241 if (tc)
3242 wattr_on(view->window,
3243 COLOR_PAIR(tc->colorpair), NULL);
3244 if (s->first_displayed_line + nprinted == s->matched_line &&
3245 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3246 err = add_matched_line(&width, line, view->ncols, 0,
3247 view->window, view->x, regmatch);
3248 if (err) {
3249 free(line);
3250 return err;
3252 view->maxx = MAX(view->maxx, width);
3253 } else {
3254 int skip;
3256 /* Set view->maxx based on full line length. */
3257 err = format_line(&wline, &width, NULL, line,
3258 0, INT_MAX, 0, view->x ? 1 : 0);
3259 if (err) {
3260 free(line);
3261 return err;
3263 view->maxx = MAX(view->maxx, width);
3266 * Get a new version of the line for display.
3267 * This will be scrolled and/or trimmed in length.
3269 free(wline);
3270 err = format_line(&wline, &width, &skip, line,
3271 view->x, view->ncols, 0, view->x ? 1 : 0);
3272 if (err) {
3273 free(line);
3274 return err;
3276 waddwstr(view->window, &wline[skip]);
3277 free(wline);
3278 wline = NULL;
3280 if (tc)
3281 wattr_off(view->window,
3282 COLOR_PAIR(tc->colorpair), NULL);
3283 if (width <= view->ncols - 1)
3284 waddch(view->window, '\n');
3285 nprinted++;
3287 free(line);
3288 if (nprinted >= 1)
3289 s->last_displayed_line = s->first_displayed_line +
3290 (nprinted - 1);
3291 else
3292 s->last_displayed_line = s->first_displayed_line;
3294 view_vborder(view);
3296 if (s->eof) {
3297 while (nprinted < view->nlines) {
3298 waddch(view->window, '\n');
3299 nprinted++;
3302 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3303 view->ncols, 0, 0);
3304 if (err) {
3305 return err;
3308 wstandout(view->window);
3309 waddwstr(view->window, wline);
3310 free(wline);
3311 wline = NULL;
3312 wstandend(view->window);
3315 return NULL;
3318 static char *
3319 get_datestr(time_t *time, char *datebuf)
3321 struct tm mytm, *tm;
3322 char *p, *s;
3324 tm = gmtime_r(time, &mytm);
3325 if (tm == NULL)
3326 return NULL;
3327 s = asctime_r(tm, datebuf);
3328 if (s == NULL)
3329 return NULL;
3330 p = strchr(s, '\n');
3331 if (p)
3332 *p = '\0';
3333 return s;
3336 static const struct got_error *
3337 get_changed_paths(struct got_pathlist_head *paths,
3338 struct got_commit_object *commit, struct got_repository *repo)
3340 const struct got_error *err = NULL;
3341 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3342 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3343 struct got_object_qid *qid;
3345 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3346 if (qid != NULL) {
3347 struct got_commit_object *pcommit;
3348 err = got_object_open_as_commit(&pcommit, repo,
3349 &qid->id);
3350 if (err)
3351 return err;
3353 tree_id1 = got_object_id_dup(
3354 got_object_commit_get_tree_id(pcommit));
3355 if (tree_id1 == NULL) {
3356 got_object_commit_close(pcommit);
3357 return got_error_from_errno("got_object_id_dup");
3359 got_object_commit_close(pcommit);
3363 if (tree_id1) {
3364 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3365 if (err)
3366 goto done;
3369 tree_id2 = got_object_commit_get_tree_id(commit);
3370 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3371 if (err)
3372 goto done;
3374 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3375 got_diff_tree_collect_changed_paths, paths, 0);
3376 done:
3377 if (tree1)
3378 got_object_tree_close(tree1);
3379 if (tree2)
3380 got_object_tree_close(tree2);
3381 free(tree_id1);
3382 return err;
3385 static const struct got_error *
3386 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3388 off_t *p;
3390 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3391 if (p == NULL)
3392 return got_error_from_errno("reallocarray");
3393 *line_offsets = p;
3394 (*line_offsets)[*nlines] = off;
3395 (*nlines)++;
3396 return NULL;
3399 static const struct got_error *
3400 write_commit_info(off_t **line_offsets, size_t *nlines,
3401 struct got_object_id *commit_id, struct got_reflist_head *refs,
3402 struct got_repository *repo, FILE *outfile)
3404 const struct got_error *err = NULL;
3405 char datebuf[26], *datestr;
3406 struct got_commit_object *commit;
3407 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3408 time_t committer_time;
3409 const char *author, *committer;
3410 char *refs_str = NULL;
3411 struct got_pathlist_head changed_paths;
3412 struct got_pathlist_entry *pe;
3413 off_t outoff = 0;
3414 int n;
3416 TAILQ_INIT(&changed_paths);
3418 if (refs) {
3419 err = build_refs_str(&refs_str, refs, commit_id, repo);
3420 if (err)
3421 return err;
3424 err = got_object_open_as_commit(&commit, repo, commit_id);
3425 if (err)
3426 return err;
3428 err = got_object_id_str(&id_str, commit_id);
3429 if (err) {
3430 err = got_error_from_errno("got_object_id_str");
3431 goto done;
3434 err = add_line_offset(line_offsets, nlines, 0);
3435 if (err)
3436 goto done;
3438 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3439 refs_str ? refs_str : "", refs_str ? ")" : "");
3440 if (n < 0) {
3441 err = got_error_from_errno("fprintf");
3442 goto done;
3444 outoff += n;
3445 err = add_line_offset(line_offsets, nlines, outoff);
3446 if (err)
3447 goto done;
3449 n = fprintf(outfile, "from: %s\n",
3450 got_object_commit_get_author(commit));
3451 if (n < 0) {
3452 err = got_error_from_errno("fprintf");
3453 goto done;
3455 outoff += n;
3456 err = add_line_offset(line_offsets, nlines, outoff);
3457 if (err)
3458 goto done;
3460 committer_time = got_object_commit_get_committer_time(commit);
3461 datestr = get_datestr(&committer_time, datebuf);
3462 if (datestr) {
3463 n = fprintf(outfile, "date: %s UTC\n", datestr);
3464 if (n < 0) {
3465 err = got_error_from_errno("fprintf");
3466 goto done;
3468 outoff += n;
3469 err = add_line_offset(line_offsets, nlines, outoff);
3470 if (err)
3471 goto done;
3473 author = got_object_commit_get_author(commit);
3474 committer = got_object_commit_get_committer(commit);
3475 if (strcmp(author, committer) != 0) {
3476 n = fprintf(outfile, "via: %s\n", committer);
3477 if (n < 0) {
3478 err = got_error_from_errno("fprintf");
3479 goto done;
3481 outoff += n;
3482 err = add_line_offset(line_offsets, nlines, outoff);
3483 if (err)
3484 goto done;
3486 if (got_object_commit_get_nparents(commit) > 1) {
3487 const struct got_object_id_queue *parent_ids;
3488 struct got_object_qid *qid;
3489 int pn = 1;
3490 parent_ids = got_object_commit_get_parent_ids(commit);
3491 STAILQ_FOREACH(qid, parent_ids, entry) {
3492 err = got_object_id_str(&id_str, &qid->id);
3493 if (err)
3494 goto done;
3495 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3496 if (n < 0) {
3497 err = got_error_from_errno("fprintf");
3498 goto done;
3500 outoff += n;
3501 err = add_line_offset(line_offsets, nlines, outoff);
3502 if (err)
3503 goto done;
3504 free(id_str);
3505 id_str = NULL;
3509 err = got_object_commit_get_logmsg(&logmsg, commit);
3510 if (err)
3511 goto done;
3512 s = logmsg;
3513 while ((line = strsep(&s, "\n")) != NULL) {
3514 n = fprintf(outfile, "%s\n", line);
3515 if (n < 0) {
3516 err = got_error_from_errno("fprintf");
3517 goto done;
3519 outoff += n;
3520 err = add_line_offset(line_offsets, nlines, outoff);
3521 if (err)
3522 goto done;
3525 err = get_changed_paths(&changed_paths, commit, repo);
3526 if (err)
3527 goto done;
3528 TAILQ_FOREACH(pe, &changed_paths, entry) {
3529 struct got_diff_changed_path *cp = pe->data;
3530 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3531 if (n < 0) {
3532 err = got_error_from_errno("fprintf");
3533 goto done;
3535 outoff += n;
3536 err = add_line_offset(line_offsets, nlines, outoff);
3537 if (err)
3538 goto done;
3539 free((char *)pe->path);
3540 free(pe->data);
3543 fputc('\n', outfile);
3544 outoff++;
3545 err = add_line_offset(line_offsets, nlines, outoff);
3546 done:
3547 got_pathlist_free(&changed_paths);
3548 free(id_str);
3549 free(logmsg);
3550 free(refs_str);
3551 got_object_commit_close(commit);
3552 if (err) {
3553 free(*line_offsets);
3554 *line_offsets = NULL;
3555 *nlines = 0;
3557 return err;
3560 static const struct got_error *
3561 create_diff(struct tog_diff_view_state *s)
3563 const struct got_error *err = NULL;
3564 FILE *f = NULL;
3565 int obj_type;
3567 free(s->line_offsets);
3568 s->line_offsets = malloc(sizeof(off_t));
3569 if (s->line_offsets == NULL)
3570 return got_error_from_errno("malloc");
3571 s->nlines = 0;
3573 f = got_opentemp();
3574 if (f == NULL) {
3575 err = got_error_from_errno("got_opentemp");
3576 goto done;
3578 if (s->f && fclose(s->f) == EOF) {
3579 err = got_error_from_errno("fclose");
3580 goto done;
3582 s->f = f;
3584 if (s->id1)
3585 err = got_object_get_type(&obj_type, s->repo, s->id1);
3586 else
3587 err = got_object_get_type(&obj_type, s->repo, s->id2);
3588 if (err)
3589 goto done;
3591 switch (obj_type) {
3592 case GOT_OBJ_TYPE_BLOB:
3593 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3594 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3595 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3596 s->repo, s->f);
3597 break;
3598 case GOT_OBJ_TYPE_TREE:
3599 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3600 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3601 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3602 break;
3603 case GOT_OBJ_TYPE_COMMIT: {
3604 const struct got_object_id_queue *parent_ids;
3605 struct got_object_qid *pid;
3606 struct got_commit_object *commit2;
3607 struct got_reflist_head *refs;
3609 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3610 if (err)
3611 goto done;
3612 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3613 /* Show commit info if we're diffing to a parent/root commit. */
3614 if (s->id1 == NULL) {
3615 err = write_commit_info(&s->line_offsets, &s->nlines,
3616 s->id2, refs, s->repo, s->f);
3617 if (err)
3618 goto done;
3619 } else {
3620 parent_ids = got_object_commit_get_parent_ids(commit2);
3621 STAILQ_FOREACH(pid, parent_ids, entry) {
3622 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3623 err = write_commit_info(
3624 &s->line_offsets, &s->nlines,
3625 s->id2, refs, s->repo, s->f);
3626 if (err)
3627 goto done;
3628 break;
3632 got_object_commit_close(commit2);
3634 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3635 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3636 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3637 break;
3639 default:
3640 err = got_error(GOT_ERR_OBJ_TYPE);
3641 break;
3643 if (err)
3644 goto done;
3645 done:
3646 if (s->f && fflush(s->f) != 0 && err == NULL)
3647 err = got_error_from_errno("fflush");
3648 return err;
3651 static void
3652 diff_view_indicate_progress(struct tog_view *view)
3654 mvwaddstr(view->window, 0, 0, "diffing...");
3655 update_panels();
3656 doupdate();
3659 static const struct got_error *
3660 search_start_diff_view(struct tog_view *view)
3662 struct tog_diff_view_state *s = &view->state.diff;
3664 s->matched_line = 0;
3665 return NULL;
3668 static const struct got_error *
3669 search_next_diff_view(struct tog_view *view)
3671 struct tog_diff_view_state *s = &view->state.diff;
3672 const struct got_error *err = NULL;
3673 int lineno;
3674 char *exstr = NULL, *line = NULL;
3675 size_t linesize = 0;
3676 ssize_t linelen;
3678 if (!view->searching) {
3679 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3680 return NULL;
3683 if (s->matched_line) {
3684 if (view->searching == TOG_SEARCH_FORWARD)
3685 lineno = s->matched_line + 1;
3686 else
3687 lineno = s->matched_line - 1;
3688 } else
3689 lineno = s->first_displayed_line;
3691 while (1) {
3692 off_t offset;
3694 if (lineno <= 0 || lineno > s->nlines) {
3695 if (s->matched_line == 0) {
3696 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3697 break;
3700 if (view->searching == TOG_SEARCH_FORWARD)
3701 lineno = 1;
3702 else
3703 lineno = s->nlines;
3706 offset = s->line_offsets[lineno - 1];
3707 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3708 free(line);
3709 return got_error_from_errno("fseeko");
3711 linelen = getline(&line, &linesize, s->f);
3712 err = expand_tab(&exstr, line);
3713 if (err)
3714 break;
3715 if (linelen != -1 &&
3716 match_line(exstr, &view->regex, 1, &view->regmatch)) {
3717 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3718 s->matched_line = lineno;
3719 break;
3721 free(exstr);
3722 exstr = NULL;
3723 if (view->searching == TOG_SEARCH_FORWARD)
3724 lineno++;
3725 else
3726 lineno--;
3728 free(line);
3729 free(exstr);
3731 if (s->matched_line) {
3732 s->first_displayed_line = s->matched_line;
3733 s->selected_line = 1;
3736 return err;
3739 static const struct got_error *
3740 close_diff_view(struct tog_view *view)
3742 const struct got_error *err = NULL;
3743 struct tog_diff_view_state *s = &view->state.diff;
3745 free(s->id1);
3746 s->id1 = NULL;
3747 free(s->id2);
3748 s->id2 = NULL;
3749 if (s->f && fclose(s->f) == EOF)
3750 err = got_error_from_errno("fclose");
3751 s->f = NULL;
3752 if (s->f1 && fclose(s->f1) == EOF)
3753 err = got_error_from_errno("fclose");
3754 s->f1 = NULL;
3755 if (s->f2 && fclose(s->f2) == EOF)
3756 err = got_error_from_errno("fclose");
3757 s->f2 = NULL;
3758 free_colors(&s->colors);
3759 free(s->line_offsets);
3760 s->line_offsets = NULL;
3761 s->nlines = 0;
3762 return err;
3765 static const struct got_error *
3766 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3767 struct got_object_id *id2, const char *label1, const char *label2,
3768 int diff_context, int ignore_whitespace, int force_text_diff,
3769 struct tog_view *log_view, struct got_repository *repo)
3771 const struct got_error *err;
3772 struct tog_diff_view_state *s = &view->state.diff;
3774 memset(s, 0, sizeof(*s));
3776 if (id1 != NULL && id2 != NULL) {
3777 int type1, type2;
3778 err = got_object_get_type(&type1, repo, id1);
3779 if (err)
3780 return err;
3781 err = got_object_get_type(&type2, repo, id2);
3782 if (err)
3783 return err;
3785 if (type1 != type2)
3786 return got_error(GOT_ERR_OBJ_TYPE);
3788 s->first_displayed_line = 1;
3789 s->last_displayed_line = view->nlines;
3790 s->selected_line = 1;
3791 s->repo = repo;
3792 s->id1 = id1;
3793 s->id2 = id2;
3794 s->label1 = label1;
3795 s->label2 = label2;
3797 if (id1) {
3798 s->id1 = got_object_id_dup(id1);
3799 if (s->id1 == NULL)
3800 return got_error_from_errno("got_object_id_dup");
3801 s->f1 = got_opentemp();
3802 if (s->f1 == NULL) {
3803 err = got_error_from_errno("got_opentemp");
3804 goto done;
3806 } else
3807 s->id1 = NULL;
3809 s->id2 = got_object_id_dup(id2);
3810 if (s->id2 == NULL) {
3811 err = got_error_from_errno("got_object_id_dup");
3812 goto done;
3815 s->f2 = got_opentemp();
3816 if (s->f2 == NULL) {
3817 err = got_error_from_errno("got_opentemp");
3818 goto done;
3821 s->first_displayed_line = 1;
3822 s->last_displayed_line = view->nlines;
3823 s->diff_context = diff_context;
3824 s->ignore_whitespace = ignore_whitespace;
3825 s->force_text_diff = force_text_diff;
3826 s->log_view = log_view;
3827 s->repo = repo;
3829 STAILQ_INIT(&s->colors);
3830 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3831 err = add_color(&s->colors,
3832 "^-", TOG_COLOR_DIFF_MINUS,
3833 get_color_value("TOG_COLOR_DIFF_MINUS"));
3834 if (err)
3835 goto done;
3836 err = add_color(&s->colors, "^\\+",
3837 TOG_COLOR_DIFF_PLUS,
3838 get_color_value("TOG_COLOR_DIFF_PLUS"));
3839 if (err)
3840 goto done;
3841 err = add_color(&s->colors,
3842 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3843 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3844 if (err)
3845 goto done;
3847 err = add_color(&s->colors,
3848 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3849 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3850 get_color_value("TOG_COLOR_DIFF_META"));
3851 if (err)
3852 goto done;
3854 err = add_color(&s->colors,
3855 "^(from|via): ", TOG_COLOR_AUTHOR,
3856 get_color_value("TOG_COLOR_AUTHOR"));
3857 if (err)
3858 goto done;
3860 err = add_color(&s->colors,
3861 "^date: ", TOG_COLOR_DATE,
3862 get_color_value("TOG_COLOR_DATE"));
3863 if (err)
3864 goto done;
3867 if (log_view && view_is_splitscreen(view))
3868 show_log_view(log_view); /* draw vborder */
3869 diff_view_indicate_progress(view);
3871 err = create_diff(s);
3873 view->show = show_diff_view;
3874 view->input = input_diff_view;
3875 view->close = close_diff_view;
3876 view->search_start = search_start_diff_view;
3877 view->search_next = search_next_diff_view;
3878 done:
3879 if (err)
3880 close_diff_view(view);
3881 return err;
3884 static const struct got_error *
3885 show_diff_view(struct tog_view *view)
3887 const struct got_error *err;
3888 struct tog_diff_view_state *s = &view->state.diff;
3889 char *id_str1 = NULL, *id_str2, *header;
3890 const char *label1, *label2;
3892 if (s->id1) {
3893 err = got_object_id_str(&id_str1, s->id1);
3894 if (err)
3895 return err;
3896 label1 = s->label1 ? : id_str1;
3897 } else
3898 label1 = "/dev/null";
3900 err = got_object_id_str(&id_str2, s->id2);
3901 if (err)
3902 return err;
3903 label2 = s->label2 ? : id_str2;
3905 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3906 err = got_error_from_errno("asprintf");
3907 free(id_str1);
3908 free(id_str2);
3909 return err;
3911 free(id_str1);
3912 free(id_str2);
3914 err = draw_file(view, header);
3915 free(header);
3916 return err;
3919 static const struct got_error *
3920 set_selected_commit(struct tog_diff_view_state *s,
3921 struct commit_queue_entry *entry)
3923 const struct got_error *err;
3924 const struct got_object_id_queue *parent_ids;
3925 struct got_commit_object *selected_commit;
3926 struct got_object_qid *pid;
3928 free(s->id2);
3929 s->id2 = got_object_id_dup(entry->id);
3930 if (s->id2 == NULL)
3931 return got_error_from_errno("got_object_id_dup");
3933 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3934 if (err)
3935 return err;
3936 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3937 free(s->id1);
3938 pid = STAILQ_FIRST(parent_ids);
3939 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3940 got_object_commit_close(selected_commit);
3941 return NULL;
3944 static const struct got_error *
3945 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3947 const struct got_error *err = NULL;
3948 struct tog_diff_view_state *s = &view->state.diff;
3949 struct tog_log_view_state *ls;
3950 struct commit_queue_entry *old_selected_entry;
3951 char *line = NULL;
3952 size_t linesize = 0;
3953 ssize_t linelen;
3954 int i, nscroll = view->nlines - 1;
3956 switch (ch) {
3957 case '0':
3958 view->x = 0;
3959 break;
3960 case '$':
3961 view->x = MAX(view->maxx - view->ncols / 3, 0);
3962 break;
3963 case KEY_RIGHT:
3964 case 'l':
3965 if (view->x + view->ncols / 3 < view->maxx)
3966 view->x += 2; /* move two columns right */
3967 break;
3968 case KEY_LEFT:
3969 case 'h':
3970 view->x -= MIN(view->x, 2); /* move two columns back */
3971 break;
3972 case 'a':
3973 case 'w':
3974 if (ch == 'a')
3975 s->force_text_diff = !s->force_text_diff;
3976 if (ch == 'w')
3977 s->ignore_whitespace = !s->ignore_whitespace;
3978 wclear(view->window);
3979 s->first_displayed_line = 1;
3980 s->last_displayed_line = view->nlines;
3981 s->matched_line = 0;
3982 diff_view_indicate_progress(view);
3983 err = create_diff(s);
3984 break;
3985 case 'g':
3986 case KEY_HOME:
3987 s->first_displayed_line = 1;
3988 break;
3989 case 'G':
3990 case KEY_END:
3991 if (s->eof)
3992 break;
3994 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3995 s->eof = 1;
3996 break;
3997 case 'k':
3998 case KEY_UP:
3999 case CTRL('p'):
4000 if (s->first_displayed_line > 1)
4001 s->first_displayed_line--;
4002 break;
4003 case CTRL('u'):
4004 case 'u':
4005 nscroll /= 2;
4006 /* FALL THROUGH */
4007 case KEY_PPAGE:
4008 case CTRL('b'):
4009 if (s->first_displayed_line == 1)
4010 break;
4011 i = 0;
4012 while (i++ < nscroll && s->first_displayed_line > 1)
4013 s->first_displayed_line--;
4014 break;
4015 case 'j':
4016 case KEY_DOWN:
4017 case CTRL('n'):
4018 if (!s->eof)
4019 s->first_displayed_line++;
4020 break;
4021 case CTRL('d'):
4022 case 'd':
4023 nscroll /= 2;
4024 /* FALL THROUGH */
4025 case KEY_NPAGE:
4026 case CTRL('f'):
4027 case ' ':
4028 if (s->eof)
4029 break;
4030 i = 0;
4031 while (!s->eof && i++ < nscroll) {
4032 linelen = getline(&line, &linesize, s->f);
4033 s->first_displayed_line++;
4034 if (linelen == -1) {
4035 if (feof(s->f)) {
4036 s->eof = 1;
4037 } else
4038 err = got_ferror(s->f, GOT_ERR_IO);
4039 break;
4042 free(line);
4043 break;
4044 case '[':
4045 if (s->diff_context > 0) {
4046 s->diff_context--;
4047 s->matched_line = 0;
4048 diff_view_indicate_progress(view);
4049 err = create_diff(s);
4050 if (s->first_displayed_line + view->nlines - 1 >
4051 s->nlines) {
4052 s->first_displayed_line = 1;
4053 s->last_displayed_line = view->nlines;
4056 break;
4057 case ']':
4058 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4059 s->diff_context++;
4060 s->matched_line = 0;
4061 diff_view_indicate_progress(view);
4062 err = create_diff(s);
4064 break;
4065 case '<':
4066 case ',':
4067 if (s->log_view == NULL)
4068 break;
4069 ls = &s->log_view->state.log;
4070 old_selected_entry = ls->selected_entry;
4072 err = input_log_view(NULL, s->log_view, KEY_UP);
4073 if (err)
4074 break;
4076 if (old_selected_entry == ls->selected_entry)
4077 break;
4079 err = set_selected_commit(s, ls->selected_entry);
4080 if (err)
4081 break;
4083 s->first_displayed_line = 1;
4084 s->last_displayed_line = view->nlines;
4085 s->matched_line = 0;
4086 view->x = 0;
4088 diff_view_indicate_progress(view);
4089 err = create_diff(s);
4090 break;
4091 case '>':
4092 case '.':
4093 if (s->log_view == NULL)
4094 break;
4095 ls = &s->log_view->state.log;
4096 old_selected_entry = ls->selected_entry;
4098 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4099 if (err)
4100 break;
4102 if (old_selected_entry == ls->selected_entry)
4103 break;
4105 err = set_selected_commit(s, ls->selected_entry);
4106 if (err)
4107 break;
4109 s->first_displayed_line = 1;
4110 s->last_displayed_line = view->nlines;
4111 s->matched_line = 0;
4112 view->x = 0;
4114 diff_view_indicate_progress(view);
4115 err = create_diff(s);
4116 break;
4117 default:
4118 break;
4121 return err;
4124 static const struct got_error *
4125 cmd_diff(int argc, char *argv[])
4127 const struct got_error *error = NULL;
4128 struct got_repository *repo = NULL;
4129 struct got_worktree *worktree = NULL;
4130 struct got_object_id *id1 = NULL, *id2 = NULL;
4131 char *repo_path = NULL, *cwd = NULL;
4132 char *id_str1 = NULL, *id_str2 = NULL;
4133 char *label1 = NULL, *label2 = NULL;
4134 int diff_context = 3, ignore_whitespace = 0;
4135 int ch, force_text_diff = 0;
4136 const char *errstr;
4137 struct tog_view *view;
4138 int *pack_fds = NULL;
4140 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4141 switch (ch) {
4142 case 'a':
4143 force_text_diff = 1;
4144 break;
4145 case 'C':
4146 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4147 &errstr);
4148 if (errstr != NULL)
4149 errx(1, "number of context lines is %s: %s",
4150 errstr, errstr);
4151 break;
4152 case 'r':
4153 repo_path = realpath(optarg, NULL);
4154 if (repo_path == NULL)
4155 return got_error_from_errno2("realpath",
4156 optarg);
4157 got_path_strip_trailing_slashes(repo_path);
4158 break;
4159 case 'w':
4160 ignore_whitespace = 1;
4161 break;
4162 default:
4163 usage_diff();
4164 /* NOTREACHED */
4168 argc -= optind;
4169 argv += optind;
4171 if (argc == 0) {
4172 usage_diff(); /* TODO show local worktree changes */
4173 } else if (argc == 2) {
4174 id_str1 = argv[0];
4175 id_str2 = argv[1];
4176 } else
4177 usage_diff();
4179 error = got_repo_pack_fds_open(&pack_fds);
4180 if (error)
4181 goto done;
4183 if (repo_path == NULL) {
4184 cwd = getcwd(NULL, 0);
4185 if (cwd == NULL)
4186 return got_error_from_errno("getcwd");
4187 error = got_worktree_open(&worktree, cwd);
4188 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4189 goto done;
4190 if (worktree)
4191 repo_path =
4192 strdup(got_worktree_get_repo_path(worktree));
4193 else
4194 repo_path = strdup(cwd);
4195 if (repo_path == NULL) {
4196 error = got_error_from_errno("strdup");
4197 goto done;
4201 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4202 if (error)
4203 goto done;
4205 init_curses();
4207 error = apply_unveil(got_repo_get_path(repo), NULL);
4208 if (error)
4209 goto done;
4211 error = tog_load_refs(repo, 0);
4212 if (error)
4213 goto done;
4215 error = got_repo_match_object_id(&id1, &label1, id_str1,
4216 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4217 if (error)
4218 goto done;
4220 error = got_repo_match_object_id(&id2, &label2, id_str2,
4221 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4222 if (error)
4223 goto done;
4225 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4226 if (view == NULL) {
4227 error = got_error_from_errno("view_open");
4228 goto done;
4230 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4231 ignore_whitespace, force_text_diff, NULL, repo);
4232 if (error)
4233 goto done;
4234 error = view_loop(view);
4235 done:
4236 free(label1);
4237 free(label2);
4238 free(repo_path);
4239 free(cwd);
4240 if (repo) {
4241 const struct got_error *close_err = got_repo_close(repo);
4242 if (error == NULL)
4243 error = close_err;
4245 if (worktree)
4246 got_worktree_close(worktree);
4247 if (pack_fds) {
4248 const struct got_error *pack_err =
4249 got_repo_pack_fds_close(pack_fds);
4250 if (error == NULL)
4251 error = pack_err;
4253 tog_free_refs();
4254 return error;
4257 __dead static void
4258 usage_blame(void)
4260 endwin();
4261 fprintf(stderr,
4262 "usage: %s blame [-c commit] [-r repository-path] path\n",
4263 getprogname());
4264 exit(1);
4267 struct tog_blame_line {
4268 int annotated;
4269 struct got_object_id *id;
4272 static const struct got_error *
4273 draw_blame(struct tog_view *view)
4275 struct tog_blame_view_state *s = &view->state.blame;
4276 struct tog_blame *blame = &s->blame;
4277 regmatch_t *regmatch = &view->regmatch;
4278 const struct got_error *err;
4279 int lineno = 0, nprinted = 0;
4280 char *line = NULL;
4281 size_t linesize = 0;
4282 ssize_t linelen;
4283 wchar_t *wline;
4284 int width;
4285 struct tog_blame_line *blame_line;
4286 struct got_object_id *prev_id = NULL;
4287 char *id_str;
4288 struct tog_color *tc;
4290 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4291 if (err)
4292 return err;
4294 rewind(blame->f);
4295 werase(view->window);
4297 if (asprintf(&line, "commit %s", id_str) == -1) {
4298 err = got_error_from_errno("asprintf");
4299 free(id_str);
4300 return err;
4303 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4304 free(line);
4305 line = NULL;
4306 if (err)
4307 return err;
4308 if (view_needs_focus_indication(view))
4309 wstandout(view->window);
4310 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4311 if (tc)
4312 wattr_on(view->window,
4313 COLOR_PAIR(tc->colorpair), NULL);
4314 waddwstr(view->window, wline);
4315 if (tc)
4316 wattr_off(view->window,
4317 COLOR_PAIR(tc->colorpair), NULL);
4318 if (view_needs_focus_indication(view))
4319 wstandend(view->window);
4320 free(wline);
4321 wline = NULL;
4322 if (width < view->ncols - 1)
4323 waddch(view->window, '\n');
4325 if (asprintf(&line, "[%d/%d] %s%s",
4326 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4327 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4328 free(id_str);
4329 return got_error_from_errno("asprintf");
4331 free(id_str);
4332 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4333 free(line);
4334 line = NULL;
4335 if (err)
4336 return err;
4337 waddwstr(view->window, wline);
4338 free(wline);
4339 wline = NULL;
4340 if (width < view->ncols - 1)
4341 waddch(view->window, '\n');
4343 s->eof = 0;
4344 view->maxx = 0;
4345 while (nprinted < view->nlines - 2) {
4346 linelen = getline(&line, &linesize, blame->f);
4347 if (linelen == -1) {
4348 if (feof(blame->f)) {
4349 s->eof = 1;
4350 break;
4352 free(line);
4353 return got_ferror(blame->f, GOT_ERR_IO);
4355 if (++lineno < s->first_displayed_line)
4356 continue;
4358 if (view->focussed && nprinted == s->selected_line - 1)
4359 wstandout(view->window);
4361 if (blame->nlines > 0) {
4362 blame_line = &blame->lines[lineno - 1];
4363 if (blame_line->annotated && prev_id &&
4364 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4365 !(view->focussed &&
4366 nprinted == s->selected_line - 1)) {
4367 waddstr(view->window, " ");
4368 } else if (blame_line->annotated) {
4369 char *id_str;
4370 err = got_object_id_str(&id_str,
4371 blame_line->id);
4372 if (err) {
4373 free(line);
4374 return err;
4376 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4377 if (tc)
4378 wattr_on(view->window,
4379 COLOR_PAIR(tc->colorpair), NULL);
4380 wprintw(view->window, "%.8s", id_str);
4381 if (tc)
4382 wattr_off(view->window,
4383 COLOR_PAIR(tc->colorpair), NULL);
4384 free(id_str);
4385 prev_id = blame_line->id;
4386 } else {
4387 waddstr(view->window, "........");
4388 prev_id = NULL;
4390 } else {
4391 waddstr(view->window, "........");
4392 prev_id = NULL;
4395 if (view->focussed && nprinted == s->selected_line - 1)
4396 wstandend(view->window);
4397 waddstr(view->window, " ");
4399 if (view->ncols <= 9) {
4400 width = 9;
4401 wline = wcsdup(L"");
4402 if (wline == NULL) {
4403 err = got_error_from_errno("wcsdup");
4404 free(line);
4405 return err;
4407 } else if (s->first_displayed_line + nprinted ==
4408 s->matched_line &&
4409 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4410 err = add_matched_line(&width, line, view->ncols - 9, 9,
4411 view->window, view->x, regmatch);
4412 if (err) {
4413 free(line);
4414 return err;
4416 view->maxx = MAX(view->maxx, width);
4417 width += 9;
4418 } else {
4419 int skip;
4421 /* Set view->maxx based on full line length. */
4422 err = format_line(&wline, &width, NULL, line,
4423 0, INT_MAX, 9, 1);
4424 if (err) {
4425 free(line);
4426 return err;
4428 view->maxx = MAX(view->maxx, width);
4431 * Get a new version of the line for display.
4432 * This will be scrolled and/or trimmed in length.
4434 free(wline);
4435 err = format_line(&wline, &width, &skip, line,
4436 view->x, view->ncols - 9, 9, 1);
4437 if (err) {
4438 free(line);
4439 return err;
4441 waddwstr(view->window, &wline[skip]);
4442 width += 9;
4443 free(wline);
4444 wline = NULL;
4447 if (width <= view->ncols - 1)
4448 waddch(view->window, '\n');
4449 if (++nprinted == 1)
4450 s->first_displayed_line = lineno;
4452 free(line);
4453 s->last_displayed_line = lineno;
4455 view_vborder(view);
4457 return NULL;
4460 static const struct got_error *
4461 blame_cb(void *arg, int nlines, int lineno,
4462 struct got_commit_object *commit, struct got_object_id *id)
4464 const struct got_error *err = NULL;
4465 struct tog_blame_cb_args *a = arg;
4466 struct tog_blame_line *line;
4467 int errcode;
4469 if (nlines != a->nlines ||
4470 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4471 return got_error(GOT_ERR_RANGE);
4473 errcode = pthread_mutex_lock(&tog_mutex);
4474 if (errcode)
4475 return got_error_set_errno(errcode, "pthread_mutex_lock");
4477 if (*a->quit) { /* user has quit the blame view */
4478 err = got_error(GOT_ERR_ITER_COMPLETED);
4479 goto done;
4482 if (lineno == -1)
4483 goto done; /* no change in this commit */
4485 line = &a->lines[lineno - 1];
4486 if (line->annotated)
4487 goto done;
4489 line->id = got_object_id_dup(id);
4490 if (line->id == NULL) {
4491 err = got_error_from_errno("got_object_id_dup");
4492 goto done;
4494 line->annotated = 1;
4495 done:
4496 errcode = pthread_mutex_unlock(&tog_mutex);
4497 if (errcode)
4498 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4499 return err;
4502 static void *
4503 blame_thread(void *arg)
4505 const struct got_error *err, *close_err;
4506 struct tog_blame_thread_args *ta = arg;
4507 struct tog_blame_cb_args *a = ta->cb_args;
4508 int errcode;
4510 err = block_signals_used_by_main_thread();
4511 if (err)
4512 return (void *)err;
4514 err = got_blame(ta->path, a->commit_id, ta->repo,
4515 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4516 if (err && err->code == GOT_ERR_CANCELLED)
4517 err = NULL;
4519 errcode = pthread_mutex_lock(&tog_mutex);
4520 if (errcode)
4521 return (void *)got_error_set_errno(errcode,
4522 "pthread_mutex_lock");
4524 close_err = got_repo_close(ta->repo);
4525 if (err == NULL)
4526 err = close_err;
4527 ta->repo = NULL;
4528 *ta->complete = 1;
4530 errcode = pthread_mutex_unlock(&tog_mutex);
4531 if (errcode && err == NULL)
4532 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4534 return (void *)err;
4537 static struct got_object_id *
4538 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4539 int first_displayed_line, int selected_line)
4541 struct tog_blame_line *line;
4543 if (nlines <= 0)
4544 return NULL;
4546 line = &lines[first_displayed_line - 1 + selected_line - 1];
4547 if (!line->annotated)
4548 return NULL;
4550 return line->id;
4553 static const struct got_error *
4554 stop_blame(struct tog_blame *blame)
4556 const struct got_error *err = NULL;
4557 int i;
4559 if (blame->thread) {
4560 int errcode;
4561 errcode = pthread_mutex_unlock(&tog_mutex);
4562 if (errcode)
4563 return got_error_set_errno(errcode,
4564 "pthread_mutex_unlock");
4565 errcode = pthread_join(blame->thread, (void **)&err);
4566 if (errcode)
4567 return got_error_set_errno(errcode, "pthread_join");
4568 errcode = pthread_mutex_lock(&tog_mutex);
4569 if (errcode)
4570 return got_error_set_errno(errcode,
4571 "pthread_mutex_lock");
4572 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4573 err = NULL;
4574 blame->thread = NULL;
4576 if (blame->thread_args.repo) {
4577 const struct got_error *close_err;
4578 close_err = got_repo_close(blame->thread_args.repo);
4579 if (err == NULL)
4580 err = close_err;
4581 blame->thread_args.repo = NULL;
4583 if (blame->f) {
4584 if (fclose(blame->f) == EOF && err == NULL)
4585 err = got_error_from_errno("fclose");
4586 blame->f = NULL;
4588 if (blame->lines) {
4589 for (i = 0; i < blame->nlines; i++)
4590 free(blame->lines[i].id);
4591 free(blame->lines);
4592 blame->lines = NULL;
4594 free(blame->cb_args.commit_id);
4595 blame->cb_args.commit_id = NULL;
4596 if (blame->pack_fds) {
4597 const struct got_error *pack_err =
4598 got_repo_pack_fds_close(blame->pack_fds);
4599 if (err == NULL)
4600 err = pack_err;
4601 blame->pack_fds = NULL;
4603 return err;
4606 static const struct got_error *
4607 cancel_blame_view(void *arg)
4609 const struct got_error *err = NULL;
4610 int *done = arg;
4611 int errcode;
4613 errcode = pthread_mutex_lock(&tog_mutex);
4614 if (errcode)
4615 return got_error_set_errno(errcode,
4616 "pthread_mutex_unlock");
4618 if (*done)
4619 err = got_error(GOT_ERR_CANCELLED);
4621 errcode = pthread_mutex_unlock(&tog_mutex);
4622 if (errcode)
4623 return got_error_set_errno(errcode,
4624 "pthread_mutex_lock");
4626 return err;
4629 static const struct got_error *
4630 run_blame(struct tog_view *view)
4632 struct tog_blame_view_state *s = &view->state.blame;
4633 struct tog_blame *blame = &s->blame;
4634 const struct got_error *err = NULL;
4635 struct got_commit_object *commit = NULL;
4636 struct got_blob_object *blob = NULL;
4637 struct got_repository *thread_repo = NULL;
4638 struct got_object_id *obj_id = NULL;
4639 int obj_type;
4640 int *pack_fds = NULL;
4642 err = got_object_open_as_commit(&commit, s->repo,
4643 &s->blamed_commit->id);
4644 if (err)
4645 return err;
4647 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4648 if (err)
4649 goto done;
4651 err = got_object_get_type(&obj_type, s->repo, obj_id);
4652 if (err)
4653 goto done;
4655 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4656 err = got_error(GOT_ERR_OBJ_TYPE);
4657 goto done;
4660 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4661 if (err)
4662 goto done;
4663 blame->f = got_opentemp();
4664 if (blame->f == NULL) {
4665 err = got_error_from_errno("got_opentemp");
4666 goto done;
4668 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4669 &blame->line_offsets, blame->f, blob);
4670 if (err)
4671 goto done;
4672 if (blame->nlines == 0) {
4673 s->blame_complete = 1;
4674 goto done;
4677 /* Don't include \n at EOF in the blame line count. */
4678 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4679 blame->nlines--;
4681 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4682 if (blame->lines == NULL) {
4683 err = got_error_from_errno("calloc");
4684 goto done;
4687 err = got_repo_pack_fds_open(&pack_fds);
4688 if (err)
4689 goto done;
4690 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4691 pack_fds);
4692 if (err)
4693 goto done;
4695 blame->pack_fds = pack_fds;
4696 blame->cb_args.view = view;
4697 blame->cb_args.lines = blame->lines;
4698 blame->cb_args.nlines = blame->nlines;
4699 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4700 if (blame->cb_args.commit_id == NULL) {
4701 err = got_error_from_errno("got_object_id_dup");
4702 goto done;
4704 blame->cb_args.quit = &s->done;
4706 blame->thread_args.path = s->path;
4707 blame->thread_args.repo = thread_repo;
4708 blame->thread_args.cb_args = &blame->cb_args;
4709 blame->thread_args.complete = &s->blame_complete;
4710 blame->thread_args.cancel_cb = cancel_blame_view;
4711 blame->thread_args.cancel_arg = &s->done;
4712 s->blame_complete = 0;
4714 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4715 s->first_displayed_line = 1;
4716 s->last_displayed_line = view->nlines;
4717 s->selected_line = 1;
4719 s->matched_line = 0;
4721 done:
4722 if (commit)
4723 got_object_commit_close(commit);
4724 if (blob)
4725 got_object_blob_close(blob);
4726 free(obj_id);
4727 if (err)
4728 stop_blame(blame);
4729 return err;
4732 static const struct got_error *
4733 open_blame_view(struct tog_view *view, char *path,
4734 struct got_object_id *commit_id, struct got_repository *repo)
4736 const struct got_error *err = NULL;
4737 struct tog_blame_view_state *s = &view->state.blame;
4739 STAILQ_INIT(&s->blamed_commits);
4741 s->path = strdup(path);
4742 if (s->path == NULL)
4743 return got_error_from_errno("strdup");
4745 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4746 if (err) {
4747 free(s->path);
4748 return err;
4751 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4752 s->first_displayed_line = 1;
4753 s->last_displayed_line = view->nlines;
4754 s->selected_line = 1;
4755 s->blame_complete = 0;
4756 s->repo = repo;
4757 s->commit_id = commit_id;
4758 memset(&s->blame, 0, sizeof(s->blame));
4760 STAILQ_INIT(&s->colors);
4761 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4762 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4763 get_color_value("TOG_COLOR_COMMIT"));
4764 if (err)
4765 return err;
4768 view->show = show_blame_view;
4769 view->input = input_blame_view;
4770 view->close = close_blame_view;
4771 view->search_start = search_start_blame_view;
4772 view->search_next = search_next_blame_view;
4774 return run_blame(view);
4777 static const struct got_error *
4778 close_blame_view(struct tog_view *view)
4780 const struct got_error *err = NULL;
4781 struct tog_blame_view_state *s = &view->state.blame;
4783 if (s->blame.thread)
4784 err = stop_blame(&s->blame);
4786 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4787 struct got_object_qid *blamed_commit;
4788 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4789 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4790 got_object_qid_free(blamed_commit);
4793 free(s->path);
4794 free_colors(&s->colors);
4795 return err;
4798 static const struct got_error *
4799 search_start_blame_view(struct tog_view *view)
4801 struct tog_blame_view_state *s = &view->state.blame;
4803 s->matched_line = 0;
4804 return NULL;
4807 static const struct got_error *
4808 search_next_blame_view(struct tog_view *view)
4810 struct tog_blame_view_state *s = &view->state.blame;
4811 const struct got_error *err = NULL;
4812 int lineno;
4813 char *exstr = NULL, *line = NULL;
4814 size_t linesize = 0;
4815 ssize_t linelen;
4817 if (!view->searching) {
4818 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4819 return NULL;
4822 if (s->matched_line) {
4823 if (view->searching == TOG_SEARCH_FORWARD)
4824 lineno = s->matched_line + 1;
4825 else
4826 lineno = s->matched_line - 1;
4827 } else
4828 lineno = s->first_displayed_line - 1 + s->selected_line;
4830 while (1) {
4831 off_t offset;
4833 if (lineno <= 0 || lineno > s->blame.nlines) {
4834 if (s->matched_line == 0) {
4835 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4836 break;
4839 if (view->searching == TOG_SEARCH_FORWARD)
4840 lineno = 1;
4841 else
4842 lineno = s->blame.nlines;
4845 offset = s->blame.line_offsets[lineno - 1];
4846 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4847 free(line);
4848 return got_error_from_errno("fseeko");
4850 linelen = getline(&line, &linesize, s->blame.f);
4851 err = expand_tab(&exstr, line);
4852 if (err)
4853 break;
4854 if (linelen != -1 &&
4855 match_line(exstr, &view->regex, 1, &view->regmatch)) {
4856 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4857 s->matched_line = lineno;
4858 break;
4860 free(exstr);
4861 exstr = NULL;
4862 if (view->searching == TOG_SEARCH_FORWARD)
4863 lineno++;
4864 else
4865 lineno--;
4867 free(line);
4868 free(exstr);
4870 if (s->matched_line) {
4871 s->first_displayed_line = s->matched_line;
4872 s->selected_line = 1;
4875 return err;
4878 static const struct got_error *
4879 show_blame_view(struct tog_view *view)
4881 const struct got_error *err = NULL;
4882 struct tog_blame_view_state *s = &view->state.blame;
4883 int errcode;
4885 if (s->blame.thread == NULL && !s->blame_complete) {
4886 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4887 &s->blame.thread_args);
4888 if (errcode)
4889 return got_error_set_errno(errcode, "pthread_create");
4891 halfdelay(1); /* fast refresh while annotating */
4894 if (s->blame_complete)
4895 halfdelay(10); /* disable fast refresh */
4897 err = draw_blame(view);
4899 view_vborder(view);
4900 return err;
4903 static const struct got_error *
4904 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4906 const struct got_error *err = NULL, *thread_err = NULL;
4907 struct tog_view *diff_view;
4908 struct tog_blame_view_state *s = &view->state.blame;
4909 int begin_x = 0, nscroll = view->nlines - 2;
4911 switch (ch) {
4912 case '0':
4913 view->x = 0;
4914 break;
4915 case '$':
4916 view->x = MAX(view->maxx - view->ncols / 3, 0);
4917 break;
4918 case KEY_RIGHT:
4919 case 'l':
4920 if (view->x + view->ncols / 3 < view->maxx)
4921 view->x += 2; /* move two columns right */
4922 break;
4923 case KEY_LEFT:
4924 case 'h':
4925 view->x -= MIN(view->x, 2); /* move two columns back */
4926 break;
4927 case 'q':
4928 s->done = 1;
4929 break;
4930 case 'g':
4931 case KEY_HOME:
4932 s->selected_line = 1;
4933 s->first_displayed_line = 1;
4934 break;
4935 case 'G':
4936 case KEY_END:
4937 if (s->blame.nlines < view->nlines - 2) {
4938 s->selected_line = s->blame.nlines;
4939 s->first_displayed_line = 1;
4940 } else {
4941 s->selected_line = view->nlines - 2;
4942 s->first_displayed_line = s->blame.nlines -
4943 (view->nlines - 3);
4945 break;
4946 case 'k':
4947 case KEY_UP:
4948 case CTRL('p'):
4949 if (s->selected_line > 1)
4950 s->selected_line--;
4951 else if (s->selected_line == 1 &&
4952 s->first_displayed_line > 1)
4953 s->first_displayed_line--;
4954 break;
4955 case CTRL('u'):
4956 case 'u':
4957 nscroll /= 2;
4958 /* FALL THROUGH */
4959 case KEY_PPAGE:
4960 case CTRL('b'):
4961 if (s->first_displayed_line == 1) {
4962 s->selected_line = MAX(1, s->selected_line - nscroll);
4963 break;
4965 if (s->first_displayed_line > nscroll)
4966 s->first_displayed_line -= nscroll;
4967 else
4968 s->first_displayed_line = 1;
4969 break;
4970 case 'j':
4971 case KEY_DOWN:
4972 case CTRL('n'):
4973 if (s->selected_line < view->nlines - 2 &&
4974 s->first_displayed_line +
4975 s->selected_line <= s->blame.nlines)
4976 s->selected_line++;
4977 else if (s->last_displayed_line <
4978 s->blame.nlines)
4979 s->first_displayed_line++;
4980 break;
4981 case 'b':
4982 case 'p': {
4983 struct got_object_id *id = NULL;
4984 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4985 s->first_displayed_line, s->selected_line);
4986 if (id == NULL)
4987 break;
4988 if (ch == 'p') {
4989 struct got_commit_object *commit, *pcommit;
4990 struct got_object_qid *pid;
4991 struct got_object_id *blob_id = NULL;
4992 int obj_type;
4993 err = got_object_open_as_commit(&commit,
4994 s->repo, id);
4995 if (err)
4996 break;
4997 pid = STAILQ_FIRST(
4998 got_object_commit_get_parent_ids(commit));
4999 if (pid == NULL) {
5000 got_object_commit_close(commit);
5001 break;
5003 /* Check if path history ends here. */
5004 err = got_object_open_as_commit(&pcommit,
5005 s->repo, &pid->id);
5006 if (err)
5007 break;
5008 err = got_object_id_by_path(&blob_id, s->repo,
5009 pcommit, s->path);
5010 got_object_commit_close(pcommit);
5011 if (err) {
5012 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5013 err = NULL;
5014 got_object_commit_close(commit);
5015 break;
5017 err = got_object_get_type(&obj_type, s->repo,
5018 blob_id);
5019 free(blob_id);
5020 /* Can't blame non-blob type objects. */
5021 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5022 got_object_commit_close(commit);
5023 break;
5025 err = got_object_qid_alloc(&s->blamed_commit,
5026 &pid->id);
5027 got_object_commit_close(commit);
5028 } else {
5029 if (got_object_id_cmp(id,
5030 &s->blamed_commit->id) == 0)
5031 break;
5032 err = got_object_qid_alloc(&s->blamed_commit,
5033 id);
5035 if (err)
5036 break;
5037 s->done = 1;
5038 thread_err = stop_blame(&s->blame);
5039 s->done = 0;
5040 if (thread_err)
5041 break;
5042 STAILQ_INSERT_HEAD(&s->blamed_commits,
5043 s->blamed_commit, entry);
5044 err = run_blame(view);
5045 if (err)
5046 break;
5047 break;
5049 case 'B': {
5050 struct got_object_qid *first;
5051 first = STAILQ_FIRST(&s->blamed_commits);
5052 if (!got_object_id_cmp(&first->id, s->commit_id))
5053 break;
5054 s->done = 1;
5055 thread_err = stop_blame(&s->blame);
5056 s->done = 0;
5057 if (thread_err)
5058 break;
5059 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5060 got_object_qid_free(s->blamed_commit);
5061 s->blamed_commit =
5062 STAILQ_FIRST(&s->blamed_commits);
5063 err = run_blame(view);
5064 if (err)
5065 break;
5066 break;
5068 case KEY_ENTER:
5069 case '\r': {
5070 struct got_object_id *id = NULL;
5071 struct got_object_qid *pid;
5072 struct got_commit_object *commit = NULL;
5073 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5074 s->first_displayed_line, s->selected_line);
5075 if (id == NULL)
5076 break;
5077 err = got_object_open_as_commit(&commit, s->repo, id);
5078 if (err)
5079 break;
5080 pid = STAILQ_FIRST(
5081 got_object_commit_get_parent_ids(commit));
5082 if (view_is_parent_view(view))
5083 begin_x = view_split_begin_x(view->begin_x);
5084 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5085 if (diff_view == NULL) {
5086 got_object_commit_close(commit);
5087 err = got_error_from_errno("view_open");
5088 break;
5090 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5091 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5092 got_object_commit_close(commit);
5093 if (err) {
5094 view_close(diff_view);
5095 break;
5097 view->focussed = 0;
5098 diff_view->focussed = 1;
5099 if (view_is_parent_view(view)) {
5100 err = view_close_child(view);
5101 if (err)
5102 break;
5103 view_set_child(view, diff_view);
5104 view->focus_child = 1;
5105 } else
5106 *new_view = diff_view;
5107 if (err)
5108 break;
5109 break;
5111 case CTRL('d'):
5112 case 'd':
5113 nscroll /= 2;
5114 /* FALL THROUGH */
5115 case KEY_NPAGE:
5116 case CTRL('f'):
5117 case ' ':
5118 if (s->last_displayed_line >= s->blame.nlines &&
5119 s->selected_line >= MIN(s->blame.nlines,
5120 view->nlines - 2)) {
5121 break;
5123 if (s->last_displayed_line >= s->blame.nlines &&
5124 s->selected_line < view->nlines - 2) {
5125 s->selected_line +=
5126 MIN(nscroll, s->last_displayed_line -
5127 s->first_displayed_line - s->selected_line + 1);
5129 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5130 s->first_displayed_line += nscroll;
5131 else
5132 s->first_displayed_line =
5133 s->blame.nlines - (view->nlines - 3);
5134 break;
5135 case KEY_RESIZE:
5136 if (s->selected_line > view->nlines - 2) {
5137 s->selected_line = MIN(s->blame.nlines,
5138 view->nlines - 2);
5140 break;
5141 default:
5142 break;
5144 return thread_err ? thread_err : err;
5147 static const struct got_error *
5148 cmd_blame(int argc, char *argv[])
5150 const struct got_error *error;
5151 struct got_repository *repo = NULL;
5152 struct got_worktree *worktree = NULL;
5153 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5154 char *link_target = NULL;
5155 struct got_object_id *commit_id = NULL;
5156 struct got_commit_object *commit = NULL;
5157 char *commit_id_str = NULL;
5158 int ch;
5159 struct tog_view *view;
5160 int *pack_fds = NULL;
5162 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5163 switch (ch) {
5164 case 'c':
5165 commit_id_str = optarg;
5166 break;
5167 case 'r':
5168 repo_path = realpath(optarg, NULL);
5169 if (repo_path == NULL)
5170 return got_error_from_errno2("realpath",
5171 optarg);
5172 break;
5173 default:
5174 usage_blame();
5175 /* NOTREACHED */
5179 argc -= optind;
5180 argv += optind;
5182 if (argc != 1)
5183 usage_blame();
5185 error = got_repo_pack_fds_open(&pack_fds);
5186 if (error != NULL)
5187 goto done;
5189 if (repo_path == NULL) {
5190 cwd = getcwd(NULL, 0);
5191 if (cwd == NULL)
5192 return got_error_from_errno("getcwd");
5193 error = got_worktree_open(&worktree, cwd);
5194 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5195 goto done;
5196 if (worktree)
5197 repo_path =
5198 strdup(got_worktree_get_repo_path(worktree));
5199 else
5200 repo_path = strdup(cwd);
5201 if (repo_path == NULL) {
5202 error = got_error_from_errno("strdup");
5203 goto done;
5207 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5208 if (error != NULL)
5209 goto done;
5211 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5212 worktree);
5213 if (error)
5214 goto done;
5216 init_curses();
5218 error = apply_unveil(got_repo_get_path(repo), NULL);
5219 if (error)
5220 goto done;
5222 error = tog_load_refs(repo, 0);
5223 if (error)
5224 goto done;
5226 if (commit_id_str == NULL) {
5227 struct got_reference *head_ref;
5228 error = got_ref_open(&head_ref, repo, worktree ?
5229 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5230 if (error != NULL)
5231 goto done;
5232 error = got_ref_resolve(&commit_id, repo, head_ref);
5233 got_ref_close(head_ref);
5234 } else {
5235 error = got_repo_match_object_id(&commit_id, NULL,
5236 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5238 if (error != NULL)
5239 goto done;
5241 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5242 if (view == NULL) {
5243 error = got_error_from_errno("view_open");
5244 goto done;
5247 error = got_object_open_as_commit(&commit, repo, commit_id);
5248 if (error)
5249 goto done;
5251 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5252 commit, repo);
5253 if (error)
5254 goto done;
5256 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5257 commit_id, repo);
5258 if (error)
5259 goto done;
5260 if (worktree) {
5261 /* Release work tree lock. */
5262 got_worktree_close(worktree);
5263 worktree = NULL;
5265 error = view_loop(view);
5266 done:
5267 free(repo_path);
5268 free(in_repo_path);
5269 free(link_target);
5270 free(cwd);
5271 free(commit_id);
5272 if (commit)
5273 got_object_commit_close(commit);
5274 if (worktree)
5275 got_worktree_close(worktree);
5276 if (repo) {
5277 const struct got_error *close_err = got_repo_close(repo);
5278 if (error == NULL)
5279 error = close_err;
5281 if (pack_fds) {
5282 const struct got_error *pack_err =
5283 got_repo_pack_fds_close(pack_fds);
5284 if (error == NULL)
5285 error = pack_err;
5287 tog_free_refs();
5288 return error;
5291 static const struct got_error *
5292 draw_tree_entries(struct tog_view *view, const char *parent_path)
5294 struct tog_tree_view_state *s = &view->state.tree;
5295 const struct got_error *err = NULL;
5296 struct got_tree_entry *te;
5297 wchar_t *wline;
5298 struct tog_color *tc;
5299 int width, n, i, nentries;
5300 int limit = view->nlines;
5302 s->ndisplayed = 0;
5304 werase(view->window);
5306 if (limit == 0)
5307 return NULL;
5309 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5310 0, 0);
5311 if (err)
5312 return err;
5313 if (view_needs_focus_indication(view))
5314 wstandout(view->window);
5315 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5316 if (tc)
5317 wattr_on(view->window,
5318 COLOR_PAIR(tc->colorpair), NULL);
5319 waddwstr(view->window, wline);
5320 if (tc)
5321 wattr_off(view->window,
5322 COLOR_PAIR(tc->colorpair), NULL);
5323 if (view_needs_focus_indication(view))
5324 wstandend(view->window);
5325 free(wline);
5326 wline = NULL;
5327 if (width < view->ncols - 1)
5328 waddch(view->window, '\n');
5329 if (--limit <= 0)
5330 return NULL;
5331 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5332 0, 0);
5333 if (err)
5334 return err;
5335 waddwstr(view->window, wline);
5336 free(wline);
5337 wline = NULL;
5338 if (width < view->ncols - 1)
5339 waddch(view->window, '\n');
5340 if (--limit <= 0)
5341 return NULL;
5342 waddch(view->window, '\n');
5343 if (--limit <= 0)
5344 return NULL;
5346 if (s->first_displayed_entry == NULL) {
5347 te = got_object_tree_get_first_entry(s->tree);
5348 if (s->selected == 0) {
5349 if (view->focussed)
5350 wstandout(view->window);
5351 s->selected_entry = NULL;
5353 waddstr(view->window, " ..\n"); /* parent directory */
5354 if (s->selected == 0 && view->focussed)
5355 wstandend(view->window);
5356 s->ndisplayed++;
5357 if (--limit <= 0)
5358 return NULL;
5359 n = 1;
5360 } else {
5361 n = 0;
5362 te = s->first_displayed_entry;
5365 nentries = got_object_tree_get_nentries(s->tree);
5366 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5367 char *line = NULL, *id_str = NULL, *link_target = NULL;
5368 const char *modestr = "";
5369 mode_t mode;
5371 te = got_object_tree_get_entry(s->tree, i);
5372 mode = got_tree_entry_get_mode(te);
5374 if (s->show_ids) {
5375 err = got_object_id_str(&id_str,
5376 got_tree_entry_get_id(te));
5377 if (err)
5378 return got_error_from_errno(
5379 "got_object_id_str");
5381 if (got_object_tree_entry_is_submodule(te))
5382 modestr = "$";
5383 else if (S_ISLNK(mode)) {
5384 int i;
5386 err = got_tree_entry_get_symlink_target(&link_target,
5387 te, s->repo);
5388 if (err) {
5389 free(id_str);
5390 return err;
5392 for (i = 0; i < strlen(link_target); i++) {
5393 if (!isprint((unsigned char)link_target[i]))
5394 link_target[i] = '?';
5396 modestr = "@";
5398 else if (S_ISDIR(mode))
5399 modestr = "/";
5400 else if (mode & S_IXUSR)
5401 modestr = "*";
5402 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5403 got_tree_entry_get_name(te), modestr,
5404 link_target ? " -> ": "",
5405 link_target ? link_target : "") == -1) {
5406 free(id_str);
5407 free(link_target);
5408 return got_error_from_errno("asprintf");
5410 free(id_str);
5411 free(link_target);
5412 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5413 0, 0);
5414 if (err) {
5415 free(line);
5416 break;
5418 if (n == s->selected) {
5419 if (view->focussed)
5420 wstandout(view->window);
5421 s->selected_entry = te;
5423 tc = match_color(&s->colors, line);
5424 if (tc)
5425 wattr_on(view->window,
5426 COLOR_PAIR(tc->colorpair), NULL);
5427 waddwstr(view->window, wline);
5428 if (tc)
5429 wattr_off(view->window,
5430 COLOR_PAIR(tc->colorpair), NULL);
5431 if (width < view->ncols - 1)
5432 waddch(view->window, '\n');
5433 if (n == s->selected && view->focussed)
5434 wstandend(view->window);
5435 free(line);
5436 free(wline);
5437 wline = NULL;
5438 n++;
5439 s->ndisplayed++;
5440 s->last_displayed_entry = te;
5441 if (--limit <= 0)
5442 break;
5445 return err;
5448 static void
5449 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5451 struct got_tree_entry *te;
5452 int isroot = s->tree == s->root;
5453 int i = 0;
5455 if (s->first_displayed_entry == NULL)
5456 return;
5458 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5459 while (i++ < maxscroll) {
5460 if (te == NULL) {
5461 if (!isroot)
5462 s->first_displayed_entry = NULL;
5463 break;
5465 s->first_displayed_entry = te;
5466 te = got_tree_entry_get_prev(s->tree, te);
5470 static void
5471 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5473 struct got_tree_entry *next, *last;
5474 int n = 0;
5476 if (s->first_displayed_entry)
5477 next = got_tree_entry_get_next(s->tree,
5478 s->first_displayed_entry);
5479 else
5480 next = got_object_tree_get_first_entry(s->tree);
5482 last = s->last_displayed_entry;
5483 while (next && last && n++ < maxscroll) {
5484 last = got_tree_entry_get_next(s->tree, last);
5485 if (last) {
5486 s->first_displayed_entry = next;
5487 next = got_tree_entry_get_next(s->tree, next);
5492 static const struct got_error *
5493 tree_entry_path(char **path, struct tog_parent_trees *parents,
5494 struct got_tree_entry *te)
5496 const struct got_error *err = NULL;
5497 struct tog_parent_tree *pt;
5498 size_t len = 2; /* for leading slash and NUL */
5500 TAILQ_FOREACH(pt, parents, entry)
5501 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5502 + 1 /* slash */;
5503 if (te)
5504 len += strlen(got_tree_entry_get_name(te));
5506 *path = calloc(1, len);
5507 if (path == NULL)
5508 return got_error_from_errno("calloc");
5510 (*path)[0] = '/';
5511 pt = TAILQ_LAST(parents, tog_parent_trees);
5512 while (pt) {
5513 const char *name = got_tree_entry_get_name(pt->selected_entry);
5514 if (strlcat(*path, name, len) >= len) {
5515 err = got_error(GOT_ERR_NO_SPACE);
5516 goto done;
5518 if (strlcat(*path, "/", len) >= len) {
5519 err = got_error(GOT_ERR_NO_SPACE);
5520 goto done;
5522 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5524 if (te) {
5525 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5526 err = got_error(GOT_ERR_NO_SPACE);
5527 goto done;
5530 done:
5531 if (err) {
5532 free(*path);
5533 *path = NULL;
5535 return err;
5538 static const struct got_error *
5539 blame_tree_entry(struct tog_view **new_view, int begin_x,
5540 struct got_tree_entry *te, struct tog_parent_trees *parents,
5541 struct got_object_id *commit_id, struct got_repository *repo)
5543 const struct got_error *err = NULL;
5544 char *path;
5545 struct tog_view *blame_view;
5547 *new_view = NULL;
5549 err = tree_entry_path(&path, parents, te);
5550 if (err)
5551 return err;
5553 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5554 if (blame_view == NULL) {
5555 err = got_error_from_errno("view_open");
5556 goto done;
5559 err = open_blame_view(blame_view, path, commit_id, repo);
5560 if (err) {
5561 if (err->code == GOT_ERR_CANCELLED)
5562 err = NULL;
5563 view_close(blame_view);
5564 } else
5565 *new_view = blame_view;
5566 done:
5567 free(path);
5568 return err;
5571 static const struct got_error *
5572 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5573 struct tog_tree_view_state *s)
5575 struct tog_view *log_view;
5576 const struct got_error *err = NULL;
5577 char *path;
5579 *new_view = NULL;
5581 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5582 if (log_view == NULL)
5583 return got_error_from_errno("view_open");
5585 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5586 if (err)
5587 return err;
5589 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5590 path, 0);
5591 if (err)
5592 view_close(log_view);
5593 else
5594 *new_view = log_view;
5595 free(path);
5596 return err;
5599 static const struct got_error *
5600 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5601 const char *head_ref_name, struct got_repository *repo)
5603 const struct got_error *err = NULL;
5604 char *commit_id_str = NULL;
5605 struct tog_tree_view_state *s = &view->state.tree;
5606 struct got_commit_object *commit = NULL;
5608 TAILQ_INIT(&s->parents);
5609 STAILQ_INIT(&s->colors);
5611 s->commit_id = got_object_id_dup(commit_id);
5612 if (s->commit_id == NULL)
5613 return got_error_from_errno("got_object_id_dup");
5615 err = got_object_open_as_commit(&commit, repo, commit_id);
5616 if (err)
5617 goto done;
5620 * The root is opened here and will be closed when the view is closed.
5621 * Any visited subtrees and their path-wise parents are opened and
5622 * closed on demand.
5624 err = got_object_open_as_tree(&s->root, repo,
5625 got_object_commit_get_tree_id(commit));
5626 if (err)
5627 goto done;
5628 s->tree = s->root;
5630 err = got_object_id_str(&commit_id_str, commit_id);
5631 if (err != NULL)
5632 goto done;
5634 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5635 err = got_error_from_errno("asprintf");
5636 goto done;
5639 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5640 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5641 if (head_ref_name) {
5642 s->head_ref_name = strdup(head_ref_name);
5643 if (s->head_ref_name == NULL) {
5644 err = got_error_from_errno("strdup");
5645 goto done;
5648 s->repo = repo;
5650 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5651 err = add_color(&s->colors, "\\$$",
5652 TOG_COLOR_TREE_SUBMODULE,
5653 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5654 if (err)
5655 goto done;
5656 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5657 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5658 if (err)
5659 goto done;
5660 err = add_color(&s->colors, "/$",
5661 TOG_COLOR_TREE_DIRECTORY,
5662 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5663 if (err)
5664 goto done;
5666 err = add_color(&s->colors, "\\*$",
5667 TOG_COLOR_TREE_EXECUTABLE,
5668 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5669 if (err)
5670 goto done;
5672 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5673 get_color_value("TOG_COLOR_COMMIT"));
5674 if (err)
5675 goto done;
5678 view->show = show_tree_view;
5679 view->input = input_tree_view;
5680 view->close = close_tree_view;
5681 view->search_start = search_start_tree_view;
5682 view->search_next = search_next_tree_view;
5683 done:
5684 free(commit_id_str);
5685 if (commit)
5686 got_object_commit_close(commit);
5687 if (err)
5688 close_tree_view(view);
5689 return err;
5692 static const struct got_error *
5693 close_tree_view(struct tog_view *view)
5695 struct tog_tree_view_state *s = &view->state.tree;
5697 free_colors(&s->colors);
5698 free(s->tree_label);
5699 s->tree_label = NULL;
5700 free(s->commit_id);
5701 s->commit_id = NULL;
5702 free(s->head_ref_name);
5703 s->head_ref_name = NULL;
5704 while (!TAILQ_EMPTY(&s->parents)) {
5705 struct tog_parent_tree *parent;
5706 parent = TAILQ_FIRST(&s->parents);
5707 TAILQ_REMOVE(&s->parents, parent, entry);
5708 if (parent->tree != s->root)
5709 got_object_tree_close(parent->tree);
5710 free(parent);
5713 if (s->tree != NULL && s->tree != s->root)
5714 got_object_tree_close(s->tree);
5715 if (s->root)
5716 got_object_tree_close(s->root);
5717 return NULL;
5720 static const struct got_error *
5721 search_start_tree_view(struct tog_view *view)
5723 struct tog_tree_view_state *s = &view->state.tree;
5725 s->matched_entry = NULL;
5726 return NULL;
5729 static int
5730 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5732 regmatch_t regmatch;
5734 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5735 0) == 0;
5738 static const struct got_error *
5739 search_next_tree_view(struct tog_view *view)
5741 struct tog_tree_view_state *s = &view->state.tree;
5742 struct got_tree_entry *te = NULL;
5744 if (!view->searching) {
5745 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5746 return NULL;
5749 if (s->matched_entry) {
5750 if (view->searching == TOG_SEARCH_FORWARD) {
5751 if (s->selected_entry)
5752 te = got_tree_entry_get_next(s->tree,
5753 s->selected_entry);
5754 else
5755 te = got_object_tree_get_first_entry(s->tree);
5756 } else {
5757 if (s->selected_entry == NULL)
5758 te = got_object_tree_get_last_entry(s->tree);
5759 else
5760 te = got_tree_entry_get_prev(s->tree,
5761 s->selected_entry);
5763 } else {
5764 if (s->selected_entry)
5765 te = s->selected_entry;
5766 else if (view->searching == TOG_SEARCH_FORWARD)
5767 te = got_object_tree_get_first_entry(s->tree);
5768 else
5769 te = got_object_tree_get_last_entry(s->tree);
5772 while (1) {
5773 if (te == NULL) {
5774 if (s->matched_entry == NULL) {
5775 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5776 return NULL;
5778 if (view->searching == TOG_SEARCH_FORWARD)
5779 te = got_object_tree_get_first_entry(s->tree);
5780 else
5781 te = got_object_tree_get_last_entry(s->tree);
5784 if (match_tree_entry(te, &view->regex)) {
5785 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5786 s->matched_entry = te;
5787 break;
5790 if (view->searching == TOG_SEARCH_FORWARD)
5791 te = got_tree_entry_get_next(s->tree, te);
5792 else
5793 te = got_tree_entry_get_prev(s->tree, te);
5796 if (s->matched_entry) {
5797 s->first_displayed_entry = s->matched_entry;
5798 s->selected = 0;
5801 return NULL;
5804 static const struct got_error *
5805 show_tree_view(struct tog_view *view)
5807 const struct got_error *err = NULL;
5808 struct tog_tree_view_state *s = &view->state.tree;
5809 char *parent_path;
5811 err = tree_entry_path(&parent_path, &s->parents, NULL);
5812 if (err)
5813 return err;
5815 err = draw_tree_entries(view, parent_path);
5816 free(parent_path);
5818 view_vborder(view);
5819 return err;
5822 static const struct got_error *
5823 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5825 const struct got_error *err = NULL;
5826 struct tog_tree_view_state *s = &view->state.tree;
5827 struct tog_view *log_view, *ref_view;
5828 struct got_tree_entry *te;
5829 int begin_x = 0, n, nscroll = view->nlines - 3;
5831 switch (ch) {
5832 case 'i':
5833 s->show_ids = !s->show_ids;
5834 break;
5835 case 'l':
5836 if (!s->selected_entry)
5837 break;
5838 if (view_is_parent_view(view))
5839 begin_x = view_split_begin_x(view->begin_x);
5840 err = log_selected_tree_entry(&log_view, begin_x, s);
5841 view->focussed = 0;
5842 log_view->focussed = 1;
5843 if (view_is_parent_view(view)) {
5844 err = view_close_child(view);
5845 if (err)
5846 return err;
5847 view_set_child(view, log_view);
5848 view->focus_child = 1;
5849 } else
5850 *new_view = log_view;
5851 break;
5852 case 'r':
5853 if (view_is_parent_view(view))
5854 begin_x = view_split_begin_x(view->begin_x);
5855 ref_view = view_open(view->nlines, view->ncols,
5856 view->begin_y, begin_x, TOG_VIEW_REF);
5857 if (ref_view == NULL)
5858 return got_error_from_errno("view_open");
5859 err = open_ref_view(ref_view, s->repo);
5860 if (err) {
5861 view_close(ref_view);
5862 return err;
5864 view->focussed = 0;
5865 ref_view->focussed = 1;
5866 if (view_is_parent_view(view)) {
5867 err = view_close_child(view);
5868 if (err)
5869 return err;
5870 view_set_child(view, ref_view);
5871 view->focus_child = 1;
5872 } else
5873 *new_view = ref_view;
5874 break;
5875 case 'g':
5876 case KEY_HOME:
5877 s->selected = 0;
5878 if (s->tree == s->root)
5879 s->first_displayed_entry =
5880 got_object_tree_get_first_entry(s->tree);
5881 else
5882 s->first_displayed_entry = NULL;
5883 break;
5884 case 'G':
5885 case KEY_END:
5886 s->selected = 0;
5887 te = got_object_tree_get_last_entry(s->tree);
5888 for (n = 0; n < view->nlines - 3; n++) {
5889 if (te == NULL) {
5890 if(s->tree != s->root) {
5891 s->first_displayed_entry = NULL;
5892 n++;
5894 break;
5896 s->first_displayed_entry = te;
5897 te = got_tree_entry_get_prev(s->tree, te);
5899 if (n > 0)
5900 s->selected = n - 1;
5901 break;
5902 case 'k':
5903 case KEY_UP:
5904 case CTRL('p'):
5905 if (s->selected > 0) {
5906 s->selected--;
5907 break;
5909 tree_scroll_up(s, 1);
5910 break;
5911 case CTRL('u'):
5912 case 'u':
5913 nscroll /= 2;
5914 /* FALL THROUGH */
5915 case KEY_PPAGE:
5916 case CTRL('b'):
5917 if (s->tree == s->root) {
5918 if (got_object_tree_get_first_entry(s->tree) ==
5919 s->first_displayed_entry)
5920 s->selected -= MIN(s->selected, nscroll);
5921 } else {
5922 if (s->first_displayed_entry == NULL)
5923 s->selected -= MIN(s->selected, nscroll);
5925 tree_scroll_up(s, MAX(0, nscroll));
5926 break;
5927 case 'j':
5928 case KEY_DOWN:
5929 case CTRL('n'):
5930 if (s->selected < s->ndisplayed - 1) {
5931 s->selected++;
5932 break;
5934 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5935 == NULL)
5936 /* can't scroll any further */
5937 break;
5938 tree_scroll_down(s, 1);
5939 break;
5940 case CTRL('d'):
5941 case 'd':
5942 nscroll /= 2;
5943 /* FALL THROUGH */
5944 case KEY_NPAGE:
5945 case CTRL('f'):
5946 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5947 == NULL) {
5948 /* can't scroll any further; move cursor down */
5949 if (s->selected < s->ndisplayed - 1)
5950 s->selected += MIN(nscroll,
5951 s->ndisplayed - s->selected - 1);
5952 break;
5954 tree_scroll_down(s, nscroll);
5955 break;
5956 case KEY_ENTER:
5957 case '\r':
5958 case KEY_BACKSPACE:
5959 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5960 struct tog_parent_tree *parent;
5961 /* user selected '..' */
5962 if (s->tree == s->root)
5963 break;
5964 parent = TAILQ_FIRST(&s->parents);
5965 TAILQ_REMOVE(&s->parents, parent,
5966 entry);
5967 got_object_tree_close(s->tree);
5968 s->tree = parent->tree;
5969 s->first_displayed_entry =
5970 parent->first_displayed_entry;
5971 s->selected_entry =
5972 parent->selected_entry;
5973 s->selected = parent->selected;
5974 free(parent);
5975 } else if (S_ISDIR(got_tree_entry_get_mode(
5976 s->selected_entry))) {
5977 struct got_tree_object *subtree;
5978 err = got_object_open_as_tree(&subtree, s->repo,
5979 got_tree_entry_get_id(s->selected_entry));
5980 if (err)
5981 break;
5982 err = tree_view_visit_subtree(s, subtree);
5983 if (err) {
5984 got_object_tree_close(subtree);
5985 break;
5987 } else if (S_ISREG(got_tree_entry_get_mode(
5988 s->selected_entry))) {
5989 struct tog_view *blame_view;
5990 int begin_x = view_is_parent_view(view) ?
5991 view_split_begin_x(view->begin_x) : 0;
5993 err = blame_tree_entry(&blame_view, begin_x,
5994 s->selected_entry, &s->parents,
5995 s->commit_id, s->repo);
5996 if (err)
5997 break;
5998 view->focussed = 0;
5999 blame_view->focussed = 1;
6000 if (view_is_parent_view(view)) {
6001 err = view_close_child(view);
6002 if (err)
6003 return err;
6004 view_set_child(view, blame_view);
6005 view->focus_child = 1;
6006 } else
6007 *new_view = blame_view;
6009 break;
6010 case KEY_RESIZE:
6011 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6012 s->selected = view->nlines - 4;
6013 break;
6014 default:
6015 break;
6018 return err;
6021 __dead static void
6022 usage_tree(void)
6024 endwin();
6025 fprintf(stderr,
6026 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6027 getprogname());
6028 exit(1);
6031 static const struct got_error *
6032 cmd_tree(int argc, char *argv[])
6034 const struct got_error *error;
6035 struct got_repository *repo = NULL;
6036 struct got_worktree *worktree = NULL;
6037 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6038 struct got_object_id *commit_id = NULL;
6039 struct got_commit_object *commit = NULL;
6040 const char *commit_id_arg = NULL;
6041 char *label = NULL;
6042 struct got_reference *ref = NULL;
6043 const char *head_ref_name = NULL;
6044 int ch;
6045 struct tog_view *view;
6046 int *pack_fds = NULL;
6048 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6049 switch (ch) {
6050 case 'c':
6051 commit_id_arg = optarg;
6052 break;
6053 case 'r':
6054 repo_path = realpath(optarg, NULL);
6055 if (repo_path == NULL)
6056 return got_error_from_errno2("realpath",
6057 optarg);
6058 break;
6059 default:
6060 usage_tree();
6061 /* NOTREACHED */
6065 argc -= optind;
6066 argv += optind;
6068 if (argc > 1)
6069 usage_tree();
6071 error = got_repo_pack_fds_open(&pack_fds);
6072 if (error != NULL)
6073 goto done;
6075 if (repo_path == NULL) {
6076 cwd = getcwd(NULL, 0);
6077 if (cwd == NULL)
6078 return got_error_from_errno("getcwd");
6079 error = got_worktree_open(&worktree, cwd);
6080 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6081 goto done;
6082 if (worktree)
6083 repo_path =
6084 strdup(got_worktree_get_repo_path(worktree));
6085 else
6086 repo_path = strdup(cwd);
6087 if (repo_path == NULL) {
6088 error = got_error_from_errno("strdup");
6089 goto done;
6093 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6094 if (error != NULL)
6095 goto done;
6097 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6098 repo, worktree);
6099 if (error)
6100 goto done;
6102 init_curses();
6104 error = apply_unveil(got_repo_get_path(repo), NULL);
6105 if (error)
6106 goto done;
6108 error = tog_load_refs(repo, 0);
6109 if (error)
6110 goto done;
6112 if (commit_id_arg == NULL) {
6113 error = got_repo_match_object_id(&commit_id, &label,
6114 worktree ? got_worktree_get_head_ref_name(worktree) :
6115 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6116 if (error)
6117 goto done;
6118 head_ref_name = label;
6119 } else {
6120 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6121 if (error == NULL)
6122 head_ref_name = got_ref_get_name(ref);
6123 else if (error->code != GOT_ERR_NOT_REF)
6124 goto done;
6125 error = got_repo_match_object_id(&commit_id, NULL,
6126 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6127 if (error)
6128 goto done;
6131 error = got_object_open_as_commit(&commit, repo, commit_id);
6132 if (error)
6133 goto done;
6135 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6136 if (view == NULL) {
6137 error = got_error_from_errno("view_open");
6138 goto done;
6140 error = open_tree_view(view, commit_id, head_ref_name, repo);
6141 if (error)
6142 goto done;
6143 if (!got_path_is_root_dir(in_repo_path)) {
6144 error = tree_view_walk_path(&view->state.tree, commit,
6145 in_repo_path);
6146 if (error)
6147 goto done;
6150 if (worktree) {
6151 /* Release work tree lock. */
6152 got_worktree_close(worktree);
6153 worktree = NULL;
6155 error = view_loop(view);
6156 done:
6157 free(repo_path);
6158 free(cwd);
6159 free(commit_id);
6160 free(label);
6161 if (ref)
6162 got_ref_close(ref);
6163 if (repo) {
6164 const struct got_error *close_err = got_repo_close(repo);
6165 if (error == NULL)
6166 error = close_err;
6168 if (pack_fds) {
6169 const struct got_error *pack_err =
6170 got_repo_pack_fds_close(pack_fds);
6171 if (error == NULL)
6172 error = pack_err;
6174 tog_free_refs();
6175 return error;
6178 static const struct got_error *
6179 ref_view_load_refs(struct tog_ref_view_state *s)
6181 struct got_reflist_entry *sre;
6182 struct tog_reflist_entry *re;
6184 s->nrefs = 0;
6185 TAILQ_FOREACH(sre, &tog_refs, entry) {
6186 if (strncmp(got_ref_get_name(sre->ref),
6187 "refs/got/", 9) == 0 &&
6188 strncmp(got_ref_get_name(sre->ref),
6189 "refs/got/backup/", 16) != 0)
6190 continue;
6192 re = malloc(sizeof(*re));
6193 if (re == NULL)
6194 return got_error_from_errno("malloc");
6196 re->ref = got_ref_dup(sre->ref);
6197 if (re->ref == NULL)
6198 return got_error_from_errno("got_ref_dup");
6199 re->idx = s->nrefs++;
6200 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6203 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6204 return NULL;
6207 void
6208 ref_view_free_refs(struct tog_ref_view_state *s)
6210 struct tog_reflist_entry *re;
6212 while (!TAILQ_EMPTY(&s->refs)) {
6213 re = TAILQ_FIRST(&s->refs);
6214 TAILQ_REMOVE(&s->refs, re, entry);
6215 got_ref_close(re->ref);
6216 free(re);
6220 static const struct got_error *
6221 open_ref_view(struct tog_view *view, struct got_repository *repo)
6223 const struct got_error *err = NULL;
6224 struct tog_ref_view_state *s = &view->state.ref;
6226 s->selected_entry = 0;
6227 s->repo = repo;
6229 TAILQ_INIT(&s->refs);
6230 STAILQ_INIT(&s->colors);
6232 err = ref_view_load_refs(s);
6233 if (err)
6234 return err;
6236 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6237 err = add_color(&s->colors, "^refs/heads/",
6238 TOG_COLOR_REFS_HEADS,
6239 get_color_value("TOG_COLOR_REFS_HEADS"));
6240 if (err)
6241 goto done;
6243 err = add_color(&s->colors, "^refs/tags/",
6244 TOG_COLOR_REFS_TAGS,
6245 get_color_value("TOG_COLOR_REFS_TAGS"));
6246 if (err)
6247 goto done;
6249 err = add_color(&s->colors, "^refs/remotes/",
6250 TOG_COLOR_REFS_REMOTES,
6251 get_color_value("TOG_COLOR_REFS_REMOTES"));
6252 if (err)
6253 goto done;
6255 err = add_color(&s->colors, "^refs/got/backup/",
6256 TOG_COLOR_REFS_BACKUP,
6257 get_color_value("TOG_COLOR_REFS_BACKUP"));
6258 if (err)
6259 goto done;
6262 view->show = show_ref_view;
6263 view->input = input_ref_view;
6264 view->close = close_ref_view;
6265 view->search_start = search_start_ref_view;
6266 view->search_next = search_next_ref_view;
6267 done:
6268 if (err)
6269 free_colors(&s->colors);
6270 return err;
6273 static const struct got_error *
6274 close_ref_view(struct tog_view *view)
6276 struct tog_ref_view_state *s = &view->state.ref;
6278 ref_view_free_refs(s);
6279 free_colors(&s->colors);
6281 return NULL;
6284 static const struct got_error *
6285 resolve_reflist_entry(struct got_object_id **commit_id,
6286 struct tog_reflist_entry *re, struct got_repository *repo)
6288 const struct got_error *err = NULL;
6289 struct got_object_id *obj_id;
6290 struct got_tag_object *tag = NULL;
6291 int obj_type;
6293 *commit_id = NULL;
6295 err = got_ref_resolve(&obj_id, repo, re->ref);
6296 if (err)
6297 return err;
6299 err = got_object_get_type(&obj_type, repo, obj_id);
6300 if (err)
6301 goto done;
6303 switch (obj_type) {
6304 case GOT_OBJ_TYPE_COMMIT:
6305 *commit_id = obj_id;
6306 break;
6307 case GOT_OBJ_TYPE_TAG:
6308 err = got_object_open_as_tag(&tag, repo, obj_id);
6309 if (err)
6310 goto done;
6311 free(obj_id);
6312 err = got_object_get_type(&obj_type, repo,
6313 got_object_tag_get_object_id(tag));
6314 if (err)
6315 goto done;
6316 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6317 err = got_error(GOT_ERR_OBJ_TYPE);
6318 goto done;
6320 *commit_id = got_object_id_dup(
6321 got_object_tag_get_object_id(tag));
6322 if (*commit_id == NULL) {
6323 err = got_error_from_errno("got_object_id_dup");
6324 goto done;
6326 break;
6327 default:
6328 err = got_error(GOT_ERR_OBJ_TYPE);
6329 break;
6332 done:
6333 if (tag)
6334 got_object_tag_close(tag);
6335 if (err) {
6336 free(*commit_id);
6337 *commit_id = NULL;
6339 return err;
6342 static const struct got_error *
6343 log_ref_entry(struct tog_view **new_view, int begin_x,
6344 struct tog_reflist_entry *re, struct got_repository *repo)
6346 struct tog_view *log_view;
6347 const struct got_error *err = NULL;
6348 struct got_object_id *commit_id = NULL;
6350 *new_view = NULL;
6352 err = resolve_reflist_entry(&commit_id, re, repo);
6353 if (err) {
6354 if (err->code != GOT_ERR_OBJ_TYPE)
6355 return err;
6356 else
6357 return NULL;
6360 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6361 if (log_view == NULL) {
6362 err = got_error_from_errno("view_open");
6363 goto done;
6366 err = open_log_view(log_view, commit_id, repo,
6367 got_ref_get_name(re->ref), "", 0);
6368 done:
6369 if (err)
6370 view_close(log_view);
6371 else
6372 *new_view = log_view;
6373 free(commit_id);
6374 return err;
6377 static void
6378 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6380 struct tog_reflist_entry *re;
6381 int i = 0;
6383 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6384 return;
6386 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6387 while (i++ < maxscroll) {
6388 if (re == NULL)
6389 break;
6390 s->first_displayed_entry = re;
6391 re = TAILQ_PREV(re, tog_reflist_head, entry);
6395 static void
6396 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6398 struct tog_reflist_entry *next, *last;
6399 int n = 0;
6401 if (s->first_displayed_entry)
6402 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6403 else
6404 next = TAILQ_FIRST(&s->refs);
6406 last = s->last_displayed_entry;
6407 while (next && last && n++ < maxscroll) {
6408 last = TAILQ_NEXT(last, entry);
6409 if (last) {
6410 s->first_displayed_entry = next;
6411 next = TAILQ_NEXT(next, entry);
6416 static const struct got_error *
6417 search_start_ref_view(struct tog_view *view)
6419 struct tog_ref_view_state *s = &view->state.ref;
6421 s->matched_entry = NULL;
6422 return NULL;
6425 static int
6426 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6428 regmatch_t regmatch;
6430 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6431 0) == 0;
6434 static const struct got_error *
6435 search_next_ref_view(struct tog_view *view)
6437 struct tog_ref_view_state *s = &view->state.ref;
6438 struct tog_reflist_entry *re = NULL;
6440 if (!view->searching) {
6441 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6442 return NULL;
6445 if (s->matched_entry) {
6446 if (view->searching == TOG_SEARCH_FORWARD) {
6447 if (s->selected_entry)
6448 re = TAILQ_NEXT(s->selected_entry, entry);
6449 else
6450 re = TAILQ_PREV(s->selected_entry,
6451 tog_reflist_head, entry);
6452 } else {
6453 if (s->selected_entry == NULL)
6454 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6455 else
6456 re = TAILQ_PREV(s->selected_entry,
6457 tog_reflist_head, entry);
6459 } else {
6460 if (s->selected_entry)
6461 re = s->selected_entry;
6462 else if (view->searching == TOG_SEARCH_FORWARD)
6463 re = TAILQ_FIRST(&s->refs);
6464 else
6465 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6468 while (1) {
6469 if (re == NULL) {
6470 if (s->matched_entry == NULL) {
6471 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6472 return NULL;
6474 if (view->searching == TOG_SEARCH_FORWARD)
6475 re = TAILQ_FIRST(&s->refs);
6476 else
6477 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6480 if (match_reflist_entry(re, &view->regex)) {
6481 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6482 s->matched_entry = re;
6483 break;
6486 if (view->searching == TOG_SEARCH_FORWARD)
6487 re = TAILQ_NEXT(re, entry);
6488 else
6489 re = TAILQ_PREV(re, tog_reflist_head, entry);
6492 if (s->matched_entry) {
6493 s->first_displayed_entry = s->matched_entry;
6494 s->selected = 0;
6497 return NULL;
6500 static const struct got_error *
6501 show_ref_view(struct tog_view *view)
6503 const struct got_error *err = NULL;
6504 struct tog_ref_view_state *s = &view->state.ref;
6505 struct tog_reflist_entry *re;
6506 char *line = NULL;
6507 wchar_t *wline;
6508 struct tog_color *tc;
6509 int width, n;
6510 int limit = view->nlines;
6512 werase(view->window);
6514 s->ndisplayed = 0;
6516 if (limit == 0)
6517 return NULL;
6519 re = s->first_displayed_entry;
6521 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6522 s->nrefs) == -1)
6523 return got_error_from_errno("asprintf");
6525 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6526 if (err) {
6527 free(line);
6528 return err;
6530 if (view_needs_focus_indication(view))
6531 wstandout(view->window);
6532 waddwstr(view->window, wline);
6533 if (view_needs_focus_indication(view))
6534 wstandend(view->window);
6535 free(wline);
6536 wline = NULL;
6537 free(line);
6538 line = NULL;
6539 if (width < view->ncols - 1)
6540 waddch(view->window, '\n');
6541 if (--limit <= 0)
6542 return NULL;
6544 n = 0;
6545 while (re && limit > 0) {
6546 char *line = NULL;
6547 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6549 if (s->show_date) {
6550 struct got_commit_object *ci;
6551 struct got_tag_object *tag;
6552 struct got_object_id *id;
6553 struct tm tm;
6554 time_t t;
6556 err = got_ref_resolve(&id, s->repo, re->ref);
6557 if (err)
6558 return err;
6559 err = got_object_open_as_tag(&tag, s->repo, id);
6560 if (err) {
6561 if (err->code != GOT_ERR_OBJ_TYPE) {
6562 free(id);
6563 return err;
6565 err = got_object_open_as_commit(&ci, s->repo,
6566 id);
6567 if (err) {
6568 free(id);
6569 return err;
6571 t = got_object_commit_get_committer_time(ci);
6572 got_object_commit_close(ci);
6573 } else {
6574 t = got_object_tag_get_tagger_time(tag);
6575 got_object_tag_close(tag);
6577 free(id);
6578 if (gmtime_r(&t, &tm) == NULL)
6579 return got_error_from_errno("gmtime_r");
6580 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6581 return got_error(GOT_ERR_NO_SPACE);
6583 if (got_ref_is_symbolic(re->ref)) {
6584 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6585 ymd : "", got_ref_get_name(re->ref),
6586 got_ref_get_symref_target(re->ref)) == -1)
6587 return got_error_from_errno("asprintf");
6588 } else if (s->show_ids) {
6589 struct got_object_id *id;
6590 char *id_str;
6591 err = got_ref_resolve(&id, s->repo, re->ref);
6592 if (err)
6593 return err;
6594 err = got_object_id_str(&id_str, id);
6595 if (err) {
6596 free(id);
6597 return err;
6599 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6600 got_ref_get_name(re->ref), id_str) == -1) {
6601 err = got_error_from_errno("asprintf");
6602 free(id);
6603 free(id_str);
6604 return err;
6606 free(id);
6607 free(id_str);
6608 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6609 got_ref_get_name(re->ref)) == -1)
6610 return got_error_from_errno("asprintf");
6612 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6613 0, 0);
6614 if (err) {
6615 free(line);
6616 return err;
6618 if (n == s->selected) {
6619 if (view->focussed)
6620 wstandout(view->window);
6621 s->selected_entry = re;
6623 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6624 if (tc)
6625 wattr_on(view->window,
6626 COLOR_PAIR(tc->colorpair), NULL);
6627 waddwstr(view->window, wline);
6628 if (tc)
6629 wattr_off(view->window,
6630 COLOR_PAIR(tc->colorpair), NULL);
6631 if (width < view->ncols - 1)
6632 waddch(view->window, '\n');
6633 if (n == s->selected && view->focussed)
6634 wstandend(view->window);
6635 free(line);
6636 free(wline);
6637 wline = NULL;
6638 n++;
6639 s->ndisplayed++;
6640 s->last_displayed_entry = re;
6642 limit--;
6643 re = TAILQ_NEXT(re, entry);
6646 view_vborder(view);
6647 return err;
6650 static const struct got_error *
6651 browse_ref_tree(struct tog_view **new_view, int begin_x,
6652 struct tog_reflist_entry *re, struct got_repository *repo)
6654 const struct got_error *err = NULL;
6655 struct got_object_id *commit_id = NULL;
6656 struct tog_view *tree_view;
6658 *new_view = NULL;
6660 err = resolve_reflist_entry(&commit_id, re, repo);
6661 if (err) {
6662 if (err->code != GOT_ERR_OBJ_TYPE)
6663 return err;
6664 else
6665 return NULL;
6669 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6670 if (tree_view == NULL) {
6671 err = got_error_from_errno("view_open");
6672 goto done;
6675 err = open_tree_view(tree_view, commit_id,
6676 got_ref_get_name(re->ref), repo);
6677 if (err)
6678 goto done;
6680 *new_view = tree_view;
6681 done:
6682 free(commit_id);
6683 return err;
6685 static const struct got_error *
6686 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6688 const struct got_error *err = NULL;
6689 struct tog_ref_view_state *s = &view->state.ref;
6690 struct tog_view *log_view, *tree_view;
6691 struct tog_reflist_entry *re;
6692 int begin_x = 0, n, nscroll = view->nlines - 1;
6694 switch (ch) {
6695 case 'i':
6696 s->show_ids = !s->show_ids;
6697 break;
6698 case 'm':
6699 s->show_date = !s->show_date;
6700 break;
6701 case 'o':
6702 s->sort_by_date = !s->sort_by_date;
6703 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6704 got_ref_cmp_by_commit_timestamp_descending :
6705 tog_ref_cmp_by_name, s->repo);
6706 if (err)
6707 break;
6708 got_reflist_object_id_map_free(tog_refs_idmap);
6709 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6710 &tog_refs, s->repo);
6711 if (err)
6712 break;
6713 ref_view_free_refs(s);
6714 err = ref_view_load_refs(s);
6715 break;
6716 case KEY_ENTER:
6717 case '\r':
6718 if (!s->selected_entry)
6719 break;
6720 if (view_is_parent_view(view))
6721 begin_x = view_split_begin_x(view->begin_x);
6722 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6723 s->repo);
6724 view->focussed = 0;
6725 log_view->focussed = 1;
6726 if (view_is_parent_view(view)) {
6727 err = view_close_child(view);
6728 if (err)
6729 return err;
6730 view_set_child(view, log_view);
6731 view->focus_child = 1;
6732 } else
6733 *new_view = log_view;
6734 break;
6735 case 't':
6736 if (!s->selected_entry)
6737 break;
6738 if (view_is_parent_view(view))
6739 begin_x = view_split_begin_x(view->begin_x);
6740 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6741 s->repo);
6742 if (err || tree_view == NULL)
6743 break;
6744 view->focussed = 0;
6745 tree_view->focussed = 1;
6746 if (view_is_parent_view(view)) {
6747 err = view_close_child(view);
6748 if (err)
6749 return err;
6750 view_set_child(view, tree_view);
6751 view->focus_child = 1;
6752 } else
6753 *new_view = tree_view;
6754 break;
6755 case 'g':
6756 case KEY_HOME:
6757 s->selected = 0;
6758 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6759 break;
6760 case 'G':
6761 case KEY_END:
6762 s->selected = 0;
6763 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6764 for (n = 0; n < view->nlines - 1; n++) {
6765 if (re == NULL)
6766 break;
6767 s->first_displayed_entry = re;
6768 re = TAILQ_PREV(re, tog_reflist_head, entry);
6770 if (n > 0)
6771 s->selected = n - 1;
6772 break;
6773 case 'k':
6774 case KEY_UP:
6775 case CTRL('p'):
6776 if (s->selected > 0) {
6777 s->selected--;
6778 break;
6780 ref_scroll_up(s, 1);
6781 break;
6782 case CTRL('u'):
6783 case 'u':
6784 nscroll /= 2;
6785 /* FALL THROUGH */
6786 case KEY_PPAGE:
6787 case CTRL('b'):
6788 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6789 s->selected -= MIN(nscroll, s->selected);
6790 ref_scroll_up(s, MAX(0, nscroll));
6791 break;
6792 case 'j':
6793 case KEY_DOWN:
6794 case CTRL('n'):
6795 if (s->selected < s->ndisplayed - 1) {
6796 s->selected++;
6797 break;
6799 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6800 /* can't scroll any further */
6801 break;
6802 ref_scroll_down(s, 1);
6803 break;
6804 case CTRL('d'):
6805 case 'd':
6806 nscroll /= 2;
6807 /* FALL THROUGH */
6808 case KEY_NPAGE:
6809 case CTRL('f'):
6810 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6811 /* can't scroll any further; move cursor down */
6812 if (s->selected < s->ndisplayed - 1)
6813 s->selected += MIN(nscroll,
6814 s->ndisplayed - s->selected - 1);
6815 break;
6817 ref_scroll_down(s, nscroll);
6818 break;
6819 case CTRL('l'):
6820 tog_free_refs();
6821 err = tog_load_refs(s->repo, s->sort_by_date);
6822 if (err)
6823 break;
6824 ref_view_free_refs(s);
6825 err = ref_view_load_refs(s);
6826 break;
6827 case KEY_RESIZE:
6828 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6829 s->selected = view->nlines - 2;
6830 break;
6831 default:
6832 break;
6835 return err;
6838 __dead static void
6839 usage_ref(void)
6841 endwin();
6842 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6843 getprogname());
6844 exit(1);
6847 static const struct got_error *
6848 cmd_ref(int argc, char *argv[])
6850 const struct got_error *error;
6851 struct got_repository *repo = NULL;
6852 struct got_worktree *worktree = NULL;
6853 char *cwd = NULL, *repo_path = NULL;
6854 int ch;
6855 struct tog_view *view;
6856 int *pack_fds = NULL;
6858 while ((ch = getopt(argc, argv, "r:")) != -1) {
6859 switch (ch) {
6860 case 'r':
6861 repo_path = realpath(optarg, NULL);
6862 if (repo_path == NULL)
6863 return got_error_from_errno2("realpath",
6864 optarg);
6865 break;
6866 default:
6867 usage_ref();
6868 /* NOTREACHED */
6872 argc -= optind;
6873 argv += optind;
6875 if (argc > 1)
6876 usage_ref();
6878 error = got_repo_pack_fds_open(&pack_fds);
6879 if (error != NULL)
6880 goto done;
6882 if (repo_path == NULL) {
6883 cwd = getcwd(NULL, 0);
6884 if (cwd == NULL)
6885 return got_error_from_errno("getcwd");
6886 error = got_worktree_open(&worktree, cwd);
6887 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6888 goto done;
6889 if (worktree)
6890 repo_path =
6891 strdup(got_worktree_get_repo_path(worktree));
6892 else
6893 repo_path = strdup(cwd);
6894 if (repo_path == NULL) {
6895 error = got_error_from_errno("strdup");
6896 goto done;
6900 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6901 if (error != NULL)
6902 goto done;
6904 init_curses();
6906 error = apply_unveil(got_repo_get_path(repo), NULL);
6907 if (error)
6908 goto done;
6910 error = tog_load_refs(repo, 0);
6911 if (error)
6912 goto done;
6914 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6915 if (view == NULL) {
6916 error = got_error_from_errno("view_open");
6917 goto done;
6920 error = open_ref_view(view, repo);
6921 if (error)
6922 goto done;
6924 if (worktree) {
6925 /* Release work tree lock. */
6926 got_worktree_close(worktree);
6927 worktree = NULL;
6929 error = view_loop(view);
6930 done:
6931 free(repo_path);
6932 free(cwd);
6933 if (repo) {
6934 const struct got_error *close_err = got_repo_close(repo);
6935 if (close_err)
6936 error = close_err;
6938 if (pack_fds) {
6939 const struct got_error *pack_err =
6940 got_repo_pack_fds_close(pack_fds);
6941 if (error == NULL)
6942 error = pack_err;
6944 tog_free_refs();
6945 return error;
6948 static void
6949 list_commands(FILE *fp)
6951 size_t i;
6953 fprintf(fp, "commands:");
6954 for (i = 0; i < nitems(tog_commands); i++) {
6955 const struct tog_cmd *cmd = &tog_commands[i];
6956 fprintf(fp, " %s", cmd->name);
6958 fputc('\n', fp);
6961 __dead static void
6962 usage(int hflag, int status)
6964 FILE *fp = (status == 0) ? stdout : stderr;
6966 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6967 getprogname());
6968 if (hflag) {
6969 fprintf(fp, "lazy usage: %s path\n", getprogname());
6970 list_commands(fp);
6972 exit(status);
6975 static char **
6976 make_argv(int argc, ...)
6978 va_list ap;
6979 char **argv;
6980 int i;
6982 va_start(ap, argc);
6984 argv = calloc(argc, sizeof(char *));
6985 if (argv == NULL)
6986 err(1, "calloc");
6987 for (i = 0; i < argc; i++) {
6988 argv[i] = strdup(va_arg(ap, char *));
6989 if (argv[i] == NULL)
6990 err(1, "strdup");
6993 va_end(ap);
6994 return argv;
6998 * Try to convert 'tog path' into a 'tog log path' command.
6999 * The user could simply have mistyped the command rather than knowingly
7000 * provided a path. So check whether argv[0] can in fact be resolved
7001 * to a path in the HEAD commit and print a special error if not.
7002 * This hack is for mpi@ <3
7004 static const struct got_error *
7005 tog_log_with_path(int argc, char *argv[])
7007 const struct got_error *error = NULL, *close_err;
7008 const struct tog_cmd *cmd = NULL;
7009 struct got_repository *repo = NULL;
7010 struct got_worktree *worktree = NULL;
7011 struct got_object_id *commit_id = NULL, *id = NULL;
7012 struct got_commit_object *commit = NULL;
7013 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7014 char *commit_id_str = NULL, **cmd_argv = NULL;
7015 int *pack_fds = NULL;
7017 cwd = getcwd(NULL, 0);
7018 if (cwd == NULL)
7019 return got_error_from_errno("getcwd");
7021 error = got_repo_pack_fds_open(&pack_fds);
7022 if (error != NULL)
7023 goto done;
7025 error = got_worktree_open(&worktree, cwd);
7026 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7027 goto done;
7029 if (worktree)
7030 repo_path = strdup(got_worktree_get_repo_path(worktree));
7031 else
7032 repo_path = strdup(cwd);
7033 if (repo_path == NULL) {
7034 error = got_error_from_errno("strdup");
7035 goto done;
7038 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7039 if (error != NULL)
7040 goto done;
7042 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7043 repo, worktree);
7044 if (error)
7045 goto done;
7047 error = tog_load_refs(repo, 0);
7048 if (error)
7049 goto done;
7050 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7051 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7052 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7053 if (error)
7054 goto done;
7056 if (worktree) {
7057 got_worktree_close(worktree);
7058 worktree = NULL;
7061 error = got_object_open_as_commit(&commit, repo, commit_id);
7062 if (error)
7063 goto done;
7065 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7066 if (error) {
7067 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7068 goto done;
7069 fprintf(stderr, "%s: '%s' is no known command or path\n",
7070 getprogname(), argv[0]);
7071 usage(1, 1);
7072 /* not reached */
7075 close_err = got_repo_close(repo);
7076 if (error == NULL)
7077 error = close_err;
7078 repo = NULL;
7080 error = got_object_id_str(&commit_id_str, commit_id);
7081 if (error)
7082 goto done;
7084 cmd = &tog_commands[0]; /* log */
7085 argc = 4;
7086 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7087 error = cmd->cmd_main(argc, cmd_argv);
7088 done:
7089 if (repo) {
7090 close_err = got_repo_close(repo);
7091 if (error == NULL)
7092 error = close_err;
7094 if (commit)
7095 got_object_commit_close(commit);
7096 if (worktree)
7097 got_worktree_close(worktree);
7098 if (pack_fds) {
7099 const struct got_error *pack_err =
7100 got_repo_pack_fds_close(pack_fds);
7101 if (error == NULL)
7102 error = pack_err;
7104 free(id);
7105 free(commit_id_str);
7106 free(commit_id);
7107 free(cwd);
7108 free(repo_path);
7109 free(in_repo_path);
7110 if (cmd_argv) {
7111 int i;
7112 for (i = 0; i < argc; i++)
7113 free(cmd_argv[i]);
7114 free(cmd_argv);
7116 tog_free_refs();
7117 return error;
7120 int
7121 main(int argc, char *argv[])
7123 const struct got_error *error = NULL;
7124 const struct tog_cmd *cmd = NULL;
7125 int ch, hflag = 0, Vflag = 0;
7126 char **cmd_argv = NULL;
7127 static const struct option longopts[] = {
7128 { "version", no_argument, NULL, 'V' },
7129 { NULL, 0, NULL, 0}
7132 setlocale(LC_CTYPE, "");
7134 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7135 switch (ch) {
7136 case 'h':
7137 hflag = 1;
7138 break;
7139 case 'V':
7140 Vflag = 1;
7141 break;
7142 default:
7143 usage(hflag, 1);
7144 /* NOTREACHED */
7148 argc -= optind;
7149 argv += optind;
7150 optind = 1;
7151 optreset = 1;
7153 if (Vflag) {
7154 got_version_print_str();
7155 return 0;
7158 #ifndef PROFILE
7159 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7160 NULL) == -1)
7161 err(1, "pledge");
7162 #endif
7164 if (argc == 0) {
7165 if (hflag)
7166 usage(hflag, 0);
7167 /* Build an argument vector which runs a default command. */
7168 cmd = &tog_commands[0];
7169 argc = 1;
7170 cmd_argv = make_argv(argc, cmd->name);
7171 } else {
7172 size_t i;
7174 /* Did the user specify a command? */
7175 for (i = 0; i < nitems(tog_commands); i++) {
7176 if (strncmp(tog_commands[i].name, argv[0],
7177 strlen(argv[0])) == 0) {
7178 cmd = &tog_commands[i];
7179 break;
7184 if (cmd == NULL) {
7185 if (argc != 1)
7186 usage(0, 1);
7187 /* No command specified; try log with a path */
7188 error = tog_log_with_path(argc, argv);
7189 } else {
7190 if (hflag)
7191 cmd->cmd_usage();
7192 else
7193 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7196 endwin();
7197 putchar('\n');
7198 if (cmd_argv) {
7199 int i;
7200 for (i = 0; i < argc; i++)
7201 free(cmd_argv[i]);
7202 free(cmd_argv);
7205 if (error && error->code != GOT_ERR_CANCELLED)
7206 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7207 return 0;