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/stat.h>
18 #include <sys/ioctl.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #if defined(__FreeBSD__) || defined(__APPLE__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #endif
25 #include <curses.h>
26 #include <panel.h>
27 #include <locale.h>
28 #include <sha1.h>
29 #include <signal.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <getopt.h>
34 #include <string.h>
35 #include <err.h>
36 #include <unistd.h>
37 #include <limits.h>
38 #include <wchar.h>
39 #include <time.h>
40 #include <pthread.h>
41 #include <libgen.h>
42 #include <regex.h>
43 #include <sched.h>
45 #include "got_compat.h"
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
52 #include "got_diff.h"
53 #include "got_opentemp.h"
54 #include "got_utf8.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
59 #include "got_path.h"
60 #include "got_worktree.h"
62 //#define update_panels() (0)
63 //#define doupdate() (0)
65 #ifndef MIN
66 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 #endif
69 #ifndef MAX
70 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 #endif
73 #ifndef CTRL
74 #define CTRL(x) ((x) & 0x1f)
75 #endif
77 #ifndef nitems
78 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
79 #endif
81 struct tog_cmd {
82 const char *name;
83 const struct got_error *(*cmd_main)(int, char *[]);
84 void (*cmd_usage)(void);
85 };
87 __dead static void usage(int, int);
88 __dead static void usage_log(void);
89 __dead static void usage_diff(void);
90 __dead static void usage_blame(void);
91 __dead static void usage_tree(void);
92 __dead static void usage_ref(void);
94 static const struct got_error* cmd_log(int, char *[]);
95 static const struct got_error* cmd_diff(int, char *[]);
96 static const struct got_error* cmd_blame(int, char *[]);
97 static const struct got_error* cmd_tree(int, char *[]);
98 static const struct got_error* cmd_ref(int, char *[]);
100 static const struct tog_cmd tog_commands[] = {
101 { "log", cmd_log, usage_log },
102 { "diff", cmd_diff, usage_diff },
103 { "blame", cmd_blame, usage_blame },
104 { "tree", cmd_tree, usage_tree },
105 { "ref", cmd_ref, usage_ref },
106 };
108 enum tog_view_type {
109 TOG_VIEW_DIFF,
110 TOG_VIEW_LOG,
111 TOG_VIEW_BLAME,
112 TOG_VIEW_TREE,
113 TOG_VIEW_REF,
114 };
116 #define TOG_EOF_STRING "(END)"
118 struct commit_queue_entry {
119 TAILQ_ENTRY(commit_queue_entry) entry;
120 struct got_object_id *id;
121 struct got_commit_object *commit;
122 int idx;
123 };
124 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 struct commit_queue {
126 int ncommits;
127 struct commit_queue_head head;
128 };
130 struct tog_color {
131 STAILQ_ENTRY(tog_color) entry;
132 regex_t regex;
133 short colorpair;
134 };
135 STAILQ_HEAD(tog_colors, tog_color);
137 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 static struct got_reflist_object_id_map *tog_refs_idmap;
140 static const struct got_error *
141 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
142 struct got_reference* re2)
144 const char *name1 = got_ref_get_name(re1);
145 const char *name2 = got_ref_get_name(re2);
146 int isbackup1, isbackup2;
148 /* Sort backup refs towards the bottom of the list. */
149 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
150 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
151 if (!isbackup1 && isbackup2) {
152 *cmp = -1;
153 return NULL;
154 } else if (isbackup1 && !isbackup2) {
155 *cmp = 1;
156 return NULL;
159 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
160 return NULL;
163 static const struct got_error *
164 tog_load_refs(struct got_repository *repo, int sort_by_date)
166 const struct got_error *err;
168 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
169 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
170 repo);
171 if (err)
172 return err;
174 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
175 repo);
178 static void
179 tog_free_refs(void)
181 if (tog_refs_idmap) {
182 got_reflist_object_id_map_free(tog_refs_idmap);
183 tog_refs_idmap = NULL;
185 got_ref_list_free(&tog_refs);
188 static const struct got_error *
189 add_color(struct tog_colors *colors, const char *pattern,
190 int idx, short color)
192 const struct got_error *err = NULL;
193 struct tog_color *tc;
194 int regerr = 0;
196 if (idx < 1 || idx > COLOR_PAIRS - 1)
197 return NULL;
199 init_pair(idx, color, -1);
201 tc = calloc(1, sizeof(*tc));
202 if (tc == NULL)
203 return got_error_from_errno("calloc");
204 regerr = regcomp(&tc->regex, pattern,
205 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
206 if (regerr) {
207 static char regerr_msg[512];
208 static char err_msg[512];
209 regerror(regerr, &tc->regex, regerr_msg,
210 sizeof(regerr_msg));
211 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
212 regerr_msg);
213 err = got_error_msg(GOT_ERR_REGEX, err_msg);
214 free(tc);
215 return err;
217 tc->colorpair = idx;
218 STAILQ_INSERT_HEAD(colors, tc, entry);
219 return NULL;
222 static void
223 free_colors(struct tog_colors *colors)
225 struct tog_color *tc;
227 while (!STAILQ_EMPTY(colors)) {
228 tc = STAILQ_FIRST(colors);
229 STAILQ_REMOVE_HEAD(colors, entry);
230 regfree(&tc->regex);
231 free(tc);
235 struct tog_color *
236 get_color(struct tog_colors *colors, int colorpair)
238 struct tog_color *tc = NULL;
240 STAILQ_FOREACH(tc, colors, entry) {
241 if (tc->colorpair == colorpair)
242 return tc;
245 return NULL;
248 static int
249 default_color_value(const char *envvar)
251 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
254 return COLOR_CYAN;
255 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
256 return COLOR_YELLOW;
257 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
258 return COLOR_GREEN;
259 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
260 return COLOR_MAGENTA;
261 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
262 return COLOR_MAGENTA;
263 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
264 return COLOR_CYAN;
265 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
266 return COLOR_GREEN;
267 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
268 return COLOR_GREEN;
269 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
270 return COLOR_CYAN;
271 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
272 return COLOR_YELLOW;
273 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
274 return COLOR_GREEN;
275 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
276 return COLOR_MAGENTA;
277 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
278 return COLOR_YELLOW;
279 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
280 return COLOR_CYAN;
282 return -1;
285 static int
286 get_color_value(const char *envvar)
288 const char *val = getenv(envvar);
290 if (val == NULL)
291 return default_color_value(envvar);
293 if (strcasecmp(val, "black") == 0)
294 return COLOR_BLACK;
295 if (strcasecmp(val, "red") == 0)
296 return COLOR_RED;
297 if (strcasecmp(val, "green") == 0)
298 return COLOR_GREEN;
299 if (strcasecmp(val, "yellow") == 0)
300 return COLOR_YELLOW;
301 if (strcasecmp(val, "blue") == 0)
302 return COLOR_BLUE;
303 if (strcasecmp(val, "magenta") == 0)
304 return COLOR_MAGENTA;
305 if (strcasecmp(val, "cyan") == 0)
306 return COLOR_CYAN;
307 if (strcasecmp(val, "white") == 0)
308 return COLOR_WHITE;
309 if (strcasecmp(val, "default") == 0)
310 return -1;
312 return default_color_value(envvar);
316 struct tog_diff_view_state {
317 struct got_object_id *id1, *id2;
318 const char *label1, *label2;
319 FILE *f;
320 int first_displayed_line;
321 int last_displayed_line;
322 int eof;
323 int diff_context;
324 int ignore_whitespace;
325 int force_text_diff;
326 struct got_repository *repo;
327 struct tog_colors colors;
328 size_t nlines;
329 off_t *line_offsets;
330 int matched_line;
331 int selected_line;
333 /* passed from log view; may be NULL */
334 struct tog_view *log_view;
335 };
337 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
339 struct tog_log_thread_args {
340 pthread_cond_t need_commits;
341 pthread_cond_t commit_loaded;
342 int commits_needed;
343 int load_all;
344 struct got_commit_graph *graph;
345 struct commit_queue *commits;
346 const char *in_repo_path;
347 struct got_object_id *start_id;
348 struct got_repository *repo;
349 int log_complete;
350 sig_atomic_t *quit;
351 struct commit_queue_entry **first_displayed_entry;
352 struct commit_queue_entry **selected_entry;
353 int *searching;
354 int *search_next_done;
355 regex_t *regex;
356 };
358 struct tog_log_view_state {
359 struct commit_queue commits;
360 struct commit_queue_entry *first_displayed_entry;
361 struct commit_queue_entry *last_displayed_entry;
362 struct commit_queue_entry *selected_entry;
363 int selected;
364 char *in_repo_path;
365 char *head_ref_name;
366 int log_branches;
367 struct got_repository *repo;
368 struct got_object_id *start_id;
369 sig_atomic_t quit;
370 pthread_t thread;
371 struct tog_log_thread_args thread_args;
372 struct commit_queue_entry *matched_entry;
373 struct commit_queue_entry *search_entry;
374 struct tog_colors colors;
375 };
377 #define TOG_COLOR_DIFF_MINUS 1
378 #define TOG_COLOR_DIFF_PLUS 2
379 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
380 #define TOG_COLOR_DIFF_META 4
381 #define TOG_COLOR_TREE_SUBMODULE 5
382 #define TOG_COLOR_TREE_SYMLINK 6
383 #define TOG_COLOR_TREE_DIRECTORY 7
384 #define TOG_COLOR_TREE_EXECUTABLE 8
385 #define TOG_COLOR_COMMIT 9
386 #define TOG_COLOR_AUTHOR 10
387 #define TOG_COLOR_DATE 11
388 #define TOG_COLOR_REFS_HEADS 12
389 #define TOG_COLOR_REFS_TAGS 13
390 #define TOG_COLOR_REFS_REMOTES 14
391 #define TOG_COLOR_REFS_BACKUP 15
393 struct tog_blame_cb_args {
394 struct tog_blame_line *lines; /* one per line */
395 int nlines;
397 struct tog_view *view;
398 struct got_object_id *commit_id;
399 int *quit;
400 };
402 struct tog_blame_thread_args {
403 const char *path;
404 struct got_repository *repo;
405 struct tog_blame_cb_args *cb_args;
406 int *complete;
407 got_cancel_cb cancel_cb;
408 void *cancel_arg;
409 };
411 struct tog_blame {
412 FILE *f;
413 off_t filesize;
414 struct tog_blame_line *lines;
415 int nlines;
416 off_t *line_offsets;
417 pthread_t thread;
418 struct tog_blame_thread_args thread_args;
419 struct tog_blame_cb_args cb_args;
420 const char *path;
421 };
423 struct tog_blame_view_state {
424 int first_displayed_line;
425 int last_displayed_line;
426 int selected_line;
427 int blame_complete;
428 int eof;
429 int done;
430 struct got_object_id_queue blamed_commits;
431 struct got_object_qid *blamed_commit;
432 char *path;
433 struct got_repository *repo;
434 struct got_object_id *commit_id;
435 struct tog_blame blame;
436 int matched_line;
437 struct tog_colors colors;
438 };
440 struct tog_parent_tree {
441 TAILQ_ENTRY(tog_parent_tree) entry;
442 struct got_tree_object *tree;
443 struct got_tree_entry *first_displayed_entry;
444 struct got_tree_entry *selected_entry;
445 int selected;
446 };
448 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
450 struct tog_tree_view_state {
451 char *tree_label;
452 struct got_object_id *commit_id;/* commit which this tree belongs to */
453 struct got_tree_object *root; /* the commit's root tree entry */
454 struct got_tree_object *tree; /* currently displayed (sub-)tree */
455 struct got_tree_entry *first_displayed_entry;
456 struct got_tree_entry *last_displayed_entry;
457 struct got_tree_entry *selected_entry;
458 int ndisplayed, selected, show_ids;
459 struct tog_parent_trees parents; /* parent trees of current sub-tree */
460 char *head_ref_name;
461 struct got_repository *repo;
462 struct got_tree_entry *matched_entry;
463 struct tog_colors colors;
464 };
466 struct tog_reflist_entry {
467 TAILQ_ENTRY(tog_reflist_entry) entry;
468 struct got_reference *ref;
469 int idx;
470 };
472 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
474 struct tog_ref_view_state {
475 struct tog_reflist_head refs;
476 struct tog_reflist_entry *first_displayed_entry;
477 struct tog_reflist_entry *last_displayed_entry;
478 struct tog_reflist_entry *selected_entry;
479 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
480 struct got_repository *repo;
481 struct tog_reflist_entry *matched_entry;
482 struct tog_colors colors;
483 };
485 /*
486 * We implement two types of views: parent views and child views.
488 * The 'Tab' key switches focus between a parent view and its child view.
489 * Child views are shown side-by-side to their parent view, provided
490 * there is enough screen estate.
492 * When a new view is opened from within a parent view, this new view
493 * becomes a child view of the parent view, replacing any existing child.
495 * When a new view is opened from within a child view, this new view
496 * becomes a parent view which will obscure the views below until the
497 * user quits the new parent view by typing 'q'.
499 * This list of views contains parent views only.
500 * Child views are only pointed to by their parent view.
501 */
502 TAILQ_HEAD(tog_view_list_head, tog_view);
504 struct tog_view {
505 TAILQ_ENTRY(tog_view) entry;
506 WINDOW *window;
507 PANEL *panel;
508 int nlines, ncols, begin_y, begin_x;
509 int lines, cols; /* copies of LINES and COLS */
510 int focussed; /* Only set on one parent or child view at a time. */
511 int dying;
512 struct tog_view *parent;
513 struct tog_view *child;
515 /*
516 * This flag is initially set on parent views when a new child view
517 * is created. It gets toggled when the 'Tab' key switches focus
518 * between parent and child.
519 * The flag indicates whether focus should be passed on to our child
520 * view if this parent view gets picked for focus after another parent
521 * view was closed. This prevents child views from losing focus in such
522 * situations.
523 */
524 int focus_child;
526 /* type-specific state */
527 enum tog_view_type type;
528 union {
529 struct tog_diff_view_state diff;
530 struct tog_log_view_state log;
531 struct tog_blame_view_state blame;
532 struct tog_tree_view_state tree;
533 struct tog_ref_view_state ref;
534 } state;
536 const struct got_error *(*show)(struct tog_view *);
537 const struct got_error *(*input)(struct tog_view **,
538 struct tog_view *, int);
539 const struct got_error *(*close)(struct tog_view *);
541 const struct got_error *(*search_start)(struct tog_view *);
542 const struct got_error *(*search_next)(struct tog_view *);
543 int search_started;
544 int searching;
545 #define TOG_SEARCH_FORWARD 1
546 #define TOG_SEARCH_BACKWARD 2
547 int search_next_done;
548 #define TOG_SEARCH_HAVE_MORE 1
549 #define TOG_SEARCH_NO_MORE 2
550 #define TOG_SEARCH_HAVE_NONE 3
551 regex_t regex;
552 regmatch_t regmatch;
553 };
555 static const struct got_error *open_diff_view(struct tog_view *,
556 struct got_object_id *, struct got_object_id *,
557 const char *, const char *, int, int, int, struct tog_view *,
558 struct got_repository *);
559 static const struct got_error *show_diff_view(struct tog_view *);
560 static const struct got_error *input_diff_view(struct tog_view **,
561 struct tog_view *, int);
562 static const struct got_error* close_diff_view(struct tog_view *);
563 static const struct got_error *search_start_diff_view(struct tog_view *);
564 static const struct got_error *search_next_diff_view(struct tog_view *);
566 static const struct got_error *open_log_view(struct tog_view *,
567 struct got_object_id *, struct got_repository *,
568 const char *, const char *, int);
569 static const struct got_error * show_log_view(struct tog_view *);
570 static const struct got_error *input_log_view(struct tog_view **,
571 struct tog_view *, int);
572 static const struct got_error *close_log_view(struct tog_view *);
573 static const struct got_error *search_start_log_view(struct tog_view *);
574 static const struct got_error *search_next_log_view(struct tog_view *);
576 static const struct got_error *open_blame_view(struct tog_view *, char *,
577 struct got_object_id *, struct got_repository *);
578 static const struct got_error *show_blame_view(struct tog_view *);
579 static const struct got_error *input_blame_view(struct tog_view **,
580 struct tog_view *, int);
581 static const struct got_error *close_blame_view(struct tog_view *);
582 static const struct got_error *search_start_blame_view(struct tog_view *);
583 static const struct got_error *search_next_blame_view(struct tog_view *);
585 static const struct got_error *open_tree_view(struct tog_view *,
586 struct got_object_id *, const char *, struct got_repository *);
587 static const struct got_error *show_tree_view(struct tog_view *);
588 static const struct got_error *input_tree_view(struct tog_view **,
589 struct tog_view *, int);
590 static const struct got_error *close_tree_view(struct tog_view *);
591 static const struct got_error *search_start_tree_view(struct tog_view *);
592 static const struct got_error *search_next_tree_view(struct tog_view *);
594 static const struct got_error *open_ref_view(struct tog_view *,
595 struct got_repository *);
596 static const struct got_error *show_ref_view(struct tog_view *);
597 static const struct got_error *input_ref_view(struct tog_view **,
598 struct tog_view *, int);
599 static const struct got_error *close_ref_view(struct tog_view *);
600 static const struct got_error *search_start_ref_view(struct tog_view *);
601 static const struct got_error *search_next_ref_view(struct tog_view *);
603 static volatile sig_atomic_t tog_sigwinch_received;
604 static volatile sig_atomic_t tog_sigpipe_received;
605 static volatile sig_atomic_t tog_sigcont_received;
607 static void
608 tog_sigwinch(int signo)
610 tog_sigwinch_received = 1;
613 static void
614 tog_sigpipe(int signo)
616 tog_sigpipe_received = 1;
619 static void
620 tog_sigcont(int signo)
622 tog_sigcont_received = 1;
625 static const struct got_error *
626 view_close(struct tog_view *view)
628 const struct got_error *err = NULL;
630 if (view->child) {
631 view_close(view->child);
632 view->child = NULL;
634 if (view->close)
635 err = view->close(view);
636 if (view->panel)
637 del_panel(view->panel);
638 if (view->window)
639 delwin(view->window);
640 free(view);
641 return err;
644 static struct tog_view *
645 view_open(int nlines, int ncols, int begin_y, int begin_x,
646 enum tog_view_type type)
648 struct tog_view *view = calloc(1, sizeof(*view));
650 if (view == NULL)
651 return NULL;
653 view->type = type;
654 view->lines = LINES;
655 view->cols = COLS;
656 view->nlines = nlines ? nlines : LINES - begin_y;
657 view->ncols = ncols ? ncols : COLS - begin_x;
658 view->begin_y = begin_y;
659 view->begin_x = begin_x;
660 view->window = newwin(nlines, ncols, begin_y, begin_x);
661 if (view->window == NULL) {
662 view_close(view);
663 return NULL;
665 view->panel = new_panel(view->window);
666 if (view->panel == NULL ||
667 set_panel_userptr(view->panel, view) != OK) {
668 view_close(view);
669 return NULL;
672 keypad(view->window, TRUE);
673 return view;
676 static int
677 view_split_begin_x(int begin_x)
679 if (begin_x > 0 || COLS < 120)
680 return 0;
681 return (COLS - MAX(COLS / 2, 80));
684 static const struct got_error *view_resize(struct tog_view *);
686 static const struct got_error *
687 view_splitscreen(struct tog_view *view)
689 const struct got_error *err = NULL;
691 view->begin_y = 0;
692 view->begin_x = view_split_begin_x(0);
693 view->nlines = LINES;
694 view->ncols = COLS - view->begin_x;
695 view->lines = LINES;
696 view->cols = COLS;
697 err = view_resize(view);
698 if (err)
699 return err;
701 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
702 return got_error_from_errno("mvwin");
704 return NULL;
707 static const struct got_error *
708 view_fullscreen(struct tog_view *view)
710 const struct got_error *err = NULL;
712 view->begin_x = 0;
713 view->begin_y = 0;
714 view->nlines = LINES;
715 view->ncols = COLS;
716 view->lines = LINES;
717 view->cols = COLS;
718 err = view_resize(view);
719 if (err)
720 return err;
722 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
723 return got_error_from_errno("mvwin");
725 return NULL;
728 static int
729 view_is_parent_view(struct tog_view *view)
731 return view->parent == NULL;
734 static const struct got_error *
735 view_resize(struct tog_view *view)
737 int nlines, ncols;
739 if (view->lines > LINES)
740 nlines = view->nlines - (view->lines - LINES);
741 else
742 nlines = view->nlines + (LINES - view->lines);
744 if (view->cols > COLS)
745 ncols = view->ncols - (view->cols - COLS);
746 else
747 ncols = view->ncols + (COLS - view->cols);
749 if (wresize(view->window, nlines, ncols) == ERR)
750 return got_error_from_errno("wresize");
751 if (replace_panel(view->panel, view->window) == ERR)
752 return got_error_from_errno("replace_panel");
753 wclear(view->window);
755 view->nlines = nlines;
756 view->ncols = ncols;
757 view->lines = LINES;
758 view->cols = COLS;
760 if (view->child) {
761 view->child->begin_x = view_split_begin_x(view->begin_x);
762 if (view->child->begin_x == 0) {
763 view_fullscreen(view->child);
764 if (view->child->focussed)
765 show_panel(view->child->panel);
766 else
767 show_panel(view->panel);
768 } else {
769 view_splitscreen(view->child);
770 show_panel(view->child->panel);
774 return NULL;
777 static const struct got_error *
778 view_close_child(struct tog_view *view)
780 const struct got_error *err = NULL;
782 if (view->child == NULL)
783 return NULL;
785 err = view_close(view->child);
786 view->child = NULL;
787 return err;
790 static void
791 view_set_child(struct tog_view *view, struct tog_view *child)
793 view->child = child;
794 child->parent = view;
797 static int
798 view_is_splitscreen(struct tog_view *view)
800 return view->begin_x > 0;
803 static void
804 tog_resizeterm(void)
806 int cols, lines;
807 struct winsize size;
809 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
810 cols = 80; /* Default */
811 lines = 24;
812 } else {
813 cols = size.ws_col;
814 lines = size.ws_row;
816 resize_term(lines, cols);
819 static const struct got_error *
820 view_search_start(struct tog_view *view)
822 const struct got_error *err = NULL;
823 char pattern[1024];
824 int ret;
826 if (view->search_started) {
827 regfree(&view->regex);
828 view->searching = 0;
829 memset(&view->regmatch, 0, sizeof(view->regmatch));
831 view->search_started = 0;
833 if (view->nlines < 1)
834 return NULL;
836 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
837 wclrtoeol(view->window);
839 nocbreak();
840 echo();
841 ret = wgetnstr(view->window, pattern, sizeof(pattern));
842 cbreak();
843 noecho();
844 if (ret == ERR)
845 return NULL;
847 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
848 err = view->search_start(view);
849 if (err) {
850 regfree(&view->regex);
851 return err;
853 view->search_started = 1;
854 view->searching = TOG_SEARCH_FORWARD;
855 view->search_next_done = 0;
856 view->search_next(view);
859 return NULL;
862 static const struct got_error *
863 view_input(struct tog_view **new, int *done, struct tog_view *view,
864 struct tog_view_list_head *views)
866 const struct got_error *err = NULL;
867 struct tog_view *v;
868 int ch, errcode;
870 *new = NULL;
872 /* Clear "no matches" indicator. */
873 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
874 view->search_next_done == TOG_SEARCH_HAVE_NONE)
875 view->search_next_done = TOG_SEARCH_HAVE_MORE;
877 if (view->searching && !view->search_next_done) {
878 errcode = pthread_mutex_unlock(&tog_mutex);
879 if (errcode)
880 return got_error_set_errno(errcode,
881 "pthread_mutex_unlock");
882 sched_yield();
883 errcode = pthread_mutex_lock(&tog_mutex);
884 if (errcode)
885 return got_error_set_errno(errcode,
886 "pthread_mutex_lock");
887 view->search_next(view);
888 return NULL;
891 nodelay(stdscr, FALSE);
892 /* Allow threads to make progress while we are waiting for input. */
893 errcode = pthread_mutex_unlock(&tog_mutex);
894 if (errcode)
895 return got_error_set_errno(errcode, "pthread_mutex_unlock");
896 ch = wgetch(view->window);
897 errcode = pthread_mutex_lock(&tog_mutex);
898 if (errcode)
899 return got_error_set_errno(errcode, "pthread_mutex_lock");
900 nodelay(stdscr, TRUE);
902 if (tog_sigwinch_received || tog_sigcont_received) {
903 tog_resizeterm();
904 tog_sigwinch_received = 0;
905 tog_sigcont_received = 0;
906 TAILQ_FOREACH(v, views, entry) {
907 err = view_resize(v);
908 if (err)
909 return err;
910 err = v->input(new, v, KEY_RESIZE);
911 if (err)
912 return err;
913 if (v->child) {
914 err = view_resize(v->child);
915 if (err)
916 return err;
917 err = v->child->input(new, v->child,
918 KEY_RESIZE);
919 if (err)
920 return err;
925 switch (ch) {
926 case '\t':
927 if (view->child) {
928 view->focussed = 0;
929 view->child->focussed = 1;
930 view->focus_child = 1;
931 } else if (view->parent) {
932 view->focussed = 0;
933 view->parent->focussed = 1;
934 view->parent->focus_child = 0;
936 break;
937 case 'q':
938 err = view->input(new, view, ch);
939 view->dying = 1;
940 break;
941 case 'Q':
942 *done = 1;
943 break;
944 case 'f':
945 if (view_is_parent_view(view)) {
946 if (view->child == NULL)
947 break;
948 if (view_is_splitscreen(view->child)) {
949 view->focussed = 0;
950 view->child->focussed = 1;
951 err = view_fullscreen(view->child);
952 } else
953 err = view_splitscreen(view->child);
954 if (err)
955 break;
956 err = view->child->input(new, view->child,
957 KEY_RESIZE);
958 } else {
959 if (view_is_splitscreen(view)) {
960 view->parent->focussed = 0;
961 view->focussed = 1;
962 err = view_fullscreen(view);
963 } else {
964 err = view_splitscreen(view);
966 if (err)
967 break;
968 err = view->input(new, view, KEY_RESIZE);
970 break;
971 case KEY_RESIZE:
972 break;
973 case '/':
974 if (view->search_start)
975 view_search_start(view);
976 else
977 err = view->input(new, view, ch);
978 break;
979 case 'N':
980 case 'n':
981 if (view->search_started && view->search_next) {
982 view->searching = (ch == 'n' ?
983 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
984 view->search_next_done = 0;
985 view->search_next(view);
986 } else
987 err = view->input(new, view, ch);
988 break;
989 default:
990 err = view->input(new, view, ch);
991 break;
994 return err;
997 void
998 view_vborder(struct tog_view *view)
1000 PANEL *panel;
1001 const struct tog_view *view_above;
1003 if (view->parent)
1004 return view_vborder(view->parent);
1006 panel = panel_above(view->panel);
1007 if (panel == NULL)
1008 return;
1010 view_above = panel_userptr(panel);
1011 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1012 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1015 int
1016 view_needs_focus_indication(struct tog_view *view)
1018 if (view_is_parent_view(view)) {
1019 if (view->child == NULL || view->child->focussed)
1020 return 0;
1021 if (!view_is_splitscreen(view->child))
1022 return 0;
1023 } else if (!view_is_splitscreen(view))
1024 return 0;
1026 return view->focussed;
1029 static const struct got_error *
1030 view_loop(struct tog_view *view)
1032 const struct got_error *err = NULL;
1033 struct tog_view_list_head views;
1034 struct tog_view *new_view;
1035 int fast_refresh = 10;
1036 int done = 0, errcode;
1038 errcode = pthread_mutex_lock(&tog_mutex);
1039 if (errcode)
1040 return got_error_set_errno(errcode, "pthread_mutex_lock");
1042 TAILQ_INIT(&views);
1043 TAILQ_INSERT_HEAD(&views, view, entry);
1045 view->focussed = 1;
1046 err = view->show(view);
1047 if (err)
1048 return err;
1049 update_panels();
1050 doupdate();
1051 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1052 /* Refresh fast during initialization, then become slower. */
1053 if (fast_refresh && fast_refresh-- == 0)
1054 halfdelay(10); /* switch to once per second */
1056 err = view_input(&new_view, &done, view, &views);
1057 if (err)
1058 break;
1059 if (view->dying) {
1060 struct tog_view *v, *prev = NULL;
1062 if (view_is_parent_view(view))
1063 prev = TAILQ_PREV(view, tog_view_list_head,
1064 entry);
1065 else if (view->parent)
1066 prev = view->parent;
1068 if (view->parent) {
1069 view->parent->child = NULL;
1070 view->parent->focus_child = 0;
1071 } else
1072 TAILQ_REMOVE(&views, view, entry);
1074 err = view_close(view);
1075 if (err)
1076 goto done;
1078 view = NULL;
1079 TAILQ_FOREACH(v, &views, entry) {
1080 if (v->focussed)
1081 break;
1083 if (view == NULL && new_view == NULL) {
1084 /* No view has focus. Try to pick one. */
1085 if (prev)
1086 view = prev;
1087 else if (!TAILQ_EMPTY(&views)) {
1088 view = TAILQ_LAST(&views,
1089 tog_view_list_head);
1091 if (view) {
1092 if (view->focus_child) {
1093 view->child->focussed = 1;
1094 view = view->child;
1095 } else
1096 view->focussed = 1;
1100 if (new_view) {
1101 struct tog_view *v, *t;
1102 /* Only allow one parent view per type. */
1103 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1104 if (v->type != new_view->type)
1105 continue;
1106 TAILQ_REMOVE(&views, v, entry);
1107 err = view_close(v);
1108 if (err)
1109 goto done;
1110 break;
1112 TAILQ_INSERT_TAIL(&views, new_view, entry);
1113 view = new_view;
1115 if (view) {
1116 if (view_is_parent_view(view)) {
1117 if (view->child && view->child->focussed)
1118 view = view->child;
1119 } else {
1120 if (view->parent && view->parent->focussed)
1121 view = view->parent;
1123 show_panel(view->panel);
1124 if (view->child && view_is_splitscreen(view->child))
1125 show_panel(view->child->panel);
1126 if (view->parent && view_is_splitscreen(view)) {
1127 err = view->parent->show(view->parent);
1128 if (err)
1129 goto done;
1131 err = view->show(view);
1132 if (err)
1133 goto done;
1134 if (view->child) {
1135 err = view->child->show(view->child);
1136 if (err)
1137 goto done;
1139 update_panels();
1140 doupdate();
1143 done:
1144 while (!TAILQ_EMPTY(&views)) {
1145 view = TAILQ_FIRST(&views);
1146 TAILQ_REMOVE(&views, view, entry);
1147 view_close(view);
1150 errcode = pthread_mutex_unlock(&tog_mutex);
1151 if (errcode && err == NULL)
1152 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1154 return err;
1157 __dead static void
1158 usage_log(void)
1160 endwin();
1161 fprintf(stderr,
1162 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1163 getprogname());
1164 exit(1);
1167 /* Create newly allocated wide-character string equivalent to a byte string. */
1168 static const struct got_error *
1169 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1171 char *vis = NULL;
1172 const struct got_error *err = NULL;
1174 *ws = NULL;
1175 *wlen = mbstowcs(NULL, s, 0);
1176 if (*wlen == (size_t)-1) {
1177 int vislen;
1178 if (errno != EILSEQ)
1179 return got_error_from_errno("mbstowcs");
1181 /* byte string invalid in current encoding; try to "fix" it */
1182 err = got_mbsavis(&vis, &vislen, s);
1183 if (err)
1184 return err;
1185 *wlen = mbstowcs(NULL, vis, 0);
1186 if (*wlen == (size_t)-1) {
1187 err = got_error_from_errno("mbstowcs"); /* give up */
1188 goto done;
1192 *ws = calloc(*wlen + 1, sizeof(**ws));
1193 if (*ws == NULL) {
1194 err = got_error_from_errno("calloc");
1195 goto done;
1198 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1199 err = got_error_from_errno("mbstowcs");
1200 done:
1201 free(vis);
1202 if (err) {
1203 free(*ws);
1204 *ws = NULL;
1205 *wlen = 0;
1207 return err;
1210 /* Format a line for display, ensuring that it won't overflow a width limit. */
1211 static const struct got_error *
1212 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1213 int col_tab_align)
1215 const struct got_error *err = NULL;
1216 int cols = 0;
1217 wchar_t *wline = NULL;
1218 size_t wlen;
1219 int i;
1221 *wlinep = NULL;
1222 *widthp = 0;
1224 err = mbs2ws(&wline, &wlen, line);
1225 if (err)
1226 return err;
1228 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1229 wline[wlen - 1] = L'\0';
1230 wlen--;
1232 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1233 wline[wlen - 1] = L'\0';
1234 wlen--;
1237 i = 0;
1238 while (i < wlen) {
1239 int width = wcwidth(wline[i]);
1241 if (width == 0) {
1242 i++;
1243 continue;
1246 if (width == 1 || width == 2) {
1247 if (cols + width > wlimit)
1248 break;
1249 cols += width;
1250 i++;
1251 } else if (width == -1) {
1252 if (wline[i] == L'\t') {
1253 width = TABSIZE -
1254 ((cols + col_tab_align) % TABSIZE);
1255 } else {
1256 width = 1;
1257 wline[i] = L'.';
1259 if (cols + width > wlimit)
1260 break;
1261 cols += width;
1262 i++;
1263 } else {
1264 err = got_error_from_errno("wcwidth");
1265 goto done;
1268 wline[i] = L'\0';
1269 if (widthp)
1270 *widthp = cols;
1271 done:
1272 if (err)
1273 free(wline);
1274 else
1275 *wlinep = wline;
1276 return err;
1279 static const struct got_error*
1280 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1281 struct got_object_id *id, struct got_repository *repo)
1283 static const struct got_error *err = NULL;
1284 struct got_reflist_entry *re;
1285 char *s;
1286 const char *name;
1288 *refs_str = NULL;
1290 TAILQ_FOREACH(re, refs, entry) {
1291 struct got_tag_object *tag = NULL;
1292 struct got_object_id *ref_id;
1293 int cmp;
1295 name = got_ref_get_name(re->ref);
1296 if (strcmp(name, GOT_REF_HEAD) == 0)
1297 continue;
1298 if (strncmp(name, "refs/", 5) == 0)
1299 name += 5;
1300 if (strncmp(name, "got/", 4) == 0 &&
1301 strncmp(name, "got/backup/", 11) != 0)
1302 continue;
1303 if (strncmp(name, "heads/", 6) == 0)
1304 name += 6;
1305 if (strncmp(name, "remotes/", 8) == 0) {
1306 name += 8;
1307 s = strstr(name, "/" GOT_REF_HEAD);
1308 if (s != NULL && s[strlen(s)] == '\0')
1309 continue;
1311 err = got_ref_resolve(&ref_id, repo, re->ref);
1312 if (err)
1313 break;
1314 if (strncmp(name, "tags/", 5) == 0) {
1315 err = got_object_open_as_tag(&tag, repo, ref_id);
1316 if (err) {
1317 if (err->code != GOT_ERR_OBJ_TYPE) {
1318 free(ref_id);
1319 break;
1321 /* Ref points at something other than a tag. */
1322 err = NULL;
1323 tag = NULL;
1326 cmp = got_object_id_cmp(tag ?
1327 got_object_tag_get_object_id(tag) : ref_id, id);
1328 free(ref_id);
1329 if (tag)
1330 got_object_tag_close(tag);
1331 if (cmp != 0)
1332 continue;
1333 s = *refs_str;
1334 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1335 s ? ", " : "", name) == -1) {
1336 err = got_error_from_errno("asprintf");
1337 free(s);
1338 *refs_str = NULL;
1339 break;
1341 free(s);
1344 return err;
1347 static const struct got_error *
1348 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1349 int col_tab_align)
1351 char *smallerthan;
1353 smallerthan = strchr(author, '<');
1354 if (smallerthan && smallerthan[1] != '\0')
1355 author = smallerthan + 1;
1356 author[strcspn(author, "@>")] = '\0';
1357 return format_line(wauthor, author_width, author, limit, col_tab_align);
1360 static const struct got_error *
1361 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1362 struct got_object_id *id, const size_t date_display_cols,
1363 int author_display_cols)
1365 struct tog_log_view_state *s = &view->state.log;
1366 const struct got_error *err = NULL;
1367 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1368 char *logmsg0 = NULL, *logmsg = NULL;
1369 char *author = NULL;
1370 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1371 int author_width, logmsg_width;
1372 char *newline, *line = NULL;
1373 int col, limit;
1374 const int avail = view->ncols;
1375 struct tm tm;
1376 time_t committer_time;
1377 struct tog_color *tc;
1379 committer_time = got_object_commit_get_committer_time(commit);
1380 if (gmtime_r(&committer_time, &tm) == NULL)
1381 return got_error_from_errno("gmtime_r");
1382 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1383 return got_error(GOT_ERR_NO_SPACE);
1385 if (avail <= date_display_cols)
1386 limit = MIN(sizeof(datebuf) - 1, avail);
1387 else
1388 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1389 tc = get_color(&s->colors, TOG_COLOR_DATE);
1390 if (tc)
1391 wattr_on(view->window,
1392 COLOR_PAIR(tc->colorpair), NULL);
1393 waddnstr(view->window, datebuf, limit);
1394 if (tc)
1395 wattr_off(view->window,
1396 COLOR_PAIR(tc->colorpair), NULL);
1397 col = limit;
1398 if (col > avail)
1399 goto done;
1401 if (avail >= 120) {
1402 char *id_str;
1403 err = got_object_id_str(&id_str, id);
1404 if (err)
1405 goto done;
1406 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1407 if (tc)
1408 wattr_on(view->window,
1409 COLOR_PAIR(tc->colorpair), NULL);
1410 wprintw(view->window, "%.8s ", id_str);
1411 if (tc)
1412 wattr_off(view->window,
1413 COLOR_PAIR(tc->colorpair), NULL);
1414 free(id_str);
1415 col += 9;
1416 if (col > avail)
1417 goto done;
1420 author = strdup(got_object_commit_get_author(commit));
1421 if (author == NULL) {
1422 err = got_error_from_errno("strdup");
1423 goto done;
1425 err = format_author(&wauthor, &author_width, author, avail - col, col);
1426 if (err)
1427 goto done;
1428 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1429 if (tc)
1430 wattr_on(view->window,
1431 COLOR_PAIR(tc->colorpair), NULL);
1432 waddwstr(view->window, wauthor);
1433 if (tc)
1434 wattr_off(view->window,
1435 COLOR_PAIR(tc->colorpair), NULL);
1436 col += author_width;
1437 while (col < avail && author_width < author_display_cols + 2) {
1438 waddch(view->window, ' ');
1439 col++;
1440 author_width++;
1442 if (col > avail)
1443 goto done;
1445 err = got_object_commit_get_logmsg(&logmsg0, commit);
1446 if (err)
1447 goto done;
1448 logmsg = logmsg0;
1449 while (*logmsg == '\n')
1450 logmsg++;
1451 newline = strchr(logmsg, '\n');
1452 if (newline)
1453 *newline = '\0';
1454 limit = avail - col;
1455 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1456 if (err)
1457 goto done;
1458 waddwstr(view->window, wlogmsg);
1459 col += logmsg_width;
1460 while (col < avail) {
1461 waddch(view->window, ' ');
1462 col++;
1464 done:
1465 free(logmsg0);
1466 free(wlogmsg);
1467 free(author);
1468 free(wauthor);
1469 free(line);
1470 return err;
1473 static struct commit_queue_entry *
1474 alloc_commit_queue_entry(struct got_commit_object *commit,
1475 struct got_object_id *id)
1477 struct commit_queue_entry *entry;
1479 entry = calloc(1, sizeof(*entry));
1480 if (entry == NULL)
1481 return NULL;
1483 entry->id = id;
1484 entry->commit = commit;
1485 return entry;
1488 static void
1489 pop_commit(struct commit_queue *commits)
1491 struct commit_queue_entry *entry;
1493 entry = TAILQ_FIRST(&commits->head);
1494 TAILQ_REMOVE(&commits->head, entry, entry);
1495 got_object_commit_close(entry->commit);
1496 commits->ncommits--;
1497 /* Don't free entry->id! It is owned by the commit graph. */
1498 free(entry);
1501 static void
1502 free_commits(struct commit_queue *commits)
1504 while (!TAILQ_EMPTY(&commits->head))
1505 pop_commit(commits);
1508 static const struct got_error *
1509 match_commit(int *have_match, struct got_object_id *id,
1510 struct got_commit_object *commit, regex_t *regex)
1512 const struct got_error *err = NULL;
1513 regmatch_t regmatch;
1514 char *id_str = NULL, *logmsg = NULL;
1516 *have_match = 0;
1518 err = got_object_id_str(&id_str, id);
1519 if (err)
1520 return err;
1522 err = got_object_commit_get_logmsg(&logmsg, commit);
1523 if (err)
1524 goto done;
1526 if (regexec(regex, got_object_commit_get_author(commit), 1,
1527 &regmatch, 0) == 0 ||
1528 regexec(regex, got_object_commit_get_committer(commit), 1,
1529 &regmatch, 0) == 0 ||
1530 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1531 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1532 *have_match = 1;
1533 done:
1534 free(id_str);
1535 free(logmsg);
1536 return err;
1539 static const struct got_error *
1540 queue_commits(struct tog_log_thread_args *a)
1542 const struct got_error *err = NULL;
1545 * We keep all commits open throughout the lifetime of the log
1546 * view in order to avoid having to re-fetch commits from disk
1547 * while updating the display.
1549 do {
1550 struct got_object_id *id;
1551 struct got_commit_object *commit;
1552 struct commit_queue_entry *entry;
1553 int errcode;
1555 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1556 NULL, NULL);
1557 if (err || id == NULL)
1558 break;
1560 err = got_object_open_as_commit(&commit, a->repo, id);
1561 if (err)
1562 break;
1563 entry = alloc_commit_queue_entry(commit, id);
1564 if (entry == NULL) {
1565 err = got_error_from_errno("alloc_commit_queue_entry");
1566 break;
1569 errcode = pthread_mutex_lock(&tog_mutex);
1570 if (errcode) {
1571 err = got_error_set_errno(errcode,
1572 "pthread_mutex_lock");
1573 break;
1576 entry->idx = a->commits->ncommits;
1577 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1578 a->commits->ncommits++;
1580 if (*a->searching == TOG_SEARCH_FORWARD &&
1581 !*a->search_next_done) {
1582 int have_match;
1583 err = match_commit(&have_match, id, commit, a->regex);
1584 if (err)
1585 break;
1586 if (have_match)
1587 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1590 errcode = pthread_mutex_unlock(&tog_mutex);
1591 if (errcode && err == NULL)
1592 err = got_error_set_errno(errcode,
1593 "pthread_mutex_unlock");
1594 if (err)
1595 break;
1596 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1598 return err;
1601 static void
1602 select_commit(struct tog_log_view_state *s)
1604 struct commit_queue_entry *entry;
1605 int ncommits = 0;
1607 entry = s->first_displayed_entry;
1608 while (entry) {
1609 if (ncommits == s->selected) {
1610 s->selected_entry = entry;
1611 break;
1613 entry = TAILQ_NEXT(entry, entry);
1614 ncommits++;
1618 static const struct got_error *
1619 draw_commits(struct tog_view *view)
1621 const struct got_error *err = NULL;
1622 struct tog_log_view_state *s = &view->state.log;
1623 struct commit_queue_entry *entry = s->selected_entry;
1624 const int limit = view->nlines;
1625 int width;
1626 int ncommits, author_cols = 4;
1627 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1628 char *refs_str = NULL;
1629 wchar_t *wline;
1630 struct tog_color *tc;
1631 static const size_t date_display_cols = 12;
1633 if (s->selected_entry &&
1634 !(view->searching && view->search_next_done == 0)) {
1635 struct got_reflist_head *refs;
1636 err = got_object_id_str(&id_str, s->selected_entry->id);
1637 if (err)
1638 return err;
1639 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1640 s->selected_entry->id);
1641 if (refs) {
1642 err = build_refs_str(&refs_str, refs,
1643 s->selected_entry->id, s->repo);
1644 if (err)
1645 goto done;
1649 if (s->thread_args.commits_needed == 0)
1650 halfdelay(10); /* disable fast refresh */
1652 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1653 if (asprintf(&ncommits_str, " [%d/%d] %s",
1654 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1655 (view->searching && !view->search_next_done) ?
1656 "searching..." : "loading...") == -1) {
1657 err = got_error_from_errno("asprintf");
1658 goto done;
1660 } else {
1661 const char *search_str = NULL;
1663 if (view->searching) {
1664 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1665 search_str = "no more matches";
1666 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1667 search_str = "no matches found";
1668 else if (!view->search_next_done)
1669 search_str = "searching...";
1672 if (asprintf(&ncommits_str, " [%d/%d] %s",
1673 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1674 search_str ? search_str :
1675 (refs_str ? refs_str : "")) == -1) {
1676 err = got_error_from_errno("asprintf");
1677 goto done;
1681 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1682 if (asprintf(&header, "commit %s %s%s",
1683 id_str ? id_str : "........................................",
1684 s->in_repo_path, ncommits_str) == -1) {
1685 err = got_error_from_errno("asprintf");
1686 header = NULL;
1687 goto done;
1689 } else if (asprintf(&header, "commit %s%s",
1690 id_str ? id_str : "........................................",
1691 ncommits_str) == -1) {
1692 err = got_error_from_errno("asprintf");
1693 header = NULL;
1694 goto done;
1696 err = format_line(&wline, &width, header, view->ncols, 0);
1697 if (err)
1698 goto done;
1700 werase(view->window);
1702 if (view_needs_focus_indication(view))
1703 wstandout(view->window);
1704 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1705 if (tc)
1706 wattr_on(view->window,
1707 COLOR_PAIR(tc->colorpair), NULL);
1708 waddwstr(view->window, wline);
1709 if (tc)
1710 wattr_off(view->window,
1711 COLOR_PAIR(tc->colorpair), NULL);
1712 while (width < view->ncols) {
1713 waddch(view->window, ' ');
1714 width++;
1716 if (view_needs_focus_indication(view))
1717 wstandend(view->window);
1718 free(wline);
1719 if (limit <= 1)
1720 goto done;
1722 /* Grow author column size if necessary. */
1723 entry = s->first_displayed_entry;
1724 ncommits = 0;
1725 while (entry) {
1726 char *author;
1727 wchar_t *wauthor;
1728 int width;
1729 if (ncommits >= limit - 1)
1730 break;
1731 author = strdup(got_object_commit_get_author(entry->commit));
1732 if (author == NULL) {
1733 err = got_error_from_errno("strdup");
1734 goto done;
1736 err = format_author(&wauthor, &width, author, COLS,
1737 date_display_cols);
1738 if (author_cols < width)
1739 author_cols = width;
1740 free(wauthor);
1741 free(author);
1742 ncommits++;
1743 entry = TAILQ_NEXT(entry, entry);
1746 entry = s->first_displayed_entry;
1747 s->last_displayed_entry = s->first_displayed_entry;
1748 ncommits = 0;
1749 while (entry) {
1750 if (ncommits >= limit - 1)
1751 break;
1752 if (ncommits == s->selected)
1753 wstandout(view->window);
1754 err = draw_commit(view, entry->commit, entry->id,
1755 date_display_cols, author_cols);
1756 if (ncommits == s->selected)
1757 wstandend(view->window);
1758 if (err)
1759 goto done;
1760 ncommits++;
1761 s->last_displayed_entry = entry;
1762 entry = TAILQ_NEXT(entry, entry);
1765 view_vborder(view);
1766 update_panels();
1767 doupdate();
1768 done:
1769 free(id_str);
1770 free(refs_str);
1771 free(ncommits_str);
1772 free(header);
1773 return err;
1776 static void
1777 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1779 struct commit_queue_entry *entry;
1780 int nscrolled = 0;
1782 entry = TAILQ_FIRST(&s->commits.head);
1783 if (s->first_displayed_entry == entry)
1784 return;
1786 entry = s->first_displayed_entry;
1787 while (entry && nscrolled < maxscroll) {
1788 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1789 if (entry) {
1790 s->first_displayed_entry = entry;
1791 nscrolled++;
1796 static const struct got_error *
1797 trigger_log_thread(struct tog_view *view, int wait)
1799 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1800 int errcode;
1802 halfdelay(1); /* fast refresh while loading commits */
1804 while (ta->commits_needed > 0 || ta->load_all) {
1805 if (ta->log_complete)
1806 break;
1808 /* Wake the log thread. */
1809 errcode = pthread_cond_signal(&ta->need_commits);
1810 if (errcode)
1811 return got_error_set_errno(errcode,
1812 "pthread_cond_signal");
1815 * The mutex will be released while the view loop waits
1816 * in wgetch(), at which time the log thread will run.
1818 if (!wait)
1819 break;
1821 /* Display progress update in log view. */
1822 show_log_view(view);
1823 update_panels();
1824 doupdate();
1826 /* Wait right here while next commit is being loaded. */
1827 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1828 if (errcode)
1829 return got_error_set_errno(errcode,
1830 "pthread_cond_wait");
1832 /* Display progress update in log view. */
1833 show_log_view(view);
1834 update_panels();
1835 doupdate();
1838 return NULL;
1841 static const struct got_error *
1842 log_scroll_down(struct tog_view *view, int maxscroll)
1844 struct tog_log_view_state *s = &view->state.log;
1845 const struct got_error *err = NULL;
1846 struct commit_queue_entry *pentry;
1847 int nscrolled = 0, ncommits_needed;
1849 if (s->last_displayed_entry == NULL)
1850 return NULL;
1852 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1853 if (s->commits.ncommits < ncommits_needed &&
1854 !s->thread_args.log_complete) {
1856 * Ask the log thread for required amount of commits.
1858 s->thread_args.commits_needed += maxscroll;
1859 err = trigger_log_thread(view, 1);
1860 if (err)
1861 return err;
1864 do {
1865 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1866 if (pentry == NULL)
1867 break;
1869 s->last_displayed_entry = pentry;
1871 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1872 if (pentry == NULL)
1873 break;
1874 s->first_displayed_entry = pentry;
1875 } while (++nscrolled < maxscroll);
1877 return err;
1880 static const struct got_error *
1881 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1882 struct got_commit_object *commit, struct got_object_id *commit_id,
1883 struct tog_view *log_view, struct got_repository *repo)
1885 const struct got_error *err;
1886 struct got_object_qid *parent_id;
1887 struct tog_view *diff_view;
1889 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1890 if (diff_view == NULL)
1891 return got_error_from_errno("view_open");
1893 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1894 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1895 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1896 if (err == NULL)
1897 *new_view = diff_view;
1898 return err;
1901 static const struct got_error *
1902 tree_view_visit_subtree(struct tog_tree_view_state *s,
1903 struct got_tree_object *subtree)
1905 struct tog_parent_tree *parent;
1907 parent = calloc(1, sizeof(*parent));
1908 if (parent == NULL)
1909 return got_error_from_errno("calloc");
1911 parent->tree = s->tree;
1912 parent->first_displayed_entry = s->first_displayed_entry;
1913 parent->selected_entry = s->selected_entry;
1914 parent->selected = s->selected;
1915 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1916 s->tree = subtree;
1917 s->selected = 0;
1918 s->first_displayed_entry = NULL;
1919 return NULL;
1922 static const struct got_error *
1923 tree_view_walk_path(struct tog_tree_view_state *s,
1924 struct got_commit_object *commit, const char *path)
1926 const struct got_error *err = NULL;
1927 struct got_tree_object *tree = NULL;
1928 const char *p;
1929 char *slash, *subpath = NULL;
1931 /* Walk the path and open corresponding tree objects. */
1932 p = path;
1933 while (*p) {
1934 struct got_tree_entry *te;
1935 struct got_object_id *tree_id;
1936 char *te_name;
1938 while (p[0] == '/')
1939 p++;
1941 /* Ensure the correct subtree entry is selected. */
1942 slash = strchr(p, '/');
1943 if (slash == NULL)
1944 te_name = strdup(p);
1945 else
1946 te_name = strndup(p, slash - p);
1947 if (te_name == NULL) {
1948 err = got_error_from_errno("strndup");
1949 break;
1951 te = got_object_tree_find_entry(s->tree, te_name);
1952 if (te == NULL) {
1953 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1954 free(te_name);
1955 break;
1957 free(te_name);
1958 s->first_displayed_entry = s->selected_entry = te;
1960 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1961 break; /* jump to this file's entry */
1963 slash = strchr(p, '/');
1964 if (slash)
1965 subpath = strndup(path, slash - path);
1966 else
1967 subpath = strdup(path);
1968 if (subpath == NULL) {
1969 err = got_error_from_errno("strdup");
1970 break;
1973 err = got_object_id_by_path(&tree_id, s->repo, commit,
1974 subpath);
1975 if (err)
1976 break;
1978 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1979 free(tree_id);
1980 if (err)
1981 break;
1983 err = tree_view_visit_subtree(s, tree);
1984 if (err) {
1985 got_object_tree_close(tree);
1986 break;
1988 if (slash == NULL)
1989 break;
1990 free(subpath);
1991 subpath = NULL;
1992 p = slash;
1995 free(subpath);
1996 return err;
1999 static const struct got_error *
2000 browse_commit_tree(struct tog_view **new_view, int begin_x,
2001 struct commit_queue_entry *entry, const char *path,
2002 const char *head_ref_name, struct got_repository *repo)
2004 const struct got_error *err = NULL;
2005 struct tog_tree_view_state *s;
2006 struct tog_view *tree_view;
2008 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2009 if (tree_view == NULL)
2010 return got_error_from_errno("view_open");
2012 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2013 if (err)
2014 return err;
2015 s = &tree_view->state.tree;
2017 *new_view = tree_view;
2019 if (got_path_is_root_dir(path))
2020 return NULL;
2022 return tree_view_walk_path(s, entry->commit, path);
2025 static const struct got_error *
2026 block_signals_used_by_main_thread(void)
2028 sigset_t sigset;
2029 int errcode;
2031 if (sigemptyset(&sigset) == -1)
2032 return got_error_from_errno("sigemptyset");
2034 /* tog handles SIGWINCH and SIGCONT */
2035 if (sigaddset(&sigset, SIGWINCH) == -1)
2036 return got_error_from_errno("sigaddset");
2037 if (sigaddset(&sigset, SIGCONT) == -1)
2038 return got_error_from_errno("sigaddset");
2040 /* ncurses handles SIGTSTP */
2041 if (sigaddset(&sigset, SIGTSTP) == -1)
2042 return got_error_from_errno("sigaddset");
2044 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2045 if (errcode)
2046 return got_error_set_errno(errcode, "pthread_sigmask");
2048 return NULL;
2051 static void *
2052 log_thread(void *arg)
2054 const struct got_error *err = NULL;
2055 int errcode = 0;
2056 struct tog_log_thread_args *a = arg;
2057 int done = 0;
2059 err = block_signals_used_by_main_thread();
2060 if (err)
2061 return (void *)err;
2063 while (!done && !err && !tog_sigpipe_received) {
2064 err = queue_commits(a);
2065 if (err) {
2066 if (err->code != GOT_ERR_ITER_COMPLETED)
2067 return (void *)err;
2068 err = NULL;
2069 done = 1;
2070 } else if (a->commits_needed > 0 && !a->load_all)
2071 a->commits_needed--;
2073 errcode = pthread_mutex_lock(&tog_mutex);
2074 if (errcode) {
2075 err = got_error_set_errno(errcode,
2076 "pthread_mutex_lock");
2077 break;
2078 } else if (*a->quit)
2079 done = 1;
2080 else if (*a->first_displayed_entry == NULL) {
2081 *a->first_displayed_entry =
2082 TAILQ_FIRST(&a->commits->head);
2083 *a->selected_entry = *a->first_displayed_entry;
2086 errcode = pthread_cond_signal(&a->commit_loaded);
2087 if (errcode) {
2088 err = got_error_set_errno(errcode,
2089 "pthread_cond_signal");
2090 pthread_mutex_unlock(&tog_mutex);
2091 break;
2094 if (done)
2095 a->commits_needed = 0;
2096 else {
2097 if (a->commits_needed == 0 && !a->load_all) {
2098 errcode = pthread_cond_wait(&a->need_commits,
2099 &tog_mutex);
2100 if (errcode)
2101 err = got_error_set_errno(errcode,
2102 "pthread_cond_wait");
2103 if (*a->quit)
2104 done = 1;
2108 errcode = pthread_mutex_unlock(&tog_mutex);
2109 if (errcode && err == NULL)
2110 err = got_error_set_errno(errcode,
2111 "pthread_mutex_unlock");
2113 a->log_complete = 1;
2114 return (void *)err;
2117 static const struct got_error *
2118 stop_log_thread(struct tog_log_view_state *s)
2120 const struct got_error *err = NULL;
2121 int errcode;
2123 if (s->thread) {
2124 s->quit = 1;
2125 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2126 if (errcode)
2127 return got_error_set_errno(errcode,
2128 "pthread_cond_signal");
2129 errcode = pthread_mutex_unlock(&tog_mutex);
2130 if (errcode)
2131 return got_error_set_errno(errcode,
2132 "pthread_mutex_unlock");
2133 errcode = pthread_join(s->thread, (void **)&err);
2134 if (errcode)
2135 return got_error_set_errno(errcode, "pthread_join");
2136 errcode = pthread_mutex_lock(&tog_mutex);
2137 if (errcode)
2138 return got_error_set_errno(errcode,
2139 "pthread_mutex_lock");
2140 s->thread = 0; //NULL;
2143 if (s->thread_args.repo) {
2144 err = got_repo_close(s->thread_args.repo);
2145 s->thread_args.repo = NULL;
2148 if (s->thread_args.graph) {
2149 got_commit_graph_close(s->thread_args.graph);
2150 s->thread_args.graph = NULL;
2153 return err;
2156 static const struct got_error *
2157 close_log_view(struct tog_view *view)
2159 const struct got_error *err = NULL;
2160 struct tog_log_view_state *s = &view->state.log;
2161 int errcode;
2163 err = stop_log_thread(s);
2165 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2166 if (errcode && err == NULL)
2167 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2169 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2170 if (errcode && err == NULL)
2171 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2173 free_commits(&s->commits);
2174 free(s->in_repo_path);
2175 s->in_repo_path = NULL;
2176 free(s->start_id);
2177 s->start_id = NULL;
2178 free(s->head_ref_name);
2179 s->head_ref_name = NULL;
2180 return err;
2183 static const struct got_error *
2184 search_start_log_view(struct tog_view *view)
2186 struct tog_log_view_state *s = &view->state.log;
2188 s->matched_entry = NULL;
2189 s->search_entry = NULL;
2190 return NULL;
2193 static const struct got_error *
2194 search_next_log_view(struct tog_view *view)
2196 const struct got_error *err = NULL;
2197 struct tog_log_view_state *s = &view->state.log;
2198 struct commit_queue_entry *entry;
2200 /* Display progress update in log view. */
2201 show_log_view(view);
2202 update_panels();
2203 doupdate();
2205 if (s->search_entry) {
2206 int errcode, ch;
2207 errcode = pthread_mutex_unlock(&tog_mutex);
2208 if (errcode)
2209 return got_error_set_errno(errcode,
2210 "pthread_mutex_unlock");
2211 ch = wgetch(view->window);
2212 errcode = pthread_mutex_lock(&tog_mutex);
2213 if (errcode)
2214 return got_error_set_errno(errcode,
2215 "pthread_mutex_lock");
2216 if (ch == KEY_BACKSPACE) {
2217 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2218 return NULL;
2220 if (view->searching == TOG_SEARCH_FORWARD)
2221 entry = TAILQ_NEXT(s->search_entry, entry);
2222 else
2223 entry = TAILQ_PREV(s->search_entry,
2224 commit_queue_head, entry);
2225 } else if (s->matched_entry) {
2226 if (view->searching == TOG_SEARCH_FORWARD)
2227 entry = TAILQ_NEXT(s->matched_entry, entry);
2228 else
2229 entry = TAILQ_PREV(s->matched_entry,
2230 commit_queue_head, entry);
2231 } else {
2232 entry = s->selected_entry;
2235 while (1) {
2236 int have_match = 0;
2238 if (entry == NULL) {
2239 if (s->thread_args.log_complete ||
2240 view->searching == TOG_SEARCH_BACKWARD) {
2241 view->search_next_done =
2242 (s->matched_entry == NULL ?
2243 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2244 s->search_entry = NULL;
2245 return NULL;
2248 * Poke the log thread for more commits and return,
2249 * allowing the main loop to make progress. Search
2250 * will resume at s->search_entry once we come back.
2252 s->thread_args.commits_needed++;
2253 return trigger_log_thread(view, 0);
2256 err = match_commit(&have_match, entry->id, entry->commit,
2257 &view->regex);
2258 if (err)
2259 break;
2260 if (have_match) {
2261 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2262 s->matched_entry = entry;
2263 break;
2266 s->search_entry = entry;
2267 if (view->searching == TOG_SEARCH_FORWARD)
2268 entry = TAILQ_NEXT(entry, entry);
2269 else
2270 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2273 if (s->matched_entry) {
2274 int cur = s->selected_entry->idx;
2275 while (cur < s->matched_entry->idx) {
2276 err = input_log_view(NULL, view, KEY_DOWN);
2277 if (err)
2278 return err;
2279 cur++;
2281 while (cur > s->matched_entry->idx) {
2282 err = input_log_view(NULL, view, KEY_UP);
2283 if (err)
2284 return err;
2285 cur--;
2289 s->search_entry = NULL;
2291 return NULL;
2294 static const struct got_error *
2295 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2296 struct got_repository *repo, const char *head_ref_name,
2297 const char *in_repo_path, int log_branches)
2299 const struct got_error *err = NULL;
2300 struct tog_log_view_state *s = &view->state.log;
2301 struct got_repository *thread_repo = NULL;
2302 struct got_commit_graph *thread_graph = NULL;
2303 int errcode;
2305 if (in_repo_path != s->in_repo_path) {
2306 free(s->in_repo_path);
2307 s->in_repo_path = strdup(in_repo_path);
2308 if (s->in_repo_path == NULL)
2309 return got_error_from_errno("strdup");
2312 /* The commit queue only contains commits being displayed. */
2313 TAILQ_INIT(&s->commits.head);
2314 s->commits.ncommits = 0;
2316 s->repo = repo;
2317 if (head_ref_name) {
2318 s->head_ref_name = strdup(head_ref_name);
2319 if (s->head_ref_name == NULL) {
2320 err = got_error_from_errno("strdup");
2321 goto done;
2324 s->start_id = got_object_id_dup(start_id);
2325 if (s->start_id == NULL) {
2326 err = got_error_from_errno("got_object_id_dup");
2327 goto done;
2329 s->log_branches = log_branches;
2331 STAILQ_INIT(&s->colors);
2332 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2333 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2334 get_color_value("TOG_COLOR_COMMIT"));
2335 if (err)
2336 goto done;
2337 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2338 get_color_value("TOG_COLOR_AUTHOR"));
2339 if (err) {
2340 free_colors(&s->colors);
2341 goto done;
2343 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2344 get_color_value("TOG_COLOR_DATE"));
2345 if (err) {
2346 free_colors(&s->colors);
2347 goto done;
2351 view->show = show_log_view;
2352 view->input = input_log_view;
2353 view->close = close_log_view;
2354 view->search_start = search_start_log_view;
2355 view->search_next = search_next_log_view;
2357 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2358 if (err)
2359 goto done;
2360 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2361 !s->log_branches);
2362 if (err)
2363 goto done;
2364 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2365 s->repo, NULL, NULL);
2366 if (err)
2367 goto done;
2369 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2370 if (errcode) {
2371 err = got_error_set_errno(errcode, "pthread_cond_init");
2372 goto done;
2374 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2375 if (errcode) {
2376 err = got_error_set_errno(errcode, "pthread_cond_init");
2377 goto done;
2380 s->thread_args.commits_needed = view->nlines;
2381 s->thread_args.graph = thread_graph;
2382 s->thread_args.commits = &s->commits;
2383 s->thread_args.in_repo_path = s->in_repo_path;
2384 s->thread_args.start_id = s->start_id;
2385 s->thread_args.repo = thread_repo;
2386 s->thread_args.log_complete = 0;
2387 s->thread_args.quit = &s->quit;
2388 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2389 s->thread_args.selected_entry = &s->selected_entry;
2390 s->thread_args.searching = &view->searching;
2391 s->thread_args.search_next_done = &view->search_next_done;
2392 s->thread_args.regex = &view->regex;
2393 done:
2394 if (err)
2395 close_log_view(view);
2396 return err;
2399 static const struct got_error *
2400 show_log_view(struct tog_view *view)
2402 const struct got_error *err;
2403 struct tog_log_view_state *s = &view->state.log;
2405 if (s->thread == 0) { //NULL) {
2406 int errcode = pthread_create(&s->thread, NULL, log_thread,
2407 &s->thread_args);
2408 if (errcode)
2409 return got_error_set_errno(errcode, "pthread_create");
2410 if (s->thread_args.commits_needed > 0) {
2411 err = trigger_log_thread(view, 1);
2412 if (err)
2413 return err;
2417 return draw_commits(view);
2420 static const struct got_error *
2421 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2423 const struct got_error *err = NULL;
2424 struct tog_log_view_state *s = &view->state.log;
2425 struct tog_view *diff_view = NULL, *tree_view = NULL;
2426 struct tog_view *ref_view = NULL;
2427 struct commit_queue_entry *entry;
2428 int begin_x = 0, n;
2430 if (s->thread_args.load_all) {
2431 if (ch == KEY_BACKSPACE)
2432 s->thread_args.load_all = 0;
2433 else if (s->thread_args.log_complete) {
2434 s->thread_args.load_all = 0;
2435 log_scroll_down(view, s->commits.ncommits);
2436 s->selected = MIN(view->nlines - 2,
2437 s->commits.ncommits - 1);
2438 select_commit(s);
2440 return NULL;
2443 switch (ch) {
2444 case 'q':
2445 s->quit = 1;
2446 break;
2447 case 'k':
2448 case KEY_UP:
2449 case '<':
2450 case ',':
2451 case CTRL('p'):
2452 if (s->first_displayed_entry == NULL)
2453 break;
2454 if (s->selected > 0)
2455 s->selected--;
2456 else
2457 log_scroll_up(s, 1);
2458 select_commit(s);
2459 break;
2460 case 'g':
2461 case KEY_HOME:
2462 s->selected = 0;
2463 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2464 select_commit(s);
2465 break;
2466 case KEY_PPAGE:
2467 case CTRL('b'):
2468 if (s->first_displayed_entry == NULL)
2469 break;
2470 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2471 s->selected = 0;
2472 else
2473 log_scroll_up(s, view->nlines - 1);
2474 select_commit(s);
2475 break;
2476 case 'j':
2477 case KEY_DOWN:
2478 case '>':
2479 case '.':
2480 case CTRL('n'):
2481 if (s->first_displayed_entry == NULL)
2482 break;
2483 if (s->selected < MIN(view->nlines - 2,
2484 s->commits.ncommits - 1))
2485 s->selected++;
2486 else {
2487 err = log_scroll_down(view, 1);
2488 if (err)
2489 break;
2491 select_commit(s);
2492 break;
2493 case 'G':
2494 case KEY_END: {
2495 /* We don't know yet how many commits, so we're forced to
2496 * traverse them all. */
2497 if (!s->thread_args.log_complete) {
2498 s->thread_args.load_all = 1;
2499 return trigger_log_thread(view, 0);
2502 s->selected = 0;
2503 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2504 for (n = 0; n < view->nlines - 1; n++) {
2505 if (entry == NULL)
2506 break;
2507 s->first_displayed_entry = entry;
2508 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2510 if (n > 0)
2511 s->selected = n - 1;
2512 select_commit(s);
2513 break;
2515 case KEY_NPAGE:
2516 case CTRL('f'): {
2517 struct commit_queue_entry *first;
2518 first = s->first_displayed_entry;
2519 if (first == NULL)
2520 break;
2521 err = log_scroll_down(view, view->nlines - 1);
2522 if (err)
2523 break;
2524 if (first == s->first_displayed_entry &&
2525 s->selected < MIN(view->nlines - 2,
2526 s->commits.ncommits - 1)) {
2527 /* can't scroll further down */
2528 s->selected = MIN(view->nlines - 2,
2529 s->commits.ncommits - 1);
2531 select_commit(s);
2532 break;
2534 case KEY_RESIZE:
2535 if (s->selected > view->nlines - 2)
2536 s->selected = view->nlines - 2;
2537 if (s->selected > s->commits.ncommits - 1)
2538 s->selected = s->commits.ncommits - 1;
2539 select_commit(s);
2540 if (s->commits.ncommits < view->nlines - 1 &&
2541 !s->thread_args.log_complete) {
2542 s->thread_args.commits_needed += (view->nlines - 1) -
2543 s->commits.ncommits;
2544 err = trigger_log_thread(view, 1);
2546 break;
2547 case KEY_ENTER:
2548 case ' ':
2549 case '\r':
2550 if (s->selected_entry == NULL)
2551 break;
2552 if (view_is_parent_view(view))
2553 begin_x = view_split_begin_x(view->begin_x);
2554 err = open_diff_view_for_commit(&diff_view, begin_x,
2555 s->selected_entry->commit, s->selected_entry->id,
2556 view, s->repo);
2557 if (err)
2558 break;
2559 view->focussed = 0;
2560 diff_view->focussed = 1;
2561 if (view_is_parent_view(view)) {
2562 err = view_close_child(view);
2563 if (err)
2564 return err;
2565 view_set_child(view, diff_view);
2566 view->focus_child = 1;
2567 } else
2568 *new_view = diff_view;
2569 break;
2570 case 't':
2571 if (s->selected_entry == NULL)
2572 break;
2573 if (view_is_parent_view(view))
2574 begin_x = view_split_begin_x(view->begin_x);
2575 err = browse_commit_tree(&tree_view, begin_x,
2576 s->selected_entry, s->in_repo_path, s->head_ref_name,
2577 s->repo);
2578 if (err)
2579 break;
2580 view->focussed = 0;
2581 tree_view->focussed = 1;
2582 if (view_is_parent_view(view)) {
2583 err = view_close_child(view);
2584 if (err)
2585 return err;
2586 view_set_child(view, tree_view);
2587 view->focus_child = 1;
2588 } else
2589 *new_view = tree_view;
2590 break;
2591 case KEY_BACKSPACE:
2592 case CTRL('l'):
2593 case 'B':
2594 if (ch == KEY_BACKSPACE &&
2595 got_path_is_root_dir(s->in_repo_path))
2596 break;
2597 err = stop_log_thread(s);
2598 if (err)
2599 return err;
2600 if (ch == KEY_BACKSPACE) {
2601 char *parent_path;
2602 err = got_path_dirname(&parent_path, s->in_repo_path);
2603 if (err)
2604 return err;
2605 free(s->in_repo_path);
2606 s->in_repo_path = parent_path;
2607 s->thread_args.in_repo_path = s->in_repo_path;
2608 } else if (ch == CTRL('l')) {
2609 struct got_object_id *start_id;
2610 err = got_repo_match_object_id(&start_id, NULL,
2611 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2612 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2613 if (err)
2614 return err;
2615 free(s->start_id);
2616 s->start_id = start_id;
2617 s->thread_args.start_id = s->start_id;
2618 } else /* 'B' */
2619 s->log_branches = !s->log_branches;
2621 err = got_repo_open(&s->thread_args.repo,
2622 got_repo_get_path(s->repo), NULL);
2623 if (err)
2624 return err;
2625 tog_free_refs();
2626 err = tog_load_refs(s->repo, 0);
2627 if (err)
2628 return err;
2629 err = got_commit_graph_open(&s->thread_args.graph,
2630 s->in_repo_path, !s->log_branches);
2631 if (err)
2632 return err;
2633 err = got_commit_graph_iter_start(s->thread_args.graph,
2634 s->start_id, s->repo, NULL, NULL);
2635 if (err)
2636 return err;
2637 free_commits(&s->commits);
2638 s->first_displayed_entry = NULL;
2639 s->last_displayed_entry = NULL;
2640 s->selected_entry = NULL;
2641 s->selected = 0;
2642 s->thread_args.log_complete = 0;
2643 s->quit = 0;
2644 s->thread_args.commits_needed = view->nlines;
2645 break;
2646 case 'r':
2647 if (view_is_parent_view(view))
2648 begin_x = view_split_begin_x(view->begin_x);
2649 ref_view = view_open(view->nlines, view->ncols,
2650 view->begin_y, begin_x, TOG_VIEW_REF);
2651 if (ref_view == NULL)
2652 return got_error_from_errno("view_open");
2653 err = open_ref_view(ref_view, s->repo);
2654 if (err) {
2655 view_close(ref_view);
2656 return err;
2658 view->focussed = 0;
2659 ref_view->focussed = 1;
2660 if (view_is_parent_view(view)) {
2661 err = view_close_child(view);
2662 if (err)
2663 return err;
2664 view_set_child(view, ref_view);
2665 view->focus_child = 1;
2666 } else
2667 *new_view = ref_view;
2668 break;
2669 default:
2670 break;
2673 return err;
2676 static const struct got_error *
2677 apply_unveil(const char *repo_path, const char *worktree_path)
2679 const struct got_error *error;
2681 #ifdef PROFILE
2682 if (unveil("gmon.out", "rwc") != 0)
2683 return got_error_from_errno2("unveil", "gmon.out");
2684 #endif
2685 if (repo_path && unveil(repo_path, "r") != 0)
2686 return got_error_from_errno2("unveil", repo_path);
2688 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2689 return got_error_from_errno2("unveil", worktree_path);
2691 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2692 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2694 error = got_privsep_unveil_exec_helpers();
2695 if (error != NULL)
2696 return error;
2698 if (unveil(NULL, NULL) != 0)
2699 return got_error_from_errno("unveil");
2701 return NULL;
2704 static void
2705 init_curses(void)
2707 initscr();
2708 cbreak();
2709 halfdelay(1); /* Do fast refresh while initial view is loading. */
2710 noecho();
2711 nonl();
2712 intrflush(stdscr, FALSE);
2713 keypad(stdscr, TRUE);
2714 curs_set(0);
2715 if (getenv("TOG_COLORS") != NULL) {
2716 start_color();
2717 use_default_colors();
2719 signal(SIGWINCH, tog_sigwinch);
2720 signal(SIGPIPE, tog_sigpipe);
2721 signal(SIGCONT, tog_sigcont);
2724 static const struct got_error *
2725 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2726 struct got_repository *repo, struct got_worktree *worktree)
2728 const struct got_error *err = NULL;
2730 if (argc == 0) {
2731 *in_repo_path = strdup("/");
2732 if (*in_repo_path == NULL)
2733 return got_error_from_errno("strdup");
2734 return NULL;
2737 if (worktree) {
2738 const char *prefix = got_worktree_get_path_prefix(worktree);
2739 char *p;
2741 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2742 if (err)
2743 return err;
2744 if (asprintf(in_repo_path, "%s%s%s", prefix,
2745 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2746 p) == -1) {
2747 err = got_error_from_errno("asprintf");
2748 *in_repo_path = NULL;
2750 free(p);
2751 } else
2752 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2754 return err;
2757 static const struct got_error *
2758 cmd_log(int argc, char *argv[])
2760 const struct got_error *error;
2761 struct got_repository *repo = NULL;
2762 struct got_worktree *worktree = NULL;
2763 struct got_object_id *start_id = NULL;
2764 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2765 char *start_commit = NULL, *label = NULL;
2766 struct got_reference *ref = NULL;
2767 const char *head_ref_name = NULL;
2768 int ch, log_branches = 0;
2769 struct tog_view *view;
2771 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2772 switch (ch) {
2773 case 'b':
2774 log_branches = 1;
2775 break;
2776 case 'c':
2777 start_commit = optarg;
2778 break;
2779 case 'r':
2780 repo_path = realpath(optarg, NULL);
2781 if (repo_path == NULL)
2782 return got_error_from_errno2("realpath",
2783 optarg);
2784 break;
2785 default:
2786 usage_log();
2787 /* NOTREACHED */
2791 argc -= optind;
2792 argv += optind;
2794 if (argc > 1)
2795 usage_log();
2797 if (repo_path == NULL) {
2798 cwd = getcwd(NULL, 0);
2799 if (cwd == NULL)
2800 return got_error_from_errno("getcwd");
2801 error = got_worktree_open(&worktree, cwd);
2802 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2803 goto done;
2804 if (worktree)
2805 repo_path =
2806 strdup(got_worktree_get_repo_path(worktree));
2807 else
2808 repo_path = strdup(cwd);
2809 if (repo_path == NULL) {
2810 error = got_error_from_errno("strdup");
2811 goto done;
2815 error = got_repo_open(&repo, repo_path, NULL);
2816 if (error != NULL)
2817 goto done;
2819 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2820 repo, worktree);
2821 if (error)
2822 goto done;
2824 init_curses();
2826 error = apply_unveil(got_repo_get_path(repo),
2827 worktree ? got_worktree_get_root_path(worktree) : NULL);
2828 if (error)
2829 goto done;
2831 /* already loaded by tog_log_with_path()? */
2832 if (TAILQ_EMPTY(&tog_refs)) {
2833 error = tog_load_refs(repo, 0);
2834 if (error)
2835 goto done;
2838 if (start_commit == NULL) {
2839 error = got_repo_match_object_id(&start_id, &label,
2840 worktree ? got_worktree_get_head_ref_name(worktree) :
2841 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2842 if (error)
2843 goto done;
2844 head_ref_name = label;
2845 } else {
2846 error = got_ref_open(&ref, repo, start_commit, 0);
2847 if (error == NULL)
2848 head_ref_name = got_ref_get_name(ref);
2849 else if (error->code != GOT_ERR_NOT_REF)
2850 goto done;
2851 error = got_repo_match_object_id(&start_id, NULL,
2852 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2853 if (error)
2854 goto done;
2857 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2858 if (view == NULL) {
2859 error = got_error_from_errno("view_open");
2860 goto done;
2862 error = open_log_view(view, start_id, repo, head_ref_name,
2863 in_repo_path, log_branches);
2864 if (error)
2865 goto done;
2866 if (worktree) {
2867 /* Release work tree lock. */
2868 got_worktree_close(worktree);
2869 worktree = NULL;
2871 error = view_loop(view);
2872 done:
2873 free(in_repo_path);
2874 free(repo_path);
2875 free(cwd);
2876 free(start_id);
2877 free(label);
2878 if (ref)
2879 got_ref_close(ref);
2880 if (repo) {
2881 const struct got_error *close_err = got_repo_close(repo);
2882 if (error == NULL)
2883 error = close_err;
2885 if (worktree)
2886 got_worktree_close(worktree);
2887 tog_free_refs();
2888 return error;
2891 __dead static void
2892 usage_diff(void)
2894 endwin();
2895 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2896 "[-w] object1 object2\n", getprogname());
2897 exit(1);
2900 static int
2901 match_line(const char *line, regex_t *regex, size_t nmatch,
2902 regmatch_t *regmatch)
2904 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2907 struct tog_color *
2908 match_color(struct tog_colors *colors, const char *line)
2910 struct tog_color *tc = NULL;
2912 STAILQ_FOREACH(tc, colors, entry) {
2913 if (match_line(line, &tc->regex, 0, NULL))
2914 return tc;
2917 return NULL;
2920 static const struct got_error *
2921 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2922 WINDOW *window, regmatch_t *regmatch)
2924 const struct got_error *err = NULL;
2925 wchar_t *wline;
2926 int width;
2927 char *s;
2929 *wtotal = 0;
2931 s = strndup(line, regmatch->rm_so);
2932 if (s == NULL)
2933 return got_error_from_errno("strndup");
2935 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2936 if (err) {
2937 free(s);
2938 return err;
2940 waddwstr(window, wline);
2941 free(wline);
2942 free(s);
2943 wlimit -= width;
2944 *wtotal += width;
2946 if (wlimit > 0) {
2947 s = strndup(line + regmatch->rm_so,
2948 regmatch->rm_eo - regmatch->rm_so);
2949 if (s == NULL) {
2950 err = got_error_from_errno("strndup");
2951 free(s);
2952 return err;
2954 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2955 if (err) {
2956 free(s);
2957 return err;
2959 wattr_on(window, A_STANDOUT, NULL);
2960 waddwstr(window, wline);
2961 wattr_off(window, A_STANDOUT, NULL);
2962 free(wline);
2963 free(s);
2964 wlimit -= width;
2965 *wtotal += width;
2968 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2969 err = format_line(&wline, &width,
2970 line + regmatch->rm_eo, wlimit, col_tab_align);
2971 if (err)
2972 return err;
2973 waddwstr(window, wline);
2974 free(wline);
2975 *wtotal += width;
2978 return NULL;
2981 static const struct got_error *
2982 draw_file(struct tog_view *view, const char *header)
2984 struct tog_diff_view_state *s = &view->state.diff;
2985 regmatch_t *regmatch = &view->regmatch;
2986 const struct got_error *err;
2987 int nprinted = 0;
2988 char *line;
2989 size_t linesize = 0;
2990 ssize_t linelen;
2991 struct tog_color *tc;
2992 wchar_t *wline;
2993 int width;
2994 int max_lines = view->nlines;
2995 int nlines = s->nlines;
2996 off_t line_offset;
2998 line_offset = s->line_offsets[s->first_displayed_line - 1];
2999 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3000 return got_error_from_errno("fseek");
3002 werase(view->window);
3004 if (header) {
3005 if (asprintf(&line, "[%d/%d] %s",
3006 s->first_displayed_line - 1 + s->selected_line, nlines,
3007 header) == -1)
3008 return got_error_from_errno("asprintf");
3009 err = format_line(&wline, &width, line, view->ncols, 0);
3010 free(line);
3011 if (err)
3012 return err;
3014 if (view_needs_focus_indication(view))
3015 wstandout(view->window);
3016 waddwstr(view->window, wline);
3017 free(wline);
3018 wline = NULL;
3019 if (view_needs_focus_indication(view))
3020 wstandend(view->window);
3021 if (width <= view->ncols - 1)
3022 waddch(view->window, '\n');
3024 if (max_lines <= 1)
3025 return NULL;
3026 max_lines--;
3029 s->eof = 0;
3030 line = NULL;
3031 while (max_lines > 0 && nprinted < max_lines) {
3032 linelen = getline(&line, &linesize, s->f);
3033 if (linelen == -1) {
3034 if (feof(s->f)) {
3035 s->eof = 1;
3036 break;
3038 free(line);
3039 return got_ferror(s->f, GOT_ERR_IO);
3042 tc = match_color(&s->colors, line);
3043 if (tc)
3044 wattr_on(view->window,
3045 COLOR_PAIR(tc->colorpair), NULL);
3046 if (s->first_displayed_line + nprinted == s->matched_line &&
3047 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3048 err = add_matched_line(&width, line, view->ncols, 0,
3049 view->window, regmatch);
3050 if (err) {
3051 free(line);
3052 return err;
3054 } else {
3055 err = format_line(&wline, &width, line, view->ncols, 0);
3056 if (err) {
3057 free(line);
3058 return err;
3060 waddwstr(view->window, wline);
3061 free(wline);
3062 wline = NULL;
3064 if (tc)
3065 wattr_off(view->window,
3066 COLOR_PAIR(tc->colorpair), NULL);
3067 if (width <= view->ncols - 1)
3068 waddch(view->window, '\n');
3069 nprinted++;
3071 free(line);
3072 if (nprinted >= 1)
3073 s->last_displayed_line = s->first_displayed_line +
3074 (nprinted - 1);
3075 else
3076 s->last_displayed_line = s->first_displayed_line;
3078 view_vborder(view);
3080 if (s->eof) {
3081 while (nprinted < view->nlines) {
3082 waddch(view->window, '\n');
3083 nprinted++;
3086 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3087 if (err) {
3088 return err;
3091 wstandout(view->window);
3092 waddwstr(view->window, wline);
3093 free(wline);
3094 wline = NULL;
3095 wstandend(view->window);
3098 return NULL;
3101 static char *
3102 get_datestr(time_t *time, char *datebuf)
3104 struct tm mytm, *tm;
3105 char *p, *s;
3107 tm = gmtime_r(time, &mytm);
3108 if (tm == NULL)
3109 return NULL;
3110 s = asctime_r(tm, datebuf);
3111 if (s == NULL)
3112 return NULL;
3113 p = strchr(s, '\n');
3114 if (p)
3115 *p = '\0';
3116 return s;
3119 static const struct got_error *
3120 get_changed_paths(struct got_pathlist_head *paths,
3121 struct got_commit_object *commit, struct got_repository *repo)
3123 const struct got_error *err = NULL;
3124 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3125 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3126 struct got_object_qid *qid;
3128 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3129 if (qid != NULL) {
3130 struct got_commit_object *pcommit;
3131 err = got_object_open_as_commit(&pcommit, repo,
3132 &qid->id);
3133 if (err)
3134 return err;
3136 tree_id1 = got_object_id_dup(
3137 got_object_commit_get_tree_id(pcommit));
3138 if (tree_id1 == NULL) {
3139 got_object_commit_close(pcommit);
3140 return got_error_from_errno("got_object_id_dup");
3142 got_object_commit_close(pcommit);
3146 if (tree_id1) {
3147 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3148 if (err)
3149 goto done;
3152 tree_id2 = got_object_commit_get_tree_id(commit);
3153 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3154 if (err)
3155 goto done;
3157 err = got_diff_tree(tree1, tree2, "", "", repo,
3158 got_diff_tree_collect_changed_paths, paths, 0);
3159 done:
3160 if (tree1)
3161 got_object_tree_close(tree1);
3162 if (tree2)
3163 got_object_tree_close(tree2);
3164 free(tree_id1);
3165 return err;
3168 static const struct got_error *
3169 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3171 off_t *p;
3173 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3174 if (p == NULL)
3175 return got_error_from_errno("reallocarray");
3176 *line_offsets = p;
3177 (*line_offsets)[*nlines] = off;
3178 (*nlines)++;
3179 return NULL;
3182 static const struct got_error *
3183 write_commit_info(off_t **line_offsets, size_t *nlines,
3184 struct got_object_id *commit_id, struct got_reflist_head *refs,
3185 struct got_repository *repo, FILE *outfile)
3187 const struct got_error *err = NULL;
3188 char datebuf[26], *datestr;
3189 struct got_commit_object *commit;
3190 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3191 time_t committer_time;
3192 const char *author, *committer;
3193 char *refs_str = NULL;
3194 struct got_pathlist_head changed_paths;
3195 struct got_pathlist_entry *pe;
3196 off_t outoff = 0;
3197 int n;
3199 TAILQ_INIT(&changed_paths);
3201 if (refs) {
3202 err = build_refs_str(&refs_str, refs, commit_id, repo);
3203 if (err)
3204 return err;
3207 err = got_object_open_as_commit(&commit, repo, commit_id);
3208 if (err)
3209 return err;
3211 err = got_object_id_str(&id_str, commit_id);
3212 if (err) {
3213 err = got_error_from_errno("got_object_id_str");
3214 goto done;
3217 err = add_line_offset(line_offsets, nlines, 0);
3218 if (err)
3219 goto done;
3221 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3222 refs_str ? refs_str : "", refs_str ? ")" : "");
3223 if (n < 0) {
3224 err = got_error_from_errno("fprintf");
3225 goto done;
3227 outoff += n;
3228 err = add_line_offset(line_offsets, nlines, outoff);
3229 if (err)
3230 goto done;
3232 n = fprintf(outfile, "from: %s\n",
3233 got_object_commit_get_author(commit));
3234 if (n < 0) {
3235 err = got_error_from_errno("fprintf");
3236 goto done;
3238 outoff += n;
3239 err = add_line_offset(line_offsets, nlines, outoff);
3240 if (err)
3241 goto done;
3243 committer_time = got_object_commit_get_committer_time(commit);
3244 datestr = get_datestr(&committer_time, datebuf);
3245 if (datestr) {
3246 n = fprintf(outfile, "date: %s UTC\n", datestr);
3247 if (n < 0) {
3248 err = got_error_from_errno("fprintf");
3249 goto done;
3251 outoff += n;
3252 err = add_line_offset(line_offsets, nlines, outoff);
3253 if (err)
3254 goto done;
3256 author = got_object_commit_get_author(commit);
3257 committer = got_object_commit_get_committer(commit);
3258 if (strcmp(author, committer) != 0) {
3259 n = fprintf(outfile, "via: %s\n", committer);
3260 if (n < 0) {
3261 err = got_error_from_errno("fprintf");
3262 goto done;
3264 outoff += n;
3265 err = add_line_offset(line_offsets, nlines, outoff);
3266 if (err)
3267 goto done;
3269 if (got_object_commit_get_nparents(commit) > 1) {
3270 const struct got_object_id_queue *parent_ids;
3271 struct got_object_qid *qid;
3272 int pn = 1;
3273 parent_ids = got_object_commit_get_parent_ids(commit);
3274 STAILQ_FOREACH(qid, parent_ids, entry) {
3275 err = got_object_id_str(&id_str, &qid->id);
3276 if (err)
3277 goto done;
3278 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3279 if (n < 0) {
3280 err = got_error_from_errno("fprintf");
3281 goto done;
3283 outoff += n;
3284 err = add_line_offset(line_offsets, nlines, outoff);
3285 if (err)
3286 goto done;
3287 free(id_str);
3288 id_str = NULL;
3292 err = got_object_commit_get_logmsg(&logmsg, commit);
3293 if (err)
3294 goto done;
3295 s = logmsg;
3296 while ((line = strsep(&s, "\n")) != NULL) {
3297 n = fprintf(outfile, "%s\n", line);
3298 if (n < 0) {
3299 err = got_error_from_errno("fprintf");
3300 goto done;
3302 outoff += n;
3303 err = add_line_offset(line_offsets, nlines, outoff);
3304 if (err)
3305 goto done;
3308 err = get_changed_paths(&changed_paths, commit, repo);
3309 if (err)
3310 goto done;
3311 TAILQ_FOREACH(pe, &changed_paths, entry) {
3312 struct got_diff_changed_path *cp = pe->data;
3313 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3314 if (n < 0) {
3315 err = got_error_from_errno("fprintf");
3316 goto done;
3318 outoff += n;
3319 err = add_line_offset(line_offsets, nlines, outoff);
3320 if (err)
3321 goto done;
3322 free((char *)pe->path);
3323 free(pe->data);
3326 fputc('\n', outfile);
3327 outoff++;
3328 err = add_line_offset(line_offsets, nlines, outoff);
3329 done:
3330 got_pathlist_free(&changed_paths);
3331 free(id_str);
3332 free(logmsg);
3333 free(refs_str);
3334 got_object_commit_close(commit);
3335 if (err) {
3336 free(*line_offsets);
3337 *line_offsets = NULL;
3338 *nlines = 0;
3340 return err;
3343 static const struct got_error *
3344 create_diff(struct tog_diff_view_state *s)
3346 const struct got_error *err = NULL;
3347 FILE *f = NULL;
3348 int obj_type;
3350 free(s->line_offsets);
3351 s->line_offsets = malloc(sizeof(off_t));
3352 if (s->line_offsets == NULL)
3353 return got_error_from_errno("malloc");
3354 s->nlines = 0;
3356 f = got_opentemp();
3357 if (f == NULL) {
3358 err = got_error_from_errno("got_opentemp");
3359 goto done;
3361 if (s->f && fclose(s->f) == EOF) {
3362 err = got_error_from_errno("fclose");
3363 goto done;
3365 s->f = f;
3367 if (s->id1)
3368 err = got_object_get_type(&obj_type, s->repo, s->id1);
3369 else
3370 err = got_object_get_type(&obj_type, s->repo, s->id2);
3371 if (err)
3372 goto done;
3374 switch (obj_type) {
3375 case GOT_OBJ_TYPE_BLOB:
3376 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3377 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3378 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3379 break;
3380 case GOT_OBJ_TYPE_TREE:
3381 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3382 s->id1, s->id2, NULL, "", "", s->diff_context,
3383 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3384 break;
3385 case GOT_OBJ_TYPE_COMMIT: {
3386 const struct got_object_id_queue *parent_ids;
3387 struct got_object_qid *pid;
3388 struct got_commit_object *commit2;
3389 struct got_reflist_head *refs;
3391 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3392 if (err)
3393 goto done;
3394 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3395 /* Show commit info if we're diffing to a parent/root commit. */
3396 if (s->id1 == NULL) {
3397 err = write_commit_info(&s->line_offsets, &s->nlines,
3398 s->id2, refs, s->repo, s->f);
3399 if (err)
3400 goto done;
3401 } else {
3402 parent_ids = got_object_commit_get_parent_ids(commit2);
3403 STAILQ_FOREACH(pid, parent_ids, entry) {
3404 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3405 err = write_commit_info(
3406 &s->line_offsets, &s->nlines,
3407 s->id2, refs, s->repo, s->f);
3408 if (err)
3409 goto done;
3410 break;
3414 got_object_commit_close(commit2);
3416 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3417 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3418 s->force_text_diff, s->repo, s->f);
3419 break;
3421 default:
3422 err = got_error(GOT_ERR_OBJ_TYPE);
3423 break;
3425 if (err)
3426 goto done;
3427 done:
3428 if (s->f && fflush(s->f) != 0 && err == NULL)
3429 err = got_error_from_errno("fflush");
3430 return err;
3433 static void
3434 diff_view_indicate_progress(struct tog_view *view)
3436 mvwaddstr(view->window, 0, 0, "diffing...");
3437 update_panels();
3438 doupdate();
3441 static const struct got_error *
3442 search_start_diff_view(struct tog_view *view)
3444 struct tog_diff_view_state *s = &view->state.diff;
3446 s->matched_line = 0;
3447 return NULL;
3450 static const struct got_error *
3451 search_next_diff_view(struct tog_view *view)
3453 struct tog_diff_view_state *s = &view->state.diff;
3454 int lineno;
3455 char *line = NULL;
3456 size_t linesize = 0;
3457 ssize_t linelen;
3459 if (!view->searching) {
3460 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3461 return NULL;
3464 if (s->matched_line) {
3465 if (view->searching == TOG_SEARCH_FORWARD)
3466 lineno = s->matched_line + 1;
3467 else
3468 lineno = s->matched_line - 1;
3469 } else
3470 lineno = s->first_displayed_line;
3472 while (1) {
3473 off_t offset;
3475 if (lineno <= 0 || lineno > s->nlines) {
3476 if (s->matched_line == 0) {
3477 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3478 break;
3481 if (view->searching == TOG_SEARCH_FORWARD)
3482 lineno = 1;
3483 else
3484 lineno = s->nlines;
3487 offset = s->line_offsets[lineno - 1];
3488 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3489 free(line);
3490 return got_error_from_errno("fseeko");
3492 linelen = getline(&line, &linesize, s->f);
3493 if (linelen != -1 &&
3494 match_line(line, &view->regex, 1, &view->regmatch)) {
3495 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3496 s->matched_line = lineno;
3497 break;
3499 if (view->searching == TOG_SEARCH_FORWARD)
3500 lineno++;
3501 else
3502 lineno--;
3504 free(line);
3506 if (s->matched_line) {
3507 s->first_displayed_line = s->matched_line;
3508 s->selected_line = 1;
3511 return NULL;
3514 static const struct got_error *
3515 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3516 struct got_object_id *id2, const char *label1, const char *label2,
3517 int diff_context, int ignore_whitespace, int force_text_diff,
3518 struct tog_view *log_view, struct got_repository *repo)
3520 const struct got_error *err;
3521 struct tog_diff_view_state *s = &view->state.diff;
3523 if (id1 != NULL && id2 != NULL) {
3524 int type1, type2;
3525 err = got_object_get_type(&type1, repo, id1);
3526 if (err)
3527 return err;
3528 err = got_object_get_type(&type2, repo, id2);
3529 if (err)
3530 return err;
3532 if (type1 != type2)
3533 return got_error(GOT_ERR_OBJ_TYPE);
3535 s->first_displayed_line = 1;
3536 s->last_displayed_line = view->nlines;
3537 s->selected_line = 1;
3538 s->repo = repo;
3539 s->id1 = id1;
3540 s->id2 = id2;
3541 s->label1 = label1;
3542 s->label2 = label2;
3544 if (id1) {
3545 s->id1 = got_object_id_dup(id1);
3546 if (s->id1 == NULL)
3547 return got_error_from_errno("got_object_id_dup");
3548 } else
3549 s->id1 = NULL;
3551 s->id2 = got_object_id_dup(id2);
3552 if (s->id2 == NULL) {
3553 free(s->id1);
3554 s->id1 = NULL;
3555 return got_error_from_errno("got_object_id_dup");
3557 s->f = NULL;
3558 s->first_displayed_line = 1;
3559 s->last_displayed_line = view->nlines;
3560 s->diff_context = diff_context;
3561 s->ignore_whitespace = ignore_whitespace;
3562 s->force_text_diff = force_text_diff;
3563 s->log_view = log_view;
3564 s->repo = repo;
3566 STAILQ_INIT(&s->colors);
3567 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3568 err = add_color(&s->colors,
3569 "^-", TOG_COLOR_DIFF_MINUS,
3570 get_color_value("TOG_COLOR_DIFF_MINUS"));
3571 if (err)
3572 return err;
3573 err = add_color(&s->colors, "^\\+",
3574 TOG_COLOR_DIFF_PLUS,
3575 get_color_value("TOG_COLOR_DIFF_PLUS"));
3576 if (err) {
3577 free_colors(&s->colors);
3578 return err;
3580 err = add_color(&s->colors,
3581 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3582 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3583 if (err) {
3584 free_colors(&s->colors);
3585 return err;
3588 err = add_color(&s->colors,
3589 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3590 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3591 get_color_value("TOG_COLOR_DIFF_META"));
3592 if (err) {
3593 free_colors(&s->colors);
3594 return err;
3597 err = add_color(&s->colors,
3598 "^(from|via): ", TOG_COLOR_AUTHOR,
3599 get_color_value("TOG_COLOR_AUTHOR"));
3600 if (err) {
3601 free_colors(&s->colors);
3602 return err;
3605 err = add_color(&s->colors,
3606 "^date: ", TOG_COLOR_DATE,
3607 get_color_value("TOG_COLOR_DATE"));
3608 if (err) {
3609 free_colors(&s->colors);
3610 return err;
3614 if (log_view && view_is_splitscreen(view))
3615 show_log_view(log_view); /* draw vborder */
3616 diff_view_indicate_progress(view);
3618 s->line_offsets = NULL;
3619 s->nlines = 0;
3620 err = create_diff(s);
3621 if (err) {
3622 free(s->id1);
3623 s->id1 = NULL;
3624 free(s->id2);
3625 s->id2 = NULL;
3626 free_colors(&s->colors);
3627 return err;
3630 view->show = show_diff_view;
3631 view->input = input_diff_view;
3632 view->close = close_diff_view;
3633 view->search_start = search_start_diff_view;
3634 view->search_next = search_next_diff_view;
3636 return NULL;
3639 static const struct got_error *
3640 close_diff_view(struct tog_view *view)
3642 const struct got_error *err = NULL;
3643 struct tog_diff_view_state *s = &view->state.diff;
3645 free(s->id1);
3646 s->id1 = NULL;
3647 free(s->id2);
3648 s->id2 = NULL;
3649 if (s->f && fclose(s->f) == EOF)
3650 err = got_error_from_errno("fclose");
3651 free_colors(&s->colors);
3652 free(s->line_offsets);
3653 s->line_offsets = NULL;
3654 s->nlines = 0;
3655 return err;
3658 static const struct got_error *
3659 show_diff_view(struct tog_view *view)
3661 const struct got_error *err;
3662 struct tog_diff_view_state *s = &view->state.diff;
3663 char *id_str1 = NULL, *id_str2, *header;
3664 const char *label1, *label2;
3666 if (s->id1) {
3667 err = got_object_id_str(&id_str1, s->id1);
3668 if (err)
3669 return err;
3670 label1 = s->label1 ? : id_str1;
3671 } else
3672 label1 = "/dev/null";
3674 err = got_object_id_str(&id_str2, s->id2);
3675 if (err)
3676 return err;
3677 label2 = s->label2 ? : id_str2;
3679 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3680 err = got_error_from_errno("asprintf");
3681 free(id_str1);
3682 free(id_str2);
3683 return err;
3685 free(id_str1);
3686 free(id_str2);
3688 err = draw_file(view, header);
3689 free(header);
3690 return err;
3693 static const struct got_error *
3694 set_selected_commit(struct tog_diff_view_state *s,
3695 struct commit_queue_entry *entry)
3697 const struct got_error *err;
3698 const struct got_object_id_queue *parent_ids;
3699 struct got_commit_object *selected_commit;
3700 struct got_object_qid *pid;
3702 free(s->id2);
3703 s->id2 = got_object_id_dup(entry->id);
3704 if (s->id2 == NULL)
3705 return got_error_from_errno("got_object_id_dup");
3707 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3708 if (err)
3709 return err;
3710 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3711 free(s->id1);
3712 pid = STAILQ_FIRST(parent_ids);
3713 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3714 got_object_commit_close(selected_commit);
3715 return NULL;
3718 static const struct got_error *
3719 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3721 const struct got_error *err = NULL;
3722 struct tog_diff_view_state *s = &view->state.diff;
3723 struct tog_log_view_state *ls;
3724 struct commit_queue_entry *old_selected_entry;
3725 char *line = NULL;
3726 size_t linesize = 0;
3727 ssize_t linelen;
3728 int i;
3730 switch (ch) {
3731 case 'a':
3732 case 'w':
3733 if (ch == 'a')
3734 s->force_text_diff = !s->force_text_diff;
3735 if (ch == 'w')
3736 s->ignore_whitespace = !s->ignore_whitespace;
3737 wclear(view->window);
3738 s->first_displayed_line = 1;
3739 s->last_displayed_line = view->nlines;
3740 s->matched_line = 0;
3741 diff_view_indicate_progress(view);
3742 err = create_diff(s);
3743 break;
3744 case 'g':
3745 case KEY_HOME:
3746 s->first_displayed_line = 1;
3747 break;
3748 case 'G':
3749 case KEY_END:
3750 if (s->eof)
3751 break;
3753 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3754 s->eof = 1;
3755 break;
3756 case 'k':
3757 case KEY_UP:
3758 case CTRL('p'):
3759 if (s->first_displayed_line > 1)
3760 s->first_displayed_line--;
3761 break;
3762 case KEY_PPAGE:
3763 case CTRL('b'):
3764 if (s->first_displayed_line == 1)
3765 break;
3766 i = 0;
3767 while (i++ < view->nlines - 1 &&
3768 s->first_displayed_line > 1)
3769 s->first_displayed_line--;
3770 break;
3771 case 'j':
3772 case KEY_DOWN:
3773 case CTRL('n'):
3774 if (!s->eof)
3775 s->first_displayed_line++;
3776 break;
3777 case KEY_NPAGE:
3778 case CTRL('f'):
3779 case ' ':
3780 if (s->eof)
3781 break;
3782 i = 0;
3783 while (!s->eof && i++ < view->nlines - 1) {
3784 linelen = getline(&line, &linesize, s->f);
3785 s->first_displayed_line++;
3786 if (linelen == -1) {
3787 if (feof(s->f)) {
3788 s->eof = 1;
3789 } else
3790 err = got_ferror(s->f, GOT_ERR_IO);
3791 break;
3794 free(line);
3795 break;
3796 case '[':
3797 if (s->diff_context > 0) {
3798 s->diff_context--;
3799 s->matched_line = 0;
3800 diff_view_indicate_progress(view);
3801 err = create_diff(s);
3802 if (s->first_displayed_line + view->nlines - 1 >
3803 s->nlines) {
3804 s->first_displayed_line = 1;
3805 s->last_displayed_line = view->nlines;
3808 break;
3809 case ']':
3810 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3811 s->diff_context++;
3812 s->matched_line = 0;
3813 diff_view_indicate_progress(view);
3814 err = create_diff(s);
3816 break;
3817 case '<':
3818 case ',':
3819 if (s->log_view == NULL)
3820 break;
3821 ls = &s->log_view->state.log;
3822 old_selected_entry = ls->selected_entry;
3824 err = input_log_view(NULL, s->log_view, KEY_UP);
3825 if (err)
3826 break;
3828 if (old_selected_entry == ls->selected_entry)
3829 break;
3831 err = set_selected_commit(s, ls->selected_entry);
3832 if (err)
3833 break;
3835 s->first_displayed_line = 1;
3836 s->last_displayed_line = view->nlines;
3837 s->matched_line = 0;
3839 diff_view_indicate_progress(view);
3840 err = create_diff(s);
3841 break;
3842 case '>':
3843 case '.':
3844 if (s->log_view == NULL)
3845 break;
3846 ls = &s->log_view->state.log;
3847 old_selected_entry = ls->selected_entry;
3849 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3850 if (err)
3851 break;
3853 if (old_selected_entry == ls->selected_entry)
3854 break;
3856 err = set_selected_commit(s, ls->selected_entry);
3857 if (err)
3858 break;
3860 s->first_displayed_line = 1;
3861 s->last_displayed_line = view->nlines;
3862 s->matched_line = 0;
3864 diff_view_indicate_progress(view);
3865 err = create_diff(s);
3866 break;
3867 default:
3868 break;
3871 return err;
3874 static const struct got_error *
3875 cmd_diff(int argc, char *argv[])
3877 const struct got_error *error = NULL;
3878 struct got_repository *repo = NULL;
3879 struct got_worktree *worktree = NULL;
3880 struct got_object_id *id1 = NULL, *id2 = NULL;
3881 char *repo_path = NULL, *cwd = NULL;
3882 char *id_str1 = NULL, *id_str2 = NULL;
3883 char *label1 = NULL, *label2 = NULL;
3884 int diff_context = 3, ignore_whitespace = 0;
3885 int ch, force_text_diff = 0;
3886 const char *errstr;
3887 struct tog_view *view;
3889 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3890 switch (ch) {
3891 case 'a':
3892 force_text_diff = 1;
3893 break;
3894 case 'C':
3895 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3896 &errstr);
3897 if (errstr != NULL)
3898 errx(1, "number of context lines is %s: %s",
3899 errstr, errstr);
3900 break;
3901 case 'r':
3902 repo_path = realpath(optarg, NULL);
3903 if (repo_path == NULL)
3904 return got_error_from_errno2("realpath",
3905 optarg);
3906 got_path_strip_trailing_slashes(repo_path);
3907 break;
3908 case 'w':
3909 ignore_whitespace = 1;
3910 break;
3911 default:
3912 usage_diff();
3913 /* NOTREACHED */
3917 argc -= optind;
3918 argv += optind;
3920 if (argc == 0) {
3921 usage_diff(); /* TODO show local worktree changes */
3922 } else if (argc == 2) {
3923 id_str1 = argv[0];
3924 id_str2 = argv[1];
3925 } else
3926 usage_diff();
3928 if (repo_path == NULL) {
3929 cwd = getcwd(NULL, 0);
3930 if (cwd == NULL)
3931 return got_error_from_errno("getcwd");
3932 error = got_worktree_open(&worktree, cwd);
3933 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3934 goto done;
3935 if (worktree)
3936 repo_path =
3937 strdup(got_worktree_get_repo_path(worktree));
3938 else
3939 repo_path = strdup(cwd);
3940 if (repo_path == NULL) {
3941 error = got_error_from_errno("strdup");
3942 goto done;
3946 error = got_repo_open(&repo, repo_path, NULL);
3947 if (error)
3948 goto done;
3950 init_curses();
3952 error = apply_unveil(got_repo_get_path(repo), NULL);
3953 if (error)
3954 goto done;
3956 error = tog_load_refs(repo, 0);
3957 if (error)
3958 goto done;
3960 error = got_repo_match_object_id(&id1, &label1, id_str1,
3961 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3962 if (error)
3963 goto done;
3965 error = got_repo_match_object_id(&id2, &label2, id_str2,
3966 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3967 if (error)
3968 goto done;
3970 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3971 if (view == NULL) {
3972 error = got_error_from_errno("view_open");
3973 goto done;
3975 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3976 ignore_whitespace, force_text_diff, NULL, repo);
3977 if (error)
3978 goto done;
3979 error = view_loop(view);
3980 done:
3981 free(label1);
3982 free(label2);
3983 free(repo_path);
3984 free(cwd);
3985 if (repo) {
3986 const struct got_error *close_err = got_repo_close(repo);
3987 if (error == NULL)
3988 error = close_err;
3990 if (worktree)
3991 got_worktree_close(worktree);
3992 tog_free_refs();
3993 return error;
3996 __dead static void
3997 usage_blame(void)
3999 endwin();
4000 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4001 getprogname());
4002 exit(1);
4005 struct tog_blame_line {
4006 int annotated;
4007 struct got_object_id *id;
4010 static const struct got_error *
4011 draw_blame(struct tog_view *view)
4013 struct tog_blame_view_state *s = &view->state.blame;
4014 struct tog_blame *blame = &s->blame;
4015 regmatch_t *regmatch = &view->regmatch;
4016 const struct got_error *err;
4017 int lineno = 0, nprinted = 0;
4018 char *line = NULL;
4019 size_t linesize = 0;
4020 ssize_t linelen;
4021 wchar_t *wline;
4022 int width;
4023 struct tog_blame_line *blame_line;
4024 struct got_object_id *prev_id = NULL;
4025 char *id_str;
4026 struct tog_color *tc;
4028 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4029 if (err)
4030 return err;
4032 rewind(blame->f);
4033 werase(view->window);
4035 if (asprintf(&line, "commit %s", id_str) == -1) {
4036 err = got_error_from_errno("asprintf");
4037 free(id_str);
4038 return err;
4041 err = format_line(&wline, &width, line, view->ncols, 0);
4042 free(line);
4043 line = NULL;
4044 if (err)
4045 return err;
4046 if (view_needs_focus_indication(view))
4047 wstandout(view->window);
4048 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4049 if (tc)
4050 wattr_on(view->window,
4051 COLOR_PAIR(tc->colorpair), NULL);
4052 waddwstr(view->window, wline);
4053 if (tc)
4054 wattr_off(view->window,
4055 COLOR_PAIR(tc->colorpair), NULL);
4056 if (view_needs_focus_indication(view))
4057 wstandend(view->window);
4058 free(wline);
4059 wline = NULL;
4060 if (width < view->ncols - 1)
4061 waddch(view->window, '\n');
4063 if (asprintf(&line, "[%d/%d] %s%s",
4064 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4065 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4066 free(id_str);
4067 return got_error_from_errno("asprintf");
4069 free(id_str);
4070 err = format_line(&wline, &width, line, view->ncols, 0);
4071 free(line);
4072 line = NULL;
4073 if (err)
4074 return err;
4075 waddwstr(view->window, wline);
4076 free(wline);
4077 wline = NULL;
4078 if (width < view->ncols - 1)
4079 waddch(view->window, '\n');
4081 s->eof = 0;
4082 while (nprinted < view->nlines - 2) {
4083 linelen = getline(&line, &linesize, blame->f);
4084 if (linelen == -1) {
4085 if (feof(blame->f)) {
4086 s->eof = 1;
4087 break;
4089 free(line);
4090 return got_ferror(blame->f, GOT_ERR_IO);
4092 if (++lineno < s->first_displayed_line)
4093 continue;
4095 if (view->focussed && nprinted == s->selected_line - 1)
4096 wstandout(view->window);
4098 if (blame->nlines > 0) {
4099 blame_line = &blame->lines[lineno - 1];
4100 if (blame_line->annotated && prev_id &&
4101 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4102 !(view->focussed &&
4103 nprinted == s->selected_line - 1)) {
4104 waddstr(view->window, " ");
4105 } else if (blame_line->annotated) {
4106 char *id_str;
4107 err = got_object_id_str(&id_str, blame_line->id);
4108 if (err) {
4109 free(line);
4110 return err;
4112 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4113 if (tc)
4114 wattr_on(view->window,
4115 COLOR_PAIR(tc->colorpair), NULL);
4116 wprintw(view->window, "%.8s", id_str);
4117 if (tc)
4118 wattr_off(view->window,
4119 COLOR_PAIR(tc->colorpair), NULL);
4120 free(id_str);
4121 prev_id = blame_line->id;
4122 } else {
4123 waddstr(view->window, "........");
4124 prev_id = NULL;
4126 } else {
4127 waddstr(view->window, "........");
4128 prev_id = NULL;
4131 if (view->focussed && nprinted == s->selected_line - 1)
4132 wstandend(view->window);
4133 waddstr(view->window, " ");
4135 if (view->ncols <= 9) {
4136 width = 9;
4137 wline = wcsdup(L"");
4138 if (wline == NULL) {
4139 err = got_error_from_errno("wcsdup");
4140 free(line);
4141 return err;
4143 } else if (s->first_displayed_line + nprinted ==
4144 s->matched_line &&
4145 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4146 err = add_matched_line(&width, line, view->ncols - 9, 9,
4147 view->window, regmatch);
4148 if (err) {
4149 free(line);
4150 return err;
4152 width += 9;
4153 } else {
4154 err = format_line(&wline, &width, line,
4155 view->ncols - 9, 9);
4156 waddwstr(view->window, wline);
4157 free(wline);
4158 wline = NULL;
4159 width += 9;
4162 if (width <= view->ncols - 1)
4163 waddch(view->window, '\n');
4164 if (++nprinted == 1)
4165 s->first_displayed_line = lineno;
4167 free(line);
4168 s->last_displayed_line = lineno;
4170 view_vborder(view);
4172 return NULL;
4175 static const struct got_error *
4176 blame_cb(void *arg, int nlines, int lineno,
4177 struct got_commit_object *commit, struct got_object_id *id)
4179 const struct got_error *err = NULL;
4180 struct tog_blame_cb_args *a = arg;
4181 struct tog_blame_line *line;
4182 int errcode;
4184 if (nlines != a->nlines ||
4185 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4186 return got_error(GOT_ERR_RANGE);
4188 errcode = pthread_mutex_lock(&tog_mutex);
4189 if (errcode)
4190 return got_error_set_errno(errcode, "pthread_mutex_lock");
4192 if (*a->quit) { /* user has quit the blame view */
4193 err = got_error(GOT_ERR_ITER_COMPLETED);
4194 goto done;
4197 if (lineno == -1)
4198 goto done; /* no change in this commit */
4200 line = &a->lines[lineno - 1];
4201 if (line->annotated)
4202 goto done;
4204 line->id = got_object_id_dup(id);
4205 if (line->id == NULL) {
4206 err = got_error_from_errno("got_object_id_dup");
4207 goto done;
4209 line->annotated = 1;
4210 done:
4211 errcode = pthread_mutex_unlock(&tog_mutex);
4212 if (errcode)
4213 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4214 return err;
4217 static void *
4218 blame_thread(void *arg)
4220 const struct got_error *err, *close_err;
4221 struct tog_blame_thread_args *ta = arg;
4222 struct tog_blame_cb_args *a = ta->cb_args;
4223 int errcode;
4225 err = block_signals_used_by_main_thread();
4226 if (err)
4227 return (void *)err;
4229 err = got_blame(ta->path, a->commit_id, ta->repo,
4230 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4231 if (err && err->code == GOT_ERR_CANCELLED)
4232 err = NULL;
4234 errcode = pthread_mutex_lock(&tog_mutex);
4235 if (errcode)
4236 return (void *)got_error_set_errno(errcode,
4237 "pthread_mutex_lock");
4239 close_err = got_repo_close(ta->repo);
4240 if (err == NULL)
4241 err = close_err;
4242 ta->repo = NULL;
4243 *ta->complete = 1;
4245 errcode = pthread_mutex_unlock(&tog_mutex);
4246 if (errcode && err == NULL)
4247 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4249 return (void *)err;
4252 static struct got_object_id *
4253 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4254 int first_displayed_line, int selected_line)
4256 struct tog_blame_line *line;
4258 if (nlines <= 0)
4259 return NULL;
4261 line = &lines[first_displayed_line - 1 + selected_line - 1];
4262 if (!line->annotated)
4263 return NULL;
4265 return line->id;
4268 static const struct got_error *
4269 stop_blame(struct tog_blame *blame)
4271 const struct got_error *err = NULL;
4272 int i;
4274 if (blame->thread) {
4275 int errcode;
4276 errcode = pthread_mutex_unlock(&tog_mutex);
4277 if (errcode)
4278 return got_error_set_errno(errcode,
4279 "pthread_mutex_unlock");
4280 errcode = pthread_join(blame->thread, (void **)&err);
4281 if (errcode)
4282 return got_error_set_errno(errcode, "pthread_join");
4283 errcode = pthread_mutex_lock(&tog_mutex);
4284 if (errcode)
4285 return got_error_set_errno(errcode,
4286 "pthread_mutex_lock");
4287 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4288 err = NULL;
4289 blame->thread = 0; //NULL;
4291 if (blame->thread_args.repo) {
4292 const struct got_error *close_err;
4293 close_err = got_repo_close(blame->thread_args.repo);
4294 if (err == NULL)
4295 err = close_err;
4296 blame->thread_args.repo = NULL;
4298 if (blame->f) {
4299 if (fclose(blame->f) == EOF && err == NULL)
4300 err = got_error_from_errno("fclose");
4301 blame->f = NULL;
4303 if (blame->lines) {
4304 for (i = 0; i < blame->nlines; i++)
4305 free(blame->lines[i].id);
4306 free(blame->lines);
4307 blame->lines = NULL;
4309 free(blame->cb_args.commit_id);
4310 blame->cb_args.commit_id = NULL;
4312 return err;
4315 static const struct got_error *
4316 cancel_blame_view(void *arg)
4318 const struct got_error *err = NULL;
4319 int *done = arg;
4320 int errcode;
4322 errcode = pthread_mutex_lock(&tog_mutex);
4323 if (errcode)
4324 return got_error_set_errno(errcode,
4325 "pthread_mutex_unlock");
4327 if (*done)
4328 err = got_error(GOT_ERR_CANCELLED);
4330 errcode = pthread_mutex_unlock(&tog_mutex);
4331 if (errcode)
4332 return got_error_set_errno(errcode,
4333 "pthread_mutex_lock");
4335 return err;
4338 static const struct got_error *
4339 run_blame(struct tog_view *view)
4341 struct tog_blame_view_state *s = &view->state.blame;
4342 struct tog_blame *blame = &s->blame;
4343 const struct got_error *err = NULL;
4344 struct got_commit_object *commit = NULL;
4345 struct got_blob_object *blob = NULL;
4346 struct got_repository *thread_repo = NULL;
4347 struct got_object_id *obj_id = NULL;
4348 int obj_type;
4350 err = got_object_open_as_commit(&commit, s->repo,
4351 &s->blamed_commit->id);
4352 if (err)
4353 return err;
4355 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4356 if (err)
4357 goto done;
4359 err = got_object_get_type(&obj_type, s->repo, obj_id);
4360 if (err)
4361 goto done;
4363 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4364 err = got_error(GOT_ERR_OBJ_TYPE);
4365 goto done;
4368 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4369 if (err)
4370 goto done;
4371 blame->f = got_opentemp();
4372 if (blame->f == NULL) {
4373 err = got_error_from_errno("got_opentemp");
4374 goto done;
4376 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4377 &blame->line_offsets, blame->f, blob);
4378 if (err)
4379 goto done;
4380 if (blame->nlines == 0) {
4381 s->blame_complete = 1;
4382 goto done;
4385 /* Don't include \n at EOF in the blame line count. */
4386 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4387 blame->nlines--;
4389 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4390 if (blame->lines == NULL) {
4391 err = got_error_from_errno("calloc");
4392 goto done;
4395 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4396 if (err)
4397 goto done;
4399 blame->cb_args.view = view;
4400 blame->cb_args.lines = blame->lines;
4401 blame->cb_args.nlines = blame->nlines;
4402 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4403 if (blame->cb_args.commit_id == NULL) {
4404 err = got_error_from_errno("got_object_id_dup");
4405 goto done;
4407 blame->cb_args.quit = &s->done;
4409 blame->thread_args.path = s->path;
4410 blame->thread_args.repo = thread_repo;
4411 blame->thread_args.cb_args = &blame->cb_args;
4412 blame->thread_args.complete = &s->blame_complete;
4413 blame->thread_args.cancel_cb = cancel_blame_view;
4414 blame->thread_args.cancel_arg = &s->done;
4415 s->blame_complete = 0;
4417 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4418 s->first_displayed_line = 1;
4419 s->last_displayed_line = view->nlines;
4420 s->selected_line = 1;
4422 s->matched_line = 0;
4424 done:
4425 if (commit)
4426 got_object_commit_close(commit);
4427 if (blob)
4428 got_object_blob_close(blob);
4429 free(obj_id);
4430 if (err)
4431 stop_blame(blame);
4432 return err;
4435 static const struct got_error *
4436 open_blame_view(struct tog_view *view, char *path,
4437 struct got_object_id *commit_id, struct got_repository *repo)
4439 const struct got_error *err = NULL;
4440 struct tog_blame_view_state *s = &view->state.blame;
4442 STAILQ_INIT(&s->blamed_commits);
4444 s->path = strdup(path);
4445 if (s->path == NULL)
4446 return got_error_from_errno("strdup");
4448 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4449 if (err) {
4450 free(s->path);
4451 return err;
4454 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4455 s->first_displayed_line = 1;
4456 s->last_displayed_line = view->nlines;
4457 s->selected_line = 1;
4458 s->blame_complete = 0;
4459 s->repo = repo;
4460 s->commit_id = commit_id;
4461 memset(&s->blame, 0, sizeof(s->blame));
4463 STAILQ_INIT(&s->colors);
4464 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4465 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4466 get_color_value("TOG_COLOR_COMMIT"));
4467 if (err)
4468 return err;
4471 view->show = show_blame_view;
4472 view->input = input_blame_view;
4473 view->close = close_blame_view;
4474 view->search_start = search_start_blame_view;
4475 view->search_next = search_next_blame_view;
4477 return run_blame(view);
4480 static const struct got_error *
4481 close_blame_view(struct tog_view *view)
4483 const struct got_error *err = NULL;
4484 struct tog_blame_view_state *s = &view->state.blame;
4486 if (s->blame.thread)
4487 err = stop_blame(&s->blame);
4489 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4490 struct got_object_qid *blamed_commit;
4491 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4492 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4493 got_object_qid_free(blamed_commit);
4496 free(s->path);
4497 free_colors(&s->colors);
4499 return err;
4502 static const struct got_error *
4503 search_start_blame_view(struct tog_view *view)
4505 struct tog_blame_view_state *s = &view->state.blame;
4507 s->matched_line = 0;
4508 return NULL;
4511 static const struct got_error *
4512 search_next_blame_view(struct tog_view *view)
4514 struct tog_blame_view_state *s = &view->state.blame;
4515 int lineno;
4516 char *line = NULL;
4517 size_t linesize = 0;
4518 ssize_t linelen;
4520 if (!view->searching) {
4521 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4522 return NULL;
4525 if (s->matched_line) {
4526 if (view->searching == TOG_SEARCH_FORWARD)
4527 lineno = s->matched_line + 1;
4528 else
4529 lineno = s->matched_line - 1;
4530 } else
4531 lineno = s->first_displayed_line - 1 + s->selected_line;
4533 while (1) {
4534 off_t offset;
4536 if (lineno <= 0 || lineno > s->blame.nlines) {
4537 if (s->matched_line == 0) {
4538 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4539 break;
4542 if (view->searching == TOG_SEARCH_FORWARD)
4543 lineno = 1;
4544 else
4545 lineno = s->blame.nlines;
4548 offset = s->blame.line_offsets[lineno - 1];
4549 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4550 free(line);
4551 return got_error_from_errno("fseeko");
4553 linelen = getline(&line, &linesize, s->blame.f);
4554 if (linelen != -1 &&
4555 match_line(line, &view->regex, 1, &view->regmatch)) {
4556 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4557 s->matched_line = lineno;
4558 break;
4560 if (view->searching == TOG_SEARCH_FORWARD)
4561 lineno++;
4562 else
4563 lineno--;
4565 free(line);
4567 if (s->matched_line) {
4568 s->first_displayed_line = s->matched_line;
4569 s->selected_line = 1;
4572 return NULL;
4575 static const struct got_error *
4576 show_blame_view(struct tog_view *view)
4578 const struct got_error *err = NULL;
4579 struct tog_blame_view_state *s = &view->state.blame;
4580 int errcode;
4582 if (s->blame.thread == 0 && !s->blame_complete) {
4583 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4584 &s->blame.thread_args);
4585 if (errcode)
4586 return got_error_set_errno(errcode, "pthread_create");
4588 halfdelay(1); /* fast refresh while annotating */
4591 if (s->blame_complete)
4592 halfdelay(10); /* disable fast refresh */
4594 err = draw_blame(view);
4596 view_vborder(view);
4597 return err;
4600 static const struct got_error *
4601 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4603 const struct got_error *err = NULL, *thread_err = NULL;
4604 struct tog_view *diff_view;
4605 struct tog_blame_view_state *s = &view->state.blame;
4606 int begin_x = 0;
4608 switch (ch) {
4609 case 'q':
4610 s->done = 1;
4611 break;
4612 case 'g':
4613 case KEY_HOME:
4614 s->selected_line = 1;
4615 s->first_displayed_line = 1;
4616 break;
4617 case 'G':
4618 case KEY_END:
4619 if (s->blame.nlines < view->nlines - 2) {
4620 s->selected_line = s->blame.nlines;
4621 s->first_displayed_line = 1;
4622 } else {
4623 s->selected_line = view->nlines - 2;
4624 s->first_displayed_line = s->blame.nlines -
4625 (view->nlines - 3);
4627 break;
4628 case 'k':
4629 case KEY_UP:
4630 case CTRL('p'):
4631 if (s->selected_line > 1)
4632 s->selected_line--;
4633 else if (s->selected_line == 1 &&
4634 s->first_displayed_line > 1)
4635 s->first_displayed_line--;
4636 break;
4637 case KEY_PPAGE:
4638 case CTRL('b'):
4639 if (s->first_displayed_line == 1) {
4640 s->selected_line = 1;
4641 break;
4643 if (s->first_displayed_line > view->nlines - 2)
4644 s->first_displayed_line -=
4645 (view->nlines - 2);
4646 else
4647 s->first_displayed_line = 1;
4648 break;
4649 case 'j':
4650 case KEY_DOWN:
4651 case CTRL('n'):
4652 if (s->selected_line < view->nlines - 2 &&
4653 s->first_displayed_line +
4654 s->selected_line <= s->blame.nlines)
4655 s->selected_line++;
4656 else if (s->last_displayed_line <
4657 s->blame.nlines)
4658 s->first_displayed_line++;
4659 break;
4660 case 'b':
4661 case 'p': {
4662 struct got_object_id *id = NULL;
4663 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4664 s->first_displayed_line, s->selected_line);
4665 if (id == NULL)
4666 break;
4667 if (ch == 'p') {
4668 struct got_commit_object *commit, *pcommit;
4669 struct got_object_qid *pid;
4670 struct got_object_id *blob_id = NULL;
4671 int obj_type;
4672 err = got_object_open_as_commit(&commit,
4673 s->repo, id);
4674 if (err)
4675 break;
4676 pid = STAILQ_FIRST(
4677 got_object_commit_get_parent_ids(commit));
4678 if (pid == NULL) {
4679 got_object_commit_close(commit);
4680 break;
4682 /* Check if path history ends here. */
4683 err = got_object_open_as_commit(&pcommit,
4684 s->repo, &pid->id);
4685 if (err)
4686 break;
4687 err = got_object_id_by_path(&blob_id, s->repo,
4688 pcommit, s->path);
4689 got_object_commit_close(pcommit);
4690 if (err) {
4691 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4692 err = NULL;
4693 got_object_commit_close(commit);
4694 break;
4696 err = got_object_get_type(&obj_type, s->repo,
4697 blob_id);
4698 free(blob_id);
4699 /* Can't blame non-blob type objects. */
4700 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4701 got_object_commit_close(commit);
4702 break;
4704 err = got_object_qid_alloc(&s->blamed_commit,
4705 &pid->id);
4706 got_object_commit_close(commit);
4707 } else {
4708 if (got_object_id_cmp(id,
4709 &s->blamed_commit->id) == 0)
4710 break;
4711 err = got_object_qid_alloc(&s->blamed_commit,
4712 id);
4714 if (err)
4715 break;
4716 s->done = 1;
4717 thread_err = stop_blame(&s->blame);
4718 s->done = 0;
4719 if (thread_err)
4720 break;
4721 STAILQ_INSERT_HEAD(&s->blamed_commits,
4722 s->blamed_commit, entry);
4723 err = run_blame(view);
4724 if (err)
4725 break;
4726 break;
4728 case 'B': {
4729 struct got_object_qid *first;
4730 first = STAILQ_FIRST(&s->blamed_commits);
4731 if (!got_object_id_cmp(&first->id, s->commit_id))
4732 break;
4733 s->done = 1;
4734 thread_err = stop_blame(&s->blame);
4735 s->done = 0;
4736 if (thread_err)
4737 break;
4738 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4739 got_object_qid_free(s->blamed_commit);
4740 s->blamed_commit =
4741 STAILQ_FIRST(&s->blamed_commits);
4742 err = run_blame(view);
4743 if (err)
4744 break;
4745 break;
4747 case KEY_ENTER:
4748 case '\r': {
4749 struct got_object_id *id = NULL;
4750 struct got_object_qid *pid;
4751 struct got_commit_object *commit = NULL;
4752 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4753 s->first_displayed_line, s->selected_line);
4754 if (id == NULL)
4755 break;
4756 err = got_object_open_as_commit(&commit, s->repo, id);
4757 if (err)
4758 break;
4759 pid = STAILQ_FIRST(
4760 got_object_commit_get_parent_ids(commit));
4761 if (view_is_parent_view(view))
4762 begin_x = view_split_begin_x(view->begin_x);
4763 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4764 if (diff_view == NULL) {
4765 got_object_commit_close(commit);
4766 err = got_error_from_errno("view_open");
4767 break;
4769 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4770 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4771 got_object_commit_close(commit);
4772 if (err) {
4773 view_close(diff_view);
4774 break;
4776 view->focussed = 0;
4777 diff_view->focussed = 1;
4778 if (view_is_parent_view(view)) {
4779 err = view_close_child(view);
4780 if (err)
4781 break;
4782 view_set_child(view, diff_view);
4783 view->focus_child = 1;
4784 } else
4785 *new_view = diff_view;
4786 if (err)
4787 break;
4788 break;
4790 case KEY_NPAGE:
4791 case CTRL('f'):
4792 case ' ':
4793 if (s->last_displayed_line >= s->blame.nlines &&
4794 s->selected_line >= MIN(s->blame.nlines,
4795 view->nlines - 2)) {
4796 break;
4798 if (s->last_displayed_line >= s->blame.nlines &&
4799 s->selected_line < view->nlines - 2) {
4800 s->selected_line = MIN(s->blame.nlines,
4801 view->nlines - 2);
4802 break;
4804 if (s->last_displayed_line + view->nlines - 2
4805 <= s->blame.nlines)
4806 s->first_displayed_line +=
4807 view->nlines - 2;
4808 else
4809 s->first_displayed_line =
4810 s->blame.nlines -
4811 (view->nlines - 3);
4812 break;
4813 case KEY_RESIZE:
4814 if (s->selected_line > view->nlines - 2) {
4815 s->selected_line = MIN(s->blame.nlines,
4816 view->nlines - 2);
4818 break;
4819 default:
4820 break;
4822 return thread_err ? thread_err : err;
4825 static const struct got_error *
4826 cmd_blame(int argc, char *argv[])
4828 const struct got_error *error;
4829 struct got_repository *repo = NULL;
4830 struct got_worktree *worktree = NULL;
4831 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4832 char *link_target = NULL;
4833 struct got_object_id *commit_id = NULL;
4834 struct got_commit_object *commit = NULL;
4835 char *commit_id_str = NULL;
4836 int ch;
4837 struct tog_view *view;
4839 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4840 switch (ch) {
4841 case 'c':
4842 commit_id_str = optarg;
4843 break;
4844 case 'r':
4845 repo_path = realpath(optarg, NULL);
4846 if (repo_path == NULL)
4847 return got_error_from_errno2("realpath",
4848 optarg);
4849 break;
4850 default:
4851 usage_blame();
4852 /* NOTREACHED */
4856 argc -= optind;
4857 argv += optind;
4859 if (argc != 1)
4860 usage_blame();
4862 if (repo_path == NULL) {
4863 cwd = getcwd(NULL, 0);
4864 if (cwd == NULL)
4865 return got_error_from_errno("getcwd");
4866 error = got_worktree_open(&worktree, cwd);
4867 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4868 goto done;
4869 if (worktree)
4870 repo_path =
4871 strdup(got_worktree_get_repo_path(worktree));
4872 else
4873 repo_path = strdup(cwd);
4874 if (repo_path == NULL) {
4875 error = got_error_from_errno("strdup");
4876 goto done;
4880 error = got_repo_open(&repo, repo_path, NULL);
4881 if (error != NULL)
4882 goto done;
4884 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4885 worktree);
4886 if (error)
4887 goto done;
4889 init_curses();
4891 error = apply_unveil(got_repo_get_path(repo), NULL);
4892 if (error)
4893 goto done;
4895 error = tog_load_refs(repo, 0);
4896 if (error)
4897 goto done;
4899 if (commit_id_str == NULL) {
4900 struct got_reference *head_ref;
4901 error = got_ref_open(&head_ref, repo, worktree ?
4902 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4903 if (error != NULL)
4904 goto done;
4905 error = got_ref_resolve(&commit_id, repo, head_ref);
4906 got_ref_close(head_ref);
4907 } else {
4908 error = got_repo_match_object_id(&commit_id, NULL,
4909 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4911 if (error != NULL)
4912 goto done;
4914 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4915 if (view == NULL) {
4916 error = got_error_from_errno("view_open");
4917 goto done;
4920 error = got_object_open_as_commit(&commit, repo, commit_id);
4921 if (error)
4922 goto done;
4924 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4925 commit, repo);
4926 if (error)
4927 goto done;
4929 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4930 commit_id, repo);
4931 if (error)
4932 goto done;
4933 if (worktree) {
4934 /* Release work tree lock. */
4935 got_worktree_close(worktree);
4936 worktree = NULL;
4938 error = view_loop(view);
4939 done:
4940 free(repo_path);
4941 free(in_repo_path);
4942 free(link_target);
4943 free(cwd);
4944 free(commit_id);
4945 if (commit)
4946 got_object_commit_close(commit);
4947 if (worktree)
4948 got_worktree_close(worktree);
4949 if (repo) {
4950 const struct got_error *close_err = got_repo_close(repo);
4951 if (error == NULL)
4952 error = close_err;
4954 tog_free_refs();
4955 return error;
4958 static const struct got_error *
4959 draw_tree_entries(struct tog_view *view, const char *parent_path)
4961 struct tog_tree_view_state *s = &view->state.tree;
4962 const struct got_error *err = NULL;
4963 struct got_tree_entry *te;
4964 wchar_t *wline;
4965 struct tog_color *tc;
4966 int width, n, i, nentries;
4967 int limit = view->nlines;
4969 s->ndisplayed = 0;
4971 werase(view->window);
4973 if (limit == 0)
4974 return NULL;
4976 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4977 if (err)
4978 return err;
4979 if (view_needs_focus_indication(view))
4980 wstandout(view->window);
4981 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4982 if (tc)
4983 wattr_on(view->window,
4984 COLOR_PAIR(tc->colorpair), NULL);
4985 waddwstr(view->window, wline);
4986 if (tc)
4987 wattr_off(view->window,
4988 COLOR_PAIR(tc->colorpair), NULL);
4989 if (view_needs_focus_indication(view))
4990 wstandend(view->window);
4991 free(wline);
4992 wline = NULL;
4993 if (width < view->ncols - 1)
4994 waddch(view->window, '\n');
4995 if (--limit <= 0)
4996 return NULL;
4997 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4998 if (err)
4999 return err;
5000 waddwstr(view->window, wline);
5001 free(wline);
5002 wline = NULL;
5003 if (width < view->ncols - 1)
5004 waddch(view->window, '\n');
5005 if (--limit <= 0)
5006 return NULL;
5007 waddch(view->window, '\n');
5008 if (--limit <= 0)
5009 return NULL;
5011 if (s->first_displayed_entry == NULL) {
5012 te = got_object_tree_get_first_entry(s->tree);
5013 if (s->selected == 0) {
5014 if (view->focussed)
5015 wstandout(view->window);
5016 s->selected_entry = NULL;
5018 waddstr(view->window, " ..\n"); /* parent directory */
5019 if (s->selected == 0 && view->focussed)
5020 wstandend(view->window);
5021 s->ndisplayed++;
5022 if (--limit <= 0)
5023 return NULL;
5024 n = 1;
5025 } else {
5026 n = 0;
5027 te = s->first_displayed_entry;
5030 nentries = got_object_tree_get_nentries(s->tree);
5031 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5032 char *line = NULL, *id_str = NULL, *link_target = NULL;
5033 const char *modestr = "";
5034 mode_t mode;
5036 te = got_object_tree_get_entry(s->tree, i);
5037 mode = got_tree_entry_get_mode(te);
5039 if (s->show_ids) {
5040 err = got_object_id_str(&id_str,
5041 got_tree_entry_get_id(te));
5042 if (err)
5043 return got_error_from_errno(
5044 "got_object_id_str");
5046 if (got_object_tree_entry_is_submodule(te))
5047 modestr = "$";
5048 else if (S_ISLNK(mode)) {
5049 int i;
5051 err = got_tree_entry_get_symlink_target(&link_target,
5052 te, s->repo);
5053 if (err) {
5054 free(id_str);
5055 return err;
5057 for (i = 0; i < strlen(link_target); i++) {
5058 if (!isprint((unsigned char)link_target[i]))
5059 link_target[i] = '?';
5061 modestr = "@";
5063 else if (S_ISDIR(mode))
5064 modestr = "/";
5065 else if (mode & S_IXUSR)
5066 modestr = "*";
5067 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5068 got_tree_entry_get_name(te), modestr,
5069 link_target ? " -> ": "",
5070 link_target ? link_target : "") == -1) {
5071 free(id_str);
5072 free(link_target);
5073 return got_error_from_errno("asprintf");
5075 free(id_str);
5076 free(link_target);
5077 err = format_line(&wline, &width, line, view->ncols, 0);
5078 if (err) {
5079 free(line);
5080 break;
5082 if (n == s->selected) {
5083 if (view->focussed)
5084 wstandout(view->window);
5085 s->selected_entry = te;
5087 tc = match_color(&s->colors, line);
5088 if (tc)
5089 wattr_on(view->window,
5090 COLOR_PAIR(tc->colorpair), NULL);
5091 waddwstr(view->window, wline);
5092 if (tc)
5093 wattr_off(view->window,
5094 COLOR_PAIR(tc->colorpair), NULL);
5095 if (width < view->ncols - 1)
5096 waddch(view->window, '\n');
5097 if (n == s->selected && view->focussed)
5098 wstandend(view->window);
5099 free(line);
5100 free(wline);
5101 wline = NULL;
5102 n++;
5103 s->ndisplayed++;
5104 s->last_displayed_entry = te;
5105 if (--limit <= 0)
5106 break;
5109 return err;
5112 static void
5113 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5115 struct got_tree_entry *te;
5116 int isroot = s->tree == s->root;
5117 int i = 0;
5119 if (s->first_displayed_entry == NULL)
5120 return;
5122 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5123 while (i++ < maxscroll) {
5124 if (te == NULL) {
5125 if (!isroot)
5126 s->first_displayed_entry = NULL;
5127 break;
5129 s->first_displayed_entry = te;
5130 te = got_tree_entry_get_prev(s->tree, te);
5134 static void
5135 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5137 struct got_tree_entry *next, *last;
5138 int n = 0;
5140 if (s->first_displayed_entry)
5141 next = got_tree_entry_get_next(s->tree,
5142 s->first_displayed_entry);
5143 else
5144 next = got_object_tree_get_first_entry(s->tree);
5146 last = s->last_displayed_entry;
5147 while (next && last && n++ < maxscroll) {
5148 last = got_tree_entry_get_next(s->tree, last);
5149 if (last) {
5150 s->first_displayed_entry = next;
5151 next = got_tree_entry_get_next(s->tree, next);
5156 static const struct got_error *
5157 tree_entry_path(char **path, struct tog_parent_trees *parents,
5158 struct got_tree_entry *te)
5160 const struct got_error *err = NULL;
5161 struct tog_parent_tree *pt;
5162 size_t len = 2; /* for leading slash and NUL */
5164 TAILQ_FOREACH(pt, parents, entry)
5165 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5166 + 1 /* slash */;
5167 if (te)
5168 len += strlen(got_tree_entry_get_name(te));
5170 *path = calloc(1, len);
5171 if (path == NULL)
5172 return got_error_from_errno("calloc");
5174 (*path)[0] = '/';
5175 pt = TAILQ_LAST(parents, tog_parent_trees);
5176 while (pt) {
5177 const char *name = got_tree_entry_get_name(pt->selected_entry);
5178 if (strlcat(*path, name, len) >= len) {
5179 err = got_error(GOT_ERR_NO_SPACE);
5180 goto done;
5182 if (strlcat(*path, "/", len) >= len) {
5183 err = got_error(GOT_ERR_NO_SPACE);
5184 goto done;
5186 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5188 if (te) {
5189 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5190 err = got_error(GOT_ERR_NO_SPACE);
5191 goto done;
5194 done:
5195 if (err) {
5196 free(*path);
5197 *path = NULL;
5199 return err;
5202 static const struct got_error *
5203 blame_tree_entry(struct tog_view **new_view, int begin_x,
5204 struct got_tree_entry *te, struct tog_parent_trees *parents,
5205 struct got_object_id *commit_id, struct got_repository *repo)
5207 const struct got_error *err = NULL;
5208 char *path;
5209 struct tog_view *blame_view;
5211 *new_view = NULL;
5213 err = tree_entry_path(&path, parents, te);
5214 if (err)
5215 return err;
5217 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5218 if (blame_view == NULL) {
5219 err = got_error_from_errno("view_open");
5220 goto done;
5223 err = open_blame_view(blame_view, path, commit_id, repo);
5224 if (err) {
5225 if (err->code == GOT_ERR_CANCELLED)
5226 err = NULL;
5227 view_close(blame_view);
5228 } else
5229 *new_view = blame_view;
5230 done:
5231 free(path);
5232 return err;
5235 static const struct got_error *
5236 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5237 struct tog_tree_view_state *s)
5239 struct tog_view *log_view;
5240 const struct got_error *err = NULL;
5241 char *path;
5243 *new_view = NULL;
5245 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5246 if (log_view == NULL)
5247 return got_error_from_errno("view_open");
5249 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5250 if (err)
5251 return err;
5253 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5254 path, 0);
5255 if (err)
5256 view_close(log_view);
5257 else
5258 *new_view = log_view;
5259 free(path);
5260 return err;
5263 static const struct got_error *
5264 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5265 const char *head_ref_name, struct got_repository *repo)
5267 const struct got_error *err = NULL;
5268 char *commit_id_str = NULL;
5269 struct tog_tree_view_state *s = &view->state.tree;
5270 struct got_commit_object *commit = NULL;
5272 TAILQ_INIT(&s->parents);
5273 STAILQ_INIT(&s->colors);
5275 s->commit_id = got_object_id_dup(commit_id);
5276 if (s->commit_id == NULL)
5277 return got_error_from_errno("got_object_id_dup");
5279 err = got_object_open_as_commit(&commit, repo, commit_id);
5280 if (err)
5281 goto done;
5284 * The root is opened here and will be closed when the view is closed.
5285 * Any visited subtrees and their path-wise parents are opened and
5286 * closed on demand.
5288 err = got_object_open_as_tree(&s->root, repo,
5289 got_object_commit_get_tree_id(commit));
5290 if (err)
5291 goto done;
5292 s->tree = s->root;
5294 err = got_object_id_str(&commit_id_str, commit_id);
5295 if (err != NULL)
5296 goto done;
5298 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5299 err = got_error_from_errno("asprintf");
5300 goto done;
5303 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5304 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5305 if (head_ref_name) {
5306 s->head_ref_name = strdup(head_ref_name);
5307 if (s->head_ref_name == NULL) {
5308 err = got_error_from_errno("strdup");
5309 goto done;
5312 s->repo = repo;
5314 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5315 err = add_color(&s->colors, "\\$$",
5316 TOG_COLOR_TREE_SUBMODULE,
5317 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5318 if (err)
5319 goto done;
5320 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5321 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5322 if (err)
5323 goto done;
5324 err = add_color(&s->colors, "/$",
5325 TOG_COLOR_TREE_DIRECTORY,
5326 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5327 if (err)
5328 goto done;
5330 err = add_color(&s->colors, "\\*$",
5331 TOG_COLOR_TREE_EXECUTABLE,
5332 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5333 if (err)
5334 goto done;
5336 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5337 get_color_value("TOG_COLOR_COMMIT"));
5338 if (err)
5339 goto done;
5342 view->show = show_tree_view;
5343 view->input = input_tree_view;
5344 view->close = close_tree_view;
5345 view->search_start = search_start_tree_view;
5346 view->search_next = search_next_tree_view;
5347 done:
5348 free(commit_id_str);
5349 if (commit)
5350 got_object_commit_close(commit);
5351 if (err)
5352 close_tree_view(view);
5353 return err;
5356 static const struct got_error *
5357 close_tree_view(struct tog_view *view)
5359 struct tog_tree_view_state *s = &view->state.tree;
5361 free_colors(&s->colors);
5362 free(s->tree_label);
5363 s->tree_label = NULL;
5364 free(s->commit_id);
5365 s->commit_id = NULL;
5366 free(s->head_ref_name);
5367 s->head_ref_name = NULL;
5368 while (!TAILQ_EMPTY(&s->parents)) {
5369 struct tog_parent_tree *parent;
5370 parent = TAILQ_FIRST(&s->parents);
5371 TAILQ_REMOVE(&s->parents, parent, entry);
5372 if (parent->tree != s->root)
5373 got_object_tree_close(parent->tree);
5374 free(parent);
5377 if (s->tree != NULL && s->tree != s->root)
5378 got_object_tree_close(s->tree);
5379 if (s->root)
5380 got_object_tree_close(s->root);
5381 return NULL;
5384 static const struct got_error *
5385 search_start_tree_view(struct tog_view *view)
5387 struct tog_tree_view_state *s = &view->state.tree;
5389 s->matched_entry = NULL;
5390 return NULL;
5393 static int
5394 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5396 regmatch_t regmatch;
5398 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5399 0) == 0;
5402 static const struct got_error *
5403 search_next_tree_view(struct tog_view *view)
5405 struct tog_tree_view_state *s = &view->state.tree;
5406 struct got_tree_entry *te = NULL;
5408 if (!view->searching) {
5409 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5410 return NULL;
5413 if (s->matched_entry) {
5414 if (view->searching == TOG_SEARCH_FORWARD) {
5415 if (s->selected_entry)
5416 te = got_tree_entry_get_next(s->tree,
5417 s->selected_entry);
5418 else
5419 te = got_object_tree_get_first_entry(s->tree);
5420 } else {
5421 if (s->selected_entry == NULL)
5422 te = got_object_tree_get_last_entry(s->tree);
5423 else
5424 te = got_tree_entry_get_prev(s->tree,
5425 s->selected_entry);
5427 } else {
5428 if (s->selected_entry)
5429 te = s->selected_entry;
5430 else if (view->searching == TOG_SEARCH_FORWARD)
5431 te = got_object_tree_get_first_entry(s->tree);
5432 else
5433 te = got_object_tree_get_last_entry(s->tree);
5436 while (1) {
5437 if (te == NULL) {
5438 if (s->matched_entry == NULL) {
5439 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5440 return NULL;
5442 if (view->searching == TOG_SEARCH_FORWARD)
5443 te = got_object_tree_get_first_entry(s->tree);
5444 else
5445 te = got_object_tree_get_last_entry(s->tree);
5448 if (match_tree_entry(te, &view->regex)) {
5449 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5450 s->matched_entry = te;
5451 break;
5454 if (view->searching == TOG_SEARCH_FORWARD)
5455 te = got_tree_entry_get_next(s->tree, te);
5456 else
5457 te = got_tree_entry_get_prev(s->tree, te);
5460 if (s->matched_entry) {
5461 s->first_displayed_entry = s->matched_entry;
5462 s->selected = 0;
5465 return NULL;
5468 static const struct got_error *
5469 show_tree_view(struct tog_view *view)
5471 const struct got_error *err = NULL;
5472 struct tog_tree_view_state *s = &view->state.tree;
5473 char *parent_path;
5475 err = tree_entry_path(&parent_path, &s->parents, NULL);
5476 if (err)
5477 return err;
5479 err = draw_tree_entries(view, parent_path);
5480 free(parent_path);
5482 view_vborder(view);
5483 return err;
5486 static const struct got_error *
5487 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5489 const struct got_error *err = NULL;
5490 struct tog_tree_view_state *s = &view->state.tree;
5491 struct tog_view *log_view, *ref_view;
5492 struct got_tree_entry *te;
5493 int begin_x = 0, n;
5495 switch (ch) {
5496 case 'i':
5497 s->show_ids = !s->show_ids;
5498 break;
5499 case 'l':
5500 if (!s->selected_entry)
5501 break;
5502 if (view_is_parent_view(view))
5503 begin_x = view_split_begin_x(view->begin_x);
5504 err = log_selected_tree_entry(&log_view, begin_x, s);
5505 view->focussed = 0;
5506 log_view->focussed = 1;
5507 if (view_is_parent_view(view)) {
5508 err = view_close_child(view);
5509 if (err)
5510 return err;
5511 view_set_child(view, log_view);
5512 view->focus_child = 1;
5513 } else
5514 *new_view = log_view;
5515 break;
5516 case 'r':
5517 if (view_is_parent_view(view))
5518 begin_x = view_split_begin_x(view->begin_x);
5519 ref_view = view_open(view->nlines, view->ncols,
5520 view->begin_y, begin_x, TOG_VIEW_REF);
5521 if (ref_view == NULL)
5522 return got_error_from_errno("view_open");
5523 err = open_ref_view(ref_view, s->repo);
5524 if (err) {
5525 view_close(ref_view);
5526 return err;
5528 view->focussed = 0;
5529 ref_view->focussed = 1;
5530 if (view_is_parent_view(view)) {
5531 err = view_close_child(view);
5532 if (err)
5533 return err;
5534 view_set_child(view, ref_view);
5535 view->focus_child = 1;
5536 } else
5537 *new_view = ref_view;
5538 break;
5539 case 'g':
5540 case KEY_HOME:
5541 s->selected = 0;
5542 if (s->tree == s->root)
5543 s->first_displayed_entry =
5544 got_object_tree_get_first_entry(s->tree);
5545 else
5546 s->first_displayed_entry = NULL;
5547 break;
5548 case 'G':
5549 case KEY_END:
5550 s->selected = 0;
5551 te = got_object_tree_get_last_entry(s->tree);
5552 for (n = 0; n < view->nlines - 3; n++) {
5553 if (te == NULL) {
5554 if(s->tree != s->root) {
5555 s->first_displayed_entry = NULL;
5556 n++;
5558 break;
5560 s->first_displayed_entry = te;
5561 te = got_tree_entry_get_prev(s->tree, te);
5563 if (n > 0)
5564 s->selected = n - 1;
5565 break;
5566 case 'k':
5567 case KEY_UP:
5568 case CTRL('p'):
5569 if (s->selected > 0) {
5570 s->selected--;
5571 break;
5573 tree_scroll_up(s, 1);
5574 break;
5575 case KEY_PPAGE:
5576 case CTRL('b'):
5577 if (s->tree == s->root) {
5578 if (got_object_tree_get_first_entry(s->tree) ==
5579 s->first_displayed_entry)
5580 s->selected = 0;
5581 } else {
5582 if (s->first_displayed_entry == NULL)
5583 s->selected = 0;
5585 tree_scroll_up(s, MAX(0, view->nlines - 3));
5586 break;
5587 case 'j':
5588 case KEY_DOWN:
5589 case CTRL('n'):
5590 if (s->selected < s->ndisplayed - 1) {
5591 s->selected++;
5592 break;
5594 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5595 == NULL)
5596 /* can't scroll any further */
5597 break;
5598 tree_scroll_down(s, 1);
5599 break;
5600 case KEY_NPAGE:
5601 case CTRL('f'):
5602 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5603 == NULL) {
5604 /* can't scroll any further; move cursor down */
5605 if (s->selected < s->ndisplayed - 1)
5606 s->selected = s->ndisplayed - 1;
5607 break;
5609 tree_scroll_down(s, view->nlines - 3);
5610 break;
5611 case KEY_ENTER:
5612 case '\r':
5613 case KEY_BACKSPACE:
5614 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5615 struct tog_parent_tree *parent;
5616 /* user selected '..' */
5617 if (s->tree == s->root)
5618 break;
5619 parent = TAILQ_FIRST(&s->parents);
5620 TAILQ_REMOVE(&s->parents, parent,
5621 entry);
5622 got_object_tree_close(s->tree);
5623 s->tree = parent->tree;
5624 s->first_displayed_entry =
5625 parent->first_displayed_entry;
5626 s->selected_entry =
5627 parent->selected_entry;
5628 s->selected = parent->selected;
5629 free(parent);
5630 } else if (S_ISDIR(got_tree_entry_get_mode(
5631 s->selected_entry))) {
5632 struct got_tree_object *subtree;
5633 err = got_object_open_as_tree(&subtree, s->repo,
5634 got_tree_entry_get_id(s->selected_entry));
5635 if (err)
5636 break;
5637 err = tree_view_visit_subtree(s, subtree);
5638 if (err) {
5639 got_object_tree_close(subtree);
5640 break;
5642 } else if (S_ISREG(got_tree_entry_get_mode(
5643 s->selected_entry))) {
5644 struct tog_view *blame_view;
5645 int begin_x = view_is_parent_view(view) ?
5646 view_split_begin_x(view->begin_x) : 0;
5648 err = blame_tree_entry(&blame_view, begin_x,
5649 s->selected_entry, &s->parents,
5650 s->commit_id, s->repo);
5651 if (err)
5652 break;
5653 view->focussed = 0;
5654 blame_view->focussed = 1;
5655 if (view_is_parent_view(view)) {
5656 err = view_close_child(view);
5657 if (err)
5658 return err;
5659 view_set_child(view, blame_view);
5660 view->focus_child = 1;
5661 } else
5662 *new_view = blame_view;
5664 break;
5665 case KEY_RESIZE:
5666 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5667 s->selected = view->nlines - 4;
5668 break;
5669 default:
5670 break;
5673 return err;
5676 __dead static void
5677 usage_tree(void)
5679 endwin();
5680 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5681 getprogname());
5682 exit(1);
5685 static const struct got_error *
5686 cmd_tree(int argc, char *argv[])
5688 const struct got_error *error;
5689 struct got_repository *repo = NULL;
5690 struct got_worktree *worktree = NULL;
5691 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5692 struct got_object_id *commit_id = NULL;
5693 struct got_commit_object *commit = NULL;
5694 const char *commit_id_arg = NULL;
5695 char *label = NULL;
5696 struct got_reference *ref = NULL;
5697 const char *head_ref_name = NULL;
5698 int ch;
5699 struct tog_view *view;
5701 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5702 switch (ch) {
5703 case 'c':
5704 commit_id_arg = optarg;
5705 break;
5706 case 'r':
5707 repo_path = realpath(optarg, NULL);
5708 if (repo_path == NULL)
5709 return got_error_from_errno2("realpath",
5710 optarg);
5711 break;
5712 default:
5713 usage_tree();
5714 /* NOTREACHED */
5718 argc -= optind;
5719 argv += optind;
5721 if (argc > 1)
5722 usage_tree();
5724 if (repo_path == NULL) {
5725 cwd = getcwd(NULL, 0);
5726 if (cwd == NULL)
5727 return got_error_from_errno("getcwd");
5728 error = got_worktree_open(&worktree, cwd);
5729 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5730 goto done;
5731 if (worktree)
5732 repo_path =
5733 strdup(got_worktree_get_repo_path(worktree));
5734 else
5735 repo_path = strdup(cwd);
5736 if (repo_path == NULL) {
5737 error = got_error_from_errno("strdup");
5738 goto done;
5742 error = got_repo_open(&repo, repo_path, NULL);
5743 if (error != NULL)
5744 goto done;
5746 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5747 repo, worktree);
5748 if (error)
5749 goto done;
5751 init_curses();
5753 error = apply_unveil(got_repo_get_path(repo), NULL);
5754 if (error)
5755 goto done;
5757 error = tog_load_refs(repo, 0);
5758 if (error)
5759 goto done;
5761 if (commit_id_arg == NULL) {
5762 error = got_repo_match_object_id(&commit_id, &label,
5763 worktree ? got_worktree_get_head_ref_name(worktree) :
5764 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5765 if (error)
5766 goto done;
5767 head_ref_name = label;
5768 } else {
5769 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5770 if (error == NULL)
5771 head_ref_name = got_ref_get_name(ref);
5772 else if (error->code != GOT_ERR_NOT_REF)
5773 goto done;
5774 error = got_repo_match_object_id(&commit_id, NULL,
5775 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5776 if (error)
5777 goto done;
5780 error = got_object_open_as_commit(&commit, repo, commit_id);
5781 if (error)
5782 goto done;
5784 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5785 if (view == NULL) {
5786 error = got_error_from_errno("view_open");
5787 goto done;
5789 error = open_tree_view(view, commit_id, head_ref_name, repo);
5790 if (error)
5791 goto done;
5792 if (!got_path_is_root_dir(in_repo_path)) {
5793 error = tree_view_walk_path(&view->state.tree, commit,
5794 in_repo_path);
5795 if (error)
5796 goto done;
5799 if (worktree) {
5800 /* Release work tree lock. */
5801 got_worktree_close(worktree);
5802 worktree = NULL;
5804 error = view_loop(view);
5805 done:
5806 free(repo_path);
5807 free(cwd);
5808 free(commit_id);
5809 free(label);
5810 if (ref)
5811 got_ref_close(ref);
5812 if (repo) {
5813 const struct got_error *close_err = got_repo_close(repo);
5814 if (error == NULL)
5815 error = close_err;
5817 tog_free_refs();
5818 return error;
5821 static const struct got_error *
5822 ref_view_load_refs(struct tog_ref_view_state *s)
5824 struct got_reflist_entry *sre;
5825 struct tog_reflist_entry *re;
5827 s->nrefs = 0;
5828 TAILQ_FOREACH(sre, &tog_refs, entry) {
5829 if (strncmp(got_ref_get_name(sre->ref),
5830 "refs/got/", 9) == 0 &&
5831 strncmp(got_ref_get_name(sre->ref),
5832 "refs/got/backup/", 16) != 0)
5833 continue;
5835 re = malloc(sizeof(*re));
5836 if (re == NULL)
5837 return got_error_from_errno("malloc");
5839 re->ref = got_ref_dup(sre->ref);
5840 if (re->ref == NULL)
5841 return got_error_from_errno("got_ref_dup");
5842 re->idx = s->nrefs++;
5843 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5846 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5847 return NULL;
5850 void
5851 ref_view_free_refs(struct tog_ref_view_state *s)
5853 struct tog_reflist_entry *re;
5855 while (!TAILQ_EMPTY(&s->refs)) {
5856 re = TAILQ_FIRST(&s->refs);
5857 TAILQ_REMOVE(&s->refs, re, entry);
5858 got_ref_close(re->ref);
5859 free(re);
5863 static const struct got_error *
5864 open_ref_view(struct tog_view *view, struct got_repository *repo)
5866 const struct got_error *err = NULL;
5867 struct tog_ref_view_state *s = &view->state.ref;
5869 s->selected_entry = 0;
5870 s->repo = repo;
5872 TAILQ_INIT(&s->refs);
5873 STAILQ_INIT(&s->colors);
5875 err = ref_view_load_refs(s);
5876 if (err)
5877 return err;
5879 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5880 err = add_color(&s->colors, "^refs/heads/",
5881 TOG_COLOR_REFS_HEADS,
5882 get_color_value("TOG_COLOR_REFS_HEADS"));
5883 if (err)
5884 goto done;
5886 err = add_color(&s->colors, "^refs/tags/",
5887 TOG_COLOR_REFS_TAGS,
5888 get_color_value("TOG_COLOR_REFS_TAGS"));
5889 if (err)
5890 goto done;
5892 err = add_color(&s->colors, "^refs/remotes/",
5893 TOG_COLOR_REFS_REMOTES,
5894 get_color_value("TOG_COLOR_REFS_REMOTES"));
5895 if (err)
5896 goto done;
5898 err = add_color(&s->colors, "^refs/got/backup/",
5899 TOG_COLOR_REFS_BACKUP,
5900 get_color_value("TOG_COLOR_REFS_BACKUP"));
5901 if (err)
5902 goto done;
5905 view->show = show_ref_view;
5906 view->input = input_ref_view;
5907 view->close = close_ref_view;
5908 view->search_start = search_start_ref_view;
5909 view->search_next = search_next_ref_view;
5910 done:
5911 if (err)
5912 free_colors(&s->colors);
5913 return err;
5916 static const struct got_error *
5917 close_ref_view(struct tog_view *view)
5919 struct tog_ref_view_state *s = &view->state.ref;
5921 ref_view_free_refs(s);
5922 free_colors(&s->colors);
5924 return NULL;
5927 static const struct got_error *
5928 resolve_reflist_entry(struct got_object_id **commit_id,
5929 struct tog_reflist_entry *re, struct got_repository *repo)
5931 const struct got_error *err = NULL;
5932 struct got_object_id *obj_id;
5933 struct got_tag_object *tag = NULL;
5934 int obj_type;
5936 *commit_id = NULL;
5938 err = got_ref_resolve(&obj_id, repo, re->ref);
5939 if (err)
5940 return err;
5942 err = got_object_get_type(&obj_type, repo, obj_id);
5943 if (err)
5944 goto done;
5946 switch (obj_type) {
5947 case GOT_OBJ_TYPE_COMMIT:
5948 *commit_id = obj_id;
5949 break;
5950 case GOT_OBJ_TYPE_TAG:
5951 err = got_object_open_as_tag(&tag, repo, obj_id);
5952 if (err)
5953 goto done;
5954 free(obj_id);
5955 err = got_object_get_type(&obj_type, repo,
5956 got_object_tag_get_object_id(tag));
5957 if (err)
5958 goto done;
5959 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5960 err = got_error(GOT_ERR_OBJ_TYPE);
5961 goto done;
5963 *commit_id = got_object_id_dup(
5964 got_object_tag_get_object_id(tag));
5965 if (*commit_id == NULL) {
5966 err = got_error_from_errno("got_object_id_dup");
5967 goto done;
5969 break;
5970 default:
5971 err = got_error(GOT_ERR_OBJ_TYPE);
5972 break;
5975 done:
5976 if (tag)
5977 got_object_tag_close(tag);
5978 if (err) {
5979 free(*commit_id);
5980 *commit_id = NULL;
5982 return err;
5985 static const struct got_error *
5986 log_ref_entry(struct tog_view **new_view, int begin_x,
5987 struct tog_reflist_entry *re, struct got_repository *repo)
5989 struct tog_view *log_view;
5990 const struct got_error *err = NULL;
5991 struct got_object_id *commit_id = NULL;
5993 *new_view = NULL;
5995 err = resolve_reflist_entry(&commit_id, re, repo);
5996 if (err) {
5997 if (err->code != GOT_ERR_OBJ_TYPE)
5998 return err;
5999 else
6000 return NULL;
6003 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6004 if (log_view == NULL) {
6005 err = got_error_from_errno("view_open");
6006 goto done;
6009 err = open_log_view(log_view, commit_id, repo,
6010 got_ref_get_name(re->ref), "", 0);
6011 done:
6012 if (err)
6013 view_close(log_view);
6014 else
6015 *new_view = log_view;
6016 free(commit_id);
6017 return err;
6020 static void
6021 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6023 struct tog_reflist_entry *re;
6024 int i = 0;
6026 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6027 return;
6029 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6030 while (i++ < maxscroll) {
6031 if (re == NULL)
6032 break;
6033 s->first_displayed_entry = re;
6034 re = TAILQ_PREV(re, tog_reflist_head, entry);
6038 static void
6039 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6041 struct tog_reflist_entry *next, *last;
6042 int n = 0;
6044 if (s->first_displayed_entry)
6045 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6046 else
6047 next = TAILQ_FIRST(&s->refs);
6049 last = s->last_displayed_entry;
6050 while (next && last && n++ < maxscroll) {
6051 last = TAILQ_NEXT(last, entry);
6052 if (last) {
6053 s->first_displayed_entry = next;
6054 next = TAILQ_NEXT(next, entry);
6059 static const struct got_error *
6060 search_start_ref_view(struct tog_view *view)
6062 struct tog_ref_view_state *s = &view->state.ref;
6064 s->matched_entry = NULL;
6065 return NULL;
6068 static int
6069 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6071 regmatch_t regmatch;
6073 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6074 0) == 0;
6077 static const struct got_error *
6078 search_next_ref_view(struct tog_view *view)
6080 struct tog_ref_view_state *s = &view->state.ref;
6081 struct tog_reflist_entry *re = NULL;
6083 if (!view->searching) {
6084 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6085 return NULL;
6088 if (s->matched_entry) {
6089 if (view->searching == TOG_SEARCH_FORWARD) {
6090 if (s->selected_entry)
6091 re = TAILQ_NEXT(s->selected_entry, entry);
6092 else
6093 re = TAILQ_PREV(s->selected_entry,
6094 tog_reflist_head, entry);
6095 } else {
6096 if (s->selected_entry == NULL)
6097 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6098 else
6099 re = TAILQ_PREV(s->selected_entry,
6100 tog_reflist_head, entry);
6102 } else {
6103 if (s->selected_entry)
6104 re = s->selected_entry;
6105 else if (view->searching == TOG_SEARCH_FORWARD)
6106 re = TAILQ_FIRST(&s->refs);
6107 else
6108 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6111 while (1) {
6112 if (re == NULL) {
6113 if (s->matched_entry == NULL) {
6114 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6115 return NULL;
6117 if (view->searching == TOG_SEARCH_FORWARD)
6118 re = TAILQ_FIRST(&s->refs);
6119 else
6120 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6123 if (match_reflist_entry(re, &view->regex)) {
6124 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6125 s->matched_entry = re;
6126 break;
6129 if (view->searching == TOG_SEARCH_FORWARD)
6130 re = TAILQ_NEXT(re, entry);
6131 else
6132 re = TAILQ_PREV(re, tog_reflist_head, entry);
6135 if (s->matched_entry) {
6136 s->first_displayed_entry = s->matched_entry;
6137 s->selected = 0;
6140 return NULL;
6143 static const struct got_error *
6144 show_ref_view(struct tog_view *view)
6146 const struct got_error *err = NULL;
6147 struct tog_ref_view_state *s = &view->state.ref;
6148 struct tog_reflist_entry *re;
6149 char *line = NULL;
6150 wchar_t *wline;
6151 struct tog_color *tc;
6152 int width, n;
6153 int limit = view->nlines;
6155 werase(view->window);
6157 s->ndisplayed = 0;
6159 if (limit == 0)
6160 return NULL;
6162 re = s->first_displayed_entry;
6164 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6165 s->nrefs) == -1)
6166 return got_error_from_errno("asprintf");
6168 err = format_line(&wline, &width, line, view->ncols, 0);
6169 if (err) {
6170 free(line);
6171 return err;
6173 if (view_needs_focus_indication(view))
6174 wstandout(view->window);
6175 waddwstr(view->window, wline);
6176 if (view_needs_focus_indication(view))
6177 wstandend(view->window);
6178 free(wline);
6179 wline = NULL;
6180 free(line);
6181 line = NULL;
6182 if (width < view->ncols - 1)
6183 waddch(view->window, '\n');
6184 if (--limit <= 0)
6185 return NULL;
6187 n = 0;
6188 while (re && limit > 0) {
6189 char *line = NULL;
6191 if (got_ref_is_symbolic(re->ref)) {
6192 if (asprintf(&line, "%s -> %s",
6193 got_ref_get_name(re->ref),
6194 got_ref_get_symref_target(re->ref)) == -1)
6195 return got_error_from_errno("asprintf");
6196 } else if (s->show_ids) {
6197 struct got_object_id *id;
6198 char *id_str;
6199 err = got_ref_resolve(&id, s->repo, re->ref);
6200 if (err)
6201 return err;
6202 err = got_object_id_str(&id_str, id);
6203 if (err) {
6204 free(id);
6205 return err;
6207 if (asprintf(&line, "%s: %s",
6208 got_ref_get_name(re->ref), id_str) == -1) {
6209 err = got_error_from_errno("asprintf");
6210 free(id);
6211 free(id_str);
6212 return err;
6214 free(id);
6215 free(id_str);
6216 } else {
6217 line = strdup(got_ref_get_name(re->ref));
6218 if (line == NULL)
6219 return got_error_from_errno("strdup");
6222 err = format_line(&wline, &width, line, view->ncols, 0);
6223 if (err) {
6224 free(line);
6225 return err;
6227 if (n == s->selected) {
6228 if (view->focussed)
6229 wstandout(view->window);
6230 s->selected_entry = re;
6232 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6233 if (tc)
6234 wattr_on(view->window,
6235 COLOR_PAIR(tc->colorpair), NULL);
6236 waddwstr(view->window, wline);
6237 if (tc)
6238 wattr_off(view->window,
6239 COLOR_PAIR(tc->colorpair), NULL);
6240 if (width < view->ncols - 1)
6241 waddch(view->window, '\n');
6242 if (n == s->selected && view->focussed)
6243 wstandend(view->window);
6244 free(line);
6245 free(wline);
6246 wline = NULL;
6247 n++;
6248 s->ndisplayed++;
6249 s->last_displayed_entry = re;
6251 limit--;
6252 re = TAILQ_NEXT(re, entry);
6255 view_vborder(view);
6256 return err;
6259 static const struct got_error *
6260 browse_ref_tree(struct tog_view **new_view, int begin_x,
6261 struct tog_reflist_entry *re, struct got_repository *repo)
6263 const struct got_error *err = NULL;
6264 struct got_object_id *commit_id = NULL;
6265 struct tog_view *tree_view;
6267 *new_view = NULL;
6269 err = resolve_reflist_entry(&commit_id, re, repo);
6270 if (err) {
6271 if (err->code != GOT_ERR_OBJ_TYPE)
6272 return err;
6273 else
6274 return NULL;
6278 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6279 if (tree_view == NULL) {
6280 err = got_error_from_errno("view_open");
6281 goto done;
6284 err = open_tree_view(tree_view, commit_id,
6285 got_ref_get_name(re->ref), repo);
6286 if (err)
6287 goto done;
6289 *new_view = tree_view;
6290 done:
6291 free(commit_id);
6292 return err;
6294 static const struct got_error *
6295 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6297 const struct got_error *err = NULL;
6298 struct tog_ref_view_state *s = &view->state.ref;
6299 struct tog_view *log_view, *tree_view;
6300 struct tog_reflist_entry *re;
6301 int begin_x = 0, n;
6303 switch (ch) {
6304 case 'i':
6305 s->show_ids = !s->show_ids;
6306 break;
6307 case 'o':
6308 s->sort_by_date = !s->sort_by_date;
6309 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6310 got_ref_cmp_by_commit_timestamp_descending :
6311 tog_ref_cmp_by_name, s->repo);
6312 if (err)
6313 break;
6314 got_reflist_object_id_map_free(tog_refs_idmap);
6315 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6316 &tog_refs, s->repo);
6317 if (err)
6318 break;
6319 ref_view_free_refs(s);
6320 err = ref_view_load_refs(s);
6321 break;
6322 case KEY_ENTER:
6323 case '\r':
6324 if (!s->selected_entry)
6325 break;
6326 if (view_is_parent_view(view))
6327 begin_x = view_split_begin_x(view->begin_x);
6328 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6329 s->repo);
6330 view->focussed = 0;
6331 log_view->focussed = 1;
6332 if (view_is_parent_view(view)) {
6333 err = view_close_child(view);
6334 if (err)
6335 return err;
6336 view_set_child(view, log_view);
6337 view->focus_child = 1;
6338 } else
6339 *new_view = log_view;
6340 break;
6341 case 't':
6342 if (!s->selected_entry)
6343 break;
6344 if (view_is_parent_view(view))
6345 begin_x = view_split_begin_x(view->begin_x);
6346 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6347 s->repo);
6348 if (err || tree_view == NULL)
6349 break;
6350 view->focussed = 0;
6351 tree_view->focussed = 1;
6352 if (view_is_parent_view(view)) {
6353 err = view_close_child(view);
6354 if (err)
6355 return err;
6356 view_set_child(view, tree_view);
6357 view->focus_child = 1;
6358 } else
6359 *new_view = tree_view;
6360 break;
6361 case 'g':
6362 case KEY_HOME:
6363 s->selected = 0;
6364 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6365 break;
6366 case 'G':
6367 case KEY_END:
6368 s->selected = 0;
6369 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6370 for (n = 0; n < view->nlines - 1; n++) {
6371 if (re == NULL)
6372 break;
6373 s->first_displayed_entry = re;
6374 re = TAILQ_PREV(re, tog_reflist_head, entry);
6376 if (n > 0)
6377 s->selected = n - 1;
6378 break;
6379 case 'k':
6380 case KEY_UP:
6381 case CTRL('p'):
6382 if (s->selected > 0) {
6383 s->selected--;
6384 break;
6386 ref_scroll_up(s, 1);
6387 break;
6388 case KEY_PPAGE:
6389 case CTRL('b'):
6390 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6391 s->selected = 0;
6392 ref_scroll_up(s, MAX(0, view->nlines - 1));
6393 break;
6394 case 'j':
6395 case KEY_DOWN:
6396 case CTRL('n'):
6397 if (s->selected < s->ndisplayed - 1) {
6398 s->selected++;
6399 break;
6401 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6402 /* can't scroll any further */
6403 break;
6404 ref_scroll_down(s, 1);
6405 break;
6406 case KEY_NPAGE:
6407 case CTRL('f'):
6408 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6409 /* can't scroll any further; move cursor down */
6410 if (s->selected < s->ndisplayed - 1)
6411 s->selected = s->ndisplayed - 1;
6412 break;
6414 ref_scroll_down(s, view->nlines - 1);
6415 break;
6416 case CTRL('l'):
6417 tog_free_refs();
6418 err = tog_load_refs(s->repo, s->sort_by_date);
6419 if (err)
6420 break;
6421 ref_view_free_refs(s);
6422 err = ref_view_load_refs(s);
6423 break;
6424 case KEY_RESIZE:
6425 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6426 s->selected = view->nlines - 2;
6427 break;
6428 default:
6429 break;
6432 return err;
6435 __dead static void
6436 usage_ref(void)
6438 endwin();
6439 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6440 getprogname());
6441 exit(1);
6444 static const struct got_error *
6445 cmd_ref(int argc, char *argv[])
6447 const struct got_error *error;
6448 struct got_repository *repo = NULL;
6449 struct got_worktree *worktree = NULL;
6450 char *cwd = NULL, *repo_path = NULL;
6451 int ch;
6452 struct tog_view *view;
6454 while ((ch = getopt(argc, argv, "r:")) != -1) {
6455 switch (ch) {
6456 case 'r':
6457 repo_path = realpath(optarg, NULL);
6458 if (repo_path == NULL)
6459 return got_error_from_errno2("realpath",
6460 optarg);
6461 break;
6462 default:
6463 usage_ref();
6464 /* NOTREACHED */
6468 argc -= optind;
6469 argv += optind;
6471 if (argc > 1)
6472 usage_ref();
6474 if (repo_path == NULL) {
6475 cwd = getcwd(NULL, 0);
6476 if (cwd == NULL)
6477 return got_error_from_errno("getcwd");
6478 error = got_worktree_open(&worktree, cwd);
6479 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6480 goto done;
6481 if (worktree)
6482 repo_path =
6483 strdup(got_worktree_get_repo_path(worktree));
6484 else
6485 repo_path = strdup(cwd);
6486 if (repo_path == NULL) {
6487 error = got_error_from_errno("strdup");
6488 goto done;
6492 error = got_repo_open(&repo, repo_path, NULL);
6493 if (error != NULL)
6494 goto done;
6496 init_curses();
6498 error = apply_unveil(got_repo_get_path(repo), NULL);
6499 if (error)
6500 goto done;
6502 error = tog_load_refs(repo, 0);
6503 if (error)
6504 goto done;
6506 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6507 if (view == NULL) {
6508 error = got_error_from_errno("view_open");
6509 goto done;
6512 error = open_ref_view(view, repo);
6513 if (error)
6514 goto done;
6516 if (worktree) {
6517 /* Release work tree lock. */
6518 got_worktree_close(worktree);
6519 worktree = NULL;
6521 error = view_loop(view);
6522 done:
6523 free(repo_path);
6524 free(cwd);
6525 if (repo) {
6526 const struct got_error *close_err = got_repo_close(repo);
6527 if (close_err)
6528 error = close_err;
6530 tog_free_refs();
6531 return error;
6534 static void
6535 list_commands(FILE *fp)
6537 size_t i;
6539 fprintf(fp, "commands:");
6540 for (i = 0; i < nitems(tog_commands); i++) {
6541 const struct tog_cmd *cmd = &tog_commands[i];
6542 fprintf(fp, " %s", cmd->name);
6544 fputc('\n', fp);
6547 __dead static void
6548 usage(int hflag, int status)
6550 FILE *fp = (status == 0) ? stdout : stderr;
6552 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6553 getprogname());
6554 if (hflag) {
6555 fprintf(fp, "lazy usage: %s path\n", getprogname());
6556 list_commands(fp);
6558 exit(status);
6561 static char **
6562 make_argv(int argc, ...)
6564 va_list ap;
6565 char **argv;
6566 int i;
6568 va_start(ap, argc);
6570 argv = calloc(argc, sizeof(char *));
6571 if (argv == NULL)
6572 err(1, "calloc");
6573 for (i = 0; i < argc; i++) {
6574 argv[i] = strdup(va_arg(ap, char *));
6575 if (argv[i] == NULL)
6576 err(1, "strdup");
6579 va_end(ap);
6580 return argv;
6584 * Try to convert 'tog path' into a 'tog log path' command.
6585 * The user could simply have mistyped the command rather than knowingly
6586 * provided a path. So check whether argv[0] can in fact be resolved
6587 * to a path in the HEAD commit and print a special error if not.
6588 * This hack is for mpi@ <3
6590 static const struct got_error *
6591 tog_log_with_path(int argc, char *argv[])
6593 const struct got_error *error = NULL, *close_err;
6594 const struct tog_cmd *cmd = NULL;
6595 struct got_repository *repo = NULL;
6596 struct got_worktree *worktree = NULL;
6597 struct got_object_id *commit_id = NULL, *id = NULL;
6598 struct got_commit_object *commit = NULL;
6599 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6600 char *commit_id_str = NULL, **cmd_argv = NULL;
6602 cwd = getcwd(NULL, 0);
6603 if (cwd == NULL)
6604 return got_error_from_errno("getcwd");
6606 error = got_worktree_open(&worktree, cwd);
6607 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6608 goto done;
6610 if (worktree)
6611 repo_path = strdup(got_worktree_get_repo_path(worktree));
6612 else
6613 repo_path = strdup(cwd);
6614 if (repo_path == NULL) {
6615 error = got_error_from_errno("strdup");
6616 goto done;
6619 error = got_repo_open(&repo, repo_path, NULL);
6620 if (error != NULL)
6621 goto done;
6623 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6624 repo, worktree);
6625 if (error)
6626 goto done;
6628 error = tog_load_refs(repo, 0);
6629 if (error)
6630 goto done;
6631 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6632 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6633 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6634 if (error)
6635 goto done;
6637 if (worktree) {
6638 got_worktree_close(worktree);
6639 worktree = NULL;
6642 error = got_object_open_as_commit(&commit, repo, commit_id);
6643 if (error)
6644 goto done;
6646 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6647 if (error) {
6648 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6649 goto done;
6650 fprintf(stderr, "%s: '%s' is no known command or path\n",
6651 getprogname(), argv[0]);
6652 usage(1, 1);
6653 /* not reached */
6656 close_err = got_repo_close(repo);
6657 if (error == NULL)
6658 error = close_err;
6659 repo = NULL;
6661 error = got_object_id_str(&commit_id_str, commit_id);
6662 if (error)
6663 goto done;
6665 cmd = &tog_commands[0]; /* log */
6666 argc = 4;
6667 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6668 error = cmd->cmd_main(argc, cmd_argv);
6669 done:
6670 if (repo) {
6671 close_err = got_repo_close(repo);
6672 if (error == NULL)
6673 error = close_err;
6675 if (commit)
6676 got_object_commit_close(commit);
6677 if (worktree)
6678 got_worktree_close(worktree);
6679 free(id);
6680 free(commit_id_str);
6681 free(commit_id);
6682 free(cwd);
6683 free(repo_path);
6684 free(in_repo_path);
6685 if (cmd_argv) {
6686 int i;
6687 for (i = 0; i < argc; i++)
6688 free(cmd_argv[i]);
6689 free(cmd_argv);
6691 tog_free_refs();
6692 return error;
6695 int
6696 main(int argc, char *argv[])
6698 const struct got_error *error = NULL;
6699 const struct tog_cmd *cmd = NULL;
6700 int ch, hflag = 0, Vflag = 0;
6701 char **cmd_argv = NULL;
6702 static const struct option longopts[] = {
6703 { "version", no_argument, NULL, 'V' },
6704 { NULL, 0, NULL, 0}
6707 setlocale(LC_CTYPE, "");
6709 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6710 switch (ch) {
6711 case 'h':
6712 hflag = 1;
6713 break;
6714 case 'V':
6715 Vflag = 1;
6716 break;
6717 default:
6718 usage(hflag, 1);
6719 /* NOTREACHED */
6723 argc -= optind;
6724 argv += optind;
6725 optind = 1;
6726 optreset = 1;
6728 if (Vflag) {
6729 got_version_print_str();
6730 return 0;
6733 #ifndef PROFILE
6734 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6735 NULL) == -1)
6736 err(1, "pledge");
6737 #endif
6739 if (argc == 0) {
6740 if (hflag)
6741 usage(hflag, 0);
6742 /* Build an argument vector which runs a default command. */
6743 cmd = &tog_commands[0];
6744 argc = 1;
6745 cmd_argv = make_argv(argc, cmd->name);
6746 } else {
6747 size_t i;
6749 /* Did the user specify a command? */
6750 for (i = 0; i < nitems(tog_commands); i++) {
6751 if (strncmp(tog_commands[i].name, argv[0],
6752 strlen(argv[0])) == 0) {
6753 cmd = &tog_commands[i];
6754 break;
6759 if (cmd == NULL) {
6760 if (argc != 1)
6761 usage(0, 1);
6762 /* No command specified; try log with a path */
6763 error = tog_log_with_path(argc, argv);
6764 } else {
6765 if (hflag)
6766 cmd->cmd_usage();
6767 else
6768 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6771 endwin();
6772 putchar('\n');
6773 if (cmd_argv) {
6774 int i;
6775 for (i = 0; i < argc; i++)
6776 free(cmd_argv[i]);
6777 free(cmd_argv);
6780 if (error && error->code != GOT_ERR_CANCELLED)
6781 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6782 return 0;