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 struct got_repository *repo;
253 struct got_reflist_head *refs;
254 struct tog_colors colors;
255 size_t nlines;
256 off_t *line_offsets;
257 int matched_line;
258 int selected_line;
260 /* passed from log view; may be NULL */
261 struct tog_view *log_view;
262 };
264 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
266 struct tog_log_thread_args {
267 pthread_cond_t need_commits;
268 pthread_cond_t commit_loaded;
269 int commits_needed;
270 struct got_commit_graph *graph;
271 struct commit_queue *commits;
272 const char *in_repo_path;
273 struct got_object_id *start_id;
274 struct got_repository *repo;
275 int log_complete;
276 sig_atomic_t *quit;
277 struct commit_queue_entry **first_displayed_entry;
278 struct commit_queue_entry **selected_entry;
279 int *searching;
280 int *search_next_done;
281 regex_t *regex;
282 };
284 struct tog_log_view_state {
285 struct commit_queue commits;
286 struct commit_queue_entry *first_displayed_entry;
287 struct commit_queue_entry *last_displayed_entry;
288 struct commit_queue_entry *selected_entry;
289 int selected;
290 char *in_repo_path;
291 const char *head_ref_name;
292 int log_branches;
293 struct got_repository *repo;
294 struct got_reflist_head *refs;
295 struct got_object_id *start_id;
296 sig_atomic_t quit;
297 pthread_t thread;
298 struct tog_log_thread_args thread_args;
299 struct commit_queue_entry *matched_entry;
300 struct commit_queue_entry *search_entry;
301 struct tog_colors colors;
302 };
304 #define TOG_COLOR_DIFF_MINUS 1
305 #define TOG_COLOR_DIFF_PLUS 2
306 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
307 #define TOG_COLOR_DIFF_META 4
308 #define TOG_COLOR_TREE_SUBMODULE 5
309 #define TOG_COLOR_TREE_SYMLINK 6
310 #define TOG_COLOR_TREE_DIRECTORY 7
311 #define TOG_COLOR_TREE_EXECUTABLE 8
312 #define TOG_COLOR_COMMIT 9
313 #define TOG_COLOR_AUTHOR 10
314 #define TOG_COLOR_DATE 11
316 struct tog_blame_cb_args {
317 struct tog_blame_line *lines; /* one per line */
318 int nlines;
320 struct tog_view *view;
321 struct got_object_id *commit_id;
322 int *quit;
323 };
325 struct tog_blame_thread_args {
326 const char *path;
327 struct got_repository *repo;
328 struct tog_blame_cb_args *cb_args;
329 int *complete;
330 got_cancel_cb cancel_cb;
331 void *cancel_arg;
332 };
334 struct tog_blame {
335 FILE *f;
336 size_t filesize;
337 struct tog_blame_line *lines;
338 int nlines;
339 off_t *line_offsets;
340 pthread_t thread;
341 struct tog_blame_thread_args thread_args;
342 struct tog_blame_cb_args cb_args;
343 const char *path;
344 };
346 struct tog_blame_view_state {
347 int first_displayed_line;
348 int last_displayed_line;
349 int selected_line;
350 int blame_complete;
351 int eof;
352 int done;
353 struct got_object_id_queue blamed_commits;
354 struct got_object_qid *blamed_commit;
355 char *path;
356 struct got_repository *repo;
357 struct got_reflist_head *refs;
358 struct got_object_id *commit_id;
359 struct tog_blame blame;
360 int matched_line;
361 struct tog_colors colors;
362 };
364 struct tog_parent_tree {
365 TAILQ_ENTRY(tog_parent_tree) entry;
366 struct got_tree_object *tree;
367 struct got_tree_entry *first_displayed_entry;
368 struct got_tree_entry *selected_entry;
369 int selected;
370 };
372 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
374 struct tog_tree_view_state {
375 char *tree_label;
376 struct got_tree_object *root;
377 struct got_tree_object *tree;
378 struct got_tree_entry *first_displayed_entry;
379 struct got_tree_entry *last_displayed_entry;
380 struct got_tree_entry *selected_entry;
381 int ndisplayed, selected, show_ids;
382 struct tog_parent_trees parents;
383 struct got_object_id *commit_id;
384 struct got_repository *repo;
385 struct got_reflist_head *refs;
386 struct got_tree_entry *matched_entry;
387 struct tog_colors colors;
388 };
390 /*
391 * We implement two types of views: parent views and child views.
393 * The 'Tab' key switches between a parent view and its child view.
394 * Child views are shown side-by-side to their parent view, provided
395 * there is enough screen estate.
397 * When a new view is opened from within a parent view, this new view
398 * becomes a child view of the parent view, replacing any existing child.
400 * When a new view is opened from within a child view, this new view
401 * becomes a parent view which will obscure the views below until the
402 * user quits the new parent view by typing 'q'.
404 * This list of views contains parent views only.
405 * Child views are only pointed to by their parent view.
406 */
407 TAILQ_HEAD(tog_view_list_head, tog_view);
409 struct tog_view {
410 TAILQ_ENTRY(tog_view) entry;
411 WINDOW *window;
412 PANEL *panel;
413 int nlines, ncols, begin_y, begin_x;
414 int lines, cols; /* copies of LINES and COLS */
415 int focussed;
416 struct tog_view *parent;
417 struct tog_view *child;
418 int child_focussed;
420 /* type-specific state */
421 enum tog_view_type type;
422 union {
423 struct tog_diff_view_state diff;
424 struct tog_log_view_state log;
425 struct tog_blame_view_state blame;
426 struct tog_tree_view_state tree;
427 } state;
429 const struct got_error *(*show)(struct tog_view *);
430 const struct got_error *(*input)(struct tog_view **,
431 struct tog_view **, struct tog_view**, struct tog_view *, int);
432 const struct got_error *(*close)(struct tog_view *);
434 const struct got_error *(*search_start)(struct tog_view *);
435 const struct got_error *(*search_next)(struct tog_view *);
436 int searching;
437 #define TOG_SEARCH_FORWARD 1
438 #define TOG_SEARCH_BACKWARD 2
439 int search_next_done;
440 #define TOG_SEARCH_HAVE_MORE 1
441 #define TOG_SEARCH_NO_MORE 2
442 #define TOG_SEARCH_HAVE_NONE 3
443 regex_t regex;
444 regmatch_t regmatch;
445 };
447 static const struct got_error *open_diff_view(struct tog_view *,
448 struct got_object_id *, struct got_object_id *, struct tog_view *,
449 struct got_reflist_head *, struct got_repository *);
450 static const struct got_error *show_diff_view(struct tog_view *);
451 static const struct got_error *input_diff_view(struct tog_view **,
452 struct tog_view **, struct tog_view **, struct tog_view *, int);
453 static const struct got_error* close_diff_view(struct tog_view *);
454 static const struct got_error *search_start_diff_view(struct tog_view *);
455 static const struct got_error *search_next_diff_view(struct tog_view *);
457 static const struct got_error *open_log_view(struct tog_view *,
458 struct got_object_id *, struct got_reflist_head *,
459 struct got_repository *, const char *, const char *, int);
460 static const struct got_error * show_log_view(struct tog_view *);
461 static const struct got_error *input_log_view(struct tog_view **,
462 struct tog_view **, struct tog_view **, struct tog_view *, int);
463 static const struct got_error *close_log_view(struct tog_view *);
464 static const struct got_error *search_start_log_view(struct tog_view *);
465 static const struct got_error *search_next_log_view(struct tog_view *);
467 static const struct got_error *open_blame_view(struct tog_view *, char *,
468 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
469 static const struct got_error *show_blame_view(struct tog_view *);
470 static const struct got_error *input_blame_view(struct tog_view **,
471 struct tog_view **, struct tog_view **, struct tog_view *, int);
472 static const struct got_error *close_blame_view(struct tog_view *);
473 static const struct got_error *search_start_blame_view(struct tog_view *);
474 static const struct got_error *search_next_blame_view(struct tog_view *);
476 static const struct got_error *open_tree_view(struct tog_view *,
477 struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
478 struct got_repository *);
479 static const struct got_error *show_tree_view(struct tog_view *);
480 static const struct got_error *input_tree_view(struct tog_view **,
481 struct tog_view **, struct tog_view **, struct tog_view *, int);
482 static const struct got_error *close_tree_view(struct tog_view *);
483 static const struct got_error *search_start_tree_view(struct tog_view *);
484 static const struct got_error *search_next_tree_view(struct tog_view *);
486 static volatile sig_atomic_t tog_sigwinch_received;
487 static volatile sig_atomic_t tog_sigpipe_received;
488 static volatile sig_atomic_t tog_sigcont_received;
490 static void
491 tog_sigwinch(int signo)
493 tog_sigwinch_received = 1;
496 static void
497 tog_sigpipe(int signo)
499 tog_sigpipe_received = 1;
502 static void
503 tog_sigcont(int signo)
505 tog_sigcont_received = 1;
508 static const struct got_error *
509 view_close(struct tog_view *view)
511 const struct got_error *err = NULL;
513 if (view->child) {
514 view_close(view->child);
515 view->child = NULL;
517 if (view->close)
518 err = view->close(view);
519 if (view->panel)
520 del_panel(view->panel);
521 if (view->window)
522 delwin(view->window);
523 free(view);
524 return err;
527 static struct tog_view *
528 view_open(int nlines, int ncols, int begin_y, int begin_x,
529 enum tog_view_type type)
531 struct tog_view *view = calloc(1, sizeof(*view));
533 if (view == NULL)
534 return NULL;
536 view->type = type;
537 view->lines = LINES;
538 view->cols = COLS;
539 view->nlines = nlines ? nlines : LINES - begin_y;
540 view->ncols = ncols ? ncols : COLS - begin_x;
541 view->begin_y = begin_y;
542 view->begin_x = begin_x;
543 view->window = newwin(nlines, ncols, begin_y, begin_x);
544 if (view->window == NULL) {
545 view_close(view);
546 return NULL;
548 view->panel = new_panel(view->window);
549 if (view->panel == NULL ||
550 set_panel_userptr(view->panel, view) != OK) {
551 view_close(view);
552 return NULL;
555 keypad(view->window, TRUE);
556 return view;
559 static int
560 view_split_begin_x(int begin_x)
562 if (begin_x > 0 || COLS < 120)
563 return 0;
564 return (COLS - MAX(COLS / 2, 80));
567 static const struct got_error *view_resize(struct tog_view *);
569 static const struct got_error *
570 view_splitscreen(struct tog_view *view)
572 const struct got_error *err = NULL;
574 view->begin_y = 0;
575 view->begin_x = view_split_begin_x(0);
576 view->nlines = LINES;
577 view->ncols = COLS - view->begin_x;
578 view->lines = LINES;
579 view->cols = COLS;
580 err = view_resize(view);
581 if (err)
582 return err;
584 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
585 return got_error_from_errno("mvwin");
587 return NULL;
590 static const struct got_error *
591 view_fullscreen(struct tog_view *view)
593 const struct got_error *err = NULL;
595 view->begin_x = 0;
596 view->begin_y = 0;
597 view->nlines = LINES;
598 view->ncols = COLS;
599 view->lines = LINES;
600 view->cols = COLS;
601 err = view_resize(view);
602 if (err)
603 return err;
605 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
606 return got_error_from_errno("mvwin");
608 return NULL;
611 static int
612 view_is_parent_view(struct tog_view *view)
614 return view->parent == NULL;
617 static const struct got_error *
618 view_resize(struct tog_view *view)
620 int nlines, ncols;
622 if (view->lines > LINES)
623 nlines = view->nlines - (view->lines - LINES);
624 else
625 nlines = view->nlines + (LINES - view->lines);
627 if (view->cols > COLS)
628 ncols = view->ncols - (view->cols - COLS);
629 else
630 ncols = view->ncols + (COLS - view->cols);
632 if (wresize(view->window, nlines, ncols) == ERR)
633 return got_error_from_errno("wresize");
634 if (replace_panel(view->panel, view->window) == ERR)
635 return got_error_from_errno("replace_panel");
636 wclear(view->window);
638 view->nlines = nlines;
639 view->ncols = ncols;
640 view->lines = LINES;
641 view->cols = COLS;
643 if (view->child) {
644 view->child->begin_x = view_split_begin_x(view->begin_x);
645 if (view->child->begin_x == 0) {
646 view_fullscreen(view->child);
647 if (view->child->focussed)
648 show_panel(view->child->panel);
649 else
650 show_panel(view->panel);
651 } else {
652 view_splitscreen(view->child);
653 show_panel(view->child->panel);
657 return NULL;
660 static const struct got_error *
661 view_close_child(struct tog_view *view)
663 const struct got_error *err = NULL;
665 if (view->child == NULL)
666 return NULL;
668 err = view_close(view->child);
669 view->child = NULL;
670 return err;
673 static const struct got_error *
674 view_set_child(struct tog_view *view, struct tog_view *child)
676 const struct got_error *err = NULL;
678 view->child = child;
679 child->parent = view;
680 return err;
683 static int
684 view_is_splitscreen(struct tog_view *view)
686 return view->begin_x > 0;
689 static void
690 tog_resizeterm(void)
692 int cols, lines;
693 struct winsize size;
695 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
696 cols = 80; /* Default */
697 lines = 24;
698 } else {
699 cols = size.ws_col;
700 lines = size.ws_row;
702 resize_term(lines, cols);
705 static const struct got_error *
706 view_search_start(struct tog_view *view)
708 const struct got_error *err = NULL;
709 char pattern[1024];
710 int ret;
712 if (view->nlines < 1)
713 return NULL;
715 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
716 wclrtoeol(view->window);
718 nocbreak();
719 echo();
720 ret = wgetnstr(view->window, pattern, sizeof(pattern));
721 cbreak();
722 noecho();
723 if (ret == ERR)
724 return NULL;
726 if (view->searching) {
727 regfree(&view->regex);
728 view->searching = 0;
731 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
732 err = view->search_start(view);
733 if (err) {
734 regfree(&view->regex);
735 return err;
737 view->searching = TOG_SEARCH_FORWARD;
738 view->search_next_done = 0;
739 view->search_next(view);
742 return NULL;
745 static const struct got_error *
746 view_input(struct tog_view **new, struct tog_view **dead,
747 struct tog_view **focus, int *done, struct tog_view *view,
748 struct tog_view_list_head *views)
750 const struct got_error *err = NULL;
751 struct tog_view *v;
752 int ch, errcode;
754 *new = NULL;
755 *dead = NULL;
756 *focus = NULL;
758 /* Clear "no matches" indicator. */
759 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
760 view->search_next_done == TOG_SEARCH_HAVE_NONE)
761 view->search_next_done = TOG_SEARCH_HAVE_MORE;
763 if (view->searching && !view->search_next_done) {
764 errcode = pthread_mutex_unlock(&tog_mutex);
765 if (errcode)
766 return got_error_set_errno(errcode,
767 "pthread_mutex_unlock");
768 pthread_yield();
769 errcode = pthread_mutex_lock(&tog_mutex);
770 if (errcode)
771 return got_error_set_errno(errcode,
772 "pthread_mutex_lock");
773 view->search_next(view);
774 return NULL;
777 nodelay(stdscr, FALSE);
778 /* Allow threads to make progress while we are waiting for input. */
779 errcode = pthread_mutex_unlock(&tog_mutex);
780 if (errcode)
781 return got_error_set_errno(errcode, "pthread_mutex_unlock");
782 ch = wgetch(view->window);
783 errcode = pthread_mutex_lock(&tog_mutex);
784 if (errcode)
785 return got_error_set_errno(errcode, "pthread_mutex_lock");
786 nodelay(stdscr, TRUE);
788 if (tog_sigwinch_received || tog_sigcont_received) {
789 tog_resizeterm();
790 tog_sigwinch_received = 0;
791 tog_sigcont_received = 0;
792 TAILQ_FOREACH(v, views, entry) {
793 err = view_resize(v);
794 if (err)
795 return err;
796 err = v->input(new, dead, focus, v, KEY_RESIZE);
797 if (err)
798 return err;
802 switch (ch) {
803 case ERR:
804 break;
805 case '\t':
806 if (view->child) {
807 *focus = view->child;
808 view->child_focussed = 1;
809 } else if (view->parent) {
810 *focus = view->parent;
811 view->parent->child_focussed = 0;
813 break;
814 case 'q':
815 err = view->input(new, dead, focus, view, ch);
816 *dead = view;
817 break;
818 case 'Q':
819 *done = 1;
820 break;
821 case 'f':
822 if (view_is_parent_view(view)) {
823 if (view->child == NULL)
824 break;
825 if (view_is_splitscreen(view->child)) {
826 *focus = view->child;
827 view->child_focussed = 1;
828 err = view_fullscreen(view->child);
829 } else
830 err = view_splitscreen(view->child);
831 if (err)
832 break;
833 err = view->child->input(new, dead, focus,
834 view->child, KEY_RESIZE);
835 } else {
836 if (view_is_splitscreen(view)) {
837 *focus = view;
838 view->parent->child_focussed = 1;
839 err = view_fullscreen(view);
840 } else {
841 err = view_splitscreen(view);
843 if (err)
844 break;
845 err = view->input(new, dead, focus, view,
846 KEY_RESIZE);
848 break;
849 case KEY_RESIZE:
850 break;
851 case '/':
852 if (view->search_start)
853 view_search_start(view);
854 else
855 err = view->input(new, dead, focus, view, ch);
856 break;
857 case 'N':
858 case 'n':
859 if (view->search_next) {
860 view->searching = (ch == 'n' ?
861 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
862 view->search_next_done = 0;
863 view->search_next(view);
864 } else
865 err = view->input(new, dead, focus, view, ch);
866 break;
867 default:
868 err = view->input(new, dead, focus, view, ch);
869 break;
872 return err;
875 void
876 view_vborder(struct tog_view *view)
878 PANEL *panel;
879 struct tog_view *view_above;
881 if (view->parent)
882 return view_vborder(view->parent);
884 panel = panel_above(view->panel);
885 if (panel == NULL)
886 return;
888 view_above = panel_userptr(panel);
889 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
890 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
893 int
894 view_needs_focus_indication(struct tog_view *view)
896 if (view_is_parent_view(view)) {
897 if (view->child == NULL || view->child_focussed)
898 return 0;
899 if (!view_is_splitscreen(view->child))
900 return 0;
901 } else if (!view_is_splitscreen(view))
902 return 0;
904 return view->focussed;
907 static const struct got_error *
908 view_loop(struct tog_view *view)
910 const struct got_error *err = NULL;
911 struct tog_view_list_head views;
912 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
913 int fast_refresh = 10;
914 int done = 0, errcode;
916 errcode = pthread_mutex_lock(&tog_mutex);
917 if (errcode)
918 return got_error_set_errno(errcode, "pthread_mutex_lock");
920 TAILQ_INIT(&views);
921 TAILQ_INSERT_HEAD(&views, view, entry);
923 main_view = view;
924 view->focussed = 1;
925 err = view->show(view);
926 if (err)
927 return err;
928 update_panels();
929 doupdate();
930 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
931 /* Refresh fast during initialization, then become slower. */
932 if (fast_refresh && fast_refresh-- == 0)
933 halfdelay(10); /* switch to once per second */
935 err = view_input(&new_view, &dead_view, &focus_view, &done,
936 view, &views);
937 if (err)
938 break;
939 if (dead_view) {
940 struct tog_view *prev = NULL;
942 if (view_is_parent_view(dead_view))
943 prev = TAILQ_PREV(dead_view,
944 tog_view_list_head, entry);
945 else if (view->parent != dead_view)
946 prev = view->parent;
948 if (dead_view->parent)
949 dead_view->parent->child = NULL;
950 else
951 TAILQ_REMOVE(&views, dead_view, entry);
953 err = view_close(dead_view);
954 if (err || (dead_view == main_view && new_view == NULL))
955 goto done;
957 if (view == dead_view) {
958 if (focus_view)
959 view = focus_view;
960 else if (prev)
961 view = prev;
962 else if (!TAILQ_EMPTY(&views))
963 view = TAILQ_LAST(&views,
964 tog_view_list_head);
965 else
966 view = NULL;
967 if (view) {
968 if (view->child && view->child_focussed)
969 focus_view = view->child;
970 else
971 focus_view = view;
975 if (new_view) {
976 struct tog_view *v, *t;
977 /* Only allow one parent view per type. */
978 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
979 if (v->type != new_view->type)
980 continue;
981 TAILQ_REMOVE(&views, v, entry);
982 err = view_close(v);
983 if (err)
984 goto done;
985 break;
987 TAILQ_INSERT_TAIL(&views, new_view, entry);
988 view = new_view;
989 if (focus_view == NULL)
990 focus_view = new_view;
992 if (focus_view) {
993 show_panel(focus_view->panel);
994 if (view)
995 view->focussed = 0;
996 focus_view->focussed = 1;
997 view = focus_view;
998 if (new_view)
999 show_panel(new_view->panel);
1000 if (view->child && view_is_splitscreen(view->child))
1001 show_panel(view->child->panel);
1003 if (view) {
1004 if (focus_view == NULL) {
1005 view->focussed = 1;
1006 show_panel(view->panel);
1007 if (view->child && view_is_splitscreen(view->child))
1008 show_panel(view->child->panel);
1009 focus_view = view;
1011 if (view->parent) {
1012 err = view->parent->show(view->parent);
1013 if (err)
1014 goto done;
1016 err = view->show(view);
1017 if (err)
1018 goto done;
1019 if (view->child) {
1020 err = view->child->show(view->child);
1021 if (err)
1022 goto done;
1024 update_panels();
1025 doupdate();
1028 done:
1029 while (!TAILQ_EMPTY(&views)) {
1030 view = TAILQ_FIRST(&views);
1031 TAILQ_REMOVE(&views, view, entry);
1032 view_close(view);
1035 errcode = pthread_mutex_unlock(&tog_mutex);
1036 if (errcode && err == NULL)
1037 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1039 return err;
1042 __dead static void
1043 usage_log(void)
1045 endwin();
1046 fprintf(stderr,
1047 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1048 getprogname());
1049 exit(1);
1052 /* Create newly allocated wide-character string equivalent to a byte string. */
1053 static const struct got_error *
1054 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1056 char *vis = NULL;
1057 const struct got_error *err = NULL;
1059 *ws = NULL;
1060 *wlen = mbstowcs(NULL, s, 0);
1061 if (*wlen == (size_t)-1) {
1062 int vislen;
1063 if (errno != EILSEQ)
1064 return got_error_from_errno("mbstowcs");
1066 /* byte string invalid in current encoding; try to "fix" it */
1067 err = got_mbsavis(&vis, &vislen, s);
1068 if (err)
1069 return err;
1070 *wlen = mbstowcs(NULL, vis, 0);
1071 if (*wlen == (size_t)-1) {
1072 err = got_error_from_errno("mbstowcs"); /* give up */
1073 goto done;
1077 *ws = calloc(*wlen + 1, sizeof(**ws));
1078 if (*ws == NULL) {
1079 err = got_error_from_errno("calloc");
1080 goto done;
1083 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1084 err = got_error_from_errno("mbstowcs");
1085 done:
1086 free(vis);
1087 if (err) {
1088 free(*ws);
1089 *ws = NULL;
1090 *wlen = 0;
1092 return err;
1095 /* Format a line for display, ensuring that it won't overflow a width limit. */
1096 static const struct got_error *
1097 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1098 int col_tab_align)
1100 const struct got_error *err = NULL;
1101 int cols = 0;
1102 wchar_t *wline = NULL;
1103 size_t wlen;
1104 int i;
1106 *wlinep = NULL;
1107 *widthp = 0;
1109 err = mbs2ws(&wline, &wlen, line);
1110 if (err)
1111 return err;
1113 i = 0;
1114 while (i < wlen) {
1115 int width = wcwidth(wline[i]);
1117 if (width == 0) {
1118 i++;
1119 continue;
1122 if (width == 1 || width == 2) {
1123 if (cols + width > wlimit)
1124 break;
1125 cols += width;
1126 i++;
1127 } else if (width == -1) {
1128 if (wline[i] == L'\t') {
1129 width = TABSIZE -
1130 ((cols + col_tab_align) % TABSIZE);
1131 if (cols + width > wlimit)
1132 break;
1133 cols += width;
1135 i++;
1136 } else {
1137 err = got_error_from_errno("wcwidth");
1138 goto done;
1141 wline[i] = L'\0';
1142 if (widthp)
1143 *widthp = cols;
1144 done:
1145 if (err)
1146 free(wline);
1147 else
1148 *wlinep = wline;
1149 return err;
1152 static const struct got_error*
1153 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1154 struct got_object_id *id, struct got_repository *repo)
1156 static const struct got_error *err = NULL;
1157 struct got_reflist_entry *re;
1158 char *s;
1159 const char *name;
1161 *refs_str = NULL;
1163 SIMPLEQ_FOREACH(re, refs, entry) {
1164 struct got_tag_object *tag = NULL;
1165 struct got_object_id *ref_id;
1166 int cmp;
1168 name = got_ref_get_name(re->ref);
1169 if (strcmp(name, GOT_REF_HEAD) == 0)
1170 continue;
1171 if (strncmp(name, "refs/", 5) == 0)
1172 name += 5;
1173 if (strncmp(name, "got/", 4) == 0)
1174 continue;
1175 if (strncmp(name, "heads/", 6) == 0)
1176 name += 6;
1177 if (strncmp(name, "remotes/", 8) == 0) {
1178 name += 8;
1179 s = strstr(name, "/" GOT_REF_HEAD);
1180 if (s != NULL && s[strlen(s)] == '\0')
1181 continue;
1183 err = got_ref_resolve(&ref_id, repo, re->ref);
1184 if (err)
1185 break;
1186 if (strncmp(name, "tags/", 5) == 0) {
1187 err = got_object_open_as_tag(&tag, repo, ref_id);
1188 if (err) {
1189 if (err->code != GOT_ERR_OBJ_TYPE) {
1190 free(ref_id);
1191 break;
1193 /* Ref points at something other than a tag. */
1194 err = NULL;
1195 tag = NULL;
1198 cmp = got_object_id_cmp(tag ?
1199 got_object_tag_get_object_id(tag) : ref_id, id);
1200 free(ref_id);
1201 if (tag)
1202 got_object_tag_close(tag);
1203 if (cmp != 0)
1204 continue;
1205 s = *refs_str;
1206 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1207 s ? ", " : "", name) == -1) {
1208 err = got_error_from_errno("asprintf");
1209 free(s);
1210 *refs_str = NULL;
1211 break;
1213 free(s);
1216 return err;
1219 static const struct got_error *
1220 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1221 int col_tab_align)
1223 char *smallerthan, *at;
1225 smallerthan = strchr(author, '<');
1226 if (smallerthan && smallerthan[1] != '\0')
1227 author = smallerthan + 1;
1228 at = strchr(author, '@');
1229 if (at)
1230 *at = '\0';
1231 return format_line(wauthor, author_width, author, limit, col_tab_align);
1234 static const struct got_error *
1235 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1236 struct got_object_id *id, struct got_reflist_head *refs,
1237 const size_t date_display_cols, int author_display_cols,
1238 struct tog_colors *colors)
1240 const struct got_error *err = NULL;
1241 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1242 char *logmsg0 = NULL, *logmsg = NULL;
1243 char *author = NULL;
1244 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1245 int author_width, logmsg_width;
1246 char *newline, *line = NULL;
1247 int col, limit;
1248 const int avail = view->ncols;
1249 struct tm tm;
1250 time_t committer_time;
1251 struct tog_color *tc;
1253 committer_time = got_object_commit_get_committer_time(commit);
1254 if (localtime_r(&committer_time, &tm) == NULL)
1255 return got_error_from_errno("localtime_r");
1256 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1257 >= sizeof(datebuf))
1258 return got_error(GOT_ERR_NO_SPACE);
1260 if (avail <= date_display_cols)
1261 limit = MIN(sizeof(datebuf) - 1, avail);
1262 else
1263 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1264 tc = get_color(colors, TOG_COLOR_DATE);
1265 if (tc)
1266 wattr_on(view->window,
1267 COLOR_PAIR(tc->colorpair), NULL);
1268 waddnstr(view->window, datebuf, limit);
1269 if (tc)
1270 wattr_off(view->window,
1271 COLOR_PAIR(tc->colorpair), NULL);
1272 col = limit;
1273 if (col > avail)
1274 goto done;
1276 if (avail >= 120) {
1277 char *id_str;
1278 err = got_object_id_str(&id_str, id);
1279 if (err)
1280 goto done;
1281 tc = get_color(colors, TOG_COLOR_COMMIT);
1282 if (tc)
1283 wattr_on(view->window,
1284 COLOR_PAIR(tc->colorpair), NULL);
1285 wprintw(view->window, "%.8s ", id_str);
1286 if (tc)
1287 wattr_off(view->window,
1288 COLOR_PAIR(tc->colorpair), NULL);
1289 free(id_str);
1290 col += 9;
1291 if (col > avail)
1292 goto done;
1295 author = strdup(got_object_commit_get_author(commit));
1296 if (author == NULL) {
1297 err = got_error_from_errno("strdup");
1298 goto done;
1300 err = format_author(&wauthor, &author_width, author, avail - col, col);
1301 if (err)
1302 goto done;
1303 tc = get_color(colors, TOG_COLOR_AUTHOR);
1304 if (tc)
1305 wattr_on(view->window,
1306 COLOR_PAIR(tc->colorpair), NULL);
1307 waddwstr(view->window, wauthor);
1308 if (tc)
1309 wattr_off(view->window,
1310 COLOR_PAIR(tc->colorpair), NULL);
1311 col += author_width;
1312 while (col < avail && author_width < author_display_cols + 2) {
1313 waddch(view->window, ' ');
1314 col++;
1315 author_width++;
1317 if (col > avail)
1318 goto done;
1320 err = got_object_commit_get_logmsg(&logmsg0, commit);
1321 if (err)
1322 goto done;
1323 logmsg = logmsg0;
1324 while (*logmsg == '\n')
1325 logmsg++;
1326 newline = strchr(logmsg, '\n');
1327 if (newline)
1328 *newline = '\0';
1329 limit = avail - col;
1330 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1331 if (err)
1332 goto done;
1333 waddwstr(view->window, wlogmsg);
1334 col += logmsg_width;
1335 while (col < avail) {
1336 waddch(view->window, ' ');
1337 col++;
1339 done:
1340 free(logmsg0);
1341 free(wlogmsg);
1342 free(author);
1343 free(wauthor);
1344 free(line);
1345 return err;
1348 static struct commit_queue_entry *
1349 alloc_commit_queue_entry(struct got_commit_object *commit,
1350 struct got_object_id *id)
1352 struct commit_queue_entry *entry;
1354 entry = calloc(1, sizeof(*entry));
1355 if (entry == NULL)
1356 return NULL;
1358 entry->id = id;
1359 entry->commit = commit;
1360 return entry;
1363 static void
1364 pop_commit(struct commit_queue *commits)
1366 struct commit_queue_entry *entry;
1368 entry = TAILQ_FIRST(&commits->head);
1369 TAILQ_REMOVE(&commits->head, entry, entry);
1370 got_object_commit_close(entry->commit);
1371 commits->ncommits--;
1372 /* Don't free entry->id! It is owned by the commit graph. */
1373 free(entry);
1376 static void
1377 free_commits(struct commit_queue *commits)
1379 while (!TAILQ_EMPTY(&commits->head))
1380 pop_commit(commits);
1383 static const struct got_error *
1384 match_commit(int *have_match, struct got_object_id *id,
1385 struct got_commit_object *commit, regex_t *regex)
1387 const struct got_error *err = NULL;
1388 regmatch_t regmatch;
1389 char *id_str = NULL, *logmsg = NULL;
1391 *have_match = 0;
1393 err = got_object_id_str(&id_str, id);
1394 if (err)
1395 return err;
1397 err = got_object_commit_get_logmsg(&logmsg, commit);
1398 if (err)
1399 goto done;
1401 if (regexec(regex, got_object_commit_get_author(commit), 1,
1402 &regmatch, 0) == 0 ||
1403 regexec(regex, got_object_commit_get_committer(commit), 1,
1404 &regmatch, 0) == 0 ||
1405 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1406 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1407 *have_match = 1;
1408 done:
1409 free(id_str);
1410 free(logmsg);
1411 return err;
1414 static const struct got_error *
1415 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1416 int minqueue, struct got_repository *repo, const char *path,
1417 int *searching, int *search_next_done, regex_t *regex)
1419 const struct got_error *err = NULL;
1420 int nqueued = 0;
1423 * We keep all commits open throughout the lifetime of the log
1424 * view in order to avoid having to re-fetch commits from disk
1425 * while updating the display.
1427 while (nqueued < minqueue ||
1428 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1429 struct got_object_id *id;
1430 struct got_commit_object *commit;
1431 struct commit_queue_entry *entry;
1432 int errcode;
1434 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1435 if (err || id == NULL)
1436 break;
1438 err = got_object_open_as_commit(&commit, repo, id);
1439 if (err)
1440 break;
1441 entry = alloc_commit_queue_entry(commit, id);
1442 if (entry == NULL) {
1443 err = got_error_from_errno("alloc_commit_queue_entry");
1444 break;
1447 errcode = pthread_mutex_lock(&tog_mutex);
1448 if (errcode) {
1449 err = got_error_set_errno(errcode,
1450 "pthread_mutex_lock");
1451 break;
1454 entry->idx = commits->ncommits;
1455 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1456 nqueued++;
1457 commits->ncommits++;
1459 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1460 int have_match;
1461 err = match_commit(&have_match, id, commit, regex);
1462 if (err)
1463 break;
1464 if (have_match)
1465 *search_next_done = TOG_SEARCH_HAVE_MORE;
1468 errcode = pthread_mutex_unlock(&tog_mutex);
1469 if (errcode && err == NULL)
1470 err = got_error_set_errno(errcode,
1471 "pthread_mutex_unlock");
1472 if (err)
1473 break;
1476 return err;
1479 static const struct got_error *
1480 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1481 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1482 struct commit_queue *commits, int selected_idx, int limit,
1483 struct got_reflist_head *refs, const char *path, int commits_needed,
1484 struct tog_colors *colors)
1486 const struct got_error *err = NULL;
1487 struct tog_log_view_state *s = &view->state.log;
1488 struct commit_queue_entry *entry;
1489 int width;
1490 int ncommits, author_cols = 4;
1491 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1492 char *refs_str = NULL;
1493 wchar_t *wline;
1494 struct tog_color *tc;
1495 static const size_t date_display_cols = 12;
1497 entry = first;
1498 ncommits = 0;
1499 while (entry) {
1500 if (ncommits == selected_idx) {
1501 *selected = entry;
1502 break;
1504 entry = TAILQ_NEXT(entry, entry);
1505 ncommits++;
1508 if (*selected && !(view->searching && view->search_next_done == 0)) {
1509 err = got_object_id_str(&id_str, (*selected)->id);
1510 if (err)
1511 return err;
1512 if (refs) {
1513 err = build_refs_str(&refs_str, refs, (*selected)->id,
1514 s->repo);
1515 if (err)
1516 goto done;
1520 if (commits_needed == 0)
1521 halfdelay(10); /* disable fast refresh */
1523 if (commits_needed > 0) {
1524 if (asprintf(&ncommits_str, " [%d/%d] %s",
1525 entry ? entry->idx + 1 : 0, commits->ncommits,
1526 (view->searching && !view->search_next_done) ?
1527 "searching..." : "loading...") == -1) {
1528 err = got_error_from_errno("asprintf");
1529 goto done;
1531 } else {
1532 const char *search_str = NULL;
1534 if (view->searching) {
1535 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1536 search_str = "no more matches";
1537 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1538 search_str = "no matches found";
1539 else if (!view->search_next_done)
1540 search_str = "searching...";
1543 if (asprintf(&ncommits_str, " [%d/%d] %s",
1544 entry ? entry->idx + 1 : 0, commits->ncommits,
1545 search_str ? search_str :
1546 (refs_str ? refs_str : "")) == -1) {
1547 err = got_error_from_errno("asprintf");
1548 goto done;
1552 if (path && strcmp(path, "/") != 0) {
1553 if (asprintf(&header, "commit %s %s%s",
1554 id_str ? id_str : "........................................",
1555 path, ncommits_str) == -1) {
1556 err = got_error_from_errno("asprintf");
1557 header = NULL;
1558 goto done;
1560 } else if (asprintf(&header, "commit %s%s",
1561 id_str ? id_str : "........................................",
1562 ncommits_str) == -1) {
1563 err = got_error_from_errno("asprintf");
1564 header = NULL;
1565 goto done;
1567 err = format_line(&wline, &width, header, view->ncols, 0);
1568 if (err)
1569 goto done;
1571 werase(view->window);
1573 if (view_needs_focus_indication(view))
1574 wstandout(view->window);
1575 tc = get_color(colors, TOG_COLOR_COMMIT);
1576 if (tc)
1577 wattr_on(view->window,
1578 COLOR_PAIR(tc->colorpair), NULL);
1579 waddwstr(view->window, wline);
1580 if (tc)
1581 wattr_off(view->window,
1582 COLOR_PAIR(tc->colorpair), NULL);
1583 while (width < view->ncols) {
1584 waddch(view->window, ' ');
1585 width++;
1587 if (view_needs_focus_indication(view))
1588 wstandend(view->window);
1589 free(wline);
1590 if (limit <= 1)
1591 goto done;
1593 /* Grow author column size if necessary. */
1594 entry = first;
1595 ncommits = 0;
1596 while (entry) {
1597 char *author;
1598 wchar_t *wauthor;
1599 int width;
1600 if (ncommits >= limit - 1)
1601 break;
1602 author = strdup(got_object_commit_get_author(entry->commit));
1603 if (author == NULL) {
1604 err = got_error_from_errno("strdup");
1605 goto done;
1607 err = format_author(&wauthor, &width, author, COLS,
1608 date_display_cols);
1609 if (author_cols < width)
1610 author_cols = width;
1611 free(wauthor);
1612 free(author);
1613 ncommits++;
1614 entry = TAILQ_NEXT(entry, entry);
1617 entry = first;
1618 *last = first;
1619 ncommits = 0;
1620 while (entry) {
1621 if (ncommits >= limit - 1)
1622 break;
1623 if (ncommits == selected_idx)
1624 wstandout(view->window);
1625 err = draw_commit(view, entry->commit, entry->id, refs,
1626 date_display_cols, author_cols, colors);
1627 if (ncommits == selected_idx)
1628 wstandend(view->window);
1629 if (err)
1630 goto done;
1631 ncommits++;
1632 *last = entry;
1633 entry = TAILQ_NEXT(entry, entry);
1636 view_vborder(view);
1637 done:
1638 free(id_str);
1639 free(refs_str);
1640 free(ncommits_str);
1641 free(header);
1642 return err;
1645 static void
1646 scroll_up(struct tog_view *view,
1647 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1648 struct commit_queue *commits)
1650 struct commit_queue_entry *entry;
1651 int nscrolled = 0;
1653 entry = TAILQ_FIRST(&commits->head);
1654 if (*first_displayed_entry == entry)
1655 return;
1657 entry = *first_displayed_entry;
1658 while (entry && nscrolled < maxscroll) {
1659 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1660 if (entry) {
1661 *first_displayed_entry = entry;
1662 nscrolled++;
1667 static const struct got_error *
1668 trigger_log_thread(struct tog_view *log_view, int wait,
1669 int *commits_needed, int *log_complete,
1670 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1672 int errcode;
1674 halfdelay(1); /* fast refresh while loading commits */
1676 while (*commits_needed > 0) {
1677 if (*log_complete)
1678 break;
1680 /* Wake the log thread. */
1681 errcode = pthread_cond_signal(need_commits);
1682 if (errcode)
1683 return got_error_set_errno(errcode,
1684 "pthread_cond_signal");
1687 * The mutex will be released while the view loop waits
1688 * in wgetch(), at which time the log thread will run.
1690 if (!wait)
1691 break;
1693 /* Display progress update in log view. */
1694 show_log_view(log_view);
1695 update_panels();
1696 doupdate();
1698 /* Wait right here while next commit is being loaded. */
1699 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1700 if (errcode)
1701 return got_error_set_errno(errcode,
1702 "pthread_cond_wait");
1704 /* Display progress update in log view. */
1705 show_log_view(log_view);
1706 update_panels();
1707 doupdate();
1710 return NULL;
1713 static const struct got_error *
1714 scroll_down(struct tog_view *log_view,
1715 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1716 struct commit_queue_entry **last_displayed_entry,
1717 struct commit_queue *commits, int *log_complete, int *commits_needed,
1718 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1720 const struct got_error *err = NULL;
1721 struct commit_queue_entry *pentry;
1722 int nscrolled = 0, ncommits_needed;
1724 if (*last_displayed_entry == NULL)
1725 return NULL;
1727 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1728 if (commits->ncommits < ncommits_needed && !*log_complete) {
1730 * Ask the log thread for required amount of commits.
1732 (*commits_needed) += maxscroll;
1733 err = trigger_log_thread(log_view, 1, commits_needed,
1734 log_complete, need_commits, commit_loaded);
1735 if (err)
1736 return err;
1739 do {
1740 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1741 if (pentry == NULL)
1742 break;
1744 *last_displayed_entry = pentry;
1746 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1747 if (pentry == NULL)
1748 break;
1749 *first_displayed_entry = pentry;
1750 } while (++nscrolled < maxscroll);
1752 return err;
1755 static const struct got_error *
1756 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1757 struct got_commit_object *commit, struct got_object_id *commit_id,
1758 struct tog_view *log_view, struct got_reflist_head *refs,
1759 struct got_repository *repo)
1761 const struct got_error *err;
1762 struct got_object_qid *parent_id;
1763 struct tog_view *diff_view;
1765 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1766 if (diff_view == NULL)
1767 return got_error_from_errno("view_open");
1769 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1770 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1771 commit_id, log_view, refs, repo);
1772 if (err == NULL)
1773 *new_view = diff_view;
1774 return err;
1777 static const struct got_error *
1778 tree_view_visit_subtree(struct got_tree_object *subtree,
1779 struct tog_tree_view_state *s)
1781 struct tog_parent_tree *parent;
1783 parent = calloc(1, sizeof(*parent));
1784 if (parent == NULL)
1785 return got_error_from_errno("calloc");
1787 parent->tree = s->tree;
1788 parent->first_displayed_entry = s->first_displayed_entry;
1789 parent->selected_entry = s->selected_entry;
1790 parent->selected = s->selected;
1791 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1792 s->tree = subtree;
1793 s->selected = 0;
1794 s->first_displayed_entry = NULL;
1795 return NULL;
1798 static const struct got_error *
1799 tree_view_walk_path(struct tog_tree_view_state *s,
1800 struct got_object_id *commit_id,
1801 const char *path, struct got_repository *repo)
1803 const struct got_error *err = NULL;
1804 struct got_tree_object *tree = NULL;
1805 const char *p;
1806 char *slash, *subpath = NULL;
1808 /* Walk the path and open corresponding tree objects. */
1809 p = path;
1810 while (*p) {
1811 struct got_tree_entry *te;
1812 struct got_object_id *tree_id;
1813 char *te_name;
1815 while (p[0] == '/')
1816 p++;
1818 /* Ensure the correct subtree entry is selected. */
1819 slash = strchr(p, '/');
1820 if (slash == NULL)
1821 te_name = strdup(p);
1822 else
1823 te_name = strndup(p, slash - p);
1824 if (te_name == NULL) {
1825 err = got_error_from_errno("strndup");
1826 break;
1828 te = got_object_tree_find_entry(s->tree, te_name);
1829 if (te == NULL) {
1830 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1831 free(te_name);
1832 break;
1834 free(te_name);
1835 s->selected_entry = te;
1836 s->selected = got_tree_entry_get_index(te);
1837 if (s->tree != s->root)
1838 s->selected++; /* skip '..' */
1840 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1841 /* Jump to this file's entry. */
1842 s->first_displayed_entry = s->selected_entry;
1843 s->selected = 0;
1844 break;
1847 slash = strchr(p, '/');
1848 if (slash)
1849 subpath = strndup(path, slash - path);
1850 else
1851 subpath = strdup(path);
1852 if (subpath == NULL) {
1853 err = got_error_from_errno("strdup");
1854 break;
1857 err = got_object_id_by_path(&tree_id, repo, commit_id,
1858 subpath);
1859 if (err)
1860 break;
1862 err = got_object_open_as_tree(&tree, repo, tree_id);
1863 free(tree_id);
1864 if (err)
1865 break;
1867 err = tree_view_visit_subtree(tree, s);
1868 if (err) {
1869 got_object_tree_close(tree);
1870 break;
1872 if (slash == NULL)
1873 break;
1874 free(subpath);
1875 subpath = NULL;
1876 p = slash;
1879 free(subpath);
1880 return err;
1883 static const struct got_error *
1884 browse_commit_tree(struct tog_view **new_view, int begin_x,
1885 struct commit_queue_entry *entry, const char *path,
1886 struct got_reflist_head *refs, struct got_repository *repo)
1888 const struct got_error *err = NULL;
1889 struct got_tree_object *tree;
1890 struct tog_tree_view_state *s;
1891 struct tog_view *tree_view;
1893 err = got_object_open_as_tree(&tree, repo,
1894 got_object_commit_get_tree_id(entry->commit));
1895 if (err)
1896 return err;
1898 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1899 if (tree_view == NULL)
1900 return got_error_from_errno("view_open");
1902 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1903 if (err) {
1904 got_object_tree_close(tree);
1905 return err;
1907 s = &tree_view->state.tree;
1909 *new_view = tree_view;
1911 if (got_path_is_root_dir(path))
1912 return NULL;
1914 return tree_view_walk_path(s, entry->id, path, repo);
1917 static const struct got_error *
1918 block_signals_used_by_main_thread(void)
1920 sigset_t sigset;
1921 int errcode;
1923 if (sigemptyset(&sigset) == -1)
1924 return got_error_from_errno("sigemptyset");
1926 /* tog handles SIGWINCH and SIGCONT */
1927 if (sigaddset(&sigset, SIGWINCH) == -1)
1928 return got_error_from_errno("sigaddset");
1929 if (sigaddset(&sigset, SIGCONT) == -1)
1930 return got_error_from_errno("sigaddset");
1932 /* ncurses handles SIGTSTP */
1933 if (sigaddset(&sigset, SIGTSTP) == -1)
1934 return got_error_from_errno("sigaddset");
1936 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1937 if (errcode)
1938 return got_error_set_errno(errcode, "pthread_sigmask");
1940 return NULL;
1943 static void *
1944 log_thread(void *arg)
1946 const struct got_error *err = NULL;
1947 int errcode = 0;
1948 struct tog_log_thread_args *a = arg;
1949 int done = 0;
1951 err = block_signals_used_by_main_thread();
1952 if (err)
1953 return (void *)err;
1955 while (!done && !err && !tog_sigpipe_received) {
1956 err = queue_commits(a->graph, a->commits, 1, a->repo,
1957 a->in_repo_path, a->searching, a->search_next_done,
1958 a->regex);
1959 if (err) {
1960 if (err->code != GOT_ERR_ITER_COMPLETED)
1961 return (void *)err;
1962 err = NULL;
1963 done = 1;
1964 } else if (a->commits_needed > 0)
1965 a->commits_needed--;
1967 errcode = pthread_mutex_lock(&tog_mutex);
1968 if (errcode) {
1969 err = got_error_set_errno(errcode,
1970 "pthread_mutex_lock");
1971 break;
1972 } else if (*a->quit)
1973 done = 1;
1974 else if (*a->first_displayed_entry == NULL) {
1975 *a->first_displayed_entry =
1976 TAILQ_FIRST(&a->commits->head);
1977 *a->selected_entry = *a->first_displayed_entry;
1980 errcode = pthread_cond_signal(&a->commit_loaded);
1981 if (errcode) {
1982 err = got_error_set_errno(errcode,
1983 "pthread_cond_signal");
1984 pthread_mutex_unlock(&tog_mutex);
1985 break;
1988 if (done)
1989 a->commits_needed = 0;
1990 else {
1991 if (a->commits_needed == 0) {
1992 errcode = pthread_cond_wait(&a->need_commits,
1993 &tog_mutex);
1994 if (errcode)
1995 err = got_error_set_errno(errcode,
1996 "pthread_cond_wait");
2000 errcode = pthread_mutex_unlock(&tog_mutex);
2001 if (errcode && err == NULL)
2002 err = got_error_set_errno(errcode,
2003 "pthread_mutex_unlock");
2005 a->log_complete = 1;
2006 return (void *)err;
2009 static const struct got_error *
2010 stop_log_thread(struct tog_log_view_state *s)
2012 const struct got_error *err = NULL;
2013 int errcode;
2015 if (s->thread) {
2016 s->quit = 1;
2017 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2018 if (errcode)
2019 return got_error_set_errno(errcode,
2020 "pthread_cond_signal");
2021 errcode = pthread_mutex_unlock(&tog_mutex);
2022 if (errcode)
2023 return got_error_set_errno(errcode,
2024 "pthread_mutex_unlock");
2025 errcode = pthread_join(s->thread, (void **)&err);
2026 if (errcode)
2027 return got_error_set_errno(errcode, "pthread_join");
2028 errcode = pthread_mutex_lock(&tog_mutex);
2029 if (errcode)
2030 return got_error_set_errno(errcode,
2031 "pthread_mutex_lock");
2032 s->thread = NULL;
2035 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2036 if (errcode && err == NULL)
2037 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2039 if (s->thread_args.repo) {
2040 got_repo_close(s->thread_args.repo);
2041 s->thread_args.repo = NULL;
2044 if (s->thread_args.graph) {
2045 got_commit_graph_close(s->thread_args.graph);
2046 s->thread_args.graph = NULL;
2049 return err;
2052 static const struct got_error *
2053 close_log_view(struct tog_view *view)
2055 const struct got_error *err = NULL;
2056 struct tog_log_view_state *s = &view->state.log;
2058 err = stop_log_thread(s);
2059 free_commits(&s->commits);
2060 free(s->in_repo_path);
2061 s->in_repo_path = NULL;
2062 free(s->start_id);
2063 s->start_id = NULL;
2064 return err;
2067 static const struct got_error *
2068 search_start_log_view(struct tog_view *view)
2070 struct tog_log_view_state *s = &view->state.log;
2072 s->matched_entry = NULL;
2073 s->search_entry = NULL;
2074 return NULL;
2077 static const struct got_error *
2078 search_next_log_view(struct tog_view *view)
2080 const struct got_error *err = NULL;
2081 struct tog_log_view_state *s = &view->state.log;
2082 struct commit_queue_entry *entry;
2084 /* Display progress update in log view. */
2085 show_log_view(view);
2086 update_panels();
2087 doupdate();
2089 if (s->search_entry) {
2090 int errcode, ch;
2091 errcode = pthread_mutex_unlock(&tog_mutex);
2092 if (errcode)
2093 return got_error_set_errno(errcode,
2094 "pthread_mutex_unlock");
2095 ch = wgetch(view->window);
2096 errcode = pthread_mutex_lock(&tog_mutex);
2097 if (errcode)
2098 return got_error_set_errno(errcode,
2099 "pthread_mutex_lock");
2100 if (ch == KEY_BACKSPACE) {
2101 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2102 return NULL;
2104 if (view->searching == TOG_SEARCH_FORWARD)
2105 entry = TAILQ_NEXT(s->search_entry, entry);
2106 else
2107 entry = TAILQ_PREV(s->search_entry,
2108 commit_queue_head, entry);
2109 } else if (s->matched_entry) {
2110 if (view->searching == TOG_SEARCH_FORWARD)
2111 entry = TAILQ_NEXT(s->matched_entry, entry);
2112 else
2113 entry = TAILQ_PREV(s->matched_entry,
2114 commit_queue_head, entry);
2115 } else {
2116 if (view->searching == TOG_SEARCH_FORWARD)
2117 entry = TAILQ_FIRST(&s->commits.head);
2118 else
2119 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2122 while (1) {
2123 int have_match = 0;
2125 if (entry == NULL) {
2126 if (s->thread_args.log_complete ||
2127 view->searching == TOG_SEARCH_BACKWARD) {
2128 view->search_next_done =
2129 (s->matched_entry == NULL ?
2130 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2131 s->search_entry = NULL;
2132 return NULL;
2135 * Poke the log thread for more commits and return,
2136 * allowing the main loop to make progress. Search
2137 * will resume at s->search_entry once we come back.
2139 s->thread_args.commits_needed++;
2140 return trigger_log_thread(view, 0,
2141 &s->thread_args.commits_needed,
2142 &s->thread_args.log_complete,
2143 &s->thread_args.need_commits,
2144 &s->thread_args.commit_loaded);
2147 err = match_commit(&have_match, entry->id, entry->commit,
2148 &view->regex);
2149 if (err)
2150 break;
2151 if (have_match) {
2152 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2153 s->matched_entry = entry;
2154 break;
2157 s->search_entry = entry;
2158 if (view->searching == TOG_SEARCH_FORWARD)
2159 entry = TAILQ_NEXT(entry, entry);
2160 else
2161 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2164 if (s->matched_entry) {
2165 int cur = s->selected_entry->idx;
2166 while (cur < s->matched_entry->idx) {
2167 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2168 if (err)
2169 return err;
2170 cur++;
2172 while (cur > s->matched_entry->idx) {
2173 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2174 if (err)
2175 return err;
2176 cur--;
2180 s->search_entry = NULL;
2182 return NULL;
2185 static const struct got_error *
2186 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2187 struct got_reflist_head *refs, struct got_repository *repo,
2188 const char *head_ref_name, const char *in_repo_path,
2189 int log_branches)
2191 const struct got_error *err = NULL;
2192 struct tog_log_view_state *s = &view->state.log;
2193 struct got_repository *thread_repo = NULL;
2194 struct got_commit_graph *thread_graph = NULL;
2195 int errcode;
2197 if (in_repo_path != s->in_repo_path) {
2198 free(s->in_repo_path);
2199 s->in_repo_path = strdup(in_repo_path);
2200 if (s->in_repo_path == NULL)
2201 return got_error_from_errno("strdup");
2204 /* The commit queue only contains commits being displayed. */
2205 TAILQ_INIT(&s->commits.head);
2206 s->commits.ncommits = 0;
2208 s->refs = refs;
2209 s->repo = repo;
2210 s->head_ref_name = head_ref_name;
2211 s->start_id = got_object_id_dup(start_id);
2212 if (s->start_id == NULL) {
2213 err = got_error_from_errno("got_object_id_dup");
2214 goto done;
2216 s->log_branches = log_branches;
2218 SIMPLEQ_INIT(&s->colors);
2219 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2220 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2221 get_color_value("TOG_COLOR_COMMIT"));
2222 if (err)
2223 goto done;
2224 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2225 get_color_value("TOG_COLOR_AUTHOR"));
2226 if (err) {
2227 free_colors(&s->colors);
2228 goto done;
2230 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2231 get_color_value("TOG_COLOR_DATE"));
2232 if (err) {
2233 free_colors(&s->colors);
2234 goto done;
2238 view->show = show_log_view;
2239 view->input = input_log_view;
2240 view->close = close_log_view;
2241 view->search_start = search_start_log_view;
2242 view->search_next = search_next_log_view;
2244 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2245 if (err)
2246 goto done;
2247 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2248 !s->log_branches);
2249 if (err)
2250 goto done;
2251 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2252 s->repo, NULL, NULL);
2253 if (err)
2254 goto done;
2256 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2257 if (errcode) {
2258 err = got_error_set_errno(errcode, "pthread_cond_init");
2259 goto done;
2261 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2262 if (errcode) {
2263 err = got_error_set_errno(errcode, "pthread_cond_init");
2264 goto done;
2267 s->thread_args.commits_needed = view->nlines;
2268 s->thread_args.graph = thread_graph;
2269 s->thread_args.commits = &s->commits;
2270 s->thread_args.in_repo_path = s->in_repo_path;
2271 s->thread_args.start_id = s->start_id;
2272 s->thread_args.repo = thread_repo;
2273 s->thread_args.log_complete = 0;
2274 s->thread_args.quit = &s->quit;
2275 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2276 s->thread_args.selected_entry = &s->selected_entry;
2277 s->thread_args.searching = &view->searching;
2278 s->thread_args.search_next_done = &view->search_next_done;
2279 s->thread_args.regex = &view->regex;
2280 done:
2281 if (err)
2282 close_log_view(view);
2283 return err;
2286 static const struct got_error *
2287 show_log_view(struct tog_view *view)
2289 struct tog_log_view_state *s = &view->state.log;
2291 if (s->thread == NULL) {
2292 int errcode = pthread_create(&s->thread, NULL, log_thread,
2293 &s->thread_args);
2294 if (errcode)
2295 return got_error_set_errno(errcode, "pthread_create");
2298 return draw_commits(view, &s->last_displayed_entry,
2299 &s->selected_entry, s->first_displayed_entry,
2300 &s->commits, s->selected, view->nlines, s->refs,
2301 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2304 static const struct got_error *
2305 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2306 struct tog_view **focus_view, struct tog_view *view, int ch)
2308 const struct got_error *err = NULL;
2309 struct tog_log_view_state *s = &view->state.log;
2310 char *parent_path, *in_repo_path = NULL;
2311 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2312 int begin_x = 0;
2313 struct got_object_id *start_id;
2315 switch (ch) {
2316 case 'q':
2317 s->quit = 1;
2318 break;
2319 case 'k':
2320 case KEY_UP:
2321 case '<':
2322 case ',':
2323 if (s->first_displayed_entry == NULL)
2324 break;
2325 if (s->selected > 0)
2326 s->selected--;
2327 else
2328 scroll_up(view, &s->first_displayed_entry, 1,
2329 &s->commits);
2330 break;
2331 case KEY_PPAGE:
2332 case CTRL('b'):
2333 if (s->first_displayed_entry == NULL)
2334 break;
2335 if (TAILQ_FIRST(&s->commits.head) ==
2336 s->first_displayed_entry) {
2337 s->selected = 0;
2338 break;
2340 scroll_up(view, &s->first_displayed_entry,
2341 view->nlines - 1, &s->commits);
2342 break;
2343 case 'j':
2344 case KEY_DOWN:
2345 case '>':
2346 case '.':
2347 if (s->first_displayed_entry == NULL)
2348 break;
2349 if (s->selected < MIN(view->nlines - 2,
2350 s->commits.ncommits - 1)) {
2351 s->selected++;
2352 break;
2354 err = scroll_down(view, &s->first_displayed_entry, 1,
2355 &s->last_displayed_entry, &s->commits,
2356 &s->thread_args.log_complete,
2357 &s->thread_args.commits_needed,
2358 &s->thread_args.need_commits,
2359 &s->thread_args.commit_loaded);
2360 break;
2361 case KEY_NPAGE:
2362 case CTRL('f'): {
2363 struct commit_queue_entry *first;
2364 first = s->first_displayed_entry;
2365 if (first == NULL)
2366 break;
2367 err = scroll_down(view, &s->first_displayed_entry,
2368 view->nlines - 1, &s->last_displayed_entry,
2369 &s->commits, &s->thread_args.log_complete,
2370 &s->thread_args.commits_needed,
2371 &s->thread_args.need_commits,
2372 &s->thread_args.commit_loaded);
2373 if (err)
2374 break;
2375 if (first == s->first_displayed_entry &&
2376 s->selected < MIN(view->nlines - 2,
2377 s->commits.ncommits - 1)) {
2378 /* can't scroll further down */
2379 s->selected = MIN(view->nlines - 2,
2380 s->commits.ncommits - 1);
2382 err = NULL;
2383 break;
2385 case KEY_RESIZE:
2386 if (s->selected > view->nlines - 2)
2387 s->selected = view->nlines - 2;
2388 if (s->selected > s->commits.ncommits - 1)
2389 s->selected = s->commits.ncommits - 1;
2390 break;
2391 case KEY_ENTER:
2392 case ' ':
2393 case '\r':
2394 if (s->selected_entry == NULL)
2395 break;
2396 if (view_is_parent_view(view))
2397 begin_x = view_split_begin_x(view->begin_x);
2398 err = open_diff_view_for_commit(&diff_view, begin_x,
2399 s->selected_entry->commit, s->selected_entry->id,
2400 view, s->refs, s->repo);
2401 if (err)
2402 break;
2403 if (view_is_parent_view(view)) {
2404 err = view_close_child(view);
2405 if (err)
2406 return err;
2407 err = view_set_child(view, diff_view);
2408 if (err) {
2409 view_close(diff_view);
2410 break;
2412 *focus_view = diff_view;
2413 view->child_focussed = 1;
2414 } else
2415 *new_view = diff_view;
2416 break;
2417 case 't':
2418 if (s->selected_entry == NULL)
2419 break;
2420 if (view_is_parent_view(view))
2421 begin_x = view_split_begin_x(view->begin_x);
2422 err = browse_commit_tree(&tree_view, begin_x,
2423 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2424 if (err)
2425 break;
2426 if (view_is_parent_view(view)) {
2427 err = view_close_child(view);
2428 if (err)
2429 return err;
2430 err = view_set_child(view, tree_view);
2431 if (err) {
2432 view_close(tree_view);
2433 break;
2435 *focus_view = tree_view;
2436 view->child_focussed = 1;
2437 } else
2438 *new_view = tree_view;
2439 break;
2440 case KEY_BACKSPACE:
2441 if (got_path_cmp(s->in_repo_path, "/",
2442 strlen(s->in_repo_path), 1) == 0)
2443 break;
2444 err = got_path_dirname(&parent_path, s->in_repo_path);
2445 if (err)
2446 return err;
2447 err = stop_log_thread(s);
2448 if (err) {
2449 free(parent_path);
2450 return err;
2452 lv = view_open(view->nlines, view->ncols,
2453 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2454 if (lv == NULL) {
2455 free(parent_path);
2456 return got_error_from_errno("view_open");
2458 err = open_log_view(lv, s->start_id, s->refs,
2459 s->repo, s->head_ref_name, parent_path,
2460 s->log_branches);
2461 free(parent_path);
2462 if (err)
2463 return err;;
2464 if (view_is_parent_view(view))
2465 *new_view = lv;
2466 else {
2467 view_set_child(view->parent, lv);
2468 *focus_view = lv;
2470 break;
2471 case CTRL('l'):
2472 err = stop_log_thread(s);
2473 if (err)
2474 return err;
2475 lv = view_open(view->nlines, view->ncols,
2476 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2477 if (lv == NULL)
2478 return got_error_from_errno("view_open");
2479 err = got_repo_match_object_id(&start_id, NULL,
2480 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2481 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2482 if (err) {
2483 view_close(lv);
2484 return err;
2486 in_repo_path = strdup(s->in_repo_path);
2487 if (in_repo_path == NULL) {
2488 free(start_id);
2489 view_close(lv);
2490 return got_error_from_errno("strdup");
2492 got_ref_list_free(s->refs);
2493 err = got_ref_list(s->refs, s->repo, NULL,
2494 got_ref_cmp_by_name, NULL);
2495 if (err) {
2496 free(start_id);
2497 view_close(lv);
2498 return err;
2500 err = open_log_view(lv, start_id, s->refs, s->repo,
2501 s->head_ref_name, in_repo_path, s->log_branches);
2502 if (err) {
2503 free(start_id);
2504 view_close(lv);
2505 return err;;
2507 *dead_view = view;
2508 *new_view = lv;
2509 break;
2510 case 'B':
2511 s->log_branches = !s->log_branches;
2512 err = stop_log_thread(s);
2513 if (err)
2514 return err;
2515 lv = view_open(view->nlines, view->ncols,
2516 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2517 if (lv == NULL)
2518 return got_error_from_errno("view_open");
2519 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2520 s->head_ref_name, s->in_repo_path, s->log_branches);
2521 if (err) {
2522 view_close(lv);
2523 return err;;
2525 *dead_view = view;
2526 *new_view = lv;
2527 break;
2528 default:
2529 break;
2532 return err;
2535 static const struct got_error *
2536 apply_unveil(const char *repo_path, const char *worktree_path)
2538 const struct got_error *error;
2540 #ifdef PROFILE
2541 if (unveil("gmon.out", "rwc") != 0)
2542 return got_error_from_errno2("unveil", "gmon.out");
2543 #endif
2544 if (repo_path && unveil(repo_path, "r") != 0)
2545 return got_error_from_errno2("unveil", repo_path);
2547 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2548 return got_error_from_errno2("unveil", worktree_path);
2550 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2551 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2553 error = got_privsep_unveil_exec_helpers();
2554 if (error != NULL)
2555 return error;
2557 if (unveil(NULL, NULL) != 0)
2558 return got_error_from_errno("unveil");
2560 return NULL;
2563 static void
2564 init_curses(void)
2566 initscr();
2567 cbreak();
2568 halfdelay(1); /* Do fast refresh while initial view is loading. */
2569 noecho();
2570 nonl();
2571 intrflush(stdscr, FALSE);
2572 keypad(stdscr, TRUE);
2573 curs_set(0);
2574 if (getenv("TOG_COLORS") != NULL) {
2575 start_color();
2576 use_default_colors();
2578 signal(SIGWINCH, tog_sigwinch);
2579 signal(SIGPIPE, tog_sigpipe);
2580 signal(SIGCONT, tog_sigcont);
2583 static const struct got_error *
2584 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2585 struct got_repository *repo, struct got_worktree *worktree)
2587 const struct got_error *err = NULL;
2589 if (argc == 0) {
2590 *in_repo_path = strdup("/");
2591 if (*in_repo_path == NULL)
2592 return got_error_from_errno("strdup");
2593 return NULL;
2596 if (worktree) {
2597 const char *prefix = got_worktree_get_path_prefix(worktree);
2598 char *wt_path, *p;
2600 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2601 if (err)
2602 return err;
2604 if (asprintf(&p, "%s%s%s", prefix,
2605 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2606 err = got_error_from_errno("asprintf");
2607 free(wt_path);
2608 return err;
2610 err = got_repo_map_path(in_repo_path, repo, p, 0);
2611 free(p);
2612 free(wt_path);
2613 } else
2614 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2616 return err;
2619 static const struct got_error *
2620 cmd_log(int argc, char *argv[])
2622 const struct got_error *error;
2623 struct got_repository *repo = NULL;
2624 struct got_worktree *worktree = NULL;
2625 struct got_reflist_head refs;
2626 struct got_object_id *start_id = NULL;
2627 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2628 char *start_commit = NULL, *head_ref_name = NULL;
2629 int ch, log_branches = 0;
2630 struct tog_view *view;
2632 SIMPLEQ_INIT(&refs);
2634 #ifndef PROFILE
2635 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2636 NULL) == -1)
2637 err(1, "pledge");
2638 #endif
2640 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2641 switch (ch) {
2642 case 'b':
2643 log_branches = 1;
2644 break;
2645 case 'c':
2646 start_commit = optarg;
2647 break;
2648 case 'r':
2649 repo_path = realpath(optarg, NULL);
2650 if (repo_path == NULL)
2651 return got_error_from_errno2("realpath",
2652 optarg);
2653 break;
2654 default:
2655 usage_log();
2656 /* NOTREACHED */
2660 argc -= optind;
2661 argv += optind;
2663 if (argc > 1)
2664 usage_log();
2666 cwd = getcwd(NULL, 0);
2667 if (cwd == NULL)
2668 return got_error_from_errno("getcwd");
2670 error = got_worktree_open(&worktree, cwd);
2671 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2672 goto done;
2674 if (repo_path == NULL) {
2675 if (worktree)
2676 repo_path =
2677 strdup(got_worktree_get_repo_path(worktree));
2678 else
2679 repo_path = strdup(cwd);
2681 if (repo_path == NULL) {
2682 error = got_error_from_errno("strdup");
2683 goto done;
2686 error = got_repo_open(&repo, repo_path, NULL);
2687 if (error != NULL)
2688 goto done;
2690 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2691 repo, worktree);
2692 if (error)
2693 goto done;
2695 init_curses();
2697 error = apply_unveil(got_repo_get_path(repo),
2698 worktree ? got_worktree_get_root_path(worktree) : NULL);
2699 if (error)
2700 goto done;
2702 if (start_commit == NULL)
2703 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2704 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2705 GOT_OBJ_TYPE_COMMIT, 1, repo);
2706 else
2707 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2708 GOT_OBJ_TYPE_COMMIT, 1, repo);
2709 if (error != NULL)
2710 goto done;
2712 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2713 if (error)
2714 goto done;
2716 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2717 if (view == NULL) {
2718 error = got_error_from_errno("view_open");
2719 goto done;
2721 if (worktree) {
2722 head_ref_name = strdup(
2723 got_worktree_get_head_ref_name(worktree));
2724 if (head_ref_name == NULL) {
2725 error = got_error_from_errno("strdup");
2726 goto done;
2729 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2730 in_repo_path, log_branches);
2731 if (error)
2732 goto done;
2733 if (worktree) {
2734 /* Release work tree lock. */
2735 got_worktree_close(worktree);
2736 worktree = NULL;
2738 error = view_loop(view);
2739 done:
2740 free(in_repo_path);
2741 free(repo_path);
2742 free(cwd);
2743 free(start_id);
2744 free(head_ref_name);
2745 if (repo)
2746 got_repo_close(repo);
2747 if (worktree)
2748 got_worktree_close(worktree);
2749 got_ref_list_free(&refs);
2750 return error;
2753 __dead static void
2754 usage_diff(void)
2756 endwin();
2757 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2758 getprogname());
2759 exit(1);
2762 static char *
2763 parse_next_line(FILE *f, size_t *len)
2765 char *line;
2766 size_t linelen;
2767 size_t lineno;
2768 const char delim[3] = { '\0', '\0', '\0'};
2770 line = fparseln(f, &linelen, &lineno, delim, 0);
2771 if (len)
2772 *len = linelen;
2773 return line;
2776 static int
2777 match_line(const char *line, regex_t *regex, size_t nmatch,
2778 regmatch_t *regmatch)
2780 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2783 struct tog_color *
2784 match_color(struct tog_colors *colors, const char *line)
2786 struct tog_color *tc = NULL;
2788 SIMPLEQ_FOREACH(tc, colors, entry) {
2789 if (match_line(line, &tc->regex, 0, NULL))
2790 return tc;
2793 return NULL;
2796 static const struct got_error *
2797 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2798 WINDOW *window, regmatch_t *regmatch)
2800 const struct got_error *err = NULL;
2801 wchar_t *wline;
2802 int width;
2803 char *s;
2805 *wtotal = 0;
2807 s = strndup(line, regmatch->rm_so);
2808 if (s == NULL)
2809 return got_error_from_errno("strndup");
2811 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2812 if (err) {
2813 free(s);
2814 return err;
2816 waddwstr(window, wline);
2817 free(wline);
2818 free(s);
2819 wlimit -= width;
2820 *wtotal += width;
2822 if (wlimit > 0) {
2823 s = strndup(line + regmatch->rm_so,
2824 regmatch->rm_eo - regmatch->rm_so);
2825 if (s == NULL) {
2826 err = got_error_from_errno("strndup");
2827 free(s);
2828 return err;
2830 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2831 if (err) {
2832 free(s);
2833 return err;
2835 wattr_on(window, A_STANDOUT, NULL);
2836 waddwstr(window, wline);
2837 wattr_off(window, A_STANDOUT, NULL);
2838 free(wline);
2839 free(s);
2840 wlimit -= width;
2841 *wtotal += width;
2844 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2845 err = format_line(&wline, &width,
2846 line + regmatch->rm_eo, wlimit, col_tab_align);
2847 if (err)
2848 return err;
2849 waddwstr(window, wline);
2850 free(wline);
2851 *wtotal += width;
2854 return NULL;
2857 static const struct got_error *
2858 draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2859 off_t *line_offsets, int selected_line, int max_lines,
2860 int *last_displayed_line, int *eof, char *header,
2861 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
2863 const struct got_error *err;
2864 int nprinted = 0;
2865 char *line;
2866 struct tog_color *tc;
2867 size_t len;
2868 wchar_t *wline;
2869 int width;
2870 off_t line_offset;
2872 line_offset = line_offsets[first_displayed_line - 1];
2873 if (fseeko(f, line_offset, SEEK_SET) == -1)
2874 return got_error_from_errno("fseek");
2876 werase(view->window);
2878 if (header) {
2879 if (asprintf(&line, "[%d/%d] %s",
2880 first_displayed_line - 1 + selected_line, nlines,
2881 header) == -1)
2882 return got_error_from_errno("asprintf");
2883 err = format_line(&wline, &width, line, view->ncols, 0);
2884 free(line);
2885 if (err)
2886 return err;
2888 if (view_needs_focus_indication(view))
2889 wstandout(view->window);
2890 waddwstr(view->window, wline);
2891 free(wline);
2892 wline = NULL;
2893 if (view_needs_focus_indication(view))
2894 wstandend(view->window);
2895 if (width <= view->ncols - 1)
2896 waddch(view->window, '\n');
2898 if (max_lines <= 1)
2899 return NULL;
2900 max_lines--;
2903 *eof = 0;
2904 while (max_lines > 0 && nprinted < max_lines) {
2905 line = parse_next_line(f, &len);
2906 if (line == NULL) {
2907 *eof = 1;
2908 break;
2911 tc = match_color(colors, line);
2912 if (tc)
2913 wattr_on(view->window,
2914 COLOR_PAIR(tc->colorpair), NULL);
2915 if (first_displayed_line + nprinted == matched_line &&
2916 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2917 err = add_matched_line(&width, line, view->ncols, 0,
2918 view->window, regmatch);
2919 if (err) {
2920 free(line);
2921 return err;
2923 } else {
2924 err = format_line(&wline, &width, line, view->ncols, 0);
2925 if (err) {
2926 free(line);
2927 return err;
2929 waddwstr(view->window, wline);
2930 free(wline);
2931 wline = NULL;
2933 if (tc)
2934 wattr_off(view->window,
2935 COLOR_PAIR(tc->colorpair), NULL);
2936 if (width <= view->ncols - 1)
2937 waddch(view->window, '\n');
2938 nprinted++;
2939 free(line);
2941 if (nprinted >= 1)
2942 *last_displayed_line = first_displayed_line + (nprinted - 1);
2943 else
2944 *last_displayed_line = first_displayed_line;
2946 view_vborder(view);
2948 if (*eof) {
2949 while (nprinted < view->nlines) {
2950 waddch(view->window, '\n');
2951 nprinted++;
2954 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2955 if (err) {
2956 return err;
2959 wstandout(view->window);
2960 waddwstr(view->window, wline);
2961 free(wline);
2962 wline = NULL;
2963 wstandend(view->window);
2966 return NULL;
2969 static char *
2970 get_datestr(time_t *time, char *datebuf)
2972 struct tm mytm, *tm;
2973 char *p, *s;
2975 tm = gmtime_r(time, &mytm);
2976 if (tm == NULL)
2977 return NULL;
2978 s = asctime_r(tm, datebuf);
2979 if (s == NULL)
2980 return NULL;
2981 p = strchr(s, '\n');
2982 if (p)
2983 *p = '\0';
2984 return s;
2987 static const struct got_error *
2988 get_changed_paths(struct got_pathlist_head *paths,
2989 struct got_commit_object *commit, struct got_repository *repo)
2991 const struct got_error *err = NULL;
2992 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2993 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2994 struct got_object_qid *qid;
2996 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2997 if (qid != NULL) {
2998 struct got_commit_object *pcommit;
2999 err = got_object_open_as_commit(&pcommit, repo,
3000 qid->id);
3001 if (err)
3002 return err;
3004 tree_id1 = got_object_commit_get_tree_id(pcommit);
3005 got_object_commit_close(pcommit);
3009 if (tree_id1) {
3010 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3011 if (err)
3012 goto done;
3015 tree_id2 = got_object_commit_get_tree_id(commit);
3016 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3017 if (err)
3018 goto done;
3020 err = got_diff_tree(tree1, tree2, "", "", repo,
3021 got_diff_tree_collect_changed_paths, paths, 0);
3022 done:
3023 if (tree1)
3024 got_object_tree_close(tree1);
3025 if (tree2)
3026 got_object_tree_close(tree2);
3027 return err;
3030 static const struct got_error *
3031 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3033 off_t *p;
3035 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3036 if (p == NULL)
3037 return got_error_from_errno("reallocarray");
3038 *line_offsets = p;
3039 (*line_offsets)[*nlines] = off;
3040 (*nlines)++;
3041 return NULL;
3044 static const struct got_error *
3045 write_commit_info(off_t **line_offsets, size_t *nlines,
3046 struct got_object_id *commit_id, struct got_reflist_head *refs,
3047 struct got_repository *repo, FILE *outfile)
3049 const struct got_error *err = NULL;
3050 char datebuf[26], *datestr;
3051 struct got_commit_object *commit;
3052 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3053 time_t committer_time;
3054 const char *author, *committer;
3055 char *refs_str = NULL;
3056 struct got_pathlist_head changed_paths;
3057 struct got_pathlist_entry *pe;
3058 off_t outoff = 0;
3059 int n;
3061 TAILQ_INIT(&changed_paths);
3063 if (refs) {
3064 err = build_refs_str(&refs_str, refs, commit_id, repo);
3065 if (err)
3066 return err;
3069 err = got_object_open_as_commit(&commit, repo, commit_id);
3070 if (err)
3071 return err;
3073 err = got_object_id_str(&id_str, commit_id);
3074 if (err) {
3075 err = got_error_from_errno("got_object_id_str");
3076 goto done;
3079 err = add_line_offset(line_offsets, nlines, 0);
3080 if (err)
3081 goto done;
3083 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3084 refs_str ? refs_str : "", refs_str ? ")" : "");
3085 if (n < 0) {
3086 err = got_error_from_errno("fprintf");
3087 goto done;
3089 outoff += n;
3090 err = add_line_offset(line_offsets, nlines, outoff);
3091 if (err)
3092 goto done;
3094 n = fprintf(outfile, "from: %s\n",
3095 got_object_commit_get_author(commit));
3096 if (n < 0) {
3097 err = got_error_from_errno("fprintf");
3098 goto done;
3100 outoff += n;
3101 err = add_line_offset(line_offsets, nlines, outoff);
3102 if (err)
3103 goto done;
3105 committer_time = got_object_commit_get_committer_time(commit);
3106 datestr = get_datestr(&committer_time, datebuf);
3107 if (datestr) {
3108 n = fprintf(outfile, "date: %s UTC\n", datestr);
3109 if (n < 0) {
3110 err = got_error_from_errno("fprintf");
3111 goto done;
3113 outoff += n;
3114 err = add_line_offset(line_offsets, nlines, outoff);
3115 if (err)
3116 goto done;
3118 author = got_object_commit_get_author(commit);
3119 committer = got_object_commit_get_committer(commit);
3120 if (strcmp(author, committer) != 0) {
3121 n = fprintf(outfile, "via: %s\n", committer);
3122 if (n < 0) {
3123 err = got_error_from_errno("fprintf");
3124 goto done;
3126 outoff += n;
3127 err = add_line_offset(line_offsets, nlines, outoff);
3128 if (err)
3129 goto done;
3131 err = got_object_commit_get_logmsg(&logmsg, commit);
3132 if (err)
3133 goto done;
3134 s = logmsg;
3135 while ((line = strsep(&s, "\n")) != NULL) {
3136 n = fprintf(outfile, "%s\n", line);
3137 if (n < 0) {
3138 err = got_error_from_errno("fprintf");
3139 goto done;
3141 outoff += n;
3142 err = add_line_offset(line_offsets, nlines, outoff);
3143 if (err)
3144 goto done;
3147 err = get_changed_paths(&changed_paths, commit, repo);
3148 if (err)
3149 goto done;
3150 TAILQ_FOREACH(pe, &changed_paths, entry) {
3151 struct got_diff_changed_path *cp = pe->data;
3152 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3153 if (n < 0) {
3154 err = got_error_from_errno("fprintf");
3155 goto done;
3157 outoff += n;
3158 err = add_line_offset(line_offsets, nlines, outoff);
3159 if (err)
3160 goto done;
3161 free((char *)pe->path);
3162 free(pe->data);
3165 fputc('\n', outfile);
3166 outoff++;
3167 err = add_line_offset(line_offsets, nlines, outoff);
3168 done:
3169 got_pathlist_free(&changed_paths);
3170 free(id_str);
3171 free(logmsg);
3172 free(refs_str);
3173 got_object_commit_close(commit);
3174 if (err) {
3175 free(*line_offsets);
3176 *line_offsets = NULL;
3177 *nlines = 0;
3179 return err;
3182 static const struct got_error *
3183 create_diff(struct tog_diff_view_state *s)
3185 const struct got_error *err = NULL;
3186 FILE *f = NULL;
3187 int obj_type;
3189 free(s->line_offsets);
3190 s->line_offsets = malloc(sizeof(off_t));
3191 if (s->line_offsets == NULL)
3192 return got_error_from_errno("malloc");
3193 s->nlines = 0;
3195 f = got_opentemp();
3196 if (f == NULL) {
3197 err = got_error_from_errno("got_opentemp");
3198 goto done;
3200 if (s->f && fclose(s->f) != 0) {
3201 err = got_error_from_errno("fclose");
3202 goto done;
3204 s->f = f;
3206 if (s->id1)
3207 err = got_object_get_type(&obj_type, s->repo, s->id1);
3208 else
3209 err = got_object_get_type(&obj_type, s->repo, s->id2);
3210 if (err)
3211 goto done;
3213 switch (obj_type) {
3214 case GOT_OBJ_TYPE_BLOB:
3215 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3216 s->id1, s->id2, NULL, NULL, s->diff_context, 0,
3217 s->repo, s->f);
3218 break;
3219 case GOT_OBJ_TYPE_TREE:
3220 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3221 s->id1, s->id2, "", "", s->diff_context, 0, s->repo, s->f);
3222 break;
3223 case GOT_OBJ_TYPE_COMMIT: {
3224 const struct got_object_id_queue *parent_ids;
3225 struct got_object_qid *pid;
3226 struct got_commit_object *commit2;
3228 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3229 if (err)
3230 goto done;
3231 /* Show commit info if we're diffing to a parent/root commit. */
3232 if (s->id1 == NULL) {
3233 err = write_commit_info(&s->line_offsets, &s->nlines,
3234 s->id2, s->refs, s->repo, s->f);
3235 if (err)
3236 goto done;
3237 } else {
3238 parent_ids = got_object_commit_get_parent_ids(commit2);
3239 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3240 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3241 err = write_commit_info(
3242 &s->line_offsets, &s->nlines,
3243 s->id2, s->refs, s->repo, s->f);
3244 if (err)
3245 goto done;
3246 break;
3250 got_object_commit_close(commit2);
3252 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3253 s->id1, s->id2, s->diff_context, 0, s->repo, s->f);
3254 break;
3256 default:
3257 err = got_error(GOT_ERR_OBJ_TYPE);
3258 break;
3260 if (err)
3261 goto done;
3262 done:
3263 if (s->f && fflush(s->f) != 0 && err == NULL)
3264 err = got_error_from_errno("fflush");
3265 return err;
3268 static void
3269 diff_view_indicate_progress(struct tog_view *view)
3271 mvwaddstr(view->window, 0, 0, "diffing...");
3272 update_panels();
3273 doupdate();
3276 static const struct got_error *
3277 search_start_diff_view(struct tog_view *view)
3279 struct tog_diff_view_state *s = &view->state.diff;
3281 s->matched_line = 0;
3282 return NULL;
3285 static const struct got_error *
3286 search_next_diff_view(struct tog_view *view)
3288 struct tog_diff_view_state *s = &view->state.diff;
3289 int lineno;
3291 if (!view->searching) {
3292 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3293 return NULL;
3296 if (s->matched_line) {
3297 if (view->searching == TOG_SEARCH_FORWARD)
3298 lineno = s->matched_line + 1;
3299 else
3300 lineno = s->matched_line - 1;
3301 } else {
3302 if (view->searching == TOG_SEARCH_FORWARD)
3303 lineno = 1;
3304 else
3305 lineno = s->nlines;
3308 while (1) {
3309 char *line = NULL;
3310 off_t offset;
3311 size_t len;
3313 if (lineno <= 0 || lineno > s->nlines) {
3314 if (s->matched_line == 0) {
3315 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3316 free(line);
3317 break;
3320 if (view->searching == TOG_SEARCH_FORWARD)
3321 lineno = 1;
3322 else
3323 lineno = s->nlines;
3326 offset = s->line_offsets[lineno - 1];
3327 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3328 free(line);
3329 return got_error_from_errno("fseeko");
3331 free(line);
3332 line = parse_next_line(s->f, &len);
3333 if (line &&
3334 match_line(line, &view->regex, 1, &view->regmatch)) {
3335 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3336 s->matched_line = lineno;
3337 free(line);
3338 break;
3340 free(line);
3341 if (view->searching == TOG_SEARCH_FORWARD)
3342 lineno++;
3343 else
3344 lineno--;
3347 if (s->matched_line) {
3348 s->first_displayed_line = s->matched_line;
3349 s->selected_line = 1;
3352 return NULL;
3355 static const struct got_error *
3356 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3357 struct got_object_id *id2, struct tog_view *log_view,
3358 struct got_reflist_head *refs, struct got_repository *repo)
3360 const struct got_error *err;
3361 struct tog_diff_view_state *s = &view->state.diff;
3363 if (id1 != NULL && id2 != NULL) {
3364 int type1, type2;
3365 err = got_object_get_type(&type1, repo, id1);
3366 if (err)
3367 return err;
3368 err = got_object_get_type(&type2, repo, id2);
3369 if (err)
3370 return err;
3372 if (type1 != type2)
3373 return got_error(GOT_ERR_OBJ_TYPE);
3375 s->first_displayed_line = 1;
3376 s->last_displayed_line = view->nlines;
3377 s->selected_line = 1;
3378 s->repo = repo;
3379 s->refs = refs;
3380 s->id1 = id1;
3381 s->id2 = id2;
3383 if (id1) {
3384 s->id1 = got_object_id_dup(id1);
3385 if (s->id1 == NULL)
3386 return got_error_from_errno("got_object_id_dup");
3387 } else
3388 s->id1 = NULL;
3390 s->id2 = got_object_id_dup(id2);
3391 if (s->id2 == NULL) {
3392 free(s->id1);
3393 s->id1 = NULL;
3394 return got_error_from_errno("got_object_id_dup");
3396 s->f = NULL;
3397 s->first_displayed_line = 1;
3398 s->last_displayed_line = view->nlines;
3399 s->diff_context = 3;
3400 s->log_view = log_view;
3401 s->repo = repo;
3402 s->refs = refs;
3404 SIMPLEQ_INIT(&s->colors);
3405 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3406 err = add_color(&s->colors,
3407 "^-", TOG_COLOR_DIFF_MINUS,
3408 get_color_value("TOG_COLOR_DIFF_MINUS"));
3409 if (err)
3410 return err;
3411 err = add_color(&s->colors, "^\\+",
3412 TOG_COLOR_DIFF_PLUS,
3413 get_color_value("TOG_COLOR_DIFF_PLUS"));
3414 if (err) {
3415 free_colors(&s->colors);
3416 return err;
3418 err = add_color(&s->colors,
3419 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3420 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3421 if (err) {
3422 free_colors(&s->colors);
3423 return err;
3426 err = add_color(&s->colors,
3427 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3428 TOG_COLOR_DIFF_META,
3429 get_color_value("TOG_COLOR_DIFF_META"));
3430 if (err) {
3431 free_colors(&s->colors);
3432 return err;
3435 err = add_color(&s->colors,
3436 "^(from|via): ", TOG_COLOR_AUTHOR,
3437 get_color_value("TOG_COLOR_AUTHOR"));
3438 if (err) {
3439 free_colors(&s->colors);
3440 return err;
3443 err = add_color(&s->colors,
3444 "^date: ", TOG_COLOR_DATE,
3445 get_color_value("TOG_COLOR_DATE"));
3446 if (err) {
3447 free_colors(&s->colors);
3448 return err;
3452 if (log_view && view_is_splitscreen(view))
3453 show_log_view(log_view); /* draw vborder */
3454 diff_view_indicate_progress(view);
3456 s->line_offsets = NULL;
3457 s->nlines = 0;
3458 err = create_diff(s);
3459 if (err) {
3460 free(s->id1);
3461 s->id1 = NULL;
3462 free(s->id2);
3463 s->id2 = NULL;
3464 return err;
3467 view->show = show_diff_view;
3468 view->input = input_diff_view;
3469 view->close = close_diff_view;
3470 view->search_start = search_start_diff_view;
3471 view->search_next = search_next_diff_view;
3473 return NULL;
3476 static const struct got_error *
3477 close_diff_view(struct tog_view *view)
3479 const struct got_error *err = NULL;
3480 struct tog_diff_view_state *s = &view->state.diff;
3482 free(s->id1);
3483 s->id1 = NULL;
3484 free(s->id2);
3485 s->id2 = NULL;
3486 if (s->f && fclose(s->f) == EOF)
3487 err = got_error_from_errno("fclose");
3488 free_colors(&s->colors);
3489 free(s->line_offsets);
3490 s->line_offsets = NULL;
3491 s->nlines = 0;
3492 return err;
3495 static const struct got_error *
3496 show_diff_view(struct tog_view *view)
3498 const struct got_error *err;
3499 struct tog_diff_view_state *s = &view->state.diff;
3500 char *id_str1 = NULL, *id_str2, *header;
3502 if (s->id1) {
3503 err = got_object_id_str(&id_str1, s->id1);
3504 if (err)
3505 return err;
3507 err = got_object_id_str(&id_str2, s->id2);
3508 if (err)
3509 return err;
3511 if (asprintf(&header, "diff %s %s",
3512 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3513 err = got_error_from_errno("asprintf");
3514 free(id_str1);
3515 free(id_str2);
3516 return err;
3518 free(id_str1);
3519 free(id_str2);
3521 return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3522 s->line_offsets, s->selected_line, view->nlines,
3523 &s->last_displayed_line, &s->eof, header, &s->colors,
3524 s->matched_line, &view->regmatch);
3527 static const struct got_error *
3528 set_selected_commit(struct tog_diff_view_state *s,
3529 struct commit_queue_entry *entry)
3531 const struct got_error *err;
3532 const struct got_object_id_queue *parent_ids;
3533 struct got_commit_object *selected_commit;
3534 struct got_object_qid *pid;
3536 free(s->id2);
3537 s->id2 = got_object_id_dup(entry->id);
3538 if (s->id2 == NULL)
3539 return got_error_from_errno("got_object_id_dup");
3541 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3542 if (err)
3543 return err;
3544 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3545 free(s->id1);
3546 pid = SIMPLEQ_FIRST(parent_ids);
3547 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3548 got_object_commit_close(selected_commit);
3549 return NULL;
3552 static const struct got_error *
3553 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3554 struct tog_view **focus_view, struct tog_view *view, int ch)
3556 const struct got_error *err = NULL;
3557 struct tog_diff_view_state *s = &view->state.diff;
3558 struct tog_log_view_state *ls;
3559 struct commit_queue_entry *entry;
3560 int i;
3562 switch (ch) {
3563 case 'k':
3564 case KEY_UP:
3565 if (s->first_displayed_line > 1)
3566 s->first_displayed_line--;
3567 break;
3568 case KEY_PPAGE:
3569 case CTRL('b'):
3570 if (s->first_displayed_line == 1)
3571 break;
3572 i = 0;
3573 while (i++ < view->nlines - 1 &&
3574 s->first_displayed_line > 1)
3575 s->first_displayed_line--;
3576 break;
3577 case 'j':
3578 case KEY_DOWN:
3579 if (!s->eof)
3580 s->first_displayed_line++;
3581 break;
3582 case KEY_NPAGE:
3583 case CTRL('f'):
3584 case ' ':
3585 if (s->eof)
3586 break;
3587 i = 0;
3588 while (!s->eof && i++ < view->nlines - 1) {
3589 char *line;
3590 line = parse_next_line(s->f, NULL);
3591 s->first_displayed_line++;
3592 if (line == NULL)
3593 break;
3595 break;
3596 case '[':
3597 if (s->diff_context > 0) {
3598 s->diff_context--;
3599 diff_view_indicate_progress(view);
3600 err = create_diff(s);
3602 break;
3603 case ']':
3604 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3605 s->diff_context++;
3606 diff_view_indicate_progress(view);
3607 err = create_diff(s);
3609 break;
3610 case '<':
3611 case ',':
3612 if (s->log_view == NULL)
3613 break;
3614 ls = &s->log_view->state.log;
3615 entry = TAILQ_PREV(ls->selected_entry,
3616 commit_queue_head, entry);
3617 if (entry == NULL)
3618 break;
3620 err = input_log_view(NULL, NULL, NULL, s->log_view,
3621 KEY_UP);
3622 if (err)
3623 break;
3625 err = set_selected_commit(s, entry);
3626 if (err)
3627 break;
3629 s->first_displayed_line = 1;
3630 s->last_displayed_line = view->nlines;
3632 diff_view_indicate_progress(view);
3633 err = create_diff(s);
3634 break;
3635 case '>':
3636 case '.':
3637 if (s->log_view == NULL)
3638 break;
3639 ls = &s->log_view->state.log;
3641 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3642 ls->thread_args.commits_needed++;
3643 err = trigger_log_thread(s->log_view, 1,
3644 &ls->thread_args.commits_needed,
3645 &ls->thread_args.log_complete,
3646 &ls->thread_args.need_commits,
3647 &ls->thread_args.commit_loaded);
3648 if (err)
3649 break;
3651 err = input_log_view(NULL, NULL, NULL, s->log_view,
3652 KEY_DOWN);
3653 if (err)
3654 break;
3656 entry = TAILQ_NEXT(ls->selected_entry, entry);
3657 if (entry == NULL)
3658 break;
3660 err = set_selected_commit(s, entry);
3661 if (err)
3662 break;
3664 s->first_displayed_line = 1;
3665 s->last_displayed_line = view->nlines;
3667 diff_view_indicate_progress(view);
3668 err = create_diff(s);
3669 break;
3670 default:
3671 break;
3674 return err;
3677 static const struct got_error *
3678 cmd_diff(int argc, char *argv[])
3680 const struct got_error *error = NULL;
3681 struct got_repository *repo = NULL;
3682 struct got_worktree *worktree = NULL;
3683 struct got_reflist_head refs;
3684 struct got_object_id *id1 = NULL, *id2 = NULL;
3685 char *repo_path = NULL, *cwd = NULL;
3686 char *id_str1 = NULL, *id_str2 = NULL;
3687 int ch;
3688 struct tog_view *view;
3690 SIMPLEQ_INIT(&refs);
3692 #ifndef PROFILE
3693 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3694 NULL) == -1)
3695 err(1, "pledge");
3696 #endif
3698 while ((ch = getopt(argc, argv, "r:")) != -1) {
3699 switch (ch) {
3700 case 'r':
3701 repo_path = realpath(optarg, NULL);
3702 if (repo_path == NULL)
3703 return got_error_from_errno2("realpath",
3704 optarg);
3705 break;
3706 default:
3707 usage_diff();
3708 /* NOTREACHED */
3712 argc -= optind;
3713 argv += optind;
3715 if (argc == 0) {
3716 usage_diff(); /* TODO show local worktree changes */
3717 } else if (argc == 2) {
3718 id_str1 = argv[0];
3719 id_str2 = argv[1];
3720 } else
3721 usage_diff();
3723 cwd = getcwd(NULL, 0);
3724 if (cwd == NULL)
3725 return got_error_from_errno("getcwd");
3727 error = got_worktree_open(&worktree, cwd);
3728 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3729 goto done;
3731 if (repo_path == NULL) {
3732 if (worktree)
3733 repo_path =
3734 strdup(got_worktree_get_repo_path(worktree));
3735 else
3736 repo_path = strdup(cwd);
3738 if (repo_path == NULL) {
3739 error = got_error_from_errno("strdup");
3740 goto done;
3743 error = got_repo_open(&repo, repo_path, NULL);
3744 if (error)
3745 goto done;
3747 init_curses();
3749 error = apply_unveil(got_repo_get_path(repo), NULL);
3750 if (error)
3751 goto done;
3753 error = got_repo_match_object_id_prefix(&id1, id_str1,
3754 GOT_OBJ_TYPE_ANY, repo);
3755 if (error)
3756 goto done;
3758 error = got_repo_match_object_id_prefix(&id2, id_str2,
3759 GOT_OBJ_TYPE_ANY, repo);
3760 if (error)
3761 goto done;
3763 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3764 if (error)
3765 goto done;
3767 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3768 if (view == NULL) {
3769 error = got_error_from_errno("view_open");
3770 goto done;
3772 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3773 if (error)
3774 goto done;
3775 error = view_loop(view);
3776 done:
3777 free(repo_path);
3778 free(cwd);
3779 if (repo)
3780 got_repo_close(repo);
3781 if (worktree)
3782 got_worktree_close(worktree);
3783 got_ref_list_free(&refs);
3784 return error;
3787 __dead static void
3788 usage_blame(void)
3790 endwin();
3791 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3792 getprogname());
3793 exit(1);
3796 struct tog_blame_line {
3797 int annotated;
3798 struct got_object_id *id;
3801 static const struct got_error *
3802 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3803 const char *path, struct tog_blame_line *lines, int nlines,
3804 int blame_complete, int selected_line, int *first_displayed_line,
3805 int *last_displayed_line, int *eof, int max_lines,
3806 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
3808 const struct got_error *err;
3809 int lineno = 0, nprinted = 0;
3810 char *line;
3811 size_t len;
3812 wchar_t *wline;
3813 int width;
3814 struct tog_blame_line *blame_line;
3815 struct got_object_id *prev_id = NULL;
3816 char *id_str;
3817 struct tog_color *tc;
3819 err = got_object_id_str(&id_str, id);
3820 if (err)
3821 return err;
3823 rewind(f);
3824 werase(view->window);
3826 if (asprintf(&line, "commit %s", id_str) == -1) {
3827 err = got_error_from_errno("asprintf");
3828 free(id_str);
3829 return err;
3832 err = format_line(&wline, &width, line, view->ncols, 0);
3833 free(line);
3834 line = NULL;
3835 if (err)
3836 return err;
3837 if (view_needs_focus_indication(view))
3838 wstandout(view->window);
3839 tc = get_color(colors, TOG_COLOR_COMMIT);
3840 if (tc)
3841 wattr_on(view->window,
3842 COLOR_PAIR(tc->colorpair), NULL);
3843 waddwstr(view->window, wline);
3844 if (tc)
3845 wattr_off(view->window,
3846 COLOR_PAIR(tc->colorpair), NULL);
3847 if (view_needs_focus_indication(view))
3848 wstandend(view->window);
3849 free(wline);
3850 wline = NULL;
3851 if (width < view->ncols - 1)
3852 waddch(view->window, '\n');
3854 if (asprintf(&line, "[%d/%d] %s%s",
3855 *first_displayed_line - 1 + selected_line, nlines,
3856 blame_complete ? "" : "annotating... ", path) == -1) {
3857 free(id_str);
3858 return got_error_from_errno("asprintf");
3860 free(id_str);
3861 err = format_line(&wline, &width, line, view->ncols, 0);
3862 free(line);
3863 line = NULL;
3864 if (err)
3865 return err;
3866 waddwstr(view->window, wline);
3867 free(wline);
3868 wline = NULL;
3869 if (width < view->ncols - 1)
3870 waddch(view->window, '\n');
3872 *eof = 0;
3873 while (nprinted < max_lines - 2) {
3874 line = parse_next_line(f, &len);
3875 if (line == NULL) {
3876 *eof = 1;
3877 break;
3879 if (++lineno < *first_displayed_line) {
3880 free(line);
3881 continue;
3884 if (view->focussed && nprinted == selected_line - 1)
3885 wstandout(view->window);
3887 if (nlines > 0) {
3888 blame_line = &lines[lineno - 1];
3889 if (blame_line->annotated && prev_id &&
3890 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3891 !(view->focussed &&
3892 nprinted == selected_line - 1)) {
3893 waddstr(view->window, " ");
3894 } else if (blame_line->annotated) {
3895 char *id_str;
3896 err = got_object_id_str(&id_str, blame_line->id);
3897 if (err) {
3898 free(line);
3899 return err;
3901 tc = get_color(colors, TOG_COLOR_COMMIT);
3902 if (tc)
3903 wattr_on(view->window,
3904 COLOR_PAIR(tc->colorpair), NULL);
3905 wprintw(view->window, "%.8s", id_str);
3906 if (tc)
3907 wattr_off(view->window,
3908 COLOR_PAIR(tc->colorpair), NULL);
3909 free(id_str);
3910 prev_id = blame_line->id;
3911 } else {
3912 waddstr(view->window, "........");
3913 prev_id = NULL;
3915 } else {
3916 waddstr(view->window, "........");
3917 prev_id = NULL;
3920 if (view->focussed && nprinted == selected_line - 1)
3921 wstandend(view->window);
3922 waddstr(view->window, " ");
3924 if (view->ncols <= 9) {
3925 width = 9;
3926 wline = wcsdup(L"");
3927 if (wline == NULL) {
3928 err = got_error_from_errno("wcsdup");
3929 free(line);
3930 return err;
3932 } else if (*first_displayed_line + nprinted == matched_line &&
3933 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3934 err = add_matched_line(&width, line, view->ncols - 9, 9,
3935 view->window, regmatch);
3936 if (err) {
3937 free(line);
3938 return err;
3940 width += 9;
3941 } else {
3942 err = format_line(&wline, &width, line,
3943 view->ncols - 9, 9);
3944 waddwstr(view->window, wline);
3945 free(wline);
3946 wline = NULL;
3947 width += 9;
3950 if (width <= view->ncols - 1)
3951 waddch(view->window, '\n');
3952 if (++nprinted == 1)
3953 *first_displayed_line = lineno;
3954 free(line);
3956 *last_displayed_line = lineno;
3958 view_vborder(view);
3960 return NULL;
3963 static const struct got_error *
3964 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3966 const struct got_error *err = NULL;
3967 struct tog_blame_cb_args *a = arg;
3968 struct tog_blame_line *line;
3969 int errcode;
3971 if (nlines != a->nlines ||
3972 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3973 return got_error(GOT_ERR_RANGE);
3975 errcode = pthread_mutex_lock(&tog_mutex);
3976 if (errcode)
3977 return got_error_set_errno(errcode, "pthread_mutex_lock");
3979 if (*a->quit) { /* user has quit the blame view */
3980 err = got_error(GOT_ERR_ITER_COMPLETED);
3981 goto done;
3984 if (lineno == -1)
3985 goto done; /* no change in this commit */
3987 line = &a->lines[lineno - 1];
3988 if (line->annotated)
3989 goto done;
3991 line->id = got_object_id_dup(id);
3992 if (line->id == NULL) {
3993 err = got_error_from_errno("got_object_id_dup");
3994 goto done;
3996 line->annotated = 1;
3997 done:
3998 errcode = pthread_mutex_unlock(&tog_mutex);
3999 if (errcode)
4000 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4001 return err;
4004 static void *
4005 blame_thread(void *arg)
4007 const struct got_error *err;
4008 struct tog_blame_thread_args *ta = arg;
4009 struct tog_blame_cb_args *a = ta->cb_args;
4010 int errcode;
4012 err = block_signals_used_by_main_thread();
4013 if (err)
4014 return (void *)err;
4016 err = got_blame(ta->path, a->commit_id, ta->repo,
4017 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4018 if (err && err->code == GOT_ERR_CANCELLED)
4019 err = NULL;
4021 errcode = pthread_mutex_lock(&tog_mutex);
4022 if (errcode)
4023 return (void *)got_error_set_errno(errcode,
4024 "pthread_mutex_lock");
4026 got_repo_close(ta->repo);
4027 ta->repo = NULL;
4028 *ta->complete = 1;
4030 errcode = pthread_mutex_unlock(&tog_mutex);
4031 if (errcode && err == NULL)
4032 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4034 return (void *)err;
4037 static struct got_object_id *
4038 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4039 int first_displayed_line, int selected_line)
4041 struct tog_blame_line *line;
4043 if (nlines <= 0)
4044 return NULL;
4046 line = &lines[first_displayed_line - 1 + selected_line - 1];
4047 if (!line->annotated)
4048 return NULL;
4050 return line->id;
4053 static const struct got_error *
4054 stop_blame(struct tog_blame *blame)
4056 const struct got_error *err = NULL;
4057 int i;
4059 if (blame->thread) {
4060 int errcode;
4061 errcode = pthread_mutex_unlock(&tog_mutex);
4062 if (errcode)
4063 return got_error_set_errno(errcode,
4064 "pthread_mutex_unlock");
4065 errcode = pthread_join(blame->thread, (void **)&err);
4066 if (errcode)
4067 return got_error_set_errno(errcode, "pthread_join");
4068 errcode = pthread_mutex_lock(&tog_mutex);
4069 if (errcode)
4070 return got_error_set_errno(errcode,
4071 "pthread_mutex_lock");
4072 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4073 err = NULL;
4074 blame->thread = NULL;
4076 if (blame->thread_args.repo) {
4077 got_repo_close(blame->thread_args.repo);
4078 blame->thread_args.repo = NULL;
4080 if (blame->f) {
4081 if (fclose(blame->f) != 0 && err == NULL)
4082 err = got_error_from_errno("fclose");
4083 blame->f = NULL;
4085 if (blame->lines) {
4086 for (i = 0; i < blame->nlines; i++)
4087 free(blame->lines[i].id);
4088 free(blame->lines);
4089 blame->lines = NULL;
4091 free(blame->cb_args.commit_id);
4092 blame->cb_args.commit_id = NULL;
4094 return err;
4097 static const struct got_error *
4098 cancel_blame_view(void *arg)
4100 const struct got_error *err = NULL;
4101 int *done = arg;
4102 int errcode;
4104 errcode = pthread_mutex_lock(&tog_mutex);
4105 if (errcode)
4106 return got_error_set_errno(errcode,
4107 "pthread_mutex_unlock");
4109 if (*done)
4110 err = got_error(GOT_ERR_CANCELLED);
4112 errcode = pthread_mutex_unlock(&tog_mutex);
4113 if (errcode)
4114 return got_error_set_errno(errcode,
4115 "pthread_mutex_lock");
4117 return err;
4120 static const struct got_error *
4121 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
4122 int *first_displayed_line, int *last_displayed_line, int *selected_line,
4123 int *done, int *eof, const char *path, struct got_object_id *commit_id,
4124 struct got_repository *repo)
4126 const struct got_error *err = NULL;
4127 struct got_blob_object *blob = NULL;
4128 struct got_repository *thread_repo = NULL;
4129 struct got_object_id *obj_id = NULL;
4130 int obj_type;
4132 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
4133 if (err)
4134 return err;
4136 err = got_object_get_type(&obj_type, repo, obj_id);
4137 if (err)
4138 goto done;
4140 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4141 err = got_error(GOT_ERR_OBJ_TYPE);
4142 goto done;
4145 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4146 if (err)
4147 goto done;
4148 blame->f = got_opentemp();
4149 if (blame->f == NULL) {
4150 err = got_error_from_errno("got_opentemp");
4151 goto done;
4153 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4154 &blame->line_offsets, blame->f, blob);
4155 if (err || blame->nlines == 0)
4156 goto done;
4158 /* Don't include \n at EOF in the blame line count. */
4159 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4160 blame->nlines--;
4162 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4163 if (blame->lines == NULL) {
4164 err = got_error_from_errno("calloc");
4165 goto done;
4168 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4169 if (err)
4170 goto done;
4172 blame->cb_args.view = view;
4173 blame->cb_args.lines = blame->lines;
4174 blame->cb_args.nlines = blame->nlines;
4175 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4176 if (blame->cb_args.commit_id == NULL) {
4177 err = got_error_from_errno("got_object_id_dup");
4178 goto done;
4180 blame->cb_args.quit = done;
4182 blame->thread_args.path = path;
4183 blame->thread_args.repo = thread_repo;
4184 blame->thread_args.cb_args = &blame->cb_args;
4185 blame->thread_args.complete = blame_complete;
4186 blame->thread_args.cancel_cb = cancel_blame_view;
4187 blame->thread_args.cancel_arg = done;
4188 *blame_complete = 0;
4190 done:
4191 if (blob)
4192 got_object_blob_close(blob);
4193 free(obj_id);
4194 if (err)
4195 stop_blame(blame);
4196 return err;
4199 static const struct got_error *
4200 open_blame_view(struct tog_view *view, char *path,
4201 struct got_object_id *commit_id, struct got_reflist_head *refs,
4202 struct got_repository *repo)
4204 const struct got_error *err = NULL;
4205 struct tog_blame_view_state *s = &view->state.blame;
4207 SIMPLEQ_INIT(&s->blamed_commits);
4209 s->path = strdup(path);
4210 if (s->path == NULL)
4211 return got_error_from_errno("strdup");
4213 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4214 if (err) {
4215 free(s->path);
4216 return err;
4219 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4220 s->first_displayed_line = 1;
4221 s->last_displayed_line = view->nlines;
4222 s->selected_line = 1;
4223 s->blame_complete = 0;
4224 s->repo = repo;
4225 s->refs = refs;
4226 s->commit_id = commit_id;
4227 memset(&s->blame, 0, sizeof(s->blame));
4229 SIMPLEQ_INIT(&s->colors);
4230 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4231 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4232 get_color_value("TOG_COLOR_COMMIT"));
4233 if (err)
4234 return err;
4237 view->show = show_blame_view;
4238 view->input = input_blame_view;
4239 view->close = close_blame_view;
4240 view->search_start = search_start_blame_view;
4241 view->search_next = search_next_blame_view;
4243 return run_blame(&s->blame, view, &s->blame_complete,
4244 &s->first_displayed_line, &s->last_displayed_line,
4245 &s->selected_line, &s->done, &s->eof, s->path,
4246 s->blamed_commit->id, s->repo);
4249 static const struct got_error *
4250 close_blame_view(struct tog_view *view)
4252 const struct got_error *err = NULL;
4253 struct tog_blame_view_state *s = &view->state.blame;
4255 if (s->blame.thread)
4256 err = stop_blame(&s->blame);
4258 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4259 struct got_object_qid *blamed_commit;
4260 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4261 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4262 got_object_qid_free(blamed_commit);
4265 free(s->path);
4266 free_colors(&s->colors);
4268 return err;
4271 static const struct got_error *
4272 search_start_blame_view(struct tog_view *view)
4274 struct tog_blame_view_state *s = &view->state.blame;
4276 s->matched_line = 0;
4277 return NULL;
4280 static const struct got_error *
4281 search_next_blame_view(struct tog_view *view)
4283 struct tog_blame_view_state *s = &view->state.blame;
4284 int lineno;
4286 if (!view->searching) {
4287 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4288 return NULL;
4291 if (s->matched_line) {
4292 if (view->searching == TOG_SEARCH_FORWARD)
4293 lineno = s->matched_line + 1;
4294 else
4295 lineno = s->matched_line - 1;
4296 } else {
4297 if (view->searching == TOG_SEARCH_FORWARD)
4298 lineno = 1;
4299 else
4300 lineno = s->blame.nlines;
4303 while (1) {
4304 char *line = NULL;
4305 off_t offset;
4306 size_t len;
4308 if (lineno <= 0 || lineno > s->blame.nlines) {
4309 if (s->matched_line == 0) {
4310 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4311 free(line);
4312 break;
4315 if (view->searching == TOG_SEARCH_FORWARD)
4316 lineno = 1;
4317 else
4318 lineno = s->blame.nlines;
4321 offset = s->blame.line_offsets[lineno - 1];
4322 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4323 free(line);
4324 return got_error_from_errno("fseeko");
4326 free(line);
4327 line = parse_next_line(s->blame.f, &len);
4328 if (line &&
4329 match_line(line, &view->regex, 1, &view->regmatch)) {
4330 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4331 s->matched_line = lineno;
4332 free(line);
4333 break;
4335 free(line);
4336 if (view->searching == TOG_SEARCH_FORWARD)
4337 lineno++;
4338 else
4339 lineno--;
4342 if (s->matched_line) {
4343 s->first_displayed_line = s->matched_line;
4344 s->selected_line = 1;
4347 return NULL;
4350 static const struct got_error *
4351 show_blame_view(struct tog_view *view)
4353 const struct got_error *err = NULL;
4354 struct tog_blame_view_state *s = &view->state.blame;
4355 int errcode;
4357 if (s->blame.thread == NULL) {
4358 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4359 &s->blame.thread_args);
4360 if (errcode)
4361 return got_error_set_errno(errcode, "pthread_create");
4363 halfdelay(1); /* fast refresh while annotating */
4366 if (s->blame_complete)
4367 halfdelay(10); /* disable fast refresh */
4369 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4370 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4371 s->selected_line, &s->first_displayed_line,
4372 &s->last_displayed_line, &s->eof, view->nlines, &s->colors,
4373 s->matched_line, &view->regmatch);
4375 view_vborder(view);
4376 return err;
4379 static const struct got_error *
4380 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4381 struct tog_view **focus_view, struct tog_view *view, int ch)
4383 const struct got_error *err = NULL, *thread_err = NULL;
4384 struct tog_view *diff_view;
4385 struct tog_blame_view_state *s = &view->state.blame;
4386 int begin_x = 0;
4388 switch (ch) {
4389 case 'q':
4390 s->done = 1;
4391 break;
4392 case 'k':
4393 case KEY_UP:
4394 if (s->selected_line > 1)
4395 s->selected_line--;
4396 else if (s->selected_line == 1 &&
4397 s->first_displayed_line > 1)
4398 s->first_displayed_line--;
4399 break;
4400 case KEY_PPAGE:
4401 case CTRL('b'):
4402 if (s->first_displayed_line == 1) {
4403 s->selected_line = 1;
4404 break;
4406 if (s->first_displayed_line > view->nlines - 2)
4407 s->first_displayed_line -=
4408 (view->nlines - 2);
4409 else
4410 s->first_displayed_line = 1;
4411 break;
4412 case 'j':
4413 case KEY_DOWN:
4414 if (s->selected_line < view->nlines - 2 &&
4415 s->first_displayed_line +
4416 s->selected_line <= s->blame.nlines)
4417 s->selected_line++;
4418 else if (s->last_displayed_line <
4419 s->blame.nlines)
4420 s->first_displayed_line++;
4421 break;
4422 case 'b':
4423 case 'p': {
4424 struct got_object_id *id = NULL;
4425 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4426 s->first_displayed_line, s->selected_line);
4427 if (id == NULL)
4428 break;
4429 if (ch == 'p') {
4430 struct got_commit_object *commit;
4431 struct got_object_qid *pid;
4432 struct got_object_id *blob_id = NULL;
4433 int obj_type;
4434 err = got_object_open_as_commit(&commit,
4435 s->repo, id);
4436 if (err)
4437 break;
4438 pid = SIMPLEQ_FIRST(
4439 got_object_commit_get_parent_ids(commit));
4440 if (pid == NULL) {
4441 got_object_commit_close(commit);
4442 break;
4444 /* Check if path history ends here. */
4445 err = got_object_id_by_path(&blob_id, s->repo,
4446 pid->id, s->path);
4447 if (err) {
4448 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4449 err = NULL;
4450 got_object_commit_close(commit);
4451 break;
4453 err = got_object_get_type(&obj_type, s->repo,
4454 blob_id);
4455 free(blob_id);
4456 /* Can't blame non-blob type objects. */
4457 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4458 got_object_commit_close(commit);
4459 break;
4461 err = got_object_qid_alloc(&s->blamed_commit,
4462 pid->id);
4463 got_object_commit_close(commit);
4464 } else {
4465 if (got_object_id_cmp(id,
4466 s->blamed_commit->id) == 0)
4467 break;
4468 err = got_object_qid_alloc(&s->blamed_commit,
4469 id);
4471 if (err)
4472 break;
4473 s->done = 1;
4474 thread_err = stop_blame(&s->blame);
4475 s->done = 0;
4476 if (thread_err)
4477 break;
4478 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4479 s->blamed_commit, entry);
4480 err = run_blame(&s->blame, view, &s->blame_complete,
4481 &s->first_displayed_line, &s->last_displayed_line,
4482 &s->selected_line, &s->done, &s->eof,
4483 s->path, s->blamed_commit->id, s->repo);
4484 if (err)
4485 break;
4486 break;
4488 case 'B': {
4489 struct got_object_qid *first;
4490 first = SIMPLEQ_FIRST(&s->blamed_commits);
4491 if (!got_object_id_cmp(first->id, s->commit_id))
4492 break;
4493 s->done = 1;
4494 thread_err = stop_blame(&s->blame);
4495 s->done = 0;
4496 if (thread_err)
4497 break;
4498 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4499 got_object_qid_free(s->blamed_commit);
4500 s->blamed_commit =
4501 SIMPLEQ_FIRST(&s->blamed_commits);
4502 err = run_blame(&s->blame, view, &s->blame_complete,
4503 &s->first_displayed_line, &s->last_displayed_line,
4504 &s->selected_line, &s->done, &s->eof, s->path,
4505 s->blamed_commit->id, s->repo);
4506 if (err)
4507 break;
4508 break;
4510 case KEY_ENTER:
4511 case '\r': {
4512 struct got_object_id *id = NULL;
4513 struct got_object_qid *pid;
4514 struct got_commit_object *commit = NULL;
4515 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4516 s->first_displayed_line, s->selected_line);
4517 if (id == NULL)
4518 break;
4519 err = got_object_open_as_commit(&commit, s->repo, id);
4520 if (err)
4521 break;
4522 pid = SIMPLEQ_FIRST(
4523 got_object_commit_get_parent_ids(commit));
4524 if (view_is_parent_view(view))
4525 begin_x = view_split_begin_x(view->begin_x);
4526 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4527 if (diff_view == NULL) {
4528 got_object_commit_close(commit);
4529 err = got_error_from_errno("view_open");
4530 break;
4532 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4533 id, NULL, s->refs, s->repo);
4534 got_object_commit_close(commit);
4535 if (err) {
4536 view_close(diff_view);
4537 break;
4539 if (view_is_parent_view(view)) {
4540 err = view_close_child(view);
4541 if (err)
4542 break;
4543 err = view_set_child(view, diff_view);
4544 if (err) {
4545 view_close(diff_view);
4546 break;
4548 *focus_view = diff_view;
4549 view->child_focussed = 1;
4550 } else
4551 *new_view = diff_view;
4552 if (err)
4553 break;
4554 break;
4556 case KEY_NPAGE:
4557 case CTRL('f'):
4558 case ' ':
4559 if (s->last_displayed_line >= s->blame.nlines &&
4560 s->selected_line >= MIN(s->blame.nlines,
4561 view->nlines - 2)) {
4562 break;
4564 if (s->last_displayed_line >= s->blame.nlines &&
4565 s->selected_line < view->nlines - 2) {
4566 s->selected_line = MIN(s->blame.nlines,
4567 view->nlines - 2);
4568 break;
4570 if (s->last_displayed_line + view->nlines - 2
4571 <= s->blame.nlines)
4572 s->first_displayed_line +=
4573 view->nlines - 2;
4574 else
4575 s->first_displayed_line =
4576 s->blame.nlines -
4577 (view->nlines - 3);
4578 break;
4579 case KEY_RESIZE:
4580 if (s->selected_line > view->nlines - 2) {
4581 s->selected_line = MIN(s->blame.nlines,
4582 view->nlines - 2);
4584 break;
4585 default:
4586 break;
4588 return thread_err ? thread_err : err;
4591 static const struct got_error *
4592 cmd_blame(int argc, char *argv[])
4594 const struct got_error *error;
4595 struct got_repository *repo = NULL;
4596 struct got_reflist_head refs;
4597 struct got_worktree *worktree = NULL;
4598 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4599 char *link_target = NULL;
4600 struct got_object_id *commit_id = NULL;
4601 char *commit_id_str = NULL;
4602 int ch;
4603 struct tog_view *view;
4605 SIMPLEQ_INIT(&refs);
4607 #ifndef PROFILE
4608 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4609 NULL) == -1)
4610 err(1, "pledge");
4611 #endif
4613 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4614 switch (ch) {
4615 case 'c':
4616 commit_id_str = optarg;
4617 break;
4618 case 'r':
4619 repo_path = realpath(optarg, NULL);
4620 if (repo_path == NULL)
4621 return got_error_from_errno2("realpath",
4622 optarg);
4623 break;
4624 default:
4625 usage_blame();
4626 /* NOTREACHED */
4630 argc -= optind;
4631 argv += optind;
4633 if (argc != 1)
4634 usage_blame();
4636 cwd = getcwd(NULL, 0);
4637 if (cwd == NULL)
4638 return got_error_from_errno("getcwd");
4640 error = got_worktree_open(&worktree, cwd);
4641 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4642 goto done;
4644 if (repo_path == NULL) {
4645 if (worktree)
4646 repo_path =
4647 strdup(got_worktree_get_repo_path(worktree));
4648 else
4649 repo_path = strdup(cwd);
4651 if (repo_path == NULL) {
4652 error = got_error_from_errno("strdup");
4653 goto done;
4656 error = got_repo_open(&repo, repo_path, NULL);
4657 if (error != NULL)
4658 goto done;
4660 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4661 worktree);
4662 if (error)
4663 goto done;
4665 init_curses();
4667 error = apply_unveil(got_repo_get_path(repo), NULL);
4668 if (error)
4669 goto done;
4671 if (commit_id_str == NULL) {
4672 struct got_reference *head_ref;
4673 error = got_ref_open(&head_ref, repo, worktree ?
4674 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4675 if (error != NULL)
4676 goto done;
4677 error = got_ref_resolve(&commit_id, repo, head_ref);
4678 got_ref_close(head_ref);
4679 } else {
4680 error = got_repo_match_object_id(&commit_id, NULL,
4681 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4683 if (error != NULL)
4684 goto done;
4686 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4687 if (error)
4688 goto done;
4690 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4691 if (view == NULL) {
4692 error = got_error_from_errno("view_open");
4693 goto done;
4696 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4697 commit_id, repo);
4698 if (error)
4699 goto done;
4701 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4702 commit_id, &refs, repo);
4703 if (error)
4704 goto done;
4705 if (worktree) {
4706 /* Release work tree lock. */
4707 got_worktree_close(worktree);
4708 worktree = NULL;
4710 error = view_loop(view);
4711 done:
4712 free(repo_path);
4713 free(in_repo_path);
4714 free(link_target);
4715 free(cwd);
4716 free(commit_id);
4717 if (worktree)
4718 got_worktree_close(worktree);
4719 if (repo)
4720 got_repo_close(repo);
4721 got_ref_list_free(&refs);
4722 return error;
4725 static const struct got_error *
4726 draw_tree_entries(struct tog_view *view,
4727 struct got_tree_entry **first_displayed_entry,
4728 struct got_tree_entry **last_displayed_entry,
4729 struct got_tree_entry **selected_entry, int *ndisplayed,
4730 const char *label, int show_ids, const char *parent_path,
4731 struct got_tree_object *tree, int selected, int limit,
4732 int isroot, struct tog_colors *colors, struct got_repository *repo)
4734 const struct got_error *err = NULL;
4735 struct got_tree_entry *te;
4736 wchar_t *wline;
4737 struct tog_color *tc;
4738 int width, n, i, nentries;
4740 *ndisplayed = 0;
4742 werase(view->window);
4744 if (limit == 0)
4745 return NULL;
4747 err = format_line(&wline, &width, label, view->ncols, 0);
4748 if (err)
4749 return err;
4750 if (view_needs_focus_indication(view))
4751 wstandout(view->window);
4752 tc = get_color(colors, TOG_COLOR_COMMIT);
4753 if (tc)
4754 wattr_on(view->window,
4755 COLOR_PAIR(tc->colorpair), NULL);
4756 waddwstr(view->window, wline);
4757 if (tc)
4758 wattr_off(view->window,
4759 COLOR_PAIR(tc->colorpair), NULL);
4760 if (view_needs_focus_indication(view))
4761 wstandend(view->window);
4762 free(wline);
4763 wline = NULL;
4764 if (width < view->ncols - 1)
4765 waddch(view->window, '\n');
4766 if (--limit <= 0)
4767 return NULL;
4768 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4769 if (err)
4770 return err;
4771 waddwstr(view->window, wline);
4772 free(wline);
4773 wline = NULL;
4774 if (width < view->ncols - 1)
4775 waddch(view->window, '\n');
4776 if (--limit <= 0)
4777 return NULL;
4778 waddch(view->window, '\n');
4779 if (--limit <= 0)
4780 return NULL;
4782 if (*first_displayed_entry == NULL) {
4783 te = got_object_tree_get_first_entry(tree);
4784 if (selected == 0) {
4785 if (view->focussed)
4786 wstandout(view->window);
4787 *selected_entry = NULL;
4789 waddstr(view->window, " ..\n"); /* parent directory */
4790 if (selected == 0 && view->focussed)
4791 wstandend(view->window);
4792 (*ndisplayed)++;
4793 if (--limit <= 0)
4794 return NULL;
4795 n = 1;
4796 } else {
4797 n = 0;
4798 te = *first_displayed_entry;
4801 nentries = got_object_tree_get_nentries(tree);
4802 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4803 char *line = NULL, *id_str = NULL, *link_target = NULL;
4804 const char *modestr = "";
4805 mode_t mode;
4807 te = got_object_tree_get_entry(tree, i);
4808 mode = got_tree_entry_get_mode(te);
4810 if (show_ids) {
4811 err = got_object_id_str(&id_str,
4812 got_tree_entry_get_id(te));
4813 if (err)
4814 return got_error_from_errno(
4815 "got_object_id_str");
4817 if (got_object_tree_entry_is_submodule(te))
4818 modestr = "$";
4819 else if (S_ISLNK(mode)) {
4820 int i;
4822 err = got_tree_entry_get_symlink_target(&link_target,
4823 te, repo);
4824 if (err) {
4825 free(id_str);
4826 return err;
4828 for (i = 0; i < strlen(link_target); i++) {
4829 if (!isprint((unsigned char)link_target[i]))
4830 link_target[i] = '?';
4832 modestr = "@";
4834 else if (S_ISDIR(mode))
4835 modestr = "/";
4836 else if (mode & S_IXUSR)
4837 modestr = "*";
4838 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4839 got_tree_entry_get_name(te), modestr,
4840 link_target ? " -> ": "",
4841 link_target ? link_target : "") == -1) {
4842 free(id_str);
4843 free(link_target);
4844 return got_error_from_errno("asprintf");
4846 free(id_str);
4847 free(link_target);
4848 err = format_line(&wline, &width, line, view->ncols, 0);
4849 if (err) {
4850 free(line);
4851 break;
4853 if (n == selected) {
4854 if (view->focussed)
4855 wstandout(view->window);
4856 *selected_entry = te;
4858 tc = match_color(colors, line);
4859 if (tc)
4860 wattr_on(view->window,
4861 COLOR_PAIR(tc->colorpair), NULL);
4862 waddwstr(view->window, wline);
4863 if (tc)
4864 wattr_off(view->window,
4865 COLOR_PAIR(tc->colorpair), NULL);
4866 if (width < view->ncols - 1)
4867 waddch(view->window, '\n');
4868 if (n == selected && view->focussed)
4869 wstandend(view->window);
4870 free(line);
4871 free(wline);
4872 wline = NULL;
4873 n++;
4874 (*ndisplayed)++;
4875 *last_displayed_entry = te;
4876 if (--limit <= 0)
4877 break;
4880 return err;
4883 static void
4884 tree_scroll_up(struct tog_view *view,
4885 struct got_tree_entry **first_displayed_entry, int maxscroll,
4886 struct got_tree_object *tree, int isroot)
4888 struct got_tree_entry *te;
4889 int i;
4891 if (*first_displayed_entry == NULL)
4892 return;
4894 te = got_object_tree_get_entry(tree, 0);
4895 if (*first_displayed_entry == te) {
4896 if (!isroot)
4897 *first_displayed_entry = NULL;
4898 return;
4901 i = 0;
4902 while (*first_displayed_entry && i < maxscroll) {
4903 *first_displayed_entry = got_tree_entry_get_prev(tree,
4904 *first_displayed_entry);
4905 i++;
4907 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4908 *first_displayed_entry = NULL;
4911 static int
4912 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4913 struct got_tree_entry *last_displayed_entry,
4914 struct got_tree_object *tree)
4916 struct got_tree_entry *next, *last;
4917 int n = 0;
4919 if (*first_displayed_entry)
4920 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4921 else
4922 next = got_object_tree_get_first_entry(tree);
4924 last = last_displayed_entry;
4925 while (next && last && n++ < maxscroll) {
4926 last = got_tree_entry_get_next(tree, last);
4927 if (last) {
4928 *first_displayed_entry = next;
4929 next = got_tree_entry_get_next(tree, next);
4932 return n;
4935 static const struct got_error *
4936 tree_entry_path(char **path, struct tog_parent_trees *parents,
4937 struct got_tree_entry *te)
4939 const struct got_error *err = NULL;
4940 struct tog_parent_tree *pt;
4941 size_t len = 2; /* for leading slash and NUL */
4943 TAILQ_FOREACH(pt, parents, entry)
4944 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4945 + 1 /* slash */;
4946 if (te)
4947 len += strlen(got_tree_entry_get_name(te));
4949 *path = calloc(1, len);
4950 if (path == NULL)
4951 return got_error_from_errno("calloc");
4953 (*path)[0] = '/';
4954 pt = TAILQ_LAST(parents, tog_parent_trees);
4955 while (pt) {
4956 const char *name = got_tree_entry_get_name(pt->selected_entry);
4957 if (strlcat(*path, name, len) >= len) {
4958 err = got_error(GOT_ERR_NO_SPACE);
4959 goto done;
4961 if (strlcat(*path, "/", len) >= len) {
4962 err = got_error(GOT_ERR_NO_SPACE);
4963 goto done;
4965 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4967 if (te) {
4968 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4969 err = got_error(GOT_ERR_NO_SPACE);
4970 goto done;
4973 done:
4974 if (err) {
4975 free(*path);
4976 *path = NULL;
4978 return err;
4981 static const struct got_error *
4982 blame_tree_entry(struct tog_view **new_view, int begin_x,
4983 struct got_tree_entry *te, struct tog_parent_trees *parents,
4984 struct got_object_id *commit_id, struct got_reflist_head *refs,
4985 struct got_repository *repo)
4987 const struct got_error *err = NULL;
4988 char *path;
4989 struct tog_view *blame_view;
4991 *new_view = NULL;
4993 err = tree_entry_path(&path, parents, te);
4994 if (err)
4995 return err;
4997 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4998 if (blame_view == NULL) {
4999 err = got_error_from_errno("view_open");
5000 goto done;
5003 err = open_blame_view(blame_view, path, commit_id, refs, repo);
5004 if (err) {
5005 if (err->code == GOT_ERR_CANCELLED)
5006 err = NULL;
5007 view_close(blame_view);
5008 } else
5009 *new_view = blame_view;
5010 done:
5011 free(path);
5012 return err;
5015 static const struct got_error *
5016 log_tree_entry(struct tog_view **new_view, int begin_x,
5017 struct got_tree_entry *te, struct tog_parent_trees *parents,
5018 struct got_object_id *commit_id, struct got_reflist_head *refs,
5019 struct got_repository *repo)
5021 struct tog_view *log_view;
5022 const struct got_error *err = NULL;
5023 char *path;
5025 *new_view = NULL;
5027 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5028 if (log_view == NULL)
5029 return got_error_from_errno("view_open");
5031 err = tree_entry_path(&path, parents, te);
5032 if (err)
5033 return err;
5035 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
5036 if (err)
5037 view_close(log_view);
5038 else
5039 *new_view = log_view;
5040 free(path);
5041 return err;
5044 static const struct got_error *
5045 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5046 struct got_object_id *commit_id, struct got_reflist_head *refs,
5047 struct got_repository *repo)
5049 const struct got_error *err = NULL;
5050 char *commit_id_str = NULL;
5051 struct tog_tree_view_state *s = &view->state.tree;
5053 TAILQ_INIT(&s->parents);
5055 err = got_object_id_str(&commit_id_str, commit_id);
5056 if (err != NULL)
5057 goto done;
5059 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5060 err = got_error_from_errno("asprintf");
5061 goto done;
5064 s->root = s->tree = root;
5065 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5066 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5067 s->commit_id = got_object_id_dup(commit_id);
5068 if (s->commit_id == NULL) {
5069 err = got_error_from_errno("got_object_id_dup");
5070 goto done;
5072 s->refs = refs;
5073 s->repo = repo;
5075 SIMPLEQ_INIT(&s->colors);
5077 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5078 err = add_color(&s->colors, "\\$$",
5079 TOG_COLOR_TREE_SUBMODULE,
5080 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5081 if (err)
5082 goto done;
5083 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5084 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5085 if (err) {
5086 free_colors(&s->colors);
5087 goto done;
5089 err = add_color(&s->colors, "/$",
5090 TOG_COLOR_TREE_DIRECTORY,
5091 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5092 if (err) {
5093 free_colors(&s->colors);
5094 goto done;
5097 err = add_color(&s->colors, "\\*$",
5098 TOG_COLOR_TREE_EXECUTABLE,
5099 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5100 if (err) {
5101 free_colors(&s->colors);
5102 goto done;
5105 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5106 get_color_value("TOG_COLOR_COMMIT"));
5107 if (err) {
5108 free_colors(&s->colors);
5109 goto done;
5113 view->show = show_tree_view;
5114 view->input = input_tree_view;
5115 view->close = close_tree_view;
5116 view->search_start = search_start_tree_view;
5117 view->search_next = search_next_tree_view;
5118 done:
5119 free(commit_id_str);
5120 if (err) {
5121 free(s->tree_label);
5122 s->tree_label = NULL;
5124 return err;
5127 static const struct got_error *
5128 close_tree_view(struct tog_view *view)
5130 struct tog_tree_view_state *s = &view->state.tree;
5132 free_colors(&s->colors);
5133 free(s->tree_label);
5134 s->tree_label = NULL;
5135 free(s->commit_id);
5136 s->commit_id = NULL;
5137 while (!TAILQ_EMPTY(&s->parents)) {
5138 struct tog_parent_tree *parent;
5139 parent = TAILQ_FIRST(&s->parents);
5140 TAILQ_REMOVE(&s->parents, parent, entry);
5141 free(parent);
5144 if (s->tree != s->root)
5145 got_object_tree_close(s->tree);
5146 got_object_tree_close(s->root);
5148 return NULL;
5151 static const struct got_error *
5152 search_start_tree_view(struct tog_view *view)
5154 struct tog_tree_view_state *s = &view->state.tree;
5156 s->matched_entry = NULL;
5157 return NULL;
5160 static int
5161 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5163 regmatch_t regmatch;
5165 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5166 0) == 0;
5169 static const struct got_error *
5170 search_next_tree_view(struct tog_view *view)
5172 struct tog_tree_view_state *s = &view->state.tree;
5173 struct got_tree_entry *te = NULL;
5175 if (!view->searching) {
5176 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5177 return NULL;
5180 if (s->matched_entry) {
5181 if (view->searching == TOG_SEARCH_FORWARD) {
5182 if (s->selected_entry)
5183 te = got_tree_entry_get_next(s->tree,
5184 s->selected_entry);
5185 else
5186 te = got_object_tree_get_first_entry(s->tree);
5187 } else {
5188 if (s->selected_entry == NULL)
5189 te = got_object_tree_get_last_entry(s->tree);
5190 else
5191 te = got_tree_entry_get_prev(s->tree,
5192 s->selected_entry);
5194 } else {
5195 if (view->searching == TOG_SEARCH_FORWARD)
5196 te = got_object_tree_get_first_entry(s->tree);
5197 else
5198 te = got_object_tree_get_last_entry(s->tree);
5201 while (1) {
5202 if (te == NULL) {
5203 if (s->matched_entry == NULL) {
5204 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5205 return NULL;
5207 if (view->searching == TOG_SEARCH_FORWARD)
5208 te = got_object_tree_get_first_entry(s->tree);
5209 else
5210 te = got_object_tree_get_last_entry(s->tree);
5213 if (match_tree_entry(te, &view->regex)) {
5214 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5215 s->matched_entry = te;
5216 break;
5219 if (view->searching == TOG_SEARCH_FORWARD)
5220 te = got_tree_entry_get_next(s->tree, te);
5221 else
5222 te = got_tree_entry_get_prev(s->tree, te);
5225 if (s->matched_entry) {
5226 s->first_displayed_entry = s->matched_entry;
5227 s->selected = 0;
5230 return NULL;
5233 static const struct got_error *
5234 show_tree_view(struct tog_view *view)
5236 const struct got_error *err = NULL;
5237 struct tog_tree_view_state *s = &view->state.tree;
5238 char *parent_path;
5240 err = tree_entry_path(&parent_path, &s->parents, NULL);
5241 if (err)
5242 return err;
5244 err = draw_tree_entries(view, &s->first_displayed_entry,
5245 &s->last_displayed_entry, &s->selected_entry,
5246 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5247 s->tree, s->selected, view->nlines, s->tree == s->root,
5248 &s->colors, s->repo);
5249 free(parent_path);
5251 view_vborder(view);
5252 return err;
5255 static const struct got_error *
5256 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5257 struct tog_view **focus_view, struct tog_view *view, int ch)
5259 const struct got_error *err = NULL;
5260 struct tog_tree_view_state *s = &view->state.tree;
5261 struct tog_view *log_view;
5262 int begin_x = 0, nscrolled;
5264 switch (ch) {
5265 case 'i':
5266 s->show_ids = !s->show_ids;
5267 break;
5268 case 'l':
5269 if (!s->selected_entry)
5270 break;
5271 if (view_is_parent_view(view))
5272 begin_x = view_split_begin_x(view->begin_x);
5273 err = log_tree_entry(&log_view, begin_x,
5274 s->selected_entry, &s->parents,
5275 s->commit_id, s->refs, s->repo);
5276 if (view_is_parent_view(view)) {
5277 err = view_close_child(view);
5278 if (err)
5279 return err;
5280 err = view_set_child(view, log_view);
5281 if (err) {
5282 view_close(log_view);
5283 break;
5285 *focus_view = log_view;
5286 view->child_focussed = 1;
5287 } else
5288 *new_view = log_view;
5289 break;
5290 case 'k':
5291 case KEY_UP:
5292 if (s->selected > 0) {
5293 s->selected--;
5294 if (s->selected == 0)
5295 break;
5297 if (s->selected > 0)
5298 break;
5299 tree_scroll_up(view, &s->first_displayed_entry, 1,
5300 s->tree, s->tree == s->root);
5301 break;
5302 case KEY_PPAGE:
5303 case CTRL('b'):
5304 tree_scroll_up(view, &s->first_displayed_entry,
5305 MAX(0, view->nlines - 4 - s->selected), s->tree,
5306 s->tree == s->root);
5307 s->selected = 0;
5308 if (got_object_tree_get_first_entry(s->tree) ==
5309 s->first_displayed_entry && s->tree != s->root)
5310 s->first_displayed_entry = NULL;
5311 break;
5312 case 'j':
5313 case KEY_DOWN:
5314 if (s->selected < s->ndisplayed - 1) {
5315 s->selected++;
5316 break;
5318 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5319 == NULL)
5320 /* can't scroll any further */
5321 break;
5322 tree_scroll_down(&s->first_displayed_entry, 1,
5323 s->last_displayed_entry, s->tree);
5324 break;
5325 case KEY_NPAGE:
5326 case CTRL('f'):
5327 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5328 == NULL) {
5329 /* can't scroll any further; move cursor down */
5330 if (s->selected < s->ndisplayed - 1)
5331 s->selected = s->ndisplayed - 1;
5332 break;
5334 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5335 view->nlines, s->last_displayed_entry, s->tree);
5336 if (nscrolled < view->nlines) {
5337 int ndisplayed = 0;
5338 struct got_tree_entry *te;
5339 te = s->first_displayed_entry;
5340 do {
5341 ndisplayed++;
5342 te = got_tree_entry_get_next(s->tree, te);
5343 } while (te);
5344 s->selected = ndisplayed - 1;
5346 break;
5347 case KEY_ENTER:
5348 case '\r':
5349 case KEY_BACKSPACE:
5350 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5351 struct tog_parent_tree *parent;
5352 /* user selected '..' */
5353 if (s->tree == s->root)
5354 break;
5355 parent = TAILQ_FIRST(&s->parents);
5356 TAILQ_REMOVE(&s->parents, parent,
5357 entry);
5358 got_object_tree_close(s->tree);
5359 s->tree = parent->tree;
5360 s->first_displayed_entry =
5361 parent->first_displayed_entry;
5362 s->selected_entry =
5363 parent->selected_entry;
5364 s->selected = parent->selected;
5365 free(parent);
5366 } else if (S_ISDIR(got_tree_entry_get_mode(
5367 s->selected_entry))) {
5368 struct got_tree_object *subtree;
5369 err = got_object_open_as_tree(&subtree, s->repo,
5370 got_tree_entry_get_id(s->selected_entry));
5371 if (err)
5372 break;
5373 err = tree_view_visit_subtree(subtree, s);
5374 if (err) {
5375 got_object_tree_close(subtree);
5376 break;
5378 } else if (S_ISREG(got_tree_entry_get_mode(
5379 s->selected_entry))) {
5380 struct tog_view *blame_view;
5381 int begin_x = view_is_parent_view(view) ?
5382 view_split_begin_x(view->begin_x) : 0;
5384 err = blame_tree_entry(&blame_view, begin_x,
5385 s->selected_entry, &s->parents,
5386 s->commit_id, s->refs, s->repo);
5387 if (err)
5388 break;
5389 if (view_is_parent_view(view)) {
5390 err = view_close_child(view);
5391 if (err)
5392 return err;
5393 err = view_set_child(view, blame_view);
5394 if (err) {
5395 view_close(blame_view);
5396 break;
5398 *focus_view = blame_view;
5399 view->child_focussed = 1;
5400 } else
5401 *new_view = blame_view;
5403 break;
5404 case KEY_RESIZE:
5405 if (s->selected > view->nlines)
5406 s->selected = s->ndisplayed - 1;
5407 break;
5408 default:
5409 break;
5412 return err;
5415 __dead static void
5416 usage_tree(void)
5418 endwin();
5419 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5420 getprogname());
5421 exit(1);
5424 static const struct got_error *
5425 cmd_tree(int argc, char *argv[])
5427 const struct got_error *error;
5428 struct got_repository *repo = NULL;
5429 struct got_worktree *worktree = NULL;
5430 struct got_reflist_head refs;
5431 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5432 struct got_object_id *commit_id = NULL;
5433 char *commit_id_arg = NULL;
5434 struct got_commit_object *commit = NULL;
5435 struct got_tree_object *tree = NULL;
5436 int ch;
5437 struct tog_view *view;
5439 SIMPLEQ_INIT(&refs);
5441 #ifndef PROFILE
5442 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5443 NULL) == -1)
5444 err(1, "pledge");
5445 #endif
5447 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5448 switch (ch) {
5449 case 'c':
5450 commit_id_arg = optarg;
5451 break;
5452 case 'r':
5453 repo_path = realpath(optarg, NULL);
5454 if (repo_path == NULL)
5455 return got_error_from_errno2("realpath",
5456 optarg);
5457 break;
5458 default:
5459 usage_tree();
5460 /* NOTREACHED */
5464 argc -= optind;
5465 argv += optind;
5467 if (argc > 1)
5468 usage_tree();
5470 cwd = getcwd(NULL, 0);
5471 if (cwd == NULL)
5472 return got_error_from_errno("getcwd");
5474 error = got_worktree_open(&worktree, cwd);
5475 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5476 goto done;
5478 if (repo_path == NULL) {
5479 if (worktree)
5480 repo_path =
5481 strdup(got_worktree_get_repo_path(worktree));
5482 else
5483 repo_path = strdup(cwd);
5485 if (repo_path == NULL) {
5486 error = got_error_from_errno("strdup");
5487 goto done;
5490 error = got_repo_open(&repo, repo_path, NULL);
5491 if (error != NULL)
5492 goto done;
5494 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5495 repo, worktree);
5496 if (error)
5497 goto done;
5499 init_curses();
5501 error = apply_unveil(got_repo_get_path(repo), NULL);
5502 if (error)
5503 goto done;
5505 error = got_repo_match_object_id(&commit_id, NULL,
5506 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5507 GOT_OBJ_TYPE_COMMIT, 1, repo);
5508 if (error)
5509 goto done;
5511 error = got_object_open_as_commit(&commit, repo, commit_id);
5512 if (error)
5513 goto done;
5515 error = got_object_open_as_tree(&tree, repo,
5516 got_object_commit_get_tree_id(commit));
5517 if (error)
5518 goto done;
5520 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5521 if (error)
5522 goto done;
5524 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5525 if (view == NULL) {
5526 error = got_error_from_errno("view_open");
5527 goto done;
5529 error = open_tree_view(view, tree, commit_id, &refs, repo);
5530 if (error)
5531 goto done;
5532 if (!got_path_is_root_dir(in_repo_path)) {
5533 error = tree_view_walk_path(&view->state.tree, commit_id,
5534 in_repo_path, repo);
5535 if (error)
5536 goto done;
5539 if (worktree) {
5540 /* Release work tree lock. */
5541 got_worktree_close(worktree);
5542 worktree = NULL;
5544 error = view_loop(view);
5545 done:
5546 free(repo_path);
5547 free(cwd);
5548 free(commit_id);
5549 if (commit)
5550 got_object_commit_close(commit);
5551 if (tree)
5552 got_object_tree_close(tree);
5553 if (repo)
5554 got_repo_close(repo);
5555 got_ref_list_free(&refs);
5556 return error;
5559 static void
5560 list_commands(FILE *fp)
5562 int i;
5564 fprintf(fp, "commands:");
5565 for (i = 0; i < nitems(tog_commands); i++) {
5566 struct tog_cmd *cmd = &tog_commands[i];
5567 fprintf(fp, " %s", cmd->name);
5569 fputc('\n', fp);
5572 __dead static void
5573 usage(int hflag, int status)
5575 FILE *fp = (status == 0) ? stdout : stderr;
5577 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5578 getprogname());
5579 if (hflag) {
5580 fprintf(fp, "lazy usage: %s path\n", getprogname());
5581 list_commands(fp);
5583 exit(status);
5586 static char **
5587 make_argv(int argc, ...)
5589 va_list ap;
5590 char **argv;
5591 int i;
5593 va_start(ap, argc);
5595 argv = calloc(argc, sizeof(char *));
5596 if (argv == NULL)
5597 err(1, "calloc");
5598 for (i = 0; i < argc; i++) {
5599 argv[i] = strdup(va_arg(ap, char *));
5600 if (argv[i] == NULL)
5601 err(1, "strdup");
5604 va_end(ap);
5605 return argv;
5609 * Try to convert 'tog path' into a 'tog log path' command.
5610 * The user could simply have mistyped the command rather than knowingly
5611 * provided a path. So check whether argv[0] can in fact be resolved
5612 * to a path in the HEAD commit and print a special error if not.
5613 * This hack is for mpi@ <3
5615 static const struct got_error *
5616 tog_log_with_path(int argc, char *argv[])
5618 const struct got_error *error = NULL;
5619 struct tog_cmd *cmd = NULL;
5620 struct got_repository *repo = NULL;
5621 struct got_worktree *worktree = NULL;
5622 struct got_object_id *commit_id = NULL, *id = NULL;
5623 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5624 char *commit_id_str = NULL, **cmd_argv = NULL;
5626 cwd = getcwd(NULL, 0);
5627 if (cwd == NULL)
5628 return got_error_from_errno("getcwd");
5630 error = got_worktree_open(&worktree, cwd);
5631 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5632 goto done;
5634 if (worktree)
5635 repo_path = strdup(got_worktree_get_repo_path(worktree));
5636 else
5637 repo_path = strdup(cwd);
5638 if (repo_path == NULL) {
5639 error = got_error_from_errno("strdup");
5640 goto done;
5643 error = got_repo_open(&repo, repo_path, NULL);
5644 if (error != NULL)
5645 goto done;
5647 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5648 repo, worktree);
5649 if (error)
5650 goto done;
5652 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5653 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5654 GOT_OBJ_TYPE_COMMIT, 1, repo);
5655 if (error)
5656 goto done;
5658 if (worktree) {
5659 got_worktree_close(worktree);
5660 worktree = NULL;
5663 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5664 if (error) {
5665 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5666 goto done;
5667 fprintf(stderr, "%s: '%s' is no known command or path\n",
5668 getprogname(), argv[0]);
5669 usage(1, 1);
5670 /* not reached */
5673 got_repo_close(repo);
5674 repo = NULL;
5676 error = got_object_id_str(&commit_id_str, commit_id);
5677 if (error)
5678 goto done;
5680 cmd = &tog_commands[0]; /* log */
5681 argc = 4;
5682 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5683 error = cmd->cmd_main(argc, cmd_argv);
5684 done:
5685 if (repo)
5686 got_repo_close(repo);
5687 if (worktree)
5688 got_worktree_close(worktree);
5689 free(id);
5690 free(commit_id_str);
5691 free(commit_id);
5692 free(cwd);
5693 free(repo_path);
5694 free(in_repo_path);
5695 if (cmd_argv) {
5696 int i;
5697 for (i = 0; i < argc; i++)
5698 free(cmd_argv[i]);
5699 free(cmd_argv);
5701 return error;
5704 int
5705 main(int argc, char *argv[])
5707 const struct got_error *error = NULL;
5708 struct tog_cmd *cmd = NULL;
5709 int ch, hflag = 0, Vflag = 0;
5710 char **cmd_argv = NULL;
5711 static struct option longopts[] = {
5712 { "version", no_argument, NULL, 'V' },
5713 { NULL, 0, NULL, 0}
5716 setlocale(LC_CTYPE, "");
5718 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5719 switch (ch) {
5720 case 'h':
5721 hflag = 1;
5722 break;
5723 case 'V':
5724 Vflag = 1;
5725 break;
5726 default:
5727 usage(hflag, 1);
5728 /* NOTREACHED */
5732 argc -= optind;
5733 argv += optind;
5734 optind = 1;
5735 optreset = 1;
5737 if (Vflag) {
5738 got_version_print_str();
5739 return 0;
5742 if (argc == 0) {
5743 if (hflag)
5744 usage(hflag, 0);
5745 /* Build an argument vector which runs a default command. */
5746 cmd = &tog_commands[0];
5747 argc = 1;
5748 cmd_argv = make_argv(argc, cmd->name);
5749 } else {
5750 int i;
5752 /* Did the user specify a command? */
5753 for (i = 0; i < nitems(tog_commands); i++) {
5754 if (strncmp(tog_commands[i].name, argv[0],
5755 strlen(argv[0])) == 0) {
5756 cmd = &tog_commands[i];
5757 break;
5762 if (cmd == NULL) {
5763 if (argc != 1)
5764 usage(0, 1);
5765 /* No command specified; try log with a path */
5766 error = tog_log_with_path(argc, argv);
5767 } else {
5768 if (hflag)
5769 cmd->cmd_usage();
5770 else
5771 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5774 endwin();
5775 putchar('\n');
5776 if (cmd_argv) {
5777 int i;
5778 for (i = 0; i < argc; i++)
5779 free(cmd_argv[i]);
5780 free(cmd_argv);
5783 if (error && error->code != GOT_ERR_CANCELLED)
5784 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5785 return 0;