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);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
90 static struct tog_cmd tog_commands[] = {
91 { "log", cmd_log, usage_log },
92 { "diff", cmd_diff, usage_diff },
93 { "blame", cmd_blame, usage_blame },
94 { "tree", cmd_tree, usage_tree },
95 };
97 enum tog_view_type {
98 TOG_VIEW_DIFF,
99 TOG_VIEW_LOG,
100 TOG_VIEW_BLAME,
101 TOG_VIEW_TREE
102 };
104 #define TOG_EOF_STRING "(END)"
106 struct commit_queue_entry {
107 TAILQ_ENTRY(commit_queue_entry) entry;
108 struct got_object_id *id;
109 struct got_commit_object *commit;
110 int idx;
111 };
112 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
113 struct commit_queue {
114 int ncommits;
115 struct commit_queue_head head;
116 };
118 struct tog_color {
119 SIMPLEQ_ENTRY(tog_color) entry;
120 regex_t regex;
121 short colorpair;
122 };
123 SIMPLEQ_HEAD(tog_colors, tog_color);
125 static const struct got_error *
126 add_color(struct tog_colors *colors, const char *pattern,
127 int idx, short color)
129 const struct got_error *err = NULL;
130 struct tog_color *tc;
131 int regerr = 0;
133 if (idx < 1 || idx > COLOR_PAIRS - 1)
134 return NULL;
136 init_pair(idx, color, -1);
138 tc = calloc(1, sizeof(*tc));
139 if (tc == NULL)
140 return got_error_from_errno("calloc");
141 regerr = regcomp(&tc->regex, pattern,
142 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
143 if (regerr) {
144 static char regerr_msg[512];
145 static char err_msg[512];
146 regerror(regerr, &tc->regex, regerr_msg,
147 sizeof(regerr_msg));
148 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
149 regerr_msg);
150 err = got_error_msg(GOT_ERR_REGEX, err_msg);
151 free(tc);
152 return err;
154 tc->colorpair = idx;
155 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
156 return NULL;
159 static void
160 free_colors(struct tog_colors *colors)
162 struct tog_color *tc;
164 while (!SIMPLEQ_EMPTY(colors)) {
165 tc = SIMPLEQ_FIRST(colors);
166 SIMPLEQ_REMOVE_HEAD(colors, entry);
167 regfree(&tc->regex);
168 free(tc);
172 struct tog_color *
173 get_color(struct tog_colors *colors, int colorpair)
175 struct tog_color *tc = NULL;
177 SIMPLEQ_FOREACH(tc, colors, entry) {
178 if (tc->colorpair == colorpair)
179 return tc;
182 return NULL;
185 static int
186 default_color_value(const char *envvar)
188 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
189 return COLOR_MAGENTA;
190 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
191 return COLOR_CYAN;
192 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
193 return COLOR_YELLOW;
194 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
195 return COLOR_GREEN;
196 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
197 return COLOR_MAGENTA;
198 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
199 return COLOR_MAGENTA;
200 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
201 return COLOR_CYAN;
202 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
203 return COLOR_GREEN;
204 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
205 return COLOR_GREEN;
206 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
207 return COLOR_CYAN;
208 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
209 return COLOR_YELLOW;
211 return -1;
214 static int
215 get_color_value(const char *envvar)
217 const char *val = getenv(envvar);
219 if (val == NULL)
220 return default_color_value(envvar);
222 if (strcasecmp(val, "black") == 0)
223 return COLOR_BLACK;
224 if (strcasecmp(val, "red") == 0)
225 return COLOR_RED;
226 if (strcasecmp(val, "green") == 0)
227 return COLOR_GREEN;
228 if (strcasecmp(val, "yellow") == 0)
229 return COLOR_YELLOW;
230 if (strcasecmp(val, "blue") == 0)
231 return COLOR_BLUE;
232 if (strcasecmp(val, "magenta") == 0)
233 return COLOR_MAGENTA;
234 if (strcasecmp(val, "cyan") == 0)
235 return COLOR_CYAN;
236 if (strcasecmp(val, "white") == 0)
237 return COLOR_WHITE;
238 if (strcasecmp(val, "default") == 0)
239 return -1;
241 return default_color_value(envvar);
245 struct tog_diff_view_state {
246 struct got_object_id *id1, *id2;
247 FILE *f;
248 int first_displayed_line;
249 int last_displayed_line;
250 int eof;
251 int diff_context;
252 int force_text_diff;
253 struct got_repository *repo;
254 struct got_reflist_head *refs;
255 struct tog_colors colors;
256 size_t nlines;
257 off_t *line_offsets;
258 int matched_line;
259 int selected_line;
261 /* passed from log view; may be NULL */
262 struct tog_view *log_view;
263 };
265 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
267 struct tog_log_thread_args {
268 pthread_cond_t need_commits;
269 pthread_cond_t commit_loaded;
270 int commits_needed;
271 struct got_commit_graph *graph;
272 struct commit_queue *commits;
273 const char *in_repo_path;
274 struct got_object_id *start_id;
275 struct got_repository *repo;
276 int log_complete;
277 sig_atomic_t *quit;
278 struct commit_queue_entry **first_displayed_entry;
279 struct commit_queue_entry **selected_entry;
280 int *searching;
281 int *search_next_done;
282 regex_t *regex;
283 };
285 struct tog_log_view_state {
286 struct commit_queue commits;
287 struct commit_queue_entry *first_displayed_entry;
288 struct commit_queue_entry *last_displayed_entry;
289 struct commit_queue_entry *selected_entry;
290 int selected;
291 char *in_repo_path;
292 const char *head_ref_name;
293 int log_branches;
294 struct got_repository *repo;
295 struct got_reflist_head *refs;
296 struct got_object_id *start_id;
297 sig_atomic_t quit;
298 pthread_t thread;
299 struct tog_log_thread_args thread_args;
300 struct commit_queue_entry *matched_entry;
301 struct commit_queue_entry *search_entry;
302 struct tog_colors colors;
303 };
305 #define TOG_COLOR_DIFF_MINUS 1
306 #define TOG_COLOR_DIFF_PLUS 2
307 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
308 #define TOG_COLOR_DIFF_META 4
309 #define TOG_COLOR_TREE_SUBMODULE 5
310 #define TOG_COLOR_TREE_SYMLINK 6
311 #define TOG_COLOR_TREE_DIRECTORY 7
312 #define TOG_COLOR_TREE_EXECUTABLE 8
313 #define TOG_COLOR_COMMIT 9
314 #define TOG_COLOR_AUTHOR 10
315 #define TOG_COLOR_DATE 11
317 struct tog_blame_cb_args {
318 struct tog_blame_line *lines; /* one per line */
319 int nlines;
321 struct tog_view *view;
322 struct got_object_id *commit_id;
323 int *quit;
324 };
326 struct tog_blame_thread_args {
327 const char *path;
328 struct got_repository *repo;
329 struct tog_blame_cb_args *cb_args;
330 int *complete;
331 got_cancel_cb cancel_cb;
332 void *cancel_arg;
333 };
335 struct tog_blame {
336 FILE *f;
337 off_t filesize;
338 struct tog_blame_line *lines;
339 int nlines;
340 off_t *line_offsets;
341 pthread_t thread;
342 struct tog_blame_thread_args thread_args;
343 struct tog_blame_cb_args cb_args;
344 const char *path;
345 };
347 struct tog_blame_view_state {
348 int first_displayed_line;
349 int last_displayed_line;
350 int selected_line;
351 int blame_complete;
352 int eof;
353 int done;
354 struct got_object_id_queue blamed_commits;
355 struct got_object_qid *blamed_commit;
356 char *path;
357 struct got_repository *repo;
358 struct got_reflist_head *refs;
359 struct got_object_id *commit_id;
360 struct tog_blame blame;
361 int matched_line;
362 struct tog_colors colors;
363 };
365 struct tog_parent_tree {
366 TAILQ_ENTRY(tog_parent_tree) entry;
367 struct got_tree_object *tree;
368 struct got_tree_entry *first_displayed_entry;
369 struct got_tree_entry *selected_entry;
370 int selected;
371 };
373 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
375 struct tog_tree_view_state {
376 char *tree_label;
377 struct got_tree_object *root;
378 struct got_tree_object *tree;
379 struct got_tree_entry *first_displayed_entry;
380 struct got_tree_entry *last_displayed_entry;
381 struct got_tree_entry *selected_entry;
382 int ndisplayed, selected, show_ids;
383 struct tog_parent_trees parents;
384 struct got_object_id *commit_id;
385 struct got_repository *repo;
386 struct got_reflist_head *refs;
387 struct got_tree_entry *matched_entry;
388 struct tog_colors colors;
389 };
391 /*
392 * We implement two types of views: parent views and child views.
394 * The 'Tab' key switches between a parent view and its child view.
395 * Child views are shown side-by-side to their parent view, provided
396 * there is enough screen estate.
398 * When a new view is opened from within a parent view, this new view
399 * becomes a child view of the parent view, replacing any existing child.
401 * When a new view is opened from within a child view, this new view
402 * becomes a parent view which will obscure the views below until the
403 * user quits the new parent view by typing 'q'.
405 * This list of views contains parent views only.
406 * Child views are only pointed to by their parent view.
407 */
408 TAILQ_HEAD(tog_view_list_head, tog_view);
410 struct tog_view {
411 TAILQ_ENTRY(tog_view) entry;
412 WINDOW *window;
413 PANEL *panel;
414 int nlines, ncols, begin_y, begin_x;
415 int lines, cols; /* copies of LINES and COLS */
416 int focussed;
417 struct tog_view *parent;
418 struct tog_view *child;
419 int child_focussed;
421 /* type-specific state */
422 enum tog_view_type type;
423 union {
424 struct tog_diff_view_state diff;
425 struct tog_log_view_state log;
426 struct tog_blame_view_state blame;
427 struct tog_tree_view_state tree;
428 } state;
430 const struct got_error *(*show)(struct tog_view *);
431 const struct got_error *(*input)(struct tog_view **,
432 struct tog_view **, struct tog_view**, struct tog_view *, int);
433 const struct got_error *(*close)(struct tog_view *);
435 const struct got_error *(*search_start)(struct tog_view *);
436 const struct got_error *(*search_next)(struct tog_view *);
437 int searching;
438 #define TOG_SEARCH_FORWARD 1
439 #define TOG_SEARCH_BACKWARD 2
440 int search_next_done;
441 #define TOG_SEARCH_HAVE_MORE 1
442 #define TOG_SEARCH_NO_MORE 2
443 #define TOG_SEARCH_HAVE_NONE 3
444 regex_t regex;
445 regmatch_t regmatch;
446 };
448 static const struct got_error *open_diff_view(struct tog_view *,
449 struct got_object_id *, struct got_object_id *, int, struct tog_view *,
450 struct got_reflist_head *, struct got_repository *);
451 static const struct got_error *show_diff_view(struct tog_view *);
452 static const struct got_error *input_diff_view(struct tog_view **,
453 struct tog_view **, struct tog_view **, struct tog_view *, int);
454 static const struct got_error* close_diff_view(struct tog_view *);
455 static const struct got_error *search_start_diff_view(struct tog_view *);
456 static const struct got_error *search_next_diff_view(struct tog_view *);
458 static const struct got_error *open_log_view(struct tog_view *,
459 struct got_object_id *, struct got_reflist_head *,
460 struct got_repository *, const char *, const char *, int);
461 static const struct got_error * show_log_view(struct tog_view *);
462 static const struct got_error *input_log_view(struct tog_view **,
463 struct tog_view **, struct tog_view **, struct tog_view *, int);
464 static const struct got_error *close_log_view(struct tog_view *);
465 static const struct got_error *search_start_log_view(struct tog_view *);
466 static const struct got_error *search_next_log_view(struct tog_view *);
468 static const struct got_error *open_blame_view(struct tog_view *, char *,
469 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
470 static const struct got_error *show_blame_view(struct tog_view *);
471 static const struct got_error *input_blame_view(struct tog_view **,
472 struct tog_view **, struct tog_view **, struct tog_view *, int);
473 static const struct got_error *close_blame_view(struct tog_view *);
474 static const struct got_error *search_start_blame_view(struct tog_view *);
475 static const struct got_error *search_next_blame_view(struct tog_view *);
477 static const struct got_error *open_tree_view(struct tog_view *,
478 struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
479 struct got_repository *);
480 static const struct got_error *show_tree_view(struct tog_view *);
481 static const struct got_error *input_tree_view(struct tog_view **,
482 struct tog_view **, struct tog_view **, struct tog_view *, int);
483 static const struct got_error *close_tree_view(struct tog_view *);
484 static const struct got_error *search_start_tree_view(struct tog_view *);
485 static const struct got_error *search_next_tree_view(struct tog_view *);
487 static volatile sig_atomic_t tog_sigwinch_received;
488 static volatile sig_atomic_t tog_sigpipe_received;
489 static volatile sig_atomic_t tog_sigcont_received;
491 static void
492 tog_sigwinch(int signo)
494 tog_sigwinch_received = 1;
497 static void
498 tog_sigpipe(int signo)
500 tog_sigpipe_received = 1;
503 static void
504 tog_sigcont(int signo)
506 tog_sigcont_received = 1;
509 static const struct got_error *
510 view_close(struct tog_view *view)
512 const struct got_error *err = NULL;
514 if (view->child) {
515 view_close(view->child);
516 view->child = NULL;
518 if (view->close)
519 err = view->close(view);
520 if (view->panel)
521 del_panel(view->panel);
522 if (view->window)
523 delwin(view->window);
524 free(view);
525 return err;
528 static struct tog_view *
529 view_open(int nlines, int ncols, int begin_y, int begin_x,
530 enum tog_view_type type)
532 struct tog_view *view = calloc(1, sizeof(*view));
534 if (view == NULL)
535 return NULL;
537 view->type = type;
538 view->lines = LINES;
539 view->cols = COLS;
540 view->nlines = nlines ? nlines : LINES - begin_y;
541 view->ncols = ncols ? ncols : COLS - begin_x;
542 view->begin_y = begin_y;
543 view->begin_x = begin_x;
544 view->window = newwin(nlines, ncols, begin_y, begin_x);
545 if (view->window == NULL) {
546 view_close(view);
547 return NULL;
549 view->panel = new_panel(view->window);
550 if (view->panel == NULL ||
551 set_panel_userptr(view->panel, view) != OK) {
552 view_close(view);
553 return NULL;
556 keypad(view->window, TRUE);
557 return view;
560 static int
561 view_split_begin_x(int begin_x)
563 if (begin_x > 0 || COLS < 120)
564 return 0;
565 return (COLS - MAX(COLS / 2, 80));
568 static const struct got_error *view_resize(struct tog_view *);
570 static const struct got_error *
571 view_splitscreen(struct tog_view *view)
573 const struct got_error *err = NULL;
575 view->begin_y = 0;
576 view->begin_x = view_split_begin_x(0);
577 view->nlines = LINES;
578 view->ncols = COLS - view->begin_x;
579 view->lines = LINES;
580 view->cols = COLS;
581 err = view_resize(view);
582 if (err)
583 return err;
585 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
586 return got_error_from_errno("mvwin");
588 return NULL;
591 static const struct got_error *
592 view_fullscreen(struct tog_view *view)
594 const struct got_error *err = NULL;
596 view->begin_x = 0;
597 view->begin_y = 0;
598 view->nlines = LINES;
599 view->ncols = COLS;
600 view->lines = LINES;
601 view->cols = COLS;
602 err = view_resize(view);
603 if (err)
604 return err;
606 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
607 return got_error_from_errno("mvwin");
609 return NULL;
612 static int
613 view_is_parent_view(struct tog_view *view)
615 return view->parent == NULL;
618 static const struct got_error *
619 view_resize(struct tog_view *view)
621 int nlines, ncols;
623 if (view->lines > LINES)
624 nlines = view->nlines - (view->lines - LINES);
625 else
626 nlines = view->nlines + (LINES - view->lines);
628 if (view->cols > COLS)
629 ncols = view->ncols - (view->cols - COLS);
630 else
631 ncols = view->ncols + (COLS - view->cols);
633 if (wresize(view->window, nlines, ncols) == ERR)
634 return got_error_from_errno("wresize");
635 if (replace_panel(view->panel, view->window) == ERR)
636 return got_error_from_errno("replace_panel");
637 wclear(view->window);
639 view->nlines = nlines;
640 view->ncols = ncols;
641 view->lines = LINES;
642 view->cols = COLS;
644 if (view->child) {
645 view->child->begin_x = view_split_begin_x(view->begin_x);
646 if (view->child->begin_x == 0) {
647 view_fullscreen(view->child);
648 if (view->child->focussed)
649 show_panel(view->child->panel);
650 else
651 show_panel(view->panel);
652 } else {
653 view_splitscreen(view->child);
654 show_panel(view->child->panel);
658 return NULL;
661 static const struct got_error *
662 view_close_child(struct tog_view *view)
664 const struct got_error *err = NULL;
666 if (view->child == NULL)
667 return NULL;
669 err = view_close(view->child);
670 view->child = NULL;
671 return err;
674 static const struct got_error *
675 view_set_child(struct tog_view *view, struct tog_view *child)
677 const struct got_error *err = NULL;
679 view->child = child;
680 child->parent = view;
681 return err;
684 static int
685 view_is_splitscreen(struct tog_view *view)
687 return view->begin_x > 0;
690 static void
691 tog_resizeterm(void)
693 int cols, lines;
694 struct winsize size;
696 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
697 cols = 80; /* Default */
698 lines = 24;
699 } else {
700 cols = size.ws_col;
701 lines = size.ws_row;
703 resize_term(lines, cols);
706 static const struct got_error *
707 view_search_start(struct tog_view *view)
709 const struct got_error *err = NULL;
710 char pattern[1024];
711 int ret;
713 if (view->nlines < 1)
714 return NULL;
716 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
717 wclrtoeol(view->window);
719 nocbreak();
720 echo();
721 ret = wgetnstr(view->window, pattern, sizeof(pattern));
722 cbreak();
723 noecho();
724 if (ret == ERR)
725 return NULL;
727 if (view->searching) {
728 regfree(&view->regex);
729 view->searching = 0;
732 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
733 err = view->search_start(view);
734 if (err) {
735 regfree(&view->regex);
736 return err;
738 view->searching = TOG_SEARCH_FORWARD;
739 view->search_next_done = 0;
740 view->search_next(view);
743 return NULL;
746 static const struct got_error *
747 view_input(struct tog_view **new, struct tog_view **dead,
748 struct tog_view **focus, int *done, struct tog_view *view,
749 struct tog_view_list_head *views)
751 const struct got_error *err = NULL;
752 struct tog_view *v;
753 int ch, errcode;
755 *new = NULL;
756 *dead = NULL;
757 *focus = NULL;
759 /* Clear "no matches" indicator. */
760 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
761 view->search_next_done == TOG_SEARCH_HAVE_NONE)
762 view->search_next_done = TOG_SEARCH_HAVE_MORE;
764 if (view->searching && !view->search_next_done) {
765 errcode = pthread_mutex_unlock(&tog_mutex);
766 if (errcode)
767 return got_error_set_errno(errcode,
768 "pthread_mutex_unlock");
769 pthread_yield();
770 errcode = pthread_mutex_lock(&tog_mutex);
771 if (errcode)
772 return got_error_set_errno(errcode,
773 "pthread_mutex_lock");
774 view->search_next(view);
775 return NULL;
778 nodelay(stdscr, FALSE);
779 /* Allow threads to make progress while we are waiting for input. */
780 errcode = pthread_mutex_unlock(&tog_mutex);
781 if (errcode)
782 return got_error_set_errno(errcode, "pthread_mutex_unlock");
783 ch = wgetch(view->window);
784 errcode = pthread_mutex_lock(&tog_mutex);
785 if (errcode)
786 return got_error_set_errno(errcode, "pthread_mutex_lock");
787 nodelay(stdscr, TRUE);
789 if (tog_sigwinch_received || tog_sigcont_received) {
790 tog_resizeterm();
791 tog_sigwinch_received = 0;
792 tog_sigcont_received = 0;
793 TAILQ_FOREACH(v, views, entry) {
794 err = view_resize(v);
795 if (err)
796 return err;
797 err = v->input(new, dead, focus, v, KEY_RESIZE);
798 if (err)
799 return err;
803 switch (ch) {
804 case ERR:
805 break;
806 case '\t':
807 if (view->child) {
808 *focus = view->child;
809 view->child_focussed = 1;
810 } else if (view->parent) {
811 *focus = view->parent;
812 view->parent->child_focussed = 0;
814 break;
815 case 'q':
816 err = view->input(new, dead, focus, view, ch);
817 *dead = view;
818 break;
819 case 'Q':
820 *done = 1;
821 break;
822 case 'f':
823 if (view_is_parent_view(view)) {
824 if (view->child == NULL)
825 break;
826 if (view_is_splitscreen(view->child)) {
827 *focus = view->child;
828 view->child_focussed = 1;
829 err = view_fullscreen(view->child);
830 } else
831 err = view_splitscreen(view->child);
832 if (err)
833 break;
834 err = view->child->input(new, dead, focus,
835 view->child, KEY_RESIZE);
836 } else {
837 if (view_is_splitscreen(view)) {
838 *focus = view;
839 view->parent->child_focussed = 1;
840 err = view_fullscreen(view);
841 } else {
842 err = view_splitscreen(view);
844 if (err)
845 break;
846 err = view->input(new, dead, focus, view,
847 KEY_RESIZE);
849 break;
850 case KEY_RESIZE:
851 break;
852 case '/':
853 if (view->search_start)
854 view_search_start(view);
855 else
856 err = view->input(new, dead, focus, view, ch);
857 break;
858 case 'N':
859 case 'n':
860 if (view->search_next) {
861 view->searching = (ch == 'n' ?
862 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
863 view->search_next_done = 0;
864 view->search_next(view);
865 } else
866 err = view->input(new, dead, focus, view, ch);
867 break;
868 default:
869 err = view->input(new, dead, focus, view, ch);
870 break;
873 return err;
876 void
877 view_vborder(struct tog_view *view)
879 PANEL *panel;
880 struct tog_view *view_above;
882 if (view->parent)
883 return view_vborder(view->parent);
885 panel = panel_above(view->panel);
886 if (panel == NULL)
887 return;
889 view_above = panel_userptr(panel);
890 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
891 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
894 int
895 view_needs_focus_indication(struct tog_view *view)
897 if (view_is_parent_view(view)) {
898 if (view->child == NULL || view->child_focussed)
899 return 0;
900 if (!view_is_splitscreen(view->child))
901 return 0;
902 } else if (!view_is_splitscreen(view))
903 return 0;
905 return view->focussed;
908 static const struct got_error *
909 view_loop(struct tog_view *view)
911 const struct got_error *err = NULL;
912 struct tog_view_list_head views;
913 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
914 int fast_refresh = 10;
915 int done = 0, errcode;
917 errcode = pthread_mutex_lock(&tog_mutex);
918 if (errcode)
919 return got_error_set_errno(errcode, "pthread_mutex_lock");
921 TAILQ_INIT(&views);
922 TAILQ_INSERT_HEAD(&views, view, entry);
924 main_view = view;
925 view->focussed = 1;
926 err = view->show(view);
927 if (err)
928 return err;
929 update_panels();
930 doupdate();
931 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
932 /* Refresh fast during initialization, then become slower. */
933 if (fast_refresh && fast_refresh-- == 0)
934 halfdelay(10); /* switch to once per second */
936 err = view_input(&new_view, &dead_view, &focus_view, &done,
937 view, &views);
938 if (err)
939 break;
940 if (dead_view) {
941 struct tog_view *prev = NULL;
943 if (view_is_parent_view(dead_view))
944 prev = TAILQ_PREV(dead_view,
945 tog_view_list_head, entry);
946 else if (view->parent != dead_view)
947 prev = view->parent;
949 if (dead_view->parent)
950 dead_view->parent->child = NULL;
951 else
952 TAILQ_REMOVE(&views, dead_view, entry);
954 err = view_close(dead_view);
955 if (err || (dead_view == main_view && new_view == NULL))
956 goto done;
958 if (view == dead_view) {
959 if (focus_view)
960 view = focus_view;
961 else if (prev)
962 view = prev;
963 else if (!TAILQ_EMPTY(&views))
964 view = TAILQ_LAST(&views,
965 tog_view_list_head);
966 else
967 view = NULL;
968 if (view) {
969 if (view->child && view->child_focussed)
970 focus_view = view->child;
971 else
972 focus_view = view;
976 if (new_view) {
977 struct tog_view *v, *t;
978 /* Only allow one parent view per type. */
979 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
980 if (v->type != new_view->type)
981 continue;
982 TAILQ_REMOVE(&views, v, entry);
983 err = view_close(v);
984 if (err)
985 goto done;
986 break;
988 TAILQ_INSERT_TAIL(&views, new_view, entry);
989 view = new_view;
990 if (focus_view == NULL)
991 focus_view = new_view;
993 if (focus_view) {
994 show_panel(focus_view->panel);
995 if (view)
996 view->focussed = 0;
997 focus_view->focussed = 1;
998 view = focus_view;
999 if (new_view)
1000 show_panel(new_view->panel);
1001 if (view->child && view_is_splitscreen(view->child))
1002 show_panel(view->child->panel);
1004 if (view) {
1005 if (focus_view == NULL) {
1006 view->focussed = 1;
1007 show_panel(view->panel);
1008 if (view->child && view_is_splitscreen(view->child))
1009 show_panel(view->child->panel);
1010 focus_view = view;
1012 if (view->parent) {
1013 err = view->parent->show(view->parent);
1014 if (err)
1015 goto done;
1017 err = view->show(view);
1018 if (err)
1019 goto done;
1020 if (view->child) {
1021 err = view->child->show(view->child);
1022 if (err)
1023 goto done;
1025 update_panels();
1026 doupdate();
1029 done:
1030 while (!TAILQ_EMPTY(&views)) {
1031 view = TAILQ_FIRST(&views);
1032 TAILQ_REMOVE(&views, view, entry);
1033 view_close(view);
1036 errcode = pthread_mutex_unlock(&tog_mutex);
1037 if (errcode && err == NULL)
1038 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1040 return err;
1043 __dead static void
1044 usage_log(void)
1046 endwin();
1047 fprintf(stderr,
1048 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1049 getprogname());
1050 exit(1);
1053 /* Create newly allocated wide-character string equivalent to a byte string. */
1054 static const struct got_error *
1055 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1057 char *vis = NULL;
1058 const struct got_error *err = NULL;
1060 *ws = NULL;
1061 *wlen = mbstowcs(NULL, s, 0);
1062 if (*wlen == (size_t)-1) {
1063 int vislen;
1064 if (errno != EILSEQ)
1065 return got_error_from_errno("mbstowcs");
1067 /* byte string invalid in current encoding; try to "fix" it */
1068 err = got_mbsavis(&vis, &vislen, s);
1069 if (err)
1070 return err;
1071 *wlen = mbstowcs(NULL, vis, 0);
1072 if (*wlen == (size_t)-1) {
1073 err = got_error_from_errno("mbstowcs"); /* give up */
1074 goto done;
1078 *ws = calloc(*wlen + 1, sizeof(**ws));
1079 if (*ws == NULL) {
1080 err = got_error_from_errno("calloc");
1081 goto done;
1084 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1085 err = got_error_from_errno("mbstowcs");
1086 done:
1087 free(vis);
1088 if (err) {
1089 free(*ws);
1090 *ws = NULL;
1091 *wlen = 0;
1093 return err;
1096 /* Format a line for display, ensuring that it won't overflow a width limit. */
1097 static const struct got_error *
1098 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1099 int col_tab_align)
1101 const struct got_error *err = NULL;
1102 int cols = 0;
1103 wchar_t *wline = NULL;
1104 size_t wlen;
1105 int i;
1107 *wlinep = NULL;
1108 *widthp = 0;
1110 err = mbs2ws(&wline, &wlen, line);
1111 if (err)
1112 return err;
1114 i = 0;
1115 while (i < wlen) {
1116 int width = wcwidth(wline[i]);
1118 if (width == 0) {
1119 i++;
1120 continue;
1123 if (width == 1 || width == 2) {
1124 if (cols + width > wlimit)
1125 break;
1126 cols += width;
1127 i++;
1128 } else if (width == -1) {
1129 if (wline[i] == L'\t') {
1130 width = TABSIZE -
1131 ((cols + col_tab_align) % TABSIZE);
1132 if (cols + width > wlimit)
1133 break;
1134 cols += width;
1136 i++;
1137 } else {
1138 err = got_error_from_errno("wcwidth");
1139 goto done;
1142 wline[i] = L'\0';
1143 if (widthp)
1144 *widthp = cols;
1145 done:
1146 if (err)
1147 free(wline);
1148 else
1149 *wlinep = wline;
1150 return err;
1153 static const struct got_error*
1154 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1155 struct got_object_id *id, struct got_repository *repo)
1157 static const struct got_error *err = NULL;
1158 struct got_reflist_entry *re;
1159 char *s;
1160 const char *name;
1162 *refs_str = NULL;
1164 SIMPLEQ_FOREACH(re, refs, entry) {
1165 struct got_tag_object *tag = NULL;
1166 struct got_object_id *ref_id;
1167 int cmp;
1169 name = got_ref_get_name(re->ref);
1170 if (strcmp(name, GOT_REF_HEAD) == 0)
1171 continue;
1172 if (strncmp(name, "refs/", 5) == 0)
1173 name += 5;
1174 if (strncmp(name, "got/", 4) == 0)
1175 continue;
1176 if (strncmp(name, "heads/", 6) == 0)
1177 name += 6;
1178 if (strncmp(name, "remotes/", 8) == 0) {
1179 name += 8;
1180 s = strstr(name, "/" GOT_REF_HEAD);
1181 if (s != NULL && s[strlen(s)] == '\0')
1182 continue;
1184 err = got_ref_resolve(&ref_id, repo, re->ref);
1185 if (err)
1186 break;
1187 if (strncmp(name, "tags/", 5) == 0) {
1188 err = got_object_open_as_tag(&tag, repo, ref_id);
1189 if (err) {
1190 if (err->code != GOT_ERR_OBJ_TYPE) {
1191 free(ref_id);
1192 break;
1194 /* Ref points at something other than a tag. */
1195 err = NULL;
1196 tag = NULL;
1199 cmp = got_object_id_cmp(tag ?
1200 got_object_tag_get_object_id(tag) : ref_id, id);
1201 free(ref_id);
1202 if (tag)
1203 got_object_tag_close(tag);
1204 if (cmp != 0)
1205 continue;
1206 s = *refs_str;
1207 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1208 s ? ", " : "", name) == -1) {
1209 err = got_error_from_errno("asprintf");
1210 free(s);
1211 *refs_str = NULL;
1212 break;
1214 free(s);
1217 return err;
1220 static const struct got_error *
1221 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1222 int col_tab_align)
1224 char *smallerthan, *at;
1226 smallerthan = strchr(author, '<');
1227 if (smallerthan && smallerthan[1] != '\0')
1228 author = smallerthan + 1;
1229 at = strchr(author, '@');
1230 if (at)
1231 *at = '\0';
1232 return format_line(wauthor, author_width, author, limit, col_tab_align);
1235 static const struct got_error *
1236 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1237 struct got_object_id *id, struct got_reflist_head *refs,
1238 const size_t date_display_cols, int author_display_cols,
1239 struct tog_colors *colors)
1241 const struct got_error *err = NULL;
1242 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1243 char *logmsg0 = NULL, *logmsg = NULL;
1244 char *author = NULL;
1245 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1246 int author_width, logmsg_width;
1247 char *newline, *line = NULL;
1248 int col, limit;
1249 const int avail = view->ncols;
1250 struct tm tm;
1251 time_t committer_time;
1252 struct tog_color *tc;
1254 committer_time = got_object_commit_get_committer_time(commit);
1255 if (localtime_r(&committer_time, &tm) == NULL)
1256 return got_error_from_errno("localtime_r");
1257 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1258 >= sizeof(datebuf))
1259 return got_error(GOT_ERR_NO_SPACE);
1261 if (avail <= date_display_cols)
1262 limit = MIN(sizeof(datebuf) - 1, avail);
1263 else
1264 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1265 tc = get_color(colors, TOG_COLOR_DATE);
1266 if (tc)
1267 wattr_on(view->window,
1268 COLOR_PAIR(tc->colorpair), NULL);
1269 waddnstr(view->window, datebuf, limit);
1270 if (tc)
1271 wattr_off(view->window,
1272 COLOR_PAIR(tc->colorpair), NULL);
1273 col = limit;
1274 if (col > avail)
1275 goto done;
1277 if (avail >= 120) {
1278 char *id_str;
1279 err = got_object_id_str(&id_str, id);
1280 if (err)
1281 goto done;
1282 tc = get_color(colors, TOG_COLOR_COMMIT);
1283 if (tc)
1284 wattr_on(view->window,
1285 COLOR_PAIR(tc->colorpair), NULL);
1286 wprintw(view->window, "%.8s ", id_str);
1287 if (tc)
1288 wattr_off(view->window,
1289 COLOR_PAIR(tc->colorpair), NULL);
1290 free(id_str);
1291 col += 9;
1292 if (col > avail)
1293 goto done;
1296 author = strdup(got_object_commit_get_author(commit));
1297 if (author == NULL) {
1298 err = got_error_from_errno("strdup");
1299 goto done;
1301 err = format_author(&wauthor, &author_width, author, avail - col, col);
1302 if (err)
1303 goto done;
1304 tc = get_color(colors, TOG_COLOR_AUTHOR);
1305 if (tc)
1306 wattr_on(view->window,
1307 COLOR_PAIR(tc->colorpair), NULL);
1308 waddwstr(view->window, wauthor);
1309 if (tc)
1310 wattr_off(view->window,
1311 COLOR_PAIR(tc->colorpair), NULL);
1312 col += author_width;
1313 while (col < avail && author_width < author_display_cols + 2) {
1314 waddch(view->window, ' ');
1315 col++;
1316 author_width++;
1318 if (col > avail)
1319 goto done;
1321 err = got_object_commit_get_logmsg(&logmsg0, commit);
1322 if (err)
1323 goto done;
1324 logmsg = logmsg0;
1325 while (*logmsg == '\n')
1326 logmsg++;
1327 newline = strchr(logmsg, '\n');
1328 if (newline)
1329 *newline = '\0';
1330 limit = avail - col;
1331 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1332 if (err)
1333 goto done;
1334 waddwstr(view->window, wlogmsg);
1335 col += logmsg_width;
1336 while (col < avail) {
1337 waddch(view->window, ' ');
1338 col++;
1340 done:
1341 free(logmsg0);
1342 free(wlogmsg);
1343 free(author);
1344 free(wauthor);
1345 free(line);
1346 return err;
1349 static struct commit_queue_entry *
1350 alloc_commit_queue_entry(struct got_commit_object *commit,
1351 struct got_object_id *id)
1353 struct commit_queue_entry *entry;
1355 entry = calloc(1, sizeof(*entry));
1356 if (entry == NULL)
1357 return NULL;
1359 entry->id = id;
1360 entry->commit = commit;
1361 return entry;
1364 static void
1365 pop_commit(struct commit_queue *commits)
1367 struct commit_queue_entry *entry;
1369 entry = TAILQ_FIRST(&commits->head);
1370 TAILQ_REMOVE(&commits->head, entry, entry);
1371 got_object_commit_close(entry->commit);
1372 commits->ncommits--;
1373 /* Don't free entry->id! It is owned by the commit graph. */
1374 free(entry);
1377 static void
1378 free_commits(struct commit_queue *commits)
1380 while (!TAILQ_EMPTY(&commits->head))
1381 pop_commit(commits);
1384 static const struct got_error *
1385 match_commit(int *have_match, struct got_object_id *id,
1386 struct got_commit_object *commit, regex_t *regex)
1388 const struct got_error *err = NULL;
1389 regmatch_t regmatch;
1390 char *id_str = NULL, *logmsg = NULL;
1392 *have_match = 0;
1394 err = got_object_id_str(&id_str, id);
1395 if (err)
1396 return err;
1398 err = got_object_commit_get_logmsg(&logmsg, commit);
1399 if (err)
1400 goto done;
1402 if (regexec(regex, got_object_commit_get_author(commit), 1,
1403 &regmatch, 0) == 0 ||
1404 regexec(regex, got_object_commit_get_committer(commit), 1,
1405 &regmatch, 0) == 0 ||
1406 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1407 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1408 *have_match = 1;
1409 done:
1410 free(id_str);
1411 free(logmsg);
1412 return err;
1415 static const struct got_error *
1416 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1417 int minqueue, struct got_repository *repo, const char *path,
1418 int *searching, int *search_next_done, regex_t *regex)
1420 const struct got_error *err = NULL;
1421 int nqueued = 0;
1424 * We keep all commits open throughout the lifetime of the log
1425 * view in order to avoid having to re-fetch commits from disk
1426 * while updating the display.
1428 while (nqueued < minqueue ||
1429 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1430 struct got_object_id *id;
1431 struct got_commit_object *commit;
1432 struct commit_queue_entry *entry;
1433 int errcode;
1435 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1436 if (err || id == NULL)
1437 break;
1439 err = got_object_open_as_commit(&commit, repo, id);
1440 if (err)
1441 break;
1442 entry = alloc_commit_queue_entry(commit, id);
1443 if (entry == NULL) {
1444 err = got_error_from_errno("alloc_commit_queue_entry");
1445 break;
1448 errcode = pthread_mutex_lock(&tog_mutex);
1449 if (errcode) {
1450 err = got_error_set_errno(errcode,
1451 "pthread_mutex_lock");
1452 break;
1455 entry->idx = commits->ncommits;
1456 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1457 nqueued++;
1458 commits->ncommits++;
1460 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1461 int have_match;
1462 err = match_commit(&have_match, id, commit, regex);
1463 if (err)
1464 break;
1465 if (have_match)
1466 *search_next_done = TOG_SEARCH_HAVE_MORE;
1469 errcode = pthread_mutex_unlock(&tog_mutex);
1470 if (errcode && err == NULL)
1471 err = got_error_set_errno(errcode,
1472 "pthread_mutex_unlock");
1473 if (err)
1474 break;
1477 return err;
1480 static const struct got_error *
1481 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1482 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1483 struct commit_queue *commits, int selected_idx, int limit,
1484 struct got_reflist_head *refs, const char *path, int commits_needed,
1485 struct tog_colors *colors)
1487 const struct got_error *err = NULL;
1488 struct tog_log_view_state *s = &view->state.log;
1489 struct commit_queue_entry *entry;
1490 int width;
1491 int ncommits, author_cols = 4;
1492 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1493 char *refs_str = NULL;
1494 wchar_t *wline;
1495 struct tog_color *tc;
1496 static const size_t date_display_cols = 12;
1498 entry = first;
1499 ncommits = 0;
1500 while (entry) {
1501 if (ncommits == selected_idx) {
1502 *selected = entry;
1503 break;
1505 entry = TAILQ_NEXT(entry, entry);
1506 ncommits++;
1509 if (*selected && !(view->searching && view->search_next_done == 0)) {
1510 err = got_object_id_str(&id_str, (*selected)->id);
1511 if (err)
1512 return err;
1513 if (refs) {
1514 err = build_refs_str(&refs_str, refs, (*selected)->id,
1515 s->repo);
1516 if (err)
1517 goto done;
1521 if (commits_needed == 0)
1522 halfdelay(10); /* disable fast refresh */
1524 if (commits_needed > 0) {
1525 if (asprintf(&ncommits_str, " [%d/%d] %s",
1526 entry ? entry->idx + 1 : 0, commits->ncommits,
1527 (view->searching && !view->search_next_done) ?
1528 "searching..." : "loading...") == -1) {
1529 err = got_error_from_errno("asprintf");
1530 goto done;
1532 } else {
1533 const char *search_str = NULL;
1535 if (view->searching) {
1536 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1537 search_str = "no more matches";
1538 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1539 search_str = "no matches found";
1540 else if (!view->search_next_done)
1541 search_str = "searching...";
1544 if (asprintf(&ncommits_str, " [%d/%d] %s",
1545 entry ? entry->idx + 1 : 0, commits->ncommits,
1546 search_str ? search_str :
1547 (refs_str ? refs_str : "")) == -1) {
1548 err = got_error_from_errno("asprintf");
1549 goto done;
1553 if (path && strcmp(path, "/") != 0) {
1554 if (asprintf(&header, "commit %s %s%s",
1555 id_str ? id_str : "........................................",
1556 path, ncommits_str) == -1) {
1557 err = got_error_from_errno("asprintf");
1558 header = NULL;
1559 goto done;
1561 } else if (asprintf(&header, "commit %s%s",
1562 id_str ? id_str : "........................................",
1563 ncommits_str) == -1) {
1564 err = got_error_from_errno("asprintf");
1565 header = NULL;
1566 goto done;
1568 err = format_line(&wline, &width, header, view->ncols, 0);
1569 if (err)
1570 goto done;
1572 werase(view->window);
1574 if (view_needs_focus_indication(view))
1575 wstandout(view->window);
1576 tc = get_color(colors, TOG_COLOR_COMMIT);
1577 if (tc)
1578 wattr_on(view->window,
1579 COLOR_PAIR(tc->colorpair), NULL);
1580 waddwstr(view->window, wline);
1581 if (tc)
1582 wattr_off(view->window,
1583 COLOR_PAIR(tc->colorpair), NULL);
1584 while (width < view->ncols) {
1585 waddch(view->window, ' ');
1586 width++;
1588 if (view_needs_focus_indication(view))
1589 wstandend(view->window);
1590 free(wline);
1591 if (limit <= 1)
1592 goto done;
1594 /* Grow author column size if necessary. */
1595 entry = first;
1596 ncommits = 0;
1597 while (entry) {
1598 char *author;
1599 wchar_t *wauthor;
1600 int width;
1601 if (ncommits >= limit - 1)
1602 break;
1603 author = strdup(got_object_commit_get_author(entry->commit));
1604 if (author == NULL) {
1605 err = got_error_from_errno("strdup");
1606 goto done;
1608 err = format_author(&wauthor, &width, author, COLS,
1609 date_display_cols);
1610 if (author_cols < width)
1611 author_cols = width;
1612 free(wauthor);
1613 free(author);
1614 ncommits++;
1615 entry = TAILQ_NEXT(entry, entry);
1618 entry = first;
1619 *last = first;
1620 ncommits = 0;
1621 while (entry) {
1622 if (ncommits >= limit - 1)
1623 break;
1624 if (ncommits == selected_idx)
1625 wstandout(view->window);
1626 err = draw_commit(view, entry->commit, entry->id, refs,
1627 date_display_cols, author_cols, colors);
1628 if (ncommits == selected_idx)
1629 wstandend(view->window);
1630 if (err)
1631 goto done;
1632 ncommits++;
1633 *last = entry;
1634 entry = TAILQ_NEXT(entry, entry);
1637 view_vborder(view);
1638 done:
1639 free(id_str);
1640 free(refs_str);
1641 free(ncommits_str);
1642 free(header);
1643 return err;
1646 static void
1647 scroll_up(struct tog_view *view,
1648 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1649 struct commit_queue *commits)
1651 struct commit_queue_entry *entry;
1652 int nscrolled = 0;
1654 entry = TAILQ_FIRST(&commits->head);
1655 if (*first_displayed_entry == entry)
1656 return;
1658 entry = *first_displayed_entry;
1659 while (entry && nscrolled < maxscroll) {
1660 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1661 if (entry) {
1662 *first_displayed_entry = entry;
1663 nscrolled++;
1668 static const struct got_error *
1669 trigger_log_thread(struct tog_view *log_view, int wait,
1670 int *commits_needed, int *log_complete,
1671 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1673 int errcode;
1675 halfdelay(1); /* fast refresh while loading commits */
1677 while (*commits_needed > 0) {
1678 if (*log_complete)
1679 break;
1681 /* Wake the log thread. */
1682 errcode = pthread_cond_signal(need_commits);
1683 if (errcode)
1684 return got_error_set_errno(errcode,
1685 "pthread_cond_signal");
1688 * The mutex will be released while the view loop waits
1689 * in wgetch(), at which time the log thread will run.
1691 if (!wait)
1692 break;
1694 /* Display progress update in log view. */
1695 show_log_view(log_view);
1696 update_panels();
1697 doupdate();
1699 /* Wait right here while next commit is being loaded. */
1700 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1701 if (errcode)
1702 return got_error_set_errno(errcode,
1703 "pthread_cond_wait");
1705 /* Display progress update in log view. */
1706 show_log_view(log_view);
1707 update_panels();
1708 doupdate();
1711 return NULL;
1714 static const struct got_error *
1715 scroll_down(struct tog_view *log_view,
1716 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1717 struct commit_queue_entry **last_displayed_entry,
1718 struct commit_queue *commits, int *log_complete, int *commits_needed,
1719 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1721 const struct got_error *err = NULL;
1722 struct commit_queue_entry *pentry;
1723 int nscrolled = 0, ncommits_needed;
1725 if (*last_displayed_entry == NULL)
1726 return NULL;
1728 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1729 if (commits->ncommits < ncommits_needed && !*log_complete) {
1731 * Ask the log thread for required amount of commits.
1733 (*commits_needed) += maxscroll;
1734 err = trigger_log_thread(log_view, 1, commits_needed,
1735 log_complete, need_commits, commit_loaded);
1736 if (err)
1737 return err;
1740 do {
1741 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1742 if (pentry == NULL)
1743 break;
1745 *last_displayed_entry = pentry;
1747 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1748 if (pentry == NULL)
1749 break;
1750 *first_displayed_entry = pentry;
1751 } while (++nscrolled < maxscroll);
1753 return err;
1756 static const struct got_error *
1757 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1758 struct got_commit_object *commit, struct got_object_id *commit_id,
1759 struct tog_view *log_view, struct got_reflist_head *refs,
1760 struct got_repository *repo)
1762 const struct got_error *err;
1763 struct got_object_qid *parent_id;
1764 struct tog_view *diff_view;
1766 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1767 if (diff_view == NULL)
1768 return got_error_from_errno("view_open");
1770 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1771 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1772 commit_id, 0, log_view, refs, repo);
1773 if (err == NULL)
1774 *new_view = diff_view;
1775 return err;
1778 static const struct got_error *
1779 tree_view_visit_subtree(struct got_tree_object *subtree,
1780 struct tog_tree_view_state *s)
1782 struct tog_parent_tree *parent;
1784 parent = calloc(1, sizeof(*parent));
1785 if (parent == NULL)
1786 return got_error_from_errno("calloc");
1788 parent->tree = s->tree;
1789 parent->first_displayed_entry = s->first_displayed_entry;
1790 parent->selected_entry = s->selected_entry;
1791 parent->selected = s->selected;
1792 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1793 s->tree = subtree;
1794 s->selected = 0;
1795 s->first_displayed_entry = NULL;
1796 return NULL;
1799 static const struct got_error *
1800 tree_view_walk_path(struct tog_tree_view_state *s,
1801 struct got_object_id *commit_id,
1802 const char *path, struct got_repository *repo)
1804 const struct got_error *err = NULL;
1805 struct got_tree_object *tree = NULL;
1806 const char *p;
1807 char *slash, *subpath = NULL;
1809 /* Walk the path and open corresponding tree objects. */
1810 p = path;
1811 while (*p) {
1812 struct got_tree_entry *te;
1813 struct got_object_id *tree_id;
1814 char *te_name;
1816 while (p[0] == '/')
1817 p++;
1819 /* Ensure the correct subtree entry is selected. */
1820 slash = strchr(p, '/');
1821 if (slash == NULL)
1822 te_name = strdup(p);
1823 else
1824 te_name = strndup(p, slash - p);
1825 if (te_name == NULL) {
1826 err = got_error_from_errno("strndup");
1827 break;
1829 te = got_object_tree_find_entry(s->tree, te_name);
1830 if (te == NULL) {
1831 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1832 free(te_name);
1833 break;
1835 free(te_name);
1836 s->selected_entry = te;
1837 s->selected = got_tree_entry_get_index(te);
1838 if (s->tree != s->root)
1839 s->selected++; /* skip '..' */
1841 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1842 /* Jump to this file's entry. */
1843 s->first_displayed_entry = s->selected_entry;
1844 s->selected = 0;
1845 break;
1848 slash = strchr(p, '/');
1849 if (slash)
1850 subpath = strndup(path, slash - path);
1851 else
1852 subpath = strdup(path);
1853 if (subpath == NULL) {
1854 err = got_error_from_errno("strdup");
1855 break;
1858 err = got_object_id_by_path(&tree_id, repo, commit_id,
1859 subpath);
1860 if (err)
1861 break;
1863 err = got_object_open_as_tree(&tree, repo, tree_id);
1864 free(tree_id);
1865 if (err)
1866 break;
1868 err = tree_view_visit_subtree(tree, s);
1869 if (err) {
1870 got_object_tree_close(tree);
1871 break;
1873 if (slash == NULL)
1874 break;
1875 free(subpath);
1876 subpath = NULL;
1877 p = slash;
1880 free(subpath);
1881 return err;
1884 static const struct got_error *
1885 browse_commit_tree(struct tog_view **new_view, int begin_x,
1886 struct commit_queue_entry *entry, const char *path,
1887 struct got_reflist_head *refs, struct got_repository *repo)
1889 const struct got_error *err = NULL;
1890 struct got_tree_object *tree;
1891 struct tog_tree_view_state *s;
1892 struct tog_view *tree_view;
1894 err = got_object_open_as_tree(&tree, repo,
1895 got_object_commit_get_tree_id(entry->commit));
1896 if (err)
1897 return err;
1899 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1900 if (tree_view == NULL)
1901 return got_error_from_errno("view_open");
1903 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1904 if (err) {
1905 got_object_tree_close(tree);
1906 return err;
1908 s = &tree_view->state.tree;
1910 *new_view = tree_view;
1912 if (got_path_is_root_dir(path))
1913 return NULL;
1915 return tree_view_walk_path(s, entry->id, path, repo);
1918 static const struct got_error *
1919 block_signals_used_by_main_thread(void)
1921 sigset_t sigset;
1922 int errcode;
1924 if (sigemptyset(&sigset) == -1)
1925 return got_error_from_errno("sigemptyset");
1927 /* tog handles SIGWINCH and SIGCONT */
1928 if (sigaddset(&sigset, SIGWINCH) == -1)
1929 return got_error_from_errno("sigaddset");
1930 if (sigaddset(&sigset, SIGCONT) == -1)
1931 return got_error_from_errno("sigaddset");
1933 /* ncurses handles SIGTSTP */
1934 if (sigaddset(&sigset, SIGTSTP) == -1)
1935 return got_error_from_errno("sigaddset");
1937 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1938 if (errcode)
1939 return got_error_set_errno(errcode, "pthread_sigmask");
1941 return NULL;
1944 static void *
1945 log_thread(void *arg)
1947 const struct got_error *err = NULL;
1948 int errcode = 0;
1949 struct tog_log_thread_args *a = arg;
1950 int done = 0;
1952 err = block_signals_used_by_main_thread();
1953 if (err)
1954 return (void *)err;
1956 while (!done && !err && !tog_sigpipe_received) {
1957 err = queue_commits(a->graph, a->commits, 1, a->repo,
1958 a->in_repo_path, a->searching, a->search_next_done,
1959 a->regex);
1960 if (err) {
1961 if (err->code != GOT_ERR_ITER_COMPLETED)
1962 return (void *)err;
1963 err = NULL;
1964 done = 1;
1965 } else if (a->commits_needed > 0)
1966 a->commits_needed--;
1968 errcode = pthread_mutex_lock(&tog_mutex);
1969 if (errcode) {
1970 err = got_error_set_errno(errcode,
1971 "pthread_mutex_lock");
1972 break;
1973 } else if (*a->quit)
1974 done = 1;
1975 else if (*a->first_displayed_entry == NULL) {
1976 *a->first_displayed_entry =
1977 TAILQ_FIRST(&a->commits->head);
1978 *a->selected_entry = *a->first_displayed_entry;
1981 errcode = pthread_cond_signal(&a->commit_loaded);
1982 if (errcode) {
1983 err = got_error_set_errno(errcode,
1984 "pthread_cond_signal");
1985 pthread_mutex_unlock(&tog_mutex);
1986 break;
1989 if (done)
1990 a->commits_needed = 0;
1991 else {
1992 if (a->commits_needed == 0) {
1993 errcode = pthread_cond_wait(&a->need_commits,
1994 &tog_mutex);
1995 if (errcode)
1996 err = got_error_set_errno(errcode,
1997 "pthread_cond_wait");
2001 errcode = pthread_mutex_unlock(&tog_mutex);
2002 if (errcode && err == NULL)
2003 err = got_error_set_errno(errcode,
2004 "pthread_mutex_unlock");
2006 a->log_complete = 1;
2007 return (void *)err;
2010 static const struct got_error *
2011 stop_log_thread(struct tog_log_view_state *s)
2013 const struct got_error *err = NULL;
2014 int errcode;
2016 if (s->thread) {
2017 s->quit = 1;
2018 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2019 if (errcode)
2020 return got_error_set_errno(errcode,
2021 "pthread_cond_signal");
2022 errcode = pthread_mutex_unlock(&tog_mutex);
2023 if (errcode)
2024 return got_error_set_errno(errcode,
2025 "pthread_mutex_unlock");
2026 errcode = pthread_join(s->thread, (void **)&err);
2027 if (errcode)
2028 return got_error_set_errno(errcode, "pthread_join");
2029 errcode = pthread_mutex_lock(&tog_mutex);
2030 if (errcode)
2031 return got_error_set_errno(errcode,
2032 "pthread_mutex_lock");
2033 s->thread = NULL;
2036 if (s->thread_args.repo) {
2037 got_repo_close(s->thread_args.repo);
2038 s->thread_args.repo = NULL;
2041 if (s->thread_args.graph) {
2042 got_commit_graph_close(s->thread_args.graph);
2043 s->thread_args.graph = NULL;
2046 return err;
2049 static const struct got_error *
2050 close_log_view(struct tog_view *view)
2052 const struct got_error *err = NULL;
2053 struct tog_log_view_state *s = &view->state.log;
2054 int errcode;
2056 err = stop_log_thread(s);
2058 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2059 if (errcode && err == NULL)
2060 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2062 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2063 if (errcode && err == NULL)
2064 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2066 free_commits(&s->commits);
2067 free(s->in_repo_path);
2068 s->in_repo_path = NULL;
2069 free(s->start_id);
2070 s->start_id = NULL;
2071 return err;
2074 static const struct got_error *
2075 search_start_log_view(struct tog_view *view)
2077 struct tog_log_view_state *s = &view->state.log;
2079 s->matched_entry = NULL;
2080 s->search_entry = NULL;
2081 return NULL;
2084 static const struct got_error *
2085 search_next_log_view(struct tog_view *view)
2087 const struct got_error *err = NULL;
2088 struct tog_log_view_state *s = &view->state.log;
2089 struct commit_queue_entry *entry;
2091 /* Display progress update in log view. */
2092 show_log_view(view);
2093 update_panels();
2094 doupdate();
2096 if (s->search_entry) {
2097 int errcode, ch;
2098 errcode = pthread_mutex_unlock(&tog_mutex);
2099 if (errcode)
2100 return got_error_set_errno(errcode,
2101 "pthread_mutex_unlock");
2102 ch = wgetch(view->window);
2103 errcode = pthread_mutex_lock(&tog_mutex);
2104 if (errcode)
2105 return got_error_set_errno(errcode,
2106 "pthread_mutex_lock");
2107 if (ch == KEY_BACKSPACE) {
2108 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2109 return NULL;
2111 if (view->searching == TOG_SEARCH_FORWARD)
2112 entry = TAILQ_NEXT(s->search_entry, entry);
2113 else
2114 entry = TAILQ_PREV(s->search_entry,
2115 commit_queue_head, entry);
2116 } else if (s->matched_entry) {
2117 if (view->searching == TOG_SEARCH_FORWARD)
2118 entry = TAILQ_NEXT(s->matched_entry, entry);
2119 else
2120 entry = TAILQ_PREV(s->matched_entry,
2121 commit_queue_head, entry);
2122 } else {
2123 if (view->searching == TOG_SEARCH_FORWARD)
2124 entry = TAILQ_FIRST(&s->commits.head);
2125 else
2126 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2129 while (1) {
2130 int have_match = 0;
2132 if (entry == NULL) {
2133 if (s->thread_args.log_complete ||
2134 view->searching == TOG_SEARCH_BACKWARD) {
2135 view->search_next_done =
2136 (s->matched_entry == NULL ?
2137 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2138 s->search_entry = NULL;
2139 return NULL;
2142 * Poke the log thread for more commits and return,
2143 * allowing the main loop to make progress. Search
2144 * will resume at s->search_entry once we come back.
2146 s->thread_args.commits_needed++;
2147 return trigger_log_thread(view, 0,
2148 &s->thread_args.commits_needed,
2149 &s->thread_args.log_complete,
2150 &s->thread_args.need_commits,
2151 &s->thread_args.commit_loaded);
2154 err = match_commit(&have_match, entry->id, entry->commit,
2155 &view->regex);
2156 if (err)
2157 break;
2158 if (have_match) {
2159 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2160 s->matched_entry = entry;
2161 break;
2164 s->search_entry = entry;
2165 if (view->searching == TOG_SEARCH_FORWARD)
2166 entry = TAILQ_NEXT(entry, entry);
2167 else
2168 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2171 if (s->matched_entry) {
2172 int cur = s->selected_entry->idx;
2173 while (cur < s->matched_entry->idx) {
2174 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2175 if (err)
2176 return err;
2177 cur++;
2179 while (cur > s->matched_entry->idx) {
2180 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2181 if (err)
2182 return err;
2183 cur--;
2187 s->search_entry = NULL;
2189 return NULL;
2192 static const struct got_error *
2193 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2194 struct got_reflist_head *refs, struct got_repository *repo,
2195 const char *head_ref_name, const char *in_repo_path,
2196 int log_branches)
2198 const struct got_error *err = NULL;
2199 struct tog_log_view_state *s = &view->state.log;
2200 struct got_repository *thread_repo = NULL;
2201 struct got_commit_graph *thread_graph = NULL;
2202 int errcode;
2204 if (in_repo_path != s->in_repo_path) {
2205 free(s->in_repo_path);
2206 s->in_repo_path = strdup(in_repo_path);
2207 if (s->in_repo_path == NULL)
2208 return got_error_from_errno("strdup");
2211 /* The commit queue only contains commits being displayed. */
2212 TAILQ_INIT(&s->commits.head);
2213 s->commits.ncommits = 0;
2215 s->refs = refs;
2216 s->repo = repo;
2217 s->head_ref_name = head_ref_name;
2218 s->start_id = got_object_id_dup(start_id);
2219 if (s->start_id == NULL) {
2220 err = got_error_from_errno("got_object_id_dup");
2221 goto done;
2223 s->log_branches = log_branches;
2225 SIMPLEQ_INIT(&s->colors);
2226 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2227 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2228 get_color_value("TOG_COLOR_COMMIT"));
2229 if (err)
2230 goto done;
2231 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2232 get_color_value("TOG_COLOR_AUTHOR"));
2233 if (err) {
2234 free_colors(&s->colors);
2235 goto done;
2237 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2238 get_color_value("TOG_COLOR_DATE"));
2239 if (err) {
2240 free_colors(&s->colors);
2241 goto done;
2245 view->show = show_log_view;
2246 view->input = input_log_view;
2247 view->close = close_log_view;
2248 view->search_start = search_start_log_view;
2249 view->search_next = search_next_log_view;
2251 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2252 if (err)
2253 goto done;
2254 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2255 !s->log_branches);
2256 if (err)
2257 goto done;
2258 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2259 s->repo, NULL, NULL);
2260 if (err)
2261 goto done;
2263 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2264 if (errcode) {
2265 err = got_error_set_errno(errcode, "pthread_cond_init");
2266 goto done;
2268 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2269 if (errcode) {
2270 err = got_error_set_errno(errcode, "pthread_cond_init");
2271 goto done;
2274 s->thread_args.commits_needed = view->nlines;
2275 s->thread_args.graph = thread_graph;
2276 s->thread_args.commits = &s->commits;
2277 s->thread_args.in_repo_path = s->in_repo_path;
2278 s->thread_args.start_id = s->start_id;
2279 s->thread_args.repo = thread_repo;
2280 s->thread_args.log_complete = 0;
2281 s->thread_args.quit = &s->quit;
2282 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2283 s->thread_args.selected_entry = &s->selected_entry;
2284 s->thread_args.searching = &view->searching;
2285 s->thread_args.search_next_done = &view->search_next_done;
2286 s->thread_args.regex = &view->regex;
2287 done:
2288 if (err)
2289 close_log_view(view);
2290 return err;
2293 static const struct got_error *
2294 show_log_view(struct tog_view *view)
2296 struct tog_log_view_state *s = &view->state.log;
2298 if (s->thread == NULL) {
2299 int errcode = pthread_create(&s->thread, NULL, log_thread,
2300 &s->thread_args);
2301 if (errcode)
2302 return got_error_set_errno(errcode, "pthread_create");
2305 return draw_commits(view, &s->last_displayed_entry,
2306 &s->selected_entry, s->first_displayed_entry,
2307 &s->commits, s->selected, view->nlines, s->refs,
2308 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2311 static const struct got_error *
2312 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2313 struct tog_view **focus_view, struct tog_view *view, int ch)
2315 const struct got_error *err = NULL;
2316 struct tog_log_view_state *s = &view->state.log;
2317 char *parent_path, *in_repo_path = NULL;
2318 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2319 int begin_x = 0;
2320 struct got_object_id *start_id;
2322 switch (ch) {
2323 case 'q':
2324 s->quit = 1;
2325 break;
2326 case 'k':
2327 case KEY_UP:
2328 case '<':
2329 case ',':
2330 if (s->first_displayed_entry == NULL)
2331 break;
2332 if (s->selected > 0)
2333 s->selected--;
2334 else
2335 scroll_up(view, &s->first_displayed_entry, 1,
2336 &s->commits);
2337 break;
2338 case KEY_PPAGE:
2339 case CTRL('b'):
2340 if (s->first_displayed_entry == NULL)
2341 break;
2342 if (TAILQ_FIRST(&s->commits.head) ==
2343 s->first_displayed_entry) {
2344 s->selected = 0;
2345 break;
2347 scroll_up(view, &s->first_displayed_entry,
2348 view->nlines - 1, &s->commits);
2349 break;
2350 case 'j':
2351 case KEY_DOWN:
2352 case '>':
2353 case '.':
2354 if (s->first_displayed_entry == NULL)
2355 break;
2356 if (s->selected < MIN(view->nlines - 2,
2357 s->commits.ncommits - 1)) {
2358 s->selected++;
2359 break;
2361 err = scroll_down(view, &s->first_displayed_entry, 1,
2362 &s->last_displayed_entry, &s->commits,
2363 &s->thread_args.log_complete,
2364 &s->thread_args.commits_needed,
2365 &s->thread_args.need_commits,
2366 &s->thread_args.commit_loaded);
2367 break;
2368 case KEY_NPAGE:
2369 case CTRL('f'): {
2370 struct commit_queue_entry *first;
2371 first = s->first_displayed_entry;
2372 if (first == NULL)
2373 break;
2374 err = scroll_down(view, &s->first_displayed_entry,
2375 view->nlines - 1, &s->last_displayed_entry,
2376 &s->commits, &s->thread_args.log_complete,
2377 &s->thread_args.commits_needed,
2378 &s->thread_args.need_commits,
2379 &s->thread_args.commit_loaded);
2380 if (err)
2381 break;
2382 if (first == s->first_displayed_entry &&
2383 s->selected < MIN(view->nlines - 2,
2384 s->commits.ncommits - 1)) {
2385 /* can't scroll further down */
2386 s->selected = MIN(view->nlines - 2,
2387 s->commits.ncommits - 1);
2389 err = NULL;
2390 break;
2392 case KEY_RESIZE:
2393 if (s->selected > view->nlines - 2)
2394 s->selected = view->nlines - 2;
2395 if (s->selected > s->commits.ncommits - 1)
2396 s->selected = s->commits.ncommits - 1;
2397 break;
2398 case KEY_ENTER:
2399 case ' ':
2400 case '\r':
2401 if (s->selected_entry == NULL)
2402 break;
2403 if (view_is_parent_view(view))
2404 begin_x = view_split_begin_x(view->begin_x);
2405 err = open_diff_view_for_commit(&diff_view, begin_x,
2406 s->selected_entry->commit, s->selected_entry->id,
2407 view, s->refs, s->repo);
2408 if (err)
2409 break;
2410 if (view_is_parent_view(view)) {
2411 err = view_close_child(view);
2412 if (err)
2413 return err;
2414 err = view_set_child(view, diff_view);
2415 if (err) {
2416 view_close(diff_view);
2417 break;
2419 *focus_view = diff_view;
2420 view->child_focussed = 1;
2421 } else
2422 *new_view = diff_view;
2423 break;
2424 case 't':
2425 if (s->selected_entry == NULL)
2426 break;
2427 if (view_is_parent_view(view))
2428 begin_x = view_split_begin_x(view->begin_x);
2429 err = browse_commit_tree(&tree_view, begin_x,
2430 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2431 if (err)
2432 break;
2433 if (view_is_parent_view(view)) {
2434 err = view_close_child(view);
2435 if (err)
2436 return err;
2437 err = view_set_child(view, tree_view);
2438 if (err) {
2439 view_close(tree_view);
2440 break;
2442 *focus_view = tree_view;
2443 view->child_focussed = 1;
2444 } else
2445 *new_view = tree_view;
2446 break;
2447 case KEY_BACKSPACE:
2448 if (got_path_cmp(s->in_repo_path, "/",
2449 strlen(s->in_repo_path), 1) == 0)
2450 break;
2451 err = got_path_dirname(&parent_path, s->in_repo_path);
2452 if (err)
2453 return err;
2454 err = stop_log_thread(s);
2455 if (err) {
2456 free(parent_path);
2457 return err;
2459 lv = view_open(view->nlines, view->ncols,
2460 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2461 if (lv == NULL) {
2462 free(parent_path);
2463 return got_error_from_errno("view_open");
2465 err = open_log_view(lv, s->start_id, s->refs,
2466 s->repo, s->head_ref_name, parent_path,
2467 s->log_branches);
2468 free(parent_path);
2469 if (err)
2470 return err;;
2471 if (view_is_parent_view(view))
2472 *new_view = lv;
2473 else {
2474 view_set_child(view->parent, lv);
2475 *focus_view = lv;
2477 break;
2478 case CTRL('l'):
2479 err = stop_log_thread(s);
2480 if (err)
2481 return err;
2482 lv = view_open(view->nlines, view->ncols,
2483 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2484 if (lv == NULL)
2485 return got_error_from_errno("view_open");
2486 err = got_repo_match_object_id(&start_id, NULL,
2487 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2488 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2489 if (err) {
2490 view_close(lv);
2491 return err;
2493 in_repo_path = strdup(s->in_repo_path);
2494 if (in_repo_path == NULL) {
2495 free(start_id);
2496 view_close(lv);
2497 return got_error_from_errno("strdup");
2499 got_ref_list_free(s->refs);
2500 err = got_ref_list(s->refs, s->repo, NULL,
2501 got_ref_cmp_by_name, NULL);
2502 if (err) {
2503 free(start_id);
2504 view_close(lv);
2505 return err;
2507 err = open_log_view(lv, start_id, s->refs, s->repo,
2508 s->head_ref_name, in_repo_path, s->log_branches);
2509 if (err) {
2510 free(start_id);
2511 view_close(lv);
2512 return err;;
2514 *dead_view = view;
2515 *new_view = lv;
2516 break;
2517 case 'B':
2518 s->log_branches = !s->log_branches;
2519 err = stop_log_thread(s);
2520 if (err)
2521 return err;
2522 lv = view_open(view->nlines, view->ncols,
2523 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2524 if (lv == NULL)
2525 return got_error_from_errno("view_open");
2526 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2527 s->head_ref_name, s->in_repo_path, s->log_branches);
2528 if (err) {
2529 view_close(lv);
2530 return err;;
2532 *dead_view = view;
2533 *new_view = lv;
2534 break;
2535 default:
2536 break;
2539 return err;
2542 static const struct got_error *
2543 apply_unveil(const char *repo_path, const char *worktree_path)
2545 const struct got_error *error;
2547 #ifdef PROFILE
2548 if (unveil("gmon.out", "rwc") != 0)
2549 return got_error_from_errno2("unveil", "gmon.out");
2550 #endif
2551 if (repo_path && unveil(repo_path, "r") != 0)
2552 return got_error_from_errno2("unveil", repo_path);
2554 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2555 return got_error_from_errno2("unveil", worktree_path);
2557 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2558 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2560 error = got_privsep_unveil_exec_helpers();
2561 if (error != NULL)
2562 return error;
2564 if (unveil(NULL, NULL) != 0)
2565 return got_error_from_errno("unveil");
2567 return NULL;
2570 static void
2571 init_curses(void)
2573 initscr();
2574 cbreak();
2575 halfdelay(1); /* Do fast refresh while initial view is loading. */
2576 noecho();
2577 nonl();
2578 intrflush(stdscr, FALSE);
2579 keypad(stdscr, TRUE);
2580 curs_set(0);
2581 if (getenv("TOG_COLORS") != NULL) {
2582 start_color();
2583 use_default_colors();
2585 signal(SIGWINCH, tog_sigwinch);
2586 signal(SIGPIPE, tog_sigpipe);
2587 signal(SIGCONT, tog_sigcont);
2590 static const struct got_error *
2591 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2592 struct got_repository *repo, struct got_worktree *worktree)
2594 const struct got_error *err = NULL;
2596 if (argc == 0) {
2597 *in_repo_path = strdup("/");
2598 if (*in_repo_path == NULL)
2599 return got_error_from_errno("strdup");
2600 return NULL;
2603 if (worktree) {
2604 const char *prefix = got_worktree_get_path_prefix(worktree);
2605 char *p;
2607 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2608 if (err)
2609 return err;
2610 if (asprintf(in_repo_path, "%s%s%s", prefix,
2611 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2612 p) == -1) {
2613 err = got_error_from_errno("asprintf");
2614 *in_repo_path = NULL;
2616 free(p);
2617 } else
2618 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2620 return err;
2623 static const struct got_error *
2624 cmd_log(int argc, char *argv[])
2626 const struct got_error *error;
2627 struct got_repository *repo = NULL;
2628 struct got_worktree *worktree = NULL;
2629 struct got_reflist_head refs;
2630 struct got_object_id *start_id = NULL;
2631 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2632 char *start_commit = NULL, *head_ref_name = NULL;
2633 int ch, log_branches = 0;
2634 struct tog_view *view;
2636 SIMPLEQ_INIT(&refs);
2638 #ifndef PROFILE
2639 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2640 NULL) == -1)
2641 err(1, "pledge");
2642 #endif
2644 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2645 switch (ch) {
2646 case 'b':
2647 log_branches = 1;
2648 break;
2649 case 'c':
2650 start_commit = optarg;
2651 break;
2652 case 'r':
2653 repo_path = realpath(optarg, NULL);
2654 if (repo_path == NULL)
2655 return got_error_from_errno2("realpath",
2656 optarg);
2657 break;
2658 default:
2659 usage_log();
2660 /* NOTREACHED */
2664 argc -= optind;
2665 argv += optind;
2667 if (argc > 1)
2668 usage_log();
2670 cwd = getcwd(NULL, 0);
2671 if (cwd == NULL)
2672 return got_error_from_errno("getcwd");
2674 error = got_worktree_open(&worktree, cwd);
2675 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2676 goto done;
2678 if (repo_path == NULL) {
2679 if (worktree)
2680 repo_path =
2681 strdup(got_worktree_get_repo_path(worktree));
2682 else
2683 repo_path = strdup(cwd);
2685 if (repo_path == NULL) {
2686 error = got_error_from_errno("strdup");
2687 goto done;
2690 error = got_repo_open(&repo, repo_path, NULL);
2691 if (error != NULL)
2692 goto done;
2694 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2695 repo, worktree);
2696 if (error)
2697 goto done;
2699 init_curses();
2701 error = apply_unveil(got_repo_get_path(repo),
2702 worktree ? got_worktree_get_root_path(worktree) : NULL);
2703 if (error)
2704 goto done;
2706 if (start_commit == NULL)
2707 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2708 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2709 GOT_OBJ_TYPE_COMMIT, 1, repo);
2710 else
2711 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2712 GOT_OBJ_TYPE_COMMIT, 1, repo);
2713 if (error != NULL)
2714 goto done;
2716 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2717 if (error)
2718 goto done;
2720 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2721 if (view == NULL) {
2722 error = got_error_from_errno("view_open");
2723 goto done;
2725 if (worktree) {
2726 head_ref_name = strdup(
2727 got_worktree_get_head_ref_name(worktree));
2728 if (head_ref_name == NULL) {
2729 error = got_error_from_errno("strdup");
2730 goto done;
2733 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2734 in_repo_path, log_branches);
2735 if (error)
2736 goto done;
2737 if (worktree) {
2738 /* Release work tree lock. */
2739 got_worktree_close(worktree);
2740 worktree = NULL;
2742 error = view_loop(view);
2743 done:
2744 free(in_repo_path);
2745 free(repo_path);
2746 free(cwd);
2747 free(start_id);
2748 free(head_ref_name);
2749 if (repo)
2750 got_repo_close(repo);
2751 if (worktree)
2752 got_worktree_close(worktree);
2753 got_ref_list_free(&refs);
2754 return error;
2757 __dead static void
2758 usage_diff(void)
2760 endwin();
2761 fprintf(stderr, "usage: %s diff [-a] [-r repository-path] "
2762 "object1 object2\n", getprogname());
2763 exit(1);
2766 static char *
2767 parse_next_line(FILE *f, size_t *len)
2769 char *line;
2770 size_t linelen;
2771 size_t lineno;
2772 const char delim[3] = { '\0', '\0', '\0'};
2774 line = fparseln(f, &linelen, &lineno, delim, 0);
2775 if (len)
2776 *len = linelen;
2777 return line;
2780 static int
2781 match_line(const char *line, regex_t *regex, size_t nmatch,
2782 regmatch_t *regmatch)
2784 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2787 struct tog_color *
2788 match_color(struct tog_colors *colors, const char *line)
2790 struct tog_color *tc = NULL;
2792 SIMPLEQ_FOREACH(tc, colors, entry) {
2793 if (match_line(line, &tc->regex, 0, NULL))
2794 return tc;
2797 return NULL;
2800 static const struct got_error *
2801 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2802 WINDOW *window, regmatch_t *regmatch)
2804 const struct got_error *err = NULL;
2805 wchar_t *wline;
2806 int width;
2807 char *s;
2809 *wtotal = 0;
2811 s = strndup(line, regmatch->rm_so);
2812 if (s == NULL)
2813 return got_error_from_errno("strndup");
2815 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2816 if (err) {
2817 free(s);
2818 return err;
2820 waddwstr(window, wline);
2821 free(wline);
2822 free(s);
2823 wlimit -= width;
2824 *wtotal += width;
2826 if (wlimit > 0) {
2827 s = strndup(line + regmatch->rm_so,
2828 regmatch->rm_eo - regmatch->rm_so);
2829 if (s == NULL) {
2830 err = got_error_from_errno("strndup");
2831 free(s);
2832 return err;
2834 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2835 if (err) {
2836 free(s);
2837 return err;
2839 wattr_on(window, A_STANDOUT, NULL);
2840 waddwstr(window, wline);
2841 wattr_off(window, A_STANDOUT, NULL);
2842 free(wline);
2843 free(s);
2844 wlimit -= width;
2845 *wtotal += width;
2848 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2849 err = format_line(&wline, &width,
2850 line + regmatch->rm_eo, wlimit, col_tab_align);
2851 if (err)
2852 return err;
2853 waddwstr(window, wline);
2854 free(wline);
2855 *wtotal += width;
2858 return NULL;
2861 static const struct got_error *
2862 draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2863 off_t *line_offsets, int selected_line, int max_lines,
2864 int *last_displayed_line, int *eof, char *header,
2865 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
2867 const struct got_error *err;
2868 int nprinted = 0;
2869 char *line;
2870 struct tog_color *tc;
2871 size_t len;
2872 wchar_t *wline;
2873 int width;
2874 off_t line_offset;
2876 line_offset = line_offsets[first_displayed_line - 1];
2877 if (fseeko(f, line_offset, SEEK_SET) == -1)
2878 return got_error_from_errno("fseek");
2880 werase(view->window);
2882 if (header) {
2883 if (asprintf(&line, "[%d/%d] %s",
2884 first_displayed_line - 1 + selected_line, nlines,
2885 header) == -1)
2886 return got_error_from_errno("asprintf");
2887 err = format_line(&wline, &width, line, view->ncols, 0);
2888 free(line);
2889 if (err)
2890 return err;
2892 if (view_needs_focus_indication(view))
2893 wstandout(view->window);
2894 waddwstr(view->window, wline);
2895 free(wline);
2896 wline = NULL;
2897 if (view_needs_focus_indication(view))
2898 wstandend(view->window);
2899 if (width <= view->ncols - 1)
2900 waddch(view->window, '\n');
2902 if (max_lines <= 1)
2903 return NULL;
2904 max_lines--;
2907 *eof = 0;
2908 while (max_lines > 0 && nprinted < max_lines) {
2909 line = parse_next_line(f, &len);
2910 if (line == NULL) {
2911 *eof = 1;
2912 break;
2915 tc = match_color(colors, line);
2916 if (tc)
2917 wattr_on(view->window,
2918 COLOR_PAIR(tc->colorpair), NULL);
2919 if (first_displayed_line + nprinted == matched_line &&
2920 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2921 err = add_matched_line(&width, line, view->ncols, 0,
2922 view->window, regmatch);
2923 if (err) {
2924 free(line);
2925 return err;
2927 } else {
2928 err = format_line(&wline, &width, line, view->ncols, 0);
2929 if (err) {
2930 free(line);
2931 return err;
2933 waddwstr(view->window, wline);
2934 free(wline);
2935 wline = NULL;
2937 if (tc)
2938 wattr_off(view->window,
2939 COLOR_PAIR(tc->colorpair), NULL);
2940 if (width <= view->ncols - 1)
2941 waddch(view->window, '\n');
2942 nprinted++;
2943 free(line);
2945 if (nprinted >= 1)
2946 *last_displayed_line = first_displayed_line + (nprinted - 1);
2947 else
2948 *last_displayed_line = first_displayed_line;
2950 view_vborder(view);
2952 if (*eof) {
2953 while (nprinted < view->nlines) {
2954 waddch(view->window, '\n');
2955 nprinted++;
2958 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2959 if (err) {
2960 return err;
2963 wstandout(view->window);
2964 waddwstr(view->window, wline);
2965 free(wline);
2966 wline = NULL;
2967 wstandend(view->window);
2970 return NULL;
2973 static char *
2974 get_datestr(time_t *time, char *datebuf)
2976 struct tm mytm, *tm;
2977 char *p, *s;
2979 tm = gmtime_r(time, &mytm);
2980 if (tm == NULL)
2981 return NULL;
2982 s = asctime_r(tm, datebuf);
2983 if (s == NULL)
2984 return NULL;
2985 p = strchr(s, '\n');
2986 if (p)
2987 *p = '\0';
2988 return s;
2991 static const struct got_error *
2992 get_changed_paths(struct got_pathlist_head *paths,
2993 struct got_commit_object *commit, struct got_repository *repo)
2995 const struct got_error *err = NULL;
2996 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2997 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2998 struct got_object_qid *qid;
3000 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3001 if (qid != NULL) {
3002 struct got_commit_object *pcommit;
3003 err = got_object_open_as_commit(&pcommit, repo,
3004 qid->id);
3005 if (err)
3006 return err;
3008 tree_id1 = got_object_commit_get_tree_id(pcommit);
3009 got_object_commit_close(pcommit);
3013 if (tree_id1) {
3014 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3015 if (err)
3016 goto done;
3019 tree_id2 = got_object_commit_get_tree_id(commit);
3020 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3021 if (err)
3022 goto done;
3024 err = got_diff_tree(tree1, tree2, "", "", repo,
3025 got_diff_tree_collect_changed_paths, paths, 0);
3026 done:
3027 if (tree1)
3028 got_object_tree_close(tree1);
3029 if (tree2)
3030 got_object_tree_close(tree2);
3031 return err;
3034 static const struct got_error *
3035 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3037 off_t *p;
3039 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3040 if (p == NULL)
3041 return got_error_from_errno("reallocarray");
3042 *line_offsets = p;
3043 (*line_offsets)[*nlines] = off;
3044 (*nlines)++;
3045 return NULL;
3048 static const struct got_error *
3049 write_commit_info(off_t **line_offsets, size_t *nlines,
3050 struct got_object_id *commit_id, struct got_reflist_head *refs,
3051 struct got_repository *repo, FILE *outfile)
3053 const struct got_error *err = NULL;
3054 char datebuf[26], *datestr;
3055 struct got_commit_object *commit;
3056 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3057 time_t committer_time;
3058 const char *author, *committer;
3059 char *refs_str = NULL;
3060 struct got_pathlist_head changed_paths;
3061 struct got_pathlist_entry *pe;
3062 off_t outoff = 0;
3063 int n;
3065 TAILQ_INIT(&changed_paths);
3067 if (refs) {
3068 err = build_refs_str(&refs_str, refs, commit_id, repo);
3069 if (err)
3070 return err;
3073 err = got_object_open_as_commit(&commit, repo, commit_id);
3074 if (err)
3075 return err;
3077 err = got_object_id_str(&id_str, commit_id);
3078 if (err) {
3079 err = got_error_from_errno("got_object_id_str");
3080 goto done;
3083 err = add_line_offset(line_offsets, nlines, 0);
3084 if (err)
3085 goto done;
3087 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3088 refs_str ? refs_str : "", refs_str ? ")" : "");
3089 if (n < 0) {
3090 err = got_error_from_errno("fprintf");
3091 goto done;
3093 outoff += n;
3094 err = add_line_offset(line_offsets, nlines, outoff);
3095 if (err)
3096 goto done;
3098 n = fprintf(outfile, "from: %s\n",
3099 got_object_commit_get_author(commit));
3100 if (n < 0) {
3101 err = got_error_from_errno("fprintf");
3102 goto done;
3104 outoff += n;
3105 err = add_line_offset(line_offsets, nlines, outoff);
3106 if (err)
3107 goto done;
3109 committer_time = got_object_commit_get_committer_time(commit);
3110 datestr = get_datestr(&committer_time, datebuf);
3111 if (datestr) {
3112 n = fprintf(outfile, "date: %s UTC\n", datestr);
3113 if (n < 0) {
3114 err = got_error_from_errno("fprintf");
3115 goto done;
3117 outoff += n;
3118 err = add_line_offset(line_offsets, nlines, outoff);
3119 if (err)
3120 goto done;
3122 author = got_object_commit_get_author(commit);
3123 committer = got_object_commit_get_committer(commit);
3124 if (strcmp(author, committer) != 0) {
3125 n = fprintf(outfile, "via: %s\n", committer);
3126 if (n < 0) {
3127 err = got_error_from_errno("fprintf");
3128 goto done;
3130 outoff += n;
3131 err = add_line_offset(line_offsets, nlines, outoff);
3132 if (err)
3133 goto done;
3135 err = got_object_commit_get_logmsg(&logmsg, commit);
3136 if (err)
3137 goto done;
3138 s = logmsg;
3139 while ((line = strsep(&s, "\n")) != NULL) {
3140 n = fprintf(outfile, "%s\n", line);
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;
3151 err = get_changed_paths(&changed_paths, commit, repo);
3152 if (err)
3153 goto done;
3154 TAILQ_FOREACH(pe, &changed_paths, entry) {
3155 struct got_diff_changed_path *cp = pe->data;
3156 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3157 if (n < 0) {
3158 err = got_error_from_errno("fprintf");
3159 goto done;
3161 outoff += n;
3162 err = add_line_offset(line_offsets, nlines, outoff);
3163 if (err)
3164 goto done;
3165 free((char *)pe->path);
3166 free(pe->data);
3169 fputc('\n', outfile);
3170 outoff++;
3171 err = add_line_offset(line_offsets, nlines, outoff);
3172 done:
3173 got_pathlist_free(&changed_paths);
3174 free(id_str);
3175 free(logmsg);
3176 free(refs_str);
3177 got_object_commit_close(commit);
3178 if (err) {
3179 free(*line_offsets);
3180 *line_offsets = NULL;
3181 *nlines = 0;
3183 return err;
3186 static const struct got_error *
3187 create_diff(struct tog_diff_view_state *s)
3189 const struct got_error *err = NULL;
3190 FILE *f = NULL;
3191 int obj_type;
3193 free(s->line_offsets);
3194 s->line_offsets = malloc(sizeof(off_t));
3195 if (s->line_offsets == NULL)
3196 return got_error_from_errno("malloc");
3197 s->nlines = 0;
3199 f = got_opentemp();
3200 if (f == NULL) {
3201 err = got_error_from_errno("got_opentemp");
3202 goto done;
3204 if (s->f && fclose(s->f) != 0) {
3205 err = got_error_from_errno("fclose");
3206 goto done;
3208 s->f = f;
3210 if (s->id1)
3211 err = got_object_get_type(&obj_type, s->repo, s->id1);
3212 else
3213 err = got_object_get_type(&obj_type, s->repo, s->id2);
3214 if (err)
3215 goto done;
3217 switch (obj_type) {
3218 case GOT_OBJ_TYPE_BLOB:
3219 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3220 s->id1, s->id2, NULL, NULL, s->diff_context, 0,
3221 s->force_text_diff, s->repo, s->f);
3222 break;
3223 case GOT_OBJ_TYPE_TREE:
3224 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3225 s->id1, s->id2, "", "", s->diff_context, 0,
3226 s->force_text_diff, s->repo, s->f);
3227 break;
3228 case GOT_OBJ_TYPE_COMMIT: {
3229 const struct got_object_id_queue *parent_ids;
3230 struct got_object_qid *pid;
3231 struct got_commit_object *commit2;
3233 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3234 if (err)
3235 goto done;
3236 /* Show commit info if we're diffing to a parent/root commit. */
3237 if (s->id1 == NULL) {
3238 err = write_commit_info(&s->line_offsets, &s->nlines,
3239 s->id2, s->refs, s->repo, s->f);
3240 if (err)
3241 goto done;
3242 } else {
3243 parent_ids = got_object_commit_get_parent_ids(commit2);
3244 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3245 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3246 err = write_commit_info(
3247 &s->line_offsets, &s->nlines,
3248 s->id2, s->refs, s->repo, s->f);
3249 if (err)
3250 goto done;
3251 break;
3255 got_object_commit_close(commit2);
3257 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3258 s->id1, s->id2, s->diff_context, 0, s->force_text_diff,
3259 s->repo, s->f);
3260 break;
3262 default:
3263 err = got_error(GOT_ERR_OBJ_TYPE);
3264 break;
3266 if (err)
3267 goto done;
3268 done:
3269 if (s->f && fflush(s->f) != 0 && err == NULL)
3270 err = got_error_from_errno("fflush");
3271 return err;
3274 static void
3275 diff_view_indicate_progress(struct tog_view *view)
3277 mvwaddstr(view->window, 0, 0, "diffing...");
3278 update_panels();
3279 doupdate();
3282 static const struct got_error *
3283 search_start_diff_view(struct tog_view *view)
3285 struct tog_diff_view_state *s = &view->state.diff;
3287 s->matched_line = 0;
3288 return NULL;
3291 static const struct got_error *
3292 search_next_diff_view(struct tog_view *view)
3294 struct tog_diff_view_state *s = &view->state.diff;
3295 int lineno;
3297 if (!view->searching) {
3298 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3299 return NULL;
3302 if (s->matched_line) {
3303 if (view->searching == TOG_SEARCH_FORWARD)
3304 lineno = s->matched_line + 1;
3305 else
3306 lineno = s->matched_line - 1;
3307 } else {
3308 if (view->searching == TOG_SEARCH_FORWARD)
3309 lineno = 1;
3310 else
3311 lineno = s->nlines;
3314 while (1) {
3315 char *line = NULL;
3316 off_t offset;
3317 size_t len;
3319 if (lineno <= 0 || lineno > s->nlines) {
3320 if (s->matched_line == 0) {
3321 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3322 free(line);
3323 break;
3326 if (view->searching == TOG_SEARCH_FORWARD)
3327 lineno = 1;
3328 else
3329 lineno = s->nlines;
3332 offset = s->line_offsets[lineno - 1];
3333 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3334 free(line);
3335 return got_error_from_errno("fseeko");
3337 free(line);
3338 line = parse_next_line(s->f, &len);
3339 if (line &&
3340 match_line(line, &view->regex, 1, &view->regmatch)) {
3341 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3342 s->matched_line = lineno;
3343 free(line);
3344 break;
3346 free(line);
3347 if (view->searching == TOG_SEARCH_FORWARD)
3348 lineno++;
3349 else
3350 lineno--;
3353 if (s->matched_line) {
3354 s->first_displayed_line = s->matched_line;
3355 s->selected_line = 1;
3358 return NULL;
3361 static const struct got_error *
3362 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3363 struct got_object_id *id2, int force_text_diff, struct tog_view *log_view,
3364 struct got_reflist_head *refs, struct got_repository *repo)
3366 const struct got_error *err;
3367 struct tog_diff_view_state *s = &view->state.diff;
3369 if (id1 != NULL && id2 != NULL) {
3370 int type1, type2;
3371 err = got_object_get_type(&type1, repo, id1);
3372 if (err)
3373 return err;
3374 err = got_object_get_type(&type2, repo, id2);
3375 if (err)
3376 return err;
3378 if (type1 != type2)
3379 return got_error(GOT_ERR_OBJ_TYPE);
3381 s->first_displayed_line = 1;
3382 s->last_displayed_line = view->nlines;
3383 s->selected_line = 1;
3384 s->repo = repo;
3385 s->refs = refs;
3386 s->id1 = id1;
3387 s->id2 = id2;
3389 if (id1) {
3390 s->id1 = got_object_id_dup(id1);
3391 if (s->id1 == NULL)
3392 return got_error_from_errno("got_object_id_dup");
3393 } else
3394 s->id1 = NULL;
3396 s->id2 = got_object_id_dup(id2);
3397 if (s->id2 == NULL) {
3398 free(s->id1);
3399 s->id1 = NULL;
3400 return got_error_from_errno("got_object_id_dup");
3402 s->f = NULL;
3403 s->first_displayed_line = 1;
3404 s->last_displayed_line = view->nlines;
3405 s->diff_context = 3;
3406 s->force_text_diff = force_text_diff;
3407 s->log_view = log_view;
3408 s->repo = repo;
3409 s->refs = refs;
3411 SIMPLEQ_INIT(&s->colors);
3412 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3413 err = add_color(&s->colors,
3414 "^-", TOG_COLOR_DIFF_MINUS,
3415 get_color_value("TOG_COLOR_DIFF_MINUS"));
3416 if (err)
3417 return err;
3418 err = add_color(&s->colors, "^\\+",
3419 TOG_COLOR_DIFF_PLUS,
3420 get_color_value("TOG_COLOR_DIFF_PLUS"));
3421 if (err) {
3422 free_colors(&s->colors);
3423 return err;
3425 err = add_color(&s->colors,
3426 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3427 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3428 if (err) {
3429 free_colors(&s->colors);
3430 return err;
3433 err = add_color(&s->colors,
3434 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3435 TOG_COLOR_DIFF_META,
3436 get_color_value("TOG_COLOR_DIFF_META"));
3437 if (err) {
3438 free_colors(&s->colors);
3439 return err;
3442 err = add_color(&s->colors,
3443 "^(from|via): ", TOG_COLOR_AUTHOR,
3444 get_color_value("TOG_COLOR_AUTHOR"));
3445 if (err) {
3446 free_colors(&s->colors);
3447 return err;
3450 err = add_color(&s->colors,
3451 "^date: ", TOG_COLOR_DATE,
3452 get_color_value("TOG_COLOR_DATE"));
3453 if (err) {
3454 free_colors(&s->colors);
3455 return err;
3459 if (log_view && view_is_splitscreen(view))
3460 show_log_view(log_view); /* draw vborder */
3461 diff_view_indicate_progress(view);
3463 s->line_offsets = NULL;
3464 s->nlines = 0;
3465 err = create_diff(s);
3466 if (err) {
3467 free(s->id1);
3468 s->id1 = NULL;
3469 free(s->id2);
3470 s->id2 = NULL;
3471 return err;
3474 view->show = show_diff_view;
3475 view->input = input_diff_view;
3476 view->close = close_diff_view;
3477 view->search_start = search_start_diff_view;
3478 view->search_next = search_next_diff_view;
3480 return NULL;
3483 static const struct got_error *
3484 close_diff_view(struct tog_view *view)
3486 const struct got_error *err = NULL;
3487 struct tog_diff_view_state *s = &view->state.diff;
3489 free(s->id1);
3490 s->id1 = NULL;
3491 free(s->id2);
3492 s->id2 = NULL;
3493 if (s->f && fclose(s->f) == EOF)
3494 err = got_error_from_errno("fclose");
3495 free_colors(&s->colors);
3496 free(s->line_offsets);
3497 s->line_offsets = NULL;
3498 s->nlines = 0;
3499 return err;
3502 static const struct got_error *
3503 show_diff_view(struct tog_view *view)
3505 const struct got_error *err;
3506 struct tog_diff_view_state *s = &view->state.diff;
3507 char *id_str1 = NULL, *id_str2, *header;
3509 if (s->id1) {
3510 err = got_object_id_str(&id_str1, s->id1);
3511 if (err)
3512 return err;
3514 err = got_object_id_str(&id_str2, s->id2);
3515 if (err)
3516 return err;
3518 if (asprintf(&header, "diff %s %s",
3519 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3520 err = got_error_from_errno("asprintf");
3521 free(id_str1);
3522 free(id_str2);
3523 return err;
3525 free(id_str1);
3526 free(id_str2);
3528 return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3529 s->line_offsets, s->selected_line, view->nlines,
3530 &s->last_displayed_line, &s->eof, header, &s->colors,
3531 s->matched_line, &view->regmatch);
3534 static const struct got_error *
3535 set_selected_commit(struct tog_diff_view_state *s,
3536 struct commit_queue_entry *entry)
3538 const struct got_error *err;
3539 const struct got_object_id_queue *parent_ids;
3540 struct got_commit_object *selected_commit;
3541 struct got_object_qid *pid;
3543 free(s->id2);
3544 s->id2 = got_object_id_dup(entry->id);
3545 if (s->id2 == NULL)
3546 return got_error_from_errno("got_object_id_dup");
3548 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3549 if (err)
3550 return err;
3551 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3552 free(s->id1);
3553 pid = SIMPLEQ_FIRST(parent_ids);
3554 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3555 got_object_commit_close(selected_commit);
3556 return NULL;
3559 static const struct got_error *
3560 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3561 struct tog_view **focus_view, struct tog_view *view, int ch)
3563 const struct got_error *err = NULL;
3564 struct tog_diff_view_state *s = &view->state.diff;
3565 struct tog_log_view_state *ls;
3566 struct commit_queue_entry *entry;
3567 int i;
3569 switch (ch) {
3570 case 'a':
3571 s->force_text_diff = !s->force_text_diff;
3572 wclear(view->window);
3573 s->first_displayed_line = 1;
3574 s->last_displayed_line = view->nlines;
3575 diff_view_indicate_progress(view);
3576 err = create_diff(s);
3577 break;
3578 case 'k':
3579 case KEY_UP:
3580 if (s->first_displayed_line > 1)
3581 s->first_displayed_line--;
3582 break;
3583 case KEY_PPAGE:
3584 case CTRL('b'):
3585 if (s->first_displayed_line == 1)
3586 break;
3587 i = 0;
3588 while (i++ < view->nlines - 1 &&
3589 s->first_displayed_line > 1)
3590 s->first_displayed_line--;
3591 break;
3592 case 'j':
3593 case KEY_DOWN:
3594 if (!s->eof)
3595 s->first_displayed_line++;
3596 break;
3597 case KEY_NPAGE:
3598 case CTRL('f'):
3599 case ' ':
3600 if (s->eof)
3601 break;
3602 i = 0;
3603 while (!s->eof && i++ < view->nlines - 1) {
3604 char *line;
3605 line = parse_next_line(s->f, NULL);
3606 s->first_displayed_line++;
3607 if (line == NULL)
3608 break;
3610 break;
3611 case '[':
3612 if (s->diff_context > 0) {
3613 s->diff_context--;
3614 diff_view_indicate_progress(view);
3615 err = create_diff(s);
3616 if (s->first_displayed_line + view->nlines - 1 >
3617 s->nlines) {
3618 s->first_displayed_line = 1;
3619 s->last_displayed_line = view->nlines;
3622 break;
3623 case ']':
3624 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3625 s->diff_context++;
3626 diff_view_indicate_progress(view);
3627 err = create_diff(s);
3629 break;
3630 case '<':
3631 case ',':
3632 if (s->log_view == NULL)
3633 break;
3634 ls = &s->log_view->state.log;
3635 entry = TAILQ_PREV(ls->selected_entry,
3636 commit_queue_head, entry);
3637 if (entry == NULL)
3638 break;
3640 err = input_log_view(NULL, NULL, NULL, s->log_view,
3641 KEY_UP);
3642 if (err)
3643 break;
3645 err = set_selected_commit(s, entry);
3646 if (err)
3647 break;
3649 s->first_displayed_line = 1;
3650 s->last_displayed_line = view->nlines;
3652 diff_view_indicate_progress(view);
3653 err = create_diff(s);
3654 break;
3655 case '>':
3656 case '.':
3657 if (s->log_view == NULL)
3658 break;
3659 ls = &s->log_view->state.log;
3661 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3662 ls->thread_args.commits_needed++;
3663 err = trigger_log_thread(s->log_view, 1,
3664 &ls->thread_args.commits_needed,
3665 &ls->thread_args.log_complete,
3666 &ls->thread_args.need_commits,
3667 &ls->thread_args.commit_loaded);
3668 if (err)
3669 break;
3671 err = input_log_view(NULL, NULL, NULL, s->log_view,
3672 KEY_DOWN);
3673 if (err)
3674 break;
3676 entry = TAILQ_NEXT(ls->selected_entry, entry);
3677 if (entry == NULL)
3678 break;
3680 err = set_selected_commit(s, entry);
3681 if (err)
3682 break;
3684 s->first_displayed_line = 1;
3685 s->last_displayed_line = view->nlines;
3687 diff_view_indicate_progress(view);
3688 err = create_diff(s);
3689 break;
3690 default:
3691 break;
3694 return err;
3697 static const struct got_error *
3698 cmd_diff(int argc, char *argv[])
3700 const struct got_error *error = NULL;
3701 struct got_repository *repo = NULL;
3702 struct got_worktree *worktree = NULL;
3703 struct got_reflist_head refs;
3704 struct got_object_id *id1 = NULL, *id2 = NULL;
3705 char *repo_path = NULL, *cwd = NULL;
3706 char *id_str1 = NULL, *id_str2 = NULL;
3707 int ch, force_text_diff = 0;
3708 struct tog_view *view;
3710 SIMPLEQ_INIT(&refs);
3712 #ifndef PROFILE
3713 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3714 NULL) == -1)
3715 err(1, "pledge");
3716 #endif
3718 while ((ch = getopt(argc, argv, "ar:")) != -1) {
3719 switch (ch) {
3720 case 'a':
3721 force_text_diff = 1;
3722 break;
3723 case 'r':
3724 repo_path = realpath(optarg, NULL);
3725 if (repo_path == NULL)
3726 return got_error_from_errno2("realpath",
3727 optarg);
3728 break;
3729 default:
3730 usage_diff();
3731 /* NOTREACHED */
3735 argc -= optind;
3736 argv += optind;
3738 if (argc == 0) {
3739 usage_diff(); /* TODO show local worktree changes */
3740 } else if (argc == 2) {
3741 id_str1 = argv[0];
3742 id_str2 = argv[1];
3743 } else
3744 usage_diff();
3746 cwd = getcwd(NULL, 0);
3747 if (cwd == NULL)
3748 return got_error_from_errno("getcwd");
3750 error = got_worktree_open(&worktree, cwd);
3751 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3752 goto done;
3754 if (repo_path == NULL) {
3755 if (worktree)
3756 repo_path =
3757 strdup(got_worktree_get_repo_path(worktree));
3758 else
3759 repo_path = strdup(cwd);
3761 if (repo_path == NULL) {
3762 error = got_error_from_errno("strdup");
3763 goto done;
3766 error = got_repo_open(&repo, repo_path, NULL);
3767 if (error)
3768 goto done;
3770 init_curses();
3772 error = apply_unveil(got_repo_get_path(repo), NULL);
3773 if (error)
3774 goto done;
3776 error = got_repo_match_object_id_prefix(&id1, id_str1,
3777 GOT_OBJ_TYPE_ANY, repo);
3778 if (error)
3779 goto done;
3781 error = got_repo_match_object_id_prefix(&id2, id_str2,
3782 GOT_OBJ_TYPE_ANY, repo);
3783 if (error)
3784 goto done;
3786 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3787 if (error)
3788 goto done;
3790 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3791 if (view == NULL) {
3792 error = got_error_from_errno("view_open");
3793 goto done;
3795 error = open_diff_view(view, id1, id2, force_text_diff, NULL, &refs,
3796 repo);
3797 if (error)
3798 goto done;
3799 error = view_loop(view);
3800 done:
3801 free(repo_path);
3802 free(cwd);
3803 if (repo)
3804 got_repo_close(repo);
3805 if (worktree)
3806 got_worktree_close(worktree);
3807 got_ref_list_free(&refs);
3808 return error;
3811 __dead static void
3812 usage_blame(void)
3814 endwin();
3815 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3816 getprogname());
3817 exit(1);
3820 struct tog_blame_line {
3821 int annotated;
3822 struct got_object_id *id;
3825 static const struct got_error *
3826 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3827 const char *path, struct tog_blame_line *lines, int nlines,
3828 int blame_complete, int selected_line, int *first_displayed_line,
3829 int *last_displayed_line, int *eof, int max_lines,
3830 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
3832 const struct got_error *err;
3833 int lineno = 0, nprinted = 0;
3834 char *line;
3835 size_t len;
3836 wchar_t *wline;
3837 int width;
3838 struct tog_blame_line *blame_line;
3839 struct got_object_id *prev_id = NULL;
3840 char *id_str;
3841 struct tog_color *tc;
3843 err = got_object_id_str(&id_str, id);
3844 if (err)
3845 return err;
3847 rewind(f);
3848 werase(view->window);
3850 if (asprintf(&line, "commit %s", id_str) == -1) {
3851 err = got_error_from_errno("asprintf");
3852 free(id_str);
3853 return err;
3856 err = format_line(&wline, &width, line, view->ncols, 0);
3857 free(line);
3858 line = NULL;
3859 if (err)
3860 return err;
3861 if (view_needs_focus_indication(view))
3862 wstandout(view->window);
3863 tc = get_color(colors, TOG_COLOR_COMMIT);
3864 if (tc)
3865 wattr_on(view->window,
3866 COLOR_PAIR(tc->colorpair), NULL);
3867 waddwstr(view->window, wline);
3868 if (tc)
3869 wattr_off(view->window,
3870 COLOR_PAIR(tc->colorpair), NULL);
3871 if (view_needs_focus_indication(view))
3872 wstandend(view->window);
3873 free(wline);
3874 wline = NULL;
3875 if (width < view->ncols - 1)
3876 waddch(view->window, '\n');
3878 if (asprintf(&line, "[%d/%d] %s%s",
3879 *first_displayed_line - 1 + selected_line, nlines,
3880 blame_complete ? "" : "annotating... ", path) == -1) {
3881 free(id_str);
3882 return got_error_from_errno("asprintf");
3884 free(id_str);
3885 err = format_line(&wline, &width, line, view->ncols, 0);
3886 free(line);
3887 line = NULL;
3888 if (err)
3889 return err;
3890 waddwstr(view->window, wline);
3891 free(wline);
3892 wline = NULL;
3893 if (width < view->ncols - 1)
3894 waddch(view->window, '\n');
3896 *eof = 0;
3897 while (nprinted < max_lines - 2) {
3898 line = parse_next_line(f, &len);
3899 if (line == NULL) {
3900 *eof = 1;
3901 break;
3903 if (++lineno < *first_displayed_line) {
3904 free(line);
3905 continue;
3908 if (view->focussed && nprinted == selected_line - 1)
3909 wstandout(view->window);
3911 if (nlines > 0) {
3912 blame_line = &lines[lineno - 1];
3913 if (blame_line->annotated && prev_id &&
3914 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3915 !(view->focussed &&
3916 nprinted == selected_line - 1)) {
3917 waddstr(view->window, " ");
3918 } else if (blame_line->annotated) {
3919 char *id_str;
3920 err = got_object_id_str(&id_str, blame_line->id);
3921 if (err) {
3922 free(line);
3923 return err;
3925 tc = get_color(colors, TOG_COLOR_COMMIT);
3926 if (tc)
3927 wattr_on(view->window,
3928 COLOR_PAIR(tc->colorpair), NULL);
3929 wprintw(view->window, "%.8s", id_str);
3930 if (tc)
3931 wattr_off(view->window,
3932 COLOR_PAIR(tc->colorpair), NULL);
3933 free(id_str);
3934 prev_id = blame_line->id;
3935 } else {
3936 waddstr(view->window, "........");
3937 prev_id = NULL;
3939 } else {
3940 waddstr(view->window, "........");
3941 prev_id = NULL;
3944 if (view->focussed && nprinted == selected_line - 1)
3945 wstandend(view->window);
3946 waddstr(view->window, " ");
3948 if (view->ncols <= 9) {
3949 width = 9;
3950 wline = wcsdup(L"");
3951 if (wline == NULL) {
3952 err = got_error_from_errno("wcsdup");
3953 free(line);
3954 return err;
3956 } else if (*first_displayed_line + nprinted == matched_line &&
3957 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3958 err = add_matched_line(&width, line, view->ncols - 9, 9,
3959 view->window, regmatch);
3960 if (err) {
3961 free(line);
3962 return err;
3964 width += 9;
3965 } else {
3966 err = format_line(&wline, &width, line,
3967 view->ncols - 9, 9);
3968 waddwstr(view->window, wline);
3969 free(wline);
3970 wline = NULL;
3971 width += 9;
3974 if (width <= view->ncols - 1)
3975 waddch(view->window, '\n');
3976 if (++nprinted == 1)
3977 *first_displayed_line = lineno;
3978 free(line);
3980 *last_displayed_line = lineno;
3982 view_vborder(view);
3984 return NULL;
3987 static const struct got_error *
3988 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3990 const struct got_error *err = NULL;
3991 struct tog_blame_cb_args *a = arg;
3992 struct tog_blame_line *line;
3993 int errcode;
3995 if (nlines != a->nlines ||
3996 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3997 return got_error(GOT_ERR_RANGE);
3999 errcode = pthread_mutex_lock(&tog_mutex);
4000 if (errcode)
4001 return got_error_set_errno(errcode, "pthread_mutex_lock");
4003 if (*a->quit) { /* user has quit the blame view */
4004 err = got_error(GOT_ERR_ITER_COMPLETED);
4005 goto done;
4008 if (lineno == -1)
4009 goto done; /* no change in this commit */
4011 line = &a->lines[lineno - 1];
4012 if (line->annotated)
4013 goto done;
4015 line->id = got_object_id_dup(id);
4016 if (line->id == NULL) {
4017 err = got_error_from_errno("got_object_id_dup");
4018 goto done;
4020 line->annotated = 1;
4021 done:
4022 errcode = pthread_mutex_unlock(&tog_mutex);
4023 if (errcode)
4024 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4025 return err;
4028 static void *
4029 blame_thread(void *arg)
4031 const struct got_error *err;
4032 struct tog_blame_thread_args *ta = arg;
4033 struct tog_blame_cb_args *a = ta->cb_args;
4034 int errcode;
4036 err = block_signals_used_by_main_thread();
4037 if (err)
4038 return (void *)err;
4040 err = got_blame(ta->path, a->commit_id, ta->repo,
4041 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4042 if (err && err->code == GOT_ERR_CANCELLED)
4043 err = NULL;
4045 errcode = pthread_mutex_lock(&tog_mutex);
4046 if (errcode)
4047 return (void *)got_error_set_errno(errcode,
4048 "pthread_mutex_lock");
4050 got_repo_close(ta->repo);
4051 ta->repo = NULL;
4052 *ta->complete = 1;
4054 errcode = pthread_mutex_unlock(&tog_mutex);
4055 if (errcode && err == NULL)
4056 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4058 return (void *)err;
4061 static struct got_object_id *
4062 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4063 int first_displayed_line, int selected_line)
4065 struct tog_blame_line *line;
4067 if (nlines <= 0)
4068 return NULL;
4070 line = &lines[first_displayed_line - 1 + selected_line - 1];
4071 if (!line->annotated)
4072 return NULL;
4074 return line->id;
4077 static const struct got_error *
4078 stop_blame(struct tog_blame *blame)
4080 const struct got_error *err = NULL;
4081 int i;
4083 if (blame->thread) {
4084 int errcode;
4085 errcode = pthread_mutex_unlock(&tog_mutex);
4086 if (errcode)
4087 return got_error_set_errno(errcode,
4088 "pthread_mutex_unlock");
4089 errcode = pthread_join(blame->thread, (void **)&err);
4090 if (errcode)
4091 return got_error_set_errno(errcode, "pthread_join");
4092 errcode = pthread_mutex_lock(&tog_mutex);
4093 if (errcode)
4094 return got_error_set_errno(errcode,
4095 "pthread_mutex_lock");
4096 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4097 err = NULL;
4098 blame->thread = NULL;
4100 if (blame->thread_args.repo) {
4101 got_repo_close(blame->thread_args.repo);
4102 blame->thread_args.repo = NULL;
4104 if (blame->f) {
4105 if (fclose(blame->f) != 0 && err == NULL)
4106 err = got_error_from_errno("fclose");
4107 blame->f = NULL;
4109 if (blame->lines) {
4110 for (i = 0; i < blame->nlines; i++)
4111 free(blame->lines[i].id);
4112 free(blame->lines);
4113 blame->lines = NULL;
4115 free(blame->cb_args.commit_id);
4116 blame->cb_args.commit_id = NULL;
4118 return err;
4121 static const struct got_error *
4122 cancel_blame_view(void *arg)
4124 const struct got_error *err = NULL;
4125 int *done = arg;
4126 int errcode;
4128 errcode = pthread_mutex_lock(&tog_mutex);
4129 if (errcode)
4130 return got_error_set_errno(errcode,
4131 "pthread_mutex_unlock");
4133 if (*done)
4134 err = got_error(GOT_ERR_CANCELLED);
4136 errcode = pthread_mutex_unlock(&tog_mutex);
4137 if (errcode)
4138 return got_error_set_errno(errcode,
4139 "pthread_mutex_lock");
4141 return err;
4144 static const struct got_error *
4145 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
4146 int *first_displayed_line, int *last_displayed_line, int *selected_line,
4147 int *done, int *eof, const char *path, struct got_object_id *commit_id,
4148 struct got_repository *repo)
4150 const struct got_error *err = NULL;
4151 struct got_blob_object *blob = NULL;
4152 struct got_repository *thread_repo = NULL;
4153 struct got_object_id *obj_id = NULL;
4154 int obj_type;
4156 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
4157 if (err)
4158 return err;
4160 err = got_object_get_type(&obj_type, repo, obj_id);
4161 if (err)
4162 goto done;
4164 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4165 err = got_error(GOT_ERR_OBJ_TYPE);
4166 goto done;
4169 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4170 if (err)
4171 goto done;
4172 blame->f = got_opentemp();
4173 if (blame->f == NULL) {
4174 err = got_error_from_errno("got_opentemp");
4175 goto done;
4177 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4178 &blame->line_offsets, blame->f, blob);
4179 if (err || blame->nlines == 0)
4180 goto done;
4182 /* Don't include \n at EOF in the blame line count. */
4183 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4184 blame->nlines--;
4186 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4187 if (blame->lines == NULL) {
4188 err = got_error_from_errno("calloc");
4189 goto done;
4192 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4193 if (err)
4194 goto done;
4196 blame->cb_args.view = view;
4197 blame->cb_args.lines = blame->lines;
4198 blame->cb_args.nlines = blame->nlines;
4199 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4200 if (blame->cb_args.commit_id == NULL) {
4201 err = got_error_from_errno("got_object_id_dup");
4202 goto done;
4204 blame->cb_args.quit = done;
4206 blame->thread_args.path = path;
4207 blame->thread_args.repo = thread_repo;
4208 blame->thread_args.cb_args = &blame->cb_args;
4209 blame->thread_args.complete = blame_complete;
4210 blame->thread_args.cancel_cb = cancel_blame_view;
4211 blame->thread_args.cancel_arg = done;
4212 *blame_complete = 0;
4214 done:
4215 if (blob)
4216 got_object_blob_close(blob);
4217 free(obj_id);
4218 if (err)
4219 stop_blame(blame);
4220 return err;
4223 static const struct got_error *
4224 open_blame_view(struct tog_view *view, char *path,
4225 struct got_object_id *commit_id, struct got_reflist_head *refs,
4226 struct got_repository *repo)
4228 const struct got_error *err = NULL;
4229 struct tog_blame_view_state *s = &view->state.blame;
4231 SIMPLEQ_INIT(&s->blamed_commits);
4233 s->path = strdup(path);
4234 if (s->path == NULL)
4235 return got_error_from_errno("strdup");
4237 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4238 if (err) {
4239 free(s->path);
4240 return err;
4243 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4244 s->first_displayed_line = 1;
4245 s->last_displayed_line = view->nlines;
4246 s->selected_line = 1;
4247 s->blame_complete = 0;
4248 s->repo = repo;
4249 s->refs = refs;
4250 s->commit_id = commit_id;
4251 memset(&s->blame, 0, sizeof(s->blame));
4253 SIMPLEQ_INIT(&s->colors);
4254 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4255 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4256 get_color_value("TOG_COLOR_COMMIT"));
4257 if (err)
4258 return err;
4261 view->show = show_blame_view;
4262 view->input = input_blame_view;
4263 view->close = close_blame_view;
4264 view->search_start = search_start_blame_view;
4265 view->search_next = search_next_blame_view;
4267 return run_blame(&s->blame, view, &s->blame_complete,
4268 &s->first_displayed_line, &s->last_displayed_line,
4269 &s->selected_line, &s->done, &s->eof, s->path,
4270 s->blamed_commit->id, s->repo);
4273 static const struct got_error *
4274 close_blame_view(struct tog_view *view)
4276 const struct got_error *err = NULL;
4277 struct tog_blame_view_state *s = &view->state.blame;
4279 if (s->blame.thread)
4280 err = stop_blame(&s->blame);
4282 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4283 struct got_object_qid *blamed_commit;
4284 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4285 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4286 got_object_qid_free(blamed_commit);
4289 free(s->path);
4290 free_colors(&s->colors);
4292 return err;
4295 static const struct got_error *
4296 search_start_blame_view(struct tog_view *view)
4298 struct tog_blame_view_state *s = &view->state.blame;
4300 s->matched_line = 0;
4301 return NULL;
4304 static const struct got_error *
4305 search_next_blame_view(struct tog_view *view)
4307 struct tog_blame_view_state *s = &view->state.blame;
4308 int lineno;
4310 if (!view->searching) {
4311 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4312 return NULL;
4315 if (s->matched_line) {
4316 if (view->searching == TOG_SEARCH_FORWARD)
4317 lineno = s->matched_line + 1;
4318 else
4319 lineno = s->matched_line - 1;
4320 } else {
4321 if (view->searching == TOG_SEARCH_FORWARD)
4322 lineno = 1;
4323 else
4324 lineno = s->blame.nlines;
4327 while (1) {
4328 char *line = NULL;
4329 off_t offset;
4330 size_t len;
4332 if (lineno <= 0 || lineno > s->blame.nlines) {
4333 if (s->matched_line == 0) {
4334 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4335 free(line);
4336 break;
4339 if (view->searching == TOG_SEARCH_FORWARD)
4340 lineno = 1;
4341 else
4342 lineno = s->blame.nlines;
4345 offset = s->blame.line_offsets[lineno - 1];
4346 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4347 free(line);
4348 return got_error_from_errno("fseeko");
4350 free(line);
4351 line = parse_next_line(s->blame.f, &len);
4352 if (line &&
4353 match_line(line, &view->regex, 1, &view->regmatch)) {
4354 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4355 s->matched_line = lineno;
4356 free(line);
4357 break;
4359 free(line);
4360 if (view->searching == TOG_SEARCH_FORWARD)
4361 lineno++;
4362 else
4363 lineno--;
4366 if (s->matched_line) {
4367 s->first_displayed_line = s->matched_line;
4368 s->selected_line = 1;
4371 return NULL;
4374 static const struct got_error *
4375 show_blame_view(struct tog_view *view)
4377 const struct got_error *err = NULL;
4378 struct tog_blame_view_state *s = &view->state.blame;
4379 int errcode;
4381 if (s->blame.thread == NULL) {
4382 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4383 &s->blame.thread_args);
4384 if (errcode)
4385 return got_error_set_errno(errcode, "pthread_create");
4387 halfdelay(1); /* fast refresh while annotating */
4390 if (s->blame_complete)
4391 halfdelay(10); /* disable fast refresh */
4393 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4394 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4395 s->selected_line, &s->first_displayed_line,
4396 &s->last_displayed_line, &s->eof, view->nlines, &s->colors,
4397 s->matched_line, &view->regmatch);
4399 view_vborder(view);
4400 return err;
4403 static const struct got_error *
4404 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4405 struct tog_view **focus_view, struct tog_view *view, int ch)
4407 const struct got_error *err = NULL, *thread_err = NULL;
4408 struct tog_view *diff_view;
4409 struct tog_blame_view_state *s = &view->state.blame;
4410 int begin_x = 0;
4412 switch (ch) {
4413 case 'q':
4414 s->done = 1;
4415 break;
4416 case 'k':
4417 case KEY_UP:
4418 if (s->selected_line > 1)
4419 s->selected_line--;
4420 else if (s->selected_line == 1 &&
4421 s->first_displayed_line > 1)
4422 s->first_displayed_line--;
4423 break;
4424 case KEY_PPAGE:
4425 case CTRL('b'):
4426 if (s->first_displayed_line == 1) {
4427 s->selected_line = 1;
4428 break;
4430 if (s->first_displayed_line > view->nlines - 2)
4431 s->first_displayed_line -=
4432 (view->nlines - 2);
4433 else
4434 s->first_displayed_line = 1;
4435 break;
4436 case 'j':
4437 case KEY_DOWN:
4438 if (s->selected_line < view->nlines - 2 &&
4439 s->first_displayed_line +
4440 s->selected_line <= s->blame.nlines)
4441 s->selected_line++;
4442 else if (s->last_displayed_line <
4443 s->blame.nlines)
4444 s->first_displayed_line++;
4445 break;
4446 case 'b':
4447 case 'p': {
4448 struct got_object_id *id = NULL;
4449 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4450 s->first_displayed_line, s->selected_line);
4451 if (id == NULL)
4452 break;
4453 if (ch == 'p') {
4454 struct got_commit_object *commit;
4455 struct got_object_qid *pid;
4456 struct got_object_id *blob_id = NULL;
4457 int obj_type;
4458 err = got_object_open_as_commit(&commit,
4459 s->repo, id);
4460 if (err)
4461 break;
4462 pid = SIMPLEQ_FIRST(
4463 got_object_commit_get_parent_ids(commit));
4464 if (pid == NULL) {
4465 got_object_commit_close(commit);
4466 break;
4468 /* Check if path history ends here. */
4469 err = got_object_id_by_path(&blob_id, s->repo,
4470 pid->id, s->path);
4471 if (err) {
4472 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4473 err = NULL;
4474 got_object_commit_close(commit);
4475 break;
4477 err = got_object_get_type(&obj_type, s->repo,
4478 blob_id);
4479 free(blob_id);
4480 /* Can't blame non-blob type objects. */
4481 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4482 got_object_commit_close(commit);
4483 break;
4485 err = got_object_qid_alloc(&s->blamed_commit,
4486 pid->id);
4487 got_object_commit_close(commit);
4488 } else {
4489 if (got_object_id_cmp(id,
4490 s->blamed_commit->id) == 0)
4491 break;
4492 err = got_object_qid_alloc(&s->blamed_commit,
4493 id);
4495 if (err)
4496 break;
4497 s->done = 1;
4498 thread_err = stop_blame(&s->blame);
4499 s->done = 0;
4500 if (thread_err)
4501 break;
4502 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4503 s->blamed_commit, entry);
4504 err = run_blame(&s->blame, view, &s->blame_complete,
4505 &s->first_displayed_line, &s->last_displayed_line,
4506 &s->selected_line, &s->done, &s->eof,
4507 s->path, s->blamed_commit->id, s->repo);
4508 if (err)
4509 break;
4510 break;
4512 case 'B': {
4513 struct got_object_qid *first;
4514 first = SIMPLEQ_FIRST(&s->blamed_commits);
4515 if (!got_object_id_cmp(first->id, s->commit_id))
4516 break;
4517 s->done = 1;
4518 thread_err = stop_blame(&s->blame);
4519 s->done = 0;
4520 if (thread_err)
4521 break;
4522 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4523 got_object_qid_free(s->blamed_commit);
4524 s->blamed_commit =
4525 SIMPLEQ_FIRST(&s->blamed_commits);
4526 err = run_blame(&s->blame, view, &s->blame_complete,
4527 &s->first_displayed_line, &s->last_displayed_line,
4528 &s->selected_line, &s->done, &s->eof, s->path,
4529 s->blamed_commit->id, s->repo);
4530 if (err)
4531 break;
4532 break;
4534 case KEY_ENTER:
4535 case '\r': {
4536 struct got_object_id *id = NULL;
4537 struct got_object_qid *pid;
4538 struct got_commit_object *commit = NULL;
4539 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4540 s->first_displayed_line, s->selected_line);
4541 if (id == NULL)
4542 break;
4543 err = got_object_open_as_commit(&commit, s->repo, id);
4544 if (err)
4545 break;
4546 pid = SIMPLEQ_FIRST(
4547 got_object_commit_get_parent_ids(commit));
4548 if (view_is_parent_view(view))
4549 begin_x = view_split_begin_x(view->begin_x);
4550 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4551 if (diff_view == NULL) {
4552 got_object_commit_close(commit);
4553 err = got_error_from_errno("view_open");
4554 break;
4556 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4557 id, 0, NULL, s->refs, s->repo);
4558 got_object_commit_close(commit);
4559 if (err) {
4560 view_close(diff_view);
4561 break;
4563 if (view_is_parent_view(view)) {
4564 err = view_close_child(view);
4565 if (err)
4566 break;
4567 err = view_set_child(view, diff_view);
4568 if (err) {
4569 view_close(diff_view);
4570 break;
4572 *focus_view = diff_view;
4573 view->child_focussed = 1;
4574 } else
4575 *new_view = diff_view;
4576 if (err)
4577 break;
4578 break;
4580 case KEY_NPAGE:
4581 case CTRL('f'):
4582 case ' ':
4583 if (s->last_displayed_line >= s->blame.nlines &&
4584 s->selected_line >= MIN(s->blame.nlines,
4585 view->nlines - 2)) {
4586 break;
4588 if (s->last_displayed_line >= s->blame.nlines &&
4589 s->selected_line < view->nlines - 2) {
4590 s->selected_line = MIN(s->blame.nlines,
4591 view->nlines - 2);
4592 break;
4594 if (s->last_displayed_line + view->nlines - 2
4595 <= s->blame.nlines)
4596 s->first_displayed_line +=
4597 view->nlines - 2;
4598 else
4599 s->first_displayed_line =
4600 s->blame.nlines -
4601 (view->nlines - 3);
4602 break;
4603 case KEY_RESIZE:
4604 if (s->selected_line > view->nlines - 2) {
4605 s->selected_line = MIN(s->blame.nlines,
4606 view->nlines - 2);
4608 break;
4609 default:
4610 break;
4612 return thread_err ? thread_err : err;
4615 static const struct got_error *
4616 cmd_blame(int argc, char *argv[])
4618 const struct got_error *error;
4619 struct got_repository *repo = NULL;
4620 struct got_reflist_head refs;
4621 struct got_worktree *worktree = NULL;
4622 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4623 char *link_target = NULL;
4624 struct got_object_id *commit_id = NULL;
4625 char *commit_id_str = NULL;
4626 int ch;
4627 struct tog_view *view;
4629 SIMPLEQ_INIT(&refs);
4631 #ifndef PROFILE
4632 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4633 NULL) == -1)
4634 err(1, "pledge");
4635 #endif
4637 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4638 switch (ch) {
4639 case 'c':
4640 commit_id_str = optarg;
4641 break;
4642 case 'r':
4643 repo_path = realpath(optarg, NULL);
4644 if (repo_path == NULL)
4645 return got_error_from_errno2("realpath",
4646 optarg);
4647 break;
4648 default:
4649 usage_blame();
4650 /* NOTREACHED */
4654 argc -= optind;
4655 argv += optind;
4657 if (argc != 1)
4658 usage_blame();
4660 cwd = getcwd(NULL, 0);
4661 if (cwd == NULL)
4662 return got_error_from_errno("getcwd");
4664 error = got_worktree_open(&worktree, cwd);
4665 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4666 goto done;
4668 if (repo_path == NULL) {
4669 if (worktree)
4670 repo_path =
4671 strdup(got_worktree_get_repo_path(worktree));
4672 else
4673 repo_path = strdup(cwd);
4675 if (repo_path == NULL) {
4676 error = got_error_from_errno("strdup");
4677 goto done;
4680 error = got_repo_open(&repo, repo_path, NULL);
4681 if (error != NULL)
4682 goto done;
4684 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4685 worktree);
4686 if (error)
4687 goto done;
4689 init_curses();
4691 error = apply_unveil(got_repo_get_path(repo), NULL);
4692 if (error)
4693 goto done;
4695 if (commit_id_str == NULL) {
4696 struct got_reference *head_ref;
4697 error = got_ref_open(&head_ref, repo, worktree ?
4698 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4699 if (error != NULL)
4700 goto done;
4701 error = got_ref_resolve(&commit_id, repo, head_ref);
4702 got_ref_close(head_ref);
4703 } else {
4704 error = got_repo_match_object_id(&commit_id, NULL,
4705 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4707 if (error != NULL)
4708 goto done;
4710 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4711 if (error)
4712 goto done;
4714 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4715 if (view == NULL) {
4716 error = got_error_from_errno("view_open");
4717 goto done;
4720 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4721 commit_id, repo);
4722 if (error)
4723 goto done;
4725 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4726 commit_id, &refs, repo);
4727 if (error)
4728 goto done;
4729 if (worktree) {
4730 /* Release work tree lock. */
4731 got_worktree_close(worktree);
4732 worktree = NULL;
4734 error = view_loop(view);
4735 done:
4736 free(repo_path);
4737 free(in_repo_path);
4738 free(link_target);
4739 free(cwd);
4740 free(commit_id);
4741 if (worktree)
4742 got_worktree_close(worktree);
4743 if (repo)
4744 got_repo_close(repo);
4745 got_ref_list_free(&refs);
4746 return error;
4749 static const struct got_error *
4750 draw_tree_entries(struct tog_view *view,
4751 struct got_tree_entry **first_displayed_entry,
4752 struct got_tree_entry **last_displayed_entry,
4753 struct got_tree_entry **selected_entry, int *ndisplayed,
4754 const char *label, int show_ids, const char *parent_path,
4755 struct got_tree_object *tree, int selected, int limit,
4756 int isroot, struct tog_colors *colors, struct got_repository *repo)
4758 const struct got_error *err = NULL;
4759 struct got_tree_entry *te;
4760 wchar_t *wline;
4761 struct tog_color *tc;
4762 int width, n, i, nentries;
4764 *ndisplayed = 0;
4766 werase(view->window);
4768 if (limit == 0)
4769 return NULL;
4771 err = format_line(&wline, &width, label, view->ncols, 0);
4772 if (err)
4773 return err;
4774 if (view_needs_focus_indication(view))
4775 wstandout(view->window);
4776 tc = get_color(colors, TOG_COLOR_COMMIT);
4777 if (tc)
4778 wattr_on(view->window,
4779 COLOR_PAIR(tc->colorpair), NULL);
4780 waddwstr(view->window, wline);
4781 if (tc)
4782 wattr_off(view->window,
4783 COLOR_PAIR(tc->colorpair), NULL);
4784 if (view_needs_focus_indication(view))
4785 wstandend(view->window);
4786 free(wline);
4787 wline = NULL;
4788 if (width < view->ncols - 1)
4789 waddch(view->window, '\n');
4790 if (--limit <= 0)
4791 return NULL;
4792 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4793 if (err)
4794 return err;
4795 waddwstr(view->window, wline);
4796 free(wline);
4797 wline = NULL;
4798 if (width < view->ncols - 1)
4799 waddch(view->window, '\n');
4800 if (--limit <= 0)
4801 return NULL;
4802 waddch(view->window, '\n');
4803 if (--limit <= 0)
4804 return NULL;
4806 if (*first_displayed_entry == NULL) {
4807 te = got_object_tree_get_first_entry(tree);
4808 if (selected == 0) {
4809 if (view->focussed)
4810 wstandout(view->window);
4811 *selected_entry = NULL;
4813 waddstr(view->window, " ..\n"); /* parent directory */
4814 if (selected == 0 && view->focussed)
4815 wstandend(view->window);
4816 (*ndisplayed)++;
4817 if (--limit <= 0)
4818 return NULL;
4819 n = 1;
4820 } else {
4821 n = 0;
4822 te = *first_displayed_entry;
4825 nentries = got_object_tree_get_nentries(tree);
4826 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4827 char *line = NULL, *id_str = NULL, *link_target = NULL;
4828 const char *modestr = "";
4829 mode_t mode;
4831 te = got_object_tree_get_entry(tree, i);
4832 mode = got_tree_entry_get_mode(te);
4834 if (show_ids) {
4835 err = got_object_id_str(&id_str,
4836 got_tree_entry_get_id(te));
4837 if (err)
4838 return got_error_from_errno(
4839 "got_object_id_str");
4841 if (got_object_tree_entry_is_submodule(te))
4842 modestr = "$";
4843 else if (S_ISLNK(mode)) {
4844 int i;
4846 err = got_tree_entry_get_symlink_target(&link_target,
4847 te, repo);
4848 if (err) {
4849 free(id_str);
4850 return err;
4852 for (i = 0; i < strlen(link_target); i++) {
4853 if (!isprint((unsigned char)link_target[i]))
4854 link_target[i] = '?';
4856 modestr = "@";
4858 else if (S_ISDIR(mode))
4859 modestr = "/";
4860 else if (mode & S_IXUSR)
4861 modestr = "*";
4862 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4863 got_tree_entry_get_name(te), modestr,
4864 link_target ? " -> ": "",
4865 link_target ? link_target : "") == -1) {
4866 free(id_str);
4867 free(link_target);
4868 return got_error_from_errno("asprintf");
4870 free(id_str);
4871 free(link_target);
4872 err = format_line(&wline, &width, line, view->ncols, 0);
4873 if (err) {
4874 free(line);
4875 break;
4877 if (n == selected) {
4878 if (view->focussed)
4879 wstandout(view->window);
4880 *selected_entry = te;
4882 tc = match_color(colors, line);
4883 if (tc)
4884 wattr_on(view->window,
4885 COLOR_PAIR(tc->colorpair), NULL);
4886 waddwstr(view->window, wline);
4887 if (tc)
4888 wattr_off(view->window,
4889 COLOR_PAIR(tc->colorpair), NULL);
4890 if (width < view->ncols - 1)
4891 waddch(view->window, '\n');
4892 if (n == selected && view->focussed)
4893 wstandend(view->window);
4894 free(line);
4895 free(wline);
4896 wline = NULL;
4897 n++;
4898 (*ndisplayed)++;
4899 *last_displayed_entry = te;
4900 if (--limit <= 0)
4901 break;
4904 return err;
4907 static void
4908 tree_scroll_up(struct tog_view *view,
4909 struct got_tree_entry **first_displayed_entry, int maxscroll,
4910 struct got_tree_object *tree, int isroot)
4912 struct got_tree_entry *te;
4913 int i;
4915 if (*first_displayed_entry == NULL)
4916 return;
4918 te = got_object_tree_get_entry(tree, 0);
4919 if (*first_displayed_entry == te) {
4920 if (!isroot)
4921 *first_displayed_entry = NULL;
4922 return;
4925 i = 0;
4926 while (*first_displayed_entry && i < maxscroll) {
4927 *first_displayed_entry = got_tree_entry_get_prev(tree,
4928 *first_displayed_entry);
4929 i++;
4931 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4932 *first_displayed_entry = NULL;
4935 static int
4936 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4937 struct got_tree_entry *last_displayed_entry,
4938 struct got_tree_object *tree)
4940 struct got_tree_entry *next, *last;
4941 int n = 0;
4943 if (*first_displayed_entry)
4944 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4945 else
4946 next = got_object_tree_get_first_entry(tree);
4948 last = last_displayed_entry;
4949 while (next && last && n++ < maxscroll) {
4950 last = got_tree_entry_get_next(tree, last);
4951 if (last) {
4952 *first_displayed_entry = next;
4953 next = got_tree_entry_get_next(tree, next);
4956 return n;
4959 static const struct got_error *
4960 tree_entry_path(char **path, struct tog_parent_trees *parents,
4961 struct got_tree_entry *te)
4963 const struct got_error *err = NULL;
4964 struct tog_parent_tree *pt;
4965 size_t len = 2; /* for leading slash and NUL */
4967 TAILQ_FOREACH(pt, parents, entry)
4968 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4969 + 1 /* slash */;
4970 if (te)
4971 len += strlen(got_tree_entry_get_name(te));
4973 *path = calloc(1, len);
4974 if (path == NULL)
4975 return got_error_from_errno("calloc");
4977 (*path)[0] = '/';
4978 pt = TAILQ_LAST(parents, tog_parent_trees);
4979 while (pt) {
4980 const char *name = got_tree_entry_get_name(pt->selected_entry);
4981 if (strlcat(*path, name, len) >= len) {
4982 err = got_error(GOT_ERR_NO_SPACE);
4983 goto done;
4985 if (strlcat(*path, "/", len) >= len) {
4986 err = got_error(GOT_ERR_NO_SPACE);
4987 goto done;
4989 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4991 if (te) {
4992 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4993 err = got_error(GOT_ERR_NO_SPACE);
4994 goto done;
4997 done:
4998 if (err) {
4999 free(*path);
5000 *path = NULL;
5002 return err;
5005 static const struct got_error *
5006 blame_tree_entry(struct tog_view **new_view, int begin_x,
5007 struct got_tree_entry *te, struct tog_parent_trees *parents,
5008 struct got_object_id *commit_id, struct got_reflist_head *refs,
5009 struct got_repository *repo)
5011 const struct got_error *err = NULL;
5012 char *path;
5013 struct tog_view *blame_view;
5015 *new_view = NULL;
5017 err = tree_entry_path(&path, parents, te);
5018 if (err)
5019 return err;
5021 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5022 if (blame_view == NULL) {
5023 err = got_error_from_errno("view_open");
5024 goto done;
5027 err = open_blame_view(blame_view, path, commit_id, refs, repo);
5028 if (err) {
5029 if (err->code == GOT_ERR_CANCELLED)
5030 err = NULL;
5031 view_close(blame_view);
5032 } else
5033 *new_view = blame_view;
5034 done:
5035 free(path);
5036 return err;
5039 static const struct got_error *
5040 log_tree_entry(struct tog_view **new_view, int begin_x,
5041 struct got_tree_entry *te, struct tog_parent_trees *parents,
5042 struct got_object_id *commit_id, struct got_reflist_head *refs,
5043 struct got_repository *repo)
5045 struct tog_view *log_view;
5046 const struct got_error *err = NULL;
5047 char *path;
5049 *new_view = NULL;
5051 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5052 if (log_view == NULL)
5053 return got_error_from_errno("view_open");
5055 err = tree_entry_path(&path, parents, te);
5056 if (err)
5057 return err;
5059 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
5060 if (err)
5061 view_close(log_view);
5062 else
5063 *new_view = log_view;
5064 free(path);
5065 return err;
5068 static const struct got_error *
5069 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5070 struct got_object_id *commit_id, struct got_reflist_head *refs,
5071 struct got_repository *repo)
5073 const struct got_error *err = NULL;
5074 char *commit_id_str = NULL;
5075 struct tog_tree_view_state *s = &view->state.tree;
5077 TAILQ_INIT(&s->parents);
5079 err = got_object_id_str(&commit_id_str, commit_id);
5080 if (err != NULL)
5081 goto done;
5083 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5084 err = got_error_from_errno("asprintf");
5085 goto done;
5088 s->root = s->tree = root;
5089 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5090 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5091 s->commit_id = got_object_id_dup(commit_id);
5092 if (s->commit_id == NULL) {
5093 err = got_error_from_errno("got_object_id_dup");
5094 goto done;
5096 s->refs = refs;
5097 s->repo = repo;
5099 SIMPLEQ_INIT(&s->colors);
5101 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5102 err = add_color(&s->colors, "\\$$",
5103 TOG_COLOR_TREE_SUBMODULE,
5104 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5105 if (err)
5106 goto done;
5107 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5108 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5109 if (err) {
5110 free_colors(&s->colors);
5111 goto done;
5113 err = add_color(&s->colors, "/$",
5114 TOG_COLOR_TREE_DIRECTORY,
5115 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5116 if (err) {
5117 free_colors(&s->colors);
5118 goto done;
5121 err = add_color(&s->colors, "\\*$",
5122 TOG_COLOR_TREE_EXECUTABLE,
5123 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5124 if (err) {
5125 free_colors(&s->colors);
5126 goto done;
5129 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5130 get_color_value("TOG_COLOR_COMMIT"));
5131 if (err) {
5132 free_colors(&s->colors);
5133 goto done;
5137 view->show = show_tree_view;
5138 view->input = input_tree_view;
5139 view->close = close_tree_view;
5140 view->search_start = search_start_tree_view;
5141 view->search_next = search_next_tree_view;
5142 done:
5143 free(commit_id_str);
5144 if (err) {
5145 free(s->tree_label);
5146 s->tree_label = NULL;
5148 return err;
5151 static const struct got_error *
5152 close_tree_view(struct tog_view *view)
5154 struct tog_tree_view_state *s = &view->state.tree;
5156 free_colors(&s->colors);
5157 free(s->tree_label);
5158 s->tree_label = NULL;
5159 free(s->commit_id);
5160 s->commit_id = NULL;
5161 while (!TAILQ_EMPTY(&s->parents)) {
5162 struct tog_parent_tree *parent;
5163 parent = TAILQ_FIRST(&s->parents);
5164 TAILQ_REMOVE(&s->parents, parent, entry);
5165 free(parent);
5168 if (s->tree != s->root)
5169 got_object_tree_close(s->tree);
5170 got_object_tree_close(s->root);
5172 return NULL;
5175 static const struct got_error *
5176 search_start_tree_view(struct tog_view *view)
5178 struct tog_tree_view_state *s = &view->state.tree;
5180 s->matched_entry = NULL;
5181 return NULL;
5184 static int
5185 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5187 regmatch_t regmatch;
5189 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5190 0) == 0;
5193 static const struct got_error *
5194 search_next_tree_view(struct tog_view *view)
5196 struct tog_tree_view_state *s = &view->state.tree;
5197 struct got_tree_entry *te = NULL;
5199 if (!view->searching) {
5200 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5201 return NULL;
5204 if (s->matched_entry) {
5205 if (view->searching == TOG_SEARCH_FORWARD) {
5206 if (s->selected_entry)
5207 te = got_tree_entry_get_next(s->tree,
5208 s->selected_entry);
5209 else
5210 te = got_object_tree_get_first_entry(s->tree);
5211 } else {
5212 if (s->selected_entry == NULL)
5213 te = got_object_tree_get_last_entry(s->tree);
5214 else
5215 te = got_tree_entry_get_prev(s->tree,
5216 s->selected_entry);
5218 } else {
5219 if (view->searching == TOG_SEARCH_FORWARD)
5220 te = got_object_tree_get_first_entry(s->tree);
5221 else
5222 te = got_object_tree_get_last_entry(s->tree);
5225 while (1) {
5226 if (te == NULL) {
5227 if (s->matched_entry == NULL) {
5228 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5229 return NULL;
5231 if (view->searching == TOG_SEARCH_FORWARD)
5232 te = got_object_tree_get_first_entry(s->tree);
5233 else
5234 te = got_object_tree_get_last_entry(s->tree);
5237 if (match_tree_entry(te, &view->regex)) {
5238 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5239 s->matched_entry = te;
5240 break;
5243 if (view->searching == TOG_SEARCH_FORWARD)
5244 te = got_tree_entry_get_next(s->tree, te);
5245 else
5246 te = got_tree_entry_get_prev(s->tree, te);
5249 if (s->matched_entry) {
5250 s->first_displayed_entry = s->matched_entry;
5251 s->selected = 0;
5254 return NULL;
5257 static const struct got_error *
5258 show_tree_view(struct tog_view *view)
5260 const struct got_error *err = NULL;
5261 struct tog_tree_view_state *s = &view->state.tree;
5262 char *parent_path;
5264 err = tree_entry_path(&parent_path, &s->parents, NULL);
5265 if (err)
5266 return err;
5268 err = draw_tree_entries(view, &s->first_displayed_entry,
5269 &s->last_displayed_entry, &s->selected_entry,
5270 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5271 s->tree, s->selected, view->nlines, s->tree == s->root,
5272 &s->colors, s->repo);
5273 free(parent_path);
5275 view_vborder(view);
5276 return err;
5279 static const struct got_error *
5280 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5281 struct tog_view **focus_view, struct tog_view *view, int ch)
5283 const struct got_error *err = NULL;
5284 struct tog_tree_view_state *s = &view->state.tree;
5285 struct tog_view *log_view;
5286 int begin_x = 0, nscrolled;
5288 switch (ch) {
5289 case 'i':
5290 s->show_ids = !s->show_ids;
5291 break;
5292 case 'l':
5293 if (!s->selected_entry)
5294 break;
5295 if (view_is_parent_view(view))
5296 begin_x = view_split_begin_x(view->begin_x);
5297 err = log_tree_entry(&log_view, begin_x,
5298 s->selected_entry, &s->parents,
5299 s->commit_id, s->refs, s->repo);
5300 if (view_is_parent_view(view)) {
5301 err = view_close_child(view);
5302 if (err)
5303 return err;
5304 err = view_set_child(view, log_view);
5305 if (err) {
5306 view_close(log_view);
5307 break;
5309 *focus_view = log_view;
5310 view->child_focussed = 1;
5311 } else
5312 *new_view = log_view;
5313 break;
5314 case 'k':
5315 case KEY_UP:
5316 if (s->selected > 0) {
5317 s->selected--;
5318 if (s->selected == 0)
5319 break;
5321 if (s->selected > 0)
5322 break;
5323 tree_scroll_up(view, &s->first_displayed_entry, 1,
5324 s->tree, s->tree == s->root);
5325 break;
5326 case KEY_PPAGE:
5327 case CTRL('b'):
5328 tree_scroll_up(view, &s->first_displayed_entry,
5329 MAX(0, view->nlines - 4 - s->selected), s->tree,
5330 s->tree == s->root);
5331 s->selected = 0;
5332 if (got_object_tree_get_first_entry(s->tree) ==
5333 s->first_displayed_entry && s->tree != s->root)
5334 s->first_displayed_entry = NULL;
5335 break;
5336 case 'j':
5337 case KEY_DOWN:
5338 if (s->selected < s->ndisplayed - 1) {
5339 s->selected++;
5340 break;
5342 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5343 == NULL)
5344 /* can't scroll any further */
5345 break;
5346 tree_scroll_down(&s->first_displayed_entry, 1,
5347 s->last_displayed_entry, s->tree);
5348 break;
5349 case KEY_NPAGE:
5350 case CTRL('f'):
5351 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5352 == NULL) {
5353 /* can't scroll any further; move cursor down */
5354 if (s->selected < s->ndisplayed - 1)
5355 s->selected = s->ndisplayed - 1;
5356 break;
5358 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5359 view->nlines, s->last_displayed_entry, s->tree);
5360 if (nscrolled < view->nlines) {
5361 int ndisplayed = 0;
5362 struct got_tree_entry *te;
5363 te = s->first_displayed_entry;
5364 do {
5365 ndisplayed++;
5366 te = got_tree_entry_get_next(s->tree, te);
5367 } while (te);
5368 s->selected = ndisplayed - 1;
5370 break;
5371 case KEY_ENTER:
5372 case '\r':
5373 case KEY_BACKSPACE:
5374 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5375 struct tog_parent_tree *parent;
5376 /* user selected '..' */
5377 if (s->tree == s->root)
5378 break;
5379 parent = TAILQ_FIRST(&s->parents);
5380 TAILQ_REMOVE(&s->parents, parent,
5381 entry);
5382 got_object_tree_close(s->tree);
5383 s->tree = parent->tree;
5384 s->first_displayed_entry =
5385 parent->first_displayed_entry;
5386 s->selected_entry =
5387 parent->selected_entry;
5388 s->selected = parent->selected;
5389 free(parent);
5390 } else if (S_ISDIR(got_tree_entry_get_mode(
5391 s->selected_entry))) {
5392 struct got_tree_object *subtree;
5393 err = got_object_open_as_tree(&subtree, s->repo,
5394 got_tree_entry_get_id(s->selected_entry));
5395 if (err)
5396 break;
5397 err = tree_view_visit_subtree(subtree, s);
5398 if (err) {
5399 got_object_tree_close(subtree);
5400 break;
5402 } else if (S_ISREG(got_tree_entry_get_mode(
5403 s->selected_entry))) {
5404 struct tog_view *blame_view;
5405 int begin_x = view_is_parent_view(view) ?
5406 view_split_begin_x(view->begin_x) : 0;
5408 err = blame_tree_entry(&blame_view, begin_x,
5409 s->selected_entry, &s->parents,
5410 s->commit_id, s->refs, s->repo);
5411 if (err)
5412 break;
5413 if (view_is_parent_view(view)) {
5414 err = view_close_child(view);
5415 if (err)
5416 return err;
5417 err = view_set_child(view, blame_view);
5418 if (err) {
5419 view_close(blame_view);
5420 break;
5422 *focus_view = blame_view;
5423 view->child_focussed = 1;
5424 } else
5425 *new_view = blame_view;
5427 break;
5428 case KEY_RESIZE:
5429 if (s->selected > view->nlines)
5430 s->selected = s->ndisplayed - 1;
5431 break;
5432 default:
5433 break;
5436 return err;
5439 __dead static void
5440 usage_tree(void)
5442 endwin();
5443 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5444 getprogname());
5445 exit(1);
5448 static const struct got_error *
5449 cmd_tree(int argc, char *argv[])
5451 const struct got_error *error;
5452 struct got_repository *repo = NULL;
5453 struct got_worktree *worktree = NULL;
5454 struct got_reflist_head refs;
5455 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5456 struct got_object_id *commit_id = NULL;
5457 char *commit_id_arg = NULL;
5458 struct got_commit_object *commit = NULL;
5459 struct got_tree_object *tree = NULL;
5460 int ch;
5461 struct tog_view *view;
5463 SIMPLEQ_INIT(&refs);
5465 #ifndef PROFILE
5466 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5467 NULL) == -1)
5468 err(1, "pledge");
5469 #endif
5471 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5472 switch (ch) {
5473 case 'c':
5474 commit_id_arg = optarg;
5475 break;
5476 case 'r':
5477 repo_path = realpath(optarg, NULL);
5478 if (repo_path == NULL)
5479 return got_error_from_errno2("realpath",
5480 optarg);
5481 break;
5482 default:
5483 usage_tree();
5484 /* NOTREACHED */
5488 argc -= optind;
5489 argv += optind;
5491 if (argc > 1)
5492 usage_tree();
5494 cwd = getcwd(NULL, 0);
5495 if (cwd == NULL)
5496 return got_error_from_errno("getcwd");
5498 error = got_worktree_open(&worktree, cwd);
5499 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5500 goto done;
5502 if (repo_path == NULL) {
5503 if (worktree)
5504 repo_path =
5505 strdup(got_worktree_get_repo_path(worktree));
5506 else
5507 repo_path = strdup(cwd);
5509 if (repo_path == NULL) {
5510 error = got_error_from_errno("strdup");
5511 goto done;
5514 error = got_repo_open(&repo, repo_path, NULL);
5515 if (error != NULL)
5516 goto done;
5518 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5519 repo, worktree);
5520 if (error)
5521 goto done;
5523 init_curses();
5525 error = apply_unveil(got_repo_get_path(repo), NULL);
5526 if (error)
5527 goto done;
5529 error = got_repo_match_object_id(&commit_id, NULL,
5530 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5531 GOT_OBJ_TYPE_COMMIT, 1, repo);
5532 if (error)
5533 goto done;
5535 error = got_object_open_as_commit(&commit, repo, commit_id);
5536 if (error)
5537 goto done;
5539 error = got_object_open_as_tree(&tree, repo,
5540 got_object_commit_get_tree_id(commit));
5541 if (error)
5542 goto done;
5544 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5545 if (error)
5546 goto done;
5548 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5549 if (view == NULL) {
5550 error = got_error_from_errno("view_open");
5551 goto done;
5553 error = open_tree_view(view, tree, commit_id, &refs, repo);
5554 if (error)
5555 goto done;
5556 if (!got_path_is_root_dir(in_repo_path)) {
5557 error = tree_view_walk_path(&view->state.tree, commit_id,
5558 in_repo_path, repo);
5559 if (error)
5560 goto done;
5563 if (worktree) {
5564 /* Release work tree lock. */
5565 got_worktree_close(worktree);
5566 worktree = NULL;
5568 error = view_loop(view);
5569 done:
5570 free(repo_path);
5571 free(cwd);
5572 free(commit_id);
5573 if (commit)
5574 got_object_commit_close(commit);
5575 if (tree)
5576 got_object_tree_close(tree);
5577 if (repo)
5578 got_repo_close(repo);
5579 got_ref_list_free(&refs);
5580 return error;
5583 static void
5584 list_commands(FILE *fp)
5586 int i;
5588 fprintf(fp, "commands:");
5589 for (i = 0; i < nitems(tog_commands); i++) {
5590 struct tog_cmd *cmd = &tog_commands[i];
5591 fprintf(fp, " %s", cmd->name);
5593 fputc('\n', fp);
5596 __dead static void
5597 usage(int hflag, int status)
5599 FILE *fp = (status == 0) ? stdout : stderr;
5601 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5602 getprogname());
5603 if (hflag) {
5604 fprintf(fp, "lazy usage: %s path\n", getprogname());
5605 list_commands(fp);
5607 exit(status);
5610 static char **
5611 make_argv(int argc, ...)
5613 va_list ap;
5614 char **argv;
5615 int i;
5617 va_start(ap, argc);
5619 argv = calloc(argc, sizeof(char *));
5620 if (argv == NULL)
5621 err(1, "calloc");
5622 for (i = 0; i < argc; i++) {
5623 argv[i] = strdup(va_arg(ap, char *));
5624 if (argv[i] == NULL)
5625 err(1, "strdup");
5628 va_end(ap);
5629 return argv;
5633 * Try to convert 'tog path' into a 'tog log path' command.
5634 * The user could simply have mistyped the command rather than knowingly
5635 * provided a path. So check whether argv[0] can in fact be resolved
5636 * to a path in the HEAD commit and print a special error if not.
5637 * This hack is for mpi@ <3
5639 static const struct got_error *
5640 tog_log_with_path(int argc, char *argv[])
5642 const struct got_error *error = NULL;
5643 struct tog_cmd *cmd = NULL;
5644 struct got_repository *repo = NULL;
5645 struct got_worktree *worktree = NULL;
5646 struct got_object_id *commit_id = NULL, *id = NULL;
5647 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5648 char *commit_id_str = NULL, **cmd_argv = NULL;
5650 cwd = getcwd(NULL, 0);
5651 if (cwd == NULL)
5652 return got_error_from_errno("getcwd");
5654 error = got_worktree_open(&worktree, cwd);
5655 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5656 goto done;
5658 if (worktree)
5659 repo_path = strdup(got_worktree_get_repo_path(worktree));
5660 else
5661 repo_path = strdup(cwd);
5662 if (repo_path == NULL) {
5663 error = got_error_from_errno("strdup");
5664 goto done;
5667 error = got_repo_open(&repo, repo_path, NULL);
5668 if (error != NULL)
5669 goto done;
5671 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5672 repo, worktree);
5673 if (error)
5674 goto done;
5676 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5677 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5678 GOT_OBJ_TYPE_COMMIT, 1, repo);
5679 if (error)
5680 goto done;
5682 if (worktree) {
5683 got_worktree_close(worktree);
5684 worktree = NULL;
5687 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5688 if (error) {
5689 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5690 goto done;
5691 fprintf(stderr, "%s: '%s' is no known command or path\n",
5692 getprogname(), argv[0]);
5693 usage(1, 1);
5694 /* not reached */
5697 got_repo_close(repo);
5698 repo = NULL;
5700 error = got_object_id_str(&commit_id_str, commit_id);
5701 if (error)
5702 goto done;
5704 cmd = &tog_commands[0]; /* log */
5705 argc = 4;
5706 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5707 error = cmd->cmd_main(argc, cmd_argv);
5708 done:
5709 if (repo)
5710 got_repo_close(repo);
5711 if (worktree)
5712 got_worktree_close(worktree);
5713 free(id);
5714 free(commit_id_str);
5715 free(commit_id);
5716 free(cwd);
5717 free(repo_path);
5718 free(in_repo_path);
5719 if (cmd_argv) {
5720 int i;
5721 for (i = 0; i < argc; i++)
5722 free(cmd_argv[i]);
5723 free(cmd_argv);
5725 return error;
5728 int
5729 main(int argc, char *argv[])
5731 const struct got_error *error = NULL;
5732 struct tog_cmd *cmd = NULL;
5733 int ch, hflag = 0, Vflag = 0;
5734 char **cmd_argv = NULL;
5735 static struct option longopts[] = {
5736 { "version", no_argument, NULL, 'V' },
5737 { NULL, 0, NULL, 0}
5740 setlocale(LC_CTYPE, "");
5742 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5743 switch (ch) {
5744 case 'h':
5745 hflag = 1;
5746 break;
5747 case 'V':
5748 Vflag = 1;
5749 break;
5750 default:
5751 usage(hflag, 1);
5752 /* NOTREACHED */
5756 argc -= optind;
5757 argv += optind;
5758 optind = 1;
5759 optreset = 1;
5761 if (Vflag) {
5762 got_version_print_str();
5763 return 0;
5766 if (argc == 0) {
5767 if (hflag)
5768 usage(hflag, 0);
5769 /* Build an argument vector which runs a default command. */
5770 cmd = &tog_commands[0];
5771 argc = 1;
5772 cmd_argv = make_argv(argc, cmd->name);
5773 } else {
5774 int i;
5776 /* Did the user specify a command? */
5777 for (i = 0; i < nitems(tog_commands); i++) {
5778 if (strncmp(tog_commands[i].name, argv[0],
5779 strlen(argv[0])) == 0) {
5780 cmd = &tog_commands[i];
5781 break;
5786 if (cmd == NULL) {
5787 if (argc != 1)
5788 usage(0, 1);
5789 /* No command specified; try log with a path */
5790 error = tog_log_with_path(argc, argv);
5791 } else {
5792 if (hflag)
5793 cmd->cmd_usage();
5794 else
5795 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5798 endwin();
5799 putchar('\n');
5800 if (cmd_argv) {
5801 int i;
5802 for (i = 0; i < argc; i++)
5803 free(cmd_argv[i]);
5804 free(cmd_argv);
5807 if (error && error->code != GOT_ERR_CANCELLED)
5808 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5809 return 0;