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) * sizeof(char));
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 n += nb;
1245 dst = reallocarray(dst, n, sizeof(char));
1246 if (dst == NULL)
1247 return got_error_from_errno("reallocarray");
1248 memcpy(dst + sz, " ", nb);
1249 sz += nb;
1250 } else
1251 dst[sz++] = src[idx];
1252 ++idx;
1255 dst[sz] = '\0';
1256 *ptr = dst;
1257 return NULL;
1260 /* Format a line for display, ensuring that it won't overflow a width limit. */
1261 static const struct got_error *
1262 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1263 int col_tab_align, int expand)
1265 const struct got_error *err = NULL;
1266 int cols = 0;
1267 wchar_t *wline = NULL;
1268 char *exstr = NULL;
1269 size_t wlen;
1270 int i;
1272 *wlinep = NULL;
1273 *widthp = 0;
1275 if (expand) {
1276 err = expand_tab(&exstr, line);
1277 if (err)
1278 return err;
1281 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1282 free(exstr);
1283 if (err)
1284 return err;
1286 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1287 wline[wlen - 1] = L'\0';
1288 wlen--;
1290 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1291 wline[wlen - 1] = L'\0';
1292 wlen--;
1295 i = 0;
1296 while (i < wlen) {
1297 int width = wcwidth(wline[i]);
1299 if (width == 0) {
1300 i++;
1301 continue;
1304 if (width == 1 || width == 2) {
1305 if (cols + width > wlimit)
1306 break;
1307 cols += width;
1308 i++;
1309 } else if (width == -1) {
1310 if (wline[i] == L'\t') {
1311 width = TABSIZE -
1312 ((cols + col_tab_align) % TABSIZE);
1313 } else {
1314 width = 1;
1315 wline[i] = L'.';
1317 if (cols + width > wlimit)
1318 break;
1319 cols += width;
1320 i++;
1321 } else {
1322 err = got_error_from_errno("wcwidth");
1323 goto done;
1326 wline[i] = L'\0';
1327 if (widthp)
1328 *widthp = cols;
1329 done:
1330 if (err)
1331 free(wline);
1332 else
1333 *wlinep = wline;
1334 return err;
1337 static const struct got_error*
1338 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1339 struct got_object_id *id, struct got_repository *repo)
1341 static const struct got_error *err = NULL;
1342 struct got_reflist_entry *re;
1343 char *s;
1344 const char *name;
1346 *refs_str = NULL;
1348 TAILQ_FOREACH(re, refs, entry) {
1349 struct got_tag_object *tag = NULL;
1350 struct got_object_id *ref_id;
1351 int cmp;
1353 name = got_ref_get_name(re->ref);
1354 if (strcmp(name, GOT_REF_HEAD) == 0)
1355 continue;
1356 if (strncmp(name, "refs/", 5) == 0)
1357 name += 5;
1358 if (strncmp(name, "got/", 4) == 0 &&
1359 strncmp(name, "got/backup/", 11) != 0)
1360 continue;
1361 if (strncmp(name, "heads/", 6) == 0)
1362 name += 6;
1363 if (strncmp(name, "remotes/", 8) == 0) {
1364 name += 8;
1365 s = strstr(name, "/" GOT_REF_HEAD);
1366 if (s != NULL && s[strlen(s)] == '\0')
1367 continue;
1369 err = got_ref_resolve(&ref_id, repo, re->ref);
1370 if (err)
1371 break;
1372 if (strncmp(name, "tags/", 5) == 0) {
1373 err = got_object_open_as_tag(&tag, repo, ref_id);
1374 if (err) {
1375 if (err->code != GOT_ERR_OBJ_TYPE) {
1376 free(ref_id);
1377 break;
1379 /* Ref points at something other than a tag. */
1380 err = NULL;
1381 tag = NULL;
1384 cmp = got_object_id_cmp(tag ?
1385 got_object_tag_get_object_id(tag) : ref_id, id);
1386 free(ref_id);
1387 if (tag)
1388 got_object_tag_close(tag);
1389 if (cmp != 0)
1390 continue;
1391 s = *refs_str;
1392 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1393 s ? ", " : "", name) == -1) {
1394 err = got_error_from_errno("asprintf");
1395 free(s);
1396 *refs_str = NULL;
1397 break;
1399 free(s);
1402 return err;
1405 static const struct got_error *
1406 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1407 int col_tab_align)
1409 char *smallerthan;
1411 smallerthan = strchr(author, '<');
1412 if (smallerthan && smallerthan[1] != '\0')
1413 author = smallerthan + 1;
1414 author[strcspn(author, "@>")] = '\0';
1415 return format_line(wauthor, author_width, author, limit, col_tab_align,
1416 0);
1419 static const struct got_error *
1420 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1421 struct got_object_id *id, const size_t date_display_cols,
1422 int author_display_cols)
1424 struct tog_log_view_state *s = &view->state.log;
1425 const struct got_error *err = NULL;
1426 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1427 char *logmsg0 = NULL, *logmsg = NULL;
1428 char *author = NULL;
1429 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1430 int author_width, logmsg_width;
1431 char *newline, *line = NULL;
1432 int col, limit;
1433 const int avail = view->ncols;
1434 struct tm tm;
1435 time_t committer_time;
1436 struct tog_color *tc;
1438 committer_time = got_object_commit_get_committer_time(commit);
1439 if (gmtime_r(&committer_time, &tm) == NULL)
1440 return got_error_from_errno("gmtime_r");
1441 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1442 return got_error(GOT_ERR_NO_SPACE);
1444 if (avail <= date_display_cols)
1445 limit = MIN(sizeof(datebuf) - 1, avail);
1446 else
1447 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1448 tc = get_color(&s->colors, TOG_COLOR_DATE);
1449 if (tc)
1450 wattr_on(view->window,
1451 COLOR_PAIR(tc->colorpair), NULL);
1452 waddnstr(view->window, datebuf, limit);
1453 if (tc)
1454 wattr_off(view->window,
1455 COLOR_PAIR(tc->colorpair), NULL);
1456 col = limit;
1457 if (col > avail)
1458 goto done;
1460 if (avail >= 120) {
1461 char *id_str;
1462 err = got_object_id_str(&id_str, id);
1463 if (err)
1464 goto done;
1465 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1466 if (tc)
1467 wattr_on(view->window,
1468 COLOR_PAIR(tc->colorpair), NULL);
1469 wprintw(view->window, "%.8s ", id_str);
1470 if (tc)
1471 wattr_off(view->window,
1472 COLOR_PAIR(tc->colorpair), NULL);
1473 free(id_str);
1474 col += 9;
1475 if (col > avail)
1476 goto done;
1479 author = strdup(got_object_commit_get_author(commit));
1480 if (author == NULL) {
1481 err = got_error_from_errno("strdup");
1482 goto done;
1484 err = format_author(&wauthor, &author_width, author, avail - col, col);
1485 if (err)
1486 goto done;
1487 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1488 if (tc)
1489 wattr_on(view->window,
1490 COLOR_PAIR(tc->colorpair), NULL);
1491 waddwstr(view->window, wauthor);
1492 if (tc)
1493 wattr_off(view->window,
1494 COLOR_PAIR(tc->colorpair), NULL);
1495 col += author_width;
1496 while (col < avail && author_width < author_display_cols + 2) {
1497 waddch(view->window, ' ');
1498 col++;
1499 author_width++;
1501 if (col > avail)
1502 goto done;
1504 err = got_object_commit_get_logmsg(&logmsg0, commit);
1505 if (err)
1506 goto done;
1507 logmsg = logmsg0;
1508 while (*logmsg == '\n')
1509 logmsg++;
1510 newline = strchr(logmsg, '\n');
1511 if (newline)
1512 *newline = '\0';
1513 limit = view->x + avail - col;
1514 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col, 1);
1515 if (err)
1516 goto done;
1517 if (view->x < logmsg_width - 1)
1518 waddwstr(view->window, wlogmsg + view->x);
1519 else
1520 logmsg_width = 0;
1521 col += MAX(logmsg_width - view->x, 0);
1522 while (col < avail) {
1523 waddch(view->window, ' ');
1524 col++;
1526 done:
1527 free(logmsg0);
1528 free(wlogmsg);
1529 free(author);
1530 free(wauthor);
1531 free(line);
1532 return err;
1535 static struct commit_queue_entry *
1536 alloc_commit_queue_entry(struct got_commit_object *commit,
1537 struct got_object_id *id)
1539 struct commit_queue_entry *entry;
1541 entry = calloc(1, sizeof(*entry));
1542 if (entry == NULL)
1543 return NULL;
1545 entry->id = id;
1546 entry->commit = commit;
1547 return entry;
1550 static void
1551 pop_commit(struct commit_queue *commits)
1553 struct commit_queue_entry *entry;
1555 entry = TAILQ_FIRST(&commits->head);
1556 TAILQ_REMOVE(&commits->head, entry, entry);
1557 got_object_commit_close(entry->commit);
1558 commits->ncommits--;
1559 /* Don't free entry->id! It is owned by the commit graph. */
1560 free(entry);
1563 static void
1564 free_commits(struct commit_queue *commits)
1566 while (!TAILQ_EMPTY(&commits->head))
1567 pop_commit(commits);
1570 static const struct got_error *
1571 match_commit(int *have_match, struct got_object_id *id,
1572 struct got_commit_object *commit, regex_t *regex)
1574 const struct got_error *err = NULL;
1575 regmatch_t regmatch;
1576 char *id_str = NULL, *logmsg = NULL;
1578 *have_match = 0;
1580 err = got_object_id_str(&id_str, id);
1581 if (err)
1582 return err;
1584 err = got_object_commit_get_logmsg(&logmsg, commit);
1585 if (err)
1586 goto done;
1588 if (regexec(regex, got_object_commit_get_author(commit), 1,
1589 &regmatch, 0) == 0 ||
1590 regexec(regex, got_object_commit_get_committer(commit), 1,
1591 &regmatch, 0) == 0 ||
1592 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1593 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1594 *have_match = 1;
1595 done:
1596 free(id_str);
1597 free(logmsg);
1598 return err;
1601 static const struct got_error *
1602 queue_commits(struct tog_log_thread_args *a)
1604 const struct got_error *err = NULL;
1607 * We keep all commits open throughout the lifetime of the log
1608 * view in order to avoid having to re-fetch commits from disk
1609 * while updating the display.
1611 do {
1612 struct got_object_id *id;
1613 struct got_commit_object *commit;
1614 struct commit_queue_entry *entry;
1615 int errcode;
1617 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1618 NULL, NULL);
1619 if (err || id == NULL)
1620 break;
1622 err = got_object_open_as_commit(&commit, a->repo, id);
1623 if (err)
1624 break;
1625 entry = alloc_commit_queue_entry(commit, id);
1626 if (entry == NULL) {
1627 err = got_error_from_errno("alloc_commit_queue_entry");
1628 break;
1631 errcode = pthread_mutex_lock(&tog_mutex);
1632 if (errcode) {
1633 err = got_error_set_errno(errcode,
1634 "pthread_mutex_lock");
1635 break;
1638 entry->idx = a->commits->ncommits;
1639 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1640 a->commits->ncommits++;
1642 if (*a->searching == TOG_SEARCH_FORWARD &&
1643 !*a->search_next_done) {
1644 int have_match;
1645 err = match_commit(&have_match, id, commit, a->regex);
1646 if (err)
1647 break;
1648 if (have_match)
1649 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1652 errcode = pthread_mutex_unlock(&tog_mutex);
1653 if (errcode && err == NULL)
1654 err = got_error_set_errno(errcode,
1655 "pthread_mutex_unlock");
1656 if (err)
1657 break;
1658 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1660 return err;
1663 static void
1664 select_commit(struct tog_log_view_state *s)
1666 struct commit_queue_entry *entry;
1667 int ncommits = 0;
1669 entry = s->first_displayed_entry;
1670 while (entry) {
1671 if (ncommits == s->selected) {
1672 s->selected_entry = entry;
1673 break;
1675 entry = TAILQ_NEXT(entry, entry);
1676 ncommits++;
1680 static const struct got_error *
1681 draw_commits(struct tog_view *view)
1683 const struct got_error *err = NULL;
1684 struct tog_log_view_state *s = &view->state.log;
1685 struct commit_queue_entry *entry = s->selected_entry;
1686 const int limit = view->nlines;
1687 int width;
1688 int ncommits, author_cols = 4;
1689 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1690 char *refs_str = NULL;
1691 wchar_t *wline;
1692 struct tog_color *tc;
1693 static const size_t date_display_cols = 12;
1695 if (s->selected_entry &&
1696 !(view->searching && view->search_next_done == 0)) {
1697 struct got_reflist_head *refs;
1698 err = got_object_id_str(&id_str, s->selected_entry->id);
1699 if (err)
1700 return err;
1701 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1702 s->selected_entry->id);
1703 if (refs) {
1704 err = build_refs_str(&refs_str, refs,
1705 s->selected_entry->id, s->repo);
1706 if (err)
1707 goto done;
1711 if (s->thread_args.commits_needed == 0)
1712 halfdelay(10); /* disable fast refresh */
1714 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1715 if (asprintf(&ncommits_str, " [%d/%d] %s",
1716 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1717 (view->searching && !view->search_next_done) ?
1718 "searching..." : "loading...") == -1) {
1719 err = got_error_from_errno("asprintf");
1720 goto done;
1722 } else {
1723 const char *search_str = NULL;
1725 if (view->searching) {
1726 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1727 search_str = "no more matches";
1728 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1729 search_str = "no matches found";
1730 else if (!view->search_next_done)
1731 search_str = "searching...";
1734 if (asprintf(&ncommits_str, " [%d/%d] %s",
1735 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1736 search_str ? search_str :
1737 (refs_str ? refs_str : "")) == -1) {
1738 err = got_error_from_errno("asprintf");
1739 goto done;
1743 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1744 if (asprintf(&header, "commit %s %s%s",
1745 id_str ? id_str : "........................................",
1746 s->in_repo_path, ncommits_str) == -1) {
1747 err = got_error_from_errno("asprintf");
1748 header = NULL;
1749 goto done;
1751 } else if (asprintf(&header, "commit %s%s",
1752 id_str ? id_str : "........................................",
1753 ncommits_str) == -1) {
1754 err = got_error_from_errno("asprintf");
1755 header = NULL;
1756 goto done;
1758 err = format_line(&wline, &width, header, view->ncols, 0, 0);
1759 if (err)
1760 goto done;
1762 werase(view->window);
1764 if (view_needs_focus_indication(view))
1765 wstandout(view->window);
1766 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1767 if (tc)
1768 wattr_on(view->window,
1769 COLOR_PAIR(tc->colorpair), NULL);
1770 waddwstr(view->window, wline);
1771 if (tc)
1772 wattr_off(view->window,
1773 COLOR_PAIR(tc->colorpair), NULL);
1774 while (width < view->ncols) {
1775 waddch(view->window, ' ');
1776 width++;
1778 if (view_needs_focus_indication(view))
1779 wstandend(view->window);
1780 free(wline);
1781 if (limit <= 1)
1782 goto done;
1784 /* Grow author column size if necessary. */
1785 entry = s->first_displayed_entry;
1786 ncommits = 0;
1787 view->maxx = 0;
1788 while (entry) {
1789 char *author, *eol, *msg, *msg0;
1790 wchar_t *wauthor;
1791 int width;
1792 if (ncommits >= limit - 1)
1793 break;
1794 author = strdup(got_object_commit_get_author(entry->commit));
1795 if (author == NULL) {
1796 err = got_error_from_errno("strdup");
1797 goto done;
1799 err = format_author(&wauthor, &width, author, COLS,
1800 date_display_cols);
1801 if (author_cols < width)
1802 author_cols = width;
1803 free(wauthor);
1804 free(author);
1805 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1806 if (err)
1807 goto done;
1808 msg = msg0;
1809 while (*msg == '\n')
1810 ++msg;
1811 if ((eol = strchr(msg, '\n')))
1812 view->maxx = MAX(view->maxx, eol - msg);
1813 else
1814 view->maxx = MAX(view->maxx, strlen(msg));
1815 free(msg0);
1816 ncommits++;
1817 entry = TAILQ_NEXT(entry, entry);
1820 entry = s->first_displayed_entry;
1821 s->last_displayed_entry = s->first_displayed_entry;
1822 ncommits = 0;
1823 while (entry) {
1824 if (ncommits >= limit - 1)
1825 break;
1826 if (ncommits == s->selected)
1827 wstandout(view->window);
1828 err = draw_commit(view, entry->commit, entry->id,
1829 date_display_cols, author_cols);
1830 if (ncommits == s->selected)
1831 wstandend(view->window);
1832 if (err)
1833 goto done;
1834 ncommits++;
1835 s->last_displayed_entry = entry;
1836 entry = TAILQ_NEXT(entry, entry);
1839 view_vborder(view);
1840 done:
1841 free(id_str);
1842 free(refs_str);
1843 free(ncommits_str);
1844 free(header);
1845 return err;
1848 static void
1849 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1851 struct commit_queue_entry *entry;
1852 int nscrolled = 0;
1854 entry = TAILQ_FIRST(&s->commits.head);
1855 if (s->first_displayed_entry == entry)
1856 return;
1858 entry = s->first_displayed_entry;
1859 while (entry && nscrolled < maxscroll) {
1860 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1861 if (entry) {
1862 s->first_displayed_entry = entry;
1863 nscrolled++;
1868 static const struct got_error *
1869 trigger_log_thread(struct tog_view *view, int wait)
1871 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1872 int errcode;
1874 halfdelay(1); /* fast refresh while loading commits */
1876 while (ta->commits_needed > 0 || ta->load_all) {
1877 if (ta->log_complete)
1878 break;
1880 /* Wake the log thread. */
1881 errcode = pthread_cond_signal(&ta->need_commits);
1882 if (errcode)
1883 return got_error_set_errno(errcode,
1884 "pthread_cond_signal");
1887 * The mutex will be released while the view loop waits
1888 * in wgetch(), at which time the log thread will run.
1890 if (!wait)
1891 break;
1893 /* Display progress update in log view. */
1894 show_log_view(view);
1895 update_panels();
1896 doupdate();
1898 /* Wait right here while next commit is being loaded. */
1899 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1900 if (errcode)
1901 return got_error_set_errno(errcode,
1902 "pthread_cond_wait");
1904 /* Display progress update in log view. */
1905 show_log_view(view);
1906 update_panels();
1907 doupdate();
1910 return NULL;
1913 static const struct got_error *
1914 log_scroll_down(struct tog_view *view, int maxscroll)
1916 struct tog_log_view_state *s = &view->state.log;
1917 const struct got_error *err = NULL;
1918 struct commit_queue_entry *pentry;
1919 int nscrolled = 0, ncommits_needed;
1921 if (s->last_displayed_entry == NULL)
1922 return NULL;
1924 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1925 if (s->commits.ncommits < ncommits_needed &&
1926 !s->thread_args.log_complete) {
1928 * Ask the log thread for required amount of commits.
1930 s->thread_args.commits_needed += maxscroll;
1931 err = trigger_log_thread(view, 1);
1932 if (err)
1933 return err;
1936 do {
1937 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1938 if (pentry == NULL)
1939 break;
1941 s->last_displayed_entry = pentry;
1943 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1944 if (pentry == NULL)
1945 break;
1946 s->first_displayed_entry = pentry;
1947 } while (++nscrolled < maxscroll);
1949 return err;
1952 static const struct got_error *
1953 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1954 struct got_commit_object *commit, struct got_object_id *commit_id,
1955 struct tog_view *log_view, struct got_repository *repo)
1957 const struct got_error *err;
1958 struct got_object_qid *parent_id;
1959 struct tog_view *diff_view;
1961 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1962 if (diff_view == NULL)
1963 return got_error_from_errno("view_open");
1965 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1966 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1967 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1968 if (err == NULL)
1969 *new_view = diff_view;
1970 return err;
1973 static const struct got_error *
1974 tree_view_visit_subtree(struct tog_tree_view_state *s,
1975 struct got_tree_object *subtree)
1977 struct tog_parent_tree *parent;
1979 parent = calloc(1, sizeof(*parent));
1980 if (parent == NULL)
1981 return got_error_from_errno("calloc");
1983 parent->tree = s->tree;
1984 parent->first_displayed_entry = s->first_displayed_entry;
1985 parent->selected_entry = s->selected_entry;
1986 parent->selected = s->selected;
1987 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1988 s->tree = subtree;
1989 s->selected = 0;
1990 s->first_displayed_entry = NULL;
1991 return NULL;
1994 static const struct got_error *
1995 tree_view_walk_path(struct tog_tree_view_state *s,
1996 struct got_commit_object *commit, const char *path)
1998 const struct got_error *err = NULL;
1999 struct got_tree_object *tree = NULL;
2000 const char *p;
2001 char *slash, *subpath = NULL;
2003 /* Walk the path and open corresponding tree objects. */
2004 p = path;
2005 while (*p) {
2006 struct got_tree_entry *te;
2007 struct got_object_id *tree_id;
2008 char *te_name;
2010 while (p[0] == '/')
2011 p++;
2013 /* Ensure the correct subtree entry is selected. */
2014 slash = strchr(p, '/');
2015 if (slash == NULL)
2016 te_name = strdup(p);
2017 else
2018 te_name = strndup(p, slash - p);
2019 if (te_name == NULL) {
2020 err = got_error_from_errno("strndup");
2021 break;
2023 te = got_object_tree_find_entry(s->tree, te_name);
2024 if (te == NULL) {
2025 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2026 free(te_name);
2027 break;
2029 free(te_name);
2030 s->first_displayed_entry = s->selected_entry = te;
2032 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2033 break; /* jump to this file's entry */
2035 slash = strchr(p, '/');
2036 if (slash)
2037 subpath = strndup(path, slash - path);
2038 else
2039 subpath = strdup(path);
2040 if (subpath == NULL) {
2041 err = got_error_from_errno("strdup");
2042 break;
2045 err = got_object_id_by_path(&tree_id, s->repo, commit,
2046 subpath);
2047 if (err)
2048 break;
2050 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2051 free(tree_id);
2052 if (err)
2053 break;
2055 err = tree_view_visit_subtree(s, tree);
2056 if (err) {
2057 got_object_tree_close(tree);
2058 break;
2060 if (slash == NULL)
2061 break;
2062 free(subpath);
2063 subpath = NULL;
2064 p = slash;
2067 free(subpath);
2068 return err;
2071 static const struct got_error *
2072 browse_commit_tree(struct tog_view **new_view, int begin_x,
2073 struct commit_queue_entry *entry, const char *path,
2074 const char *head_ref_name, struct got_repository *repo)
2076 const struct got_error *err = NULL;
2077 struct tog_tree_view_state *s;
2078 struct tog_view *tree_view;
2080 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2081 if (tree_view == NULL)
2082 return got_error_from_errno("view_open");
2084 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2085 if (err)
2086 return err;
2087 s = &tree_view->state.tree;
2089 *new_view = tree_view;
2091 if (got_path_is_root_dir(path))
2092 return NULL;
2094 return tree_view_walk_path(s, entry->commit, path);
2097 static const struct got_error *
2098 block_signals_used_by_main_thread(void)
2100 sigset_t sigset;
2101 int errcode;
2103 if (sigemptyset(&sigset) == -1)
2104 return got_error_from_errno("sigemptyset");
2106 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2107 if (sigaddset(&sigset, SIGWINCH) == -1)
2108 return got_error_from_errno("sigaddset");
2109 if (sigaddset(&sigset, SIGCONT) == -1)
2110 return got_error_from_errno("sigaddset");
2111 if (sigaddset(&sigset, SIGINT) == -1)
2112 return got_error_from_errno("sigaddset");
2113 if (sigaddset(&sigset, SIGTERM) == -1)
2114 return got_error_from_errno("sigaddset");
2116 /* ncurses handles SIGTSTP */
2117 if (sigaddset(&sigset, SIGTSTP) == -1)
2118 return got_error_from_errno("sigaddset");
2120 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2121 if (errcode)
2122 return got_error_set_errno(errcode, "pthread_sigmask");
2124 return NULL;
2127 static void *
2128 log_thread(void *arg)
2130 const struct got_error *err = NULL;
2131 int errcode = 0;
2132 struct tog_log_thread_args *a = arg;
2133 int done = 0;
2135 err = block_signals_used_by_main_thread();
2136 if (err)
2137 return (void *)err;
2139 while (!done && !err && !tog_fatal_signal_received()) {
2140 err = queue_commits(a);
2141 if (err) {
2142 if (err->code != GOT_ERR_ITER_COMPLETED)
2143 return (void *)err;
2144 err = NULL;
2145 done = 1;
2146 } else if (a->commits_needed > 0 && !a->load_all)
2147 a->commits_needed--;
2149 errcode = pthread_mutex_lock(&tog_mutex);
2150 if (errcode) {
2151 err = got_error_set_errno(errcode,
2152 "pthread_mutex_lock");
2153 break;
2154 } else if (*a->quit)
2155 done = 1;
2156 else if (*a->first_displayed_entry == NULL) {
2157 *a->first_displayed_entry =
2158 TAILQ_FIRST(&a->commits->head);
2159 *a->selected_entry = *a->first_displayed_entry;
2162 errcode = pthread_cond_signal(&a->commit_loaded);
2163 if (errcode) {
2164 err = got_error_set_errno(errcode,
2165 "pthread_cond_signal");
2166 pthread_mutex_unlock(&tog_mutex);
2167 break;
2170 if (done)
2171 a->commits_needed = 0;
2172 else {
2173 if (a->commits_needed == 0 && !a->load_all) {
2174 errcode = pthread_cond_wait(&a->need_commits,
2175 &tog_mutex);
2176 if (errcode)
2177 err = got_error_set_errno(errcode,
2178 "pthread_cond_wait");
2179 if (*a->quit)
2180 done = 1;
2184 errcode = pthread_mutex_unlock(&tog_mutex);
2185 if (errcode && err == NULL)
2186 err = got_error_set_errno(errcode,
2187 "pthread_mutex_unlock");
2189 a->log_complete = 1;
2190 return (void *)err;
2193 static const struct got_error *
2194 stop_log_thread(struct tog_log_view_state *s)
2196 const struct got_error *err = NULL;
2197 int errcode;
2199 if (s->thread) {
2200 s->quit = 1;
2201 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2202 if (errcode)
2203 return got_error_set_errno(errcode,
2204 "pthread_cond_signal");
2205 errcode = pthread_mutex_unlock(&tog_mutex);
2206 if (errcode)
2207 return got_error_set_errno(errcode,
2208 "pthread_mutex_unlock");
2209 errcode = pthread_join(s->thread, (void **)&err);
2210 if (errcode)
2211 return got_error_set_errno(errcode, "pthread_join");
2212 errcode = pthread_mutex_lock(&tog_mutex);
2213 if (errcode)
2214 return got_error_set_errno(errcode,
2215 "pthread_mutex_lock");
2216 s->thread = NULL;
2219 if (s->thread_args.repo) {
2220 err = got_repo_close(s->thread_args.repo);
2221 s->thread_args.repo = NULL;
2224 if (s->thread_args.pack_fds) {
2225 const struct got_error *pack_err =
2226 got_repo_pack_fds_close(s->thread_args.pack_fds);
2227 if (err == NULL)
2228 err = pack_err;
2229 s->thread_args.pack_fds = NULL;
2232 if (s->thread_args.graph) {
2233 got_commit_graph_close(s->thread_args.graph);
2234 s->thread_args.graph = NULL;
2237 return err;
2240 static const struct got_error *
2241 close_log_view(struct tog_view *view)
2243 const struct got_error *err = NULL;
2244 struct tog_log_view_state *s = &view->state.log;
2245 int errcode;
2247 err = stop_log_thread(s);
2249 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2250 if (errcode && err == NULL)
2251 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2253 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2254 if (errcode && err == NULL)
2255 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2257 free_commits(&s->commits);
2258 free(s->in_repo_path);
2259 s->in_repo_path = NULL;
2260 free(s->start_id);
2261 s->start_id = NULL;
2262 free(s->head_ref_name);
2263 s->head_ref_name = NULL;
2264 return err;
2267 static const struct got_error *
2268 search_start_log_view(struct tog_view *view)
2270 struct tog_log_view_state *s = &view->state.log;
2272 s->matched_entry = NULL;
2273 s->search_entry = NULL;
2274 return NULL;
2277 static const struct got_error *
2278 search_next_log_view(struct tog_view *view)
2280 const struct got_error *err = NULL;
2281 struct tog_log_view_state *s = &view->state.log;
2282 struct commit_queue_entry *entry;
2284 /* Display progress update in log view. */
2285 show_log_view(view);
2286 update_panels();
2287 doupdate();
2289 if (s->search_entry) {
2290 int errcode, ch;
2291 errcode = pthread_mutex_unlock(&tog_mutex);
2292 if (errcode)
2293 return got_error_set_errno(errcode,
2294 "pthread_mutex_unlock");
2295 ch = wgetch(view->window);
2296 errcode = pthread_mutex_lock(&tog_mutex);
2297 if (errcode)
2298 return got_error_set_errno(errcode,
2299 "pthread_mutex_lock");
2300 if (ch == KEY_BACKSPACE) {
2301 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2302 return NULL;
2304 if (view->searching == TOG_SEARCH_FORWARD)
2305 entry = TAILQ_NEXT(s->search_entry, entry);
2306 else
2307 entry = TAILQ_PREV(s->search_entry,
2308 commit_queue_head, entry);
2309 } else if (s->matched_entry) {
2310 if (view->searching == TOG_SEARCH_FORWARD)
2311 entry = TAILQ_NEXT(s->matched_entry, entry);
2312 else
2313 entry = TAILQ_PREV(s->matched_entry,
2314 commit_queue_head, entry);
2315 } else {
2316 entry = s->selected_entry;
2319 while (1) {
2320 int have_match = 0;
2322 if (entry == NULL) {
2323 if (s->thread_args.log_complete ||
2324 view->searching == TOG_SEARCH_BACKWARD) {
2325 view->search_next_done =
2326 (s->matched_entry == NULL ?
2327 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2328 s->search_entry = NULL;
2329 return NULL;
2332 * Poke the log thread for more commits and return,
2333 * allowing the main loop to make progress. Search
2334 * will resume at s->search_entry once we come back.
2336 s->thread_args.commits_needed++;
2337 return trigger_log_thread(view, 0);
2340 err = match_commit(&have_match, entry->id, entry->commit,
2341 &view->regex);
2342 if (err)
2343 break;
2344 if (have_match) {
2345 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2346 s->matched_entry = entry;
2347 break;
2350 s->search_entry = entry;
2351 if (view->searching == TOG_SEARCH_FORWARD)
2352 entry = TAILQ_NEXT(entry, entry);
2353 else
2354 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2357 if (s->matched_entry) {
2358 int cur = s->selected_entry->idx;
2359 while (cur < s->matched_entry->idx) {
2360 err = input_log_view(NULL, view, KEY_DOWN);
2361 if (err)
2362 return err;
2363 cur++;
2365 while (cur > s->matched_entry->idx) {
2366 err = input_log_view(NULL, view, KEY_UP);
2367 if (err)
2368 return err;
2369 cur--;
2373 s->search_entry = NULL;
2375 return NULL;
2378 static const struct got_error *
2379 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2380 struct got_repository *repo, const char *head_ref_name,
2381 const char *in_repo_path, int log_branches)
2383 const struct got_error *err = NULL;
2384 struct tog_log_view_state *s = &view->state.log;
2385 struct got_repository *thread_repo = NULL;
2386 struct got_commit_graph *thread_graph = NULL;
2387 int errcode;
2389 if (in_repo_path != s->in_repo_path) {
2390 free(s->in_repo_path);
2391 s->in_repo_path = strdup(in_repo_path);
2392 if (s->in_repo_path == NULL)
2393 return got_error_from_errno("strdup");
2396 /* The commit queue only contains commits being displayed. */
2397 TAILQ_INIT(&s->commits.head);
2398 s->commits.ncommits = 0;
2400 s->repo = repo;
2401 if (head_ref_name) {
2402 s->head_ref_name = strdup(head_ref_name);
2403 if (s->head_ref_name == NULL) {
2404 err = got_error_from_errno("strdup");
2405 goto done;
2408 s->start_id = got_object_id_dup(start_id);
2409 if (s->start_id == NULL) {
2410 err = got_error_from_errno("got_object_id_dup");
2411 goto done;
2413 s->log_branches = log_branches;
2415 STAILQ_INIT(&s->colors);
2416 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2417 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2418 get_color_value("TOG_COLOR_COMMIT"));
2419 if (err)
2420 goto done;
2421 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2422 get_color_value("TOG_COLOR_AUTHOR"));
2423 if (err) {
2424 free_colors(&s->colors);
2425 goto done;
2427 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2428 get_color_value("TOG_COLOR_DATE"));
2429 if (err) {
2430 free_colors(&s->colors);
2431 goto done;
2435 view->show = show_log_view;
2436 view->input = input_log_view;
2437 view->close = close_log_view;
2438 view->search_start = search_start_log_view;
2439 view->search_next = search_next_log_view;
2441 if (s->thread_args.pack_fds == NULL) {
2442 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2443 if (err)
2444 goto done;
2446 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2447 s->thread_args.pack_fds);
2448 if (err)
2449 goto done;
2450 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2451 !s->log_branches);
2452 if (err)
2453 goto done;
2454 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2455 s->repo, NULL, NULL);
2456 if (err)
2457 goto done;
2459 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2460 if (errcode) {
2461 err = got_error_set_errno(errcode, "pthread_cond_init");
2462 goto done;
2464 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2465 if (errcode) {
2466 err = got_error_set_errno(errcode, "pthread_cond_init");
2467 goto done;
2470 s->thread_args.commits_needed = view->nlines;
2471 s->thread_args.graph = thread_graph;
2472 s->thread_args.commits = &s->commits;
2473 s->thread_args.in_repo_path = s->in_repo_path;
2474 s->thread_args.start_id = s->start_id;
2475 s->thread_args.repo = thread_repo;
2476 s->thread_args.log_complete = 0;
2477 s->thread_args.quit = &s->quit;
2478 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2479 s->thread_args.selected_entry = &s->selected_entry;
2480 s->thread_args.searching = &view->searching;
2481 s->thread_args.search_next_done = &view->search_next_done;
2482 s->thread_args.regex = &view->regex;
2483 done:
2484 if (err)
2485 close_log_view(view);
2486 return err;
2489 static const struct got_error *
2490 show_log_view(struct tog_view *view)
2492 const struct got_error *err;
2493 struct tog_log_view_state *s = &view->state.log;
2495 if (s->thread == NULL) {
2496 int errcode = pthread_create(&s->thread, NULL, log_thread,
2497 &s->thread_args);
2498 if (errcode)
2499 return got_error_set_errno(errcode, "pthread_create");
2500 if (s->thread_args.commits_needed > 0) {
2501 err = trigger_log_thread(view, 1);
2502 if (err)
2503 return err;
2507 return draw_commits(view);
2510 static const struct got_error *
2511 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2513 const struct got_error *err = NULL;
2514 struct tog_log_view_state *s = &view->state.log;
2515 struct tog_view *diff_view = NULL, *tree_view = NULL;
2516 struct tog_view *ref_view = NULL;
2517 struct commit_queue_entry *entry;
2518 int begin_x = 0, n, nscroll = view->nlines - 1;
2520 if (s->thread_args.load_all) {
2521 if (ch == KEY_BACKSPACE)
2522 s->thread_args.load_all = 0;
2523 else if (s->thread_args.log_complete) {
2524 s->thread_args.load_all = 0;
2525 log_scroll_down(view, s->commits.ncommits);
2526 s->selected = MIN(view->nlines - 2,
2527 s->commits.ncommits - 1);
2528 select_commit(s);
2530 return NULL;
2533 switch (ch) {
2534 case 'q':
2535 s->quit = 1;
2536 break;
2537 case '0':
2538 view->x = 0;
2539 break;
2540 case '$':
2541 view->x = MAX(view->maxx - view->ncols / 2, 0);
2542 break;
2543 case KEY_RIGHT:
2544 case 'l':
2545 if (view->x + view->ncols / 2 < view->maxx)
2546 view->x += 2; /* move two columns right */
2547 break;
2548 case KEY_LEFT:
2549 case 'h':
2550 view->x -= MIN(view->x, 2); /* move two columns back */
2551 break;
2552 case 'k':
2553 case KEY_UP:
2554 case '<':
2555 case ',':
2556 case CTRL('p'):
2557 if (s->first_displayed_entry == NULL)
2558 break;
2559 if (s->selected > 0)
2560 s->selected--;
2561 else
2562 log_scroll_up(s, 1);
2563 select_commit(s);
2564 break;
2565 case 'g':
2566 case KEY_HOME:
2567 s->selected = 0;
2568 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2569 select_commit(s);
2570 break;
2571 case CTRL('u'):
2572 case 'u':
2573 nscroll /= 2;
2574 /* FALL THROUGH */
2575 case KEY_PPAGE:
2576 case CTRL('b'):
2577 if (s->first_displayed_entry == NULL)
2578 break;
2579 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2580 s->selected = MAX(0, s->selected - nscroll - 1);
2581 else
2582 log_scroll_up(s, nscroll);
2583 select_commit(s);
2584 break;
2585 case 'j':
2586 case KEY_DOWN:
2587 case '>':
2588 case '.':
2589 case CTRL('n'):
2590 if (s->first_displayed_entry == NULL)
2591 break;
2592 if (s->selected < MIN(view->nlines - 2,
2593 s->commits.ncommits - 1))
2594 s->selected++;
2595 else {
2596 err = log_scroll_down(view, 1);
2597 if (err)
2598 break;
2600 select_commit(s);
2601 break;
2602 case 'G':
2603 case KEY_END: {
2604 /* We don't know yet how many commits, so we're forced to
2605 * traverse them all. */
2606 if (!s->thread_args.log_complete) {
2607 s->thread_args.load_all = 1;
2608 return trigger_log_thread(view, 0);
2611 s->selected = 0;
2612 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2613 for (n = 0; n < view->nlines - 1; n++) {
2614 if (entry == NULL)
2615 break;
2616 s->first_displayed_entry = entry;
2617 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2619 if (n > 0)
2620 s->selected = n - 1;
2621 select_commit(s);
2622 break;
2624 case CTRL('d'):
2625 case 'd':
2626 nscroll /= 2;
2627 /* FALL THROUGH */
2628 case KEY_NPAGE:
2629 case CTRL('f'): {
2630 struct commit_queue_entry *first;
2631 first = s->first_displayed_entry;
2632 if (first == NULL)
2633 break;
2634 err = log_scroll_down(view, nscroll);
2635 if (err)
2636 break;
2637 if (first == s->first_displayed_entry &&
2638 s->selected < MIN(view->nlines - 2,
2639 s->commits.ncommits - 1)) {
2640 /* can't scroll further down */
2641 s->selected += MIN(s->last_displayed_entry->idx -
2642 s->selected_entry->idx, nscroll + 1);
2644 select_commit(s);
2645 break;
2647 case KEY_RESIZE:
2648 if (s->selected > view->nlines - 2)
2649 s->selected = view->nlines - 2;
2650 if (s->selected > s->commits.ncommits - 1)
2651 s->selected = s->commits.ncommits - 1;
2652 select_commit(s);
2653 if (s->commits.ncommits < view->nlines - 1 &&
2654 !s->thread_args.log_complete) {
2655 s->thread_args.commits_needed += (view->nlines - 1) -
2656 s->commits.ncommits;
2657 err = trigger_log_thread(view, 1);
2659 break;
2660 case KEY_ENTER:
2661 case ' ':
2662 case '\r':
2663 if (s->selected_entry == NULL)
2664 break;
2665 if (view_is_parent_view(view))
2666 begin_x = view_split_begin_x(view->begin_x);
2667 err = open_diff_view_for_commit(&diff_view, begin_x,
2668 s->selected_entry->commit, s->selected_entry->id,
2669 view, s->repo);
2670 if (err)
2671 break;
2672 view->focussed = 0;
2673 diff_view->focussed = 1;
2674 if (view_is_parent_view(view)) {
2675 err = view_close_child(view);
2676 if (err)
2677 return err;
2678 view_set_child(view, diff_view);
2679 view->focus_child = 1;
2680 } else
2681 *new_view = diff_view;
2682 break;
2683 case 't':
2684 if (s->selected_entry == NULL)
2685 break;
2686 if (view_is_parent_view(view))
2687 begin_x = view_split_begin_x(view->begin_x);
2688 err = browse_commit_tree(&tree_view, begin_x,
2689 s->selected_entry, s->in_repo_path, s->head_ref_name,
2690 s->repo);
2691 if (err)
2692 break;
2693 view->focussed = 0;
2694 tree_view->focussed = 1;
2695 if (view_is_parent_view(view)) {
2696 err = view_close_child(view);
2697 if (err)
2698 return err;
2699 view_set_child(view, tree_view);
2700 view->focus_child = 1;
2701 } else
2702 *new_view = tree_view;
2703 break;
2704 case KEY_BACKSPACE:
2705 case CTRL('l'):
2706 case 'B':
2707 if (ch == KEY_BACKSPACE &&
2708 got_path_is_root_dir(s->in_repo_path))
2709 break;
2710 err = stop_log_thread(s);
2711 if (err)
2712 return err;
2713 if (ch == KEY_BACKSPACE) {
2714 char *parent_path;
2715 err = got_path_dirname(&parent_path, s->in_repo_path);
2716 if (err)
2717 return err;
2718 free(s->in_repo_path);
2719 s->in_repo_path = parent_path;
2720 s->thread_args.in_repo_path = s->in_repo_path;
2721 } else if (ch == CTRL('l')) {
2722 struct got_object_id *start_id;
2723 err = got_repo_match_object_id(&start_id, NULL,
2724 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2725 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2726 if (err)
2727 return err;
2728 free(s->start_id);
2729 s->start_id = start_id;
2730 s->thread_args.start_id = s->start_id;
2731 } else /* 'B' */
2732 s->log_branches = !s->log_branches;
2734 err = got_repo_open(&s->thread_args.repo,
2735 got_repo_get_path(s->repo), NULL,
2736 s->thread_args.pack_fds);
2737 if (err)
2738 return err;
2739 tog_free_refs();
2740 err = tog_load_refs(s->repo, 0);
2741 if (err)
2742 return err;
2743 err = got_commit_graph_open(&s->thread_args.graph,
2744 s->in_repo_path, !s->log_branches);
2745 if (err)
2746 return err;
2747 err = got_commit_graph_iter_start(s->thread_args.graph,
2748 s->start_id, s->repo, NULL, NULL);
2749 if (err)
2750 return err;
2751 free_commits(&s->commits);
2752 s->first_displayed_entry = NULL;
2753 s->last_displayed_entry = NULL;
2754 s->selected_entry = NULL;
2755 s->selected = 0;
2756 s->thread_args.log_complete = 0;
2757 s->quit = 0;
2758 s->thread_args.commits_needed = view->nlines;
2759 break;
2760 case 'r':
2761 if (view_is_parent_view(view))
2762 begin_x = view_split_begin_x(view->begin_x);
2763 ref_view = view_open(view->nlines, view->ncols,
2764 view->begin_y, begin_x, TOG_VIEW_REF);
2765 if (ref_view == NULL)
2766 return got_error_from_errno("view_open");
2767 err = open_ref_view(ref_view, s->repo);
2768 if (err) {
2769 view_close(ref_view);
2770 return err;
2772 view->focussed = 0;
2773 ref_view->focussed = 1;
2774 if (view_is_parent_view(view)) {
2775 err = view_close_child(view);
2776 if (err)
2777 return err;
2778 view_set_child(view, ref_view);
2779 view->focus_child = 1;
2780 } else
2781 *new_view = ref_view;
2782 break;
2783 default:
2784 break;
2787 return err;
2790 static const struct got_error *
2791 apply_unveil(const char *repo_path, const char *worktree_path)
2793 const struct got_error *error;
2795 #ifdef PROFILE
2796 if (unveil("gmon.out", "rwc") != 0)
2797 return got_error_from_errno2("unveil", "gmon.out");
2798 #endif
2799 if (repo_path && unveil(repo_path, "r") != 0)
2800 return got_error_from_errno2("unveil", repo_path);
2802 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2803 return got_error_from_errno2("unveil", worktree_path);
2805 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2806 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2808 error = got_privsep_unveil_exec_helpers();
2809 if (error != NULL)
2810 return error;
2812 if (unveil(NULL, NULL) != 0)
2813 return got_error_from_errno("unveil");
2815 return NULL;
2818 static void
2819 init_curses(void)
2822 * Override default signal handlers before starting ncurses.
2823 * This should prevent ncurses from installing its own
2824 * broken cleanup() signal handler.
2826 signal(SIGWINCH, tog_sigwinch);
2827 signal(SIGPIPE, tog_sigpipe);
2828 signal(SIGCONT, tog_sigcont);
2829 signal(SIGINT, tog_sigint);
2830 signal(SIGTERM, tog_sigterm);
2832 initscr();
2833 cbreak();
2834 halfdelay(1); /* Do fast refresh while initial view is loading. */
2835 noecho();
2836 nonl();
2837 intrflush(stdscr, FALSE);
2838 keypad(stdscr, TRUE);
2839 curs_set(0);
2840 if (getenv("TOG_COLORS") != NULL) {
2841 start_color();
2842 use_default_colors();
2846 static const struct got_error *
2847 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2848 struct got_repository *repo, struct got_worktree *worktree)
2850 const struct got_error *err = NULL;
2852 if (argc == 0) {
2853 *in_repo_path = strdup("/");
2854 if (*in_repo_path == NULL)
2855 return got_error_from_errno("strdup");
2856 return NULL;
2859 if (worktree) {
2860 const char *prefix = got_worktree_get_path_prefix(worktree);
2861 char *p;
2863 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2864 if (err)
2865 return err;
2866 if (asprintf(in_repo_path, "%s%s%s", prefix,
2867 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2868 p) == -1) {
2869 err = got_error_from_errno("asprintf");
2870 *in_repo_path = NULL;
2872 free(p);
2873 } else
2874 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2876 return err;
2879 static const struct got_error *
2880 cmd_log(int argc, char *argv[])
2882 const struct got_error *error;
2883 struct got_repository *repo = NULL;
2884 struct got_worktree *worktree = NULL;
2885 struct got_object_id *start_id = NULL;
2886 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2887 char *start_commit = NULL, *label = NULL;
2888 struct got_reference *ref = NULL;
2889 const char *head_ref_name = NULL;
2890 int ch, log_branches = 0;
2891 struct tog_view *view;
2892 int *pack_fds = NULL;
2894 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2895 switch (ch) {
2896 case 'b':
2897 log_branches = 1;
2898 break;
2899 case 'c':
2900 start_commit = optarg;
2901 break;
2902 case 'r':
2903 repo_path = realpath(optarg, NULL);
2904 if (repo_path == NULL)
2905 return got_error_from_errno2("realpath",
2906 optarg);
2907 break;
2908 default:
2909 usage_log();
2910 /* NOTREACHED */
2914 argc -= optind;
2915 argv += optind;
2917 if (argc > 1)
2918 usage_log();
2920 error = got_repo_pack_fds_open(&pack_fds);
2921 if (error != NULL)
2922 goto done;
2924 if (repo_path == NULL) {
2925 cwd = getcwd(NULL, 0);
2926 if (cwd == NULL)
2927 return got_error_from_errno("getcwd");
2928 error = got_worktree_open(&worktree, cwd);
2929 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2930 goto done;
2931 if (worktree)
2932 repo_path =
2933 strdup(got_worktree_get_repo_path(worktree));
2934 else
2935 repo_path = strdup(cwd);
2936 if (repo_path == NULL) {
2937 error = got_error_from_errno("strdup");
2938 goto done;
2942 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2943 if (error != NULL)
2944 goto done;
2946 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2947 repo, worktree);
2948 if (error)
2949 goto done;
2951 init_curses();
2953 error = apply_unveil(got_repo_get_path(repo),
2954 worktree ? got_worktree_get_root_path(worktree) : NULL);
2955 if (error)
2956 goto done;
2958 /* already loaded by tog_log_with_path()? */
2959 if (TAILQ_EMPTY(&tog_refs)) {
2960 error = tog_load_refs(repo, 0);
2961 if (error)
2962 goto done;
2965 if (start_commit == NULL) {
2966 error = got_repo_match_object_id(&start_id, &label,
2967 worktree ? got_worktree_get_head_ref_name(worktree) :
2968 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2969 if (error)
2970 goto done;
2971 head_ref_name = label;
2972 } else {
2973 error = got_ref_open(&ref, repo, start_commit, 0);
2974 if (error == NULL)
2975 head_ref_name = got_ref_get_name(ref);
2976 else if (error->code != GOT_ERR_NOT_REF)
2977 goto done;
2978 error = got_repo_match_object_id(&start_id, NULL,
2979 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2980 if (error)
2981 goto done;
2984 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2985 if (view == NULL) {
2986 error = got_error_from_errno("view_open");
2987 goto done;
2989 error = open_log_view(view, start_id, repo, head_ref_name,
2990 in_repo_path, log_branches);
2991 if (error)
2992 goto done;
2993 if (worktree) {
2994 /* Release work tree lock. */
2995 got_worktree_close(worktree);
2996 worktree = NULL;
2998 error = view_loop(view);
2999 done:
3000 free(in_repo_path);
3001 free(repo_path);
3002 free(cwd);
3003 free(start_id);
3004 free(label);
3005 if (ref)
3006 got_ref_close(ref);
3007 if (repo) {
3008 const struct got_error *close_err = got_repo_close(repo);
3009 if (error == NULL)
3010 error = close_err;
3012 if (worktree)
3013 got_worktree_close(worktree);
3014 if (pack_fds) {
3015 const struct got_error *pack_err =
3016 got_repo_pack_fds_close(pack_fds);
3017 if (error == NULL)
3018 error = pack_err;
3020 tog_free_refs();
3021 return error;
3024 __dead static void
3025 usage_diff(void)
3027 endwin();
3028 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3029 "[-w] object1 object2\n", getprogname());
3030 exit(1);
3033 static int
3034 match_line(const char *line, regex_t *regex, size_t nmatch,
3035 regmatch_t *regmatch)
3037 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3040 struct tog_color *
3041 match_color(struct tog_colors *colors, const char *line)
3043 struct tog_color *tc = NULL;
3045 STAILQ_FOREACH(tc, colors, entry) {
3046 if (match_line(line, &tc->regex, 0, NULL))
3047 return tc;
3050 return NULL;
3053 static const struct got_error *
3054 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3055 WINDOW *window, int skip, regmatch_t *regmatch)
3057 const struct got_error *err = NULL;
3058 wchar_t *wline;
3059 int rme, rms, n, width;
3061 *wtotal = 0;
3062 rms = regmatch->rm_so;
3063 rme = regmatch->rm_eo;
3065 err = format_line(&wline, &width, line, wlimit + skip,
3066 col_tab_align, 1);
3067 if (err)
3068 return err;
3070 /* draw up to matched token if we haven't scrolled past it */
3071 n = MAX(rms - skip, 0);
3072 if (n) {
3073 waddnwstr(window, wline + skip, n);
3074 wlimit -= n;
3075 *wtotal += n;
3078 if (wlimit > 0) {
3079 int len = rme - rms;
3080 n = 0;
3081 if (skip > rms) {
3082 n = skip - rms;
3083 len = MAX(len - n, 0);
3085 /* draw (visible part of) matched token (if scrolled into it) */
3086 if (len) {
3087 wattron(window, A_STANDOUT);
3088 waddnwstr(window, wline + rms + n, len);
3089 wattroff(window, A_STANDOUT);
3090 wlimit -= len;
3091 *wtotal += len;
3095 if (wlimit > 0 && skip < width) { /* draw rest of line */
3096 n = 0;
3097 if (skip > rme)
3098 n = MIN(skip - rme, width - rme);
3099 waddnwstr(window, wline + rme + n, wlimit);
3102 *wtotal = width;
3103 free(wline);
3104 return NULL;
3107 static const struct got_error *
3108 draw_file(struct tog_view *view, const char *header)
3110 struct tog_diff_view_state *s = &view->state.diff;
3111 regmatch_t *regmatch = &view->regmatch;
3112 const struct got_error *err;
3113 int nprinted = 0;
3114 char *line;
3115 size_t linesize = 0;
3116 ssize_t linelen;
3117 struct tog_color *tc;
3118 wchar_t *wline;
3119 int width;
3120 int max_lines = view->nlines;
3121 int nlines = s->nlines;
3122 off_t line_offset;
3124 line_offset = s->line_offsets[s->first_displayed_line - 1];
3125 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3126 return got_error_from_errno("fseek");
3128 werase(view->window);
3130 if (header) {
3131 if (asprintf(&line, "[%d/%d] %s",
3132 s->first_displayed_line - 1 + s->selected_line, nlines,
3133 header) == -1)
3134 return got_error_from_errno("asprintf");
3135 err = format_line(&wline, &width, line, view->ncols, 0, 0);
3136 free(line);
3137 if (err)
3138 return err;
3140 if (view_needs_focus_indication(view))
3141 wstandout(view->window);
3142 waddwstr(view->window, wline);
3143 free(wline);
3144 wline = NULL;
3145 if (view_needs_focus_indication(view))
3146 wstandend(view->window);
3147 if (width <= view->ncols - 1)
3148 waddch(view->window, '\n');
3150 if (max_lines <= 1)
3151 return NULL;
3152 max_lines--;
3155 s->eof = 0;
3156 view->maxx = 0;
3157 line = NULL;
3158 while (max_lines > 0 && nprinted < max_lines) {
3159 linelen = getline(&line, &linesize, s->f);
3160 if (linelen == -1) {
3161 if (feof(s->f)) {
3162 s->eof = 1;
3163 break;
3165 free(line);
3166 return got_ferror(s->f, GOT_ERR_IO);
3169 view->maxx = MAX(view->maxx, linelen);
3171 tc = match_color(&s->colors, line);
3172 if (tc)
3173 wattr_on(view->window,
3174 COLOR_PAIR(tc->colorpair), NULL);
3175 if (s->first_displayed_line + nprinted == s->matched_line &&
3176 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3177 err = add_matched_line(&width, line, view->ncols, 0,
3178 view->window, view->x, regmatch);
3179 if (err) {
3180 free(line);
3181 return err;
3183 } else {
3184 err = format_line(&wline, &width, line,
3185 view->x + view->ncols, 0, view->x ? 1 : 0);
3186 if (err) {
3187 free(line);
3188 return err;
3190 if (view->x < width - 1)
3191 waddwstr(view->window, wline + view->x);
3192 free(wline);
3193 wline = NULL;
3195 if (tc)
3196 wattr_off(view->window,
3197 COLOR_PAIR(tc->colorpair), NULL);
3198 if (width - view->x <= view->ncols - 1)
3199 waddch(view->window, '\n');
3200 nprinted++;
3202 free(line);
3203 if (nprinted >= 1)
3204 s->last_displayed_line = s->first_displayed_line +
3205 (nprinted - 1);
3206 else
3207 s->last_displayed_line = s->first_displayed_line;
3209 view_vborder(view);
3211 if (s->eof) {
3212 while (nprinted < view->nlines) {
3213 waddch(view->window, '\n');
3214 nprinted++;
3217 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols,
3218 0, 0);
3219 if (err) {
3220 return err;
3223 wstandout(view->window);
3224 waddwstr(view->window, wline);
3225 free(wline);
3226 wline = NULL;
3227 wstandend(view->window);
3230 return NULL;
3233 static char *
3234 get_datestr(time_t *time, char *datebuf)
3236 struct tm mytm, *tm;
3237 char *p, *s;
3239 tm = gmtime_r(time, &mytm);
3240 if (tm == NULL)
3241 return NULL;
3242 s = asctime_r(tm, datebuf);
3243 if (s == NULL)
3244 return NULL;
3245 p = strchr(s, '\n');
3246 if (p)
3247 *p = '\0';
3248 return s;
3251 static const struct got_error *
3252 get_changed_paths(struct got_pathlist_head *paths,
3253 struct got_commit_object *commit, struct got_repository *repo)
3255 const struct got_error *err = NULL;
3256 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3257 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3258 struct got_object_qid *qid;
3260 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3261 if (qid != NULL) {
3262 struct got_commit_object *pcommit;
3263 err = got_object_open_as_commit(&pcommit, repo,
3264 &qid->id);
3265 if (err)
3266 return err;
3268 tree_id1 = got_object_id_dup(
3269 got_object_commit_get_tree_id(pcommit));
3270 if (tree_id1 == NULL) {
3271 got_object_commit_close(pcommit);
3272 return got_error_from_errno("got_object_id_dup");
3274 got_object_commit_close(pcommit);
3278 if (tree_id1) {
3279 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3280 if (err)
3281 goto done;
3284 tree_id2 = got_object_commit_get_tree_id(commit);
3285 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3286 if (err)
3287 goto done;
3289 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3290 got_diff_tree_collect_changed_paths, paths, 0);
3291 done:
3292 if (tree1)
3293 got_object_tree_close(tree1);
3294 if (tree2)
3295 got_object_tree_close(tree2);
3296 free(tree_id1);
3297 return err;
3300 static const struct got_error *
3301 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3303 off_t *p;
3305 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3306 if (p == NULL)
3307 return got_error_from_errno("reallocarray");
3308 *line_offsets = p;
3309 (*line_offsets)[*nlines] = off;
3310 (*nlines)++;
3311 return NULL;
3314 static const struct got_error *
3315 write_commit_info(off_t **line_offsets, size_t *nlines,
3316 struct got_object_id *commit_id, struct got_reflist_head *refs,
3317 struct got_repository *repo, FILE *outfile)
3319 const struct got_error *err = NULL;
3320 char datebuf[26], *datestr;
3321 struct got_commit_object *commit;
3322 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3323 time_t committer_time;
3324 const char *author, *committer;
3325 char *refs_str = NULL;
3326 struct got_pathlist_head changed_paths;
3327 struct got_pathlist_entry *pe;
3328 off_t outoff = 0;
3329 int n;
3331 TAILQ_INIT(&changed_paths);
3333 if (refs) {
3334 err = build_refs_str(&refs_str, refs, commit_id, repo);
3335 if (err)
3336 return err;
3339 err = got_object_open_as_commit(&commit, repo, commit_id);
3340 if (err)
3341 return err;
3343 err = got_object_id_str(&id_str, commit_id);
3344 if (err) {
3345 err = got_error_from_errno("got_object_id_str");
3346 goto done;
3349 err = add_line_offset(line_offsets, nlines, 0);
3350 if (err)
3351 goto done;
3353 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3354 refs_str ? refs_str : "", refs_str ? ")" : "");
3355 if (n < 0) {
3356 err = got_error_from_errno("fprintf");
3357 goto done;
3359 outoff += n;
3360 err = add_line_offset(line_offsets, nlines, outoff);
3361 if (err)
3362 goto done;
3364 n = fprintf(outfile, "from: %s\n",
3365 got_object_commit_get_author(commit));
3366 if (n < 0) {
3367 err = got_error_from_errno("fprintf");
3368 goto done;
3370 outoff += n;
3371 err = add_line_offset(line_offsets, nlines, outoff);
3372 if (err)
3373 goto done;
3375 committer_time = got_object_commit_get_committer_time(commit);
3376 datestr = get_datestr(&committer_time, datebuf);
3377 if (datestr) {
3378 n = fprintf(outfile, "date: %s UTC\n", datestr);
3379 if (n < 0) {
3380 err = got_error_from_errno("fprintf");
3381 goto done;
3383 outoff += n;
3384 err = add_line_offset(line_offsets, nlines, outoff);
3385 if (err)
3386 goto done;
3388 author = got_object_commit_get_author(commit);
3389 committer = got_object_commit_get_committer(commit);
3390 if (strcmp(author, committer) != 0) {
3391 n = fprintf(outfile, "via: %s\n", committer);
3392 if (n < 0) {
3393 err = got_error_from_errno("fprintf");
3394 goto done;
3396 outoff += n;
3397 err = add_line_offset(line_offsets, nlines, outoff);
3398 if (err)
3399 goto done;
3401 if (got_object_commit_get_nparents(commit) > 1) {
3402 const struct got_object_id_queue *parent_ids;
3403 struct got_object_qid *qid;
3404 int pn = 1;
3405 parent_ids = got_object_commit_get_parent_ids(commit);
3406 STAILQ_FOREACH(qid, parent_ids, entry) {
3407 err = got_object_id_str(&id_str, &qid->id);
3408 if (err)
3409 goto done;
3410 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3411 if (n < 0) {
3412 err = got_error_from_errno("fprintf");
3413 goto done;
3415 outoff += n;
3416 err = add_line_offset(line_offsets, nlines, outoff);
3417 if (err)
3418 goto done;
3419 free(id_str);
3420 id_str = NULL;
3424 err = got_object_commit_get_logmsg(&logmsg, commit);
3425 if (err)
3426 goto done;
3427 s = logmsg;
3428 while ((line = strsep(&s, "\n")) != NULL) {
3429 n = fprintf(outfile, "%s\n", line);
3430 if (n < 0) {
3431 err = got_error_from_errno("fprintf");
3432 goto done;
3434 outoff += n;
3435 err = add_line_offset(line_offsets, nlines, outoff);
3436 if (err)
3437 goto done;
3440 err = get_changed_paths(&changed_paths, commit, repo);
3441 if (err)
3442 goto done;
3443 TAILQ_FOREACH(pe, &changed_paths, entry) {
3444 struct got_diff_changed_path *cp = pe->data;
3445 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3446 if (n < 0) {
3447 err = got_error_from_errno("fprintf");
3448 goto done;
3450 outoff += n;
3451 err = add_line_offset(line_offsets, nlines, outoff);
3452 if (err)
3453 goto done;
3454 free((char *)pe->path);
3455 free(pe->data);
3458 fputc('\n', outfile);
3459 outoff++;
3460 err = add_line_offset(line_offsets, nlines, outoff);
3461 done:
3462 got_pathlist_free(&changed_paths);
3463 free(id_str);
3464 free(logmsg);
3465 free(refs_str);
3466 got_object_commit_close(commit);
3467 if (err) {
3468 free(*line_offsets);
3469 *line_offsets = NULL;
3470 *nlines = 0;
3472 return err;
3475 static const struct got_error *
3476 create_diff(struct tog_diff_view_state *s)
3478 const struct got_error *err = NULL;
3479 FILE *f = NULL;
3480 int obj_type;
3482 free(s->line_offsets);
3483 s->line_offsets = malloc(sizeof(off_t));
3484 if (s->line_offsets == NULL)
3485 return got_error_from_errno("malloc");
3486 s->nlines = 0;
3488 f = got_opentemp();
3489 if (f == NULL) {
3490 err = got_error_from_errno("got_opentemp");
3491 goto done;
3493 if (s->f && fclose(s->f) == EOF) {
3494 err = got_error_from_errno("fclose");
3495 goto done;
3497 s->f = f;
3499 if (s->id1)
3500 err = got_object_get_type(&obj_type, s->repo, s->id1);
3501 else
3502 err = got_object_get_type(&obj_type, s->repo, s->id2);
3503 if (err)
3504 goto done;
3506 switch (obj_type) {
3507 case GOT_OBJ_TYPE_BLOB:
3508 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3509 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3510 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3511 s->repo, s->f);
3512 break;
3513 case GOT_OBJ_TYPE_TREE:
3514 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3515 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3516 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3517 break;
3518 case GOT_OBJ_TYPE_COMMIT: {
3519 const struct got_object_id_queue *parent_ids;
3520 struct got_object_qid *pid;
3521 struct got_commit_object *commit2;
3522 struct got_reflist_head *refs;
3524 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3525 if (err)
3526 goto done;
3527 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3528 /* Show commit info if we're diffing to a parent/root commit. */
3529 if (s->id1 == NULL) {
3530 err = write_commit_info(&s->line_offsets, &s->nlines,
3531 s->id2, refs, s->repo, s->f);
3532 if (err)
3533 goto done;
3534 } else {
3535 parent_ids = got_object_commit_get_parent_ids(commit2);
3536 STAILQ_FOREACH(pid, parent_ids, entry) {
3537 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3538 err = write_commit_info(
3539 &s->line_offsets, &s->nlines,
3540 s->id2, refs, s->repo, s->f);
3541 if (err)
3542 goto done;
3543 break;
3547 got_object_commit_close(commit2);
3549 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3550 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3551 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3552 break;
3554 default:
3555 err = got_error(GOT_ERR_OBJ_TYPE);
3556 break;
3558 if (err)
3559 goto done;
3560 done:
3561 if (s->f && fflush(s->f) != 0 && err == NULL)
3562 err = got_error_from_errno("fflush");
3563 return err;
3566 static void
3567 diff_view_indicate_progress(struct tog_view *view)
3569 mvwaddstr(view->window, 0, 0, "diffing...");
3570 update_panels();
3571 doupdate();
3574 static const struct got_error *
3575 search_start_diff_view(struct tog_view *view)
3577 struct tog_diff_view_state *s = &view->state.diff;
3579 s->matched_line = 0;
3580 return NULL;
3583 static const struct got_error *
3584 search_next_diff_view(struct tog_view *view)
3586 struct tog_diff_view_state *s = &view->state.diff;
3587 const struct got_error *err = NULL;
3588 int lineno;
3589 char *exstr = NULL, *line = NULL;
3590 size_t linesize = 0;
3591 ssize_t linelen;
3593 if (!view->searching) {
3594 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3595 return NULL;
3598 if (s->matched_line) {
3599 if (view->searching == TOG_SEARCH_FORWARD)
3600 lineno = s->matched_line + 1;
3601 else
3602 lineno = s->matched_line - 1;
3603 } else
3604 lineno = s->first_displayed_line;
3606 while (1) {
3607 off_t offset;
3609 if (lineno <= 0 || lineno > s->nlines) {
3610 if (s->matched_line == 0) {
3611 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3612 break;
3615 if (view->searching == TOG_SEARCH_FORWARD)
3616 lineno = 1;
3617 else
3618 lineno = s->nlines;
3621 offset = s->line_offsets[lineno - 1];
3622 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3623 free(line);
3624 return got_error_from_errno("fseeko");
3626 linelen = getline(&line, &linesize, s->f);
3627 err = expand_tab(&exstr, line);
3628 if (err)
3629 break;
3630 if (linelen != -1 &&
3631 match_line(exstr, &view->regex, 1, &view->regmatch)) {
3632 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3633 s->matched_line = lineno;
3634 break;
3636 free(exstr);
3637 exstr = NULL;
3638 if (view->searching == TOG_SEARCH_FORWARD)
3639 lineno++;
3640 else
3641 lineno--;
3643 free(line);
3644 free(exstr);
3646 if (s->matched_line) {
3647 s->first_displayed_line = s->matched_line;
3648 s->selected_line = 1;
3651 return err;
3654 static const struct got_error *
3655 close_diff_view(struct tog_view *view)
3657 const struct got_error *err = NULL;
3658 struct tog_diff_view_state *s = &view->state.diff;
3660 free(s->id1);
3661 s->id1 = NULL;
3662 free(s->id2);
3663 s->id2 = NULL;
3664 if (s->f && fclose(s->f) == EOF)
3665 err = got_error_from_errno("fclose");
3666 s->f = NULL;
3667 if (s->f1 && fclose(s->f1) == EOF)
3668 err = got_error_from_errno("fclose");
3669 s->f1 = NULL;
3670 if (s->f2 && fclose(s->f2) == EOF)
3671 err = got_error_from_errno("fclose");
3672 s->f2 = NULL;
3673 free_colors(&s->colors);
3674 free(s->line_offsets);
3675 s->line_offsets = NULL;
3676 s->nlines = 0;
3677 return err;
3680 static const struct got_error *
3681 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3682 struct got_object_id *id2, const char *label1, const char *label2,
3683 int diff_context, int ignore_whitespace, int force_text_diff,
3684 struct tog_view *log_view, struct got_repository *repo)
3686 const struct got_error *err;
3687 struct tog_diff_view_state *s = &view->state.diff;
3689 memset(s, 0, sizeof(*s));
3691 if (id1 != NULL && id2 != NULL) {
3692 int type1, type2;
3693 err = got_object_get_type(&type1, repo, id1);
3694 if (err)
3695 return err;
3696 err = got_object_get_type(&type2, repo, id2);
3697 if (err)
3698 return err;
3700 if (type1 != type2)
3701 return got_error(GOT_ERR_OBJ_TYPE);
3703 s->first_displayed_line = 1;
3704 s->last_displayed_line = view->nlines;
3705 s->selected_line = 1;
3706 s->repo = repo;
3707 s->id1 = id1;
3708 s->id2 = id2;
3709 s->label1 = label1;
3710 s->label2 = label2;
3712 if (id1) {
3713 s->id1 = got_object_id_dup(id1);
3714 if (s->id1 == NULL)
3715 return got_error_from_errno("got_object_id_dup");
3716 s->f1 = got_opentemp();
3717 if (s->f1 == NULL) {
3718 err = got_error_from_errno("got_opentemp");
3719 goto done;
3721 } else
3722 s->id1 = NULL;
3724 s->id2 = got_object_id_dup(id2);
3725 if (s->id2 == NULL) {
3726 err = got_error_from_errno("got_object_id_dup");
3727 goto done;
3730 s->f2 = got_opentemp();
3731 if (s->f2 == NULL) {
3732 err = got_error_from_errno("got_opentemp");
3733 goto done;
3736 s->first_displayed_line = 1;
3737 s->last_displayed_line = view->nlines;
3738 s->diff_context = diff_context;
3739 s->ignore_whitespace = ignore_whitespace;
3740 s->force_text_diff = force_text_diff;
3741 s->log_view = log_view;
3742 s->repo = repo;
3744 STAILQ_INIT(&s->colors);
3745 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3746 err = add_color(&s->colors,
3747 "^-", TOG_COLOR_DIFF_MINUS,
3748 get_color_value("TOG_COLOR_DIFF_MINUS"));
3749 if (err)
3750 goto done;
3751 err = add_color(&s->colors, "^\\+",
3752 TOG_COLOR_DIFF_PLUS,
3753 get_color_value("TOG_COLOR_DIFF_PLUS"));
3754 if (err)
3755 goto done;
3756 err = add_color(&s->colors,
3757 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3758 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3759 if (err)
3760 goto done;
3762 err = add_color(&s->colors,
3763 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3764 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3765 get_color_value("TOG_COLOR_DIFF_META"));
3766 if (err)
3767 goto done;
3769 err = add_color(&s->colors,
3770 "^(from|via): ", TOG_COLOR_AUTHOR,
3771 get_color_value("TOG_COLOR_AUTHOR"));
3772 if (err)
3773 goto done;
3775 err = add_color(&s->colors,
3776 "^date: ", TOG_COLOR_DATE,
3777 get_color_value("TOG_COLOR_DATE"));
3778 if (err)
3779 goto done;
3782 if (log_view && view_is_splitscreen(view))
3783 show_log_view(log_view); /* draw vborder */
3784 diff_view_indicate_progress(view);
3786 err = create_diff(s);
3788 view->show = show_diff_view;
3789 view->input = input_diff_view;
3790 view->close = close_diff_view;
3791 view->search_start = search_start_diff_view;
3792 view->search_next = search_next_diff_view;
3793 done:
3794 if (err)
3795 close_diff_view(view);
3796 return err;
3799 static const struct got_error *
3800 show_diff_view(struct tog_view *view)
3802 const struct got_error *err;
3803 struct tog_diff_view_state *s = &view->state.diff;
3804 char *id_str1 = NULL, *id_str2, *header;
3805 const char *label1, *label2;
3807 if (s->id1) {
3808 err = got_object_id_str(&id_str1, s->id1);
3809 if (err)
3810 return err;
3811 label1 = s->label1 ? : id_str1;
3812 } else
3813 label1 = "/dev/null";
3815 err = got_object_id_str(&id_str2, s->id2);
3816 if (err)
3817 return err;
3818 label2 = s->label2 ? : id_str2;
3820 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3821 err = got_error_from_errno("asprintf");
3822 free(id_str1);
3823 free(id_str2);
3824 return err;
3826 free(id_str1);
3827 free(id_str2);
3829 err = draw_file(view, header);
3830 free(header);
3831 return err;
3834 static const struct got_error *
3835 set_selected_commit(struct tog_diff_view_state *s,
3836 struct commit_queue_entry *entry)
3838 const struct got_error *err;
3839 const struct got_object_id_queue *parent_ids;
3840 struct got_commit_object *selected_commit;
3841 struct got_object_qid *pid;
3843 free(s->id2);
3844 s->id2 = got_object_id_dup(entry->id);
3845 if (s->id2 == NULL)
3846 return got_error_from_errno("got_object_id_dup");
3848 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3849 if (err)
3850 return err;
3851 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3852 free(s->id1);
3853 pid = STAILQ_FIRST(parent_ids);
3854 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3855 got_object_commit_close(selected_commit);
3856 return NULL;
3859 static const struct got_error *
3860 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3862 const struct got_error *err = NULL;
3863 struct tog_diff_view_state *s = &view->state.diff;
3864 struct tog_log_view_state *ls;
3865 struct commit_queue_entry *old_selected_entry;
3866 char *line = NULL;
3867 size_t linesize = 0;
3868 ssize_t linelen;
3869 int i, nscroll = view->nlines - 1;
3871 switch (ch) {
3872 case '0':
3873 view->x = 0;
3874 break;
3875 case '$':
3876 view->x = MAX(view->maxx - view->ncols / 3, 0);
3877 break;
3878 case KEY_RIGHT:
3879 case 'l':
3880 if (view->x + view->ncols / 3 < view->maxx)
3881 view->x += 2; /* move two columns right */
3882 break;
3883 case KEY_LEFT:
3884 case 'h':
3885 view->x -= MIN(view->x, 2); /* move two columns back */
3886 break;
3887 case 'a':
3888 case 'w':
3889 if (ch == 'a')
3890 s->force_text_diff = !s->force_text_diff;
3891 if (ch == 'w')
3892 s->ignore_whitespace = !s->ignore_whitespace;
3893 wclear(view->window);
3894 s->first_displayed_line = 1;
3895 s->last_displayed_line = view->nlines;
3896 s->matched_line = 0;
3897 diff_view_indicate_progress(view);
3898 err = create_diff(s);
3899 break;
3900 case 'g':
3901 case KEY_HOME:
3902 s->first_displayed_line = 1;
3903 break;
3904 case 'G':
3905 case KEY_END:
3906 if (s->eof)
3907 break;
3909 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3910 s->eof = 1;
3911 break;
3912 case 'k':
3913 case KEY_UP:
3914 case CTRL('p'):
3915 if (s->first_displayed_line > 1)
3916 s->first_displayed_line--;
3917 break;
3918 case CTRL('u'):
3919 case 'u':
3920 nscroll /= 2;
3921 /* FALL THROUGH */
3922 case KEY_PPAGE:
3923 case CTRL('b'):
3924 if (s->first_displayed_line == 1)
3925 break;
3926 i = 0;
3927 while (i++ < nscroll && s->first_displayed_line > 1)
3928 s->first_displayed_line--;
3929 break;
3930 case 'j':
3931 case KEY_DOWN:
3932 case CTRL('n'):
3933 if (!s->eof)
3934 s->first_displayed_line++;
3935 break;
3936 case CTRL('d'):
3937 case 'd':
3938 nscroll /= 2;
3939 /* FALL THROUGH */
3940 case KEY_NPAGE:
3941 case CTRL('f'):
3942 case ' ':
3943 if (s->eof)
3944 break;
3945 i = 0;
3946 while (!s->eof && i++ < nscroll) {
3947 linelen = getline(&line, &linesize, s->f);
3948 s->first_displayed_line++;
3949 if (linelen == -1) {
3950 if (feof(s->f)) {
3951 s->eof = 1;
3952 } else
3953 err = got_ferror(s->f, GOT_ERR_IO);
3954 break;
3957 free(line);
3958 break;
3959 case '[':
3960 if (s->diff_context > 0) {
3961 s->diff_context--;
3962 s->matched_line = 0;
3963 diff_view_indicate_progress(view);
3964 err = create_diff(s);
3965 if (s->first_displayed_line + view->nlines - 1 >
3966 s->nlines) {
3967 s->first_displayed_line = 1;
3968 s->last_displayed_line = view->nlines;
3971 break;
3972 case ']':
3973 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3974 s->diff_context++;
3975 s->matched_line = 0;
3976 diff_view_indicate_progress(view);
3977 err = create_diff(s);
3979 break;
3980 case '<':
3981 case ',':
3982 if (s->log_view == NULL)
3983 break;
3984 ls = &s->log_view->state.log;
3985 old_selected_entry = ls->selected_entry;
3987 err = input_log_view(NULL, s->log_view, KEY_UP);
3988 if (err)
3989 break;
3991 if (old_selected_entry == ls->selected_entry)
3992 break;
3994 err = set_selected_commit(s, ls->selected_entry);
3995 if (err)
3996 break;
3998 s->first_displayed_line = 1;
3999 s->last_displayed_line = view->nlines;
4000 s->matched_line = 0;
4001 view->x = 0;
4003 diff_view_indicate_progress(view);
4004 err = create_diff(s);
4005 break;
4006 case '>':
4007 case '.':
4008 if (s->log_view == NULL)
4009 break;
4010 ls = &s->log_view->state.log;
4011 old_selected_entry = ls->selected_entry;
4013 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4014 if (err)
4015 break;
4017 if (old_selected_entry == ls->selected_entry)
4018 break;
4020 err = set_selected_commit(s, ls->selected_entry);
4021 if (err)
4022 break;
4024 s->first_displayed_line = 1;
4025 s->last_displayed_line = view->nlines;
4026 s->matched_line = 0;
4027 view->x = 0;
4029 diff_view_indicate_progress(view);
4030 err = create_diff(s);
4031 break;
4032 default:
4033 break;
4036 return err;
4039 static const struct got_error *
4040 cmd_diff(int argc, char *argv[])
4042 const struct got_error *error = NULL;
4043 struct got_repository *repo = NULL;
4044 struct got_worktree *worktree = NULL;
4045 struct got_object_id *id1 = NULL, *id2 = NULL;
4046 char *repo_path = NULL, *cwd = NULL;
4047 char *id_str1 = NULL, *id_str2 = NULL;
4048 char *label1 = NULL, *label2 = NULL;
4049 int diff_context = 3, ignore_whitespace = 0;
4050 int ch, force_text_diff = 0;
4051 const char *errstr;
4052 struct tog_view *view;
4053 int *pack_fds = NULL;
4055 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4056 switch (ch) {
4057 case 'a':
4058 force_text_diff = 1;
4059 break;
4060 case 'C':
4061 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4062 &errstr);
4063 if (errstr != NULL)
4064 errx(1, "number of context lines is %s: %s",
4065 errstr, errstr);
4066 break;
4067 case 'r':
4068 repo_path = realpath(optarg, NULL);
4069 if (repo_path == NULL)
4070 return got_error_from_errno2("realpath",
4071 optarg);
4072 got_path_strip_trailing_slashes(repo_path);
4073 break;
4074 case 'w':
4075 ignore_whitespace = 1;
4076 break;
4077 default:
4078 usage_diff();
4079 /* NOTREACHED */
4083 argc -= optind;
4084 argv += optind;
4086 if (argc == 0) {
4087 usage_diff(); /* TODO show local worktree changes */
4088 } else if (argc == 2) {
4089 id_str1 = argv[0];
4090 id_str2 = argv[1];
4091 } else
4092 usage_diff();
4094 error = got_repo_pack_fds_open(&pack_fds);
4095 if (error)
4096 goto done;
4098 if (repo_path == NULL) {
4099 cwd = getcwd(NULL, 0);
4100 if (cwd == NULL)
4101 return got_error_from_errno("getcwd");
4102 error = got_worktree_open(&worktree, cwd);
4103 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4104 goto done;
4105 if (worktree)
4106 repo_path =
4107 strdup(got_worktree_get_repo_path(worktree));
4108 else
4109 repo_path = strdup(cwd);
4110 if (repo_path == NULL) {
4111 error = got_error_from_errno("strdup");
4112 goto done;
4116 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4117 if (error)
4118 goto done;
4120 init_curses();
4122 error = apply_unveil(got_repo_get_path(repo), NULL);
4123 if (error)
4124 goto done;
4126 error = tog_load_refs(repo, 0);
4127 if (error)
4128 goto done;
4130 error = got_repo_match_object_id(&id1, &label1, id_str1,
4131 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4132 if (error)
4133 goto done;
4135 error = got_repo_match_object_id(&id2, &label2, id_str2,
4136 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4137 if (error)
4138 goto done;
4140 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4141 if (view == NULL) {
4142 error = got_error_from_errno("view_open");
4143 goto done;
4145 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4146 ignore_whitespace, force_text_diff, NULL, repo);
4147 if (error)
4148 goto done;
4149 error = view_loop(view);
4150 done:
4151 free(label1);
4152 free(label2);
4153 free(repo_path);
4154 free(cwd);
4155 if (repo) {
4156 const struct got_error *close_err = got_repo_close(repo);
4157 if (error == NULL)
4158 error = close_err;
4160 if (worktree)
4161 got_worktree_close(worktree);
4162 if (pack_fds) {
4163 const struct got_error *pack_err =
4164 got_repo_pack_fds_close(pack_fds);
4165 if (error == NULL)
4166 error = pack_err;
4168 tog_free_refs();
4169 return error;
4172 __dead static void
4173 usage_blame(void)
4175 endwin();
4176 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4177 getprogname());
4178 exit(1);
4181 struct tog_blame_line {
4182 int annotated;
4183 struct got_object_id *id;
4186 static const struct got_error *
4187 draw_blame(struct tog_view *view)
4189 struct tog_blame_view_state *s = &view->state.blame;
4190 struct tog_blame *blame = &s->blame;
4191 regmatch_t *regmatch = &view->regmatch;
4192 const struct got_error *err;
4193 int lineno = 0, nprinted = 0;
4194 char *line = NULL;
4195 size_t linesize = 0;
4196 ssize_t linelen;
4197 wchar_t *wline;
4198 int width;
4199 struct tog_blame_line *blame_line;
4200 struct got_object_id *prev_id = NULL;
4201 char *id_str;
4202 struct tog_color *tc;
4204 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4205 if (err)
4206 return err;
4208 rewind(blame->f);
4209 werase(view->window);
4211 if (asprintf(&line, "commit %s", id_str) == -1) {
4212 err = got_error_from_errno("asprintf");
4213 free(id_str);
4214 return err;
4217 err = format_line(&wline, &width, line, view->ncols, 0, 0);
4218 free(line);
4219 line = NULL;
4220 if (err)
4221 return err;
4222 if (view_needs_focus_indication(view))
4223 wstandout(view->window);
4224 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4225 if (tc)
4226 wattr_on(view->window,
4227 COLOR_PAIR(tc->colorpair), NULL);
4228 waddwstr(view->window, wline);
4229 if (tc)
4230 wattr_off(view->window,
4231 COLOR_PAIR(tc->colorpair), NULL);
4232 if (view_needs_focus_indication(view))
4233 wstandend(view->window);
4234 free(wline);
4235 wline = NULL;
4236 if (width < view->ncols - 1)
4237 waddch(view->window, '\n');
4239 if (asprintf(&line, "[%d/%d] %s%s",
4240 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4241 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4242 free(id_str);
4243 return got_error_from_errno("asprintf");
4245 free(id_str);
4246 err = format_line(&wline, &width, line, view->ncols, 0, 0);
4247 free(line);
4248 line = NULL;
4249 if (err)
4250 return err;
4251 waddwstr(view->window, wline);
4252 free(wline);
4253 wline = NULL;
4254 if (width < view->ncols - 1)
4255 waddch(view->window, '\n');
4257 s->eof = 0;
4258 view->maxx = 0;
4259 while (nprinted < view->nlines - 2) {
4260 linelen = getline(&line, &linesize, blame->f);
4261 if (linelen == -1) {
4262 if (feof(blame->f)) {
4263 s->eof = 1;
4264 break;
4266 free(line);
4267 return got_ferror(blame->f, GOT_ERR_IO);
4269 if (++lineno < s->first_displayed_line)
4270 continue;
4272 view->maxx = MAX(view->maxx, linelen);
4274 if (view->focussed && nprinted == s->selected_line - 1)
4275 wstandout(view->window);
4277 if (blame->nlines > 0) {
4278 blame_line = &blame->lines[lineno - 1];
4279 if (blame_line->annotated && prev_id &&
4280 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4281 !(view->focussed &&
4282 nprinted == s->selected_line - 1)) {
4283 waddstr(view->window, " ");
4284 } else if (blame_line->annotated) {
4285 char *id_str;
4286 err = got_object_id_str(&id_str, blame_line->id);
4287 if (err) {
4288 free(line);
4289 return err;
4291 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4292 if (tc)
4293 wattr_on(view->window,
4294 COLOR_PAIR(tc->colorpair), NULL);
4295 wprintw(view->window, "%.8s", id_str);
4296 if (tc)
4297 wattr_off(view->window,
4298 COLOR_PAIR(tc->colorpair), NULL);
4299 free(id_str);
4300 prev_id = blame_line->id;
4301 } else {
4302 waddstr(view->window, "........");
4303 prev_id = NULL;
4305 } else {
4306 waddstr(view->window, "........");
4307 prev_id = NULL;
4310 if (view->focussed && nprinted == s->selected_line - 1)
4311 wstandend(view->window);
4312 waddstr(view->window, " ");
4314 if (view->ncols <= 9) {
4315 width = 9;
4316 wline = wcsdup(L"");
4317 if (wline == NULL) {
4318 err = got_error_from_errno("wcsdup");
4319 free(line);
4320 return err;
4322 } else if (s->first_displayed_line + nprinted ==
4323 s->matched_line &&
4324 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4325 err = add_matched_line(&width, line, view->ncols - 9, 9,
4326 view->window, view->x, regmatch);
4327 if (err) {
4328 free(line);
4329 return err;
4331 width += 9;
4332 } else {
4333 err = format_line(&wline, &width, line,
4334 view->x + view->ncols - 9, 9, 1);
4335 if (!err && view->x < width - 1) {
4336 waddwstr(view->window, wline + view->x);
4337 width += 9;
4339 free(wline);
4340 wline = NULL;
4341 if (err)
4342 return err;
4345 if (width <= view->ncols - 1)
4346 waddch(view->window, '\n');
4347 if (++nprinted == 1)
4348 s->first_displayed_line = lineno;
4350 free(line);
4351 s->last_displayed_line = lineno;
4353 view_vborder(view);
4355 return NULL;
4358 static const struct got_error *
4359 blame_cb(void *arg, int nlines, int lineno,
4360 struct got_commit_object *commit, struct got_object_id *id)
4362 const struct got_error *err = NULL;
4363 struct tog_blame_cb_args *a = arg;
4364 struct tog_blame_line *line;
4365 int errcode;
4367 if (nlines != a->nlines ||
4368 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4369 return got_error(GOT_ERR_RANGE);
4371 errcode = pthread_mutex_lock(&tog_mutex);
4372 if (errcode)
4373 return got_error_set_errno(errcode, "pthread_mutex_lock");
4375 if (*a->quit) { /* user has quit the blame view */
4376 err = got_error(GOT_ERR_ITER_COMPLETED);
4377 goto done;
4380 if (lineno == -1)
4381 goto done; /* no change in this commit */
4383 line = &a->lines[lineno - 1];
4384 if (line->annotated)
4385 goto done;
4387 line->id = got_object_id_dup(id);
4388 if (line->id == NULL) {
4389 err = got_error_from_errno("got_object_id_dup");
4390 goto done;
4392 line->annotated = 1;
4393 done:
4394 errcode = pthread_mutex_unlock(&tog_mutex);
4395 if (errcode)
4396 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4397 return err;
4400 static void *
4401 blame_thread(void *arg)
4403 const struct got_error *err, *close_err;
4404 struct tog_blame_thread_args *ta = arg;
4405 struct tog_blame_cb_args *a = ta->cb_args;
4406 int errcode;
4408 err = block_signals_used_by_main_thread();
4409 if (err)
4410 return (void *)err;
4412 err = got_blame(ta->path, a->commit_id, ta->repo,
4413 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4414 if (err && err->code == GOT_ERR_CANCELLED)
4415 err = NULL;
4417 errcode = pthread_mutex_lock(&tog_mutex);
4418 if (errcode)
4419 return (void *)got_error_set_errno(errcode,
4420 "pthread_mutex_lock");
4422 close_err = got_repo_close(ta->repo);
4423 if (err == NULL)
4424 err = close_err;
4425 ta->repo = NULL;
4426 *ta->complete = 1;
4428 errcode = pthread_mutex_unlock(&tog_mutex);
4429 if (errcode && err == NULL)
4430 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4432 return (void *)err;
4435 static struct got_object_id *
4436 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4437 int first_displayed_line, int selected_line)
4439 struct tog_blame_line *line;
4441 if (nlines <= 0)
4442 return NULL;
4444 line = &lines[first_displayed_line - 1 + selected_line - 1];
4445 if (!line->annotated)
4446 return NULL;
4448 return line->id;
4451 static const struct got_error *
4452 stop_blame(struct tog_blame *blame)
4454 const struct got_error *err = NULL;
4455 int i;
4457 if (blame->thread) {
4458 int errcode;
4459 errcode = pthread_mutex_unlock(&tog_mutex);
4460 if (errcode)
4461 return got_error_set_errno(errcode,
4462 "pthread_mutex_unlock");
4463 errcode = pthread_join(blame->thread, (void **)&err);
4464 if (errcode)
4465 return got_error_set_errno(errcode, "pthread_join");
4466 errcode = pthread_mutex_lock(&tog_mutex);
4467 if (errcode)
4468 return got_error_set_errno(errcode,
4469 "pthread_mutex_lock");
4470 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4471 err = NULL;
4472 blame->thread = NULL;
4474 if (blame->thread_args.repo) {
4475 const struct got_error *close_err;
4476 close_err = got_repo_close(blame->thread_args.repo);
4477 if (err == NULL)
4478 err = close_err;
4479 blame->thread_args.repo = NULL;
4481 if (blame->f) {
4482 if (fclose(blame->f) == EOF && err == NULL)
4483 err = got_error_from_errno("fclose");
4484 blame->f = NULL;
4486 if (blame->lines) {
4487 for (i = 0; i < blame->nlines; i++)
4488 free(blame->lines[i].id);
4489 free(blame->lines);
4490 blame->lines = NULL;
4492 free(blame->cb_args.commit_id);
4493 blame->cb_args.commit_id = NULL;
4494 if (blame->pack_fds) {
4495 const struct got_error *pack_err =
4496 got_repo_pack_fds_close(blame->pack_fds);
4497 if (err == NULL)
4498 err = pack_err;
4499 blame->pack_fds = NULL;
4501 return err;
4504 static const struct got_error *
4505 cancel_blame_view(void *arg)
4507 const struct got_error *err = NULL;
4508 int *done = arg;
4509 int errcode;
4511 errcode = pthread_mutex_lock(&tog_mutex);
4512 if (errcode)
4513 return got_error_set_errno(errcode,
4514 "pthread_mutex_unlock");
4516 if (*done)
4517 err = got_error(GOT_ERR_CANCELLED);
4519 errcode = pthread_mutex_unlock(&tog_mutex);
4520 if (errcode)
4521 return got_error_set_errno(errcode,
4522 "pthread_mutex_lock");
4524 return err;
4527 static const struct got_error *
4528 run_blame(struct tog_view *view)
4530 struct tog_blame_view_state *s = &view->state.blame;
4531 struct tog_blame *blame = &s->blame;
4532 const struct got_error *err = NULL;
4533 struct got_commit_object *commit = NULL;
4534 struct got_blob_object *blob = NULL;
4535 struct got_repository *thread_repo = NULL;
4536 struct got_object_id *obj_id = NULL;
4537 int obj_type;
4538 int *pack_fds = NULL;
4540 err = got_object_open_as_commit(&commit, s->repo,
4541 &s->blamed_commit->id);
4542 if (err)
4543 return err;
4545 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4546 if (err)
4547 goto done;
4549 err = got_object_get_type(&obj_type, s->repo, obj_id);
4550 if (err)
4551 goto done;
4553 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4554 err = got_error(GOT_ERR_OBJ_TYPE);
4555 goto done;
4558 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4559 if (err)
4560 goto done;
4561 blame->f = got_opentemp();
4562 if (blame->f == NULL) {
4563 err = got_error_from_errno("got_opentemp");
4564 goto done;
4566 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4567 &blame->line_offsets, blame->f, blob);
4568 if (err)
4569 goto done;
4570 if (blame->nlines == 0) {
4571 s->blame_complete = 1;
4572 goto done;
4575 /* Don't include \n at EOF in the blame line count. */
4576 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4577 blame->nlines--;
4579 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4580 if (blame->lines == NULL) {
4581 err = got_error_from_errno("calloc");
4582 goto done;
4585 err = got_repo_pack_fds_open(&pack_fds);
4586 if (err)
4587 goto done;
4588 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4589 pack_fds);
4590 if (err)
4591 goto done;
4593 blame->pack_fds = pack_fds;
4594 blame->cb_args.view = view;
4595 blame->cb_args.lines = blame->lines;
4596 blame->cb_args.nlines = blame->nlines;
4597 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4598 if (blame->cb_args.commit_id == NULL) {
4599 err = got_error_from_errno("got_object_id_dup");
4600 goto done;
4602 blame->cb_args.quit = &s->done;
4604 blame->thread_args.path = s->path;
4605 blame->thread_args.repo = thread_repo;
4606 blame->thread_args.cb_args = &blame->cb_args;
4607 blame->thread_args.complete = &s->blame_complete;
4608 blame->thread_args.cancel_cb = cancel_blame_view;
4609 blame->thread_args.cancel_arg = &s->done;
4610 s->blame_complete = 0;
4612 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4613 s->first_displayed_line = 1;
4614 s->last_displayed_line = view->nlines;
4615 s->selected_line = 1;
4617 s->matched_line = 0;
4619 done:
4620 if (commit)
4621 got_object_commit_close(commit);
4622 if (blob)
4623 got_object_blob_close(blob);
4624 free(obj_id);
4625 if (err)
4626 stop_blame(blame);
4627 return err;
4630 static const struct got_error *
4631 open_blame_view(struct tog_view *view, char *path,
4632 struct got_object_id *commit_id, struct got_repository *repo)
4634 const struct got_error *err = NULL;
4635 struct tog_blame_view_state *s = &view->state.blame;
4637 STAILQ_INIT(&s->blamed_commits);
4639 s->path = strdup(path);
4640 if (s->path == NULL)
4641 return got_error_from_errno("strdup");
4643 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4644 if (err) {
4645 free(s->path);
4646 return err;
4649 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4650 s->first_displayed_line = 1;
4651 s->last_displayed_line = view->nlines;
4652 s->selected_line = 1;
4653 s->blame_complete = 0;
4654 s->repo = repo;
4655 s->commit_id = commit_id;
4656 memset(&s->blame, 0, sizeof(s->blame));
4658 STAILQ_INIT(&s->colors);
4659 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4660 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4661 get_color_value("TOG_COLOR_COMMIT"));
4662 if (err)
4663 return err;
4666 view->show = show_blame_view;
4667 view->input = input_blame_view;
4668 view->close = close_blame_view;
4669 view->search_start = search_start_blame_view;
4670 view->search_next = search_next_blame_view;
4672 return run_blame(view);
4675 static const struct got_error *
4676 close_blame_view(struct tog_view *view)
4678 const struct got_error *err = NULL;
4679 struct tog_blame_view_state *s = &view->state.blame;
4681 if (s->blame.thread)
4682 err = stop_blame(&s->blame);
4684 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4685 struct got_object_qid *blamed_commit;
4686 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4687 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4688 got_object_qid_free(blamed_commit);
4691 free(s->path);
4692 free_colors(&s->colors);
4693 return err;
4696 static const struct got_error *
4697 search_start_blame_view(struct tog_view *view)
4699 struct tog_blame_view_state *s = &view->state.blame;
4701 s->matched_line = 0;
4702 return NULL;
4705 static const struct got_error *
4706 search_next_blame_view(struct tog_view *view)
4708 struct tog_blame_view_state *s = &view->state.blame;
4709 const struct got_error *err = NULL;
4710 int lineno;
4711 char *exstr = NULL, *line = NULL;
4712 size_t linesize = 0;
4713 ssize_t linelen;
4715 if (!view->searching) {
4716 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4717 return NULL;
4720 if (s->matched_line) {
4721 if (view->searching == TOG_SEARCH_FORWARD)
4722 lineno = s->matched_line + 1;
4723 else
4724 lineno = s->matched_line - 1;
4725 } else
4726 lineno = s->first_displayed_line - 1 + s->selected_line;
4728 while (1) {
4729 off_t offset;
4731 if (lineno <= 0 || lineno > s->blame.nlines) {
4732 if (s->matched_line == 0) {
4733 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4734 break;
4737 if (view->searching == TOG_SEARCH_FORWARD)
4738 lineno = 1;
4739 else
4740 lineno = s->blame.nlines;
4743 offset = s->blame.line_offsets[lineno - 1];
4744 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4745 free(line);
4746 return got_error_from_errno("fseeko");
4748 linelen = getline(&line, &linesize, s->blame.f);
4749 err = expand_tab(&exstr, line);
4750 if (err)
4751 break;
4752 if (linelen != -1 &&
4753 match_line(exstr, &view->regex, 1, &view->regmatch)) {
4754 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4755 s->matched_line = lineno;
4756 break;
4758 free(exstr);
4759 exstr = NULL;
4760 if (view->searching == TOG_SEARCH_FORWARD)
4761 lineno++;
4762 else
4763 lineno--;
4765 free(line);
4766 free(exstr);
4768 if (s->matched_line) {
4769 s->first_displayed_line = s->matched_line;
4770 s->selected_line = 1;
4773 return err;
4776 static const struct got_error *
4777 show_blame_view(struct tog_view *view)
4779 const struct got_error *err = NULL;
4780 struct tog_blame_view_state *s = &view->state.blame;
4781 int errcode;
4783 if (s->blame.thread == NULL && !s->blame_complete) {
4784 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4785 &s->blame.thread_args);
4786 if (errcode)
4787 return got_error_set_errno(errcode, "pthread_create");
4789 halfdelay(1); /* fast refresh while annotating */
4792 if (s->blame_complete)
4793 halfdelay(10); /* disable fast refresh */
4795 err = draw_blame(view);
4797 view_vborder(view);
4798 return err;
4801 static const struct got_error *
4802 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4804 const struct got_error *err = NULL, *thread_err = NULL;
4805 struct tog_view *diff_view;
4806 struct tog_blame_view_state *s = &view->state.blame;
4807 int begin_x = 0, nscroll = view->nlines - 2;
4809 switch (ch) {
4810 case '0':
4811 view->x = 0;
4812 break;
4813 case '$':
4814 view->x = MAX(view->maxx - view->ncols / 3, 0);
4815 break;
4816 case KEY_RIGHT:
4817 case 'l':
4818 if (view->x + view->ncols / 3 < view->maxx)
4819 view->x += 2; /* move two columns right */
4820 break;
4821 case KEY_LEFT:
4822 case 'h':
4823 view->x -= MIN(view->x, 2); /* move two columns back */
4824 break;
4825 case 'q':
4826 s->done = 1;
4827 break;
4828 case 'g':
4829 case KEY_HOME:
4830 s->selected_line = 1;
4831 s->first_displayed_line = 1;
4832 break;
4833 case 'G':
4834 case KEY_END:
4835 if (s->blame.nlines < view->nlines - 2) {
4836 s->selected_line = s->blame.nlines;
4837 s->first_displayed_line = 1;
4838 } else {
4839 s->selected_line = view->nlines - 2;
4840 s->first_displayed_line = s->blame.nlines -
4841 (view->nlines - 3);
4843 break;
4844 case 'k':
4845 case KEY_UP:
4846 case CTRL('p'):
4847 if (s->selected_line > 1)
4848 s->selected_line--;
4849 else if (s->selected_line == 1 &&
4850 s->first_displayed_line > 1)
4851 s->first_displayed_line--;
4852 break;
4853 case CTRL('u'):
4854 case 'u':
4855 nscroll /= 2;
4856 /* FALL THROUGH */
4857 case KEY_PPAGE:
4858 case CTRL('b'):
4859 if (s->first_displayed_line == 1) {
4860 s->selected_line = MAX(1, s->selected_line - nscroll);
4861 break;
4863 if (s->first_displayed_line > nscroll)
4864 s->first_displayed_line -= nscroll;
4865 else
4866 s->first_displayed_line = 1;
4867 break;
4868 case 'j':
4869 case KEY_DOWN:
4870 case CTRL('n'):
4871 if (s->selected_line < view->nlines - 2 &&
4872 s->first_displayed_line +
4873 s->selected_line <= s->blame.nlines)
4874 s->selected_line++;
4875 else if (s->last_displayed_line <
4876 s->blame.nlines)
4877 s->first_displayed_line++;
4878 break;
4879 case 'b':
4880 case 'p': {
4881 struct got_object_id *id = NULL;
4882 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4883 s->first_displayed_line, s->selected_line);
4884 if (id == NULL)
4885 break;
4886 if (ch == 'p') {
4887 struct got_commit_object *commit, *pcommit;
4888 struct got_object_qid *pid;
4889 struct got_object_id *blob_id = NULL;
4890 int obj_type;
4891 err = got_object_open_as_commit(&commit,
4892 s->repo, id);
4893 if (err)
4894 break;
4895 pid = STAILQ_FIRST(
4896 got_object_commit_get_parent_ids(commit));
4897 if (pid == NULL) {
4898 got_object_commit_close(commit);
4899 break;
4901 /* Check if path history ends here. */
4902 err = got_object_open_as_commit(&pcommit,
4903 s->repo, &pid->id);
4904 if (err)
4905 break;
4906 err = got_object_id_by_path(&blob_id, s->repo,
4907 pcommit, s->path);
4908 got_object_commit_close(pcommit);
4909 if (err) {
4910 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4911 err = NULL;
4912 got_object_commit_close(commit);
4913 break;
4915 err = got_object_get_type(&obj_type, s->repo,
4916 blob_id);
4917 free(blob_id);
4918 /* Can't blame non-blob type objects. */
4919 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4920 got_object_commit_close(commit);
4921 break;
4923 err = got_object_qid_alloc(&s->blamed_commit,
4924 &pid->id);
4925 got_object_commit_close(commit);
4926 } else {
4927 if (got_object_id_cmp(id,
4928 &s->blamed_commit->id) == 0)
4929 break;
4930 err = got_object_qid_alloc(&s->blamed_commit,
4931 id);
4933 if (err)
4934 break;
4935 s->done = 1;
4936 thread_err = stop_blame(&s->blame);
4937 s->done = 0;
4938 if (thread_err)
4939 break;
4940 STAILQ_INSERT_HEAD(&s->blamed_commits,
4941 s->blamed_commit, entry);
4942 err = run_blame(view);
4943 if (err)
4944 break;
4945 break;
4947 case 'B': {
4948 struct got_object_qid *first;
4949 first = STAILQ_FIRST(&s->blamed_commits);
4950 if (!got_object_id_cmp(&first->id, s->commit_id))
4951 break;
4952 s->done = 1;
4953 thread_err = stop_blame(&s->blame);
4954 s->done = 0;
4955 if (thread_err)
4956 break;
4957 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4958 got_object_qid_free(s->blamed_commit);
4959 s->blamed_commit =
4960 STAILQ_FIRST(&s->blamed_commits);
4961 err = run_blame(view);
4962 if (err)
4963 break;
4964 break;
4966 case KEY_ENTER:
4967 case '\r': {
4968 struct got_object_id *id = NULL;
4969 struct got_object_qid *pid;
4970 struct got_commit_object *commit = NULL;
4971 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4972 s->first_displayed_line, s->selected_line);
4973 if (id == NULL)
4974 break;
4975 err = got_object_open_as_commit(&commit, s->repo, id);
4976 if (err)
4977 break;
4978 pid = STAILQ_FIRST(
4979 got_object_commit_get_parent_ids(commit));
4980 if (view_is_parent_view(view))
4981 begin_x = view_split_begin_x(view->begin_x);
4982 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4983 if (diff_view == NULL) {
4984 got_object_commit_close(commit);
4985 err = got_error_from_errno("view_open");
4986 break;
4988 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4989 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4990 got_object_commit_close(commit);
4991 if (err) {
4992 view_close(diff_view);
4993 break;
4995 view->focussed = 0;
4996 diff_view->focussed = 1;
4997 if (view_is_parent_view(view)) {
4998 err = view_close_child(view);
4999 if (err)
5000 break;
5001 view_set_child(view, diff_view);
5002 view->focus_child = 1;
5003 } else
5004 *new_view = diff_view;
5005 if (err)
5006 break;
5007 break;
5009 case CTRL('d'):
5010 case 'd':
5011 nscroll /= 2;
5012 /* FALL THROUGH */
5013 case KEY_NPAGE:
5014 case CTRL('f'):
5015 case ' ':
5016 if (s->last_displayed_line >= s->blame.nlines &&
5017 s->selected_line >= MIN(s->blame.nlines,
5018 view->nlines - 2)) {
5019 break;
5021 if (s->last_displayed_line >= s->blame.nlines &&
5022 s->selected_line < view->nlines - 2) {
5023 s->selected_line +=
5024 MIN(nscroll, s->last_displayed_line -
5025 s->first_displayed_line - s->selected_line + 1);
5027 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5028 s->first_displayed_line += nscroll;
5029 else
5030 s->first_displayed_line =
5031 s->blame.nlines - (view->nlines - 3);
5032 break;
5033 case KEY_RESIZE:
5034 if (s->selected_line > view->nlines - 2) {
5035 s->selected_line = MIN(s->blame.nlines,
5036 view->nlines - 2);
5038 break;
5039 default:
5040 break;
5042 return thread_err ? thread_err : err;
5045 static const struct got_error *
5046 cmd_blame(int argc, char *argv[])
5048 const struct got_error *error;
5049 struct got_repository *repo = NULL;
5050 struct got_worktree *worktree = NULL;
5051 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5052 char *link_target = NULL;
5053 struct got_object_id *commit_id = NULL;
5054 struct got_commit_object *commit = NULL;
5055 char *commit_id_str = NULL;
5056 int ch;
5057 struct tog_view *view;
5058 int *pack_fds = NULL;
5060 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5061 switch (ch) {
5062 case 'c':
5063 commit_id_str = optarg;
5064 break;
5065 case 'r':
5066 repo_path = realpath(optarg, NULL);
5067 if (repo_path == NULL)
5068 return got_error_from_errno2("realpath",
5069 optarg);
5070 break;
5071 default:
5072 usage_blame();
5073 /* NOTREACHED */
5077 argc -= optind;
5078 argv += optind;
5080 if (argc != 1)
5081 usage_blame();
5083 error = got_repo_pack_fds_open(&pack_fds);
5084 if (error != NULL)
5085 goto done;
5087 if (repo_path == NULL) {
5088 cwd = getcwd(NULL, 0);
5089 if (cwd == NULL)
5090 return got_error_from_errno("getcwd");
5091 error = got_worktree_open(&worktree, cwd);
5092 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5093 goto done;
5094 if (worktree)
5095 repo_path =
5096 strdup(got_worktree_get_repo_path(worktree));
5097 else
5098 repo_path = strdup(cwd);
5099 if (repo_path == NULL) {
5100 error = got_error_from_errno("strdup");
5101 goto done;
5105 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5106 if (error != NULL)
5107 goto done;
5109 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5110 worktree);
5111 if (error)
5112 goto done;
5114 init_curses();
5116 error = apply_unveil(got_repo_get_path(repo), NULL);
5117 if (error)
5118 goto done;
5120 error = tog_load_refs(repo, 0);
5121 if (error)
5122 goto done;
5124 if (commit_id_str == NULL) {
5125 struct got_reference *head_ref;
5126 error = got_ref_open(&head_ref, repo, worktree ?
5127 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5128 if (error != NULL)
5129 goto done;
5130 error = got_ref_resolve(&commit_id, repo, head_ref);
5131 got_ref_close(head_ref);
5132 } else {
5133 error = got_repo_match_object_id(&commit_id, NULL,
5134 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5136 if (error != NULL)
5137 goto done;
5139 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5140 if (view == NULL) {
5141 error = got_error_from_errno("view_open");
5142 goto done;
5145 error = got_object_open_as_commit(&commit, repo, commit_id);
5146 if (error)
5147 goto done;
5149 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5150 commit, repo);
5151 if (error)
5152 goto done;
5154 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5155 commit_id, repo);
5156 if (error)
5157 goto done;
5158 if (worktree) {
5159 /* Release work tree lock. */
5160 got_worktree_close(worktree);
5161 worktree = NULL;
5163 error = view_loop(view);
5164 done:
5165 free(repo_path);
5166 free(in_repo_path);
5167 free(link_target);
5168 free(cwd);
5169 free(commit_id);
5170 if (commit)
5171 got_object_commit_close(commit);
5172 if (worktree)
5173 got_worktree_close(worktree);
5174 if (repo) {
5175 const struct got_error *close_err = got_repo_close(repo);
5176 if (error == NULL)
5177 error = close_err;
5179 if (pack_fds) {
5180 const struct got_error *pack_err =
5181 got_repo_pack_fds_close(pack_fds);
5182 if (error == NULL)
5183 error = pack_err;
5185 tog_free_refs();
5186 return error;
5189 static const struct got_error *
5190 draw_tree_entries(struct tog_view *view, const char *parent_path)
5192 struct tog_tree_view_state *s = &view->state.tree;
5193 const struct got_error *err = NULL;
5194 struct got_tree_entry *te;
5195 wchar_t *wline;
5196 struct tog_color *tc;
5197 int width, n, i, nentries;
5198 int limit = view->nlines;
5200 s->ndisplayed = 0;
5202 werase(view->window);
5204 if (limit == 0)
5205 return NULL;
5207 err = format_line(&wline, &width, s->tree_label, view->ncols, 0, 0);
5208 if (err)
5209 return err;
5210 if (view_needs_focus_indication(view))
5211 wstandout(view->window);
5212 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5213 if (tc)
5214 wattr_on(view->window,
5215 COLOR_PAIR(tc->colorpair), NULL);
5216 waddwstr(view->window, wline);
5217 if (tc)
5218 wattr_off(view->window,
5219 COLOR_PAIR(tc->colorpair), NULL);
5220 if (view_needs_focus_indication(view))
5221 wstandend(view->window);
5222 free(wline);
5223 wline = NULL;
5224 if (width < view->ncols - 1)
5225 waddch(view->window, '\n');
5226 if (--limit <= 0)
5227 return NULL;
5228 err = format_line(&wline, &width, parent_path, view->ncols, 0, 0);
5229 if (err)
5230 return err;
5231 waddwstr(view->window, wline);
5232 free(wline);
5233 wline = NULL;
5234 if (width < view->ncols - 1)
5235 waddch(view->window, '\n');
5236 if (--limit <= 0)
5237 return NULL;
5238 waddch(view->window, '\n');
5239 if (--limit <= 0)
5240 return NULL;
5242 if (s->first_displayed_entry == NULL) {
5243 te = got_object_tree_get_first_entry(s->tree);
5244 if (s->selected == 0) {
5245 if (view->focussed)
5246 wstandout(view->window);
5247 s->selected_entry = NULL;
5249 waddstr(view->window, " ..\n"); /* parent directory */
5250 if (s->selected == 0 && view->focussed)
5251 wstandend(view->window);
5252 s->ndisplayed++;
5253 if (--limit <= 0)
5254 return NULL;
5255 n = 1;
5256 } else {
5257 n = 0;
5258 te = s->first_displayed_entry;
5261 nentries = got_object_tree_get_nentries(s->tree);
5262 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5263 char *line = NULL, *id_str = NULL, *link_target = NULL;
5264 const char *modestr = "";
5265 mode_t mode;
5267 te = got_object_tree_get_entry(s->tree, i);
5268 mode = got_tree_entry_get_mode(te);
5270 if (s->show_ids) {
5271 err = got_object_id_str(&id_str,
5272 got_tree_entry_get_id(te));
5273 if (err)
5274 return got_error_from_errno(
5275 "got_object_id_str");
5277 if (got_object_tree_entry_is_submodule(te))
5278 modestr = "$";
5279 else if (S_ISLNK(mode)) {
5280 int i;
5282 err = got_tree_entry_get_symlink_target(&link_target,
5283 te, s->repo);
5284 if (err) {
5285 free(id_str);
5286 return err;
5288 for (i = 0; i < strlen(link_target); i++) {
5289 if (!isprint((unsigned char)link_target[i]))
5290 link_target[i] = '?';
5292 modestr = "@";
5294 else if (S_ISDIR(mode))
5295 modestr = "/";
5296 else if (mode & S_IXUSR)
5297 modestr = "*";
5298 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5299 got_tree_entry_get_name(te), modestr,
5300 link_target ? " -> ": "",
5301 link_target ? link_target : "") == -1) {
5302 free(id_str);
5303 free(link_target);
5304 return got_error_from_errno("asprintf");
5306 free(id_str);
5307 free(link_target);
5308 err = format_line(&wline, &width, line, view->ncols, 0, 0);
5309 if (err) {
5310 free(line);
5311 break;
5313 if (n == s->selected) {
5314 if (view->focussed)
5315 wstandout(view->window);
5316 s->selected_entry = te;
5318 tc = match_color(&s->colors, line);
5319 if (tc)
5320 wattr_on(view->window,
5321 COLOR_PAIR(tc->colorpair), NULL);
5322 waddwstr(view->window, wline);
5323 if (tc)
5324 wattr_off(view->window,
5325 COLOR_PAIR(tc->colorpair), NULL);
5326 if (width < view->ncols - 1)
5327 waddch(view->window, '\n');
5328 if (n == s->selected && view->focussed)
5329 wstandend(view->window);
5330 free(line);
5331 free(wline);
5332 wline = NULL;
5333 n++;
5334 s->ndisplayed++;
5335 s->last_displayed_entry = te;
5336 if (--limit <= 0)
5337 break;
5340 return err;
5343 static void
5344 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5346 struct got_tree_entry *te;
5347 int isroot = s->tree == s->root;
5348 int i = 0;
5350 if (s->first_displayed_entry == NULL)
5351 return;
5353 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5354 while (i++ < maxscroll) {
5355 if (te == NULL) {
5356 if (!isroot)
5357 s->first_displayed_entry = NULL;
5358 break;
5360 s->first_displayed_entry = te;
5361 te = got_tree_entry_get_prev(s->tree, te);
5365 static void
5366 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5368 struct got_tree_entry *next, *last;
5369 int n = 0;
5371 if (s->first_displayed_entry)
5372 next = got_tree_entry_get_next(s->tree,
5373 s->first_displayed_entry);
5374 else
5375 next = got_object_tree_get_first_entry(s->tree);
5377 last = s->last_displayed_entry;
5378 while (next && last && n++ < maxscroll) {
5379 last = got_tree_entry_get_next(s->tree, last);
5380 if (last) {
5381 s->first_displayed_entry = next;
5382 next = got_tree_entry_get_next(s->tree, next);
5387 static const struct got_error *
5388 tree_entry_path(char **path, struct tog_parent_trees *parents,
5389 struct got_tree_entry *te)
5391 const struct got_error *err = NULL;
5392 struct tog_parent_tree *pt;
5393 size_t len = 2; /* for leading slash and NUL */
5395 TAILQ_FOREACH(pt, parents, entry)
5396 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5397 + 1 /* slash */;
5398 if (te)
5399 len += strlen(got_tree_entry_get_name(te));
5401 *path = calloc(1, len);
5402 if (path == NULL)
5403 return got_error_from_errno("calloc");
5405 (*path)[0] = '/';
5406 pt = TAILQ_LAST(parents, tog_parent_trees);
5407 while (pt) {
5408 const char *name = got_tree_entry_get_name(pt->selected_entry);
5409 if (strlcat(*path, name, len) >= len) {
5410 err = got_error(GOT_ERR_NO_SPACE);
5411 goto done;
5413 if (strlcat(*path, "/", len) >= len) {
5414 err = got_error(GOT_ERR_NO_SPACE);
5415 goto done;
5417 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5419 if (te) {
5420 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5421 err = got_error(GOT_ERR_NO_SPACE);
5422 goto done;
5425 done:
5426 if (err) {
5427 free(*path);
5428 *path = NULL;
5430 return err;
5433 static const struct got_error *
5434 blame_tree_entry(struct tog_view **new_view, int begin_x,
5435 struct got_tree_entry *te, struct tog_parent_trees *parents,
5436 struct got_object_id *commit_id, struct got_repository *repo)
5438 const struct got_error *err = NULL;
5439 char *path;
5440 struct tog_view *blame_view;
5442 *new_view = NULL;
5444 err = tree_entry_path(&path, parents, te);
5445 if (err)
5446 return err;
5448 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5449 if (blame_view == NULL) {
5450 err = got_error_from_errno("view_open");
5451 goto done;
5454 err = open_blame_view(blame_view, path, commit_id, repo);
5455 if (err) {
5456 if (err->code == GOT_ERR_CANCELLED)
5457 err = NULL;
5458 view_close(blame_view);
5459 } else
5460 *new_view = blame_view;
5461 done:
5462 free(path);
5463 return err;
5466 static const struct got_error *
5467 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5468 struct tog_tree_view_state *s)
5470 struct tog_view *log_view;
5471 const struct got_error *err = NULL;
5472 char *path;
5474 *new_view = NULL;
5476 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5477 if (log_view == NULL)
5478 return got_error_from_errno("view_open");
5480 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5481 if (err)
5482 return err;
5484 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5485 path, 0);
5486 if (err)
5487 view_close(log_view);
5488 else
5489 *new_view = log_view;
5490 free(path);
5491 return err;
5494 static const struct got_error *
5495 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5496 const char *head_ref_name, struct got_repository *repo)
5498 const struct got_error *err = NULL;
5499 char *commit_id_str = NULL;
5500 struct tog_tree_view_state *s = &view->state.tree;
5501 struct got_commit_object *commit = NULL;
5503 TAILQ_INIT(&s->parents);
5504 STAILQ_INIT(&s->colors);
5506 s->commit_id = got_object_id_dup(commit_id);
5507 if (s->commit_id == NULL)
5508 return got_error_from_errno("got_object_id_dup");
5510 err = got_object_open_as_commit(&commit, repo, commit_id);
5511 if (err)
5512 goto done;
5515 * The root is opened here and will be closed when the view is closed.
5516 * Any visited subtrees and their path-wise parents are opened and
5517 * closed on demand.
5519 err = got_object_open_as_tree(&s->root, repo,
5520 got_object_commit_get_tree_id(commit));
5521 if (err)
5522 goto done;
5523 s->tree = s->root;
5525 err = got_object_id_str(&commit_id_str, commit_id);
5526 if (err != NULL)
5527 goto done;
5529 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5530 err = got_error_from_errno("asprintf");
5531 goto done;
5534 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5535 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5536 if (head_ref_name) {
5537 s->head_ref_name = strdup(head_ref_name);
5538 if (s->head_ref_name == NULL) {
5539 err = got_error_from_errno("strdup");
5540 goto done;
5543 s->repo = repo;
5545 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5546 err = add_color(&s->colors, "\\$$",
5547 TOG_COLOR_TREE_SUBMODULE,
5548 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5549 if (err)
5550 goto done;
5551 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5552 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5553 if (err)
5554 goto done;
5555 err = add_color(&s->colors, "/$",
5556 TOG_COLOR_TREE_DIRECTORY,
5557 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5558 if (err)
5559 goto done;
5561 err = add_color(&s->colors, "\\*$",
5562 TOG_COLOR_TREE_EXECUTABLE,
5563 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5564 if (err)
5565 goto done;
5567 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5568 get_color_value("TOG_COLOR_COMMIT"));
5569 if (err)
5570 goto done;
5573 view->show = show_tree_view;
5574 view->input = input_tree_view;
5575 view->close = close_tree_view;
5576 view->search_start = search_start_tree_view;
5577 view->search_next = search_next_tree_view;
5578 done:
5579 free(commit_id_str);
5580 if (commit)
5581 got_object_commit_close(commit);
5582 if (err)
5583 close_tree_view(view);
5584 return err;
5587 static const struct got_error *
5588 close_tree_view(struct tog_view *view)
5590 struct tog_tree_view_state *s = &view->state.tree;
5592 free_colors(&s->colors);
5593 free(s->tree_label);
5594 s->tree_label = NULL;
5595 free(s->commit_id);
5596 s->commit_id = NULL;
5597 free(s->head_ref_name);
5598 s->head_ref_name = NULL;
5599 while (!TAILQ_EMPTY(&s->parents)) {
5600 struct tog_parent_tree *parent;
5601 parent = TAILQ_FIRST(&s->parents);
5602 TAILQ_REMOVE(&s->parents, parent, entry);
5603 if (parent->tree != s->root)
5604 got_object_tree_close(parent->tree);
5605 free(parent);
5608 if (s->tree != NULL && s->tree != s->root)
5609 got_object_tree_close(s->tree);
5610 if (s->root)
5611 got_object_tree_close(s->root);
5612 return NULL;
5615 static const struct got_error *
5616 search_start_tree_view(struct tog_view *view)
5618 struct tog_tree_view_state *s = &view->state.tree;
5620 s->matched_entry = NULL;
5621 return NULL;
5624 static int
5625 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5627 regmatch_t regmatch;
5629 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5630 0) == 0;
5633 static const struct got_error *
5634 search_next_tree_view(struct tog_view *view)
5636 struct tog_tree_view_state *s = &view->state.tree;
5637 struct got_tree_entry *te = NULL;
5639 if (!view->searching) {
5640 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5641 return NULL;
5644 if (s->matched_entry) {
5645 if (view->searching == TOG_SEARCH_FORWARD) {
5646 if (s->selected_entry)
5647 te = got_tree_entry_get_next(s->tree,
5648 s->selected_entry);
5649 else
5650 te = got_object_tree_get_first_entry(s->tree);
5651 } else {
5652 if (s->selected_entry == NULL)
5653 te = got_object_tree_get_last_entry(s->tree);
5654 else
5655 te = got_tree_entry_get_prev(s->tree,
5656 s->selected_entry);
5658 } else {
5659 if (s->selected_entry)
5660 te = s->selected_entry;
5661 else if (view->searching == TOG_SEARCH_FORWARD)
5662 te = got_object_tree_get_first_entry(s->tree);
5663 else
5664 te = got_object_tree_get_last_entry(s->tree);
5667 while (1) {
5668 if (te == NULL) {
5669 if (s->matched_entry == NULL) {
5670 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5671 return NULL;
5673 if (view->searching == TOG_SEARCH_FORWARD)
5674 te = got_object_tree_get_first_entry(s->tree);
5675 else
5676 te = got_object_tree_get_last_entry(s->tree);
5679 if (match_tree_entry(te, &view->regex)) {
5680 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5681 s->matched_entry = te;
5682 break;
5685 if (view->searching == TOG_SEARCH_FORWARD)
5686 te = got_tree_entry_get_next(s->tree, te);
5687 else
5688 te = got_tree_entry_get_prev(s->tree, te);
5691 if (s->matched_entry) {
5692 s->first_displayed_entry = s->matched_entry;
5693 s->selected = 0;
5696 return NULL;
5699 static const struct got_error *
5700 show_tree_view(struct tog_view *view)
5702 const struct got_error *err = NULL;
5703 struct tog_tree_view_state *s = &view->state.tree;
5704 char *parent_path;
5706 err = tree_entry_path(&parent_path, &s->parents, NULL);
5707 if (err)
5708 return err;
5710 err = draw_tree_entries(view, parent_path);
5711 free(parent_path);
5713 view_vborder(view);
5714 return err;
5717 static const struct got_error *
5718 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5720 const struct got_error *err = NULL;
5721 struct tog_tree_view_state *s = &view->state.tree;
5722 struct tog_view *log_view, *ref_view;
5723 struct got_tree_entry *te;
5724 int begin_x = 0, n, nscroll = view->nlines - 3;
5726 switch (ch) {
5727 case 'i':
5728 s->show_ids = !s->show_ids;
5729 break;
5730 case 'l':
5731 if (!s->selected_entry)
5732 break;
5733 if (view_is_parent_view(view))
5734 begin_x = view_split_begin_x(view->begin_x);
5735 err = log_selected_tree_entry(&log_view, begin_x, s);
5736 view->focussed = 0;
5737 log_view->focussed = 1;
5738 if (view_is_parent_view(view)) {
5739 err = view_close_child(view);
5740 if (err)
5741 return err;
5742 view_set_child(view, log_view);
5743 view->focus_child = 1;
5744 } else
5745 *new_view = log_view;
5746 break;
5747 case 'r':
5748 if (view_is_parent_view(view))
5749 begin_x = view_split_begin_x(view->begin_x);
5750 ref_view = view_open(view->nlines, view->ncols,
5751 view->begin_y, begin_x, TOG_VIEW_REF);
5752 if (ref_view == NULL)
5753 return got_error_from_errno("view_open");
5754 err = open_ref_view(ref_view, s->repo);
5755 if (err) {
5756 view_close(ref_view);
5757 return err;
5759 view->focussed = 0;
5760 ref_view->focussed = 1;
5761 if (view_is_parent_view(view)) {
5762 err = view_close_child(view);
5763 if (err)
5764 return err;
5765 view_set_child(view, ref_view);
5766 view->focus_child = 1;
5767 } else
5768 *new_view = ref_view;
5769 break;
5770 case 'g':
5771 case KEY_HOME:
5772 s->selected = 0;
5773 if (s->tree == s->root)
5774 s->first_displayed_entry =
5775 got_object_tree_get_first_entry(s->tree);
5776 else
5777 s->first_displayed_entry = NULL;
5778 break;
5779 case 'G':
5780 case KEY_END:
5781 s->selected = 0;
5782 te = got_object_tree_get_last_entry(s->tree);
5783 for (n = 0; n < view->nlines - 3; n++) {
5784 if (te == NULL) {
5785 if(s->tree != s->root) {
5786 s->first_displayed_entry = NULL;
5787 n++;
5789 break;
5791 s->first_displayed_entry = te;
5792 te = got_tree_entry_get_prev(s->tree, te);
5794 if (n > 0)
5795 s->selected = n - 1;
5796 break;
5797 case 'k':
5798 case KEY_UP:
5799 case CTRL('p'):
5800 if (s->selected > 0) {
5801 s->selected--;
5802 break;
5804 tree_scroll_up(s, 1);
5805 break;
5806 case CTRL('u'):
5807 case 'u':
5808 nscroll /= 2;
5809 /* FALL THROUGH */
5810 case KEY_PPAGE:
5811 case CTRL('b'):
5812 if (s->tree == s->root) {
5813 if (got_object_tree_get_first_entry(s->tree) ==
5814 s->first_displayed_entry)
5815 s->selected -= MIN(s->selected, nscroll);
5816 } else {
5817 if (s->first_displayed_entry == NULL)
5818 s->selected -= MIN(s->selected, nscroll);
5820 tree_scroll_up(s, MAX(0, nscroll));
5821 break;
5822 case 'j':
5823 case KEY_DOWN:
5824 case CTRL('n'):
5825 if (s->selected < s->ndisplayed - 1) {
5826 s->selected++;
5827 break;
5829 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5830 == NULL)
5831 /* can't scroll any further */
5832 break;
5833 tree_scroll_down(s, 1);
5834 break;
5835 case CTRL('d'):
5836 case 'd':
5837 nscroll /= 2;
5838 /* FALL THROUGH */
5839 case KEY_NPAGE:
5840 case CTRL('f'):
5841 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5842 == NULL) {
5843 /* can't scroll any further; move cursor down */
5844 if (s->selected < s->ndisplayed - 1)
5845 s->selected += MIN(nscroll,
5846 s->ndisplayed - s->selected - 1);
5847 break;
5849 tree_scroll_down(s, nscroll);
5850 break;
5851 case KEY_ENTER:
5852 case '\r':
5853 case KEY_BACKSPACE:
5854 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5855 struct tog_parent_tree *parent;
5856 /* user selected '..' */
5857 if (s->tree == s->root)
5858 break;
5859 parent = TAILQ_FIRST(&s->parents);
5860 TAILQ_REMOVE(&s->parents, parent,
5861 entry);
5862 got_object_tree_close(s->tree);
5863 s->tree = parent->tree;
5864 s->first_displayed_entry =
5865 parent->first_displayed_entry;
5866 s->selected_entry =
5867 parent->selected_entry;
5868 s->selected = parent->selected;
5869 free(parent);
5870 } else if (S_ISDIR(got_tree_entry_get_mode(
5871 s->selected_entry))) {
5872 struct got_tree_object *subtree;
5873 err = got_object_open_as_tree(&subtree, s->repo,
5874 got_tree_entry_get_id(s->selected_entry));
5875 if (err)
5876 break;
5877 err = tree_view_visit_subtree(s, subtree);
5878 if (err) {
5879 got_object_tree_close(subtree);
5880 break;
5882 } else if (S_ISREG(got_tree_entry_get_mode(
5883 s->selected_entry))) {
5884 struct tog_view *blame_view;
5885 int begin_x = view_is_parent_view(view) ?
5886 view_split_begin_x(view->begin_x) : 0;
5888 err = blame_tree_entry(&blame_view, begin_x,
5889 s->selected_entry, &s->parents,
5890 s->commit_id, s->repo);
5891 if (err)
5892 break;
5893 view->focussed = 0;
5894 blame_view->focussed = 1;
5895 if (view_is_parent_view(view)) {
5896 err = view_close_child(view);
5897 if (err)
5898 return err;
5899 view_set_child(view, blame_view);
5900 view->focus_child = 1;
5901 } else
5902 *new_view = blame_view;
5904 break;
5905 case KEY_RESIZE:
5906 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5907 s->selected = view->nlines - 4;
5908 break;
5909 default:
5910 break;
5913 return err;
5916 __dead static void
5917 usage_tree(void)
5919 endwin();
5920 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5921 getprogname());
5922 exit(1);
5925 static const struct got_error *
5926 cmd_tree(int argc, char *argv[])
5928 const struct got_error *error;
5929 struct got_repository *repo = NULL;
5930 struct got_worktree *worktree = NULL;
5931 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5932 struct got_object_id *commit_id = NULL;
5933 struct got_commit_object *commit = NULL;
5934 const char *commit_id_arg = NULL;
5935 char *label = NULL;
5936 struct got_reference *ref = NULL;
5937 const char *head_ref_name = NULL;
5938 int ch;
5939 struct tog_view *view;
5940 int *pack_fds = NULL;
5942 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5943 switch (ch) {
5944 case 'c':
5945 commit_id_arg = optarg;
5946 break;
5947 case 'r':
5948 repo_path = realpath(optarg, NULL);
5949 if (repo_path == NULL)
5950 return got_error_from_errno2("realpath",
5951 optarg);
5952 break;
5953 default:
5954 usage_tree();
5955 /* NOTREACHED */
5959 argc -= optind;
5960 argv += optind;
5962 if (argc > 1)
5963 usage_tree();
5965 error = got_repo_pack_fds_open(&pack_fds);
5966 if (error != NULL)
5967 goto done;
5969 if (repo_path == NULL) {
5970 cwd = getcwd(NULL, 0);
5971 if (cwd == NULL)
5972 return got_error_from_errno("getcwd");
5973 error = got_worktree_open(&worktree, cwd);
5974 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5975 goto done;
5976 if (worktree)
5977 repo_path =
5978 strdup(got_worktree_get_repo_path(worktree));
5979 else
5980 repo_path = strdup(cwd);
5981 if (repo_path == NULL) {
5982 error = got_error_from_errno("strdup");
5983 goto done;
5987 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5988 if (error != NULL)
5989 goto done;
5991 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5992 repo, worktree);
5993 if (error)
5994 goto done;
5996 init_curses();
5998 error = apply_unveil(got_repo_get_path(repo), NULL);
5999 if (error)
6000 goto done;
6002 error = tog_load_refs(repo, 0);
6003 if (error)
6004 goto done;
6006 if (commit_id_arg == NULL) {
6007 error = got_repo_match_object_id(&commit_id, &label,
6008 worktree ? got_worktree_get_head_ref_name(worktree) :
6009 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6010 if (error)
6011 goto done;
6012 head_ref_name = label;
6013 } else {
6014 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6015 if (error == NULL)
6016 head_ref_name = got_ref_get_name(ref);
6017 else if (error->code != GOT_ERR_NOT_REF)
6018 goto done;
6019 error = got_repo_match_object_id(&commit_id, NULL,
6020 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6021 if (error)
6022 goto done;
6025 error = got_object_open_as_commit(&commit, repo, commit_id);
6026 if (error)
6027 goto done;
6029 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6030 if (view == NULL) {
6031 error = got_error_from_errno("view_open");
6032 goto done;
6034 error = open_tree_view(view, commit_id, head_ref_name, repo);
6035 if (error)
6036 goto done;
6037 if (!got_path_is_root_dir(in_repo_path)) {
6038 error = tree_view_walk_path(&view->state.tree, commit,
6039 in_repo_path);
6040 if (error)
6041 goto done;
6044 if (worktree) {
6045 /* Release work tree lock. */
6046 got_worktree_close(worktree);
6047 worktree = NULL;
6049 error = view_loop(view);
6050 done:
6051 free(repo_path);
6052 free(cwd);
6053 free(commit_id);
6054 free(label);
6055 if (ref)
6056 got_ref_close(ref);
6057 if (repo) {
6058 const struct got_error *close_err = got_repo_close(repo);
6059 if (error == NULL)
6060 error = close_err;
6062 if (pack_fds) {
6063 const struct got_error *pack_err =
6064 got_repo_pack_fds_close(pack_fds);
6065 if (error == NULL)
6066 error = pack_err;
6068 tog_free_refs();
6069 return error;
6072 static const struct got_error *
6073 ref_view_load_refs(struct tog_ref_view_state *s)
6075 struct got_reflist_entry *sre;
6076 struct tog_reflist_entry *re;
6078 s->nrefs = 0;
6079 TAILQ_FOREACH(sre, &tog_refs, entry) {
6080 if (strncmp(got_ref_get_name(sre->ref),
6081 "refs/got/", 9) == 0 &&
6082 strncmp(got_ref_get_name(sre->ref),
6083 "refs/got/backup/", 16) != 0)
6084 continue;
6086 re = malloc(sizeof(*re));
6087 if (re == NULL)
6088 return got_error_from_errno("malloc");
6090 re->ref = got_ref_dup(sre->ref);
6091 if (re->ref == NULL)
6092 return got_error_from_errno("got_ref_dup");
6093 re->idx = s->nrefs++;
6094 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6097 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6098 return NULL;
6101 void
6102 ref_view_free_refs(struct tog_ref_view_state *s)
6104 struct tog_reflist_entry *re;
6106 while (!TAILQ_EMPTY(&s->refs)) {
6107 re = TAILQ_FIRST(&s->refs);
6108 TAILQ_REMOVE(&s->refs, re, entry);
6109 got_ref_close(re->ref);
6110 free(re);
6114 static const struct got_error *
6115 open_ref_view(struct tog_view *view, struct got_repository *repo)
6117 const struct got_error *err = NULL;
6118 struct tog_ref_view_state *s = &view->state.ref;
6120 s->selected_entry = 0;
6121 s->repo = repo;
6123 TAILQ_INIT(&s->refs);
6124 STAILQ_INIT(&s->colors);
6126 err = ref_view_load_refs(s);
6127 if (err)
6128 return err;
6130 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6131 err = add_color(&s->colors, "^refs/heads/",
6132 TOG_COLOR_REFS_HEADS,
6133 get_color_value("TOG_COLOR_REFS_HEADS"));
6134 if (err)
6135 goto done;
6137 err = add_color(&s->colors, "^refs/tags/",
6138 TOG_COLOR_REFS_TAGS,
6139 get_color_value("TOG_COLOR_REFS_TAGS"));
6140 if (err)
6141 goto done;
6143 err = add_color(&s->colors, "^refs/remotes/",
6144 TOG_COLOR_REFS_REMOTES,
6145 get_color_value("TOG_COLOR_REFS_REMOTES"));
6146 if (err)
6147 goto done;
6149 err = add_color(&s->colors, "^refs/got/backup/",
6150 TOG_COLOR_REFS_BACKUP,
6151 get_color_value("TOG_COLOR_REFS_BACKUP"));
6152 if (err)
6153 goto done;
6156 view->show = show_ref_view;
6157 view->input = input_ref_view;
6158 view->close = close_ref_view;
6159 view->search_start = search_start_ref_view;
6160 view->search_next = search_next_ref_view;
6161 done:
6162 if (err)
6163 free_colors(&s->colors);
6164 return err;
6167 static const struct got_error *
6168 close_ref_view(struct tog_view *view)
6170 struct tog_ref_view_state *s = &view->state.ref;
6172 ref_view_free_refs(s);
6173 free_colors(&s->colors);
6175 return NULL;
6178 static const struct got_error *
6179 resolve_reflist_entry(struct got_object_id **commit_id,
6180 struct tog_reflist_entry *re, struct got_repository *repo)
6182 const struct got_error *err = NULL;
6183 struct got_object_id *obj_id;
6184 struct got_tag_object *tag = NULL;
6185 int obj_type;
6187 *commit_id = NULL;
6189 err = got_ref_resolve(&obj_id, repo, re->ref);
6190 if (err)
6191 return err;
6193 err = got_object_get_type(&obj_type, repo, obj_id);
6194 if (err)
6195 goto done;
6197 switch (obj_type) {
6198 case GOT_OBJ_TYPE_COMMIT:
6199 *commit_id = obj_id;
6200 break;
6201 case GOT_OBJ_TYPE_TAG:
6202 err = got_object_open_as_tag(&tag, repo, obj_id);
6203 if (err)
6204 goto done;
6205 free(obj_id);
6206 err = got_object_get_type(&obj_type, repo,
6207 got_object_tag_get_object_id(tag));
6208 if (err)
6209 goto done;
6210 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6211 err = got_error(GOT_ERR_OBJ_TYPE);
6212 goto done;
6214 *commit_id = got_object_id_dup(
6215 got_object_tag_get_object_id(tag));
6216 if (*commit_id == NULL) {
6217 err = got_error_from_errno("got_object_id_dup");
6218 goto done;
6220 break;
6221 default:
6222 err = got_error(GOT_ERR_OBJ_TYPE);
6223 break;
6226 done:
6227 if (tag)
6228 got_object_tag_close(tag);
6229 if (err) {
6230 free(*commit_id);
6231 *commit_id = NULL;
6233 return err;
6236 static const struct got_error *
6237 log_ref_entry(struct tog_view **new_view, int begin_x,
6238 struct tog_reflist_entry *re, struct got_repository *repo)
6240 struct tog_view *log_view;
6241 const struct got_error *err = NULL;
6242 struct got_object_id *commit_id = NULL;
6244 *new_view = NULL;
6246 err = resolve_reflist_entry(&commit_id, re, repo);
6247 if (err) {
6248 if (err->code != GOT_ERR_OBJ_TYPE)
6249 return err;
6250 else
6251 return NULL;
6254 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6255 if (log_view == NULL) {
6256 err = got_error_from_errno("view_open");
6257 goto done;
6260 err = open_log_view(log_view, commit_id, repo,
6261 got_ref_get_name(re->ref), "", 0);
6262 done:
6263 if (err)
6264 view_close(log_view);
6265 else
6266 *new_view = log_view;
6267 free(commit_id);
6268 return err;
6271 static void
6272 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6274 struct tog_reflist_entry *re;
6275 int i = 0;
6277 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6278 return;
6280 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6281 while (i++ < maxscroll) {
6282 if (re == NULL)
6283 break;
6284 s->first_displayed_entry = re;
6285 re = TAILQ_PREV(re, tog_reflist_head, entry);
6289 static void
6290 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6292 struct tog_reflist_entry *next, *last;
6293 int n = 0;
6295 if (s->first_displayed_entry)
6296 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6297 else
6298 next = TAILQ_FIRST(&s->refs);
6300 last = s->last_displayed_entry;
6301 while (next && last && n++ < maxscroll) {
6302 last = TAILQ_NEXT(last, entry);
6303 if (last) {
6304 s->first_displayed_entry = next;
6305 next = TAILQ_NEXT(next, entry);
6310 static const struct got_error *
6311 search_start_ref_view(struct tog_view *view)
6313 struct tog_ref_view_state *s = &view->state.ref;
6315 s->matched_entry = NULL;
6316 return NULL;
6319 static int
6320 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6322 regmatch_t regmatch;
6324 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6325 0) == 0;
6328 static const struct got_error *
6329 search_next_ref_view(struct tog_view *view)
6331 struct tog_ref_view_state *s = &view->state.ref;
6332 struct tog_reflist_entry *re = NULL;
6334 if (!view->searching) {
6335 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6336 return NULL;
6339 if (s->matched_entry) {
6340 if (view->searching == TOG_SEARCH_FORWARD) {
6341 if (s->selected_entry)
6342 re = TAILQ_NEXT(s->selected_entry, entry);
6343 else
6344 re = TAILQ_PREV(s->selected_entry,
6345 tog_reflist_head, entry);
6346 } else {
6347 if (s->selected_entry == NULL)
6348 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6349 else
6350 re = TAILQ_PREV(s->selected_entry,
6351 tog_reflist_head, entry);
6353 } else {
6354 if (s->selected_entry)
6355 re = s->selected_entry;
6356 else if (view->searching == TOG_SEARCH_FORWARD)
6357 re = TAILQ_FIRST(&s->refs);
6358 else
6359 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6362 while (1) {
6363 if (re == NULL) {
6364 if (s->matched_entry == NULL) {
6365 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6366 return NULL;
6368 if (view->searching == TOG_SEARCH_FORWARD)
6369 re = TAILQ_FIRST(&s->refs);
6370 else
6371 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6374 if (match_reflist_entry(re, &view->regex)) {
6375 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6376 s->matched_entry = re;
6377 break;
6380 if (view->searching == TOG_SEARCH_FORWARD)
6381 re = TAILQ_NEXT(re, entry);
6382 else
6383 re = TAILQ_PREV(re, tog_reflist_head, entry);
6386 if (s->matched_entry) {
6387 s->first_displayed_entry = s->matched_entry;
6388 s->selected = 0;
6391 return NULL;
6394 static const struct got_error *
6395 show_ref_view(struct tog_view *view)
6397 const struct got_error *err = NULL;
6398 struct tog_ref_view_state *s = &view->state.ref;
6399 struct tog_reflist_entry *re;
6400 char *line = NULL;
6401 wchar_t *wline;
6402 struct tog_color *tc;
6403 int width, n;
6404 int limit = view->nlines;
6406 werase(view->window);
6408 s->ndisplayed = 0;
6410 if (limit == 0)
6411 return NULL;
6413 re = s->first_displayed_entry;
6415 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6416 s->nrefs) == -1)
6417 return got_error_from_errno("asprintf");
6419 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6420 if (err) {
6421 free(line);
6422 return err;
6424 if (view_needs_focus_indication(view))
6425 wstandout(view->window);
6426 waddwstr(view->window, wline);
6427 if (view_needs_focus_indication(view))
6428 wstandend(view->window);
6429 free(wline);
6430 wline = NULL;
6431 free(line);
6432 line = NULL;
6433 if (width < view->ncols - 1)
6434 waddch(view->window, '\n');
6435 if (--limit <= 0)
6436 return NULL;
6438 n = 0;
6439 while (re && limit > 0) {
6440 char *line = NULL;
6441 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6443 if (s->show_date) {
6444 struct got_commit_object *ci;
6445 struct got_tag_object *tag;
6446 struct got_object_id *id;
6447 struct tm tm;
6448 time_t t;
6450 err = got_ref_resolve(&id, s->repo, re->ref);
6451 if (err)
6452 return err;
6453 err = got_object_open_as_tag(&tag, s->repo, id);
6454 if (err) {
6455 if (err->code != GOT_ERR_OBJ_TYPE) {
6456 free(id);
6457 return err;
6459 err = got_object_open_as_commit(&ci, s->repo,
6460 id);
6461 if (err) {
6462 free(id);
6463 return err;
6465 t = got_object_commit_get_committer_time(ci);
6466 got_object_commit_close(ci);
6467 } else {
6468 t = got_object_tag_get_tagger_time(tag);
6469 got_object_tag_close(tag);
6471 free(id);
6472 if (gmtime_r(&t, &tm) == NULL)
6473 return got_error_from_errno("gmtime_r");
6474 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6475 return got_error(GOT_ERR_NO_SPACE);
6477 if (got_ref_is_symbolic(re->ref)) {
6478 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6479 ymd : "", got_ref_get_name(re->ref),
6480 got_ref_get_symref_target(re->ref)) == -1)
6481 return got_error_from_errno("asprintf");
6482 } else if (s->show_ids) {
6483 struct got_object_id *id;
6484 char *id_str;
6485 err = got_ref_resolve(&id, s->repo, re->ref);
6486 if (err)
6487 return err;
6488 err = got_object_id_str(&id_str, id);
6489 if (err) {
6490 free(id);
6491 return err;
6493 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6494 got_ref_get_name(re->ref), id_str) == -1) {
6495 err = got_error_from_errno("asprintf");
6496 free(id);
6497 free(id_str);
6498 return err;
6500 free(id);
6501 free(id_str);
6502 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6503 got_ref_get_name(re->ref)) == -1)
6504 return got_error_from_errno("asprintf");
6506 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6507 if (err) {
6508 free(line);
6509 return err;
6511 if (n == s->selected) {
6512 if (view->focussed)
6513 wstandout(view->window);
6514 s->selected_entry = re;
6516 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6517 if (tc)
6518 wattr_on(view->window,
6519 COLOR_PAIR(tc->colorpair), NULL);
6520 waddwstr(view->window, wline);
6521 if (tc)
6522 wattr_off(view->window,
6523 COLOR_PAIR(tc->colorpair), NULL);
6524 if (width < view->ncols - 1)
6525 waddch(view->window, '\n');
6526 if (n == s->selected && view->focussed)
6527 wstandend(view->window);
6528 free(line);
6529 free(wline);
6530 wline = NULL;
6531 n++;
6532 s->ndisplayed++;
6533 s->last_displayed_entry = re;
6535 limit--;
6536 re = TAILQ_NEXT(re, entry);
6539 view_vborder(view);
6540 return err;
6543 static const struct got_error *
6544 browse_ref_tree(struct tog_view **new_view, int begin_x,
6545 struct tog_reflist_entry *re, struct got_repository *repo)
6547 const struct got_error *err = NULL;
6548 struct got_object_id *commit_id = NULL;
6549 struct tog_view *tree_view;
6551 *new_view = NULL;
6553 err = resolve_reflist_entry(&commit_id, re, repo);
6554 if (err) {
6555 if (err->code != GOT_ERR_OBJ_TYPE)
6556 return err;
6557 else
6558 return NULL;
6562 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6563 if (tree_view == NULL) {
6564 err = got_error_from_errno("view_open");
6565 goto done;
6568 err = open_tree_view(tree_view, commit_id,
6569 got_ref_get_name(re->ref), repo);
6570 if (err)
6571 goto done;
6573 *new_view = tree_view;
6574 done:
6575 free(commit_id);
6576 return err;
6578 static const struct got_error *
6579 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6581 const struct got_error *err = NULL;
6582 struct tog_ref_view_state *s = &view->state.ref;
6583 struct tog_view *log_view, *tree_view;
6584 struct tog_reflist_entry *re;
6585 int begin_x = 0, n, nscroll = view->nlines - 1;
6587 switch (ch) {
6588 case 'i':
6589 s->show_ids = !s->show_ids;
6590 break;
6591 case 'm':
6592 s->show_date = !s->show_date;
6593 break;
6594 case 'o':
6595 s->sort_by_date = !s->sort_by_date;
6596 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6597 got_ref_cmp_by_commit_timestamp_descending :
6598 tog_ref_cmp_by_name, s->repo);
6599 if (err)
6600 break;
6601 got_reflist_object_id_map_free(tog_refs_idmap);
6602 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6603 &tog_refs, s->repo);
6604 if (err)
6605 break;
6606 ref_view_free_refs(s);
6607 err = ref_view_load_refs(s);
6608 break;
6609 case KEY_ENTER:
6610 case '\r':
6611 if (!s->selected_entry)
6612 break;
6613 if (view_is_parent_view(view))
6614 begin_x = view_split_begin_x(view->begin_x);
6615 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6616 s->repo);
6617 view->focussed = 0;
6618 log_view->focussed = 1;
6619 if (view_is_parent_view(view)) {
6620 err = view_close_child(view);
6621 if (err)
6622 return err;
6623 view_set_child(view, log_view);
6624 view->focus_child = 1;
6625 } else
6626 *new_view = log_view;
6627 break;
6628 case 't':
6629 if (!s->selected_entry)
6630 break;
6631 if (view_is_parent_view(view))
6632 begin_x = view_split_begin_x(view->begin_x);
6633 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6634 s->repo);
6635 if (err || tree_view == NULL)
6636 break;
6637 view->focussed = 0;
6638 tree_view->focussed = 1;
6639 if (view_is_parent_view(view)) {
6640 err = view_close_child(view);
6641 if (err)
6642 return err;
6643 view_set_child(view, tree_view);
6644 view->focus_child = 1;
6645 } else
6646 *new_view = tree_view;
6647 break;
6648 case 'g':
6649 case KEY_HOME:
6650 s->selected = 0;
6651 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6652 break;
6653 case 'G':
6654 case KEY_END:
6655 s->selected = 0;
6656 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6657 for (n = 0; n < view->nlines - 1; n++) {
6658 if (re == NULL)
6659 break;
6660 s->first_displayed_entry = re;
6661 re = TAILQ_PREV(re, tog_reflist_head, entry);
6663 if (n > 0)
6664 s->selected = n - 1;
6665 break;
6666 case 'k':
6667 case KEY_UP:
6668 case CTRL('p'):
6669 if (s->selected > 0) {
6670 s->selected--;
6671 break;
6673 ref_scroll_up(s, 1);
6674 break;
6675 case CTRL('u'):
6676 case 'u':
6677 nscroll /= 2;
6678 /* FALL THROUGH */
6679 case KEY_PPAGE:
6680 case CTRL('b'):
6681 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6682 s->selected -= MIN(nscroll, s->selected);
6683 ref_scroll_up(s, MAX(0, nscroll));
6684 break;
6685 case 'j':
6686 case KEY_DOWN:
6687 case CTRL('n'):
6688 if (s->selected < s->ndisplayed - 1) {
6689 s->selected++;
6690 break;
6692 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6693 /* can't scroll any further */
6694 break;
6695 ref_scroll_down(s, 1);
6696 break;
6697 case CTRL('d'):
6698 case 'd':
6699 nscroll /= 2;
6700 /* FALL THROUGH */
6701 case KEY_NPAGE:
6702 case CTRL('f'):
6703 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6704 /* can't scroll any further; move cursor down */
6705 if (s->selected < s->ndisplayed - 1)
6706 s->selected += MIN(nscroll,
6707 s->ndisplayed - s->selected - 1);
6708 break;
6710 ref_scroll_down(s, nscroll);
6711 break;
6712 case CTRL('l'):
6713 tog_free_refs();
6714 err = tog_load_refs(s->repo, s->sort_by_date);
6715 if (err)
6716 break;
6717 ref_view_free_refs(s);
6718 err = ref_view_load_refs(s);
6719 break;
6720 case KEY_RESIZE:
6721 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6722 s->selected = view->nlines - 2;
6723 break;
6724 default:
6725 break;
6728 return err;
6731 __dead static void
6732 usage_ref(void)
6734 endwin();
6735 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6736 getprogname());
6737 exit(1);
6740 static const struct got_error *
6741 cmd_ref(int argc, char *argv[])
6743 const struct got_error *error;
6744 struct got_repository *repo = NULL;
6745 struct got_worktree *worktree = NULL;
6746 char *cwd = NULL, *repo_path = NULL;
6747 int ch;
6748 struct tog_view *view;
6749 int *pack_fds = NULL;
6751 while ((ch = getopt(argc, argv, "r:")) != -1) {
6752 switch (ch) {
6753 case 'r':
6754 repo_path = realpath(optarg, NULL);
6755 if (repo_path == NULL)
6756 return got_error_from_errno2("realpath",
6757 optarg);
6758 break;
6759 default:
6760 usage_ref();
6761 /* NOTREACHED */
6765 argc -= optind;
6766 argv += optind;
6768 if (argc > 1)
6769 usage_ref();
6771 error = got_repo_pack_fds_open(&pack_fds);
6772 if (error != NULL)
6773 goto done;
6775 if (repo_path == NULL) {
6776 cwd = getcwd(NULL, 0);
6777 if (cwd == NULL)
6778 return got_error_from_errno("getcwd");
6779 error = got_worktree_open(&worktree, cwd);
6780 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6781 goto done;
6782 if (worktree)
6783 repo_path =
6784 strdup(got_worktree_get_repo_path(worktree));
6785 else
6786 repo_path = strdup(cwd);
6787 if (repo_path == NULL) {
6788 error = got_error_from_errno("strdup");
6789 goto done;
6793 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6794 if (error != NULL)
6795 goto done;
6797 init_curses();
6799 error = apply_unveil(got_repo_get_path(repo), NULL);
6800 if (error)
6801 goto done;
6803 error = tog_load_refs(repo, 0);
6804 if (error)
6805 goto done;
6807 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6808 if (view == NULL) {
6809 error = got_error_from_errno("view_open");
6810 goto done;
6813 error = open_ref_view(view, repo);
6814 if (error)
6815 goto done;
6817 if (worktree) {
6818 /* Release work tree lock. */
6819 got_worktree_close(worktree);
6820 worktree = NULL;
6822 error = view_loop(view);
6823 done:
6824 free(repo_path);
6825 free(cwd);
6826 if (repo) {
6827 const struct got_error *close_err = got_repo_close(repo);
6828 if (close_err)
6829 error = close_err;
6831 if (pack_fds) {
6832 const struct got_error *pack_err =
6833 got_repo_pack_fds_close(pack_fds);
6834 if (error == NULL)
6835 error = pack_err;
6837 tog_free_refs();
6838 return error;
6841 static void
6842 list_commands(FILE *fp)
6844 size_t i;
6846 fprintf(fp, "commands:");
6847 for (i = 0; i < nitems(tog_commands); i++) {
6848 const struct tog_cmd *cmd = &tog_commands[i];
6849 fprintf(fp, " %s", cmd->name);
6851 fputc('\n', fp);
6854 __dead static void
6855 usage(int hflag, int status)
6857 FILE *fp = (status == 0) ? stdout : stderr;
6859 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6860 getprogname());
6861 if (hflag) {
6862 fprintf(fp, "lazy usage: %s path\n", getprogname());
6863 list_commands(fp);
6865 exit(status);
6868 static char **
6869 make_argv(int argc, ...)
6871 va_list ap;
6872 char **argv;
6873 int i;
6875 va_start(ap, argc);
6877 argv = calloc(argc, sizeof(char *));
6878 if (argv == NULL)
6879 err(1, "calloc");
6880 for (i = 0; i < argc; i++) {
6881 argv[i] = strdup(va_arg(ap, char *));
6882 if (argv[i] == NULL)
6883 err(1, "strdup");
6886 va_end(ap);
6887 return argv;
6891 * Try to convert 'tog path' into a 'tog log path' command.
6892 * The user could simply have mistyped the command rather than knowingly
6893 * provided a path. So check whether argv[0] can in fact be resolved
6894 * to a path in the HEAD commit and print a special error if not.
6895 * This hack is for mpi@ <3
6897 static const struct got_error *
6898 tog_log_with_path(int argc, char *argv[])
6900 const struct got_error *error = NULL, *close_err;
6901 const struct tog_cmd *cmd = NULL;
6902 struct got_repository *repo = NULL;
6903 struct got_worktree *worktree = NULL;
6904 struct got_object_id *commit_id = NULL, *id = NULL;
6905 struct got_commit_object *commit = NULL;
6906 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6907 char *commit_id_str = NULL, **cmd_argv = NULL;
6908 int *pack_fds = NULL;
6910 cwd = getcwd(NULL, 0);
6911 if (cwd == NULL)
6912 return got_error_from_errno("getcwd");
6914 error = got_repo_pack_fds_open(&pack_fds);
6915 if (error != NULL)
6916 goto done;
6918 error = got_worktree_open(&worktree, cwd);
6919 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6920 goto done;
6922 if (worktree)
6923 repo_path = strdup(got_worktree_get_repo_path(worktree));
6924 else
6925 repo_path = strdup(cwd);
6926 if (repo_path == NULL) {
6927 error = got_error_from_errno("strdup");
6928 goto done;
6931 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6932 if (error != NULL)
6933 goto done;
6935 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6936 repo, worktree);
6937 if (error)
6938 goto done;
6940 error = tog_load_refs(repo, 0);
6941 if (error)
6942 goto done;
6943 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6944 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6945 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6946 if (error)
6947 goto done;
6949 if (worktree) {
6950 got_worktree_close(worktree);
6951 worktree = NULL;
6954 error = got_object_open_as_commit(&commit, repo, commit_id);
6955 if (error)
6956 goto done;
6958 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6959 if (error) {
6960 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6961 goto done;
6962 fprintf(stderr, "%s: '%s' is no known command or path\n",
6963 getprogname(), argv[0]);
6964 usage(1, 1);
6965 /* not reached */
6968 close_err = got_repo_close(repo);
6969 if (error == NULL)
6970 error = close_err;
6971 repo = NULL;
6973 error = got_object_id_str(&commit_id_str, commit_id);
6974 if (error)
6975 goto done;
6977 cmd = &tog_commands[0]; /* log */
6978 argc = 4;
6979 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6980 error = cmd->cmd_main(argc, cmd_argv);
6981 done:
6982 if (repo) {
6983 close_err = got_repo_close(repo);
6984 if (error == NULL)
6985 error = close_err;
6987 if (commit)
6988 got_object_commit_close(commit);
6989 if (worktree)
6990 got_worktree_close(worktree);
6991 if (pack_fds) {
6992 const struct got_error *pack_err =
6993 got_repo_pack_fds_close(pack_fds);
6994 if (error == NULL)
6995 error = pack_err;
6997 free(id);
6998 free(commit_id_str);
6999 free(commit_id);
7000 free(cwd);
7001 free(repo_path);
7002 free(in_repo_path);
7003 if (cmd_argv) {
7004 int i;
7005 for (i = 0; i < argc; i++)
7006 free(cmd_argv[i]);
7007 free(cmd_argv);
7009 tog_free_refs();
7010 return error;
7013 int
7014 main(int argc, char *argv[])
7016 const struct got_error *error = NULL;
7017 const struct tog_cmd *cmd = NULL;
7018 int ch, hflag = 0, Vflag = 0;
7019 char **cmd_argv = NULL;
7020 static const struct option longopts[] = {
7021 { "version", no_argument, NULL, 'V' },
7022 { NULL, 0, NULL, 0}
7025 setlocale(LC_CTYPE, "");
7027 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7028 switch (ch) {
7029 case 'h':
7030 hflag = 1;
7031 break;
7032 case 'V':
7033 Vflag = 1;
7034 break;
7035 default:
7036 usage(hflag, 1);
7037 /* NOTREACHED */
7041 argc -= optind;
7042 argv += optind;
7043 optind = 1;
7044 optreset = 1;
7046 if (Vflag) {
7047 got_version_print_str();
7048 return 0;
7051 #ifndef PROFILE
7052 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7053 NULL) == -1)
7054 err(1, "pledge");
7055 #endif
7057 if (argc == 0) {
7058 if (hflag)
7059 usage(hflag, 0);
7060 /* Build an argument vector which runs a default command. */
7061 cmd = &tog_commands[0];
7062 argc = 1;
7063 cmd_argv = make_argv(argc, cmd->name);
7064 } else {
7065 size_t i;
7067 /* Did the user specify a command? */
7068 for (i = 0; i < nitems(tog_commands); i++) {
7069 if (strncmp(tog_commands[i].name, argv[0],
7070 strlen(argv[0])) == 0) {
7071 cmd = &tog_commands[i];
7072 break;
7077 if (cmd == NULL) {
7078 if (argc != 1)
7079 usage(0, 1);
7080 /* No command specified; try log with a path */
7081 error = tog_log_with_path(argc, argv);
7082 } else {
7083 if (hflag)
7084 cmd->cmd_usage();
7085 else
7086 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7089 endwin();
7090 putchar('\n');
7091 if (cmd_argv) {
7092 int i;
7093 for (i = 0; i < argc; i++)
7094 free(cmd_argv[i]);
7095 free(cmd_argv);
7098 if (error && error->code != GOT_ERR_CANCELLED)
7099 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7100 return 0;