Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/ioctl.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #if defined(__FreeBSD__) || defined(__APPLE__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #endif
25 #include <curses.h>
26 #include <panel.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
51 #include "got_diff.h"
52 #include "got_opentemp.h"
53 #include "got_utf8.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_path.h"
59 #include "got_worktree.h"
61 //#define update_panels() (0)
62 //#define doupdate() (0)
64 #ifndef MIN
65 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #endif
68 #ifndef MAX
69 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 #endif
72 #ifndef CTRL
73 #define CTRL(x) ((x) & 0x1f)
74 #endif
76 #ifndef nitems
77 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
78 #endif
80 struct tog_cmd {
81 const char *name;
82 const struct got_error *(*cmd_main)(int, char *[]);
83 void (*cmd_usage)(void);
84 };
86 __dead static void usage(int, int);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_ref(void);
93 static const struct got_error* cmd_log(int, char *[]);
94 static const struct got_error* cmd_diff(int, char *[]);
95 static const struct got_error* cmd_blame(int, char *[]);
96 static const struct got_error* cmd_tree(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
99 static const struct tog_cmd tog_commands[] = {
100 { "log", cmd_log, usage_log },
101 { "diff", cmd_diff, usage_diff },
102 { "blame", cmd_blame, usage_blame },
103 { "tree", cmd_tree, usage_tree },
104 { "ref", cmd_ref, usage_ref },
105 };
107 enum tog_view_type {
108 TOG_VIEW_DIFF,
109 TOG_VIEW_LOG,
110 TOG_VIEW_BLAME,
111 TOG_VIEW_TREE,
112 TOG_VIEW_REF,
113 };
115 #define TOG_EOF_STRING "(END)"
117 struct commit_queue_entry {
118 TAILQ_ENTRY(commit_queue_entry) entry;
119 struct got_object_id *id;
120 struct got_commit_object *commit;
121 int idx;
122 };
123 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 struct commit_queue {
125 int ncommits;
126 struct commit_queue_head head;
127 };
129 struct tog_color {
130 STAILQ_ENTRY(tog_color) entry;
131 regex_t regex;
132 short colorpair;
133 };
134 STAILQ_HEAD(tog_colors, tog_color);
136 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static const struct got_error *
140 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
141 struct got_reference* re2)
143 const char *name1 = got_ref_get_name(re1);
144 const char *name2 = got_ref_get_name(re2);
145 int isbackup1, isbackup2;
147 /* Sort backup refs towards the bottom of the list. */
148 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
149 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
150 if (!isbackup1 && isbackup2) {
151 *cmp = -1;
152 return NULL;
153 } else if (isbackup1 && !isbackup2) {
154 *cmp = 1;
155 return NULL;
158 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
159 return NULL;
162 static const struct got_error *
163 tog_load_refs(struct got_repository *repo, int sort_by_date)
165 const struct got_error *err;
167 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
168 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
169 repo);
170 if (err)
171 return err;
173 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
174 repo);
177 static void
178 tog_free_refs(void)
180 if (tog_refs_idmap) {
181 got_reflist_object_id_map_free(tog_refs_idmap);
182 tog_refs_idmap = NULL;
184 got_ref_list_free(&tog_refs);
187 static const struct got_error *
188 add_color(struct tog_colors *colors, const char *pattern,
189 int idx, short color)
191 const struct got_error *err = NULL;
192 struct tog_color *tc;
193 int regerr = 0;
195 if (idx < 1 || idx > COLOR_PAIRS - 1)
196 return NULL;
198 init_pair(idx, color, -1);
200 tc = calloc(1, sizeof(*tc));
201 if (tc == NULL)
202 return got_error_from_errno("calloc");
203 regerr = regcomp(&tc->regex, pattern,
204 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
205 if (regerr) {
206 static char regerr_msg[512];
207 static char err_msg[512];
208 regerror(regerr, &tc->regex, regerr_msg,
209 sizeof(regerr_msg));
210 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
211 regerr_msg);
212 err = got_error_msg(GOT_ERR_REGEX, err_msg);
213 free(tc);
214 return err;
216 tc->colorpair = idx;
217 STAILQ_INSERT_HEAD(colors, tc, entry);
218 return NULL;
221 static void
222 free_colors(struct tog_colors *colors)
224 struct tog_color *tc;
226 while (!STAILQ_EMPTY(colors)) {
227 tc = STAILQ_FIRST(colors);
228 STAILQ_REMOVE_HEAD(colors, entry);
229 regfree(&tc->regex);
230 free(tc);
234 struct tog_color *
235 get_color(struct tog_colors *colors, int colorpair)
237 struct tog_color *tc = NULL;
239 STAILQ_FOREACH(tc, colors, entry) {
240 if (tc->colorpair == colorpair)
241 return tc;
244 return NULL;
247 static int
248 default_color_value(const char *envvar)
250 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
251 return COLOR_MAGENTA;
252 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
253 return COLOR_CYAN;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
255 return COLOR_YELLOW;
256 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
257 return COLOR_GREEN;
258 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
259 return COLOR_MAGENTA;
260 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
263 return COLOR_CYAN;
264 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
265 return COLOR_GREEN;
266 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
267 return COLOR_GREEN;
268 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
269 return COLOR_CYAN;
270 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
271 return COLOR_YELLOW;
272 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
273 return COLOR_GREEN;
274 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
275 return COLOR_MAGENTA;
276 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
277 return COLOR_YELLOW;
278 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
279 return COLOR_CYAN;
281 return -1;
284 static int
285 get_color_value(const char *envvar)
287 const char *val = getenv(envvar);
289 if (val == NULL)
290 return default_color_value(envvar);
292 if (strcasecmp(val, "black") == 0)
293 return COLOR_BLACK;
294 if (strcasecmp(val, "red") == 0)
295 return COLOR_RED;
296 if (strcasecmp(val, "green") == 0)
297 return COLOR_GREEN;
298 if (strcasecmp(val, "yellow") == 0)
299 return COLOR_YELLOW;
300 if (strcasecmp(val, "blue") == 0)
301 return COLOR_BLUE;
302 if (strcasecmp(val, "magenta") == 0)
303 return COLOR_MAGENTA;
304 if (strcasecmp(val, "cyan") == 0)
305 return COLOR_CYAN;
306 if (strcasecmp(val, "white") == 0)
307 return COLOR_WHITE;
308 if (strcasecmp(val, "default") == 0)
309 return -1;
311 return default_color_value(envvar);
315 struct tog_diff_view_state {
316 struct got_object_id *id1, *id2;
317 const char *label1, *label2;
318 FILE *f, *f1, *f2;
319 int first_displayed_line;
320 int last_displayed_line;
321 int eof;
322 int diff_context;
323 int ignore_whitespace;
324 int force_text_diff;
325 struct got_repository *repo;
326 struct tog_colors colors;
327 size_t nlines;
328 off_t *line_offsets;
329 int matched_line;
330 int selected_line;
332 /* passed from log view; may be NULL */
333 struct tog_view *log_view;
334 };
336 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
338 struct tog_log_thread_args {
339 pthread_cond_t need_commits;
340 pthread_cond_t commit_loaded;
341 int commits_needed;
342 int load_all;
343 struct got_commit_graph *graph;
344 struct commit_queue *commits;
345 const char *in_repo_path;
346 struct got_object_id *start_id;
347 struct got_repository *repo;
348 int *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 focussed; /* Only set on one parent or child view at a time. */
513 int dying;
514 struct tog_view *parent;
515 struct tog_view *child;
517 /*
518 * This flag is initially set on parent views when a new child view
519 * is created. It gets toggled when the 'Tab' key switches focus
520 * between parent and child.
521 * The flag indicates whether focus should be passed on to our child
522 * view if this parent view gets picked for focus after another parent
523 * view was closed. This prevents child views from losing focus in such
524 * situations.
525 */
526 int focus_child;
528 /* type-specific state */
529 enum tog_view_type type;
530 union {
531 struct tog_diff_view_state diff;
532 struct tog_log_view_state log;
533 struct tog_blame_view_state blame;
534 struct tog_tree_view_state tree;
535 struct tog_ref_view_state ref;
536 } state;
538 const struct got_error *(*show)(struct tog_view *);
539 const struct got_error *(*input)(struct tog_view **,
540 struct tog_view *, int);
541 const struct got_error *(*close)(struct tog_view *);
543 const struct got_error *(*search_start)(struct tog_view *);
544 const struct got_error *(*search_next)(struct tog_view *);
545 int search_started;
546 int searching;
547 #define TOG_SEARCH_FORWARD 1
548 #define TOG_SEARCH_BACKWARD 2
549 int search_next_done;
550 #define TOG_SEARCH_HAVE_MORE 1
551 #define TOG_SEARCH_NO_MORE 2
552 #define TOG_SEARCH_HAVE_NONE 3
553 regex_t regex;
554 regmatch_t regmatch;
555 };
557 static const struct got_error *open_diff_view(struct tog_view *,
558 struct got_object_id *, struct got_object_id *,
559 const char *, const char *, int, int, int, struct tog_view *,
560 struct got_repository *);
561 static const struct got_error *show_diff_view(struct tog_view *);
562 static const struct got_error *input_diff_view(struct tog_view **,
563 struct tog_view *, int);
564 static const struct got_error* close_diff_view(struct tog_view *);
565 static const struct got_error *search_start_diff_view(struct tog_view *);
566 static const struct got_error *search_next_diff_view(struct tog_view *);
568 static const struct got_error *open_log_view(struct tog_view *,
569 struct got_object_id *, struct got_repository *,
570 const char *, const char *, int);
571 static const struct got_error * show_log_view(struct tog_view *);
572 static const struct got_error *input_log_view(struct tog_view **,
573 struct tog_view *, int);
574 static const struct got_error *close_log_view(struct tog_view *);
575 static const struct got_error *search_start_log_view(struct tog_view *);
576 static const struct got_error *search_next_log_view(struct tog_view *);
578 static const struct got_error *open_blame_view(struct tog_view *, char *,
579 struct got_object_id *, struct got_repository *);
580 static const struct got_error *show_blame_view(struct tog_view *);
581 static const struct got_error *input_blame_view(struct tog_view **,
582 struct tog_view *, int);
583 static const struct got_error *close_blame_view(struct tog_view *);
584 static const struct got_error *search_start_blame_view(struct tog_view *);
585 static const struct got_error *search_next_blame_view(struct tog_view *);
587 static const struct got_error *open_tree_view(struct tog_view *,
588 struct got_object_id *, const char *, struct got_repository *);
589 static const struct got_error *show_tree_view(struct tog_view *);
590 static const struct got_error *input_tree_view(struct tog_view **,
591 struct tog_view *, int);
592 static const struct got_error *close_tree_view(struct tog_view *);
593 static const struct got_error *search_start_tree_view(struct tog_view *);
594 static const struct got_error *search_next_tree_view(struct tog_view *);
596 static const struct got_error *open_ref_view(struct tog_view *,
597 struct got_repository *);
598 static const struct got_error *show_ref_view(struct tog_view *);
599 static const struct got_error *input_ref_view(struct tog_view **,
600 struct tog_view *, int);
601 static const struct got_error *close_ref_view(struct tog_view *);
602 static const struct got_error *search_start_ref_view(struct tog_view *);
603 static const struct got_error *search_next_ref_view(struct tog_view *);
605 static volatile sig_atomic_t tog_sigwinch_received;
606 static volatile sig_atomic_t tog_sigpipe_received;
607 static volatile sig_atomic_t tog_sigcont_received;
608 static volatile sig_atomic_t tog_sigint_received;
609 static volatile sig_atomic_t tog_sigterm_received;
611 static void
612 tog_sigwinch(int signo)
614 tog_sigwinch_received = 1;
617 static void
618 tog_sigpipe(int signo)
620 tog_sigpipe_received = 1;
623 static void
624 tog_sigcont(int signo)
626 tog_sigcont_received = 1;
629 static void
630 tog_sigint(int signo)
632 tog_sigint_received = 1;
635 static void
636 tog_sigterm(int signo)
638 tog_sigterm_received = 1;
641 static int
642 tog_fatal_signal_received(void)
644 return (tog_sigpipe_received ||
645 tog_sigint_received || tog_sigint_received);
649 static const struct got_error *
650 view_close(struct tog_view *view)
652 const struct got_error *err = NULL;
654 if (view->child) {
655 view_close(view->child);
656 view->child = NULL;
658 if (view->close)
659 err = view->close(view);
660 if (view->panel)
661 del_panel(view->panel);
662 if (view->window)
663 delwin(view->window);
664 free(view);
665 return err;
668 static struct tog_view *
669 view_open(int nlines, int ncols, int begin_y, int begin_x,
670 enum tog_view_type type)
672 struct tog_view *view = calloc(1, sizeof(*view));
674 if (view == NULL)
675 return NULL;
677 view->type = type;
678 view->lines = LINES;
679 view->cols = COLS;
680 view->nlines = nlines ? nlines : LINES - begin_y;
681 view->ncols = ncols ? ncols : COLS - begin_x;
682 view->begin_y = begin_y;
683 view->begin_x = begin_x;
684 view->window = newwin(nlines, ncols, begin_y, begin_x);
685 if (view->window == NULL) {
686 view_close(view);
687 return NULL;
689 view->panel = new_panel(view->window);
690 if (view->panel == NULL ||
691 set_panel_userptr(view->panel, view) != OK) {
692 view_close(view);
693 return NULL;
696 keypad(view->window, TRUE);
697 return view;
700 static int
701 view_split_begin_x(int begin_x)
703 if (begin_x > 0 || COLS < 120)
704 return 0;
705 return (COLS - MAX(COLS / 2, 80));
708 static const struct got_error *view_resize(struct tog_view *);
710 static const struct got_error *
711 view_splitscreen(struct tog_view *view)
713 const struct got_error *err = NULL;
715 view->begin_y = 0;
716 view->begin_x = view_split_begin_x(0);
717 view->nlines = LINES;
718 view->ncols = COLS - view->begin_x;
719 view->lines = LINES;
720 view->cols = COLS;
721 err = view_resize(view);
722 if (err)
723 return err;
725 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
726 return got_error_from_errno("mvwin");
728 return NULL;
731 static const struct got_error *
732 view_fullscreen(struct tog_view *view)
734 const struct got_error *err = NULL;
736 view->begin_x = 0;
737 view->begin_y = 0;
738 view->nlines = LINES;
739 view->ncols = COLS;
740 view->lines = LINES;
741 view->cols = COLS;
742 err = view_resize(view);
743 if (err)
744 return err;
746 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
747 return got_error_from_errno("mvwin");
749 return NULL;
752 static int
753 view_is_parent_view(struct tog_view *view)
755 return view->parent == NULL;
758 static int
759 view_is_splitscreen(struct tog_view *view)
761 return view->begin_x > 0;
765 static const struct got_error *
766 view_resize(struct tog_view *view)
768 int nlines, ncols;
770 if (view->lines > LINES)
771 nlines = view->nlines - (view->lines - LINES);
772 else
773 nlines = view->nlines + (LINES - view->lines);
775 if (view->cols > COLS)
776 ncols = view->ncols - (view->cols - COLS);
777 else
778 ncols = view->ncols + (COLS - view->cols);
780 if (view->child && view_is_splitscreen(view->child)) {
781 view->child->begin_x = view_split_begin_x(view->begin_x);
782 if (view->child->begin_x == 0) {
783 ncols = COLS;
785 view_fullscreen(view->child);
786 if (view->child->focussed)
787 show_panel(view->child->panel);
788 else
789 show_panel(view->panel);
790 } else {
791 ncols = view->child->begin_x;
793 view_splitscreen(view->child);
794 show_panel(view->child->panel);
796 } else if (view->parent == NULL)
797 ncols = COLS;
799 if (wresize(view->window, nlines, ncols) == ERR)
800 return got_error_from_errno("wresize");
801 if (replace_panel(view->panel, view->window) == ERR)
802 return got_error_from_errno("replace_panel");
803 wclear(view->window);
805 view->nlines = nlines;
806 view->ncols = ncols;
807 view->lines = LINES;
808 view->cols = COLS;
810 return NULL;
813 static const struct got_error *
814 view_close_child(struct tog_view *view)
816 const struct got_error *err = NULL;
818 if (view->child == NULL)
819 return NULL;
821 err = view_close(view->child);
822 view->child = NULL;
823 return err;
826 static const struct got_error *
827 view_set_child(struct tog_view *view, struct tog_view *child)
829 view->child = child;
830 child->parent = view;
832 return view_resize(view);
835 static void
836 tog_resizeterm(void)
838 int cols, lines;
839 struct winsize size;
841 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
842 cols = 80; /* Default */
843 lines = 24;
844 } else {
845 cols = size.ws_col;
846 lines = size.ws_row;
848 resize_term(lines, cols);
851 static const struct got_error *
852 view_search_start(struct tog_view *view)
854 const struct got_error *err = NULL;
855 char pattern[1024];
856 int ret;
858 if (view->search_started) {
859 regfree(&view->regex);
860 view->searching = 0;
861 memset(&view->regmatch, 0, sizeof(view->regmatch));
863 view->search_started = 0;
865 if (view->nlines < 1)
866 return NULL;
868 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
869 wclrtoeol(view->window);
871 nocbreak();
872 echo();
873 ret = wgetnstr(view->window, pattern, sizeof(pattern));
874 cbreak();
875 noecho();
876 if (ret == ERR)
877 return NULL;
879 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
880 err = view->search_start(view);
881 if (err) {
882 regfree(&view->regex);
883 return err;
885 view->search_started = 1;
886 view->searching = TOG_SEARCH_FORWARD;
887 view->search_next_done = 0;
888 view->search_next(view);
891 return NULL;
894 static const struct got_error *
895 view_input(struct tog_view **new, int *done, struct tog_view *view,
896 struct tog_view_list_head *views)
898 const struct got_error *err = NULL;
899 struct tog_view *v;
900 int ch, errcode;
902 *new = NULL;
904 /* Clear "no matches" indicator. */
905 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
906 view->search_next_done == TOG_SEARCH_HAVE_NONE)
907 view->search_next_done = TOG_SEARCH_HAVE_MORE;
909 if (view->searching && !view->search_next_done) {
910 errcode = pthread_mutex_unlock(&tog_mutex);
911 if (errcode)
912 return got_error_set_errno(errcode,
913 "pthread_mutex_unlock");
914 sched_yield();
915 errcode = pthread_mutex_lock(&tog_mutex);
916 if (errcode)
917 return got_error_set_errno(errcode,
918 "pthread_mutex_lock");
919 view->search_next(view);
920 return NULL;
923 nodelay(stdscr, FALSE);
924 /* Allow threads to make progress while we are waiting for input. */
925 errcode = pthread_mutex_unlock(&tog_mutex);
926 if (errcode)
927 return got_error_set_errno(errcode, "pthread_mutex_unlock");
928 ch = wgetch(view->window);
929 errcode = pthread_mutex_lock(&tog_mutex);
930 if (errcode)
931 return got_error_set_errno(errcode, "pthread_mutex_lock");
932 nodelay(stdscr, TRUE);
934 if (tog_sigwinch_received || tog_sigcont_received) {
935 tog_resizeterm();
936 tog_sigwinch_received = 0;
937 tog_sigcont_received = 0;
938 TAILQ_FOREACH(v, views, entry) {
939 err = view_resize(v);
940 if (err)
941 return err;
942 err = v->input(new, v, KEY_RESIZE);
943 if (err)
944 return err;
945 if (v->child) {
946 err = view_resize(v->child);
947 if (err)
948 return err;
949 err = v->child->input(new, v->child,
950 KEY_RESIZE);
951 if (err)
952 return err;
957 switch (ch) {
958 case '\t':
959 if (view->child) {
960 view->focussed = 0;
961 view->child->focussed = 1;
962 view->focus_child = 1;
963 } else if (view->parent) {
964 view->focussed = 0;
965 view->parent->focussed = 1;
966 view->parent->focus_child = 0;
967 if (!view_is_splitscreen(view))
968 err = view_fullscreen(view->parent);
970 break;
971 case 'q':
972 err = view->input(new, view, ch);
973 view->dying = 1;
974 break;
975 case 'Q':
976 *done = 1;
977 break;
978 case 'F':
979 if (view_is_parent_view(view)) {
980 if (view->child == NULL)
981 break;
982 if (view_is_splitscreen(view->child)) {
983 view->focussed = 0;
984 view->child->focussed = 1;
985 err = view_fullscreen(view->child);
986 } else
987 err = view_splitscreen(view->child);
988 if (err)
989 break;
990 err = view->child->input(new, view->child,
991 KEY_RESIZE);
992 } else {
993 if (view_is_splitscreen(view)) {
994 view->parent->focussed = 0;
995 view->focussed = 1;
996 err = view_fullscreen(view);
997 } else {
998 err = view_splitscreen(view);
999 if (!err)
1000 err = view_resize(view->parent);
1002 if (err)
1003 break;
1004 err = view->input(new, view, KEY_RESIZE);
1006 break;
1007 case KEY_RESIZE:
1008 break;
1009 case '/':
1010 if (view->search_start)
1011 view_search_start(view);
1012 else
1013 err = view->input(new, view, ch);
1014 break;
1015 case 'N':
1016 case 'n':
1017 if (view->search_started && view->search_next) {
1018 view->searching = (ch == 'n' ?
1019 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1020 view->search_next_done = 0;
1021 view->search_next(view);
1022 } else
1023 err = view->input(new, view, ch);
1024 break;
1025 default:
1026 err = view->input(new, view, ch);
1027 break;
1030 return err;
1033 void
1034 view_vborder(struct tog_view *view)
1036 PANEL *panel;
1037 const struct tog_view *view_above;
1039 if (view->parent)
1040 return view_vborder(view->parent);
1042 panel = panel_above(view->panel);
1043 if (panel == NULL)
1044 return;
1046 view_above = panel_userptr(panel);
1047 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1048 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1051 int
1052 view_needs_focus_indication(struct tog_view *view)
1054 if (view_is_parent_view(view)) {
1055 if (view->child == NULL || view->child->focussed)
1056 return 0;
1057 if (!view_is_splitscreen(view->child))
1058 return 0;
1059 } else if (!view_is_splitscreen(view))
1060 return 0;
1062 return view->focussed;
1065 static const struct got_error *
1066 view_loop(struct tog_view *view)
1068 const struct got_error *err = NULL;
1069 struct tog_view_list_head views;
1070 struct tog_view *new_view;
1071 int fast_refresh = 10;
1072 int done = 0, errcode;
1074 errcode = pthread_mutex_lock(&tog_mutex);
1075 if (errcode)
1076 return got_error_set_errno(errcode, "pthread_mutex_lock");
1078 TAILQ_INIT(&views);
1079 TAILQ_INSERT_HEAD(&views, view, entry);
1081 view->focussed = 1;
1082 err = view->show(view);
1083 if (err)
1084 return err;
1085 update_panels();
1086 doupdate();
1087 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1088 /* Refresh fast during initialization, then become slower. */
1089 if (fast_refresh && fast_refresh-- == 0)
1090 halfdelay(10); /* switch to once per second */
1092 err = view_input(&new_view, &done, view, &views);
1093 if (err)
1094 break;
1095 if (view->dying) {
1096 struct tog_view *v, *prev = NULL;
1098 if (view_is_parent_view(view))
1099 prev = TAILQ_PREV(view, tog_view_list_head,
1100 entry);
1101 else if (view->parent)
1102 prev = view->parent;
1104 if (view->parent) {
1105 view->parent->child = NULL;
1106 view->parent->focus_child = 0;
1108 err = view_resize(view->parent);
1109 if (err)
1110 break;
1111 } else
1112 TAILQ_REMOVE(&views, view, entry);
1114 err = view_close(view);
1115 if (err)
1116 goto done;
1118 view = NULL;
1119 TAILQ_FOREACH(v, &views, entry) {
1120 if (v->focussed)
1121 break;
1123 if (view == NULL && new_view == NULL) {
1124 /* No view has focus. Try to pick one. */
1125 if (prev)
1126 view = prev;
1127 else if (!TAILQ_EMPTY(&views)) {
1128 view = TAILQ_LAST(&views,
1129 tog_view_list_head);
1131 if (view) {
1132 if (view->focus_child) {
1133 view->child->focussed = 1;
1134 view = view->child;
1135 } else
1136 view->focussed = 1;
1140 if (new_view) {
1141 struct tog_view *v, *t;
1142 /* Only allow one parent view per type. */
1143 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1144 if (v->type != new_view->type)
1145 continue;
1146 TAILQ_REMOVE(&views, v, entry);
1147 err = view_close(v);
1148 if (err)
1149 goto done;
1150 break;
1152 TAILQ_INSERT_TAIL(&views, new_view, entry);
1153 view = new_view;
1155 if (view) {
1156 if (view_is_parent_view(view)) {
1157 if (view->child && view->child->focussed)
1158 view = view->child;
1159 } else {
1160 if (view->parent && view->parent->focussed)
1161 view = view->parent;
1163 show_panel(view->panel);
1164 if (view->child && view_is_splitscreen(view->child))
1165 show_panel(view->child->panel);
1166 if (view->parent && view_is_splitscreen(view)) {
1167 err = view->parent->show(view->parent);
1168 if (err)
1169 goto done;
1171 err = view->show(view);
1172 if (err)
1173 goto done;
1174 if (view->child) {
1175 err = view->child->show(view->child);
1176 if (err)
1177 goto done;
1179 update_panels();
1180 doupdate();
1183 done:
1184 while (!TAILQ_EMPTY(&views)) {
1185 view = TAILQ_FIRST(&views);
1186 TAILQ_REMOVE(&views, view, entry);
1187 view_close(view);
1190 errcode = pthread_mutex_unlock(&tog_mutex);
1191 if (errcode && err == NULL)
1192 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1194 return err;
1197 __dead static void
1198 usage_log(void)
1200 endwin();
1201 fprintf(stderr,
1202 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1203 getprogname());
1204 exit(1);
1207 /* Create newly allocated wide-character string equivalent to a byte string. */
1208 static const struct got_error *
1209 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1211 char *vis = NULL;
1212 const struct got_error *err = NULL;
1214 *ws = NULL;
1215 *wlen = mbstowcs(NULL, s, 0);
1216 if (*wlen == (size_t)-1) {
1217 int vislen;
1218 if (errno != EILSEQ)
1219 return got_error_from_errno("mbstowcs");
1221 /* byte string invalid in current encoding; try to "fix" it */
1222 err = got_mbsavis(&vis, &vislen, s);
1223 if (err)
1224 return err;
1225 *wlen = mbstowcs(NULL, vis, 0);
1226 if (*wlen == (size_t)-1) {
1227 err = got_error_from_errno("mbstowcs"); /* give up */
1228 goto done;
1232 *ws = calloc(*wlen + 1, sizeof(**ws));
1233 if (*ws == NULL) {
1234 err = got_error_from_errno("calloc");
1235 goto done;
1238 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1239 err = got_error_from_errno("mbstowcs");
1240 done:
1241 free(vis);
1242 if (err) {
1243 free(*ws);
1244 *ws = NULL;
1245 *wlen = 0;
1247 return err;
1250 static const struct got_error *
1251 expand_tab(char **ptr, const char *src)
1253 char *dst;
1254 size_t len, n, idx = 0, sz = 0;
1256 *ptr = NULL;
1257 n = len = strlen(src);
1258 dst = malloc(n + 1);
1259 if (dst == NULL)
1260 return got_error_from_errno("malloc");
1262 while (idx < len && src[idx]) {
1263 const char c = src[idx];
1265 if (c == '\t') {
1266 size_t nb = TABSIZE - sz % TABSIZE;
1267 char *p;
1269 p = realloc(dst, n + nb);
1270 if (p == NULL) {
1271 free(dst);
1272 return got_error_from_errno("realloc");
1275 dst = p;
1276 n += nb;
1277 memset(dst + sz, ' ', nb);
1278 sz += nb;
1279 } else
1280 dst[sz++] = src[idx];
1281 ++idx;
1284 dst[sz] = '\0';
1285 *ptr = dst;
1286 return NULL;
1290 * Advance at most n columns from wline starting at offset off.
1291 * Return the index to the first character after the span operation.
1292 * Return the combined column width of all spanned wide character in
1293 * *rcol.
1295 static int
1296 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1298 int width, i, cols = 0;
1300 if (n == 0) {
1301 *rcol = cols;
1302 return off;
1305 for (i = off; wline[i] != L'\0'; ++i) {
1306 if (wline[i] == L'\t')
1307 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1308 else
1309 width = wcwidth(wline[i]);
1311 if (width == -1) {
1312 width = 1;
1313 wline[i] = L'.';
1316 if (cols + width > n)
1317 break;
1318 cols += width;
1321 *rcol = cols;
1322 return i;
1326 * Format a line for display, ensuring that it won't overflow a width limit.
1327 * With scrolling, the width returned refers to the scrolled version of the
1328 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1330 static const struct got_error *
1331 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1332 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1334 const struct got_error *err = NULL;
1335 int cols;
1336 wchar_t *wline = NULL;
1337 char *exstr = NULL;
1338 size_t wlen;
1339 int i, scrollx;
1341 *wlinep = NULL;
1342 *widthp = 0;
1344 if (expand) {
1345 err = expand_tab(&exstr, line);
1346 if (err)
1347 return err;
1350 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1351 free(exstr);
1352 if (err)
1353 return err;
1355 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1357 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1358 wline[wlen - 1] = L'\0';
1359 wlen--;
1361 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1362 wline[wlen - 1] = L'\0';
1363 wlen--;
1366 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1367 wline[i] = L'\0';
1369 if (widthp)
1370 *widthp = cols;
1371 if (scrollxp)
1372 *scrollxp = scrollx;
1373 if (err)
1374 free(wline);
1375 else
1376 *wlinep = wline;
1377 return err;
1380 static const struct got_error*
1381 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1382 struct got_object_id *id, struct got_repository *repo)
1384 static const struct got_error *err = NULL;
1385 struct got_reflist_entry *re;
1386 char *s;
1387 const char *name;
1389 *refs_str = NULL;
1391 TAILQ_FOREACH(re, refs, entry) {
1392 struct got_tag_object *tag = NULL;
1393 struct got_object_id *ref_id;
1394 int cmp;
1396 name = got_ref_get_name(re->ref);
1397 if (strcmp(name, GOT_REF_HEAD) == 0)
1398 continue;
1399 if (strncmp(name, "refs/", 5) == 0)
1400 name += 5;
1401 if (strncmp(name, "got/", 4) == 0 &&
1402 strncmp(name, "got/backup/", 11) != 0)
1403 continue;
1404 if (strncmp(name, "heads/", 6) == 0)
1405 name += 6;
1406 if (strncmp(name, "remotes/", 8) == 0) {
1407 name += 8;
1408 s = strstr(name, "/" GOT_REF_HEAD);
1409 if (s != NULL && s[strlen(s)] == '\0')
1410 continue;
1412 err = got_ref_resolve(&ref_id, repo, re->ref);
1413 if (err)
1414 break;
1415 if (strncmp(name, "tags/", 5) == 0) {
1416 err = got_object_open_as_tag(&tag, repo, ref_id);
1417 if (err) {
1418 if (err->code != GOT_ERR_OBJ_TYPE) {
1419 free(ref_id);
1420 break;
1422 /* Ref points at something other than a tag. */
1423 err = NULL;
1424 tag = NULL;
1427 cmp = got_object_id_cmp(tag ?
1428 got_object_tag_get_object_id(tag) : ref_id, id);
1429 free(ref_id);
1430 if (tag)
1431 got_object_tag_close(tag);
1432 if (cmp != 0)
1433 continue;
1434 s = *refs_str;
1435 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1436 s ? ", " : "", name) == -1) {
1437 err = got_error_from_errno("asprintf");
1438 free(s);
1439 *refs_str = NULL;
1440 break;
1442 free(s);
1445 return err;
1448 static const struct got_error *
1449 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1450 int col_tab_align)
1452 char *smallerthan;
1454 smallerthan = strchr(author, '<');
1455 if (smallerthan && smallerthan[1] != '\0')
1456 author = smallerthan + 1;
1457 author[strcspn(author, "@>")] = '\0';
1458 return format_line(wauthor, author_width, NULL, author, 0, limit,
1459 col_tab_align, 0);
1462 static const struct got_error *
1463 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1464 struct got_object_id *id, const size_t date_display_cols,
1465 int author_display_cols)
1467 struct tog_log_view_state *s = &view->state.log;
1468 const struct got_error *err = NULL;
1469 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1470 char *logmsg0 = NULL, *logmsg = NULL;
1471 char *author = NULL;
1472 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1473 int author_width, logmsg_width;
1474 char *newline, *line = NULL;
1475 int col, limit, scrollx;
1476 const int avail = view->ncols;
1477 struct tm tm;
1478 time_t committer_time;
1479 struct tog_color *tc;
1481 committer_time = got_object_commit_get_committer_time(commit);
1482 if (gmtime_r(&committer_time, &tm) == NULL)
1483 return got_error_from_errno("gmtime_r");
1484 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1485 return got_error(GOT_ERR_NO_SPACE);
1487 if (avail <= date_display_cols)
1488 limit = MIN(sizeof(datebuf) - 1, avail);
1489 else
1490 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1491 tc = get_color(&s->colors, TOG_COLOR_DATE);
1492 if (tc)
1493 wattr_on(view->window,
1494 COLOR_PAIR(tc->colorpair), NULL);
1495 waddnstr(view->window, datebuf, limit);
1496 if (tc)
1497 wattr_off(view->window,
1498 COLOR_PAIR(tc->colorpair), NULL);
1499 col = limit;
1500 if (col > avail)
1501 goto done;
1503 if (avail >= 120) {
1504 char *id_str;
1505 err = got_object_id_str(&id_str, id);
1506 if (err)
1507 goto done;
1508 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1509 if (tc)
1510 wattr_on(view->window,
1511 COLOR_PAIR(tc->colorpair), NULL);
1512 wprintw(view->window, "%.8s ", id_str);
1513 if (tc)
1514 wattr_off(view->window,
1515 COLOR_PAIR(tc->colorpair), NULL);
1516 free(id_str);
1517 col += 9;
1518 if (col > avail)
1519 goto done;
1522 author = strdup(got_object_commit_get_author(commit));
1523 if (author == NULL) {
1524 err = got_error_from_errno("strdup");
1525 goto done;
1527 err = format_author(&wauthor, &author_width, author, avail - col, col);
1528 if (err)
1529 goto done;
1530 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1531 if (tc)
1532 wattr_on(view->window,
1533 COLOR_PAIR(tc->colorpair), NULL);
1534 waddwstr(view->window, wauthor);
1535 if (tc)
1536 wattr_off(view->window,
1537 COLOR_PAIR(tc->colorpair), NULL);
1538 col += author_width;
1539 while (col < avail && author_width < author_display_cols + 2) {
1540 waddch(view->window, ' ');
1541 col++;
1542 author_width++;
1544 if (col > avail)
1545 goto done;
1547 err = got_object_commit_get_logmsg(&logmsg0, commit);
1548 if (err)
1549 goto done;
1550 logmsg = logmsg0;
1551 while (*logmsg == '\n')
1552 logmsg++;
1553 newline = strchr(logmsg, '\n');
1554 if (newline)
1555 *newline = '\0';
1556 limit = avail - col;
1557 if (view->child && view_is_splitscreen(view->child) && limit > 0)
1558 limit--; /* for the border */
1559 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1560 limit, col, 1);
1561 if (err)
1562 goto done;
1563 waddwstr(view->window, &wlogmsg[scrollx]);
1564 col += MAX(logmsg_width, 0);
1565 while (col < avail) {
1566 waddch(view->window, ' ');
1567 col++;
1569 done:
1570 free(logmsg0);
1571 free(wlogmsg);
1572 free(author);
1573 free(wauthor);
1574 free(line);
1575 return err;
1578 static struct commit_queue_entry *
1579 alloc_commit_queue_entry(struct got_commit_object *commit,
1580 struct got_object_id *id)
1582 struct commit_queue_entry *entry;
1584 entry = calloc(1, sizeof(*entry));
1585 if (entry == NULL)
1586 return NULL;
1588 entry->id = id;
1589 entry->commit = commit;
1590 return entry;
1593 static void
1594 pop_commit(struct commit_queue *commits)
1596 struct commit_queue_entry *entry;
1598 entry = TAILQ_FIRST(&commits->head);
1599 TAILQ_REMOVE(&commits->head, entry, entry);
1600 got_object_commit_close(entry->commit);
1601 commits->ncommits--;
1602 /* Don't free entry->id! It is owned by the commit graph. */
1603 free(entry);
1606 static void
1607 free_commits(struct commit_queue *commits)
1609 while (!TAILQ_EMPTY(&commits->head))
1610 pop_commit(commits);
1613 static const struct got_error *
1614 match_commit(int *have_match, struct got_object_id *id,
1615 struct got_commit_object *commit, regex_t *regex)
1617 const struct got_error *err = NULL;
1618 regmatch_t regmatch;
1619 char *id_str = NULL, *logmsg = NULL;
1621 *have_match = 0;
1623 err = got_object_id_str(&id_str, id);
1624 if (err)
1625 return err;
1627 err = got_object_commit_get_logmsg(&logmsg, commit);
1628 if (err)
1629 goto done;
1631 if (regexec(regex, got_object_commit_get_author(commit), 1,
1632 &regmatch, 0) == 0 ||
1633 regexec(regex, got_object_commit_get_committer(commit), 1,
1634 &regmatch, 0) == 0 ||
1635 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1636 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1637 *have_match = 1;
1638 done:
1639 free(id_str);
1640 free(logmsg);
1641 return err;
1644 static const struct got_error *
1645 queue_commits(struct tog_log_thread_args *a)
1647 const struct got_error *err = NULL;
1650 * We keep all commits open throughout the lifetime of the log
1651 * view in order to avoid having to re-fetch commits from disk
1652 * while updating the display.
1654 do {
1655 struct got_object_id *id;
1656 struct got_commit_object *commit;
1657 struct commit_queue_entry *entry;
1658 int errcode;
1660 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1661 NULL, NULL);
1662 if (err || id == NULL)
1663 break;
1665 err = got_object_open_as_commit(&commit, a->repo, id);
1666 if (err)
1667 break;
1668 entry = alloc_commit_queue_entry(commit, id);
1669 if (entry == NULL) {
1670 err = got_error_from_errno("alloc_commit_queue_entry");
1671 break;
1674 errcode = pthread_mutex_lock(&tog_mutex);
1675 if (errcode) {
1676 err = got_error_set_errno(errcode,
1677 "pthread_mutex_lock");
1678 break;
1681 entry->idx = a->commits->ncommits;
1682 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1683 a->commits->ncommits++;
1685 if (*a->searching == TOG_SEARCH_FORWARD &&
1686 !*a->search_next_done) {
1687 int have_match;
1688 err = match_commit(&have_match, id, commit, a->regex);
1689 if (err)
1690 break;
1691 if (have_match)
1692 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1695 errcode = pthread_mutex_unlock(&tog_mutex);
1696 if (errcode && err == NULL)
1697 err = got_error_set_errno(errcode,
1698 "pthread_mutex_unlock");
1699 if (err)
1700 break;
1701 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1703 return err;
1706 static void
1707 select_commit(struct tog_log_view_state *s)
1709 struct commit_queue_entry *entry;
1710 int ncommits = 0;
1712 entry = s->first_displayed_entry;
1713 while (entry) {
1714 if (ncommits == s->selected) {
1715 s->selected_entry = entry;
1716 break;
1718 entry = TAILQ_NEXT(entry, entry);
1719 ncommits++;
1723 static const struct got_error *
1724 draw_commits(struct tog_view *view)
1726 const struct got_error *err = NULL;
1727 struct tog_log_view_state *s = &view->state.log;
1728 struct commit_queue_entry *entry = s->selected_entry;
1729 const int limit = view->nlines;
1730 int width;
1731 int ncommits, author_cols = 4;
1732 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1733 char *refs_str = NULL;
1734 wchar_t *wline;
1735 struct tog_color *tc;
1736 static const size_t date_display_cols = 12;
1738 if (s->selected_entry &&
1739 !(view->searching && view->search_next_done == 0)) {
1740 struct got_reflist_head *refs;
1741 err = got_object_id_str(&id_str, s->selected_entry->id);
1742 if (err)
1743 return err;
1744 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1745 s->selected_entry->id);
1746 if (refs) {
1747 err = build_refs_str(&refs_str, refs,
1748 s->selected_entry->id, s->repo);
1749 if (err)
1750 goto done;
1754 if (s->thread_args.commits_needed == 0)
1755 halfdelay(10); /* disable fast refresh */
1757 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1758 if (asprintf(&ncommits_str, " [%d/%d] %s",
1759 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1760 (view->searching && !view->search_next_done) ?
1761 "searching..." : "loading...") == -1) {
1762 err = got_error_from_errno("asprintf");
1763 goto done;
1765 } else {
1766 const char *search_str = NULL;
1768 if (view->searching) {
1769 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1770 search_str = "no more matches";
1771 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1772 search_str = "no matches found";
1773 else if (!view->search_next_done)
1774 search_str = "searching...";
1777 if (asprintf(&ncommits_str, " [%d/%d] %s",
1778 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1779 search_str ? search_str :
1780 (refs_str ? refs_str : "")) == -1) {
1781 err = got_error_from_errno("asprintf");
1782 goto done;
1786 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1787 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1788 "........................................",
1789 s->in_repo_path, ncommits_str) == -1) {
1790 err = got_error_from_errno("asprintf");
1791 header = NULL;
1792 goto done;
1794 } else if (asprintf(&header, "commit %s%s",
1795 id_str ? id_str : "........................................",
1796 ncommits_str) == -1) {
1797 err = got_error_from_errno("asprintf");
1798 header = NULL;
1799 goto done;
1801 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1802 if (err)
1803 goto done;
1805 werase(view->window);
1807 if (view_needs_focus_indication(view))
1808 wstandout(view->window);
1809 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1810 if (tc)
1811 wattr_on(view->window,
1812 COLOR_PAIR(tc->colorpair), NULL);
1813 waddwstr(view->window, wline);
1814 if (tc)
1815 wattr_off(view->window,
1816 COLOR_PAIR(tc->colorpair), NULL);
1817 while (width < view->ncols) {
1818 waddch(view->window, ' ');
1819 width++;
1821 if (view_needs_focus_indication(view))
1822 wstandend(view->window);
1823 free(wline);
1824 if (limit <= 1)
1825 goto done;
1827 /* Grow author column size if necessary, and set view->maxx. */
1828 entry = s->first_displayed_entry;
1829 ncommits = 0;
1830 view->maxx = 0;
1831 while (entry) {
1832 char *author, *eol, *msg, *msg0;
1833 wchar_t *wauthor, *wmsg;
1834 int width;
1835 if (ncommits >= limit - 1)
1836 break;
1837 author = strdup(got_object_commit_get_author(entry->commit));
1838 if (author == NULL) {
1839 err = got_error_from_errno("strdup");
1840 goto done;
1842 err = format_author(&wauthor, &width, author, COLS,
1843 date_display_cols);
1844 if (author_cols < width)
1845 author_cols = width;
1846 free(wauthor);
1847 free(author);
1848 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1849 if (err)
1850 goto done;
1851 msg = msg0;
1852 while (*msg == '\n')
1853 ++msg;
1854 if ((eol = strchr(msg, '\n')))
1855 *eol = '\0';
1856 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1857 date_display_cols + author_cols, 0);
1858 if (err)
1859 goto done;
1860 view->maxx = MAX(view->maxx, width);
1861 free(msg0);
1862 free(wmsg);
1863 ncommits++;
1864 entry = TAILQ_NEXT(entry, entry);
1867 entry = s->first_displayed_entry;
1868 s->last_displayed_entry = s->first_displayed_entry;
1869 ncommits = 0;
1870 while (entry) {
1871 if (ncommits >= limit - 1)
1872 break;
1873 if (ncommits == s->selected)
1874 wstandout(view->window);
1875 err = draw_commit(view, entry->commit, entry->id,
1876 date_display_cols, author_cols);
1877 if (ncommits == s->selected)
1878 wstandend(view->window);
1879 if (err)
1880 goto done;
1881 ncommits++;
1882 s->last_displayed_entry = entry;
1883 entry = TAILQ_NEXT(entry, entry);
1886 view_vborder(view);
1887 update_panels();
1888 doupdate();
1889 done:
1890 free(id_str);
1891 free(refs_str);
1892 free(ncommits_str);
1893 free(header);
1894 return err;
1897 static void
1898 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1900 struct commit_queue_entry *entry;
1901 int nscrolled = 0;
1903 entry = TAILQ_FIRST(&s->commits.head);
1904 if (s->first_displayed_entry == entry)
1905 return;
1907 entry = s->first_displayed_entry;
1908 while (entry && nscrolled < maxscroll) {
1909 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1910 if (entry) {
1911 s->first_displayed_entry = entry;
1912 nscrolled++;
1917 static const struct got_error *
1918 trigger_log_thread(struct tog_view *view, int wait)
1920 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1921 int errcode;
1923 halfdelay(1); /* fast refresh while loading commits */
1925 while (ta->commits_needed > 0 || ta->load_all) {
1926 if (ta->log_complete)
1927 break;
1929 /* Wake the log thread. */
1930 errcode = pthread_cond_signal(&ta->need_commits);
1931 if (errcode)
1932 return got_error_set_errno(errcode,
1933 "pthread_cond_signal");
1936 * The mutex will be released while the view loop waits
1937 * in wgetch(), at which time the log thread will run.
1939 if (!wait)
1940 break;
1942 /* Display progress update in log view. */
1943 show_log_view(view);
1944 update_panels();
1945 doupdate();
1947 /* Wait right here while next commit is being loaded. */
1948 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1949 if (errcode)
1950 return got_error_set_errno(errcode,
1951 "pthread_cond_wait");
1953 /* Display progress update in log view. */
1954 show_log_view(view);
1955 update_panels();
1956 doupdate();
1959 return NULL;
1962 static const struct got_error *
1963 log_scroll_down(struct tog_view *view, int maxscroll)
1965 struct tog_log_view_state *s = &view->state.log;
1966 const struct got_error *err = NULL;
1967 struct commit_queue_entry *pentry;
1968 int nscrolled = 0, ncommits_needed;
1970 if (s->last_displayed_entry == NULL)
1971 return NULL;
1973 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1974 if (s->commits.ncommits < ncommits_needed &&
1975 !s->thread_args.log_complete) {
1977 * Ask the log thread for required amount of commits.
1979 s->thread_args.commits_needed += maxscroll;
1980 err = trigger_log_thread(view, 1);
1981 if (err)
1982 return err;
1985 do {
1986 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1987 if (pentry == NULL)
1988 break;
1990 s->last_displayed_entry = pentry;
1992 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1993 if (pentry == NULL)
1994 break;
1995 s->first_displayed_entry = pentry;
1996 } while (++nscrolled < maxscroll);
1998 return err;
2001 static const struct got_error *
2002 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2003 struct got_commit_object *commit, struct got_object_id *commit_id,
2004 struct tog_view *log_view, struct got_repository *repo)
2006 const struct got_error *err;
2007 struct got_object_qid *parent_id;
2008 struct tog_view *diff_view;
2010 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2011 if (diff_view == NULL)
2012 return got_error_from_errno("view_open");
2014 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2015 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2016 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2017 if (err == NULL)
2018 *new_view = diff_view;
2019 return err;
2022 static const struct got_error *
2023 tree_view_visit_subtree(struct tog_tree_view_state *s,
2024 struct got_tree_object *subtree)
2026 struct tog_parent_tree *parent;
2028 parent = calloc(1, sizeof(*parent));
2029 if (parent == NULL)
2030 return got_error_from_errno("calloc");
2032 parent->tree = s->tree;
2033 parent->first_displayed_entry = s->first_displayed_entry;
2034 parent->selected_entry = s->selected_entry;
2035 parent->selected = s->selected;
2036 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2037 s->tree = subtree;
2038 s->selected = 0;
2039 s->first_displayed_entry = NULL;
2040 return NULL;
2043 static const struct got_error *
2044 tree_view_walk_path(struct tog_tree_view_state *s,
2045 struct got_commit_object *commit, const char *path)
2047 const struct got_error *err = NULL;
2048 struct got_tree_object *tree = NULL;
2049 const char *p;
2050 char *slash, *subpath = NULL;
2052 /* Walk the path and open corresponding tree objects. */
2053 p = path;
2054 while (*p) {
2055 struct got_tree_entry *te;
2056 struct got_object_id *tree_id;
2057 char *te_name;
2059 while (p[0] == '/')
2060 p++;
2062 /* Ensure the correct subtree entry is selected. */
2063 slash = strchr(p, '/');
2064 if (slash == NULL)
2065 te_name = strdup(p);
2066 else
2067 te_name = strndup(p, slash - p);
2068 if (te_name == NULL) {
2069 err = got_error_from_errno("strndup");
2070 break;
2072 te = got_object_tree_find_entry(s->tree, te_name);
2073 if (te == NULL) {
2074 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2075 free(te_name);
2076 break;
2078 free(te_name);
2079 s->first_displayed_entry = s->selected_entry = te;
2081 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2082 break; /* jump to this file's entry */
2084 slash = strchr(p, '/');
2085 if (slash)
2086 subpath = strndup(path, slash - path);
2087 else
2088 subpath = strdup(path);
2089 if (subpath == NULL) {
2090 err = got_error_from_errno("strdup");
2091 break;
2094 err = got_object_id_by_path(&tree_id, s->repo, commit,
2095 subpath);
2096 if (err)
2097 break;
2099 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2100 free(tree_id);
2101 if (err)
2102 break;
2104 err = tree_view_visit_subtree(s, tree);
2105 if (err) {
2106 got_object_tree_close(tree);
2107 break;
2109 if (slash == NULL)
2110 break;
2111 free(subpath);
2112 subpath = NULL;
2113 p = slash;
2116 free(subpath);
2117 return err;
2120 static const struct got_error *
2121 browse_commit_tree(struct tog_view **new_view, int begin_x,
2122 struct commit_queue_entry *entry, const char *path,
2123 const char *head_ref_name, struct got_repository *repo)
2125 const struct got_error *err = NULL;
2126 struct tog_tree_view_state *s;
2127 struct tog_view *tree_view;
2129 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2130 if (tree_view == NULL)
2131 return got_error_from_errno("view_open");
2133 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2134 if (err)
2135 return err;
2136 s = &tree_view->state.tree;
2138 *new_view = tree_view;
2140 if (got_path_is_root_dir(path))
2141 return NULL;
2143 return tree_view_walk_path(s, entry->commit, path);
2146 static const struct got_error *
2147 block_signals_used_by_main_thread(void)
2149 sigset_t sigset;
2150 int errcode;
2152 if (sigemptyset(&sigset) == -1)
2153 return got_error_from_errno("sigemptyset");
2155 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2156 if (sigaddset(&sigset, SIGWINCH) == -1)
2157 return got_error_from_errno("sigaddset");
2158 if (sigaddset(&sigset, SIGCONT) == -1)
2159 return got_error_from_errno("sigaddset");
2160 if (sigaddset(&sigset, SIGINT) == -1)
2161 return got_error_from_errno("sigaddset");
2162 if (sigaddset(&sigset, SIGTERM) == -1)
2163 return got_error_from_errno("sigaddset");
2165 /* ncurses handles SIGTSTP */
2166 if (sigaddset(&sigset, SIGTSTP) == -1)
2167 return got_error_from_errno("sigaddset");
2169 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2170 if (errcode)
2171 return got_error_set_errno(errcode, "pthread_sigmask");
2173 return NULL;
2176 static void *
2177 log_thread(void *arg)
2179 const struct got_error *err = NULL;
2180 int errcode = 0;
2181 struct tog_log_thread_args *a = arg;
2182 int done = 0;
2184 err = block_signals_used_by_main_thread();
2185 if (err)
2186 return (void *)err;
2188 while (!done && !err && !tog_fatal_signal_received()) {
2189 err = queue_commits(a);
2190 if (err) {
2191 if (err->code != GOT_ERR_ITER_COMPLETED)
2192 return (void *)err;
2193 err = NULL;
2194 done = 1;
2195 } else if (a->commits_needed > 0 && !a->load_all)
2196 a->commits_needed--;
2198 errcode = pthread_mutex_lock(&tog_mutex);
2199 if (errcode) {
2200 err = got_error_set_errno(errcode,
2201 "pthread_mutex_lock");
2202 break;
2203 } else if (*a->quit)
2204 done = 1;
2205 else if (*a->first_displayed_entry == NULL) {
2206 *a->first_displayed_entry =
2207 TAILQ_FIRST(&a->commits->head);
2208 *a->selected_entry = *a->first_displayed_entry;
2211 errcode = pthread_cond_signal(&a->commit_loaded);
2212 if (errcode) {
2213 err = got_error_set_errno(errcode,
2214 "pthread_cond_signal");
2215 pthread_mutex_unlock(&tog_mutex);
2216 break;
2219 if (done)
2220 a->commits_needed = 0;
2221 else {
2222 if (a->commits_needed == 0 && !a->load_all) {
2223 errcode = pthread_cond_wait(&a->need_commits,
2224 &tog_mutex);
2225 if (errcode)
2226 err = got_error_set_errno(errcode,
2227 "pthread_cond_wait");
2228 if (*a->quit)
2229 done = 1;
2233 errcode = pthread_mutex_unlock(&tog_mutex);
2234 if (errcode && err == NULL)
2235 err = got_error_set_errno(errcode,
2236 "pthread_mutex_unlock");
2238 a->log_complete = 1;
2239 return (void *)err;
2242 static const struct got_error *
2243 stop_log_thread(struct tog_log_view_state *s)
2245 const struct got_error *err = NULL;
2246 int errcode;
2248 if (s->thread) {
2249 s->quit = 1;
2250 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2251 if (errcode)
2252 return got_error_set_errno(errcode,
2253 "pthread_cond_signal");
2254 errcode = pthread_mutex_unlock(&tog_mutex);
2255 if (errcode)
2256 return got_error_set_errno(errcode,
2257 "pthread_mutex_unlock");
2258 errcode = pthread_join(s->thread, (void **)&err);
2259 if (errcode)
2260 return got_error_set_errno(errcode, "pthread_join");
2261 errcode = pthread_mutex_lock(&tog_mutex);
2262 if (errcode)
2263 return got_error_set_errno(errcode,
2264 "pthread_mutex_lock");
2265 s->thread = 0; //NULL;
2268 if (s->thread_args.repo) {
2269 err = got_repo_close(s->thread_args.repo);
2270 s->thread_args.repo = NULL;
2273 if (s->thread_args.pack_fds) {
2274 const struct got_error *pack_err =
2275 got_repo_pack_fds_close(s->thread_args.pack_fds);
2276 if (err == NULL)
2277 err = pack_err;
2278 s->thread_args.pack_fds = NULL;
2281 if (s->thread_args.graph) {
2282 got_commit_graph_close(s->thread_args.graph);
2283 s->thread_args.graph = NULL;
2286 return err;
2289 static const struct got_error *
2290 close_log_view(struct tog_view *view)
2292 const struct got_error *err = NULL;
2293 struct tog_log_view_state *s = &view->state.log;
2294 int errcode;
2296 err = stop_log_thread(s);
2298 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2299 if (errcode && err == NULL)
2300 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2302 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2303 if (errcode && err == NULL)
2304 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2306 free_commits(&s->commits);
2307 free(s->in_repo_path);
2308 s->in_repo_path = NULL;
2309 free(s->start_id);
2310 s->start_id = NULL;
2311 free(s->head_ref_name);
2312 s->head_ref_name = NULL;
2313 return err;
2316 static const struct got_error *
2317 search_start_log_view(struct tog_view *view)
2319 struct tog_log_view_state *s = &view->state.log;
2321 s->matched_entry = NULL;
2322 s->search_entry = NULL;
2323 return NULL;
2326 static const struct got_error *
2327 search_next_log_view(struct tog_view *view)
2329 const struct got_error *err = NULL;
2330 struct tog_log_view_state *s = &view->state.log;
2331 struct commit_queue_entry *entry;
2333 /* Display progress update in log view. */
2334 show_log_view(view);
2335 update_panels();
2336 doupdate();
2338 if (s->search_entry) {
2339 int errcode, ch;
2340 errcode = pthread_mutex_unlock(&tog_mutex);
2341 if (errcode)
2342 return got_error_set_errno(errcode,
2343 "pthread_mutex_unlock");
2344 ch = wgetch(view->window);
2345 errcode = pthread_mutex_lock(&tog_mutex);
2346 if (errcode)
2347 return got_error_set_errno(errcode,
2348 "pthread_mutex_lock");
2349 if (ch == KEY_BACKSPACE) {
2350 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2351 return NULL;
2353 if (view->searching == TOG_SEARCH_FORWARD)
2354 entry = TAILQ_NEXT(s->search_entry, entry);
2355 else
2356 entry = TAILQ_PREV(s->search_entry,
2357 commit_queue_head, entry);
2358 } else if (s->matched_entry) {
2359 int matched_idx = s->matched_entry->idx;
2360 int selected_idx = s->selected_entry->idx;
2363 * If the user has moved the cursor after we hit a match,
2364 * the position from where we should continue searching
2365 * might have changed.
2367 if (view->searching == TOG_SEARCH_FORWARD) {
2368 if (matched_idx > selected_idx)
2369 entry = TAILQ_NEXT(s->selected_entry, entry);
2370 else
2371 entry = TAILQ_NEXT(s->matched_entry, entry);
2372 } else {
2373 if (matched_idx < selected_idx)
2374 entry = TAILQ_PREV(s->selected_entry,
2375 commit_queue_head, entry);
2376 else
2377 entry = TAILQ_PREV(s->matched_entry,
2378 commit_queue_head, entry);
2380 } else {
2381 entry = s->selected_entry;
2384 while (1) {
2385 int have_match = 0;
2387 if (entry == NULL) {
2388 if (s->thread_args.log_complete ||
2389 view->searching == TOG_SEARCH_BACKWARD) {
2390 view->search_next_done =
2391 (s->matched_entry == NULL ?
2392 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2393 s->search_entry = NULL;
2394 return NULL;
2397 * Poke the log thread for more commits and return,
2398 * allowing the main loop to make progress. Search
2399 * will resume at s->search_entry once we come back.
2401 s->thread_args.commits_needed++;
2402 return trigger_log_thread(view, 0);
2405 err = match_commit(&have_match, entry->id, entry->commit,
2406 &view->regex);
2407 if (err)
2408 break;
2409 if (have_match) {
2410 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2411 s->matched_entry = entry;
2412 break;
2415 s->search_entry = entry;
2416 if (view->searching == TOG_SEARCH_FORWARD)
2417 entry = TAILQ_NEXT(entry, entry);
2418 else
2419 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2422 if (s->matched_entry) {
2423 int cur = s->selected_entry->idx;
2424 while (cur < s->matched_entry->idx) {
2425 err = input_log_view(NULL, view, KEY_DOWN);
2426 if (err)
2427 return err;
2428 cur++;
2430 while (cur > s->matched_entry->idx) {
2431 err = input_log_view(NULL, view, KEY_UP);
2432 if (err)
2433 return err;
2434 cur--;
2438 s->search_entry = NULL;
2440 return NULL;
2443 static const struct got_error *
2444 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2445 struct got_repository *repo, const char *head_ref_name,
2446 const char *in_repo_path, int log_branches)
2448 const struct got_error *err = NULL;
2449 struct tog_log_view_state *s = &view->state.log;
2450 struct got_repository *thread_repo = NULL;
2451 struct got_commit_graph *thread_graph = NULL;
2452 int errcode;
2454 if (in_repo_path != s->in_repo_path) {
2455 free(s->in_repo_path);
2456 s->in_repo_path = strdup(in_repo_path);
2457 if (s->in_repo_path == NULL)
2458 return got_error_from_errno("strdup");
2461 /* The commit queue only contains commits being displayed. */
2462 TAILQ_INIT(&s->commits.head);
2463 s->commits.ncommits = 0;
2465 s->repo = repo;
2466 if (head_ref_name) {
2467 s->head_ref_name = strdup(head_ref_name);
2468 if (s->head_ref_name == NULL) {
2469 err = got_error_from_errno("strdup");
2470 goto done;
2473 s->start_id = got_object_id_dup(start_id);
2474 if (s->start_id == NULL) {
2475 err = got_error_from_errno("got_object_id_dup");
2476 goto done;
2478 s->log_branches = log_branches;
2480 STAILQ_INIT(&s->colors);
2481 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2482 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2483 get_color_value("TOG_COLOR_COMMIT"));
2484 if (err)
2485 goto done;
2486 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2487 get_color_value("TOG_COLOR_AUTHOR"));
2488 if (err) {
2489 free_colors(&s->colors);
2490 goto done;
2492 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2493 get_color_value("TOG_COLOR_DATE"));
2494 if (err) {
2495 free_colors(&s->colors);
2496 goto done;
2500 view->show = show_log_view;
2501 view->input = input_log_view;
2502 view->close = close_log_view;
2503 view->search_start = search_start_log_view;
2504 view->search_next = search_next_log_view;
2506 if (s->thread_args.pack_fds == NULL) {
2507 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2508 if (err)
2509 goto done;
2511 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2512 s->thread_args.pack_fds);
2513 if (err)
2514 goto done;
2515 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2516 !s->log_branches);
2517 if (err)
2518 goto done;
2519 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2520 s->repo, NULL, NULL);
2521 if (err)
2522 goto done;
2524 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2525 if (errcode) {
2526 err = got_error_set_errno(errcode, "pthread_cond_init");
2527 goto done;
2529 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2530 if (errcode) {
2531 err = got_error_set_errno(errcode, "pthread_cond_init");
2532 goto done;
2535 s->thread_args.commits_needed = view->nlines;
2536 s->thread_args.graph = thread_graph;
2537 s->thread_args.commits = &s->commits;
2538 s->thread_args.in_repo_path = s->in_repo_path;
2539 s->thread_args.start_id = s->start_id;
2540 s->thread_args.repo = thread_repo;
2541 s->thread_args.log_complete = 0;
2542 s->thread_args.quit = &s->quit;
2543 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2544 s->thread_args.selected_entry = &s->selected_entry;
2545 s->thread_args.searching = &view->searching;
2546 s->thread_args.search_next_done = &view->search_next_done;
2547 s->thread_args.regex = &view->regex;
2548 done:
2549 if (err)
2550 close_log_view(view);
2551 return err;
2554 static const struct got_error *
2555 show_log_view(struct tog_view *view)
2557 const struct got_error *err;
2558 struct tog_log_view_state *s = &view->state.log;
2560 if (s->thread == 0) { //NULL) {
2561 int errcode = pthread_create(&s->thread, NULL, log_thread,
2562 &s->thread_args);
2563 if (errcode)
2564 return got_error_set_errno(errcode, "pthread_create");
2565 if (s->thread_args.commits_needed > 0) {
2566 err = trigger_log_thread(view, 1);
2567 if (err)
2568 return err;
2572 return draw_commits(view);
2575 static const struct got_error *
2576 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2578 const struct got_error *err = NULL;
2579 struct tog_log_view_state *s = &view->state.log;
2580 struct tog_view *diff_view = NULL, *tree_view = NULL;
2581 struct tog_view *ref_view = NULL;
2582 struct commit_queue_entry *entry;
2583 int begin_x = 0, n, nscroll = view->nlines - 1;
2585 if (s->thread_args.load_all) {
2586 if (ch == KEY_BACKSPACE)
2587 s->thread_args.load_all = 0;
2588 else if (s->thread_args.log_complete) {
2589 s->thread_args.load_all = 0;
2590 log_scroll_down(view, s->commits.ncommits);
2591 s->selected = MIN(view->nlines - 2,
2592 s->commits.ncommits - 1);
2593 select_commit(s);
2595 return NULL;
2598 switch (ch) {
2599 case 'q':
2600 s->quit = 1;
2601 break;
2602 case '0':
2603 view->x = 0;
2604 break;
2605 case '$':
2606 view->x = MAX(view->maxx - view->ncols / 2, 0);
2607 break;
2608 case KEY_RIGHT:
2609 case 'l':
2610 if (view->x + view->ncols / 2 < view->maxx)
2611 view->x += 2; /* move two columns right */
2612 break;
2613 case KEY_LEFT:
2614 case 'h':
2615 view->x -= MIN(view->x, 2); /* move two columns back */
2616 break;
2617 case 'k':
2618 case KEY_UP:
2619 case '<':
2620 case ',':
2621 case CTRL('p'):
2622 if (s->first_displayed_entry == NULL)
2623 break;
2624 if (s->selected > 0)
2625 s->selected--;
2626 else
2627 log_scroll_up(s, 1);
2628 select_commit(s);
2629 break;
2630 case 'g':
2631 case KEY_HOME:
2632 s->selected = 0;
2633 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2634 select_commit(s);
2635 break;
2636 case CTRL('u'):
2637 case 'u':
2638 nscroll /= 2;
2639 /* FALL THROUGH */
2640 case KEY_PPAGE:
2641 case CTRL('b'):
2642 case 'b':
2643 if (s->first_displayed_entry == NULL)
2644 break;
2645 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2646 s->selected = MAX(0, s->selected - nscroll - 1);
2647 else
2648 log_scroll_up(s, nscroll);
2649 select_commit(s);
2650 break;
2651 case 'j':
2652 case KEY_DOWN:
2653 case '>':
2654 case '.':
2655 case CTRL('n'):
2656 if (s->first_displayed_entry == NULL)
2657 break;
2658 if (s->selected < MIN(view->nlines - 2,
2659 s->commits.ncommits - 1))
2660 s->selected++;
2661 else {
2662 err = log_scroll_down(view, 1);
2663 if (err)
2664 break;
2666 select_commit(s);
2667 break;
2668 case 'G':
2669 case KEY_END: {
2670 /* We don't know yet how many commits, so we're forced to
2671 * traverse them all. */
2672 if (!s->thread_args.log_complete) {
2673 s->thread_args.load_all = 1;
2674 return trigger_log_thread(view, 0);
2677 s->selected = 0;
2678 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2679 for (n = 0; n < view->nlines - 1; n++) {
2680 if (entry == NULL)
2681 break;
2682 s->first_displayed_entry = entry;
2683 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2685 if (n > 0)
2686 s->selected = n - 1;
2687 select_commit(s);
2688 break;
2690 case CTRL('d'):
2691 case 'd':
2692 nscroll /= 2;
2693 /* FALL THROUGH */
2694 case KEY_NPAGE:
2695 case CTRL('f'):
2696 case 'f':
2697 case ' ': {
2698 struct commit_queue_entry *first;
2699 first = s->first_displayed_entry;
2700 if (first == NULL)
2701 break;
2702 err = log_scroll_down(view, nscroll);
2703 if (err)
2704 break;
2705 if (first == s->first_displayed_entry &&
2706 s->selected < MIN(view->nlines - 2,
2707 s->commits.ncommits - 1)) {
2708 /* can't scroll further down */
2709 s->selected += MIN(s->last_displayed_entry->idx -
2710 s->selected_entry->idx, nscroll + 1);
2712 select_commit(s);
2713 break;
2715 case KEY_RESIZE:
2716 if (s->selected > view->nlines - 2)
2717 s->selected = view->nlines - 2;
2718 if (s->selected > s->commits.ncommits - 1)
2719 s->selected = s->commits.ncommits - 1;
2720 select_commit(s);
2721 if (s->commits.ncommits < view->nlines - 1 &&
2722 !s->thread_args.log_complete) {
2723 s->thread_args.commits_needed += (view->nlines - 1) -
2724 s->commits.ncommits;
2725 err = trigger_log_thread(view, 1);
2727 break;
2728 case KEY_ENTER:
2729 case '\r':
2730 if (s->selected_entry == NULL)
2731 break;
2732 if (view_is_parent_view(view))
2733 begin_x = view_split_begin_x(view->begin_x);
2734 err = open_diff_view_for_commit(&diff_view, begin_x,
2735 s->selected_entry->commit, s->selected_entry->id,
2736 view, s->repo);
2737 if (err)
2738 break;
2739 view->focussed = 0;
2740 diff_view->focussed = 1;
2741 if (view_is_parent_view(view)) {
2742 err = view_close_child(view);
2743 if (err)
2744 return err;
2745 err = view_set_child(view, diff_view);
2746 if (err)
2747 return err;
2748 view->focus_child = 1;
2749 } else
2750 *new_view = diff_view;
2751 break;
2752 case 't':
2753 if (s->selected_entry == NULL)
2754 break;
2755 if (view_is_parent_view(view))
2756 begin_x = view_split_begin_x(view->begin_x);
2757 err = browse_commit_tree(&tree_view, begin_x,
2758 s->selected_entry, s->in_repo_path, s->head_ref_name,
2759 s->repo);
2760 if (err)
2761 break;
2762 view->focussed = 0;
2763 tree_view->focussed = 1;
2764 if (view_is_parent_view(view)) {
2765 err = view_close_child(view);
2766 if (err)
2767 return err;
2768 err = view_set_child(view, tree_view);
2769 if (err)
2770 return err;
2771 view->focus_child = 1;
2772 } else
2773 *new_view = tree_view;
2774 break;
2775 case KEY_BACKSPACE:
2776 case CTRL('l'):
2777 case 'B':
2778 if (ch == KEY_BACKSPACE &&
2779 got_path_is_root_dir(s->in_repo_path))
2780 break;
2781 err = stop_log_thread(s);
2782 if (err)
2783 return err;
2784 if (ch == KEY_BACKSPACE) {
2785 char *parent_path;
2786 err = got_path_dirname(&parent_path, s->in_repo_path);
2787 if (err)
2788 return err;
2789 free(s->in_repo_path);
2790 s->in_repo_path = parent_path;
2791 s->thread_args.in_repo_path = s->in_repo_path;
2792 } else if (ch == CTRL('l')) {
2793 struct got_object_id *start_id;
2794 err = got_repo_match_object_id(&start_id, NULL,
2795 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2796 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2797 if (err)
2798 return err;
2799 free(s->start_id);
2800 s->start_id = start_id;
2801 s->thread_args.start_id = s->start_id;
2802 } else /* 'B' */
2803 s->log_branches = !s->log_branches;
2805 if (s->thread_args.pack_fds == NULL) {
2806 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2807 if (err)
2808 return err;
2810 err = got_repo_open(&s->thread_args.repo,
2811 got_repo_get_path(s->repo), NULL,
2812 s->thread_args.pack_fds);
2813 if (err)
2814 return err;
2815 tog_free_refs();
2816 err = tog_load_refs(s->repo, 0);
2817 if (err)
2818 return err;
2819 err = got_commit_graph_open(&s->thread_args.graph,
2820 s->in_repo_path, !s->log_branches);
2821 if (err)
2822 return err;
2823 err = got_commit_graph_iter_start(s->thread_args.graph,
2824 s->start_id, s->repo, NULL, NULL);
2825 if (err)
2826 return err;
2827 free_commits(&s->commits);
2828 s->first_displayed_entry = NULL;
2829 s->last_displayed_entry = NULL;
2830 s->selected_entry = NULL;
2831 s->selected = 0;
2832 s->thread_args.log_complete = 0;
2833 s->quit = 0;
2834 s->thread_args.commits_needed = view->nlines;
2835 s->matched_entry = NULL;
2836 s->search_entry = NULL;
2837 break;
2838 case 'r':
2839 if (view_is_parent_view(view))
2840 begin_x = view_split_begin_x(view->begin_x);
2841 ref_view = view_open(view->nlines, view->ncols,
2842 view->begin_y, begin_x, TOG_VIEW_REF);
2843 if (ref_view == NULL)
2844 return got_error_from_errno("view_open");
2845 err = open_ref_view(ref_view, s->repo);
2846 if (err) {
2847 view_close(ref_view);
2848 return err;
2850 view->focussed = 0;
2851 ref_view->focussed = 1;
2852 if (view_is_parent_view(view)) {
2853 err = view_close_child(view);
2854 if (err)
2855 return err;
2856 err = view_set_child(view, ref_view);
2857 if (err)
2858 return err;
2859 view->focus_child = 1;
2860 } else
2861 *new_view = ref_view;
2862 break;
2863 default:
2864 break;
2867 return err;
2870 static const struct got_error *
2871 apply_unveil(const char *repo_path, const char *worktree_path)
2873 const struct got_error *error;
2875 #ifdef PROFILE
2876 if (unveil("gmon.out", "rwc") != 0)
2877 return got_error_from_errno2("unveil", "gmon.out");
2878 #endif
2879 if (repo_path && unveil(repo_path, "r") != 0)
2880 return got_error_from_errno2("unveil", repo_path);
2882 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2883 return got_error_from_errno2("unveil", worktree_path);
2885 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2886 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2888 error = got_privsep_unveil_exec_helpers();
2889 if (error != NULL)
2890 return error;
2892 if (unveil(NULL, NULL) != 0)
2893 return got_error_from_errno("unveil");
2895 return NULL;
2898 static void
2899 init_curses(void)
2902 * Override default signal handlers before starting ncurses.
2903 * This should prevent ncurses from installing its own
2904 * broken cleanup() signal handler.
2906 signal(SIGWINCH, tog_sigwinch);
2907 signal(SIGPIPE, tog_sigpipe);
2908 signal(SIGCONT, tog_sigcont);
2909 signal(SIGINT, tog_sigint);
2910 signal(SIGTERM, tog_sigterm);
2912 initscr();
2913 cbreak();
2914 halfdelay(1); /* Do fast refresh while initial view is loading. */
2915 noecho();
2916 nonl();
2917 intrflush(stdscr, FALSE);
2918 keypad(stdscr, TRUE);
2919 curs_set(0);
2920 if (getenv("TOG_COLORS") != NULL) {
2921 start_color();
2922 use_default_colors();
2926 static const struct got_error *
2927 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2928 struct got_repository *repo, struct got_worktree *worktree)
2930 const struct got_error *err = NULL;
2932 if (argc == 0) {
2933 *in_repo_path = strdup("/");
2934 if (*in_repo_path == NULL)
2935 return got_error_from_errno("strdup");
2936 return NULL;
2939 if (worktree) {
2940 const char *prefix = got_worktree_get_path_prefix(worktree);
2941 char *p;
2943 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2944 if (err)
2945 return err;
2946 if (asprintf(in_repo_path, "%s%s%s", prefix,
2947 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2948 p) == -1) {
2949 err = got_error_from_errno("asprintf");
2950 *in_repo_path = NULL;
2952 free(p);
2953 } else
2954 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2956 return err;
2959 static const struct got_error *
2960 cmd_log(int argc, char *argv[])
2962 const struct got_error *error;
2963 struct got_repository *repo = NULL;
2964 struct got_worktree *worktree = NULL;
2965 struct got_object_id *start_id = NULL;
2966 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2967 char *start_commit = NULL, *label = NULL;
2968 struct got_reference *ref = NULL;
2969 const char *head_ref_name = NULL;
2970 int ch, log_branches = 0;
2971 struct tog_view *view;
2972 int *pack_fds = NULL;
2974 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2975 switch (ch) {
2976 case 'b':
2977 log_branches = 1;
2978 break;
2979 case 'c':
2980 start_commit = optarg;
2981 break;
2982 case 'r':
2983 repo_path = realpath(optarg, NULL);
2984 if (repo_path == NULL)
2985 return got_error_from_errno2("realpath",
2986 optarg);
2987 break;
2988 default:
2989 usage_log();
2990 /* NOTREACHED */
2994 argc -= optind;
2995 argv += optind;
2997 if (argc > 1)
2998 usage_log();
3000 error = got_repo_pack_fds_open(&pack_fds);
3001 if (error != NULL)
3002 goto done;
3004 if (repo_path == NULL) {
3005 cwd = getcwd(NULL, 0);
3006 if (cwd == NULL)
3007 return got_error_from_errno("getcwd");
3008 error = got_worktree_open(&worktree, cwd);
3009 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3010 goto done;
3011 if (worktree)
3012 repo_path =
3013 strdup(got_worktree_get_repo_path(worktree));
3014 else
3015 repo_path = strdup(cwd);
3016 if (repo_path == NULL) {
3017 error = got_error_from_errno("strdup");
3018 goto done;
3022 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3023 if (error != NULL)
3024 goto done;
3026 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3027 repo, worktree);
3028 if (error)
3029 goto done;
3031 init_curses();
3033 error = apply_unveil(got_repo_get_path(repo),
3034 worktree ? got_worktree_get_root_path(worktree) : NULL);
3035 if (error)
3036 goto done;
3038 /* already loaded by tog_log_with_path()? */
3039 if (TAILQ_EMPTY(&tog_refs)) {
3040 error = tog_load_refs(repo, 0);
3041 if (error)
3042 goto done;
3045 if (start_commit == NULL) {
3046 error = got_repo_match_object_id(&start_id, &label,
3047 worktree ? got_worktree_get_head_ref_name(worktree) :
3048 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3049 if (error)
3050 goto done;
3051 head_ref_name = label;
3052 } else {
3053 error = got_ref_open(&ref, repo, start_commit, 0);
3054 if (error == NULL)
3055 head_ref_name = got_ref_get_name(ref);
3056 else if (error->code != GOT_ERR_NOT_REF)
3057 goto done;
3058 error = got_repo_match_object_id(&start_id, NULL,
3059 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3060 if (error)
3061 goto done;
3064 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3065 if (view == NULL) {
3066 error = got_error_from_errno("view_open");
3067 goto done;
3069 error = open_log_view(view, start_id, repo, head_ref_name,
3070 in_repo_path, log_branches);
3071 if (error)
3072 goto done;
3073 if (worktree) {
3074 /* Release work tree lock. */
3075 got_worktree_close(worktree);
3076 worktree = NULL;
3078 error = view_loop(view);
3079 done:
3080 free(in_repo_path);
3081 free(repo_path);
3082 free(cwd);
3083 free(start_id);
3084 free(label);
3085 if (ref)
3086 got_ref_close(ref);
3087 if (repo) {
3088 const struct got_error *close_err = got_repo_close(repo);
3089 if (error == NULL)
3090 error = close_err;
3092 if (worktree)
3093 got_worktree_close(worktree);
3094 if (pack_fds) {
3095 const struct got_error *pack_err =
3096 got_repo_pack_fds_close(pack_fds);
3097 if (error == NULL)
3098 error = pack_err;
3100 tog_free_refs();
3101 return error;
3104 __dead static void
3105 usage_diff(void)
3107 endwin();
3108 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3109 "[-w] object1 object2\n", getprogname());
3110 exit(1);
3113 static int
3114 match_line(const char *line, regex_t *regex, size_t nmatch,
3115 regmatch_t *regmatch)
3117 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3120 struct tog_color *
3121 match_color(struct tog_colors *colors, const char *line)
3123 struct tog_color *tc = NULL;
3125 STAILQ_FOREACH(tc, colors, entry) {
3126 if (match_line(line, &tc->regex, 0, NULL))
3127 return tc;
3130 return NULL;
3133 static const struct got_error *
3134 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3135 WINDOW *window, int skipcol, regmatch_t *regmatch)
3137 const struct got_error *err = NULL;
3138 char *exstr = NULL;
3139 wchar_t *wline = NULL;
3140 int rme, rms, n, width, scrollx;
3141 int width0 = 0, width1 = 0, width2 = 0;
3142 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3144 *wtotal = 0;
3146 rms = regmatch->rm_so;
3147 rme = regmatch->rm_eo;
3149 err = expand_tab(&exstr, line);
3150 if (err)
3151 return err;
3153 /* Split the line into 3 segments, according to match offsets. */
3154 seg0 = strndup(exstr, rms);
3155 if (seg0 == NULL) {
3156 err = got_error_from_errno("strndup");
3157 goto done;
3159 seg1 = strndup(exstr + rms, rme - rms);
3160 if (seg1 == NULL) {
3161 err = got_error_from_errno("strndup");
3162 goto done;
3164 seg2 = strdup(exstr + rme);
3165 if (seg2 == NULL) {
3166 err = got_error_from_errno("strndup");
3167 goto done;
3170 /* draw up to matched token if we haven't scrolled past it */
3171 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3172 col_tab_align, 1);
3173 if (err)
3174 goto done;
3175 n = MAX(width0 - skipcol, 0);
3176 if (n) {
3177 free(wline);
3178 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3179 wlimit, col_tab_align, 1);
3180 if (err)
3181 goto done;
3182 waddwstr(window, &wline[scrollx]);
3183 wlimit -= width;
3184 *wtotal += width;
3187 if (wlimit > 0) {
3188 int i = 0, w = 0;
3189 size_t wlen;
3191 free(wline);
3192 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3193 col_tab_align, 1);
3194 if (err)
3195 goto done;
3196 wlen = wcslen(wline);
3197 while (i < wlen) {
3198 width = wcwidth(wline[i]);
3199 if (width == -1) {
3200 /* should not happen, tabs are expanded */
3201 err = got_error(GOT_ERR_RANGE);
3202 goto done;
3204 if (width0 + w + width > skipcol)
3205 break;
3206 w += width;
3207 i++;
3209 /* draw (visible part of) matched token (if scrolled into it) */
3210 if (width1 - w > 0) {
3211 wattron(window, A_STANDOUT);
3212 waddwstr(window, &wline[i]);
3213 wattroff(window, A_STANDOUT);
3214 wlimit -= (width1 - w);
3215 *wtotal += (width1 - w);
3219 if (wlimit > 0) { /* draw rest of line */
3220 free(wline);
3221 if (skipcol > width0 + width1) {
3222 err = format_line(&wline, &width2, &scrollx, seg2,
3223 skipcol - (width0 + width1), wlimit,
3224 col_tab_align, 1);
3225 if (err)
3226 goto done;
3227 waddwstr(window, &wline[scrollx]);
3228 } else {
3229 err = format_line(&wline, &width2, NULL, seg2, 0,
3230 wlimit, col_tab_align, 1);
3231 if (err)
3232 goto done;
3233 waddwstr(window, wline);
3235 *wtotal += width2;
3237 done:
3238 free(wline);
3239 free(exstr);
3240 free(seg0);
3241 free(seg1);
3242 free(seg2);
3243 return err;
3246 static const struct got_error *
3247 draw_file(struct tog_view *view, const char *header)
3249 struct tog_diff_view_state *s = &view->state.diff;
3250 regmatch_t *regmatch = &view->regmatch;
3251 const struct got_error *err;
3252 int nprinted = 0;
3253 char *line;
3254 size_t linesize = 0;
3255 ssize_t linelen;
3256 struct tog_color *tc;
3257 wchar_t *wline;
3258 int width;
3259 int max_lines = view->nlines;
3260 int nlines = s->nlines;
3261 off_t line_offset;
3263 line_offset = s->line_offsets[s->first_displayed_line - 1];
3264 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3265 return got_error_from_errno("fseek");
3267 werase(view->window);
3269 if (header) {
3270 if (asprintf(&line, "[%d/%d] %s",
3271 s->first_displayed_line - 1 + s->selected_line, nlines,
3272 header) == -1)
3273 return got_error_from_errno("asprintf");
3274 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3275 0, 0);
3276 free(line);
3277 if (err)
3278 return err;
3280 if (view_needs_focus_indication(view))
3281 wstandout(view->window);
3282 waddwstr(view->window, wline);
3283 free(wline);
3284 wline = NULL;
3285 if (view_needs_focus_indication(view))
3286 wstandend(view->window);
3287 if (width <= view->ncols - 1)
3288 waddch(view->window, '\n');
3290 if (max_lines <= 1)
3291 return NULL;
3292 max_lines--;
3295 s->eof = 0;
3296 view->maxx = 0;
3297 line = NULL;
3298 while (max_lines > 0 && nprinted < max_lines) {
3299 linelen = getline(&line, &linesize, s->f);
3300 if (linelen == -1) {
3301 if (feof(s->f)) {
3302 s->eof = 1;
3303 break;
3305 free(line);
3306 return got_ferror(s->f, GOT_ERR_IO);
3309 /* Set view->maxx based on full line length. */
3310 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3311 view->x ? 1 : 0);
3312 if (err) {
3313 free(line);
3314 return err;
3316 view->maxx = MAX(view->maxx, width);
3317 free(wline);
3318 wline = NULL;
3320 tc = match_color(&s->colors, line);
3321 if (tc)
3322 wattr_on(view->window,
3323 COLOR_PAIR(tc->colorpair), NULL);
3324 if (s->first_displayed_line + nprinted == s->matched_line &&
3325 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3326 err = add_matched_line(&width, line, view->ncols, 0,
3327 view->window, view->x, regmatch);
3328 if (err) {
3329 free(line);
3330 return err;
3332 } else {
3333 int skip;
3334 err = format_line(&wline, &width, &skip, line,
3335 view->x, view->ncols, 0, view->x ? 1 : 0);
3336 if (err) {
3337 free(line);
3338 return err;
3340 waddwstr(view->window, &wline[skip]);
3341 free(wline);
3342 wline = NULL;
3344 if (tc)
3345 wattr_off(view->window,
3346 COLOR_PAIR(tc->colorpair), NULL);
3347 if (width <= view->ncols - 1)
3348 waddch(view->window, '\n');
3349 nprinted++;
3351 free(line);
3352 if (nprinted >= 1)
3353 s->last_displayed_line = s->first_displayed_line +
3354 (nprinted - 1);
3355 else
3356 s->last_displayed_line = s->first_displayed_line;
3358 view_vborder(view);
3360 if (s->eof) {
3361 while (nprinted < view->nlines) {
3362 waddch(view->window, '\n');
3363 nprinted++;
3366 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3367 view->ncols, 0, 0);
3368 if (err) {
3369 return err;
3372 wstandout(view->window);
3373 waddwstr(view->window, wline);
3374 free(wline);
3375 wline = NULL;
3376 wstandend(view->window);
3379 return NULL;
3382 static char *
3383 get_datestr(time_t *time, char *datebuf)
3385 struct tm mytm, *tm;
3386 char *p, *s;
3388 tm = gmtime_r(time, &mytm);
3389 if (tm == NULL)
3390 return NULL;
3391 s = asctime_r(tm, datebuf);
3392 if (s == NULL)
3393 return NULL;
3394 p = strchr(s, '\n');
3395 if (p)
3396 *p = '\0';
3397 return s;
3400 static const struct got_error *
3401 get_changed_paths(struct got_pathlist_head *paths,
3402 struct got_commit_object *commit, struct got_repository *repo)
3404 const struct got_error *err = NULL;
3405 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3406 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3407 struct got_object_qid *qid;
3409 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3410 if (qid != NULL) {
3411 struct got_commit_object *pcommit;
3412 err = got_object_open_as_commit(&pcommit, repo,
3413 &qid->id);
3414 if (err)
3415 return err;
3417 tree_id1 = got_object_id_dup(
3418 got_object_commit_get_tree_id(pcommit));
3419 if (tree_id1 == NULL) {
3420 got_object_commit_close(pcommit);
3421 return got_error_from_errno("got_object_id_dup");
3423 got_object_commit_close(pcommit);
3427 if (tree_id1) {
3428 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3429 if (err)
3430 goto done;
3433 tree_id2 = got_object_commit_get_tree_id(commit);
3434 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3435 if (err)
3436 goto done;
3438 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3439 got_diff_tree_collect_changed_paths, paths, 0);
3440 done:
3441 if (tree1)
3442 got_object_tree_close(tree1);
3443 if (tree2)
3444 got_object_tree_close(tree2);
3445 free(tree_id1);
3446 return err;
3449 static const struct got_error *
3450 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3452 off_t *p;
3454 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3455 if (p == NULL)
3456 return got_error_from_errno("reallocarray");
3457 *line_offsets = p;
3458 (*line_offsets)[*nlines] = off;
3459 (*nlines)++;
3460 return NULL;
3463 static const struct got_error *
3464 write_commit_info(off_t **line_offsets, size_t *nlines,
3465 struct got_object_id *commit_id, struct got_reflist_head *refs,
3466 struct got_repository *repo, FILE *outfile)
3468 const struct got_error *err = NULL;
3469 char datebuf[26], *datestr;
3470 struct got_commit_object *commit;
3471 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3472 time_t committer_time;
3473 const char *author, *committer;
3474 char *refs_str = NULL;
3475 struct got_pathlist_head changed_paths;
3476 struct got_pathlist_entry *pe;
3477 off_t outoff = 0;
3478 int n;
3480 TAILQ_INIT(&changed_paths);
3482 if (refs) {
3483 err = build_refs_str(&refs_str, refs, commit_id, repo);
3484 if (err)
3485 return err;
3488 err = got_object_open_as_commit(&commit, repo, commit_id);
3489 if (err)
3490 return err;
3492 err = got_object_id_str(&id_str, commit_id);
3493 if (err) {
3494 err = got_error_from_errno("got_object_id_str");
3495 goto done;
3498 err = add_line_offset(line_offsets, nlines, 0);
3499 if (err)
3500 goto done;
3502 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3503 refs_str ? refs_str : "", refs_str ? ")" : "");
3504 if (n < 0) {
3505 err = got_error_from_errno("fprintf");
3506 goto done;
3508 outoff += n;
3509 err = add_line_offset(line_offsets, nlines, outoff);
3510 if (err)
3511 goto done;
3513 n = fprintf(outfile, "from: %s\n",
3514 got_object_commit_get_author(commit));
3515 if (n < 0) {
3516 err = got_error_from_errno("fprintf");
3517 goto done;
3519 outoff += n;
3520 err = add_line_offset(line_offsets, nlines, outoff);
3521 if (err)
3522 goto done;
3524 committer_time = got_object_commit_get_committer_time(commit);
3525 datestr = get_datestr(&committer_time, datebuf);
3526 if (datestr) {
3527 n = fprintf(outfile, "date: %s UTC\n", datestr);
3528 if (n < 0) {
3529 err = got_error_from_errno("fprintf");
3530 goto done;
3532 outoff += n;
3533 err = add_line_offset(line_offsets, nlines, outoff);
3534 if (err)
3535 goto done;
3537 author = got_object_commit_get_author(commit);
3538 committer = got_object_commit_get_committer(commit);
3539 if (strcmp(author, committer) != 0) {
3540 n = fprintf(outfile, "via: %s\n", committer);
3541 if (n < 0) {
3542 err = got_error_from_errno("fprintf");
3543 goto done;
3545 outoff += n;
3546 err = add_line_offset(line_offsets, nlines, outoff);
3547 if (err)
3548 goto done;
3550 if (got_object_commit_get_nparents(commit) > 1) {
3551 const struct got_object_id_queue *parent_ids;
3552 struct got_object_qid *qid;
3553 int pn = 1;
3554 parent_ids = got_object_commit_get_parent_ids(commit);
3555 STAILQ_FOREACH(qid, parent_ids, entry) {
3556 err = got_object_id_str(&id_str, &qid->id);
3557 if (err)
3558 goto done;
3559 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3560 if (n < 0) {
3561 err = got_error_from_errno("fprintf");
3562 goto done;
3564 outoff += n;
3565 err = add_line_offset(line_offsets, nlines, outoff);
3566 if (err)
3567 goto done;
3568 free(id_str);
3569 id_str = NULL;
3573 err = got_object_commit_get_logmsg(&logmsg, commit);
3574 if (err)
3575 goto done;
3576 s = logmsg;
3577 while ((line = strsep(&s, "\n")) != NULL) {
3578 n = fprintf(outfile, "%s\n", line);
3579 if (n < 0) {
3580 err = got_error_from_errno("fprintf");
3581 goto done;
3583 outoff += n;
3584 err = add_line_offset(line_offsets, nlines, outoff);
3585 if (err)
3586 goto done;
3589 err = get_changed_paths(&changed_paths, commit, repo);
3590 if (err)
3591 goto done;
3592 TAILQ_FOREACH(pe, &changed_paths, entry) {
3593 struct got_diff_changed_path *cp = pe->data;
3594 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3595 if (n < 0) {
3596 err = got_error_from_errno("fprintf");
3597 goto done;
3599 outoff += n;
3600 err = add_line_offset(line_offsets, nlines, outoff);
3601 if (err)
3602 goto done;
3603 free((char *)pe->path);
3604 free(pe->data);
3607 fputc('\n', outfile);
3608 outoff++;
3609 err = add_line_offset(line_offsets, nlines, outoff);
3610 done:
3611 got_pathlist_free(&changed_paths);
3612 free(id_str);
3613 free(logmsg);
3614 free(refs_str);
3615 got_object_commit_close(commit);
3616 if (err) {
3617 free(*line_offsets);
3618 *line_offsets = NULL;
3619 *nlines = 0;
3621 return err;
3624 static const struct got_error *
3625 create_diff(struct tog_diff_view_state *s)
3627 const struct got_error *err = NULL;
3628 FILE *f = NULL;
3629 int obj_type;
3631 free(s->line_offsets);
3632 s->line_offsets = malloc(sizeof(off_t));
3633 if (s->line_offsets == NULL)
3634 return got_error_from_errno("malloc");
3635 s->nlines = 0;
3637 f = got_opentemp();
3638 if (f == NULL) {
3639 err = got_error_from_errno("got_opentemp");
3640 goto done;
3642 if (s->f && fclose(s->f) == EOF) {
3643 err = got_error_from_errno("fclose");
3644 goto done;
3646 s->f = f;
3648 if (s->id1)
3649 err = got_object_get_type(&obj_type, s->repo, s->id1);
3650 else
3651 err = got_object_get_type(&obj_type, s->repo, s->id2);
3652 if (err)
3653 goto done;
3655 switch (obj_type) {
3656 case GOT_OBJ_TYPE_BLOB:
3657 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3658 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3659 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3660 s->repo, s->f);
3661 break;
3662 case GOT_OBJ_TYPE_TREE:
3663 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3664 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3665 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3666 break;
3667 case GOT_OBJ_TYPE_COMMIT: {
3668 const struct got_object_id_queue *parent_ids;
3669 struct got_object_qid *pid;
3670 struct got_commit_object *commit2;
3671 struct got_reflist_head *refs;
3673 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3674 if (err)
3675 goto done;
3676 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3677 /* Show commit info if we're diffing to a parent/root commit. */
3678 if (s->id1 == NULL) {
3679 err = write_commit_info(&s->line_offsets, &s->nlines,
3680 s->id2, refs, s->repo, s->f);
3681 if (err)
3682 goto done;
3683 } else {
3684 parent_ids = got_object_commit_get_parent_ids(commit2);
3685 STAILQ_FOREACH(pid, parent_ids, entry) {
3686 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3687 err = write_commit_info(
3688 &s->line_offsets, &s->nlines,
3689 s->id2, refs, s->repo, s->f);
3690 if (err)
3691 goto done;
3692 break;
3696 got_object_commit_close(commit2);
3698 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3699 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3700 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3701 break;
3703 default:
3704 err = got_error(GOT_ERR_OBJ_TYPE);
3705 break;
3707 if (err)
3708 goto done;
3709 done:
3710 if (s->f && fflush(s->f) != 0 && err == NULL)
3711 err = got_error_from_errno("fflush");
3712 return err;
3715 static void
3716 diff_view_indicate_progress(struct tog_view *view)
3718 mvwaddstr(view->window, 0, 0, "diffing...");
3719 update_panels();
3720 doupdate();
3723 static const struct got_error *
3724 search_start_diff_view(struct tog_view *view)
3726 struct tog_diff_view_state *s = &view->state.diff;
3728 s->matched_line = 0;
3729 return NULL;
3732 static const struct got_error *
3733 search_next_diff_view(struct tog_view *view)
3735 struct tog_diff_view_state *s = &view->state.diff;
3736 const struct got_error *err = NULL;
3737 int lineno;
3738 char *line = NULL;
3739 size_t linesize = 0;
3740 ssize_t linelen;
3742 if (!view->searching) {
3743 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3744 return NULL;
3747 if (s->matched_line) {
3748 if (view->searching == TOG_SEARCH_FORWARD)
3749 lineno = s->matched_line + 1;
3750 else
3751 lineno = s->matched_line - 1;
3752 } else
3753 lineno = s->first_displayed_line;
3755 while (1) {
3756 off_t offset;
3758 if (lineno <= 0 || lineno > s->nlines) {
3759 if (s->matched_line == 0) {
3760 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3761 break;
3764 if (view->searching == TOG_SEARCH_FORWARD)
3765 lineno = 1;
3766 else
3767 lineno = s->nlines;
3770 offset = s->line_offsets[lineno - 1];
3771 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3772 free(line);
3773 return got_error_from_errno("fseeko");
3775 linelen = getline(&line, &linesize, s->f);
3776 if (linelen != -1) {
3777 char *exstr;
3778 err = expand_tab(&exstr, line);
3779 if (err)
3780 break;
3781 if (match_line(exstr, &view->regex, 1,
3782 &view->regmatch)) {
3783 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3784 s->matched_line = lineno;
3785 free(exstr);
3786 break;
3788 free(exstr);
3790 if (view->searching == TOG_SEARCH_FORWARD)
3791 lineno++;
3792 else
3793 lineno--;
3795 free(line);
3797 if (s->matched_line) {
3798 s->first_displayed_line = s->matched_line;
3799 s->selected_line = 1;
3802 return err;
3805 static const struct got_error *
3806 close_diff_view(struct tog_view *view)
3808 const struct got_error *err = NULL;
3809 struct tog_diff_view_state *s = &view->state.diff;
3811 free(s->id1);
3812 s->id1 = NULL;
3813 free(s->id2);
3814 s->id2 = NULL;
3815 if (s->f && fclose(s->f) == EOF)
3816 err = got_error_from_errno("fclose");
3817 s->f = NULL;
3818 if (s->f1 && fclose(s->f1) == EOF)
3819 err = got_error_from_errno("fclose");
3820 s->f1 = NULL;
3821 if (s->f2 && fclose(s->f2) == EOF)
3822 err = got_error_from_errno("fclose");
3823 s->f2 = NULL;
3824 free_colors(&s->colors);
3825 free(s->line_offsets);
3826 s->line_offsets = NULL;
3827 s->nlines = 0;
3828 return err;
3831 static const struct got_error *
3832 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3833 struct got_object_id *id2, const char *label1, const char *label2,
3834 int diff_context, int ignore_whitespace, int force_text_diff,
3835 struct tog_view *log_view, struct got_repository *repo)
3837 const struct got_error *err;
3838 struct tog_diff_view_state *s = &view->state.diff;
3840 memset(s, 0, sizeof(*s));
3842 if (id1 != NULL && id2 != NULL) {
3843 int type1, type2;
3844 err = got_object_get_type(&type1, repo, id1);
3845 if (err)
3846 return err;
3847 err = got_object_get_type(&type2, repo, id2);
3848 if (err)
3849 return err;
3851 if (type1 != type2)
3852 return got_error(GOT_ERR_OBJ_TYPE);
3854 s->first_displayed_line = 1;
3855 s->last_displayed_line = view->nlines;
3856 s->selected_line = 1;
3857 s->repo = repo;
3858 s->id1 = id1;
3859 s->id2 = id2;
3860 s->label1 = label1;
3861 s->label2 = label2;
3863 if (id1) {
3864 s->id1 = got_object_id_dup(id1);
3865 if (s->id1 == NULL)
3866 return got_error_from_errno("got_object_id_dup");
3867 } else
3868 s->id1 = NULL;
3870 s->id2 = got_object_id_dup(id2);
3871 if (s->id2 == NULL) {
3872 err = got_error_from_errno("got_object_id_dup");
3873 goto done;
3876 s->f1 = got_opentemp();
3877 if (s->f1 == NULL) {
3878 err = got_error_from_errno("got_opentemp");
3879 goto done;
3882 s->f2 = got_opentemp();
3883 if (s->f2 == NULL) {
3884 err = got_error_from_errno("got_opentemp");
3885 goto done;
3888 s->first_displayed_line = 1;
3889 s->last_displayed_line = view->nlines;
3890 s->diff_context = diff_context;
3891 s->ignore_whitespace = ignore_whitespace;
3892 s->force_text_diff = force_text_diff;
3893 s->log_view = log_view;
3894 s->repo = repo;
3896 STAILQ_INIT(&s->colors);
3897 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3898 err = add_color(&s->colors,
3899 "^-", TOG_COLOR_DIFF_MINUS,
3900 get_color_value("TOG_COLOR_DIFF_MINUS"));
3901 if (err)
3902 goto done;
3903 err = add_color(&s->colors, "^\\+",
3904 TOG_COLOR_DIFF_PLUS,
3905 get_color_value("TOG_COLOR_DIFF_PLUS"));
3906 if (err)
3907 goto done;
3908 err = add_color(&s->colors,
3909 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3910 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3911 if (err)
3912 goto done;
3914 err = add_color(&s->colors,
3915 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3916 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3917 get_color_value("TOG_COLOR_DIFF_META"));
3918 if (err)
3919 goto done;
3921 err = add_color(&s->colors,
3922 "^(from|via): ", TOG_COLOR_AUTHOR,
3923 get_color_value("TOG_COLOR_AUTHOR"));
3924 if (err)
3925 goto done;
3927 err = add_color(&s->colors,
3928 "^date: ", TOG_COLOR_DATE,
3929 get_color_value("TOG_COLOR_DATE"));
3930 if (err)
3931 goto done;
3934 if (log_view && view_is_splitscreen(view))
3935 show_log_view(log_view); /* draw vborder */
3936 diff_view_indicate_progress(view);
3938 err = create_diff(s);
3940 view->show = show_diff_view;
3941 view->input = input_diff_view;
3942 view->close = close_diff_view;
3943 view->search_start = search_start_diff_view;
3944 view->search_next = search_next_diff_view;
3945 done:
3946 if (err)
3947 close_diff_view(view);
3948 return err;
3951 static const struct got_error *
3952 show_diff_view(struct tog_view *view)
3954 const struct got_error *err;
3955 struct tog_diff_view_state *s = &view->state.diff;
3956 char *id_str1 = NULL, *id_str2, *header;
3957 const char *label1, *label2;
3959 if (s->id1) {
3960 err = got_object_id_str(&id_str1, s->id1);
3961 if (err)
3962 return err;
3963 label1 = s->label1 ? : id_str1;
3964 } else
3965 label1 = "/dev/null";
3967 err = got_object_id_str(&id_str2, s->id2);
3968 if (err)
3969 return err;
3970 label2 = s->label2 ? : id_str2;
3972 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3973 err = got_error_from_errno("asprintf");
3974 free(id_str1);
3975 free(id_str2);
3976 return err;
3978 free(id_str1);
3979 free(id_str2);
3981 err = draw_file(view, header);
3982 free(header);
3983 return err;
3986 static const struct got_error *
3987 set_selected_commit(struct tog_diff_view_state *s,
3988 struct commit_queue_entry *entry)
3990 const struct got_error *err;
3991 const struct got_object_id_queue *parent_ids;
3992 struct got_commit_object *selected_commit;
3993 struct got_object_qid *pid;
3995 free(s->id2);
3996 s->id2 = got_object_id_dup(entry->id);
3997 if (s->id2 == NULL)
3998 return got_error_from_errno("got_object_id_dup");
4000 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4001 if (err)
4002 return err;
4003 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4004 free(s->id1);
4005 pid = STAILQ_FIRST(parent_ids);
4006 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4007 got_object_commit_close(selected_commit);
4008 return NULL;
4011 static const struct got_error *
4012 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4014 const struct got_error *err = NULL;
4015 struct tog_diff_view_state *s = &view->state.diff;
4016 struct tog_log_view_state *ls;
4017 struct commit_queue_entry *old_selected_entry;
4018 char *line = NULL;
4019 size_t linesize = 0;
4020 ssize_t linelen;
4021 int i, nscroll = view->nlines - 1;
4023 switch (ch) {
4024 case '0':
4025 view->x = 0;
4026 break;
4027 case '$':
4028 view->x = MAX(view->maxx - view->ncols / 3, 0);
4029 break;
4030 case KEY_RIGHT:
4031 case 'l':
4032 if (view->x + view->ncols / 3 < view->maxx)
4033 view->x += 2; /* move two columns right */
4034 break;
4035 case KEY_LEFT:
4036 case 'h':
4037 view->x -= MIN(view->x, 2); /* move two columns back */
4038 break;
4039 case 'a':
4040 case 'w':
4041 if (ch == 'a')
4042 s->force_text_diff = !s->force_text_diff;
4043 if (ch == 'w')
4044 s->ignore_whitespace = !s->ignore_whitespace;
4045 wclear(view->window);
4046 s->first_displayed_line = 1;
4047 s->last_displayed_line = view->nlines;
4048 s->matched_line = 0;
4049 diff_view_indicate_progress(view);
4050 err = create_diff(s);
4051 break;
4052 case 'g':
4053 case KEY_HOME:
4054 s->first_displayed_line = 1;
4055 break;
4056 case 'G':
4057 case KEY_END:
4058 if (s->eof)
4059 break;
4061 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4062 s->eof = 1;
4063 break;
4064 case 'k':
4065 case KEY_UP:
4066 case CTRL('p'):
4067 if (s->first_displayed_line > 1)
4068 s->first_displayed_line--;
4069 break;
4070 case CTRL('u'):
4071 case 'u':
4072 nscroll /= 2;
4073 /* FALL THROUGH */
4074 case KEY_PPAGE:
4075 case CTRL('b'):
4076 case 'b':
4077 if (s->first_displayed_line == 1)
4078 break;
4079 i = 0;
4080 while (i++ < nscroll && s->first_displayed_line > 1)
4081 s->first_displayed_line--;
4082 break;
4083 case 'j':
4084 case KEY_DOWN:
4085 case CTRL('n'):
4086 if (!s->eof)
4087 s->first_displayed_line++;
4088 break;
4089 case CTRL('d'):
4090 case 'd':
4091 nscroll /= 2;
4092 /* FALL THROUGH */
4093 case KEY_NPAGE:
4094 case CTRL('f'):
4095 case 'f':
4096 case ' ':
4097 if (s->eof)
4098 break;
4099 i = 0;
4100 while (!s->eof && i++ < nscroll) {
4101 linelen = getline(&line, &linesize, s->f);
4102 s->first_displayed_line++;
4103 if (linelen == -1) {
4104 if (feof(s->f)) {
4105 s->eof = 1;
4106 } else
4107 err = got_ferror(s->f, GOT_ERR_IO);
4108 break;
4111 free(line);
4112 break;
4113 case '[':
4114 if (s->diff_context > 0) {
4115 s->diff_context--;
4116 s->matched_line = 0;
4117 diff_view_indicate_progress(view);
4118 err = create_diff(s);
4119 if (s->first_displayed_line + view->nlines - 1 >
4120 s->nlines) {
4121 s->first_displayed_line = 1;
4122 s->last_displayed_line = view->nlines;
4125 break;
4126 case ']':
4127 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4128 s->diff_context++;
4129 s->matched_line = 0;
4130 diff_view_indicate_progress(view);
4131 err = create_diff(s);
4133 break;
4134 case '<':
4135 case ',':
4136 if (s->log_view == NULL)
4137 break;
4138 ls = &s->log_view->state.log;
4139 old_selected_entry = ls->selected_entry;
4141 err = input_log_view(NULL, s->log_view, KEY_UP);
4142 if (err)
4143 break;
4145 if (old_selected_entry == ls->selected_entry)
4146 break;
4148 err = set_selected_commit(s, ls->selected_entry);
4149 if (err)
4150 break;
4152 s->first_displayed_line = 1;
4153 s->last_displayed_line = view->nlines;
4154 s->matched_line = 0;
4155 view->x = 0;
4157 diff_view_indicate_progress(view);
4158 err = create_diff(s);
4159 break;
4160 case '>':
4161 case '.':
4162 if (s->log_view == NULL)
4163 break;
4164 ls = &s->log_view->state.log;
4165 old_selected_entry = ls->selected_entry;
4167 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4168 if (err)
4169 break;
4171 if (old_selected_entry == ls->selected_entry)
4172 break;
4174 err = set_selected_commit(s, ls->selected_entry);
4175 if (err)
4176 break;
4178 s->first_displayed_line = 1;
4179 s->last_displayed_line = view->nlines;
4180 s->matched_line = 0;
4181 view->x = 0;
4183 diff_view_indicate_progress(view);
4184 err = create_diff(s);
4185 break;
4186 default:
4187 break;
4190 return err;
4193 static const struct got_error *
4194 cmd_diff(int argc, char *argv[])
4196 const struct got_error *error = NULL;
4197 struct got_repository *repo = NULL;
4198 struct got_worktree *worktree = NULL;
4199 struct got_object_id *id1 = NULL, *id2 = NULL;
4200 char *repo_path = NULL, *cwd = NULL;
4201 char *id_str1 = NULL, *id_str2 = NULL;
4202 char *label1 = NULL, *label2 = NULL;
4203 int diff_context = 3, ignore_whitespace = 0;
4204 int ch, force_text_diff = 0;
4205 const char *errstr;
4206 struct tog_view *view;
4207 int *pack_fds = NULL;
4209 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4210 switch (ch) {
4211 case 'a':
4212 force_text_diff = 1;
4213 break;
4214 case 'C':
4215 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4216 &errstr);
4217 if (errstr != NULL)
4218 errx(1, "number of context lines is %s: %s",
4219 errstr, errstr);
4220 break;
4221 case 'r':
4222 repo_path = realpath(optarg, NULL);
4223 if (repo_path == NULL)
4224 return got_error_from_errno2("realpath",
4225 optarg);
4226 got_path_strip_trailing_slashes(repo_path);
4227 break;
4228 case 'w':
4229 ignore_whitespace = 1;
4230 break;
4231 default:
4232 usage_diff();
4233 /* NOTREACHED */
4237 argc -= optind;
4238 argv += optind;
4240 if (argc == 0) {
4241 usage_diff(); /* TODO show local worktree changes */
4242 } else if (argc == 2) {
4243 id_str1 = argv[0];
4244 id_str2 = argv[1];
4245 } else
4246 usage_diff();
4248 error = got_repo_pack_fds_open(&pack_fds);
4249 if (error)
4250 goto done;
4252 if (repo_path == NULL) {
4253 cwd = getcwd(NULL, 0);
4254 if (cwd == NULL)
4255 return got_error_from_errno("getcwd");
4256 error = got_worktree_open(&worktree, cwd);
4257 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4258 goto done;
4259 if (worktree)
4260 repo_path =
4261 strdup(got_worktree_get_repo_path(worktree));
4262 else
4263 repo_path = strdup(cwd);
4264 if (repo_path == NULL) {
4265 error = got_error_from_errno("strdup");
4266 goto done;
4270 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4271 if (error)
4272 goto done;
4274 init_curses();
4276 error = apply_unveil(got_repo_get_path(repo), NULL);
4277 if (error)
4278 goto done;
4280 error = tog_load_refs(repo, 0);
4281 if (error)
4282 goto done;
4284 error = got_repo_match_object_id(&id1, &label1, id_str1,
4285 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4286 if (error)
4287 goto done;
4289 error = got_repo_match_object_id(&id2, &label2, id_str2,
4290 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4291 if (error)
4292 goto done;
4294 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4295 if (view == NULL) {
4296 error = got_error_from_errno("view_open");
4297 goto done;
4299 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4300 ignore_whitespace, force_text_diff, NULL, repo);
4301 if (error)
4302 goto done;
4303 error = view_loop(view);
4304 done:
4305 free(label1);
4306 free(label2);
4307 free(repo_path);
4308 free(cwd);
4309 if (repo) {
4310 const struct got_error *close_err = got_repo_close(repo);
4311 if (error == NULL)
4312 error = close_err;
4314 if (worktree)
4315 got_worktree_close(worktree);
4316 if (pack_fds) {
4317 const struct got_error *pack_err =
4318 got_repo_pack_fds_close(pack_fds);
4319 if (error == NULL)
4320 error = pack_err;
4322 tog_free_refs();
4323 return error;
4326 __dead static void
4327 usage_blame(void)
4329 endwin();
4330 fprintf(stderr,
4331 "usage: %s blame [-c commit] [-r repository-path] path\n",
4332 getprogname());
4333 exit(1);
4336 struct tog_blame_line {
4337 int annotated;
4338 struct got_object_id *id;
4341 static const struct got_error *
4342 draw_blame(struct tog_view *view)
4344 struct tog_blame_view_state *s = &view->state.blame;
4345 struct tog_blame *blame = &s->blame;
4346 regmatch_t *regmatch = &view->regmatch;
4347 const struct got_error *err;
4348 int lineno = 0, nprinted = 0;
4349 char *line = NULL;
4350 size_t linesize = 0;
4351 ssize_t linelen;
4352 wchar_t *wline;
4353 int width;
4354 struct tog_blame_line *blame_line;
4355 struct got_object_id *prev_id = NULL;
4356 char *id_str;
4357 struct tog_color *tc;
4359 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4360 if (err)
4361 return err;
4363 rewind(blame->f);
4364 werase(view->window);
4366 if (asprintf(&line, "commit %s", id_str) == -1) {
4367 err = got_error_from_errno("asprintf");
4368 free(id_str);
4369 return err;
4372 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4373 free(line);
4374 line = NULL;
4375 if (err)
4376 return err;
4377 if (view_needs_focus_indication(view))
4378 wstandout(view->window);
4379 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4380 if (tc)
4381 wattr_on(view->window,
4382 COLOR_PAIR(tc->colorpair), NULL);
4383 waddwstr(view->window, wline);
4384 if (tc)
4385 wattr_off(view->window,
4386 COLOR_PAIR(tc->colorpair), NULL);
4387 if (view_needs_focus_indication(view))
4388 wstandend(view->window);
4389 free(wline);
4390 wline = NULL;
4391 if (width < view->ncols - 1)
4392 waddch(view->window, '\n');
4394 if (asprintf(&line, "[%d/%d] %s%s",
4395 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4396 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4397 free(id_str);
4398 return got_error_from_errno("asprintf");
4400 free(id_str);
4401 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4402 free(line);
4403 line = NULL;
4404 if (err)
4405 return err;
4406 waddwstr(view->window, wline);
4407 free(wline);
4408 wline = NULL;
4409 if (width < view->ncols - 1)
4410 waddch(view->window, '\n');
4412 s->eof = 0;
4413 view->maxx = 0;
4414 while (nprinted < view->nlines - 2) {
4415 linelen = getline(&line, &linesize, blame->f);
4416 if (linelen == -1) {
4417 if (feof(blame->f)) {
4418 s->eof = 1;
4419 break;
4421 free(line);
4422 return got_ferror(blame->f, GOT_ERR_IO);
4424 if (++lineno < s->first_displayed_line)
4425 continue;
4427 /* Set view->maxx based on full line length. */
4428 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4429 if (err) {
4430 free(line);
4431 return err;
4433 free(wline);
4434 wline = NULL;
4435 view->maxx = MAX(view->maxx, width);
4437 if (view->focussed && nprinted == s->selected_line - 1)
4438 wstandout(view->window);
4440 if (blame->nlines > 0) {
4441 blame_line = &blame->lines[lineno - 1];
4442 if (blame_line->annotated && prev_id &&
4443 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4444 !(view->focussed &&
4445 nprinted == s->selected_line - 1)) {
4446 waddstr(view->window, " ");
4447 } else if (blame_line->annotated) {
4448 char *id_str;
4449 err = got_object_id_str(&id_str,
4450 blame_line->id);
4451 if (err) {
4452 free(line);
4453 return err;
4455 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4456 if (tc)
4457 wattr_on(view->window,
4458 COLOR_PAIR(tc->colorpair), NULL);
4459 wprintw(view->window, "%.8s", id_str);
4460 if (tc)
4461 wattr_off(view->window,
4462 COLOR_PAIR(tc->colorpair), NULL);
4463 free(id_str);
4464 prev_id = blame_line->id;
4465 } else {
4466 waddstr(view->window, "........");
4467 prev_id = NULL;
4469 } else {
4470 waddstr(view->window, "........");
4471 prev_id = NULL;
4474 if (view->focussed && nprinted == s->selected_line - 1)
4475 wstandend(view->window);
4476 waddstr(view->window, " ");
4478 if (view->ncols <= 9) {
4479 width = 9;
4480 } else if (s->first_displayed_line + nprinted ==
4481 s->matched_line &&
4482 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4483 err = add_matched_line(&width, line, view->ncols - 9, 9,
4484 view->window, view->x, regmatch);
4485 if (err) {
4486 free(line);
4487 return err;
4489 width += 9;
4490 } else {
4491 int skip;
4492 err = format_line(&wline, &width, &skip, line,
4493 view->x, view->ncols - 9, 9, 1);
4494 if (err) {
4495 free(line);
4496 return err;
4498 waddwstr(view->window, &wline[skip]);
4499 width += 9;
4500 free(wline);
4501 wline = NULL;
4504 if (width <= view->ncols - 1)
4505 waddch(view->window, '\n');
4506 if (++nprinted == 1)
4507 s->first_displayed_line = lineno;
4509 free(line);
4510 s->last_displayed_line = lineno;
4512 view_vborder(view);
4514 return NULL;
4517 static const struct got_error *
4518 blame_cb(void *arg, int nlines, int lineno,
4519 struct got_commit_object *commit, struct got_object_id *id)
4521 const struct got_error *err = NULL;
4522 struct tog_blame_cb_args *a = arg;
4523 struct tog_blame_line *line;
4524 int errcode;
4526 if (nlines != a->nlines ||
4527 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4528 return got_error(GOT_ERR_RANGE);
4530 errcode = pthread_mutex_lock(&tog_mutex);
4531 if (errcode)
4532 return got_error_set_errno(errcode, "pthread_mutex_lock");
4534 if (*a->quit) { /* user has quit the blame view */
4535 err = got_error(GOT_ERR_ITER_COMPLETED);
4536 goto done;
4539 if (lineno == -1)
4540 goto done; /* no change in this commit */
4542 line = &a->lines[lineno - 1];
4543 if (line->annotated)
4544 goto done;
4546 line->id = got_object_id_dup(id);
4547 if (line->id == NULL) {
4548 err = got_error_from_errno("got_object_id_dup");
4549 goto done;
4551 line->annotated = 1;
4552 done:
4553 errcode = pthread_mutex_unlock(&tog_mutex);
4554 if (errcode)
4555 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4556 return err;
4559 static void *
4560 blame_thread(void *arg)
4562 const struct got_error *err, *close_err;
4563 struct tog_blame_thread_args *ta = arg;
4564 struct tog_blame_cb_args *a = ta->cb_args;
4565 int errcode;
4567 err = block_signals_used_by_main_thread();
4568 if (err)
4569 return (void *)err;
4571 err = got_blame(ta->path, a->commit_id, ta->repo,
4572 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4573 if (err && err->code == GOT_ERR_CANCELLED)
4574 err = NULL;
4576 errcode = pthread_mutex_lock(&tog_mutex);
4577 if (errcode)
4578 return (void *)got_error_set_errno(errcode,
4579 "pthread_mutex_lock");
4581 close_err = got_repo_close(ta->repo);
4582 if (err == NULL)
4583 err = close_err;
4584 ta->repo = NULL;
4585 *ta->complete = 1;
4587 errcode = pthread_mutex_unlock(&tog_mutex);
4588 if (errcode && err == NULL)
4589 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4591 return (void *)err;
4594 static struct got_object_id *
4595 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4596 int first_displayed_line, int selected_line)
4598 struct tog_blame_line *line;
4600 if (nlines <= 0)
4601 return NULL;
4603 line = &lines[first_displayed_line - 1 + selected_line - 1];
4604 if (!line->annotated)
4605 return NULL;
4607 return line->id;
4610 static const struct got_error *
4611 stop_blame(struct tog_blame *blame)
4613 const struct got_error *err = NULL;
4614 int i;
4616 if (blame->thread) {
4617 int errcode;
4618 errcode = pthread_mutex_unlock(&tog_mutex);
4619 if (errcode)
4620 return got_error_set_errno(errcode,
4621 "pthread_mutex_unlock");
4622 errcode = pthread_join(blame->thread, (void **)&err);
4623 if (errcode)
4624 return got_error_set_errno(errcode, "pthread_join");
4625 errcode = pthread_mutex_lock(&tog_mutex);
4626 if (errcode)
4627 return got_error_set_errno(errcode,
4628 "pthread_mutex_lock");
4629 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4630 err = NULL;
4631 blame->thread = 0; //NULL;
4633 if (blame->thread_args.repo) {
4634 const struct got_error *close_err;
4635 close_err = got_repo_close(blame->thread_args.repo);
4636 if (err == NULL)
4637 err = close_err;
4638 blame->thread_args.repo = NULL;
4640 if (blame->f) {
4641 if (fclose(blame->f) == EOF && err == NULL)
4642 err = got_error_from_errno("fclose");
4643 blame->f = NULL;
4645 if (blame->lines) {
4646 for (i = 0; i < blame->nlines; i++)
4647 free(blame->lines[i].id);
4648 free(blame->lines);
4649 blame->lines = NULL;
4651 free(blame->cb_args.commit_id);
4652 blame->cb_args.commit_id = NULL;
4653 if (blame->pack_fds) {
4654 const struct got_error *pack_err =
4655 got_repo_pack_fds_close(blame->pack_fds);
4656 if (err == NULL)
4657 err = pack_err;
4658 blame->pack_fds = NULL;
4660 return err;
4663 static const struct got_error *
4664 cancel_blame_view(void *arg)
4666 const struct got_error *err = NULL;
4667 int *done = arg;
4668 int errcode;
4670 errcode = pthread_mutex_lock(&tog_mutex);
4671 if (errcode)
4672 return got_error_set_errno(errcode,
4673 "pthread_mutex_unlock");
4675 if (*done)
4676 err = got_error(GOT_ERR_CANCELLED);
4678 errcode = pthread_mutex_unlock(&tog_mutex);
4679 if (errcode)
4680 return got_error_set_errno(errcode,
4681 "pthread_mutex_lock");
4683 return err;
4686 static const struct got_error *
4687 run_blame(struct tog_view *view)
4689 struct tog_blame_view_state *s = &view->state.blame;
4690 struct tog_blame *blame = &s->blame;
4691 const struct got_error *err = NULL;
4692 struct got_commit_object *commit = NULL;
4693 struct got_blob_object *blob = NULL;
4694 struct got_repository *thread_repo = NULL;
4695 struct got_object_id *obj_id = NULL;
4696 int obj_type;
4697 int *pack_fds = NULL;
4699 err = got_object_open_as_commit(&commit, s->repo,
4700 &s->blamed_commit->id);
4701 if (err)
4702 return err;
4704 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4705 if (err)
4706 goto done;
4708 err = got_object_get_type(&obj_type, s->repo, obj_id);
4709 if (err)
4710 goto done;
4712 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4713 err = got_error(GOT_ERR_OBJ_TYPE);
4714 goto done;
4717 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4718 if (err)
4719 goto done;
4720 blame->f = got_opentemp();
4721 if (blame->f == NULL) {
4722 err = got_error_from_errno("got_opentemp");
4723 goto done;
4725 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4726 &blame->line_offsets, blame->f, blob);
4727 if (err)
4728 goto done;
4729 if (blame->nlines == 0) {
4730 s->blame_complete = 1;
4731 goto done;
4734 /* Don't include \n at EOF in the blame line count. */
4735 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4736 blame->nlines--;
4738 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4739 if (blame->lines == NULL) {
4740 err = got_error_from_errno("calloc");
4741 goto done;
4744 err = got_repo_pack_fds_open(&pack_fds);
4745 if (err)
4746 goto done;
4747 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4748 pack_fds);
4749 if (err)
4750 goto done;
4752 blame->pack_fds = pack_fds;
4753 blame->cb_args.view = view;
4754 blame->cb_args.lines = blame->lines;
4755 blame->cb_args.nlines = blame->nlines;
4756 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4757 if (blame->cb_args.commit_id == NULL) {
4758 err = got_error_from_errno("got_object_id_dup");
4759 goto done;
4761 blame->cb_args.quit = &s->done;
4763 blame->thread_args.path = s->path;
4764 blame->thread_args.repo = thread_repo;
4765 blame->thread_args.cb_args = &blame->cb_args;
4766 blame->thread_args.complete = &s->blame_complete;
4767 blame->thread_args.cancel_cb = cancel_blame_view;
4768 blame->thread_args.cancel_arg = &s->done;
4769 s->blame_complete = 0;
4771 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4772 s->first_displayed_line = 1;
4773 s->last_displayed_line = view->nlines;
4774 s->selected_line = 1;
4776 s->matched_line = 0;
4778 done:
4779 if (commit)
4780 got_object_commit_close(commit);
4781 if (blob)
4782 got_object_blob_close(blob);
4783 free(obj_id);
4784 if (err)
4785 stop_blame(blame);
4786 return err;
4789 static const struct got_error *
4790 open_blame_view(struct tog_view *view, char *path,
4791 struct got_object_id *commit_id, struct got_repository *repo)
4793 const struct got_error *err = NULL;
4794 struct tog_blame_view_state *s = &view->state.blame;
4796 STAILQ_INIT(&s->blamed_commits);
4798 s->path = strdup(path);
4799 if (s->path == NULL)
4800 return got_error_from_errno("strdup");
4802 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4803 if (err) {
4804 free(s->path);
4805 return err;
4808 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4809 s->first_displayed_line = 1;
4810 s->last_displayed_line = view->nlines;
4811 s->selected_line = 1;
4812 s->blame_complete = 0;
4813 s->repo = repo;
4814 s->commit_id = commit_id;
4815 memset(&s->blame, 0, sizeof(s->blame));
4817 STAILQ_INIT(&s->colors);
4818 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4819 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4820 get_color_value("TOG_COLOR_COMMIT"));
4821 if (err)
4822 return err;
4825 view->show = show_blame_view;
4826 view->input = input_blame_view;
4827 view->close = close_blame_view;
4828 view->search_start = search_start_blame_view;
4829 view->search_next = search_next_blame_view;
4831 return run_blame(view);
4834 static const struct got_error *
4835 close_blame_view(struct tog_view *view)
4837 const struct got_error *err = NULL;
4838 struct tog_blame_view_state *s = &view->state.blame;
4840 if (s->blame.thread)
4841 err = stop_blame(&s->blame);
4843 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4844 struct got_object_qid *blamed_commit;
4845 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4846 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4847 got_object_qid_free(blamed_commit);
4850 free(s->path);
4851 free_colors(&s->colors);
4852 return err;
4855 static const struct got_error *
4856 search_start_blame_view(struct tog_view *view)
4858 struct tog_blame_view_state *s = &view->state.blame;
4860 s->matched_line = 0;
4861 return NULL;
4864 static const struct got_error *
4865 search_next_blame_view(struct tog_view *view)
4867 struct tog_blame_view_state *s = &view->state.blame;
4868 const struct got_error *err = NULL;
4869 int lineno;
4870 char *line = NULL;
4871 size_t linesize = 0;
4872 ssize_t linelen;
4874 if (!view->searching) {
4875 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4876 return NULL;
4879 if (s->matched_line) {
4880 if (view->searching == TOG_SEARCH_FORWARD)
4881 lineno = s->matched_line + 1;
4882 else
4883 lineno = s->matched_line - 1;
4884 } else
4885 lineno = s->first_displayed_line - 1 + s->selected_line;
4887 while (1) {
4888 off_t offset;
4890 if (lineno <= 0 || lineno > s->blame.nlines) {
4891 if (s->matched_line == 0) {
4892 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4893 break;
4896 if (view->searching == TOG_SEARCH_FORWARD)
4897 lineno = 1;
4898 else
4899 lineno = s->blame.nlines;
4902 offset = s->blame.line_offsets[lineno - 1];
4903 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4904 free(line);
4905 return got_error_from_errno("fseeko");
4907 linelen = getline(&line, &linesize, s->blame.f);
4908 if (linelen != -1) {
4909 char *exstr;
4910 err = expand_tab(&exstr, line);
4911 if (err)
4912 break;
4913 if (match_line(exstr, &view->regex, 1,
4914 &view->regmatch)) {
4915 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4916 s->matched_line = lineno;
4917 free(exstr);
4918 break;
4920 free(exstr);
4922 if (view->searching == TOG_SEARCH_FORWARD)
4923 lineno++;
4924 else
4925 lineno--;
4927 free(line);
4929 if (s->matched_line) {
4930 s->first_displayed_line = s->matched_line;
4931 s->selected_line = 1;
4934 return err;
4937 static const struct got_error *
4938 show_blame_view(struct tog_view *view)
4940 const struct got_error *err = NULL;
4941 struct tog_blame_view_state *s = &view->state.blame;
4942 int errcode;
4944 if (s->blame.thread == 0 && !s->blame_complete) {
4945 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4946 &s->blame.thread_args);
4947 if (errcode)
4948 return got_error_set_errno(errcode, "pthread_create");
4950 halfdelay(1); /* fast refresh while annotating */
4953 if (s->blame_complete)
4954 halfdelay(10); /* disable fast refresh */
4956 err = draw_blame(view);
4958 view_vborder(view);
4959 return err;
4962 static const struct got_error *
4963 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4965 const struct got_error *err = NULL, *thread_err = NULL;
4966 struct tog_view *diff_view;
4967 struct tog_blame_view_state *s = &view->state.blame;
4968 int begin_x = 0, nscroll = view->nlines - 2;
4970 switch (ch) {
4971 case '0':
4972 view->x = 0;
4973 break;
4974 case '$':
4975 view->x = MAX(view->maxx - view->ncols / 3, 0);
4976 break;
4977 case KEY_RIGHT:
4978 case 'l':
4979 if (view->x + view->ncols / 3 < view->maxx)
4980 view->x += 2; /* move two columns right */
4981 break;
4982 case KEY_LEFT:
4983 case 'h':
4984 view->x -= MIN(view->x, 2); /* move two columns back */
4985 break;
4986 case 'q':
4987 s->done = 1;
4988 break;
4989 case 'g':
4990 case KEY_HOME:
4991 s->selected_line = 1;
4992 s->first_displayed_line = 1;
4993 break;
4994 case 'G':
4995 case KEY_END:
4996 if (s->blame.nlines < view->nlines - 2) {
4997 s->selected_line = s->blame.nlines;
4998 s->first_displayed_line = 1;
4999 } else {
5000 s->selected_line = view->nlines - 2;
5001 s->first_displayed_line = s->blame.nlines -
5002 (view->nlines - 3);
5004 break;
5005 case 'k':
5006 case KEY_UP:
5007 case CTRL('p'):
5008 if (s->selected_line > 1)
5009 s->selected_line--;
5010 else if (s->selected_line == 1 &&
5011 s->first_displayed_line > 1)
5012 s->first_displayed_line--;
5013 break;
5014 case CTRL('u'):
5015 case 'u':
5016 nscroll /= 2;
5017 /* FALL THROUGH */
5018 case KEY_PPAGE:
5019 case CTRL('b'):
5020 case 'b':
5021 if (s->first_displayed_line == 1) {
5022 s->selected_line = MAX(1, s->selected_line - nscroll);
5023 break;
5025 if (s->first_displayed_line > nscroll)
5026 s->first_displayed_line -= nscroll;
5027 else
5028 s->first_displayed_line = 1;
5029 break;
5030 case 'j':
5031 case KEY_DOWN:
5032 case CTRL('n'):
5033 if (s->selected_line < view->nlines - 2 &&
5034 s->first_displayed_line +
5035 s->selected_line <= s->blame.nlines)
5036 s->selected_line++;
5037 else if (s->last_displayed_line <
5038 s->blame.nlines)
5039 s->first_displayed_line++;
5040 break;
5041 case 'c':
5042 case 'p': {
5043 struct got_object_id *id = NULL;
5044 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5045 s->first_displayed_line, s->selected_line);
5046 if (id == NULL)
5047 break;
5048 if (ch == 'p') {
5049 struct got_commit_object *commit, *pcommit;
5050 struct got_object_qid *pid;
5051 struct got_object_id *blob_id = NULL;
5052 int obj_type;
5053 err = got_object_open_as_commit(&commit,
5054 s->repo, id);
5055 if (err)
5056 break;
5057 pid = STAILQ_FIRST(
5058 got_object_commit_get_parent_ids(commit));
5059 if (pid == NULL) {
5060 got_object_commit_close(commit);
5061 break;
5063 /* Check if path history ends here. */
5064 err = got_object_open_as_commit(&pcommit,
5065 s->repo, &pid->id);
5066 if (err)
5067 break;
5068 err = got_object_id_by_path(&blob_id, s->repo,
5069 pcommit, s->path);
5070 got_object_commit_close(pcommit);
5071 if (err) {
5072 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5073 err = NULL;
5074 got_object_commit_close(commit);
5075 break;
5077 err = got_object_get_type(&obj_type, s->repo,
5078 blob_id);
5079 free(blob_id);
5080 /* Can't blame non-blob type objects. */
5081 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5082 got_object_commit_close(commit);
5083 break;
5085 err = got_object_qid_alloc(&s->blamed_commit,
5086 &pid->id);
5087 got_object_commit_close(commit);
5088 } else {
5089 if (got_object_id_cmp(id,
5090 &s->blamed_commit->id) == 0)
5091 break;
5092 err = got_object_qid_alloc(&s->blamed_commit,
5093 id);
5095 if (err)
5096 break;
5097 s->done = 1;
5098 thread_err = stop_blame(&s->blame);
5099 s->done = 0;
5100 if (thread_err)
5101 break;
5102 STAILQ_INSERT_HEAD(&s->blamed_commits,
5103 s->blamed_commit, entry);
5104 err = run_blame(view);
5105 if (err)
5106 break;
5107 break;
5109 case 'C': {
5110 struct got_object_qid *first;
5111 first = STAILQ_FIRST(&s->blamed_commits);
5112 if (!got_object_id_cmp(&first->id, s->commit_id))
5113 break;
5114 s->done = 1;
5115 thread_err = stop_blame(&s->blame);
5116 s->done = 0;
5117 if (thread_err)
5118 break;
5119 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5120 got_object_qid_free(s->blamed_commit);
5121 s->blamed_commit =
5122 STAILQ_FIRST(&s->blamed_commits);
5123 err = run_blame(view);
5124 if (err)
5125 break;
5126 break;
5128 case KEY_ENTER:
5129 case '\r': {
5130 struct got_object_id *id = NULL;
5131 struct got_object_qid *pid;
5132 struct got_commit_object *commit = NULL;
5133 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5134 s->first_displayed_line, s->selected_line);
5135 if (id == NULL)
5136 break;
5137 err = got_object_open_as_commit(&commit, s->repo, id);
5138 if (err)
5139 break;
5140 pid = STAILQ_FIRST(
5141 got_object_commit_get_parent_ids(commit));
5142 if (view_is_parent_view(view))
5143 begin_x = view_split_begin_x(view->begin_x);
5144 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5145 if (diff_view == NULL) {
5146 got_object_commit_close(commit);
5147 err = got_error_from_errno("view_open");
5148 break;
5150 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5151 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5152 got_object_commit_close(commit);
5153 if (err) {
5154 view_close(diff_view);
5155 break;
5157 view->focussed = 0;
5158 diff_view->focussed = 1;
5159 if (view_is_parent_view(view)) {
5160 err = view_close_child(view);
5161 if (err)
5162 break;
5163 err = view_set_child(view, diff_view);
5164 if (err)
5165 break;
5166 view->focus_child = 1;
5167 } else
5168 *new_view = diff_view;
5169 if (err)
5170 break;
5171 break;
5173 case CTRL('d'):
5174 case 'd':
5175 nscroll /= 2;
5176 /* FALL THROUGH */
5177 case KEY_NPAGE:
5178 case CTRL('f'):
5179 case 'f':
5180 case ' ':
5181 if (s->last_displayed_line >= s->blame.nlines &&
5182 s->selected_line >= MIN(s->blame.nlines,
5183 view->nlines - 2)) {
5184 break;
5186 if (s->last_displayed_line >= s->blame.nlines &&
5187 s->selected_line < view->nlines - 2) {
5188 s->selected_line +=
5189 MIN(nscroll, s->last_displayed_line -
5190 s->first_displayed_line - s->selected_line + 1);
5192 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5193 s->first_displayed_line += nscroll;
5194 else
5195 s->first_displayed_line =
5196 s->blame.nlines - (view->nlines - 3);
5197 break;
5198 case KEY_RESIZE:
5199 if (s->selected_line > view->nlines - 2) {
5200 s->selected_line = MIN(s->blame.nlines,
5201 view->nlines - 2);
5203 break;
5204 default:
5205 break;
5207 return thread_err ? thread_err : err;
5210 static const struct got_error *
5211 cmd_blame(int argc, char *argv[])
5213 const struct got_error *error;
5214 struct got_repository *repo = NULL;
5215 struct got_worktree *worktree = NULL;
5216 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5217 char *link_target = NULL;
5218 struct got_object_id *commit_id = NULL;
5219 struct got_commit_object *commit = NULL;
5220 char *commit_id_str = NULL;
5221 int ch;
5222 struct tog_view *view;
5223 int *pack_fds = NULL;
5225 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5226 switch (ch) {
5227 case 'c':
5228 commit_id_str = optarg;
5229 break;
5230 case 'r':
5231 repo_path = realpath(optarg, NULL);
5232 if (repo_path == NULL)
5233 return got_error_from_errno2("realpath",
5234 optarg);
5235 break;
5236 default:
5237 usage_blame();
5238 /* NOTREACHED */
5242 argc -= optind;
5243 argv += optind;
5245 if (argc != 1)
5246 usage_blame();
5248 error = got_repo_pack_fds_open(&pack_fds);
5249 if (error != NULL)
5250 goto done;
5252 if (repo_path == NULL) {
5253 cwd = getcwd(NULL, 0);
5254 if (cwd == NULL)
5255 return got_error_from_errno("getcwd");
5256 error = got_worktree_open(&worktree, cwd);
5257 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5258 goto done;
5259 if (worktree)
5260 repo_path =
5261 strdup(got_worktree_get_repo_path(worktree));
5262 else
5263 repo_path = strdup(cwd);
5264 if (repo_path == NULL) {
5265 error = got_error_from_errno("strdup");
5266 goto done;
5270 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5271 if (error != NULL)
5272 goto done;
5274 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5275 worktree);
5276 if (error)
5277 goto done;
5279 init_curses();
5281 error = apply_unveil(got_repo_get_path(repo), NULL);
5282 if (error)
5283 goto done;
5285 error = tog_load_refs(repo, 0);
5286 if (error)
5287 goto done;
5289 if (commit_id_str == NULL) {
5290 struct got_reference *head_ref;
5291 error = got_ref_open(&head_ref, repo, worktree ?
5292 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5293 if (error != NULL)
5294 goto done;
5295 error = got_ref_resolve(&commit_id, repo, head_ref);
5296 got_ref_close(head_ref);
5297 } else {
5298 error = got_repo_match_object_id(&commit_id, NULL,
5299 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5301 if (error != NULL)
5302 goto done;
5304 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5305 if (view == NULL) {
5306 error = got_error_from_errno("view_open");
5307 goto done;
5310 error = got_object_open_as_commit(&commit, repo, commit_id);
5311 if (error)
5312 goto done;
5314 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5315 commit, repo);
5316 if (error)
5317 goto done;
5319 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5320 commit_id, repo);
5321 if (error)
5322 goto done;
5323 if (worktree) {
5324 /* Release work tree lock. */
5325 got_worktree_close(worktree);
5326 worktree = NULL;
5328 error = view_loop(view);
5329 done:
5330 free(repo_path);
5331 free(in_repo_path);
5332 free(link_target);
5333 free(cwd);
5334 free(commit_id);
5335 if (commit)
5336 got_object_commit_close(commit);
5337 if (worktree)
5338 got_worktree_close(worktree);
5339 if (repo) {
5340 const struct got_error *close_err = got_repo_close(repo);
5341 if (error == NULL)
5342 error = close_err;
5344 if (pack_fds) {
5345 const struct got_error *pack_err =
5346 got_repo_pack_fds_close(pack_fds);
5347 if (error == NULL)
5348 error = pack_err;
5350 tog_free_refs();
5351 return error;
5354 static const struct got_error *
5355 draw_tree_entries(struct tog_view *view, const char *parent_path)
5357 struct tog_tree_view_state *s = &view->state.tree;
5358 const struct got_error *err = NULL;
5359 struct got_tree_entry *te;
5360 wchar_t *wline;
5361 struct tog_color *tc;
5362 int width, n, i, nentries;
5363 int limit = view->nlines;
5365 s->ndisplayed = 0;
5367 werase(view->window);
5369 if (limit == 0)
5370 return NULL;
5372 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5373 0, 0);
5374 if (err)
5375 return err;
5376 if (view_needs_focus_indication(view))
5377 wstandout(view->window);
5378 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5379 if (tc)
5380 wattr_on(view->window,
5381 COLOR_PAIR(tc->colorpair), NULL);
5382 waddwstr(view->window, wline);
5383 if (tc)
5384 wattr_off(view->window,
5385 COLOR_PAIR(tc->colorpair), NULL);
5386 if (view_needs_focus_indication(view))
5387 wstandend(view->window);
5388 free(wline);
5389 wline = NULL;
5390 if (width < view->ncols - 1)
5391 waddch(view->window, '\n');
5392 if (--limit <= 0)
5393 return NULL;
5394 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5395 0, 0);
5396 if (err)
5397 return err;
5398 waddwstr(view->window, wline);
5399 free(wline);
5400 wline = NULL;
5401 if (width < view->ncols - 1)
5402 waddch(view->window, '\n');
5403 if (--limit <= 0)
5404 return NULL;
5405 waddch(view->window, '\n');
5406 if (--limit <= 0)
5407 return NULL;
5409 if (s->first_displayed_entry == NULL) {
5410 te = got_object_tree_get_first_entry(s->tree);
5411 if (s->selected == 0) {
5412 if (view->focussed)
5413 wstandout(view->window);
5414 s->selected_entry = NULL;
5416 waddstr(view->window, " ..\n"); /* parent directory */
5417 if (s->selected == 0 && view->focussed)
5418 wstandend(view->window);
5419 s->ndisplayed++;
5420 if (--limit <= 0)
5421 return NULL;
5422 n = 1;
5423 } else {
5424 n = 0;
5425 te = s->first_displayed_entry;
5428 nentries = got_object_tree_get_nentries(s->tree);
5429 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5430 char *line = NULL, *id_str = NULL, *link_target = NULL;
5431 const char *modestr = "";
5432 mode_t mode;
5434 te = got_object_tree_get_entry(s->tree, i);
5435 mode = got_tree_entry_get_mode(te);
5437 if (s->show_ids) {
5438 err = got_object_id_str(&id_str,
5439 got_tree_entry_get_id(te));
5440 if (err)
5441 return got_error_from_errno(
5442 "got_object_id_str");
5444 if (got_object_tree_entry_is_submodule(te))
5445 modestr = "$";
5446 else if (S_ISLNK(mode)) {
5447 int i;
5449 err = got_tree_entry_get_symlink_target(&link_target,
5450 te, s->repo);
5451 if (err) {
5452 free(id_str);
5453 return err;
5455 for (i = 0; i < strlen(link_target); i++) {
5456 if (!isprint((unsigned char)link_target[i]))
5457 link_target[i] = '?';
5459 modestr = "@";
5461 else if (S_ISDIR(mode))
5462 modestr = "/";
5463 else if (mode & S_IXUSR)
5464 modestr = "*";
5465 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5466 got_tree_entry_get_name(te), modestr,
5467 link_target ? " -> ": "",
5468 link_target ? link_target : "") == -1) {
5469 free(id_str);
5470 free(link_target);
5471 return got_error_from_errno("asprintf");
5473 free(id_str);
5474 free(link_target);
5475 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5476 0, 0);
5477 if (err) {
5478 free(line);
5479 break;
5481 if (n == s->selected) {
5482 if (view->focussed)
5483 wstandout(view->window);
5484 s->selected_entry = te;
5486 tc = match_color(&s->colors, line);
5487 if (tc)
5488 wattr_on(view->window,
5489 COLOR_PAIR(tc->colorpair), NULL);
5490 waddwstr(view->window, wline);
5491 if (tc)
5492 wattr_off(view->window,
5493 COLOR_PAIR(tc->colorpair), NULL);
5494 if (width < view->ncols - 1)
5495 waddch(view->window, '\n');
5496 if (n == s->selected && view->focussed)
5497 wstandend(view->window);
5498 free(line);
5499 free(wline);
5500 wline = NULL;
5501 n++;
5502 s->ndisplayed++;
5503 s->last_displayed_entry = te;
5504 if (--limit <= 0)
5505 break;
5508 return err;
5511 static void
5512 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5514 struct got_tree_entry *te;
5515 int isroot = s->tree == s->root;
5516 int i = 0;
5518 if (s->first_displayed_entry == NULL)
5519 return;
5521 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5522 while (i++ < maxscroll) {
5523 if (te == NULL) {
5524 if (!isroot)
5525 s->first_displayed_entry = NULL;
5526 break;
5528 s->first_displayed_entry = te;
5529 te = got_tree_entry_get_prev(s->tree, te);
5533 static void
5534 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5536 struct got_tree_entry *next, *last;
5537 int n = 0;
5539 if (s->first_displayed_entry)
5540 next = got_tree_entry_get_next(s->tree,
5541 s->first_displayed_entry);
5542 else
5543 next = got_object_tree_get_first_entry(s->tree);
5545 last = s->last_displayed_entry;
5546 while (next && last && n++ < maxscroll) {
5547 last = got_tree_entry_get_next(s->tree, last);
5548 if (last) {
5549 s->first_displayed_entry = next;
5550 next = got_tree_entry_get_next(s->tree, next);
5555 static const struct got_error *
5556 tree_entry_path(char **path, struct tog_parent_trees *parents,
5557 struct got_tree_entry *te)
5559 const struct got_error *err = NULL;
5560 struct tog_parent_tree *pt;
5561 size_t len = 2; /* for leading slash and NUL */
5563 TAILQ_FOREACH(pt, parents, entry)
5564 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5565 + 1 /* slash */;
5566 if (te)
5567 len += strlen(got_tree_entry_get_name(te));
5569 *path = calloc(1, len);
5570 if (path == NULL)
5571 return got_error_from_errno("calloc");
5573 (*path)[0] = '/';
5574 pt = TAILQ_LAST(parents, tog_parent_trees);
5575 while (pt) {
5576 const char *name = got_tree_entry_get_name(pt->selected_entry);
5577 if (strlcat(*path, name, len) >= len) {
5578 err = got_error(GOT_ERR_NO_SPACE);
5579 goto done;
5581 if (strlcat(*path, "/", len) >= len) {
5582 err = got_error(GOT_ERR_NO_SPACE);
5583 goto done;
5585 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5587 if (te) {
5588 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5589 err = got_error(GOT_ERR_NO_SPACE);
5590 goto done;
5593 done:
5594 if (err) {
5595 free(*path);
5596 *path = NULL;
5598 return err;
5601 static const struct got_error *
5602 blame_tree_entry(struct tog_view **new_view, int begin_x,
5603 struct got_tree_entry *te, struct tog_parent_trees *parents,
5604 struct got_object_id *commit_id, struct got_repository *repo)
5606 const struct got_error *err = NULL;
5607 char *path;
5608 struct tog_view *blame_view;
5610 *new_view = NULL;
5612 err = tree_entry_path(&path, parents, te);
5613 if (err)
5614 return err;
5616 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5617 if (blame_view == NULL) {
5618 err = got_error_from_errno("view_open");
5619 goto done;
5622 err = open_blame_view(blame_view, path, commit_id, repo);
5623 if (err) {
5624 if (err->code == GOT_ERR_CANCELLED)
5625 err = NULL;
5626 view_close(blame_view);
5627 } else
5628 *new_view = blame_view;
5629 done:
5630 free(path);
5631 return err;
5634 static const struct got_error *
5635 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5636 struct tog_tree_view_state *s)
5638 struct tog_view *log_view;
5639 const struct got_error *err = NULL;
5640 char *path;
5642 *new_view = NULL;
5644 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5645 if (log_view == NULL)
5646 return got_error_from_errno("view_open");
5648 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5649 if (err)
5650 return err;
5652 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5653 path, 0);
5654 if (err)
5655 view_close(log_view);
5656 else
5657 *new_view = log_view;
5658 free(path);
5659 return err;
5662 static const struct got_error *
5663 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5664 const char *head_ref_name, struct got_repository *repo)
5666 const struct got_error *err = NULL;
5667 char *commit_id_str = NULL;
5668 struct tog_tree_view_state *s = &view->state.tree;
5669 struct got_commit_object *commit = NULL;
5671 TAILQ_INIT(&s->parents);
5672 STAILQ_INIT(&s->colors);
5674 s->commit_id = got_object_id_dup(commit_id);
5675 if (s->commit_id == NULL)
5676 return got_error_from_errno("got_object_id_dup");
5678 err = got_object_open_as_commit(&commit, repo, commit_id);
5679 if (err)
5680 goto done;
5683 * The root is opened here and will be closed when the view is closed.
5684 * Any visited subtrees and their path-wise parents are opened and
5685 * closed on demand.
5687 err = got_object_open_as_tree(&s->root, repo,
5688 got_object_commit_get_tree_id(commit));
5689 if (err)
5690 goto done;
5691 s->tree = s->root;
5693 err = got_object_id_str(&commit_id_str, commit_id);
5694 if (err != NULL)
5695 goto done;
5697 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5698 err = got_error_from_errno("asprintf");
5699 goto done;
5702 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5703 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5704 if (head_ref_name) {
5705 s->head_ref_name = strdup(head_ref_name);
5706 if (s->head_ref_name == NULL) {
5707 err = got_error_from_errno("strdup");
5708 goto done;
5711 s->repo = repo;
5713 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5714 err = add_color(&s->colors, "\\$$",
5715 TOG_COLOR_TREE_SUBMODULE,
5716 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5717 if (err)
5718 goto done;
5719 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5720 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5721 if (err)
5722 goto done;
5723 err = add_color(&s->colors, "/$",
5724 TOG_COLOR_TREE_DIRECTORY,
5725 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5726 if (err)
5727 goto done;
5729 err = add_color(&s->colors, "\\*$",
5730 TOG_COLOR_TREE_EXECUTABLE,
5731 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5732 if (err)
5733 goto done;
5735 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5736 get_color_value("TOG_COLOR_COMMIT"));
5737 if (err)
5738 goto done;
5741 view->show = show_tree_view;
5742 view->input = input_tree_view;
5743 view->close = close_tree_view;
5744 view->search_start = search_start_tree_view;
5745 view->search_next = search_next_tree_view;
5746 done:
5747 free(commit_id_str);
5748 if (commit)
5749 got_object_commit_close(commit);
5750 if (err)
5751 close_tree_view(view);
5752 return err;
5755 static const struct got_error *
5756 close_tree_view(struct tog_view *view)
5758 struct tog_tree_view_state *s = &view->state.tree;
5760 free_colors(&s->colors);
5761 free(s->tree_label);
5762 s->tree_label = NULL;
5763 free(s->commit_id);
5764 s->commit_id = NULL;
5765 free(s->head_ref_name);
5766 s->head_ref_name = NULL;
5767 while (!TAILQ_EMPTY(&s->parents)) {
5768 struct tog_parent_tree *parent;
5769 parent = TAILQ_FIRST(&s->parents);
5770 TAILQ_REMOVE(&s->parents, parent, entry);
5771 if (parent->tree != s->root)
5772 got_object_tree_close(parent->tree);
5773 free(parent);
5776 if (s->tree != NULL && s->tree != s->root)
5777 got_object_tree_close(s->tree);
5778 if (s->root)
5779 got_object_tree_close(s->root);
5780 return NULL;
5783 static const struct got_error *
5784 search_start_tree_view(struct tog_view *view)
5786 struct tog_tree_view_state *s = &view->state.tree;
5788 s->matched_entry = NULL;
5789 return NULL;
5792 static int
5793 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5795 regmatch_t regmatch;
5797 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5798 0) == 0;
5801 static const struct got_error *
5802 search_next_tree_view(struct tog_view *view)
5804 struct tog_tree_view_state *s = &view->state.tree;
5805 struct got_tree_entry *te = NULL;
5807 if (!view->searching) {
5808 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5809 return NULL;
5812 if (s->matched_entry) {
5813 if (view->searching == TOG_SEARCH_FORWARD) {
5814 if (s->selected_entry)
5815 te = got_tree_entry_get_next(s->tree,
5816 s->selected_entry);
5817 else
5818 te = got_object_tree_get_first_entry(s->tree);
5819 } else {
5820 if (s->selected_entry == NULL)
5821 te = got_object_tree_get_last_entry(s->tree);
5822 else
5823 te = got_tree_entry_get_prev(s->tree,
5824 s->selected_entry);
5826 } else {
5827 if (s->selected_entry)
5828 te = s->selected_entry;
5829 else if (view->searching == TOG_SEARCH_FORWARD)
5830 te = got_object_tree_get_first_entry(s->tree);
5831 else
5832 te = got_object_tree_get_last_entry(s->tree);
5835 while (1) {
5836 if (te == NULL) {
5837 if (s->matched_entry == NULL) {
5838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5839 return NULL;
5841 if (view->searching == TOG_SEARCH_FORWARD)
5842 te = got_object_tree_get_first_entry(s->tree);
5843 else
5844 te = got_object_tree_get_last_entry(s->tree);
5847 if (match_tree_entry(te, &view->regex)) {
5848 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5849 s->matched_entry = te;
5850 break;
5853 if (view->searching == TOG_SEARCH_FORWARD)
5854 te = got_tree_entry_get_next(s->tree, te);
5855 else
5856 te = got_tree_entry_get_prev(s->tree, te);
5859 if (s->matched_entry) {
5860 s->first_displayed_entry = s->matched_entry;
5861 s->selected = 0;
5864 return NULL;
5867 static const struct got_error *
5868 show_tree_view(struct tog_view *view)
5870 const struct got_error *err = NULL;
5871 struct tog_tree_view_state *s = &view->state.tree;
5872 char *parent_path;
5874 err = tree_entry_path(&parent_path, &s->parents, NULL);
5875 if (err)
5876 return err;
5878 err = draw_tree_entries(view, parent_path);
5879 free(parent_path);
5881 view_vborder(view);
5882 return err;
5885 static const struct got_error *
5886 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5888 const struct got_error *err = NULL;
5889 struct tog_tree_view_state *s = &view->state.tree;
5890 struct tog_view *log_view, *ref_view;
5891 struct got_tree_entry *te;
5892 int begin_x = 0, n, nscroll = view->nlines - 3;
5894 switch (ch) {
5895 case 'i':
5896 s->show_ids = !s->show_ids;
5897 break;
5898 case 'l':
5899 if (!s->selected_entry)
5900 break;
5901 if (view_is_parent_view(view))
5902 begin_x = view_split_begin_x(view->begin_x);
5903 err = log_selected_tree_entry(&log_view, begin_x, s);
5904 view->focussed = 0;
5905 log_view->focussed = 1;
5906 if (view_is_parent_view(view)) {
5907 err = view_close_child(view);
5908 if (err)
5909 return err;
5910 err = view_set_child(view, log_view);
5911 if (err)
5912 return err;
5913 view->focus_child = 1;
5914 } else
5915 *new_view = log_view;
5916 break;
5917 case 'r':
5918 if (view_is_parent_view(view))
5919 begin_x = view_split_begin_x(view->begin_x);
5920 ref_view = view_open(view->nlines, view->ncols,
5921 view->begin_y, begin_x, TOG_VIEW_REF);
5922 if (ref_view == NULL)
5923 return got_error_from_errno("view_open");
5924 err = open_ref_view(ref_view, s->repo);
5925 if (err) {
5926 view_close(ref_view);
5927 return err;
5929 view->focussed = 0;
5930 ref_view->focussed = 1;
5931 if (view_is_parent_view(view)) {
5932 err = view_close_child(view);
5933 if (err)
5934 return err;
5935 err = view_set_child(view, ref_view);
5936 if (err)
5937 return err;
5938 view->focus_child = 1;
5939 } else
5940 *new_view = ref_view;
5941 break;
5942 case 'g':
5943 case KEY_HOME:
5944 s->selected = 0;
5945 if (s->tree == s->root)
5946 s->first_displayed_entry =
5947 got_object_tree_get_first_entry(s->tree);
5948 else
5949 s->first_displayed_entry = NULL;
5950 break;
5951 case 'G':
5952 case KEY_END:
5953 s->selected = 0;
5954 te = got_object_tree_get_last_entry(s->tree);
5955 for (n = 0; n < view->nlines - 3; n++) {
5956 if (te == NULL) {
5957 if(s->tree != s->root) {
5958 s->first_displayed_entry = NULL;
5959 n++;
5961 break;
5963 s->first_displayed_entry = te;
5964 te = got_tree_entry_get_prev(s->tree, te);
5966 if (n > 0)
5967 s->selected = n - 1;
5968 break;
5969 case 'k':
5970 case KEY_UP:
5971 case CTRL('p'):
5972 if (s->selected > 0) {
5973 s->selected--;
5974 break;
5976 tree_scroll_up(s, 1);
5977 break;
5978 case CTRL('u'):
5979 case 'u':
5980 nscroll /= 2;
5981 /* FALL THROUGH */
5982 case KEY_PPAGE:
5983 case CTRL('b'):
5984 case 'b':
5985 if (s->tree == s->root) {
5986 if (got_object_tree_get_first_entry(s->tree) ==
5987 s->first_displayed_entry)
5988 s->selected -= MIN(s->selected, nscroll);
5989 } else {
5990 if (s->first_displayed_entry == NULL)
5991 s->selected -= MIN(s->selected, nscroll);
5993 tree_scroll_up(s, MAX(0, nscroll));
5994 break;
5995 case 'j':
5996 case KEY_DOWN:
5997 case CTRL('n'):
5998 if (s->selected < s->ndisplayed - 1) {
5999 s->selected++;
6000 break;
6002 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6003 == NULL)
6004 /* can't scroll any further */
6005 break;
6006 tree_scroll_down(s, 1);
6007 break;
6008 case CTRL('d'):
6009 case 'd':
6010 nscroll /= 2;
6011 /* FALL THROUGH */
6012 case KEY_NPAGE:
6013 case CTRL('f'):
6014 case 'f':
6015 case ' ':
6016 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6017 == NULL) {
6018 /* can't scroll any further; move cursor down */
6019 if (s->selected < s->ndisplayed - 1)
6020 s->selected += MIN(nscroll,
6021 s->ndisplayed - s->selected - 1);
6022 break;
6024 tree_scroll_down(s, nscroll);
6025 break;
6026 case KEY_ENTER:
6027 case '\r':
6028 case KEY_BACKSPACE:
6029 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6030 struct tog_parent_tree *parent;
6031 /* user selected '..' */
6032 if (s->tree == s->root)
6033 break;
6034 parent = TAILQ_FIRST(&s->parents);
6035 TAILQ_REMOVE(&s->parents, parent,
6036 entry);
6037 got_object_tree_close(s->tree);
6038 s->tree = parent->tree;
6039 s->first_displayed_entry =
6040 parent->first_displayed_entry;
6041 s->selected_entry =
6042 parent->selected_entry;
6043 s->selected = parent->selected;
6044 free(parent);
6045 } else if (S_ISDIR(got_tree_entry_get_mode(
6046 s->selected_entry))) {
6047 struct got_tree_object *subtree;
6048 err = got_object_open_as_tree(&subtree, s->repo,
6049 got_tree_entry_get_id(s->selected_entry));
6050 if (err)
6051 break;
6052 err = tree_view_visit_subtree(s, subtree);
6053 if (err) {
6054 got_object_tree_close(subtree);
6055 break;
6057 } else if (S_ISREG(got_tree_entry_get_mode(
6058 s->selected_entry))) {
6059 struct tog_view *blame_view;
6060 int begin_x = view_is_parent_view(view) ?
6061 view_split_begin_x(view->begin_x) : 0;
6063 err = blame_tree_entry(&blame_view, begin_x,
6064 s->selected_entry, &s->parents,
6065 s->commit_id, s->repo);
6066 if (err)
6067 break;
6068 view->focussed = 0;
6069 blame_view->focussed = 1;
6070 if (view_is_parent_view(view)) {
6071 err = view_close_child(view);
6072 if (err)
6073 return err;
6074 err = view_set_child(view, blame_view);
6075 if (err)
6076 return err;
6077 view->focus_child = 1;
6078 } else
6079 *new_view = blame_view;
6081 break;
6082 case KEY_RESIZE:
6083 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6084 s->selected = view->nlines - 4;
6085 break;
6086 default:
6087 break;
6090 return err;
6093 __dead static void
6094 usage_tree(void)
6096 endwin();
6097 fprintf(stderr,
6098 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6099 getprogname());
6100 exit(1);
6103 static const struct got_error *
6104 cmd_tree(int argc, char *argv[])
6106 const struct got_error *error;
6107 struct got_repository *repo = NULL;
6108 struct got_worktree *worktree = NULL;
6109 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6110 struct got_object_id *commit_id = NULL;
6111 struct got_commit_object *commit = NULL;
6112 const char *commit_id_arg = NULL;
6113 char *label = NULL;
6114 struct got_reference *ref = NULL;
6115 const char *head_ref_name = NULL;
6116 int ch;
6117 struct tog_view *view;
6118 int *pack_fds = NULL;
6120 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6121 switch (ch) {
6122 case 'c':
6123 commit_id_arg = optarg;
6124 break;
6125 case 'r':
6126 repo_path = realpath(optarg, NULL);
6127 if (repo_path == NULL)
6128 return got_error_from_errno2("realpath",
6129 optarg);
6130 break;
6131 default:
6132 usage_tree();
6133 /* NOTREACHED */
6137 argc -= optind;
6138 argv += optind;
6140 if (argc > 1)
6141 usage_tree();
6143 error = got_repo_pack_fds_open(&pack_fds);
6144 if (error != NULL)
6145 goto done;
6147 if (repo_path == NULL) {
6148 cwd = getcwd(NULL, 0);
6149 if (cwd == NULL)
6150 return got_error_from_errno("getcwd");
6151 error = got_worktree_open(&worktree, cwd);
6152 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6153 goto done;
6154 if (worktree)
6155 repo_path =
6156 strdup(got_worktree_get_repo_path(worktree));
6157 else
6158 repo_path = strdup(cwd);
6159 if (repo_path == NULL) {
6160 error = got_error_from_errno("strdup");
6161 goto done;
6165 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6166 if (error != NULL)
6167 goto done;
6169 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6170 repo, worktree);
6171 if (error)
6172 goto done;
6174 init_curses();
6176 error = apply_unveil(got_repo_get_path(repo), NULL);
6177 if (error)
6178 goto done;
6180 error = tog_load_refs(repo, 0);
6181 if (error)
6182 goto done;
6184 if (commit_id_arg == NULL) {
6185 error = got_repo_match_object_id(&commit_id, &label,
6186 worktree ? got_worktree_get_head_ref_name(worktree) :
6187 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6188 if (error)
6189 goto done;
6190 head_ref_name = label;
6191 } else {
6192 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6193 if (error == NULL)
6194 head_ref_name = got_ref_get_name(ref);
6195 else if (error->code != GOT_ERR_NOT_REF)
6196 goto done;
6197 error = got_repo_match_object_id(&commit_id, NULL,
6198 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6199 if (error)
6200 goto done;
6203 error = got_object_open_as_commit(&commit, repo, commit_id);
6204 if (error)
6205 goto done;
6207 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6208 if (view == NULL) {
6209 error = got_error_from_errno("view_open");
6210 goto done;
6212 error = open_tree_view(view, commit_id, head_ref_name, repo);
6213 if (error)
6214 goto done;
6215 if (!got_path_is_root_dir(in_repo_path)) {
6216 error = tree_view_walk_path(&view->state.tree, commit,
6217 in_repo_path);
6218 if (error)
6219 goto done;
6222 if (worktree) {
6223 /* Release work tree lock. */
6224 got_worktree_close(worktree);
6225 worktree = NULL;
6227 error = view_loop(view);
6228 done:
6229 free(repo_path);
6230 free(cwd);
6231 free(commit_id);
6232 free(label);
6233 if (ref)
6234 got_ref_close(ref);
6235 if (repo) {
6236 const struct got_error *close_err = got_repo_close(repo);
6237 if (error == NULL)
6238 error = close_err;
6240 if (pack_fds) {
6241 const struct got_error *pack_err =
6242 got_repo_pack_fds_close(pack_fds);
6243 if (error == NULL)
6244 error = pack_err;
6246 tog_free_refs();
6247 return error;
6250 static const struct got_error *
6251 ref_view_load_refs(struct tog_ref_view_state *s)
6253 struct got_reflist_entry *sre;
6254 struct tog_reflist_entry *re;
6256 s->nrefs = 0;
6257 TAILQ_FOREACH(sre, &tog_refs, entry) {
6258 if (strncmp(got_ref_get_name(sre->ref),
6259 "refs/got/", 9) == 0 &&
6260 strncmp(got_ref_get_name(sre->ref),
6261 "refs/got/backup/", 16) != 0)
6262 continue;
6264 re = malloc(sizeof(*re));
6265 if (re == NULL)
6266 return got_error_from_errno("malloc");
6268 re->ref = got_ref_dup(sre->ref);
6269 if (re->ref == NULL)
6270 return got_error_from_errno("got_ref_dup");
6271 re->idx = s->nrefs++;
6272 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6275 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6276 return NULL;
6279 void
6280 ref_view_free_refs(struct tog_ref_view_state *s)
6282 struct tog_reflist_entry *re;
6284 while (!TAILQ_EMPTY(&s->refs)) {
6285 re = TAILQ_FIRST(&s->refs);
6286 TAILQ_REMOVE(&s->refs, re, entry);
6287 got_ref_close(re->ref);
6288 free(re);
6292 static const struct got_error *
6293 open_ref_view(struct tog_view *view, struct got_repository *repo)
6295 const struct got_error *err = NULL;
6296 struct tog_ref_view_state *s = &view->state.ref;
6298 s->selected_entry = 0;
6299 s->repo = repo;
6301 TAILQ_INIT(&s->refs);
6302 STAILQ_INIT(&s->colors);
6304 err = ref_view_load_refs(s);
6305 if (err)
6306 return err;
6308 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6309 err = add_color(&s->colors, "^refs/heads/",
6310 TOG_COLOR_REFS_HEADS,
6311 get_color_value("TOG_COLOR_REFS_HEADS"));
6312 if (err)
6313 goto done;
6315 err = add_color(&s->colors, "^refs/tags/",
6316 TOG_COLOR_REFS_TAGS,
6317 get_color_value("TOG_COLOR_REFS_TAGS"));
6318 if (err)
6319 goto done;
6321 err = add_color(&s->colors, "^refs/remotes/",
6322 TOG_COLOR_REFS_REMOTES,
6323 get_color_value("TOG_COLOR_REFS_REMOTES"));
6324 if (err)
6325 goto done;
6327 err = add_color(&s->colors, "^refs/got/backup/",
6328 TOG_COLOR_REFS_BACKUP,
6329 get_color_value("TOG_COLOR_REFS_BACKUP"));
6330 if (err)
6331 goto done;
6334 view->show = show_ref_view;
6335 view->input = input_ref_view;
6336 view->close = close_ref_view;
6337 view->search_start = search_start_ref_view;
6338 view->search_next = search_next_ref_view;
6339 done:
6340 if (err)
6341 free_colors(&s->colors);
6342 return err;
6345 static const struct got_error *
6346 close_ref_view(struct tog_view *view)
6348 struct tog_ref_view_state *s = &view->state.ref;
6350 ref_view_free_refs(s);
6351 free_colors(&s->colors);
6353 return NULL;
6356 static const struct got_error *
6357 resolve_reflist_entry(struct got_object_id **commit_id,
6358 struct tog_reflist_entry *re, struct got_repository *repo)
6360 const struct got_error *err = NULL;
6361 struct got_object_id *obj_id;
6362 struct got_tag_object *tag = NULL;
6363 int obj_type;
6365 *commit_id = NULL;
6367 err = got_ref_resolve(&obj_id, repo, re->ref);
6368 if (err)
6369 return err;
6371 err = got_object_get_type(&obj_type, repo, obj_id);
6372 if (err)
6373 goto done;
6375 switch (obj_type) {
6376 case GOT_OBJ_TYPE_COMMIT:
6377 *commit_id = obj_id;
6378 break;
6379 case GOT_OBJ_TYPE_TAG:
6380 err = got_object_open_as_tag(&tag, repo, obj_id);
6381 if (err)
6382 goto done;
6383 free(obj_id);
6384 err = got_object_get_type(&obj_type, repo,
6385 got_object_tag_get_object_id(tag));
6386 if (err)
6387 goto done;
6388 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6389 err = got_error(GOT_ERR_OBJ_TYPE);
6390 goto done;
6392 *commit_id = got_object_id_dup(
6393 got_object_tag_get_object_id(tag));
6394 if (*commit_id == NULL) {
6395 err = got_error_from_errno("got_object_id_dup");
6396 goto done;
6398 break;
6399 default:
6400 err = got_error(GOT_ERR_OBJ_TYPE);
6401 break;
6404 done:
6405 if (tag)
6406 got_object_tag_close(tag);
6407 if (err) {
6408 free(*commit_id);
6409 *commit_id = NULL;
6411 return err;
6414 static const struct got_error *
6415 log_ref_entry(struct tog_view **new_view, int begin_x,
6416 struct tog_reflist_entry *re, struct got_repository *repo)
6418 struct tog_view *log_view;
6419 const struct got_error *err = NULL;
6420 struct got_object_id *commit_id = NULL;
6422 *new_view = NULL;
6424 err = resolve_reflist_entry(&commit_id, re, repo);
6425 if (err) {
6426 if (err->code != GOT_ERR_OBJ_TYPE)
6427 return err;
6428 else
6429 return NULL;
6432 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6433 if (log_view == NULL) {
6434 err = got_error_from_errno("view_open");
6435 goto done;
6438 err = open_log_view(log_view, commit_id, repo,
6439 got_ref_get_name(re->ref), "", 0);
6440 done:
6441 if (err)
6442 view_close(log_view);
6443 else
6444 *new_view = log_view;
6445 free(commit_id);
6446 return err;
6449 static void
6450 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6452 struct tog_reflist_entry *re;
6453 int i = 0;
6455 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6456 return;
6458 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6459 while (i++ < maxscroll) {
6460 if (re == NULL)
6461 break;
6462 s->first_displayed_entry = re;
6463 re = TAILQ_PREV(re, tog_reflist_head, entry);
6467 static void
6468 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6470 struct tog_reflist_entry *next, *last;
6471 int n = 0;
6473 if (s->first_displayed_entry)
6474 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6475 else
6476 next = TAILQ_FIRST(&s->refs);
6478 last = s->last_displayed_entry;
6479 while (next && last && n++ < maxscroll) {
6480 last = TAILQ_NEXT(last, entry);
6481 if (last) {
6482 s->first_displayed_entry = next;
6483 next = TAILQ_NEXT(next, entry);
6488 static const struct got_error *
6489 search_start_ref_view(struct tog_view *view)
6491 struct tog_ref_view_state *s = &view->state.ref;
6493 s->matched_entry = NULL;
6494 return NULL;
6497 static int
6498 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6500 regmatch_t regmatch;
6502 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6503 0) == 0;
6506 static const struct got_error *
6507 search_next_ref_view(struct tog_view *view)
6509 struct tog_ref_view_state *s = &view->state.ref;
6510 struct tog_reflist_entry *re = NULL;
6512 if (!view->searching) {
6513 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6514 return NULL;
6517 if (s->matched_entry) {
6518 if (view->searching == TOG_SEARCH_FORWARD) {
6519 if (s->selected_entry)
6520 re = TAILQ_NEXT(s->selected_entry, entry);
6521 else
6522 re = TAILQ_PREV(s->selected_entry,
6523 tog_reflist_head, entry);
6524 } else {
6525 if (s->selected_entry == NULL)
6526 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6527 else
6528 re = TAILQ_PREV(s->selected_entry,
6529 tog_reflist_head, entry);
6531 } else {
6532 if (s->selected_entry)
6533 re = s->selected_entry;
6534 else if (view->searching == TOG_SEARCH_FORWARD)
6535 re = TAILQ_FIRST(&s->refs);
6536 else
6537 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6540 while (1) {
6541 if (re == NULL) {
6542 if (s->matched_entry == NULL) {
6543 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6544 return NULL;
6546 if (view->searching == TOG_SEARCH_FORWARD)
6547 re = TAILQ_FIRST(&s->refs);
6548 else
6549 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6552 if (match_reflist_entry(re, &view->regex)) {
6553 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6554 s->matched_entry = re;
6555 break;
6558 if (view->searching == TOG_SEARCH_FORWARD)
6559 re = TAILQ_NEXT(re, entry);
6560 else
6561 re = TAILQ_PREV(re, tog_reflist_head, entry);
6564 if (s->matched_entry) {
6565 s->first_displayed_entry = s->matched_entry;
6566 s->selected = 0;
6569 return NULL;
6572 static const struct got_error *
6573 show_ref_view(struct tog_view *view)
6575 const struct got_error *err = NULL;
6576 struct tog_ref_view_state *s = &view->state.ref;
6577 struct tog_reflist_entry *re;
6578 char *line = NULL;
6579 wchar_t *wline;
6580 struct tog_color *tc;
6581 int width, n;
6582 int limit = view->nlines;
6584 werase(view->window);
6586 s->ndisplayed = 0;
6588 if (limit == 0)
6589 return NULL;
6591 re = s->first_displayed_entry;
6593 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6594 s->nrefs) == -1)
6595 return got_error_from_errno("asprintf");
6597 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6598 if (err) {
6599 free(line);
6600 return err;
6602 if (view_needs_focus_indication(view))
6603 wstandout(view->window);
6604 waddwstr(view->window, wline);
6605 if (view_needs_focus_indication(view))
6606 wstandend(view->window);
6607 free(wline);
6608 wline = NULL;
6609 free(line);
6610 line = NULL;
6611 if (width < view->ncols - 1)
6612 waddch(view->window, '\n');
6613 if (--limit <= 0)
6614 return NULL;
6616 n = 0;
6617 while (re && limit > 0) {
6618 char *line = NULL;
6619 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6621 if (s->show_date) {
6622 struct got_commit_object *ci;
6623 struct got_tag_object *tag;
6624 struct got_object_id *id;
6625 struct tm tm;
6626 time_t t;
6628 err = got_ref_resolve(&id, s->repo, re->ref);
6629 if (err)
6630 return err;
6631 err = got_object_open_as_tag(&tag, s->repo, id);
6632 if (err) {
6633 if (err->code != GOT_ERR_OBJ_TYPE) {
6634 free(id);
6635 return err;
6637 err = got_object_open_as_commit(&ci, s->repo,
6638 id);
6639 if (err) {
6640 free(id);
6641 return err;
6643 t = got_object_commit_get_committer_time(ci);
6644 got_object_commit_close(ci);
6645 } else {
6646 t = got_object_tag_get_tagger_time(tag);
6647 got_object_tag_close(tag);
6649 free(id);
6650 if (gmtime_r(&t, &tm) == NULL)
6651 return got_error_from_errno("gmtime_r");
6652 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6653 return got_error(GOT_ERR_NO_SPACE);
6655 if (got_ref_is_symbolic(re->ref)) {
6656 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6657 ymd : "", got_ref_get_name(re->ref),
6658 got_ref_get_symref_target(re->ref)) == -1)
6659 return got_error_from_errno("asprintf");
6660 } else if (s->show_ids) {
6661 struct got_object_id *id;
6662 char *id_str;
6663 err = got_ref_resolve(&id, s->repo, re->ref);
6664 if (err)
6665 return err;
6666 err = got_object_id_str(&id_str, id);
6667 if (err) {
6668 free(id);
6669 return err;
6671 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6672 got_ref_get_name(re->ref), id_str) == -1) {
6673 err = got_error_from_errno("asprintf");
6674 free(id);
6675 free(id_str);
6676 return err;
6678 free(id);
6679 free(id_str);
6680 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6681 got_ref_get_name(re->ref)) == -1)
6682 return got_error_from_errno("asprintf");
6684 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6685 0, 0);
6686 if (err) {
6687 free(line);
6688 return err;
6690 if (n == s->selected) {
6691 if (view->focussed)
6692 wstandout(view->window);
6693 s->selected_entry = re;
6695 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6696 if (tc)
6697 wattr_on(view->window,
6698 COLOR_PAIR(tc->colorpair), NULL);
6699 waddwstr(view->window, wline);
6700 if (tc)
6701 wattr_off(view->window,
6702 COLOR_PAIR(tc->colorpair), NULL);
6703 if (width < view->ncols - 1)
6704 waddch(view->window, '\n');
6705 if (n == s->selected && view->focussed)
6706 wstandend(view->window);
6707 free(line);
6708 free(wline);
6709 wline = NULL;
6710 n++;
6711 s->ndisplayed++;
6712 s->last_displayed_entry = re;
6714 limit--;
6715 re = TAILQ_NEXT(re, entry);
6718 view_vborder(view);
6719 return err;
6722 static const struct got_error *
6723 browse_ref_tree(struct tog_view **new_view, int begin_x,
6724 struct tog_reflist_entry *re, struct got_repository *repo)
6726 const struct got_error *err = NULL;
6727 struct got_object_id *commit_id = NULL;
6728 struct tog_view *tree_view;
6730 *new_view = NULL;
6732 err = resolve_reflist_entry(&commit_id, re, repo);
6733 if (err) {
6734 if (err->code != GOT_ERR_OBJ_TYPE)
6735 return err;
6736 else
6737 return NULL;
6741 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6742 if (tree_view == NULL) {
6743 err = got_error_from_errno("view_open");
6744 goto done;
6747 err = open_tree_view(tree_view, commit_id,
6748 got_ref_get_name(re->ref), repo);
6749 if (err)
6750 goto done;
6752 *new_view = tree_view;
6753 done:
6754 free(commit_id);
6755 return err;
6757 static const struct got_error *
6758 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6760 const struct got_error *err = NULL;
6761 struct tog_ref_view_state *s = &view->state.ref;
6762 struct tog_view *log_view, *tree_view;
6763 struct tog_reflist_entry *re;
6764 int begin_x = 0, n, nscroll = view->nlines - 1;
6766 switch (ch) {
6767 case 'i':
6768 s->show_ids = !s->show_ids;
6769 break;
6770 case 'm':
6771 s->show_date = !s->show_date;
6772 break;
6773 case 'o':
6774 s->sort_by_date = !s->sort_by_date;
6775 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6776 got_ref_cmp_by_commit_timestamp_descending :
6777 tog_ref_cmp_by_name, s->repo);
6778 if (err)
6779 break;
6780 got_reflist_object_id_map_free(tog_refs_idmap);
6781 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6782 &tog_refs, s->repo);
6783 if (err)
6784 break;
6785 ref_view_free_refs(s);
6786 err = ref_view_load_refs(s);
6787 break;
6788 case KEY_ENTER:
6789 case '\r':
6790 if (!s->selected_entry)
6791 break;
6792 if (view_is_parent_view(view))
6793 begin_x = view_split_begin_x(view->begin_x);
6794 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6795 s->repo);
6796 view->focussed = 0;
6797 log_view->focussed = 1;
6798 if (view_is_parent_view(view)) {
6799 err = view_close_child(view);
6800 if (err)
6801 return err;
6802 err = view_set_child(view, log_view);
6803 if (err)
6804 return err;
6805 view->focus_child = 1;
6806 } else
6807 *new_view = log_view;
6808 break;
6809 case 't':
6810 if (!s->selected_entry)
6811 break;
6812 if (view_is_parent_view(view))
6813 begin_x = view_split_begin_x(view->begin_x);
6814 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6815 s->repo);
6816 if (err || tree_view == NULL)
6817 break;
6818 view->focussed = 0;
6819 tree_view->focussed = 1;
6820 if (view_is_parent_view(view)) {
6821 err = view_close_child(view);
6822 if (err)
6823 return err;
6824 err = view_set_child(view, tree_view);
6825 if (err)
6826 return err;
6827 view->focus_child = 1;
6828 } else
6829 *new_view = tree_view;
6830 break;
6831 case 'g':
6832 case KEY_HOME:
6833 s->selected = 0;
6834 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6835 break;
6836 case 'G':
6837 case KEY_END:
6838 s->selected = 0;
6839 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6840 for (n = 0; n < view->nlines - 1; n++) {
6841 if (re == NULL)
6842 break;
6843 s->first_displayed_entry = re;
6844 re = TAILQ_PREV(re, tog_reflist_head, entry);
6846 if (n > 0)
6847 s->selected = n - 1;
6848 break;
6849 case 'k':
6850 case KEY_UP:
6851 case CTRL('p'):
6852 if (s->selected > 0) {
6853 s->selected--;
6854 break;
6856 ref_scroll_up(s, 1);
6857 break;
6858 case CTRL('u'):
6859 case 'u':
6860 nscroll /= 2;
6861 /* FALL THROUGH */
6862 case KEY_PPAGE:
6863 case CTRL('b'):
6864 case 'b':
6865 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6866 s->selected -= MIN(nscroll, s->selected);
6867 ref_scroll_up(s, MAX(0, nscroll));
6868 break;
6869 case 'j':
6870 case KEY_DOWN:
6871 case CTRL('n'):
6872 if (s->selected < s->ndisplayed - 1) {
6873 s->selected++;
6874 break;
6876 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6877 /* can't scroll any further */
6878 break;
6879 ref_scroll_down(s, 1);
6880 break;
6881 case CTRL('d'):
6882 case 'd':
6883 nscroll /= 2;
6884 /* FALL THROUGH */
6885 case KEY_NPAGE:
6886 case CTRL('f'):
6887 case 'f':
6888 case ' ':
6889 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6890 /* can't scroll any further; move cursor down */
6891 if (s->selected < s->ndisplayed - 1)
6892 s->selected += MIN(nscroll,
6893 s->ndisplayed - s->selected - 1);
6894 break;
6896 ref_scroll_down(s, nscroll);
6897 break;
6898 case CTRL('l'):
6899 tog_free_refs();
6900 err = tog_load_refs(s->repo, s->sort_by_date);
6901 if (err)
6902 break;
6903 ref_view_free_refs(s);
6904 err = ref_view_load_refs(s);
6905 break;
6906 case KEY_RESIZE:
6907 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6908 s->selected = view->nlines - 2;
6909 break;
6910 default:
6911 break;
6914 return err;
6917 __dead static void
6918 usage_ref(void)
6920 endwin();
6921 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6922 getprogname());
6923 exit(1);
6926 static const struct got_error *
6927 cmd_ref(int argc, char *argv[])
6929 const struct got_error *error;
6930 struct got_repository *repo = NULL;
6931 struct got_worktree *worktree = NULL;
6932 char *cwd = NULL, *repo_path = NULL;
6933 int ch;
6934 struct tog_view *view;
6935 int *pack_fds = NULL;
6937 while ((ch = getopt(argc, argv, "r:")) != -1) {
6938 switch (ch) {
6939 case 'r':
6940 repo_path = realpath(optarg, NULL);
6941 if (repo_path == NULL)
6942 return got_error_from_errno2("realpath",
6943 optarg);
6944 break;
6945 default:
6946 usage_ref();
6947 /* NOTREACHED */
6951 argc -= optind;
6952 argv += optind;
6954 if (argc > 1)
6955 usage_ref();
6957 error = got_repo_pack_fds_open(&pack_fds);
6958 if (error != NULL)
6959 goto done;
6961 if (repo_path == NULL) {
6962 cwd = getcwd(NULL, 0);
6963 if (cwd == NULL)
6964 return got_error_from_errno("getcwd");
6965 error = got_worktree_open(&worktree, cwd);
6966 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6967 goto done;
6968 if (worktree)
6969 repo_path =
6970 strdup(got_worktree_get_repo_path(worktree));
6971 else
6972 repo_path = strdup(cwd);
6973 if (repo_path == NULL) {
6974 error = got_error_from_errno("strdup");
6975 goto done;
6979 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6980 if (error != NULL)
6981 goto done;
6983 init_curses();
6985 error = apply_unveil(got_repo_get_path(repo), NULL);
6986 if (error)
6987 goto done;
6989 error = tog_load_refs(repo, 0);
6990 if (error)
6991 goto done;
6993 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6994 if (view == NULL) {
6995 error = got_error_from_errno("view_open");
6996 goto done;
6999 error = open_ref_view(view, repo);
7000 if (error)
7001 goto done;
7003 if (worktree) {
7004 /* Release work tree lock. */
7005 got_worktree_close(worktree);
7006 worktree = NULL;
7008 error = view_loop(view);
7009 done:
7010 free(repo_path);
7011 free(cwd);
7012 if (repo) {
7013 const struct got_error *close_err = got_repo_close(repo);
7014 if (close_err)
7015 error = close_err;
7017 if (pack_fds) {
7018 const struct got_error *pack_err =
7019 got_repo_pack_fds_close(pack_fds);
7020 if (error == NULL)
7021 error = pack_err;
7023 tog_free_refs();
7024 return error;
7027 static void
7028 list_commands(FILE *fp)
7030 size_t i;
7032 fprintf(fp, "commands:");
7033 for (i = 0; i < nitems(tog_commands); i++) {
7034 const struct tog_cmd *cmd = &tog_commands[i];
7035 fprintf(fp, " %s", cmd->name);
7037 fputc('\n', fp);
7040 __dead static void
7041 usage(int hflag, int status)
7043 FILE *fp = (status == 0) ? stdout : stderr;
7045 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7046 getprogname());
7047 if (hflag) {
7048 fprintf(fp, "lazy usage: %s path\n", getprogname());
7049 list_commands(fp);
7051 exit(status);
7054 static char **
7055 make_argv(int argc, ...)
7057 va_list ap;
7058 char **argv;
7059 int i;
7061 va_start(ap, argc);
7063 argv = calloc(argc, sizeof(char *));
7064 if (argv == NULL)
7065 err(1, "calloc");
7066 for (i = 0; i < argc; i++) {
7067 argv[i] = strdup(va_arg(ap, char *));
7068 if (argv[i] == NULL)
7069 err(1, "strdup");
7072 va_end(ap);
7073 return argv;
7077 * Try to convert 'tog path' into a 'tog log path' command.
7078 * The user could simply have mistyped the command rather than knowingly
7079 * provided a path. So check whether argv[0] can in fact be resolved
7080 * to a path in the HEAD commit and print a special error if not.
7081 * This hack is for mpi@ <3
7083 static const struct got_error *
7084 tog_log_with_path(int argc, char *argv[])
7086 const struct got_error *error = NULL, *close_err;
7087 const struct tog_cmd *cmd = NULL;
7088 struct got_repository *repo = NULL;
7089 struct got_worktree *worktree = NULL;
7090 struct got_object_id *commit_id = NULL, *id = NULL;
7091 struct got_commit_object *commit = NULL;
7092 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7093 char *commit_id_str = NULL, **cmd_argv = NULL;
7094 int *pack_fds = NULL;
7096 cwd = getcwd(NULL, 0);
7097 if (cwd == NULL)
7098 return got_error_from_errno("getcwd");
7100 error = got_repo_pack_fds_open(&pack_fds);
7101 if (error != NULL)
7102 goto done;
7104 error = got_worktree_open(&worktree, cwd);
7105 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7106 goto done;
7108 if (worktree)
7109 repo_path = strdup(got_worktree_get_repo_path(worktree));
7110 else
7111 repo_path = strdup(cwd);
7112 if (repo_path == NULL) {
7113 error = got_error_from_errno("strdup");
7114 goto done;
7117 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7118 if (error != NULL)
7119 goto done;
7121 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7122 repo, worktree);
7123 if (error)
7124 goto done;
7126 error = tog_load_refs(repo, 0);
7127 if (error)
7128 goto done;
7129 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7130 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7131 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7132 if (error)
7133 goto done;
7135 if (worktree) {
7136 got_worktree_close(worktree);
7137 worktree = NULL;
7140 error = got_object_open_as_commit(&commit, repo, commit_id);
7141 if (error)
7142 goto done;
7144 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7145 if (error) {
7146 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7147 goto done;
7148 fprintf(stderr, "%s: '%s' is no known command or path\n",
7149 getprogname(), argv[0]);
7150 usage(1, 1);
7151 /* not reached */
7154 close_err = got_repo_close(repo);
7155 if (error == NULL)
7156 error = close_err;
7157 repo = NULL;
7159 error = got_object_id_str(&commit_id_str, commit_id);
7160 if (error)
7161 goto done;
7163 cmd = &tog_commands[0]; /* log */
7164 argc = 4;
7165 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7166 error = cmd->cmd_main(argc, cmd_argv);
7167 done:
7168 if (repo) {
7169 close_err = got_repo_close(repo);
7170 if (error == NULL)
7171 error = close_err;
7173 if (commit)
7174 got_object_commit_close(commit);
7175 if (worktree)
7176 got_worktree_close(worktree);
7177 if (pack_fds) {
7178 const struct got_error *pack_err =
7179 got_repo_pack_fds_close(pack_fds);
7180 if (error == NULL)
7181 error = pack_err;
7183 free(id);
7184 free(commit_id_str);
7185 free(commit_id);
7186 free(cwd);
7187 free(repo_path);
7188 free(in_repo_path);
7189 if (cmd_argv) {
7190 int i;
7191 for (i = 0; i < argc; i++)
7192 free(cmd_argv[i]);
7193 free(cmd_argv);
7195 tog_free_refs();
7196 return error;
7199 int
7200 main(int argc, char *argv[])
7202 const struct got_error *error = NULL;
7203 const struct tog_cmd *cmd = NULL;
7204 int ch, hflag = 0, Vflag = 0;
7205 char **cmd_argv = NULL;
7206 static const struct option longopts[] = {
7207 { "version", no_argument, NULL, 'V' },
7208 { NULL, 0, NULL, 0}
7211 setlocale(LC_CTYPE, "");
7213 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7214 switch (ch) {
7215 case 'h':
7216 hflag = 1;
7217 break;
7218 case 'V':
7219 Vflag = 1;
7220 break;
7221 default:
7222 usage(hflag, 1);
7223 /* NOTREACHED */
7227 argc -= optind;
7228 argv += optind;
7229 optind = 1;
7230 optreset = 1;
7232 if (Vflag) {
7233 got_version_print_str();
7234 return 0;
7237 #ifndef PROFILE
7238 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7239 NULL) == -1)
7240 err(1, "pledge");
7241 #endif
7243 if (argc == 0) {
7244 if (hflag)
7245 usage(hflag, 0);
7246 /* Build an argument vector which runs a default command. */
7247 cmd = &tog_commands[0];
7248 argc = 1;
7249 cmd_argv = make_argv(argc, cmd->name);
7250 } else {
7251 size_t i;
7253 /* Did the user specify a command? */
7254 for (i = 0; i < nitems(tog_commands); i++) {
7255 if (strncmp(tog_commands[i].name, argv[0],
7256 strlen(argv[0])) == 0) {
7257 cmd = &tog_commands[i];
7258 break;
7263 if (cmd == NULL) {
7264 if (argc != 1)
7265 usage(0, 1);
7266 /* No command specified; try log with a path */
7267 error = tog_log_with_path(argc, argv);
7268 } else {
7269 if (hflag)
7270 cmd->cmd_usage();
7271 else
7272 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7275 endwin();
7276 putchar('\n');
7277 if (cmd_argv) {
7278 int i;
7279 for (i = 0; i < argc; i++)
7280 free(cmd_argv[i]);
7281 free(cmd_argv);
7284 if (error && error->code != GOT_ERR_CANCELLED)
7285 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7286 return 0;