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);
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 int nlines;
256 off_t *line_offsets;
257 int matched_line;
258 int selected_line;
259 size_t filesize;
261 /* passed from log view; may be NULL */
262 struct tog_view *log_view;
263 };
265 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
267 struct tog_log_thread_args {
268 pthread_cond_t need_commits;
269 pthread_cond_t commit_loaded;
270 int commits_needed;
271 struct got_commit_graph *graph;
272 struct commit_queue *commits;
273 const char *in_repo_path;
274 struct got_object_id *start_id;
275 struct got_repository *repo;
276 int log_complete;
277 sig_atomic_t *quit;
278 struct commit_queue_entry **first_displayed_entry;
279 struct commit_queue_entry **selected_entry;
280 int *searching;
281 int *search_next_done;
282 regex_t *regex;
283 };
285 struct tog_log_view_state {
286 struct commit_queue commits;
287 struct commit_queue_entry *first_displayed_entry;
288 struct commit_queue_entry *last_displayed_entry;
289 struct commit_queue_entry *selected_entry;
290 int selected;
291 char *in_repo_path;
292 const char *head_ref_name;
293 int log_branches;
294 struct got_repository *repo;
295 struct got_reflist_head *refs;
296 struct got_object_id *start_id;
297 sig_atomic_t quit;
298 pthread_t thread;
299 struct tog_log_thread_args thread_args;
300 struct commit_queue_entry *matched_entry;
301 struct commit_queue_entry *search_entry;
302 struct tog_colors colors;
303 };
305 #define TOG_COLOR_DIFF_MINUS 1
306 #define TOG_COLOR_DIFF_PLUS 2
307 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
308 #define TOG_COLOR_DIFF_META 4
309 #define TOG_COLOR_TREE_SUBMODULE 5
310 #define TOG_COLOR_TREE_SYMLINK 6
311 #define TOG_COLOR_TREE_DIRECTORY 7
312 #define TOG_COLOR_TREE_EXECUTABLE 8
313 #define TOG_COLOR_COMMIT 9
314 #define TOG_COLOR_AUTHOR 10
315 #define TOG_COLOR_DATE 11
317 struct tog_blame_cb_args {
318 struct tog_blame_line *lines; /* one per line */
319 int nlines;
321 struct tog_view *view;
322 struct got_object_id *commit_id;
323 int *quit;
324 };
326 struct tog_blame_thread_args {
327 const char *path;
328 struct got_repository *repo;
329 struct tog_blame_cb_args *cb_args;
330 int *complete;
331 got_cancel_cb cancel_cb;
332 void *cancel_arg;
333 };
335 struct tog_blame {
336 FILE *f;
337 size_t filesize;
338 struct tog_blame_line *lines;
339 int nlines;
340 off_t *line_offsets;
341 pthread_t thread;
342 struct tog_blame_thread_args thread_args;
343 struct tog_blame_cb_args cb_args;
344 const char *path;
345 };
347 struct tog_blame_view_state {
348 int first_displayed_line;
349 int last_displayed_line;
350 int selected_line;
351 int blame_complete;
352 int eof;
353 int done;
354 struct got_object_id_queue blamed_commits;
355 struct got_object_qid *blamed_commit;
356 char *path;
357 struct got_repository *repo;
358 struct got_reflist_head *refs;
359 struct got_object_id *commit_id;
360 struct tog_blame blame;
361 int matched_line;
362 struct tog_colors colors;
363 };
365 struct tog_parent_tree {
366 TAILQ_ENTRY(tog_parent_tree) entry;
367 struct got_tree_object *tree;
368 struct got_tree_entry *first_displayed_entry;
369 struct got_tree_entry *selected_entry;
370 int selected;
371 };
373 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
375 struct tog_tree_view_state {
376 char *tree_label;
377 struct got_tree_object *root;
378 struct got_tree_object *tree;
379 struct got_tree_entry *first_displayed_entry;
380 struct got_tree_entry *last_displayed_entry;
381 struct got_tree_entry *selected_entry;
382 int ndisplayed, selected, show_ids;
383 struct tog_parent_trees parents;
384 struct got_object_id *commit_id;
385 struct got_repository *repo;
386 struct got_reflist_head *refs;
387 struct got_tree_entry *matched_entry;
388 struct tog_colors colors;
389 };
391 /*
392 * We implement two types of views: parent views and child views.
394 * The 'Tab' key switches between a parent view and its child view.
395 * Child views are shown side-by-side to their parent view, provided
396 * there is enough screen estate.
398 * When a new view is opened from within a parent view, this new view
399 * becomes a child view of the parent view, replacing any existing child.
401 * When a new view is opened from within a child view, this new view
402 * becomes a parent view which will obscure the views below until the
403 * user quits the new parent view by typing 'q'.
405 * This list of views contains parent views only.
406 * Child views are only pointed to by their parent view.
407 */
408 TAILQ_HEAD(tog_view_list_head, tog_view);
410 struct tog_view {
411 TAILQ_ENTRY(tog_view) entry;
412 WINDOW *window;
413 PANEL *panel;
414 int nlines, ncols, begin_y, begin_x;
415 int lines, cols; /* copies of LINES and COLS */
416 int focussed;
417 struct tog_view *parent;
418 struct tog_view *child;
419 int child_focussed;
421 /* type-specific state */
422 enum tog_view_type type;
423 union {
424 struct tog_diff_view_state diff;
425 struct tog_log_view_state log;
426 struct tog_blame_view_state blame;
427 struct tog_tree_view_state tree;
428 } state;
430 const struct got_error *(*show)(struct tog_view *);
431 const struct got_error *(*input)(struct tog_view **,
432 struct tog_view **, struct tog_view**, struct tog_view *, int);
433 const struct got_error *(*close)(struct tog_view *);
435 const struct got_error *(*search_start)(struct tog_view *);
436 const struct got_error *(*search_next)(struct tog_view *);
437 int searching;
438 #define TOG_SEARCH_FORWARD 1
439 #define TOG_SEARCH_BACKWARD 2
440 int search_next_done;
441 #define TOG_SEARCH_HAVE_MORE 1
442 #define TOG_SEARCH_NO_MORE 2
443 #define TOG_SEARCH_HAVE_NONE 3
444 regex_t regex;
445 };
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,
732 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
733 err = view->search_start(view);
734 if (err) {
735 regfree(&view->regex);
736 return err;
738 view->searching = TOG_SEARCH_FORWARD;
739 view->search_next_done = 0;
740 view->search_next(view);
743 return NULL;
746 static const struct got_error *
747 view_input(struct tog_view **new, struct tog_view **dead,
748 struct tog_view **focus, int *done, struct tog_view *view,
749 struct tog_view_list_head *views)
751 const struct got_error *err = NULL;
752 struct tog_view *v;
753 int ch, errcode;
755 *new = NULL;
756 *dead = NULL;
757 *focus = NULL;
759 /* Clear "no matches" indicator. */
760 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
761 view->search_next_done == TOG_SEARCH_HAVE_NONE)
762 view->search_next_done = TOG_SEARCH_HAVE_MORE;
764 if (view->searching && !view->search_next_done) {
765 errcode = pthread_mutex_unlock(&tog_mutex);
766 if (errcode)
767 return got_error_set_errno(errcode,
768 "pthread_mutex_unlock");
769 pthread_yield();
770 errcode = pthread_mutex_lock(&tog_mutex);
771 if (errcode)
772 return got_error_set_errno(errcode,
773 "pthread_mutex_lock");
774 view->search_next(view);
775 return NULL;
778 nodelay(stdscr, FALSE);
779 /* Allow threads to make progress while we are waiting for input. */
780 errcode = pthread_mutex_unlock(&tog_mutex);
781 if (errcode)
782 return got_error_set_errno(errcode, "pthread_mutex_unlock");
783 ch = wgetch(view->window);
784 errcode = pthread_mutex_lock(&tog_mutex);
785 if (errcode)
786 return got_error_set_errno(errcode, "pthread_mutex_lock");
787 nodelay(stdscr, TRUE);
789 if (tog_sigwinch_received || tog_sigcont_received) {
790 tog_resizeterm();
791 tog_sigwinch_received = 0;
792 tog_sigcont_received = 0;
793 TAILQ_FOREACH(v, views, entry) {
794 err = view_resize(v);
795 if (err)
796 return err;
797 err = v->input(new, dead, focus, v, KEY_RESIZE);
798 if (err)
799 return err;
803 switch (ch) {
804 case ERR:
805 break;
806 case '\t':
807 if (view->child) {
808 *focus = view->child;
809 view->child_focussed = 1;
810 } else if (view->parent) {
811 *focus = view->parent;
812 view->parent->child_focussed = 0;
814 break;
815 case 'q':
816 err = view->input(new, dead, focus, view, ch);
817 *dead = view;
818 break;
819 case 'Q':
820 *done = 1;
821 break;
822 case 'f':
823 if (view_is_parent_view(view)) {
824 if (view->child == NULL)
825 break;
826 if (view_is_splitscreen(view->child)) {
827 *focus = view->child;
828 view->child_focussed = 1;
829 err = view_fullscreen(view->child);
830 } else
831 err = view_splitscreen(view->child);
832 if (err)
833 break;
834 err = view->child->input(new, dead, focus,
835 view->child, KEY_RESIZE);
836 } else {
837 if (view_is_splitscreen(view)) {
838 *focus = view;
839 view->parent->child_focussed = 1;
840 err = view_fullscreen(view);
841 } else {
842 err = view_splitscreen(view);
844 if (err)
845 break;
846 err = view->input(new, dead, focus, view,
847 KEY_RESIZE);
849 break;
850 case KEY_RESIZE:
851 break;
852 case '/':
853 if (view->search_start)
854 view_search_start(view);
855 else
856 err = view->input(new, dead, focus, view, ch);
857 break;
858 case 'N':
859 case 'n':
860 if (view->search_next) {
861 view->searching = (ch == 'n' ?
862 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
863 view->search_next_done = 0;
864 view->search_next(view);
865 } else
866 err = view->input(new, dead, focus, view, ch);
867 break;
868 default:
869 err = view->input(new, dead, focus, view, ch);
870 break;
873 return err;
876 void
877 view_vborder(struct tog_view *view)
879 PANEL *panel;
880 struct tog_view *view_above;
882 if (view->parent)
883 return view_vborder(view->parent);
885 panel = panel_above(view->panel);
886 if (panel == NULL)
887 return;
889 view_above = panel_userptr(panel);
890 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
891 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
894 int
895 view_needs_focus_indication(struct tog_view *view)
897 if (view_is_parent_view(view)) {
898 if (view->child == NULL || view->child_focussed)
899 return 0;
900 if (!view_is_splitscreen(view->child))
901 return 0;
902 } else if (!view_is_splitscreen(view))
903 return 0;
905 return view->focussed;
908 static const struct got_error *
909 view_loop(struct tog_view *view)
911 const struct got_error *err = NULL;
912 struct tog_view_list_head views;
913 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
914 int fast_refresh = 10;
915 int done = 0, errcode;
917 errcode = pthread_mutex_lock(&tog_mutex);
918 if (errcode)
919 return got_error_set_errno(errcode, "pthread_mutex_lock");
921 TAILQ_INIT(&views);
922 TAILQ_INSERT_HEAD(&views, view, entry);
924 main_view = view;
925 view->focussed = 1;
926 err = view->show(view);
927 if (err)
928 return err;
929 update_panels();
930 doupdate();
931 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
932 /* Refresh fast during initialization, then become slower. */
933 if (fast_refresh && fast_refresh-- == 0)
934 halfdelay(10); /* switch to once per second */
936 err = view_input(&new_view, &dead_view, &focus_view, &done,
937 view, &views);
938 if (err)
939 break;
940 if (dead_view) {
941 struct tog_view *prev = NULL;
943 if (view_is_parent_view(dead_view))
944 prev = TAILQ_PREV(dead_view,
945 tog_view_list_head, entry);
946 else if (view->parent != dead_view)
947 prev = view->parent;
949 if (dead_view->parent)
950 dead_view->parent->child = NULL;
951 else
952 TAILQ_REMOVE(&views, dead_view, entry);
954 err = view_close(dead_view);
955 if (err || (dead_view == main_view && new_view == NULL))
956 goto done;
958 if (view == dead_view) {
959 if (focus_view)
960 view = focus_view;
961 else if (prev)
962 view = prev;
963 else if (!TAILQ_EMPTY(&views))
964 view = TAILQ_LAST(&views,
965 tog_view_list_head);
966 else
967 view = NULL;
968 if (view) {
969 if (view->child && view->child_focussed)
970 focus_view = view->child;
971 else
972 focus_view = view;
976 if (new_view) {
977 struct tog_view *v, *t;
978 /* Only allow one parent view per type. */
979 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
980 if (v->type != new_view->type)
981 continue;
982 TAILQ_REMOVE(&views, v, entry);
983 err = view_close(v);
984 if (err)
985 goto done;
986 break;
988 TAILQ_INSERT_TAIL(&views, new_view, entry);
989 view = new_view;
990 if (focus_view == NULL)
991 focus_view = new_view;
993 if (focus_view) {
994 show_panel(focus_view->panel);
995 if (view)
996 view->focussed = 0;
997 focus_view->focussed = 1;
998 view = focus_view;
999 if (new_view)
1000 show_panel(new_view->panel);
1001 if (view->child && view_is_splitscreen(view->child))
1002 show_panel(view->child->panel);
1004 if (view) {
1005 if (focus_view == NULL) {
1006 view->focussed = 1;
1007 show_panel(view->panel);
1008 if (view->child && view_is_splitscreen(view->child))
1009 show_panel(view->child->panel);
1010 focus_view = view;
1012 if (view->parent) {
1013 err = view->parent->show(view->parent);
1014 if (err)
1015 goto done;
1017 err = view->show(view);
1018 if (err)
1019 goto done;
1020 if (view->child) {
1021 err = view->child->show(view->child);
1022 if (err)
1023 goto done;
1025 update_panels();
1026 doupdate();
1029 done:
1030 while (!TAILQ_EMPTY(&views)) {
1031 view = TAILQ_FIRST(&views);
1032 TAILQ_REMOVE(&views, view, entry);
1033 view_close(view);
1036 errcode = pthread_mutex_unlock(&tog_mutex);
1037 if (errcode && err == NULL)
1038 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1040 return err;
1043 __dead static void
1044 usage_log(void)
1046 endwin();
1047 fprintf(stderr,
1048 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1049 getprogname());
1050 exit(1);
1053 /* Create newly allocated wide-character string equivalent to a byte string. */
1054 static const struct got_error *
1055 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1057 char *vis = NULL;
1058 const struct got_error *err = NULL;
1060 *ws = NULL;
1061 *wlen = mbstowcs(NULL, s, 0);
1062 if (*wlen == (size_t)-1) {
1063 int vislen;
1064 if (errno != EILSEQ)
1065 return got_error_from_errno("mbstowcs");
1067 /* byte string invalid in current encoding; try to "fix" it */
1068 err = got_mbsavis(&vis, &vislen, s);
1069 if (err)
1070 return err;
1071 *wlen = mbstowcs(NULL, vis, 0);
1072 if (*wlen == (size_t)-1) {
1073 err = got_error_from_errno("mbstowcs"); /* give up */
1074 goto done;
1078 *ws = calloc(*wlen + 1, sizeof(**ws));
1079 if (*ws == NULL) {
1080 err = got_error_from_errno("calloc");
1081 goto done;
1084 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1085 err = got_error_from_errno("mbstowcs");
1086 done:
1087 free(vis);
1088 if (err) {
1089 free(*ws);
1090 *ws = NULL;
1091 *wlen = 0;
1093 return err;
1096 /* Format a line for display, ensuring that it won't overflow a width limit. */
1097 static const struct got_error *
1098 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1099 int col_tab_align)
1101 const struct got_error *err = NULL;
1102 int cols = 0;
1103 wchar_t *wline = NULL;
1104 size_t wlen;
1105 int i;
1107 *wlinep = NULL;
1108 *widthp = 0;
1110 err = mbs2ws(&wline, &wlen, line);
1111 if (err)
1112 return err;
1114 i = 0;
1115 while (i < wlen) {
1116 int width = wcwidth(wline[i]);
1118 if (width == 0) {
1119 i++;
1120 continue;
1123 if (width == 1 || width == 2) {
1124 if (cols + width > wlimit)
1125 break;
1126 cols += width;
1127 i++;
1128 } else if (width == -1) {
1129 if (wline[i] == L'\t') {
1130 width = TABSIZE -
1131 ((cols + col_tab_align) % TABSIZE);
1132 if (cols + width > wlimit)
1133 break;
1134 cols += width;
1136 i++;
1137 } else {
1138 err = got_error_from_errno("wcwidth");
1139 goto done;
1142 wline[i] = L'\0';
1143 if (widthp)
1144 *widthp = cols;
1145 done:
1146 if (err)
1147 free(wline);
1148 else
1149 *wlinep = wline;
1150 return err;
1153 static const struct got_error*
1154 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1155 struct got_object_id *id, struct got_repository *repo)
1157 static const struct got_error *err = NULL;
1158 struct got_reflist_entry *re;
1159 char *s;
1160 const char *name;
1162 *refs_str = NULL;
1164 SIMPLEQ_FOREACH(re, refs, entry) {
1165 struct got_tag_object *tag = NULL;
1166 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 if (strncmp(name, "tags/", 5) == 0) {
1184 err = got_object_open_as_tag(&tag, repo, re->id);
1185 if (err) {
1186 if (err->code != GOT_ERR_OBJ_TYPE)
1187 break;
1188 /* Ref points at something other than a tag. */
1189 err = NULL;
1190 tag = NULL;
1193 cmp = got_object_id_cmp(tag ?
1194 got_object_tag_get_object_id(tag) : re->id, id);
1195 if (tag)
1196 got_object_tag_close(tag);
1197 if (cmp != 0)
1198 continue;
1199 s = *refs_str;
1200 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1201 s ? ", " : "", name) == -1) {
1202 err = got_error_from_errno("asprintf");
1203 free(s);
1204 *refs_str = NULL;
1205 break;
1207 free(s);
1210 return err;
1213 static const struct got_error *
1214 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1215 int col_tab_align)
1217 char *smallerthan, *at;
1219 smallerthan = strchr(author, '<');
1220 if (smallerthan && smallerthan[1] != '\0')
1221 author = smallerthan + 1;
1222 at = strchr(author, '@');
1223 if (at)
1224 *at = '\0';
1225 return format_line(wauthor, author_width, author, limit, col_tab_align);
1228 static const struct got_error *
1229 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1230 struct got_object_id *id, struct got_reflist_head *refs,
1231 const size_t date_display_cols, int author_display_cols,
1232 struct tog_colors *colors)
1234 const struct got_error *err = NULL;
1235 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1236 char *logmsg0 = NULL, *logmsg = NULL;
1237 char *author = NULL;
1238 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1239 int author_width, logmsg_width;
1240 char *newline, *line = NULL;
1241 int col, limit;
1242 const int avail = view->ncols;
1243 struct tm tm;
1244 time_t committer_time;
1245 struct tog_color *tc;
1247 committer_time = got_object_commit_get_committer_time(commit);
1248 if (localtime_r(&committer_time, &tm) == NULL)
1249 return got_error_from_errno("localtime_r");
1250 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1251 >= sizeof(datebuf))
1252 return got_error(GOT_ERR_NO_SPACE);
1254 if (avail <= date_display_cols)
1255 limit = MIN(sizeof(datebuf) - 1, avail);
1256 else
1257 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1258 tc = get_color(colors, TOG_COLOR_DATE);
1259 if (tc)
1260 wattr_on(view->window,
1261 COLOR_PAIR(tc->colorpair), NULL);
1262 waddnstr(view->window, datebuf, limit);
1263 if (tc)
1264 wattr_off(view->window,
1265 COLOR_PAIR(tc->colorpair), NULL);
1266 col = limit;
1267 if (col > avail)
1268 goto done;
1270 if (avail >= 120) {
1271 char *id_str;
1272 err = got_object_id_str(&id_str, id);
1273 if (err)
1274 goto done;
1275 tc = get_color(colors, TOG_COLOR_COMMIT);
1276 if (tc)
1277 wattr_on(view->window,
1278 COLOR_PAIR(tc->colorpair), NULL);
1279 wprintw(view->window, "%.8s ", id_str);
1280 if (tc)
1281 wattr_off(view->window,
1282 COLOR_PAIR(tc->colorpair), NULL);
1283 free(id_str);
1284 col += 9;
1285 if (col > avail)
1286 goto done;
1289 author = strdup(got_object_commit_get_author(commit));
1290 if (author == NULL) {
1291 err = got_error_from_errno("strdup");
1292 goto done;
1294 err = format_author(&wauthor, &author_width, author, avail - col, col);
1295 if (err)
1296 goto done;
1297 tc = get_color(colors, TOG_COLOR_AUTHOR);
1298 if (tc)
1299 wattr_on(view->window,
1300 COLOR_PAIR(tc->colorpair), NULL);
1301 waddwstr(view->window, wauthor);
1302 if (tc)
1303 wattr_off(view->window,
1304 COLOR_PAIR(tc->colorpair), NULL);
1305 col += author_width;
1306 while (col < avail && author_width < author_display_cols + 2) {
1307 waddch(view->window, ' ');
1308 col++;
1309 author_width++;
1311 if (col > avail)
1312 goto done;
1314 err = got_object_commit_get_logmsg(&logmsg0, commit);
1315 if (err)
1316 goto done;
1317 logmsg = logmsg0;
1318 while (*logmsg == '\n')
1319 logmsg++;
1320 newline = strchr(logmsg, '\n');
1321 if (newline)
1322 *newline = '\0';
1323 limit = avail - col;
1324 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1325 if (err)
1326 goto done;
1327 waddwstr(view->window, wlogmsg);
1328 col += logmsg_width;
1329 while (col < avail) {
1330 waddch(view->window, ' ');
1331 col++;
1333 done:
1334 free(logmsg0);
1335 free(wlogmsg);
1336 free(author);
1337 free(wauthor);
1338 free(line);
1339 return err;
1342 static struct commit_queue_entry *
1343 alloc_commit_queue_entry(struct got_commit_object *commit,
1344 struct got_object_id *id)
1346 struct commit_queue_entry *entry;
1348 entry = calloc(1, sizeof(*entry));
1349 if (entry == NULL)
1350 return NULL;
1352 entry->id = id;
1353 entry->commit = commit;
1354 return entry;
1357 static void
1358 pop_commit(struct commit_queue *commits)
1360 struct commit_queue_entry *entry;
1362 entry = TAILQ_FIRST(&commits->head);
1363 TAILQ_REMOVE(&commits->head, entry, entry);
1364 got_object_commit_close(entry->commit);
1365 commits->ncommits--;
1366 /* Don't free entry->id! It is owned by the commit graph. */
1367 free(entry);
1370 static void
1371 free_commits(struct commit_queue *commits)
1373 while (!TAILQ_EMPTY(&commits->head))
1374 pop_commit(commits);
1377 static const struct got_error *
1378 match_commit(int *have_match, struct got_object_id *id,
1379 struct got_commit_object *commit, regex_t *regex)
1381 const struct got_error *err = NULL;
1382 regmatch_t regmatch;
1383 char *id_str = NULL, *logmsg = NULL;
1385 *have_match = 0;
1387 err = got_object_id_str(&id_str, id);
1388 if (err)
1389 return err;
1391 err = got_object_commit_get_logmsg(&logmsg, commit);
1392 if (err)
1393 goto done;
1395 if (regexec(regex, got_object_commit_get_author(commit), 1,
1396 &regmatch, 0) == 0 ||
1397 regexec(regex, got_object_commit_get_committer(commit), 1,
1398 &regmatch, 0) == 0 ||
1399 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1400 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1401 *have_match = 1;
1402 done:
1403 free(id_str);
1404 free(logmsg);
1405 return err;
1408 static const struct got_error *
1409 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1410 int minqueue, struct got_repository *repo, const char *path,
1411 int *searching, int *search_next_done, regex_t *regex)
1413 const struct got_error *err = NULL;
1414 int nqueued = 0;
1417 * We keep all commits open throughout the lifetime of the log
1418 * view in order to avoid having to re-fetch commits from disk
1419 * while updating the display.
1421 while (nqueued < minqueue ||
1422 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1423 struct got_object_id *id;
1424 struct got_commit_object *commit;
1425 struct commit_queue_entry *entry;
1426 int errcode;
1428 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1429 if (err || id == NULL)
1430 break;
1432 err = got_object_open_as_commit(&commit, repo, id);
1433 if (err)
1434 break;
1435 entry = alloc_commit_queue_entry(commit, id);
1436 if (entry == NULL) {
1437 err = got_error_from_errno("alloc_commit_queue_entry");
1438 break;
1441 errcode = pthread_mutex_lock(&tog_mutex);
1442 if (errcode) {
1443 err = got_error_set_errno(errcode,
1444 "pthread_mutex_lock");
1445 break;
1448 entry->idx = commits->ncommits;
1449 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1450 nqueued++;
1451 commits->ncommits++;
1453 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1454 int have_match;
1455 err = match_commit(&have_match, id, commit, regex);
1456 if (err)
1457 break;
1458 if (have_match)
1459 *search_next_done = TOG_SEARCH_HAVE_MORE;
1462 errcode = pthread_mutex_unlock(&tog_mutex);
1463 if (errcode && err == NULL)
1464 err = got_error_set_errno(errcode,
1465 "pthread_mutex_unlock");
1466 if (err)
1467 break;
1470 return err;
1473 static const struct got_error *
1474 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1475 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1476 struct commit_queue *commits, int selected_idx, int limit,
1477 struct got_reflist_head *refs, const char *path, int commits_needed,
1478 struct tog_colors *colors)
1480 const struct got_error *err = NULL;
1481 struct tog_log_view_state *s = &view->state.log;
1482 struct commit_queue_entry *entry;
1483 int width;
1484 int ncommits, author_cols = 4;
1485 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1486 char *refs_str = NULL;
1487 wchar_t *wline;
1488 struct tog_color *tc;
1489 static const size_t date_display_cols = 12;
1491 entry = first;
1492 ncommits = 0;
1493 while (entry) {
1494 if (ncommits == selected_idx) {
1495 *selected = entry;
1496 break;
1498 entry = TAILQ_NEXT(entry, entry);
1499 ncommits++;
1502 if (*selected && !(view->searching && view->search_next_done == 0)) {
1503 err = got_object_id_str(&id_str, (*selected)->id);
1504 if (err)
1505 return err;
1506 if (refs) {
1507 err = build_refs_str(&refs_str, refs, (*selected)->id,
1508 s->repo);
1509 if (err)
1510 goto done;
1514 if (commits_needed == 0)
1515 halfdelay(10); /* disable fast refresh */
1517 if (commits_needed > 0) {
1518 if (asprintf(&ncommits_str, " [%d/%d] %s",
1519 entry ? entry->idx + 1 : 0, commits->ncommits,
1520 (view->searching && !view->search_next_done) ?
1521 "searching..." : "loading...") == -1) {
1522 err = got_error_from_errno("asprintf");
1523 goto done;
1525 } else {
1526 const char *search_str = NULL;
1528 if (view->searching) {
1529 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1530 search_str = "no more matches";
1531 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1532 search_str = "no matches found";
1533 else if (!view->search_next_done)
1534 search_str = "searching...";
1537 if (asprintf(&ncommits_str, " [%d/%d] %s",
1538 entry ? entry->idx + 1 : 0, commits->ncommits,
1539 search_str ? search_str :
1540 (refs_str ? refs_str : "")) == -1) {
1541 err = got_error_from_errno("asprintf");
1542 goto done;
1546 if (path && strcmp(path, "/") != 0) {
1547 if (asprintf(&header, "commit %s %s%s",
1548 id_str ? id_str : "........................................",
1549 path, ncommits_str) == -1) {
1550 err = got_error_from_errno("asprintf");
1551 header = NULL;
1552 goto done;
1554 } else if (asprintf(&header, "commit %s%s",
1555 id_str ? id_str : "........................................",
1556 ncommits_str) == -1) {
1557 err = got_error_from_errno("asprintf");
1558 header = NULL;
1559 goto done;
1561 err = format_line(&wline, &width, header, view->ncols, 0);
1562 if (err)
1563 goto done;
1565 werase(view->window);
1567 if (view_needs_focus_indication(view))
1568 wstandout(view->window);
1569 tc = get_color(colors, TOG_COLOR_COMMIT);
1570 if (tc)
1571 wattr_on(view->window,
1572 COLOR_PAIR(tc->colorpair), NULL);
1573 waddwstr(view->window, wline);
1574 if (tc)
1575 wattr_off(view->window,
1576 COLOR_PAIR(tc->colorpair), NULL);
1577 while (width < view->ncols) {
1578 waddch(view->window, ' ');
1579 width++;
1581 if (view_needs_focus_indication(view))
1582 wstandend(view->window);
1583 free(wline);
1584 if (limit <= 1)
1585 goto done;
1587 /* Grow author column size if necessary. */
1588 entry = first;
1589 ncommits = 0;
1590 while (entry) {
1591 char *author;
1592 wchar_t *wauthor;
1593 int width;
1594 if (ncommits >= limit - 1)
1595 break;
1596 author = strdup(got_object_commit_get_author(entry->commit));
1597 if (author == NULL) {
1598 err = got_error_from_errno("strdup");
1599 goto done;
1601 err = format_author(&wauthor, &width, author, COLS,
1602 date_display_cols);
1603 if (author_cols < width)
1604 author_cols = width;
1605 free(wauthor);
1606 free(author);
1607 ncommits++;
1608 entry = TAILQ_NEXT(entry, entry);
1611 entry = first;
1612 *last = first;
1613 ncommits = 0;
1614 while (entry) {
1615 if (ncommits >= limit - 1)
1616 break;
1617 if (ncommits == selected_idx)
1618 wstandout(view->window);
1619 err = draw_commit(view, entry->commit, entry->id, refs,
1620 date_display_cols, author_cols, colors);
1621 if (ncommits == selected_idx)
1622 wstandend(view->window);
1623 if (err)
1624 goto done;
1625 ncommits++;
1626 *last = entry;
1627 entry = TAILQ_NEXT(entry, entry);
1630 view_vborder(view);
1631 done:
1632 free(id_str);
1633 free(refs_str);
1634 free(ncommits_str);
1635 free(header);
1636 return err;
1639 static void
1640 scroll_up(struct tog_view *view,
1641 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1642 struct commit_queue *commits)
1644 struct commit_queue_entry *entry;
1645 int nscrolled = 0;
1647 entry = TAILQ_FIRST(&commits->head);
1648 if (*first_displayed_entry == entry)
1649 return;
1651 entry = *first_displayed_entry;
1652 while (entry && nscrolled < maxscroll) {
1653 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1654 if (entry) {
1655 *first_displayed_entry = entry;
1656 nscrolled++;
1661 static const struct got_error *
1662 trigger_log_thread(struct tog_view *log_view, int wait,
1663 int *commits_needed, int *log_complete,
1664 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1666 int errcode;
1668 halfdelay(1); /* fast refresh while loading commits */
1670 while (*commits_needed > 0) {
1671 if (*log_complete)
1672 break;
1674 /* Wake the log thread. */
1675 errcode = pthread_cond_signal(need_commits);
1676 if (errcode)
1677 return got_error_set_errno(errcode,
1678 "pthread_cond_signal");
1681 * The mutex will be released while the view loop waits
1682 * in wgetch(), at which time the log thread will run.
1684 if (!wait)
1685 break;
1687 /* Display progress update in log view. */
1688 show_log_view(log_view);
1689 update_panels();
1690 doupdate();
1692 /* Wait right here while next commit is being loaded. */
1693 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1694 if (errcode)
1695 return got_error_set_errno(errcode,
1696 "pthread_cond_wait");
1698 /* Display progress update in log view. */
1699 show_log_view(log_view);
1700 update_panels();
1701 doupdate();
1704 return NULL;
1707 static const struct got_error *
1708 scroll_down(struct tog_view *log_view,
1709 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1710 struct commit_queue_entry **last_displayed_entry,
1711 struct commit_queue *commits, int *log_complete, int *commits_needed,
1712 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1714 const struct got_error *err = NULL;
1715 struct commit_queue_entry *pentry;
1716 int nscrolled = 0, ncommits_needed;
1718 if (*last_displayed_entry == NULL)
1719 return NULL;
1721 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1722 if (commits->ncommits < ncommits_needed && !*log_complete) {
1724 * Ask the log thread for required amount of commits.
1726 (*commits_needed) += maxscroll;
1727 err = trigger_log_thread(log_view, 1, commits_needed,
1728 log_complete, need_commits, commit_loaded);
1729 if (err)
1730 return err;
1733 do {
1734 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1735 if (pentry == NULL)
1736 break;
1738 *last_displayed_entry = pentry;
1740 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1741 if (pentry == NULL)
1742 break;
1743 *first_displayed_entry = pentry;
1744 } while (++nscrolled < maxscroll);
1746 return err;
1749 static const struct got_error *
1750 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1751 struct got_commit_object *commit, struct got_object_id *commit_id,
1752 struct tog_view *log_view, struct got_reflist_head *refs,
1753 struct got_repository *repo)
1755 const struct got_error *err;
1756 struct got_object_qid *parent_id;
1757 struct tog_view *diff_view;
1759 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1760 if (diff_view == NULL)
1761 return got_error_from_errno("view_open");
1763 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1764 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1765 commit_id, log_view, refs, repo);
1766 if (err == NULL)
1767 *new_view = diff_view;
1768 return err;
1771 static const struct got_error *
1772 tree_view_visit_subtree(struct got_tree_object *subtree,
1773 struct tog_tree_view_state *s)
1775 struct tog_parent_tree *parent;
1777 parent = calloc(1, sizeof(*parent));
1778 if (parent == NULL)
1779 return got_error_from_errno("calloc");
1781 parent->tree = s->tree;
1782 parent->first_displayed_entry = s->first_displayed_entry;
1783 parent->selected_entry = s->selected_entry;
1784 parent->selected = s->selected;
1785 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1786 s->tree = subtree;
1787 s->selected = 0;
1788 s->first_displayed_entry = NULL;
1789 return NULL;
1792 static const struct got_error *
1793 tree_view_walk_path(struct tog_tree_view_state *s,
1794 struct got_object_id *commit_id,
1795 const char *path, struct got_repository *repo)
1797 const struct got_error *err = NULL;
1798 struct got_tree_object *tree = NULL;
1799 const char *p;
1800 char *slash, *subpath = NULL;
1802 /* Walk the path and open corresponding tree objects. */
1803 p = path;
1804 while (*p) {
1805 struct got_tree_entry *te;
1806 struct got_object_id *tree_id;
1807 char *te_name;
1809 while (p[0] == '/')
1810 p++;
1812 /* Ensure the correct subtree entry is selected. */
1813 slash = strchr(p, '/');
1814 if (slash == NULL)
1815 te_name = strdup(p);
1816 else
1817 te_name = strndup(p, slash - p);
1818 if (te_name == NULL) {
1819 err = got_error_from_errno("strndup");
1820 break;
1822 te = got_object_tree_find_entry(s->tree, te_name);
1823 if (te == NULL) {
1824 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1825 free(te_name);
1826 break;
1828 free(te_name);
1829 s->selected_entry = te;
1830 s->selected = got_tree_entry_get_index(te);
1831 if (s->tree != s->root)
1832 s->selected++; /* skip '..' */
1834 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1835 /* Jump to this file's entry. */
1836 s->first_displayed_entry = s->selected_entry;
1837 s->selected = 0;
1838 break;
1841 slash = strchr(p, '/');
1842 if (slash)
1843 subpath = strndup(path, slash - path);
1844 else
1845 subpath = strdup(path);
1846 if (subpath == NULL) {
1847 err = got_error_from_errno("strdup");
1848 break;
1851 err = got_object_id_by_path(&tree_id, repo, commit_id,
1852 subpath);
1853 if (err)
1854 break;
1856 err = got_object_open_as_tree(&tree, repo, tree_id);
1857 free(tree_id);
1858 if (err)
1859 break;
1861 err = tree_view_visit_subtree(tree, s);
1862 if (err) {
1863 got_object_tree_close(tree);
1864 break;
1866 if (slash == NULL)
1867 break;
1868 free(subpath);
1869 subpath = NULL;
1870 p = slash;
1873 free(subpath);
1874 return err;
1877 static const struct got_error *
1878 browse_commit_tree(struct tog_view **new_view, int begin_x,
1879 struct commit_queue_entry *entry, const char *path,
1880 struct got_reflist_head *refs, struct got_repository *repo)
1882 const struct got_error *err = NULL;
1883 struct got_tree_object *tree;
1884 struct tog_tree_view_state *s;
1885 struct tog_view *tree_view;
1887 err = got_object_open_as_tree(&tree, repo,
1888 got_object_commit_get_tree_id(entry->commit));
1889 if (err)
1890 return err;
1892 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1893 if (tree_view == NULL)
1894 return got_error_from_errno("view_open");
1896 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1897 if (err) {
1898 got_object_tree_close(tree);
1899 return err;
1901 s = &tree_view->state.tree;
1903 *new_view = tree_view;
1905 if (got_path_is_root_dir(path))
1906 return NULL;
1908 return tree_view_walk_path(s, entry->id, path, repo);
1911 static const struct got_error *
1912 block_signals_used_by_main_thread(void)
1914 sigset_t sigset;
1915 int errcode;
1917 if (sigemptyset(&sigset) == -1)
1918 return got_error_from_errno("sigemptyset");
1920 /* tog handles SIGWINCH and SIGCONT */
1921 if (sigaddset(&sigset, SIGWINCH) == -1)
1922 return got_error_from_errno("sigaddset");
1923 if (sigaddset(&sigset, SIGCONT) == -1)
1924 return got_error_from_errno("sigaddset");
1926 /* ncurses handles SIGTSTP */
1927 if (sigaddset(&sigset, SIGTSTP) == -1)
1928 return got_error_from_errno("sigaddset");
1930 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1931 if (errcode)
1932 return got_error_set_errno(errcode, "pthread_sigmask");
1934 return NULL;
1937 static void *
1938 log_thread(void *arg)
1940 const struct got_error *err = NULL;
1941 int errcode = 0;
1942 struct tog_log_thread_args *a = arg;
1943 int done = 0;
1945 err = block_signals_used_by_main_thread();
1946 if (err)
1947 return (void *)err;
1949 while (!done && !err && !tog_sigpipe_received) {
1950 err = queue_commits(a->graph, a->commits, 1, a->repo,
1951 a->in_repo_path, a->searching, a->search_next_done,
1952 a->regex);
1953 if (err) {
1954 if (err->code != GOT_ERR_ITER_COMPLETED)
1955 return (void *)err;
1956 err = NULL;
1957 done = 1;
1958 } else if (a->commits_needed > 0)
1959 a->commits_needed--;
1961 errcode = pthread_mutex_lock(&tog_mutex);
1962 if (errcode) {
1963 err = got_error_set_errno(errcode,
1964 "pthread_mutex_lock");
1965 break;
1966 } else if (*a->quit)
1967 done = 1;
1968 else if (*a->first_displayed_entry == NULL) {
1969 *a->first_displayed_entry =
1970 TAILQ_FIRST(&a->commits->head);
1971 *a->selected_entry = *a->first_displayed_entry;
1974 errcode = pthread_cond_signal(&a->commit_loaded);
1975 if (errcode) {
1976 err = got_error_set_errno(errcode,
1977 "pthread_cond_signal");
1978 pthread_mutex_unlock(&tog_mutex);
1979 break;
1982 if (done)
1983 a->commits_needed = 0;
1984 else {
1985 if (a->commits_needed == 0) {
1986 errcode = pthread_cond_wait(&a->need_commits,
1987 &tog_mutex);
1988 if (errcode)
1989 err = got_error_set_errno(errcode,
1990 "pthread_cond_wait");
1994 errcode = pthread_mutex_unlock(&tog_mutex);
1995 if (errcode && err == NULL)
1996 err = got_error_set_errno(errcode,
1997 "pthread_mutex_unlock");
1999 a->log_complete = 1;
2000 return (void *)err;
2003 static const struct got_error *
2004 stop_log_thread(struct tog_log_view_state *s)
2006 const struct got_error *err = NULL;
2007 int errcode;
2009 if (s->thread) {
2010 s->quit = 1;
2011 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2012 if (errcode)
2013 return got_error_set_errno(errcode,
2014 "pthread_cond_signal");
2015 errcode = pthread_mutex_unlock(&tog_mutex);
2016 if (errcode)
2017 return got_error_set_errno(errcode,
2018 "pthread_mutex_unlock");
2019 errcode = pthread_join(s->thread, (void **)&err);
2020 if (errcode)
2021 return got_error_set_errno(errcode, "pthread_join");
2022 errcode = pthread_mutex_lock(&tog_mutex);
2023 if (errcode)
2024 return got_error_set_errno(errcode,
2025 "pthread_mutex_lock");
2026 s->thread = NULL;
2029 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2030 if (errcode && err == NULL)
2031 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2033 if (s->thread_args.repo) {
2034 got_repo_close(s->thread_args.repo);
2035 s->thread_args.repo = NULL;
2038 if (s->thread_args.graph) {
2039 got_commit_graph_close(s->thread_args.graph);
2040 s->thread_args.graph = NULL;
2043 return err;
2046 static const struct got_error *
2047 close_log_view(struct tog_view *view)
2049 const struct got_error *err = NULL;
2050 struct tog_log_view_state *s = &view->state.log;
2052 err = stop_log_thread(s);
2053 free_commits(&s->commits);
2054 free(s->in_repo_path);
2055 s->in_repo_path = NULL;
2056 free(s->start_id);
2057 s->start_id = NULL;
2058 return err;
2061 static const struct got_error *
2062 search_start_log_view(struct tog_view *view)
2064 struct tog_log_view_state *s = &view->state.log;
2066 s->matched_entry = NULL;
2067 s->search_entry = NULL;
2068 return NULL;
2071 static const struct got_error *
2072 search_next_log_view(struct tog_view *view)
2074 const struct got_error *err = NULL;
2075 struct tog_log_view_state *s = &view->state.log;
2076 struct commit_queue_entry *entry;
2078 /* Display progress update in log view. */
2079 show_log_view(view);
2080 update_panels();
2081 doupdate();
2083 if (s->search_entry) {
2084 int errcode, ch;
2085 errcode = pthread_mutex_unlock(&tog_mutex);
2086 if (errcode)
2087 return got_error_set_errno(errcode,
2088 "pthread_mutex_unlock");
2089 ch = wgetch(view->window);
2090 errcode = pthread_mutex_lock(&tog_mutex);
2091 if (errcode)
2092 return got_error_set_errno(errcode,
2093 "pthread_mutex_lock");
2094 if (ch == KEY_BACKSPACE) {
2095 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2096 return NULL;
2098 if (view->searching == TOG_SEARCH_FORWARD)
2099 entry = TAILQ_NEXT(s->search_entry, entry);
2100 else
2101 entry = TAILQ_PREV(s->search_entry,
2102 commit_queue_head, entry);
2103 } else if (s->matched_entry) {
2104 if (view->searching == TOG_SEARCH_FORWARD)
2105 entry = TAILQ_NEXT(s->matched_entry, entry);
2106 else
2107 entry = TAILQ_PREV(s->matched_entry,
2108 commit_queue_head, entry);
2109 } else {
2110 if (view->searching == TOG_SEARCH_FORWARD)
2111 entry = TAILQ_FIRST(&s->commits.head);
2112 else
2113 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2116 while (1) {
2117 int have_match = 0;
2119 if (entry == NULL) {
2120 if (s->thread_args.log_complete ||
2121 view->searching == TOG_SEARCH_BACKWARD) {
2122 view->search_next_done =
2123 (s->matched_entry == NULL ?
2124 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2125 s->search_entry = NULL;
2126 return NULL;
2129 * Poke the log thread for more commits and return,
2130 * allowing the main loop to make progress. Search
2131 * will resume at s->search_entry once we come back.
2133 s->thread_args.commits_needed++;
2134 return trigger_log_thread(view, 0,
2135 &s->thread_args.commits_needed,
2136 &s->thread_args.log_complete,
2137 &s->thread_args.need_commits,
2138 &s->thread_args.commit_loaded);
2141 err = match_commit(&have_match, entry->id, entry->commit,
2142 &view->regex);
2143 if (err)
2144 break;
2145 if (have_match) {
2146 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2147 s->matched_entry = entry;
2148 break;
2151 s->search_entry = entry;
2152 if (view->searching == TOG_SEARCH_FORWARD)
2153 entry = TAILQ_NEXT(entry, entry);
2154 else
2155 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2158 if (s->matched_entry) {
2159 int cur = s->selected_entry->idx;
2160 while (cur < s->matched_entry->idx) {
2161 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2162 if (err)
2163 return err;
2164 cur++;
2166 while (cur > s->matched_entry->idx) {
2167 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2168 if (err)
2169 return err;
2170 cur--;
2174 s->search_entry = NULL;
2176 return NULL;
2179 static const struct got_error *
2180 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2181 struct got_reflist_head *refs, struct got_repository *repo,
2182 const char *head_ref_name, const char *in_repo_path,
2183 int log_branches)
2185 const struct got_error *err = NULL;
2186 struct tog_log_view_state *s = &view->state.log;
2187 struct got_repository *thread_repo = NULL;
2188 struct got_commit_graph *thread_graph = NULL;
2189 int errcode;
2191 if (in_repo_path != s->in_repo_path) {
2192 free(s->in_repo_path);
2193 s->in_repo_path = strdup(in_repo_path);
2194 if (s->in_repo_path == NULL)
2195 return got_error_from_errno("strdup");
2198 /* The commit queue only contains commits being displayed. */
2199 TAILQ_INIT(&s->commits.head);
2200 s->commits.ncommits = 0;
2202 s->refs = refs;
2203 s->repo = repo;
2204 s->head_ref_name = head_ref_name;
2205 s->start_id = got_object_id_dup(start_id);
2206 if (s->start_id == NULL) {
2207 err = got_error_from_errno("got_object_id_dup");
2208 goto done;
2210 s->log_branches = log_branches;
2212 SIMPLEQ_INIT(&s->colors);
2213 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2214 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2215 get_color_value("TOG_COLOR_COMMIT"));
2216 if (err)
2217 goto done;
2218 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2219 get_color_value("TOG_COLOR_AUTHOR"));
2220 if (err) {
2221 free_colors(&s->colors);
2222 goto done;
2224 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2225 get_color_value("TOG_COLOR_DATE"));
2226 if (err) {
2227 free_colors(&s->colors);
2228 goto done;
2232 view->show = show_log_view;
2233 view->input = input_log_view;
2234 view->close = close_log_view;
2235 view->search_start = search_start_log_view;
2236 view->search_next = search_next_log_view;
2238 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2239 if (err)
2240 goto done;
2241 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2242 !s->log_branches);
2243 if (err)
2244 goto done;
2245 err = got_commit_graph_iter_start(thread_graph,
2246 s->start_id, s->repo, NULL, NULL);
2247 if (err)
2248 goto done;
2250 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2251 if (errcode) {
2252 err = got_error_set_errno(errcode, "pthread_cond_init");
2253 goto done;
2255 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2256 if (errcode) {
2257 err = got_error_set_errno(errcode, "pthread_cond_init");
2258 goto done;
2261 s->thread_args.commits_needed = view->nlines;
2262 s->thread_args.graph = thread_graph;
2263 s->thread_args.commits = &s->commits;
2264 s->thread_args.in_repo_path = s->in_repo_path;
2265 s->thread_args.start_id = s->start_id;
2266 s->thread_args.repo = thread_repo;
2267 s->thread_args.log_complete = 0;
2268 s->thread_args.quit = &s->quit;
2269 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2270 s->thread_args.selected_entry = &s->selected_entry;
2271 s->thread_args.searching = &view->searching;
2272 s->thread_args.search_next_done = &view->search_next_done;
2273 s->thread_args.regex = &view->regex;
2274 done:
2275 if (err)
2276 close_log_view(view);
2277 return err;
2280 static const struct got_error *
2281 show_log_view(struct tog_view *view)
2283 struct tog_log_view_state *s = &view->state.log;
2285 if (s->thread == NULL) {
2286 int errcode = pthread_create(&s->thread, NULL, log_thread,
2287 &s->thread_args);
2288 if (errcode)
2289 return got_error_set_errno(errcode, "pthread_create");
2292 return draw_commits(view, &s->last_displayed_entry,
2293 &s->selected_entry, s->first_displayed_entry,
2294 &s->commits, s->selected, view->nlines, s->refs,
2295 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2298 static const struct got_error *
2299 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2300 struct tog_view **focus_view, struct tog_view *view, int ch)
2302 const struct got_error *err = NULL;
2303 struct tog_log_view_state *s = &view->state.log;
2304 char *parent_path, *in_repo_path = NULL;
2305 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2306 int begin_x = 0;
2307 struct got_object_id *start_id;
2309 switch (ch) {
2310 case 'q':
2311 s->quit = 1;
2312 break;
2313 case 'k':
2314 case KEY_UP:
2315 case '<':
2316 case ',':
2317 if (s->first_displayed_entry == NULL)
2318 break;
2319 if (s->selected > 0)
2320 s->selected--;
2321 else
2322 scroll_up(view, &s->first_displayed_entry, 1,
2323 &s->commits);
2324 break;
2325 case KEY_PPAGE:
2326 case CTRL('b'):
2327 if (s->first_displayed_entry == NULL)
2328 break;
2329 if (TAILQ_FIRST(&s->commits.head) ==
2330 s->first_displayed_entry) {
2331 s->selected = 0;
2332 break;
2334 scroll_up(view, &s->first_displayed_entry,
2335 view->nlines - 1, &s->commits);
2336 break;
2337 case 'j':
2338 case KEY_DOWN:
2339 case '>':
2340 case '.':
2341 if (s->first_displayed_entry == NULL)
2342 break;
2343 if (s->selected < MIN(view->nlines - 2,
2344 s->commits.ncommits - 1)) {
2345 s->selected++;
2346 break;
2348 err = scroll_down(view, &s->first_displayed_entry, 1,
2349 &s->last_displayed_entry, &s->commits,
2350 &s->thread_args.log_complete,
2351 &s->thread_args.commits_needed,
2352 &s->thread_args.need_commits,
2353 &s->thread_args.commit_loaded);
2354 break;
2355 case KEY_NPAGE:
2356 case CTRL('f'): {
2357 struct commit_queue_entry *first;
2358 first = s->first_displayed_entry;
2359 if (first == NULL)
2360 break;
2361 err = scroll_down(view, &s->first_displayed_entry,
2362 view->nlines - 1, &s->last_displayed_entry,
2363 &s->commits, &s->thread_args.log_complete,
2364 &s->thread_args.commits_needed,
2365 &s->thread_args.need_commits,
2366 &s->thread_args.commit_loaded);
2367 if (err)
2368 break;
2369 if (first == s->first_displayed_entry &&
2370 s->selected < MIN(view->nlines - 2,
2371 s->commits.ncommits - 1)) {
2372 /* can't scroll further down */
2373 s->selected = MIN(view->nlines - 2,
2374 s->commits.ncommits - 1);
2376 err = NULL;
2377 break;
2379 case KEY_RESIZE:
2380 if (s->selected > view->nlines - 2)
2381 s->selected = view->nlines - 2;
2382 if (s->selected > s->commits.ncommits - 1)
2383 s->selected = s->commits.ncommits - 1;
2384 break;
2385 case KEY_ENTER:
2386 case ' ':
2387 case '\r':
2388 if (s->selected_entry == NULL)
2389 break;
2390 if (view_is_parent_view(view))
2391 begin_x = view_split_begin_x(view->begin_x);
2392 err = open_diff_view_for_commit(&diff_view, begin_x,
2393 s->selected_entry->commit, s->selected_entry->id,
2394 view, s->refs, s->repo);
2395 if (err)
2396 break;
2397 if (view_is_parent_view(view)) {
2398 err = view_close_child(view);
2399 if (err)
2400 return err;
2401 err = view_set_child(view, diff_view);
2402 if (err) {
2403 view_close(diff_view);
2404 break;
2406 *focus_view = diff_view;
2407 view->child_focussed = 1;
2408 } else
2409 *new_view = diff_view;
2410 break;
2411 case 't':
2412 if (s->selected_entry == NULL)
2413 break;
2414 if (view_is_parent_view(view))
2415 begin_x = view_split_begin_x(view->begin_x);
2416 err = browse_commit_tree(&tree_view, begin_x,
2417 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2418 if (err)
2419 break;
2420 if (view_is_parent_view(view)) {
2421 err = view_close_child(view);
2422 if (err)
2423 return err;
2424 err = view_set_child(view, tree_view);
2425 if (err) {
2426 view_close(tree_view);
2427 break;
2429 *focus_view = tree_view;
2430 view->child_focussed = 1;
2431 } else
2432 *new_view = tree_view;
2433 break;
2434 case KEY_BACKSPACE:
2435 if (strcmp(s->in_repo_path, "/") == 0)
2436 break;
2437 parent_path = dirname(s->in_repo_path);
2438 if (parent_path && strcmp(parent_path, ".") != 0) {
2439 err = stop_log_thread(s);
2440 if (err)
2441 return err;
2442 lv = view_open(view->nlines, view->ncols,
2443 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2444 if (lv == NULL)
2445 return got_error_from_errno(
2446 "view_open");
2447 err = open_log_view(lv, s->start_id, s->refs,
2448 s->repo, s->head_ref_name, parent_path,
2449 s->log_branches);
2450 if (err)
2451 return err;;
2452 if (view_is_parent_view(view))
2453 *new_view = lv;
2454 else {
2455 view_set_child(view->parent, lv);
2456 *focus_view = lv;
2458 return NULL;
2460 break;
2461 case CTRL('l'):
2462 err = stop_log_thread(s);
2463 if (err)
2464 return err;
2465 lv = view_open(view->nlines, view->ncols,
2466 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2467 if (lv == NULL)
2468 return got_error_from_errno("view_open");
2469 err = got_repo_match_object_id(&start_id, NULL,
2470 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2471 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2472 if (err) {
2473 view_close(lv);
2474 return err;
2476 in_repo_path = strdup(s->in_repo_path);
2477 if (in_repo_path == NULL) {
2478 free(start_id);
2479 view_close(lv);
2480 return got_error_from_errno("strdup");
2482 got_ref_list_free(s->refs);
2483 err = got_ref_list(s->refs, s->repo, NULL,
2484 got_ref_cmp_by_name, NULL);
2485 if (err) {
2486 free(start_id);
2487 view_close(lv);
2488 return err;
2490 err = open_log_view(lv, start_id, s->refs, s->repo,
2491 s->head_ref_name, in_repo_path, s->log_branches);
2492 if (err) {
2493 free(start_id);
2494 view_close(lv);
2495 return err;;
2497 *dead_view = view;
2498 *new_view = lv;
2499 break;
2500 case 'B':
2501 s->log_branches = !s->log_branches;
2502 err = stop_log_thread(s);
2503 if (err)
2504 return err;
2505 lv = view_open(view->nlines, view->ncols,
2506 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2507 if (lv == NULL)
2508 return got_error_from_errno("view_open");
2509 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2510 s->head_ref_name, s->in_repo_path, s->log_branches);
2511 if (err) {
2512 view_close(lv);
2513 return err;;
2515 *dead_view = view;
2516 *new_view = lv;
2517 break;
2518 default:
2519 break;
2522 return err;
2525 static const struct got_error *
2526 apply_unveil(const char *repo_path, const char *worktree_path)
2528 const struct got_error *error;
2530 #ifdef PROFILE
2531 if (unveil("gmon.out", "rwc") != 0)
2532 return got_error_from_errno2("unveil", "gmon.out");
2533 #endif
2534 if (repo_path && unveil(repo_path, "r") != 0)
2535 return got_error_from_errno2("unveil", repo_path);
2537 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2538 return got_error_from_errno2("unveil", worktree_path);
2540 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2541 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2543 error = got_privsep_unveil_exec_helpers();
2544 if (error != NULL)
2545 return error;
2547 if (unveil(NULL, NULL) != 0)
2548 return got_error_from_errno("unveil");
2550 return NULL;
2553 static void
2554 init_curses(void)
2556 initscr();
2557 cbreak();
2558 halfdelay(1); /* Do fast refresh while initial view is loading. */
2559 noecho();
2560 nonl();
2561 intrflush(stdscr, FALSE);
2562 keypad(stdscr, TRUE);
2563 curs_set(0);
2564 if (getenv("TOG_COLORS") != NULL) {
2565 start_color();
2566 use_default_colors();
2568 signal(SIGWINCH, tog_sigwinch);
2569 signal(SIGPIPE, tog_sigpipe);
2570 signal(SIGCONT, tog_sigcont);
2573 static const struct got_error *
2574 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2575 struct got_repository *repo, struct got_worktree *worktree)
2577 const struct got_error *err = NULL;
2579 if (argc == 0) {
2580 *in_repo_path = strdup("/");
2581 if (*in_repo_path == NULL)
2582 return got_error_from_errno("strdup");
2583 return NULL;
2586 if (worktree) {
2587 const char *prefix = got_worktree_get_path_prefix(worktree);
2588 char *wt_path, *p;
2590 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2591 if (err)
2592 return err;
2594 if (asprintf(&p, "%s%s%s", prefix,
2595 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2596 err = got_error_from_errno("asprintf");
2597 free(wt_path);
2598 return err;
2600 err = got_repo_map_path(in_repo_path, repo, p, 0);
2601 free(p);
2602 free(wt_path);
2603 } else
2604 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2606 return err;
2609 static const struct got_error *
2610 cmd_log(int argc, char *argv[])
2612 const struct got_error *error;
2613 struct got_repository *repo = NULL;
2614 struct got_worktree *worktree = NULL;
2615 struct got_reflist_head refs;
2616 struct got_object_id *start_id = NULL;
2617 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2618 char *start_commit = NULL, *head_ref_name = NULL;
2619 int ch, log_branches = 0;
2620 struct tog_view *view;
2622 SIMPLEQ_INIT(&refs);
2624 #ifndef PROFILE
2625 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2626 NULL) == -1)
2627 err(1, "pledge");
2628 #endif
2630 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2631 switch (ch) {
2632 case 'b':
2633 log_branches = 1;
2634 break;
2635 case 'c':
2636 start_commit = optarg;
2637 break;
2638 case 'r':
2639 repo_path = realpath(optarg, NULL);
2640 if (repo_path == NULL)
2641 return got_error_from_errno2("realpath",
2642 optarg);
2643 break;
2644 default:
2645 usage_log();
2646 /* NOTREACHED */
2650 argc -= optind;
2651 argv += optind;
2653 if (argc > 1)
2654 usage_log();
2656 cwd = getcwd(NULL, 0);
2657 if (cwd == NULL)
2658 return got_error_from_errno("getcwd");
2660 error = got_worktree_open(&worktree, cwd);
2661 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2662 goto done;
2664 if (repo_path == NULL) {
2665 if (worktree)
2666 repo_path =
2667 strdup(got_worktree_get_repo_path(worktree));
2668 else
2669 repo_path = strdup(cwd);
2671 if (repo_path == NULL) {
2672 error = got_error_from_errno("strdup");
2673 goto done;
2676 error = got_repo_open(&repo, repo_path, NULL);
2677 if (error != NULL)
2678 goto done;
2680 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2681 repo, worktree);
2682 if (error)
2683 goto done;
2685 init_curses();
2687 error = apply_unveil(got_repo_get_path(repo),
2688 worktree ? got_worktree_get_root_path(worktree) : NULL);
2689 if (error)
2690 goto done;
2692 if (start_commit == NULL)
2693 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2694 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2695 GOT_OBJ_TYPE_COMMIT, 1, repo);
2696 else
2697 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2698 GOT_OBJ_TYPE_COMMIT, 1, repo);
2699 if (error != NULL)
2700 goto done;
2702 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2703 if (error)
2704 goto done;
2706 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2707 if (view == NULL) {
2708 error = got_error_from_errno("view_open");
2709 goto done;
2711 if (worktree) {
2712 head_ref_name = strdup(
2713 got_worktree_get_head_ref_name(worktree));
2714 if (head_ref_name == NULL) {
2715 error = got_error_from_errno("strdup");
2716 goto done;
2719 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2720 in_repo_path, log_branches);
2721 if (error)
2722 goto done;
2723 if (worktree) {
2724 /* Release work tree lock. */
2725 got_worktree_close(worktree);
2726 worktree = NULL;
2728 error = view_loop(view);
2729 done:
2730 free(in_repo_path);
2731 free(repo_path);
2732 free(cwd);
2733 free(start_id);
2734 free(head_ref_name);
2735 if (repo)
2736 got_repo_close(repo);
2737 if (worktree)
2738 got_worktree_close(worktree);
2739 got_ref_list_free(&refs);
2740 return error;
2743 __dead static void
2744 usage_diff(void)
2746 endwin();
2747 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2748 getprogname());
2749 exit(1);
2752 static char *
2753 parse_next_line(FILE *f, size_t *len)
2755 char *line;
2756 size_t linelen;
2757 size_t lineno;
2758 const char delim[3] = { '\0', '\0', '\0'};
2760 line = fparseln(f, &linelen, &lineno, delim, 0);
2761 if (len)
2762 *len = linelen;
2763 return line;
2766 static int
2767 match_line(const char *line, regex_t *regex)
2769 regmatch_t regmatch;
2771 return regexec(regex, line, 1, &regmatch, 0) == 0;
2774 struct tog_color *
2775 match_color(struct tog_colors *colors, const char *line)
2777 struct tog_color *tc = NULL;
2779 SIMPLEQ_FOREACH(tc, colors, entry) {
2780 if (match_line(line, &tc->regex))
2781 return tc;
2784 return NULL;
2787 static const struct got_error *
2788 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2789 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2790 char *header, struct tog_colors *colors)
2792 const struct got_error *err;
2793 int lineno = 0, nprinted = 0;
2794 char *line;
2795 struct tog_color *tc;
2796 size_t len;
2797 wchar_t *wline;
2798 int width;
2800 rewind(f);
2801 werase(view->window);
2803 if (header) {
2804 err = format_line(&wline, &width, header, view->ncols, 0);
2805 if (err) {
2806 return err;
2809 if (view_needs_focus_indication(view))
2810 wstandout(view->window);
2811 waddwstr(view->window, wline);
2812 if (view_needs_focus_indication(view))
2813 wstandend(view->window);
2814 if (width <= view->ncols - 1)
2815 waddch(view->window, '\n');
2817 if (max_lines <= 1)
2818 return NULL;
2819 max_lines--;
2822 *eof = 0;
2823 while (nprinted < max_lines) {
2824 line = parse_next_line(f, &len);
2825 if (line == NULL) {
2826 *eof = 1;
2827 break;
2829 if (++lineno < *first_displayed_line) {
2830 free(line);
2831 continue;
2834 err = format_line(&wline, &width, line, view->ncols, 0);
2835 if (err) {
2836 free(line);
2837 return err;
2840 tc = match_color(colors, line);
2841 if (tc)
2842 wattr_on(view->window,
2843 COLOR_PAIR(tc->colorpair), NULL);
2844 waddwstr(view->window, wline);
2845 if (tc)
2846 wattr_off(view->window,
2847 COLOR_PAIR(tc->colorpair), NULL);
2848 if (width <= view->ncols - 1)
2849 waddch(view->window, '\n');
2850 if (++nprinted == 1)
2851 *first_displayed_line = lineno;
2852 free(line);
2853 free(wline);
2854 wline = NULL;
2856 *last_displayed_line = lineno;
2858 view_vborder(view);
2860 if (*eof) {
2861 while (nprinted < view->nlines) {
2862 waddch(view->window, '\n');
2863 nprinted++;
2866 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2867 if (err) {
2868 return err;
2871 wstandout(view->window);
2872 waddwstr(view->window, wline);
2873 wstandend(view->window);
2876 return NULL;
2879 static char *
2880 get_datestr(time_t *time, char *datebuf)
2882 struct tm mytm, *tm;
2883 char *p, *s;
2885 tm = gmtime_r(time, &mytm);
2886 if (tm == NULL)
2887 return NULL;
2888 s = asctime_r(tm, datebuf);
2889 if (s == NULL)
2890 return NULL;
2891 p = strchr(s, '\n');
2892 if (p)
2893 *p = '\0';
2894 return s;
2897 static const struct got_error *
2898 get_changed_paths(struct got_pathlist_head *paths,
2899 struct got_commit_object *commit, struct got_repository *repo)
2901 const struct got_error *err = NULL;
2902 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2903 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2904 struct got_object_qid *qid;
2906 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2907 if (qid != NULL) {
2908 struct got_commit_object *pcommit;
2909 err = got_object_open_as_commit(&pcommit, repo,
2910 qid->id);
2911 if (err)
2912 return err;
2914 tree_id1 = got_object_commit_get_tree_id(pcommit);
2915 got_object_commit_close(pcommit);
2919 if (tree_id1) {
2920 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2921 if (err)
2922 goto done;
2925 tree_id2 = got_object_commit_get_tree_id(commit);
2926 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2927 if (err)
2928 goto done;
2930 err = got_diff_tree(tree1, tree2, "", "", repo,
2931 got_diff_tree_collect_changed_paths, paths, 0);
2932 done:
2933 if (tree1)
2934 got_object_tree_close(tree1);
2935 if (tree2)
2936 got_object_tree_close(tree2);
2937 return err;
2940 static const struct got_error *
2941 write_commit_info(struct got_object_id *commit_id,
2942 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2944 const struct got_error *err = NULL;
2945 char datebuf[26], *datestr;
2946 struct got_commit_object *commit;
2947 char *id_str = NULL, *logmsg = NULL;
2948 time_t committer_time;
2949 const char *author, *committer;
2950 char *refs_str = NULL;
2951 struct got_pathlist_head changed_paths;
2952 struct got_pathlist_entry *pe;
2954 TAILQ_INIT(&changed_paths);
2956 if (refs) {
2957 err = build_refs_str(&refs_str, refs, commit_id, repo);
2958 if (err)
2959 return err;
2962 err = got_object_open_as_commit(&commit, repo, commit_id);
2963 if (err)
2964 return err;
2966 err = got_object_id_str(&id_str, commit_id);
2967 if (err) {
2968 err = got_error_from_errno("got_object_id_str");
2969 goto done;
2972 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2973 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2974 err = got_error_from_errno("fprintf");
2975 goto done;
2977 if (fprintf(outfile, "from: %s\n",
2978 got_object_commit_get_author(commit)) < 0) {
2979 err = got_error_from_errno("fprintf");
2980 goto done;
2982 committer_time = got_object_commit_get_committer_time(commit);
2983 datestr = get_datestr(&committer_time, datebuf);
2984 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2985 err = got_error_from_errno("fprintf");
2986 goto done;
2988 author = got_object_commit_get_author(commit);
2989 committer = got_object_commit_get_committer(commit);
2990 if (strcmp(author, committer) != 0 &&
2991 fprintf(outfile, "via: %s\n", committer) < 0) {
2992 err = got_error_from_errno("fprintf");
2993 goto done;
2995 err = got_object_commit_get_logmsg(&logmsg, commit);
2996 if (err)
2997 goto done;
2998 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2999 err = got_error_from_errno("fprintf");
3000 goto done;
3002 err = get_changed_paths(&changed_paths, commit, repo);
3003 if (err)
3004 goto done;
3005 TAILQ_FOREACH(pe, &changed_paths, entry) {
3006 struct got_diff_changed_path *cp = pe->data;
3007 fprintf(outfile, "%c %s\n", cp->status, pe->path);
3008 free((char *)pe->path);
3009 free(pe->data);
3011 fputc('\n', outfile);
3012 done:
3013 got_pathlist_free(&changed_paths);
3014 free(id_str);
3015 free(logmsg);
3016 free(refs_str);
3017 got_object_commit_close(commit);
3018 return err;
3021 const struct got_error *
3022 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
3023 FILE *infile)
3025 size_t len;
3026 char *buf = NULL;
3027 int i;
3028 size_t noffsets = 0;
3029 off_t off = 0;
3031 if (line_offsets)
3032 *line_offsets = NULL;
3033 if (filesize)
3034 *filesize = 0;
3035 if (nlines)
3036 *nlines = 0;
3038 if (fseek(infile, 0, SEEK_END) == -1)
3039 return got_error_from_errno("fseek");
3040 len = ftell(infile) + 1;
3041 if (ferror(infile))
3042 return got_error_from_errno("ftell");
3043 if (fseek(infile, 0, SEEK_SET) == -1)
3044 return got_error_from_errno("fseek");
3046 if (len == 0)
3047 return NULL;
3048 if ((buf = calloc(len, sizeof(char *))) == NULL)
3049 return got_error_from_errno("calloc");
3051 fread(buf, 1, len, infile);
3052 if (ferror(infile))
3053 return got_error_from_errno("fread");
3055 i = 0;
3056 if (line_offsets && nlines) {
3057 if (*line_offsets == NULL) {
3058 /* Have some data but perhaps no '\n'. */
3059 noffsets = 1;
3060 *nlines = 1;
3061 *line_offsets = calloc(1, sizeof(**line_offsets));
3062 if (*line_offsets == NULL)
3063 return got_error_from_errno("calloc");
3064 /* Skip forward over end of first line. */
3065 while (i < len) {
3066 if (buf[i] == '\n')
3067 break;
3068 i++;
3071 /* Scan '\n' offsets in remaining chunk of data. */
3072 while (i < len) {
3073 if (buf[i] != '\n') {
3074 i++;
3075 continue;
3077 (*nlines)++;
3078 if (noffsets < *nlines) {
3079 off_t *o = recallocarray(*line_offsets,
3080 noffsets, *nlines,
3081 sizeof(**line_offsets));
3082 if (o == NULL) {
3083 free(*line_offsets);
3084 *line_offsets = NULL;
3085 return got_error_from_errno(
3086 "recallocarray");
3088 *line_offsets = o;
3089 noffsets = *nlines;
3091 off = i + 1;
3092 (*line_offsets)[*nlines - 1] = off;
3093 i++;
3097 if (fflush(infile) != 0)
3098 return got_error_from_errno("fflush");
3099 rewind(infile);
3101 if (filesize)
3102 *filesize = len;
3104 return NULL;
3107 static const struct got_error *
3108 create_diff(struct tog_diff_view_state *s)
3110 const struct got_error *err = NULL;
3111 FILE *f = NULL;
3112 int obj_type;
3114 f = got_opentemp();
3115 if (f == NULL) {
3116 err = got_error_from_errno("got_opentemp");
3117 goto done;
3119 if (s->f && fclose(s->f) != 0) {
3120 err = got_error_from_errno("fclose");
3121 goto done;
3123 s->f = f;
3125 if (s->id1)
3126 err = got_object_get_type(&obj_type, s->repo, s->id1);
3127 else
3128 err = got_object_get_type(&obj_type, s->repo, s->id2);
3129 if (err)
3130 goto done;
3132 switch (obj_type) {
3133 case GOT_OBJ_TYPE_BLOB:
3134 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3135 s->diff_context, 0, s->repo, s->f);
3136 break;
3137 case GOT_OBJ_TYPE_TREE:
3138 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3139 s->diff_context, 0, s->repo, s->f);
3140 break;
3141 case GOT_OBJ_TYPE_COMMIT: {
3142 const struct got_object_id_queue *parent_ids;
3143 struct got_object_qid *pid;
3144 struct got_commit_object *commit2;
3146 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3147 if (err)
3148 goto done;
3149 /* Show commit info if we're diffing to a parent/root commit. */
3150 if (s->id1 == NULL) {
3151 err = write_commit_info(s->id2, s->refs, s->repo, s->f);
3152 if (err)
3153 goto done;
3154 } else {
3155 parent_ids = got_object_commit_get_parent_ids(commit2);
3156 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3157 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3158 err = write_commit_info(s->id2, s->refs,
3159 s->repo, s->f);
3160 if (err)
3161 goto done;
3162 break;
3166 got_object_commit_close(commit2);
3168 err = got_diff_objects_as_commits(s->id1, s->id2,
3169 s->diff_context, 0, s->repo, s->f);
3170 break;
3172 default:
3173 err = got_error(GOT_ERR_OBJ_TYPE);
3174 break;
3176 if (err)
3177 goto done;
3178 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3179 s->f);
3180 done:
3181 if (s->f && fflush(s->f) != 0 && err == NULL)
3182 err = got_error_from_errno("fflush");
3183 return err;
3186 static void
3187 diff_view_indicate_progress(struct tog_view *view)
3189 mvwaddstr(view->window, 0, 0, "diffing...");
3190 update_panels();
3191 doupdate();
3194 static const struct got_error *
3195 search_start_diff_view(struct tog_view *view)
3197 struct tog_diff_view_state *s = &view->state.diff;
3199 s->matched_line = 0;
3200 return NULL;
3203 static const struct got_error *
3204 search_next_diff_view(struct tog_view *view)
3206 struct tog_diff_view_state *s = &view->state.diff;
3207 int lineno;
3209 if (!view->searching) {
3210 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3211 return NULL;
3214 if (s->matched_line) {
3215 if (view->searching == TOG_SEARCH_FORWARD)
3216 lineno = s->matched_line + 1;
3217 else
3218 lineno = s->matched_line - 1;
3219 } else {
3220 if (view->searching == TOG_SEARCH_FORWARD)
3221 lineno = 1;
3222 else
3223 lineno = s->nlines;
3226 while (1) {
3227 char *line = NULL;
3228 off_t offset;
3229 size_t len;
3231 if (lineno <= 0 || lineno > s->nlines) {
3232 if (s->matched_line == 0) {
3233 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3234 free(line);
3235 break;
3238 if (view->searching == TOG_SEARCH_FORWARD)
3239 lineno = 1;
3240 else
3241 lineno = s->nlines;
3244 offset = s->line_offsets[lineno - 1];
3245 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3246 free(line);
3247 return got_error_from_errno("fseeko");
3249 free(line);
3250 line = parse_next_line(s->f, &len);
3251 if (line && match_line(line, &view->regex)) {
3252 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3253 s->matched_line = lineno;
3254 free(line);
3255 break;
3257 free(line);
3258 if (view->searching == TOG_SEARCH_FORWARD)
3259 lineno++;
3260 else
3261 lineno--;
3264 if (s->matched_line) {
3265 s->first_displayed_line = s->matched_line;
3266 s->selected_line = 1;
3269 return NULL;
3272 static const struct got_error *
3273 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3274 struct got_object_id *id2, struct tog_view *log_view,
3275 struct got_reflist_head *refs, struct got_repository *repo)
3277 const struct got_error *err;
3278 struct tog_diff_view_state *s = &view->state.diff;
3280 if (id1 != NULL && id2 != NULL) {
3281 int type1, type2;
3282 err = got_object_get_type(&type1, repo, id1);
3283 if (err)
3284 return err;
3285 err = got_object_get_type(&type2, repo, id2);
3286 if (err)
3287 return err;
3289 if (type1 != type2)
3290 return got_error(GOT_ERR_OBJ_TYPE);
3292 s->first_displayed_line = 1;
3293 s->last_displayed_line = view->nlines;
3294 s->selected_line = 1;
3295 s->repo = repo;
3296 s->refs = refs;
3297 s->id1 = id1;
3298 s->id2 = id2;
3300 if (id1) {
3301 s->id1 = got_object_id_dup(id1);
3302 if (s->id1 == NULL)
3303 return got_error_from_errno("got_object_id_dup");
3304 } else
3305 s->id1 = NULL;
3307 s->id2 = got_object_id_dup(id2);
3308 if (s->id2 == NULL) {
3309 free(s->id1);
3310 s->id1 = NULL;
3311 return got_error_from_errno("got_object_id_dup");
3313 s->f = NULL;
3314 s->first_displayed_line = 1;
3315 s->last_displayed_line = view->nlines;
3316 s->diff_context = 3;
3317 s->log_view = log_view;
3318 s->repo = repo;
3319 s->refs = refs;
3321 SIMPLEQ_INIT(&s->colors);
3322 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3323 err = add_color(&s->colors,
3324 "^-", TOG_COLOR_DIFF_MINUS,
3325 get_color_value("TOG_COLOR_DIFF_MINUS"));
3326 if (err)
3327 return err;
3328 err = add_color(&s->colors, "^\\+",
3329 TOG_COLOR_DIFF_PLUS,
3330 get_color_value("TOG_COLOR_DIFF_PLUS"));
3331 if (err) {
3332 free_colors(&s->colors);
3333 return err;
3335 err = add_color(&s->colors,
3336 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3337 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3338 if (err) {
3339 free_colors(&s->colors);
3340 return err;
3343 err = add_color(&s->colors,
3344 "^(commit|(blob|file) [-+] |[MDmA] [^ ])",
3345 TOG_COLOR_DIFF_META,
3346 get_color_value("TOG_COLOR_DIFF_META"));
3347 if (err) {
3348 free_colors(&s->colors);
3349 return err;
3352 err = add_color(&s->colors,
3353 "^(from|via): ", TOG_COLOR_AUTHOR,
3354 get_color_value("TOG_COLOR_AUTHOR"));
3355 if (err) {
3356 free_colors(&s->colors);
3357 return err;
3360 err = add_color(&s->colors,
3361 "^date: ", TOG_COLOR_DATE,
3362 get_color_value("TOG_COLOR_DATE"));
3363 if (err) {
3364 free_colors(&s->colors);
3365 return err;
3369 if (log_view && view_is_splitscreen(view))
3370 show_log_view(log_view); /* draw vborder */
3371 diff_view_indicate_progress(view);
3373 err = create_diff(s);
3374 if (err) {
3375 free(s->id1);
3376 s->id1 = NULL;
3377 free(s->id2);
3378 s->id2 = NULL;
3379 return err;
3382 view->show = show_diff_view;
3383 view->input = input_diff_view;
3384 view->close = close_diff_view;
3385 view->search_start = search_start_diff_view;
3386 view->search_next = search_next_diff_view;
3388 return NULL;
3391 static const struct got_error *
3392 close_diff_view(struct tog_view *view)
3394 const struct got_error *err = NULL;
3395 struct tog_diff_view_state *s = &view->state.diff;
3397 free(s->id1);
3398 s->id1 = NULL;
3399 free(s->id2);
3400 s->id2 = NULL;
3401 if (s->f && fclose(s->f) == EOF)
3402 err = got_error_from_errno("fclose");
3403 free_colors(&s->colors);
3404 free(s->line_offsets);
3405 return err;
3408 static const struct got_error *
3409 show_diff_view(struct tog_view *view)
3411 const struct got_error *err;
3412 struct tog_diff_view_state *s = &view->state.diff;
3413 char *id_str1 = NULL, *id_str2, *header;
3415 if (s->id1) {
3416 err = got_object_id_str(&id_str1, s->id1);
3417 if (err)
3418 return err;
3420 err = got_object_id_str(&id_str2, s->id2);
3421 if (err)
3422 return err;
3424 if (asprintf(&header, "diff %s %s",
3425 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3426 err = got_error_from_errno("asprintf");
3427 free(id_str1);
3428 free(id_str2);
3429 return err;
3431 free(id_str1);
3432 free(id_str2);
3434 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3435 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3436 header, &s->colors);
3439 static const struct got_error *
3440 set_selected_commit(struct tog_diff_view_state *s,
3441 struct commit_queue_entry *entry)
3443 const struct got_error *err;
3444 const struct got_object_id_queue *parent_ids;
3445 struct got_commit_object *selected_commit;
3446 struct got_object_qid *pid;
3448 free(s->id2);
3449 s->id2 = got_object_id_dup(entry->id);
3450 if (s->id2 == NULL)
3451 return got_error_from_errno("got_object_id_dup");
3453 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3454 if (err)
3455 return err;
3456 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3457 free(s->id1);
3458 pid = SIMPLEQ_FIRST(parent_ids);
3459 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3460 got_object_commit_close(selected_commit);
3461 return NULL;
3464 static const struct got_error *
3465 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3466 struct tog_view **focus_view, struct tog_view *view, int ch)
3468 const struct got_error *err = NULL;
3469 struct tog_diff_view_state *s = &view->state.diff;
3470 struct tog_log_view_state *ls;
3471 struct commit_queue_entry *entry;
3472 int i;
3474 switch (ch) {
3475 case 'k':
3476 case KEY_UP:
3477 if (s->first_displayed_line > 1)
3478 s->first_displayed_line--;
3479 break;
3480 case KEY_PPAGE:
3481 case CTRL('b'):
3482 if (s->first_displayed_line == 1)
3483 break;
3484 i = 0;
3485 while (i++ < view->nlines - 1 &&
3486 s->first_displayed_line > 1)
3487 s->first_displayed_line--;
3488 break;
3489 case 'j':
3490 case KEY_DOWN:
3491 if (!s->eof)
3492 s->first_displayed_line++;
3493 break;
3494 case KEY_NPAGE:
3495 case CTRL('f'):
3496 case ' ':
3497 if (s->eof)
3498 break;
3499 i = 0;
3500 while (!s->eof && i++ < view->nlines - 1) {
3501 char *line;
3502 line = parse_next_line(s->f, NULL);
3503 s->first_displayed_line++;
3504 if (line == NULL)
3505 break;
3507 break;
3508 case '[':
3509 if (s->diff_context > 0) {
3510 s->diff_context--;
3511 diff_view_indicate_progress(view);
3512 err = create_diff(s);
3514 break;
3515 case ']':
3516 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3517 s->diff_context++;
3518 diff_view_indicate_progress(view);
3519 err = create_diff(s);
3521 break;
3522 case '<':
3523 case ',':
3524 if (s->log_view == NULL)
3525 break;
3526 ls = &s->log_view->state.log;
3527 entry = TAILQ_PREV(ls->selected_entry,
3528 commit_queue_head, entry);
3529 if (entry == NULL)
3530 break;
3532 err = input_log_view(NULL, NULL, NULL, s->log_view,
3533 KEY_UP);
3534 if (err)
3535 break;
3537 err = set_selected_commit(s, entry);
3538 if (err)
3539 break;
3541 s->first_displayed_line = 1;
3542 s->last_displayed_line = view->nlines;
3544 diff_view_indicate_progress(view);
3545 err = create_diff(s);
3546 break;
3547 case '>':
3548 case '.':
3549 if (s->log_view == NULL)
3550 break;
3551 ls = &s->log_view->state.log;
3553 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3554 ls->thread_args.commits_needed++;
3555 err = trigger_log_thread(s->log_view, 1,
3556 &ls->thread_args.commits_needed,
3557 &ls->thread_args.log_complete,
3558 &ls->thread_args.need_commits,
3559 &ls->thread_args.commit_loaded);
3560 if (err)
3561 break;
3563 err = input_log_view(NULL, NULL, NULL, s->log_view,
3564 KEY_DOWN);
3565 if (err)
3566 break;
3568 entry = TAILQ_NEXT(ls->selected_entry, entry);
3569 if (entry == NULL)
3570 break;
3572 err = set_selected_commit(s, entry);
3573 if (err)
3574 break;
3576 s->first_displayed_line = 1;
3577 s->last_displayed_line = view->nlines;
3579 diff_view_indicate_progress(view);
3580 err = create_diff(s);
3581 break;
3582 default:
3583 break;
3586 return err;
3589 static const struct got_error *
3590 cmd_diff(int argc, char *argv[])
3592 const struct got_error *error = NULL;
3593 struct got_repository *repo = NULL;
3594 struct got_worktree *worktree = NULL;
3595 struct got_reflist_head refs;
3596 struct got_object_id *id1 = NULL, *id2 = NULL;
3597 char *repo_path = NULL, *cwd = NULL;
3598 char *id_str1 = NULL, *id_str2 = NULL;
3599 int ch;
3600 struct tog_view *view;
3602 SIMPLEQ_INIT(&refs);
3604 #ifndef PROFILE
3605 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3606 NULL) == -1)
3607 err(1, "pledge");
3608 #endif
3610 while ((ch = getopt(argc, argv, "r:")) != -1) {
3611 switch (ch) {
3612 case 'r':
3613 repo_path = realpath(optarg, NULL);
3614 if (repo_path == NULL)
3615 return got_error_from_errno2("realpath",
3616 optarg);
3617 break;
3618 default:
3619 usage_diff();
3620 /* NOTREACHED */
3624 argc -= optind;
3625 argv += optind;
3627 if (argc == 0) {
3628 usage_diff(); /* TODO show local worktree changes */
3629 } else if (argc == 2) {
3630 id_str1 = argv[0];
3631 id_str2 = argv[1];
3632 } else
3633 usage_diff();
3635 cwd = getcwd(NULL, 0);
3636 if (cwd == NULL)
3637 return got_error_from_errno("getcwd");
3639 error = got_worktree_open(&worktree, cwd);
3640 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3641 goto done;
3643 if (repo_path == NULL) {
3644 if (worktree)
3645 repo_path =
3646 strdup(got_worktree_get_repo_path(worktree));
3647 else
3648 repo_path = strdup(cwd);
3650 if (repo_path == NULL) {
3651 error = got_error_from_errno("strdup");
3652 goto done;
3655 error = got_repo_open(&repo, repo_path, NULL);
3656 if (error)
3657 goto done;
3659 init_curses();
3661 error = apply_unveil(got_repo_get_path(repo), NULL);
3662 if (error)
3663 goto done;
3665 error = got_repo_match_object_id_prefix(&id1, id_str1,
3666 GOT_OBJ_TYPE_ANY, repo);
3667 if (error)
3668 goto done;
3670 error = got_repo_match_object_id_prefix(&id2, id_str2,
3671 GOT_OBJ_TYPE_ANY, repo);
3672 if (error)
3673 goto done;
3675 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3676 if (error)
3677 goto done;
3679 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3680 if (view == NULL) {
3681 error = got_error_from_errno("view_open");
3682 goto done;
3684 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3685 if (error)
3686 goto done;
3687 error = view_loop(view);
3688 done:
3689 free(repo_path);
3690 free(cwd);
3691 if (repo)
3692 got_repo_close(repo);
3693 if (worktree)
3694 got_worktree_close(worktree);
3695 got_ref_list_free(&refs);
3696 return error;
3699 __dead static void
3700 usage_blame(void)
3702 endwin();
3703 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3704 getprogname());
3705 exit(1);
3708 struct tog_blame_line {
3709 int annotated;
3710 struct got_object_id *id;
3713 static const struct got_error *
3714 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3715 const char *path, struct tog_blame_line *lines, int nlines,
3716 int blame_complete, int selected_line, int *first_displayed_line,
3717 int *last_displayed_line, int *eof, int max_lines,
3718 struct tog_colors *colors)
3720 const struct got_error *err;
3721 int lineno = 0, nprinted = 0;
3722 char *line;
3723 size_t len;
3724 wchar_t *wline;
3725 int width;
3726 struct tog_blame_line *blame_line;
3727 struct got_object_id *prev_id = NULL;
3728 char *id_str;
3729 struct tog_color *tc;
3731 err = got_object_id_str(&id_str, id);
3732 if (err)
3733 return err;
3735 rewind(f);
3736 werase(view->window);
3738 if (asprintf(&line, "commit %s", id_str) == -1) {
3739 err = got_error_from_errno("asprintf");
3740 free(id_str);
3741 return err;
3744 err = format_line(&wline, &width, line, view->ncols, 0);
3745 free(line);
3746 line = NULL;
3747 if (err)
3748 return err;
3749 if (view_needs_focus_indication(view))
3750 wstandout(view->window);
3751 tc = get_color(colors, TOG_COLOR_COMMIT);
3752 if (tc)
3753 wattr_on(view->window,
3754 COLOR_PAIR(tc->colorpair), NULL);
3755 waddwstr(view->window, wline);
3756 if (tc)
3757 wattr_off(view->window,
3758 COLOR_PAIR(tc->colorpair), NULL);
3759 if (view_needs_focus_indication(view))
3760 wstandend(view->window);
3761 free(wline);
3762 wline = NULL;
3763 if (width < view->ncols - 1)
3764 waddch(view->window, '\n');
3766 if (asprintf(&line, "[%d/%d] %s%s",
3767 *first_displayed_line - 1 + selected_line, nlines,
3768 blame_complete ? "" : "annotating... ", path) == -1) {
3769 free(id_str);
3770 return got_error_from_errno("asprintf");
3772 free(id_str);
3773 err = format_line(&wline, &width, line, view->ncols, 0);
3774 free(line);
3775 line = NULL;
3776 if (err)
3777 return err;
3778 waddwstr(view->window, wline);
3779 free(wline);
3780 wline = NULL;
3781 if (width < view->ncols - 1)
3782 waddch(view->window, '\n');
3784 *eof = 0;
3785 while (nprinted < max_lines - 2) {
3786 line = parse_next_line(f, &len);
3787 if (line == NULL) {
3788 *eof = 1;
3789 break;
3791 if (++lineno < *first_displayed_line) {
3792 free(line);
3793 continue;
3796 if (view->ncols <= 9) {
3797 width = 9;
3798 wline = wcsdup(L"");
3799 if (wline == NULL)
3800 err = got_error_from_errno("wcsdup");
3801 } else {
3802 err = format_line(&wline, &width, line,
3803 view->ncols - 9, 9);
3804 width += 9;
3806 if (err) {
3807 free(line);
3808 return err;
3811 if (view->focussed && nprinted == selected_line - 1)
3812 wstandout(view->window);
3814 if (nlines > 0) {
3815 blame_line = &lines[lineno - 1];
3816 if (blame_line->annotated && prev_id &&
3817 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3818 !(view->focussed &&
3819 nprinted == selected_line - 1)) {
3820 waddstr(view->window, " ");
3821 } else if (blame_line->annotated) {
3822 char *id_str;
3823 err = got_object_id_str(&id_str, blame_line->id);
3824 if (err) {
3825 free(line);
3826 free(wline);
3827 return err;
3829 tc = get_color(colors, TOG_COLOR_COMMIT);
3830 if (tc)
3831 wattr_on(view->window,
3832 COLOR_PAIR(tc->colorpair), NULL);
3833 wprintw(view->window, "%.8s", id_str);
3834 if (tc)
3835 wattr_off(view->window,
3836 COLOR_PAIR(tc->colorpair), NULL);
3837 free(id_str);
3838 prev_id = blame_line->id;
3839 } else {
3840 waddstr(view->window, "........");
3841 prev_id = NULL;
3843 } else {
3844 waddstr(view->window, "........");
3845 prev_id = NULL;
3848 if (view->focussed && nprinted == selected_line - 1)
3849 wstandend(view->window);
3850 waddstr(view->window, " ");
3852 waddwstr(view->window, wline);
3853 if (width <= view->ncols - 1)
3854 waddch(view->window, '\n');
3855 if (++nprinted == 1)
3856 *first_displayed_line = lineno;
3857 free(line);
3858 free(wline);
3859 wline = NULL;
3861 *last_displayed_line = lineno;
3863 view_vborder(view);
3865 return NULL;
3868 static const struct got_error *
3869 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3871 const struct got_error *err = NULL;
3872 struct tog_blame_cb_args *a = arg;
3873 struct tog_blame_line *line;
3874 int errcode;
3876 if (nlines != a->nlines ||
3877 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3878 return got_error(GOT_ERR_RANGE);
3880 errcode = pthread_mutex_lock(&tog_mutex);
3881 if (errcode)
3882 return got_error_set_errno(errcode, "pthread_mutex_lock");
3884 if (*a->quit) { /* user has quit the blame view */
3885 err = got_error(GOT_ERR_ITER_COMPLETED);
3886 goto done;
3889 if (lineno == -1)
3890 goto done; /* no change in this commit */
3892 line = &a->lines[lineno - 1];
3893 if (line->annotated)
3894 goto done;
3896 line->id = got_object_id_dup(id);
3897 if (line->id == NULL) {
3898 err = got_error_from_errno("got_object_id_dup");
3899 goto done;
3901 line->annotated = 1;
3902 done:
3903 errcode = pthread_mutex_unlock(&tog_mutex);
3904 if (errcode)
3905 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3906 return err;
3909 static void *
3910 blame_thread(void *arg)
3912 const struct got_error *err;
3913 struct tog_blame_thread_args *ta = arg;
3914 struct tog_blame_cb_args *a = ta->cb_args;
3915 int errcode;
3917 err = block_signals_used_by_main_thread();
3918 if (err)
3919 return (void *)err;
3921 err = got_blame(ta->path, a->commit_id, ta->repo,
3922 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3923 if (err && err->code == GOT_ERR_CANCELLED)
3924 err = NULL;
3926 errcode = pthread_mutex_lock(&tog_mutex);
3927 if (errcode)
3928 return (void *)got_error_set_errno(errcode,
3929 "pthread_mutex_lock");
3931 got_repo_close(ta->repo);
3932 ta->repo = NULL;
3933 *ta->complete = 1;
3935 errcode = pthread_mutex_unlock(&tog_mutex);
3936 if (errcode && err == NULL)
3937 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3939 return (void *)err;
3942 static struct got_object_id *
3943 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3944 int first_displayed_line, int selected_line)
3946 struct tog_blame_line *line;
3948 if (nlines <= 0)
3949 return NULL;
3951 line = &lines[first_displayed_line - 1 + selected_line - 1];
3952 if (!line->annotated)
3953 return NULL;
3955 return line->id;
3958 static const struct got_error *
3959 stop_blame(struct tog_blame *blame)
3961 const struct got_error *err = NULL;
3962 int i;
3964 if (blame->thread) {
3965 int errcode;
3966 errcode = pthread_mutex_unlock(&tog_mutex);
3967 if (errcode)
3968 return got_error_set_errno(errcode,
3969 "pthread_mutex_unlock");
3970 errcode = pthread_join(blame->thread, (void **)&err);
3971 if (errcode)
3972 return got_error_set_errno(errcode, "pthread_join");
3973 errcode = pthread_mutex_lock(&tog_mutex);
3974 if (errcode)
3975 return got_error_set_errno(errcode,
3976 "pthread_mutex_lock");
3977 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3978 err = NULL;
3979 blame->thread = NULL;
3981 if (blame->thread_args.repo) {
3982 got_repo_close(blame->thread_args.repo);
3983 blame->thread_args.repo = NULL;
3985 if (blame->f) {
3986 if (fclose(blame->f) != 0 && err == NULL)
3987 err = got_error_from_errno("fclose");
3988 blame->f = NULL;
3990 if (blame->lines) {
3991 for (i = 0; i < blame->nlines; i++)
3992 free(blame->lines[i].id);
3993 free(blame->lines);
3994 blame->lines = NULL;
3996 free(blame->cb_args.commit_id);
3997 blame->cb_args.commit_id = NULL;
3999 return err;
4002 static const struct got_error *
4003 cancel_blame_view(void *arg)
4005 const struct got_error *err = NULL;
4006 int *done = arg;
4007 int errcode;
4009 errcode = pthread_mutex_lock(&tog_mutex);
4010 if (errcode)
4011 return got_error_set_errno(errcode,
4012 "pthread_mutex_unlock");
4014 if (*done)
4015 err = got_error(GOT_ERR_CANCELLED);
4017 errcode = pthread_mutex_unlock(&tog_mutex);
4018 if (errcode)
4019 return got_error_set_errno(errcode,
4020 "pthread_mutex_lock");
4022 return err;
4025 static const struct got_error *
4026 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
4027 int *first_displayed_line, int *last_displayed_line, int *selected_line,
4028 int *done, int *eof, const char *path, struct got_object_id *commit_id,
4029 struct got_repository *repo)
4031 const struct got_error *err = NULL;
4032 struct got_blob_object *blob = NULL;
4033 struct got_repository *thread_repo = NULL;
4034 struct got_object_id *obj_id = NULL;
4035 int obj_type;
4037 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
4038 if (err)
4039 return err;
4041 err = got_object_get_type(&obj_type, repo, obj_id);
4042 if (err)
4043 goto done;
4045 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4046 err = got_error(GOT_ERR_OBJ_TYPE);
4047 goto done;
4050 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4051 if (err)
4052 goto done;
4053 blame->f = got_opentemp();
4054 if (blame->f == NULL) {
4055 err = got_error_from_errno("got_opentemp");
4056 goto done;
4058 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4059 &blame->line_offsets, blame->f, blob);
4060 if (err || blame->nlines == 0)
4061 goto done;
4063 /* Don't include \n at EOF in the blame line count. */
4064 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4065 blame->nlines--;
4067 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4068 if (blame->lines == NULL) {
4069 err = got_error_from_errno("calloc");
4070 goto done;
4073 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4074 if (err)
4075 goto done;
4077 blame->cb_args.view = view;
4078 blame->cb_args.lines = blame->lines;
4079 blame->cb_args.nlines = blame->nlines;
4080 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4081 if (blame->cb_args.commit_id == NULL) {
4082 err = got_error_from_errno("got_object_id_dup");
4083 goto done;
4085 blame->cb_args.quit = done;
4087 blame->thread_args.path = path;
4088 blame->thread_args.repo = thread_repo;
4089 blame->thread_args.cb_args = &blame->cb_args;
4090 blame->thread_args.complete = blame_complete;
4091 blame->thread_args.cancel_cb = cancel_blame_view;
4092 blame->thread_args.cancel_arg = done;
4093 *blame_complete = 0;
4095 done:
4096 if (blob)
4097 got_object_blob_close(blob);
4098 free(obj_id);
4099 if (err)
4100 stop_blame(blame);
4101 return err;
4104 static const struct got_error *
4105 open_blame_view(struct tog_view *view, char *path,
4106 struct got_object_id *commit_id, struct got_reflist_head *refs,
4107 struct got_repository *repo)
4109 const struct got_error *err = NULL;
4110 struct tog_blame_view_state *s = &view->state.blame;
4112 SIMPLEQ_INIT(&s->blamed_commits);
4114 s->path = strdup(path);
4115 if (s->path == NULL)
4116 return got_error_from_errno("strdup");
4118 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4119 if (err) {
4120 free(s->path);
4121 return err;
4124 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4125 s->first_displayed_line = 1;
4126 s->last_displayed_line = view->nlines;
4127 s->selected_line = 1;
4128 s->blame_complete = 0;
4129 s->repo = repo;
4130 s->refs = refs;
4131 s->commit_id = commit_id;
4132 memset(&s->blame, 0, sizeof(s->blame));
4134 SIMPLEQ_INIT(&s->colors);
4135 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4136 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4137 get_color_value("TOG_COLOR_COMMIT"));
4138 if (err)
4139 return err;
4142 view->show = show_blame_view;
4143 view->input = input_blame_view;
4144 view->close = close_blame_view;
4145 view->search_start = search_start_blame_view;
4146 view->search_next = search_next_blame_view;
4148 return run_blame(&s->blame, view, &s->blame_complete,
4149 &s->first_displayed_line, &s->last_displayed_line,
4150 &s->selected_line, &s->done, &s->eof, s->path,
4151 s->blamed_commit->id, s->repo);
4154 static const struct got_error *
4155 close_blame_view(struct tog_view *view)
4157 const struct got_error *err = NULL;
4158 struct tog_blame_view_state *s = &view->state.blame;
4160 if (s->blame.thread)
4161 err = stop_blame(&s->blame);
4163 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4164 struct got_object_qid *blamed_commit;
4165 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4166 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4167 got_object_qid_free(blamed_commit);
4170 free(s->path);
4171 free_colors(&s->colors);
4173 return err;
4176 static const struct got_error *
4177 search_start_blame_view(struct tog_view *view)
4179 struct tog_blame_view_state *s = &view->state.blame;
4181 s->matched_line = 0;
4182 return NULL;
4185 static const struct got_error *
4186 search_next_blame_view(struct tog_view *view)
4188 struct tog_blame_view_state *s = &view->state.blame;
4189 int lineno;
4191 if (!view->searching) {
4192 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4193 return NULL;
4196 if (s->matched_line) {
4197 if (view->searching == TOG_SEARCH_FORWARD)
4198 lineno = s->matched_line + 1;
4199 else
4200 lineno = s->matched_line - 1;
4201 } else {
4202 if (view->searching == TOG_SEARCH_FORWARD)
4203 lineno = 1;
4204 else
4205 lineno = s->blame.nlines;
4208 while (1) {
4209 char *line = NULL;
4210 off_t offset;
4211 size_t len;
4213 if (lineno <= 0 || lineno > s->blame.nlines) {
4214 if (s->matched_line == 0) {
4215 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4216 free(line);
4217 break;
4220 if (view->searching == TOG_SEARCH_FORWARD)
4221 lineno = 1;
4222 else
4223 lineno = s->blame.nlines;
4226 offset = s->blame.line_offsets[lineno - 1];
4227 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4228 free(line);
4229 return got_error_from_errno("fseeko");
4231 free(line);
4232 line = parse_next_line(s->blame.f, &len);
4233 if (line && match_line(line, &view->regex)) {
4234 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4235 s->matched_line = lineno;
4236 free(line);
4237 break;
4239 free(line);
4240 if (view->searching == TOG_SEARCH_FORWARD)
4241 lineno++;
4242 else
4243 lineno--;
4246 if (s->matched_line) {
4247 s->first_displayed_line = s->matched_line;
4248 s->selected_line = 1;
4251 return NULL;
4254 static const struct got_error *
4255 show_blame_view(struct tog_view *view)
4257 const struct got_error *err = NULL;
4258 struct tog_blame_view_state *s = &view->state.blame;
4259 int errcode;
4261 if (s->blame.thread == NULL) {
4262 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4263 &s->blame.thread_args);
4264 if (errcode)
4265 return got_error_set_errno(errcode, "pthread_create");
4267 halfdelay(1); /* fast refresh while annotating */
4270 if (s->blame_complete)
4271 halfdelay(10); /* disable fast refresh */
4273 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4274 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4275 s->selected_line, &s->first_displayed_line,
4276 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4278 view_vborder(view);
4279 return err;
4282 static const struct got_error *
4283 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4284 struct tog_view **focus_view, struct tog_view *view, int ch)
4286 const struct got_error *err = NULL, *thread_err = NULL;
4287 struct tog_view *diff_view;
4288 struct tog_blame_view_state *s = &view->state.blame;
4289 int begin_x = 0;
4291 switch (ch) {
4292 case 'q':
4293 s->done = 1;
4294 break;
4295 case 'k':
4296 case KEY_UP:
4297 if (s->selected_line > 1)
4298 s->selected_line--;
4299 else if (s->selected_line == 1 &&
4300 s->first_displayed_line > 1)
4301 s->first_displayed_line--;
4302 break;
4303 case KEY_PPAGE:
4304 case CTRL('b'):
4305 if (s->first_displayed_line == 1) {
4306 s->selected_line = 1;
4307 break;
4309 if (s->first_displayed_line > view->nlines - 2)
4310 s->first_displayed_line -=
4311 (view->nlines - 2);
4312 else
4313 s->first_displayed_line = 1;
4314 break;
4315 case 'j':
4316 case KEY_DOWN:
4317 if (s->selected_line < view->nlines - 2 &&
4318 s->first_displayed_line +
4319 s->selected_line <= s->blame.nlines)
4320 s->selected_line++;
4321 else if (s->last_displayed_line <
4322 s->blame.nlines)
4323 s->first_displayed_line++;
4324 break;
4325 case 'b':
4326 case 'p': {
4327 struct got_object_id *id = NULL;
4328 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4329 s->first_displayed_line, s->selected_line);
4330 if (id == NULL)
4331 break;
4332 if (ch == 'p') {
4333 struct got_commit_object *commit;
4334 struct got_object_qid *pid;
4335 struct got_object_id *blob_id = NULL;
4336 int obj_type;
4337 err = got_object_open_as_commit(&commit,
4338 s->repo, id);
4339 if (err)
4340 break;
4341 pid = SIMPLEQ_FIRST(
4342 got_object_commit_get_parent_ids(commit));
4343 if (pid == NULL) {
4344 got_object_commit_close(commit);
4345 break;
4347 /* Check if path history ends here. */
4348 err = got_object_id_by_path(&blob_id, s->repo,
4349 pid->id, s->path);
4350 if (err) {
4351 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4352 err = NULL;
4353 got_object_commit_close(commit);
4354 break;
4356 err = got_object_get_type(&obj_type, s->repo,
4357 blob_id);
4358 free(blob_id);
4359 /* Can't blame non-blob type objects. */
4360 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4361 got_object_commit_close(commit);
4362 break;
4364 err = got_object_qid_alloc(&s->blamed_commit,
4365 pid->id);
4366 got_object_commit_close(commit);
4367 } else {
4368 if (got_object_id_cmp(id,
4369 s->blamed_commit->id) == 0)
4370 break;
4371 err = got_object_qid_alloc(&s->blamed_commit,
4372 id);
4374 if (err)
4375 break;
4376 s->done = 1;
4377 thread_err = stop_blame(&s->blame);
4378 s->done = 0;
4379 if (thread_err)
4380 break;
4381 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4382 s->blamed_commit, entry);
4383 err = run_blame(&s->blame, view, &s->blame_complete,
4384 &s->first_displayed_line, &s->last_displayed_line,
4385 &s->selected_line, &s->done, &s->eof,
4386 s->path, s->blamed_commit->id, s->repo);
4387 if (err)
4388 break;
4389 break;
4391 case 'B': {
4392 struct got_object_qid *first;
4393 first = SIMPLEQ_FIRST(&s->blamed_commits);
4394 if (!got_object_id_cmp(first->id, s->commit_id))
4395 break;
4396 s->done = 1;
4397 thread_err = stop_blame(&s->blame);
4398 s->done = 0;
4399 if (thread_err)
4400 break;
4401 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4402 got_object_qid_free(s->blamed_commit);
4403 s->blamed_commit =
4404 SIMPLEQ_FIRST(&s->blamed_commits);
4405 err = run_blame(&s->blame, view, &s->blame_complete,
4406 &s->first_displayed_line, &s->last_displayed_line,
4407 &s->selected_line, &s->done, &s->eof, s->path,
4408 s->blamed_commit->id, s->repo);
4409 if (err)
4410 break;
4411 break;
4413 case KEY_ENTER:
4414 case '\r': {
4415 struct got_object_id *id = NULL;
4416 struct got_object_qid *pid;
4417 struct got_commit_object *commit = NULL;
4418 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4419 s->first_displayed_line, s->selected_line);
4420 if (id == NULL)
4421 break;
4422 err = got_object_open_as_commit(&commit, s->repo, id);
4423 if (err)
4424 break;
4425 pid = SIMPLEQ_FIRST(
4426 got_object_commit_get_parent_ids(commit));
4427 if (view_is_parent_view(view))
4428 begin_x = view_split_begin_x(view->begin_x);
4429 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4430 if (diff_view == NULL) {
4431 got_object_commit_close(commit);
4432 err = got_error_from_errno("view_open");
4433 break;
4435 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4436 id, NULL, s->refs, s->repo);
4437 got_object_commit_close(commit);
4438 if (err) {
4439 view_close(diff_view);
4440 break;
4442 if (view_is_parent_view(view)) {
4443 err = view_close_child(view);
4444 if (err)
4445 break;
4446 err = view_set_child(view, diff_view);
4447 if (err) {
4448 view_close(diff_view);
4449 break;
4451 *focus_view = diff_view;
4452 view->child_focussed = 1;
4453 } else
4454 *new_view = diff_view;
4455 if (err)
4456 break;
4457 break;
4459 case KEY_NPAGE:
4460 case CTRL('f'):
4461 case ' ':
4462 if (s->last_displayed_line >= s->blame.nlines &&
4463 s->selected_line >= MIN(s->blame.nlines,
4464 view->nlines - 2)) {
4465 break;
4467 if (s->last_displayed_line >= s->blame.nlines &&
4468 s->selected_line < view->nlines - 2) {
4469 s->selected_line = MIN(s->blame.nlines,
4470 view->nlines - 2);
4471 break;
4473 if (s->last_displayed_line + view->nlines - 2
4474 <= s->blame.nlines)
4475 s->first_displayed_line +=
4476 view->nlines - 2;
4477 else
4478 s->first_displayed_line =
4479 s->blame.nlines -
4480 (view->nlines - 3);
4481 break;
4482 case KEY_RESIZE:
4483 if (s->selected_line > view->nlines - 2) {
4484 s->selected_line = MIN(s->blame.nlines,
4485 view->nlines - 2);
4487 break;
4488 default:
4489 break;
4491 return thread_err ? thread_err : err;
4494 static const struct got_error *
4495 cmd_blame(int argc, char *argv[])
4497 const struct got_error *error;
4498 struct got_repository *repo = NULL;
4499 struct got_reflist_head refs;
4500 struct got_worktree *worktree = NULL;
4501 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4502 char *link_target = NULL;
4503 struct got_object_id *commit_id = NULL;
4504 char *commit_id_str = NULL;
4505 int ch;
4506 struct tog_view *view;
4508 SIMPLEQ_INIT(&refs);
4510 #ifndef PROFILE
4511 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4512 NULL) == -1)
4513 err(1, "pledge");
4514 #endif
4516 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4517 switch (ch) {
4518 case 'c':
4519 commit_id_str = optarg;
4520 break;
4521 case 'r':
4522 repo_path = realpath(optarg, NULL);
4523 if (repo_path == NULL)
4524 return got_error_from_errno2("realpath",
4525 optarg);
4526 break;
4527 default:
4528 usage_blame();
4529 /* NOTREACHED */
4533 argc -= optind;
4534 argv += optind;
4536 if (argc != 1)
4537 usage_blame();
4539 cwd = getcwd(NULL, 0);
4540 if (cwd == NULL)
4541 return got_error_from_errno("getcwd");
4543 error = got_worktree_open(&worktree, cwd);
4544 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4545 goto done;
4547 if (repo_path == NULL) {
4548 if (worktree)
4549 repo_path =
4550 strdup(got_worktree_get_repo_path(worktree));
4551 else
4552 repo_path = strdup(cwd);
4554 if (repo_path == NULL) {
4555 error = got_error_from_errno("strdup");
4556 goto done;
4559 error = got_repo_open(&repo, repo_path, NULL);
4560 if (error != NULL)
4561 goto done;
4563 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4564 worktree);
4565 if (error)
4566 goto done;
4568 init_curses();
4570 error = apply_unveil(got_repo_get_path(repo), NULL);
4571 if (error)
4572 goto done;
4574 if (commit_id_str == NULL) {
4575 struct got_reference *head_ref;
4576 error = got_ref_open(&head_ref, repo, worktree ?
4577 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4578 if (error != NULL)
4579 goto done;
4580 error = got_ref_resolve(&commit_id, repo, head_ref);
4581 got_ref_close(head_ref);
4582 } else {
4583 error = got_repo_match_object_id(&commit_id, NULL,
4584 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4586 if (error != NULL)
4587 goto done;
4589 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4590 if (error)
4591 goto done;
4593 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4594 if (view == NULL) {
4595 error = got_error_from_errno("view_open");
4596 goto done;
4599 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4600 commit_id, repo);
4601 if (error)
4602 goto done;
4604 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4605 commit_id, &refs, repo);
4606 if (error)
4607 goto done;
4608 if (worktree) {
4609 /* Release work tree lock. */
4610 got_worktree_close(worktree);
4611 worktree = NULL;
4613 error = view_loop(view);
4614 done:
4615 free(repo_path);
4616 free(in_repo_path);
4617 free(link_target);
4618 free(cwd);
4619 free(commit_id);
4620 if (worktree)
4621 got_worktree_close(worktree);
4622 if (repo)
4623 got_repo_close(repo);
4624 got_ref_list_free(&refs);
4625 return error;
4628 static const struct got_error *
4629 draw_tree_entries(struct tog_view *view,
4630 struct got_tree_entry **first_displayed_entry,
4631 struct got_tree_entry **last_displayed_entry,
4632 struct got_tree_entry **selected_entry, int *ndisplayed,
4633 const char *label, int show_ids, const char *parent_path,
4634 struct got_tree_object *tree, int selected, int limit,
4635 int isroot, struct tog_colors *colors, struct got_repository *repo)
4637 const struct got_error *err = NULL;
4638 struct got_tree_entry *te;
4639 wchar_t *wline;
4640 struct tog_color *tc;
4641 int width, n, i, nentries;
4643 *ndisplayed = 0;
4645 werase(view->window);
4647 if (limit == 0)
4648 return NULL;
4650 err = format_line(&wline, &width, label, view->ncols, 0);
4651 if (err)
4652 return err;
4653 if (view_needs_focus_indication(view))
4654 wstandout(view->window);
4655 tc = get_color(colors, TOG_COLOR_COMMIT);
4656 if (tc)
4657 wattr_on(view->window,
4658 COLOR_PAIR(tc->colorpair), NULL);
4659 waddwstr(view->window, wline);
4660 if (tc)
4661 wattr_off(view->window,
4662 COLOR_PAIR(tc->colorpair), NULL);
4663 if (view_needs_focus_indication(view))
4664 wstandend(view->window);
4665 free(wline);
4666 wline = NULL;
4667 if (width < view->ncols - 1)
4668 waddch(view->window, '\n');
4669 if (--limit <= 0)
4670 return NULL;
4671 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4672 if (err)
4673 return err;
4674 waddwstr(view->window, wline);
4675 free(wline);
4676 wline = NULL;
4677 if (width < view->ncols - 1)
4678 waddch(view->window, '\n');
4679 if (--limit <= 0)
4680 return NULL;
4681 waddch(view->window, '\n');
4682 if (--limit <= 0)
4683 return NULL;
4685 if (*first_displayed_entry == NULL) {
4686 te = got_object_tree_get_first_entry(tree);
4687 if (selected == 0) {
4688 if (view->focussed)
4689 wstandout(view->window);
4690 *selected_entry = NULL;
4692 waddstr(view->window, " ..\n"); /* parent directory */
4693 if (selected == 0 && view->focussed)
4694 wstandend(view->window);
4695 (*ndisplayed)++;
4696 if (--limit <= 0)
4697 return NULL;
4698 n = 1;
4699 } else {
4700 n = 0;
4701 te = *first_displayed_entry;
4704 nentries = got_object_tree_get_nentries(tree);
4705 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4706 char *line = NULL, *id_str = NULL, *link_target = NULL;
4707 const char *modestr = "";
4708 mode_t mode;
4710 te = got_object_tree_get_entry(tree, i);
4711 mode = got_tree_entry_get_mode(te);
4713 if (show_ids) {
4714 err = got_object_id_str(&id_str,
4715 got_tree_entry_get_id(te));
4716 if (err)
4717 return got_error_from_errno(
4718 "got_object_id_str");
4720 if (got_object_tree_entry_is_submodule(te))
4721 modestr = "$";
4722 else if (S_ISLNK(mode)) {
4723 int i;
4725 err = got_tree_entry_get_symlink_target(&link_target,
4726 te, repo);
4727 if (err) {
4728 free(id_str);
4729 return err;
4731 for (i = 0; i < strlen(link_target); i++) {
4732 if (!isprint((unsigned char)link_target[i]))
4733 link_target[i] = '?';
4735 modestr = "@";
4737 else if (S_ISDIR(mode))
4738 modestr = "/";
4739 else if (mode & S_IXUSR)
4740 modestr = "*";
4741 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4742 got_tree_entry_get_name(te), modestr,
4743 link_target ? " -> ": "",
4744 link_target ? link_target : "") == -1) {
4745 free(id_str);
4746 free(link_target);
4747 return got_error_from_errno("asprintf");
4749 free(id_str);
4750 free(link_target);
4751 err = format_line(&wline, &width, line, view->ncols, 0);
4752 if (err) {
4753 free(line);
4754 break;
4756 if (n == selected) {
4757 if (view->focussed)
4758 wstandout(view->window);
4759 *selected_entry = te;
4761 tc = match_color(colors, line);
4762 if (tc)
4763 wattr_on(view->window,
4764 COLOR_PAIR(tc->colorpair), NULL);
4765 waddwstr(view->window, wline);
4766 if (tc)
4767 wattr_off(view->window,
4768 COLOR_PAIR(tc->colorpair), NULL);
4769 if (width < view->ncols - 1)
4770 waddch(view->window, '\n');
4771 if (n == selected && view->focussed)
4772 wstandend(view->window);
4773 free(line);
4774 free(wline);
4775 wline = NULL;
4776 n++;
4777 (*ndisplayed)++;
4778 *last_displayed_entry = te;
4779 if (--limit <= 0)
4780 break;
4783 return err;
4786 static void
4787 tree_scroll_up(struct tog_view *view,
4788 struct got_tree_entry **first_displayed_entry, int maxscroll,
4789 struct got_tree_object *tree, int isroot)
4791 struct got_tree_entry *te;
4792 int i;
4794 if (*first_displayed_entry == NULL)
4795 return;
4797 te = got_object_tree_get_entry(tree, 0);
4798 if (*first_displayed_entry == te) {
4799 if (!isroot)
4800 *first_displayed_entry = NULL;
4801 return;
4804 i = 0;
4805 while (*first_displayed_entry && i < maxscroll) {
4806 *first_displayed_entry = got_tree_entry_get_prev(tree,
4807 *first_displayed_entry);
4808 i++;
4810 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4811 *first_displayed_entry = NULL;
4814 static int
4815 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4816 struct got_tree_entry *last_displayed_entry,
4817 struct got_tree_object *tree)
4819 struct got_tree_entry *next, *last;
4820 int n = 0;
4822 if (*first_displayed_entry)
4823 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4824 else
4825 next = got_object_tree_get_first_entry(tree);
4827 last = last_displayed_entry;
4828 while (next && last && n++ < maxscroll) {
4829 last = got_tree_entry_get_next(tree, last);
4830 if (last) {
4831 *first_displayed_entry = next;
4832 next = got_tree_entry_get_next(tree, next);
4835 return n;
4838 static const struct got_error *
4839 tree_entry_path(char **path, struct tog_parent_trees *parents,
4840 struct got_tree_entry *te)
4842 const struct got_error *err = NULL;
4843 struct tog_parent_tree *pt;
4844 size_t len = 2; /* for leading slash and NUL */
4846 TAILQ_FOREACH(pt, parents, entry)
4847 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4848 + 1 /* slash */;
4849 if (te)
4850 len += strlen(got_tree_entry_get_name(te));
4852 *path = calloc(1, len);
4853 if (path == NULL)
4854 return got_error_from_errno("calloc");
4856 (*path)[0] = '/';
4857 pt = TAILQ_LAST(parents, tog_parent_trees);
4858 while (pt) {
4859 const char *name = got_tree_entry_get_name(pt->selected_entry);
4860 if (strlcat(*path, name, len) >= len) {
4861 err = got_error(GOT_ERR_NO_SPACE);
4862 goto done;
4864 if (strlcat(*path, "/", len) >= len) {
4865 err = got_error(GOT_ERR_NO_SPACE);
4866 goto done;
4868 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4870 if (te) {
4871 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4872 err = got_error(GOT_ERR_NO_SPACE);
4873 goto done;
4876 done:
4877 if (err) {
4878 free(*path);
4879 *path = NULL;
4881 return err;
4884 static const struct got_error *
4885 blame_tree_entry(struct tog_view **new_view, int begin_x,
4886 struct got_tree_entry *te, struct tog_parent_trees *parents,
4887 struct got_object_id *commit_id, struct got_reflist_head *refs,
4888 struct got_repository *repo)
4890 const struct got_error *err = NULL;
4891 char *path;
4892 struct tog_view *blame_view;
4894 *new_view = NULL;
4896 err = tree_entry_path(&path, parents, te);
4897 if (err)
4898 return err;
4900 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4901 if (blame_view == NULL) {
4902 err = got_error_from_errno("view_open");
4903 goto done;
4906 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4907 if (err) {
4908 if (err->code == GOT_ERR_CANCELLED)
4909 err = NULL;
4910 view_close(blame_view);
4911 } else
4912 *new_view = blame_view;
4913 done:
4914 free(path);
4915 return err;
4918 static const struct got_error *
4919 log_tree_entry(struct tog_view **new_view, int begin_x,
4920 struct got_tree_entry *te, struct tog_parent_trees *parents,
4921 struct got_object_id *commit_id, struct got_reflist_head *refs,
4922 struct got_repository *repo)
4924 struct tog_view *log_view;
4925 const struct got_error *err = NULL;
4926 char *path;
4928 *new_view = NULL;
4930 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4931 if (log_view == NULL)
4932 return got_error_from_errno("view_open");
4934 err = tree_entry_path(&path, parents, te);
4935 if (err)
4936 return err;
4938 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4939 if (err)
4940 view_close(log_view);
4941 else
4942 *new_view = log_view;
4943 free(path);
4944 return err;
4947 static const struct got_error *
4948 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4949 struct got_object_id *commit_id, struct got_reflist_head *refs,
4950 struct got_repository *repo)
4952 const struct got_error *err = NULL;
4953 char *commit_id_str = NULL;
4954 struct tog_tree_view_state *s = &view->state.tree;
4956 TAILQ_INIT(&s->parents);
4958 err = got_object_id_str(&commit_id_str, commit_id);
4959 if (err != NULL)
4960 goto done;
4962 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4963 err = got_error_from_errno("asprintf");
4964 goto done;
4967 s->root = s->tree = root;
4968 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4969 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4970 s->commit_id = got_object_id_dup(commit_id);
4971 if (s->commit_id == NULL) {
4972 err = got_error_from_errno("got_object_id_dup");
4973 goto done;
4975 s->refs = refs;
4976 s->repo = repo;
4978 SIMPLEQ_INIT(&s->colors);
4980 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4981 err = add_color(&s->colors, "\\$$",
4982 TOG_COLOR_TREE_SUBMODULE,
4983 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4984 if (err)
4985 goto done;
4986 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4987 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4988 if (err) {
4989 free_colors(&s->colors);
4990 goto done;
4992 err = add_color(&s->colors, "/$",
4993 TOG_COLOR_TREE_DIRECTORY,
4994 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4995 if (err) {
4996 free_colors(&s->colors);
4997 goto done;
5000 err = add_color(&s->colors, "\\*$",
5001 TOG_COLOR_TREE_EXECUTABLE,
5002 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5003 if (err) {
5004 free_colors(&s->colors);
5005 goto done;
5008 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5009 get_color_value("TOG_COLOR_COMMIT"));
5010 if (err) {
5011 free_colors(&s->colors);
5012 goto done;
5016 view->show = show_tree_view;
5017 view->input = input_tree_view;
5018 view->close = close_tree_view;
5019 view->search_start = search_start_tree_view;
5020 view->search_next = search_next_tree_view;
5021 done:
5022 free(commit_id_str);
5023 if (err) {
5024 free(s->tree_label);
5025 s->tree_label = NULL;
5027 return err;
5030 static const struct got_error *
5031 close_tree_view(struct tog_view *view)
5033 struct tog_tree_view_state *s = &view->state.tree;
5035 free_colors(&s->colors);
5036 free(s->tree_label);
5037 s->tree_label = NULL;
5038 free(s->commit_id);
5039 s->commit_id = NULL;
5040 while (!TAILQ_EMPTY(&s->parents)) {
5041 struct tog_parent_tree *parent;
5042 parent = TAILQ_FIRST(&s->parents);
5043 TAILQ_REMOVE(&s->parents, parent, entry);
5044 free(parent);
5047 if (s->tree != s->root)
5048 got_object_tree_close(s->tree);
5049 got_object_tree_close(s->root);
5051 return NULL;
5054 static const struct got_error *
5055 search_start_tree_view(struct tog_view *view)
5057 struct tog_tree_view_state *s = &view->state.tree;
5059 s->matched_entry = NULL;
5060 return NULL;
5063 static int
5064 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5066 regmatch_t regmatch;
5068 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5069 0) == 0;
5072 static const struct got_error *
5073 search_next_tree_view(struct tog_view *view)
5075 struct tog_tree_view_state *s = &view->state.tree;
5076 struct got_tree_entry *te = NULL;
5078 if (!view->searching) {
5079 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5080 return NULL;
5083 if (s->matched_entry) {
5084 if (view->searching == TOG_SEARCH_FORWARD) {
5085 if (s->selected_entry)
5086 te = got_tree_entry_get_next(s->tree,
5087 s->selected_entry);
5088 else
5089 te = got_object_tree_get_first_entry(s->tree);
5090 } else {
5091 if (s->selected_entry == NULL)
5092 te = got_object_tree_get_last_entry(s->tree);
5093 else
5094 te = got_tree_entry_get_prev(s->tree,
5095 s->selected_entry);
5097 } else {
5098 if (view->searching == TOG_SEARCH_FORWARD)
5099 te = got_object_tree_get_first_entry(s->tree);
5100 else
5101 te = got_object_tree_get_last_entry(s->tree);
5104 while (1) {
5105 if (te == NULL) {
5106 if (s->matched_entry == NULL) {
5107 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5108 return NULL;
5110 if (view->searching == TOG_SEARCH_FORWARD)
5111 te = got_object_tree_get_first_entry(s->tree);
5112 else
5113 te = got_object_tree_get_last_entry(s->tree);
5116 if (match_tree_entry(te, &view->regex)) {
5117 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5118 s->matched_entry = te;
5119 break;
5122 if (view->searching == TOG_SEARCH_FORWARD)
5123 te = got_tree_entry_get_next(s->tree, te);
5124 else
5125 te = got_tree_entry_get_prev(s->tree, te);
5128 if (s->matched_entry) {
5129 s->first_displayed_entry = s->matched_entry;
5130 s->selected = 0;
5133 return NULL;
5136 static const struct got_error *
5137 show_tree_view(struct tog_view *view)
5139 const struct got_error *err = NULL;
5140 struct tog_tree_view_state *s = &view->state.tree;
5141 char *parent_path;
5143 err = tree_entry_path(&parent_path, &s->parents, NULL);
5144 if (err)
5145 return err;
5147 err = draw_tree_entries(view, &s->first_displayed_entry,
5148 &s->last_displayed_entry, &s->selected_entry,
5149 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5150 s->tree, s->selected, view->nlines, s->tree == s->root,
5151 &s->colors, s->repo);
5152 free(parent_path);
5154 view_vborder(view);
5155 return err;
5158 static const struct got_error *
5159 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5160 struct tog_view **focus_view, struct tog_view *view, int ch)
5162 const struct got_error *err = NULL;
5163 struct tog_tree_view_state *s = &view->state.tree;
5164 struct tog_view *log_view;
5165 int begin_x = 0, nscrolled;
5167 switch (ch) {
5168 case 'i':
5169 s->show_ids = !s->show_ids;
5170 break;
5171 case 'l':
5172 if (!s->selected_entry)
5173 break;
5174 if (view_is_parent_view(view))
5175 begin_x = view_split_begin_x(view->begin_x);
5176 err = log_tree_entry(&log_view, begin_x,
5177 s->selected_entry, &s->parents,
5178 s->commit_id, s->refs, s->repo);
5179 if (view_is_parent_view(view)) {
5180 err = view_close_child(view);
5181 if (err)
5182 return err;
5183 err = view_set_child(view, log_view);
5184 if (err) {
5185 view_close(log_view);
5186 break;
5188 *focus_view = log_view;
5189 view->child_focussed = 1;
5190 } else
5191 *new_view = log_view;
5192 break;
5193 case 'k':
5194 case KEY_UP:
5195 if (s->selected > 0) {
5196 s->selected--;
5197 if (s->selected == 0)
5198 break;
5200 if (s->selected > 0)
5201 break;
5202 tree_scroll_up(view, &s->first_displayed_entry, 1,
5203 s->tree, s->tree == s->root);
5204 break;
5205 case KEY_PPAGE:
5206 case CTRL('b'):
5207 tree_scroll_up(view, &s->first_displayed_entry,
5208 MAX(0, view->nlines - 4 - s->selected), s->tree,
5209 s->tree == s->root);
5210 s->selected = 0;
5211 if (got_object_tree_get_first_entry(s->tree) ==
5212 s->first_displayed_entry && s->tree != s->root)
5213 s->first_displayed_entry = NULL;
5214 break;
5215 case 'j':
5216 case KEY_DOWN:
5217 if (s->selected < s->ndisplayed - 1) {
5218 s->selected++;
5219 break;
5221 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5222 == NULL)
5223 /* can't scroll any further */
5224 break;
5225 tree_scroll_down(&s->first_displayed_entry, 1,
5226 s->last_displayed_entry, s->tree);
5227 break;
5228 case KEY_NPAGE:
5229 case CTRL('f'):
5230 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5231 == NULL) {
5232 /* can't scroll any further; move cursor down */
5233 if (s->selected < s->ndisplayed - 1)
5234 s->selected = s->ndisplayed - 1;
5235 break;
5237 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5238 view->nlines, s->last_displayed_entry, s->tree);
5239 if (nscrolled < view->nlines) {
5240 int ndisplayed = 0;
5241 struct got_tree_entry *te;
5242 te = s->first_displayed_entry;
5243 do {
5244 ndisplayed++;
5245 te = got_tree_entry_get_next(s->tree, te);
5246 } while (te);
5247 s->selected = ndisplayed - 1;
5249 break;
5250 case KEY_ENTER:
5251 case '\r':
5252 case KEY_BACKSPACE:
5253 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5254 struct tog_parent_tree *parent;
5255 /* user selected '..' */
5256 if (s->tree == s->root)
5257 break;
5258 parent = TAILQ_FIRST(&s->parents);
5259 TAILQ_REMOVE(&s->parents, parent,
5260 entry);
5261 got_object_tree_close(s->tree);
5262 s->tree = parent->tree;
5263 s->first_displayed_entry =
5264 parent->first_displayed_entry;
5265 s->selected_entry =
5266 parent->selected_entry;
5267 s->selected = parent->selected;
5268 free(parent);
5269 } else if (S_ISDIR(got_tree_entry_get_mode(
5270 s->selected_entry))) {
5271 struct got_tree_object *subtree;
5272 err = got_object_open_as_tree(&subtree, s->repo,
5273 got_tree_entry_get_id(s->selected_entry));
5274 if (err)
5275 break;
5276 err = tree_view_visit_subtree(subtree, s);
5277 if (err) {
5278 got_object_tree_close(subtree);
5279 break;
5281 } else if (S_ISREG(got_tree_entry_get_mode(
5282 s->selected_entry))) {
5283 struct tog_view *blame_view;
5284 int begin_x = view_is_parent_view(view) ?
5285 view_split_begin_x(view->begin_x) : 0;
5287 err = blame_tree_entry(&blame_view, begin_x,
5288 s->selected_entry, &s->parents,
5289 s->commit_id, s->refs, s->repo);
5290 if (err)
5291 break;
5292 if (view_is_parent_view(view)) {
5293 err = view_close_child(view);
5294 if (err)
5295 return err;
5296 err = view_set_child(view, blame_view);
5297 if (err) {
5298 view_close(blame_view);
5299 break;
5301 *focus_view = blame_view;
5302 view->child_focussed = 1;
5303 } else
5304 *new_view = blame_view;
5306 break;
5307 case KEY_RESIZE:
5308 if (s->selected > view->nlines)
5309 s->selected = s->ndisplayed - 1;
5310 break;
5311 default:
5312 break;
5315 return err;
5318 __dead static void
5319 usage_tree(void)
5321 endwin();
5322 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5323 getprogname());
5324 exit(1);
5327 static const struct got_error *
5328 cmd_tree(int argc, char *argv[])
5330 const struct got_error *error;
5331 struct got_repository *repo = NULL;
5332 struct got_worktree *worktree = NULL;
5333 struct got_reflist_head refs;
5334 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5335 struct got_object_id *commit_id = NULL;
5336 char *commit_id_arg = NULL;
5337 struct got_commit_object *commit = NULL;
5338 struct got_tree_object *tree = NULL;
5339 int ch;
5340 struct tog_view *view;
5342 SIMPLEQ_INIT(&refs);
5344 #ifndef PROFILE
5345 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5346 NULL) == -1)
5347 err(1, "pledge");
5348 #endif
5350 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5351 switch (ch) {
5352 case 'c':
5353 commit_id_arg = optarg;
5354 break;
5355 case 'r':
5356 repo_path = realpath(optarg, NULL);
5357 if (repo_path == NULL)
5358 return got_error_from_errno2("realpath",
5359 optarg);
5360 break;
5361 default:
5362 usage_tree();
5363 /* NOTREACHED */
5367 argc -= optind;
5368 argv += optind;
5370 if (argc > 1)
5371 usage_tree();
5373 cwd = getcwd(NULL, 0);
5374 if (cwd == NULL)
5375 return got_error_from_errno("getcwd");
5377 error = got_worktree_open(&worktree, cwd);
5378 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5379 goto done;
5381 if (repo_path == NULL) {
5382 if (worktree)
5383 repo_path =
5384 strdup(got_worktree_get_repo_path(worktree));
5385 else
5386 repo_path = strdup(cwd);
5388 if (repo_path == NULL) {
5389 error = got_error_from_errno("strdup");
5390 goto done;
5393 error = got_repo_open(&repo, repo_path, NULL);
5394 if (error != NULL)
5395 goto done;
5397 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5398 repo, worktree);
5399 if (error)
5400 goto done;
5402 init_curses();
5404 error = apply_unveil(got_repo_get_path(repo), NULL);
5405 if (error)
5406 goto done;
5408 error = got_repo_match_object_id(&commit_id, NULL,
5409 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5410 GOT_OBJ_TYPE_COMMIT, 1, repo);
5411 if (error)
5412 goto done;
5414 error = got_object_open_as_commit(&commit, repo, commit_id);
5415 if (error)
5416 goto done;
5418 error = got_object_open_as_tree(&tree, repo,
5419 got_object_commit_get_tree_id(commit));
5420 if (error)
5421 goto done;
5423 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5424 if (error)
5425 goto done;
5427 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5428 if (view == NULL) {
5429 error = got_error_from_errno("view_open");
5430 goto done;
5432 error = open_tree_view(view, tree, commit_id, &refs, repo);
5433 if (error)
5434 goto done;
5435 if (!got_path_is_root_dir(in_repo_path)) {
5436 error = tree_view_walk_path(&view->state.tree, commit_id,
5437 in_repo_path, repo);
5438 if (error)
5439 goto done;
5442 if (worktree) {
5443 /* Release work tree lock. */
5444 got_worktree_close(worktree);
5445 worktree = NULL;
5447 error = view_loop(view);
5448 done:
5449 free(repo_path);
5450 free(cwd);
5451 free(commit_id);
5452 if (commit)
5453 got_object_commit_close(commit);
5454 if (tree)
5455 got_object_tree_close(tree);
5456 if (repo)
5457 got_repo_close(repo);
5458 got_ref_list_free(&refs);
5459 return error;
5462 static void
5463 list_commands(void)
5465 int i;
5467 fprintf(stderr, "commands:");
5468 for (i = 0; i < nitems(tog_commands); i++) {
5469 struct tog_cmd *cmd = &tog_commands[i];
5470 fprintf(stderr, " %s", cmd->name);
5472 fputc('\n', stderr);
5475 __dead static void
5476 usage(int hflag)
5478 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] "
5479 "[arg ...]\n", getprogname());
5480 if (hflag) {
5481 fprintf(stderr, "lazy usage: %s path\n", getprogname());
5482 list_commands();
5484 exit(1);
5487 static char **
5488 make_argv(int argc, ...)
5490 va_list ap;
5491 char **argv;
5492 int i;
5494 va_start(ap, argc);
5496 argv = calloc(argc, sizeof(char *));
5497 if (argv == NULL)
5498 err(1, "calloc");
5499 for (i = 0; i < argc; i++) {
5500 argv[i] = strdup(va_arg(ap, char *));
5501 if (argv[i] == NULL)
5502 err(1, "strdup");
5505 va_end(ap);
5506 return argv;
5510 * Try to convert 'tog path' into a 'tog log path' command.
5511 * The user could simply have mistyped the command rather than knowingly
5512 * provided a path. So check whether argv[0] can in fact be resolved
5513 * to a path in the HEAD commit and print a special error if not.
5514 * This hack is for mpi@ <3
5516 static const struct got_error *
5517 tog_log_with_path(int argc, char *argv[])
5519 const struct got_error *error = NULL;
5520 struct tog_cmd *cmd = NULL;
5521 struct got_repository *repo = NULL;
5522 struct got_worktree *worktree = NULL;
5523 struct got_object_id *commit_id = NULL, *id = NULL;
5524 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5525 char *commit_id_str = NULL, **cmd_argv = NULL;
5527 cwd = getcwd(NULL, 0);
5528 if (cwd == NULL)
5529 return got_error_from_errno("getcwd");
5531 error = got_worktree_open(&worktree, cwd);
5532 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5533 goto done;
5535 if (worktree)
5536 repo_path = strdup(got_worktree_get_repo_path(worktree));
5537 else
5538 repo_path = strdup(cwd);
5539 if (repo_path == NULL) {
5540 error = got_error_from_errno("strdup");
5541 goto done;
5544 error = got_repo_open(&repo, repo_path, NULL);
5545 if (error != NULL)
5546 goto done;
5548 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5549 repo, worktree);
5550 if (error)
5551 goto done;
5553 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5554 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5555 GOT_OBJ_TYPE_COMMIT, 1, repo);
5556 if (error)
5557 goto done;
5559 if (worktree) {
5560 got_worktree_close(worktree);
5561 worktree = NULL;
5564 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5565 if (error) {
5566 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5567 goto done;
5568 fprintf(stderr, "%s: '%s' is no known command or path\n",
5569 getprogname(), argv[0]);
5570 usage(1);
5571 /* not reached */
5574 got_repo_close(repo);
5575 repo = NULL;
5577 error = got_object_id_str(&commit_id_str, commit_id);
5578 if (error)
5579 goto done;
5581 cmd = &tog_commands[0]; /* log */
5582 argc = 4;
5583 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5584 error = cmd->cmd_main(argc, cmd_argv);
5585 done:
5586 if (repo)
5587 got_repo_close(repo);
5588 if (worktree)
5589 got_worktree_close(worktree);
5590 free(id);
5591 free(commit_id_str);
5592 free(commit_id);
5593 free(cwd);
5594 free(repo_path);
5595 free(in_repo_path);
5596 if (cmd_argv) {
5597 int i;
5598 for (i = 0; i < argc; i++)
5599 free(cmd_argv[i]);
5600 free(cmd_argv);
5602 return error;
5605 int
5606 main(int argc, char *argv[])
5608 const struct got_error *error = NULL;
5609 struct tog_cmd *cmd = NULL;
5610 int ch, hflag = 0, Vflag = 0;
5611 char **cmd_argv = NULL;
5612 static struct option longopts[] = {
5613 { "version", no_argument, NULL, 'V' },
5614 { NULL, 0, NULL, 0}
5617 setlocale(LC_CTYPE, "");
5619 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5620 switch (ch) {
5621 case 'h':
5622 hflag = 1;
5623 break;
5624 case 'V':
5625 Vflag = 1;
5626 break;
5627 default:
5628 usage(hflag);
5629 /* NOTREACHED */
5633 argc -= optind;
5634 argv += optind;
5635 optind = 0;
5636 optreset = 1;
5638 if (Vflag) {
5639 got_version_print_str();
5640 return 1;
5643 if (argc == 0) {
5644 if (hflag)
5645 usage(hflag);
5646 /* Build an argument vector which runs a default command. */
5647 cmd = &tog_commands[0];
5648 argc = 1;
5649 cmd_argv = make_argv(argc, cmd->name);
5650 } else {
5651 int i;
5653 /* Did the user specify a command? */
5654 for (i = 0; i < nitems(tog_commands); i++) {
5655 if (strncmp(tog_commands[i].name, argv[0],
5656 strlen(argv[0])) == 0) {
5657 cmd = &tog_commands[i];
5658 break;
5663 if (cmd == NULL) {
5664 if (argc != 1)
5665 usage(0);
5666 /* No command specified; try log with a path */
5667 error = tog_log_with_path(argc, argv);
5668 } else {
5669 if (hflag)
5670 cmd->cmd_usage();
5671 else
5672 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5675 endwin();
5676 if (cmd_argv) {
5677 int i;
5678 for (i = 0; i < argc; i++)
5679 free(cmd_argv[i]);
5680 free(cmd_argv);
5683 if (error && error->code != GOT_ERR_CANCELLED)
5684 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5685 return 0;