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;
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 && view->x < width - 1) {
4345 waddwstr(view->window, wline + view->x);
4346 width += 9;
4348 free(wline);
4349 wline = NULL;
4350 if (err)
4351 return err;
4354 if (width <= view->ncols - 1)
4355 waddch(view->window, '\n');
4356 if (++nprinted == 1)
4357 s->first_displayed_line = lineno;
4359 free(line);
4360 s->last_displayed_line = lineno;
4362 view_vborder(view);
4364 return NULL;
4367 static const struct got_error *
4368 blame_cb(void *arg, int nlines, int lineno,
4369 struct got_commit_object *commit, struct got_object_id *id)
4371 const struct got_error *err = NULL;
4372 struct tog_blame_cb_args *a = arg;
4373 struct tog_blame_line *line;
4374 int errcode;
4376 if (nlines != a->nlines ||
4377 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4378 return got_error(GOT_ERR_RANGE);
4380 errcode = pthread_mutex_lock(&tog_mutex);
4381 if (errcode)
4382 return got_error_set_errno(errcode, "pthread_mutex_lock");
4384 if (*a->quit) { /* user has quit the blame view */
4385 err = got_error(GOT_ERR_ITER_COMPLETED);
4386 goto done;
4389 if (lineno == -1)
4390 goto done; /* no change in this commit */
4392 line = &a->lines[lineno - 1];
4393 if (line->annotated)
4394 goto done;
4396 line->id = got_object_id_dup(id);
4397 if (line->id == NULL) {
4398 err = got_error_from_errno("got_object_id_dup");
4399 goto done;
4401 line->annotated = 1;
4402 done:
4403 errcode = pthread_mutex_unlock(&tog_mutex);
4404 if (errcode)
4405 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4406 return err;
4409 static void *
4410 blame_thread(void *arg)
4412 const struct got_error *err, *close_err;
4413 struct tog_blame_thread_args *ta = arg;
4414 struct tog_blame_cb_args *a = ta->cb_args;
4415 int errcode;
4417 err = block_signals_used_by_main_thread();
4418 if (err)
4419 return (void *)err;
4421 err = got_blame(ta->path, a->commit_id, ta->repo,
4422 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4423 if (err && err->code == GOT_ERR_CANCELLED)
4424 err = NULL;
4426 errcode = pthread_mutex_lock(&tog_mutex);
4427 if (errcode)
4428 return (void *)got_error_set_errno(errcode,
4429 "pthread_mutex_lock");
4431 close_err = got_repo_close(ta->repo);
4432 if (err == NULL)
4433 err = close_err;
4434 ta->repo = NULL;
4435 *ta->complete = 1;
4437 errcode = pthread_mutex_unlock(&tog_mutex);
4438 if (errcode && err == NULL)
4439 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4441 return (void *)err;
4444 static struct got_object_id *
4445 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4446 int first_displayed_line, int selected_line)
4448 struct tog_blame_line *line;
4450 if (nlines <= 0)
4451 return NULL;
4453 line = &lines[first_displayed_line - 1 + selected_line - 1];
4454 if (!line->annotated)
4455 return NULL;
4457 return line->id;
4460 static const struct got_error *
4461 stop_blame(struct tog_blame *blame)
4463 const struct got_error *err = NULL;
4464 int i;
4466 if (blame->thread) {
4467 int errcode;
4468 errcode = pthread_mutex_unlock(&tog_mutex);
4469 if (errcode)
4470 return got_error_set_errno(errcode,
4471 "pthread_mutex_unlock");
4472 errcode = pthread_join(blame->thread, (void **)&err);
4473 if (errcode)
4474 return got_error_set_errno(errcode, "pthread_join");
4475 errcode = pthread_mutex_lock(&tog_mutex);
4476 if (errcode)
4477 return got_error_set_errno(errcode,
4478 "pthread_mutex_lock");
4479 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4480 err = NULL;
4481 blame->thread = 0; //NULL;
4483 if (blame->thread_args.repo) {
4484 const struct got_error *close_err;
4485 close_err = got_repo_close(blame->thread_args.repo);
4486 if (err == NULL)
4487 err = close_err;
4488 blame->thread_args.repo = NULL;
4490 if (blame->f) {
4491 if (fclose(blame->f) == EOF && err == NULL)
4492 err = got_error_from_errno("fclose");
4493 blame->f = NULL;
4495 if (blame->lines) {
4496 for (i = 0; i < blame->nlines; i++)
4497 free(blame->lines[i].id);
4498 free(blame->lines);
4499 blame->lines = NULL;
4501 free(blame->cb_args.commit_id);
4502 blame->cb_args.commit_id = NULL;
4503 if (blame->pack_fds) {
4504 const struct got_error *pack_err =
4505 got_repo_pack_fds_close(blame->pack_fds);
4506 if (err == NULL)
4507 err = pack_err;
4508 blame->pack_fds = NULL;
4510 return err;
4513 static const struct got_error *
4514 cancel_blame_view(void *arg)
4516 const struct got_error *err = NULL;
4517 int *done = arg;
4518 int errcode;
4520 errcode = pthread_mutex_lock(&tog_mutex);
4521 if (errcode)
4522 return got_error_set_errno(errcode,
4523 "pthread_mutex_unlock");
4525 if (*done)
4526 err = got_error(GOT_ERR_CANCELLED);
4528 errcode = pthread_mutex_unlock(&tog_mutex);
4529 if (errcode)
4530 return got_error_set_errno(errcode,
4531 "pthread_mutex_lock");
4533 return err;
4536 static const struct got_error *
4537 run_blame(struct tog_view *view)
4539 struct tog_blame_view_state *s = &view->state.blame;
4540 struct tog_blame *blame = &s->blame;
4541 const struct got_error *err = NULL;
4542 struct got_commit_object *commit = NULL;
4543 struct got_blob_object *blob = NULL;
4544 struct got_repository *thread_repo = NULL;
4545 struct got_object_id *obj_id = NULL;
4546 int obj_type;
4547 int *pack_fds = NULL;
4549 err = got_object_open_as_commit(&commit, s->repo,
4550 &s->blamed_commit->id);
4551 if (err)
4552 return err;
4554 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4555 if (err)
4556 goto done;
4558 err = got_object_get_type(&obj_type, s->repo, obj_id);
4559 if (err)
4560 goto done;
4562 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4563 err = got_error(GOT_ERR_OBJ_TYPE);
4564 goto done;
4567 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4568 if (err)
4569 goto done;
4570 blame->f = got_opentemp();
4571 if (blame->f == NULL) {
4572 err = got_error_from_errno("got_opentemp");
4573 goto done;
4575 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4576 &blame->line_offsets, blame->f, blob);
4577 if (err)
4578 goto done;
4579 if (blame->nlines == 0) {
4580 s->blame_complete = 1;
4581 goto done;
4584 /* Don't include \n at EOF in the blame line count. */
4585 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4586 blame->nlines--;
4588 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4589 if (blame->lines == NULL) {
4590 err = got_error_from_errno("calloc");
4591 goto done;
4594 err = got_repo_pack_fds_open(&pack_fds);
4595 if (err)
4596 goto done;
4597 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4598 pack_fds);
4599 if (err)
4600 goto done;
4602 blame->pack_fds = pack_fds;
4603 blame->cb_args.view = view;
4604 blame->cb_args.lines = blame->lines;
4605 blame->cb_args.nlines = blame->nlines;
4606 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4607 if (blame->cb_args.commit_id == NULL) {
4608 err = got_error_from_errno("got_object_id_dup");
4609 goto done;
4611 blame->cb_args.quit = &s->done;
4613 blame->thread_args.path = s->path;
4614 blame->thread_args.repo = thread_repo;
4615 blame->thread_args.cb_args = &blame->cb_args;
4616 blame->thread_args.complete = &s->blame_complete;
4617 blame->thread_args.cancel_cb = cancel_blame_view;
4618 blame->thread_args.cancel_arg = &s->done;
4619 s->blame_complete = 0;
4621 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4622 s->first_displayed_line = 1;
4623 s->last_displayed_line = view->nlines;
4624 s->selected_line = 1;
4626 s->matched_line = 0;
4628 done:
4629 if (commit)
4630 got_object_commit_close(commit);
4631 if (blob)
4632 got_object_blob_close(blob);
4633 free(obj_id);
4634 if (err)
4635 stop_blame(blame);
4636 return err;
4639 static const struct got_error *
4640 open_blame_view(struct tog_view *view, char *path,
4641 struct got_object_id *commit_id, struct got_repository *repo)
4643 const struct got_error *err = NULL;
4644 struct tog_blame_view_state *s = &view->state.blame;
4646 STAILQ_INIT(&s->blamed_commits);
4648 s->path = strdup(path);
4649 if (s->path == NULL)
4650 return got_error_from_errno("strdup");
4652 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4653 if (err) {
4654 free(s->path);
4655 return err;
4658 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4659 s->first_displayed_line = 1;
4660 s->last_displayed_line = view->nlines;
4661 s->selected_line = 1;
4662 s->blame_complete = 0;
4663 s->repo = repo;
4664 s->commit_id = commit_id;
4665 memset(&s->blame, 0, sizeof(s->blame));
4667 STAILQ_INIT(&s->colors);
4668 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4669 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4670 get_color_value("TOG_COLOR_COMMIT"));
4671 if (err)
4672 return err;
4675 view->show = show_blame_view;
4676 view->input = input_blame_view;
4677 view->close = close_blame_view;
4678 view->search_start = search_start_blame_view;
4679 view->search_next = search_next_blame_view;
4681 return run_blame(view);
4684 static const struct got_error *
4685 close_blame_view(struct tog_view *view)
4687 const struct got_error *err = NULL;
4688 struct tog_blame_view_state *s = &view->state.blame;
4690 if (s->blame.thread)
4691 err = stop_blame(&s->blame);
4693 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4694 struct got_object_qid *blamed_commit;
4695 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4696 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4697 got_object_qid_free(blamed_commit);
4700 free(s->path);
4701 free_colors(&s->colors);
4702 return err;
4705 static const struct got_error *
4706 search_start_blame_view(struct tog_view *view)
4708 struct tog_blame_view_state *s = &view->state.blame;
4710 s->matched_line = 0;
4711 return NULL;
4714 static const struct got_error *
4715 search_next_blame_view(struct tog_view *view)
4717 struct tog_blame_view_state *s = &view->state.blame;
4718 const struct got_error *err = NULL;
4719 int lineno;
4720 char *exstr = NULL, *line = NULL;
4721 size_t linesize = 0;
4722 ssize_t linelen;
4724 if (!view->searching) {
4725 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4726 return NULL;
4729 if (s->matched_line) {
4730 if (view->searching == TOG_SEARCH_FORWARD)
4731 lineno = s->matched_line + 1;
4732 else
4733 lineno = s->matched_line - 1;
4734 } else
4735 lineno = s->first_displayed_line - 1 + s->selected_line;
4737 while (1) {
4738 off_t offset;
4740 if (lineno <= 0 || lineno > s->blame.nlines) {
4741 if (s->matched_line == 0) {
4742 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4743 break;
4746 if (view->searching == TOG_SEARCH_FORWARD)
4747 lineno = 1;
4748 else
4749 lineno = s->blame.nlines;
4752 offset = s->blame.line_offsets[lineno - 1];
4753 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4754 free(line);
4755 return got_error_from_errno("fseeko");
4757 linelen = getline(&line, &linesize, s->blame.f);
4758 err = expand_tab(&exstr, line);
4759 if (err)
4760 break;
4761 if (linelen != -1 &&
4762 match_line(exstr, &view->regex, 1, &view->regmatch)) {
4763 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4764 s->matched_line = lineno;
4765 break;
4767 free(exstr);
4768 exstr = NULL;
4769 if (view->searching == TOG_SEARCH_FORWARD)
4770 lineno++;
4771 else
4772 lineno--;
4774 free(line);
4775 free(exstr);
4777 if (s->matched_line) {
4778 s->first_displayed_line = s->matched_line;
4779 s->selected_line = 1;
4782 return err;
4785 static const struct got_error *
4786 show_blame_view(struct tog_view *view)
4788 const struct got_error *err = NULL;
4789 struct tog_blame_view_state *s = &view->state.blame;
4790 int errcode;
4792 if (s->blame.thread == 0 && !s->blame_complete) {
4793 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4794 &s->blame.thread_args);
4795 if (errcode)
4796 return got_error_set_errno(errcode, "pthread_create");
4798 halfdelay(1); /* fast refresh while annotating */
4801 if (s->blame_complete)
4802 halfdelay(10); /* disable fast refresh */
4804 err = draw_blame(view);
4806 view_vborder(view);
4807 return err;
4810 static const struct got_error *
4811 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4813 const struct got_error *err = NULL, *thread_err = NULL;
4814 struct tog_view *diff_view;
4815 struct tog_blame_view_state *s = &view->state.blame;
4816 int begin_x = 0, nscroll = view->nlines - 2;
4818 switch (ch) {
4819 case '0':
4820 view->x = 0;
4821 break;
4822 case '$':
4823 view->x = MAX(view->maxx - view->ncols / 3, 0);
4824 break;
4825 case KEY_RIGHT:
4826 case 'l':
4827 if (view->x + view->ncols / 3 < view->maxx)
4828 view->x += 2; /* move two columns right */
4829 break;
4830 case KEY_LEFT:
4831 case 'h':
4832 view->x -= MIN(view->x, 2); /* move two columns back */
4833 break;
4834 case 'q':
4835 s->done = 1;
4836 break;
4837 case 'g':
4838 case KEY_HOME:
4839 s->selected_line = 1;
4840 s->first_displayed_line = 1;
4841 break;
4842 case 'G':
4843 case KEY_END:
4844 if (s->blame.nlines < view->nlines - 2) {
4845 s->selected_line = s->blame.nlines;
4846 s->first_displayed_line = 1;
4847 } else {
4848 s->selected_line = view->nlines - 2;
4849 s->first_displayed_line = s->blame.nlines -
4850 (view->nlines - 3);
4852 break;
4853 case 'k':
4854 case KEY_UP:
4855 case CTRL('p'):
4856 if (s->selected_line > 1)
4857 s->selected_line--;
4858 else if (s->selected_line == 1 &&
4859 s->first_displayed_line > 1)
4860 s->first_displayed_line--;
4861 break;
4862 case CTRL('u'):
4863 case 'u':
4864 nscroll /= 2;
4865 /* FALL THROUGH */
4866 case KEY_PPAGE:
4867 case CTRL('b'):
4868 if (s->first_displayed_line == 1) {
4869 s->selected_line = MAX(1, s->selected_line - nscroll);
4870 break;
4872 if (s->first_displayed_line > nscroll)
4873 s->first_displayed_line -= nscroll;
4874 else
4875 s->first_displayed_line = 1;
4876 break;
4877 case 'j':
4878 case KEY_DOWN:
4879 case CTRL('n'):
4880 if (s->selected_line < view->nlines - 2 &&
4881 s->first_displayed_line +
4882 s->selected_line <= s->blame.nlines)
4883 s->selected_line++;
4884 else if (s->last_displayed_line <
4885 s->blame.nlines)
4886 s->first_displayed_line++;
4887 break;
4888 case 'b':
4889 case 'p': {
4890 struct got_object_id *id = NULL;
4891 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4892 s->first_displayed_line, s->selected_line);
4893 if (id == NULL)
4894 break;
4895 if (ch == 'p') {
4896 struct got_commit_object *commit, *pcommit;
4897 struct got_object_qid *pid;
4898 struct got_object_id *blob_id = NULL;
4899 int obj_type;
4900 err = got_object_open_as_commit(&commit,
4901 s->repo, id);
4902 if (err)
4903 break;
4904 pid = STAILQ_FIRST(
4905 got_object_commit_get_parent_ids(commit));
4906 if (pid == NULL) {
4907 got_object_commit_close(commit);
4908 break;
4910 /* Check if path history ends here. */
4911 err = got_object_open_as_commit(&pcommit,
4912 s->repo, &pid->id);
4913 if (err)
4914 break;
4915 err = got_object_id_by_path(&blob_id, s->repo,
4916 pcommit, s->path);
4917 got_object_commit_close(pcommit);
4918 if (err) {
4919 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4920 err = NULL;
4921 got_object_commit_close(commit);
4922 break;
4924 err = got_object_get_type(&obj_type, s->repo,
4925 blob_id);
4926 free(blob_id);
4927 /* Can't blame non-blob type objects. */
4928 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4929 got_object_commit_close(commit);
4930 break;
4932 err = got_object_qid_alloc(&s->blamed_commit,
4933 &pid->id);
4934 got_object_commit_close(commit);
4935 } else {
4936 if (got_object_id_cmp(id,
4937 &s->blamed_commit->id) == 0)
4938 break;
4939 err = got_object_qid_alloc(&s->blamed_commit,
4940 id);
4942 if (err)
4943 break;
4944 s->done = 1;
4945 thread_err = stop_blame(&s->blame);
4946 s->done = 0;
4947 if (thread_err)
4948 break;
4949 STAILQ_INSERT_HEAD(&s->blamed_commits,
4950 s->blamed_commit, entry);
4951 err = run_blame(view);
4952 if (err)
4953 break;
4954 break;
4956 case 'B': {
4957 struct got_object_qid *first;
4958 first = STAILQ_FIRST(&s->blamed_commits);
4959 if (!got_object_id_cmp(&first->id, s->commit_id))
4960 break;
4961 s->done = 1;
4962 thread_err = stop_blame(&s->blame);
4963 s->done = 0;
4964 if (thread_err)
4965 break;
4966 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4967 got_object_qid_free(s->blamed_commit);
4968 s->blamed_commit =
4969 STAILQ_FIRST(&s->blamed_commits);
4970 err = run_blame(view);
4971 if (err)
4972 break;
4973 break;
4975 case KEY_ENTER:
4976 case '\r': {
4977 struct got_object_id *id = NULL;
4978 struct got_object_qid *pid;
4979 struct got_commit_object *commit = NULL;
4980 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4981 s->first_displayed_line, s->selected_line);
4982 if (id == NULL)
4983 break;
4984 err = got_object_open_as_commit(&commit, s->repo, id);
4985 if (err)
4986 break;
4987 pid = STAILQ_FIRST(
4988 got_object_commit_get_parent_ids(commit));
4989 if (view_is_parent_view(view))
4990 begin_x = view_split_begin_x(view->begin_x);
4991 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4992 if (diff_view == NULL) {
4993 got_object_commit_close(commit);
4994 err = got_error_from_errno("view_open");
4995 break;
4997 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4998 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4999 got_object_commit_close(commit);
5000 if (err) {
5001 view_close(diff_view);
5002 break;
5004 view->focussed = 0;
5005 diff_view->focussed = 1;
5006 if (view_is_parent_view(view)) {
5007 err = view_close_child(view);
5008 if (err)
5009 break;
5010 view_set_child(view, diff_view);
5011 view->focus_child = 1;
5012 } else
5013 *new_view = diff_view;
5014 if (err)
5015 break;
5016 break;
5018 case CTRL('d'):
5019 case 'd':
5020 nscroll /= 2;
5021 /* FALL THROUGH */
5022 case KEY_NPAGE:
5023 case CTRL('f'):
5024 case ' ':
5025 if (s->last_displayed_line >= s->blame.nlines &&
5026 s->selected_line >= MIN(s->blame.nlines,
5027 view->nlines - 2)) {
5028 break;
5030 if (s->last_displayed_line >= s->blame.nlines &&
5031 s->selected_line < view->nlines - 2) {
5032 s->selected_line +=
5033 MIN(nscroll, s->last_displayed_line -
5034 s->first_displayed_line - s->selected_line + 1);
5036 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5037 s->first_displayed_line += nscroll;
5038 else
5039 s->first_displayed_line =
5040 s->blame.nlines - (view->nlines - 3);
5041 break;
5042 case KEY_RESIZE:
5043 if (s->selected_line > view->nlines - 2) {
5044 s->selected_line = MIN(s->blame.nlines,
5045 view->nlines - 2);
5047 break;
5048 default:
5049 break;
5051 return thread_err ? thread_err : err;
5054 static const struct got_error *
5055 cmd_blame(int argc, char *argv[])
5057 const struct got_error *error;
5058 struct got_repository *repo = NULL;
5059 struct got_worktree *worktree = NULL;
5060 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5061 char *link_target = NULL;
5062 struct got_object_id *commit_id = NULL;
5063 struct got_commit_object *commit = NULL;
5064 char *commit_id_str = NULL;
5065 int ch;
5066 struct tog_view *view;
5067 int *pack_fds = NULL;
5069 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5070 switch (ch) {
5071 case 'c':
5072 commit_id_str = optarg;
5073 break;
5074 case 'r':
5075 repo_path = realpath(optarg, NULL);
5076 if (repo_path == NULL)
5077 return got_error_from_errno2("realpath",
5078 optarg);
5079 break;
5080 default:
5081 usage_blame();
5082 /* NOTREACHED */
5086 argc -= optind;
5087 argv += optind;
5089 if (argc != 1)
5090 usage_blame();
5092 error = got_repo_pack_fds_open(&pack_fds);
5093 if (error != NULL)
5094 goto done;
5096 if (repo_path == NULL) {
5097 cwd = getcwd(NULL, 0);
5098 if (cwd == NULL)
5099 return got_error_from_errno("getcwd");
5100 error = got_worktree_open(&worktree, cwd);
5101 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5102 goto done;
5103 if (worktree)
5104 repo_path =
5105 strdup(got_worktree_get_repo_path(worktree));
5106 else
5107 repo_path = strdup(cwd);
5108 if (repo_path == NULL) {
5109 error = got_error_from_errno("strdup");
5110 goto done;
5114 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5115 if (error != NULL)
5116 goto done;
5118 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5119 worktree);
5120 if (error)
5121 goto done;
5123 init_curses();
5125 error = apply_unveil(got_repo_get_path(repo), NULL);
5126 if (error)
5127 goto done;
5129 error = tog_load_refs(repo, 0);
5130 if (error)
5131 goto done;
5133 if (commit_id_str == NULL) {
5134 struct got_reference *head_ref;
5135 error = got_ref_open(&head_ref, repo, worktree ?
5136 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5137 if (error != NULL)
5138 goto done;
5139 error = got_ref_resolve(&commit_id, repo, head_ref);
5140 got_ref_close(head_ref);
5141 } else {
5142 error = got_repo_match_object_id(&commit_id, NULL,
5143 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5145 if (error != NULL)
5146 goto done;
5148 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5149 if (view == NULL) {
5150 error = got_error_from_errno("view_open");
5151 goto done;
5154 error = got_object_open_as_commit(&commit, repo, commit_id);
5155 if (error)
5156 goto done;
5158 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5159 commit, repo);
5160 if (error)
5161 goto done;
5163 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5164 commit_id, repo);
5165 if (error)
5166 goto done;
5167 if (worktree) {
5168 /* Release work tree lock. */
5169 got_worktree_close(worktree);
5170 worktree = NULL;
5172 error = view_loop(view);
5173 done:
5174 free(repo_path);
5175 free(in_repo_path);
5176 free(link_target);
5177 free(cwd);
5178 free(commit_id);
5179 if (commit)
5180 got_object_commit_close(commit);
5181 if (worktree)
5182 got_worktree_close(worktree);
5183 if (repo) {
5184 const struct got_error *close_err = got_repo_close(repo);
5185 if (error == NULL)
5186 error = close_err;
5188 if (pack_fds) {
5189 const struct got_error *pack_err =
5190 got_repo_pack_fds_close(pack_fds);
5191 if (error == NULL)
5192 error = pack_err;
5194 tog_free_refs();
5195 return error;
5198 static const struct got_error *
5199 draw_tree_entries(struct tog_view *view, const char *parent_path)
5201 struct tog_tree_view_state *s = &view->state.tree;
5202 const struct got_error *err = NULL;
5203 struct got_tree_entry *te;
5204 wchar_t *wline;
5205 struct tog_color *tc;
5206 int width, n, i, nentries;
5207 int limit = view->nlines;
5209 s->ndisplayed = 0;
5211 werase(view->window);
5213 if (limit == 0)
5214 return NULL;
5216 err = format_line(&wline, &width, s->tree_label, view->ncols, 0, 0);
5217 if (err)
5218 return err;
5219 if (view_needs_focus_indication(view))
5220 wstandout(view->window);
5221 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5222 if (tc)
5223 wattr_on(view->window,
5224 COLOR_PAIR(tc->colorpair), NULL);
5225 waddwstr(view->window, wline);
5226 if (tc)
5227 wattr_off(view->window,
5228 COLOR_PAIR(tc->colorpair), NULL);
5229 if (view_needs_focus_indication(view))
5230 wstandend(view->window);
5231 free(wline);
5232 wline = NULL;
5233 if (width < view->ncols - 1)
5234 waddch(view->window, '\n');
5235 if (--limit <= 0)
5236 return NULL;
5237 err = format_line(&wline, &width, parent_path, view->ncols, 0, 0);
5238 if (err)
5239 return err;
5240 waddwstr(view->window, wline);
5241 free(wline);
5242 wline = NULL;
5243 if (width < view->ncols - 1)
5244 waddch(view->window, '\n');
5245 if (--limit <= 0)
5246 return NULL;
5247 waddch(view->window, '\n');
5248 if (--limit <= 0)
5249 return NULL;
5251 if (s->first_displayed_entry == NULL) {
5252 te = got_object_tree_get_first_entry(s->tree);
5253 if (s->selected == 0) {
5254 if (view->focussed)
5255 wstandout(view->window);
5256 s->selected_entry = NULL;
5258 waddstr(view->window, " ..\n"); /* parent directory */
5259 if (s->selected == 0 && view->focussed)
5260 wstandend(view->window);
5261 s->ndisplayed++;
5262 if (--limit <= 0)
5263 return NULL;
5264 n = 1;
5265 } else {
5266 n = 0;
5267 te = s->first_displayed_entry;
5270 nentries = got_object_tree_get_nentries(s->tree);
5271 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5272 char *line = NULL, *id_str = NULL, *link_target = NULL;
5273 const char *modestr = "";
5274 mode_t mode;
5276 te = got_object_tree_get_entry(s->tree, i);
5277 mode = got_tree_entry_get_mode(te);
5279 if (s->show_ids) {
5280 err = got_object_id_str(&id_str,
5281 got_tree_entry_get_id(te));
5282 if (err)
5283 return got_error_from_errno(
5284 "got_object_id_str");
5286 if (got_object_tree_entry_is_submodule(te))
5287 modestr = "$";
5288 else if (S_ISLNK(mode)) {
5289 int i;
5291 err = got_tree_entry_get_symlink_target(&link_target,
5292 te, s->repo);
5293 if (err) {
5294 free(id_str);
5295 return err;
5297 for (i = 0; i < strlen(link_target); i++) {
5298 if (!isprint((unsigned char)link_target[i]))
5299 link_target[i] = '?';
5301 modestr = "@";
5303 else if (S_ISDIR(mode))
5304 modestr = "/";
5305 else if (mode & S_IXUSR)
5306 modestr = "*";
5307 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5308 got_tree_entry_get_name(te), modestr,
5309 link_target ? " -> ": "",
5310 link_target ? link_target : "") == -1) {
5311 free(id_str);
5312 free(link_target);
5313 return got_error_from_errno("asprintf");
5315 free(id_str);
5316 free(link_target);
5317 err = format_line(&wline, &width, line, view->ncols, 0, 0);
5318 if (err) {
5319 free(line);
5320 break;
5322 if (n == s->selected) {
5323 if (view->focussed)
5324 wstandout(view->window);
5325 s->selected_entry = te;
5327 tc = match_color(&s->colors, line);
5328 if (tc)
5329 wattr_on(view->window,
5330 COLOR_PAIR(tc->colorpair), NULL);
5331 waddwstr(view->window, wline);
5332 if (tc)
5333 wattr_off(view->window,
5334 COLOR_PAIR(tc->colorpair), NULL);
5335 if (width < view->ncols - 1)
5336 waddch(view->window, '\n');
5337 if (n == s->selected && view->focussed)
5338 wstandend(view->window);
5339 free(line);
5340 free(wline);
5341 wline = NULL;
5342 n++;
5343 s->ndisplayed++;
5344 s->last_displayed_entry = te;
5345 if (--limit <= 0)
5346 break;
5349 return err;
5352 static void
5353 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5355 struct got_tree_entry *te;
5356 int isroot = s->tree == s->root;
5357 int i = 0;
5359 if (s->first_displayed_entry == NULL)
5360 return;
5362 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5363 while (i++ < maxscroll) {
5364 if (te == NULL) {
5365 if (!isroot)
5366 s->first_displayed_entry = NULL;
5367 break;
5369 s->first_displayed_entry = te;
5370 te = got_tree_entry_get_prev(s->tree, te);
5374 static void
5375 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5377 struct got_tree_entry *next, *last;
5378 int n = 0;
5380 if (s->first_displayed_entry)
5381 next = got_tree_entry_get_next(s->tree,
5382 s->first_displayed_entry);
5383 else
5384 next = got_object_tree_get_first_entry(s->tree);
5386 last = s->last_displayed_entry;
5387 while (next && last && n++ < maxscroll) {
5388 last = got_tree_entry_get_next(s->tree, last);
5389 if (last) {
5390 s->first_displayed_entry = next;
5391 next = got_tree_entry_get_next(s->tree, next);
5396 static const struct got_error *
5397 tree_entry_path(char **path, struct tog_parent_trees *parents,
5398 struct got_tree_entry *te)
5400 const struct got_error *err = NULL;
5401 struct tog_parent_tree *pt;
5402 size_t len = 2; /* for leading slash and NUL */
5404 TAILQ_FOREACH(pt, parents, entry)
5405 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5406 + 1 /* slash */;
5407 if (te)
5408 len += strlen(got_tree_entry_get_name(te));
5410 *path = calloc(1, len);
5411 if (path == NULL)
5412 return got_error_from_errno("calloc");
5414 (*path)[0] = '/';
5415 pt = TAILQ_LAST(parents, tog_parent_trees);
5416 while (pt) {
5417 const char *name = got_tree_entry_get_name(pt->selected_entry);
5418 if (strlcat(*path, name, len) >= len) {
5419 err = got_error(GOT_ERR_NO_SPACE);
5420 goto done;
5422 if (strlcat(*path, "/", len) >= len) {
5423 err = got_error(GOT_ERR_NO_SPACE);
5424 goto done;
5426 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5428 if (te) {
5429 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5430 err = got_error(GOT_ERR_NO_SPACE);
5431 goto done;
5434 done:
5435 if (err) {
5436 free(*path);
5437 *path = NULL;
5439 return err;
5442 static const struct got_error *
5443 blame_tree_entry(struct tog_view **new_view, int begin_x,
5444 struct got_tree_entry *te, struct tog_parent_trees *parents,
5445 struct got_object_id *commit_id, struct got_repository *repo)
5447 const struct got_error *err = NULL;
5448 char *path;
5449 struct tog_view *blame_view;
5451 *new_view = NULL;
5453 err = tree_entry_path(&path, parents, te);
5454 if (err)
5455 return err;
5457 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5458 if (blame_view == NULL) {
5459 err = got_error_from_errno("view_open");
5460 goto done;
5463 err = open_blame_view(blame_view, path, commit_id, repo);
5464 if (err) {
5465 if (err->code == GOT_ERR_CANCELLED)
5466 err = NULL;
5467 view_close(blame_view);
5468 } else
5469 *new_view = blame_view;
5470 done:
5471 free(path);
5472 return err;
5475 static const struct got_error *
5476 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5477 struct tog_tree_view_state *s)
5479 struct tog_view *log_view;
5480 const struct got_error *err = NULL;
5481 char *path;
5483 *new_view = NULL;
5485 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5486 if (log_view == NULL)
5487 return got_error_from_errno("view_open");
5489 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5490 if (err)
5491 return err;
5493 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5494 path, 0);
5495 if (err)
5496 view_close(log_view);
5497 else
5498 *new_view = log_view;
5499 free(path);
5500 return err;
5503 static const struct got_error *
5504 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5505 const char *head_ref_name, struct got_repository *repo)
5507 const struct got_error *err = NULL;
5508 char *commit_id_str = NULL;
5509 struct tog_tree_view_state *s = &view->state.tree;
5510 struct got_commit_object *commit = NULL;
5512 TAILQ_INIT(&s->parents);
5513 STAILQ_INIT(&s->colors);
5515 s->commit_id = got_object_id_dup(commit_id);
5516 if (s->commit_id == NULL)
5517 return got_error_from_errno("got_object_id_dup");
5519 err = got_object_open_as_commit(&commit, repo, commit_id);
5520 if (err)
5521 goto done;
5524 * The root is opened here and will be closed when the view is closed.
5525 * Any visited subtrees and their path-wise parents are opened and
5526 * closed on demand.
5528 err = got_object_open_as_tree(&s->root, repo,
5529 got_object_commit_get_tree_id(commit));
5530 if (err)
5531 goto done;
5532 s->tree = s->root;
5534 err = got_object_id_str(&commit_id_str, commit_id);
5535 if (err != NULL)
5536 goto done;
5538 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5539 err = got_error_from_errno("asprintf");
5540 goto done;
5543 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5544 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5545 if (head_ref_name) {
5546 s->head_ref_name = strdup(head_ref_name);
5547 if (s->head_ref_name == NULL) {
5548 err = got_error_from_errno("strdup");
5549 goto done;
5552 s->repo = repo;
5554 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5555 err = add_color(&s->colors, "\\$$",
5556 TOG_COLOR_TREE_SUBMODULE,
5557 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5558 if (err)
5559 goto done;
5560 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5561 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5562 if (err)
5563 goto done;
5564 err = add_color(&s->colors, "/$",
5565 TOG_COLOR_TREE_DIRECTORY,
5566 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5567 if (err)
5568 goto done;
5570 err = add_color(&s->colors, "\\*$",
5571 TOG_COLOR_TREE_EXECUTABLE,
5572 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5573 if (err)
5574 goto done;
5576 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5577 get_color_value("TOG_COLOR_COMMIT"));
5578 if (err)
5579 goto done;
5582 view->show = show_tree_view;
5583 view->input = input_tree_view;
5584 view->close = close_tree_view;
5585 view->search_start = search_start_tree_view;
5586 view->search_next = search_next_tree_view;
5587 done:
5588 free(commit_id_str);
5589 if (commit)
5590 got_object_commit_close(commit);
5591 if (err)
5592 close_tree_view(view);
5593 return err;
5596 static const struct got_error *
5597 close_tree_view(struct tog_view *view)
5599 struct tog_tree_view_state *s = &view->state.tree;
5601 free_colors(&s->colors);
5602 free(s->tree_label);
5603 s->tree_label = NULL;
5604 free(s->commit_id);
5605 s->commit_id = NULL;
5606 free(s->head_ref_name);
5607 s->head_ref_name = NULL;
5608 while (!TAILQ_EMPTY(&s->parents)) {
5609 struct tog_parent_tree *parent;
5610 parent = TAILQ_FIRST(&s->parents);
5611 TAILQ_REMOVE(&s->parents, parent, entry);
5612 if (parent->tree != s->root)
5613 got_object_tree_close(parent->tree);
5614 free(parent);
5617 if (s->tree != NULL && s->tree != s->root)
5618 got_object_tree_close(s->tree);
5619 if (s->root)
5620 got_object_tree_close(s->root);
5621 return NULL;
5624 static const struct got_error *
5625 search_start_tree_view(struct tog_view *view)
5627 struct tog_tree_view_state *s = &view->state.tree;
5629 s->matched_entry = NULL;
5630 return NULL;
5633 static int
5634 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5636 regmatch_t regmatch;
5638 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5639 0) == 0;
5642 static const struct got_error *
5643 search_next_tree_view(struct tog_view *view)
5645 struct tog_tree_view_state *s = &view->state.tree;
5646 struct got_tree_entry *te = NULL;
5648 if (!view->searching) {
5649 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5650 return NULL;
5653 if (s->matched_entry) {
5654 if (view->searching == TOG_SEARCH_FORWARD) {
5655 if (s->selected_entry)
5656 te = got_tree_entry_get_next(s->tree,
5657 s->selected_entry);
5658 else
5659 te = got_object_tree_get_first_entry(s->tree);
5660 } else {
5661 if (s->selected_entry == NULL)
5662 te = got_object_tree_get_last_entry(s->tree);
5663 else
5664 te = got_tree_entry_get_prev(s->tree,
5665 s->selected_entry);
5667 } else {
5668 if (s->selected_entry)
5669 te = s->selected_entry;
5670 else if (view->searching == TOG_SEARCH_FORWARD)
5671 te = got_object_tree_get_first_entry(s->tree);
5672 else
5673 te = got_object_tree_get_last_entry(s->tree);
5676 while (1) {
5677 if (te == NULL) {
5678 if (s->matched_entry == NULL) {
5679 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5680 return NULL;
5682 if (view->searching == TOG_SEARCH_FORWARD)
5683 te = got_object_tree_get_first_entry(s->tree);
5684 else
5685 te = got_object_tree_get_last_entry(s->tree);
5688 if (match_tree_entry(te, &view->regex)) {
5689 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5690 s->matched_entry = te;
5691 break;
5694 if (view->searching == TOG_SEARCH_FORWARD)
5695 te = got_tree_entry_get_next(s->tree, te);
5696 else
5697 te = got_tree_entry_get_prev(s->tree, te);
5700 if (s->matched_entry) {
5701 s->first_displayed_entry = s->matched_entry;
5702 s->selected = 0;
5705 return NULL;
5708 static const struct got_error *
5709 show_tree_view(struct tog_view *view)
5711 const struct got_error *err = NULL;
5712 struct tog_tree_view_state *s = &view->state.tree;
5713 char *parent_path;
5715 err = tree_entry_path(&parent_path, &s->parents, NULL);
5716 if (err)
5717 return err;
5719 err = draw_tree_entries(view, parent_path);
5720 free(parent_path);
5722 view_vborder(view);
5723 return err;
5726 static const struct got_error *
5727 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5729 const struct got_error *err = NULL;
5730 struct tog_tree_view_state *s = &view->state.tree;
5731 struct tog_view *log_view, *ref_view;
5732 struct got_tree_entry *te;
5733 int begin_x = 0, n, nscroll = view->nlines - 3;
5735 switch (ch) {
5736 case 'i':
5737 s->show_ids = !s->show_ids;
5738 break;
5739 case 'l':
5740 if (!s->selected_entry)
5741 break;
5742 if (view_is_parent_view(view))
5743 begin_x = view_split_begin_x(view->begin_x);
5744 err = log_selected_tree_entry(&log_view, begin_x, s);
5745 view->focussed = 0;
5746 log_view->focussed = 1;
5747 if (view_is_parent_view(view)) {
5748 err = view_close_child(view);
5749 if (err)
5750 return err;
5751 view_set_child(view, log_view);
5752 view->focus_child = 1;
5753 } else
5754 *new_view = log_view;
5755 break;
5756 case 'r':
5757 if (view_is_parent_view(view))
5758 begin_x = view_split_begin_x(view->begin_x);
5759 ref_view = view_open(view->nlines, view->ncols,
5760 view->begin_y, begin_x, TOG_VIEW_REF);
5761 if (ref_view == NULL)
5762 return got_error_from_errno("view_open");
5763 err = open_ref_view(ref_view, s->repo);
5764 if (err) {
5765 view_close(ref_view);
5766 return err;
5768 view->focussed = 0;
5769 ref_view->focussed = 1;
5770 if (view_is_parent_view(view)) {
5771 err = view_close_child(view);
5772 if (err)
5773 return err;
5774 view_set_child(view, ref_view);
5775 view->focus_child = 1;
5776 } else
5777 *new_view = ref_view;
5778 break;
5779 case 'g':
5780 case KEY_HOME:
5781 s->selected = 0;
5782 if (s->tree == s->root)
5783 s->first_displayed_entry =
5784 got_object_tree_get_first_entry(s->tree);
5785 else
5786 s->first_displayed_entry = NULL;
5787 break;
5788 case 'G':
5789 case KEY_END:
5790 s->selected = 0;
5791 te = got_object_tree_get_last_entry(s->tree);
5792 for (n = 0; n < view->nlines - 3; n++) {
5793 if (te == NULL) {
5794 if(s->tree != s->root) {
5795 s->first_displayed_entry = NULL;
5796 n++;
5798 break;
5800 s->first_displayed_entry = te;
5801 te = got_tree_entry_get_prev(s->tree, te);
5803 if (n > 0)
5804 s->selected = n - 1;
5805 break;
5806 case 'k':
5807 case KEY_UP:
5808 case CTRL('p'):
5809 if (s->selected > 0) {
5810 s->selected--;
5811 break;
5813 tree_scroll_up(s, 1);
5814 break;
5815 case CTRL('u'):
5816 case 'u':
5817 nscroll /= 2;
5818 /* FALL THROUGH */
5819 case KEY_PPAGE:
5820 case CTRL('b'):
5821 if (s->tree == s->root) {
5822 if (got_object_tree_get_first_entry(s->tree) ==
5823 s->first_displayed_entry)
5824 s->selected -= MIN(s->selected, nscroll);
5825 } else {
5826 if (s->first_displayed_entry == NULL)
5827 s->selected -= MIN(s->selected, nscroll);
5829 tree_scroll_up(s, MAX(0, nscroll));
5830 break;
5831 case 'j':
5832 case KEY_DOWN:
5833 case CTRL('n'):
5834 if (s->selected < s->ndisplayed - 1) {
5835 s->selected++;
5836 break;
5838 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5839 == NULL)
5840 /* can't scroll any further */
5841 break;
5842 tree_scroll_down(s, 1);
5843 break;
5844 case CTRL('d'):
5845 case 'd':
5846 nscroll /= 2;
5847 /* FALL THROUGH */
5848 case KEY_NPAGE:
5849 case CTRL('f'):
5850 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5851 == NULL) {
5852 /* can't scroll any further; move cursor down */
5853 if (s->selected < s->ndisplayed - 1)
5854 s->selected += MIN(nscroll,
5855 s->ndisplayed - s->selected - 1);
5856 break;
5858 tree_scroll_down(s, nscroll);
5859 break;
5860 case KEY_ENTER:
5861 case '\r':
5862 case KEY_BACKSPACE:
5863 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5864 struct tog_parent_tree *parent;
5865 /* user selected '..' */
5866 if (s->tree == s->root)
5867 break;
5868 parent = TAILQ_FIRST(&s->parents);
5869 TAILQ_REMOVE(&s->parents, parent,
5870 entry);
5871 got_object_tree_close(s->tree);
5872 s->tree = parent->tree;
5873 s->first_displayed_entry =
5874 parent->first_displayed_entry;
5875 s->selected_entry =
5876 parent->selected_entry;
5877 s->selected = parent->selected;
5878 free(parent);
5879 } else if (S_ISDIR(got_tree_entry_get_mode(
5880 s->selected_entry))) {
5881 struct got_tree_object *subtree;
5882 err = got_object_open_as_tree(&subtree, s->repo,
5883 got_tree_entry_get_id(s->selected_entry));
5884 if (err)
5885 break;
5886 err = tree_view_visit_subtree(s, subtree);
5887 if (err) {
5888 got_object_tree_close(subtree);
5889 break;
5891 } else if (S_ISREG(got_tree_entry_get_mode(
5892 s->selected_entry))) {
5893 struct tog_view *blame_view;
5894 int begin_x = view_is_parent_view(view) ?
5895 view_split_begin_x(view->begin_x) : 0;
5897 err = blame_tree_entry(&blame_view, begin_x,
5898 s->selected_entry, &s->parents,
5899 s->commit_id, s->repo);
5900 if (err)
5901 break;
5902 view->focussed = 0;
5903 blame_view->focussed = 1;
5904 if (view_is_parent_view(view)) {
5905 err = view_close_child(view);
5906 if (err)
5907 return err;
5908 view_set_child(view, blame_view);
5909 view->focus_child = 1;
5910 } else
5911 *new_view = blame_view;
5913 break;
5914 case KEY_RESIZE:
5915 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5916 s->selected = view->nlines - 4;
5917 break;
5918 default:
5919 break;
5922 return err;
5925 __dead static void
5926 usage_tree(void)
5928 endwin();
5929 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5930 getprogname());
5931 exit(1);
5934 static const struct got_error *
5935 cmd_tree(int argc, char *argv[])
5937 const struct got_error *error;
5938 struct got_repository *repo = NULL;
5939 struct got_worktree *worktree = NULL;
5940 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5941 struct got_object_id *commit_id = NULL;
5942 struct got_commit_object *commit = NULL;
5943 const char *commit_id_arg = NULL;
5944 char *label = NULL;
5945 struct got_reference *ref = NULL;
5946 const char *head_ref_name = NULL;
5947 int ch;
5948 struct tog_view *view;
5949 int *pack_fds = NULL;
5951 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5952 switch (ch) {
5953 case 'c':
5954 commit_id_arg = optarg;
5955 break;
5956 case 'r':
5957 repo_path = realpath(optarg, NULL);
5958 if (repo_path == NULL)
5959 return got_error_from_errno2("realpath",
5960 optarg);
5961 break;
5962 default:
5963 usage_tree();
5964 /* NOTREACHED */
5968 argc -= optind;
5969 argv += optind;
5971 if (argc > 1)
5972 usage_tree();
5974 error = got_repo_pack_fds_open(&pack_fds);
5975 if (error != NULL)
5976 goto done;
5978 if (repo_path == NULL) {
5979 cwd = getcwd(NULL, 0);
5980 if (cwd == NULL)
5981 return got_error_from_errno("getcwd");
5982 error = got_worktree_open(&worktree, cwd);
5983 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5984 goto done;
5985 if (worktree)
5986 repo_path =
5987 strdup(got_worktree_get_repo_path(worktree));
5988 else
5989 repo_path = strdup(cwd);
5990 if (repo_path == NULL) {
5991 error = got_error_from_errno("strdup");
5992 goto done;
5996 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5997 if (error != NULL)
5998 goto done;
6000 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6001 repo, worktree);
6002 if (error)
6003 goto done;
6005 init_curses();
6007 error = apply_unveil(got_repo_get_path(repo), NULL);
6008 if (error)
6009 goto done;
6011 error = tog_load_refs(repo, 0);
6012 if (error)
6013 goto done;
6015 if (commit_id_arg == NULL) {
6016 error = got_repo_match_object_id(&commit_id, &label,
6017 worktree ? got_worktree_get_head_ref_name(worktree) :
6018 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6019 if (error)
6020 goto done;
6021 head_ref_name = label;
6022 } else {
6023 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6024 if (error == NULL)
6025 head_ref_name = got_ref_get_name(ref);
6026 else if (error->code != GOT_ERR_NOT_REF)
6027 goto done;
6028 error = got_repo_match_object_id(&commit_id, NULL,
6029 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6030 if (error)
6031 goto done;
6034 error = got_object_open_as_commit(&commit, repo, commit_id);
6035 if (error)
6036 goto done;
6038 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6039 if (view == NULL) {
6040 error = got_error_from_errno("view_open");
6041 goto done;
6043 error = open_tree_view(view, commit_id, head_ref_name, repo);
6044 if (error)
6045 goto done;
6046 if (!got_path_is_root_dir(in_repo_path)) {
6047 error = tree_view_walk_path(&view->state.tree, commit,
6048 in_repo_path);
6049 if (error)
6050 goto done;
6053 if (worktree) {
6054 /* Release work tree lock. */
6055 got_worktree_close(worktree);
6056 worktree = NULL;
6058 error = view_loop(view);
6059 done:
6060 free(repo_path);
6061 free(cwd);
6062 free(commit_id);
6063 free(label);
6064 if (ref)
6065 got_ref_close(ref);
6066 if (repo) {
6067 const struct got_error *close_err = got_repo_close(repo);
6068 if (error == NULL)
6069 error = close_err;
6071 if (pack_fds) {
6072 const struct got_error *pack_err =
6073 got_repo_pack_fds_close(pack_fds);
6074 if (error == NULL)
6075 error = pack_err;
6077 tog_free_refs();
6078 return error;
6081 static const struct got_error *
6082 ref_view_load_refs(struct tog_ref_view_state *s)
6084 struct got_reflist_entry *sre;
6085 struct tog_reflist_entry *re;
6087 s->nrefs = 0;
6088 TAILQ_FOREACH(sre, &tog_refs, entry) {
6089 if (strncmp(got_ref_get_name(sre->ref),
6090 "refs/got/", 9) == 0 &&
6091 strncmp(got_ref_get_name(sre->ref),
6092 "refs/got/backup/", 16) != 0)
6093 continue;
6095 re = malloc(sizeof(*re));
6096 if (re == NULL)
6097 return got_error_from_errno("malloc");
6099 re->ref = got_ref_dup(sre->ref);
6100 if (re->ref == NULL)
6101 return got_error_from_errno("got_ref_dup");
6102 re->idx = s->nrefs++;
6103 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6106 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6107 return NULL;
6110 void
6111 ref_view_free_refs(struct tog_ref_view_state *s)
6113 struct tog_reflist_entry *re;
6115 while (!TAILQ_EMPTY(&s->refs)) {
6116 re = TAILQ_FIRST(&s->refs);
6117 TAILQ_REMOVE(&s->refs, re, entry);
6118 got_ref_close(re->ref);
6119 free(re);
6123 static const struct got_error *
6124 open_ref_view(struct tog_view *view, struct got_repository *repo)
6126 const struct got_error *err = NULL;
6127 struct tog_ref_view_state *s = &view->state.ref;
6129 s->selected_entry = 0;
6130 s->repo = repo;
6132 TAILQ_INIT(&s->refs);
6133 STAILQ_INIT(&s->colors);
6135 err = ref_view_load_refs(s);
6136 if (err)
6137 return err;
6139 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6140 err = add_color(&s->colors, "^refs/heads/",
6141 TOG_COLOR_REFS_HEADS,
6142 get_color_value("TOG_COLOR_REFS_HEADS"));
6143 if (err)
6144 goto done;
6146 err = add_color(&s->colors, "^refs/tags/",
6147 TOG_COLOR_REFS_TAGS,
6148 get_color_value("TOG_COLOR_REFS_TAGS"));
6149 if (err)
6150 goto done;
6152 err = add_color(&s->colors, "^refs/remotes/",
6153 TOG_COLOR_REFS_REMOTES,
6154 get_color_value("TOG_COLOR_REFS_REMOTES"));
6155 if (err)
6156 goto done;
6158 err = add_color(&s->colors, "^refs/got/backup/",
6159 TOG_COLOR_REFS_BACKUP,
6160 get_color_value("TOG_COLOR_REFS_BACKUP"));
6161 if (err)
6162 goto done;
6165 view->show = show_ref_view;
6166 view->input = input_ref_view;
6167 view->close = close_ref_view;
6168 view->search_start = search_start_ref_view;
6169 view->search_next = search_next_ref_view;
6170 done:
6171 if (err)
6172 free_colors(&s->colors);
6173 return err;
6176 static const struct got_error *
6177 close_ref_view(struct tog_view *view)
6179 struct tog_ref_view_state *s = &view->state.ref;
6181 ref_view_free_refs(s);
6182 free_colors(&s->colors);
6184 return NULL;
6187 static const struct got_error *
6188 resolve_reflist_entry(struct got_object_id **commit_id,
6189 struct tog_reflist_entry *re, struct got_repository *repo)
6191 const struct got_error *err = NULL;
6192 struct got_object_id *obj_id;
6193 struct got_tag_object *tag = NULL;
6194 int obj_type;
6196 *commit_id = NULL;
6198 err = got_ref_resolve(&obj_id, repo, re->ref);
6199 if (err)
6200 return err;
6202 err = got_object_get_type(&obj_type, repo, obj_id);
6203 if (err)
6204 goto done;
6206 switch (obj_type) {
6207 case GOT_OBJ_TYPE_COMMIT:
6208 *commit_id = obj_id;
6209 break;
6210 case GOT_OBJ_TYPE_TAG:
6211 err = got_object_open_as_tag(&tag, repo, obj_id);
6212 if (err)
6213 goto done;
6214 free(obj_id);
6215 err = got_object_get_type(&obj_type, repo,
6216 got_object_tag_get_object_id(tag));
6217 if (err)
6218 goto done;
6219 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6220 err = got_error(GOT_ERR_OBJ_TYPE);
6221 goto done;
6223 *commit_id = got_object_id_dup(
6224 got_object_tag_get_object_id(tag));
6225 if (*commit_id == NULL) {
6226 err = got_error_from_errno("got_object_id_dup");
6227 goto done;
6229 break;
6230 default:
6231 err = got_error(GOT_ERR_OBJ_TYPE);
6232 break;
6235 done:
6236 if (tag)
6237 got_object_tag_close(tag);
6238 if (err) {
6239 free(*commit_id);
6240 *commit_id = NULL;
6242 return err;
6245 static const struct got_error *
6246 log_ref_entry(struct tog_view **new_view, int begin_x,
6247 struct tog_reflist_entry *re, struct got_repository *repo)
6249 struct tog_view *log_view;
6250 const struct got_error *err = NULL;
6251 struct got_object_id *commit_id = NULL;
6253 *new_view = NULL;
6255 err = resolve_reflist_entry(&commit_id, re, repo);
6256 if (err) {
6257 if (err->code != GOT_ERR_OBJ_TYPE)
6258 return err;
6259 else
6260 return NULL;
6263 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6264 if (log_view == NULL) {
6265 err = got_error_from_errno("view_open");
6266 goto done;
6269 err = open_log_view(log_view, commit_id, repo,
6270 got_ref_get_name(re->ref), "", 0);
6271 done:
6272 if (err)
6273 view_close(log_view);
6274 else
6275 *new_view = log_view;
6276 free(commit_id);
6277 return err;
6280 static void
6281 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6283 struct tog_reflist_entry *re;
6284 int i = 0;
6286 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6287 return;
6289 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6290 while (i++ < maxscroll) {
6291 if (re == NULL)
6292 break;
6293 s->first_displayed_entry = re;
6294 re = TAILQ_PREV(re, tog_reflist_head, entry);
6298 static void
6299 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6301 struct tog_reflist_entry *next, *last;
6302 int n = 0;
6304 if (s->first_displayed_entry)
6305 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6306 else
6307 next = TAILQ_FIRST(&s->refs);
6309 last = s->last_displayed_entry;
6310 while (next && last && n++ < maxscroll) {
6311 last = TAILQ_NEXT(last, entry);
6312 if (last) {
6313 s->first_displayed_entry = next;
6314 next = TAILQ_NEXT(next, entry);
6319 static const struct got_error *
6320 search_start_ref_view(struct tog_view *view)
6322 struct tog_ref_view_state *s = &view->state.ref;
6324 s->matched_entry = NULL;
6325 return NULL;
6328 static int
6329 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6331 regmatch_t regmatch;
6333 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6334 0) == 0;
6337 static const struct got_error *
6338 search_next_ref_view(struct tog_view *view)
6340 struct tog_ref_view_state *s = &view->state.ref;
6341 struct tog_reflist_entry *re = NULL;
6343 if (!view->searching) {
6344 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6345 return NULL;
6348 if (s->matched_entry) {
6349 if (view->searching == TOG_SEARCH_FORWARD) {
6350 if (s->selected_entry)
6351 re = TAILQ_NEXT(s->selected_entry, entry);
6352 else
6353 re = TAILQ_PREV(s->selected_entry,
6354 tog_reflist_head, entry);
6355 } else {
6356 if (s->selected_entry == NULL)
6357 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6358 else
6359 re = TAILQ_PREV(s->selected_entry,
6360 tog_reflist_head, entry);
6362 } else {
6363 if (s->selected_entry)
6364 re = s->selected_entry;
6365 else if (view->searching == TOG_SEARCH_FORWARD)
6366 re = TAILQ_FIRST(&s->refs);
6367 else
6368 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6371 while (1) {
6372 if (re == NULL) {
6373 if (s->matched_entry == NULL) {
6374 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6375 return NULL;
6377 if (view->searching == TOG_SEARCH_FORWARD)
6378 re = TAILQ_FIRST(&s->refs);
6379 else
6380 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6383 if (match_reflist_entry(re, &view->regex)) {
6384 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6385 s->matched_entry = re;
6386 break;
6389 if (view->searching == TOG_SEARCH_FORWARD)
6390 re = TAILQ_NEXT(re, entry);
6391 else
6392 re = TAILQ_PREV(re, tog_reflist_head, entry);
6395 if (s->matched_entry) {
6396 s->first_displayed_entry = s->matched_entry;
6397 s->selected = 0;
6400 return NULL;
6403 static const struct got_error *
6404 show_ref_view(struct tog_view *view)
6406 const struct got_error *err = NULL;
6407 struct tog_ref_view_state *s = &view->state.ref;
6408 struct tog_reflist_entry *re;
6409 char *line = NULL;
6410 wchar_t *wline;
6411 struct tog_color *tc;
6412 int width, n;
6413 int limit = view->nlines;
6415 werase(view->window);
6417 s->ndisplayed = 0;
6419 if (limit == 0)
6420 return NULL;
6422 re = s->first_displayed_entry;
6424 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6425 s->nrefs) == -1)
6426 return got_error_from_errno("asprintf");
6428 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6429 if (err) {
6430 free(line);
6431 return err;
6433 if (view_needs_focus_indication(view))
6434 wstandout(view->window);
6435 waddwstr(view->window, wline);
6436 if (view_needs_focus_indication(view))
6437 wstandend(view->window);
6438 free(wline);
6439 wline = NULL;
6440 free(line);
6441 line = NULL;
6442 if (width < view->ncols - 1)
6443 waddch(view->window, '\n');
6444 if (--limit <= 0)
6445 return NULL;
6447 n = 0;
6448 while (re && limit > 0) {
6449 char *line = NULL;
6450 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6452 if (s->show_date) {
6453 struct got_commit_object *ci;
6454 struct got_tag_object *tag;
6455 struct got_object_id *id;
6456 struct tm tm;
6457 time_t t;
6459 err = got_ref_resolve(&id, s->repo, re->ref);
6460 if (err)
6461 return err;
6462 err = got_object_open_as_tag(&tag, s->repo, id);
6463 if (err) {
6464 if (err->code != GOT_ERR_OBJ_TYPE) {
6465 free(id);
6466 return err;
6468 err = got_object_open_as_commit(&ci, s->repo,
6469 id);
6470 if (err) {
6471 free(id);
6472 return err;
6474 t = got_object_commit_get_committer_time(ci);
6475 got_object_commit_close(ci);
6476 } else {
6477 t = got_object_tag_get_tagger_time(tag);
6478 got_object_tag_close(tag);
6480 free(id);
6481 if (gmtime_r(&t, &tm) == NULL)
6482 return got_error_from_errno("gmtime_r");
6483 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6484 return got_error(GOT_ERR_NO_SPACE);
6486 if (got_ref_is_symbolic(re->ref)) {
6487 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6488 ymd : "", got_ref_get_name(re->ref),
6489 got_ref_get_symref_target(re->ref)) == -1)
6490 return got_error_from_errno("asprintf");
6491 } else if (s->show_ids) {
6492 struct got_object_id *id;
6493 char *id_str;
6494 err = got_ref_resolve(&id, s->repo, re->ref);
6495 if (err)
6496 return err;
6497 err = got_object_id_str(&id_str, id);
6498 if (err) {
6499 free(id);
6500 return err;
6502 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6503 got_ref_get_name(re->ref), id_str) == -1) {
6504 err = got_error_from_errno("asprintf");
6505 free(id);
6506 free(id_str);
6507 return err;
6509 free(id);
6510 free(id_str);
6511 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6512 got_ref_get_name(re->ref)) == -1)
6513 return got_error_from_errno("asprintf");
6515 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6516 if (err) {
6517 free(line);
6518 return err;
6520 if (n == s->selected) {
6521 if (view->focussed)
6522 wstandout(view->window);
6523 s->selected_entry = re;
6525 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6526 if (tc)
6527 wattr_on(view->window,
6528 COLOR_PAIR(tc->colorpair), NULL);
6529 waddwstr(view->window, wline);
6530 if (tc)
6531 wattr_off(view->window,
6532 COLOR_PAIR(tc->colorpair), NULL);
6533 if (width < view->ncols - 1)
6534 waddch(view->window, '\n');
6535 if (n == s->selected && view->focussed)
6536 wstandend(view->window);
6537 free(line);
6538 free(wline);
6539 wline = NULL;
6540 n++;
6541 s->ndisplayed++;
6542 s->last_displayed_entry = re;
6544 limit--;
6545 re = TAILQ_NEXT(re, entry);
6548 view_vborder(view);
6549 return err;
6552 static const struct got_error *
6553 browse_ref_tree(struct tog_view **new_view, int begin_x,
6554 struct tog_reflist_entry *re, struct got_repository *repo)
6556 const struct got_error *err = NULL;
6557 struct got_object_id *commit_id = NULL;
6558 struct tog_view *tree_view;
6560 *new_view = NULL;
6562 err = resolve_reflist_entry(&commit_id, re, repo);
6563 if (err) {
6564 if (err->code != GOT_ERR_OBJ_TYPE)
6565 return err;
6566 else
6567 return NULL;
6571 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6572 if (tree_view == NULL) {
6573 err = got_error_from_errno("view_open");
6574 goto done;
6577 err = open_tree_view(tree_view, commit_id,
6578 got_ref_get_name(re->ref), repo);
6579 if (err)
6580 goto done;
6582 *new_view = tree_view;
6583 done:
6584 free(commit_id);
6585 return err;
6587 static const struct got_error *
6588 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6590 const struct got_error *err = NULL;
6591 struct tog_ref_view_state *s = &view->state.ref;
6592 struct tog_view *log_view, *tree_view;
6593 struct tog_reflist_entry *re;
6594 int begin_x = 0, n, nscroll = view->nlines - 1;
6596 switch (ch) {
6597 case 'i':
6598 s->show_ids = !s->show_ids;
6599 break;
6600 case 'm':
6601 s->show_date = !s->show_date;
6602 break;
6603 case 'o':
6604 s->sort_by_date = !s->sort_by_date;
6605 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6606 got_ref_cmp_by_commit_timestamp_descending :
6607 tog_ref_cmp_by_name, s->repo);
6608 if (err)
6609 break;
6610 got_reflist_object_id_map_free(tog_refs_idmap);
6611 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6612 &tog_refs, s->repo);
6613 if (err)
6614 break;
6615 ref_view_free_refs(s);
6616 err = ref_view_load_refs(s);
6617 break;
6618 case KEY_ENTER:
6619 case '\r':
6620 if (!s->selected_entry)
6621 break;
6622 if (view_is_parent_view(view))
6623 begin_x = view_split_begin_x(view->begin_x);
6624 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6625 s->repo);
6626 view->focussed = 0;
6627 log_view->focussed = 1;
6628 if (view_is_parent_view(view)) {
6629 err = view_close_child(view);
6630 if (err)
6631 return err;
6632 view_set_child(view, log_view);
6633 view->focus_child = 1;
6634 } else
6635 *new_view = log_view;
6636 break;
6637 case 't':
6638 if (!s->selected_entry)
6639 break;
6640 if (view_is_parent_view(view))
6641 begin_x = view_split_begin_x(view->begin_x);
6642 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6643 s->repo);
6644 if (err || tree_view == NULL)
6645 break;
6646 view->focussed = 0;
6647 tree_view->focussed = 1;
6648 if (view_is_parent_view(view)) {
6649 err = view_close_child(view);
6650 if (err)
6651 return err;
6652 view_set_child(view, tree_view);
6653 view->focus_child = 1;
6654 } else
6655 *new_view = tree_view;
6656 break;
6657 case 'g':
6658 case KEY_HOME:
6659 s->selected = 0;
6660 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6661 break;
6662 case 'G':
6663 case KEY_END:
6664 s->selected = 0;
6665 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6666 for (n = 0; n < view->nlines - 1; n++) {
6667 if (re == NULL)
6668 break;
6669 s->first_displayed_entry = re;
6670 re = TAILQ_PREV(re, tog_reflist_head, entry);
6672 if (n > 0)
6673 s->selected = n - 1;
6674 break;
6675 case 'k':
6676 case KEY_UP:
6677 case CTRL('p'):
6678 if (s->selected > 0) {
6679 s->selected--;
6680 break;
6682 ref_scroll_up(s, 1);
6683 break;
6684 case CTRL('u'):
6685 case 'u':
6686 nscroll /= 2;
6687 /* FALL THROUGH */
6688 case KEY_PPAGE:
6689 case CTRL('b'):
6690 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6691 s->selected -= MIN(nscroll, s->selected);
6692 ref_scroll_up(s, MAX(0, nscroll));
6693 break;
6694 case 'j':
6695 case KEY_DOWN:
6696 case CTRL('n'):
6697 if (s->selected < s->ndisplayed - 1) {
6698 s->selected++;
6699 break;
6701 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6702 /* can't scroll any further */
6703 break;
6704 ref_scroll_down(s, 1);
6705 break;
6706 case CTRL('d'):
6707 case 'd':
6708 nscroll /= 2;
6709 /* FALL THROUGH */
6710 case KEY_NPAGE:
6711 case CTRL('f'):
6712 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6713 /* can't scroll any further; move cursor down */
6714 if (s->selected < s->ndisplayed - 1)
6715 s->selected += MIN(nscroll,
6716 s->ndisplayed - s->selected - 1);
6717 break;
6719 ref_scroll_down(s, nscroll);
6720 break;
6721 case CTRL('l'):
6722 tog_free_refs();
6723 err = tog_load_refs(s->repo, s->sort_by_date);
6724 if (err)
6725 break;
6726 ref_view_free_refs(s);
6727 err = ref_view_load_refs(s);
6728 break;
6729 case KEY_RESIZE:
6730 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6731 s->selected = view->nlines - 2;
6732 break;
6733 default:
6734 break;
6737 return err;
6740 __dead static void
6741 usage_ref(void)
6743 endwin();
6744 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6745 getprogname());
6746 exit(1);
6749 static const struct got_error *
6750 cmd_ref(int argc, char *argv[])
6752 const struct got_error *error;
6753 struct got_repository *repo = NULL;
6754 struct got_worktree *worktree = NULL;
6755 char *cwd = NULL, *repo_path = NULL;
6756 int ch;
6757 struct tog_view *view;
6758 int *pack_fds = NULL;
6760 while ((ch = getopt(argc, argv, "r:")) != -1) {
6761 switch (ch) {
6762 case 'r':
6763 repo_path = realpath(optarg, NULL);
6764 if (repo_path == NULL)
6765 return got_error_from_errno2("realpath",
6766 optarg);
6767 break;
6768 default:
6769 usage_ref();
6770 /* NOTREACHED */
6774 argc -= optind;
6775 argv += optind;
6777 if (argc > 1)
6778 usage_ref();
6780 error = got_repo_pack_fds_open(&pack_fds);
6781 if (error != NULL)
6782 goto done;
6784 if (repo_path == NULL) {
6785 cwd = getcwd(NULL, 0);
6786 if (cwd == NULL)
6787 return got_error_from_errno("getcwd");
6788 error = got_worktree_open(&worktree, cwd);
6789 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6790 goto done;
6791 if (worktree)
6792 repo_path =
6793 strdup(got_worktree_get_repo_path(worktree));
6794 else
6795 repo_path = strdup(cwd);
6796 if (repo_path == NULL) {
6797 error = got_error_from_errno("strdup");
6798 goto done;
6802 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6803 if (error != NULL)
6804 goto done;
6806 init_curses();
6808 error = apply_unveil(got_repo_get_path(repo), NULL);
6809 if (error)
6810 goto done;
6812 error = tog_load_refs(repo, 0);
6813 if (error)
6814 goto done;
6816 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6817 if (view == NULL) {
6818 error = got_error_from_errno("view_open");
6819 goto done;
6822 error = open_ref_view(view, repo);
6823 if (error)
6824 goto done;
6826 if (worktree) {
6827 /* Release work tree lock. */
6828 got_worktree_close(worktree);
6829 worktree = NULL;
6831 error = view_loop(view);
6832 done:
6833 free(repo_path);
6834 free(cwd);
6835 if (repo) {
6836 const struct got_error *close_err = got_repo_close(repo);
6837 if (close_err)
6838 error = close_err;
6840 if (pack_fds) {
6841 const struct got_error *pack_err =
6842 got_repo_pack_fds_close(pack_fds);
6843 if (error == NULL)
6844 error = pack_err;
6846 tog_free_refs();
6847 return error;
6850 static void
6851 list_commands(FILE *fp)
6853 size_t i;
6855 fprintf(fp, "commands:");
6856 for (i = 0; i < nitems(tog_commands); i++) {
6857 const struct tog_cmd *cmd = &tog_commands[i];
6858 fprintf(fp, " %s", cmd->name);
6860 fputc('\n', fp);
6863 __dead static void
6864 usage(int hflag, int status)
6866 FILE *fp = (status == 0) ? stdout : stderr;
6868 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6869 getprogname());
6870 if (hflag) {
6871 fprintf(fp, "lazy usage: %s path\n", getprogname());
6872 list_commands(fp);
6874 exit(status);
6877 static char **
6878 make_argv(int argc, ...)
6880 va_list ap;
6881 char **argv;
6882 int i;
6884 va_start(ap, argc);
6886 argv = calloc(argc, sizeof(char *));
6887 if (argv == NULL)
6888 err(1, "calloc");
6889 for (i = 0; i < argc; i++) {
6890 argv[i] = strdup(va_arg(ap, char *));
6891 if (argv[i] == NULL)
6892 err(1, "strdup");
6895 va_end(ap);
6896 return argv;
6900 * Try to convert 'tog path' into a 'tog log path' command.
6901 * The user could simply have mistyped the command rather than knowingly
6902 * provided a path. So check whether argv[0] can in fact be resolved
6903 * to a path in the HEAD commit and print a special error if not.
6904 * This hack is for mpi@ <3
6906 static const struct got_error *
6907 tog_log_with_path(int argc, char *argv[])
6909 const struct got_error *error = NULL, *close_err;
6910 const struct tog_cmd *cmd = NULL;
6911 struct got_repository *repo = NULL;
6912 struct got_worktree *worktree = NULL;
6913 struct got_object_id *commit_id = NULL, *id = NULL;
6914 struct got_commit_object *commit = NULL;
6915 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6916 char *commit_id_str = NULL, **cmd_argv = NULL;
6917 int *pack_fds = NULL;
6919 cwd = getcwd(NULL, 0);
6920 if (cwd == NULL)
6921 return got_error_from_errno("getcwd");
6923 error = got_repo_pack_fds_open(&pack_fds);
6924 if (error != NULL)
6925 goto done;
6927 error = got_worktree_open(&worktree, cwd);
6928 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6929 goto done;
6931 if (worktree)
6932 repo_path = strdup(got_worktree_get_repo_path(worktree));
6933 else
6934 repo_path = strdup(cwd);
6935 if (repo_path == NULL) {
6936 error = got_error_from_errno("strdup");
6937 goto done;
6940 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6941 if (error != NULL)
6942 goto done;
6944 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6945 repo, worktree);
6946 if (error)
6947 goto done;
6949 error = tog_load_refs(repo, 0);
6950 if (error)
6951 goto done;
6952 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6953 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6954 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6955 if (error)
6956 goto done;
6958 if (worktree) {
6959 got_worktree_close(worktree);
6960 worktree = NULL;
6963 error = got_object_open_as_commit(&commit, repo, commit_id);
6964 if (error)
6965 goto done;
6967 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6968 if (error) {
6969 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6970 goto done;
6971 fprintf(stderr, "%s: '%s' is no known command or path\n",
6972 getprogname(), argv[0]);
6973 usage(1, 1);
6974 /* not reached */
6977 close_err = got_repo_close(repo);
6978 if (error == NULL)
6979 error = close_err;
6980 repo = NULL;
6982 error = got_object_id_str(&commit_id_str, commit_id);
6983 if (error)
6984 goto done;
6986 cmd = &tog_commands[0]; /* log */
6987 argc = 4;
6988 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6989 error = cmd->cmd_main(argc, cmd_argv);
6990 done:
6991 if (repo) {
6992 close_err = got_repo_close(repo);
6993 if (error == NULL)
6994 error = close_err;
6996 if (commit)
6997 got_object_commit_close(commit);
6998 if (worktree)
6999 got_worktree_close(worktree);
7000 if (pack_fds) {
7001 const struct got_error *pack_err =
7002 got_repo_pack_fds_close(pack_fds);
7003 if (error == NULL)
7004 error = pack_err;
7006 free(id);
7007 free(commit_id_str);
7008 free(commit_id);
7009 free(cwd);
7010 free(repo_path);
7011 free(in_repo_path);
7012 if (cmd_argv) {
7013 int i;
7014 for (i = 0; i < argc; i++)
7015 free(cmd_argv[i]);
7016 free(cmd_argv);
7018 tog_free_refs();
7019 return error;
7022 int
7023 main(int argc, char *argv[])
7025 const struct got_error *error = NULL;
7026 const struct tog_cmd *cmd = NULL;
7027 int ch, hflag = 0, Vflag = 0;
7028 char **cmd_argv = NULL;
7029 static const struct option longopts[] = {
7030 { "version", no_argument, NULL, 'V' },
7031 { NULL, 0, NULL, 0}
7034 setlocale(LC_CTYPE, "");
7036 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7037 switch (ch) {
7038 case 'h':
7039 hflag = 1;
7040 break;
7041 case 'V':
7042 Vflag = 1;
7043 break;
7044 default:
7045 usage(hflag, 1);
7046 /* NOTREACHED */
7050 argc -= optind;
7051 argv += optind;
7052 optind = 1;
7053 optreset = 1;
7055 if (Vflag) {
7056 got_version_print_str();
7057 return 0;
7060 #ifndef PROFILE
7061 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7062 NULL) == -1)
7063 err(1, "pledge");
7064 #endif
7066 if (argc == 0) {
7067 if (hflag)
7068 usage(hflag, 0);
7069 /* Build an argument vector which runs a default command. */
7070 cmd = &tog_commands[0];
7071 argc = 1;
7072 cmd_argv = make_argv(argc, cmd->name);
7073 } else {
7074 size_t i;
7076 /* Did the user specify a command? */
7077 for (i = 0; i < nitems(tog_commands); i++) {
7078 if (strncmp(tog_commands[i].name, argv[0],
7079 strlen(argv[0])) == 0) {
7080 cmd = &tog_commands[i];
7081 break;
7086 if (cmd == NULL) {
7087 if (argc != 1)
7088 usage(0, 1);
7089 /* No command specified; try log with a path */
7090 error = tog_log_with_path(argc, argv);
7091 } else {
7092 if (hflag)
7093 cmd->cmd_usage();
7094 else
7095 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7098 endwin();
7099 putchar('\n');
7100 if (cmd_argv) {
7101 int i;
7102 for (i = 0; i < argc; i++)
7103 free(cmd_argv[i]);
7104 free(cmd_argv);
7107 if (error && error->code != GOT_ERR_CANCELLED)
7108 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7109 return 0;