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/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED
24 #include <curses.h>
25 #undef _XOPEN_SOURCE_EXTENDED
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 <util.h>
37 #include <limits.h>
38 #include <wchar.h>
39 #include <time.h>
40 #include <pthread.h>
41 #include <libgen.h>
42 #include <regex.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_diff.h"
50 #include "got_opentemp.h"
51 #include "got_utf8.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_path.h"
57 #include "got_worktree.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #ifndef MAX
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #endif
67 #define CTRL(x) ((x) & 0x1f)
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 struct tog_cmd {
74 const char *name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 };
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
98 };
100 enum tog_view_type {
101 TOG_VIEW_DIFF,
102 TOG_VIEW_LOG,
103 TOG_VIEW_BLAME,
104 TOG_VIEW_TREE,
105 TOG_VIEW_REF,
106 };
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
114 int idx;
115 };
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
118 int ncommits;
119 struct commit_queue_head head;
120 };
122 struct tog_color {
123 SIMPLEQ_ENTRY(tog_color) entry;
124 regex_t regex;
125 short colorpair;
126 };
127 SIMPLEQ_HEAD(tog_colors, tog_color);
129 static const struct got_error *
130 add_color(struct tog_colors *colors, const char *pattern,
131 int idx, short color)
133 const struct got_error *err = NULL;
134 struct tog_color *tc;
135 int regerr = 0;
137 if (idx < 1 || idx > COLOR_PAIRS - 1)
138 return NULL;
140 init_pair(idx, color, -1);
142 tc = calloc(1, sizeof(*tc));
143 if (tc == NULL)
144 return got_error_from_errno("calloc");
145 regerr = regcomp(&tc->regex, pattern,
146 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
147 if (regerr) {
148 static char regerr_msg[512];
149 static char err_msg[512];
150 regerror(regerr, &tc->regex, regerr_msg,
151 sizeof(regerr_msg));
152 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
153 regerr_msg);
154 err = got_error_msg(GOT_ERR_REGEX, err_msg);
155 free(tc);
156 return err;
158 tc->colorpair = idx;
159 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
160 return NULL;
163 static void
164 free_colors(struct tog_colors *colors)
166 struct tog_color *tc;
168 while (!SIMPLEQ_EMPTY(colors)) {
169 tc = SIMPLEQ_FIRST(colors);
170 SIMPLEQ_REMOVE_HEAD(colors, entry);
171 regfree(&tc->regex);
172 free(tc);
176 struct tog_color *
177 get_color(struct tog_colors *colors, int colorpair)
179 struct tog_color *tc = NULL;
181 SIMPLEQ_FOREACH(tc, colors, entry) {
182 if (tc->colorpair == colorpair)
183 return tc;
186 return NULL;
189 static int
190 default_color_value(const char *envvar)
192 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
193 return COLOR_MAGENTA;
194 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
195 return COLOR_CYAN;
196 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
197 return COLOR_YELLOW;
198 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
199 return COLOR_GREEN;
200 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
201 return COLOR_MAGENTA;
202 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
203 return COLOR_MAGENTA;
204 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
205 return COLOR_CYAN;
206 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
207 return COLOR_GREEN;
208 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
209 return COLOR_GREEN;
210 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
211 return COLOR_CYAN;
212 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
213 return COLOR_YELLOW;
214 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
215 return COLOR_GREEN;
216 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
217 return COLOR_MAGENTA;
218 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
219 return COLOR_YELLOW;
221 return -1;
224 static int
225 get_color_value(const char *envvar)
227 const char *val = getenv(envvar);
229 if (val == NULL)
230 return default_color_value(envvar);
232 if (strcasecmp(val, "black") == 0)
233 return COLOR_BLACK;
234 if (strcasecmp(val, "red") == 0)
235 return COLOR_RED;
236 if (strcasecmp(val, "green") == 0)
237 return COLOR_GREEN;
238 if (strcasecmp(val, "yellow") == 0)
239 return COLOR_YELLOW;
240 if (strcasecmp(val, "blue") == 0)
241 return COLOR_BLUE;
242 if (strcasecmp(val, "magenta") == 0)
243 return COLOR_MAGENTA;
244 if (strcasecmp(val, "cyan") == 0)
245 return COLOR_CYAN;
246 if (strcasecmp(val, "white") == 0)
247 return COLOR_WHITE;
248 if (strcasecmp(val, "default") == 0)
249 return -1;
251 return default_color_value(envvar);
255 struct tog_diff_view_state {
256 struct got_object_id *id1, *id2;
257 const char *label1, *label2;
258 FILE *f;
259 int first_displayed_line;
260 int last_displayed_line;
261 int eof;
262 int diff_context;
263 int ignore_whitespace;
264 int force_text_diff;
265 struct got_repository *repo;
266 struct got_reflist_head refs;
267 struct tog_colors colors;
268 size_t nlines;
269 off_t *line_offsets;
270 int matched_line;
271 int selected_line;
273 /* passed from log view; may be NULL */
274 struct tog_view *log_view;
275 };
277 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
279 struct tog_log_thread_args {
280 pthread_cond_t need_commits;
281 pthread_cond_t commit_loaded;
282 int commits_needed;
283 struct got_commit_graph *graph;
284 struct commit_queue *commits;
285 const char *in_repo_path;
286 struct got_object_id *start_id;
287 struct got_repository *repo;
288 int log_complete;
289 sig_atomic_t *quit;
290 struct commit_queue_entry **first_displayed_entry;
291 struct commit_queue_entry **selected_entry;
292 int *searching;
293 int *search_next_done;
294 regex_t *regex;
295 };
297 struct tog_log_view_state {
298 struct commit_queue commits;
299 struct commit_queue_entry *first_displayed_entry;
300 struct commit_queue_entry *last_displayed_entry;
301 struct commit_queue_entry *selected_entry;
302 int selected;
303 char *in_repo_path;
304 const char *head_ref_name;
305 int log_branches;
306 struct got_repository *repo;
307 struct got_reflist_head refs;
308 struct got_object_id *start_id;
309 sig_atomic_t quit;
310 pthread_t thread;
311 struct tog_log_thread_args thread_args;
312 struct commit_queue_entry *matched_entry;
313 struct commit_queue_entry *search_entry;
314 struct tog_colors colors;
315 };
317 #define TOG_COLOR_DIFF_MINUS 1
318 #define TOG_COLOR_DIFF_PLUS 2
319 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
320 #define TOG_COLOR_DIFF_META 4
321 #define TOG_COLOR_TREE_SUBMODULE 5
322 #define TOG_COLOR_TREE_SYMLINK 6
323 #define TOG_COLOR_TREE_DIRECTORY 7
324 #define TOG_COLOR_TREE_EXECUTABLE 8
325 #define TOG_COLOR_COMMIT 9
326 #define TOG_COLOR_AUTHOR 10
327 #define TOG_COLOR_DATE 11
328 #define TOG_COLOR_REFS_HEADS 12
329 #define TOG_COLOR_REFS_TAGS 13
330 #define TOG_COLOR_REFS_REMOTES 14
332 struct tog_blame_cb_args {
333 struct tog_blame_line *lines; /* one per line */
334 int nlines;
336 struct tog_view *view;
337 struct got_object_id *commit_id;
338 int *quit;
339 };
341 struct tog_blame_thread_args {
342 const char *path;
343 struct got_repository *repo;
344 struct tog_blame_cb_args *cb_args;
345 int *complete;
346 got_cancel_cb cancel_cb;
347 void *cancel_arg;
348 };
350 struct tog_blame {
351 FILE *f;
352 off_t filesize;
353 struct tog_blame_line *lines;
354 int nlines;
355 off_t *line_offsets;
356 pthread_t thread;
357 struct tog_blame_thread_args thread_args;
358 struct tog_blame_cb_args cb_args;
359 const char *path;
360 };
362 struct tog_blame_view_state {
363 int first_displayed_line;
364 int last_displayed_line;
365 int selected_line;
366 int blame_complete;
367 int eof;
368 int done;
369 struct got_object_id_queue blamed_commits;
370 struct got_object_qid *blamed_commit;
371 char *path;
372 struct got_repository *repo;
373 struct got_object_id *commit_id;
374 struct tog_blame blame;
375 int matched_line;
376 struct tog_colors colors;
377 };
379 struct tog_parent_tree {
380 TAILQ_ENTRY(tog_parent_tree) entry;
381 struct got_tree_object *tree;
382 struct got_tree_entry *first_displayed_entry;
383 struct got_tree_entry *selected_entry;
384 int selected;
385 };
387 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
389 struct tog_tree_view_state {
390 char *tree_label;
391 struct got_tree_object *root;
392 struct got_tree_object *tree;
393 struct got_tree_entry *first_displayed_entry;
394 struct got_tree_entry *last_displayed_entry;
395 struct got_tree_entry *selected_entry;
396 int ndisplayed, selected, show_ids;
397 struct tog_parent_trees parents;
398 struct got_object_id *commit_id;
399 struct got_repository *repo;
400 struct got_tree_entry *matched_entry;
401 struct tog_colors colors;
402 };
404 struct tog_reflist_entry {
405 TAILQ_ENTRY(tog_reflist_entry) entry;
406 struct got_reference *ref;
407 int idx;
408 };
410 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
412 struct tog_ref_view_state {
413 struct got_reflist_head simplerefs; /* SIMPLEQ */
414 struct tog_reflist_head refs; /* TAILQ */
415 struct tog_reflist_entry *first_displayed_entry;
416 struct tog_reflist_entry *last_displayed_entry;
417 struct tog_reflist_entry *selected_entry;
418 int nrefs, ndisplayed, selected, show_ids;
419 struct got_repository *repo;
420 struct tog_reflist_entry *matched_entry;
421 struct tog_colors colors;
422 };
424 /*
425 * We implement two types of views: parent views and child views.
427 * The 'Tab' key switches between a parent view and its child view.
428 * Child views are shown side-by-side to their parent view, provided
429 * there is enough screen estate.
431 * When a new view is opened from within a parent view, this new view
432 * becomes a child view of the parent view, replacing any existing child.
434 * When a new view is opened from within a child view, this new view
435 * becomes a parent view which will obscure the views below until the
436 * user quits the new parent view by typing 'q'.
438 * This list of views contains parent views only.
439 * Child views are only pointed to by their parent view.
440 */
441 TAILQ_HEAD(tog_view_list_head, tog_view);
443 struct tog_view {
444 TAILQ_ENTRY(tog_view) entry;
445 WINDOW *window;
446 PANEL *panel;
447 int nlines, ncols, begin_y, begin_x;
448 int lines, cols; /* copies of LINES and COLS */
449 int focussed;
450 struct tog_view *parent;
451 struct tog_view *child;
452 int child_focussed;
454 /* type-specific state */
455 enum tog_view_type type;
456 union {
457 struct tog_diff_view_state diff;
458 struct tog_log_view_state log;
459 struct tog_blame_view_state blame;
460 struct tog_tree_view_state tree;
461 struct tog_ref_view_state ref;
462 } state;
464 const struct got_error *(*show)(struct tog_view *);
465 const struct got_error *(*input)(struct tog_view **,
466 struct tog_view **, struct tog_view**, struct tog_view *, int);
467 const struct got_error *(*close)(struct tog_view *);
469 const struct got_error *(*search_start)(struct tog_view *);
470 const struct got_error *(*search_next)(struct tog_view *);
471 int searching;
472 #define TOG_SEARCH_FORWARD 1
473 #define TOG_SEARCH_BACKWARD 2
474 int search_next_done;
475 #define TOG_SEARCH_HAVE_MORE 1
476 #define TOG_SEARCH_NO_MORE 2
477 #define TOG_SEARCH_HAVE_NONE 3
478 regex_t regex;
479 regmatch_t regmatch;
480 };
482 static const struct got_error *open_diff_view(struct tog_view *,
483 struct got_object_id *, struct got_object_id *,
484 const char *, const char *, int, int, int, struct tog_view *,
485 struct got_repository *);
486 static const struct got_error *show_diff_view(struct tog_view *);
487 static const struct got_error *input_diff_view(struct tog_view **,
488 struct tog_view **, struct tog_view **, struct tog_view *, int);
489 static const struct got_error* close_diff_view(struct tog_view *);
490 static const struct got_error *search_start_diff_view(struct tog_view *);
491 static const struct got_error *search_next_diff_view(struct tog_view *);
493 static const struct got_error *open_log_view(struct tog_view *,
494 struct got_object_id *, struct got_repository *,
495 const char *, const char *, int);
496 static const struct got_error * show_log_view(struct tog_view *);
497 static const struct got_error *input_log_view(struct tog_view **,
498 struct tog_view **, struct tog_view **, struct tog_view *, int);
499 static const struct got_error *close_log_view(struct tog_view *);
500 static const struct got_error *search_start_log_view(struct tog_view *);
501 static const struct got_error *search_next_log_view(struct tog_view *);
503 static const struct got_error *open_blame_view(struct tog_view *, char *,
504 struct got_object_id *, struct got_repository *);
505 static const struct got_error *show_blame_view(struct tog_view *);
506 static const struct got_error *input_blame_view(struct tog_view **,
507 struct tog_view **, struct tog_view **, struct tog_view *, int);
508 static const struct got_error *close_blame_view(struct tog_view *);
509 static const struct got_error *search_start_blame_view(struct tog_view *);
510 static const struct got_error *search_next_blame_view(struct tog_view *);
512 static const struct got_error *open_tree_view(struct tog_view *,
513 struct got_tree_object *, struct got_object_id *, struct got_repository *);
514 static const struct got_error *show_tree_view(struct tog_view *);
515 static const struct got_error *input_tree_view(struct tog_view **,
516 struct tog_view **, struct tog_view **, struct tog_view *, int);
517 static const struct got_error *close_tree_view(struct tog_view *);
518 static const struct got_error *search_start_tree_view(struct tog_view *);
519 static const struct got_error *search_next_tree_view(struct tog_view *);
521 static const struct got_error *open_ref_view(struct tog_view *,
522 struct got_repository *);
523 static const struct got_error *show_ref_view(struct tog_view *);
524 static const struct got_error *input_ref_view(struct tog_view **,
525 struct tog_view **, struct tog_view **, struct tog_view *, int);
526 static const struct got_error *close_ref_view(struct tog_view *);
527 static const struct got_error *search_start_ref_view(struct tog_view *);
528 static const struct got_error *search_next_ref_view(struct tog_view *);
530 static volatile sig_atomic_t tog_sigwinch_received;
531 static volatile sig_atomic_t tog_sigpipe_received;
532 static volatile sig_atomic_t tog_sigcont_received;
534 static void
535 tog_sigwinch(int signo)
537 tog_sigwinch_received = 1;
540 static void
541 tog_sigpipe(int signo)
543 tog_sigpipe_received = 1;
546 static void
547 tog_sigcont(int signo)
549 tog_sigcont_received = 1;
552 static const struct got_error *
553 view_close(struct tog_view *view)
555 const struct got_error *err = NULL;
557 if (view->child) {
558 view_close(view->child);
559 view->child = NULL;
561 if (view->close)
562 err = view->close(view);
563 if (view->panel)
564 del_panel(view->panel);
565 if (view->window)
566 delwin(view->window);
567 free(view);
568 return err;
571 static struct tog_view *
572 view_open(int nlines, int ncols, int begin_y, int begin_x,
573 enum tog_view_type type)
575 struct tog_view *view = calloc(1, sizeof(*view));
577 if (view == NULL)
578 return NULL;
580 view->type = type;
581 view->lines = LINES;
582 view->cols = COLS;
583 view->nlines = nlines ? nlines : LINES - begin_y;
584 view->ncols = ncols ? ncols : COLS - begin_x;
585 view->begin_y = begin_y;
586 view->begin_x = begin_x;
587 view->window = newwin(nlines, ncols, begin_y, begin_x);
588 if (view->window == NULL) {
589 view_close(view);
590 return NULL;
592 view->panel = new_panel(view->window);
593 if (view->panel == NULL ||
594 set_panel_userptr(view->panel, view) != OK) {
595 view_close(view);
596 return NULL;
599 keypad(view->window, TRUE);
600 return view;
603 static int
604 view_split_begin_x(int begin_x)
606 if (begin_x > 0 || COLS < 120)
607 return 0;
608 return (COLS - MAX(COLS / 2, 80));
611 static const struct got_error *view_resize(struct tog_view *);
613 static const struct got_error *
614 view_splitscreen(struct tog_view *view)
616 const struct got_error *err = NULL;
618 view->begin_y = 0;
619 view->begin_x = view_split_begin_x(0);
620 view->nlines = LINES;
621 view->ncols = COLS - view->begin_x;
622 view->lines = LINES;
623 view->cols = COLS;
624 err = view_resize(view);
625 if (err)
626 return err;
628 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
629 return got_error_from_errno("mvwin");
631 return NULL;
634 static const struct got_error *
635 view_fullscreen(struct tog_view *view)
637 const struct got_error *err = NULL;
639 view->begin_x = 0;
640 view->begin_y = 0;
641 view->nlines = LINES;
642 view->ncols = COLS;
643 view->lines = LINES;
644 view->cols = COLS;
645 err = view_resize(view);
646 if (err)
647 return err;
649 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
650 return got_error_from_errno("mvwin");
652 return NULL;
655 static int
656 view_is_parent_view(struct tog_view *view)
658 return view->parent == NULL;
661 static const struct got_error *
662 view_resize(struct tog_view *view)
664 int nlines, ncols;
666 if (view->lines > LINES)
667 nlines = view->nlines - (view->lines - LINES);
668 else
669 nlines = view->nlines + (LINES - view->lines);
671 if (view->cols > COLS)
672 ncols = view->ncols - (view->cols - COLS);
673 else
674 ncols = view->ncols + (COLS - view->cols);
676 if (wresize(view->window, nlines, ncols) == ERR)
677 return got_error_from_errno("wresize");
678 if (replace_panel(view->panel, view->window) == ERR)
679 return got_error_from_errno("replace_panel");
680 wclear(view->window);
682 view->nlines = nlines;
683 view->ncols = ncols;
684 view->lines = LINES;
685 view->cols = COLS;
687 if (view->child) {
688 view->child->begin_x = view_split_begin_x(view->begin_x);
689 if (view->child->begin_x == 0) {
690 view_fullscreen(view->child);
691 if (view->child->focussed)
692 show_panel(view->child->panel);
693 else
694 show_panel(view->panel);
695 } else {
696 view_splitscreen(view->child);
697 show_panel(view->child->panel);
701 return NULL;
704 static const struct got_error *
705 view_close_child(struct tog_view *view)
707 const struct got_error *err = NULL;
709 if (view->child == NULL)
710 return NULL;
712 err = view_close(view->child);
713 view->child = NULL;
714 return err;
717 static const struct got_error *
718 view_set_child(struct tog_view *view, struct tog_view *child)
720 const struct got_error *err = NULL;
722 view->child = child;
723 child->parent = view;
724 return err;
727 static int
728 view_is_splitscreen(struct tog_view *view)
730 return view->begin_x > 0;
733 static void
734 tog_resizeterm(void)
736 int cols, lines;
737 struct winsize size;
739 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
740 cols = 80; /* Default */
741 lines = 24;
742 } else {
743 cols = size.ws_col;
744 lines = size.ws_row;
746 resize_term(lines, cols);
749 static const struct got_error *
750 view_search_start(struct tog_view *view)
752 const struct got_error *err = NULL;
753 char pattern[1024];
754 int ret;
756 if (view->nlines < 1)
757 return NULL;
759 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
760 wclrtoeol(view->window);
762 nocbreak();
763 echo();
764 ret = wgetnstr(view->window, pattern, sizeof(pattern));
765 cbreak();
766 noecho();
767 if (ret == ERR)
768 return NULL;
770 if (view->searching) {
771 regfree(&view->regex);
772 view->searching = 0;
775 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
776 err = view->search_start(view);
777 if (err) {
778 regfree(&view->regex);
779 return err;
781 view->searching = TOG_SEARCH_FORWARD;
782 view->search_next_done = 0;
783 view->search_next(view);
786 return NULL;
789 static const struct got_error *
790 view_input(struct tog_view **new, struct tog_view **dead,
791 struct tog_view **focus, int *done, struct tog_view *view,
792 struct tog_view_list_head *views)
794 const struct got_error *err = NULL;
795 struct tog_view *v;
796 int ch, errcode;
798 *new = NULL;
799 *dead = NULL;
800 *focus = NULL;
802 /* Clear "no matches" indicator. */
803 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
804 view->search_next_done == TOG_SEARCH_HAVE_NONE)
805 view->search_next_done = TOG_SEARCH_HAVE_MORE;
807 if (view->searching && !view->search_next_done) {
808 errcode = pthread_mutex_unlock(&tog_mutex);
809 if (errcode)
810 return got_error_set_errno(errcode,
811 "pthread_mutex_unlock");
812 pthread_yield();
813 errcode = pthread_mutex_lock(&tog_mutex);
814 if (errcode)
815 return got_error_set_errno(errcode,
816 "pthread_mutex_lock");
817 view->search_next(view);
818 return NULL;
821 nodelay(stdscr, FALSE);
822 /* Allow threads to make progress while we are waiting for input. */
823 errcode = pthread_mutex_unlock(&tog_mutex);
824 if (errcode)
825 return got_error_set_errno(errcode, "pthread_mutex_unlock");
826 ch = wgetch(view->window);
827 errcode = pthread_mutex_lock(&tog_mutex);
828 if (errcode)
829 return got_error_set_errno(errcode, "pthread_mutex_lock");
830 nodelay(stdscr, TRUE);
832 if (tog_sigwinch_received || tog_sigcont_received) {
833 tog_resizeterm();
834 tog_sigwinch_received = 0;
835 tog_sigcont_received = 0;
836 TAILQ_FOREACH(v, views, entry) {
837 err = view_resize(v);
838 if (err)
839 return err;
840 err = v->input(new, dead, focus, v, KEY_RESIZE);
841 if (err)
842 return err;
846 switch (ch) {
847 case ERR:
848 break;
849 case '\t':
850 if (view->child) {
851 *focus = view->child;
852 view->child_focussed = 1;
853 } else if (view->parent) {
854 *focus = view->parent;
855 view->parent->child_focussed = 0;
857 break;
858 case 'q':
859 err = view->input(new, dead, focus, view, ch);
860 *dead = view;
861 break;
862 case 'Q':
863 *done = 1;
864 break;
865 case 'f':
866 if (view_is_parent_view(view)) {
867 if (view->child == NULL)
868 break;
869 if (view_is_splitscreen(view->child)) {
870 *focus = view->child;
871 view->child_focussed = 1;
872 err = view_fullscreen(view->child);
873 } else
874 err = view_splitscreen(view->child);
875 if (err)
876 break;
877 err = view->child->input(new, dead, focus,
878 view->child, KEY_RESIZE);
879 } else {
880 if (view_is_splitscreen(view)) {
881 *focus = view;
882 view->parent->child_focussed = 1;
883 err = view_fullscreen(view);
884 } else {
885 err = view_splitscreen(view);
887 if (err)
888 break;
889 err = view->input(new, dead, focus, view,
890 KEY_RESIZE);
892 break;
893 case KEY_RESIZE:
894 break;
895 case '/':
896 if (view->search_start)
897 view_search_start(view);
898 else
899 err = view->input(new, dead, focus, view, ch);
900 break;
901 case 'N':
902 case 'n':
903 if (view->search_next) {
904 view->searching = (ch == 'n' ?
905 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
906 view->search_next_done = 0;
907 view->search_next(view);
908 } else
909 err = view->input(new, dead, focus, view, ch);
910 break;
911 default:
912 err = view->input(new, dead, focus, view, ch);
913 break;
916 return err;
919 void
920 view_vborder(struct tog_view *view)
922 PANEL *panel;
923 struct tog_view *view_above;
925 if (view->parent)
926 return view_vborder(view->parent);
928 panel = panel_above(view->panel);
929 if (panel == NULL)
930 return;
932 view_above = panel_userptr(panel);
933 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
934 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
937 int
938 view_needs_focus_indication(struct tog_view *view)
940 if (view_is_parent_view(view)) {
941 if (view->child == NULL || view->child_focussed)
942 return 0;
943 if (!view_is_splitscreen(view->child))
944 return 0;
945 } else if (!view_is_splitscreen(view))
946 return 0;
948 return view->focussed;
951 static const struct got_error *
952 view_loop(struct tog_view *view)
954 const struct got_error *err = NULL;
955 struct tog_view_list_head views;
956 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
957 int fast_refresh = 10;
958 int done = 0, errcode;
960 errcode = pthread_mutex_lock(&tog_mutex);
961 if (errcode)
962 return got_error_set_errno(errcode, "pthread_mutex_lock");
964 TAILQ_INIT(&views);
965 TAILQ_INSERT_HEAD(&views, view, entry);
967 main_view = view;
968 view->focussed = 1;
969 err = view->show(view);
970 if (err)
971 return err;
972 update_panels();
973 doupdate();
974 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
975 /* Refresh fast during initialization, then become slower. */
976 if (fast_refresh && fast_refresh-- == 0)
977 halfdelay(10); /* switch to once per second */
979 err = view_input(&new_view, &dead_view, &focus_view, &done,
980 view, &views);
981 if (err)
982 break;
983 if (dead_view) {
984 struct tog_view *prev = NULL;
986 if (view_is_parent_view(dead_view))
987 prev = TAILQ_PREV(dead_view,
988 tog_view_list_head, entry);
989 else if (view->parent != dead_view)
990 prev = view->parent;
992 if (dead_view->parent)
993 dead_view->parent->child = NULL;
994 else
995 TAILQ_REMOVE(&views, dead_view, entry);
997 err = view_close(dead_view);
998 if (err || (dead_view == main_view && new_view == NULL))
999 goto done;
1001 if (view == dead_view) {
1002 if (focus_view)
1003 view = focus_view;
1004 else if (prev)
1005 view = prev;
1006 else if (!TAILQ_EMPTY(&views))
1007 view = TAILQ_LAST(&views,
1008 tog_view_list_head);
1009 else
1010 view = NULL;
1011 if (view) {
1012 if (view->child && view->child_focussed)
1013 focus_view = view->child;
1014 else
1015 focus_view = view;
1019 if (new_view) {
1020 struct tog_view *v, *t;
1021 /* Only allow one parent view per type. */
1022 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1023 if (v->type != new_view->type)
1024 continue;
1025 TAILQ_REMOVE(&views, v, entry);
1026 err = view_close(v);
1027 if (err)
1028 goto done;
1029 break;
1031 TAILQ_INSERT_TAIL(&views, new_view, entry);
1032 view = new_view;
1033 if (focus_view == NULL)
1034 focus_view = new_view;
1036 if (focus_view) {
1037 show_panel(focus_view->panel);
1038 if (view)
1039 view->focussed = 0;
1040 focus_view->focussed = 1;
1041 view = focus_view;
1042 if (new_view)
1043 show_panel(new_view->panel);
1044 if (view->child && view_is_splitscreen(view->child))
1045 show_panel(view->child->panel);
1047 if (view) {
1048 if (focus_view == NULL) {
1049 view->focussed = 1;
1050 show_panel(view->panel);
1051 if (view->child && view_is_splitscreen(view->child))
1052 show_panel(view->child->panel);
1053 focus_view = view;
1055 if (view->parent) {
1056 err = view->parent->show(view->parent);
1057 if (err)
1058 goto done;
1060 err = view->show(view);
1061 if (err)
1062 goto done;
1063 if (view->child) {
1064 err = view->child->show(view->child);
1065 if (err)
1066 goto done;
1068 update_panels();
1069 doupdate();
1072 done:
1073 while (!TAILQ_EMPTY(&views)) {
1074 view = TAILQ_FIRST(&views);
1075 TAILQ_REMOVE(&views, view, entry);
1076 view_close(view);
1079 errcode = pthread_mutex_unlock(&tog_mutex);
1080 if (errcode && err == NULL)
1081 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1083 return err;
1086 __dead static void
1087 usage_log(void)
1089 endwin();
1090 fprintf(stderr,
1091 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1092 getprogname());
1093 exit(1);
1096 /* Create newly allocated wide-character string equivalent to a byte string. */
1097 static const struct got_error *
1098 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1100 char *vis = NULL;
1101 const struct got_error *err = NULL;
1103 *ws = NULL;
1104 *wlen = mbstowcs(NULL, s, 0);
1105 if (*wlen == (size_t)-1) {
1106 int vislen;
1107 if (errno != EILSEQ)
1108 return got_error_from_errno("mbstowcs");
1110 /* byte string invalid in current encoding; try to "fix" it */
1111 err = got_mbsavis(&vis, &vislen, s);
1112 if (err)
1113 return err;
1114 *wlen = mbstowcs(NULL, vis, 0);
1115 if (*wlen == (size_t)-1) {
1116 err = got_error_from_errno("mbstowcs"); /* give up */
1117 goto done;
1121 *ws = calloc(*wlen + 1, sizeof(**ws));
1122 if (*ws == NULL) {
1123 err = got_error_from_errno("calloc");
1124 goto done;
1127 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1128 err = got_error_from_errno("mbstowcs");
1129 done:
1130 free(vis);
1131 if (err) {
1132 free(*ws);
1133 *ws = NULL;
1134 *wlen = 0;
1136 return err;
1139 /* Format a line for display, ensuring that it won't overflow a width limit. */
1140 static const struct got_error *
1141 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1142 int col_tab_align)
1144 const struct got_error *err = NULL;
1145 int cols = 0;
1146 wchar_t *wline = NULL;
1147 size_t wlen;
1148 int i;
1150 *wlinep = NULL;
1151 *widthp = 0;
1153 err = mbs2ws(&wline, &wlen, line);
1154 if (err)
1155 return err;
1157 i = 0;
1158 while (i < wlen) {
1159 int width = wcwidth(wline[i]);
1161 if (width == 0) {
1162 i++;
1163 continue;
1166 if (width == 1 || width == 2) {
1167 if (cols + width > wlimit)
1168 break;
1169 cols += width;
1170 i++;
1171 } else if (width == -1) {
1172 if (wline[i] == L'\t') {
1173 width = TABSIZE -
1174 ((cols + col_tab_align) % TABSIZE);
1175 if (cols + width > wlimit)
1176 break;
1177 cols += width;
1179 i++;
1180 } else {
1181 err = got_error_from_errno("wcwidth");
1182 goto done;
1185 wline[i] = L'\0';
1186 if (widthp)
1187 *widthp = cols;
1188 done:
1189 if (err)
1190 free(wline);
1191 else
1192 *wlinep = wline;
1193 return err;
1196 static const struct got_error*
1197 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1198 struct got_object_id *id, struct got_repository *repo)
1200 static const struct got_error *err = NULL;
1201 struct got_reflist_entry *re;
1202 char *s;
1203 const char *name;
1205 *refs_str = NULL;
1207 SIMPLEQ_FOREACH(re, refs, entry) {
1208 struct got_tag_object *tag = NULL;
1209 struct got_object_id *ref_id;
1210 int cmp;
1212 name = got_ref_get_name(re->ref);
1213 if (strcmp(name, GOT_REF_HEAD) == 0)
1214 continue;
1215 if (strncmp(name, "refs/", 5) == 0)
1216 name += 5;
1217 if (strncmp(name, "got/", 4) == 0)
1218 continue;
1219 if (strncmp(name, "heads/", 6) == 0)
1220 name += 6;
1221 if (strncmp(name, "remotes/", 8) == 0) {
1222 name += 8;
1223 s = strstr(name, "/" GOT_REF_HEAD);
1224 if (s != NULL && s[strlen(s)] == '\0')
1225 continue;
1227 err = got_ref_resolve(&ref_id, repo, re->ref);
1228 if (err)
1229 break;
1230 if (strncmp(name, "tags/", 5) == 0) {
1231 err = got_object_open_as_tag(&tag, repo, ref_id);
1232 if (err) {
1233 if (err->code != GOT_ERR_OBJ_TYPE) {
1234 free(ref_id);
1235 break;
1237 /* Ref points at something other than a tag. */
1238 err = NULL;
1239 tag = NULL;
1242 cmp = got_object_id_cmp(tag ?
1243 got_object_tag_get_object_id(tag) : ref_id, id);
1244 free(ref_id);
1245 if (tag)
1246 got_object_tag_close(tag);
1247 if (cmp != 0)
1248 continue;
1249 s = *refs_str;
1250 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1251 s ? ", " : "", name) == -1) {
1252 err = got_error_from_errno("asprintf");
1253 free(s);
1254 *refs_str = NULL;
1255 break;
1257 free(s);
1260 return err;
1263 static const struct got_error *
1264 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1265 int col_tab_align)
1267 char *smallerthan, *at;
1269 smallerthan = strchr(author, '<');
1270 if (smallerthan && smallerthan[1] != '\0')
1271 author = smallerthan + 1;
1272 at = strchr(author, '@');
1273 if (at)
1274 *at = '\0';
1275 return format_line(wauthor, author_width, author, limit, col_tab_align);
1278 static const struct got_error *
1279 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1280 struct got_object_id *id, struct got_reflist_head *refs,
1281 const size_t date_display_cols, int author_display_cols,
1282 struct tog_colors *colors)
1284 const struct got_error *err = NULL;
1285 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1286 char *logmsg0 = NULL, *logmsg = NULL;
1287 char *author = NULL;
1288 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1289 int author_width, logmsg_width;
1290 char *newline, *line = NULL;
1291 int col, limit;
1292 const int avail = view->ncols;
1293 struct tm tm;
1294 time_t committer_time;
1295 struct tog_color *tc;
1297 committer_time = got_object_commit_get_committer_time(commit);
1298 if (localtime_r(&committer_time, &tm) == NULL)
1299 return got_error_from_errno("localtime_r");
1300 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1301 >= sizeof(datebuf))
1302 return got_error(GOT_ERR_NO_SPACE);
1304 if (avail <= date_display_cols)
1305 limit = MIN(sizeof(datebuf) - 1, avail);
1306 else
1307 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1308 tc = get_color(colors, TOG_COLOR_DATE);
1309 if (tc)
1310 wattr_on(view->window,
1311 COLOR_PAIR(tc->colorpair), NULL);
1312 waddnstr(view->window, datebuf, limit);
1313 if (tc)
1314 wattr_off(view->window,
1315 COLOR_PAIR(tc->colorpair), NULL);
1316 col = limit;
1317 if (col > avail)
1318 goto done;
1320 if (avail >= 120) {
1321 char *id_str;
1322 err = got_object_id_str(&id_str, id);
1323 if (err)
1324 goto done;
1325 tc = get_color(colors, TOG_COLOR_COMMIT);
1326 if (tc)
1327 wattr_on(view->window,
1328 COLOR_PAIR(tc->colorpair), NULL);
1329 wprintw(view->window, "%.8s ", id_str);
1330 if (tc)
1331 wattr_off(view->window,
1332 COLOR_PAIR(tc->colorpair), NULL);
1333 free(id_str);
1334 col += 9;
1335 if (col > avail)
1336 goto done;
1339 author = strdup(got_object_commit_get_author(commit));
1340 if (author == NULL) {
1341 err = got_error_from_errno("strdup");
1342 goto done;
1344 err = format_author(&wauthor, &author_width, author, avail - col, col);
1345 if (err)
1346 goto done;
1347 tc = get_color(colors, TOG_COLOR_AUTHOR);
1348 if (tc)
1349 wattr_on(view->window,
1350 COLOR_PAIR(tc->colorpair), NULL);
1351 waddwstr(view->window, wauthor);
1352 if (tc)
1353 wattr_off(view->window,
1354 COLOR_PAIR(tc->colorpair), NULL);
1355 col += author_width;
1356 while (col < avail && author_width < author_display_cols + 2) {
1357 waddch(view->window, ' ');
1358 col++;
1359 author_width++;
1361 if (col > avail)
1362 goto done;
1364 err = got_object_commit_get_logmsg(&logmsg0, commit);
1365 if (err)
1366 goto done;
1367 logmsg = logmsg0;
1368 while (*logmsg == '\n')
1369 logmsg++;
1370 newline = strchr(logmsg, '\n');
1371 if (newline)
1372 *newline = '\0';
1373 limit = avail - col;
1374 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1375 if (err)
1376 goto done;
1377 waddwstr(view->window, wlogmsg);
1378 col += logmsg_width;
1379 while (col < avail) {
1380 waddch(view->window, ' ');
1381 col++;
1383 done:
1384 free(logmsg0);
1385 free(wlogmsg);
1386 free(author);
1387 free(wauthor);
1388 free(line);
1389 return err;
1392 static struct commit_queue_entry *
1393 alloc_commit_queue_entry(struct got_commit_object *commit,
1394 struct got_object_id *id)
1396 struct commit_queue_entry *entry;
1398 entry = calloc(1, sizeof(*entry));
1399 if (entry == NULL)
1400 return NULL;
1402 entry->id = id;
1403 entry->commit = commit;
1404 return entry;
1407 static void
1408 pop_commit(struct commit_queue *commits)
1410 struct commit_queue_entry *entry;
1412 entry = TAILQ_FIRST(&commits->head);
1413 TAILQ_REMOVE(&commits->head, entry, entry);
1414 got_object_commit_close(entry->commit);
1415 commits->ncommits--;
1416 /* Don't free entry->id! It is owned by the commit graph. */
1417 free(entry);
1420 static void
1421 free_commits(struct commit_queue *commits)
1423 while (!TAILQ_EMPTY(&commits->head))
1424 pop_commit(commits);
1427 static const struct got_error *
1428 match_commit(int *have_match, struct got_object_id *id,
1429 struct got_commit_object *commit, regex_t *regex)
1431 const struct got_error *err = NULL;
1432 regmatch_t regmatch;
1433 char *id_str = NULL, *logmsg = NULL;
1435 *have_match = 0;
1437 err = got_object_id_str(&id_str, id);
1438 if (err)
1439 return err;
1441 err = got_object_commit_get_logmsg(&logmsg, commit);
1442 if (err)
1443 goto done;
1445 if (regexec(regex, got_object_commit_get_author(commit), 1,
1446 &regmatch, 0) == 0 ||
1447 regexec(regex, got_object_commit_get_committer(commit), 1,
1448 &regmatch, 0) == 0 ||
1449 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1450 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1451 *have_match = 1;
1452 done:
1453 free(id_str);
1454 free(logmsg);
1455 return err;
1458 static const struct got_error *
1459 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1460 int minqueue, struct got_repository *repo, const char *path,
1461 int *searching, int *search_next_done, regex_t *regex)
1463 const struct got_error *err = NULL;
1464 int nqueued = 0;
1467 * We keep all commits open throughout the lifetime of the log
1468 * view in order to avoid having to re-fetch commits from disk
1469 * while updating the display.
1471 while (nqueued < minqueue ||
1472 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1473 struct got_object_id *id;
1474 struct got_commit_object *commit;
1475 struct commit_queue_entry *entry;
1476 int errcode;
1478 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1479 if (err || id == NULL)
1480 break;
1482 err = got_object_open_as_commit(&commit, repo, id);
1483 if (err)
1484 break;
1485 entry = alloc_commit_queue_entry(commit, id);
1486 if (entry == NULL) {
1487 err = got_error_from_errno("alloc_commit_queue_entry");
1488 break;
1491 errcode = pthread_mutex_lock(&tog_mutex);
1492 if (errcode) {
1493 err = got_error_set_errno(errcode,
1494 "pthread_mutex_lock");
1495 break;
1498 entry->idx = commits->ncommits;
1499 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1500 nqueued++;
1501 commits->ncommits++;
1503 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1504 int have_match;
1505 err = match_commit(&have_match, id, commit, regex);
1506 if (err)
1507 break;
1508 if (have_match)
1509 *search_next_done = TOG_SEARCH_HAVE_MORE;
1512 errcode = pthread_mutex_unlock(&tog_mutex);
1513 if (errcode && err == NULL)
1514 err = got_error_set_errno(errcode,
1515 "pthread_mutex_unlock");
1516 if (err)
1517 break;
1520 return err;
1523 static const struct got_error *
1524 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1525 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1526 struct commit_queue *commits, int selected_idx, int limit,
1527 struct got_reflist_head *refs, const char *path, int commits_needed,
1528 struct tog_colors *colors)
1530 const struct got_error *err = NULL;
1531 struct tog_log_view_state *s = &view->state.log;
1532 struct commit_queue_entry *entry;
1533 int width;
1534 int ncommits, author_cols = 4;
1535 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1536 char *refs_str = NULL;
1537 wchar_t *wline;
1538 struct tog_color *tc;
1539 static const size_t date_display_cols = 12;
1541 entry = first;
1542 ncommits = 0;
1543 while (entry) {
1544 if (ncommits == selected_idx) {
1545 *selected = entry;
1546 break;
1548 entry = TAILQ_NEXT(entry, entry);
1549 ncommits++;
1552 if (*selected && !(view->searching && view->search_next_done == 0)) {
1553 err = got_object_id_str(&id_str, (*selected)->id);
1554 if (err)
1555 return err;
1556 if (refs) {
1557 err = build_refs_str(&refs_str, refs, (*selected)->id,
1558 s->repo);
1559 if (err)
1560 goto done;
1564 if (commits_needed == 0)
1565 halfdelay(10); /* disable fast refresh */
1567 if (commits_needed > 0) {
1568 if (asprintf(&ncommits_str, " [%d/%d] %s",
1569 entry ? entry->idx + 1 : 0, commits->ncommits,
1570 (view->searching && !view->search_next_done) ?
1571 "searching..." : "loading...") == -1) {
1572 err = got_error_from_errno("asprintf");
1573 goto done;
1575 } else {
1576 const char *search_str = NULL;
1578 if (view->searching) {
1579 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1580 search_str = "no more matches";
1581 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1582 search_str = "no matches found";
1583 else if (!view->search_next_done)
1584 search_str = "searching...";
1587 if (asprintf(&ncommits_str, " [%d/%d] %s",
1588 entry ? entry->idx + 1 : 0, commits->ncommits,
1589 search_str ? search_str :
1590 (refs_str ? refs_str : "")) == -1) {
1591 err = got_error_from_errno("asprintf");
1592 goto done;
1596 if (path && strcmp(path, "/") != 0) {
1597 if (asprintf(&header, "commit %s %s%s",
1598 id_str ? id_str : "........................................",
1599 path, ncommits_str) == -1) {
1600 err = got_error_from_errno("asprintf");
1601 header = NULL;
1602 goto done;
1604 } else if (asprintf(&header, "commit %s%s",
1605 id_str ? id_str : "........................................",
1606 ncommits_str) == -1) {
1607 err = got_error_from_errno("asprintf");
1608 header = NULL;
1609 goto done;
1611 err = format_line(&wline, &width, header, view->ncols, 0);
1612 if (err)
1613 goto done;
1615 werase(view->window);
1617 if (view_needs_focus_indication(view))
1618 wstandout(view->window);
1619 tc = get_color(colors, TOG_COLOR_COMMIT);
1620 if (tc)
1621 wattr_on(view->window,
1622 COLOR_PAIR(tc->colorpair), NULL);
1623 waddwstr(view->window, wline);
1624 if (tc)
1625 wattr_off(view->window,
1626 COLOR_PAIR(tc->colorpair), NULL);
1627 while (width < view->ncols) {
1628 waddch(view->window, ' ');
1629 width++;
1631 if (view_needs_focus_indication(view))
1632 wstandend(view->window);
1633 free(wline);
1634 if (limit <= 1)
1635 goto done;
1637 /* Grow author column size if necessary. */
1638 entry = first;
1639 ncommits = 0;
1640 while (entry) {
1641 char *author;
1642 wchar_t *wauthor;
1643 int width;
1644 if (ncommits >= limit - 1)
1645 break;
1646 author = strdup(got_object_commit_get_author(entry->commit));
1647 if (author == NULL) {
1648 err = got_error_from_errno("strdup");
1649 goto done;
1651 err = format_author(&wauthor, &width, author, COLS,
1652 date_display_cols);
1653 if (author_cols < width)
1654 author_cols = width;
1655 free(wauthor);
1656 free(author);
1657 ncommits++;
1658 entry = TAILQ_NEXT(entry, entry);
1661 entry = first;
1662 *last = first;
1663 ncommits = 0;
1664 while (entry) {
1665 if (ncommits >= limit - 1)
1666 break;
1667 if (ncommits == selected_idx)
1668 wstandout(view->window);
1669 err = draw_commit(view, entry->commit, entry->id, refs,
1670 date_display_cols, author_cols, colors);
1671 if (ncommits == selected_idx)
1672 wstandend(view->window);
1673 if (err)
1674 goto done;
1675 ncommits++;
1676 *last = entry;
1677 entry = TAILQ_NEXT(entry, entry);
1680 view_vborder(view);
1681 done:
1682 free(id_str);
1683 free(refs_str);
1684 free(ncommits_str);
1685 free(header);
1686 return err;
1689 static void
1690 log_scroll_up(struct tog_view *view, int maxscroll)
1692 struct tog_log_view_state *s = &view->state.log;
1693 struct commit_queue_entry *entry;
1694 int nscrolled = 0;
1696 entry = TAILQ_FIRST(&s->commits.head);
1697 if (s->first_displayed_entry == entry)
1698 return;
1700 entry = s->first_displayed_entry;
1701 while (entry && nscrolled < maxscroll) {
1702 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1703 if (entry) {
1704 s->first_displayed_entry = entry;
1705 nscrolled++;
1710 static const struct got_error *
1711 trigger_log_thread(struct tog_view *view, int wait)
1713 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1714 int errcode;
1716 halfdelay(1); /* fast refresh while loading commits */
1718 while (ta->commits_needed > 0) {
1719 if (ta->log_complete)
1720 break;
1722 /* Wake the log thread. */
1723 errcode = pthread_cond_signal(&ta->need_commits);
1724 if (errcode)
1725 return got_error_set_errno(errcode,
1726 "pthread_cond_signal");
1729 * The mutex will be released while the view loop waits
1730 * in wgetch(), at which time the log thread will run.
1732 if (!wait)
1733 break;
1735 /* Display progress update in log view. */
1736 show_log_view(view);
1737 update_panels();
1738 doupdate();
1740 /* Wait right here while next commit is being loaded. */
1741 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1742 if (errcode)
1743 return got_error_set_errno(errcode,
1744 "pthread_cond_wait");
1746 /* Display progress update in log view. */
1747 show_log_view(view);
1748 update_panels();
1749 doupdate();
1752 return NULL;
1755 static const struct got_error *
1756 log_scroll_down(struct tog_view *view, int maxscroll)
1758 struct tog_log_view_state *s = &view->state.log;
1759 const struct got_error *err = NULL;
1760 struct commit_queue_entry *pentry;
1761 int nscrolled = 0, ncommits_needed;
1763 if (s->last_displayed_entry == NULL)
1764 return NULL;
1766 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1767 if (s->commits.ncommits < ncommits_needed &&
1768 !s->thread_args.log_complete) {
1770 * Ask the log thread for required amount of commits.
1772 s->thread_args.commits_needed += maxscroll;
1773 err = trigger_log_thread(view, 1);
1774 if (err)
1775 return err;
1778 do {
1779 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1780 if (pentry == NULL)
1781 break;
1783 s->last_displayed_entry = pentry;
1785 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1786 if (pentry == NULL)
1787 break;
1788 s->first_displayed_entry = pentry;
1789 } while (++nscrolled < maxscroll);
1791 return err;
1794 static const struct got_error *
1795 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1796 struct got_commit_object *commit, struct got_object_id *commit_id,
1797 struct tog_view *log_view, struct got_repository *repo)
1799 const struct got_error *err;
1800 struct got_object_qid *parent_id;
1801 struct tog_view *diff_view;
1803 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1804 if (diff_view == NULL)
1805 return got_error_from_errno("view_open");
1807 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1808 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1809 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1810 if (err == NULL)
1811 *new_view = diff_view;
1812 return err;
1815 static const struct got_error *
1816 tree_view_visit_subtree(struct got_tree_object *subtree,
1817 struct tog_tree_view_state *s)
1819 struct tog_parent_tree *parent;
1821 parent = calloc(1, sizeof(*parent));
1822 if (parent == NULL)
1823 return got_error_from_errno("calloc");
1825 parent->tree = s->tree;
1826 parent->first_displayed_entry = s->first_displayed_entry;
1827 parent->selected_entry = s->selected_entry;
1828 parent->selected = s->selected;
1829 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1830 s->tree = subtree;
1831 s->selected = 0;
1832 s->first_displayed_entry = NULL;
1833 return NULL;
1836 static const struct got_error *
1837 tree_view_walk_path(struct tog_tree_view_state *s,
1838 struct got_object_id *commit_id,
1839 const char *path, struct got_repository *repo)
1841 const struct got_error *err = NULL;
1842 struct got_tree_object *tree = NULL;
1843 const char *p;
1844 char *slash, *subpath = NULL;
1846 /* Walk the path and open corresponding tree objects. */
1847 p = path;
1848 while (*p) {
1849 struct got_tree_entry *te;
1850 struct got_object_id *tree_id;
1851 char *te_name;
1853 while (p[0] == '/')
1854 p++;
1856 /* Ensure the correct subtree entry is selected. */
1857 slash = strchr(p, '/');
1858 if (slash == NULL)
1859 te_name = strdup(p);
1860 else
1861 te_name = strndup(p, slash - p);
1862 if (te_name == NULL) {
1863 err = got_error_from_errno("strndup");
1864 break;
1866 te = got_object_tree_find_entry(s->tree, te_name);
1867 if (te == NULL) {
1868 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1869 free(te_name);
1870 break;
1872 free(te_name);
1873 s->first_displayed_entry = s->selected_entry = te;
1875 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1876 break; /* jump to this file's entry */
1878 slash = strchr(p, '/');
1879 if (slash)
1880 subpath = strndup(path, slash - path);
1881 else
1882 subpath = strdup(path);
1883 if (subpath == NULL) {
1884 err = got_error_from_errno("strdup");
1885 break;
1888 err = got_object_id_by_path(&tree_id, repo, commit_id,
1889 subpath);
1890 if (err)
1891 break;
1893 err = got_object_open_as_tree(&tree, repo, tree_id);
1894 free(tree_id);
1895 if (err)
1896 break;
1898 err = tree_view_visit_subtree(tree, s);
1899 if (err) {
1900 got_object_tree_close(tree);
1901 break;
1903 if (slash == NULL)
1904 break;
1905 free(subpath);
1906 subpath = NULL;
1907 p = slash;
1910 free(subpath);
1911 return err;
1914 static const struct got_error *
1915 browse_commit_tree(struct tog_view **new_view, int begin_x,
1916 struct commit_queue_entry *entry, const char *path,
1917 struct got_repository *repo)
1919 const struct got_error *err = NULL;
1920 struct got_tree_object *tree;
1921 struct tog_tree_view_state *s;
1922 struct tog_view *tree_view;
1924 err = got_object_open_as_tree(&tree, repo,
1925 got_object_commit_get_tree_id(entry->commit));
1926 if (err)
1927 return err;
1929 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1930 if (tree_view == NULL)
1931 return got_error_from_errno("view_open");
1933 err = open_tree_view(tree_view, tree, entry->id, repo);
1934 if (err) {
1935 got_object_tree_close(tree);
1936 return err;
1938 s = &tree_view->state.tree;
1940 *new_view = tree_view;
1942 if (got_path_is_root_dir(path))
1943 return NULL;
1945 return tree_view_walk_path(s, entry->id, path, repo);
1948 static const struct got_error *
1949 block_signals_used_by_main_thread(void)
1951 sigset_t sigset;
1952 int errcode;
1954 if (sigemptyset(&sigset) == -1)
1955 return got_error_from_errno("sigemptyset");
1957 /* tog handles SIGWINCH and SIGCONT */
1958 if (sigaddset(&sigset, SIGWINCH) == -1)
1959 return got_error_from_errno("sigaddset");
1960 if (sigaddset(&sigset, SIGCONT) == -1)
1961 return got_error_from_errno("sigaddset");
1963 /* ncurses handles SIGTSTP */
1964 if (sigaddset(&sigset, SIGTSTP) == -1)
1965 return got_error_from_errno("sigaddset");
1967 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1968 if (errcode)
1969 return got_error_set_errno(errcode, "pthread_sigmask");
1971 return NULL;
1974 static void *
1975 log_thread(void *arg)
1977 const struct got_error *err = NULL;
1978 int errcode = 0;
1979 struct tog_log_thread_args *a = arg;
1980 int done = 0;
1982 err = block_signals_used_by_main_thread();
1983 if (err)
1984 return (void *)err;
1986 while (!done && !err && !tog_sigpipe_received) {
1987 err = queue_commits(a->graph, a->commits, 1, a->repo,
1988 a->in_repo_path, a->searching, a->search_next_done,
1989 a->regex);
1990 if (err) {
1991 if (err->code != GOT_ERR_ITER_COMPLETED)
1992 return (void *)err;
1993 err = NULL;
1994 done = 1;
1995 } else if (a->commits_needed > 0)
1996 a->commits_needed--;
1998 errcode = pthread_mutex_lock(&tog_mutex);
1999 if (errcode) {
2000 err = got_error_set_errno(errcode,
2001 "pthread_mutex_lock");
2002 break;
2003 } else if (*a->quit)
2004 done = 1;
2005 else if (*a->first_displayed_entry == NULL) {
2006 *a->first_displayed_entry =
2007 TAILQ_FIRST(&a->commits->head);
2008 *a->selected_entry = *a->first_displayed_entry;
2011 errcode = pthread_cond_signal(&a->commit_loaded);
2012 if (errcode) {
2013 err = got_error_set_errno(errcode,
2014 "pthread_cond_signal");
2015 pthread_mutex_unlock(&tog_mutex);
2016 break;
2019 if (done)
2020 a->commits_needed = 0;
2021 else {
2022 if (a->commits_needed == 0) {
2023 errcode = pthread_cond_wait(&a->need_commits,
2024 &tog_mutex);
2025 if (errcode)
2026 err = got_error_set_errno(errcode,
2027 "pthread_cond_wait");
2031 errcode = pthread_mutex_unlock(&tog_mutex);
2032 if (errcode && err == NULL)
2033 err = got_error_set_errno(errcode,
2034 "pthread_mutex_unlock");
2036 a->log_complete = 1;
2037 return (void *)err;
2040 static const struct got_error *
2041 stop_log_thread(struct tog_log_view_state *s)
2043 const struct got_error *err = NULL;
2044 int errcode;
2046 if (s->thread) {
2047 s->quit = 1;
2048 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2049 if (errcode)
2050 return got_error_set_errno(errcode,
2051 "pthread_cond_signal");
2052 errcode = pthread_mutex_unlock(&tog_mutex);
2053 if (errcode)
2054 return got_error_set_errno(errcode,
2055 "pthread_mutex_unlock");
2056 errcode = pthread_join(s->thread, (void **)&err);
2057 if (errcode)
2058 return got_error_set_errno(errcode, "pthread_join");
2059 errcode = pthread_mutex_lock(&tog_mutex);
2060 if (errcode)
2061 return got_error_set_errno(errcode,
2062 "pthread_mutex_lock");
2063 s->thread = NULL;
2066 if (s->thread_args.repo) {
2067 got_repo_close(s->thread_args.repo);
2068 s->thread_args.repo = NULL;
2071 if (s->thread_args.graph) {
2072 got_commit_graph_close(s->thread_args.graph);
2073 s->thread_args.graph = NULL;
2076 return err;
2079 static const struct got_error *
2080 close_log_view(struct tog_view *view)
2082 const struct got_error *err = NULL;
2083 struct tog_log_view_state *s = &view->state.log;
2084 int errcode;
2086 err = stop_log_thread(s);
2088 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2089 if (errcode && err == NULL)
2090 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2092 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2093 if (errcode && err == NULL)
2094 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2096 free_commits(&s->commits);
2097 free(s->in_repo_path);
2098 s->in_repo_path = NULL;
2099 free(s->start_id);
2100 s->start_id = NULL;
2101 got_ref_list_free(&s->refs);
2102 return err;
2105 static const struct got_error *
2106 search_start_log_view(struct tog_view *view)
2108 struct tog_log_view_state *s = &view->state.log;
2110 s->matched_entry = NULL;
2111 s->search_entry = NULL;
2112 return NULL;
2115 static const struct got_error *
2116 search_next_log_view(struct tog_view *view)
2118 const struct got_error *err = NULL;
2119 struct tog_log_view_state *s = &view->state.log;
2120 struct commit_queue_entry *entry;
2122 /* Display progress update in log view. */
2123 show_log_view(view);
2124 update_panels();
2125 doupdate();
2127 if (s->search_entry) {
2128 int errcode, ch;
2129 errcode = pthread_mutex_unlock(&tog_mutex);
2130 if (errcode)
2131 return got_error_set_errno(errcode,
2132 "pthread_mutex_unlock");
2133 ch = wgetch(view->window);
2134 errcode = pthread_mutex_lock(&tog_mutex);
2135 if (errcode)
2136 return got_error_set_errno(errcode,
2137 "pthread_mutex_lock");
2138 if (ch == KEY_BACKSPACE) {
2139 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2140 return NULL;
2142 if (view->searching == TOG_SEARCH_FORWARD)
2143 entry = TAILQ_NEXT(s->search_entry, entry);
2144 else
2145 entry = TAILQ_PREV(s->search_entry,
2146 commit_queue_head, entry);
2147 } else if (s->matched_entry) {
2148 if (view->searching == TOG_SEARCH_FORWARD)
2149 entry = TAILQ_NEXT(s->matched_entry, entry);
2150 else
2151 entry = TAILQ_PREV(s->matched_entry,
2152 commit_queue_head, entry);
2153 } else {
2154 if (view->searching == TOG_SEARCH_FORWARD)
2155 entry = TAILQ_FIRST(&s->commits.head);
2156 else
2157 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2160 while (1) {
2161 int have_match = 0;
2163 if (entry == NULL) {
2164 if (s->thread_args.log_complete ||
2165 view->searching == TOG_SEARCH_BACKWARD) {
2166 view->search_next_done =
2167 (s->matched_entry == NULL ?
2168 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2169 s->search_entry = NULL;
2170 return NULL;
2173 * Poke the log thread for more commits and return,
2174 * allowing the main loop to make progress. Search
2175 * will resume at s->search_entry once we come back.
2177 s->thread_args.commits_needed++;
2178 return trigger_log_thread(view, 0);
2181 err = match_commit(&have_match, entry->id, entry->commit,
2182 &view->regex);
2183 if (err)
2184 break;
2185 if (have_match) {
2186 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2187 s->matched_entry = entry;
2188 break;
2191 s->search_entry = entry;
2192 if (view->searching == TOG_SEARCH_FORWARD)
2193 entry = TAILQ_NEXT(entry, entry);
2194 else
2195 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2198 if (s->matched_entry) {
2199 int cur = s->selected_entry->idx;
2200 while (cur < s->matched_entry->idx) {
2201 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2202 if (err)
2203 return err;
2204 cur++;
2206 while (cur > s->matched_entry->idx) {
2207 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2208 if (err)
2209 return err;
2210 cur--;
2214 s->search_entry = NULL;
2216 return NULL;
2219 static const struct got_error *
2220 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2221 struct got_repository *repo, const char *head_ref_name,
2222 const char *in_repo_path, int log_branches)
2224 const struct got_error *err = NULL;
2225 struct tog_log_view_state *s = &view->state.log;
2226 struct got_repository *thread_repo = NULL;
2227 struct got_commit_graph *thread_graph = NULL;
2228 int errcode;
2230 SIMPLEQ_INIT(&s->refs);
2232 if (in_repo_path != s->in_repo_path) {
2233 free(s->in_repo_path);
2234 s->in_repo_path = strdup(in_repo_path);
2235 if (s->in_repo_path == NULL)
2236 return got_error_from_errno("strdup");
2239 /* The commit queue only contains commits being displayed. */
2240 TAILQ_INIT(&s->commits.head);
2241 s->commits.ncommits = 0;
2243 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2244 if (err)
2245 goto done;
2247 s->repo = repo;
2248 s->head_ref_name = head_ref_name;
2249 s->start_id = got_object_id_dup(start_id);
2250 if (s->start_id == NULL) {
2251 err = got_error_from_errno("got_object_id_dup");
2252 goto done;
2254 s->log_branches = log_branches;
2256 SIMPLEQ_INIT(&s->colors);
2257 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2258 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2259 get_color_value("TOG_COLOR_COMMIT"));
2260 if (err)
2261 goto done;
2262 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2263 get_color_value("TOG_COLOR_AUTHOR"));
2264 if (err) {
2265 free_colors(&s->colors);
2266 goto done;
2268 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2269 get_color_value("TOG_COLOR_DATE"));
2270 if (err) {
2271 free_colors(&s->colors);
2272 goto done;
2276 view->show = show_log_view;
2277 view->input = input_log_view;
2278 view->close = close_log_view;
2279 view->search_start = search_start_log_view;
2280 view->search_next = search_next_log_view;
2282 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2283 if (err)
2284 goto done;
2285 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2286 !s->log_branches);
2287 if (err)
2288 goto done;
2289 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2290 s->repo, NULL, NULL);
2291 if (err)
2292 goto done;
2294 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2295 if (errcode) {
2296 err = got_error_set_errno(errcode, "pthread_cond_init");
2297 goto done;
2299 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2300 if (errcode) {
2301 err = got_error_set_errno(errcode, "pthread_cond_init");
2302 goto done;
2305 s->thread_args.commits_needed = view->nlines;
2306 s->thread_args.graph = thread_graph;
2307 s->thread_args.commits = &s->commits;
2308 s->thread_args.in_repo_path = s->in_repo_path;
2309 s->thread_args.start_id = s->start_id;
2310 s->thread_args.repo = thread_repo;
2311 s->thread_args.log_complete = 0;
2312 s->thread_args.quit = &s->quit;
2313 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2314 s->thread_args.selected_entry = &s->selected_entry;
2315 s->thread_args.searching = &view->searching;
2316 s->thread_args.search_next_done = &view->search_next_done;
2317 s->thread_args.regex = &view->regex;
2318 done:
2319 if (err)
2320 close_log_view(view);
2321 return err;
2324 static const struct got_error *
2325 show_log_view(struct tog_view *view)
2327 const struct got_error *err;
2328 struct tog_log_view_state *s = &view->state.log;
2330 if (s->thread == NULL) {
2331 int errcode = pthread_create(&s->thread, NULL, log_thread,
2332 &s->thread_args);
2333 if (errcode)
2334 return got_error_set_errno(errcode, "pthread_create");
2335 if (s->thread_args.commits_needed > 0) {
2336 err = trigger_log_thread(view, 1);
2337 if (err)
2338 return err;
2342 return draw_commits(view, &s->last_displayed_entry,
2343 &s->selected_entry, s->first_displayed_entry,
2344 &s->commits, s->selected, view->nlines, &s->refs,
2345 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2348 static const struct got_error *
2349 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2350 struct tog_view **focus_view, struct tog_view *view, int ch)
2352 const struct got_error *err = NULL;
2353 struct tog_log_view_state *s = &view->state.log;
2354 char *parent_path, *in_repo_path = NULL;
2355 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2356 struct tog_view *ref_view = NULL;
2357 int begin_x = 0;
2358 struct got_object_id *start_id;
2360 switch (ch) {
2361 case 'q':
2362 s->quit = 1;
2363 break;
2364 case 'k':
2365 case KEY_UP:
2366 case '<':
2367 case ',':
2368 if (s->first_displayed_entry == NULL)
2369 break;
2370 if (s->selected > 0)
2371 s->selected--;
2372 else
2373 log_scroll_up(view, 1);
2374 break;
2375 case KEY_PPAGE:
2376 case CTRL('b'):
2377 if (s->first_displayed_entry == NULL)
2378 break;
2379 if (TAILQ_FIRST(&s->commits.head) ==
2380 s->first_displayed_entry) {
2381 s->selected = 0;
2382 break;
2384 log_scroll_up(view, view->nlines - 1);
2385 break;
2386 case 'j':
2387 case KEY_DOWN:
2388 case '>':
2389 case '.':
2390 if (s->first_displayed_entry == NULL)
2391 break;
2392 if (s->selected < MIN(view->nlines - 2,
2393 s->commits.ncommits - 1)) {
2394 s->selected++;
2395 break;
2397 err = log_scroll_down(view, 1);
2398 break;
2399 case KEY_NPAGE:
2400 case CTRL('f'): {
2401 struct commit_queue_entry *first;
2402 first = s->first_displayed_entry;
2403 if (first == NULL)
2404 break;
2405 err = log_scroll_down(view, view->nlines - 1);
2406 if (err)
2407 break;
2408 if (first == s->first_displayed_entry &&
2409 s->selected < MIN(view->nlines - 2,
2410 s->commits.ncommits - 1)) {
2411 /* can't scroll further down */
2412 s->selected = MIN(view->nlines - 2,
2413 s->commits.ncommits - 1);
2415 err = NULL;
2416 break;
2418 case KEY_RESIZE:
2419 if (s->selected > view->nlines - 2)
2420 s->selected = view->nlines - 2;
2421 if (s->selected > s->commits.ncommits - 1)
2422 s->selected = s->commits.ncommits - 1;
2423 break;
2424 case KEY_ENTER:
2425 case ' ':
2426 case '\r':
2427 if (s->selected_entry == NULL)
2428 break;
2429 if (view_is_parent_view(view))
2430 begin_x = view_split_begin_x(view->begin_x);
2431 err = open_diff_view_for_commit(&diff_view, begin_x,
2432 s->selected_entry->commit, s->selected_entry->id,
2433 view, s->repo);
2434 if (err)
2435 break;
2436 if (view_is_parent_view(view)) {
2437 err = view_close_child(view);
2438 if (err)
2439 return err;
2440 err = view_set_child(view, diff_view);
2441 if (err) {
2442 view_close(diff_view);
2443 break;
2445 *focus_view = diff_view;
2446 view->child_focussed = 1;
2447 } else
2448 *new_view = diff_view;
2449 break;
2450 case 't':
2451 if (s->selected_entry == NULL)
2452 break;
2453 if (view_is_parent_view(view))
2454 begin_x = view_split_begin_x(view->begin_x);
2455 err = browse_commit_tree(&tree_view, begin_x,
2456 s->selected_entry, s->in_repo_path, s->repo);
2457 if (err)
2458 break;
2459 if (view_is_parent_view(view)) {
2460 err = view_close_child(view);
2461 if (err)
2462 return err;
2463 err = view_set_child(view, tree_view);
2464 if (err) {
2465 view_close(tree_view);
2466 break;
2468 *focus_view = tree_view;
2469 view->child_focussed = 1;
2470 } else
2471 *new_view = tree_view;
2472 break;
2473 case KEY_BACKSPACE:
2474 if (got_path_cmp(s->in_repo_path, "/",
2475 strlen(s->in_repo_path), 1) == 0)
2476 break;
2477 err = got_path_dirname(&parent_path, s->in_repo_path);
2478 if (err)
2479 return err;
2480 err = stop_log_thread(s);
2481 if (err) {
2482 free(parent_path);
2483 return err;
2485 lv = view_open(view->nlines, view->ncols,
2486 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2487 if (lv == NULL) {
2488 free(parent_path);
2489 return got_error_from_errno("view_open");
2491 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2492 parent_path, s->log_branches);
2493 free(parent_path);
2494 if (err)
2495 return err;;
2496 if (view_is_parent_view(view))
2497 *new_view = lv;
2498 else {
2499 view_set_child(view->parent, lv);
2500 *focus_view = lv;
2502 break;
2503 case CTRL('l'):
2504 err = stop_log_thread(s);
2505 if (err)
2506 return err;
2507 lv = view_open(view->nlines, view->ncols,
2508 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2509 if (lv == NULL)
2510 return got_error_from_errno("view_open");
2511 err = got_repo_match_object_id(&start_id, NULL,
2512 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2513 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2514 if (err) {
2515 view_close(lv);
2516 return err;
2518 in_repo_path = strdup(s->in_repo_path);
2519 if (in_repo_path == NULL) {
2520 free(start_id);
2521 view_close(lv);
2522 return got_error_from_errno("strdup");
2524 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2525 in_repo_path, s->log_branches);
2526 if (err) {
2527 free(start_id);
2528 view_close(lv);
2529 return err;;
2531 *dead_view = view;
2532 *new_view = lv;
2533 break;
2534 case 'B':
2535 s->log_branches = !s->log_branches;
2536 err = stop_log_thread(s);
2537 if (err)
2538 return err;
2539 lv = view_open(view->nlines, view->ncols,
2540 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2541 if (lv == NULL)
2542 return got_error_from_errno("view_open");
2543 err = open_log_view(lv, s->start_id, s->repo,
2544 s->head_ref_name, s->in_repo_path, s->log_branches);
2545 if (err) {
2546 view_close(lv);
2547 return err;;
2549 *dead_view = view;
2550 *new_view = lv;
2551 break;
2552 case 'r':
2553 if (view_is_parent_view(view))
2554 begin_x = view_split_begin_x(view->begin_x);
2555 ref_view = view_open(view->nlines, view->ncols,
2556 view->begin_y, begin_x, TOG_VIEW_REF);
2557 if (ref_view == NULL)
2558 return got_error_from_errno("view_open");
2559 err = open_ref_view(ref_view, s->repo);
2560 if (err) {
2561 view_close(ref_view);
2562 return err;
2564 if (view_is_parent_view(view)) {
2565 err = view_close_child(view);
2566 if (err)
2567 return err;
2568 err = view_set_child(view, ref_view);
2569 if (err) {
2570 view_close(ref_view);
2571 break;
2573 *focus_view = ref_view;
2574 view->child_focussed = 1;
2575 } else
2576 *new_view = ref_view;
2577 break;
2578 default:
2579 break;
2582 return err;
2585 static const struct got_error *
2586 apply_unveil(const char *repo_path, const char *worktree_path)
2588 const struct got_error *error;
2590 #ifdef PROFILE
2591 if (unveil("gmon.out", "rwc") != 0)
2592 return got_error_from_errno2("unveil", "gmon.out");
2593 #endif
2594 if (repo_path && unveil(repo_path, "r") != 0)
2595 return got_error_from_errno2("unveil", repo_path);
2597 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2598 return got_error_from_errno2("unveil", worktree_path);
2600 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2601 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2603 error = got_privsep_unveil_exec_helpers();
2604 if (error != NULL)
2605 return error;
2607 if (unveil(NULL, NULL) != 0)
2608 return got_error_from_errno("unveil");
2610 return NULL;
2613 static void
2614 init_curses(void)
2616 initscr();
2617 cbreak();
2618 halfdelay(1); /* Do fast refresh while initial view is loading. */
2619 noecho();
2620 nonl();
2621 intrflush(stdscr, FALSE);
2622 keypad(stdscr, TRUE);
2623 curs_set(0);
2624 if (getenv("TOG_COLORS") != NULL) {
2625 start_color();
2626 use_default_colors();
2628 signal(SIGWINCH, tog_sigwinch);
2629 signal(SIGPIPE, tog_sigpipe);
2630 signal(SIGCONT, tog_sigcont);
2633 static const struct got_error *
2634 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2635 struct got_repository *repo, struct got_worktree *worktree)
2637 const struct got_error *err = NULL;
2639 if (argc == 0) {
2640 *in_repo_path = strdup("/");
2641 if (*in_repo_path == NULL)
2642 return got_error_from_errno("strdup");
2643 return NULL;
2646 if (worktree) {
2647 const char *prefix = got_worktree_get_path_prefix(worktree);
2648 char *p;
2650 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2651 if (err)
2652 return err;
2653 if (asprintf(in_repo_path, "%s%s%s", prefix,
2654 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2655 p) == -1) {
2656 err = got_error_from_errno("asprintf");
2657 *in_repo_path = NULL;
2659 free(p);
2660 } else
2661 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2663 return err;
2666 static const struct got_error *
2667 cmd_log(int argc, char *argv[])
2669 const struct got_error *error;
2670 struct got_repository *repo = NULL;
2671 struct got_worktree *worktree = NULL;
2672 struct got_object_id *start_id = NULL;
2673 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2674 char *start_commit = NULL, *head_ref_name = NULL;
2675 int ch, log_branches = 0;
2676 struct tog_view *view;
2678 #ifndef PROFILE
2679 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2680 NULL) == -1)
2681 err(1, "pledge");
2682 #endif
2684 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2685 switch (ch) {
2686 case 'b':
2687 log_branches = 1;
2688 break;
2689 case 'c':
2690 start_commit = optarg;
2691 break;
2692 case 'r':
2693 repo_path = realpath(optarg, NULL);
2694 if (repo_path == NULL)
2695 return got_error_from_errno2("realpath",
2696 optarg);
2697 break;
2698 default:
2699 usage_log();
2700 /* NOTREACHED */
2704 argc -= optind;
2705 argv += optind;
2707 if (argc > 1)
2708 usage_log();
2710 cwd = getcwd(NULL, 0);
2711 if (cwd == NULL)
2712 return got_error_from_errno("getcwd");
2714 error = got_worktree_open(&worktree, cwd);
2715 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2716 goto done;
2718 if (repo_path == NULL) {
2719 if (worktree)
2720 repo_path =
2721 strdup(got_worktree_get_repo_path(worktree));
2722 else
2723 repo_path = strdup(cwd);
2725 if (repo_path == NULL) {
2726 error = got_error_from_errno("strdup");
2727 goto done;
2730 error = got_repo_open(&repo, repo_path, NULL);
2731 if (error != NULL)
2732 goto done;
2734 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2735 repo, worktree);
2736 if (error)
2737 goto done;
2739 init_curses();
2741 error = apply_unveil(got_repo_get_path(repo),
2742 worktree ? got_worktree_get_root_path(worktree) : NULL);
2743 if (error)
2744 goto done;
2746 if (start_commit == NULL)
2747 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2748 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2749 GOT_OBJ_TYPE_COMMIT, 1, repo);
2750 else
2751 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2752 GOT_OBJ_TYPE_COMMIT, 1, repo);
2753 if (error != NULL)
2754 goto done;
2756 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2757 if (view == NULL) {
2758 error = got_error_from_errno("view_open");
2759 goto done;
2761 if (worktree) {
2762 head_ref_name = strdup(
2763 got_worktree_get_head_ref_name(worktree));
2764 if (head_ref_name == NULL) {
2765 error = got_error_from_errno("strdup");
2766 goto done;
2769 error = open_log_view(view, start_id, repo, head_ref_name,
2770 in_repo_path, log_branches);
2771 if (error)
2772 goto done;
2773 if (worktree) {
2774 /* Release work tree lock. */
2775 got_worktree_close(worktree);
2776 worktree = NULL;
2778 error = view_loop(view);
2779 done:
2780 free(in_repo_path);
2781 free(repo_path);
2782 free(cwd);
2783 free(start_id);
2784 free(head_ref_name);
2785 if (repo)
2786 got_repo_close(repo);
2787 if (worktree)
2788 got_worktree_close(worktree);
2789 return error;
2792 __dead static void
2793 usage_diff(void)
2795 endwin();
2796 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2797 "[-w] object1 object2\n", getprogname());
2798 exit(1);
2801 static char *
2802 parse_next_line(FILE *f, size_t *len)
2804 char *line;
2805 size_t linelen;
2806 size_t lineno;
2807 const char delim[3] = { '\0', '\0', '\0'};
2809 line = fparseln(f, &linelen, &lineno, delim, 0);
2810 if (len)
2811 *len = linelen;
2812 return line;
2815 static int
2816 match_line(const char *line, regex_t *regex, size_t nmatch,
2817 regmatch_t *regmatch)
2819 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2822 struct tog_color *
2823 match_color(struct tog_colors *colors, const char *line)
2825 struct tog_color *tc = NULL;
2827 SIMPLEQ_FOREACH(tc, colors, entry) {
2828 if (match_line(line, &tc->regex, 0, NULL))
2829 return tc;
2832 return NULL;
2835 static const struct got_error *
2836 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2837 WINDOW *window, regmatch_t *regmatch)
2839 const struct got_error *err = NULL;
2840 wchar_t *wline;
2841 int width;
2842 char *s;
2844 *wtotal = 0;
2846 s = strndup(line, regmatch->rm_so);
2847 if (s == NULL)
2848 return got_error_from_errno("strndup");
2850 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2851 if (err) {
2852 free(s);
2853 return err;
2855 waddwstr(window, wline);
2856 free(wline);
2857 free(s);
2858 wlimit -= width;
2859 *wtotal += width;
2861 if (wlimit > 0) {
2862 s = strndup(line + regmatch->rm_so,
2863 regmatch->rm_eo - regmatch->rm_so);
2864 if (s == NULL) {
2865 err = got_error_from_errno("strndup");
2866 free(s);
2867 return err;
2869 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2870 if (err) {
2871 free(s);
2872 return err;
2874 wattr_on(window, A_STANDOUT, NULL);
2875 waddwstr(window, wline);
2876 wattr_off(window, A_STANDOUT, NULL);
2877 free(wline);
2878 free(s);
2879 wlimit -= width;
2880 *wtotal += width;
2883 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2884 err = format_line(&wline, &width,
2885 line + regmatch->rm_eo, wlimit, col_tab_align);
2886 if (err)
2887 return err;
2888 waddwstr(window, wline);
2889 free(wline);
2890 *wtotal += width;
2893 return NULL;
2896 static const struct got_error *
2897 draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2898 off_t *line_offsets, int selected_line, int max_lines,
2899 int *last_displayed_line, int *eof, const char *header,
2900 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
2902 const struct got_error *err;
2903 int nprinted = 0;
2904 char *line;
2905 struct tog_color *tc;
2906 size_t len;
2907 wchar_t *wline;
2908 int width;
2909 off_t line_offset;
2911 line_offset = line_offsets[first_displayed_line - 1];
2912 if (fseeko(f, line_offset, SEEK_SET) == -1)
2913 return got_error_from_errno("fseek");
2915 werase(view->window);
2917 if (header) {
2918 if (asprintf(&line, "[%d/%d] %s",
2919 first_displayed_line - 1 + selected_line, nlines,
2920 header) == -1)
2921 return got_error_from_errno("asprintf");
2922 err = format_line(&wline, &width, line, view->ncols, 0);
2923 free(line);
2924 if (err)
2925 return err;
2927 if (view_needs_focus_indication(view))
2928 wstandout(view->window);
2929 waddwstr(view->window, wline);
2930 free(wline);
2931 wline = NULL;
2932 if (view_needs_focus_indication(view))
2933 wstandend(view->window);
2934 if (width <= view->ncols - 1)
2935 waddch(view->window, '\n');
2937 if (max_lines <= 1)
2938 return NULL;
2939 max_lines--;
2942 *eof = 0;
2943 while (max_lines > 0 && nprinted < max_lines) {
2944 line = parse_next_line(f, &len);
2945 if (line == NULL) {
2946 *eof = 1;
2947 break;
2950 tc = match_color(colors, line);
2951 if (tc)
2952 wattr_on(view->window,
2953 COLOR_PAIR(tc->colorpair), NULL);
2954 if (first_displayed_line + nprinted == matched_line &&
2955 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2956 err = add_matched_line(&width, line, view->ncols, 0,
2957 view->window, regmatch);
2958 if (err) {
2959 free(line);
2960 return err;
2962 } else {
2963 err = format_line(&wline, &width, line, view->ncols, 0);
2964 if (err) {
2965 free(line);
2966 return err;
2968 waddwstr(view->window, wline);
2969 free(wline);
2970 wline = NULL;
2972 if (tc)
2973 wattr_off(view->window,
2974 COLOR_PAIR(tc->colorpair), NULL);
2975 if (width <= view->ncols - 1)
2976 waddch(view->window, '\n');
2977 nprinted++;
2978 free(line);
2980 if (nprinted >= 1)
2981 *last_displayed_line = first_displayed_line + (nprinted - 1);
2982 else
2983 *last_displayed_line = first_displayed_line;
2985 view_vborder(view);
2987 if (*eof) {
2988 while (nprinted < view->nlines) {
2989 waddch(view->window, '\n');
2990 nprinted++;
2993 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2994 if (err) {
2995 return err;
2998 wstandout(view->window);
2999 waddwstr(view->window, wline);
3000 free(wline);
3001 wline = NULL;
3002 wstandend(view->window);
3005 return NULL;
3008 static char *
3009 get_datestr(time_t *time, char *datebuf)
3011 struct tm mytm, *tm;
3012 char *p, *s;
3014 tm = gmtime_r(time, &mytm);
3015 if (tm == NULL)
3016 return NULL;
3017 s = asctime_r(tm, datebuf);
3018 if (s == NULL)
3019 return NULL;
3020 p = strchr(s, '\n');
3021 if (p)
3022 *p = '\0';
3023 return s;
3026 static const struct got_error *
3027 get_changed_paths(struct got_pathlist_head *paths,
3028 struct got_commit_object *commit, struct got_repository *repo)
3030 const struct got_error *err = NULL;
3031 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3032 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3033 struct got_object_qid *qid;
3035 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3036 if (qid != NULL) {
3037 struct got_commit_object *pcommit;
3038 err = got_object_open_as_commit(&pcommit, repo,
3039 qid->id);
3040 if (err)
3041 return err;
3043 tree_id1 = got_object_commit_get_tree_id(pcommit);
3044 got_object_commit_close(pcommit);
3048 if (tree_id1) {
3049 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3050 if (err)
3051 goto done;
3054 tree_id2 = got_object_commit_get_tree_id(commit);
3055 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3056 if (err)
3057 goto done;
3059 err = got_diff_tree(tree1, tree2, "", "", repo,
3060 got_diff_tree_collect_changed_paths, paths, 0);
3061 done:
3062 if (tree1)
3063 got_object_tree_close(tree1);
3064 if (tree2)
3065 got_object_tree_close(tree2);
3066 return err;
3069 static const struct got_error *
3070 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3072 off_t *p;
3074 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3075 if (p == NULL)
3076 return got_error_from_errno("reallocarray");
3077 *line_offsets = p;
3078 (*line_offsets)[*nlines] = off;
3079 (*nlines)++;
3080 return NULL;
3083 static const struct got_error *
3084 write_commit_info(off_t **line_offsets, size_t *nlines,
3085 struct got_object_id *commit_id, struct got_reflist_head *refs,
3086 struct got_repository *repo, FILE *outfile)
3088 const struct got_error *err = NULL;
3089 char datebuf[26], *datestr;
3090 struct got_commit_object *commit;
3091 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3092 time_t committer_time;
3093 const char *author, *committer;
3094 char *refs_str = NULL;
3095 struct got_pathlist_head changed_paths;
3096 struct got_pathlist_entry *pe;
3097 off_t outoff = 0;
3098 int n;
3100 TAILQ_INIT(&changed_paths);
3102 if (refs) {
3103 err = build_refs_str(&refs_str, refs, commit_id, repo);
3104 if (err)
3105 return err;
3108 err = got_object_open_as_commit(&commit, repo, commit_id);
3109 if (err)
3110 return err;
3112 err = got_object_id_str(&id_str, commit_id);
3113 if (err) {
3114 err = got_error_from_errno("got_object_id_str");
3115 goto done;
3118 err = add_line_offset(line_offsets, nlines, 0);
3119 if (err)
3120 goto done;
3122 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3123 refs_str ? refs_str : "", refs_str ? ")" : "");
3124 if (n < 0) {
3125 err = got_error_from_errno("fprintf");
3126 goto done;
3128 outoff += n;
3129 err = add_line_offset(line_offsets, nlines, outoff);
3130 if (err)
3131 goto done;
3133 n = fprintf(outfile, "from: %s\n",
3134 got_object_commit_get_author(commit));
3135 if (n < 0) {
3136 err = got_error_from_errno("fprintf");
3137 goto done;
3139 outoff += n;
3140 err = add_line_offset(line_offsets, nlines, outoff);
3141 if (err)
3142 goto done;
3144 committer_time = got_object_commit_get_committer_time(commit);
3145 datestr = get_datestr(&committer_time, datebuf);
3146 if (datestr) {
3147 n = fprintf(outfile, "date: %s UTC\n", datestr);
3148 if (n < 0) {
3149 err = got_error_from_errno("fprintf");
3150 goto done;
3152 outoff += n;
3153 err = add_line_offset(line_offsets, nlines, outoff);
3154 if (err)
3155 goto done;
3157 author = got_object_commit_get_author(commit);
3158 committer = got_object_commit_get_committer(commit);
3159 if (strcmp(author, committer) != 0) {
3160 n = fprintf(outfile, "via: %s\n", committer);
3161 if (n < 0) {
3162 err = got_error_from_errno("fprintf");
3163 goto done;
3165 outoff += n;
3166 err = add_line_offset(line_offsets, nlines, outoff);
3167 if (err)
3168 goto done;
3170 err = got_object_commit_get_logmsg(&logmsg, commit);
3171 if (err)
3172 goto done;
3173 s = logmsg;
3174 while ((line = strsep(&s, "\n")) != NULL) {
3175 n = fprintf(outfile, "%s\n", line);
3176 if (n < 0) {
3177 err = got_error_from_errno("fprintf");
3178 goto done;
3180 outoff += n;
3181 err = add_line_offset(line_offsets, nlines, outoff);
3182 if (err)
3183 goto done;
3186 err = get_changed_paths(&changed_paths, commit, repo);
3187 if (err)
3188 goto done;
3189 TAILQ_FOREACH(pe, &changed_paths, entry) {
3190 struct got_diff_changed_path *cp = pe->data;
3191 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3192 if (n < 0) {
3193 err = got_error_from_errno("fprintf");
3194 goto done;
3196 outoff += n;
3197 err = add_line_offset(line_offsets, nlines, outoff);
3198 if (err)
3199 goto done;
3200 free((char *)pe->path);
3201 free(pe->data);
3204 fputc('\n', outfile);
3205 outoff++;
3206 err = add_line_offset(line_offsets, nlines, outoff);
3207 done:
3208 got_pathlist_free(&changed_paths);
3209 free(id_str);
3210 free(logmsg);
3211 free(refs_str);
3212 got_object_commit_close(commit);
3213 if (err) {
3214 free(*line_offsets);
3215 *line_offsets = NULL;
3216 *nlines = 0;
3218 return err;
3221 static const struct got_error *
3222 create_diff(struct tog_diff_view_state *s)
3224 const struct got_error *err = NULL;
3225 FILE *f = NULL;
3226 int obj_type;
3228 free(s->line_offsets);
3229 s->line_offsets = malloc(sizeof(off_t));
3230 if (s->line_offsets == NULL)
3231 return got_error_from_errno("malloc");
3232 s->nlines = 0;
3234 f = got_opentemp();
3235 if (f == NULL) {
3236 err = got_error_from_errno("got_opentemp");
3237 goto done;
3239 if (s->f && fclose(s->f) != 0) {
3240 err = got_error_from_errno("fclose");
3241 goto done;
3243 s->f = f;
3245 if (s->id1)
3246 err = got_object_get_type(&obj_type, s->repo, s->id1);
3247 else
3248 err = got_object_get_type(&obj_type, s->repo, s->id2);
3249 if (err)
3250 goto done;
3252 switch (obj_type) {
3253 case GOT_OBJ_TYPE_BLOB:
3254 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3255 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3256 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3257 break;
3258 case GOT_OBJ_TYPE_TREE:
3259 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3260 s->id1, s->id2, "", "", s->diff_context,
3261 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3262 break;
3263 case GOT_OBJ_TYPE_COMMIT: {
3264 const struct got_object_id_queue *parent_ids;
3265 struct got_object_qid *pid;
3266 struct got_commit_object *commit2;
3268 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3269 if (err)
3270 goto done;
3271 /* Show commit info if we're diffing to a parent/root commit. */
3272 if (s->id1 == NULL) {
3273 err = write_commit_info(&s->line_offsets, &s->nlines,
3274 s->id2, &s->refs, s->repo, s->f);
3275 if (err)
3276 goto done;
3277 } else {
3278 parent_ids = got_object_commit_get_parent_ids(commit2);
3279 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3280 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3281 err = write_commit_info(
3282 &s->line_offsets, &s->nlines,
3283 s->id2, &s->refs, s->repo, s->f);
3284 if (err)
3285 goto done;
3286 break;
3290 got_object_commit_close(commit2);
3292 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3293 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3294 s->force_text_diff, s->repo, s->f);
3295 break;
3297 default:
3298 err = got_error(GOT_ERR_OBJ_TYPE);
3299 break;
3301 if (err)
3302 goto done;
3303 done:
3304 if (s->f && fflush(s->f) != 0 && err == NULL)
3305 err = got_error_from_errno("fflush");
3306 return err;
3309 static void
3310 diff_view_indicate_progress(struct tog_view *view)
3312 mvwaddstr(view->window, 0, 0, "diffing...");
3313 update_panels();
3314 doupdate();
3317 static const struct got_error *
3318 search_start_diff_view(struct tog_view *view)
3320 struct tog_diff_view_state *s = &view->state.diff;
3322 s->matched_line = 0;
3323 return NULL;
3326 static const struct got_error *
3327 search_next_diff_view(struct tog_view *view)
3329 struct tog_diff_view_state *s = &view->state.diff;
3330 int lineno;
3332 if (!view->searching) {
3333 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3334 return NULL;
3337 if (s->matched_line) {
3338 if (view->searching == TOG_SEARCH_FORWARD)
3339 lineno = s->matched_line + 1;
3340 else
3341 lineno = s->matched_line - 1;
3342 } else {
3343 if (view->searching == TOG_SEARCH_FORWARD)
3344 lineno = 1;
3345 else
3346 lineno = s->nlines;
3349 while (1) {
3350 char *line = NULL;
3351 off_t offset;
3352 size_t len;
3354 if (lineno <= 0 || lineno > s->nlines) {
3355 if (s->matched_line == 0) {
3356 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3357 free(line);
3358 break;
3361 if (view->searching == TOG_SEARCH_FORWARD)
3362 lineno = 1;
3363 else
3364 lineno = s->nlines;
3367 offset = s->line_offsets[lineno - 1];
3368 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3369 free(line);
3370 return got_error_from_errno("fseeko");
3372 free(line);
3373 line = parse_next_line(s->f, &len);
3374 if (line &&
3375 match_line(line, &view->regex, 1, &view->regmatch)) {
3376 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3377 s->matched_line = lineno;
3378 free(line);
3379 break;
3381 free(line);
3382 if (view->searching == TOG_SEARCH_FORWARD)
3383 lineno++;
3384 else
3385 lineno--;
3388 if (s->matched_line) {
3389 s->first_displayed_line = s->matched_line;
3390 s->selected_line = 1;
3393 return NULL;
3396 static const struct got_error *
3397 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3398 struct got_object_id *id2, const char *label1, const char *label2,
3399 int diff_context, int ignore_whitespace, int force_text_diff,
3400 struct tog_view *log_view, struct got_repository *repo)
3402 const struct got_error *err;
3403 struct tog_diff_view_state *s = &view->state.diff;
3405 SIMPLEQ_INIT(&s->refs);
3407 if (id1 != NULL && id2 != NULL) {
3408 int type1, type2;
3409 err = got_object_get_type(&type1, repo, id1);
3410 if (err)
3411 return err;
3412 err = got_object_get_type(&type2, repo, id2);
3413 if (err)
3414 return err;
3416 if (type1 != type2)
3417 return got_error(GOT_ERR_OBJ_TYPE);
3419 s->first_displayed_line = 1;
3420 s->last_displayed_line = view->nlines;
3421 s->selected_line = 1;
3422 s->repo = repo;
3423 s->id1 = id1;
3424 s->id2 = id2;
3425 s->label1 = label1;
3426 s->label2 = label2;
3428 if (id1) {
3429 s->id1 = got_object_id_dup(id1);
3430 if (s->id1 == NULL)
3431 return got_error_from_errno("got_object_id_dup");
3432 } else
3433 s->id1 = NULL;
3435 s->id2 = got_object_id_dup(id2);
3436 if (s->id2 == NULL) {
3437 free(s->id1);
3438 s->id1 = NULL;
3439 return got_error_from_errno("got_object_id_dup");
3441 s->f = NULL;
3442 s->first_displayed_line = 1;
3443 s->last_displayed_line = view->nlines;
3444 s->diff_context = diff_context;
3445 s->ignore_whitespace = ignore_whitespace;
3446 s->force_text_diff = force_text_diff;
3447 s->log_view = log_view;
3448 s->repo = repo;
3450 SIMPLEQ_INIT(&s->colors);
3451 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3452 err = add_color(&s->colors,
3453 "^-", TOG_COLOR_DIFF_MINUS,
3454 get_color_value("TOG_COLOR_DIFF_MINUS"));
3455 if (err)
3456 return err;
3457 err = add_color(&s->colors, "^\\+",
3458 TOG_COLOR_DIFF_PLUS,
3459 get_color_value("TOG_COLOR_DIFF_PLUS"));
3460 if (err) {
3461 free_colors(&s->colors);
3462 return err;
3464 err = add_color(&s->colors,
3465 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3466 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3467 if (err) {
3468 free_colors(&s->colors);
3469 return err;
3472 err = add_color(&s->colors,
3473 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3474 TOG_COLOR_DIFF_META,
3475 get_color_value("TOG_COLOR_DIFF_META"));
3476 if (err) {
3477 free_colors(&s->colors);
3478 return err;
3481 err = add_color(&s->colors,
3482 "^(from|via): ", TOG_COLOR_AUTHOR,
3483 get_color_value("TOG_COLOR_AUTHOR"));
3484 if (err) {
3485 free_colors(&s->colors);
3486 return err;
3489 err = add_color(&s->colors,
3490 "^date: ", TOG_COLOR_DATE,
3491 get_color_value("TOG_COLOR_DATE"));
3492 if (err) {
3493 free_colors(&s->colors);
3494 return err;
3498 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3499 if (err) {
3500 free(s->id1);
3501 s->id1 = NULL;
3502 free(s->id2);
3503 s->id2 = NULL;
3504 free_colors(&s->colors);
3505 return err;
3508 if (log_view && view_is_splitscreen(view))
3509 show_log_view(log_view); /* draw vborder */
3510 diff_view_indicate_progress(view);
3512 s->line_offsets = NULL;
3513 s->nlines = 0;
3514 err = create_diff(s);
3515 if (err) {
3516 free(s->id1);
3517 s->id1 = NULL;
3518 free(s->id2);
3519 s->id2 = NULL;
3520 free_colors(&s->colors);
3521 got_ref_list_free(&s->refs);
3522 return err;
3525 view->show = show_diff_view;
3526 view->input = input_diff_view;
3527 view->close = close_diff_view;
3528 view->search_start = search_start_diff_view;
3529 view->search_next = search_next_diff_view;
3531 return NULL;
3534 static const struct got_error *
3535 close_diff_view(struct tog_view *view)
3537 const struct got_error *err = NULL;
3538 struct tog_diff_view_state *s = &view->state.diff;
3540 free(s->id1);
3541 s->id1 = NULL;
3542 free(s->id2);
3543 s->id2 = NULL;
3544 if (s->f && fclose(s->f) == EOF)
3545 err = got_error_from_errno("fclose");
3546 free_colors(&s->colors);
3547 free(s->line_offsets);
3548 s->line_offsets = NULL;
3549 s->nlines = 0;
3550 got_ref_list_free(&s->refs);
3551 return err;
3554 static const struct got_error *
3555 show_diff_view(struct tog_view *view)
3557 const struct got_error *err;
3558 struct tog_diff_view_state *s = &view->state.diff;
3559 char *id_str1 = NULL, *id_str2, *header;
3560 const char *label1, *label2;
3562 if (s->id1) {
3563 err = got_object_id_str(&id_str1, s->id1);
3564 if (err)
3565 return err;
3566 label1 = s->label1 ? : id_str1;
3567 } else
3568 label1 = "/dev/null";
3570 err = got_object_id_str(&id_str2, s->id2);
3571 if (err)
3572 return err;
3573 label2 = s->label2 ? : id_str2;
3575 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3576 err = got_error_from_errno("asprintf");
3577 free(id_str1);
3578 free(id_str2);
3579 return err;
3581 free(id_str1);
3582 free(id_str2);
3584 return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3585 s->line_offsets, s->selected_line, view->nlines,
3586 &s->last_displayed_line, &s->eof, header, &s->colors,
3587 s->matched_line, &view->regmatch);
3590 static const struct got_error *
3591 set_selected_commit(struct tog_diff_view_state *s,
3592 struct commit_queue_entry *entry)
3594 const struct got_error *err;
3595 const struct got_object_id_queue *parent_ids;
3596 struct got_commit_object *selected_commit;
3597 struct got_object_qid *pid;
3599 free(s->id2);
3600 s->id2 = got_object_id_dup(entry->id);
3601 if (s->id2 == NULL)
3602 return got_error_from_errno("got_object_id_dup");
3604 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3605 if (err)
3606 return err;
3607 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3608 free(s->id1);
3609 pid = SIMPLEQ_FIRST(parent_ids);
3610 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3611 got_object_commit_close(selected_commit);
3612 return NULL;
3615 static const struct got_error *
3616 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3617 struct tog_view **focus_view, struct tog_view *view, int ch)
3619 const struct got_error *err = NULL;
3620 struct tog_diff_view_state *s = &view->state.diff;
3621 struct tog_log_view_state *ls;
3622 struct commit_queue_entry *entry;
3623 int i;
3625 switch (ch) {
3626 case 'a':
3627 case 'w':
3628 if (ch == 'a')
3629 s->force_text_diff = !s->force_text_diff;
3630 if (ch == 'w')
3631 s->ignore_whitespace = !s->ignore_whitespace;
3632 wclear(view->window);
3633 s->first_displayed_line = 1;
3634 s->last_displayed_line = view->nlines;
3635 diff_view_indicate_progress(view);
3636 err = create_diff(s);
3637 break;
3638 case 'k':
3639 case KEY_UP:
3640 if (s->first_displayed_line > 1)
3641 s->first_displayed_line--;
3642 break;
3643 case KEY_PPAGE:
3644 case CTRL('b'):
3645 if (s->first_displayed_line == 1)
3646 break;
3647 i = 0;
3648 while (i++ < view->nlines - 1 &&
3649 s->first_displayed_line > 1)
3650 s->first_displayed_line--;
3651 break;
3652 case 'j':
3653 case KEY_DOWN:
3654 if (!s->eof)
3655 s->first_displayed_line++;
3656 break;
3657 case KEY_NPAGE:
3658 case CTRL('f'):
3659 case ' ':
3660 if (s->eof)
3661 break;
3662 i = 0;
3663 while (!s->eof && i++ < view->nlines - 1) {
3664 char *line;
3665 line = parse_next_line(s->f, NULL);
3666 s->first_displayed_line++;
3667 if (line == NULL)
3668 break;
3670 break;
3671 case '[':
3672 if (s->diff_context > 0) {
3673 s->diff_context--;
3674 diff_view_indicate_progress(view);
3675 err = create_diff(s);
3676 if (s->first_displayed_line + view->nlines - 1 >
3677 s->nlines) {
3678 s->first_displayed_line = 1;
3679 s->last_displayed_line = view->nlines;
3682 break;
3683 case ']':
3684 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3685 s->diff_context++;
3686 diff_view_indicate_progress(view);
3687 err = create_diff(s);
3689 break;
3690 case '<':
3691 case ',':
3692 if (s->log_view == NULL)
3693 break;
3694 ls = &s->log_view->state.log;
3695 entry = TAILQ_PREV(ls->selected_entry,
3696 commit_queue_head, entry);
3697 if (entry == NULL)
3698 break;
3700 err = input_log_view(NULL, NULL, NULL, s->log_view,
3701 KEY_UP);
3702 if (err)
3703 break;
3705 err = set_selected_commit(s, entry);
3706 if (err)
3707 break;
3709 s->first_displayed_line = 1;
3710 s->last_displayed_line = view->nlines;
3712 diff_view_indicate_progress(view);
3713 err = create_diff(s);
3714 break;
3715 case '>':
3716 case '.':
3717 if (s->log_view == NULL)
3718 break;
3719 ls = &s->log_view->state.log;
3721 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3722 ls->thread_args.commits_needed++;
3723 err = trigger_log_thread(s->log_view, 1);
3724 if (err)
3725 break;
3727 err = input_log_view(NULL, NULL, NULL, s->log_view,
3728 KEY_DOWN);
3729 if (err)
3730 break;
3732 entry = TAILQ_NEXT(ls->selected_entry, entry);
3733 if (entry == NULL)
3734 break;
3736 err = set_selected_commit(s, entry);
3737 if (err)
3738 break;
3740 s->first_displayed_line = 1;
3741 s->last_displayed_line = view->nlines;
3743 diff_view_indicate_progress(view);
3744 err = create_diff(s);
3745 break;
3746 default:
3747 break;
3750 return err;
3753 static const struct got_error *
3754 cmd_diff(int argc, char *argv[])
3756 const struct got_error *error = NULL;
3757 struct got_repository *repo = NULL;
3758 struct got_worktree *worktree = NULL;
3759 struct got_object_id *id1 = NULL, *id2 = NULL;
3760 char *repo_path = NULL, *cwd = NULL;
3761 char *id_str1 = NULL, *id_str2 = NULL;
3762 char *label1 = NULL, *label2 = NULL;
3763 int diff_context = 3, ignore_whitespace = 0;
3764 int ch, force_text_diff = 0;
3765 const char *errstr;
3766 struct tog_view *view;
3768 #ifndef PROFILE
3769 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3770 NULL) == -1)
3771 err(1, "pledge");
3772 #endif
3773 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3774 switch (ch) {
3775 case 'a':
3776 force_text_diff = 1;
3777 break;
3778 case 'C':
3779 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3780 &errstr);
3781 if (errstr != NULL)
3782 err(1, "-C option %s", errstr);
3783 break;
3784 case 'r':
3785 repo_path = realpath(optarg, NULL);
3786 if (repo_path == NULL)
3787 return got_error_from_errno2("realpath",
3788 optarg);
3789 got_path_strip_trailing_slashes(repo_path);
3790 break;
3791 case 'w':
3792 ignore_whitespace = 1;
3793 break;
3794 default:
3795 usage_diff();
3796 /* NOTREACHED */
3800 argc -= optind;
3801 argv += optind;
3803 if (argc == 0) {
3804 usage_diff(); /* TODO show local worktree changes */
3805 } else if (argc == 2) {
3806 id_str1 = argv[0];
3807 id_str2 = argv[1];
3808 } else
3809 usage_diff();
3811 cwd = getcwd(NULL, 0);
3812 if (cwd == NULL)
3813 return got_error_from_errno("getcwd");
3815 error = got_worktree_open(&worktree, cwd);
3816 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3817 goto done;
3819 if (repo_path == NULL) {
3820 if (worktree)
3821 repo_path =
3822 strdup(got_worktree_get_repo_path(worktree));
3823 else
3824 repo_path = strdup(cwd);
3826 if (repo_path == NULL) {
3827 error = got_error_from_errno("strdup");
3828 goto done;
3831 error = got_repo_open(&repo, repo_path, NULL);
3832 if (error)
3833 goto done;
3835 init_curses();
3837 error = apply_unveil(got_repo_get_path(repo), NULL);
3838 if (error)
3839 goto done;
3841 error = got_repo_match_object_id(&id1, &label1, id_str1,
3842 GOT_OBJ_TYPE_ANY, 1, repo);
3843 if (error)
3844 goto done;
3846 error = got_repo_match_object_id(&id2, &label2, id_str2,
3847 GOT_OBJ_TYPE_ANY, 1, repo);
3848 if (error)
3849 goto done;
3851 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3852 if (view == NULL) {
3853 error = got_error_from_errno("view_open");
3854 goto done;
3856 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3857 ignore_whitespace, force_text_diff, NULL, repo);
3858 if (error)
3859 goto done;
3860 error = view_loop(view);
3861 done:
3862 free(label1);
3863 free(label2);
3864 free(repo_path);
3865 free(cwd);
3866 if (repo)
3867 got_repo_close(repo);
3868 if (worktree)
3869 got_worktree_close(worktree);
3870 return error;
3873 __dead static void
3874 usage_blame(void)
3876 endwin();
3877 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3878 getprogname());
3879 exit(1);
3882 struct tog_blame_line {
3883 int annotated;
3884 struct got_object_id *id;
3887 static const struct got_error *
3888 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3889 const char *path, struct tog_blame_line *lines, int nlines,
3890 int blame_complete, int selected_line, int *first_displayed_line,
3891 int *last_displayed_line, int *eof, int max_lines,
3892 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
3894 const struct got_error *err;
3895 int lineno = 0, nprinted = 0;
3896 char *line;
3897 size_t len;
3898 wchar_t *wline;
3899 int width;
3900 struct tog_blame_line *blame_line;
3901 struct got_object_id *prev_id = NULL;
3902 char *id_str;
3903 struct tog_color *tc;
3905 err = got_object_id_str(&id_str, id);
3906 if (err)
3907 return err;
3909 rewind(f);
3910 werase(view->window);
3912 if (asprintf(&line, "commit %s", id_str) == -1) {
3913 err = got_error_from_errno("asprintf");
3914 free(id_str);
3915 return err;
3918 err = format_line(&wline, &width, line, view->ncols, 0);
3919 free(line);
3920 line = NULL;
3921 if (err)
3922 return err;
3923 if (view_needs_focus_indication(view))
3924 wstandout(view->window);
3925 tc = get_color(colors, TOG_COLOR_COMMIT);
3926 if (tc)
3927 wattr_on(view->window,
3928 COLOR_PAIR(tc->colorpair), NULL);
3929 waddwstr(view->window, wline);
3930 if (tc)
3931 wattr_off(view->window,
3932 COLOR_PAIR(tc->colorpair), NULL);
3933 if (view_needs_focus_indication(view))
3934 wstandend(view->window);
3935 free(wline);
3936 wline = NULL;
3937 if (width < view->ncols - 1)
3938 waddch(view->window, '\n');
3940 if (asprintf(&line, "[%d/%d] %s%s",
3941 *first_displayed_line - 1 + selected_line, nlines,
3942 blame_complete ? "" : "annotating... ", path) == -1) {
3943 free(id_str);
3944 return got_error_from_errno("asprintf");
3946 free(id_str);
3947 err = format_line(&wline, &width, line, view->ncols, 0);
3948 free(line);
3949 line = NULL;
3950 if (err)
3951 return err;
3952 waddwstr(view->window, wline);
3953 free(wline);
3954 wline = NULL;
3955 if (width < view->ncols - 1)
3956 waddch(view->window, '\n');
3958 *eof = 0;
3959 while (nprinted < max_lines - 2) {
3960 line = parse_next_line(f, &len);
3961 if (line == NULL) {
3962 *eof = 1;
3963 break;
3965 if (++lineno < *first_displayed_line) {
3966 free(line);
3967 continue;
3970 if (view->focussed && nprinted == selected_line - 1)
3971 wstandout(view->window);
3973 if (nlines > 0) {
3974 blame_line = &lines[lineno - 1];
3975 if (blame_line->annotated && prev_id &&
3976 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3977 !(view->focussed &&
3978 nprinted == selected_line - 1)) {
3979 waddstr(view->window, " ");
3980 } else if (blame_line->annotated) {
3981 char *id_str;
3982 err = got_object_id_str(&id_str, blame_line->id);
3983 if (err) {
3984 free(line);
3985 return err;
3987 tc = get_color(colors, TOG_COLOR_COMMIT);
3988 if (tc)
3989 wattr_on(view->window,
3990 COLOR_PAIR(tc->colorpair), NULL);
3991 wprintw(view->window, "%.8s", id_str);
3992 if (tc)
3993 wattr_off(view->window,
3994 COLOR_PAIR(tc->colorpair), NULL);
3995 free(id_str);
3996 prev_id = blame_line->id;
3997 } else {
3998 waddstr(view->window, "........");
3999 prev_id = NULL;
4001 } else {
4002 waddstr(view->window, "........");
4003 prev_id = NULL;
4006 if (view->focussed && nprinted == selected_line - 1)
4007 wstandend(view->window);
4008 waddstr(view->window, " ");
4010 if (view->ncols <= 9) {
4011 width = 9;
4012 wline = wcsdup(L"");
4013 if (wline == NULL) {
4014 err = got_error_from_errno("wcsdup");
4015 free(line);
4016 return err;
4018 } else if (*first_displayed_line + nprinted == matched_line &&
4019 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4020 err = add_matched_line(&width, line, view->ncols - 9, 9,
4021 view->window, regmatch);
4022 if (err) {
4023 free(line);
4024 return err;
4026 width += 9;
4027 } else {
4028 err = format_line(&wline, &width, line,
4029 view->ncols - 9, 9);
4030 waddwstr(view->window, wline);
4031 free(wline);
4032 wline = NULL;
4033 width += 9;
4036 if (width <= view->ncols - 1)
4037 waddch(view->window, '\n');
4038 if (++nprinted == 1)
4039 *first_displayed_line = lineno;
4040 free(line);
4042 *last_displayed_line = lineno;
4044 view_vborder(view);
4046 return NULL;
4049 static const struct got_error *
4050 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4052 const struct got_error *err = NULL;
4053 struct tog_blame_cb_args *a = arg;
4054 struct tog_blame_line *line;
4055 int errcode;
4057 if (nlines != a->nlines ||
4058 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4059 return got_error(GOT_ERR_RANGE);
4061 errcode = pthread_mutex_lock(&tog_mutex);
4062 if (errcode)
4063 return got_error_set_errno(errcode, "pthread_mutex_lock");
4065 if (*a->quit) { /* user has quit the blame view */
4066 err = got_error(GOT_ERR_ITER_COMPLETED);
4067 goto done;
4070 if (lineno == -1)
4071 goto done; /* no change in this commit */
4073 line = &a->lines[lineno - 1];
4074 if (line->annotated)
4075 goto done;
4077 line->id = got_object_id_dup(id);
4078 if (line->id == NULL) {
4079 err = got_error_from_errno("got_object_id_dup");
4080 goto done;
4082 line->annotated = 1;
4083 done:
4084 errcode = pthread_mutex_unlock(&tog_mutex);
4085 if (errcode)
4086 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4087 return err;
4090 static void *
4091 blame_thread(void *arg)
4093 const struct got_error *err;
4094 struct tog_blame_thread_args *ta = arg;
4095 struct tog_blame_cb_args *a = ta->cb_args;
4096 int errcode;
4098 err = block_signals_used_by_main_thread();
4099 if (err)
4100 return (void *)err;
4102 err = got_blame(ta->path, a->commit_id, ta->repo,
4103 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4104 if (err && err->code == GOT_ERR_CANCELLED)
4105 err = NULL;
4107 errcode = pthread_mutex_lock(&tog_mutex);
4108 if (errcode)
4109 return (void *)got_error_set_errno(errcode,
4110 "pthread_mutex_lock");
4112 got_repo_close(ta->repo);
4113 ta->repo = NULL;
4114 *ta->complete = 1;
4116 errcode = pthread_mutex_unlock(&tog_mutex);
4117 if (errcode && err == NULL)
4118 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4120 return (void *)err;
4123 static struct got_object_id *
4124 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4125 int first_displayed_line, int selected_line)
4127 struct tog_blame_line *line;
4129 if (nlines <= 0)
4130 return NULL;
4132 line = &lines[first_displayed_line - 1 + selected_line - 1];
4133 if (!line->annotated)
4134 return NULL;
4136 return line->id;
4139 static const struct got_error *
4140 stop_blame(struct tog_blame *blame)
4142 const struct got_error *err = NULL;
4143 int i;
4145 if (blame->thread) {
4146 int errcode;
4147 errcode = pthread_mutex_unlock(&tog_mutex);
4148 if (errcode)
4149 return got_error_set_errno(errcode,
4150 "pthread_mutex_unlock");
4151 errcode = pthread_join(blame->thread, (void **)&err);
4152 if (errcode)
4153 return got_error_set_errno(errcode, "pthread_join");
4154 errcode = pthread_mutex_lock(&tog_mutex);
4155 if (errcode)
4156 return got_error_set_errno(errcode,
4157 "pthread_mutex_lock");
4158 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4159 err = NULL;
4160 blame->thread = NULL;
4162 if (blame->thread_args.repo) {
4163 got_repo_close(blame->thread_args.repo);
4164 blame->thread_args.repo = NULL;
4166 if (blame->f) {
4167 if (fclose(blame->f) != 0 && err == NULL)
4168 err = got_error_from_errno("fclose");
4169 blame->f = NULL;
4171 if (blame->lines) {
4172 for (i = 0; i < blame->nlines; i++)
4173 free(blame->lines[i].id);
4174 free(blame->lines);
4175 blame->lines = NULL;
4177 free(blame->cb_args.commit_id);
4178 blame->cb_args.commit_id = NULL;
4180 return err;
4183 static const struct got_error *
4184 cancel_blame_view(void *arg)
4186 const struct got_error *err = NULL;
4187 int *done = arg;
4188 int errcode;
4190 errcode = pthread_mutex_lock(&tog_mutex);
4191 if (errcode)
4192 return got_error_set_errno(errcode,
4193 "pthread_mutex_unlock");
4195 if (*done)
4196 err = got_error(GOT_ERR_CANCELLED);
4198 errcode = pthread_mutex_unlock(&tog_mutex);
4199 if (errcode)
4200 return got_error_set_errno(errcode,
4201 "pthread_mutex_lock");
4203 return err;
4206 static const struct got_error *
4207 run_blame(struct tog_view *view)
4209 struct tog_blame_view_state *s = &view->state.blame;
4210 struct tog_blame *blame = &s->blame;
4211 const struct got_error *err = NULL;
4212 struct got_blob_object *blob = NULL;
4213 struct got_repository *thread_repo = NULL;
4214 struct got_object_id *obj_id = NULL;
4215 int obj_type;
4217 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4218 s->path);
4219 if (err)
4220 return err;
4222 err = got_object_get_type(&obj_type, s->repo, obj_id);
4223 if (err)
4224 goto done;
4226 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4227 err = got_error(GOT_ERR_OBJ_TYPE);
4228 goto done;
4231 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4232 if (err)
4233 goto done;
4234 blame->f = got_opentemp();
4235 if (blame->f == NULL) {
4236 err = got_error_from_errno("got_opentemp");
4237 goto done;
4239 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4240 &blame->line_offsets, blame->f, blob);
4241 if (err || blame->nlines == 0)
4242 goto done;
4244 /* Don't include \n at EOF in the blame line count. */
4245 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4246 blame->nlines--;
4248 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4249 if (blame->lines == NULL) {
4250 err = got_error_from_errno("calloc");
4251 goto done;
4254 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4255 if (err)
4256 goto done;
4258 blame->cb_args.view = view;
4259 blame->cb_args.lines = blame->lines;
4260 blame->cb_args.nlines = blame->nlines;
4261 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4262 if (blame->cb_args.commit_id == NULL) {
4263 err = got_error_from_errno("got_object_id_dup");
4264 goto done;
4266 blame->cb_args.quit = &s->done;
4268 blame->thread_args.path = s->path;
4269 blame->thread_args.repo = thread_repo;
4270 blame->thread_args.cb_args = &blame->cb_args;
4271 blame->thread_args.complete = &s->blame_complete;
4272 blame->thread_args.cancel_cb = cancel_blame_view;
4273 blame->thread_args.cancel_arg = &s->done;
4274 s->blame_complete = 0;
4276 done:
4277 if (blob)
4278 got_object_blob_close(blob);
4279 free(obj_id);
4280 if (err)
4281 stop_blame(blame);
4282 return err;
4285 static const struct got_error *
4286 open_blame_view(struct tog_view *view, char *path,
4287 struct got_object_id *commit_id, struct got_repository *repo)
4289 const struct got_error *err = NULL;
4290 struct tog_blame_view_state *s = &view->state.blame;
4292 SIMPLEQ_INIT(&s->blamed_commits);
4294 s->path = strdup(path);
4295 if (s->path == NULL)
4296 return got_error_from_errno("strdup");
4298 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4299 if (err) {
4300 free(s->path);
4301 return err;
4304 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4305 s->first_displayed_line = 1;
4306 s->last_displayed_line = view->nlines;
4307 s->selected_line = 1;
4308 s->blame_complete = 0;
4309 s->repo = repo;
4310 s->commit_id = commit_id;
4311 memset(&s->blame, 0, sizeof(s->blame));
4313 SIMPLEQ_INIT(&s->colors);
4314 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4315 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4316 get_color_value("TOG_COLOR_COMMIT"));
4317 if (err)
4318 return err;
4321 view->show = show_blame_view;
4322 view->input = input_blame_view;
4323 view->close = close_blame_view;
4324 view->search_start = search_start_blame_view;
4325 view->search_next = search_next_blame_view;
4327 return run_blame(view);
4330 static const struct got_error *
4331 close_blame_view(struct tog_view *view)
4333 const struct got_error *err = NULL;
4334 struct tog_blame_view_state *s = &view->state.blame;
4336 if (s->blame.thread)
4337 err = stop_blame(&s->blame);
4339 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4340 struct got_object_qid *blamed_commit;
4341 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4342 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4343 got_object_qid_free(blamed_commit);
4346 free(s->path);
4347 free_colors(&s->colors);
4349 return err;
4352 static const struct got_error *
4353 search_start_blame_view(struct tog_view *view)
4355 struct tog_blame_view_state *s = &view->state.blame;
4357 s->matched_line = 0;
4358 return NULL;
4361 static const struct got_error *
4362 search_next_blame_view(struct tog_view *view)
4364 struct tog_blame_view_state *s = &view->state.blame;
4365 int lineno;
4367 if (!view->searching) {
4368 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4369 return NULL;
4372 if (s->matched_line) {
4373 if (view->searching == TOG_SEARCH_FORWARD)
4374 lineno = s->matched_line + 1;
4375 else
4376 lineno = s->matched_line - 1;
4377 } else {
4378 if (view->searching == TOG_SEARCH_FORWARD)
4379 lineno = 1;
4380 else
4381 lineno = s->blame.nlines;
4384 while (1) {
4385 char *line = NULL;
4386 off_t offset;
4387 size_t len;
4389 if (lineno <= 0 || lineno > s->blame.nlines) {
4390 if (s->matched_line == 0) {
4391 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4392 free(line);
4393 break;
4396 if (view->searching == TOG_SEARCH_FORWARD)
4397 lineno = 1;
4398 else
4399 lineno = s->blame.nlines;
4402 offset = s->blame.line_offsets[lineno - 1];
4403 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4404 free(line);
4405 return got_error_from_errno("fseeko");
4407 free(line);
4408 line = parse_next_line(s->blame.f, &len);
4409 if (line &&
4410 match_line(line, &view->regex, 1, &view->regmatch)) {
4411 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4412 s->matched_line = lineno;
4413 free(line);
4414 break;
4416 free(line);
4417 if (view->searching == TOG_SEARCH_FORWARD)
4418 lineno++;
4419 else
4420 lineno--;
4423 if (s->matched_line) {
4424 s->first_displayed_line = s->matched_line;
4425 s->selected_line = 1;
4428 return NULL;
4431 static const struct got_error *
4432 show_blame_view(struct tog_view *view)
4434 const struct got_error *err = NULL;
4435 struct tog_blame_view_state *s = &view->state.blame;
4436 int errcode;
4438 if (s->blame.thread == NULL) {
4439 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4440 &s->blame.thread_args);
4441 if (errcode)
4442 return got_error_set_errno(errcode, "pthread_create");
4444 halfdelay(1); /* fast refresh while annotating */
4447 if (s->blame_complete)
4448 halfdelay(10); /* disable fast refresh */
4450 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4451 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4452 s->selected_line, &s->first_displayed_line,
4453 &s->last_displayed_line, &s->eof, view->nlines, &s->colors,
4454 s->matched_line, &view->regmatch);
4456 view_vborder(view);
4457 return err;
4460 static const struct got_error *
4461 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4462 struct tog_view **focus_view, struct tog_view *view, int ch)
4464 const struct got_error *err = NULL, *thread_err = NULL;
4465 struct tog_view *diff_view;
4466 struct tog_blame_view_state *s = &view->state.blame;
4467 int begin_x = 0;
4469 switch (ch) {
4470 case 'q':
4471 s->done = 1;
4472 break;
4473 case 'k':
4474 case KEY_UP:
4475 if (s->selected_line > 1)
4476 s->selected_line--;
4477 else if (s->selected_line == 1 &&
4478 s->first_displayed_line > 1)
4479 s->first_displayed_line--;
4480 break;
4481 case KEY_PPAGE:
4482 case CTRL('b'):
4483 if (s->first_displayed_line == 1) {
4484 s->selected_line = 1;
4485 break;
4487 if (s->first_displayed_line > view->nlines - 2)
4488 s->first_displayed_line -=
4489 (view->nlines - 2);
4490 else
4491 s->first_displayed_line = 1;
4492 break;
4493 case 'j':
4494 case KEY_DOWN:
4495 if (s->selected_line < view->nlines - 2 &&
4496 s->first_displayed_line +
4497 s->selected_line <= s->blame.nlines)
4498 s->selected_line++;
4499 else if (s->last_displayed_line <
4500 s->blame.nlines)
4501 s->first_displayed_line++;
4502 break;
4503 case 'b':
4504 case 'p': {
4505 struct got_object_id *id = NULL;
4506 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4507 s->first_displayed_line, s->selected_line);
4508 if (id == NULL)
4509 break;
4510 if (ch == 'p') {
4511 struct got_commit_object *commit;
4512 struct got_object_qid *pid;
4513 struct got_object_id *blob_id = NULL;
4514 int obj_type;
4515 err = got_object_open_as_commit(&commit,
4516 s->repo, id);
4517 if (err)
4518 break;
4519 pid = SIMPLEQ_FIRST(
4520 got_object_commit_get_parent_ids(commit));
4521 if (pid == NULL) {
4522 got_object_commit_close(commit);
4523 break;
4525 /* Check if path history ends here. */
4526 err = got_object_id_by_path(&blob_id, s->repo,
4527 pid->id, s->path);
4528 if (err) {
4529 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4530 err = NULL;
4531 got_object_commit_close(commit);
4532 break;
4534 err = got_object_get_type(&obj_type, s->repo,
4535 blob_id);
4536 free(blob_id);
4537 /* Can't blame non-blob type objects. */
4538 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4539 got_object_commit_close(commit);
4540 break;
4542 err = got_object_qid_alloc(&s->blamed_commit,
4543 pid->id);
4544 got_object_commit_close(commit);
4545 } else {
4546 if (got_object_id_cmp(id,
4547 s->blamed_commit->id) == 0)
4548 break;
4549 err = got_object_qid_alloc(&s->blamed_commit,
4550 id);
4552 if (err)
4553 break;
4554 s->done = 1;
4555 thread_err = stop_blame(&s->blame);
4556 s->done = 0;
4557 if (thread_err)
4558 break;
4559 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4560 s->blamed_commit, entry);
4561 err = run_blame(view);
4562 if (err)
4563 break;
4564 break;
4566 case 'B': {
4567 struct got_object_qid *first;
4568 first = SIMPLEQ_FIRST(&s->blamed_commits);
4569 if (!got_object_id_cmp(first->id, s->commit_id))
4570 break;
4571 s->done = 1;
4572 thread_err = stop_blame(&s->blame);
4573 s->done = 0;
4574 if (thread_err)
4575 break;
4576 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4577 got_object_qid_free(s->blamed_commit);
4578 s->blamed_commit =
4579 SIMPLEQ_FIRST(&s->blamed_commits);
4580 err = run_blame(view);
4581 if (err)
4582 break;
4583 break;
4585 case KEY_ENTER:
4586 case '\r': {
4587 struct got_object_id *id = NULL;
4588 struct got_object_qid *pid;
4589 struct got_commit_object *commit = NULL;
4590 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4591 s->first_displayed_line, s->selected_line);
4592 if (id == NULL)
4593 break;
4594 err = got_object_open_as_commit(&commit, s->repo, id);
4595 if (err)
4596 break;
4597 pid = SIMPLEQ_FIRST(
4598 got_object_commit_get_parent_ids(commit));
4599 if (view_is_parent_view(view))
4600 begin_x = view_split_begin_x(view->begin_x);
4601 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4602 if (diff_view == NULL) {
4603 got_object_commit_close(commit);
4604 err = got_error_from_errno("view_open");
4605 break;
4607 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4608 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4609 got_object_commit_close(commit);
4610 if (err) {
4611 view_close(diff_view);
4612 break;
4614 if (view_is_parent_view(view)) {
4615 err = view_close_child(view);
4616 if (err)
4617 break;
4618 err = view_set_child(view, diff_view);
4619 if (err) {
4620 view_close(diff_view);
4621 break;
4623 *focus_view = diff_view;
4624 view->child_focussed = 1;
4625 } else
4626 *new_view = diff_view;
4627 if (err)
4628 break;
4629 break;
4631 case KEY_NPAGE:
4632 case CTRL('f'):
4633 case ' ':
4634 if (s->last_displayed_line >= s->blame.nlines &&
4635 s->selected_line >= MIN(s->blame.nlines,
4636 view->nlines - 2)) {
4637 break;
4639 if (s->last_displayed_line >= s->blame.nlines &&
4640 s->selected_line < view->nlines - 2) {
4641 s->selected_line = MIN(s->blame.nlines,
4642 view->nlines - 2);
4643 break;
4645 if (s->last_displayed_line + view->nlines - 2
4646 <= s->blame.nlines)
4647 s->first_displayed_line +=
4648 view->nlines - 2;
4649 else
4650 s->first_displayed_line =
4651 s->blame.nlines -
4652 (view->nlines - 3);
4653 break;
4654 case KEY_RESIZE:
4655 if (s->selected_line > view->nlines - 2) {
4656 s->selected_line = MIN(s->blame.nlines,
4657 view->nlines - 2);
4659 break;
4660 default:
4661 break;
4663 return thread_err ? thread_err : err;
4666 static const struct got_error *
4667 cmd_blame(int argc, char *argv[])
4669 const struct got_error *error;
4670 struct got_repository *repo = NULL;
4671 struct got_worktree *worktree = NULL;
4672 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4673 char *link_target = NULL;
4674 struct got_object_id *commit_id = NULL;
4675 char *commit_id_str = NULL;
4676 int ch;
4677 struct tog_view *view;
4679 #ifndef PROFILE
4680 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4681 NULL) == -1)
4682 err(1, "pledge");
4683 #endif
4685 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4686 switch (ch) {
4687 case 'c':
4688 commit_id_str = optarg;
4689 break;
4690 case 'r':
4691 repo_path = realpath(optarg, NULL);
4692 if (repo_path == NULL)
4693 return got_error_from_errno2("realpath",
4694 optarg);
4695 break;
4696 default:
4697 usage_blame();
4698 /* NOTREACHED */
4702 argc -= optind;
4703 argv += optind;
4705 if (argc != 1)
4706 usage_blame();
4708 cwd = getcwd(NULL, 0);
4709 if (cwd == NULL)
4710 return got_error_from_errno("getcwd");
4712 error = got_worktree_open(&worktree, cwd);
4713 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4714 goto done;
4716 if (repo_path == NULL) {
4717 if (worktree)
4718 repo_path =
4719 strdup(got_worktree_get_repo_path(worktree));
4720 else
4721 repo_path = strdup(cwd);
4723 if (repo_path == NULL) {
4724 error = got_error_from_errno("strdup");
4725 goto done;
4728 error = got_repo_open(&repo, repo_path, NULL);
4729 if (error != NULL)
4730 goto done;
4732 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4733 worktree);
4734 if (error)
4735 goto done;
4737 init_curses();
4739 error = apply_unveil(got_repo_get_path(repo), NULL);
4740 if (error)
4741 goto done;
4743 if (commit_id_str == NULL) {
4744 struct got_reference *head_ref;
4745 error = got_ref_open(&head_ref, repo, worktree ?
4746 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4747 if (error != NULL)
4748 goto done;
4749 error = got_ref_resolve(&commit_id, repo, head_ref);
4750 got_ref_close(head_ref);
4751 } else {
4752 error = got_repo_match_object_id(&commit_id, NULL,
4753 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4755 if (error != NULL)
4756 goto done;
4758 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4759 if (view == NULL) {
4760 error = got_error_from_errno("view_open");
4761 goto done;
4764 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4765 commit_id, repo);
4766 if (error)
4767 goto done;
4769 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4770 commit_id, repo);
4771 if (error)
4772 goto done;
4773 if (worktree) {
4774 /* Release work tree lock. */
4775 got_worktree_close(worktree);
4776 worktree = NULL;
4778 error = view_loop(view);
4779 done:
4780 free(repo_path);
4781 free(in_repo_path);
4782 free(link_target);
4783 free(cwd);
4784 free(commit_id);
4785 if (worktree)
4786 got_worktree_close(worktree);
4787 if (repo)
4788 got_repo_close(repo);
4789 return error;
4792 static const struct got_error *
4793 draw_tree_entries(struct tog_view *view,
4794 struct got_tree_entry **first_displayed_entry,
4795 struct got_tree_entry **last_displayed_entry,
4796 struct got_tree_entry **selected_entry, int *ndisplayed,
4797 const char *label, int show_ids, const char *parent_path,
4798 struct got_tree_object *tree, int selected, int limit,
4799 int isroot, struct tog_colors *colors, struct got_repository *repo)
4801 const struct got_error *err = NULL;
4802 struct got_tree_entry *te;
4803 wchar_t *wline;
4804 struct tog_color *tc;
4805 int width, n, i, nentries;
4807 *ndisplayed = 0;
4809 werase(view->window);
4811 if (limit == 0)
4812 return NULL;
4814 err = format_line(&wline, &width, label, view->ncols, 0);
4815 if (err)
4816 return err;
4817 if (view_needs_focus_indication(view))
4818 wstandout(view->window);
4819 tc = get_color(colors, TOG_COLOR_COMMIT);
4820 if (tc)
4821 wattr_on(view->window,
4822 COLOR_PAIR(tc->colorpair), NULL);
4823 waddwstr(view->window, wline);
4824 if (tc)
4825 wattr_off(view->window,
4826 COLOR_PAIR(tc->colorpair), NULL);
4827 if (view_needs_focus_indication(view))
4828 wstandend(view->window);
4829 free(wline);
4830 wline = NULL;
4831 if (width < view->ncols - 1)
4832 waddch(view->window, '\n');
4833 if (--limit <= 0)
4834 return NULL;
4835 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4836 if (err)
4837 return err;
4838 waddwstr(view->window, wline);
4839 free(wline);
4840 wline = NULL;
4841 if (width < view->ncols - 1)
4842 waddch(view->window, '\n');
4843 if (--limit <= 0)
4844 return NULL;
4845 waddch(view->window, '\n');
4846 if (--limit <= 0)
4847 return NULL;
4849 if (*first_displayed_entry == NULL) {
4850 te = got_object_tree_get_first_entry(tree);
4851 if (selected == 0) {
4852 if (view->focussed)
4853 wstandout(view->window);
4854 *selected_entry = NULL;
4856 waddstr(view->window, " ..\n"); /* parent directory */
4857 if (selected == 0 && view->focussed)
4858 wstandend(view->window);
4859 (*ndisplayed)++;
4860 if (--limit <= 0)
4861 return NULL;
4862 n = 1;
4863 } else {
4864 n = 0;
4865 te = *first_displayed_entry;
4868 nentries = got_object_tree_get_nentries(tree);
4869 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4870 char *line = NULL, *id_str = NULL, *link_target = NULL;
4871 const char *modestr = "";
4872 mode_t mode;
4874 te = got_object_tree_get_entry(tree, i);
4875 mode = got_tree_entry_get_mode(te);
4877 if (show_ids) {
4878 err = got_object_id_str(&id_str,
4879 got_tree_entry_get_id(te));
4880 if (err)
4881 return got_error_from_errno(
4882 "got_object_id_str");
4884 if (got_object_tree_entry_is_submodule(te))
4885 modestr = "$";
4886 else if (S_ISLNK(mode)) {
4887 int i;
4889 err = got_tree_entry_get_symlink_target(&link_target,
4890 te, repo);
4891 if (err) {
4892 free(id_str);
4893 return err;
4895 for (i = 0; i < strlen(link_target); i++) {
4896 if (!isprint((unsigned char)link_target[i]))
4897 link_target[i] = '?';
4899 modestr = "@";
4901 else if (S_ISDIR(mode))
4902 modestr = "/";
4903 else if (mode & S_IXUSR)
4904 modestr = "*";
4905 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4906 got_tree_entry_get_name(te), modestr,
4907 link_target ? " -> ": "",
4908 link_target ? link_target : "") == -1) {
4909 free(id_str);
4910 free(link_target);
4911 return got_error_from_errno("asprintf");
4913 free(id_str);
4914 free(link_target);
4915 err = format_line(&wline, &width, line, view->ncols, 0);
4916 if (err) {
4917 free(line);
4918 break;
4920 if (n == selected) {
4921 if (view->focussed)
4922 wstandout(view->window);
4923 *selected_entry = te;
4925 tc = match_color(colors, line);
4926 if (tc)
4927 wattr_on(view->window,
4928 COLOR_PAIR(tc->colorpair), NULL);
4929 waddwstr(view->window, wline);
4930 if (tc)
4931 wattr_off(view->window,
4932 COLOR_PAIR(tc->colorpair), NULL);
4933 if (width < view->ncols - 1)
4934 waddch(view->window, '\n');
4935 if (n == selected && view->focussed)
4936 wstandend(view->window);
4937 free(line);
4938 free(wline);
4939 wline = NULL;
4940 n++;
4941 (*ndisplayed)++;
4942 *last_displayed_entry = te;
4943 if (--limit <= 0)
4944 break;
4947 return err;
4950 static void
4951 tree_scroll_up(struct tog_view *view, int maxscroll)
4953 struct tog_tree_view_state *s = &view->state.tree;
4954 struct got_tree_entry *te;
4955 int isroot = s->tree == s->root;
4956 int i = 0;
4958 if (s->first_displayed_entry == NULL)
4959 return;
4961 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4962 while (i++ < maxscroll) {
4963 if (te == NULL) {
4964 if (!isroot)
4965 s->first_displayed_entry = NULL;
4966 break;
4968 s->first_displayed_entry = te;
4969 te = got_tree_entry_get_prev(s->tree, te);
4973 static void
4974 tree_scroll_down(struct tog_view *view, int maxscroll)
4976 struct tog_tree_view_state *s = &view->state.tree;
4977 struct got_tree_entry *next, *last;
4978 int n = 0;
4980 if (s->first_displayed_entry)
4981 next = got_tree_entry_get_next(s->tree,
4982 s->first_displayed_entry);
4983 else
4984 next = got_object_tree_get_first_entry(s->tree);
4986 last = s->last_displayed_entry;
4987 while (next && last && n++ < maxscroll) {
4988 last = got_tree_entry_get_next(s->tree, last);
4989 if (last) {
4990 s->first_displayed_entry = next;
4991 next = got_tree_entry_get_next(s->tree, next);
4996 static const struct got_error *
4997 tree_entry_path(char **path, struct tog_parent_trees *parents,
4998 struct got_tree_entry *te)
5000 const struct got_error *err = NULL;
5001 struct tog_parent_tree *pt;
5002 size_t len = 2; /* for leading slash and NUL */
5004 TAILQ_FOREACH(pt, parents, entry)
5005 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5006 + 1 /* slash */;
5007 if (te)
5008 len += strlen(got_tree_entry_get_name(te));
5010 *path = calloc(1, len);
5011 if (path == NULL)
5012 return got_error_from_errno("calloc");
5014 (*path)[0] = '/';
5015 pt = TAILQ_LAST(parents, tog_parent_trees);
5016 while (pt) {
5017 const char *name = got_tree_entry_get_name(pt->selected_entry);
5018 if (strlcat(*path, name, len) >= len) {
5019 err = got_error(GOT_ERR_NO_SPACE);
5020 goto done;
5022 if (strlcat(*path, "/", len) >= len) {
5023 err = got_error(GOT_ERR_NO_SPACE);
5024 goto done;
5026 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5028 if (te) {
5029 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5030 err = got_error(GOT_ERR_NO_SPACE);
5031 goto done;
5034 done:
5035 if (err) {
5036 free(*path);
5037 *path = NULL;
5039 return err;
5042 static const struct got_error *
5043 blame_tree_entry(struct tog_view **new_view, int begin_x,
5044 struct got_tree_entry *te, struct tog_parent_trees *parents,
5045 struct got_object_id *commit_id, struct got_repository *repo)
5047 const struct got_error *err = NULL;
5048 char *path;
5049 struct tog_view *blame_view;
5051 *new_view = NULL;
5053 err = tree_entry_path(&path, parents, te);
5054 if (err)
5055 return err;
5057 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5058 if (blame_view == NULL) {
5059 err = got_error_from_errno("view_open");
5060 goto done;
5063 err = open_blame_view(blame_view, path, commit_id, repo);
5064 if (err) {
5065 if (err->code == GOT_ERR_CANCELLED)
5066 err = NULL;
5067 view_close(blame_view);
5068 } else
5069 *new_view = blame_view;
5070 done:
5071 free(path);
5072 return err;
5075 static const struct got_error *
5076 log_tree_entry(struct tog_view **new_view, int begin_x,
5077 struct got_tree_entry *te, struct tog_parent_trees *parents,
5078 struct got_object_id *commit_id, struct got_repository *repo)
5080 struct tog_view *log_view;
5081 const struct got_error *err = NULL;
5082 char *path;
5084 *new_view = NULL;
5086 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5087 if (log_view == NULL)
5088 return got_error_from_errno("view_open");
5090 err = tree_entry_path(&path, parents, te);
5091 if (err)
5092 return err;
5094 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5095 if (err)
5096 view_close(log_view);
5097 else
5098 *new_view = log_view;
5099 free(path);
5100 return err;
5103 static const struct got_error *
5104 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5105 struct got_object_id *commit_id, struct got_repository *repo)
5107 const struct got_error *err = NULL;
5108 char *commit_id_str = NULL;
5109 struct tog_tree_view_state *s = &view->state.tree;
5111 TAILQ_INIT(&s->parents);
5113 err = got_object_id_str(&commit_id_str, commit_id);
5114 if (err != NULL)
5115 goto done;
5117 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5118 err = got_error_from_errno("asprintf");
5119 goto done;
5122 s->root = s->tree = root;
5123 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5124 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5125 s->commit_id = got_object_id_dup(commit_id);
5126 if (s->commit_id == NULL) {
5127 err = got_error_from_errno("got_object_id_dup");
5128 goto done;
5130 s->repo = repo;
5132 SIMPLEQ_INIT(&s->colors);
5134 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5135 err = add_color(&s->colors, "\\$$",
5136 TOG_COLOR_TREE_SUBMODULE,
5137 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5138 if (err)
5139 goto done;
5140 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5141 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5142 if (err) {
5143 free_colors(&s->colors);
5144 goto done;
5146 err = add_color(&s->colors, "/$",
5147 TOG_COLOR_TREE_DIRECTORY,
5148 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5149 if (err) {
5150 free_colors(&s->colors);
5151 goto done;
5154 err = add_color(&s->colors, "\\*$",
5155 TOG_COLOR_TREE_EXECUTABLE,
5156 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5157 if (err) {
5158 free_colors(&s->colors);
5159 goto done;
5162 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5163 get_color_value("TOG_COLOR_COMMIT"));
5164 if (err) {
5165 free_colors(&s->colors);
5166 goto done;
5170 view->show = show_tree_view;
5171 view->input = input_tree_view;
5172 view->close = close_tree_view;
5173 view->search_start = search_start_tree_view;
5174 view->search_next = search_next_tree_view;
5175 done:
5176 free(commit_id_str);
5177 if (err) {
5178 free(s->tree_label);
5179 s->tree_label = NULL;
5181 return err;
5184 static const struct got_error *
5185 close_tree_view(struct tog_view *view)
5187 struct tog_tree_view_state *s = &view->state.tree;
5189 free_colors(&s->colors);
5190 free(s->tree_label);
5191 s->tree_label = NULL;
5192 free(s->commit_id);
5193 s->commit_id = NULL;
5194 while (!TAILQ_EMPTY(&s->parents)) {
5195 struct tog_parent_tree *parent;
5196 parent = TAILQ_FIRST(&s->parents);
5197 TAILQ_REMOVE(&s->parents, parent, entry);
5198 free(parent);
5201 if (s->tree != s->root)
5202 got_object_tree_close(s->tree);
5203 got_object_tree_close(s->root);
5204 return NULL;
5207 static const struct got_error *
5208 search_start_tree_view(struct tog_view *view)
5210 struct tog_tree_view_state *s = &view->state.tree;
5212 s->matched_entry = NULL;
5213 return NULL;
5216 static int
5217 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5219 regmatch_t regmatch;
5221 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5222 0) == 0;
5225 static const struct got_error *
5226 search_next_tree_view(struct tog_view *view)
5228 struct tog_tree_view_state *s = &view->state.tree;
5229 struct got_tree_entry *te = NULL;
5231 if (!view->searching) {
5232 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5233 return NULL;
5236 if (s->matched_entry) {
5237 if (view->searching == TOG_SEARCH_FORWARD) {
5238 if (s->selected_entry)
5239 te = got_tree_entry_get_next(s->tree,
5240 s->selected_entry);
5241 else
5242 te = got_object_tree_get_first_entry(s->tree);
5243 } else {
5244 if (s->selected_entry == NULL)
5245 te = got_object_tree_get_last_entry(s->tree);
5246 else
5247 te = got_tree_entry_get_prev(s->tree,
5248 s->selected_entry);
5250 } else {
5251 if (view->searching == TOG_SEARCH_FORWARD)
5252 te = got_object_tree_get_first_entry(s->tree);
5253 else
5254 te = got_object_tree_get_last_entry(s->tree);
5257 while (1) {
5258 if (te == NULL) {
5259 if (s->matched_entry == NULL) {
5260 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5261 return NULL;
5263 if (view->searching == TOG_SEARCH_FORWARD)
5264 te = got_object_tree_get_first_entry(s->tree);
5265 else
5266 te = got_object_tree_get_last_entry(s->tree);
5269 if (match_tree_entry(te, &view->regex)) {
5270 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5271 s->matched_entry = te;
5272 break;
5275 if (view->searching == TOG_SEARCH_FORWARD)
5276 te = got_tree_entry_get_next(s->tree, te);
5277 else
5278 te = got_tree_entry_get_prev(s->tree, te);
5281 if (s->matched_entry) {
5282 s->first_displayed_entry = s->matched_entry;
5283 s->selected = 0;
5286 return NULL;
5289 static const struct got_error *
5290 show_tree_view(struct tog_view *view)
5292 const struct got_error *err = NULL;
5293 struct tog_tree_view_state *s = &view->state.tree;
5294 char *parent_path;
5296 err = tree_entry_path(&parent_path, &s->parents, NULL);
5297 if (err)
5298 return err;
5300 err = draw_tree_entries(view, &s->first_displayed_entry,
5301 &s->last_displayed_entry, &s->selected_entry,
5302 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5303 s->tree, s->selected, view->nlines, s->tree == s->root,
5304 &s->colors, s->repo);
5305 free(parent_path);
5307 view_vborder(view);
5308 return err;
5311 static const struct got_error *
5312 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5313 struct tog_view **focus_view, struct tog_view *view, int ch)
5315 const struct got_error *err = NULL;
5316 struct tog_tree_view_state *s = &view->state.tree;
5317 struct tog_view *log_view, *ref_view;
5318 int begin_x = 0;
5320 switch (ch) {
5321 case 'i':
5322 s->show_ids = !s->show_ids;
5323 break;
5324 case 'l':
5325 if (!s->selected_entry)
5326 break;
5327 if (view_is_parent_view(view))
5328 begin_x = view_split_begin_x(view->begin_x);
5329 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5330 &s->parents, s->commit_id, s->repo);
5331 if (view_is_parent_view(view)) {
5332 err = view_close_child(view);
5333 if (err)
5334 return err;
5335 err = view_set_child(view, log_view);
5336 if (err) {
5337 view_close(log_view);
5338 break;
5340 *focus_view = log_view;
5341 view->child_focussed = 1;
5342 } else
5343 *new_view = log_view;
5344 break;
5345 case 'r':
5346 if (view_is_parent_view(view))
5347 begin_x = view_split_begin_x(view->begin_x);
5348 ref_view = view_open(view->nlines, view->ncols,
5349 view->begin_y, begin_x, TOG_VIEW_REF);
5350 if (ref_view == NULL)
5351 return got_error_from_errno("view_open");
5352 err = open_ref_view(ref_view, s->repo);
5353 if (err) {
5354 view_close(ref_view);
5355 return err;
5357 if (view_is_parent_view(view)) {
5358 err = view_close_child(view);
5359 if (err)
5360 return err;
5361 err = view_set_child(view, ref_view);
5362 if (err) {
5363 view_close(ref_view);
5364 break;
5366 *focus_view = ref_view;
5367 view->child_focussed = 1;
5368 } else
5369 *new_view = ref_view;
5370 break;
5371 case 'k':
5372 case KEY_UP:
5373 if (s->selected > 0) {
5374 s->selected--;
5375 break;
5377 tree_scroll_up(view, 1);
5378 break;
5379 case KEY_PPAGE:
5380 case CTRL('b'):
5381 if (s->tree == s->root) {
5382 if (got_object_tree_get_first_entry(s->tree) ==
5383 s->first_displayed_entry)
5384 s->selected = 0;
5385 } else {
5386 if (s->first_displayed_entry == NULL)
5387 s->selected = 0;
5389 tree_scroll_up(view, MAX(0, view->nlines - 3));
5390 break;
5391 case 'j':
5392 case KEY_DOWN:
5393 if (s->selected < s->ndisplayed - 1) {
5394 s->selected++;
5395 break;
5397 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5398 == NULL)
5399 /* can't scroll any further */
5400 break;
5401 tree_scroll_down(view, 1);
5402 break;
5403 case KEY_NPAGE:
5404 case CTRL('f'):
5405 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5406 == NULL) {
5407 /* can't scroll any further; move cursor down */
5408 if (s->selected < s->ndisplayed - 1)
5409 s->selected = s->ndisplayed - 1;
5410 break;
5412 tree_scroll_down(view, view->nlines - 3);
5413 break;
5414 case KEY_ENTER:
5415 case '\r':
5416 case KEY_BACKSPACE:
5417 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5418 struct tog_parent_tree *parent;
5419 /* user selected '..' */
5420 if (s->tree == s->root)
5421 break;
5422 parent = TAILQ_FIRST(&s->parents);
5423 TAILQ_REMOVE(&s->parents, parent,
5424 entry);
5425 got_object_tree_close(s->tree);
5426 s->tree = parent->tree;
5427 s->first_displayed_entry =
5428 parent->first_displayed_entry;
5429 s->selected_entry =
5430 parent->selected_entry;
5431 s->selected = parent->selected;
5432 free(parent);
5433 } else if (S_ISDIR(got_tree_entry_get_mode(
5434 s->selected_entry))) {
5435 struct got_tree_object *subtree;
5436 err = got_object_open_as_tree(&subtree, s->repo,
5437 got_tree_entry_get_id(s->selected_entry));
5438 if (err)
5439 break;
5440 err = tree_view_visit_subtree(subtree, s);
5441 if (err) {
5442 got_object_tree_close(subtree);
5443 break;
5445 } else if (S_ISREG(got_tree_entry_get_mode(
5446 s->selected_entry))) {
5447 struct tog_view *blame_view;
5448 int begin_x = view_is_parent_view(view) ?
5449 view_split_begin_x(view->begin_x) : 0;
5451 err = blame_tree_entry(&blame_view, begin_x,
5452 s->selected_entry, &s->parents,
5453 s->commit_id, s->repo);
5454 if (err)
5455 break;
5456 if (view_is_parent_view(view)) {
5457 err = view_close_child(view);
5458 if (err)
5459 return err;
5460 err = view_set_child(view, blame_view);
5461 if (err) {
5462 view_close(blame_view);
5463 break;
5465 *focus_view = blame_view;
5466 view->child_focussed = 1;
5467 } else
5468 *new_view = blame_view;
5470 break;
5471 case KEY_RESIZE:
5472 if (s->selected > view->nlines)
5473 s->selected = s->ndisplayed - 1;
5474 break;
5475 default:
5476 break;
5479 return err;
5482 __dead static void
5483 usage_tree(void)
5485 endwin();
5486 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5487 getprogname());
5488 exit(1);
5491 static const struct got_error *
5492 cmd_tree(int argc, char *argv[])
5494 const struct got_error *error;
5495 struct got_repository *repo = NULL;
5496 struct got_worktree *worktree = NULL;
5497 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5498 struct got_object_id *commit_id = NULL;
5499 char *commit_id_arg = NULL;
5500 struct got_commit_object *commit = NULL;
5501 struct got_tree_object *tree = NULL;
5502 int ch;
5503 struct tog_view *view;
5505 #ifndef PROFILE
5506 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5507 NULL) == -1)
5508 err(1, "pledge");
5509 #endif
5511 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5512 switch (ch) {
5513 case 'c':
5514 commit_id_arg = optarg;
5515 break;
5516 case 'r':
5517 repo_path = realpath(optarg, NULL);
5518 if (repo_path == NULL)
5519 return got_error_from_errno2("realpath",
5520 optarg);
5521 break;
5522 default:
5523 usage_tree();
5524 /* NOTREACHED */
5528 argc -= optind;
5529 argv += optind;
5531 if (argc > 1)
5532 usage_tree();
5534 cwd = getcwd(NULL, 0);
5535 if (cwd == NULL)
5536 return got_error_from_errno("getcwd");
5538 error = got_worktree_open(&worktree, cwd);
5539 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5540 goto done;
5542 if (repo_path == NULL) {
5543 if (worktree)
5544 repo_path =
5545 strdup(got_worktree_get_repo_path(worktree));
5546 else
5547 repo_path = strdup(cwd);
5549 if (repo_path == NULL) {
5550 error = got_error_from_errno("strdup");
5551 goto done;
5554 error = got_repo_open(&repo, repo_path, NULL);
5555 if (error != NULL)
5556 goto done;
5558 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5559 repo, worktree);
5560 if (error)
5561 goto done;
5563 init_curses();
5565 error = apply_unveil(got_repo_get_path(repo), NULL);
5566 if (error)
5567 goto done;
5569 error = got_repo_match_object_id(&commit_id, NULL,
5570 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5571 GOT_OBJ_TYPE_COMMIT, 1, repo);
5572 if (error)
5573 goto done;
5575 error = got_object_open_as_commit(&commit, repo, commit_id);
5576 if (error)
5577 goto done;
5579 error = got_object_open_as_tree(&tree, repo,
5580 got_object_commit_get_tree_id(commit));
5581 if (error)
5582 goto done;
5584 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5585 if (view == NULL) {
5586 error = got_error_from_errno("view_open");
5587 goto done;
5589 error = open_tree_view(view, tree, commit_id, repo);
5590 if (error)
5591 goto done;
5592 if (!got_path_is_root_dir(in_repo_path)) {
5593 error = tree_view_walk_path(&view->state.tree, commit_id,
5594 in_repo_path, repo);
5595 if (error)
5596 goto done;
5599 if (worktree) {
5600 /* Release work tree lock. */
5601 got_worktree_close(worktree);
5602 worktree = NULL;
5604 error = view_loop(view);
5605 done:
5606 free(repo_path);
5607 free(cwd);
5608 free(commit_id);
5609 if (commit)
5610 got_object_commit_close(commit);
5611 if (tree)
5612 got_object_tree_close(tree);
5613 if (repo)
5614 got_repo_close(repo);
5615 return error;
5618 static const struct got_error *
5619 ref_view_load_refs(struct tog_ref_view_state *s)
5621 const struct got_error *err;
5622 struct got_reflist_entry *sre;
5623 struct tog_reflist_entry *re;
5625 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5626 got_ref_cmp_by_name, NULL);
5627 if (err)
5628 return err;
5630 s->nrefs = 0;
5631 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5632 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5633 continue;
5635 re = malloc(sizeof(*re));
5636 if (re == NULL)
5637 return got_error_from_errno("malloc");
5639 re->ref = sre->ref;
5640 re->idx = s->nrefs++;
5641 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5644 return NULL;
5647 void
5648 ref_view_free_refs(struct tog_ref_view_state *s)
5650 struct tog_reflist_entry *re;
5652 while (!TAILQ_EMPTY(&s->refs)) {
5653 re = TAILQ_FIRST(&s->refs);
5654 TAILQ_REMOVE(&s->refs, re, entry);
5655 free(re);
5657 got_ref_list_free(&s->simplerefs);
5660 static const struct got_error *
5661 open_ref_view(struct tog_view *view, struct got_repository *repo)
5663 const struct got_error *err = NULL;
5664 struct tog_ref_view_state *s = &view->state.ref;
5666 s->selected_entry = 0;
5667 s->repo = repo;
5669 SIMPLEQ_INIT(&s->simplerefs);
5670 TAILQ_INIT(&s->refs);
5671 SIMPLEQ_INIT(&s->colors);
5673 err = ref_view_load_refs(s);
5674 if (err)
5675 return err;
5677 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5679 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5680 err = add_color(&s->colors, "^refs/heads/",
5681 TOG_COLOR_REFS_HEADS,
5682 get_color_value("TOG_COLOR_REFS_HEADS"));
5683 if (err)
5684 goto done;
5686 err = add_color(&s->colors, "^refs/tags/",
5687 TOG_COLOR_REFS_TAGS,
5688 get_color_value("TOG_COLOR_REFS_TAGS"));
5689 if (err)
5690 goto done;
5692 err = add_color(&s->colors, "^refs/remotes/",
5693 TOG_COLOR_REFS_REMOTES,
5694 get_color_value("TOG_COLOR_REFS_REMOTES"));
5695 if (err)
5696 goto done;
5699 view->show = show_ref_view;
5700 view->input = input_ref_view;
5701 view->close = close_ref_view;
5702 view->search_start = search_start_ref_view;
5703 view->search_next = search_next_ref_view;
5704 done:
5705 if (err)
5706 free_colors(&s->colors);
5707 return err;
5710 static const struct got_error *
5711 close_ref_view(struct tog_view *view)
5713 struct tog_ref_view_state *s = &view->state.ref;
5715 ref_view_free_refs(s);
5716 free_colors(&s->colors);
5718 return NULL;
5721 static const struct got_error *
5722 resolve_reflist_entry(struct got_object_id **commit_id,
5723 struct tog_reflist_entry *re, struct got_repository *repo)
5725 const struct got_error *err = NULL;
5726 struct got_object_id *obj_id;
5727 struct got_tag_object *tag = NULL;
5728 int obj_type;
5730 *commit_id = NULL;
5732 err = got_ref_resolve(&obj_id, repo, re->ref);
5733 if (err)
5734 return err;
5736 err = got_object_get_type(&obj_type, repo, obj_id);
5737 if (err)
5738 goto done;
5740 switch (obj_type) {
5741 case GOT_OBJ_TYPE_COMMIT:
5742 *commit_id = obj_id;
5743 break;
5744 case GOT_OBJ_TYPE_TAG:
5745 err = got_object_open_as_tag(&tag, repo, obj_id);
5746 if (err)
5747 goto done;
5748 free(obj_id);
5749 err = got_object_get_type(&obj_type, repo,
5750 got_object_tag_get_object_id(tag));
5751 if (err)
5752 goto done;
5753 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5754 err = got_error(GOT_ERR_OBJ_TYPE);
5755 goto done;
5757 *commit_id = got_object_id_dup(
5758 got_object_tag_get_object_id(tag));
5759 if (*commit_id == NULL) {
5760 err = got_error_from_errno("got_object_id_dup");
5761 goto done;
5763 break;
5764 default:
5765 err = got_error(GOT_ERR_OBJ_TYPE);
5766 break;
5769 done:
5770 if (tag)
5771 got_object_tag_close(tag);
5772 if (err) {
5773 free(*commit_id);
5774 *commit_id = NULL;
5776 return err;
5779 static const struct got_error *
5780 log_ref_entry(struct tog_view **new_view, int begin_x,
5781 struct tog_reflist_entry *re, struct got_repository *repo)
5783 struct tog_view *log_view;
5784 const struct got_error *err = NULL;
5785 struct got_object_id *commit_id = NULL;
5787 *new_view = NULL;
5789 err = resolve_reflist_entry(&commit_id, re, repo);
5790 if (err) {
5791 if (err->code != GOT_ERR_OBJ_TYPE)
5792 return err;
5793 else
5794 return NULL;
5797 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5798 if (log_view == NULL) {
5799 err = got_error_from_errno("view_open");
5800 goto done;
5803 err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5804 done:
5805 if (err)
5806 view_close(log_view);
5807 else
5808 *new_view = log_view;
5809 free(commit_id);
5810 return err;
5813 static void
5814 ref_scroll_up(struct tog_view *view, int maxscroll)
5816 struct tog_ref_view_state *s = &view->state.ref;
5817 struct tog_reflist_entry *re;
5818 int i = 0;
5820 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5821 return;
5823 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5824 while (i++ < maxscroll) {
5825 if (re == NULL)
5826 break;
5827 s->first_displayed_entry = re;
5828 re = TAILQ_PREV(re, tog_reflist_head, entry);
5832 static void
5833 ref_scroll_down(struct tog_view *view, int maxscroll)
5835 struct tog_ref_view_state *s = &view->state.ref;
5836 struct tog_reflist_entry *next, *last;
5837 int n = 0;
5839 if (s->first_displayed_entry)
5840 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5841 else
5842 next = TAILQ_FIRST(&s->refs);
5844 last = s->last_displayed_entry;
5845 while (next && last && n++ < maxscroll) {
5846 last = TAILQ_NEXT(last, entry);
5847 if (last) {
5848 s->first_displayed_entry = next;
5849 next = TAILQ_NEXT(next, entry);
5854 static const struct got_error *
5855 search_start_ref_view(struct tog_view *view)
5857 struct tog_ref_view_state *s = &view->state.ref;
5859 s->matched_entry = NULL;
5860 return NULL;
5863 static int
5864 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5866 regmatch_t regmatch;
5868 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5869 0) == 0;
5872 static const struct got_error *
5873 search_next_ref_view(struct tog_view *view)
5875 struct tog_ref_view_state *s = &view->state.ref;
5876 struct tog_reflist_entry *re = NULL;
5878 if (!view->searching) {
5879 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5880 return NULL;
5883 if (s->matched_entry) {
5884 if (view->searching == TOG_SEARCH_FORWARD) {
5885 if (s->selected_entry)
5886 re = TAILQ_NEXT(s->selected_entry, entry);
5887 else
5888 re = TAILQ_PREV(s->selected_entry,
5889 tog_reflist_head, entry);
5890 } else {
5891 if (s->selected_entry == NULL)
5892 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5893 else
5894 re = TAILQ_PREV(s->selected_entry,
5895 tog_reflist_head, entry);
5897 } else {
5898 if (view->searching == TOG_SEARCH_FORWARD)
5899 re = TAILQ_FIRST(&s->refs);
5900 else
5901 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5904 while (1) {
5905 if (re == NULL) {
5906 if (s->matched_entry == NULL) {
5907 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5908 return NULL;
5910 if (view->searching == TOG_SEARCH_FORWARD)
5911 re = TAILQ_FIRST(&s->refs);
5912 else
5913 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5916 if (match_reflist_entry(re, &view->regex)) {
5917 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5918 s->matched_entry = re;
5919 break;
5922 if (view->searching == TOG_SEARCH_FORWARD)
5923 re = TAILQ_NEXT(re, entry);
5924 else
5925 re = TAILQ_PREV(re, tog_reflist_head, entry);
5928 if (s->matched_entry) {
5929 s->first_displayed_entry = s->matched_entry;
5930 s->selected = 0;
5933 return NULL;
5936 static const struct got_error *
5937 show_ref_view(struct tog_view *view)
5939 const struct got_error *err = NULL;
5940 struct tog_ref_view_state *s = &view->state.ref;
5941 struct tog_reflist_entry *re;
5942 char *line = NULL;
5943 wchar_t *wline;
5944 struct tog_color *tc;
5945 int width, n;
5946 int limit = view->nlines;
5948 werase(view->window);
5950 s->ndisplayed = 0;
5952 if (limit == 0)
5953 return NULL;
5955 re = s->first_displayed_entry;
5957 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5958 s->nrefs) == -1)
5959 return got_error_from_errno("asprintf");
5961 err = format_line(&wline, &width, line, view->ncols, 0);
5962 if (err) {
5963 free(line);
5964 return err;
5966 if (view_needs_focus_indication(view))
5967 wstandout(view->window);
5968 waddwstr(view->window, wline);
5969 if (view_needs_focus_indication(view))
5970 wstandend(view->window);
5971 free(wline);
5972 wline = NULL;
5973 free(line);
5974 line = NULL;
5975 if (width < view->ncols - 1)
5976 waddch(view->window, '\n');
5977 if (--limit <= 0)
5978 return NULL;
5980 n = 0;
5981 while (re && limit > 0) {
5982 char *line = NULL;
5984 if (got_ref_is_symbolic(re->ref)) {
5985 if (asprintf(&line, "%s -> %s",
5986 got_ref_get_name(re->ref),
5987 got_ref_get_symref_target(re->ref)) == -1)
5988 return got_error_from_errno("asprintf");
5989 } else if (s->show_ids) {
5990 struct got_object_id *id;
5991 char *id_str;
5992 err = got_ref_resolve(&id, s->repo, re->ref);
5993 if (err)
5994 return err;
5995 err = got_object_id_str(&id_str, id);
5996 if (err) {
5997 free(id);
5998 return err;
6000 if (asprintf(&line, "%s: %s",
6001 got_ref_get_name(re->ref), id_str) == -1) {
6002 err = got_error_from_errno("asprintf");
6003 free(id);
6004 free(id_str);
6005 return err;
6007 free(id);
6008 free(id_str);
6009 } else {
6010 line = strdup(got_ref_get_name(re->ref));
6011 if (line == NULL)
6012 return got_error_from_errno("strdup");
6015 err = format_line(&wline, &width, line, view->ncols, 0);
6016 if (err) {
6017 free(line);
6018 return err;
6020 if (n == s->selected) {
6021 if (view->focussed)
6022 wstandout(view->window);
6023 s->selected_entry = re;
6025 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6026 if (tc)
6027 wattr_on(view->window,
6028 COLOR_PAIR(tc->colorpair), NULL);
6029 waddwstr(view->window, wline);
6030 if (tc)
6031 wattr_off(view->window,
6032 COLOR_PAIR(tc->colorpair), NULL);
6033 if (width < view->ncols - 1)
6034 waddch(view->window, '\n');
6035 if (n == s->selected && view->focussed)
6036 wstandend(view->window);
6037 free(line);
6038 free(wline);
6039 wline = NULL;
6040 n++;
6041 s->ndisplayed++;
6042 s->last_displayed_entry = re;
6044 limit--;
6045 re = TAILQ_NEXT(re, entry);
6048 view_vborder(view);
6049 return err;
6052 static const struct got_error *
6053 browse_ref_tree(struct tog_view **new_view, int begin_x,
6054 struct tog_reflist_entry *re, struct got_repository *repo)
6056 const struct got_error *err = NULL;
6057 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6058 struct got_tree_object *tree = NULL;
6059 struct tog_view *tree_view;
6061 *new_view = NULL;
6063 err = resolve_reflist_entry(&commit_id, re, repo);
6064 if (err) {
6065 if (err->code != GOT_ERR_OBJ_TYPE)
6066 return err;
6067 else
6068 return NULL;
6071 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6072 if (err)
6073 goto done;
6075 err = got_object_open_as_tree(&tree, repo, tree_id);
6076 if (err)
6077 goto done;
6079 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6080 if (tree_view == NULL) {
6081 err = got_error_from_errno("view_open");
6082 goto done;
6085 err = open_tree_view(tree_view, tree, commit_id, repo);
6086 if (err)
6087 goto done;
6089 *new_view = tree_view;
6090 done:
6091 free(commit_id);
6092 free(tree_id);
6093 if (err) {
6094 if (tree)
6095 got_object_tree_close(tree);
6097 return err;
6099 static const struct got_error *
6100 input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6101 struct tog_view **focus_view, struct tog_view *view, int ch)
6103 const struct got_error *err = NULL;
6104 struct tog_ref_view_state *s = &view->state.ref;
6105 struct tog_view *log_view, *tree_view;
6106 int begin_x = 0;
6108 switch (ch) {
6109 case 'i':
6110 s->show_ids = !s->show_ids;
6111 break;
6112 case KEY_ENTER:
6113 case '\r':
6114 if (!s->selected_entry)
6115 break;
6116 if (view_is_parent_view(view))
6117 begin_x = view_split_begin_x(view->begin_x);
6118 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6119 s->repo);
6120 if (view_is_parent_view(view)) {
6121 err = view_close_child(view);
6122 if (err)
6123 return err;
6124 err = view_set_child(view, log_view);
6125 if (err) {
6126 view_close(log_view);
6127 break;
6129 *focus_view = log_view;
6130 view->child_focussed = 1;
6131 } else
6132 *new_view = log_view;
6133 break;
6134 case 't':
6135 if (!s->selected_entry)
6136 break;
6137 if (view_is_parent_view(view))
6138 begin_x = view_split_begin_x(view->begin_x);
6139 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6140 s->repo);
6141 if (err || tree_view == NULL)
6142 break;
6143 if (view_is_parent_view(view)) {
6144 err = view_close_child(view);
6145 if (err)
6146 return err;
6147 err = view_set_child(view, tree_view);
6148 if (err) {
6149 view_close(tree_view);
6150 break;
6152 *focus_view = tree_view;
6153 view->child_focussed = 1;
6154 } else
6155 *new_view = tree_view;
6156 break;
6157 case 'k':
6158 case KEY_UP:
6159 if (s->selected > 0) {
6160 s->selected--;
6161 break;
6163 ref_scroll_up(view, 1);
6164 break;
6165 case KEY_PPAGE:
6166 case CTRL('b'):
6167 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6168 s->selected = 0;
6169 ref_scroll_up(view, MAX(0, view->nlines - 1));
6170 break;
6171 case 'j':
6172 case KEY_DOWN:
6173 if (s->selected < s->ndisplayed - 1) {
6174 s->selected++;
6175 break;
6177 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6178 /* can't scroll any further */
6179 break;
6180 ref_scroll_down(view, 1);
6181 break;
6182 case KEY_NPAGE:
6183 case CTRL('f'):
6184 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6185 /* can't scroll any further; move cursor down */
6186 if (s->selected < s->ndisplayed - 1)
6187 s->selected = s->ndisplayed - 1;
6188 break;
6190 ref_scroll_down(view, view->nlines - 1);
6191 break;
6192 case CTRL('l'):
6193 ref_view_free_refs(s);
6194 err = ref_view_load_refs(s);
6195 break;
6196 case KEY_RESIZE:
6197 if (s->selected > view->nlines)
6198 s->selected = s->ndisplayed - 1;
6199 break;
6200 default:
6201 break;
6204 return err;
6207 __dead static void
6208 usage_ref(void)
6210 endwin();
6211 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6212 getprogname());
6213 exit(1);
6216 static const struct got_error *
6217 cmd_ref(int argc, char *argv[])
6219 const struct got_error *error;
6220 struct got_repository *repo = NULL;
6221 struct got_worktree *worktree = NULL;
6222 char *cwd = NULL, *repo_path = NULL;
6223 int ch;
6224 struct tog_view *view;
6226 #ifndef PROFILE
6227 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6228 NULL) == -1)
6229 err(1, "pledge");
6230 #endif
6232 while ((ch = getopt(argc, argv, "r:")) != -1) {
6233 switch (ch) {
6234 case 'r':
6235 repo_path = realpath(optarg, NULL);
6236 if (repo_path == NULL)
6237 return got_error_from_errno2("realpath",
6238 optarg);
6239 break;
6240 default:
6241 usage_ref();
6242 /* NOTREACHED */
6246 argc -= optind;
6247 argv += optind;
6249 if (argc > 1)
6250 usage_ref();
6252 cwd = getcwd(NULL, 0);
6253 if (cwd == NULL)
6254 return got_error_from_errno("getcwd");
6256 error = got_worktree_open(&worktree, cwd);
6257 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6258 goto done;
6260 if (repo_path == NULL) {
6261 if (worktree)
6262 repo_path =
6263 strdup(got_worktree_get_repo_path(worktree));
6264 else
6265 repo_path = strdup(cwd);
6267 if (repo_path == NULL) {
6268 error = got_error_from_errno("strdup");
6269 goto done;
6272 error = got_repo_open(&repo, repo_path, NULL);
6273 if (error != NULL)
6274 goto done;
6276 init_curses();
6278 error = apply_unveil(got_repo_get_path(repo), NULL);
6279 if (error)
6280 goto done;
6282 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6283 if (view == NULL) {
6284 error = got_error_from_errno("view_open");
6285 goto done;
6288 error = open_ref_view(view, repo);
6289 if (error)
6290 goto done;
6292 if (worktree) {
6293 /* Release work tree lock. */
6294 got_worktree_close(worktree);
6295 worktree = NULL;
6297 error = view_loop(view);
6298 done:
6299 free(repo_path);
6300 free(cwd);
6301 if (repo)
6302 got_repo_close(repo);
6303 return error;
6306 static void
6307 list_commands(FILE *fp)
6309 int i;
6311 fprintf(fp, "commands:");
6312 for (i = 0; i < nitems(tog_commands); i++) {
6313 struct tog_cmd *cmd = &tog_commands[i];
6314 fprintf(fp, " %s", cmd->name);
6316 fputc('\n', fp);
6319 __dead static void
6320 usage(int hflag, int status)
6322 FILE *fp = (status == 0) ? stdout : stderr;
6324 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6325 getprogname());
6326 if (hflag) {
6327 fprintf(fp, "lazy usage: %s path\n", getprogname());
6328 list_commands(fp);
6330 exit(status);
6333 static char **
6334 make_argv(int argc, ...)
6336 va_list ap;
6337 char **argv;
6338 int i;
6340 va_start(ap, argc);
6342 argv = calloc(argc, sizeof(char *));
6343 if (argv == NULL)
6344 err(1, "calloc");
6345 for (i = 0; i < argc; i++) {
6346 argv[i] = strdup(va_arg(ap, char *));
6347 if (argv[i] == NULL)
6348 err(1, "strdup");
6351 va_end(ap);
6352 return argv;
6356 * Try to convert 'tog path' into a 'tog log path' command.
6357 * The user could simply have mistyped the command rather than knowingly
6358 * provided a path. So check whether argv[0] can in fact be resolved
6359 * to a path in the HEAD commit and print a special error if not.
6360 * This hack is for mpi@ <3
6362 static const struct got_error *
6363 tog_log_with_path(int argc, char *argv[])
6365 const struct got_error *error = NULL;
6366 struct tog_cmd *cmd = NULL;
6367 struct got_repository *repo = NULL;
6368 struct got_worktree *worktree = NULL;
6369 struct got_object_id *commit_id = NULL, *id = NULL;
6370 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6371 char *commit_id_str = NULL, **cmd_argv = NULL;
6373 cwd = getcwd(NULL, 0);
6374 if (cwd == NULL)
6375 return got_error_from_errno("getcwd");
6377 error = got_worktree_open(&worktree, cwd);
6378 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6379 goto done;
6381 if (worktree)
6382 repo_path = strdup(got_worktree_get_repo_path(worktree));
6383 else
6384 repo_path = strdup(cwd);
6385 if (repo_path == NULL) {
6386 error = got_error_from_errno("strdup");
6387 goto done;
6390 error = got_repo_open(&repo, repo_path, NULL);
6391 if (error != NULL)
6392 goto done;
6394 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6395 repo, worktree);
6396 if (error)
6397 goto done;
6399 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6400 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6401 GOT_OBJ_TYPE_COMMIT, 1, repo);
6402 if (error)
6403 goto done;
6405 if (worktree) {
6406 got_worktree_close(worktree);
6407 worktree = NULL;
6410 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6411 if (error) {
6412 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6413 goto done;
6414 fprintf(stderr, "%s: '%s' is no known command or path\n",
6415 getprogname(), argv[0]);
6416 usage(1, 1);
6417 /* not reached */
6420 got_repo_close(repo);
6421 repo = NULL;
6423 error = got_object_id_str(&commit_id_str, commit_id);
6424 if (error)
6425 goto done;
6427 cmd = &tog_commands[0]; /* log */
6428 argc = 4;
6429 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6430 error = cmd->cmd_main(argc, cmd_argv);
6431 done:
6432 if (repo)
6433 got_repo_close(repo);
6434 if (worktree)
6435 got_worktree_close(worktree);
6436 free(id);
6437 free(commit_id_str);
6438 free(commit_id);
6439 free(cwd);
6440 free(repo_path);
6441 free(in_repo_path);
6442 if (cmd_argv) {
6443 int i;
6444 for (i = 0; i < argc; i++)
6445 free(cmd_argv[i]);
6446 free(cmd_argv);
6448 return error;
6451 int
6452 main(int argc, char *argv[])
6454 const struct got_error *error = NULL;
6455 struct tog_cmd *cmd = NULL;
6456 int ch, hflag = 0, Vflag = 0;
6457 char **cmd_argv = NULL;
6458 static struct option longopts[] = {
6459 { "version", no_argument, NULL, 'V' },
6460 { NULL, 0, NULL, 0}
6463 setlocale(LC_CTYPE, "");
6465 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6466 switch (ch) {
6467 case 'h':
6468 hflag = 1;
6469 break;
6470 case 'V':
6471 Vflag = 1;
6472 break;
6473 default:
6474 usage(hflag, 1);
6475 /* NOTREACHED */
6479 argc -= optind;
6480 argv += optind;
6481 optind = 1;
6482 optreset = 1;
6484 if (Vflag) {
6485 got_version_print_str();
6486 return 0;
6489 if (argc == 0) {
6490 if (hflag)
6491 usage(hflag, 0);
6492 /* Build an argument vector which runs a default command. */
6493 cmd = &tog_commands[0];
6494 argc = 1;
6495 cmd_argv = make_argv(argc, cmd->name);
6496 } else {
6497 int i;
6499 /* Did the user specify a command? */
6500 for (i = 0; i < nitems(tog_commands); i++) {
6501 if (strncmp(tog_commands[i].name, argv[0],
6502 strlen(argv[0])) == 0) {
6503 cmd = &tog_commands[i];
6504 break;
6509 if (cmd == NULL) {
6510 if (argc != 1)
6511 usage(0, 1);
6512 /* No command specified; try log with a path */
6513 error = tog_log_with_path(argc, argv);
6514 } else {
6515 if (hflag)
6516 cmd->cmd_usage();
6517 else
6518 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6521 endwin();
6522 putchar('\n');
6523 if (cmd_argv) {
6524 int i;
6525 for (i = 0; i < argc; i++)
6526 free(cmd_argv[i]);
6527 free(cmd_argv);
6530 if (error && error->code != GOT_ERR_CANCELLED)
6531 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6532 return 0;