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 <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
51 #include "got_diff.h"
52 #include "got_opentemp.h"
53 #include "got_utf8.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_path.h"
59 #include "got_worktree.h"
61 //#define update_panels() (0)
62 //#define doupdate() (0)
64 #ifndef MIN
65 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #endif
68 #ifndef MAX
69 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 #endif
72 #ifndef CTRL
73 #define CTRL(x) ((x) & 0x1f)
74 #endif
76 #ifndef nitems
77 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
78 #endif
80 struct tog_cmd {
81 const char *name;
82 const struct got_error *(*cmd_main)(int, char *[]);
83 void (*cmd_usage)(void);
84 };
86 __dead static void usage(int, int);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_ref(void);
93 static const struct got_error* cmd_log(int, char *[]);
94 static const struct got_error* cmd_diff(int, char *[]);
95 static const struct got_error* cmd_blame(int, char *[]);
96 static const struct got_error* cmd_tree(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
99 static const struct tog_cmd tog_commands[] = {
100 { "log", cmd_log, usage_log },
101 { "diff", cmd_diff, usage_diff },
102 { "blame", cmd_blame, usage_blame },
103 { "tree", cmd_tree, usage_tree },
104 { "ref", cmd_ref, usage_ref },
105 };
107 enum tog_view_type {
108 TOG_VIEW_DIFF,
109 TOG_VIEW_LOG,
110 TOG_VIEW_BLAME,
111 TOG_VIEW_TREE,
112 TOG_VIEW_REF,
113 };
115 #define TOG_EOF_STRING "(END)"
117 struct commit_queue_entry {
118 TAILQ_ENTRY(commit_queue_entry) entry;
119 struct got_object_id *id;
120 struct got_commit_object *commit;
121 int idx;
122 };
123 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 struct commit_queue {
125 int ncommits;
126 struct commit_queue_head head;
127 };
129 struct tog_color {
130 STAILQ_ENTRY(tog_color) entry;
131 regex_t regex;
132 short colorpair;
133 };
134 STAILQ_HEAD(tog_colors, tog_color);
136 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static const struct got_error *
140 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
141 struct got_reference* re2)
143 const char *name1 = got_ref_get_name(re1);
144 const char *name2 = got_ref_get_name(re2);
145 int isbackup1, isbackup2;
147 /* Sort backup refs towards the bottom of the list. */
148 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
149 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
150 if (!isbackup1 && isbackup2) {
151 *cmp = -1;
152 return NULL;
153 } else if (isbackup1 && !isbackup2) {
154 *cmp = 1;
155 return NULL;
158 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
159 return NULL;
162 static const struct got_error *
163 tog_load_refs(struct got_repository *repo, int sort_by_date)
165 const struct got_error *err;
167 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
168 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
169 repo);
170 if (err)
171 return err;
173 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
174 repo);
177 static void
178 tog_free_refs(void)
180 if (tog_refs_idmap) {
181 got_reflist_object_id_map_free(tog_refs_idmap);
182 tog_refs_idmap = NULL;
184 got_ref_list_free(&tog_refs);
187 static const struct got_error *
188 add_color(struct tog_colors *colors, const char *pattern,
189 int idx, short color)
191 const struct got_error *err = NULL;
192 struct tog_color *tc;
193 int regerr = 0;
195 if (idx < 1 || idx > COLOR_PAIRS - 1)
196 return NULL;
198 init_pair(idx, color, -1);
200 tc = calloc(1, sizeof(*tc));
201 if (tc == NULL)
202 return got_error_from_errno("calloc");
203 regerr = regcomp(&tc->regex, pattern,
204 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
205 if (regerr) {
206 static char regerr_msg[512];
207 static char err_msg[512];
208 regerror(regerr, &tc->regex, regerr_msg,
209 sizeof(regerr_msg));
210 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
211 regerr_msg);
212 err = got_error_msg(GOT_ERR_REGEX, err_msg);
213 free(tc);
214 return err;
216 tc->colorpair = idx;
217 STAILQ_INSERT_HEAD(colors, tc, entry);
218 return NULL;
221 static void
222 free_colors(struct tog_colors *colors)
224 struct tog_color *tc;
226 while (!STAILQ_EMPTY(colors)) {
227 tc = STAILQ_FIRST(colors);
228 STAILQ_REMOVE_HEAD(colors, entry);
229 regfree(&tc->regex);
230 free(tc);
234 struct tog_color *
235 get_color(struct tog_colors *colors, int colorpair)
237 struct tog_color *tc = NULL;
239 STAILQ_FOREACH(tc, colors, entry) {
240 if (tc->colorpair == colorpair)
241 return tc;
244 return NULL;
247 static int
248 default_color_value(const char *envvar)
250 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
251 return COLOR_MAGENTA;
252 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
253 return COLOR_CYAN;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
255 return COLOR_YELLOW;
256 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
257 return COLOR_GREEN;
258 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
259 return COLOR_MAGENTA;
260 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
263 return COLOR_CYAN;
264 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
265 return COLOR_GREEN;
266 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
267 return COLOR_GREEN;
268 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
269 return COLOR_CYAN;
270 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
271 return COLOR_YELLOW;
272 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
273 return COLOR_GREEN;
274 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
275 return COLOR_MAGENTA;
276 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
277 return COLOR_YELLOW;
278 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
279 return COLOR_CYAN;
281 return -1;
284 static int
285 get_color_value(const char *envvar)
287 const char *val = getenv(envvar);
289 if (val == NULL)
290 return default_color_value(envvar);
292 if (strcasecmp(val, "black") == 0)
293 return COLOR_BLACK;
294 if (strcasecmp(val, "red") == 0)
295 return COLOR_RED;
296 if (strcasecmp(val, "green") == 0)
297 return COLOR_GREEN;
298 if (strcasecmp(val, "yellow") == 0)
299 return COLOR_YELLOW;
300 if (strcasecmp(val, "blue") == 0)
301 return COLOR_BLUE;
302 if (strcasecmp(val, "magenta") == 0)
303 return COLOR_MAGENTA;
304 if (strcasecmp(val, "cyan") == 0)
305 return COLOR_CYAN;
306 if (strcasecmp(val, "white") == 0)
307 return COLOR_WHITE;
308 if (strcasecmp(val, "default") == 0)
309 return -1;
311 return default_color_value(envvar);
315 struct tog_diff_view_state {
316 struct got_object_id *id1, *id2;
317 const char *label1, *label2;
318 FILE *f;
319 int first_displayed_line;
320 int last_displayed_line;
321 int eof;
322 int diff_context;
323 int ignore_whitespace;
324 int force_text_diff;
325 struct got_repository *repo;
326 struct tog_colors colors;
327 size_t nlines;
328 off_t *line_offsets;
329 int matched_line;
330 int selected_line;
332 /* passed from log view; may be NULL */
333 struct tog_view *log_view;
334 };
336 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
338 struct tog_log_thread_args {
339 pthread_cond_t need_commits;
340 pthread_cond_t commit_loaded;
341 int commits_needed;
342 int load_all;
343 struct got_commit_graph *graph;
344 struct commit_queue *commits;
345 const char *in_repo_path;
346 struct got_object_id *start_id;
347 struct got_repository *repo;
348 int log_complete;
349 sig_atomic_t *quit;
350 struct commit_queue_entry **first_displayed_entry;
351 struct commit_queue_entry **selected_entry;
352 int *searching;
353 int *search_next_done;
354 regex_t *regex;
355 };
357 struct tog_log_view_state {
358 struct commit_queue commits;
359 struct commit_queue_entry *first_displayed_entry;
360 struct commit_queue_entry *last_displayed_entry;
361 struct commit_queue_entry *selected_entry;
362 int selected;
363 char *in_repo_path;
364 char *head_ref_name;
365 int log_branches;
366 struct got_repository *repo;
367 struct got_object_id *start_id;
368 sig_atomic_t quit;
369 pthread_t thread;
370 struct tog_log_thread_args thread_args;
371 struct commit_queue_entry *matched_entry;
372 struct commit_queue_entry *search_entry;
373 struct tog_colors colors;
374 };
376 #define TOG_COLOR_DIFF_MINUS 1
377 #define TOG_COLOR_DIFF_PLUS 2
378 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
379 #define TOG_COLOR_DIFF_META 4
380 #define TOG_COLOR_TREE_SUBMODULE 5
381 #define TOG_COLOR_TREE_SYMLINK 6
382 #define TOG_COLOR_TREE_DIRECTORY 7
383 #define TOG_COLOR_TREE_EXECUTABLE 8
384 #define TOG_COLOR_COMMIT 9
385 #define TOG_COLOR_AUTHOR 10
386 #define TOG_COLOR_DATE 11
387 #define TOG_COLOR_REFS_HEADS 12
388 #define TOG_COLOR_REFS_TAGS 13
389 #define TOG_COLOR_REFS_REMOTES 14
390 #define TOG_COLOR_REFS_BACKUP 15
392 struct tog_blame_cb_args {
393 struct tog_blame_line *lines; /* one per line */
394 int nlines;
396 struct tog_view *view;
397 struct got_object_id *commit_id;
398 int *quit;
399 };
401 struct tog_blame_thread_args {
402 const char *path;
403 struct got_repository *repo;
404 struct tog_blame_cb_args *cb_args;
405 int *complete;
406 got_cancel_cb cancel_cb;
407 void *cancel_arg;
408 };
410 struct tog_blame {
411 FILE *f;
412 off_t filesize;
413 struct tog_blame_line *lines;
414 int nlines;
415 off_t *line_offsets;
416 pthread_t thread;
417 struct tog_blame_thread_args thread_args;
418 struct tog_blame_cb_args cb_args;
419 const char *path;
420 };
422 struct tog_blame_view_state {
423 int first_displayed_line;
424 int last_displayed_line;
425 int selected_line;
426 int blame_complete;
427 int eof;
428 int done;
429 struct got_object_id_queue blamed_commits;
430 struct got_object_qid *blamed_commit;
431 char *path;
432 struct got_repository *repo;
433 struct got_object_id *commit_id;
434 struct tog_blame blame;
435 int matched_line;
436 struct tog_colors colors;
437 };
439 struct tog_parent_tree {
440 TAILQ_ENTRY(tog_parent_tree) entry;
441 struct got_tree_object *tree;
442 struct got_tree_entry *first_displayed_entry;
443 struct got_tree_entry *selected_entry;
444 int selected;
445 };
447 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
449 struct tog_tree_view_state {
450 char *tree_label;
451 struct got_object_id *commit_id;/* commit which this tree belongs to */
452 struct got_tree_object *root; /* the commit's root tree entry */
453 struct got_tree_object *tree; /* currently displayed (sub-)tree */
454 struct got_tree_entry *first_displayed_entry;
455 struct got_tree_entry *last_displayed_entry;
456 struct got_tree_entry *selected_entry;
457 int ndisplayed, selected, show_ids;
458 struct tog_parent_trees parents; /* parent trees of current sub-tree */
459 char *head_ref_name;
460 struct got_repository *repo;
461 struct got_tree_entry *matched_entry;
462 struct tog_colors colors;
463 };
465 struct tog_reflist_entry {
466 TAILQ_ENTRY(tog_reflist_entry) entry;
467 struct got_reference *ref;
468 int idx;
469 };
471 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
473 struct tog_ref_view_state {
474 struct tog_reflist_head refs;
475 struct tog_reflist_entry *first_displayed_entry;
476 struct tog_reflist_entry *last_displayed_entry;
477 struct tog_reflist_entry *selected_entry;
478 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
479 struct got_repository *repo;
480 struct tog_reflist_entry *matched_entry;
481 struct tog_colors colors;
482 };
484 /*
485 * We implement two types of views: parent views and child views.
487 * The 'Tab' key switches focus between a parent view and its child view.
488 * Child views are shown side-by-side to their parent view, provided
489 * there is enough screen estate.
491 * When a new view is opened from within a parent view, this new view
492 * becomes a child view of the parent view, replacing any existing child.
494 * When a new view is opened from within a child view, this new view
495 * becomes a parent view which will obscure the views below until the
496 * user quits the new parent view by typing 'q'.
498 * This list of views contains parent views only.
499 * Child views are only pointed to by their parent view.
500 */
501 TAILQ_HEAD(tog_view_list_head, tog_view);
503 struct tog_view {
504 TAILQ_ENTRY(tog_view) entry;
505 WINDOW *window;
506 PANEL *panel;
507 int nlines, ncols, begin_y, begin_x;
508 int lines, cols; /* copies of LINES and COLS */
509 int focussed; /* Only set on one parent or child view at a time. */
510 int dying;
511 struct tog_view *parent;
512 struct tog_view *child;
514 /*
515 * This flag is initially set on parent views when a new child view
516 * is created. It gets toggled when the 'Tab' key switches focus
517 * between parent and child.
518 * The flag indicates whether focus should be passed on to our child
519 * view if this parent view gets picked for focus after another parent
520 * view was closed. This prevents child views from losing focus in such
521 * situations.
522 */
523 int focus_child;
525 /* type-specific state */
526 enum tog_view_type type;
527 union {
528 struct tog_diff_view_state diff;
529 struct tog_log_view_state log;
530 struct tog_blame_view_state blame;
531 struct tog_tree_view_state tree;
532 struct tog_ref_view_state ref;
533 } state;
535 const struct got_error *(*show)(struct tog_view *);
536 const struct got_error *(*input)(struct tog_view **,
537 struct tog_view *, int);
538 const struct got_error *(*close)(struct tog_view *);
540 const struct got_error *(*search_start)(struct tog_view *);
541 const struct got_error *(*search_next)(struct tog_view *);
542 int search_started;
543 int searching;
544 #define TOG_SEARCH_FORWARD 1
545 #define TOG_SEARCH_BACKWARD 2
546 int search_next_done;
547 #define TOG_SEARCH_HAVE_MORE 1
548 #define TOG_SEARCH_NO_MORE 2
549 #define TOG_SEARCH_HAVE_NONE 3
550 regex_t regex;
551 regmatch_t regmatch;
552 };
554 static const struct got_error *open_diff_view(struct tog_view *,
555 struct got_object_id *, struct got_object_id *,
556 const char *, const char *, int, int, int, struct tog_view *,
557 struct got_repository *);
558 static const struct got_error *show_diff_view(struct tog_view *);
559 static const struct got_error *input_diff_view(struct tog_view **,
560 struct tog_view *, int);
561 static const struct got_error* close_diff_view(struct tog_view *);
562 static const struct got_error *search_start_diff_view(struct tog_view *);
563 static const struct got_error *search_next_diff_view(struct tog_view *);
565 static const struct got_error *open_log_view(struct tog_view *,
566 struct got_object_id *, struct got_repository *,
567 const char *, const char *, int);
568 static const struct got_error * show_log_view(struct tog_view *);
569 static const struct got_error *input_log_view(struct tog_view **,
570 struct tog_view *, int);
571 static const struct got_error *close_log_view(struct tog_view *);
572 static const struct got_error *search_start_log_view(struct tog_view *);
573 static const struct got_error *search_next_log_view(struct tog_view *);
575 static const struct got_error *open_blame_view(struct tog_view *, char *,
576 struct got_object_id *, struct got_repository *);
577 static const struct got_error *show_blame_view(struct tog_view *);
578 static const struct got_error *input_blame_view(struct tog_view **,
579 struct tog_view *, int);
580 static const struct got_error *close_blame_view(struct tog_view *);
581 static const struct got_error *search_start_blame_view(struct tog_view *);
582 static const struct got_error *search_next_blame_view(struct tog_view *);
584 static const struct got_error *open_tree_view(struct tog_view *,
585 struct got_object_id *, const char *, struct got_repository *);
586 static const struct got_error *show_tree_view(struct tog_view *);
587 static const struct got_error *input_tree_view(struct tog_view **,
588 struct tog_view *, int);
589 static const struct got_error *close_tree_view(struct tog_view *);
590 static const struct got_error *search_start_tree_view(struct tog_view *);
591 static const struct got_error *search_next_tree_view(struct tog_view *);
593 static const struct got_error *open_ref_view(struct tog_view *,
594 struct got_repository *);
595 static const struct got_error *show_ref_view(struct tog_view *);
596 static const struct got_error *input_ref_view(struct tog_view **,
597 struct tog_view *, int);
598 static const struct got_error *close_ref_view(struct tog_view *);
599 static const struct got_error *search_start_ref_view(struct tog_view *);
600 static const struct got_error *search_next_ref_view(struct tog_view *);
602 static volatile sig_atomic_t tog_sigwinch_received;
603 static volatile sig_atomic_t tog_sigpipe_received;
604 static volatile sig_atomic_t tog_sigcont_received;
606 static void
607 tog_sigwinch(int signo)
609 tog_sigwinch_received = 1;
612 static void
613 tog_sigpipe(int signo)
615 tog_sigpipe_received = 1;
618 static void
619 tog_sigcont(int signo)
621 tog_sigcont_received = 1;
624 static const struct got_error *
625 view_close(struct tog_view *view)
627 const struct got_error *err = NULL;
629 if (view->child) {
630 view_close(view->child);
631 view->child = NULL;
633 if (view->close)
634 err = view->close(view);
635 if (view->panel)
636 del_panel(view->panel);
637 if (view->window)
638 delwin(view->window);
639 free(view);
640 return err;
643 static struct tog_view *
644 view_open(int nlines, int ncols, int begin_y, int begin_x,
645 enum tog_view_type type)
647 struct tog_view *view = calloc(1, sizeof(*view));
649 if (view == NULL)
650 return NULL;
652 view->type = type;
653 view->lines = LINES;
654 view->cols = COLS;
655 view->nlines = nlines ? nlines : LINES - begin_y;
656 view->ncols = ncols ? ncols : COLS - begin_x;
657 view->begin_y = begin_y;
658 view->begin_x = begin_x;
659 view->window = newwin(nlines, ncols, begin_y, begin_x);
660 if (view->window == NULL) {
661 view_close(view);
662 return NULL;
664 view->panel = new_panel(view->window);
665 if (view->panel == NULL ||
666 set_panel_userptr(view->panel, view) != OK) {
667 view_close(view);
668 return NULL;
671 keypad(view->window, TRUE);
672 return view;
675 static int
676 view_split_begin_x(int begin_x)
678 if (begin_x > 0 || COLS < 120)
679 return 0;
680 return (COLS - MAX(COLS / 2, 80));
683 static const struct got_error *view_resize(struct tog_view *);
685 static const struct got_error *
686 view_splitscreen(struct tog_view *view)
688 const struct got_error *err = NULL;
690 view->begin_y = 0;
691 view->begin_x = view_split_begin_x(0);
692 view->nlines = LINES;
693 view->ncols = COLS - view->begin_x;
694 view->lines = LINES;
695 view->cols = COLS;
696 err = view_resize(view);
697 if (err)
698 return err;
700 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
701 return got_error_from_errno("mvwin");
703 return NULL;
706 static const struct got_error *
707 view_fullscreen(struct tog_view *view)
709 const struct got_error *err = NULL;
711 view->begin_x = 0;
712 view->begin_y = 0;
713 view->nlines = LINES;
714 view->ncols = COLS;
715 view->lines = LINES;
716 view->cols = COLS;
717 err = view_resize(view);
718 if (err)
719 return err;
721 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
722 return got_error_from_errno("mvwin");
724 return NULL;
727 static int
728 view_is_parent_view(struct tog_view *view)
730 return view->parent == NULL;
733 static const struct got_error *
734 view_resize(struct tog_view *view)
736 int nlines, ncols;
738 if (view->lines > LINES)
739 nlines = view->nlines - (view->lines - LINES);
740 else
741 nlines = view->nlines + (LINES - view->lines);
743 if (view->cols > COLS)
744 ncols = view->ncols - (view->cols - COLS);
745 else
746 ncols = view->ncols + (COLS - view->cols);
748 if (wresize(view->window, nlines, ncols) == ERR)
749 return got_error_from_errno("wresize");
750 if (replace_panel(view->panel, view->window) == ERR)
751 return got_error_from_errno("replace_panel");
752 wclear(view->window);
754 view->nlines = nlines;
755 view->ncols = ncols;
756 view->lines = LINES;
757 view->cols = COLS;
759 if (view->child) {
760 view->child->begin_x = view_split_begin_x(view->begin_x);
761 if (view->child->begin_x == 0) {
762 view_fullscreen(view->child);
763 if (view->child->focussed)
764 show_panel(view->child->panel);
765 else
766 show_panel(view->panel);
767 } else {
768 view_splitscreen(view->child);
769 show_panel(view->child->panel);
773 return NULL;
776 static const struct got_error *
777 view_close_child(struct tog_view *view)
779 const struct got_error *err = NULL;
781 if (view->child == NULL)
782 return NULL;
784 err = view_close(view->child);
785 view->child = NULL;
786 return err;
789 static void
790 view_set_child(struct tog_view *view, struct tog_view *child)
792 view->child = child;
793 child->parent = view;
796 static int
797 view_is_splitscreen(struct tog_view *view)
799 return view->begin_x > 0;
802 static void
803 tog_resizeterm(void)
805 int cols, lines;
806 struct winsize size;
808 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
809 cols = 80; /* Default */
810 lines = 24;
811 } else {
812 cols = size.ws_col;
813 lines = size.ws_row;
815 resize_term(lines, cols);
818 static const struct got_error *
819 view_search_start(struct tog_view *view)
821 const struct got_error *err = NULL;
822 char pattern[1024];
823 int ret;
825 if (view->search_started) {
826 regfree(&view->regex);
827 view->searching = 0;
828 memset(&view->regmatch, 0, sizeof(view->regmatch));
830 view->search_started = 0;
832 if (view->nlines < 1)
833 return NULL;
835 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
836 wclrtoeol(view->window);
838 nocbreak();
839 echo();
840 ret = wgetnstr(view->window, pattern, sizeof(pattern));
841 cbreak();
842 noecho();
843 if (ret == ERR)
844 return NULL;
846 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
847 err = view->search_start(view);
848 if (err) {
849 regfree(&view->regex);
850 return err;
852 view->search_started = 1;
853 view->searching = TOG_SEARCH_FORWARD;
854 view->search_next_done = 0;
855 view->search_next(view);
858 return NULL;
861 static const struct got_error *
862 view_input(struct tog_view **new, int *done, struct tog_view *view,
863 struct tog_view_list_head *views)
865 const struct got_error *err = NULL;
866 struct tog_view *v;
867 int ch, errcode;
869 *new = NULL;
871 /* Clear "no matches" indicator. */
872 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
873 view->search_next_done == TOG_SEARCH_HAVE_NONE)
874 view->search_next_done = TOG_SEARCH_HAVE_MORE;
876 if (view->searching && !view->search_next_done) {
877 errcode = pthread_mutex_unlock(&tog_mutex);
878 if (errcode)
879 return got_error_set_errno(errcode,
880 "pthread_mutex_unlock");
881 sched_yield();
882 errcode = pthread_mutex_lock(&tog_mutex);
883 if (errcode)
884 return got_error_set_errno(errcode,
885 "pthread_mutex_lock");
886 view->search_next(view);
887 return NULL;
890 nodelay(stdscr, FALSE);
891 /* Allow threads to make progress while we are waiting for input. */
892 errcode = pthread_mutex_unlock(&tog_mutex);
893 if (errcode)
894 return got_error_set_errno(errcode, "pthread_mutex_unlock");
895 ch = wgetch(view->window);
896 errcode = pthread_mutex_lock(&tog_mutex);
897 if (errcode)
898 return got_error_set_errno(errcode, "pthread_mutex_lock");
899 nodelay(stdscr, TRUE);
901 if (tog_sigwinch_received || tog_sigcont_received) {
902 tog_resizeterm();
903 tog_sigwinch_received = 0;
904 tog_sigcont_received = 0;
905 TAILQ_FOREACH(v, views, entry) {
906 err = view_resize(v);
907 if (err)
908 return err;
909 err = v->input(new, v, KEY_RESIZE);
910 if (err)
911 return err;
912 if (v->child) {
913 err = view_resize(v->child);
914 if (err)
915 return err;
916 err = v->child->input(new, v->child,
917 KEY_RESIZE);
918 if (err)
919 return err;
924 switch (ch) {
925 case '\t':
926 if (view->child) {
927 view->focussed = 0;
928 view->child->focussed = 1;
929 view->focus_child = 1;
930 } else if (view->parent) {
931 view->focussed = 0;
932 view->parent->focussed = 1;
933 view->parent->focus_child = 0;
935 break;
936 case 'q':
937 err = view->input(new, view, ch);
938 view->dying = 1;
939 break;
940 case 'Q':
941 *done = 1;
942 break;
943 case 'f':
944 if (view_is_parent_view(view)) {
945 if (view->child == NULL)
946 break;
947 if (view_is_splitscreen(view->child)) {
948 view->focussed = 0;
949 view->child->focussed = 1;
950 err = view_fullscreen(view->child);
951 } else
952 err = view_splitscreen(view->child);
953 if (err)
954 break;
955 err = view->child->input(new, view->child,
956 KEY_RESIZE);
957 } else {
958 if (view_is_splitscreen(view)) {
959 view->parent->focussed = 0;
960 view->focussed = 1;
961 err = view_fullscreen(view);
962 } else {
963 err = view_splitscreen(view);
965 if (err)
966 break;
967 err = view->input(new, view, KEY_RESIZE);
969 break;
970 case KEY_RESIZE:
971 break;
972 case '/':
973 if (view->search_start)
974 view_search_start(view);
975 else
976 err = view->input(new, view, ch);
977 break;
978 case 'N':
979 case 'n':
980 if (view->search_started && view->search_next) {
981 view->searching = (ch == 'n' ?
982 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
983 view->search_next_done = 0;
984 view->search_next(view);
985 } else
986 err = view->input(new, view, ch);
987 break;
988 default:
989 err = view->input(new, view, ch);
990 break;
993 return err;
996 void
997 view_vborder(struct tog_view *view)
999 PANEL *panel;
1000 const struct tog_view *view_above;
1002 if (view->parent)
1003 return view_vborder(view->parent);
1005 panel = panel_above(view->panel);
1006 if (panel == NULL)
1007 return;
1009 view_above = panel_userptr(panel);
1010 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1011 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1014 int
1015 view_needs_focus_indication(struct tog_view *view)
1017 if (view_is_parent_view(view)) {
1018 if (view->child == NULL || view->child->focussed)
1019 return 0;
1020 if (!view_is_splitscreen(view->child))
1021 return 0;
1022 } else if (!view_is_splitscreen(view))
1023 return 0;
1025 return view->focussed;
1028 static const struct got_error *
1029 view_loop(struct tog_view *view)
1031 const struct got_error *err = NULL;
1032 struct tog_view_list_head views;
1033 struct tog_view *new_view;
1034 int fast_refresh = 10;
1035 int done = 0, errcode;
1037 errcode = pthread_mutex_lock(&tog_mutex);
1038 if (errcode)
1039 return got_error_set_errno(errcode, "pthread_mutex_lock");
1041 TAILQ_INIT(&views);
1042 TAILQ_INSERT_HEAD(&views, view, entry);
1044 view->focussed = 1;
1045 err = view->show(view);
1046 if (err)
1047 return err;
1048 update_panels();
1049 doupdate();
1050 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1051 /* Refresh fast during initialization, then become slower. */
1052 if (fast_refresh && fast_refresh-- == 0)
1053 halfdelay(10); /* switch to once per second */
1055 err = view_input(&new_view, &done, view, &views);
1056 if (err)
1057 break;
1058 if (view->dying) {
1059 struct tog_view *v, *prev = NULL;
1061 if (view_is_parent_view(view))
1062 prev = TAILQ_PREV(view, tog_view_list_head,
1063 entry);
1064 else if (view->parent)
1065 prev = view->parent;
1067 if (view->parent) {
1068 view->parent->child = NULL;
1069 view->parent->focus_child = 0;
1070 } else
1071 TAILQ_REMOVE(&views, view, entry);
1073 err = view_close(view);
1074 if (err)
1075 goto done;
1077 view = NULL;
1078 TAILQ_FOREACH(v, &views, entry) {
1079 if (v->focussed)
1080 break;
1082 if (view == NULL && new_view == NULL) {
1083 /* No view has focus. Try to pick one. */
1084 if (prev)
1085 view = prev;
1086 else if (!TAILQ_EMPTY(&views)) {
1087 view = TAILQ_LAST(&views,
1088 tog_view_list_head);
1090 if (view) {
1091 if (view->focus_child) {
1092 view->child->focussed = 1;
1093 view = view->child;
1094 } else
1095 view->focussed = 1;
1099 if (new_view) {
1100 struct tog_view *v, *t;
1101 /* Only allow one parent view per type. */
1102 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1103 if (v->type != new_view->type)
1104 continue;
1105 TAILQ_REMOVE(&views, v, entry);
1106 err = view_close(v);
1107 if (err)
1108 goto done;
1109 break;
1111 TAILQ_INSERT_TAIL(&views, new_view, entry);
1112 view = new_view;
1114 if (view) {
1115 if (view_is_parent_view(view)) {
1116 if (view->child && view->child->focussed)
1117 view = view->child;
1118 } else {
1119 if (view->parent && view->parent->focussed)
1120 view = view->parent;
1122 show_panel(view->panel);
1123 if (view->child && view_is_splitscreen(view->child))
1124 show_panel(view->child->panel);
1125 if (view->parent && view_is_splitscreen(view)) {
1126 err = view->parent->show(view->parent);
1127 if (err)
1128 goto done;
1130 err = view->show(view);
1131 if (err)
1132 goto done;
1133 if (view->child) {
1134 err = view->child->show(view->child);
1135 if (err)
1136 goto done;
1138 update_panels();
1139 doupdate();
1142 done:
1143 while (!TAILQ_EMPTY(&views)) {
1144 view = TAILQ_FIRST(&views);
1145 TAILQ_REMOVE(&views, view, entry);
1146 view_close(view);
1149 errcode = pthread_mutex_unlock(&tog_mutex);
1150 if (errcode && err == NULL)
1151 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1153 return err;
1156 __dead static void
1157 usage_log(void)
1159 endwin();
1160 fprintf(stderr,
1161 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1162 getprogname());
1163 exit(1);
1166 /* Create newly allocated wide-character string equivalent to a byte string. */
1167 static const struct got_error *
1168 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1170 char *vis = NULL;
1171 const struct got_error *err = NULL;
1173 *ws = NULL;
1174 *wlen = mbstowcs(NULL, s, 0);
1175 if (*wlen == (size_t)-1) {
1176 int vislen;
1177 if (errno != EILSEQ)
1178 return got_error_from_errno("mbstowcs");
1180 /* byte string invalid in current encoding; try to "fix" it */
1181 err = got_mbsavis(&vis, &vislen, s);
1182 if (err)
1183 return err;
1184 *wlen = mbstowcs(NULL, vis, 0);
1185 if (*wlen == (size_t)-1) {
1186 err = got_error_from_errno("mbstowcs"); /* give up */
1187 goto done;
1191 *ws = calloc(*wlen + 1, sizeof(**ws));
1192 if (*ws == NULL) {
1193 err = got_error_from_errno("calloc");
1194 goto done;
1197 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1198 err = got_error_from_errno("mbstowcs");
1199 done:
1200 free(vis);
1201 if (err) {
1202 free(*ws);
1203 *ws = NULL;
1204 *wlen = 0;
1206 return err;
1209 /* Format a line for display, ensuring that it won't overflow a width limit. */
1210 static const struct got_error *
1211 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1212 int col_tab_align)
1214 const struct got_error *err = NULL;
1215 int cols = 0;
1216 wchar_t *wline = NULL;
1217 size_t wlen;
1218 int i;
1220 *wlinep = NULL;
1221 *widthp = 0;
1223 err = mbs2ws(&wline, &wlen, line);
1224 if (err)
1225 return err;
1227 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1228 wline[wlen - 1] = L'\0';
1229 wlen--;
1231 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1232 wline[wlen - 1] = L'\0';
1233 wlen--;
1236 i = 0;
1237 while (i < wlen) {
1238 int width = wcwidth(wline[i]);
1240 if (width == 0) {
1241 i++;
1242 continue;
1245 if (width == 1 || width == 2) {
1246 if (cols + width > wlimit)
1247 break;
1248 cols += width;
1249 i++;
1250 } else if (width == -1) {
1251 if (wline[i] == L'\t') {
1252 width = TABSIZE -
1253 ((cols + col_tab_align) % TABSIZE);
1254 } else {
1255 width = 1;
1256 wline[i] = L'.';
1258 if (cols + width > wlimit)
1259 break;
1260 cols += width;
1261 i++;
1262 } else {
1263 err = got_error_from_errno("wcwidth");
1264 goto done;
1267 wline[i] = L'\0';
1268 if (widthp)
1269 *widthp = cols;
1270 done:
1271 if (err)
1272 free(wline);
1273 else
1274 *wlinep = wline;
1275 return err;
1278 static const struct got_error*
1279 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1280 struct got_object_id *id, struct got_repository *repo)
1282 static const struct got_error *err = NULL;
1283 struct got_reflist_entry *re;
1284 char *s;
1285 const char *name;
1287 *refs_str = NULL;
1289 TAILQ_FOREACH(re, refs, entry) {
1290 struct got_tag_object *tag = NULL;
1291 struct got_object_id *ref_id;
1292 int cmp;
1294 name = got_ref_get_name(re->ref);
1295 if (strcmp(name, GOT_REF_HEAD) == 0)
1296 continue;
1297 if (strncmp(name, "refs/", 5) == 0)
1298 name += 5;
1299 if (strncmp(name, "got/", 4) == 0 &&
1300 strncmp(name, "got/backup/", 11) != 0)
1301 continue;
1302 if (strncmp(name, "heads/", 6) == 0)
1303 name += 6;
1304 if (strncmp(name, "remotes/", 8) == 0) {
1305 name += 8;
1306 s = strstr(name, "/" GOT_REF_HEAD);
1307 if (s != NULL && s[strlen(s)] == '\0')
1308 continue;
1310 err = got_ref_resolve(&ref_id, repo, re->ref);
1311 if (err)
1312 break;
1313 if (strncmp(name, "tags/", 5) == 0) {
1314 err = got_object_open_as_tag(&tag, repo, ref_id);
1315 if (err) {
1316 if (err->code != GOT_ERR_OBJ_TYPE) {
1317 free(ref_id);
1318 break;
1320 /* Ref points at something other than a tag. */
1321 err = NULL;
1322 tag = NULL;
1325 cmp = got_object_id_cmp(tag ?
1326 got_object_tag_get_object_id(tag) : ref_id, id);
1327 free(ref_id);
1328 if (tag)
1329 got_object_tag_close(tag);
1330 if (cmp != 0)
1331 continue;
1332 s = *refs_str;
1333 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1334 s ? ", " : "", name) == -1) {
1335 err = got_error_from_errno("asprintf");
1336 free(s);
1337 *refs_str = NULL;
1338 break;
1340 free(s);
1343 return err;
1346 static const struct got_error *
1347 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1348 int col_tab_align)
1350 char *smallerthan;
1352 smallerthan = strchr(author, '<');
1353 if (smallerthan && smallerthan[1] != '\0')
1354 author = smallerthan + 1;
1355 author[strcspn(author, "@>")] = '\0';
1356 return format_line(wauthor, author_width, author, limit, col_tab_align);
1359 static const struct got_error *
1360 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1361 struct got_object_id *id, const size_t date_display_cols,
1362 int author_display_cols)
1364 struct tog_log_view_state *s = &view->state.log;
1365 const struct got_error *err = NULL;
1366 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1367 char *logmsg0 = NULL, *logmsg = NULL;
1368 char *author = NULL;
1369 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1370 int author_width, logmsg_width;
1371 char *newline, *line = NULL;
1372 int col, limit;
1373 const int avail = view->ncols;
1374 struct tm tm;
1375 time_t committer_time;
1376 struct tog_color *tc;
1378 committer_time = got_object_commit_get_committer_time(commit);
1379 if (gmtime_r(&committer_time, &tm) == NULL)
1380 return got_error_from_errno("gmtime_r");
1381 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1382 return got_error(GOT_ERR_NO_SPACE);
1384 if (avail <= date_display_cols)
1385 limit = MIN(sizeof(datebuf) - 1, avail);
1386 else
1387 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1388 tc = get_color(&s->colors, TOG_COLOR_DATE);
1389 if (tc)
1390 wattr_on(view->window,
1391 COLOR_PAIR(tc->colorpair), NULL);
1392 waddnstr(view->window, datebuf, limit);
1393 if (tc)
1394 wattr_off(view->window,
1395 COLOR_PAIR(tc->colorpair), NULL);
1396 col = limit;
1397 if (col > avail)
1398 goto done;
1400 if (avail >= 120) {
1401 char *id_str;
1402 err = got_object_id_str(&id_str, id);
1403 if (err)
1404 goto done;
1405 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1406 if (tc)
1407 wattr_on(view->window,
1408 COLOR_PAIR(tc->colorpair), NULL);
1409 wprintw(view->window, "%.8s ", id_str);
1410 if (tc)
1411 wattr_off(view->window,
1412 COLOR_PAIR(tc->colorpair), NULL);
1413 free(id_str);
1414 col += 9;
1415 if (col > avail)
1416 goto done;
1419 author = strdup(got_object_commit_get_author(commit));
1420 if (author == NULL) {
1421 err = got_error_from_errno("strdup");
1422 goto done;
1424 err = format_author(&wauthor, &author_width, author, avail - col, col);
1425 if (err)
1426 goto done;
1427 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1428 if (tc)
1429 wattr_on(view->window,
1430 COLOR_PAIR(tc->colorpair), NULL);
1431 waddwstr(view->window, wauthor);
1432 if (tc)
1433 wattr_off(view->window,
1434 COLOR_PAIR(tc->colorpair), NULL);
1435 col += author_width;
1436 while (col < avail && author_width < author_display_cols + 2) {
1437 waddch(view->window, ' ');
1438 col++;
1439 author_width++;
1441 if (col > avail)
1442 goto done;
1444 err = got_object_commit_get_logmsg(&logmsg0, commit);
1445 if (err)
1446 goto done;
1447 logmsg = logmsg0;
1448 while (*logmsg == '\n')
1449 logmsg++;
1450 newline = strchr(logmsg, '\n');
1451 if (newline)
1452 *newline = '\0';
1453 limit = avail - col;
1454 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1455 if (err)
1456 goto done;
1457 waddwstr(view->window, wlogmsg);
1458 col += logmsg_width;
1459 while (col < avail) {
1460 waddch(view->window, ' ');
1461 col++;
1463 done:
1464 free(logmsg0);
1465 free(wlogmsg);
1466 free(author);
1467 free(wauthor);
1468 free(line);
1469 return err;
1472 static struct commit_queue_entry *
1473 alloc_commit_queue_entry(struct got_commit_object *commit,
1474 struct got_object_id *id)
1476 struct commit_queue_entry *entry;
1478 entry = calloc(1, sizeof(*entry));
1479 if (entry == NULL)
1480 return NULL;
1482 entry->id = id;
1483 entry->commit = commit;
1484 return entry;
1487 static void
1488 pop_commit(struct commit_queue *commits)
1490 struct commit_queue_entry *entry;
1492 entry = TAILQ_FIRST(&commits->head);
1493 TAILQ_REMOVE(&commits->head, entry, entry);
1494 got_object_commit_close(entry->commit);
1495 commits->ncommits--;
1496 /* Don't free entry->id! It is owned by the commit graph. */
1497 free(entry);
1500 static void
1501 free_commits(struct commit_queue *commits)
1503 while (!TAILQ_EMPTY(&commits->head))
1504 pop_commit(commits);
1507 static const struct got_error *
1508 match_commit(int *have_match, struct got_object_id *id,
1509 struct got_commit_object *commit, regex_t *regex)
1511 const struct got_error *err = NULL;
1512 regmatch_t regmatch;
1513 char *id_str = NULL, *logmsg = NULL;
1515 *have_match = 0;
1517 err = got_object_id_str(&id_str, id);
1518 if (err)
1519 return err;
1521 err = got_object_commit_get_logmsg(&logmsg, commit);
1522 if (err)
1523 goto done;
1525 if (regexec(regex, got_object_commit_get_author(commit), 1,
1526 &regmatch, 0) == 0 ||
1527 regexec(regex, got_object_commit_get_committer(commit), 1,
1528 &regmatch, 0) == 0 ||
1529 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1530 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1531 *have_match = 1;
1532 done:
1533 free(id_str);
1534 free(logmsg);
1535 return err;
1538 static const struct got_error *
1539 queue_commits(struct tog_log_thread_args *a)
1541 const struct got_error *err = NULL;
1544 * We keep all commits open throughout the lifetime of the log
1545 * view in order to avoid having to re-fetch commits from disk
1546 * while updating the display.
1548 do {
1549 struct got_object_id *id;
1550 struct got_commit_object *commit;
1551 struct commit_queue_entry *entry;
1552 int errcode;
1554 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1555 NULL, NULL);
1556 if (err || id == NULL)
1557 break;
1559 err = got_object_open_as_commit(&commit, a->repo, id);
1560 if (err)
1561 break;
1562 entry = alloc_commit_queue_entry(commit, id);
1563 if (entry == NULL) {
1564 err = got_error_from_errno("alloc_commit_queue_entry");
1565 break;
1568 errcode = pthread_mutex_lock(&tog_mutex);
1569 if (errcode) {
1570 err = got_error_set_errno(errcode,
1571 "pthread_mutex_lock");
1572 break;
1575 entry->idx = a->commits->ncommits;
1576 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1577 a->commits->ncommits++;
1579 if (*a->searching == TOG_SEARCH_FORWARD &&
1580 !*a->search_next_done) {
1581 int have_match;
1582 err = match_commit(&have_match, id, commit, a->regex);
1583 if (err)
1584 break;
1585 if (have_match)
1586 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1589 errcode = pthread_mutex_unlock(&tog_mutex);
1590 if (errcode && err == NULL)
1591 err = got_error_set_errno(errcode,
1592 "pthread_mutex_unlock");
1593 if (err)
1594 break;
1595 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1597 return err;
1600 static void
1601 select_commit(struct tog_log_view_state *s)
1603 struct commit_queue_entry *entry;
1604 int ncommits = 0;
1606 entry = s->first_displayed_entry;
1607 while (entry) {
1608 if (ncommits == s->selected) {
1609 s->selected_entry = entry;
1610 break;
1612 entry = TAILQ_NEXT(entry, entry);
1613 ncommits++;
1617 static const struct got_error *
1618 draw_commits(struct tog_view *view)
1620 const struct got_error *err = NULL;
1621 struct tog_log_view_state *s = &view->state.log;
1622 struct commit_queue_entry *entry = s->selected_entry;
1623 const int limit = view->nlines;
1624 int width;
1625 int ncommits, author_cols = 4;
1626 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1627 char *refs_str = NULL;
1628 wchar_t *wline;
1629 struct tog_color *tc;
1630 static const size_t date_display_cols = 12;
1632 if (s->selected_entry &&
1633 !(view->searching && view->search_next_done == 0)) {
1634 struct got_reflist_head *refs;
1635 err = got_object_id_str(&id_str, s->selected_entry->id);
1636 if (err)
1637 return err;
1638 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1639 s->selected_entry->id);
1640 if (refs) {
1641 err = build_refs_str(&refs_str, refs,
1642 s->selected_entry->id, s->repo);
1643 if (err)
1644 goto done;
1648 if (s->thread_args.commits_needed == 0)
1649 halfdelay(10); /* disable fast refresh */
1651 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1652 if (asprintf(&ncommits_str, " [%d/%d] %s",
1653 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1654 (view->searching && !view->search_next_done) ?
1655 "searching..." : "loading...") == -1) {
1656 err = got_error_from_errno("asprintf");
1657 goto done;
1659 } else {
1660 const char *search_str = NULL;
1662 if (view->searching) {
1663 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1664 search_str = "no more matches";
1665 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1666 search_str = "no matches found";
1667 else if (!view->search_next_done)
1668 search_str = "searching...";
1671 if (asprintf(&ncommits_str, " [%d/%d] %s",
1672 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1673 search_str ? search_str :
1674 (refs_str ? refs_str : "")) == -1) {
1675 err = got_error_from_errno("asprintf");
1676 goto done;
1680 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1681 if (asprintf(&header, "commit %s %s%s",
1682 id_str ? id_str : "........................................",
1683 s->in_repo_path, ncommits_str) == -1) {
1684 err = got_error_from_errno("asprintf");
1685 header = NULL;
1686 goto done;
1688 } else if (asprintf(&header, "commit %s%s",
1689 id_str ? id_str : "........................................",
1690 ncommits_str) == -1) {
1691 err = got_error_from_errno("asprintf");
1692 header = NULL;
1693 goto done;
1695 err = format_line(&wline, &width, header, view->ncols, 0);
1696 if (err)
1697 goto done;
1699 werase(view->window);
1701 if (view_needs_focus_indication(view))
1702 wstandout(view->window);
1703 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1704 if (tc)
1705 wattr_on(view->window,
1706 COLOR_PAIR(tc->colorpair), NULL);
1707 waddwstr(view->window, wline);
1708 if (tc)
1709 wattr_off(view->window,
1710 COLOR_PAIR(tc->colorpair), NULL);
1711 while (width < view->ncols) {
1712 waddch(view->window, ' ');
1713 width++;
1715 if (view_needs_focus_indication(view))
1716 wstandend(view->window);
1717 free(wline);
1718 if (limit <= 1)
1719 goto done;
1721 /* Grow author column size if necessary. */
1722 entry = s->first_displayed_entry;
1723 ncommits = 0;
1724 while (entry) {
1725 char *author;
1726 wchar_t *wauthor;
1727 int width;
1728 if (ncommits >= limit - 1)
1729 break;
1730 author = strdup(got_object_commit_get_author(entry->commit));
1731 if (author == NULL) {
1732 err = got_error_from_errno("strdup");
1733 goto done;
1735 err = format_author(&wauthor, &width, author, COLS,
1736 date_display_cols);
1737 if (author_cols < width)
1738 author_cols = width;
1739 free(wauthor);
1740 free(author);
1741 ncommits++;
1742 entry = TAILQ_NEXT(entry, entry);
1745 entry = s->first_displayed_entry;
1746 s->last_displayed_entry = s->first_displayed_entry;
1747 ncommits = 0;
1748 while (entry) {
1749 if (ncommits >= limit - 1)
1750 break;
1751 if (ncommits == s->selected)
1752 wstandout(view->window);
1753 err = draw_commit(view, entry->commit, entry->id,
1754 date_display_cols, author_cols);
1755 if (ncommits == s->selected)
1756 wstandend(view->window);
1757 if (err)
1758 goto done;
1759 ncommits++;
1760 s->last_displayed_entry = entry;
1761 entry = TAILQ_NEXT(entry, entry);
1764 view_vborder(view);
1765 update_panels();
1766 doupdate();
1767 done:
1768 free(id_str);
1769 free(refs_str);
1770 free(ncommits_str);
1771 free(header);
1772 return err;
1775 static void
1776 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1778 struct commit_queue_entry *entry;
1779 int nscrolled = 0;
1781 entry = TAILQ_FIRST(&s->commits.head);
1782 if (s->first_displayed_entry == entry)
1783 return;
1785 entry = s->first_displayed_entry;
1786 while (entry && nscrolled < maxscroll) {
1787 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1788 if (entry) {
1789 s->first_displayed_entry = entry;
1790 nscrolled++;
1795 static const struct got_error *
1796 trigger_log_thread(struct tog_view *view, int wait)
1798 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1799 int errcode;
1801 halfdelay(1); /* fast refresh while loading commits */
1803 while (ta->commits_needed > 0 || ta->load_all) {
1804 if (ta->log_complete)
1805 break;
1807 /* Wake the log thread. */
1808 errcode = pthread_cond_signal(&ta->need_commits);
1809 if (errcode)
1810 return got_error_set_errno(errcode,
1811 "pthread_cond_signal");
1814 * The mutex will be released while the view loop waits
1815 * in wgetch(), at which time the log thread will run.
1817 if (!wait)
1818 break;
1820 /* Display progress update in log view. */
1821 show_log_view(view);
1822 update_panels();
1823 doupdate();
1825 /* Wait right here while next commit is being loaded. */
1826 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1827 if (errcode)
1828 return got_error_set_errno(errcode,
1829 "pthread_cond_wait");
1831 /* Display progress update in log view. */
1832 show_log_view(view);
1833 update_panels();
1834 doupdate();
1837 return NULL;
1840 static const struct got_error *
1841 log_scroll_down(struct tog_view *view, int maxscroll)
1843 struct tog_log_view_state *s = &view->state.log;
1844 const struct got_error *err = NULL;
1845 struct commit_queue_entry *pentry;
1846 int nscrolled = 0, ncommits_needed;
1848 if (s->last_displayed_entry == NULL)
1849 return NULL;
1851 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1852 if (s->commits.ncommits < ncommits_needed &&
1853 !s->thread_args.log_complete) {
1855 * Ask the log thread for required amount of commits.
1857 s->thread_args.commits_needed += maxscroll;
1858 err = trigger_log_thread(view, 1);
1859 if (err)
1860 return err;
1863 do {
1864 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1865 if (pentry == NULL)
1866 break;
1868 s->last_displayed_entry = pentry;
1870 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1871 if (pentry == NULL)
1872 break;
1873 s->first_displayed_entry = pentry;
1874 } while (++nscrolled < maxscroll);
1876 return err;
1879 static const struct got_error *
1880 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1881 struct got_commit_object *commit, struct got_object_id *commit_id,
1882 struct tog_view *log_view, struct got_repository *repo)
1884 const struct got_error *err;
1885 struct got_object_qid *parent_id;
1886 struct tog_view *diff_view;
1888 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1889 if (diff_view == NULL)
1890 return got_error_from_errno("view_open");
1892 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1893 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1894 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1895 if (err == NULL)
1896 *new_view = diff_view;
1897 return err;
1900 static const struct got_error *
1901 tree_view_visit_subtree(struct tog_tree_view_state *s,
1902 struct got_tree_object *subtree)
1904 struct tog_parent_tree *parent;
1906 parent = calloc(1, sizeof(*parent));
1907 if (parent == NULL)
1908 return got_error_from_errno("calloc");
1910 parent->tree = s->tree;
1911 parent->first_displayed_entry = s->first_displayed_entry;
1912 parent->selected_entry = s->selected_entry;
1913 parent->selected = s->selected;
1914 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1915 s->tree = subtree;
1916 s->selected = 0;
1917 s->first_displayed_entry = NULL;
1918 return NULL;
1921 static const struct got_error *
1922 tree_view_walk_path(struct tog_tree_view_state *s,
1923 struct got_commit_object *commit, const char *path)
1925 const struct got_error *err = NULL;
1926 struct got_tree_object *tree = NULL;
1927 const char *p;
1928 char *slash, *subpath = NULL;
1930 /* Walk the path and open corresponding tree objects. */
1931 p = path;
1932 while (*p) {
1933 struct got_tree_entry *te;
1934 struct got_object_id *tree_id;
1935 char *te_name;
1937 while (p[0] == '/')
1938 p++;
1940 /* Ensure the correct subtree entry is selected. */
1941 slash = strchr(p, '/');
1942 if (slash == NULL)
1943 te_name = strdup(p);
1944 else
1945 te_name = strndup(p, slash - p);
1946 if (te_name == NULL) {
1947 err = got_error_from_errno("strndup");
1948 break;
1950 te = got_object_tree_find_entry(s->tree, te_name);
1951 if (te == NULL) {
1952 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1953 free(te_name);
1954 break;
1956 free(te_name);
1957 s->first_displayed_entry = s->selected_entry = te;
1959 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1960 break; /* jump to this file's entry */
1962 slash = strchr(p, '/');
1963 if (slash)
1964 subpath = strndup(path, slash - path);
1965 else
1966 subpath = strdup(path);
1967 if (subpath == NULL) {
1968 err = got_error_from_errno("strdup");
1969 break;
1972 err = got_object_id_by_path(&tree_id, s->repo, commit,
1973 subpath);
1974 if (err)
1975 break;
1977 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1978 free(tree_id);
1979 if (err)
1980 break;
1982 err = tree_view_visit_subtree(s, tree);
1983 if (err) {
1984 got_object_tree_close(tree);
1985 break;
1987 if (slash == NULL)
1988 break;
1989 free(subpath);
1990 subpath = NULL;
1991 p = slash;
1994 free(subpath);
1995 return err;
1998 static const struct got_error *
1999 browse_commit_tree(struct tog_view **new_view, int begin_x,
2000 struct commit_queue_entry *entry, const char *path,
2001 const char *head_ref_name, struct got_repository *repo)
2003 const struct got_error *err = NULL;
2004 struct tog_tree_view_state *s;
2005 struct tog_view *tree_view;
2007 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2008 if (tree_view == NULL)
2009 return got_error_from_errno("view_open");
2011 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2012 if (err)
2013 return err;
2014 s = &tree_view->state.tree;
2016 *new_view = tree_view;
2018 if (got_path_is_root_dir(path))
2019 return NULL;
2021 return tree_view_walk_path(s, entry->commit, path);
2024 static const struct got_error *
2025 block_signals_used_by_main_thread(void)
2027 sigset_t sigset;
2028 int errcode;
2030 if (sigemptyset(&sigset) == -1)
2031 return got_error_from_errno("sigemptyset");
2033 /* tog handles SIGWINCH and SIGCONT */
2034 if (sigaddset(&sigset, SIGWINCH) == -1)
2035 return got_error_from_errno("sigaddset");
2036 if (sigaddset(&sigset, SIGCONT) == -1)
2037 return got_error_from_errno("sigaddset");
2039 /* ncurses handles SIGTSTP */
2040 if (sigaddset(&sigset, SIGTSTP) == -1)
2041 return got_error_from_errno("sigaddset");
2043 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2044 if (errcode)
2045 return got_error_set_errno(errcode, "pthread_sigmask");
2047 return NULL;
2050 static void *
2051 log_thread(void *arg)
2053 const struct got_error *err = NULL;
2054 int errcode = 0;
2055 struct tog_log_thread_args *a = arg;
2056 int done = 0;
2058 err = block_signals_used_by_main_thread();
2059 if (err)
2060 return (void *)err;
2062 while (!done && !err && !tog_sigpipe_received) {
2063 err = queue_commits(a);
2064 if (err) {
2065 if (err->code != GOT_ERR_ITER_COMPLETED)
2066 return (void *)err;
2067 err = NULL;
2068 done = 1;
2069 } else if (a->commits_needed > 0 && !a->load_all)
2070 a->commits_needed--;
2072 errcode = pthread_mutex_lock(&tog_mutex);
2073 if (errcode) {
2074 err = got_error_set_errno(errcode,
2075 "pthread_mutex_lock");
2076 break;
2077 } else if (*a->quit)
2078 done = 1;
2079 else if (*a->first_displayed_entry == NULL) {
2080 *a->first_displayed_entry =
2081 TAILQ_FIRST(&a->commits->head);
2082 *a->selected_entry = *a->first_displayed_entry;
2085 errcode = pthread_cond_signal(&a->commit_loaded);
2086 if (errcode) {
2087 err = got_error_set_errno(errcode,
2088 "pthread_cond_signal");
2089 pthread_mutex_unlock(&tog_mutex);
2090 break;
2093 if (done)
2094 a->commits_needed = 0;
2095 else {
2096 if (a->commits_needed == 0 && !a->load_all) {
2097 errcode = pthread_cond_wait(&a->need_commits,
2098 &tog_mutex);
2099 if (errcode)
2100 err = got_error_set_errno(errcode,
2101 "pthread_cond_wait");
2102 if (*a->quit)
2103 done = 1;
2107 errcode = pthread_mutex_unlock(&tog_mutex);
2108 if (errcode && err == NULL)
2109 err = got_error_set_errno(errcode,
2110 "pthread_mutex_unlock");
2112 a->log_complete = 1;
2113 return (void *)err;
2116 static const struct got_error *
2117 stop_log_thread(struct tog_log_view_state *s)
2119 const struct got_error *err = NULL;
2120 int errcode;
2122 if (s->thread) {
2123 s->quit = 1;
2124 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2125 if (errcode)
2126 return got_error_set_errno(errcode,
2127 "pthread_cond_signal");
2128 errcode = pthread_mutex_unlock(&tog_mutex);
2129 if (errcode)
2130 return got_error_set_errno(errcode,
2131 "pthread_mutex_unlock");
2132 errcode = pthread_join(s->thread, (void **)&err);
2133 if (errcode)
2134 return got_error_set_errno(errcode, "pthread_join");
2135 errcode = pthread_mutex_lock(&tog_mutex);
2136 if (errcode)
2137 return got_error_set_errno(errcode,
2138 "pthread_mutex_lock");
2139 s->thread = 0; //NULL;
2142 if (s->thread_args.repo) {
2143 err = got_repo_close(s->thread_args.repo);
2144 s->thread_args.repo = NULL;
2147 if (s->thread_args.graph) {
2148 got_commit_graph_close(s->thread_args.graph);
2149 s->thread_args.graph = NULL;
2152 return err;
2155 static const struct got_error *
2156 close_log_view(struct tog_view *view)
2158 const struct got_error *err = NULL;
2159 struct tog_log_view_state *s = &view->state.log;
2160 int errcode;
2162 err = stop_log_thread(s);
2164 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2165 if (errcode && err == NULL)
2166 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2168 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2169 if (errcode && err == NULL)
2170 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2172 free_commits(&s->commits);
2173 free(s->in_repo_path);
2174 s->in_repo_path = NULL;
2175 free(s->start_id);
2176 s->start_id = NULL;
2177 free(s->head_ref_name);
2178 s->head_ref_name = NULL;
2179 return err;
2182 static const struct got_error *
2183 search_start_log_view(struct tog_view *view)
2185 struct tog_log_view_state *s = &view->state.log;
2187 s->matched_entry = NULL;
2188 s->search_entry = NULL;
2189 return NULL;
2192 static const struct got_error *
2193 search_next_log_view(struct tog_view *view)
2195 const struct got_error *err = NULL;
2196 struct tog_log_view_state *s = &view->state.log;
2197 struct commit_queue_entry *entry;
2199 /* Display progress update in log view. */
2200 show_log_view(view);
2201 update_panels();
2202 doupdate();
2204 if (s->search_entry) {
2205 int errcode, ch;
2206 errcode = pthread_mutex_unlock(&tog_mutex);
2207 if (errcode)
2208 return got_error_set_errno(errcode,
2209 "pthread_mutex_unlock");
2210 ch = wgetch(view->window);
2211 errcode = pthread_mutex_lock(&tog_mutex);
2212 if (errcode)
2213 return got_error_set_errno(errcode,
2214 "pthread_mutex_lock");
2215 if (ch == KEY_BACKSPACE) {
2216 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2217 return NULL;
2219 if (view->searching == TOG_SEARCH_FORWARD)
2220 entry = TAILQ_NEXT(s->search_entry, entry);
2221 else
2222 entry = TAILQ_PREV(s->search_entry,
2223 commit_queue_head, entry);
2224 } else if (s->matched_entry) {
2225 if (view->searching == TOG_SEARCH_FORWARD)
2226 entry = TAILQ_NEXT(s->matched_entry, entry);
2227 else
2228 entry = TAILQ_PREV(s->matched_entry,
2229 commit_queue_head, entry);
2230 } else {
2231 entry = s->selected_entry;
2234 while (1) {
2235 int have_match = 0;
2237 if (entry == NULL) {
2238 if (s->thread_args.log_complete ||
2239 view->searching == TOG_SEARCH_BACKWARD) {
2240 view->search_next_done =
2241 (s->matched_entry == NULL ?
2242 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2243 s->search_entry = NULL;
2244 return NULL;
2247 * Poke the log thread for more commits and return,
2248 * allowing the main loop to make progress. Search
2249 * will resume at s->search_entry once we come back.
2251 s->thread_args.commits_needed++;
2252 return trigger_log_thread(view, 0);
2255 err = match_commit(&have_match, entry->id, entry->commit,
2256 &view->regex);
2257 if (err)
2258 break;
2259 if (have_match) {
2260 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2261 s->matched_entry = entry;
2262 break;
2265 s->search_entry = entry;
2266 if (view->searching == TOG_SEARCH_FORWARD)
2267 entry = TAILQ_NEXT(entry, entry);
2268 else
2269 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2272 if (s->matched_entry) {
2273 int cur = s->selected_entry->idx;
2274 while (cur < s->matched_entry->idx) {
2275 err = input_log_view(NULL, view, KEY_DOWN);
2276 if (err)
2277 return err;
2278 cur++;
2280 while (cur > s->matched_entry->idx) {
2281 err = input_log_view(NULL, view, KEY_UP);
2282 if (err)
2283 return err;
2284 cur--;
2288 s->search_entry = NULL;
2290 return NULL;
2293 static const struct got_error *
2294 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2295 struct got_repository *repo, const char *head_ref_name,
2296 const char *in_repo_path, int log_branches)
2298 const struct got_error *err = NULL;
2299 struct tog_log_view_state *s = &view->state.log;
2300 struct got_repository *thread_repo = NULL;
2301 struct got_commit_graph *thread_graph = NULL;
2302 int errcode;
2304 if (in_repo_path != s->in_repo_path) {
2305 free(s->in_repo_path);
2306 s->in_repo_path = strdup(in_repo_path);
2307 if (s->in_repo_path == NULL)
2308 return got_error_from_errno("strdup");
2311 /* The commit queue only contains commits being displayed. */
2312 TAILQ_INIT(&s->commits.head);
2313 s->commits.ncommits = 0;
2315 s->repo = repo;
2316 if (head_ref_name) {
2317 s->head_ref_name = strdup(head_ref_name);
2318 if (s->head_ref_name == NULL) {
2319 err = got_error_from_errno("strdup");
2320 goto done;
2323 s->start_id = got_object_id_dup(start_id);
2324 if (s->start_id == NULL) {
2325 err = got_error_from_errno("got_object_id_dup");
2326 goto done;
2328 s->log_branches = log_branches;
2330 STAILQ_INIT(&s->colors);
2331 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2332 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2333 get_color_value("TOG_COLOR_COMMIT"));
2334 if (err)
2335 goto done;
2336 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2337 get_color_value("TOG_COLOR_AUTHOR"));
2338 if (err) {
2339 free_colors(&s->colors);
2340 goto done;
2342 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2343 get_color_value("TOG_COLOR_DATE"));
2344 if (err) {
2345 free_colors(&s->colors);
2346 goto done;
2350 view->show = show_log_view;
2351 view->input = input_log_view;
2352 view->close = close_log_view;
2353 view->search_start = search_start_log_view;
2354 view->search_next = search_next_log_view;
2356 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2357 if (err)
2358 goto done;
2359 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2360 !s->log_branches);
2361 if (err)
2362 goto done;
2363 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2364 s->repo, NULL, NULL);
2365 if (err)
2366 goto done;
2368 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2369 if (errcode) {
2370 err = got_error_set_errno(errcode, "pthread_cond_init");
2371 goto done;
2373 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2374 if (errcode) {
2375 err = got_error_set_errno(errcode, "pthread_cond_init");
2376 goto done;
2379 s->thread_args.commits_needed = view->nlines;
2380 s->thread_args.graph = thread_graph;
2381 s->thread_args.commits = &s->commits;
2382 s->thread_args.in_repo_path = s->in_repo_path;
2383 s->thread_args.start_id = s->start_id;
2384 s->thread_args.repo = thread_repo;
2385 s->thread_args.log_complete = 0;
2386 s->thread_args.quit = &s->quit;
2387 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2388 s->thread_args.selected_entry = &s->selected_entry;
2389 s->thread_args.searching = &view->searching;
2390 s->thread_args.search_next_done = &view->search_next_done;
2391 s->thread_args.regex = &view->regex;
2392 done:
2393 if (err)
2394 close_log_view(view);
2395 return err;
2398 static const struct got_error *
2399 show_log_view(struct tog_view *view)
2401 const struct got_error *err;
2402 struct tog_log_view_state *s = &view->state.log;
2404 if (s->thread == 0) { //NULL) {
2405 int errcode = pthread_create(&s->thread, NULL, log_thread,
2406 &s->thread_args);
2407 if (errcode)
2408 return got_error_set_errno(errcode, "pthread_create");
2409 if (s->thread_args.commits_needed > 0) {
2410 err = trigger_log_thread(view, 1);
2411 if (err)
2412 return err;
2416 return draw_commits(view);
2419 static const struct got_error *
2420 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2422 const struct got_error *err = NULL;
2423 struct tog_log_view_state *s = &view->state.log;
2424 struct tog_view *diff_view = NULL, *tree_view = NULL;
2425 struct tog_view *ref_view = NULL;
2426 struct commit_queue_entry *entry;
2427 int begin_x = 0, n;
2429 if (s->thread_args.load_all) {
2430 if (ch == KEY_BACKSPACE)
2431 s->thread_args.load_all = 0;
2432 else if (s->thread_args.log_complete) {
2433 s->thread_args.load_all = 0;
2434 log_scroll_down(view, s->commits.ncommits);
2435 s->selected = MIN(view->nlines - 2,
2436 s->commits.ncommits - 1);
2437 select_commit(s);
2439 return NULL;
2442 switch (ch) {
2443 case 'q':
2444 s->quit = 1;
2445 break;
2446 case 'k':
2447 case KEY_UP:
2448 case '<':
2449 case ',':
2450 case CTRL('p'):
2451 if (s->first_displayed_entry == NULL)
2452 break;
2453 if (s->selected > 0)
2454 s->selected--;
2455 else
2456 log_scroll_up(s, 1);
2457 select_commit(s);
2458 break;
2459 case 'g':
2460 case KEY_HOME:
2461 s->selected = 0;
2462 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2463 select_commit(s);
2464 break;
2465 case KEY_PPAGE:
2466 case CTRL('b'):
2467 if (s->first_displayed_entry == NULL)
2468 break;
2469 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2470 s->selected = 0;
2471 else
2472 log_scroll_up(s, view->nlines - 1);
2473 select_commit(s);
2474 break;
2475 case 'j':
2476 case KEY_DOWN:
2477 case '>':
2478 case '.':
2479 case CTRL('n'):
2480 if (s->first_displayed_entry == NULL)
2481 break;
2482 if (s->selected < MIN(view->nlines - 2,
2483 s->commits.ncommits - 1))
2484 s->selected++;
2485 else {
2486 err = log_scroll_down(view, 1);
2487 if (err)
2488 break;
2490 select_commit(s);
2491 break;
2492 case 'G':
2493 case KEY_END: {
2494 /* We don't know yet how many commits, so we're forced to
2495 * traverse them all. */
2496 if (!s->thread_args.log_complete) {
2497 s->thread_args.load_all = 1;
2498 return trigger_log_thread(view, 0);
2501 s->selected = 0;
2502 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2503 for (n = 0; n < view->nlines - 1; n++) {
2504 if (entry == NULL)
2505 break;
2506 s->first_displayed_entry = entry;
2507 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2509 if (n > 0)
2510 s->selected = n - 1;
2511 select_commit(s);
2512 break;
2514 case KEY_NPAGE:
2515 case CTRL('f'): {
2516 struct commit_queue_entry *first;
2517 first = s->first_displayed_entry;
2518 if (first == NULL)
2519 break;
2520 err = log_scroll_down(view, view->nlines - 1);
2521 if (err)
2522 break;
2523 if (first == s->first_displayed_entry &&
2524 s->selected < MIN(view->nlines - 2,
2525 s->commits.ncommits - 1)) {
2526 /* can't scroll further down */
2527 s->selected = MIN(view->nlines - 2,
2528 s->commits.ncommits - 1);
2530 select_commit(s);
2531 break;
2533 case KEY_RESIZE:
2534 if (s->selected > view->nlines - 2)
2535 s->selected = view->nlines - 2;
2536 if (s->selected > s->commits.ncommits - 1)
2537 s->selected = s->commits.ncommits - 1;
2538 select_commit(s);
2539 if (s->commits.ncommits < view->nlines - 1 &&
2540 !s->thread_args.log_complete) {
2541 s->thread_args.commits_needed += (view->nlines - 1) -
2542 s->commits.ncommits;
2543 err = trigger_log_thread(view, 1);
2545 break;
2546 case KEY_ENTER:
2547 case ' ':
2548 case '\r':
2549 if (s->selected_entry == NULL)
2550 break;
2551 if (view_is_parent_view(view))
2552 begin_x = view_split_begin_x(view->begin_x);
2553 err = open_diff_view_for_commit(&diff_view, begin_x,
2554 s->selected_entry->commit, s->selected_entry->id,
2555 view, s->repo);
2556 if (err)
2557 break;
2558 view->focussed = 0;
2559 diff_view->focussed = 1;
2560 if (view_is_parent_view(view)) {
2561 err = view_close_child(view);
2562 if (err)
2563 return err;
2564 view_set_child(view, diff_view);
2565 view->focus_child = 1;
2566 } else
2567 *new_view = diff_view;
2568 break;
2569 case 't':
2570 if (s->selected_entry == NULL)
2571 break;
2572 if (view_is_parent_view(view))
2573 begin_x = view_split_begin_x(view->begin_x);
2574 err = browse_commit_tree(&tree_view, begin_x,
2575 s->selected_entry, s->in_repo_path, s->head_ref_name,
2576 s->repo);
2577 if (err)
2578 break;
2579 view->focussed = 0;
2580 tree_view->focussed = 1;
2581 if (view_is_parent_view(view)) {
2582 err = view_close_child(view);
2583 if (err)
2584 return err;
2585 view_set_child(view, tree_view);
2586 view->focus_child = 1;
2587 } else
2588 *new_view = tree_view;
2589 break;
2590 case KEY_BACKSPACE:
2591 case CTRL('l'):
2592 case 'B':
2593 if (ch == KEY_BACKSPACE &&
2594 got_path_is_root_dir(s->in_repo_path))
2595 break;
2596 err = stop_log_thread(s);
2597 if (err)
2598 return err;
2599 if (ch == KEY_BACKSPACE) {
2600 char *parent_path;
2601 err = got_path_dirname(&parent_path, s->in_repo_path);
2602 if (err)
2603 return err;
2604 free(s->in_repo_path);
2605 s->in_repo_path = parent_path;
2606 s->thread_args.in_repo_path = s->in_repo_path;
2607 } else if (ch == CTRL('l')) {
2608 struct got_object_id *start_id;
2609 err = got_repo_match_object_id(&start_id, NULL,
2610 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2611 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2612 if (err)
2613 return err;
2614 free(s->start_id);
2615 s->start_id = start_id;
2616 s->thread_args.start_id = s->start_id;
2617 } else /* 'B' */
2618 s->log_branches = !s->log_branches;
2620 err = got_repo_open(&s->thread_args.repo,
2621 got_repo_get_path(s->repo), NULL);
2622 if (err)
2623 return err;
2624 tog_free_refs();
2625 err = tog_load_refs(s->repo, 0);
2626 if (err)
2627 return err;
2628 err = got_commit_graph_open(&s->thread_args.graph,
2629 s->in_repo_path, !s->log_branches);
2630 if (err)
2631 return err;
2632 err = got_commit_graph_iter_start(s->thread_args.graph,
2633 s->start_id, s->repo, NULL, NULL);
2634 if (err)
2635 return err;
2636 free_commits(&s->commits);
2637 s->first_displayed_entry = NULL;
2638 s->last_displayed_entry = NULL;
2639 s->selected_entry = NULL;
2640 s->selected = 0;
2641 s->thread_args.log_complete = 0;
2642 s->quit = 0;
2643 s->thread_args.commits_needed = view->nlines;
2644 break;
2645 case 'r':
2646 if (view_is_parent_view(view))
2647 begin_x = view_split_begin_x(view->begin_x);
2648 ref_view = view_open(view->nlines, view->ncols,
2649 view->begin_y, begin_x, TOG_VIEW_REF);
2650 if (ref_view == NULL)
2651 return got_error_from_errno("view_open");
2652 err = open_ref_view(ref_view, s->repo);
2653 if (err) {
2654 view_close(ref_view);
2655 return err;
2657 view->focussed = 0;
2658 ref_view->focussed = 1;
2659 if (view_is_parent_view(view)) {
2660 err = view_close_child(view);
2661 if (err)
2662 return err;
2663 view_set_child(view, ref_view);
2664 view->focus_child = 1;
2665 } else
2666 *new_view = ref_view;
2667 break;
2668 default:
2669 break;
2672 return err;
2675 static const struct got_error *
2676 apply_unveil(const char *repo_path, const char *worktree_path)
2678 const struct got_error *error;
2680 #ifdef PROFILE
2681 if (unveil("gmon.out", "rwc") != 0)
2682 return got_error_from_errno2("unveil", "gmon.out");
2683 #endif
2684 if (repo_path && unveil(repo_path, "r") != 0)
2685 return got_error_from_errno2("unveil", repo_path);
2687 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2688 return got_error_from_errno2("unveil", worktree_path);
2690 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2691 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2693 error = got_privsep_unveil_exec_helpers();
2694 if (error != NULL)
2695 return error;
2697 if (unveil(NULL, NULL) != 0)
2698 return got_error_from_errno("unveil");
2700 return NULL;
2703 static void
2704 init_curses(void)
2706 initscr();
2707 cbreak();
2708 halfdelay(1); /* Do fast refresh while initial view is loading. */
2709 noecho();
2710 nonl();
2711 intrflush(stdscr, FALSE);
2712 keypad(stdscr, TRUE);
2713 curs_set(0);
2714 if (getenv("TOG_COLORS") != NULL) {
2715 start_color();
2716 use_default_colors();
2718 signal(SIGWINCH, tog_sigwinch);
2719 signal(SIGPIPE, tog_sigpipe);
2720 signal(SIGCONT, tog_sigcont);
2723 static const struct got_error *
2724 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2725 struct got_repository *repo, struct got_worktree *worktree)
2727 const struct got_error *err = NULL;
2729 if (argc == 0) {
2730 *in_repo_path = strdup("/");
2731 if (*in_repo_path == NULL)
2732 return got_error_from_errno("strdup");
2733 return NULL;
2736 if (worktree) {
2737 const char *prefix = got_worktree_get_path_prefix(worktree);
2738 char *p;
2740 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2741 if (err)
2742 return err;
2743 if (asprintf(in_repo_path, "%s%s%s", prefix,
2744 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2745 p) == -1) {
2746 err = got_error_from_errno("asprintf");
2747 *in_repo_path = NULL;
2749 free(p);
2750 } else
2751 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2753 return err;
2756 static const struct got_error *
2757 cmd_log(int argc, char *argv[])
2759 const struct got_error *error;
2760 struct got_repository *repo = NULL;
2761 struct got_worktree *worktree = NULL;
2762 struct got_object_id *start_id = NULL;
2763 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2764 char *start_commit = NULL, *label = NULL;
2765 struct got_reference *ref = NULL;
2766 const char *head_ref_name = NULL;
2767 int ch, log_branches = 0;
2768 struct tog_view *view;
2770 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2771 switch (ch) {
2772 case 'b':
2773 log_branches = 1;
2774 break;
2775 case 'c':
2776 start_commit = optarg;
2777 break;
2778 case 'r':
2779 repo_path = realpath(optarg, NULL);
2780 if (repo_path == NULL)
2781 return got_error_from_errno2("realpath",
2782 optarg);
2783 break;
2784 default:
2785 usage_log();
2786 /* NOTREACHED */
2790 argc -= optind;
2791 argv += optind;
2793 if (argc > 1)
2794 usage_log();
2796 if (repo_path == NULL) {
2797 cwd = getcwd(NULL, 0);
2798 if (cwd == NULL)
2799 return got_error_from_errno("getcwd");
2800 error = got_worktree_open(&worktree, cwd);
2801 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2802 goto done;
2803 if (worktree)
2804 repo_path =
2805 strdup(got_worktree_get_repo_path(worktree));
2806 else
2807 repo_path = strdup(cwd);
2808 if (repo_path == NULL) {
2809 error = got_error_from_errno("strdup");
2810 goto done;
2814 error = got_repo_open(&repo, repo_path, NULL);
2815 if (error != NULL)
2816 goto done;
2818 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2819 repo, worktree);
2820 if (error)
2821 goto done;
2823 init_curses();
2825 error = apply_unveil(got_repo_get_path(repo),
2826 worktree ? got_worktree_get_root_path(worktree) : NULL);
2827 if (error)
2828 goto done;
2830 /* already loaded by tog_log_with_path()? */
2831 if (TAILQ_EMPTY(&tog_refs)) {
2832 error = tog_load_refs(repo, 0);
2833 if (error)
2834 goto done;
2837 if (start_commit == NULL) {
2838 error = got_repo_match_object_id(&start_id, &label,
2839 worktree ? got_worktree_get_head_ref_name(worktree) :
2840 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2841 if (error)
2842 goto done;
2843 head_ref_name = label;
2844 } else {
2845 error = got_ref_open(&ref, repo, start_commit, 0);
2846 if (error == NULL)
2847 head_ref_name = got_ref_get_name(ref);
2848 else if (error->code != GOT_ERR_NOT_REF)
2849 goto done;
2850 error = got_repo_match_object_id(&start_id, NULL,
2851 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2852 if (error)
2853 goto done;
2856 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2857 if (view == NULL) {
2858 error = got_error_from_errno("view_open");
2859 goto done;
2861 error = open_log_view(view, start_id, repo, head_ref_name,
2862 in_repo_path, log_branches);
2863 if (error)
2864 goto done;
2865 if (worktree) {
2866 /* Release work tree lock. */
2867 got_worktree_close(worktree);
2868 worktree = NULL;
2870 error = view_loop(view);
2871 done:
2872 free(in_repo_path);
2873 free(repo_path);
2874 free(cwd);
2875 free(start_id);
2876 free(label);
2877 if (ref)
2878 got_ref_close(ref);
2879 if (repo) {
2880 const struct got_error *close_err = got_repo_close(repo);
2881 if (error == NULL)
2882 error = close_err;
2884 if (worktree)
2885 got_worktree_close(worktree);
2886 tog_free_refs();
2887 return error;
2890 __dead static void
2891 usage_diff(void)
2893 endwin();
2894 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2895 "[-w] object1 object2\n", getprogname());
2896 exit(1);
2899 static int
2900 match_line(const char *line, regex_t *regex, size_t nmatch,
2901 regmatch_t *regmatch)
2903 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2906 struct tog_color *
2907 match_color(struct tog_colors *colors, const char *line)
2909 struct tog_color *tc = NULL;
2911 STAILQ_FOREACH(tc, colors, entry) {
2912 if (match_line(line, &tc->regex, 0, NULL))
2913 return tc;
2916 return NULL;
2919 static const struct got_error *
2920 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2921 WINDOW *window, regmatch_t *regmatch)
2923 const struct got_error *err = NULL;
2924 wchar_t *wline;
2925 int width;
2926 char *s;
2928 *wtotal = 0;
2930 s = strndup(line, regmatch->rm_so);
2931 if (s == NULL)
2932 return got_error_from_errno("strndup");
2934 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2935 if (err) {
2936 free(s);
2937 return err;
2939 waddwstr(window, wline);
2940 free(wline);
2941 free(s);
2942 wlimit -= width;
2943 *wtotal += width;
2945 if (wlimit > 0) {
2946 s = strndup(line + regmatch->rm_so,
2947 regmatch->rm_eo - regmatch->rm_so);
2948 if (s == NULL) {
2949 err = got_error_from_errno("strndup");
2950 free(s);
2951 return err;
2953 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2954 if (err) {
2955 free(s);
2956 return err;
2958 wattr_on(window, A_STANDOUT, NULL);
2959 waddwstr(window, wline);
2960 wattr_off(window, A_STANDOUT, NULL);
2961 free(wline);
2962 free(s);
2963 wlimit -= width;
2964 *wtotal += width;
2967 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2968 err = format_line(&wline, &width,
2969 line + regmatch->rm_eo, wlimit, col_tab_align);
2970 if (err)
2971 return err;
2972 waddwstr(window, wline);
2973 free(wline);
2974 *wtotal += width;
2977 return NULL;
2980 static const struct got_error *
2981 draw_file(struct tog_view *view, const char *header)
2983 struct tog_diff_view_state *s = &view->state.diff;
2984 regmatch_t *regmatch = &view->regmatch;
2985 const struct got_error *err;
2986 int nprinted = 0;
2987 char *line;
2988 size_t linesize = 0;
2989 ssize_t linelen;
2990 struct tog_color *tc;
2991 wchar_t *wline;
2992 int width;
2993 int max_lines = view->nlines;
2994 int nlines = s->nlines;
2995 off_t line_offset;
2997 line_offset = s->line_offsets[s->first_displayed_line - 1];
2998 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2999 return got_error_from_errno("fseek");
3001 werase(view->window);
3003 if (header) {
3004 if (asprintf(&line, "[%d/%d] %s",
3005 s->first_displayed_line - 1 + s->selected_line, nlines,
3006 header) == -1)
3007 return got_error_from_errno("asprintf");
3008 err = format_line(&wline, &width, line, view->ncols, 0);
3009 free(line);
3010 if (err)
3011 return err;
3013 if (view_needs_focus_indication(view))
3014 wstandout(view->window);
3015 waddwstr(view->window, wline);
3016 free(wline);
3017 wline = NULL;
3018 if (view_needs_focus_indication(view))
3019 wstandend(view->window);
3020 if (width <= view->ncols - 1)
3021 waddch(view->window, '\n');
3023 if (max_lines <= 1)
3024 return NULL;
3025 max_lines--;
3028 s->eof = 0;
3029 line = NULL;
3030 while (max_lines > 0 && nprinted < max_lines) {
3031 linelen = getline(&line, &linesize, s->f);
3032 if (linelen == -1) {
3033 if (feof(s->f)) {
3034 s->eof = 1;
3035 break;
3037 free(line);
3038 return got_ferror(s->f, GOT_ERR_IO);
3041 tc = match_color(&s->colors, line);
3042 if (tc)
3043 wattr_on(view->window,
3044 COLOR_PAIR(tc->colorpair), NULL);
3045 if (s->first_displayed_line + nprinted == s->matched_line &&
3046 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3047 err = add_matched_line(&width, line, view->ncols, 0,
3048 view->window, regmatch);
3049 if (err) {
3050 free(line);
3051 return err;
3053 } else {
3054 err = format_line(&wline, &width, line, view->ncols, 0);
3055 if (err) {
3056 free(line);
3057 return err;
3059 waddwstr(view->window, wline);
3060 free(wline);
3061 wline = NULL;
3063 if (tc)
3064 wattr_off(view->window,
3065 COLOR_PAIR(tc->colorpair), NULL);
3066 if (width <= view->ncols - 1)
3067 waddch(view->window, '\n');
3068 nprinted++;
3070 free(line);
3071 if (nprinted >= 1)
3072 s->last_displayed_line = s->first_displayed_line +
3073 (nprinted - 1);
3074 else
3075 s->last_displayed_line = s->first_displayed_line;
3077 view_vborder(view);
3079 if (s->eof) {
3080 while (nprinted < view->nlines) {
3081 waddch(view->window, '\n');
3082 nprinted++;
3085 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3086 if (err) {
3087 return err;
3090 wstandout(view->window);
3091 waddwstr(view->window, wline);
3092 free(wline);
3093 wline = NULL;
3094 wstandend(view->window);
3097 return NULL;
3100 static char *
3101 get_datestr(time_t *time, char *datebuf)
3103 struct tm mytm, *tm;
3104 char *p, *s;
3106 tm = gmtime_r(time, &mytm);
3107 if (tm == NULL)
3108 return NULL;
3109 s = asctime_r(tm, datebuf);
3110 if (s == NULL)
3111 return NULL;
3112 p = strchr(s, '\n');
3113 if (p)
3114 *p = '\0';
3115 return s;
3118 static const struct got_error *
3119 get_changed_paths(struct got_pathlist_head *paths,
3120 struct got_commit_object *commit, struct got_repository *repo)
3122 const struct got_error *err = NULL;
3123 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3124 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3125 struct got_object_qid *qid;
3127 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3128 if (qid != NULL) {
3129 struct got_commit_object *pcommit;
3130 err = got_object_open_as_commit(&pcommit, repo,
3131 &qid->id);
3132 if (err)
3133 return err;
3135 tree_id1 = got_object_id_dup(
3136 got_object_commit_get_tree_id(pcommit));
3137 if (tree_id1 == NULL) {
3138 got_object_commit_close(pcommit);
3139 return got_error_from_errno("got_object_id_dup");
3141 got_object_commit_close(pcommit);
3145 if (tree_id1) {
3146 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3147 if (err)
3148 goto done;
3151 tree_id2 = got_object_commit_get_tree_id(commit);
3152 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3153 if (err)
3154 goto done;
3156 err = got_diff_tree(tree1, tree2, "", "", repo,
3157 got_diff_tree_collect_changed_paths, paths, 0);
3158 done:
3159 if (tree1)
3160 got_object_tree_close(tree1);
3161 if (tree2)
3162 got_object_tree_close(tree2);
3163 free(tree_id1);
3164 return err;
3167 static const struct got_error *
3168 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3170 off_t *p;
3172 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3173 if (p == NULL)
3174 return got_error_from_errno("reallocarray");
3175 *line_offsets = p;
3176 (*line_offsets)[*nlines] = off;
3177 (*nlines)++;
3178 return NULL;
3181 static const struct got_error *
3182 write_commit_info(off_t **line_offsets, size_t *nlines,
3183 struct got_object_id *commit_id, struct got_reflist_head *refs,
3184 struct got_repository *repo, FILE *outfile)
3186 const struct got_error *err = NULL;
3187 char datebuf[26], *datestr;
3188 struct got_commit_object *commit;
3189 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3190 time_t committer_time;
3191 const char *author, *committer;
3192 char *refs_str = NULL;
3193 struct got_pathlist_head changed_paths;
3194 struct got_pathlist_entry *pe;
3195 off_t outoff = 0;
3196 int n;
3198 TAILQ_INIT(&changed_paths);
3200 if (refs) {
3201 err = build_refs_str(&refs_str, refs, commit_id, repo);
3202 if (err)
3203 return err;
3206 err = got_object_open_as_commit(&commit, repo, commit_id);
3207 if (err)
3208 return err;
3210 err = got_object_id_str(&id_str, commit_id);
3211 if (err) {
3212 err = got_error_from_errno("got_object_id_str");
3213 goto done;
3216 err = add_line_offset(line_offsets, nlines, 0);
3217 if (err)
3218 goto done;
3220 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3221 refs_str ? refs_str : "", refs_str ? ")" : "");
3222 if (n < 0) {
3223 err = got_error_from_errno("fprintf");
3224 goto done;
3226 outoff += n;
3227 err = add_line_offset(line_offsets, nlines, outoff);
3228 if (err)
3229 goto done;
3231 n = fprintf(outfile, "from: %s\n",
3232 got_object_commit_get_author(commit));
3233 if (n < 0) {
3234 err = got_error_from_errno("fprintf");
3235 goto done;
3237 outoff += n;
3238 err = add_line_offset(line_offsets, nlines, outoff);
3239 if (err)
3240 goto done;
3242 committer_time = got_object_commit_get_committer_time(commit);
3243 datestr = get_datestr(&committer_time, datebuf);
3244 if (datestr) {
3245 n = fprintf(outfile, "date: %s UTC\n", datestr);
3246 if (n < 0) {
3247 err = got_error_from_errno("fprintf");
3248 goto done;
3250 outoff += n;
3251 err = add_line_offset(line_offsets, nlines, outoff);
3252 if (err)
3253 goto done;
3255 author = got_object_commit_get_author(commit);
3256 committer = got_object_commit_get_committer(commit);
3257 if (strcmp(author, committer) != 0) {
3258 n = fprintf(outfile, "via: %s\n", committer);
3259 if (n < 0) {
3260 err = got_error_from_errno("fprintf");
3261 goto done;
3263 outoff += n;
3264 err = add_line_offset(line_offsets, nlines, outoff);
3265 if (err)
3266 goto done;
3268 if (got_object_commit_get_nparents(commit) > 1) {
3269 const struct got_object_id_queue *parent_ids;
3270 struct got_object_qid *qid;
3271 int pn = 1;
3272 parent_ids = got_object_commit_get_parent_ids(commit);
3273 STAILQ_FOREACH(qid, parent_ids, entry) {
3274 err = got_object_id_str(&id_str, &qid->id);
3275 if (err)
3276 goto done;
3277 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3278 if (n < 0) {
3279 err = got_error_from_errno("fprintf");
3280 goto done;
3282 outoff += n;
3283 err = add_line_offset(line_offsets, nlines, outoff);
3284 if (err)
3285 goto done;
3286 free(id_str);
3287 id_str = NULL;
3291 err = got_object_commit_get_logmsg(&logmsg, commit);
3292 if (err)
3293 goto done;
3294 s = logmsg;
3295 while ((line = strsep(&s, "\n")) != NULL) {
3296 n = fprintf(outfile, "%s\n", line);
3297 if (n < 0) {
3298 err = got_error_from_errno("fprintf");
3299 goto done;
3301 outoff += n;
3302 err = add_line_offset(line_offsets, nlines, outoff);
3303 if (err)
3304 goto done;
3307 err = get_changed_paths(&changed_paths, commit, repo);
3308 if (err)
3309 goto done;
3310 TAILQ_FOREACH(pe, &changed_paths, entry) {
3311 struct got_diff_changed_path *cp = pe->data;
3312 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3313 if (n < 0) {
3314 err = got_error_from_errno("fprintf");
3315 goto done;
3317 outoff += n;
3318 err = add_line_offset(line_offsets, nlines, outoff);
3319 if (err)
3320 goto done;
3321 free((char *)pe->path);
3322 free(pe->data);
3325 fputc('\n', outfile);
3326 outoff++;
3327 err = add_line_offset(line_offsets, nlines, outoff);
3328 done:
3329 got_pathlist_free(&changed_paths);
3330 free(id_str);
3331 free(logmsg);
3332 free(refs_str);
3333 got_object_commit_close(commit);
3334 if (err) {
3335 free(*line_offsets);
3336 *line_offsets = NULL;
3337 *nlines = 0;
3339 return err;
3342 static const struct got_error *
3343 create_diff(struct tog_diff_view_state *s)
3345 const struct got_error *err = NULL;
3346 FILE *f = NULL;
3347 int obj_type;
3349 free(s->line_offsets);
3350 s->line_offsets = malloc(sizeof(off_t));
3351 if (s->line_offsets == NULL)
3352 return got_error_from_errno("malloc");
3353 s->nlines = 0;
3355 f = got_opentemp();
3356 if (f == NULL) {
3357 err = got_error_from_errno("got_opentemp");
3358 goto done;
3360 if (s->f && fclose(s->f) == EOF) {
3361 err = got_error_from_errno("fclose");
3362 goto done;
3364 s->f = f;
3366 if (s->id1)
3367 err = got_object_get_type(&obj_type, s->repo, s->id1);
3368 else
3369 err = got_object_get_type(&obj_type, s->repo, s->id2);
3370 if (err)
3371 goto done;
3373 switch (obj_type) {
3374 case GOT_OBJ_TYPE_BLOB:
3375 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3376 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3377 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3378 break;
3379 case GOT_OBJ_TYPE_TREE:
3380 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3381 s->id1, s->id2, NULL, "", "", s->diff_context,
3382 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3383 break;
3384 case GOT_OBJ_TYPE_COMMIT: {
3385 const struct got_object_id_queue *parent_ids;
3386 struct got_object_qid *pid;
3387 struct got_commit_object *commit2;
3388 struct got_reflist_head *refs;
3390 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3391 if (err)
3392 goto done;
3393 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3394 /* Show commit info if we're diffing to a parent/root commit. */
3395 if (s->id1 == NULL) {
3396 err = write_commit_info(&s->line_offsets, &s->nlines,
3397 s->id2, refs, s->repo, s->f);
3398 if (err)
3399 goto done;
3400 } else {
3401 parent_ids = got_object_commit_get_parent_ids(commit2);
3402 STAILQ_FOREACH(pid, parent_ids, entry) {
3403 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3404 err = write_commit_info(
3405 &s->line_offsets, &s->nlines,
3406 s->id2, refs, s->repo, s->f);
3407 if (err)
3408 goto done;
3409 break;
3413 got_object_commit_close(commit2);
3415 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3416 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3417 s->force_text_diff, s->repo, s->f);
3418 break;
3420 default:
3421 err = got_error(GOT_ERR_OBJ_TYPE);
3422 break;
3424 if (err)
3425 goto done;
3426 done:
3427 if (s->f && fflush(s->f) != 0 && err == NULL)
3428 err = got_error_from_errno("fflush");
3429 return err;
3432 static void
3433 diff_view_indicate_progress(struct tog_view *view)
3435 mvwaddstr(view->window, 0, 0, "diffing...");
3436 update_panels();
3437 doupdate();
3440 static const struct got_error *
3441 search_start_diff_view(struct tog_view *view)
3443 struct tog_diff_view_state *s = &view->state.diff;
3445 s->matched_line = 0;
3446 return NULL;
3449 static const struct got_error *
3450 search_next_diff_view(struct tog_view *view)
3452 struct tog_diff_view_state *s = &view->state.diff;
3453 int lineno;
3454 char *line = NULL;
3455 size_t linesize = 0;
3456 ssize_t linelen;
3458 if (!view->searching) {
3459 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3460 return NULL;
3463 if (s->matched_line) {
3464 if (view->searching == TOG_SEARCH_FORWARD)
3465 lineno = s->matched_line + 1;
3466 else
3467 lineno = s->matched_line - 1;
3468 } else
3469 lineno = s->first_displayed_line;
3471 while (1) {
3472 off_t offset;
3474 if (lineno <= 0 || lineno > s->nlines) {
3475 if (s->matched_line == 0) {
3476 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3477 break;
3480 if (view->searching == TOG_SEARCH_FORWARD)
3481 lineno = 1;
3482 else
3483 lineno = s->nlines;
3486 offset = s->line_offsets[lineno - 1];
3487 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3488 free(line);
3489 return got_error_from_errno("fseeko");
3491 linelen = getline(&line, &linesize, s->f);
3492 if (linelen != -1 &&
3493 match_line(line, &view->regex, 1, &view->regmatch)) {
3494 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3495 s->matched_line = lineno;
3496 break;
3498 if (view->searching == TOG_SEARCH_FORWARD)
3499 lineno++;
3500 else
3501 lineno--;
3503 free(line);
3505 if (s->matched_line) {
3506 s->first_displayed_line = s->matched_line;
3507 s->selected_line = 1;
3510 return NULL;
3513 static const struct got_error *
3514 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3515 struct got_object_id *id2, const char *label1, const char *label2,
3516 int diff_context, int ignore_whitespace, int force_text_diff,
3517 struct tog_view *log_view, struct got_repository *repo)
3519 const struct got_error *err;
3520 struct tog_diff_view_state *s = &view->state.diff;
3522 if (id1 != NULL && id2 != NULL) {
3523 int type1, type2;
3524 err = got_object_get_type(&type1, repo, id1);
3525 if (err)
3526 return err;
3527 err = got_object_get_type(&type2, repo, id2);
3528 if (err)
3529 return err;
3531 if (type1 != type2)
3532 return got_error(GOT_ERR_OBJ_TYPE);
3534 s->first_displayed_line = 1;
3535 s->last_displayed_line = view->nlines;
3536 s->selected_line = 1;
3537 s->repo = repo;
3538 s->id1 = id1;
3539 s->id2 = id2;
3540 s->label1 = label1;
3541 s->label2 = label2;
3543 if (id1) {
3544 s->id1 = got_object_id_dup(id1);
3545 if (s->id1 == NULL)
3546 return got_error_from_errno("got_object_id_dup");
3547 } else
3548 s->id1 = NULL;
3550 s->id2 = got_object_id_dup(id2);
3551 if (s->id2 == NULL) {
3552 free(s->id1);
3553 s->id1 = NULL;
3554 return got_error_from_errno("got_object_id_dup");
3556 s->f = NULL;
3557 s->first_displayed_line = 1;
3558 s->last_displayed_line = view->nlines;
3559 s->diff_context = diff_context;
3560 s->ignore_whitespace = ignore_whitespace;
3561 s->force_text_diff = force_text_diff;
3562 s->log_view = log_view;
3563 s->repo = repo;
3565 STAILQ_INIT(&s->colors);
3566 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3567 err = add_color(&s->colors,
3568 "^-", TOG_COLOR_DIFF_MINUS,
3569 get_color_value("TOG_COLOR_DIFF_MINUS"));
3570 if (err)
3571 return err;
3572 err = add_color(&s->colors, "^\\+",
3573 TOG_COLOR_DIFF_PLUS,
3574 get_color_value("TOG_COLOR_DIFF_PLUS"));
3575 if (err) {
3576 free_colors(&s->colors);
3577 return err;
3579 err = add_color(&s->colors,
3580 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3581 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3582 if (err) {
3583 free_colors(&s->colors);
3584 return err;
3587 err = add_color(&s->colors,
3588 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3589 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3590 get_color_value("TOG_COLOR_DIFF_META"));
3591 if (err) {
3592 free_colors(&s->colors);
3593 return err;
3596 err = add_color(&s->colors,
3597 "^(from|via): ", TOG_COLOR_AUTHOR,
3598 get_color_value("TOG_COLOR_AUTHOR"));
3599 if (err) {
3600 free_colors(&s->colors);
3601 return err;
3604 err = add_color(&s->colors,
3605 "^date: ", TOG_COLOR_DATE,
3606 get_color_value("TOG_COLOR_DATE"));
3607 if (err) {
3608 free_colors(&s->colors);
3609 return err;
3613 if (log_view && view_is_splitscreen(view))
3614 show_log_view(log_view); /* draw vborder */
3615 diff_view_indicate_progress(view);
3617 s->line_offsets = NULL;
3618 s->nlines = 0;
3619 err = create_diff(s);
3620 if (err) {
3621 free(s->id1);
3622 s->id1 = NULL;
3623 free(s->id2);
3624 s->id2 = NULL;
3625 free_colors(&s->colors);
3626 return err;
3629 view->show = show_diff_view;
3630 view->input = input_diff_view;
3631 view->close = close_diff_view;
3632 view->search_start = search_start_diff_view;
3633 view->search_next = search_next_diff_view;
3635 return NULL;
3638 static const struct got_error *
3639 close_diff_view(struct tog_view *view)
3641 const struct got_error *err = NULL;
3642 struct tog_diff_view_state *s = &view->state.diff;
3644 free(s->id1);
3645 s->id1 = NULL;
3646 free(s->id2);
3647 s->id2 = NULL;
3648 if (s->f && fclose(s->f) == EOF)
3649 err = got_error_from_errno("fclose");
3650 free_colors(&s->colors);
3651 free(s->line_offsets);
3652 s->line_offsets = NULL;
3653 s->nlines = 0;
3654 return err;
3657 static const struct got_error *
3658 show_diff_view(struct tog_view *view)
3660 const struct got_error *err;
3661 struct tog_diff_view_state *s = &view->state.diff;
3662 char *id_str1 = NULL, *id_str2, *header;
3663 const char *label1, *label2;
3665 if (s->id1) {
3666 err = got_object_id_str(&id_str1, s->id1);
3667 if (err)
3668 return err;
3669 label1 = s->label1 ? : id_str1;
3670 } else
3671 label1 = "/dev/null";
3673 err = got_object_id_str(&id_str2, s->id2);
3674 if (err)
3675 return err;
3676 label2 = s->label2 ? : id_str2;
3678 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3679 err = got_error_from_errno("asprintf");
3680 free(id_str1);
3681 free(id_str2);
3682 return err;
3684 free(id_str1);
3685 free(id_str2);
3687 err = draw_file(view, header);
3688 free(header);
3689 return err;
3692 static const struct got_error *
3693 set_selected_commit(struct tog_diff_view_state *s,
3694 struct commit_queue_entry *entry)
3696 const struct got_error *err;
3697 const struct got_object_id_queue *parent_ids;
3698 struct got_commit_object *selected_commit;
3699 struct got_object_qid *pid;
3701 free(s->id2);
3702 s->id2 = got_object_id_dup(entry->id);
3703 if (s->id2 == NULL)
3704 return got_error_from_errno("got_object_id_dup");
3706 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3707 if (err)
3708 return err;
3709 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3710 free(s->id1);
3711 pid = STAILQ_FIRST(parent_ids);
3712 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3713 got_object_commit_close(selected_commit);
3714 return NULL;
3717 static const struct got_error *
3718 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3720 const struct got_error *err = NULL;
3721 struct tog_diff_view_state *s = &view->state.diff;
3722 struct tog_log_view_state *ls;
3723 struct commit_queue_entry *old_selected_entry;
3724 char *line = NULL;
3725 size_t linesize = 0;
3726 ssize_t linelen;
3727 int i;
3729 switch (ch) {
3730 case 'a':
3731 case 'w':
3732 if (ch == 'a')
3733 s->force_text_diff = !s->force_text_diff;
3734 if (ch == 'w')
3735 s->ignore_whitespace = !s->ignore_whitespace;
3736 wclear(view->window);
3737 s->first_displayed_line = 1;
3738 s->last_displayed_line = view->nlines;
3739 s->matched_line = 0;
3740 diff_view_indicate_progress(view);
3741 err = create_diff(s);
3742 break;
3743 case 'g':
3744 case KEY_HOME:
3745 s->first_displayed_line = 1;
3746 break;
3747 case 'G':
3748 case KEY_END:
3749 if (s->eof)
3750 break;
3752 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3753 s->eof = 1;
3754 break;
3755 case 'k':
3756 case KEY_UP:
3757 case CTRL('p'):
3758 if (s->first_displayed_line > 1)
3759 s->first_displayed_line--;
3760 break;
3761 case KEY_PPAGE:
3762 case CTRL('b'):
3763 if (s->first_displayed_line == 1)
3764 break;
3765 i = 0;
3766 while (i++ < view->nlines - 1 &&
3767 s->first_displayed_line > 1)
3768 s->first_displayed_line--;
3769 break;
3770 case 'j':
3771 case KEY_DOWN:
3772 case CTRL('n'):
3773 if (!s->eof)
3774 s->first_displayed_line++;
3775 break;
3776 case KEY_NPAGE:
3777 case CTRL('f'):
3778 case ' ':
3779 if (s->eof)
3780 break;
3781 i = 0;
3782 while (!s->eof && i++ < view->nlines - 1) {
3783 linelen = getline(&line, &linesize, s->f);
3784 s->first_displayed_line++;
3785 if (linelen == -1) {
3786 if (feof(s->f)) {
3787 s->eof = 1;
3788 } else
3789 err = got_ferror(s->f, GOT_ERR_IO);
3790 break;
3793 free(line);
3794 break;
3795 case '[':
3796 if (s->diff_context > 0) {
3797 s->diff_context--;
3798 s->matched_line = 0;
3799 diff_view_indicate_progress(view);
3800 err = create_diff(s);
3801 if (s->first_displayed_line + view->nlines - 1 >
3802 s->nlines) {
3803 s->first_displayed_line = 1;
3804 s->last_displayed_line = view->nlines;
3807 break;
3808 case ']':
3809 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3810 s->diff_context++;
3811 s->matched_line = 0;
3812 diff_view_indicate_progress(view);
3813 err = create_diff(s);
3815 break;
3816 case '<':
3817 case ',':
3818 if (s->log_view == NULL)
3819 break;
3820 ls = &s->log_view->state.log;
3821 old_selected_entry = ls->selected_entry;
3823 err = input_log_view(NULL, s->log_view, KEY_UP);
3824 if (err)
3825 break;
3827 if (old_selected_entry == ls->selected_entry)
3828 break;
3830 err = set_selected_commit(s, ls->selected_entry);
3831 if (err)
3832 break;
3834 s->first_displayed_line = 1;
3835 s->last_displayed_line = view->nlines;
3836 s->matched_line = 0;
3838 diff_view_indicate_progress(view);
3839 err = create_diff(s);
3840 break;
3841 case '>':
3842 case '.':
3843 if (s->log_view == NULL)
3844 break;
3845 ls = &s->log_view->state.log;
3846 old_selected_entry = ls->selected_entry;
3848 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3849 if (err)
3850 break;
3852 if (old_selected_entry == ls->selected_entry)
3853 break;
3855 err = set_selected_commit(s, ls->selected_entry);
3856 if (err)
3857 break;
3859 s->first_displayed_line = 1;
3860 s->last_displayed_line = view->nlines;
3861 s->matched_line = 0;
3863 diff_view_indicate_progress(view);
3864 err = create_diff(s);
3865 break;
3866 default:
3867 break;
3870 return err;
3873 static const struct got_error *
3874 cmd_diff(int argc, char *argv[])
3876 const struct got_error *error = NULL;
3877 struct got_repository *repo = NULL;
3878 struct got_worktree *worktree = NULL;
3879 struct got_object_id *id1 = NULL, *id2 = NULL;
3880 char *repo_path = NULL, *cwd = NULL;
3881 char *id_str1 = NULL, *id_str2 = NULL;
3882 char *label1 = NULL, *label2 = NULL;
3883 int diff_context = 3, ignore_whitespace = 0;
3884 int ch, force_text_diff = 0;
3885 const char *errstr;
3886 struct tog_view *view;
3888 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3889 switch (ch) {
3890 case 'a':
3891 force_text_diff = 1;
3892 break;
3893 case 'C':
3894 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3895 &errstr);
3896 if (errstr != NULL)
3897 errx(1, "number of context lines is %s: %s",
3898 errstr, errstr);
3899 break;
3900 case 'r':
3901 repo_path = realpath(optarg, NULL);
3902 if (repo_path == NULL)
3903 return got_error_from_errno2("realpath",
3904 optarg);
3905 got_path_strip_trailing_slashes(repo_path);
3906 break;
3907 case 'w':
3908 ignore_whitespace = 1;
3909 break;
3910 default:
3911 usage_diff();
3912 /* NOTREACHED */
3916 argc -= optind;
3917 argv += optind;
3919 if (argc == 0) {
3920 usage_diff(); /* TODO show local worktree changes */
3921 } else if (argc == 2) {
3922 id_str1 = argv[0];
3923 id_str2 = argv[1];
3924 } else
3925 usage_diff();
3927 if (repo_path == NULL) {
3928 cwd = getcwd(NULL, 0);
3929 if (cwd == NULL)
3930 return got_error_from_errno("getcwd");
3931 error = got_worktree_open(&worktree, cwd);
3932 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3933 goto done;
3934 if (worktree)
3935 repo_path =
3936 strdup(got_worktree_get_repo_path(worktree));
3937 else
3938 repo_path = strdup(cwd);
3939 if (repo_path == NULL) {
3940 error = got_error_from_errno("strdup");
3941 goto done;
3945 error = got_repo_open(&repo, repo_path, NULL);
3946 if (error)
3947 goto done;
3949 init_curses();
3951 error = apply_unveil(got_repo_get_path(repo), NULL);
3952 if (error)
3953 goto done;
3955 error = tog_load_refs(repo, 0);
3956 if (error)
3957 goto done;
3959 error = got_repo_match_object_id(&id1, &label1, id_str1,
3960 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3961 if (error)
3962 goto done;
3964 error = got_repo_match_object_id(&id2, &label2, id_str2,
3965 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3966 if (error)
3967 goto done;
3969 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3970 if (view == NULL) {
3971 error = got_error_from_errno("view_open");
3972 goto done;
3974 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3975 ignore_whitespace, force_text_diff, NULL, repo);
3976 if (error)
3977 goto done;
3978 error = view_loop(view);
3979 done:
3980 free(label1);
3981 free(label2);
3982 free(repo_path);
3983 free(cwd);
3984 if (repo) {
3985 const struct got_error *close_err = got_repo_close(repo);
3986 if (error == NULL)
3987 error = close_err;
3989 if (worktree)
3990 got_worktree_close(worktree);
3991 tog_free_refs();
3992 return error;
3995 __dead static void
3996 usage_blame(void)
3998 endwin();
3999 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4000 getprogname());
4001 exit(1);
4004 struct tog_blame_line {
4005 int annotated;
4006 struct got_object_id *id;
4009 static const struct got_error *
4010 draw_blame(struct tog_view *view)
4012 struct tog_blame_view_state *s = &view->state.blame;
4013 struct tog_blame *blame = &s->blame;
4014 regmatch_t *regmatch = &view->regmatch;
4015 const struct got_error *err;
4016 int lineno = 0, nprinted = 0;
4017 char *line = NULL;
4018 size_t linesize = 0;
4019 ssize_t linelen;
4020 wchar_t *wline;
4021 int width;
4022 struct tog_blame_line *blame_line;
4023 struct got_object_id *prev_id = NULL;
4024 char *id_str;
4025 struct tog_color *tc;
4027 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4028 if (err)
4029 return err;
4031 rewind(blame->f);
4032 werase(view->window);
4034 if (asprintf(&line, "commit %s", id_str) == -1) {
4035 err = got_error_from_errno("asprintf");
4036 free(id_str);
4037 return err;
4040 err = format_line(&wline, &width, line, view->ncols, 0);
4041 free(line);
4042 line = NULL;
4043 if (err)
4044 return err;
4045 if (view_needs_focus_indication(view))
4046 wstandout(view->window);
4047 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4048 if (tc)
4049 wattr_on(view->window,
4050 COLOR_PAIR(tc->colorpair), NULL);
4051 waddwstr(view->window, wline);
4052 if (tc)
4053 wattr_off(view->window,
4054 COLOR_PAIR(tc->colorpair), NULL);
4055 if (view_needs_focus_indication(view))
4056 wstandend(view->window);
4057 free(wline);
4058 wline = NULL;
4059 if (width < view->ncols - 1)
4060 waddch(view->window, '\n');
4062 if (asprintf(&line, "[%d/%d] %s%s",
4063 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4064 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4065 free(id_str);
4066 return got_error_from_errno("asprintf");
4068 free(id_str);
4069 err = format_line(&wline, &width, line, view->ncols, 0);
4070 free(line);
4071 line = NULL;
4072 if (err)
4073 return err;
4074 waddwstr(view->window, wline);
4075 free(wline);
4076 wline = NULL;
4077 if (width < view->ncols - 1)
4078 waddch(view->window, '\n');
4080 s->eof = 0;
4081 while (nprinted < view->nlines - 2) {
4082 linelen = getline(&line, &linesize, blame->f);
4083 if (linelen == -1) {
4084 if (feof(blame->f)) {
4085 s->eof = 1;
4086 break;
4088 free(line);
4089 return got_ferror(blame->f, GOT_ERR_IO);
4091 if (++lineno < s->first_displayed_line)
4092 continue;
4094 if (view->focussed && nprinted == s->selected_line - 1)
4095 wstandout(view->window);
4097 if (blame->nlines > 0) {
4098 blame_line = &blame->lines[lineno - 1];
4099 if (blame_line->annotated && prev_id &&
4100 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4101 !(view->focussed &&
4102 nprinted == s->selected_line - 1)) {
4103 waddstr(view->window, " ");
4104 } else if (blame_line->annotated) {
4105 char *id_str;
4106 err = got_object_id_str(&id_str, blame_line->id);
4107 if (err) {
4108 free(line);
4109 return err;
4111 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4112 if (tc)
4113 wattr_on(view->window,
4114 COLOR_PAIR(tc->colorpair), NULL);
4115 wprintw(view->window, "%.8s", id_str);
4116 if (tc)
4117 wattr_off(view->window,
4118 COLOR_PAIR(tc->colorpair), NULL);
4119 free(id_str);
4120 prev_id = blame_line->id;
4121 } else {
4122 waddstr(view->window, "........");
4123 prev_id = NULL;
4125 } else {
4126 waddstr(view->window, "........");
4127 prev_id = NULL;
4130 if (view->focussed && nprinted == s->selected_line - 1)
4131 wstandend(view->window);
4132 waddstr(view->window, " ");
4134 if (view->ncols <= 9) {
4135 width = 9;
4136 wline = wcsdup(L"");
4137 if (wline == NULL) {
4138 err = got_error_from_errno("wcsdup");
4139 free(line);
4140 return err;
4142 } else if (s->first_displayed_line + nprinted ==
4143 s->matched_line &&
4144 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4145 err = add_matched_line(&width, line, view->ncols - 9, 9,
4146 view->window, regmatch);
4147 if (err) {
4148 free(line);
4149 return err;
4151 width += 9;
4152 } else {
4153 err = format_line(&wline, &width, line,
4154 view->ncols - 9, 9);
4155 waddwstr(view->window, wline);
4156 free(wline);
4157 wline = NULL;
4158 width += 9;
4161 if (width <= view->ncols - 1)
4162 waddch(view->window, '\n');
4163 if (++nprinted == 1)
4164 s->first_displayed_line = lineno;
4166 free(line);
4167 s->last_displayed_line = lineno;
4169 view_vborder(view);
4171 return NULL;
4174 static const struct got_error *
4175 blame_cb(void *arg, int nlines, int lineno,
4176 struct got_commit_object *commit, struct got_object_id *id)
4178 const struct got_error *err = NULL;
4179 struct tog_blame_cb_args *a = arg;
4180 struct tog_blame_line *line;
4181 int errcode;
4183 if (nlines != a->nlines ||
4184 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4185 return got_error(GOT_ERR_RANGE);
4187 errcode = pthread_mutex_lock(&tog_mutex);
4188 if (errcode)
4189 return got_error_set_errno(errcode, "pthread_mutex_lock");
4191 if (*a->quit) { /* user has quit the blame view */
4192 err = got_error(GOT_ERR_ITER_COMPLETED);
4193 goto done;
4196 if (lineno == -1)
4197 goto done; /* no change in this commit */
4199 line = &a->lines[lineno - 1];
4200 if (line->annotated)
4201 goto done;
4203 line->id = got_object_id_dup(id);
4204 if (line->id == NULL) {
4205 err = got_error_from_errno("got_object_id_dup");
4206 goto done;
4208 line->annotated = 1;
4209 done:
4210 errcode = pthread_mutex_unlock(&tog_mutex);
4211 if (errcode)
4212 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4213 return err;
4216 static void *
4217 blame_thread(void *arg)
4219 const struct got_error *err, *close_err;
4220 struct tog_blame_thread_args *ta = arg;
4221 struct tog_blame_cb_args *a = ta->cb_args;
4222 int errcode;
4224 err = block_signals_used_by_main_thread();
4225 if (err)
4226 return (void *)err;
4228 err = got_blame(ta->path, a->commit_id, ta->repo,
4229 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4230 if (err && err->code == GOT_ERR_CANCELLED)
4231 err = NULL;
4233 errcode = pthread_mutex_lock(&tog_mutex);
4234 if (errcode)
4235 return (void *)got_error_set_errno(errcode,
4236 "pthread_mutex_lock");
4238 close_err = got_repo_close(ta->repo);
4239 if (err == NULL)
4240 err = close_err;
4241 ta->repo = NULL;
4242 *ta->complete = 1;
4244 errcode = pthread_mutex_unlock(&tog_mutex);
4245 if (errcode && err == NULL)
4246 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4248 return (void *)err;
4251 static struct got_object_id *
4252 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4253 int first_displayed_line, int selected_line)
4255 struct tog_blame_line *line;
4257 if (nlines <= 0)
4258 return NULL;
4260 line = &lines[first_displayed_line - 1 + selected_line - 1];
4261 if (!line->annotated)
4262 return NULL;
4264 return line->id;
4267 static const struct got_error *
4268 stop_blame(struct tog_blame *blame)
4270 const struct got_error *err = NULL;
4271 int i;
4273 if (blame->thread) {
4274 int errcode;
4275 errcode = pthread_mutex_unlock(&tog_mutex);
4276 if (errcode)
4277 return got_error_set_errno(errcode,
4278 "pthread_mutex_unlock");
4279 errcode = pthread_join(blame->thread, (void **)&err);
4280 if (errcode)
4281 return got_error_set_errno(errcode, "pthread_join");
4282 errcode = pthread_mutex_lock(&tog_mutex);
4283 if (errcode)
4284 return got_error_set_errno(errcode,
4285 "pthread_mutex_lock");
4286 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4287 err = NULL;
4288 blame->thread = 0; //NULL;
4290 if (blame->thread_args.repo) {
4291 const struct got_error *close_err;
4292 close_err = got_repo_close(blame->thread_args.repo);
4293 if (err == NULL)
4294 err = close_err;
4295 blame->thread_args.repo = NULL;
4297 if (blame->f) {
4298 if (fclose(blame->f) == EOF && err == NULL)
4299 err = got_error_from_errno("fclose");
4300 blame->f = NULL;
4302 if (blame->lines) {
4303 for (i = 0; i < blame->nlines; i++)
4304 free(blame->lines[i].id);
4305 free(blame->lines);
4306 blame->lines = NULL;
4308 free(blame->cb_args.commit_id);
4309 blame->cb_args.commit_id = NULL;
4311 return err;
4314 static const struct got_error *
4315 cancel_blame_view(void *arg)
4317 const struct got_error *err = NULL;
4318 int *done = arg;
4319 int errcode;
4321 errcode = pthread_mutex_lock(&tog_mutex);
4322 if (errcode)
4323 return got_error_set_errno(errcode,
4324 "pthread_mutex_unlock");
4326 if (*done)
4327 err = got_error(GOT_ERR_CANCELLED);
4329 errcode = pthread_mutex_unlock(&tog_mutex);
4330 if (errcode)
4331 return got_error_set_errno(errcode,
4332 "pthread_mutex_lock");
4334 return err;
4337 static const struct got_error *
4338 run_blame(struct tog_view *view)
4340 struct tog_blame_view_state *s = &view->state.blame;
4341 struct tog_blame *blame = &s->blame;
4342 const struct got_error *err = NULL;
4343 struct got_commit_object *commit = NULL;
4344 struct got_blob_object *blob = NULL;
4345 struct got_repository *thread_repo = NULL;
4346 struct got_object_id *obj_id = NULL;
4347 int obj_type;
4349 err = got_object_open_as_commit(&commit, s->repo,
4350 &s->blamed_commit->id);
4351 if (err)
4352 return err;
4354 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4355 if (err)
4356 goto done;
4358 err = got_object_get_type(&obj_type, s->repo, obj_id);
4359 if (err)
4360 goto done;
4362 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4363 err = got_error(GOT_ERR_OBJ_TYPE);
4364 goto done;
4367 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4368 if (err)
4369 goto done;
4370 blame->f = got_opentemp();
4371 if (blame->f == NULL) {
4372 err = got_error_from_errno("got_opentemp");
4373 goto done;
4375 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4376 &blame->line_offsets, blame->f, blob);
4377 if (err)
4378 goto done;
4379 if (blame->nlines == 0) {
4380 s->blame_complete = 1;
4381 goto done;
4384 /* Don't include \n at EOF in the blame line count. */
4385 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4386 blame->nlines--;
4388 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4389 if (blame->lines == NULL) {
4390 err = got_error_from_errno("calloc");
4391 goto done;
4394 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4395 if (err)
4396 goto done;
4398 blame->cb_args.view = view;
4399 blame->cb_args.lines = blame->lines;
4400 blame->cb_args.nlines = blame->nlines;
4401 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4402 if (blame->cb_args.commit_id == NULL) {
4403 err = got_error_from_errno("got_object_id_dup");
4404 goto done;
4406 blame->cb_args.quit = &s->done;
4408 blame->thread_args.path = s->path;
4409 blame->thread_args.repo = thread_repo;
4410 blame->thread_args.cb_args = &blame->cb_args;
4411 blame->thread_args.complete = &s->blame_complete;
4412 blame->thread_args.cancel_cb = cancel_blame_view;
4413 blame->thread_args.cancel_arg = &s->done;
4414 s->blame_complete = 0;
4416 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4417 s->first_displayed_line = 1;
4418 s->last_displayed_line = view->nlines;
4419 s->selected_line = 1;
4421 s->matched_line = 0;
4423 done:
4424 if (commit)
4425 got_object_commit_close(commit);
4426 if (blob)
4427 got_object_blob_close(blob);
4428 free(obj_id);
4429 if (err)
4430 stop_blame(blame);
4431 return err;
4434 static const struct got_error *
4435 open_blame_view(struct tog_view *view, char *path,
4436 struct got_object_id *commit_id, struct got_repository *repo)
4438 const struct got_error *err = NULL;
4439 struct tog_blame_view_state *s = &view->state.blame;
4441 STAILQ_INIT(&s->blamed_commits);
4443 s->path = strdup(path);
4444 if (s->path == NULL)
4445 return got_error_from_errno("strdup");
4447 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4448 if (err) {
4449 free(s->path);
4450 return err;
4453 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4454 s->first_displayed_line = 1;
4455 s->last_displayed_line = view->nlines;
4456 s->selected_line = 1;
4457 s->blame_complete = 0;
4458 s->repo = repo;
4459 s->commit_id = commit_id;
4460 memset(&s->blame, 0, sizeof(s->blame));
4462 STAILQ_INIT(&s->colors);
4463 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4464 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4465 get_color_value("TOG_COLOR_COMMIT"));
4466 if (err)
4467 return err;
4470 view->show = show_blame_view;
4471 view->input = input_blame_view;
4472 view->close = close_blame_view;
4473 view->search_start = search_start_blame_view;
4474 view->search_next = search_next_blame_view;
4476 return run_blame(view);
4479 static const struct got_error *
4480 close_blame_view(struct tog_view *view)
4482 const struct got_error *err = NULL;
4483 struct tog_blame_view_state *s = &view->state.blame;
4485 if (s->blame.thread)
4486 err = stop_blame(&s->blame);
4488 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4489 struct got_object_qid *blamed_commit;
4490 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4491 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4492 got_object_qid_free(blamed_commit);
4495 free(s->path);
4496 free_colors(&s->colors);
4498 return err;
4501 static const struct got_error *
4502 search_start_blame_view(struct tog_view *view)
4504 struct tog_blame_view_state *s = &view->state.blame;
4506 s->matched_line = 0;
4507 return NULL;
4510 static const struct got_error *
4511 search_next_blame_view(struct tog_view *view)
4513 struct tog_blame_view_state *s = &view->state.blame;
4514 int lineno;
4515 char *line = NULL;
4516 size_t linesize = 0;
4517 ssize_t linelen;
4519 if (!view->searching) {
4520 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4521 return NULL;
4524 if (s->matched_line) {
4525 if (view->searching == TOG_SEARCH_FORWARD)
4526 lineno = s->matched_line + 1;
4527 else
4528 lineno = s->matched_line - 1;
4529 } else
4530 lineno = s->first_displayed_line - 1 + s->selected_line;
4532 while (1) {
4533 off_t offset;
4535 if (lineno <= 0 || lineno > s->blame.nlines) {
4536 if (s->matched_line == 0) {
4537 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4538 break;
4541 if (view->searching == TOG_SEARCH_FORWARD)
4542 lineno = 1;
4543 else
4544 lineno = s->blame.nlines;
4547 offset = s->blame.line_offsets[lineno - 1];
4548 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4549 free(line);
4550 return got_error_from_errno("fseeko");
4552 linelen = getline(&line, &linesize, s->blame.f);
4553 if (linelen != -1 &&
4554 match_line(line, &view->regex, 1, &view->regmatch)) {
4555 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4556 s->matched_line = lineno;
4557 break;
4559 if (view->searching == TOG_SEARCH_FORWARD)
4560 lineno++;
4561 else
4562 lineno--;
4564 free(line);
4566 if (s->matched_line) {
4567 s->first_displayed_line = s->matched_line;
4568 s->selected_line = 1;
4571 return NULL;
4574 static const struct got_error *
4575 show_blame_view(struct tog_view *view)
4577 const struct got_error *err = NULL;
4578 struct tog_blame_view_state *s = &view->state.blame;
4579 int errcode;
4581 if (s->blame.thread == 0 && !s->blame_complete) {
4582 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4583 &s->blame.thread_args);
4584 if (errcode)
4585 return got_error_set_errno(errcode, "pthread_create");
4587 halfdelay(1); /* fast refresh while annotating */
4590 if (s->blame_complete)
4591 halfdelay(10); /* disable fast refresh */
4593 err = draw_blame(view);
4595 view_vborder(view);
4596 return err;
4599 static const struct got_error *
4600 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4602 const struct got_error *err = NULL, *thread_err = NULL;
4603 struct tog_view *diff_view;
4604 struct tog_blame_view_state *s = &view->state.blame;
4605 int begin_x = 0;
4607 switch (ch) {
4608 case 'q':
4609 s->done = 1;
4610 break;
4611 case 'g':
4612 case KEY_HOME:
4613 s->selected_line = 1;
4614 s->first_displayed_line = 1;
4615 break;
4616 case 'G':
4617 case KEY_END:
4618 if (s->blame.nlines < view->nlines - 2) {
4619 s->selected_line = s->blame.nlines;
4620 s->first_displayed_line = 1;
4621 } else {
4622 s->selected_line = view->nlines - 2;
4623 s->first_displayed_line = s->blame.nlines -
4624 (view->nlines - 3);
4626 break;
4627 case 'k':
4628 case KEY_UP:
4629 case CTRL('p'):
4630 if (s->selected_line > 1)
4631 s->selected_line--;
4632 else if (s->selected_line == 1 &&
4633 s->first_displayed_line > 1)
4634 s->first_displayed_line--;
4635 break;
4636 case KEY_PPAGE:
4637 case CTRL('b'):
4638 if (s->first_displayed_line == 1) {
4639 s->selected_line = 1;
4640 break;
4642 if (s->first_displayed_line > view->nlines - 2)
4643 s->first_displayed_line -=
4644 (view->nlines - 2);
4645 else
4646 s->first_displayed_line = 1;
4647 break;
4648 case 'j':
4649 case KEY_DOWN:
4650 case CTRL('n'):
4651 if (s->selected_line < view->nlines - 2 &&
4652 s->first_displayed_line +
4653 s->selected_line <= s->blame.nlines)
4654 s->selected_line++;
4655 else if (s->last_displayed_line <
4656 s->blame.nlines)
4657 s->first_displayed_line++;
4658 break;
4659 case 'b':
4660 case 'p': {
4661 struct got_object_id *id = NULL;
4662 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4663 s->first_displayed_line, s->selected_line);
4664 if (id == NULL)
4665 break;
4666 if (ch == 'p') {
4667 struct got_commit_object *commit, *pcommit;
4668 struct got_object_qid *pid;
4669 struct got_object_id *blob_id = NULL;
4670 int obj_type;
4671 err = got_object_open_as_commit(&commit,
4672 s->repo, id);
4673 if (err)
4674 break;
4675 pid = STAILQ_FIRST(
4676 got_object_commit_get_parent_ids(commit));
4677 if (pid == NULL) {
4678 got_object_commit_close(commit);
4679 break;
4681 /* Check if path history ends here. */
4682 err = got_object_open_as_commit(&pcommit,
4683 s->repo, &pid->id);
4684 if (err)
4685 break;
4686 err = got_object_id_by_path(&blob_id, s->repo,
4687 pcommit, s->path);
4688 got_object_commit_close(pcommit);
4689 if (err) {
4690 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4691 err = NULL;
4692 got_object_commit_close(commit);
4693 break;
4695 err = got_object_get_type(&obj_type, s->repo,
4696 blob_id);
4697 free(blob_id);
4698 /* Can't blame non-blob type objects. */
4699 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4700 got_object_commit_close(commit);
4701 break;
4703 err = got_object_qid_alloc(&s->blamed_commit,
4704 &pid->id);
4705 got_object_commit_close(commit);
4706 } else {
4707 if (got_object_id_cmp(id,
4708 &s->blamed_commit->id) == 0)
4709 break;
4710 err = got_object_qid_alloc(&s->blamed_commit,
4711 id);
4713 if (err)
4714 break;
4715 s->done = 1;
4716 thread_err = stop_blame(&s->blame);
4717 s->done = 0;
4718 if (thread_err)
4719 break;
4720 STAILQ_INSERT_HEAD(&s->blamed_commits,
4721 s->blamed_commit, entry);
4722 err = run_blame(view);
4723 if (err)
4724 break;
4725 break;
4727 case 'B': {
4728 struct got_object_qid *first;
4729 first = STAILQ_FIRST(&s->blamed_commits);
4730 if (!got_object_id_cmp(&first->id, s->commit_id))
4731 break;
4732 s->done = 1;
4733 thread_err = stop_blame(&s->blame);
4734 s->done = 0;
4735 if (thread_err)
4736 break;
4737 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4738 got_object_qid_free(s->blamed_commit);
4739 s->blamed_commit =
4740 STAILQ_FIRST(&s->blamed_commits);
4741 err = run_blame(view);
4742 if (err)
4743 break;
4744 break;
4746 case KEY_ENTER:
4747 case '\r': {
4748 struct got_object_id *id = NULL;
4749 struct got_object_qid *pid;
4750 struct got_commit_object *commit = NULL;
4751 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4752 s->first_displayed_line, s->selected_line);
4753 if (id == NULL)
4754 break;
4755 err = got_object_open_as_commit(&commit, s->repo, id);
4756 if (err)
4757 break;
4758 pid = STAILQ_FIRST(
4759 got_object_commit_get_parent_ids(commit));
4760 if (view_is_parent_view(view))
4761 begin_x = view_split_begin_x(view->begin_x);
4762 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4763 if (diff_view == NULL) {
4764 got_object_commit_close(commit);
4765 err = got_error_from_errno("view_open");
4766 break;
4768 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4769 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4770 got_object_commit_close(commit);
4771 if (err) {
4772 view_close(diff_view);
4773 break;
4775 view->focussed = 0;
4776 diff_view->focussed = 1;
4777 if (view_is_parent_view(view)) {
4778 err = view_close_child(view);
4779 if (err)
4780 break;
4781 view_set_child(view, diff_view);
4782 view->focus_child = 1;
4783 } else
4784 *new_view = diff_view;
4785 if (err)
4786 break;
4787 break;
4789 case KEY_NPAGE:
4790 case CTRL('f'):
4791 case ' ':
4792 if (s->last_displayed_line >= s->blame.nlines &&
4793 s->selected_line >= MIN(s->blame.nlines,
4794 view->nlines - 2)) {
4795 break;
4797 if (s->last_displayed_line >= s->blame.nlines &&
4798 s->selected_line < view->nlines - 2) {
4799 s->selected_line = MIN(s->blame.nlines,
4800 view->nlines - 2);
4801 break;
4803 if (s->last_displayed_line + view->nlines - 2
4804 <= s->blame.nlines)
4805 s->first_displayed_line +=
4806 view->nlines - 2;
4807 else
4808 s->first_displayed_line =
4809 s->blame.nlines -
4810 (view->nlines - 3);
4811 break;
4812 case KEY_RESIZE:
4813 if (s->selected_line > view->nlines - 2) {
4814 s->selected_line = MIN(s->blame.nlines,
4815 view->nlines - 2);
4817 break;
4818 default:
4819 break;
4821 return thread_err ? thread_err : err;
4824 static const struct got_error *
4825 cmd_blame(int argc, char *argv[])
4827 const struct got_error *error;
4828 struct got_repository *repo = NULL;
4829 struct got_worktree *worktree = NULL;
4830 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4831 char *link_target = NULL;
4832 struct got_object_id *commit_id = NULL;
4833 struct got_commit_object *commit = NULL;
4834 char *commit_id_str = NULL;
4835 int ch;
4836 struct tog_view *view;
4838 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4839 switch (ch) {
4840 case 'c':
4841 commit_id_str = optarg;
4842 break;
4843 case 'r':
4844 repo_path = realpath(optarg, NULL);
4845 if (repo_path == NULL)
4846 return got_error_from_errno2("realpath",
4847 optarg);
4848 break;
4849 default:
4850 usage_blame();
4851 /* NOTREACHED */
4855 argc -= optind;
4856 argv += optind;
4858 if (argc != 1)
4859 usage_blame();
4861 if (repo_path == NULL) {
4862 cwd = getcwd(NULL, 0);
4863 if (cwd == NULL)
4864 return got_error_from_errno("getcwd");
4865 error = got_worktree_open(&worktree, cwd);
4866 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4867 goto done;
4868 if (worktree)
4869 repo_path =
4870 strdup(got_worktree_get_repo_path(worktree));
4871 else
4872 repo_path = strdup(cwd);
4873 if (repo_path == NULL) {
4874 error = got_error_from_errno("strdup");
4875 goto done;
4879 error = got_repo_open(&repo, repo_path, NULL);
4880 if (error != NULL)
4881 goto done;
4883 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4884 worktree);
4885 if (error)
4886 goto done;
4888 init_curses();
4890 error = apply_unveil(got_repo_get_path(repo), NULL);
4891 if (error)
4892 goto done;
4894 error = tog_load_refs(repo, 0);
4895 if (error)
4896 goto done;
4898 if (commit_id_str == NULL) {
4899 struct got_reference *head_ref;
4900 error = got_ref_open(&head_ref, repo, worktree ?
4901 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4902 if (error != NULL)
4903 goto done;
4904 error = got_ref_resolve(&commit_id, repo, head_ref);
4905 got_ref_close(head_ref);
4906 } else {
4907 error = got_repo_match_object_id(&commit_id, NULL,
4908 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4910 if (error != NULL)
4911 goto done;
4913 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4914 if (view == NULL) {
4915 error = got_error_from_errno("view_open");
4916 goto done;
4919 error = got_object_open_as_commit(&commit, repo, commit_id);
4920 if (error)
4921 goto done;
4923 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4924 commit, repo);
4925 if (error)
4926 goto done;
4928 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4929 commit_id, repo);
4930 if (error)
4931 goto done;
4932 if (worktree) {
4933 /* Release work tree lock. */
4934 got_worktree_close(worktree);
4935 worktree = NULL;
4937 error = view_loop(view);
4938 done:
4939 free(repo_path);
4940 free(in_repo_path);
4941 free(link_target);
4942 free(cwd);
4943 free(commit_id);
4944 if (commit)
4945 got_object_commit_close(commit);
4946 if (worktree)
4947 got_worktree_close(worktree);
4948 if (repo) {
4949 const struct got_error *close_err = got_repo_close(repo);
4950 if (error == NULL)
4951 error = close_err;
4953 tog_free_refs();
4954 return error;
4957 static const struct got_error *
4958 draw_tree_entries(struct tog_view *view, const char *parent_path)
4960 struct tog_tree_view_state *s = &view->state.tree;
4961 const struct got_error *err = NULL;
4962 struct got_tree_entry *te;
4963 wchar_t *wline;
4964 struct tog_color *tc;
4965 int width, n, i, nentries;
4966 int limit = view->nlines;
4968 s->ndisplayed = 0;
4970 werase(view->window);
4972 if (limit == 0)
4973 return NULL;
4975 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4976 if (err)
4977 return err;
4978 if (view_needs_focus_indication(view))
4979 wstandout(view->window);
4980 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4981 if (tc)
4982 wattr_on(view->window,
4983 COLOR_PAIR(tc->colorpair), NULL);
4984 waddwstr(view->window, wline);
4985 if (tc)
4986 wattr_off(view->window,
4987 COLOR_PAIR(tc->colorpair), NULL);
4988 if (view_needs_focus_indication(view))
4989 wstandend(view->window);
4990 free(wline);
4991 wline = NULL;
4992 if (width < view->ncols - 1)
4993 waddch(view->window, '\n');
4994 if (--limit <= 0)
4995 return NULL;
4996 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4997 if (err)
4998 return err;
4999 waddwstr(view->window, wline);
5000 free(wline);
5001 wline = NULL;
5002 if (width < view->ncols - 1)
5003 waddch(view->window, '\n');
5004 if (--limit <= 0)
5005 return NULL;
5006 waddch(view->window, '\n');
5007 if (--limit <= 0)
5008 return NULL;
5010 if (s->first_displayed_entry == NULL) {
5011 te = got_object_tree_get_first_entry(s->tree);
5012 if (s->selected == 0) {
5013 if (view->focussed)
5014 wstandout(view->window);
5015 s->selected_entry = NULL;
5017 waddstr(view->window, " ..\n"); /* parent directory */
5018 if (s->selected == 0 && view->focussed)
5019 wstandend(view->window);
5020 s->ndisplayed++;
5021 if (--limit <= 0)
5022 return NULL;
5023 n = 1;
5024 } else {
5025 n = 0;
5026 te = s->first_displayed_entry;
5029 nentries = got_object_tree_get_nentries(s->tree);
5030 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5031 char *line = NULL, *id_str = NULL, *link_target = NULL;
5032 const char *modestr = "";
5033 mode_t mode;
5035 te = got_object_tree_get_entry(s->tree, i);
5036 mode = got_tree_entry_get_mode(te);
5038 if (s->show_ids) {
5039 err = got_object_id_str(&id_str,
5040 got_tree_entry_get_id(te));
5041 if (err)
5042 return got_error_from_errno(
5043 "got_object_id_str");
5045 if (got_object_tree_entry_is_submodule(te))
5046 modestr = "$";
5047 else if (S_ISLNK(mode)) {
5048 int i;
5050 err = got_tree_entry_get_symlink_target(&link_target,
5051 te, s->repo);
5052 if (err) {
5053 free(id_str);
5054 return err;
5056 for (i = 0; i < strlen(link_target); i++) {
5057 if (!isprint((unsigned char)link_target[i]))
5058 link_target[i] = '?';
5060 modestr = "@";
5062 else if (S_ISDIR(mode))
5063 modestr = "/";
5064 else if (mode & S_IXUSR)
5065 modestr = "*";
5066 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5067 got_tree_entry_get_name(te), modestr,
5068 link_target ? " -> ": "",
5069 link_target ? link_target : "") == -1) {
5070 free(id_str);
5071 free(link_target);
5072 return got_error_from_errno("asprintf");
5074 free(id_str);
5075 free(link_target);
5076 err = format_line(&wline, &width, line, view->ncols, 0);
5077 if (err) {
5078 free(line);
5079 break;
5081 if (n == s->selected) {
5082 if (view->focussed)
5083 wstandout(view->window);
5084 s->selected_entry = te;
5086 tc = match_color(&s->colors, line);
5087 if (tc)
5088 wattr_on(view->window,
5089 COLOR_PAIR(tc->colorpair), NULL);
5090 waddwstr(view->window, wline);
5091 if (tc)
5092 wattr_off(view->window,
5093 COLOR_PAIR(tc->colorpair), NULL);
5094 if (width < view->ncols - 1)
5095 waddch(view->window, '\n');
5096 if (n == s->selected && view->focussed)
5097 wstandend(view->window);
5098 free(line);
5099 free(wline);
5100 wline = NULL;
5101 n++;
5102 s->ndisplayed++;
5103 s->last_displayed_entry = te;
5104 if (--limit <= 0)
5105 break;
5108 return err;
5111 static void
5112 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5114 struct got_tree_entry *te;
5115 int isroot = s->tree == s->root;
5116 int i = 0;
5118 if (s->first_displayed_entry == NULL)
5119 return;
5121 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5122 while (i++ < maxscroll) {
5123 if (te == NULL) {
5124 if (!isroot)
5125 s->first_displayed_entry = NULL;
5126 break;
5128 s->first_displayed_entry = te;
5129 te = got_tree_entry_get_prev(s->tree, te);
5133 static void
5134 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5136 struct got_tree_entry *next, *last;
5137 int n = 0;
5139 if (s->first_displayed_entry)
5140 next = got_tree_entry_get_next(s->tree,
5141 s->first_displayed_entry);
5142 else
5143 next = got_object_tree_get_first_entry(s->tree);
5145 last = s->last_displayed_entry;
5146 while (next && last && n++ < maxscroll) {
5147 last = got_tree_entry_get_next(s->tree, last);
5148 if (last) {
5149 s->first_displayed_entry = next;
5150 next = got_tree_entry_get_next(s->tree, next);
5155 static const struct got_error *
5156 tree_entry_path(char **path, struct tog_parent_trees *parents,
5157 struct got_tree_entry *te)
5159 const struct got_error *err = NULL;
5160 struct tog_parent_tree *pt;
5161 size_t len = 2; /* for leading slash and NUL */
5163 TAILQ_FOREACH(pt, parents, entry)
5164 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5165 + 1 /* slash */;
5166 if (te)
5167 len += strlen(got_tree_entry_get_name(te));
5169 *path = calloc(1, len);
5170 if (path == NULL)
5171 return got_error_from_errno("calloc");
5173 (*path)[0] = '/';
5174 pt = TAILQ_LAST(parents, tog_parent_trees);
5175 while (pt) {
5176 const char *name = got_tree_entry_get_name(pt->selected_entry);
5177 if (strlcat(*path, name, len) >= len) {
5178 err = got_error(GOT_ERR_NO_SPACE);
5179 goto done;
5181 if (strlcat(*path, "/", len) >= len) {
5182 err = got_error(GOT_ERR_NO_SPACE);
5183 goto done;
5185 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5187 if (te) {
5188 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5189 err = got_error(GOT_ERR_NO_SPACE);
5190 goto done;
5193 done:
5194 if (err) {
5195 free(*path);
5196 *path = NULL;
5198 return err;
5201 static const struct got_error *
5202 blame_tree_entry(struct tog_view **new_view, int begin_x,
5203 struct got_tree_entry *te, struct tog_parent_trees *parents,
5204 struct got_object_id *commit_id, struct got_repository *repo)
5206 const struct got_error *err = NULL;
5207 char *path;
5208 struct tog_view *blame_view;
5210 *new_view = NULL;
5212 err = tree_entry_path(&path, parents, te);
5213 if (err)
5214 return err;
5216 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5217 if (blame_view == NULL) {
5218 err = got_error_from_errno("view_open");
5219 goto done;
5222 err = open_blame_view(blame_view, path, commit_id, repo);
5223 if (err) {
5224 if (err->code == GOT_ERR_CANCELLED)
5225 err = NULL;
5226 view_close(blame_view);
5227 } else
5228 *new_view = blame_view;
5229 done:
5230 free(path);
5231 return err;
5234 static const struct got_error *
5235 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5236 struct tog_tree_view_state *s)
5238 struct tog_view *log_view;
5239 const struct got_error *err = NULL;
5240 char *path;
5242 *new_view = NULL;
5244 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5245 if (log_view == NULL)
5246 return got_error_from_errno("view_open");
5248 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5249 if (err)
5250 return err;
5252 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5253 path, 0);
5254 if (err)
5255 view_close(log_view);
5256 else
5257 *new_view = log_view;
5258 free(path);
5259 return err;
5262 static const struct got_error *
5263 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5264 const char *head_ref_name, struct got_repository *repo)
5266 const struct got_error *err = NULL;
5267 char *commit_id_str = NULL;
5268 struct tog_tree_view_state *s = &view->state.tree;
5269 struct got_commit_object *commit = NULL;
5271 TAILQ_INIT(&s->parents);
5272 STAILQ_INIT(&s->colors);
5274 s->commit_id = got_object_id_dup(commit_id);
5275 if (s->commit_id == NULL)
5276 return got_error_from_errno("got_object_id_dup");
5278 err = got_object_open_as_commit(&commit, repo, commit_id);
5279 if (err)
5280 goto done;
5283 * The root is opened here and will be closed when the view is closed.
5284 * Any visited subtrees and their path-wise parents are opened and
5285 * closed on demand.
5287 err = got_object_open_as_tree(&s->root, repo,
5288 got_object_commit_get_tree_id(commit));
5289 if (err)
5290 goto done;
5291 s->tree = s->root;
5293 err = got_object_id_str(&commit_id_str, commit_id);
5294 if (err != NULL)
5295 goto done;
5297 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5298 err = got_error_from_errno("asprintf");
5299 goto done;
5302 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5303 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5304 if (head_ref_name) {
5305 s->head_ref_name = strdup(head_ref_name);
5306 if (s->head_ref_name == NULL) {
5307 err = got_error_from_errno("strdup");
5308 goto done;
5311 s->repo = repo;
5313 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5314 err = add_color(&s->colors, "\\$$",
5315 TOG_COLOR_TREE_SUBMODULE,
5316 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5317 if (err)
5318 goto done;
5319 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5320 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5321 if (err)
5322 goto done;
5323 err = add_color(&s->colors, "/$",
5324 TOG_COLOR_TREE_DIRECTORY,
5325 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5326 if (err)
5327 goto done;
5329 err = add_color(&s->colors, "\\*$",
5330 TOG_COLOR_TREE_EXECUTABLE,
5331 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5332 if (err)
5333 goto done;
5335 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5336 get_color_value("TOG_COLOR_COMMIT"));
5337 if (err)
5338 goto done;
5341 view->show = show_tree_view;
5342 view->input = input_tree_view;
5343 view->close = close_tree_view;
5344 view->search_start = search_start_tree_view;
5345 view->search_next = search_next_tree_view;
5346 done:
5347 free(commit_id_str);
5348 if (commit)
5349 got_object_commit_close(commit);
5350 if (err)
5351 close_tree_view(view);
5352 return err;
5355 static const struct got_error *
5356 close_tree_view(struct tog_view *view)
5358 struct tog_tree_view_state *s = &view->state.tree;
5360 free_colors(&s->colors);
5361 free(s->tree_label);
5362 s->tree_label = NULL;
5363 free(s->commit_id);
5364 s->commit_id = NULL;
5365 free(s->head_ref_name);
5366 s->head_ref_name = NULL;
5367 while (!TAILQ_EMPTY(&s->parents)) {
5368 struct tog_parent_tree *parent;
5369 parent = TAILQ_FIRST(&s->parents);
5370 TAILQ_REMOVE(&s->parents, parent, entry);
5371 if (parent->tree != s->root)
5372 got_object_tree_close(parent->tree);
5373 free(parent);
5376 if (s->tree != NULL && s->tree != s->root)
5377 got_object_tree_close(s->tree);
5378 if (s->root)
5379 got_object_tree_close(s->root);
5380 return NULL;
5383 static const struct got_error *
5384 search_start_tree_view(struct tog_view *view)
5386 struct tog_tree_view_state *s = &view->state.tree;
5388 s->matched_entry = NULL;
5389 return NULL;
5392 static int
5393 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5395 regmatch_t regmatch;
5397 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5398 0) == 0;
5401 static const struct got_error *
5402 search_next_tree_view(struct tog_view *view)
5404 struct tog_tree_view_state *s = &view->state.tree;
5405 struct got_tree_entry *te = NULL;
5407 if (!view->searching) {
5408 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5409 return NULL;
5412 if (s->matched_entry) {
5413 if (view->searching == TOG_SEARCH_FORWARD) {
5414 if (s->selected_entry)
5415 te = got_tree_entry_get_next(s->tree,
5416 s->selected_entry);
5417 else
5418 te = got_object_tree_get_first_entry(s->tree);
5419 } else {
5420 if (s->selected_entry == NULL)
5421 te = got_object_tree_get_last_entry(s->tree);
5422 else
5423 te = got_tree_entry_get_prev(s->tree,
5424 s->selected_entry);
5426 } else {
5427 if (s->selected_entry)
5428 te = s->selected_entry;
5429 else if (view->searching == TOG_SEARCH_FORWARD)
5430 te = got_object_tree_get_first_entry(s->tree);
5431 else
5432 te = got_object_tree_get_last_entry(s->tree);
5435 while (1) {
5436 if (te == NULL) {
5437 if (s->matched_entry == NULL) {
5438 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5439 return NULL;
5441 if (view->searching == TOG_SEARCH_FORWARD)
5442 te = got_object_tree_get_first_entry(s->tree);
5443 else
5444 te = got_object_tree_get_last_entry(s->tree);
5447 if (match_tree_entry(te, &view->regex)) {
5448 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5449 s->matched_entry = te;
5450 break;
5453 if (view->searching == TOG_SEARCH_FORWARD)
5454 te = got_tree_entry_get_next(s->tree, te);
5455 else
5456 te = got_tree_entry_get_prev(s->tree, te);
5459 if (s->matched_entry) {
5460 s->first_displayed_entry = s->matched_entry;
5461 s->selected = 0;
5464 return NULL;
5467 static const struct got_error *
5468 show_tree_view(struct tog_view *view)
5470 const struct got_error *err = NULL;
5471 struct tog_tree_view_state *s = &view->state.tree;
5472 char *parent_path;
5474 err = tree_entry_path(&parent_path, &s->parents, NULL);
5475 if (err)
5476 return err;
5478 err = draw_tree_entries(view, parent_path);
5479 free(parent_path);
5481 view_vborder(view);
5482 return err;
5485 static const struct got_error *
5486 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5488 const struct got_error *err = NULL;
5489 struct tog_tree_view_state *s = &view->state.tree;
5490 struct tog_view *log_view, *ref_view;
5491 struct got_tree_entry *te;
5492 int begin_x = 0, n;
5494 switch (ch) {
5495 case 'i':
5496 s->show_ids = !s->show_ids;
5497 break;
5498 case 'l':
5499 if (!s->selected_entry)
5500 break;
5501 if (view_is_parent_view(view))
5502 begin_x = view_split_begin_x(view->begin_x);
5503 err = log_selected_tree_entry(&log_view, begin_x, s);
5504 view->focussed = 0;
5505 log_view->focussed = 1;
5506 if (view_is_parent_view(view)) {
5507 err = view_close_child(view);
5508 if (err)
5509 return err;
5510 view_set_child(view, log_view);
5511 view->focus_child = 1;
5512 } else
5513 *new_view = log_view;
5514 break;
5515 case 'r':
5516 if (view_is_parent_view(view))
5517 begin_x = view_split_begin_x(view->begin_x);
5518 ref_view = view_open(view->nlines, view->ncols,
5519 view->begin_y, begin_x, TOG_VIEW_REF);
5520 if (ref_view == NULL)
5521 return got_error_from_errno("view_open");
5522 err = open_ref_view(ref_view, s->repo);
5523 if (err) {
5524 view_close(ref_view);
5525 return err;
5527 view->focussed = 0;
5528 ref_view->focussed = 1;
5529 if (view_is_parent_view(view)) {
5530 err = view_close_child(view);
5531 if (err)
5532 return err;
5533 view_set_child(view, ref_view);
5534 view->focus_child = 1;
5535 } else
5536 *new_view = ref_view;
5537 break;
5538 case 'g':
5539 case KEY_HOME:
5540 s->selected = 0;
5541 if (s->tree == s->root)
5542 s->first_displayed_entry =
5543 got_object_tree_get_first_entry(s->tree);
5544 else
5545 s->first_displayed_entry = NULL;
5546 break;
5547 case 'G':
5548 case KEY_END:
5549 s->selected = 0;
5550 te = got_object_tree_get_last_entry(s->tree);
5551 for (n = 0; n < view->nlines - 3; n++) {
5552 if (te == NULL) {
5553 if(s->tree != s->root) {
5554 s->first_displayed_entry = NULL;
5555 n++;
5557 break;
5559 s->first_displayed_entry = te;
5560 te = got_tree_entry_get_prev(s->tree, te);
5562 if (n > 0)
5563 s->selected = n - 1;
5564 break;
5565 case 'k':
5566 case KEY_UP:
5567 case CTRL('p'):
5568 if (s->selected > 0) {
5569 s->selected--;
5570 break;
5572 tree_scroll_up(s, 1);
5573 break;
5574 case KEY_PPAGE:
5575 case CTRL('b'):
5576 if (s->tree == s->root) {
5577 if (got_object_tree_get_first_entry(s->tree) ==
5578 s->first_displayed_entry)
5579 s->selected = 0;
5580 } else {
5581 if (s->first_displayed_entry == NULL)
5582 s->selected = 0;
5584 tree_scroll_up(s, MAX(0, view->nlines - 3));
5585 break;
5586 case 'j':
5587 case KEY_DOWN:
5588 case CTRL('n'):
5589 if (s->selected < s->ndisplayed - 1) {
5590 s->selected++;
5591 break;
5593 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5594 == NULL)
5595 /* can't scroll any further */
5596 break;
5597 tree_scroll_down(s, 1);
5598 break;
5599 case KEY_NPAGE:
5600 case CTRL('f'):
5601 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5602 == NULL) {
5603 /* can't scroll any further; move cursor down */
5604 if (s->selected < s->ndisplayed - 1)
5605 s->selected = s->ndisplayed - 1;
5606 break;
5608 tree_scroll_down(s, view->nlines - 3);
5609 break;
5610 case KEY_ENTER:
5611 case '\r':
5612 case KEY_BACKSPACE:
5613 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5614 struct tog_parent_tree *parent;
5615 /* user selected '..' */
5616 if (s->tree == s->root)
5617 break;
5618 parent = TAILQ_FIRST(&s->parents);
5619 TAILQ_REMOVE(&s->parents, parent,
5620 entry);
5621 got_object_tree_close(s->tree);
5622 s->tree = parent->tree;
5623 s->first_displayed_entry =
5624 parent->first_displayed_entry;
5625 s->selected_entry =
5626 parent->selected_entry;
5627 s->selected = parent->selected;
5628 free(parent);
5629 } else if (S_ISDIR(got_tree_entry_get_mode(
5630 s->selected_entry))) {
5631 struct got_tree_object *subtree;
5632 err = got_object_open_as_tree(&subtree, s->repo,
5633 got_tree_entry_get_id(s->selected_entry));
5634 if (err)
5635 break;
5636 err = tree_view_visit_subtree(s, subtree);
5637 if (err) {
5638 got_object_tree_close(subtree);
5639 break;
5641 } else if (S_ISREG(got_tree_entry_get_mode(
5642 s->selected_entry))) {
5643 struct tog_view *blame_view;
5644 int begin_x = view_is_parent_view(view) ?
5645 view_split_begin_x(view->begin_x) : 0;
5647 err = blame_tree_entry(&blame_view, begin_x,
5648 s->selected_entry, &s->parents,
5649 s->commit_id, s->repo);
5650 if (err)
5651 break;
5652 view->focussed = 0;
5653 blame_view->focussed = 1;
5654 if (view_is_parent_view(view)) {
5655 err = view_close_child(view);
5656 if (err)
5657 return err;
5658 view_set_child(view, blame_view);
5659 view->focus_child = 1;
5660 } else
5661 *new_view = blame_view;
5663 break;
5664 case KEY_RESIZE:
5665 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5666 s->selected = view->nlines - 4;
5667 break;
5668 default:
5669 break;
5672 return err;
5675 __dead static void
5676 usage_tree(void)
5678 endwin();
5679 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5680 getprogname());
5681 exit(1);
5684 static const struct got_error *
5685 cmd_tree(int argc, char *argv[])
5687 const struct got_error *error;
5688 struct got_repository *repo = NULL;
5689 struct got_worktree *worktree = NULL;
5690 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5691 struct got_object_id *commit_id = NULL;
5692 struct got_commit_object *commit = NULL;
5693 const char *commit_id_arg = NULL;
5694 char *label = NULL;
5695 struct got_reference *ref = NULL;
5696 const char *head_ref_name = NULL;
5697 int ch;
5698 struct tog_view *view;
5700 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5701 switch (ch) {
5702 case 'c':
5703 commit_id_arg = optarg;
5704 break;
5705 case 'r':
5706 repo_path = realpath(optarg, NULL);
5707 if (repo_path == NULL)
5708 return got_error_from_errno2("realpath",
5709 optarg);
5710 break;
5711 default:
5712 usage_tree();
5713 /* NOTREACHED */
5717 argc -= optind;
5718 argv += optind;
5720 if (argc > 1)
5721 usage_tree();
5723 if (repo_path == NULL) {
5724 cwd = getcwd(NULL, 0);
5725 if (cwd == NULL)
5726 return got_error_from_errno("getcwd");
5727 error = got_worktree_open(&worktree, cwd);
5728 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5729 goto done;
5730 if (worktree)
5731 repo_path =
5732 strdup(got_worktree_get_repo_path(worktree));
5733 else
5734 repo_path = strdup(cwd);
5735 if (repo_path == NULL) {
5736 error = got_error_from_errno("strdup");
5737 goto done;
5741 error = got_repo_open(&repo, repo_path, NULL);
5742 if (error != NULL)
5743 goto done;
5745 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5746 repo, worktree);
5747 if (error)
5748 goto done;
5750 init_curses();
5752 error = apply_unveil(got_repo_get_path(repo), NULL);
5753 if (error)
5754 goto done;
5756 error = tog_load_refs(repo, 0);
5757 if (error)
5758 goto done;
5760 if (commit_id_arg == NULL) {
5761 error = got_repo_match_object_id(&commit_id, &label,
5762 worktree ? got_worktree_get_head_ref_name(worktree) :
5763 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5764 if (error)
5765 goto done;
5766 head_ref_name = label;
5767 } else {
5768 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5769 if (error == NULL)
5770 head_ref_name = got_ref_get_name(ref);
5771 else if (error->code != GOT_ERR_NOT_REF)
5772 goto done;
5773 error = got_repo_match_object_id(&commit_id, NULL,
5774 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5775 if (error)
5776 goto done;
5779 error = got_object_open_as_commit(&commit, repo, commit_id);
5780 if (error)
5781 goto done;
5783 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5784 if (view == NULL) {
5785 error = got_error_from_errno("view_open");
5786 goto done;
5788 error = open_tree_view(view, commit_id, head_ref_name, repo);
5789 if (error)
5790 goto done;
5791 if (!got_path_is_root_dir(in_repo_path)) {
5792 error = tree_view_walk_path(&view->state.tree, commit,
5793 in_repo_path);
5794 if (error)
5795 goto done;
5798 if (worktree) {
5799 /* Release work tree lock. */
5800 got_worktree_close(worktree);
5801 worktree = NULL;
5803 error = view_loop(view);
5804 done:
5805 free(repo_path);
5806 free(cwd);
5807 free(commit_id);
5808 free(label);
5809 if (ref)
5810 got_ref_close(ref);
5811 if (repo) {
5812 const struct got_error *close_err = got_repo_close(repo);
5813 if (error == NULL)
5814 error = close_err;
5816 tog_free_refs();
5817 return error;
5820 static const struct got_error *
5821 ref_view_load_refs(struct tog_ref_view_state *s)
5823 struct got_reflist_entry *sre;
5824 struct tog_reflist_entry *re;
5826 s->nrefs = 0;
5827 TAILQ_FOREACH(sre, &tog_refs, entry) {
5828 if (strncmp(got_ref_get_name(sre->ref),
5829 "refs/got/", 9) == 0 &&
5830 strncmp(got_ref_get_name(sre->ref),
5831 "refs/got/backup/", 16) != 0)
5832 continue;
5834 re = malloc(sizeof(*re));
5835 if (re == NULL)
5836 return got_error_from_errno("malloc");
5838 re->ref = got_ref_dup(sre->ref);
5839 if (re->ref == NULL)
5840 return got_error_from_errno("got_ref_dup");
5841 re->idx = s->nrefs++;
5842 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5845 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5846 return NULL;
5849 void
5850 ref_view_free_refs(struct tog_ref_view_state *s)
5852 struct tog_reflist_entry *re;
5854 while (!TAILQ_EMPTY(&s->refs)) {
5855 re = TAILQ_FIRST(&s->refs);
5856 TAILQ_REMOVE(&s->refs, re, entry);
5857 got_ref_close(re->ref);
5858 free(re);
5862 static const struct got_error *
5863 open_ref_view(struct tog_view *view, struct got_repository *repo)
5865 const struct got_error *err = NULL;
5866 struct tog_ref_view_state *s = &view->state.ref;
5868 s->selected_entry = 0;
5869 s->repo = repo;
5871 TAILQ_INIT(&s->refs);
5872 STAILQ_INIT(&s->colors);
5874 err = ref_view_load_refs(s);
5875 if (err)
5876 return err;
5878 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5879 err = add_color(&s->colors, "^refs/heads/",
5880 TOG_COLOR_REFS_HEADS,
5881 get_color_value("TOG_COLOR_REFS_HEADS"));
5882 if (err)
5883 goto done;
5885 err = add_color(&s->colors, "^refs/tags/",
5886 TOG_COLOR_REFS_TAGS,
5887 get_color_value("TOG_COLOR_REFS_TAGS"));
5888 if (err)
5889 goto done;
5891 err = add_color(&s->colors, "^refs/remotes/",
5892 TOG_COLOR_REFS_REMOTES,
5893 get_color_value("TOG_COLOR_REFS_REMOTES"));
5894 if (err)
5895 goto done;
5897 err = add_color(&s->colors, "^refs/got/backup/",
5898 TOG_COLOR_REFS_BACKUP,
5899 get_color_value("TOG_COLOR_REFS_BACKUP"));
5900 if (err)
5901 goto done;
5904 view->show = show_ref_view;
5905 view->input = input_ref_view;
5906 view->close = close_ref_view;
5907 view->search_start = search_start_ref_view;
5908 view->search_next = search_next_ref_view;
5909 done:
5910 if (err)
5911 free_colors(&s->colors);
5912 return err;
5915 static const struct got_error *
5916 close_ref_view(struct tog_view *view)
5918 struct tog_ref_view_state *s = &view->state.ref;
5920 ref_view_free_refs(s);
5921 free_colors(&s->colors);
5923 return NULL;
5926 static const struct got_error *
5927 resolve_reflist_entry(struct got_object_id **commit_id,
5928 struct tog_reflist_entry *re, struct got_repository *repo)
5930 const struct got_error *err = NULL;
5931 struct got_object_id *obj_id;
5932 struct got_tag_object *tag = NULL;
5933 int obj_type;
5935 *commit_id = NULL;
5937 err = got_ref_resolve(&obj_id, repo, re->ref);
5938 if (err)
5939 return err;
5941 err = got_object_get_type(&obj_type, repo, obj_id);
5942 if (err)
5943 goto done;
5945 switch (obj_type) {
5946 case GOT_OBJ_TYPE_COMMIT:
5947 *commit_id = obj_id;
5948 break;
5949 case GOT_OBJ_TYPE_TAG:
5950 err = got_object_open_as_tag(&tag, repo, obj_id);
5951 if (err)
5952 goto done;
5953 free(obj_id);
5954 err = got_object_get_type(&obj_type, repo,
5955 got_object_tag_get_object_id(tag));
5956 if (err)
5957 goto done;
5958 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5959 err = got_error(GOT_ERR_OBJ_TYPE);
5960 goto done;
5962 *commit_id = got_object_id_dup(
5963 got_object_tag_get_object_id(tag));
5964 if (*commit_id == NULL) {
5965 err = got_error_from_errno("got_object_id_dup");
5966 goto done;
5968 break;
5969 default:
5970 err = got_error(GOT_ERR_OBJ_TYPE);
5971 break;
5974 done:
5975 if (tag)
5976 got_object_tag_close(tag);
5977 if (err) {
5978 free(*commit_id);
5979 *commit_id = NULL;
5981 return err;
5984 static const struct got_error *
5985 log_ref_entry(struct tog_view **new_view, int begin_x,
5986 struct tog_reflist_entry *re, struct got_repository *repo)
5988 struct tog_view *log_view;
5989 const struct got_error *err = NULL;
5990 struct got_object_id *commit_id = NULL;
5992 *new_view = NULL;
5994 err = resolve_reflist_entry(&commit_id, re, repo);
5995 if (err) {
5996 if (err->code != GOT_ERR_OBJ_TYPE)
5997 return err;
5998 else
5999 return NULL;
6002 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6003 if (log_view == NULL) {
6004 err = got_error_from_errno("view_open");
6005 goto done;
6008 err = open_log_view(log_view, commit_id, repo,
6009 got_ref_get_name(re->ref), "", 0);
6010 done:
6011 if (err)
6012 view_close(log_view);
6013 else
6014 *new_view = log_view;
6015 free(commit_id);
6016 return err;
6019 static void
6020 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6022 struct tog_reflist_entry *re;
6023 int i = 0;
6025 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6026 return;
6028 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6029 while (i++ < maxscroll) {
6030 if (re == NULL)
6031 break;
6032 s->first_displayed_entry = re;
6033 re = TAILQ_PREV(re, tog_reflist_head, entry);
6037 static void
6038 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6040 struct tog_reflist_entry *next, *last;
6041 int n = 0;
6043 if (s->first_displayed_entry)
6044 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6045 else
6046 next = TAILQ_FIRST(&s->refs);
6048 last = s->last_displayed_entry;
6049 while (next && last && n++ < maxscroll) {
6050 last = TAILQ_NEXT(last, entry);
6051 if (last) {
6052 s->first_displayed_entry = next;
6053 next = TAILQ_NEXT(next, entry);
6058 static const struct got_error *
6059 search_start_ref_view(struct tog_view *view)
6061 struct tog_ref_view_state *s = &view->state.ref;
6063 s->matched_entry = NULL;
6064 return NULL;
6067 static int
6068 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6070 regmatch_t regmatch;
6072 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6073 0) == 0;
6076 static const struct got_error *
6077 search_next_ref_view(struct tog_view *view)
6079 struct tog_ref_view_state *s = &view->state.ref;
6080 struct tog_reflist_entry *re = NULL;
6082 if (!view->searching) {
6083 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6084 return NULL;
6087 if (s->matched_entry) {
6088 if (view->searching == TOG_SEARCH_FORWARD) {
6089 if (s->selected_entry)
6090 re = TAILQ_NEXT(s->selected_entry, entry);
6091 else
6092 re = TAILQ_PREV(s->selected_entry,
6093 tog_reflist_head, entry);
6094 } else {
6095 if (s->selected_entry == NULL)
6096 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6097 else
6098 re = TAILQ_PREV(s->selected_entry,
6099 tog_reflist_head, entry);
6101 } else {
6102 if (s->selected_entry)
6103 re = s->selected_entry;
6104 else if (view->searching == TOG_SEARCH_FORWARD)
6105 re = TAILQ_FIRST(&s->refs);
6106 else
6107 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6110 while (1) {
6111 if (re == NULL) {
6112 if (s->matched_entry == NULL) {
6113 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6114 return NULL;
6116 if (view->searching == TOG_SEARCH_FORWARD)
6117 re = TAILQ_FIRST(&s->refs);
6118 else
6119 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6122 if (match_reflist_entry(re, &view->regex)) {
6123 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6124 s->matched_entry = re;
6125 break;
6128 if (view->searching == TOG_SEARCH_FORWARD)
6129 re = TAILQ_NEXT(re, entry);
6130 else
6131 re = TAILQ_PREV(re, tog_reflist_head, entry);
6134 if (s->matched_entry) {
6135 s->first_displayed_entry = s->matched_entry;
6136 s->selected = 0;
6139 return NULL;
6142 static const struct got_error *
6143 show_ref_view(struct tog_view *view)
6145 const struct got_error *err = NULL;
6146 struct tog_ref_view_state *s = &view->state.ref;
6147 struct tog_reflist_entry *re;
6148 char *line = NULL;
6149 wchar_t *wline;
6150 struct tog_color *tc;
6151 int width, n;
6152 int limit = view->nlines;
6154 werase(view->window);
6156 s->ndisplayed = 0;
6158 if (limit == 0)
6159 return NULL;
6161 re = s->first_displayed_entry;
6163 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6164 s->nrefs) == -1)
6165 return got_error_from_errno("asprintf");
6167 err = format_line(&wline, &width, line, view->ncols, 0);
6168 if (err) {
6169 free(line);
6170 return err;
6172 if (view_needs_focus_indication(view))
6173 wstandout(view->window);
6174 waddwstr(view->window, wline);
6175 if (view_needs_focus_indication(view))
6176 wstandend(view->window);
6177 free(wline);
6178 wline = NULL;
6179 free(line);
6180 line = NULL;
6181 if (width < view->ncols - 1)
6182 waddch(view->window, '\n');
6183 if (--limit <= 0)
6184 return NULL;
6186 n = 0;
6187 while (re && limit > 0) {
6188 char *line = NULL;
6190 if (got_ref_is_symbolic(re->ref)) {
6191 if (asprintf(&line, "%s -> %s",
6192 got_ref_get_name(re->ref),
6193 got_ref_get_symref_target(re->ref)) == -1)
6194 return got_error_from_errno("asprintf");
6195 } else if (s->show_ids) {
6196 struct got_object_id *id;
6197 char *id_str;
6198 err = got_ref_resolve(&id, s->repo, re->ref);
6199 if (err)
6200 return err;
6201 err = got_object_id_str(&id_str, id);
6202 if (err) {
6203 free(id);
6204 return err;
6206 if (asprintf(&line, "%s: %s",
6207 got_ref_get_name(re->ref), id_str) == -1) {
6208 err = got_error_from_errno("asprintf");
6209 free(id);
6210 free(id_str);
6211 return err;
6213 free(id);
6214 free(id_str);
6215 } else {
6216 line = strdup(got_ref_get_name(re->ref));
6217 if (line == NULL)
6218 return got_error_from_errno("strdup");
6221 err = format_line(&wline, &width, line, view->ncols, 0);
6222 if (err) {
6223 free(line);
6224 return err;
6226 if (n == s->selected) {
6227 if (view->focussed)
6228 wstandout(view->window);
6229 s->selected_entry = re;
6231 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6232 if (tc)
6233 wattr_on(view->window,
6234 COLOR_PAIR(tc->colorpair), NULL);
6235 waddwstr(view->window, wline);
6236 if (tc)
6237 wattr_off(view->window,
6238 COLOR_PAIR(tc->colorpair), NULL);
6239 if (width < view->ncols - 1)
6240 waddch(view->window, '\n');
6241 if (n == s->selected && view->focussed)
6242 wstandend(view->window);
6243 free(line);
6244 free(wline);
6245 wline = NULL;
6246 n++;
6247 s->ndisplayed++;
6248 s->last_displayed_entry = re;
6250 limit--;
6251 re = TAILQ_NEXT(re, entry);
6254 view_vborder(view);
6255 return err;
6258 static const struct got_error *
6259 browse_ref_tree(struct tog_view **new_view, int begin_x,
6260 struct tog_reflist_entry *re, struct got_repository *repo)
6262 const struct got_error *err = NULL;
6263 struct got_object_id *commit_id = NULL;
6264 struct tog_view *tree_view;
6266 *new_view = NULL;
6268 err = resolve_reflist_entry(&commit_id, re, repo);
6269 if (err) {
6270 if (err->code != GOT_ERR_OBJ_TYPE)
6271 return err;
6272 else
6273 return NULL;
6277 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6278 if (tree_view == NULL) {
6279 err = got_error_from_errno("view_open");
6280 goto done;
6283 err = open_tree_view(tree_view, commit_id,
6284 got_ref_get_name(re->ref), repo);
6285 if (err)
6286 goto done;
6288 *new_view = tree_view;
6289 done:
6290 free(commit_id);
6291 return err;
6293 static const struct got_error *
6294 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6296 const struct got_error *err = NULL;
6297 struct tog_ref_view_state *s = &view->state.ref;
6298 struct tog_view *log_view, *tree_view;
6299 struct tog_reflist_entry *re;
6300 int begin_x = 0, n;
6302 switch (ch) {
6303 case 'i':
6304 s->show_ids = !s->show_ids;
6305 break;
6306 case 'o':
6307 s->sort_by_date = !s->sort_by_date;
6308 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6309 got_ref_cmp_by_commit_timestamp_descending :
6310 tog_ref_cmp_by_name, s->repo);
6311 if (err)
6312 break;
6313 got_reflist_object_id_map_free(tog_refs_idmap);
6314 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6315 &tog_refs, s->repo);
6316 if (err)
6317 break;
6318 ref_view_free_refs(s);
6319 err = ref_view_load_refs(s);
6320 break;
6321 case KEY_ENTER:
6322 case '\r':
6323 if (!s->selected_entry)
6324 break;
6325 if (view_is_parent_view(view))
6326 begin_x = view_split_begin_x(view->begin_x);
6327 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6328 s->repo);
6329 view->focussed = 0;
6330 log_view->focussed = 1;
6331 if (view_is_parent_view(view)) {
6332 err = view_close_child(view);
6333 if (err)
6334 return err;
6335 view_set_child(view, log_view);
6336 view->focus_child = 1;
6337 } else
6338 *new_view = log_view;
6339 break;
6340 case 't':
6341 if (!s->selected_entry)
6342 break;
6343 if (view_is_parent_view(view))
6344 begin_x = view_split_begin_x(view->begin_x);
6345 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6346 s->repo);
6347 if (err || tree_view == NULL)
6348 break;
6349 view->focussed = 0;
6350 tree_view->focussed = 1;
6351 if (view_is_parent_view(view)) {
6352 err = view_close_child(view);
6353 if (err)
6354 return err;
6355 view_set_child(view, tree_view);
6356 view->focus_child = 1;
6357 } else
6358 *new_view = tree_view;
6359 break;
6360 case 'g':
6361 case KEY_HOME:
6362 s->selected = 0;
6363 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6364 break;
6365 case 'G':
6366 case KEY_END:
6367 s->selected = 0;
6368 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6369 for (n = 0; n < view->nlines - 1; n++) {
6370 if (re == NULL)
6371 break;
6372 s->first_displayed_entry = re;
6373 re = TAILQ_PREV(re, tog_reflist_head, entry);
6375 if (n > 0)
6376 s->selected = n - 1;
6377 break;
6378 case 'k':
6379 case KEY_UP:
6380 case CTRL('p'):
6381 if (s->selected > 0) {
6382 s->selected--;
6383 break;
6385 ref_scroll_up(s, 1);
6386 break;
6387 case KEY_PPAGE:
6388 case CTRL('b'):
6389 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6390 s->selected = 0;
6391 ref_scroll_up(s, MAX(0, view->nlines - 1));
6392 break;
6393 case 'j':
6394 case KEY_DOWN:
6395 case CTRL('n'):
6396 if (s->selected < s->ndisplayed - 1) {
6397 s->selected++;
6398 break;
6400 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6401 /* can't scroll any further */
6402 break;
6403 ref_scroll_down(s, 1);
6404 break;
6405 case KEY_NPAGE:
6406 case CTRL('f'):
6407 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6408 /* can't scroll any further; move cursor down */
6409 if (s->selected < s->ndisplayed - 1)
6410 s->selected = s->ndisplayed - 1;
6411 break;
6413 ref_scroll_down(s, view->nlines - 1);
6414 break;
6415 case CTRL('l'):
6416 tog_free_refs();
6417 err = tog_load_refs(s->repo, s->sort_by_date);
6418 if (err)
6419 break;
6420 ref_view_free_refs(s);
6421 err = ref_view_load_refs(s);
6422 break;
6423 case KEY_RESIZE:
6424 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6425 s->selected = view->nlines - 2;
6426 break;
6427 default:
6428 break;
6431 return err;
6434 __dead static void
6435 usage_ref(void)
6437 endwin();
6438 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6439 getprogname());
6440 exit(1);
6443 static const struct got_error *
6444 cmd_ref(int argc, char *argv[])
6446 const struct got_error *error;
6447 struct got_repository *repo = NULL;
6448 struct got_worktree *worktree = NULL;
6449 char *cwd = NULL, *repo_path = NULL;
6450 int ch;
6451 struct tog_view *view;
6453 while ((ch = getopt(argc, argv, "r:")) != -1) {
6454 switch (ch) {
6455 case 'r':
6456 repo_path = realpath(optarg, NULL);
6457 if (repo_path == NULL)
6458 return got_error_from_errno2("realpath",
6459 optarg);
6460 break;
6461 default:
6462 usage_ref();
6463 /* NOTREACHED */
6467 argc -= optind;
6468 argv += optind;
6470 if (argc > 1)
6471 usage_ref();
6473 if (repo_path == NULL) {
6474 cwd = getcwd(NULL, 0);
6475 if (cwd == NULL)
6476 return got_error_from_errno("getcwd");
6477 error = got_worktree_open(&worktree, cwd);
6478 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6479 goto done;
6480 if (worktree)
6481 repo_path =
6482 strdup(got_worktree_get_repo_path(worktree));
6483 else
6484 repo_path = strdup(cwd);
6485 if (repo_path == NULL) {
6486 error = got_error_from_errno("strdup");
6487 goto done;
6491 error = got_repo_open(&repo, repo_path, NULL);
6492 if (error != NULL)
6493 goto done;
6495 init_curses();
6497 error = apply_unveil(got_repo_get_path(repo), NULL);
6498 if (error)
6499 goto done;
6501 error = tog_load_refs(repo, 0);
6502 if (error)
6503 goto done;
6505 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6506 if (view == NULL) {
6507 error = got_error_from_errno("view_open");
6508 goto done;
6511 error = open_ref_view(view, repo);
6512 if (error)
6513 goto done;
6515 if (worktree) {
6516 /* Release work tree lock. */
6517 got_worktree_close(worktree);
6518 worktree = NULL;
6520 error = view_loop(view);
6521 done:
6522 free(repo_path);
6523 free(cwd);
6524 if (repo) {
6525 const struct got_error *close_err = got_repo_close(repo);
6526 if (close_err)
6527 error = close_err;
6529 tog_free_refs();
6530 return error;
6533 static void
6534 list_commands(FILE *fp)
6536 size_t i;
6538 fprintf(fp, "commands:");
6539 for (i = 0; i < nitems(tog_commands); i++) {
6540 const struct tog_cmd *cmd = &tog_commands[i];
6541 fprintf(fp, " %s", cmd->name);
6543 fputc('\n', fp);
6546 __dead static void
6547 usage(int hflag, int status)
6549 FILE *fp = (status == 0) ? stdout : stderr;
6551 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6552 getprogname());
6553 if (hflag) {
6554 fprintf(fp, "lazy usage: %s path\n", getprogname());
6555 list_commands(fp);
6557 exit(status);
6560 static char **
6561 make_argv(int argc, ...)
6563 va_list ap;
6564 char **argv;
6565 int i;
6567 va_start(ap, argc);
6569 argv = calloc(argc, sizeof(char *));
6570 if (argv == NULL)
6571 err(1, "calloc");
6572 for (i = 0; i < argc; i++) {
6573 argv[i] = strdup(va_arg(ap, char *));
6574 if (argv[i] == NULL)
6575 err(1, "strdup");
6578 va_end(ap);
6579 return argv;
6583 * Try to convert 'tog path' into a 'tog log path' command.
6584 * The user could simply have mistyped the command rather than knowingly
6585 * provided a path. So check whether argv[0] can in fact be resolved
6586 * to a path in the HEAD commit and print a special error if not.
6587 * This hack is for mpi@ <3
6589 static const struct got_error *
6590 tog_log_with_path(int argc, char *argv[])
6592 const struct got_error *error = NULL, *close_err;
6593 const struct tog_cmd *cmd = NULL;
6594 struct got_repository *repo = NULL;
6595 struct got_worktree *worktree = NULL;
6596 struct got_object_id *commit_id = NULL, *id = NULL;
6597 struct got_commit_object *commit = NULL;
6598 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6599 char *commit_id_str = NULL, **cmd_argv = NULL;
6601 cwd = getcwd(NULL, 0);
6602 if (cwd == NULL)
6603 return got_error_from_errno("getcwd");
6605 error = got_worktree_open(&worktree, cwd);
6606 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6607 goto done;
6609 if (worktree)
6610 repo_path = strdup(got_worktree_get_repo_path(worktree));
6611 else
6612 repo_path = strdup(cwd);
6613 if (repo_path == NULL) {
6614 error = got_error_from_errno("strdup");
6615 goto done;
6618 error = got_repo_open(&repo, repo_path, NULL);
6619 if (error != NULL)
6620 goto done;
6622 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6623 repo, worktree);
6624 if (error)
6625 goto done;
6627 error = tog_load_refs(repo, 0);
6628 if (error)
6629 goto done;
6630 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6631 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6632 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6633 if (error)
6634 goto done;
6636 if (worktree) {
6637 got_worktree_close(worktree);
6638 worktree = NULL;
6641 error = got_object_open_as_commit(&commit, repo, commit_id);
6642 if (error)
6643 goto done;
6645 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6646 if (error) {
6647 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6648 goto done;
6649 fprintf(stderr, "%s: '%s' is no known command or path\n",
6650 getprogname(), argv[0]);
6651 usage(1, 1);
6652 /* not reached */
6655 close_err = got_repo_close(repo);
6656 if (error == NULL)
6657 error = close_err;
6658 repo = NULL;
6660 error = got_object_id_str(&commit_id_str, commit_id);
6661 if (error)
6662 goto done;
6664 cmd = &tog_commands[0]; /* log */
6665 argc = 4;
6666 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6667 error = cmd->cmd_main(argc, cmd_argv);
6668 done:
6669 if (repo) {
6670 close_err = got_repo_close(repo);
6671 if (error == NULL)
6672 error = close_err;
6674 if (commit)
6675 got_object_commit_close(commit);
6676 if (worktree)
6677 got_worktree_close(worktree);
6678 free(id);
6679 free(commit_id_str);
6680 free(commit_id);
6681 free(cwd);
6682 free(repo_path);
6683 free(in_repo_path);
6684 if (cmd_argv) {
6685 int i;
6686 for (i = 0; i < argc; i++)
6687 free(cmd_argv[i]);
6688 free(cmd_argv);
6690 tog_free_refs();
6691 return error;
6694 int
6695 main(int argc, char *argv[])
6697 const struct got_error *error = NULL;
6698 const struct tog_cmd *cmd = NULL;
6699 int ch, hflag = 0, Vflag = 0;
6700 char **cmd_argv = NULL;
6701 static const struct option longopts[] = {
6702 { "version", no_argument, NULL, 'V' },
6703 { NULL, 0, NULL, 0}
6706 setlocale(LC_CTYPE, "");
6708 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6709 switch (ch) {
6710 case 'h':
6711 hflag = 1;
6712 break;
6713 case 'V':
6714 Vflag = 1;
6715 break;
6716 default:
6717 usage(hflag, 1);
6718 /* NOTREACHED */
6722 argc -= optind;
6723 argv += optind;
6724 optind = 1;
6725 optreset = 1;
6727 if (Vflag) {
6728 got_version_print_str();
6729 return 0;
6732 #ifndef PROFILE
6733 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6734 NULL) == -1)
6735 err(1, "pledge");
6736 #endif
6738 if (argc == 0) {
6739 if (hflag)
6740 usage(hflag, 0);
6741 /* Build an argument vector which runs a default command. */
6742 cmd = &tog_commands[0];
6743 argc = 1;
6744 cmd_argv = make_argv(argc, cmd->name);
6745 } else {
6746 size_t i;
6748 /* Did the user specify a command? */
6749 for (i = 0; i < nitems(tog_commands); i++) {
6750 if (strncmp(tog_commands[i].name, argv[0],
6751 strlen(argv[0])) == 0) {
6752 cmd = &tog_commands[i];
6753 break;
6758 if (cmd == NULL) {
6759 if (argc != 1)
6760 usage(0, 1);
6761 /* No command specified; try log with a path */
6762 error = tog_log_with_path(argc, argv);
6763 } else {
6764 if (hflag)
6765 cmd->cmd_usage();
6766 else
6767 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6770 endwin();
6771 putchar('\n');
6772 if (cmd_argv) {
6773 int i;
6774 for (i = 0; i < argc; i++)
6775 free(cmd_argv[i]);
6776 free(cmd_argv);
6779 if (error && error->code != GOT_ERR_CANCELLED)
6780 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6781 return 0;