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 if (view->searching == TOG_SEARCH_FORWARD)
2355 entry = TAILQ_NEXT(s->matched_entry, entry);
2356 else
2357 entry = TAILQ_PREV(s->matched_entry,
2358 commit_queue_head, entry);
2359 } else {
2360 entry = s->selected_entry;
2363 while (1) {
2364 int have_match = 0;
2366 if (entry == NULL) {
2367 if (s->thread_args.log_complete ||
2368 view->searching == TOG_SEARCH_BACKWARD) {
2369 view->search_next_done =
2370 (s->matched_entry == NULL ?
2371 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2372 s->search_entry = NULL;
2373 return NULL;
2376 * Poke the log thread for more commits and return,
2377 * allowing the main loop to make progress. Search
2378 * will resume at s->search_entry once we come back.
2380 s->thread_args.commits_needed++;
2381 return trigger_log_thread(view, 0);
2384 err = match_commit(&have_match, entry->id, entry->commit,
2385 &view->regex);
2386 if (err)
2387 break;
2388 if (have_match) {
2389 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2390 s->matched_entry = entry;
2391 break;
2394 s->search_entry = entry;
2395 if (view->searching == TOG_SEARCH_FORWARD)
2396 entry = TAILQ_NEXT(entry, entry);
2397 else
2398 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2401 if (s->matched_entry) {
2402 int cur = s->selected_entry->idx;
2403 while (cur < s->matched_entry->idx) {
2404 err = input_log_view(NULL, view, KEY_DOWN);
2405 if (err)
2406 return err;
2407 cur++;
2409 while (cur > s->matched_entry->idx) {
2410 err = input_log_view(NULL, view, KEY_UP);
2411 if (err)
2412 return err;
2413 cur--;
2417 s->search_entry = NULL;
2419 return NULL;
2422 static const struct got_error *
2423 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2424 struct got_repository *repo, const char *head_ref_name,
2425 const char *in_repo_path, int log_branches)
2427 const struct got_error *err = NULL;
2428 struct tog_log_view_state *s = &view->state.log;
2429 struct got_repository *thread_repo = NULL;
2430 struct got_commit_graph *thread_graph = NULL;
2431 int errcode;
2433 if (in_repo_path != s->in_repo_path) {
2434 free(s->in_repo_path);
2435 s->in_repo_path = strdup(in_repo_path);
2436 if (s->in_repo_path == NULL)
2437 return got_error_from_errno("strdup");
2440 /* The commit queue only contains commits being displayed. */
2441 TAILQ_INIT(&s->commits.head);
2442 s->commits.ncommits = 0;
2444 s->repo = repo;
2445 if (head_ref_name) {
2446 s->head_ref_name = strdup(head_ref_name);
2447 if (s->head_ref_name == NULL) {
2448 err = got_error_from_errno("strdup");
2449 goto done;
2452 s->start_id = got_object_id_dup(start_id);
2453 if (s->start_id == NULL) {
2454 err = got_error_from_errno("got_object_id_dup");
2455 goto done;
2457 s->log_branches = log_branches;
2459 STAILQ_INIT(&s->colors);
2460 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2461 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2462 get_color_value("TOG_COLOR_COMMIT"));
2463 if (err)
2464 goto done;
2465 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2466 get_color_value("TOG_COLOR_AUTHOR"));
2467 if (err) {
2468 free_colors(&s->colors);
2469 goto done;
2471 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2472 get_color_value("TOG_COLOR_DATE"));
2473 if (err) {
2474 free_colors(&s->colors);
2475 goto done;
2479 view->show = show_log_view;
2480 view->input = input_log_view;
2481 view->close = close_log_view;
2482 view->search_start = search_start_log_view;
2483 view->search_next = search_next_log_view;
2485 if (s->thread_args.pack_fds == NULL) {
2486 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2487 if (err)
2488 goto done;
2490 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2491 s->thread_args.pack_fds);
2492 if (err)
2493 goto done;
2494 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2495 !s->log_branches);
2496 if (err)
2497 goto done;
2498 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2499 s->repo, NULL, NULL);
2500 if (err)
2501 goto done;
2503 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2504 if (errcode) {
2505 err = got_error_set_errno(errcode, "pthread_cond_init");
2506 goto done;
2508 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2509 if (errcode) {
2510 err = got_error_set_errno(errcode, "pthread_cond_init");
2511 goto done;
2514 s->thread_args.commits_needed = view->nlines;
2515 s->thread_args.graph = thread_graph;
2516 s->thread_args.commits = &s->commits;
2517 s->thread_args.in_repo_path = s->in_repo_path;
2518 s->thread_args.start_id = s->start_id;
2519 s->thread_args.repo = thread_repo;
2520 s->thread_args.log_complete = 0;
2521 s->thread_args.quit = &s->quit;
2522 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2523 s->thread_args.selected_entry = &s->selected_entry;
2524 s->thread_args.searching = &view->searching;
2525 s->thread_args.search_next_done = &view->search_next_done;
2526 s->thread_args.regex = &view->regex;
2527 done:
2528 if (err)
2529 close_log_view(view);
2530 return err;
2533 static const struct got_error *
2534 show_log_view(struct tog_view *view)
2536 const struct got_error *err;
2537 struct tog_log_view_state *s = &view->state.log;
2539 if (s->thread == 0) { //NULL) {
2540 int errcode = pthread_create(&s->thread, NULL, log_thread,
2541 &s->thread_args);
2542 if (errcode)
2543 return got_error_set_errno(errcode, "pthread_create");
2544 if (s->thread_args.commits_needed > 0) {
2545 err = trigger_log_thread(view, 1);
2546 if (err)
2547 return err;
2551 return draw_commits(view);
2554 static const struct got_error *
2555 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2557 const struct got_error *err = NULL;
2558 struct tog_log_view_state *s = &view->state.log;
2559 struct tog_view *diff_view = NULL, *tree_view = NULL;
2560 struct tog_view *ref_view = NULL;
2561 struct commit_queue_entry *entry;
2562 int begin_x = 0, n, nscroll = view->nlines - 1;
2564 if (s->thread_args.load_all) {
2565 if (ch == KEY_BACKSPACE)
2566 s->thread_args.load_all = 0;
2567 else if (s->thread_args.log_complete) {
2568 s->thread_args.load_all = 0;
2569 log_scroll_down(view, s->commits.ncommits);
2570 s->selected = MIN(view->nlines - 2,
2571 s->commits.ncommits - 1);
2572 select_commit(s);
2574 return NULL;
2577 switch (ch) {
2578 case 'q':
2579 s->quit = 1;
2580 break;
2581 case '0':
2582 view->x = 0;
2583 break;
2584 case '$':
2585 view->x = MAX(view->maxx - view->ncols / 2, 0);
2586 break;
2587 case KEY_RIGHT:
2588 case 'l':
2589 if (view->x + view->ncols / 2 < view->maxx)
2590 view->x += 2; /* move two columns right */
2591 break;
2592 case KEY_LEFT:
2593 case 'h':
2594 view->x -= MIN(view->x, 2); /* move two columns back */
2595 break;
2596 case 'k':
2597 case KEY_UP:
2598 case '<':
2599 case ',':
2600 case CTRL('p'):
2601 if (s->first_displayed_entry == NULL)
2602 break;
2603 if (s->selected > 0)
2604 s->selected--;
2605 else
2606 log_scroll_up(s, 1);
2607 select_commit(s);
2608 break;
2609 case 'g':
2610 case KEY_HOME:
2611 s->selected = 0;
2612 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2613 select_commit(s);
2614 break;
2615 case CTRL('u'):
2616 case 'u':
2617 nscroll /= 2;
2618 /* FALL THROUGH */
2619 case KEY_PPAGE:
2620 case CTRL('b'):
2621 if (s->first_displayed_entry == NULL)
2622 break;
2623 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2624 s->selected = MAX(0, s->selected - nscroll - 1);
2625 else
2626 log_scroll_up(s, nscroll);
2627 select_commit(s);
2628 break;
2629 case 'j':
2630 case KEY_DOWN:
2631 case '>':
2632 case '.':
2633 case CTRL('n'):
2634 if (s->first_displayed_entry == NULL)
2635 break;
2636 if (s->selected < MIN(view->nlines - 2,
2637 s->commits.ncommits - 1))
2638 s->selected++;
2639 else {
2640 err = log_scroll_down(view, 1);
2641 if (err)
2642 break;
2644 select_commit(s);
2645 break;
2646 case 'G':
2647 case KEY_END: {
2648 /* We don't know yet how many commits, so we're forced to
2649 * traverse them all. */
2650 if (!s->thread_args.log_complete) {
2651 s->thread_args.load_all = 1;
2652 return trigger_log_thread(view, 0);
2655 s->selected = 0;
2656 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2657 for (n = 0; n < view->nlines - 1; n++) {
2658 if (entry == NULL)
2659 break;
2660 s->first_displayed_entry = entry;
2661 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2663 if (n > 0)
2664 s->selected = n - 1;
2665 select_commit(s);
2666 break;
2668 case CTRL('d'):
2669 case 'd':
2670 nscroll /= 2;
2671 /* FALL THROUGH */
2672 case KEY_NPAGE:
2673 case CTRL('f'): {
2674 struct commit_queue_entry *first;
2675 first = s->first_displayed_entry;
2676 if (first == NULL)
2677 break;
2678 err = log_scroll_down(view, nscroll);
2679 if (err)
2680 break;
2681 if (first == s->first_displayed_entry &&
2682 s->selected < MIN(view->nlines - 2,
2683 s->commits.ncommits - 1)) {
2684 /* can't scroll further down */
2685 s->selected += MIN(s->last_displayed_entry->idx -
2686 s->selected_entry->idx, nscroll + 1);
2688 select_commit(s);
2689 break;
2691 case KEY_RESIZE:
2692 if (s->selected > view->nlines - 2)
2693 s->selected = view->nlines - 2;
2694 if (s->selected > s->commits.ncommits - 1)
2695 s->selected = s->commits.ncommits - 1;
2696 select_commit(s);
2697 if (s->commits.ncommits < view->nlines - 1 &&
2698 !s->thread_args.log_complete) {
2699 s->thread_args.commits_needed += (view->nlines - 1) -
2700 s->commits.ncommits;
2701 err = trigger_log_thread(view, 1);
2703 break;
2704 case KEY_ENTER:
2705 case ' ':
2706 case '\r':
2707 if (s->selected_entry == NULL)
2708 break;
2709 if (view_is_parent_view(view))
2710 begin_x = view_split_begin_x(view->begin_x);
2711 err = open_diff_view_for_commit(&diff_view, begin_x,
2712 s->selected_entry->commit, s->selected_entry->id,
2713 view, s->repo);
2714 if (err)
2715 break;
2716 view->focussed = 0;
2717 diff_view->focussed = 1;
2718 if (view_is_parent_view(view)) {
2719 err = view_close_child(view);
2720 if (err)
2721 return err;
2722 err = view_set_child(view, diff_view);
2723 if (err)
2724 return err;
2725 view->focus_child = 1;
2726 } else
2727 *new_view = diff_view;
2728 break;
2729 case 't':
2730 if (s->selected_entry == NULL)
2731 break;
2732 if (view_is_parent_view(view))
2733 begin_x = view_split_begin_x(view->begin_x);
2734 err = browse_commit_tree(&tree_view, begin_x,
2735 s->selected_entry, s->in_repo_path, s->head_ref_name,
2736 s->repo);
2737 if (err)
2738 break;
2739 view->focussed = 0;
2740 tree_view->focussed = 1;
2741 if (view_is_parent_view(view)) {
2742 err = view_close_child(view);
2743 if (err)
2744 return err;
2745 err = view_set_child(view, tree_view);
2746 if (err)
2747 return err;
2748 view->focus_child = 1;
2749 } else
2750 *new_view = tree_view;
2751 break;
2752 case KEY_BACKSPACE:
2753 case CTRL('l'):
2754 case 'B':
2755 if (ch == KEY_BACKSPACE &&
2756 got_path_is_root_dir(s->in_repo_path))
2757 break;
2758 err = stop_log_thread(s);
2759 if (err)
2760 return err;
2761 if (ch == KEY_BACKSPACE) {
2762 char *parent_path;
2763 err = got_path_dirname(&parent_path, s->in_repo_path);
2764 if (err)
2765 return err;
2766 free(s->in_repo_path);
2767 s->in_repo_path = parent_path;
2768 s->thread_args.in_repo_path = s->in_repo_path;
2769 } else if (ch == CTRL('l')) {
2770 struct got_object_id *start_id;
2771 err = got_repo_match_object_id(&start_id, NULL,
2772 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2773 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2774 if (err)
2775 return err;
2776 free(s->start_id);
2777 s->start_id = start_id;
2778 s->thread_args.start_id = s->start_id;
2779 } else /* 'B' */
2780 s->log_branches = !s->log_branches;
2782 if (s->thread_args.pack_fds == NULL) {
2783 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2784 if (err)
2785 return err;
2787 err = got_repo_open(&s->thread_args.repo,
2788 got_repo_get_path(s->repo), NULL,
2789 s->thread_args.pack_fds);
2790 if (err)
2791 return err;
2792 tog_free_refs();
2793 err = tog_load_refs(s->repo, 0);
2794 if (err)
2795 return err;
2796 err = got_commit_graph_open(&s->thread_args.graph,
2797 s->in_repo_path, !s->log_branches);
2798 if (err)
2799 return err;
2800 err = got_commit_graph_iter_start(s->thread_args.graph,
2801 s->start_id, s->repo, NULL, NULL);
2802 if (err)
2803 return err;
2804 free_commits(&s->commits);
2805 s->first_displayed_entry = NULL;
2806 s->last_displayed_entry = NULL;
2807 s->selected_entry = NULL;
2808 s->selected = 0;
2809 s->thread_args.log_complete = 0;
2810 s->quit = 0;
2811 s->thread_args.commits_needed = view->nlines;
2812 s->matched_entry = NULL;
2813 s->search_entry = NULL;
2814 break;
2815 case 'r':
2816 if (view_is_parent_view(view))
2817 begin_x = view_split_begin_x(view->begin_x);
2818 ref_view = view_open(view->nlines, view->ncols,
2819 view->begin_y, begin_x, TOG_VIEW_REF);
2820 if (ref_view == NULL)
2821 return got_error_from_errno("view_open");
2822 err = open_ref_view(ref_view, s->repo);
2823 if (err) {
2824 view_close(ref_view);
2825 return err;
2827 view->focussed = 0;
2828 ref_view->focussed = 1;
2829 if (view_is_parent_view(view)) {
2830 err = view_close_child(view);
2831 if (err)
2832 return err;
2833 err = view_set_child(view, ref_view);
2834 if (err)
2835 return err;
2836 view->focus_child = 1;
2837 } else
2838 *new_view = ref_view;
2839 break;
2840 default:
2841 break;
2844 return err;
2847 static const struct got_error *
2848 apply_unveil(const char *repo_path, const char *worktree_path)
2850 const struct got_error *error;
2852 #ifdef PROFILE
2853 if (unveil("gmon.out", "rwc") != 0)
2854 return got_error_from_errno2("unveil", "gmon.out");
2855 #endif
2856 if (repo_path && unveil(repo_path, "r") != 0)
2857 return got_error_from_errno2("unveil", repo_path);
2859 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2860 return got_error_from_errno2("unveil", worktree_path);
2862 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2863 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2865 error = got_privsep_unveil_exec_helpers();
2866 if (error != NULL)
2867 return error;
2869 if (unveil(NULL, NULL) != 0)
2870 return got_error_from_errno("unveil");
2872 return NULL;
2875 static void
2876 init_curses(void)
2879 * Override default signal handlers before starting ncurses.
2880 * This should prevent ncurses from installing its own
2881 * broken cleanup() signal handler.
2883 signal(SIGWINCH, tog_sigwinch);
2884 signal(SIGPIPE, tog_sigpipe);
2885 signal(SIGCONT, tog_sigcont);
2886 signal(SIGINT, tog_sigint);
2887 signal(SIGTERM, tog_sigterm);
2889 initscr();
2890 cbreak();
2891 halfdelay(1); /* Do fast refresh while initial view is loading. */
2892 noecho();
2893 nonl();
2894 intrflush(stdscr, FALSE);
2895 keypad(stdscr, TRUE);
2896 curs_set(0);
2897 if (getenv("TOG_COLORS") != NULL) {
2898 start_color();
2899 use_default_colors();
2903 static const struct got_error *
2904 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2905 struct got_repository *repo, struct got_worktree *worktree)
2907 const struct got_error *err = NULL;
2909 if (argc == 0) {
2910 *in_repo_path = strdup("/");
2911 if (*in_repo_path == NULL)
2912 return got_error_from_errno("strdup");
2913 return NULL;
2916 if (worktree) {
2917 const char *prefix = got_worktree_get_path_prefix(worktree);
2918 char *p;
2920 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2921 if (err)
2922 return err;
2923 if (asprintf(in_repo_path, "%s%s%s", prefix,
2924 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2925 p) == -1) {
2926 err = got_error_from_errno("asprintf");
2927 *in_repo_path = NULL;
2929 free(p);
2930 } else
2931 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2933 return err;
2936 static const struct got_error *
2937 cmd_log(int argc, char *argv[])
2939 const struct got_error *error;
2940 struct got_repository *repo = NULL;
2941 struct got_worktree *worktree = NULL;
2942 struct got_object_id *start_id = NULL;
2943 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2944 char *start_commit = NULL, *label = NULL;
2945 struct got_reference *ref = NULL;
2946 const char *head_ref_name = NULL;
2947 int ch, log_branches = 0;
2948 struct tog_view *view;
2949 int *pack_fds = NULL;
2951 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2952 switch (ch) {
2953 case 'b':
2954 log_branches = 1;
2955 break;
2956 case 'c':
2957 start_commit = optarg;
2958 break;
2959 case 'r':
2960 repo_path = realpath(optarg, NULL);
2961 if (repo_path == NULL)
2962 return got_error_from_errno2("realpath",
2963 optarg);
2964 break;
2965 default:
2966 usage_log();
2967 /* NOTREACHED */
2971 argc -= optind;
2972 argv += optind;
2974 if (argc > 1)
2975 usage_log();
2977 error = got_repo_pack_fds_open(&pack_fds);
2978 if (error != NULL)
2979 goto done;
2981 if (repo_path == NULL) {
2982 cwd = getcwd(NULL, 0);
2983 if (cwd == NULL)
2984 return got_error_from_errno("getcwd");
2985 error = got_worktree_open(&worktree, cwd);
2986 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2987 goto done;
2988 if (worktree)
2989 repo_path =
2990 strdup(got_worktree_get_repo_path(worktree));
2991 else
2992 repo_path = strdup(cwd);
2993 if (repo_path == NULL) {
2994 error = got_error_from_errno("strdup");
2995 goto done;
2999 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3000 if (error != NULL)
3001 goto done;
3003 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3004 repo, worktree);
3005 if (error)
3006 goto done;
3008 init_curses();
3010 error = apply_unveil(got_repo_get_path(repo),
3011 worktree ? got_worktree_get_root_path(worktree) : NULL);
3012 if (error)
3013 goto done;
3015 /* already loaded by tog_log_with_path()? */
3016 if (TAILQ_EMPTY(&tog_refs)) {
3017 error = tog_load_refs(repo, 0);
3018 if (error)
3019 goto done;
3022 if (start_commit == NULL) {
3023 error = got_repo_match_object_id(&start_id, &label,
3024 worktree ? got_worktree_get_head_ref_name(worktree) :
3025 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3026 if (error)
3027 goto done;
3028 head_ref_name = label;
3029 } else {
3030 error = got_ref_open(&ref, repo, start_commit, 0);
3031 if (error == NULL)
3032 head_ref_name = got_ref_get_name(ref);
3033 else if (error->code != GOT_ERR_NOT_REF)
3034 goto done;
3035 error = got_repo_match_object_id(&start_id, NULL,
3036 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3037 if (error)
3038 goto done;
3041 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3042 if (view == NULL) {
3043 error = got_error_from_errno("view_open");
3044 goto done;
3046 error = open_log_view(view, start_id, repo, head_ref_name,
3047 in_repo_path, log_branches);
3048 if (error)
3049 goto done;
3050 if (worktree) {
3051 /* Release work tree lock. */
3052 got_worktree_close(worktree);
3053 worktree = NULL;
3055 error = view_loop(view);
3056 done:
3057 free(in_repo_path);
3058 free(repo_path);
3059 free(cwd);
3060 free(start_id);
3061 free(label);
3062 if (ref)
3063 got_ref_close(ref);
3064 if (repo) {
3065 const struct got_error *close_err = got_repo_close(repo);
3066 if (error == NULL)
3067 error = close_err;
3069 if (worktree)
3070 got_worktree_close(worktree);
3071 if (pack_fds) {
3072 const struct got_error *pack_err =
3073 got_repo_pack_fds_close(pack_fds);
3074 if (error == NULL)
3075 error = pack_err;
3077 tog_free_refs();
3078 return error;
3081 __dead static void
3082 usage_diff(void)
3084 endwin();
3085 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3086 "[-w] object1 object2\n", getprogname());
3087 exit(1);
3090 static int
3091 match_line(const char *line, regex_t *regex, size_t nmatch,
3092 regmatch_t *regmatch)
3094 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3097 struct tog_color *
3098 match_color(struct tog_colors *colors, const char *line)
3100 struct tog_color *tc = NULL;
3102 STAILQ_FOREACH(tc, colors, entry) {
3103 if (match_line(line, &tc->regex, 0, NULL))
3104 return tc;
3107 return NULL;
3110 static const struct got_error *
3111 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3112 WINDOW *window, int skipcol, regmatch_t *regmatch)
3114 const struct got_error *err = NULL;
3115 char *exstr = NULL;
3116 wchar_t *wline = NULL;
3117 int rme, rms, n, width, scrollx;
3118 int width0 = 0, width1 = 0, width2 = 0;
3119 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3121 *wtotal = 0;
3123 rms = regmatch->rm_so;
3124 rme = regmatch->rm_eo;
3126 err = expand_tab(&exstr, line);
3127 if (err)
3128 return err;
3130 /* Split the line into 3 segments, according to match offsets. */
3131 seg0 = strndup(exstr, rms);
3132 if (seg0 == NULL) {
3133 err = got_error_from_errno("strndup");
3134 goto done;
3136 seg1 = strndup(exstr + rms, rme - rms);
3137 if (seg1 == NULL) {
3138 err = got_error_from_errno("strndup");
3139 goto done;
3141 seg2 = strdup(exstr + rme);
3142 if (seg2 == NULL) {
3143 err = got_error_from_errno("strndup");
3144 goto done;
3147 /* draw up to matched token if we haven't scrolled past it */
3148 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3149 col_tab_align, 1);
3150 if (err)
3151 goto done;
3152 n = MAX(width0 - skipcol, 0);
3153 if (n) {
3154 free(wline);
3155 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3156 wlimit, col_tab_align, 1);
3157 if (err)
3158 goto done;
3159 waddwstr(window, &wline[scrollx]);
3160 wlimit -= width;
3161 *wtotal += width;
3164 if (wlimit > 0) {
3165 int i = 0, w = 0;
3166 size_t wlen;
3168 free(wline);
3169 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3170 col_tab_align, 1);
3171 if (err)
3172 goto done;
3173 wlen = wcslen(wline);
3174 while (i < wlen) {
3175 width = wcwidth(wline[i]);
3176 if (width == -1) {
3177 /* should not happen, tabs are expanded */
3178 err = got_error(GOT_ERR_RANGE);
3179 goto done;
3181 if (width0 + w + width > skipcol)
3182 break;
3183 w += width;
3184 i++;
3186 /* draw (visible part of) matched token (if scrolled into it) */
3187 if (width1 - w > 0) {
3188 wattron(window, A_STANDOUT);
3189 waddwstr(window, &wline[i]);
3190 wattroff(window, A_STANDOUT);
3191 wlimit -= (width1 - w);
3192 *wtotal += (width1 - w);
3196 if (wlimit > 0) { /* draw rest of line */
3197 free(wline);
3198 if (skipcol > width0 + width1) {
3199 err = format_line(&wline, &width2, &scrollx, seg2,
3200 skipcol - (width0 + width1), wlimit,
3201 col_tab_align, 1);
3202 if (err)
3203 goto done;
3204 waddwstr(window, &wline[scrollx]);
3205 } else {
3206 err = format_line(&wline, &width2, NULL, seg2, 0,
3207 wlimit, col_tab_align, 1);
3208 if (err)
3209 goto done;
3210 waddwstr(window, wline);
3212 *wtotal += width2;
3214 done:
3215 free(wline);
3216 free(exstr);
3217 free(seg0);
3218 free(seg1);
3219 free(seg2);
3220 return err;
3223 static const struct got_error *
3224 draw_file(struct tog_view *view, const char *header)
3226 struct tog_diff_view_state *s = &view->state.diff;
3227 regmatch_t *regmatch = &view->regmatch;
3228 const struct got_error *err;
3229 int nprinted = 0;
3230 char *line;
3231 size_t linesize = 0;
3232 ssize_t linelen;
3233 struct tog_color *tc;
3234 wchar_t *wline;
3235 int width;
3236 int max_lines = view->nlines;
3237 int nlines = s->nlines;
3238 off_t line_offset;
3240 line_offset = s->line_offsets[s->first_displayed_line - 1];
3241 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3242 return got_error_from_errno("fseek");
3244 werase(view->window);
3246 if (header) {
3247 if (asprintf(&line, "[%d/%d] %s",
3248 s->first_displayed_line - 1 + s->selected_line, nlines,
3249 header) == -1)
3250 return got_error_from_errno("asprintf");
3251 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3252 0, 0);
3253 free(line);
3254 if (err)
3255 return err;
3257 if (view_needs_focus_indication(view))
3258 wstandout(view->window);
3259 waddwstr(view->window, wline);
3260 free(wline);
3261 wline = NULL;
3262 if (view_needs_focus_indication(view))
3263 wstandend(view->window);
3264 if (width <= view->ncols - 1)
3265 waddch(view->window, '\n');
3267 if (max_lines <= 1)
3268 return NULL;
3269 max_lines--;
3272 s->eof = 0;
3273 view->maxx = 0;
3274 line = NULL;
3275 while (max_lines > 0 && nprinted < max_lines) {
3276 linelen = getline(&line, &linesize, s->f);
3277 if (linelen == -1) {
3278 if (feof(s->f)) {
3279 s->eof = 1;
3280 break;
3282 free(line);
3283 return got_ferror(s->f, GOT_ERR_IO);
3286 /* Set view->maxx based on full line length. */
3287 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3288 view->x ? 1 : 0);
3289 if (err) {
3290 free(line);
3291 return err;
3293 view->maxx = MAX(view->maxx, width);
3294 free(wline);
3295 wline = NULL;
3297 tc = match_color(&s->colors, line);
3298 if (tc)
3299 wattr_on(view->window,
3300 COLOR_PAIR(tc->colorpair), NULL);
3301 if (s->first_displayed_line + nprinted == s->matched_line &&
3302 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3303 err = add_matched_line(&width, line, view->ncols, 0,
3304 view->window, view->x, regmatch);
3305 if (err) {
3306 free(line);
3307 return err;
3309 } else {
3310 int skip;
3311 err = format_line(&wline, &width, &skip, line,
3312 view->x, view->ncols, 0, view->x ? 1 : 0);
3313 if (err) {
3314 free(line);
3315 return err;
3317 waddwstr(view->window, &wline[skip]);
3318 free(wline);
3319 wline = NULL;
3321 if (tc)
3322 wattr_off(view->window,
3323 COLOR_PAIR(tc->colorpair), NULL);
3324 if (width <= view->ncols - 1)
3325 waddch(view->window, '\n');
3326 nprinted++;
3328 free(line);
3329 if (nprinted >= 1)
3330 s->last_displayed_line = s->first_displayed_line +
3331 (nprinted - 1);
3332 else
3333 s->last_displayed_line = s->first_displayed_line;
3335 view_vborder(view);
3337 if (s->eof) {
3338 while (nprinted < view->nlines) {
3339 waddch(view->window, '\n');
3340 nprinted++;
3343 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3344 view->ncols, 0, 0);
3345 if (err) {
3346 return err;
3349 wstandout(view->window);
3350 waddwstr(view->window, wline);
3351 free(wline);
3352 wline = NULL;
3353 wstandend(view->window);
3356 return NULL;
3359 static char *
3360 get_datestr(time_t *time, char *datebuf)
3362 struct tm mytm, *tm;
3363 char *p, *s;
3365 tm = gmtime_r(time, &mytm);
3366 if (tm == NULL)
3367 return NULL;
3368 s = asctime_r(tm, datebuf);
3369 if (s == NULL)
3370 return NULL;
3371 p = strchr(s, '\n');
3372 if (p)
3373 *p = '\0';
3374 return s;
3377 static const struct got_error *
3378 get_changed_paths(struct got_pathlist_head *paths,
3379 struct got_commit_object *commit, struct got_repository *repo)
3381 const struct got_error *err = NULL;
3382 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3383 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3384 struct got_object_qid *qid;
3386 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3387 if (qid != NULL) {
3388 struct got_commit_object *pcommit;
3389 err = got_object_open_as_commit(&pcommit, repo,
3390 &qid->id);
3391 if (err)
3392 return err;
3394 tree_id1 = got_object_id_dup(
3395 got_object_commit_get_tree_id(pcommit));
3396 if (tree_id1 == NULL) {
3397 got_object_commit_close(pcommit);
3398 return got_error_from_errno("got_object_id_dup");
3400 got_object_commit_close(pcommit);
3404 if (tree_id1) {
3405 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3406 if (err)
3407 goto done;
3410 tree_id2 = got_object_commit_get_tree_id(commit);
3411 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3412 if (err)
3413 goto done;
3415 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3416 got_diff_tree_collect_changed_paths, paths, 0);
3417 done:
3418 if (tree1)
3419 got_object_tree_close(tree1);
3420 if (tree2)
3421 got_object_tree_close(tree2);
3422 free(tree_id1);
3423 return err;
3426 static const struct got_error *
3427 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3429 off_t *p;
3431 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3432 if (p == NULL)
3433 return got_error_from_errno("reallocarray");
3434 *line_offsets = p;
3435 (*line_offsets)[*nlines] = off;
3436 (*nlines)++;
3437 return NULL;
3440 static const struct got_error *
3441 write_commit_info(off_t **line_offsets, size_t *nlines,
3442 struct got_object_id *commit_id, struct got_reflist_head *refs,
3443 struct got_repository *repo, FILE *outfile)
3445 const struct got_error *err = NULL;
3446 char datebuf[26], *datestr;
3447 struct got_commit_object *commit;
3448 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3449 time_t committer_time;
3450 const char *author, *committer;
3451 char *refs_str = NULL;
3452 struct got_pathlist_head changed_paths;
3453 struct got_pathlist_entry *pe;
3454 off_t outoff = 0;
3455 int n;
3457 TAILQ_INIT(&changed_paths);
3459 if (refs) {
3460 err = build_refs_str(&refs_str, refs, commit_id, repo);
3461 if (err)
3462 return err;
3465 err = got_object_open_as_commit(&commit, repo, commit_id);
3466 if (err)
3467 return err;
3469 err = got_object_id_str(&id_str, commit_id);
3470 if (err) {
3471 err = got_error_from_errno("got_object_id_str");
3472 goto done;
3475 err = add_line_offset(line_offsets, nlines, 0);
3476 if (err)
3477 goto done;
3479 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3480 refs_str ? refs_str : "", refs_str ? ")" : "");
3481 if (n < 0) {
3482 err = got_error_from_errno("fprintf");
3483 goto done;
3485 outoff += n;
3486 err = add_line_offset(line_offsets, nlines, outoff);
3487 if (err)
3488 goto done;
3490 n = fprintf(outfile, "from: %s\n",
3491 got_object_commit_get_author(commit));
3492 if (n < 0) {
3493 err = got_error_from_errno("fprintf");
3494 goto done;
3496 outoff += n;
3497 err = add_line_offset(line_offsets, nlines, outoff);
3498 if (err)
3499 goto done;
3501 committer_time = got_object_commit_get_committer_time(commit);
3502 datestr = get_datestr(&committer_time, datebuf);
3503 if (datestr) {
3504 n = fprintf(outfile, "date: %s UTC\n", datestr);
3505 if (n < 0) {
3506 err = got_error_from_errno("fprintf");
3507 goto done;
3509 outoff += n;
3510 err = add_line_offset(line_offsets, nlines, outoff);
3511 if (err)
3512 goto done;
3514 author = got_object_commit_get_author(commit);
3515 committer = got_object_commit_get_committer(commit);
3516 if (strcmp(author, committer) != 0) {
3517 n = fprintf(outfile, "via: %s\n", committer);
3518 if (n < 0) {
3519 err = got_error_from_errno("fprintf");
3520 goto done;
3522 outoff += n;
3523 err = add_line_offset(line_offsets, nlines, outoff);
3524 if (err)
3525 goto done;
3527 if (got_object_commit_get_nparents(commit) > 1) {
3528 const struct got_object_id_queue *parent_ids;
3529 struct got_object_qid *qid;
3530 int pn = 1;
3531 parent_ids = got_object_commit_get_parent_ids(commit);
3532 STAILQ_FOREACH(qid, parent_ids, entry) {
3533 err = got_object_id_str(&id_str, &qid->id);
3534 if (err)
3535 goto done;
3536 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3537 if (n < 0) {
3538 err = got_error_from_errno("fprintf");
3539 goto done;
3541 outoff += n;
3542 err = add_line_offset(line_offsets, nlines, outoff);
3543 if (err)
3544 goto done;
3545 free(id_str);
3546 id_str = NULL;
3550 err = got_object_commit_get_logmsg(&logmsg, commit);
3551 if (err)
3552 goto done;
3553 s = logmsg;
3554 while ((line = strsep(&s, "\n")) != NULL) {
3555 n = fprintf(outfile, "%s\n", line);
3556 if (n < 0) {
3557 err = got_error_from_errno("fprintf");
3558 goto done;
3560 outoff += n;
3561 err = add_line_offset(line_offsets, nlines, outoff);
3562 if (err)
3563 goto done;
3566 err = get_changed_paths(&changed_paths, commit, repo);
3567 if (err)
3568 goto done;
3569 TAILQ_FOREACH(pe, &changed_paths, entry) {
3570 struct got_diff_changed_path *cp = pe->data;
3571 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
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;
3580 free((char *)pe->path);
3581 free(pe->data);
3584 fputc('\n', outfile);
3585 outoff++;
3586 err = add_line_offset(line_offsets, nlines, outoff);
3587 done:
3588 got_pathlist_free(&changed_paths);
3589 free(id_str);
3590 free(logmsg);
3591 free(refs_str);
3592 got_object_commit_close(commit);
3593 if (err) {
3594 free(*line_offsets);
3595 *line_offsets = NULL;
3596 *nlines = 0;
3598 return err;
3601 static const struct got_error *
3602 create_diff(struct tog_diff_view_state *s)
3604 const struct got_error *err = NULL;
3605 FILE *f = NULL;
3606 int obj_type;
3608 free(s->line_offsets);
3609 s->line_offsets = malloc(sizeof(off_t));
3610 if (s->line_offsets == NULL)
3611 return got_error_from_errno("malloc");
3612 s->nlines = 0;
3614 f = got_opentemp();
3615 if (f == NULL) {
3616 err = got_error_from_errno("got_opentemp");
3617 goto done;
3619 if (s->f && fclose(s->f) == EOF) {
3620 err = got_error_from_errno("fclose");
3621 goto done;
3623 s->f = f;
3625 if (s->id1)
3626 err = got_object_get_type(&obj_type, s->repo, s->id1);
3627 else
3628 err = got_object_get_type(&obj_type, s->repo, s->id2);
3629 if (err)
3630 goto done;
3632 switch (obj_type) {
3633 case GOT_OBJ_TYPE_BLOB:
3634 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3635 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3636 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3637 s->repo, s->f);
3638 break;
3639 case GOT_OBJ_TYPE_TREE:
3640 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3641 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3642 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3643 break;
3644 case GOT_OBJ_TYPE_COMMIT: {
3645 const struct got_object_id_queue *parent_ids;
3646 struct got_object_qid *pid;
3647 struct got_commit_object *commit2;
3648 struct got_reflist_head *refs;
3650 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3651 if (err)
3652 goto done;
3653 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3654 /* Show commit info if we're diffing to a parent/root commit. */
3655 if (s->id1 == NULL) {
3656 err = write_commit_info(&s->line_offsets, &s->nlines,
3657 s->id2, refs, s->repo, s->f);
3658 if (err)
3659 goto done;
3660 } else {
3661 parent_ids = got_object_commit_get_parent_ids(commit2);
3662 STAILQ_FOREACH(pid, parent_ids, entry) {
3663 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3664 err = write_commit_info(
3665 &s->line_offsets, &s->nlines,
3666 s->id2, refs, s->repo, s->f);
3667 if (err)
3668 goto done;
3669 break;
3673 got_object_commit_close(commit2);
3675 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3676 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3677 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3678 break;
3680 default:
3681 err = got_error(GOT_ERR_OBJ_TYPE);
3682 break;
3684 if (err)
3685 goto done;
3686 done:
3687 if (s->f && fflush(s->f) != 0 && err == NULL)
3688 err = got_error_from_errno("fflush");
3689 return err;
3692 static void
3693 diff_view_indicate_progress(struct tog_view *view)
3695 mvwaddstr(view->window, 0, 0, "diffing...");
3696 update_panels();
3697 doupdate();
3700 static const struct got_error *
3701 search_start_diff_view(struct tog_view *view)
3703 struct tog_diff_view_state *s = &view->state.diff;
3705 s->matched_line = 0;
3706 return NULL;
3709 static const struct got_error *
3710 search_next_diff_view(struct tog_view *view)
3712 struct tog_diff_view_state *s = &view->state.diff;
3713 const struct got_error *err = NULL;
3714 int lineno;
3715 char *line = NULL;
3716 size_t linesize = 0;
3717 ssize_t linelen;
3719 if (!view->searching) {
3720 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3721 return NULL;
3724 if (s->matched_line) {
3725 if (view->searching == TOG_SEARCH_FORWARD)
3726 lineno = s->matched_line + 1;
3727 else
3728 lineno = s->matched_line - 1;
3729 } else
3730 lineno = s->first_displayed_line;
3732 while (1) {
3733 off_t offset;
3735 if (lineno <= 0 || lineno > s->nlines) {
3736 if (s->matched_line == 0) {
3737 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3738 break;
3741 if (view->searching == TOG_SEARCH_FORWARD)
3742 lineno = 1;
3743 else
3744 lineno = s->nlines;
3747 offset = s->line_offsets[lineno - 1];
3748 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3749 free(line);
3750 return got_error_from_errno("fseeko");
3752 linelen = getline(&line, &linesize, s->f);
3753 if (linelen != -1) {
3754 char *exstr;
3755 err = expand_tab(&exstr, line);
3756 if (err)
3757 break;
3758 if (match_line(exstr, &view->regex, 1,
3759 &view->regmatch)) {
3760 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3761 s->matched_line = lineno;
3762 free(exstr);
3763 break;
3765 free(exstr);
3767 if (view->searching == TOG_SEARCH_FORWARD)
3768 lineno++;
3769 else
3770 lineno--;
3772 free(line);
3774 if (s->matched_line) {
3775 s->first_displayed_line = s->matched_line;
3776 s->selected_line = 1;
3779 return err;
3782 static const struct got_error *
3783 close_diff_view(struct tog_view *view)
3785 const struct got_error *err = NULL;
3786 struct tog_diff_view_state *s = &view->state.diff;
3788 free(s->id1);
3789 s->id1 = NULL;
3790 free(s->id2);
3791 s->id2 = NULL;
3792 if (s->f && fclose(s->f) == EOF)
3793 err = got_error_from_errno("fclose");
3794 s->f = NULL;
3795 if (s->f1 && fclose(s->f1) == EOF)
3796 err = got_error_from_errno("fclose");
3797 s->f1 = NULL;
3798 if (s->f2 && fclose(s->f2) == EOF)
3799 err = got_error_from_errno("fclose");
3800 s->f2 = NULL;
3801 free_colors(&s->colors);
3802 free(s->line_offsets);
3803 s->line_offsets = NULL;
3804 s->nlines = 0;
3805 return err;
3808 static const struct got_error *
3809 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3810 struct got_object_id *id2, const char *label1, const char *label2,
3811 int diff_context, int ignore_whitespace, int force_text_diff,
3812 struct tog_view *log_view, struct got_repository *repo)
3814 const struct got_error *err;
3815 struct tog_diff_view_state *s = &view->state.diff;
3817 memset(s, 0, sizeof(*s));
3819 if (id1 != NULL && id2 != NULL) {
3820 int type1, type2;
3821 err = got_object_get_type(&type1, repo, id1);
3822 if (err)
3823 return err;
3824 err = got_object_get_type(&type2, repo, id2);
3825 if (err)
3826 return err;
3828 if (type1 != type2)
3829 return got_error(GOT_ERR_OBJ_TYPE);
3831 s->first_displayed_line = 1;
3832 s->last_displayed_line = view->nlines;
3833 s->selected_line = 1;
3834 s->repo = repo;
3835 s->id1 = id1;
3836 s->id2 = id2;
3837 s->label1 = label1;
3838 s->label2 = label2;
3840 if (id1) {
3841 s->id1 = got_object_id_dup(id1);
3842 if (s->id1 == NULL)
3843 return got_error_from_errno("got_object_id_dup");
3844 } else
3845 s->id1 = NULL;
3847 s->id2 = got_object_id_dup(id2);
3848 if (s->id2 == NULL) {
3849 err = got_error_from_errno("got_object_id_dup");
3850 goto done;
3853 s->f1 = got_opentemp();
3854 if (s->f1 == NULL) {
3855 err = got_error_from_errno("got_opentemp");
3856 goto done;
3859 s->f2 = got_opentemp();
3860 if (s->f2 == NULL) {
3861 err = got_error_from_errno("got_opentemp");
3862 goto done;
3865 s->first_displayed_line = 1;
3866 s->last_displayed_line = view->nlines;
3867 s->diff_context = diff_context;
3868 s->ignore_whitespace = ignore_whitespace;
3869 s->force_text_diff = force_text_diff;
3870 s->log_view = log_view;
3871 s->repo = repo;
3873 STAILQ_INIT(&s->colors);
3874 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3875 err = add_color(&s->colors,
3876 "^-", TOG_COLOR_DIFF_MINUS,
3877 get_color_value("TOG_COLOR_DIFF_MINUS"));
3878 if (err)
3879 goto done;
3880 err = add_color(&s->colors, "^\\+",
3881 TOG_COLOR_DIFF_PLUS,
3882 get_color_value("TOG_COLOR_DIFF_PLUS"));
3883 if (err)
3884 goto done;
3885 err = add_color(&s->colors,
3886 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3887 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3888 if (err)
3889 goto done;
3891 err = add_color(&s->colors,
3892 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3893 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3894 get_color_value("TOG_COLOR_DIFF_META"));
3895 if (err)
3896 goto done;
3898 err = add_color(&s->colors,
3899 "^(from|via): ", TOG_COLOR_AUTHOR,
3900 get_color_value("TOG_COLOR_AUTHOR"));
3901 if (err)
3902 goto done;
3904 err = add_color(&s->colors,
3905 "^date: ", TOG_COLOR_DATE,
3906 get_color_value("TOG_COLOR_DATE"));
3907 if (err)
3908 goto done;
3911 if (log_view && view_is_splitscreen(view))
3912 show_log_view(log_view); /* draw vborder */
3913 diff_view_indicate_progress(view);
3915 err = create_diff(s);
3917 view->show = show_diff_view;
3918 view->input = input_diff_view;
3919 view->close = close_diff_view;
3920 view->search_start = search_start_diff_view;
3921 view->search_next = search_next_diff_view;
3922 done:
3923 if (err)
3924 close_diff_view(view);
3925 return err;
3928 static const struct got_error *
3929 show_diff_view(struct tog_view *view)
3931 const struct got_error *err;
3932 struct tog_diff_view_state *s = &view->state.diff;
3933 char *id_str1 = NULL, *id_str2, *header;
3934 const char *label1, *label2;
3936 if (s->id1) {
3937 err = got_object_id_str(&id_str1, s->id1);
3938 if (err)
3939 return err;
3940 label1 = s->label1 ? : id_str1;
3941 } else
3942 label1 = "/dev/null";
3944 err = got_object_id_str(&id_str2, s->id2);
3945 if (err)
3946 return err;
3947 label2 = s->label2 ? : id_str2;
3949 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3950 err = got_error_from_errno("asprintf");
3951 free(id_str1);
3952 free(id_str2);
3953 return err;
3955 free(id_str1);
3956 free(id_str2);
3958 err = draw_file(view, header);
3959 free(header);
3960 return err;
3963 static const struct got_error *
3964 set_selected_commit(struct tog_diff_view_state *s,
3965 struct commit_queue_entry *entry)
3967 const struct got_error *err;
3968 const struct got_object_id_queue *parent_ids;
3969 struct got_commit_object *selected_commit;
3970 struct got_object_qid *pid;
3972 free(s->id2);
3973 s->id2 = got_object_id_dup(entry->id);
3974 if (s->id2 == NULL)
3975 return got_error_from_errno("got_object_id_dup");
3977 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3978 if (err)
3979 return err;
3980 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3981 free(s->id1);
3982 pid = STAILQ_FIRST(parent_ids);
3983 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3984 got_object_commit_close(selected_commit);
3985 return NULL;
3988 static const struct got_error *
3989 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3991 const struct got_error *err = NULL;
3992 struct tog_diff_view_state *s = &view->state.diff;
3993 struct tog_log_view_state *ls;
3994 struct commit_queue_entry *old_selected_entry;
3995 char *line = NULL;
3996 size_t linesize = 0;
3997 ssize_t linelen;
3998 int i, nscroll = view->nlines - 1;
4000 switch (ch) {
4001 case '0':
4002 view->x = 0;
4003 break;
4004 case '$':
4005 view->x = MAX(view->maxx - view->ncols / 3, 0);
4006 break;
4007 case KEY_RIGHT:
4008 case 'l':
4009 if (view->x + view->ncols / 3 < view->maxx)
4010 view->x += 2; /* move two columns right */
4011 break;
4012 case KEY_LEFT:
4013 case 'h':
4014 view->x -= MIN(view->x, 2); /* move two columns back */
4015 break;
4016 case 'a':
4017 case 'w':
4018 if (ch == 'a')
4019 s->force_text_diff = !s->force_text_diff;
4020 if (ch == 'w')
4021 s->ignore_whitespace = !s->ignore_whitespace;
4022 wclear(view->window);
4023 s->first_displayed_line = 1;
4024 s->last_displayed_line = view->nlines;
4025 s->matched_line = 0;
4026 diff_view_indicate_progress(view);
4027 err = create_diff(s);
4028 break;
4029 case 'g':
4030 case KEY_HOME:
4031 s->first_displayed_line = 1;
4032 break;
4033 case 'G':
4034 case KEY_END:
4035 if (s->eof)
4036 break;
4038 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4039 s->eof = 1;
4040 break;
4041 case 'k':
4042 case KEY_UP:
4043 case CTRL('p'):
4044 if (s->first_displayed_line > 1)
4045 s->first_displayed_line--;
4046 break;
4047 case CTRL('u'):
4048 case 'u':
4049 nscroll /= 2;
4050 /* FALL THROUGH */
4051 case KEY_PPAGE:
4052 case CTRL('b'):
4053 if (s->first_displayed_line == 1)
4054 break;
4055 i = 0;
4056 while (i++ < nscroll && s->first_displayed_line > 1)
4057 s->first_displayed_line--;
4058 break;
4059 case 'j':
4060 case KEY_DOWN:
4061 case CTRL('n'):
4062 if (!s->eof)
4063 s->first_displayed_line++;
4064 break;
4065 case CTRL('d'):
4066 case 'd':
4067 nscroll /= 2;
4068 /* FALL THROUGH */
4069 case KEY_NPAGE:
4070 case CTRL('f'):
4071 case ' ':
4072 if (s->eof)
4073 break;
4074 i = 0;
4075 while (!s->eof && i++ < nscroll) {
4076 linelen = getline(&line, &linesize, s->f);
4077 s->first_displayed_line++;
4078 if (linelen == -1) {
4079 if (feof(s->f)) {
4080 s->eof = 1;
4081 } else
4082 err = got_ferror(s->f, GOT_ERR_IO);
4083 break;
4086 free(line);
4087 break;
4088 case '[':
4089 if (s->diff_context > 0) {
4090 s->diff_context--;
4091 s->matched_line = 0;
4092 diff_view_indicate_progress(view);
4093 err = create_diff(s);
4094 if (s->first_displayed_line + view->nlines - 1 >
4095 s->nlines) {
4096 s->first_displayed_line = 1;
4097 s->last_displayed_line = view->nlines;
4100 break;
4101 case ']':
4102 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4103 s->diff_context++;
4104 s->matched_line = 0;
4105 diff_view_indicate_progress(view);
4106 err = create_diff(s);
4108 break;
4109 case '<':
4110 case ',':
4111 if (s->log_view == NULL)
4112 break;
4113 ls = &s->log_view->state.log;
4114 old_selected_entry = ls->selected_entry;
4116 err = input_log_view(NULL, s->log_view, KEY_UP);
4117 if (err)
4118 break;
4120 if (old_selected_entry == ls->selected_entry)
4121 break;
4123 err = set_selected_commit(s, ls->selected_entry);
4124 if (err)
4125 break;
4127 s->first_displayed_line = 1;
4128 s->last_displayed_line = view->nlines;
4129 s->matched_line = 0;
4130 view->x = 0;
4132 diff_view_indicate_progress(view);
4133 err = create_diff(s);
4134 break;
4135 case '>':
4136 case '.':
4137 if (s->log_view == NULL)
4138 break;
4139 ls = &s->log_view->state.log;
4140 old_selected_entry = ls->selected_entry;
4142 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4143 if (err)
4144 break;
4146 if (old_selected_entry == ls->selected_entry)
4147 break;
4149 err = set_selected_commit(s, ls->selected_entry);
4150 if (err)
4151 break;
4153 s->first_displayed_line = 1;
4154 s->last_displayed_line = view->nlines;
4155 s->matched_line = 0;
4156 view->x = 0;
4158 diff_view_indicate_progress(view);
4159 err = create_diff(s);
4160 break;
4161 default:
4162 break;
4165 return err;
4168 static const struct got_error *
4169 cmd_diff(int argc, char *argv[])
4171 const struct got_error *error = NULL;
4172 struct got_repository *repo = NULL;
4173 struct got_worktree *worktree = NULL;
4174 struct got_object_id *id1 = NULL, *id2 = NULL;
4175 char *repo_path = NULL, *cwd = NULL;
4176 char *id_str1 = NULL, *id_str2 = NULL;
4177 char *label1 = NULL, *label2 = NULL;
4178 int diff_context = 3, ignore_whitespace = 0;
4179 int ch, force_text_diff = 0;
4180 const char *errstr;
4181 struct tog_view *view;
4182 int *pack_fds = NULL;
4184 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4185 switch (ch) {
4186 case 'a':
4187 force_text_diff = 1;
4188 break;
4189 case 'C':
4190 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4191 &errstr);
4192 if (errstr != NULL)
4193 errx(1, "number of context lines is %s: %s",
4194 errstr, errstr);
4195 break;
4196 case 'r':
4197 repo_path = realpath(optarg, NULL);
4198 if (repo_path == NULL)
4199 return got_error_from_errno2("realpath",
4200 optarg);
4201 got_path_strip_trailing_slashes(repo_path);
4202 break;
4203 case 'w':
4204 ignore_whitespace = 1;
4205 break;
4206 default:
4207 usage_diff();
4208 /* NOTREACHED */
4212 argc -= optind;
4213 argv += optind;
4215 if (argc == 0) {
4216 usage_diff(); /* TODO show local worktree changes */
4217 } else if (argc == 2) {
4218 id_str1 = argv[0];
4219 id_str2 = argv[1];
4220 } else
4221 usage_diff();
4223 error = got_repo_pack_fds_open(&pack_fds);
4224 if (error)
4225 goto done;
4227 if (repo_path == NULL) {
4228 cwd = getcwd(NULL, 0);
4229 if (cwd == NULL)
4230 return got_error_from_errno("getcwd");
4231 error = got_worktree_open(&worktree, cwd);
4232 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4233 goto done;
4234 if (worktree)
4235 repo_path =
4236 strdup(got_worktree_get_repo_path(worktree));
4237 else
4238 repo_path = strdup(cwd);
4239 if (repo_path == NULL) {
4240 error = got_error_from_errno("strdup");
4241 goto done;
4245 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4246 if (error)
4247 goto done;
4249 init_curses();
4251 error = apply_unveil(got_repo_get_path(repo), NULL);
4252 if (error)
4253 goto done;
4255 error = tog_load_refs(repo, 0);
4256 if (error)
4257 goto done;
4259 error = got_repo_match_object_id(&id1, &label1, id_str1,
4260 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4261 if (error)
4262 goto done;
4264 error = got_repo_match_object_id(&id2, &label2, id_str2,
4265 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4266 if (error)
4267 goto done;
4269 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4270 if (view == NULL) {
4271 error = got_error_from_errno("view_open");
4272 goto done;
4274 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4275 ignore_whitespace, force_text_diff, NULL, repo);
4276 if (error)
4277 goto done;
4278 error = view_loop(view);
4279 done:
4280 free(label1);
4281 free(label2);
4282 free(repo_path);
4283 free(cwd);
4284 if (repo) {
4285 const struct got_error *close_err = got_repo_close(repo);
4286 if (error == NULL)
4287 error = close_err;
4289 if (worktree)
4290 got_worktree_close(worktree);
4291 if (pack_fds) {
4292 const struct got_error *pack_err =
4293 got_repo_pack_fds_close(pack_fds);
4294 if (error == NULL)
4295 error = pack_err;
4297 tog_free_refs();
4298 return error;
4301 __dead static void
4302 usage_blame(void)
4304 endwin();
4305 fprintf(stderr,
4306 "usage: %s blame [-c commit] [-r repository-path] path\n",
4307 getprogname());
4308 exit(1);
4311 struct tog_blame_line {
4312 int annotated;
4313 struct got_object_id *id;
4316 static const struct got_error *
4317 draw_blame(struct tog_view *view)
4319 struct tog_blame_view_state *s = &view->state.blame;
4320 struct tog_blame *blame = &s->blame;
4321 regmatch_t *regmatch = &view->regmatch;
4322 const struct got_error *err;
4323 int lineno = 0, nprinted = 0;
4324 char *line = NULL;
4325 size_t linesize = 0;
4326 ssize_t linelen;
4327 wchar_t *wline;
4328 int width;
4329 struct tog_blame_line *blame_line;
4330 struct got_object_id *prev_id = NULL;
4331 char *id_str;
4332 struct tog_color *tc;
4334 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4335 if (err)
4336 return err;
4338 rewind(blame->f);
4339 werase(view->window);
4341 if (asprintf(&line, "commit %s", id_str) == -1) {
4342 err = got_error_from_errno("asprintf");
4343 free(id_str);
4344 return err;
4347 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4348 free(line);
4349 line = NULL;
4350 if (err)
4351 return err;
4352 if (view_needs_focus_indication(view))
4353 wstandout(view->window);
4354 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4355 if (tc)
4356 wattr_on(view->window,
4357 COLOR_PAIR(tc->colorpair), NULL);
4358 waddwstr(view->window, wline);
4359 if (tc)
4360 wattr_off(view->window,
4361 COLOR_PAIR(tc->colorpair), NULL);
4362 if (view_needs_focus_indication(view))
4363 wstandend(view->window);
4364 free(wline);
4365 wline = NULL;
4366 if (width < view->ncols - 1)
4367 waddch(view->window, '\n');
4369 if (asprintf(&line, "[%d/%d] %s%s",
4370 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4371 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4372 free(id_str);
4373 return got_error_from_errno("asprintf");
4375 free(id_str);
4376 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4377 free(line);
4378 line = NULL;
4379 if (err)
4380 return err;
4381 waddwstr(view->window, wline);
4382 free(wline);
4383 wline = NULL;
4384 if (width < view->ncols - 1)
4385 waddch(view->window, '\n');
4387 s->eof = 0;
4388 view->maxx = 0;
4389 while (nprinted < view->nlines - 2) {
4390 linelen = getline(&line, &linesize, blame->f);
4391 if (linelen == -1) {
4392 if (feof(blame->f)) {
4393 s->eof = 1;
4394 break;
4396 free(line);
4397 return got_ferror(blame->f, GOT_ERR_IO);
4399 if (++lineno < s->first_displayed_line)
4400 continue;
4402 /* Set view->maxx based on full line length. */
4403 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4404 if (err) {
4405 free(line);
4406 return err;
4408 free(wline);
4409 wline = NULL;
4410 view->maxx = MAX(view->maxx, width);
4412 if (view->focussed && nprinted == s->selected_line - 1)
4413 wstandout(view->window);
4415 if (blame->nlines > 0) {
4416 blame_line = &blame->lines[lineno - 1];
4417 if (blame_line->annotated && prev_id &&
4418 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4419 !(view->focussed &&
4420 nprinted == s->selected_line - 1)) {
4421 waddstr(view->window, " ");
4422 } else if (blame_line->annotated) {
4423 char *id_str;
4424 err = got_object_id_str(&id_str,
4425 blame_line->id);
4426 if (err) {
4427 free(line);
4428 return err;
4430 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4431 if (tc)
4432 wattr_on(view->window,
4433 COLOR_PAIR(tc->colorpair), NULL);
4434 wprintw(view->window, "%.8s", id_str);
4435 if (tc)
4436 wattr_off(view->window,
4437 COLOR_PAIR(tc->colorpair), NULL);
4438 free(id_str);
4439 prev_id = blame_line->id;
4440 } else {
4441 waddstr(view->window, "........");
4442 prev_id = NULL;
4444 } else {
4445 waddstr(view->window, "........");
4446 prev_id = NULL;
4449 if (view->focussed && nprinted == s->selected_line - 1)
4450 wstandend(view->window);
4451 waddstr(view->window, " ");
4453 if (view->ncols <= 9) {
4454 width = 9;
4455 } else if (s->first_displayed_line + nprinted ==
4456 s->matched_line &&
4457 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4458 err = add_matched_line(&width, line, view->ncols - 9, 9,
4459 view->window, view->x, regmatch);
4460 if (err) {
4461 free(line);
4462 return err;
4464 width += 9;
4465 } else {
4466 int skip;
4467 err = format_line(&wline, &width, &skip, line,
4468 view->x, view->ncols - 9, 9, 1);
4469 if (err) {
4470 free(line);
4471 return err;
4473 waddwstr(view->window, &wline[skip]);
4474 width += 9;
4475 free(wline);
4476 wline = NULL;
4479 if (width <= view->ncols - 1)
4480 waddch(view->window, '\n');
4481 if (++nprinted == 1)
4482 s->first_displayed_line = lineno;
4484 free(line);
4485 s->last_displayed_line = lineno;
4487 view_vborder(view);
4489 return NULL;
4492 static const struct got_error *
4493 blame_cb(void *arg, int nlines, int lineno,
4494 struct got_commit_object *commit, struct got_object_id *id)
4496 const struct got_error *err = NULL;
4497 struct tog_blame_cb_args *a = arg;
4498 struct tog_blame_line *line;
4499 int errcode;
4501 if (nlines != a->nlines ||
4502 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4503 return got_error(GOT_ERR_RANGE);
4505 errcode = pthread_mutex_lock(&tog_mutex);
4506 if (errcode)
4507 return got_error_set_errno(errcode, "pthread_mutex_lock");
4509 if (*a->quit) { /* user has quit the blame view */
4510 err = got_error(GOT_ERR_ITER_COMPLETED);
4511 goto done;
4514 if (lineno == -1)
4515 goto done; /* no change in this commit */
4517 line = &a->lines[lineno - 1];
4518 if (line->annotated)
4519 goto done;
4521 line->id = got_object_id_dup(id);
4522 if (line->id == NULL) {
4523 err = got_error_from_errno("got_object_id_dup");
4524 goto done;
4526 line->annotated = 1;
4527 done:
4528 errcode = pthread_mutex_unlock(&tog_mutex);
4529 if (errcode)
4530 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4531 return err;
4534 static void *
4535 blame_thread(void *arg)
4537 const struct got_error *err, *close_err;
4538 struct tog_blame_thread_args *ta = arg;
4539 struct tog_blame_cb_args *a = ta->cb_args;
4540 int errcode;
4542 err = block_signals_used_by_main_thread();
4543 if (err)
4544 return (void *)err;
4546 err = got_blame(ta->path, a->commit_id, ta->repo,
4547 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4548 if (err && err->code == GOT_ERR_CANCELLED)
4549 err = NULL;
4551 errcode = pthread_mutex_lock(&tog_mutex);
4552 if (errcode)
4553 return (void *)got_error_set_errno(errcode,
4554 "pthread_mutex_lock");
4556 close_err = got_repo_close(ta->repo);
4557 if (err == NULL)
4558 err = close_err;
4559 ta->repo = NULL;
4560 *ta->complete = 1;
4562 errcode = pthread_mutex_unlock(&tog_mutex);
4563 if (errcode && err == NULL)
4564 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4566 return (void *)err;
4569 static struct got_object_id *
4570 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4571 int first_displayed_line, int selected_line)
4573 struct tog_blame_line *line;
4575 if (nlines <= 0)
4576 return NULL;
4578 line = &lines[first_displayed_line - 1 + selected_line - 1];
4579 if (!line->annotated)
4580 return NULL;
4582 return line->id;
4585 static const struct got_error *
4586 stop_blame(struct tog_blame *blame)
4588 const struct got_error *err = NULL;
4589 int i;
4591 if (blame->thread) {
4592 int errcode;
4593 errcode = pthread_mutex_unlock(&tog_mutex);
4594 if (errcode)
4595 return got_error_set_errno(errcode,
4596 "pthread_mutex_unlock");
4597 errcode = pthread_join(blame->thread, (void **)&err);
4598 if (errcode)
4599 return got_error_set_errno(errcode, "pthread_join");
4600 errcode = pthread_mutex_lock(&tog_mutex);
4601 if (errcode)
4602 return got_error_set_errno(errcode,
4603 "pthread_mutex_lock");
4604 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4605 err = NULL;
4606 blame->thread = 0; //NULL;
4608 if (blame->thread_args.repo) {
4609 const struct got_error *close_err;
4610 close_err = got_repo_close(blame->thread_args.repo);
4611 if (err == NULL)
4612 err = close_err;
4613 blame->thread_args.repo = NULL;
4615 if (blame->f) {
4616 if (fclose(blame->f) == EOF && err == NULL)
4617 err = got_error_from_errno("fclose");
4618 blame->f = NULL;
4620 if (blame->lines) {
4621 for (i = 0; i < blame->nlines; i++)
4622 free(blame->lines[i].id);
4623 free(blame->lines);
4624 blame->lines = NULL;
4626 free(blame->cb_args.commit_id);
4627 blame->cb_args.commit_id = NULL;
4628 if (blame->pack_fds) {
4629 const struct got_error *pack_err =
4630 got_repo_pack_fds_close(blame->pack_fds);
4631 if (err == NULL)
4632 err = pack_err;
4633 blame->pack_fds = NULL;
4635 return err;
4638 static const struct got_error *
4639 cancel_blame_view(void *arg)
4641 const struct got_error *err = NULL;
4642 int *done = arg;
4643 int errcode;
4645 errcode = pthread_mutex_lock(&tog_mutex);
4646 if (errcode)
4647 return got_error_set_errno(errcode,
4648 "pthread_mutex_unlock");
4650 if (*done)
4651 err = got_error(GOT_ERR_CANCELLED);
4653 errcode = pthread_mutex_unlock(&tog_mutex);
4654 if (errcode)
4655 return got_error_set_errno(errcode,
4656 "pthread_mutex_lock");
4658 return err;
4661 static const struct got_error *
4662 run_blame(struct tog_view *view)
4664 struct tog_blame_view_state *s = &view->state.blame;
4665 struct tog_blame *blame = &s->blame;
4666 const struct got_error *err = NULL;
4667 struct got_commit_object *commit = NULL;
4668 struct got_blob_object *blob = NULL;
4669 struct got_repository *thread_repo = NULL;
4670 struct got_object_id *obj_id = NULL;
4671 int obj_type;
4672 int *pack_fds = NULL;
4674 err = got_object_open_as_commit(&commit, s->repo,
4675 &s->blamed_commit->id);
4676 if (err)
4677 return err;
4679 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4680 if (err)
4681 goto done;
4683 err = got_object_get_type(&obj_type, s->repo, obj_id);
4684 if (err)
4685 goto done;
4687 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4688 err = got_error(GOT_ERR_OBJ_TYPE);
4689 goto done;
4692 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4693 if (err)
4694 goto done;
4695 blame->f = got_opentemp();
4696 if (blame->f == NULL) {
4697 err = got_error_from_errno("got_opentemp");
4698 goto done;
4700 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4701 &blame->line_offsets, blame->f, blob);
4702 if (err)
4703 goto done;
4704 if (blame->nlines == 0) {
4705 s->blame_complete = 1;
4706 goto done;
4709 /* Don't include \n at EOF in the blame line count. */
4710 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4711 blame->nlines--;
4713 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4714 if (blame->lines == NULL) {
4715 err = got_error_from_errno("calloc");
4716 goto done;
4719 err = got_repo_pack_fds_open(&pack_fds);
4720 if (err)
4721 goto done;
4722 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4723 pack_fds);
4724 if (err)
4725 goto done;
4727 blame->pack_fds = pack_fds;
4728 blame->cb_args.view = view;
4729 blame->cb_args.lines = blame->lines;
4730 blame->cb_args.nlines = blame->nlines;
4731 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4732 if (blame->cb_args.commit_id == NULL) {
4733 err = got_error_from_errno("got_object_id_dup");
4734 goto done;
4736 blame->cb_args.quit = &s->done;
4738 blame->thread_args.path = s->path;
4739 blame->thread_args.repo = thread_repo;
4740 blame->thread_args.cb_args = &blame->cb_args;
4741 blame->thread_args.complete = &s->blame_complete;
4742 blame->thread_args.cancel_cb = cancel_blame_view;
4743 blame->thread_args.cancel_arg = &s->done;
4744 s->blame_complete = 0;
4746 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4747 s->first_displayed_line = 1;
4748 s->last_displayed_line = view->nlines;
4749 s->selected_line = 1;
4751 s->matched_line = 0;
4753 done:
4754 if (commit)
4755 got_object_commit_close(commit);
4756 if (blob)
4757 got_object_blob_close(blob);
4758 free(obj_id);
4759 if (err)
4760 stop_blame(blame);
4761 return err;
4764 static const struct got_error *
4765 open_blame_view(struct tog_view *view, char *path,
4766 struct got_object_id *commit_id, struct got_repository *repo)
4768 const struct got_error *err = NULL;
4769 struct tog_blame_view_state *s = &view->state.blame;
4771 STAILQ_INIT(&s->blamed_commits);
4773 s->path = strdup(path);
4774 if (s->path == NULL)
4775 return got_error_from_errno("strdup");
4777 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4778 if (err) {
4779 free(s->path);
4780 return err;
4783 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4784 s->first_displayed_line = 1;
4785 s->last_displayed_line = view->nlines;
4786 s->selected_line = 1;
4787 s->blame_complete = 0;
4788 s->repo = repo;
4789 s->commit_id = commit_id;
4790 memset(&s->blame, 0, sizeof(s->blame));
4792 STAILQ_INIT(&s->colors);
4793 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4794 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4795 get_color_value("TOG_COLOR_COMMIT"));
4796 if (err)
4797 return err;
4800 view->show = show_blame_view;
4801 view->input = input_blame_view;
4802 view->close = close_blame_view;
4803 view->search_start = search_start_blame_view;
4804 view->search_next = search_next_blame_view;
4806 return run_blame(view);
4809 static const struct got_error *
4810 close_blame_view(struct tog_view *view)
4812 const struct got_error *err = NULL;
4813 struct tog_blame_view_state *s = &view->state.blame;
4815 if (s->blame.thread)
4816 err = stop_blame(&s->blame);
4818 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4819 struct got_object_qid *blamed_commit;
4820 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4821 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4822 got_object_qid_free(blamed_commit);
4825 free(s->path);
4826 free_colors(&s->colors);
4827 return err;
4830 static const struct got_error *
4831 search_start_blame_view(struct tog_view *view)
4833 struct tog_blame_view_state *s = &view->state.blame;
4835 s->matched_line = 0;
4836 return NULL;
4839 static const struct got_error *
4840 search_next_blame_view(struct tog_view *view)
4842 struct tog_blame_view_state *s = &view->state.blame;
4843 const struct got_error *err = NULL;
4844 int lineno;
4845 char *line = NULL;
4846 size_t linesize = 0;
4847 ssize_t linelen;
4849 if (!view->searching) {
4850 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4851 return NULL;
4854 if (s->matched_line) {
4855 if (view->searching == TOG_SEARCH_FORWARD)
4856 lineno = s->matched_line + 1;
4857 else
4858 lineno = s->matched_line - 1;
4859 } else
4860 lineno = s->first_displayed_line - 1 + s->selected_line;
4862 while (1) {
4863 off_t offset;
4865 if (lineno <= 0 || lineno > s->blame.nlines) {
4866 if (s->matched_line == 0) {
4867 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4868 break;
4871 if (view->searching == TOG_SEARCH_FORWARD)
4872 lineno = 1;
4873 else
4874 lineno = s->blame.nlines;
4877 offset = s->blame.line_offsets[lineno - 1];
4878 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4879 free(line);
4880 return got_error_from_errno("fseeko");
4882 linelen = getline(&line, &linesize, s->blame.f);
4883 if (linelen != -1) {
4884 char *exstr;
4885 err = expand_tab(&exstr, line);
4886 if (err)
4887 break;
4888 if (match_line(exstr, &view->regex, 1,
4889 &view->regmatch)) {
4890 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4891 s->matched_line = lineno;
4892 free(exstr);
4893 break;
4895 free(exstr);
4897 if (view->searching == TOG_SEARCH_FORWARD)
4898 lineno++;
4899 else
4900 lineno--;
4902 free(line);
4904 if (s->matched_line) {
4905 s->first_displayed_line = s->matched_line;
4906 s->selected_line = 1;
4909 return err;
4912 static const struct got_error *
4913 show_blame_view(struct tog_view *view)
4915 const struct got_error *err = NULL;
4916 struct tog_blame_view_state *s = &view->state.blame;
4917 int errcode;
4919 if (s->blame.thread == 0 && !s->blame_complete) {
4920 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4921 &s->blame.thread_args);
4922 if (errcode)
4923 return got_error_set_errno(errcode, "pthread_create");
4925 halfdelay(1); /* fast refresh while annotating */
4928 if (s->blame_complete)
4929 halfdelay(10); /* disable fast refresh */
4931 err = draw_blame(view);
4933 view_vborder(view);
4934 return err;
4937 static const struct got_error *
4938 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4940 const struct got_error *err = NULL, *thread_err = NULL;
4941 struct tog_view *diff_view;
4942 struct tog_blame_view_state *s = &view->state.blame;
4943 int begin_x = 0, nscroll = view->nlines - 2;
4945 switch (ch) {
4946 case '0':
4947 view->x = 0;
4948 break;
4949 case '$':
4950 view->x = MAX(view->maxx - view->ncols / 3, 0);
4951 break;
4952 case KEY_RIGHT:
4953 case 'l':
4954 if (view->x + view->ncols / 3 < view->maxx)
4955 view->x += 2; /* move two columns right */
4956 break;
4957 case KEY_LEFT:
4958 case 'h':
4959 view->x -= MIN(view->x, 2); /* move two columns back */
4960 break;
4961 case 'q':
4962 s->done = 1;
4963 break;
4964 case 'g':
4965 case KEY_HOME:
4966 s->selected_line = 1;
4967 s->first_displayed_line = 1;
4968 break;
4969 case 'G':
4970 case KEY_END:
4971 if (s->blame.nlines < view->nlines - 2) {
4972 s->selected_line = s->blame.nlines;
4973 s->first_displayed_line = 1;
4974 } else {
4975 s->selected_line = view->nlines - 2;
4976 s->first_displayed_line = s->blame.nlines -
4977 (view->nlines - 3);
4979 break;
4980 case 'k':
4981 case KEY_UP:
4982 case CTRL('p'):
4983 if (s->selected_line > 1)
4984 s->selected_line--;
4985 else if (s->selected_line == 1 &&
4986 s->first_displayed_line > 1)
4987 s->first_displayed_line--;
4988 break;
4989 case CTRL('u'):
4990 case 'u':
4991 nscroll /= 2;
4992 /* FALL THROUGH */
4993 case KEY_PPAGE:
4994 case CTRL('b'):
4995 if (s->first_displayed_line == 1) {
4996 s->selected_line = MAX(1, s->selected_line - nscroll);
4997 break;
4999 if (s->first_displayed_line > nscroll)
5000 s->first_displayed_line -= nscroll;
5001 else
5002 s->first_displayed_line = 1;
5003 break;
5004 case 'j':
5005 case KEY_DOWN:
5006 case CTRL('n'):
5007 if (s->selected_line < view->nlines - 2 &&
5008 s->first_displayed_line +
5009 s->selected_line <= s->blame.nlines)
5010 s->selected_line++;
5011 else if (s->last_displayed_line <
5012 s->blame.nlines)
5013 s->first_displayed_line++;
5014 break;
5015 case 'b':
5016 case 'p': {
5017 struct got_object_id *id = NULL;
5018 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5019 s->first_displayed_line, s->selected_line);
5020 if (id == NULL)
5021 break;
5022 if (ch == 'p') {
5023 struct got_commit_object *commit, *pcommit;
5024 struct got_object_qid *pid;
5025 struct got_object_id *blob_id = NULL;
5026 int obj_type;
5027 err = got_object_open_as_commit(&commit,
5028 s->repo, id);
5029 if (err)
5030 break;
5031 pid = STAILQ_FIRST(
5032 got_object_commit_get_parent_ids(commit));
5033 if (pid == NULL) {
5034 got_object_commit_close(commit);
5035 break;
5037 /* Check if path history ends here. */
5038 err = got_object_open_as_commit(&pcommit,
5039 s->repo, &pid->id);
5040 if (err)
5041 break;
5042 err = got_object_id_by_path(&blob_id, s->repo,
5043 pcommit, s->path);
5044 got_object_commit_close(pcommit);
5045 if (err) {
5046 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5047 err = NULL;
5048 got_object_commit_close(commit);
5049 break;
5051 err = got_object_get_type(&obj_type, s->repo,
5052 blob_id);
5053 free(blob_id);
5054 /* Can't blame non-blob type objects. */
5055 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5056 got_object_commit_close(commit);
5057 break;
5059 err = got_object_qid_alloc(&s->blamed_commit,
5060 &pid->id);
5061 got_object_commit_close(commit);
5062 } else {
5063 if (got_object_id_cmp(id,
5064 &s->blamed_commit->id) == 0)
5065 break;
5066 err = got_object_qid_alloc(&s->blamed_commit,
5067 id);
5069 if (err)
5070 break;
5071 s->done = 1;
5072 thread_err = stop_blame(&s->blame);
5073 s->done = 0;
5074 if (thread_err)
5075 break;
5076 STAILQ_INSERT_HEAD(&s->blamed_commits,
5077 s->blamed_commit, entry);
5078 err = run_blame(view);
5079 if (err)
5080 break;
5081 break;
5083 case 'B': {
5084 struct got_object_qid *first;
5085 first = STAILQ_FIRST(&s->blamed_commits);
5086 if (!got_object_id_cmp(&first->id, s->commit_id))
5087 break;
5088 s->done = 1;
5089 thread_err = stop_blame(&s->blame);
5090 s->done = 0;
5091 if (thread_err)
5092 break;
5093 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5094 got_object_qid_free(s->blamed_commit);
5095 s->blamed_commit =
5096 STAILQ_FIRST(&s->blamed_commits);
5097 err = run_blame(view);
5098 if (err)
5099 break;
5100 break;
5102 case KEY_ENTER:
5103 case '\r': {
5104 struct got_object_id *id = NULL;
5105 struct got_object_qid *pid;
5106 struct got_commit_object *commit = NULL;
5107 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5108 s->first_displayed_line, s->selected_line);
5109 if (id == NULL)
5110 break;
5111 err = got_object_open_as_commit(&commit, s->repo, id);
5112 if (err)
5113 break;
5114 pid = STAILQ_FIRST(
5115 got_object_commit_get_parent_ids(commit));
5116 if (view_is_parent_view(view))
5117 begin_x = view_split_begin_x(view->begin_x);
5118 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5119 if (diff_view == NULL) {
5120 got_object_commit_close(commit);
5121 err = got_error_from_errno("view_open");
5122 break;
5124 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5125 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5126 got_object_commit_close(commit);
5127 if (err) {
5128 view_close(diff_view);
5129 break;
5131 view->focussed = 0;
5132 diff_view->focussed = 1;
5133 if (view_is_parent_view(view)) {
5134 err = view_close_child(view);
5135 if (err)
5136 break;
5137 err = view_set_child(view, diff_view);
5138 if (err)
5139 break;
5140 view->focus_child = 1;
5141 } else
5142 *new_view = diff_view;
5143 if (err)
5144 break;
5145 break;
5147 case CTRL('d'):
5148 case 'd':
5149 nscroll /= 2;
5150 /* FALL THROUGH */
5151 case KEY_NPAGE:
5152 case CTRL('f'):
5153 case ' ':
5154 if (s->last_displayed_line >= s->blame.nlines &&
5155 s->selected_line >= MIN(s->blame.nlines,
5156 view->nlines - 2)) {
5157 break;
5159 if (s->last_displayed_line >= s->blame.nlines &&
5160 s->selected_line < view->nlines - 2) {
5161 s->selected_line +=
5162 MIN(nscroll, s->last_displayed_line -
5163 s->first_displayed_line - s->selected_line + 1);
5165 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5166 s->first_displayed_line += nscroll;
5167 else
5168 s->first_displayed_line =
5169 s->blame.nlines - (view->nlines - 3);
5170 break;
5171 case KEY_RESIZE:
5172 if (s->selected_line > view->nlines - 2) {
5173 s->selected_line = MIN(s->blame.nlines,
5174 view->nlines - 2);
5176 break;
5177 default:
5178 break;
5180 return thread_err ? thread_err : err;
5183 static const struct got_error *
5184 cmd_blame(int argc, char *argv[])
5186 const struct got_error *error;
5187 struct got_repository *repo = NULL;
5188 struct got_worktree *worktree = NULL;
5189 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5190 char *link_target = NULL;
5191 struct got_object_id *commit_id = NULL;
5192 struct got_commit_object *commit = NULL;
5193 char *commit_id_str = NULL;
5194 int ch;
5195 struct tog_view *view;
5196 int *pack_fds = NULL;
5198 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5199 switch (ch) {
5200 case 'c':
5201 commit_id_str = optarg;
5202 break;
5203 case 'r':
5204 repo_path = realpath(optarg, NULL);
5205 if (repo_path == NULL)
5206 return got_error_from_errno2("realpath",
5207 optarg);
5208 break;
5209 default:
5210 usage_blame();
5211 /* NOTREACHED */
5215 argc -= optind;
5216 argv += optind;
5218 if (argc != 1)
5219 usage_blame();
5221 error = got_repo_pack_fds_open(&pack_fds);
5222 if (error != NULL)
5223 goto done;
5225 if (repo_path == NULL) {
5226 cwd = getcwd(NULL, 0);
5227 if (cwd == NULL)
5228 return got_error_from_errno("getcwd");
5229 error = got_worktree_open(&worktree, cwd);
5230 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5231 goto done;
5232 if (worktree)
5233 repo_path =
5234 strdup(got_worktree_get_repo_path(worktree));
5235 else
5236 repo_path = strdup(cwd);
5237 if (repo_path == NULL) {
5238 error = got_error_from_errno("strdup");
5239 goto done;
5243 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5244 if (error != NULL)
5245 goto done;
5247 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5248 worktree);
5249 if (error)
5250 goto done;
5252 init_curses();
5254 error = apply_unveil(got_repo_get_path(repo), NULL);
5255 if (error)
5256 goto done;
5258 error = tog_load_refs(repo, 0);
5259 if (error)
5260 goto done;
5262 if (commit_id_str == NULL) {
5263 struct got_reference *head_ref;
5264 error = got_ref_open(&head_ref, repo, worktree ?
5265 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5266 if (error != NULL)
5267 goto done;
5268 error = got_ref_resolve(&commit_id, repo, head_ref);
5269 got_ref_close(head_ref);
5270 } else {
5271 error = got_repo_match_object_id(&commit_id, NULL,
5272 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5274 if (error != NULL)
5275 goto done;
5277 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5278 if (view == NULL) {
5279 error = got_error_from_errno("view_open");
5280 goto done;
5283 error = got_object_open_as_commit(&commit, repo, commit_id);
5284 if (error)
5285 goto done;
5287 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5288 commit, repo);
5289 if (error)
5290 goto done;
5292 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5293 commit_id, repo);
5294 if (error)
5295 goto done;
5296 if (worktree) {
5297 /* Release work tree lock. */
5298 got_worktree_close(worktree);
5299 worktree = NULL;
5301 error = view_loop(view);
5302 done:
5303 free(repo_path);
5304 free(in_repo_path);
5305 free(link_target);
5306 free(cwd);
5307 free(commit_id);
5308 if (commit)
5309 got_object_commit_close(commit);
5310 if (worktree)
5311 got_worktree_close(worktree);
5312 if (repo) {
5313 const struct got_error *close_err = got_repo_close(repo);
5314 if (error == NULL)
5315 error = close_err;
5317 if (pack_fds) {
5318 const struct got_error *pack_err =
5319 got_repo_pack_fds_close(pack_fds);
5320 if (error == NULL)
5321 error = pack_err;
5323 tog_free_refs();
5324 return error;
5327 static const struct got_error *
5328 draw_tree_entries(struct tog_view *view, const char *parent_path)
5330 struct tog_tree_view_state *s = &view->state.tree;
5331 const struct got_error *err = NULL;
5332 struct got_tree_entry *te;
5333 wchar_t *wline;
5334 struct tog_color *tc;
5335 int width, n, i, nentries;
5336 int limit = view->nlines;
5338 s->ndisplayed = 0;
5340 werase(view->window);
5342 if (limit == 0)
5343 return NULL;
5345 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5346 0, 0);
5347 if (err)
5348 return err;
5349 if (view_needs_focus_indication(view))
5350 wstandout(view->window);
5351 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5352 if (tc)
5353 wattr_on(view->window,
5354 COLOR_PAIR(tc->colorpair), NULL);
5355 waddwstr(view->window, wline);
5356 if (tc)
5357 wattr_off(view->window,
5358 COLOR_PAIR(tc->colorpair), NULL);
5359 if (view_needs_focus_indication(view))
5360 wstandend(view->window);
5361 free(wline);
5362 wline = NULL;
5363 if (width < view->ncols - 1)
5364 waddch(view->window, '\n');
5365 if (--limit <= 0)
5366 return NULL;
5367 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5368 0, 0);
5369 if (err)
5370 return err;
5371 waddwstr(view->window, wline);
5372 free(wline);
5373 wline = NULL;
5374 if (width < view->ncols - 1)
5375 waddch(view->window, '\n');
5376 if (--limit <= 0)
5377 return NULL;
5378 waddch(view->window, '\n');
5379 if (--limit <= 0)
5380 return NULL;
5382 if (s->first_displayed_entry == NULL) {
5383 te = got_object_tree_get_first_entry(s->tree);
5384 if (s->selected == 0) {
5385 if (view->focussed)
5386 wstandout(view->window);
5387 s->selected_entry = NULL;
5389 waddstr(view->window, " ..\n"); /* parent directory */
5390 if (s->selected == 0 && view->focussed)
5391 wstandend(view->window);
5392 s->ndisplayed++;
5393 if (--limit <= 0)
5394 return NULL;
5395 n = 1;
5396 } else {
5397 n = 0;
5398 te = s->first_displayed_entry;
5401 nentries = got_object_tree_get_nentries(s->tree);
5402 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5403 char *line = NULL, *id_str = NULL, *link_target = NULL;
5404 const char *modestr = "";
5405 mode_t mode;
5407 te = got_object_tree_get_entry(s->tree, i);
5408 mode = got_tree_entry_get_mode(te);
5410 if (s->show_ids) {
5411 err = got_object_id_str(&id_str,
5412 got_tree_entry_get_id(te));
5413 if (err)
5414 return got_error_from_errno(
5415 "got_object_id_str");
5417 if (got_object_tree_entry_is_submodule(te))
5418 modestr = "$";
5419 else if (S_ISLNK(mode)) {
5420 int i;
5422 err = got_tree_entry_get_symlink_target(&link_target,
5423 te, s->repo);
5424 if (err) {
5425 free(id_str);
5426 return err;
5428 for (i = 0; i < strlen(link_target); i++) {
5429 if (!isprint((unsigned char)link_target[i]))
5430 link_target[i] = '?';
5432 modestr = "@";
5434 else if (S_ISDIR(mode))
5435 modestr = "/";
5436 else if (mode & S_IXUSR)
5437 modestr = "*";
5438 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5439 got_tree_entry_get_name(te), modestr,
5440 link_target ? " -> ": "",
5441 link_target ? link_target : "") == -1) {
5442 free(id_str);
5443 free(link_target);
5444 return got_error_from_errno("asprintf");
5446 free(id_str);
5447 free(link_target);
5448 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5449 0, 0);
5450 if (err) {
5451 free(line);
5452 break;
5454 if (n == s->selected) {
5455 if (view->focussed)
5456 wstandout(view->window);
5457 s->selected_entry = te;
5459 tc = match_color(&s->colors, line);
5460 if (tc)
5461 wattr_on(view->window,
5462 COLOR_PAIR(tc->colorpair), NULL);
5463 waddwstr(view->window, wline);
5464 if (tc)
5465 wattr_off(view->window,
5466 COLOR_PAIR(tc->colorpair), NULL);
5467 if (width < view->ncols - 1)
5468 waddch(view->window, '\n');
5469 if (n == s->selected && view->focussed)
5470 wstandend(view->window);
5471 free(line);
5472 free(wline);
5473 wline = NULL;
5474 n++;
5475 s->ndisplayed++;
5476 s->last_displayed_entry = te;
5477 if (--limit <= 0)
5478 break;
5481 return err;
5484 static void
5485 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5487 struct got_tree_entry *te;
5488 int isroot = s->tree == s->root;
5489 int i = 0;
5491 if (s->first_displayed_entry == NULL)
5492 return;
5494 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5495 while (i++ < maxscroll) {
5496 if (te == NULL) {
5497 if (!isroot)
5498 s->first_displayed_entry = NULL;
5499 break;
5501 s->first_displayed_entry = te;
5502 te = got_tree_entry_get_prev(s->tree, te);
5506 static void
5507 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5509 struct got_tree_entry *next, *last;
5510 int n = 0;
5512 if (s->first_displayed_entry)
5513 next = got_tree_entry_get_next(s->tree,
5514 s->first_displayed_entry);
5515 else
5516 next = got_object_tree_get_first_entry(s->tree);
5518 last = s->last_displayed_entry;
5519 while (next && last && n++ < maxscroll) {
5520 last = got_tree_entry_get_next(s->tree, last);
5521 if (last) {
5522 s->first_displayed_entry = next;
5523 next = got_tree_entry_get_next(s->tree, next);
5528 static const struct got_error *
5529 tree_entry_path(char **path, struct tog_parent_trees *parents,
5530 struct got_tree_entry *te)
5532 const struct got_error *err = NULL;
5533 struct tog_parent_tree *pt;
5534 size_t len = 2; /* for leading slash and NUL */
5536 TAILQ_FOREACH(pt, parents, entry)
5537 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5538 + 1 /* slash */;
5539 if (te)
5540 len += strlen(got_tree_entry_get_name(te));
5542 *path = calloc(1, len);
5543 if (path == NULL)
5544 return got_error_from_errno("calloc");
5546 (*path)[0] = '/';
5547 pt = TAILQ_LAST(parents, tog_parent_trees);
5548 while (pt) {
5549 const char *name = got_tree_entry_get_name(pt->selected_entry);
5550 if (strlcat(*path, name, len) >= len) {
5551 err = got_error(GOT_ERR_NO_SPACE);
5552 goto done;
5554 if (strlcat(*path, "/", len) >= len) {
5555 err = got_error(GOT_ERR_NO_SPACE);
5556 goto done;
5558 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5560 if (te) {
5561 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5562 err = got_error(GOT_ERR_NO_SPACE);
5563 goto done;
5566 done:
5567 if (err) {
5568 free(*path);
5569 *path = NULL;
5571 return err;
5574 static const struct got_error *
5575 blame_tree_entry(struct tog_view **new_view, int begin_x,
5576 struct got_tree_entry *te, struct tog_parent_trees *parents,
5577 struct got_object_id *commit_id, struct got_repository *repo)
5579 const struct got_error *err = NULL;
5580 char *path;
5581 struct tog_view *blame_view;
5583 *new_view = NULL;
5585 err = tree_entry_path(&path, parents, te);
5586 if (err)
5587 return err;
5589 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5590 if (blame_view == NULL) {
5591 err = got_error_from_errno("view_open");
5592 goto done;
5595 err = open_blame_view(blame_view, path, commit_id, repo);
5596 if (err) {
5597 if (err->code == GOT_ERR_CANCELLED)
5598 err = NULL;
5599 view_close(blame_view);
5600 } else
5601 *new_view = blame_view;
5602 done:
5603 free(path);
5604 return err;
5607 static const struct got_error *
5608 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5609 struct tog_tree_view_state *s)
5611 struct tog_view *log_view;
5612 const struct got_error *err = NULL;
5613 char *path;
5615 *new_view = NULL;
5617 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5618 if (log_view == NULL)
5619 return got_error_from_errno("view_open");
5621 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5622 if (err)
5623 return err;
5625 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5626 path, 0);
5627 if (err)
5628 view_close(log_view);
5629 else
5630 *new_view = log_view;
5631 free(path);
5632 return err;
5635 static const struct got_error *
5636 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5637 const char *head_ref_name, struct got_repository *repo)
5639 const struct got_error *err = NULL;
5640 char *commit_id_str = NULL;
5641 struct tog_tree_view_state *s = &view->state.tree;
5642 struct got_commit_object *commit = NULL;
5644 TAILQ_INIT(&s->parents);
5645 STAILQ_INIT(&s->colors);
5647 s->commit_id = got_object_id_dup(commit_id);
5648 if (s->commit_id == NULL)
5649 return got_error_from_errno("got_object_id_dup");
5651 err = got_object_open_as_commit(&commit, repo, commit_id);
5652 if (err)
5653 goto done;
5656 * The root is opened here and will be closed when the view is closed.
5657 * Any visited subtrees and their path-wise parents are opened and
5658 * closed on demand.
5660 err = got_object_open_as_tree(&s->root, repo,
5661 got_object_commit_get_tree_id(commit));
5662 if (err)
5663 goto done;
5664 s->tree = s->root;
5666 err = got_object_id_str(&commit_id_str, commit_id);
5667 if (err != NULL)
5668 goto done;
5670 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5671 err = got_error_from_errno("asprintf");
5672 goto done;
5675 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5676 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5677 if (head_ref_name) {
5678 s->head_ref_name = strdup(head_ref_name);
5679 if (s->head_ref_name == NULL) {
5680 err = got_error_from_errno("strdup");
5681 goto done;
5684 s->repo = repo;
5686 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5687 err = add_color(&s->colors, "\\$$",
5688 TOG_COLOR_TREE_SUBMODULE,
5689 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5690 if (err)
5691 goto done;
5692 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5693 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5694 if (err)
5695 goto done;
5696 err = add_color(&s->colors, "/$",
5697 TOG_COLOR_TREE_DIRECTORY,
5698 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5699 if (err)
5700 goto done;
5702 err = add_color(&s->colors, "\\*$",
5703 TOG_COLOR_TREE_EXECUTABLE,
5704 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5705 if (err)
5706 goto done;
5708 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5709 get_color_value("TOG_COLOR_COMMIT"));
5710 if (err)
5711 goto done;
5714 view->show = show_tree_view;
5715 view->input = input_tree_view;
5716 view->close = close_tree_view;
5717 view->search_start = search_start_tree_view;
5718 view->search_next = search_next_tree_view;
5719 done:
5720 free(commit_id_str);
5721 if (commit)
5722 got_object_commit_close(commit);
5723 if (err)
5724 close_tree_view(view);
5725 return err;
5728 static const struct got_error *
5729 close_tree_view(struct tog_view *view)
5731 struct tog_tree_view_state *s = &view->state.tree;
5733 free_colors(&s->colors);
5734 free(s->tree_label);
5735 s->tree_label = NULL;
5736 free(s->commit_id);
5737 s->commit_id = NULL;
5738 free(s->head_ref_name);
5739 s->head_ref_name = NULL;
5740 while (!TAILQ_EMPTY(&s->parents)) {
5741 struct tog_parent_tree *parent;
5742 parent = TAILQ_FIRST(&s->parents);
5743 TAILQ_REMOVE(&s->parents, parent, entry);
5744 if (parent->tree != s->root)
5745 got_object_tree_close(parent->tree);
5746 free(parent);
5749 if (s->tree != NULL && s->tree != s->root)
5750 got_object_tree_close(s->tree);
5751 if (s->root)
5752 got_object_tree_close(s->root);
5753 return NULL;
5756 static const struct got_error *
5757 search_start_tree_view(struct tog_view *view)
5759 struct tog_tree_view_state *s = &view->state.tree;
5761 s->matched_entry = NULL;
5762 return NULL;
5765 static int
5766 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5768 regmatch_t regmatch;
5770 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5771 0) == 0;
5774 static const struct got_error *
5775 search_next_tree_view(struct tog_view *view)
5777 struct tog_tree_view_state *s = &view->state.tree;
5778 struct got_tree_entry *te = NULL;
5780 if (!view->searching) {
5781 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5782 return NULL;
5785 if (s->matched_entry) {
5786 if (view->searching == TOG_SEARCH_FORWARD) {
5787 if (s->selected_entry)
5788 te = got_tree_entry_get_next(s->tree,
5789 s->selected_entry);
5790 else
5791 te = got_object_tree_get_first_entry(s->tree);
5792 } else {
5793 if (s->selected_entry == NULL)
5794 te = got_object_tree_get_last_entry(s->tree);
5795 else
5796 te = got_tree_entry_get_prev(s->tree,
5797 s->selected_entry);
5799 } else {
5800 if (s->selected_entry)
5801 te = s->selected_entry;
5802 else if (view->searching == TOG_SEARCH_FORWARD)
5803 te = got_object_tree_get_first_entry(s->tree);
5804 else
5805 te = got_object_tree_get_last_entry(s->tree);
5808 while (1) {
5809 if (te == NULL) {
5810 if (s->matched_entry == NULL) {
5811 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5812 return NULL;
5814 if (view->searching == TOG_SEARCH_FORWARD)
5815 te = got_object_tree_get_first_entry(s->tree);
5816 else
5817 te = got_object_tree_get_last_entry(s->tree);
5820 if (match_tree_entry(te, &view->regex)) {
5821 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5822 s->matched_entry = te;
5823 break;
5826 if (view->searching == TOG_SEARCH_FORWARD)
5827 te = got_tree_entry_get_next(s->tree, te);
5828 else
5829 te = got_tree_entry_get_prev(s->tree, te);
5832 if (s->matched_entry) {
5833 s->first_displayed_entry = s->matched_entry;
5834 s->selected = 0;
5837 return NULL;
5840 static const struct got_error *
5841 show_tree_view(struct tog_view *view)
5843 const struct got_error *err = NULL;
5844 struct tog_tree_view_state *s = &view->state.tree;
5845 char *parent_path;
5847 err = tree_entry_path(&parent_path, &s->parents, NULL);
5848 if (err)
5849 return err;
5851 err = draw_tree_entries(view, parent_path);
5852 free(parent_path);
5854 view_vborder(view);
5855 return err;
5858 static const struct got_error *
5859 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5861 const struct got_error *err = NULL;
5862 struct tog_tree_view_state *s = &view->state.tree;
5863 struct tog_view *log_view, *ref_view;
5864 struct got_tree_entry *te;
5865 int begin_x = 0, n, nscroll = view->nlines - 3;
5867 switch (ch) {
5868 case 'i':
5869 s->show_ids = !s->show_ids;
5870 break;
5871 case 'l':
5872 if (!s->selected_entry)
5873 break;
5874 if (view_is_parent_view(view))
5875 begin_x = view_split_begin_x(view->begin_x);
5876 err = log_selected_tree_entry(&log_view, begin_x, s);
5877 view->focussed = 0;
5878 log_view->focussed = 1;
5879 if (view_is_parent_view(view)) {
5880 err = view_close_child(view);
5881 if (err)
5882 return err;
5883 err = view_set_child(view, log_view);
5884 if (err)
5885 return err;
5886 view->focus_child = 1;
5887 } else
5888 *new_view = log_view;
5889 break;
5890 case 'r':
5891 if (view_is_parent_view(view))
5892 begin_x = view_split_begin_x(view->begin_x);
5893 ref_view = view_open(view->nlines, view->ncols,
5894 view->begin_y, begin_x, TOG_VIEW_REF);
5895 if (ref_view == NULL)
5896 return got_error_from_errno("view_open");
5897 err = open_ref_view(ref_view, s->repo);
5898 if (err) {
5899 view_close(ref_view);
5900 return err;
5902 view->focussed = 0;
5903 ref_view->focussed = 1;
5904 if (view_is_parent_view(view)) {
5905 err = view_close_child(view);
5906 if (err)
5907 return err;
5908 err = view_set_child(view, ref_view);
5909 if (err)
5910 return err;
5911 view->focus_child = 1;
5912 } else
5913 *new_view = ref_view;
5914 break;
5915 case 'g':
5916 case KEY_HOME:
5917 s->selected = 0;
5918 if (s->tree == s->root)
5919 s->first_displayed_entry =
5920 got_object_tree_get_first_entry(s->tree);
5921 else
5922 s->first_displayed_entry = NULL;
5923 break;
5924 case 'G':
5925 case KEY_END:
5926 s->selected = 0;
5927 te = got_object_tree_get_last_entry(s->tree);
5928 for (n = 0; n < view->nlines - 3; n++) {
5929 if (te == NULL) {
5930 if(s->tree != s->root) {
5931 s->first_displayed_entry = NULL;
5932 n++;
5934 break;
5936 s->first_displayed_entry = te;
5937 te = got_tree_entry_get_prev(s->tree, te);
5939 if (n > 0)
5940 s->selected = n - 1;
5941 break;
5942 case 'k':
5943 case KEY_UP:
5944 case CTRL('p'):
5945 if (s->selected > 0) {
5946 s->selected--;
5947 break;
5949 tree_scroll_up(s, 1);
5950 break;
5951 case CTRL('u'):
5952 case 'u':
5953 nscroll /= 2;
5954 /* FALL THROUGH */
5955 case KEY_PPAGE:
5956 case CTRL('b'):
5957 if (s->tree == s->root) {
5958 if (got_object_tree_get_first_entry(s->tree) ==
5959 s->first_displayed_entry)
5960 s->selected -= MIN(s->selected, nscroll);
5961 } else {
5962 if (s->first_displayed_entry == NULL)
5963 s->selected -= MIN(s->selected, nscroll);
5965 tree_scroll_up(s, MAX(0, nscroll));
5966 break;
5967 case 'j':
5968 case KEY_DOWN:
5969 case CTRL('n'):
5970 if (s->selected < s->ndisplayed - 1) {
5971 s->selected++;
5972 break;
5974 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5975 == NULL)
5976 /* can't scroll any further */
5977 break;
5978 tree_scroll_down(s, 1);
5979 break;
5980 case CTRL('d'):
5981 case 'd':
5982 nscroll /= 2;
5983 /* FALL THROUGH */
5984 case KEY_NPAGE:
5985 case CTRL('f'):
5986 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5987 == NULL) {
5988 /* can't scroll any further; move cursor down */
5989 if (s->selected < s->ndisplayed - 1)
5990 s->selected += MIN(nscroll,
5991 s->ndisplayed - s->selected - 1);
5992 break;
5994 tree_scroll_down(s, nscroll);
5995 break;
5996 case KEY_ENTER:
5997 case '\r':
5998 case KEY_BACKSPACE:
5999 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6000 struct tog_parent_tree *parent;
6001 /* user selected '..' */
6002 if (s->tree == s->root)
6003 break;
6004 parent = TAILQ_FIRST(&s->parents);
6005 TAILQ_REMOVE(&s->parents, parent,
6006 entry);
6007 got_object_tree_close(s->tree);
6008 s->tree = parent->tree;
6009 s->first_displayed_entry =
6010 parent->first_displayed_entry;
6011 s->selected_entry =
6012 parent->selected_entry;
6013 s->selected = parent->selected;
6014 free(parent);
6015 } else if (S_ISDIR(got_tree_entry_get_mode(
6016 s->selected_entry))) {
6017 struct got_tree_object *subtree;
6018 err = got_object_open_as_tree(&subtree, s->repo,
6019 got_tree_entry_get_id(s->selected_entry));
6020 if (err)
6021 break;
6022 err = tree_view_visit_subtree(s, subtree);
6023 if (err) {
6024 got_object_tree_close(subtree);
6025 break;
6027 } else if (S_ISREG(got_tree_entry_get_mode(
6028 s->selected_entry))) {
6029 struct tog_view *blame_view;
6030 int begin_x = view_is_parent_view(view) ?
6031 view_split_begin_x(view->begin_x) : 0;
6033 err = blame_tree_entry(&blame_view, begin_x,
6034 s->selected_entry, &s->parents,
6035 s->commit_id, s->repo);
6036 if (err)
6037 break;
6038 view->focussed = 0;
6039 blame_view->focussed = 1;
6040 if (view_is_parent_view(view)) {
6041 err = view_close_child(view);
6042 if (err)
6043 return err;
6044 err = view_set_child(view, blame_view);
6045 if (err)
6046 return err;
6047 view->focus_child = 1;
6048 } else
6049 *new_view = blame_view;
6051 break;
6052 case KEY_RESIZE:
6053 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6054 s->selected = view->nlines - 4;
6055 break;
6056 default:
6057 break;
6060 return err;
6063 __dead static void
6064 usage_tree(void)
6066 endwin();
6067 fprintf(stderr,
6068 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6069 getprogname());
6070 exit(1);
6073 static const struct got_error *
6074 cmd_tree(int argc, char *argv[])
6076 const struct got_error *error;
6077 struct got_repository *repo = NULL;
6078 struct got_worktree *worktree = NULL;
6079 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6080 struct got_object_id *commit_id = NULL;
6081 struct got_commit_object *commit = NULL;
6082 const char *commit_id_arg = NULL;
6083 char *label = NULL;
6084 struct got_reference *ref = NULL;
6085 const char *head_ref_name = NULL;
6086 int ch;
6087 struct tog_view *view;
6088 int *pack_fds = NULL;
6090 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6091 switch (ch) {
6092 case 'c':
6093 commit_id_arg = optarg;
6094 break;
6095 case 'r':
6096 repo_path = realpath(optarg, NULL);
6097 if (repo_path == NULL)
6098 return got_error_from_errno2("realpath",
6099 optarg);
6100 break;
6101 default:
6102 usage_tree();
6103 /* NOTREACHED */
6107 argc -= optind;
6108 argv += optind;
6110 if (argc > 1)
6111 usage_tree();
6113 error = got_repo_pack_fds_open(&pack_fds);
6114 if (error != NULL)
6115 goto done;
6117 if (repo_path == NULL) {
6118 cwd = getcwd(NULL, 0);
6119 if (cwd == NULL)
6120 return got_error_from_errno("getcwd");
6121 error = got_worktree_open(&worktree, cwd);
6122 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6123 goto done;
6124 if (worktree)
6125 repo_path =
6126 strdup(got_worktree_get_repo_path(worktree));
6127 else
6128 repo_path = strdup(cwd);
6129 if (repo_path == NULL) {
6130 error = got_error_from_errno("strdup");
6131 goto done;
6135 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6136 if (error != NULL)
6137 goto done;
6139 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6140 repo, worktree);
6141 if (error)
6142 goto done;
6144 init_curses();
6146 error = apply_unveil(got_repo_get_path(repo), NULL);
6147 if (error)
6148 goto done;
6150 error = tog_load_refs(repo, 0);
6151 if (error)
6152 goto done;
6154 if (commit_id_arg == NULL) {
6155 error = got_repo_match_object_id(&commit_id, &label,
6156 worktree ? got_worktree_get_head_ref_name(worktree) :
6157 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6158 if (error)
6159 goto done;
6160 head_ref_name = label;
6161 } else {
6162 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6163 if (error == NULL)
6164 head_ref_name = got_ref_get_name(ref);
6165 else if (error->code != GOT_ERR_NOT_REF)
6166 goto done;
6167 error = got_repo_match_object_id(&commit_id, NULL,
6168 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6169 if (error)
6170 goto done;
6173 error = got_object_open_as_commit(&commit, repo, commit_id);
6174 if (error)
6175 goto done;
6177 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6178 if (view == NULL) {
6179 error = got_error_from_errno("view_open");
6180 goto done;
6182 error = open_tree_view(view, commit_id, head_ref_name, repo);
6183 if (error)
6184 goto done;
6185 if (!got_path_is_root_dir(in_repo_path)) {
6186 error = tree_view_walk_path(&view->state.tree, commit,
6187 in_repo_path);
6188 if (error)
6189 goto done;
6192 if (worktree) {
6193 /* Release work tree lock. */
6194 got_worktree_close(worktree);
6195 worktree = NULL;
6197 error = view_loop(view);
6198 done:
6199 free(repo_path);
6200 free(cwd);
6201 free(commit_id);
6202 free(label);
6203 if (ref)
6204 got_ref_close(ref);
6205 if (repo) {
6206 const struct got_error *close_err = got_repo_close(repo);
6207 if (error == NULL)
6208 error = close_err;
6210 if (pack_fds) {
6211 const struct got_error *pack_err =
6212 got_repo_pack_fds_close(pack_fds);
6213 if (error == NULL)
6214 error = pack_err;
6216 tog_free_refs();
6217 return error;
6220 static const struct got_error *
6221 ref_view_load_refs(struct tog_ref_view_state *s)
6223 struct got_reflist_entry *sre;
6224 struct tog_reflist_entry *re;
6226 s->nrefs = 0;
6227 TAILQ_FOREACH(sre, &tog_refs, entry) {
6228 if (strncmp(got_ref_get_name(sre->ref),
6229 "refs/got/", 9) == 0 &&
6230 strncmp(got_ref_get_name(sre->ref),
6231 "refs/got/backup/", 16) != 0)
6232 continue;
6234 re = malloc(sizeof(*re));
6235 if (re == NULL)
6236 return got_error_from_errno("malloc");
6238 re->ref = got_ref_dup(sre->ref);
6239 if (re->ref == NULL)
6240 return got_error_from_errno("got_ref_dup");
6241 re->idx = s->nrefs++;
6242 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6245 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6246 return NULL;
6249 void
6250 ref_view_free_refs(struct tog_ref_view_state *s)
6252 struct tog_reflist_entry *re;
6254 while (!TAILQ_EMPTY(&s->refs)) {
6255 re = TAILQ_FIRST(&s->refs);
6256 TAILQ_REMOVE(&s->refs, re, entry);
6257 got_ref_close(re->ref);
6258 free(re);
6262 static const struct got_error *
6263 open_ref_view(struct tog_view *view, struct got_repository *repo)
6265 const struct got_error *err = NULL;
6266 struct tog_ref_view_state *s = &view->state.ref;
6268 s->selected_entry = 0;
6269 s->repo = repo;
6271 TAILQ_INIT(&s->refs);
6272 STAILQ_INIT(&s->colors);
6274 err = ref_view_load_refs(s);
6275 if (err)
6276 return err;
6278 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6279 err = add_color(&s->colors, "^refs/heads/",
6280 TOG_COLOR_REFS_HEADS,
6281 get_color_value("TOG_COLOR_REFS_HEADS"));
6282 if (err)
6283 goto done;
6285 err = add_color(&s->colors, "^refs/tags/",
6286 TOG_COLOR_REFS_TAGS,
6287 get_color_value("TOG_COLOR_REFS_TAGS"));
6288 if (err)
6289 goto done;
6291 err = add_color(&s->colors, "^refs/remotes/",
6292 TOG_COLOR_REFS_REMOTES,
6293 get_color_value("TOG_COLOR_REFS_REMOTES"));
6294 if (err)
6295 goto done;
6297 err = add_color(&s->colors, "^refs/got/backup/",
6298 TOG_COLOR_REFS_BACKUP,
6299 get_color_value("TOG_COLOR_REFS_BACKUP"));
6300 if (err)
6301 goto done;
6304 view->show = show_ref_view;
6305 view->input = input_ref_view;
6306 view->close = close_ref_view;
6307 view->search_start = search_start_ref_view;
6308 view->search_next = search_next_ref_view;
6309 done:
6310 if (err)
6311 free_colors(&s->colors);
6312 return err;
6315 static const struct got_error *
6316 close_ref_view(struct tog_view *view)
6318 struct tog_ref_view_state *s = &view->state.ref;
6320 ref_view_free_refs(s);
6321 free_colors(&s->colors);
6323 return NULL;
6326 static const struct got_error *
6327 resolve_reflist_entry(struct got_object_id **commit_id,
6328 struct tog_reflist_entry *re, struct got_repository *repo)
6330 const struct got_error *err = NULL;
6331 struct got_object_id *obj_id;
6332 struct got_tag_object *tag = NULL;
6333 int obj_type;
6335 *commit_id = NULL;
6337 err = got_ref_resolve(&obj_id, repo, re->ref);
6338 if (err)
6339 return err;
6341 err = got_object_get_type(&obj_type, repo, obj_id);
6342 if (err)
6343 goto done;
6345 switch (obj_type) {
6346 case GOT_OBJ_TYPE_COMMIT:
6347 *commit_id = obj_id;
6348 break;
6349 case GOT_OBJ_TYPE_TAG:
6350 err = got_object_open_as_tag(&tag, repo, obj_id);
6351 if (err)
6352 goto done;
6353 free(obj_id);
6354 err = got_object_get_type(&obj_type, repo,
6355 got_object_tag_get_object_id(tag));
6356 if (err)
6357 goto done;
6358 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6359 err = got_error(GOT_ERR_OBJ_TYPE);
6360 goto done;
6362 *commit_id = got_object_id_dup(
6363 got_object_tag_get_object_id(tag));
6364 if (*commit_id == NULL) {
6365 err = got_error_from_errno("got_object_id_dup");
6366 goto done;
6368 break;
6369 default:
6370 err = got_error(GOT_ERR_OBJ_TYPE);
6371 break;
6374 done:
6375 if (tag)
6376 got_object_tag_close(tag);
6377 if (err) {
6378 free(*commit_id);
6379 *commit_id = NULL;
6381 return err;
6384 static const struct got_error *
6385 log_ref_entry(struct tog_view **new_view, int begin_x,
6386 struct tog_reflist_entry *re, struct got_repository *repo)
6388 struct tog_view *log_view;
6389 const struct got_error *err = NULL;
6390 struct got_object_id *commit_id = NULL;
6392 *new_view = NULL;
6394 err = resolve_reflist_entry(&commit_id, re, repo);
6395 if (err) {
6396 if (err->code != GOT_ERR_OBJ_TYPE)
6397 return err;
6398 else
6399 return NULL;
6402 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6403 if (log_view == NULL) {
6404 err = got_error_from_errno("view_open");
6405 goto done;
6408 err = open_log_view(log_view, commit_id, repo,
6409 got_ref_get_name(re->ref), "", 0);
6410 done:
6411 if (err)
6412 view_close(log_view);
6413 else
6414 *new_view = log_view;
6415 free(commit_id);
6416 return err;
6419 static void
6420 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6422 struct tog_reflist_entry *re;
6423 int i = 0;
6425 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6426 return;
6428 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6429 while (i++ < maxscroll) {
6430 if (re == NULL)
6431 break;
6432 s->first_displayed_entry = re;
6433 re = TAILQ_PREV(re, tog_reflist_head, entry);
6437 static void
6438 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6440 struct tog_reflist_entry *next, *last;
6441 int n = 0;
6443 if (s->first_displayed_entry)
6444 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6445 else
6446 next = TAILQ_FIRST(&s->refs);
6448 last = s->last_displayed_entry;
6449 while (next && last && n++ < maxscroll) {
6450 last = TAILQ_NEXT(last, entry);
6451 if (last) {
6452 s->first_displayed_entry = next;
6453 next = TAILQ_NEXT(next, entry);
6458 static const struct got_error *
6459 search_start_ref_view(struct tog_view *view)
6461 struct tog_ref_view_state *s = &view->state.ref;
6463 s->matched_entry = NULL;
6464 return NULL;
6467 static int
6468 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6470 regmatch_t regmatch;
6472 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6473 0) == 0;
6476 static const struct got_error *
6477 search_next_ref_view(struct tog_view *view)
6479 struct tog_ref_view_state *s = &view->state.ref;
6480 struct tog_reflist_entry *re = NULL;
6482 if (!view->searching) {
6483 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6484 return NULL;
6487 if (s->matched_entry) {
6488 if (view->searching == TOG_SEARCH_FORWARD) {
6489 if (s->selected_entry)
6490 re = TAILQ_NEXT(s->selected_entry, entry);
6491 else
6492 re = TAILQ_PREV(s->selected_entry,
6493 tog_reflist_head, entry);
6494 } else {
6495 if (s->selected_entry == NULL)
6496 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6497 else
6498 re = TAILQ_PREV(s->selected_entry,
6499 tog_reflist_head, entry);
6501 } else {
6502 if (s->selected_entry)
6503 re = s->selected_entry;
6504 else if (view->searching == TOG_SEARCH_FORWARD)
6505 re = TAILQ_FIRST(&s->refs);
6506 else
6507 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6510 while (1) {
6511 if (re == NULL) {
6512 if (s->matched_entry == NULL) {
6513 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6514 return NULL;
6516 if (view->searching == TOG_SEARCH_FORWARD)
6517 re = TAILQ_FIRST(&s->refs);
6518 else
6519 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6522 if (match_reflist_entry(re, &view->regex)) {
6523 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6524 s->matched_entry = re;
6525 break;
6528 if (view->searching == TOG_SEARCH_FORWARD)
6529 re = TAILQ_NEXT(re, entry);
6530 else
6531 re = TAILQ_PREV(re, tog_reflist_head, entry);
6534 if (s->matched_entry) {
6535 s->first_displayed_entry = s->matched_entry;
6536 s->selected = 0;
6539 return NULL;
6542 static const struct got_error *
6543 show_ref_view(struct tog_view *view)
6545 const struct got_error *err = NULL;
6546 struct tog_ref_view_state *s = &view->state.ref;
6547 struct tog_reflist_entry *re;
6548 char *line = NULL;
6549 wchar_t *wline;
6550 struct tog_color *tc;
6551 int width, n;
6552 int limit = view->nlines;
6554 werase(view->window);
6556 s->ndisplayed = 0;
6558 if (limit == 0)
6559 return NULL;
6561 re = s->first_displayed_entry;
6563 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6564 s->nrefs) == -1)
6565 return got_error_from_errno("asprintf");
6567 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6568 if (err) {
6569 free(line);
6570 return err;
6572 if (view_needs_focus_indication(view))
6573 wstandout(view->window);
6574 waddwstr(view->window, wline);
6575 if (view_needs_focus_indication(view))
6576 wstandend(view->window);
6577 free(wline);
6578 wline = NULL;
6579 free(line);
6580 line = NULL;
6581 if (width < view->ncols - 1)
6582 waddch(view->window, '\n');
6583 if (--limit <= 0)
6584 return NULL;
6586 n = 0;
6587 while (re && limit > 0) {
6588 char *line = NULL;
6589 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6591 if (s->show_date) {
6592 struct got_commit_object *ci;
6593 struct got_tag_object *tag;
6594 struct got_object_id *id;
6595 struct tm tm;
6596 time_t t;
6598 err = got_ref_resolve(&id, s->repo, re->ref);
6599 if (err)
6600 return err;
6601 err = got_object_open_as_tag(&tag, s->repo, id);
6602 if (err) {
6603 if (err->code != GOT_ERR_OBJ_TYPE) {
6604 free(id);
6605 return err;
6607 err = got_object_open_as_commit(&ci, s->repo,
6608 id);
6609 if (err) {
6610 free(id);
6611 return err;
6613 t = got_object_commit_get_committer_time(ci);
6614 got_object_commit_close(ci);
6615 } else {
6616 t = got_object_tag_get_tagger_time(tag);
6617 got_object_tag_close(tag);
6619 free(id);
6620 if (gmtime_r(&t, &tm) == NULL)
6621 return got_error_from_errno("gmtime_r");
6622 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6623 return got_error(GOT_ERR_NO_SPACE);
6625 if (got_ref_is_symbolic(re->ref)) {
6626 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6627 ymd : "", got_ref_get_name(re->ref),
6628 got_ref_get_symref_target(re->ref)) == -1)
6629 return got_error_from_errno("asprintf");
6630 } else if (s->show_ids) {
6631 struct got_object_id *id;
6632 char *id_str;
6633 err = got_ref_resolve(&id, s->repo, re->ref);
6634 if (err)
6635 return err;
6636 err = got_object_id_str(&id_str, id);
6637 if (err) {
6638 free(id);
6639 return err;
6641 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6642 got_ref_get_name(re->ref), id_str) == -1) {
6643 err = got_error_from_errno("asprintf");
6644 free(id);
6645 free(id_str);
6646 return err;
6648 free(id);
6649 free(id_str);
6650 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6651 got_ref_get_name(re->ref)) == -1)
6652 return got_error_from_errno("asprintf");
6654 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6655 0, 0);
6656 if (err) {
6657 free(line);
6658 return err;
6660 if (n == s->selected) {
6661 if (view->focussed)
6662 wstandout(view->window);
6663 s->selected_entry = re;
6665 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6666 if (tc)
6667 wattr_on(view->window,
6668 COLOR_PAIR(tc->colorpair), NULL);
6669 waddwstr(view->window, wline);
6670 if (tc)
6671 wattr_off(view->window,
6672 COLOR_PAIR(tc->colorpair), NULL);
6673 if (width < view->ncols - 1)
6674 waddch(view->window, '\n');
6675 if (n == s->selected && view->focussed)
6676 wstandend(view->window);
6677 free(line);
6678 free(wline);
6679 wline = NULL;
6680 n++;
6681 s->ndisplayed++;
6682 s->last_displayed_entry = re;
6684 limit--;
6685 re = TAILQ_NEXT(re, entry);
6688 view_vborder(view);
6689 return err;
6692 static const struct got_error *
6693 browse_ref_tree(struct tog_view **new_view, int begin_x,
6694 struct tog_reflist_entry *re, struct got_repository *repo)
6696 const struct got_error *err = NULL;
6697 struct got_object_id *commit_id = NULL;
6698 struct tog_view *tree_view;
6700 *new_view = NULL;
6702 err = resolve_reflist_entry(&commit_id, re, repo);
6703 if (err) {
6704 if (err->code != GOT_ERR_OBJ_TYPE)
6705 return err;
6706 else
6707 return NULL;
6711 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6712 if (tree_view == NULL) {
6713 err = got_error_from_errno("view_open");
6714 goto done;
6717 err = open_tree_view(tree_view, commit_id,
6718 got_ref_get_name(re->ref), repo);
6719 if (err)
6720 goto done;
6722 *new_view = tree_view;
6723 done:
6724 free(commit_id);
6725 return err;
6727 static const struct got_error *
6728 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6730 const struct got_error *err = NULL;
6731 struct tog_ref_view_state *s = &view->state.ref;
6732 struct tog_view *log_view, *tree_view;
6733 struct tog_reflist_entry *re;
6734 int begin_x = 0, n, nscroll = view->nlines - 1;
6736 switch (ch) {
6737 case 'i':
6738 s->show_ids = !s->show_ids;
6739 break;
6740 case 'm':
6741 s->show_date = !s->show_date;
6742 break;
6743 case 'o':
6744 s->sort_by_date = !s->sort_by_date;
6745 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6746 got_ref_cmp_by_commit_timestamp_descending :
6747 tog_ref_cmp_by_name, s->repo);
6748 if (err)
6749 break;
6750 got_reflist_object_id_map_free(tog_refs_idmap);
6751 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6752 &tog_refs, s->repo);
6753 if (err)
6754 break;
6755 ref_view_free_refs(s);
6756 err = ref_view_load_refs(s);
6757 break;
6758 case KEY_ENTER:
6759 case '\r':
6760 if (!s->selected_entry)
6761 break;
6762 if (view_is_parent_view(view))
6763 begin_x = view_split_begin_x(view->begin_x);
6764 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6765 s->repo);
6766 view->focussed = 0;
6767 log_view->focussed = 1;
6768 if (view_is_parent_view(view)) {
6769 err = view_close_child(view);
6770 if (err)
6771 return err;
6772 err = view_set_child(view, log_view);
6773 if (err)
6774 return err;
6775 view->focus_child = 1;
6776 } else
6777 *new_view = log_view;
6778 break;
6779 case 't':
6780 if (!s->selected_entry)
6781 break;
6782 if (view_is_parent_view(view))
6783 begin_x = view_split_begin_x(view->begin_x);
6784 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6785 s->repo);
6786 if (err || tree_view == NULL)
6787 break;
6788 view->focussed = 0;
6789 tree_view->focussed = 1;
6790 if (view_is_parent_view(view)) {
6791 err = view_close_child(view);
6792 if (err)
6793 return err;
6794 err = view_set_child(view, tree_view);
6795 if (err)
6796 return err;
6797 view->focus_child = 1;
6798 } else
6799 *new_view = tree_view;
6800 break;
6801 case 'g':
6802 case KEY_HOME:
6803 s->selected = 0;
6804 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6805 break;
6806 case 'G':
6807 case KEY_END:
6808 s->selected = 0;
6809 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6810 for (n = 0; n < view->nlines - 1; n++) {
6811 if (re == NULL)
6812 break;
6813 s->first_displayed_entry = re;
6814 re = TAILQ_PREV(re, tog_reflist_head, entry);
6816 if (n > 0)
6817 s->selected = n - 1;
6818 break;
6819 case 'k':
6820 case KEY_UP:
6821 case CTRL('p'):
6822 if (s->selected > 0) {
6823 s->selected--;
6824 break;
6826 ref_scroll_up(s, 1);
6827 break;
6828 case CTRL('u'):
6829 case 'u':
6830 nscroll /= 2;
6831 /* FALL THROUGH */
6832 case KEY_PPAGE:
6833 case CTRL('b'):
6834 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6835 s->selected -= MIN(nscroll, s->selected);
6836 ref_scroll_up(s, MAX(0, nscroll));
6837 break;
6838 case 'j':
6839 case KEY_DOWN:
6840 case CTRL('n'):
6841 if (s->selected < s->ndisplayed - 1) {
6842 s->selected++;
6843 break;
6845 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6846 /* can't scroll any further */
6847 break;
6848 ref_scroll_down(s, 1);
6849 break;
6850 case CTRL('d'):
6851 case 'd':
6852 nscroll /= 2;
6853 /* FALL THROUGH */
6854 case KEY_NPAGE:
6855 case CTRL('f'):
6856 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6857 /* can't scroll any further; move cursor down */
6858 if (s->selected < s->ndisplayed - 1)
6859 s->selected += MIN(nscroll,
6860 s->ndisplayed - s->selected - 1);
6861 break;
6863 ref_scroll_down(s, nscroll);
6864 break;
6865 case CTRL('l'):
6866 tog_free_refs();
6867 err = tog_load_refs(s->repo, s->sort_by_date);
6868 if (err)
6869 break;
6870 ref_view_free_refs(s);
6871 err = ref_view_load_refs(s);
6872 break;
6873 case KEY_RESIZE:
6874 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6875 s->selected = view->nlines - 2;
6876 break;
6877 default:
6878 break;
6881 return err;
6884 __dead static void
6885 usage_ref(void)
6887 endwin();
6888 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6889 getprogname());
6890 exit(1);
6893 static const struct got_error *
6894 cmd_ref(int argc, char *argv[])
6896 const struct got_error *error;
6897 struct got_repository *repo = NULL;
6898 struct got_worktree *worktree = NULL;
6899 char *cwd = NULL, *repo_path = NULL;
6900 int ch;
6901 struct tog_view *view;
6902 int *pack_fds = NULL;
6904 while ((ch = getopt(argc, argv, "r:")) != -1) {
6905 switch (ch) {
6906 case 'r':
6907 repo_path = realpath(optarg, NULL);
6908 if (repo_path == NULL)
6909 return got_error_from_errno2("realpath",
6910 optarg);
6911 break;
6912 default:
6913 usage_ref();
6914 /* NOTREACHED */
6918 argc -= optind;
6919 argv += optind;
6921 if (argc > 1)
6922 usage_ref();
6924 error = got_repo_pack_fds_open(&pack_fds);
6925 if (error != NULL)
6926 goto done;
6928 if (repo_path == NULL) {
6929 cwd = getcwd(NULL, 0);
6930 if (cwd == NULL)
6931 return got_error_from_errno("getcwd");
6932 error = got_worktree_open(&worktree, cwd);
6933 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6934 goto done;
6935 if (worktree)
6936 repo_path =
6937 strdup(got_worktree_get_repo_path(worktree));
6938 else
6939 repo_path = strdup(cwd);
6940 if (repo_path == NULL) {
6941 error = got_error_from_errno("strdup");
6942 goto done;
6946 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6947 if (error != NULL)
6948 goto done;
6950 init_curses();
6952 error = apply_unveil(got_repo_get_path(repo), NULL);
6953 if (error)
6954 goto done;
6956 error = tog_load_refs(repo, 0);
6957 if (error)
6958 goto done;
6960 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6961 if (view == NULL) {
6962 error = got_error_from_errno("view_open");
6963 goto done;
6966 error = open_ref_view(view, repo);
6967 if (error)
6968 goto done;
6970 if (worktree) {
6971 /* Release work tree lock. */
6972 got_worktree_close(worktree);
6973 worktree = NULL;
6975 error = view_loop(view);
6976 done:
6977 free(repo_path);
6978 free(cwd);
6979 if (repo) {
6980 const struct got_error *close_err = got_repo_close(repo);
6981 if (close_err)
6982 error = close_err;
6984 if (pack_fds) {
6985 const struct got_error *pack_err =
6986 got_repo_pack_fds_close(pack_fds);
6987 if (error == NULL)
6988 error = pack_err;
6990 tog_free_refs();
6991 return error;
6994 static void
6995 list_commands(FILE *fp)
6997 size_t i;
6999 fprintf(fp, "commands:");
7000 for (i = 0; i < nitems(tog_commands); i++) {
7001 const struct tog_cmd *cmd = &tog_commands[i];
7002 fprintf(fp, " %s", cmd->name);
7004 fputc('\n', fp);
7007 __dead static void
7008 usage(int hflag, int status)
7010 FILE *fp = (status == 0) ? stdout : stderr;
7012 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7013 getprogname());
7014 if (hflag) {
7015 fprintf(fp, "lazy usage: %s path\n", getprogname());
7016 list_commands(fp);
7018 exit(status);
7021 static char **
7022 make_argv(int argc, ...)
7024 va_list ap;
7025 char **argv;
7026 int i;
7028 va_start(ap, argc);
7030 argv = calloc(argc, sizeof(char *));
7031 if (argv == NULL)
7032 err(1, "calloc");
7033 for (i = 0; i < argc; i++) {
7034 argv[i] = strdup(va_arg(ap, char *));
7035 if (argv[i] == NULL)
7036 err(1, "strdup");
7039 va_end(ap);
7040 return argv;
7044 * Try to convert 'tog path' into a 'tog log path' command.
7045 * The user could simply have mistyped the command rather than knowingly
7046 * provided a path. So check whether argv[0] can in fact be resolved
7047 * to a path in the HEAD commit and print a special error if not.
7048 * This hack is for mpi@ <3
7050 static const struct got_error *
7051 tog_log_with_path(int argc, char *argv[])
7053 const struct got_error *error = NULL, *close_err;
7054 const struct tog_cmd *cmd = NULL;
7055 struct got_repository *repo = NULL;
7056 struct got_worktree *worktree = NULL;
7057 struct got_object_id *commit_id = NULL, *id = NULL;
7058 struct got_commit_object *commit = NULL;
7059 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7060 char *commit_id_str = NULL, **cmd_argv = NULL;
7061 int *pack_fds = NULL;
7063 cwd = getcwd(NULL, 0);
7064 if (cwd == NULL)
7065 return got_error_from_errno("getcwd");
7067 error = got_repo_pack_fds_open(&pack_fds);
7068 if (error != NULL)
7069 goto done;
7071 error = got_worktree_open(&worktree, cwd);
7072 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7073 goto done;
7075 if (worktree)
7076 repo_path = strdup(got_worktree_get_repo_path(worktree));
7077 else
7078 repo_path = strdup(cwd);
7079 if (repo_path == NULL) {
7080 error = got_error_from_errno("strdup");
7081 goto done;
7084 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7085 if (error != NULL)
7086 goto done;
7088 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7089 repo, worktree);
7090 if (error)
7091 goto done;
7093 error = tog_load_refs(repo, 0);
7094 if (error)
7095 goto done;
7096 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7097 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7098 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7099 if (error)
7100 goto done;
7102 if (worktree) {
7103 got_worktree_close(worktree);
7104 worktree = NULL;
7107 error = got_object_open_as_commit(&commit, repo, commit_id);
7108 if (error)
7109 goto done;
7111 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7112 if (error) {
7113 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7114 goto done;
7115 fprintf(stderr, "%s: '%s' is no known command or path\n",
7116 getprogname(), argv[0]);
7117 usage(1, 1);
7118 /* not reached */
7121 close_err = got_repo_close(repo);
7122 if (error == NULL)
7123 error = close_err;
7124 repo = NULL;
7126 error = got_object_id_str(&commit_id_str, commit_id);
7127 if (error)
7128 goto done;
7130 cmd = &tog_commands[0]; /* log */
7131 argc = 4;
7132 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7133 error = cmd->cmd_main(argc, cmd_argv);
7134 done:
7135 if (repo) {
7136 close_err = got_repo_close(repo);
7137 if (error == NULL)
7138 error = close_err;
7140 if (commit)
7141 got_object_commit_close(commit);
7142 if (worktree)
7143 got_worktree_close(worktree);
7144 if (pack_fds) {
7145 const struct got_error *pack_err =
7146 got_repo_pack_fds_close(pack_fds);
7147 if (error == NULL)
7148 error = pack_err;
7150 free(id);
7151 free(commit_id_str);
7152 free(commit_id);
7153 free(cwd);
7154 free(repo_path);
7155 free(in_repo_path);
7156 if (cmd_argv) {
7157 int i;
7158 for (i = 0; i < argc; i++)
7159 free(cmd_argv[i]);
7160 free(cmd_argv);
7162 tog_free_refs();
7163 return error;
7166 int
7167 main(int argc, char *argv[])
7169 const struct got_error *error = NULL;
7170 const struct tog_cmd *cmd = NULL;
7171 int ch, hflag = 0, Vflag = 0;
7172 char **cmd_argv = NULL;
7173 static const struct option longopts[] = {
7174 { "version", no_argument, NULL, 'V' },
7175 { NULL, 0, NULL, 0}
7178 setlocale(LC_CTYPE, "");
7180 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7181 switch (ch) {
7182 case 'h':
7183 hflag = 1;
7184 break;
7185 case 'V':
7186 Vflag = 1;
7187 break;
7188 default:
7189 usage(hflag, 1);
7190 /* NOTREACHED */
7194 argc -= optind;
7195 argv += optind;
7196 optind = 1;
7197 optreset = 1;
7199 if (Vflag) {
7200 got_version_print_str();
7201 return 0;
7204 #ifndef PROFILE
7205 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7206 NULL) == -1)
7207 err(1, "pledge");
7208 #endif
7210 if (argc == 0) {
7211 if (hflag)
7212 usage(hflag, 0);
7213 /* Build an argument vector which runs a default command. */
7214 cmd = &tog_commands[0];
7215 argc = 1;
7216 cmd_argv = make_argv(argc, cmd->name);
7217 } else {
7218 size_t i;
7220 /* Did the user specify a command? */
7221 for (i = 0; i < nitems(tog_commands); i++) {
7222 if (strncmp(tog_commands[i].name, argv[0],
7223 strlen(argv[0])) == 0) {
7224 cmd = &tog_commands[i];
7225 break;
7230 if (cmd == NULL) {
7231 if (argc != 1)
7232 usage(0, 1);
7233 /* No command specified; try log with a path */
7234 error = tog_log_with_path(argc, argv);
7235 } else {
7236 if (hflag)
7237 cmd->cmd_usage();
7238 else
7239 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7242 endwin();
7243 putchar('\n');
7244 if (cmd_argv) {
7245 int i;
7246 for (i = 0; i < argc; i++)
7247 free(cmd_argv[i]);
7248 free(cmd_argv);
7251 if (error && error->code != GOT_ERR_CANCELLED)
7252 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7253 return 0;