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 if (s->thread_args.repo) {
2036 got_repo_close(s->thread_args.repo);
2037 s->thread_args.repo = NULL;
2040 if (s->thread_args.graph) {
2041 got_commit_graph_close(s->thread_args.graph);
2042 s->thread_args.graph = NULL;
2045 return err;
2048 static const struct got_error *
2049 close_log_view(struct tog_view *view)
2051 const struct got_error *err = NULL;
2052 struct tog_log_view_state *s = &view->state.log;
2053 int errcode;
2055 err = stop_log_thread(s);
2057 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2058 if (errcode && err == NULL)
2059 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2061 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2062 if (errcode && err == NULL)
2063 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2065 free_commits(&s->commits);
2066 free(s->in_repo_path);
2067 s->in_repo_path = NULL;
2068 free(s->start_id);
2069 s->start_id = NULL;
2070 return err;
2073 static const struct got_error *
2074 search_start_log_view(struct tog_view *view)
2076 struct tog_log_view_state *s = &view->state.log;
2078 s->matched_entry = NULL;
2079 s->search_entry = NULL;
2080 return NULL;
2083 static const struct got_error *
2084 search_next_log_view(struct tog_view *view)
2086 const struct got_error *err = NULL;
2087 struct tog_log_view_state *s = &view->state.log;
2088 struct commit_queue_entry *entry;
2090 /* Display progress update in log view. */
2091 show_log_view(view);
2092 update_panels();
2093 doupdate();
2095 if (s->search_entry) {
2096 int errcode, ch;
2097 errcode = pthread_mutex_unlock(&tog_mutex);
2098 if (errcode)
2099 return got_error_set_errno(errcode,
2100 "pthread_mutex_unlock");
2101 ch = wgetch(view->window);
2102 errcode = pthread_mutex_lock(&tog_mutex);
2103 if (errcode)
2104 return got_error_set_errno(errcode,
2105 "pthread_mutex_lock");
2106 if (ch == KEY_BACKSPACE) {
2107 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2108 return NULL;
2110 if (view->searching == TOG_SEARCH_FORWARD)
2111 entry = TAILQ_NEXT(s->search_entry, entry);
2112 else
2113 entry = TAILQ_PREV(s->search_entry,
2114 commit_queue_head, entry);
2115 } else if (s->matched_entry) {
2116 if (view->searching == TOG_SEARCH_FORWARD)
2117 entry = TAILQ_NEXT(s->matched_entry, entry);
2118 else
2119 entry = TAILQ_PREV(s->matched_entry,
2120 commit_queue_head, entry);
2121 } else {
2122 if (view->searching == TOG_SEARCH_FORWARD)
2123 entry = TAILQ_FIRST(&s->commits.head);
2124 else
2125 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2128 while (1) {
2129 int have_match = 0;
2131 if (entry == NULL) {
2132 if (s->thread_args.log_complete ||
2133 view->searching == TOG_SEARCH_BACKWARD) {
2134 view->search_next_done =
2135 (s->matched_entry == NULL ?
2136 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2137 s->search_entry = NULL;
2138 return NULL;
2141 * Poke the log thread for more commits and return,
2142 * allowing the main loop to make progress. Search
2143 * will resume at s->search_entry once we come back.
2145 s->thread_args.commits_needed++;
2146 return trigger_log_thread(view, 0,
2147 &s->thread_args.commits_needed,
2148 &s->thread_args.log_complete,
2149 &s->thread_args.need_commits,
2150 &s->thread_args.commit_loaded);
2153 err = match_commit(&have_match, entry->id, entry->commit,
2154 &view->regex);
2155 if (err)
2156 break;
2157 if (have_match) {
2158 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2159 s->matched_entry = entry;
2160 break;
2163 s->search_entry = entry;
2164 if (view->searching == TOG_SEARCH_FORWARD)
2165 entry = TAILQ_NEXT(entry, entry);
2166 else
2167 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2170 if (s->matched_entry) {
2171 int cur = s->selected_entry->idx;
2172 while (cur < s->matched_entry->idx) {
2173 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2174 if (err)
2175 return err;
2176 cur++;
2178 while (cur > s->matched_entry->idx) {
2179 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2180 if (err)
2181 return err;
2182 cur--;
2186 s->search_entry = NULL;
2188 return NULL;
2191 static const struct got_error *
2192 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2193 struct got_reflist_head *refs, struct got_repository *repo,
2194 const char *head_ref_name, const char *in_repo_path,
2195 int log_branches)
2197 const struct got_error *err = NULL;
2198 struct tog_log_view_state *s = &view->state.log;
2199 struct got_repository *thread_repo = NULL;
2200 struct got_commit_graph *thread_graph = NULL;
2201 int errcode;
2203 if (in_repo_path != s->in_repo_path) {
2204 free(s->in_repo_path);
2205 s->in_repo_path = strdup(in_repo_path);
2206 if (s->in_repo_path == NULL)
2207 return got_error_from_errno("strdup");
2210 /* The commit queue only contains commits being displayed. */
2211 TAILQ_INIT(&s->commits.head);
2212 s->commits.ncommits = 0;
2214 s->refs = refs;
2215 s->repo = repo;
2216 s->head_ref_name = head_ref_name;
2217 s->start_id = got_object_id_dup(start_id);
2218 if (s->start_id == NULL) {
2219 err = got_error_from_errno("got_object_id_dup");
2220 goto done;
2222 s->log_branches = log_branches;
2224 SIMPLEQ_INIT(&s->colors);
2225 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2226 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2227 get_color_value("TOG_COLOR_COMMIT"));
2228 if (err)
2229 goto done;
2230 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2231 get_color_value("TOG_COLOR_AUTHOR"));
2232 if (err) {
2233 free_colors(&s->colors);
2234 goto done;
2236 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2237 get_color_value("TOG_COLOR_DATE"));
2238 if (err) {
2239 free_colors(&s->colors);
2240 goto done;
2244 view->show = show_log_view;
2245 view->input = input_log_view;
2246 view->close = close_log_view;
2247 view->search_start = search_start_log_view;
2248 view->search_next = search_next_log_view;
2250 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2251 if (err)
2252 goto done;
2253 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2254 !s->log_branches);
2255 if (err)
2256 goto done;
2257 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2258 s->repo, NULL, NULL);
2259 if (err)
2260 goto done;
2262 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2263 if (errcode) {
2264 err = got_error_set_errno(errcode, "pthread_cond_init");
2265 goto done;
2267 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2268 if (errcode) {
2269 err = got_error_set_errno(errcode, "pthread_cond_init");
2270 goto done;
2273 s->thread_args.commits_needed = view->nlines;
2274 s->thread_args.graph = thread_graph;
2275 s->thread_args.commits = &s->commits;
2276 s->thread_args.in_repo_path = s->in_repo_path;
2277 s->thread_args.start_id = s->start_id;
2278 s->thread_args.repo = thread_repo;
2279 s->thread_args.log_complete = 0;
2280 s->thread_args.quit = &s->quit;
2281 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2282 s->thread_args.selected_entry = &s->selected_entry;
2283 s->thread_args.searching = &view->searching;
2284 s->thread_args.search_next_done = &view->search_next_done;
2285 s->thread_args.regex = &view->regex;
2286 done:
2287 if (err)
2288 close_log_view(view);
2289 return err;
2292 static const struct got_error *
2293 show_log_view(struct tog_view *view)
2295 struct tog_log_view_state *s = &view->state.log;
2297 if (s->thread == NULL) {
2298 int errcode = pthread_create(&s->thread, NULL, log_thread,
2299 &s->thread_args);
2300 if (errcode)
2301 return got_error_set_errno(errcode, "pthread_create");
2304 return draw_commits(view, &s->last_displayed_entry,
2305 &s->selected_entry, s->first_displayed_entry,
2306 &s->commits, s->selected, view->nlines, s->refs,
2307 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2310 static const struct got_error *
2311 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2312 struct tog_view **focus_view, struct tog_view *view, int ch)
2314 const struct got_error *err = NULL;
2315 struct tog_log_view_state *s = &view->state.log;
2316 char *parent_path, *in_repo_path = NULL;
2317 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2318 int begin_x = 0;
2319 struct got_object_id *start_id;
2321 switch (ch) {
2322 case 'q':
2323 s->quit = 1;
2324 break;
2325 case 'k':
2326 case KEY_UP:
2327 case '<':
2328 case ',':
2329 if (s->first_displayed_entry == NULL)
2330 break;
2331 if (s->selected > 0)
2332 s->selected--;
2333 else
2334 scroll_up(view, &s->first_displayed_entry, 1,
2335 &s->commits);
2336 break;
2337 case KEY_PPAGE:
2338 case CTRL('b'):
2339 if (s->first_displayed_entry == NULL)
2340 break;
2341 if (TAILQ_FIRST(&s->commits.head) ==
2342 s->first_displayed_entry) {
2343 s->selected = 0;
2344 break;
2346 scroll_up(view, &s->first_displayed_entry,
2347 view->nlines - 1, &s->commits);
2348 break;
2349 case 'j':
2350 case KEY_DOWN:
2351 case '>':
2352 case '.':
2353 if (s->first_displayed_entry == NULL)
2354 break;
2355 if (s->selected < MIN(view->nlines - 2,
2356 s->commits.ncommits - 1)) {
2357 s->selected++;
2358 break;
2360 err = scroll_down(view, &s->first_displayed_entry, 1,
2361 &s->last_displayed_entry, &s->commits,
2362 &s->thread_args.log_complete,
2363 &s->thread_args.commits_needed,
2364 &s->thread_args.need_commits,
2365 &s->thread_args.commit_loaded);
2366 break;
2367 case KEY_NPAGE:
2368 case CTRL('f'): {
2369 struct commit_queue_entry *first;
2370 first = s->first_displayed_entry;
2371 if (first == NULL)
2372 break;
2373 err = scroll_down(view, &s->first_displayed_entry,
2374 view->nlines - 1, &s->last_displayed_entry,
2375 &s->commits, &s->thread_args.log_complete,
2376 &s->thread_args.commits_needed,
2377 &s->thread_args.need_commits,
2378 &s->thread_args.commit_loaded);
2379 if (err)
2380 break;
2381 if (first == s->first_displayed_entry &&
2382 s->selected < MIN(view->nlines - 2,
2383 s->commits.ncommits - 1)) {
2384 /* can't scroll further down */
2385 s->selected = MIN(view->nlines - 2,
2386 s->commits.ncommits - 1);
2388 err = NULL;
2389 break;
2391 case KEY_RESIZE:
2392 if (s->selected > view->nlines - 2)
2393 s->selected = view->nlines - 2;
2394 if (s->selected > s->commits.ncommits - 1)
2395 s->selected = s->commits.ncommits - 1;
2396 break;
2397 case KEY_ENTER:
2398 case ' ':
2399 case '\r':
2400 if (s->selected_entry == NULL)
2401 break;
2402 if (view_is_parent_view(view))
2403 begin_x = view_split_begin_x(view->begin_x);
2404 err = open_diff_view_for_commit(&diff_view, begin_x,
2405 s->selected_entry->commit, s->selected_entry->id,
2406 view, s->refs, s->repo);
2407 if (err)
2408 break;
2409 if (view_is_parent_view(view)) {
2410 err = view_close_child(view);
2411 if (err)
2412 return err;
2413 err = view_set_child(view, diff_view);
2414 if (err) {
2415 view_close(diff_view);
2416 break;
2418 *focus_view = diff_view;
2419 view->child_focussed = 1;
2420 } else
2421 *new_view = diff_view;
2422 break;
2423 case 't':
2424 if (s->selected_entry == NULL)
2425 break;
2426 if (view_is_parent_view(view))
2427 begin_x = view_split_begin_x(view->begin_x);
2428 err = browse_commit_tree(&tree_view, begin_x,
2429 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2430 if (err)
2431 break;
2432 if (view_is_parent_view(view)) {
2433 err = view_close_child(view);
2434 if (err)
2435 return err;
2436 err = view_set_child(view, tree_view);
2437 if (err) {
2438 view_close(tree_view);
2439 break;
2441 *focus_view = tree_view;
2442 view->child_focussed = 1;
2443 } else
2444 *new_view = tree_view;
2445 break;
2446 case KEY_BACKSPACE:
2447 if (got_path_cmp(s->in_repo_path, "/",
2448 strlen(s->in_repo_path), 1) == 0)
2449 break;
2450 err = got_path_dirname(&parent_path, s->in_repo_path);
2451 if (err)
2452 return err;
2453 err = stop_log_thread(s);
2454 if (err) {
2455 free(parent_path);
2456 return err;
2458 lv = view_open(view->nlines, view->ncols,
2459 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2460 if (lv == NULL) {
2461 free(parent_path);
2462 return got_error_from_errno("view_open");
2464 err = open_log_view(lv, s->start_id, s->refs,
2465 s->repo, s->head_ref_name, parent_path,
2466 s->log_branches);
2467 free(parent_path);
2468 if (err)
2469 return err;;
2470 if (view_is_parent_view(view))
2471 *new_view = lv;
2472 else {
2473 view_set_child(view->parent, lv);
2474 *focus_view = lv;
2476 break;
2477 case CTRL('l'):
2478 err = stop_log_thread(s);
2479 if (err)
2480 return err;
2481 lv = view_open(view->nlines, view->ncols,
2482 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2483 if (lv == NULL)
2484 return got_error_from_errno("view_open");
2485 err = got_repo_match_object_id(&start_id, NULL,
2486 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2487 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2488 if (err) {
2489 view_close(lv);
2490 return err;
2492 in_repo_path = strdup(s->in_repo_path);
2493 if (in_repo_path == NULL) {
2494 free(start_id);
2495 view_close(lv);
2496 return got_error_from_errno("strdup");
2498 got_ref_list_free(s->refs);
2499 err = got_ref_list(s->refs, s->repo, NULL,
2500 got_ref_cmp_by_name, NULL);
2501 if (err) {
2502 free(start_id);
2503 view_close(lv);
2504 return err;
2506 err = open_log_view(lv, start_id, s->refs, s->repo,
2507 s->head_ref_name, in_repo_path, s->log_branches);
2508 if (err) {
2509 free(start_id);
2510 view_close(lv);
2511 return err;;
2513 *dead_view = view;
2514 *new_view = lv;
2515 break;
2516 case 'B':
2517 s->log_branches = !s->log_branches;
2518 err = stop_log_thread(s);
2519 if (err)
2520 return err;
2521 lv = view_open(view->nlines, view->ncols,
2522 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2523 if (lv == NULL)
2524 return got_error_from_errno("view_open");
2525 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2526 s->head_ref_name, s->in_repo_path, s->log_branches);
2527 if (err) {
2528 view_close(lv);
2529 return err;;
2531 *dead_view = view;
2532 *new_view = lv;
2533 break;
2534 default:
2535 break;
2538 return err;
2541 static const struct got_error *
2542 apply_unveil(const char *repo_path, const char *worktree_path)
2544 const struct got_error *error;
2546 #ifdef PROFILE
2547 if (unveil("gmon.out", "rwc") != 0)
2548 return got_error_from_errno2("unveil", "gmon.out");
2549 #endif
2550 if (repo_path && unveil(repo_path, "r") != 0)
2551 return got_error_from_errno2("unveil", repo_path);
2553 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2554 return got_error_from_errno2("unveil", worktree_path);
2556 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2557 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2559 error = got_privsep_unveil_exec_helpers();
2560 if (error != NULL)
2561 return error;
2563 if (unveil(NULL, NULL) != 0)
2564 return got_error_from_errno("unveil");
2566 return NULL;
2569 static void
2570 init_curses(void)
2572 initscr();
2573 cbreak();
2574 halfdelay(1); /* Do fast refresh while initial view is loading. */
2575 noecho();
2576 nonl();
2577 intrflush(stdscr, FALSE);
2578 keypad(stdscr, TRUE);
2579 curs_set(0);
2580 if (getenv("TOG_COLORS") != NULL) {
2581 start_color();
2582 use_default_colors();
2584 signal(SIGWINCH, tog_sigwinch);
2585 signal(SIGPIPE, tog_sigpipe);
2586 signal(SIGCONT, tog_sigcont);
2589 static const struct got_error *
2590 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2591 struct got_repository *repo, struct got_worktree *worktree)
2593 const struct got_error *err = NULL;
2595 if (argc == 0) {
2596 *in_repo_path = strdup("/");
2597 if (*in_repo_path == NULL)
2598 return got_error_from_errno("strdup");
2599 return NULL;
2602 if (worktree) {
2603 const char *prefix = got_worktree_get_path_prefix(worktree);
2604 char *p;
2606 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2607 if (err)
2608 return err;
2609 if (asprintf(in_repo_path, "%s%s%s", prefix,
2610 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2611 p) == -1) {
2612 err = got_error_from_errno("asprintf");
2613 *in_repo_path = NULL;
2615 free(p);
2616 } else
2617 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2619 return err;
2622 static const struct got_error *
2623 cmd_log(int argc, char *argv[])
2625 const struct got_error *error;
2626 struct got_repository *repo = NULL;
2627 struct got_worktree *worktree = NULL;
2628 struct got_reflist_head refs;
2629 struct got_object_id *start_id = NULL;
2630 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2631 char *start_commit = NULL, *head_ref_name = NULL;
2632 int ch, log_branches = 0;
2633 struct tog_view *view;
2635 SIMPLEQ_INIT(&refs);
2637 #ifndef PROFILE
2638 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2639 NULL) == -1)
2640 err(1, "pledge");
2641 #endif
2643 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2644 switch (ch) {
2645 case 'b':
2646 log_branches = 1;
2647 break;
2648 case 'c':
2649 start_commit = optarg;
2650 break;
2651 case 'r':
2652 repo_path = realpath(optarg, NULL);
2653 if (repo_path == NULL)
2654 return got_error_from_errno2("realpath",
2655 optarg);
2656 break;
2657 default:
2658 usage_log();
2659 /* NOTREACHED */
2663 argc -= optind;
2664 argv += optind;
2666 if (argc > 1)
2667 usage_log();
2669 cwd = getcwd(NULL, 0);
2670 if (cwd == NULL)
2671 return got_error_from_errno("getcwd");
2673 error = got_worktree_open(&worktree, cwd);
2674 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2675 goto done;
2677 if (repo_path == NULL) {
2678 if (worktree)
2679 repo_path =
2680 strdup(got_worktree_get_repo_path(worktree));
2681 else
2682 repo_path = strdup(cwd);
2684 if (repo_path == NULL) {
2685 error = got_error_from_errno("strdup");
2686 goto done;
2689 error = got_repo_open(&repo, repo_path, NULL);
2690 if (error != NULL)
2691 goto done;
2693 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2694 repo, worktree);
2695 if (error)
2696 goto done;
2698 init_curses();
2700 error = apply_unveil(got_repo_get_path(repo),
2701 worktree ? got_worktree_get_root_path(worktree) : NULL);
2702 if (error)
2703 goto done;
2705 if (start_commit == NULL)
2706 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2707 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2708 GOT_OBJ_TYPE_COMMIT, 1, repo);
2709 else
2710 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2711 GOT_OBJ_TYPE_COMMIT, 1, repo);
2712 if (error != NULL)
2713 goto done;
2715 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2716 if (error)
2717 goto done;
2719 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2720 if (view == NULL) {
2721 error = got_error_from_errno("view_open");
2722 goto done;
2724 if (worktree) {
2725 head_ref_name = strdup(
2726 got_worktree_get_head_ref_name(worktree));
2727 if (head_ref_name == NULL) {
2728 error = got_error_from_errno("strdup");
2729 goto done;
2732 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2733 in_repo_path, log_branches);
2734 if (error)
2735 goto done;
2736 if (worktree) {
2737 /* Release work tree lock. */
2738 got_worktree_close(worktree);
2739 worktree = NULL;
2741 error = view_loop(view);
2742 done:
2743 free(in_repo_path);
2744 free(repo_path);
2745 free(cwd);
2746 free(start_id);
2747 free(head_ref_name);
2748 if (repo)
2749 got_repo_close(repo);
2750 if (worktree)
2751 got_worktree_close(worktree);
2752 got_ref_list_free(&refs);
2753 return error;
2756 __dead static void
2757 usage_diff(void)
2759 endwin();
2760 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2761 getprogname());
2762 exit(1);
2765 static char *
2766 parse_next_line(FILE *f, size_t *len)
2768 char *line;
2769 size_t linelen;
2770 size_t lineno;
2771 const char delim[3] = { '\0', '\0', '\0'};
2773 line = fparseln(f, &linelen, &lineno, delim, 0);
2774 if (len)
2775 *len = linelen;
2776 return line;
2779 static int
2780 match_line(const char *line, regex_t *regex, size_t nmatch,
2781 regmatch_t *regmatch)
2783 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2786 struct tog_color *
2787 match_color(struct tog_colors *colors, const char *line)
2789 struct tog_color *tc = NULL;
2791 SIMPLEQ_FOREACH(tc, colors, entry) {
2792 if (match_line(line, &tc->regex, 0, NULL))
2793 return tc;
2796 return NULL;
2799 static const struct got_error *
2800 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2801 WINDOW *window, regmatch_t *regmatch)
2803 const struct got_error *err = NULL;
2804 wchar_t *wline;
2805 int width;
2806 char *s;
2808 *wtotal = 0;
2810 s = strndup(line, regmatch->rm_so);
2811 if (s == NULL)
2812 return got_error_from_errno("strndup");
2814 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2815 if (err) {
2816 free(s);
2817 return err;
2819 waddwstr(window, wline);
2820 free(wline);
2821 free(s);
2822 wlimit -= width;
2823 *wtotal += width;
2825 if (wlimit > 0) {
2826 s = strndup(line + regmatch->rm_so,
2827 regmatch->rm_eo - regmatch->rm_so);
2828 if (s == NULL) {
2829 err = got_error_from_errno("strndup");
2830 free(s);
2831 return err;
2833 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2834 if (err) {
2835 free(s);
2836 return err;
2838 wattr_on(window, A_STANDOUT, NULL);
2839 waddwstr(window, wline);
2840 wattr_off(window, A_STANDOUT, NULL);
2841 free(wline);
2842 free(s);
2843 wlimit -= width;
2844 *wtotal += width;
2847 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2848 err = format_line(&wline, &width,
2849 line + regmatch->rm_eo, wlimit, col_tab_align);
2850 if (err)
2851 return err;
2852 waddwstr(window, wline);
2853 free(wline);
2854 *wtotal += width;
2857 return NULL;
2860 static const struct got_error *
2861 draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2862 off_t *line_offsets, int selected_line, int max_lines,
2863 int *last_displayed_line, int *eof, char *header,
2864 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
2866 const struct got_error *err;
2867 int nprinted = 0;
2868 char *line;
2869 struct tog_color *tc;
2870 size_t len;
2871 wchar_t *wline;
2872 int width;
2873 off_t line_offset;
2875 line_offset = line_offsets[first_displayed_line - 1];
2876 if (fseeko(f, line_offset, SEEK_SET) == -1)
2877 return got_error_from_errno("fseek");
2879 werase(view->window);
2881 if (header) {
2882 if (asprintf(&line, "[%d/%d] %s",
2883 first_displayed_line - 1 + selected_line, nlines,
2884 header) == -1)
2885 return got_error_from_errno("asprintf");
2886 err = format_line(&wline, &width, line, view->ncols, 0);
2887 free(line);
2888 if (err)
2889 return err;
2891 if (view_needs_focus_indication(view))
2892 wstandout(view->window);
2893 waddwstr(view->window, wline);
2894 free(wline);
2895 wline = NULL;
2896 if (view_needs_focus_indication(view))
2897 wstandend(view->window);
2898 if (width <= view->ncols - 1)
2899 waddch(view->window, '\n');
2901 if (max_lines <= 1)
2902 return NULL;
2903 max_lines--;
2906 *eof = 0;
2907 while (max_lines > 0 && nprinted < max_lines) {
2908 line = parse_next_line(f, &len);
2909 if (line == NULL) {
2910 *eof = 1;
2911 break;
2914 tc = match_color(colors, line);
2915 if (tc)
2916 wattr_on(view->window,
2917 COLOR_PAIR(tc->colorpair), NULL);
2918 if (first_displayed_line + nprinted == matched_line &&
2919 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2920 err = add_matched_line(&width, line, view->ncols, 0,
2921 view->window, regmatch);
2922 if (err) {
2923 free(line);
2924 return err;
2926 } else {
2927 err = format_line(&wline, &width, line, view->ncols, 0);
2928 if (err) {
2929 free(line);
2930 return err;
2932 waddwstr(view->window, wline);
2933 free(wline);
2934 wline = NULL;
2936 if (tc)
2937 wattr_off(view->window,
2938 COLOR_PAIR(tc->colorpair), NULL);
2939 if (width <= view->ncols - 1)
2940 waddch(view->window, '\n');
2941 nprinted++;
2942 free(line);
2944 if (nprinted >= 1)
2945 *last_displayed_line = first_displayed_line + (nprinted - 1);
2946 else
2947 *last_displayed_line = first_displayed_line;
2949 view_vborder(view);
2951 if (*eof) {
2952 while (nprinted < view->nlines) {
2953 waddch(view->window, '\n');
2954 nprinted++;
2957 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2958 if (err) {
2959 return err;
2962 wstandout(view->window);
2963 waddwstr(view->window, wline);
2964 free(wline);
2965 wline = NULL;
2966 wstandend(view->window);
2969 return NULL;
2972 static char *
2973 get_datestr(time_t *time, char *datebuf)
2975 struct tm mytm, *tm;
2976 char *p, *s;
2978 tm = gmtime_r(time, &mytm);
2979 if (tm == NULL)
2980 return NULL;
2981 s = asctime_r(tm, datebuf);
2982 if (s == NULL)
2983 return NULL;
2984 p = strchr(s, '\n');
2985 if (p)
2986 *p = '\0';
2987 return s;
2990 static const struct got_error *
2991 get_changed_paths(struct got_pathlist_head *paths,
2992 struct got_commit_object *commit, struct got_repository *repo)
2994 const struct got_error *err = NULL;
2995 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2996 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2997 struct got_object_qid *qid;
2999 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3000 if (qid != NULL) {
3001 struct got_commit_object *pcommit;
3002 err = got_object_open_as_commit(&pcommit, repo,
3003 qid->id);
3004 if (err)
3005 return err;
3007 tree_id1 = got_object_commit_get_tree_id(pcommit);
3008 got_object_commit_close(pcommit);
3012 if (tree_id1) {
3013 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3014 if (err)
3015 goto done;
3018 tree_id2 = got_object_commit_get_tree_id(commit);
3019 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3020 if (err)
3021 goto done;
3023 err = got_diff_tree(tree1, tree2, "", "", repo,
3024 got_diff_tree_collect_changed_paths, paths, 0);
3025 done:
3026 if (tree1)
3027 got_object_tree_close(tree1);
3028 if (tree2)
3029 got_object_tree_close(tree2);
3030 return err;
3033 static const struct got_error *
3034 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3036 off_t *p;
3038 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3039 if (p == NULL)
3040 return got_error_from_errno("reallocarray");
3041 *line_offsets = p;
3042 (*line_offsets)[*nlines] = off;
3043 (*nlines)++;
3044 return NULL;
3047 static const struct got_error *
3048 write_commit_info(off_t **line_offsets, size_t *nlines,
3049 struct got_object_id *commit_id, struct got_reflist_head *refs,
3050 struct got_repository *repo, FILE *outfile)
3052 const struct got_error *err = NULL;
3053 char datebuf[26], *datestr;
3054 struct got_commit_object *commit;
3055 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3056 time_t committer_time;
3057 const char *author, *committer;
3058 char *refs_str = NULL;
3059 struct got_pathlist_head changed_paths;
3060 struct got_pathlist_entry *pe;
3061 off_t outoff = 0;
3062 int n;
3064 TAILQ_INIT(&changed_paths);
3066 if (refs) {
3067 err = build_refs_str(&refs_str, refs, commit_id, repo);
3068 if (err)
3069 return err;
3072 err = got_object_open_as_commit(&commit, repo, commit_id);
3073 if (err)
3074 return err;
3076 err = got_object_id_str(&id_str, commit_id);
3077 if (err) {
3078 err = got_error_from_errno("got_object_id_str");
3079 goto done;
3082 err = add_line_offset(line_offsets, nlines, 0);
3083 if (err)
3084 goto done;
3086 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3087 refs_str ? refs_str : "", refs_str ? ")" : "");
3088 if (n < 0) {
3089 err = got_error_from_errno("fprintf");
3090 goto done;
3092 outoff += n;
3093 err = add_line_offset(line_offsets, nlines, outoff);
3094 if (err)
3095 goto done;
3097 n = fprintf(outfile, "from: %s\n",
3098 got_object_commit_get_author(commit));
3099 if (n < 0) {
3100 err = got_error_from_errno("fprintf");
3101 goto done;
3103 outoff += n;
3104 err = add_line_offset(line_offsets, nlines, outoff);
3105 if (err)
3106 goto done;
3108 committer_time = got_object_commit_get_committer_time(commit);
3109 datestr = get_datestr(&committer_time, datebuf);
3110 if (datestr) {
3111 n = fprintf(outfile, "date: %s UTC\n", datestr);
3112 if (n < 0) {
3113 err = got_error_from_errno("fprintf");
3114 goto done;
3116 outoff += n;
3117 err = add_line_offset(line_offsets, nlines, outoff);
3118 if (err)
3119 goto done;
3121 author = got_object_commit_get_author(commit);
3122 committer = got_object_commit_get_committer(commit);
3123 if (strcmp(author, committer) != 0) {
3124 n = fprintf(outfile, "via: %s\n", committer);
3125 if (n < 0) {
3126 err = got_error_from_errno("fprintf");
3127 goto done;
3129 outoff += n;
3130 err = add_line_offset(line_offsets, nlines, outoff);
3131 if (err)
3132 goto done;
3134 err = got_object_commit_get_logmsg(&logmsg, commit);
3135 if (err)
3136 goto done;
3137 s = logmsg;
3138 while ((line = strsep(&s, "\n")) != NULL) {
3139 n = fprintf(outfile, "%s\n", line);
3140 if (n < 0) {
3141 err = got_error_from_errno("fprintf");
3142 goto done;
3144 outoff += n;
3145 err = add_line_offset(line_offsets, nlines, outoff);
3146 if (err)
3147 goto done;
3150 err = get_changed_paths(&changed_paths, commit, repo);
3151 if (err)
3152 goto done;
3153 TAILQ_FOREACH(pe, &changed_paths, entry) {
3154 struct got_diff_changed_path *cp = pe->data;
3155 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3156 if (n < 0) {
3157 err = got_error_from_errno("fprintf");
3158 goto done;
3160 outoff += n;
3161 err = add_line_offset(line_offsets, nlines, outoff);
3162 if (err)
3163 goto done;
3164 free((char *)pe->path);
3165 free(pe->data);
3168 fputc('\n', outfile);
3169 outoff++;
3170 err = add_line_offset(line_offsets, nlines, outoff);
3171 done:
3172 got_pathlist_free(&changed_paths);
3173 free(id_str);
3174 free(logmsg);
3175 free(refs_str);
3176 got_object_commit_close(commit);
3177 if (err) {
3178 free(*line_offsets);
3179 *line_offsets = NULL;
3180 *nlines = 0;
3182 return err;
3185 static const struct got_error *
3186 create_diff(struct tog_diff_view_state *s)
3188 const struct got_error *err = NULL;
3189 FILE *f = NULL;
3190 int obj_type;
3192 free(s->line_offsets);
3193 s->line_offsets = malloc(sizeof(off_t));
3194 if (s->line_offsets == NULL)
3195 return got_error_from_errno("malloc");
3196 s->nlines = 0;
3198 f = got_opentemp();
3199 if (f == NULL) {
3200 err = got_error_from_errno("got_opentemp");
3201 goto done;
3203 if (s->f && fclose(s->f) != 0) {
3204 err = got_error_from_errno("fclose");
3205 goto done;
3207 s->f = f;
3209 if (s->id1)
3210 err = got_object_get_type(&obj_type, s->repo, s->id1);
3211 else
3212 err = got_object_get_type(&obj_type, s->repo, s->id2);
3213 if (err)
3214 goto done;
3216 switch (obj_type) {
3217 case GOT_OBJ_TYPE_BLOB:
3218 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3219 s->id1, s->id2, NULL, NULL, s->diff_context, 0,
3220 s->repo, s->f);
3221 break;
3222 case GOT_OBJ_TYPE_TREE:
3223 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3224 s->id1, s->id2, "", "", s->diff_context, 0, s->repo, s->f);
3225 break;
3226 case GOT_OBJ_TYPE_COMMIT: {
3227 const struct got_object_id_queue *parent_ids;
3228 struct got_object_qid *pid;
3229 struct got_commit_object *commit2;
3231 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3232 if (err)
3233 goto done;
3234 /* Show commit info if we're diffing to a parent/root commit. */
3235 if (s->id1 == NULL) {
3236 err = write_commit_info(&s->line_offsets, &s->nlines,
3237 s->id2, s->refs, s->repo, s->f);
3238 if (err)
3239 goto done;
3240 } else {
3241 parent_ids = got_object_commit_get_parent_ids(commit2);
3242 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3243 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3244 err = write_commit_info(
3245 &s->line_offsets, &s->nlines,
3246 s->id2, s->refs, s->repo, s->f);
3247 if (err)
3248 goto done;
3249 break;
3253 got_object_commit_close(commit2);
3255 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3256 s->id1, s->id2, s->diff_context, 0, s->repo, s->f);
3257 break;
3259 default:
3260 err = got_error(GOT_ERR_OBJ_TYPE);
3261 break;
3263 if (err)
3264 goto done;
3265 done:
3266 if (s->f && fflush(s->f) != 0 && err == NULL)
3267 err = got_error_from_errno("fflush");
3268 return err;
3271 static void
3272 diff_view_indicate_progress(struct tog_view *view)
3274 mvwaddstr(view->window, 0, 0, "diffing...");
3275 update_panels();
3276 doupdate();
3279 static const struct got_error *
3280 search_start_diff_view(struct tog_view *view)
3282 struct tog_diff_view_state *s = &view->state.diff;
3284 s->matched_line = 0;
3285 return NULL;
3288 static const struct got_error *
3289 search_next_diff_view(struct tog_view *view)
3291 struct tog_diff_view_state *s = &view->state.diff;
3292 int lineno;
3294 if (!view->searching) {
3295 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3296 return NULL;
3299 if (s->matched_line) {
3300 if (view->searching == TOG_SEARCH_FORWARD)
3301 lineno = s->matched_line + 1;
3302 else
3303 lineno = s->matched_line - 1;
3304 } else {
3305 if (view->searching == TOG_SEARCH_FORWARD)
3306 lineno = 1;
3307 else
3308 lineno = s->nlines;
3311 while (1) {
3312 char *line = NULL;
3313 off_t offset;
3314 size_t len;
3316 if (lineno <= 0 || lineno > s->nlines) {
3317 if (s->matched_line == 0) {
3318 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3319 free(line);
3320 break;
3323 if (view->searching == TOG_SEARCH_FORWARD)
3324 lineno = 1;
3325 else
3326 lineno = s->nlines;
3329 offset = s->line_offsets[lineno - 1];
3330 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3331 free(line);
3332 return got_error_from_errno("fseeko");
3334 free(line);
3335 line = parse_next_line(s->f, &len);
3336 if (line &&
3337 match_line(line, &view->regex, 1, &view->regmatch)) {
3338 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3339 s->matched_line = lineno;
3340 free(line);
3341 break;
3343 free(line);
3344 if (view->searching == TOG_SEARCH_FORWARD)
3345 lineno++;
3346 else
3347 lineno--;
3350 if (s->matched_line) {
3351 s->first_displayed_line = s->matched_line;
3352 s->selected_line = 1;
3355 return NULL;
3358 static const struct got_error *
3359 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3360 struct got_object_id *id2, struct tog_view *log_view,
3361 struct got_reflist_head *refs, struct got_repository *repo)
3363 const struct got_error *err;
3364 struct tog_diff_view_state *s = &view->state.diff;
3366 if (id1 != NULL && id2 != NULL) {
3367 int type1, type2;
3368 err = got_object_get_type(&type1, repo, id1);
3369 if (err)
3370 return err;
3371 err = got_object_get_type(&type2, repo, id2);
3372 if (err)
3373 return err;
3375 if (type1 != type2)
3376 return got_error(GOT_ERR_OBJ_TYPE);
3378 s->first_displayed_line = 1;
3379 s->last_displayed_line = view->nlines;
3380 s->selected_line = 1;
3381 s->repo = repo;
3382 s->refs = refs;
3383 s->id1 = id1;
3384 s->id2 = id2;
3386 if (id1) {
3387 s->id1 = got_object_id_dup(id1);
3388 if (s->id1 == NULL)
3389 return got_error_from_errno("got_object_id_dup");
3390 } else
3391 s->id1 = NULL;
3393 s->id2 = got_object_id_dup(id2);
3394 if (s->id2 == NULL) {
3395 free(s->id1);
3396 s->id1 = NULL;
3397 return got_error_from_errno("got_object_id_dup");
3399 s->f = NULL;
3400 s->first_displayed_line = 1;
3401 s->last_displayed_line = view->nlines;
3402 s->diff_context = 3;
3403 s->log_view = log_view;
3404 s->repo = repo;
3405 s->refs = refs;
3407 SIMPLEQ_INIT(&s->colors);
3408 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3409 err = add_color(&s->colors,
3410 "^-", TOG_COLOR_DIFF_MINUS,
3411 get_color_value("TOG_COLOR_DIFF_MINUS"));
3412 if (err)
3413 return err;
3414 err = add_color(&s->colors, "^\\+",
3415 TOG_COLOR_DIFF_PLUS,
3416 get_color_value("TOG_COLOR_DIFF_PLUS"));
3417 if (err) {
3418 free_colors(&s->colors);
3419 return err;
3421 err = add_color(&s->colors,
3422 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3423 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3424 if (err) {
3425 free_colors(&s->colors);
3426 return err;
3429 err = add_color(&s->colors,
3430 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3431 TOG_COLOR_DIFF_META,
3432 get_color_value("TOG_COLOR_DIFF_META"));
3433 if (err) {
3434 free_colors(&s->colors);
3435 return err;
3438 err = add_color(&s->colors,
3439 "^(from|via): ", TOG_COLOR_AUTHOR,
3440 get_color_value("TOG_COLOR_AUTHOR"));
3441 if (err) {
3442 free_colors(&s->colors);
3443 return err;
3446 err = add_color(&s->colors,
3447 "^date: ", TOG_COLOR_DATE,
3448 get_color_value("TOG_COLOR_DATE"));
3449 if (err) {
3450 free_colors(&s->colors);
3451 return err;
3455 if (log_view && view_is_splitscreen(view))
3456 show_log_view(log_view); /* draw vborder */
3457 diff_view_indicate_progress(view);
3459 s->line_offsets = NULL;
3460 s->nlines = 0;
3461 err = create_diff(s);
3462 if (err) {
3463 free(s->id1);
3464 s->id1 = NULL;
3465 free(s->id2);
3466 s->id2 = NULL;
3467 return err;
3470 view->show = show_diff_view;
3471 view->input = input_diff_view;
3472 view->close = close_diff_view;
3473 view->search_start = search_start_diff_view;
3474 view->search_next = search_next_diff_view;
3476 return NULL;
3479 static const struct got_error *
3480 close_diff_view(struct tog_view *view)
3482 const struct got_error *err = NULL;
3483 struct tog_diff_view_state *s = &view->state.diff;
3485 free(s->id1);
3486 s->id1 = NULL;
3487 free(s->id2);
3488 s->id2 = NULL;
3489 if (s->f && fclose(s->f) == EOF)
3490 err = got_error_from_errno("fclose");
3491 free_colors(&s->colors);
3492 free(s->line_offsets);
3493 s->line_offsets = NULL;
3494 s->nlines = 0;
3495 return err;
3498 static const struct got_error *
3499 show_diff_view(struct tog_view *view)
3501 const struct got_error *err;
3502 struct tog_diff_view_state *s = &view->state.diff;
3503 char *id_str1 = NULL, *id_str2, *header;
3505 if (s->id1) {
3506 err = got_object_id_str(&id_str1, s->id1);
3507 if (err)
3508 return err;
3510 err = got_object_id_str(&id_str2, s->id2);
3511 if (err)
3512 return err;
3514 if (asprintf(&header, "diff %s %s",
3515 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3516 err = got_error_from_errno("asprintf");
3517 free(id_str1);
3518 free(id_str2);
3519 return err;
3521 free(id_str1);
3522 free(id_str2);
3524 return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3525 s->line_offsets, s->selected_line, view->nlines,
3526 &s->last_displayed_line, &s->eof, header, &s->colors,
3527 s->matched_line, &view->regmatch);
3530 static const struct got_error *
3531 set_selected_commit(struct tog_diff_view_state *s,
3532 struct commit_queue_entry *entry)
3534 const struct got_error *err;
3535 const struct got_object_id_queue *parent_ids;
3536 struct got_commit_object *selected_commit;
3537 struct got_object_qid *pid;
3539 free(s->id2);
3540 s->id2 = got_object_id_dup(entry->id);
3541 if (s->id2 == NULL)
3542 return got_error_from_errno("got_object_id_dup");
3544 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3545 if (err)
3546 return err;
3547 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3548 free(s->id1);
3549 pid = SIMPLEQ_FIRST(parent_ids);
3550 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3551 got_object_commit_close(selected_commit);
3552 return NULL;
3555 static const struct got_error *
3556 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3557 struct tog_view **focus_view, struct tog_view *view, int ch)
3559 const struct got_error *err = NULL;
3560 struct tog_diff_view_state *s = &view->state.diff;
3561 struct tog_log_view_state *ls;
3562 struct commit_queue_entry *entry;
3563 int i;
3565 switch (ch) {
3566 case 'k':
3567 case KEY_UP:
3568 if (s->first_displayed_line > 1)
3569 s->first_displayed_line--;
3570 break;
3571 case KEY_PPAGE:
3572 case CTRL('b'):
3573 if (s->first_displayed_line == 1)
3574 break;
3575 i = 0;
3576 while (i++ < view->nlines - 1 &&
3577 s->first_displayed_line > 1)
3578 s->first_displayed_line--;
3579 break;
3580 case 'j':
3581 case KEY_DOWN:
3582 if (!s->eof)
3583 s->first_displayed_line++;
3584 break;
3585 case KEY_NPAGE:
3586 case CTRL('f'):
3587 case ' ':
3588 if (s->eof)
3589 break;
3590 i = 0;
3591 while (!s->eof && i++ < view->nlines - 1) {
3592 char *line;
3593 line = parse_next_line(s->f, NULL);
3594 s->first_displayed_line++;
3595 if (line == NULL)
3596 break;
3598 break;
3599 case '[':
3600 if (s->diff_context > 0) {
3601 s->diff_context--;
3602 diff_view_indicate_progress(view);
3603 err = create_diff(s);
3605 break;
3606 case ']':
3607 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3608 s->diff_context++;
3609 diff_view_indicate_progress(view);
3610 err = create_diff(s);
3612 break;
3613 case '<':
3614 case ',':
3615 if (s->log_view == NULL)
3616 break;
3617 ls = &s->log_view->state.log;
3618 entry = TAILQ_PREV(ls->selected_entry,
3619 commit_queue_head, entry);
3620 if (entry == NULL)
3621 break;
3623 err = input_log_view(NULL, NULL, NULL, s->log_view,
3624 KEY_UP);
3625 if (err)
3626 break;
3628 err = set_selected_commit(s, entry);
3629 if (err)
3630 break;
3632 s->first_displayed_line = 1;
3633 s->last_displayed_line = view->nlines;
3635 diff_view_indicate_progress(view);
3636 err = create_diff(s);
3637 break;
3638 case '>':
3639 case '.':
3640 if (s->log_view == NULL)
3641 break;
3642 ls = &s->log_view->state.log;
3644 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3645 ls->thread_args.commits_needed++;
3646 err = trigger_log_thread(s->log_view, 1,
3647 &ls->thread_args.commits_needed,
3648 &ls->thread_args.log_complete,
3649 &ls->thread_args.need_commits,
3650 &ls->thread_args.commit_loaded);
3651 if (err)
3652 break;
3654 err = input_log_view(NULL, NULL, NULL, s->log_view,
3655 KEY_DOWN);
3656 if (err)
3657 break;
3659 entry = TAILQ_NEXT(ls->selected_entry, entry);
3660 if (entry == NULL)
3661 break;
3663 err = set_selected_commit(s, entry);
3664 if (err)
3665 break;
3667 s->first_displayed_line = 1;
3668 s->last_displayed_line = view->nlines;
3670 diff_view_indicate_progress(view);
3671 err = create_diff(s);
3672 break;
3673 default:
3674 break;
3677 return err;
3680 static const struct got_error *
3681 cmd_diff(int argc, char *argv[])
3683 const struct got_error *error = NULL;
3684 struct got_repository *repo = NULL;
3685 struct got_worktree *worktree = NULL;
3686 struct got_reflist_head refs;
3687 struct got_object_id *id1 = NULL, *id2 = NULL;
3688 char *repo_path = NULL, *cwd = NULL;
3689 char *id_str1 = NULL, *id_str2 = NULL;
3690 int ch;
3691 struct tog_view *view;
3693 SIMPLEQ_INIT(&refs);
3695 #ifndef PROFILE
3696 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3697 NULL) == -1)
3698 err(1, "pledge");
3699 #endif
3701 while ((ch = getopt(argc, argv, "r:")) != -1) {
3702 switch (ch) {
3703 case 'r':
3704 repo_path = realpath(optarg, NULL);
3705 if (repo_path == NULL)
3706 return got_error_from_errno2("realpath",
3707 optarg);
3708 break;
3709 default:
3710 usage_diff();
3711 /* NOTREACHED */
3715 argc -= optind;
3716 argv += optind;
3718 if (argc == 0) {
3719 usage_diff(); /* TODO show local worktree changes */
3720 } else if (argc == 2) {
3721 id_str1 = argv[0];
3722 id_str2 = argv[1];
3723 } else
3724 usage_diff();
3726 cwd = getcwd(NULL, 0);
3727 if (cwd == NULL)
3728 return got_error_from_errno("getcwd");
3730 error = got_worktree_open(&worktree, cwd);
3731 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3732 goto done;
3734 if (repo_path == NULL) {
3735 if (worktree)
3736 repo_path =
3737 strdup(got_worktree_get_repo_path(worktree));
3738 else
3739 repo_path = strdup(cwd);
3741 if (repo_path == NULL) {
3742 error = got_error_from_errno("strdup");
3743 goto done;
3746 error = got_repo_open(&repo, repo_path, NULL);
3747 if (error)
3748 goto done;
3750 init_curses();
3752 error = apply_unveil(got_repo_get_path(repo), NULL);
3753 if (error)
3754 goto done;
3756 error = got_repo_match_object_id_prefix(&id1, id_str1,
3757 GOT_OBJ_TYPE_ANY, repo);
3758 if (error)
3759 goto done;
3761 error = got_repo_match_object_id_prefix(&id2, id_str2,
3762 GOT_OBJ_TYPE_ANY, repo);
3763 if (error)
3764 goto done;
3766 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3767 if (error)
3768 goto done;
3770 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3771 if (view == NULL) {
3772 error = got_error_from_errno("view_open");
3773 goto done;
3775 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3776 if (error)
3777 goto done;
3778 error = view_loop(view);
3779 done:
3780 free(repo_path);
3781 free(cwd);
3782 if (repo)
3783 got_repo_close(repo);
3784 if (worktree)
3785 got_worktree_close(worktree);
3786 got_ref_list_free(&refs);
3787 return error;
3790 __dead static void
3791 usage_blame(void)
3793 endwin();
3794 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3795 getprogname());
3796 exit(1);
3799 struct tog_blame_line {
3800 int annotated;
3801 struct got_object_id *id;
3804 static const struct got_error *
3805 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3806 const char *path, struct tog_blame_line *lines, int nlines,
3807 int blame_complete, int selected_line, int *first_displayed_line,
3808 int *last_displayed_line, int *eof, int max_lines,
3809 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
3811 const struct got_error *err;
3812 int lineno = 0, nprinted = 0;
3813 char *line;
3814 size_t len;
3815 wchar_t *wline;
3816 int width;
3817 struct tog_blame_line *blame_line;
3818 struct got_object_id *prev_id = NULL;
3819 char *id_str;
3820 struct tog_color *tc;
3822 err = got_object_id_str(&id_str, id);
3823 if (err)
3824 return err;
3826 rewind(f);
3827 werase(view->window);
3829 if (asprintf(&line, "commit %s", id_str) == -1) {
3830 err = got_error_from_errno("asprintf");
3831 free(id_str);
3832 return err;
3835 err = format_line(&wline, &width, line, view->ncols, 0);
3836 free(line);
3837 line = NULL;
3838 if (err)
3839 return err;
3840 if (view_needs_focus_indication(view))
3841 wstandout(view->window);
3842 tc = get_color(colors, TOG_COLOR_COMMIT);
3843 if (tc)
3844 wattr_on(view->window,
3845 COLOR_PAIR(tc->colorpair), NULL);
3846 waddwstr(view->window, wline);
3847 if (tc)
3848 wattr_off(view->window,
3849 COLOR_PAIR(tc->colorpair), NULL);
3850 if (view_needs_focus_indication(view))
3851 wstandend(view->window);
3852 free(wline);
3853 wline = NULL;
3854 if (width < view->ncols - 1)
3855 waddch(view->window, '\n');
3857 if (asprintf(&line, "[%d/%d] %s%s",
3858 *first_displayed_line - 1 + selected_line, nlines,
3859 blame_complete ? "" : "annotating... ", path) == -1) {
3860 free(id_str);
3861 return got_error_from_errno("asprintf");
3863 free(id_str);
3864 err = format_line(&wline, &width, line, view->ncols, 0);
3865 free(line);
3866 line = NULL;
3867 if (err)
3868 return err;
3869 waddwstr(view->window, wline);
3870 free(wline);
3871 wline = NULL;
3872 if (width < view->ncols - 1)
3873 waddch(view->window, '\n');
3875 *eof = 0;
3876 while (nprinted < max_lines - 2) {
3877 line = parse_next_line(f, &len);
3878 if (line == NULL) {
3879 *eof = 1;
3880 break;
3882 if (++lineno < *first_displayed_line) {
3883 free(line);
3884 continue;
3887 if (view->focussed && nprinted == selected_line - 1)
3888 wstandout(view->window);
3890 if (nlines > 0) {
3891 blame_line = &lines[lineno - 1];
3892 if (blame_line->annotated && prev_id &&
3893 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3894 !(view->focussed &&
3895 nprinted == selected_line - 1)) {
3896 waddstr(view->window, " ");
3897 } else if (blame_line->annotated) {
3898 char *id_str;
3899 err = got_object_id_str(&id_str, blame_line->id);
3900 if (err) {
3901 free(line);
3902 return err;
3904 tc = get_color(colors, TOG_COLOR_COMMIT);
3905 if (tc)
3906 wattr_on(view->window,
3907 COLOR_PAIR(tc->colorpair), NULL);
3908 wprintw(view->window, "%.8s", id_str);
3909 if (tc)
3910 wattr_off(view->window,
3911 COLOR_PAIR(tc->colorpair), NULL);
3912 free(id_str);
3913 prev_id = blame_line->id;
3914 } else {
3915 waddstr(view->window, "........");
3916 prev_id = NULL;
3918 } else {
3919 waddstr(view->window, "........");
3920 prev_id = NULL;
3923 if (view->focussed && nprinted == selected_line - 1)
3924 wstandend(view->window);
3925 waddstr(view->window, " ");
3927 if (view->ncols <= 9) {
3928 width = 9;
3929 wline = wcsdup(L"");
3930 if (wline == NULL) {
3931 err = got_error_from_errno("wcsdup");
3932 free(line);
3933 return err;
3935 } else if (*first_displayed_line + nprinted == matched_line &&
3936 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3937 err = add_matched_line(&width, line, view->ncols - 9, 9,
3938 view->window, regmatch);
3939 if (err) {
3940 free(line);
3941 return err;
3943 width += 9;
3944 } else {
3945 err = format_line(&wline, &width, line,
3946 view->ncols - 9, 9);
3947 waddwstr(view->window, wline);
3948 free(wline);
3949 wline = NULL;
3950 width += 9;
3953 if (width <= view->ncols - 1)
3954 waddch(view->window, '\n');
3955 if (++nprinted == 1)
3956 *first_displayed_line = lineno;
3957 free(line);
3959 *last_displayed_line = lineno;
3961 view_vborder(view);
3963 return NULL;
3966 static const struct got_error *
3967 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3969 const struct got_error *err = NULL;
3970 struct tog_blame_cb_args *a = arg;
3971 struct tog_blame_line *line;
3972 int errcode;
3974 if (nlines != a->nlines ||
3975 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3976 return got_error(GOT_ERR_RANGE);
3978 errcode = pthread_mutex_lock(&tog_mutex);
3979 if (errcode)
3980 return got_error_set_errno(errcode, "pthread_mutex_lock");
3982 if (*a->quit) { /* user has quit the blame view */
3983 err = got_error(GOT_ERR_ITER_COMPLETED);
3984 goto done;
3987 if (lineno == -1)
3988 goto done; /* no change in this commit */
3990 line = &a->lines[lineno - 1];
3991 if (line->annotated)
3992 goto done;
3994 line->id = got_object_id_dup(id);
3995 if (line->id == NULL) {
3996 err = got_error_from_errno("got_object_id_dup");
3997 goto done;
3999 line->annotated = 1;
4000 done:
4001 errcode = pthread_mutex_unlock(&tog_mutex);
4002 if (errcode)
4003 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4004 return err;
4007 static void *
4008 blame_thread(void *arg)
4010 const struct got_error *err;
4011 struct tog_blame_thread_args *ta = arg;
4012 struct tog_blame_cb_args *a = ta->cb_args;
4013 int errcode;
4015 err = block_signals_used_by_main_thread();
4016 if (err)
4017 return (void *)err;
4019 err = got_blame(ta->path, a->commit_id, ta->repo,
4020 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4021 if (err && err->code == GOT_ERR_CANCELLED)
4022 err = NULL;
4024 errcode = pthread_mutex_lock(&tog_mutex);
4025 if (errcode)
4026 return (void *)got_error_set_errno(errcode,
4027 "pthread_mutex_lock");
4029 got_repo_close(ta->repo);
4030 ta->repo = NULL;
4031 *ta->complete = 1;
4033 errcode = pthread_mutex_unlock(&tog_mutex);
4034 if (errcode && err == NULL)
4035 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4037 return (void *)err;
4040 static struct got_object_id *
4041 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4042 int first_displayed_line, int selected_line)
4044 struct tog_blame_line *line;
4046 if (nlines <= 0)
4047 return NULL;
4049 line = &lines[first_displayed_line - 1 + selected_line - 1];
4050 if (!line->annotated)
4051 return NULL;
4053 return line->id;
4056 static const struct got_error *
4057 stop_blame(struct tog_blame *blame)
4059 const struct got_error *err = NULL;
4060 int i;
4062 if (blame->thread) {
4063 int errcode;
4064 errcode = pthread_mutex_unlock(&tog_mutex);
4065 if (errcode)
4066 return got_error_set_errno(errcode,
4067 "pthread_mutex_unlock");
4068 errcode = pthread_join(blame->thread, (void **)&err);
4069 if (errcode)
4070 return got_error_set_errno(errcode, "pthread_join");
4071 errcode = pthread_mutex_lock(&tog_mutex);
4072 if (errcode)
4073 return got_error_set_errno(errcode,
4074 "pthread_mutex_lock");
4075 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4076 err = NULL;
4077 blame->thread = NULL;
4079 if (blame->thread_args.repo) {
4080 got_repo_close(blame->thread_args.repo);
4081 blame->thread_args.repo = NULL;
4083 if (blame->f) {
4084 if (fclose(blame->f) != 0 && err == NULL)
4085 err = got_error_from_errno("fclose");
4086 blame->f = NULL;
4088 if (blame->lines) {
4089 for (i = 0; i < blame->nlines; i++)
4090 free(blame->lines[i].id);
4091 free(blame->lines);
4092 blame->lines = NULL;
4094 free(blame->cb_args.commit_id);
4095 blame->cb_args.commit_id = NULL;
4097 return err;
4100 static const struct got_error *
4101 cancel_blame_view(void *arg)
4103 const struct got_error *err = NULL;
4104 int *done = arg;
4105 int errcode;
4107 errcode = pthread_mutex_lock(&tog_mutex);
4108 if (errcode)
4109 return got_error_set_errno(errcode,
4110 "pthread_mutex_unlock");
4112 if (*done)
4113 err = got_error(GOT_ERR_CANCELLED);
4115 errcode = pthread_mutex_unlock(&tog_mutex);
4116 if (errcode)
4117 return got_error_set_errno(errcode,
4118 "pthread_mutex_lock");
4120 return err;
4123 static const struct got_error *
4124 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
4125 int *first_displayed_line, int *last_displayed_line, int *selected_line,
4126 int *done, int *eof, const char *path, struct got_object_id *commit_id,
4127 struct got_repository *repo)
4129 const struct got_error *err = NULL;
4130 struct got_blob_object *blob = NULL;
4131 struct got_repository *thread_repo = NULL;
4132 struct got_object_id *obj_id = NULL;
4133 int obj_type;
4135 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
4136 if (err)
4137 return err;
4139 err = got_object_get_type(&obj_type, repo, obj_id);
4140 if (err)
4141 goto done;
4143 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4144 err = got_error(GOT_ERR_OBJ_TYPE);
4145 goto done;
4148 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4149 if (err)
4150 goto done;
4151 blame->f = got_opentemp();
4152 if (blame->f == NULL) {
4153 err = got_error_from_errno("got_opentemp");
4154 goto done;
4156 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4157 &blame->line_offsets, blame->f, blob);
4158 if (err || blame->nlines == 0)
4159 goto done;
4161 /* Don't include \n at EOF in the blame line count. */
4162 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4163 blame->nlines--;
4165 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4166 if (blame->lines == NULL) {
4167 err = got_error_from_errno("calloc");
4168 goto done;
4171 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4172 if (err)
4173 goto done;
4175 blame->cb_args.view = view;
4176 blame->cb_args.lines = blame->lines;
4177 blame->cb_args.nlines = blame->nlines;
4178 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4179 if (blame->cb_args.commit_id == NULL) {
4180 err = got_error_from_errno("got_object_id_dup");
4181 goto done;
4183 blame->cb_args.quit = done;
4185 blame->thread_args.path = path;
4186 blame->thread_args.repo = thread_repo;
4187 blame->thread_args.cb_args = &blame->cb_args;
4188 blame->thread_args.complete = blame_complete;
4189 blame->thread_args.cancel_cb = cancel_blame_view;
4190 blame->thread_args.cancel_arg = done;
4191 *blame_complete = 0;
4193 done:
4194 if (blob)
4195 got_object_blob_close(blob);
4196 free(obj_id);
4197 if (err)
4198 stop_blame(blame);
4199 return err;
4202 static const struct got_error *
4203 open_blame_view(struct tog_view *view, char *path,
4204 struct got_object_id *commit_id, struct got_reflist_head *refs,
4205 struct got_repository *repo)
4207 const struct got_error *err = NULL;
4208 struct tog_blame_view_state *s = &view->state.blame;
4210 SIMPLEQ_INIT(&s->blamed_commits);
4212 s->path = strdup(path);
4213 if (s->path == NULL)
4214 return got_error_from_errno("strdup");
4216 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4217 if (err) {
4218 free(s->path);
4219 return err;
4222 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4223 s->first_displayed_line = 1;
4224 s->last_displayed_line = view->nlines;
4225 s->selected_line = 1;
4226 s->blame_complete = 0;
4227 s->repo = repo;
4228 s->refs = refs;
4229 s->commit_id = commit_id;
4230 memset(&s->blame, 0, sizeof(s->blame));
4232 SIMPLEQ_INIT(&s->colors);
4233 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4234 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4235 get_color_value("TOG_COLOR_COMMIT"));
4236 if (err)
4237 return err;
4240 view->show = show_blame_view;
4241 view->input = input_blame_view;
4242 view->close = close_blame_view;
4243 view->search_start = search_start_blame_view;
4244 view->search_next = search_next_blame_view;
4246 return run_blame(&s->blame, view, &s->blame_complete,
4247 &s->first_displayed_line, &s->last_displayed_line,
4248 &s->selected_line, &s->done, &s->eof, s->path,
4249 s->blamed_commit->id, s->repo);
4252 static const struct got_error *
4253 close_blame_view(struct tog_view *view)
4255 const struct got_error *err = NULL;
4256 struct tog_blame_view_state *s = &view->state.blame;
4258 if (s->blame.thread)
4259 err = stop_blame(&s->blame);
4261 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4262 struct got_object_qid *blamed_commit;
4263 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4264 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4265 got_object_qid_free(blamed_commit);
4268 free(s->path);
4269 free_colors(&s->colors);
4271 return err;
4274 static const struct got_error *
4275 search_start_blame_view(struct tog_view *view)
4277 struct tog_blame_view_state *s = &view->state.blame;
4279 s->matched_line = 0;
4280 return NULL;
4283 static const struct got_error *
4284 search_next_blame_view(struct tog_view *view)
4286 struct tog_blame_view_state *s = &view->state.blame;
4287 int lineno;
4289 if (!view->searching) {
4290 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4291 return NULL;
4294 if (s->matched_line) {
4295 if (view->searching == TOG_SEARCH_FORWARD)
4296 lineno = s->matched_line + 1;
4297 else
4298 lineno = s->matched_line - 1;
4299 } else {
4300 if (view->searching == TOG_SEARCH_FORWARD)
4301 lineno = 1;
4302 else
4303 lineno = s->blame.nlines;
4306 while (1) {
4307 char *line = NULL;
4308 off_t offset;
4309 size_t len;
4311 if (lineno <= 0 || lineno > s->blame.nlines) {
4312 if (s->matched_line == 0) {
4313 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4314 free(line);
4315 break;
4318 if (view->searching == TOG_SEARCH_FORWARD)
4319 lineno = 1;
4320 else
4321 lineno = s->blame.nlines;
4324 offset = s->blame.line_offsets[lineno - 1];
4325 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4326 free(line);
4327 return got_error_from_errno("fseeko");
4329 free(line);
4330 line = parse_next_line(s->blame.f, &len);
4331 if (line &&
4332 match_line(line, &view->regex, 1, &view->regmatch)) {
4333 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4334 s->matched_line = lineno;
4335 free(line);
4336 break;
4338 free(line);
4339 if (view->searching == TOG_SEARCH_FORWARD)
4340 lineno++;
4341 else
4342 lineno--;
4345 if (s->matched_line) {
4346 s->first_displayed_line = s->matched_line;
4347 s->selected_line = 1;
4350 return NULL;
4353 static const struct got_error *
4354 show_blame_view(struct tog_view *view)
4356 const struct got_error *err = NULL;
4357 struct tog_blame_view_state *s = &view->state.blame;
4358 int errcode;
4360 if (s->blame.thread == NULL) {
4361 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4362 &s->blame.thread_args);
4363 if (errcode)
4364 return got_error_set_errno(errcode, "pthread_create");
4366 halfdelay(1); /* fast refresh while annotating */
4369 if (s->blame_complete)
4370 halfdelay(10); /* disable fast refresh */
4372 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4373 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4374 s->selected_line, &s->first_displayed_line,
4375 &s->last_displayed_line, &s->eof, view->nlines, &s->colors,
4376 s->matched_line, &view->regmatch);
4378 view_vborder(view);
4379 return err;
4382 static const struct got_error *
4383 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4384 struct tog_view **focus_view, struct tog_view *view, int ch)
4386 const struct got_error *err = NULL, *thread_err = NULL;
4387 struct tog_view *diff_view;
4388 struct tog_blame_view_state *s = &view->state.blame;
4389 int begin_x = 0;
4391 switch (ch) {
4392 case 'q':
4393 s->done = 1;
4394 break;
4395 case 'k':
4396 case KEY_UP:
4397 if (s->selected_line > 1)
4398 s->selected_line--;
4399 else if (s->selected_line == 1 &&
4400 s->first_displayed_line > 1)
4401 s->first_displayed_line--;
4402 break;
4403 case KEY_PPAGE:
4404 case CTRL('b'):
4405 if (s->first_displayed_line == 1) {
4406 s->selected_line = 1;
4407 break;
4409 if (s->first_displayed_line > view->nlines - 2)
4410 s->first_displayed_line -=
4411 (view->nlines - 2);
4412 else
4413 s->first_displayed_line = 1;
4414 break;
4415 case 'j':
4416 case KEY_DOWN:
4417 if (s->selected_line < view->nlines - 2 &&
4418 s->first_displayed_line +
4419 s->selected_line <= s->blame.nlines)
4420 s->selected_line++;
4421 else if (s->last_displayed_line <
4422 s->blame.nlines)
4423 s->first_displayed_line++;
4424 break;
4425 case 'b':
4426 case 'p': {
4427 struct got_object_id *id = NULL;
4428 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4429 s->first_displayed_line, s->selected_line);
4430 if (id == NULL)
4431 break;
4432 if (ch == 'p') {
4433 struct got_commit_object *commit;
4434 struct got_object_qid *pid;
4435 struct got_object_id *blob_id = NULL;
4436 int obj_type;
4437 err = got_object_open_as_commit(&commit,
4438 s->repo, id);
4439 if (err)
4440 break;
4441 pid = SIMPLEQ_FIRST(
4442 got_object_commit_get_parent_ids(commit));
4443 if (pid == NULL) {
4444 got_object_commit_close(commit);
4445 break;
4447 /* Check if path history ends here. */
4448 err = got_object_id_by_path(&blob_id, s->repo,
4449 pid->id, s->path);
4450 if (err) {
4451 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4452 err = NULL;
4453 got_object_commit_close(commit);
4454 break;
4456 err = got_object_get_type(&obj_type, s->repo,
4457 blob_id);
4458 free(blob_id);
4459 /* Can't blame non-blob type objects. */
4460 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4461 got_object_commit_close(commit);
4462 break;
4464 err = got_object_qid_alloc(&s->blamed_commit,
4465 pid->id);
4466 got_object_commit_close(commit);
4467 } else {
4468 if (got_object_id_cmp(id,
4469 s->blamed_commit->id) == 0)
4470 break;
4471 err = got_object_qid_alloc(&s->blamed_commit,
4472 id);
4474 if (err)
4475 break;
4476 s->done = 1;
4477 thread_err = stop_blame(&s->blame);
4478 s->done = 0;
4479 if (thread_err)
4480 break;
4481 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4482 s->blamed_commit, entry);
4483 err = run_blame(&s->blame, view, &s->blame_complete,
4484 &s->first_displayed_line, &s->last_displayed_line,
4485 &s->selected_line, &s->done, &s->eof,
4486 s->path, s->blamed_commit->id, s->repo);
4487 if (err)
4488 break;
4489 break;
4491 case 'B': {
4492 struct got_object_qid *first;
4493 first = SIMPLEQ_FIRST(&s->blamed_commits);
4494 if (!got_object_id_cmp(first->id, s->commit_id))
4495 break;
4496 s->done = 1;
4497 thread_err = stop_blame(&s->blame);
4498 s->done = 0;
4499 if (thread_err)
4500 break;
4501 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4502 got_object_qid_free(s->blamed_commit);
4503 s->blamed_commit =
4504 SIMPLEQ_FIRST(&s->blamed_commits);
4505 err = run_blame(&s->blame, view, &s->blame_complete,
4506 &s->first_displayed_line, &s->last_displayed_line,
4507 &s->selected_line, &s->done, &s->eof, s->path,
4508 s->blamed_commit->id, s->repo);
4509 if (err)
4510 break;
4511 break;
4513 case KEY_ENTER:
4514 case '\r': {
4515 struct got_object_id *id = NULL;
4516 struct got_object_qid *pid;
4517 struct got_commit_object *commit = NULL;
4518 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4519 s->first_displayed_line, s->selected_line);
4520 if (id == NULL)
4521 break;
4522 err = got_object_open_as_commit(&commit, s->repo, id);
4523 if (err)
4524 break;
4525 pid = SIMPLEQ_FIRST(
4526 got_object_commit_get_parent_ids(commit));
4527 if (view_is_parent_view(view))
4528 begin_x = view_split_begin_x(view->begin_x);
4529 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4530 if (diff_view == NULL) {
4531 got_object_commit_close(commit);
4532 err = got_error_from_errno("view_open");
4533 break;
4535 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4536 id, NULL, s->refs, s->repo);
4537 got_object_commit_close(commit);
4538 if (err) {
4539 view_close(diff_view);
4540 break;
4542 if (view_is_parent_view(view)) {
4543 err = view_close_child(view);
4544 if (err)
4545 break;
4546 err = view_set_child(view, diff_view);
4547 if (err) {
4548 view_close(diff_view);
4549 break;
4551 *focus_view = diff_view;
4552 view->child_focussed = 1;
4553 } else
4554 *new_view = diff_view;
4555 if (err)
4556 break;
4557 break;
4559 case KEY_NPAGE:
4560 case CTRL('f'):
4561 case ' ':
4562 if (s->last_displayed_line >= s->blame.nlines &&
4563 s->selected_line >= MIN(s->blame.nlines,
4564 view->nlines - 2)) {
4565 break;
4567 if (s->last_displayed_line >= s->blame.nlines &&
4568 s->selected_line < view->nlines - 2) {
4569 s->selected_line = MIN(s->blame.nlines,
4570 view->nlines - 2);
4571 break;
4573 if (s->last_displayed_line + view->nlines - 2
4574 <= s->blame.nlines)
4575 s->first_displayed_line +=
4576 view->nlines - 2;
4577 else
4578 s->first_displayed_line =
4579 s->blame.nlines -
4580 (view->nlines - 3);
4581 break;
4582 case KEY_RESIZE:
4583 if (s->selected_line > view->nlines - 2) {
4584 s->selected_line = MIN(s->blame.nlines,
4585 view->nlines - 2);
4587 break;
4588 default:
4589 break;
4591 return thread_err ? thread_err : err;
4594 static const struct got_error *
4595 cmd_blame(int argc, char *argv[])
4597 const struct got_error *error;
4598 struct got_repository *repo = NULL;
4599 struct got_reflist_head refs;
4600 struct got_worktree *worktree = NULL;
4601 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4602 char *link_target = NULL;
4603 struct got_object_id *commit_id = NULL;
4604 char *commit_id_str = NULL;
4605 int ch;
4606 struct tog_view *view;
4608 SIMPLEQ_INIT(&refs);
4610 #ifndef PROFILE
4611 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4612 NULL) == -1)
4613 err(1, "pledge");
4614 #endif
4616 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4617 switch (ch) {
4618 case 'c':
4619 commit_id_str = optarg;
4620 break;
4621 case 'r':
4622 repo_path = realpath(optarg, NULL);
4623 if (repo_path == NULL)
4624 return got_error_from_errno2("realpath",
4625 optarg);
4626 break;
4627 default:
4628 usage_blame();
4629 /* NOTREACHED */
4633 argc -= optind;
4634 argv += optind;
4636 if (argc != 1)
4637 usage_blame();
4639 cwd = getcwd(NULL, 0);
4640 if (cwd == NULL)
4641 return got_error_from_errno("getcwd");
4643 error = got_worktree_open(&worktree, cwd);
4644 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4645 goto done;
4647 if (repo_path == NULL) {
4648 if (worktree)
4649 repo_path =
4650 strdup(got_worktree_get_repo_path(worktree));
4651 else
4652 repo_path = strdup(cwd);
4654 if (repo_path == NULL) {
4655 error = got_error_from_errno("strdup");
4656 goto done;
4659 error = got_repo_open(&repo, repo_path, NULL);
4660 if (error != NULL)
4661 goto done;
4663 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4664 worktree);
4665 if (error)
4666 goto done;
4668 init_curses();
4670 error = apply_unveil(got_repo_get_path(repo), NULL);
4671 if (error)
4672 goto done;
4674 if (commit_id_str == NULL) {
4675 struct got_reference *head_ref;
4676 error = got_ref_open(&head_ref, repo, worktree ?
4677 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4678 if (error != NULL)
4679 goto done;
4680 error = got_ref_resolve(&commit_id, repo, head_ref);
4681 got_ref_close(head_ref);
4682 } else {
4683 error = got_repo_match_object_id(&commit_id, NULL,
4684 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4686 if (error != NULL)
4687 goto done;
4689 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4690 if (error)
4691 goto done;
4693 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4694 if (view == NULL) {
4695 error = got_error_from_errno("view_open");
4696 goto done;
4699 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4700 commit_id, repo);
4701 if (error)
4702 goto done;
4704 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4705 commit_id, &refs, repo);
4706 if (error)
4707 goto done;
4708 if (worktree) {
4709 /* Release work tree lock. */
4710 got_worktree_close(worktree);
4711 worktree = NULL;
4713 error = view_loop(view);
4714 done:
4715 free(repo_path);
4716 free(in_repo_path);
4717 free(link_target);
4718 free(cwd);
4719 free(commit_id);
4720 if (worktree)
4721 got_worktree_close(worktree);
4722 if (repo)
4723 got_repo_close(repo);
4724 got_ref_list_free(&refs);
4725 return error;
4728 static const struct got_error *
4729 draw_tree_entries(struct tog_view *view,
4730 struct got_tree_entry **first_displayed_entry,
4731 struct got_tree_entry **last_displayed_entry,
4732 struct got_tree_entry **selected_entry, int *ndisplayed,
4733 const char *label, int show_ids, const char *parent_path,
4734 struct got_tree_object *tree, int selected, int limit,
4735 int isroot, struct tog_colors *colors, struct got_repository *repo)
4737 const struct got_error *err = NULL;
4738 struct got_tree_entry *te;
4739 wchar_t *wline;
4740 struct tog_color *tc;
4741 int width, n, i, nentries;
4743 *ndisplayed = 0;
4745 werase(view->window);
4747 if (limit == 0)
4748 return NULL;
4750 err = format_line(&wline, &width, label, view->ncols, 0);
4751 if (err)
4752 return err;
4753 if (view_needs_focus_indication(view))
4754 wstandout(view->window);
4755 tc = get_color(colors, TOG_COLOR_COMMIT);
4756 if (tc)
4757 wattr_on(view->window,
4758 COLOR_PAIR(tc->colorpair), NULL);
4759 waddwstr(view->window, wline);
4760 if (tc)
4761 wattr_off(view->window,
4762 COLOR_PAIR(tc->colorpair), NULL);
4763 if (view_needs_focus_indication(view))
4764 wstandend(view->window);
4765 free(wline);
4766 wline = NULL;
4767 if (width < view->ncols - 1)
4768 waddch(view->window, '\n');
4769 if (--limit <= 0)
4770 return NULL;
4771 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4772 if (err)
4773 return err;
4774 waddwstr(view->window, wline);
4775 free(wline);
4776 wline = NULL;
4777 if (width < view->ncols - 1)
4778 waddch(view->window, '\n');
4779 if (--limit <= 0)
4780 return NULL;
4781 waddch(view->window, '\n');
4782 if (--limit <= 0)
4783 return NULL;
4785 if (*first_displayed_entry == NULL) {
4786 te = got_object_tree_get_first_entry(tree);
4787 if (selected == 0) {
4788 if (view->focussed)
4789 wstandout(view->window);
4790 *selected_entry = NULL;
4792 waddstr(view->window, " ..\n"); /* parent directory */
4793 if (selected == 0 && view->focussed)
4794 wstandend(view->window);
4795 (*ndisplayed)++;
4796 if (--limit <= 0)
4797 return NULL;
4798 n = 1;
4799 } else {
4800 n = 0;
4801 te = *first_displayed_entry;
4804 nentries = got_object_tree_get_nentries(tree);
4805 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4806 char *line = NULL, *id_str = NULL, *link_target = NULL;
4807 const char *modestr = "";
4808 mode_t mode;
4810 te = got_object_tree_get_entry(tree, i);
4811 mode = got_tree_entry_get_mode(te);
4813 if (show_ids) {
4814 err = got_object_id_str(&id_str,
4815 got_tree_entry_get_id(te));
4816 if (err)
4817 return got_error_from_errno(
4818 "got_object_id_str");
4820 if (got_object_tree_entry_is_submodule(te))
4821 modestr = "$";
4822 else if (S_ISLNK(mode)) {
4823 int i;
4825 err = got_tree_entry_get_symlink_target(&link_target,
4826 te, repo);
4827 if (err) {
4828 free(id_str);
4829 return err;
4831 for (i = 0; i < strlen(link_target); i++) {
4832 if (!isprint((unsigned char)link_target[i]))
4833 link_target[i] = '?';
4835 modestr = "@";
4837 else if (S_ISDIR(mode))
4838 modestr = "/";
4839 else if (mode & S_IXUSR)
4840 modestr = "*";
4841 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4842 got_tree_entry_get_name(te), modestr,
4843 link_target ? " -> ": "",
4844 link_target ? link_target : "") == -1) {
4845 free(id_str);
4846 free(link_target);
4847 return got_error_from_errno("asprintf");
4849 free(id_str);
4850 free(link_target);
4851 err = format_line(&wline, &width, line, view->ncols, 0);
4852 if (err) {
4853 free(line);
4854 break;
4856 if (n == selected) {
4857 if (view->focussed)
4858 wstandout(view->window);
4859 *selected_entry = te;
4861 tc = match_color(colors, line);
4862 if (tc)
4863 wattr_on(view->window,
4864 COLOR_PAIR(tc->colorpair), NULL);
4865 waddwstr(view->window, wline);
4866 if (tc)
4867 wattr_off(view->window,
4868 COLOR_PAIR(tc->colorpair), NULL);
4869 if (width < view->ncols - 1)
4870 waddch(view->window, '\n');
4871 if (n == selected && view->focussed)
4872 wstandend(view->window);
4873 free(line);
4874 free(wline);
4875 wline = NULL;
4876 n++;
4877 (*ndisplayed)++;
4878 *last_displayed_entry = te;
4879 if (--limit <= 0)
4880 break;
4883 return err;
4886 static void
4887 tree_scroll_up(struct tog_view *view,
4888 struct got_tree_entry **first_displayed_entry, int maxscroll,
4889 struct got_tree_object *tree, int isroot)
4891 struct got_tree_entry *te;
4892 int i;
4894 if (*first_displayed_entry == NULL)
4895 return;
4897 te = got_object_tree_get_entry(tree, 0);
4898 if (*first_displayed_entry == te) {
4899 if (!isroot)
4900 *first_displayed_entry = NULL;
4901 return;
4904 i = 0;
4905 while (*first_displayed_entry && i < maxscroll) {
4906 *first_displayed_entry = got_tree_entry_get_prev(tree,
4907 *first_displayed_entry);
4908 i++;
4910 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4911 *first_displayed_entry = NULL;
4914 static int
4915 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4916 struct got_tree_entry *last_displayed_entry,
4917 struct got_tree_object *tree)
4919 struct got_tree_entry *next, *last;
4920 int n = 0;
4922 if (*first_displayed_entry)
4923 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4924 else
4925 next = got_object_tree_get_first_entry(tree);
4927 last = last_displayed_entry;
4928 while (next && last && n++ < maxscroll) {
4929 last = got_tree_entry_get_next(tree, last);
4930 if (last) {
4931 *first_displayed_entry = next;
4932 next = got_tree_entry_get_next(tree, next);
4935 return n;
4938 static const struct got_error *
4939 tree_entry_path(char **path, struct tog_parent_trees *parents,
4940 struct got_tree_entry *te)
4942 const struct got_error *err = NULL;
4943 struct tog_parent_tree *pt;
4944 size_t len = 2; /* for leading slash and NUL */
4946 TAILQ_FOREACH(pt, parents, entry)
4947 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4948 + 1 /* slash */;
4949 if (te)
4950 len += strlen(got_tree_entry_get_name(te));
4952 *path = calloc(1, len);
4953 if (path == NULL)
4954 return got_error_from_errno("calloc");
4956 (*path)[0] = '/';
4957 pt = TAILQ_LAST(parents, tog_parent_trees);
4958 while (pt) {
4959 const char *name = got_tree_entry_get_name(pt->selected_entry);
4960 if (strlcat(*path, name, len) >= len) {
4961 err = got_error(GOT_ERR_NO_SPACE);
4962 goto done;
4964 if (strlcat(*path, "/", len) >= len) {
4965 err = got_error(GOT_ERR_NO_SPACE);
4966 goto done;
4968 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4970 if (te) {
4971 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4972 err = got_error(GOT_ERR_NO_SPACE);
4973 goto done;
4976 done:
4977 if (err) {
4978 free(*path);
4979 *path = NULL;
4981 return err;
4984 static const struct got_error *
4985 blame_tree_entry(struct tog_view **new_view, int begin_x,
4986 struct got_tree_entry *te, struct tog_parent_trees *parents,
4987 struct got_object_id *commit_id, struct got_reflist_head *refs,
4988 struct got_repository *repo)
4990 const struct got_error *err = NULL;
4991 char *path;
4992 struct tog_view *blame_view;
4994 *new_view = NULL;
4996 err = tree_entry_path(&path, parents, te);
4997 if (err)
4998 return err;
5000 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5001 if (blame_view == NULL) {
5002 err = got_error_from_errno("view_open");
5003 goto done;
5006 err = open_blame_view(blame_view, path, commit_id, refs, repo);
5007 if (err) {
5008 if (err->code == GOT_ERR_CANCELLED)
5009 err = NULL;
5010 view_close(blame_view);
5011 } else
5012 *new_view = blame_view;
5013 done:
5014 free(path);
5015 return err;
5018 static const struct got_error *
5019 log_tree_entry(struct tog_view **new_view, int begin_x,
5020 struct got_tree_entry *te, struct tog_parent_trees *parents,
5021 struct got_object_id *commit_id, struct got_reflist_head *refs,
5022 struct got_repository *repo)
5024 struct tog_view *log_view;
5025 const struct got_error *err = NULL;
5026 char *path;
5028 *new_view = NULL;
5030 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5031 if (log_view == NULL)
5032 return got_error_from_errno("view_open");
5034 err = tree_entry_path(&path, parents, te);
5035 if (err)
5036 return err;
5038 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
5039 if (err)
5040 view_close(log_view);
5041 else
5042 *new_view = log_view;
5043 free(path);
5044 return err;
5047 static const struct got_error *
5048 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5049 struct got_object_id *commit_id, struct got_reflist_head *refs,
5050 struct got_repository *repo)
5052 const struct got_error *err = NULL;
5053 char *commit_id_str = NULL;
5054 struct tog_tree_view_state *s = &view->state.tree;
5056 TAILQ_INIT(&s->parents);
5058 err = got_object_id_str(&commit_id_str, commit_id);
5059 if (err != NULL)
5060 goto done;
5062 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5063 err = got_error_from_errno("asprintf");
5064 goto done;
5067 s->root = s->tree = root;
5068 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5069 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5070 s->commit_id = got_object_id_dup(commit_id);
5071 if (s->commit_id == NULL) {
5072 err = got_error_from_errno("got_object_id_dup");
5073 goto done;
5075 s->refs = refs;
5076 s->repo = repo;
5078 SIMPLEQ_INIT(&s->colors);
5080 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5081 err = add_color(&s->colors, "\\$$",
5082 TOG_COLOR_TREE_SUBMODULE,
5083 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5084 if (err)
5085 goto done;
5086 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5087 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5088 if (err) {
5089 free_colors(&s->colors);
5090 goto done;
5092 err = add_color(&s->colors, "/$",
5093 TOG_COLOR_TREE_DIRECTORY,
5094 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5095 if (err) {
5096 free_colors(&s->colors);
5097 goto done;
5100 err = add_color(&s->colors, "\\*$",
5101 TOG_COLOR_TREE_EXECUTABLE,
5102 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5103 if (err) {
5104 free_colors(&s->colors);
5105 goto done;
5108 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5109 get_color_value("TOG_COLOR_COMMIT"));
5110 if (err) {
5111 free_colors(&s->colors);
5112 goto done;
5116 view->show = show_tree_view;
5117 view->input = input_tree_view;
5118 view->close = close_tree_view;
5119 view->search_start = search_start_tree_view;
5120 view->search_next = search_next_tree_view;
5121 done:
5122 free(commit_id_str);
5123 if (err) {
5124 free(s->tree_label);
5125 s->tree_label = NULL;
5127 return err;
5130 static const struct got_error *
5131 close_tree_view(struct tog_view *view)
5133 struct tog_tree_view_state *s = &view->state.tree;
5135 free_colors(&s->colors);
5136 free(s->tree_label);
5137 s->tree_label = NULL;
5138 free(s->commit_id);
5139 s->commit_id = NULL;
5140 while (!TAILQ_EMPTY(&s->parents)) {
5141 struct tog_parent_tree *parent;
5142 parent = TAILQ_FIRST(&s->parents);
5143 TAILQ_REMOVE(&s->parents, parent, entry);
5144 free(parent);
5147 if (s->tree != s->root)
5148 got_object_tree_close(s->tree);
5149 got_object_tree_close(s->root);
5151 return NULL;
5154 static const struct got_error *
5155 search_start_tree_view(struct tog_view *view)
5157 struct tog_tree_view_state *s = &view->state.tree;
5159 s->matched_entry = NULL;
5160 return NULL;
5163 static int
5164 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5166 regmatch_t regmatch;
5168 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5169 0) == 0;
5172 static const struct got_error *
5173 search_next_tree_view(struct tog_view *view)
5175 struct tog_tree_view_state *s = &view->state.tree;
5176 struct got_tree_entry *te = NULL;
5178 if (!view->searching) {
5179 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5180 return NULL;
5183 if (s->matched_entry) {
5184 if (view->searching == TOG_SEARCH_FORWARD) {
5185 if (s->selected_entry)
5186 te = got_tree_entry_get_next(s->tree,
5187 s->selected_entry);
5188 else
5189 te = got_object_tree_get_first_entry(s->tree);
5190 } else {
5191 if (s->selected_entry == NULL)
5192 te = got_object_tree_get_last_entry(s->tree);
5193 else
5194 te = got_tree_entry_get_prev(s->tree,
5195 s->selected_entry);
5197 } else {
5198 if (view->searching == TOG_SEARCH_FORWARD)
5199 te = got_object_tree_get_first_entry(s->tree);
5200 else
5201 te = got_object_tree_get_last_entry(s->tree);
5204 while (1) {
5205 if (te == NULL) {
5206 if (s->matched_entry == NULL) {
5207 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5208 return NULL;
5210 if (view->searching == TOG_SEARCH_FORWARD)
5211 te = got_object_tree_get_first_entry(s->tree);
5212 else
5213 te = got_object_tree_get_last_entry(s->tree);
5216 if (match_tree_entry(te, &view->regex)) {
5217 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5218 s->matched_entry = te;
5219 break;
5222 if (view->searching == TOG_SEARCH_FORWARD)
5223 te = got_tree_entry_get_next(s->tree, te);
5224 else
5225 te = got_tree_entry_get_prev(s->tree, te);
5228 if (s->matched_entry) {
5229 s->first_displayed_entry = s->matched_entry;
5230 s->selected = 0;
5233 return NULL;
5236 static const struct got_error *
5237 show_tree_view(struct tog_view *view)
5239 const struct got_error *err = NULL;
5240 struct tog_tree_view_state *s = &view->state.tree;
5241 char *parent_path;
5243 err = tree_entry_path(&parent_path, &s->parents, NULL);
5244 if (err)
5245 return err;
5247 err = draw_tree_entries(view, &s->first_displayed_entry,
5248 &s->last_displayed_entry, &s->selected_entry,
5249 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5250 s->tree, s->selected, view->nlines, s->tree == s->root,
5251 &s->colors, s->repo);
5252 free(parent_path);
5254 view_vborder(view);
5255 return err;
5258 static const struct got_error *
5259 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5260 struct tog_view **focus_view, struct tog_view *view, int ch)
5262 const struct got_error *err = NULL;
5263 struct tog_tree_view_state *s = &view->state.tree;
5264 struct tog_view *log_view;
5265 int begin_x = 0, nscrolled;
5267 switch (ch) {
5268 case 'i':
5269 s->show_ids = !s->show_ids;
5270 break;
5271 case 'l':
5272 if (!s->selected_entry)
5273 break;
5274 if (view_is_parent_view(view))
5275 begin_x = view_split_begin_x(view->begin_x);
5276 err = log_tree_entry(&log_view, begin_x,
5277 s->selected_entry, &s->parents,
5278 s->commit_id, s->refs, s->repo);
5279 if (view_is_parent_view(view)) {
5280 err = view_close_child(view);
5281 if (err)
5282 return err;
5283 err = view_set_child(view, log_view);
5284 if (err) {
5285 view_close(log_view);
5286 break;
5288 *focus_view = log_view;
5289 view->child_focussed = 1;
5290 } else
5291 *new_view = log_view;
5292 break;
5293 case 'k':
5294 case KEY_UP:
5295 if (s->selected > 0) {
5296 s->selected--;
5297 if (s->selected == 0)
5298 break;
5300 if (s->selected > 0)
5301 break;
5302 tree_scroll_up(view, &s->first_displayed_entry, 1,
5303 s->tree, s->tree == s->root);
5304 break;
5305 case KEY_PPAGE:
5306 case CTRL('b'):
5307 tree_scroll_up(view, &s->first_displayed_entry,
5308 MAX(0, view->nlines - 4 - s->selected), s->tree,
5309 s->tree == s->root);
5310 s->selected = 0;
5311 if (got_object_tree_get_first_entry(s->tree) ==
5312 s->first_displayed_entry && s->tree != s->root)
5313 s->first_displayed_entry = NULL;
5314 break;
5315 case 'j':
5316 case KEY_DOWN:
5317 if (s->selected < s->ndisplayed - 1) {
5318 s->selected++;
5319 break;
5321 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5322 == NULL)
5323 /* can't scroll any further */
5324 break;
5325 tree_scroll_down(&s->first_displayed_entry, 1,
5326 s->last_displayed_entry, s->tree);
5327 break;
5328 case KEY_NPAGE:
5329 case CTRL('f'):
5330 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5331 == NULL) {
5332 /* can't scroll any further; move cursor down */
5333 if (s->selected < s->ndisplayed - 1)
5334 s->selected = s->ndisplayed - 1;
5335 break;
5337 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5338 view->nlines, s->last_displayed_entry, s->tree);
5339 if (nscrolled < view->nlines) {
5340 int ndisplayed = 0;
5341 struct got_tree_entry *te;
5342 te = s->first_displayed_entry;
5343 do {
5344 ndisplayed++;
5345 te = got_tree_entry_get_next(s->tree, te);
5346 } while (te);
5347 s->selected = ndisplayed - 1;
5349 break;
5350 case KEY_ENTER:
5351 case '\r':
5352 case KEY_BACKSPACE:
5353 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5354 struct tog_parent_tree *parent;
5355 /* user selected '..' */
5356 if (s->tree == s->root)
5357 break;
5358 parent = TAILQ_FIRST(&s->parents);
5359 TAILQ_REMOVE(&s->parents, parent,
5360 entry);
5361 got_object_tree_close(s->tree);
5362 s->tree = parent->tree;
5363 s->first_displayed_entry =
5364 parent->first_displayed_entry;
5365 s->selected_entry =
5366 parent->selected_entry;
5367 s->selected = parent->selected;
5368 free(parent);
5369 } else if (S_ISDIR(got_tree_entry_get_mode(
5370 s->selected_entry))) {
5371 struct got_tree_object *subtree;
5372 err = got_object_open_as_tree(&subtree, s->repo,
5373 got_tree_entry_get_id(s->selected_entry));
5374 if (err)
5375 break;
5376 err = tree_view_visit_subtree(subtree, s);
5377 if (err) {
5378 got_object_tree_close(subtree);
5379 break;
5381 } else if (S_ISREG(got_tree_entry_get_mode(
5382 s->selected_entry))) {
5383 struct tog_view *blame_view;
5384 int begin_x = view_is_parent_view(view) ?
5385 view_split_begin_x(view->begin_x) : 0;
5387 err = blame_tree_entry(&blame_view, begin_x,
5388 s->selected_entry, &s->parents,
5389 s->commit_id, s->refs, s->repo);
5390 if (err)
5391 break;
5392 if (view_is_parent_view(view)) {
5393 err = view_close_child(view);
5394 if (err)
5395 return err;
5396 err = view_set_child(view, blame_view);
5397 if (err) {
5398 view_close(blame_view);
5399 break;
5401 *focus_view = blame_view;
5402 view->child_focussed = 1;
5403 } else
5404 *new_view = blame_view;
5406 break;
5407 case KEY_RESIZE:
5408 if (s->selected > view->nlines)
5409 s->selected = s->ndisplayed - 1;
5410 break;
5411 default:
5412 break;
5415 return err;
5418 __dead static void
5419 usage_tree(void)
5421 endwin();
5422 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5423 getprogname());
5424 exit(1);
5427 static const struct got_error *
5428 cmd_tree(int argc, char *argv[])
5430 const struct got_error *error;
5431 struct got_repository *repo = NULL;
5432 struct got_worktree *worktree = NULL;
5433 struct got_reflist_head refs;
5434 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5435 struct got_object_id *commit_id = NULL;
5436 char *commit_id_arg = NULL;
5437 struct got_commit_object *commit = NULL;
5438 struct got_tree_object *tree = NULL;
5439 int ch;
5440 struct tog_view *view;
5442 SIMPLEQ_INIT(&refs);
5444 #ifndef PROFILE
5445 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5446 NULL) == -1)
5447 err(1, "pledge");
5448 #endif
5450 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5451 switch (ch) {
5452 case 'c':
5453 commit_id_arg = optarg;
5454 break;
5455 case 'r':
5456 repo_path = realpath(optarg, NULL);
5457 if (repo_path == NULL)
5458 return got_error_from_errno2("realpath",
5459 optarg);
5460 break;
5461 default:
5462 usage_tree();
5463 /* NOTREACHED */
5467 argc -= optind;
5468 argv += optind;
5470 if (argc > 1)
5471 usage_tree();
5473 cwd = getcwd(NULL, 0);
5474 if (cwd == NULL)
5475 return got_error_from_errno("getcwd");
5477 error = got_worktree_open(&worktree, cwd);
5478 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5479 goto done;
5481 if (repo_path == NULL) {
5482 if (worktree)
5483 repo_path =
5484 strdup(got_worktree_get_repo_path(worktree));
5485 else
5486 repo_path = strdup(cwd);
5488 if (repo_path == NULL) {
5489 error = got_error_from_errno("strdup");
5490 goto done;
5493 error = got_repo_open(&repo, repo_path, NULL);
5494 if (error != NULL)
5495 goto done;
5497 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5498 repo, worktree);
5499 if (error)
5500 goto done;
5502 init_curses();
5504 error = apply_unveil(got_repo_get_path(repo), NULL);
5505 if (error)
5506 goto done;
5508 error = got_repo_match_object_id(&commit_id, NULL,
5509 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5510 GOT_OBJ_TYPE_COMMIT, 1, repo);
5511 if (error)
5512 goto done;
5514 error = got_object_open_as_commit(&commit, repo, commit_id);
5515 if (error)
5516 goto done;
5518 error = got_object_open_as_tree(&tree, repo,
5519 got_object_commit_get_tree_id(commit));
5520 if (error)
5521 goto done;
5523 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5524 if (error)
5525 goto done;
5527 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5528 if (view == NULL) {
5529 error = got_error_from_errno("view_open");
5530 goto done;
5532 error = open_tree_view(view, tree, commit_id, &refs, repo);
5533 if (error)
5534 goto done;
5535 if (!got_path_is_root_dir(in_repo_path)) {
5536 error = tree_view_walk_path(&view->state.tree, commit_id,
5537 in_repo_path, repo);
5538 if (error)
5539 goto done;
5542 if (worktree) {
5543 /* Release work tree lock. */
5544 got_worktree_close(worktree);
5545 worktree = NULL;
5547 error = view_loop(view);
5548 done:
5549 free(repo_path);
5550 free(cwd);
5551 free(commit_id);
5552 if (commit)
5553 got_object_commit_close(commit);
5554 if (tree)
5555 got_object_tree_close(tree);
5556 if (repo)
5557 got_repo_close(repo);
5558 got_ref_list_free(&refs);
5559 return error;
5562 static void
5563 list_commands(FILE *fp)
5565 int i;
5567 fprintf(fp, "commands:");
5568 for (i = 0; i < nitems(tog_commands); i++) {
5569 struct tog_cmd *cmd = &tog_commands[i];
5570 fprintf(fp, " %s", cmd->name);
5572 fputc('\n', fp);
5575 __dead static void
5576 usage(int hflag, int status)
5578 FILE *fp = (status == 0) ? stdout : stderr;
5580 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5581 getprogname());
5582 if (hflag) {
5583 fprintf(fp, "lazy usage: %s path\n", getprogname());
5584 list_commands(fp);
5586 exit(status);
5589 static char **
5590 make_argv(int argc, ...)
5592 va_list ap;
5593 char **argv;
5594 int i;
5596 va_start(ap, argc);
5598 argv = calloc(argc, sizeof(char *));
5599 if (argv == NULL)
5600 err(1, "calloc");
5601 for (i = 0; i < argc; i++) {
5602 argv[i] = strdup(va_arg(ap, char *));
5603 if (argv[i] == NULL)
5604 err(1, "strdup");
5607 va_end(ap);
5608 return argv;
5612 * Try to convert 'tog path' into a 'tog log path' command.
5613 * The user could simply have mistyped the command rather than knowingly
5614 * provided a path. So check whether argv[0] can in fact be resolved
5615 * to a path in the HEAD commit and print a special error if not.
5616 * This hack is for mpi@ <3
5618 static const struct got_error *
5619 tog_log_with_path(int argc, char *argv[])
5621 const struct got_error *error = NULL;
5622 struct tog_cmd *cmd = NULL;
5623 struct got_repository *repo = NULL;
5624 struct got_worktree *worktree = NULL;
5625 struct got_object_id *commit_id = NULL, *id = NULL;
5626 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5627 char *commit_id_str = NULL, **cmd_argv = NULL;
5629 cwd = getcwd(NULL, 0);
5630 if (cwd == NULL)
5631 return got_error_from_errno("getcwd");
5633 error = got_worktree_open(&worktree, cwd);
5634 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5635 goto done;
5637 if (worktree)
5638 repo_path = strdup(got_worktree_get_repo_path(worktree));
5639 else
5640 repo_path = strdup(cwd);
5641 if (repo_path == NULL) {
5642 error = got_error_from_errno("strdup");
5643 goto done;
5646 error = got_repo_open(&repo, repo_path, NULL);
5647 if (error != NULL)
5648 goto done;
5650 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5651 repo, worktree);
5652 if (error)
5653 goto done;
5655 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5656 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5657 GOT_OBJ_TYPE_COMMIT, 1, repo);
5658 if (error)
5659 goto done;
5661 if (worktree) {
5662 got_worktree_close(worktree);
5663 worktree = NULL;
5666 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5667 if (error) {
5668 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5669 goto done;
5670 fprintf(stderr, "%s: '%s' is no known command or path\n",
5671 getprogname(), argv[0]);
5672 usage(1, 1);
5673 /* not reached */
5676 got_repo_close(repo);
5677 repo = NULL;
5679 error = got_object_id_str(&commit_id_str, commit_id);
5680 if (error)
5681 goto done;
5683 cmd = &tog_commands[0]; /* log */
5684 argc = 4;
5685 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5686 error = cmd->cmd_main(argc, cmd_argv);
5687 done:
5688 if (repo)
5689 got_repo_close(repo);
5690 if (worktree)
5691 got_worktree_close(worktree);
5692 free(id);
5693 free(commit_id_str);
5694 free(commit_id);
5695 free(cwd);
5696 free(repo_path);
5697 free(in_repo_path);
5698 if (cmd_argv) {
5699 int i;
5700 for (i = 0; i < argc; i++)
5701 free(cmd_argv[i]);
5702 free(cmd_argv);
5704 return error;
5707 int
5708 main(int argc, char *argv[])
5710 const struct got_error *error = NULL;
5711 struct tog_cmd *cmd = NULL;
5712 int ch, hflag = 0, Vflag = 0;
5713 char **cmd_argv = NULL;
5714 static struct option longopts[] = {
5715 { "version", no_argument, NULL, 'V' },
5716 { NULL, 0, NULL, 0}
5719 setlocale(LC_CTYPE, "");
5721 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5722 switch (ch) {
5723 case 'h':
5724 hflag = 1;
5725 break;
5726 case 'V':
5727 Vflag = 1;
5728 break;
5729 default:
5730 usage(hflag, 1);
5731 /* NOTREACHED */
5735 argc -= optind;
5736 argv += optind;
5737 optind = 1;
5738 optreset = 1;
5740 if (Vflag) {
5741 got_version_print_str();
5742 return 0;
5745 if (argc == 0) {
5746 if (hflag)
5747 usage(hflag, 0);
5748 /* Build an argument vector which runs a default command. */
5749 cmd = &tog_commands[0];
5750 argc = 1;
5751 cmd_argv = make_argv(argc, cmd->name);
5752 } else {
5753 int i;
5755 /* Did the user specify a command? */
5756 for (i = 0; i < nitems(tog_commands); i++) {
5757 if (strncmp(tog_commands[i].name, argv[0],
5758 strlen(argv[0])) == 0) {
5759 cmd = &tog_commands[i];
5760 break;
5765 if (cmd == NULL) {
5766 if (argc != 1)
5767 usage(0, 1);
5768 /* No command specified; try log with a path */
5769 error = tog_log_with_path(argc, argv);
5770 } else {
5771 if (hflag)
5772 cmd->cmd_usage();
5773 else
5774 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5777 endwin();
5778 putchar('\n');
5779 if (cmd_argv) {
5780 int i;
5781 for (i = 0; i < argc; i++)
5782 free(cmd_argv[i]);
5783 free(cmd_argv);
5786 if (error && error->code != GOT_ERR_CANCELLED)
5787 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5788 return 0;