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 static 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 *pack_fds;
349 int log_complete;
350 sig_atomic_t *quit;
351 struct commit_queue_entry **first_displayed_entry;
352 struct commit_queue_entry **selected_entry;
353 int *searching;
354 int *search_next_done;
355 regex_t *regex;
356 };
358 struct tog_log_view_state {
359 struct commit_queue commits;
360 struct commit_queue_entry *first_displayed_entry;
361 struct commit_queue_entry *last_displayed_entry;
362 struct commit_queue_entry *selected_entry;
363 int selected;
364 char *in_repo_path;
365 char *head_ref_name;
366 int log_branches;
367 struct got_repository *repo;
368 struct got_object_id *start_id;
369 sig_atomic_t quit;
370 pthread_t thread;
371 struct tog_log_thread_args thread_args;
372 struct commit_queue_entry *matched_entry;
373 struct commit_queue_entry *search_entry;
374 struct tog_colors colors;
375 };
377 #define TOG_COLOR_DIFF_MINUS 1
378 #define TOG_COLOR_DIFF_PLUS 2
379 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
380 #define TOG_COLOR_DIFF_META 4
381 #define TOG_COLOR_TREE_SUBMODULE 5
382 #define TOG_COLOR_TREE_SYMLINK 6
383 #define TOG_COLOR_TREE_DIRECTORY 7
384 #define TOG_COLOR_TREE_EXECUTABLE 8
385 #define TOG_COLOR_COMMIT 9
386 #define TOG_COLOR_AUTHOR 10
387 #define TOG_COLOR_DATE 11
388 #define TOG_COLOR_REFS_HEADS 12
389 #define TOG_COLOR_REFS_TAGS 13
390 #define TOG_COLOR_REFS_REMOTES 14
391 #define TOG_COLOR_REFS_BACKUP 15
393 struct tog_blame_cb_args {
394 struct tog_blame_line *lines; /* one per line */
395 int nlines;
397 struct tog_view *view;
398 struct got_object_id *commit_id;
399 int *quit;
400 };
402 struct tog_blame_thread_args {
403 const char *path;
404 struct got_repository *repo;
405 struct tog_blame_cb_args *cb_args;
406 int *complete;
407 got_cancel_cb cancel_cb;
408 void *cancel_arg;
409 };
411 struct tog_blame {
412 FILE *f;
413 off_t filesize;
414 struct tog_blame_line *lines;
415 int nlines;
416 off_t *line_offsets;
417 pthread_t thread;
418 struct tog_blame_thread_args thread_args;
419 struct tog_blame_cb_args cb_args;
420 const char *path;
421 int *pack_fds;
422 };
424 struct tog_blame_view_state {
425 int first_displayed_line;
426 int last_displayed_line;
427 int selected_line;
428 int blame_complete;
429 int eof;
430 int done;
431 struct got_object_id_queue blamed_commits;
432 struct got_object_qid *blamed_commit;
433 char *path;
434 struct got_repository *repo;
435 struct got_object_id *commit_id;
436 struct tog_blame blame;
437 int matched_line;
438 struct tog_colors colors;
439 };
441 struct tog_parent_tree {
442 TAILQ_ENTRY(tog_parent_tree) entry;
443 struct got_tree_object *tree;
444 struct got_tree_entry *first_displayed_entry;
445 struct got_tree_entry *selected_entry;
446 int selected;
447 };
449 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
451 struct tog_tree_view_state {
452 char *tree_label;
453 struct got_object_id *commit_id;/* commit which this tree belongs to */
454 struct got_tree_object *root; /* the commit's root tree entry */
455 struct got_tree_object *tree; /* currently displayed (sub-)tree */
456 struct got_tree_entry *first_displayed_entry;
457 struct got_tree_entry *last_displayed_entry;
458 struct got_tree_entry *selected_entry;
459 int ndisplayed, selected, show_ids;
460 struct tog_parent_trees parents; /* parent trees of current sub-tree */
461 char *head_ref_name;
462 struct got_repository *repo;
463 struct got_tree_entry *matched_entry;
464 struct tog_colors colors;
465 };
467 struct tog_reflist_entry {
468 TAILQ_ENTRY(tog_reflist_entry) entry;
469 struct got_reference *ref;
470 int idx;
471 };
473 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
475 struct tog_ref_view_state {
476 struct tog_reflist_head refs;
477 struct tog_reflist_entry *first_displayed_entry;
478 struct tog_reflist_entry *last_displayed_entry;
479 struct tog_reflist_entry *selected_entry;
480 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
481 struct got_repository *repo;
482 struct tog_reflist_entry *matched_entry;
483 struct tog_colors colors;
484 };
486 /*
487 * We implement two types of views: parent views and child views.
489 * The 'Tab' key switches focus between a parent view and its child view.
490 * Child views are shown side-by-side to their parent view, provided
491 * there is enough screen estate.
493 * When a new view is opened from within a parent view, this new view
494 * becomes a child view of the parent view, replacing any existing child.
496 * When a new view is opened from within a child view, this new view
497 * becomes a parent view which will obscure the views below until the
498 * user quits the new parent view by typing 'q'.
500 * This list of views contains parent views only.
501 * Child views are only pointed to by their parent view.
502 */
503 TAILQ_HEAD(tog_view_list_head, tog_view);
505 struct tog_view {
506 TAILQ_ENTRY(tog_view) entry;
507 WINDOW *window;
508 PANEL *panel;
509 int nlines, ncols, begin_y, begin_x;
510 int maxx, x; /* max column and current start column */
511 int lines, cols; /* copies of LINES and COLS */
512 int ch, count; /* current keymap and count prefix */
513 int focussed; /* Only set on one parent or child view at a time. */
514 int dying;
515 struct tog_view *parent;
516 struct tog_view *child;
518 /*
519 * This flag is initially set on parent views when a new child view
520 * is created. It gets toggled when the 'Tab' key switches focus
521 * between parent and child.
522 * The flag indicates whether focus should be passed on to our child
523 * view if this parent view gets picked for focus after another parent
524 * view was closed. This prevents child views from losing focus in such
525 * situations.
526 */
527 int focus_child;
529 /* type-specific state */
530 enum tog_view_type type;
531 union {
532 struct tog_diff_view_state diff;
533 struct tog_log_view_state log;
534 struct tog_blame_view_state blame;
535 struct tog_tree_view_state tree;
536 struct tog_ref_view_state ref;
537 } state;
539 const struct got_error *(*show)(struct tog_view *);
540 const struct got_error *(*input)(struct tog_view **,
541 struct tog_view *, int);
542 const struct got_error *(*close)(struct tog_view *);
544 const struct got_error *(*search_start)(struct tog_view *);
545 const struct got_error *(*search_next)(struct tog_view *);
546 int search_started;
547 int searching;
548 #define TOG_SEARCH_FORWARD 1
549 #define TOG_SEARCH_BACKWARD 2
550 int search_next_done;
551 #define TOG_SEARCH_HAVE_MORE 1
552 #define TOG_SEARCH_NO_MORE 2
553 #define TOG_SEARCH_HAVE_NONE 3
554 regex_t regex;
555 regmatch_t regmatch;
556 };
558 static const struct got_error *open_diff_view(struct tog_view *,
559 struct got_object_id *, struct got_object_id *,
560 const char *, const char *, int, int, int, struct tog_view *,
561 struct got_repository *);
562 static const struct got_error *show_diff_view(struct tog_view *);
563 static const struct got_error *input_diff_view(struct tog_view **,
564 struct tog_view *, int);
565 static const struct got_error* close_diff_view(struct tog_view *);
566 static const struct got_error *search_start_diff_view(struct tog_view *);
567 static const struct got_error *search_next_diff_view(struct tog_view *);
569 static const struct got_error *open_log_view(struct tog_view *,
570 struct got_object_id *, struct got_repository *,
571 const char *, const char *, int);
572 static const struct got_error * show_log_view(struct tog_view *);
573 static const struct got_error *input_log_view(struct tog_view **,
574 struct tog_view *, int);
575 static const struct got_error *close_log_view(struct tog_view *);
576 static const struct got_error *search_start_log_view(struct tog_view *);
577 static const struct got_error *search_next_log_view(struct tog_view *);
579 static const struct got_error *open_blame_view(struct tog_view *, char *,
580 struct got_object_id *, struct got_repository *);
581 static const struct got_error *show_blame_view(struct tog_view *);
582 static const struct got_error *input_blame_view(struct tog_view **,
583 struct tog_view *, int);
584 static const struct got_error *close_blame_view(struct tog_view *);
585 static const struct got_error *search_start_blame_view(struct tog_view *);
586 static const struct got_error *search_next_blame_view(struct tog_view *);
588 static const struct got_error *open_tree_view(struct tog_view *,
589 struct got_object_id *, const char *, struct got_repository *);
590 static const struct got_error *show_tree_view(struct tog_view *);
591 static const struct got_error *input_tree_view(struct tog_view **,
592 struct tog_view *, int);
593 static const struct got_error *close_tree_view(struct tog_view *);
594 static const struct got_error *search_start_tree_view(struct tog_view *);
595 static const struct got_error *search_next_tree_view(struct tog_view *);
597 static const struct got_error *open_ref_view(struct tog_view *,
598 struct got_repository *);
599 static const struct got_error *show_ref_view(struct tog_view *);
600 static const struct got_error *input_ref_view(struct tog_view **,
601 struct tog_view *, int);
602 static const struct got_error *close_ref_view(struct tog_view *);
603 static const struct got_error *search_start_ref_view(struct tog_view *);
604 static const struct got_error *search_next_ref_view(struct tog_view *);
606 static volatile sig_atomic_t tog_sigwinch_received;
607 static volatile sig_atomic_t tog_sigpipe_received;
608 static volatile sig_atomic_t tog_sigcont_received;
609 static volatile sig_atomic_t tog_sigint_received;
610 static volatile sig_atomic_t tog_sigterm_received;
612 static void
613 tog_sigwinch(int signo)
615 tog_sigwinch_received = 1;
618 static void
619 tog_sigpipe(int signo)
621 tog_sigpipe_received = 1;
624 static void
625 tog_sigcont(int signo)
627 tog_sigcont_received = 1;
630 static void
631 tog_sigint(int signo)
633 tog_sigint_received = 1;
636 static void
637 tog_sigterm(int signo)
639 tog_sigterm_received = 1;
642 static int
643 tog_fatal_signal_received(void)
645 return (tog_sigpipe_received ||
646 tog_sigint_received || tog_sigint_received);
650 static const struct got_error *
651 view_close(struct tog_view *view)
653 const struct got_error *err = NULL;
655 if (view->child) {
656 view_close(view->child);
657 view->child = NULL;
659 if (view->close)
660 err = view->close(view);
661 if (view->panel)
662 del_panel(view->panel);
663 if (view->window)
664 delwin(view->window);
665 free(view);
666 return err;
669 static struct tog_view *
670 view_open(int nlines, int ncols, int begin_y, int begin_x,
671 enum tog_view_type type)
673 struct tog_view *view = calloc(1, sizeof(*view));
675 if (view == NULL)
676 return NULL;
678 view->ch = 0;
679 view->count = 0;
680 view->type = type;
681 view->lines = LINES;
682 view->cols = COLS;
683 view->nlines = nlines ? nlines : LINES - begin_y;
684 view->ncols = ncols ? ncols : COLS - begin_x;
685 view->begin_y = begin_y;
686 view->begin_x = begin_x;
687 view->window = newwin(nlines, ncols, begin_y, begin_x);
688 if (view->window == NULL) {
689 view_close(view);
690 return NULL;
692 view->panel = new_panel(view->window);
693 if (view->panel == NULL ||
694 set_panel_userptr(view->panel, view) != OK) {
695 view_close(view);
696 return NULL;
699 keypad(view->window, TRUE);
700 return view;
703 static int
704 view_split_begin_x(int begin_x)
706 if (begin_x > 0 || COLS < 120)
707 return 0;
708 return (COLS - MAX(COLS / 2, 80));
711 static const struct got_error *view_resize(struct tog_view *);
713 static const struct got_error *
714 view_splitscreen(struct tog_view *view)
716 const struct got_error *err = NULL;
718 view->begin_y = 0;
719 view->begin_x = view_split_begin_x(0);
720 view->nlines = LINES;
721 view->ncols = COLS - view->begin_x;
722 view->lines = LINES;
723 view->cols = COLS;
724 err = view_resize(view);
725 if (err)
726 return err;
728 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
729 return got_error_from_errno("mvwin");
731 return NULL;
734 static const struct got_error *
735 view_fullscreen(struct tog_view *view)
737 const struct got_error *err = NULL;
739 view->begin_x = 0;
740 view->begin_y = 0;
741 view->nlines = LINES;
742 view->ncols = COLS;
743 view->lines = LINES;
744 view->cols = COLS;
745 err = view_resize(view);
746 if (err)
747 return err;
749 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
750 return got_error_from_errno("mvwin");
752 return NULL;
755 static int
756 view_is_parent_view(struct tog_view *view)
758 return view->parent == NULL;
761 static int
762 view_is_splitscreen(struct tog_view *view)
764 return view->begin_x > 0;
768 static const struct got_error *
769 view_resize(struct tog_view *view)
771 int nlines, ncols;
773 if (view->lines > LINES)
774 nlines = view->nlines - (view->lines - LINES);
775 else
776 nlines = view->nlines + (LINES - view->lines);
778 if (view->cols > COLS)
779 ncols = view->ncols - (view->cols - COLS);
780 else
781 ncols = view->ncols + (COLS - view->cols);
783 if (view->child && view_is_splitscreen(view->child)) {
784 view->child->begin_x = view_split_begin_x(view->begin_x);
785 if (view->child->begin_x == 0) {
786 ncols = COLS;
788 view_fullscreen(view->child);
789 if (view->child->focussed)
790 show_panel(view->child->panel);
791 else
792 show_panel(view->panel);
793 } else {
794 ncols = view->child->begin_x;
796 view_splitscreen(view->child);
797 show_panel(view->child->panel);
799 } else if (view->parent == NULL)
800 ncols = COLS;
802 if (wresize(view->window, nlines, ncols) == ERR)
803 return got_error_from_errno("wresize");
804 if (replace_panel(view->panel, view->window) == ERR)
805 return got_error_from_errno("replace_panel");
806 wclear(view->window);
808 view->nlines = nlines;
809 view->ncols = ncols;
810 view->lines = LINES;
811 view->cols = COLS;
813 return NULL;
816 static const struct got_error *
817 view_close_child(struct tog_view *view)
819 const struct got_error *err = NULL;
821 if (view->child == NULL)
822 return NULL;
824 err = view_close(view->child);
825 view->child = NULL;
826 return err;
829 static const struct got_error *
830 view_set_child(struct tog_view *view, struct tog_view *child)
832 view->child = child;
833 child->parent = view;
835 return view_resize(view);
838 static void
839 tog_resizeterm(void)
841 int cols, lines;
842 struct winsize size;
844 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
845 cols = 80; /* Default */
846 lines = 24;
847 } else {
848 cols = size.ws_col;
849 lines = size.ws_row;
851 resize_term(lines, cols);
854 static const struct got_error *
855 view_search_start(struct tog_view *view)
857 const struct got_error *err = NULL;
858 char pattern[1024];
859 int ret;
861 if (view->search_started) {
862 regfree(&view->regex);
863 view->searching = 0;
864 memset(&view->regmatch, 0, sizeof(view->regmatch));
866 view->search_started = 0;
868 if (view->nlines < 1)
869 return NULL;
871 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
872 wclrtoeol(view->window);
874 nocbreak();
875 echo();
876 ret = wgetnstr(view->window, pattern, sizeof(pattern));
877 cbreak();
878 noecho();
879 if (ret == ERR)
880 return NULL;
882 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
883 err = view->search_start(view);
884 if (err) {
885 regfree(&view->regex);
886 return err;
888 view->search_started = 1;
889 view->searching = TOG_SEARCH_FORWARD;
890 view->search_next_done = 0;
891 view->search_next(view);
894 return NULL;
897 /*
898 * Compute view->count from numeric user input. User has five-tenths of a
899 * second to follow each numeric keypress with another number to form count.
900 * Return first non-numeric input or ERR and assign total to view->count.
901 * XXX Should we add support for user-defined timeout?
902 */
903 static int
904 get_compound_key(struct tog_view *view, int c)
906 int x, n = 0;
908 view->count = 0;
909 halfdelay(5); /* block for half a second */
910 wattron(view->window, A_BOLD);
911 wmove(view->window, view->nlines - 1, 0);
912 wclrtoeol(view->window);
913 waddch(view->window, ':');
915 do {
916 x = getcurx(view->window);
917 if (x != ERR && x < view->ncols)
918 waddch(view->window, c);
919 /*
920 * Don't overflow. Max valid request should be the greatest
921 * between the longest and total lines; cap at 10 million.
922 */
923 if (n >= 9999999)
924 n = 9999999;
925 else
926 n = n * 10 + (c - '0');
927 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
929 /* Massage excessive or inapplicable values at the input handler. */
930 view->count = n;
932 wattroff(view->window, A_BOLD);
933 cbreak(); /* return to blocking */
934 return c;
937 static const struct got_error *
938 view_input(struct tog_view **new, int *done, struct tog_view *view,
939 struct tog_view_list_head *views)
941 const struct got_error *err = NULL;
942 struct tog_view *v;
943 int ch, errcode;
945 *new = NULL;
947 /* Clear "no matches" indicator. */
948 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
949 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
950 view->search_next_done = TOG_SEARCH_HAVE_MORE;
951 view->count = 0;
954 if (view->searching && !view->search_next_done) {
955 errcode = pthread_mutex_unlock(&tog_mutex);
956 if (errcode)
957 return got_error_set_errno(errcode,
958 "pthread_mutex_unlock");
959 sched_yield();
960 errcode = pthread_mutex_lock(&tog_mutex);
961 if (errcode)
962 return got_error_set_errno(errcode,
963 "pthread_mutex_lock");
964 view->search_next(view);
965 return NULL;
968 nodelay(stdscr, FALSE);
969 /* Allow threads to make progress while we are waiting for input. */
970 errcode = pthread_mutex_unlock(&tog_mutex);
971 if (errcode)
972 return got_error_set_errno(errcode, "pthread_mutex_unlock");
973 /* If we have an unfinished count, don't get a new key map. */
974 ch = view->ch;
975 if ((view->count && --view->count == 0) || !view->count) {
976 ch = wgetch(view->window);
977 if (ch >= '1' && ch <= '9')
978 view->ch = ch = get_compound_key(view, ch);
980 errcode = pthread_mutex_lock(&tog_mutex);
981 if (errcode)
982 return got_error_set_errno(errcode, "pthread_mutex_lock");
983 nodelay(stdscr, TRUE);
985 if (tog_sigwinch_received || tog_sigcont_received) {
986 tog_resizeterm();
987 tog_sigwinch_received = 0;
988 tog_sigcont_received = 0;
989 TAILQ_FOREACH(v, views, entry) {
990 err = view_resize(v);
991 if (err)
992 return err;
993 err = v->input(new, v, KEY_RESIZE);
994 if (err)
995 return err;
996 if (v->child) {
997 err = view_resize(v->child);
998 if (err)
999 return err;
1000 err = v->child->input(new, v->child,
1001 KEY_RESIZE);
1002 if (err)
1003 return err;
1008 switch (ch) {
1009 case '\t':
1010 view->count = 0;
1011 if (view->child) {
1012 view->focussed = 0;
1013 view->child->focussed = 1;
1014 view->focus_child = 1;
1015 } else if (view->parent) {
1016 view->focussed = 0;
1017 view->parent->focussed = 1;
1018 view->parent->focus_child = 0;
1019 if (!view_is_splitscreen(view))
1020 err = view_fullscreen(view->parent);
1022 break;
1023 case 'q':
1024 err = view->input(new, view, ch);
1025 view->dying = 1;
1026 break;
1027 case 'Q':
1028 *done = 1;
1029 break;
1030 case 'F':
1031 view->count = 0;
1032 if (view_is_parent_view(view)) {
1033 if (view->child == NULL)
1034 break;
1035 if (view_is_splitscreen(view->child)) {
1036 view->focussed = 0;
1037 view->child->focussed = 1;
1038 err = view_fullscreen(view->child);
1039 } else
1040 err = view_splitscreen(view->child);
1041 if (err)
1042 break;
1043 err = view->child->input(new, view->child,
1044 KEY_RESIZE);
1045 } else {
1046 if (view_is_splitscreen(view)) {
1047 view->parent->focussed = 0;
1048 view->focussed = 1;
1049 err = view_fullscreen(view);
1050 } else {
1051 err = view_splitscreen(view);
1052 if (!err)
1053 err = view_resize(view->parent);
1055 if (err)
1056 break;
1057 err = view->input(new, view, KEY_RESIZE);
1059 break;
1060 case KEY_RESIZE:
1061 break;
1062 case '/':
1063 view->count = 0;
1064 if (view->search_start)
1065 view_search_start(view);
1066 else
1067 err = view->input(new, view, ch);
1068 break;
1069 case 'N':
1070 case 'n':
1071 if (view->search_started && view->search_next) {
1072 view->searching = (ch == 'n' ?
1073 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1074 view->search_next_done = 0;
1075 view->search_next(view);
1076 } else
1077 err = view->input(new, view, ch);
1078 break;
1079 default:
1080 err = view->input(new, view, ch);
1081 break;
1084 return err;
1087 static void
1088 view_vborder(struct tog_view *view)
1090 PANEL *panel;
1091 const struct tog_view *view_above;
1093 if (view->parent)
1094 return view_vborder(view->parent);
1096 panel = panel_above(view->panel);
1097 if (panel == NULL)
1098 return;
1100 view_above = panel_userptr(panel);
1101 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1102 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1105 static int
1106 view_needs_focus_indication(struct tog_view *view)
1108 if (view_is_parent_view(view)) {
1109 if (view->child == NULL || view->child->focussed)
1110 return 0;
1111 if (!view_is_splitscreen(view->child))
1112 return 0;
1113 } else if (!view_is_splitscreen(view))
1114 return 0;
1116 return view->focussed;
1119 static const struct got_error *
1120 view_loop(struct tog_view *view)
1122 const struct got_error *err = NULL;
1123 struct tog_view_list_head views;
1124 struct tog_view *new_view;
1125 int fast_refresh = 10;
1126 int done = 0, errcode;
1128 errcode = pthread_mutex_lock(&tog_mutex);
1129 if (errcode)
1130 return got_error_set_errno(errcode, "pthread_mutex_lock");
1132 TAILQ_INIT(&views);
1133 TAILQ_INSERT_HEAD(&views, view, entry);
1135 view->focussed = 1;
1136 err = view->show(view);
1137 if (err)
1138 return err;
1139 update_panels();
1140 doupdate();
1141 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1142 /* Refresh fast during initialization, then become slower. */
1143 if (fast_refresh && fast_refresh-- == 0)
1144 halfdelay(10); /* switch to once per second */
1146 err = view_input(&new_view, &done, view, &views);
1147 if (err)
1148 break;
1149 if (view->dying) {
1150 struct tog_view *v, *prev = NULL;
1152 if (view_is_parent_view(view))
1153 prev = TAILQ_PREV(view, tog_view_list_head,
1154 entry);
1155 else if (view->parent)
1156 prev = view->parent;
1158 if (view->parent) {
1159 view->parent->child = NULL;
1160 view->parent->focus_child = 0;
1162 err = view_resize(view->parent);
1163 if (err)
1164 break;
1165 } else
1166 TAILQ_REMOVE(&views, view, entry);
1168 err = view_close(view);
1169 if (err)
1170 goto done;
1172 view = NULL;
1173 TAILQ_FOREACH(v, &views, entry) {
1174 if (v->focussed)
1175 break;
1177 if (view == NULL && new_view == NULL) {
1178 /* No view has focus. Try to pick one. */
1179 if (prev)
1180 view = prev;
1181 else if (!TAILQ_EMPTY(&views)) {
1182 view = TAILQ_LAST(&views,
1183 tog_view_list_head);
1185 if (view) {
1186 if (view->focus_child) {
1187 view->child->focussed = 1;
1188 view = view->child;
1189 } else
1190 view->focussed = 1;
1194 if (new_view) {
1195 struct tog_view *v, *t;
1196 /* Only allow one parent view per type. */
1197 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1198 if (v->type != new_view->type)
1199 continue;
1200 TAILQ_REMOVE(&views, v, entry);
1201 err = view_close(v);
1202 if (err)
1203 goto done;
1204 break;
1206 TAILQ_INSERT_TAIL(&views, new_view, entry);
1207 view = new_view;
1209 if (view) {
1210 if (view_is_parent_view(view)) {
1211 if (view->child && view->child->focussed)
1212 view = view->child;
1213 } else {
1214 if (view->parent && view->parent->focussed)
1215 view = view->parent;
1217 show_panel(view->panel);
1218 if (view->child && view_is_splitscreen(view->child))
1219 show_panel(view->child->panel);
1220 if (view->parent && view_is_splitscreen(view)) {
1221 err = view->parent->show(view->parent);
1222 if (err)
1223 goto done;
1225 err = view->show(view);
1226 if (err)
1227 goto done;
1228 if (view->child) {
1229 err = view->child->show(view->child);
1230 if (err)
1231 goto done;
1233 update_panels();
1234 doupdate();
1237 done:
1238 while (!TAILQ_EMPTY(&views)) {
1239 view = TAILQ_FIRST(&views);
1240 TAILQ_REMOVE(&views, view, entry);
1241 view_close(view);
1244 errcode = pthread_mutex_unlock(&tog_mutex);
1245 if (errcode && err == NULL)
1246 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1248 return err;
1251 __dead static void
1252 usage_log(void)
1254 endwin();
1255 fprintf(stderr,
1256 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1257 getprogname());
1258 exit(1);
1261 /* Create newly allocated wide-character string equivalent to a byte string. */
1262 static const struct got_error *
1263 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1265 char *vis = NULL;
1266 const struct got_error *err = NULL;
1268 *ws = NULL;
1269 *wlen = mbstowcs(NULL, s, 0);
1270 if (*wlen == (size_t)-1) {
1271 int vislen;
1272 if (errno != EILSEQ)
1273 return got_error_from_errno("mbstowcs");
1275 /* byte string invalid in current encoding; try to "fix" it */
1276 err = got_mbsavis(&vis, &vislen, s);
1277 if (err)
1278 return err;
1279 *wlen = mbstowcs(NULL, vis, 0);
1280 if (*wlen == (size_t)-1) {
1281 err = got_error_from_errno("mbstowcs"); /* give up */
1282 goto done;
1286 *ws = calloc(*wlen + 1, sizeof(**ws));
1287 if (*ws == NULL) {
1288 err = got_error_from_errno("calloc");
1289 goto done;
1292 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1293 err = got_error_from_errno("mbstowcs");
1294 done:
1295 free(vis);
1296 if (err) {
1297 free(*ws);
1298 *ws = NULL;
1299 *wlen = 0;
1301 return err;
1304 static const struct got_error *
1305 expand_tab(char **ptr, const char *src)
1307 char *dst;
1308 size_t len, n, idx = 0, sz = 0;
1310 *ptr = NULL;
1311 n = len = strlen(src);
1312 dst = malloc(n + 1);
1313 if (dst == NULL)
1314 return got_error_from_errno("malloc");
1316 while (idx < len && src[idx]) {
1317 const char c = src[idx];
1319 if (c == '\t') {
1320 size_t nb = TABSIZE - sz % TABSIZE;
1321 char *p;
1323 p = realloc(dst, n + nb);
1324 if (p == NULL) {
1325 free(dst);
1326 return got_error_from_errno("realloc");
1329 dst = p;
1330 n += nb;
1331 memset(dst + sz, ' ', nb);
1332 sz += nb;
1333 } else
1334 dst[sz++] = src[idx];
1335 ++idx;
1338 dst[sz] = '\0';
1339 *ptr = dst;
1340 return NULL;
1344 * Advance at most n columns from wline starting at offset off.
1345 * Return the index to the first character after the span operation.
1346 * Return the combined column width of all spanned wide character in
1347 * *rcol.
1349 static int
1350 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1352 int width, i, cols = 0;
1354 if (n == 0) {
1355 *rcol = cols;
1356 return off;
1359 for (i = off; wline[i] != L'\0'; ++i) {
1360 if (wline[i] == L'\t')
1361 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1362 else
1363 width = wcwidth(wline[i]);
1365 if (width == -1) {
1366 width = 1;
1367 wline[i] = L'.';
1370 if (cols + width > n)
1371 break;
1372 cols += width;
1375 *rcol = cols;
1376 return i;
1380 * Format a line for display, ensuring that it won't overflow a width limit.
1381 * With scrolling, the width returned refers to the scrolled version of the
1382 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1384 static const struct got_error *
1385 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1386 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1388 const struct got_error *err = NULL;
1389 int cols;
1390 wchar_t *wline = NULL;
1391 char *exstr = NULL;
1392 size_t wlen;
1393 int i, scrollx;
1395 *wlinep = NULL;
1396 *widthp = 0;
1398 if (expand) {
1399 err = expand_tab(&exstr, line);
1400 if (err)
1401 return err;
1404 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1405 free(exstr);
1406 if (err)
1407 return err;
1409 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1411 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1412 wline[wlen - 1] = L'\0';
1413 wlen--;
1415 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1416 wline[wlen - 1] = L'\0';
1417 wlen--;
1420 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1421 wline[i] = L'\0';
1423 if (widthp)
1424 *widthp = cols;
1425 if (scrollxp)
1426 *scrollxp = scrollx;
1427 if (err)
1428 free(wline);
1429 else
1430 *wlinep = wline;
1431 return err;
1434 static const struct got_error*
1435 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1436 struct got_object_id *id, struct got_repository *repo)
1438 static const struct got_error *err = NULL;
1439 struct got_reflist_entry *re;
1440 char *s;
1441 const char *name;
1443 *refs_str = NULL;
1445 TAILQ_FOREACH(re, refs, entry) {
1446 struct got_tag_object *tag = NULL;
1447 struct got_object_id *ref_id;
1448 int cmp;
1450 name = got_ref_get_name(re->ref);
1451 if (strcmp(name, GOT_REF_HEAD) == 0)
1452 continue;
1453 if (strncmp(name, "refs/", 5) == 0)
1454 name += 5;
1455 if (strncmp(name, "got/", 4) == 0 &&
1456 strncmp(name, "got/backup/", 11) != 0)
1457 continue;
1458 if (strncmp(name, "heads/", 6) == 0)
1459 name += 6;
1460 if (strncmp(name, "remotes/", 8) == 0) {
1461 name += 8;
1462 s = strstr(name, "/" GOT_REF_HEAD);
1463 if (s != NULL && s[strlen(s)] == '\0')
1464 continue;
1466 err = got_ref_resolve(&ref_id, repo, re->ref);
1467 if (err)
1468 break;
1469 if (strncmp(name, "tags/", 5) == 0) {
1470 err = got_object_open_as_tag(&tag, repo, ref_id);
1471 if (err) {
1472 if (err->code != GOT_ERR_OBJ_TYPE) {
1473 free(ref_id);
1474 break;
1476 /* Ref points at something other than a tag. */
1477 err = NULL;
1478 tag = NULL;
1481 cmp = got_object_id_cmp(tag ?
1482 got_object_tag_get_object_id(tag) : ref_id, id);
1483 free(ref_id);
1484 if (tag)
1485 got_object_tag_close(tag);
1486 if (cmp != 0)
1487 continue;
1488 s = *refs_str;
1489 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1490 s ? ", " : "", name) == -1) {
1491 err = got_error_from_errno("asprintf");
1492 free(s);
1493 *refs_str = NULL;
1494 break;
1496 free(s);
1499 return err;
1502 static const struct got_error *
1503 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1504 int col_tab_align)
1506 char *smallerthan;
1508 smallerthan = strchr(author, '<');
1509 if (smallerthan && smallerthan[1] != '\0')
1510 author = smallerthan + 1;
1511 author[strcspn(author, "@>")] = '\0';
1512 return format_line(wauthor, author_width, NULL, author, 0, limit,
1513 col_tab_align, 0);
1516 static const struct got_error *
1517 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1518 struct got_object_id *id, const size_t date_display_cols,
1519 int author_display_cols)
1521 struct tog_log_view_state *s = &view->state.log;
1522 const struct got_error *err = NULL;
1523 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1524 char *logmsg0 = NULL, *logmsg = NULL;
1525 char *author = NULL;
1526 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1527 int author_width, logmsg_width;
1528 char *newline, *line = NULL;
1529 int col, limit, scrollx;
1530 const int avail = view->ncols;
1531 struct tm tm;
1532 time_t committer_time;
1533 struct tog_color *tc;
1535 committer_time = got_object_commit_get_committer_time(commit);
1536 if (gmtime_r(&committer_time, &tm) == NULL)
1537 return got_error_from_errno("gmtime_r");
1538 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1539 return got_error(GOT_ERR_NO_SPACE);
1541 if (avail <= date_display_cols)
1542 limit = MIN(sizeof(datebuf) - 1, avail);
1543 else
1544 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1545 tc = get_color(&s->colors, TOG_COLOR_DATE);
1546 if (tc)
1547 wattr_on(view->window,
1548 COLOR_PAIR(tc->colorpair), NULL);
1549 waddnstr(view->window, datebuf, limit);
1550 if (tc)
1551 wattr_off(view->window,
1552 COLOR_PAIR(tc->colorpair), NULL);
1553 col = limit;
1554 if (col > avail)
1555 goto done;
1557 if (avail >= 120) {
1558 char *id_str;
1559 err = got_object_id_str(&id_str, id);
1560 if (err)
1561 goto done;
1562 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1563 if (tc)
1564 wattr_on(view->window,
1565 COLOR_PAIR(tc->colorpair), NULL);
1566 wprintw(view->window, "%.8s ", id_str);
1567 if (tc)
1568 wattr_off(view->window,
1569 COLOR_PAIR(tc->colorpair), NULL);
1570 free(id_str);
1571 col += 9;
1572 if (col > avail)
1573 goto done;
1576 author = strdup(got_object_commit_get_author(commit));
1577 if (author == NULL) {
1578 err = got_error_from_errno("strdup");
1579 goto done;
1581 err = format_author(&wauthor, &author_width, author, avail - col, col);
1582 if (err)
1583 goto done;
1584 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1585 if (tc)
1586 wattr_on(view->window,
1587 COLOR_PAIR(tc->colorpair), NULL);
1588 waddwstr(view->window, wauthor);
1589 if (tc)
1590 wattr_off(view->window,
1591 COLOR_PAIR(tc->colorpair), NULL);
1592 col += author_width;
1593 while (col < avail && author_width < author_display_cols + 2) {
1594 waddch(view->window, ' ');
1595 col++;
1596 author_width++;
1598 if (col > avail)
1599 goto done;
1601 err = got_object_commit_get_logmsg(&logmsg0, commit);
1602 if (err)
1603 goto done;
1604 logmsg = logmsg0;
1605 while (*logmsg == '\n')
1606 logmsg++;
1607 newline = strchr(logmsg, '\n');
1608 if (newline)
1609 *newline = '\0';
1610 limit = avail - col;
1611 if (view->child && view_is_splitscreen(view->child) && limit > 0)
1612 limit--; /* for the border */
1613 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1614 limit, col, 1);
1615 if (err)
1616 goto done;
1617 waddwstr(view->window, &wlogmsg[scrollx]);
1618 col += MAX(logmsg_width, 0);
1619 while (col < avail) {
1620 waddch(view->window, ' ');
1621 col++;
1623 done:
1624 free(logmsg0);
1625 free(wlogmsg);
1626 free(author);
1627 free(wauthor);
1628 free(line);
1629 return err;
1632 static struct commit_queue_entry *
1633 alloc_commit_queue_entry(struct got_commit_object *commit,
1634 struct got_object_id *id)
1636 struct commit_queue_entry *entry;
1638 entry = calloc(1, sizeof(*entry));
1639 if (entry == NULL)
1640 return NULL;
1642 entry->id = id;
1643 entry->commit = commit;
1644 return entry;
1647 static void
1648 pop_commit(struct commit_queue *commits)
1650 struct commit_queue_entry *entry;
1652 entry = TAILQ_FIRST(&commits->head);
1653 TAILQ_REMOVE(&commits->head, entry, entry);
1654 got_object_commit_close(entry->commit);
1655 commits->ncommits--;
1656 /* Don't free entry->id! It is owned by the commit graph. */
1657 free(entry);
1660 static void
1661 free_commits(struct commit_queue *commits)
1663 while (!TAILQ_EMPTY(&commits->head))
1664 pop_commit(commits);
1667 static const struct got_error *
1668 match_commit(int *have_match, struct got_object_id *id,
1669 struct got_commit_object *commit, regex_t *regex)
1671 const struct got_error *err = NULL;
1672 regmatch_t regmatch;
1673 char *id_str = NULL, *logmsg = NULL;
1675 *have_match = 0;
1677 err = got_object_id_str(&id_str, id);
1678 if (err)
1679 return err;
1681 err = got_object_commit_get_logmsg(&logmsg, commit);
1682 if (err)
1683 goto done;
1685 if (regexec(regex, got_object_commit_get_author(commit), 1,
1686 &regmatch, 0) == 0 ||
1687 regexec(regex, got_object_commit_get_committer(commit), 1,
1688 &regmatch, 0) == 0 ||
1689 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1690 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1691 *have_match = 1;
1692 done:
1693 free(id_str);
1694 free(logmsg);
1695 return err;
1698 static const struct got_error *
1699 queue_commits(struct tog_log_thread_args *a)
1701 const struct got_error *err = NULL;
1704 * We keep all commits open throughout the lifetime of the log
1705 * view in order to avoid having to re-fetch commits from disk
1706 * while updating the display.
1708 do {
1709 struct got_object_id *id;
1710 struct got_commit_object *commit;
1711 struct commit_queue_entry *entry;
1712 int errcode;
1714 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1715 NULL, NULL);
1716 if (err || id == NULL)
1717 break;
1719 err = got_object_open_as_commit(&commit, a->repo, id);
1720 if (err)
1721 break;
1722 entry = alloc_commit_queue_entry(commit, id);
1723 if (entry == NULL) {
1724 err = got_error_from_errno("alloc_commit_queue_entry");
1725 break;
1728 errcode = pthread_mutex_lock(&tog_mutex);
1729 if (errcode) {
1730 err = got_error_set_errno(errcode,
1731 "pthread_mutex_lock");
1732 break;
1735 entry->idx = a->commits->ncommits;
1736 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1737 a->commits->ncommits++;
1739 if (*a->searching == TOG_SEARCH_FORWARD &&
1740 !*a->search_next_done) {
1741 int have_match;
1742 err = match_commit(&have_match, id, commit, a->regex);
1743 if (err)
1744 break;
1745 if (have_match)
1746 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1749 errcode = pthread_mutex_unlock(&tog_mutex);
1750 if (errcode && err == NULL)
1751 err = got_error_set_errno(errcode,
1752 "pthread_mutex_unlock");
1753 if (err)
1754 break;
1755 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1757 return err;
1760 static void
1761 select_commit(struct tog_log_view_state *s)
1763 struct commit_queue_entry *entry;
1764 int ncommits = 0;
1766 entry = s->first_displayed_entry;
1767 while (entry) {
1768 if (ncommits == s->selected) {
1769 s->selected_entry = entry;
1770 break;
1772 entry = TAILQ_NEXT(entry, entry);
1773 ncommits++;
1777 static const struct got_error *
1778 draw_commits(struct tog_view *view)
1780 const struct got_error *err = NULL;
1781 struct tog_log_view_state *s = &view->state.log;
1782 struct commit_queue_entry *entry = s->selected_entry;
1783 const int limit = view->nlines;
1784 int width;
1785 int ncommits, author_cols = 4;
1786 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1787 char *refs_str = NULL;
1788 wchar_t *wline;
1789 struct tog_color *tc;
1790 static const size_t date_display_cols = 12;
1792 if (s->selected_entry &&
1793 !(view->searching && view->search_next_done == 0)) {
1794 struct got_reflist_head *refs;
1795 err = got_object_id_str(&id_str, s->selected_entry->id);
1796 if (err)
1797 return err;
1798 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1799 s->selected_entry->id);
1800 if (refs) {
1801 err = build_refs_str(&refs_str, refs,
1802 s->selected_entry->id, s->repo);
1803 if (err)
1804 goto done;
1808 if (s->thread_args.commits_needed == 0)
1809 halfdelay(10); /* disable fast refresh */
1811 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1812 if (asprintf(&ncommits_str, " [%d/%d] %s",
1813 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1814 (view->searching && !view->search_next_done) ?
1815 "searching..." : "loading...") == -1) {
1816 err = got_error_from_errno("asprintf");
1817 goto done;
1819 } else {
1820 const char *search_str = NULL;
1822 if (view->searching) {
1823 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1824 search_str = "no more matches";
1825 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1826 search_str = "no matches found";
1827 else if (!view->search_next_done)
1828 search_str = "searching...";
1831 if (asprintf(&ncommits_str, " [%d/%d] %s",
1832 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1833 search_str ? search_str :
1834 (refs_str ? refs_str : "")) == -1) {
1835 err = got_error_from_errno("asprintf");
1836 goto done;
1840 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1841 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1842 "........................................",
1843 s->in_repo_path, ncommits_str) == -1) {
1844 err = got_error_from_errno("asprintf");
1845 header = NULL;
1846 goto done;
1848 } else if (asprintf(&header, "commit %s%s",
1849 id_str ? id_str : "........................................",
1850 ncommits_str) == -1) {
1851 err = got_error_from_errno("asprintf");
1852 header = NULL;
1853 goto done;
1855 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1856 if (err)
1857 goto done;
1859 werase(view->window);
1861 if (view_needs_focus_indication(view))
1862 wstandout(view->window);
1863 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1864 if (tc)
1865 wattr_on(view->window,
1866 COLOR_PAIR(tc->colorpair), NULL);
1867 waddwstr(view->window, wline);
1868 if (tc)
1869 wattr_off(view->window,
1870 COLOR_PAIR(tc->colorpair), NULL);
1871 while (width < view->ncols) {
1872 waddch(view->window, ' ');
1873 width++;
1875 if (view_needs_focus_indication(view))
1876 wstandend(view->window);
1877 free(wline);
1878 if (limit <= 1)
1879 goto done;
1881 /* Grow author column size if necessary, and set view->maxx. */
1882 entry = s->first_displayed_entry;
1883 ncommits = 0;
1884 view->maxx = 0;
1885 while (entry) {
1886 char *author, *eol, *msg, *msg0;
1887 wchar_t *wauthor, *wmsg;
1888 int width;
1889 if (ncommits >= limit - 1)
1890 break;
1891 author = strdup(got_object_commit_get_author(entry->commit));
1892 if (author == NULL) {
1893 err = got_error_from_errno("strdup");
1894 goto done;
1896 err = format_author(&wauthor, &width, author, COLS,
1897 date_display_cols);
1898 if (author_cols < width)
1899 author_cols = width;
1900 free(wauthor);
1901 free(author);
1902 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1903 if (err)
1904 goto done;
1905 msg = msg0;
1906 while (*msg == '\n')
1907 ++msg;
1908 if ((eol = strchr(msg, '\n')))
1909 *eol = '\0';
1910 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1911 date_display_cols + author_cols, 0);
1912 if (err)
1913 goto done;
1914 view->maxx = MAX(view->maxx, width);
1915 free(msg0);
1916 free(wmsg);
1917 ncommits++;
1918 entry = TAILQ_NEXT(entry, entry);
1921 entry = s->first_displayed_entry;
1922 s->last_displayed_entry = s->first_displayed_entry;
1923 ncommits = 0;
1924 while (entry) {
1925 if (ncommits >= limit - 1)
1926 break;
1927 if (ncommits == s->selected)
1928 wstandout(view->window);
1929 err = draw_commit(view, entry->commit, entry->id,
1930 date_display_cols, author_cols);
1931 if (ncommits == s->selected)
1932 wstandend(view->window);
1933 if (err)
1934 goto done;
1935 ncommits++;
1936 s->last_displayed_entry = entry;
1937 entry = TAILQ_NEXT(entry, entry);
1940 view_vborder(view);
1941 update_panels();
1942 doupdate();
1943 done:
1944 free(id_str);
1945 free(refs_str);
1946 free(ncommits_str);
1947 free(header);
1948 return err;
1951 static void
1952 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1954 struct commit_queue_entry *entry;
1955 int nscrolled = 0;
1957 entry = TAILQ_FIRST(&s->commits.head);
1958 if (s->first_displayed_entry == entry)
1959 return;
1961 entry = s->first_displayed_entry;
1962 while (entry && nscrolled < maxscroll) {
1963 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1964 if (entry) {
1965 s->first_displayed_entry = entry;
1966 nscrolled++;
1971 static const struct got_error *
1972 trigger_log_thread(struct tog_view *view, int wait)
1974 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1975 int errcode;
1977 halfdelay(1); /* fast refresh while loading commits */
1979 while (ta->commits_needed > 0 || ta->load_all) {
1980 if (ta->log_complete)
1981 break;
1983 /* Wake the log thread. */
1984 errcode = pthread_cond_signal(&ta->need_commits);
1985 if (errcode)
1986 return got_error_set_errno(errcode,
1987 "pthread_cond_signal");
1990 * The mutex will be released while the view loop waits
1991 * in wgetch(), at which time the log thread will run.
1993 if (!wait)
1994 break;
1996 /* Display progress update in log view. */
1997 show_log_view(view);
1998 update_panels();
1999 doupdate();
2001 /* Wait right here while next commit is being loaded. */
2002 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2003 if (errcode)
2004 return got_error_set_errno(errcode,
2005 "pthread_cond_wait");
2007 /* Display progress update in log view. */
2008 show_log_view(view);
2009 update_panels();
2010 doupdate();
2013 return NULL;
2016 static const struct got_error *
2017 log_scroll_down(struct tog_view *view, int maxscroll)
2019 struct tog_log_view_state *s = &view->state.log;
2020 const struct got_error *err = NULL;
2021 struct commit_queue_entry *pentry;
2022 int nscrolled = 0, ncommits_needed;
2024 if (s->last_displayed_entry == NULL)
2025 return NULL;
2027 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2028 if (s->commits.ncommits < ncommits_needed &&
2029 !s->thread_args.log_complete) {
2031 * Ask the log thread for required amount of commits.
2033 s->thread_args.commits_needed += maxscroll;
2034 err = trigger_log_thread(view, 1);
2035 if (err)
2036 return err;
2039 do {
2040 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2041 if (pentry == NULL)
2042 break;
2044 s->last_displayed_entry = pentry;
2046 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2047 if (pentry == NULL)
2048 break;
2049 s->first_displayed_entry = pentry;
2050 } while (++nscrolled < maxscroll);
2052 return err;
2055 static const struct got_error *
2056 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2057 struct got_commit_object *commit, struct got_object_id *commit_id,
2058 struct tog_view *log_view, struct got_repository *repo)
2060 const struct got_error *err;
2061 struct got_object_qid *parent_id;
2062 struct tog_view *diff_view;
2064 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2065 if (diff_view == NULL)
2066 return got_error_from_errno("view_open");
2068 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2069 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2070 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2071 if (err == NULL)
2072 *new_view = diff_view;
2073 return err;
2076 static const struct got_error *
2077 tree_view_visit_subtree(struct tog_tree_view_state *s,
2078 struct got_tree_object *subtree)
2080 struct tog_parent_tree *parent;
2082 parent = calloc(1, sizeof(*parent));
2083 if (parent == NULL)
2084 return got_error_from_errno("calloc");
2086 parent->tree = s->tree;
2087 parent->first_displayed_entry = s->first_displayed_entry;
2088 parent->selected_entry = s->selected_entry;
2089 parent->selected = s->selected;
2090 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2091 s->tree = subtree;
2092 s->selected = 0;
2093 s->first_displayed_entry = NULL;
2094 return NULL;
2097 static const struct got_error *
2098 tree_view_walk_path(struct tog_tree_view_state *s,
2099 struct got_commit_object *commit, const char *path)
2101 const struct got_error *err = NULL;
2102 struct got_tree_object *tree = NULL;
2103 const char *p;
2104 char *slash, *subpath = NULL;
2106 /* Walk the path and open corresponding tree objects. */
2107 p = path;
2108 while (*p) {
2109 struct got_tree_entry *te;
2110 struct got_object_id *tree_id;
2111 char *te_name;
2113 while (p[0] == '/')
2114 p++;
2116 /* Ensure the correct subtree entry is selected. */
2117 slash = strchr(p, '/');
2118 if (slash == NULL)
2119 te_name = strdup(p);
2120 else
2121 te_name = strndup(p, slash - p);
2122 if (te_name == NULL) {
2123 err = got_error_from_errno("strndup");
2124 break;
2126 te = got_object_tree_find_entry(s->tree, te_name);
2127 if (te == NULL) {
2128 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2129 free(te_name);
2130 break;
2132 free(te_name);
2133 s->first_displayed_entry = s->selected_entry = te;
2135 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2136 break; /* jump to this file's entry */
2138 slash = strchr(p, '/');
2139 if (slash)
2140 subpath = strndup(path, slash - path);
2141 else
2142 subpath = strdup(path);
2143 if (subpath == NULL) {
2144 err = got_error_from_errno("strdup");
2145 break;
2148 err = got_object_id_by_path(&tree_id, s->repo, commit,
2149 subpath);
2150 if (err)
2151 break;
2153 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2154 free(tree_id);
2155 if (err)
2156 break;
2158 err = tree_view_visit_subtree(s, tree);
2159 if (err) {
2160 got_object_tree_close(tree);
2161 break;
2163 if (slash == NULL)
2164 break;
2165 free(subpath);
2166 subpath = NULL;
2167 p = slash;
2170 free(subpath);
2171 return err;
2174 static const struct got_error *
2175 browse_commit_tree(struct tog_view **new_view, int begin_x,
2176 struct commit_queue_entry *entry, const char *path,
2177 const char *head_ref_name, struct got_repository *repo)
2179 const struct got_error *err = NULL;
2180 struct tog_tree_view_state *s;
2181 struct tog_view *tree_view;
2183 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2184 if (tree_view == NULL)
2185 return got_error_from_errno("view_open");
2187 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2188 if (err)
2189 return err;
2190 s = &tree_view->state.tree;
2192 *new_view = tree_view;
2194 if (got_path_is_root_dir(path))
2195 return NULL;
2197 return tree_view_walk_path(s, entry->commit, path);
2200 static const struct got_error *
2201 block_signals_used_by_main_thread(void)
2203 sigset_t sigset;
2204 int errcode;
2206 if (sigemptyset(&sigset) == -1)
2207 return got_error_from_errno("sigemptyset");
2209 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2210 if (sigaddset(&sigset, SIGWINCH) == -1)
2211 return got_error_from_errno("sigaddset");
2212 if (sigaddset(&sigset, SIGCONT) == -1)
2213 return got_error_from_errno("sigaddset");
2214 if (sigaddset(&sigset, SIGINT) == -1)
2215 return got_error_from_errno("sigaddset");
2216 if (sigaddset(&sigset, SIGTERM) == -1)
2217 return got_error_from_errno("sigaddset");
2219 /* ncurses handles SIGTSTP */
2220 if (sigaddset(&sigset, SIGTSTP) == -1)
2221 return got_error_from_errno("sigaddset");
2223 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2224 if (errcode)
2225 return got_error_set_errno(errcode, "pthread_sigmask");
2227 return NULL;
2230 static void *
2231 log_thread(void *arg)
2233 const struct got_error *err = NULL;
2234 int errcode = 0;
2235 struct tog_log_thread_args *a = arg;
2236 int done = 0;
2238 err = block_signals_used_by_main_thread();
2239 if (err)
2240 return (void *)err;
2242 while (!done && !err && !tog_fatal_signal_received()) {
2243 err = queue_commits(a);
2244 if (err) {
2245 if (err->code != GOT_ERR_ITER_COMPLETED)
2246 return (void *)err;
2247 err = NULL;
2248 done = 1;
2249 } else if (a->commits_needed > 0 && !a->load_all)
2250 a->commits_needed--;
2252 errcode = pthread_mutex_lock(&tog_mutex);
2253 if (errcode) {
2254 err = got_error_set_errno(errcode,
2255 "pthread_mutex_lock");
2256 break;
2257 } else if (*a->quit)
2258 done = 1;
2259 else if (*a->first_displayed_entry == NULL) {
2260 *a->first_displayed_entry =
2261 TAILQ_FIRST(&a->commits->head);
2262 *a->selected_entry = *a->first_displayed_entry;
2265 errcode = pthread_cond_signal(&a->commit_loaded);
2266 if (errcode) {
2267 err = got_error_set_errno(errcode,
2268 "pthread_cond_signal");
2269 pthread_mutex_unlock(&tog_mutex);
2270 break;
2273 if (done)
2274 a->commits_needed = 0;
2275 else {
2276 if (a->commits_needed == 0 && !a->load_all) {
2277 errcode = pthread_cond_wait(&a->need_commits,
2278 &tog_mutex);
2279 if (errcode)
2280 err = got_error_set_errno(errcode,
2281 "pthread_cond_wait");
2282 if (*a->quit)
2283 done = 1;
2287 errcode = pthread_mutex_unlock(&tog_mutex);
2288 if (errcode && err == NULL)
2289 err = got_error_set_errno(errcode,
2290 "pthread_mutex_unlock");
2292 a->log_complete = 1;
2293 return (void *)err;
2296 static const struct got_error *
2297 stop_log_thread(struct tog_log_view_state *s)
2299 const struct got_error *err = NULL;
2300 int errcode;
2302 if (s->thread) {
2303 s->quit = 1;
2304 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2305 if (errcode)
2306 return got_error_set_errno(errcode,
2307 "pthread_cond_signal");
2308 errcode = pthread_mutex_unlock(&tog_mutex);
2309 if (errcode)
2310 return got_error_set_errno(errcode,
2311 "pthread_mutex_unlock");
2312 errcode = pthread_join(s->thread, (void **)&err);
2313 if (errcode)
2314 return got_error_set_errno(errcode, "pthread_join");
2315 errcode = pthread_mutex_lock(&tog_mutex);
2316 if (errcode)
2317 return got_error_set_errno(errcode,
2318 "pthread_mutex_lock");
2319 s->thread = 0; //NULL;
2322 if (s->thread_args.repo) {
2323 err = got_repo_close(s->thread_args.repo);
2324 s->thread_args.repo = NULL;
2327 if (s->thread_args.pack_fds) {
2328 const struct got_error *pack_err =
2329 got_repo_pack_fds_close(s->thread_args.pack_fds);
2330 if (err == NULL)
2331 err = pack_err;
2332 s->thread_args.pack_fds = NULL;
2335 if (s->thread_args.graph) {
2336 got_commit_graph_close(s->thread_args.graph);
2337 s->thread_args.graph = NULL;
2340 return err;
2343 static const struct got_error *
2344 close_log_view(struct tog_view *view)
2346 const struct got_error *err = NULL;
2347 struct tog_log_view_state *s = &view->state.log;
2348 int errcode;
2350 err = stop_log_thread(s);
2352 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2353 if (errcode && err == NULL)
2354 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2356 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2357 if (errcode && err == NULL)
2358 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2360 free_commits(&s->commits);
2361 free(s->in_repo_path);
2362 s->in_repo_path = NULL;
2363 free(s->start_id);
2364 s->start_id = NULL;
2365 free(s->head_ref_name);
2366 s->head_ref_name = NULL;
2367 return err;
2370 static const struct got_error *
2371 search_start_log_view(struct tog_view *view)
2373 struct tog_log_view_state *s = &view->state.log;
2375 s->matched_entry = NULL;
2376 s->search_entry = NULL;
2377 return NULL;
2380 static const struct got_error *
2381 search_next_log_view(struct tog_view *view)
2383 const struct got_error *err = NULL;
2384 struct tog_log_view_state *s = &view->state.log;
2385 struct commit_queue_entry *entry;
2387 /* Display progress update in log view. */
2388 show_log_view(view);
2389 update_panels();
2390 doupdate();
2392 if (s->search_entry) {
2393 int errcode, ch;
2394 errcode = pthread_mutex_unlock(&tog_mutex);
2395 if (errcode)
2396 return got_error_set_errno(errcode,
2397 "pthread_mutex_unlock");
2398 ch = wgetch(view->window);
2399 errcode = pthread_mutex_lock(&tog_mutex);
2400 if (errcode)
2401 return got_error_set_errno(errcode,
2402 "pthread_mutex_lock");
2403 if (ch == KEY_BACKSPACE) {
2404 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2405 return NULL;
2407 if (view->searching == TOG_SEARCH_FORWARD)
2408 entry = TAILQ_NEXT(s->search_entry, entry);
2409 else
2410 entry = TAILQ_PREV(s->search_entry,
2411 commit_queue_head, entry);
2412 } else if (s->matched_entry) {
2413 int matched_idx = s->matched_entry->idx;
2414 int selected_idx = s->selected_entry->idx;
2417 * If the user has moved the cursor after we hit a match,
2418 * the position from where we should continue searching
2419 * might have changed.
2421 if (view->searching == TOG_SEARCH_FORWARD) {
2422 if (matched_idx > selected_idx)
2423 entry = TAILQ_NEXT(s->selected_entry, entry);
2424 else
2425 entry = TAILQ_NEXT(s->matched_entry, entry);
2426 } else {
2427 if (matched_idx < selected_idx)
2428 entry = TAILQ_PREV(s->selected_entry,
2429 commit_queue_head, entry);
2430 else
2431 entry = TAILQ_PREV(s->matched_entry,
2432 commit_queue_head, entry);
2434 } else {
2435 entry = s->selected_entry;
2438 while (1) {
2439 int have_match = 0;
2441 if (entry == NULL) {
2442 if (s->thread_args.log_complete ||
2443 view->searching == TOG_SEARCH_BACKWARD) {
2444 view->search_next_done =
2445 (s->matched_entry == NULL ?
2446 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2447 s->search_entry = NULL;
2448 return NULL;
2451 * Poke the log thread for more commits and return,
2452 * allowing the main loop to make progress. Search
2453 * will resume at s->search_entry once we come back.
2455 s->thread_args.commits_needed++;
2456 return trigger_log_thread(view, 0);
2459 err = match_commit(&have_match, entry->id, entry->commit,
2460 &view->regex);
2461 if (err)
2462 break;
2463 if (have_match) {
2464 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2465 s->matched_entry = entry;
2466 break;
2469 s->search_entry = entry;
2470 if (view->searching == TOG_SEARCH_FORWARD)
2471 entry = TAILQ_NEXT(entry, entry);
2472 else
2473 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2476 if (s->matched_entry) {
2477 int cur = s->selected_entry->idx;
2478 while (cur < s->matched_entry->idx) {
2479 err = input_log_view(NULL, view, KEY_DOWN);
2480 if (err)
2481 return err;
2482 cur++;
2484 while (cur > s->matched_entry->idx) {
2485 err = input_log_view(NULL, view, KEY_UP);
2486 if (err)
2487 return err;
2488 cur--;
2492 s->search_entry = NULL;
2494 return NULL;
2497 static const struct got_error *
2498 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2499 struct got_repository *repo, const char *head_ref_name,
2500 const char *in_repo_path, int log_branches)
2502 const struct got_error *err = NULL;
2503 struct tog_log_view_state *s = &view->state.log;
2504 struct got_repository *thread_repo = NULL;
2505 struct got_commit_graph *thread_graph = NULL;
2506 int errcode;
2508 if (in_repo_path != s->in_repo_path) {
2509 free(s->in_repo_path);
2510 s->in_repo_path = strdup(in_repo_path);
2511 if (s->in_repo_path == NULL)
2512 return got_error_from_errno("strdup");
2515 /* The commit queue only contains commits being displayed. */
2516 TAILQ_INIT(&s->commits.head);
2517 s->commits.ncommits = 0;
2519 s->repo = repo;
2520 if (head_ref_name) {
2521 s->head_ref_name = strdup(head_ref_name);
2522 if (s->head_ref_name == NULL) {
2523 err = got_error_from_errno("strdup");
2524 goto done;
2527 s->start_id = got_object_id_dup(start_id);
2528 if (s->start_id == NULL) {
2529 err = got_error_from_errno("got_object_id_dup");
2530 goto done;
2532 s->log_branches = log_branches;
2534 STAILQ_INIT(&s->colors);
2535 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2536 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2537 get_color_value("TOG_COLOR_COMMIT"));
2538 if (err)
2539 goto done;
2540 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2541 get_color_value("TOG_COLOR_AUTHOR"));
2542 if (err) {
2543 free_colors(&s->colors);
2544 goto done;
2546 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2547 get_color_value("TOG_COLOR_DATE"));
2548 if (err) {
2549 free_colors(&s->colors);
2550 goto done;
2554 view->show = show_log_view;
2555 view->input = input_log_view;
2556 view->close = close_log_view;
2557 view->search_start = search_start_log_view;
2558 view->search_next = search_next_log_view;
2560 if (s->thread_args.pack_fds == NULL) {
2561 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2562 if (err)
2563 goto done;
2565 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2566 s->thread_args.pack_fds);
2567 if (err)
2568 goto done;
2569 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2570 !s->log_branches);
2571 if (err)
2572 goto done;
2573 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2574 s->repo, NULL, NULL);
2575 if (err)
2576 goto done;
2578 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2579 if (errcode) {
2580 err = got_error_set_errno(errcode, "pthread_cond_init");
2581 goto done;
2583 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2584 if (errcode) {
2585 err = got_error_set_errno(errcode, "pthread_cond_init");
2586 goto done;
2589 s->thread_args.commits_needed = view->nlines;
2590 s->thread_args.graph = thread_graph;
2591 s->thread_args.commits = &s->commits;
2592 s->thread_args.in_repo_path = s->in_repo_path;
2593 s->thread_args.start_id = s->start_id;
2594 s->thread_args.repo = thread_repo;
2595 s->thread_args.log_complete = 0;
2596 s->thread_args.quit = &s->quit;
2597 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2598 s->thread_args.selected_entry = &s->selected_entry;
2599 s->thread_args.searching = &view->searching;
2600 s->thread_args.search_next_done = &view->search_next_done;
2601 s->thread_args.regex = &view->regex;
2602 done:
2603 if (err)
2604 close_log_view(view);
2605 return err;
2608 static const struct got_error *
2609 show_log_view(struct tog_view *view)
2611 const struct got_error *err;
2612 struct tog_log_view_state *s = &view->state.log;
2614 if (s->thread == 0) { //NULL) {
2615 int errcode = pthread_create(&s->thread, NULL, log_thread,
2616 &s->thread_args);
2617 if (errcode)
2618 return got_error_set_errno(errcode, "pthread_create");
2619 if (s->thread_args.commits_needed > 0) {
2620 err = trigger_log_thread(view, 1);
2621 if (err)
2622 return err;
2626 return draw_commits(view);
2629 static const struct got_error *
2630 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2632 const struct got_error *err = NULL;
2633 struct tog_log_view_state *s = &view->state.log;
2634 struct tog_view *diff_view = NULL, *tree_view = NULL;
2635 struct tog_view *ref_view = NULL;
2636 struct commit_queue_entry *entry;
2637 int begin_x = 0, n, nscroll = view->nlines - 1;
2639 if (s->thread_args.load_all) {
2640 if (ch == KEY_BACKSPACE)
2641 s->thread_args.load_all = 0;
2642 else if (s->thread_args.log_complete) {
2643 s->thread_args.load_all = 0;
2644 log_scroll_down(view, s->commits.ncommits);
2645 s->selected = MIN(view->nlines - 2,
2646 s->commits.ncommits - 1);
2647 select_commit(s);
2649 return NULL;
2652 switch (ch) {
2653 case 'q':
2654 s->quit = 1;
2655 break;
2656 case '0':
2657 view->x = 0;
2658 break;
2659 case '$':
2660 view->x = MAX(view->maxx - view->ncols / 2, 0);
2661 view->count = 0;
2662 break;
2663 case KEY_RIGHT:
2664 case 'l':
2665 if (view->x + view->ncols / 2 < view->maxx)
2666 view->x += 2; /* move two columns right */
2667 else
2668 view->count = 0;
2669 break;
2670 case KEY_LEFT:
2671 case 'h':
2672 view->x -= MIN(view->x, 2); /* move two columns back */
2673 if (view->x <= 0)
2674 view->count = 0;
2675 break;
2676 case 'k':
2677 case KEY_UP:
2678 case '<':
2679 case ',':
2680 case CTRL('p'):
2681 if (s->selected_entry->idx == 0)
2682 view->count = 0;
2683 if (s->first_displayed_entry == NULL)
2684 break;
2685 if (s->selected > 0)
2686 s->selected--;
2687 else
2688 log_scroll_up(s, 1);
2689 select_commit(s);
2690 break;
2691 case 'g':
2692 case KEY_HOME:
2693 s->selected = 0;
2694 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2695 select_commit(s);
2696 view->count = 0;
2697 break;
2698 case CTRL('u'):
2699 case 'u':
2700 nscroll /= 2;
2701 /* FALL THROUGH */
2702 case KEY_PPAGE:
2703 case CTRL('b'):
2704 case 'b':
2705 if (s->first_displayed_entry == NULL)
2706 break;
2707 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2708 s->selected = MAX(0, s->selected - nscroll - 1);
2709 else
2710 log_scroll_up(s, nscroll);
2711 select_commit(s);
2712 if (s->selected_entry->idx == 0)
2713 view->count = 0;
2714 break;
2715 case 'j':
2716 case KEY_DOWN:
2717 case '>':
2718 case '.':
2719 case CTRL('n'):
2720 if (s->first_displayed_entry == NULL)
2721 break;
2722 if (s->selected < MIN(view->nlines - 2,
2723 s->commits.ncommits - 1))
2724 s->selected++;
2725 else {
2726 err = log_scroll_down(view, 1);
2727 if (err)
2728 break;
2730 select_commit(s);
2731 if (s->thread_args.log_complete &&
2732 s->selected_entry->idx == s->commits.ncommits - 1)
2733 view->count = 0;
2734 break;
2735 case 'G':
2736 case KEY_END: {
2737 /* We don't know yet how many commits, so we're forced to
2738 * traverse them all. */
2739 view->count = 0;
2740 if (!s->thread_args.log_complete) {
2741 s->thread_args.load_all = 1;
2742 return trigger_log_thread(view, 0);
2745 s->selected = 0;
2746 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2747 for (n = 0; n < view->nlines - 1; n++) {
2748 if (entry == NULL)
2749 break;
2750 s->first_displayed_entry = entry;
2751 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2753 if (n > 0)
2754 s->selected = n - 1;
2755 select_commit(s);
2756 break;
2758 case CTRL('d'):
2759 case 'd':
2760 nscroll /= 2;
2761 /* FALL THROUGH */
2762 case KEY_NPAGE:
2763 case CTRL('f'):
2764 case 'f':
2765 case ' ': {
2766 struct commit_queue_entry *first;
2767 first = s->first_displayed_entry;
2768 if (first == NULL) {
2769 view->count = 0;
2770 break;
2772 err = log_scroll_down(view, nscroll);
2773 if (err)
2774 break;
2775 if (first == s->first_displayed_entry &&
2776 s->selected < MIN(view->nlines - 2,
2777 s->commits.ncommits - 1)) {
2778 /* can't scroll further down */
2779 s->selected += MIN(s->last_displayed_entry->idx -
2780 s->selected_entry->idx, nscroll + 1);
2782 select_commit(s);
2783 if (s->thread_args.log_complete &&
2784 s->selected_entry->idx == s->commits.ncommits - 1)
2785 view->count = 0;
2786 break;
2788 case KEY_RESIZE:
2789 if (s->selected > view->nlines - 2)
2790 s->selected = view->nlines - 2;
2791 if (s->selected > s->commits.ncommits - 1)
2792 s->selected = s->commits.ncommits - 1;
2793 select_commit(s);
2794 if (s->commits.ncommits < view->nlines - 1 &&
2795 !s->thread_args.log_complete) {
2796 s->thread_args.commits_needed += (view->nlines - 1) -
2797 s->commits.ncommits;
2798 err = trigger_log_thread(view, 1);
2800 break;
2801 case KEY_ENTER:
2802 case '\r':
2803 view->count = 0;
2804 if (s->selected_entry == NULL)
2805 break;
2806 if (view_is_parent_view(view))
2807 begin_x = view_split_begin_x(view->begin_x);
2808 err = open_diff_view_for_commit(&diff_view, begin_x,
2809 s->selected_entry->commit, s->selected_entry->id,
2810 view, s->repo);
2811 if (err)
2812 break;
2813 view->focussed = 0;
2814 diff_view->focussed = 1;
2815 if (view_is_parent_view(view)) {
2816 err = view_close_child(view);
2817 if (err)
2818 return err;
2819 err = view_set_child(view, diff_view);
2820 if (err)
2821 return err;
2822 view->focus_child = 1;
2823 } else
2824 *new_view = diff_view;
2825 break;
2826 case 't':
2827 view->count = 0;
2828 if (s->selected_entry == NULL)
2829 break;
2830 if (view_is_parent_view(view))
2831 begin_x = view_split_begin_x(view->begin_x);
2832 err = browse_commit_tree(&tree_view, begin_x,
2833 s->selected_entry, s->in_repo_path, s->head_ref_name,
2834 s->repo);
2835 if (err)
2836 break;
2837 view->focussed = 0;
2838 tree_view->focussed = 1;
2839 if (view_is_parent_view(view)) {
2840 err = view_close_child(view);
2841 if (err)
2842 return err;
2843 err = view_set_child(view, tree_view);
2844 if (err)
2845 return err;
2846 view->focus_child = 1;
2847 } else
2848 *new_view = tree_view;
2849 break;
2850 case KEY_BACKSPACE:
2851 case CTRL('l'):
2852 case 'B':
2853 view->count = 0;
2854 if (ch == KEY_BACKSPACE &&
2855 got_path_is_root_dir(s->in_repo_path))
2856 break;
2857 err = stop_log_thread(s);
2858 if (err)
2859 return err;
2860 if (ch == KEY_BACKSPACE) {
2861 char *parent_path;
2862 err = got_path_dirname(&parent_path, s->in_repo_path);
2863 if (err)
2864 return err;
2865 free(s->in_repo_path);
2866 s->in_repo_path = parent_path;
2867 s->thread_args.in_repo_path = s->in_repo_path;
2868 } else if (ch == CTRL('l')) {
2869 struct got_object_id *start_id;
2870 err = got_repo_match_object_id(&start_id, NULL,
2871 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2872 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2873 if (err)
2874 return err;
2875 free(s->start_id);
2876 s->start_id = start_id;
2877 s->thread_args.start_id = s->start_id;
2878 } else /* 'B' */
2879 s->log_branches = !s->log_branches;
2881 if (s->thread_args.pack_fds == NULL) {
2882 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2883 if (err)
2884 return err;
2886 err = got_repo_open(&s->thread_args.repo,
2887 got_repo_get_path(s->repo), NULL,
2888 s->thread_args.pack_fds);
2889 if (err)
2890 return err;
2891 tog_free_refs();
2892 err = tog_load_refs(s->repo, 0);
2893 if (err)
2894 return err;
2895 err = got_commit_graph_open(&s->thread_args.graph,
2896 s->in_repo_path, !s->log_branches);
2897 if (err)
2898 return err;
2899 err = got_commit_graph_iter_start(s->thread_args.graph,
2900 s->start_id, s->repo, NULL, NULL);
2901 if (err)
2902 return err;
2903 free_commits(&s->commits);
2904 s->first_displayed_entry = NULL;
2905 s->last_displayed_entry = NULL;
2906 s->selected_entry = NULL;
2907 s->selected = 0;
2908 s->thread_args.log_complete = 0;
2909 s->quit = 0;
2910 s->thread_args.commits_needed = view->nlines;
2911 s->matched_entry = NULL;
2912 s->search_entry = NULL;
2913 break;
2914 case 'r':
2915 view->count = 0;
2916 if (view_is_parent_view(view))
2917 begin_x = view_split_begin_x(view->begin_x);
2918 ref_view = view_open(view->nlines, view->ncols,
2919 view->begin_y, begin_x, TOG_VIEW_REF);
2920 if (ref_view == NULL)
2921 return got_error_from_errno("view_open");
2922 err = open_ref_view(ref_view, s->repo);
2923 if (err) {
2924 view_close(ref_view);
2925 return err;
2927 view->focussed = 0;
2928 ref_view->focussed = 1;
2929 if (view_is_parent_view(view)) {
2930 err = view_close_child(view);
2931 if (err)
2932 return err;
2933 err = view_set_child(view, ref_view);
2934 if (err)
2935 return err;
2936 view->focus_child = 1;
2937 } else
2938 *new_view = ref_view;
2939 break;
2940 default:
2941 view->count = 0;
2942 break;
2945 return err;
2948 static const struct got_error *
2949 apply_unveil(const char *repo_path, const char *worktree_path)
2951 const struct got_error *error;
2953 #ifdef PROFILE
2954 if (unveil("gmon.out", "rwc") != 0)
2955 return got_error_from_errno2("unveil", "gmon.out");
2956 #endif
2957 if (repo_path && unveil(repo_path, "r") != 0)
2958 return got_error_from_errno2("unveil", repo_path);
2960 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2961 return got_error_from_errno2("unveil", worktree_path);
2963 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2964 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2966 error = got_privsep_unveil_exec_helpers();
2967 if (error != NULL)
2968 return error;
2970 if (unveil(NULL, NULL) != 0)
2971 return got_error_from_errno("unveil");
2973 return NULL;
2976 static void
2977 init_curses(void)
2980 * Override default signal handlers before starting ncurses.
2981 * This should prevent ncurses from installing its own
2982 * broken cleanup() signal handler.
2984 signal(SIGWINCH, tog_sigwinch);
2985 signal(SIGPIPE, tog_sigpipe);
2986 signal(SIGCONT, tog_sigcont);
2987 signal(SIGINT, tog_sigint);
2988 signal(SIGTERM, tog_sigterm);
2990 initscr();
2991 cbreak();
2992 halfdelay(1); /* Do fast refresh while initial view is loading. */
2993 noecho();
2994 nonl();
2995 intrflush(stdscr, FALSE);
2996 keypad(stdscr, TRUE);
2997 curs_set(0);
2998 if (getenv("TOG_COLORS") != NULL) {
2999 start_color();
3000 use_default_colors();
3004 static const struct got_error *
3005 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3006 struct got_repository *repo, struct got_worktree *worktree)
3008 const struct got_error *err = NULL;
3010 if (argc == 0) {
3011 *in_repo_path = strdup("/");
3012 if (*in_repo_path == NULL)
3013 return got_error_from_errno("strdup");
3014 return NULL;
3017 if (worktree) {
3018 const char *prefix = got_worktree_get_path_prefix(worktree);
3019 char *p;
3021 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3022 if (err)
3023 return err;
3024 if (asprintf(in_repo_path, "%s%s%s", prefix,
3025 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3026 p) == -1) {
3027 err = got_error_from_errno("asprintf");
3028 *in_repo_path = NULL;
3030 free(p);
3031 } else
3032 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3034 return err;
3037 static const struct got_error *
3038 cmd_log(int argc, char *argv[])
3040 const struct got_error *error;
3041 struct got_repository *repo = NULL;
3042 struct got_worktree *worktree = NULL;
3043 struct got_object_id *start_id = NULL;
3044 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3045 char *start_commit = NULL, *label = NULL;
3046 struct got_reference *ref = NULL;
3047 const char *head_ref_name = NULL;
3048 int ch, log_branches = 0;
3049 struct tog_view *view;
3050 int *pack_fds = NULL;
3052 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3053 switch (ch) {
3054 case 'b':
3055 log_branches = 1;
3056 break;
3057 case 'c':
3058 start_commit = optarg;
3059 break;
3060 case 'r':
3061 repo_path = realpath(optarg, NULL);
3062 if (repo_path == NULL)
3063 return got_error_from_errno2("realpath",
3064 optarg);
3065 break;
3066 default:
3067 usage_log();
3068 /* NOTREACHED */
3072 argc -= optind;
3073 argv += optind;
3075 if (argc > 1)
3076 usage_log();
3078 error = got_repo_pack_fds_open(&pack_fds);
3079 if (error != NULL)
3080 goto done;
3082 if (repo_path == NULL) {
3083 cwd = getcwd(NULL, 0);
3084 if (cwd == NULL)
3085 return got_error_from_errno("getcwd");
3086 error = got_worktree_open(&worktree, cwd);
3087 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3088 goto done;
3089 if (worktree)
3090 repo_path =
3091 strdup(got_worktree_get_repo_path(worktree));
3092 else
3093 repo_path = strdup(cwd);
3094 if (repo_path == NULL) {
3095 error = got_error_from_errno("strdup");
3096 goto done;
3100 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3101 if (error != NULL)
3102 goto done;
3104 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3105 repo, worktree);
3106 if (error)
3107 goto done;
3109 init_curses();
3111 error = apply_unveil(got_repo_get_path(repo),
3112 worktree ? got_worktree_get_root_path(worktree) : NULL);
3113 if (error)
3114 goto done;
3116 /* already loaded by tog_log_with_path()? */
3117 if (TAILQ_EMPTY(&tog_refs)) {
3118 error = tog_load_refs(repo, 0);
3119 if (error)
3120 goto done;
3123 if (start_commit == NULL) {
3124 error = got_repo_match_object_id(&start_id, &label,
3125 worktree ? got_worktree_get_head_ref_name(worktree) :
3126 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3127 if (error)
3128 goto done;
3129 head_ref_name = label;
3130 } else {
3131 error = got_ref_open(&ref, repo, start_commit, 0);
3132 if (error == NULL)
3133 head_ref_name = got_ref_get_name(ref);
3134 else if (error->code != GOT_ERR_NOT_REF)
3135 goto done;
3136 error = got_repo_match_object_id(&start_id, NULL,
3137 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3138 if (error)
3139 goto done;
3142 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3143 if (view == NULL) {
3144 error = got_error_from_errno("view_open");
3145 goto done;
3147 error = open_log_view(view, start_id, repo, head_ref_name,
3148 in_repo_path, log_branches);
3149 if (error)
3150 goto done;
3151 if (worktree) {
3152 /* Release work tree lock. */
3153 got_worktree_close(worktree);
3154 worktree = NULL;
3156 error = view_loop(view);
3157 done:
3158 free(in_repo_path);
3159 free(repo_path);
3160 free(cwd);
3161 free(start_id);
3162 free(label);
3163 if (ref)
3164 got_ref_close(ref);
3165 if (repo) {
3166 const struct got_error *close_err = got_repo_close(repo);
3167 if (error == NULL)
3168 error = close_err;
3170 if (worktree)
3171 got_worktree_close(worktree);
3172 if (pack_fds) {
3173 const struct got_error *pack_err =
3174 got_repo_pack_fds_close(pack_fds);
3175 if (error == NULL)
3176 error = pack_err;
3178 tog_free_refs();
3179 return error;
3182 __dead static void
3183 usage_diff(void)
3185 endwin();
3186 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3187 "[-w] object1 object2\n", getprogname());
3188 exit(1);
3191 static int
3192 match_line(const char *line, regex_t *regex, size_t nmatch,
3193 regmatch_t *regmatch)
3195 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3198 static struct tog_color *
3199 match_color(struct tog_colors *colors, const char *line)
3201 struct tog_color *tc = NULL;
3203 STAILQ_FOREACH(tc, colors, entry) {
3204 if (match_line(line, &tc->regex, 0, NULL))
3205 return tc;
3208 return NULL;
3211 static const struct got_error *
3212 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3213 WINDOW *window, int skipcol, regmatch_t *regmatch)
3215 const struct got_error *err = NULL;
3216 char *exstr = NULL;
3217 wchar_t *wline = NULL;
3218 int rme, rms, n, width, scrollx;
3219 int width0 = 0, width1 = 0, width2 = 0;
3220 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3222 *wtotal = 0;
3224 rms = regmatch->rm_so;
3225 rme = regmatch->rm_eo;
3227 err = expand_tab(&exstr, line);
3228 if (err)
3229 return err;
3231 /* Split the line into 3 segments, according to match offsets. */
3232 seg0 = strndup(exstr, rms);
3233 if (seg0 == NULL) {
3234 err = got_error_from_errno("strndup");
3235 goto done;
3237 seg1 = strndup(exstr + rms, rme - rms);
3238 if (seg1 == NULL) {
3239 err = got_error_from_errno("strndup");
3240 goto done;
3242 seg2 = strdup(exstr + rme);
3243 if (seg2 == NULL) {
3244 err = got_error_from_errno("strndup");
3245 goto done;
3248 /* draw up to matched token if we haven't scrolled past it */
3249 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3250 col_tab_align, 1);
3251 if (err)
3252 goto done;
3253 n = MAX(width0 - skipcol, 0);
3254 if (n) {
3255 free(wline);
3256 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3257 wlimit, col_tab_align, 1);
3258 if (err)
3259 goto done;
3260 waddwstr(window, &wline[scrollx]);
3261 wlimit -= width;
3262 *wtotal += width;
3265 if (wlimit > 0) {
3266 int i = 0, w = 0;
3267 size_t wlen;
3269 free(wline);
3270 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3271 col_tab_align, 1);
3272 if (err)
3273 goto done;
3274 wlen = wcslen(wline);
3275 while (i < wlen) {
3276 width = wcwidth(wline[i]);
3277 if (width == -1) {
3278 /* should not happen, tabs are expanded */
3279 err = got_error(GOT_ERR_RANGE);
3280 goto done;
3282 if (width0 + w + width > skipcol)
3283 break;
3284 w += width;
3285 i++;
3287 /* draw (visible part of) matched token (if scrolled into it) */
3288 if (width1 - w > 0) {
3289 wattron(window, A_STANDOUT);
3290 waddwstr(window, &wline[i]);
3291 wattroff(window, A_STANDOUT);
3292 wlimit -= (width1 - w);
3293 *wtotal += (width1 - w);
3297 if (wlimit > 0) { /* draw rest of line */
3298 free(wline);
3299 if (skipcol > width0 + width1) {
3300 err = format_line(&wline, &width2, &scrollx, seg2,
3301 skipcol - (width0 + width1), wlimit,
3302 col_tab_align, 1);
3303 if (err)
3304 goto done;
3305 waddwstr(window, &wline[scrollx]);
3306 } else {
3307 err = format_line(&wline, &width2, NULL, seg2, 0,
3308 wlimit, col_tab_align, 1);
3309 if (err)
3310 goto done;
3311 waddwstr(window, wline);
3313 *wtotal += width2;
3315 done:
3316 free(wline);
3317 free(exstr);
3318 free(seg0);
3319 free(seg1);
3320 free(seg2);
3321 return err;
3324 static const struct got_error *
3325 draw_file(struct tog_view *view, const char *header)
3327 struct tog_diff_view_state *s = &view->state.diff;
3328 regmatch_t *regmatch = &view->regmatch;
3329 const struct got_error *err;
3330 int nprinted = 0;
3331 char *line;
3332 size_t linesize = 0;
3333 ssize_t linelen;
3334 struct tog_color *tc;
3335 wchar_t *wline;
3336 int width;
3337 int max_lines = view->nlines;
3338 int nlines = s->nlines;
3339 off_t line_offset;
3341 line_offset = s->line_offsets[s->first_displayed_line - 1];
3342 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3343 return got_error_from_errno("fseek");
3345 werase(view->window);
3347 if (header) {
3348 if (asprintf(&line, "[%d/%d] %s",
3349 s->first_displayed_line - 1 + s->selected_line, nlines,
3350 header) == -1)
3351 return got_error_from_errno("asprintf");
3352 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3353 0, 0);
3354 free(line);
3355 if (err)
3356 return err;
3358 if (view_needs_focus_indication(view))
3359 wstandout(view->window);
3360 waddwstr(view->window, wline);
3361 free(wline);
3362 wline = NULL;
3363 if (view_needs_focus_indication(view))
3364 wstandend(view->window);
3365 if (width <= view->ncols - 1)
3366 waddch(view->window, '\n');
3368 if (max_lines <= 1)
3369 return NULL;
3370 max_lines--;
3373 s->eof = 0;
3374 view->maxx = 0;
3375 line = NULL;
3376 while (max_lines > 0 && nprinted < max_lines) {
3377 linelen = getline(&line, &linesize, s->f);
3378 if (linelen == -1) {
3379 if (feof(s->f)) {
3380 s->eof = 1;
3381 break;
3383 free(line);
3384 return got_ferror(s->f, GOT_ERR_IO);
3387 /* Set view->maxx based on full line length. */
3388 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3389 view->x ? 1 : 0);
3390 if (err) {
3391 free(line);
3392 return err;
3394 view->maxx = MAX(view->maxx, width);
3395 free(wline);
3396 wline = NULL;
3398 tc = match_color(&s->colors, line);
3399 if (tc)
3400 wattr_on(view->window,
3401 COLOR_PAIR(tc->colorpair), NULL);
3402 if (s->first_displayed_line + nprinted == s->matched_line &&
3403 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3404 err = add_matched_line(&width, line, view->ncols, 0,
3405 view->window, view->x, regmatch);
3406 if (err) {
3407 free(line);
3408 return err;
3410 } else {
3411 int skip;
3412 err = format_line(&wline, &width, &skip, line,
3413 view->x, view->ncols, 0, view->x ? 1 : 0);
3414 if (err) {
3415 free(line);
3416 return err;
3418 waddwstr(view->window, &wline[skip]);
3419 free(wline);
3420 wline = NULL;
3422 if (tc)
3423 wattr_off(view->window,
3424 COLOR_PAIR(tc->colorpair), NULL);
3425 if (width <= view->ncols - 1)
3426 waddch(view->window, '\n');
3427 nprinted++;
3429 free(line);
3430 if (nprinted >= 1)
3431 s->last_displayed_line = s->first_displayed_line +
3432 (nprinted - 1);
3433 else
3434 s->last_displayed_line = s->first_displayed_line;
3436 view_vborder(view);
3438 if (s->eof) {
3439 while (nprinted < view->nlines) {
3440 waddch(view->window, '\n');
3441 nprinted++;
3444 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3445 view->ncols, 0, 0);
3446 if (err) {
3447 return err;
3450 wstandout(view->window);
3451 waddwstr(view->window, wline);
3452 free(wline);
3453 wline = NULL;
3454 wstandend(view->window);
3457 return NULL;
3460 static char *
3461 get_datestr(time_t *time, char *datebuf)
3463 struct tm mytm, *tm;
3464 char *p, *s;
3466 tm = gmtime_r(time, &mytm);
3467 if (tm == NULL)
3468 return NULL;
3469 s = asctime_r(tm, datebuf);
3470 if (s == NULL)
3471 return NULL;
3472 p = strchr(s, '\n');
3473 if (p)
3474 *p = '\0';
3475 return s;
3478 static const struct got_error *
3479 get_changed_paths(struct got_pathlist_head *paths,
3480 struct got_commit_object *commit, struct got_repository *repo)
3482 const struct got_error *err = NULL;
3483 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3484 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3485 struct got_object_qid *qid;
3487 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3488 if (qid != NULL) {
3489 struct got_commit_object *pcommit;
3490 err = got_object_open_as_commit(&pcommit, repo,
3491 &qid->id);
3492 if (err)
3493 return err;
3495 tree_id1 = got_object_id_dup(
3496 got_object_commit_get_tree_id(pcommit));
3497 if (tree_id1 == NULL) {
3498 got_object_commit_close(pcommit);
3499 return got_error_from_errno("got_object_id_dup");
3501 got_object_commit_close(pcommit);
3505 if (tree_id1) {
3506 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3507 if (err)
3508 goto done;
3511 tree_id2 = got_object_commit_get_tree_id(commit);
3512 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3513 if (err)
3514 goto done;
3516 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3517 got_diff_tree_collect_changed_paths, paths, 0);
3518 done:
3519 if (tree1)
3520 got_object_tree_close(tree1);
3521 if (tree2)
3522 got_object_tree_close(tree2);
3523 free(tree_id1);
3524 return err;
3527 static const struct got_error *
3528 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3530 off_t *p;
3532 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3533 if (p == NULL)
3534 return got_error_from_errno("reallocarray");
3535 *line_offsets = p;
3536 (*line_offsets)[*nlines] = off;
3537 (*nlines)++;
3538 return NULL;
3541 static const struct got_error *
3542 write_commit_info(off_t **line_offsets, size_t *nlines,
3543 struct got_object_id *commit_id, struct got_reflist_head *refs,
3544 struct got_repository *repo, FILE *outfile)
3546 const struct got_error *err = NULL;
3547 char datebuf[26], *datestr;
3548 struct got_commit_object *commit;
3549 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3550 time_t committer_time;
3551 const char *author, *committer;
3552 char *refs_str = NULL;
3553 struct got_pathlist_head changed_paths;
3554 struct got_pathlist_entry *pe;
3555 off_t outoff = 0;
3556 int n;
3558 TAILQ_INIT(&changed_paths);
3560 if (refs) {
3561 err = build_refs_str(&refs_str, refs, commit_id, repo);
3562 if (err)
3563 return err;
3566 err = got_object_open_as_commit(&commit, repo, commit_id);
3567 if (err)
3568 return err;
3570 err = got_object_id_str(&id_str, commit_id);
3571 if (err) {
3572 err = got_error_from_errno("got_object_id_str");
3573 goto done;
3576 err = add_line_offset(line_offsets, nlines, 0);
3577 if (err)
3578 goto done;
3580 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3581 refs_str ? refs_str : "", refs_str ? ")" : "");
3582 if (n < 0) {
3583 err = got_error_from_errno("fprintf");
3584 goto done;
3586 outoff += n;
3587 err = add_line_offset(line_offsets, nlines, outoff);
3588 if (err)
3589 goto done;
3591 n = fprintf(outfile, "from: %s\n",
3592 got_object_commit_get_author(commit));
3593 if (n < 0) {
3594 err = got_error_from_errno("fprintf");
3595 goto done;
3597 outoff += n;
3598 err = add_line_offset(line_offsets, nlines, outoff);
3599 if (err)
3600 goto done;
3602 committer_time = got_object_commit_get_committer_time(commit);
3603 datestr = get_datestr(&committer_time, datebuf);
3604 if (datestr) {
3605 n = fprintf(outfile, "date: %s UTC\n", datestr);
3606 if (n < 0) {
3607 err = got_error_from_errno("fprintf");
3608 goto done;
3610 outoff += n;
3611 err = add_line_offset(line_offsets, nlines, outoff);
3612 if (err)
3613 goto done;
3615 author = got_object_commit_get_author(commit);
3616 committer = got_object_commit_get_committer(commit);
3617 if (strcmp(author, committer) != 0) {
3618 n = fprintf(outfile, "via: %s\n", committer);
3619 if (n < 0) {
3620 err = got_error_from_errno("fprintf");
3621 goto done;
3623 outoff += n;
3624 err = add_line_offset(line_offsets, nlines, outoff);
3625 if (err)
3626 goto done;
3628 if (got_object_commit_get_nparents(commit) > 1) {
3629 const struct got_object_id_queue *parent_ids;
3630 struct got_object_qid *qid;
3631 int pn = 1;
3632 parent_ids = got_object_commit_get_parent_ids(commit);
3633 STAILQ_FOREACH(qid, parent_ids, entry) {
3634 err = got_object_id_str(&id_str, &qid->id);
3635 if (err)
3636 goto done;
3637 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3638 if (n < 0) {
3639 err = got_error_from_errno("fprintf");
3640 goto done;
3642 outoff += n;
3643 err = add_line_offset(line_offsets, nlines, outoff);
3644 if (err)
3645 goto done;
3646 free(id_str);
3647 id_str = NULL;
3651 err = got_object_commit_get_logmsg(&logmsg, commit);
3652 if (err)
3653 goto done;
3654 s = logmsg;
3655 while ((line = strsep(&s, "\n")) != NULL) {
3656 n = fprintf(outfile, "%s\n", line);
3657 if (n < 0) {
3658 err = got_error_from_errno("fprintf");
3659 goto done;
3661 outoff += n;
3662 err = add_line_offset(line_offsets, nlines, outoff);
3663 if (err)
3664 goto done;
3667 err = get_changed_paths(&changed_paths, commit, repo);
3668 if (err)
3669 goto done;
3670 TAILQ_FOREACH(pe, &changed_paths, entry) {
3671 struct got_diff_changed_path *cp = pe->data;
3672 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3673 if (n < 0) {
3674 err = got_error_from_errno("fprintf");
3675 goto done;
3677 outoff += n;
3678 err = add_line_offset(line_offsets, nlines, outoff);
3679 if (err)
3680 goto done;
3681 free((char *)pe->path);
3682 free(pe->data);
3685 fputc('\n', outfile);
3686 outoff++;
3687 err = add_line_offset(line_offsets, nlines, outoff);
3688 done:
3689 got_pathlist_free(&changed_paths);
3690 free(id_str);
3691 free(logmsg);
3692 free(refs_str);
3693 got_object_commit_close(commit);
3694 if (err) {
3695 free(*line_offsets);
3696 *line_offsets = NULL;
3697 *nlines = 0;
3699 return err;
3702 static const struct got_error *
3703 create_diff(struct tog_diff_view_state *s)
3705 const struct got_error *err = NULL;
3706 FILE *f = NULL;
3707 int obj_type;
3709 free(s->line_offsets);
3710 s->line_offsets = malloc(sizeof(off_t));
3711 if (s->line_offsets == NULL)
3712 return got_error_from_errno("malloc");
3713 s->nlines = 0;
3715 f = got_opentemp();
3716 if (f == NULL) {
3717 err = got_error_from_errno("got_opentemp");
3718 goto done;
3720 if (s->f && fclose(s->f) == EOF) {
3721 err = got_error_from_errno("fclose");
3722 goto done;
3724 s->f = f;
3726 if (s->id1)
3727 err = got_object_get_type(&obj_type, s->repo, s->id1);
3728 else
3729 err = got_object_get_type(&obj_type, s->repo, s->id2);
3730 if (err)
3731 goto done;
3733 switch (obj_type) {
3734 case GOT_OBJ_TYPE_BLOB:
3735 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3736 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3737 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3738 s->repo, s->f);
3739 break;
3740 case GOT_OBJ_TYPE_TREE:
3741 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3742 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3743 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3744 break;
3745 case GOT_OBJ_TYPE_COMMIT: {
3746 const struct got_object_id_queue *parent_ids;
3747 struct got_object_qid *pid;
3748 struct got_commit_object *commit2;
3749 struct got_reflist_head *refs;
3751 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3752 if (err)
3753 goto done;
3754 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3755 /* Show commit info if we're diffing to a parent/root commit. */
3756 if (s->id1 == NULL) {
3757 err = write_commit_info(&s->line_offsets, &s->nlines,
3758 s->id2, refs, s->repo, s->f);
3759 if (err)
3760 goto done;
3761 } else {
3762 parent_ids = got_object_commit_get_parent_ids(commit2);
3763 STAILQ_FOREACH(pid, parent_ids, entry) {
3764 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3765 err = write_commit_info(
3766 &s->line_offsets, &s->nlines,
3767 s->id2, refs, s->repo, s->f);
3768 if (err)
3769 goto done;
3770 break;
3774 got_object_commit_close(commit2);
3776 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3777 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3778 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3779 break;
3781 default:
3782 err = got_error(GOT_ERR_OBJ_TYPE);
3783 break;
3785 if (err)
3786 goto done;
3787 done:
3788 if (s->f && fflush(s->f) != 0 && err == NULL)
3789 err = got_error_from_errno("fflush");
3790 return err;
3793 static void
3794 diff_view_indicate_progress(struct tog_view *view)
3796 mvwaddstr(view->window, 0, 0, "diffing...");
3797 update_panels();
3798 doupdate();
3801 static const struct got_error *
3802 search_start_diff_view(struct tog_view *view)
3804 struct tog_diff_view_state *s = &view->state.diff;
3806 s->matched_line = 0;
3807 return NULL;
3810 static const struct got_error *
3811 search_next_diff_view(struct tog_view *view)
3813 struct tog_diff_view_state *s = &view->state.diff;
3814 const struct got_error *err = NULL;
3815 int lineno;
3816 char *line = NULL;
3817 size_t linesize = 0;
3818 ssize_t linelen;
3820 if (!view->searching) {
3821 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3822 return NULL;
3825 if (s->matched_line) {
3826 if (view->searching == TOG_SEARCH_FORWARD)
3827 lineno = s->matched_line + 1;
3828 else
3829 lineno = s->matched_line - 1;
3830 } else
3831 lineno = s->first_displayed_line;
3833 while (1) {
3834 off_t offset;
3836 if (lineno <= 0 || lineno > s->nlines) {
3837 if (s->matched_line == 0) {
3838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3839 break;
3842 if (view->searching == TOG_SEARCH_FORWARD)
3843 lineno = 1;
3844 else
3845 lineno = s->nlines;
3848 offset = s->line_offsets[lineno - 1];
3849 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3850 free(line);
3851 return got_error_from_errno("fseeko");
3853 linelen = getline(&line, &linesize, s->f);
3854 if (linelen != -1) {
3855 char *exstr;
3856 err = expand_tab(&exstr, line);
3857 if (err)
3858 break;
3859 if (match_line(exstr, &view->regex, 1,
3860 &view->regmatch)) {
3861 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3862 s->matched_line = lineno;
3863 free(exstr);
3864 break;
3866 free(exstr);
3868 if (view->searching == TOG_SEARCH_FORWARD)
3869 lineno++;
3870 else
3871 lineno--;
3873 free(line);
3875 if (s->matched_line) {
3876 s->first_displayed_line = s->matched_line;
3877 s->selected_line = 1;
3880 return err;
3883 static const struct got_error *
3884 close_diff_view(struct tog_view *view)
3886 const struct got_error *err = NULL;
3887 struct tog_diff_view_state *s = &view->state.diff;
3889 free(s->id1);
3890 s->id1 = NULL;
3891 free(s->id2);
3892 s->id2 = NULL;
3893 if (s->f && fclose(s->f) == EOF)
3894 err = got_error_from_errno("fclose");
3895 s->f = NULL;
3896 if (s->f1 && fclose(s->f1) == EOF)
3897 err = got_error_from_errno("fclose");
3898 s->f1 = NULL;
3899 if (s->f2 && fclose(s->f2) == EOF)
3900 err = got_error_from_errno("fclose");
3901 s->f2 = NULL;
3902 free_colors(&s->colors);
3903 free(s->line_offsets);
3904 s->line_offsets = NULL;
3905 s->nlines = 0;
3906 return err;
3909 static const struct got_error *
3910 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3911 struct got_object_id *id2, const char *label1, const char *label2,
3912 int diff_context, int ignore_whitespace, int force_text_diff,
3913 struct tog_view *log_view, struct got_repository *repo)
3915 const struct got_error *err;
3916 struct tog_diff_view_state *s = &view->state.diff;
3918 memset(s, 0, sizeof(*s));
3920 if (id1 != NULL && id2 != NULL) {
3921 int type1, type2;
3922 err = got_object_get_type(&type1, repo, id1);
3923 if (err)
3924 return err;
3925 err = got_object_get_type(&type2, repo, id2);
3926 if (err)
3927 return err;
3929 if (type1 != type2)
3930 return got_error(GOT_ERR_OBJ_TYPE);
3932 s->first_displayed_line = 1;
3933 s->last_displayed_line = view->nlines;
3934 s->selected_line = 1;
3935 s->repo = repo;
3936 s->id1 = id1;
3937 s->id2 = id2;
3938 s->label1 = label1;
3939 s->label2 = label2;
3941 if (id1) {
3942 s->id1 = got_object_id_dup(id1);
3943 if (s->id1 == NULL)
3944 return got_error_from_errno("got_object_id_dup");
3945 } else
3946 s->id1 = NULL;
3948 s->id2 = got_object_id_dup(id2);
3949 if (s->id2 == NULL) {
3950 err = got_error_from_errno("got_object_id_dup");
3951 goto done;
3954 s->f1 = got_opentemp();
3955 if (s->f1 == NULL) {
3956 err = got_error_from_errno("got_opentemp");
3957 goto done;
3960 s->f2 = got_opentemp();
3961 if (s->f2 == NULL) {
3962 err = got_error_from_errno("got_opentemp");
3963 goto done;
3966 s->first_displayed_line = 1;
3967 s->last_displayed_line = view->nlines;
3968 s->diff_context = diff_context;
3969 s->ignore_whitespace = ignore_whitespace;
3970 s->force_text_diff = force_text_diff;
3971 s->log_view = log_view;
3972 s->repo = repo;
3974 STAILQ_INIT(&s->colors);
3975 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3976 err = add_color(&s->colors,
3977 "^-", TOG_COLOR_DIFF_MINUS,
3978 get_color_value("TOG_COLOR_DIFF_MINUS"));
3979 if (err)
3980 goto done;
3981 err = add_color(&s->colors, "^\\+",
3982 TOG_COLOR_DIFF_PLUS,
3983 get_color_value("TOG_COLOR_DIFF_PLUS"));
3984 if (err)
3985 goto done;
3986 err = add_color(&s->colors,
3987 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3988 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3989 if (err)
3990 goto done;
3992 err = add_color(&s->colors,
3993 "^(commit [0-9a-f]|parent [0-9]|"
3994 "(blob|file|tree|commit) [-+] |"
3995 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3996 get_color_value("TOG_COLOR_DIFF_META"));
3997 if (err)
3998 goto done;
4000 err = add_color(&s->colors,
4001 "^(from|via): ", TOG_COLOR_AUTHOR,
4002 get_color_value("TOG_COLOR_AUTHOR"));
4003 if (err)
4004 goto done;
4006 err = add_color(&s->colors,
4007 "^date: ", TOG_COLOR_DATE,
4008 get_color_value("TOG_COLOR_DATE"));
4009 if (err)
4010 goto done;
4013 if (log_view && view_is_splitscreen(view))
4014 show_log_view(log_view); /* draw vborder */
4015 diff_view_indicate_progress(view);
4017 err = create_diff(s);
4019 view->show = show_diff_view;
4020 view->input = input_diff_view;
4021 view->close = close_diff_view;
4022 view->search_start = search_start_diff_view;
4023 view->search_next = search_next_diff_view;
4024 done:
4025 if (err)
4026 close_diff_view(view);
4027 return err;
4030 static const struct got_error *
4031 show_diff_view(struct tog_view *view)
4033 const struct got_error *err;
4034 struct tog_diff_view_state *s = &view->state.diff;
4035 char *id_str1 = NULL, *id_str2, *header;
4036 const char *label1, *label2;
4038 if (s->id1) {
4039 err = got_object_id_str(&id_str1, s->id1);
4040 if (err)
4041 return err;
4042 label1 = s->label1 ? : id_str1;
4043 } else
4044 label1 = "/dev/null";
4046 err = got_object_id_str(&id_str2, s->id2);
4047 if (err)
4048 return err;
4049 label2 = s->label2 ? : id_str2;
4051 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4052 err = got_error_from_errno("asprintf");
4053 free(id_str1);
4054 free(id_str2);
4055 return err;
4057 free(id_str1);
4058 free(id_str2);
4060 err = draw_file(view, header);
4061 free(header);
4062 return err;
4065 static const struct got_error *
4066 set_selected_commit(struct tog_diff_view_state *s,
4067 struct commit_queue_entry *entry)
4069 const struct got_error *err;
4070 const struct got_object_id_queue *parent_ids;
4071 struct got_commit_object *selected_commit;
4072 struct got_object_qid *pid;
4074 free(s->id2);
4075 s->id2 = got_object_id_dup(entry->id);
4076 if (s->id2 == NULL)
4077 return got_error_from_errno("got_object_id_dup");
4079 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4080 if (err)
4081 return err;
4082 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4083 free(s->id1);
4084 pid = STAILQ_FIRST(parent_ids);
4085 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4086 got_object_commit_close(selected_commit);
4087 return NULL;
4090 static const struct got_error *
4091 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4093 const struct got_error *err = NULL;
4094 struct tog_diff_view_state *s = &view->state.diff;
4095 struct tog_log_view_state *ls;
4096 struct commit_queue_entry *old_selected_entry;
4097 char *line = NULL;
4098 size_t linesize = 0;
4099 ssize_t linelen;
4100 int i, nscroll = view->nlines - 1;
4102 switch (ch) {
4103 case '0':
4104 view->x = 0;
4105 break;
4106 case '$':
4107 view->x = MAX(view->maxx - view->ncols / 3, 0);
4108 view->count = 0;
4109 break;
4110 case KEY_RIGHT:
4111 case 'l':
4112 if (view->x + view->ncols / 3 < view->maxx)
4113 view->x += 2; /* move two columns right */
4114 else
4115 view->count = 0;
4116 break;
4117 case KEY_LEFT:
4118 case 'h':
4119 view->x -= MIN(view->x, 2); /* move two columns back */
4120 if (view->x <= 0)
4121 view->count = 0;
4122 break;
4123 case 'a':
4124 case 'w':
4125 if (ch == 'a')
4126 s->force_text_diff = !s->force_text_diff;
4127 if (ch == 'w')
4128 s->ignore_whitespace = !s->ignore_whitespace;
4129 wclear(view->window);
4130 s->first_displayed_line = 1;
4131 s->last_displayed_line = view->nlines;
4132 s->matched_line = 0;
4133 diff_view_indicate_progress(view);
4134 err = create_diff(s);
4135 view->count = 0;
4136 break;
4137 case 'g':
4138 case KEY_HOME:
4139 s->first_displayed_line = 1;
4140 view->count = 0;
4141 break;
4142 case 'G':
4143 case KEY_END:
4144 view->count = 0;
4145 if (s->eof)
4146 break;
4148 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4149 s->eof = 1;
4150 break;
4151 case 'k':
4152 case KEY_UP:
4153 case CTRL('p'):
4154 if (s->first_displayed_line > 1)
4155 s->first_displayed_line--;
4156 else
4157 view->count = 0;
4158 break;
4159 case CTRL('u'):
4160 case 'u':
4161 nscroll /= 2;
4162 /* FALL THROUGH */
4163 case KEY_PPAGE:
4164 case CTRL('b'):
4165 case 'b':
4166 if (s->first_displayed_line == 1) {
4167 view->count = 0;
4168 break;
4170 i = 0;
4171 while (i++ < nscroll && s->first_displayed_line > 1)
4172 s->first_displayed_line--;
4173 break;
4174 case 'j':
4175 case KEY_DOWN:
4176 case CTRL('n'):
4177 if (!s->eof)
4178 s->first_displayed_line++;
4179 else
4180 view->count = 0;
4181 break;
4182 case CTRL('d'):
4183 case 'd':
4184 nscroll /= 2;
4185 /* FALL THROUGH */
4186 case KEY_NPAGE:
4187 case CTRL('f'):
4188 case 'f':
4189 case ' ':
4190 if (s->eof) {
4191 view->count = 0;
4192 break;
4194 i = 0;
4195 while (!s->eof && i++ < nscroll) {
4196 linelen = getline(&line, &linesize, s->f);
4197 s->first_displayed_line++;
4198 if (linelen == -1) {
4199 if (feof(s->f)) {
4200 s->eof = 1;
4201 } else
4202 err = got_ferror(s->f, GOT_ERR_IO);
4203 break;
4206 free(line);
4207 break;
4208 case '[':
4209 if (s->diff_context > 0) {
4210 s->diff_context--;
4211 s->matched_line = 0;
4212 diff_view_indicate_progress(view);
4213 err = create_diff(s);
4214 if (s->first_displayed_line + view->nlines - 1 >
4215 s->nlines) {
4216 s->first_displayed_line = 1;
4217 s->last_displayed_line = view->nlines;
4219 } else
4220 view->count = 0;
4221 break;
4222 case ']':
4223 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4224 s->diff_context++;
4225 s->matched_line = 0;
4226 diff_view_indicate_progress(view);
4227 err = create_diff(s);
4228 } else
4229 view->count = 0;
4230 break;
4231 case '<':
4232 case ',':
4233 if (s->log_view == NULL) {
4234 view->count = 0;
4235 break;
4237 ls = &s->log_view->state.log;
4238 old_selected_entry = ls->selected_entry;
4240 /* view->count handled in input_log_view() */
4241 err = input_log_view(NULL, s->log_view, KEY_UP);
4242 if (err)
4243 break;
4245 if (old_selected_entry == ls->selected_entry)
4246 break;
4248 err = set_selected_commit(s, ls->selected_entry);
4249 if (err)
4250 break;
4252 s->first_displayed_line = 1;
4253 s->last_displayed_line = view->nlines;
4254 s->matched_line = 0;
4255 view->x = 0;
4257 diff_view_indicate_progress(view);
4258 err = create_diff(s);
4259 break;
4260 case '>':
4261 case '.':
4262 if (s->log_view == NULL) {
4263 view->count = 0;
4264 break;
4266 ls = &s->log_view->state.log;
4267 old_selected_entry = ls->selected_entry;
4269 /* view->count handled in input_log_view() */
4270 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4271 if (err)
4272 break;
4274 if (old_selected_entry == ls->selected_entry)
4275 break;
4277 err = set_selected_commit(s, ls->selected_entry);
4278 if (err)
4279 break;
4281 s->first_displayed_line = 1;
4282 s->last_displayed_line = view->nlines;
4283 s->matched_line = 0;
4284 view->x = 0;
4286 diff_view_indicate_progress(view);
4287 err = create_diff(s);
4288 break;
4289 default:
4290 view->count = 0;
4291 break;
4294 return err;
4297 static const struct got_error *
4298 cmd_diff(int argc, char *argv[])
4300 const struct got_error *error = NULL;
4301 struct got_repository *repo = NULL;
4302 struct got_worktree *worktree = NULL;
4303 struct got_object_id *id1 = NULL, *id2 = NULL;
4304 char *repo_path = NULL, *cwd = NULL;
4305 char *id_str1 = NULL, *id_str2 = NULL;
4306 char *label1 = NULL, *label2 = NULL;
4307 int diff_context = 3, ignore_whitespace = 0;
4308 int ch, force_text_diff = 0;
4309 const char *errstr;
4310 struct tog_view *view;
4311 int *pack_fds = NULL;
4313 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4314 switch (ch) {
4315 case 'a':
4316 force_text_diff = 1;
4317 break;
4318 case 'C':
4319 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4320 &errstr);
4321 if (errstr != NULL)
4322 errx(1, "number of context lines is %s: %s",
4323 errstr, errstr);
4324 break;
4325 case 'r':
4326 repo_path = realpath(optarg, NULL);
4327 if (repo_path == NULL)
4328 return got_error_from_errno2("realpath",
4329 optarg);
4330 got_path_strip_trailing_slashes(repo_path);
4331 break;
4332 case 'w':
4333 ignore_whitespace = 1;
4334 break;
4335 default:
4336 usage_diff();
4337 /* NOTREACHED */
4341 argc -= optind;
4342 argv += optind;
4344 if (argc == 0) {
4345 usage_diff(); /* TODO show local worktree changes */
4346 } else if (argc == 2) {
4347 id_str1 = argv[0];
4348 id_str2 = argv[1];
4349 } else
4350 usage_diff();
4352 error = got_repo_pack_fds_open(&pack_fds);
4353 if (error)
4354 goto done;
4356 if (repo_path == NULL) {
4357 cwd = getcwd(NULL, 0);
4358 if (cwd == NULL)
4359 return got_error_from_errno("getcwd");
4360 error = got_worktree_open(&worktree, cwd);
4361 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4362 goto done;
4363 if (worktree)
4364 repo_path =
4365 strdup(got_worktree_get_repo_path(worktree));
4366 else
4367 repo_path = strdup(cwd);
4368 if (repo_path == NULL) {
4369 error = got_error_from_errno("strdup");
4370 goto done;
4374 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4375 if (error)
4376 goto done;
4378 init_curses();
4380 error = apply_unveil(got_repo_get_path(repo), NULL);
4381 if (error)
4382 goto done;
4384 error = tog_load_refs(repo, 0);
4385 if (error)
4386 goto done;
4388 error = got_repo_match_object_id(&id1, &label1, id_str1,
4389 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4390 if (error)
4391 goto done;
4393 error = got_repo_match_object_id(&id2, &label2, id_str2,
4394 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4395 if (error)
4396 goto done;
4398 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4399 if (view == NULL) {
4400 error = got_error_from_errno("view_open");
4401 goto done;
4403 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4404 ignore_whitespace, force_text_diff, NULL, repo);
4405 if (error)
4406 goto done;
4407 error = view_loop(view);
4408 done:
4409 free(label1);
4410 free(label2);
4411 free(repo_path);
4412 free(cwd);
4413 if (repo) {
4414 const struct got_error *close_err = got_repo_close(repo);
4415 if (error == NULL)
4416 error = close_err;
4418 if (worktree)
4419 got_worktree_close(worktree);
4420 if (pack_fds) {
4421 const struct got_error *pack_err =
4422 got_repo_pack_fds_close(pack_fds);
4423 if (error == NULL)
4424 error = pack_err;
4426 tog_free_refs();
4427 return error;
4430 __dead static void
4431 usage_blame(void)
4433 endwin();
4434 fprintf(stderr,
4435 "usage: %s blame [-c commit] [-r repository-path] path\n",
4436 getprogname());
4437 exit(1);
4440 struct tog_blame_line {
4441 int annotated;
4442 struct got_object_id *id;
4445 static const struct got_error *
4446 draw_blame(struct tog_view *view)
4448 struct tog_blame_view_state *s = &view->state.blame;
4449 struct tog_blame *blame = &s->blame;
4450 regmatch_t *regmatch = &view->regmatch;
4451 const struct got_error *err;
4452 int lineno = 0, nprinted = 0;
4453 char *line = NULL;
4454 size_t linesize = 0;
4455 ssize_t linelen;
4456 wchar_t *wline;
4457 int width;
4458 struct tog_blame_line *blame_line;
4459 struct got_object_id *prev_id = NULL;
4460 char *id_str;
4461 struct tog_color *tc;
4463 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4464 if (err)
4465 return err;
4467 rewind(blame->f);
4468 werase(view->window);
4470 if (asprintf(&line, "commit %s", id_str) == -1) {
4471 err = got_error_from_errno("asprintf");
4472 free(id_str);
4473 return err;
4476 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4477 free(line);
4478 line = NULL;
4479 if (err)
4480 return err;
4481 if (view_needs_focus_indication(view))
4482 wstandout(view->window);
4483 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4484 if (tc)
4485 wattr_on(view->window,
4486 COLOR_PAIR(tc->colorpair), NULL);
4487 waddwstr(view->window, wline);
4488 if (tc)
4489 wattr_off(view->window,
4490 COLOR_PAIR(tc->colorpair), NULL);
4491 if (view_needs_focus_indication(view))
4492 wstandend(view->window);
4493 free(wline);
4494 wline = NULL;
4495 if (width < view->ncols - 1)
4496 waddch(view->window, '\n');
4498 if (asprintf(&line, "[%d/%d] %s%s",
4499 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4500 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4501 free(id_str);
4502 return got_error_from_errno("asprintf");
4504 free(id_str);
4505 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4506 free(line);
4507 line = NULL;
4508 if (err)
4509 return err;
4510 waddwstr(view->window, wline);
4511 free(wline);
4512 wline = NULL;
4513 if (width < view->ncols - 1)
4514 waddch(view->window, '\n');
4516 s->eof = 0;
4517 view->maxx = 0;
4518 while (nprinted < view->nlines - 2) {
4519 linelen = getline(&line, &linesize, blame->f);
4520 if (linelen == -1) {
4521 if (feof(blame->f)) {
4522 s->eof = 1;
4523 break;
4525 free(line);
4526 return got_ferror(blame->f, GOT_ERR_IO);
4528 if (++lineno < s->first_displayed_line)
4529 continue;
4531 /* Set view->maxx based on full line length. */
4532 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4533 if (err) {
4534 free(line);
4535 return err;
4537 free(wline);
4538 wline = NULL;
4539 view->maxx = MAX(view->maxx, width);
4541 if (view->focussed && nprinted == s->selected_line - 1)
4542 wstandout(view->window);
4544 if (blame->nlines > 0) {
4545 blame_line = &blame->lines[lineno - 1];
4546 if (blame_line->annotated && prev_id &&
4547 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4548 !(view->focussed &&
4549 nprinted == s->selected_line - 1)) {
4550 waddstr(view->window, " ");
4551 } else if (blame_line->annotated) {
4552 char *id_str;
4553 err = got_object_id_str(&id_str,
4554 blame_line->id);
4555 if (err) {
4556 free(line);
4557 return err;
4559 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4560 if (tc)
4561 wattr_on(view->window,
4562 COLOR_PAIR(tc->colorpair), NULL);
4563 wprintw(view->window, "%.8s", id_str);
4564 if (tc)
4565 wattr_off(view->window,
4566 COLOR_PAIR(tc->colorpair), NULL);
4567 free(id_str);
4568 prev_id = blame_line->id;
4569 } else {
4570 waddstr(view->window, "........");
4571 prev_id = NULL;
4573 } else {
4574 waddstr(view->window, "........");
4575 prev_id = NULL;
4578 if (view->focussed && nprinted == s->selected_line - 1)
4579 wstandend(view->window);
4580 waddstr(view->window, " ");
4582 if (view->ncols <= 9) {
4583 width = 9;
4584 } else if (s->first_displayed_line + nprinted ==
4585 s->matched_line &&
4586 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4587 err = add_matched_line(&width, line, view->ncols - 9, 9,
4588 view->window, view->x, regmatch);
4589 if (err) {
4590 free(line);
4591 return err;
4593 width += 9;
4594 } else {
4595 int skip;
4596 err = format_line(&wline, &width, &skip, line,
4597 view->x, view->ncols - 9, 9, 1);
4598 if (err) {
4599 free(line);
4600 return err;
4602 waddwstr(view->window, &wline[skip]);
4603 width += 9;
4604 free(wline);
4605 wline = NULL;
4608 if (width <= view->ncols - 1)
4609 waddch(view->window, '\n');
4610 if (++nprinted == 1)
4611 s->first_displayed_line = lineno;
4613 free(line);
4614 s->last_displayed_line = lineno;
4616 view_vborder(view);
4618 return NULL;
4621 static const struct got_error *
4622 blame_cb(void *arg, int nlines, int lineno,
4623 struct got_commit_object *commit, struct got_object_id *id)
4625 const struct got_error *err = NULL;
4626 struct tog_blame_cb_args *a = arg;
4627 struct tog_blame_line *line;
4628 int errcode;
4630 if (nlines != a->nlines ||
4631 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4632 return got_error(GOT_ERR_RANGE);
4634 errcode = pthread_mutex_lock(&tog_mutex);
4635 if (errcode)
4636 return got_error_set_errno(errcode, "pthread_mutex_lock");
4638 if (*a->quit) { /* user has quit the blame view */
4639 err = got_error(GOT_ERR_ITER_COMPLETED);
4640 goto done;
4643 if (lineno == -1)
4644 goto done; /* no change in this commit */
4646 line = &a->lines[lineno - 1];
4647 if (line->annotated)
4648 goto done;
4650 line->id = got_object_id_dup(id);
4651 if (line->id == NULL) {
4652 err = got_error_from_errno("got_object_id_dup");
4653 goto done;
4655 line->annotated = 1;
4656 done:
4657 errcode = pthread_mutex_unlock(&tog_mutex);
4658 if (errcode)
4659 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4660 return err;
4663 static void *
4664 blame_thread(void *arg)
4666 const struct got_error *err, *close_err;
4667 struct tog_blame_thread_args *ta = arg;
4668 struct tog_blame_cb_args *a = ta->cb_args;
4669 int errcode;
4671 err = block_signals_used_by_main_thread();
4672 if (err)
4673 return (void *)err;
4675 err = got_blame(ta->path, a->commit_id, ta->repo,
4676 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4677 if (err && err->code == GOT_ERR_CANCELLED)
4678 err = NULL;
4680 errcode = pthread_mutex_lock(&tog_mutex);
4681 if (errcode)
4682 return (void *)got_error_set_errno(errcode,
4683 "pthread_mutex_lock");
4685 close_err = got_repo_close(ta->repo);
4686 if (err == NULL)
4687 err = close_err;
4688 ta->repo = NULL;
4689 *ta->complete = 1;
4691 errcode = pthread_mutex_unlock(&tog_mutex);
4692 if (errcode && err == NULL)
4693 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4695 return (void *)err;
4698 static struct got_object_id *
4699 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4700 int first_displayed_line, int selected_line)
4702 struct tog_blame_line *line;
4704 if (nlines <= 0)
4705 return NULL;
4707 line = &lines[first_displayed_line - 1 + selected_line - 1];
4708 if (!line->annotated)
4709 return NULL;
4711 return line->id;
4714 static const struct got_error *
4715 stop_blame(struct tog_blame *blame)
4717 const struct got_error *err = NULL;
4718 int i;
4720 if (blame->thread) {
4721 int errcode;
4722 errcode = pthread_mutex_unlock(&tog_mutex);
4723 if (errcode)
4724 return got_error_set_errno(errcode,
4725 "pthread_mutex_unlock");
4726 errcode = pthread_join(blame->thread, (void **)&err);
4727 if (errcode)
4728 return got_error_set_errno(errcode, "pthread_join");
4729 errcode = pthread_mutex_lock(&tog_mutex);
4730 if (errcode)
4731 return got_error_set_errno(errcode,
4732 "pthread_mutex_lock");
4733 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4734 err = NULL;
4735 blame->thread = 0; //NULL;
4737 if (blame->thread_args.repo) {
4738 const struct got_error *close_err;
4739 close_err = got_repo_close(blame->thread_args.repo);
4740 if (err == NULL)
4741 err = close_err;
4742 blame->thread_args.repo = NULL;
4744 if (blame->f) {
4745 if (fclose(blame->f) == EOF && err == NULL)
4746 err = got_error_from_errno("fclose");
4747 blame->f = NULL;
4749 if (blame->lines) {
4750 for (i = 0; i < blame->nlines; i++)
4751 free(blame->lines[i].id);
4752 free(blame->lines);
4753 blame->lines = NULL;
4755 free(blame->cb_args.commit_id);
4756 blame->cb_args.commit_id = NULL;
4757 if (blame->pack_fds) {
4758 const struct got_error *pack_err =
4759 got_repo_pack_fds_close(blame->pack_fds);
4760 if (err == NULL)
4761 err = pack_err;
4762 blame->pack_fds = NULL;
4764 return err;
4767 static const struct got_error *
4768 cancel_blame_view(void *arg)
4770 const struct got_error *err = NULL;
4771 int *done = arg;
4772 int errcode;
4774 errcode = pthread_mutex_lock(&tog_mutex);
4775 if (errcode)
4776 return got_error_set_errno(errcode,
4777 "pthread_mutex_unlock");
4779 if (*done)
4780 err = got_error(GOT_ERR_CANCELLED);
4782 errcode = pthread_mutex_unlock(&tog_mutex);
4783 if (errcode)
4784 return got_error_set_errno(errcode,
4785 "pthread_mutex_lock");
4787 return err;
4790 static const struct got_error *
4791 run_blame(struct tog_view *view)
4793 struct tog_blame_view_state *s = &view->state.blame;
4794 struct tog_blame *blame = &s->blame;
4795 const struct got_error *err = NULL;
4796 struct got_commit_object *commit = NULL;
4797 struct got_blob_object *blob = NULL;
4798 struct got_repository *thread_repo = NULL;
4799 struct got_object_id *obj_id = NULL;
4800 int obj_type, fd = -1;
4801 int *pack_fds = NULL;
4803 err = got_object_open_as_commit(&commit, s->repo,
4804 &s->blamed_commit->id);
4805 if (err)
4806 return err;
4808 fd = got_opentempfd();
4809 if (fd == -1) {
4810 err = got_error_from_errno("got_opentempfd");
4811 goto done;
4814 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4815 if (err)
4816 goto done;
4818 err = got_object_get_type(&obj_type, s->repo, obj_id);
4819 if (err)
4820 goto done;
4822 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4823 err = got_error(GOT_ERR_OBJ_TYPE);
4824 goto done;
4827 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
4828 if (err)
4829 goto done;
4830 blame->f = got_opentemp();
4831 if (blame->f == NULL) {
4832 err = got_error_from_errno("got_opentemp");
4833 goto done;
4835 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4836 &blame->line_offsets, blame->f, blob);
4837 if (err)
4838 goto done;
4839 if (blame->nlines == 0) {
4840 s->blame_complete = 1;
4841 goto done;
4844 /* Don't include \n at EOF in the blame line count. */
4845 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4846 blame->nlines--;
4848 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4849 if (blame->lines == NULL) {
4850 err = got_error_from_errno("calloc");
4851 goto done;
4854 err = got_repo_pack_fds_open(&pack_fds);
4855 if (err)
4856 goto done;
4857 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4858 pack_fds);
4859 if (err)
4860 goto done;
4862 blame->pack_fds = pack_fds;
4863 blame->cb_args.view = view;
4864 blame->cb_args.lines = blame->lines;
4865 blame->cb_args.nlines = blame->nlines;
4866 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4867 if (blame->cb_args.commit_id == NULL) {
4868 err = got_error_from_errno("got_object_id_dup");
4869 goto done;
4871 blame->cb_args.quit = &s->done;
4873 blame->thread_args.path = s->path;
4874 blame->thread_args.repo = thread_repo;
4875 blame->thread_args.cb_args = &blame->cb_args;
4876 blame->thread_args.complete = &s->blame_complete;
4877 blame->thread_args.cancel_cb = cancel_blame_view;
4878 blame->thread_args.cancel_arg = &s->done;
4879 s->blame_complete = 0;
4881 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4882 s->first_displayed_line = 1;
4883 s->last_displayed_line = view->nlines;
4884 s->selected_line = 1;
4886 s->matched_line = 0;
4888 done:
4889 if (commit)
4890 got_object_commit_close(commit);
4891 if (fd != -1 && close(fd) == -1 && err == NULL)
4892 err = got_error_from_errno("close");
4893 if (blob)
4894 got_object_blob_close(blob);
4895 free(obj_id);
4896 if (err)
4897 stop_blame(blame);
4898 return err;
4901 static const struct got_error *
4902 open_blame_view(struct tog_view *view, char *path,
4903 struct got_object_id *commit_id, struct got_repository *repo)
4905 const struct got_error *err = NULL;
4906 struct tog_blame_view_state *s = &view->state.blame;
4908 STAILQ_INIT(&s->blamed_commits);
4910 s->path = strdup(path);
4911 if (s->path == NULL)
4912 return got_error_from_errno("strdup");
4914 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4915 if (err) {
4916 free(s->path);
4917 return err;
4920 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4921 s->first_displayed_line = 1;
4922 s->last_displayed_line = view->nlines;
4923 s->selected_line = 1;
4924 s->blame_complete = 0;
4925 s->repo = repo;
4926 s->commit_id = commit_id;
4927 memset(&s->blame, 0, sizeof(s->blame));
4929 STAILQ_INIT(&s->colors);
4930 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4931 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4932 get_color_value("TOG_COLOR_COMMIT"));
4933 if (err)
4934 return err;
4937 view->show = show_blame_view;
4938 view->input = input_blame_view;
4939 view->close = close_blame_view;
4940 view->search_start = search_start_blame_view;
4941 view->search_next = search_next_blame_view;
4943 return run_blame(view);
4946 static const struct got_error *
4947 close_blame_view(struct tog_view *view)
4949 const struct got_error *err = NULL;
4950 struct tog_blame_view_state *s = &view->state.blame;
4952 if (s->blame.thread)
4953 err = stop_blame(&s->blame);
4955 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4956 struct got_object_qid *blamed_commit;
4957 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4958 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4959 got_object_qid_free(blamed_commit);
4962 free(s->path);
4963 free_colors(&s->colors);
4964 return err;
4967 static const struct got_error *
4968 search_start_blame_view(struct tog_view *view)
4970 struct tog_blame_view_state *s = &view->state.blame;
4972 s->matched_line = 0;
4973 return NULL;
4976 static const struct got_error *
4977 search_next_blame_view(struct tog_view *view)
4979 struct tog_blame_view_state *s = &view->state.blame;
4980 const struct got_error *err = NULL;
4981 int lineno;
4982 char *line = NULL;
4983 size_t linesize = 0;
4984 ssize_t linelen;
4986 if (!view->searching) {
4987 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4988 return NULL;
4991 if (s->matched_line) {
4992 if (view->searching == TOG_SEARCH_FORWARD)
4993 lineno = s->matched_line + 1;
4994 else
4995 lineno = s->matched_line - 1;
4996 } else
4997 lineno = s->first_displayed_line - 1 + s->selected_line;
4999 while (1) {
5000 off_t offset;
5002 if (lineno <= 0 || lineno > s->blame.nlines) {
5003 if (s->matched_line == 0) {
5004 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5005 break;
5008 if (view->searching == TOG_SEARCH_FORWARD)
5009 lineno = 1;
5010 else
5011 lineno = s->blame.nlines;
5014 offset = s->blame.line_offsets[lineno - 1];
5015 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5016 free(line);
5017 return got_error_from_errno("fseeko");
5019 linelen = getline(&line, &linesize, s->blame.f);
5020 if (linelen != -1) {
5021 char *exstr;
5022 err = expand_tab(&exstr, line);
5023 if (err)
5024 break;
5025 if (match_line(exstr, &view->regex, 1,
5026 &view->regmatch)) {
5027 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5028 s->matched_line = lineno;
5029 free(exstr);
5030 break;
5032 free(exstr);
5034 if (view->searching == TOG_SEARCH_FORWARD)
5035 lineno++;
5036 else
5037 lineno--;
5039 free(line);
5041 if (s->matched_line) {
5042 s->first_displayed_line = s->matched_line;
5043 s->selected_line = 1;
5046 return err;
5049 static const struct got_error *
5050 show_blame_view(struct tog_view *view)
5052 const struct got_error *err = NULL;
5053 struct tog_blame_view_state *s = &view->state.blame;
5054 int errcode;
5056 if (s->blame.thread == 0 && !s->blame_complete) {
5057 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5058 &s->blame.thread_args);
5059 if (errcode)
5060 return got_error_set_errno(errcode, "pthread_create");
5062 halfdelay(1); /* fast refresh while annotating */
5065 if (s->blame_complete)
5066 halfdelay(10); /* disable fast refresh */
5068 err = draw_blame(view);
5070 view_vborder(view);
5071 return err;
5074 static const struct got_error *
5075 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5077 const struct got_error *err = NULL, *thread_err = NULL;
5078 struct tog_view *diff_view;
5079 struct tog_blame_view_state *s = &view->state.blame;
5080 int begin_x = 0, nscroll = view->nlines - 2;
5082 switch (ch) {
5083 case '0':
5084 view->x = 0;
5085 break;
5086 case '$':
5087 view->x = MAX(view->maxx - view->ncols / 3, 0);
5088 view->count = 0;
5089 break;
5090 case KEY_RIGHT:
5091 case 'l':
5092 if (view->x + view->ncols / 3 < view->maxx)
5093 view->x += 2; /* move two columns right */
5094 else
5095 view->count = 0;
5096 break;
5097 case KEY_LEFT:
5098 case 'h':
5099 view->x -= MIN(view->x, 2); /* move two columns back */
5100 if (view->x <= 0)
5101 view->count = 0;
5102 break;
5103 case 'q':
5104 s->done = 1;
5105 break;
5106 case 'g':
5107 case KEY_HOME:
5108 s->selected_line = 1;
5109 s->first_displayed_line = 1;
5110 view->count = 0;
5111 break;
5112 case 'G':
5113 case KEY_END:
5114 if (s->blame.nlines < view->nlines - 2) {
5115 s->selected_line = s->blame.nlines;
5116 s->first_displayed_line = 1;
5117 } else {
5118 s->selected_line = view->nlines - 2;
5119 s->first_displayed_line = s->blame.nlines -
5120 (view->nlines - 3);
5122 view->count = 0;
5123 break;
5124 case 'k':
5125 case KEY_UP:
5126 case CTRL('p'):
5127 if (s->selected_line > 1)
5128 s->selected_line--;
5129 else if (s->selected_line == 1 &&
5130 s->first_displayed_line > 1)
5131 s->first_displayed_line--;
5132 else
5133 view->count = 0;
5134 break;
5135 case CTRL('u'):
5136 case 'u':
5137 nscroll /= 2;
5138 /* FALL THROUGH */
5139 case KEY_PPAGE:
5140 case CTRL('b'):
5141 case 'b':
5142 if (s->first_displayed_line == 1) {
5143 if (view->count > 1)
5144 nscroll += nscroll;
5145 s->selected_line = MAX(1, s->selected_line - nscroll);
5146 view->count = 0;
5147 break;
5149 if (s->first_displayed_line > nscroll)
5150 s->first_displayed_line -= nscroll;
5151 else
5152 s->first_displayed_line = 1;
5153 break;
5154 case 'j':
5155 case KEY_DOWN:
5156 case CTRL('n'):
5157 if (s->selected_line < view->nlines - 2 &&
5158 s->first_displayed_line +
5159 s->selected_line <= s->blame.nlines)
5160 s->selected_line++;
5161 else if (s->last_displayed_line <
5162 s->blame.nlines)
5163 s->first_displayed_line++;
5164 else
5165 view->count = 0;
5166 break;
5167 case 'c':
5168 case 'p': {
5169 struct got_object_id *id = NULL;
5171 view->count = 0;
5172 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5173 s->first_displayed_line, s->selected_line);
5174 if (id == NULL)
5175 break;
5176 if (ch == 'p') {
5177 struct got_commit_object *commit, *pcommit;
5178 struct got_object_qid *pid;
5179 struct got_object_id *blob_id = NULL;
5180 int obj_type;
5181 err = got_object_open_as_commit(&commit,
5182 s->repo, id);
5183 if (err)
5184 break;
5185 pid = STAILQ_FIRST(
5186 got_object_commit_get_parent_ids(commit));
5187 if (pid == NULL) {
5188 got_object_commit_close(commit);
5189 break;
5191 /* Check if path history ends here. */
5192 err = got_object_open_as_commit(&pcommit,
5193 s->repo, &pid->id);
5194 if (err)
5195 break;
5196 err = got_object_id_by_path(&blob_id, s->repo,
5197 pcommit, s->path);
5198 got_object_commit_close(pcommit);
5199 if (err) {
5200 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5201 err = NULL;
5202 got_object_commit_close(commit);
5203 break;
5205 err = got_object_get_type(&obj_type, s->repo,
5206 blob_id);
5207 free(blob_id);
5208 /* Can't blame non-blob type objects. */
5209 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5210 got_object_commit_close(commit);
5211 break;
5213 err = got_object_qid_alloc(&s->blamed_commit,
5214 &pid->id);
5215 got_object_commit_close(commit);
5216 } else {
5217 if (got_object_id_cmp(id,
5218 &s->blamed_commit->id) == 0)
5219 break;
5220 err = got_object_qid_alloc(&s->blamed_commit,
5221 id);
5223 if (err)
5224 break;
5225 s->done = 1;
5226 thread_err = stop_blame(&s->blame);
5227 s->done = 0;
5228 if (thread_err)
5229 break;
5230 STAILQ_INSERT_HEAD(&s->blamed_commits,
5231 s->blamed_commit, entry);
5232 err = run_blame(view);
5233 if (err)
5234 break;
5235 break;
5237 case 'C': {
5238 struct got_object_qid *first;
5240 view->count = 0;
5241 first = STAILQ_FIRST(&s->blamed_commits);
5242 if (!got_object_id_cmp(&first->id, s->commit_id))
5243 break;
5244 s->done = 1;
5245 thread_err = stop_blame(&s->blame);
5246 s->done = 0;
5247 if (thread_err)
5248 break;
5249 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5250 got_object_qid_free(s->blamed_commit);
5251 s->blamed_commit =
5252 STAILQ_FIRST(&s->blamed_commits);
5253 err = run_blame(view);
5254 if (err)
5255 break;
5256 break;
5258 case KEY_ENTER:
5259 case '\r': {
5260 struct got_object_id *id = NULL;
5261 struct got_object_qid *pid;
5262 struct got_commit_object *commit = NULL;
5264 view->count = 0;
5265 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5266 s->first_displayed_line, s->selected_line);
5267 if (id == NULL)
5268 break;
5269 err = got_object_open_as_commit(&commit, s->repo, id);
5270 if (err)
5271 break;
5272 pid = STAILQ_FIRST(
5273 got_object_commit_get_parent_ids(commit));
5274 if (view_is_parent_view(view))
5275 begin_x = view_split_begin_x(view->begin_x);
5276 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5277 if (diff_view == NULL) {
5278 got_object_commit_close(commit);
5279 err = got_error_from_errno("view_open");
5280 break;
5282 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5283 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5284 got_object_commit_close(commit);
5285 if (err) {
5286 view_close(diff_view);
5287 break;
5289 view->focussed = 0;
5290 diff_view->focussed = 1;
5291 if (view_is_parent_view(view)) {
5292 err = view_close_child(view);
5293 if (err)
5294 break;
5295 err = view_set_child(view, diff_view);
5296 if (err)
5297 break;
5298 view->focus_child = 1;
5299 } else
5300 *new_view = diff_view;
5301 if (err)
5302 break;
5303 break;
5305 case CTRL('d'):
5306 case 'd':
5307 nscroll /= 2;
5308 /* FALL THROUGH */
5309 case KEY_NPAGE:
5310 case CTRL('f'):
5311 case 'f':
5312 case ' ':
5313 if (s->last_displayed_line >= s->blame.nlines &&
5314 s->selected_line >= MIN(s->blame.nlines,
5315 view->nlines - 2)) {
5316 view->count = 0;
5317 break;
5319 if (s->last_displayed_line >= s->blame.nlines &&
5320 s->selected_line < view->nlines - 2) {
5321 s->selected_line +=
5322 MIN(nscroll, s->last_displayed_line -
5323 s->first_displayed_line - s->selected_line + 1);
5325 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5326 s->first_displayed_line += nscroll;
5327 else
5328 s->first_displayed_line =
5329 s->blame.nlines - (view->nlines - 3);
5330 break;
5331 case KEY_RESIZE:
5332 if (s->selected_line > view->nlines - 2) {
5333 s->selected_line = MIN(s->blame.nlines,
5334 view->nlines - 2);
5336 break;
5337 default:
5338 view->count = 0;
5339 break;
5341 return thread_err ? thread_err : err;
5344 static const struct got_error *
5345 cmd_blame(int argc, char *argv[])
5347 const struct got_error *error;
5348 struct got_repository *repo = NULL;
5349 struct got_worktree *worktree = NULL;
5350 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5351 char *link_target = NULL;
5352 struct got_object_id *commit_id = NULL;
5353 struct got_commit_object *commit = NULL;
5354 char *commit_id_str = NULL;
5355 int ch;
5356 struct tog_view *view;
5357 int *pack_fds = NULL;
5359 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5360 switch (ch) {
5361 case 'c':
5362 commit_id_str = optarg;
5363 break;
5364 case 'r':
5365 repo_path = realpath(optarg, NULL);
5366 if (repo_path == NULL)
5367 return got_error_from_errno2("realpath",
5368 optarg);
5369 break;
5370 default:
5371 usage_blame();
5372 /* NOTREACHED */
5376 argc -= optind;
5377 argv += optind;
5379 if (argc != 1)
5380 usage_blame();
5382 error = got_repo_pack_fds_open(&pack_fds);
5383 if (error != NULL)
5384 goto done;
5386 if (repo_path == NULL) {
5387 cwd = getcwd(NULL, 0);
5388 if (cwd == NULL)
5389 return got_error_from_errno("getcwd");
5390 error = got_worktree_open(&worktree, cwd);
5391 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5392 goto done;
5393 if (worktree)
5394 repo_path =
5395 strdup(got_worktree_get_repo_path(worktree));
5396 else
5397 repo_path = strdup(cwd);
5398 if (repo_path == NULL) {
5399 error = got_error_from_errno("strdup");
5400 goto done;
5404 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5405 if (error != NULL)
5406 goto done;
5408 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5409 worktree);
5410 if (error)
5411 goto done;
5413 init_curses();
5415 error = apply_unveil(got_repo_get_path(repo), NULL);
5416 if (error)
5417 goto done;
5419 error = tog_load_refs(repo, 0);
5420 if (error)
5421 goto done;
5423 if (commit_id_str == NULL) {
5424 struct got_reference *head_ref;
5425 error = got_ref_open(&head_ref, repo, worktree ?
5426 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5427 if (error != NULL)
5428 goto done;
5429 error = got_ref_resolve(&commit_id, repo, head_ref);
5430 got_ref_close(head_ref);
5431 } else {
5432 error = got_repo_match_object_id(&commit_id, NULL,
5433 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5435 if (error != NULL)
5436 goto done;
5438 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5439 if (view == NULL) {
5440 error = got_error_from_errno("view_open");
5441 goto done;
5444 error = got_object_open_as_commit(&commit, repo, commit_id);
5445 if (error)
5446 goto done;
5448 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5449 commit, repo);
5450 if (error)
5451 goto done;
5453 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5454 commit_id, repo);
5455 if (error)
5456 goto done;
5457 if (worktree) {
5458 /* Release work tree lock. */
5459 got_worktree_close(worktree);
5460 worktree = NULL;
5462 error = view_loop(view);
5463 done:
5464 free(repo_path);
5465 free(in_repo_path);
5466 free(link_target);
5467 free(cwd);
5468 free(commit_id);
5469 if (commit)
5470 got_object_commit_close(commit);
5471 if (worktree)
5472 got_worktree_close(worktree);
5473 if (repo) {
5474 const struct got_error *close_err = got_repo_close(repo);
5475 if (error == NULL)
5476 error = close_err;
5478 if (pack_fds) {
5479 const struct got_error *pack_err =
5480 got_repo_pack_fds_close(pack_fds);
5481 if (error == NULL)
5482 error = pack_err;
5484 tog_free_refs();
5485 return error;
5488 static const struct got_error *
5489 draw_tree_entries(struct tog_view *view, const char *parent_path)
5491 struct tog_tree_view_state *s = &view->state.tree;
5492 const struct got_error *err = NULL;
5493 struct got_tree_entry *te;
5494 wchar_t *wline;
5495 struct tog_color *tc;
5496 int width, n, i, nentries;
5497 int limit = view->nlines;
5499 s->ndisplayed = 0;
5501 werase(view->window);
5503 if (limit == 0)
5504 return NULL;
5506 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5507 0, 0);
5508 if (err)
5509 return err;
5510 if (view_needs_focus_indication(view))
5511 wstandout(view->window);
5512 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5513 if (tc)
5514 wattr_on(view->window,
5515 COLOR_PAIR(tc->colorpair), NULL);
5516 waddwstr(view->window, wline);
5517 if (tc)
5518 wattr_off(view->window,
5519 COLOR_PAIR(tc->colorpair), NULL);
5520 if (view_needs_focus_indication(view))
5521 wstandend(view->window);
5522 free(wline);
5523 wline = NULL;
5524 if (width < view->ncols - 1)
5525 waddch(view->window, '\n');
5526 if (--limit <= 0)
5527 return NULL;
5528 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5529 0, 0);
5530 if (err)
5531 return err;
5532 waddwstr(view->window, wline);
5533 free(wline);
5534 wline = NULL;
5535 if (width < view->ncols - 1)
5536 waddch(view->window, '\n');
5537 if (--limit <= 0)
5538 return NULL;
5539 waddch(view->window, '\n');
5540 if (--limit <= 0)
5541 return NULL;
5543 if (s->first_displayed_entry == NULL) {
5544 te = got_object_tree_get_first_entry(s->tree);
5545 if (s->selected == 0) {
5546 if (view->focussed)
5547 wstandout(view->window);
5548 s->selected_entry = NULL;
5550 waddstr(view->window, " ..\n"); /* parent directory */
5551 if (s->selected == 0 && view->focussed)
5552 wstandend(view->window);
5553 s->ndisplayed++;
5554 if (--limit <= 0)
5555 return NULL;
5556 n = 1;
5557 } else {
5558 n = 0;
5559 te = s->first_displayed_entry;
5562 nentries = got_object_tree_get_nentries(s->tree);
5563 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5564 char *line = NULL, *id_str = NULL, *link_target = NULL;
5565 const char *modestr = "";
5566 mode_t mode;
5568 te = got_object_tree_get_entry(s->tree, i);
5569 mode = got_tree_entry_get_mode(te);
5571 if (s->show_ids) {
5572 err = got_object_id_str(&id_str,
5573 got_tree_entry_get_id(te));
5574 if (err)
5575 return got_error_from_errno(
5576 "got_object_id_str");
5578 if (got_object_tree_entry_is_submodule(te))
5579 modestr = "$";
5580 else if (S_ISLNK(mode)) {
5581 int i;
5583 err = got_tree_entry_get_symlink_target(&link_target,
5584 te, s->repo);
5585 if (err) {
5586 free(id_str);
5587 return err;
5589 for (i = 0; i < strlen(link_target); i++) {
5590 if (!isprint((unsigned char)link_target[i]))
5591 link_target[i] = '?';
5593 modestr = "@";
5595 else if (S_ISDIR(mode))
5596 modestr = "/";
5597 else if (mode & S_IXUSR)
5598 modestr = "*";
5599 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5600 got_tree_entry_get_name(te), modestr,
5601 link_target ? " -> ": "",
5602 link_target ? link_target : "") == -1) {
5603 free(id_str);
5604 free(link_target);
5605 return got_error_from_errno("asprintf");
5607 free(id_str);
5608 free(link_target);
5609 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5610 0, 0);
5611 if (err) {
5612 free(line);
5613 break;
5615 if (n == s->selected) {
5616 if (view->focussed)
5617 wstandout(view->window);
5618 s->selected_entry = te;
5620 tc = match_color(&s->colors, line);
5621 if (tc)
5622 wattr_on(view->window,
5623 COLOR_PAIR(tc->colorpair), NULL);
5624 waddwstr(view->window, wline);
5625 if (tc)
5626 wattr_off(view->window,
5627 COLOR_PAIR(tc->colorpair), NULL);
5628 if (width < view->ncols - 1)
5629 waddch(view->window, '\n');
5630 if (n == s->selected && view->focussed)
5631 wstandend(view->window);
5632 free(line);
5633 free(wline);
5634 wline = NULL;
5635 n++;
5636 s->ndisplayed++;
5637 s->last_displayed_entry = te;
5638 if (--limit <= 0)
5639 break;
5642 return err;
5645 static void
5646 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5648 struct got_tree_entry *te;
5649 int isroot = s->tree == s->root;
5650 int i = 0;
5652 if (s->first_displayed_entry == NULL)
5653 return;
5655 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5656 while (i++ < maxscroll) {
5657 if (te == NULL) {
5658 if (!isroot)
5659 s->first_displayed_entry = NULL;
5660 break;
5662 s->first_displayed_entry = te;
5663 te = got_tree_entry_get_prev(s->tree, te);
5667 static void
5668 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5670 struct got_tree_entry *next, *last;
5671 int n = 0;
5673 if (s->first_displayed_entry)
5674 next = got_tree_entry_get_next(s->tree,
5675 s->first_displayed_entry);
5676 else
5677 next = got_object_tree_get_first_entry(s->tree);
5679 last = s->last_displayed_entry;
5680 while (next && last && n++ < maxscroll) {
5681 last = got_tree_entry_get_next(s->tree, last);
5682 if (last) {
5683 s->first_displayed_entry = next;
5684 next = got_tree_entry_get_next(s->tree, next);
5689 static const struct got_error *
5690 tree_entry_path(char **path, struct tog_parent_trees *parents,
5691 struct got_tree_entry *te)
5693 const struct got_error *err = NULL;
5694 struct tog_parent_tree *pt;
5695 size_t len = 2; /* for leading slash and NUL */
5697 TAILQ_FOREACH(pt, parents, entry)
5698 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5699 + 1 /* slash */;
5700 if (te)
5701 len += strlen(got_tree_entry_get_name(te));
5703 *path = calloc(1, len);
5704 if (path == NULL)
5705 return got_error_from_errno("calloc");
5707 (*path)[0] = '/';
5708 pt = TAILQ_LAST(parents, tog_parent_trees);
5709 while (pt) {
5710 const char *name = got_tree_entry_get_name(pt->selected_entry);
5711 if (strlcat(*path, name, len) >= len) {
5712 err = got_error(GOT_ERR_NO_SPACE);
5713 goto done;
5715 if (strlcat(*path, "/", len) >= len) {
5716 err = got_error(GOT_ERR_NO_SPACE);
5717 goto done;
5719 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5721 if (te) {
5722 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5723 err = got_error(GOT_ERR_NO_SPACE);
5724 goto done;
5727 done:
5728 if (err) {
5729 free(*path);
5730 *path = NULL;
5732 return err;
5735 static const struct got_error *
5736 blame_tree_entry(struct tog_view **new_view, int begin_x,
5737 struct got_tree_entry *te, struct tog_parent_trees *parents,
5738 struct got_object_id *commit_id, struct got_repository *repo)
5740 const struct got_error *err = NULL;
5741 char *path;
5742 struct tog_view *blame_view;
5744 *new_view = NULL;
5746 err = tree_entry_path(&path, parents, te);
5747 if (err)
5748 return err;
5750 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5751 if (blame_view == NULL) {
5752 err = got_error_from_errno("view_open");
5753 goto done;
5756 err = open_blame_view(blame_view, path, commit_id, repo);
5757 if (err) {
5758 if (err->code == GOT_ERR_CANCELLED)
5759 err = NULL;
5760 view_close(blame_view);
5761 } else
5762 *new_view = blame_view;
5763 done:
5764 free(path);
5765 return err;
5768 static const struct got_error *
5769 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5770 struct tog_tree_view_state *s)
5772 struct tog_view *log_view;
5773 const struct got_error *err = NULL;
5774 char *path;
5776 *new_view = NULL;
5778 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5779 if (log_view == NULL)
5780 return got_error_from_errno("view_open");
5782 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5783 if (err)
5784 return err;
5786 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5787 path, 0);
5788 if (err)
5789 view_close(log_view);
5790 else
5791 *new_view = log_view;
5792 free(path);
5793 return err;
5796 static const struct got_error *
5797 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5798 const char *head_ref_name, struct got_repository *repo)
5800 const struct got_error *err = NULL;
5801 char *commit_id_str = NULL;
5802 struct tog_tree_view_state *s = &view->state.tree;
5803 struct got_commit_object *commit = NULL;
5805 TAILQ_INIT(&s->parents);
5806 STAILQ_INIT(&s->colors);
5808 s->commit_id = got_object_id_dup(commit_id);
5809 if (s->commit_id == NULL)
5810 return got_error_from_errno("got_object_id_dup");
5812 err = got_object_open_as_commit(&commit, repo, commit_id);
5813 if (err)
5814 goto done;
5817 * The root is opened here and will be closed when the view is closed.
5818 * Any visited subtrees and their path-wise parents are opened and
5819 * closed on demand.
5821 err = got_object_open_as_tree(&s->root, repo,
5822 got_object_commit_get_tree_id(commit));
5823 if (err)
5824 goto done;
5825 s->tree = s->root;
5827 err = got_object_id_str(&commit_id_str, commit_id);
5828 if (err != NULL)
5829 goto done;
5831 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5832 err = got_error_from_errno("asprintf");
5833 goto done;
5836 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5837 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5838 if (head_ref_name) {
5839 s->head_ref_name = strdup(head_ref_name);
5840 if (s->head_ref_name == NULL) {
5841 err = got_error_from_errno("strdup");
5842 goto done;
5845 s->repo = repo;
5847 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5848 err = add_color(&s->colors, "\\$$",
5849 TOG_COLOR_TREE_SUBMODULE,
5850 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5851 if (err)
5852 goto done;
5853 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5854 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5855 if (err)
5856 goto done;
5857 err = add_color(&s->colors, "/$",
5858 TOG_COLOR_TREE_DIRECTORY,
5859 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5860 if (err)
5861 goto done;
5863 err = add_color(&s->colors, "\\*$",
5864 TOG_COLOR_TREE_EXECUTABLE,
5865 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5866 if (err)
5867 goto done;
5869 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5870 get_color_value("TOG_COLOR_COMMIT"));
5871 if (err)
5872 goto done;
5875 view->show = show_tree_view;
5876 view->input = input_tree_view;
5877 view->close = close_tree_view;
5878 view->search_start = search_start_tree_view;
5879 view->search_next = search_next_tree_view;
5880 done:
5881 free(commit_id_str);
5882 if (commit)
5883 got_object_commit_close(commit);
5884 if (err)
5885 close_tree_view(view);
5886 return err;
5889 static const struct got_error *
5890 close_tree_view(struct tog_view *view)
5892 struct tog_tree_view_state *s = &view->state.tree;
5894 free_colors(&s->colors);
5895 free(s->tree_label);
5896 s->tree_label = NULL;
5897 free(s->commit_id);
5898 s->commit_id = NULL;
5899 free(s->head_ref_name);
5900 s->head_ref_name = NULL;
5901 while (!TAILQ_EMPTY(&s->parents)) {
5902 struct tog_parent_tree *parent;
5903 parent = TAILQ_FIRST(&s->parents);
5904 TAILQ_REMOVE(&s->parents, parent, entry);
5905 if (parent->tree != s->root)
5906 got_object_tree_close(parent->tree);
5907 free(parent);
5910 if (s->tree != NULL && s->tree != s->root)
5911 got_object_tree_close(s->tree);
5912 if (s->root)
5913 got_object_tree_close(s->root);
5914 return NULL;
5917 static const struct got_error *
5918 search_start_tree_view(struct tog_view *view)
5920 struct tog_tree_view_state *s = &view->state.tree;
5922 s->matched_entry = NULL;
5923 return NULL;
5926 static int
5927 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5929 regmatch_t regmatch;
5931 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5932 0) == 0;
5935 static const struct got_error *
5936 search_next_tree_view(struct tog_view *view)
5938 struct tog_tree_view_state *s = &view->state.tree;
5939 struct got_tree_entry *te = NULL;
5941 if (!view->searching) {
5942 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5943 return NULL;
5946 if (s->matched_entry) {
5947 if (view->searching == TOG_SEARCH_FORWARD) {
5948 if (s->selected_entry)
5949 te = got_tree_entry_get_next(s->tree,
5950 s->selected_entry);
5951 else
5952 te = got_object_tree_get_first_entry(s->tree);
5953 } else {
5954 if (s->selected_entry == NULL)
5955 te = got_object_tree_get_last_entry(s->tree);
5956 else
5957 te = got_tree_entry_get_prev(s->tree,
5958 s->selected_entry);
5960 } else {
5961 if (s->selected_entry)
5962 te = s->selected_entry;
5963 else if (view->searching == TOG_SEARCH_FORWARD)
5964 te = got_object_tree_get_first_entry(s->tree);
5965 else
5966 te = got_object_tree_get_last_entry(s->tree);
5969 while (1) {
5970 if (te == NULL) {
5971 if (s->matched_entry == NULL) {
5972 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5973 return NULL;
5975 if (view->searching == TOG_SEARCH_FORWARD)
5976 te = got_object_tree_get_first_entry(s->tree);
5977 else
5978 te = got_object_tree_get_last_entry(s->tree);
5981 if (match_tree_entry(te, &view->regex)) {
5982 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5983 s->matched_entry = te;
5984 break;
5987 if (view->searching == TOG_SEARCH_FORWARD)
5988 te = got_tree_entry_get_next(s->tree, te);
5989 else
5990 te = got_tree_entry_get_prev(s->tree, te);
5993 if (s->matched_entry) {
5994 s->first_displayed_entry = s->matched_entry;
5995 s->selected = 0;
5998 return NULL;
6001 static const struct got_error *
6002 show_tree_view(struct tog_view *view)
6004 const struct got_error *err = NULL;
6005 struct tog_tree_view_state *s = &view->state.tree;
6006 char *parent_path;
6008 err = tree_entry_path(&parent_path, &s->parents, NULL);
6009 if (err)
6010 return err;
6012 err = draw_tree_entries(view, parent_path);
6013 free(parent_path);
6015 view_vborder(view);
6016 return err;
6019 static const struct got_error *
6020 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6022 const struct got_error *err = NULL;
6023 struct tog_tree_view_state *s = &view->state.tree;
6024 struct tog_view *log_view, *ref_view;
6025 struct got_tree_entry *te;
6026 int begin_x = 0, n, nscroll = view->nlines - 3;
6028 switch (ch) {
6029 case 'i':
6030 s->show_ids = !s->show_ids;
6031 view->count = 0;
6032 break;
6033 case 'l':
6034 view->count = 0;
6035 if (!s->selected_entry)
6036 break;
6037 if (view_is_parent_view(view))
6038 begin_x = view_split_begin_x(view->begin_x);
6039 err = log_selected_tree_entry(&log_view, begin_x, s);
6040 view->focussed = 0;
6041 log_view->focussed = 1;
6042 if (view_is_parent_view(view)) {
6043 err = view_close_child(view);
6044 if (err)
6045 return err;
6046 err = view_set_child(view, log_view);
6047 if (err)
6048 return err;
6049 view->focus_child = 1;
6050 } else
6051 *new_view = log_view;
6052 break;
6053 case 'r':
6054 view->count = 0;
6055 if (view_is_parent_view(view))
6056 begin_x = view_split_begin_x(view->begin_x);
6057 ref_view = view_open(view->nlines, view->ncols,
6058 view->begin_y, begin_x, TOG_VIEW_REF);
6059 if (ref_view == NULL)
6060 return got_error_from_errno("view_open");
6061 err = open_ref_view(ref_view, s->repo);
6062 if (err) {
6063 view_close(ref_view);
6064 return err;
6066 view->focussed = 0;
6067 ref_view->focussed = 1;
6068 if (view_is_parent_view(view)) {
6069 err = view_close_child(view);
6070 if (err)
6071 return err;
6072 err = view_set_child(view, ref_view);
6073 if (err)
6074 return err;
6075 view->focus_child = 1;
6076 } else
6077 *new_view = ref_view;
6078 break;
6079 case 'g':
6080 case KEY_HOME:
6081 s->selected = 0;
6082 view->count = 0;
6083 if (s->tree == s->root)
6084 s->first_displayed_entry =
6085 got_object_tree_get_first_entry(s->tree);
6086 else
6087 s->first_displayed_entry = NULL;
6088 break;
6089 case 'G':
6090 case KEY_END:
6091 s->selected = 0;
6092 view->count = 0;
6093 te = got_object_tree_get_last_entry(s->tree);
6094 for (n = 0; n < view->nlines - 3; n++) {
6095 if (te == NULL) {
6096 if(s->tree != s->root) {
6097 s->first_displayed_entry = NULL;
6098 n++;
6100 break;
6102 s->first_displayed_entry = te;
6103 te = got_tree_entry_get_prev(s->tree, te);
6105 if (n > 0)
6106 s->selected = n - 1;
6107 break;
6108 case 'k':
6109 case KEY_UP:
6110 case CTRL('p'):
6111 if (s->selected > 0) {
6112 s->selected--;
6113 break;
6115 tree_scroll_up(s, 1);
6116 if (s->selected_entry == NULL ||
6117 (s->tree == s->root && s->selected_entry ==
6118 got_object_tree_get_first_entry(s->tree)))
6119 view->count = 0;
6120 break;
6121 case CTRL('u'):
6122 case 'u':
6123 nscroll /= 2;
6124 /* FALL THROUGH */
6125 case KEY_PPAGE:
6126 case CTRL('b'):
6127 case 'b':
6128 if (s->tree == s->root) {
6129 if (got_object_tree_get_first_entry(s->tree) ==
6130 s->first_displayed_entry)
6131 s->selected -= MIN(s->selected, nscroll);
6132 } else {
6133 if (s->first_displayed_entry == NULL)
6134 s->selected -= MIN(s->selected, nscroll);
6136 tree_scroll_up(s, MAX(0, nscroll));
6137 if (s->selected_entry == NULL ||
6138 (s->tree == s->root && s->selected_entry ==
6139 got_object_tree_get_first_entry(s->tree)))
6140 view->count = 0;
6141 break;
6142 case 'j':
6143 case KEY_DOWN:
6144 case CTRL('n'):
6145 if (s->selected < s->ndisplayed - 1) {
6146 s->selected++;
6147 break;
6149 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6150 == NULL) {
6151 /* can't scroll any further */
6152 view->count = 0;
6153 break;
6155 tree_scroll_down(s, 1);
6156 break;
6157 case CTRL('d'):
6158 case 'd':
6159 nscroll /= 2;
6160 /* FALL THROUGH */
6161 case KEY_NPAGE:
6162 case CTRL('f'):
6163 case 'f':
6164 case ' ':
6165 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6166 == NULL) {
6167 /* can't scroll any further; move cursor down */
6168 if (s->selected < s->ndisplayed - 1)
6169 s->selected += MIN(nscroll,
6170 s->ndisplayed - s->selected - 1);
6171 else
6172 view->count = 0;
6173 break;
6175 tree_scroll_down(s, nscroll);
6176 break;
6177 case KEY_ENTER:
6178 case '\r':
6179 case KEY_BACKSPACE:
6180 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6181 struct tog_parent_tree *parent;
6182 /* user selected '..' */
6183 if (s->tree == s->root) {
6184 view->count = 0;
6185 break;
6187 parent = TAILQ_FIRST(&s->parents);
6188 TAILQ_REMOVE(&s->parents, parent,
6189 entry);
6190 got_object_tree_close(s->tree);
6191 s->tree = parent->tree;
6192 s->first_displayed_entry =
6193 parent->first_displayed_entry;
6194 s->selected_entry =
6195 parent->selected_entry;
6196 s->selected = parent->selected;
6197 free(parent);
6198 } else if (S_ISDIR(got_tree_entry_get_mode(
6199 s->selected_entry))) {
6200 struct got_tree_object *subtree;
6201 view->count = 0;
6202 err = got_object_open_as_tree(&subtree, s->repo,
6203 got_tree_entry_get_id(s->selected_entry));
6204 if (err)
6205 break;
6206 err = tree_view_visit_subtree(s, subtree);
6207 if (err) {
6208 got_object_tree_close(subtree);
6209 break;
6211 } else if (S_ISREG(got_tree_entry_get_mode(
6212 s->selected_entry))) {
6213 struct tog_view *blame_view;
6214 int begin_x = view_is_parent_view(view) ?
6215 view_split_begin_x(view->begin_x) : 0;
6217 err = blame_tree_entry(&blame_view, begin_x,
6218 s->selected_entry, &s->parents,
6219 s->commit_id, s->repo);
6220 if (err)
6221 break;
6222 view->count = 0;
6223 view->focussed = 0;
6224 blame_view->focussed = 1;
6225 if (view_is_parent_view(view)) {
6226 err = view_close_child(view);
6227 if (err)
6228 return err;
6229 err = view_set_child(view, blame_view);
6230 if (err)
6231 return err;
6232 view->focus_child = 1;
6233 } else
6234 *new_view = blame_view;
6236 break;
6237 case KEY_RESIZE:
6238 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6239 s->selected = view->nlines - 4;
6240 view->count = 0;
6241 break;
6242 default:
6243 view->count = 0;
6244 break;
6247 return err;
6250 __dead static void
6251 usage_tree(void)
6253 endwin();
6254 fprintf(stderr,
6255 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6256 getprogname());
6257 exit(1);
6260 static const struct got_error *
6261 cmd_tree(int argc, char *argv[])
6263 const struct got_error *error;
6264 struct got_repository *repo = NULL;
6265 struct got_worktree *worktree = NULL;
6266 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6267 struct got_object_id *commit_id = NULL;
6268 struct got_commit_object *commit = NULL;
6269 const char *commit_id_arg = NULL;
6270 char *label = NULL;
6271 struct got_reference *ref = NULL;
6272 const char *head_ref_name = NULL;
6273 int ch;
6274 struct tog_view *view;
6275 int *pack_fds = NULL;
6277 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6278 switch (ch) {
6279 case 'c':
6280 commit_id_arg = optarg;
6281 break;
6282 case 'r':
6283 repo_path = realpath(optarg, NULL);
6284 if (repo_path == NULL)
6285 return got_error_from_errno2("realpath",
6286 optarg);
6287 break;
6288 default:
6289 usage_tree();
6290 /* NOTREACHED */
6294 argc -= optind;
6295 argv += optind;
6297 if (argc > 1)
6298 usage_tree();
6300 error = got_repo_pack_fds_open(&pack_fds);
6301 if (error != NULL)
6302 goto done;
6304 if (repo_path == NULL) {
6305 cwd = getcwd(NULL, 0);
6306 if (cwd == NULL)
6307 return got_error_from_errno("getcwd");
6308 error = got_worktree_open(&worktree, cwd);
6309 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6310 goto done;
6311 if (worktree)
6312 repo_path =
6313 strdup(got_worktree_get_repo_path(worktree));
6314 else
6315 repo_path = strdup(cwd);
6316 if (repo_path == NULL) {
6317 error = got_error_from_errno("strdup");
6318 goto done;
6322 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6323 if (error != NULL)
6324 goto done;
6326 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6327 repo, worktree);
6328 if (error)
6329 goto done;
6331 init_curses();
6333 error = apply_unveil(got_repo_get_path(repo), NULL);
6334 if (error)
6335 goto done;
6337 error = tog_load_refs(repo, 0);
6338 if (error)
6339 goto done;
6341 if (commit_id_arg == NULL) {
6342 error = got_repo_match_object_id(&commit_id, &label,
6343 worktree ? got_worktree_get_head_ref_name(worktree) :
6344 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6345 if (error)
6346 goto done;
6347 head_ref_name = label;
6348 } else {
6349 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6350 if (error == NULL)
6351 head_ref_name = got_ref_get_name(ref);
6352 else if (error->code != GOT_ERR_NOT_REF)
6353 goto done;
6354 error = got_repo_match_object_id(&commit_id, NULL,
6355 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6356 if (error)
6357 goto done;
6360 error = got_object_open_as_commit(&commit, repo, commit_id);
6361 if (error)
6362 goto done;
6364 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6365 if (view == NULL) {
6366 error = got_error_from_errno("view_open");
6367 goto done;
6369 error = open_tree_view(view, commit_id, head_ref_name, repo);
6370 if (error)
6371 goto done;
6372 if (!got_path_is_root_dir(in_repo_path)) {
6373 error = tree_view_walk_path(&view->state.tree, commit,
6374 in_repo_path);
6375 if (error)
6376 goto done;
6379 if (worktree) {
6380 /* Release work tree lock. */
6381 got_worktree_close(worktree);
6382 worktree = NULL;
6384 error = view_loop(view);
6385 done:
6386 free(repo_path);
6387 free(cwd);
6388 free(commit_id);
6389 free(label);
6390 if (ref)
6391 got_ref_close(ref);
6392 if (repo) {
6393 const struct got_error *close_err = got_repo_close(repo);
6394 if (error == NULL)
6395 error = close_err;
6397 if (pack_fds) {
6398 const struct got_error *pack_err =
6399 got_repo_pack_fds_close(pack_fds);
6400 if (error == NULL)
6401 error = pack_err;
6403 tog_free_refs();
6404 return error;
6407 static const struct got_error *
6408 ref_view_load_refs(struct tog_ref_view_state *s)
6410 struct got_reflist_entry *sre;
6411 struct tog_reflist_entry *re;
6413 s->nrefs = 0;
6414 TAILQ_FOREACH(sre, &tog_refs, entry) {
6415 if (strncmp(got_ref_get_name(sre->ref),
6416 "refs/got/", 9) == 0 &&
6417 strncmp(got_ref_get_name(sre->ref),
6418 "refs/got/backup/", 16) != 0)
6419 continue;
6421 re = malloc(sizeof(*re));
6422 if (re == NULL)
6423 return got_error_from_errno("malloc");
6425 re->ref = got_ref_dup(sre->ref);
6426 if (re->ref == NULL)
6427 return got_error_from_errno("got_ref_dup");
6428 re->idx = s->nrefs++;
6429 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6432 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6433 return NULL;
6436 static void
6437 ref_view_free_refs(struct tog_ref_view_state *s)
6439 struct tog_reflist_entry *re;
6441 while (!TAILQ_EMPTY(&s->refs)) {
6442 re = TAILQ_FIRST(&s->refs);
6443 TAILQ_REMOVE(&s->refs, re, entry);
6444 got_ref_close(re->ref);
6445 free(re);
6449 static const struct got_error *
6450 open_ref_view(struct tog_view *view, struct got_repository *repo)
6452 const struct got_error *err = NULL;
6453 struct tog_ref_view_state *s = &view->state.ref;
6455 s->selected_entry = 0;
6456 s->repo = repo;
6458 TAILQ_INIT(&s->refs);
6459 STAILQ_INIT(&s->colors);
6461 err = ref_view_load_refs(s);
6462 if (err)
6463 return err;
6465 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6466 err = add_color(&s->colors, "^refs/heads/",
6467 TOG_COLOR_REFS_HEADS,
6468 get_color_value("TOG_COLOR_REFS_HEADS"));
6469 if (err)
6470 goto done;
6472 err = add_color(&s->colors, "^refs/tags/",
6473 TOG_COLOR_REFS_TAGS,
6474 get_color_value("TOG_COLOR_REFS_TAGS"));
6475 if (err)
6476 goto done;
6478 err = add_color(&s->colors, "^refs/remotes/",
6479 TOG_COLOR_REFS_REMOTES,
6480 get_color_value("TOG_COLOR_REFS_REMOTES"));
6481 if (err)
6482 goto done;
6484 err = add_color(&s->colors, "^refs/got/backup/",
6485 TOG_COLOR_REFS_BACKUP,
6486 get_color_value("TOG_COLOR_REFS_BACKUP"));
6487 if (err)
6488 goto done;
6491 view->show = show_ref_view;
6492 view->input = input_ref_view;
6493 view->close = close_ref_view;
6494 view->search_start = search_start_ref_view;
6495 view->search_next = search_next_ref_view;
6496 done:
6497 if (err)
6498 free_colors(&s->colors);
6499 return err;
6502 static const struct got_error *
6503 close_ref_view(struct tog_view *view)
6505 struct tog_ref_view_state *s = &view->state.ref;
6507 ref_view_free_refs(s);
6508 free_colors(&s->colors);
6510 return NULL;
6513 static const struct got_error *
6514 resolve_reflist_entry(struct got_object_id **commit_id,
6515 struct tog_reflist_entry *re, struct got_repository *repo)
6517 const struct got_error *err = NULL;
6518 struct got_object_id *obj_id;
6519 struct got_tag_object *tag = NULL;
6520 int obj_type;
6522 *commit_id = NULL;
6524 err = got_ref_resolve(&obj_id, repo, re->ref);
6525 if (err)
6526 return err;
6528 err = got_object_get_type(&obj_type, repo, obj_id);
6529 if (err)
6530 goto done;
6532 switch (obj_type) {
6533 case GOT_OBJ_TYPE_COMMIT:
6534 *commit_id = obj_id;
6535 break;
6536 case GOT_OBJ_TYPE_TAG:
6537 err = got_object_open_as_tag(&tag, repo, obj_id);
6538 if (err)
6539 goto done;
6540 free(obj_id);
6541 err = got_object_get_type(&obj_type, repo,
6542 got_object_tag_get_object_id(tag));
6543 if (err)
6544 goto done;
6545 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6546 err = got_error(GOT_ERR_OBJ_TYPE);
6547 goto done;
6549 *commit_id = got_object_id_dup(
6550 got_object_tag_get_object_id(tag));
6551 if (*commit_id == NULL) {
6552 err = got_error_from_errno("got_object_id_dup");
6553 goto done;
6555 break;
6556 default:
6557 err = got_error(GOT_ERR_OBJ_TYPE);
6558 break;
6561 done:
6562 if (tag)
6563 got_object_tag_close(tag);
6564 if (err) {
6565 free(*commit_id);
6566 *commit_id = NULL;
6568 return err;
6571 static const struct got_error *
6572 log_ref_entry(struct tog_view **new_view, int begin_x,
6573 struct tog_reflist_entry *re, struct got_repository *repo)
6575 struct tog_view *log_view;
6576 const struct got_error *err = NULL;
6577 struct got_object_id *commit_id = NULL;
6579 *new_view = NULL;
6581 err = resolve_reflist_entry(&commit_id, re, repo);
6582 if (err) {
6583 if (err->code != GOT_ERR_OBJ_TYPE)
6584 return err;
6585 else
6586 return NULL;
6589 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6590 if (log_view == NULL) {
6591 err = got_error_from_errno("view_open");
6592 goto done;
6595 err = open_log_view(log_view, commit_id, repo,
6596 got_ref_get_name(re->ref), "", 0);
6597 done:
6598 if (err)
6599 view_close(log_view);
6600 else
6601 *new_view = log_view;
6602 free(commit_id);
6603 return err;
6606 static void
6607 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6609 struct tog_reflist_entry *re;
6610 int i = 0;
6612 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6613 return;
6615 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6616 while (i++ < maxscroll) {
6617 if (re == NULL)
6618 break;
6619 s->first_displayed_entry = re;
6620 re = TAILQ_PREV(re, tog_reflist_head, entry);
6624 static void
6625 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6627 struct tog_reflist_entry *next, *last;
6628 int n = 0;
6630 if (s->first_displayed_entry)
6631 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6632 else
6633 next = TAILQ_FIRST(&s->refs);
6635 last = s->last_displayed_entry;
6636 while (next && last && n++ < maxscroll) {
6637 last = TAILQ_NEXT(last, entry);
6638 if (last) {
6639 s->first_displayed_entry = next;
6640 next = TAILQ_NEXT(next, entry);
6645 static const struct got_error *
6646 search_start_ref_view(struct tog_view *view)
6648 struct tog_ref_view_state *s = &view->state.ref;
6650 s->matched_entry = NULL;
6651 return NULL;
6654 static int
6655 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6657 regmatch_t regmatch;
6659 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6660 0) == 0;
6663 static const struct got_error *
6664 search_next_ref_view(struct tog_view *view)
6666 struct tog_ref_view_state *s = &view->state.ref;
6667 struct tog_reflist_entry *re = NULL;
6669 if (!view->searching) {
6670 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6671 return NULL;
6674 if (s->matched_entry) {
6675 if (view->searching == TOG_SEARCH_FORWARD) {
6676 if (s->selected_entry)
6677 re = TAILQ_NEXT(s->selected_entry, entry);
6678 else
6679 re = TAILQ_PREV(s->selected_entry,
6680 tog_reflist_head, entry);
6681 } else {
6682 if (s->selected_entry == NULL)
6683 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6684 else
6685 re = TAILQ_PREV(s->selected_entry,
6686 tog_reflist_head, entry);
6688 } else {
6689 if (s->selected_entry)
6690 re = s->selected_entry;
6691 else if (view->searching == TOG_SEARCH_FORWARD)
6692 re = TAILQ_FIRST(&s->refs);
6693 else
6694 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6697 while (1) {
6698 if (re == NULL) {
6699 if (s->matched_entry == NULL) {
6700 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6701 return NULL;
6703 if (view->searching == TOG_SEARCH_FORWARD)
6704 re = TAILQ_FIRST(&s->refs);
6705 else
6706 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6709 if (match_reflist_entry(re, &view->regex)) {
6710 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6711 s->matched_entry = re;
6712 break;
6715 if (view->searching == TOG_SEARCH_FORWARD)
6716 re = TAILQ_NEXT(re, entry);
6717 else
6718 re = TAILQ_PREV(re, tog_reflist_head, entry);
6721 if (s->matched_entry) {
6722 s->first_displayed_entry = s->matched_entry;
6723 s->selected = 0;
6726 return NULL;
6729 static const struct got_error *
6730 show_ref_view(struct tog_view *view)
6732 const struct got_error *err = NULL;
6733 struct tog_ref_view_state *s = &view->state.ref;
6734 struct tog_reflist_entry *re;
6735 char *line = NULL;
6736 wchar_t *wline;
6737 struct tog_color *tc;
6738 int width, n;
6739 int limit = view->nlines;
6741 werase(view->window);
6743 s->ndisplayed = 0;
6745 if (limit == 0)
6746 return NULL;
6748 re = s->first_displayed_entry;
6750 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6751 s->nrefs) == -1)
6752 return got_error_from_errno("asprintf");
6754 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6755 if (err) {
6756 free(line);
6757 return err;
6759 if (view_needs_focus_indication(view))
6760 wstandout(view->window);
6761 waddwstr(view->window, wline);
6762 if (view_needs_focus_indication(view))
6763 wstandend(view->window);
6764 free(wline);
6765 wline = NULL;
6766 free(line);
6767 line = NULL;
6768 if (width < view->ncols - 1)
6769 waddch(view->window, '\n');
6770 if (--limit <= 0)
6771 return NULL;
6773 n = 0;
6774 while (re && limit > 0) {
6775 char *line = NULL;
6776 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6778 if (s->show_date) {
6779 struct got_commit_object *ci;
6780 struct got_tag_object *tag;
6781 struct got_object_id *id;
6782 struct tm tm;
6783 time_t t;
6785 err = got_ref_resolve(&id, s->repo, re->ref);
6786 if (err)
6787 return err;
6788 err = got_object_open_as_tag(&tag, s->repo, id);
6789 if (err) {
6790 if (err->code != GOT_ERR_OBJ_TYPE) {
6791 free(id);
6792 return err;
6794 err = got_object_open_as_commit(&ci, s->repo,
6795 id);
6796 if (err) {
6797 free(id);
6798 return err;
6800 t = got_object_commit_get_committer_time(ci);
6801 got_object_commit_close(ci);
6802 } else {
6803 t = got_object_tag_get_tagger_time(tag);
6804 got_object_tag_close(tag);
6806 free(id);
6807 if (gmtime_r(&t, &tm) == NULL)
6808 return got_error_from_errno("gmtime_r");
6809 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6810 return got_error(GOT_ERR_NO_SPACE);
6812 if (got_ref_is_symbolic(re->ref)) {
6813 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6814 ymd : "", got_ref_get_name(re->ref),
6815 got_ref_get_symref_target(re->ref)) == -1)
6816 return got_error_from_errno("asprintf");
6817 } else if (s->show_ids) {
6818 struct got_object_id *id;
6819 char *id_str;
6820 err = got_ref_resolve(&id, s->repo, re->ref);
6821 if (err)
6822 return err;
6823 err = got_object_id_str(&id_str, id);
6824 if (err) {
6825 free(id);
6826 return err;
6828 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6829 got_ref_get_name(re->ref), id_str) == -1) {
6830 err = got_error_from_errno("asprintf");
6831 free(id);
6832 free(id_str);
6833 return err;
6835 free(id);
6836 free(id_str);
6837 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6838 got_ref_get_name(re->ref)) == -1)
6839 return got_error_from_errno("asprintf");
6841 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6842 0, 0);
6843 if (err) {
6844 free(line);
6845 return err;
6847 if (n == s->selected) {
6848 if (view->focussed)
6849 wstandout(view->window);
6850 s->selected_entry = re;
6852 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6853 if (tc)
6854 wattr_on(view->window,
6855 COLOR_PAIR(tc->colorpair), NULL);
6856 waddwstr(view->window, wline);
6857 if (tc)
6858 wattr_off(view->window,
6859 COLOR_PAIR(tc->colorpair), NULL);
6860 if (width < view->ncols - 1)
6861 waddch(view->window, '\n');
6862 if (n == s->selected && view->focussed)
6863 wstandend(view->window);
6864 free(line);
6865 free(wline);
6866 wline = NULL;
6867 n++;
6868 s->ndisplayed++;
6869 s->last_displayed_entry = re;
6871 limit--;
6872 re = TAILQ_NEXT(re, entry);
6875 view_vborder(view);
6876 return err;
6879 static const struct got_error *
6880 browse_ref_tree(struct tog_view **new_view, int begin_x,
6881 struct tog_reflist_entry *re, struct got_repository *repo)
6883 const struct got_error *err = NULL;
6884 struct got_object_id *commit_id = NULL;
6885 struct tog_view *tree_view;
6887 *new_view = NULL;
6889 err = resolve_reflist_entry(&commit_id, re, repo);
6890 if (err) {
6891 if (err->code != GOT_ERR_OBJ_TYPE)
6892 return err;
6893 else
6894 return NULL;
6898 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6899 if (tree_view == NULL) {
6900 err = got_error_from_errno("view_open");
6901 goto done;
6904 err = open_tree_view(tree_view, commit_id,
6905 got_ref_get_name(re->ref), repo);
6906 if (err)
6907 goto done;
6909 *new_view = tree_view;
6910 done:
6911 free(commit_id);
6912 return err;
6914 static const struct got_error *
6915 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6917 const struct got_error *err = NULL;
6918 struct tog_ref_view_state *s = &view->state.ref;
6919 struct tog_view *log_view, *tree_view;
6920 struct tog_reflist_entry *re;
6921 int begin_x = 0, n, nscroll = view->nlines - 1;
6923 switch (ch) {
6924 case 'i':
6925 s->show_ids = !s->show_ids;
6926 view->count = 0;
6927 break;
6928 case 'm':
6929 s->show_date = !s->show_date;
6930 view->count = 0;
6931 break;
6932 case 'o':
6933 s->sort_by_date = !s->sort_by_date;
6934 view->count = 0;
6935 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6936 got_ref_cmp_by_commit_timestamp_descending :
6937 tog_ref_cmp_by_name, s->repo);
6938 if (err)
6939 break;
6940 got_reflist_object_id_map_free(tog_refs_idmap);
6941 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6942 &tog_refs, s->repo);
6943 if (err)
6944 break;
6945 ref_view_free_refs(s);
6946 err = ref_view_load_refs(s);
6947 break;
6948 case KEY_ENTER:
6949 case '\r':
6950 view->count = 0;
6951 if (!s->selected_entry)
6952 break;
6953 if (view_is_parent_view(view))
6954 begin_x = view_split_begin_x(view->begin_x);
6955 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6956 s->repo);
6957 view->focussed = 0;
6958 log_view->focussed = 1;
6959 if (view_is_parent_view(view)) {
6960 err = view_close_child(view);
6961 if (err)
6962 return err;
6963 err = view_set_child(view, log_view);
6964 if (err)
6965 return err;
6966 view->focus_child = 1;
6967 } else
6968 *new_view = log_view;
6969 break;
6970 case 't':
6971 view->count = 0;
6972 if (!s->selected_entry)
6973 break;
6974 if (view_is_parent_view(view))
6975 begin_x = view_split_begin_x(view->begin_x);
6976 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6977 s->repo);
6978 if (err || tree_view == NULL)
6979 break;
6980 view->focussed = 0;
6981 tree_view->focussed = 1;
6982 if (view_is_parent_view(view)) {
6983 err = view_close_child(view);
6984 if (err)
6985 return err;
6986 err = view_set_child(view, tree_view);
6987 if (err)
6988 return err;
6989 view->focus_child = 1;
6990 } else
6991 *new_view = tree_view;
6992 break;
6993 case 'g':
6994 case KEY_HOME:
6995 s->selected = 0;
6996 view->count = 0;
6997 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6998 break;
6999 case 'G':
7000 case KEY_END:
7001 s->selected = 0;
7002 view->count = 0;
7003 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7004 for (n = 0; n < view->nlines - 1; n++) {
7005 if (re == NULL)
7006 break;
7007 s->first_displayed_entry = re;
7008 re = TAILQ_PREV(re, tog_reflist_head, entry);
7010 if (n > 0)
7011 s->selected = n - 1;
7012 break;
7013 case 'k':
7014 case KEY_UP:
7015 case CTRL('p'):
7016 if (s->selected > 0) {
7017 s->selected--;
7018 break;
7020 ref_scroll_up(s, 1);
7021 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7022 view->count = 0;
7023 break;
7024 case CTRL('u'):
7025 case 'u':
7026 nscroll /= 2;
7027 /* FALL THROUGH */
7028 case KEY_PPAGE:
7029 case CTRL('b'):
7030 case 'b':
7031 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7032 s->selected -= MIN(nscroll, s->selected);
7033 ref_scroll_up(s, MAX(0, nscroll));
7034 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7035 view->count = 0;
7036 break;
7037 case 'j':
7038 case KEY_DOWN:
7039 case CTRL('n'):
7040 if (s->selected < s->ndisplayed - 1) {
7041 s->selected++;
7042 break;
7044 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7045 /* can't scroll any further */
7046 view->count = 0;
7047 break;
7049 ref_scroll_down(s, 1);
7050 break;
7051 case CTRL('d'):
7052 case 'd':
7053 nscroll /= 2;
7054 /* FALL THROUGH */
7055 case KEY_NPAGE:
7056 case CTRL('f'):
7057 case 'f':
7058 case ' ':
7059 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7060 /* can't scroll any further; move cursor down */
7061 if (s->selected < s->ndisplayed - 1)
7062 s->selected += MIN(nscroll,
7063 s->ndisplayed - s->selected - 1);
7064 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7065 s->selected += s->ndisplayed - s->selected - 1;
7066 view->count = 0;
7067 break;
7069 ref_scroll_down(s, nscroll);
7070 break;
7071 case CTRL('l'):
7072 view->count = 0;
7073 tog_free_refs();
7074 err = tog_load_refs(s->repo, s->sort_by_date);
7075 if (err)
7076 break;
7077 ref_view_free_refs(s);
7078 err = ref_view_load_refs(s);
7079 break;
7080 case KEY_RESIZE:
7081 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7082 s->selected = view->nlines - 2;
7083 break;
7084 default:
7085 view->count = 0;
7086 break;
7089 return err;
7092 __dead static void
7093 usage_ref(void)
7095 endwin();
7096 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7097 getprogname());
7098 exit(1);
7101 static const struct got_error *
7102 cmd_ref(int argc, char *argv[])
7104 const struct got_error *error;
7105 struct got_repository *repo = NULL;
7106 struct got_worktree *worktree = NULL;
7107 char *cwd = NULL, *repo_path = NULL;
7108 int ch;
7109 struct tog_view *view;
7110 int *pack_fds = NULL;
7112 while ((ch = getopt(argc, argv, "r:")) != -1) {
7113 switch (ch) {
7114 case 'r':
7115 repo_path = realpath(optarg, NULL);
7116 if (repo_path == NULL)
7117 return got_error_from_errno2("realpath",
7118 optarg);
7119 break;
7120 default:
7121 usage_ref();
7122 /* NOTREACHED */
7126 argc -= optind;
7127 argv += optind;
7129 if (argc > 1)
7130 usage_ref();
7132 error = got_repo_pack_fds_open(&pack_fds);
7133 if (error != NULL)
7134 goto done;
7136 if (repo_path == NULL) {
7137 cwd = getcwd(NULL, 0);
7138 if (cwd == NULL)
7139 return got_error_from_errno("getcwd");
7140 error = got_worktree_open(&worktree, cwd);
7141 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7142 goto done;
7143 if (worktree)
7144 repo_path =
7145 strdup(got_worktree_get_repo_path(worktree));
7146 else
7147 repo_path = strdup(cwd);
7148 if (repo_path == NULL) {
7149 error = got_error_from_errno("strdup");
7150 goto done;
7154 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7155 if (error != NULL)
7156 goto done;
7158 init_curses();
7160 error = apply_unveil(got_repo_get_path(repo), NULL);
7161 if (error)
7162 goto done;
7164 error = tog_load_refs(repo, 0);
7165 if (error)
7166 goto done;
7168 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7169 if (view == NULL) {
7170 error = got_error_from_errno("view_open");
7171 goto done;
7174 error = open_ref_view(view, repo);
7175 if (error)
7176 goto done;
7178 if (worktree) {
7179 /* Release work tree lock. */
7180 got_worktree_close(worktree);
7181 worktree = NULL;
7183 error = view_loop(view);
7184 done:
7185 free(repo_path);
7186 free(cwd);
7187 if (repo) {
7188 const struct got_error *close_err = got_repo_close(repo);
7189 if (close_err)
7190 error = close_err;
7192 if (pack_fds) {
7193 const struct got_error *pack_err =
7194 got_repo_pack_fds_close(pack_fds);
7195 if (error == NULL)
7196 error = pack_err;
7198 tog_free_refs();
7199 return error;
7202 static void
7203 list_commands(FILE *fp)
7205 size_t i;
7207 fprintf(fp, "commands:");
7208 for (i = 0; i < nitems(tog_commands); i++) {
7209 const struct tog_cmd *cmd = &tog_commands[i];
7210 fprintf(fp, " %s", cmd->name);
7212 fputc('\n', fp);
7215 __dead static void
7216 usage(int hflag, int status)
7218 FILE *fp = (status == 0) ? stdout : stderr;
7220 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7221 getprogname());
7222 if (hflag) {
7223 fprintf(fp, "lazy usage: %s path\n", getprogname());
7224 list_commands(fp);
7226 exit(status);
7229 static char **
7230 make_argv(int argc, ...)
7232 va_list ap;
7233 char **argv;
7234 int i;
7236 va_start(ap, argc);
7238 argv = calloc(argc, sizeof(char *));
7239 if (argv == NULL)
7240 err(1, "calloc");
7241 for (i = 0; i < argc; i++) {
7242 argv[i] = strdup(va_arg(ap, char *));
7243 if (argv[i] == NULL)
7244 err(1, "strdup");
7247 va_end(ap);
7248 return argv;
7252 * Try to convert 'tog path' into a 'tog log path' command.
7253 * The user could simply have mistyped the command rather than knowingly
7254 * provided a path. So check whether argv[0] can in fact be resolved
7255 * to a path in the HEAD commit and print a special error if not.
7256 * This hack is for mpi@ <3
7258 static const struct got_error *
7259 tog_log_with_path(int argc, char *argv[])
7261 const struct got_error *error = NULL, *close_err;
7262 const struct tog_cmd *cmd = NULL;
7263 struct got_repository *repo = NULL;
7264 struct got_worktree *worktree = NULL;
7265 struct got_object_id *commit_id = NULL, *id = NULL;
7266 struct got_commit_object *commit = NULL;
7267 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7268 char *commit_id_str = NULL, **cmd_argv = NULL;
7269 int *pack_fds = NULL;
7271 cwd = getcwd(NULL, 0);
7272 if (cwd == NULL)
7273 return got_error_from_errno("getcwd");
7275 error = got_repo_pack_fds_open(&pack_fds);
7276 if (error != NULL)
7277 goto done;
7279 error = got_worktree_open(&worktree, cwd);
7280 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7281 goto done;
7283 if (worktree)
7284 repo_path = strdup(got_worktree_get_repo_path(worktree));
7285 else
7286 repo_path = strdup(cwd);
7287 if (repo_path == NULL) {
7288 error = got_error_from_errno("strdup");
7289 goto done;
7292 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7293 if (error != NULL)
7294 goto done;
7296 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7297 repo, worktree);
7298 if (error)
7299 goto done;
7301 error = tog_load_refs(repo, 0);
7302 if (error)
7303 goto done;
7304 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7305 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7306 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7307 if (error)
7308 goto done;
7310 if (worktree) {
7311 got_worktree_close(worktree);
7312 worktree = NULL;
7315 error = got_object_open_as_commit(&commit, repo, commit_id);
7316 if (error)
7317 goto done;
7319 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7320 if (error) {
7321 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7322 goto done;
7323 fprintf(stderr, "%s: '%s' is no known command or path\n",
7324 getprogname(), argv[0]);
7325 usage(1, 1);
7326 /* not reached */
7329 close_err = got_repo_close(repo);
7330 if (error == NULL)
7331 error = close_err;
7332 repo = NULL;
7334 error = got_object_id_str(&commit_id_str, commit_id);
7335 if (error)
7336 goto done;
7338 cmd = &tog_commands[0]; /* log */
7339 argc = 4;
7340 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7341 error = cmd->cmd_main(argc, cmd_argv);
7342 done:
7343 if (repo) {
7344 close_err = got_repo_close(repo);
7345 if (error == NULL)
7346 error = close_err;
7348 if (commit)
7349 got_object_commit_close(commit);
7350 if (worktree)
7351 got_worktree_close(worktree);
7352 if (pack_fds) {
7353 const struct got_error *pack_err =
7354 got_repo_pack_fds_close(pack_fds);
7355 if (error == NULL)
7356 error = pack_err;
7358 free(id);
7359 free(commit_id_str);
7360 free(commit_id);
7361 free(cwd);
7362 free(repo_path);
7363 free(in_repo_path);
7364 if (cmd_argv) {
7365 int i;
7366 for (i = 0; i < argc; i++)
7367 free(cmd_argv[i]);
7368 free(cmd_argv);
7370 tog_free_refs();
7371 return error;
7374 int
7375 main(int argc, char *argv[])
7377 const struct got_error *error = NULL;
7378 const struct tog_cmd *cmd = NULL;
7379 int ch, hflag = 0, Vflag = 0;
7380 char **cmd_argv = NULL;
7381 static const struct option longopts[] = {
7382 { "version", no_argument, NULL, 'V' },
7383 { NULL, 0, NULL, 0}
7386 setlocale(LC_CTYPE, "");
7388 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7389 switch (ch) {
7390 case 'h':
7391 hflag = 1;
7392 break;
7393 case 'V':
7394 Vflag = 1;
7395 break;
7396 default:
7397 usage(hflag, 1);
7398 /* NOTREACHED */
7402 argc -= optind;
7403 argv += optind;
7404 optind = 1;
7405 optreset = 1;
7407 if (Vflag) {
7408 got_version_print_str();
7409 return 0;
7412 #ifndef PROFILE
7413 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7414 NULL) == -1)
7415 err(1, "pledge");
7416 #endif
7418 if (argc == 0) {
7419 if (hflag)
7420 usage(hflag, 0);
7421 /* Build an argument vector which runs a default command. */
7422 cmd = &tog_commands[0];
7423 argc = 1;
7424 cmd_argv = make_argv(argc, cmd->name);
7425 } else {
7426 size_t i;
7428 /* Did the user specify a command? */
7429 for (i = 0; i < nitems(tog_commands); i++) {
7430 if (strncmp(tog_commands[i].name, argv[0],
7431 strlen(argv[0])) == 0) {
7432 cmd = &tog_commands[i];
7433 break;
7438 if (cmd == NULL) {
7439 if (argc != 1)
7440 usage(0, 1);
7441 /* No command specified; try log with a path */
7442 error = tog_log_with_path(argc, argv);
7443 } else {
7444 if (hflag)
7445 cmd->cmd_usage();
7446 else
7447 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7450 endwin();
7451 putchar('\n');
7452 if (cmd_argv) {
7453 int i;
7454 for (i = 0; i < argc; i++)
7455 free(cmd_argv[i]);
7456 free(cmd_argv);
7459 if (error && error->code != GOT_ERR_CANCELLED)
7460 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7461 return 0;