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 case 'u':
2493 nscroll /= 2;
2494 /* FALL THROUGH */
2495 case KEY_PPAGE:
2496 case CTRL('b'):
2497 if (s->first_displayed_entry == NULL)
2498 break;
2499 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2500 s->selected = MAX(0, s->selected - nscroll - 1);
2501 else
2502 log_scroll_up(s, nscroll);
2503 select_commit(s);
2504 break;
2505 case 'j':
2506 case KEY_DOWN:
2507 case '>':
2508 case '.':
2509 case CTRL('n'):
2510 if (s->first_displayed_entry == NULL)
2511 break;
2512 if (s->selected < MIN(view->nlines - 2,
2513 s->commits.ncommits - 1))
2514 s->selected++;
2515 else {
2516 err = log_scroll_down(view, 1);
2517 if (err)
2518 break;
2520 select_commit(s);
2521 break;
2522 case 'G':
2523 case KEY_END: {
2524 /* We don't know yet how many commits, so we're forced to
2525 * traverse them all. */
2526 if (!s->thread_args.log_complete) {
2527 s->thread_args.load_all = 1;
2528 return trigger_log_thread(view, 0);
2531 s->selected = 0;
2532 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2533 for (n = 0; n < view->nlines - 1; n++) {
2534 if (entry == NULL)
2535 break;
2536 s->first_displayed_entry = entry;
2537 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2539 if (n > 0)
2540 s->selected = n - 1;
2541 select_commit(s);
2542 break;
2544 case CTRL('d'):
2545 case 'd':
2546 nscroll /= 2;
2547 /* FALL THROUGH */
2548 case KEY_NPAGE:
2549 case CTRL('f'): {
2550 struct commit_queue_entry *first;
2551 first = s->first_displayed_entry;
2552 if (first == NULL)
2553 break;
2554 err = log_scroll_down(view, nscroll);
2555 if (err)
2556 break;
2557 if (first == s->first_displayed_entry &&
2558 s->selected < MIN(view->nlines - 2,
2559 s->commits.ncommits - 1)) {
2560 /* can't scroll further down */
2561 s->selected += MIN(s->last_displayed_entry->idx -
2562 s->selected_entry->idx, nscroll + 1);
2564 select_commit(s);
2565 break;
2567 case KEY_RESIZE:
2568 if (s->selected > view->nlines - 2)
2569 s->selected = view->nlines - 2;
2570 if (s->selected > s->commits.ncommits - 1)
2571 s->selected = s->commits.ncommits - 1;
2572 select_commit(s);
2573 if (s->commits.ncommits < view->nlines - 1 &&
2574 !s->thread_args.log_complete) {
2575 s->thread_args.commits_needed += (view->nlines - 1) -
2576 s->commits.ncommits;
2577 err = trigger_log_thread(view, 1);
2579 break;
2580 case KEY_ENTER:
2581 case ' ':
2582 case '\r':
2583 if (s->selected_entry == NULL)
2584 break;
2585 if (view_is_parent_view(view))
2586 begin_x = view_split_begin_x(view->begin_x);
2587 err = open_diff_view_for_commit(&diff_view, begin_x,
2588 s->selected_entry->commit, s->selected_entry->id,
2589 view, s->repo);
2590 if (err)
2591 break;
2592 view->focussed = 0;
2593 diff_view->focussed = 1;
2594 if (view_is_parent_view(view)) {
2595 err = view_close_child(view);
2596 if (err)
2597 return err;
2598 view_set_child(view, diff_view);
2599 view->focus_child = 1;
2600 } else
2601 *new_view = diff_view;
2602 break;
2603 case 't':
2604 if (s->selected_entry == NULL)
2605 break;
2606 if (view_is_parent_view(view))
2607 begin_x = view_split_begin_x(view->begin_x);
2608 err = browse_commit_tree(&tree_view, begin_x,
2609 s->selected_entry, s->in_repo_path, s->head_ref_name,
2610 s->repo);
2611 if (err)
2612 break;
2613 view->focussed = 0;
2614 tree_view->focussed = 1;
2615 if (view_is_parent_view(view)) {
2616 err = view_close_child(view);
2617 if (err)
2618 return err;
2619 view_set_child(view, tree_view);
2620 view->focus_child = 1;
2621 } else
2622 *new_view = tree_view;
2623 break;
2624 case KEY_BACKSPACE:
2625 case CTRL('l'):
2626 case 'B':
2627 if (ch == KEY_BACKSPACE &&
2628 got_path_is_root_dir(s->in_repo_path))
2629 break;
2630 err = stop_log_thread(s);
2631 if (err)
2632 return err;
2633 if (ch == KEY_BACKSPACE) {
2634 char *parent_path;
2635 err = got_path_dirname(&parent_path, s->in_repo_path);
2636 if (err)
2637 return err;
2638 free(s->in_repo_path);
2639 s->in_repo_path = parent_path;
2640 s->thread_args.in_repo_path = s->in_repo_path;
2641 } else if (ch == CTRL('l')) {
2642 struct got_object_id *start_id;
2643 err = got_repo_match_object_id(&start_id, NULL,
2644 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2645 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2646 if (err)
2647 return err;
2648 free(s->start_id);
2649 s->start_id = start_id;
2650 s->thread_args.start_id = s->start_id;
2651 } else /* 'B' */
2652 s->log_branches = !s->log_branches;
2654 err = got_repo_open(&s->thread_args.repo,
2655 got_repo_get_path(s->repo), NULL);
2656 if (err)
2657 return err;
2658 tog_free_refs();
2659 err = tog_load_refs(s->repo, 0);
2660 if (err)
2661 return err;
2662 err = got_commit_graph_open(&s->thread_args.graph,
2663 s->in_repo_path, !s->log_branches);
2664 if (err)
2665 return err;
2666 err = got_commit_graph_iter_start(s->thread_args.graph,
2667 s->start_id, s->repo, NULL, NULL);
2668 if (err)
2669 return err;
2670 free_commits(&s->commits);
2671 s->first_displayed_entry = NULL;
2672 s->last_displayed_entry = NULL;
2673 s->selected_entry = NULL;
2674 s->selected = 0;
2675 s->thread_args.log_complete = 0;
2676 s->quit = 0;
2677 s->thread_args.commits_needed = view->nlines;
2678 break;
2679 case 'r':
2680 if (view_is_parent_view(view))
2681 begin_x = view_split_begin_x(view->begin_x);
2682 ref_view = view_open(view->nlines, view->ncols,
2683 view->begin_y, begin_x, TOG_VIEW_REF);
2684 if (ref_view == NULL)
2685 return got_error_from_errno("view_open");
2686 err = open_ref_view(ref_view, s->repo);
2687 if (err) {
2688 view_close(ref_view);
2689 return err;
2691 view->focussed = 0;
2692 ref_view->focussed = 1;
2693 if (view_is_parent_view(view)) {
2694 err = view_close_child(view);
2695 if (err)
2696 return err;
2697 view_set_child(view, ref_view);
2698 view->focus_child = 1;
2699 } else
2700 *new_view = ref_view;
2701 break;
2702 default:
2703 break;
2706 return err;
2709 static const struct got_error *
2710 apply_unveil(const char *repo_path, const char *worktree_path)
2712 const struct got_error *error;
2714 #ifdef PROFILE
2715 if (unveil("gmon.out", "rwc") != 0)
2716 return got_error_from_errno2("unveil", "gmon.out");
2717 #endif
2718 if (repo_path && unveil(repo_path, "r") != 0)
2719 return got_error_from_errno2("unveil", repo_path);
2721 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2722 return got_error_from_errno2("unveil", worktree_path);
2724 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2725 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2727 error = got_privsep_unveil_exec_helpers();
2728 if (error != NULL)
2729 return error;
2731 if (unveil(NULL, NULL) != 0)
2732 return got_error_from_errno("unveil");
2734 return NULL;
2737 static void
2738 init_curses(void)
2741 * Override default signal handlers before starting ncurses.
2742 * This should prevent ncurses from installing its own
2743 * broken cleanup() signal handler.
2745 signal(SIGWINCH, tog_sigwinch);
2746 signal(SIGPIPE, tog_sigpipe);
2747 signal(SIGCONT, tog_sigcont);
2748 signal(SIGINT, tog_sigint);
2749 signal(SIGTERM, tog_sigterm);
2751 initscr();
2752 cbreak();
2753 halfdelay(1); /* Do fast refresh while initial view is loading. */
2754 noecho();
2755 nonl();
2756 intrflush(stdscr, FALSE);
2757 keypad(stdscr, TRUE);
2758 curs_set(0);
2759 if (getenv("TOG_COLORS") != NULL) {
2760 start_color();
2761 use_default_colors();
2765 static const struct got_error *
2766 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2767 struct got_repository *repo, struct got_worktree *worktree)
2769 const struct got_error *err = NULL;
2771 if (argc == 0) {
2772 *in_repo_path = strdup("/");
2773 if (*in_repo_path == NULL)
2774 return got_error_from_errno("strdup");
2775 return NULL;
2778 if (worktree) {
2779 const char *prefix = got_worktree_get_path_prefix(worktree);
2780 char *p;
2782 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2783 if (err)
2784 return err;
2785 if (asprintf(in_repo_path, "%s%s%s", prefix,
2786 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2787 p) == -1) {
2788 err = got_error_from_errno("asprintf");
2789 *in_repo_path = NULL;
2791 free(p);
2792 } else
2793 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2795 return err;
2798 static const struct got_error *
2799 cmd_log(int argc, char *argv[])
2801 const struct got_error *error;
2802 struct got_repository *repo = NULL;
2803 struct got_worktree *worktree = NULL;
2804 struct got_object_id *start_id = NULL;
2805 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2806 char *start_commit = NULL, *label = NULL;
2807 struct got_reference *ref = NULL;
2808 const char *head_ref_name = NULL;
2809 int ch, log_branches = 0;
2810 struct tog_view *view;
2812 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2813 switch (ch) {
2814 case 'b':
2815 log_branches = 1;
2816 break;
2817 case 'c':
2818 start_commit = optarg;
2819 break;
2820 case 'r':
2821 repo_path = realpath(optarg, NULL);
2822 if (repo_path == NULL)
2823 return got_error_from_errno2("realpath",
2824 optarg);
2825 break;
2826 default:
2827 usage_log();
2828 /* NOTREACHED */
2832 argc -= optind;
2833 argv += optind;
2835 if (argc > 1)
2836 usage_log();
2838 if (repo_path == NULL) {
2839 cwd = getcwd(NULL, 0);
2840 if (cwd == NULL)
2841 return got_error_from_errno("getcwd");
2842 error = got_worktree_open(&worktree, cwd);
2843 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2844 goto done;
2845 if (worktree)
2846 repo_path =
2847 strdup(got_worktree_get_repo_path(worktree));
2848 else
2849 repo_path = strdup(cwd);
2850 if (repo_path == NULL) {
2851 error = got_error_from_errno("strdup");
2852 goto done;
2856 error = got_repo_open(&repo, repo_path, NULL);
2857 if (error != NULL)
2858 goto done;
2860 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2861 repo, worktree);
2862 if (error)
2863 goto done;
2865 init_curses();
2867 error = apply_unveil(got_repo_get_path(repo),
2868 worktree ? got_worktree_get_root_path(worktree) : NULL);
2869 if (error)
2870 goto done;
2872 /* already loaded by tog_log_with_path()? */
2873 if (TAILQ_EMPTY(&tog_refs)) {
2874 error = tog_load_refs(repo, 0);
2875 if (error)
2876 goto done;
2879 if (start_commit == NULL) {
2880 error = got_repo_match_object_id(&start_id, &label,
2881 worktree ? got_worktree_get_head_ref_name(worktree) :
2882 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2883 if (error)
2884 goto done;
2885 head_ref_name = label;
2886 } else {
2887 error = got_ref_open(&ref, repo, start_commit, 0);
2888 if (error == NULL)
2889 head_ref_name = got_ref_get_name(ref);
2890 else if (error->code != GOT_ERR_NOT_REF)
2891 goto done;
2892 error = got_repo_match_object_id(&start_id, NULL,
2893 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2894 if (error)
2895 goto done;
2898 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2899 if (view == NULL) {
2900 error = got_error_from_errno("view_open");
2901 goto done;
2903 error = open_log_view(view, start_id, repo, head_ref_name,
2904 in_repo_path, log_branches);
2905 if (error)
2906 goto done;
2907 if (worktree) {
2908 /* Release work tree lock. */
2909 got_worktree_close(worktree);
2910 worktree = NULL;
2912 error = view_loop(view);
2913 done:
2914 free(in_repo_path);
2915 free(repo_path);
2916 free(cwd);
2917 free(start_id);
2918 free(label);
2919 if (ref)
2920 got_ref_close(ref);
2921 if (repo) {
2922 const struct got_error *close_err = got_repo_close(repo);
2923 if (error == NULL)
2924 error = close_err;
2926 if (worktree)
2927 got_worktree_close(worktree);
2928 tog_free_refs();
2929 return error;
2932 __dead static void
2933 usage_diff(void)
2935 endwin();
2936 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2937 "[-w] object1 object2\n", getprogname());
2938 exit(1);
2941 static int
2942 match_line(const char *line, regex_t *regex, size_t nmatch,
2943 regmatch_t *regmatch)
2945 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2948 struct tog_color *
2949 match_color(struct tog_colors *colors, const char *line)
2951 struct tog_color *tc = NULL;
2953 STAILQ_FOREACH(tc, colors, entry) {
2954 if (match_line(line, &tc->regex, 0, NULL))
2955 return tc;
2958 return NULL;
2961 static const struct got_error *
2962 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2963 WINDOW *window, regmatch_t *regmatch)
2965 const struct got_error *err = NULL;
2966 wchar_t *wline;
2967 int width;
2968 char *s;
2970 *wtotal = 0;
2972 s = strndup(line, regmatch->rm_so);
2973 if (s == NULL)
2974 return got_error_from_errno("strndup");
2976 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2977 if (err) {
2978 free(s);
2979 return err;
2981 waddwstr(window, wline);
2982 free(wline);
2983 free(s);
2984 wlimit -= width;
2985 *wtotal += width;
2987 if (wlimit > 0) {
2988 s = strndup(line + regmatch->rm_so,
2989 regmatch->rm_eo - regmatch->rm_so);
2990 if (s == NULL) {
2991 err = got_error_from_errno("strndup");
2992 free(s);
2993 return err;
2995 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2996 if (err) {
2997 free(s);
2998 return err;
3000 wattr_on(window, A_STANDOUT, NULL);
3001 waddwstr(window, wline);
3002 wattr_off(window, A_STANDOUT, NULL);
3003 free(wline);
3004 free(s);
3005 wlimit -= width;
3006 *wtotal += width;
3009 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3010 err = format_line(&wline, &width,
3011 line + regmatch->rm_eo, wlimit, col_tab_align);
3012 if (err)
3013 return err;
3014 waddwstr(window, wline);
3015 free(wline);
3016 *wtotal += width;
3019 return NULL;
3022 static const struct got_error *
3023 draw_file(struct tog_view *view, const char *header)
3025 struct tog_diff_view_state *s = &view->state.diff;
3026 regmatch_t *regmatch = &view->regmatch;
3027 const struct got_error *err;
3028 int nprinted = 0;
3029 char *line;
3030 size_t linesize = 0;
3031 ssize_t linelen;
3032 struct tog_color *tc;
3033 wchar_t *wline;
3034 int width;
3035 int max_lines = view->nlines;
3036 int nlines = s->nlines;
3037 off_t line_offset;
3039 line_offset = s->line_offsets[s->first_displayed_line - 1];
3040 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3041 return got_error_from_errno("fseek");
3043 werase(view->window);
3045 if (header) {
3046 if (asprintf(&line, "[%d/%d] %s",
3047 s->first_displayed_line - 1 + s->selected_line, nlines,
3048 header) == -1)
3049 return got_error_from_errno("asprintf");
3050 err = format_line(&wline, &width, line, view->ncols, 0);
3051 free(line);
3052 if (err)
3053 return err;
3055 if (view_needs_focus_indication(view))
3056 wstandout(view->window);
3057 waddwstr(view->window, wline);
3058 free(wline);
3059 wline = NULL;
3060 if (view_needs_focus_indication(view))
3061 wstandend(view->window);
3062 if (width <= view->ncols - 1)
3063 waddch(view->window, '\n');
3065 if (max_lines <= 1)
3066 return NULL;
3067 max_lines--;
3070 s->eof = 0;
3071 line = NULL;
3072 while (max_lines > 0 && nprinted < max_lines) {
3073 linelen = getline(&line, &linesize, s->f);
3074 if (linelen == -1) {
3075 if (feof(s->f)) {
3076 s->eof = 1;
3077 break;
3079 free(line);
3080 return got_ferror(s->f, GOT_ERR_IO);
3083 tc = match_color(&s->colors, line);
3084 if (tc)
3085 wattr_on(view->window,
3086 COLOR_PAIR(tc->colorpair), NULL);
3087 if (s->first_displayed_line + nprinted == s->matched_line &&
3088 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3089 err = add_matched_line(&width, line, view->ncols, 0,
3090 view->window, regmatch);
3091 if (err) {
3092 free(line);
3093 return err;
3095 } else {
3096 err = format_line(&wline, &width, line, view->ncols, 0);
3097 if (err) {
3098 free(line);
3099 return err;
3101 waddwstr(view->window, wline);
3102 free(wline);
3103 wline = NULL;
3105 if (tc)
3106 wattr_off(view->window,
3107 COLOR_PAIR(tc->colorpair), NULL);
3108 if (width <= view->ncols - 1)
3109 waddch(view->window, '\n');
3110 nprinted++;
3112 free(line);
3113 if (nprinted >= 1)
3114 s->last_displayed_line = s->first_displayed_line +
3115 (nprinted - 1);
3116 else
3117 s->last_displayed_line = s->first_displayed_line;
3119 view_vborder(view);
3121 if (s->eof) {
3122 while (nprinted < view->nlines) {
3123 waddch(view->window, '\n');
3124 nprinted++;
3127 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3128 if (err) {
3129 return err;
3132 wstandout(view->window);
3133 waddwstr(view->window, wline);
3134 free(wline);
3135 wline = NULL;
3136 wstandend(view->window);
3139 return NULL;
3142 static char *
3143 get_datestr(time_t *time, char *datebuf)
3145 struct tm mytm, *tm;
3146 char *p, *s;
3148 tm = gmtime_r(time, &mytm);
3149 if (tm == NULL)
3150 return NULL;
3151 s = asctime_r(tm, datebuf);
3152 if (s == NULL)
3153 return NULL;
3154 p = strchr(s, '\n');
3155 if (p)
3156 *p = '\0';
3157 return s;
3160 static const struct got_error *
3161 get_changed_paths(struct got_pathlist_head *paths,
3162 struct got_commit_object *commit, struct got_repository *repo)
3164 const struct got_error *err = NULL;
3165 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3166 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3167 struct got_object_qid *qid;
3169 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3170 if (qid != NULL) {
3171 struct got_commit_object *pcommit;
3172 err = got_object_open_as_commit(&pcommit, repo,
3173 &qid->id);
3174 if (err)
3175 return err;
3177 tree_id1 = got_object_id_dup(
3178 got_object_commit_get_tree_id(pcommit));
3179 if (tree_id1 == NULL) {
3180 got_object_commit_close(pcommit);
3181 return got_error_from_errno("got_object_id_dup");
3183 got_object_commit_close(pcommit);
3187 if (tree_id1) {
3188 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3189 if (err)
3190 goto done;
3193 tree_id2 = got_object_commit_get_tree_id(commit);
3194 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3195 if (err)
3196 goto done;
3198 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3199 got_diff_tree_collect_changed_paths, paths, 0);
3200 done:
3201 if (tree1)
3202 got_object_tree_close(tree1);
3203 if (tree2)
3204 got_object_tree_close(tree2);
3205 free(tree_id1);
3206 return err;
3209 static const struct got_error *
3210 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3212 off_t *p;
3214 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3215 if (p == NULL)
3216 return got_error_from_errno("reallocarray");
3217 *line_offsets = p;
3218 (*line_offsets)[*nlines] = off;
3219 (*nlines)++;
3220 return NULL;
3223 static const struct got_error *
3224 write_commit_info(off_t **line_offsets, size_t *nlines,
3225 struct got_object_id *commit_id, struct got_reflist_head *refs,
3226 struct got_repository *repo, FILE *outfile)
3228 const struct got_error *err = NULL;
3229 char datebuf[26], *datestr;
3230 struct got_commit_object *commit;
3231 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3232 time_t committer_time;
3233 const char *author, *committer;
3234 char *refs_str = NULL;
3235 struct got_pathlist_head changed_paths;
3236 struct got_pathlist_entry *pe;
3237 off_t outoff = 0;
3238 int n;
3240 TAILQ_INIT(&changed_paths);
3242 if (refs) {
3243 err = build_refs_str(&refs_str, refs, commit_id, repo);
3244 if (err)
3245 return err;
3248 err = got_object_open_as_commit(&commit, repo, commit_id);
3249 if (err)
3250 return err;
3252 err = got_object_id_str(&id_str, commit_id);
3253 if (err) {
3254 err = got_error_from_errno("got_object_id_str");
3255 goto done;
3258 err = add_line_offset(line_offsets, nlines, 0);
3259 if (err)
3260 goto done;
3262 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3263 refs_str ? refs_str : "", refs_str ? ")" : "");
3264 if (n < 0) {
3265 err = got_error_from_errno("fprintf");
3266 goto done;
3268 outoff += n;
3269 err = add_line_offset(line_offsets, nlines, outoff);
3270 if (err)
3271 goto done;
3273 n = fprintf(outfile, "from: %s\n",
3274 got_object_commit_get_author(commit));
3275 if (n < 0) {
3276 err = got_error_from_errno("fprintf");
3277 goto done;
3279 outoff += n;
3280 err = add_line_offset(line_offsets, nlines, outoff);
3281 if (err)
3282 goto done;
3284 committer_time = got_object_commit_get_committer_time(commit);
3285 datestr = get_datestr(&committer_time, datebuf);
3286 if (datestr) {
3287 n = fprintf(outfile, "date: %s UTC\n", datestr);
3288 if (n < 0) {
3289 err = got_error_from_errno("fprintf");
3290 goto done;
3292 outoff += n;
3293 err = add_line_offset(line_offsets, nlines, outoff);
3294 if (err)
3295 goto done;
3297 author = got_object_commit_get_author(commit);
3298 committer = got_object_commit_get_committer(commit);
3299 if (strcmp(author, committer) != 0) {
3300 n = fprintf(outfile, "via: %s\n", committer);
3301 if (n < 0) {
3302 err = got_error_from_errno("fprintf");
3303 goto done;
3305 outoff += n;
3306 err = add_line_offset(line_offsets, nlines, outoff);
3307 if (err)
3308 goto done;
3310 if (got_object_commit_get_nparents(commit) > 1) {
3311 const struct got_object_id_queue *parent_ids;
3312 struct got_object_qid *qid;
3313 int pn = 1;
3314 parent_ids = got_object_commit_get_parent_ids(commit);
3315 STAILQ_FOREACH(qid, parent_ids, entry) {
3316 err = got_object_id_str(&id_str, &qid->id);
3317 if (err)
3318 goto done;
3319 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3320 if (n < 0) {
3321 err = got_error_from_errno("fprintf");
3322 goto done;
3324 outoff += n;
3325 err = add_line_offset(line_offsets, nlines, outoff);
3326 if (err)
3327 goto done;
3328 free(id_str);
3329 id_str = NULL;
3333 err = got_object_commit_get_logmsg(&logmsg, commit);
3334 if (err)
3335 goto done;
3336 s = logmsg;
3337 while ((line = strsep(&s, "\n")) != NULL) {
3338 n = fprintf(outfile, "%s\n", line);
3339 if (n < 0) {
3340 err = got_error_from_errno("fprintf");
3341 goto done;
3343 outoff += n;
3344 err = add_line_offset(line_offsets, nlines, outoff);
3345 if (err)
3346 goto done;
3349 err = get_changed_paths(&changed_paths, commit, repo);
3350 if (err)
3351 goto done;
3352 TAILQ_FOREACH(pe, &changed_paths, entry) {
3353 struct got_diff_changed_path *cp = pe->data;
3354 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3355 if (n < 0) {
3356 err = got_error_from_errno("fprintf");
3357 goto done;
3359 outoff += n;
3360 err = add_line_offset(line_offsets, nlines, outoff);
3361 if (err)
3362 goto done;
3363 free((char *)pe->path);
3364 free(pe->data);
3367 fputc('\n', outfile);
3368 outoff++;
3369 err = add_line_offset(line_offsets, nlines, outoff);
3370 done:
3371 got_pathlist_free(&changed_paths);
3372 free(id_str);
3373 free(logmsg);
3374 free(refs_str);
3375 got_object_commit_close(commit);
3376 if (err) {
3377 free(*line_offsets);
3378 *line_offsets = NULL;
3379 *nlines = 0;
3381 return err;
3384 static const struct got_error *
3385 create_diff(struct tog_diff_view_state *s)
3387 const struct got_error *err = NULL;
3388 FILE *f = NULL;
3389 int obj_type;
3391 free(s->line_offsets);
3392 s->line_offsets = malloc(sizeof(off_t));
3393 if (s->line_offsets == NULL)
3394 return got_error_from_errno("malloc");
3395 s->nlines = 0;
3397 f = got_opentemp();
3398 if (f == NULL) {
3399 err = got_error_from_errno("got_opentemp");
3400 goto done;
3402 if (s->f && fclose(s->f) == EOF) {
3403 err = got_error_from_errno("fclose");
3404 goto done;
3406 s->f = f;
3408 if (s->id1)
3409 err = got_object_get_type(&obj_type, s->repo, s->id1);
3410 else
3411 err = got_object_get_type(&obj_type, s->repo, s->id2);
3412 if (err)
3413 goto done;
3415 switch (obj_type) {
3416 case GOT_OBJ_TYPE_BLOB:
3417 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3418 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3419 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3420 s->repo, s->f);
3421 break;
3422 case GOT_OBJ_TYPE_TREE:
3423 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3424 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3425 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3426 break;
3427 case GOT_OBJ_TYPE_COMMIT: {
3428 const struct got_object_id_queue *parent_ids;
3429 struct got_object_qid *pid;
3430 struct got_commit_object *commit2;
3431 struct got_reflist_head *refs;
3433 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3434 if (err)
3435 goto done;
3436 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3437 /* Show commit info if we're diffing to a parent/root commit. */
3438 if (s->id1 == NULL) {
3439 err = write_commit_info(&s->line_offsets, &s->nlines,
3440 s->id2, refs, s->repo, s->f);
3441 if (err)
3442 goto done;
3443 } else {
3444 parent_ids = got_object_commit_get_parent_ids(commit2);
3445 STAILQ_FOREACH(pid, parent_ids, entry) {
3446 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3447 err = write_commit_info(
3448 &s->line_offsets, &s->nlines,
3449 s->id2, refs, s->repo, s->f);
3450 if (err)
3451 goto done;
3452 break;
3456 got_object_commit_close(commit2);
3458 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3459 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3460 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3461 break;
3463 default:
3464 err = got_error(GOT_ERR_OBJ_TYPE);
3465 break;
3467 if (err)
3468 goto done;
3469 done:
3470 if (s->f && fflush(s->f) != 0 && err == NULL)
3471 err = got_error_from_errno("fflush");
3472 return err;
3475 static void
3476 diff_view_indicate_progress(struct tog_view *view)
3478 mvwaddstr(view->window, 0, 0, "diffing...");
3479 update_panels();
3480 doupdate();
3483 static const struct got_error *
3484 search_start_diff_view(struct tog_view *view)
3486 struct tog_diff_view_state *s = &view->state.diff;
3488 s->matched_line = 0;
3489 return NULL;
3492 static const struct got_error *
3493 search_next_diff_view(struct tog_view *view)
3495 struct tog_diff_view_state *s = &view->state.diff;
3496 int lineno;
3497 char *line = NULL;
3498 size_t linesize = 0;
3499 ssize_t linelen;
3501 if (!view->searching) {
3502 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3503 return NULL;
3506 if (s->matched_line) {
3507 if (view->searching == TOG_SEARCH_FORWARD)
3508 lineno = s->matched_line + 1;
3509 else
3510 lineno = s->matched_line - 1;
3511 } else
3512 lineno = s->first_displayed_line;
3514 while (1) {
3515 off_t offset;
3517 if (lineno <= 0 || lineno > s->nlines) {
3518 if (s->matched_line == 0) {
3519 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3520 break;
3523 if (view->searching == TOG_SEARCH_FORWARD)
3524 lineno = 1;
3525 else
3526 lineno = s->nlines;
3529 offset = s->line_offsets[lineno - 1];
3530 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3531 free(line);
3532 return got_error_from_errno("fseeko");
3534 linelen = getline(&line, &linesize, s->f);
3535 if (linelen != -1 &&
3536 match_line(line, &view->regex, 1, &view->regmatch)) {
3537 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3538 s->matched_line = lineno;
3539 break;
3541 if (view->searching == TOG_SEARCH_FORWARD)
3542 lineno++;
3543 else
3544 lineno--;
3546 free(line);
3548 if (s->matched_line) {
3549 s->first_displayed_line = s->matched_line;
3550 s->selected_line = 1;
3553 return NULL;
3556 static const struct got_error *
3557 close_diff_view(struct tog_view *view)
3559 const struct got_error *err = NULL;
3560 struct tog_diff_view_state *s = &view->state.diff;
3562 free(s->id1);
3563 s->id1 = NULL;
3564 free(s->id2);
3565 s->id2 = NULL;
3566 if (s->f && fclose(s->f) == EOF)
3567 err = got_error_from_errno("fclose");
3568 s->f = NULL;
3569 if (s->f1 && fclose(s->f1) == EOF)
3570 err = got_error_from_errno("fclose");
3571 s->f1 = NULL;
3572 if (s->f2 && fclose(s->f2) == EOF)
3573 err = got_error_from_errno("fclose");
3574 s->f2 = NULL;
3575 free_colors(&s->colors);
3576 free(s->line_offsets);
3577 s->line_offsets = NULL;
3578 s->nlines = 0;
3579 return err;
3582 static const struct got_error *
3583 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3584 struct got_object_id *id2, const char *label1, const char *label2,
3585 int diff_context, int ignore_whitespace, int force_text_diff,
3586 struct tog_view *log_view, struct got_repository *repo)
3588 const struct got_error *err;
3589 struct tog_diff_view_state *s = &view->state.diff;
3591 memset(s, 0, sizeof(*s));
3593 if (id1 != NULL && id2 != NULL) {
3594 int type1, type2;
3595 err = got_object_get_type(&type1, repo, id1);
3596 if (err)
3597 return err;
3598 err = got_object_get_type(&type2, repo, id2);
3599 if (err)
3600 return err;
3602 if (type1 != type2)
3603 return got_error(GOT_ERR_OBJ_TYPE);
3605 s->first_displayed_line = 1;
3606 s->last_displayed_line = view->nlines;
3607 s->selected_line = 1;
3608 s->repo = repo;
3609 s->id1 = id1;
3610 s->id2 = id2;
3611 s->label1 = label1;
3612 s->label2 = label2;
3614 if (id1) {
3615 s->id1 = got_object_id_dup(id1);
3616 if (s->id1 == NULL)
3617 return got_error_from_errno("got_object_id_dup");
3618 s->f1 = got_opentemp();
3619 if (s->f1 == NULL) {
3620 err = got_error_from_errno("got_opentemp");
3621 goto done;
3623 } else
3624 s->id1 = NULL;
3626 s->id2 = got_object_id_dup(id2);
3627 if (s->id2 == NULL) {
3628 err = got_error_from_errno("got_object_id_dup");
3629 goto done;
3632 s->f2 = got_opentemp();
3633 if (s->f2 == NULL) {
3634 err = got_error_from_errno("got_opentemp");
3635 goto done;
3638 s->first_displayed_line = 1;
3639 s->last_displayed_line = view->nlines;
3640 s->diff_context = diff_context;
3641 s->ignore_whitespace = ignore_whitespace;
3642 s->force_text_diff = force_text_diff;
3643 s->log_view = log_view;
3644 s->repo = repo;
3646 STAILQ_INIT(&s->colors);
3647 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3648 err = add_color(&s->colors,
3649 "^-", TOG_COLOR_DIFF_MINUS,
3650 get_color_value("TOG_COLOR_DIFF_MINUS"));
3651 if (err)
3652 goto done;
3653 err = add_color(&s->colors, "^\\+",
3654 TOG_COLOR_DIFF_PLUS,
3655 get_color_value("TOG_COLOR_DIFF_PLUS"));
3656 if (err)
3657 goto done;
3658 err = add_color(&s->colors,
3659 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3660 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3661 if (err)
3662 goto done;
3664 err = add_color(&s->colors,
3665 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3666 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3667 get_color_value("TOG_COLOR_DIFF_META"));
3668 if (err)
3669 goto done;
3671 err = add_color(&s->colors,
3672 "^(from|via): ", TOG_COLOR_AUTHOR,
3673 get_color_value("TOG_COLOR_AUTHOR"));
3674 if (err)
3675 goto done;
3677 err = add_color(&s->colors,
3678 "^date: ", TOG_COLOR_DATE,
3679 get_color_value("TOG_COLOR_DATE"));
3680 if (err)
3681 goto done;
3684 if (log_view && view_is_splitscreen(view))
3685 show_log_view(log_view); /* draw vborder */
3686 diff_view_indicate_progress(view);
3688 err = create_diff(s);
3690 view->show = show_diff_view;
3691 view->input = input_diff_view;
3692 view->close = close_diff_view;
3693 view->search_start = search_start_diff_view;
3694 view->search_next = search_next_diff_view;
3695 done:
3696 if (err)
3697 close_diff_view(view);
3698 return err;
3701 static const struct got_error *
3702 show_diff_view(struct tog_view *view)
3704 const struct got_error *err;
3705 struct tog_diff_view_state *s = &view->state.diff;
3706 char *id_str1 = NULL, *id_str2, *header;
3707 const char *label1, *label2;
3709 if (s->id1) {
3710 err = got_object_id_str(&id_str1, s->id1);
3711 if (err)
3712 return err;
3713 label1 = s->label1 ? : id_str1;
3714 } else
3715 label1 = "/dev/null";
3717 err = got_object_id_str(&id_str2, s->id2);
3718 if (err)
3719 return err;
3720 label2 = s->label2 ? : id_str2;
3722 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3723 err = got_error_from_errno("asprintf");
3724 free(id_str1);
3725 free(id_str2);
3726 return err;
3728 free(id_str1);
3729 free(id_str2);
3731 err = draw_file(view, header);
3732 free(header);
3733 return err;
3736 static const struct got_error *
3737 set_selected_commit(struct tog_diff_view_state *s,
3738 struct commit_queue_entry *entry)
3740 const struct got_error *err;
3741 const struct got_object_id_queue *parent_ids;
3742 struct got_commit_object *selected_commit;
3743 struct got_object_qid *pid;
3745 free(s->id2);
3746 s->id2 = got_object_id_dup(entry->id);
3747 if (s->id2 == NULL)
3748 return got_error_from_errno("got_object_id_dup");
3750 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3751 if (err)
3752 return err;
3753 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3754 free(s->id1);
3755 pid = STAILQ_FIRST(parent_ids);
3756 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3757 got_object_commit_close(selected_commit);
3758 return NULL;
3761 static const struct got_error *
3762 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3764 const struct got_error *err = NULL;
3765 struct tog_diff_view_state *s = &view->state.diff;
3766 struct tog_log_view_state *ls;
3767 struct commit_queue_entry *old_selected_entry;
3768 char *line = NULL;
3769 size_t linesize = 0;
3770 ssize_t linelen;
3771 int i, nscroll = view->nlines - 1;
3773 switch (ch) {
3774 case 'a':
3775 case 'w':
3776 if (ch == 'a')
3777 s->force_text_diff = !s->force_text_diff;
3778 if (ch == 'w')
3779 s->ignore_whitespace = !s->ignore_whitespace;
3780 wclear(view->window);
3781 s->first_displayed_line = 1;
3782 s->last_displayed_line = view->nlines;
3783 s->matched_line = 0;
3784 diff_view_indicate_progress(view);
3785 err = create_diff(s);
3786 break;
3787 case 'g':
3788 case KEY_HOME:
3789 s->first_displayed_line = 1;
3790 break;
3791 case 'G':
3792 case KEY_END:
3793 if (s->eof)
3794 break;
3796 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3797 s->eof = 1;
3798 break;
3799 case 'k':
3800 case KEY_UP:
3801 case CTRL('p'):
3802 if (s->first_displayed_line > 1)
3803 s->first_displayed_line--;
3804 break;
3805 case CTRL('u'):
3806 case 'u':
3807 nscroll /= 2;
3808 /* FALL THROUGH */
3809 case KEY_PPAGE:
3810 case CTRL('b'):
3811 if (s->first_displayed_line == 1)
3812 break;
3813 i = 0;
3814 while (i++ < nscroll && s->first_displayed_line > 1)
3815 s->first_displayed_line--;
3816 break;
3817 case 'j':
3818 case KEY_DOWN:
3819 case CTRL('n'):
3820 if (!s->eof)
3821 s->first_displayed_line++;
3822 break;
3823 case CTRL('d'):
3824 case 'd':
3825 nscroll /= 2;
3826 /* FALL THROUGH */
3827 case KEY_NPAGE:
3828 case CTRL('f'):
3829 case ' ':
3830 if (s->eof)
3831 break;
3832 i = 0;
3833 while (!s->eof && i++ < nscroll) {
3834 linelen = getline(&line, &linesize, s->f);
3835 s->first_displayed_line++;
3836 if (linelen == -1) {
3837 if (feof(s->f)) {
3838 s->eof = 1;
3839 } else
3840 err = got_ferror(s->f, GOT_ERR_IO);
3841 break;
3844 free(line);
3845 break;
3846 case '[':
3847 if (s->diff_context > 0) {
3848 s->diff_context--;
3849 s->matched_line = 0;
3850 diff_view_indicate_progress(view);
3851 err = create_diff(s);
3852 if (s->first_displayed_line + view->nlines - 1 >
3853 s->nlines) {
3854 s->first_displayed_line = 1;
3855 s->last_displayed_line = view->nlines;
3858 break;
3859 case ']':
3860 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3861 s->diff_context++;
3862 s->matched_line = 0;
3863 diff_view_indicate_progress(view);
3864 err = create_diff(s);
3866 break;
3867 case '<':
3868 case ',':
3869 if (s->log_view == NULL)
3870 break;
3871 ls = &s->log_view->state.log;
3872 old_selected_entry = ls->selected_entry;
3874 err = input_log_view(NULL, s->log_view, KEY_UP);
3875 if (err)
3876 break;
3878 if (old_selected_entry == ls->selected_entry)
3879 break;
3881 err = set_selected_commit(s, ls->selected_entry);
3882 if (err)
3883 break;
3885 s->first_displayed_line = 1;
3886 s->last_displayed_line = view->nlines;
3887 s->matched_line = 0;
3889 diff_view_indicate_progress(view);
3890 err = create_diff(s);
3891 break;
3892 case '>':
3893 case '.':
3894 if (s->log_view == NULL)
3895 break;
3896 ls = &s->log_view->state.log;
3897 old_selected_entry = ls->selected_entry;
3899 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3900 if (err)
3901 break;
3903 if (old_selected_entry == ls->selected_entry)
3904 break;
3906 err = set_selected_commit(s, ls->selected_entry);
3907 if (err)
3908 break;
3910 s->first_displayed_line = 1;
3911 s->last_displayed_line = view->nlines;
3912 s->matched_line = 0;
3914 diff_view_indicate_progress(view);
3915 err = create_diff(s);
3916 break;
3917 default:
3918 break;
3921 return err;
3924 static const struct got_error *
3925 cmd_diff(int argc, char *argv[])
3927 const struct got_error *error = NULL;
3928 struct got_repository *repo = NULL;
3929 struct got_worktree *worktree = NULL;
3930 struct got_object_id *id1 = NULL, *id2 = NULL;
3931 char *repo_path = NULL, *cwd = NULL;
3932 char *id_str1 = NULL, *id_str2 = NULL;
3933 char *label1 = NULL, *label2 = NULL;
3934 int diff_context = 3, ignore_whitespace = 0;
3935 int ch, force_text_diff = 0;
3936 const char *errstr;
3937 struct tog_view *view;
3939 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3940 switch (ch) {
3941 case 'a':
3942 force_text_diff = 1;
3943 break;
3944 case 'C':
3945 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3946 &errstr);
3947 if (errstr != NULL)
3948 errx(1, "number of context lines is %s: %s",
3949 errstr, errstr);
3950 break;
3951 case 'r':
3952 repo_path = realpath(optarg, NULL);
3953 if (repo_path == NULL)
3954 return got_error_from_errno2("realpath",
3955 optarg);
3956 got_path_strip_trailing_slashes(repo_path);
3957 break;
3958 case 'w':
3959 ignore_whitespace = 1;
3960 break;
3961 default:
3962 usage_diff();
3963 /* NOTREACHED */
3967 argc -= optind;
3968 argv += optind;
3970 if (argc == 0) {
3971 usage_diff(); /* TODO show local worktree changes */
3972 } else if (argc == 2) {
3973 id_str1 = argv[0];
3974 id_str2 = argv[1];
3975 } else
3976 usage_diff();
3978 if (repo_path == NULL) {
3979 cwd = getcwd(NULL, 0);
3980 if (cwd == NULL)
3981 return got_error_from_errno("getcwd");
3982 error = got_worktree_open(&worktree, cwd);
3983 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3984 goto done;
3985 if (worktree)
3986 repo_path =
3987 strdup(got_worktree_get_repo_path(worktree));
3988 else
3989 repo_path = strdup(cwd);
3990 if (repo_path == NULL) {
3991 error = got_error_from_errno("strdup");
3992 goto done;
3996 error = got_repo_open(&repo, repo_path, NULL);
3997 if (error)
3998 goto done;
4000 init_curses();
4002 error = apply_unveil(got_repo_get_path(repo), NULL);
4003 if (error)
4004 goto done;
4006 error = tog_load_refs(repo, 0);
4007 if (error)
4008 goto done;
4010 error = got_repo_match_object_id(&id1, &label1, id_str1,
4011 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4012 if (error)
4013 goto done;
4015 error = got_repo_match_object_id(&id2, &label2, id_str2,
4016 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4017 if (error)
4018 goto done;
4020 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4021 if (view == NULL) {
4022 error = got_error_from_errno("view_open");
4023 goto done;
4025 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4026 ignore_whitespace, force_text_diff, NULL, repo);
4027 if (error)
4028 goto done;
4029 error = view_loop(view);
4030 done:
4031 free(label1);
4032 free(label2);
4033 free(repo_path);
4034 free(cwd);
4035 if (repo) {
4036 const struct got_error *close_err = got_repo_close(repo);
4037 if (error == NULL)
4038 error = close_err;
4040 if (worktree)
4041 got_worktree_close(worktree);
4042 tog_free_refs();
4043 return error;
4046 __dead static void
4047 usage_blame(void)
4049 endwin();
4050 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4051 getprogname());
4052 exit(1);
4055 struct tog_blame_line {
4056 int annotated;
4057 struct got_object_id *id;
4060 static const struct got_error *
4061 draw_blame(struct tog_view *view)
4063 struct tog_blame_view_state *s = &view->state.blame;
4064 struct tog_blame *blame = &s->blame;
4065 regmatch_t *regmatch = &view->regmatch;
4066 const struct got_error *err;
4067 int lineno = 0, nprinted = 0;
4068 char *line = NULL;
4069 size_t linesize = 0;
4070 ssize_t linelen;
4071 wchar_t *wline;
4072 int width;
4073 struct tog_blame_line *blame_line;
4074 struct got_object_id *prev_id = NULL;
4075 char *id_str;
4076 struct tog_color *tc;
4078 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4079 if (err)
4080 return err;
4082 rewind(blame->f);
4083 werase(view->window);
4085 if (asprintf(&line, "commit %s", id_str) == -1) {
4086 err = got_error_from_errno("asprintf");
4087 free(id_str);
4088 return err;
4091 err = format_line(&wline, &width, line, view->ncols, 0);
4092 free(line);
4093 line = NULL;
4094 if (err)
4095 return err;
4096 if (view_needs_focus_indication(view))
4097 wstandout(view->window);
4098 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4099 if (tc)
4100 wattr_on(view->window,
4101 COLOR_PAIR(tc->colorpair), NULL);
4102 waddwstr(view->window, wline);
4103 if (tc)
4104 wattr_off(view->window,
4105 COLOR_PAIR(tc->colorpair), NULL);
4106 if (view_needs_focus_indication(view))
4107 wstandend(view->window);
4108 free(wline);
4109 wline = NULL;
4110 if (width < view->ncols - 1)
4111 waddch(view->window, '\n');
4113 if (asprintf(&line, "[%d/%d] %s%s",
4114 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4115 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4116 free(id_str);
4117 return got_error_from_errno("asprintf");
4119 free(id_str);
4120 err = format_line(&wline, &width, line, view->ncols, 0);
4121 free(line);
4122 line = NULL;
4123 if (err)
4124 return err;
4125 waddwstr(view->window, wline);
4126 free(wline);
4127 wline = NULL;
4128 if (width < view->ncols - 1)
4129 waddch(view->window, '\n');
4131 s->eof = 0;
4132 while (nprinted < view->nlines - 2) {
4133 linelen = getline(&line, &linesize, blame->f);
4134 if (linelen == -1) {
4135 if (feof(blame->f)) {
4136 s->eof = 1;
4137 break;
4139 free(line);
4140 return got_ferror(blame->f, GOT_ERR_IO);
4142 if (++lineno < s->first_displayed_line)
4143 continue;
4145 if (view->focussed && nprinted == s->selected_line - 1)
4146 wstandout(view->window);
4148 if (blame->nlines > 0) {
4149 blame_line = &blame->lines[lineno - 1];
4150 if (blame_line->annotated && prev_id &&
4151 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4152 !(view->focussed &&
4153 nprinted == s->selected_line - 1)) {
4154 waddstr(view->window, " ");
4155 } else if (blame_line->annotated) {
4156 char *id_str;
4157 err = got_object_id_str(&id_str, blame_line->id);
4158 if (err) {
4159 free(line);
4160 return err;
4162 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4163 if (tc)
4164 wattr_on(view->window,
4165 COLOR_PAIR(tc->colorpair), NULL);
4166 wprintw(view->window, "%.8s", id_str);
4167 if (tc)
4168 wattr_off(view->window,
4169 COLOR_PAIR(tc->colorpair), NULL);
4170 free(id_str);
4171 prev_id = blame_line->id;
4172 } else {
4173 waddstr(view->window, "........");
4174 prev_id = NULL;
4176 } else {
4177 waddstr(view->window, "........");
4178 prev_id = NULL;
4181 if (view->focussed && nprinted == s->selected_line - 1)
4182 wstandend(view->window);
4183 waddstr(view->window, " ");
4185 if (view->ncols <= 9) {
4186 width = 9;
4187 wline = wcsdup(L"");
4188 if (wline == NULL) {
4189 err = got_error_from_errno("wcsdup");
4190 free(line);
4191 return err;
4193 } else if (s->first_displayed_line + nprinted ==
4194 s->matched_line &&
4195 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4196 err = add_matched_line(&width, line, view->ncols - 9, 9,
4197 view->window, regmatch);
4198 if (err) {
4199 free(line);
4200 return err;
4202 width += 9;
4203 } else {
4204 err = format_line(&wline, &width, line,
4205 view->ncols - 9, 9);
4206 waddwstr(view->window, wline);
4207 free(wline);
4208 wline = NULL;
4209 width += 9;
4212 if (width <= view->ncols - 1)
4213 waddch(view->window, '\n');
4214 if (++nprinted == 1)
4215 s->first_displayed_line = lineno;
4217 free(line);
4218 s->last_displayed_line = lineno;
4220 view_vborder(view);
4222 return NULL;
4225 static const struct got_error *
4226 blame_cb(void *arg, int nlines, int lineno,
4227 struct got_commit_object *commit, struct got_object_id *id)
4229 const struct got_error *err = NULL;
4230 struct tog_blame_cb_args *a = arg;
4231 struct tog_blame_line *line;
4232 int errcode;
4234 if (nlines != a->nlines ||
4235 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4236 return got_error(GOT_ERR_RANGE);
4238 errcode = pthread_mutex_lock(&tog_mutex);
4239 if (errcode)
4240 return got_error_set_errno(errcode, "pthread_mutex_lock");
4242 if (*a->quit) { /* user has quit the blame view */
4243 err = got_error(GOT_ERR_ITER_COMPLETED);
4244 goto done;
4247 if (lineno == -1)
4248 goto done; /* no change in this commit */
4250 line = &a->lines[lineno - 1];
4251 if (line->annotated)
4252 goto done;
4254 line->id = got_object_id_dup(id);
4255 if (line->id == NULL) {
4256 err = got_error_from_errno("got_object_id_dup");
4257 goto done;
4259 line->annotated = 1;
4260 done:
4261 errcode = pthread_mutex_unlock(&tog_mutex);
4262 if (errcode)
4263 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4264 return err;
4267 static void *
4268 blame_thread(void *arg)
4270 const struct got_error *err, *close_err;
4271 struct tog_blame_thread_args *ta = arg;
4272 struct tog_blame_cb_args *a = ta->cb_args;
4273 int errcode;
4275 err = block_signals_used_by_main_thread();
4276 if (err)
4277 return (void *)err;
4279 err = got_blame(ta->path, a->commit_id, ta->repo,
4280 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4281 if (err && err->code == GOT_ERR_CANCELLED)
4282 err = NULL;
4284 errcode = pthread_mutex_lock(&tog_mutex);
4285 if (errcode)
4286 return (void *)got_error_set_errno(errcode,
4287 "pthread_mutex_lock");
4289 close_err = got_repo_close(ta->repo);
4290 if (err == NULL)
4291 err = close_err;
4292 ta->repo = NULL;
4293 *ta->complete = 1;
4295 errcode = pthread_mutex_unlock(&tog_mutex);
4296 if (errcode && err == NULL)
4297 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4299 return (void *)err;
4302 static struct got_object_id *
4303 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4304 int first_displayed_line, int selected_line)
4306 struct tog_blame_line *line;
4308 if (nlines <= 0)
4309 return NULL;
4311 line = &lines[first_displayed_line - 1 + selected_line - 1];
4312 if (!line->annotated)
4313 return NULL;
4315 return line->id;
4318 static const struct got_error *
4319 stop_blame(struct tog_blame *blame)
4321 const struct got_error *err = NULL;
4322 int i;
4324 if (blame->thread) {
4325 int errcode;
4326 errcode = pthread_mutex_unlock(&tog_mutex);
4327 if (errcode)
4328 return got_error_set_errno(errcode,
4329 "pthread_mutex_unlock");
4330 errcode = pthread_join(blame->thread, (void **)&err);
4331 if (errcode)
4332 return got_error_set_errno(errcode, "pthread_join");
4333 errcode = pthread_mutex_lock(&tog_mutex);
4334 if (errcode)
4335 return got_error_set_errno(errcode,
4336 "pthread_mutex_lock");
4337 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4338 err = NULL;
4339 blame->thread = 0; //NULL;
4341 if (blame->thread_args.repo) {
4342 const struct got_error *close_err;
4343 close_err = got_repo_close(blame->thread_args.repo);
4344 if (err == NULL)
4345 err = close_err;
4346 blame->thread_args.repo = NULL;
4348 if (blame->f) {
4349 if (fclose(blame->f) == EOF && err == NULL)
4350 err = got_error_from_errno("fclose");
4351 blame->f = NULL;
4353 if (blame->lines) {
4354 for (i = 0; i < blame->nlines; i++)
4355 free(blame->lines[i].id);
4356 free(blame->lines);
4357 blame->lines = NULL;
4359 free(blame->cb_args.commit_id);
4360 blame->cb_args.commit_id = NULL;
4362 return err;
4365 static const struct got_error *
4366 cancel_blame_view(void *arg)
4368 const struct got_error *err = NULL;
4369 int *done = arg;
4370 int errcode;
4372 errcode = pthread_mutex_lock(&tog_mutex);
4373 if (errcode)
4374 return got_error_set_errno(errcode,
4375 "pthread_mutex_unlock");
4377 if (*done)
4378 err = got_error(GOT_ERR_CANCELLED);
4380 errcode = pthread_mutex_unlock(&tog_mutex);
4381 if (errcode)
4382 return got_error_set_errno(errcode,
4383 "pthread_mutex_lock");
4385 return err;
4388 static const struct got_error *
4389 run_blame(struct tog_view *view)
4391 struct tog_blame_view_state *s = &view->state.blame;
4392 struct tog_blame *blame = &s->blame;
4393 const struct got_error *err = NULL;
4394 struct got_commit_object *commit = NULL;
4395 struct got_blob_object *blob = NULL;
4396 struct got_repository *thread_repo = NULL;
4397 struct got_object_id *obj_id = NULL;
4398 int obj_type;
4400 err = got_object_open_as_commit(&commit, s->repo,
4401 &s->blamed_commit->id);
4402 if (err)
4403 return err;
4405 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4406 if (err)
4407 goto done;
4409 err = got_object_get_type(&obj_type, s->repo, obj_id);
4410 if (err)
4411 goto done;
4413 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4414 err = got_error(GOT_ERR_OBJ_TYPE);
4415 goto done;
4418 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4419 if (err)
4420 goto done;
4421 blame->f = got_opentemp();
4422 if (blame->f == NULL) {
4423 err = got_error_from_errno("got_opentemp");
4424 goto done;
4426 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4427 &blame->line_offsets, blame->f, blob);
4428 if (err)
4429 goto done;
4430 if (blame->nlines == 0) {
4431 s->blame_complete = 1;
4432 goto done;
4435 /* Don't include \n at EOF in the blame line count. */
4436 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4437 blame->nlines--;
4439 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4440 if (blame->lines == NULL) {
4441 err = got_error_from_errno("calloc");
4442 goto done;
4445 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4446 if (err)
4447 goto done;
4449 blame->cb_args.view = view;
4450 blame->cb_args.lines = blame->lines;
4451 blame->cb_args.nlines = blame->nlines;
4452 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4453 if (blame->cb_args.commit_id == NULL) {
4454 err = got_error_from_errno("got_object_id_dup");
4455 goto done;
4457 blame->cb_args.quit = &s->done;
4459 blame->thread_args.path = s->path;
4460 blame->thread_args.repo = thread_repo;
4461 blame->thread_args.cb_args = &blame->cb_args;
4462 blame->thread_args.complete = &s->blame_complete;
4463 blame->thread_args.cancel_cb = cancel_blame_view;
4464 blame->thread_args.cancel_arg = &s->done;
4465 s->blame_complete = 0;
4467 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4468 s->first_displayed_line = 1;
4469 s->last_displayed_line = view->nlines;
4470 s->selected_line = 1;
4472 s->matched_line = 0;
4474 done:
4475 if (commit)
4476 got_object_commit_close(commit);
4477 if (blob)
4478 got_object_blob_close(blob);
4479 free(obj_id);
4480 if (err)
4481 stop_blame(blame);
4482 return err;
4485 static const struct got_error *
4486 open_blame_view(struct tog_view *view, char *path,
4487 struct got_object_id *commit_id, struct got_repository *repo)
4489 const struct got_error *err = NULL;
4490 struct tog_blame_view_state *s = &view->state.blame;
4492 STAILQ_INIT(&s->blamed_commits);
4494 s->path = strdup(path);
4495 if (s->path == NULL)
4496 return got_error_from_errno("strdup");
4498 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4499 if (err) {
4500 free(s->path);
4501 return err;
4504 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4505 s->first_displayed_line = 1;
4506 s->last_displayed_line = view->nlines;
4507 s->selected_line = 1;
4508 s->blame_complete = 0;
4509 s->repo = repo;
4510 s->commit_id = commit_id;
4511 memset(&s->blame, 0, sizeof(s->blame));
4513 STAILQ_INIT(&s->colors);
4514 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4515 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4516 get_color_value("TOG_COLOR_COMMIT"));
4517 if (err)
4518 return err;
4521 view->show = show_blame_view;
4522 view->input = input_blame_view;
4523 view->close = close_blame_view;
4524 view->search_start = search_start_blame_view;
4525 view->search_next = search_next_blame_view;
4527 return run_blame(view);
4530 static const struct got_error *
4531 close_blame_view(struct tog_view *view)
4533 const struct got_error *err = NULL;
4534 struct tog_blame_view_state *s = &view->state.blame;
4536 if (s->blame.thread)
4537 err = stop_blame(&s->blame);
4539 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4540 struct got_object_qid *blamed_commit;
4541 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4542 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4543 got_object_qid_free(blamed_commit);
4546 free(s->path);
4547 free_colors(&s->colors);
4549 return err;
4552 static const struct got_error *
4553 search_start_blame_view(struct tog_view *view)
4555 struct tog_blame_view_state *s = &view->state.blame;
4557 s->matched_line = 0;
4558 return NULL;
4561 static const struct got_error *
4562 search_next_blame_view(struct tog_view *view)
4564 struct tog_blame_view_state *s = &view->state.blame;
4565 int lineno;
4566 char *line = NULL;
4567 size_t linesize = 0;
4568 ssize_t linelen;
4570 if (!view->searching) {
4571 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4572 return NULL;
4575 if (s->matched_line) {
4576 if (view->searching == TOG_SEARCH_FORWARD)
4577 lineno = s->matched_line + 1;
4578 else
4579 lineno = s->matched_line - 1;
4580 } else
4581 lineno = s->first_displayed_line - 1 + s->selected_line;
4583 while (1) {
4584 off_t offset;
4586 if (lineno <= 0 || lineno > s->blame.nlines) {
4587 if (s->matched_line == 0) {
4588 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4589 break;
4592 if (view->searching == TOG_SEARCH_FORWARD)
4593 lineno = 1;
4594 else
4595 lineno = s->blame.nlines;
4598 offset = s->blame.line_offsets[lineno - 1];
4599 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4600 free(line);
4601 return got_error_from_errno("fseeko");
4603 linelen = getline(&line, &linesize, s->blame.f);
4604 if (linelen != -1 &&
4605 match_line(line, &view->regex, 1, &view->regmatch)) {
4606 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4607 s->matched_line = lineno;
4608 break;
4610 if (view->searching == TOG_SEARCH_FORWARD)
4611 lineno++;
4612 else
4613 lineno--;
4615 free(line);
4617 if (s->matched_line) {
4618 s->first_displayed_line = s->matched_line;
4619 s->selected_line = 1;
4622 return NULL;
4625 static const struct got_error *
4626 show_blame_view(struct tog_view *view)
4628 const struct got_error *err = NULL;
4629 struct tog_blame_view_state *s = &view->state.blame;
4630 int errcode;
4632 if (s->blame.thread == 0 && !s->blame_complete) {
4633 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4634 &s->blame.thread_args);
4635 if (errcode)
4636 return got_error_set_errno(errcode, "pthread_create");
4638 halfdelay(1); /* fast refresh while annotating */
4641 if (s->blame_complete)
4642 halfdelay(10); /* disable fast refresh */
4644 err = draw_blame(view);
4646 view_vborder(view);
4647 return err;
4650 static const struct got_error *
4651 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4653 const struct got_error *err = NULL, *thread_err = NULL;
4654 struct tog_view *diff_view;
4655 struct tog_blame_view_state *s = &view->state.blame;
4656 int begin_x = 0, nscroll = view->nlines - 2;
4658 switch (ch) {
4659 case 'q':
4660 s->done = 1;
4661 break;
4662 case 'g':
4663 case KEY_HOME:
4664 s->selected_line = 1;
4665 s->first_displayed_line = 1;
4666 break;
4667 case 'G':
4668 case KEY_END:
4669 if (s->blame.nlines < view->nlines - 2) {
4670 s->selected_line = s->blame.nlines;
4671 s->first_displayed_line = 1;
4672 } else {
4673 s->selected_line = view->nlines - 2;
4674 s->first_displayed_line = s->blame.nlines -
4675 (view->nlines - 3);
4677 break;
4678 case 'k':
4679 case KEY_UP:
4680 case CTRL('p'):
4681 if (s->selected_line > 1)
4682 s->selected_line--;
4683 else if (s->selected_line == 1 &&
4684 s->first_displayed_line > 1)
4685 s->first_displayed_line--;
4686 break;
4687 case CTRL('u'):
4688 case 'u':
4689 nscroll /= 2;
4690 /* FALL THROUGH */
4691 case KEY_PPAGE:
4692 case CTRL('b'):
4693 if (s->first_displayed_line == 1) {
4694 s->selected_line = MAX(1, s->selected_line - nscroll);
4695 break;
4697 if (s->first_displayed_line > nscroll)
4698 s->first_displayed_line -= nscroll;
4699 else
4700 s->first_displayed_line = 1;
4701 break;
4702 case 'j':
4703 case KEY_DOWN:
4704 case CTRL('n'):
4705 if (s->selected_line < view->nlines - 2 &&
4706 s->first_displayed_line +
4707 s->selected_line <= s->blame.nlines)
4708 s->selected_line++;
4709 else if (s->last_displayed_line <
4710 s->blame.nlines)
4711 s->first_displayed_line++;
4712 break;
4713 case 'b':
4714 case 'p': {
4715 struct got_object_id *id = NULL;
4716 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4717 s->first_displayed_line, s->selected_line);
4718 if (id == NULL)
4719 break;
4720 if (ch == 'p') {
4721 struct got_commit_object *commit, *pcommit;
4722 struct got_object_qid *pid;
4723 struct got_object_id *blob_id = NULL;
4724 int obj_type;
4725 err = got_object_open_as_commit(&commit,
4726 s->repo, id);
4727 if (err)
4728 break;
4729 pid = STAILQ_FIRST(
4730 got_object_commit_get_parent_ids(commit));
4731 if (pid == NULL) {
4732 got_object_commit_close(commit);
4733 break;
4735 /* Check if path history ends here. */
4736 err = got_object_open_as_commit(&pcommit,
4737 s->repo, &pid->id);
4738 if (err)
4739 break;
4740 err = got_object_id_by_path(&blob_id, s->repo,
4741 pcommit, s->path);
4742 got_object_commit_close(pcommit);
4743 if (err) {
4744 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4745 err = NULL;
4746 got_object_commit_close(commit);
4747 break;
4749 err = got_object_get_type(&obj_type, s->repo,
4750 blob_id);
4751 free(blob_id);
4752 /* Can't blame non-blob type objects. */
4753 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4754 got_object_commit_close(commit);
4755 break;
4757 err = got_object_qid_alloc(&s->blamed_commit,
4758 &pid->id);
4759 got_object_commit_close(commit);
4760 } else {
4761 if (got_object_id_cmp(id,
4762 &s->blamed_commit->id) == 0)
4763 break;
4764 err = got_object_qid_alloc(&s->blamed_commit,
4765 id);
4767 if (err)
4768 break;
4769 s->done = 1;
4770 thread_err = stop_blame(&s->blame);
4771 s->done = 0;
4772 if (thread_err)
4773 break;
4774 STAILQ_INSERT_HEAD(&s->blamed_commits,
4775 s->blamed_commit, entry);
4776 err = run_blame(view);
4777 if (err)
4778 break;
4779 break;
4781 case 'B': {
4782 struct got_object_qid *first;
4783 first = STAILQ_FIRST(&s->blamed_commits);
4784 if (!got_object_id_cmp(&first->id, s->commit_id))
4785 break;
4786 s->done = 1;
4787 thread_err = stop_blame(&s->blame);
4788 s->done = 0;
4789 if (thread_err)
4790 break;
4791 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4792 got_object_qid_free(s->blamed_commit);
4793 s->blamed_commit =
4794 STAILQ_FIRST(&s->blamed_commits);
4795 err = run_blame(view);
4796 if (err)
4797 break;
4798 break;
4800 case KEY_ENTER:
4801 case '\r': {
4802 struct got_object_id *id = NULL;
4803 struct got_object_qid *pid;
4804 struct got_commit_object *commit = NULL;
4805 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4806 s->first_displayed_line, s->selected_line);
4807 if (id == NULL)
4808 break;
4809 err = got_object_open_as_commit(&commit, s->repo, id);
4810 if (err)
4811 break;
4812 pid = STAILQ_FIRST(
4813 got_object_commit_get_parent_ids(commit));
4814 if (view_is_parent_view(view))
4815 begin_x = view_split_begin_x(view->begin_x);
4816 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4817 if (diff_view == NULL) {
4818 got_object_commit_close(commit);
4819 err = got_error_from_errno("view_open");
4820 break;
4822 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4823 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4824 got_object_commit_close(commit);
4825 if (err) {
4826 view_close(diff_view);
4827 break;
4829 view->focussed = 0;
4830 diff_view->focussed = 1;
4831 if (view_is_parent_view(view)) {
4832 err = view_close_child(view);
4833 if (err)
4834 break;
4835 view_set_child(view, diff_view);
4836 view->focus_child = 1;
4837 } else
4838 *new_view = diff_view;
4839 if (err)
4840 break;
4841 break;
4843 case CTRL('d'):
4844 case 'd':
4845 nscroll /= 2;
4846 /* FALL THROUGH */
4847 case KEY_NPAGE:
4848 case CTRL('f'):
4849 case ' ':
4850 if (s->last_displayed_line >= s->blame.nlines &&
4851 s->selected_line >= MIN(s->blame.nlines,
4852 view->nlines - 2)) {
4853 break;
4855 if (s->last_displayed_line >= s->blame.nlines &&
4856 s->selected_line < view->nlines - 2) {
4857 s->selected_line +=
4858 MIN(nscroll, s->last_displayed_line -
4859 s->first_displayed_line - s->selected_line + 1);
4861 if (s->last_displayed_line + nscroll <= s->blame.nlines)
4862 s->first_displayed_line += nscroll;
4863 else
4864 s->first_displayed_line =
4865 s->blame.nlines - (view->nlines - 3);
4866 break;
4867 case KEY_RESIZE:
4868 if (s->selected_line > view->nlines - 2) {
4869 s->selected_line = MIN(s->blame.nlines,
4870 view->nlines - 2);
4872 break;
4873 default:
4874 break;
4876 return thread_err ? thread_err : err;
4879 static const struct got_error *
4880 cmd_blame(int argc, char *argv[])
4882 const struct got_error *error;
4883 struct got_repository *repo = NULL;
4884 struct got_worktree *worktree = NULL;
4885 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4886 char *link_target = NULL;
4887 struct got_object_id *commit_id = NULL;
4888 struct got_commit_object *commit = NULL;
4889 char *commit_id_str = NULL;
4890 int ch;
4891 struct tog_view *view;
4893 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4894 switch (ch) {
4895 case 'c':
4896 commit_id_str = optarg;
4897 break;
4898 case 'r':
4899 repo_path = realpath(optarg, NULL);
4900 if (repo_path == NULL)
4901 return got_error_from_errno2("realpath",
4902 optarg);
4903 break;
4904 default:
4905 usage_blame();
4906 /* NOTREACHED */
4910 argc -= optind;
4911 argv += optind;
4913 if (argc != 1)
4914 usage_blame();
4916 if (repo_path == NULL) {
4917 cwd = getcwd(NULL, 0);
4918 if (cwd == NULL)
4919 return got_error_from_errno("getcwd");
4920 error = got_worktree_open(&worktree, cwd);
4921 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4922 goto done;
4923 if (worktree)
4924 repo_path =
4925 strdup(got_worktree_get_repo_path(worktree));
4926 else
4927 repo_path = strdup(cwd);
4928 if (repo_path == NULL) {
4929 error = got_error_from_errno("strdup");
4930 goto done;
4934 error = got_repo_open(&repo, repo_path, NULL);
4935 if (error != NULL)
4936 goto done;
4938 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4939 worktree);
4940 if (error)
4941 goto done;
4943 init_curses();
4945 error = apply_unveil(got_repo_get_path(repo), NULL);
4946 if (error)
4947 goto done;
4949 error = tog_load_refs(repo, 0);
4950 if (error)
4951 goto done;
4953 if (commit_id_str == NULL) {
4954 struct got_reference *head_ref;
4955 error = got_ref_open(&head_ref, repo, worktree ?
4956 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4957 if (error != NULL)
4958 goto done;
4959 error = got_ref_resolve(&commit_id, repo, head_ref);
4960 got_ref_close(head_ref);
4961 } else {
4962 error = got_repo_match_object_id(&commit_id, NULL,
4963 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4965 if (error != NULL)
4966 goto done;
4968 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4969 if (view == NULL) {
4970 error = got_error_from_errno("view_open");
4971 goto done;
4974 error = got_object_open_as_commit(&commit, repo, commit_id);
4975 if (error)
4976 goto done;
4978 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4979 commit, repo);
4980 if (error)
4981 goto done;
4983 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4984 commit_id, repo);
4985 if (error)
4986 goto done;
4987 if (worktree) {
4988 /* Release work tree lock. */
4989 got_worktree_close(worktree);
4990 worktree = NULL;
4992 error = view_loop(view);
4993 done:
4994 free(repo_path);
4995 free(in_repo_path);
4996 free(link_target);
4997 free(cwd);
4998 free(commit_id);
4999 if (commit)
5000 got_object_commit_close(commit);
5001 if (worktree)
5002 got_worktree_close(worktree);
5003 if (repo) {
5004 const struct got_error *close_err = got_repo_close(repo);
5005 if (error == NULL)
5006 error = close_err;
5008 tog_free_refs();
5009 return error;
5012 static const struct got_error *
5013 draw_tree_entries(struct tog_view *view, const char *parent_path)
5015 struct tog_tree_view_state *s = &view->state.tree;
5016 const struct got_error *err = NULL;
5017 struct got_tree_entry *te;
5018 wchar_t *wline;
5019 struct tog_color *tc;
5020 int width, n, i, nentries;
5021 int limit = view->nlines;
5023 s->ndisplayed = 0;
5025 werase(view->window);
5027 if (limit == 0)
5028 return NULL;
5030 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5031 if (err)
5032 return err;
5033 if (view_needs_focus_indication(view))
5034 wstandout(view->window);
5035 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5036 if (tc)
5037 wattr_on(view->window,
5038 COLOR_PAIR(tc->colorpair), NULL);
5039 waddwstr(view->window, wline);
5040 if (tc)
5041 wattr_off(view->window,
5042 COLOR_PAIR(tc->colorpair), NULL);
5043 if (view_needs_focus_indication(view))
5044 wstandend(view->window);
5045 free(wline);
5046 wline = NULL;
5047 if (width < view->ncols - 1)
5048 waddch(view->window, '\n');
5049 if (--limit <= 0)
5050 return NULL;
5051 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5052 if (err)
5053 return err;
5054 waddwstr(view->window, wline);
5055 free(wline);
5056 wline = NULL;
5057 if (width < view->ncols - 1)
5058 waddch(view->window, '\n');
5059 if (--limit <= 0)
5060 return NULL;
5061 waddch(view->window, '\n');
5062 if (--limit <= 0)
5063 return NULL;
5065 if (s->first_displayed_entry == NULL) {
5066 te = got_object_tree_get_first_entry(s->tree);
5067 if (s->selected == 0) {
5068 if (view->focussed)
5069 wstandout(view->window);
5070 s->selected_entry = NULL;
5072 waddstr(view->window, " ..\n"); /* parent directory */
5073 if (s->selected == 0 && view->focussed)
5074 wstandend(view->window);
5075 s->ndisplayed++;
5076 if (--limit <= 0)
5077 return NULL;
5078 n = 1;
5079 } else {
5080 n = 0;
5081 te = s->first_displayed_entry;
5084 nentries = got_object_tree_get_nentries(s->tree);
5085 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5086 char *line = NULL, *id_str = NULL, *link_target = NULL;
5087 const char *modestr = "";
5088 mode_t mode;
5090 te = got_object_tree_get_entry(s->tree, i);
5091 mode = got_tree_entry_get_mode(te);
5093 if (s->show_ids) {
5094 err = got_object_id_str(&id_str,
5095 got_tree_entry_get_id(te));
5096 if (err)
5097 return got_error_from_errno(
5098 "got_object_id_str");
5100 if (got_object_tree_entry_is_submodule(te))
5101 modestr = "$";
5102 else if (S_ISLNK(mode)) {
5103 int i;
5105 err = got_tree_entry_get_symlink_target(&link_target,
5106 te, s->repo);
5107 if (err) {
5108 free(id_str);
5109 return err;
5111 for (i = 0; i < strlen(link_target); i++) {
5112 if (!isprint((unsigned char)link_target[i]))
5113 link_target[i] = '?';
5115 modestr = "@";
5117 else if (S_ISDIR(mode))
5118 modestr = "/";
5119 else if (mode & S_IXUSR)
5120 modestr = "*";
5121 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5122 got_tree_entry_get_name(te), modestr,
5123 link_target ? " -> ": "",
5124 link_target ? link_target : "") == -1) {
5125 free(id_str);
5126 free(link_target);
5127 return got_error_from_errno("asprintf");
5129 free(id_str);
5130 free(link_target);
5131 err = format_line(&wline, &width, line, view->ncols, 0);
5132 if (err) {
5133 free(line);
5134 break;
5136 if (n == s->selected) {
5137 if (view->focussed)
5138 wstandout(view->window);
5139 s->selected_entry = te;
5141 tc = match_color(&s->colors, line);
5142 if (tc)
5143 wattr_on(view->window,
5144 COLOR_PAIR(tc->colorpair), NULL);
5145 waddwstr(view->window, wline);
5146 if (tc)
5147 wattr_off(view->window,
5148 COLOR_PAIR(tc->colorpair), NULL);
5149 if (width < view->ncols - 1)
5150 waddch(view->window, '\n');
5151 if (n == s->selected && view->focussed)
5152 wstandend(view->window);
5153 free(line);
5154 free(wline);
5155 wline = NULL;
5156 n++;
5157 s->ndisplayed++;
5158 s->last_displayed_entry = te;
5159 if (--limit <= 0)
5160 break;
5163 return err;
5166 static void
5167 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5169 struct got_tree_entry *te;
5170 int isroot = s->tree == s->root;
5171 int i = 0;
5173 if (s->first_displayed_entry == NULL)
5174 return;
5176 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5177 while (i++ < maxscroll) {
5178 if (te == NULL) {
5179 if (!isroot)
5180 s->first_displayed_entry = NULL;
5181 break;
5183 s->first_displayed_entry = te;
5184 te = got_tree_entry_get_prev(s->tree, te);
5188 static void
5189 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5191 struct got_tree_entry *next, *last;
5192 int n = 0;
5194 if (s->first_displayed_entry)
5195 next = got_tree_entry_get_next(s->tree,
5196 s->first_displayed_entry);
5197 else
5198 next = got_object_tree_get_first_entry(s->tree);
5200 last = s->last_displayed_entry;
5201 while (next && last && n++ < maxscroll) {
5202 last = got_tree_entry_get_next(s->tree, last);
5203 if (last) {
5204 s->first_displayed_entry = next;
5205 next = got_tree_entry_get_next(s->tree, next);
5210 static const struct got_error *
5211 tree_entry_path(char **path, struct tog_parent_trees *parents,
5212 struct got_tree_entry *te)
5214 const struct got_error *err = NULL;
5215 struct tog_parent_tree *pt;
5216 size_t len = 2; /* for leading slash and NUL */
5218 TAILQ_FOREACH(pt, parents, entry)
5219 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5220 + 1 /* slash */;
5221 if (te)
5222 len += strlen(got_tree_entry_get_name(te));
5224 *path = calloc(1, len);
5225 if (path == NULL)
5226 return got_error_from_errno("calloc");
5228 (*path)[0] = '/';
5229 pt = TAILQ_LAST(parents, tog_parent_trees);
5230 while (pt) {
5231 const char *name = got_tree_entry_get_name(pt->selected_entry);
5232 if (strlcat(*path, name, len) >= len) {
5233 err = got_error(GOT_ERR_NO_SPACE);
5234 goto done;
5236 if (strlcat(*path, "/", len) >= len) {
5237 err = got_error(GOT_ERR_NO_SPACE);
5238 goto done;
5240 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5242 if (te) {
5243 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5244 err = got_error(GOT_ERR_NO_SPACE);
5245 goto done;
5248 done:
5249 if (err) {
5250 free(*path);
5251 *path = NULL;
5253 return err;
5256 static const struct got_error *
5257 blame_tree_entry(struct tog_view **new_view, int begin_x,
5258 struct got_tree_entry *te, struct tog_parent_trees *parents,
5259 struct got_object_id *commit_id, struct got_repository *repo)
5261 const struct got_error *err = NULL;
5262 char *path;
5263 struct tog_view *blame_view;
5265 *new_view = NULL;
5267 err = tree_entry_path(&path, parents, te);
5268 if (err)
5269 return err;
5271 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5272 if (blame_view == NULL) {
5273 err = got_error_from_errno("view_open");
5274 goto done;
5277 err = open_blame_view(blame_view, path, commit_id, repo);
5278 if (err) {
5279 if (err->code == GOT_ERR_CANCELLED)
5280 err = NULL;
5281 view_close(blame_view);
5282 } else
5283 *new_view = blame_view;
5284 done:
5285 free(path);
5286 return err;
5289 static const struct got_error *
5290 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5291 struct tog_tree_view_state *s)
5293 struct tog_view *log_view;
5294 const struct got_error *err = NULL;
5295 char *path;
5297 *new_view = NULL;
5299 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5300 if (log_view == NULL)
5301 return got_error_from_errno("view_open");
5303 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5304 if (err)
5305 return err;
5307 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5308 path, 0);
5309 if (err)
5310 view_close(log_view);
5311 else
5312 *new_view = log_view;
5313 free(path);
5314 return err;
5317 static const struct got_error *
5318 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5319 const char *head_ref_name, struct got_repository *repo)
5321 const struct got_error *err = NULL;
5322 char *commit_id_str = NULL;
5323 struct tog_tree_view_state *s = &view->state.tree;
5324 struct got_commit_object *commit = NULL;
5326 TAILQ_INIT(&s->parents);
5327 STAILQ_INIT(&s->colors);
5329 s->commit_id = got_object_id_dup(commit_id);
5330 if (s->commit_id == NULL)
5331 return got_error_from_errno("got_object_id_dup");
5333 err = got_object_open_as_commit(&commit, repo, commit_id);
5334 if (err)
5335 goto done;
5338 * The root is opened here and will be closed when the view is closed.
5339 * Any visited subtrees and their path-wise parents are opened and
5340 * closed on demand.
5342 err = got_object_open_as_tree(&s->root, repo,
5343 got_object_commit_get_tree_id(commit));
5344 if (err)
5345 goto done;
5346 s->tree = s->root;
5348 err = got_object_id_str(&commit_id_str, commit_id);
5349 if (err != NULL)
5350 goto done;
5352 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5353 err = got_error_from_errno("asprintf");
5354 goto done;
5357 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5358 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5359 if (head_ref_name) {
5360 s->head_ref_name = strdup(head_ref_name);
5361 if (s->head_ref_name == NULL) {
5362 err = got_error_from_errno("strdup");
5363 goto done;
5366 s->repo = repo;
5368 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5369 err = add_color(&s->colors, "\\$$",
5370 TOG_COLOR_TREE_SUBMODULE,
5371 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5372 if (err)
5373 goto done;
5374 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5375 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5376 if (err)
5377 goto done;
5378 err = add_color(&s->colors, "/$",
5379 TOG_COLOR_TREE_DIRECTORY,
5380 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5381 if (err)
5382 goto done;
5384 err = add_color(&s->colors, "\\*$",
5385 TOG_COLOR_TREE_EXECUTABLE,
5386 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5387 if (err)
5388 goto done;
5390 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5391 get_color_value("TOG_COLOR_COMMIT"));
5392 if (err)
5393 goto done;
5396 view->show = show_tree_view;
5397 view->input = input_tree_view;
5398 view->close = close_tree_view;
5399 view->search_start = search_start_tree_view;
5400 view->search_next = search_next_tree_view;
5401 done:
5402 free(commit_id_str);
5403 if (commit)
5404 got_object_commit_close(commit);
5405 if (err)
5406 close_tree_view(view);
5407 return err;
5410 static const struct got_error *
5411 close_tree_view(struct tog_view *view)
5413 struct tog_tree_view_state *s = &view->state.tree;
5415 free_colors(&s->colors);
5416 free(s->tree_label);
5417 s->tree_label = NULL;
5418 free(s->commit_id);
5419 s->commit_id = NULL;
5420 free(s->head_ref_name);
5421 s->head_ref_name = NULL;
5422 while (!TAILQ_EMPTY(&s->parents)) {
5423 struct tog_parent_tree *parent;
5424 parent = TAILQ_FIRST(&s->parents);
5425 TAILQ_REMOVE(&s->parents, parent, entry);
5426 if (parent->tree != s->root)
5427 got_object_tree_close(parent->tree);
5428 free(parent);
5431 if (s->tree != NULL && s->tree != s->root)
5432 got_object_tree_close(s->tree);
5433 if (s->root)
5434 got_object_tree_close(s->root);
5435 return NULL;
5438 static const struct got_error *
5439 search_start_tree_view(struct tog_view *view)
5441 struct tog_tree_view_state *s = &view->state.tree;
5443 s->matched_entry = NULL;
5444 return NULL;
5447 static int
5448 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5450 regmatch_t regmatch;
5452 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5453 0) == 0;
5456 static const struct got_error *
5457 search_next_tree_view(struct tog_view *view)
5459 struct tog_tree_view_state *s = &view->state.tree;
5460 struct got_tree_entry *te = NULL;
5462 if (!view->searching) {
5463 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5464 return NULL;
5467 if (s->matched_entry) {
5468 if (view->searching == TOG_SEARCH_FORWARD) {
5469 if (s->selected_entry)
5470 te = got_tree_entry_get_next(s->tree,
5471 s->selected_entry);
5472 else
5473 te = got_object_tree_get_first_entry(s->tree);
5474 } else {
5475 if (s->selected_entry == NULL)
5476 te = got_object_tree_get_last_entry(s->tree);
5477 else
5478 te = got_tree_entry_get_prev(s->tree,
5479 s->selected_entry);
5481 } else {
5482 if (s->selected_entry)
5483 te = s->selected_entry;
5484 else if (view->searching == TOG_SEARCH_FORWARD)
5485 te = got_object_tree_get_first_entry(s->tree);
5486 else
5487 te = got_object_tree_get_last_entry(s->tree);
5490 while (1) {
5491 if (te == NULL) {
5492 if (s->matched_entry == NULL) {
5493 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5494 return NULL;
5496 if (view->searching == TOG_SEARCH_FORWARD)
5497 te = got_object_tree_get_first_entry(s->tree);
5498 else
5499 te = got_object_tree_get_last_entry(s->tree);
5502 if (match_tree_entry(te, &view->regex)) {
5503 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5504 s->matched_entry = te;
5505 break;
5508 if (view->searching == TOG_SEARCH_FORWARD)
5509 te = got_tree_entry_get_next(s->tree, te);
5510 else
5511 te = got_tree_entry_get_prev(s->tree, te);
5514 if (s->matched_entry) {
5515 s->first_displayed_entry = s->matched_entry;
5516 s->selected = 0;
5519 return NULL;
5522 static const struct got_error *
5523 show_tree_view(struct tog_view *view)
5525 const struct got_error *err = NULL;
5526 struct tog_tree_view_state *s = &view->state.tree;
5527 char *parent_path;
5529 err = tree_entry_path(&parent_path, &s->parents, NULL);
5530 if (err)
5531 return err;
5533 err = draw_tree_entries(view, parent_path);
5534 free(parent_path);
5536 view_vborder(view);
5537 return err;
5540 static const struct got_error *
5541 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5543 const struct got_error *err = NULL;
5544 struct tog_tree_view_state *s = &view->state.tree;
5545 struct tog_view *log_view, *ref_view;
5546 struct got_tree_entry *te;
5547 int begin_x = 0, n, nscroll = view->nlines - 3;
5549 switch (ch) {
5550 case 'i':
5551 s->show_ids = !s->show_ids;
5552 break;
5553 case 'l':
5554 if (!s->selected_entry)
5555 break;
5556 if (view_is_parent_view(view))
5557 begin_x = view_split_begin_x(view->begin_x);
5558 err = log_selected_tree_entry(&log_view, begin_x, s);
5559 view->focussed = 0;
5560 log_view->focussed = 1;
5561 if (view_is_parent_view(view)) {
5562 err = view_close_child(view);
5563 if (err)
5564 return err;
5565 view_set_child(view, log_view);
5566 view->focus_child = 1;
5567 } else
5568 *new_view = log_view;
5569 break;
5570 case 'r':
5571 if (view_is_parent_view(view))
5572 begin_x = view_split_begin_x(view->begin_x);
5573 ref_view = view_open(view->nlines, view->ncols,
5574 view->begin_y, begin_x, TOG_VIEW_REF);
5575 if (ref_view == NULL)
5576 return got_error_from_errno("view_open");
5577 err = open_ref_view(ref_view, s->repo);
5578 if (err) {
5579 view_close(ref_view);
5580 return err;
5582 view->focussed = 0;
5583 ref_view->focussed = 1;
5584 if (view_is_parent_view(view)) {
5585 err = view_close_child(view);
5586 if (err)
5587 return err;
5588 view_set_child(view, ref_view);
5589 view->focus_child = 1;
5590 } else
5591 *new_view = ref_view;
5592 break;
5593 case 'g':
5594 case KEY_HOME:
5595 s->selected = 0;
5596 if (s->tree == s->root)
5597 s->first_displayed_entry =
5598 got_object_tree_get_first_entry(s->tree);
5599 else
5600 s->first_displayed_entry = NULL;
5601 break;
5602 case 'G':
5603 case KEY_END:
5604 s->selected = 0;
5605 te = got_object_tree_get_last_entry(s->tree);
5606 for (n = 0; n < view->nlines - 3; n++) {
5607 if (te == NULL) {
5608 if(s->tree != s->root) {
5609 s->first_displayed_entry = NULL;
5610 n++;
5612 break;
5614 s->first_displayed_entry = te;
5615 te = got_tree_entry_get_prev(s->tree, te);
5617 if (n > 0)
5618 s->selected = n - 1;
5619 break;
5620 case 'k':
5621 case KEY_UP:
5622 case CTRL('p'):
5623 if (s->selected > 0) {
5624 s->selected--;
5625 break;
5627 tree_scroll_up(s, 1);
5628 break;
5629 case CTRL('u'):
5630 case 'u':
5631 nscroll /= 2;
5632 /* FALL THROUGH */
5633 case KEY_PPAGE:
5634 case CTRL('b'):
5635 if (s->tree == s->root) {
5636 if (got_object_tree_get_first_entry(s->tree) ==
5637 s->first_displayed_entry)
5638 s->selected -= MIN(s->selected, nscroll);
5639 } else {
5640 if (s->first_displayed_entry == NULL)
5641 s->selected -= MIN(s->selected, nscroll);
5643 tree_scroll_up(s, MAX(0, nscroll));
5644 break;
5645 case 'j':
5646 case KEY_DOWN:
5647 case CTRL('n'):
5648 if (s->selected < s->ndisplayed - 1) {
5649 s->selected++;
5650 break;
5652 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5653 == NULL)
5654 /* can't scroll any further */
5655 break;
5656 tree_scroll_down(s, 1);
5657 break;
5658 case CTRL('d'):
5659 case 'd':
5660 nscroll /= 2;
5661 /* FALL THROUGH */
5662 case KEY_NPAGE:
5663 case CTRL('f'):
5664 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5665 == NULL) {
5666 /* can't scroll any further; move cursor down */
5667 if (s->selected < s->ndisplayed - 1)
5668 s->selected += MIN(nscroll,
5669 s->ndisplayed - s->selected - 1);
5670 break;
5672 tree_scroll_down(s, nscroll);
5673 break;
5674 case KEY_ENTER:
5675 case '\r':
5676 case KEY_BACKSPACE:
5677 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5678 struct tog_parent_tree *parent;
5679 /* user selected '..' */
5680 if (s->tree == s->root)
5681 break;
5682 parent = TAILQ_FIRST(&s->parents);
5683 TAILQ_REMOVE(&s->parents, parent,
5684 entry);
5685 got_object_tree_close(s->tree);
5686 s->tree = parent->tree;
5687 s->first_displayed_entry =
5688 parent->first_displayed_entry;
5689 s->selected_entry =
5690 parent->selected_entry;
5691 s->selected = parent->selected;
5692 free(parent);
5693 } else if (S_ISDIR(got_tree_entry_get_mode(
5694 s->selected_entry))) {
5695 struct got_tree_object *subtree;
5696 err = got_object_open_as_tree(&subtree, s->repo,
5697 got_tree_entry_get_id(s->selected_entry));
5698 if (err)
5699 break;
5700 err = tree_view_visit_subtree(s, subtree);
5701 if (err) {
5702 got_object_tree_close(subtree);
5703 break;
5705 } else if (S_ISREG(got_tree_entry_get_mode(
5706 s->selected_entry))) {
5707 struct tog_view *blame_view;
5708 int begin_x = view_is_parent_view(view) ?
5709 view_split_begin_x(view->begin_x) : 0;
5711 err = blame_tree_entry(&blame_view, begin_x,
5712 s->selected_entry, &s->parents,
5713 s->commit_id, s->repo);
5714 if (err)
5715 break;
5716 view->focussed = 0;
5717 blame_view->focussed = 1;
5718 if (view_is_parent_view(view)) {
5719 err = view_close_child(view);
5720 if (err)
5721 return err;
5722 view_set_child(view, blame_view);
5723 view->focus_child = 1;
5724 } else
5725 *new_view = blame_view;
5727 break;
5728 case KEY_RESIZE:
5729 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5730 s->selected = view->nlines - 4;
5731 break;
5732 default:
5733 break;
5736 return err;
5739 __dead static void
5740 usage_tree(void)
5742 endwin();
5743 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5744 getprogname());
5745 exit(1);
5748 static const struct got_error *
5749 cmd_tree(int argc, char *argv[])
5751 const struct got_error *error;
5752 struct got_repository *repo = NULL;
5753 struct got_worktree *worktree = NULL;
5754 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5755 struct got_object_id *commit_id = NULL;
5756 struct got_commit_object *commit = NULL;
5757 const char *commit_id_arg = NULL;
5758 char *label = NULL;
5759 struct got_reference *ref = NULL;
5760 const char *head_ref_name = NULL;
5761 int ch;
5762 struct tog_view *view;
5764 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5765 switch (ch) {
5766 case 'c':
5767 commit_id_arg = optarg;
5768 break;
5769 case 'r':
5770 repo_path = realpath(optarg, NULL);
5771 if (repo_path == NULL)
5772 return got_error_from_errno2("realpath",
5773 optarg);
5774 break;
5775 default:
5776 usage_tree();
5777 /* NOTREACHED */
5781 argc -= optind;
5782 argv += optind;
5784 if (argc > 1)
5785 usage_tree();
5787 if (repo_path == NULL) {
5788 cwd = getcwd(NULL, 0);
5789 if (cwd == NULL)
5790 return got_error_from_errno("getcwd");
5791 error = got_worktree_open(&worktree, cwd);
5792 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5793 goto done;
5794 if (worktree)
5795 repo_path =
5796 strdup(got_worktree_get_repo_path(worktree));
5797 else
5798 repo_path = strdup(cwd);
5799 if (repo_path == NULL) {
5800 error = got_error_from_errno("strdup");
5801 goto done;
5805 error = got_repo_open(&repo, repo_path, NULL);
5806 if (error != NULL)
5807 goto done;
5809 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5810 repo, worktree);
5811 if (error)
5812 goto done;
5814 init_curses();
5816 error = apply_unveil(got_repo_get_path(repo), NULL);
5817 if (error)
5818 goto done;
5820 error = tog_load_refs(repo, 0);
5821 if (error)
5822 goto done;
5824 if (commit_id_arg == NULL) {
5825 error = got_repo_match_object_id(&commit_id, &label,
5826 worktree ? got_worktree_get_head_ref_name(worktree) :
5827 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5828 if (error)
5829 goto done;
5830 head_ref_name = label;
5831 } else {
5832 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5833 if (error == NULL)
5834 head_ref_name = got_ref_get_name(ref);
5835 else if (error->code != GOT_ERR_NOT_REF)
5836 goto done;
5837 error = got_repo_match_object_id(&commit_id, NULL,
5838 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5839 if (error)
5840 goto done;
5843 error = got_object_open_as_commit(&commit, repo, commit_id);
5844 if (error)
5845 goto done;
5847 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5848 if (view == NULL) {
5849 error = got_error_from_errno("view_open");
5850 goto done;
5852 error = open_tree_view(view, commit_id, head_ref_name, repo);
5853 if (error)
5854 goto done;
5855 if (!got_path_is_root_dir(in_repo_path)) {
5856 error = tree_view_walk_path(&view->state.tree, commit,
5857 in_repo_path);
5858 if (error)
5859 goto done;
5862 if (worktree) {
5863 /* Release work tree lock. */
5864 got_worktree_close(worktree);
5865 worktree = NULL;
5867 error = view_loop(view);
5868 done:
5869 free(repo_path);
5870 free(cwd);
5871 free(commit_id);
5872 free(label);
5873 if (ref)
5874 got_ref_close(ref);
5875 if (repo) {
5876 const struct got_error *close_err = got_repo_close(repo);
5877 if (error == NULL)
5878 error = close_err;
5880 tog_free_refs();
5881 return error;
5884 static const struct got_error *
5885 ref_view_load_refs(struct tog_ref_view_state *s)
5887 struct got_reflist_entry *sre;
5888 struct tog_reflist_entry *re;
5890 s->nrefs = 0;
5891 TAILQ_FOREACH(sre, &tog_refs, entry) {
5892 if (strncmp(got_ref_get_name(sre->ref),
5893 "refs/got/", 9) == 0 &&
5894 strncmp(got_ref_get_name(sre->ref),
5895 "refs/got/backup/", 16) != 0)
5896 continue;
5898 re = malloc(sizeof(*re));
5899 if (re == NULL)
5900 return got_error_from_errno("malloc");
5902 re->ref = got_ref_dup(sre->ref);
5903 if (re->ref == NULL)
5904 return got_error_from_errno("got_ref_dup");
5905 re->idx = s->nrefs++;
5906 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5909 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5910 return NULL;
5913 void
5914 ref_view_free_refs(struct tog_ref_view_state *s)
5916 struct tog_reflist_entry *re;
5918 while (!TAILQ_EMPTY(&s->refs)) {
5919 re = TAILQ_FIRST(&s->refs);
5920 TAILQ_REMOVE(&s->refs, re, entry);
5921 got_ref_close(re->ref);
5922 free(re);
5926 static const struct got_error *
5927 open_ref_view(struct tog_view *view, struct got_repository *repo)
5929 const struct got_error *err = NULL;
5930 struct tog_ref_view_state *s = &view->state.ref;
5932 s->selected_entry = 0;
5933 s->repo = repo;
5935 TAILQ_INIT(&s->refs);
5936 STAILQ_INIT(&s->colors);
5938 err = ref_view_load_refs(s);
5939 if (err)
5940 return err;
5942 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5943 err = add_color(&s->colors, "^refs/heads/",
5944 TOG_COLOR_REFS_HEADS,
5945 get_color_value("TOG_COLOR_REFS_HEADS"));
5946 if (err)
5947 goto done;
5949 err = add_color(&s->colors, "^refs/tags/",
5950 TOG_COLOR_REFS_TAGS,
5951 get_color_value("TOG_COLOR_REFS_TAGS"));
5952 if (err)
5953 goto done;
5955 err = add_color(&s->colors, "^refs/remotes/",
5956 TOG_COLOR_REFS_REMOTES,
5957 get_color_value("TOG_COLOR_REFS_REMOTES"));
5958 if (err)
5959 goto done;
5961 err = add_color(&s->colors, "^refs/got/backup/",
5962 TOG_COLOR_REFS_BACKUP,
5963 get_color_value("TOG_COLOR_REFS_BACKUP"));
5964 if (err)
5965 goto done;
5968 view->show = show_ref_view;
5969 view->input = input_ref_view;
5970 view->close = close_ref_view;
5971 view->search_start = search_start_ref_view;
5972 view->search_next = search_next_ref_view;
5973 done:
5974 if (err)
5975 free_colors(&s->colors);
5976 return err;
5979 static const struct got_error *
5980 close_ref_view(struct tog_view *view)
5982 struct tog_ref_view_state *s = &view->state.ref;
5984 ref_view_free_refs(s);
5985 free_colors(&s->colors);
5987 return NULL;
5990 static const struct got_error *
5991 resolve_reflist_entry(struct got_object_id **commit_id,
5992 struct tog_reflist_entry *re, struct got_repository *repo)
5994 const struct got_error *err = NULL;
5995 struct got_object_id *obj_id;
5996 struct got_tag_object *tag = NULL;
5997 int obj_type;
5999 *commit_id = NULL;
6001 err = got_ref_resolve(&obj_id, repo, re->ref);
6002 if (err)
6003 return err;
6005 err = got_object_get_type(&obj_type, repo, obj_id);
6006 if (err)
6007 goto done;
6009 switch (obj_type) {
6010 case GOT_OBJ_TYPE_COMMIT:
6011 *commit_id = obj_id;
6012 break;
6013 case GOT_OBJ_TYPE_TAG:
6014 err = got_object_open_as_tag(&tag, repo, obj_id);
6015 if (err)
6016 goto done;
6017 free(obj_id);
6018 err = got_object_get_type(&obj_type, repo,
6019 got_object_tag_get_object_id(tag));
6020 if (err)
6021 goto done;
6022 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6023 err = got_error(GOT_ERR_OBJ_TYPE);
6024 goto done;
6026 *commit_id = got_object_id_dup(
6027 got_object_tag_get_object_id(tag));
6028 if (*commit_id == NULL) {
6029 err = got_error_from_errno("got_object_id_dup");
6030 goto done;
6032 break;
6033 default:
6034 err = got_error(GOT_ERR_OBJ_TYPE);
6035 break;
6038 done:
6039 if (tag)
6040 got_object_tag_close(tag);
6041 if (err) {
6042 free(*commit_id);
6043 *commit_id = NULL;
6045 return err;
6048 static const struct got_error *
6049 log_ref_entry(struct tog_view **new_view, int begin_x,
6050 struct tog_reflist_entry *re, struct got_repository *repo)
6052 struct tog_view *log_view;
6053 const struct got_error *err = NULL;
6054 struct got_object_id *commit_id = NULL;
6056 *new_view = NULL;
6058 err = resolve_reflist_entry(&commit_id, re, repo);
6059 if (err) {
6060 if (err->code != GOT_ERR_OBJ_TYPE)
6061 return err;
6062 else
6063 return NULL;
6066 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6067 if (log_view == NULL) {
6068 err = got_error_from_errno("view_open");
6069 goto done;
6072 err = open_log_view(log_view, commit_id, repo,
6073 got_ref_get_name(re->ref), "", 0);
6074 done:
6075 if (err)
6076 view_close(log_view);
6077 else
6078 *new_view = log_view;
6079 free(commit_id);
6080 return err;
6083 static void
6084 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6086 struct tog_reflist_entry *re;
6087 int i = 0;
6089 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6090 return;
6092 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6093 while (i++ < maxscroll) {
6094 if (re == NULL)
6095 break;
6096 s->first_displayed_entry = re;
6097 re = TAILQ_PREV(re, tog_reflist_head, entry);
6101 static void
6102 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6104 struct tog_reflist_entry *next, *last;
6105 int n = 0;
6107 if (s->first_displayed_entry)
6108 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6109 else
6110 next = TAILQ_FIRST(&s->refs);
6112 last = s->last_displayed_entry;
6113 while (next && last && n++ < maxscroll) {
6114 last = TAILQ_NEXT(last, entry);
6115 if (last) {
6116 s->first_displayed_entry = next;
6117 next = TAILQ_NEXT(next, entry);
6122 static const struct got_error *
6123 search_start_ref_view(struct tog_view *view)
6125 struct tog_ref_view_state *s = &view->state.ref;
6127 s->matched_entry = NULL;
6128 return NULL;
6131 static int
6132 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6134 regmatch_t regmatch;
6136 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6137 0) == 0;
6140 static const struct got_error *
6141 search_next_ref_view(struct tog_view *view)
6143 struct tog_ref_view_state *s = &view->state.ref;
6144 struct tog_reflist_entry *re = NULL;
6146 if (!view->searching) {
6147 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6148 return NULL;
6151 if (s->matched_entry) {
6152 if (view->searching == TOG_SEARCH_FORWARD) {
6153 if (s->selected_entry)
6154 re = TAILQ_NEXT(s->selected_entry, entry);
6155 else
6156 re = TAILQ_PREV(s->selected_entry,
6157 tog_reflist_head, entry);
6158 } else {
6159 if (s->selected_entry == NULL)
6160 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6161 else
6162 re = TAILQ_PREV(s->selected_entry,
6163 tog_reflist_head, entry);
6165 } else {
6166 if (s->selected_entry)
6167 re = s->selected_entry;
6168 else if (view->searching == TOG_SEARCH_FORWARD)
6169 re = TAILQ_FIRST(&s->refs);
6170 else
6171 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6174 while (1) {
6175 if (re == NULL) {
6176 if (s->matched_entry == NULL) {
6177 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6178 return NULL;
6180 if (view->searching == TOG_SEARCH_FORWARD)
6181 re = TAILQ_FIRST(&s->refs);
6182 else
6183 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6186 if (match_reflist_entry(re, &view->regex)) {
6187 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6188 s->matched_entry = re;
6189 break;
6192 if (view->searching == TOG_SEARCH_FORWARD)
6193 re = TAILQ_NEXT(re, entry);
6194 else
6195 re = TAILQ_PREV(re, tog_reflist_head, entry);
6198 if (s->matched_entry) {
6199 s->first_displayed_entry = s->matched_entry;
6200 s->selected = 0;
6203 return NULL;
6206 static const struct got_error *
6207 show_ref_view(struct tog_view *view)
6209 const struct got_error *err = NULL;
6210 struct tog_ref_view_state *s = &view->state.ref;
6211 struct tog_reflist_entry *re;
6212 char *line = NULL;
6213 wchar_t *wline;
6214 struct tog_color *tc;
6215 int width, n;
6216 int limit = view->nlines;
6218 werase(view->window);
6220 s->ndisplayed = 0;
6222 if (limit == 0)
6223 return NULL;
6225 re = s->first_displayed_entry;
6227 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6228 s->nrefs) == -1)
6229 return got_error_from_errno("asprintf");
6231 err = format_line(&wline, &width, line, view->ncols, 0);
6232 if (err) {
6233 free(line);
6234 return err;
6236 if (view_needs_focus_indication(view))
6237 wstandout(view->window);
6238 waddwstr(view->window, wline);
6239 if (view_needs_focus_indication(view))
6240 wstandend(view->window);
6241 free(wline);
6242 wline = NULL;
6243 free(line);
6244 line = NULL;
6245 if (width < view->ncols - 1)
6246 waddch(view->window, '\n');
6247 if (--limit <= 0)
6248 return NULL;
6250 n = 0;
6251 while (re && limit > 0) {
6252 char *line = NULL;
6254 if (got_ref_is_symbolic(re->ref)) {
6255 if (asprintf(&line, "%s -> %s",
6256 got_ref_get_name(re->ref),
6257 got_ref_get_symref_target(re->ref)) == -1)
6258 return got_error_from_errno("asprintf");
6259 } else if (s->show_ids) {
6260 struct got_object_id *id;
6261 char *id_str;
6262 err = got_ref_resolve(&id, s->repo, re->ref);
6263 if (err)
6264 return err;
6265 err = got_object_id_str(&id_str, id);
6266 if (err) {
6267 free(id);
6268 return err;
6270 if (asprintf(&line, "%s: %s",
6271 got_ref_get_name(re->ref), id_str) == -1) {
6272 err = got_error_from_errno("asprintf");
6273 free(id);
6274 free(id_str);
6275 return err;
6277 free(id);
6278 free(id_str);
6279 } else {
6280 line = strdup(got_ref_get_name(re->ref));
6281 if (line == NULL)
6282 return got_error_from_errno("strdup");
6285 err = format_line(&wline, &width, line, view->ncols, 0);
6286 if (err) {
6287 free(line);
6288 return err;
6290 if (n == s->selected) {
6291 if (view->focussed)
6292 wstandout(view->window);
6293 s->selected_entry = re;
6295 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6296 if (tc)
6297 wattr_on(view->window,
6298 COLOR_PAIR(tc->colorpair), NULL);
6299 waddwstr(view->window, wline);
6300 if (tc)
6301 wattr_off(view->window,
6302 COLOR_PAIR(tc->colorpair), NULL);
6303 if (width < view->ncols - 1)
6304 waddch(view->window, '\n');
6305 if (n == s->selected && view->focussed)
6306 wstandend(view->window);
6307 free(line);
6308 free(wline);
6309 wline = NULL;
6310 n++;
6311 s->ndisplayed++;
6312 s->last_displayed_entry = re;
6314 limit--;
6315 re = TAILQ_NEXT(re, entry);
6318 view_vborder(view);
6319 return err;
6322 static const struct got_error *
6323 browse_ref_tree(struct tog_view **new_view, int begin_x,
6324 struct tog_reflist_entry *re, struct got_repository *repo)
6326 const struct got_error *err = NULL;
6327 struct got_object_id *commit_id = NULL;
6328 struct tog_view *tree_view;
6330 *new_view = NULL;
6332 err = resolve_reflist_entry(&commit_id, re, repo);
6333 if (err) {
6334 if (err->code != GOT_ERR_OBJ_TYPE)
6335 return err;
6336 else
6337 return NULL;
6341 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6342 if (tree_view == NULL) {
6343 err = got_error_from_errno("view_open");
6344 goto done;
6347 err = open_tree_view(tree_view, commit_id,
6348 got_ref_get_name(re->ref), repo);
6349 if (err)
6350 goto done;
6352 *new_view = tree_view;
6353 done:
6354 free(commit_id);
6355 return err;
6357 static const struct got_error *
6358 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6360 const struct got_error *err = NULL;
6361 struct tog_ref_view_state *s = &view->state.ref;
6362 struct tog_view *log_view, *tree_view;
6363 struct tog_reflist_entry *re;
6364 int begin_x = 0, n, nscroll = view->nlines - 1;
6366 switch (ch) {
6367 case 'i':
6368 s->show_ids = !s->show_ids;
6369 break;
6370 case 'o':
6371 s->sort_by_date = !s->sort_by_date;
6372 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6373 got_ref_cmp_by_commit_timestamp_descending :
6374 tog_ref_cmp_by_name, s->repo);
6375 if (err)
6376 break;
6377 got_reflist_object_id_map_free(tog_refs_idmap);
6378 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6379 &tog_refs, s->repo);
6380 if (err)
6381 break;
6382 ref_view_free_refs(s);
6383 err = ref_view_load_refs(s);
6384 break;
6385 case KEY_ENTER:
6386 case '\r':
6387 if (!s->selected_entry)
6388 break;
6389 if (view_is_parent_view(view))
6390 begin_x = view_split_begin_x(view->begin_x);
6391 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6392 s->repo);
6393 view->focussed = 0;
6394 log_view->focussed = 1;
6395 if (view_is_parent_view(view)) {
6396 err = view_close_child(view);
6397 if (err)
6398 return err;
6399 view_set_child(view, log_view);
6400 view->focus_child = 1;
6401 } else
6402 *new_view = log_view;
6403 break;
6404 case 't':
6405 if (!s->selected_entry)
6406 break;
6407 if (view_is_parent_view(view))
6408 begin_x = view_split_begin_x(view->begin_x);
6409 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6410 s->repo);
6411 if (err || tree_view == NULL)
6412 break;
6413 view->focussed = 0;
6414 tree_view->focussed = 1;
6415 if (view_is_parent_view(view)) {
6416 err = view_close_child(view);
6417 if (err)
6418 return err;
6419 view_set_child(view, tree_view);
6420 view->focus_child = 1;
6421 } else
6422 *new_view = tree_view;
6423 break;
6424 case 'g':
6425 case KEY_HOME:
6426 s->selected = 0;
6427 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6428 break;
6429 case 'G':
6430 case KEY_END:
6431 s->selected = 0;
6432 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6433 for (n = 0; n < view->nlines - 1; n++) {
6434 if (re == NULL)
6435 break;
6436 s->first_displayed_entry = re;
6437 re = TAILQ_PREV(re, tog_reflist_head, entry);
6439 if (n > 0)
6440 s->selected = n - 1;
6441 break;
6442 case 'k':
6443 case KEY_UP:
6444 case CTRL('p'):
6445 if (s->selected > 0) {
6446 s->selected--;
6447 break;
6449 ref_scroll_up(s, 1);
6450 break;
6451 case CTRL('u'):
6452 case 'u':
6453 nscroll /= 2;
6454 /* FALL THROUGH */
6455 case KEY_PPAGE:
6456 case CTRL('b'):
6457 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6458 s->selected -= MIN(nscroll, s->selected);
6459 ref_scroll_up(s, MAX(0, nscroll));
6460 break;
6461 case 'j':
6462 case KEY_DOWN:
6463 case CTRL('n'):
6464 if (s->selected < s->ndisplayed - 1) {
6465 s->selected++;
6466 break;
6468 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6469 /* can't scroll any further */
6470 break;
6471 ref_scroll_down(s, 1);
6472 break;
6473 case CTRL('d'):
6474 case 'd':
6475 nscroll /= 2;
6476 /* FALL THROUGH */
6477 case KEY_NPAGE:
6478 case CTRL('f'):
6479 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6480 /* can't scroll any further; move cursor down */
6481 if (s->selected < s->ndisplayed - 1)
6482 s->selected += MIN(nscroll,
6483 s->ndisplayed - s->selected - 1);
6484 break;
6486 ref_scroll_down(s, nscroll);
6487 break;
6488 case CTRL('l'):
6489 tog_free_refs();
6490 err = tog_load_refs(s->repo, s->sort_by_date);
6491 if (err)
6492 break;
6493 ref_view_free_refs(s);
6494 err = ref_view_load_refs(s);
6495 break;
6496 case KEY_RESIZE:
6497 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6498 s->selected = view->nlines - 2;
6499 break;
6500 default:
6501 break;
6504 return err;
6507 __dead static void
6508 usage_ref(void)
6510 endwin();
6511 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6512 getprogname());
6513 exit(1);
6516 static const struct got_error *
6517 cmd_ref(int argc, char *argv[])
6519 const struct got_error *error;
6520 struct got_repository *repo = NULL;
6521 struct got_worktree *worktree = NULL;
6522 char *cwd = NULL, *repo_path = NULL;
6523 int ch;
6524 struct tog_view *view;
6526 while ((ch = getopt(argc, argv, "r:")) != -1) {
6527 switch (ch) {
6528 case 'r':
6529 repo_path = realpath(optarg, NULL);
6530 if (repo_path == NULL)
6531 return got_error_from_errno2("realpath",
6532 optarg);
6533 break;
6534 default:
6535 usage_ref();
6536 /* NOTREACHED */
6540 argc -= optind;
6541 argv += optind;
6543 if (argc > 1)
6544 usage_ref();
6546 if (repo_path == NULL) {
6547 cwd = getcwd(NULL, 0);
6548 if (cwd == NULL)
6549 return got_error_from_errno("getcwd");
6550 error = got_worktree_open(&worktree, cwd);
6551 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6552 goto done;
6553 if (worktree)
6554 repo_path =
6555 strdup(got_worktree_get_repo_path(worktree));
6556 else
6557 repo_path = strdup(cwd);
6558 if (repo_path == NULL) {
6559 error = got_error_from_errno("strdup");
6560 goto done;
6564 error = got_repo_open(&repo, repo_path, NULL);
6565 if (error != NULL)
6566 goto done;
6568 init_curses();
6570 error = apply_unveil(got_repo_get_path(repo), NULL);
6571 if (error)
6572 goto done;
6574 error = tog_load_refs(repo, 0);
6575 if (error)
6576 goto done;
6578 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6579 if (view == NULL) {
6580 error = got_error_from_errno("view_open");
6581 goto done;
6584 error = open_ref_view(view, repo);
6585 if (error)
6586 goto done;
6588 if (worktree) {
6589 /* Release work tree lock. */
6590 got_worktree_close(worktree);
6591 worktree = NULL;
6593 error = view_loop(view);
6594 done:
6595 free(repo_path);
6596 free(cwd);
6597 if (repo) {
6598 const struct got_error *close_err = got_repo_close(repo);
6599 if (close_err)
6600 error = close_err;
6602 tog_free_refs();
6603 return error;
6606 static void
6607 list_commands(FILE *fp)
6609 size_t i;
6611 fprintf(fp, "commands:");
6612 for (i = 0; i < nitems(tog_commands); i++) {
6613 const struct tog_cmd *cmd = &tog_commands[i];
6614 fprintf(fp, " %s", cmd->name);
6616 fputc('\n', fp);
6619 __dead static void
6620 usage(int hflag, int status)
6622 FILE *fp = (status == 0) ? stdout : stderr;
6624 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6625 getprogname());
6626 if (hflag) {
6627 fprintf(fp, "lazy usage: %s path\n", getprogname());
6628 list_commands(fp);
6630 exit(status);
6633 static char **
6634 make_argv(int argc, ...)
6636 va_list ap;
6637 char **argv;
6638 int i;
6640 va_start(ap, argc);
6642 argv = calloc(argc, sizeof(char *));
6643 if (argv == NULL)
6644 err(1, "calloc");
6645 for (i = 0; i < argc; i++) {
6646 argv[i] = strdup(va_arg(ap, char *));
6647 if (argv[i] == NULL)
6648 err(1, "strdup");
6651 va_end(ap);
6652 return argv;
6656 * Try to convert 'tog path' into a 'tog log path' command.
6657 * The user could simply have mistyped the command rather than knowingly
6658 * provided a path. So check whether argv[0] can in fact be resolved
6659 * to a path in the HEAD commit and print a special error if not.
6660 * This hack is for mpi@ <3
6662 static const struct got_error *
6663 tog_log_with_path(int argc, char *argv[])
6665 const struct got_error *error = NULL, *close_err;
6666 const struct tog_cmd *cmd = NULL;
6667 struct got_repository *repo = NULL;
6668 struct got_worktree *worktree = NULL;
6669 struct got_object_id *commit_id = NULL, *id = NULL;
6670 struct got_commit_object *commit = NULL;
6671 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6672 char *commit_id_str = NULL, **cmd_argv = NULL;
6674 cwd = getcwd(NULL, 0);
6675 if (cwd == NULL)
6676 return got_error_from_errno("getcwd");
6678 error = got_worktree_open(&worktree, cwd);
6679 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6680 goto done;
6682 if (worktree)
6683 repo_path = strdup(got_worktree_get_repo_path(worktree));
6684 else
6685 repo_path = strdup(cwd);
6686 if (repo_path == NULL) {
6687 error = got_error_from_errno("strdup");
6688 goto done;
6691 error = got_repo_open(&repo, repo_path, NULL);
6692 if (error != NULL)
6693 goto done;
6695 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6696 repo, worktree);
6697 if (error)
6698 goto done;
6700 error = tog_load_refs(repo, 0);
6701 if (error)
6702 goto done;
6703 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6704 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6705 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6706 if (error)
6707 goto done;
6709 if (worktree) {
6710 got_worktree_close(worktree);
6711 worktree = NULL;
6714 error = got_object_open_as_commit(&commit, repo, commit_id);
6715 if (error)
6716 goto done;
6718 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6719 if (error) {
6720 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6721 goto done;
6722 fprintf(stderr, "%s: '%s' is no known command or path\n",
6723 getprogname(), argv[0]);
6724 usage(1, 1);
6725 /* not reached */
6728 close_err = got_repo_close(repo);
6729 if (error == NULL)
6730 error = close_err;
6731 repo = NULL;
6733 error = got_object_id_str(&commit_id_str, commit_id);
6734 if (error)
6735 goto done;
6737 cmd = &tog_commands[0]; /* log */
6738 argc = 4;
6739 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6740 error = cmd->cmd_main(argc, cmd_argv);
6741 done:
6742 if (repo) {
6743 close_err = got_repo_close(repo);
6744 if (error == NULL)
6745 error = close_err;
6747 if (commit)
6748 got_object_commit_close(commit);
6749 if (worktree)
6750 got_worktree_close(worktree);
6751 free(id);
6752 free(commit_id_str);
6753 free(commit_id);
6754 free(cwd);
6755 free(repo_path);
6756 free(in_repo_path);
6757 if (cmd_argv) {
6758 int i;
6759 for (i = 0; i < argc; i++)
6760 free(cmd_argv[i]);
6761 free(cmd_argv);
6763 tog_free_refs();
6764 return error;
6767 int
6768 main(int argc, char *argv[])
6770 const struct got_error *error = NULL;
6771 const struct tog_cmd *cmd = NULL;
6772 int ch, hflag = 0, Vflag = 0;
6773 char **cmd_argv = NULL;
6774 static const struct option longopts[] = {
6775 { "version", no_argument, NULL, 'V' },
6776 { NULL, 0, NULL, 0}
6779 setlocale(LC_CTYPE, "");
6781 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6782 switch (ch) {
6783 case 'h':
6784 hflag = 1;
6785 break;
6786 case 'V':
6787 Vflag = 1;
6788 break;
6789 default:
6790 usage(hflag, 1);
6791 /* NOTREACHED */
6795 argc -= optind;
6796 argv += optind;
6797 optind = 1;
6798 optreset = 1;
6800 if (Vflag) {
6801 got_version_print_str();
6802 return 0;
6805 #ifndef PROFILE
6806 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6807 NULL) == -1)
6808 err(1, "pledge");
6809 #endif
6811 if (argc == 0) {
6812 if (hflag)
6813 usage(hflag, 0);
6814 /* Build an argument vector which runs a default command. */
6815 cmd = &tog_commands[0];
6816 argc = 1;
6817 cmd_argv = make_argv(argc, cmd->name);
6818 } else {
6819 size_t i;
6821 /* Did the user specify a command? */
6822 for (i = 0; i < nitems(tog_commands); i++) {
6823 if (strncmp(tog_commands[i].name, argv[0],
6824 strlen(argv[0])) == 0) {
6825 cmd = &tog_commands[i];
6826 break;
6831 if (cmd == NULL) {
6832 if (argc != 1)
6833 usage(0, 1);
6834 /* No command specified; try log with a path */
6835 error = tog_log_with_path(argc, argv);
6836 } else {
6837 if (hflag)
6838 cmd->cmd_usage();
6839 else
6840 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6843 endwin();
6844 putchar('\n');
6845 if (cmd_argv) {
6846 int i;
6847 for (i = 0; i < argc; i++)
6848 free(cmd_argv[i]);
6849 free(cmd_argv);
6852 if (error && error->code != GOT_ERR_CANCELLED)
6853 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6854 return 0;