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, const size_t date_display_cols,
1281 int author_display_cols)
1283 struct tog_log_view_state *s = &view->state.log;
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(&s->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(&s->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(&s->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)
1526 const struct got_error *err = NULL;
1527 struct tog_log_view_state *s = &view->state.log;
1528 struct commit_queue_entry *entry;
1529 const int limit = view->nlines;
1530 int width;
1531 int ncommits, author_cols = 4;
1532 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1533 char *refs_str = NULL;
1534 wchar_t *wline;
1535 struct tog_color *tc;
1536 static const size_t date_display_cols = 12;
1538 entry = s->first_displayed_entry;
1539 ncommits = 0;
1540 while (entry) {
1541 if (ncommits == s->selected) {
1542 s->selected_entry = entry;
1543 break;
1545 entry = TAILQ_NEXT(entry, entry);
1546 ncommits++;
1549 if (s->selected_entry &&
1550 !(view->searching && view->search_next_done == 0)) {
1551 err = got_object_id_str(&id_str, s->selected_entry->id);
1552 if (err)
1553 return err;
1554 err = build_refs_str(&refs_str, &s->refs,
1555 s->selected_entry->id, s->repo);
1556 if (err)
1557 goto done;
1560 if (s->thread_args.commits_needed == 0)
1561 halfdelay(10); /* disable fast refresh */
1563 if (s->thread_args.commits_needed > 0) {
1564 if (asprintf(&ncommits_str, " [%d/%d] %s",
1565 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1566 (view->searching && !view->search_next_done) ?
1567 "searching..." : "loading...") == -1) {
1568 err = got_error_from_errno("asprintf");
1569 goto done;
1571 } else {
1572 const char *search_str = NULL;
1574 if (view->searching) {
1575 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1576 search_str = "no more matches";
1577 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1578 search_str = "no matches found";
1579 else if (!view->search_next_done)
1580 search_str = "searching...";
1583 if (asprintf(&ncommits_str, " [%d/%d] %s",
1584 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1585 search_str ? search_str :
1586 (refs_str ? refs_str : "")) == -1) {
1587 err = got_error_from_errno("asprintf");
1588 goto done;
1592 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1593 if (asprintf(&header, "commit %s %s%s",
1594 id_str ? id_str : "........................................",
1595 s->in_repo_path, ncommits_str) == -1) {
1596 err = got_error_from_errno("asprintf");
1597 header = NULL;
1598 goto done;
1600 } else if (asprintf(&header, "commit %s%s",
1601 id_str ? id_str : "........................................",
1602 ncommits_str) == -1) {
1603 err = got_error_from_errno("asprintf");
1604 header = NULL;
1605 goto done;
1607 err = format_line(&wline, &width, header, view->ncols, 0);
1608 if (err)
1609 goto done;
1611 werase(view->window);
1613 if (view_needs_focus_indication(view))
1614 wstandout(view->window);
1615 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1616 if (tc)
1617 wattr_on(view->window,
1618 COLOR_PAIR(tc->colorpair), NULL);
1619 waddwstr(view->window, wline);
1620 if (tc)
1621 wattr_off(view->window,
1622 COLOR_PAIR(tc->colorpair), NULL);
1623 while (width < view->ncols) {
1624 waddch(view->window, ' ');
1625 width++;
1627 if (view_needs_focus_indication(view))
1628 wstandend(view->window);
1629 free(wline);
1630 if (limit <= 1)
1631 goto done;
1633 /* Grow author column size if necessary. */
1634 entry = s->first_displayed_entry;
1635 ncommits = 0;
1636 while (entry) {
1637 char *author;
1638 wchar_t *wauthor;
1639 int width;
1640 if (ncommits >= limit - 1)
1641 break;
1642 author = strdup(got_object_commit_get_author(entry->commit));
1643 if (author == NULL) {
1644 err = got_error_from_errno("strdup");
1645 goto done;
1647 err = format_author(&wauthor, &width, author, COLS,
1648 date_display_cols);
1649 if (author_cols < width)
1650 author_cols = width;
1651 free(wauthor);
1652 free(author);
1653 ncommits++;
1654 entry = TAILQ_NEXT(entry, entry);
1657 entry = s->first_displayed_entry;
1658 s->last_displayed_entry = s->first_displayed_entry;
1659 ncommits = 0;
1660 while (entry) {
1661 if (ncommits >= limit - 1)
1662 break;
1663 if (ncommits == s->selected)
1664 wstandout(view->window);
1665 err = draw_commit(view, entry->commit, entry->id,
1666 date_display_cols, author_cols);
1667 if (ncommits == s->selected)
1668 wstandend(view->window);
1669 if (err)
1670 goto done;
1671 ncommits++;
1672 s->last_displayed_entry = entry;
1673 entry = TAILQ_NEXT(entry, entry);
1676 view_vborder(view);
1677 done:
1678 free(id_str);
1679 free(refs_str);
1680 free(ncommits_str);
1681 free(header);
1682 return err;
1685 static void
1686 log_scroll_up(struct tog_view *view, int maxscroll)
1688 struct tog_log_view_state *s = &view->state.log;
1689 struct commit_queue_entry *entry;
1690 int nscrolled = 0;
1692 entry = TAILQ_FIRST(&s->commits.head);
1693 if (s->first_displayed_entry == entry)
1694 return;
1696 entry = s->first_displayed_entry;
1697 while (entry && nscrolled < maxscroll) {
1698 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1699 if (entry) {
1700 s->first_displayed_entry = entry;
1701 nscrolled++;
1706 static const struct got_error *
1707 trigger_log_thread(struct tog_view *view, int wait)
1709 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1710 int errcode;
1712 halfdelay(1); /* fast refresh while loading commits */
1714 while (ta->commits_needed > 0) {
1715 if (ta->log_complete)
1716 break;
1718 /* Wake the log thread. */
1719 errcode = pthread_cond_signal(&ta->need_commits);
1720 if (errcode)
1721 return got_error_set_errno(errcode,
1722 "pthread_cond_signal");
1725 * The mutex will be released while the view loop waits
1726 * in wgetch(), at which time the log thread will run.
1728 if (!wait)
1729 break;
1731 /* Display progress update in log view. */
1732 show_log_view(view);
1733 update_panels();
1734 doupdate();
1736 /* Wait right here while next commit is being loaded. */
1737 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1738 if (errcode)
1739 return got_error_set_errno(errcode,
1740 "pthread_cond_wait");
1742 /* Display progress update in log view. */
1743 show_log_view(view);
1744 update_panels();
1745 doupdate();
1748 return NULL;
1751 static const struct got_error *
1752 log_scroll_down(struct tog_view *view, int maxscroll)
1754 struct tog_log_view_state *s = &view->state.log;
1755 const struct got_error *err = NULL;
1756 struct commit_queue_entry *pentry;
1757 int nscrolled = 0, ncommits_needed;
1759 if (s->last_displayed_entry == NULL)
1760 return NULL;
1762 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1763 if (s->commits.ncommits < ncommits_needed &&
1764 !s->thread_args.log_complete) {
1766 * Ask the log thread for required amount of commits.
1768 s->thread_args.commits_needed += maxscroll;
1769 err = trigger_log_thread(view, 1);
1770 if (err)
1771 return err;
1774 do {
1775 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1776 if (pentry == NULL)
1777 break;
1779 s->last_displayed_entry = pentry;
1781 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1782 if (pentry == NULL)
1783 break;
1784 s->first_displayed_entry = pentry;
1785 } while (++nscrolled < maxscroll);
1787 return err;
1790 static const struct got_error *
1791 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1792 struct got_commit_object *commit, struct got_object_id *commit_id,
1793 struct tog_view *log_view, struct got_repository *repo)
1795 const struct got_error *err;
1796 struct got_object_qid *parent_id;
1797 struct tog_view *diff_view;
1799 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1800 if (diff_view == NULL)
1801 return got_error_from_errno("view_open");
1803 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1804 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1805 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1806 if (err == NULL)
1807 *new_view = diff_view;
1808 return err;
1811 static const struct got_error *
1812 tree_view_visit_subtree(struct got_tree_object *subtree,
1813 struct tog_tree_view_state *s)
1815 struct tog_parent_tree *parent;
1817 parent = calloc(1, sizeof(*parent));
1818 if (parent == NULL)
1819 return got_error_from_errno("calloc");
1821 parent->tree = s->tree;
1822 parent->first_displayed_entry = s->first_displayed_entry;
1823 parent->selected_entry = s->selected_entry;
1824 parent->selected = s->selected;
1825 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1826 s->tree = subtree;
1827 s->selected = 0;
1828 s->first_displayed_entry = NULL;
1829 return NULL;
1832 static const struct got_error *
1833 tree_view_walk_path(struct tog_tree_view_state *s,
1834 struct got_object_id *commit_id,
1835 const char *path, struct got_repository *repo)
1837 const struct got_error *err = NULL;
1838 struct got_tree_object *tree = NULL;
1839 const char *p;
1840 char *slash, *subpath = NULL;
1842 /* Walk the path and open corresponding tree objects. */
1843 p = path;
1844 while (*p) {
1845 struct got_tree_entry *te;
1846 struct got_object_id *tree_id;
1847 char *te_name;
1849 while (p[0] == '/')
1850 p++;
1852 /* Ensure the correct subtree entry is selected. */
1853 slash = strchr(p, '/');
1854 if (slash == NULL)
1855 te_name = strdup(p);
1856 else
1857 te_name = strndup(p, slash - p);
1858 if (te_name == NULL) {
1859 err = got_error_from_errno("strndup");
1860 break;
1862 te = got_object_tree_find_entry(s->tree, te_name);
1863 if (te == NULL) {
1864 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1865 free(te_name);
1866 break;
1868 free(te_name);
1869 s->first_displayed_entry = s->selected_entry = te;
1871 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1872 break; /* jump to this file's entry */
1874 slash = strchr(p, '/');
1875 if (slash)
1876 subpath = strndup(path, slash - path);
1877 else
1878 subpath = strdup(path);
1879 if (subpath == NULL) {
1880 err = got_error_from_errno("strdup");
1881 break;
1884 err = got_object_id_by_path(&tree_id, repo, commit_id,
1885 subpath);
1886 if (err)
1887 break;
1889 err = got_object_open_as_tree(&tree, repo, tree_id);
1890 free(tree_id);
1891 if (err)
1892 break;
1894 err = tree_view_visit_subtree(tree, s);
1895 if (err) {
1896 got_object_tree_close(tree);
1897 break;
1899 if (slash == NULL)
1900 break;
1901 free(subpath);
1902 subpath = NULL;
1903 p = slash;
1906 free(subpath);
1907 return err;
1910 static const struct got_error *
1911 browse_commit_tree(struct tog_view **new_view, int begin_x,
1912 struct commit_queue_entry *entry, const char *path,
1913 struct got_repository *repo)
1915 const struct got_error *err = NULL;
1916 struct got_tree_object *tree;
1917 struct tog_tree_view_state *s;
1918 struct tog_view *tree_view;
1920 err = got_object_open_as_tree(&tree, repo,
1921 got_object_commit_get_tree_id(entry->commit));
1922 if (err)
1923 return err;
1925 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1926 if (tree_view == NULL)
1927 return got_error_from_errno("view_open");
1929 err = open_tree_view(tree_view, tree, entry->id, repo);
1930 if (err) {
1931 got_object_tree_close(tree);
1932 return err;
1934 s = &tree_view->state.tree;
1936 *new_view = tree_view;
1938 if (got_path_is_root_dir(path))
1939 return NULL;
1941 return tree_view_walk_path(s, entry->id, path, repo);
1944 static const struct got_error *
1945 block_signals_used_by_main_thread(void)
1947 sigset_t sigset;
1948 int errcode;
1950 if (sigemptyset(&sigset) == -1)
1951 return got_error_from_errno("sigemptyset");
1953 /* tog handles SIGWINCH and SIGCONT */
1954 if (sigaddset(&sigset, SIGWINCH) == -1)
1955 return got_error_from_errno("sigaddset");
1956 if (sigaddset(&sigset, SIGCONT) == -1)
1957 return got_error_from_errno("sigaddset");
1959 /* ncurses handles SIGTSTP */
1960 if (sigaddset(&sigset, SIGTSTP) == -1)
1961 return got_error_from_errno("sigaddset");
1963 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1964 if (errcode)
1965 return got_error_set_errno(errcode, "pthread_sigmask");
1967 return NULL;
1970 static void *
1971 log_thread(void *arg)
1973 const struct got_error *err = NULL;
1974 int errcode = 0;
1975 struct tog_log_thread_args *a = arg;
1976 int done = 0;
1978 err = block_signals_used_by_main_thread();
1979 if (err)
1980 return (void *)err;
1982 while (!done && !err && !tog_sigpipe_received) {
1983 err = queue_commits(a->graph, a->commits, 1, a->repo,
1984 a->in_repo_path, a->searching, a->search_next_done,
1985 a->regex);
1986 if (err) {
1987 if (err->code != GOT_ERR_ITER_COMPLETED)
1988 return (void *)err;
1989 err = NULL;
1990 done = 1;
1991 } else if (a->commits_needed > 0)
1992 a->commits_needed--;
1994 errcode = pthread_mutex_lock(&tog_mutex);
1995 if (errcode) {
1996 err = got_error_set_errno(errcode,
1997 "pthread_mutex_lock");
1998 break;
1999 } else if (*a->quit)
2000 done = 1;
2001 else if (*a->first_displayed_entry == NULL) {
2002 *a->first_displayed_entry =
2003 TAILQ_FIRST(&a->commits->head);
2004 *a->selected_entry = *a->first_displayed_entry;
2007 errcode = pthread_cond_signal(&a->commit_loaded);
2008 if (errcode) {
2009 err = got_error_set_errno(errcode,
2010 "pthread_cond_signal");
2011 pthread_mutex_unlock(&tog_mutex);
2012 break;
2015 if (done)
2016 a->commits_needed = 0;
2017 else {
2018 if (a->commits_needed == 0) {
2019 errcode = pthread_cond_wait(&a->need_commits,
2020 &tog_mutex);
2021 if (errcode)
2022 err = got_error_set_errno(errcode,
2023 "pthread_cond_wait");
2027 errcode = pthread_mutex_unlock(&tog_mutex);
2028 if (errcode && err == NULL)
2029 err = got_error_set_errno(errcode,
2030 "pthread_mutex_unlock");
2032 a->log_complete = 1;
2033 return (void *)err;
2036 static const struct got_error *
2037 stop_log_thread(struct tog_log_view_state *s)
2039 const struct got_error *err = NULL;
2040 int errcode;
2042 if (s->thread) {
2043 s->quit = 1;
2044 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2045 if (errcode)
2046 return got_error_set_errno(errcode,
2047 "pthread_cond_signal");
2048 errcode = pthread_mutex_unlock(&tog_mutex);
2049 if (errcode)
2050 return got_error_set_errno(errcode,
2051 "pthread_mutex_unlock");
2052 errcode = pthread_join(s->thread, (void **)&err);
2053 if (errcode)
2054 return got_error_set_errno(errcode, "pthread_join");
2055 errcode = pthread_mutex_lock(&tog_mutex);
2056 if (errcode)
2057 return got_error_set_errno(errcode,
2058 "pthread_mutex_lock");
2059 s->thread = NULL;
2062 if (s->thread_args.repo) {
2063 got_repo_close(s->thread_args.repo);
2064 s->thread_args.repo = NULL;
2067 if (s->thread_args.graph) {
2068 got_commit_graph_close(s->thread_args.graph);
2069 s->thread_args.graph = NULL;
2072 return err;
2075 static const struct got_error *
2076 close_log_view(struct tog_view *view)
2078 const struct got_error *err = NULL;
2079 struct tog_log_view_state *s = &view->state.log;
2080 int errcode;
2082 err = stop_log_thread(s);
2084 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2085 if (errcode && err == NULL)
2086 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2088 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2089 if (errcode && err == NULL)
2090 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2092 free_commits(&s->commits);
2093 free(s->in_repo_path);
2094 s->in_repo_path = NULL;
2095 free(s->start_id);
2096 s->start_id = NULL;
2097 got_ref_list_free(&s->refs);
2098 return err;
2101 static const struct got_error *
2102 search_start_log_view(struct tog_view *view)
2104 struct tog_log_view_state *s = &view->state.log;
2106 s->matched_entry = NULL;
2107 s->search_entry = NULL;
2108 return NULL;
2111 static const struct got_error *
2112 search_next_log_view(struct tog_view *view)
2114 const struct got_error *err = NULL;
2115 struct tog_log_view_state *s = &view->state.log;
2116 struct commit_queue_entry *entry;
2118 /* Display progress update in log view. */
2119 show_log_view(view);
2120 update_panels();
2121 doupdate();
2123 if (s->search_entry) {
2124 int errcode, ch;
2125 errcode = pthread_mutex_unlock(&tog_mutex);
2126 if (errcode)
2127 return got_error_set_errno(errcode,
2128 "pthread_mutex_unlock");
2129 ch = wgetch(view->window);
2130 errcode = pthread_mutex_lock(&tog_mutex);
2131 if (errcode)
2132 return got_error_set_errno(errcode,
2133 "pthread_mutex_lock");
2134 if (ch == KEY_BACKSPACE) {
2135 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2136 return NULL;
2138 if (view->searching == TOG_SEARCH_FORWARD)
2139 entry = TAILQ_NEXT(s->search_entry, entry);
2140 else
2141 entry = TAILQ_PREV(s->search_entry,
2142 commit_queue_head, entry);
2143 } else if (s->matched_entry) {
2144 if (view->searching == TOG_SEARCH_FORWARD)
2145 entry = TAILQ_NEXT(s->matched_entry, entry);
2146 else
2147 entry = TAILQ_PREV(s->matched_entry,
2148 commit_queue_head, entry);
2149 } else {
2150 if (view->searching == TOG_SEARCH_FORWARD)
2151 entry = TAILQ_FIRST(&s->commits.head);
2152 else
2153 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2156 while (1) {
2157 int have_match = 0;
2159 if (entry == NULL) {
2160 if (s->thread_args.log_complete ||
2161 view->searching == TOG_SEARCH_BACKWARD) {
2162 view->search_next_done =
2163 (s->matched_entry == NULL ?
2164 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2165 s->search_entry = NULL;
2166 return NULL;
2169 * Poke the log thread for more commits and return,
2170 * allowing the main loop to make progress. Search
2171 * will resume at s->search_entry once we come back.
2173 s->thread_args.commits_needed++;
2174 return trigger_log_thread(view, 0);
2177 err = match_commit(&have_match, entry->id, entry->commit,
2178 &view->regex);
2179 if (err)
2180 break;
2181 if (have_match) {
2182 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2183 s->matched_entry = entry;
2184 break;
2187 s->search_entry = entry;
2188 if (view->searching == TOG_SEARCH_FORWARD)
2189 entry = TAILQ_NEXT(entry, entry);
2190 else
2191 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2194 if (s->matched_entry) {
2195 int cur = s->selected_entry->idx;
2196 while (cur < s->matched_entry->idx) {
2197 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2198 if (err)
2199 return err;
2200 cur++;
2202 while (cur > s->matched_entry->idx) {
2203 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2204 if (err)
2205 return err;
2206 cur--;
2210 s->search_entry = NULL;
2212 return NULL;
2215 static const struct got_error *
2216 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2217 struct got_repository *repo, const char *head_ref_name,
2218 const char *in_repo_path, int log_branches)
2220 const struct got_error *err = NULL;
2221 struct tog_log_view_state *s = &view->state.log;
2222 struct got_repository *thread_repo = NULL;
2223 struct got_commit_graph *thread_graph = NULL;
2224 int errcode;
2226 SIMPLEQ_INIT(&s->refs);
2228 if (in_repo_path != s->in_repo_path) {
2229 free(s->in_repo_path);
2230 s->in_repo_path = strdup(in_repo_path);
2231 if (s->in_repo_path == NULL)
2232 return got_error_from_errno("strdup");
2235 /* The commit queue only contains commits being displayed. */
2236 TAILQ_INIT(&s->commits.head);
2237 s->commits.ncommits = 0;
2239 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2240 if (err)
2241 goto done;
2243 s->repo = repo;
2244 s->head_ref_name = head_ref_name;
2245 s->start_id = got_object_id_dup(start_id);
2246 if (s->start_id == NULL) {
2247 err = got_error_from_errno("got_object_id_dup");
2248 goto done;
2250 s->log_branches = log_branches;
2252 SIMPLEQ_INIT(&s->colors);
2253 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2254 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2255 get_color_value("TOG_COLOR_COMMIT"));
2256 if (err)
2257 goto done;
2258 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2259 get_color_value("TOG_COLOR_AUTHOR"));
2260 if (err) {
2261 free_colors(&s->colors);
2262 goto done;
2264 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2265 get_color_value("TOG_COLOR_DATE"));
2266 if (err) {
2267 free_colors(&s->colors);
2268 goto done;
2272 view->show = show_log_view;
2273 view->input = input_log_view;
2274 view->close = close_log_view;
2275 view->search_start = search_start_log_view;
2276 view->search_next = search_next_log_view;
2278 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2279 if (err)
2280 goto done;
2281 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2282 !s->log_branches);
2283 if (err)
2284 goto done;
2285 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2286 s->repo, NULL, NULL);
2287 if (err)
2288 goto done;
2290 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2291 if (errcode) {
2292 err = got_error_set_errno(errcode, "pthread_cond_init");
2293 goto done;
2295 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2296 if (errcode) {
2297 err = got_error_set_errno(errcode, "pthread_cond_init");
2298 goto done;
2301 s->thread_args.commits_needed = view->nlines;
2302 s->thread_args.graph = thread_graph;
2303 s->thread_args.commits = &s->commits;
2304 s->thread_args.in_repo_path = s->in_repo_path;
2305 s->thread_args.start_id = s->start_id;
2306 s->thread_args.repo = thread_repo;
2307 s->thread_args.log_complete = 0;
2308 s->thread_args.quit = &s->quit;
2309 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2310 s->thread_args.selected_entry = &s->selected_entry;
2311 s->thread_args.searching = &view->searching;
2312 s->thread_args.search_next_done = &view->search_next_done;
2313 s->thread_args.regex = &view->regex;
2314 done:
2315 if (err)
2316 close_log_view(view);
2317 return err;
2320 static const struct got_error *
2321 show_log_view(struct tog_view *view)
2323 const struct got_error *err;
2324 struct tog_log_view_state *s = &view->state.log;
2326 if (s->thread == NULL) {
2327 int errcode = pthread_create(&s->thread, NULL, log_thread,
2328 &s->thread_args);
2329 if (errcode)
2330 return got_error_set_errno(errcode, "pthread_create");
2331 if (s->thread_args.commits_needed > 0) {
2332 err = trigger_log_thread(view, 1);
2333 if (err)
2334 return err;
2338 return draw_commits(view);
2341 static const struct got_error *
2342 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2343 struct tog_view **focus_view, struct tog_view *view, int ch)
2345 const struct got_error *err = NULL;
2346 struct tog_log_view_state *s = &view->state.log;
2347 char *parent_path, *in_repo_path = NULL;
2348 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2349 struct tog_view *ref_view = NULL;
2350 int begin_x = 0;
2351 struct got_object_id *start_id;
2353 switch (ch) {
2354 case 'q':
2355 s->quit = 1;
2356 break;
2357 case 'k':
2358 case KEY_UP:
2359 case '<':
2360 case ',':
2361 if (s->first_displayed_entry == NULL)
2362 break;
2363 if (s->selected > 0)
2364 s->selected--;
2365 else
2366 log_scroll_up(view, 1);
2367 break;
2368 case KEY_PPAGE:
2369 case CTRL('b'):
2370 if (s->first_displayed_entry == NULL)
2371 break;
2372 if (TAILQ_FIRST(&s->commits.head) ==
2373 s->first_displayed_entry) {
2374 s->selected = 0;
2375 break;
2377 log_scroll_up(view, view->nlines - 1);
2378 break;
2379 case 'j':
2380 case KEY_DOWN:
2381 case '>':
2382 case '.':
2383 if (s->first_displayed_entry == NULL)
2384 break;
2385 if (s->selected < MIN(view->nlines - 2,
2386 s->commits.ncommits - 1)) {
2387 s->selected++;
2388 break;
2390 err = log_scroll_down(view, 1);
2391 break;
2392 case KEY_NPAGE:
2393 case CTRL('f'): {
2394 struct commit_queue_entry *first;
2395 first = s->first_displayed_entry;
2396 if (first == NULL)
2397 break;
2398 err = log_scroll_down(view, view->nlines - 1);
2399 if (err)
2400 break;
2401 if (first == s->first_displayed_entry &&
2402 s->selected < MIN(view->nlines - 2,
2403 s->commits.ncommits - 1)) {
2404 /* can't scroll further down */
2405 s->selected = MIN(view->nlines - 2,
2406 s->commits.ncommits - 1);
2408 err = NULL;
2409 break;
2411 case KEY_RESIZE:
2412 if (s->selected > view->nlines - 2)
2413 s->selected = view->nlines - 2;
2414 if (s->selected > s->commits.ncommits - 1)
2415 s->selected = s->commits.ncommits - 1;
2416 break;
2417 case KEY_ENTER:
2418 case ' ':
2419 case '\r':
2420 if (s->selected_entry == NULL)
2421 break;
2422 if (view_is_parent_view(view))
2423 begin_x = view_split_begin_x(view->begin_x);
2424 err = open_diff_view_for_commit(&diff_view, begin_x,
2425 s->selected_entry->commit, s->selected_entry->id,
2426 view, s->repo);
2427 if (err)
2428 break;
2429 if (view_is_parent_view(view)) {
2430 err = view_close_child(view);
2431 if (err)
2432 return err;
2433 err = view_set_child(view, diff_view);
2434 if (err) {
2435 view_close(diff_view);
2436 break;
2438 *focus_view = diff_view;
2439 view->child_focussed = 1;
2440 } else
2441 *new_view = diff_view;
2442 break;
2443 case 't':
2444 if (s->selected_entry == NULL)
2445 break;
2446 if (view_is_parent_view(view))
2447 begin_x = view_split_begin_x(view->begin_x);
2448 err = browse_commit_tree(&tree_view, begin_x,
2449 s->selected_entry, s->in_repo_path, s->repo);
2450 if (err)
2451 break;
2452 if (view_is_parent_view(view)) {
2453 err = view_close_child(view);
2454 if (err)
2455 return err;
2456 err = view_set_child(view, tree_view);
2457 if (err) {
2458 view_close(tree_view);
2459 break;
2461 *focus_view = tree_view;
2462 view->child_focussed = 1;
2463 } else
2464 *new_view = tree_view;
2465 break;
2466 case KEY_BACKSPACE:
2467 if (got_path_cmp(s->in_repo_path, "/",
2468 strlen(s->in_repo_path), 1) == 0)
2469 break;
2470 err = got_path_dirname(&parent_path, s->in_repo_path);
2471 if (err)
2472 return err;
2473 err = stop_log_thread(s);
2474 if (err) {
2475 free(parent_path);
2476 return err;
2478 lv = view_open(view->nlines, view->ncols,
2479 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2480 if (lv == NULL) {
2481 free(parent_path);
2482 return got_error_from_errno("view_open");
2484 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2485 parent_path, s->log_branches);
2486 free(parent_path);
2487 if (err)
2488 return err;;
2489 if (view_is_parent_view(view))
2490 *new_view = lv;
2491 else {
2492 view_set_child(view->parent, lv);
2493 *focus_view = lv;
2495 break;
2496 case CTRL('l'):
2497 err = stop_log_thread(s);
2498 if (err)
2499 return err;
2500 lv = view_open(view->nlines, view->ncols,
2501 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2502 if (lv == NULL)
2503 return got_error_from_errno("view_open");
2504 err = got_repo_match_object_id(&start_id, NULL,
2505 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2506 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2507 if (err) {
2508 view_close(lv);
2509 return err;
2511 in_repo_path = strdup(s->in_repo_path);
2512 if (in_repo_path == NULL) {
2513 free(start_id);
2514 view_close(lv);
2515 return got_error_from_errno("strdup");
2517 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2518 in_repo_path, s->log_branches);
2519 if (err) {
2520 free(start_id);
2521 view_close(lv);
2522 return err;;
2524 *dead_view = view;
2525 *new_view = lv;
2526 break;
2527 case 'B':
2528 s->log_branches = !s->log_branches;
2529 err = stop_log_thread(s);
2530 if (err)
2531 return err;
2532 lv = view_open(view->nlines, view->ncols,
2533 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2534 if (lv == NULL)
2535 return got_error_from_errno("view_open");
2536 err = open_log_view(lv, s->start_id, s->repo,
2537 s->head_ref_name, s->in_repo_path, s->log_branches);
2538 if (err) {
2539 view_close(lv);
2540 return err;;
2542 *dead_view = view;
2543 *new_view = lv;
2544 break;
2545 case 'r':
2546 if (view_is_parent_view(view))
2547 begin_x = view_split_begin_x(view->begin_x);
2548 ref_view = view_open(view->nlines, view->ncols,
2549 view->begin_y, begin_x, TOG_VIEW_REF);
2550 if (ref_view == NULL)
2551 return got_error_from_errno("view_open");
2552 err = open_ref_view(ref_view, s->repo);
2553 if (err) {
2554 view_close(ref_view);
2555 return err;
2557 if (view_is_parent_view(view)) {
2558 err = view_close_child(view);
2559 if (err)
2560 return err;
2561 err = view_set_child(view, ref_view);
2562 if (err) {
2563 view_close(ref_view);
2564 break;
2566 *focus_view = ref_view;
2567 view->child_focussed = 1;
2568 } else
2569 *new_view = ref_view;
2570 break;
2571 default:
2572 break;
2575 return err;
2578 static const struct got_error *
2579 apply_unveil(const char *repo_path, const char *worktree_path)
2581 const struct got_error *error;
2583 #ifdef PROFILE
2584 if (unveil("gmon.out", "rwc") != 0)
2585 return got_error_from_errno2("unveil", "gmon.out");
2586 #endif
2587 if (repo_path && unveil(repo_path, "r") != 0)
2588 return got_error_from_errno2("unveil", repo_path);
2590 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2591 return got_error_from_errno2("unveil", worktree_path);
2593 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2594 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2596 error = got_privsep_unveil_exec_helpers();
2597 if (error != NULL)
2598 return error;
2600 if (unveil(NULL, NULL) != 0)
2601 return got_error_from_errno("unveil");
2603 return NULL;
2606 static void
2607 init_curses(void)
2609 initscr();
2610 cbreak();
2611 halfdelay(1); /* Do fast refresh while initial view is loading. */
2612 noecho();
2613 nonl();
2614 intrflush(stdscr, FALSE);
2615 keypad(stdscr, TRUE);
2616 curs_set(0);
2617 if (getenv("TOG_COLORS") != NULL) {
2618 start_color();
2619 use_default_colors();
2621 signal(SIGWINCH, tog_sigwinch);
2622 signal(SIGPIPE, tog_sigpipe);
2623 signal(SIGCONT, tog_sigcont);
2626 static const struct got_error *
2627 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2628 struct got_repository *repo, struct got_worktree *worktree)
2630 const struct got_error *err = NULL;
2632 if (argc == 0) {
2633 *in_repo_path = strdup("/");
2634 if (*in_repo_path == NULL)
2635 return got_error_from_errno("strdup");
2636 return NULL;
2639 if (worktree) {
2640 const char *prefix = got_worktree_get_path_prefix(worktree);
2641 char *p;
2643 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2644 if (err)
2645 return err;
2646 if (asprintf(in_repo_path, "%s%s%s", prefix,
2647 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2648 p) == -1) {
2649 err = got_error_from_errno("asprintf");
2650 *in_repo_path = NULL;
2652 free(p);
2653 } else
2654 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2656 return err;
2659 static const struct got_error *
2660 cmd_log(int argc, char *argv[])
2662 const struct got_error *error;
2663 struct got_repository *repo = NULL;
2664 struct got_worktree *worktree = NULL;
2665 struct got_object_id *start_id = NULL;
2666 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2667 char *start_commit = NULL, *head_ref_name = NULL;
2668 int ch, log_branches = 0;
2669 struct tog_view *view;
2671 #ifndef PROFILE
2672 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2673 NULL) == -1)
2674 err(1, "pledge");
2675 #endif
2677 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2678 switch (ch) {
2679 case 'b':
2680 log_branches = 1;
2681 break;
2682 case 'c':
2683 start_commit = optarg;
2684 break;
2685 case 'r':
2686 repo_path = realpath(optarg, NULL);
2687 if (repo_path == NULL)
2688 return got_error_from_errno2("realpath",
2689 optarg);
2690 break;
2691 default:
2692 usage_log();
2693 /* NOTREACHED */
2697 argc -= optind;
2698 argv += optind;
2700 if (argc > 1)
2701 usage_log();
2703 cwd = getcwd(NULL, 0);
2704 if (cwd == NULL)
2705 return got_error_from_errno("getcwd");
2707 error = got_worktree_open(&worktree, cwd);
2708 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2709 goto done;
2711 if (repo_path == NULL) {
2712 if (worktree)
2713 repo_path =
2714 strdup(got_worktree_get_repo_path(worktree));
2715 else
2716 repo_path = strdup(cwd);
2718 if (repo_path == NULL) {
2719 error = got_error_from_errno("strdup");
2720 goto done;
2723 error = got_repo_open(&repo, repo_path, NULL);
2724 if (error != NULL)
2725 goto done;
2727 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2728 repo, worktree);
2729 if (error)
2730 goto done;
2732 init_curses();
2734 error = apply_unveil(got_repo_get_path(repo),
2735 worktree ? got_worktree_get_root_path(worktree) : NULL);
2736 if (error)
2737 goto done;
2739 if (start_commit == NULL)
2740 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2741 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2742 GOT_OBJ_TYPE_COMMIT, 1, repo);
2743 else
2744 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2745 GOT_OBJ_TYPE_COMMIT, 1, repo);
2746 if (error != NULL)
2747 goto done;
2749 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2750 if (view == NULL) {
2751 error = got_error_from_errno("view_open");
2752 goto done;
2754 if (worktree) {
2755 head_ref_name = strdup(
2756 got_worktree_get_head_ref_name(worktree));
2757 if (head_ref_name == NULL) {
2758 error = got_error_from_errno("strdup");
2759 goto done;
2762 error = open_log_view(view, start_id, repo, head_ref_name,
2763 in_repo_path, log_branches);
2764 if (error)
2765 goto done;
2766 if (worktree) {
2767 /* Release work tree lock. */
2768 got_worktree_close(worktree);
2769 worktree = NULL;
2771 error = view_loop(view);
2772 done:
2773 free(in_repo_path);
2774 free(repo_path);
2775 free(cwd);
2776 free(start_id);
2777 free(head_ref_name);
2778 if (repo)
2779 got_repo_close(repo);
2780 if (worktree)
2781 got_worktree_close(worktree);
2782 return error;
2785 __dead static void
2786 usage_diff(void)
2788 endwin();
2789 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2790 "[-w] object1 object2\n", getprogname());
2791 exit(1);
2794 static char *
2795 parse_next_line(FILE *f, size_t *len)
2797 char *line;
2798 size_t linelen;
2799 size_t lineno;
2800 const char delim[3] = { '\0', '\0', '\0'};
2802 line = fparseln(f, &linelen, &lineno, delim, 0);
2803 if (len)
2804 *len = linelen;
2805 return line;
2808 static int
2809 match_line(const char *line, regex_t *regex, size_t nmatch,
2810 regmatch_t *regmatch)
2812 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2815 struct tog_color *
2816 match_color(struct tog_colors *colors, const char *line)
2818 struct tog_color *tc = NULL;
2820 SIMPLEQ_FOREACH(tc, colors, entry) {
2821 if (match_line(line, &tc->regex, 0, NULL))
2822 return tc;
2825 return NULL;
2828 static const struct got_error *
2829 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2830 WINDOW *window, regmatch_t *regmatch)
2832 const struct got_error *err = NULL;
2833 wchar_t *wline;
2834 int width;
2835 char *s;
2837 *wtotal = 0;
2839 s = strndup(line, regmatch->rm_so);
2840 if (s == NULL)
2841 return got_error_from_errno("strndup");
2843 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2844 if (err) {
2845 free(s);
2846 return err;
2848 waddwstr(window, wline);
2849 free(wline);
2850 free(s);
2851 wlimit -= width;
2852 *wtotal += width;
2854 if (wlimit > 0) {
2855 s = strndup(line + regmatch->rm_so,
2856 regmatch->rm_eo - regmatch->rm_so);
2857 if (s == NULL) {
2858 err = got_error_from_errno("strndup");
2859 free(s);
2860 return err;
2862 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2863 if (err) {
2864 free(s);
2865 return err;
2867 wattr_on(window, A_STANDOUT, NULL);
2868 waddwstr(window, wline);
2869 wattr_off(window, A_STANDOUT, NULL);
2870 free(wline);
2871 free(s);
2872 wlimit -= width;
2873 *wtotal += width;
2876 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2877 err = format_line(&wline, &width,
2878 line + regmatch->rm_eo, wlimit, col_tab_align);
2879 if (err)
2880 return err;
2881 waddwstr(window, wline);
2882 free(wline);
2883 *wtotal += width;
2886 return NULL;
2889 static const struct got_error *
2890 draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2891 off_t *line_offsets, int selected_line, int max_lines,
2892 int *last_displayed_line, int *eof, const char *header,
2893 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
2895 const struct got_error *err;
2896 int nprinted = 0;
2897 char *line;
2898 struct tog_color *tc;
2899 size_t len;
2900 wchar_t *wline;
2901 int width;
2902 off_t line_offset;
2904 line_offset = line_offsets[first_displayed_line - 1];
2905 if (fseeko(f, line_offset, SEEK_SET) == -1)
2906 return got_error_from_errno("fseek");
2908 werase(view->window);
2910 if (header) {
2911 if (asprintf(&line, "[%d/%d] %s",
2912 first_displayed_line - 1 + selected_line, nlines,
2913 header) == -1)
2914 return got_error_from_errno("asprintf");
2915 err = format_line(&wline, &width, line, view->ncols, 0);
2916 free(line);
2917 if (err)
2918 return err;
2920 if (view_needs_focus_indication(view))
2921 wstandout(view->window);
2922 waddwstr(view->window, wline);
2923 free(wline);
2924 wline = NULL;
2925 if (view_needs_focus_indication(view))
2926 wstandend(view->window);
2927 if (width <= view->ncols - 1)
2928 waddch(view->window, '\n');
2930 if (max_lines <= 1)
2931 return NULL;
2932 max_lines--;
2935 *eof = 0;
2936 while (max_lines > 0 && nprinted < max_lines) {
2937 line = parse_next_line(f, &len);
2938 if (line == NULL) {
2939 *eof = 1;
2940 break;
2943 tc = match_color(colors, line);
2944 if (tc)
2945 wattr_on(view->window,
2946 COLOR_PAIR(tc->colorpair), NULL);
2947 if (first_displayed_line + nprinted == matched_line &&
2948 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2949 err = add_matched_line(&width, line, view->ncols, 0,
2950 view->window, regmatch);
2951 if (err) {
2952 free(line);
2953 return err;
2955 } else {
2956 err = format_line(&wline, &width, line, view->ncols, 0);
2957 if (err) {
2958 free(line);
2959 return err;
2961 waddwstr(view->window, wline);
2962 free(wline);
2963 wline = NULL;
2965 if (tc)
2966 wattr_off(view->window,
2967 COLOR_PAIR(tc->colorpair), NULL);
2968 if (width <= view->ncols - 1)
2969 waddch(view->window, '\n');
2970 nprinted++;
2971 free(line);
2973 if (nprinted >= 1)
2974 *last_displayed_line = first_displayed_line + (nprinted - 1);
2975 else
2976 *last_displayed_line = first_displayed_line;
2978 view_vborder(view);
2980 if (*eof) {
2981 while (nprinted < view->nlines) {
2982 waddch(view->window, '\n');
2983 nprinted++;
2986 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2987 if (err) {
2988 return err;
2991 wstandout(view->window);
2992 waddwstr(view->window, wline);
2993 free(wline);
2994 wline = NULL;
2995 wstandend(view->window);
2998 return NULL;
3001 static char *
3002 get_datestr(time_t *time, char *datebuf)
3004 struct tm mytm, *tm;
3005 char *p, *s;
3007 tm = gmtime_r(time, &mytm);
3008 if (tm == NULL)
3009 return NULL;
3010 s = asctime_r(tm, datebuf);
3011 if (s == NULL)
3012 return NULL;
3013 p = strchr(s, '\n');
3014 if (p)
3015 *p = '\0';
3016 return s;
3019 static const struct got_error *
3020 get_changed_paths(struct got_pathlist_head *paths,
3021 struct got_commit_object *commit, struct got_repository *repo)
3023 const struct got_error *err = NULL;
3024 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3025 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3026 struct got_object_qid *qid;
3028 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3029 if (qid != NULL) {
3030 struct got_commit_object *pcommit;
3031 err = got_object_open_as_commit(&pcommit, repo,
3032 qid->id);
3033 if (err)
3034 return err;
3036 tree_id1 = got_object_commit_get_tree_id(pcommit);
3037 got_object_commit_close(pcommit);
3041 if (tree_id1) {
3042 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3043 if (err)
3044 goto done;
3047 tree_id2 = got_object_commit_get_tree_id(commit);
3048 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3049 if (err)
3050 goto done;
3052 err = got_diff_tree(tree1, tree2, "", "", repo,
3053 got_diff_tree_collect_changed_paths, paths, 0);
3054 done:
3055 if (tree1)
3056 got_object_tree_close(tree1);
3057 if (tree2)
3058 got_object_tree_close(tree2);
3059 return err;
3062 static const struct got_error *
3063 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3065 off_t *p;
3067 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3068 if (p == NULL)
3069 return got_error_from_errno("reallocarray");
3070 *line_offsets = p;
3071 (*line_offsets)[*nlines] = off;
3072 (*nlines)++;
3073 return NULL;
3076 static const struct got_error *
3077 write_commit_info(off_t **line_offsets, size_t *nlines,
3078 struct got_object_id *commit_id, struct got_reflist_head *refs,
3079 struct got_repository *repo, FILE *outfile)
3081 const struct got_error *err = NULL;
3082 char datebuf[26], *datestr;
3083 struct got_commit_object *commit;
3084 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3085 time_t committer_time;
3086 const char *author, *committer;
3087 char *refs_str = NULL;
3088 struct got_pathlist_head changed_paths;
3089 struct got_pathlist_entry *pe;
3090 off_t outoff = 0;
3091 int n;
3093 TAILQ_INIT(&changed_paths);
3095 if (refs) {
3096 err = build_refs_str(&refs_str, refs, commit_id, repo);
3097 if (err)
3098 return err;
3101 err = got_object_open_as_commit(&commit, repo, commit_id);
3102 if (err)
3103 return err;
3105 err = got_object_id_str(&id_str, commit_id);
3106 if (err) {
3107 err = got_error_from_errno("got_object_id_str");
3108 goto done;
3111 err = add_line_offset(line_offsets, nlines, 0);
3112 if (err)
3113 goto done;
3115 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3116 refs_str ? refs_str : "", refs_str ? ")" : "");
3117 if (n < 0) {
3118 err = got_error_from_errno("fprintf");
3119 goto done;
3121 outoff += n;
3122 err = add_line_offset(line_offsets, nlines, outoff);
3123 if (err)
3124 goto done;
3126 n = fprintf(outfile, "from: %s\n",
3127 got_object_commit_get_author(commit));
3128 if (n < 0) {
3129 err = got_error_from_errno("fprintf");
3130 goto done;
3132 outoff += n;
3133 err = add_line_offset(line_offsets, nlines, outoff);
3134 if (err)
3135 goto done;
3137 committer_time = got_object_commit_get_committer_time(commit);
3138 datestr = get_datestr(&committer_time, datebuf);
3139 if (datestr) {
3140 n = fprintf(outfile, "date: %s UTC\n", datestr);
3141 if (n < 0) {
3142 err = got_error_from_errno("fprintf");
3143 goto done;
3145 outoff += n;
3146 err = add_line_offset(line_offsets, nlines, outoff);
3147 if (err)
3148 goto done;
3150 author = got_object_commit_get_author(commit);
3151 committer = got_object_commit_get_committer(commit);
3152 if (strcmp(author, committer) != 0) {
3153 n = fprintf(outfile, "via: %s\n", committer);
3154 if (n < 0) {
3155 err = got_error_from_errno("fprintf");
3156 goto done;
3158 outoff += n;
3159 err = add_line_offset(line_offsets, nlines, outoff);
3160 if (err)
3161 goto done;
3163 err = got_object_commit_get_logmsg(&logmsg, commit);
3164 if (err)
3165 goto done;
3166 s = logmsg;
3167 while ((line = strsep(&s, "\n")) != NULL) {
3168 n = fprintf(outfile, "%s\n", line);
3169 if (n < 0) {
3170 err = got_error_from_errno("fprintf");
3171 goto done;
3173 outoff += n;
3174 err = add_line_offset(line_offsets, nlines, outoff);
3175 if (err)
3176 goto done;
3179 err = get_changed_paths(&changed_paths, commit, repo);
3180 if (err)
3181 goto done;
3182 TAILQ_FOREACH(pe, &changed_paths, entry) {
3183 struct got_diff_changed_path *cp = pe->data;
3184 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3185 if (n < 0) {
3186 err = got_error_from_errno("fprintf");
3187 goto done;
3189 outoff += n;
3190 err = add_line_offset(line_offsets, nlines, outoff);
3191 if (err)
3192 goto done;
3193 free((char *)pe->path);
3194 free(pe->data);
3197 fputc('\n', outfile);
3198 outoff++;
3199 err = add_line_offset(line_offsets, nlines, outoff);
3200 done:
3201 got_pathlist_free(&changed_paths);
3202 free(id_str);
3203 free(logmsg);
3204 free(refs_str);
3205 got_object_commit_close(commit);
3206 if (err) {
3207 free(*line_offsets);
3208 *line_offsets = NULL;
3209 *nlines = 0;
3211 return err;
3214 static const struct got_error *
3215 create_diff(struct tog_diff_view_state *s)
3217 const struct got_error *err = NULL;
3218 FILE *f = NULL;
3219 int obj_type;
3221 free(s->line_offsets);
3222 s->line_offsets = malloc(sizeof(off_t));
3223 if (s->line_offsets == NULL)
3224 return got_error_from_errno("malloc");
3225 s->nlines = 0;
3227 f = got_opentemp();
3228 if (f == NULL) {
3229 err = got_error_from_errno("got_opentemp");
3230 goto done;
3232 if (s->f && fclose(s->f) != 0) {
3233 err = got_error_from_errno("fclose");
3234 goto done;
3236 s->f = f;
3238 if (s->id1)
3239 err = got_object_get_type(&obj_type, s->repo, s->id1);
3240 else
3241 err = got_object_get_type(&obj_type, s->repo, s->id2);
3242 if (err)
3243 goto done;
3245 switch (obj_type) {
3246 case GOT_OBJ_TYPE_BLOB:
3247 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3248 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3249 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3250 break;
3251 case GOT_OBJ_TYPE_TREE:
3252 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3253 s->id1, s->id2, "", "", s->diff_context,
3254 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3255 break;
3256 case GOT_OBJ_TYPE_COMMIT: {
3257 const struct got_object_id_queue *parent_ids;
3258 struct got_object_qid *pid;
3259 struct got_commit_object *commit2;
3261 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3262 if (err)
3263 goto done;
3264 /* Show commit info if we're diffing to a parent/root commit. */
3265 if (s->id1 == NULL) {
3266 err = write_commit_info(&s->line_offsets, &s->nlines,
3267 s->id2, &s->refs, s->repo, s->f);
3268 if (err)
3269 goto done;
3270 } else {
3271 parent_ids = got_object_commit_get_parent_ids(commit2);
3272 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3273 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3274 err = write_commit_info(
3275 &s->line_offsets, &s->nlines,
3276 s->id2, &s->refs, s->repo, s->f);
3277 if (err)
3278 goto done;
3279 break;
3283 got_object_commit_close(commit2);
3285 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3286 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3287 s->force_text_diff, s->repo, s->f);
3288 break;
3290 default:
3291 err = got_error(GOT_ERR_OBJ_TYPE);
3292 break;
3294 if (err)
3295 goto done;
3296 done:
3297 if (s->f && fflush(s->f) != 0 && err == NULL)
3298 err = got_error_from_errno("fflush");
3299 return err;
3302 static void
3303 diff_view_indicate_progress(struct tog_view *view)
3305 mvwaddstr(view->window, 0, 0, "diffing...");
3306 update_panels();
3307 doupdate();
3310 static const struct got_error *
3311 search_start_diff_view(struct tog_view *view)
3313 struct tog_diff_view_state *s = &view->state.diff;
3315 s->matched_line = 0;
3316 return NULL;
3319 static const struct got_error *
3320 search_next_diff_view(struct tog_view *view)
3322 struct tog_diff_view_state *s = &view->state.diff;
3323 int lineno;
3325 if (!view->searching) {
3326 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3327 return NULL;
3330 if (s->matched_line) {
3331 if (view->searching == TOG_SEARCH_FORWARD)
3332 lineno = s->matched_line + 1;
3333 else
3334 lineno = s->matched_line - 1;
3335 } else {
3336 if (view->searching == TOG_SEARCH_FORWARD)
3337 lineno = 1;
3338 else
3339 lineno = s->nlines;
3342 while (1) {
3343 char *line = NULL;
3344 off_t offset;
3345 size_t len;
3347 if (lineno <= 0 || lineno > s->nlines) {
3348 if (s->matched_line == 0) {
3349 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3350 free(line);
3351 break;
3354 if (view->searching == TOG_SEARCH_FORWARD)
3355 lineno = 1;
3356 else
3357 lineno = s->nlines;
3360 offset = s->line_offsets[lineno - 1];
3361 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3362 free(line);
3363 return got_error_from_errno("fseeko");
3365 free(line);
3366 line = parse_next_line(s->f, &len);
3367 if (line &&
3368 match_line(line, &view->regex, 1, &view->regmatch)) {
3369 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3370 s->matched_line = lineno;
3371 free(line);
3372 break;
3374 free(line);
3375 if (view->searching == TOG_SEARCH_FORWARD)
3376 lineno++;
3377 else
3378 lineno--;
3381 if (s->matched_line) {
3382 s->first_displayed_line = s->matched_line;
3383 s->selected_line = 1;
3386 return NULL;
3389 static const struct got_error *
3390 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3391 struct got_object_id *id2, const char *label1, const char *label2,
3392 int diff_context, int ignore_whitespace, int force_text_diff,
3393 struct tog_view *log_view, struct got_repository *repo)
3395 const struct got_error *err;
3396 struct tog_diff_view_state *s = &view->state.diff;
3398 SIMPLEQ_INIT(&s->refs);
3400 if (id1 != NULL && id2 != NULL) {
3401 int type1, type2;
3402 err = got_object_get_type(&type1, repo, id1);
3403 if (err)
3404 return err;
3405 err = got_object_get_type(&type2, repo, id2);
3406 if (err)
3407 return err;
3409 if (type1 != type2)
3410 return got_error(GOT_ERR_OBJ_TYPE);
3412 s->first_displayed_line = 1;
3413 s->last_displayed_line = view->nlines;
3414 s->selected_line = 1;
3415 s->repo = repo;
3416 s->id1 = id1;
3417 s->id2 = id2;
3418 s->label1 = label1;
3419 s->label2 = label2;
3421 if (id1) {
3422 s->id1 = got_object_id_dup(id1);
3423 if (s->id1 == NULL)
3424 return got_error_from_errno("got_object_id_dup");
3425 } else
3426 s->id1 = NULL;
3428 s->id2 = got_object_id_dup(id2);
3429 if (s->id2 == NULL) {
3430 free(s->id1);
3431 s->id1 = NULL;
3432 return got_error_from_errno("got_object_id_dup");
3434 s->f = NULL;
3435 s->first_displayed_line = 1;
3436 s->last_displayed_line = view->nlines;
3437 s->diff_context = diff_context;
3438 s->ignore_whitespace = ignore_whitespace;
3439 s->force_text_diff = force_text_diff;
3440 s->log_view = log_view;
3441 s->repo = repo;
3443 SIMPLEQ_INIT(&s->colors);
3444 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3445 err = add_color(&s->colors,
3446 "^-", TOG_COLOR_DIFF_MINUS,
3447 get_color_value("TOG_COLOR_DIFF_MINUS"));
3448 if (err)
3449 return err;
3450 err = add_color(&s->colors, "^\\+",
3451 TOG_COLOR_DIFF_PLUS,
3452 get_color_value("TOG_COLOR_DIFF_PLUS"));
3453 if (err) {
3454 free_colors(&s->colors);
3455 return err;
3457 err = add_color(&s->colors,
3458 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3459 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3460 if (err) {
3461 free_colors(&s->colors);
3462 return err;
3465 err = add_color(&s->colors,
3466 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3467 TOG_COLOR_DIFF_META,
3468 get_color_value("TOG_COLOR_DIFF_META"));
3469 if (err) {
3470 free_colors(&s->colors);
3471 return err;
3474 err = add_color(&s->colors,
3475 "^(from|via): ", TOG_COLOR_AUTHOR,
3476 get_color_value("TOG_COLOR_AUTHOR"));
3477 if (err) {
3478 free_colors(&s->colors);
3479 return err;
3482 err = add_color(&s->colors,
3483 "^date: ", TOG_COLOR_DATE,
3484 get_color_value("TOG_COLOR_DATE"));
3485 if (err) {
3486 free_colors(&s->colors);
3487 return err;
3491 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3492 if (err) {
3493 free(s->id1);
3494 s->id1 = NULL;
3495 free(s->id2);
3496 s->id2 = NULL;
3497 free_colors(&s->colors);
3498 return err;
3501 if (log_view && view_is_splitscreen(view))
3502 show_log_view(log_view); /* draw vborder */
3503 diff_view_indicate_progress(view);
3505 s->line_offsets = NULL;
3506 s->nlines = 0;
3507 err = create_diff(s);
3508 if (err) {
3509 free(s->id1);
3510 s->id1 = NULL;
3511 free(s->id2);
3512 s->id2 = NULL;
3513 free_colors(&s->colors);
3514 got_ref_list_free(&s->refs);
3515 return err;
3518 view->show = show_diff_view;
3519 view->input = input_diff_view;
3520 view->close = close_diff_view;
3521 view->search_start = search_start_diff_view;
3522 view->search_next = search_next_diff_view;
3524 return NULL;
3527 static const struct got_error *
3528 close_diff_view(struct tog_view *view)
3530 const struct got_error *err = NULL;
3531 struct tog_diff_view_state *s = &view->state.diff;
3533 free(s->id1);
3534 s->id1 = NULL;
3535 free(s->id2);
3536 s->id2 = NULL;
3537 if (s->f && fclose(s->f) == EOF)
3538 err = got_error_from_errno("fclose");
3539 free_colors(&s->colors);
3540 free(s->line_offsets);
3541 s->line_offsets = NULL;
3542 s->nlines = 0;
3543 got_ref_list_free(&s->refs);
3544 return err;
3547 static const struct got_error *
3548 show_diff_view(struct tog_view *view)
3550 const struct got_error *err;
3551 struct tog_diff_view_state *s = &view->state.diff;
3552 char *id_str1 = NULL, *id_str2, *header;
3553 const char *label1, *label2;
3555 if (s->id1) {
3556 err = got_object_id_str(&id_str1, s->id1);
3557 if (err)
3558 return err;
3559 label1 = s->label1 ? : id_str1;
3560 } else
3561 label1 = "/dev/null";
3563 err = got_object_id_str(&id_str2, s->id2);
3564 if (err)
3565 return err;
3566 label2 = s->label2 ? : id_str2;
3568 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3569 err = got_error_from_errno("asprintf");
3570 free(id_str1);
3571 free(id_str2);
3572 return err;
3574 free(id_str1);
3575 free(id_str2);
3577 return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3578 s->line_offsets, s->selected_line, view->nlines,
3579 &s->last_displayed_line, &s->eof, header, &s->colors,
3580 s->matched_line, &view->regmatch);
3583 static const struct got_error *
3584 set_selected_commit(struct tog_diff_view_state *s,
3585 struct commit_queue_entry *entry)
3587 const struct got_error *err;
3588 const struct got_object_id_queue *parent_ids;
3589 struct got_commit_object *selected_commit;
3590 struct got_object_qid *pid;
3592 free(s->id2);
3593 s->id2 = got_object_id_dup(entry->id);
3594 if (s->id2 == NULL)
3595 return got_error_from_errno("got_object_id_dup");
3597 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3598 if (err)
3599 return err;
3600 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3601 free(s->id1);
3602 pid = SIMPLEQ_FIRST(parent_ids);
3603 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3604 got_object_commit_close(selected_commit);
3605 return NULL;
3608 static const struct got_error *
3609 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3610 struct tog_view **focus_view, struct tog_view *view, int ch)
3612 const struct got_error *err = NULL;
3613 struct tog_diff_view_state *s = &view->state.diff;
3614 struct tog_log_view_state *ls;
3615 struct commit_queue_entry *entry;
3616 int i;
3618 switch (ch) {
3619 case 'a':
3620 case 'w':
3621 if (ch == 'a')
3622 s->force_text_diff = !s->force_text_diff;
3623 if (ch == 'w')
3624 s->ignore_whitespace = !s->ignore_whitespace;
3625 wclear(view->window);
3626 s->first_displayed_line = 1;
3627 s->last_displayed_line = view->nlines;
3628 diff_view_indicate_progress(view);
3629 err = create_diff(s);
3630 break;
3631 case 'k':
3632 case KEY_UP:
3633 if (s->first_displayed_line > 1)
3634 s->first_displayed_line--;
3635 break;
3636 case KEY_PPAGE:
3637 case CTRL('b'):
3638 if (s->first_displayed_line == 1)
3639 break;
3640 i = 0;
3641 while (i++ < view->nlines - 1 &&
3642 s->first_displayed_line > 1)
3643 s->first_displayed_line--;
3644 break;
3645 case 'j':
3646 case KEY_DOWN:
3647 if (!s->eof)
3648 s->first_displayed_line++;
3649 break;
3650 case KEY_NPAGE:
3651 case CTRL('f'):
3652 case ' ':
3653 if (s->eof)
3654 break;
3655 i = 0;
3656 while (!s->eof && i++ < view->nlines - 1) {
3657 char *line;
3658 line = parse_next_line(s->f, NULL);
3659 s->first_displayed_line++;
3660 if (line == NULL)
3661 break;
3663 break;
3664 case '[':
3665 if (s->diff_context > 0) {
3666 s->diff_context--;
3667 diff_view_indicate_progress(view);
3668 err = create_diff(s);
3669 if (s->first_displayed_line + view->nlines - 1 >
3670 s->nlines) {
3671 s->first_displayed_line = 1;
3672 s->last_displayed_line = view->nlines;
3675 break;
3676 case ']':
3677 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3678 s->diff_context++;
3679 diff_view_indicate_progress(view);
3680 err = create_diff(s);
3682 break;
3683 case '<':
3684 case ',':
3685 if (s->log_view == NULL)
3686 break;
3687 ls = &s->log_view->state.log;
3688 entry = TAILQ_PREV(ls->selected_entry,
3689 commit_queue_head, entry);
3690 if (entry == NULL)
3691 break;
3693 err = input_log_view(NULL, NULL, NULL, s->log_view,
3694 KEY_UP);
3695 if (err)
3696 break;
3698 err = set_selected_commit(s, entry);
3699 if (err)
3700 break;
3702 s->first_displayed_line = 1;
3703 s->last_displayed_line = view->nlines;
3705 diff_view_indicate_progress(view);
3706 err = create_diff(s);
3707 break;
3708 case '>':
3709 case '.':
3710 if (s->log_view == NULL)
3711 break;
3712 ls = &s->log_view->state.log;
3714 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3715 ls->thread_args.commits_needed++;
3716 err = trigger_log_thread(s->log_view, 1);
3717 if (err)
3718 break;
3720 err = input_log_view(NULL, NULL, NULL, s->log_view,
3721 KEY_DOWN);
3722 if (err)
3723 break;
3725 entry = TAILQ_NEXT(ls->selected_entry, entry);
3726 if (entry == NULL)
3727 break;
3729 err = set_selected_commit(s, entry);
3730 if (err)
3731 break;
3733 s->first_displayed_line = 1;
3734 s->last_displayed_line = view->nlines;
3736 diff_view_indicate_progress(view);
3737 err = create_diff(s);
3738 break;
3739 default:
3740 break;
3743 return err;
3746 static const struct got_error *
3747 cmd_diff(int argc, char *argv[])
3749 const struct got_error *error = NULL;
3750 struct got_repository *repo = NULL;
3751 struct got_worktree *worktree = NULL;
3752 struct got_object_id *id1 = NULL, *id2 = NULL;
3753 char *repo_path = NULL, *cwd = NULL;
3754 char *id_str1 = NULL, *id_str2 = NULL;
3755 char *label1 = NULL, *label2 = NULL;
3756 int diff_context = 3, ignore_whitespace = 0;
3757 int ch, force_text_diff = 0;
3758 const char *errstr;
3759 struct tog_view *view;
3761 #ifndef PROFILE
3762 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3763 NULL) == -1)
3764 err(1, "pledge");
3765 #endif
3766 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3767 switch (ch) {
3768 case 'a':
3769 force_text_diff = 1;
3770 break;
3771 case 'C':
3772 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3773 &errstr);
3774 if (errstr != NULL)
3775 err(1, "-C option %s", errstr);
3776 break;
3777 case 'r':
3778 repo_path = realpath(optarg, NULL);
3779 if (repo_path == NULL)
3780 return got_error_from_errno2("realpath",
3781 optarg);
3782 got_path_strip_trailing_slashes(repo_path);
3783 break;
3784 case 'w':
3785 ignore_whitespace = 1;
3786 break;
3787 default:
3788 usage_diff();
3789 /* NOTREACHED */
3793 argc -= optind;
3794 argv += optind;
3796 if (argc == 0) {
3797 usage_diff(); /* TODO show local worktree changes */
3798 } else if (argc == 2) {
3799 id_str1 = argv[0];
3800 id_str2 = argv[1];
3801 } else
3802 usage_diff();
3804 cwd = getcwd(NULL, 0);
3805 if (cwd == NULL)
3806 return got_error_from_errno("getcwd");
3808 error = got_worktree_open(&worktree, cwd);
3809 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3810 goto done;
3812 if (repo_path == NULL) {
3813 if (worktree)
3814 repo_path =
3815 strdup(got_worktree_get_repo_path(worktree));
3816 else
3817 repo_path = strdup(cwd);
3819 if (repo_path == NULL) {
3820 error = got_error_from_errno("strdup");
3821 goto done;
3824 error = got_repo_open(&repo, repo_path, NULL);
3825 if (error)
3826 goto done;
3828 init_curses();
3830 error = apply_unveil(got_repo_get_path(repo), NULL);
3831 if (error)
3832 goto done;
3834 error = got_repo_match_object_id(&id1, &label1, id_str1,
3835 GOT_OBJ_TYPE_ANY, 1, repo);
3836 if (error)
3837 goto done;
3839 error = got_repo_match_object_id(&id2, &label2, id_str2,
3840 GOT_OBJ_TYPE_ANY, 1, repo);
3841 if (error)
3842 goto done;
3844 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3845 if (view == NULL) {
3846 error = got_error_from_errno("view_open");
3847 goto done;
3849 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3850 ignore_whitespace, force_text_diff, NULL, repo);
3851 if (error)
3852 goto done;
3853 error = view_loop(view);
3854 done:
3855 free(label1);
3856 free(label2);
3857 free(repo_path);
3858 free(cwd);
3859 if (repo)
3860 got_repo_close(repo);
3861 if (worktree)
3862 got_worktree_close(worktree);
3863 return error;
3866 __dead static void
3867 usage_blame(void)
3869 endwin();
3870 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3871 getprogname());
3872 exit(1);
3875 struct tog_blame_line {
3876 int annotated;
3877 struct got_object_id *id;
3880 static const struct got_error *
3881 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3882 const char *path, struct tog_blame_line *lines, int nlines,
3883 int blame_complete, int selected_line, int *first_displayed_line,
3884 int *last_displayed_line, int *eof, int max_lines,
3885 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
3887 const struct got_error *err;
3888 int lineno = 0, nprinted = 0;
3889 char *line;
3890 size_t len;
3891 wchar_t *wline;
3892 int width;
3893 struct tog_blame_line *blame_line;
3894 struct got_object_id *prev_id = NULL;
3895 char *id_str;
3896 struct tog_color *tc;
3898 err = got_object_id_str(&id_str, id);
3899 if (err)
3900 return err;
3902 rewind(f);
3903 werase(view->window);
3905 if (asprintf(&line, "commit %s", id_str) == -1) {
3906 err = got_error_from_errno("asprintf");
3907 free(id_str);
3908 return err;
3911 err = format_line(&wline, &width, line, view->ncols, 0);
3912 free(line);
3913 line = NULL;
3914 if (err)
3915 return err;
3916 if (view_needs_focus_indication(view))
3917 wstandout(view->window);
3918 tc = get_color(colors, TOG_COLOR_COMMIT);
3919 if (tc)
3920 wattr_on(view->window,
3921 COLOR_PAIR(tc->colorpair), NULL);
3922 waddwstr(view->window, wline);
3923 if (tc)
3924 wattr_off(view->window,
3925 COLOR_PAIR(tc->colorpair), NULL);
3926 if (view_needs_focus_indication(view))
3927 wstandend(view->window);
3928 free(wline);
3929 wline = NULL;
3930 if (width < view->ncols - 1)
3931 waddch(view->window, '\n');
3933 if (asprintf(&line, "[%d/%d] %s%s",
3934 *first_displayed_line - 1 + selected_line, nlines,
3935 blame_complete ? "" : "annotating... ", path) == -1) {
3936 free(id_str);
3937 return got_error_from_errno("asprintf");
3939 free(id_str);
3940 err = format_line(&wline, &width, line, view->ncols, 0);
3941 free(line);
3942 line = NULL;
3943 if (err)
3944 return err;
3945 waddwstr(view->window, wline);
3946 free(wline);
3947 wline = NULL;
3948 if (width < view->ncols - 1)
3949 waddch(view->window, '\n');
3951 *eof = 0;
3952 while (nprinted < max_lines - 2) {
3953 line = parse_next_line(f, &len);
3954 if (line == NULL) {
3955 *eof = 1;
3956 break;
3958 if (++lineno < *first_displayed_line) {
3959 free(line);
3960 continue;
3963 if (view->focussed && nprinted == selected_line - 1)
3964 wstandout(view->window);
3966 if (nlines > 0) {
3967 blame_line = &lines[lineno - 1];
3968 if (blame_line->annotated && prev_id &&
3969 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3970 !(view->focussed &&
3971 nprinted == selected_line - 1)) {
3972 waddstr(view->window, " ");
3973 } else if (blame_line->annotated) {
3974 char *id_str;
3975 err = got_object_id_str(&id_str, blame_line->id);
3976 if (err) {
3977 free(line);
3978 return err;
3980 tc = get_color(colors, TOG_COLOR_COMMIT);
3981 if (tc)
3982 wattr_on(view->window,
3983 COLOR_PAIR(tc->colorpair), NULL);
3984 wprintw(view->window, "%.8s", id_str);
3985 if (tc)
3986 wattr_off(view->window,
3987 COLOR_PAIR(tc->colorpair), NULL);
3988 free(id_str);
3989 prev_id = blame_line->id;
3990 } else {
3991 waddstr(view->window, "........");
3992 prev_id = NULL;
3994 } else {
3995 waddstr(view->window, "........");
3996 prev_id = NULL;
3999 if (view->focussed && nprinted == selected_line - 1)
4000 wstandend(view->window);
4001 waddstr(view->window, " ");
4003 if (view->ncols <= 9) {
4004 width = 9;
4005 wline = wcsdup(L"");
4006 if (wline == NULL) {
4007 err = got_error_from_errno("wcsdup");
4008 free(line);
4009 return err;
4011 } else if (*first_displayed_line + nprinted == matched_line &&
4012 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4013 err = add_matched_line(&width, line, view->ncols - 9, 9,
4014 view->window, regmatch);
4015 if (err) {
4016 free(line);
4017 return err;
4019 width += 9;
4020 } else {
4021 err = format_line(&wline, &width, line,
4022 view->ncols - 9, 9);
4023 waddwstr(view->window, wline);
4024 free(wline);
4025 wline = NULL;
4026 width += 9;
4029 if (width <= view->ncols - 1)
4030 waddch(view->window, '\n');
4031 if (++nprinted == 1)
4032 *first_displayed_line = lineno;
4033 free(line);
4035 *last_displayed_line = lineno;
4037 view_vborder(view);
4039 return NULL;
4042 static const struct got_error *
4043 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4045 const struct got_error *err = NULL;
4046 struct tog_blame_cb_args *a = arg;
4047 struct tog_blame_line *line;
4048 int errcode;
4050 if (nlines != a->nlines ||
4051 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4052 return got_error(GOT_ERR_RANGE);
4054 errcode = pthread_mutex_lock(&tog_mutex);
4055 if (errcode)
4056 return got_error_set_errno(errcode, "pthread_mutex_lock");
4058 if (*a->quit) { /* user has quit the blame view */
4059 err = got_error(GOT_ERR_ITER_COMPLETED);
4060 goto done;
4063 if (lineno == -1)
4064 goto done; /* no change in this commit */
4066 line = &a->lines[lineno - 1];
4067 if (line->annotated)
4068 goto done;
4070 line->id = got_object_id_dup(id);
4071 if (line->id == NULL) {
4072 err = got_error_from_errno("got_object_id_dup");
4073 goto done;
4075 line->annotated = 1;
4076 done:
4077 errcode = pthread_mutex_unlock(&tog_mutex);
4078 if (errcode)
4079 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4080 return err;
4083 static void *
4084 blame_thread(void *arg)
4086 const struct got_error *err;
4087 struct tog_blame_thread_args *ta = arg;
4088 struct tog_blame_cb_args *a = ta->cb_args;
4089 int errcode;
4091 err = block_signals_used_by_main_thread();
4092 if (err)
4093 return (void *)err;
4095 err = got_blame(ta->path, a->commit_id, ta->repo,
4096 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4097 if (err && err->code == GOT_ERR_CANCELLED)
4098 err = NULL;
4100 errcode = pthread_mutex_lock(&tog_mutex);
4101 if (errcode)
4102 return (void *)got_error_set_errno(errcode,
4103 "pthread_mutex_lock");
4105 got_repo_close(ta->repo);
4106 ta->repo = NULL;
4107 *ta->complete = 1;
4109 errcode = pthread_mutex_unlock(&tog_mutex);
4110 if (errcode && err == NULL)
4111 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4113 return (void *)err;
4116 static struct got_object_id *
4117 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4118 int first_displayed_line, int selected_line)
4120 struct tog_blame_line *line;
4122 if (nlines <= 0)
4123 return NULL;
4125 line = &lines[first_displayed_line - 1 + selected_line - 1];
4126 if (!line->annotated)
4127 return NULL;
4129 return line->id;
4132 static const struct got_error *
4133 stop_blame(struct tog_blame *blame)
4135 const struct got_error *err = NULL;
4136 int i;
4138 if (blame->thread) {
4139 int errcode;
4140 errcode = pthread_mutex_unlock(&tog_mutex);
4141 if (errcode)
4142 return got_error_set_errno(errcode,
4143 "pthread_mutex_unlock");
4144 errcode = pthread_join(blame->thread, (void **)&err);
4145 if (errcode)
4146 return got_error_set_errno(errcode, "pthread_join");
4147 errcode = pthread_mutex_lock(&tog_mutex);
4148 if (errcode)
4149 return got_error_set_errno(errcode,
4150 "pthread_mutex_lock");
4151 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4152 err = NULL;
4153 blame->thread = NULL;
4155 if (blame->thread_args.repo) {
4156 got_repo_close(blame->thread_args.repo);
4157 blame->thread_args.repo = NULL;
4159 if (blame->f) {
4160 if (fclose(blame->f) != 0 && err == NULL)
4161 err = got_error_from_errno("fclose");
4162 blame->f = NULL;
4164 if (blame->lines) {
4165 for (i = 0; i < blame->nlines; i++)
4166 free(blame->lines[i].id);
4167 free(blame->lines);
4168 blame->lines = NULL;
4170 free(blame->cb_args.commit_id);
4171 blame->cb_args.commit_id = NULL;
4173 return err;
4176 static const struct got_error *
4177 cancel_blame_view(void *arg)
4179 const struct got_error *err = NULL;
4180 int *done = arg;
4181 int errcode;
4183 errcode = pthread_mutex_lock(&tog_mutex);
4184 if (errcode)
4185 return got_error_set_errno(errcode,
4186 "pthread_mutex_unlock");
4188 if (*done)
4189 err = got_error(GOT_ERR_CANCELLED);
4191 errcode = pthread_mutex_unlock(&tog_mutex);
4192 if (errcode)
4193 return got_error_set_errno(errcode,
4194 "pthread_mutex_lock");
4196 return err;
4199 static const struct got_error *
4200 run_blame(struct tog_view *view)
4202 struct tog_blame_view_state *s = &view->state.blame;
4203 struct tog_blame *blame = &s->blame;
4204 const struct got_error *err = NULL;
4205 struct got_blob_object *blob = NULL;
4206 struct got_repository *thread_repo = NULL;
4207 struct got_object_id *obj_id = NULL;
4208 int obj_type;
4210 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4211 s->path);
4212 if (err)
4213 return err;
4215 err = got_object_get_type(&obj_type, s->repo, obj_id);
4216 if (err)
4217 goto done;
4219 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4220 err = got_error(GOT_ERR_OBJ_TYPE);
4221 goto done;
4224 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4225 if (err)
4226 goto done;
4227 blame->f = got_opentemp();
4228 if (blame->f == NULL) {
4229 err = got_error_from_errno("got_opentemp");
4230 goto done;
4232 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4233 &blame->line_offsets, blame->f, blob);
4234 if (err || blame->nlines == 0)
4235 goto done;
4237 /* Don't include \n at EOF in the blame line count. */
4238 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4239 blame->nlines--;
4241 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4242 if (blame->lines == NULL) {
4243 err = got_error_from_errno("calloc");
4244 goto done;
4247 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4248 if (err)
4249 goto done;
4251 blame->cb_args.view = view;
4252 blame->cb_args.lines = blame->lines;
4253 blame->cb_args.nlines = blame->nlines;
4254 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4255 if (blame->cb_args.commit_id == NULL) {
4256 err = got_error_from_errno("got_object_id_dup");
4257 goto done;
4259 blame->cb_args.quit = &s->done;
4261 blame->thread_args.path = s->path;
4262 blame->thread_args.repo = thread_repo;
4263 blame->thread_args.cb_args = &blame->cb_args;
4264 blame->thread_args.complete = &s->blame_complete;
4265 blame->thread_args.cancel_cb = cancel_blame_view;
4266 blame->thread_args.cancel_arg = &s->done;
4267 s->blame_complete = 0;
4269 done:
4270 if (blob)
4271 got_object_blob_close(blob);
4272 free(obj_id);
4273 if (err)
4274 stop_blame(blame);
4275 return err;
4278 static const struct got_error *
4279 open_blame_view(struct tog_view *view, char *path,
4280 struct got_object_id *commit_id, struct got_repository *repo)
4282 const struct got_error *err = NULL;
4283 struct tog_blame_view_state *s = &view->state.blame;
4285 SIMPLEQ_INIT(&s->blamed_commits);
4287 s->path = strdup(path);
4288 if (s->path == NULL)
4289 return got_error_from_errno("strdup");
4291 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4292 if (err) {
4293 free(s->path);
4294 return err;
4297 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4298 s->first_displayed_line = 1;
4299 s->last_displayed_line = view->nlines;
4300 s->selected_line = 1;
4301 s->blame_complete = 0;
4302 s->repo = repo;
4303 s->commit_id = commit_id;
4304 memset(&s->blame, 0, sizeof(s->blame));
4306 SIMPLEQ_INIT(&s->colors);
4307 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4308 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4309 get_color_value("TOG_COLOR_COMMIT"));
4310 if (err)
4311 return err;
4314 view->show = show_blame_view;
4315 view->input = input_blame_view;
4316 view->close = close_blame_view;
4317 view->search_start = search_start_blame_view;
4318 view->search_next = search_next_blame_view;
4320 return run_blame(view);
4323 static const struct got_error *
4324 close_blame_view(struct tog_view *view)
4326 const struct got_error *err = NULL;
4327 struct tog_blame_view_state *s = &view->state.blame;
4329 if (s->blame.thread)
4330 err = stop_blame(&s->blame);
4332 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4333 struct got_object_qid *blamed_commit;
4334 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4335 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4336 got_object_qid_free(blamed_commit);
4339 free(s->path);
4340 free_colors(&s->colors);
4342 return err;
4345 static const struct got_error *
4346 search_start_blame_view(struct tog_view *view)
4348 struct tog_blame_view_state *s = &view->state.blame;
4350 s->matched_line = 0;
4351 return NULL;
4354 static const struct got_error *
4355 search_next_blame_view(struct tog_view *view)
4357 struct tog_blame_view_state *s = &view->state.blame;
4358 int lineno;
4360 if (!view->searching) {
4361 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4362 return NULL;
4365 if (s->matched_line) {
4366 if (view->searching == TOG_SEARCH_FORWARD)
4367 lineno = s->matched_line + 1;
4368 else
4369 lineno = s->matched_line - 1;
4370 } else {
4371 if (view->searching == TOG_SEARCH_FORWARD)
4372 lineno = 1;
4373 else
4374 lineno = s->blame.nlines;
4377 while (1) {
4378 char *line = NULL;
4379 off_t offset;
4380 size_t len;
4382 if (lineno <= 0 || lineno > s->blame.nlines) {
4383 if (s->matched_line == 0) {
4384 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4385 free(line);
4386 break;
4389 if (view->searching == TOG_SEARCH_FORWARD)
4390 lineno = 1;
4391 else
4392 lineno = s->blame.nlines;
4395 offset = s->blame.line_offsets[lineno - 1];
4396 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4397 free(line);
4398 return got_error_from_errno("fseeko");
4400 free(line);
4401 line = parse_next_line(s->blame.f, &len);
4402 if (line &&
4403 match_line(line, &view->regex, 1, &view->regmatch)) {
4404 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4405 s->matched_line = lineno;
4406 free(line);
4407 break;
4409 free(line);
4410 if (view->searching == TOG_SEARCH_FORWARD)
4411 lineno++;
4412 else
4413 lineno--;
4416 if (s->matched_line) {
4417 s->first_displayed_line = s->matched_line;
4418 s->selected_line = 1;
4421 return NULL;
4424 static const struct got_error *
4425 show_blame_view(struct tog_view *view)
4427 const struct got_error *err = NULL;
4428 struct tog_blame_view_state *s = &view->state.blame;
4429 int errcode;
4431 if (s->blame.thread == NULL) {
4432 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4433 &s->blame.thread_args);
4434 if (errcode)
4435 return got_error_set_errno(errcode, "pthread_create");
4437 halfdelay(1); /* fast refresh while annotating */
4440 if (s->blame_complete)
4441 halfdelay(10); /* disable fast refresh */
4443 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4444 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4445 s->selected_line, &s->first_displayed_line,
4446 &s->last_displayed_line, &s->eof, view->nlines, &s->colors,
4447 s->matched_line, &view->regmatch);
4449 view_vborder(view);
4450 return err;
4453 static const struct got_error *
4454 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4455 struct tog_view **focus_view, struct tog_view *view, int ch)
4457 const struct got_error *err = NULL, *thread_err = NULL;
4458 struct tog_view *diff_view;
4459 struct tog_blame_view_state *s = &view->state.blame;
4460 int begin_x = 0;
4462 switch (ch) {
4463 case 'q':
4464 s->done = 1;
4465 break;
4466 case 'k':
4467 case KEY_UP:
4468 if (s->selected_line > 1)
4469 s->selected_line--;
4470 else if (s->selected_line == 1 &&
4471 s->first_displayed_line > 1)
4472 s->first_displayed_line--;
4473 break;
4474 case KEY_PPAGE:
4475 case CTRL('b'):
4476 if (s->first_displayed_line == 1) {
4477 s->selected_line = 1;
4478 break;
4480 if (s->first_displayed_line > view->nlines - 2)
4481 s->first_displayed_line -=
4482 (view->nlines - 2);
4483 else
4484 s->first_displayed_line = 1;
4485 break;
4486 case 'j':
4487 case KEY_DOWN:
4488 if (s->selected_line < view->nlines - 2 &&
4489 s->first_displayed_line +
4490 s->selected_line <= s->blame.nlines)
4491 s->selected_line++;
4492 else if (s->last_displayed_line <
4493 s->blame.nlines)
4494 s->first_displayed_line++;
4495 break;
4496 case 'b':
4497 case 'p': {
4498 struct got_object_id *id = NULL;
4499 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4500 s->first_displayed_line, s->selected_line);
4501 if (id == NULL)
4502 break;
4503 if (ch == 'p') {
4504 struct got_commit_object *commit;
4505 struct got_object_qid *pid;
4506 struct got_object_id *blob_id = NULL;
4507 int obj_type;
4508 err = got_object_open_as_commit(&commit,
4509 s->repo, id);
4510 if (err)
4511 break;
4512 pid = SIMPLEQ_FIRST(
4513 got_object_commit_get_parent_ids(commit));
4514 if (pid == NULL) {
4515 got_object_commit_close(commit);
4516 break;
4518 /* Check if path history ends here. */
4519 err = got_object_id_by_path(&blob_id, s->repo,
4520 pid->id, s->path);
4521 if (err) {
4522 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4523 err = NULL;
4524 got_object_commit_close(commit);
4525 break;
4527 err = got_object_get_type(&obj_type, s->repo,
4528 blob_id);
4529 free(blob_id);
4530 /* Can't blame non-blob type objects. */
4531 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4532 got_object_commit_close(commit);
4533 break;
4535 err = got_object_qid_alloc(&s->blamed_commit,
4536 pid->id);
4537 got_object_commit_close(commit);
4538 } else {
4539 if (got_object_id_cmp(id,
4540 s->blamed_commit->id) == 0)
4541 break;
4542 err = got_object_qid_alloc(&s->blamed_commit,
4543 id);
4545 if (err)
4546 break;
4547 s->done = 1;
4548 thread_err = stop_blame(&s->blame);
4549 s->done = 0;
4550 if (thread_err)
4551 break;
4552 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4553 s->blamed_commit, entry);
4554 err = run_blame(view);
4555 if (err)
4556 break;
4557 break;
4559 case 'B': {
4560 struct got_object_qid *first;
4561 first = SIMPLEQ_FIRST(&s->blamed_commits);
4562 if (!got_object_id_cmp(first->id, s->commit_id))
4563 break;
4564 s->done = 1;
4565 thread_err = stop_blame(&s->blame);
4566 s->done = 0;
4567 if (thread_err)
4568 break;
4569 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4570 got_object_qid_free(s->blamed_commit);
4571 s->blamed_commit =
4572 SIMPLEQ_FIRST(&s->blamed_commits);
4573 err = run_blame(view);
4574 if (err)
4575 break;
4576 break;
4578 case KEY_ENTER:
4579 case '\r': {
4580 struct got_object_id *id = NULL;
4581 struct got_object_qid *pid;
4582 struct got_commit_object *commit = NULL;
4583 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4584 s->first_displayed_line, s->selected_line);
4585 if (id == NULL)
4586 break;
4587 err = got_object_open_as_commit(&commit, s->repo, id);
4588 if (err)
4589 break;
4590 pid = SIMPLEQ_FIRST(
4591 got_object_commit_get_parent_ids(commit));
4592 if (view_is_parent_view(view))
4593 begin_x = view_split_begin_x(view->begin_x);
4594 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4595 if (diff_view == NULL) {
4596 got_object_commit_close(commit);
4597 err = got_error_from_errno("view_open");
4598 break;
4600 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4601 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4602 got_object_commit_close(commit);
4603 if (err) {
4604 view_close(diff_view);
4605 break;
4607 if (view_is_parent_view(view)) {
4608 err = view_close_child(view);
4609 if (err)
4610 break;
4611 err = view_set_child(view, diff_view);
4612 if (err) {
4613 view_close(diff_view);
4614 break;
4616 *focus_view = diff_view;
4617 view->child_focussed = 1;
4618 } else
4619 *new_view = diff_view;
4620 if (err)
4621 break;
4622 break;
4624 case KEY_NPAGE:
4625 case CTRL('f'):
4626 case ' ':
4627 if (s->last_displayed_line >= s->blame.nlines &&
4628 s->selected_line >= MIN(s->blame.nlines,
4629 view->nlines - 2)) {
4630 break;
4632 if (s->last_displayed_line >= s->blame.nlines &&
4633 s->selected_line < view->nlines - 2) {
4634 s->selected_line = MIN(s->blame.nlines,
4635 view->nlines - 2);
4636 break;
4638 if (s->last_displayed_line + view->nlines - 2
4639 <= s->blame.nlines)
4640 s->first_displayed_line +=
4641 view->nlines - 2;
4642 else
4643 s->first_displayed_line =
4644 s->blame.nlines -
4645 (view->nlines - 3);
4646 break;
4647 case KEY_RESIZE:
4648 if (s->selected_line > view->nlines - 2) {
4649 s->selected_line = MIN(s->blame.nlines,
4650 view->nlines - 2);
4652 break;
4653 default:
4654 break;
4656 return thread_err ? thread_err : err;
4659 static const struct got_error *
4660 cmd_blame(int argc, char *argv[])
4662 const struct got_error *error;
4663 struct got_repository *repo = NULL;
4664 struct got_worktree *worktree = NULL;
4665 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4666 char *link_target = NULL;
4667 struct got_object_id *commit_id = NULL;
4668 char *commit_id_str = NULL;
4669 int ch;
4670 struct tog_view *view;
4672 #ifndef PROFILE
4673 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4674 NULL) == -1)
4675 err(1, "pledge");
4676 #endif
4678 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4679 switch (ch) {
4680 case 'c':
4681 commit_id_str = optarg;
4682 break;
4683 case 'r':
4684 repo_path = realpath(optarg, NULL);
4685 if (repo_path == NULL)
4686 return got_error_from_errno2("realpath",
4687 optarg);
4688 break;
4689 default:
4690 usage_blame();
4691 /* NOTREACHED */
4695 argc -= optind;
4696 argv += optind;
4698 if (argc != 1)
4699 usage_blame();
4701 cwd = getcwd(NULL, 0);
4702 if (cwd == NULL)
4703 return got_error_from_errno("getcwd");
4705 error = got_worktree_open(&worktree, cwd);
4706 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4707 goto done;
4709 if (repo_path == NULL) {
4710 if (worktree)
4711 repo_path =
4712 strdup(got_worktree_get_repo_path(worktree));
4713 else
4714 repo_path = strdup(cwd);
4716 if (repo_path == NULL) {
4717 error = got_error_from_errno("strdup");
4718 goto done;
4721 error = got_repo_open(&repo, repo_path, NULL);
4722 if (error != NULL)
4723 goto done;
4725 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4726 worktree);
4727 if (error)
4728 goto done;
4730 init_curses();
4732 error = apply_unveil(got_repo_get_path(repo), NULL);
4733 if (error)
4734 goto done;
4736 if (commit_id_str == NULL) {
4737 struct got_reference *head_ref;
4738 error = got_ref_open(&head_ref, repo, worktree ?
4739 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4740 if (error != NULL)
4741 goto done;
4742 error = got_ref_resolve(&commit_id, repo, head_ref);
4743 got_ref_close(head_ref);
4744 } else {
4745 error = got_repo_match_object_id(&commit_id, NULL,
4746 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4748 if (error != NULL)
4749 goto done;
4751 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4752 if (view == NULL) {
4753 error = got_error_from_errno("view_open");
4754 goto done;
4757 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4758 commit_id, repo);
4759 if (error)
4760 goto done;
4762 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4763 commit_id, repo);
4764 if (error)
4765 goto done;
4766 if (worktree) {
4767 /* Release work tree lock. */
4768 got_worktree_close(worktree);
4769 worktree = NULL;
4771 error = view_loop(view);
4772 done:
4773 free(repo_path);
4774 free(in_repo_path);
4775 free(link_target);
4776 free(cwd);
4777 free(commit_id);
4778 if (worktree)
4779 got_worktree_close(worktree);
4780 if (repo)
4781 got_repo_close(repo);
4782 return error;
4785 static const struct got_error *
4786 draw_tree_entries(struct tog_view *view,
4787 struct got_tree_entry **first_displayed_entry,
4788 struct got_tree_entry **last_displayed_entry,
4789 struct got_tree_entry **selected_entry, int *ndisplayed,
4790 const char *label, int show_ids, const char *parent_path,
4791 struct got_tree_object *tree, int selected, int limit,
4792 int isroot, struct tog_colors *colors, struct got_repository *repo)
4794 const struct got_error *err = NULL;
4795 struct got_tree_entry *te;
4796 wchar_t *wline;
4797 struct tog_color *tc;
4798 int width, n, i, nentries;
4800 *ndisplayed = 0;
4802 werase(view->window);
4804 if (limit == 0)
4805 return NULL;
4807 err = format_line(&wline, &width, label, view->ncols, 0);
4808 if (err)
4809 return err;
4810 if (view_needs_focus_indication(view))
4811 wstandout(view->window);
4812 tc = get_color(colors, TOG_COLOR_COMMIT);
4813 if (tc)
4814 wattr_on(view->window,
4815 COLOR_PAIR(tc->colorpair), NULL);
4816 waddwstr(view->window, wline);
4817 if (tc)
4818 wattr_off(view->window,
4819 COLOR_PAIR(tc->colorpair), NULL);
4820 if (view_needs_focus_indication(view))
4821 wstandend(view->window);
4822 free(wline);
4823 wline = NULL;
4824 if (width < view->ncols - 1)
4825 waddch(view->window, '\n');
4826 if (--limit <= 0)
4827 return NULL;
4828 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4829 if (err)
4830 return err;
4831 waddwstr(view->window, wline);
4832 free(wline);
4833 wline = NULL;
4834 if (width < view->ncols - 1)
4835 waddch(view->window, '\n');
4836 if (--limit <= 0)
4837 return NULL;
4838 waddch(view->window, '\n');
4839 if (--limit <= 0)
4840 return NULL;
4842 if (*first_displayed_entry == NULL) {
4843 te = got_object_tree_get_first_entry(tree);
4844 if (selected == 0) {
4845 if (view->focussed)
4846 wstandout(view->window);
4847 *selected_entry = NULL;
4849 waddstr(view->window, " ..\n"); /* parent directory */
4850 if (selected == 0 && view->focussed)
4851 wstandend(view->window);
4852 (*ndisplayed)++;
4853 if (--limit <= 0)
4854 return NULL;
4855 n = 1;
4856 } else {
4857 n = 0;
4858 te = *first_displayed_entry;
4861 nentries = got_object_tree_get_nentries(tree);
4862 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4863 char *line = NULL, *id_str = NULL, *link_target = NULL;
4864 const char *modestr = "";
4865 mode_t mode;
4867 te = got_object_tree_get_entry(tree, i);
4868 mode = got_tree_entry_get_mode(te);
4870 if (show_ids) {
4871 err = got_object_id_str(&id_str,
4872 got_tree_entry_get_id(te));
4873 if (err)
4874 return got_error_from_errno(
4875 "got_object_id_str");
4877 if (got_object_tree_entry_is_submodule(te))
4878 modestr = "$";
4879 else if (S_ISLNK(mode)) {
4880 int i;
4882 err = got_tree_entry_get_symlink_target(&link_target,
4883 te, repo);
4884 if (err) {
4885 free(id_str);
4886 return err;
4888 for (i = 0; i < strlen(link_target); i++) {
4889 if (!isprint((unsigned char)link_target[i]))
4890 link_target[i] = '?';
4892 modestr = "@";
4894 else if (S_ISDIR(mode))
4895 modestr = "/";
4896 else if (mode & S_IXUSR)
4897 modestr = "*";
4898 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4899 got_tree_entry_get_name(te), modestr,
4900 link_target ? " -> ": "",
4901 link_target ? link_target : "") == -1) {
4902 free(id_str);
4903 free(link_target);
4904 return got_error_from_errno("asprintf");
4906 free(id_str);
4907 free(link_target);
4908 err = format_line(&wline, &width, line, view->ncols, 0);
4909 if (err) {
4910 free(line);
4911 break;
4913 if (n == selected) {
4914 if (view->focussed)
4915 wstandout(view->window);
4916 *selected_entry = te;
4918 tc = match_color(colors, line);
4919 if (tc)
4920 wattr_on(view->window,
4921 COLOR_PAIR(tc->colorpair), NULL);
4922 waddwstr(view->window, wline);
4923 if (tc)
4924 wattr_off(view->window,
4925 COLOR_PAIR(tc->colorpair), NULL);
4926 if (width < view->ncols - 1)
4927 waddch(view->window, '\n');
4928 if (n == selected && view->focussed)
4929 wstandend(view->window);
4930 free(line);
4931 free(wline);
4932 wline = NULL;
4933 n++;
4934 (*ndisplayed)++;
4935 *last_displayed_entry = te;
4936 if (--limit <= 0)
4937 break;
4940 return err;
4943 static void
4944 tree_scroll_up(struct tog_view *view, int maxscroll)
4946 struct tog_tree_view_state *s = &view->state.tree;
4947 struct got_tree_entry *te;
4948 int isroot = s->tree == s->root;
4949 int i = 0;
4951 if (s->first_displayed_entry == NULL)
4952 return;
4954 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4955 while (i++ < maxscroll) {
4956 if (te == NULL) {
4957 if (!isroot)
4958 s->first_displayed_entry = NULL;
4959 break;
4961 s->first_displayed_entry = te;
4962 te = got_tree_entry_get_prev(s->tree, te);
4966 static void
4967 tree_scroll_down(struct tog_view *view, int maxscroll)
4969 struct tog_tree_view_state *s = &view->state.tree;
4970 struct got_tree_entry *next, *last;
4971 int n = 0;
4973 if (s->first_displayed_entry)
4974 next = got_tree_entry_get_next(s->tree,
4975 s->first_displayed_entry);
4976 else
4977 next = got_object_tree_get_first_entry(s->tree);
4979 last = s->last_displayed_entry;
4980 while (next && last && n++ < maxscroll) {
4981 last = got_tree_entry_get_next(s->tree, last);
4982 if (last) {
4983 s->first_displayed_entry = next;
4984 next = got_tree_entry_get_next(s->tree, next);
4989 static const struct got_error *
4990 tree_entry_path(char **path, struct tog_parent_trees *parents,
4991 struct got_tree_entry *te)
4993 const struct got_error *err = NULL;
4994 struct tog_parent_tree *pt;
4995 size_t len = 2; /* for leading slash and NUL */
4997 TAILQ_FOREACH(pt, parents, entry)
4998 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4999 + 1 /* slash */;
5000 if (te)
5001 len += strlen(got_tree_entry_get_name(te));
5003 *path = calloc(1, len);
5004 if (path == NULL)
5005 return got_error_from_errno("calloc");
5007 (*path)[0] = '/';
5008 pt = TAILQ_LAST(parents, tog_parent_trees);
5009 while (pt) {
5010 const char *name = got_tree_entry_get_name(pt->selected_entry);
5011 if (strlcat(*path, name, len) >= len) {
5012 err = got_error(GOT_ERR_NO_SPACE);
5013 goto done;
5015 if (strlcat(*path, "/", len) >= len) {
5016 err = got_error(GOT_ERR_NO_SPACE);
5017 goto done;
5019 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5021 if (te) {
5022 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5023 err = got_error(GOT_ERR_NO_SPACE);
5024 goto done;
5027 done:
5028 if (err) {
5029 free(*path);
5030 *path = NULL;
5032 return err;
5035 static const struct got_error *
5036 blame_tree_entry(struct tog_view **new_view, int begin_x,
5037 struct got_tree_entry *te, struct tog_parent_trees *parents,
5038 struct got_object_id *commit_id, struct got_repository *repo)
5040 const struct got_error *err = NULL;
5041 char *path;
5042 struct tog_view *blame_view;
5044 *new_view = NULL;
5046 err = tree_entry_path(&path, parents, te);
5047 if (err)
5048 return err;
5050 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5051 if (blame_view == NULL) {
5052 err = got_error_from_errno("view_open");
5053 goto done;
5056 err = open_blame_view(blame_view, path, commit_id, repo);
5057 if (err) {
5058 if (err->code == GOT_ERR_CANCELLED)
5059 err = NULL;
5060 view_close(blame_view);
5061 } else
5062 *new_view = blame_view;
5063 done:
5064 free(path);
5065 return err;
5068 static const struct got_error *
5069 log_tree_entry(struct tog_view **new_view, int begin_x,
5070 struct got_tree_entry *te, struct tog_parent_trees *parents,
5071 struct got_object_id *commit_id, struct got_repository *repo)
5073 struct tog_view *log_view;
5074 const struct got_error *err = NULL;
5075 char *path;
5077 *new_view = NULL;
5079 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5080 if (log_view == NULL)
5081 return got_error_from_errno("view_open");
5083 err = tree_entry_path(&path, parents, te);
5084 if (err)
5085 return err;
5087 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5088 if (err)
5089 view_close(log_view);
5090 else
5091 *new_view = log_view;
5092 free(path);
5093 return err;
5096 static const struct got_error *
5097 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5098 struct got_object_id *commit_id, struct got_repository *repo)
5100 const struct got_error *err = NULL;
5101 char *commit_id_str = NULL;
5102 struct tog_tree_view_state *s = &view->state.tree;
5104 TAILQ_INIT(&s->parents);
5106 err = got_object_id_str(&commit_id_str, commit_id);
5107 if (err != NULL)
5108 goto done;
5110 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5111 err = got_error_from_errno("asprintf");
5112 goto done;
5115 s->root = s->tree = root;
5116 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5117 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5118 s->commit_id = got_object_id_dup(commit_id);
5119 if (s->commit_id == NULL) {
5120 err = got_error_from_errno("got_object_id_dup");
5121 goto done;
5123 s->repo = repo;
5125 SIMPLEQ_INIT(&s->colors);
5127 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5128 err = add_color(&s->colors, "\\$$",
5129 TOG_COLOR_TREE_SUBMODULE,
5130 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5131 if (err)
5132 goto done;
5133 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5134 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5135 if (err) {
5136 free_colors(&s->colors);
5137 goto done;
5139 err = add_color(&s->colors, "/$",
5140 TOG_COLOR_TREE_DIRECTORY,
5141 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5142 if (err) {
5143 free_colors(&s->colors);
5144 goto done;
5147 err = add_color(&s->colors, "\\*$",
5148 TOG_COLOR_TREE_EXECUTABLE,
5149 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5150 if (err) {
5151 free_colors(&s->colors);
5152 goto done;
5155 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5156 get_color_value("TOG_COLOR_COMMIT"));
5157 if (err) {
5158 free_colors(&s->colors);
5159 goto done;
5163 view->show = show_tree_view;
5164 view->input = input_tree_view;
5165 view->close = close_tree_view;
5166 view->search_start = search_start_tree_view;
5167 view->search_next = search_next_tree_view;
5168 done:
5169 free(commit_id_str);
5170 if (err) {
5171 free(s->tree_label);
5172 s->tree_label = NULL;
5174 return err;
5177 static const struct got_error *
5178 close_tree_view(struct tog_view *view)
5180 struct tog_tree_view_state *s = &view->state.tree;
5182 free_colors(&s->colors);
5183 free(s->tree_label);
5184 s->tree_label = NULL;
5185 free(s->commit_id);
5186 s->commit_id = NULL;
5187 while (!TAILQ_EMPTY(&s->parents)) {
5188 struct tog_parent_tree *parent;
5189 parent = TAILQ_FIRST(&s->parents);
5190 TAILQ_REMOVE(&s->parents, parent, entry);
5191 free(parent);
5194 if (s->tree != s->root)
5195 got_object_tree_close(s->tree);
5196 got_object_tree_close(s->root);
5197 return NULL;
5200 static const struct got_error *
5201 search_start_tree_view(struct tog_view *view)
5203 struct tog_tree_view_state *s = &view->state.tree;
5205 s->matched_entry = NULL;
5206 return NULL;
5209 static int
5210 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5212 regmatch_t regmatch;
5214 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5215 0) == 0;
5218 static const struct got_error *
5219 search_next_tree_view(struct tog_view *view)
5221 struct tog_tree_view_state *s = &view->state.tree;
5222 struct got_tree_entry *te = NULL;
5224 if (!view->searching) {
5225 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5226 return NULL;
5229 if (s->matched_entry) {
5230 if (view->searching == TOG_SEARCH_FORWARD) {
5231 if (s->selected_entry)
5232 te = got_tree_entry_get_next(s->tree,
5233 s->selected_entry);
5234 else
5235 te = got_object_tree_get_first_entry(s->tree);
5236 } else {
5237 if (s->selected_entry == NULL)
5238 te = got_object_tree_get_last_entry(s->tree);
5239 else
5240 te = got_tree_entry_get_prev(s->tree,
5241 s->selected_entry);
5243 } else {
5244 if (view->searching == TOG_SEARCH_FORWARD)
5245 te = got_object_tree_get_first_entry(s->tree);
5246 else
5247 te = got_object_tree_get_last_entry(s->tree);
5250 while (1) {
5251 if (te == NULL) {
5252 if (s->matched_entry == NULL) {
5253 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5254 return NULL;
5256 if (view->searching == TOG_SEARCH_FORWARD)
5257 te = got_object_tree_get_first_entry(s->tree);
5258 else
5259 te = got_object_tree_get_last_entry(s->tree);
5262 if (match_tree_entry(te, &view->regex)) {
5263 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5264 s->matched_entry = te;
5265 break;
5268 if (view->searching == TOG_SEARCH_FORWARD)
5269 te = got_tree_entry_get_next(s->tree, te);
5270 else
5271 te = got_tree_entry_get_prev(s->tree, te);
5274 if (s->matched_entry) {
5275 s->first_displayed_entry = s->matched_entry;
5276 s->selected = 0;
5279 return NULL;
5282 static const struct got_error *
5283 show_tree_view(struct tog_view *view)
5285 const struct got_error *err = NULL;
5286 struct tog_tree_view_state *s = &view->state.tree;
5287 char *parent_path;
5289 err = tree_entry_path(&parent_path, &s->parents, NULL);
5290 if (err)
5291 return err;
5293 err = draw_tree_entries(view, &s->first_displayed_entry,
5294 &s->last_displayed_entry, &s->selected_entry,
5295 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5296 s->tree, s->selected, view->nlines, s->tree == s->root,
5297 &s->colors, s->repo);
5298 free(parent_path);
5300 view_vborder(view);
5301 return err;
5304 static const struct got_error *
5305 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5306 struct tog_view **focus_view, struct tog_view *view, int ch)
5308 const struct got_error *err = NULL;
5309 struct tog_tree_view_state *s = &view->state.tree;
5310 struct tog_view *log_view, *ref_view;
5311 int begin_x = 0;
5313 switch (ch) {
5314 case 'i':
5315 s->show_ids = !s->show_ids;
5316 break;
5317 case 'l':
5318 if (!s->selected_entry)
5319 break;
5320 if (view_is_parent_view(view))
5321 begin_x = view_split_begin_x(view->begin_x);
5322 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5323 &s->parents, s->commit_id, s->repo);
5324 if (view_is_parent_view(view)) {
5325 err = view_close_child(view);
5326 if (err)
5327 return err;
5328 err = view_set_child(view, log_view);
5329 if (err) {
5330 view_close(log_view);
5331 break;
5333 *focus_view = log_view;
5334 view->child_focussed = 1;
5335 } else
5336 *new_view = log_view;
5337 break;
5338 case 'r':
5339 if (view_is_parent_view(view))
5340 begin_x = view_split_begin_x(view->begin_x);
5341 ref_view = view_open(view->nlines, view->ncols,
5342 view->begin_y, begin_x, TOG_VIEW_REF);
5343 if (ref_view == NULL)
5344 return got_error_from_errno("view_open");
5345 err = open_ref_view(ref_view, s->repo);
5346 if (err) {
5347 view_close(ref_view);
5348 return err;
5350 if (view_is_parent_view(view)) {
5351 err = view_close_child(view);
5352 if (err)
5353 return err;
5354 err = view_set_child(view, ref_view);
5355 if (err) {
5356 view_close(ref_view);
5357 break;
5359 *focus_view = ref_view;
5360 view->child_focussed = 1;
5361 } else
5362 *new_view = ref_view;
5363 break;
5364 case 'k':
5365 case KEY_UP:
5366 if (s->selected > 0) {
5367 s->selected--;
5368 break;
5370 tree_scroll_up(view, 1);
5371 break;
5372 case KEY_PPAGE:
5373 case CTRL('b'):
5374 if (s->tree == s->root) {
5375 if (got_object_tree_get_first_entry(s->tree) ==
5376 s->first_displayed_entry)
5377 s->selected = 0;
5378 } else {
5379 if (s->first_displayed_entry == NULL)
5380 s->selected = 0;
5382 tree_scroll_up(view, MAX(0, view->nlines - 3));
5383 break;
5384 case 'j':
5385 case KEY_DOWN:
5386 if (s->selected < s->ndisplayed - 1) {
5387 s->selected++;
5388 break;
5390 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5391 == NULL)
5392 /* can't scroll any further */
5393 break;
5394 tree_scroll_down(view, 1);
5395 break;
5396 case KEY_NPAGE:
5397 case CTRL('f'):
5398 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5399 == NULL) {
5400 /* can't scroll any further; move cursor down */
5401 if (s->selected < s->ndisplayed - 1)
5402 s->selected = s->ndisplayed - 1;
5403 break;
5405 tree_scroll_down(view, view->nlines - 3);
5406 break;
5407 case KEY_ENTER:
5408 case '\r':
5409 case KEY_BACKSPACE:
5410 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5411 struct tog_parent_tree *parent;
5412 /* user selected '..' */
5413 if (s->tree == s->root)
5414 break;
5415 parent = TAILQ_FIRST(&s->parents);
5416 TAILQ_REMOVE(&s->parents, parent,
5417 entry);
5418 got_object_tree_close(s->tree);
5419 s->tree = parent->tree;
5420 s->first_displayed_entry =
5421 parent->first_displayed_entry;
5422 s->selected_entry =
5423 parent->selected_entry;
5424 s->selected = parent->selected;
5425 free(parent);
5426 } else if (S_ISDIR(got_tree_entry_get_mode(
5427 s->selected_entry))) {
5428 struct got_tree_object *subtree;
5429 err = got_object_open_as_tree(&subtree, s->repo,
5430 got_tree_entry_get_id(s->selected_entry));
5431 if (err)
5432 break;
5433 err = tree_view_visit_subtree(subtree, s);
5434 if (err) {
5435 got_object_tree_close(subtree);
5436 break;
5438 } else if (S_ISREG(got_tree_entry_get_mode(
5439 s->selected_entry))) {
5440 struct tog_view *blame_view;
5441 int begin_x = view_is_parent_view(view) ?
5442 view_split_begin_x(view->begin_x) : 0;
5444 err = blame_tree_entry(&blame_view, begin_x,
5445 s->selected_entry, &s->parents,
5446 s->commit_id, s->repo);
5447 if (err)
5448 break;
5449 if (view_is_parent_view(view)) {
5450 err = view_close_child(view);
5451 if (err)
5452 return err;
5453 err = view_set_child(view, blame_view);
5454 if (err) {
5455 view_close(blame_view);
5456 break;
5458 *focus_view = blame_view;
5459 view->child_focussed = 1;
5460 } else
5461 *new_view = blame_view;
5463 break;
5464 case KEY_RESIZE:
5465 if (s->selected > view->nlines)
5466 s->selected = s->ndisplayed - 1;
5467 break;
5468 default:
5469 break;
5472 return err;
5475 __dead static void
5476 usage_tree(void)
5478 endwin();
5479 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5480 getprogname());
5481 exit(1);
5484 static const struct got_error *
5485 cmd_tree(int argc, char *argv[])
5487 const struct got_error *error;
5488 struct got_repository *repo = NULL;
5489 struct got_worktree *worktree = NULL;
5490 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5491 struct got_object_id *commit_id = NULL;
5492 char *commit_id_arg = NULL;
5493 struct got_commit_object *commit = NULL;
5494 struct got_tree_object *tree = NULL;
5495 int ch;
5496 struct tog_view *view;
5498 #ifndef PROFILE
5499 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5500 NULL) == -1)
5501 err(1, "pledge");
5502 #endif
5504 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5505 switch (ch) {
5506 case 'c':
5507 commit_id_arg = optarg;
5508 break;
5509 case 'r':
5510 repo_path = realpath(optarg, NULL);
5511 if (repo_path == NULL)
5512 return got_error_from_errno2("realpath",
5513 optarg);
5514 break;
5515 default:
5516 usage_tree();
5517 /* NOTREACHED */
5521 argc -= optind;
5522 argv += optind;
5524 if (argc > 1)
5525 usage_tree();
5527 cwd = getcwd(NULL, 0);
5528 if (cwd == NULL)
5529 return got_error_from_errno("getcwd");
5531 error = got_worktree_open(&worktree, cwd);
5532 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5533 goto done;
5535 if (repo_path == NULL) {
5536 if (worktree)
5537 repo_path =
5538 strdup(got_worktree_get_repo_path(worktree));
5539 else
5540 repo_path = strdup(cwd);
5542 if (repo_path == NULL) {
5543 error = got_error_from_errno("strdup");
5544 goto done;
5547 error = got_repo_open(&repo, repo_path, NULL);
5548 if (error != NULL)
5549 goto done;
5551 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5552 repo, worktree);
5553 if (error)
5554 goto done;
5556 init_curses();
5558 error = apply_unveil(got_repo_get_path(repo), NULL);
5559 if (error)
5560 goto done;
5562 error = got_repo_match_object_id(&commit_id, NULL,
5563 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5564 GOT_OBJ_TYPE_COMMIT, 1, repo);
5565 if (error)
5566 goto done;
5568 error = got_object_open_as_commit(&commit, repo, commit_id);
5569 if (error)
5570 goto done;
5572 error = got_object_open_as_tree(&tree, repo,
5573 got_object_commit_get_tree_id(commit));
5574 if (error)
5575 goto done;
5577 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5578 if (view == NULL) {
5579 error = got_error_from_errno("view_open");
5580 goto done;
5582 error = open_tree_view(view, tree, commit_id, repo);
5583 if (error)
5584 goto done;
5585 if (!got_path_is_root_dir(in_repo_path)) {
5586 error = tree_view_walk_path(&view->state.tree, commit_id,
5587 in_repo_path, repo);
5588 if (error)
5589 goto done;
5592 if (worktree) {
5593 /* Release work tree lock. */
5594 got_worktree_close(worktree);
5595 worktree = NULL;
5597 error = view_loop(view);
5598 done:
5599 free(repo_path);
5600 free(cwd);
5601 free(commit_id);
5602 if (commit)
5603 got_object_commit_close(commit);
5604 if (tree)
5605 got_object_tree_close(tree);
5606 if (repo)
5607 got_repo_close(repo);
5608 return error;
5611 static const struct got_error *
5612 ref_view_load_refs(struct tog_ref_view_state *s)
5614 const struct got_error *err;
5615 struct got_reflist_entry *sre;
5616 struct tog_reflist_entry *re;
5618 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5619 got_ref_cmp_by_name, NULL);
5620 if (err)
5621 return err;
5623 s->nrefs = 0;
5624 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5625 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5626 continue;
5628 re = malloc(sizeof(*re));
5629 if (re == NULL)
5630 return got_error_from_errno("malloc");
5632 re->ref = sre->ref;
5633 re->idx = s->nrefs++;
5634 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5637 return NULL;
5640 void
5641 ref_view_free_refs(struct tog_ref_view_state *s)
5643 struct tog_reflist_entry *re;
5645 while (!TAILQ_EMPTY(&s->refs)) {
5646 re = TAILQ_FIRST(&s->refs);
5647 TAILQ_REMOVE(&s->refs, re, entry);
5648 free(re);
5650 got_ref_list_free(&s->simplerefs);
5653 static const struct got_error *
5654 open_ref_view(struct tog_view *view, struct got_repository *repo)
5656 const struct got_error *err = NULL;
5657 struct tog_ref_view_state *s = &view->state.ref;
5659 s->selected_entry = 0;
5660 s->repo = repo;
5662 SIMPLEQ_INIT(&s->simplerefs);
5663 TAILQ_INIT(&s->refs);
5664 SIMPLEQ_INIT(&s->colors);
5666 err = ref_view_load_refs(s);
5667 if (err)
5668 return err;
5670 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5672 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5673 err = add_color(&s->colors, "^refs/heads/",
5674 TOG_COLOR_REFS_HEADS,
5675 get_color_value("TOG_COLOR_REFS_HEADS"));
5676 if (err)
5677 goto done;
5679 err = add_color(&s->colors, "^refs/tags/",
5680 TOG_COLOR_REFS_TAGS,
5681 get_color_value("TOG_COLOR_REFS_TAGS"));
5682 if (err)
5683 goto done;
5685 err = add_color(&s->colors, "^refs/remotes/",
5686 TOG_COLOR_REFS_REMOTES,
5687 get_color_value("TOG_COLOR_REFS_REMOTES"));
5688 if (err)
5689 goto done;
5692 view->show = show_ref_view;
5693 view->input = input_ref_view;
5694 view->close = close_ref_view;
5695 view->search_start = search_start_ref_view;
5696 view->search_next = search_next_ref_view;
5697 done:
5698 if (err)
5699 free_colors(&s->colors);
5700 return err;
5703 static const struct got_error *
5704 close_ref_view(struct tog_view *view)
5706 struct tog_ref_view_state *s = &view->state.ref;
5708 ref_view_free_refs(s);
5709 free_colors(&s->colors);
5711 return NULL;
5714 static const struct got_error *
5715 resolve_reflist_entry(struct got_object_id **commit_id,
5716 struct tog_reflist_entry *re, struct got_repository *repo)
5718 const struct got_error *err = NULL;
5719 struct got_object_id *obj_id;
5720 struct got_tag_object *tag = NULL;
5721 int obj_type;
5723 *commit_id = NULL;
5725 err = got_ref_resolve(&obj_id, repo, re->ref);
5726 if (err)
5727 return err;
5729 err = got_object_get_type(&obj_type, repo, obj_id);
5730 if (err)
5731 goto done;
5733 switch (obj_type) {
5734 case GOT_OBJ_TYPE_COMMIT:
5735 *commit_id = obj_id;
5736 break;
5737 case GOT_OBJ_TYPE_TAG:
5738 err = got_object_open_as_tag(&tag, repo, obj_id);
5739 if (err)
5740 goto done;
5741 free(obj_id);
5742 err = got_object_get_type(&obj_type, repo,
5743 got_object_tag_get_object_id(tag));
5744 if (err)
5745 goto done;
5746 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5747 err = got_error(GOT_ERR_OBJ_TYPE);
5748 goto done;
5750 *commit_id = got_object_id_dup(
5751 got_object_tag_get_object_id(tag));
5752 if (*commit_id == NULL) {
5753 err = got_error_from_errno("got_object_id_dup");
5754 goto done;
5756 break;
5757 default:
5758 err = got_error(GOT_ERR_OBJ_TYPE);
5759 break;
5762 done:
5763 if (tag)
5764 got_object_tag_close(tag);
5765 if (err) {
5766 free(*commit_id);
5767 *commit_id = NULL;
5769 return err;
5772 static const struct got_error *
5773 log_ref_entry(struct tog_view **new_view, int begin_x,
5774 struct tog_reflist_entry *re, struct got_repository *repo)
5776 struct tog_view *log_view;
5777 const struct got_error *err = NULL;
5778 struct got_object_id *commit_id = NULL;
5780 *new_view = NULL;
5782 err = resolve_reflist_entry(&commit_id, re, repo);
5783 if (err) {
5784 if (err->code != GOT_ERR_OBJ_TYPE)
5785 return err;
5786 else
5787 return NULL;
5790 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5791 if (log_view == NULL) {
5792 err = got_error_from_errno("view_open");
5793 goto done;
5796 err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5797 done:
5798 if (err)
5799 view_close(log_view);
5800 else
5801 *new_view = log_view;
5802 free(commit_id);
5803 return err;
5806 static void
5807 ref_scroll_up(struct tog_view *view, int maxscroll)
5809 struct tog_ref_view_state *s = &view->state.ref;
5810 struct tog_reflist_entry *re;
5811 int i = 0;
5813 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5814 return;
5816 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5817 while (i++ < maxscroll) {
5818 if (re == NULL)
5819 break;
5820 s->first_displayed_entry = re;
5821 re = TAILQ_PREV(re, tog_reflist_head, entry);
5825 static void
5826 ref_scroll_down(struct tog_view *view, int maxscroll)
5828 struct tog_ref_view_state *s = &view->state.ref;
5829 struct tog_reflist_entry *next, *last;
5830 int n = 0;
5832 if (s->first_displayed_entry)
5833 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5834 else
5835 next = TAILQ_FIRST(&s->refs);
5837 last = s->last_displayed_entry;
5838 while (next && last && n++ < maxscroll) {
5839 last = TAILQ_NEXT(last, entry);
5840 if (last) {
5841 s->first_displayed_entry = next;
5842 next = TAILQ_NEXT(next, entry);
5847 static const struct got_error *
5848 search_start_ref_view(struct tog_view *view)
5850 struct tog_ref_view_state *s = &view->state.ref;
5852 s->matched_entry = NULL;
5853 return NULL;
5856 static int
5857 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5859 regmatch_t regmatch;
5861 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5862 0) == 0;
5865 static const struct got_error *
5866 search_next_ref_view(struct tog_view *view)
5868 struct tog_ref_view_state *s = &view->state.ref;
5869 struct tog_reflist_entry *re = NULL;
5871 if (!view->searching) {
5872 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5873 return NULL;
5876 if (s->matched_entry) {
5877 if (view->searching == TOG_SEARCH_FORWARD) {
5878 if (s->selected_entry)
5879 re = TAILQ_NEXT(s->selected_entry, entry);
5880 else
5881 re = TAILQ_PREV(s->selected_entry,
5882 tog_reflist_head, entry);
5883 } else {
5884 if (s->selected_entry == NULL)
5885 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5886 else
5887 re = TAILQ_PREV(s->selected_entry,
5888 tog_reflist_head, entry);
5890 } else {
5891 if (view->searching == TOG_SEARCH_FORWARD)
5892 re = TAILQ_FIRST(&s->refs);
5893 else
5894 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5897 while (1) {
5898 if (re == NULL) {
5899 if (s->matched_entry == NULL) {
5900 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5901 return NULL;
5903 if (view->searching == TOG_SEARCH_FORWARD)
5904 re = TAILQ_FIRST(&s->refs);
5905 else
5906 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5909 if (match_reflist_entry(re, &view->regex)) {
5910 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5911 s->matched_entry = re;
5912 break;
5915 if (view->searching == TOG_SEARCH_FORWARD)
5916 re = TAILQ_NEXT(re, entry);
5917 else
5918 re = TAILQ_PREV(re, tog_reflist_head, entry);
5921 if (s->matched_entry) {
5922 s->first_displayed_entry = s->matched_entry;
5923 s->selected = 0;
5926 return NULL;
5929 static const struct got_error *
5930 show_ref_view(struct tog_view *view)
5932 const struct got_error *err = NULL;
5933 struct tog_ref_view_state *s = &view->state.ref;
5934 struct tog_reflist_entry *re;
5935 char *line = NULL;
5936 wchar_t *wline;
5937 struct tog_color *tc;
5938 int width, n;
5939 int limit = view->nlines;
5941 werase(view->window);
5943 s->ndisplayed = 0;
5945 if (limit == 0)
5946 return NULL;
5948 re = s->first_displayed_entry;
5950 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5951 s->nrefs) == -1)
5952 return got_error_from_errno("asprintf");
5954 err = format_line(&wline, &width, line, view->ncols, 0);
5955 if (err) {
5956 free(line);
5957 return err;
5959 if (view_needs_focus_indication(view))
5960 wstandout(view->window);
5961 waddwstr(view->window, wline);
5962 if (view_needs_focus_indication(view))
5963 wstandend(view->window);
5964 free(wline);
5965 wline = NULL;
5966 free(line);
5967 line = NULL;
5968 if (width < view->ncols - 1)
5969 waddch(view->window, '\n');
5970 if (--limit <= 0)
5971 return NULL;
5973 n = 0;
5974 while (re && limit > 0) {
5975 char *line = NULL;
5977 if (got_ref_is_symbolic(re->ref)) {
5978 if (asprintf(&line, "%s -> %s",
5979 got_ref_get_name(re->ref),
5980 got_ref_get_symref_target(re->ref)) == -1)
5981 return got_error_from_errno("asprintf");
5982 } else if (s->show_ids) {
5983 struct got_object_id *id;
5984 char *id_str;
5985 err = got_ref_resolve(&id, s->repo, re->ref);
5986 if (err)
5987 return err;
5988 err = got_object_id_str(&id_str, id);
5989 if (err) {
5990 free(id);
5991 return err;
5993 if (asprintf(&line, "%s: %s",
5994 got_ref_get_name(re->ref), id_str) == -1) {
5995 err = got_error_from_errno("asprintf");
5996 free(id);
5997 free(id_str);
5998 return err;
6000 free(id);
6001 free(id_str);
6002 } else {
6003 line = strdup(got_ref_get_name(re->ref));
6004 if (line == NULL)
6005 return got_error_from_errno("strdup");
6008 err = format_line(&wline, &width, line, view->ncols, 0);
6009 if (err) {
6010 free(line);
6011 return err;
6013 if (n == s->selected) {
6014 if (view->focussed)
6015 wstandout(view->window);
6016 s->selected_entry = re;
6018 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6019 if (tc)
6020 wattr_on(view->window,
6021 COLOR_PAIR(tc->colorpair), NULL);
6022 waddwstr(view->window, wline);
6023 if (tc)
6024 wattr_off(view->window,
6025 COLOR_PAIR(tc->colorpair), NULL);
6026 if (width < view->ncols - 1)
6027 waddch(view->window, '\n');
6028 if (n == s->selected && view->focussed)
6029 wstandend(view->window);
6030 free(line);
6031 free(wline);
6032 wline = NULL;
6033 n++;
6034 s->ndisplayed++;
6035 s->last_displayed_entry = re;
6037 limit--;
6038 re = TAILQ_NEXT(re, entry);
6041 view_vborder(view);
6042 return err;
6045 static const struct got_error *
6046 browse_ref_tree(struct tog_view **new_view, int begin_x,
6047 struct tog_reflist_entry *re, struct got_repository *repo)
6049 const struct got_error *err = NULL;
6050 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6051 struct got_tree_object *tree = NULL;
6052 struct tog_view *tree_view;
6054 *new_view = NULL;
6056 err = resolve_reflist_entry(&commit_id, re, repo);
6057 if (err) {
6058 if (err->code != GOT_ERR_OBJ_TYPE)
6059 return err;
6060 else
6061 return NULL;
6064 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6065 if (err)
6066 goto done;
6068 err = got_object_open_as_tree(&tree, repo, tree_id);
6069 if (err)
6070 goto done;
6072 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6073 if (tree_view == NULL) {
6074 err = got_error_from_errno("view_open");
6075 goto done;
6078 err = open_tree_view(tree_view, tree, commit_id, repo);
6079 if (err)
6080 goto done;
6082 *new_view = tree_view;
6083 done:
6084 free(commit_id);
6085 free(tree_id);
6086 if (err) {
6087 if (tree)
6088 got_object_tree_close(tree);
6090 return err;
6092 static const struct got_error *
6093 input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6094 struct tog_view **focus_view, struct tog_view *view, int ch)
6096 const struct got_error *err = NULL;
6097 struct tog_ref_view_state *s = &view->state.ref;
6098 struct tog_view *log_view, *tree_view;
6099 int begin_x = 0;
6101 switch (ch) {
6102 case 'i':
6103 s->show_ids = !s->show_ids;
6104 break;
6105 case KEY_ENTER:
6106 case '\r':
6107 if (!s->selected_entry)
6108 break;
6109 if (view_is_parent_view(view))
6110 begin_x = view_split_begin_x(view->begin_x);
6111 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6112 s->repo);
6113 if (view_is_parent_view(view)) {
6114 err = view_close_child(view);
6115 if (err)
6116 return err;
6117 err = view_set_child(view, log_view);
6118 if (err) {
6119 view_close(log_view);
6120 break;
6122 *focus_view = log_view;
6123 view->child_focussed = 1;
6124 } else
6125 *new_view = log_view;
6126 break;
6127 case 't':
6128 if (!s->selected_entry)
6129 break;
6130 if (view_is_parent_view(view))
6131 begin_x = view_split_begin_x(view->begin_x);
6132 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6133 s->repo);
6134 if (err || tree_view == NULL)
6135 break;
6136 if (view_is_parent_view(view)) {
6137 err = view_close_child(view);
6138 if (err)
6139 return err;
6140 err = view_set_child(view, tree_view);
6141 if (err) {
6142 view_close(tree_view);
6143 break;
6145 *focus_view = tree_view;
6146 view->child_focussed = 1;
6147 } else
6148 *new_view = tree_view;
6149 break;
6150 case 'k':
6151 case KEY_UP:
6152 if (s->selected > 0) {
6153 s->selected--;
6154 break;
6156 ref_scroll_up(view, 1);
6157 break;
6158 case KEY_PPAGE:
6159 case CTRL('b'):
6160 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6161 s->selected = 0;
6162 ref_scroll_up(view, MAX(0, view->nlines - 1));
6163 break;
6164 case 'j':
6165 case KEY_DOWN:
6166 if (s->selected < s->ndisplayed - 1) {
6167 s->selected++;
6168 break;
6170 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6171 /* can't scroll any further */
6172 break;
6173 ref_scroll_down(view, 1);
6174 break;
6175 case KEY_NPAGE:
6176 case CTRL('f'):
6177 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6178 /* can't scroll any further; move cursor down */
6179 if (s->selected < s->ndisplayed - 1)
6180 s->selected = s->ndisplayed - 1;
6181 break;
6183 ref_scroll_down(view, view->nlines - 1);
6184 break;
6185 case CTRL('l'):
6186 ref_view_free_refs(s);
6187 err = ref_view_load_refs(s);
6188 break;
6189 case KEY_RESIZE:
6190 if (s->selected > view->nlines)
6191 s->selected = s->ndisplayed - 1;
6192 break;
6193 default:
6194 break;
6197 return err;
6200 __dead static void
6201 usage_ref(void)
6203 endwin();
6204 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6205 getprogname());
6206 exit(1);
6209 static const struct got_error *
6210 cmd_ref(int argc, char *argv[])
6212 const struct got_error *error;
6213 struct got_repository *repo = NULL;
6214 struct got_worktree *worktree = NULL;
6215 char *cwd = NULL, *repo_path = NULL;
6216 int ch;
6217 struct tog_view *view;
6219 #ifndef PROFILE
6220 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6221 NULL) == -1)
6222 err(1, "pledge");
6223 #endif
6225 while ((ch = getopt(argc, argv, "r:")) != -1) {
6226 switch (ch) {
6227 case 'r':
6228 repo_path = realpath(optarg, NULL);
6229 if (repo_path == NULL)
6230 return got_error_from_errno2("realpath",
6231 optarg);
6232 break;
6233 default:
6234 usage_ref();
6235 /* NOTREACHED */
6239 argc -= optind;
6240 argv += optind;
6242 if (argc > 1)
6243 usage_ref();
6245 cwd = getcwd(NULL, 0);
6246 if (cwd == NULL)
6247 return got_error_from_errno("getcwd");
6249 error = got_worktree_open(&worktree, cwd);
6250 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6251 goto done;
6253 if (repo_path == NULL) {
6254 if (worktree)
6255 repo_path =
6256 strdup(got_worktree_get_repo_path(worktree));
6257 else
6258 repo_path = strdup(cwd);
6260 if (repo_path == NULL) {
6261 error = got_error_from_errno("strdup");
6262 goto done;
6265 error = got_repo_open(&repo, repo_path, NULL);
6266 if (error != NULL)
6267 goto done;
6269 init_curses();
6271 error = apply_unveil(got_repo_get_path(repo), NULL);
6272 if (error)
6273 goto done;
6275 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6276 if (view == NULL) {
6277 error = got_error_from_errno("view_open");
6278 goto done;
6281 error = open_ref_view(view, repo);
6282 if (error)
6283 goto done;
6285 if (worktree) {
6286 /* Release work tree lock. */
6287 got_worktree_close(worktree);
6288 worktree = NULL;
6290 error = view_loop(view);
6291 done:
6292 free(repo_path);
6293 free(cwd);
6294 if (repo)
6295 got_repo_close(repo);
6296 return error;
6299 static void
6300 list_commands(FILE *fp)
6302 int i;
6304 fprintf(fp, "commands:");
6305 for (i = 0; i < nitems(tog_commands); i++) {
6306 struct tog_cmd *cmd = &tog_commands[i];
6307 fprintf(fp, " %s", cmd->name);
6309 fputc('\n', fp);
6312 __dead static void
6313 usage(int hflag, int status)
6315 FILE *fp = (status == 0) ? stdout : stderr;
6317 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6318 getprogname());
6319 if (hflag) {
6320 fprintf(fp, "lazy usage: %s path\n", getprogname());
6321 list_commands(fp);
6323 exit(status);
6326 static char **
6327 make_argv(int argc, ...)
6329 va_list ap;
6330 char **argv;
6331 int i;
6333 va_start(ap, argc);
6335 argv = calloc(argc, sizeof(char *));
6336 if (argv == NULL)
6337 err(1, "calloc");
6338 for (i = 0; i < argc; i++) {
6339 argv[i] = strdup(va_arg(ap, char *));
6340 if (argv[i] == NULL)
6341 err(1, "strdup");
6344 va_end(ap);
6345 return argv;
6349 * Try to convert 'tog path' into a 'tog log path' command.
6350 * The user could simply have mistyped the command rather than knowingly
6351 * provided a path. So check whether argv[0] can in fact be resolved
6352 * to a path in the HEAD commit and print a special error if not.
6353 * This hack is for mpi@ <3
6355 static const struct got_error *
6356 tog_log_with_path(int argc, char *argv[])
6358 const struct got_error *error = NULL;
6359 struct tog_cmd *cmd = NULL;
6360 struct got_repository *repo = NULL;
6361 struct got_worktree *worktree = NULL;
6362 struct got_object_id *commit_id = NULL, *id = NULL;
6363 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6364 char *commit_id_str = NULL, **cmd_argv = NULL;
6366 cwd = getcwd(NULL, 0);
6367 if (cwd == NULL)
6368 return got_error_from_errno("getcwd");
6370 error = got_worktree_open(&worktree, cwd);
6371 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6372 goto done;
6374 if (worktree)
6375 repo_path = strdup(got_worktree_get_repo_path(worktree));
6376 else
6377 repo_path = strdup(cwd);
6378 if (repo_path == NULL) {
6379 error = got_error_from_errno("strdup");
6380 goto done;
6383 error = got_repo_open(&repo, repo_path, NULL);
6384 if (error != NULL)
6385 goto done;
6387 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6388 repo, worktree);
6389 if (error)
6390 goto done;
6392 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6393 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6394 GOT_OBJ_TYPE_COMMIT, 1, repo);
6395 if (error)
6396 goto done;
6398 if (worktree) {
6399 got_worktree_close(worktree);
6400 worktree = NULL;
6403 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6404 if (error) {
6405 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6406 goto done;
6407 fprintf(stderr, "%s: '%s' is no known command or path\n",
6408 getprogname(), argv[0]);
6409 usage(1, 1);
6410 /* not reached */
6413 got_repo_close(repo);
6414 repo = NULL;
6416 error = got_object_id_str(&commit_id_str, commit_id);
6417 if (error)
6418 goto done;
6420 cmd = &tog_commands[0]; /* log */
6421 argc = 4;
6422 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6423 error = cmd->cmd_main(argc, cmd_argv);
6424 done:
6425 if (repo)
6426 got_repo_close(repo);
6427 if (worktree)
6428 got_worktree_close(worktree);
6429 free(id);
6430 free(commit_id_str);
6431 free(commit_id);
6432 free(cwd);
6433 free(repo_path);
6434 free(in_repo_path);
6435 if (cmd_argv) {
6436 int i;
6437 for (i = 0; i < argc; i++)
6438 free(cmd_argv[i]);
6439 free(cmd_argv);
6441 return error;
6444 int
6445 main(int argc, char *argv[])
6447 const struct got_error *error = NULL;
6448 struct tog_cmd *cmd = NULL;
6449 int ch, hflag = 0, Vflag = 0;
6450 char **cmd_argv = NULL;
6451 static struct option longopts[] = {
6452 { "version", no_argument, NULL, 'V' },
6453 { NULL, 0, NULL, 0}
6456 setlocale(LC_CTYPE, "");
6458 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6459 switch (ch) {
6460 case 'h':
6461 hflag = 1;
6462 break;
6463 case 'V':
6464 Vflag = 1;
6465 break;
6466 default:
6467 usage(hflag, 1);
6468 /* NOTREACHED */
6472 argc -= optind;
6473 argv += optind;
6474 optind = 1;
6475 optreset = 1;
6477 if (Vflag) {
6478 got_version_print_str();
6479 return 0;
6482 if (argc == 0) {
6483 if (hflag)
6484 usage(hflag, 0);
6485 /* Build an argument vector which runs a default command. */
6486 cmd = &tog_commands[0];
6487 argc = 1;
6488 cmd_argv = make_argv(argc, cmd->name);
6489 } else {
6490 int i;
6492 /* Did the user specify a command? */
6493 for (i = 0; i < nitems(tog_commands); i++) {
6494 if (strncmp(tog_commands[i].name, argv[0],
6495 strlen(argv[0])) == 0) {
6496 cmd = &tog_commands[i];
6497 break;
6502 if (cmd == NULL) {
6503 if (argc != 1)
6504 usage(0, 1);
6505 /* No command specified; try log with a path */
6506 error = tog_log_with_path(argc, argv);
6507 } else {
6508 if (hflag)
6509 cmd->cmd_usage();
6510 else
6511 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6514 endwin();
6515 putchar('\n');
6516 if (cmd_argv) {
6517 int i;
6518 for (i = 0; i < argc; i++)
6519 free(cmd_argv[i]);
6520 free(cmd_argv);
6523 if (error && error->code != GOT_ERR_CANCELLED)
6524 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6525 return 0;