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, *f1, *f2;
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;
605 static volatile sig_atomic_t tog_sigint_received;
606 static volatile sig_atomic_t tog_sigterm_received;
608 static void
609 tog_sigwinch(int signo)
611 tog_sigwinch_received = 1;
614 static void
615 tog_sigpipe(int signo)
617 tog_sigpipe_received = 1;
620 static void
621 tog_sigcont(int signo)
623 tog_sigcont_received = 1;
626 static void
627 tog_sigint(int signo)
629 tog_sigint_received = 1;
632 static void
633 tog_sigterm(int signo)
635 tog_sigterm_received = 1;
638 static int
639 tog_fatal_signal_received()
641 return (tog_sigpipe_received ||
642 tog_sigint_received || tog_sigint_received);
646 static const struct got_error *
647 view_close(struct tog_view *view)
649 const struct got_error *err = NULL;
651 if (view->child) {
652 view_close(view->child);
653 view->child = NULL;
655 if (view->close)
656 err = view->close(view);
657 if (view->panel)
658 del_panel(view->panel);
659 if (view->window)
660 delwin(view->window);
661 free(view);
662 return err;
665 static struct tog_view *
666 view_open(int nlines, int ncols, int begin_y, int begin_x,
667 enum tog_view_type type)
669 struct tog_view *view = calloc(1, sizeof(*view));
671 if (view == NULL)
672 return NULL;
674 view->type = type;
675 view->lines = LINES;
676 view->cols = COLS;
677 view->nlines = nlines ? nlines : LINES - begin_y;
678 view->ncols = ncols ? ncols : COLS - begin_x;
679 view->begin_y = begin_y;
680 view->begin_x = begin_x;
681 view->window = newwin(nlines, ncols, begin_y, begin_x);
682 if (view->window == NULL) {
683 view_close(view);
684 return NULL;
686 view->panel = new_panel(view->window);
687 if (view->panel == NULL ||
688 set_panel_userptr(view->panel, view) != OK) {
689 view_close(view);
690 return NULL;
693 keypad(view->window, TRUE);
694 return view;
697 static int
698 view_split_begin_x(int begin_x)
700 if (begin_x > 0 || COLS < 120)
701 return 0;
702 return (COLS - MAX(COLS / 2, 80));
705 static const struct got_error *view_resize(struct tog_view *);
707 static const struct got_error *
708 view_splitscreen(struct tog_view *view)
710 const struct got_error *err = NULL;
712 view->begin_y = 0;
713 view->begin_x = view_split_begin_x(0);
714 view->nlines = LINES;
715 view->ncols = COLS - view->begin_x;
716 view->lines = LINES;
717 view->cols = COLS;
718 err = view_resize(view);
719 if (err)
720 return err;
722 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
723 return got_error_from_errno("mvwin");
725 return NULL;
728 static const struct got_error *
729 view_fullscreen(struct tog_view *view)
731 const struct got_error *err = NULL;
733 view->begin_x = 0;
734 view->begin_y = 0;
735 view->nlines = LINES;
736 view->ncols = COLS;
737 view->lines = LINES;
738 view->cols = COLS;
739 err = view_resize(view);
740 if (err)
741 return err;
743 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
744 return got_error_from_errno("mvwin");
746 return NULL;
749 static int
750 view_is_parent_view(struct tog_view *view)
752 return view->parent == NULL;
755 static const struct got_error *
756 view_resize(struct tog_view *view)
758 int nlines, ncols;
760 if (view->lines > LINES)
761 nlines = view->nlines - (view->lines - LINES);
762 else
763 nlines = view->nlines + (LINES - view->lines);
765 if (view->cols > COLS)
766 ncols = view->ncols - (view->cols - COLS);
767 else
768 ncols = view->ncols + (COLS - view->cols);
770 if (wresize(view->window, nlines, ncols) == ERR)
771 return got_error_from_errno("wresize");
772 if (replace_panel(view->panel, view->window) == ERR)
773 return got_error_from_errno("replace_panel");
774 wclear(view->window);
776 view->nlines = nlines;
777 view->ncols = ncols;
778 view->lines = LINES;
779 view->cols = COLS;
781 if (view->child) {
782 view->child->begin_x = view_split_begin_x(view->begin_x);
783 if (view->child->begin_x == 0) {
784 view_fullscreen(view->child);
785 if (view->child->focussed)
786 show_panel(view->child->panel);
787 else
788 show_panel(view->panel);
789 } else {
790 view_splitscreen(view->child);
791 show_panel(view->child->panel);
795 return NULL;
798 static const struct got_error *
799 view_close_child(struct tog_view *view)
801 const struct got_error *err = NULL;
803 if (view->child == NULL)
804 return NULL;
806 err = view_close(view->child);
807 view->child = NULL;
808 return err;
811 static void
812 view_set_child(struct tog_view *view, struct tog_view *child)
814 view->child = child;
815 child->parent = view;
818 static int
819 view_is_splitscreen(struct tog_view *view)
821 return view->begin_x > 0;
824 static void
825 tog_resizeterm(void)
827 int cols, lines;
828 struct winsize size;
830 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
831 cols = 80; /* Default */
832 lines = 24;
833 } else {
834 cols = size.ws_col;
835 lines = size.ws_row;
837 resize_term(lines, cols);
840 static const struct got_error *
841 view_search_start(struct tog_view *view)
843 const struct got_error *err = NULL;
844 char pattern[1024];
845 int ret;
847 if (view->search_started) {
848 regfree(&view->regex);
849 view->searching = 0;
850 memset(&view->regmatch, 0, sizeof(view->regmatch));
852 view->search_started = 0;
854 if (view->nlines < 1)
855 return NULL;
857 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
858 wclrtoeol(view->window);
860 nocbreak();
861 echo();
862 ret = wgetnstr(view->window, pattern, sizeof(pattern));
863 cbreak();
864 noecho();
865 if (ret == ERR)
866 return NULL;
868 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
869 err = view->search_start(view);
870 if (err) {
871 regfree(&view->regex);
872 return err;
874 view->search_started = 1;
875 view->searching = TOG_SEARCH_FORWARD;
876 view->search_next_done = 0;
877 view->search_next(view);
880 return NULL;
883 static const struct got_error *
884 view_input(struct tog_view **new, int *done, struct tog_view *view,
885 struct tog_view_list_head *views)
887 const struct got_error *err = NULL;
888 struct tog_view *v;
889 int ch, errcode;
891 *new = NULL;
893 /* Clear "no matches" indicator. */
894 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
895 view->search_next_done == TOG_SEARCH_HAVE_NONE)
896 view->search_next_done = TOG_SEARCH_HAVE_MORE;
898 if (view->searching && !view->search_next_done) {
899 errcode = pthread_mutex_unlock(&tog_mutex);
900 if (errcode)
901 return got_error_set_errno(errcode,
902 "pthread_mutex_unlock");
903 sched_yield();
904 errcode = pthread_mutex_lock(&tog_mutex);
905 if (errcode)
906 return got_error_set_errno(errcode,
907 "pthread_mutex_lock");
908 view->search_next(view);
909 return NULL;
912 nodelay(stdscr, FALSE);
913 /* Allow threads to make progress while we are waiting for input. */
914 errcode = pthread_mutex_unlock(&tog_mutex);
915 if (errcode)
916 return got_error_set_errno(errcode, "pthread_mutex_unlock");
917 ch = wgetch(view->window);
918 errcode = pthread_mutex_lock(&tog_mutex);
919 if (errcode)
920 return got_error_set_errno(errcode, "pthread_mutex_lock");
921 nodelay(stdscr, TRUE);
923 if (tog_sigwinch_received || tog_sigcont_received) {
924 tog_resizeterm();
925 tog_sigwinch_received = 0;
926 tog_sigcont_received = 0;
927 TAILQ_FOREACH(v, views, entry) {
928 err = view_resize(v);
929 if (err)
930 return err;
931 err = v->input(new, v, KEY_RESIZE);
932 if (err)
933 return err;
934 if (v->child) {
935 err = view_resize(v->child);
936 if (err)
937 return err;
938 err = v->child->input(new, v->child,
939 KEY_RESIZE);
940 if (err)
941 return err;
946 switch (ch) {
947 case '\t':
948 if (view->child) {
949 view->focussed = 0;
950 view->child->focussed = 1;
951 view->focus_child = 1;
952 } else if (view->parent) {
953 view->focussed = 0;
954 view->parent->focussed = 1;
955 view->parent->focus_child = 0;
957 break;
958 case 'q':
959 err = view->input(new, view, ch);
960 view->dying = 1;
961 break;
962 case 'Q':
963 *done = 1;
964 break;
965 case 'f':
966 if (view_is_parent_view(view)) {
967 if (view->child == NULL)
968 break;
969 if (view_is_splitscreen(view->child)) {
970 view->focussed = 0;
971 view->child->focussed = 1;
972 err = view_fullscreen(view->child);
973 } else
974 err = view_splitscreen(view->child);
975 if (err)
976 break;
977 err = view->child->input(new, view->child,
978 KEY_RESIZE);
979 } else {
980 if (view_is_splitscreen(view)) {
981 view->parent->focussed = 0;
982 view->focussed = 1;
983 err = view_fullscreen(view);
984 } else {
985 err = view_splitscreen(view);
987 if (err)
988 break;
989 err = view->input(new, view, KEY_RESIZE);
991 break;
992 case KEY_RESIZE:
993 break;
994 case '/':
995 if (view->search_start)
996 view_search_start(view);
997 else
998 err = view->input(new, view, ch);
999 break;
1000 case 'N':
1001 case 'n':
1002 if (view->search_started && view->search_next) {
1003 view->searching = (ch == 'n' ?
1004 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1005 view->search_next_done = 0;
1006 view->search_next(view);
1007 } else
1008 err = view->input(new, view, ch);
1009 break;
1010 default:
1011 err = view->input(new, view, ch);
1012 break;
1015 return err;
1018 void
1019 view_vborder(struct tog_view *view)
1021 PANEL *panel;
1022 const struct tog_view *view_above;
1024 if (view->parent)
1025 return view_vborder(view->parent);
1027 panel = panel_above(view->panel);
1028 if (panel == NULL)
1029 return;
1031 view_above = panel_userptr(panel);
1032 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1033 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1036 int
1037 view_needs_focus_indication(struct tog_view *view)
1039 if (view_is_parent_view(view)) {
1040 if (view->child == NULL || view->child->focussed)
1041 return 0;
1042 if (!view_is_splitscreen(view->child))
1043 return 0;
1044 } else if (!view_is_splitscreen(view))
1045 return 0;
1047 return view->focussed;
1050 static const struct got_error *
1051 view_loop(struct tog_view *view)
1053 const struct got_error *err = NULL;
1054 struct tog_view_list_head views;
1055 struct tog_view *new_view;
1056 int fast_refresh = 10;
1057 int done = 0, errcode;
1059 errcode = pthread_mutex_lock(&tog_mutex);
1060 if (errcode)
1061 return got_error_set_errno(errcode, "pthread_mutex_lock");
1063 TAILQ_INIT(&views);
1064 TAILQ_INSERT_HEAD(&views, view, entry);
1066 view->focussed = 1;
1067 err = view->show(view);
1068 if (err)
1069 return err;
1070 update_panels();
1071 doupdate();
1072 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1073 /* Refresh fast during initialization, then become slower. */
1074 if (fast_refresh && fast_refresh-- == 0)
1075 halfdelay(10); /* switch to once per second */
1077 err = view_input(&new_view, &done, view, &views);
1078 if (err)
1079 break;
1080 if (view->dying) {
1081 struct tog_view *v, *prev = NULL;
1083 if (view_is_parent_view(view))
1084 prev = TAILQ_PREV(view, tog_view_list_head,
1085 entry);
1086 else if (view->parent)
1087 prev = view->parent;
1089 if (view->parent) {
1090 view->parent->child = NULL;
1091 view->parent->focus_child = 0;
1092 } else
1093 TAILQ_REMOVE(&views, view, entry);
1095 err = view_close(view);
1096 if (err)
1097 goto done;
1099 view = NULL;
1100 TAILQ_FOREACH(v, &views, entry) {
1101 if (v->focussed)
1102 break;
1104 if (view == NULL && new_view == NULL) {
1105 /* No view has focus. Try to pick one. */
1106 if (prev)
1107 view = prev;
1108 else if (!TAILQ_EMPTY(&views)) {
1109 view = TAILQ_LAST(&views,
1110 tog_view_list_head);
1112 if (view) {
1113 if (view->focus_child) {
1114 view->child->focussed = 1;
1115 view = view->child;
1116 } else
1117 view->focussed = 1;
1121 if (new_view) {
1122 struct tog_view *v, *t;
1123 /* Only allow one parent view per type. */
1124 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1125 if (v->type != new_view->type)
1126 continue;
1127 TAILQ_REMOVE(&views, v, entry);
1128 err = view_close(v);
1129 if (err)
1130 goto done;
1131 break;
1133 TAILQ_INSERT_TAIL(&views, new_view, entry);
1134 view = new_view;
1136 if (view) {
1137 if (view_is_parent_view(view)) {
1138 if (view->child && view->child->focussed)
1139 view = view->child;
1140 } else {
1141 if (view->parent && view->parent->focussed)
1142 view = view->parent;
1144 show_panel(view->panel);
1145 if (view->child && view_is_splitscreen(view->child))
1146 show_panel(view->child->panel);
1147 if (view->parent && view_is_splitscreen(view)) {
1148 err = view->parent->show(view->parent);
1149 if (err)
1150 goto done;
1152 err = view->show(view);
1153 if (err)
1154 goto done;
1155 if (view->child) {
1156 err = view->child->show(view->child);
1157 if (err)
1158 goto done;
1160 update_panels();
1161 doupdate();
1164 done:
1165 while (!TAILQ_EMPTY(&views)) {
1166 view = TAILQ_FIRST(&views);
1167 TAILQ_REMOVE(&views, view, entry);
1168 view_close(view);
1171 errcode = pthread_mutex_unlock(&tog_mutex);
1172 if (errcode && err == NULL)
1173 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1175 return err;
1178 __dead static void
1179 usage_log(void)
1181 endwin();
1182 fprintf(stderr,
1183 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1184 getprogname());
1185 exit(1);
1188 /* Create newly allocated wide-character string equivalent to a byte string. */
1189 static const struct got_error *
1190 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1192 char *vis = NULL;
1193 const struct got_error *err = NULL;
1195 *ws = NULL;
1196 *wlen = mbstowcs(NULL, s, 0);
1197 if (*wlen == (size_t)-1) {
1198 int vislen;
1199 if (errno != EILSEQ)
1200 return got_error_from_errno("mbstowcs");
1202 /* byte string invalid in current encoding; try to "fix" it */
1203 err = got_mbsavis(&vis, &vislen, s);
1204 if (err)
1205 return err;
1206 *wlen = mbstowcs(NULL, vis, 0);
1207 if (*wlen == (size_t)-1) {
1208 err = got_error_from_errno("mbstowcs"); /* give up */
1209 goto done;
1213 *ws = calloc(*wlen + 1, sizeof(**ws));
1214 if (*ws == NULL) {
1215 err = got_error_from_errno("calloc");
1216 goto done;
1219 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1220 err = got_error_from_errno("mbstowcs");
1221 done:
1222 free(vis);
1223 if (err) {
1224 free(*ws);
1225 *ws = NULL;
1226 *wlen = 0;
1228 return err;
1231 /* Format a line for display, ensuring that it won't overflow a width limit. */
1232 static const struct got_error *
1233 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1234 int col_tab_align)
1236 const struct got_error *err = NULL;
1237 int cols = 0;
1238 wchar_t *wline = NULL;
1239 size_t wlen;
1240 int i;
1242 *wlinep = NULL;
1243 *widthp = 0;
1245 err = mbs2ws(&wline, &wlen, line);
1246 if (err)
1247 return err;
1249 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1250 wline[wlen - 1] = L'\0';
1251 wlen--;
1253 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1254 wline[wlen - 1] = L'\0';
1255 wlen--;
1258 i = 0;
1259 while (i < wlen) {
1260 int width = wcwidth(wline[i]);
1262 if (width == 0) {
1263 i++;
1264 continue;
1267 if (width == 1 || width == 2) {
1268 if (cols + width > wlimit)
1269 break;
1270 cols += width;
1271 i++;
1272 } else if (width == -1) {
1273 if (wline[i] == L'\t') {
1274 width = TABSIZE -
1275 ((cols + col_tab_align) % TABSIZE);
1276 } else {
1277 width = 1;
1278 wline[i] = L'.';
1280 if (cols + width > wlimit)
1281 break;
1282 cols += width;
1283 i++;
1284 } else {
1285 err = got_error_from_errno("wcwidth");
1286 goto done;
1289 wline[i] = L'\0';
1290 if (widthp)
1291 *widthp = cols;
1292 done:
1293 if (err)
1294 free(wline);
1295 else
1296 *wlinep = wline;
1297 return err;
1300 static const struct got_error*
1301 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1302 struct got_object_id *id, struct got_repository *repo)
1304 static const struct got_error *err = NULL;
1305 struct got_reflist_entry *re;
1306 char *s;
1307 const char *name;
1309 *refs_str = NULL;
1311 TAILQ_FOREACH(re, refs, entry) {
1312 struct got_tag_object *tag = NULL;
1313 struct got_object_id *ref_id;
1314 int cmp;
1316 name = got_ref_get_name(re->ref);
1317 if (strcmp(name, GOT_REF_HEAD) == 0)
1318 continue;
1319 if (strncmp(name, "refs/", 5) == 0)
1320 name += 5;
1321 if (strncmp(name, "got/", 4) == 0 &&
1322 strncmp(name, "got/backup/", 11) != 0)
1323 continue;
1324 if (strncmp(name, "heads/", 6) == 0)
1325 name += 6;
1326 if (strncmp(name, "remotes/", 8) == 0) {
1327 name += 8;
1328 s = strstr(name, "/" GOT_REF_HEAD);
1329 if (s != NULL && s[strlen(s)] == '\0')
1330 continue;
1332 err = got_ref_resolve(&ref_id, repo, re->ref);
1333 if (err)
1334 break;
1335 if (strncmp(name, "tags/", 5) == 0) {
1336 err = got_object_open_as_tag(&tag, repo, ref_id);
1337 if (err) {
1338 if (err->code != GOT_ERR_OBJ_TYPE) {
1339 free(ref_id);
1340 break;
1342 /* Ref points at something other than a tag. */
1343 err = NULL;
1344 tag = NULL;
1347 cmp = got_object_id_cmp(tag ?
1348 got_object_tag_get_object_id(tag) : ref_id, id);
1349 free(ref_id);
1350 if (tag)
1351 got_object_tag_close(tag);
1352 if (cmp != 0)
1353 continue;
1354 s = *refs_str;
1355 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1356 s ? ", " : "", name) == -1) {
1357 err = got_error_from_errno("asprintf");
1358 free(s);
1359 *refs_str = NULL;
1360 break;
1362 free(s);
1365 return err;
1368 static const struct got_error *
1369 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1370 int col_tab_align)
1372 char *smallerthan;
1374 smallerthan = strchr(author, '<');
1375 if (smallerthan && smallerthan[1] != '\0')
1376 author = smallerthan + 1;
1377 author[strcspn(author, "@>")] = '\0';
1378 return format_line(wauthor, author_width, author, limit, col_tab_align);
1381 static const struct got_error *
1382 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1383 struct got_object_id *id, const size_t date_display_cols,
1384 int author_display_cols)
1386 struct tog_log_view_state *s = &view->state.log;
1387 const struct got_error *err = NULL;
1388 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1389 char *logmsg0 = NULL, *logmsg = NULL;
1390 char *author = NULL;
1391 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1392 int author_width, logmsg_width;
1393 char *newline, *line = NULL;
1394 int col, limit;
1395 const int avail = view->ncols;
1396 struct tm tm;
1397 time_t committer_time;
1398 struct tog_color *tc;
1400 committer_time = got_object_commit_get_committer_time(commit);
1401 if (gmtime_r(&committer_time, &tm) == NULL)
1402 return got_error_from_errno("gmtime_r");
1403 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1404 return got_error(GOT_ERR_NO_SPACE);
1406 if (avail <= date_display_cols)
1407 limit = MIN(sizeof(datebuf) - 1, avail);
1408 else
1409 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1410 tc = get_color(&s->colors, TOG_COLOR_DATE);
1411 if (tc)
1412 wattr_on(view->window,
1413 COLOR_PAIR(tc->colorpair), NULL);
1414 waddnstr(view->window, datebuf, limit);
1415 if (tc)
1416 wattr_off(view->window,
1417 COLOR_PAIR(tc->colorpair), NULL);
1418 col = limit;
1419 if (col > avail)
1420 goto done;
1422 if (avail >= 120) {
1423 char *id_str;
1424 err = got_object_id_str(&id_str, id);
1425 if (err)
1426 goto done;
1427 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1428 if (tc)
1429 wattr_on(view->window,
1430 COLOR_PAIR(tc->colorpair), NULL);
1431 wprintw(view->window, "%.8s ", id_str);
1432 if (tc)
1433 wattr_off(view->window,
1434 COLOR_PAIR(tc->colorpair), NULL);
1435 free(id_str);
1436 col += 9;
1437 if (col > avail)
1438 goto done;
1441 author = strdup(got_object_commit_get_author(commit));
1442 if (author == NULL) {
1443 err = got_error_from_errno("strdup");
1444 goto done;
1446 err = format_author(&wauthor, &author_width, author, avail - col, col);
1447 if (err)
1448 goto done;
1449 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1450 if (tc)
1451 wattr_on(view->window,
1452 COLOR_PAIR(tc->colorpair), NULL);
1453 waddwstr(view->window, wauthor);
1454 if (tc)
1455 wattr_off(view->window,
1456 COLOR_PAIR(tc->colorpair), NULL);
1457 col += author_width;
1458 while (col < avail && author_width < author_display_cols + 2) {
1459 waddch(view->window, ' ');
1460 col++;
1461 author_width++;
1463 if (col > avail)
1464 goto done;
1466 err = got_object_commit_get_logmsg(&logmsg0, commit);
1467 if (err)
1468 goto done;
1469 logmsg = logmsg0;
1470 while (*logmsg == '\n')
1471 logmsg++;
1472 newline = strchr(logmsg, '\n');
1473 if (newline)
1474 *newline = '\0';
1475 limit = avail - col;
1476 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1477 if (err)
1478 goto done;
1479 waddwstr(view->window, wlogmsg);
1480 col += logmsg_width;
1481 while (col < avail) {
1482 waddch(view->window, ' ');
1483 col++;
1485 done:
1486 free(logmsg0);
1487 free(wlogmsg);
1488 free(author);
1489 free(wauthor);
1490 free(line);
1491 return err;
1494 static struct commit_queue_entry *
1495 alloc_commit_queue_entry(struct got_commit_object *commit,
1496 struct got_object_id *id)
1498 struct commit_queue_entry *entry;
1500 entry = calloc(1, sizeof(*entry));
1501 if (entry == NULL)
1502 return NULL;
1504 entry->id = id;
1505 entry->commit = commit;
1506 return entry;
1509 static void
1510 pop_commit(struct commit_queue *commits)
1512 struct commit_queue_entry *entry;
1514 entry = TAILQ_FIRST(&commits->head);
1515 TAILQ_REMOVE(&commits->head, entry, entry);
1516 got_object_commit_close(entry->commit);
1517 commits->ncommits--;
1518 /* Don't free entry->id! It is owned by the commit graph. */
1519 free(entry);
1522 static void
1523 free_commits(struct commit_queue *commits)
1525 while (!TAILQ_EMPTY(&commits->head))
1526 pop_commit(commits);
1529 static const struct got_error *
1530 match_commit(int *have_match, struct got_object_id *id,
1531 struct got_commit_object *commit, regex_t *regex)
1533 const struct got_error *err = NULL;
1534 regmatch_t regmatch;
1535 char *id_str = NULL, *logmsg = NULL;
1537 *have_match = 0;
1539 err = got_object_id_str(&id_str, id);
1540 if (err)
1541 return err;
1543 err = got_object_commit_get_logmsg(&logmsg, commit);
1544 if (err)
1545 goto done;
1547 if (regexec(regex, got_object_commit_get_author(commit), 1,
1548 &regmatch, 0) == 0 ||
1549 regexec(regex, got_object_commit_get_committer(commit), 1,
1550 &regmatch, 0) == 0 ||
1551 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1552 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1553 *have_match = 1;
1554 done:
1555 free(id_str);
1556 free(logmsg);
1557 return err;
1560 static const struct got_error *
1561 queue_commits(struct tog_log_thread_args *a)
1563 const struct got_error *err = NULL;
1566 * We keep all commits open throughout the lifetime of the log
1567 * view in order to avoid having to re-fetch commits from disk
1568 * while updating the display.
1570 do {
1571 struct got_object_id *id;
1572 struct got_commit_object *commit;
1573 struct commit_queue_entry *entry;
1574 int errcode;
1576 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1577 NULL, NULL);
1578 if (err || id == NULL)
1579 break;
1581 err = got_object_open_as_commit(&commit, a->repo, id);
1582 if (err)
1583 break;
1584 entry = alloc_commit_queue_entry(commit, id);
1585 if (entry == NULL) {
1586 err = got_error_from_errno("alloc_commit_queue_entry");
1587 break;
1590 errcode = pthread_mutex_lock(&tog_mutex);
1591 if (errcode) {
1592 err = got_error_set_errno(errcode,
1593 "pthread_mutex_lock");
1594 break;
1597 entry->idx = a->commits->ncommits;
1598 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1599 a->commits->ncommits++;
1601 if (*a->searching == TOG_SEARCH_FORWARD &&
1602 !*a->search_next_done) {
1603 int have_match;
1604 err = match_commit(&have_match, id, commit, a->regex);
1605 if (err)
1606 break;
1607 if (have_match)
1608 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1611 errcode = pthread_mutex_unlock(&tog_mutex);
1612 if (errcode && err == NULL)
1613 err = got_error_set_errno(errcode,
1614 "pthread_mutex_unlock");
1615 if (err)
1616 break;
1617 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1619 return err;
1622 static void
1623 select_commit(struct tog_log_view_state *s)
1625 struct commit_queue_entry *entry;
1626 int ncommits = 0;
1628 entry = s->first_displayed_entry;
1629 while (entry) {
1630 if (ncommits == s->selected) {
1631 s->selected_entry = entry;
1632 break;
1634 entry = TAILQ_NEXT(entry, entry);
1635 ncommits++;
1639 static const struct got_error *
1640 draw_commits(struct tog_view *view)
1642 const struct got_error *err = NULL;
1643 struct tog_log_view_state *s = &view->state.log;
1644 struct commit_queue_entry *entry = s->selected_entry;
1645 const int limit = view->nlines;
1646 int width;
1647 int ncommits, author_cols = 4;
1648 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1649 char *refs_str = NULL;
1650 wchar_t *wline;
1651 struct tog_color *tc;
1652 static const size_t date_display_cols = 12;
1654 if (s->selected_entry &&
1655 !(view->searching && view->search_next_done == 0)) {
1656 struct got_reflist_head *refs;
1657 err = got_object_id_str(&id_str, s->selected_entry->id);
1658 if (err)
1659 return err;
1660 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1661 s->selected_entry->id);
1662 if (refs) {
1663 err = build_refs_str(&refs_str, refs,
1664 s->selected_entry->id, s->repo);
1665 if (err)
1666 goto done;
1670 if (s->thread_args.commits_needed == 0)
1671 halfdelay(10); /* disable fast refresh */
1673 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1674 if (asprintf(&ncommits_str, " [%d/%d] %s",
1675 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1676 (view->searching && !view->search_next_done) ?
1677 "searching..." : "loading...") == -1) {
1678 err = got_error_from_errno("asprintf");
1679 goto done;
1681 } else {
1682 const char *search_str = NULL;
1684 if (view->searching) {
1685 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1686 search_str = "no more matches";
1687 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1688 search_str = "no matches found";
1689 else if (!view->search_next_done)
1690 search_str = "searching...";
1693 if (asprintf(&ncommits_str, " [%d/%d] %s",
1694 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1695 search_str ? search_str :
1696 (refs_str ? refs_str : "")) == -1) {
1697 err = got_error_from_errno("asprintf");
1698 goto done;
1702 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1703 if (asprintf(&header, "commit %s %s%s",
1704 id_str ? id_str : "........................................",
1705 s->in_repo_path, ncommits_str) == -1) {
1706 err = got_error_from_errno("asprintf");
1707 header = NULL;
1708 goto done;
1710 } else if (asprintf(&header, "commit %s%s",
1711 id_str ? id_str : "........................................",
1712 ncommits_str) == -1) {
1713 err = got_error_from_errno("asprintf");
1714 header = NULL;
1715 goto done;
1717 err = format_line(&wline, &width, header, view->ncols, 0);
1718 if (err)
1719 goto done;
1721 werase(view->window);
1723 if (view_needs_focus_indication(view))
1724 wstandout(view->window);
1725 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1726 if (tc)
1727 wattr_on(view->window,
1728 COLOR_PAIR(tc->colorpair), NULL);
1729 waddwstr(view->window, wline);
1730 if (tc)
1731 wattr_off(view->window,
1732 COLOR_PAIR(tc->colorpair), NULL);
1733 while (width < view->ncols) {
1734 waddch(view->window, ' ');
1735 width++;
1737 if (view_needs_focus_indication(view))
1738 wstandend(view->window);
1739 free(wline);
1740 if (limit <= 1)
1741 goto done;
1743 /* Grow author column size if necessary. */
1744 entry = s->first_displayed_entry;
1745 ncommits = 0;
1746 while (entry) {
1747 char *author;
1748 wchar_t *wauthor;
1749 int width;
1750 if (ncommits >= limit - 1)
1751 break;
1752 author = strdup(got_object_commit_get_author(entry->commit));
1753 if (author == NULL) {
1754 err = got_error_from_errno("strdup");
1755 goto done;
1757 err = format_author(&wauthor, &width, author, COLS,
1758 date_display_cols);
1759 if (author_cols < width)
1760 author_cols = width;
1761 free(wauthor);
1762 free(author);
1763 ncommits++;
1764 entry = TAILQ_NEXT(entry, entry);
1767 entry = s->first_displayed_entry;
1768 s->last_displayed_entry = s->first_displayed_entry;
1769 ncommits = 0;
1770 while (entry) {
1771 if (ncommits >= limit - 1)
1772 break;
1773 if (ncommits == s->selected)
1774 wstandout(view->window);
1775 err = draw_commit(view, entry->commit, entry->id,
1776 date_display_cols, author_cols);
1777 if (ncommits == s->selected)
1778 wstandend(view->window);
1779 if (err)
1780 goto done;
1781 ncommits++;
1782 s->last_displayed_entry = entry;
1783 entry = TAILQ_NEXT(entry, entry);
1786 view_vborder(view);
1787 update_panels();
1788 doupdate();
1789 done:
1790 free(id_str);
1791 free(refs_str);
1792 free(ncommits_str);
1793 free(header);
1794 return err;
1797 static void
1798 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1800 struct commit_queue_entry *entry;
1801 int nscrolled = 0;
1803 entry = TAILQ_FIRST(&s->commits.head);
1804 if (s->first_displayed_entry == entry)
1805 return;
1807 entry = s->first_displayed_entry;
1808 while (entry && nscrolled < maxscroll) {
1809 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1810 if (entry) {
1811 s->first_displayed_entry = entry;
1812 nscrolled++;
1817 static const struct got_error *
1818 trigger_log_thread(struct tog_view *view, int wait)
1820 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1821 int errcode;
1823 halfdelay(1); /* fast refresh while loading commits */
1825 while (ta->commits_needed > 0 || ta->load_all) {
1826 if (ta->log_complete)
1827 break;
1829 /* Wake the log thread. */
1830 errcode = pthread_cond_signal(&ta->need_commits);
1831 if (errcode)
1832 return got_error_set_errno(errcode,
1833 "pthread_cond_signal");
1836 * The mutex will be released while the view loop waits
1837 * in wgetch(), at which time the log thread will run.
1839 if (!wait)
1840 break;
1842 /* Display progress update in log view. */
1843 show_log_view(view);
1844 update_panels();
1845 doupdate();
1847 /* Wait right here while next commit is being loaded. */
1848 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1849 if (errcode)
1850 return got_error_set_errno(errcode,
1851 "pthread_cond_wait");
1853 /* Display progress update in log view. */
1854 show_log_view(view);
1855 update_panels();
1856 doupdate();
1859 return NULL;
1862 static const struct got_error *
1863 log_scroll_down(struct tog_view *view, int maxscroll)
1865 struct tog_log_view_state *s = &view->state.log;
1866 const struct got_error *err = NULL;
1867 struct commit_queue_entry *pentry;
1868 int nscrolled = 0, ncommits_needed;
1870 if (s->last_displayed_entry == NULL)
1871 return NULL;
1873 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1874 if (s->commits.ncommits < ncommits_needed &&
1875 !s->thread_args.log_complete) {
1877 * Ask the log thread for required amount of commits.
1879 s->thread_args.commits_needed += maxscroll;
1880 err = trigger_log_thread(view, 1);
1881 if (err)
1882 return err;
1885 do {
1886 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1887 if (pentry == NULL)
1888 break;
1890 s->last_displayed_entry = pentry;
1892 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1893 if (pentry == NULL)
1894 break;
1895 s->first_displayed_entry = pentry;
1896 } while (++nscrolled < maxscroll);
1898 return err;
1901 static const struct got_error *
1902 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1903 struct got_commit_object *commit, struct got_object_id *commit_id,
1904 struct tog_view *log_view, struct got_repository *repo)
1906 const struct got_error *err;
1907 struct got_object_qid *parent_id;
1908 struct tog_view *diff_view;
1910 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1911 if (diff_view == NULL)
1912 return got_error_from_errno("view_open");
1914 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1915 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1916 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1917 if (err == NULL)
1918 *new_view = diff_view;
1919 return err;
1922 static const struct got_error *
1923 tree_view_visit_subtree(struct tog_tree_view_state *s,
1924 struct got_tree_object *subtree)
1926 struct tog_parent_tree *parent;
1928 parent = calloc(1, sizeof(*parent));
1929 if (parent == NULL)
1930 return got_error_from_errno("calloc");
1932 parent->tree = s->tree;
1933 parent->first_displayed_entry = s->first_displayed_entry;
1934 parent->selected_entry = s->selected_entry;
1935 parent->selected = s->selected;
1936 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1937 s->tree = subtree;
1938 s->selected = 0;
1939 s->first_displayed_entry = NULL;
1940 return NULL;
1943 static const struct got_error *
1944 tree_view_walk_path(struct tog_tree_view_state *s,
1945 struct got_commit_object *commit, const char *path)
1947 const struct got_error *err = NULL;
1948 struct got_tree_object *tree = NULL;
1949 const char *p;
1950 char *slash, *subpath = NULL;
1952 /* Walk the path and open corresponding tree objects. */
1953 p = path;
1954 while (*p) {
1955 struct got_tree_entry *te;
1956 struct got_object_id *tree_id;
1957 char *te_name;
1959 while (p[0] == '/')
1960 p++;
1962 /* Ensure the correct subtree entry is selected. */
1963 slash = strchr(p, '/');
1964 if (slash == NULL)
1965 te_name = strdup(p);
1966 else
1967 te_name = strndup(p, slash - p);
1968 if (te_name == NULL) {
1969 err = got_error_from_errno("strndup");
1970 break;
1972 te = got_object_tree_find_entry(s->tree, te_name);
1973 if (te == NULL) {
1974 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1975 free(te_name);
1976 break;
1978 free(te_name);
1979 s->first_displayed_entry = s->selected_entry = te;
1981 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1982 break; /* jump to this file's entry */
1984 slash = strchr(p, '/');
1985 if (slash)
1986 subpath = strndup(path, slash - path);
1987 else
1988 subpath = strdup(path);
1989 if (subpath == NULL) {
1990 err = got_error_from_errno("strdup");
1991 break;
1994 err = got_object_id_by_path(&tree_id, s->repo, commit,
1995 subpath);
1996 if (err)
1997 break;
1999 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2000 free(tree_id);
2001 if (err)
2002 break;
2004 err = tree_view_visit_subtree(s, tree);
2005 if (err) {
2006 got_object_tree_close(tree);
2007 break;
2009 if (slash == NULL)
2010 break;
2011 free(subpath);
2012 subpath = NULL;
2013 p = slash;
2016 free(subpath);
2017 return err;
2020 static const struct got_error *
2021 browse_commit_tree(struct tog_view **new_view, int begin_x,
2022 struct commit_queue_entry *entry, const char *path,
2023 const char *head_ref_name, struct got_repository *repo)
2025 const struct got_error *err = NULL;
2026 struct tog_tree_view_state *s;
2027 struct tog_view *tree_view;
2029 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2030 if (tree_view == NULL)
2031 return got_error_from_errno("view_open");
2033 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2034 if (err)
2035 return err;
2036 s = &tree_view->state.tree;
2038 *new_view = tree_view;
2040 if (got_path_is_root_dir(path))
2041 return NULL;
2043 return tree_view_walk_path(s, entry->commit, path);
2046 static const struct got_error *
2047 block_signals_used_by_main_thread(void)
2049 sigset_t sigset;
2050 int errcode;
2052 if (sigemptyset(&sigset) == -1)
2053 return got_error_from_errno("sigemptyset");
2055 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2056 if (sigaddset(&sigset, SIGWINCH) == -1)
2057 return got_error_from_errno("sigaddset");
2058 if (sigaddset(&sigset, SIGCONT) == -1)
2059 return got_error_from_errno("sigaddset");
2060 if (sigaddset(&sigset, SIGINT) == -1)
2061 return got_error_from_errno("sigaddset");
2062 if (sigaddset(&sigset, SIGTERM) == -1)
2063 return got_error_from_errno("sigaddset");
2065 /* ncurses handles SIGTSTP */
2066 if (sigaddset(&sigset, SIGTSTP) == -1)
2067 return got_error_from_errno("sigaddset");
2069 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2070 if (errcode)
2071 return got_error_set_errno(errcode, "pthread_sigmask");
2073 return NULL;
2076 static void *
2077 log_thread(void *arg)
2079 const struct got_error *err = NULL;
2080 int errcode = 0;
2081 struct tog_log_thread_args *a = arg;
2082 int done = 0;
2084 err = block_signals_used_by_main_thread();
2085 if (err)
2086 return (void *)err;
2088 while (!done && !err && !tog_fatal_signal_received()) {
2089 err = queue_commits(a);
2090 if (err) {
2091 if (err->code != GOT_ERR_ITER_COMPLETED)
2092 return (void *)err;
2093 err = NULL;
2094 done = 1;
2095 } else if (a->commits_needed > 0 && !a->load_all)
2096 a->commits_needed--;
2098 errcode = pthread_mutex_lock(&tog_mutex);
2099 if (errcode) {
2100 err = got_error_set_errno(errcode,
2101 "pthread_mutex_lock");
2102 break;
2103 } else if (*a->quit)
2104 done = 1;
2105 else if (*a->first_displayed_entry == NULL) {
2106 *a->first_displayed_entry =
2107 TAILQ_FIRST(&a->commits->head);
2108 *a->selected_entry = *a->first_displayed_entry;
2111 errcode = pthread_cond_signal(&a->commit_loaded);
2112 if (errcode) {
2113 err = got_error_set_errno(errcode,
2114 "pthread_cond_signal");
2115 pthread_mutex_unlock(&tog_mutex);
2116 break;
2119 if (done)
2120 a->commits_needed = 0;
2121 else {
2122 if (a->commits_needed == 0 && !a->load_all) {
2123 errcode = pthread_cond_wait(&a->need_commits,
2124 &tog_mutex);
2125 if (errcode)
2126 err = got_error_set_errno(errcode,
2127 "pthread_cond_wait");
2128 if (*a->quit)
2129 done = 1;
2133 errcode = pthread_mutex_unlock(&tog_mutex);
2134 if (errcode && err == NULL)
2135 err = got_error_set_errno(errcode,
2136 "pthread_mutex_unlock");
2138 a->log_complete = 1;
2139 return (void *)err;
2142 static const struct got_error *
2143 stop_log_thread(struct tog_log_view_state *s)
2145 const struct got_error *err = NULL;
2146 int errcode;
2148 if (s->thread) {
2149 s->quit = 1;
2150 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2151 if (errcode)
2152 return got_error_set_errno(errcode,
2153 "pthread_cond_signal");
2154 errcode = pthread_mutex_unlock(&tog_mutex);
2155 if (errcode)
2156 return got_error_set_errno(errcode,
2157 "pthread_mutex_unlock");
2158 errcode = pthread_join(s->thread, (void **)&err);
2159 if (errcode)
2160 return got_error_set_errno(errcode, "pthread_join");
2161 errcode = pthread_mutex_lock(&tog_mutex);
2162 if (errcode)
2163 return got_error_set_errno(errcode,
2164 "pthread_mutex_lock");
2165 s->thread = 0; //NULL;
2168 if (s->thread_args.repo) {
2169 err = got_repo_close(s->thread_args.repo);
2170 s->thread_args.repo = NULL;
2173 if (s->thread_args.graph) {
2174 got_commit_graph_close(s->thread_args.graph);
2175 s->thread_args.graph = NULL;
2178 return err;
2181 static const struct got_error *
2182 close_log_view(struct tog_view *view)
2184 const struct got_error *err = NULL;
2185 struct tog_log_view_state *s = &view->state.log;
2186 int errcode;
2188 err = stop_log_thread(s);
2190 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2191 if (errcode && err == NULL)
2192 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2194 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2195 if (errcode && err == NULL)
2196 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2198 free_commits(&s->commits);
2199 free(s->in_repo_path);
2200 s->in_repo_path = NULL;
2201 free(s->start_id);
2202 s->start_id = NULL;
2203 free(s->head_ref_name);
2204 s->head_ref_name = NULL;
2205 return err;
2208 static const struct got_error *
2209 search_start_log_view(struct tog_view *view)
2211 struct tog_log_view_state *s = &view->state.log;
2213 s->matched_entry = NULL;
2214 s->search_entry = NULL;
2215 return NULL;
2218 static const struct got_error *
2219 search_next_log_view(struct tog_view *view)
2221 const struct got_error *err = NULL;
2222 struct tog_log_view_state *s = &view->state.log;
2223 struct commit_queue_entry *entry;
2225 /* Display progress update in log view. */
2226 show_log_view(view);
2227 update_panels();
2228 doupdate();
2230 if (s->search_entry) {
2231 int errcode, ch;
2232 errcode = pthread_mutex_unlock(&tog_mutex);
2233 if (errcode)
2234 return got_error_set_errno(errcode,
2235 "pthread_mutex_unlock");
2236 ch = wgetch(view->window);
2237 errcode = pthread_mutex_lock(&tog_mutex);
2238 if (errcode)
2239 return got_error_set_errno(errcode,
2240 "pthread_mutex_lock");
2241 if (ch == KEY_BACKSPACE) {
2242 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2243 return NULL;
2245 if (view->searching == TOG_SEARCH_FORWARD)
2246 entry = TAILQ_NEXT(s->search_entry, entry);
2247 else
2248 entry = TAILQ_PREV(s->search_entry,
2249 commit_queue_head, entry);
2250 } else if (s->matched_entry) {
2251 if (view->searching == TOG_SEARCH_FORWARD)
2252 entry = TAILQ_NEXT(s->matched_entry, entry);
2253 else
2254 entry = TAILQ_PREV(s->matched_entry,
2255 commit_queue_head, entry);
2256 } else {
2257 entry = s->selected_entry;
2260 while (1) {
2261 int have_match = 0;
2263 if (entry == NULL) {
2264 if (s->thread_args.log_complete ||
2265 view->searching == TOG_SEARCH_BACKWARD) {
2266 view->search_next_done =
2267 (s->matched_entry == NULL ?
2268 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2269 s->search_entry = NULL;
2270 return NULL;
2273 * Poke the log thread for more commits and return,
2274 * allowing the main loop to make progress. Search
2275 * will resume at s->search_entry once we come back.
2277 s->thread_args.commits_needed++;
2278 return trigger_log_thread(view, 0);
2281 err = match_commit(&have_match, entry->id, entry->commit,
2282 &view->regex);
2283 if (err)
2284 break;
2285 if (have_match) {
2286 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2287 s->matched_entry = entry;
2288 break;
2291 s->search_entry = entry;
2292 if (view->searching == TOG_SEARCH_FORWARD)
2293 entry = TAILQ_NEXT(entry, entry);
2294 else
2295 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2298 if (s->matched_entry) {
2299 int cur = s->selected_entry->idx;
2300 while (cur < s->matched_entry->idx) {
2301 err = input_log_view(NULL, view, KEY_DOWN);
2302 if (err)
2303 return err;
2304 cur++;
2306 while (cur > s->matched_entry->idx) {
2307 err = input_log_view(NULL, view, KEY_UP);
2308 if (err)
2309 return err;
2310 cur--;
2314 s->search_entry = NULL;
2316 return NULL;
2319 static const struct got_error *
2320 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2321 struct got_repository *repo, const char *head_ref_name,
2322 const char *in_repo_path, int log_branches)
2324 const struct got_error *err = NULL;
2325 struct tog_log_view_state *s = &view->state.log;
2326 struct got_repository *thread_repo = NULL;
2327 struct got_commit_graph *thread_graph = NULL;
2328 int errcode;
2330 if (in_repo_path != s->in_repo_path) {
2331 free(s->in_repo_path);
2332 s->in_repo_path = strdup(in_repo_path);
2333 if (s->in_repo_path == NULL)
2334 return got_error_from_errno("strdup");
2337 /* The commit queue only contains commits being displayed. */
2338 TAILQ_INIT(&s->commits.head);
2339 s->commits.ncommits = 0;
2341 s->repo = repo;
2342 if (head_ref_name) {
2343 s->head_ref_name = strdup(head_ref_name);
2344 if (s->head_ref_name == NULL) {
2345 err = got_error_from_errno("strdup");
2346 goto done;
2349 s->start_id = got_object_id_dup(start_id);
2350 if (s->start_id == NULL) {
2351 err = got_error_from_errno("got_object_id_dup");
2352 goto done;
2354 s->log_branches = log_branches;
2356 STAILQ_INIT(&s->colors);
2357 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2358 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2359 get_color_value("TOG_COLOR_COMMIT"));
2360 if (err)
2361 goto done;
2362 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2363 get_color_value("TOG_COLOR_AUTHOR"));
2364 if (err) {
2365 free_colors(&s->colors);
2366 goto done;
2368 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2369 get_color_value("TOG_COLOR_DATE"));
2370 if (err) {
2371 free_colors(&s->colors);
2372 goto done;
2376 view->show = show_log_view;
2377 view->input = input_log_view;
2378 view->close = close_log_view;
2379 view->search_start = search_start_log_view;
2380 view->search_next = search_next_log_view;
2382 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2383 if (err)
2384 goto done;
2385 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2386 !s->log_branches);
2387 if (err)
2388 goto done;
2389 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2390 s->repo, NULL, NULL);
2391 if (err)
2392 goto done;
2394 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2395 if (errcode) {
2396 err = got_error_set_errno(errcode, "pthread_cond_init");
2397 goto done;
2399 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2400 if (errcode) {
2401 err = got_error_set_errno(errcode, "pthread_cond_init");
2402 goto done;
2405 s->thread_args.commits_needed = view->nlines;
2406 s->thread_args.graph = thread_graph;
2407 s->thread_args.commits = &s->commits;
2408 s->thread_args.in_repo_path = s->in_repo_path;
2409 s->thread_args.start_id = s->start_id;
2410 s->thread_args.repo = thread_repo;
2411 s->thread_args.log_complete = 0;
2412 s->thread_args.quit = &s->quit;
2413 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2414 s->thread_args.selected_entry = &s->selected_entry;
2415 s->thread_args.searching = &view->searching;
2416 s->thread_args.search_next_done = &view->search_next_done;
2417 s->thread_args.regex = &view->regex;
2418 done:
2419 if (err)
2420 close_log_view(view);
2421 return err;
2424 static const struct got_error *
2425 show_log_view(struct tog_view *view)
2427 const struct got_error *err;
2428 struct tog_log_view_state *s = &view->state.log;
2430 if (s->thread == 0) { //NULL) {
2431 int errcode = pthread_create(&s->thread, NULL, log_thread,
2432 &s->thread_args);
2433 if (errcode)
2434 return got_error_set_errno(errcode, "pthread_create");
2435 if (s->thread_args.commits_needed > 0) {
2436 err = trigger_log_thread(view, 1);
2437 if (err)
2438 return err;
2442 return draw_commits(view);
2445 static const struct got_error *
2446 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2448 const struct got_error *err = NULL;
2449 struct tog_log_view_state *s = &view->state.log;
2450 struct tog_view *diff_view = NULL, *tree_view = NULL;
2451 struct tog_view *ref_view = NULL;
2452 struct commit_queue_entry *entry;
2453 int begin_x = 0, n, nscroll = view->nlines - 1;
2455 if (s->thread_args.load_all) {
2456 if (ch == KEY_BACKSPACE)
2457 s->thread_args.load_all = 0;
2458 else if (s->thread_args.log_complete) {
2459 s->thread_args.load_all = 0;
2460 log_scroll_down(view, s->commits.ncommits);
2461 s->selected = MIN(view->nlines - 2,
2462 s->commits.ncommits - 1);
2463 select_commit(s);
2465 return NULL;
2468 switch (ch) {
2469 case 'q':
2470 s->quit = 1;
2471 break;
2472 case 'k':
2473 case KEY_UP:
2474 case '<':
2475 case ',':
2476 case CTRL('p'):
2477 if (s->first_displayed_entry == NULL)
2478 break;
2479 if (s->selected > 0)
2480 s->selected--;
2481 else
2482 log_scroll_up(s, 1);
2483 select_commit(s);
2484 break;
2485 case 'g':
2486 case KEY_HOME:
2487 s->selected = 0;
2488 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2489 select_commit(s);
2490 break;
2491 case CTRL('u'):
2492 nscroll /= 2;
2493 /* FALL THROUGH */
2494 case KEY_PPAGE:
2495 case CTRL('b'):
2496 if (s->first_displayed_entry == NULL)
2497 break;
2498 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2499 s->selected = MAX(0, s->selected - nscroll - 1);
2500 else
2501 log_scroll_up(s, nscroll);
2502 select_commit(s);
2503 break;
2504 case 'j':
2505 case KEY_DOWN:
2506 case '>':
2507 case '.':
2508 case CTRL('n'):
2509 if (s->first_displayed_entry == NULL)
2510 break;
2511 if (s->selected < MIN(view->nlines - 2,
2512 s->commits.ncommits - 1))
2513 s->selected++;
2514 else {
2515 err = log_scroll_down(view, 1);
2516 if (err)
2517 break;
2519 select_commit(s);
2520 break;
2521 case 'G':
2522 case KEY_END: {
2523 /* We don't know yet how many commits, so we're forced to
2524 * traverse them all. */
2525 if (!s->thread_args.log_complete) {
2526 s->thread_args.load_all = 1;
2527 return trigger_log_thread(view, 0);
2530 s->selected = 0;
2531 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2532 for (n = 0; n < view->nlines - 1; n++) {
2533 if (entry == NULL)
2534 break;
2535 s->first_displayed_entry = entry;
2536 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2538 if (n > 0)
2539 s->selected = n - 1;
2540 select_commit(s);
2541 break;
2543 case CTRL('d'):
2544 nscroll /= 2;
2545 /* FALL THROUGH */
2546 case KEY_NPAGE:
2547 case CTRL('f'): {
2548 struct commit_queue_entry *first;
2549 first = s->first_displayed_entry;
2550 if (first == NULL)
2551 break;
2552 err = log_scroll_down(view, nscroll);
2553 if (err)
2554 break;
2555 if (first == s->first_displayed_entry &&
2556 s->selected < MIN(view->nlines - 2,
2557 s->commits.ncommits - 1)) {
2558 /* can't scroll further down */
2559 s->selected += MIN(s->last_displayed_entry->idx -
2560 s->selected_entry->idx, nscroll + 1);
2562 select_commit(s);
2563 break;
2565 case KEY_RESIZE:
2566 if (s->selected > view->nlines - 2)
2567 s->selected = view->nlines - 2;
2568 if (s->selected > s->commits.ncommits - 1)
2569 s->selected = s->commits.ncommits - 1;
2570 select_commit(s);
2571 if (s->commits.ncommits < view->nlines - 1 &&
2572 !s->thread_args.log_complete) {
2573 s->thread_args.commits_needed += (view->nlines - 1) -
2574 s->commits.ncommits;
2575 err = trigger_log_thread(view, 1);
2577 break;
2578 case KEY_ENTER:
2579 case ' ':
2580 case '\r':
2581 if (s->selected_entry == NULL)
2582 break;
2583 if (view_is_parent_view(view))
2584 begin_x = view_split_begin_x(view->begin_x);
2585 err = open_diff_view_for_commit(&diff_view, begin_x,
2586 s->selected_entry->commit, s->selected_entry->id,
2587 view, s->repo);
2588 if (err)
2589 break;
2590 view->focussed = 0;
2591 diff_view->focussed = 1;
2592 if (view_is_parent_view(view)) {
2593 err = view_close_child(view);
2594 if (err)
2595 return err;
2596 view_set_child(view, diff_view);
2597 view->focus_child = 1;
2598 } else
2599 *new_view = diff_view;
2600 break;
2601 case 't':
2602 if (s->selected_entry == NULL)
2603 break;
2604 if (view_is_parent_view(view))
2605 begin_x = view_split_begin_x(view->begin_x);
2606 err = browse_commit_tree(&tree_view, begin_x,
2607 s->selected_entry, s->in_repo_path, s->head_ref_name,
2608 s->repo);
2609 if (err)
2610 break;
2611 view->focussed = 0;
2612 tree_view->focussed = 1;
2613 if (view_is_parent_view(view)) {
2614 err = view_close_child(view);
2615 if (err)
2616 return err;
2617 view_set_child(view, tree_view);
2618 view->focus_child = 1;
2619 } else
2620 *new_view = tree_view;
2621 break;
2622 case KEY_BACKSPACE:
2623 case CTRL('l'):
2624 case 'B':
2625 if (ch == KEY_BACKSPACE &&
2626 got_path_is_root_dir(s->in_repo_path))
2627 break;
2628 err = stop_log_thread(s);
2629 if (err)
2630 return err;
2631 if (ch == KEY_BACKSPACE) {
2632 char *parent_path;
2633 err = got_path_dirname(&parent_path, s->in_repo_path);
2634 if (err)
2635 return err;
2636 free(s->in_repo_path);
2637 s->in_repo_path = parent_path;
2638 s->thread_args.in_repo_path = s->in_repo_path;
2639 } else if (ch == CTRL('l')) {
2640 struct got_object_id *start_id;
2641 err = got_repo_match_object_id(&start_id, NULL,
2642 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2643 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2644 if (err)
2645 return err;
2646 free(s->start_id);
2647 s->start_id = start_id;
2648 s->thread_args.start_id = s->start_id;
2649 } else /* 'B' */
2650 s->log_branches = !s->log_branches;
2652 err = got_repo_open(&s->thread_args.repo,
2653 got_repo_get_path(s->repo), NULL);
2654 if (err)
2655 return err;
2656 tog_free_refs();
2657 err = tog_load_refs(s->repo, 0);
2658 if (err)
2659 return err;
2660 err = got_commit_graph_open(&s->thread_args.graph,
2661 s->in_repo_path, !s->log_branches);
2662 if (err)
2663 return err;
2664 err = got_commit_graph_iter_start(s->thread_args.graph,
2665 s->start_id, s->repo, NULL, NULL);
2666 if (err)
2667 return err;
2668 free_commits(&s->commits);
2669 s->first_displayed_entry = NULL;
2670 s->last_displayed_entry = NULL;
2671 s->selected_entry = NULL;
2672 s->selected = 0;
2673 s->thread_args.log_complete = 0;
2674 s->quit = 0;
2675 s->thread_args.commits_needed = view->nlines;
2676 break;
2677 case 'r':
2678 if (view_is_parent_view(view))
2679 begin_x = view_split_begin_x(view->begin_x);
2680 ref_view = view_open(view->nlines, view->ncols,
2681 view->begin_y, begin_x, TOG_VIEW_REF);
2682 if (ref_view == NULL)
2683 return got_error_from_errno("view_open");
2684 err = open_ref_view(ref_view, s->repo);
2685 if (err) {
2686 view_close(ref_view);
2687 return err;
2689 view->focussed = 0;
2690 ref_view->focussed = 1;
2691 if (view_is_parent_view(view)) {
2692 err = view_close_child(view);
2693 if (err)
2694 return err;
2695 view_set_child(view, ref_view);
2696 view->focus_child = 1;
2697 } else
2698 *new_view = ref_view;
2699 break;
2700 default:
2701 break;
2704 return err;
2707 static const struct got_error *
2708 apply_unveil(const char *repo_path, const char *worktree_path)
2710 const struct got_error *error;
2712 #ifdef PROFILE
2713 if (unveil("gmon.out", "rwc") != 0)
2714 return got_error_from_errno2("unveil", "gmon.out");
2715 #endif
2716 if (repo_path && unveil(repo_path, "r") != 0)
2717 return got_error_from_errno2("unveil", repo_path);
2719 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2720 return got_error_from_errno2("unveil", worktree_path);
2722 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2723 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2725 error = got_privsep_unveil_exec_helpers();
2726 if (error != NULL)
2727 return error;
2729 if (unveil(NULL, NULL) != 0)
2730 return got_error_from_errno("unveil");
2732 return NULL;
2735 static void
2736 init_curses(void)
2739 * Override default signal handlers before starting ncurses.
2740 * This should prevent ncurses from installing its own
2741 * broken cleanup() signal handler.
2743 signal(SIGWINCH, tog_sigwinch);
2744 signal(SIGPIPE, tog_sigpipe);
2745 signal(SIGCONT, tog_sigcont);
2746 signal(SIGINT, tog_sigint);
2747 signal(SIGTERM, tog_sigterm);
2749 initscr();
2750 cbreak();
2751 halfdelay(1); /* Do fast refresh while initial view is loading. */
2752 noecho();
2753 nonl();
2754 intrflush(stdscr, FALSE);
2755 keypad(stdscr, TRUE);
2756 curs_set(0);
2757 if (getenv("TOG_COLORS") != NULL) {
2758 start_color();
2759 use_default_colors();
2763 static const struct got_error *
2764 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2765 struct got_repository *repo, struct got_worktree *worktree)
2767 const struct got_error *err = NULL;
2769 if (argc == 0) {
2770 *in_repo_path = strdup("/");
2771 if (*in_repo_path == NULL)
2772 return got_error_from_errno("strdup");
2773 return NULL;
2776 if (worktree) {
2777 const char *prefix = got_worktree_get_path_prefix(worktree);
2778 char *p;
2780 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2781 if (err)
2782 return err;
2783 if (asprintf(in_repo_path, "%s%s%s", prefix,
2784 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2785 p) == -1) {
2786 err = got_error_from_errno("asprintf");
2787 *in_repo_path = NULL;
2789 free(p);
2790 } else
2791 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2793 return err;
2796 static const struct got_error *
2797 cmd_log(int argc, char *argv[])
2799 const struct got_error *error;
2800 struct got_repository *repo = NULL;
2801 struct got_worktree *worktree = NULL;
2802 struct got_object_id *start_id = NULL;
2803 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2804 char *start_commit = NULL, *label = NULL;
2805 struct got_reference *ref = NULL;
2806 const char *head_ref_name = NULL;
2807 int ch, log_branches = 0;
2808 struct tog_view *view;
2810 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2811 switch (ch) {
2812 case 'b':
2813 log_branches = 1;
2814 break;
2815 case 'c':
2816 start_commit = optarg;
2817 break;
2818 case 'r':
2819 repo_path = realpath(optarg, NULL);
2820 if (repo_path == NULL)
2821 return got_error_from_errno2("realpath",
2822 optarg);
2823 break;
2824 default:
2825 usage_log();
2826 /* NOTREACHED */
2830 argc -= optind;
2831 argv += optind;
2833 if (argc > 1)
2834 usage_log();
2836 if (repo_path == NULL) {
2837 cwd = getcwd(NULL, 0);
2838 if (cwd == NULL)
2839 return got_error_from_errno("getcwd");
2840 error = got_worktree_open(&worktree, cwd);
2841 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2842 goto done;
2843 if (worktree)
2844 repo_path =
2845 strdup(got_worktree_get_repo_path(worktree));
2846 else
2847 repo_path = strdup(cwd);
2848 if (repo_path == NULL) {
2849 error = got_error_from_errno("strdup");
2850 goto done;
2854 error = got_repo_open(&repo, repo_path, NULL);
2855 if (error != NULL)
2856 goto done;
2858 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2859 repo, worktree);
2860 if (error)
2861 goto done;
2863 init_curses();
2865 error = apply_unveil(got_repo_get_path(repo),
2866 worktree ? got_worktree_get_root_path(worktree) : NULL);
2867 if (error)
2868 goto done;
2870 /* already loaded by tog_log_with_path()? */
2871 if (TAILQ_EMPTY(&tog_refs)) {
2872 error = tog_load_refs(repo, 0);
2873 if (error)
2874 goto done;
2877 if (start_commit == NULL) {
2878 error = got_repo_match_object_id(&start_id, &label,
2879 worktree ? got_worktree_get_head_ref_name(worktree) :
2880 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2881 if (error)
2882 goto done;
2883 head_ref_name = label;
2884 } else {
2885 error = got_ref_open(&ref, repo, start_commit, 0);
2886 if (error == NULL)
2887 head_ref_name = got_ref_get_name(ref);
2888 else if (error->code != GOT_ERR_NOT_REF)
2889 goto done;
2890 error = got_repo_match_object_id(&start_id, NULL,
2891 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2892 if (error)
2893 goto done;
2896 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2897 if (view == NULL) {
2898 error = got_error_from_errno("view_open");
2899 goto done;
2901 error = open_log_view(view, start_id, repo, head_ref_name,
2902 in_repo_path, log_branches);
2903 if (error)
2904 goto done;
2905 if (worktree) {
2906 /* Release work tree lock. */
2907 got_worktree_close(worktree);
2908 worktree = NULL;
2910 error = view_loop(view);
2911 done:
2912 free(in_repo_path);
2913 free(repo_path);
2914 free(cwd);
2915 free(start_id);
2916 free(label);
2917 if (ref)
2918 got_ref_close(ref);
2919 if (repo) {
2920 const struct got_error *close_err = got_repo_close(repo);
2921 if (error == NULL)
2922 error = close_err;
2924 if (worktree)
2925 got_worktree_close(worktree);
2926 tog_free_refs();
2927 return error;
2930 __dead static void
2931 usage_diff(void)
2933 endwin();
2934 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2935 "[-w] object1 object2\n", getprogname());
2936 exit(1);
2939 static int
2940 match_line(const char *line, regex_t *regex, size_t nmatch,
2941 regmatch_t *regmatch)
2943 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2946 struct tog_color *
2947 match_color(struct tog_colors *colors, const char *line)
2949 struct tog_color *tc = NULL;
2951 STAILQ_FOREACH(tc, colors, entry) {
2952 if (match_line(line, &tc->regex, 0, NULL))
2953 return tc;
2956 return NULL;
2959 static const struct got_error *
2960 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2961 WINDOW *window, regmatch_t *regmatch)
2963 const struct got_error *err = NULL;
2964 wchar_t *wline;
2965 int width;
2966 char *s;
2968 *wtotal = 0;
2970 s = strndup(line, regmatch->rm_so);
2971 if (s == NULL)
2972 return got_error_from_errno("strndup");
2974 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2975 if (err) {
2976 free(s);
2977 return err;
2979 waddwstr(window, wline);
2980 free(wline);
2981 free(s);
2982 wlimit -= width;
2983 *wtotal += width;
2985 if (wlimit > 0) {
2986 s = strndup(line + regmatch->rm_so,
2987 regmatch->rm_eo - regmatch->rm_so);
2988 if (s == NULL) {
2989 err = got_error_from_errno("strndup");
2990 free(s);
2991 return err;
2993 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2994 if (err) {
2995 free(s);
2996 return err;
2998 wattr_on(window, A_STANDOUT, NULL);
2999 waddwstr(window, wline);
3000 wattr_off(window, A_STANDOUT, NULL);
3001 free(wline);
3002 free(s);
3003 wlimit -= width;
3004 *wtotal += width;
3007 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3008 err = format_line(&wline, &width,
3009 line + regmatch->rm_eo, wlimit, col_tab_align);
3010 if (err)
3011 return err;
3012 waddwstr(window, wline);
3013 free(wline);
3014 *wtotal += width;
3017 return NULL;
3020 static const struct got_error *
3021 draw_file(struct tog_view *view, const char *header)
3023 struct tog_diff_view_state *s = &view->state.diff;
3024 regmatch_t *regmatch = &view->regmatch;
3025 const struct got_error *err;
3026 int nprinted = 0;
3027 char *line;
3028 size_t linesize = 0;
3029 ssize_t linelen;
3030 struct tog_color *tc;
3031 wchar_t *wline;
3032 int width;
3033 int max_lines = view->nlines;
3034 int nlines = s->nlines;
3035 off_t line_offset;
3037 line_offset = s->line_offsets[s->first_displayed_line - 1];
3038 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3039 return got_error_from_errno("fseek");
3041 werase(view->window);
3043 if (header) {
3044 if (asprintf(&line, "[%d/%d] %s",
3045 s->first_displayed_line - 1 + s->selected_line, nlines,
3046 header) == -1)
3047 return got_error_from_errno("asprintf");
3048 err = format_line(&wline, &width, line, view->ncols, 0);
3049 free(line);
3050 if (err)
3051 return err;
3053 if (view_needs_focus_indication(view))
3054 wstandout(view->window);
3055 waddwstr(view->window, wline);
3056 free(wline);
3057 wline = NULL;
3058 if (view_needs_focus_indication(view))
3059 wstandend(view->window);
3060 if (width <= view->ncols - 1)
3061 waddch(view->window, '\n');
3063 if (max_lines <= 1)
3064 return NULL;
3065 max_lines--;
3068 s->eof = 0;
3069 line = NULL;
3070 while (max_lines > 0 && nprinted < max_lines) {
3071 linelen = getline(&line, &linesize, s->f);
3072 if (linelen == -1) {
3073 if (feof(s->f)) {
3074 s->eof = 1;
3075 break;
3077 free(line);
3078 return got_ferror(s->f, GOT_ERR_IO);
3081 tc = match_color(&s->colors, line);
3082 if (tc)
3083 wattr_on(view->window,
3084 COLOR_PAIR(tc->colorpair), NULL);
3085 if (s->first_displayed_line + nprinted == s->matched_line &&
3086 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3087 err = add_matched_line(&width, line, view->ncols, 0,
3088 view->window, regmatch);
3089 if (err) {
3090 free(line);
3091 return err;
3093 } else {
3094 err = format_line(&wline, &width, line, view->ncols, 0);
3095 if (err) {
3096 free(line);
3097 return err;
3099 waddwstr(view->window, wline);
3100 free(wline);
3101 wline = NULL;
3103 if (tc)
3104 wattr_off(view->window,
3105 COLOR_PAIR(tc->colorpair), NULL);
3106 if (width <= view->ncols - 1)
3107 waddch(view->window, '\n');
3108 nprinted++;
3110 free(line);
3111 if (nprinted >= 1)
3112 s->last_displayed_line = s->first_displayed_line +
3113 (nprinted - 1);
3114 else
3115 s->last_displayed_line = s->first_displayed_line;
3117 view_vborder(view);
3119 if (s->eof) {
3120 while (nprinted < view->nlines) {
3121 waddch(view->window, '\n');
3122 nprinted++;
3125 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3126 if (err) {
3127 return err;
3130 wstandout(view->window);
3131 waddwstr(view->window, wline);
3132 free(wline);
3133 wline = NULL;
3134 wstandend(view->window);
3137 return NULL;
3140 static char *
3141 get_datestr(time_t *time, char *datebuf)
3143 struct tm mytm, *tm;
3144 char *p, *s;
3146 tm = gmtime_r(time, &mytm);
3147 if (tm == NULL)
3148 return NULL;
3149 s = asctime_r(tm, datebuf);
3150 if (s == NULL)
3151 return NULL;
3152 p = strchr(s, '\n');
3153 if (p)
3154 *p = '\0';
3155 return s;
3158 static const struct got_error *
3159 get_changed_paths(struct got_pathlist_head *paths,
3160 struct got_commit_object *commit, struct got_repository *repo)
3162 const struct got_error *err = NULL;
3163 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3164 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3165 struct got_object_qid *qid;
3167 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3168 if (qid != NULL) {
3169 struct got_commit_object *pcommit;
3170 err = got_object_open_as_commit(&pcommit, repo,
3171 &qid->id);
3172 if (err)
3173 return err;
3175 tree_id1 = got_object_id_dup(
3176 got_object_commit_get_tree_id(pcommit));
3177 if (tree_id1 == NULL) {
3178 got_object_commit_close(pcommit);
3179 return got_error_from_errno("got_object_id_dup");
3181 got_object_commit_close(pcommit);
3185 if (tree_id1) {
3186 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3187 if (err)
3188 goto done;
3191 tree_id2 = got_object_commit_get_tree_id(commit);
3192 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3193 if (err)
3194 goto done;
3196 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3197 got_diff_tree_collect_changed_paths, paths, 0);
3198 done:
3199 if (tree1)
3200 got_object_tree_close(tree1);
3201 if (tree2)
3202 got_object_tree_close(tree2);
3203 free(tree_id1);
3204 return err;
3207 static const struct got_error *
3208 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3210 off_t *p;
3212 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3213 if (p == NULL)
3214 return got_error_from_errno("reallocarray");
3215 *line_offsets = p;
3216 (*line_offsets)[*nlines] = off;
3217 (*nlines)++;
3218 return NULL;
3221 static const struct got_error *
3222 write_commit_info(off_t **line_offsets, size_t *nlines,
3223 struct got_object_id *commit_id, struct got_reflist_head *refs,
3224 struct got_repository *repo, FILE *outfile)
3226 const struct got_error *err = NULL;
3227 char datebuf[26], *datestr;
3228 struct got_commit_object *commit;
3229 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3230 time_t committer_time;
3231 const char *author, *committer;
3232 char *refs_str = NULL;
3233 struct got_pathlist_head changed_paths;
3234 struct got_pathlist_entry *pe;
3235 off_t outoff = 0;
3236 int n;
3238 TAILQ_INIT(&changed_paths);
3240 if (refs) {
3241 err = build_refs_str(&refs_str, refs, commit_id, repo);
3242 if (err)
3243 return err;
3246 err = got_object_open_as_commit(&commit, repo, commit_id);
3247 if (err)
3248 return err;
3250 err = got_object_id_str(&id_str, commit_id);
3251 if (err) {
3252 err = got_error_from_errno("got_object_id_str");
3253 goto done;
3256 err = add_line_offset(line_offsets, nlines, 0);
3257 if (err)
3258 goto done;
3260 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3261 refs_str ? refs_str : "", refs_str ? ")" : "");
3262 if (n < 0) {
3263 err = got_error_from_errno("fprintf");
3264 goto done;
3266 outoff += n;
3267 err = add_line_offset(line_offsets, nlines, outoff);
3268 if (err)
3269 goto done;
3271 n = fprintf(outfile, "from: %s\n",
3272 got_object_commit_get_author(commit));
3273 if (n < 0) {
3274 err = got_error_from_errno("fprintf");
3275 goto done;
3277 outoff += n;
3278 err = add_line_offset(line_offsets, nlines, outoff);
3279 if (err)
3280 goto done;
3282 committer_time = got_object_commit_get_committer_time(commit);
3283 datestr = get_datestr(&committer_time, datebuf);
3284 if (datestr) {
3285 n = fprintf(outfile, "date: %s UTC\n", datestr);
3286 if (n < 0) {
3287 err = got_error_from_errno("fprintf");
3288 goto done;
3290 outoff += n;
3291 err = add_line_offset(line_offsets, nlines, outoff);
3292 if (err)
3293 goto done;
3295 author = got_object_commit_get_author(commit);
3296 committer = got_object_commit_get_committer(commit);
3297 if (strcmp(author, committer) != 0) {
3298 n = fprintf(outfile, "via: %s\n", committer);
3299 if (n < 0) {
3300 err = got_error_from_errno("fprintf");
3301 goto done;
3303 outoff += n;
3304 err = add_line_offset(line_offsets, nlines, outoff);
3305 if (err)
3306 goto done;
3308 if (got_object_commit_get_nparents(commit) > 1) {
3309 const struct got_object_id_queue *parent_ids;
3310 struct got_object_qid *qid;
3311 int pn = 1;
3312 parent_ids = got_object_commit_get_parent_ids(commit);
3313 STAILQ_FOREACH(qid, parent_ids, entry) {
3314 err = got_object_id_str(&id_str, &qid->id);
3315 if (err)
3316 goto done;
3317 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3318 if (n < 0) {
3319 err = got_error_from_errno("fprintf");
3320 goto done;
3322 outoff += n;
3323 err = add_line_offset(line_offsets, nlines, outoff);
3324 if (err)
3325 goto done;
3326 free(id_str);
3327 id_str = NULL;
3331 err = got_object_commit_get_logmsg(&logmsg, commit);
3332 if (err)
3333 goto done;
3334 s = logmsg;
3335 while ((line = strsep(&s, "\n")) != NULL) {
3336 n = fprintf(outfile, "%s\n", line);
3337 if (n < 0) {
3338 err = got_error_from_errno("fprintf");
3339 goto done;
3341 outoff += n;
3342 err = add_line_offset(line_offsets, nlines, outoff);
3343 if (err)
3344 goto done;
3347 err = get_changed_paths(&changed_paths, commit, repo);
3348 if (err)
3349 goto done;
3350 TAILQ_FOREACH(pe, &changed_paths, entry) {
3351 struct got_diff_changed_path *cp = pe->data;
3352 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3353 if (n < 0) {
3354 err = got_error_from_errno("fprintf");
3355 goto done;
3357 outoff += n;
3358 err = add_line_offset(line_offsets, nlines, outoff);
3359 if (err)
3360 goto done;
3361 free((char *)pe->path);
3362 free(pe->data);
3365 fputc('\n', outfile);
3366 outoff++;
3367 err = add_line_offset(line_offsets, nlines, outoff);
3368 done:
3369 got_pathlist_free(&changed_paths);
3370 free(id_str);
3371 free(logmsg);
3372 free(refs_str);
3373 got_object_commit_close(commit);
3374 if (err) {
3375 free(*line_offsets);
3376 *line_offsets = NULL;
3377 *nlines = 0;
3379 return err;
3382 static const struct got_error *
3383 create_diff(struct tog_diff_view_state *s)
3385 const struct got_error *err = NULL;
3386 FILE *f = NULL;
3387 int obj_type;
3389 free(s->line_offsets);
3390 s->line_offsets = malloc(sizeof(off_t));
3391 if (s->line_offsets == NULL)
3392 return got_error_from_errno("malloc");
3393 s->nlines = 0;
3395 f = got_opentemp();
3396 if (f == NULL) {
3397 err = got_error_from_errno("got_opentemp");
3398 goto done;
3400 if (s->f && fclose(s->f) == EOF) {
3401 err = got_error_from_errno("fclose");
3402 goto done;
3404 s->f = f;
3406 if (s->id1)
3407 err = got_object_get_type(&obj_type, s->repo, s->id1);
3408 else
3409 err = got_object_get_type(&obj_type, s->repo, s->id2);
3410 if (err)
3411 goto done;
3413 switch (obj_type) {
3414 case GOT_OBJ_TYPE_BLOB:
3415 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3416 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3417 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3418 s->repo, s->f);
3419 break;
3420 case GOT_OBJ_TYPE_TREE:
3421 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3422 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3423 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3424 break;
3425 case GOT_OBJ_TYPE_COMMIT: {
3426 const struct got_object_id_queue *parent_ids;
3427 struct got_object_qid *pid;
3428 struct got_commit_object *commit2;
3429 struct got_reflist_head *refs;
3431 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3432 if (err)
3433 goto done;
3434 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3435 /* Show commit info if we're diffing to a parent/root commit. */
3436 if (s->id1 == NULL) {
3437 err = write_commit_info(&s->line_offsets, &s->nlines,
3438 s->id2, refs, s->repo, s->f);
3439 if (err)
3440 goto done;
3441 } else {
3442 parent_ids = got_object_commit_get_parent_ids(commit2);
3443 STAILQ_FOREACH(pid, parent_ids, entry) {
3444 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3445 err = write_commit_info(
3446 &s->line_offsets, &s->nlines,
3447 s->id2, refs, s->repo, s->f);
3448 if (err)
3449 goto done;
3450 break;
3454 got_object_commit_close(commit2);
3456 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3457 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3458 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3459 break;
3461 default:
3462 err = got_error(GOT_ERR_OBJ_TYPE);
3463 break;
3465 if (err)
3466 goto done;
3467 done:
3468 if (s->f && fflush(s->f) != 0 && err == NULL)
3469 err = got_error_from_errno("fflush");
3470 return err;
3473 static void
3474 diff_view_indicate_progress(struct tog_view *view)
3476 mvwaddstr(view->window, 0, 0, "diffing...");
3477 update_panels();
3478 doupdate();
3481 static const struct got_error *
3482 search_start_diff_view(struct tog_view *view)
3484 struct tog_diff_view_state *s = &view->state.diff;
3486 s->matched_line = 0;
3487 return NULL;
3490 static const struct got_error *
3491 search_next_diff_view(struct tog_view *view)
3493 struct tog_diff_view_state *s = &view->state.diff;
3494 int lineno;
3495 char *line = NULL;
3496 size_t linesize = 0;
3497 ssize_t linelen;
3499 if (!view->searching) {
3500 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3501 return NULL;
3504 if (s->matched_line) {
3505 if (view->searching == TOG_SEARCH_FORWARD)
3506 lineno = s->matched_line + 1;
3507 else
3508 lineno = s->matched_line - 1;
3509 } else
3510 lineno = s->first_displayed_line;
3512 while (1) {
3513 off_t offset;
3515 if (lineno <= 0 || lineno > s->nlines) {
3516 if (s->matched_line == 0) {
3517 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3518 break;
3521 if (view->searching == TOG_SEARCH_FORWARD)
3522 lineno = 1;
3523 else
3524 lineno = s->nlines;
3527 offset = s->line_offsets[lineno - 1];
3528 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3529 free(line);
3530 return got_error_from_errno("fseeko");
3532 linelen = getline(&line, &linesize, s->f);
3533 if (linelen != -1 &&
3534 match_line(line, &view->regex, 1, &view->regmatch)) {
3535 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3536 s->matched_line = lineno;
3537 break;
3539 if (view->searching == TOG_SEARCH_FORWARD)
3540 lineno++;
3541 else
3542 lineno--;
3544 free(line);
3546 if (s->matched_line) {
3547 s->first_displayed_line = s->matched_line;
3548 s->selected_line = 1;
3551 return NULL;
3554 static const struct got_error *
3555 close_diff_view(struct tog_view *view)
3557 const struct got_error *err = NULL;
3558 struct tog_diff_view_state *s = &view->state.diff;
3560 free(s->id1);
3561 s->id1 = NULL;
3562 free(s->id2);
3563 s->id2 = NULL;
3564 if (s->f && fclose(s->f) == EOF)
3565 err = got_error_from_errno("fclose");
3566 s->f = NULL;
3567 if (s->f1 && fclose(s->f1) == EOF)
3568 err = got_error_from_errno("fclose");
3569 s->f1 = NULL;
3570 if (s->f2 && fclose(s->f2) == EOF)
3571 err = got_error_from_errno("fclose");
3572 s->f2 = NULL;
3573 free_colors(&s->colors);
3574 free(s->line_offsets);
3575 s->line_offsets = NULL;
3576 s->nlines = 0;
3577 return err;
3580 static const struct got_error *
3581 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3582 struct got_object_id *id2, const char *label1, const char *label2,
3583 int diff_context, int ignore_whitespace, int force_text_diff,
3584 struct tog_view *log_view, struct got_repository *repo)
3586 const struct got_error *err;
3587 struct tog_diff_view_state *s = &view->state.diff;
3589 memset(s, 0, sizeof(*s));
3591 if (id1 != NULL && id2 != NULL) {
3592 int type1, type2;
3593 err = got_object_get_type(&type1, repo, id1);
3594 if (err)
3595 return err;
3596 err = got_object_get_type(&type2, repo, id2);
3597 if (err)
3598 return err;
3600 if (type1 != type2)
3601 return got_error(GOT_ERR_OBJ_TYPE);
3603 s->first_displayed_line = 1;
3604 s->last_displayed_line = view->nlines;
3605 s->selected_line = 1;
3606 s->repo = repo;
3607 s->id1 = id1;
3608 s->id2 = id2;
3609 s->label1 = label1;
3610 s->label2 = label2;
3612 if (id1) {
3613 s->id1 = got_object_id_dup(id1);
3614 if (s->id1 == NULL)
3615 return got_error_from_errno("got_object_id_dup");
3616 s->f1 = got_opentemp();
3617 if (s->f1 == NULL) {
3618 err = got_error_from_errno("got_opentemp");
3619 goto done;
3621 } else
3622 s->id1 = NULL;
3624 s->id2 = got_object_id_dup(id2);
3625 if (s->id2 == NULL) {
3626 err = got_error_from_errno("got_object_id_dup");
3627 goto done;
3630 s->f2 = got_opentemp();
3631 if (s->f2 == NULL) {
3632 err = got_error_from_errno("got_opentemp");
3633 goto done;
3636 s->first_displayed_line = 1;
3637 s->last_displayed_line = view->nlines;
3638 s->diff_context = diff_context;
3639 s->ignore_whitespace = ignore_whitespace;
3640 s->force_text_diff = force_text_diff;
3641 s->log_view = log_view;
3642 s->repo = repo;
3644 STAILQ_INIT(&s->colors);
3645 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3646 err = add_color(&s->colors,
3647 "^-", TOG_COLOR_DIFF_MINUS,
3648 get_color_value("TOG_COLOR_DIFF_MINUS"));
3649 if (err)
3650 goto done;
3651 err = add_color(&s->colors, "^\\+",
3652 TOG_COLOR_DIFF_PLUS,
3653 get_color_value("TOG_COLOR_DIFF_PLUS"));
3654 if (err)
3655 goto done;
3656 err = add_color(&s->colors,
3657 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3658 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3659 if (err)
3660 goto done;
3662 err = add_color(&s->colors,
3663 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3664 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3665 get_color_value("TOG_COLOR_DIFF_META"));
3666 if (err)
3667 goto done;
3669 err = add_color(&s->colors,
3670 "^(from|via): ", TOG_COLOR_AUTHOR,
3671 get_color_value("TOG_COLOR_AUTHOR"));
3672 if (err)
3673 goto done;
3675 err = add_color(&s->colors,
3676 "^date: ", TOG_COLOR_DATE,
3677 get_color_value("TOG_COLOR_DATE"));
3678 if (err)
3679 goto done;
3682 if (log_view && view_is_splitscreen(view))
3683 show_log_view(log_view); /* draw vborder */
3684 diff_view_indicate_progress(view);
3686 err = create_diff(s);
3688 view->show = show_diff_view;
3689 view->input = input_diff_view;
3690 view->close = close_diff_view;
3691 view->search_start = search_start_diff_view;
3692 view->search_next = search_next_diff_view;
3693 done:
3694 if (err)
3695 close_diff_view(view);
3696 return err;
3699 static const struct got_error *
3700 show_diff_view(struct tog_view *view)
3702 const struct got_error *err;
3703 struct tog_diff_view_state *s = &view->state.diff;
3704 char *id_str1 = NULL, *id_str2, *header;
3705 const char *label1, *label2;
3707 if (s->id1) {
3708 err = got_object_id_str(&id_str1, s->id1);
3709 if (err)
3710 return err;
3711 label1 = s->label1 ? : id_str1;
3712 } else
3713 label1 = "/dev/null";
3715 err = got_object_id_str(&id_str2, s->id2);
3716 if (err)
3717 return err;
3718 label2 = s->label2 ? : id_str2;
3720 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3721 err = got_error_from_errno("asprintf");
3722 free(id_str1);
3723 free(id_str2);
3724 return err;
3726 free(id_str1);
3727 free(id_str2);
3729 err = draw_file(view, header);
3730 free(header);
3731 return err;
3734 static const struct got_error *
3735 set_selected_commit(struct tog_diff_view_state *s,
3736 struct commit_queue_entry *entry)
3738 const struct got_error *err;
3739 const struct got_object_id_queue *parent_ids;
3740 struct got_commit_object *selected_commit;
3741 struct got_object_qid *pid;
3743 free(s->id2);
3744 s->id2 = got_object_id_dup(entry->id);
3745 if (s->id2 == NULL)
3746 return got_error_from_errno("got_object_id_dup");
3748 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3749 if (err)
3750 return err;
3751 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3752 free(s->id1);
3753 pid = STAILQ_FIRST(parent_ids);
3754 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3755 got_object_commit_close(selected_commit);
3756 return NULL;
3759 static const struct got_error *
3760 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3762 const struct got_error *err = NULL;
3763 struct tog_diff_view_state *s = &view->state.diff;
3764 struct tog_log_view_state *ls;
3765 struct commit_queue_entry *old_selected_entry;
3766 char *line = NULL;
3767 size_t linesize = 0;
3768 ssize_t linelen;
3769 int i, nscroll = view->nlines - 1;
3771 switch (ch) {
3772 case 'a':
3773 case 'w':
3774 if (ch == 'a')
3775 s->force_text_diff = !s->force_text_diff;
3776 if (ch == 'w')
3777 s->ignore_whitespace = !s->ignore_whitespace;
3778 wclear(view->window);
3779 s->first_displayed_line = 1;
3780 s->last_displayed_line = view->nlines;
3781 s->matched_line = 0;
3782 diff_view_indicate_progress(view);
3783 err = create_diff(s);
3784 break;
3785 case 'g':
3786 case KEY_HOME:
3787 s->first_displayed_line = 1;
3788 break;
3789 case 'G':
3790 case KEY_END:
3791 if (s->eof)
3792 break;
3794 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3795 s->eof = 1;
3796 break;
3797 case 'k':
3798 case KEY_UP:
3799 case CTRL('p'):
3800 if (s->first_displayed_line > 1)
3801 s->first_displayed_line--;
3802 break;
3803 case CTRL('u'):
3804 nscroll /= 2;
3805 /* FALL THROUGH */
3806 case KEY_PPAGE:
3807 case CTRL('b'):
3808 if (s->first_displayed_line == 1)
3809 break;
3810 i = 0;
3811 while (i++ < nscroll && s->first_displayed_line > 1)
3812 s->first_displayed_line--;
3813 break;
3814 case 'j':
3815 case KEY_DOWN:
3816 case CTRL('n'):
3817 if (!s->eof)
3818 s->first_displayed_line++;
3819 break;
3820 case CTRL('d'):
3821 nscroll /= 2;
3822 /* FALL THROUGH */
3823 case KEY_NPAGE:
3824 case CTRL('f'):
3825 case ' ':
3826 if (s->eof)
3827 break;
3828 i = 0;
3829 while (!s->eof && i++ < nscroll) {
3830 linelen = getline(&line, &linesize, s->f);
3831 s->first_displayed_line++;
3832 if (linelen == -1) {
3833 if (feof(s->f)) {
3834 s->eof = 1;
3835 } else
3836 err = got_ferror(s->f, GOT_ERR_IO);
3837 break;
3840 free(line);
3841 break;
3842 case '[':
3843 if (s->diff_context > 0) {
3844 s->diff_context--;
3845 s->matched_line = 0;
3846 diff_view_indicate_progress(view);
3847 err = create_diff(s);
3848 if (s->first_displayed_line + view->nlines - 1 >
3849 s->nlines) {
3850 s->first_displayed_line = 1;
3851 s->last_displayed_line = view->nlines;
3854 break;
3855 case ']':
3856 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3857 s->diff_context++;
3858 s->matched_line = 0;
3859 diff_view_indicate_progress(view);
3860 err = create_diff(s);
3862 break;
3863 case '<':
3864 case ',':
3865 if (s->log_view == NULL)
3866 break;
3867 ls = &s->log_view->state.log;
3868 old_selected_entry = ls->selected_entry;
3870 err = input_log_view(NULL, s->log_view, KEY_UP);
3871 if (err)
3872 break;
3874 if (old_selected_entry == ls->selected_entry)
3875 break;
3877 err = set_selected_commit(s, ls->selected_entry);
3878 if (err)
3879 break;
3881 s->first_displayed_line = 1;
3882 s->last_displayed_line = view->nlines;
3883 s->matched_line = 0;
3885 diff_view_indicate_progress(view);
3886 err = create_diff(s);
3887 break;
3888 case '>':
3889 case '.':
3890 if (s->log_view == NULL)
3891 break;
3892 ls = &s->log_view->state.log;
3893 old_selected_entry = ls->selected_entry;
3895 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3896 if (err)
3897 break;
3899 if (old_selected_entry == ls->selected_entry)
3900 break;
3902 err = set_selected_commit(s, ls->selected_entry);
3903 if (err)
3904 break;
3906 s->first_displayed_line = 1;
3907 s->last_displayed_line = view->nlines;
3908 s->matched_line = 0;
3910 diff_view_indicate_progress(view);
3911 err = create_diff(s);
3912 break;
3913 default:
3914 break;
3917 return err;
3920 static const struct got_error *
3921 cmd_diff(int argc, char *argv[])
3923 const struct got_error *error = NULL;
3924 struct got_repository *repo = NULL;
3925 struct got_worktree *worktree = NULL;
3926 struct got_object_id *id1 = NULL, *id2 = NULL;
3927 char *repo_path = NULL, *cwd = NULL;
3928 char *id_str1 = NULL, *id_str2 = NULL;
3929 char *label1 = NULL, *label2 = NULL;
3930 int diff_context = 3, ignore_whitespace = 0;
3931 int ch, force_text_diff = 0;
3932 const char *errstr;
3933 struct tog_view *view;
3935 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3936 switch (ch) {
3937 case 'a':
3938 force_text_diff = 1;
3939 break;
3940 case 'C':
3941 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3942 &errstr);
3943 if (errstr != NULL)
3944 errx(1, "number of context lines is %s: %s",
3945 errstr, errstr);
3946 break;
3947 case 'r':
3948 repo_path = realpath(optarg, NULL);
3949 if (repo_path == NULL)
3950 return got_error_from_errno2("realpath",
3951 optarg);
3952 got_path_strip_trailing_slashes(repo_path);
3953 break;
3954 case 'w':
3955 ignore_whitespace = 1;
3956 break;
3957 default:
3958 usage_diff();
3959 /* NOTREACHED */
3963 argc -= optind;
3964 argv += optind;
3966 if (argc == 0) {
3967 usage_diff(); /* TODO show local worktree changes */
3968 } else if (argc == 2) {
3969 id_str1 = argv[0];
3970 id_str2 = argv[1];
3971 } else
3972 usage_diff();
3974 if (repo_path == NULL) {
3975 cwd = getcwd(NULL, 0);
3976 if (cwd == NULL)
3977 return got_error_from_errno("getcwd");
3978 error = got_worktree_open(&worktree, cwd);
3979 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3980 goto done;
3981 if (worktree)
3982 repo_path =
3983 strdup(got_worktree_get_repo_path(worktree));
3984 else
3985 repo_path = strdup(cwd);
3986 if (repo_path == NULL) {
3987 error = got_error_from_errno("strdup");
3988 goto done;
3992 error = got_repo_open(&repo, repo_path, NULL);
3993 if (error)
3994 goto done;
3996 init_curses();
3998 error = apply_unveil(got_repo_get_path(repo), NULL);
3999 if (error)
4000 goto done;
4002 error = tog_load_refs(repo, 0);
4003 if (error)
4004 goto done;
4006 error = got_repo_match_object_id(&id1, &label1, id_str1,
4007 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4008 if (error)
4009 goto done;
4011 error = got_repo_match_object_id(&id2, &label2, id_str2,
4012 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4013 if (error)
4014 goto done;
4016 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4017 if (view == NULL) {
4018 error = got_error_from_errno("view_open");
4019 goto done;
4021 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4022 ignore_whitespace, force_text_diff, NULL, repo);
4023 if (error)
4024 goto done;
4025 error = view_loop(view);
4026 done:
4027 free(label1);
4028 free(label2);
4029 free(repo_path);
4030 free(cwd);
4031 if (repo) {
4032 const struct got_error *close_err = got_repo_close(repo);
4033 if (error == NULL)
4034 error = close_err;
4036 if (worktree)
4037 got_worktree_close(worktree);
4038 tog_free_refs();
4039 return error;
4042 __dead static void
4043 usage_blame(void)
4045 endwin();
4046 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4047 getprogname());
4048 exit(1);
4051 struct tog_blame_line {
4052 int annotated;
4053 struct got_object_id *id;
4056 static const struct got_error *
4057 draw_blame(struct tog_view *view)
4059 struct tog_blame_view_state *s = &view->state.blame;
4060 struct tog_blame *blame = &s->blame;
4061 regmatch_t *regmatch = &view->regmatch;
4062 const struct got_error *err;
4063 int lineno = 0, nprinted = 0;
4064 char *line = NULL;
4065 size_t linesize = 0;
4066 ssize_t linelen;
4067 wchar_t *wline;
4068 int width;
4069 struct tog_blame_line *blame_line;
4070 struct got_object_id *prev_id = NULL;
4071 char *id_str;
4072 struct tog_color *tc;
4074 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4075 if (err)
4076 return err;
4078 rewind(blame->f);
4079 werase(view->window);
4081 if (asprintf(&line, "commit %s", id_str) == -1) {
4082 err = got_error_from_errno("asprintf");
4083 free(id_str);
4084 return err;
4087 err = format_line(&wline, &width, line, view->ncols, 0);
4088 free(line);
4089 line = NULL;
4090 if (err)
4091 return err;
4092 if (view_needs_focus_indication(view))
4093 wstandout(view->window);
4094 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4095 if (tc)
4096 wattr_on(view->window,
4097 COLOR_PAIR(tc->colorpair), NULL);
4098 waddwstr(view->window, wline);
4099 if (tc)
4100 wattr_off(view->window,
4101 COLOR_PAIR(tc->colorpair), NULL);
4102 if (view_needs_focus_indication(view))
4103 wstandend(view->window);
4104 free(wline);
4105 wline = NULL;
4106 if (width < view->ncols - 1)
4107 waddch(view->window, '\n');
4109 if (asprintf(&line, "[%d/%d] %s%s",
4110 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4111 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4112 free(id_str);
4113 return got_error_from_errno("asprintf");
4115 free(id_str);
4116 err = format_line(&wline, &width, line, view->ncols, 0);
4117 free(line);
4118 line = NULL;
4119 if (err)
4120 return err;
4121 waddwstr(view->window, wline);
4122 free(wline);
4123 wline = NULL;
4124 if (width < view->ncols - 1)
4125 waddch(view->window, '\n');
4127 s->eof = 0;
4128 while (nprinted < view->nlines - 2) {
4129 linelen = getline(&line, &linesize, blame->f);
4130 if (linelen == -1) {
4131 if (feof(blame->f)) {
4132 s->eof = 1;
4133 break;
4135 free(line);
4136 return got_ferror(blame->f, GOT_ERR_IO);
4138 if (++lineno < s->first_displayed_line)
4139 continue;
4141 if (view->focussed && nprinted == s->selected_line - 1)
4142 wstandout(view->window);
4144 if (blame->nlines > 0) {
4145 blame_line = &blame->lines[lineno - 1];
4146 if (blame_line->annotated && prev_id &&
4147 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4148 !(view->focussed &&
4149 nprinted == s->selected_line - 1)) {
4150 waddstr(view->window, " ");
4151 } else if (blame_line->annotated) {
4152 char *id_str;
4153 err = got_object_id_str(&id_str, blame_line->id);
4154 if (err) {
4155 free(line);
4156 return err;
4158 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4159 if (tc)
4160 wattr_on(view->window,
4161 COLOR_PAIR(tc->colorpair), NULL);
4162 wprintw(view->window, "%.8s", id_str);
4163 if (tc)
4164 wattr_off(view->window,
4165 COLOR_PAIR(tc->colorpair), NULL);
4166 free(id_str);
4167 prev_id = blame_line->id;
4168 } else {
4169 waddstr(view->window, "........");
4170 prev_id = NULL;
4172 } else {
4173 waddstr(view->window, "........");
4174 prev_id = NULL;
4177 if (view->focussed && nprinted == s->selected_line - 1)
4178 wstandend(view->window);
4179 waddstr(view->window, " ");
4181 if (view->ncols <= 9) {
4182 width = 9;
4183 wline = wcsdup(L"");
4184 if (wline == NULL) {
4185 err = got_error_from_errno("wcsdup");
4186 free(line);
4187 return err;
4189 } else if (s->first_displayed_line + nprinted ==
4190 s->matched_line &&
4191 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4192 err = add_matched_line(&width, line, view->ncols - 9, 9,
4193 view->window, regmatch);
4194 if (err) {
4195 free(line);
4196 return err;
4198 width += 9;
4199 } else {
4200 err = format_line(&wline, &width, line,
4201 view->ncols - 9, 9);
4202 waddwstr(view->window, wline);
4203 free(wline);
4204 wline = NULL;
4205 width += 9;
4208 if (width <= view->ncols - 1)
4209 waddch(view->window, '\n');
4210 if (++nprinted == 1)
4211 s->first_displayed_line = lineno;
4213 free(line);
4214 s->last_displayed_line = lineno;
4216 view_vborder(view);
4218 return NULL;
4221 static const struct got_error *
4222 blame_cb(void *arg, int nlines, int lineno,
4223 struct got_commit_object *commit, struct got_object_id *id)
4225 const struct got_error *err = NULL;
4226 struct tog_blame_cb_args *a = arg;
4227 struct tog_blame_line *line;
4228 int errcode;
4230 if (nlines != a->nlines ||
4231 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4232 return got_error(GOT_ERR_RANGE);
4234 errcode = pthread_mutex_lock(&tog_mutex);
4235 if (errcode)
4236 return got_error_set_errno(errcode, "pthread_mutex_lock");
4238 if (*a->quit) { /* user has quit the blame view */
4239 err = got_error(GOT_ERR_ITER_COMPLETED);
4240 goto done;
4243 if (lineno == -1)
4244 goto done; /* no change in this commit */
4246 line = &a->lines[lineno - 1];
4247 if (line->annotated)
4248 goto done;
4250 line->id = got_object_id_dup(id);
4251 if (line->id == NULL) {
4252 err = got_error_from_errno("got_object_id_dup");
4253 goto done;
4255 line->annotated = 1;
4256 done:
4257 errcode = pthread_mutex_unlock(&tog_mutex);
4258 if (errcode)
4259 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4260 return err;
4263 static void *
4264 blame_thread(void *arg)
4266 const struct got_error *err, *close_err;
4267 struct tog_blame_thread_args *ta = arg;
4268 struct tog_blame_cb_args *a = ta->cb_args;
4269 int errcode;
4271 err = block_signals_used_by_main_thread();
4272 if (err)
4273 return (void *)err;
4275 err = got_blame(ta->path, a->commit_id, ta->repo,
4276 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4277 if (err && err->code == GOT_ERR_CANCELLED)
4278 err = NULL;
4280 errcode = pthread_mutex_lock(&tog_mutex);
4281 if (errcode)
4282 return (void *)got_error_set_errno(errcode,
4283 "pthread_mutex_lock");
4285 close_err = got_repo_close(ta->repo);
4286 if (err == NULL)
4287 err = close_err;
4288 ta->repo = NULL;
4289 *ta->complete = 1;
4291 errcode = pthread_mutex_unlock(&tog_mutex);
4292 if (errcode && err == NULL)
4293 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4295 return (void *)err;
4298 static struct got_object_id *
4299 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4300 int first_displayed_line, int selected_line)
4302 struct tog_blame_line *line;
4304 if (nlines <= 0)
4305 return NULL;
4307 line = &lines[first_displayed_line - 1 + selected_line - 1];
4308 if (!line->annotated)
4309 return NULL;
4311 return line->id;
4314 static const struct got_error *
4315 stop_blame(struct tog_blame *blame)
4317 const struct got_error *err = NULL;
4318 int i;
4320 if (blame->thread) {
4321 int errcode;
4322 errcode = pthread_mutex_unlock(&tog_mutex);
4323 if (errcode)
4324 return got_error_set_errno(errcode,
4325 "pthread_mutex_unlock");
4326 errcode = pthread_join(blame->thread, (void **)&err);
4327 if (errcode)
4328 return got_error_set_errno(errcode, "pthread_join");
4329 errcode = pthread_mutex_lock(&tog_mutex);
4330 if (errcode)
4331 return got_error_set_errno(errcode,
4332 "pthread_mutex_lock");
4333 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4334 err = NULL;
4335 blame->thread = 0; //NULL;
4337 if (blame->thread_args.repo) {
4338 const struct got_error *close_err;
4339 close_err = got_repo_close(blame->thread_args.repo);
4340 if (err == NULL)
4341 err = close_err;
4342 blame->thread_args.repo = NULL;
4344 if (blame->f) {
4345 if (fclose(blame->f) == EOF && err == NULL)
4346 err = got_error_from_errno("fclose");
4347 blame->f = NULL;
4349 if (blame->lines) {
4350 for (i = 0; i < blame->nlines; i++)
4351 free(blame->lines[i].id);
4352 free(blame->lines);
4353 blame->lines = NULL;
4355 free(blame->cb_args.commit_id);
4356 blame->cb_args.commit_id = NULL;
4358 return err;
4361 static const struct got_error *
4362 cancel_blame_view(void *arg)
4364 const struct got_error *err = NULL;
4365 int *done = arg;
4366 int errcode;
4368 errcode = pthread_mutex_lock(&tog_mutex);
4369 if (errcode)
4370 return got_error_set_errno(errcode,
4371 "pthread_mutex_unlock");
4373 if (*done)
4374 err = got_error(GOT_ERR_CANCELLED);
4376 errcode = pthread_mutex_unlock(&tog_mutex);
4377 if (errcode)
4378 return got_error_set_errno(errcode,
4379 "pthread_mutex_lock");
4381 return err;
4384 static const struct got_error *
4385 run_blame(struct tog_view *view)
4387 struct tog_blame_view_state *s = &view->state.blame;
4388 struct tog_blame *blame = &s->blame;
4389 const struct got_error *err = NULL;
4390 struct got_commit_object *commit = NULL;
4391 struct got_blob_object *blob = NULL;
4392 struct got_repository *thread_repo = NULL;
4393 struct got_object_id *obj_id = NULL;
4394 int obj_type;
4396 err = got_object_open_as_commit(&commit, s->repo,
4397 &s->blamed_commit->id);
4398 if (err)
4399 return err;
4401 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4402 if (err)
4403 goto done;
4405 err = got_object_get_type(&obj_type, s->repo, obj_id);
4406 if (err)
4407 goto done;
4409 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4410 err = got_error(GOT_ERR_OBJ_TYPE);
4411 goto done;
4414 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4415 if (err)
4416 goto done;
4417 blame->f = got_opentemp();
4418 if (blame->f == NULL) {
4419 err = got_error_from_errno("got_opentemp");
4420 goto done;
4422 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4423 &blame->line_offsets, blame->f, blob);
4424 if (err)
4425 goto done;
4426 if (blame->nlines == 0) {
4427 s->blame_complete = 1;
4428 goto done;
4431 /* Don't include \n at EOF in the blame line count. */
4432 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4433 blame->nlines--;
4435 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4436 if (blame->lines == NULL) {
4437 err = got_error_from_errno("calloc");
4438 goto done;
4441 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4442 if (err)
4443 goto done;
4445 blame->cb_args.view = view;
4446 blame->cb_args.lines = blame->lines;
4447 blame->cb_args.nlines = blame->nlines;
4448 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4449 if (blame->cb_args.commit_id == NULL) {
4450 err = got_error_from_errno("got_object_id_dup");
4451 goto done;
4453 blame->cb_args.quit = &s->done;
4455 blame->thread_args.path = s->path;
4456 blame->thread_args.repo = thread_repo;
4457 blame->thread_args.cb_args = &blame->cb_args;
4458 blame->thread_args.complete = &s->blame_complete;
4459 blame->thread_args.cancel_cb = cancel_blame_view;
4460 blame->thread_args.cancel_arg = &s->done;
4461 s->blame_complete = 0;
4463 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4464 s->first_displayed_line = 1;
4465 s->last_displayed_line = view->nlines;
4466 s->selected_line = 1;
4468 s->matched_line = 0;
4470 done:
4471 if (commit)
4472 got_object_commit_close(commit);
4473 if (blob)
4474 got_object_blob_close(blob);
4475 free(obj_id);
4476 if (err)
4477 stop_blame(blame);
4478 return err;
4481 static const struct got_error *
4482 open_blame_view(struct tog_view *view, char *path,
4483 struct got_object_id *commit_id, struct got_repository *repo)
4485 const struct got_error *err = NULL;
4486 struct tog_blame_view_state *s = &view->state.blame;
4488 STAILQ_INIT(&s->blamed_commits);
4490 s->path = strdup(path);
4491 if (s->path == NULL)
4492 return got_error_from_errno("strdup");
4494 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4495 if (err) {
4496 free(s->path);
4497 return err;
4500 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4501 s->first_displayed_line = 1;
4502 s->last_displayed_line = view->nlines;
4503 s->selected_line = 1;
4504 s->blame_complete = 0;
4505 s->repo = repo;
4506 s->commit_id = commit_id;
4507 memset(&s->blame, 0, sizeof(s->blame));
4509 STAILQ_INIT(&s->colors);
4510 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4511 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4512 get_color_value("TOG_COLOR_COMMIT"));
4513 if (err)
4514 return err;
4517 view->show = show_blame_view;
4518 view->input = input_blame_view;
4519 view->close = close_blame_view;
4520 view->search_start = search_start_blame_view;
4521 view->search_next = search_next_blame_view;
4523 return run_blame(view);
4526 static const struct got_error *
4527 close_blame_view(struct tog_view *view)
4529 const struct got_error *err = NULL;
4530 struct tog_blame_view_state *s = &view->state.blame;
4532 if (s->blame.thread)
4533 err = stop_blame(&s->blame);
4535 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4536 struct got_object_qid *blamed_commit;
4537 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4538 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4539 got_object_qid_free(blamed_commit);
4542 free(s->path);
4543 free_colors(&s->colors);
4545 return err;
4548 static const struct got_error *
4549 search_start_blame_view(struct tog_view *view)
4551 struct tog_blame_view_state *s = &view->state.blame;
4553 s->matched_line = 0;
4554 return NULL;
4557 static const struct got_error *
4558 search_next_blame_view(struct tog_view *view)
4560 struct tog_blame_view_state *s = &view->state.blame;
4561 int lineno;
4562 char *line = NULL;
4563 size_t linesize = 0;
4564 ssize_t linelen;
4566 if (!view->searching) {
4567 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4568 return NULL;
4571 if (s->matched_line) {
4572 if (view->searching == TOG_SEARCH_FORWARD)
4573 lineno = s->matched_line + 1;
4574 else
4575 lineno = s->matched_line - 1;
4576 } else
4577 lineno = s->first_displayed_line - 1 + s->selected_line;
4579 while (1) {
4580 off_t offset;
4582 if (lineno <= 0 || lineno > s->blame.nlines) {
4583 if (s->matched_line == 0) {
4584 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4585 break;
4588 if (view->searching == TOG_SEARCH_FORWARD)
4589 lineno = 1;
4590 else
4591 lineno = s->blame.nlines;
4594 offset = s->blame.line_offsets[lineno - 1];
4595 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4596 free(line);
4597 return got_error_from_errno("fseeko");
4599 linelen = getline(&line, &linesize, s->blame.f);
4600 if (linelen != -1 &&
4601 match_line(line, &view->regex, 1, &view->regmatch)) {
4602 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4603 s->matched_line = lineno;
4604 break;
4606 if (view->searching == TOG_SEARCH_FORWARD)
4607 lineno++;
4608 else
4609 lineno--;
4611 free(line);
4613 if (s->matched_line) {
4614 s->first_displayed_line = s->matched_line;
4615 s->selected_line = 1;
4618 return NULL;
4621 static const struct got_error *
4622 show_blame_view(struct tog_view *view)
4624 const struct got_error *err = NULL;
4625 struct tog_blame_view_state *s = &view->state.blame;
4626 int errcode;
4628 if (s->blame.thread == 0 && !s->blame_complete) {
4629 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4630 &s->blame.thread_args);
4631 if (errcode)
4632 return got_error_set_errno(errcode, "pthread_create");
4634 halfdelay(1); /* fast refresh while annotating */
4637 if (s->blame_complete)
4638 halfdelay(10); /* disable fast refresh */
4640 err = draw_blame(view);
4642 view_vborder(view);
4643 return err;
4646 static const struct got_error *
4647 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4649 const struct got_error *err = NULL, *thread_err = NULL;
4650 struct tog_view *diff_view;
4651 struct tog_blame_view_state *s = &view->state.blame;
4652 int begin_x = 0, nscroll = view->nlines - 2;
4654 switch (ch) {
4655 case 'q':
4656 s->done = 1;
4657 break;
4658 case 'g':
4659 case KEY_HOME:
4660 s->selected_line = 1;
4661 s->first_displayed_line = 1;
4662 break;
4663 case 'G':
4664 case KEY_END:
4665 if (s->blame.nlines < view->nlines - 2) {
4666 s->selected_line = s->blame.nlines;
4667 s->first_displayed_line = 1;
4668 } else {
4669 s->selected_line = view->nlines - 2;
4670 s->first_displayed_line = s->blame.nlines -
4671 (view->nlines - 3);
4673 break;
4674 case 'k':
4675 case KEY_UP:
4676 case CTRL('p'):
4677 if (s->selected_line > 1)
4678 s->selected_line--;
4679 else if (s->selected_line == 1 &&
4680 s->first_displayed_line > 1)
4681 s->first_displayed_line--;
4682 break;
4683 case CTRL('u'):
4684 nscroll /= 2;
4685 /* FALL THROUGH */
4686 case KEY_PPAGE:
4687 case CTRL('b'):
4688 if (s->first_displayed_line == 1) {
4689 s->selected_line = MAX(1, s->selected_line - nscroll);
4690 break;
4692 if (s->first_displayed_line > nscroll)
4693 s->first_displayed_line -= nscroll;
4694 else
4695 s->first_displayed_line = 1;
4696 break;
4697 case 'j':
4698 case KEY_DOWN:
4699 case CTRL('n'):
4700 if (s->selected_line < view->nlines - 2 &&
4701 s->first_displayed_line +
4702 s->selected_line <= s->blame.nlines)
4703 s->selected_line++;
4704 else if (s->last_displayed_line <
4705 s->blame.nlines)
4706 s->first_displayed_line++;
4707 break;
4708 case 'b':
4709 case 'p': {
4710 struct got_object_id *id = NULL;
4711 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4712 s->first_displayed_line, s->selected_line);
4713 if (id == NULL)
4714 break;
4715 if (ch == 'p') {
4716 struct got_commit_object *commit, *pcommit;
4717 struct got_object_qid *pid;
4718 struct got_object_id *blob_id = NULL;
4719 int obj_type;
4720 err = got_object_open_as_commit(&commit,
4721 s->repo, id);
4722 if (err)
4723 break;
4724 pid = STAILQ_FIRST(
4725 got_object_commit_get_parent_ids(commit));
4726 if (pid == NULL) {
4727 got_object_commit_close(commit);
4728 break;
4730 /* Check if path history ends here. */
4731 err = got_object_open_as_commit(&pcommit,
4732 s->repo, &pid->id);
4733 if (err)
4734 break;
4735 err = got_object_id_by_path(&blob_id, s->repo,
4736 pcommit, s->path);
4737 got_object_commit_close(pcommit);
4738 if (err) {
4739 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4740 err = NULL;
4741 got_object_commit_close(commit);
4742 break;
4744 err = got_object_get_type(&obj_type, s->repo,
4745 blob_id);
4746 free(blob_id);
4747 /* Can't blame non-blob type objects. */
4748 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4749 got_object_commit_close(commit);
4750 break;
4752 err = got_object_qid_alloc(&s->blamed_commit,
4753 &pid->id);
4754 got_object_commit_close(commit);
4755 } else {
4756 if (got_object_id_cmp(id,
4757 &s->blamed_commit->id) == 0)
4758 break;
4759 err = got_object_qid_alloc(&s->blamed_commit,
4760 id);
4762 if (err)
4763 break;
4764 s->done = 1;
4765 thread_err = stop_blame(&s->blame);
4766 s->done = 0;
4767 if (thread_err)
4768 break;
4769 STAILQ_INSERT_HEAD(&s->blamed_commits,
4770 s->blamed_commit, entry);
4771 err = run_blame(view);
4772 if (err)
4773 break;
4774 break;
4776 case 'B': {
4777 struct got_object_qid *first;
4778 first = STAILQ_FIRST(&s->blamed_commits);
4779 if (!got_object_id_cmp(&first->id, s->commit_id))
4780 break;
4781 s->done = 1;
4782 thread_err = stop_blame(&s->blame);
4783 s->done = 0;
4784 if (thread_err)
4785 break;
4786 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4787 got_object_qid_free(s->blamed_commit);
4788 s->blamed_commit =
4789 STAILQ_FIRST(&s->blamed_commits);
4790 err = run_blame(view);
4791 if (err)
4792 break;
4793 break;
4795 case KEY_ENTER:
4796 case '\r': {
4797 struct got_object_id *id = NULL;
4798 struct got_object_qid *pid;
4799 struct got_commit_object *commit = NULL;
4800 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4801 s->first_displayed_line, s->selected_line);
4802 if (id == NULL)
4803 break;
4804 err = got_object_open_as_commit(&commit, s->repo, id);
4805 if (err)
4806 break;
4807 pid = STAILQ_FIRST(
4808 got_object_commit_get_parent_ids(commit));
4809 if (view_is_parent_view(view))
4810 begin_x = view_split_begin_x(view->begin_x);
4811 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4812 if (diff_view == NULL) {
4813 got_object_commit_close(commit);
4814 err = got_error_from_errno("view_open");
4815 break;
4817 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4818 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4819 got_object_commit_close(commit);
4820 if (err) {
4821 view_close(diff_view);
4822 break;
4824 view->focussed = 0;
4825 diff_view->focussed = 1;
4826 if (view_is_parent_view(view)) {
4827 err = view_close_child(view);
4828 if (err)
4829 break;
4830 view_set_child(view, diff_view);
4831 view->focus_child = 1;
4832 } else
4833 *new_view = diff_view;
4834 if (err)
4835 break;
4836 break;
4838 case CTRL('d'):
4839 nscroll /= 2;
4840 /* FALL THROUGH */
4841 case KEY_NPAGE:
4842 case CTRL('f'):
4843 case ' ':
4844 if (s->last_displayed_line >= s->blame.nlines &&
4845 s->selected_line >= MIN(s->blame.nlines,
4846 view->nlines - 2)) {
4847 break;
4849 if (s->last_displayed_line >= s->blame.nlines &&
4850 s->selected_line < view->nlines - 2) {
4851 s->selected_line +=
4852 MIN(nscroll, s->last_displayed_line -
4853 s->first_displayed_line - s->selected_line + 1);
4855 if (s->last_displayed_line + nscroll <= s->blame.nlines)
4856 s->first_displayed_line += nscroll;
4857 else
4858 s->first_displayed_line =
4859 s->blame.nlines - (view->nlines - 3);
4860 break;
4861 case KEY_RESIZE:
4862 if (s->selected_line > view->nlines - 2) {
4863 s->selected_line = MIN(s->blame.nlines,
4864 view->nlines - 2);
4866 break;
4867 default:
4868 break;
4870 return thread_err ? thread_err : err;
4873 static const struct got_error *
4874 cmd_blame(int argc, char *argv[])
4876 const struct got_error *error;
4877 struct got_repository *repo = NULL;
4878 struct got_worktree *worktree = NULL;
4879 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4880 char *link_target = NULL;
4881 struct got_object_id *commit_id = NULL;
4882 struct got_commit_object *commit = NULL;
4883 char *commit_id_str = NULL;
4884 int ch;
4885 struct tog_view *view;
4887 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4888 switch (ch) {
4889 case 'c':
4890 commit_id_str = optarg;
4891 break;
4892 case 'r':
4893 repo_path = realpath(optarg, NULL);
4894 if (repo_path == NULL)
4895 return got_error_from_errno2("realpath",
4896 optarg);
4897 break;
4898 default:
4899 usage_blame();
4900 /* NOTREACHED */
4904 argc -= optind;
4905 argv += optind;
4907 if (argc != 1)
4908 usage_blame();
4910 if (repo_path == NULL) {
4911 cwd = getcwd(NULL, 0);
4912 if (cwd == NULL)
4913 return got_error_from_errno("getcwd");
4914 error = got_worktree_open(&worktree, cwd);
4915 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4916 goto done;
4917 if (worktree)
4918 repo_path =
4919 strdup(got_worktree_get_repo_path(worktree));
4920 else
4921 repo_path = strdup(cwd);
4922 if (repo_path == NULL) {
4923 error = got_error_from_errno("strdup");
4924 goto done;
4928 error = got_repo_open(&repo, repo_path, NULL);
4929 if (error != NULL)
4930 goto done;
4932 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4933 worktree);
4934 if (error)
4935 goto done;
4937 init_curses();
4939 error = apply_unveil(got_repo_get_path(repo), NULL);
4940 if (error)
4941 goto done;
4943 error = tog_load_refs(repo, 0);
4944 if (error)
4945 goto done;
4947 if (commit_id_str == NULL) {
4948 struct got_reference *head_ref;
4949 error = got_ref_open(&head_ref, repo, worktree ?
4950 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4951 if (error != NULL)
4952 goto done;
4953 error = got_ref_resolve(&commit_id, repo, head_ref);
4954 got_ref_close(head_ref);
4955 } else {
4956 error = got_repo_match_object_id(&commit_id, NULL,
4957 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4959 if (error != NULL)
4960 goto done;
4962 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4963 if (view == NULL) {
4964 error = got_error_from_errno("view_open");
4965 goto done;
4968 error = got_object_open_as_commit(&commit, repo, commit_id);
4969 if (error)
4970 goto done;
4972 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4973 commit, repo);
4974 if (error)
4975 goto done;
4977 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4978 commit_id, repo);
4979 if (error)
4980 goto done;
4981 if (worktree) {
4982 /* Release work tree lock. */
4983 got_worktree_close(worktree);
4984 worktree = NULL;
4986 error = view_loop(view);
4987 done:
4988 free(repo_path);
4989 free(in_repo_path);
4990 free(link_target);
4991 free(cwd);
4992 free(commit_id);
4993 if (commit)
4994 got_object_commit_close(commit);
4995 if (worktree)
4996 got_worktree_close(worktree);
4997 if (repo) {
4998 const struct got_error *close_err = got_repo_close(repo);
4999 if (error == NULL)
5000 error = close_err;
5002 tog_free_refs();
5003 return error;
5006 static const struct got_error *
5007 draw_tree_entries(struct tog_view *view, const char *parent_path)
5009 struct tog_tree_view_state *s = &view->state.tree;
5010 const struct got_error *err = NULL;
5011 struct got_tree_entry *te;
5012 wchar_t *wline;
5013 struct tog_color *tc;
5014 int width, n, i, nentries;
5015 int limit = view->nlines;
5017 s->ndisplayed = 0;
5019 werase(view->window);
5021 if (limit == 0)
5022 return NULL;
5024 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5025 if (err)
5026 return err;
5027 if (view_needs_focus_indication(view))
5028 wstandout(view->window);
5029 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5030 if (tc)
5031 wattr_on(view->window,
5032 COLOR_PAIR(tc->colorpair), NULL);
5033 waddwstr(view->window, wline);
5034 if (tc)
5035 wattr_off(view->window,
5036 COLOR_PAIR(tc->colorpair), NULL);
5037 if (view_needs_focus_indication(view))
5038 wstandend(view->window);
5039 free(wline);
5040 wline = NULL;
5041 if (width < view->ncols - 1)
5042 waddch(view->window, '\n');
5043 if (--limit <= 0)
5044 return NULL;
5045 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5046 if (err)
5047 return err;
5048 waddwstr(view->window, wline);
5049 free(wline);
5050 wline = NULL;
5051 if (width < view->ncols - 1)
5052 waddch(view->window, '\n');
5053 if (--limit <= 0)
5054 return NULL;
5055 waddch(view->window, '\n');
5056 if (--limit <= 0)
5057 return NULL;
5059 if (s->first_displayed_entry == NULL) {
5060 te = got_object_tree_get_first_entry(s->tree);
5061 if (s->selected == 0) {
5062 if (view->focussed)
5063 wstandout(view->window);
5064 s->selected_entry = NULL;
5066 waddstr(view->window, " ..\n"); /* parent directory */
5067 if (s->selected == 0 && view->focussed)
5068 wstandend(view->window);
5069 s->ndisplayed++;
5070 if (--limit <= 0)
5071 return NULL;
5072 n = 1;
5073 } else {
5074 n = 0;
5075 te = s->first_displayed_entry;
5078 nentries = got_object_tree_get_nentries(s->tree);
5079 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5080 char *line = NULL, *id_str = NULL, *link_target = NULL;
5081 const char *modestr = "";
5082 mode_t mode;
5084 te = got_object_tree_get_entry(s->tree, i);
5085 mode = got_tree_entry_get_mode(te);
5087 if (s->show_ids) {
5088 err = got_object_id_str(&id_str,
5089 got_tree_entry_get_id(te));
5090 if (err)
5091 return got_error_from_errno(
5092 "got_object_id_str");
5094 if (got_object_tree_entry_is_submodule(te))
5095 modestr = "$";
5096 else if (S_ISLNK(mode)) {
5097 int i;
5099 err = got_tree_entry_get_symlink_target(&link_target,
5100 te, s->repo);
5101 if (err) {
5102 free(id_str);
5103 return err;
5105 for (i = 0; i < strlen(link_target); i++) {
5106 if (!isprint((unsigned char)link_target[i]))
5107 link_target[i] = '?';
5109 modestr = "@";
5111 else if (S_ISDIR(mode))
5112 modestr = "/";
5113 else if (mode & S_IXUSR)
5114 modestr = "*";
5115 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5116 got_tree_entry_get_name(te), modestr,
5117 link_target ? " -> ": "",
5118 link_target ? link_target : "") == -1) {
5119 free(id_str);
5120 free(link_target);
5121 return got_error_from_errno("asprintf");
5123 free(id_str);
5124 free(link_target);
5125 err = format_line(&wline, &width, line, view->ncols, 0);
5126 if (err) {
5127 free(line);
5128 break;
5130 if (n == s->selected) {
5131 if (view->focussed)
5132 wstandout(view->window);
5133 s->selected_entry = te;
5135 tc = match_color(&s->colors, line);
5136 if (tc)
5137 wattr_on(view->window,
5138 COLOR_PAIR(tc->colorpair), NULL);
5139 waddwstr(view->window, wline);
5140 if (tc)
5141 wattr_off(view->window,
5142 COLOR_PAIR(tc->colorpair), NULL);
5143 if (width < view->ncols - 1)
5144 waddch(view->window, '\n');
5145 if (n == s->selected && view->focussed)
5146 wstandend(view->window);
5147 free(line);
5148 free(wline);
5149 wline = NULL;
5150 n++;
5151 s->ndisplayed++;
5152 s->last_displayed_entry = te;
5153 if (--limit <= 0)
5154 break;
5157 return err;
5160 static void
5161 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5163 struct got_tree_entry *te;
5164 int isroot = s->tree == s->root;
5165 int i = 0;
5167 if (s->first_displayed_entry == NULL)
5168 return;
5170 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5171 while (i++ < maxscroll) {
5172 if (te == NULL) {
5173 if (!isroot)
5174 s->first_displayed_entry = NULL;
5175 break;
5177 s->first_displayed_entry = te;
5178 te = got_tree_entry_get_prev(s->tree, te);
5182 static void
5183 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5185 struct got_tree_entry *next, *last;
5186 int n = 0;
5188 if (s->first_displayed_entry)
5189 next = got_tree_entry_get_next(s->tree,
5190 s->first_displayed_entry);
5191 else
5192 next = got_object_tree_get_first_entry(s->tree);
5194 last = s->last_displayed_entry;
5195 while (next && last && n++ < maxscroll) {
5196 last = got_tree_entry_get_next(s->tree, last);
5197 if (last) {
5198 s->first_displayed_entry = next;
5199 next = got_tree_entry_get_next(s->tree, next);
5204 static const struct got_error *
5205 tree_entry_path(char **path, struct tog_parent_trees *parents,
5206 struct got_tree_entry *te)
5208 const struct got_error *err = NULL;
5209 struct tog_parent_tree *pt;
5210 size_t len = 2; /* for leading slash and NUL */
5212 TAILQ_FOREACH(pt, parents, entry)
5213 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5214 + 1 /* slash */;
5215 if (te)
5216 len += strlen(got_tree_entry_get_name(te));
5218 *path = calloc(1, len);
5219 if (path == NULL)
5220 return got_error_from_errno("calloc");
5222 (*path)[0] = '/';
5223 pt = TAILQ_LAST(parents, tog_parent_trees);
5224 while (pt) {
5225 const char *name = got_tree_entry_get_name(pt->selected_entry);
5226 if (strlcat(*path, name, len) >= len) {
5227 err = got_error(GOT_ERR_NO_SPACE);
5228 goto done;
5230 if (strlcat(*path, "/", len) >= len) {
5231 err = got_error(GOT_ERR_NO_SPACE);
5232 goto done;
5234 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5236 if (te) {
5237 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5238 err = got_error(GOT_ERR_NO_SPACE);
5239 goto done;
5242 done:
5243 if (err) {
5244 free(*path);
5245 *path = NULL;
5247 return err;
5250 static const struct got_error *
5251 blame_tree_entry(struct tog_view **new_view, int begin_x,
5252 struct got_tree_entry *te, struct tog_parent_trees *parents,
5253 struct got_object_id *commit_id, struct got_repository *repo)
5255 const struct got_error *err = NULL;
5256 char *path;
5257 struct tog_view *blame_view;
5259 *new_view = NULL;
5261 err = tree_entry_path(&path, parents, te);
5262 if (err)
5263 return err;
5265 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5266 if (blame_view == NULL) {
5267 err = got_error_from_errno("view_open");
5268 goto done;
5271 err = open_blame_view(blame_view, path, commit_id, repo);
5272 if (err) {
5273 if (err->code == GOT_ERR_CANCELLED)
5274 err = NULL;
5275 view_close(blame_view);
5276 } else
5277 *new_view = blame_view;
5278 done:
5279 free(path);
5280 return err;
5283 static const struct got_error *
5284 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5285 struct tog_tree_view_state *s)
5287 struct tog_view *log_view;
5288 const struct got_error *err = NULL;
5289 char *path;
5291 *new_view = NULL;
5293 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5294 if (log_view == NULL)
5295 return got_error_from_errno("view_open");
5297 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5298 if (err)
5299 return err;
5301 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5302 path, 0);
5303 if (err)
5304 view_close(log_view);
5305 else
5306 *new_view = log_view;
5307 free(path);
5308 return err;
5311 static const struct got_error *
5312 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5313 const char *head_ref_name, struct got_repository *repo)
5315 const struct got_error *err = NULL;
5316 char *commit_id_str = NULL;
5317 struct tog_tree_view_state *s = &view->state.tree;
5318 struct got_commit_object *commit = NULL;
5320 TAILQ_INIT(&s->parents);
5321 STAILQ_INIT(&s->colors);
5323 s->commit_id = got_object_id_dup(commit_id);
5324 if (s->commit_id == NULL)
5325 return got_error_from_errno("got_object_id_dup");
5327 err = got_object_open_as_commit(&commit, repo, commit_id);
5328 if (err)
5329 goto done;
5332 * The root is opened here and will be closed when the view is closed.
5333 * Any visited subtrees and their path-wise parents are opened and
5334 * closed on demand.
5336 err = got_object_open_as_tree(&s->root, repo,
5337 got_object_commit_get_tree_id(commit));
5338 if (err)
5339 goto done;
5340 s->tree = s->root;
5342 err = got_object_id_str(&commit_id_str, commit_id);
5343 if (err != NULL)
5344 goto done;
5346 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5347 err = got_error_from_errno("asprintf");
5348 goto done;
5351 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5352 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5353 if (head_ref_name) {
5354 s->head_ref_name = strdup(head_ref_name);
5355 if (s->head_ref_name == NULL) {
5356 err = got_error_from_errno("strdup");
5357 goto done;
5360 s->repo = repo;
5362 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5363 err = add_color(&s->colors, "\\$$",
5364 TOG_COLOR_TREE_SUBMODULE,
5365 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5366 if (err)
5367 goto done;
5368 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5369 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5370 if (err)
5371 goto done;
5372 err = add_color(&s->colors, "/$",
5373 TOG_COLOR_TREE_DIRECTORY,
5374 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5375 if (err)
5376 goto done;
5378 err = add_color(&s->colors, "\\*$",
5379 TOG_COLOR_TREE_EXECUTABLE,
5380 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5381 if (err)
5382 goto done;
5384 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5385 get_color_value("TOG_COLOR_COMMIT"));
5386 if (err)
5387 goto done;
5390 view->show = show_tree_view;
5391 view->input = input_tree_view;
5392 view->close = close_tree_view;
5393 view->search_start = search_start_tree_view;
5394 view->search_next = search_next_tree_view;
5395 done:
5396 free(commit_id_str);
5397 if (commit)
5398 got_object_commit_close(commit);
5399 if (err)
5400 close_tree_view(view);
5401 return err;
5404 static const struct got_error *
5405 close_tree_view(struct tog_view *view)
5407 struct tog_tree_view_state *s = &view->state.tree;
5409 free_colors(&s->colors);
5410 free(s->tree_label);
5411 s->tree_label = NULL;
5412 free(s->commit_id);
5413 s->commit_id = NULL;
5414 free(s->head_ref_name);
5415 s->head_ref_name = NULL;
5416 while (!TAILQ_EMPTY(&s->parents)) {
5417 struct tog_parent_tree *parent;
5418 parent = TAILQ_FIRST(&s->parents);
5419 TAILQ_REMOVE(&s->parents, parent, entry);
5420 if (parent->tree != s->root)
5421 got_object_tree_close(parent->tree);
5422 free(parent);
5425 if (s->tree != NULL && s->tree != s->root)
5426 got_object_tree_close(s->tree);
5427 if (s->root)
5428 got_object_tree_close(s->root);
5429 return NULL;
5432 static const struct got_error *
5433 search_start_tree_view(struct tog_view *view)
5435 struct tog_tree_view_state *s = &view->state.tree;
5437 s->matched_entry = NULL;
5438 return NULL;
5441 static int
5442 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5444 regmatch_t regmatch;
5446 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5447 0) == 0;
5450 static const struct got_error *
5451 search_next_tree_view(struct tog_view *view)
5453 struct tog_tree_view_state *s = &view->state.tree;
5454 struct got_tree_entry *te = NULL;
5456 if (!view->searching) {
5457 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5458 return NULL;
5461 if (s->matched_entry) {
5462 if (view->searching == TOG_SEARCH_FORWARD) {
5463 if (s->selected_entry)
5464 te = got_tree_entry_get_next(s->tree,
5465 s->selected_entry);
5466 else
5467 te = got_object_tree_get_first_entry(s->tree);
5468 } else {
5469 if (s->selected_entry == NULL)
5470 te = got_object_tree_get_last_entry(s->tree);
5471 else
5472 te = got_tree_entry_get_prev(s->tree,
5473 s->selected_entry);
5475 } else {
5476 if (s->selected_entry)
5477 te = s->selected_entry;
5478 else if (view->searching == TOG_SEARCH_FORWARD)
5479 te = got_object_tree_get_first_entry(s->tree);
5480 else
5481 te = got_object_tree_get_last_entry(s->tree);
5484 while (1) {
5485 if (te == NULL) {
5486 if (s->matched_entry == NULL) {
5487 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5488 return NULL;
5490 if (view->searching == TOG_SEARCH_FORWARD)
5491 te = got_object_tree_get_first_entry(s->tree);
5492 else
5493 te = got_object_tree_get_last_entry(s->tree);
5496 if (match_tree_entry(te, &view->regex)) {
5497 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5498 s->matched_entry = te;
5499 break;
5502 if (view->searching == TOG_SEARCH_FORWARD)
5503 te = got_tree_entry_get_next(s->tree, te);
5504 else
5505 te = got_tree_entry_get_prev(s->tree, te);
5508 if (s->matched_entry) {
5509 s->first_displayed_entry = s->matched_entry;
5510 s->selected = 0;
5513 return NULL;
5516 static const struct got_error *
5517 show_tree_view(struct tog_view *view)
5519 const struct got_error *err = NULL;
5520 struct tog_tree_view_state *s = &view->state.tree;
5521 char *parent_path;
5523 err = tree_entry_path(&parent_path, &s->parents, NULL);
5524 if (err)
5525 return err;
5527 err = draw_tree_entries(view, parent_path);
5528 free(parent_path);
5530 view_vborder(view);
5531 return err;
5534 static const struct got_error *
5535 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5537 const struct got_error *err = NULL;
5538 struct tog_tree_view_state *s = &view->state.tree;
5539 struct tog_view *log_view, *ref_view;
5540 struct got_tree_entry *te;
5541 int begin_x = 0, n, nscroll = view->nlines - 3;
5543 switch (ch) {
5544 case 'i':
5545 s->show_ids = !s->show_ids;
5546 break;
5547 case 'l':
5548 if (!s->selected_entry)
5549 break;
5550 if (view_is_parent_view(view))
5551 begin_x = view_split_begin_x(view->begin_x);
5552 err = log_selected_tree_entry(&log_view, begin_x, s);
5553 view->focussed = 0;
5554 log_view->focussed = 1;
5555 if (view_is_parent_view(view)) {
5556 err = view_close_child(view);
5557 if (err)
5558 return err;
5559 view_set_child(view, log_view);
5560 view->focus_child = 1;
5561 } else
5562 *new_view = log_view;
5563 break;
5564 case 'r':
5565 if (view_is_parent_view(view))
5566 begin_x = view_split_begin_x(view->begin_x);
5567 ref_view = view_open(view->nlines, view->ncols,
5568 view->begin_y, begin_x, TOG_VIEW_REF);
5569 if (ref_view == NULL)
5570 return got_error_from_errno("view_open");
5571 err = open_ref_view(ref_view, s->repo);
5572 if (err) {
5573 view_close(ref_view);
5574 return err;
5576 view->focussed = 0;
5577 ref_view->focussed = 1;
5578 if (view_is_parent_view(view)) {
5579 err = view_close_child(view);
5580 if (err)
5581 return err;
5582 view_set_child(view, ref_view);
5583 view->focus_child = 1;
5584 } else
5585 *new_view = ref_view;
5586 break;
5587 case 'g':
5588 case KEY_HOME:
5589 s->selected = 0;
5590 if (s->tree == s->root)
5591 s->first_displayed_entry =
5592 got_object_tree_get_first_entry(s->tree);
5593 else
5594 s->first_displayed_entry = NULL;
5595 break;
5596 case 'G':
5597 case KEY_END:
5598 s->selected = 0;
5599 te = got_object_tree_get_last_entry(s->tree);
5600 for (n = 0; n < view->nlines - 3; n++) {
5601 if (te == NULL) {
5602 if(s->tree != s->root) {
5603 s->first_displayed_entry = NULL;
5604 n++;
5606 break;
5608 s->first_displayed_entry = te;
5609 te = got_tree_entry_get_prev(s->tree, te);
5611 if (n > 0)
5612 s->selected = n - 1;
5613 break;
5614 case 'k':
5615 case KEY_UP:
5616 case CTRL('p'):
5617 if (s->selected > 0) {
5618 s->selected--;
5619 break;
5621 tree_scroll_up(s, 1);
5622 break;
5623 case CTRL('u'):
5624 nscroll /= 2;
5625 /* FALL THROUGH */
5626 case KEY_PPAGE:
5627 case CTRL('b'):
5628 if (s->tree == s->root) {
5629 if (got_object_tree_get_first_entry(s->tree) ==
5630 s->first_displayed_entry)
5631 s->selected -= MIN(s->selected, nscroll);
5632 } else {
5633 if (s->first_displayed_entry == NULL)
5634 s->selected -= MIN(s->selected, nscroll);
5636 tree_scroll_up(s, MAX(0, nscroll));
5637 break;
5638 case 'j':
5639 case KEY_DOWN:
5640 case CTRL('n'):
5641 if (s->selected < s->ndisplayed - 1) {
5642 s->selected++;
5643 break;
5645 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5646 == NULL)
5647 /* can't scroll any further */
5648 break;
5649 tree_scroll_down(s, 1);
5650 break;
5651 case CTRL('d'):
5652 nscroll /= 2;
5653 /* FALL THROUGH */
5654 case KEY_NPAGE:
5655 case CTRL('f'):
5656 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5657 == NULL) {
5658 /* can't scroll any further; move cursor down */
5659 if (s->selected < s->ndisplayed - 1)
5660 s->selected += MIN(nscroll,
5661 s->ndisplayed - s->selected - 1);
5662 break;
5664 tree_scroll_down(s, nscroll);
5665 break;
5666 case KEY_ENTER:
5667 case '\r':
5668 case KEY_BACKSPACE:
5669 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5670 struct tog_parent_tree *parent;
5671 /* user selected '..' */
5672 if (s->tree == s->root)
5673 break;
5674 parent = TAILQ_FIRST(&s->parents);
5675 TAILQ_REMOVE(&s->parents, parent,
5676 entry);
5677 got_object_tree_close(s->tree);
5678 s->tree = parent->tree;
5679 s->first_displayed_entry =
5680 parent->first_displayed_entry;
5681 s->selected_entry =
5682 parent->selected_entry;
5683 s->selected = parent->selected;
5684 free(parent);
5685 } else if (S_ISDIR(got_tree_entry_get_mode(
5686 s->selected_entry))) {
5687 struct got_tree_object *subtree;
5688 err = got_object_open_as_tree(&subtree, s->repo,
5689 got_tree_entry_get_id(s->selected_entry));
5690 if (err)
5691 break;
5692 err = tree_view_visit_subtree(s, subtree);
5693 if (err) {
5694 got_object_tree_close(subtree);
5695 break;
5697 } else if (S_ISREG(got_tree_entry_get_mode(
5698 s->selected_entry))) {
5699 struct tog_view *blame_view;
5700 int begin_x = view_is_parent_view(view) ?
5701 view_split_begin_x(view->begin_x) : 0;
5703 err = blame_tree_entry(&blame_view, begin_x,
5704 s->selected_entry, &s->parents,
5705 s->commit_id, s->repo);
5706 if (err)
5707 break;
5708 view->focussed = 0;
5709 blame_view->focussed = 1;
5710 if (view_is_parent_view(view)) {
5711 err = view_close_child(view);
5712 if (err)
5713 return err;
5714 view_set_child(view, blame_view);
5715 view->focus_child = 1;
5716 } else
5717 *new_view = blame_view;
5719 break;
5720 case KEY_RESIZE:
5721 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5722 s->selected = view->nlines - 4;
5723 break;
5724 default:
5725 break;
5728 return err;
5731 __dead static void
5732 usage_tree(void)
5734 endwin();
5735 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5736 getprogname());
5737 exit(1);
5740 static const struct got_error *
5741 cmd_tree(int argc, char *argv[])
5743 const struct got_error *error;
5744 struct got_repository *repo = NULL;
5745 struct got_worktree *worktree = NULL;
5746 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5747 struct got_object_id *commit_id = NULL;
5748 struct got_commit_object *commit = NULL;
5749 const char *commit_id_arg = NULL;
5750 char *label = NULL;
5751 struct got_reference *ref = NULL;
5752 const char *head_ref_name = NULL;
5753 int ch;
5754 struct tog_view *view;
5756 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5757 switch (ch) {
5758 case 'c':
5759 commit_id_arg = optarg;
5760 break;
5761 case 'r':
5762 repo_path = realpath(optarg, NULL);
5763 if (repo_path == NULL)
5764 return got_error_from_errno2("realpath",
5765 optarg);
5766 break;
5767 default:
5768 usage_tree();
5769 /* NOTREACHED */
5773 argc -= optind;
5774 argv += optind;
5776 if (argc > 1)
5777 usage_tree();
5779 if (repo_path == NULL) {
5780 cwd = getcwd(NULL, 0);
5781 if (cwd == NULL)
5782 return got_error_from_errno("getcwd");
5783 error = got_worktree_open(&worktree, cwd);
5784 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5785 goto done;
5786 if (worktree)
5787 repo_path =
5788 strdup(got_worktree_get_repo_path(worktree));
5789 else
5790 repo_path = strdup(cwd);
5791 if (repo_path == NULL) {
5792 error = got_error_from_errno("strdup");
5793 goto done;
5797 error = got_repo_open(&repo, repo_path, NULL);
5798 if (error != NULL)
5799 goto done;
5801 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5802 repo, worktree);
5803 if (error)
5804 goto done;
5806 init_curses();
5808 error = apply_unveil(got_repo_get_path(repo), NULL);
5809 if (error)
5810 goto done;
5812 error = tog_load_refs(repo, 0);
5813 if (error)
5814 goto done;
5816 if (commit_id_arg == NULL) {
5817 error = got_repo_match_object_id(&commit_id, &label,
5818 worktree ? got_worktree_get_head_ref_name(worktree) :
5819 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5820 if (error)
5821 goto done;
5822 head_ref_name = label;
5823 } else {
5824 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5825 if (error == NULL)
5826 head_ref_name = got_ref_get_name(ref);
5827 else if (error->code != GOT_ERR_NOT_REF)
5828 goto done;
5829 error = got_repo_match_object_id(&commit_id, NULL,
5830 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5831 if (error)
5832 goto done;
5835 error = got_object_open_as_commit(&commit, repo, commit_id);
5836 if (error)
5837 goto done;
5839 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5840 if (view == NULL) {
5841 error = got_error_from_errno("view_open");
5842 goto done;
5844 error = open_tree_view(view, commit_id, head_ref_name, repo);
5845 if (error)
5846 goto done;
5847 if (!got_path_is_root_dir(in_repo_path)) {
5848 error = tree_view_walk_path(&view->state.tree, commit,
5849 in_repo_path);
5850 if (error)
5851 goto done;
5854 if (worktree) {
5855 /* Release work tree lock. */
5856 got_worktree_close(worktree);
5857 worktree = NULL;
5859 error = view_loop(view);
5860 done:
5861 free(repo_path);
5862 free(cwd);
5863 free(commit_id);
5864 free(label);
5865 if (ref)
5866 got_ref_close(ref);
5867 if (repo) {
5868 const struct got_error *close_err = got_repo_close(repo);
5869 if (error == NULL)
5870 error = close_err;
5872 tog_free_refs();
5873 return error;
5876 static const struct got_error *
5877 ref_view_load_refs(struct tog_ref_view_state *s)
5879 struct got_reflist_entry *sre;
5880 struct tog_reflist_entry *re;
5882 s->nrefs = 0;
5883 TAILQ_FOREACH(sre, &tog_refs, entry) {
5884 if (strncmp(got_ref_get_name(sre->ref),
5885 "refs/got/", 9) == 0 &&
5886 strncmp(got_ref_get_name(sre->ref),
5887 "refs/got/backup/", 16) != 0)
5888 continue;
5890 re = malloc(sizeof(*re));
5891 if (re == NULL)
5892 return got_error_from_errno("malloc");
5894 re->ref = got_ref_dup(sre->ref);
5895 if (re->ref == NULL)
5896 return got_error_from_errno("got_ref_dup");
5897 re->idx = s->nrefs++;
5898 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5901 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5902 return NULL;
5905 void
5906 ref_view_free_refs(struct tog_ref_view_state *s)
5908 struct tog_reflist_entry *re;
5910 while (!TAILQ_EMPTY(&s->refs)) {
5911 re = TAILQ_FIRST(&s->refs);
5912 TAILQ_REMOVE(&s->refs, re, entry);
5913 got_ref_close(re->ref);
5914 free(re);
5918 static const struct got_error *
5919 open_ref_view(struct tog_view *view, struct got_repository *repo)
5921 const struct got_error *err = NULL;
5922 struct tog_ref_view_state *s = &view->state.ref;
5924 s->selected_entry = 0;
5925 s->repo = repo;
5927 TAILQ_INIT(&s->refs);
5928 STAILQ_INIT(&s->colors);
5930 err = ref_view_load_refs(s);
5931 if (err)
5932 return err;
5934 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5935 err = add_color(&s->colors, "^refs/heads/",
5936 TOG_COLOR_REFS_HEADS,
5937 get_color_value("TOG_COLOR_REFS_HEADS"));
5938 if (err)
5939 goto done;
5941 err = add_color(&s->colors, "^refs/tags/",
5942 TOG_COLOR_REFS_TAGS,
5943 get_color_value("TOG_COLOR_REFS_TAGS"));
5944 if (err)
5945 goto done;
5947 err = add_color(&s->colors, "^refs/remotes/",
5948 TOG_COLOR_REFS_REMOTES,
5949 get_color_value("TOG_COLOR_REFS_REMOTES"));
5950 if (err)
5951 goto done;
5953 err = add_color(&s->colors, "^refs/got/backup/",
5954 TOG_COLOR_REFS_BACKUP,
5955 get_color_value("TOG_COLOR_REFS_BACKUP"));
5956 if (err)
5957 goto done;
5960 view->show = show_ref_view;
5961 view->input = input_ref_view;
5962 view->close = close_ref_view;
5963 view->search_start = search_start_ref_view;
5964 view->search_next = search_next_ref_view;
5965 done:
5966 if (err)
5967 free_colors(&s->colors);
5968 return err;
5971 static const struct got_error *
5972 close_ref_view(struct tog_view *view)
5974 struct tog_ref_view_state *s = &view->state.ref;
5976 ref_view_free_refs(s);
5977 free_colors(&s->colors);
5979 return NULL;
5982 static const struct got_error *
5983 resolve_reflist_entry(struct got_object_id **commit_id,
5984 struct tog_reflist_entry *re, struct got_repository *repo)
5986 const struct got_error *err = NULL;
5987 struct got_object_id *obj_id;
5988 struct got_tag_object *tag = NULL;
5989 int obj_type;
5991 *commit_id = NULL;
5993 err = got_ref_resolve(&obj_id, repo, re->ref);
5994 if (err)
5995 return err;
5997 err = got_object_get_type(&obj_type, repo, obj_id);
5998 if (err)
5999 goto done;
6001 switch (obj_type) {
6002 case GOT_OBJ_TYPE_COMMIT:
6003 *commit_id = obj_id;
6004 break;
6005 case GOT_OBJ_TYPE_TAG:
6006 err = got_object_open_as_tag(&tag, repo, obj_id);
6007 if (err)
6008 goto done;
6009 free(obj_id);
6010 err = got_object_get_type(&obj_type, repo,
6011 got_object_tag_get_object_id(tag));
6012 if (err)
6013 goto done;
6014 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6015 err = got_error(GOT_ERR_OBJ_TYPE);
6016 goto done;
6018 *commit_id = got_object_id_dup(
6019 got_object_tag_get_object_id(tag));
6020 if (*commit_id == NULL) {
6021 err = got_error_from_errno("got_object_id_dup");
6022 goto done;
6024 break;
6025 default:
6026 err = got_error(GOT_ERR_OBJ_TYPE);
6027 break;
6030 done:
6031 if (tag)
6032 got_object_tag_close(tag);
6033 if (err) {
6034 free(*commit_id);
6035 *commit_id = NULL;
6037 return err;
6040 static const struct got_error *
6041 log_ref_entry(struct tog_view **new_view, int begin_x,
6042 struct tog_reflist_entry *re, struct got_repository *repo)
6044 struct tog_view *log_view;
6045 const struct got_error *err = NULL;
6046 struct got_object_id *commit_id = NULL;
6048 *new_view = NULL;
6050 err = resolve_reflist_entry(&commit_id, re, repo);
6051 if (err) {
6052 if (err->code != GOT_ERR_OBJ_TYPE)
6053 return err;
6054 else
6055 return NULL;
6058 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6059 if (log_view == NULL) {
6060 err = got_error_from_errno("view_open");
6061 goto done;
6064 err = open_log_view(log_view, commit_id, repo,
6065 got_ref_get_name(re->ref), "", 0);
6066 done:
6067 if (err)
6068 view_close(log_view);
6069 else
6070 *new_view = log_view;
6071 free(commit_id);
6072 return err;
6075 static void
6076 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6078 struct tog_reflist_entry *re;
6079 int i = 0;
6081 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6082 return;
6084 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6085 while (i++ < maxscroll) {
6086 if (re == NULL)
6087 break;
6088 s->first_displayed_entry = re;
6089 re = TAILQ_PREV(re, tog_reflist_head, entry);
6093 static void
6094 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6096 struct tog_reflist_entry *next, *last;
6097 int n = 0;
6099 if (s->first_displayed_entry)
6100 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6101 else
6102 next = TAILQ_FIRST(&s->refs);
6104 last = s->last_displayed_entry;
6105 while (next && last && n++ < maxscroll) {
6106 last = TAILQ_NEXT(last, entry);
6107 if (last) {
6108 s->first_displayed_entry = next;
6109 next = TAILQ_NEXT(next, entry);
6114 static const struct got_error *
6115 search_start_ref_view(struct tog_view *view)
6117 struct tog_ref_view_state *s = &view->state.ref;
6119 s->matched_entry = NULL;
6120 return NULL;
6123 static int
6124 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6126 regmatch_t regmatch;
6128 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6129 0) == 0;
6132 static const struct got_error *
6133 search_next_ref_view(struct tog_view *view)
6135 struct tog_ref_view_state *s = &view->state.ref;
6136 struct tog_reflist_entry *re = NULL;
6138 if (!view->searching) {
6139 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6140 return NULL;
6143 if (s->matched_entry) {
6144 if (view->searching == TOG_SEARCH_FORWARD) {
6145 if (s->selected_entry)
6146 re = TAILQ_NEXT(s->selected_entry, entry);
6147 else
6148 re = TAILQ_PREV(s->selected_entry,
6149 tog_reflist_head, entry);
6150 } else {
6151 if (s->selected_entry == NULL)
6152 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6153 else
6154 re = TAILQ_PREV(s->selected_entry,
6155 tog_reflist_head, entry);
6157 } else {
6158 if (s->selected_entry)
6159 re = s->selected_entry;
6160 else if (view->searching == TOG_SEARCH_FORWARD)
6161 re = TAILQ_FIRST(&s->refs);
6162 else
6163 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6166 while (1) {
6167 if (re == NULL) {
6168 if (s->matched_entry == NULL) {
6169 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6170 return NULL;
6172 if (view->searching == TOG_SEARCH_FORWARD)
6173 re = TAILQ_FIRST(&s->refs);
6174 else
6175 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6178 if (match_reflist_entry(re, &view->regex)) {
6179 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6180 s->matched_entry = re;
6181 break;
6184 if (view->searching == TOG_SEARCH_FORWARD)
6185 re = TAILQ_NEXT(re, entry);
6186 else
6187 re = TAILQ_PREV(re, tog_reflist_head, entry);
6190 if (s->matched_entry) {
6191 s->first_displayed_entry = s->matched_entry;
6192 s->selected = 0;
6195 return NULL;
6198 static const struct got_error *
6199 show_ref_view(struct tog_view *view)
6201 const struct got_error *err = NULL;
6202 struct tog_ref_view_state *s = &view->state.ref;
6203 struct tog_reflist_entry *re;
6204 char *line = NULL;
6205 wchar_t *wline;
6206 struct tog_color *tc;
6207 int width, n;
6208 int limit = view->nlines;
6210 werase(view->window);
6212 s->ndisplayed = 0;
6214 if (limit == 0)
6215 return NULL;
6217 re = s->first_displayed_entry;
6219 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6220 s->nrefs) == -1)
6221 return got_error_from_errno("asprintf");
6223 err = format_line(&wline, &width, line, view->ncols, 0);
6224 if (err) {
6225 free(line);
6226 return err;
6228 if (view_needs_focus_indication(view))
6229 wstandout(view->window);
6230 waddwstr(view->window, wline);
6231 if (view_needs_focus_indication(view))
6232 wstandend(view->window);
6233 free(wline);
6234 wline = NULL;
6235 free(line);
6236 line = NULL;
6237 if (width < view->ncols - 1)
6238 waddch(view->window, '\n');
6239 if (--limit <= 0)
6240 return NULL;
6242 n = 0;
6243 while (re && limit > 0) {
6244 char *line = NULL;
6246 if (got_ref_is_symbolic(re->ref)) {
6247 if (asprintf(&line, "%s -> %s",
6248 got_ref_get_name(re->ref),
6249 got_ref_get_symref_target(re->ref)) == -1)
6250 return got_error_from_errno("asprintf");
6251 } else if (s->show_ids) {
6252 struct got_object_id *id;
6253 char *id_str;
6254 err = got_ref_resolve(&id, s->repo, re->ref);
6255 if (err)
6256 return err;
6257 err = got_object_id_str(&id_str, id);
6258 if (err) {
6259 free(id);
6260 return err;
6262 if (asprintf(&line, "%s: %s",
6263 got_ref_get_name(re->ref), id_str) == -1) {
6264 err = got_error_from_errno("asprintf");
6265 free(id);
6266 free(id_str);
6267 return err;
6269 free(id);
6270 free(id_str);
6271 } else {
6272 line = strdup(got_ref_get_name(re->ref));
6273 if (line == NULL)
6274 return got_error_from_errno("strdup");
6277 err = format_line(&wline, &width, line, view->ncols, 0);
6278 if (err) {
6279 free(line);
6280 return err;
6282 if (n == s->selected) {
6283 if (view->focussed)
6284 wstandout(view->window);
6285 s->selected_entry = re;
6287 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6288 if (tc)
6289 wattr_on(view->window,
6290 COLOR_PAIR(tc->colorpair), NULL);
6291 waddwstr(view->window, wline);
6292 if (tc)
6293 wattr_off(view->window,
6294 COLOR_PAIR(tc->colorpair), NULL);
6295 if (width < view->ncols - 1)
6296 waddch(view->window, '\n');
6297 if (n == s->selected && view->focussed)
6298 wstandend(view->window);
6299 free(line);
6300 free(wline);
6301 wline = NULL;
6302 n++;
6303 s->ndisplayed++;
6304 s->last_displayed_entry = re;
6306 limit--;
6307 re = TAILQ_NEXT(re, entry);
6310 view_vborder(view);
6311 return err;
6314 static const struct got_error *
6315 browse_ref_tree(struct tog_view **new_view, int begin_x,
6316 struct tog_reflist_entry *re, struct got_repository *repo)
6318 const struct got_error *err = NULL;
6319 struct got_object_id *commit_id = NULL;
6320 struct tog_view *tree_view;
6322 *new_view = NULL;
6324 err = resolve_reflist_entry(&commit_id, re, repo);
6325 if (err) {
6326 if (err->code != GOT_ERR_OBJ_TYPE)
6327 return err;
6328 else
6329 return NULL;
6333 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6334 if (tree_view == NULL) {
6335 err = got_error_from_errno("view_open");
6336 goto done;
6339 err = open_tree_view(tree_view, commit_id,
6340 got_ref_get_name(re->ref), repo);
6341 if (err)
6342 goto done;
6344 *new_view = tree_view;
6345 done:
6346 free(commit_id);
6347 return err;
6349 static const struct got_error *
6350 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6352 const struct got_error *err = NULL;
6353 struct tog_ref_view_state *s = &view->state.ref;
6354 struct tog_view *log_view, *tree_view;
6355 struct tog_reflist_entry *re;
6356 int begin_x = 0, n, nscroll = view->nlines - 1;
6358 switch (ch) {
6359 case 'i':
6360 s->show_ids = !s->show_ids;
6361 break;
6362 case 'o':
6363 s->sort_by_date = !s->sort_by_date;
6364 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6365 got_ref_cmp_by_commit_timestamp_descending :
6366 tog_ref_cmp_by_name, s->repo);
6367 if (err)
6368 break;
6369 got_reflist_object_id_map_free(tog_refs_idmap);
6370 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6371 &tog_refs, s->repo);
6372 if (err)
6373 break;
6374 ref_view_free_refs(s);
6375 err = ref_view_load_refs(s);
6376 break;
6377 case KEY_ENTER:
6378 case '\r':
6379 if (!s->selected_entry)
6380 break;
6381 if (view_is_parent_view(view))
6382 begin_x = view_split_begin_x(view->begin_x);
6383 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6384 s->repo);
6385 view->focussed = 0;
6386 log_view->focussed = 1;
6387 if (view_is_parent_view(view)) {
6388 err = view_close_child(view);
6389 if (err)
6390 return err;
6391 view_set_child(view, log_view);
6392 view->focus_child = 1;
6393 } else
6394 *new_view = log_view;
6395 break;
6396 case 't':
6397 if (!s->selected_entry)
6398 break;
6399 if (view_is_parent_view(view))
6400 begin_x = view_split_begin_x(view->begin_x);
6401 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6402 s->repo);
6403 if (err || tree_view == NULL)
6404 break;
6405 view->focussed = 0;
6406 tree_view->focussed = 1;
6407 if (view_is_parent_view(view)) {
6408 err = view_close_child(view);
6409 if (err)
6410 return err;
6411 view_set_child(view, tree_view);
6412 view->focus_child = 1;
6413 } else
6414 *new_view = tree_view;
6415 break;
6416 case 'g':
6417 case KEY_HOME:
6418 s->selected = 0;
6419 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6420 break;
6421 case 'G':
6422 case KEY_END:
6423 s->selected = 0;
6424 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6425 for (n = 0; n < view->nlines - 1; n++) {
6426 if (re == NULL)
6427 break;
6428 s->first_displayed_entry = re;
6429 re = TAILQ_PREV(re, tog_reflist_head, entry);
6431 if (n > 0)
6432 s->selected = n - 1;
6433 break;
6434 case 'k':
6435 case KEY_UP:
6436 case CTRL('p'):
6437 if (s->selected > 0) {
6438 s->selected--;
6439 break;
6441 ref_scroll_up(s, 1);
6442 break;
6443 case CTRL('u'):
6444 nscroll /= 2;
6445 /* FALL THROUGH */
6446 case KEY_PPAGE:
6447 case CTRL('b'):
6448 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6449 s->selected -= MIN(nscroll, s->selected);
6450 ref_scroll_up(s, MAX(0, nscroll));
6451 break;
6452 case 'j':
6453 case KEY_DOWN:
6454 case CTRL('n'):
6455 if (s->selected < s->ndisplayed - 1) {
6456 s->selected++;
6457 break;
6459 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6460 /* can't scroll any further */
6461 break;
6462 ref_scroll_down(s, 1);
6463 break;
6464 case CTRL('d'):
6465 nscroll /= 2;
6466 /* FALL THROUGH */
6467 case KEY_NPAGE:
6468 case CTRL('f'):
6469 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6470 /* can't scroll any further; move cursor down */
6471 if (s->selected < s->ndisplayed - 1)
6472 s->selected += MIN(nscroll,
6473 s->ndisplayed - s->selected - 1);
6474 break;
6476 ref_scroll_down(s, nscroll);
6477 break;
6478 case CTRL('l'):
6479 tog_free_refs();
6480 err = tog_load_refs(s->repo, s->sort_by_date);
6481 if (err)
6482 break;
6483 ref_view_free_refs(s);
6484 err = ref_view_load_refs(s);
6485 break;
6486 case KEY_RESIZE:
6487 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6488 s->selected = view->nlines - 2;
6489 break;
6490 default:
6491 break;
6494 return err;
6497 __dead static void
6498 usage_ref(void)
6500 endwin();
6501 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6502 getprogname());
6503 exit(1);
6506 static const struct got_error *
6507 cmd_ref(int argc, char *argv[])
6509 const struct got_error *error;
6510 struct got_repository *repo = NULL;
6511 struct got_worktree *worktree = NULL;
6512 char *cwd = NULL, *repo_path = NULL;
6513 int ch;
6514 struct tog_view *view;
6516 while ((ch = getopt(argc, argv, "r:")) != -1) {
6517 switch (ch) {
6518 case 'r':
6519 repo_path = realpath(optarg, NULL);
6520 if (repo_path == NULL)
6521 return got_error_from_errno2("realpath",
6522 optarg);
6523 break;
6524 default:
6525 usage_ref();
6526 /* NOTREACHED */
6530 argc -= optind;
6531 argv += optind;
6533 if (argc > 1)
6534 usage_ref();
6536 if (repo_path == NULL) {
6537 cwd = getcwd(NULL, 0);
6538 if (cwd == NULL)
6539 return got_error_from_errno("getcwd");
6540 error = got_worktree_open(&worktree, cwd);
6541 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6542 goto done;
6543 if (worktree)
6544 repo_path =
6545 strdup(got_worktree_get_repo_path(worktree));
6546 else
6547 repo_path = strdup(cwd);
6548 if (repo_path == NULL) {
6549 error = got_error_from_errno("strdup");
6550 goto done;
6554 error = got_repo_open(&repo, repo_path, NULL);
6555 if (error != NULL)
6556 goto done;
6558 init_curses();
6560 error = apply_unveil(got_repo_get_path(repo), NULL);
6561 if (error)
6562 goto done;
6564 error = tog_load_refs(repo, 0);
6565 if (error)
6566 goto done;
6568 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6569 if (view == NULL) {
6570 error = got_error_from_errno("view_open");
6571 goto done;
6574 error = open_ref_view(view, repo);
6575 if (error)
6576 goto done;
6578 if (worktree) {
6579 /* Release work tree lock. */
6580 got_worktree_close(worktree);
6581 worktree = NULL;
6583 error = view_loop(view);
6584 done:
6585 free(repo_path);
6586 free(cwd);
6587 if (repo) {
6588 const struct got_error *close_err = got_repo_close(repo);
6589 if (close_err)
6590 error = close_err;
6592 tog_free_refs();
6593 return error;
6596 static void
6597 list_commands(FILE *fp)
6599 size_t i;
6601 fprintf(fp, "commands:");
6602 for (i = 0; i < nitems(tog_commands); i++) {
6603 const struct tog_cmd *cmd = &tog_commands[i];
6604 fprintf(fp, " %s", cmd->name);
6606 fputc('\n', fp);
6609 __dead static void
6610 usage(int hflag, int status)
6612 FILE *fp = (status == 0) ? stdout : stderr;
6614 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6615 getprogname());
6616 if (hflag) {
6617 fprintf(fp, "lazy usage: %s path\n", getprogname());
6618 list_commands(fp);
6620 exit(status);
6623 static char **
6624 make_argv(int argc, ...)
6626 va_list ap;
6627 char **argv;
6628 int i;
6630 va_start(ap, argc);
6632 argv = calloc(argc, sizeof(char *));
6633 if (argv == NULL)
6634 err(1, "calloc");
6635 for (i = 0; i < argc; i++) {
6636 argv[i] = strdup(va_arg(ap, char *));
6637 if (argv[i] == NULL)
6638 err(1, "strdup");
6641 va_end(ap);
6642 return argv;
6646 * Try to convert 'tog path' into a 'tog log path' command.
6647 * The user could simply have mistyped the command rather than knowingly
6648 * provided a path. So check whether argv[0] can in fact be resolved
6649 * to a path in the HEAD commit and print a special error if not.
6650 * This hack is for mpi@ <3
6652 static const struct got_error *
6653 tog_log_with_path(int argc, char *argv[])
6655 const struct got_error *error = NULL, *close_err;
6656 const struct tog_cmd *cmd = NULL;
6657 struct got_repository *repo = NULL;
6658 struct got_worktree *worktree = NULL;
6659 struct got_object_id *commit_id = NULL, *id = NULL;
6660 struct got_commit_object *commit = NULL;
6661 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6662 char *commit_id_str = NULL, **cmd_argv = NULL;
6664 cwd = getcwd(NULL, 0);
6665 if (cwd == NULL)
6666 return got_error_from_errno("getcwd");
6668 error = got_worktree_open(&worktree, cwd);
6669 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6670 goto done;
6672 if (worktree)
6673 repo_path = strdup(got_worktree_get_repo_path(worktree));
6674 else
6675 repo_path = strdup(cwd);
6676 if (repo_path == NULL) {
6677 error = got_error_from_errno("strdup");
6678 goto done;
6681 error = got_repo_open(&repo, repo_path, NULL);
6682 if (error != NULL)
6683 goto done;
6685 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6686 repo, worktree);
6687 if (error)
6688 goto done;
6690 error = tog_load_refs(repo, 0);
6691 if (error)
6692 goto done;
6693 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6694 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6695 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6696 if (error)
6697 goto done;
6699 if (worktree) {
6700 got_worktree_close(worktree);
6701 worktree = NULL;
6704 error = got_object_open_as_commit(&commit, repo, commit_id);
6705 if (error)
6706 goto done;
6708 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6709 if (error) {
6710 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6711 goto done;
6712 fprintf(stderr, "%s: '%s' is no known command or path\n",
6713 getprogname(), argv[0]);
6714 usage(1, 1);
6715 /* not reached */
6718 close_err = got_repo_close(repo);
6719 if (error == NULL)
6720 error = close_err;
6721 repo = NULL;
6723 error = got_object_id_str(&commit_id_str, commit_id);
6724 if (error)
6725 goto done;
6727 cmd = &tog_commands[0]; /* log */
6728 argc = 4;
6729 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6730 error = cmd->cmd_main(argc, cmd_argv);
6731 done:
6732 if (repo) {
6733 close_err = got_repo_close(repo);
6734 if (error == NULL)
6735 error = close_err;
6737 if (commit)
6738 got_object_commit_close(commit);
6739 if (worktree)
6740 got_worktree_close(worktree);
6741 free(id);
6742 free(commit_id_str);
6743 free(commit_id);
6744 free(cwd);
6745 free(repo_path);
6746 free(in_repo_path);
6747 if (cmd_argv) {
6748 int i;
6749 for (i = 0; i < argc; i++)
6750 free(cmd_argv[i]);
6751 free(cmd_argv);
6753 tog_free_refs();
6754 return error;
6757 int
6758 main(int argc, char *argv[])
6760 const struct got_error *error = NULL;
6761 const struct tog_cmd *cmd = NULL;
6762 int ch, hflag = 0, Vflag = 0;
6763 char **cmd_argv = NULL;
6764 static const struct option longopts[] = {
6765 { "version", no_argument, NULL, 'V' },
6766 { NULL, 0, NULL, 0}
6769 setlocale(LC_CTYPE, "");
6771 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6772 switch (ch) {
6773 case 'h':
6774 hflag = 1;
6775 break;
6776 case 'V':
6777 Vflag = 1;
6778 break;
6779 default:
6780 usage(hflag, 1);
6781 /* NOTREACHED */
6785 argc -= optind;
6786 argv += optind;
6787 optind = 1;
6788 optreset = 1;
6790 if (Vflag) {
6791 got_version_print_str();
6792 return 0;
6795 #ifndef PROFILE
6796 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6797 NULL) == -1)
6798 err(1, "pledge");
6799 #endif
6801 if (argc == 0) {
6802 if (hflag)
6803 usage(hflag, 0);
6804 /* Build an argument vector which runs a default command. */
6805 cmd = &tog_commands[0];
6806 argc = 1;
6807 cmd_argv = make_argv(argc, cmd->name);
6808 } else {
6809 size_t i;
6811 /* Did the user specify a command? */
6812 for (i = 0; i < nitems(tog_commands); i++) {
6813 if (strncmp(tog_commands[i].name, argv[0],
6814 strlen(argv[0])) == 0) {
6815 cmd = &tog_commands[i];
6816 break;
6821 if (cmd == NULL) {
6822 if (argc != 1)
6823 usage(0, 1);
6824 /* No command specified; try log with a path */
6825 error = tog_log_with_path(argc, argv);
6826 } else {
6827 if (hflag)
6828 cmd->cmd_usage();
6829 else
6830 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6833 endwin();
6834 putchar('\n');
6835 if (cmd_argv) {
6836 int i;
6837 for (i = 0; i < argc; i++)
6838 free(cmd_argv[i]);
6839 free(cmd_argv);
6842 if (error && error->code != GOT_ERR_CANCELLED)
6843 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6844 return 0;