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_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 lines, cols; /* copies of LINES and COLS */
504 int focussed; /* Only set on one parent or child view at a time. */
505 int dying;
506 struct tog_view *parent;
507 struct tog_view *child;
509 /*
510 * This flag is initially set on parent views when a new child view
511 * is created. It gets toggled when the 'Tab' key switches focus
512 * between parent and child.
513 * The flag indicates whether focus should be passed on to our child
514 * view if this parent view gets picked for focus after another parent
515 * view was closed. This prevents child views from losing focus in such
516 * situations.
517 */
518 int focus_child;
520 /* type-specific state */
521 enum tog_view_type type;
522 union {
523 struct tog_diff_view_state diff;
524 struct tog_log_view_state log;
525 struct tog_blame_view_state blame;
526 struct tog_tree_view_state tree;
527 struct tog_ref_view_state ref;
528 } state;
530 const struct got_error *(*show)(struct tog_view *);
531 const struct got_error *(*input)(struct tog_view **,
532 struct tog_view *, int);
533 const struct got_error *(*close)(struct tog_view *);
535 const struct got_error *(*search_start)(struct tog_view *);
536 const struct got_error *(*search_next)(struct tog_view *);
537 int search_started;
538 int searching;
539 #define TOG_SEARCH_FORWARD 1
540 #define TOG_SEARCH_BACKWARD 2
541 int search_next_done;
542 #define TOG_SEARCH_HAVE_MORE 1
543 #define TOG_SEARCH_NO_MORE 2
544 #define TOG_SEARCH_HAVE_NONE 3
545 regex_t regex;
546 regmatch_t regmatch;
547 };
549 static const struct got_error *open_diff_view(struct tog_view *,
550 struct got_object_id *, struct got_object_id *,
551 const char *, const char *, int, int, int, struct tog_view *,
552 struct got_repository *);
553 static const struct got_error *show_diff_view(struct tog_view *);
554 static const struct got_error *input_diff_view(struct tog_view **,
555 struct tog_view *, int);
556 static const struct got_error* close_diff_view(struct tog_view *);
557 static const struct got_error *search_start_diff_view(struct tog_view *);
558 static const struct got_error *search_next_diff_view(struct tog_view *);
560 static const struct got_error *open_log_view(struct tog_view *,
561 struct got_object_id *, struct got_repository *,
562 const char *, const char *, int);
563 static const struct got_error * show_log_view(struct tog_view *);
564 static const struct got_error *input_log_view(struct tog_view **,
565 struct tog_view *, int);
566 static const struct got_error *close_log_view(struct tog_view *);
567 static const struct got_error *search_start_log_view(struct tog_view *);
568 static const struct got_error *search_next_log_view(struct tog_view *);
570 static const struct got_error *open_blame_view(struct tog_view *, char *,
571 struct got_object_id *, struct got_repository *);
572 static const struct got_error *show_blame_view(struct tog_view *);
573 static const struct got_error *input_blame_view(struct tog_view **,
574 struct tog_view *, int);
575 static const struct got_error *close_blame_view(struct tog_view *);
576 static const struct got_error *search_start_blame_view(struct tog_view *);
577 static const struct got_error *search_next_blame_view(struct tog_view *);
579 static const struct got_error *open_tree_view(struct tog_view *,
580 struct got_object_id *, const char *, struct got_repository *);
581 static const struct got_error *show_tree_view(struct tog_view *);
582 static const struct got_error *input_tree_view(struct tog_view **,
583 struct tog_view *, int);
584 static const struct got_error *close_tree_view(struct tog_view *);
585 static const struct got_error *search_start_tree_view(struct tog_view *);
586 static const struct got_error *search_next_tree_view(struct tog_view *);
588 static const struct got_error *open_ref_view(struct tog_view *,
589 struct got_repository *);
590 static const struct got_error *show_ref_view(struct tog_view *);
591 static const struct got_error *input_ref_view(struct tog_view **,
592 struct tog_view *, int);
593 static const struct got_error *close_ref_view(struct tog_view *);
594 static const struct got_error *search_start_ref_view(struct tog_view *);
595 static const struct got_error *search_next_ref_view(struct tog_view *);
597 static volatile sig_atomic_t tog_sigwinch_received;
598 static volatile sig_atomic_t tog_sigpipe_received;
599 static volatile sig_atomic_t tog_sigcont_received;
600 static volatile sig_atomic_t tog_sigint_received;
601 static volatile sig_atomic_t tog_sigterm_received;
603 static void
604 tog_sigwinch(int signo)
606 tog_sigwinch_received = 1;
609 static void
610 tog_sigpipe(int signo)
612 tog_sigpipe_received = 1;
615 static void
616 tog_sigcont(int signo)
618 tog_sigcont_received = 1;
621 static void
622 tog_sigint(int signo)
624 tog_sigint_received = 1;
627 static void
628 tog_sigterm(int signo)
630 tog_sigterm_received = 1;
633 static int
634 tog_fatal_signal_received()
636 return (tog_sigpipe_received ||
637 tog_sigint_received || tog_sigint_received);
641 static const struct got_error *
642 view_close(struct tog_view *view)
644 const struct got_error *err = NULL;
646 if (view->child) {
647 view_close(view->child);
648 view->child = NULL;
650 if (view->close)
651 err = view->close(view);
652 if (view->panel)
653 del_panel(view->panel);
654 if (view->window)
655 delwin(view->window);
656 free(view);
657 return err;
660 static struct tog_view *
661 view_open(int nlines, int ncols, int begin_y, int begin_x,
662 enum tog_view_type type)
664 struct tog_view *view = calloc(1, sizeof(*view));
666 if (view == NULL)
667 return NULL;
669 view->type = type;
670 view->lines = LINES;
671 view->cols = COLS;
672 view->nlines = nlines ? nlines : LINES - begin_y;
673 view->ncols = ncols ? ncols : COLS - begin_x;
674 view->begin_y = begin_y;
675 view->begin_x = begin_x;
676 view->window = newwin(nlines, ncols, begin_y, begin_x);
677 if (view->window == NULL) {
678 view_close(view);
679 return NULL;
681 view->panel = new_panel(view->window);
682 if (view->panel == NULL ||
683 set_panel_userptr(view->panel, view) != OK) {
684 view_close(view);
685 return NULL;
688 keypad(view->window, TRUE);
689 return view;
692 static int
693 view_split_begin_x(int begin_x)
695 if (begin_x > 0 || COLS < 120)
696 return 0;
697 return (COLS - MAX(COLS / 2, 80));
700 static const struct got_error *view_resize(struct tog_view *);
702 static const struct got_error *
703 view_splitscreen(struct tog_view *view)
705 const struct got_error *err = NULL;
707 view->begin_y = 0;
708 view->begin_x = view_split_begin_x(0);
709 view->nlines = LINES;
710 view->ncols = COLS - view->begin_x;
711 view->lines = LINES;
712 view->cols = COLS;
713 err = view_resize(view);
714 if (err)
715 return err;
717 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
718 return got_error_from_errno("mvwin");
720 return NULL;
723 static const struct got_error *
724 view_fullscreen(struct tog_view *view)
726 const struct got_error *err = NULL;
728 view->begin_x = 0;
729 view->begin_y = 0;
730 view->nlines = LINES;
731 view->ncols = COLS;
732 view->lines = LINES;
733 view->cols = COLS;
734 err = view_resize(view);
735 if (err)
736 return err;
738 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
739 return got_error_from_errno("mvwin");
741 return NULL;
744 static int
745 view_is_parent_view(struct tog_view *view)
747 return view->parent == NULL;
750 static const struct got_error *
751 view_resize(struct tog_view *view)
753 int nlines, ncols;
755 if (view->lines > LINES)
756 nlines = view->nlines - (view->lines - LINES);
757 else
758 nlines = view->nlines + (LINES - view->lines);
760 if (view->cols > COLS)
761 ncols = view->ncols - (view->cols - COLS);
762 else
763 ncols = view->ncols + (COLS - view->cols);
765 if (wresize(view->window, nlines, ncols) == ERR)
766 return got_error_from_errno("wresize");
767 if (replace_panel(view->panel, view->window) == ERR)
768 return got_error_from_errno("replace_panel");
769 wclear(view->window);
771 view->nlines = nlines;
772 view->ncols = ncols;
773 view->lines = LINES;
774 view->cols = COLS;
776 if (view->child) {
777 view->child->begin_x = view_split_begin_x(view->begin_x);
778 if (view->child->begin_x == 0) {
779 view_fullscreen(view->child);
780 if (view->child->focussed)
781 show_panel(view->child->panel);
782 else
783 show_panel(view->panel);
784 } else {
785 view_splitscreen(view->child);
786 show_panel(view->child->panel);
790 return NULL;
793 static const struct got_error *
794 view_close_child(struct tog_view *view)
796 const struct got_error *err = NULL;
798 if (view->child == NULL)
799 return NULL;
801 err = view_close(view->child);
802 view->child = NULL;
803 return err;
806 static void
807 view_set_child(struct tog_view *view, struct tog_view *child)
809 view->child = child;
810 child->parent = view;
813 static int
814 view_is_splitscreen(struct tog_view *view)
816 return view->begin_x > 0;
819 static void
820 tog_resizeterm(void)
822 int cols, lines;
823 struct winsize size;
825 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
826 cols = 80; /* Default */
827 lines = 24;
828 } else {
829 cols = size.ws_col;
830 lines = size.ws_row;
832 resize_term(lines, cols);
835 static const struct got_error *
836 view_search_start(struct tog_view *view)
838 const struct got_error *err = NULL;
839 char pattern[1024];
840 int ret;
842 if (view->search_started) {
843 regfree(&view->regex);
844 view->searching = 0;
845 memset(&view->regmatch, 0, sizeof(view->regmatch));
847 view->search_started = 0;
849 if (view->nlines < 1)
850 return NULL;
852 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
853 wclrtoeol(view->window);
855 nocbreak();
856 echo();
857 ret = wgetnstr(view->window, pattern, sizeof(pattern));
858 cbreak();
859 noecho();
860 if (ret == ERR)
861 return NULL;
863 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
864 err = view->search_start(view);
865 if (err) {
866 regfree(&view->regex);
867 return err;
869 view->search_started = 1;
870 view->searching = TOG_SEARCH_FORWARD;
871 view->search_next_done = 0;
872 view->search_next(view);
875 return NULL;
878 static const struct got_error *
879 view_input(struct tog_view **new, int *done, struct tog_view *view,
880 struct tog_view_list_head *views)
882 const struct got_error *err = NULL;
883 struct tog_view *v;
884 int ch, errcode;
886 *new = NULL;
888 /* Clear "no matches" indicator. */
889 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
890 view->search_next_done == TOG_SEARCH_HAVE_NONE)
891 view->search_next_done = TOG_SEARCH_HAVE_MORE;
893 if (view->searching && !view->search_next_done) {
894 errcode = pthread_mutex_unlock(&tog_mutex);
895 if (errcode)
896 return got_error_set_errno(errcode,
897 "pthread_mutex_unlock");
898 sched_yield();
899 errcode = pthread_mutex_lock(&tog_mutex);
900 if (errcode)
901 return got_error_set_errno(errcode,
902 "pthread_mutex_lock");
903 view->search_next(view);
904 return NULL;
907 nodelay(stdscr, FALSE);
908 /* Allow threads to make progress while we are waiting for input. */
909 errcode = pthread_mutex_unlock(&tog_mutex);
910 if (errcode)
911 return got_error_set_errno(errcode, "pthread_mutex_unlock");
912 ch = wgetch(view->window);
913 errcode = pthread_mutex_lock(&tog_mutex);
914 if (errcode)
915 return got_error_set_errno(errcode, "pthread_mutex_lock");
916 nodelay(stdscr, TRUE);
918 if (tog_sigwinch_received || tog_sigcont_received) {
919 tog_resizeterm();
920 tog_sigwinch_received = 0;
921 tog_sigcont_received = 0;
922 TAILQ_FOREACH(v, views, entry) {
923 err = view_resize(v);
924 if (err)
925 return err;
926 err = v->input(new, v, KEY_RESIZE);
927 if (err)
928 return err;
929 if (v->child) {
930 err = view_resize(v->child);
931 if (err)
932 return err;
933 err = v->child->input(new, v->child,
934 KEY_RESIZE);
935 if (err)
936 return err;
941 switch (ch) {
942 case '\t':
943 if (view->child) {
944 view->focussed = 0;
945 view->child->focussed = 1;
946 view->focus_child = 1;
947 } else if (view->parent) {
948 view->focussed = 0;
949 view->parent->focussed = 1;
950 view->parent->focus_child = 0;
952 break;
953 case 'q':
954 err = view->input(new, view, ch);
955 view->dying = 1;
956 break;
957 case 'Q':
958 *done = 1;
959 break;
960 case 'f':
961 if (view_is_parent_view(view)) {
962 if (view->child == NULL)
963 break;
964 if (view_is_splitscreen(view->child)) {
965 view->focussed = 0;
966 view->child->focussed = 1;
967 err = view_fullscreen(view->child);
968 } else
969 err = view_splitscreen(view->child);
970 if (err)
971 break;
972 err = view->child->input(new, view->child,
973 KEY_RESIZE);
974 } else {
975 if (view_is_splitscreen(view)) {
976 view->parent->focussed = 0;
977 view->focussed = 1;
978 err = view_fullscreen(view);
979 } else {
980 err = view_splitscreen(view);
982 if (err)
983 break;
984 err = view->input(new, view, KEY_RESIZE);
986 break;
987 case KEY_RESIZE:
988 break;
989 case '/':
990 if (view->search_start)
991 view_search_start(view);
992 else
993 err = view->input(new, view, ch);
994 break;
995 case 'N':
996 case 'n':
997 if (view->search_started && view->search_next) {
998 view->searching = (ch == 'n' ?
999 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1000 view->search_next_done = 0;
1001 view->search_next(view);
1002 } else
1003 err = view->input(new, view, ch);
1004 break;
1005 default:
1006 err = view->input(new, view, ch);
1007 break;
1010 return err;
1013 void
1014 view_vborder(struct tog_view *view)
1016 PANEL *panel;
1017 const struct tog_view *view_above;
1019 if (view->parent)
1020 return view_vborder(view->parent);
1022 panel = panel_above(view->panel);
1023 if (panel == NULL)
1024 return;
1026 view_above = panel_userptr(panel);
1027 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1028 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1031 int
1032 view_needs_focus_indication(struct tog_view *view)
1034 if (view_is_parent_view(view)) {
1035 if (view->child == NULL || view->child->focussed)
1036 return 0;
1037 if (!view_is_splitscreen(view->child))
1038 return 0;
1039 } else if (!view_is_splitscreen(view))
1040 return 0;
1042 return view->focussed;
1045 static const struct got_error *
1046 view_loop(struct tog_view *view)
1048 const struct got_error *err = NULL;
1049 struct tog_view_list_head views;
1050 struct tog_view *new_view;
1051 int fast_refresh = 10;
1052 int done = 0, errcode;
1054 errcode = pthread_mutex_lock(&tog_mutex);
1055 if (errcode)
1056 return got_error_set_errno(errcode, "pthread_mutex_lock");
1058 TAILQ_INIT(&views);
1059 TAILQ_INSERT_HEAD(&views, view, entry);
1061 view->focussed = 1;
1062 err = view->show(view);
1063 if (err)
1064 return err;
1065 update_panels();
1066 doupdate();
1067 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1068 /* Refresh fast during initialization, then become slower. */
1069 if (fast_refresh && fast_refresh-- == 0)
1070 halfdelay(10); /* switch to once per second */
1072 err = view_input(&new_view, &done, view, &views);
1073 if (err)
1074 break;
1075 if (view->dying) {
1076 struct tog_view *v, *prev = NULL;
1078 if (view_is_parent_view(view))
1079 prev = TAILQ_PREV(view, tog_view_list_head,
1080 entry);
1081 else if (view->parent)
1082 prev = view->parent;
1084 if (view->parent) {
1085 view->parent->child = NULL;
1086 view->parent->focus_child = 0;
1087 } else
1088 TAILQ_REMOVE(&views, view, entry);
1090 err = view_close(view);
1091 if (err)
1092 goto done;
1094 view = NULL;
1095 TAILQ_FOREACH(v, &views, entry) {
1096 if (v->focussed)
1097 break;
1099 if (view == NULL && new_view == NULL) {
1100 /* No view has focus. Try to pick one. */
1101 if (prev)
1102 view = prev;
1103 else if (!TAILQ_EMPTY(&views)) {
1104 view = TAILQ_LAST(&views,
1105 tog_view_list_head);
1107 if (view) {
1108 if (view->focus_child) {
1109 view->child->focussed = 1;
1110 view = view->child;
1111 } else
1112 view->focussed = 1;
1116 if (new_view) {
1117 struct tog_view *v, *t;
1118 /* Only allow one parent view per type. */
1119 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1120 if (v->type != new_view->type)
1121 continue;
1122 TAILQ_REMOVE(&views, v, entry);
1123 err = view_close(v);
1124 if (err)
1125 goto done;
1126 break;
1128 TAILQ_INSERT_TAIL(&views, new_view, entry);
1129 view = new_view;
1131 if (view) {
1132 if (view_is_parent_view(view)) {
1133 if (view->child && view->child->focussed)
1134 view = view->child;
1135 } else {
1136 if (view->parent && view->parent->focussed)
1137 view = view->parent;
1139 show_panel(view->panel);
1140 if (view->child && view_is_splitscreen(view->child))
1141 show_panel(view->child->panel);
1142 if (view->parent && view_is_splitscreen(view)) {
1143 err = view->parent->show(view->parent);
1144 if (err)
1145 goto done;
1147 err = view->show(view);
1148 if (err)
1149 goto done;
1150 if (view->child) {
1151 err = view->child->show(view->child);
1152 if (err)
1153 goto done;
1155 update_panels();
1156 doupdate();
1159 done:
1160 while (!TAILQ_EMPTY(&views)) {
1161 view = TAILQ_FIRST(&views);
1162 TAILQ_REMOVE(&views, view, entry);
1163 view_close(view);
1166 errcode = pthread_mutex_unlock(&tog_mutex);
1167 if (errcode && err == NULL)
1168 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1170 return err;
1173 __dead static void
1174 usage_log(void)
1176 endwin();
1177 fprintf(stderr,
1178 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1179 getprogname());
1180 exit(1);
1183 /* Create newly allocated wide-character string equivalent to a byte string. */
1184 static const struct got_error *
1185 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1187 char *vis = NULL;
1188 const struct got_error *err = NULL;
1190 *ws = NULL;
1191 *wlen = mbstowcs(NULL, s, 0);
1192 if (*wlen == (size_t)-1) {
1193 int vislen;
1194 if (errno != EILSEQ)
1195 return got_error_from_errno("mbstowcs");
1197 /* byte string invalid in current encoding; try to "fix" it */
1198 err = got_mbsavis(&vis, &vislen, s);
1199 if (err)
1200 return err;
1201 *wlen = mbstowcs(NULL, vis, 0);
1202 if (*wlen == (size_t)-1) {
1203 err = got_error_from_errno("mbstowcs"); /* give up */
1204 goto done;
1208 *ws = calloc(*wlen + 1, sizeof(**ws));
1209 if (*ws == NULL) {
1210 err = got_error_from_errno("calloc");
1211 goto done;
1214 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1215 err = got_error_from_errno("mbstowcs");
1216 done:
1217 free(vis);
1218 if (err) {
1219 free(*ws);
1220 *ws = NULL;
1221 *wlen = 0;
1223 return err;
1226 /* Format a line for display, ensuring that it won't overflow a width limit. */
1227 static const struct got_error *
1228 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1229 int col_tab_align)
1231 const struct got_error *err = NULL;
1232 int cols = 0;
1233 wchar_t *wline = NULL;
1234 size_t wlen;
1235 int i;
1237 *wlinep = NULL;
1238 *widthp = 0;
1240 err = mbs2ws(&wline, &wlen, line);
1241 if (err)
1242 return err;
1244 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1245 wline[wlen - 1] = L'\0';
1246 wlen--;
1248 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1249 wline[wlen - 1] = L'\0';
1250 wlen--;
1253 i = 0;
1254 while (i < wlen) {
1255 int width = wcwidth(wline[i]);
1257 if (width == 0) {
1258 i++;
1259 continue;
1262 if (width == 1 || width == 2) {
1263 if (cols + width > wlimit)
1264 break;
1265 cols += width;
1266 i++;
1267 } else if (width == -1) {
1268 if (wline[i] == L'\t') {
1269 width = TABSIZE -
1270 ((cols + col_tab_align) % TABSIZE);
1271 } else {
1272 width = 1;
1273 wline[i] = L'.';
1275 if (cols + width > wlimit)
1276 break;
1277 cols += width;
1278 i++;
1279 } else {
1280 err = got_error_from_errno("wcwidth");
1281 goto done;
1284 wline[i] = L'\0';
1285 if (widthp)
1286 *widthp = cols;
1287 done:
1288 if (err)
1289 free(wline);
1290 else
1291 *wlinep = wline;
1292 return err;
1295 static const struct got_error*
1296 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1297 struct got_object_id *id, struct got_repository *repo)
1299 static const struct got_error *err = NULL;
1300 struct got_reflist_entry *re;
1301 char *s;
1302 const char *name;
1304 *refs_str = NULL;
1306 TAILQ_FOREACH(re, refs, entry) {
1307 struct got_tag_object *tag = NULL;
1308 struct got_object_id *ref_id;
1309 int cmp;
1311 name = got_ref_get_name(re->ref);
1312 if (strcmp(name, GOT_REF_HEAD) == 0)
1313 continue;
1314 if (strncmp(name, "refs/", 5) == 0)
1315 name += 5;
1316 if (strncmp(name, "got/", 4) == 0 &&
1317 strncmp(name, "got/backup/", 11) != 0)
1318 continue;
1319 if (strncmp(name, "heads/", 6) == 0)
1320 name += 6;
1321 if (strncmp(name, "remotes/", 8) == 0) {
1322 name += 8;
1323 s = strstr(name, "/" GOT_REF_HEAD);
1324 if (s != NULL && s[strlen(s)] == '\0')
1325 continue;
1327 err = got_ref_resolve(&ref_id, repo, re->ref);
1328 if (err)
1329 break;
1330 if (strncmp(name, "tags/", 5) == 0) {
1331 err = got_object_open_as_tag(&tag, repo, ref_id);
1332 if (err) {
1333 if (err->code != GOT_ERR_OBJ_TYPE) {
1334 free(ref_id);
1335 break;
1337 /* Ref points at something other than a tag. */
1338 err = NULL;
1339 tag = NULL;
1342 cmp = got_object_id_cmp(tag ?
1343 got_object_tag_get_object_id(tag) : ref_id, id);
1344 free(ref_id);
1345 if (tag)
1346 got_object_tag_close(tag);
1347 if (cmp != 0)
1348 continue;
1349 s = *refs_str;
1350 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1351 s ? ", " : "", name) == -1) {
1352 err = got_error_from_errno("asprintf");
1353 free(s);
1354 *refs_str = NULL;
1355 break;
1357 free(s);
1360 return err;
1363 static const struct got_error *
1364 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1365 int col_tab_align)
1367 char *smallerthan;
1369 smallerthan = strchr(author, '<');
1370 if (smallerthan && smallerthan[1] != '\0')
1371 author = smallerthan + 1;
1372 author[strcspn(author, "@>")] = '\0';
1373 return format_line(wauthor, author_width, author, limit, col_tab_align);
1376 static const struct got_error *
1377 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1378 struct got_object_id *id, const size_t date_display_cols,
1379 int author_display_cols)
1381 struct tog_log_view_state *s = &view->state.log;
1382 const struct got_error *err = NULL;
1383 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1384 char *logmsg0 = NULL, *logmsg = NULL;
1385 char *author = NULL;
1386 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1387 int author_width, logmsg_width;
1388 char *newline, *line = NULL;
1389 int col, limit;
1390 const int avail = view->ncols;
1391 struct tm tm;
1392 time_t committer_time;
1393 struct tog_color *tc;
1395 committer_time = got_object_commit_get_committer_time(commit);
1396 if (gmtime_r(&committer_time, &tm) == NULL)
1397 return got_error_from_errno("gmtime_r");
1398 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1399 return got_error(GOT_ERR_NO_SPACE);
1401 if (avail <= date_display_cols)
1402 limit = MIN(sizeof(datebuf) - 1, avail);
1403 else
1404 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1405 tc = get_color(&s->colors, TOG_COLOR_DATE);
1406 if (tc)
1407 wattr_on(view->window,
1408 COLOR_PAIR(tc->colorpair), NULL);
1409 waddnstr(view->window, datebuf, limit);
1410 if (tc)
1411 wattr_off(view->window,
1412 COLOR_PAIR(tc->colorpair), NULL);
1413 col = limit;
1414 if (col > avail)
1415 goto done;
1417 if (avail >= 120) {
1418 char *id_str;
1419 err = got_object_id_str(&id_str, id);
1420 if (err)
1421 goto done;
1422 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1423 if (tc)
1424 wattr_on(view->window,
1425 COLOR_PAIR(tc->colorpair), NULL);
1426 wprintw(view->window, "%.8s ", id_str);
1427 if (tc)
1428 wattr_off(view->window,
1429 COLOR_PAIR(tc->colorpair), NULL);
1430 free(id_str);
1431 col += 9;
1432 if (col > avail)
1433 goto done;
1436 author = strdup(got_object_commit_get_author(commit));
1437 if (author == NULL) {
1438 err = got_error_from_errno("strdup");
1439 goto done;
1441 err = format_author(&wauthor, &author_width, author, avail - col, col);
1442 if (err)
1443 goto done;
1444 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1445 if (tc)
1446 wattr_on(view->window,
1447 COLOR_PAIR(tc->colorpair), NULL);
1448 waddwstr(view->window, wauthor);
1449 if (tc)
1450 wattr_off(view->window,
1451 COLOR_PAIR(tc->colorpair), NULL);
1452 col += author_width;
1453 while (col < avail && author_width < author_display_cols + 2) {
1454 waddch(view->window, ' ');
1455 col++;
1456 author_width++;
1458 if (col > avail)
1459 goto done;
1461 err = got_object_commit_get_logmsg(&logmsg0, commit);
1462 if (err)
1463 goto done;
1464 logmsg = logmsg0;
1465 while (*logmsg == '\n')
1466 logmsg++;
1467 newline = strchr(logmsg, '\n');
1468 if (newline)
1469 *newline = '\0';
1470 limit = avail - col;
1471 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1472 if (err)
1473 goto done;
1474 waddwstr(view->window, wlogmsg);
1475 col += logmsg_width;
1476 while (col < avail) {
1477 waddch(view->window, ' ');
1478 col++;
1480 done:
1481 free(logmsg0);
1482 free(wlogmsg);
1483 free(author);
1484 free(wauthor);
1485 free(line);
1486 return err;
1489 static struct commit_queue_entry *
1490 alloc_commit_queue_entry(struct got_commit_object *commit,
1491 struct got_object_id *id)
1493 struct commit_queue_entry *entry;
1495 entry = calloc(1, sizeof(*entry));
1496 if (entry == NULL)
1497 return NULL;
1499 entry->id = id;
1500 entry->commit = commit;
1501 return entry;
1504 static void
1505 pop_commit(struct commit_queue *commits)
1507 struct commit_queue_entry *entry;
1509 entry = TAILQ_FIRST(&commits->head);
1510 TAILQ_REMOVE(&commits->head, entry, entry);
1511 got_object_commit_close(entry->commit);
1512 commits->ncommits--;
1513 /* Don't free entry->id! It is owned by the commit graph. */
1514 free(entry);
1517 static void
1518 free_commits(struct commit_queue *commits)
1520 while (!TAILQ_EMPTY(&commits->head))
1521 pop_commit(commits);
1524 static const struct got_error *
1525 match_commit(int *have_match, struct got_object_id *id,
1526 struct got_commit_object *commit, regex_t *regex)
1528 const struct got_error *err = NULL;
1529 regmatch_t regmatch;
1530 char *id_str = NULL, *logmsg = NULL;
1532 *have_match = 0;
1534 err = got_object_id_str(&id_str, id);
1535 if (err)
1536 return err;
1538 err = got_object_commit_get_logmsg(&logmsg, commit);
1539 if (err)
1540 goto done;
1542 if (regexec(regex, got_object_commit_get_author(commit), 1,
1543 &regmatch, 0) == 0 ||
1544 regexec(regex, got_object_commit_get_committer(commit), 1,
1545 &regmatch, 0) == 0 ||
1546 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1547 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1548 *have_match = 1;
1549 done:
1550 free(id_str);
1551 free(logmsg);
1552 return err;
1555 static const struct got_error *
1556 queue_commits(struct tog_log_thread_args *a)
1558 const struct got_error *err = NULL;
1561 * We keep all commits open throughout the lifetime of the log
1562 * view in order to avoid having to re-fetch commits from disk
1563 * while updating the display.
1565 do {
1566 struct got_object_id *id;
1567 struct got_commit_object *commit;
1568 struct commit_queue_entry *entry;
1569 int errcode;
1571 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1572 NULL, NULL);
1573 if (err || id == NULL)
1574 break;
1576 err = got_object_open_as_commit(&commit, a->repo, id);
1577 if (err)
1578 break;
1579 entry = alloc_commit_queue_entry(commit, id);
1580 if (entry == NULL) {
1581 err = got_error_from_errno("alloc_commit_queue_entry");
1582 break;
1585 errcode = pthread_mutex_lock(&tog_mutex);
1586 if (errcode) {
1587 err = got_error_set_errno(errcode,
1588 "pthread_mutex_lock");
1589 break;
1592 entry->idx = a->commits->ncommits;
1593 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1594 a->commits->ncommits++;
1596 if (*a->searching == TOG_SEARCH_FORWARD &&
1597 !*a->search_next_done) {
1598 int have_match;
1599 err = match_commit(&have_match, id, commit, a->regex);
1600 if (err)
1601 break;
1602 if (have_match)
1603 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1606 errcode = pthread_mutex_unlock(&tog_mutex);
1607 if (errcode && err == NULL)
1608 err = got_error_set_errno(errcode,
1609 "pthread_mutex_unlock");
1610 if (err)
1611 break;
1612 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1614 return err;
1617 static void
1618 select_commit(struct tog_log_view_state *s)
1620 struct commit_queue_entry *entry;
1621 int ncommits = 0;
1623 entry = s->first_displayed_entry;
1624 while (entry) {
1625 if (ncommits == s->selected) {
1626 s->selected_entry = entry;
1627 break;
1629 entry = TAILQ_NEXT(entry, entry);
1630 ncommits++;
1634 static const struct got_error *
1635 draw_commits(struct tog_view *view)
1637 const struct got_error *err = NULL;
1638 struct tog_log_view_state *s = &view->state.log;
1639 struct commit_queue_entry *entry = s->selected_entry;
1640 const int limit = view->nlines;
1641 int width;
1642 int ncommits, author_cols = 4;
1643 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1644 char *refs_str = NULL;
1645 wchar_t *wline;
1646 struct tog_color *tc;
1647 static const size_t date_display_cols = 12;
1649 if (s->selected_entry &&
1650 !(view->searching && view->search_next_done == 0)) {
1651 struct got_reflist_head *refs;
1652 err = got_object_id_str(&id_str, s->selected_entry->id);
1653 if (err)
1654 return err;
1655 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1656 s->selected_entry->id);
1657 if (refs) {
1658 err = build_refs_str(&refs_str, refs,
1659 s->selected_entry->id, s->repo);
1660 if (err)
1661 goto done;
1665 if (s->thread_args.commits_needed == 0)
1666 halfdelay(10); /* disable fast refresh */
1668 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1669 if (asprintf(&ncommits_str, " [%d/%d] %s",
1670 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1671 (view->searching && !view->search_next_done) ?
1672 "searching..." : "loading...") == -1) {
1673 err = got_error_from_errno("asprintf");
1674 goto done;
1676 } else {
1677 const char *search_str = NULL;
1679 if (view->searching) {
1680 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1681 search_str = "no more matches";
1682 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1683 search_str = "no matches found";
1684 else if (!view->search_next_done)
1685 search_str = "searching...";
1688 if (asprintf(&ncommits_str, " [%d/%d] %s",
1689 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1690 search_str ? search_str :
1691 (refs_str ? refs_str : "")) == -1) {
1692 err = got_error_from_errno("asprintf");
1693 goto done;
1697 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1698 if (asprintf(&header, "commit %s %s%s",
1699 id_str ? id_str : "........................................",
1700 s->in_repo_path, ncommits_str) == -1) {
1701 err = got_error_from_errno("asprintf");
1702 header = NULL;
1703 goto done;
1705 } else if (asprintf(&header, "commit %s%s",
1706 id_str ? id_str : "........................................",
1707 ncommits_str) == -1) {
1708 err = got_error_from_errno("asprintf");
1709 header = NULL;
1710 goto done;
1712 err = format_line(&wline, &width, header, view->ncols, 0);
1713 if (err)
1714 goto done;
1716 werase(view->window);
1718 if (view_needs_focus_indication(view))
1719 wstandout(view->window);
1720 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1721 if (tc)
1722 wattr_on(view->window,
1723 COLOR_PAIR(tc->colorpair), NULL);
1724 waddwstr(view->window, wline);
1725 if (tc)
1726 wattr_off(view->window,
1727 COLOR_PAIR(tc->colorpair), NULL);
1728 while (width < view->ncols) {
1729 waddch(view->window, ' ');
1730 width++;
1732 if (view_needs_focus_indication(view))
1733 wstandend(view->window);
1734 free(wline);
1735 if (limit <= 1)
1736 goto done;
1738 /* Grow author column size if necessary. */
1739 entry = s->first_displayed_entry;
1740 ncommits = 0;
1741 while (entry) {
1742 char *author;
1743 wchar_t *wauthor;
1744 int width;
1745 if (ncommits >= limit - 1)
1746 break;
1747 author = strdup(got_object_commit_get_author(entry->commit));
1748 if (author == NULL) {
1749 err = got_error_from_errno("strdup");
1750 goto done;
1752 err = format_author(&wauthor, &width, author, COLS,
1753 date_display_cols);
1754 if (author_cols < width)
1755 author_cols = width;
1756 free(wauthor);
1757 free(author);
1758 ncommits++;
1759 entry = TAILQ_NEXT(entry, entry);
1762 entry = s->first_displayed_entry;
1763 s->last_displayed_entry = s->first_displayed_entry;
1764 ncommits = 0;
1765 while (entry) {
1766 if (ncommits >= limit - 1)
1767 break;
1768 if (ncommits == s->selected)
1769 wstandout(view->window);
1770 err = draw_commit(view, entry->commit, entry->id,
1771 date_display_cols, author_cols);
1772 if (ncommits == s->selected)
1773 wstandend(view->window);
1774 if (err)
1775 goto done;
1776 ncommits++;
1777 s->last_displayed_entry = entry;
1778 entry = TAILQ_NEXT(entry, entry);
1781 view_vborder(view);
1782 done:
1783 free(id_str);
1784 free(refs_str);
1785 free(ncommits_str);
1786 free(header);
1787 return err;
1790 static void
1791 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1793 struct commit_queue_entry *entry;
1794 int nscrolled = 0;
1796 entry = TAILQ_FIRST(&s->commits.head);
1797 if (s->first_displayed_entry == entry)
1798 return;
1800 entry = s->first_displayed_entry;
1801 while (entry && nscrolled < maxscroll) {
1802 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1803 if (entry) {
1804 s->first_displayed_entry = entry;
1805 nscrolled++;
1810 static const struct got_error *
1811 trigger_log_thread(struct tog_view *view, int wait)
1813 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1814 int errcode;
1816 halfdelay(1); /* fast refresh while loading commits */
1818 while (ta->commits_needed > 0 || ta->load_all) {
1819 if (ta->log_complete)
1820 break;
1822 /* Wake the log thread. */
1823 errcode = pthread_cond_signal(&ta->need_commits);
1824 if (errcode)
1825 return got_error_set_errno(errcode,
1826 "pthread_cond_signal");
1829 * The mutex will be released while the view loop waits
1830 * in wgetch(), at which time the log thread will run.
1832 if (!wait)
1833 break;
1835 /* Display progress update in log view. */
1836 show_log_view(view);
1837 update_panels();
1838 doupdate();
1840 /* Wait right here while next commit is being loaded. */
1841 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1842 if (errcode)
1843 return got_error_set_errno(errcode,
1844 "pthread_cond_wait");
1846 /* Display progress update in log view. */
1847 show_log_view(view);
1848 update_panels();
1849 doupdate();
1852 return NULL;
1855 static const struct got_error *
1856 log_scroll_down(struct tog_view *view, int maxscroll)
1858 struct tog_log_view_state *s = &view->state.log;
1859 const struct got_error *err = NULL;
1860 struct commit_queue_entry *pentry;
1861 int nscrolled = 0, ncommits_needed;
1863 if (s->last_displayed_entry == NULL)
1864 return NULL;
1866 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1867 if (s->commits.ncommits < ncommits_needed &&
1868 !s->thread_args.log_complete) {
1870 * Ask the log thread for required amount of commits.
1872 s->thread_args.commits_needed += maxscroll;
1873 err = trigger_log_thread(view, 1);
1874 if (err)
1875 return err;
1878 do {
1879 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1880 if (pentry == NULL)
1881 break;
1883 s->last_displayed_entry = pentry;
1885 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1886 if (pentry == NULL)
1887 break;
1888 s->first_displayed_entry = pentry;
1889 } while (++nscrolled < maxscroll);
1891 return err;
1894 static const struct got_error *
1895 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1896 struct got_commit_object *commit, struct got_object_id *commit_id,
1897 struct tog_view *log_view, struct got_repository *repo)
1899 const struct got_error *err;
1900 struct got_object_qid *parent_id;
1901 struct tog_view *diff_view;
1903 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1904 if (diff_view == NULL)
1905 return got_error_from_errno("view_open");
1907 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1908 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1909 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1910 if (err == NULL)
1911 *new_view = diff_view;
1912 return err;
1915 static const struct got_error *
1916 tree_view_visit_subtree(struct tog_tree_view_state *s,
1917 struct got_tree_object *subtree)
1919 struct tog_parent_tree *parent;
1921 parent = calloc(1, sizeof(*parent));
1922 if (parent == NULL)
1923 return got_error_from_errno("calloc");
1925 parent->tree = s->tree;
1926 parent->first_displayed_entry = s->first_displayed_entry;
1927 parent->selected_entry = s->selected_entry;
1928 parent->selected = s->selected;
1929 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1930 s->tree = subtree;
1931 s->selected = 0;
1932 s->first_displayed_entry = NULL;
1933 return NULL;
1936 static const struct got_error *
1937 tree_view_walk_path(struct tog_tree_view_state *s,
1938 struct got_commit_object *commit, const char *path)
1940 const struct got_error *err = NULL;
1941 struct got_tree_object *tree = NULL;
1942 const char *p;
1943 char *slash, *subpath = NULL;
1945 /* Walk the path and open corresponding tree objects. */
1946 p = path;
1947 while (*p) {
1948 struct got_tree_entry *te;
1949 struct got_object_id *tree_id;
1950 char *te_name;
1952 while (p[0] == '/')
1953 p++;
1955 /* Ensure the correct subtree entry is selected. */
1956 slash = strchr(p, '/');
1957 if (slash == NULL)
1958 te_name = strdup(p);
1959 else
1960 te_name = strndup(p, slash - p);
1961 if (te_name == NULL) {
1962 err = got_error_from_errno("strndup");
1963 break;
1965 te = got_object_tree_find_entry(s->tree, te_name);
1966 if (te == NULL) {
1967 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1968 free(te_name);
1969 break;
1971 free(te_name);
1972 s->first_displayed_entry = s->selected_entry = te;
1974 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1975 break; /* jump to this file's entry */
1977 slash = strchr(p, '/');
1978 if (slash)
1979 subpath = strndup(path, slash - path);
1980 else
1981 subpath = strdup(path);
1982 if (subpath == NULL) {
1983 err = got_error_from_errno("strdup");
1984 break;
1987 err = got_object_id_by_path(&tree_id, s->repo, commit,
1988 subpath);
1989 if (err)
1990 break;
1992 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1993 free(tree_id);
1994 if (err)
1995 break;
1997 err = tree_view_visit_subtree(s, tree);
1998 if (err) {
1999 got_object_tree_close(tree);
2000 break;
2002 if (slash == NULL)
2003 break;
2004 free(subpath);
2005 subpath = NULL;
2006 p = slash;
2009 free(subpath);
2010 return err;
2013 static const struct got_error *
2014 browse_commit_tree(struct tog_view **new_view, int begin_x,
2015 struct commit_queue_entry *entry, const char *path,
2016 const char *head_ref_name, struct got_repository *repo)
2018 const struct got_error *err = NULL;
2019 struct tog_tree_view_state *s;
2020 struct tog_view *tree_view;
2022 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2023 if (tree_view == NULL)
2024 return got_error_from_errno("view_open");
2026 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2027 if (err)
2028 return err;
2029 s = &tree_view->state.tree;
2031 *new_view = tree_view;
2033 if (got_path_is_root_dir(path))
2034 return NULL;
2036 return tree_view_walk_path(s, entry->commit, path);
2039 static const struct got_error *
2040 block_signals_used_by_main_thread(void)
2042 sigset_t sigset;
2043 int errcode;
2045 if (sigemptyset(&sigset) == -1)
2046 return got_error_from_errno("sigemptyset");
2048 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2049 if (sigaddset(&sigset, SIGWINCH) == -1)
2050 return got_error_from_errno("sigaddset");
2051 if (sigaddset(&sigset, SIGCONT) == -1)
2052 return got_error_from_errno("sigaddset");
2053 if (sigaddset(&sigset, SIGINT) == -1)
2054 return got_error_from_errno("sigaddset");
2055 if (sigaddset(&sigset, SIGTERM) == -1)
2056 return got_error_from_errno("sigaddset");
2058 /* ncurses handles SIGTSTP */
2059 if (sigaddset(&sigset, SIGTSTP) == -1)
2060 return got_error_from_errno("sigaddset");
2062 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2063 if (errcode)
2064 return got_error_set_errno(errcode, "pthread_sigmask");
2066 return NULL;
2069 static void *
2070 log_thread(void *arg)
2072 const struct got_error *err = NULL;
2073 int errcode = 0;
2074 struct tog_log_thread_args *a = arg;
2075 int done = 0;
2077 err = block_signals_used_by_main_thread();
2078 if (err)
2079 return (void *)err;
2081 while (!done && !err && !tog_fatal_signal_received()) {
2082 err = queue_commits(a);
2083 if (err) {
2084 if (err->code != GOT_ERR_ITER_COMPLETED)
2085 return (void *)err;
2086 err = NULL;
2087 done = 1;
2088 } else if (a->commits_needed > 0 && !a->load_all)
2089 a->commits_needed--;
2091 errcode = pthread_mutex_lock(&tog_mutex);
2092 if (errcode) {
2093 err = got_error_set_errno(errcode,
2094 "pthread_mutex_lock");
2095 break;
2096 } else if (*a->quit)
2097 done = 1;
2098 else if (*a->first_displayed_entry == NULL) {
2099 *a->first_displayed_entry =
2100 TAILQ_FIRST(&a->commits->head);
2101 *a->selected_entry = *a->first_displayed_entry;
2104 errcode = pthread_cond_signal(&a->commit_loaded);
2105 if (errcode) {
2106 err = got_error_set_errno(errcode,
2107 "pthread_cond_signal");
2108 pthread_mutex_unlock(&tog_mutex);
2109 break;
2112 if (done)
2113 a->commits_needed = 0;
2114 else {
2115 if (a->commits_needed == 0 && !a->load_all) {
2116 errcode = pthread_cond_wait(&a->need_commits,
2117 &tog_mutex);
2118 if (errcode)
2119 err = got_error_set_errno(errcode,
2120 "pthread_cond_wait");
2121 if (*a->quit)
2122 done = 1;
2126 errcode = pthread_mutex_unlock(&tog_mutex);
2127 if (errcode && err == NULL)
2128 err = got_error_set_errno(errcode,
2129 "pthread_mutex_unlock");
2131 a->log_complete = 1;
2132 return (void *)err;
2135 static const struct got_error *
2136 stop_log_thread(struct tog_log_view_state *s)
2138 const struct got_error *err = NULL;
2139 int errcode;
2141 if (s->thread) {
2142 s->quit = 1;
2143 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2144 if (errcode)
2145 return got_error_set_errno(errcode,
2146 "pthread_cond_signal");
2147 errcode = pthread_mutex_unlock(&tog_mutex);
2148 if (errcode)
2149 return got_error_set_errno(errcode,
2150 "pthread_mutex_unlock");
2151 errcode = pthread_join(s->thread, (void **)&err);
2152 if (errcode)
2153 return got_error_set_errno(errcode, "pthread_join");
2154 errcode = pthread_mutex_lock(&tog_mutex);
2155 if (errcode)
2156 return got_error_set_errno(errcode,
2157 "pthread_mutex_lock");
2158 s->thread = NULL;
2161 if (s->thread_args.repo) {
2162 err = got_repo_close(s->thread_args.repo);
2163 s->thread_args.repo = NULL;
2166 if (s->thread_args.pack_fds) {
2167 const struct got_error *pack_err =
2168 got_repo_pack_fds_close(s->thread_args.pack_fds);
2169 if (err == NULL)
2170 err = pack_err;
2171 s->thread_args.pack_fds = NULL;
2174 if (s->thread_args.graph) {
2175 got_commit_graph_close(s->thread_args.graph);
2176 s->thread_args.graph = NULL;
2179 return err;
2182 static const struct got_error *
2183 close_log_view(struct tog_view *view)
2185 const struct got_error *err = NULL;
2186 struct tog_log_view_state *s = &view->state.log;
2187 int errcode;
2189 err = stop_log_thread(s);
2191 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2192 if (errcode && err == NULL)
2193 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2195 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2196 if (errcode && err == NULL)
2197 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2199 free_commits(&s->commits);
2200 free(s->in_repo_path);
2201 s->in_repo_path = NULL;
2202 free(s->start_id);
2203 s->start_id = NULL;
2204 free(s->head_ref_name);
2205 s->head_ref_name = NULL;
2206 return err;
2209 static const struct got_error *
2210 search_start_log_view(struct tog_view *view)
2212 struct tog_log_view_state *s = &view->state.log;
2214 s->matched_entry = NULL;
2215 s->search_entry = NULL;
2216 return NULL;
2219 static const struct got_error *
2220 search_next_log_view(struct tog_view *view)
2222 const struct got_error *err = NULL;
2223 struct tog_log_view_state *s = &view->state.log;
2224 struct commit_queue_entry *entry;
2226 /* Display progress update in log view. */
2227 show_log_view(view);
2228 update_panels();
2229 doupdate();
2231 if (s->search_entry) {
2232 int errcode, ch;
2233 errcode = pthread_mutex_unlock(&tog_mutex);
2234 if (errcode)
2235 return got_error_set_errno(errcode,
2236 "pthread_mutex_unlock");
2237 ch = wgetch(view->window);
2238 errcode = pthread_mutex_lock(&tog_mutex);
2239 if (errcode)
2240 return got_error_set_errno(errcode,
2241 "pthread_mutex_lock");
2242 if (ch == KEY_BACKSPACE) {
2243 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2244 return NULL;
2246 if (view->searching == TOG_SEARCH_FORWARD)
2247 entry = TAILQ_NEXT(s->search_entry, entry);
2248 else
2249 entry = TAILQ_PREV(s->search_entry,
2250 commit_queue_head, entry);
2251 } else if (s->matched_entry) {
2252 if (view->searching == TOG_SEARCH_FORWARD)
2253 entry = TAILQ_NEXT(s->matched_entry, entry);
2254 else
2255 entry = TAILQ_PREV(s->matched_entry,
2256 commit_queue_head, entry);
2257 } else {
2258 entry = s->selected_entry;
2261 while (1) {
2262 int have_match = 0;
2264 if (entry == NULL) {
2265 if (s->thread_args.log_complete ||
2266 view->searching == TOG_SEARCH_BACKWARD) {
2267 view->search_next_done =
2268 (s->matched_entry == NULL ?
2269 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2270 s->search_entry = NULL;
2271 return NULL;
2274 * Poke the log thread for more commits and return,
2275 * allowing the main loop to make progress. Search
2276 * will resume at s->search_entry once we come back.
2278 s->thread_args.commits_needed++;
2279 return trigger_log_thread(view, 0);
2282 err = match_commit(&have_match, entry->id, entry->commit,
2283 &view->regex);
2284 if (err)
2285 break;
2286 if (have_match) {
2287 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2288 s->matched_entry = entry;
2289 break;
2292 s->search_entry = entry;
2293 if (view->searching == TOG_SEARCH_FORWARD)
2294 entry = TAILQ_NEXT(entry, entry);
2295 else
2296 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2299 if (s->matched_entry) {
2300 int cur = s->selected_entry->idx;
2301 while (cur < s->matched_entry->idx) {
2302 err = input_log_view(NULL, view, KEY_DOWN);
2303 if (err)
2304 return err;
2305 cur++;
2307 while (cur > s->matched_entry->idx) {
2308 err = input_log_view(NULL, view, KEY_UP);
2309 if (err)
2310 return err;
2311 cur--;
2315 s->search_entry = NULL;
2317 return NULL;
2320 static const struct got_error *
2321 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2322 struct got_repository *repo, const char *head_ref_name,
2323 const char *in_repo_path, int log_branches)
2325 const struct got_error *err = NULL;
2326 struct tog_log_view_state *s = &view->state.log;
2327 struct got_repository *thread_repo = NULL;
2328 struct got_commit_graph *thread_graph = NULL;
2329 int errcode;
2331 if (in_repo_path != s->in_repo_path) {
2332 free(s->in_repo_path);
2333 s->in_repo_path = strdup(in_repo_path);
2334 if (s->in_repo_path == NULL)
2335 return got_error_from_errno("strdup");
2338 /* The commit queue only contains commits being displayed. */
2339 TAILQ_INIT(&s->commits.head);
2340 s->commits.ncommits = 0;
2342 s->repo = repo;
2343 if (head_ref_name) {
2344 s->head_ref_name = strdup(head_ref_name);
2345 if (s->head_ref_name == NULL) {
2346 err = got_error_from_errno("strdup");
2347 goto done;
2350 s->start_id = got_object_id_dup(start_id);
2351 if (s->start_id == NULL) {
2352 err = got_error_from_errno("got_object_id_dup");
2353 goto done;
2355 s->log_branches = log_branches;
2357 STAILQ_INIT(&s->colors);
2358 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2359 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2360 get_color_value("TOG_COLOR_COMMIT"));
2361 if (err)
2362 goto done;
2363 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2364 get_color_value("TOG_COLOR_AUTHOR"));
2365 if (err) {
2366 free_colors(&s->colors);
2367 goto done;
2369 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2370 get_color_value("TOG_COLOR_DATE"));
2371 if (err) {
2372 free_colors(&s->colors);
2373 goto done;
2377 view->show = show_log_view;
2378 view->input = input_log_view;
2379 view->close = close_log_view;
2380 view->search_start = search_start_log_view;
2381 view->search_next = search_next_log_view;
2383 if (s->thread_args.pack_fds == NULL) {
2384 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2385 if (err)
2386 goto done;
2388 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2389 s->thread_args.pack_fds);
2390 if (err)
2391 goto done;
2392 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2393 !s->log_branches);
2394 if (err)
2395 goto done;
2396 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2397 s->repo, NULL, NULL);
2398 if (err)
2399 goto done;
2401 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2402 if (errcode) {
2403 err = got_error_set_errno(errcode, "pthread_cond_init");
2404 goto done;
2406 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2407 if (errcode) {
2408 err = got_error_set_errno(errcode, "pthread_cond_init");
2409 goto done;
2412 s->thread_args.commits_needed = view->nlines;
2413 s->thread_args.graph = thread_graph;
2414 s->thread_args.commits = &s->commits;
2415 s->thread_args.in_repo_path = s->in_repo_path;
2416 s->thread_args.start_id = s->start_id;
2417 s->thread_args.repo = thread_repo;
2418 s->thread_args.log_complete = 0;
2419 s->thread_args.quit = &s->quit;
2420 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2421 s->thread_args.selected_entry = &s->selected_entry;
2422 s->thread_args.searching = &view->searching;
2423 s->thread_args.search_next_done = &view->search_next_done;
2424 s->thread_args.regex = &view->regex;
2425 done:
2426 if (err)
2427 close_log_view(view);
2428 return err;
2431 static const struct got_error *
2432 show_log_view(struct tog_view *view)
2434 const struct got_error *err;
2435 struct tog_log_view_state *s = &view->state.log;
2437 if (s->thread == NULL) {
2438 int errcode = pthread_create(&s->thread, NULL, log_thread,
2439 &s->thread_args);
2440 if (errcode)
2441 return got_error_set_errno(errcode, "pthread_create");
2442 if (s->thread_args.commits_needed > 0) {
2443 err = trigger_log_thread(view, 1);
2444 if (err)
2445 return err;
2449 return draw_commits(view);
2452 static const struct got_error *
2453 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2455 const struct got_error *err = NULL;
2456 struct tog_log_view_state *s = &view->state.log;
2457 struct tog_view *diff_view = NULL, *tree_view = NULL;
2458 struct tog_view *ref_view = NULL;
2459 struct commit_queue_entry *entry;
2460 int begin_x = 0, n, nscroll = view->nlines - 1;
2462 if (s->thread_args.load_all) {
2463 if (ch == KEY_BACKSPACE)
2464 s->thread_args.load_all = 0;
2465 else if (s->thread_args.log_complete) {
2466 s->thread_args.load_all = 0;
2467 log_scroll_down(view, s->commits.ncommits);
2468 s->selected = MIN(view->nlines - 2,
2469 s->commits.ncommits - 1);
2470 select_commit(s);
2472 return NULL;
2475 switch (ch) {
2476 case 'q':
2477 s->quit = 1;
2478 break;
2479 case 'k':
2480 case KEY_UP:
2481 case '<':
2482 case ',':
2483 case CTRL('p'):
2484 if (s->first_displayed_entry == NULL)
2485 break;
2486 if (s->selected > 0)
2487 s->selected--;
2488 else
2489 log_scroll_up(s, 1);
2490 select_commit(s);
2491 break;
2492 case 'g':
2493 case KEY_HOME:
2494 s->selected = 0;
2495 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2496 select_commit(s);
2497 break;
2498 case CTRL('u'):
2499 case 'u':
2500 nscroll /= 2;
2501 /* FALL THROUGH */
2502 case KEY_PPAGE:
2503 case CTRL('b'):
2504 if (s->first_displayed_entry == NULL)
2505 break;
2506 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2507 s->selected = MAX(0, s->selected - nscroll - 1);
2508 else
2509 log_scroll_up(s, nscroll);
2510 select_commit(s);
2511 break;
2512 case 'j':
2513 case KEY_DOWN:
2514 case '>':
2515 case '.':
2516 case CTRL('n'):
2517 if (s->first_displayed_entry == NULL)
2518 break;
2519 if (s->selected < MIN(view->nlines - 2,
2520 s->commits.ncommits - 1))
2521 s->selected++;
2522 else {
2523 err = log_scroll_down(view, 1);
2524 if (err)
2525 break;
2527 select_commit(s);
2528 break;
2529 case 'G':
2530 case KEY_END: {
2531 /* We don't know yet how many commits, so we're forced to
2532 * traverse them all. */
2533 if (!s->thread_args.log_complete) {
2534 s->thread_args.load_all = 1;
2535 return trigger_log_thread(view, 0);
2538 s->selected = 0;
2539 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2540 for (n = 0; n < view->nlines - 1; n++) {
2541 if (entry == NULL)
2542 break;
2543 s->first_displayed_entry = entry;
2544 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2546 if (n > 0)
2547 s->selected = n - 1;
2548 select_commit(s);
2549 break;
2551 case CTRL('d'):
2552 case 'd':
2553 nscroll /= 2;
2554 /* FALL THROUGH */
2555 case KEY_NPAGE:
2556 case CTRL('f'): {
2557 struct commit_queue_entry *first;
2558 first = s->first_displayed_entry;
2559 if (first == NULL)
2560 break;
2561 err = log_scroll_down(view, nscroll);
2562 if (err)
2563 break;
2564 if (first == s->first_displayed_entry &&
2565 s->selected < MIN(view->nlines - 2,
2566 s->commits.ncommits - 1)) {
2567 /* can't scroll further down */
2568 s->selected += MIN(s->last_displayed_entry->idx -
2569 s->selected_entry->idx, nscroll + 1);
2571 select_commit(s);
2572 break;
2574 case KEY_RESIZE:
2575 if (s->selected > view->nlines - 2)
2576 s->selected = view->nlines - 2;
2577 if (s->selected > s->commits.ncommits - 1)
2578 s->selected = s->commits.ncommits - 1;
2579 select_commit(s);
2580 if (s->commits.ncommits < view->nlines - 1 &&
2581 !s->thread_args.log_complete) {
2582 s->thread_args.commits_needed += (view->nlines - 1) -
2583 s->commits.ncommits;
2584 err = trigger_log_thread(view, 1);
2586 break;
2587 case KEY_ENTER:
2588 case ' ':
2589 case '\r':
2590 if (s->selected_entry == NULL)
2591 break;
2592 if (view_is_parent_view(view))
2593 begin_x = view_split_begin_x(view->begin_x);
2594 err = open_diff_view_for_commit(&diff_view, begin_x,
2595 s->selected_entry->commit, s->selected_entry->id,
2596 view, s->repo);
2597 if (err)
2598 break;
2599 view->focussed = 0;
2600 diff_view->focussed = 1;
2601 if (view_is_parent_view(view)) {
2602 err = view_close_child(view);
2603 if (err)
2604 return err;
2605 view_set_child(view, diff_view);
2606 view->focus_child = 1;
2607 } else
2608 *new_view = diff_view;
2609 break;
2610 case 't':
2611 if (s->selected_entry == NULL)
2612 break;
2613 if (view_is_parent_view(view))
2614 begin_x = view_split_begin_x(view->begin_x);
2615 err = browse_commit_tree(&tree_view, begin_x,
2616 s->selected_entry, s->in_repo_path, s->head_ref_name,
2617 s->repo);
2618 if (err)
2619 break;
2620 view->focussed = 0;
2621 tree_view->focussed = 1;
2622 if (view_is_parent_view(view)) {
2623 err = view_close_child(view);
2624 if (err)
2625 return err;
2626 view_set_child(view, tree_view);
2627 view->focus_child = 1;
2628 } else
2629 *new_view = tree_view;
2630 break;
2631 case KEY_BACKSPACE:
2632 case CTRL('l'):
2633 case 'B':
2634 if (ch == KEY_BACKSPACE &&
2635 got_path_is_root_dir(s->in_repo_path))
2636 break;
2637 err = stop_log_thread(s);
2638 if (err)
2639 return err;
2640 if (ch == KEY_BACKSPACE) {
2641 char *parent_path;
2642 err = got_path_dirname(&parent_path, s->in_repo_path);
2643 if (err)
2644 return err;
2645 free(s->in_repo_path);
2646 s->in_repo_path = parent_path;
2647 s->thread_args.in_repo_path = s->in_repo_path;
2648 } else if (ch == CTRL('l')) {
2649 struct got_object_id *start_id;
2650 err = got_repo_match_object_id(&start_id, NULL,
2651 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2652 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2653 if (err)
2654 return err;
2655 free(s->start_id);
2656 s->start_id = start_id;
2657 s->thread_args.start_id = s->start_id;
2658 } else /* 'B' */
2659 s->log_branches = !s->log_branches;
2661 err = got_repo_open(&s->thread_args.repo,
2662 got_repo_get_path(s->repo), NULL,
2663 s->thread_args.pack_fds);
2664 if (err)
2665 return err;
2666 tog_free_refs();
2667 err = tog_load_refs(s->repo, 0);
2668 if (err)
2669 return err;
2670 err = got_commit_graph_open(&s->thread_args.graph,
2671 s->in_repo_path, !s->log_branches);
2672 if (err)
2673 return err;
2674 err = got_commit_graph_iter_start(s->thread_args.graph,
2675 s->start_id, s->repo, NULL, NULL);
2676 if (err)
2677 return err;
2678 free_commits(&s->commits);
2679 s->first_displayed_entry = NULL;
2680 s->last_displayed_entry = NULL;
2681 s->selected_entry = NULL;
2682 s->selected = 0;
2683 s->thread_args.log_complete = 0;
2684 s->quit = 0;
2685 s->thread_args.commits_needed = view->nlines;
2686 break;
2687 case 'r':
2688 if (view_is_parent_view(view))
2689 begin_x = view_split_begin_x(view->begin_x);
2690 ref_view = view_open(view->nlines, view->ncols,
2691 view->begin_y, begin_x, TOG_VIEW_REF);
2692 if (ref_view == NULL)
2693 return got_error_from_errno("view_open");
2694 err = open_ref_view(ref_view, s->repo);
2695 if (err) {
2696 view_close(ref_view);
2697 return err;
2699 view->focussed = 0;
2700 ref_view->focussed = 1;
2701 if (view_is_parent_view(view)) {
2702 err = view_close_child(view);
2703 if (err)
2704 return err;
2705 view_set_child(view, ref_view);
2706 view->focus_child = 1;
2707 } else
2708 *new_view = ref_view;
2709 break;
2710 default:
2711 break;
2714 return err;
2717 static const struct got_error *
2718 apply_unveil(const char *repo_path, const char *worktree_path)
2720 const struct got_error *error;
2722 #ifdef PROFILE
2723 if (unveil("gmon.out", "rwc") != 0)
2724 return got_error_from_errno2("unveil", "gmon.out");
2725 #endif
2726 if (repo_path && unveil(repo_path, "r") != 0)
2727 return got_error_from_errno2("unveil", repo_path);
2729 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2730 return got_error_from_errno2("unveil", worktree_path);
2732 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2733 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2735 error = got_privsep_unveil_exec_helpers();
2736 if (error != NULL)
2737 return error;
2739 if (unveil(NULL, NULL) != 0)
2740 return got_error_from_errno("unveil");
2742 return NULL;
2745 static void
2746 init_curses(void)
2749 * Override default signal handlers before starting ncurses.
2750 * This should prevent ncurses from installing its own
2751 * broken cleanup() signal handler.
2753 signal(SIGWINCH, tog_sigwinch);
2754 signal(SIGPIPE, tog_sigpipe);
2755 signal(SIGCONT, tog_sigcont);
2756 signal(SIGINT, tog_sigint);
2757 signal(SIGTERM, tog_sigterm);
2759 initscr();
2760 cbreak();
2761 halfdelay(1); /* Do fast refresh while initial view is loading. */
2762 noecho();
2763 nonl();
2764 intrflush(stdscr, FALSE);
2765 keypad(stdscr, TRUE);
2766 curs_set(0);
2767 if (getenv("TOG_COLORS") != NULL) {
2768 start_color();
2769 use_default_colors();
2773 static const struct got_error *
2774 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2775 struct got_repository *repo, struct got_worktree *worktree)
2777 const struct got_error *err = NULL;
2779 if (argc == 0) {
2780 *in_repo_path = strdup("/");
2781 if (*in_repo_path == NULL)
2782 return got_error_from_errno("strdup");
2783 return NULL;
2786 if (worktree) {
2787 const char *prefix = got_worktree_get_path_prefix(worktree);
2788 char *p;
2790 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2791 if (err)
2792 return err;
2793 if (asprintf(in_repo_path, "%s%s%s", prefix,
2794 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2795 p) == -1) {
2796 err = got_error_from_errno("asprintf");
2797 *in_repo_path = NULL;
2799 free(p);
2800 } else
2801 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2803 return err;
2806 static const struct got_error *
2807 cmd_log(int argc, char *argv[])
2809 const struct got_error *error;
2810 struct got_repository *repo = NULL;
2811 struct got_worktree *worktree = NULL;
2812 struct got_object_id *start_id = NULL;
2813 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2814 char *start_commit = NULL, *label = NULL;
2815 struct got_reference *ref = NULL;
2816 const char *head_ref_name = NULL;
2817 int ch, log_branches = 0;
2818 struct tog_view *view;
2819 int *pack_fds = NULL;
2821 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2822 switch (ch) {
2823 case 'b':
2824 log_branches = 1;
2825 break;
2826 case 'c':
2827 start_commit = optarg;
2828 break;
2829 case 'r':
2830 repo_path = realpath(optarg, NULL);
2831 if (repo_path == NULL)
2832 return got_error_from_errno2("realpath",
2833 optarg);
2834 break;
2835 default:
2836 usage_log();
2837 /* NOTREACHED */
2841 argc -= optind;
2842 argv += optind;
2844 if (argc > 1)
2845 usage_log();
2847 error = got_repo_pack_fds_open(&pack_fds);
2848 if (error != NULL)
2849 goto done;
2851 if (repo_path == NULL) {
2852 cwd = getcwd(NULL, 0);
2853 if (cwd == NULL)
2854 return got_error_from_errno("getcwd");
2855 error = got_worktree_open(&worktree, cwd);
2856 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2857 goto done;
2858 if (worktree)
2859 repo_path =
2860 strdup(got_worktree_get_repo_path(worktree));
2861 else
2862 repo_path = strdup(cwd);
2863 if (repo_path == NULL) {
2864 error = got_error_from_errno("strdup");
2865 goto done;
2869 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2870 if (error != NULL)
2871 goto done;
2873 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2874 repo, worktree);
2875 if (error)
2876 goto done;
2878 init_curses();
2880 error = apply_unveil(got_repo_get_path(repo),
2881 worktree ? got_worktree_get_root_path(worktree) : NULL);
2882 if (error)
2883 goto done;
2885 /* already loaded by tog_log_with_path()? */
2886 if (TAILQ_EMPTY(&tog_refs)) {
2887 error = tog_load_refs(repo, 0);
2888 if (error)
2889 goto done;
2892 if (start_commit == NULL) {
2893 error = got_repo_match_object_id(&start_id, &label,
2894 worktree ? got_worktree_get_head_ref_name(worktree) :
2895 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2896 if (error)
2897 goto done;
2898 head_ref_name = label;
2899 } else {
2900 error = got_ref_open(&ref, repo, start_commit, 0);
2901 if (error == NULL)
2902 head_ref_name = got_ref_get_name(ref);
2903 else if (error->code != GOT_ERR_NOT_REF)
2904 goto done;
2905 error = got_repo_match_object_id(&start_id, NULL,
2906 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2907 if (error)
2908 goto done;
2911 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2912 if (view == NULL) {
2913 error = got_error_from_errno("view_open");
2914 goto done;
2916 error = open_log_view(view, start_id, repo, head_ref_name,
2917 in_repo_path, log_branches);
2918 if (error)
2919 goto done;
2920 if (worktree) {
2921 /* Release work tree lock. */
2922 got_worktree_close(worktree);
2923 worktree = NULL;
2925 error = view_loop(view);
2926 done:
2927 free(in_repo_path);
2928 free(repo_path);
2929 free(cwd);
2930 free(start_id);
2931 free(label);
2932 if (ref)
2933 got_ref_close(ref);
2934 if (repo) {
2935 const struct got_error *close_err = got_repo_close(repo);
2936 if (error == NULL)
2937 error = close_err;
2939 if (worktree)
2940 got_worktree_close(worktree);
2941 if (pack_fds) {
2942 const struct got_error *pack_err =
2943 got_repo_pack_fds_close(pack_fds);
2944 if (error == NULL)
2945 error = pack_err;
2946 pack_fds = NULL;
2948 tog_free_refs();
2949 return error;
2952 __dead static void
2953 usage_diff(void)
2955 endwin();
2956 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2957 "[-w] object1 object2\n", getprogname());
2958 exit(1);
2961 static int
2962 match_line(const char *line, regex_t *regex, size_t nmatch,
2963 regmatch_t *regmatch)
2965 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2968 struct tog_color *
2969 match_color(struct tog_colors *colors, const char *line)
2971 struct tog_color *tc = NULL;
2973 STAILQ_FOREACH(tc, colors, entry) {
2974 if (match_line(line, &tc->regex, 0, NULL))
2975 return tc;
2978 return NULL;
2981 static const struct got_error *
2982 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2983 WINDOW *window, regmatch_t *regmatch)
2985 const struct got_error *err = NULL;
2986 wchar_t *wline;
2987 int width;
2988 char *s;
2990 *wtotal = 0;
2992 s = strndup(line, regmatch->rm_so);
2993 if (s == NULL)
2994 return got_error_from_errno("strndup");
2996 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2997 if (err) {
2998 free(s);
2999 return err;
3001 waddwstr(window, wline);
3002 free(wline);
3003 free(s);
3004 wlimit -= width;
3005 *wtotal += width;
3007 if (wlimit > 0) {
3008 s = strndup(line + regmatch->rm_so,
3009 regmatch->rm_eo - regmatch->rm_so);
3010 if (s == NULL) {
3011 err = got_error_from_errno("strndup");
3012 free(s);
3013 return err;
3015 err = format_line(&wline, &width, s, wlimit, col_tab_align);
3016 if (err) {
3017 free(s);
3018 return err;
3020 wattr_on(window, A_STANDOUT, NULL);
3021 waddwstr(window, wline);
3022 wattr_off(window, A_STANDOUT, NULL);
3023 free(wline);
3024 free(s);
3025 wlimit -= width;
3026 *wtotal += width;
3029 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3030 err = format_line(&wline, &width,
3031 line + regmatch->rm_eo, wlimit, col_tab_align);
3032 if (err)
3033 return err;
3034 waddwstr(window, wline);
3035 free(wline);
3036 *wtotal += width;
3039 return NULL;
3042 static const struct got_error *
3043 draw_file(struct tog_view *view, const char *header)
3045 struct tog_diff_view_state *s = &view->state.diff;
3046 regmatch_t *regmatch = &view->regmatch;
3047 const struct got_error *err;
3048 int nprinted = 0;
3049 char *line;
3050 size_t linesize = 0;
3051 ssize_t linelen;
3052 struct tog_color *tc;
3053 wchar_t *wline;
3054 int width;
3055 int max_lines = view->nlines;
3056 int nlines = s->nlines;
3057 off_t line_offset;
3059 line_offset = s->line_offsets[s->first_displayed_line - 1];
3060 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3061 return got_error_from_errno("fseek");
3063 werase(view->window);
3065 if (header) {
3066 if (asprintf(&line, "[%d/%d] %s",
3067 s->first_displayed_line - 1 + s->selected_line, nlines,
3068 header) == -1)
3069 return got_error_from_errno("asprintf");
3070 err = format_line(&wline, &width, line, view->ncols, 0);
3071 free(line);
3072 if (err)
3073 return err;
3075 if (view_needs_focus_indication(view))
3076 wstandout(view->window);
3077 waddwstr(view->window, wline);
3078 free(wline);
3079 wline = NULL;
3080 if (view_needs_focus_indication(view))
3081 wstandend(view->window);
3082 if (width <= view->ncols - 1)
3083 waddch(view->window, '\n');
3085 if (max_lines <= 1)
3086 return NULL;
3087 max_lines--;
3090 s->eof = 0;
3091 line = NULL;
3092 while (max_lines > 0 && nprinted < max_lines) {
3093 linelen = getline(&line, &linesize, s->f);
3094 if (linelen == -1) {
3095 if (feof(s->f)) {
3096 s->eof = 1;
3097 break;
3099 free(line);
3100 return got_ferror(s->f, GOT_ERR_IO);
3103 tc = match_color(&s->colors, line);
3104 if (tc)
3105 wattr_on(view->window,
3106 COLOR_PAIR(tc->colorpair), NULL);
3107 if (s->first_displayed_line + nprinted == s->matched_line &&
3108 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3109 err = add_matched_line(&width, line, view->ncols, 0,
3110 view->window, regmatch);
3111 if (err) {
3112 free(line);
3113 return err;
3115 } else {
3116 err = format_line(&wline, &width, line, view->ncols, 0);
3117 if (err) {
3118 free(line);
3119 return err;
3121 waddwstr(view->window, wline);
3122 free(wline);
3123 wline = NULL;
3125 if (tc)
3126 wattr_off(view->window,
3127 COLOR_PAIR(tc->colorpair), NULL);
3128 if (width <= view->ncols - 1)
3129 waddch(view->window, '\n');
3130 nprinted++;
3132 free(line);
3133 if (nprinted >= 1)
3134 s->last_displayed_line = s->first_displayed_line +
3135 (nprinted - 1);
3136 else
3137 s->last_displayed_line = s->first_displayed_line;
3139 view_vborder(view);
3141 if (s->eof) {
3142 while (nprinted < view->nlines) {
3143 waddch(view->window, '\n');
3144 nprinted++;
3147 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3148 if (err) {
3149 return err;
3152 wstandout(view->window);
3153 waddwstr(view->window, wline);
3154 free(wline);
3155 wline = NULL;
3156 wstandend(view->window);
3159 return NULL;
3162 static char *
3163 get_datestr(time_t *time, char *datebuf)
3165 struct tm mytm, *tm;
3166 char *p, *s;
3168 tm = gmtime_r(time, &mytm);
3169 if (tm == NULL)
3170 return NULL;
3171 s = asctime_r(tm, datebuf);
3172 if (s == NULL)
3173 return NULL;
3174 p = strchr(s, '\n');
3175 if (p)
3176 *p = '\0';
3177 return s;
3180 static const struct got_error *
3181 get_changed_paths(struct got_pathlist_head *paths,
3182 struct got_commit_object *commit, struct got_repository *repo)
3184 const struct got_error *err = NULL;
3185 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3186 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3187 struct got_object_qid *qid;
3189 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3190 if (qid != NULL) {
3191 struct got_commit_object *pcommit;
3192 err = got_object_open_as_commit(&pcommit, repo,
3193 &qid->id);
3194 if (err)
3195 return err;
3197 tree_id1 = got_object_id_dup(
3198 got_object_commit_get_tree_id(pcommit));
3199 if (tree_id1 == NULL) {
3200 got_object_commit_close(pcommit);
3201 return got_error_from_errno("got_object_id_dup");
3203 got_object_commit_close(pcommit);
3207 if (tree_id1) {
3208 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3209 if (err)
3210 goto done;
3213 tree_id2 = got_object_commit_get_tree_id(commit);
3214 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3215 if (err)
3216 goto done;
3218 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3219 got_diff_tree_collect_changed_paths, paths, 0);
3220 done:
3221 if (tree1)
3222 got_object_tree_close(tree1);
3223 if (tree2)
3224 got_object_tree_close(tree2);
3225 free(tree_id1);
3226 return err;
3229 static const struct got_error *
3230 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3232 off_t *p;
3234 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3235 if (p == NULL)
3236 return got_error_from_errno("reallocarray");
3237 *line_offsets = p;
3238 (*line_offsets)[*nlines] = off;
3239 (*nlines)++;
3240 return NULL;
3243 static const struct got_error *
3244 write_commit_info(off_t **line_offsets, size_t *nlines,
3245 struct got_object_id *commit_id, struct got_reflist_head *refs,
3246 struct got_repository *repo, FILE *outfile)
3248 const struct got_error *err = NULL;
3249 char datebuf[26], *datestr;
3250 struct got_commit_object *commit;
3251 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3252 time_t committer_time;
3253 const char *author, *committer;
3254 char *refs_str = NULL;
3255 struct got_pathlist_head changed_paths;
3256 struct got_pathlist_entry *pe;
3257 off_t outoff = 0;
3258 int n;
3260 TAILQ_INIT(&changed_paths);
3262 if (refs) {
3263 err = build_refs_str(&refs_str, refs, commit_id, repo);
3264 if (err)
3265 return err;
3268 err = got_object_open_as_commit(&commit, repo, commit_id);
3269 if (err)
3270 return err;
3272 err = got_object_id_str(&id_str, commit_id);
3273 if (err) {
3274 err = got_error_from_errno("got_object_id_str");
3275 goto done;
3278 err = add_line_offset(line_offsets, nlines, 0);
3279 if (err)
3280 goto done;
3282 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3283 refs_str ? refs_str : "", refs_str ? ")" : "");
3284 if (n < 0) {
3285 err = got_error_from_errno("fprintf");
3286 goto done;
3288 outoff += n;
3289 err = add_line_offset(line_offsets, nlines, outoff);
3290 if (err)
3291 goto done;
3293 n = fprintf(outfile, "from: %s\n",
3294 got_object_commit_get_author(commit));
3295 if (n < 0) {
3296 err = got_error_from_errno("fprintf");
3297 goto done;
3299 outoff += n;
3300 err = add_line_offset(line_offsets, nlines, outoff);
3301 if (err)
3302 goto done;
3304 committer_time = got_object_commit_get_committer_time(commit);
3305 datestr = get_datestr(&committer_time, datebuf);
3306 if (datestr) {
3307 n = fprintf(outfile, "date: %s UTC\n", datestr);
3308 if (n < 0) {
3309 err = got_error_from_errno("fprintf");
3310 goto done;
3312 outoff += n;
3313 err = add_line_offset(line_offsets, nlines, outoff);
3314 if (err)
3315 goto done;
3317 author = got_object_commit_get_author(commit);
3318 committer = got_object_commit_get_committer(commit);
3319 if (strcmp(author, committer) != 0) {
3320 n = fprintf(outfile, "via: %s\n", committer);
3321 if (n < 0) {
3322 err = got_error_from_errno("fprintf");
3323 goto done;
3325 outoff += n;
3326 err = add_line_offset(line_offsets, nlines, outoff);
3327 if (err)
3328 goto done;
3330 if (got_object_commit_get_nparents(commit) > 1) {
3331 const struct got_object_id_queue *parent_ids;
3332 struct got_object_qid *qid;
3333 int pn = 1;
3334 parent_ids = got_object_commit_get_parent_ids(commit);
3335 STAILQ_FOREACH(qid, parent_ids, entry) {
3336 err = got_object_id_str(&id_str, &qid->id);
3337 if (err)
3338 goto done;
3339 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3340 if (n < 0) {
3341 err = got_error_from_errno("fprintf");
3342 goto done;
3344 outoff += n;
3345 err = add_line_offset(line_offsets, nlines, outoff);
3346 if (err)
3347 goto done;
3348 free(id_str);
3349 id_str = NULL;
3353 err = got_object_commit_get_logmsg(&logmsg, commit);
3354 if (err)
3355 goto done;
3356 s = logmsg;
3357 while ((line = strsep(&s, "\n")) != NULL) {
3358 n = fprintf(outfile, "%s\n", line);
3359 if (n < 0) {
3360 err = got_error_from_errno("fprintf");
3361 goto done;
3363 outoff += n;
3364 err = add_line_offset(line_offsets, nlines, outoff);
3365 if (err)
3366 goto done;
3369 err = get_changed_paths(&changed_paths, commit, repo);
3370 if (err)
3371 goto done;
3372 TAILQ_FOREACH(pe, &changed_paths, entry) {
3373 struct got_diff_changed_path *cp = pe->data;
3374 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3375 if (n < 0) {
3376 err = got_error_from_errno("fprintf");
3377 goto done;
3379 outoff += n;
3380 err = add_line_offset(line_offsets, nlines, outoff);
3381 if (err)
3382 goto done;
3383 free((char *)pe->path);
3384 free(pe->data);
3387 fputc('\n', outfile);
3388 outoff++;
3389 err = add_line_offset(line_offsets, nlines, outoff);
3390 done:
3391 got_pathlist_free(&changed_paths);
3392 free(id_str);
3393 free(logmsg);
3394 free(refs_str);
3395 got_object_commit_close(commit);
3396 if (err) {
3397 free(*line_offsets);
3398 *line_offsets = NULL;
3399 *nlines = 0;
3401 return err;
3404 static const struct got_error *
3405 create_diff(struct tog_diff_view_state *s)
3407 const struct got_error *err = NULL;
3408 FILE *f = NULL;
3409 int obj_type;
3411 free(s->line_offsets);
3412 s->line_offsets = malloc(sizeof(off_t));
3413 if (s->line_offsets == NULL)
3414 return got_error_from_errno("malloc");
3415 s->nlines = 0;
3417 f = got_opentemp();
3418 if (f == NULL) {
3419 err = got_error_from_errno("got_opentemp");
3420 goto done;
3422 if (s->f && fclose(s->f) == EOF) {
3423 err = got_error_from_errno("fclose");
3424 goto done;
3426 s->f = f;
3428 if (s->id1)
3429 err = got_object_get_type(&obj_type, s->repo, s->id1);
3430 else
3431 err = got_object_get_type(&obj_type, s->repo, s->id2);
3432 if (err)
3433 goto done;
3435 switch (obj_type) {
3436 case GOT_OBJ_TYPE_BLOB:
3437 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3438 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3439 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3440 s->repo, s->f);
3441 break;
3442 case GOT_OBJ_TYPE_TREE:
3443 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3444 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3445 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3446 break;
3447 case GOT_OBJ_TYPE_COMMIT: {
3448 const struct got_object_id_queue *parent_ids;
3449 struct got_object_qid *pid;
3450 struct got_commit_object *commit2;
3451 struct got_reflist_head *refs;
3453 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3454 if (err)
3455 goto done;
3456 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3457 /* Show commit info if we're diffing to a parent/root commit. */
3458 if (s->id1 == NULL) {
3459 err = write_commit_info(&s->line_offsets, &s->nlines,
3460 s->id2, refs, s->repo, s->f);
3461 if (err)
3462 goto done;
3463 } else {
3464 parent_ids = got_object_commit_get_parent_ids(commit2);
3465 STAILQ_FOREACH(pid, parent_ids, entry) {
3466 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3467 err = write_commit_info(
3468 &s->line_offsets, &s->nlines,
3469 s->id2, refs, s->repo, s->f);
3470 if (err)
3471 goto done;
3472 break;
3476 got_object_commit_close(commit2);
3478 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3479 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3480 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3481 break;
3483 default:
3484 err = got_error(GOT_ERR_OBJ_TYPE);
3485 break;
3487 if (err)
3488 goto done;
3489 done:
3490 if (s->f && fflush(s->f) != 0 && err == NULL)
3491 err = got_error_from_errno("fflush");
3492 return err;
3495 static void
3496 diff_view_indicate_progress(struct tog_view *view)
3498 mvwaddstr(view->window, 0, 0, "diffing...");
3499 update_panels();
3500 doupdate();
3503 static const struct got_error *
3504 search_start_diff_view(struct tog_view *view)
3506 struct tog_diff_view_state *s = &view->state.diff;
3508 s->matched_line = 0;
3509 return NULL;
3512 static const struct got_error *
3513 search_next_diff_view(struct tog_view *view)
3515 struct tog_diff_view_state *s = &view->state.diff;
3516 int lineno;
3517 char *line = NULL;
3518 size_t linesize = 0;
3519 ssize_t linelen;
3521 if (!view->searching) {
3522 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3523 return NULL;
3526 if (s->matched_line) {
3527 if (view->searching == TOG_SEARCH_FORWARD)
3528 lineno = s->matched_line + 1;
3529 else
3530 lineno = s->matched_line - 1;
3531 } else
3532 lineno = s->first_displayed_line;
3534 while (1) {
3535 off_t offset;
3537 if (lineno <= 0 || lineno > s->nlines) {
3538 if (s->matched_line == 0) {
3539 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3540 break;
3543 if (view->searching == TOG_SEARCH_FORWARD)
3544 lineno = 1;
3545 else
3546 lineno = s->nlines;
3549 offset = s->line_offsets[lineno - 1];
3550 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3551 free(line);
3552 return got_error_from_errno("fseeko");
3554 linelen = getline(&line, &linesize, s->f);
3555 if (linelen != -1 &&
3556 match_line(line, &view->regex, 1, &view->regmatch)) {
3557 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3558 s->matched_line = lineno;
3559 break;
3561 if (view->searching == TOG_SEARCH_FORWARD)
3562 lineno++;
3563 else
3564 lineno--;
3566 free(line);
3568 if (s->matched_line) {
3569 s->first_displayed_line = s->matched_line;
3570 s->selected_line = 1;
3573 return NULL;
3576 static const struct got_error *
3577 close_diff_view(struct tog_view *view)
3579 const struct got_error *err = NULL;
3580 struct tog_diff_view_state *s = &view->state.diff;
3582 free(s->id1);
3583 s->id1 = NULL;
3584 free(s->id2);
3585 s->id2 = NULL;
3586 if (s->f && fclose(s->f) == EOF)
3587 err = got_error_from_errno("fclose");
3588 s->f = NULL;
3589 if (s->f1 && fclose(s->f1) == EOF)
3590 err = got_error_from_errno("fclose");
3591 s->f1 = NULL;
3592 if (s->f2 && fclose(s->f2) == EOF)
3593 err = got_error_from_errno("fclose");
3594 s->f2 = NULL;
3595 free_colors(&s->colors);
3596 free(s->line_offsets);
3597 s->line_offsets = NULL;
3598 s->nlines = 0;
3599 return err;
3602 static const struct got_error *
3603 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3604 struct got_object_id *id2, const char *label1, const char *label2,
3605 int diff_context, int ignore_whitespace, int force_text_diff,
3606 struct tog_view *log_view, struct got_repository *repo)
3608 const struct got_error *err;
3609 struct tog_diff_view_state *s = &view->state.diff;
3611 memset(s, 0, sizeof(*s));
3613 if (id1 != NULL && id2 != NULL) {
3614 int type1, type2;
3615 err = got_object_get_type(&type1, repo, id1);
3616 if (err)
3617 return err;
3618 err = got_object_get_type(&type2, repo, id2);
3619 if (err)
3620 return err;
3622 if (type1 != type2)
3623 return got_error(GOT_ERR_OBJ_TYPE);
3625 s->first_displayed_line = 1;
3626 s->last_displayed_line = view->nlines;
3627 s->selected_line = 1;
3628 s->repo = repo;
3629 s->id1 = id1;
3630 s->id2 = id2;
3631 s->label1 = label1;
3632 s->label2 = label2;
3634 if (id1) {
3635 s->id1 = got_object_id_dup(id1);
3636 if (s->id1 == NULL)
3637 return got_error_from_errno("got_object_id_dup");
3638 s->f1 = got_opentemp();
3639 if (s->f1 == NULL) {
3640 err = got_error_from_errno("got_opentemp");
3641 goto done;
3643 } else
3644 s->id1 = NULL;
3646 s->id2 = got_object_id_dup(id2);
3647 if (s->id2 == NULL) {
3648 err = got_error_from_errno("got_object_id_dup");
3649 goto done;
3652 s->f2 = got_opentemp();
3653 if (s->f2 == NULL) {
3654 err = got_error_from_errno("got_opentemp");
3655 goto done;
3658 s->first_displayed_line = 1;
3659 s->last_displayed_line = view->nlines;
3660 s->diff_context = diff_context;
3661 s->ignore_whitespace = ignore_whitespace;
3662 s->force_text_diff = force_text_diff;
3663 s->log_view = log_view;
3664 s->repo = repo;
3666 STAILQ_INIT(&s->colors);
3667 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3668 err = add_color(&s->colors,
3669 "^-", TOG_COLOR_DIFF_MINUS,
3670 get_color_value("TOG_COLOR_DIFF_MINUS"));
3671 if (err)
3672 goto done;
3673 err = add_color(&s->colors, "^\\+",
3674 TOG_COLOR_DIFF_PLUS,
3675 get_color_value("TOG_COLOR_DIFF_PLUS"));
3676 if (err)
3677 goto done;
3678 err = add_color(&s->colors,
3679 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3680 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3681 if (err)
3682 goto done;
3684 err = add_color(&s->colors,
3685 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3686 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3687 get_color_value("TOG_COLOR_DIFF_META"));
3688 if (err)
3689 goto done;
3691 err = add_color(&s->colors,
3692 "^(from|via): ", TOG_COLOR_AUTHOR,
3693 get_color_value("TOG_COLOR_AUTHOR"));
3694 if (err)
3695 goto done;
3697 err = add_color(&s->colors,
3698 "^date: ", TOG_COLOR_DATE,
3699 get_color_value("TOG_COLOR_DATE"));
3700 if (err)
3701 goto done;
3704 if (log_view && view_is_splitscreen(view))
3705 show_log_view(log_view); /* draw vborder */
3706 diff_view_indicate_progress(view);
3708 err = create_diff(s);
3710 view->show = show_diff_view;
3711 view->input = input_diff_view;
3712 view->close = close_diff_view;
3713 view->search_start = search_start_diff_view;
3714 view->search_next = search_next_diff_view;
3715 done:
3716 if (err)
3717 close_diff_view(view);
3718 return err;
3721 static const struct got_error *
3722 show_diff_view(struct tog_view *view)
3724 const struct got_error *err;
3725 struct tog_diff_view_state *s = &view->state.diff;
3726 char *id_str1 = NULL, *id_str2, *header;
3727 const char *label1, *label2;
3729 if (s->id1) {
3730 err = got_object_id_str(&id_str1, s->id1);
3731 if (err)
3732 return err;
3733 label1 = s->label1 ? : id_str1;
3734 } else
3735 label1 = "/dev/null";
3737 err = got_object_id_str(&id_str2, s->id2);
3738 if (err)
3739 return err;
3740 label2 = s->label2 ? : id_str2;
3742 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3743 err = got_error_from_errno("asprintf");
3744 free(id_str1);
3745 free(id_str2);
3746 return err;
3748 free(id_str1);
3749 free(id_str2);
3751 err = draw_file(view, header);
3752 free(header);
3753 return err;
3756 static const struct got_error *
3757 set_selected_commit(struct tog_diff_view_state *s,
3758 struct commit_queue_entry *entry)
3760 const struct got_error *err;
3761 const struct got_object_id_queue *parent_ids;
3762 struct got_commit_object *selected_commit;
3763 struct got_object_qid *pid;
3765 free(s->id2);
3766 s->id2 = got_object_id_dup(entry->id);
3767 if (s->id2 == NULL)
3768 return got_error_from_errno("got_object_id_dup");
3770 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3771 if (err)
3772 return err;
3773 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3774 free(s->id1);
3775 pid = STAILQ_FIRST(parent_ids);
3776 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3777 got_object_commit_close(selected_commit);
3778 return NULL;
3781 static const struct got_error *
3782 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3784 const struct got_error *err = NULL;
3785 struct tog_diff_view_state *s = &view->state.diff;
3786 struct tog_log_view_state *ls;
3787 struct commit_queue_entry *old_selected_entry;
3788 char *line = NULL;
3789 size_t linesize = 0;
3790 ssize_t linelen;
3791 int i, nscroll = view->nlines - 1;
3793 switch (ch) {
3794 case 'a':
3795 case 'w':
3796 if (ch == 'a')
3797 s->force_text_diff = !s->force_text_diff;
3798 if (ch == 'w')
3799 s->ignore_whitespace = !s->ignore_whitespace;
3800 wclear(view->window);
3801 s->first_displayed_line = 1;
3802 s->last_displayed_line = view->nlines;
3803 s->matched_line = 0;
3804 diff_view_indicate_progress(view);
3805 err = create_diff(s);
3806 break;
3807 case 'g':
3808 case KEY_HOME:
3809 s->first_displayed_line = 1;
3810 break;
3811 case 'G':
3812 case KEY_END:
3813 if (s->eof)
3814 break;
3816 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3817 s->eof = 1;
3818 break;
3819 case 'k':
3820 case KEY_UP:
3821 case CTRL('p'):
3822 if (s->first_displayed_line > 1)
3823 s->first_displayed_line--;
3824 break;
3825 case CTRL('u'):
3826 case 'u':
3827 nscroll /= 2;
3828 /* FALL THROUGH */
3829 case KEY_PPAGE:
3830 case CTRL('b'):
3831 if (s->first_displayed_line == 1)
3832 break;
3833 i = 0;
3834 while (i++ < nscroll && s->first_displayed_line > 1)
3835 s->first_displayed_line--;
3836 break;
3837 case 'j':
3838 case KEY_DOWN:
3839 case CTRL('n'):
3840 if (!s->eof)
3841 s->first_displayed_line++;
3842 break;
3843 case CTRL('d'):
3844 case 'd':
3845 nscroll /= 2;
3846 /* FALL THROUGH */
3847 case KEY_NPAGE:
3848 case CTRL('f'):
3849 case ' ':
3850 if (s->eof)
3851 break;
3852 i = 0;
3853 while (!s->eof && i++ < nscroll) {
3854 linelen = getline(&line, &linesize, s->f);
3855 s->first_displayed_line++;
3856 if (linelen == -1) {
3857 if (feof(s->f)) {
3858 s->eof = 1;
3859 } else
3860 err = got_ferror(s->f, GOT_ERR_IO);
3861 break;
3864 free(line);
3865 break;
3866 case '[':
3867 if (s->diff_context > 0) {
3868 s->diff_context--;
3869 s->matched_line = 0;
3870 diff_view_indicate_progress(view);
3871 err = create_diff(s);
3872 if (s->first_displayed_line + view->nlines - 1 >
3873 s->nlines) {
3874 s->first_displayed_line = 1;
3875 s->last_displayed_line = view->nlines;
3878 break;
3879 case ']':
3880 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3881 s->diff_context++;
3882 s->matched_line = 0;
3883 diff_view_indicate_progress(view);
3884 err = create_diff(s);
3886 break;
3887 case '<':
3888 case ',':
3889 if (s->log_view == NULL)
3890 break;
3891 ls = &s->log_view->state.log;
3892 old_selected_entry = ls->selected_entry;
3894 err = input_log_view(NULL, s->log_view, KEY_UP);
3895 if (err)
3896 break;
3898 if (old_selected_entry == ls->selected_entry)
3899 break;
3901 err = set_selected_commit(s, ls->selected_entry);
3902 if (err)
3903 break;
3905 s->first_displayed_line = 1;
3906 s->last_displayed_line = view->nlines;
3907 s->matched_line = 0;
3909 diff_view_indicate_progress(view);
3910 err = create_diff(s);
3911 break;
3912 case '>':
3913 case '.':
3914 if (s->log_view == NULL)
3915 break;
3916 ls = &s->log_view->state.log;
3917 old_selected_entry = ls->selected_entry;
3919 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3920 if (err)
3921 break;
3923 if (old_selected_entry == ls->selected_entry)
3924 break;
3926 err = set_selected_commit(s, ls->selected_entry);
3927 if (err)
3928 break;
3930 s->first_displayed_line = 1;
3931 s->last_displayed_line = view->nlines;
3932 s->matched_line = 0;
3934 diff_view_indicate_progress(view);
3935 err = create_diff(s);
3936 break;
3937 default:
3938 break;
3941 return err;
3944 static const struct got_error *
3945 cmd_diff(int argc, char *argv[])
3947 const struct got_error *error = NULL;
3948 struct got_repository *repo = NULL;
3949 struct got_worktree *worktree = NULL;
3950 struct got_object_id *id1 = NULL, *id2 = NULL;
3951 char *repo_path = NULL, *cwd = NULL;
3952 char *id_str1 = NULL, *id_str2 = NULL;
3953 char *label1 = NULL, *label2 = NULL;
3954 int diff_context = 3, ignore_whitespace = 0;
3955 int ch, force_text_diff = 0;
3956 const char *errstr;
3957 struct tog_view *view;
3958 int *pack_fds = NULL;
3960 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3961 switch (ch) {
3962 case 'a':
3963 force_text_diff = 1;
3964 break;
3965 case 'C':
3966 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3967 &errstr);
3968 if (errstr != NULL)
3969 errx(1, "number of context lines is %s: %s",
3970 errstr, errstr);
3971 break;
3972 case 'r':
3973 repo_path = realpath(optarg, NULL);
3974 if (repo_path == NULL)
3975 return got_error_from_errno2("realpath",
3976 optarg);
3977 got_path_strip_trailing_slashes(repo_path);
3978 break;
3979 case 'w':
3980 ignore_whitespace = 1;
3981 break;
3982 default:
3983 usage_diff();
3984 /* NOTREACHED */
3988 argc -= optind;
3989 argv += optind;
3991 if (argc == 0) {
3992 usage_diff(); /* TODO show local worktree changes */
3993 } else if (argc == 2) {
3994 id_str1 = argv[0];
3995 id_str2 = argv[1];
3996 } else
3997 usage_diff();
3999 error = got_repo_pack_fds_open(&pack_fds);
4000 if (error)
4001 goto done;
4003 if (repo_path == NULL) {
4004 cwd = getcwd(NULL, 0);
4005 if (cwd == NULL)
4006 return got_error_from_errno("getcwd");
4007 error = got_worktree_open(&worktree, cwd);
4008 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4009 goto done;
4010 if (worktree)
4011 repo_path =
4012 strdup(got_worktree_get_repo_path(worktree));
4013 else
4014 repo_path = strdup(cwd);
4015 if (repo_path == NULL) {
4016 error = got_error_from_errno("strdup");
4017 goto done;
4021 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4022 if (error)
4023 goto done;
4025 init_curses();
4027 error = apply_unveil(got_repo_get_path(repo), NULL);
4028 if (error)
4029 goto done;
4031 error = tog_load_refs(repo, 0);
4032 if (error)
4033 goto done;
4035 error = got_repo_match_object_id(&id1, &label1, id_str1,
4036 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4037 if (error)
4038 goto done;
4040 error = got_repo_match_object_id(&id2, &label2, id_str2,
4041 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4042 if (error)
4043 goto done;
4045 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4046 if (view == NULL) {
4047 error = got_error_from_errno("view_open");
4048 goto done;
4050 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4051 ignore_whitespace, force_text_diff, NULL, repo);
4052 if (error)
4053 goto done;
4054 error = view_loop(view);
4055 done:
4056 free(label1);
4057 free(label2);
4058 free(repo_path);
4059 free(cwd);
4060 if (repo) {
4061 const struct got_error *close_err = got_repo_close(repo);
4062 if (error == NULL)
4063 error = close_err;
4065 if (worktree)
4066 got_worktree_close(worktree);
4067 if (pack_fds) {
4068 const struct got_error *pack_err =
4069 got_repo_pack_fds_close(pack_fds);
4070 if (error == NULL)
4071 error = pack_err;
4072 pack_fds = NULL;
4074 tog_free_refs();
4075 return error;
4078 __dead static void
4079 usage_blame(void)
4081 endwin();
4082 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4083 getprogname());
4084 exit(1);
4087 struct tog_blame_line {
4088 int annotated;
4089 struct got_object_id *id;
4092 static const struct got_error *
4093 draw_blame(struct tog_view *view)
4095 struct tog_blame_view_state *s = &view->state.blame;
4096 struct tog_blame *blame = &s->blame;
4097 regmatch_t *regmatch = &view->regmatch;
4098 const struct got_error *err;
4099 int lineno = 0, nprinted = 0;
4100 char *line = NULL;
4101 size_t linesize = 0;
4102 ssize_t linelen;
4103 wchar_t *wline;
4104 int width;
4105 struct tog_blame_line *blame_line;
4106 struct got_object_id *prev_id = NULL;
4107 char *id_str;
4108 struct tog_color *tc;
4110 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4111 if (err)
4112 return err;
4114 rewind(blame->f);
4115 werase(view->window);
4117 if (asprintf(&line, "commit %s", id_str) == -1) {
4118 err = got_error_from_errno("asprintf");
4119 free(id_str);
4120 return err;
4123 err = format_line(&wline, &width, line, view->ncols, 0);
4124 free(line);
4125 line = NULL;
4126 if (err)
4127 return err;
4128 if (view_needs_focus_indication(view))
4129 wstandout(view->window);
4130 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4131 if (tc)
4132 wattr_on(view->window,
4133 COLOR_PAIR(tc->colorpair), NULL);
4134 waddwstr(view->window, wline);
4135 if (tc)
4136 wattr_off(view->window,
4137 COLOR_PAIR(tc->colorpair), NULL);
4138 if (view_needs_focus_indication(view))
4139 wstandend(view->window);
4140 free(wline);
4141 wline = NULL;
4142 if (width < view->ncols - 1)
4143 waddch(view->window, '\n');
4145 if (asprintf(&line, "[%d/%d] %s%s",
4146 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4147 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4148 free(id_str);
4149 return got_error_from_errno("asprintf");
4151 free(id_str);
4152 err = format_line(&wline, &width, line, view->ncols, 0);
4153 free(line);
4154 line = NULL;
4155 if (err)
4156 return err;
4157 waddwstr(view->window, wline);
4158 free(wline);
4159 wline = NULL;
4160 if (width < view->ncols - 1)
4161 waddch(view->window, '\n');
4163 s->eof = 0;
4164 while (nprinted < view->nlines - 2) {
4165 linelen = getline(&line, &linesize, blame->f);
4166 if (linelen == -1) {
4167 if (feof(blame->f)) {
4168 s->eof = 1;
4169 break;
4171 free(line);
4172 return got_ferror(blame->f, GOT_ERR_IO);
4174 if (++lineno < s->first_displayed_line)
4175 continue;
4177 if (view->focussed && nprinted == s->selected_line - 1)
4178 wstandout(view->window);
4180 if (blame->nlines > 0) {
4181 blame_line = &blame->lines[lineno - 1];
4182 if (blame_line->annotated && prev_id &&
4183 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4184 !(view->focussed &&
4185 nprinted == s->selected_line - 1)) {
4186 waddstr(view->window, " ");
4187 } else if (blame_line->annotated) {
4188 char *id_str;
4189 err = got_object_id_str(&id_str, blame_line->id);
4190 if (err) {
4191 free(line);
4192 return err;
4194 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4195 if (tc)
4196 wattr_on(view->window,
4197 COLOR_PAIR(tc->colorpair), NULL);
4198 wprintw(view->window, "%.8s", id_str);
4199 if (tc)
4200 wattr_off(view->window,
4201 COLOR_PAIR(tc->colorpair), NULL);
4202 free(id_str);
4203 prev_id = blame_line->id;
4204 } else {
4205 waddstr(view->window, "........");
4206 prev_id = NULL;
4208 } else {
4209 waddstr(view->window, "........");
4210 prev_id = NULL;
4213 if (view->focussed && nprinted == s->selected_line - 1)
4214 wstandend(view->window);
4215 waddstr(view->window, " ");
4217 if (view->ncols <= 9) {
4218 width = 9;
4219 wline = wcsdup(L"");
4220 if (wline == NULL) {
4221 err = got_error_from_errno("wcsdup");
4222 free(line);
4223 return err;
4225 } else if (s->first_displayed_line + nprinted ==
4226 s->matched_line &&
4227 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4228 err = add_matched_line(&width, line, view->ncols - 9, 9,
4229 view->window, regmatch);
4230 if (err) {
4231 free(line);
4232 return err;
4234 width += 9;
4235 } else {
4236 err = format_line(&wline, &width, line,
4237 view->ncols - 9, 9);
4238 waddwstr(view->window, wline);
4239 free(wline);
4240 wline = NULL;
4241 width += 9;
4244 if (width <= view->ncols - 1)
4245 waddch(view->window, '\n');
4246 if (++nprinted == 1)
4247 s->first_displayed_line = lineno;
4249 free(line);
4250 s->last_displayed_line = lineno;
4252 view_vborder(view);
4254 return NULL;
4257 static const struct got_error *
4258 blame_cb(void *arg, int nlines, int lineno,
4259 struct got_commit_object *commit, struct got_object_id *id)
4261 const struct got_error *err = NULL;
4262 struct tog_blame_cb_args *a = arg;
4263 struct tog_blame_line *line;
4264 int errcode;
4266 if (nlines != a->nlines ||
4267 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4268 return got_error(GOT_ERR_RANGE);
4270 errcode = pthread_mutex_lock(&tog_mutex);
4271 if (errcode)
4272 return got_error_set_errno(errcode, "pthread_mutex_lock");
4274 if (*a->quit) { /* user has quit the blame view */
4275 err = got_error(GOT_ERR_ITER_COMPLETED);
4276 goto done;
4279 if (lineno == -1)
4280 goto done; /* no change in this commit */
4282 line = &a->lines[lineno - 1];
4283 if (line->annotated)
4284 goto done;
4286 line->id = got_object_id_dup(id);
4287 if (line->id == NULL) {
4288 err = got_error_from_errno("got_object_id_dup");
4289 goto done;
4291 line->annotated = 1;
4292 done:
4293 errcode = pthread_mutex_unlock(&tog_mutex);
4294 if (errcode)
4295 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4296 return err;
4299 static void *
4300 blame_thread(void *arg)
4302 const struct got_error *err, *close_err;
4303 struct tog_blame_thread_args *ta = arg;
4304 struct tog_blame_cb_args *a = ta->cb_args;
4305 int errcode;
4307 err = block_signals_used_by_main_thread();
4308 if (err)
4309 return (void *)err;
4311 err = got_blame(ta->path, a->commit_id, ta->repo,
4312 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4313 if (err && err->code == GOT_ERR_CANCELLED)
4314 err = NULL;
4316 errcode = pthread_mutex_lock(&tog_mutex);
4317 if (errcode)
4318 return (void *)got_error_set_errno(errcode,
4319 "pthread_mutex_lock");
4321 close_err = got_repo_close(ta->repo);
4322 if (err == NULL)
4323 err = close_err;
4324 ta->repo = NULL;
4325 *ta->complete = 1;
4327 errcode = pthread_mutex_unlock(&tog_mutex);
4328 if (errcode && err == NULL)
4329 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4331 return (void *)err;
4334 static struct got_object_id *
4335 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4336 int first_displayed_line, int selected_line)
4338 struct tog_blame_line *line;
4340 if (nlines <= 0)
4341 return NULL;
4343 line = &lines[first_displayed_line - 1 + selected_line - 1];
4344 if (!line->annotated)
4345 return NULL;
4347 return line->id;
4350 static const struct got_error *
4351 stop_blame(struct tog_blame *blame)
4353 const struct got_error *err = NULL;
4354 int i;
4356 if (blame->thread) {
4357 int errcode;
4358 errcode = pthread_mutex_unlock(&tog_mutex);
4359 if (errcode)
4360 return got_error_set_errno(errcode,
4361 "pthread_mutex_unlock");
4362 errcode = pthread_join(blame->thread, (void **)&err);
4363 if (errcode)
4364 return got_error_set_errno(errcode, "pthread_join");
4365 errcode = pthread_mutex_lock(&tog_mutex);
4366 if (errcode)
4367 return got_error_set_errno(errcode,
4368 "pthread_mutex_lock");
4369 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4370 err = NULL;
4371 blame->thread = NULL;
4373 if (blame->thread_args.repo) {
4374 const struct got_error *close_err;
4375 close_err = got_repo_close(blame->thread_args.repo);
4376 if (err == NULL)
4377 err = close_err;
4378 blame->thread_args.repo = NULL;
4380 if (blame->f) {
4381 if (fclose(blame->f) == EOF && err == NULL)
4382 err = got_error_from_errno("fclose");
4383 blame->f = NULL;
4385 if (blame->lines) {
4386 for (i = 0; i < blame->nlines; i++)
4387 free(blame->lines[i].id);
4388 free(blame->lines);
4389 blame->lines = NULL;
4391 free(blame->cb_args.commit_id);
4392 blame->cb_args.commit_id = NULL;
4393 if (blame->pack_fds) {
4394 const struct got_error *pack_err =
4395 got_repo_pack_fds_close(blame->pack_fds);
4396 if (err == NULL)
4397 err = pack_err;
4398 blame->pack_fds = NULL;
4400 return err;
4403 static const struct got_error *
4404 cancel_blame_view(void *arg)
4406 const struct got_error *err = NULL;
4407 int *done = arg;
4408 int errcode;
4410 errcode = pthread_mutex_lock(&tog_mutex);
4411 if (errcode)
4412 return got_error_set_errno(errcode,
4413 "pthread_mutex_unlock");
4415 if (*done)
4416 err = got_error(GOT_ERR_CANCELLED);
4418 errcode = pthread_mutex_unlock(&tog_mutex);
4419 if (errcode)
4420 return got_error_set_errno(errcode,
4421 "pthread_mutex_lock");
4423 return err;
4426 static const struct got_error *
4427 run_blame(struct tog_view *view)
4429 struct tog_blame_view_state *s = &view->state.blame;
4430 struct tog_blame *blame = &s->blame;
4431 const struct got_error *err = NULL;
4432 struct got_commit_object *commit = NULL;
4433 struct got_blob_object *blob = NULL;
4434 struct got_repository *thread_repo = NULL;
4435 struct got_object_id *obj_id = NULL;
4436 int obj_type;
4437 int *pack_fds = NULL;
4439 err = got_object_open_as_commit(&commit, s->repo,
4440 &s->blamed_commit->id);
4441 if (err)
4442 return err;
4444 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4445 if (err)
4446 goto done;
4448 err = got_object_get_type(&obj_type, s->repo, obj_id);
4449 if (err)
4450 goto done;
4452 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4453 err = got_error(GOT_ERR_OBJ_TYPE);
4454 goto done;
4457 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4458 if (err)
4459 goto done;
4460 blame->f = got_opentemp();
4461 if (blame->f == NULL) {
4462 err = got_error_from_errno("got_opentemp");
4463 goto done;
4465 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4466 &blame->line_offsets, blame->f, blob);
4467 if (err)
4468 goto done;
4469 if (blame->nlines == 0) {
4470 s->blame_complete = 1;
4471 goto done;
4474 /* Don't include \n at EOF in the blame line count. */
4475 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4476 blame->nlines--;
4478 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4479 if (blame->lines == NULL) {
4480 err = got_error_from_errno("calloc");
4481 goto done;
4484 err = got_repo_pack_fds_open(&pack_fds);
4485 if (err)
4486 goto done;
4487 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4488 pack_fds);
4489 if (err)
4490 goto done;
4492 blame->pack_fds = pack_fds;
4493 blame->cb_args.view = view;
4494 blame->cb_args.lines = blame->lines;
4495 blame->cb_args.nlines = blame->nlines;
4496 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4497 if (blame->cb_args.commit_id == NULL) {
4498 err = got_error_from_errno("got_object_id_dup");
4499 goto done;
4501 blame->cb_args.quit = &s->done;
4503 blame->thread_args.path = s->path;
4504 blame->thread_args.repo = thread_repo;
4505 blame->thread_args.cb_args = &blame->cb_args;
4506 blame->thread_args.complete = &s->blame_complete;
4507 blame->thread_args.cancel_cb = cancel_blame_view;
4508 blame->thread_args.cancel_arg = &s->done;
4509 s->blame_complete = 0;
4511 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4512 s->first_displayed_line = 1;
4513 s->last_displayed_line = view->nlines;
4514 s->selected_line = 1;
4516 s->matched_line = 0;
4518 done:
4519 if (commit)
4520 got_object_commit_close(commit);
4521 if (blob)
4522 got_object_blob_close(blob);
4523 free(obj_id);
4524 if (err)
4525 stop_blame(blame);
4526 return err;
4529 static const struct got_error *
4530 open_blame_view(struct tog_view *view, char *path,
4531 struct got_object_id *commit_id, struct got_repository *repo)
4533 const struct got_error *err = NULL;
4534 struct tog_blame_view_state *s = &view->state.blame;
4536 STAILQ_INIT(&s->blamed_commits);
4538 s->path = strdup(path);
4539 if (s->path == NULL)
4540 return got_error_from_errno("strdup");
4542 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4543 if (err) {
4544 free(s->path);
4545 return err;
4548 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4549 s->first_displayed_line = 1;
4550 s->last_displayed_line = view->nlines;
4551 s->selected_line = 1;
4552 s->blame_complete = 0;
4553 s->repo = repo;
4554 s->commit_id = commit_id;
4555 memset(&s->blame, 0, sizeof(s->blame));
4557 STAILQ_INIT(&s->colors);
4558 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4559 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4560 get_color_value("TOG_COLOR_COMMIT"));
4561 if (err)
4562 return err;
4565 view->show = show_blame_view;
4566 view->input = input_blame_view;
4567 view->close = close_blame_view;
4568 view->search_start = search_start_blame_view;
4569 view->search_next = search_next_blame_view;
4571 return run_blame(view);
4574 static const struct got_error *
4575 close_blame_view(struct tog_view *view)
4577 const struct got_error *err = NULL;
4578 struct tog_blame_view_state *s = &view->state.blame;
4580 if (s->blame.thread)
4581 err = stop_blame(&s->blame);
4583 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4584 struct got_object_qid *blamed_commit;
4585 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4586 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4587 got_object_qid_free(blamed_commit);
4590 free(s->path);
4591 free_colors(&s->colors);
4592 return err;
4595 static const struct got_error *
4596 search_start_blame_view(struct tog_view *view)
4598 struct tog_blame_view_state *s = &view->state.blame;
4600 s->matched_line = 0;
4601 return NULL;
4604 static const struct got_error *
4605 search_next_blame_view(struct tog_view *view)
4607 struct tog_blame_view_state *s = &view->state.blame;
4608 int lineno;
4609 char *line = NULL;
4610 size_t linesize = 0;
4611 ssize_t linelen;
4613 if (!view->searching) {
4614 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4615 return NULL;
4618 if (s->matched_line) {
4619 if (view->searching == TOG_SEARCH_FORWARD)
4620 lineno = s->matched_line + 1;
4621 else
4622 lineno = s->matched_line - 1;
4623 } else
4624 lineno = s->first_displayed_line - 1 + s->selected_line;
4626 while (1) {
4627 off_t offset;
4629 if (lineno <= 0 || lineno > s->blame.nlines) {
4630 if (s->matched_line == 0) {
4631 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4632 break;
4635 if (view->searching == TOG_SEARCH_FORWARD)
4636 lineno = 1;
4637 else
4638 lineno = s->blame.nlines;
4641 offset = s->blame.line_offsets[lineno - 1];
4642 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4643 free(line);
4644 return got_error_from_errno("fseeko");
4646 linelen = getline(&line, &linesize, s->blame.f);
4647 if (linelen != -1 &&
4648 match_line(line, &view->regex, 1, &view->regmatch)) {
4649 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4650 s->matched_line = lineno;
4651 break;
4653 if (view->searching == TOG_SEARCH_FORWARD)
4654 lineno++;
4655 else
4656 lineno--;
4658 free(line);
4660 if (s->matched_line) {
4661 s->first_displayed_line = s->matched_line;
4662 s->selected_line = 1;
4665 return NULL;
4668 static const struct got_error *
4669 show_blame_view(struct tog_view *view)
4671 const struct got_error *err = NULL;
4672 struct tog_blame_view_state *s = &view->state.blame;
4673 int errcode;
4675 if (s->blame.thread == NULL && !s->blame_complete) {
4676 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4677 &s->blame.thread_args);
4678 if (errcode)
4679 return got_error_set_errno(errcode, "pthread_create");
4681 halfdelay(1); /* fast refresh while annotating */
4684 if (s->blame_complete)
4685 halfdelay(10); /* disable fast refresh */
4687 err = draw_blame(view);
4689 view_vborder(view);
4690 return err;
4693 static const struct got_error *
4694 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4696 const struct got_error *err = NULL, *thread_err = NULL;
4697 struct tog_view *diff_view;
4698 struct tog_blame_view_state *s = &view->state.blame;
4699 int begin_x = 0, nscroll = view->nlines - 2;
4701 switch (ch) {
4702 case 'q':
4703 s->done = 1;
4704 break;
4705 case 'g':
4706 case KEY_HOME:
4707 s->selected_line = 1;
4708 s->first_displayed_line = 1;
4709 break;
4710 case 'G':
4711 case KEY_END:
4712 if (s->blame.nlines < view->nlines - 2) {
4713 s->selected_line = s->blame.nlines;
4714 s->first_displayed_line = 1;
4715 } else {
4716 s->selected_line = view->nlines - 2;
4717 s->first_displayed_line = s->blame.nlines -
4718 (view->nlines - 3);
4720 break;
4721 case 'k':
4722 case KEY_UP:
4723 case CTRL('p'):
4724 if (s->selected_line > 1)
4725 s->selected_line--;
4726 else if (s->selected_line == 1 &&
4727 s->first_displayed_line > 1)
4728 s->first_displayed_line--;
4729 break;
4730 case CTRL('u'):
4731 case 'u':
4732 nscroll /= 2;
4733 /* FALL THROUGH */
4734 case KEY_PPAGE:
4735 case CTRL('b'):
4736 if (s->first_displayed_line == 1) {
4737 s->selected_line = MAX(1, s->selected_line - nscroll);
4738 break;
4740 if (s->first_displayed_line > nscroll)
4741 s->first_displayed_line -= nscroll;
4742 else
4743 s->first_displayed_line = 1;
4744 break;
4745 case 'j':
4746 case KEY_DOWN:
4747 case CTRL('n'):
4748 if (s->selected_line < view->nlines - 2 &&
4749 s->first_displayed_line +
4750 s->selected_line <= s->blame.nlines)
4751 s->selected_line++;
4752 else if (s->last_displayed_line <
4753 s->blame.nlines)
4754 s->first_displayed_line++;
4755 break;
4756 case 'b':
4757 case 'p': {
4758 struct got_object_id *id = NULL;
4759 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4760 s->first_displayed_line, s->selected_line);
4761 if (id == NULL)
4762 break;
4763 if (ch == 'p') {
4764 struct got_commit_object *commit, *pcommit;
4765 struct got_object_qid *pid;
4766 struct got_object_id *blob_id = NULL;
4767 int obj_type;
4768 err = got_object_open_as_commit(&commit,
4769 s->repo, id);
4770 if (err)
4771 break;
4772 pid = STAILQ_FIRST(
4773 got_object_commit_get_parent_ids(commit));
4774 if (pid == NULL) {
4775 got_object_commit_close(commit);
4776 break;
4778 /* Check if path history ends here. */
4779 err = got_object_open_as_commit(&pcommit,
4780 s->repo, &pid->id);
4781 if (err)
4782 break;
4783 err = got_object_id_by_path(&blob_id, s->repo,
4784 pcommit, s->path);
4785 got_object_commit_close(pcommit);
4786 if (err) {
4787 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4788 err = NULL;
4789 got_object_commit_close(commit);
4790 break;
4792 err = got_object_get_type(&obj_type, s->repo,
4793 blob_id);
4794 free(blob_id);
4795 /* Can't blame non-blob type objects. */
4796 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4797 got_object_commit_close(commit);
4798 break;
4800 err = got_object_qid_alloc(&s->blamed_commit,
4801 &pid->id);
4802 got_object_commit_close(commit);
4803 } else {
4804 if (got_object_id_cmp(id,
4805 &s->blamed_commit->id) == 0)
4806 break;
4807 err = got_object_qid_alloc(&s->blamed_commit,
4808 id);
4810 if (err)
4811 break;
4812 s->done = 1;
4813 thread_err = stop_blame(&s->blame);
4814 s->done = 0;
4815 if (thread_err)
4816 break;
4817 STAILQ_INSERT_HEAD(&s->blamed_commits,
4818 s->blamed_commit, entry);
4819 err = run_blame(view);
4820 if (err)
4821 break;
4822 break;
4824 case 'B': {
4825 struct got_object_qid *first;
4826 first = STAILQ_FIRST(&s->blamed_commits);
4827 if (!got_object_id_cmp(&first->id, s->commit_id))
4828 break;
4829 s->done = 1;
4830 thread_err = stop_blame(&s->blame);
4831 s->done = 0;
4832 if (thread_err)
4833 break;
4834 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4835 got_object_qid_free(s->blamed_commit);
4836 s->blamed_commit =
4837 STAILQ_FIRST(&s->blamed_commits);
4838 err = run_blame(view);
4839 if (err)
4840 break;
4841 break;
4843 case KEY_ENTER:
4844 case '\r': {
4845 struct got_object_id *id = NULL;
4846 struct got_object_qid *pid;
4847 struct got_commit_object *commit = NULL;
4848 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4849 s->first_displayed_line, s->selected_line);
4850 if (id == NULL)
4851 break;
4852 err = got_object_open_as_commit(&commit, s->repo, id);
4853 if (err)
4854 break;
4855 pid = STAILQ_FIRST(
4856 got_object_commit_get_parent_ids(commit));
4857 if (view_is_parent_view(view))
4858 begin_x = view_split_begin_x(view->begin_x);
4859 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4860 if (diff_view == NULL) {
4861 got_object_commit_close(commit);
4862 err = got_error_from_errno("view_open");
4863 break;
4865 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4866 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4867 got_object_commit_close(commit);
4868 if (err) {
4869 view_close(diff_view);
4870 break;
4872 view->focussed = 0;
4873 diff_view->focussed = 1;
4874 if (view_is_parent_view(view)) {
4875 err = view_close_child(view);
4876 if (err)
4877 break;
4878 view_set_child(view, diff_view);
4879 view->focus_child = 1;
4880 } else
4881 *new_view = diff_view;
4882 if (err)
4883 break;
4884 break;
4886 case CTRL('d'):
4887 case 'd':
4888 nscroll /= 2;
4889 /* FALL THROUGH */
4890 case KEY_NPAGE:
4891 case CTRL('f'):
4892 case ' ':
4893 if (s->last_displayed_line >= s->blame.nlines &&
4894 s->selected_line >= MIN(s->blame.nlines,
4895 view->nlines - 2)) {
4896 break;
4898 if (s->last_displayed_line >= s->blame.nlines &&
4899 s->selected_line < view->nlines - 2) {
4900 s->selected_line +=
4901 MIN(nscroll, s->last_displayed_line -
4902 s->first_displayed_line - s->selected_line + 1);
4904 if (s->last_displayed_line + nscroll <= s->blame.nlines)
4905 s->first_displayed_line += nscroll;
4906 else
4907 s->first_displayed_line =
4908 s->blame.nlines - (view->nlines - 3);
4909 break;
4910 case KEY_RESIZE:
4911 if (s->selected_line > view->nlines - 2) {
4912 s->selected_line = MIN(s->blame.nlines,
4913 view->nlines - 2);
4915 break;
4916 default:
4917 break;
4919 return thread_err ? thread_err : err;
4922 static const struct got_error *
4923 cmd_blame(int argc, char *argv[])
4925 const struct got_error *error;
4926 struct got_repository *repo = NULL;
4927 struct got_worktree *worktree = NULL;
4928 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4929 char *link_target = NULL;
4930 struct got_object_id *commit_id = NULL;
4931 struct got_commit_object *commit = NULL;
4932 char *commit_id_str = NULL;
4933 int ch;
4934 struct tog_view *view;
4935 int *pack_fds = NULL;
4937 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4938 switch (ch) {
4939 case 'c':
4940 commit_id_str = optarg;
4941 break;
4942 case 'r':
4943 repo_path = realpath(optarg, NULL);
4944 if (repo_path == NULL)
4945 return got_error_from_errno2("realpath",
4946 optarg);
4947 break;
4948 default:
4949 usage_blame();
4950 /* NOTREACHED */
4954 argc -= optind;
4955 argv += optind;
4957 if (argc != 1)
4958 usage_blame();
4960 error = got_repo_pack_fds_open(&pack_fds);
4961 if (error != NULL)
4962 goto done;
4964 if (repo_path == NULL) {
4965 cwd = getcwd(NULL, 0);
4966 if (cwd == NULL)
4967 return got_error_from_errno("getcwd");
4968 error = got_worktree_open(&worktree, cwd);
4969 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4970 goto done;
4971 if (worktree)
4972 repo_path =
4973 strdup(got_worktree_get_repo_path(worktree));
4974 else
4975 repo_path = strdup(cwd);
4976 if (repo_path == NULL) {
4977 error = got_error_from_errno("strdup");
4978 goto done;
4982 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4983 if (error != NULL)
4984 goto done;
4986 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4987 worktree);
4988 if (error)
4989 goto done;
4991 init_curses();
4993 error = apply_unveil(got_repo_get_path(repo), NULL);
4994 if (error)
4995 goto done;
4997 error = tog_load_refs(repo, 0);
4998 if (error)
4999 goto done;
5001 if (commit_id_str == NULL) {
5002 struct got_reference *head_ref;
5003 error = got_ref_open(&head_ref, repo, worktree ?
5004 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5005 if (error != NULL)
5006 goto done;
5007 error = got_ref_resolve(&commit_id, repo, head_ref);
5008 got_ref_close(head_ref);
5009 } else {
5010 error = got_repo_match_object_id(&commit_id, NULL,
5011 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5013 if (error != NULL)
5014 goto done;
5016 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5017 if (view == NULL) {
5018 error = got_error_from_errno("view_open");
5019 goto done;
5022 error = got_object_open_as_commit(&commit, repo, commit_id);
5023 if (error)
5024 goto done;
5026 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5027 commit, repo);
5028 if (error)
5029 goto done;
5031 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5032 commit_id, repo);
5033 if (error)
5034 goto done;
5035 if (worktree) {
5036 /* Release work tree lock. */
5037 got_worktree_close(worktree);
5038 worktree = NULL;
5040 error = view_loop(view);
5041 done:
5042 free(repo_path);
5043 free(in_repo_path);
5044 free(link_target);
5045 free(cwd);
5046 free(commit_id);
5047 if (commit)
5048 got_object_commit_close(commit);
5049 if (worktree)
5050 got_worktree_close(worktree);
5051 if (repo) {
5052 const struct got_error *close_err = got_repo_close(repo);
5053 if (error == NULL)
5054 error = close_err;
5056 if (pack_fds) {
5057 const struct got_error *pack_err =
5058 got_repo_pack_fds_close(pack_fds);
5059 if (error == NULL)
5060 error = pack_err;
5061 pack_fds = NULL;
5063 tog_free_refs();
5064 return error;
5067 static const struct got_error *
5068 draw_tree_entries(struct tog_view *view, const char *parent_path)
5070 struct tog_tree_view_state *s = &view->state.tree;
5071 const struct got_error *err = NULL;
5072 struct got_tree_entry *te;
5073 wchar_t *wline;
5074 struct tog_color *tc;
5075 int width, n, i, nentries;
5076 int limit = view->nlines;
5078 s->ndisplayed = 0;
5080 werase(view->window);
5082 if (limit == 0)
5083 return NULL;
5085 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5086 if (err)
5087 return err;
5088 if (view_needs_focus_indication(view))
5089 wstandout(view->window);
5090 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5091 if (tc)
5092 wattr_on(view->window,
5093 COLOR_PAIR(tc->colorpair), NULL);
5094 waddwstr(view->window, wline);
5095 if (tc)
5096 wattr_off(view->window,
5097 COLOR_PAIR(tc->colorpair), NULL);
5098 if (view_needs_focus_indication(view))
5099 wstandend(view->window);
5100 free(wline);
5101 wline = NULL;
5102 if (width < view->ncols - 1)
5103 waddch(view->window, '\n');
5104 if (--limit <= 0)
5105 return NULL;
5106 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5107 if (err)
5108 return err;
5109 waddwstr(view->window, wline);
5110 free(wline);
5111 wline = NULL;
5112 if (width < view->ncols - 1)
5113 waddch(view->window, '\n');
5114 if (--limit <= 0)
5115 return NULL;
5116 waddch(view->window, '\n');
5117 if (--limit <= 0)
5118 return NULL;
5120 if (s->first_displayed_entry == NULL) {
5121 te = got_object_tree_get_first_entry(s->tree);
5122 if (s->selected == 0) {
5123 if (view->focussed)
5124 wstandout(view->window);
5125 s->selected_entry = NULL;
5127 waddstr(view->window, " ..\n"); /* parent directory */
5128 if (s->selected == 0 && view->focussed)
5129 wstandend(view->window);
5130 s->ndisplayed++;
5131 if (--limit <= 0)
5132 return NULL;
5133 n = 1;
5134 } else {
5135 n = 0;
5136 te = s->first_displayed_entry;
5139 nentries = got_object_tree_get_nentries(s->tree);
5140 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5141 char *line = NULL, *id_str = NULL, *link_target = NULL;
5142 const char *modestr = "";
5143 mode_t mode;
5145 te = got_object_tree_get_entry(s->tree, i);
5146 mode = got_tree_entry_get_mode(te);
5148 if (s->show_ids) {
5149 err = got_object_id_str(&id_str,
5150 got_tree_entry_get_id(te));
5151 if (err)
5152 return got_error_from_errno(
5153 "got_object_id_str");
5155 if (got_object_tree_entry_is_submodule(te))
5156 modestr = "$";
5157 else if (S_ISLNK(mode)) {
5158 int i;
5160 err = got_tree_entry_get_symlink_target(&link_target,
5161 te, s->repo);
5162 if (err) {
5163 free(id_str);
5164 return err;
5166 for (i = 0; i < strlen(link_target); i++) {
5167 if (!isprint((unsigned char)link_target[i]))
5168 link_target[i] = '?';
5170 modestr = "@";
5172 else if (S_ISDIR(mode))
5173 modestr = "/";
5174 else if (mode & S_IXUSR)
5175 modestr = "*";
5176 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5177 got_tree_entry_get_name(te), modestr,
5178 link_target ? " -> ": "",
5179 link_target ? link_target : "") == -1) {
5180 free(id_str);
5181 free(link_target);
5182 return got_error_from_errno("asprintf");
5184 free(id_str);
5185 free(link_target);
5186 err = format_line(&wline, &width, line, view->ncols, 0);
5187 if (err) {
5188 free(line);
5189 break;
5191 if (n == s->selected) {
5192 if (view->focussed)
5193 wstandout(view->window);
5194 s->selected_entry = te;
5196 tc = match_color(&s->colors, line);
5197 if (tc)
5198 wattr_on(view->window,
5199 COLOR_PAIR(tc->colorpair), NULL);
5200 waddwstr(view->window, wline);
5201 if (tc)
5202 wattr_off(view->window,
5203 COLOR_PAIR(tc->colorpair), NULL);
5204 if (width < view->ncols - 1)
5205 waddch(view->window, '\n');
5206 if (n == s->selected && view->focussed)
5207 wstandend(view->window);
5208 free(line);
5209 free(wline);
5210 wline = NULL;
5211 n++;
5212 s->ndisplayed++;
5213 s->last_displayed_entry = te;
5214 if (--limit <= 0)
5215 break;
5218 return err;
5221 static void
5222 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5224 struct got_tree_entry *te;
5225 int isroot = s->tree == s->root;
5226 int i = 0;
5228 if (s->first_displayed_entry == NULL)
5229 return;
5231 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5232 while (i++ < maxscroll) {
5233 if (te == NULL) {
5234 if (!isroot)
5235 s->first_displayed_entry = NULL;
5236 break;
5238 s->first_displayed_entry = te;
5239 te = got_tree_entry_get_prev(s->tree, te);
5243 static void
5244 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5246 struct got_tree_entry *next, *last;
5247 int n = 0;
5249 if (s->first_displayed_entry)
5250 next = got_tree_entry_get_next(s->tree,
5251 s->first_displayed_entry);
5252 else
5253 next = got_object_tree_get_first_entry(s->tree);
5255 last = s->last_displayed_entry;
5256 while (next && last && n++ < maxscroll) {
5257 last = got_tree_entry_get_next(s->tree, last);
5258 if (last) {
5259 s->first_displayed_entry = next;
5260 next = got_tree_entry_get_next(s->tree, next);
5265 static const struct got_error *
5266 tree_entry_path(char **path, struct tog_parent_trees *parents,
5267 struct got_tree_entry *te)
5269 const struct got_error *err = NULL;
5270 struct tog_parent_tree *pt;
5271 size_t len = 2; /* for leading slash and NUL */
5273 TAILQ_FOREACH(pt, parents, entry)
5274 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5275 + 1 /* slash */;
5276 if (te)
5277 len += strlen(got_tree_entry_get_name(te));
5279 *path = calloc(1, len);
5280 if (path == NULL)
5281 return got_error_from_errno("calloc");
5283 (*path)[0] = '/';
5284 pt = TAILQ_LAST(parents, tog_parent_trees);
5285 while (pt) {
5286 const char *name = got_tree_entry_get_name(pt->selected_entry);
5287 if (strlcat(*path, name, len) >= len) {
5288 err = got_error(GOT_ERR_NO_SPACE);
5289 goto done;
5291 if (strlcat(*path, "/", len) >= len) {
5292 err = got_error(GOT_ERR_NO_SPACE);
5293 goto done;
5295 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5297 if (te) {
5298 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5299 err = got_error(GOT_ERR_NO_SPACE);
5300 goto done;
5303 done:
5304 if (err) {
5305 free(*path);
5306 *path = NULL;
5308 return err;
5311 static const struct got_error *
5312 blame_tree_entry(struct tog_view **new_view, int begin_x,
5313 struct got_tree_entry *te, struct tog_parent_trees *parents,
5314 struct got_object_id *commit_id, struct got_repository *repo)
5316 const struct got_error *err = NULL;
5317 char *path;
5318 struct tog_view *blame_view;
5320 *new_view = NULL;
5322 err = tree_entry_path(&path, parents, te);
5323 if (err)
5324 return err;
5326 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5327 if (blame_view == NULL) {
5328 err = got_error_from_errno("view_open");
5329 goto done;
5332 err = open_blame_view(blame_view, path, commit_id, repo);
5333 if (err) {
5334 if (err->code == GOT_ERR_CANCELLED)
5335 err = NULL;
5336 view_close(blame_view);
5337 } else
5338 *new_view = blame_view;
5339 done:
5340 free(path);
5341 return err;
5344 static const struct got_error *
5345 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5346 struct tog_tree_view_state *s)
5348 struct tog_view *log_view;
5349 const struct got_error *err = NULL;
5350 char *path;
5352 *new_view = NULL;
5354 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5355 if (log_view == NULL)
5356 return got_error_from_errno("view_open");
5358 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5359 if (err)
5360 return err;
5362 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5363 path, 0);
5364 if (err)
5365 view_close(log_view);
5366 else
5367 *new_view = log_view;
5368 free(path);
5369 return err;
5372 static const struct got_error *
5373 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5374 const char *head_ref_name, struct got_repository *repo)
5376 const struct got_error *err = NULL;
5377 char *commit_id_str = NULL;
5378 struct tog_tree_view_state *s = &view->state.tree;
5379 struct got_commit_object *commit = NULL;
5381 TAILQ_INIT(&s->parents);
5382 STAILQ_INIT(&s->colors);
5384 s->commit_id = got_object_id_dup(commit_id);
5385 if (s->commit_id == NULL)
5386 return got_error_from_errno("got_object_id_dup");
5388 err = got_object_open_as_commit(&commit, repo, commit_id);
5389 if (err)
5390 goto done;
5393 * The root is opened here and will be closed when the view is closed.
5394 * Any visited subtrees and their path-wise parents are opened and
5395 * closed on demand.
5397 err = got_object_open_as_tree(&s->root, repo,
5398 got_object_commit_get_tree_id(commit));
5399 if (err)
5400 goto done;
5401 s->tree = s->root;
5403 err = got_object_id_str(&commit_id_str, commit_id);
5404 if (err != NULL)
5405 goto done;
5407 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5408 err = got_error_from_errno("asprintf");
5409 goto done;
5412 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5413 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5414 if (head_ref_name) {
5415 s->head_ref_name = strdup(head_ref_name);
5416 if (s->head_ref_name == NULL) {
5417 err = got_error_from_errno("strdup");
5418 goto done;
5421 s->repo = repo;
5423 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5424 err = add_color(&s->colors, "\\$$",
5425 TOG_COLOR_TREE_SUBMODULE,
5426 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5427 if (err)
5428 goto done;
5429 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5430 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5431 if (err)
5432 goto done;
5433 err = add_color(&s->colors, "/$",
5434 TOG_COLOR_TREE_DIRECTORY,
5435 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5436 if (err)
5437 goto done;
5439 err = add_color(&s->colors, "\\*$",
5440 TOG_COLOR_TREE_EXECUTABLE,
5441 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5442 if (err)
5443 goto done;
5445 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5446 get_color_value("TOG_COLOR_COMMIT"));
5447 if (err)
5448 goto done;
5451 view->show = show_tree_view;
5452 view->input = input_tree_view;
5453 view->close = close_tree_view;
5454 view->search_start = search_start_tree_view;
5455 view->search_next = search_next_tree_view;
5456 done:
5457 free(commit_id_str);
5458 if (commit)
5459 got_object_commit_close(commit);
5460 if (err)
5461 close_tree_view(view);
5462 return err;
5465 static const struct got_error *
5466 close_tree_view(struct tog_view *view)
5468 struct tog_tree_view_state *s = &view->state.tree;
5470 free_colors(&s->colors);
5471 free(s->tree_label);
5472 s->tree_label = NULL;
5473 free(s->commit_id);
5474 s->commit_id = NULL;
5475 free(s->head_ref_name);
5476 s->head_ref_name = NULL;
5477 while (!TAILQ_EMPTY(&s->parents)) {
5478 struct tog_parent_tree *parent;
5479 parent = TAILQ_FIRST(&s->parents);
5480 TAILQ_REMOVE(&s->parents, parent, entry);
5481 if (parent->tree != s->root)
5482 got_object_tree_close(parent->tree);
5483 free(parent);
5486 if (s->tree != NULL && s->tree != s->root)
5487 got_object_tree_close(s->tree);
5488 if (s->root)
5489 got_object_tree_close(s->root);
5490 return NULL;
5493 static const struct got_error *
5494 search_start_tree_view(struct tog_view *view)
5496 struct tog_tree_view_state *s = &view->state.tree;
5498 s->matched_entry = NULL;
5499 return NULL;
5502 static int
5503 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5505 regmatch_t regmatch;
5507 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5508 0) == 0;
5511 static const struct got_error *
5512 search_next_tree_view(struct tog_view *view)
5514 struct tog_tree_view_state *s = &view->state.tree;
5515 struct got_tree_entry *te = NULL;
5517 if (!view->searching) {
5518 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5519 return NULL;
5522 if (s->matched_entry) {
5523 if (view->searching == TOG_SEARCH_FORWARD) {
5524 if (s->selected_entry)
5525 te = got_tree_entry_get_next(s->tree,
5526 s->selected_entry);
5527 else
5528 te = got_object_tree_get_first_entry(s->tree);
5529 } else {
5530 if (s->selected_entry == NULL)
5531 te = got_object_tree_get_last_entry(s->tree);
5532 else
5533 te = got_tree_entry_get_prev(s->tree,
5534 s->selected_entry);
5536 } else {
5537 if (s->selected_entry)
5538 te = s->selected_entry;
5539 else if (view->searching == TOG_SEARCH_FORWARD)
5540 te = got_object_tree_get_first_entry(s->tree);
5541 else
5542 te = got_object_tree_get_last_entry(s->tree);
5545 while (1) {
5546 if (te == NULL) {
5547 if (s->matched_entry == NULL) {
5548 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5549 return NULL;
5551 if (view->searching == TOG_SEARCH_FORWARD)
5552 te = got_object_tree_get_first_entry(s->tree);
5553 else
5554 te = got_object_tree_get_last_entry(s->tree);
5557 if (match_tree_entry(te, &view->regex)) {
5558 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5559 s->matched_entry = te;
5560 break;
5563 if (view->searching == TOG_SEARCH_FORWARD)
5564 te = got_tree_entry_get_next(s->tree, te);
5565 else
5566 te = got_tree_entry_get_prev(s->tree, te);
5569 if (s->matched_entry) {
5570 s->first_displayed_entry = s->matched_entry;
5571 s->selected = 0;
5574 return NULL;
5577 static const struct got_error *
5578 show_tree_view(struct tog_view *view)
5580 const struct got_error *err = NULL;
5581 struct tog_tree_view_state *s = &view->state.tree;
5582 char *parent_path;
5584 err = tree_entry_path(&parent_path, &s->parents, NULL);
5585 if (err)
5586 return err;
5588 err = draw_tree_entries(view, parent_path);
5589 free(parent_path);
5591 view_vborder(view);
5592 return err;
5595 static const struct got_error *
5596 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5598 const struct got_error *err = NULL;
5599 struct tog_tree_view_state *s = &view->state.tree;
5600 struct tog_view *log_view, *ref_view;
5601 struct got_tree_entry *te;
5602 int begin_x = 0, n, nscroll = view->nlines - 3;
5604 switch (ch) {
5605 case 'i':
5606 s->show_ids = !s->show_ids;
5607 break;
5608 case 'l':
5609 if (!s->selected_entry)
5610 break;
5611 if (view_is_parent_view(view))
5612 begin_x = view_split_begin_x(view->begin_x);
5613 err = log_selected_tree_entry(&log_view, begin_x, s);
5614 view->focussed = 0;
5615 log_view->focussed = 1;
5616 if (view_is_parent_view(view)) {
5617 err = view_close_child(view);
5618 if (err)
5619 return err;
5620 view_set_child(view, log_view);
5621 view->focus_child = 1;
5622 } else
5623 *new_view = log_view;
5624 break;
5625 case 'r':
5626 if (view_is_parent_view(view))
5627 begin_x = view_split_begin_x(view->begin_x);
5628 ref_view = view_open(view->nlines, view->ncols,
5629 view->begin_y, begin_x, TOG_VIEW_REF);
5630 if (ref_view == NULL)
5631 return got_error_from_errno("view_open");
5632 err = open_ref_view(ref_view, s->repo);
5633 if (err) {
5634 view_close(ref_view);
5635 return err;
5637 view->focussed = 0;
5638 ref_view->focussed = 1;
5639 if (view_is_parent_view(view)) {
5640 err = view_close_child(view);
5641 if (err)
5642 return err;
5643 view_set_child(view, ref_view);
5644 view->focus_child = 1;
5645 } else
5646 *new_view = ref_view;
5647 break;
5648 case 'g':
5649 case KEY_HOME:
5650 s->selected = 0;
5651 if (s->tree == s->root)
5652 s->first_displayed_entry =
5653 got_object_tree_get_first_entry(s->tree);
5654 else
5655 s->first_displayed_entry = NULL;
5656 break;
5657 case 'G':
5658 case KEY_END:
5659 s->selected = 0;
5660 te = got_object_tree_get_last_entry(s->tree);
5661 for (n = 0; n < view->nlines - 3; n++) {
5662 if (te == NULL) {
5663 if(s->tree != s->root) {
5664 s->first_displayed_entry = NULL;
5665 n++;
5667 break;
5669 s->first_displayed_entry = te;
5670 te = got_tree_entry_get_prev(s->tree, te);
5672 if (n > 0)
5673 s->selected = n - 1;
5674 break;
5675 case 'k':
5676 case KEY_UP:
5677 case CTRL('p'):
5678 if (s->selected > 0) {
5679 s->selected--;
5680 break;
5682 tree_scroll_up(s, 1);
5683 break;
5684 case CTRL('u'):
5685 case 'u':
5686 nscroll /= 2;
5687 /* FALL THROUGH */
5688 case KEY_PPAGE:
5689 case CTRL('b'):
5690 if (s->tree == s->root) {
5691 if (got_object_tree_get_first_entry(s->tree) ==
5692 s->first_displayed_entry)
5693 s->selected -= MIN(s->selected, nscroll);
5694 } else {
5695 if (s->first_displayed_entry == NULL)
5696 s->selected -= MIN(s->selected, nscroll);
5698 tree_scroll_up(s, MAX(0, nscroll));
5699 break;
5700 case 'j':
5701 case KEY_DOWN:
5702 case CTRL('n'):
5703 if (s->selected < s->ndisplayed - 1) {
5704 s->selected++;
5705 break;
5707 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5708 == NULL)
5709 /* can't scroll any further */
5710 break;
5711 tree_scroll_down(s, 1);
5712 break;
5713 case CTRL('d'):
5714 case 'd':
5715 nscroll /= 2;
5716 /* FALL THROUGH */
5717 case KEY_NPAGE:
5718 case CTRL('f'):
5719 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5720 == NULL) {
5721 /* can't scroll any further; move cursor down */
5722 if (s->selected < s->ndisplayed - 1)
5723 s->selected += MIN(nscroll,
5724 s->ndisplayed - s->selected - 1);
5725 break;
5727 tree_scroll_down(s, nscroll);
5728 break;
5729 case KEY_ENTER:
5730 case '\r':
5731 case KEY_BACKSPACE:
5732 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5733 struct tog_parent_tree *parent;
5734 /* user selected '..' */
5735 if (s->tree == s->root)
5736 break;
5737 parent = TAILQ_FIRST(&s->parents);
5738 TAILQ_REMOVE(&s->parents, parent,
5739 entry);
5740 got_object_tree_close(s->tree);
5741 s->tree = parent->tree;
5742 s->first_displayed_entry =
5743 parent->first_displayed_entry;
5744 s->selected_entry =
5745 parent->selected_entry;
5746 s->selected = parent->selected;
5747 free(parent);
5748 } else if (S_ISDIR(got_tree_entry_get_mode(
5749 s->selected_entry))) {
5750 struct got_tree_object *subtree;
5751 err = got_object_open_as_tree(&subtree, s->repo,
5752 got_tree_entry_get_id(s->selected_entry));
5753 if (err)
5754 break;
5755 err = tree_view_visit_subtree(s, subtree);
5756 if (err) {
5757 got_object_tree_close(subtree);
5758 break;
5760 } else if (S_ISREG(got_tree_entry_get_mode(
5761 s->selected_entry))) {
5762 struct tog_view *blame_view;
5763 int begin_x = view_is_parent_view(view) ?
5764 view_split_begin_x(view->begin_x) : 0;
5766 err = blame_tree_entry(&blame_view, begin_x,
5767 s->selected_entry, &s->parents,
5768 s->commit_id, s->repo);
5769 if (err)
5770 break;
5771 view->focussed = 0;
5772 blame_view->focussed = 1;
5773 if (view_is_parent_view(view)) {
5774 err = view_close_child(view);
5775 if (err)
5776 return err;
5777 view_set_child(view, blame_view);
5778 view->focus_child = 1;
5779 } else
5780 *new_view = blame_view;
5782 break;
5783 case KEY_RESIZE:
5784 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5785 s->selected = view->nlines - 4;
5786 break;
5787 default:
5788 break;
5791 return err;
5794 __dead static void
5795 usage_tree(void)
5797 endwin();
5798 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5799 getprogname());
5800 exit(1);
5803 static const struct got_error *
5804 cmd_tree(int argc, char *argv[])
5806 const struct got_error *error;
5807 struct got_repository *repo = NULL;
5808 struct got_worktree *worktree = NULL;
5809 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5810 struct got_object_id *commit_id = NULL;
5811 struct got_commit_object *commit = NULL;
5812 const char *commit_id_arg = NULL;
5813 char *label = NULL;
5814 struct got_reference *ref = NULL;
5815 const char *head_ref_name = NULL;
5816 int ch;
5817 struct tog_view *view;
5818 int *pack_fds = NULL;
5820 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5821 switch (ch) {
5822 case 'c':
5823 commit_id_arg = optarg;
5824 break;
5825 case 'r':
5826 repo_path = realpath(optarg, NULL);
5827 if (repo_path == NULL)
5828 return got_error_from_errno2("realpath",
5829 optarg);
5830 break;
5831 default:
5832 usage_tree();
5833 /* NOTREACHED */
5837 argc -= optind;
5838 argv += optind;
5840 if (argc > 1)
5841 usage_tree();
5843 error = got_repo_pack_fds_open(&pack_fds);
5844 if (error != NULL)
5845 goto done;
5847 if (repo_path == NULL) {
5848 cwd = getcwd(NULL, 0);
5849 if (cwd == NULL)
5850 return got_error_from_errno("getcwd");
5851 error = got_worktree_open(&worktree, cwd);
5852 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5853 goto done;
5854 if (worktree)
5855 repo_path =
5856 strdup(got_worktree_get_repo_path(worktree));
5857 else
5858 repo_path = strdup(cwd);
5859 if (repo_path == NULL) {
5860 error = got_error_from_errno("strdup");
5861 goto done;
5865 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5866 if (error != NULL)
5867 goto done;
5869 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5870 repo, worktree);
5871 if (error)
5872 goto done;
5874 init_curses();
5876 error = apply_unveil(got_repo_get_path(repo), NULL);
5877 if (error)
5878 goto done;
5880 error = tog_load_refs(repo, 0);
5881 if (error)
5882 goto done;
5884 if (commit_id_arg == NULL) {
5885 error = got_repo_match_object_id(&commit_id, &label,
5886 worktree ? got_worktree_get_head_ref_name(worktree) :
5887 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5888 if (error)
5889 goto done;
5890 head_ref_name = label;
5891 } else {
5892 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5893 if (error == NULL)
5894 head_ref_name = got_ref_get_name(ref);
5895 else if (error->code != GOT_ERR_NOT_REF)
5896 goto done;
5897 error = got_repo_match_object_id(&commit_id, NULL,
5898 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5899 if (error)
5900 goto done;
5903 error = got_object_open_as_commit(&commit, repo, commit_id);
5904 if (error)
5905 goto done;
5907 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5908 if (view == NULL) {
5909 error = got_error_from_errno("view_open");
5910 goto done;
5912 error = open_tree_view(view, commit_id, head_ref_name, repo);
5913 if (error)
5914 goto done;
5915 if (!got_path_is_root_dir(in_repo_path)) {
5916 error = tree_view_walk_path(&view->state.tree, commit,
5917 in_repo_path);
5918 if (error)
5919 goto done;
5922 if (worktree) {
5923 /* Release work tree lock. */
5924 got_worktree_close(worktree);
5925 worktree = NULL;
5927 error = view_loop(view);
5928 done:
5929 free(repo_path);
5930 free(cwd);
5931 free(commit_id);
5932 free(label);
5933 if (ref)
5934 got_ref_close(ref);
5935 if (repo) {
5936 const struct got_error *close_err = got_repo_close(repo);
5937 if (error == NULL)
5938 error = close_err;
5940 if (pack_fds) {
5941 const struct got_error *pack_err =
5942 got_repo_pack_fds_close(pack_fds);
5943 if (error == NULL)
5944 error = pack_err;
5945 pack_fds = NULL;
5947 tog_free_refs();
5948 return error;
5951 static const struct got_error *
5952 ref_view_load_refs(struct tog_ref_view_state *s)
5954 struct got_reflist_entry *sre;
5955 struct tog_reflist_entry *re;
5957 s->nrefs = 0;
5958 TAILQ_FOREACH(sre, &tog_refs, entry) {
5959 if (strncmp(got_ref_get_name(sre->ref),
5960 "refs/got/", 9) == 0 &&
5961 strncmp(got_ref_get_name(sre->ref),
5962 "refs/got/backup/", 16) != 0)
5963 continue;
5965 re = malloc(sizeof(*re));
5966 if (re == NULL)
5967 return got_error_from_errno("malloc");
5969 re->ref = got_ref_dup(sre->ref);
5970 if (re->ref == NULL)
5971 return got_error_from_errno("got_ref_dup");
5972 re->idx = s->nrefs++;
5973 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5976 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5977 return NULL;
5980 void
5981 ref_view_free_refs(struct tog_ref_view_state *s)
5983 struct tog_reflist_entry *re;
5985 while (!TAILQ_EMPTY(&s->refs)) {
5986 re = TAILQ_FIRST(&s->refs);
5987 TAILQ_REMOVE(&s->refs, re, entry);
5988 got_ref_close(re->ref);
5989 free(re);
5993 static const struct got_error *
5994 open_ref_view(struct tog_view *view, struct got_repository *repo)
5996 const struct got_error *err = NULL;
5997 struct tog_ref_view_state *s = &view->state.ref;
5999 s->selected_entry = 0;
6000 s->repo = repo;
6002 TAILQ_INIT(&s->refs);
6003 STAILQ_INIT(&s->colors);
6005 err = ref_view_load_refs(s);
6006 if (err)
6007 return err;
6009 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6010 err = add_color(&s->colors, "^refs/heads/",
6011 TOG_COLOR_REFS_HEADS,
6012 get_color_value("TOG_COLOR_REFS_HEADS"));
6013 if (err)
6014 goto done;
6016 err = add_color(&s->colors, "^refs/tags/",
6017 TOG_COLOR_REFS_TAGS,
6018 get_color_value("TOG_COLOR_REFS_TAGS"));
6019 if (err)
6020 goto done;
6022 err = add_color(&s->colors, "^refs/remotes/",
6023 TOG_COLOR_REFS_REMOTES,
6024 get_color_value("TOG_COLOR_REFS_REMOTES"));
6025 if (err)
6026 goto done;
6028 err = add_color(&s->colors, "^refs/got/backup/",
6029 TOG_COLOR_REFS_BACKUP,
6030 get_color_value("TOG_COLOR_REFS_BACKUP"));
6031 if (err)
6032 goto done;
6035 view->show = show_ref_view;
6036 view->input = input_ref_view;
6037 view->close = close_ref_view;
6038 view->search_start = search_start_ref_view;
6039 view->search_next = search_next_ref_view;
6040 done:
6041 if (err)
6042 free_colors(&s->colors);
6043 return err;
6046 static const struct got_error *
6047 close_ref_view(struct tog_view *view)
6049 struct tog_ref_view_state *s = &view->state.ref;
6051 ref_view_free_refs(s);
6052 free_colors(&s->colors);
6054 return NULL;
6057 static const struct got_error *
6058 resolve_reflist_entry(struct got_object_id **commit_id,
6059 struct tog_reflist_entry *re, struct got_repository *repo)
6061 const struct got_error *err = NULL;
6062 struct got_object_id *obj_id;
6063 struct got_tag_object *tag = NULL;
6064 int obj_type;
6066 *commit_id = NULL;
6068 err = got_ref_resolve(&obj_id, repo, re->ref);
6069 if (err)
6070 return err;
6072 err = got_object_get_type(&obj_type, repo, obj_id);
6073 if (err)
6074 goto done;
6076 switch (obj_type) {
6077 case GOT_OBJ_TYPE_COMMIT:
6078 *commit_id = obj_id;
6079 break;
6080 case GOT_OBJ_TYPE_TAG:
6081 err = got_object_open_as_tag(&tag, repo, obj_id);
6082 if (err)
6083 goto done;
6084 free(obj_id);
6085 err = got_object_get_type(&obj_type, repo,
6086 got_object_tag_get_object_id(tag));
6087 if (err)
6088 goto done;
6089 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6090 err = got_error(GOT_ERR_OBJ_TYPE);
6091 goto done;
6093 *commit_id = got_object_id_dup(
6094 got_object_tag_get_object_id(tag));
6095 if (*commit_id == NULL) {
6096 err = got_error_from_errno("got_object_id_dup");
6097 goto done;
6099 break;
6100 default:
6101 err = got_error(GOT_ERR_OBJ_TYPE);
6102 break;
6105 done:
6106 if (tag)
6107 got_object_tag_close(tag);
6108 if (err) {
6109 free(*commit_id);
6110 *commit_id = NULL;
6112 return err;
6115 static const struct got_error *
6116 log_ref_entry(struct tog_view **new_view, int begin_x,
6117 struct tog_reflist_entry *re, struct got_repository *repo)
6119 struct tog_view *log_view;
6120 const struct got_error *err = NULL;
6121 struct got_object_id *commit_id = NULL;
6123 *new_view = NULL;
6125 err = resolve_reflist_entry(&commit_id, re, repo);
6126 if (err) {
6127 if (err->code != GOT_ERR_OBJ_TYPE)
6128 return err;
6129 else
6130 return NULL;
6133 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6134 if (log_view == NULL) {
6135 err = got_error_from_errno("view_open");
6136 goto done;
6139 err = open_log_view(log_view, commit_id, repo,
6140 got_ref_get_name(re->ref), "", 0);
6141 done:
6142 if (err)
6143 view_close(log_view);
6144 else
6145 *new_view = log_view;
6146 free(commit_id);
6147 return err;
6150 static void
6151 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6153 struct tog_reflist_entry *re;
6154 int i = 0;
6156 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6157 return;
6159 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6160 while (i++ < maxscroll) {
6161 if (re == NULL)
6162 break;
6163 s->first_displayed_entry = re;
6164 re = TAILQ_PREV(re, tog_reflist_head, entry);
6168 static void
6169 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6171 struct tog_reflist_entry *next, *last;
6172 int n = 0;
6174 if (s->first_displayed_entry)
6175 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6176 else
6177 next = TAILQ_FIRST(&s->refs);
6179 last = s->last_displayed_entry;
6180 while (next && last && n++ < maxscroll) {
6181 last = TAILQ_NEXT(last, entry);
6182 if (last) {
6183 s->first_displayed_entry = next;
6184 next = TAILQ_NEXT(next, entry);
6189 static const struct got_error *
6190 search_start_ref_view(struct tog_view *view)
6192 struct tog_ref_view_state *s = &view->state.ref;
6194 s->matched_entry = NULL;
6195 return NULL;
6198 static int
6199 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6201 regmatch_t regmatch;
6203 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6204 0) == 0;
6207 static const struct got_error *
6208 search_next_ref_view(struct tog_view *view)
6210 struct tog_ref_view_state *s = &view->state.ref;
6211 struct tog_reflist_entry *re = NULL;
6213 if (!view->searching) {
6214 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6215 return NULL;
6218 if (s->matched_entry) {
6219 if (view->searching == TOG_SEARCH_FORWARD) {
6220 if (s->selected_entry)
6221 re = TAILQ_NEXT(s->selected_entry, entry);
6222 else
6223 re = TAILQ_PREV(s->selected_entry,
6224 tog_reflist_head, entry);
6225 } else {
6226 if (s->selected_entry == NULL)
6227 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6228 else
6229 re = TAILQ_PREV(s->selected_entry,
6230 tog_reflist_head, entry);
6232 } else {
6233 if (s->selected_entry)
6234 re = s->selected_entry;
6235 else if (view->searching == TOG_SEARCH_FORWARD)
6236 re = TAILQ_FIRST(&s->refs);
6237 else
6238 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6241 while (1) {
6242 if (re == NULL) {
6243 if (s->matched_entry == NULL) {
6244 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6245 return NULL;
6247 if (view->searching == TOG_SEARCH_FORWARD)
6248 re = TAILQ_FIRST(&s->refs);
6249 else
6250 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6253 if (match_reflist_entry(re, &view->regex)) {
6254 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6255 s->matched_entry = re;
6256 break;
6259 if (view->searching == TOG_SEARCH_FORWARD)
6260 re = TAILQ_NEXT(re, entry);
6261 else
6262 re = TAILQ_PREV(re, tog_reflist_head, entry);
6265 if (s->matched_entry) {
6266 s->first_displayed_entry = s->matched_entry;
6267 s->selected = 0;
6270 return NULL;
6273 static const struct got_error *
6274 show_ref_view(struct tog_view *view)
6276 const struct got_error *err = NULL;
6277 struct tog_ref_view_state *s = &view->state.ref;
6278 struct tog_reflist_entry *re;
6279 char *line = NULL;
6280 wchar_t *wline;
6281 struct tog_color *tc;
6282 int width, n;
6283 int limit = view->nlines;
6285 werase(view->window);
6287 s->ndisplayed = 0;
6289 if (limit == 0)
6290 return NULL;
6292 re = s->first_displayed_entry;
6294 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6295 s->nrefs) == -1)
6296 return got_error_from_errno("asprintf");
6298 err = format_line(&wline, &width, line, view->ncols, 0);
6299 if (err) {
6300 free(line);
6301 return err;
6303 if (view_needs_focus_indication(view))
6304 wstandout(view->window);
6305 waddwstr(view->window, wline);
6306 if (view_needs_focus_indication(view))
6307 wstandend(view->window);
6308 free(wline);
6309 wline = NULL;
6310 free(line);
6311 line = NULL;
6312 if (width < view->ncols - 1)
6313 waddch(view->window, '\n');
6314 if (--limit <= 0)
6315 return NULL;
6317 n = 0;
6318 while (re && limit > 0) {
6319 char *line = NULL;
6321 if (got_ref_is_symbolic(re->ref)) {
6322 if (asprintf(&line, "%s -> %s",
6323 got_ref_get_name(re->ref),
6324 got_ref_get_symref_target(re->ref)) == -1)
6325 return got_error_from_errno("asprintf");
6326 } else if (s->show_ids) {
6327 struct got_object_id *id;
6328 char *id_str;
6329 err = got_ref_resolve(&id, s->repo, re->ref);
6330 if (err)
6331 return err;
6332 err = got_object_id_str(&id_str, id);
6333 if (err) {
6334 free(id);
6335 return err;
6337 if (asprintf(&line, "%s: %s",
6338 got_ref_get_name(re->ref), id_str) == -1) {
6339 err = got_error_from_errno("asprintf");
6340 free(id);
6341 free(id_str);
6342 return err;
6344 free(id);
6345 free(id_str);
6346 } else {
6347 line = strdup(got_ref_get_name(re->ref));
6348 if (line == NULL)
6349 return got_error_from_errno("strdup");
6352 err = format_line(&wline, &width, line, view->ncols, 0);
6353 if (err) {
6354 free(line);
6355 return err;
6357 if (n == s->selected) {
6358 if (view->focussed)
6359 wstandout(view->window);
6360 s->selected_entry = re;
6362 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6363 if (tc)
6364 wattr_on(view->window,
6365 COLOR_PAIR(tc->colorpair), NULL);
6366 waddwstr(view->window, wline);
6367 if (tc)
6368 wattr_off(view->window,
6369 COLOR_PAIR(tc->colorpair), NULL);
6370 if (width < view->ncols - 1)
6371 waddch(view->window, '\n');
6372 if (n == s->selected && view->focussed)
6373 wstandend(view->window);
6374 free(line);
6375 free(wline);
6376 wline = NULL;
6377 n++;
6378 s->ndisplayed++;
6379 s->last_displayed_entry = re;
6381 limit--;
6382 re = TAILQ_NEXT(re, entry);
6385 view_vborder(view);
6386 return err;
6389 static const struct got_error *
6390 browse_ref_tree(struct tog_view **new_view, int begin_x,
6391 struct tog_reflist_entry *re, struct got_repository *repo)
6393 const struct got_error *err = NULL;
6394 struct got_object_id *commit_id = NULL;
6395 struct tog_view *tree_view;
6397 *new_view = NULL;
6399 err = resolve_reflist_entry(&commit_id, re, repo);
6400 if (err) {
6401 if (err->code != GOT_ERR_OBJ_TYPE)
6402 return err;
6403 else
6404 return NULL;
6408 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6409 if (tree_view == NULL) {
6410 err = got_error_from_errno("view_open");
6411 goto done;
6414 err = open_tree_view(tree_view, commit_id,
6415 got_ref_get_name(re->ref), repo);
6416 if (err)
6417 goto done;
6419 *new_view = tree_view;
6420 done:
6421 free(commit_id);
6422 return err;
6424 static const struct got_error *
6425 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6427 const struct got_error *err = NULL;
6428 struct tog_ref_view_state *s = &view->state.ref;
6429 struct tog_view *log_view, *tree_view;
6430 struct tog_reflist_entry *re;
6431 int begin_x = 0, n, nscroll = view->nlines - 1;
6433 switch (ch) {
6434 case 'i':
6435 s->show_ids = !s->show_ids;
6436 break;
6437 case 'o':
6438 s->sort_by_date = !s->sort_by_date;
6439 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6440 got_ref_cmp_by_commit_timestamp_descending :
6441 tog_ref_cmp_by_name, s->repo);
6442 if (err)
6443 break;
6444 got_reflist_object_id_map_free(tog_refs_idmap);
6445 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6446 &tog_refs, s->repo);
6447 if (err)
6448 break;
6449 ref_view_free_refs(s);
6450 err = ref_view_load_refs(s);
6451 break;
6452 case KEY_ENTER:
6453 case '\r':
6454 if (!s->selected_entry)
6455 break;
6456 if (view_is_parent_view(view))
6457 begin_x = view_split_begin_x(view->begin_x);
6458 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6459 s->repo);
6460 view->focussed = 0;
6461 log_view->focussed = 1;
6462 if (view_is_parent_view(view)) {
6463 err = view_close_child(view);
6464 if (err)
6465 return err;
6466 view_set_child(view, log_view);
6467 view->focus_child = 1;
6468 } else
6469 *new_view = log_view;
6470 break;
6471 case 't':
6472 if (!s->selected_entry)
6473 break;
6474 if (view_is_parent_view(view))
6475 begin_x = view_split_begin_x(view->begin_x);
6476 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6477 s->repo);
6478 if (err || tree_view == NULL)
6479 break;
6480 view->focussed = 0;
6481 tree_view->focussed = 1;
6482 if (view_is_parent_view(view)) {
6483 err = view_close_child(view);
6484 if (err)
6485 return err;
6486 view_set_child(view, tree_view);
6487 view->focus_child = 1;
6488 } else
6489 *new_view = tree_view;
6490 break;
6491 case 'g':
6492 case KEY_HOME:
6493 s->selected = 0;
6494 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6495 break;
6496 case 'G':
6497 case KEY_END:
6498 s->selected = 0;
6499 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6500 for (n = 0; n < view->nlines - 1; n++) {
6501 if (re == NULL)
6502 break;
6503 s->first_displayed_entry = re;
6504 re = TAILQ_PREV(re, tog_reflist_head, entry);
6506 if (n > 0)
6507 s->selected = n - 1;
6508 break;
6509 case 'k':
6510 case KEY_UP:
6511 case CTRL('p'):
6512 if (s->selected > 0) {
6513 s->selected--;
6514 break;
6516 ref_scroll_up(s, 1);
6517 break;
6518 case CTRL('u'):
6519 case 'u':
6520 nscroll /= 2;
6521 /* FALL THROUGH */
6522 case KEY_PPAGE:
6523 case CTRL('b'):
6524 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6525 s->selected -= MIN(nscroll, s->selected);
6526 ref_scroll_up(s, MAX(0, nscroll));
6527 break;
6528 case 'j':
6529 case KEY_DOWN:
6530 case CTRL('n'):
6531 if (s->selected < s->ndisplayed - 1) {
6532 s->selected++;
6533 break;
6535 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6536 /* can't scroll any further */
6537 break;
6538 ref_scroll_down(s, 1);
6539 break;
6540 case CTRL('d'):
6541 case 'd':
6542 nscroll /= 2;
6543 /* FALL THROUGH */
6544 case KEY_NPAGE:
6545 case CTRL('f'):
6546 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6547 /* can't scroll any further; move cursor down */
6548 if (s->selected < s->ndisplayed - 1)
6549 s->selected += MIN(nscroll,
6550 s->ndisplayed - s->selected - 1);
6551 break;
6553 ref_scroll_down(s, nscroll);
6554 break;
6555 case CTRL('l'):
6556 tog_free_refs();
6557 err = tog_load_refs(s->repo, s->sort_by_date);
6558 if (err)
6559 break;
6560 ref_view_free_refs(s);
6561 err = ref_view_load_refs(s);
6562 break;
6563 case KEY_RESIZE:
6564 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6565 s->selected = view->nlines - 2;
6566 break;
6567 default:
6568 break;
6571 return err;
6574 __dead static void
6575 usage_ref(void)
6577 endwin();
6578 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6579 getprogname());
6580 exit(1);
6583 static const struct got_error *
6584 cmd_ref(int argc, char *argv[])
6586 const struct got_error *error;
6587 struct got_repository *repo = NULL;
6588 struct got_worktree *worktree = NULL;
6589 char *cwd = NULL, *repo_path = NULL;
6590 int ch;
6591 struct tog_view *view;
6592 int *pack_fds = NULL;
6594 while ((ch = getopt(argc, argv, "r:")) != -1) {
6595 switch (ch) {
6596 case 'r':
6597 repo_path = realpath(optarg, NULL);
6598 if (repo_path == NULL)
6599 return got_error_from_errno2("realpath",
6600 optarg);
6601 break;
6602 default:
6603 usage_ref();
6604 /* NOTREACHED */
6608 argc -= optind;
6609 argv += optind;
6611 if (argc > 1)
6612 usage_ref();
6614 error = got_repo_pack_fds_open(&pack_fds);
6615 if (error != NULL)
6616 goto done;
6618 if (repo_path == NULL) {
6619 cwd = getcwd(NULL, 0);
6620 if (cwd == NULL)
6621 return got_error_from_errno("getcwd");
6622 error = got_worktree_open(&worktree, cwd);
6623 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6624 goto done;
6625 if (worktree)
6626 repo_path =
6627 strdup(got_worktree_get_repo_path(worktree));
6628 else
6629 repo_path = strdup(cwd);
6630 if (repo_path == NULL) {
6631 error = got_error_from_errno("strdup");
6632 goto done;
6636 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6637 if (error != NULL)
6638 goto done;
6640 init_curses();
6642 error = apply_unveil(got_repo_get_path(repo), NULL);
6643 if (error)
6644 goto done;
6646 error = tog_load_refs(repo, 0);
6647 if (error)
6648 goto done;
6650 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6651 if (view == NULL) {
6652 error = got_error_from_errno("view_open");
6653 goto done;
6656 error = open_ref_view(view, repo);
6657 if (error)
6658 goto done;
6660 if (worktree) {
6661 /* Release work tree lock. */
6662 got_worktree_close(worktree);
6663 worktree = NULL;
6665 error = view_loop(view);
6666 done:
6667 free(repo_path);
6668 free(cwd);
6669 if (repo) {
6670 const struct got_error *close_err = got_repo_close(repo);
6671 if (close_err)
6672 error = close_err;
6674 if (pack_fds) {
6675 const struct got_error *pack_err =
6676 got_repo_pack_fds_close(pack_fds);
6677 if (error == NULL)
6678 error = pack_err;
6679 pack_fds = NULL;
6681 tog_free_refs();
6682 return error;
6685 static void
6686 list_commands(FILE *fp)
6688 size_t i;
6690 fprintf(fp, "commands:");
6691 for (i = 0; i < nitems(tog_commands); i++) {
6692 const struct tog_cmd *cmd = &tog_commands[i];
6693 fprintf(fp, " %s", cmd->name);
6695 fputc('\n', fp);
6698 __dead static void
6699 usage(int hflag, int status)
6701 FILE *fp = (status == 0) ? stdout : stderr;
6703 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6704 getprogname());
6705 if (hflag) {
6706 fprintf(fp, "lazy usage: %s path\n", getprogname());
6707 list_commands(fp);
6709 exit(status);
6712 static char **
6713 make_argv(int argc, ...)
6715 va_list ap;
6716 char **argv;
6717 int i;
6719 va_start(ap, argc);
6721 argv = calloc(argc, sizeof(char *));
6722 if (argv == NULL)
6723 err(1, "calloc");
6724 for (i = 0; i < argc; i++) {
6725 argv[i] = strdup(va_arg(ap, char *));
6726 if (argv[i] == NULL)
6727 err(1, "strdup");
6730 va_end(ap);
6731 return argv;
6735 * Try to convert 'tog path' into a 'tog log path' command.
6736 * The user could simply have mistyped the command rather than knowingly
6737 * provided a path. So check whether argv[0] can in fact be resolved
6738 * to a path in the HEAD commit and print a special error if not.
6739 * This hack is for mpi@ <3
6741 static const struct got_error *
6742 tog_log_with_path(int argc, char *argv[])
6744 const struct got_error *error = NULL, *close_err;
6745 const struct tog_cmd *cmd = NULL;
6746 struct got_repository *repo = NULL;
6747 struct got_worktree *worktree = NULL;
6748 struct got_object_id *commit_id = NULL, *id = NULL;
6749 struct got_commit_object *commit = NULL;
6750 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6751 char *commit_id_str = NULL, **cmd_argv = NULL;
6752 int *pack_fds = NULL;
6754 cwd = getcwd(NULL, 0);
6755 if (cwd == NULL)
6756 return got_error_from_errno("getcwd");
6758 error = got_repo_pack_fds_open(&pack_fds);
6759 if (error != NULL)
6760 goto done;
6762 error = got_worktree_open(&worktree, cwd);
6763 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6764 goto done;
6766 if (worktree)
6767 repo_path = strdup(got_worktree_get_repo_path(worktree));
6768 else
6769 repo_path = strdup(cwd);
6770 if (repo_path == NULL) {
6771 error = got_error_from_errno("strdup");
6772 goto done;
6775 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6776 if (error != NULL)
6777 goto done;
6779 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6780 repo, worktree);
6781 if (error)
6782 goto done;
6784 error = tog_load_refs(repo, 0);
6785 if (error)
6786 goto done;
6787 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6788 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6789 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6790 if (error)
6791 goto done;
6793 if (worktree) {
6794 got_worktree_close(worktree);
6795 worktree = NULL;
6798 error = got_object_open_as_commit(&commit, repo, commit_id);
6799 if (error)
6800 goto done;
6802 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6803 if (error) {
6804 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6805 goto done;
6806 fprintf(stderr, "%s: '%s' is no known command or path\n",
6807 getprogname(), argv[0]);
6808 usage(1, 1);
6809 /* not reached */
6812 close_err = got_repo_close(repo);
6813 if (error == NULL)
6814 error = close_err;
6815 repo = NULL;
6817 error = got_object_id_str(&commit_id_str, commit_id);
6818 if (error)
6819 goto done;
6821 cmd = &tog_commands[0]; /* log */
6822 argc = 4;
6823 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6824 error = cmd->cmd_main(argc, cmd_argv);
6825 done:
6826 if (repo) {
6827 close_err = got_repo_close(repo);
6828 if (error == NULL)
6829 error = close_err;
6831 if (commit)
6832 got_object_commit_close(commit);
6833 if (worktree)
6834 got_worktree_close(worktree);
6835 if (pack_fds) {
6836 const struct got_error *pack_err =
6837 got_repo_pack_fds_close(pack_fds);
6838 if (error == NULL)
6839 error = pack_err;
6840 pack_fds = NULL;
6842 free(id);
6843 free(commit_id_str);
6844 free(commit_id);
6845 free(cwd);
6846 free(repo_path);
6847 free(in_repo_path);
6848 if (cmd_argv) {
6849 int i;
6850 for (i = 0; i < argc; i++)
6851 free(cmd_argv[i]);
6852 free(cmd_argv);
6854 tog_free_refs();
6855 return error;
6858 int
6859 main(int argc, char *argv[])
6861 const struct got_error *error = NULL;
6862 const struct tog_cmd *cmd = NULL;
6863 int ch, hflag = 0, Vflag = 0;
6864 char **cmd_argv = NULL;
6865 static const struct option longopts[] = {
6866 { "version", no_argument, NULL, 'V' },
6867 { NULL, 0, NULL, 0}
6870 setlocale(LC_CTYPE, "");
6872 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6873 switch (ch) {
6874 case 'h':
6875 hflag = 1;
6876 break;
6877 case 'V':
6878 Vflag = 1;
6879 break;
6880 default:
6881 usage(hflag, 1);
6882 /* NOTREACHED */
6886 argc -= optind;
6887 argv += optind;
6888 optind = 1;
6889 optreset = 1;
6891 if (Vflag) {
6892 got_version_print_str();
6893 return 0;
6896 #ifndef PROFILE
6897 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6898 NULL) == -1)
6899 err(1, "pledge");
6900 #endif
6902 if (argc == 0) {
6903 if (hflag)
6904 usage(hflag, 0);
6905 /* Build an argument vector which runs a default command. */
6906 cmd = &tog_commands[0];
6907 argc = 1;
6908 cmd_argv = make_argv(argc, cmd->name);
6909 } else {
6910 size_t i;
6912 /* Did the user specify a command? */
6913 for (i = 0; i < nitems(tog_commands); i++) {
6914 if (strncmp(tog_commands[i].name, argv[0],
6915 strlen(argv[0])) == 0) {
6916 cmd = &tog_commands[i];
6917 break;
6922 if (cmd == NULL) {
6923 if (argc != 1)
6924 usage(0, 1);
6925 /* No command specified; try log with a path */
6926 error = tog_log_with_path(argc, argv);
6927 } else {
6928 if (hflag)
6929 cmd->cmd_usage();
6930 else
6931 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6934 endwin();
6935 putchar('\n');
6936 if (cmd_argv) {
6937 int i;
6938 for (i = 0; i < argc; i++)
6939 free(cmd_argv[i]);
6940 free(cmd_argv);
6943 if (error && error->code != GOT_ERR_CANCELLED)
6944 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6945 return 0;