Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/ioctl.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #if defined(__FreeBSD__) || defined(__APPLE__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #endif
25 #include <curses.h>
26 #include <panel.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
51 #include "got_diff.h"
52 #include "got_opentemp.h"
53 #include "got_utf8.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_path.h"
59 #include "got_worktree.h"
61 //#define update_panels() (0)
62 //#define doupdate() (0)
64 #ifndef MIN
65 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #endif
68 #ifndef MAX
69 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 #endif
72 #ifndef CTRL
73 #define CTRL(x) ((x) & 0x1f)
74 #endif
76 #ifndef nitems
77 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
78 #endif
80 struct tog_cmd {
81 const char *name;
82 const struct got_error *(*cmd_main)(int, char *[]);
83 void (*cmd_usage)(void);
84 };
86 __dead static void usage(int, int);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_ref(void);
93 static const struct got_error* cmd_log(int, char *[]);
94 static const struct got_error* cmd_diff(int, char *[]);
95 static const struct got_error* cmd_blame(int, char *[]);
96 static const struct got_error* cmd_tree(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
99 static const struct tog_cmd tog_commands[] = {
100 { "log", cmd_log, usage_log },
101 { "diff", cmd_diff, usage_diff },
102 { "blame", cmd_blame, usage_blame },
103 { "tree", cmd_tree, usage_tree },
104 { "ref", cmd_ref, usage_ref },
105 };
107 enum tog_view_type {
108 TOG_VIEW_DIFF,
109 TOG_VIEW_LOG,
110 TOG_VIEW_BLAME,
111 TOG_VIEW_TREE,
112 TOG_VIEW_REF,
113 };
115 #define TOG_EOF_STRING "(END)"
117 struct commit_queue_entry {
118 TAILQ_ENTRY(commit_queue_entry) entry;
119 struct got_object_id *id;
120 struct got_commit_object *commit;
121 int idx;
122 };
123 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 struct commit_queue {
125 int ncommits;
126 struct commit_queue_head head;
127 };
129 struct tog_color {
130 STAILQ_ENTRY(tog_color) entry;
131 regex_t regex;
132 short colorpair;
133 };
134 STAILQ_HEAD(tog_colors, tog_color);
136 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static const struct got_error *
140 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
141 struct got_reference* re2)
143 const char *name1 = got_ref_get_name(re1);
144 const char *name2 = got_ref_get_name(re2);
145 int isbackup1, isbackup2;
147 /* Sort backup refs towards the bottom of the list. */
148 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
149 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
150 if (!isbackup1 && isbackup2) {
151 *cmp = -1;
152 return NULL;
153 } else if (isbackup1 && !isbackup2) {
154 *cmp = 1;
155 return NULL;
158 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
159 return NULL;
162 static const struct got_error *
163 tog_load_refs(struct got_repository *repo, int sort_by_date)
165 const struct got_error *err;
167 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
168 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
169 repo);
170 if (err)
171 return err;
173 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
174 repo);
177 static void
178 tog_free_refs(void)
180 if (tog_refs_idmap) {
181 got_reflist_object_id_map_free(tog_refs_idmap);
182 tog_refs_idmap = NULL;
184 got_ref_list_free(&tog_refs);
187 static const struct got_error *
188 add_color(struct tog_colors *colors, const char *pattern,
189 int idx, short color)
191 const struct got_error *err = NULL;
192 struct tog_color *tc;
193 int regerr = 0;
195 if (idx < 1 || idx > COLOR_PAIRS - 1)
196 return NULL;
198 init_pair(idx, color, -1);
200 tc = calloc(1, sizeof(*tc));
201 if (tc == NULL)
202 return got_error_from_errno("calloc");
203 regerr = regcomp(&tc->regex, pattern,
204 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
205 if (regerr) {
206 static char regerr_msg[512];
207 static char err_msg[512];
208 regerror(regerr, &tc->regex, regerr_msg,
209 sizeof(regerr_msg));
210 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
211 regerr_msg);
212 err = got_error_msg(GOT_ERR_REGEX, err_msg);
213 free(tc);
214 return err;
216 tc->colorpair = idx;
217 STAILQ_INSERT_HEAD(colors, tc, entry);
218 return NULL;
221 static void
222 free_colors(struct tog_colors *colors)
224 struct tog_color *tc;
226 while (!STAILQ_EMPTY(colors)) {
227 tc = STAILQ_FIRST(colors);
228 STAILQ_REMOVE_HEAD(colors, entry);
229 regfree(&tc->regex);
230 free(tc);
234 struct tog_color *
235 get_color(struct tog_colors *colors, int colorpair)
237 struct tog_color *tc = NULL;
239 STAILQ_FOREACH(tc, colors, entry) {
240 if (tc->colorpair == colorpair)
241 return tc;
244 return NULL;
247 static int
248 default_color_value(const char *envvar)
250 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
251 return COLOR_MAGENTA;
252 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
253 return COLOR_CYAN;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
255 return COLOR_YELLOW;
256 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
257 return COLOR_GREEN;
258 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
259 return COLOR_MAGENTA;
260 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
263 return COLOR_CYAN;
264 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
265 return COLOR_GREEN;
266 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
267 return COLOR_GREEN;
268 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
269 return COLOR_CYAN;
270 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
271 return COLOR_YELLOW;
272 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
273 return COLOR_GREEN;
274 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
275 return COLOR_MAGENTA;
276 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
277 return COLOR_YELLOW;
278 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
279 return COLOR_CYAN;
281 return -1;
284 static int
285 get_color_value(const char *envvar)
287 const char *val = getenv(envvar);
289 if (val == NULL)
290 return default_color_value(envvar);
292 if (strcasecmp(val, "black") == 0)
293 return COLOR_BLACK;
294 if (strcasecmp(val, "red") == 0)
295 return COLOR_RED;
296 if (strcasecmp(val, "green") == 0)
297 return COLOR_GREEN;
298 if (strcasecmp(val, "yellow") == 0)
299 return COLOR_YELLOW;
300 if (strcasecmp(val, "blue") == 0)
301 return COLOR_BLUE;
302 if (strcasecmp(val, "magenta") == 0)
303 return COLOR_MAGENTA;
304 if (strcasecmp(val, "cyan") == 0)
305 return COLOR_CYAN;
306 if (strcasecmp(val, "white") == 0)
307 return COLOR_WHITE;
308 if (strcasecmp(val, "default") == 0)
309 return -1;
311 return default_color_value(envvar);
315 struct tog_diff_view_state {
316 struct got_object_id *id1, *id2;
317 const char *label1, *label2;
318 FILE *f, *f1, *f2;
319 int first_displayed_line;
320 int last_displayed_line;
321 int eof;
322 int diff_context;
323 int ignore_whitespace;
324 int force_text_diff;
325 struct got_repository *repo;
326 struct tog_colors colors;
327 size_t nlines;
328 off_t *line_offsets;
329 int matched_line;
330 int selected_line;
332 /* passed from log view; may be NULL */
333 struct tog_view *log_view;
334 };
336 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
338 struct tog_log_thread_args {
339 pthread_cond_t need_commits;
340 pthread_cond_t commit_loaded;
341 int commits_needed;
342 int load_all;
343 struct got_commit_graph *graph;
344 struct commit_queue *commits;
345 const char *in_repo_path;
346 struct got_object_id *start_id;
347 struct got_repository *repo;
348 int *pack_fds;
349 int log_complete;
350 sig_atomic_t *quit;
351 struct commit_queue_entry **first_displayed_entry;
352 struct commit_queue_entry **selected_entry;
353 int *searching;
354 int *search_next_done;
355 regex_t *regex;
356 };
358 struct tog_log_view_state {
359 struct commit_queue commits;
360 struct commit_queue_entry *first_displayed_entry;
361 struct commit_queue_entry *last_displayed_entry;
362 struct commit_queue_entry *selected_entry;
363 int selected;
364 char *in_repo_path;
365 char *head_ref_name;
366 int log_branches;
367 struct got_repository *repo;
368 struct got_object_id *start_id;
369 sig_atomic_t quit;
370 pthread_t thread;
371 struct tog_log_thread_args thread_args;
372 struct commit_queue_entry *matched_entry;
373 struct commit_queue_entry *search_entry;
374 struct tog_colors colors;
375 };
377 #define TOG_COLOR_DIFF_MINUS 1
378 #define TOG_COLOR_DIFF_PLUS 2
379 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
380 #define TOG_COLOR_DIFF_META 4
381 #define TOG_COLOR_TREE_SUBMODULE 5
382 #define TOG_COLOR_TREE_SYMLINK 6
383 #define TOG_COLOR_TREE_DIRECTORY 7
384 #define TOG_COLOR_TREE_EXECUTABLE 8
385 #define TOG_COLOR_COMMIT 9
386 #define TOG_COLOR_AUTHOR 10
387 #define TOG_COLOR_DATE 11
388 #define TOG_COLOR_REFS_HEADS 12
389 #define TOG_COLOR_REFS_TAGS 13
390 #define TOG_COLOR_REFS_REMOTES 14
391 #define TOG_COLOR_REFS_BACKUP 15
393 struct tog_blame_cb_args {
394 struct tog_blame_line *lines; /* one per line */
395 int nlines;
397 struct tog_view *view;
398 struct got_object_id *commit_id;
399 int *quit;
400 };
402 struct tog_blame_thread_args {
403 const char *path;
404 struct got_repository *repo;
405 struct tog_blame_cb_args *cb_args;
406 int *complete;
407 got_cancel_cb cancel_cb;
408 void *cancel_arg;
409 };
411 struct tog_blame {
412 FILE *f;
413 off_t filesize;
414 struct tog_blame_line *lines;
415 int nlines;
416 off_t *line_offsets;
417 pthread_t thread;
418 struct tog_blame_thread_args thread_args;
419 struct tog_blame_cb_args cb_args;
420 const char *path;
421 int *pack_fds;
422 };
424 struct tog_blame_view_state {
425 int first_displayed_line;
426 int last_displayed_line;
427 int selected_line;
428 int blame_complete;
429 int eof;
430 int done;
431 struct got_object_id_queue blamed_commits;
432 struct got_object_qid *blamed_commit;
433 char *path;
434 struct got_repository *repo;
435 struct got_object_id *commit_id;
436 struct tog_blame blame;
437 int matched_line;
438 struct tog_colors colors;
439 };
441 struct tog_parent_tree {
442 TAILQ_ENTRY(tog_parent_tree) entry;
443 struct got_tree_object *tree;
444 struct got_tree_entry *first_displayed_entry;
445 struct got_tree_entry *selected_entry;
446 int selected;
447 };
449 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
451 struct tog_tree_view_state {
452 char *tree_label;
453 struct got_object_id *commit_id;/* commit which this tree belongs to */
454 struct got_tree_object *root; /* the commit's root tree entry */
455 struct got_tree_object *tree; /* currently displayed (sub-)tree */
456 struct got_tree_entry *first_displayed_entry;
457 struct got_tree_entry *last_displayed_entry;
458 struct got_tree_entry *selected_entry;
459 int ndisplayed, selected, show_ids;
460 struct tog_parent_trees parents; /* parent trees of current sub-tree */
461 char *head_ref_name;
462 struct got_repository *repo;
463 struct got_tree_entry *matched_entry;
464 struct tog_colors colors;
465 };
467 struct tog_reflist_entry {
468 TAILQ_ENTRY(tog_reflist_entry) entry;
469 struct got_reference *ref;
470 int idx;
471 };
473 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
475 struct tog_ref_view_state {
476 struct tog_reflist_head refs;
477 struct tog_reflist_entry *first_displayed_entry;
478 struct tog_reflist_entry *last_displayed_entry;
479 struct tog_reflist_entry *selected_entry;
480 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
481 struct got_repository *repo;
482 struct tog_reflist_entry *matched_entry;
483 struct tog_colors colors;
484 };
486 /*
487 * We implement two types of views: parent views and child views.
489 * The 'Tab' key switches focus between a parent view and its child view.
490 * Child views are shown side-by-side to their parent view, provided
491 * there is enough screen estate.
493 * When a new view is opened from within a parent view, this new view
494 * becomes a child view of the parent view, replacing any existing child.
496 * When a new view is opened from within a child view, this new view
497 * becomes a parent view which will obscure the views below until the
498 * user quits the new parent view by typing 'q'.
500 * This list of views contains parent views only.
501 * Child views are only pointed to by their parent view.
502 */
503 TAILQ_HEAD(tog_view_list_head, tog_view);
505 struct tog_view {
506 TAILQ_ENTRY(tog_view) entry;
507 WINDOW *window;
508 PANEL *panel;
509 int nlines, ncols, begin_y, begin_x;
510 int maxx, x; /* max column and current start column */
511 int lines, cols; /* copies of LINES and COLS */
512 int focussed; /* Only set on one parent or child view at a time. */
513 int dying;
514 struct tog_view *parent;
515 struct tog_view *child;
517 /*
518 * This flag is initially set on parent views when a new child view
519 * is created. It gets toggled when the 'Tab' key switches focus
520 * between parent and child.
521 * The flag indicates whether focus should be passed on to our child
522 * view if this parent view gets picked for focus after another parent
523 * view was closed. This prevents child views from losing focus in such
524 * situations.
525 */
526 int focus_child;
528 /* type-specific state */
529 enum tog_view_type type;
530 union {
531 struct tog_diff_view_state diff;
532 struct tog_log_view_state log;
533 struct tog_blame_view_state blame;
534 struct tog_tree_view_state tree;
535 struct tog_ref_view_state ref;
536 } state;
538 const struct got_error *(*show)(struct tog_view *);
539 const struct got_error *(*input)(struct tog_view **,
540 struct tog_view *, int);
541 const struct got_error *(*close)(struct tog_view *);
543 const struct got_error *(*search_start)(struct tog_view *);
544 const struct got_error *(*search_next)(struct tog_view *);
545 int search_started;
546 int searching;
547 #define TOG_SEARCH_FORWARD 1
548 #define TOG_SEARCH_BACKWARD 2
549 int search_next_done;
550 #define TOG_SEARCH_HAVE_MORE 1
551 #define TOG_SEARCH_NO_MORE 2
552 #define TOG_SEARCH_HAVE_NONE 3
553 regex_t regex;
554 regmatch_t regmatch;
555 };
557 static const struct got_error *open_diff_view(struct tog_view *,
558 struct got_object_id *, struct got_object_id *,
559 const char *, const char *, int, int, int, struct tog_view *,
560 struct got_repository *);
561 static const struct got_error *show_diff_view(struct tog_view *);
562 static const struct got_error *input_diff_view(struct tog_view **,
563 struct tog_view *, int);
564 static const struct got_error* close_diff_view(struct tog_view *);
565 static const struct got_error *search_start_diff_view(struct tog_view *);
566 static const struct got_error *search_next_diff_view(struct tog_view *);
568 static const struct got_error *open_log_view(struct tog_view *,
569 struct got_object_id *, struct got_repository *,
570 const char *, const char *, int);
571 static const struct got_error * show_log_view(struct tog_view *);
572 static const struct got_error *input_log_view(struct tog_view **,
573 struct tog_view *, int);
574 static const struct got_error *close_log_view(struct tog_view *);
575 static const struct got_error *search_start_log_view(struct tog_view *);
576 static const struct got_error *search_next_log_view(struct tog_view *);
578 static const struct got_error *open_blame_view(struct tog_view *, char *,
579 struct got_object_id *, struct got_repository *);
580 static const struct got_error *show_blame_view(struct tog_view *);
581 static const struct got_error *input_blame_view(struct tog_view **,
582 struct tog_view *, int);
583 static const struct got_error *close_blame_view(struct tog_view *);
584 static const struct got_error *search_start_blame_view(struct tog_view *);
585 static const struct got_error *search_next_blame_view(struct tog_view *);
587 static const struct got_error *open_tree_view(struct tog_view *,
588 struct got_object_id *, const char *, struct got_repository *);
589 static const struct got_error *show_tree_view(struct tog_view *);
590 static const struct got_error *input_tree_view(struct tog_view **,
591 struct tog_view *, int);
592 static const struct got_error *close_tree_view(struct tog_view *);
593 static const struct got_error *search_start_tree_view(struct tog_view *);
594 static const struct got_error *search_next_tree_view(struct tog_view *);
596 static const struct got_error *open_ref_view(struct tog_view *,
597 struct got_repository *);
598 static const struct got_error *show_ref_view(struct tog_view *);
599 static const struct got_error *input_ref_view(struct tog_view **,
600 struct tog_view *, int);
601 static const struct got_error *close_ref_view(struct tog_view *);
602 static const struct got_error *search_start_ref_view(struct tog_view *);
603 static const struct got_error *search_next_ref_view(struct tog_view *);
605 static volatile sig_atomic_t tog_sigwinch_received;
606 static volatile sig_atomic_t tog_sigpipe_received;
607 static volatile sig_atomic_t tog_sigcont_received;
608 static volatile sig_atomic_t tog_sigint_received;
609 static volatile sig_atomic_t tog_sigterm_received;
611 static void
612 tog_sigwinch(int signo)
614 tog_sigwinch_received = 1;
617 static void
618 tog_sigpipe(int signo)
620 tog_sigpipe_received = 1;
623 static void
624 tog_sigcont(int signo)
626 tog_sigcont_received = 1;
629 static void
630 tog_sigint(int signo)
632 tog_sigint_received = 1;
635 static void
636 tog_sigterm(int signo)
638 tog_sigterm_received = 1;
641 static int
642 tog_fatal_signal_received(void)
644 return (tog_sigpipe_received ||
645 tog_sigint_received || tog_sigint_received);
649 static const struct got_error *
650 view_close(struct tog_view *view)
652 const struct got_error *err = NULL;
654 if (view->child) {
655 view_close(view->child);
656 view->child = NULL;
658 if (view->close)
659 err = view->close(view);
660 if (view->panel)
661 del_panel(view->panel);
662 if (view->window)
663 delwin(view->window);
664 free(view);
665 return err;
668 static struct tog_view *
669 view_open(int nlines, int ncols, int begin_y, int begin_x,
670 enum tog_view_type type)
672 struct tog_view *view = calloc(1, sizeof(*view));
674 if (view == NULL)
675 return NULL;
677 view->type = type;
678 view->lines = LINES;
679 view->cols = COLS;
680 view->nlines = nlines ? nlines : LINES - begin_y;
681 view->ncols = ncols ? ncols : COLS - begin_x;
682 view->begin_y = begin_y;
683 view->begin_x = begin_x;
684 view->window = newwin(nlines, ncols, begin_y, begin_x);
685 if (view->window == NULL) {
686 view_close(view);
687 return NULL;
689 view->panel = new_panel(view->window);
690 if (view->panel == NULL ||
691 set_panel_userptr(view->panel, view) != OK) {
692 view_close(view);
693 return NULL;
696 keypad(view->window, TRUE);
697 return view;
700 static int
701 view_split_begin_x(int begin_x)
703 if (begin_x > 0 || COLS < 120)
704 return 0;
705 return (COLS - MAX(COLS / 2, 80));
708 static const struct got_error *view_resize(struct tog_view *);
710 static const struct got_error *
711 view_splitscreen(struct tog_view *view)
713 const struct got_error *err = NULL;
715 view->begin_y = 0;
716 view->begin_x = view_split_begin_x(0);
717 view->nlines = LINES;
718 view->ncols = COLS - view->begin_x;
719 view->lines = LINES;
720 view->cols = COLS;
721 err = view_resize(view);
722 if (err)
723 return err;
725 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
726 return got_error_from_errno("mvwin");
728 return NULL;
731 static const struct got_error *
732 view_fullscreen(struct tog_view *view)
734 const struct got_error *err = NULL;
736 view->begin_x = 0;
737 view->begin_y = 0;
738 view->nlines = LINES;
739 view->ncols = COLS;
740 view->lines = LINES;
741 view->cols = COLS;
742 err = view_resize(view);
743 if (err)
744 return err;
746 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
747 return got_error_from_errno("mvwin");
749 return NULL;
752 static int
753 view_is_parent_view(struct tog_view *view)
755 return view->parent == NULL;
758 static 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 (view->child) {
774 view->child->begin_x = view_split_begin_x(view->begin_x);
775 if (view->child->begin_x == 0) {
776 ncols = COLS;
778 view_fullscreen(view->child);
779 if (view->child->focussed)
780 show_panel(view->child->panel);
781 else
782 show_panel(view->panel);
783 } else {
784 ncols = view->child->begin_x;
786 view_splitscreen(view->child);
787 show_panel(view->child->panel);
789 } else if (view->parent == NULL)
790 ncols = COLS;
792 if (wresize(view->window, nlines, ncols) == ERR)
793 return got_error_from_errno("wresize");
794 if (replace_panel(view->panel, view->window) == ERR)
795 return got_error_from_errno("replace_panel");
796 wclear(view->window);
798 view->nlines = nlines;
799 view->ncols = ncols;
800 view->lines = LINES;
801 view->cols = COLS;
803 return NULL;
806 static const struct got_error *
807 view_close_child(struct tog_view *view)
809 const struct got_error *err = NULL;
811 if (view->child == NULL)
812 return NULL;
814 err = view_close(view->child);
815 view->child = NULL;
816 return err;
819 static const struct got_error *
820 view_set_child(struct tog_view *view, struct tog_view *child)
822 view->child = child;
823 child->parent = view;
825 return view_resize(view);
828 static int
829 view_is_splitscreen(struct tog_view *view)
831 return view->begin_x > 0;
834 static void
835 tog_resizeterm(void)
837 int cols, lines;
838 struct winsize size;
840 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
841 cols = 80; /* Default */
842 lines = 24;
843 } else {
844 cols = size.ws_col;
845 lines = size.ws_row;
847 resize_term(lines, cols);
850 static const struct got_error *
851 view_search_start(struct tog_view *view)
853 const struct got_error *err = NULL;
854 char pattern[1024];
855 int ret;
857 if (view->search_started) {
858 regfree(&view->regex);
859 view->searching = 0;
860 memset(&view->regmatch, 0, sizeof(view->regmatch));
862 view->search_started = 0;
864 if (view->nlines < 1)
865 return NULL;
867 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
868 wclrtoeol(view->window);
870 nocbreak();
871 echo();
872 ret = wgetnstr(view->window, pattern, sizeof(pattern));
873 cbreak();
874 noecho();
875 if (ret == ERR)
876 return NULL;
878 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
879 err = view->search_start(view);
880 if (err) {
881 regfree(&view->regex);
882 return err;
884 view->search_started = 1;
885 view->searching = TOG_SEARCH_FORWARD;
886 view->search_next_done = 0;
887 view->search_next(view);
890 return NULL;
893 static const struct got_error *
894 view_input(struct tog_view **new, int *done, struct tog_view *view,
895 struct tog_view_list_head *views)
897 const struct got_error *err = NULL;
898 struct tog_view *v;
899 int ch, errcode;
901 *new = NULL;
903 /* Clear "no matches" indicator. */
904 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
905 view->search_next_done == TOG_SEARCH_HAVE_NONE)
906 view->search_next_done = TOG_SEARCH_HAVE_MORE;
908 if (view->searching && !view->search_next_done) {
909 errcode = pthread_mutex_unlock(&tog_mutex);
910 if (errcode)
911 return got_error_set_errno(errcode,
912 "pthread_mutex_unlock");
913 sched_yield();
914 errcode = pthread_mutex_lock(&tog_mutex);
915 if (errcode)
916 return got_error_set_errno(errcode,
917 "pthread_mutex_lock");
918 view->search_next(view);
919 return NULL;
922 nodelay(stdscr, FALSE);
923 /* Allow threads to make progress while we are waiting for input. */
924 errcode = pthread_mutex_unlock(&tog_mutex);
925 if (errcode)
926 return got_error_set_errno(errcode, "pthread_mutex_unlock");
927 ch = wgetch(view->window);
928 errcode = pthread_mutex_lock(&tog_mutex);
929 if (errcode)
930 return got_error_set_errno(errcode, "pthread_mutex_lock");
931 nodelay(stdscr, TRUE);
933 if (tog_sigwinch_received || tog_sigcont_received) {
934 tog_resizeterm();
935 tog_sigwinch_received = 0;
936 tog_sigcont_received = 0;
937 TAILQ_FOREACH(v, views, entry) {
938 err = view_resize(v);
939 if (err)
940 return err;
941 err = v->input(new, v, KEY_RESIZE);
942 if (err)
943 return err;
944 if (v->child) {
945 err = view_resize(v->child);
946 if (err)
947 return err;
948 err = v->child->input(new, v->child,
949 KEY_RESIZE);
950 if (err)
951 return err;
956 switch (ch) {
957 case '\t':
958 if (view->child) {
959 view->focussed = 0;
960 view->child->focussed = 1;
961 view->focus_child = 1;
962 } else if (view->parent) {
963 view->focussed = 0;
964 view->parent->focussed = 1;
965 view->parent->focus_child = 0;
967 break;
968 case 'q':
969 err = view->input(new, view, ch);
970 view->dying = 1;
971 break;
972 case 'Q':
973 *done = 1;
974 break;
975 case 'f':
976 if (view_is_parent_view(view)) {
977 if (view->child == NULL)
978 break;
979 if (view_is_splitscreen(view->child)) {
980 view->focussed = 0;
981 view->child->focussed = 1;
982 err = view_fullscreen(view->child);
983 } else
984 err = view_splitscreen(view->child);
985 if (err)
986 break;
987 err = view->child->input(new, view->child,
988 KEY_RESIZE);
989 } else {
990 if (view_is_splitscreen(view)) {
991 view->parent->focussed = 0;
992 view->focussed = 1;
993 err = view_fullscreen(view);
994 } else {
995 err = view_splitscreen(view);
997 if (err)
998 break;
999 err = view->input(new, view, KEY_RESIZE);
1001 break;
1002 case KEY_RESIZE:
1003 break;
1004 case '/':
1005 if (view->search_start)
1006 view_search_start(view);
1007 else
1008 err = view->input(new, view, ch);
1009 break;
1010 case 'N':
1011 case 'n':
1012 if (view->search_started && view->search_next) {
1013 view->searching = (ch == 'n' ?
1014 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1015 view->search_next_done = 0;
1016 view->search_next(view);
1017 } else
1018 err = view->input(new, view, ch);
1019 break;
1020 default:
1021 err = view->input(new, view, ch);
1022 break;
1025 return err;
1028 void
1029 view_vborder(struct tog_view *view)
1031 PANEL *panel;
1032 const struct tog_view *view_above;
1034 if (view->parent)
1035 return view_vborder(view->parent);
1037 panel = panel_above(view->panel);
1038 if (panel == NULL)
1039 return;
1041 view_above = panel_userptr(panel);
1042 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1043 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1046 int
1047 view_needs_focus_indication(struct tog_view *view)
1049 if (view_is_parent_view(view)) {
1050 if (view->child == NULL || view->child->focussed)
1051 return 0;
1052 if (!view_is_splitscreen(view->child))
1053 return 0;
1054 } else if (!view_is_splitscreen(view))
1055 return 0;
1057 return view->focussed;
1060 static const struct got_error *
1061 view_loop(struct tog_view *view)
1063 const struct got_error *err = NULL;
1064 struct tog_view_list_head views;
1065 struct tog_view *new_view;
1066 int fast_refresh = 10;
1067 int done = 0, errcode;
1069 errcode = pthread_mutex_lock(&tog_mutex);
1070 if (errcode)
1071 return got_error_set_errno(errcode, "pthread_mutex_lock");
1073 TAILQ_INIT(&views);
1074 TAILQ_INSERT_HEAD(&views, view, entry);
1076 view->focussed = 1;
1077 err = view->show(view);
1078 if (err)
1079 return err;
1080 update_panels();
1081 doupdate();
1082 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1083 /* Refresh fast during initialization, then become slower. */
1084 if (fast_refresh && fast_refresh-- == 0)
1085 halfdelay(10); /* switch to once per second */
1087 err = view_input(&new_view, &done, view, &views);
1088 if (err)
1089 break;
1090 if (view->dying) {
1091 struct tog_view *v, *prev = NULL;
1093 if (view_is_parent_view(view))
1094 prev = TAILQ_PREV(view, tog_view_list_head,
1095 entry);
1096 else if (view->parent)
1097 prev = view->parent;
1099 if (view->parent) {
1100 view->parent->child = NULL;
1101 view->parent->focus_child = 0;
1103 err = view_resize(view->parent);
1104 if (err)
1105 break;
1106 } else
1107 TAILQ_REMOVE(&views, view, entry);
1109 err = view_close(view);
1110 if (err)
1111 goto done;
1113 view = NULL;
1114 TAILQ_FOREACH(v, &views, entry) {
1115 if (v->focussed)
1116 break;
1118 if (view == NULL && new_view == NULL) {
1119 /* No view has focus. Try to pick one. */
1120 if (prev)
1121 view = prev;
1122 else if (!TAILQ_EMPTY(&views)) {
1123 view = TAILQ_LAST(&views,
1124 tog_view_list_head);
1126 if (view) {
1127 if (view->focus_child) {
1128 view->child->focussed = 1;
1129 view = view->child;
1130 } else
1131 view->focussed = 1;
1135 if (new_view) {
1136 struct tog_view *v, *t;
1137 /* Only allow one parent view per type. */
1138 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1139 if (v->type != new_view->type)
1140 continue;
1141 TAILQ_REMOVE(&views, v, entry);
1142 err = view_close(v);
1143 if (err)
1144 goto done;
1145 break;
1147 TAILQ_INSERT_TAIL(&views, new_view, entry);
1148 view = new_view;
1150 if (view) {
1151 if (view_is_parent_view(view)) {
1152 if (view->child && view->child->focussed)
1153 view = view->child;
1154 } else {
1155 if (view->parent && view->parent->focussed)
1156 view = view->parent;
1158 show_panel(view->panel);
1159 if (view->child && view_is_splitscreen(view->child))
1160 show_panel(view->child->panel);
1161 if (view->parent && view_is_splitscreen(view)) {
1162 err = view->parent->show(view->parent);
1163 if (err)
1164 goto done;
1166 err = view->show(view);
1167 if (err)
1168 goto done;
1169 if (view->child) {
1170 err = view->child->show(view->child);
1171 if (err)
1172 goto done;
1174 update_panels();
1175 doupdate();
1178 done:
1179 while (!TAILQ_EMPTY(&views)) {
1180 view = TAILQ_FIRST(&views);
1181 TAILQ_REMOVE(&views, view, entry);
1182 view_close(view);
1185 errcode = pthread_mutex_unlock(&tog_mutex);
1186 if (errcode && err == NULL)
1187 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1189 return err;
1192 __dead static void
1193 usage_log(void)
1195 endwin();
1196 fprintf(stderr,
1197 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1198 getprogname());
1199 exit(1);
1202 /* Create newly allocated wide-character string equivalent to a byte string. */
1203 static const struct got_error *
1204 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1206 char *vis = NULL;
1207 const struct got_error *err = NULL;
1209 *ws = NULL;
1210 *wlen = mbstowcs(NULL, s, 0);
1211 if (*wlen == (size_t)-1) {
1212 int vislen;
1213 if (errno != EILSEQ)
1214 return got_error_from_errno("mbstowcs");
1216 /* byte string invalid in current encoding; try to "fix" it */
1217 err = got_mbsavis(&vis, &vislen, s);
1218 if (err)
1219 return err;
1220 *wlen = mbstowcs(NULL, vis, 0);
1221 if (*wlen == (size_t)-1) {
1222 err = got_error_from_errno("mbstowcs"); /* give up */
1223 goto done;
1227 *ws = calloc(*wlen + 1, sizeof(**ws));
1228 if (*ws == NULL) {
1229 err = got_error_from_errno("calloc");
1230 goto done;
1233 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1234 err = got_error_from_errno("mbstowcs");
1235 done:
1236 free(vis);
1237 if (err) {
1238 free(*ws);
1239 *ws = NULL;
1240 *wlen = 0;
1242 return err;
1245 static const struct got_error *
1246 expand_tab(char **ptr, const char *src)
1248 char *dst;
1249 size_t len, n, idx = 0, sz = 0;
1251 *ptr = NULL;
1252 n = len = strlen(src);
1253 dst = malloc(n + 1);
1254 if (dst == NULL)
1255 return got_error_from_errno("malloc");
1257 while (idx < len && src[idx]) {
1258 const char c = src[idx];
1260 if (c == '\t') {
1261 size_t nb = TABSIZE - sz % TABSIZE;
1262 char *p;
1264 p = realloc(dst, n + nb);
1265 if (p == NULL) {
1266 free(dst);
1267 return got_error_from_errno("realloc");
1270 dst = p;
1271 n += nb;
1272 memset(dst + sz, ' ', nb);
1273 sz += nb;
1274 } else
1275 dst[sz++] = src[idx];
1276 ++idx;
1279 dst[sz] = '\0';
1280 *ptr = dst;
1281 return NULL;
1285 * Advance at most n columns from wline starting at offset off.
1286 * Return the index to the first character after the span operation.
1287 * Return the combined column width of all spanned wide character in
1288 * *rcol.
1290 static int
1291 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1293 int width, i, cols = 0;
1295 if (n == 0) {
1296 *rcol = cols;
1297 return off;
1300 for (i = off; wline[i] != L'\0'; ++i) {
1301 if (wline[i] == L'\t')
1302 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1303 else
1304 width = wcwidth(wline[i]);
1306 if (width == -1) {
1307 width = 1;
1308 wline[i] = L'.';
1311 if (cols + width > n)
1312 break;
1313 cols += width;
1316 *rcol = cols;
1317 return i;
1321 * Format a line for display, ensuring that it won't overflow a width limit.
1322 * With scrolling, the width returned refers to the scrolled version of the
1323 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1325 static const struct got_error *
1326 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1327 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1329 const struct got_error *err = NULL;
1330 int cols;
1331 wchar_t *wline = NULL;
1332 char *exstr = NULL;
1333 size_t wlen;
1334 int i, scrollx;
1336 *wlinep = NULL;
1337 *widthp = 0;
1339 if (expand) {
1340 err = expand_tab(&exstr, line);
1341 if (err)
1342 return err;
1345 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1346 free(exstr);
1347 if (err)
1348 return err;
1350 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1352 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1353 wline[wlen - 1] = L'\0';
1354 wlen--;
1356 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1357 wline[wlen - 1] = L'\0';
1358 wlen--;
1361 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1362 wline[i] = L'\0';
1364 if (widthp)
1365 *widthp = cols;
1366 if (scrollxp)
1367 *scrollxp = scrollx;
1368 if (err)
1369 free(wline);
1370 else
1371 *wlinep = wline;
1372 return err;
1375 static const struct got_error*
1376 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1377 struct got_object_id *id, struct got_repository *repo)
1379 static const struct got_error *err = NULL;
1380 struct got_reflist_entry *re;
1381 char *s;
1382 const char *name;
1384 *refs_str = NULL;
1386 TAILQ_FOREACH(re, refs, entry) {
1387 struct got_tag_object *tag = NULL;
1388 struct got_object_id *ref_id;
1389 int cmp;
1391 name = got_ref_get_name(re->ref);
1392 if (strcmp(name, GOT_REF_HEAD) == 0)
1393 continue;
1394 if (strncmp(name, "refs/", 5) == 0)
1395 name += 5;
1396 if (strncmp(name, "got/", 4) == 0 &&
1397 strncmp(name, "got/backup/", 11) != 0)
1398 continue;
1399 if (strncmp(name, "heads/", 6) == 0)
1400 name += 6;
1401 if (strncmp(name, "remotes/", 8) == 0) {
1402 name += 8;
1403 s = strstr(name, "/" GOT_REF_HEAD);
1404 if (s != NULL && s[strlen(s)] == '\0')
1405 continue;
1407 err = got_ref_resolve(&ref_id, repo, re->ref);
1408 if (err)
1409 break;
1410 if (strncmp(name, "tags/", 5) == 0) {
1411 err = got_object_open_as_tag(&tag, repo, ref_id);
1412 if (err) {
1413 if (err->code != GOT_ERR_OBJ_TYPE) {
1414 free(ref_id);
1415 break;
1417 /* Ref points at something other than a tag. */
1418 err = NULL;
1419 tag = NULL;
1422 cmp = got_object_id_cmp(tag ?
1423 got_object_tag_get_object_id(tag) : ref_id, id);
1424 free(ref_id);
1425 if (tag)
1426 got_object_tag_close(tag);
1427 if (cmp != 0)
1428 continue;
1429 s = *refs_str;
1430 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1431 s ? ", " : "", name) == -1) {
1432 err = got_error_from_errno("asprintf");
1433 free(s);
1434 *refs_str = NULL;
1435 break;
1437 free(s);
1440 return err;
1443 static const struct got_error *
1444 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1445 int col_tab_align)
1447 char *smallerthan;
1449 smallerthan = strchr(author, '<');
1450 if (smallerthan && smallerthan[1] != '\0')
1451 author = smallerthan + 1;
1452 author[strcspn(author, "@>")] = '\0';
1453 return format_line(wauthor, author_width, NULL, author, 0, limit,
1454 col_tab_align, 0);
1457 static const struct got_error *
1458 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1459 struct got_object_id *id, const size_t date_display_cols,
1460 int author_display_cols)
1462 struct tog_log_view_state *s = &view->state.log;
1463 const struct got_error *err = NULL;
1464 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1465 char *logmsg0 = NULL, *logmsg = NULL;
1466 char *author = NULL;
1467 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1468 int author_width, logmsg_width;
1469 char *newline, *line = NULL;
1470 int col, limit, scrollx;
1471 const int avail = view->ncols;
1472 struct tm tm;
1473 time_t committer_time;
1474 struct tog_color *tc;
1476 committer_time = got_object_commit_get_committer_time(commit);
1477 if (gmtime_r(&committer_time, &tm) == NULL)
1478 return got_error_from_errno("gmtime_r");
1479 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1480 return got_error(GOT_ERR_NO_SPACE);
1482 if (avail <= date_display_cols)
1483 limit = MIN(sizeof(datebuf) - 1, avail);
1484 else
1485 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1486 tc = get_color(&s->colors, TOG_COLOR_DATE);
1487 if (tc)
1488 wattr_on(view->window,
1489 COLOR_PAIR(tc->colorpair), NULL);
1490 waddnstr(view->window, datebuf, limit);
1491 if (tc)
1492 wattr_off(view->window,
1493 COLOR_PAIR(tc->colorpair), NULL);
1494 col = limit;
1495 if (col > avail)
1496 goto done;
1498 if (avail >= 120) {
1499 char *id_str;
1500 err = got_object_id_str(&id_str, id);
1501 if (err)
1502 goto done;
1503 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1504 if (tc)
1505 wattr_on(view->window,
1506 COLOR_PAIR(tc->colorpair), NULL);
1507 wprintw(view->window, "%.8s ", id_str);
1508 if (tc)
1509 wattr_off(view->window,
1510 COLOR_PAIR(tc->colorpair), NULL);
1511 free(id_str);
1512 col += 9;
1513 if (col > avail)
1514 goto done;
1517 author = strdup(got_object_commit_get_author(commit));
1518 if (author == NULL) {
1519 err = got_error_from_errno("strdup");
1520 goto done;
1522 err = format_author(&wauthor, &author_width, author, avail - col, col);
1523 if (err)
1524 goto done;
1525 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1526 if (tc)
1527 wattr_on(view->window,
1528 COLOR_PAIR(tc->colorpair), NULL);
1529 waddwstr(view->window, wauthor);
1530 if (tc)
1531 wattr_off(view->window,
1532 COLOR_PAIR(tc->colorpair), NULL);
1533 col += author_width;
1534 while (col < avail && author_width < author_display_cols + 2) {
1535 waddch(view->window, ' ');
1536 col++;
1537 author_width++;
1539 if (col > avail)
1540 goto done;
1542 err = got_object_commit_get_logmsg(&logmsg0, commit);
1543 if (err)
1544 goto done;
1545 logmsg = logmsg0;
1546 while (*logmsg == '\n')
1547 logmsg++;
1548 newline = strchr(logmsg, '\n');
1549 if (newline)
1550 *newline = '\0';
1551 limit = avail - col;
1552 if (view->child && limit > 0)
1553 limit--; /* for the border */
1554 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1555 limit, col, 1);
1556 if (err)
1557 goto done;
1558 waddwstr(view->window, &wlogmsg[scrollx]);
1559 col += MAX(logmsg_width, 0);
1560 while (col < avail) {
1561 waddch(view->window, ' ');
1562 col++;
1564 done:
1565 free(logmsg0);
1566 free(wlogmsg);
1567 free(author);
1568 free(wauthor);
1569 free(line);
1570 return err;
1573 static struct commit_queue_entry *
1574 alloc_commit_queue_entry(struct got_commit_object *commit,
1575 struct got_object_id *id)
1577 struct commit_queue_entry *entry;
1579 entry = calloc(1, sizeof(*entry));
1580 if (entry == NULL)
1581 return NULL;
1583 entry->id = id;
1584 entry->commit = commit;
1585 return entry;
1588 static void
1589 pop_commit(struct commit_queue *commits)
1591 struct commit_queue_entry *entry;
1593 entry = TAILQ_FIRST(&commits->head);
1594 TAILQ_REMOVE(&commits->head, entry, entry);
1595 got_object_commit_close(entry->commit);
1596 commits->ncommits--;
1597 /* Don't free entry->id! It is owned by the commit graph. */
1598 free(entry);
1601 static void
1602 free_commits(struct commit_queue *commits)
1604 while (!TAILQ_EMPTY(&commits->head))
1605 pop_commit(commits);
1608 static const struct got_error *
1609 match_commit(int *have_match, struct got_object_id *id,
1610 struct got_commit_object *commit, regex_t *regex)
1612 const struct got_error *err = NULL;
1613 regmatch_t regmatch;
1614 char *id_str = NULL, *logmsg = NULL;
1616 *have_match = 0;
1618 err = got_object_id_str(&id_str, id);
1619 if (err)
1620 return err;
1622 err = got_object_commit_get_logmsg(&logmsg, commit);
1623 if (err)
1624 goto done;
1626 if (regexec(regex, got_object_commit_get_author(commit), 1,
1627 &regmatch, 0) == 0 ||
1628 regexec(regex, got_object_commit_get_committer(commit), 1,
1629 &regmatch, 0) == 0 ||
1630 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1631 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1632 *have_match = 1;
1633 done:
1634 free(id_str);
1635 free(logmsg);
1636 return err;
1639 static const struct got_error *
1640 queue_commits(struct tog_log_thread_args *a)
1642 const struct got_error *err = NULL;
1645 * We keep all commits open throughout the lifetime of the log
1646 * view in order to avoid having to re-fetch commits from disk
1647 * while updating the display.
1649 do {
1650 struct got_object_id *id;
1651 struct got_commit_object *commit;
1652 struct commit_queue_entry *entry;
1653 int errcode;
1655 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1656 NULL, NULL);
1657 if (err || id == NULL)
1658 break;
1660 err = got_object_open_as_commit(&commit, a->repo, id);
1661 if (err)
1662 break;
1663 entry = alloc_commit_queue_entry(commit, id);
1664 if (entry == NULL) {
1665 err = got_error_from_errno("alloc_commit_queue_entry");
1666 break;
1669 errcode = pthread_mutex_lock(&tog_mutex);
1670 if (errcode) {
1671 err = got_error_set_errno(errcode,
1672 "pthread_mutex_lock");
1673 break;
1676 entry->idx = a->commits->ncommits;
1677 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1678 a->commits->ncommits++;
1680 if (*a->searching == TOG_SEARCH_FORWARD &&
1681 !*a->search_next_done) {
1682 int have_match;
1683 err = match_commit(&have_match, id, commit, a->regex);
1684 if (err)
1685 break;
1686 if (have_match)
1687 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1690 errcode = pthread_mutex_unlock(&tog_mutex);
1691 if (errcode && err == NULL)
1692 err = got_error_set_errno(errcode,
1693 "pthread_mutex_unlock");
1694 if (err)
1695 break;
1696 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1698 return err;
1701 static void
1702 select_commit(struct tog_log_view_state *s)
1704 struct commit_queue_entry *entry;
1705 int ncommits = 0;
1707 entry = s->first_displayed_entry;
1708 while (entry) {
1709 if (ncommits == s->selected) {
1710 s->selected_entry = entry;
1711 break;
1713 entry = TAILQ_NEXT(entry, entry);
1714 ncommits++;
1718 static const struct got_error *
1719 draw_commits(struct tog_view *view)
1721 const struct got_error *err = NULL;
1722 struct tog_log_view_state *s = &view->state.log;
1723 struct commit_queue_entry *entry = s->selected_entry;
1724 const int limit = view->nlines;
1725 int width;
1726 int ncommits, author_cols = 4;
1727 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1728 char *refs_str = NULL;
1729 wchar_t *wline;
1730 struct tog_color *tc;
1731 static const size_t date_display_cols = 12;
1733 if (s->selected_entry &&
1734 !(view->searching && view->search_next_done == 0)) {
1735 struct got_reflist_head *refs;
1736 err = got_object_id_str(&id_str, s->selected_entry->id);
1737 if (err)
1738 return err;
1739 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1740 s->selected_entry->id);
1741 if (refs) {
1742 err = build_refs_str(&refs_str, refs,
1743 s->selected_entry->id, s->repo);
1744 if (err)
1745 goto done;
1749 if (s->thread_args.commits_needed == 0)
1750 halfdelay(10); /* disable fast refresh */
1752 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1753 if (asprintf(&ncommits_str, " [%d/%d] %s",
1754 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1755 (view->searching && !view->search_next_done) ?
1756 "searching..." : "loading...") == -1) {
1757 err = got_error_from_errno("asprintf");
1758 goto done;
1760 } else {
1761 const char *search_str = NULL;
1763 if (view->searching) {
1764 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1765 search_str = "no more matches";
1766 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1767 search_str = "no matches found";
1768 else if (!view->search_next_done)
1769 search_str = "searching...";
1772 if (asprintf(&ncommits_str, " [%d/%d] %s",
1773 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1774 search_str ? search_str :
1775 (refs_str ? refs_str : "")) == -1) {
1776 err = got_error_from_errno("asprintf");
1777 goto done;
1781 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1782 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1783 "........................................",
1784 s->in_repo_path, ncommits_str) == -1) {
1785 err = got_error_from_errno("asprintf");
1786 header = NULL;
1787 goto done;
1789 } else if (asprintf(&header, "commit %s%s",
1790 id_str ? id_str : "........................................",
1791 ncommits_str) == -1) {
1792 err = got_error_from_errno("asprintf");
1793 header = NULL;
1794 goto done;
1796 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1797 if (err)
1798 goto done;
1800 werase(view->window);
1802 if (view_needs_focus_indication(view))
1803 wstandout(view->window);
1804 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1805 if (tc)
1806 wattr_on(view->window,
1807 COLOR_PAIR(tc->colorpair), NULL);
1808 waddwstr(view->window, wline);
1809 if (tc)
1810 wattr_off(view->window,
1811 COLOR_PAIR(tc->colorpair), NULL);
1812 while (width < view->ncols) {
1813 waddch(view->window, ' ');
1814 width++;
1816 if (view_needs_focus_indication(view))
1817 wstandend(view->window);
1818 free(wline);
1819 if (limit <= 1)
1820 goto done;
1822 /* Grow author column size if necessary, and set view->maxx. */
1823 entry = s->first_displayed_entry;
1824 ncommits = 0;
1825 view->maxx = 0;
1826 while (entry) {
1827 char *author, *eol, *msg, *msg0;
1828 wchar_t *wauthor, *wmsg;
1829 int width;
1830 if (ncommits >= limit - 1)
1831 break;
1832 author = strdup(got_object_commit_get_author(entry->commit));
1833 if (author == NULL) {
1834 err = got_error_from_errno("strdup");
1835 goto done;
1837 err = format_author(&wauthor, &width, author, COLS,
1838 date_display_cols);
1839 if (author_cols < width)
1840 author_cols = width;
1841 free(wauthor);
1842 free(author);
1843 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1844 if (err)
1845 goto done;
1846 msg = msg0;
1847 while (*msg == '\n')
1848 ++msg;
1849 if ((eol = strchr(msg, '\n')))
1850 *eol = '\0';
1851 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1852 date_display_cols + author_cols, 0);
1853 if (err)
1854 goto done;
1855 view->maxx = MAX(view->maxx, width);
1856 free(msg0);
1857 free(wmsg);
1858 ncommits++;
1859 entry = TAILQ_NEXT(entry, entry);
1862 entry = s->first_displayed_entry;
1863 s->last_displayed_entry = s->first_displayed_entry;
1864 ncommits = 0;
1865 while (entry) {
1866 if (ncommits >= limit - 1)
1867 break;
1868 if (ncommits == s->selected)
1869 wstandout(view->window);
1870 err = draw_commit(view, entry->commit, entry->id,
1871 date_display_cols, author_cols);
1872 if (ncommits == s->selected)
1873 wstandend(view->window);
1874 if (err)
1875 goto done;
1876 ncommits++;
1877 s->last_displayed_entry = entry;
1878 entry = TAILQ_NEXT(entry, entry);
1881 view_vborder(view);
1882 update_panels();
1883 doupdate();
1884 done:
1885 free(id_str);
1886 free(refs_str);
1887 free(ncommits_str);
1888 free(header);
1889 return err;
1892 static void
1893 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1895 struct commit_queue_entry *entry;
1896 int nscrolled = 0;
1898 entry = TAILQ_FIRST(&s->commits.head);
1899 if (s->first_displayed_entry == entry)
1900 return;
1902 entry = s->first_displayed_entry;
1903 while (entry && nscrolled < maxscroll) {
1904 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1905 if (entry) {
1906 s->first_displayed_entry = entry;
1907 nscrolled++;
1912 static const struct got_error *
1913 trigger_log_thread(struct tog_view *view, int wait)
1915 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1916 int errcode;
1918 halfdelay(1); /* fast refresh while loading commits */
1920 while (ta->commits_needed > 0 || ta->load_all) {
1921 if (ta->log_complete)
1922 break;
1924 /* Wake the log thread. */
1925 errcode = pthread_cond_signal(&ta->need_commits);
1926 if (errcode)
1927 return got_error_set_errno(errcode,
1928 "pthread_cond_signal");
1931 * The mutex will be released while the view loop waits
1932 * in wgetch(), at which time the log thread will run.
1934 if (!wait)
1935 break;
1937 /* Display progress update in log view. */
1938 show_log_view(view);
1939 update_panels();
1940 doupdate();
1942 /* Wait right here while next commit is being loaded. */
1943 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1944 if (errcode)
1945 return got_error_set_errno(errcode,
1946 "pthread_cond_wait");
1948 /* Display progress update in log view. */
1949 show_log_view(view);
1950 update_panels();
1951 doupdate();
1954 return NULL;
1957 static const struct got_error *
1958 log_scroll_down(struct tog_view *view, int maxscroll)
1960 struct tog_log_view_state *s = &view->state.log;
1961 const struct got_error *err = NULL;
1962 struct commit_queue_entry *pentry;
1963 int nscrolled = 0, ncommits_needed;
1965 if (s->last_displayed_entry == NULL)
1966 return NULL;
1968 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1969 if (s->commits.ncommits < ncommits_needed &&
1970 !s->thread_args.log_complete) {
1972 * Ask the log thread for required amount of commits.
1974 s->thread_args.commits_needed += maxscroll;
1975 err = trigger_log_thread(view, 1);
1976 if (err)
1977 return err;
1980 do {
1981 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1982 if (pentry == NULL)
1983 break;
1985 s->last_displayed_entry = pentry;
1987 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1988 if (pentry == NULL)
1989 break;
1990 s->first_displayed_entry = pentry;
1991 } while (++nscrolled < maxscroll);
1993 return err;
1996 static const struct got_error *
1997 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1998 struct got_commit_object *commit, struct got_object_id *commit_id,
1999 struct tog_view *log_view, struct got_repository *repo)
2001 const struct got_error *err;
2002 struct got_object_qid *parent_id;
2003 struct tog_view *diff_view;
2005 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2006 if (diff_view == NULL)
2007 return got_error_from_errno("view_open");
2009 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2010 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2011 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2012 if (err == NULL)
2013 *new_view = diff_view;
2014 return err;
2017 static const struct got_error *
2018 tree_view_visit_subtree(struct tog_tree_view_state *s,
2019 struct got_tree_object *subtree)
2021 struct tog_parent_tree *parent;
2023 parent = calloc(1, sizeof(*parent));
2024 if (parent == NULL)
2025 return got_error_from_errno("calloc");
2027 parent->tree = s->tree;
2028 parent->first_displayed_entry = s->first_displayed_entry;
2029 parent->selected_entry = s->selected_entry;
2030 parent->selected = s->selected;
2031 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2032 s->tree = subtree;
2033 s->selected = 0;
2034 s->first_displayed_entry = NULL;
2035 return NULL;
2038 static const struct got_error *
2039 tree_view_walk_path(struct tog_tree_view_state *s,
2040 struct got_commit_object *commit, const char *path)
2042 const struct got_error *err = NULL;
2043 struct got_tree_object *tree = NULL;
2044 const char *p;
2045 char *slash, *subpath = NULL;
2047 /* Walk the path and open corresponding tree objects. */
2048 p = path;
2049 while (*p) {
2050 struct got_tree_entry *te;
2051 struct got_object_id *tree_id;
2052 char *te_name;
2054 while (p[0] == '/')
2055 p++;
2057 /* Ensure the correct subtree entry is selected. */
2058 slash = strchr(p, '/');
2059 if (slash == NULL)
2060 te_name = strdup(p);
2061 else
2062 te_name = strndup(p, slash - p);
2063 if (te_name == NULL) {
2064 err = got_error_from_errno("strndup");
2065 break;
2067 te = got_object_tree_find_entry(s->tree, te_name);
2068 if (te == NULL) {
2069 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2070 free(te_name);
2071 break;
2073 free(te_name);
2074 s->first_displayed_entry = s->selected_entry = te;
2076 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2077 break; /* jump to this file's entry */
2079 slash = strchr(p, '/');
2080 if (slash)
2081 subpath = strndup(path, slash - path);
2082 else
2083 subpath = strdup(path);
2084 if (subpath == NULL) {
2085 err = got_error_from_errno("strdup");
2086 break;
2089 err = got_object_id_by_path(&tree_id, s->repo, commit,
2090 subpath);
2091 if (err)
2092 break;
2094 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2095 free(tree_id);
2096 if (err)
2097 break;
2099 err = tree_view_visit_subtree(s, tree);
2100 if (err) {
2101 got_object_tree_close(tree);
2102 break;
2104 if (slash == NULL)
2105 break;
2106 free(subpath);
2107 subpath = NULL;
2108 p = slash;
2111 free(subpath);
2112 return err;
2115 static const struct got_error *
2116 browse_commit_tree(struct tog_view **new_view, int begin_x,
2117 struct commit_queue_entry *entry, const char *path,
2118 const char *head_ref_name, struct got_repository *repo)
2120 const struct got_error *err = NULL;
2121 struct tog_tree_view_state *s;
2122 struct tog_view *tree_view;
2124 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2125 if (tree_view == NULL)
2126 return got_error_from_errno("view_open");
2128 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2129 if (err)
2130 return err;
2131 s = &tree_view->state.tree;
2133 *new_view = tree_view;
2135 if (got_path_is_root_dir(path))
2136 return NULL;
2138 return tree_view_walk_path(s, entry->commit, path);
2141 static const struct got_error *
2142 block_signals_used_by_main_thread(void)
2144 sigset_t sigset;
2145 int errcode;
2147 if (sigemptyset(&sigset) == -1)
2148 return got_error_from_errno("sigemptyset");
2150 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2151 if (sigaddset(&sigset, SIGWINCH) == -1)
2152 return got_error_from_errno("sigaddset");
2153 if (sigaddset(&sigset, SIGCONT) == -1)
2154 return got_error_from_errno("sigaddset");
2155 if (sigaddset(&sigset, SIGINT) == -1)
2156 return got_error_from_errno("sigaddset");
2157 if (sigaddset(&sigset, SIGTERM) == -1)
2158 return got_error_from_errno("sigaddset");
2160 /* ncurses handles SIGTSTP */
2161 if (sigaddset(&sigset, SIGTSTP) == -1)
2162 return got_error_from_errno("sigaddset");
2164 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2165 if (errcode)
2166 return got_error_set_errno(errcode, "pthread_sigmask");
2168 return NULL;
2171 static void *
2172 log_thread(void *arg)
2174 const struct got_error *err = NULL;
2175 int errcode = 0;
2176 struct tog_log_thread_args *a = arg;
2177 int done = 0;
2179 err = block_signals_used_by_main_thread();
2180 if (err)
2181 return (void *)err;
2183 while (!done && !err && !tog_fatal_signal_received()) {
2184 err = queue_commits(a);
2185 if (err) {
2186 if (err->code != GOT_ERR_ITER_COMPLETED)
2187 return (void *)err;
2188 err = NULL;
2189 done = 1;
2190 } else if (a->commits_needed > 0 && !a->load_all)
2191 a->commits_needed--;
2193 errcode = pthread_mutex_lock(&tog_mutex);
2194 if (errcode) {
2195 err = got_error_set_errno(errcode,
2196 "pthread_mutex_lock");
2197 break;
2198 } else if (*a->quit)
2199 done = 1;
2200 else if (*a->first_displayed_entry == NULL) {
2201 *a->first_displayed_entry =
2202 TAILQ_FIRST(&a->commits->head);
2203 *a->selected_entry = *a->first_displayed_entry;
2206 errcode = pthread_cond_signal(&a->commit_loaded);
2207 if (errcode) {
2208 err = got_error_set_errno(errcode,
2209 "pthread_cond_signal");
2210 pthread_mutex_unlock(&tog_mutex);
2211 break;
2214 if (done)
2215 a->commits_needed = 0;
2216 else {
2217 if (a->commits_needed == 0 && !a->load_all) {
2218 errcode = pthread_cond_wait(&a->need_commits,
2219 &tog_mutex);
2220 if (errcode)
2221 err = got_error_set_errno(errcode,
2222 "pthread_cond_wait");
2223 if (*a->quit)
2224 done = 1;
2228 errcode = pthread_mutex_unlock(&tog_mutex);
2229 if (errcode && err == NULL)
2230 err = got_error_set_errno(errcode,
2231 "pthread_mutex_unlock");
2233 a->log_complete = 1;
2234 return (void *)err;
2237 static const struct got_error *
2238 stop_log_thread(struct tog_log_view_state *s)
2240 const struct got_error *err = NULL;
2241 int errcode;
2243 if (s->thread) {
2244 s->quit = 1;
2245 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2246 if (errcode)
2247 return got_error_set_errno(errcode,
2248 "pthread_cond_signal");
2249 errcode = pthread_mutex_unlock(&tog_mutex);
2250 if (errcode)
2251 return got_error_set_errno(errcode,
2252 "pthread_mutex_unlock");
2253 errcode = pthread_join(s->thread, (void **)&err);
2254 if (errcode)
2255 return got_error_set_errno(errcode, "pthread_join");
2256 errcode = pthread_mutex_lock(&tog_mutex);
2257 if (errcode)
2258 return got_error_set_errno(errcode,
2259 "pthread_mutex_lock");
2260 s->thread = 0; //NULL;
2263 if (s->thread_args.repo) {
2264 err = got_repo_close(s->thread_args.repo);
2265 s->thread_args.repo = NULL;
2268 if (s->thread_args.pack_fds) {
2269 const struct got_error *pack_err =
2270 got_repo_pack_fds_close(s->thread_args.pack_fds);
2271 if (err == NULL)
2272 err = pack_err;
2273 s->thread_args.pack_fds = NULL;
2276 if (s->thread_args.graph) {
2277 got_commit_graph_close(s->thread_args.graph);
2278 s->thread_args.graph = NULL;
2281 return err;
2284 static const struct got_error *
2285 close_log_view(struct tog_view *view)
2287 const struct got_error *err = NULL;
2288 struct tog_log_view_state *s = &view->state.log;
2289 int errcode;
2291 err = stop_log_thread(s);
2293 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2294 if (errcode && err == NULL)
2295 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2297 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2298 if (errcode && err == NULL)
2299 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2301 free_commits(&s->commits);
2302 free(s->in_repo_path);
2303 s->in_repo_path = NULL;
2304 free(s->start_id);
2305 s->start_id = NULL;
2306 free(s->head_ref_name);
2307 s->head_ref_name = NULL;
2308 return err;
2311 static const struct got_error *
2312 search_start_log_view(struct tog_view *view)
2314 struct tog_log_view_state *s = &view->state.log;
2316 s->matched_entry = NULL;
2317 s->search_entry = NULL;
2318 return NULL;
2321 static const struct got_error *
2322 search_next_log_view(struct tog_view *view)
2324 const struct got_error *err = NULL;
2325 struct tog_log_view_state *s = &view->state.log;
2326 struct commit_queue_entry *entry;
2328 /* Display progress update in log view. */
2329 show_log_view(view);
2330 update_panels();
2331 doupdate();
2333 if (s->search_entry) {
2334 int errcode, ch;
2335 errcode = pthread_mutex_unlock(&tog_mutex);
2336 if (errcode)
2337 return got_error_set_errno(errcode,
2338 "pthread_mutex_unlock");
2339 ch = wgetch(view->window);
2340 errcode = pthread_mutex_lock(&tog_mutex);
2341 if (errcode)
2342 return got_error_set_errno(errcode,
2343 "pthread_mutex_lock");
2344 if (ch == KEY_BACKSPACE) {
2345 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2346 return NULL;
2348 if (view->searching == TOG_SEARCH_FORWARD)
2349 entry = TAILQ_NEXT(s->search_entry, entry);
2350 else
2351 entry = TAILQ_PREV(s->search_entry,
2352 commit_queue_head, entry);
2353 } else if (s->matched_entry) {
2354 int matched_idx = s->matched_entry->idx;
2355 int selected_idx = s->selected_entry->idx;
2358 * If the user has moved the cursor after we hit a match,
2359 * the position from where we should continue searching
2360 * might have changed.
2362 if (view->searching == TOG_SEARCH_FORWARD) {
2363 if (matched_idx > selected_idx)
2364 entry = TAILQ_NEXT(s->selected_entry, entry);
2365 else
2366 entry = TAILQ_NEXT(s->matched_entry, entry);
2367 } else {
2368 if (matched_idx < selected_idx)
2369 entry = TAILQ_PREV(s->selected_entry,
2370 commit_queue_head, entry);
2371 else
2372 entry = TAILQ_PREV(s->matched_entry,
2373 commit_queue_head, entry);
2375 } else {
2376 entry = s->selected_entry;
2379 while (1) {
2380 int have_match = 0;
2382 if (entry == NULL) {
2383 if (s->thread_args.log_complete ||
2384 view->searching == TOG_SEARCH_BACKWARD) {
2385 view->search_next_done =
2386 (s->matched_entry == NULL ?
2387 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2388 s->search_entry = NULL;
2389 return NULL;
2392 * Poke the log thread for more commits and return,
2393 * allowing the main loop to make progress. Search
2394 * will resume at s->search_entry once we come back.
2396 s->thread_args.commits_needed++;
2397 return trigger_log_thread(view, 0);
2400 err = match_commit(&have_match, entry->id, entry->commit,
2401 &view->regex);
2402 if (err)
2403 break;
2404 if (have_match) {
2405 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2406 s->matched_entry = entry;
2407 break;
2410 s->search_entry = entry;
2411 if (view->searching == TOG_SEARCH_FORWARD)
2412 entry = TAILQ_NEXT(entry, entry);
2413 else
2414 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2417 if (s->matched_entry) {
2418 int cur = s->selected_entry->idx;
2419 while (cur < s->matched_entry->idx) {
2420 err = input_log_view(NULL, view, KEY_DOWN);
2421 if (err)
2422 return err;
2423 cur++;
2425 while (cur > s->matched_entry->idx) {
2426 err = input_log_view(NULL, view, KEY_UP);
2427 if (err)
2428 return err;
2429 cur--;
2433 s->search_entry = NULL;
2435 return NULL;
2438 static const struct got_error *
2439 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2440 struct got_repository *repo, const char *head_ref_name,
2441 const char *in_repo_path, int log_branches)
2443 const struct got_error *err = NULL;
2444 struct tog_log_view_state *s = &view->state.log;
2445 struct got_repository *thread_repo = NULL;
2446 struct got_commit_graph *thread_graph = NULL;
2447 int errcode;
2449 if (in_repo_path != s->in_repo_path) {
2450 free(s->in_repo_path);
2451 s->in_repo_path = strdup(in_repo_path);
2452 if (s->in_repo_path == NULL)
2453 return got_error_from_errno("strdup");
2456 /* The commit queue only contains commits being displayed. */
2457 TAILQ_INIT(&s->commits.head);
2458 s->commits.ncommits = 0;
2460 s->repo = repo;
2461 if (head_ref_name) {
2462 s->head_ref_name = strdup(head_ref_name);
2463 if (s->head_ref_name == NULL) {
2464 err = got_error_from_errno("strdup");
2465 goto done;
2468 s->start_id = got_object_id_dup(start_id);
2469 if (s->start_id == NULL) {
2470 err = got_error_from_errno("got_object_id_dup");
2471 goto done;
2473 s->log_branches = log_branches;
2475 STAILQ_INIT(&s->colors);
2476 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2477 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2478 get_color_value("TOG_COLOR_COMMIT"));
2479 if (err)
2480 goto done;
2481 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2482 get_color_value("TOG_COLOR_AUTHOR"));
2483 if (err) {
2484 free_colors(&s->colors);
2485 goto done;
2487 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2488 get_color_value("TOG_COLOR_DATE"));
2489 if (err) {
2490 free_colors(&s->colors);
2491 goto done;
2495 view->show = show_log_view;
2496 view->input = input_log_view;
2497 view->close = close_log_view;
2498 view->search_start = search_start_log_view;
2499 view->search_next = search_next_log_view;
2501 if (s->thread_args.pack_fds == NULL) {
2502 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2503 if (err)
2504 goto done;
2506 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2507 s->thread_args.pack_fds);
2508 if (err)
2509 goto done;
2510 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2511 !s->log_branches);
2512 if (err)
2513 goto done;
2514 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2515 s->repo, NULL, NULL);
2516 if (err)
2517 goto done;
2519 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2520 if (errcode) {
2521 err = got_error_set_errno(errcode, "pthread_cond_init");
2522 goto done;
2524 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2525 if (errcode) {
2526 err = got_error_set_errno(errcode, "pthread_cond_init");
2527 goto done;
2530 s->thread_args.commits_needed = view->nlines;
2531 s->thread_args.graph = thread_graph;
2532 s->thread_args.commits = &s->commits;
2533 s->thread_args.in_repo_path = s->in_repo_path;
2534 s->thread_args.start_id = s->start_id;
2535 s->thread_args.repo = thread_repo;
2536 s->thread_args.log_complete = 0;
2537 s->thread_args.quit = &s->quit;
2538 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2539 s->thread_args.selected_entry = &s->selected_entry;
2540 s->thread_args.searching = &view->searching;
2541 s->thread_args.search_next_done = &view->search_next_done;
2542 s->thread_args.regex = &view->regex;
2543 done:
2544 if (err)
2545 close_log_view(view);
2546 return err;
2549 static const struct got_error *
2550 show_log_view(struct tog_view *view)
2552 const struct got_error *err;
2553 struct tog_log_view_state *s = &view->state.log;
2555 if (s->thread == 0) { //NULL) {
2556 int errcode = pthread_create(&s->thread, NULL, log_thread,
2557 &s->thread_args);
2558 if (errcode)
2559 return got_error_set_errno(errcode, "pthread_create");
2560 if (s->thread_args.commits_needed > 0) {
2561 err = trigger_log_thread(view, 1);
2562 if (err)
2563 return err;
2567 return draw_commits(view);
2570 static const struct got_error *
2571 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2573 const struct got_error *err = NULL;
2574 struct tog_log_view_state *s = &view->state.log;
2575 struct tog_view *diff_view = NULL, *tree_view = NULL;
2576 struct tog_view *ref_view = NULL;
2577 struct commit_queue_entry *entry;
2578 int begin_x = 0, n, nscroll = view->nlines - 1;
2580 if (s->thread_args.load_all) {
2581 if (ch == KEY_BACKSPACE)
2582 s->thread_args.load_all = 0;
2583 else if (s->thread_args.log_complete) {
2584 s->thread_args.load_all = 0;
2585 log_scroll_down(view, s->commits.ncommits);
2586 s->selected = MIN(view->nlines - 2,
2587 s->commits.ncommits - 1);
2588 select_commit(s);
2590 return NULL;
2593 switch (ch) {
2594 case 'q':
2595 s->quit = 1;
2596 break;
2597 case '0':
2598 view->x = 0;
2599 break;
2600 case '$':
2601 view->x = MAX(view->maxx - view->ncols / 2, 0);
2602 break;
2603 case KEY_RIGHT:
2604 case 'l':
2605 if (view->x + view->ncols / 2 < view->maxx)
2606 view->x += 2; /* move two columns right */
2607 break;
2608 case KEY_LEFT:
2609 case 'h':
2610 view->x -= MIN(view->x, 2); /* move two columns back */
2611 break;
2612 case 'k':
2613 case KEY_UP:
2614 case '<':
2615 case ',':
2616 case CTRL('p'):
2617 if (s->first_displayed_entry == NULL)
2618 break;
2619 if (s->selected > 0)
2620 s->selected--;
2621 else
2622 log_scroll_up(s, 1);
2623 select_commit(s);
2624 break;
2625 case 'g':
2626 case KEY_HOME:
2627 s->selected = 0;
2628 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2629 select_commit(s);
2630 break;
2631 case CTRL('u'):
2632 case 'u':
2633 nscroll /= 2;
2634 /* FALL THROUGH */
2635 case KEY_PPAGE:
2636 case CTRL('b'):
2637 if (s->first_displayed_entry == NULL)
2638 break;
2639 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2640 s->selected = MAX(0, s->selected - nscroll - 1);
2641 else
2642 log_scroll_up(s, nscroll);
2643 select_commit(s);
2644 break;
2645 case 'j':
2646 case KEY_DOWN:
2647 case '>':
2648 case '.':
2649 case CTRL('n'):
2650 if (s->first_displayed_entry == NULL)
2651 break;
2652 if (s->selected < MIN(view->nlines - 2,
2653 s->commits.ncommits - 1))
2654 s->selected++;
2655 else {
2656 err = log_scroll_down(view, 1);
2657 if (err)
2658 break;
2660 select_commit(s);
2661 break;
2662 case 'G':
2663 case KEY_END: {
2664 /* We don't know yet how many commits, so we're forced to
2665 * traverse them all. */
2666 if (!s->thread_args.log_complete) {
2667 s->thread_args.load_all = 1;
2668 return trigger_log_thread(view, 0);
2671 s->selected = 0;
2672 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2673 for (n = 0; n < view->nlines - 1; n++) {
2674 if (entry == NULL)
2675 break;
2676 s->first_displayed_entry = entry;
2677 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2679 if (n > 0)
2680 s->selected = n - 1;
2681 select_commit(s);
2682 break;
2684 case CTRL('d'):
2685 case 'd':
2686 nscroll /= 2;
2687 /* FALL THROUGH */
2688 case KEY_NPAGE:
2689 case CTRL('f'): {
2690 struct commit_queue_entry *first;
2691 first = s->first_displayed_entry;
2692 if (first == NULL)
2693 break;
2694 err = log_scroll_down(view, nscroll);
2695 if (err)
2696 break;
2697 if (first == s->first_displayed_entry &&
2698 s->selected < MIN(view->nlines - 2,
2699 s->commits.ncommits - 1)) {
2700 /* can't scroll further down */
2701 s->selected += MIN(s->last_displayed_entry->idx -
2702 s->selected_entry->idx, nscroll + 1);
2704 select_commit(s);
2705 break;
2707 case KEY_RESIZE:
2708 if (s->selected > view->nlines - 2)
2709 s->selected = view->nlines - 2;
2710 if (s->selected > s->commits.ncommits - 1)
2711 s->selected = s->commits.ncommits - 1;
2712 select_commit(s);
2713 if (s->commits.ncommits < view->nlines - 1 &&
2714 !s->thread_args.log_complete) {
2715 s->thread_args.commits_needed += (view->nlines - 1) -
2716 s->commits.ncommits;
2717 err = trigger_log_thread(view, 1);
2719 break;
2720 case KEY_ENTER:
2721 case ' ':
2722 case '\r':
2723 if (s->selected_entry == NULL)
2724 break;
2725 if (view_is_parent_view(view))
2726 begin_x = view_split_begin_x(view->begin_x);
2727 err = open_diff_view_for_commit(&diff_view, begin_x,
2728 s->selected_entry->commit, s->selected_entry->id,
2729 view, s->repo);
2730 if (err)
2731 break;
2732 view->focussed = 0;
2733 diff_view->focussed = 1;
2734 if (view_is_parent_view(view)) {
2735 err = view_close_child(view);
2736 if (err)
2737 return err;
2738 err = view_set_child(view, diff_view);
2739 if (err)
2740 return err;
2741 view->focus_child = 1;
2742 } else
2743 *new_view = diff_view;
2744 break;
2745 case 't':
2746 if (s->selected_entry == NULL)
2747 break;
2748 if (view_is_parent_view(view))
2749 begin_x = view_split_begin_x(view->begin_x);
2750 err = browse_commit_tree(&tree_view, begin_x,
2751 s->selected_entry, s->in_repo_path, s->head_ref_name,
2752 s->repo);
2753 if (err)
2754 break;
2755 view->focussed = 0;
2756 tree_view->focussed = 1;
2757 if (view_is_parent_view(view)) {
2758 err = view_close_child(view);
2759 if (err)
2760 return err;
2761 err = view_set_child(view, tree_view);
2762 if (err)
2763 return err;
2764 view->focus_child = 1;
2765 } else
2766 *new_view = tree_view;
2767 break;
2768 case KEY_BACKSPACE:
2769 case CTRL('l'):
2770 case 'B':
2771 if (ch == KEY_BACKSPACE &&
2772 got_path_is_root_dir(s->in_repo_path))
2773 break;
2774 err = stop_log_thread(s);
2775 if (err)
2776 return err;
2777 if (ch == KEY_BACKSPACE) {
2778 char *parent_path;
2779 err = got_path_dirname(&parent_path, s->in_repo_path);
2780 if (err)
2781 return err;
2782 free(s->in_repo_path);
2783 s->in_repo_path = parent_path;
2784 s->thread_args.in_repo_path = s->in_repo_path;
2785 } else if (ch == CTRL('l')) {
2786 struct got_object_id *start_id;
2787 err = got_repo_match_object_id(&start_id, NULL,
2788 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2789 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2790 if (err)
2791 return err;
2792 free(s->start_id);
2793 s->start_id = start_id;
2794 s->thread_args.start_id = s->start_id;
2795 } else /* 'B' */
2796 s->log_branches = !s->log_branches;
2798 if (s->thread_args.pack_fds == NULL) {
2799 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2800 if (err)
2801 return err;
2803 err = got_repo_open(&s->thread_args.repo,
2804 got_repo_get_path(s->repo), NULL,
2805 s->thread_args.pack_fds);
2806 if (err)
2807 return err;
2808 tog_free_refs();
2809 err = tog_load_refs(s->repo, 0);
2810 if (err)
2811 return err;
2812 err = got_commit_graph_open(&s->thread_args.graph,
2813 s->in_repo_path, !s->log_branches);
2814 if (err)
2815 return err;
2816 err = got_commit_graph_iter_start(s->thread_args.graph,
2817 s->start_id, s->repo, NULL, NULL);
2818 if (err)
2819 return err;
2820 free_commits(&s->commits);
2821 s->first_displayed_entry = NULL;
2822 s->last_displayed_entry = NULL;
2823 s->selected_entry = NULL;
2824 s->selected = 0;
2825 s->thread_args.log_complete = 0;
2826 s->quit = 0;
2827 s->thread_args.commits_needed = view->nlines;
2828 s->matched_entry = NULL;
2829 s->search_entry = NULL;
2830 break;
2831 case 'r':
2832 if (view_is_parent_view(view))
2833 begin_x = view_split_begin_x(view->begin_x);
2834 ref_view = view_open(view->nlines, view->ncols,
2835 view->begin_y, begin_x, TOG_VIEW_REF);
2836 if (ref_view == NULL)
2837 return got_error_from_errno("view_open");
2838 err = open_ref_view(ref_view, s->repo);
2839 if (err) {
2840 view_close(ref_view);
2841 return err;
2843 view->focussed = 0;
2844 ref_view->focussed = 1;
2845 if (view_is_parent_view(view)) {
2846 err = view_close_child(view);
2847 if (err)
2848 return err;
2849 err = view_set_child(view, ref_view);
2850 if (err)
2851 return err;
2852 view->focus_child = 1;
2853 } else
2854 *new_view = ref_view;
2855 break;
2856 default:
2857 break;
2860 return err;
2863 static const struct got_error *
2864 apply_unveil(const char *repo_path, const char *worktree_path)
2866 const struct got_error *error;
2868 #ifdef PROFILE
2869 if (unveil("gmon.out", "rwc") != 0)
2870 return got_error_from_errno2("unveil", "gmon.out");
2871 #endif
2872 if (repo_path && unveil(repo_path, "r") != 0)
2873 return got_error_from_errno2("unveil", repo_path);
2875 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2876 return got_error_from_errno2("unveil", worktree_path);
2878 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2879 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2881 error = got_privsep_unveil_exec_helpers();
2882 if (error != NULL)
2883 return error;
2885 if (unveil(NULL, NULL) != 0)
2886 return got_error_from_errno("unveil");
2888 return NULL;
2891 static void
2892 init_curses(void)
2895 * Override default signal handlers before starting ncurses.
2896 * This should prevent ncurses from installing its own
2897 * broken cleanup() signal handler.
2899 signal(SIGWINCH, tog_sigwinch);
2900 signal(SIGPIPE, tog_sigpipe);
2901 signal(SIGCONT, tog_sigcont);
2902 signal(SIGINT, tog_sigint);
2903 signal(SIGTERM, tog_sigterm);
2905 initscr();
2906 cbreak();
2907 halfdelay(1); /* Do fast refresh while initial view is loading. */
2908 noecho();
2909 nonl();
2910 intrflush(stdscr, FALSE);
2911 keypad(stdscr, TRUE);
2912 curs_set(0);
2913 if (getenv("TOG_COLORS") != NULL) {
2914 start_color();
2915 use_default_colors();
2919 static const struct got_error *
2920 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2921 struct got_repository *repo, struct got_worktree *worktree)
2923 const struct got_error *err = NULL;
2925 if (argc == 0) {
2926 *in_repo_path = strdup("/");
2927 if (*in_repo_path == NULL)
2928 return got_error_from_errno("strdup");
2929 return NULL;
2932 if (worktree) {
2933 const char *prefix = got_worktree_get_path_prefix(worktree);
2934 char *p;
2936 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2937 if (err)
2938 return err;
2939 if (asprintf(in_repo_path, "%s%s%s", prefix,
2940 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2941 p) == -1) {
2942 err = got_error_from_errno("asprintf");
2943 *in_repo_path = NULL;
2945 free(p);
2946 } else
2947 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2949 return err;
2952 static const struct got_error *
2953 cmd_log(int argc, char *argv[])
2955 const struct got_error *error;
2956 struct got_repository *repo = NULL;
2957 struct got_worktree *worktree = NULL;
2958 struct got_object_id *start_id = NULL;
2959 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2960 char *start_commit = NULL, *label = NULL;
2961 struct got_reference *ref = NULL;
2962 const char *head_ref_name = NULL;
2963 int ch, log_branches = 0;
2964 struct tog_view *view;
2965 int *pack_fds = NULL;
2967 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2968 switch (ch) {
2969 case 'b':
2970 log_branches = 1;
2971 break;
2972 case 'c':
2973 start_commit = optarg;
2974 break;
2975 case 'r':
2976 repo_path = realpath(optarg, NULL);
2977 if (repo_path == NULL)
2978 return got_error_from_errno2("realpath",
2979 optarg);
2980 break;
2981 default:
2982 usage_log();
2983 /* NOTREACHED */
2987 argc -= optind;
2988 argv += optind;
2990 if (argc > 1)
2991 usage_log();
2993 error = got_repo_pack_fds_open(&pack_fds);
2994 if (error != NULL)
2995 goto done;
2997 if (repo_path == NULL) {
2998 cwd = getcwd(NULL, 0);
2999 if (cwd == NULL)
3000 return got_error_from_errno("getcwd");
3001 error = got_worktree_open(&worktree, cwd);
3002 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3003 goto done;
3004 if (worktree)
3005 repo_path =
3006 strdup(got_worktree_get_repo_path(worktree));
3007 else
3008 repo_path = strdup(cwd);
3009 if (repo_path == NULL) {
3010 error = got_error_from_errno("strdup");
3011 goto done;
3015 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3016 if (error != NULL)
3017 goto done;
3019 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3020 repo, worktree);
3021 if (error)
3022 goto done;
3024 init_curses();
3026 error = apply_unveil(got_repo_get_path(repo),
3027 worktree ? got_worktree_get_root_path(worktree) : NULL);
3028 if (error)
3029 goto done;
3031 /* already loaded by tog_log_with_path()? */
3032 if (TAILQ_EMPTY(&tog_refs)) {
3033 error = tog_load_refs(repo, 0);
3034 if (error)
3035 goto done;
3038 if (start_commit == NULL) {
3039 error = got_repo_match_object_id(&start_id, &label,
3040 worktree ? got_worktree_get_head_ref_name(worktree) :
3041 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3042 if (error)
3043 goto done;
3044 head_ref_name = label;
3045 } else {
3046 error = got_ref_open(&ref, repo, start_commit, 0);
3047 if (error == NULL)
3048 head_ref_name = got_ref_get_name(ref);
3049 else if (error->code != GOT_ERR_NOT_REF)
3050 goto done;
3051 error = got_repo_match_object_id(&start_id, NULL,
3052 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3053 if (error)
3054 goto done;
3057 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3058 if (view == NULL) {
3059 error = got_error_from_errno("view_open");
3060 goto done;
3062 error = open_log_view(view, start_id, repo, head_ref_name,
3063 in_repo_path, log_branches);
3064 if (error)
3065 goto done;
3066 if (worktree) {
3067 /* Release work tree lock. */
3068 got_worktree_close(worktree);
3069 worktree = NULL;
3071 error = view_loop(view);
3072 done:
3073 free(in_repo_path);
3074 free(repo_path);
3075 free(cwd);
3076 free(start_id);
3077 free(label);
3078 if (ref)
3079 got_ref_close(ref);
3080 if (repo) {
3081 const struct got_error *close_err = got_repo_close(repo);
3082 if (error == NULL)
3083 error = close_err;
3085 if (worktree)
3086 got_worktree_close(worktree);
3087 if (pack_fds) {
3088 const struct got_error *pack_err =
3089 got_repo_pack_fds_close(pack_fds);
3090 if (error == NULL)
3091 error = pack_err;
3093 tog_free_refs();
3094 return error;
3097 __dead static void
3098 usage_diff(void)
3100 endwin();
3101 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3102 "[-w] object1 object2\n", getprogname());
3103 exit(1);
3106 static int
3107 match_line(const char *line, regex_t *regex, size_t nmatch,
3108 regmatch_t *regmatch)
3110 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3113 struct tog_color *
3114 match_color(struct tog_colors *colors, const char *line)
3116 struct tog_color *tc = NULL;
3118 STAILQ_FOREACH(tc, colors, entry) {
3119 if (match_line(line, &tc->regex, 0, NULL))
3120 return tc;
3123 return NULL;
3126 static const struct got_error *
3127 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3128 WINDOW *window, int skipcol, regmatch_t *regmatch)
3130 const struct got_error *err = NULL;
3131 char *exstr = NULL;
3132 wchar_t *wline = NULL;
3133 int rme, rms, n, width, scrollx;
3134 int width0 = 0, width1 = 0, width2 = 0;
3135 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3137 *wtotal = 0;
3139 rms = regmatch->rm_so;
3140 rme = regmatch->rm_eo;
3142 err = expand_tab(&exstr, line);
3143 if (err)
3144 return err;
3146 /* Split the line into 3 segments, according to match offsets. */
3147 seg0 = strndup(exstr, rms);
3148 if (seg0 == NULL) {
3149 err = got_error_from_errno("strndup");
3150 goto done;
3152 seg1 = strndup(exstr + rms, rme - rms);
3153 if (seg1 == NULL) {
3154 err = got_error_from_errno("strndup");
3155 goto done;
3157 seg2 = strdup(exstr + rme);
3158 if (seg2 == NULL) {
3159 err = got_error_from_errno("strndup");
3160 goto done;
3163 /* draw up to matched token if we haven't scrolled past it */
3164 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3165 col_tab_align, 1);
3166 if (err)
3167 goto done;
3168 n = MAX(width0 - skipcol, 0);
3169 if (n) {
3170 free(wline);
3171 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3172 wlimit, col_tab_align, 1);
3173 if (err)
3174 goto done;
3175 waddwstr(window, &wline[scrollx]);
3176 wlimit -= width;
3177 *wtotal += width;
3180 if (wlimit > 0) {
3181 int i = 0, w = 0;
3182 size_t wlen;
3184 free(wline);
3185 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3186 col_tab_align, 1);
3187 if (err)
3188 goto done;
3189 wlen = wcslen(wline);
3190 while (i < wlen) {
3191 width = wcwidth(wline[i]);
3192 if (width == -1) {
3193 /* should not happen, tabs are expanded */
3194 err = got_error(GOT_ERR_RANGE);
3195 goto done;
3197 if (width0 + w + width > skipcol)
3198 break;
3199 w += width;
3200 i++;
3202 /* draw (visible part of) matched token (if scrolled into it) */
3203 if (width1 - w > 0) {
3204 wattron(window, A_STANDOUT);
3205 waddwstr(window, &wline[i]);
3206 wattroff(window, A_STANDOUT);
3207 wlimit -= (width1 - w);
3208 *wtotal += (width1 - w);
3212 if (wlimit > 0) { /* draw rest of line */
3213 free(wline);
3214 if (skipcol > width0 + width1) {
3215 err = format_line(&wline, &width2, &scrollx, seg2,
3216 skipcol - (width0 + width1), wlimit,
3217 col_tab_align, 1);
3218 if (err)
3219 goto done;
3220 waddwstr(window, &wline[scrollx]);
3221 } else {
3222 err = format_line(&wline, &width2, NULL, seg2, 0,
3223 wlimit, col_tab_align, 1);
3224 if (err)
3225 goto done;
3226 waddwstr(window, wline);
3228 *wtotal += width2;
3230 done:
3231 free(wline);
3232 free(exstr);
3233 free(seg0);
3234 free(seg1);
3235 free(seg2);
3236 return err;
3239 static const struct got_error *
3240 draw_file(struct tog_view *view, const char *header)
3242 struct tog_diff_view_state *s = &view->state.diff;
3243 regmatch_t *regmatch = &view->regmatch;
3244 const struct got_error *err;
3245 int nprinted = 0;
3246 char *line;
3247 size_t linesize = 0;
3248 ssize_t linelen;
3249 struct tog_color *tc;
3250 wchar_t *wline;
3251 int width;
3252 int max_lines = view->nlines;
3253 int nlines = s->nlines;
3254 off_t line_offset;
3256 line_offset = s->line_offsets[s->first_displayed_line - 1];
3257 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3258 return got_error_from_errno("fseek");
3260 werase(view->window);
3262 if (header) {
3263 if (asprintf(&line, "[%d/%d] %s",
3264 s->first_displayed_line - 1 + s->selected_line, nlines,
3265 header) == -1)
3266 return got_error_from_errno("asprintf");
3267 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3268 0, 0);
3269 free(line);
3270 if (err)
3271 return err;
3273 if (view_needs_focus_indication(view))
3274 wstandout(view->window);
3275 waddwstr(view->window, wline);
3276 free(wline);
3277 wline = NULL;
3278 if (view_needs_focus_indication(view))
3279 wstandend(view->window);
3280 if (width <= view->ncols - 1)
3281 waddch(view->window, '\n');
3283 if (max_lines <= 1)
3284 return NULL;
3285 max_lines--;
3288 s->eof = 0;
3289 view->maxx = 0;
3290 line = NULL;
3291 while (max_lines > 0 && nprinted < max_lines) {
3292 linelen = getline(&line, &linesize, s->f);
3293 if (linelen == -1) {
3294 if (feof(s->f)) {
3295 s->eof = 1;
3296 break;
3298 free(line);
3299 return got_ferror(s->f, GOT_ERR_IO);
3302 /* Set view->maxx based on full line length. */
3303 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3304 view->x ? 1 : 0);
3305 if (err) {
3306 free(line);
3307 return err;
3309 view->maxx = MAX(view->maxx, width);
3310 free(wline);
3311 wline = NULL;
3313 tc = match_color(&s->colors, line);
3314 if (tc)
3315 wattr_on(view->window,
3316 COLOR_PAIR(tc->colorpair), NULL);
3317 if (s->first_displayed_line + nprinted == s->matched_line &&
3318 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3319 err = add_matched_line(&width, line, view->ncols, 0,
3320 view->window, view->x, regmatch);
3321 if (err) {
3322 free(line);
3323 return err;
3325 } else {
3326 int skip;
3327 err = format_line(&wline, &width, &skip, line,
3328 view->x, view->ncols, 0, view->x ? 1 : 0);
3329 if (err) {
3330 free(line);
3331 return err;
3333 waddwstr(view->window, &wline[skip]);
3334 free(wline);
3335 wline = NULL;
3337 if (tc)
3338 wattr_off(view->window,
3339 COLOR_PAIR(tc->colorpair), NULL);
3340 if (width <= view->ncols - 1)
3341 waddch(view->window, '\n');
3342 nprinted++;
3344 free(line);
3345 if (nprinted >= 1)
3346 s->last_displayed_line = s->first_displayed_line +
3347 (nprinted - 1);
3348 else
3349 s->last_displayed_line = s->first_displayed_line;
3351 view_vborder(view);
3353 if (s->eof) {
3354 while (nprinted < view->nlines) {
3355 waddch(view->window, '\n');
3356 nprinted++;
3359 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3360 view->ncols, 0, 0);
3361 if (err) {
3362 return err;
3365 wstandout(view->window);
3366 waddwstr(view->window, wline);
3367 free(wline);
3368 wline = NULL;
3369 wstandend(view->window);
3372 return NULL;
3375 static char *
3376 get_datestr(time_t *time, char *datebuf)
3378 struct tm mytm, *tm;
3379 char *p, *s;
3381 tm = gmtime_r(time, &mytm);
3382 if (tm == NULL)
3383 return NULL;
3384 s = asctime_r(tm, datebuf);
3385 if (s == NULL)
3386 return NULL;
3387 p = strchr(s, '\n');
3388 if (p)
3389 *p = '\0';
3390 return s;
3393 static const struct got_error *
3394 get_changed_paths(struct got_pathlist_head *paths,
3395 struct got_commit_object *commit, struct got_repository *repo)
3397 const struct got_error *err = NULL;
3398 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3399 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3400 struct got_object_qid *qid;
3402 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3403 if (qid != NULL) {
3404 struct got_commit_object *pcommit;
3405 err = got_object_open_as_commit(&pcommit, repo,
3406 &qid->id);
3407 if (err)
3408 return err;
3410 tree_id1 = got_object_id_dup(
3411 got_object_commit_get_tree_id(pcommit));
3412 if (tree_id1 == NULL) {
3413 got_object_commit_close(pcommit);
3414 return got_error_from_errno("got_object_id_dup");
3416 got_object_commit_close(pcommit);
3420 if (tree_id1) {
3421 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3422 if (err)
3423 goto done;
3426 tree_id2 = got_object_commit_get_tree_id(commit);
3427 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3428 if (err)
3429 goto done;
3431 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3432 got_diff_tree_collect_changed_paths, paths, 0);
3433 done:
3434 if (tree1)
3435 got_object_tree_close(tree1);
3436 if (tree2)
3437 got_object_tree_close(tree2);
3438 free(tree_id1);
3439 return err;
3442 static const struct got_error *
3443 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3445 off_t *p;
3447 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3448 if (p == NULL)
3449 return got_error_from_errno("reallocarray");
3450 *line_offsets = p;
3451 (*line_offsets)[*nlines] = off;
3452 (*nlines)++;
3453 return NULL;
3456 static const struct got_error *
3457 write_commit_info(off_t **line_offsets, size_t *nlines,
3458 struct got_object_id *commit_id, struct got_reflist_head *refs,
3459 struct got_repository *repo, FILE *outfile)
3461 const struct got_error *err = NULL;
3462 char datebuf[26], *datestr;
3463 struct got_commit_object *commit;
3464 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3465 time_t committer_time;
3466 const char *author, *committer;
3467 char *refs_str = NULL;
3468 struct got_pathlist_head changed_paths;
3469 struct got_pathlist_entry *pe;
3470 off_t outoff = 0;
3471 int n;
3473 TAILQ_INIT(&changed_paths);
3475 if (refs) {
3476 err = build_refs_str(&refs_str, refs, commit_id, repo);
3477 if (err)
3478 return err;
3481 err = got_object_open_as_commit(&commit, repo, commit_id);
3482 if (err)
3483 return err;
3485 err = got_object_id_str(&id_str, commit_id);
3486 if (err) {
3487 err = got_error_from_errno("got_object_id_str");
3488 goto done;
3491 err = add_line_offset(line_offsets, nlines, 0);
3492 if (err)
3493 goto done;
3495 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3496 refs_str ? refs_str : "", refs_str ? ")" : "");
3497 if (n < 0) {
3498 err = got_error_from_errno("fprintf");
3499 goto done;
3501 outoff += n;
3502 err = add_line_offset(line_offsets, nlines, outoff);
3503 if (err)
3504 goto done;
3506 n = fprintf(outfile, "from: %s\n",
3507 got_object_commit_get_author(commit));
3508 if (n < 0) {
3509 err = got_error_from_errno("fprintf");
3510 goto done;
3512 outoff += n;
3513 err = add_line_offset(line_offsets, nlines, outoff);
3514 if (err)
3515 goto done;
3517 committer_time = got_object_commit_get_committer_time(commit);
3518 datestr = get_datestr(&committer_time, datebuf);
3519 if (datestr) {
3520 n = fprintf(outfile, "date: %s UTC\n", datestr);
3521 if (n < 0) {
3522 err = got_error_from_errno("fprintf");
3523 goto done;
3525 outoff += n;
3526 err = add_line_offset(line_offsets, nlines, outoff);
3527 if (err)
3528 goto done;
3530 author = got_object_commit_get_author(commit);
3531 committer = got_object_commit_get_committer(commit);
3532 if (strcmp(author, committer) != 0) {
3533 n = fprintf(outfile, "via: %s\n", committer);
3534 if (n < 0) {
3535 err = got_error_from_errno("fprintf");
3536 goto done;
3538 outoff += n;
3539 err = add_line_offset(line_offsets, nlines, outoff);
3540 if (err)
3541 goto done;
3543 if (got_object_commit_get_nparents(commit) > 1) {
3544 const struct got_object_id_queue *parent_ids;
3545 struct got_object_qid *qid;
3546 int pn = 1;
3547 parent_ids = got_object_commit_get_parent_ids(commit);
3548 STAILQ_FOREACH(qid, parent_ids, entry) {
3549 err = got_object_id_str(&id_str, &qid->id);
3550 if (err)
3551 goto done;
3552 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3553 if (n < 0) {
3554 err = got_error_from_errno("fprintf");
3555 goto done;
3557 outoff += n;
3558 err = add_line_offset(line_offsets, nlines, outoff);
3559 if (err)
3560 goto done;
3561 free(id_str);
3562 id_str = NULL;
3566 err = got_object_commit_get_logmsg(&logmsg, commit);
3567 if (err)
3568 goto done;
3569 s = logmsg;
3570 while ((line = strsep(&s, "\n")) != NULL) {
3571 n = fprintf(outfile, "%s\n", line);
3572 if (n < 0) {
3573 err = got_error_from_errno("fprintf");
3574 goto done;
3576 outoff += n;
3577 err = add_line_offset(line_offsets, nlines, outoff);
3578 if (err)
3579 goto done;
3582 err = get_changed_paths(&changed_paths, commit, repo);
3583 if (err)
3584 goto done;
3585 TAILQ_FOREACH(pe, &changed_paths, entry) {
3586 struct got_diff_changed_path *cp = pe->data;
3587 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3588 if (n < 0) {
3589 err = got_error_from_errno("fprintf");
3590 goto done;
3592 outoff += n;
3593 err = add_line_offset(line_offsets, nlines, outoff);
3594 if (err)
3595 goto done;
3596 free((char *)pe->path);
3597 free(pe->data);
3600 fputc('\n', outfile);
3601 outoff++;
3602 err = add_line_offset(line_offsets, nlines, outoff);
3603 done:
3604 got_pathlist_free(&changed_paths);
3605 free(id_str);
3606 free(logmsg);
3607 free(refs_str);
3608 got_object_commit_close(commit);
3609 if (err) {
3610 free(*line_offsets);
3611 *line_offsets = NULL;
3612 *nlines = 0;
3614 return err;
3617 static const struct got_error *
3618 create_diff(struct tog_diff_view_state *s)
3620 const struct got_error *err = NULL;
3621 FILE *f = NULL;
3622 int obj_type;
3624 free(s->line_offsets);
3625 s->line_offsets = malloc(sizeof(off_t));
3626 if (s->line_offsets == NULL)
3627 return got_error_from_errno("malloc");
3628 s->nlines = 0;
3630 f = got_opentemp();
3631 if (f == NULL) {
3632 err = got_error_from_errno("got_opentemp");
3633 goto done;
3635 if (s->f && fclose(s->f) == EOF) {
3636 err = got_error_from_errno("fclose");
3637 goto done;
3639 s->f = f;
3641 if (s->id1)
3642 err = got_object_get_type(&obj_type, s->repo, s->id1);
3643 else
3644 err = got_object_get_type(&obj_type, s->repo, s->id2);
3645 if (err)
3646 goto done;
3648 switch (obj_type) {
3649 case GOT_OBJ_TYPE_BLOB:
3650 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3651 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3652 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3653 s->repo, s->f);
3654 break;
3655 case GOT_OBJ_TYPE_TREE:
3656 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3657 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3658 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3659 break;
3660 case GOT_OBJ_TYPE_COMMIT: {
3661 const struct got_object_id_queue *parent_ids;
3662 struct got_object_qid *pid;
3663 struct got_commit_object *commit2;
3664 struct got_reflist_head *refs;
3666 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3667 if (err)
3668 goto done;
3669 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3670 /* Show commit info if we're diffing to a parent/root commit. */
3671 if (s->id1 == NULL) {
3672 err = write_commit_info(&s->line_offsets, &s->nlines,
3673 s->id2, refs, s->repo, s->f);
3674 if (err)
3675 goto done;
3676 } else {
3677 parent_ids = got_object_commit_get_parent_ids(commit2);
3678 STAILQ_FOREACH(pid, parent_ids, entry) {
3679 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3680 err = write_commit_info(
3681 &s->line_offsets, &s->nlines,
3682 s->id2, refs, s->repo, s->f);
3683 if (err)
3684 goto done;
3685 break;
3689 got_object_commit_close(commit2);
3691 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3692 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3693 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3694 break;
3696 default:
3697 err = got_error(GOT_ERR_OBJ_TYPE);
3698 break;
3700 if (err)
3701 goto done;
3702 done:
3703 if (s->f && fflush(s->f) != 0 && err == NULL)
3704 err = got_error_from_errno("fflush");
3705 return err;
3708 static void
3709 diff_view_indicate_progress(struct tog_view *view)
3711 mvwaddstr(view->window, 0, 0, "diffing...");
3712 update_panels();
3713 doupdate();
3716 static const struct got_error *
3717 search_start_diff_view(struct tog_view *view)
3719 struct tog_diff_view_state *s = &view->state.diff;
3721 s->matched_line = 0;
3722 return NULL;
3725 static const struct got_error *
3726 search_next_diff_view(struct tog_view *view)
3728 struct tog_diff_view_state *s = &view->state.diff;
3729 const struct got_error *err = NULL;
3730 int lineno;
3731 char *line = NULL;
3732 size_t linesize = 0;
3733 ssize_t linelen;
3735 if (!view->searching) {
3736 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3737 return NULL;
3740 if (s->matched_line) {
3741 if (view->searching == TOG_SEARCH_FORWARD)
3742 lineno = s->matched_line + 1;
3743 else
3744 lineno = s->matched_line - 1;
3745 } else
3746 lineno = s->first_displayed_line;
3748 while (1) {
3749 off_t offset;
3751 if (lineno <= 0 || lineno > s->nlines) {
3752 if (s->matched_line == 0) {
3753 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3754 break;
3757 if (view->searching == TOG_SEARCH_FORWARD)
3758 lineno = 1;
3759 else
3760 lineno = s->nlines;
3763 offset = s->line_offsets[lineno - 1];
3764 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3765 free(line);
3766 return got_error_from_errno("fseeko");
3768 linelen = getline(&line, &linesize, s->f);
3769 if (linelen != -1) {
3770 char *exstr;
3771 err = expand_tab(&exstr, line);
3772 if (err)
3773 break;
3774 if (match_line(exstr, &view->regex, 1,
3775 &view->regmatch)) {
3776 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3777 s->matched_line = lineno;
3778 free(exstr);
3779 break;
3781 free(exstr);
3783 if (view->searching == TOG_SEARCH_FORWARD)
3784 lineno++;
3785 else
3786 lineno--;
3788 free(line);
3790 if (s->matched_line) {
3791 s->first_displayed_line = s->matched_line;
3792 s->selected_line = 1;
3795 return err;
3798 static const struct got_error *
3799 close_diff_view(struct tog_view *view)
3801 const struct got_error *err = NULL;
3802 struct tog_diff_view_state *s = &view->state.diff;
3804 free(s->id1);
3805 s->id1 = NULL;
3806 free(s->id2);
3807 s->id2 = NULL;
3808 if (s->f && fclose(s->f) == EOF)
3809 err = got_error_from_errno("fclose");
3810 s->f = NULL;
3811 if (s->f1 && fclose(s->f1) == EOF)
3812 err = got_error_from_errno("fclose");
3813 s->f1 = NULL;
3814 if (s->f2 && fclose(s->f2) == EOF)
3815 err = got_error_from_errno("fclose");
3816 s->f2 = NULL;
3817 free_colors(&s->colors);
3818 free(s->line_offsets);
3819 s->line_offsets = NULL;
3820 s->nlines = 0;
3821 return err;
3824 static const struct got_error *
3825 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3826 struct got_object_id *id2, const char *label1, const char *label2,
3827 int diff_context, int ignore_whitespace, int force_text_diff,
3828 struct tog_view *log_view, struct got_repository *repo)
3830 const struct got_error *err;
3831 struct tog_diff_view_state *s = &view->state.diff;
3833 memset(s, 0, sizeof(*s));
3835 if (id1 != NULL && id2 != NULL) {
3836 int type1, type2;
3837 err = got_object_get_type(&type1, repo, id1);
3838 if (err)
3839 return err;
3840 err = got_object_get_type(&type2, repo, id2);
3841 if (err)
3842 return err;
3844 if (type1 != type2)
3845 return got_error(GOT_ERR_OBJ_TYPE);
3847 s->first_displayed_line = 1;
3848 s->last_displayed_line = view->nlines;
3849 s->selected_line = 1;
3850 s->repo = repo;
3851 s->id1 = id1;
3852 s->id2 = id2;
3853 s->label1 = label1;
3854 s->label2 = label2;
3856 if (id1) {
3857 s->id1 = got_object_id_dup(id1);
3858 if (s->id1 == NULL)
3859 return got_error_from_errno("got_object_id_dup");
3860 } else
3861 s->id1 = NULL;
3863 s->id2 = got_object_id_dup(id2);
3864 if (s->id2 == NULL) {
3865 err = got_error_from_errno("got_object_id_dup");
3866 goto done;
3869 s->f1 = got_opentemp();
3870 if (s->f1 == NULL) {
3871 err = got_error_from_errno("got_opentemp");
3872 goto done;
3875 s->f2 = got_opentemp();
3876 if (s->f2 == NULL) {
3877 err = got_error_from_errno("got_opentemp");
3878 goto done;
3881 s->first_displayed_line = 1;
3882 s->last_displayed_line = view->nlines;
3883 s->diff_context = diff_context;
3884 s->ignore_whitespace = ignore_whitespace;
3885 s->force_text_diff = force_text_diff;
3886 s->log_view = log_view;
3887 s->repo = repo;
3889 STAILQ_INIT(&s->colors);
3890 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3891 err = add_color(&s->colors,
3892 "^-", TOG_COLOR_DIFF_MINUS,
3893 get_color_value("TOG_COLOR_DIFF_MINUS"));
3894 if (err)
3895 goto done;
3896 err = add_color(&s->colors, "^\\+",
3897 TOG_COLOR_DIFF_PLUS,
3898 get_color_value("TOG_COLOR_DIFF_PLUS"));
3899 if (err)
3900 goto done;
3901 err = add_color(&s->colors,
3902 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3903 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3904 if (err)
3905 goto done;
3907 err = add_color(&s->colors,
3908 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3909 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3910 get_color_value("TOG_COLOR_DIFF_META"));
3911 if (err)
3912 goto done;
3914 err = add_color(&s->colors,
3915 "^(from|via): ", TOG_COLOR_AUTHOR,
3916 get_color_value("TOG_COLOR_AUTHOR"));
3917 if (err)
3918 goto done;
3920 err = add_color(&s->colors,
3921 "^date: ", TOG_COLOR_DATE,
3922 get_color_value("TOG_COLOR_DATE"));
3923 if (err)
3924 goto done;
3927 if (log_view && view_is_splitscreen(view))
3928 show_log_view(log_view); /* draw vborder */
3929 diff_view_indicate_progress(view);
3931 err = create_diff(s);
3933 view->show = show_diff_view;
3934 view->input = input_diff_view;
3935 view->close = close_diff_view;
3936 view->search_start = search_start_diff_view;
3937 view->search_next = search_next_diff_view;
3938 done:
3939 if (err)
3940 close_diff_view(view);
3941 return err;
3944 static const struct got_error *
3945 show_diff_view(struct tog_view *view)
3947 const struct got_error *err;
3948 struct tog_diff_view_state *s = &view->state.diff;
3949 char *id_str1 = NULL, *id_str2, *header;
3950 const char *label1, *label2;
3952 if (s->id1) {
3953 err = got_object_id_str(&id_str1, s->id1);
3954 if (err)
3955 return err;
3956 label1 = s->label1 ? : id_str1;
3957 } else
3958 label1 = "/dev/null";
3960 err = got_object_id_str(&id_str2, s->id2);
3961 if (err)
3962 return err;
3963 label2 = s->label2 ? : id_str2;
3965 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3966 err = got_error_from_errno("asprintf");
3967 free(id_str1);
3968 free(id_str2);
3969 return err;
3971 free(id_str1);
3972 free(id_str2);
3974 err = draw_file(view, header);
3975 free(header);
3976 return err;
3979 static const struct got_error *
3980 set_selected_commit(struct tog_diff_view_state *s,
3981 struct commit_queue_entry *entry)
3983 const struct got_error *err;
3984 const struct got_object_id_queue *parent_ids;
3985 struct got_commit_object *selected_commit;
3986 struct got_object_qid *pid;
3988 free(s->id2);
3989 s->id2 = got_object_id_dup(entry->id);
3990 if (s->id2 == NULL)
3991 return got_error_from_errno("got_object_id_dup");
3993 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3994 if (err)
3995 return err;
3996 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3997 free(s->id1);
3998 pid = STAILQ_FIRST(parent_ids);
3999 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4000 got_object_commit_close(selected_commit);
4001 return NULL;
4004 static const struct got_error *
4005 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4007 const struct got_error *err = NULL;
4008 struct tog_diff_view_state *s = &view->state.diff;
4009 struct tog_log_view_state *ls;
4010 struct commit_queue_entry *old_selected_entry;
4011 char *line = NULL;
4012 size_t linesize = 0;
4013 ssize_t linelen;
4014 int i, nscroll = view->nlines - 1;
4016 switch (ch) {
4017 case '0':
4018 view->x = 0;
4019 break;
4020 case '$':
4021 view->x = MAX(view->maxx - view->ncols / 3, 0);
4022 break;
4023 case KEY_RIGHT:
4024 case 'l':
4025 if (view->x + view->ncols / 3 < view->maxx)
4026 view->x += 2; /* move two columns right */
4027 break;
4028 case KEY_LEFT:
4029 case 'h':
4030 view->x -= MIN(view->x, 2); /* move two columns back */
4031 break;
4032 case 'a':
4033 case 'w':
4034 if (ch == 'a')
4035 s->force_text_diff = !s->force_text_diff;
4036 if (ch == 'w')
4037 s->ignore_whitespace = !s->ignore_whitespace;
4038 wclear(view->window);
4039 s->first_displayed_line = 1;
4040 s->last_displayed_line = view->nlines;
4041 s->matched_line = 0;
4042 diff_view_indicate_progress(view);
4043 err = create_diff(s);
4044 break;
4045 case 'g':
4046 case KEY_HOME:
4047 s->first_displayed_line = 1;
4048 break;
4049 case 'G':
4050 case KEY_END:
4051 if (s->eof)
4052 break;
4054 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4055 s->eof = 1;
4056 break;
4057 case 'k':
4058 case KEY_UP:
4059 case CTRL('p'):
4060 if (s->first_displayed_line > 1)
4061 s->first_displayed_line--;
4062 break;
4063 case CTRL('u'):
4064 case 'u':
4065 nscroll /= 2;
4066 /* FALL THROUGH */
4067 case KEY_PPAGE:
4068 case CTRL('b'):
4069 if (s->first_displayed_line == 1)
4070 break;
4071 i = 0;
4072 while (i++ < nscroll && s->first_displayed_line > 1)
4073 s->first_displayed_line--;
4074 break;
4075 case 'j':
4076 case KEY_DOWN:
4077 case CTRL('n'):
4078 if (!s->eof)
4079 s->first_displayed_line++;
4080 break;
4081 case CTRL('d'):
4082 case 'd':
4083 nscroll /= 2;
4084 /* FALL THROUGH */
4085 case KEY_NPAGE:
4086 case CTRL('f'):
4087 case ' ':
4088 if (s->eof)
4089 break;
4090 i = 0;
4091 while (!s->eof && i++ < nscroll) {
4092 linelen = getline(&line, &linesize, s->f);
4093 s->first_displayed_line++;
4094 if (linelen == -1) {
4095 if (feof(s->f)) {
4096 s->eof = 1;
4097 } else
4098 err = got_ferror(s->f, GOT_ERR_IO);
4099 break;
4102 free(line);
4103 break;
4104 case '[':
4105 if (s->diff_context > 0) {
4106 s->diff_context--;
4107 s->matched_line = 0;
4108 diff_view_indicate_progress(view);
4109 err = create_diff(s);
4110 if (s->first_displayed_line + view->nlines - 1 >
4111 s->nlines) {
4112 s->first_displayed_line = 1;
4113 s->last_displayed_line = view->nlines;
4116 break;
4117 case ']':
4118 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4119 s->diff_context++;
4120 s->matched_line = 0;
4121 diff_view_indicate_progress(view);
4122 err = create_diff(s);
4124 break;
4125 case '<':
4126 case ',':
4127 if (s->log_view == NULL)
4128 break;
4129 ls = &s->log_view->state.log;
4130 old_selected_entry = ls->selected_entry;
4132 err = input_log_view(NULL, s->log_view, KEY_UP);
4133 if (err)
4134 break;
4136 if (old_selected_entry == ls->selected_entry)
4137 break;
4139 err = set_selected_commit(s, ls->selected_entry);
4140 if (err)
4141 break;
4143 s->first_displayed_line = 1;
4144 s->last_displayed_line = view->nlines;
4145 s->matched_line = 0;
4146 view->x = 0;
4148 diff_view_indicate_progress(view);
4149 err = create_diff(s);
4150 break;
4151 case '>':
4152 case '.':
4153 if (s->log_view == NULL)
4154 break;
4155 ls = &s->log_view->state.log;
4156 old_selected_entry = ls->selected_entry;
4158 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4159 if (err)
4160 break;
4162 if (old_selected_entry == ls->selected_entry)
4163 break;
4165 err = set_selected_commit(s, ls->selected_entry);
4166 if (err)
4167 break;
4169 s->first_displayed_line = 1;
4170 s->last_displayed_line = view->nlines;
4171 s->matched_line = 0;
4172 view->x = 0;
4174 diff_view_indicate_progress(view);
4175 err = create_diff(s);
4176 break;
4177 default:
4178 break;
4181 return err;
4184 static const struct got_error *
4185 cmd_diff(int argc, char *argv[])
4187 const struct got_error *error = NULL;
4188 struct got_repository *repo = NULL;
4189 struct got_worktree *worktree = NULL;
4190 struct got_object_id *id1 = NULL, *id2 = NULL;
4191 char *repo_path = NULL, *cwd = NULL;
4192 char *id_str1 = NULL, *id_str2 = NULL;
4193 char *label1 = NULL, *label2 = NULL;
4194 int diff_context = 3, ignore_whitespace = 0;
4195 int ch, force_text_diff = 0;
4196 const char *errstr;
4197 struct tog_view *view;
4198 int *pack_fds = NULL;
4200 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4201 switch (ch) {
4202 case 'a':
4203 force_text_diff = 1;
4204 break;
4205 case 'C':
4206 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4207 &errstr);
4208 if (errstr != NULL)
4209 errx(1, "number of context lines is %s: %s",
4210 errstr, errstr);
4211 break;
4212 case 'r':
4213 repo_path = realpath(optarg, NULL);
4214 if (repo_path == NULL)
4215 return got_error_from_errno2("realpath",
4216 optarg);
4217 got_path_strip_trailing_slashes(repo_path);
4218 break;
4219 case 'w':
4220 ignore_whitespace = 1;
4221 break;
4222 default:
4223 usage_diff();
4224 /* NOTREACHED */
4228 argc -= optind;
4229 argv += optind;
4231 if (argc == 0) {
4232 usage_diff(); /* TODO show local worktree changes */
4233 } else if (argc == 2) {
4234 id_str1 = argv[0];
4235 id_str2 = argv[1];
4236 } else
4237 usage_diff();
4239 error = got_repo_pack_fds_open(&pack_fds);
4240 if (error)
4241 goto done;
4243 if (repo_path == NULL) {
4244 cwd = getcwd(NULL, 0);
4245 if (cwd == NULL)
4246 return got_error_from_errno("getcwd");
4247 error = got_worktree_open(&worktree, cwd);
4248 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4249 goto done;
4250 if (worktree)
4251 repo_path =
4252 strdup(got_worktree_get_repo_path(worktree));
4253 else
4254 repo_path = strdup(cwd);
4255 if (repo_path == NULL) {
4256 error = got_error_from_errno("strdup");
4257 goto done;
4261 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4262 if (error)
4263 goto done;
4265 init_curses();
4267 error = apply_unveil(got_repo_get_path(repo), NULL);
4268 if (error)
4269 goto done;
4271 error = tog_load_refs(repo, 0);
4272 if (error)
4273 goto done;
4275 error = got_repo_match_object_id(&id1, &label1, id_str1,
4276 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4277 if (error)
4278 goto done;
4280 error = got_repo_match_object_id(&id2, &label2, id_str2,
4281 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4282 if (error)
4283 goto done;
4285 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4286 if (view == NULL) {
4287 error = got_error_from_errno("view_open");
4288 goto done;
4290 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4291 ignore_whitespace, force_text_diff, NULL, repo);
4292 if (error)
4293 goto done;
4294 error = view_loop(view);
4295 done:
4296 free(label1);
4297 free(label2);
4298 free(repo_path);
4299 free(cwd);
4300 if (repo) {
4301 const struct got_error *close_err = got_repo_close(repo);
4302 if (error == NULL)
4303 error = close_err;
4305 if (worktree)
4306 got_worktree_close(worktree);
4307 if (pack_fds) {
4308 const struct got_error *pack_err =
4309 got_repo_pack_fds_close(pack_fds);
4310 if (error == NULL)
4311 error = pack_err;
4313 tog_free_refs();
4314 return error;
4317 __dead static void
4318 usage_blame(void)
4320 endwin();
4321 fprintf(stderr,
4322 "usage: %s blame [-c commit] [-r repository-path] path\n",
4323 getprogname());
4324 exit(1);
4327 struct tog_blame_line {
4328 int annotated;
4329 struct got_object_id *id;
4332 static const struct got_error *
4333 draw_blame(struct tog_view *view)
4335 struct tog_blame_view_state *s = &view->state.blame;
4336 struct tog_blame *blame = &s->blame;
4337 regmatch_t *regmatch = &view->regmatch;
4338 const struct got_error *err;
4339 int lineno = 0, nprinted = 0;
4340 char *line = NULL;
4341 size_t linesize = 0;
4342 ssize_t linelen;
4343 wchar_t *wline;
4344 int width;
4345 struct tog_blame_line *blame_line;
4346 struct got_object_id *prev_id = NULL;
4347 char *id_str;
4348 struct tog_color *tc;
4350 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4351 if (err)
4352 return err;
4354 rewind(blame->f);
4355 werase(view->window);
4357 if (asprintf(&line, "commit %s", id_str) == -1) {
4358 err = got_error_from_errno("asprintf");
4359 free(id_str);
4360 return err;
4363 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4364 free(line);
4365 line = NULL;
4366 if (err)
4367 return err;
4368 if (view_needs_focus_indication(view))
4369 wstandout(view->window);
4370 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4371 if (tc)
4372 wattr_on(view->window,
4373 COLOR_PAIR(tc->colorpair), NULL);
4374 waddwstr(view->window, wline);
4375 if (tc)
4376 wattr_off(view->window,
4377 COLOR_PAIR(tc->colorpair), NULL);
4378 if (view_needs_focus_indication(view))
4379 wstandend(view->window);
4380 free(wline);
4381 wline = NULL;
4382 if (width < view->ncols - 1)
4383 waddch(view->window, '\n');
4385 if (asprintf(&line, "[%d/%d] %s%s",
4386 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4387 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4388 free(id_str);
4389 return got_error_from_errno("asprintf");
4391 free(id_str);
4392 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4393 free(line);
4394 line = NULL;
4395 if (err)
4396 return err;
4397 waddwstr(view->window, wline);
4398 free(wline);
4399 wline = NULL;
4400 if (width < view->ncols - 1)
4401 waddch(view->window, '\n');
4403 s->eof = 0;
4404 view->maxx = 0;
4405 while (nprinted < view->nlines - 2) {
4406 linelen = getline(&line, &linesize, blame->f);
4407 if (linelen == -1) {
4408 if (feof(blame->f)) {
4409 s->eof = 1;
4410 break;
4412 free(line);
4413 return got_ferror(blame->f, GOT_ERR_IO);
4415 if (++lineno < s->first_displayed_line)
4416 continue;
4418 /* Set view->maxx based on full line length. */
4419 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4420 if (err) {
4421 free(line);
4422 return err;
4424 free(wline);
4425 wline = NULL;
4426 view->maxx = MAX(view->maxx, width);
4428 if (view->focussed && nprinted == s->selected_line - 1)
4429 wstandout(view->window);
4431 if (blame->nlines > 0) {
4432 blame_line = &blame->lines[lineno - 1];
4433 if (blame_line->annotated && prev_id &&
4434 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4435 !(view->focussed &&
4436 nprinted == s->selected_line - 1)) {
4437 waddstr(view->window, " ");
4438 } else if (blame_line->annotated) {
4439 char *id_str;
4440 err = got_object_id_str(&id_str,
4441 blame_line->id);
4442 if (err) {
4443 free(line);
4444 return err;
4446 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4447 if (tc)
4448 wattr_on(view->window,
4449 COLOR_PAIR(tc->colorpair), NULL);
4450 wprintw(view->window, "%.8s", id_str);
4451 if (tc)
4452 wattr_off(view->window,
4453 COLOR_PAIR(tc->colorpair), NULL);
4454 free(id_str);
4455 prev_id = blame_line->id;
4456 } else {
4457 waddstr(view->window, "........");
4458 prev_id = NULL;
4460 } else {
4461 waddstr(view->window, "........");
4462 prev_id = NULL;
4465 if (view->focussed && nprinted == s->selected_line - 1)
4466 wstandend(view->window);
4467 waddstr(view->window, " ");
4469 if (view->ncols <= 9) {
4470 width = 9;
4471 } else if (s->first_displayed_line + nprinted ==
4472 s->matched_line &&
4473 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4474 err = add_matched_line(&width, line, view->ncols - 9, 9,
4475 view->window, view->x, regmatch);
4476 if (err) {
4477 free(line);
4478 return err;
4480 width += 9;
4481 } else {
4482 int skip;
4483 err = format_line(&wline, &width, &skip, line,
4484 view->x, view->ncols - 9, 9, 1);
4485 if (err) {
4486 free(line);
4487 return err;
4489 waddwstr(view->window, &wline[skip]);
4490 width += 9;
4491 free(wline);
4492 wline = NULL;
4495 if (width <= view->ncols - 1)
4496 waddch(view->window, '\n');
4497 if (++nprinted == 1)
4498 s->first_displayed_line = lineno;
4500 free(line);
4501 s->last_displayed_line = lineno;
4503 view_vborder(view);
4505 return NULL;
4508 static const struct got_error *
4509 blame_cb(void *arg, int nlines, int lineno,
4510 struct got_commit_object *commit, struct got_object_id *id)
4512 const struct got_error *err = NULL;
4513 struct tog_blame_cb_args *a = arg;
4514 struct tog_blame_line *line;
4515 int errcode;
4517 if (nlines != a->nlines ||
4518 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4519 return got_error(GOT_ERR_RANGE);
4521 errcode = pthread_mutex_lock(&tog_mutex);
4522 if (errcode)
4523 return got_error_set_errno(errcode, "pthread_mutex_lock");
4525 if (*a->quit) { /* user has quit the blame view */
4526 err = got_error(GOT_ERR_ITER_COMPLETED);
4527 goto done;
4530 if (lineno == -1)
4531 goto done; /* no change in this commit */
4533 line = &a->lines[lineno - 1];
4534 if (line->annotated)
4535 goto done;
4537 line->id = got_object_id_dup(id);
4538 if (line->id == NULL) {
4539 err = got_error_from_errno("got_object_id_dup");
4540 goto done;
4542 line->annotated = 1;
4543 done:
4544 errcode = pthread_mutex_unlock(&tog_mutex);
4545 if (errcode)
4546 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4547 return err;
4550 static void *
4551 blame_thread(void *arg)
4553 const struct got_error *err, *close_err;
4554 struct tog_blame_thread_args *ta = arg;
4555 struct tog_blame_cb_args *a = ta->cb_args;
4556 int errcode;
4558 err = block_signals_used_by_main_thread();
4559 if (err)
4560 return (void *)err;
4562 err = got_blame(ta->path, a->commit_id, ta->repo,
4563 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4564 if (err && err->code == GOT_ERR_CANCELLED)
4565 err = NULL;
4567 errcode = pthread_mutex_lock(&tog_mutex);
4568 if (errcode)
4569 return (void *)got_error_set_errno(errcode,
4570 "pthread_mutex_lock");
4572 close_err = got_repo_close(ta->repo);
4573 if (err == NULL)
4574 err = close_err;
4575 ta->repo = NULL;
4576 *ta->complete = 1;
4578 errcode = pthread_mutex_unlock(&tog_mutex);
4579 if (errcode && err == NULL)
4580 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4582 return (void *)err;
4585 static struct got_object_id *
4586 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4587 int first_displayed_line, int selected_line)
4589 struct tog_blame_line *line;
4591 if (nlines <= 0)
4592 return NULL;
4594 line = &lines[first_displayed_line - 1 + selected_line - 1];
4595 if (!line->annotated)
4596 return NULL;
4598 return line->id;
4601 static const struct got_error *
4602 stop_blame(struct tog_blame *blame)
4604 const struct got_error *err = NULL;
4605 int i;
4607 if (blame->thread) {
4608 int errcode;
4609 errcode = pthread_mutex_unlock(&tog_mutex);
4610 if (errcode)
4611 return got_error_set_errno(errcode,
4612 "pthread_mutex_unlock");
4613 errcode = pthread_join(blame->thread, (void **)&err);
4614 if (errcode)
4615 return got_error_set_errno(errcode, "pthread_join");
4616 errcode = pthread_mutex_lock(&tog_mutex);
4617 if (errcode)
4618 return got_error_set_errno(errcode,
4619 "pthread_mutex_lock");
4620 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4621 err = NULL;
4622 blame->thread = 0; //NULL;
4624 if (blame->thread_args.repo) {
4625 const struct got_error *close_err;
4626 close_err = got_repo_close(blame->thread_args.repo);
4627 if (err == NULL)
4628 err = close_err;
4629 blame->thread_args.repo = NULL;
4631 if (blame->f) {
4632 if (fclose(blame->f) == EOF && err == NULL)
4633 err = got_error_from_errno("fclose");
4634 blame->f = NULL;
4636 if (blame->lines) {
4637 for (i = 0; i < blame->nlines; i++)
4638 free(blame->lines[i].id);
4639 free(blame->lines);
4640 blame->lines = NULL;
4642 free(blame->cb_args.commit_id);
4643 blame->cb_args.commit_id = NULL;
4644 if (blame->pack_fds) {
4645 const struct got_error *pack_err =
4646 got_repo_pack_fds_close(blame->pack_fds);
4647 if (err == NULL)
4648 err = pack_err;
4649 blame->pack_fds = NULL;
4651 return err;
4654 static const struct got_error *
4655 cancel_blame_view(void *arg)
4657 const struct got_error *err = NULL;
4658 int *done = arg;
4659 int errcode;
4661 errcode = pthread_mutex_lock(&tog_mutex);
4662 if (errcode)
4663 return got_error_set_errno(errcode,
4664 "pthread_mutex_unlock");
4666 if (*done)
4667 err = got_error(GOT_ERR_CANCELLED);
4669 errcode = pthread_mutex_unlock(&tog_mutex);
4670 if (errcode)
4671 return got_error_set_errno(errcode,
4672 "pthread_mutex_lock");
4674 return err;
4677 static const struct got_error *
4678 run_blame(struct tog_view *view)
4680 struct tog_blame_view_state *s = &view->state.blame;
4681 struct tog_blame *blame = &s->blame;
4682 const struct got_error *err = NULL;
4683 struct got_commit_object *commit = NULL;
4684 struct got_blob_object *blob = NULL;
4685 struct got_repository *thread_repo = NULL;
4686 struct got_object_id *obj_id = NULL;
4687 int obj_type;
4688 int *pack_fds = NULL;
4690 err = got_object_open_as_commit(&commit, s->repo,
4691 &s->blamed_commit->id);
4692 if (err)
4693 return err;
4695 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4696 if (err)
4697 goto done;
4699 err = got_object_get_type(&obj_type, s->repo, obj_id);
4700 if (err)
4701 goto done;
4703 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4704 err = got_error(GOT_ERR_OBJ_TYPE);
4705 goto done;
4708 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4709 if (err)
4710 goto done;
4711 blame->f = got_opentemp();
4712 if (blame->f == NULL) {
4713 err = got_error_from_errno("got_opentemp");
4714 goto done;
4716 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4717 &blame->line_offsets, blame->f, blob);
4718 if (err)
4719 goto done;
4720 if (blame->nlines == 0) {
4721 s->blame_complete = 1;
4722 goto done;
4725 /* Don't include \n at EOF in the blame line count. */
4726 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4727 blame->nlines--;
4729 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4730 if (blame->lines == NULL) {
4731 err = got_error_from_errno("calloc");
4732 goto done;
4735 err = got_repo_pack_fds_open(&pack_fds);
4736 if (err)
4737 goto done;
4738 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4739 pack_fds);
4740 if (err)
4741 goto done;
4743 blame->pack_fds = pack_fds;
4744 blame->cb_args.view = view;
4745 blame->cb_args.lines = blame->lines;
4746 blame->cb_args.nlines = blame->nlines;
4747 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4748 if (blame->cb_args.commit_id == NULL) {
4749 err = got_error_from_errno("got_object_id_dup");
4750 goto done;
4752 blame->cb_args.quit = &s->done;
4754 blame->thread_args.path = s->path;
4755 blame->thread_args.repo = thread_repo;
4756 blame->thread_args.cb_args = &blame->cb_args;
4757 blame->thread_args.complete = &s->blame_complete;
4758 blame->thread_args.cancel_cb = cancel_blame_view;
4759 blame->thread_args.cancel_arg = &s->done;
4760 s->blame_complete = 0;
4762 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4763 s->first_displayed_line = 1;
4764 s->last_displayed_line = view->nlines;
4765 s->selected_line = 1;
4767 s->matched_line = 0;
4769 done:
4770 if (commit)
4771 got_object_commit_close(commit);
4772 if (blob)
4773 got_object_blob_close(blob);
4774 free(obj_id);
4775 if (err)
4776 stop_blame(blame);
4777 return err;
4780 static const struct got_error *
4781 open_blame_view(struct tog_view *view, char *path,
4782 struct got_object_id *commit_id, struct got_repository *repo)
4784 const struct got_error *err = NULL;
4785 struct tog_blame_view_state *s = &view->state.blame;
4787 STAILQ_INIT(&s->blamed_commits);
4789 s->path = strdup(path);
4790 if (s->path == NULL)
4791 return got_error_from_errno("strdup");
4793 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4794 if (err) {
4795 free(s->path);
4796 return err;
4799 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4800 s->first_displayed_line = 1;
4801 s->last_displayed_line = view->nlines;
4802 s->selected_line = 1;
4803 s->blame_complete = 0;
4804 s->repo = repo;
4805 s->commit_id = commit_id;
4806 memset(&s->blame, 0, sizeof(s->blame));
4808 STAILQ_INIT(&s->colors);
4809 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4810 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4811 get_color_value("TOG_COLOR_COMMIT"));
4812 if (err)
4813 return err;
4816 view->show = show_blame_view;
4817 view->input = input_blame_view;
4818 view->close = close_blame_view;
4819 view->search_start = search_start_blame_view;
4820 view->search_next = search_next_blame_view;
4822 return run_blame(view);
4825 static const struct got_error *
4826 close_blame_view(struct tog_view *view)
4828 const struct got_error *err = NULL;
4829 struct tog_blame_view_state *s = &view->state.blame;
4831 if (s->blame.thread)
4832 err = stop_blame(&s->blame);
4834 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4835 struct got_object_qid *blamed_commit;
4836 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4837 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4838 got_object_qid_free(blamed_commit);
4841 free(s->path);
4842 free_colors(&s->colors);
4843 return err;
4846 static const struct got_error *
4847 search_start_blame_view(struct tog_view *view)
4849 struct tog_blame_view_state *s = &view->state.blame;
4851 s->matched_line = 0;
4852 return NULL;
4855 static const struct got_error *
4856 search_next_blame_view(struct tog_view *view)
4858 struct tog_blame_view_state *s = &view->state.blame;
4859 const struct got_error *err = NULL;
4860 int lineno;
4861 char *line = NULL;
4862 size_t linesize = 0;
4863 ssize_t linelen;
4865 if (!view->searching) {
4866 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4867 return NULL;
4870 if (s->matched_line) {
4871 if (view->searching == TOG_SEARCH_FORWARD)
4872 lineno = s->matched_line + 1;
4873 else
4874 lineno = s->matched_line - 1;
4875 } else
4876 lineno = s->first_displayed_line - 1 + s->selected_line;
4878 while (1) {
4879 off_t offset;
4881 if (lineno <= 0 || lineno > s->blame.nlines) {
4882 if (s->matched_line == 0) {
4883 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4884 break;
4887 if (view->searching == TOG_SEARCH_FORWARD)
4888 lineno = 1;
4889 else
4890 lineno = s->blame.nlines;
4893 offset = s->blame.line_offsets[lineno - 1];
4894 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4895 free(line);
4896 return got_error_from_errno("fseeko");
4898 linelen = getline(&line, &linesize, s->blame.f);
4899 if (linelen != -1) {
4900 char *exstr;
4901 err = expand_tab(&exstr, line);
4902 if (err)
4903 break;
4904 if (match_line(exstr, &view->regex, 1,
4905 &view->regmatch)) {
4906 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4907 s->matched_line = lineno;
4908 free(exstr);
4909 break;
4911 free(exstr);
4913 if (view->searching == TOG_SEARCH_FORWARD)
4914 lineno++;
4915 else
4916 lineno--;
4918 free(line);
4920 if (s->matched_line) {
4921 s->first_displayed_line = s->matched_line;
4922 s->selected_line = 1;
4925 return err;
4928 static const struct got_error *
4929 show_blame_view(struct tog_view *view)
4931 const struct got_error *err = NULL;
4932 struct tog_blame_view_state *s = &view->state.blame;
4933 int errcode;
4935 if (s->blame.thread == 0 && !s->blame_complete) {
4936 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4937 &s->blame.thread_args);
4938 if (errcode)
4939 return got_error_set_errno(errcode, "pthread_create");
4941 halfdelay(1); /* fast refresh while annotating */
4944 if (s->blame_complete)
4945 halfdelay(10); /* disable fast refresh */
4947 err = draw_blame(view);
4949 view_vborder(view);
4950 return err;
4953 static const struct got_error *
4954 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4956 const struct got_error *err = NULL, *thread_err = NULL;
4957 struct tog_view *diff_view;
4958 struct tog_blame_view_state *s = &view->state.blame;
4959 int begin_x = 0, nscroll = view->nlines - 2;
4961 switch (ch) {
4962 case '0':
4963 view->x = 0;
4964 break;
4965 case '$':
4966 view->x = MAX(view->maxx - view->ncols / 3, 0);
4967 break;
4968 case KEY_RIGHT:
4969 case 'l':
4970 if (view->x + view->ncols / 3 < view->maxx)
4971 view->x += 2; /* move two columns right */
4972 break;
4973 case KEY_LEFT:
4974 case 'h':
4975 view->x -= MIN(view->x, 2); /* move two columns back */
4976 break;
4977 case 'q':
4978 s->done = 1;
4979 break;
4980 case 'g':
4981 case KEY_HOME:
4982 s->selected_line = 1;
4983 s->first_displayed_line = 1;
4984 break;
4985 case 'G':
4986 case KEY_END:
4987 if (s->blame.nlines < view->nlines - 2) {
4988 s->selected_line = s->blame.nlines;
4989 s->first_displayed_line = 1;
4990 } else {
4991 s->selected_line = view->nlines - 2;
4992 s->first_displayed_line = s->blame.nlines -
4993 (view->nlines - 3);
4995 break;
4996 case 'k':
4997 case KEY_UP:
4998 case CTRL('p'):
4999 if (s->selected_line > 1)
5000 s->selected_line--;
5001 else if (s->selected_line == 1 &&
5002 s->first_displayed_line > 1)
5003 s->first_displayed_line--;
5004 break;
5005 case CTRL('u'):
5006 case 'u':
5007 nscroll /= 2;
5008 /* FALL THROUGH */
5009 case KEY_PPAGE:
5010 case CTRL('b'):
5011 if (s->first_displayed_line == 1) {
5012 s->selected_line = MAX(1, s->selected_line - nscroll);
5013 break;
5015 if (s->first_displayed_line > nscroll)
5016 s->first_displayed_line -= nscroll;
5017 else
5018 s->first_displayed_line = 1;
5019 break;
5020 case 'j':
5021 case KEY_DOWN:
5022 case CTRL('n'):
5023 if (s->selected_line < view->nlines - 2 &&
5024 s->first_displayed_line +
5025 s->selected_line <= s->blame.nlines)
5026 s->selected_line++;
5027 else if (s->last_displayed_line <
5028 s->blame.nlines)
5029 s->first_displayed_line++;
5030 break;
5031 case 'b':
5032 case 'p': {
5033 struct got_object_id *id = NULL;
5034 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5035 s->first_displayed_line, s->selected_line);
5036 if (id == NULL)
5037 break;
5038 if (ch == 'p') {
5039 struct got_commit_object *commit, *pcommit;
5040 struct got_object_qid *pid;
5041 struct got_object_id *blob_id = NULL;
5042 int obj_type;
5043 err = got_object_open_as_commit(&commit,
5044 s->repo, id);
5045 if (err)
5046 break;
5047 pid = STAILQ_FIRST(
5048 got_object_commit_get_parent_ids(commit));
5049 if (pid == NULL) {
5050 got_object_commit_close(commit);
5051 break;
5053 /* Check if path history ends here. */
5054 err = got_object_open_as_commit(&pcommit,
5055 s->repo, &pid->id);
5056 if (err)
5057 break;
5058 err = got_object_id_by_path(&blob_id, s->repo,
5059 pcommit, s->path);
5060 got_object_commit_close(pcommit);
5061 if (err) {
5062 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5063 err = NULL;
5064 got_object_commit_close(commit);
5065 break;
5067 err = got_object_get_type(&obj_type, s->repo,
5068 blob_id);
5069 free(blob_id);
5070 /* Can't blame non-blob type objects. */
5071 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5072 got_object_commit_close(commit);
5073 break;
5075 err = got_object_qid_alloc(&s->blamed_commit,
5076 &pid->id);
5077 got_object_commit_close(commit);
5078 } else {
5079 if (got_object_id_cmp(id,
5080 &s->blamed_commit->id) == 0)
5081 break;
5082 err = got_object_qid_alloc(&s->blamed_commit,
5083 id);
5085 if (err)
5086 break;
5087 s->done = 1;
5088 thread_err = stop_blame(&s->blame);
5089 s->done = 0;
5090 if (thread_err)
5091 break;
5092 STAILQ_INSERT_HEAD(&s->blamed_commits,
5093 s->blamed_commit, entry);
5094 err = run_blame(view);
5095 if (err)
5096 break;
5097 break;
5099 case 'B': {
5100 struct got_object_qid *first;
5101 first = STAILQ_FIRST(&s->blamed_commits);
5102 if (!got_object_id_cmp(&first->id, s->commit_id))
5103 break;
5104 s->done = 1;
5105 thread_err = stop_blame(&s->blame);
5106 s->done = 0;
5107 if (thread_err)
5108 break;
5109 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5110 got_object_qid_free(s->blamed_commit);
5111 s->blamed_commit =
5112 STAILQ_FIRST(&s->blamed_commits);
5113 err = run_blame(view);
5114 if (err)
5115 break;
5116 break;
5118 case KEY_ENTER:
5119 case '\r': {
5120 struct got_object_id *id = NULL;
5121 struct got_object_qid *pid;
5122 struct got_commit_object *commit = NULL;
5123 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5124 s->first_displayed_line, s->selected_line);
5125 if (id == NULL)
5126 break;
5127 err = got_object_open_as_commit(&commit, s->repo, id);
5128 if (err)
5129 break;
5130 pid = STAILQ_FIRST(
5131 got_object_commit_get_parent_ids(commit));
5132 if (view_is_parent_view(view))
5133 begin_x = view_split_begin_x(view->begin_x);
5134 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5135 if (diff_view == NULL) {
5136 got_object_commit_close(commit);
5137 err = got_error_from_errno("view_open");
5138 break;
5140 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5141 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5142 got_object_commit_close(commit);
5143 if (err) {
5144 view_close(diff_view);
5145 break;
5147 view->focussed = 0;
5148 diff_view->focussed = 1;
5149 if (view_is_parent_view(view)) {
5150 err = view_close_child(view);
5151 if (err)
5152 break;
5153 err = view_set_child(view, diff_view);
5154 if (err)
5155 break;
5156 view->focus_child = 1;
5157 } else
5158 *new_view = diff_view;
5159 if (err)
5160 break;
5161 break;
5163 case CTRL('d'):
5164 case 'd':
5165 nscroll /= 2;
5166 /* FALL THROUGH */
5167 case KEY_NPAGE:
5168 case CTRL('f'):
5169 case ' ':
5170 if (s->last_displayed_line >= s->blame.nlines &&
5171 s->selected_line >= MIN(s->blame.nlines,
5172 view->nlines - 2)) {
5173 break;
5175 if (s->last_displayed_line >= s->blame.nlines &&
5176 s->selected_line < view->nlines - 2) {
5177 s->selected_line +=
5178 MIN(nscroll, s->last_displayed_line -
5179 s->first_displayed_line - s->selected_line + 1);
5181 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5182 s->first_displayed_line += nscroll;
5183 else
5184 s->first_displayed_line =
5185 s->blame.nlines - (view->nlines - 3);
5186 break;
5187 case KEY_RESIZE:
5188 if (s->selected_line > view->nlines - 2) {
5189 s->selected_line = MIN(s->blame.nlines,
5190 view->nlines - 2);
5192 break;
5193 default:
5194 break;
5196 return thread_err ? thread_err : err;
5199 static const struct got_error *
5200 cmd_blame(int argc, char *argv[])
5202 const struct got_error *error;
5203 struct got_repository *repo = NULL;
5204 struct got_worktree *worktree = NULL;
5205 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5206 char *link_target = NULL;
5207 struct got_object_id *commit_id = NULL;
5208 struct got_commit_object *commit = NULL;
5209 char *commit_id_str = NULL;
5210 int ch;
5211 struct tog_view *view;
5212 int *pack_fds = NULL;
5214 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5215 switch (ch) {
5216 case 'c':
5217 commit_id_str = optarg;
5218 break;
5219 case 'r':
5220 repo_path = realpath(optarg, NULL);
5221 if (repo_path == NULL)
5222 return got_error_from_errno2("realpath",
5223 optarg);
5224 break;
5225 default:
5226 usage_blame();
5227 /* NOTREACHED */
5231 argc -= optind;
5232 argv += optind;
5234 if (argc != 1)
5235 usage_blame();
5237 error = got_repo_pack_fds_open(&pack_fds);
5238 if (error != NULL)
5239 goto done;
5241 if (repo_path == NULL) {
5242 cwd = getcwd(NULL, 0);
5243 if (cwd == NULL)
5244 return got_error_from_errno("getcwd");
5245 error = got_worktree_open(&worktree, cwd);
5246 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5247 goto done;
5248 if (worktree)
5249 repo_path =
5250 strdup(got_worktree_get_repo_path(worktree));
5251 else
5252 repo_path = strdup(cwd);
5253 if (repo_path == NULL) {
5254 error = got_error_from_errno("strdup");
5255 goto done;
5259 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5260 if (error != NULL)
5261 goto done;
5263 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5264 worktree);
5265 if (error)
5266 goto done;
5268 init_curses();
5270 error = apply_unveil(got_repo_get_path(repo), NULL);
5271 if (error)
5272 goto done;
5274 error = tog_load_refs(repo, 0);
5275 if (error)
5276 goto done;
5278 if (commit_id_str == NULL) {
5279 struct got_reference *head_ref;
5280 error = got_ref_open(&head_ref, repo, worktree ?
5281 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5282 if (error != NULL)
5283 goto done;
5284 error = got_ref_resolve(&commit_id, repo, head_ref);
5285 got_ref_close(head_ref);
5286 } else {
5287 error = got_repo_match_object_id(&commit_id, NULL,
5288 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5290 if (error != NULL)
5291 goto done;
5293 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5294 if (view == NULL) {
5295 error = got_error_from_errno("view_open");
5296 goto done;
5299 error = got_object_open_as_commit(&commit, repo, commit_id);
5300 if (error)
5301 goto done;
5303 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5304 commit, repo);
5305 if (error)
5306 goto done;
5308 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5309 commit_id, repo);
5310 if (error)
5311 goto done;
5312 if (worktree) {
5313 /* Release work tree lock. */
5314 got_worktree_close(worktree);
5315 worktree = NULL;
5317 error = view_loop(view);
5318 done:
5319 free(repo_path);
5320 free(in_repo_path);
5321 free(link_target);
5322 free(cwd);
5323 free(commit_id);
5324 if (commit)
5325 got_object_commit_close(commit);
5326 if (worktree)
5327 got_worktree_close(worktree);
5328 if (repo) {
5329 const struct got_error *close_err = got_repo_close(repo);
5330 if (error == NULL)
5331 error = close_err;
5333 if (pack_fds) {
5334 const struct got_error *pack_err =
5335 got_repo_pack_fds_close(pack_fds);
5336 if (error == NULL)
5337 error = pack_err;
5339 tog_free_refs();
5340 return error;
5343 static const struct got_error *
5344 draw_tree_entries(struct tog_view *view, const char *parent_path)
5346 struct tog_tree_view_state *s = &view->state.tree;
5347 const struct got_error *err = NULL;
5348 struct got_tree_entry *te;
5349 wchar_t *wline;
5350 struct tog_color *tc;
5351 int width, n, i, nentries;
5352 int limit = view->nlines;
5354 s->ndisplayed = 0;
5356 werase(view->window);
5358 if (limit == 0)
5359 return NULL;
5361 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5362 0, 0);
5363 if (err)
5364 return err;
5365 if (view_needs_focus_indication(view))
5366 wstandout(view->window);
5367 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5368 if (tc)
5369 wattr_on(view->window,
5370 COLOR_PAIR(tc->colorpair), NULL);
5371 waddwstr(view->window, wline);
5372 if (tc)
5373 wattr_off(view->window,
5374 COLOR_PAIR(tc->colorpair), NULL);
5375 if (view_needs_focus_indication(view))
5376 wstandend(view->window);
5377 free(wline);
5378 wline = NULL;
5379 if (width < view->ncols - 1)
5380 waddch(view->window, '\n');
5381 if (--limit <= 0)
5382 return NULL;
5383 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5384 0, 0);
5385 if (err)
5386 return err;
5387 waddwstr(view->window, wline);
5388 free(wline);
5389 wline = NULL;
5390 if (width < view->ncols - 1)
5391 waddch(view->window, '\n');
5392 if (--limit <= 0)
5393 return NULL;
5394 waddch(view->window, '\n');
5395 if (--limit <= 0)
5396 return NULL;
5398 if (s->first_displayed_entry == NULL) {
5399 te = got_object_tree_get_first_entry(s->tree);
5400 if (s->selected == 0) {
5401 if (view->focussed)
5402 wstandout(view->window);
5403 s->selected_entry = NULL;
5405 waddstr(view->window, " ..\n"); /* parent directory */
5406 if (s->selected == 0 && view->focussed)
5407 wstandend(view->window);
5408 s->ndisplayed++;
5409 if (--limit <= 0)
5410 return NULL;
5411 n = 1;
5412 } else {
5413 n = 0;
5414 te = s->first_displayed_entry;
5417 nentries = got_object_tree_get_nentries(s->tree);
5418 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5419 char *line = NULL, *id_str = NULL, *link_target = NULL;
5420 const char *modestr = "";
5421 mode_t mode;
5423 te = got_object_tree_get_entry(s->tree, i);
5424 mode = got_tree_entry_get_mode(te);
5426 if (s->show_ids) {
5427 err = got_object_id_str(&id_str,
5428 got_tree_entry_get_id(te));
5429 if (err)
5430 return got_error_from_errno(
5431 "got_object_id_str");
5433 if (got_object_tree_entry_is_submodule(te))
5434 modestr = "$";
5435 else if (S_ISLNK(mode)) {
5436 int i;
5438 err = got_tree_entry_get_symlink_target(&link_target,
5439 te, s->repo);
5440 if (err) {
5441 free(id_str);
5442 return err;
5444 for (i = 0; i < strlen(link_target); i++) {
5445 if (!isprint((unsigned char)link_target[i]))
5446 link_target[i] = '?';
5448 modestr = "@";
5450 else if (S_ISDIR(mode))
5451 modestr = "/";
5452 else if (mode & S_IXUSR)
5453 modestr = "*";
5454 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5455 got_tree_entry_get_name(te), modestr,
5456 link_target ? " -> ": "",
5457 link_target ? link_target : "") == -1) {
5458 free(id_str);
5459 free(link_target);
5460 return got_error_from_errno("asprintf");
5462 free(id_str);
5463 free(link_target);
5464 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5465 0, 0);
5466 if (err) {
5467 free(line);
5468 break;
5470 if (n == s->selected) {
5471 if (view->focussed)
5472 wstandout(view->window);
5473 s->selected_entry = te;
5475 tc = match_color(&s->colors, line);
5476 if (tc)
5477 wattr_on(view->window,
5478 COLOR_PAIR(tc->colorpair), NULL);
5479 waddwstr(view->window, wline);
5480 if (tc)
5481 wattr_off(view->window,
5482 COLOR_PAIR(tc->colorpair), NULL);
5483 if (width < view->ncols - 1)
5484 waddch(view->window, '\n');
5485 if (n == s->selected && view->focussed)
5486 wstandend(view->window);
5487 free(line);
5488 free(wline);
5489 wline = NULL;
5490 n++;
5491 s->ndisplayed++;
5492 s->last_displayed_entry = te;
5493 if (--limit <= 0)
5494 break;
5497 return err;
5500 static void
5501 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5503 struct got_tree_entry *te;
5504 int isroot = s->tree == s->root;
5505 int i = 0;
5507 if (s->first_displayed_entry == NULL)
5508 return;
5510 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5511 while (i++ < maxscroll) {
5512 if (te == NULL) {
5513 if (!isroot)
5514 s->first_displayed_entry = NULL;
5515 break;
5517 s->first_displayed_entry = te;
5518 te = got_tree_entry_get_prev(s->tree, te);
5522 static void
5523 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5525 struct got_tree_entry *next, *last;
5526 int n = 0;
5528 if (s->first_displayed_entry)
5529 next = got_tree_entry_get_next(s->tree,
5530 s->first_displayed_entry);
5531 else
5532 next = got_object_tree_get_first_entry(s->tree);
5534 last = s->last_displayed_entry;
5535 while (next && last && n++ < maxscroll) {
5536 last = got_tree_entry_get_next(s->tree, last);
5537 if (last) {
5538 s->first_displayed_entry = next;
5539 next = got_tree_entry_get_next(s->tree, next);
5544 static const struct got_error *
5545 tree_entry_path(char **path, struct tog_parent_trees *parents,
5546 struct got_tree_entry *te)
5548 const struct got_error *err = NULL;
5549 struct tog_parent_tree *pt;
5550 size_t len = 2; /* for leading slash and NUL */
5552 TAILQ_FOREACH(pt, parents, entry)
5553 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5554 + 1 /* slash */;
5555 if (te)
5556 len += strlen(got_tree_entry_get_name(te));
5558 *path = calloc(1, len);
5559 if (path == NULL)
5560 return got_error_from_errno("calloc");
5562 (*path)[0] = '/';
5563 pt = TAILQ_LAST(parents, tog_parent_trees);
5564 while (pt) {
5565 const char *name = got_tree_entry_get_name(pt->selected_entry);
5566 if (strlcat(*path, name, len) >= len) {
5567 err = got_error(GOT_ERR_NO_SPACE);
5568 goto done;
5570 if (strlcat(*path, "/", len) >= len) {
5571 err = got_error(GOT_ERR_NO_SPACE);
5572 goto done;
5574 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5576 if (te) {
5577 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5578 err = got_error(GOT_ERR_NO_SPACE);
5579 goto done;
5582 done:
5583 if (err) {
5584 free(*path);
5585 *path = NULL;
5587 return err;
5590 static const struct got_error *
5591 blame_tree_entry(struct tog_view **new_view, int begin_x,
5592 struct got_tree_entry *te, struct tog_parent_trees *parents,
5593 struct got_object_id *commit_id, struct got_repository *repo)
5595 const struct got_error *err = NULL;
5596 char *path;
5597 struct tog_view *blame_view;
5599 *new_view = NULL;
5601 err = tree_entry_path(&path, parents, te);
5602 if (err)
5603 return err;
5605 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5606 if (blame_view == NULL) {
5607 err = got_error_from_errno("view_open");
5608 goto done;
5611 err = open_blame_view(blame_view, path, commit_id, repo);
5612 if (err) {
5613 if (err->code == GOT_ERR_CANCELLED)
5614 err = NULL;
5615 view_close(blame_view);
5616 } else
5617 *new_view = blame_view;
5618 done:
5619 free(path);
5620 return err;
5623 static const struct got_error *
5624 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5625 struct tog_tree_view_state *s)
5627 struct tog_view *log_view;
5628 const struct got_error *err = NULL;
5629 char *path;
5631 *new_view = NULL;
5633 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5634 if (log_view == NULL)
5635 return got_error_from_errno("view_open");
5637 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5638 if (err)
5639 return err;
5641 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5642 path, 0);
5643 if (err)
5644 view_close(log_view);
5645 else
5646 *new_view = log_view;
5647 free(path);
5648 return err;
5651 static const struct got_error *
5652 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5653 const char *head_ref_name, struct got_repository *repo)
5655 const struct got_error *err = NULL;
5656 char *commit_id_str = NULL;
5657 struct tog_tree_view_state *s = &view->state.tree;
5658 struct got_commit_object *commit = NULL;
5660 TAILQ_INIT(&s->parents);
5661 STAILQ_INIT(&s->colors);
5663 s->commit_id = got_object_id_dup(commit_id);
5664 if (s->commit_id == NULL)
5665 return got_error_from_errno("got_object_id_dup");
5667 err = got_object_open_as_commit(&commit, repo, commit_id);
5668 if (err)
5669 goto done;
5672 * The root is opened here and will be closed when the view is closed.
5673 * Any visited subtrees and their path-wise parents are opened and
5674 * closed on demand.
5676 err = got_object_open_as_tree(&s->root, repo,
5677 got_object_commit_get_tree_id(commit));
5678 if (err)
5679 goto done;
5680 s->tree = s->root;
5682 err = got_object_id_str(&commit_id_str, commit_id);
5683 if (err != NULL)
5684 goto done;
5686 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5687 err = got_error_from_errno("asprintf");
5688 goto done;
5691 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5692 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5693 if (head_ref_name) {
5694 s->head_ref_name = strdup(head_ref_name);
5695 if (s->head_ref_name == NULL) {
5696 err = got_error_from_errno("strdup");
5697 goto done;
5700 s->repo = repo;
5702 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5703 err = add_color(&s->colors, "\\$$",
5704 TOG_COLOR_TREE_SUBMODULE,
5705 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5706 if (err)
5707 goto done;
5708 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5709 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5710 if (err)
5711 goto done;
5712 err = add_color(&s->colors, "/$",
5713 TOG_COLOR_TREE_DIRECTORY,
5714 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5715 if (err)
5716 goto done;
5718 err = add_color(&s->colors, "\\*$",
5719 TOG_COLOR_TREE_EXECUTABLE,
5720 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5721 if (err)
5722 goto done;
5724 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5725 get_color_value("TOG_COLOR_COMMIT"));
5726 if (err)
5727 goto done;
5730 view->show = show_tree_view;
5731 view->input = input_tree_view;
5732 view->close = close_tree_view;
5733 view->search_start = search_start_tree_view;
5734 view->search_next = search_next_tree_view;
5735 done:
5736 free(commit_id_str);
5737 if (commit)
5738 got_object_commit_close(commit);
5739 if (err)
5740 close_tree_view(view);
5741 return err;
5744 static const struct got_error *
5745 close_tree_view(struct tog_view *view)
5747 struct tog_tree_view_state *s = &view->state.tree;
5749 free_colors(&s->colors);
5750 free(s->tree_label);
5751 s->tree_label = NULL;
5752 free(s->commit_id);
5753 s->commit_id = NULL;
5754 free(s->head_ref_name);
5755 s->head_ref_name = NULL;
5756 while (!TAILQ_EMPTY(&s->parents)) {
5757 struct tog_parent_tree *parent;
5758 parent = TAILQ_FIRST(&s->parents);
5759 TAILQ_REMOVE(&s->parents, parent, entry);
5760 if (parent->tree != s->root)
5761 got_object_tree_close(parent->tree);
5762 free(parent);
5765 if (s->tree != NULL && s->tree != s->root)
5766 got_object_tree_close(s->tree);
5767 if (s->root)
5768 got_object_tree_close(s->root);
5769 return NULL;
5772 static const struct got_error *
5773 search_start_tree_view(struct tog_view *view)
5775 struct tog_tree_view_state *s = &view->state.tree;
5777 s->matched_entry = NULL;
5778 return NULL;
5781 static int
5782 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5784 regmatch_t regmatch;
5786 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5787 0) == 0;
5790 static const struct got_error *
5791 search_next_tree_view(struct tog_view *view)
5793 struct tog_tree_view_state *s = &view->state.tree;
5794 struct got_tree_entry *te = NULL;
5796 if (!view->searching) {
5797 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5798 return NULL;
5801 if (s->matched_entry) {
5802 if (view->searching == TOG_SEARCH_FORWARD) {
5803 if (s->selected_entry)
5804 te = got_tree_entry_get_next(s->tree,
5805 s->selected_entry);
5806 else
5807 te = got_object_tree_get_first_entry(s->tree);
5808 } else {
5809 if (s->selected_entry == NULL)
5810 te = got_object_tree_get_last_entry(s->tree);
5811 else
5812 te = got_tree_entry_get_prev(s->tree,
5813 s->selected_entry);
5815 } else {
5816 if (s->selected_entry)
5817 te = s->selected_entry;
5818 else if (view->searching == TOG_SEARCH_FORWARD)
5819 te = got_object_tree_get_first_entry(s->tree);
5820 else
5821 te = got_object_tree_get_last_entry(s->tree);
5824 while (1) {
5825 if (te == NULL) {
5826 if (s->matched_entry == NULL) {
5827 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5828 return NULL;
5830 if (view->searching == TOG_SEARCH_FORWARD)
5831 te = got_object_tree_get_first_entry(s->tree);
5832 else
5833 te = got_object_tree_get_last_entry(s->tree);
5836 if (match_tree_entry(te, &view->regex)) {
5837 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5838 s->matched_entry = te;
5839 break;
5842 if (view->searching == TOG_SEARCH_FORWARD)
5843 te = got_tree_entry_get_next(s->tree, te);
5844 else
5845 te = got_tree_entry_get_prev(s->tree, te);
5848 if (s->matched_entry) {
5849 s->first_displayed_entry = s->matched_entry;
5850 s->selected = 0;
5853 return NULL;
5856 static const struct got_error *
5857 show_tree_view(struct tog_view *view)
5859 const struct got_error *err = NULL;
5860 struct tog_tree_view_state *s = &view->state.tree;
5861 char *parent_path;
5863 err = tree_entry_path(&parent_path, &s->parents, NULL);
5864 if (err)
5865 return err;
5867 err = draw_tree_entries(view, parent_path);
5868 free(parent_path);
5870 view_vborder(view);
5871 return err;
5874 static const struct got_error *
5875 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5877 const struct got_error *err = NULL;
5878 struct tog_tree_view_state *s = &view->state.tree;
5879 struct tog_view *log_view, *ref_view;
5880 struct got_tree_entry *te;
5881 int begin_x = 0, n, nscroll = view->nlines - 3;
5883 switch (ch) {
5884 case 'i':
5885 s->show_ids = !s->show_ids;
5886 break;
5887 case 'l':
5888 if (!s->selected_entry)
5889 break;
5890 if (view_is_parent_view(view))
5891 begin_x = view_split_begin_x(view->begin_x);
5892 err = log_selected_tree_entry(&log_view, begin_x, s);
5893 view->focussed = 0;
5894 log_view->focussed = 1;
5895 if (view_is_parent_view(view)) {
5896 err = view_close_child(view);
5897 if (err)
5898 return err;
5899 err = view_set_child(view, log_view);
5900 if (err)
5901 return err;
5902 view->focus_child = 1;
5903 } else
5904 *new_view = log_view;
5905 break;
5906 case 'r':
5907 if (view_is_parent_view(view))
5908 begin_x = view_split_begin_x(view->begin_x);
5909 ref_view = view_open(view->nlines, view->ncols,
5910 view->begin_y, begin_x, TOG_VIEW_REF);
5911 if (ref_view == NULL)
5912 return got_error_from_errno("view_open");
5913 err = open_ref_view(ref_view, s->repo);
5914 if (err) {
5915 view_close(ref_view);
5916 return err;
5918 view->focussed = 0;
5919 ref_view->focussed = 1;
5920 if (view_is_parent_view(view)) {
5921 err = view_close_child(view);
5922 if (err)
5923 return err;
5924 err = view_set_child(view, ref_view);
5925 if (err)
5926 return err;
5927 view->focus_child = 1;
5928 } else
5929 *new_view = ref_view;
5930 break;
5931 case 'g':
5932 case KEY_HOME:
5933 s->selected = 0;
5934 if (s->tree == s->root)
5935 s->first_displayed_entry =
5936 got_object_tree_get_first_entry(s->tree);
5937 else
5938 s->first_displayed_entry = NULL;
5939 break;
5940 case 'G':
5941 case KEY_END:
5942 s->selected = 0;
5943 te = got_object_tree_get_last_entry(s->tree);
5944 for (n = 0; n < view->nlines - 3; n++) {
5945 if (te == NULL) {
5946 if(s->tree != s->root) {
5947 s->first_displayed_entry = NULL;
5948 n++;
5950 break;
5952 s->first_displayed_entry = te;
5953 te = got_tree_entry_get_prev(s->tree, te);
5955 if (n > 0)
5956 s->selected = n - 1;
5957 break;
5958 case 'k':
5959 case KEY_UP:
5960 case CTRL('p'):
5961 if (s->selected > 0) {
5962 s->selected--;
5963 break;
5965 tree_scroll_up(s, 1);
5966 break;
5967 case CTRL('u'):
5968 case 'u':
5969 nscroll /= 2;
5970 /* FALL THROUGH */
5971 case KEY_PPAGE:
5972 case CTRL('b'):
5973 if (s->tree == s->root) {
5974 if (got_object_tree_get_first_entry(s->tree) ==
5975 s->first_displayed_entry)
5976 s->selected -= MIN(s->selected, nscroll);
5977 } else {
5978 if (s->first_displayed_entry == NULL)
5979 s->selected -= MIN(s->selected, nscroll);
5981 tree_scroll_up(s, MAX(0, nscroll));
5982 break;
5983 case 'j':
5984 case KEY_DOWN:
5985 case CTRL('n'):
5986 if (s->selected < s->ndisplayed - 1) {
5987 s->selected++;
5988 break;
5990 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5991 == NULL)
5992 /* can't scroll any further */
5993 break;
5994 tree_scroll_down(s, 1);
5995 break;
5996 case CTRL('d'):
5997 case 'd':
5998 nscroll /= 2;
5999 /* FALL THROUGH */
6000 case KEY_NPAGE:
6001 case CTRL('f'):
6002 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6003 == NULL) {
6004 /* can't scroll any further; move cursor down */
6005 if (s->selected < s->ndisplayed - 1)
6006 s->selected += MIN(nscroll,
6007 s->ndisplayed - s->selected - 1);
6008 break;
6010 tree_scroll_down(s, nscroll);
6011 break;
6012 case KEY_ENTER:
6013 case '\r':
6014 case KEY_BACKSPACE:
6015 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6016 struct tog_parent_tree *parent;
6017 /* user selected '..' */
6018 if (s->tree == s->root)
6019 break;
6020 parent = TAILQ_FIRST(&s->parents);
6021 TAILQ_REMOVE(&s->parents, parent,
6022 entry);
6023 got_object_tree_close(s->tree);
6024 s->tree = parent->tree;
6025 s->first_displayed_entry =
6026 parent->first_displayed_entry;
6027 s->selected_entry =
6028 parent->selected_entry;
6029 s->selected = parent->selected;
6030 free(parent);
6031 } else if (S_ISDIR(got_tree_entry_get_mode(
6032 s->selected_entry))) {
6033 struct got_tree_object *subtree;
6034 err = got_object_open_as_tree(&subtree, s->repo,
6035 got_tree_entry_get_id(s->selected_entry));
6036 if (err)
6037 break;
6038 err = tree_view_visit_subtree(s, subtree);
6039 if (err) {
6040 got_object_tree_close(subtree);
6041 break;
6043 } else if (S_ISREG(got_tree_entry_get_mode(
6044 s->selected_entry))) {
6045 struct tog_view *blame_view;
6046 int begin_x = view_is_parent_view(view) ?
6047 view_split_begin_x(view->begin_x) : 0;
6049 err = blame_tree_entry(&blame_view, begin_x,
6050 s->selected_entry, &s->parents,
6051 s->commit_id, s->repo);
6052 if (err)
6053 break;
6054 view->focussed = 0;
6055 blame_view->focussed = 1;
6056 if (view_is_parent_view(view)) {
6057 err = view_close_child(view);
6058 if (err)
6059 return err;
6060 err = view_set_child(view, blame_view);
6061 if (err)
6062 return err;
6063 view->focus_child = 1;
6064 } else
6065 *new_view = blame_view;
6067 break;
6068 case KEY_RESIZE:
6069 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6070 s->selected = view->nlines - 4;
6071 break;
6072 default:
6073 break;
6076 return err;
6079 __dead static void
6080 usage_tree(void)
6082 endwin();
6083 fprintf(stderr,
6084 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6085 getprogname());
6086 exit(1);
6089 static const struct got_error *
6090 cmd_tree(int argc, char *argv[])
6092 const struct got_error *error;
6093 struct got_repository *repo = NULL;
6094 struct got_worktree *worktree = NULL;
6095 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6096 struct got_object_id *commit_id = NULL;
6097 struct got_commit_object *commit = NULL;
6098 const char *commit_id_arg = NULL;
6099 char *label = NULL;
6100 struct got_reference *ref = NULL;
6101 const char *head_ref_name = NULL;
6102 int ch;
6103 struct tog_view *view;
6104 int *pack_fds = NULL;
6106 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6107 switch (ch) {
6108 case 'c':
6109 commit_id_arg = optarg;
6110 break;
6111 case 'r':
6112 repo_path = realpath(optarg, NULL);
6113 if (repo_path == NULL)
6114 return got_error_from_errno2("realpath",
6115 optarg);
6116 break;
6117 default:
6118 usage_tree();
6119 /* NOTREACHED */
6123 argc -= optind;
6124 argv += optind;
6126 if (argc > 1)
6127 usage_tree();
6129 error = got_repo_pack_fds_open(&pack_fds);
6130 if (error != NULL)
6131 goto done;
6133 if (repo_path == NULL) {
6134 cwd = getcwd(NULL, 0);
6135 if (cwd == NULL)
6136 return got_error_from_errno("getcwd");
6137 error = got_worktree_open(&worktree, cwd);
6138 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6139 goto done;
6140 if (worktree)
6141 repo_path =
6142 strdup(got_worktree_get_repo_path(worktree));
6143 else
6144 repo_path = strdup(cwd);
6145 if (repo_path == NULL) {
6146 error = got_error_from_errno("strdup");
6147 goto done;
6151 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6152 if (error != NULL)
6153 goto done;
6155 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6156 repo, worktree);
6157 if (error)
6158 goto done;
6160 init_curses();
6162 error = apply_unveil(got_repo_get_path(repo), NULL);
6163 if (error)
6164 goto done;
6166 error = tog_load_refs(repo, 0);
6167 if (error)
6168 goto done;
6170 if (commit_id_arg == NULL) {
6171 error = got_repo_match_object_id(&commit_id, &label,
6172 worktree ? got_worktree_get_head_ref_name(worktree) :
6173 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6174 if (error)
6175 goto done;
6176 head_ref_name = label;
6177 } else {
6178 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6179 if (error == NULL)
6180 head_ref_name = got_ref_get_name(ref);
6181 else if (error->code != GOT_ERR_NOT_REF)
6182 goto done;
6183 error = got_repo_match_object_id(&commit_id, NULL,
6184 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6185 if (error)
6186 goto done;
6189 error = got_object_open_as_commit(&commit, repo, commit_id);
6190 if (error)
6191 goto done;
6193 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6194 if (view == NULL) {
6195 error = got_error_from_errno("view_open");
6196 goto done;
6198 error = open_tree_view(view, commit_id, head_ref_name, repo);
6199 if (error)
6200 goto done;
6201 if (!got_path_is_root_dir(in_repo_path)) {
6202 error = tree_view_walk_path(&view->state.tree, commit,
6203 in_repo_path);
6204 if (error)
6205 goto done;
6208 if (worktree) {
6209 /* Release work tree lock. */
6210 got_worktree_close(worktree);
6211 worktree = NULL;
6213 error = view_loop(view);
6214 done:
6215 free(repo_path);
6216 free(cwd);
6217 free(commit_id);
6218 free(label);
6219 if (ref)
6220 got_ref_close(ref);
6221 if (repo) {
6222 const struct got_error *close_err = got_repo_close(repo);
6223 if (error == NULL)
6224 error = close_err;
6226 if (pack_fds) {
6227 const struct got_error *pack_err =
6228 got_repo_pack_fds_close(pack_fds);
6229 if (error == NULL)
6230 error = pack_err;
6232 tog_free_refs();
6233 return error;
6236 static const struct got_error *
6237 ref_view_load_refs(struct tog_ref_view_state *s)
6239 struct got_reflist_entry *sre;
6240 struct tog_reflist_entry *re;
6242 s->nrefs = 0;
6243 TAILQ_FOREACH(sre, &tog_refs, entry) {
6244 if (strncmp(got_ref_get_name(sre->ref),
6245 "refs/got/", 9) == 0 &&
6246 strncmp(got_ref_get_name(sre->ref),
6247 "refs/got/backup/", 16) != 0)
6248 continue;
6250 re = malloc(sizeof(*re));
6251 if (re == NULL)
6252 return got_error_from_errno("malloc");
6254 re->ref = got_ref_dup(sre->ref);
6255 if (re->ref == NULL)
6256 return got_error_from_errno("got_ref_dup");
6257 re->idx = s->nrefs++;
6258 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6261 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6262 return NULL;
6265 void
6266 ref_view_free_refs(struct tog_ref_view_state *s)
6268 struct tog_reflist_entry *re;
6270 while (!TAILQ_EMPTY(&s->refs)) {
6271 re = TAILQ_FIRST(&s->refs);
6272 TAILQ_REMOVE(&s->refs, re, entry);
6273 got_ref_close(re->ref);
6274 free(re);
6278 static const struct got_error *
6279 open_ref_view(struct tog_view *view, struct got_repository *repo)
6281 const struct got_error *err = NULL;
6282 struct tog_ref_view_state *s = &view->state.ref;
6284 s->selected_entry = 0;
6285 s->repo = repo;
6287 TAILQ_INIT(&s->refs);
6288 STAILQ_INIT(&s->colors);
6290 err = ref_view_load_refs(s);
6291 if (err)
6292 return err;
6294 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6295 err = add_color(&s->colors, "^refs/heads/",
6296 TOG_COLOR_REFS_HEADS,
6297 get_color_value("TOG_COLOR_REFS_HEADS"));
6298 if (err)
6299 goto done;
6301 err = add_color(&s->colors, "^refs/tags/",
6302 TOG_COLOR_REFS_TAGS,
6303 get_color_value("TOG_COLOR_REFS_TAGS"));
6304 if (err)
6305 goto done;
6307 err = add_color(&s->colors, "^refs/remotes/",
6308 TOG_COLOR_REFS_REMOTES,
6309 get_color_value("TOG_COLOR_REFS_REMOTES"));
6310 if (err)
6311 goto done;
6313 err = add_color(&s->colors, "^refs/got/backup/",
6314 TOG_COLOR_REFS_BACKUP,
6315 get_color_value("TOG_COLOR_REFS_BACKUP"));
6316 if (err)
6317 goto done;
6320 view->show = show_ref_view;
6321 view->input = input_ref_view;
6322 view->close = close_ref_view;
6323 view->search_start = search_start_ref_view;
6324 view->search_next = search_next_ref_view;
6325 done:
6326 if (err)
6327 free_colors(&s->colors);
6328 return err;
6331 static const struct got_error *
6332 close_ref_view(struct tog_view *view)
6334 struct tog_ref_view_state *s = &view->state.ref;
6336 ref_view_free_refs(s);
6337 free_colors(&s->colors);
6339 return NULL;
6342 static const struct got_error *
6343 resolve_reflist_entry(struct got_object_id **commit_id,
6344 struct tog_reflist_entry *re, struct got_repository *repo)
6346 const struct got_error *err = NULL;
6347 struct got_object_id *obj_id;
6348 struct got_tag_object *tag = NULL;
6349 int obj_type;
6351 *commit_id = NULL;
6353 err = got_ref_resolve(&obj_id, repo, re->ref);
6354 if (err)
6355 return err;
6357 err = got_object_get_type(&obj_type, repo, obj_id);
6358 if (err)
6359 goto done;
6361 switch (obj_type) {
6362 case GOT_OBJ_TYPE_COMMIT:
6363 *commit_id = obj_id;
6364 break;
6365 case GOT_OBJ_TYPE_TAG:
6366 err = got_object_open_as_tag(&tag, repo, obj_id);
6367 if (err)
6368 goto done;
6369 free(obj_id);
6370 err = got_object_get_type(&obj_type, repo,
6371 got_object_tag_get_object_id(tag));
6372 if (err)
6373 goto done;
6374 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6375 err = got_error(GOT_ERR_OBJ_TYPE);
6376 goto done;
6378 *commit_id = got_object_id_dup(
6379 got_object_tag_get_object_id(tag));
6380 if (*commit_id == NULL) {
6381 err = got_error_from_errno("got_object_id_dup");
6382 goto done;
6384 break;
6385 default:
6386 err = got_error(GOT_ERR_OBJ_TYPE);
6387 break;
6390 done:
6391 if (tag)
6392 got_object_tag_close(tag);
6393 if (err) {
6394 free(*commit_id);
6395 *commit_id = NULL;
6397 return err;
6400 static const struct got_error *
6401 log_ref_entry(struct tog_view **new_view, int begin_x,
6402 struct tog_reflist_entry *re, struct got_repository *repo)
6404 struct tog_view *log_view;
6405 const struct got_error *err = NULL;
6406 struct got_object_id *commit_id = NULL;
6408 *new_view = NULL;
6410 err = resolve_reflist_entry(&commit_id, re, repo);
6411 if (err) {
6412 if (err->code != GOT_ERR_OBJ_TYPE)
6413 return err;
6414 else
6415 return NULL;
6418 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6419 if (log_view == NULL) {
6420 err = got_error_from_errno("view_open");
6421 goto done;
6424 err = open_log_view(log_view, commit_id, repo,
6425 got_ref_get_name(re->ref), "", 0);
6426 done:
6427 if (err)
6428 view_close(log_view);
6429 else
6430 *new_view = log_view;
6431 free(commit_id);
6432 return err;
6435 static void
6436 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6438 struct tog_reflist_entry *re;
6439 int i = 0;
6441 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6442 return;
6444 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6445 while (i++ < maxscroll) {
6446 if (re == NULL)
6447 break;
6448 s->first_displayed_entry = re;
6449 re = TAILQ_PREV(re, tog_reflist_head, entry);
6453 static void
6454 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6456 struct tog_reflist_entry *next, *last;
6457 int n = 0;
6459 if (s->first_displayed_entry)
6460 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6461 else
6462 next = TAILQ_FIRST(&s->refs);
6464 last = s->last_displayed_entry;
6465 while (next && last && n++ < maxscroll) {
6466 last = TAILQ_NEXT(last, entry);
6467 if (last) {
6468 s->first_displayed_entry = next;
6469 next = TAILQ_NEXT(next, entry);
6474 static const struct got_error *
6475 search_start_ref_view(struct tog_view *view)
6477 struct tog_ref_view_state *s = &view->state.ref;
6479 s->matched_entry = NULL;
6480 return NULL;
6483 static int
6484 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6486 regmatch_t regmatch;
6488 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6489 0) == 0;
6492 static const struct got_error *
6493 search_next_ref_view(struct tog_view *view)
6495 struct tog_ref_view_state *s = &view->state.ref;
6496 struct tog_reflist_entry *re = NULL;
6498 if (!view->searching) {
6499 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6500 return NULL;
6503 if (s->matched_entry) {
6504 if (view->searching == TOG_SEARCH_FORWARD) {
6505 if (s->selected_entry)
6506 re = TAILQ_NEXT(s->selected_entry, entry);
6507 else
6508 re = TAILQ_PREV(s->selected_entry,
6509 tog_reflist_head, entry);
6510 } else {
6511 if (s->selected_entry == NULL)
6512 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6513 else
6514 re = TAILQ_PREV(s->selected_entry,
6515 tog_reflist_head, entry);
6517 } else {
6518 if (s->selected_entry)
6519 re = s->selected_entry;
6520 else if (view->searching == TOG_SEARCH_FORWARD)
6521 re = TAILQ_FIRST(&s->refs);
6522 else
6523 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6526 while (1) {
6527 if (re == NULL) {
6528 if (s->matched_entry == NULL) {
6529 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6530 return NULL;
6532 if (view->searching == TOG_SEARCH_FORWARD)
6533 re = TAILQ_FIRST(&s->refs);
6534 else
6535 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6538 if (match_reflist_entry(re, &view->regex)) {
6539 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6540 s->matched_entry = re;
6541 break;
6544 if (view->searching == TOG_SEARCH_FORWARD)
6545 re = TAILQ_NEXT(re, entry);
6546 else
6547 re = TAILQ_PREV(re, tog_reflist_head, entry);
6550 if (s->matched_entry) {
6551 s->first_displayed_entry = s->matched_entry;
6552 s->selected = 0;
6555 return NULL;
6558 static const struct got_error *
6559 show_ref_view(struct tog_view *view)
6561 const struct got_error *err = NULL;
6562 struct tog_ref_view_state *s = &view->state.ref;
6563 struct tog_reflist_entry *re;
6564 char *line = NULL;
6565 wchar_t *wline;
6566 struct tog_color *tc;
6567 int width, n;
6568 int limit = view->nlines;
6570 werase(view->window);
6572 s->ndisplayed = 0;
6574 if (limit == 0)
6575 return NULL;
6577 re = s->first_displayed_entry;
6579 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6580 s->nrefs) == -1)
6581 return got_error_from_errno("asprintf");
6583 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6584 if (err) {
6585 free(line);
6586 return err;
6588 if (view_needs_focus_indication(view))
6589 wstandout(view->window);
6590 waddwstr(view->window, wline);
6591 if (view_needs_focus_indication(view))
6592 wstandend(view->window);
6593 free(wline);
6594 wline = NULL;
6595 free(line);
6596 line = NULL;
6597 if (width < view->ncols - 1)
6598 waddch(view->window, '\n');
6599 if (--limit <= 0)
6600 return NULL;
6602 n = 0;
6603 while (re && limit > 0) {
6604 char *line = NULL;
6605 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6607 if (s->show_date) {
6608 struct got_commit_object *ci;
6609 struct got_tag_object *tag;
6610 struct got_object_id *id;
6611 struct tm tm;
6612 time_t t;
6614 err = got_ref_resolve(&id, s->repo, re->ref);
6615 if (err)
6616 return err;
6617 err = got_object_open_as_tag(&tag, s->repo, id);
6618 if (err) {
6619 if (err->code != GOT_ERR_OBJ_TYPE) {
6620 free(id);
6621 return err;
6623 err = got_object_open_as_commit(&ci, s->repo,
6624 id);
6625 if (err) {
6626 free(id);
6627 return err;
6629 t = got_object_commit_get_committer_time(ci);
6630 got_object_commit_close(ci);
6631 } else {
6632 t = got_object_tag_get_tagger_time(tag);
6633 got_object_tag_close(tag);
6635 free(id);
6636 if (gmtime_r(&t, &tm) == NULL)
6637 return got_error_from_errno("gmtime_r");
6638 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6639 return got_error(GOT_ERR_NO_SPACE);
6641 if (got_ref_is_symbolic(re->ref)) {
6642 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6643 ymd : "", got_ref_get_name(re->ref),
6644 got_ref_get_symref_target(re->ref)) == -1)
6645 return got_error_from_errno("asprintf");
6646 } else if (s->show_ids) {
6647 struct got_object_id *id;
6648 char *id_str;
6649 err = got_ref_resolve(&id, s->repo, re->ref);
6650 if (err)
6651 return err;
6652 err = got_object_id_str(&id_str, id);
6653 if (err) {
6654 free(id);
6655 return err;
6657 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6658 got_ref_get_name(re->ref), id_str) == -1) {
6659 err = got_error_from_errno("asprintf");
6660 free(id);
6661 free(id_str);
6662 return err;
6664 free(id);
6665 free(id_str);
6666 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6667 got_ref_get_name(re->ref)) == -1)
6668 return got_error_from_errno("asprintf");
6670 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6671 0, 0);
6672 if (err) {
6673 free(line);
6674 return err;
6676 if (n == s->selected) {
6677 if (view->focussed)
6678 wstandout(view->window);
6679 s->selected_entry = re;
6681 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6682 if (tc)
6683 wattr_on(view->window,
6684 COLOR_PAIR(tc->colorpair), NULL);
6685 waddwstr(view->window, wline);
6686 if (tc)
6687 wattr_off(view->window,
6688 COLOR_PAIR(tc->colorpair), NULL);
6689 if (width < view->ncols - 1)
6690 waddch(view->window, '\n');
6691 if (n == s->selected && view->focussed)
6692 wstandend(view->window);
6693 free(line);
6694 free(wline);
6695 wline = NULL;
6696 n++;
6697 s->ndisplayed++;
6698 s->last_displayed_entry = re;
6700 limit--;
6701 re = TAILQ_NEXT(re, entry);
6704 view_vborder(view);
6705 return err;
6708 static const struct got_error *
6709 browse_ref_tree(struct tog_view **new_view, int begin_x,
6710 struct tog_reflist_entry *re, struct got_repository *repo)
6712 const struct got_error *err = NULL;
6713 struct got_object_id *commit_id = NULL;
6714 struct tog_view *tree_view;
6716 *new_view = NULL;
6718 err = resolve_reflist_entry(&commit_id, re, repo);
6719 if (err) {
6720 if (err->code != GOT_ERR_OBJ_TYPE)
6721 return err;
6722 else
6723 return NULL;
6727 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6728 if (tree_view == NULL) {
6729 err = got_error_from_errno("view_open");
6730 goto done;
6733 err = open_tree_view(tree_view, commit_id,
6734 got_ref_get_name(re->ref), repo);
6735 if (err)
6736 goto done;
6738 *new_view = tree_view;
6739 done:
6740 free(commit_id);
6741 return err;
6743 static const struct got_error *
6744 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6746 const struct got_error *err = NULL;
6747 struct tog_ref_view_state *s = &view->state.ref;
6748 struct tog_view *log_view, *tree_view;
6749 struct tog_reflist_entry *re;
6750 int begin_x = 0, n, nscroll = view->nlines - 1;
6752 switch (ch) {
6753 case 'i':
6754 s->show_ids = !s->show_ids;
6755 break;
6756 case 'm':
6757 s->show_date = !s->show_date;
6758 break;
6759 case 'o':
6760 s->sort_by_date = !s->sort_by_date;
6761 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6762 got_ref_cmp_by_commit_timestamp_descending :
6763 tog_ref_cmp_by_name, s->repo);
6764 if (err)
6765 break;
6766 got_reflist_object_id_map_free(tog_refs_idmap);
6767 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6768 &tog_refs, s->repo);
6769 if (err)
6770 break;
6771 ref_view_free_refs(s);
6772 err = ref_view_load_refs(s);
6773 break;
6774 case KEY_ENTER:
6775 case '\r':
6776 if (!s->selected_entry)
6777 break;
6778 if (view_is_parent_view(view))
6779 begin_x = view_split_begin_x(view->begin_x);
6780 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6781 s->repo);
6782 view->focussed = 0;
6783 log_view->focussed = 1;
6784 if (view_is_parent_view(view)) {
6785 err = view_close_child(view);
6786 if (err)
6787 return err;
6788 err = view_set_child(view, log_view);
6789 if (err)
6790 return err;
6791 view->focus_child = 1;
6792 } else
6793 *new_view = log_view;
6794 break;
6795 case 't':
6796 if (!s->selected_entry)
6797 break;
6798 if (view_is_parent_view(view))
6799 begin_x = view_split_begin_x(view->begin_x);
6800 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6801 s->repo);
6802 if (err || tree_view == NULL)
6803 break;
6804 view->focussed = 0;
6805 tree_view->focussed = 1;
6806 if (view_is_parent_view(view)) {
6807 err = view_close_child(view);
6808 if (err)
6809 return err;
6810 err = view_set_child(view, tree_view);
6811 if (err)
6812 return err;
6813 view->focus_child = 1;
6814 } else
6815 *new_view = tree_view;
6816 break;
6817 case 'g':
6818 case KEY_HOME:
6819 s->selected = 0;
6820 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6821 break;
6822 case 'G':
6823 case KEY_END:
6824 s->selected = 0;
6825 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6826 for (n = 0; n < view->nlines - 1; n++) {
6827 if (re == NULL)
6828 break;
6829 s->first_displayed_entry = re;
6830 re = TAILQ_PREV(re, tog_reflist_head, entry);
6832 if (n > 0)
6833 s->selected = n - 1;
6834 break;
6835 case 'k':
6836 case KEY_UP:
6837 case CTRL('p'):
6838 if (s->selected > 0) {
6839 s->selected--;
6840 break;
6842 ref_scroll_up(s, 1);
6843 break;
6844 case CTRL('u'):
6845 case 'u':
6846 nscroll /= 2;
6847 /* FALL THROUGH */
6848 case KEY_PPAGE:
6849 case CTRL('b'):
6850 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6851 s->selected -= MIN(nscroll, s->selected);
6852 ref_scroll_up(s, MAX(0, nscroll));
6853 break;
6854 case 'j':
6855 case KEY_DOWN:
6856 case CTRL('n'):
6857 if (s->selected < s->ndisplayed - 1) {
6858 s->selected++;
6859 break;
6861 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6862 /* can't scroll any further */
6863 break;
6864 ref_scroll_down(s, 1);
6865 break;
6866 case CTRL('d'):
6867 case 'd':
6868 nscroll /= 2;
6869 /* FALL THROUGH */
6870 case KEY_NPAGE:
6871 case CTRL('f'):
6872 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6873 /* can't scroll any further; move cursor down */
6874 if (s->selected < s->ndisplayed - 1)
6875 s->selected += MIN(nscroll,
6876 s->ndisplayed - s->selected - 1);
6877 break;
6879 ref_scroll_down(s, nscroll);
6880 break;
6881 case CTRL('l'):
6882 tog_free_refs();
6883 err = tog_load_refs(s->repo, s->sort_by_date);
6884 if (err)
6885 break;
6886 ref_view_free_refs(s);
6887 err = ref_view_load_refs(s);
6888 break;
6889 case KEY_RESIZE:
6890 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6891 s->selected = view->nlines - 2;
6892 break;
6893 default:
6894 break;
6897 return err;
6900 __dead static void
6901 usage_ref(void)
6903 endwin();
6904 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6905 getprogname());
6906 exit(1);
6909 static const struct got_error *
6910 cmd_ref(int argc, char *argv[])
6912 const struct got_error *error;
6913 struct got_repository *repo = NULL;
6914 struct got_worktree *worktree = NULL;
6915 char *cwd = NULL, *repo_path = NULL;
6916 int ch;
6917 struct tog_view *view;
6918 int *pack_fds = NULL;
6920 while ((ch = getopt(argc, argv, "r:")) != -1) {
6921 switch (ch) {
6922 case 'r':
6923 repo_path = realpath(optarg, NULL);
6924 if (repo_path == NULL)
6925 return got_error_from_errno2("realpath",
6926 optarg);
6927 break;
6928 default:
6929 usage_ref();
6930 /* NOTREACHED */
6934 argc -= optind;
6935 argv += optind;
6937 if (argc > 1)
6938 usage_ref();
6940 error = got_repo_pack_fds_open(&pack_fds);
6941 if (error != NULL)
6942 goto done;
6944 if (repo_path == NULL) {
6945 cwd = getcwd(NULL, 0);
6946 if (cwd == NULL)
6947 return got_error_from_errno("getcwd");
6948 error = got_worktree_open(&worktree, cwd);
6949 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6950 goto done;
6951 if (worktree)
6952 repo_path =
6953 strdup(got_worktree_get_repo_path(worktree));
6954 else
6955 repo_path = strdup(cwd);
6956 if (repo_path == NULL) {
6957 error = got_error_from_errno("strdup");
6958 goto done;
6962 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6963 if (error != NULL)
6964 goto done;
6966 init_curses();
6968 error = apply_unveil(got_repo_get_path(repo), NULL);
6969 if (error)
6970 goto done;
6972 error = tog_load_refs(repo, 0);
6973 if (error)
6974 goto done;
6976 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6977 if (view == NULL) {
6978 error = got_error_from_errno("view_open");
6979 goto done;
6982 error = open_ref_view(view, repo);
6983 if (error)
6984 goto done;
6986 if (worktree) {
6987 /* Release work tree lock. */
6988 got_worktree_close(worktree);
6989 worktree = NULL;
6991 error = view_loop(view);
6992 done:
6993 free(repo_path);
6994 free(cwd);
6995 if (repo) {
6996 const struct got_error *close_err = got_repo_close(repo);
6997 if (close_err)
6998 error = close_err;
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 tog_free_refs();
7007 return error;
7010 static void
7011 list_commands(FILE *fp)
7013 size_t i;
7015 fprintf(fp, "commands:");
7016 for (i = 0; i < nitems(tog_commands); i++) {
7017 const struct tog_cmd *cmd = &tog_commands[i];
7018 fprintf(fp, " %s", cmd->name);
7020 fputc('\n', fp);
7023 __dead static void
7024 usage(int hflag, int status)
7026 FILE *fp = (status == 0) ? stdout : stderr;
7028 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7029 getprogname());
7030 if (hflag) {
7031 fprintf(fp, "lazy usage: %s path\n", getprogname());
7032 list_commands(fp);
7034 exit(status);
7037 static char **
7038 make_argv(int argc, ...)
7040 va_list ap;
7041 char **argv;
7042 int i;
7044 va_start(ap, argc);
7046 argv = calloc(argc, sizeof(char *));
7047 if (argv == NULL)
7048 err(1, "calloc");
7049 for (i = 0; i < argc; i++) {
7050 argv[i] = strdup(va_arg(ap, char *));
7051 if (argv[i] == NULL)
7052 err(1, "strdup");
7055 va_end(ap);
7056 return argv;
7060 * Try to convert 'tog path' into a 'tog log path' command.
7061 * The user could simply have mistyped the command rather than knowingly
7062 * provided a path. So check whether argv[0] can in fact be resolved
7063 * to a path in the HEAD commit and print a special error if not.
7064 * This hack is for mpi@ <3
7066 static const struct got_error *
7067 tog_log_with_path(int argc, char *argv[])
7069 const struct got_error *error = NULL, *close_err;
7070 const struct tog_cmd *cmd = NULL;
7071 struct got_repository *repo = NULL;
7072 struct got_worktree *worktree = NULL;
7073 struct got_object_id *commit_id = NULL, *id = NULL;
7074 struct got_commit_object *commit = NULL;
7075 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7076 char *commit_id_str = NULL, **cmd_argv = NULL;
7077 int *pack_fds = NULL;
7079 cwd = getcwd(NULL, 0);
7080 if (cwd == NULL)
7081 return got_error_from_errno("getcwd");
7083 error = got_repo_pack_fds_open(&pack_fds);
7084 if (error != NULL)
7085 goto done;
7087 error = got_worktree_open(&worktree, cwd);
7088 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7089 goto done;
7091 if (worktree)
7092 repo_path = strdup(got_worktree_get_repo_path(worktree));
7093 else
7094 repo_path = strdup(cwd);
7095 if (repo_path == NULL) {
7096 error = got_error_from_errno("strdup");
7097 goto done;
7100 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7101 if (error != NULL)
7102 goto done;
7104 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7105 repo, worktree);
7106 if (error)
7107 goto done;
7109 error = tog_load_refs(repo, 0);
7110 if (error)
7111 goto done;
7112 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7113 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7114 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7115 if (error)
7116 goto done;
7118 if (worktree) {
7119 got_worktree_close(worktree);
7120 worktree = NULL;
7123 error = got_object_open_as_commit(&commit, repo, commit_id);
7124 if (error)
7125 goto done;
7127 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7128 if (error) {
7129 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7130 goto done;
7131 fprintf(stderr, "%s: '%s' is no known command or path\n",
7132 getprogname(), argv[0]);
7133 usage(1, 1);
7134 /* not reached */
7137 close_err = got_repo_close(repo);
7138 if (error == NULL)
7139 error = close_err;
7140 repo = NULL;
7142 error = got_object_id_str(&commit_id_str, commit_id);
7143 if (error)
7144 goto done;
7146 cmd = &tog_commands[0]; /* log */
7147 argc = 4;
7148 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7149 error = cmd->cmd_main(argc, cmd_argv);
7150 done:
7151 if (repo) {
7152 close_err = got_repo_close(repo);
7153 if (error == NULL)
7154 error = close_err;
7156 if (commit)
7157 got_object_commit_close(commit);
7158 if (worktree)
7159 got_worktree_close(worktree);
7160 if (pack_fds) {
7161 const struct got_error *pack_err =
7162 got_repo_pack_fds_close(pack_fds);
7163 if (error == NULL)
7164 error = pack_err;
7166 free(id);
7167 free(commit_id_str);
7168 free(commit_id);
7169 free(cwd);
7170 free(repo_path);
7171 free(in_repo_path);
7172 if (cmd_argv) {
7173 int i;
7174 for (i = 0; i < argc; i++)
7175 free(cmd_argv[i]);
7176 free(cmd_argv);
7178 tog_free_refs();
7179 return error;
7182 int
7183 main(int argc, char *argv[])
7185 const struct got_error *error = NULL;
7186 const struct tog_cmd *cmd = NULL;
7187 int ch, hflag = 0, Vflag = 0;
7188 char **cmd_argv = NULL;
7189 static const struct option longopts[] = {
7190 { "version", no_argument, NULL, 'V' },
7191 { NULL, 0, NULL, 0}
7194 setlocale(LC_CTYPE, "");
7196 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7197 switch (ch) {
7198 case 'h':
7199 hflag = 1;
7200 break;
7201 case 'V':
7202 Vflag = 1;
7203 break;
7204 default:
7205 usage(hflag, 1);
7206 /* NOTREACHED */
7210 argc -= optind;
7211 argv += optind;
7212 optind = 1;
7213 optreset = 1;
7215 if (Vflag) {
7216 got_version_print_str();
7217 return 0;
7220 #ifndef PROFILE
7221 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7222 NULL) == -1)
7223 err(1, "pledge");
7224 #endif
7226 if (argc == 0) {
7227 if (hflag)
7228 usage(hflag, 0);
7229 /* Build an argument vector which runs a default command. */
7230 cmd = &tog_commands[0];
7231 argc = 1;
7232 cmd_argv = make_argv(argc, cmd->name);
7233 } else {
7234 size_t i;
7236 /* Did the user specify a command? */
7237 for (i = 0; i < nitems(tog_commands); i++) {
7238 if (strncmp(tog_commands[i].name, argv[0],
7239 strlen(argv[0])) == 0) {
7240 cmd = &tog_commands[i];
7241 break;
7246 if (cmd == NULL) {
7247 if (argc != 1)
7248 usage(0, 1);
7249 /* No command specified; try log with a path */
7250 error = tog_log_with_path(argc, argv);
7251 } else {
7252 if (hflag)
7253 cmd->cmd_usage();
7254 else
7255 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7258 endwin();
7259 putchar('\n');
7260 if (cmd_argv) {
7261 int i;
7262 for (i = 0; i < argc; i++)
7263 free(cmd_argv[i]);
7264 free(cmd_argv);
7267 if (error && error->code != GOT_ERR_CANCELLED)
7268 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7269 return 0;