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()
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 const struct got_error *
759 view_resize(struct tog_view *view)
761 int nlines, ncols;
763 if (view->lines > LINES)
764 nlines = view->nlines - (view->lines - LINES);
765 else
766 nlines = view->nlines + (LINES - view->lines);
768 if (view->cols > COLS)
769 ncols = view->ncols - (view->cols - COLS);
770 else
771 ncols = view->ncols + (COLS - view->cols);
773 if (wresize(view->window, nlines, ncols) == ERR)
774 return got_error_from_errno("wresize");
775 if (replace_panel(view->panel, view->window) == ERR)
776 return got_error_from_errno("replace_panel");
777 wclear(view->window);
779 view->nlines = nlines;
780 view->ncols = ncols;
781 view->lines = LINES;
782 view->cols = COLS;
784 if (view->child) {
785 view->child->begin_x = view_split_begin_x(view->begin_x);
786 if (view->child->begin_x == 0) {
787 view_fullscreen(view->child);
788 if (view->child->focussed)
789 show_panel(view->child->panel);
790 else
791 show_panel(view->panel);
792 } else {
793 view_splitscreen(view->child);
794 show_panel(view->child->panel);
798 return NULL;
801 static const struct got_error *
802 view_close_child(struct tog_view *view)
804 const struct got_error *err = NULL;
806 if (view->child == NULL)
807 return NULL;
809 err = view_close(view->child);
810 view->child = NULL;
811 return err;
814 static void
815 view_set_child(struct tog_view *view, struct tog_view *child)
817 view->child = child;
818 child->parent = view;
821 static int
822 view_is_splitscreen(struct tog_view *view)
824 return view->begin_x > 0;
827 static void
828 tog_resizeterm(void)
830 int cols, lines;
831 struct winsize size;
833 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
834 cols = 80; /* Default */
835 lines = 24;
836 } else {
837 cols = size.ws_col;
838 lines = size.ws_row;
840 resize_term(lines, cols);
843 static const struct got_error *
844 view_search_start(struct tog_view *view)
846 const struct got_error *err = NULL;
847 char pattern[1024];
848 int ret;
850 if (view->search_started) {
851 regfree(&view->regex);
852 view->searching = 0;
853 memset(&view->regmatch, 0, sizeof(view->regmatch));
855 view->search_started = 0;
857 if (view->nlines < 1)
858 return NULL;
860 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
861 wclrtoeol(view->window);
863 nocbreak();
864 echo();
865 ret = wgetnstr(view->window, pattern, sizeof(pattern));
866 cbreak();
867 noecho();
868 if (ret == ERR)
869 return NULL;
871 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
872 err = view->search_start(view);
873 if (err) {
874 regfree(&view->regex);
875 return err;
877 view->search_started = 1;
878 view->searching = TOG_SEARCH_FORWARD;
879 view->search_next_done = 0;
880 view->search_next(view);
883 return NULL;
886 static const struct got_error *
887 view_input(struct tog_view **new, int *done, struct tog_view *view,
888 struct tog_view_list_head *views)
890 const struct got_error *err = NULL;
891 struct tog_view *v;
892 int ch, errcode;
894 *new = NULL;
896 /* Clear "no matches" indicator. */
897 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
898 view->search_next_done == TOG_SEARCH_HAVE_NONE)
899 view->search_next_done = TOG_SEARCH_HAVE_MORE;
901 if (view->searching && !view->search_next_done) {
902 errcode = pthread_mutex_unlock(&tog_mutex);
903 if (errcode)
904 return got_error_set_errno(errcode,
905 "pthread_mutex_unlock");
906 sched_yield();
907 errcode = pthread_mutex_lock(&tog_mutex);
908 if (errcode)
909 return got_error_set_errno(errcode,
910 "pthread_mutex_lock");
911 view->search_next(view);
912 return NULL;
915 nodelay(stdscr, FALSE);
916 /* Allow threads to make progress while we are waiting for input. */
917 errcode = pthread_mutex_unlock(&tog_mutex);
918 if (errcode)
919 return got_error_set_errno(errcode, "pthread_mutex_unlock");
920 ch = wgetch(view->window);
921 errcode = pthread_mutex_lock(&tog_mutex);
922 if (errcode)
923 return got_error_set_errno(errcode, "pthread_mutex_lock");
924 nodelay(stdscr, TRUE);
926 if (tog_sigwinch_received || tog_sigcont_received) {
927 tog_resizeterm();
928 tog_sigwinch_received = 0;
929 tog_sigcont_received = 0;
930 TAILQ_FOREACH(v, views, entry) {
931 err = view_resize(v);
932 if (err)
933 return err;
934 err = v->input(new, v, KEY_RESIZE);
935 if (err)
936 return err;
937 if (v->child) {
938 err = view_resize(v->child);
939 if (err)
940 return err;
941 err = v->child->input(new, v->child,
942 KEY_RESIZE);
943 if (err)
944 return err;
949 switch (ch) {
950 case '\t':
951 if (view->child) {
952 view->focussed = 0;
953 view->child->focussed = 1;
954 view->focus_child = 1;
955 } else if (view->parent) {
956 view->focussed = 0;
957 view->parent->focussed = 1;
958 view->parent->focus_child = 0;
960 break;
961 case 'q':
962 err = view->input(new, view, ch);
963 view->dying = 1;
964 break;
965 case 'Q':
966 *done = 1;
967 break;
968 case 'f':
969 if (view_is_parent_view(view)) {
970 if (view->child == NULL)
971 break;
972 if (view_is_splitscreen(view->child)) {
973 view->focussed = 0;
974 view->child->focussed = 1;
975 err = view_fullscreen(view->child);
976 } else
977 err = view_splitscreen(view->child);
978 if (err)
979 break;
980 err = view->child->input(new, view->child,
981 KEY_RESIZE);
982 } else {
983 if (view_is_splitscreen(view)) {
984 view->parent->focussed = 0;
985 view->focussed = 1;
986 err = view_fullscreen(view);
987 } else {
988 err = view_splitscreen(view);
990 if (err)
991 break;
992 err = view->input(new, view, KEY_RESIZE);
994 break;
995 case KEY_RESIZE:
996 break;
997 case '/':
998 if (view->search_start)
999 view_search_start(view);
1000 else
1001 err = view->input(new, view, ch);
1002 break;
1003 case 'N':
1004 case 'n':
1005 if (view->search_started && view->search_next) {
1006 view->searching = (ch == 'n' ?
1007 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1008 view->search_next_done = 0;
1009 view->search_next(view);
1010 } else
1011 err = view->input(new, view, ch);
1012 break;
1013 default:
1014 err = view->input(new, view, ch);
1015 break;
1018 return err;
1021 void
1022 view_vborder(struct tog_view *view)
1024 PANEL *panel;
1025 const struct tog_view *view_above;
1027 if (view->parent)
1028 return view_vborder(view->parent);
1030 panel = panel_above(view->panel);
1031 if (panel == NULL)
1032 return;
1034 view_above = panel_userptr(panel);
1035 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1036 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1039 int
1040 view_needs_focus_indication(struct tog_view *view)
1042 if (view_is_parent_view(view)) {
1043 if (view->child == NULL || view->child->focussed)
1044 return 0;
1045 if (!view_is_splitscreen(view->child))
1046 return 0;
1047 } else if (!view_is_splitscreen(view))
1048 return 0;
1050 return view->focussed;
1053 static const struct got_error *
1054 view_loop(struct tog_view *view)
1056 const struct got_error *err = NULL;
1057 struct tog_view_list_head views;
1058 struct tog_view *new_view;
1059 int fast_refresh = 10;
1060 int done = 0, errcode;
1062 errcode = pthread_mutex_lock(&tog_mutex);
1063 if (errcode)
1064 return got_error_set_errno(errcode, "pthread_mutex_lock");
1066 TAILQ_INIT(&views);
1067 TAILQ_INSERT_HEAD(&views, view, entry);
1069 view->focussed = 1;
1070 err = view->show(view);
1071 if (err)
1072 return err;
1073 update_panels();
1074 doupdate();
1075 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1076 /* Refresh fast during initialization, then become slower. */
1077 if (fast_refresh && fast_refresh-- == 0)
1078 halfdelay(10); /* switch to once per second */
1080 err = view_input(&new_view, &done, view, &views);
1081 if (err)
1082 break;
1083 if (view->dying) {
1084 struct tog_view *v, *prev = NULL;
1086 if (view_is_parent_view(view))
1087 prev = TAILQ_PREV(view, tog_view_list_head,
1088 entry);
1089 else if (view->parent)
1090 prev = view->parent;
1092 if (view->parent) {
1093 view->parent->child = NULL;
1094 view->parent->focus_child = 0;
1095 } else
1096 TAILQ_REMOVE(&views, view, entry);
1098 err = view_close(view);
1099 if (err)
1100 goto done;
1102 view = NULL;
1103 TAILQ_FOREACH(v, &views, entry) {
1104 if (v->focussed)
1105 break;
1107 if (view == NULL && new_view == NULL) {
1108 /* No view has focus. Try to pick one. */
1109 if (prev)
1110 view = prev;
1111 else if (!TAILQ_EMPTY(&views)) {
1112 view = TAILQ_LAST(&views,
1113 tog_view_list_head);
1115 if (view) {
1116 if (view->focus_child) {
1117 view->child->focussed = 1;
1118 view = view->child;
1119 } else
1120 view->focussed = 1;
1124 if (new_view) {
1125 struct tog_view *v, *t;
1126 /* Only allow one parent view per type. */
1127 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1128 if (v->type != new_view->type)
1129 continue;
1130 TAILQ_REMOVE(&views, v, entry);
1131 err = view_close(v);
1132 if (err)
1133 goto done;
1134 break;
1136 TAILQ_INSERT_TAIL(&views, new_view, entry);
1137 view = new_view;
1139 if (view) {
1140 if (view_is_parent_view(view)) {
1141 if (view->child && view->child->focussed)
1142 view = view->child;
1143 } else {
1144 if (view->parent && view->parent->focussed)
1145 view = view->parent;
1147 show_panel(view->panel);
1148 if (view->child && view_is_splitscreen(view->child))
1149 show_panel(view->child->panel);
1150 if (view->parent && view_is_splitscreen(view)) {
1151 err = view->parent->show(view->parent);
1152 if (err)
1153 goto done;
1155 err = view->show(view);
1156 if (err)
1157 goto done;
1158 if (view->child) {
1159 err = view->child->show(view->child);
1160 if (err)
1161 goto done;
1163 update_panels();
1164 doupdate();
1167 done:
1168 while (!TAILQ_EMPTY(&views)) {
1169 view = TAILQ_FIRST(&views);
1170 TAILQ_REMOVE(&views, view, entry);
1171 view_close(view);
1174 errcode = pthread_mutex_unlock(&tog_mutex);
1175 if (errcode && err == NULL)
1176 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1178 return err;
1181 __dead static void
1182 usage_log(void)
1184 endwin();
1185 fprintf(stderr,
1186 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1187 getprogname());
1188 exit(1);
1191 /* Create newly allocated wide-character string equivalent to a byte string. */
1192 static const struct got_error *
1193 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1195 char *vis = NULL;
1196 const struct got_error *err = NULL;
1198 *ws = NULL;
1199 *wlen = mbstowcs(NULL, s, 0);
1200 if (*wlen == (size_t)-1) {
1201 int vislen;
1202 if (errno != EILSEQ)
1203 return got_error_from_errno("mbstowcs");
1205 /* byte string invalid in current encoding; try to "fix" it */
1206 err = got_mbsavis(&vis, &vislen, s);
1207 if (err)
1208 return err;
1209 *wlen = mbstowcs(NULL, vis, 0);
1210 if (*wlen == (size_t)-1) {
1211 err = got_error_from_errno("mbstowcs"); /* give up */
1212 goto done;
1216 *ws = calloc(*wlen + 1, sizeof(**ws));
1217 if (*ws == NULL) {
1218 err = got_error_from_errno("calloc");
1219 goto done;
1222 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1223 err = got_error_from_errno("mbstowcs");
1224 done:
1225 free(vis);
1226 if (err) {
1227 free(*ws);
1228 *ws = NULL;
1229 *wlen = 0;
1231 return err;
1234 static const struct got_error *
1235 expand_tab(char **ptr, const char *src)
1237 char *dst;
1238 size_t len, n, idx = 0, sz = 0;
1240 *ptr = NULL;
1241 n = len = strlen(src);
1242 dst = malloc((n + 1) * sizeof(char));
1243 if (dst == NULL)
1244 return got_error_from_errno("malloc");
1246 while (idx < len && src[idx]) {
1247 const char c = src[idx];
1249 if (c == '\t') {
1250 size_t nb = TABSIZE - sz % TABSIZE;
1251 n += nb;
1252 dst = reallocarray(dst, n, sizeof(char));
1253 if (dst == NULL)
1254 return got_error_from_errno("reallocarray");
1255 memcpy(dst + sz, " ", nb);
1256 sz += nb;
1257 } else
1258 dst[sz++] = src[idx];
1259 ++idx;
1262 dst[sz] = '\0';
1263 *ptr = dst;
1264 return NULL;
1267 /* Format a line for display, ensuring that it won't overflow a width limit. */
1268 static const struct got_error *
1269 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1270 int col_tab_align, int expand)
1272 const struct got_error *err = NULL;
1273 int cols = 0;
1274 wchar_t *wline = NULL;
1275 char *exstr = NULL;
1276 size_t wlen;
1277 int i;
1279 *wlinep = NULL;
1280 *widthp = 0;
1282 if (expand) {
1283 err = expand_tab(&exstr, line);
1284 if (err)
1285 return err;
1288 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1289 free(exstr);
1290 if (err)
1291 return err;
1293 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1294 wline[wlen - 1] = L'\0';
1295 wlen--;
1297 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1298 wline[wlen - 1] = L'\0';
1299 wlen--;
1302 i = 0;
1303 while (i < wlen) {
1304 int width = wcwidth(wline[i]);
1306 if (width == 0) {
1307 i++;
1308 continue;
1311 if (width == 1 || width == 2) {
1312 if (cols + width > wlimit)
1313 break;
1314 cols += width;
1315 i++;
1316 } else if (width == -1) {
1317 if (wline[i] == L'\t') {
1318 width = TABSIZE -
1319 ((cols + col_tab_align) % TABSIZE);
1320 } else {
1321 width = 1;
1322 wline[i] = L'.';
1324 if (cols + width > wlimit)
1325 break;
1326 cols += width;
1327 i++;
1328 } else {
1329 err = got_error_from_errno("wcwidth");
1330 goto done;
1333 wline[i] = L'\0';
1334 if (widthp)
1335 *widthp = cols;
1336 done:
1337 if (err)
1338 free(wline);
1339 else
1340 *wlinep = wline;
1341 return err;
1344 static const struct got_error*
1345 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1346 struct got_object_id *id, struct got_repository *repo)
1348 static const struct got_error *err = NULL;
1349 struct got_reflist_entry *re;
1350 char *s;
1351 const char *name;
1353 *refs_str = NULL;
1355 TAILQ_FOREACH(re, refs, entry) {
1356 struct got_tag_object *tag = NULL;
1357 struct got_object_id *ref_id;
1358 int cmp;
1360 name = got_ref_get_name(re->ref);
1361 if (strcmp(name, GOT_REF_HEAD) == 0)
1362 continue;
1363 if (strncmp(name, "refs/", 5) == 0)
1364 name += 5;
1365 if (strncmp(name, "got/", 4) == 0 &&
1366 strncmp(name, "got/backup/", 11) != 0)
1367 continue;
1368 if (strncmp(name, "heads/", 6) == 0)
1369 name += 6;
1370 if (strncmp(name, "remotes/", 8) == 0) {
1371 name += 8;
1372 s = strstr(name, "/" GOT_REF_HEAD);
1373 if (s != NULL && s[strlen(s)] == '\0')
1374 continue;
1376 err = got_ref_resolve(&ref_id, repo, re->ref);
1377 if (err)
1378 break;
1379 if (strncmp(name, "tags/", 5) == 0) {
1380 err = got_object_open_as_tag(&tag, repo, ref_id);
1381 if (err) {
1382 if (err->code != GOT_ERR_OBJ_TYPE) {
1383 free(ref_id);
1384 break;
1386 /* Ref points at something other than a tag. */
1387 err = NULL;
1388 tag = NULL;
1391 cmp = got_object_id_cmp(tag ?
1392 got_object_tag_get_object_id(tag) : ref_id, id);
1393 free(ref_id);
1394 if (tag)
1395 got_object_tag_close(tag);
1396 if (cmp != 0)
1397 continue;
1398 s = *refs_str;
1399 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1400 s ? ", " : "", name) == -1) {
1401 err = got_error_from_errno("asprintf");
1402 free(s);
1403 *refs_str = NULL;
1404 break;
1406 free(s);
1409 return err;
1412 static const struct got_error *
1413 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1414 int col_tab_align)
1416 char *smallerthan;
1418 smallerthan = strchr(author, '<');
1419 if (smallerthan && smallerthan[1] != '\0')
1420 author = smallerthan + 1;
1421 author[strcspn(author, "@>")] = '\0';
1422 return format_line(wauthor, author_width, author, limit, col_tab_align,
1423 0);
1426 static const struct got_error *
1427 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1428 struct got_object_id *id, const size_t date_display_cols,
1429 int author_display_cols)
1431 struct tog_log_view_state *s = &view->state.log;
1432 const struct got_error *err = NULL;
1433 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1434 char *logmsg0 = NULL, *logmsg = NULL;
1435 char *author = NULL;
1436 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1437 int author_width, logmsg_width;
1438 char *newline, *line = NULL;
1439 int col, limit;
1440 const int avail = view->ncols;
1441 struct tm tm;
1442 time_t committer_time;
1443 struct tog_color *tc;
1445 committer_time = got_object_commit_get_committer_time(commit);
1446 if (gmtime_r(&committer_time, &tm) == NULL)
1447 return got_error_from_errno("gmtime_r");
1448 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1449 return got_error(GOT_ERR_NO_SPACE);
1451 if (avail <= date_display_cols)
1452 limit = MIN(sizeof(datebuf) - 1, avail);
1453 else
1454 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1455 tc = get_color(&s->colors, TOG_COLOR_DATE);
1456 if (tc)
1457 wattr_on(view->window,
1458 COLOR_PAIR(tc->colorpair), NULL);
1459 waddnstr(view->window, datebuf, limit);
1460 if (tc)
1461 wattr_off(view->window,
1462 COLOR_PAIR(tc->colorpair), NULL);
1463 col = limit;
1464 if (col > avail)
1465 goto done;
1467 if (avail >= 120) {
1468 char *id_str;
1469 err = got_object_id_str(&id_str, id);
1470 if (err)
1471 goto done;
1472 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1473 if (tc)
1474 wattr_on(view->window,
1475 COLOR_PAIR(tc->colorpair), NULL);
1476 wprintw(view->window, "%.8s ", id_str);
1477 if (tc)
1478 wattr_off(view->window,
1479 COLOR_PAIR(tc->colorpair), NULL);
1480 free(id_str);
1481 col += 9;
1482 if (col > avail)
1483 goto done;
1486 author = strdup(got_object_commit_get_author(commit));
1487 if (author == NULL) {
1488 err = got_error_from_errno("strdup");
1489 goto done;
1491 err = format_author(&wauthor, &author_width, author, avail - col, col);
1492 if (err)
1493 goto done;
1494 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1495 if (tc)
1496 wattr_on(view->window,
1497 COLOR_PAIR(tc->colorpair), NULL);
1498 waddwstr(view->window, wauthor);
1499 if (tc)
1500 wattr_off(view->window,
1501 COLOR_PAIR(tc->colorpair), NULL);
1502 col += author_width;
1503 while (col < avail && author_width < author_display_cols + 2) {
1504 waddch(view->window, ' ');
1505 col++;
1506 author_width++;
1508 if (col > avail)
1509 goto done;
1511 err = got_object_commit_get_logmsg(&logmsg0, commit);
1512 if (err)
1513 goto done;
1514 logmsg = logmsg0;
1515 while (*logmsg == '\n')
1516 logmsg++;
1517 newline = strchr(logmsg, '\n');
1518 if (newline)
1519 *newline = '\0';
1520 limit = view->x + avail - col;
1521 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col, 1);
1522 if (err)
1523 goto done;
1524 if (view->x < logmsg_width - 1)
1525 waddwstr(view->window, wlogmsg + view->x);
1526 else
1527 logmsg_width = 0;
1528 col += MAX(logmsg_width - view->x, 0);
1529 while (col < avail) {
1530 waddch(view->window, ' ');
1531 col++;
1533 done:
1534 free(logmsg0);
1535 free(wlogmsg);
1536 free(author);
1537 free(wauthor);
1538 free(line);
1539 return err;
1542 static struct commit_queue_entry *
1543 alloc_commit_queue_entry(struct got_commit_object *commit,
1544 struct got_object_id *id)
1546 struct commit_queue_entry *entry;
1548 entry = calloc(1, sizeof(*entry));
1549 if (entry == NULL)
1550 return NULL;
1552 entry->id = id;
1553 entry->commit = commit;
1554 return entry;
1557 static void
1558 pop_commit(struct commit_queue *commits)
1560 struct commit_queue_entry *entry;
1562 entry = TAILQ_FIRST(&commits->head);
1563 TAILQ_REMOVE(&commits->head, entry, entry);
1564 got_object_commit_close(entry->commit);
1565 commits->ncommits--;
1566 /* Don't free entry->id! It is owned by the commit graph. */
1567 free(entry);
1570 static void
1571 free_commits(struct commit_queue *commits)
1573 while (!TAILQ_EMPTY(&commits->head))
1574 pop_commit(commits);
1577 static const struct got_error *
1578 match_commit(int *have_match, struct got_object_id *id,
1579 struct got_commit_object *commit, regex_t *regex)
1581 const struct got_error *err = NULL;
1582 regmatch_t regmatch;
1583 char *id_str = NULL, *logmsg = NULL;
1585 *have_match = 0;
1587 err = got_object_id_str(&id_str, id);
1588 if (err)
1589 return err;
1591 err = got_object_commit_get_logmsg(&logmsg, commit);
1592 if (err)
1593 goto done;
1595 if (regexec(regex, got_object_commit_get_author(commit), 1,
1596 &regmatch, 0) == 0 ||
1597 regexec(regex, got_object_commit_get_committer(commit), 1,
1598 &regmatch, 0) == 0 ||
1599 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1600 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1601 *have_match = 1;
1602 done:
1603 free(id_str);
1604 free(logmsg);
1605 return err;
1608 static const struct got_error *
1609 queue_commits(struct tog_log_thread_args *a)
1611 const struct got_error *err = NULL;
1614 * We keep all commits open throughout the lifetime of the log
1615 * view in order to avoid having to re-fetch commits from disk
1616 * while updating the display.
1618 do {
1619 struct got_object_id *id;
1620 struct got_commit_object *commit;
1621 struct commit_queue_entry *entry;
1622 int errcode;
1624 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1625 NULL, NULL);
1626 if (err || id == NULL)
1627 break;
1629 err = got_object_open_as_commit(&commit, a->repo, id);
1630 if (err)
1631 break;
1632 entry = alloc_commit_queue_entry(commit, id);
1633 if (entry == NULL) {
1634 err = got_error_from_errno("alloc_commit_queue_entry");
1635 break;
1638 errcode = pthread_mutex_lock(&tog_mutex);
1639 if (errcode) {
1640 err = got_error_set_errno(errcode,
1641 "pthread_mutex_lock");
1642 break;
1645 entry->idx = a->commits->ncommits;
1646 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1647 a->commits->ncommits++;
1649 if (*a->searching == TOG_SEARCH_FORWARD &&
1650 !*a->search_next_done) {
1651 int have_match;
1652 err = match_commit(&have_match, id, commit, a->regex);
1653 if (err)
1654 break;
1655 if (have_match)
1656 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1659 errcode = pthread_mutex_unlock(&tog_mutex);
1660 if (errcode && err == NULL)
1661 err = got_error_set_errno(errcode,
1662 "pthread_mutex_unlock");
1663 if (err)
1664 break;
1665 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1667 return err;
1670 static void
1671 select_commit(struct tog_log_view_state *s)
1673 struct commit_queue_entry *entry;
1674 int ncommits = 0;
1676 entry = s->first_displayed_entry;
1677 while (entry) {
1678 if (ncommits == s->selected) {
1679 s->selected_entry = entry;
1680 break;
1682 entry = TAILQ_NEXT(entry, entry);
1683 ncommits++;
1687 static const struct got_error *
1688 draw_commits(struct tog_view *view)
1690 const struct got_error *err = NULL;
1691 struct tog_log_view_state *s = &view->state.log;
1692 struct commit_queue_entry *entry = s->selected_entry;
1693 const int limit = view->nlines;
1694 int width;
1695 int ncommits, author_cols = 4;
1696 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1697 char *refs_str = NULL;
1698 wchar_t *wline;
1699 struct tog_color *tc;
1700 static const size_t date_display_cols = 12;
1702 if (s->selected_entry &&
1703 !(view->searching && view->search_next_done == 0)) {
1704 struct got_reflist_head *refs;
1705 err = got_object_id_str(&id_str, s->selected_entry->id);
1706 if (err)
1707 return err;
1708 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1709 s->selected_entry->id);
1710 if (refs) {
1711 err = build_refs_str(&refs_str, refs,
1712 s->selected_entry->id, s->repo);
1713 if (err)
1714 goto done;
1718 if (s->thread_args.commits_needed == 0)
1719 halfdelay(10); /* disable fast refresh */
1721 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1722 if (asprintf(&ncommits_str, " [%d/%d] %s",
1723 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1724 (view->searching && !view->search_next_done) ?
1725 "searching..." : "loading...") == -1) {
1726 err = got_error_from_errno("asprintf");
1727 goto done;
1729 } else {
1730 const char *search_str = NULL;
1732 if (view->searching) {
1733 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1734 search_str = "no more matches";
1735 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1736 search_str = "no matches found";
1737 else if (!view->search_next_done)
1738 search_str = "searching...";
1741 if (asprintf(&ncommits_str, " [%d/%d] %s",
1742 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1743 search_str ? search_str :
1744 (refs_str ? refs_str : "")) == -1) {
1745 err = got_error_from_errno("asprintf");
1746 goto done;
1750 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1751 if (asprintf(&header, "commit %s %s%s",
1752 id_str ? id_str : "........................................",
1753 s->in_repo_path, ncommits_str) == -1) {
1754 err = got_error_from_errno("asprintf");
1755 header = NULL;
1756 goto done;
1758 } else if (asprintf(&header, "commit %s%s",
1759 id_str ? id_str : "........................................",
1760 ncommits_str) == -1) {
1761 err = got_error_from_errno("asprintf");
1762 header = NULL;
1763 goto done;
1765 err = format_line(&wline, &width, header, view->ncols, 0, 0);
1766 if (err)
1767 goto done;
1769 werase(view->window);
1771 if (view_needs_focus_indication(view))
1772 wstandout(view->window);
1773 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1774 if (tc)
1775 wattr_on(view->window,
1776 COLOR_PAIR(tc->colorpair), NULL);
1777 waddwstr(view->window, wline);
1778 if (tc)
1779 wattr_off(view->window,
1780 COLOR_PAIR(tc->colorpair), NULL);
1781 while (width < view->ncols) {
1782 waddch(view->window, ' ');
1783 width++;
1785 if (view_needs_focus_indication(view))
1786 wstandend(view->window);
1787 free(wline);
1788 if (limit <= 1)
1789 goto done;
1791 /* Grow author column size if necessary. */
1792 entry = s->first_displayed_entry;
1793 ncommits = 0;
1794 view->maxx = 0;
1795 while (entry) {
1796 char *author, *eol, *msg, *msg0;
1797 wchar_t *wauthor;
1798 int width;
1799 if (ncommits >= limit - 1)
1800 break;
1801 author = strdup(got_object_commit_get_author(entry->commit));
1802 if (author == NULL) {
1803 err = got_error_from_errno("strdup");
1804 goto done;
1806 err = format_author(&wauthor, &width, author, COLS,
1807 date_display_cols);
1808 if (author_cols < width)
1809 author_cols = width;
1810 free(wauthor);
1811 free(author);
1812 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1813 if (err)
1814 goto done;
1815 msg = msg0;
1816 while (*msg == '\n')
1817 ++msg;
1818 if ((eol = strchr(msg, '\n')))
1819 view->maxx = MAX(view->maxx, eol - msg);
1820 else
1821 view->maxx = MAX(view->maxx, strlen(msg));
1822 free(msg0);
1823 ncommits++;
1824 entry = TAILQ_NEXT(entry, entry);
1827 entry = s->first_displayed_entry;
1828 s->last_displayed_entry = s->first_displayed_entry;
1829 ncommits = 0;
1830 while (entry) {
1831 if (ncommits >= limit - 1)
1832 break;
1833 if (ncommits == s->selected)
1834 wstandout(view->window);
1835 err = draw_commit(view, entry->commit, entry->id,
1836 date_display_cols, author_cols);
1837 if (ncommits == s->selected)
1838 wstandend(view->window);
1839 if (err)
1840 goto done;
1841 ncommits++;
1842 s->last_displayed_entry = entry;
1843 entry = TAILQ_NEXT(entry, entry);
1846 view_vborder(view);
1847 update_panels();
1848 doupdate();
1849 done:
1850 free(id_str);
1851 free(refs_str);
1852 free(ncommits_str);
1853 free(header);
1854 return err;
1857 static void
1858 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1860 struct commit_queue_entry *entry;
1861 int nscrolled = 0;
1863 entry = TAILQ_FIRST(&s->commits.head);
1864 if (s->first_displayed_entry == entry)
1865 return;
1867 entry = s->first_displayed_entry;
1868 while (entry && nscrolled < maxscroll) {
1869 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1870 if (entry) {
1871 s->first_displayed_entry = entry;
1872 nscrolled++;
1877 static const struct got_error *
1878 trigger_log_thread(struct tog_view *view, int wait)
1880 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1881 int errcode;
1883 halfdelay(1); /* fast refresh while loading commits */
1885 while (ta->commits_needed > 0 || ta->load_all) {
1886 if (ta->log_complete)
1887 break;
1889 /* Wake the log thread. */
1890 errcode = pthread_cond_signal(&ta->need_commits);
1891 if (errcode)
1892 return got_error_set_errno(errcode,
1893 "pthread_cond_signal");
1896 * The mutex will be released while the view loop waits
1897 * in wgetch(), at which time the log thread will run.
1899 if (!wait)
1900 break;
1902 /* Display progress update in log view. */
1903 show_log_view(view);
1904 update_panels();
1905 doupdate();
1907 /* Wait right here while next commit is being loaded. */
1908 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1909 if (errcode)
1910 return got_error_set_errno(errcode,
1911 "pthread_cond_wait");
1913 /* Display progress update in log view. */
1914 show_log_view(view);
1915 update_panels();
1916 doupdate();
1919 return NULL;
1922 static const struct got_error *
1923 log_scroll_down(struct tog_view *view, int maxscroll)
1925 struct tog_log_view_state *s = &view->state.log;
1926 const struct got_error *err = NULL;
1927 struct commit_queue_entry *pentry;
1928 int nscrolled = 0, ncommits_needed;
1930 if (s->last_displayed_entry == NULL)
1931 return NULL;
1933 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1934 if (s->commits.ncommits < ncommits_needed &&
1935 !s->thread_args.log_complete) {
1937 * Ask the log thread for required amount of commits.
1939 s->thread_args.commits_needed += maxscroll;
1940 err = trigger_log_thread(view, 1);
1941 if (err)
1942 return err;
1945 do {
1946 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1947 if (pentry == NULL)
1948 break;
1950 s->last_displayed_entry = pentry;
1952 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1953 if (pentry == NULL)
1954 break;
1955 s->first_displayed_entry = pentry;
1956 } while (++nscrolled < maxscroll);
1958 return err;
1961 static const struct got_error *
1962 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1963 struct got_commit_object *commit, struct got_object_id *commit_id,
1964 struct tog_view *log_view, struct got_repository *repo)
1966 const struct got_error *err;
1967 struct got_object_qid *parent_id;
1968 struct tog_view *diff_view;
1970 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1971 if (diff_view == NULL)
1972 return got_error_from_errno("view_open");
1974 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1975 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1976 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1977 if (err == NULL)
1978 *new_view = diff_view;
1979 return err;
1982 static const struct got_error *
1983 tree_view_visit_subtree(struct tog_tree_view_state *s,
1984 struct got_tree_object *subtree)
1986 struct tog_parent_tree *parent;
1988 parent = calloc(1, sizeof(*parent));
1989 if (parent == NULL)
1990 return got_error_from_errno("calloc");
1992 parent->tree = s->tree;
1993 parent->first_displayed_entry = s->first_displayed_entry;
1994 parent->selected_entry = s->selected_entry;
1995 parent->selected = s->selected;
1996 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1997 s->tree = subtree;
1998 s->selected = 0;
1999 s->first_displayed_entry = NULL;
2000 return NULL;
2003 static const struct got_error *
2004 tree_view_walk_path(struct tog_tree_view_state *s,
2005 struct got_commit_object *commit, const char *path)
2007 const struct got_error *err = NULL;
2008 struct got_tree_object *tree = NULL;
2009 const char *p;
2010 char *slash, *subpath = NULL;
2012 /* Walk the path and open corresponding tree objects. */
2013 p = path;
2014 while (*p) {
2015 struct got_tree_entry *te;
2016 struct got_object_id *tree_id;
2017 char *te_name;
2019 while (p[0] == '/')
2020 p++;
2022 /* Ensure the correct subtree entry is selected. */
2023 slash = strchr(p, '/');
2024 if (slash == NULL)
2025 te_name = strdup(p);
2026 else
2027 te_name = strndup(p, slash - p);
2028 if (te_name == NULL) {
2029 err = got_error_from_errno("strndup");
2030 break;
2032 te = got_object_tree_find_entry(s->tree, te_name);
2033 if (te == NULL) {
2034 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2035 free(te_name);
2036 break;
2038 free(te_name);
2039 s->first_displayed_entry = s->selected_entry = te;
2041 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2042 break; /* jump to this file's entry */
2044 slash = strchr(p, '/');
2045 if (slash)
2046 subpath = strndup(path, slash - path);
2047 else
2048 subpath = strdup(path);
2049 if (subpath == NULL) {
2050 err = got_error_from_errno("strdup");
2051 break;
2054 err = got_object_id_by_path(&tree_id, s->repo, commit,
2055 subpath);
2056 if (err)
2057 break;
2059 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2060 free(tree_id);
2061 if (err)
2062 break;
2064 err = tree_view_visit_subtree(s, tree);
2065 if (err) {
2066 got_object_tree_close(tree);
2067 break;
2069 if (slash == NULL)
2070 break;
2071 free(subpath);
2072 subpath = NULL;
2073 p = slash;
2076 free(subpath);
2077 return err;
2080 static const struct got_error *
2081 browse_commit_tree(struct tog_view **new_view, int begin_x,
2082 struct commit_queue_entry *entry, const char *path,
2083 const char *head_ref_name, struct got_repository *repo)
2085 const struct got_error *err = NULL;
2086 struct tog_tree_view_state *s;
2087 struct tog_view *tree_view;
2089 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2090 if (tree_view == NULL)
2091 return got_error_from_errno("view_open");
2093 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2094 if (err)
2095 return err;
2096 s = &tree_view->state.tree;
2098 *new_view = tree_view;
2100 if (got_path_is_root_dir(path))
2101 return NULL;
2103 return tree_view_walk_path(s, entry->commit, path);
2106 static const struct got_error *
2107 block_signals_used_by_main_thread(void)
2109 sigset_t sigset;
2110 int errcode;
2112 if (sigemptyset(&sigset) == -1)
2113 return got_error_from_errno("sigemptyset");
2115 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2116 if (sigaddset(&sigset, SIGWINCH) == -1)
2117 return got_error_from_errno("sigaddset");
2118 if (sigaddset(&sigset, SIGCONT) == -1)
2119 return got_error_from_errno("sigaddset");
2120 if (sigaddset(&sigset, SIGINT) == -1)
2121 return got_error_from_errno("sigaddset");
2122 if (sigaddset(&sigset, SIGTERM) == -1)
2123 return got_error_from_errno("sigaddset");
2125 /* ncurses handles SIGTSTP */
2126 if (sigaddset(&sigset, SIGTSTP) == -1)
2127 return got_error_from_errno("sigaddset");
2129 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2130 if (errcode)
2131 return got_error_set_errno(errcode, "pthread_sigmask");
2133 return NULL;
2136 static void *
2137 log_thread(void *arg)
2139 const struct got_error *err = NULL;
2140 int errcode = 0;
2141 struct tog_log_thread_args *a = arg;
2142 int done = 0;
2144 err = block_signals_used_by_main_thread();
2145 if (err)
2146 return (void *)err;
2148 while (!done && !err && !tog_fatal_signal_received()) {
2149 err = queue_commits(a);
2150 if (err) {
2151 if (err->code != GOT_ERR_ITER_COMPLETED)
2152 return (void *)err;
2153 err = NULL;
2154 done = 1;
2155 } else if (a->commits_needed > 0 && !a->load_all)
2156 a->commits_needed--;
2158 errcode = pthread_mutex_lock(&tog_mutex);
2159 if (errcode) {
2160 err = got_error_set_errno(errcode,
2161 "pthread_mutex_lock");
2162 break;
2163 } else if (*a->quit)
2164 done = 1;
2165 else if (*a->first_displayed_entry == NULL) {
2166 *a->first_displayed_entry =
2167 TAILQ_FIRST(&a->commits->head);
2168 *a->selected_entry = *a->first_displayed_entry;
2171 errcode = pthread_cond_signal(&a->commit_loaded);
2172 if (errcode) {
2173 err = got_error_set_errno(errcode,
2174 "pthread_cond_signal");
2175 pthread_mutex_unlock(&tog_mutex);
2176 break;
2179 if (done)
2180 a->commits_needed = 0;
2181 else {
2182 if (a->commits_needed == 0 && !a->load_all) {
2183 errcode = pthread_cond_wait(&a->need_commits,
2184 &tog_mutex);
2185 if (errcode)
2186 err = got_error_set_errno(errcode,
2187 "pthread_cond_wait");
2188 if (*a->quit)
2189 done = 1;
2193 errcode = pthread_mutex_unlock(&tog_mutex);
2194 if (errcode && err == NULL)
2195 err = got_error_set_errno(errcode,
2196 "pthread_mutex_unlock");
2198 a->log_complete = 1;
2199 return (void *)err;
2202 static const struct got_error *
2203 stop_log_thread(struct tog_log_view_state *s)
2205 const struct got_error *err = NULL;
2206 int errcode;
2208 if (s->thread) {
2209 s->quit = 1;
2210 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2211 if (errcode)
2212 return got_error_set_errno(errcode,
2213 "pthread_cond_signal");
2214 errcode = pthread_mutex_unlock(&tog_mutex);
2215 if (errcode)
2216 return got_error_set_errno(errcode,
2217 "pthread_mutex_unlock");
2218 errcode = pthread_join(s->thread, (void **)&err);
2219 if (errcode)
2220 return got_error_set_errno(errcode, "pthread_join");
2221 errcode = pthread_mutex_lock(&tog_mutex);
2222 if (errcode)
2223 return got_error_set_errno(errcode,
2224 "pthread_mutex_lock");
2225 s->thread = 0; //NULL;
2228 if (s->thread_args.repo) {
2229 err = got_repo_close(s->thread_args.repo);
2230 s->thread_args.repo = NULL;
2233 if (s->thread_args.pack_fds) {
2234 const struct got_error *pack_err =
2235 got_repo_pack_fds_close(s->thread_args.pack_fds);
2236 if (err == NULL)
2237 err = pack_err;
2238 s->thread_args.pack_fds = NULL;
2241 if (s->thread_args.graph) {
2242 got_commit_graph_close(s->thread_args.graph);
2243 s->thread_args.graph = NULL;
2246 return err;
2249 static const struct got_error *
2250 close_log_view(struct tog_view *view)
2252 const struct got_error *err = NULL;
2253 struct tog_log_view_state *s = &view->state.log;
2254 int errcode;
2256 err = stop_log_thread(s);
2258 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2259 if (errcode && err == NULL)
2260 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2262 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2263 if (errcode && err == NULL)
2264 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2266 free_commits(&s->commits);
2267 free(s->in_repo_path);
2268 s->in_repo_path = NULL;
2269 free(s->start_id);
2270 s->start_id = NULL;
2271 free(s->head_ref_name);
2272 s->head_ref_name = NULL;
2273 return err;
2276 static const struct got_error *
2277 search_start_log_view(struct tog_view *view)
2279 struct tog_log_view_state *s = &view->state.log;
2281 s->matched_entry = NULL;
2282 s->search_entry = NULL;
2283 return NULL;
2286 static const struct got_error *
2287 search_next_log_view(struct tog_view *view)
2289 const struct got_error *err = NULL;
2290 struct tog_log_view_state *s = &view->state.log;
2291 struct commit_queue_entry *entry;
2293 /* Display progress update in log view. */
2294 show_log_view(view);
2295 update_panels();
2296 doupdate();
2298 if (s->search_entry) {
2299 int errcode, ch;
2300 errcode = pthread_mutex_unlock(&tog_mutex);
2301 if (errcode)
2302 return got_error_set_errno(errcode,
2303 "pthread_mutex_unlock");
2304 ch = wgetch(view->window);
2305 errcode = pthread_mutex_lock(&tog_mutex);
2306 if (errcode)
2307 return got_error_set_errno(errcode,
2308 "pthread_mutex_lock");
2309 if (ch == KEY_BACKSPACE) {
2310 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2311 return NULL;
2313 if (view->searching == TOG_SEARCH_FORWARD)
2314 entry = TAILQ_NEXT(s->search_entry, entry);
2315 else
2316 entry = TAILQ_PREV(s->search_entry,
2317 commit_queue_head, entry);
2318 } else if (s->matched_entry) {
2319 if (view->searching == TOG_SEARCH_FORWARD)
2320 entry = TAILQ_NEXT(s->matched_entry, entry);
2321 else
2322 entry = TAILQ_PREV(s->matched_entry,
2323 commit_queue_head, entry);
2324 } else {
2325 entry = s->selected_entry;
2328 while (1) {
2329 int have_match = 0;
2331 if (entry == NULL) {
2332 if (s->thread_args.log_complete ||
2333 view->searching == TOG_SEARCH_BACKWARD) {
2334 view->search_next_done =
2335 (s->matched_entry == NULL ?
2336 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2337 s->search_entry = NULL;
2338 return NULL;
2341 * Poke the log thread for more commits and return,
2342 * allowing the main loop to make progress. Search
2343 * will resume at s->search_entry once we come back.
2345 s->thread_args.commits_needed++;
2346 return trigger_log_thread(view, 0);
2349 err = match_commit(&have_match, entry->id, entry->commit,
2350 &view->regex);
2351 if (err)
2352 break;
2353 if (have_match) {
2354 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2355 s->matched_entry = entry;
2356 break;
2359 s->search_entry = entry;
2360 if (view->searching == TOG_SEARCH_FORWARD)
2361 entry = TAILQ_NEXT(entry, entry);
2362 else
2363 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2366 if (s->matched_entry) {
2367 int cur = s->selected_entry->idx;
2368 while (cur < s->matched_entry->idx) {
2369 err = input_log_view(NULL, view, KEY_DOWN);
2370 if (err)
2371 return err;
2372 cur++;
2374 while (cur > s->matched_entry->idx) {
2375 err = input_log_view(NULL, view, KEY_UP);
2376 if (err)
2377 return err;
2378 cur--;
2382 s->search_entry = NULL;
2384 return NULL;
2387 static const struct got_error *
2388 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2389 struct got_repository *repo, const char *head_ref_name,
2390 const char *in_repo_path, int log_branches)
2392 const struct got_error *err = NULL;
2393 struct tog_log_view_state *s = &view->state.log;
2394 struct got_repository *thread_repo = NULL;
2395 struct got_commit_graph *thread_graph = NULL;
2396 int errcode;
2398 if (in_repo_path != s->in_repo_path) {
2399 free(s->in_repo_path);
2400 s->in_repo_path = strdup(in_repo_path);
2401 if (s->in_repo_path == NULL)
2402 return got_error_from_errno("strdup");
2405 /* The commit queue only contains commits being displayed. */
2406 TAILQ_INIT(&s->commits.head);
2407 s->commits.ncommits = 0;
2409 s->repo = repo;
2410 if (head_ref_name) {
2411 s->head_ref_name = strdup(head_ref_name);
2412 if (s->head_ref_name == NULL) {
2413 err = got_error_from_errno("strdup");
2414 goto done;
2417 s->start_id = got_object_id_dup(start_id);
2418 if (s->start_id == NULL) {
2419 err = got_error_from_errno("got_object_id_dup");
2420 goto done;
2422 s->log_branches = log_branches;
2424 STAILQ_INIT(&s->colors);
2425 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2426 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2427 get_color_value("TOG_COLOR_COMMIT"));
2428 if (err)
2429 goto done;
2430 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2431 get_color_value("TOG_COLOR_AUTHOR"));
2432 if (err) {
2433 free_colors(&s->colors);
2434 goto done;
2436 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2437 get_color_value("TOG_COLOR_DATE"));
2438 if (err) {
2439 free_colors(&s->colors);
2440 goto done;
2444 view->show = show_log_view;
2445 view->input = input_log_view;
2446 view->close = close_log_view;
2447 view->search_start = search_start_log_view;
2448 view->search_next = search_next_log_view;
2450 if (s->thread_args.pack_fds == NULL) {
2451 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2452 if (err)
2453 goto done;
2455 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2456 s->thread_args.pack_fds);
2457 if (err)
2458 goto done;
2459 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2460 !s->log_branches);
2461 if (err)
2462 goto done;
2463 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2464 s->repo, NULL, NULL);
2465 if (err)
2466 goto done;
2468 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2469 if (errcode) {
2470 err = got_error_set_errno(errcode, "pthread_cond_init");
2471 goto done;
2473 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2474 if (errcode) {
2475 err = got_error_set_errno(errcode, "pthread_cond_init");
2476 goto done;
2479 s->thread_args.commits_needed = view->nlines;
2480 s->thread_args.graph = thread_graph;
2481 s->thread_args.commits = &s->commits;
2482 s->thread_args.in_repo_path = s->in_repo_path;
2483 s->thread_args.start_id = s->start_id;
2484 s->thread_args.repo = thread_repo;
2485 s->thread_args.log_complete = 0;
2486 s->thread_args.quit = &s->quit;
2487 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2488 s->thread_args.selected_entry = &s->selected_entry;
2489 s->thread_args.searching = &view->searching;
2490 s->thread_args.search_next_done = &view->search_next_done;
2491 s->thread_args.regex = &view->regex;
2492 done:
2493 if (err)
2494 close_log_view(view);
2495 return err;
2498 static const struct got_error *
2499 show_log_view(struct tog_view *view)
2501 const struct got_error *err;
2502 struct tog_log_view_state *s = &view->state.log;
2504 if (s->thread == 0) { //NULL) {
2505 int errcode = pthread_create(&s->thread, NULL, log_thread,
2506 &s->thread_args);
2507 if (errcode)
2508 return got_error_set_errno(errcode, "pthread_create");
2509 if (s->thread_args.commits_needed > 0) {
2510 err = trigger_log_thread(view, 1);
2511 if (err)
2512 return err;
2516 return draw_commits(view);
2519 static const struct got_error *
2520 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2522 const struct got_error *err = NULL;
2523 struct tog_log_view_state *s = &view->state.log;
2524 struct tog_view *diff_view = NULL, *tree_view = NULL;
2525 struct tog_view *ref_view = NULL;
2526 struct commit_queue_entry *entry;
2527 int begin_x = 0, n, nscroll = view->nlines - 1;
2529 if (s->thread_args.load_all) {
2530 if (ch == KEY_BACKSPACE)
2531 s->thread_args.load_all = 0;
2532 else if (s->thread_args.log_complete) {
2533 s->thread_args.load_all = 0;
2534 log_scroll_down(view, s->commits.ncommits);
2535 s->selected = MIN(view->nlines - 2,
2536 s->commits.ncommits - 1);
2537 select_commit(s);
2539 return NULL;
2542 switch (ch) {
2543 case 'q':
2544 s->quit = 1;
2545 break;
2546 case '0':
2547 view->x = 0;
2548 break;
2549 case '$':
2550 view->x = MAX(view->maxx - view->ncols / 2, 0);
2551 break;
2552 case KEY_RIGHT:
2553 case 'l':
2554 if (view->x + view->ncols / 2 < view->maxx)
2555 view->x += 2; /* move two columns right */
2556 break;
2557 case KEY_LEFT:
2558 case 'h':
2559 view->x -= MIN(view->x, 2); /* move two columns back */
2560 break;
2561 case 'k':
2562 case KEY_UP:
2563 case '<':
2564 case ',':
2565 case CTRL('p'):
2566 if (s->first_displayed_entry == NULL)
2567 break;
2568 if (s->selected > 0)
2569 s->selected--;
2570 else
2571 log_scroll_up(s, 1);
2572 select_commit(s);
2573 break;
2574 case 'g':
2575 case KEY_HOME:
2576 s->selected = 0;
2577 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2578 select_commit(s);
2579 break;
2580 case CTRL('u'):
2581 case 'u':
2582 nscroll /= 2;
2583 /* FALL THROUGH */
2584 case KEY_PPAGE:
2585 case CTRL('b'):
2586 if (s->first_displayed_entry == NULL)
2587 break;
2588 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2589 s->selected = MAX(0, s->selected - nscroll - 1);
2590 else
2591 log_scroll_up(s, nscroll);
2592 select_commit(s);
2593 break;
2594 case 'j':
2595 case KEY_DOWN:
2596 case '>':
2597 case '.':
2598 case CTRL('n'):
2599 if (s->first_displayed_entry == NULL)
2600 break;
2601 if (s->selected < MIN(view->nlines - 2,
2602 s->commits.ncommits - 1))
2603 s->selected++;
2604 else {
2605 err = log_scroll_down(view, 1);
2606 if (err)
2607 break;
2609 select_commit(s);
2610 break;
2611 case 'G':
2612 case KEY_END: {
2613 /* We don't know yet how many commits, so we're forced to
2614 * traverse them all. */
2615 if (!s->thread_args.log_complete) {
2616 s->thread_args.load_all = 1;
2617 return trigger_log_thread(view, 0);
2620 s->selected = 0;
2621 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2622 for (n = 0; n < view->nlines - 1; n++) {
2623 if (entry == NULL)
2624 break;
2625 s->first_displayed_entry = entry;
2626 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2628 if (n > 0)
2629 s->selected = n - 1;
2630 select_commit(s);
2631 break;
2633 case CTRL('d'):
2634 case 'd':
2635 nscroll /= 2;
2636 /* FALL THROUGH */
2637 case KEY_NPAGE:
2638 case CTRL('f'): {
2639 struct commit_queue_entry *first;
2640 first = s->first_displayed_entry;
2641 if (first == NULL)
2642 break;
2643 err = log_scroll_down(view, nscroll);
2644 if (err)
2645 break;
2646 if (first == s->first_displayed_entry &&
2647 s->selected < MIN(view->nlines - 2,
2648 s->commits.ncommits - 1)) {
2649 /* can't scroll further down */
2650 s->selected += MIN(s->last_displayed_entry->idx -
2651 s->selected_entry->idx, nscroll + 1);
2653 select_commit(s);
2654 break;
2656 case KEY_RESIZE:
2657 if (s->selected > view->nlines - 2)
2658 s->selected = view->nlines - 2;
2659 if (s->selected > s->commits.ncommits - 1)
2660 s->selected = s->commits.ncommits - 1;
2661 select_commit(s);
2662 if (s->commits.ncommits < view->nlines - 1 &&
2663 !s->thread_args.log_complete) {
2664 s->thread_args.commits_needed += (view->nlines - 1) -
2665 s->commits.ncommits;
2666 err = trigger_log_thread(view, 1);
2668 break;
2669 case KEY_ENTER:
2670 case ' ':
2671 case '\r':
2672 if (s->selected_entry == NULL)
2673 break;
2674 if (view_is_parent_view(view))
2675 begin_x = view_split_begin_x(view->begin_x);
2676 err = open_diff_view_for_commit(&diff_view, begin_x,
2677 s->selected_entry->commit, s->selected_entry->id,
2678 view, s->repo);
2679 if (err)
2680 break;
2681 view->focussed = 0;
2682 diff_view->focussed = 1;
2683 if (view_is_parent_view(view)) {
2684 err = view_close_child(view);
2685 if (err)
2686 return err;
2687 view_set_child(view, diff_view);
2688 view->focus_child = 1;
2689 } else
2690 *new_view = diff_view;
2691 break;
2692 case 't':
2693 if (s->selected_entry == NULL)
2694 break;
2695 if (view_is_parent_view(view))
2696 begin_x = view_split_begin_x(view->begin_x);
2697 err = browse_commit_tree(&tree_view, begin_x,
2698 s->selected_entry, s->in_repo_path, s->head_ref_name,
2699 s->repo);
2700 if (err)
2701 break;
2702 view->focussed = 0;
2703 tree_view->focussed = 1;
2704 if (view_is_parent_view(view)) {
2705 err = view_close_child(view);
2706 if (err)
2707 return err;
2708 view_set_child(view, tree_view);
2709 view->focus_child = 1;
2710 } else
2711 *new_view = tree_view;
2712 break;
2713 case KEY_BACKSPACE:
2714 case CTRL('l'):
2715 case 'B':
2716 if (ch == KEY_BACKSPACE &&
2717 got_path_is_root_dir(s->in_repo_path))
2718 break;
2719 err = stop_log_thread(s);
2720 if (err)
2721 return err;
2722 if (ch == KEY_BACKSPACE) {
2723 char *parent_path;
2724 err = got_path_dirname(&parent_path, s->in_repo_path);
2725 if (err)
2726 return err;
2727 free(s->in_repo_path);
2728 s->in_repo_path = parent_path;
2729 s->thread_args.in_repo_path = s->in_repo_path;
2730 } else if (ch == CTRL('l')) {
2731 struct got_object_id *start_id;
2732 err = got_repo_match_object_id(&start_id, NULL,
2733 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2734 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2735 if (err)
2736 return err;
2737 free(s->start_id);
2738 s->start_id = start_id;
2739 s->thread_args.start_id = s->start_id;
2740 } else /* 'B' */
2741 s->log_branches = !s->log_branches;
2743 err = got_repo_open(&s->thread_args.repo,
2744 got_repo_get_path(s->repo), NULL,
2745 s->thread_args.pack_fds);
2746 if (err)
2747 return err;
2748 tog_free_refs();
2749 err = tog_load_refs(s->repo, 0);
2750 if (err)
2751 return err;
2752 err = got_commit_graph_open(&s->thread_args.graph,
2753 s->in_repo_path, !s->log_branches);
2754 if (err)
2755 return err;
2756 err = got_commit_graph_iter_start(s->thread_args.graph,
2757 s->start_id, s->repo, NULL, NULL);
2758 if (err)
2759 return err;
2760 free_commits(&s->commits);
2761 s->first_displayed_entry = NULL;
2762 s->last_displayed_entry = NULL;
2763 s->selected_entry = NULL;
2764 s->selected = 0;
2765 s->thread_args.log_complete = 0;
2766 s->quit = 0;
2767 s->thread_args.commits_needed = view->nlines;
2768 break;
2769 case 'r':
2770 if (view_is_parent_view(view))
2771 begin_x = view_split_begin_x(view->begin_x);
2772 ref_view = view_open(view->nlines, view->ncols,
2773 view->begin_y, begin_x, TOG_VIEW_REF);
2774 if (ref_view == NULL)
2775 return got_error_from_errno("view_open");
2776 err = open_ref_view(ref_view, s->repo);
2777 if (err) {
2778 view_close(ref_view);
2779 return err;
2781 view->focussed = 0;
2782 ref_view->focussed = 1;
2783 if (view_is_parent_view(view)) {
2784 err = view_close_child(view);
2785 if (err)
2786 return err;
2787 view_set_child(view, ref_view);
2788 view->focus_child = 1;
2789 } else
2790 *new_view = ref_view;
2791 break;
2792 default:
2793 break;
2796 return err;
2799 static const struct got_error *
2800 apply_unveil(const char *repo_path, const char *worktree_path)
2802 const struct got_error *error;
2804 #ifdef PROFILE
2805 if (unveil("gmon.out", "rwc") != 0)
2806 return got_error_from_errno2("unveil", "gmon.out");
2807 #endif
2808 if (repo_path && unveil(repo_path, "r") != 0)
2809 return got_error_from_errno2("unveil", repo_path);
2811 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2812 return got_error_from_errno2("unveil", worktree_path);
2814 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2815 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2817 error = got_privsep_unveil_exec_helpers();
2818 if (error != NULL)
2819 return error;
2821 if (unveil(NULL, NULL) != 0)
2822 return got_error_from_errno("unveil");
2824 return NULL;
2827 static void
2828 init_curses(void)
2831 * Override default signal handlers before starting ncurses.
2832 * This should prevent ncurses from installing its own
2833 * broken cleanup() signal handler.
2835 signal(SIGWINCH, tog_sigwinch);
2836 signal(SIGPIPE, tog_sigpipe);
2837 signal(SIGCONT, tog_sigcont);
2838 signal(SIGINT, tog_sigint);
2839 signal(SIGTERM, tog_sigterm);
2841 initscr();
2842 cbreak();
2843 halfdelay(1); /* Do fast refresh while initial view is loading. */
2844 noecho();
2845 nonl();
2846 intrflush(stdscr, FALSE);
2847 keypad(stdscr, TRUE);
2848 curs_set(0);
2849 if (getenv("TOG_COLORS") != NULL) {
2850 start_color();
2851 use_default_colors();
2855 static const struct got_error *
2856 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2857 struct got_repository *repo, struct got_worktree *worktree)
2859 const struct got_error *err = NULL;
2861 if (argc == 0) {
2862 *in_repo_path = strdup("/");
2863 if (*in_repo_path == NULL)
2864 return got_error_from_errno("strdup");
2865 return NULL;
2868 if (worktree) {
2869 const char *prefix = got_worktree_get_path_prefix(worktree);
2870 char *p;
2872 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2873 if (err)
2874 return err;
2875 if (asprintf(in_repo_path, "%s%s%s", prefix,
2876 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2877 p) == -1) {
2878 err = got_error_from_errno("asprintf");
2879 *in_repo_path = NULL;
2881 free(p);
2882 } else
2883 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2885 return err;
2888 static const struct got_error *
2889 cmd_log(int argc, char *argv[])
2891 const struct got_error *error;
2892 struct got_repository *repo = NULL;
2893 struct got_worktree *worktree = NULL;
2894 struct got_object_id *start_id = NULL;
2895 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2896 char *start_commit = NULL, *label = NULL;
2897 struct got_reference *ref = NULL;
2898 const char *head_ref_name = NULL;
2899 int ch, log_branches = 0;
2900 struct tog_view *view;
2901 int *pack_fds = NULL;
2903 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2904 switch (ch) {
2905 case 'b':
2906 log_branches = 1;
2907 break;
2908 case 'c':
2909 start_commit = optarg;
2910 break;
2911 case 'r':
2912 repo_path = realpath(optarg, NULL);
2913 if (repo_path == NULL)
2914 return got_error_from_errno2("realpath",
2915 optarg);
2916 break;
2917 default:
2918 usage_log();
2919 /* NOTREACHED */
2923 argc -= optind;
2924 argv += optind;
2926 if (argc > 1)
2927 usage_log();
2929 error = got_repo_pack_fds_open(&pack_fds);
2930 if (error != NULL)
2931 goto done;
2933 if (repo_path == NULL) {
2934 cwd = getcwd(NULL, 0);
2935 if (cwd == NULL)
2936 return got_error_from_errno("getcwd");
2937 error = got_worktree_open(&worktree, cwd);
2938 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2939 goto done;
2940 if (worktree)
2941 repo_path =
2942 strdup(got_worktree_get_repo_path(worktree));
2943 else
2944 repo_path = strdup(cwd);
2945 if (repo_path == NULL) {
2946 error = got_error_from_errno("strdup");
2947 goto done;
2951 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2952 if (error != NULL)
2953 goto done;
2955 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2956 repo, worktree);
2957 if (error)
2958 goto done;
2960 init_curses();
2962 error = apply_unveil(got_repo_get_path(repo),
2963 worktree ? got_worktree_get_root_path(worktree) : NULL);
2964 if (error)
2965 goto done;
2967 /* already loaded by tog_log_with_path()? */
2968 if (TAILQ_EMPTY(&tog_refs)) {
2969 error = tog_load_refs(repo, 0);
2970 if (error)
2971 goto done;
2974 if (start_commit == NULL) {
2975 error = got_repo_match_object_id(&start_id, &label,
2976 worktree ? got_worktree_get_head_ref_name(worktree) :
2977 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2978 if (error)
2979 goto done;
2980 head_ref_name = label;
2981 } else {
2982 error = got_ref_open(&ref, repo, start_commit, 0);
2983 if (error == NULL)
2984 head_ref_name = got_ref_get_name(ref);
2985 else if (error->code != GOT_ERR_NOT_REF)
2986 goto done;
2987 error = got_repo_match_object_id(&start_id, NULL,
2988 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2989 if (error)
2990 goto done;
2993 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2994 if (view == NULL) {
2995 error = got_error_from_errno("view_open");
2996 goto done;
2998 error = open_log_view(view, start_id, repo, head_ref_name,
2999 in_repo_path, log_branches);
3000 if (error)
3001 goto done;
3002 if (worktree) {
3003 /* Release work tree lock. */
3004 got_worktree_close(worktree);
3005 worktree = NULL;
3007 error = view_loop(view);
3008 done:
3009 free(in_repo_path);
3010 free(repo_path);
3011 free(cwd);
3012 free(start_id);
3013 free(label);
3014 if (ref)
3015 got_ref_close(ref);
3016 if (repo) {
3017 const struct got_error *close_err = got_repo_close(repo);
3018 if (error == NULL)
3019 error = close_err;
3021 if (worktree)
3022 got_worktree_close(worktree);
3023 if (pack_fds) {
3024 const struct got_error *pack_err =
3025 got_repo_pack_fds_close(pack_fds);
3026 if (error == NULL)
3027 error = pack_err;
3029 tog_free_refs();
3030 return error;
3033 __dead static void
3034 usage_diff(void)
3036 endwin();
3037 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3038 "[-w] object1 object2\n", getprogname());
3039 exit(1);
3042 static int
3043 match_line(const char *line, regex_t *regex, size_t nmatch,
3044 regmatch_t *regmatch)
3046 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3049 struct tog_color *
3050 match_color(struct tog_colors *colors, const char *line)
3052 struct tog_color *tc = NULL;
3054 STAILQ_FOREACH(tc, colors, entry) {
3055 if (match_line(line, &tc->regex, 0, NULL))
3056 return tc;
3059 return NULL;
3062 static const struct got_error *
3063 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3064 WINDOW *window, int skip, regmatch_t *regmatch)
3066 const struct got_error *err = NULL;
3067 wchar_t *wline;
3068 int rme, rms, n, width;
3070 *wtotal = 0;
3071 rms = regmatch->rm_so;
3072 rme = regmatch->rm_eo;
3074 err = format_line(&wline, &width, line, wlimit + skip,
3075 col_tab_align, 1);
3076 if (err)
3077 return err;
3079 /* draw up to matched token if we haven't scrolled past it */
3080 n = MAX(rms - skip, 0);
3081 if (n) {
3082 waddnwstr(window, wline + skip, n);
3083 wlimit -= n;
3084 *wtotal += n;
3087 if (wlimit > 0) {
3088 int len = rme - rms;
3089 n = 0;
3090 if (skip > rms) {
3091 n = skip - rms;
3092 len = MAX(len - n, 0);
3094 /* draw (visible part of) matched token (if scrolled into it) */
3095 if (len) {
3096 wattron(window, A_STANDOUT);
3097 waddnwstr(window, wline + rms + n, len);
3098 wattroff(window, A_STANDOUT);
3099 wlimit -= len;
3100 *wtotal += len;
3104 if (wlimit > 0 && skip < width) { /* draw rest of line */
3105 n = 0;
3106 if (skip > rme)
3107 n = MIN(skip - rme, width - rme);
3108 waddnwstr(window, wline + rme + n, wlimit);
3111 *wtotal = width;
3112 free(wline);
3113 return NULL;
3116 static const struct got_error *
3117 draw_file(struct tog_view *view, const char *header)
3119 struct tog_diff_view_state *s = &view->state.diff;
3120 regmatch_t *regmatch = &view->regmatch;
3121 const struct got_error *err;
3122 int nprinted = 0;
3123 char *line;
3124 size_t linesize = 0;
3125 ssize_t linelen;
3126 struct tog_color *tc;
3127 wchar_t *wline;
3128 int width;
3129 int max_lines = view->nlines;
3130 int nlines = s->nlines;
3131 off_t line_offset;
3133 line_offset = s->line_offsets[s->first_displayed_line - 1];
3134 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3135 return got_error_from_errno("fseek");
3137 werase(view->window);
3139 if (header) {
3140 if (asprintf(&line, "[%d/%d] %s",
3141 s->first_displayed_line - 1 + s->selected_line, nlines,
3142 header) == -1)
3143 return got_error_from_errno("asprintf");
3144 err = format_line(&wline, &width, line, view->ncols, 0, 0);
3145 free(line);
3146 if (err)
3147 return err;
3149 if (view_needs_focus_indication(view))
3150 wstandout(view->window);
3151 waddwstr(view->window, wline);
3152 free(wline);
3153 wline = NULL;
3154 if (view_needs_focus_indication(view))
3155 wstandend(view->window);
3156 if (width <= view->ncols - 1)
3157 waddch(view->window, '\n');
3159 if (max_lines <= 1)
3160 return NULL;
3161 max_lines--;
3164 s->eof = 0;
3165 view->maxx = 0;
3166 line = NULL;
3167 while (max_lines > 0 && nprinted < max_lines) {
3168 linelen = getline(&line, &linesize, s->f);
3169 if (linelen == -1) {
3170 if (feof(s->f)) {
3171 s->eof = 1;
3172 break;
3174 free(line);
3175 return got_ferror(s->f, GOT_ERR_IO);
3178 view->maxx = MAX(view->maxx, linelen);
3180 tc = match_color(&s->colors, line);
3181 if (tc)
3182 wattr_on(view->window,
3183 COLOR_PAIR(tc->colorpair), NULL);
3184 if (s->first_displayed_line + nprinted == s->matched_line &&
3185 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3186 err = add_matched_line(&width, line, view->ncols, 0,
3187 view->window, view->x, regmatch);
3188 if (err) {
3189 free(line);
3190 return err;
3192 } else {
3193 err = format_line(&wline, &width, line,
3194 view->x + view->ncols, 0, view->x ? 1 : 0);
3195 if (err) {
3196 free(line);
3197 return err;
3199 if (view->x < width - 1)
3200 waddwstr(view->window, wline + view->x);
3201 free(wline);
3202 wline = NULL;
3204 if (tc)
3205 wattr_off(view->window,
3206 COLOR_PAIR(tc->colorpair), NULL);
3207 if (width - view->x <= view->ncols - 1)
3208 waddch(view->window, '\n');
3209 nprinted++;
3211 free(line);
3212 if (nprinted >= 1)
3213 s->last_displayed_line = s->first_displayed_line +
3214 (nprinted - 1);
3215 else
3216 s->last_displayed_line = s->first_displayed_line;
3218 view_vborder(view);
3220 if (s->eof) {
3221 while (nprinted < view->nlines) {
3222 waddch(view->window, '\n');
3223 nprinted++;
3226 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols,
3227 0, 0);
3228 if (err) {
3229 return err;
3232 wstandout(view->window);
3233 waddwstr(view->window, wline);
3234 free(wline);
3235 wline = NULL;
3236 wstandend(view->window);
3239 return NULL;
3242 static char *
3243 get_datestr(time_t *time, char *datebuf)
3245 struct tm mytm, *tm;
3246 char *p, *s;
3248 tm = gmtime_r(time, &mytm);
3249 if (tm == NULL)
3250 return NULL;
3251 s = asctime_r(tm, datebuf);
3252 if (s == NULL)
3253 return NULL;
3254 p = strchr(s, '\n');
3255 if (p)
3256 *p = '\0';
3257 return s;
3260 static const struct got_error *
3261 get_changed_paths(struct got_pathlist_head *paths,
3262 struct got_commit_object *commit, struct got_repository *repo)
3264 const struct got_error *err = NULL;
3265 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3266 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3267 struct got_object_qid *qid;
3269 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3270 if (qid != NULL) {
3271 struct got_commit_object *pcommit;
3272 err = got_object_open_as_commit(&pcommit, repo,
3273 &qid->id);
3274 if (err)
3275 return err;
3277 tree_id1 = got_object_id_dup(
3278 got_object_commit_get_tree_id(pcommit));
3279 if (tree_id1 == NULL) {
3280 got_object_commit_close(pcommit);
3281 return got_error_from_errno("got_object_id_dup");
3283 got_object_commit_close(pcommit);
3287 if (tree_id1) {
3288 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3289 if (err)
3290 goto done;
3293 tree_id2 = got_object_commit_get_tree_id(commit);
3294 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3295 if (err)
3296 goto done;
3298 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3299 got_diff_tree_collect_changed_paths, paths, 0);
3300 done:
3301 if (tree1)
3302 got_object_tree_close(tree1);
3303 if (tree2)
3304 got_object_tree_close(tree2);
3305 free(tree_id1);
3306 return err;
3309 static const struct got_error *
3310 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3312 off_t *p;
3314 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3315 if (p == NULL)
3316 return got_error_from_errno("reallocarray");
3317 *line_offsets = p;
3318 (*line_offsets)[*nlines] = off;
3319 (*nlines)++;
3320 return NULL;
3323 static const struct got_error *
3324 write_commit_info(off_t **line_offsets, size_t *nlines,
3325 struct got_object_id *commit_id, struct got_reflist_head *refs,
3326 struct got_repository *repo, FILE *outfile)
3328 const struct got_error *err = NULL;
3329 char datebuf[26], *datestr;
3330 struct got_commit_object *commit;
3331 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3332 time_t committer_time;
3333 const char *author, *committer;
3334 char *refs_str = NULL;
3335 struct got_pathlist_head changed_paths;
3336 struct got_pathlist_entry *pe;
3337 off_t outoff = 0;
3338 int n;
3340 TAILQ_INIT(&changed_paths);
3342 if (refs) {
3343 err = build_refs_str(&refs_str, refs, commit_id, repo);
3344 if (err)
3345 return err;
3348 err = got_object_open_as_commit(&commit, repo, commit_id);
3349 if (err)
3350 return err;
3352 err = got_object_id_str(&id_str, commit_id);
3353 if (err) {
3354 err = got_error_from_errno("got_object_id_str");
3355 goto done;
3358 err = add_line_offset(line_offsets, nlines, 0);
3359 if (err)
3360 goto done;
3362 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3363 refs_str ? refs_str : "", refs_str ? ")" : "");
3364 if (n < 0) {
3365 err = got_error_from_errno("fprintf");
3366 goto done;
3368 outoff += n;
3369 err = add_line_offset(line_offsets, nlines, outoff);
3370 if (err)
3371 goto done;
3373 n = fprintf(outfile, "from: %s\n",
3374 got_object_commit_get_author(commit));
3375 if (n < 0) {
3376 err = got_error_from_errno("fprintf");
3377 goto done;
3379 outoff += n;
3380 err = add_line_offset(line_offsets, nlines, outoff);
3381 if (err)
3382 goto done;
3384 committer_time = got_object_commit_get_committer_time(commit);
3385 datestr = get_datestr(&committer_time, datebuf);
3386 if (datestr) {
3387 n = fprintf(outfile, "date: %s UTC\n", datestr);
3388 if (n < 0) {
3389 err = got_error_from_errno("fprintf");
3390 goto done;
3392 outoff += n;
3393 err = add_line_offset(line_offsets, nlines, outoff);
3394 if (err)
3395 goto done;
3397 author = got_object_commit_get_author(commit);
3398 committer = got_object_commit_get_committer(commit);
3399 if (strcmp(author, committer) != 0) {
3400 n = fprintf(outfile, "via: %s\n", committer);
3401 if (n < 0) {
3402 err = got_error_from_errno("fprintf");
3403 goto done;
3405 outoff += n;
3406 err = add_line_offset(line_offsets, nlines, outoff);
3407 if (err)
3408 goto done;
3410 if (got_object_commit_get_nparents(commit) > 1) {
3411 const struct got_object_id_queue *parent_ids;
3412 struct got_object_qid *qid;
3413 int pn = 1;
3414 parent_ids = got_object_commit_get_parent_ids(commit);
3415 STAILQ_FOREACH(qid, parent_ids, entry) {
3416 err = got_object_id_str(&id_str, &qid->id);
3417 if (err)
3418 goto done;
3419 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3420 if (n < 0) {
3421 err = got_error_from_errno("fprintf");
3422 goto done;
3424 outoff += n;
3425 err = add_line_offset(line_offsets, nlines, outoff);
3426 if (err)
3427 goto done;
3428 free(id_str);
3429 id_str = NULL;
3433 err = got_object_commit_get_logmsg(&logmsg, commit);
3434 if (err)
3435 goto done;
3436 s = logmsg;
3437 while ((line = strsep(&s, "\n")) != NULL) {
3438 n = fprintf(outfile, "%s\n", line);
3439 if (n < 0) {
3440 err = got_error_from_errno("fprintf");
3441 goto done;
3443 outoff += n;
3444 err = add_line_offset(line_offsets, nlines, outoff);
3445 if (err)
3446 goto done;
3449 err = get_changed_paths(&changed_paths, commit, repo);
3450 if (err)
3451 goto done;
3452 TAILQ_FOREACH(pe, &changed_paths, entry) {
3453 struct got_diff_changed_path *cp = pe->data;
3454 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3455 if (n < 0) {
3456 err = got_error_from_errno("fprintf");
3457 goto done;
3459 outoff += n;
3460 err = add_line_offset(line_offsets, nlines, outoff);
3461 if (err)
3462 goto done;
3463 free((char *)pe->path);
3464 free(pe->data);
3467 fputc('\n', outfile);
3468 outoff++;
3469 err = add_line_offset(line_offsets, nlines, outoff);
3470 done:
3471 got_pathlist_free(&changed_paths);
3472 free(id_str);
3473 free(logmsg);
3474 free(refs_str);
3475 got_object_commit_close(commit);
3476 if (err) {
3477 free(*line_offsets);
3478 *line_offsets = NULL;
3479 *nlines = 0;
3481 return err;
3484 static const struct got_error *
3485 create_diff(struct tog_diff_view_state *s)
3487 const struct got_error *err = NULL;
3488 FILE *f = NULL;
3489 int obj_type;
3491 free(s->line_offsets);
3492 s->line_offsets = malloc(sizeof(off_t));
3493 if (s->line_offsets == NULL)
3494 return got_error_from_errno("malloc");
3495 s->nlines = 0;
3497 f = got_opentemp();
3498 if (f == NULL) {
3499 err = got_error_from_errno("got_opentemp");
3500 goto done;
3502 if (s->f && fclose(s->f) == EOF) {
3503 err = got_error_from_errno("fclose");
3504 goto done;
3506 s->f = f;
3508 if (s->id1)
3509 err = got_object_get_type(&obj_type, s->repo, s->id1);
3510 else
3511 err = got_object_get_type(&obj_type, s->repo, s->id2);
3512 if (err)
3513 goto done;
3515 switch (obj_type) {
3516 case GOT_OBJ_TYPE_BLOB:
3517 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3518 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3519 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3520 s->repo, s->f);
3521 break;
3522 case GOT_OBJ_TYPE_TREE:
3523 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3524 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3525 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3526 break;
3527 case GOT_OBJ_TYPE_COMMIT: {
3528 const struct got_object_id_queue *parent_ids;
3529 struct got_object_qid *pid;
3530 struct got_commit_object *commit2;
3531 struct got_reflist_head *refs;
3533 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3534 if (err)
3535 goto done;
3536 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3537 /* Show commit info if we're diffing to a parent/root commit. */
3538 if (s->id1 == NULL) {
3539 err = write_commit_info(&s->line_offsets, &s->nlines,
3540 s->id2, refs, s->repo, s->f);
3541 if (err)
3542 goto done;
3543 } else {
3544 parent_ids = got_object_commit_get_parent_ids(commit2);
3545 STAILQ_FOREACH(pid, parent_ids, entry) {
3546 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3547 err = write_commit_info(
3548 &s->line_offsets, &s->nlines,
3549 s->id2, refs, s->repo, s->f);
3550 if (err)
3551 goto done;
3552 break;
3556 got_object_commit_close(commit2);
3558 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3559 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3560 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3561 break;
3563 default:
3564 err = got_error(GOT_ERR_OBJ_TYPE);
3565 break;
3567 if (err)
3568 goto done;
3569 done:
3570 if (s->f && fflush(s->f) != 0 && err == NULL)
3571 err = got_error_from_errno("fflush");
3572 return err;
3575 static void
3576 diff_view_indicate_progress(struct tog_view *view)
3578 mvwaddstr(view->window, 0, 0, "diffing...");
3579 update_panels();
3580 doupdate();
3583 static const struct got_error *
3584 search_start_diff_view(struct tog_view *view)
3586 struct tog_diff_view_state *s = &view->state.diff;
3588 s->matched_line = 0;
3589 return NULL;
3592 static const struct got_error *
3593 search_next_diff_view(struct tog_view *view)
3595 struct tog_diff_view_state *s = &view->state.diff;
3596 const struct got_error *err = NULL;
3597 int lineno;
3598 char *exstr = NULL, *line = NULL;
3599 size_t linesize = 0;
3600 ssize_t linelen;
3602 if (!view->searching) {
3603 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3604 return NULL;
3607 if (s->matched_line) {
3608 if (view->searching == TOG_SEARCH_FORWARD)
3609 lineno = s->matched_line + 1;
3610 else
3611 lineno = s->matched_line - 1;
3612 } else
3613 lineno = s->first_displayed_line;
3615 while (1) {
3616 off_t offset;
3618 if (lineno <= 0 || lineno > s->nlines) {
3619 if (s->matched_line == 0) {
3620 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3621 break;
3624 if (view->searching == TOG_SEARCH_FORWARD)
3625 lineno = 1;
3626 else
3627 lineno = s->nlines;
3630 offset = s->line_offsets[lineno - 1];
3631 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3632 free(line);
3633 return got_error_from_errno("fseeko");
3635 linelen = getline(&line, &linesize, s->f);
3636 err = expand_tab(&exstr, line);
3637 if (err)
3638 break;
3639 if (linelen != -1 &&
3640 match_line(exstr, &view->regex, 1, &view->regmatch)) {
3641 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3642 s->matched_line = lineno;
3643 break;
3645 free(exstr);
3646 exstr = NULL;
3647 if (view->searching == TOG_SEARCH_FORWARD)
3648 lineno++;
3649 else
3650 lineno--;
3652 free(line);
3653 free(exstr);
3655 if (s->matched_line) {
3656 s->first_displayed_line = s->matched_line;
3657 s->selected_line = 1;
3660 return err;
3663 static const struct got_error *
3664 close_diff_view(struct tog_view *view)
3666 const struct got_error *err = NULL;
3667 struct tog_diff_view_state *s = &view->state.diff;
3669 free(s->id1);
3670 s->id1 = NULL;
3671 free(s->id2);
3672 s->id2 = NULL;
3673 if (s->f && fclose(s->f) == EOF)
3674 err = got_error_from_errno("fclose");
3675 s->f = NULL;
3676 if (s->f1 && fclose(s->f1) == EOF)
3677 err = got_error_from_errno("fclose");
3678 s->f1 = NULL;
3679 if (s->f2 && fclose(s->f2) == EOF)
3680 err = got_error_from_errno("fclose");
3681 s->f2 = NULL;
3682 free_colors(&s->colors);
3683 free(s->line_offsets);
3684 s->line_offsets = NULL;
3685 s->nlines = 0;
3686 return err;
3689 static const struct got_error *
3690 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3691 struct got_object_id *id2, const char *label1, const char *label2,
3692 int diff_context, int ignore_whitespace, int force_text_diff,
3693 struct tog_view *log_view, struct got_repository *repo)
3695 const struct got_error *err;
3696 struct tog_diff_view_state *s = &view->state.diff;
3698 memset(s, 0, sizeof(*s));
3700 if (id1 != NULL && id2 != NULL) {
3701 int type1, type2;
3702 err = got_object_get_type(&type1, repo, id1);
3703 if (err)
3704 return err;
3705 err = got_object_get_type(&type2, repo, id2);
3706 if (err)
3707 return err;
3709 if (type1 != type2)
3710 return got_error(GOT_ERR_OBJ_TYPE);
3712 s->first_displayed_line = 1;
3713 s->last_displayed_line = view->nlines;
3714 s->selected_line = 1;
3715 s->repo = repo;
3716 s->id1 = id1;
3717 s->id2 = id2;
3718 s->label1 = label1;
3719 s->label2 = label2;
3721 if (id1) {
3722 s->id1 = got_object_id_dup(id1);
3723 if (s->id1 == NULL)
3724 return got_error_from_errno("got_object_id_dup");
3725 s->f1 = got_opentemp();
3726 if (s->f1 == NULL) {
3727 err = got_error_from_errno("got_opentemp");
3728 goto done;
3730 } else
3731 s->id1 = NULL;
3733 s->id2 = got_object_id_dup(id2);
3734 if (s->id2 == NULL) {
3735 err = got_error_from_errno("got_object_id_dup");
3736 goto done;
3739 s->f2 = got_opentemp();
3740 if (s->f2 == NULL) {
3741 err = got_error_from_errno("got_opentemp");
3742 goto done;
3745 s->first_displayed_line = 1;
3746 s->last_displayed_line = view->nlines;
3747 s->diff_context = diff_context;
3748 s->ignore_whitespace = ignore_whitespace;
3749 s->force_text_diff = force_text_diff;
3750 s->log_view = log_view;
3751 s->repo = repo;
3753 STAILQ_INIT(&s->colors);
3754 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3755 err = add_color(&s->colors,
3756 "^-", TOG_COLOR_DIFF_MINUS,
3757 get_color_value("TOG_COLOR_DIFF_MINUS"));
3758 if (err)
3759 goto done;
3760 err = add_color(&s->colors, "^\\+",
3761 TOG_COLOR_DIFF_PLUS,
3762 get_color_value("TOG_COLOR_DIFF_PLUS"));
3763 if (err)
3764 goto done;
3765 err = add_color(&s->colors,
3766 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3767 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3768 if (err)
3769 goto done;
3771 err = add_color(&s->colors,
3772 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3773 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3774 get_color_value("TOG_COLOR_DIFF_META"));
3775 if (err)
3776 goto done;
3778 err = add_color(&s->colors,
3779 "^(from|via): ", TOG_COLOR_AUTHOR,
3780 get_color_value("TOG_COLOR_AUTHOR"));
3781 if (err)
3782 goto done;
3784 err = add_color(&s->colors,
3785 "^date: ", TOG_COLOR_DATE,
3786 get_color_value("TOG_COLOR_DATE"));
3787 if (err)
3788 goto done;
3791 if (log_view && view_is_splitscreen(view))
3792 show_log_view(log_view); /* draw vborder */
3793 diff_view_indicate_progress(view);
3795 err = create_diff(s);
3797 view->show = show_diff_view;
3798 view->input = input_diff_view;
3799 view->close = close_diff_view;
3800 view->search_start = search_start_diff_view;
3801 view->search_next = search_next_diff_view;
3802 done:
3803 if (err)
3804 close_diff_view(view);
3805 return err;
3808 static const struct got_error *
3809 show_diff_view(struct tog_view *view)
3811 const struct got_error *err;
3812 struct tog_diff_view_state *s = &view->state.diff;
3813 char *id_str1 = NULL, *id_str2, *header;
3814 const char *label1, *label2;
3816 if (s->id1) {
3817 err = got_object_id_str(&id_str1, s->id1);
3818 if (err)
3819 return err;
3820 label1 = s->label1 ? : id_str1;
3821 } else
3822 label1 = "/dev/null";
3824 err = got_object_id_str(&id_str2, s->id2);
3825 if (err)
3826 return err;
3827 label2 = s->label2 ? : id_str2;
3829 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3830 err = got_error_from_errno("asprintf");
3831 free(id_str1);
3832 free(id_str2);
3833 return err;
3835 free(id_str1);
3836 free(id_str2);
3838 err = draw_file(view, header);
3839 free(header);
3840 return err;
3843 static const struct got_error *
3844 set_selected_commit(struct tog_diff_view_state *s,
3845 struct commit_queue_entry *entry)
3847 const struct got_error *err;
3848 const struct got_object_id_queue *parent_ids;
3849 struct got_commit_object *selected_commit;
3850 struct got_object_qid *pid;
3852 free(s->id2);
3853 s->id2 = got_object_id_dup(entry->id);
3854 if (s->id2 == NULL)
3855 return got_error_from_errno("got_object_id_dup");
3857 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3858 if (err)
3859 return err;
3860 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3861 free(s->id1);
3862 pid = STAILQ_FIRST(parent_ids);
3863 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3864 got_object_commit_close(selected_commit);
3865 return NULL;
3868 static const struct got_error *
3869 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3871 const struct got_error *err = NULL;
3872 struct tog_diff_view_state *s = &view->state.diff;
3873 struct tog_log_view_state *ls;
3874 struct commit_queue_entry *old_selected_entry;
3875 char *line = NULL;
3876 size_t linesize = 0;
3877 ssize_t linelen;
3878 int i, nscroll = view->nlines - 1;
3880 switch (ch) {
3881 case '0':
3882 view->x = 0;
3883 break;
3884 case '$':
3885 view->x = MAX(view->maxx - view->ncols / 3, 0);
3886 break;
3887 case KEY_RIGHT:
3888 case 'l':
3889 if (view->x + view->ncols / 3 < view->maxx)
3890 view->x += 2; /* move two columns right */
3891 break;
3892 case KEY_LEFT:
3893 case 'h':
3894 view->x -= MIN(view->x, 2); /* move two columns back */
3895 break;
3896 case 'a':
3897 case 'w':
3898 if (ch == 'a')
3899 s->force_text_diff = !s->force_text_diff;
3900 if (ch == 'w')
3901 s->ignore_whitespace = !s->ignore_whitespace;
3902 wclear(view->window);
3903 s->first_displayed_line = 1;
3904 s->last_displayed_line = view->nlines;
3905 s->matched_line = 0;
3906 diff_view_indicate_progress(view);
3907 err = create_diff(s);
3908 break;
3909 case 'g':
3910 case KEY_HOME:
3911 s->first_displayed_line = 1;
3912 break;
3913 case 'G':
3914 case KEY_END:
3915 if (s->eof)
3916 break;
3918 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3919 s->eof = 1;
3920 break;
3921 case 'k':
3922 case KEY_UP:
3923 case CTRL('p'):
3924 if (s->first_displayed_line > 1)
3925 s->first_displayed_line--;
3926 break;
3927 case CTRL('u'):
3928 case 'u':
3929 nscroll /= 2;
3930 /* FALL THROUGH */
3931 case KEY_PPAGE:
3932 case CTRL('b'):
3933 if (s->first_displayed_line == 1)
3934 break;
3935 i = 0;
3936 while (i++ < nscroll && s->first_displayed_line > 1)
3937 s->first_displayed_line--;
3938 break;
3939 case 'j':
3940 case KEY_DOWN:
3941 case CTRL('n'):
3942 if (!s->eof)
3943 s->first_displayed_line++;
3944 break;
3945 case CTRL('d'):
3946 case 'd':
3947 nscroll /= 2;
3948 /* FALL THROUGH */
3949 case KEY_NPAGE:
3950 case CTRL('f'):
3951 case ' ':
3952 if (s->eof)
3953 break;
3954 i = 0;
3955 while (!s->eof && i++ < nscroll) {
3956 linelen = getline(&line, &linesize, s->f);
3957 s->first_displayed_line++;
3958 if (linelen == -1) {
3959 if (feof(s->f)) {
3960 s->eof = 1;
3961 } else
3962 err = got_ferror(s->f, GOT_ERR_IO);
3963 break;
3966 free(line);
3967 break;
3968 case '[':
3969 if (s->diff_context > 0) {
3970 s->diff_context--;
3971 s->matched_line = 0;
3972 diff_view_indicate_progress(view);
3973 err = create_diff(s);
3974 if (s->first_displayed_line + view->nlines - 1 >
3975 s->nlines) {
3976 s->first_displayed_line = 1;
3977 s->last_displayed_line = view->nlines;
3980 break;
3981 case ']':
3982 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3983 s->diff_context++;
3984 s->matched_line = 0;
3985 diff_view_indicate_progress(view);
3986 err = create_diff(s);
3988 break;
3989 case '<':
3990 case ',':
3991 if (s->log_view == NULL)
3992 break;
3993 ls = &s->log_view->state.log;
3994 old_selected_entry = ls->selected_entry;
3996 err = input_log_view(NULL, s->log_view, KEY_UP);
3997 if (err)
3998 break;
4000 if (old_selected_entry == ls->selected_entry)
4001 break;
4003 err = set_selected_commit(s, ls->selected_entry);
4004 if (err)
4005 break;
4007 s->first_displayed_line = 1;
4008 s->last_displayed_line = view->nlines;
4009 s->matched_line = 0;
4010 view->x = 0;
4012 diff_view_indicate_progress(view);
4013 err = create_diff(s);
4014 break;
4015 case '>':
4016 case '.':
4017 if (s->log_view == NULL)
4018 break;
4019 ls = &s->log_view->state.log;
4020 old_selected_entry = ls->selected_entry;
4022 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4023 if (err)
4024 break;
4026 if (old_selected_entry == ls->selected_entry)
4027 break;
4029 err = set_selected_commit(s, ls->selected_entry);
4030 if (err)
4031 break;
4033 s->first_displayed_line = 1;
4034 s->last_displayed_line = view->nlines;
4035 s->matched_line = 0;
4036 view->x = 0;
4038 diff_view_indicate_progress(view);
4039 err = create_diff(s);
4040 break;
4041 default:
4042 break;
4045 return err;
4048 static const struct got_error *
4049 cmd_diff(int argc, char *argv[])
4051 const struct got_error *error = NULL;
4052 struct got_repository *repo = NULL;
4053 struct got_worktree *worktree = NULL;
4054 struct got_object_id *id1 = NULL, *id2 = NULL;
4055 char *repo_path = NULL, *cwd = NULL;
4056 char *id_str1 = NULL, *id_str2 = NULL;
4057 char *label1 = NULL, *label2 = NULL;
4058 int diff_context = 3, ignore_whitespace = 0;
4059 int ch, force_text_diff = 0;
4060 const char *errstr;
4061 struct tog_view *view;
4062 int *pack_fds = NULL;
4064 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4065 switch (ch) {
4066 case 'a':
4067 force_text_diff = 1;
4068 break;
4069 case 'C':
4070 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4071 &errstr);
4072 if (errstr != NULL)
4073 errx(1, "number of context lines is %s: %s",
4074 errstr, errstr);
4075 break;
4076 case 'r':
4077 repo_path = realpath(optarg, NULL);
4078 if (repo_path == NULL)
4079 return got_error_from_errno2("realpath",
4080 optarg);
4081 got_path_strip_trailing_slashes(repo_path);
4082 break;
4083 case 'w':
4084 ignore_whitespace = 1;
4085 break;
4086 default:
4087 usage_diff();
4088 /* NOTREACHED */
4092 argc -= optind;
4093 argv += optind;
4095 if (argc == 0) {
4096 usage_diff(); /* TODO show local worktree changes */
4097 } else if (argc == 2) {
4098 id_str1 = argv[0];
4099 id_str2 = argv[1];
4100 } else
4101 usage_diff();
4103 error = got_repo_pack_fds_open(&pack_fds);
4104 if (error)
4105 goto done;
4107 if (repo_path == NULL) {
4108 cwd = getcwd(NULL, 0);
4109 if (cwd == NULL)
4110 return got_error_from_errno("getcwd");
4111 error = got_worktree_open(&worktree, cwd);
4112 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4113 goto done;
4114 if (worktree)
4115 repo_path =
4116 strdup(got_worktree_get_repo_path(worktree));
4117 else
4118 repo_path = strdup(cwd);
4119 if (repo_path == NULL) {
4120 error = got_error_from_errno("strdup");
4121 goto done;
4125 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4126 if (error)
4127 goto done;
4129 init_curses();
4131 error = apply_unveil(got_repo_get_path(repo), NULL);
4132 if (error)
4133 goto done;
4135 error = tog_load_refs(repo, 0);
4136 if (error)
4137 goto done;
4139 error = got_repo_match_object_id(&id1, &label1, id_str1,
4140 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4141 if (error)
4142 goto done;
4144 error = got_repo_match_object_id(&id2, &label2, id_str2,
4145 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4146 if (error)
4147 goto done;
4149 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4150 if (view == NULL) {
4151 error = got_error_from_errno("view_open");
4152 goto done;
4154 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4155 ignore_whitespace, force_text_diff, NULL, repo);
4156 if (error)
4157 goto done;
4158 error = view_loop(view);
4159 done:
4160 free(label1);
4161 free(label2);
4162 free(repo_path);
4163 free(cwd);
4164 if (repo) {
4165 const struct got_error *close_err = got_repo_close(repo);
4166 if (error == NULL)
4167 error = close_err;
4169 if (worktree)
4170 got_worktree_close(worktree);
4171 if (pack_fds) {
4172 const struct got_error *pack_err =
4173 got_repo_pack_fds_close(pack_fds);
4174 if (error == NULL)
4175 error = pack_err;
4177 tog_free_refs();
4178 return error;
4181 __dead static void
4182 usage_blame(void)
4184 endwin();
4185 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4186 getprogname());
4187 exit(1);
4190 struct tog_blame_line {
4191 int annotated;
4192 struct got_object_id *id;
4195 static const struct got_error *
4196 draw_blame(struct tog_view *view)
4198 struct tog_blame_view_state *s = &view->state.blame;
4199 struct tog_blame *blame = &s->blame;
4200 regmatch_t *regmatch = &view->regmatch;
4201 const struct got_error *err;
4202 int lineno = 0, nprinted = 0, i;
4203 char *line = NULL;
4204 size_t linesize = 0;
4205 ssize_t linelen;
4206 wchar_t *wline;
4207 int width;
4208 struct tog_blame_line *blame_line;
4209 struct got_object_id *prev_id = NULL;
4210 char *id_str;
4211 struct tog_color *tc;
4213 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4214 if (err)
4215 return err;
4217 rewind(blame->f);
4218 werase(view->window);
4220 if (asprintf(&line, "commit %s", id_str) == -1) {
4221 err = got_error_from_errno("asprintf");
4222 free(id_str);
4223 return err;
4226 err = format_line(&wline, &width, line, view->ncols, 0, 0);
4227 free(line);
4228 line = NULL;
4229 if (err)
4230 return err;
4231 if (view_needs_focus_indication(view))
4232 wstandout(view->window);
4233 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4234 if (tc)
4235 wattr_on(view->window,
4236 COLOR_PAIR(tc->colorpair), NULL);
4237 waddwstr(view->window, wline);
4238 if (tc)
4239 wattr_off(view->window,
4240 COLOR_PAIR(tc->colorpair), NULL);
4241 if (view_needs_focus_indication(view))
4242 wstandend(view->window);
4243 free(wline);
4244 wline = NULL;
4245 if (width < view->ncols - 1)
4246 waddch(view->window, '\n');
4248 if (asprintf(&line, "[%d/%d] %s%s",
4249 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4250 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4251 free(id_str);
4252 return got_error_from_errno("asprintf");
4254 free(id_str);
4255 err = format_line(&wline, &width, line, view->ncols, 0, 0);
4256 free(line);
4257 line = NULL;
4258 if (err)
4259 return err;
4260 waddwstr(view->window, wline);
4261 free(wline);
4262 wline = NULL;
4263 if (width < view->ncols - 1)
4264 waddch(view->window, '\n');
4266 s->eof = 0;
4267 view->maxx = 0;
4268 while (nprinted < view->nlines - 2) {
4269 linelen = getline(&line, &linesize, blame->f);
4270 if (linelen == -1) {
4271 if (feof(blame->f)) {
4272 s->eof = 1;
4273 break;
4275 free(line);
4276 return got_ferror(blame->f, GOT_ERR_IO);
4278 if (++lineno < s->first_displayed_line)
4279 continue;
4281 view->maxx = MAX(view->maxx, linelen);
4283 if (view->focussed && nprinted == s->selected_line - 1)
4284 wstandout(view->window);
4286 if (blame->nlines > 0) {
4287 blame_line = &blame->lines[lineno - 1];
4288 if (blame_line->annotated && prev_id &&
4289 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4290 !(view->focussed &&
4291 nprinted == s->selected_line - 1)) {
4292 waddstr(view->window, " ");
4293 } else if (blame_line->annotated) {
4294 char *id_str;
4295 err = got_object_id_str(&id_str, blame_line->id);
4296 if (err) {
4297 free(line);
4298 return err;
4300 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4301 if (tc)
4302 wattr_on(view->window,
4303 COLOR_PAIR(tc->colorpair), NULL);
4304 wprintw(view->window, "%.8s", id_str);
4305 if (tc)
4306 wattr_off(view->window,
4307 COLOR_PAIR(tc->colorpair), NULL);
4308 free(id_str);
4309 prev_id = blame_line->id;
4310 } else {
4311 waddstr(view->window, "........");
4312 prev_id = NULL;
4314 } else {
4315 waddstr(view->window, "........");
4316 prev_id = NULL;
4319 if (view->focussed && nprinted == s->selected_line - 1)
4320 wstandend(view->window);
4321 waddstr(view->window, " ");
4323 if (view->ncols <= 9) {
4324 width = 9;
4325 wline = wcsdup(L"");
4326 if (wline == NULL) {
4327 err = got_error_from_errno("wcsdup");
4328 free(line);
4329 return err;
4331 } else if (s->first_displayed_line + nprinted ==
4332 s->matched_line &&
4333 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4334 err = add_matched_line(&width, line, view->ncols - 9, 9,
4335 view->window, view->x, regmatch);
4336 if (err) {
4337 free(line);
4338 return err;
4340 width += 9;
4341 } else {
4342 err = format_line(&wline, &width, line,
4343 view->x + view->ncols - 9, 9, 1);
4344 if (err) {
4345 free(line);
4346 return err;
4348 if (view->x < width) {
4349 waddwstr(view->window, wline + view->x);
4350 for (i = 0; i < view->x; i++)
4351 width -= wcwidth(wline[i]);
4353 width += 9;
4354 free(wline);
4355 wline = NULL;
4358 if (width <= view->ncols - 1)
4359 waddch(view->window, '\n');
4360 if (++nprinted == 1)
4361 s->first_displayed_line = lineno;
4363 free(line);
4364 s->last_displayed_line = lineno;
4366 view_vborder(view);
4368 return NULL;
4371 static const struct got_error *
4372 blame_cb(void *arg, int nlines, int lineno,
4373 struct got_commit_object *commit, struct got_object_id *id)
4375 const struct got_error *err = NULL;
4376 struct tog_blame_cb_args *a = arg;
4377 struct tog_blame_line *line;
4378 int errcode;
4380 if (nlines != a->nlines ||
4381 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4382 return got_error(GOT_ERR_RANGE);
4384 errcode = pthread_mutex_lock(&tog_mutex);
4385 if (errcode)
4386 return got_error_set_errno(errcode, "pthread_mutex_lock");
4388 if (*a->quit) { /* user has quit the blame view */
4389 err = got_error(GOT_ERR_ITER_COMPLETED);
4390 goto done;
4393 if (lineno == -1)
4394 goto done; /* no change in this commit */
4396 line = &a->lines[lineno - 1];
4397 if (line->annotated)
4398 goto done;
4400 line->id = got_object_id_dup(id);
4401 if (line->id == NULL) {
4402 err = got_error_from_errno("got_object_id_dup");
4403 goto done;
4405 line->annotated = 1;
4406 done:
4407 errcode = pthread_mutex_unlock(&tog_mutex);
4408 if (errcode)
4409 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4410 return err;
4413 static void *
4414 blame_thread(void *arg)
4416 const struct got_error *err, *close_err;
4417 struct tog_blame_thread_args *ta = arg;
4418 struct tog_blame_cb_args *a = ta->cb_args;
4419 int errcode;
4421 err = block_signals_used_by_main_thread();
4422 if (err)
4423 return (void *)err;
4425 err = got_blame(ta->path, a->commit_id, ta->repo,
4426 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4427 if (err && err->code == GOT_ERR_CANCELLED)
4428 err = NULL;
4430 errcode = pthread_mutex_lock(&tog_mutex);
4431 if (errcode)
4432 return (void *)got_error_set_errno(errcode,
4433 "pthread_mutex_lock");
4435 close_err = got_repo_close(ta->repo);
4436 if (err == NULL)
4437 err = close_err;
4438 ta->repo = NULL;
4439 *ta->complete = 1;
4441 errcode = pthread_mutex_unlock(&tog_mutex);
4442 if (errcode && err == NULL)
4443 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4445 return (void *)err;
4448 static struct got_object_id *
4449 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4450 int first_displayed_line, int selected_line)
4452 struct tog_blame_line *line;
4454 if (nlines <= 0)
4455 return NULL;
4457 line = &lines[first_displayed_line - 1 + selected_line - 1];
4458 if (!line->annotated)
4459 return NULL;
4461 return line->id;
4464 static const struct got_error *
4465 stop_blame(struct tog_blame *blame)
4467 const struct got_error *err = NULL;
4468 int i;
4470 if (blame->thread) {
4471 int errcode;
4472 errcode = pthread_mutex_unlock(&tog_mutex);
4473 if (errcode)
4474 return got_error_set_errno(errcode,
4475 "pthread_mutex_unlock");
4476 errcode = pthread_join(blame->thread, (void **)&err);
4477 if (errcode)
4478 return got_error_set_errno(errcode, "pthread_join");
4479 errcode = pthread_mutex_lock(&tog_mutex);
4480 if (errcode)
4481 return got_error_set_errno(errcode,
4482 "pthread_mutex_lock");
4483 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4484 err = NULL;
4485 blame->thread = 0; //NULL;
4487 if (blame->thread_args.repo) {
4488 const struct got_error *close_err;
4489 close_err = got_repo_close(blame->thread_args.repo);
4490 if (err == NULL)
4491 err = close_err;
4492 blame->thread_args.repo = NULL;
4494 if (blame->f) {
4495 if (fclose(blame->f) == EOF && err == NULL)
4496 err = got_error_from_errno("fclose");
4497 blame->f = NULL;
4499 if (blame->lines) {
4500 for (i = 0; i < blame->nlines; i++)
4501 free(blame->lines[i].id);
4502 free(blame->lines);
4503 blame->lines = NULL;
4505 free(blame->cb_args.commit_id);
4506 blame->cb_args.commit_id = NULL;
4507 if (blame->pack_fds) {
4508 const struct got_error *pack_err =
4509 got_repo_pack_fds_close(blame->pack_fds);
4510 if (err == NULL)
4511 err = pack_err;
4512 blame->pack_fds = NULL;
4514 return err;
4517 static const struct got_error *
4518 cancel_blame_view(void *arg)
4520 const struct got_error *err = NULL;
4521 int *done = arg;
4522 int errcode;
4524 errcode = pthread_mutex_lock(&tog_mutex);
4525 if (errcode)
4526 return got_error_set_errno(errcode,
4527 "pthread_mutex_unlock");
4529 if (*done)
4530 err = got_error(GOT_ERR_CANCELLED);
4532 errcode = pthread_mutex_unlock(&tog_mutex);
4533 if (errcode)
4534 return got_error_set_errno(errcode,
4535 "pthread_mutex_lock");
4537 return err;
4540 static const struct got_error *
4541 run_blame(struct tog_view *view)
4543 struct tog_blame_view_state *s = &view->state.blame;
4544 struct tog_blame *blame = &s->blame;
4545 const struct got_error *err = NULL;
4546 struct got_commit_object *commit = NULL;
4547 struct got_blob_object *blob = NULL;
4548 struct got_repository *thread_repo = NULL;
4549 struct got_object_id *obj_id = NULL;
4550 int obj_type;
4551 int *pack_fds = NULL;
4553 err = got_object_open_as_commit(&commit, s->repo,
4554 &s->blamed_commit->id);
4555 if (err)
4556 return err;
4558 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4559 if (err)
4560 goto done;
4562 err = got_object_get_type(&obj_type, s->repo, obj_id);
4563 if (err)
4564 goto done;
4566 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4567 err = got_error(GOT_ERR_OBJ_TYPE);
4568 goto done;
4571 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4572 if (err)
4573 goto done;
4574 blame->f = got_opentemp();
4575 if (blame->f == NULL) {
4576 err = got_error_from_errno("got_opentemp");
4577 goto done;
4579 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4580 &blame->line_offsets, blame->f, blob);
4581 if (err)
4582 goto done;
4583 if (blame->nlines == 0) {
4584 s->blame_complete = 1;
4585 goto done;
4588 /* Don't include \n at EOF in the blame line count. */
4589 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4590 blame->nlines--;
4592 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4593 if (blame->lines == NULL) {
4594 err = got_error_from_errno("calloc");
4595 goto done;
4598 err = got_repo_pack_fds_open(&pack_fds);
4599 if (err)
4600 goto done;
4601 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4602 pack_fds);
4603 if (err)
4604 goto done;
4606 blame->pack_fds = pack_fds;
4607 blame->cb_args.view = view;
4608 blame->cb_args.lines = blame->lines;
4609 blame->cb_args.nlines = blame->nlines;
4610 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4611 if (blame->cb_args.commit_id == NULL) {
4612 err = got_error_from_errno("got_object_id_dup");
4613 goto done;
4615 blame->cb_args.quit = &s->done;
4617 blame->thread_args.path = s->path;
4618 blame->thread_args.repo = thread_repo;
4619 blame->thread_args.cb_args = &blame->cb_args;
4620 blame->thread_args.complete = &s->blame_complete;
4621 blame->thread_args.cancel_cb = cancel_blame_view;
4622 blame->thread_args.cancel_arg = &s->done;
4623 s->blame_complete = 0;
4625 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4626 s->first_displayed_line = 1;
4627 s->last_displayed_line = view->nlines;
4628 s->selected_line = 1;
4630 s->matched_line = 0;
4632 done:
4633 if (commit)
4634 got_object_commit_close(commit);
4635 if (blob)
4636 got_object_blob_close(blob);
4637 free(obj_id);
4638 if (err)
4639 stop_blame(blame);
4640 return err;
4643 static const struct got_error *
4644 open_blame_view(struct tog_view *view, char *path,
4645 struct got_object_id *commit_id, struct got_repository *repo)
4647 const struct got_error *err = NULL;
4648 struct tog_blame_view_state *s = &view->state.blame;
4650 STAILQ_INIT(&s->blamed_commits);
4652 s->path = strdup(path);
4653 if (s->path == NULL)
4654 return got_error_from_errno("strdup");
4656 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4657 if (err) {
4658 free(s->path);
4659 return err;
4662 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4663 s->first_displayed_line = 1;
4664 s->last_displayed_line = view->nlines;
4665 s->selected_line = 1;
4666 s->blame_complete = 0;
4667 s->repo = repo;
4668 s->commit_id = commit_id;
4669 memset(&s->blame, 0, sizeof(s->blame));
4671 STAILQ_INIT(&s->colors);
4672 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4673 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4674 get_color_value("TOG_COLOR_COMMIT"));
4675 if (err)
4676 return err;
4679 view->show = show_blame_view;
4680 view->input = input_blame_view;
4681 view->close = close_blame_view;
4682 view->search_start = search_start_blame_view;
4683 view->search_next = search_next_blame_view;
4685 return run_blame(view);
4688 static const struct got_error *
4689 close_blame_view(struct tog_view *view)
4691 const struct got_error *err = NULL;
4692 struct tog_blame_view_state *s = &view->state.blame;
4694 if (s->blame.thread)
4695 err = stop_blame(&s->blame);
4697 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4698 struct got_object_qid *blamed_commit;
4699 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4700 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4701 got_object_qid_free(blamed_commit);
4704 free(s->path);
4705 free_colors(&s->colors);
4706 return err;
4709 static const struct got_error *
4710 search_start_blame_view(struct tog_view *view)
4712 struct tog_blame_view_state *s = &view->state.blame;
4714 s->matched_line = 0;
4715 return NULL;
4718 static const struct got_error *
4719 search_next_blame_view(struct tog_view *view)
4721 struct tog_blame_view_state *s = &view->state.blame;
4722 const struct got_error *err = NULL;
4723 int lineno;
4724 char *exstr = NULL, *line = NULL;
4725 size_t linesize = 0;
4726 ssize_t linelen;
4728 if (!view->searching) {
4729 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4730 return NULL;
4733 if (s->matched_line) {
4734 if (view->searching == TOG_SEARCH_FORWARD)
4735 lineno = s->matched_line + 1;
4736 else
4737 lineno = s->matched_line - 1;
4738 } else
4739 lineno = s->first_displayed_line - 1 + s->selected_line;
4741 while (1) {
4742 off_t offset;
4744 if (lineno <= 0 || lineno > s->blame.nlines) {
4745 if (s->matched_line == 0) {
4746 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4747 break;
4750 if (view->searching == TOG_SEARCH_FORWARD)
4751 lineno = 1;
4752 else
4753 lineno = s->blame.nlines;
4756 offset = s->blame.line_offsets[lineno - 1];
4757 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4758 free(line);
4759 return got_error_from_errno("fseeko");
4761 linelen = getline(&line, &linesize, s->blame.f);
4762 err = expand_tab(&exstr, line);
4763 if (err)
4764 break;
4765 if (linelen != -1 &&
4766 match_line(exstr, &view->regex, 1, &view->regmatch)) {
4767 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4768 s->matched_line = lineno;
4769 break;
4771 free(exstr);
4772 exstr = NULL;
4773 if (view->searching == TOG_SEARCH_FORWARD)
4774 lineno++;
4775 else
4776 lineno--;
4778 free(line);
4779 free(exstr);
4781 if (s->matched_line) {
4782 s->first_displayed_line = s->matched_line;
4783 s->selected_line = 1;
4786 return err;
4789 static const struct got_error *
4790 show_blame_view(struct tog_view *view)
4792 const struct got_error *err = NULL;
4793 struct tog_blame_view_state *s = &view->state.blame;
4794 int errcode;
4796 if (s->blame.thread == 0 && !s->blame_complete) {
4797 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4798 &s->blame.thread_args);
4799 if (errcode)
4800 return got_error_set_errno(errcode, "pthread_create");
4802 halfdelay(1); /* fast refresh while annotating */
4805 if (s->blame_complete)
4806 halfdelay(10); /* disable fast refresh */
4808 err = draw_blame(view);
4810 view_vborder(view);
4811 return err;
4814 static const struct got_error *
4815 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4817 const struct got_error *err = NULL, *thread_err = NULL;
4818 struct tog_view *diff_view;
4819 struct tog_blame_view_state *s = &view->state.blame;
4820 int begin_x = 0, nscroll = view->nlines - 2;
4822 switch (ch) {
4823 case '0':
4824 view->x = 0;
4825 break;
4826 case '$':
4827 view->x = MAX(view->maxx - view->ncols / 3, 0);
4828 break;
4829 case KEY_RIGHT:
4830 case 'l':
4831 if (view->x + view->ncols / 3 < view->maxx)
4832 view->x += 2; /* move two columns right */
4833 break;
4834 case KEY_LEFT:
4835 case 'h':
4836 view->x -= MIN(view->x, 2); /* move two columns back */
4837 break;
4838 case 'q':
4839 s->done = 1;
4840 break;
4841 case 'g':
4842 case KEY_HOME:
4843 s->selected_line = 1;
4844 s->first_displayed_line = 1;
4845 break;
4846 case 'G':
4847 case KEY_END:
4848 if (s->blame.nlines < view->nlines - 2) {
4849 s->selected_line = s->blame.nlines;
4850 s->first_displayed_line = 1;
4851 } else {
4852 s->selected_line = view->nlines - 2;
4853 s->first_displayed_line = s->blame.nlines -
4854 (view->nlines - 3);
4856 break;
4857 case 'k':
4858 case KEY_UP:
4859 case CTRL('p'):
4860 if (s->selected_line > 1)
4861 s->selected_line--;
4862 else if (s->selected_line == 1 &&
4863 s->first_displayed_line > 1)
4864 s->first_displayed_line--;
4865 break;
4866 case CTRL('u'):
4867 case 'u':
4868 nscroll /= 2;
4869 /* FALL THROUGH */
4870 case KEY_PPAGE:
4871 case CTRL('b'):
4872 if (s->first_displayed_line == 1) {
4873 s->selected_line = MAX(1, s->selected_line - nscroll);
4874 break;
4876 if (s->first_displayed_line > nscroll)
4877 s->first_displayed_line -= nscroll;
4878 else
4879 s->first_displayed_line = 1;
4880 break;
4881 case 'j':
4882 case KEY_DOWN:
4883 case CTRL('n'):
4884 if (s->selected_line < view->nlines - 2 &&
4885 s->first_displayed_line +
4886 s->selected_line <= s->blame.nlines)
4887 s->selected_line++;
4888 else if (s->last_displayed_line <
4889 s->blame.nlines)
4890 s->first_displayed_line++;
4891 break;
4892 case 'b':
4893 case 'p': {
4894 struct got_object_id *id = NULL;
4895 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4896 s->first_displayed_line, s->selected_line);
4897 if (id == NULL)
4898 break;
4899 if (ch == 'p') {
4900 struct got_commit_object *commit, *pcommit;
4901 struct got_object_qid *pid;
4902 struct got_object_id *blob_id = NULL;
4903 int obj_type;
4904 err = got_object_open_as_commit(&commit,
4905 s->repo, id);
4906 if (err)
4907 break;
4908 pid = STAILQ_FIRST(
4909 got_object_commit_get_parent_ids(commit));
4910 if (pid == NULL) {
4911 got_object_commit_close(commit);
4912 break;
4914 /* Check if path history ends here. */
4915 err = got_object_open_as_commit(&pcommit,
4916 s->repo, &pid->id);
4917 if (err)
4918 break;
4919 err = got_object_id_by_path(&blob_id, s->repo,
4920 pcommit, s->path);
4921 got_object_commit_close(pcommit);
4922 if (err) {
4923 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4924 err = NULL;
4925 got_object_commit_close(commit);
4926 break;
4928 err = got_object_get_type(&obj_type, s->repo,
4929 blob_id);
4930 free(blob_id);
4931 /* Can't blame non-blob type objects. */
4932 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4933 got_object_commit_close(commit);
4934 break;
4936 err = got_object_qid_alloc(&s->blamed_commit,
4937 &pid->id);
4938 got_object_commit_close(commit);
4939 } else {
4940 if (got_object_id_cmp(id,
4941 &s->blamed_commit->id) == 0)
4942 break;
4943 err = got_object_qid_alloc(&s->blamed_commit,
4944 id);
4946 if (err)
4947 break;
4948 s->done = 1;
4949 thread_err = stop_blame(&s->blame);
4950 s->done = 0;
4951 if (thread_err)
4952 break;
4953 STAILQ_INSERT_HEAD(&s->blamed_commits,
4954 s->blamed_commit, entry);
4955 err = run_blame(view);
4956 if (err)
4957 break;
4958 break;
4960 case 'B': {
4961 struct got_object_qid *first;
4962 first = STAILQ_FIRST(&s->blamed_commits);
4963 if (!got_object_id_cmp(&first->id, s->commit_id))
4964 break;
4965 s->done = 1;
4966 thread_err = stop_blame(&s->blame);
4967 s->done = 0;
4968 if (thread_err)
4969 break;
4970 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4971 got_object_qid_free(s->blamed_commit);
4972 s->blamed_commit =
4973 STAILQ_FIRST(&s->blamed_commits);
4974 err = run_blame(view);
4975 if (err)
4976 break;
4977 break;
4979 case KEY_ENTER:
4980 case '\r': {
4981 struct got_object_id *id = NULL;
4982 struct got_object_qid *pid;
4983 struct got_commit_object *commit = NULL;
4984 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4985 s->first_displayed_line, s->selected_line);
4986 if (id == NULL)
4987 break;
4988 err = got_object_open_as_commit(&commit, s->repo, id);
4989 if (err)
4990 break;
4991 pid = STAILQ_FIRST(
4992 got_object_commit_get_parent_ids(commit));
4993 if (view_is_parent_view(view))
4994 begin_x = view_split_begin_x(view->begin_x);
4995 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4996 if (diff_view == NULL) {
4997 got_object_commit_close(commit);
4998 err = got_error_from_errno("view_open");
4999 break;
5001 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5002 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5003 got_object_commit_close(commit);
5004 if (err) {
5005 view_close(diff_view);
5006 break;
5008 view->focussed = 0;
5009 diff_view->focussed = 1;
5010 if (view_is_parent_view(view)) {
5011 err = view_close_child(view);
5012 if (err)
5013 break;
5014 view_set_child(view, diff_view);
5015 view->focus_child = 1;
5016 } else
5017 *new_view = diff_view;
5018 if (err)
5019 break;
5020 break;
5022 case CTRL('d'):
5023 case 'd':
5024 nscroll /= 2;
5025 /* FALL THROUGH */
5026 case KEY_NPAGE:
5027 case CTRL('f'):
5028 case ' ':
5029 if (s->last_displayed_line >= s->blame.nlines &&
5030 s->selected_line >= MIN(s->blame.nlines,
5031 view->nlines - 2)) {
5032 break;
5034 if (s->last_displayed_line >= s->blame.nlines &&
5035 s->selected_line < view->nlines - 2) {
5036 s->selected_line +=
5037 MIN(nscroll, s->last_displayed_line -
5038 s->first_displayed_line - s->selected_line + 1);
5040 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5041 s->first_displayed_line += nscroll;
5042 else
5043 s->first_displayed_line =
5044 s->blame.nlines - (view->nlines - 3);
5045 break;
5046 case KEY_RESIZE:
5047 if (s->selected_line > view->nlines - 2) {
5048 s->selected_line = MIN(s->blame.nlines,
5049 view->nlines - 2);
5051 break;
5052 default:
5053 break;
5055 return thread_err ? thread_err : err;
5058 static const struct got_error *
5059 cmd_blame(int argc, char *argv[])
5061 const struct got_error *error;
5062 struct got_repository *repo = NULL;
5063 struct got_worktree *worktree = NULL;
5064 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5065 char *link_target = NULL;
5066 struct got_object_id *commit_id = NULL;
5067 struct got_commit_object *commit = NULL;
5068 char *commit_id_str = NULL;
5069 int ch;
5070 struct tog_view *view;
5071 int *pack_fds = NULL;
5073 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5074 switch (ch) {
5075 case 'c':
5076 commit_id_str = optarg;
5077 break;
5078 case 'r':
5079 repo_path = realpath(optarg, NULL);
5080 if (repo_path == NULL)
5081 return got_error_from_errno2("realpath",
5082 optarg);
5083 break;
5084 default:
5085 usage_blame();
5086 /* NOTREACHED */
5090 argc -= optind;
5091 argv += optind;
5093 if (argc != 1)
5094 usage_blame();
5096 error = got_repo_pack_fds_open(&pack_fds);
5097 if (error != NULL)
5098 goto done;
5100 if (repo_path == NULL) {
5101 cwd = getcwd(NULL, 0);
5102 if (cwd == NULL)
5103 return got_error_from_errno("getcwd");
5104 error = got_worktree_open(&worktree, cwd);
5105 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5106 goto done;
5107 if (worktree)
5108 repo_path =
5109 strdup(got_worktree_get_repo_path(worktree));
5110 else
5111 repo_path = strdup(cwd);
5112 if (repo_path == NULL) {
5113 error = got_error_from_errno("strdup");
5114 goto done;
5118 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5119 if (error != NULL)
5120 goto done;
5122 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5123 worktree);
5124 if (error)
5125 goto done;
5127 init_curses();
5129 error = apply_unveil(got_repo_get_path(repo), NULL);
5130 if (error)
5131 goto done;
5133 error = tog_load_refs(repo, 0);
5134 if (error)
5135 goto done;
5137 if (commit_id_str == NULL) {
5138 struct got_reference *head_ref;
5139 error = got_ref_open(&head_ref, repo, worktree ?
5140 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5141 if (error != NULL)
5142 goto done;
5143 error = got_ref_resolve(&commit_id, repo, head_ref);
5144 got_ref_close(head_ref);
5145 } else {
5146 error = got_repo_match_object_id(&commit_id, NULL,
5147 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5149 if (error != NULL)
5150 goto done;
5152 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5153 if (view == NULL) {
5154 error = got_error_from_errno("view_open");
5155 goto done;
5158 error = got_object_open_as_commit(&commit, repo, commit_id);
5159 if (error)
5160 goto done;
5162 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5163 commit, repo);
5164 if (error)
5165 goto done;
5167 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5168 commit_id, repo);
5169 if (error)
5170 goto done;
5171 if (worktree) {
5172 /* Release work tree lock. */
5173 got_worktree_close(worktree);
5174 worktree = NULL;
5176 error = view_loop(view);
5177 done:
5178 free(repo_path);
5179 free(in_repo_path);
5180 free(link_target);
5181 free(cwd);
5182 free(commit_id);
5183 if (commit)
5184 got_object_commit_close(commit);
5185 if (worktree)
5186 got_worktree_close(worktree);
5187 if (repo) {
5188 const struct got_error *close_err = got_repo_close(repo);
5189 if (error == NULL)
5190 error = close_err;
5192 if (pack_fds) {
5193 const struct got_error *pack_err =
5194 got_repo_pack_fds_close(pack_fds);
5195 if (error == NULL)
5196 error = pack_err;
5198 tog_free_refs();
5199 return error;
5202 static const struct got_error *
5203 draw_tree_entries(struct tog_view *view, const char *parent_path)
5205 struct tog_tree_view_state *s = &view->state.tree;
5206 const struct got_error *err = NULL;
5207 struct got_tree_entry *te;
5208 wchar_t *wline;
5209 struct tog_color *tc;
5210 int width, n, i, nentries;
5211 int limit = view->nlines;
5213 s->ndisplayed = 0;
5215 werase(view->window);
5217 if (limit == 0)
5218 return NULL;
5220 err = format_line(&wline, &width, s->tree_label, view->ncols, 0, 0);
5221 if (err)
5222 return err;
5223 if (view_needs_focus_indication(view))
5224 wstandout(view->window);
5225 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5226 if (tc)
5227 wattr_on(view->window,
5228 COLOR_PAIR(tc->colorpair), NULL);
5229 waddwstr(view->window, wline);
5230 if (tc)
5231 wattr_off(view->window,
5232 COLOR_PAIR(tc->colorpair), NULL);
5233 if (view_needs_focus_indication(view))
5234 wstandend(view->window);
5235 free(wline);
5236 wline = NULL;
5237 if (width < view->ncols - 1)
5238 waddch(view->window, '\n');
5239 if (--limit <= 0)
5240 return NULL;
5241 err = format_line(&wline, &width, parent_path, view->ncols, 0, 0);
5242 if (err)
5243 return err;
5244 waddwstr(view->window, wline);
5245 free(wline);
5246 wline = NULL;
5247 if (width < view->ncols - 1)
5248 waddch(view->window, '\n');
5249 if (--limit <= 0)
5250 return NULL;
5251 waddch(view->window, '\n');
5252 if (--limit <= 0)
5253 return NULL;
5255 if (s->first_displayed_entry == NULL) {
5256 te = got_object_tree_get_first_entry(s->tree);
5257 if (s->selected == 0) {
5258 if (view->focussed)
5259 wstandout(view->window);
5260 s->selected_entry = NULL;
5262 waddstr(view->window, " ..\n"); /* parent directory */
5263 if (s->selected == 0 && view->focussed)
5264 wstandend(view->window);
5265 s->ndisplayed++;
5266 if (--limit <= 0)
5267 return NULL;
5268 n = 1;
5269 } else {
5270 n = 0;
5271 te = s->first_displayed_entry;
5274 nentries = got_object_tree_get_nentries(s->tree);
5275 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5276 char *line = NULL, *id_str = NULL, *link_target = NULL;
5277 const char *modestr = "";
5278 mode_t mode;
5280 te = got_object_tree_get_entry(s->tree, i);
5281 mode = got_tree_entry_get_mode(te);
5283 if (s->show_ids) {
5284 err = got_object_id_str(&id_str,
5285 got_tree_entry_get_id(te));
5286 if (err)
5287 return got_error_from_errno(
5288 "got_object_id_str");
5290 if (got_object_tree_entry_is_submodule(te))
5291 modestr = "$";
5292 else if (S_ISLNK(mode)) {
5293 int i;
5295 err = got_tree_entry_get_symlink_target(&link_target,
5296 te, s->repo);
5297 if (err) {
5298 free(id_str);
5299 return err;
5301 for (i = 0; i < strlen(link_target); i++) {
5302 if (!isprint((unsigned char)link_target[i]))
5303 link_target[i] = '?';
5305 modestr = "@";
5307 else if (S_ISDIR(mode))
5308 modestr = "/";
5309 else if (mode & S_IXUSR)
5310 modestr = "*";
5311 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5312 got_tree_entry_get_name(te), modestr,
5313 link_target ? " -> ": "",
5314 link_target ? link_target : "") == -1) {
5315 free(id_str);
5316 free(link_target);
5317 return got_error_from_errno("asprintf");
5319 free(id_str);
5320 free(link_target);
5321 err = format_line(&wline, &width, line, view->ncols, 0, 0);
5322 if (err) {
5323 free(line);
5324 break;
5326 if (n == s->selected) {
5327 if (view->focussed)
5328 wstandout(view->window);
5329 s->selected_entry = te;
5331 tc = match_color(&s->colors, line);
5332 if (tc)
5333 wattr_on(view->window,
5334 COLOR_PAIR(tc->colorpair), NULL);
5335 waddwstr(view->window, wline);
5336 if (tc)
5337 wattr_off(view->window,
5338 COLOR_PAIR(tc->colorpair), NULL);
5339 if (width < view->ncols - 1)
5340 waddch(view->window, '\n');
5341 if (n == s->selected && view->focussed)
5342 wstandend(view->window);
5343 free(line);
5344 free(wline);
5345 wline = NULL;
5346 n++;
5347 s->ndisplayed++;
5348 s->last_displayed_entry = te;
5349 if (--limit <= 0)
5350 break;
5353 return err;
5356 static void
5357 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5359 struct got_tree_entry *te;
5360 int isroot = s->tree == s->root;
5361 int i = 0;
5363 if (s->first_displayed_entry == NULL)
5364 return;
5366 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5367 while (i++ < maxscroll) {
5368 if (te == NULL) {
5369 if (!isroot)
5370 s->first_displayed_entry = NULL;
5371 break;
5373 s->first_displayed_entry = te;
5374 te = got_tree_entry_get_prev(s->tree, te);
5378 static void
5379 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5381 struct got_tree_entry *next, *last;
5382 int n = 0;
5384 if (s->first_displayed_entry)
5385 next = got_tree_entry_get_next(s->tree,
5386 s->first_displayed_entry);
5387 else
5388 next = got_object_tree_get_first_entry(s->tree);
5390 last = s->last_displayed_entry;
5391 while (next && last && n++ < maxscroll) {
5392 last = got_tree_entry_get_next(s->tree, last);
5393 if (last) {
5394 s->first_displayed_entry = next;
5395 next = got_tree_entry_get_next(s->tree, next);
5400 static const struct got_error *
5401 tree_entry_path(char **path, struct tog_parent_trees *parents,
5402 struct got_tree_entry *te)
5404 const struct got_error *err = NULL;
5405 struct tog_parent_tree *pt;
5406 size_t len = 2; /* for leading slash and NUL */
5408 TAILQ_FOREACH(pt, parents, entry)
5409 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5410 + 1 /* slash */;
5411 if (te)
5412 len += strlen(got_tree_entry_get_name(te));
5414 *path = calloc(1, len);
5415 if (path == NULL)
5416 return got_error_from_errno("calloc");
5418 (*path)[0] = '/';
5419 pt = TAILQ_LAST(parents, tog_parent_trees);
5420 while (pt) {
5421 const char *name = got_tree_entry_get_name(pt->selected_entry);
5422 if (strlcat(*path, name, len) >= len) {
5423 err = got_error(GOT_ERR_NO_SPACE);
5424 goto done;
5426 if (strlcat(*path, "/", len) >= len) {
5427 err = got_error(GOT_ERR_NO_SPACE);
5428 goto done;
5430 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5432 if (te) {
5433 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5434 err = got_error(GOT_ERR_NO_SPACE);
5435 goto done;
5438 done:
5439 if (err) {
5440 free(*path);
5441 *path = NULL;
5443 return err;
5446 static const struct got_error *
5447 blame_tree_entry(struct tog_view **new_view, int begin_x,
5448 struct got_tree_entry *te, struct tog_parent_trees *parents,
5449 struct got_object_id *commit_id, struct got_repository *repo)
5451 const struct got_error *err = NULL;
5452 char *path;
5453 struct tog_view *blame_view;
5455 *new_view = NULL;
5457 err = tree_entry_path(&path, parents, te);
5458 if (err)
5459 return err;
5461 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5462 if (blame_view == NULL) {
5463 err = got_error_from_errno("view_open");
5464 goto done;
5467 err = open_blame_view(blame_view, path, commit_id, repo);
5468 if (err) {
5469 if (err->code == GOT_ERR_CANCELLED)
5470 err = NULL;
5471 view_close(blame_view);
5472 } else
5473 *new_view = blame_view;
5474 done:
5475 free(path);
5476 return err;
5479 static const struct got_error *
5480 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5481 struct tog_tree_view_state *s)
5483 struct tog_view *log_view;
5484 const struct got_error *err = NULL;
5485 char *path;
5487 *new_view = NULL;
5489 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5490 if (log_view == NULL)
5491 return got_error_from_errno("view_open");
5493 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5494 if (err)
5495 return err;
5497 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5498 path, 0);
5499 if (err)
5500 view_close(log_view);
5501 else
5502 *new_view = log_view;
5503 free(path);
5504 return err;
5507 static const struct got_error *
5508 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5509 const char *head_ref_name, struct got_repository *repo)
5511 const struct got_error *err = NULL;
5512 char *commit_id_str = NULL;
5513 struct tog_tree_view_state *s = &view->state.tree;
5514 struct got_commit_object *commit = NULL;
5516 TAILQ_INIT(&s->parents);
5517 STAILQ_INIT(&s->colors);
5519 s->commit_id = got_object_id_dup(commit_id);
5520 if (s->commit_id == NULL)
5521 return got_error_from_errno("got_object_id_dup");
5523 err = got_object_open_as_commit(&commit, repo, commit_id);
5524 if (err)
5525 goto done;
5528 * The root is opened here and will be closed when the view is closed.
5529 * Any visited subtrees and their path-wise parents are opened and
5530 * closed on demand.
5532 err = got_object_open_as_tree(&s->root, repo,
5533 got_object_commit_get_tree_id(commit));
5534 if (err)
5535 goto done;
5536 s->tree = s->root;
5538 err = got_object_id_str(&commit_id_str, commit_id);
5539 if (err != NULL)
5540 goto done;
5542 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5543 err = got_error_from_errno("asprintf");
5544 goto done;
5547 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5548 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5549 if (head_ref_name) {
5550 s->head_ref_name = strdup(head_ref_name);
5551 if (s->head_ref_name == NULL) {
5552 err = got_error_from_errno("strdup");
5553 goto done;
5556 s->repo = repo;
5558 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5559 err = add_color(&s->colors, "\\$$",
5560 TOG_COLOR_TREE_SUBMODULE,
5561 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5562 if (err)
5563 goto done;
5564 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5565 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5566 if (err)
5567 goto done;
5568 err = add_color(&s->colors, "/$",
5569 TOG_COLOR_TREE_DIRECTORY,
5570 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5571 if (err)
5572 goto done;
5574 err = add_color(&s->colors, "\\*$",
5575 TOG_COLOR_TREE_EXECUTABLE,
5576 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5577 if (err)
5578 goto done;
5580 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5581 get_color_value("TOG_COLOR_COMMIT"));
5582 if (err)
5583 goto done;
5586 view->show = show_tree_view;
5587 view->input = input_tree_view;
5588 view->close = close_tree_view;
5589 view->search_start = search_start_tree_view;
5590 view->search_next = search_next_tree_view;
5591 done:
5592 free(commit_id_str);
5593 if (commit)
5594 got_object_commit_close(commit);
5595 if (err)
5596 close_tree_view(view);
5597 return err;
5600 static const struct got_error *
5601 close_tree_view(struct tog_view *view)
5603 struct tog_tree_view_state *s = &view->state.tree;
5605 free_colors(&s->colors);
5606 free(s->tree_label);
5607 s->tree_label = NULL;
5608 free(s->commit_id);
5609 s->commit_id = NULL;
5610 free(s->head_ref_name);
5611 s->head_ref_name = NULL;
5612 while (!TAILQ_EMPTY(&s->parents)) {
5613 struct tog_parent_tree *parent;
5614 parent = TAILQ_FIRST(&s->parents);
5615 TAILQ_REMOVE(&s->parents, parent, entry);
5616 if (parent->tree != s->root)
5617 got_object_tree_close(parent->tree);
5618 free(parent);
5621 if (s->tree != NULL && s->tree != s->root)
5622 got_object_tree_close(s->tree);
5623 if (s->root)
5624 got_object_tree_close(s->root);
5625 return NULL;
5628 static const struct got_error *
5629 search_start_tree_view(struct tog_view *view)
5631 struct tog_tree_view_state *s = &view->state.tree;
5633 s->matched_entry = NULL;
5634 return NULL;
5637 static int
5638 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5640 regmatch_t regmatch;
5642 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5643 0) == 0;
5646 static const struct got_error *
5647 search_next_tree_view(struct tog_view *view)
5649 struct tog_tree_view_state *s = &view->state.tree;
5650 struct got_tree_entry *te = NULL;
5652 if (!view->searching) {
5653 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5654 return NULL;
5657 if (s->matched_entry) {
5658 if (view->searching == TOG_SEARCH_FORWARD) {
5659 if (s->selected_entry)
5660 te = got_tree_entry_get_next(s->tree,
5661 s->selected_entry);
5662 else
5663 te = got_object_tree_get_first_entry(s->tree);
5664 } else {
5665 if (s->selected_entry == NULL)
5666 te = got_object_tree_get_last_entry(s->tree);
5667 else
5668 te = got_tree_entry_get_prev(s->tree,
5669 s->selected_entry);
5671 } else {
5672 if (s->selected_entry)
5673 te = s->selected_entry;
5674 else if (view->searching == TOG_SEARCH_FORWARD)
5675 te = got_object_tree_get_first_entry(s->tree);
5676 else
5677 te = got_object_tree_get_last_entry(s->tree);
5680 while (1) {
5681 if (te == NULL) {
5682 if (s->matched_entry == NULL) {
5683 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5684 return NULL;
5686 if (view->searching == TOG_SEARCH_FORWARD)
5687 te = got_object_tree_get_first_entry(s->tree);
5688 else
5689 te = got_object_tree_get_last_entry(s->tree);
5692 if (match_tree_entry(te, &view->regex)) {
5693 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5694 s->matched_entry = te;
5695 break;
5698 if (view->searching == TOG_SEARCH_FORWARD)
5699 te = got_tree_entry_get_next(s->tree, te);
5700 else
5701 te = got_tree_entry_get_prev(s->tree, te);
5704 if (s->matched_entry) {
5705 s->first_displayed_entry = s->matched_entry;
5706 s->selected = 0;
5709 return NULL;
5712 static const struct got_error *
5713 show_tree_view(struct tog_view *view)
5715 const struct got_error *err = NULL;
5716 struct tog_tree_view_state *s = &view->state.tree;
5717 char *parent_path;
5719 err = tree_entry_path(&parent_path, &s->parents, NULL);
5720 if (err)
5721 return err;
5723 err = draw_tree_entries(view, parent_path);
5724 free(parent_path);
5726 view_vborder(view);
5727 return err;
5730 static const struct got_error *
5731 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5733 const struct got_error *err = NULL;
5734 struct tog_tree_view_state *s = &view->state.tree;
5735 struct tog_view *log_view, *ref_view;
5736 struct got_tree_entry *te;
5737 int begin_x = 0, n, nscroll = view->nlines - 3;
5739 switch (ch) {
5740 case 'i':
5741 s->show_ids = !s->show_ids;
5742 break;
5743 case 'l':
5744 if (!s->selected_entry)
5745 break;
5746 if (view_is_parent_view(view))
5747 begin_x = view_split_begin_x(view->begin_x);
5748 err = log_selected_tree_entry(&log_view, begin_x, s);
5749 view->focussed = 0;
5750 log_view->focussed = 1;
5751 if (view_is_parent_view(view)) {
5752 err = view_close_child(view);
5753 if (err)
5754 return err;
5755 view_set_child(view, log_view);
5756 view->focus_child = 1;
5757 } else
5758 *new_view = log_view;
5759 break;
5760 case 'r':
5761 if (view_is_parent_view(view))
5762 begin_x = view_split_begin_x(view->begin_x);
5763 ref_view = view_open(view->nlines, view->ncols,
5764 view->begin_y, begin_x, TOG_VIEW_REF);
5765 if (ref_view == NULL)
5766 return got_error_from_errno("view_open");
5767 err = open_ref_view(ref_view, s->repo);
5768 if (err) {
5769 view_close(ref_view);
5770 return err;
5772 view->focussed = 0;
5773 ref_view->focussed = 1;
5774 if (view_is_parent_view(view)) {
5775 err = view_close_child(view);
5776 if (err)
5777 return err;
5778 view_set_child(view, ref_view);
5779 view->focus_child = 1;
5780 } else
5781 *new_view = ref_view;
5782 break;
5783 case 'g':
5784 case KEY_HOME:
5785 s->selected = 0;
5786 if (s->tree == s->root)
5787 s->first_displayed_entry =
5788 got_object_tree_get_first_entry(s->tree);
5789 else
5790 s->first_displayed_entry = NULL;
5791 break;
5792 case 'G':
5793 case KEY_END:
5794 s->selected = 0;
5795 te = got_object_tree_get_last_entry(s->tree);
5796 for (n = 0; n < view->nlines - 3; n++) {
5797 if (te == NULL) {
5798 if(s->tree != s->root) {
5799 s->first_displayed_entry = NULL;
5800 n++;
5802 break;
5804 s->first_displayed_entry = te;
5805 te = got_tree_entry_get_prev(s->tree, te);
5807 if (n > 0)
5808 s->selected = n - 1;
5809 break;
5810 case 'k':
5811 case KEY_UP:
5812 case CTRL('p'):
5813 if (s->selected > 0) {
5814 s->selected--;
5815 break;
5817 tree_scroll_up(s, 1);
5818 break;
5819 case CTRL('u'):
5820 case 'u':
5821 nscroll /= 2;
5822 /* FALL THROUGH */
5823 case KEY_PPAGE:
5824 case CTRL('b'):
5825 if (s->tree == s->root) {
5826 if (got_object_tree_get_first_entry(s->tree) ==
5827 s->first_displayed_entry)
5828 s->selected -= MIN(s->selected, nscroll);
5829 } else {
5830 if (s->first_displayed_entry == NULL)
5831 s->selected -= MIN(s->selected, nscroll);
5833 tree_scroll_up(s, MAX(0, nscroll));
5834 break;
5835 case 'j':
5836 case KEY_DOWN:
5837 case CTRL('n'):
5838 if (s->selected < s->ndisplayed - 1) {
5839 s->selected++;
5840 break;
5842 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5843 == NULL)
5844 /* can't scroll any further */
5845 break;
5846 tree_scroll_down(s, 1);
5847 break;
5848 case CTRL('d'):
5849 case 'd':
5850 nscroll /= 2;
5851 /* FALL THROUGH */
5852 case KEY_NPAGE:
5853 case CTRL('f'):
5854 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5855 == NULL) {
5856 /* can't scroll any further; move cursor down */
5857 if (s->selected < s->ndisplayed - 1)
5858 s->selected += MIN(nscroll,
5859 s->ndisplayed - s->selected - 1);
5860 break;
5862 tree_scroll_down(s, nscroll);
5863 break;
5864 case KEY_ENTER:
5865 case '\r':
5866 case KEY_BACKSPACE:
5867 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5868 struct tog_parent_tree *parent;
5869 /* user selected '..' */
5870 if (s->tree == s->root)
5871 break;
5872 parent = TAILQ_FIRST(&s->parents);
5873 TAILQ_REMOVE(&s->parents, parent,
5874 entry);
5875 got_object_tree_close(s->tree);
5876 s->tree = parent->tree;
5877 s->first_displayed_entry =
5878 parent->first_displayed_entry;
5879 s->selected_entry =
5880 parent->selected_entry;
5881 s->selected = parent->selected;
5882 free(parent);
5883 } else if (S_ISDIR(got_tree_entry_get_mode(
5884 s->selected_entry))) {
5885 struct got_tree_object *subtree;
5886 err = got_object_open_as_tree(&subtree, s->repo,
5887 got_tree_entry_get_id(s->selected_entry));
5888 if (err)
5889 break;
5890 err = tree_view_visit_subtree(s, subtree);
5891 if (err) {
5892 got_object_tree_close(subtree);
5893 break;
5895 } else if (S_ISREG(got_tree_entry_get_mode(
5896 s->selected_entry))) {
5897 struct tog_view *blame_view;
5898 int begin_x = view_is_parent_view(view) ?
5899 view_split_begin_x(view->begin_x) : 0;
5901 err = blame_tree_entry(&blame_view, begin_x,
5902 s->selected_entry, &s->parents,
5903 s->commit_id, s->repo);
5904 if (err)
5905 break;
5906 view->focussed = 0;
5907 blame_view->focussed = 1;
5908 if (view_is_parent_view(view)) {
5909 err = view_close_child(view);
5910 if (err)
5911 return err;
5912 view_set_child(view, blame_view);
5913 view->focus_child = 1;
5914 } else
5915 *new_view = blame_view;
5917 break;
5918 case KEY_RESIZE:
5919 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5920 s->selected = view->nlines - 4;
5921 break;
5922 default:
5923 break;
5926 return err;
5929 __dead static void
5930 usage_tree(void)
5932 endwin();
5933 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5934 getprogname());
5935 exit(1);
5938 static const struct got_error *
5939 cmd_tree(int argc, char *argv[])
5941 const struct got_error *error;
5942 struct got_repository *repo = NULL;
5943 struct got_worktree *worktree = NULL;
5944 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5945 struct got_object_id *commit_id = NULL;
5946 struct got_commit_object *commit = NULL;
5947 const char *commit_id_arg = NULL;
5948 char *label = NULL;
5949 struct got_reference *ref = NULL;
5950 const char *head_ref_name = NULL;
5951 int ch;
5952 struct tog_view *view;
5953 int *pack_fds = NULL;
5955 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5956 switch (ch) {
5957 case 'c':
5958 commit_id_arg = optarg;
5959 break;
5960 case 'r':
5961 repo_path = realpath(optarg, NULL);
5962 if (repo_path == NULL)
5963 return got_error_from_errno2("realpath",
5964 optarg);
5965 break;
5966 default:
5967 usage_tree();
5968 /* NOTREACHED */
5972 argc -= optind;
5973 argv += optind;
5975 if (argc > 1)
5976 usage_tree();
5978 error = got_repo_pack_fds_open(&pack_fds);
5979 if (error != NULL)
5980 goto done;
5982 if (repo_path == NULL) {
5983 cwd = getcwd(NULL, 0);
5984 if (cwd == NULL)
5985 return got_error_from_errno("getcwd");
5986 error = got_worktree_open(&worktree, cwd);
5987 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5988 goto done;
5989 if (worktree)
5990 repo_path =
5991 strdup(got_worktree_get_repo_path(worktree));
5992 else
5993 repo_path = strdup(cwd);
5994 if (repo_path == NULL) {
5995 error = got_error_from_errno("strdup");
5996 goto done;
6000 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6001 if (error != NULL)
6002 goto done;
6004 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6005 repo, worktree);
6006 if (error)
6007 goto done;
6009 init_curses();
6011 error = apply_unveil(got_repo_get_path(repo), NULL);
6012 if (error)
6013 goto done;
6015 error = tog_load_refs(repo, 0);
6016 if (error)
6017 goto done;
6019 if (commit_id_arg == NULL) {
6020 error = got_repo_match_object_id(&commit_id, &label,
6021 worktree ? got_worktree_get_head_ref_name(worktree) :
6022 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6023 if (error)
6024 goto done;
6025 head_ref_name = label;
6026 } else {
6027 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6028 if (error == NULL)
6029 head_ref_name = got_ref_get_name(ref);
6030 else if (error->code != GOT_ERR_NOT_REF)
6031 goto done;
6032 error = got_repo_match_object_id(&commit_id, NULL,
6033 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6034 if (error)
6035 goto done;
6038 error = got_object_open_as_commit(&commit, repo, commit_id);
6039 if (error)
6040 goto done;
6042 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6043 if (view == NULL) {
6044 error = got_error_from_errno("view_open");
6045 goto done;
6047 error = open_tree_view(view, commit_id, head_ref_name, repo);
6048 if (error)
6049 goto done;
6050 if (!got_path_is_root_dir(in_repo_path)) {
6051 error = tree_view_walk_path(&view->state.tree, commit,
6052 in_repo_path);
6053 if (error)
6054 goto done;
6057 if (worktree) {
6058 /* Release work tree lock. */
6059 got_worktree_close(worktree);
6060 worktree = NULL;
6062 error = view_loop(view);
6063 done:
6064 free(repo_path);
6065 free(cwd);
6066 free(commit_id);
6067 free(label);
6068 if (ref)
6069 got_ref_close(ref);
6070 if (repo) {
6071 const struct got_error *close_err = got_repo_close(repo);
6072 if (error == NULL)
6073 error = close_err;
6075 if (pack_fds) {
6076 const struct got_error *pack_err =
6077 got_repo_pack_fds_close(pack_fds);
6078 if (error == NULL)
6079 error = pack_err;
6081 tog_free_refs();
6082 return error;
6085 static const struct got_error *
6086 ref_view_load_refs(struct tog_ref_view_state *s)
6088 struct got_reflist_entry *sre;
6089 struct tog_reflist_entry *re;
6091 s->nrefs = 0;
6092 TAILQ_FOREACH(sre, &tog_refs, entry) {
6093 if (strncmp(got_ref_get_name(sre->ref),
6094 "refs/got/", 9) == 0 &&
6095 strncmp(got_ref_get_name(sre->ref),
6096 "refs/got/backup/", 16) != 0)
6097 continue;
6099 re = malloc(sizeof(*re));
6100 if (re == NULL)
6101 return got_error_from_errno("malloc");
6103 re->ref = got_ref_dup(sre->ref);
6104 if (re->ref == NULL)
6105 return got_error_from_errno("got_ref_dup");
6106 re->idx = s->nrefs++;
6107 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6110 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6111 return NULL;
6114 void
6115 ref_view_free_refs(struct tog_ref_view_state *s)
6117 struct tog_reflist_entry *re;
6119 while (!TAILQ_EMPTY(&s->refs)) {
6120 re = TAILQ_FIRST(&s->refs);
6121 TAILQ_REMOVE(&s->refs, re, entry);
6122 got_ref_close(re->ref);
6123 free(re);
6127 static const struct got_error *
6128 open_ref_view(struct tog_view *view, struct got_repository *repo)
6130 const struct got_error *err = NULL;
6131 struct tog_ref_view_state *s = &view->state.ref;
6133 s->selected_entry = 0;
6134 s->repo = repo;
6136 TAILQ_INIT(&s->refs);
6137 STAILQ_INIT(&s->colors);
6139 err = ref_view_load_refs(s);
6140 if (err)
6141 return err;
6143 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6144 err = add_color(&s->colors, "^refs/heads/",
6145 TOG_COLOR_REFS_HEADS,
6146 get_color_value("TOG_COLOR_REFS_HEADS"));
6147 if (err)
6148 goto done;
6150 err = add_color(&s->colors, "^refs/tags/",
6151 TOG_COLOR_REFS_TAGS,
6152 get_color_value("TOG_COLOR_REFS_TAGS"));
6153 if (err)
6154 goto done;
6156 err = add_color(&s->colors, "^refs/remotes/",
6157 TOG_COLOR_REFS_REMOTES,
6158 get_color_value("TOG_COLOR_REFS_REMOTES"));
6159 if (err)
6160 goto done;
6162 err = add_color(&s->colors, "^refs/got/backup/",
6163 TOG_COLOR_REFS_BACKUP,
6164 get_color_value("TOG_COLOR_REFS_BACKUP"));
6165 if (err)
6166 goto done;
6169 view->show = show_ref_view;
6170 view->input = input_ref_view;
6171 view->close = close_ref_view;
6172 view->search_start = search_start_ref_view;
6173 view->search_next = search_next_ref_view;
6174 done:
6175 if (err)
6176 free_colors(&s->colors);
6177 return err;
6180 static const struct got_error *
6181 close_ref_view(struct tog_view *view)
6183 struct tog_ref_view_state *s = &view->state.ref;
6185 ref_view_free_refs(s);
6186 free_colors(&s->colors);
6188 return NULL;
6191 static const struct got_error *
6192 resolve_reflist_entry(struct got_object_id **commit_id,
6193 struct tog_reflist_entry *re, struct got_repository *repo)
6195 const struct got_error *err = NULL;
6196 struct got_object_id *obj_id;
6197 struct got_tag_object *tag = NULL;
6198 int obj_type;
6200 *commit_id = NULL;
6202 err = got_ref_resolve(&obj_id, repo, re->ref);
6203 if (err)
6204 return err;
6206 err = got_object_get_type(&obj_type, repo, obj_id);
6207 if (err)
6208 goto done;
6210 switch (obj_type) {
6211 case GOT_OBJ_TYPE_COMMIT:
6212 *commit_id = obj_id;
6213 break;
6214 case GOT_OBJ_TYPE_TAG:
6215 err = got_object_open_as_tag(&tag, repo, obj_id);
6216 if (err)
6217 goto done;
6218 free(obj_id);
6219 err = got_object_get_type(&obj_type, repo,
6220 got_object_tag_get_object_id(tag));
6221 if (err)
6222 goto done;
6223 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6224 err = got_error(GOT_ERR_OBJ_TYPE);
6225 goto done;
6227 *commit_id = got_object_id_dup(
6228 got_object_tag_get_object_id(tag));
6229 if (*commit_id == NULL) {
6230 err = got_error_from_errno("got_object_id_dup");
6231 goto done;
6233 break;
6234 default:
6235 err = got_error(GOT_ERR_OBJ_TYPE);
6236 break;
6239 done:
6240 if (tag)
6241 got_object_tag_close(tag);
6242 if (err) {
6243 free(*commit_id);
6244 *commit_id = NULL;
6246 return err;
6249 static const struct got_error *
6250 log_ref_entry(struct tog_view **new_view, int begin_x,
6251 struct tog_reflist_entry *re, struct got_repository *repo)
6253 struct tog_view *log_view;
6254 const struct got_error *err = NULL;
6255 struct got_object_id *commit_id = NULL;
6257 *new_view = NULL;
6259 err = resolve_reflist_entry(&commit_id, re, repo);
6260 if (err) {
6261 if (err->code != GOT_ERR_OBJ_TYPE)
6262 return err;
6263 else
6264 return NULL;
6267 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6268 if (log_view == NULL) {
6269 err = got_error_from_errno("view_open");
6270 goto done;
6273 err = open_log_view(log_view, commit_id, repo,
6274 got_ref_get_name(re->ref), "", 0);
6275 done:
6276 if (err)
6277 view_close(log_view);
6278 else
6279 *new_view = log_view;
6280 free(commit_id);
6281 return err;
6284 static void
6285 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6287 struct tog_reflist_entry *re;
6288 int i = 0;
6290 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6291 return;
6293 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6294 while (i++ < maxscroll) {
6295 if (re == NULL)
6296 break;
6297 s->first_displayed_entry = re;
6298 re = TAILQ_PREV(re, tog_reflist_head, entry);
6302 static void
6303 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6305 struct tog_reflist_entry *next, *last;
6306 int n = 0;
6308 if (s->first_displayed_entry)
6309 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6310 else
6311 next = TAILQ_FIRST(&s->refs);
6313 last = s->last_displayed_entry;
6314 while (next && last && n++ < maxscroll) {
6315 last = TAILQ_NEXT(last, entry);
6316 if (last) {
6317 s->first_displayed_entry = next;
6318 next = TAILQ_NEXT(next, entry);
6323 static const struct got_error *
6324 search_start_ref_view(struct tog_view *view)
6326 struct tog_ref_view_state *s = &view->state.ref;
6328 s->matched_entry = NULL;
6329 return NULL;
6332 static int
6333 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6335 regmatch_t regmatch;
6337 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6338 0) == 0;
6341 static const struct got_error *
6342 search_next_ref_view(struct tog_view *view)
6344 struct tog_ref_view_state *s = &view->state.ref;
6345 struct tog_reflist_entry *re = NULL;
6347 if (!view->searching) {
6348 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6349 return NULL;
6352 if (s->matched_entry) {
6353 if (view->searching == TOG_SEARCH_FORWARD) {
6354 if (s->selected_entry)
6355 re = TAILQ_NEXT(s->selected_entry, entry);
6356 else
6357 re = TAILQ_PREV(s->selected_entry,
6358 tog_reflist_head, entry);
6359 } else {
6360 if (s->selected_entry == NULL)
6361 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6362 else
6363 re = TAILQ_PREV(s->selected_entry,
6364 tog_reflist_head, entry);
6366 } else {
6367 if (s->selected_entry)
6368 re = s->selected_entry;
6369 else if (view->searching == TOG_SEARCH_FORWARD)
6370 re = TAILQ_FIRST(&s->refs);
6371 else
6372 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6375 while (1) {
6376 if (re == NULL) {
6377 if (s->matched_entry == NULL) {
6378 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6379 return NULL;
6381 if (view->searching == TOG_SEARCH_FORWARD)
6382 re = TAILQ_FIRST(&s->refs);
6383 else
6384 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6387 if (match_reflist_entry(re, &view->regex)) {
6388 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6389 s->matched_entry = re;
6390 break;
6393 if (view->searching == TOG_SEARCH_FORWARD)
6394 re = TAILQ_NEXT(re, entry);
6395 else
6396 re = TAILQ_PREV(re, tog_reflist_head, entry);
6399 if (s->matched_entry) {
6400 s->first_displayed_entry = s->matched_entry;
6401 s->selected = 0;
6404 return NULL;
6407 static const struct got_error *
6408 show_ref_view(struct tog_view *view)
6410 const struct got_error *err = NULL;
6411 struct tog_ref_view_state *s = &view->state.ref;
6412 struct tog_reflist_entry *re;
6413 char *line = NULL;
6414 wchar_t *wline;
6415 struct tog_color *tc;
6416 int width, n;
6417 int limit = view->nlines;
6419 werase(view->window);
6421 s->ndisplayed = 0;
6423 if (limit == 0)
6424 return NULL;
6426 re = s->first_displayed_entry;
6428 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6429 s->nrefs) == -1)
6430 return got_error_from_errno("asprintf");
6432 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6433 if (err) {
6434 free(line);
6435 return err;
6437 if (view_needs_focus_indication(view))
6438 wstandout(view->window);
6439 waddwstr(view->window, wline);
6440 if (view_needs_focus_indication(view))
6441 wstandend(view->window);
6442 free(wline);
6443 wline = NULL;
6444 free(line);
6445 line = NULL;
6446 if (width < view->ncols - 1)
6447 waddch(view->window, '\n');
6448 if (--limit <= 0)
6449 return NULL;
6451 n = 0;
6452 while (re && limit > 0) {
6453 char *line = NULL;
6454 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6456 if (s->show_date) {
6457 struct got_commit_object *ci;
6458 struct got_tag_object *tag;
6459 struct got_object_id *id;
6460 struct tm tm;
6461 time_t t;
6463 err = got_ref_resolve(&id, s->repo, re->ref);
6464 if (err)
6465 return err;
6466 err = got_object_open_as_tag(&tag, s->repo, id);
6467 if (err) {
6468 if (err->code != GOT_ERR_OBJ_TYPE) {
6469 free(id);
6470 return err;
6472 err = got_object_open_as_commit(&ci, s->repo,
6473 id);
6474 if (err) {
6475 free(id);
6476 return err;
6478 t = got_object_commit_get_committer_time(ci);
6479 got_object_commit_close(ci);
6480 } else {
6481 t = got_object_tag_get_tagger_time(tag);
6482 got_object_tag_close(tag);
6484 free(id);
6485 if (gmtime_r(&t, &tm) == NULL)
6486 return got_error_from_errno("gmtime_r");
6487 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6488 return got_error(GOT_ERR_NO_SPACE);
6490 if (got_ref_is_symbolic(re->ref)) {
6491 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6492 ymd : "", got_ref_get_name(re->ref),
6493 got_ref_get_symref_target(re->ref)) == -1)
6494 return got_error_from_errno("asprintf");
6495 } else if (s->show_ids) {
6496 struct got_object_id *id;
6497 char *id_str;
6498 err = got_ref_resolve(&id, s->repo, re->ref);
6499 if (err)
6500 return err;
6501 err = got_object_id_str(&id_str, id);
6502 if (err) {
6503 free(id);
6504 return err;
6506 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6507 got_ref_get_name(re->ref), id_str) == -1) {
6508 err = got_error_from_errno("asprintf");
6509 free(id);
6510 free(id_str);
6511 return err;
6513 free(id);
6514 free(id_str);
6515 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6516 got_ref_get_name(re->ref)) == -1)
6517 return got_error_from_errno("asprintf");
6519 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6520 if (err) {
6521 free(line);
6522 return err;
6524 if (n == s->selected) {
6525 if (view->focussed)
6526 wstandout(view->window);
6527 s->selected_entry = re;
6529 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6530 if (tc)
6531 wattr_on(view->window,
6532 COLOR_PAIR(tc->colorpair), NULL);
6533 waddwstr(view->window, wline);
6534 if (tc)
6535 wattr_off(view->window,
6536 COLOR_PAIR(tc->colorpair), NULL);
6537 if (width < view->ncols - 1)
6538 waddch(view->window, '\n');
6539 if (n == s->selected && view->focussed)
6540 wstandend(view->window);
6541 free(line);
6542 free(wline);
6543 wline = NULL;
6544 n++;
6545 s->ndisplayed++;
6546 s->last_displayed_entry = re;
6548 limit--;
6549 re = TAILQ_NEXT(re, entry);
6552 view_vborder(view);
6553 return err;
6556 static const struct got_error *
6557 browse_ref_tree(struct tog_view **new_view, int begin_x,
6558 struct tog_reflist_entry *re, struct got_repository *repo)
6560 const struct got_error *err = NULL;
6561 struct got_object_id *commit_id = NULL;
6562 struct tog_view *tree_view;
6564 *new_view = NULL;
6566 err = resolve_reflist_entry(&commit_id, re, repo);
6567 if (err) {
6568 if (err->code != GOT_ERR_OBJ_TYPE)
6569 return err;
6570 else
6571 return NULL;
6575 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6576 if (tree_view == NULL) {
6577 err = got_error_from_errno("view_open");
6578 goto done;
6581 err = open_tree_view(tree_view, commit_id,
6582 got_ref_get_name(re->ref), repo);
6583 if (err)
6584 goto done;
6586 *new_view = tree_view;
6587 done:
6588 free(commit_id);
6589 return err;
6591 static const struct got_error *
6592 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6594 const struct got_error *err = NULL;
6595 struct tog_ref_view_state *s = &view->state.ref;
6596 struct tog_view *log_view, *tree_view;
6597 struct tog_reflist_entry *re;
6598 int begin_x = 0, n, nscroll = view->nlines - 1;
6600 switch (ch) {
6601 case 'i':
6602 s->show_ids = !s->show_ids;
6603 break;
6604 case 'm':
6605 s->show_date = !s->show_date;
6606 break;
6607 case 'o':
6608 s->sort_by_date = !s->sort_by_date;
6609 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6610 got_ref_cmp_by_commit_timestamp_descending :
6611 tog_ref_cmp_by_name, s->repo);
6612 if (err)
6613 break;
6614 got_reflist_object_id_map_free(tog_refs_idmap);
6615 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6616 &tog_refs, s->repo);
6617 if (err)
6618 break;
6619 ref_view_free_refs(s);
6620 err = ref_view_load_refs(s);
6621 break;
6622 case KEY_ENTER:
6623 case '\r':
6624 if (!s->selected_entry)
6625 break;
6626 if (view_is_parent_view(view))
6627 begin_x = view_split_begin_x(view->begin_x);
6628 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6629 s->repo);
6630 view->focussed = 0;
6631 log_view->focussed = 1;
6632 if (view_is_parent_view(view)) {
6633 err = view_close_child(view);
6634 if (err)
6635 return err;
6636 view_set_child(view, log_view);
6637 view->focus_child = 1;
6638 } else
6639 *new_view = log_view;
6640 break;
6641 case 't':
6642 if (!s->selected_entry)
6643 break;
6644 if (view_is_parent_view(view))
6645 begin_x = view_split_begin_x(view->begin_x);
6646 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6647 s->repo);
6648 if (err || tree_view == NULL)
6649 break;
6650 view->focussed = 0;
6651 tree_view->focussed = 1;
6652 if (view_is_parent_view(view)) {
6653 err = view_close_child(view);
6654 if (err)
6655 return err;
6656 view_set_child(view, tree_view);
6657 view->focus_child = 1;
6658 } else
6659 *new_view = tree_view;
6660 break;
6661 case 'g':
6662 case KEY_HOME:
6663 s->selected = 0;
6664 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6665 break;
6666 case 'G':
6667 case KEY_END:
6668 s->selected = 0;
6669 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6670 for (n = 0; n < view->nlines - 1; n++) {
6671 if (re == NULL)
6672 break;
6673 s->first_displayed_entry = re;
6674 re = TAILQ_PREV(re, tog_reflist_head, entry);
6676 if (n > 0)
6677 s->selected = n - 1;
6678 break;
6679 case 'k':
6680 case KEY_UP:
6681 case CTRL('p'):
6682 if (s->selected > 0) {
6683 s->selected--;
6684 break;
6686 ref_scroll_up(s, 1);
6687 break;
6688 case CTRL('u'):
6689 case 'u':
6690 nscroll /= 2;
6691 /* FALL THROUGH */
6692 case KEY_PPAGE:
6693 case CTRL('b'):
6694 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6695 s->selected -= MIN(nscroll, s->selected);
6696 ref_scroll_up(s, MAX(0, nscroll));
6697 break;
6698 case 'j':
6699 case KEY_DOWN:
6700 case CTRL('n'):
6701 if (s->selected < s->ndisplayed - 1) {
6702 s->selected++;
6703 break;
6705 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6706 /* can't scroll any further */
6707 break;
6708 ref_scroll_down(s, 1);
6709 break;
6710 case CTRL('d'):
6711 case 'd':
6712 nscroll /= 2;
6713 /* FALL THROUGH */
6714 case KEY_NPAGE:
6715 case CTRL('f'):
6716 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6717 /* can't scroll any further; move cursor down */
6718 if (s->selected < s->ndisplayed - 1)
6719 s->selected += MIN(nscroll,
6720 s->ndisplayed - s->selected - 1);
6721 break;
6723 ref_scroll_down(s, nscroll);
6724 break;
6725 case CTRL('l'):
6726 tog_free_refs();
6727 err = tog_load_refs(s->repo, s->sort_by_date);
6728 if (err)
6729 break;
6730 ref_view_free_refs(s);
6731 err = ref_view_load_refs(s);
6732 break;
6733 case KEY_RESIZE:
6734 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6735 s->selected = view->nlines - 2;
6736 break;
6737 default:
6738 break;
6741 return err;
6744 __dead static void
6745 usage_ref(void)
6747 endwin();
6748 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6749 getprogname());
6750 exit(1);
6753 static const struct got_error *
6754 cmd_ref(int argc, char *argv[])
6756 const struct got_error *error;
6757 struct got_repository *repo = NULL;
6758 struct got_worktree *worktree = NULL;
6759 char *cwd = NULL, *repo_path = NULL;
6760 int ch;
6761 struct tog_view *view;
6762 int *pack_fds = NULL;
6764 while ((ch = getopt(argc, argv, "r:")) != -1) {
6765 switch (ch) {
6766 case 'r':
6767 repo_path = realpath(optarg, NULL);
6768 if (repo_path == NULL)
6769 return got_error_from_errno2("realpath",
6770 optarg);
6771 break;
6772 default:
6773 usage_ref();
6774 /* NOTREACHED */
6778 argc -= optind;
6779 argv += optind;
6781 if (argc > 1)
6782 usage_ref();
6784 error = got_repo_pack_fds_open(&pack_fds);
6785 if (error != NULL)
6786 goto done;
6788 if (repo_path == NULL) {
6789 cwd = getcwd(NULL, 0);
6790 if (cwd == NULL)
6791 return got_error_from_errno("getcwd");
6792 error = got_worktree_open(&worktree, cwd);
6793 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6794 goto done;
6795 if (worktree)
6796 repo_path =
6797 strdup(got_worktree_get_repo_path(worktree));
6798 else
6799 repo_path = strdup(cwd);
6800 if (repo_path == NULL) {
6801 error = got_error_from_errno("strdup");
6802 goto done;
6806 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6807 if (error != NULL)
6808 goto done;
6810 init_curses();
6812 error = apply_unveil(got_repo_get_path(repo), NULL);
6813 if (error)
6814 goto done;
6816 error = tog_load_refs(repo, 0);
6817 if (error)
6818 goto done;
6820 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6821 if (view == NULL) {
6822 error = got_error_from_errno("view_open");
6823 goto done;
6826 error = open_ref_view(view, repo);
6827 if (error)
6828 goto done;
6830 if (worktree) {
6831 /* Release work tree lock. */
6832 got_worktree_close(worktree);
6833 worktree = NULL;
6835 error = view_loop(view);
6836 done:
6837 free(repo_path);
6838 free(cwd);
6839 if (repo) {
6840 const struct got_error *close_err = got_repo_close(repo);
6841 if (close_err)
6842 error = close_err;
6844 if (pack_fds) {
6845 const struct got_error *pack_err =
6846 got_repo_pack_fds_close(pack_fds);
6847 if (error == NULL)
6848 error = pack_err;
6850 tog_free_refs();
6851 return error;
6854 static void
6855 list_commands(FILE *fp)
6857 size_t i;
6859 fprintf(fp, "commands:");
6860 for (i = 0; i < nitems(tog_commands); i++) {
6861 const struct tog_cmd *cmd = &tog_commands[i];
6862 fprintf(fp, " %s", cmd->name);
6864 fputc('\n', fp);
6867 __dead static void
6868 usage(int hflag, int status)
6870 FILE *fp = (status == 0) ? stdout : stderr;
6872 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6873 getprogname());
6874 if (hflag) {
6875 fprintf(fp, "lazy usage: %s path\n", getprogname());
6876 list_commands(fp);
6878 exit(status);
6881 static char **
6882 make_argv(int argc, ...)
6884 va_list ap;
6885 char **argv;
6886 int i;
6888 va_start(ap, argc);
6890 argv = calloc(argc, sizeof(char *));
6891 if (argv == NULL)
6892 err(1, "calloc");
6893 for (i = 0; i < argc; i++) {
6894 argv[i] = strdup(va_arg(ap, char *));
6895 if (argv[i] == NULL)
6896 err(1, "strdup");
6899 va_end(ap);
6900 return argv;
6904 * Try to convert 'tog path' into a 'tog log path' command.
6905 * The user could simply have mistyped the command rather than knowingly
6906 * provided a path. So check whether argv[0] can in fact be resolved
6907 * to a path in the HEAD commit and print a special error if not.
6908 * This hack is for mpi@ <3
6910 static const struct got_error *
6911 tog_log_with_path(int argc, char *argv[])
6913 const struct got_error *error = NULL, *close_err;
6914 const struct tog_cmd *cmd = NULL;
6915 struct got_repository *repo = NULL;
6916 struct got_worktree *worktree = NULL;
6917 struct got_object_id *commit_id = NULL, *id = NULL;
6918 struct got_commit_object *commit = NULL;
6919 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6920 char *commit_id_str = NULL, **cmd_argv = NULL;
6921 int *pack_fds = NULL;
6923 cwd = getcwd(NULL, 0);
6924 if (cwd == NULL)
6925 return got_error_from_errno("getcwd");
6927 error = got_repo_pack_fds_open(&pack_fds);
6928 if (error != NULL)
6929 goto done;
6931 error = got_worktree_open(&worktree, cwd);
6932 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6933 goto done;
6935 if (worktree)
6936 repo_path = strdup(got_worktree_get_repo_path(worktree));
6937 else
6938 repo_path = strdup(cwd);
6939 if (repo_path == NULL) {
6940 error = got_error_from_errno("strdup");
6941 goto done;
6944 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6945 if (error != NULL)
6946 goto done;
6948 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6949 repo, worktree);
6950 if (error)
6951 goto done;
6953 error = tog_load_refs(repo, 0);
6954 if (error)
6955 goto done;
6956 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6957 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6958 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6959 if (error)
6960 goto done;
6962 if (worktree) {
6963 got_worktree_close(worktree);
6964 worktree = NULL;
6967 error = got_object_open_as_commit(&commit, repo, commit_id);
6968 if (error)
6969 goto done;
6971 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6972 if (error) {
6973 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6974 goto done;
6975 fprintf(stderr, "%s: '%s' is no known command or path\n",
6976 getprogname(), argv[0]);
6977 usage(1, 1);
6978 /* not reached */
6981 close_err = got_repo_close(repo);
6982 if (error == NULL)
6983 error = close_err;
6984 repo = NULL;
6986 error = got_object_id_str(&commit_id_str, commit_id);
6987 if (error)
6988 goto done;
6990 cmd = &tog_commands[0]; /* log */
6991 argc = 4;
6992 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6993 error = cmd->cmd_main(argc, cmd_argv);
6994 done:
6995 if (repo) {
6996 close_err = got_repo_close(repo);
6997 if (error == NULL)
6998 error = close_err;
7000 if (commit)
7001 got_object_commit_close(commit);
7002 if (worktree)
7003 got_worktree_close(worktree);
7004 if (pack_fds) {
7005 const struct got_error *pack_err =
7006 got_repo_pack_fds_close(pack_fds);
7007 if (error == NULL)
7008 error = pack_err;
7010 free(id);
7011 free(commit_id_str);
7012 free(commit_id);
7013 free(cwd);
7014 free(repo_path);
7015 free(in_repo_path);
7016 if (cmd_argv) {
7017 int i;
7018 for (i = 0; i < argc; i++)
7019 free(cmd_argv[i]);
7020 free(cmd_argv);
7022 tog_free_refs();
7023 return error;
7026 int
7027 main(int argc, char *argv[])
7029 const struct got_error *error = NULL;
7030 const struct tog_cmd *cmd = NULL;
7031 int ch, hflag = 0, Vflag = 0;
7032 char **cmd_argv = NULL;
7033 static const struct option longopts[] = {
7034 { "version", no_argument, NULL, 'V' },
7035 { NULL, 0, NULL, 0}
7038 setlocale(LC_CTYPE, "");
7040 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7041 switch (ch) {
7042 case 'h':
7043 hflag = 1;
7044 break;
7045 case 'V':
7046 Vflag = 1;
7047 break;
7048 default:
7049 usage(hflag, 1);
7050 /* NOTREACHED */
7054 argc -= optind;
7055 argv += optind;
7056 optind = 1;
7057 optreset = 1;
7059 if (Vflag) {
7060 got_version_print_str();
7061 return 0;
7064 #ifndef PROFILE
7065 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7066 NULL) == -1)
7067 err(1, "pledge");
7068 #endif
7070 if (argc == 0) {
7071 if (hflag)
7072 usage(hflag, 0);
7073 /* Build an argument vector which runs a default command. */
7074 cmd = &tog_commands[0];
7075 argc = 1;
7076 cmd_argv = make_argv(argc, cmd->name);
7077 } else {
7078 size_t i;
7080 /* Did the user specify a command? */
7081 for (i = 0; i < nitems(tog_commands); i++) {
7082 if (strncmp(tog_commands[i].name, argv[0],
7083 strlen(argv[0])) == 0) {
7084 cmd = &tog_commands[i];
7085 break;
7090 if (cmd == NULL) {
7091 if (argc != 1)
7092 usage(0, 1);
7093 /* No command specified; try log with a path */
7094 error = tog_log_with_path(argc, argv);
7095 } else {
7096 if (hflag)
7097 cmd->cmd_usage();
7098 else
7099 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7102 endwin();
7103 putchar('\n');
7104 if (cmd_argv) {
7105 int i;
7106 for (i = 0; i < argc; i++)
7107 free(cmd_argv[i]);
7108 free(cmd_argv);
7111 if (error && error->code != GOT_ERR_CANCELLED)
7112 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7113 return 0;