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 <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <getopt.h>
32 #include <string.h>
33 #include <err.h>
34 #include <unistd.h>
35 #include <util.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_diff.h"
49 #include "got_opentemp.h"
50 #include "got_utf8.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
55 #include "got_path.h"
56 #include "got_worktree.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #ifndef MAX
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 #endif
66 #define CTRL(x) ((x) & 0x1f)
68 #ifndef nitems
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #endif
72 struct tog_cmd {
73 const char *name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 };
78 __dead static void usage(int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
84 static const struct got_error* cmd_log(int, char *[]);
85 static const struct got_error* cmd_diff(int, char *[]);
86 static const struct got_error* cmd_blame(int, char *[]);
87 static const struct got_error* cmd_tree(int, char *[]);
89 static struct tog_cmd tog_commands[] = {
90 { "log", cmd_log, usage_log },
91 { "diff", cmd_diff, usage_diff },
92 { "blame", cmd_blame, usage_blame },
93 { "tree", cmd_tree, usage_tree },
94 };
96 enum tog_view_type {
97 TOG_VIEW_DIFF,
98 TOG_VIEW_LOG,
99 TOG_VIEW_BLAME,
100 TOG_VIEW_TREE
101 };
103 #define TOG_EOF_STRING "(END)"
105 struct commit_queue_entry {
106 TAILQ_ENTRY(commit_queue_entry) entry;
107 struct got_object_id *id;
108 struct got_commit_object *commit;
109 int idx;
110 };
111 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
112 struct commit_queue {
113 int ncommits;
114 struct commit_queue_head head;
115 };
117 struct tog_color {
118 SIMPLEQ_ENTRY(tog_color) entry;
119 regex_t regex;
120 short colorpair;
121 };
122 SIMPLEQ_HEAD(tog_colors, tog_color);
124 static const struct got_error *
125 add_color(struct tog_colors *colors, const char *pattern,
126 int idx, short color)
128 const struct got_error *err = NULL;
129 struct tog_color *tc;
130 int regerr = 0;
132 if (idx < 1 || idx > COLOR_PAIRS - 1)
133 return NULL;
135 init_pair(idx, color, -1);
137 tc = calloc(1, sizeof(*tc));
138 if (tc == NULL)
139 return got_error_from_errno("calloc");
140 regerr = regcomp(&tc->regex, pattern,
141 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
142 if (regerr) {
143 static char regerr_msg[512];
144 static char err_msg[512];
145 regerror(regerr, &tc->regex, regerr_msg,
146 sizeof(regerr_msg));
147 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
148 regerr_msg);
149 err = got_error_msg(GOT_ERR_REGEX, err_msg);
150 free(tc);
151 return err;
153 tc->colorpair = idx;
154 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
155 return NULL;
158 static void
159 free_colors(struct tog_colors *colors)
161 struct tog_color *tc;
163 while (!SIMPLEQ_EMPTY(colors)) {
164 tc = SIMPLEQ_FIRST(colors);
165 SIMPLEQ_REMOVE_HEAD(colors, entry);
166 regfree(&tc->regex);
167 free(tc);
171 struct tog_color *
172 get_color(struct tog_colors *colors, int colorpair)
174 struct tog_color *tc = NULL;
176 SIMPLEQ_FOREACH(tc, colors, entry) {
177 if (tc->colorpair == colorpair)
178 return tc;
181 return NULL;
184 static int
185 default_color_value(const char *envvar)
187 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
188 return COLOR_MAGENTA;
189 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
190 return COLOR_CYAN;
191 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
192 return COLOR_YELLOW;
193 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
194 return COLOR_GREEN;
195 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
196 return COLOR_MAGENTA;
197 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
198 return COLOR_MAGENTA;
199 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
200 return COLOR_CYAN;
201 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
202 return COLOR_GREEN;
203 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
204 return COLOR_GREEN;
205 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
206 return COLOR_CYAN;
207 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
208 return COLOR_YELLOW;
210 return -1;
213 static int
214 get_color_value(const char *envvar)
216 const char *val = getenv(envvar);
218 if (val == NULL)
219 return default_color_value(envvar);
221 if (strcasecmp(val, "black") == 0)
222 return COLOR_BLACK;
223 if (strcasecmp(val, "red") == 0)
224 return COLOR_RED;
225 if (strcasecmp(val, "green") == 0)
226 return COLOR_GREEN;
227 if (strcasecmp(val, "yellow") == 0)
228 return COLOR_YELLOW;
229 if (strcasecmp(val, "blue") == 0)
230 return COLOR_BLUE;
231 if (strcasecmp(val, "magenta") == 0)
232 return COLOR_MAGENTA;
233 if (strcasecmp(val, "cyan") == 0)
234 return COLOR_CYAN;
235 if (strcasecmp(val, "white") == 0)
236 return COLOR_WHITE;
237 if (strcasecmp(val, "default") == 0)
238 return -1;
240 return default_color_value(envvar);
244 struct tog_diff_view_state {
245 struct got_object_id *id1, *id2;
246 FILE *f;
247 int first_displayed_line;
248 int last_displayed_line;
249 int eof;
250 int diff_context;
251 struct got_repository *repo;
252 struct got_reflist_head *refs;
253 struct tog_colors colors;
254 int nlines;
255 off_t *line_offsets;
256 int matched_line;
257 int selected_line;
258 size_t filesize;
260 /* passed from log view; may be NULL */
261 struct tog_view *log_view;
262 };
264 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
266 struct tog_log_thread_args {
267 pthread_cond_t need_commits;
268 pthread_cond_t commit_loaded;
269 int commits_needed;
270 struct got_commit_graph *graph;
271 struct commit_queue *commits;
272 const char *in_repo_path;
273 struct got_object_id *start_id;
274 struct got_repository *repo;
275 int log_complete;
276 sig_atomic_t *quit;
277 struct commit_queue_entry **first_displayed_entry;
278 struct commit_queue_entry **selected_entry;
279 int *searching;
280 int *search_next_done;
281 regex_t *regex;
282 };
284 struct tog_log_view_state {
285 struct commit_queue commits;
286 struct commit_queue_entry *first_displayed_entry;
287 struct commit_queue_entry *last_displayed_entry;
288 struct commit_queue_entry *selected_entry;
289 int selected;
290 char *in_repo_path;
291 const char *head_ref_name;
292 int log_branches;
293 struct got_repository *repo;
294 struct got_reflist_head *refs;
295 struct got_object_id *start_id;
296 sig_atomic_t quit;
297 pthread_t thread;
298 struct tog_log_thread_args thread_args;
299 struct commit_queue_entry *matched_entry;
300 struct commit_queue_entry *search_entry;
301 struct tog_colors colors;
302 };
304 #define TOG_COLOR_DIFF_MINUS 1
305 #define TOG_COLOR_DIFF_PLUS 2
306 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
307 #define TOG_COLOR_DIFF_META 4
308 #define TOG_COLOR_TREE_SUBMODULE 5
309 #define TOG_COLOR_TREE_SYMLINK 6
310 #define TOG_COLOR_TREE_DIRECTORY 7
311 #define TOG_COLOR_TREE_EXECUTABLE 8
312 #define TOG_COLOR_COMMIT 9
313 #define TOG_COLOR_AUTHOR 10
314 #define TOG_COLOR_DATE 11
316 struct tog_blame_cb_args {
317 struct tog_blame_line *lines; /* one per line */
318 int nlines;
320 struct tog_view *view;
321 struct got_object_id *commit_id;
322 int *quit;
323 };
325 struct tog_blame_thread_args {
326 const char *path;
327 struct got_repository *repo;
328 struct tog_blame_cb_args *cb_args;
329 int *complete;
330 got_cancel_cb cancel_cb;
331 void *cancel_arg;
332 };
334 struct tog_blame {
335 FILE *f;
336 size_t filesize;
337 struct tog_blame_line *lines;
338 int nlines;
339 off_t *line_offsets;
340 pthread_t thread;
341 struct tog_blame_thread_args thread_args;
342 struct tog_blame_cb_args cb_args;
343 const char *path;
344 };
346 struct tog_blame_view_state {
347 int first_displayed_line;
348 int last_displayed_line;
349 int selected_line;
350 int blame_complete;
351 int eof;
352 int done;
353 struct got_object_id_queue blamed_commits;
354 struct got_object_qid *blamed_commit;
355 char *path;
356 struct got_repository *repo;
357 struct got_reflist_head *refs;
358 struct got_object_id *commit_id;
359 struct tog_blame blame;
360 int matched_line;
361 struct tog_colors colors;
362 };
364 struct tog_parent_tree {
365 TAILQ_ENTRY(tog_parent_tree) entry;
366 struct got_tree_object *tree;
367 struct got_tree_entry *first_displayed_entry;
368 struct got_tree_entry *selected_entry;
369 int selected;
370 };
372 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
374 struct tog_tree_view_state {
375 char *tree_label;
376 struct got_tree_object *root;
377 struct got_tree_object *tree;
378 struct got_tree_entry *first_displayed_entry;
379 struct got_tree_entry *last_displayed_entry;
380 struct got_tree_entry *selected_entry;
381 int ndisplayed, selected, show_ids;
382 struct tog_parent_trees parents;
383 struct got_object_id *commit_id;
384 struct got_repository *repo;
385 struct got_reflist_head *refs;
386 struct got_tree_entry *matched_entry;
387 struct tog_colors colors;
388 };
390 /*
391 * We implement two types of views: parent views and child views.
393 * The 'Tab' key switches between a parent view and its child view.
394 * Child views are shown side-by-side to their parent view, provided
395 * there is enough screen estate.
397 * When a new view is opened from within a parent view, this new view
398 * becomes a child view of the parent view, replacing any existing child.
400 * When a new view is opened from within a child view, this new view
401 * becomes a parent view which will obscure the views below until the
402 * user quits the new parent view by typing 'q'.
404 * This list of views contains parent views only.
405 * Child views are only pointed to by their parent view.
406 */
407 TAILQ_HEAD(tog_view_list_head, tog_view);
409 struct tog_view {
410 TAILQ_ENTRY(tog_view) entry;
411 WINDOW *window;
412 PANEL *panel;
413 int nlines, ncols, begin_y, begin_x;
414 int lines, cols; /* copies of LINES and COLS */
415 int focussed;
416 struct tog_view *parent;
417 struct tog_view *child;
418 int child_focussed;
420 /* type-specific state */
421 enum tog_view_type type;
422 union {
423 struct tog_diff_view_state diff;
424 struct tog_log_view_state log;
425 struct tog_blame_view_state blame;
426 struct tog_tree_view_state tree;
427 } state;
429 const struct got_error *(*show)(struct tog_view *);
430 const struct got_error *(*input)(struct tog_view **,
431 struct tog_view **, struct tog_view**, struct tog_view *, int);
432 const struct got_error *(*close)(struct tog_view *);
434 const struct got_error *(*search_start)(struct tog_view *);
435 const struct got_error *(*search_next)(struct tog_view *);
436 int searching;
437 #define TOG_SEARCH_FORWARD 1
438 #define TOG_SEARCH_BACKWARD 2
439 int search_next_done;
440 #define TOG_SEARCH_HAVE_MORE 1
441 #define TOG_SEARCH_NO_MORE 2
442 #define TOG_SEARCH_HAVE_NONE 3
443 regex_t regex;
444 };
446 static const struct got_error *open_diff_view(struct tog_view *,
447 struct got_object_id *, struct got_object_id *, struct tog_view *,
448 struct got_reflist_head *, struct got_repository *);
449 static const struct got_error *show_diff_view(struct tog_view *);
450 static const struct got_error *input_diff_view(struct tog_view **,
451 struct tog_view **, struct tog_view **, struct tog_view *, int);
452 static const struct got_error* close_diff_view(struct tog_view *);
453 static const struct got_error *search_start_diff_view(struct tog_view *);
454 static const struct got_error *search_next_diff_view(struct tog_view *);
456 static const struct got_error *open_log_view(struct tog_view *,
457 struct got_object_id *, struct got_reflist_head *,
458 struct got_repository *, const char *, const char *, int);
459 static const struct got_error * show_log_view(struct tog_view *);
460 static const struct got_error *input_log_view(struct tog_view **,
461 struct tog_view **, struct tog_view **, struct tog_view *, int);
462 static const struct got_error *close_log_view(struct tog_view *);
463 static const struct got_error *search_start_log_view(struct tog_view *);
464 static const struct got_error *search_next_log_view(struct tog_view *);
466 static const struct got_error *open_blame_view(struct tog_view *, char *,
467 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
468 static const struct got_error *show_blame_view(struct tog_view *);
469 static const struct got_error *input_blame_view(struct tog_view **,
470 struct tog_view **, struct tog_view **, struct tog_view *, int);
471 static const struct got_error *close_blame_view(struct tog_view *);
472 static const struct got_error *search_start_blame_view(struct tog_view *);
473 static const struct got_error *search_next_blame_view(struct tog_view *);
475 static const struct got_error *open_tree_view(struct tog_view *,
476 struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
477 struct got_repository *);
478 static const struct got_error *show_tree_view(struct tog_view *);
479 static const struct got_error *input_tree_view(struct tog_view **,
480 struct tog_view **, struct tog_view **, struct tog_view *, int);
481 static const struct got_error *close_tree_view(struct tog_view *);
482 static const struct got_error *search_start_tree_view(struct tog_view *);
483 static const struct got_error *search_next_tree_view(struct tog_view *);
485 static volatile sig_atomic_t tog_sigwinch_received;
486 static volatile sig_atomic_t tog_sigpipe_received;
487 static volatile sig_atomic_t tog_sigcont_received;
489 static void
490 tog_sigwinch(int signo)
492 tog_sigwinch_received = 1;
495 static void
496 tog_sigpipe(int signo)
498 tog_sigpipe_received = 1;
501 static void
502 tog_sigcont(int signo)
504 tog_sigcont_received = 1;
507 static const struct got_error *
508 view_close(struct tog_view *view)
510 const struct got_error *err = NULL;
512 if (view->child) {
513 view_close(view->child);
514 view->child = NULL;
516 if (view->close)
517 err = view->close(view);
518 if (view->panel)
519 del_panel(view->panel);
520 if (view->window)
521 delwin(view->window);
522 free(view);
523 return err;
526 static struct tog_view *
527 view_open(int nlines, int ncols, int begin_y, int begin_x,
528 enum tog_view_type type)
530 struct tog_view *view = calloc(1, sizeof(*view));
532 if (view == NULL)
533 return NULL;
535 view->type = type;
536 view->lines = LINES;
537 view->cols = COLS;
538 view->nlines = nlines ? nlines : LINES - begin_y;
539 view->ncols = ncols ? ncols : COLS - begin_x;
540 view->begin_y = begin_y;
541 view->begin_x = begin_x;
542 view->window = newwin(nlines, ncols, begin_y, begin_x);
543 if (view->window == NULL) {
544 view_close(view);
545 return NULL;
547 view->panel = new_panel(view->window);
548 if (view->panel == NULL ||
549 set_panel_userptr(view->panel, view) != OK) {
550 view_close(view);
551 return NULL;
554 keypad(view->window, TRUE);
555 return view;
558 static int
559 view_split_begin_x(int begin_x)
561 if (begin_x > 0 || COLS < 120)
562 return 0;
563 return (COLS - MAX(COLS / 2, 80));
566 static const struct got_error *view_resize(struct tog_view *);
568 static const struct got_error *
569 view_splitscreen(struct tog_view *view)
571 const struct got_error *err = NULL;
573 view->begin_y = 0;
574 view->begin_x = view_split_begin_x(0);
575 view->nlines = LINES;
576 view->ncols = COLS - view->begin_x;
577 view->lines = LINES;
578 view->cols = COLS;
579 err = view_resize(view);
580 if (err)
581 return err;
583 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
584 return got_error_from_errno("mvwin");
586 return NULL;
589 static const struct got_error *
590 view_fullscreen(struct tog_view *view)
592 const struct got_error *err = NULL;
594 view->begin_x = 0;
595 view->begin_y = 0;
596 view->nlines = LINES;
597 view->ncols = COLS;
598 view->lines = LINES;
599 view->cols = COLS;
600 err = view_resize(view);
601 if (err)
602 return err;
604 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
605 return got_error_from_errno("mvwin");
607 return NULL;
610 static int
611 view_is_parent_view(struct tog_view *view)
613 return view->parent == NULL;
616 static const struct got_error *
617 view_resize(struct tog_view *view)
619 int nlines, ncols;
621 if (view->lines > LINES)
622 nlines = view->nlines - (view->lines - LINES);
623 else
624 nlines = view->nlines + (LINES - view->lines);
626 if (view->cols > COLS)
627 ncols = view->ncols - (view->cols - COLS);
628 else
629 ncols = view->ncols + (COLS - view->cols);
631 if (wresize(view->window, nlines, ncols) == ERR)
632 return got_error_from_errno("wresize");
633 if (replace_panel(view->panel, view->window) == ERR)
634 return got_error_from_errno("replace_panel");
635 wclear(view->window);
637 view->nlines = nlines;
638 view->ncols = ncols;
639 view->lines = LINES;
640 view->cols = COLS;
642 if (view->child) {
643 view->child->begin_x = view_split_begin_x(view->begin_x);
644 if (view->child->begin_x == 0) {
645 view_fullscreen(view->child);
646 if (view->child->focussed)
647 show_panel(view->child->panel);
648 else
649 show_panel(view->panel);
650 } else {
651 view_splitscreen(view->child);
652 show_panel(view->child->panel);
656 return NULL;
659 static const struct got_error *
660 view_close_child(struct tog_view *view)
662 const struct got_error *err = NULL;
664 if (view->child == NULL)
665 return NULL;
667 err = view_close(view->child);
668 view->child = NULL;
669 return err;
672 static const struct got_error *
673 view_set_child(struct tog_view *view, struct tog_view *child)
675 const struct got_error *err = NULL;
677 view->child = child;
678 child->parent = view;
679 return err;
682 static int
683 view_is_splitscreen(struct tog_view *view)
685 return view->begin_x > 0;
688 static void
689 tog_resizeterm(void)
691 int cols, lines;
692 struct winsize size;
694 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
695 cols = 80; /* Default */
696 lines = 24;
697 } else {
698 cols = size.ws_col;
699 lines = size.ws_row;
701 resize_term(lines, cols);
704 static const struct got_error *
705 view_search_start(struct tog_view *view)
707 const struct got_error *err = NULL;
708 char pattern[1024];
709 int ret;
711 if (view->nlines < 1)
712 return NULL;
714 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
715 wclrtoeol(view->window);
717 nocbreak();
718 echo();
719 ret = wgetnstr(view->window, pattern, sizeof(pattern));
720 cbreak();
721 noecho();
722 if (ret == ERR)
723 return NULL;
725 if (view->searching) {
726 regfree(&view->regex);
727 view->searching = 0;
730 if (regcomp(&view->regex, pattern,
731 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
732 err = view->search_start(view);
733 if (err) {
734 regfree(&view->regex);
735 return err;
737 view->searching = TOG_SEARCH_FORWARD;
738 view->search_next_done = 0;
739 view->search_next(view);
742 return NULL;
745 static const struct got_error *
746 view_input(struct tog_view **new, struct tog_view **dead,
747 struct tog_view **focus, int *done, struct tog_view *view,
748 struct tog_view_list_head *views)
750 const struct got_error *err = NULL;
751 struct tog_view *v;
752 int ch, errcode;
754 *new = NULL;
755 *dead = NULL;
756 *focus = NULL;
758 /* Clear "no matches" indicator. */
759 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
760 view->search_next_done == TOG_SEARCH_HAVE_NONE)
761 view->search_next_done = TOG_SEARCH_HAVE_MORE;
763 if (view->searching && !view->search_next_done) {
764 errcode = pthread_mutex_unlock(&tog_mutex);
765 if (errcode)
766 return got_error_set_errno(errcode,
767 "pthread_mutex_unlock");
768 pthread_yield();
769 errcode = pthread_mutex_lock(&tog_mutex);
770 if (errcode)
771 return got_error_set_errno(errcode,
772 "pthread_mutex_lock");
773 view->search_next(view);
774 return NULL;
777 nodelay(stdscr, FALSE);
778 /* Allow threads to make progress while we are waiting for input. */
779 errcode = pthread_mutex_unlock(&tog_mutex);
780 if (errcode)
781 return got_error_set_errno(errcode, "pthread_mutex_unlock");
782 ch = wgetch(view->window);
783 errcode = pthread_mutex_lock(&tog_mutex);
784 if (errcode)
785 return got_error_set_errno(errcode, "pthread_mutex_lock");
786 nodelay(stdscr, TRUE);
788 if (tog_sigwinch_received || tog_sigcont_received) {
789 tog_resizeterm();
790 tog_sigwinch_received = 0;
791 tog_sigcont_received = 0;
792 TAILQ_FOREACH(v, views, entry) {
793 err = view_resize(v);
794 if (err)
795 return err;
796 err = v->input(new, dead, focus, v, KEY_RESIZE);
797 if (err)
798 return err;
802 switch (ch) {
803 case ERR:
804 break;
805 case '\t':
806 if (view->child) {
807 *focus = view->child;
808 view->child_focussed = 1;
809 } else if (view->parent) {
810 *focus = view->parent;
811 view->parent->child_focussed = 0;
813 break;
814 case 'q':
815 err = view->input(new, dead, focus, view, ch);
816 *dead = view;
817 break;
818 case 'Q':
819 *done = 1;
820 break;
821 case 'f':
822 if (view_is_parent_view(view)) {
823 if (view->child == NULL)
824 break;
825 if (view_is_splitscreen(view->child)) {
826 *focus = view->child;
827 view->child_focussed = 1;
828 err = view_fullscreen(view->child);
829 } else
830 err = view_splitscreen(view->child);
831 if (err)
832 break;
833 err = view->child->input(new, dead, focus,
834 view->child, KEY_RESIZE);
835 } else {
836 if (view_is_splitscreen(view)) {
837 *focus = view;
838 view->parent->child_focussed = 1;
839 err = view_fullscreen(view);
840 } else {
841 err = view_splitscreen(view);
843 if (err)
844 break;
845 err = view->input(new, dead, focus, view,
846 KEY_RESIZE);
848 break;
849 case KEY_RESIZE:
850 break;
851 case '/':
852 if (view->search_start)
853 view_search_start(view);
854 else
855 err = view->input(new, dead, focus, view, ch);
856 break;
857 case 'N':
858 case 'n':
859 if (view->search_next) {
860 view->searching = (ch == 'n' ?
861 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
862 view->search_next_done = 0;
863 view->search_next(view);
864 } else
865 err = view->input(new, dead, focus, view, ch);
866 break;
867 default:
868 err = view->input(new, dead, focus, view, ch);
869 break;
872 return err;
875 void
876 view_vborder(struct tog_view *view)
878 PANEL *panel;
879 struct tog_view *view_above;
881 if (view->parent)
882 return view_vborder(view->parent);
884 panel = panel_above(view->panel);
885 if (panel == NULL)
886 return;
888 view_above = panel_userptr(panel);
889 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
890 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
893 int
894 view_needs_focus_indication(struct tog_view *view)
896 if (view_is_parent_view(view)) {
897 if (view->child == NULL || view->child_focussed)
898 return 0;
899 if (!view_is_splitscreen(view->child))
900 return 0;
901 } else if (!view_is_splitscreen(view))
902 return 0;
904 return view->focussed;
907 static const struct got_error *
908 view_loop(struct tog_view *view)
910 const struct got_error *err = NULL;
911 struct tog_view_list_head views;
912 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
913 int fast_refresh = 10;
914 int done = 0, errcode;
916 errcode = pthread_mutex_lock(&tog_mutex);
917 if (errcode)
918 return got_error_set_errno(errcode, "pthread_mutex_lock");
920 TAILQ_INIT(&views);
921 TAILQ_INSERT_HEAD(&views, view, entry);
923 main_view = view;
924 view->focussed = 1;
925 err = view->show(view);
926 if (err)
927 return err;
928 update_panels();
929 doupdate();
930 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
931 /* Refresh fast during initialization, then become slower. */
932 if (fast_refresh && fast_refresh-- == 0)
933 halfdelay(10); /* switch to once per second */
935 err = view_input(&new_view, &dead_view, &focus_view, &done,
936 view, &views);
937 if (err)
938 break;
939 if (dead_view) {
940 struct tog_view *prev = NULL;
942 if (view_is_parent_view(dead_view))
943 prev = TAILQ_PREV(dead_view,
944 tog_view_list_head, entry);
945 else if (view->parent != dead_view)
946 prev = view->parent;
948 if (dead_view->parent)
949 dead_view->parent->child = NULL;
950 else
951 TAILQ_REMOVE(&views, dead_view, entry);
953 err = view_close(dead_view);
954 if (err || (dead_view == main_view && new_view == NULL))
955 goto done;
957 if (view == dead_view) {
958 if (focus_view)
959 view = focus_view;
960 else if (prev)
961 view = prev;
962 else if (!TAILQ_EMPTY(&views))
963 view = TAILQ_LAST(&views,
964 tog_view_list_head);
965 else
966 view = NULL;
967 if (view) {
968 if (view->child && view->child_focussed)
969 focus_view = view->child;
970 else
971 focus_view = view;
975 if (new_view) {
976 struct tog_view *v, *t;
977 /* Only allow one parent view per type. */
978 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
979 if (v->type != new_view->type)
980 continue;
981 TAILQ_REMOVE(&views, v, entry);
982 err = view_close(v);
983 if (err)
984 goto done;
985 break;
987 TAILQ_INSERT_TAIL(&views, new_view, entry);
988 view = new_view;
989 if (focus_view == NULL)
990 focus_view = new_view;
992 if (focus_view) {
993 show_panel(focus_view->panel);
994 if (view)
995 view->focussed = 0;
996 focus_view->focussed = 1;
997 view = focus_view;
998 if (new_view)
999 show_panel(new_view->panel);
1000 if (view->child && view_is_splitscreen(view->child))
1001 show_panel(view->child->panel);
1003 if (view) {
1004 if (focus_view == NULL) {
1005 view->focussed = 1;
1006 show_panel(view->panel);
1007 if (view->child && view_is_splitscreen(view->child))
1008 show_panel(view->child->panel);
1009 focus_view = view;
1011 if (view->parent) {
1012 err = view->parent->show(view->parent);
1013 if (err)
1014 goto done;
1016 err = view->show(view);
1017 if (err)
1018 goto done;
1019 if (view->child) {
1020 err = view->child->show(view->child);
1021 if (err)
1022 goto done;
1024 update_panels();
1025 doupdate();
1028 done:
1029 while (!TAILQ_EMPTY(&views)) {
1030 view = TAILQ_FIRST(&views);
1031 TAILQ_REMOVE(&views, view, entry);
1032 view_close(view);
1035 errcode = pthread_mutex_unlock(&tog_mutex);
1036 if (errcode && err == NULL)
1037 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1039 return err;
1042 __dead static void
1043 usage_log(void)
1045 endwin();
1046 fprintf(stderr,
1047 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1048 getprogname());
1049 exit(1);
1052 /* Create newly allocated wide-character string equivalent to a byte string. */
1053 static const struct got_error *
1054 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1056 char *vis = NULL;
1057 const struct got_error *err = NULL;
1059 *ws = NULL;
1060 *wlen = mbstowcs(NULL, s, 0);
1061 if (*wlen == (size_t)-1) {
1062 int vislen;
1063 if (errno != EILSEQ)
1064 return got_error_from_errno("mbstowcs");
1066 /* byte string invalid in current encoding; try to "fix" it */
1067 err = got_mbsavis(&vis, &vislen, s);
1068 if (err)
1069 return err;
1070 *wlen = mbstowcs(NULL, vis, 0);
1071 if (*wlen == (size_t)-1) {
1072 err = got_error_from_errno("mbstowcs"); /* give up */
1073 goto done;
1077 *ws = calloc(*wlen + 1, sizeof(**ws));
1078 if (*ws == NULL) {
1079 err = got_error_from_errno("calloc");
1080 goto done;
1083 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1084 err = got_error_from_errno("mbstowcs");
1085 done:
1086 free(vis);
1087 if (err) {
1088 free(*ws);
1089 *ws = NULL;
1090 *wlen = 0;
1092 return err;
1095 /* Format a line for display, ensuring that it won't overflow a width limit. */
1096 static const struct got_error *
1097 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1098 int col_tab_align)
1100 const struct got_error *err = NULL;
1101 int cols = 0;
1102 wchar_t *wline = NULL;
1103 size_t wlen;
1104 int i;
1106 *wlinep = NULL;
1107 *widthp = 0;
1109 err = mbs2ws(&wline, &wlen, line);
1110 if (err)
1111 return err;
1113 i = 0;
1114 while (i < wlen) {
1115 int width = wcwidth(wline[i]);
1117 if (width == 0) {
1118 i++;
1119 continue;
1122 if (width == 1 || width == 2) {
1123 if (cols + width > wlimit)
1124 break;
1125 cols += width;
1126 i++;
1127 } else if (width == -1) {
1128 if (wline[i] == L'\t') {
1129 width = TABSIZE -
1130 ((cols + col_tab_align) % TABSIZE);
1131 if (cols + width > wlimit)
1132 break;
1133 cols += width;
1135 i++;
1136 } else {
1137 err = got_error_from_errno("wcwidth");
1138 goto done;
1141 wline[i] = L'\0';
1142 if (widthp)
1143 *widthp = cols;
1144 done:
1145 if (err)
1146 free(wline);
1147 else
1148 *wlinep = wline;
1149 return err;
1152 static const struct got_error*
1153 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1154 struct got_object_id *id, struct got_repository *repo)
1156 static const struct got_error *err = NULL;
1157 struct got_reflist_entry *re;
1158 char *s;
1159 const char *name;
1161 *refs_str = NULL;
1163 SIMPLEQ_FOREACH(re, refs, entry) {
1164 struct got_tag_object *tag = NULL;
1165 int cmp;
1167 name = got_ref_get_name(re->ref);
1168 if (strcmp(name, GOT_REF_HEAD) == 0)
1169 continue;
1170 if (strncmp(name, "refs/", 5) == 0)
1171 name += 5;
1172 if (strncmp(name, "got/", 4) == 0)
1173 continue;
1174 if (strncmp(name, "heads/", 6) == 0)
1175 name += 6;
1176 if (strncmp(name, "remotes/", 8) == 0) {
1177 name += 8;
1178 s = strstr(name, "/" GOT_REF_HEAD);
1179 if (s != NULL && s[strlen(s)] == '\0')
1180 continue;
1182 if (strncmp(name, "tags/", 5) == 0) {
1183 err = got_object_open_as_tag(&tag, repo, re->id);
1184 if (err) {
1185 if (err->code != GOT_ERR_OBJ_TYPE)
1186 break;
1187 /* Ref points at something other than a tag. */
1188 err = NULL;
1189 tag = NULL;
1192 cmp = got_object_id_cmp(tag ?
1193 got_object_tag_get_object_id(tag) : re->id, id);
1194 if (tag)
1195 got_object_tag_close(tag);
1196 if (cmp != 0)
1197 continue;
1198 s = *refs_str;
1199 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1200 s ? ", " : "", name) == -1) {
1201 err = got_error_from_errno("asprintf");
1202 free(s);
1203 *refs_str = NULL;
1204 break;
1206 free(s);
1209 return err;
1212 static const struct got_error *
1213 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1214 int col_tab_align)
1216 char *smallerthan, *at;
1218 smallerthan = strchr(author, '<');
1219 if (smallerthan && smallerthan[1] != '\0')
1220 author = smallerthan + 1;
1221 at = strchr(author, '@');
1222 if (at)
1223 *at = '\0';
1224 return format_line(wauthor, author_width, author, limit, col_tab_align);
1227 static const struct got_error *
1228 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1229 struct got_object_id *id, struct got_reflist_head *refs,
1230 const size_t date_display_cols, int author_display_cols,
1231 struct tog_colors *colors)
1233 const struct got_error *err = NULL;
1234 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1235 char *logmsg0 = NULL, *logmsg = NULL;
1236 char *author = NULL;
1237 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1238 int author_width, logmsg_width;
1239 char *newline, *line = NULL;
1240 int col, limit;
1241 const int avail = view->ncols;
1242 struct tm tm;
1243 time_t committer_time;
1244 struct tog_color *tc;
1246 committer_time = got_object_commit_get_committer_time(commit);
1247 if (localtime_r(&committer_time, &tm) == NULL)
1248 return got_error_from_errno("localtime_r");
1249 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1250 >= sizeof(datebuf))
1251 return got_error(GOT_ERR_NO_SPACE);
1253 if (avail <= date_display_cols)
1254 limit = MIN(sizeof(datebuf) - 1, avail);
1255 else
1256 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1257 tc = get_color(colors, TOG_COLOR_DATE);
1258 if (tc)
1259 wattr_on(view->window,
1260 COLOR_PAIR(tc->colorpair), NULL);
1261 waddnstr(view->window, datebuf, limit);
1262 if (tc)
1263 wattr_off(view->window,
1264 COLOR_PAIR(tc->colorpair), NULL);
1265 col = limit;
1266 if (col > avail)
1267 goto done;
1269 if (avail >= 120) {
1270 char *id_str;
1271 err = got_object_id_str(&id_str, id);
1272 if (err)
1273 goto done;
1274 tc = get_color(colors, TOG_COLOR_COMMIT);
1275 if (tc)
1276 wattr_on(view->window,
1277 COLOR_PAIR(tc->colorpair), NULL);
1278 wprintw(view->window, "%.8s ", id_str);
1279 if (tc)
1280 wattr_off(view->window,
1281 COLOR_PAIR(tc->colorpair), NULL);
1282 free(id_str);
1283 col += 9;
1284 if (col > avail)
1285 goto done;
1288 author = strdup(got_object_commit_get_author(commit));
1289 if (author == NULL) {
1290 err = got_error_from_errno("strdup");
1291 goto done;
1293 err = format_author(&wauthor, &author_width, author, avail - col, col);
1294 if (err)
1295 goto done;
1296 tc = get_color(colors, TOG_COLOR_AUTHOR);
1297 if (tc)
1298 wattr_on(view->window,
1299 COLOR_PAIR(tc->colorpair), NULL);
1300 waddwstr(view->window, wauthor);
1301 if (tc)
1302 wattr_off(view->window,
1303 COLOR_PAIR(tc->colorpair), NULL);
1304 col += author_width;
1305 while (col < avail && author_width < author_display_cols + 2) {
1306 waddch(view->window, ' ');
1307 col++;
1308 author_width++;
1310 if (col > avail)
1311 goto done;
1313 err = got_object_commit_get_logmsg(&logmsg0, commit);
1314 if (err)
1315 goto done;
1316 logmsg = logmsg0;
1317 while (*logmsg == '\n')
1318 logmsg++;
1319 newline = strchr(logmsg, '\n');
1320 if (newline)
1321 *newline = '\0';
1322 limit = avail - col;
1323 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1324 if (err)
1325 goto done;
1326 waddwstr(view->window, wlogmsg);
1327 col += logmsg_width;
1328 while (col < avail) {
1329 waddch(view->window, ' ');
1330 col++;
1332 done:
1333 free(logmsg0);
1334 free(wlogmsg);
1335 free(author);
1336 free(wauthor);
1337 free(line);
1338 return err;
1341 static struct commit_queue_entry *
1342 alloc_commit_queue_entry(struct got_commit_object *commit,
1343 struct got_object_id *id)
1345 struct commit_queue_entry *entry;
1347 entry = calloc(1, sizeof(*entry));
1348 if (entry == NULL)
1349 return NULL;
1351 entry->id = id;
1352 entry->commit = commit;
1353 return entry;
1356 static void
1357 pop_commit(struct commit_queue *commits)
1359 struct commit_queue_entry *entry;
1361 entry = TAILQ_FIRST(&commits->head);
1362 TAILQ_REMOVE(&commits->head, entry, entry);
1363 got_object_commit_close(entry->commit);
1364 commits->ncommits--;
1365 /* Don't free entry->id! It is owned by the commit graph. */
1366 free(entry);
1369 static void
1370 free_commits(struct commit_queue *commits)
1372 while (!TAILQ_EMPTY(&commits->head))
1373 pop_commit(commits);
1376 static const struct got_error *
1377 match_commit(int *have_match, struct got_object_id *id,
1378 struct got_commit_object *commit, regex_t *regex)
1380 const struct got_error *err = NULL;
1381 regmatch_t regmatch;
1382 char *id_str = NULL, *logmsg = NULL;
1384 *have_match = 0;
1386 err = got_object_id_str(&id_str, id);
1387 if (err)
1388 return err;
1390 err = got_object_commit_get_logmsg(&logmsg, commit);
1391 if (err)
1392 goto done;
1394 if (regexec(regex, got_object_commit_get_author(commit), 1,
1395 &regmatch, 0) == 0 ||
1396 regexec(regex, got_object_commit_get_committer(commit), 1,
1397 &regmatch, 0) == 0 ||
1398 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1399 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1400 *have_match = 1;
1401 done:
1402 free(id_str);
1403 free(logmsg);
1404 return err;
1407 static const struct got_error *
1408 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1409 int minqueue, struct got_repository *repo, const char *path,
1410 int *searching, int *search_next_done, regex_t *regex)
1412 const struct got_error *err = NULL;
1413 int nqueued = 0;
1416 * We keep all commits open throughout the lifetime of the log
1417 * view in order to avoid having to re-fetch commits from disk
1418 * while updating the display.
1420 while (nqueued < minqueue ||
1421 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1422 struct got_object_id *id;
1423 struct got_commit_object *commit;
1424 struct commit_queue_entry *entry;
1425 int errcode;
1427 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1428 if (err || id == NULL)
1429 break;
1431 err = got_object_open_as_commit(&commit, repo, id);
1432 if (err)
1433 break;
1434 entry = alloc_commit_queue_entry(commit, id);
1435 if (entry == NULL) {
1436 err = got_error_from_errno("alloc_commit_queue_entry");
1437 break;
1440 errcode = pthread_mutex_lock(&tog_mutex);
1441 if (errcode) {
1442 err = got_error_set_errno(errcode,
1443 "pthread_mutex_lock");
1444 break;
1447 entry->idx = commits->ncommits;
1448 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1449 nqueued++;
1450 commits->ncommits++;
1452 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1453 int have_match;
1454 err = match_commit(&have_match, id, commit, regex);
1455 if (err)
1456 break;
1457 if (have_match)
1458 *search_next_done = TOG_SEARCH_HAVE_MORE;
1461 errcode = pthread_mutex_unlock(&tog_mutex);
1462 if (errcode && err == NULL)
1463 err = got_error_set_errno(errcode,
1464 "pthread_mutex_unlock");
1465 if (err)
1466 break;
1469 return err;
1472 static const struct got_error *
1473 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1474 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1475 struct commit_queue *commits, int selected_idx, int limit,
1476 struct got_reflist_head *refs, const char *path, int commits_needed,
1477 struct tog_colors *colors)
1479 const struct got_error *err = NULL;
1480 struct tog_log_view_state *s = &view->state.log;
1481 struct commit_queue_entry *entry;
1482 int width;
1483 int ncommits, author_cols = 4;
1484 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1485 char *refs_str = NULL;
1486 wchar_t *wline;
1487 struct tog_color *tc;
1488 static const size_t date_display_cols = 12;
1490 entry = first;
1491 ncommits = 0;
1492 while (entry) {
1493 if (ncommits == selected_idx) {
1494 *selected = entry;
1495 break;
1497 entry = TAILQ_NEXT(entry, entry);
1498 ncommits++;
1501 if (*selected && !(view->searching && view->search_next_done == 0)) {
1502 err = got_object_id_str(&id_str, (*selected)->id);
1503 if (err)
1504 return err;
1505 if (refs) {
1506 err = build_refs_str(&refs_str, refs, (*selected)->id,
1507 s->repo);
1508 if (err)
1509 goto done;
1513 if (commits_needed == 0)
1514 halfdelay(10); /* disable fast refresh */
1516 if (commits_needed > 0) {
1517 if (asprintf(&ncommits_str, " [%d/%d] %s",
1518 entry ? entry->idx + 1 : 0, commits->ncommits,
1519 (view->searching && !view->search_next_done) ?
1520 "searching..." : "loading...") == -1) {
1521 err = got_error_from_errno("asprintf");
1522 goto done;
1524 } else {
1525 const char *search_str = NULL;
1527 if (view->searching) {
1528 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1529 search_str = "no more matches";
1530 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1531 search_str = "no matches found";
1532 else if (!view->search_next_done)
1533 search_str = "searching...";
1536 if (asprintf(&ncommits_str, " [%d/%d] %s",
1537 entry ? entry->idx + 1 : 0, commits->ncommits,
1538 search_str ? search_str :
1539 (refs_str ? refs_str : "")) == -1) {
1540 err = got_error_from_errno("asprintf");
1541 goto done;
1545 if (path && strcmp(path, "/") != 0) {
1546 if (asprintf(&header, "commit %s %s%s",
1547 id_str ? id_str : "........................................",
1548 path, ncommits_str) == -1) {
1549 err = got_error_from_errno("asprintf");
1550 header = NULL;
1551 goto done;
1553 } else if (asprintf(&header, "commit %s%s",
1554 id_str ? id_str : "........................................",
1555 ncommits_str) == -1) {
1556 err = got_error_from_errno("asprintf");
1557 header = NULL;
1558 goto done;
1560 err = format_line(&wline, &width, header, view->ncols, 0);
1561 if (err)
1562 goto done;
1564 werase(view->window);
1566 if (view_needs_focus_indication(view))
1567 wstandout(view->window);
1568 tc = get_color(colors, TOG_COLOR_COMMIT);
1569 if (tc)
1570 wattr_on(view->window,
1571 COLOR_PAIR(tc->colorpair), NULL);
1572 waddwstr(view->window, wline);
1573 if (tc)
1574 wattr_off(view->window,
1575 COLOR_PAIR(tc->colorpair), NULL);
1576 while (width < view->ncols) {
1577 waddch(view->window, ' ');
1578 width++;
1580 if (view_needs_focus_indication(view))
1581 wstandend(view->window);
1582 free(wline);
1583 if (limit <= 1)
1584 goto done;
1586 /* Grow author column size if necessary. */
1587 entry = first;
1588 ncommits = 0;
1589 while (entry) {
1590 char *author;
1591 wchar_t *wauthor;
1592 int width;
1593 if (ncommits >= limit - 1)
1594 break;
1595 author = strdup(got_object_commit_get_author(entry->commit));
1596 if (author == NULL) {
1597 err = got_error_from_errno("strdup");
1598 goto done;
1600 err = format_author(&wauthor, &width, author, COLS,
1601 date_display_cols);
1602 if (author_cols < width)
1603 author_cols = width;
1604 free(wauthor);
1605 free(author);
1606 ncommits++;
1607 entry = TAILQ_NEXT(entry, entry);
1610 entry = first;
1611 *last = first;
1612 ncommits = 0;
1613 while (entry) {
1614 if (ncommits >= limit - 1)
1615 break;
1616 if (ncommits == selected_idx)
1617 wstandout(view->window);
1618 err = draw_commit(view, entry->commit, entry->id, refs,
1619 date_display_cols, author_cols, colors);
1620 if (ncommits == selected_idx)
1621 wstandend(view->window);
1622 if (err)
1623 goto done;
1624 ncommits++;
1625 *last = entry;
1626 entry = TAILQ_NEXT(entry, entry);
1629 view_vborder(view);
1630 done:
1631 free(id_str);
1632 free(refs_str);
1633 free(ncommits_str);
1634 free(header);
1635 return err;
1638 static void
1639 scroll_up(struct tog_view *view,
1640 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1641 struct commit_queue *commits)
1643 struct commit_queue_entry *entry;
1644 int nscrolled = 0;
1646 entry = TAILQ_FIRST(&commits->head);
1647 if (*first_displayed_entry == entry)
1648 return;
1650 entry = *first_displayed_entry;
1651 while (entry && nscrolled < maxscroll) {
1652 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1653 if (entry) {
1654 *first_displayed_entry = entry;
1655 nscrolled++;
1660 static const struct got_error *
1661 trigger_log_thread(struct tog_view *log_view, int wait,
1662 int *commits_needed, int *log_complete,
1663 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1665 int errcode;
1667 halfdelay(1); /* fast refresh while loading commits */
1669 while (*commits_needed > 0) {
1670 if (*log_complete)
1671 break;
1673 /* Wake the log thread. */
1674 errcode = pthread_cond_signal(need_commits);
1675 if (errcode)
1676 return got_error_set_errno(errcode,
1677 "pthread_cond_signal");
1680 * The mutex will be released while the view loop waits
1681 * in wgetch(), at which time the log thread will run.
1683 if (!wait)
1684 break;
1686 /* Display progress update in log view. */
1687 show_log_view(log_view);
1688 update_panels();
1689 doupdate();
1691 /* Wait right here while next commit is being loaded. */
1692 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1693 if (errcode)
1694 return got_error_set_errno(errcode,
1695 "pthread_cond_wait");
1697 /* Display progress update in log view. */
1698 show_log_view(log_view);
1699 update_panels();
1700 doupdate();
1703 return NULL;
1706 static const struct got_error *
1707 scroll_down(struct tog_view *log_view,
1708 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1709 struct commit_queue_entry **last_displayed_entry,
1710 struct commit_queue *commits, int *log_complete, int *commits_needed,
1711 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1713 const struct got_error *err = NULL;
1714 struct commit_queue_entry *pentry;
1715 int nscrolled = 0, ncommits_needed;
1717 if (*last_displayed_entry == NULL)
1718 return NULL;
1720 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1721 if (commits->ncommits < ncommits_needed && !*log_complete) {
1723 * Ask the log thread for required amount of commits.
1725 (*commits_needed) += maxscroll;
1726 err = trigger_log_thread(log_view, 1, commits_needed,
1727 log_complete, need_commits, commit_loaded);
1728 if (err)
1729 return err;
1732 do {
1733 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1734 if (pentry == NULL)
1735 break;
1737 *last_displayed_entry = pentry;
1739 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1740 if (pentry == NULL)
1741 break;
1742 *first_displayed_entry = pentry;
1743 } while (++nscrolled < maxscroll);
1745 return err;
1748 static const struct got_error *
1749 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1750 struct got_commit_object *commit, struct got_object_id *commit_id,
1751 struct tog_view *log_view, struct got_reflist_head *refs,
1752 struct got_repository *repo)
1754 const struct got_error *err;
1755 struct got_object_qid *parent_id;
1756 struct tog_view *diff_view;
1758 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1759 if (diff_view == NULL)
1760 return got_error_from_errno("view_open");
1762 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1763 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1764 commit_id, log_view, refs, repo);
1765 if (err == NULL)
1766 *new_view = diff_view;
1767 return err;
1770 static const struct got_error *
1771 tree_view_visit_subtree(struct got_tree_object *subtree,
1772 struct tog_tree_view_state *s)
1774 struct tog_parent_tree *parent;
1776 parent = calloc(1, sizeof(*parent));
1777 if (parent == NULL)
1778 return got_error_from_errno("calloc");
1780 parent->tree = s->tree;
1781 parent->first_displayed_entry = s->first_displayed_entry;
1782 parent->selected_entry = s->selected_entry;
1783 parent->selected = s->selected;
1784 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1785 s->tree = subtree;
1786 s->selected = 0;
1787 s->first_displayed_entry = NULL;
1788 return NULL;
1791 static const struct got_error *
1792 tree_view_walk_path(struct tog_tree_view_state *s,
1793 struct got_object_id *commit_id,
1794 const char *path, struct got_repository *repo)
1796 const struct got_error *err = NULL;
1797 struct got_tree_object *tree = NULL;
1798 const char *p;
1799 char *slash, *subpath = NULL;
1801 /* Walk the path and open corresponding tree objects. */
1802 p = path;
1803 while (*p) {
1804 struct got_tree_entry *te;
1805 struct got_object_id *tree_id;
1806 char *te_name;
1808 while (p[0] == '/')
1809 p++;
1811 /* Ensure the correct subtree entry is selected. */
1812 slash = strchr(p, '/');
1813 if (slash == NULL)
1814 te_name = strdup(p);
1815 else
1816 te_name = strndup(p, slash - p);
1817 if (te_name == NULL) {
1818 err = got_error_from_errno("strndup");
1819 break;
1821 te = got_object_tree_find_entry(s->tree, te_name);
1822 if (te == NULL) {
1823 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1824 free(te_name);
1825 break;
1827 free(te_name);
1828 s->selected_entry = te;
1829 s->selected = got_tree_entry_get_index(te);
1830 if (s->tree != s->root)
1831 s->selected++; /* skip '..' */
1833 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1834 /* Jump to this file's entry. */
1835 s->first_displayed_entry = s->selected_entry;
1836 s->selected = 0;
1837 break;
1840 slash = strchr(p, '/');
1841 if (slash)
1842 subpath = strndup(path, slash - path);
1843 else
1844 subpath = strdup(path);
1845 if (subpath == NULL) {
1846 err = got_error_from_errno("strdup");
1847 break;
1850 err = got_object_id_by_path(&tree_id, repo, commit_id,
1851 subpath);
1852 if (err)
1853 break;
1855 err = got_object_open_as_tree(&tree, repo, tree_id);
1856 free(tree_id);
1857 if (err)
1858 break;
1860 err = tree_view_visit_subtree(tree, s);
1861 if (err) {
1862 got_object_tree_close(tree);
1863 break;
1865 if (slash == NULL)
1866 break;
1867 free(subpath);
1868 subpath = NULL;
1869 p = slash;
1872 free(subpath);
1873 return err;
1876 static const struct got_error *
1877 browse_commit_tree(struct tog_view **new_view, int begin_x,
1878 struct commit_queue_entry *entry, const char *path,
1879 struct got_reflist_head *refs, struct got_repository *repo)
1881 const struct got_error *err = NULL;
1882 struct got_tree_object *tree;
1883 struct tog_tree_view_state *s;
1884 struct tog_view *tree_view;
1886 err = got_object_open_as_tree(&tree, repo,
1887 got_object_commit_get_tree_id(entry->commit));
1888 if (err)
1889 return err;
1891 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1892 if (tree_view == NULL)
1893 return got_error_from_errno("view_open");
1895 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1896 if (err) {
1897 got_object_tree_close(tree);
1898 return err;
1900 s = &tree_view->state.tree;
1902 *new_view = tree_view;
1904 if (got_path_is_root_dir(path))
1905 return NULL;
1907 return tree_view_walk_path(s, entry->id, path, repo);
1910 static const struct got_error *
1911 block_signals_used_by_main_thread(void)
1913 sigset_t sigset;
1914 int errcode;
1916 if (sigemptyset(&sigset) == -1)
1917 return got_error_from_errno("sigemptyset");
1919 /* tog handles SIGWINCH and SIGCONT */
1920 if (sigaddset(&sigset, SIGWINCH) == -1)
1921 return got_error_from_errno("sigaddset");
1922 if (sigaddset(&sigset, SIGCONT) == -1)
1923 return got_error_from_errno("sigaddset");
1925 /* ncurses handles SIGTSTP */
1926 if (sigaddset(&sigset, SIGTSTP) == -1)
1927 return got_error_from_errno("sigaddset");
1929 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1930 if (errcode)
1931 return got_error_set_errno(errcode, "pthread_sigmask");
1933 return NULL;
1936 static void *
1937 log_thread(void *arg)
1939 const struct got_error *err = NULL;
1940 int errcode = 0;
1941 struct tog_log_thread_args *a = arg;
1942 int done = 0;
1944 err = block_signals_used_by_main_thread();
1945 if (err)
1946 return (void *)err;
1948 while (!done && !err && !tog_sigpipe_received) {
1949 err = queue_commits(a->graph, a->commits, 1, a->repo,
1950 a->in_repo_path, a->searching, a->search_next_done,
1951 a->regex);
1952 if (err) {
1953 if (err->code != GOT_ERR_ITER_COMPLETED)
1954 return (void *)err;
1955 err = NULL;
1956 done = 1;
1957 } else if (a->commits_needed > 0)
1958 a->commits_needed--;
1960 errcode = pthread_mutex_lock(&tog_mutex);
1961 if (errcode) {
1962 err = got_error_set_errno(errcode,
1963 "pthread_mutex_lock");
1964 break;
1965 } else if (*a->quit)
1966 done = 1;
1967 else if (*a->first_displayed_entry == NULL) {
1968 *a->first_displayed_entry =
1969 TAILQ_FIRST(&a->commits->head);
1970 *a->selected_entry = *a->first_displayed_entry;
1973 errcode = pthread_cond_signal(&a->commit_loaded);
1974 if (errcode) {
1975 err = got_error_set_errno(errcode,
1976 "pthread_cond_signal");
1977 pthread_mutex_unlock(&tog_mutex);
1978 break;
1981 if (done)
1982 a->commits_needed = 0;
1983 else {
1984 if (a->commits_needed == 0) {
1985 errcode = pthread_cond_wait(&a->need_commits,
1986 &tog_mutex);
1987 if (errcode)
1988 err = got_error_set_errno(errcode,
1989 "pthread_cond_wait");
1993 errcode = pthread_mutex_unlock(&tog_mutex);
1994 if (errcode && err == NULL)
1995 err = got_error_set_errno(errcode,
1996 "pthread_mutex_unlock");
1998 a->log_complete = 1;
1999 return (void *)err;
2002 static const struct got_error *
2003 stop_log_thread(struct tog_log_view_state *s)
2005 const struct got_error *err = NULL;
2006 int errcode;
2008 if (s->thread) {
2009 s->quit = 1;
2010 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2011 if (errcode)
2012 return got_error_set_errno(errcode,
2013 "pthread_cond_signal");
2014 errcode = pthread_mutex_unlock(&tog_mutex);
2015 if (errcode)
2016 return got_error_set_errno(errcode,
2017 "pthread_mutex_unlock");
2018 errcode = pthread_join(s->thread, (void **)&err);
2019 if (errcode)
2020 return got_error_set_errno(errcode, "pthread_join");
2021 errcode = pthread_mutex_lock(&tog_mutex);
2022 if (errcode)
2023 return got_error_set_errno(errcode,
2024 "pthread_mutex_lock");
2025 s->thread = NULL;
2028 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2029 if (errcode && err == NULL)
2030 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2032 if (s->thread_args.repo) {
2033 got_repo_close(s->thread_args.repo);
2034 s->thread_args.repo = NULL;
2037 if (s->thread_args.graph) {
2038 got_commit_graph_close(s->thread_args.graph);
2039 s->thread_args.graph = NULL;
2042 return err;
2045 static const struct got_error *
2046 close_log_view(struct tog_view *view)
2048 const struct got_error *err = NULL;
2049 struct tog_log_view_state *s = &view->state.log;
2051 err = stop_log_thread(s);
2052 free_commits(&s->commits);
2053 free(s->in_repo_path);
2054 s->in_repo_path = NULL;
2055 free(s->start_id);
2056 s->start_id = NULL;
2057 return err;
2060 static const struct got_error *
2061 search_start_log_view(struct tog_view *view)
2063 struct tog_log_view_state *s = &view->state.log;
2065 s->matched_entry = NULL;
2066 s->search_entry = NULL;
2067 return NULL;
2070 static const struct got_error *
2071 search_next_log_view(struct tog_view *view)
2073 const struct got_error *err = NULL;
2074 struct tog_log_view_state *s = &view->state.log;
2075 struct commit_queue_entry *entry;
2077 /* Display progress update in log view. */
2078 show_log_view(view);
2079 update_panels();
2080 doupdate();
2082 if (s->search_entry) {
2083 int errcode, ch;
2084 errcode = pthread_mutex_unlock(&tog_mutex);
2085 if (errcode)
2086 return got_error_set_errno(errcode,
2087 "pthread_mutex_unlock");
2088 ch = wgetch(view->window);
2089 errcode = pthread_mutex_lock(&tog_mutex);
2090 if (errcode)
2091 return got_error_set_errno(errcode,
2092 "pthread_mutex_lock");
2093 if (ch == KEY_BACKSPACE) {
2094 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2095 return NULL;
2097 if (view->searching == TOG_SEARCH_FORWARD)
2098 entry = TAILQ_NEXT(s->search_entry, entry);
2099 else
2100 entry = TAILQ_PREV(s->search_entry,
2101 commit_queue_head, entry);
2102 } else if (s->matched_entry) {
2103 if (view->searching == TOG_SEARCH_FORWARD)
2104 entry = TAILQ_NEXT(s->matched_entry, entry);
2105 else
2106 entry = TAILQ_PREV(s->matched_entry,
2107 commit_queue_head, entry);
2108 } else {
2109 if (view->searching == TOG_SEARCH_FORWARD)
2110 entry = TAILQ_FIRST(&s->commits.head);
2111 else
2112 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2115 while (1) {
2116 int have_match = 0;
2118 if (entry == NULL) {
2119 if (s->thread_args.log_complete ||
2120 view->searching == TOG_SEARCH_BACKWARD) {
2121 view->search_next_done =
2122 (s->matched_entry == NULL ?
2123 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2124 s->search_entry = NULL;
2125 return NULL;
2128 * Poke the log thread for more commits and return,
2129 * allowing the main loop to make progress. Search
2130 * will resume at s->search_entry once we come back.
2132 s->thread_args.commits_needed++;
2133 return trigger_log_thread(view, 0,
2134 &s->thread_args.commits_needed,
2135 &s->thread_args.log_complete,
2136 &s->thread_args.need_commits,
2137 &s->thread_args.commit_loaded);
2140 err = match_commit(&have_match, entry->id, entry->commit,
2141 &view->regex);
2142 if (err)
2143 break;
2144 if (have_match) {
2145 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2146 s->matched_entry = entry;
2147 break;
2150 s->search_entry = entry;
2151 if (view->searching == TOG_SEARCH_FORWARD)
2152 entry = TAILQ_NEXT(entry, entry);
2153 else
2154 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2157 if (s->matched_entry) {
2158 int cur = s->selected_entry->idx;
2159 while (cur < s->matched_entry->idx) {
2160 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2161 if (err)
2162 return err;
2163 cur++;
2165 while (cur > s->matched_entry->idx) {
2166 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2167 if (err)
2168 return err;
2169 cur--;
2173 s->search_entry = NULL;
2175 return NULL;
2178 static const struct got_error *
2179 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2180 struct got_reflist_head *refs, struct got_repository *repo,
2181 const char *head_ref_name, const char *in_repo_path,
2182 int log_branches)
2184 const struct got_error *err = NULL;
2185 struct tog_log_view_state *s = &view->state.log;
2186 struct got_repository *thread_repo = NULL;
2187 struct got_commit_graph *thread_graph = NULL;
2188 int errcode;
2190 if (in_repo_path != s->in_repo_path) {
2191 free(s->in_repo_path);
2192 s->in_repo_path = strdup(in_repo_path);
2193 if (s->in_repo_path == NULL)
2194 return got_error_from_errno("strdup");
2197 /* The commit queue only contains commits being displayed. */
2198 TAILQ_INIT(&s->commits.head);
2199 s->commits.ncommits = 0;
2201 s->refs = refs;
2202 s->repo = repo;
2203 s->head_ref_name = head_ref_name;
2204 s->start_id = got_object_id_dup(start_id);
2205 if (s->start_id == NULL) {
2206 err = got_error_from_errno("got_object_id_dup");
2207 goto done;
2209 s->log_branches = log_branches;
2211 SIMPLEQ_INIT(&s->colors);
2212 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2213 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2214 get_color_value("TOG_COLOR_COMMIT"));
2215 if (err)
2216 goto done;
2217 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2218 get_color_value("TOG_COLOR_AUTHOR"));
2219 if (err) {
2220 free_colors(&s->colors);
2221 goto done;
2223 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2224 get_color_value("TOG_COLOR_DATE"));
2225 if (err) {
2226 free_colors(&s->colors);
2227 goto done;
2231 view->show = show_log_view;
2232 view->input = input_log_view;
2233 view->close = close_log_view;
2234 view->search_start = search_start_log_view;
2235 view->search_next = search_next_log_view;
2237 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2238 if (err)
2239 goto done;
2240 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2241 !s->log_branches);
2242 if (err)
2243 goto done;
2244 err = got_commit_graph_iter_start(thread_graph,
2245 s->start_id, s->repo, NULL, NULL);
2246 if (err)
2247 goto done;
2249 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2250 if (errcode) {
2251 err = got_error_set_errno(errcode, "pthread_cond_init");
2252 goto done;
2254 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2255 if (errcode) {
2256 err = got_error_set_errno(errcode, "pthread_cond_init");
2257 goto done;
2260 s->thread_args.commits_needed = view->nlines;
2261 s->thread_args.graph = thread_graph;
2262 s->thread_args.commits = &s->commits;
2263 s->thread_args.in_repo_path = s->in_repo_path;
2264 s->thread_args.start_id = s->start_id;
2265 s->thread_args.repo = thread_repo;
2266 s->thread_args.log_complete = 0;
2267 s->thread_args.quit = &s->quit;
2268 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2269 s->thread_args.selected_entry = &s->selected_entry;
2270 s->thread_args.searching = &view->searching;
2271 s->thread_args.search_next_done = &view->search_next_done;
2272 s->thread_args.regex = &view->regex;
2273 done:
2274 if (err)
2275 close_log_view(view);
2276 return err;
2279 static const struct got_error *
2280 show_log_view(struct tog_view *view)
2282 struct tog_log_view_state *s = &view->state.log;
2284 if (s->thread == NULL) {
2285 int errcode = pthread_create(&s->thread, NULL, log_thread,
2286 &s->thread_args);
2287 if (errcode)
2288 return got_error_set_errno(errcode, "pthread_create");
2291 return draw_commits(view, &s->last_displayed_entry,
2292 &s->selected_entry, s->first_displayed_entry,
2293 &s->commits, s->selected, view->nlines, s->refs,
2294 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2297 static const struct got_error *
2298 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2299 struct tog_view **focus_view, struct tog_view *view, int ch)
2301 const struct got_error *err = NULL;
2302 struct tog_log_view_state *s = &view->state.log;
2303 char *parent_path, *in_repo_path = NULL;
2304 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2305 int begin_x = 0;
2306 struct got_object_id *start_id;
2308 switch (ch) {
2309 case 'q':
2310 s->quit = 1;
2311 break;
2312 case 'k':
2313 case KEY_UP:
2314 case '<':
2315 case ',':
2316 if (s->first_displayed_entry == NULL)
2317 break;
2318 if (s->selected > 0)
2319 s->selected--;
2320 else
2321 scroll_up(view, &s->first_displayed_entry, 1,
2322 &s->commits);
2323 break;
2324 case KEY_PPAGE:
2325 case CTRL('b'):
2326 if (s->first_displayed_entry == NULL)
2327 break;
2328 if (TAILQ_FIRST(&s->commits.head) ==
2329 s->first_displayed_entry) {
2330 s->selected = 0;
2331 break;
2333 scroll_up(view, &s->first_displayed_entry,
2334 view->nlines - 1, &s->commits);
2335 break;
2336 case 'j':
2337 case KEY_DOWN:
2338 case '>':
2339 case '.':
2340 if (s->first_displayed_entry == NULL)
2341 break;
2342 if (s->selected < MIN(view->nlines - 2,
2343 s->commits.ncommits - 1)) {
2344 s->selected++;
2345 break;
2347 err = scroll_down(view, &s->first_displayed_entry, 1,
2348 &s->last_displayed_entry, &s->commits,
2349 &s->thread_args.log_complete,
2350 &s->thread_args.commits_needed,
2351 &s->thread_args.need_commits,
2352 &s->thread_args.commit_loaded);
2353 break;
2354 case KEY_NPAGE:
2355 case CTRL('f'): {
2356 struct commit_queue_entry *first;
2357 first = s->first_displayed_entry;
2358 if (first == NULL)
2359 break;
2360 err = scroll_down(view, &s->first_displayed_entry,
2361 view->nlines - 1, &s->last_displayed_entry,
2362 &s->commits, &s->thread_args.log_complete,
2363 &s->thread_args.commits_needed,
2364 &s->thread_args.need_commits,
2365 &s->thread_args.commit_loaded);
2366 if (err)
2367 break;
2368 if (first == s->first_displayed_entry &&
2369 s->selected < MIN(view->nlines - 2,
2370 s->commits.ncommits - 1)) {
2371 /* can't scroll further down */
2372 s->selected = MIN(view->nlines - 2,
2373 s->commits.ncommits - 1);
2375 err = NULL;
2376 break;
2378 case KEY_RESIZE:
2379 if (s->selected > view->nlines - 2)
2380 s->selected = view->nlines - 2;
2381 if (s->selected > s->commits.ncommits - 1)
2382 s->selected = s->commits.ncommits - 1;
2383 break;
2384 case KEY_ENTER:
2385 case ' ':
2386 case '\r':
2387 if (s->selected_entry == NULL)
2388 break;
2389 if (view_is_parent_view(view))
2390 begin_x = view_split_begin_x(view->begin_x);
2391 err = open_diff_view_for_commit(&diff_view, begin_x,
2392 s->selected_entry->commit, s->selected_entry->id,
2393 view, s->refs, s->repo);
2394 if (err)
2395 break;
2396 if (view_is_parent_view(view)) {
2397 err = view_close_child(view);
2398 if (err)
2399 return err;
2400 err = view_set_child(view, diff_view);
2401 if (err) {
2402 view_close(diff_view);
2403 break;
2405 *focus_view = diff_view;
2406 view->child_focussed = 1;
2407 } else
2408 *new_view = diff_view;
2409 break;
2410 case 't':
2411 if (s->selected_entry == NULL)
2412 break;
2413 if (view_is_parent_view(view))
2414 begin_x = view_split_begin_x(view->begin_x);
2415 err = browse_commit_tree(&tree_view, begin_x,
2416 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2417 if (err)
2418 break;
2419 if (view_is_parent_view(view)) {
2420 err = view_close_child(view);
2421 if (err)
2422 return err;
2423 err = view_set_child(view, tree_view);
2424 if (err) {
2425 view_close(tree_view);
2426 break;
2428 *focus_view = tree_view;
2429 view->child_focussed = 1;
2430 } else
2431 *new_view = tree_view;
2432 break;
2433 case KEY_BACKSPACE:
2434 if (strcmp(s->in_repo_path, "/") == 0)
2435 break;
2436 parent_path = dirname(s->in_repo_path);
2437 if (parent_path && strcmp(parent_path, ".") != 0) {
2438 err = stop_log_thread(s);
2439 if (err)
2440 return err;
2441 lv = view_open(view->nlines, view->ncols,
2442 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2443 if (lv == NULL)
2444 return got_error_from_errno(
2445 "view_open");
2446 err = open_log_view(lv, s->start_id, s->refs,
2447 s->repo, s->head_ref_name, parent_path,
2448 s->log_branches);
2449 if (err)
2450 return err;;
2451 if (view_is_parent_view(view))
2452 *new_view = lv;
2453 else {
2454 view_set_child(view->parent, lv);
2455 *focus_view = lv;
2457 return NULL;
2459 break;
2460 case CTRL('l'):
2461 err = stop_log_thread(s);
2462 if (err)
2463 return err;
2464 lv = view_open(view->nlines, view->ncols,
2465 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2466 if (lv == NULL)
2467 return got_error_from_errno("view_open");
2468 err = got_repo_match_object_id(&start_id, NULL,
2469 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2470 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2471 if (err) {
2472 view_close(lv);
2473 return err;
2475 in_repo_path = strdup(s->in_repo_path);
2476 if (in_repo_path == NULL) {
2477 free(start_id);
2478 view_close(lv);
2479 return got_error_from_errno("strdup");
2481 got_ref_list_free(s->refs);
2482 err = got_ref_list(s->refs, s->repo, NULL,
2483 got_ref_cmp_by_name, NULL);
2484 if (err) {
2485 free(start_id);
2486 view_close(lv);
2487 return err;
2489 err = open_log_view(lv, start_id, s->refs, s->repo,
2490 s->head_ref_name, in_repo_path, s->log_branches);
2491 if (err) {
2492 free(start_id);
2493 view_close(lv);
2494 return err;;
2496 *dead_view = view;
2497 *new_view = lv;
2498 break;
2499 case 'B':
2500 s->log_branches = !s->log_branches;
2501 err = stop_log_thread(s);
2502 if (err)
2503 return err;
2504 lv = view_open(view->nlines, view->ncols,
2505 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2506 if (lv == NULL)
2507 return got_error_from_errno("view_open");
2508 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2509 s->head_ref_name, s->in_repo_path, s->log_branches);
2510 if (err) {
2511 view_close(lv);
2512 return err;;
2514 *dead_view = view;
2515 *new_view = lv;
2516 break;
2517 default:
2518 break;
2521 return err;
2524 static const struct got_error *
2525 apply_unveil(const char *repo_path, const char *worktree_path)
2527 const struct got_error *error;
2529 #ifdef PROFILE
2530 if (unveil("gmon.out", "rwc") != 0)
2531 return got_error_from_errno2("unveil", "gmon.out");
2532 #endif
2533 if (repo_path && unveil(repo_path, "r") != 0)
2534 return got_error_from_errno2("unveil", repo_path);
2536 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2537 return got_error_from_errno2("unveil", worktree_path);
2539 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2540 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2542 error = got_privsep_unveil_exec_helpers();
2543 if (error != NULL)
2544 return error;
2546 if (unveil(NULL, NULL) != 0)
2547 return got_error_from_errno("unveil");
2549 return NULL;
2552 static void
2553 init_curses(void)
2555 initscr();
2556 cbreak();
2557 halfdelay(1); /* Do fast refresh while initial view is loading. */
2558 noecho();
2559 nonl();
2560 intrflush(stdscr, FALSE);
2561 keypad(stdscr, TRUE);
2562 curs_set(0);
2563 if (getenv("TOG_COLORS") != NULL) {
2564 start_color();
2565 use_default_colors();
2567 signal(SIGWINCH, tog_sigwinch);
2568 signal(SIGPIPE, tog_sigpipe);
2569 signal(SIGCONT, tog_sigcont);
2572 static const struct got_error *
2573 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2574 struct got_repository *repo, struct got_worktree *worktree)
2576 const struct got_error *err = NULL;
2578 if (argc == 0) {
2579 *in_repo_path = strdup("/");
2580 if (*in_repo_path == NULL)
2581 return got_error_from_errno("strdup");
2582 return NULL;
2585 if (worktree) {
2586 const char *prefix = got_worktree_get_path_prefix(worktree);
2587 char *wt_path, *p;
2589 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2590 if (err)
2591 return err;
2593 if (asprintf(&p, "%s%s%s", prefix,
2594 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2595 err = got_error_from_errno("asprintf");
2596 free(wt_path);
2597 return err;
2599 err = got_repo_map_path(in_repo_path, repo, p, 0);
2600 free(p);
2601 free(wt_path);
2602 } else
2603 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2605 return err;
2608 static const struct got_error *
2609 cmd_log(int argc, char *argv[])
2611 const struct got_error *error;
2612 struct got_repository *repo = NULL;
2613 struct got_worktree *worktree = NULL;
2614 struct got_reflist_head refs;
2615 struct got_object_id *start_id = NULL;
2616 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2617 char *start_commit = NULL, *head_ref_name = NULL;
2618 int ch, log_branches = 0;
2619 struct tog_view *view;
2621 SIMPLEQ_INIT(&refs);
2623 #ifndef PROFILE
2624 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2625 NULL) == -1)
2626 err(1, "pledge");
2627 #endif
2629 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2630 switch (ch) {
2631 case 'b':
2632 log_branches = 1;
2633 break;
2634 case 'c':
2635 start_commit = optarg;
2636 break;
2637 case 'r':
2638 repo_path = realpath(optarg, NULL);
2639 if (repo_path == NULL)
2640 return got_error_from_errno2("realpath",
2641 optarg);
2642 break;
2643 default:
2644 usage_log();
2645 /* NOTREACHED */
2649 argc -= optind;
2650 argv += optind;
2652 if (argc > 1)
2653 usage_log();
2655 cwd = getcwd(NULL, 0);
2656 if (cwd == NULL)
2657 return got_error_from_errno("getcwd");
2659 error = got_worktree_open(&worktree, cwd);
2660 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2661 goto done;
2663 if (repo_path == NULL) {
2664 if (worktree)
2665 repo_path =
2666 strdup(got_worktree_get_repo_path(worktree));
2667 else
2668 repo_path = strdup(cwd);
2670 if (repo_path == NULL) {
2671 error = got_error_from_errno("strdup");
2672 goto done;
2675 error = got_repo_open(&repo, repo_path, NULL);
2676 if (error != NULL)
2677 goto done;
2679 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2680 repo, worktree);
2681 if (error)
2682 goto done;
2684 init_curses();
2686 error = apply_unveil(got_repo_get_path(repo),
2687 worktree ? got_worktree_get_root_path(worktree) : NULL);
2688 if (error)
2689 goto done;
2691 if (start_commit == NULL)
2692 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2693 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2694 GOT_OBJ_TYPE_COMMIT, 1, repo);
2695 else
2696 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2697 GOT_OBJ_TYPE_COMMIT, 1, repo);
2698 if (error != NULL)
2699 goto done;
2701 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2702 if (error)
2703 goto done;
2705 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2706 if (view == NULL) {
2707 error = got_error_from_errno("view_open");
2708 goto done;
2710 if (worktree) {
2711 head_ref_name = strdup(
2712 got_worktree_get_head_ref_name(worktree));
2713 if (head_ref_name == NULL) {
2714 error = got_error_from_errno("strdup");
2715 goto done;
2718 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2719 in_repo_path, log_branches);
2720 if (error)
2721 goto done;
2722 if (worktree) {
2723 /* Release work tree lock. */
2724 got_worktree_close(worktree);
2725 worktree = NULL;
2727 error = view_loop(view);
2728 done:
2729 free(in_repo_path);
2730 free(repo_path);
2731 free(cwd);
2732 free(start_id);
2733 free(head_ref_name);
2734 if (repo)
2735 got_repo_close(repo);
2736 if (worktree)
2737 got_worktree_close(worktree);
2738 got_ref_list_free(&refs);
2739 return error;
2742 __dead static void
2743 usage_diff(void)
2745 endwin();
2746 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2747 getprogname());
2748 exit(1);
2751 static char *
2752 parse_next_line(FILE *f, size_t *len)
2754 char *line;
2755 size_t linelen;
2756 size_t lineno;
2757 const char delim[3] = { '\0', '\0', '\0'};
2759 line = fparseln(f, &linelen, &lineno, delim, 0);
2760 if (len)
2761 *len = linelen;
2762 return line;
2765 static int
2766 match_line(const char *line, regex_t *regex)
2768 regmatch_t regmatch;
2770 return regexec(regex, line, 1, &regmatch, 0) == 0;
2773 struct tog_color *
2774 match_color(struct tog_colors *colors, const char *line)
2776 struct tog_color *tc = NULL;
2778 SIMPLEQ_FOREACH(tc, colors, entry) {
2779 if (match_line(line, &tc->regex))
2780 return tc;
2783 return NULL;
2786 static const struct got_error *
2787 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2788 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2789 char *header, struct tog_colors *colors)
2791 const struct got_error *err;
2792 int lineno = 0, nprinted = 0;
2793 char *line;
2794 struct tog_color *tc;
2795 size_t len;
2796 wchar_t *wline;
2797 int width;
2799 rewind(f);
2800 werase(view->window);
2802 if (header) {
2803 err = format_line(&wline, &width, header, view->ncols, 0);
2804 if (err) {
2805 return err;
2808 if (view_needs_focus_indication(view))
2809 wstandout(view->window);
2810 waddwstr(view->window, wline);
2811 if (view_needs_focus_indication(view))
2812 wstandend(view->window);
2813 if (width <= view->ncols - 1)
2814 waddch(view->window, '\n');
2816 if (max_lines <= 1)
2817 return NULL;
2818 max_lines--;
2821 *eof = 0;
2822 while (nprinted < max_lines) {
2823 line = parse_next_line(f, &len);
2824 if (line == NULL) {
2825 *eof = 1;
2826 break;
2828 if (++lineno < *first_displayed_line) {
2829 free(line);
2830 continue;
2833 err = format_line(&wline, &width, line, view->ncols, 0);
2834 if (err) {
2835 free(line);
2836 return err;
2839 tc = match_color(colors, line);
2840 if (tc)
2841 wattr_on(view->window,
2842 COLOR_PAIR(tc->colorpair), NULL);
2843 waddwstr(view->window, wline);
2844 if (tc)
2845 wattr_off(view->window,
2846 COLOR_PAIR(tc->colorpair), NULL);
2847 if (width <= view->ncols - 1)
2848 waddch(view->window, '\n');
2849 if (++nprinted == 1)
2850 *first_displayed_line = lineno;
2851 free(line);
2852 free(wline);
2853 wline = NULL;
2855 *last_displayed_line = lineno;
2857 view_vborder(view);
2859 if (*eof) {
2860 while (nprinted < view->nlines) {
2861 waddch(view->window, '\n');
2862 nprinted++;
2865 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2866 if (err) {
2867 return err;
2870 wstandout(view->window);
2871 waddwstr(view->window, wline);
2872 wstandend(view->window);
2875 return NULL;
2878 static char *
2879 get_datestr(time_t *time, char *datebuf)
2881 struct tm mytm, *tm;
2882 char *p, *s;
2884 tm = gmtime_r(time, &mytm);
2885 if (tm == NULL)
2886 return NULL;
2887 s = asctime_r(tm, datebuf);
2888 if (s == NULL)
2889 return NULL;
2890 p = strchr(s, '\n');
2891 if (p)
2892 *p = '\0';
2893 return s;
2896 static const struct got_error *
2897 write_commit_info(struct got_object_id *commit_id,
2898 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2900 const struct got_error *err = NULL;
2901 char datebuf[26], *datestr;
2902 struct got_commit_object *commit;
2903 char *id_str = NULL, *logmsg = NULL;
2904 time_t committer_time;
2905 const char *author, *committer;
2906 char *refs_str = NULL;
2908 if (refs) {
2909 err = build_refs_str(&refs_str, refs, commit_id, repo);
2910 if (err)
2911 return err;
2914 err = got_object_open_as_commit(&commit, repo, commit_id);
2915 if (err)
2916 return err;
2918 err = got_object_id_str(&id_str, commit_id);
2919 if (err) {
2920 err = got_error_from_errno("got_object_id_str");
2921 goto done;
2924 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2925 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2926 err = got_error_from_errno("fprintf");
2927 goto done;
2929 if (fprintf(outfile, "from: %s\n",
2930 got_object_commit_get_author(commit)) < 0) {
2931 err = got_error_from_errno("fprintf");
2932 goto done;
2934 committer_time = got_object_commit_get_committer_time(commit);
2935 datestr = get_datestr(&committer_time, datebuf);
2936 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2937 err = got_error_from_errno("fprintf");
2938 goto done;
2940 author = got_object_commit_get_author(commit);
2941 committer = got_object_commit_get_committer(commit);
2942 if (strcmp(author, committer) != 0 &&
2943 fprintf(outfile, "via: %s\n", committer) < 0) {
2944 err = got_error_from_errno("fprintf");
2945 goto done;
2947 err = got_object_commit_get_logmsg(&logmsg, commit);
2948 if (err)
2949 goto done;
2950 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2951 err = got_error_from_errno("fprintf");
2952 goto done;
2954 done:
2955 free(id_str);
2956 free(logmsg);
2957 free(refs_str);
2958 got_object_commit_close(commit);
2959 return err;
2962 const struct got_error *
2963 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2964 FILE *infile)
2966 size_t len;
2967 char *buf = NULL;
2968 int i;
2969 size_t noffsets = 0;
2970 off_t off = 0;
2972 if (line_offsets)
2973 *line_offsets = NULL;
2974 if (filesize)
2975 *filesize = 0;
2976 if (nlines)
2977 *nlines = 0;
2979 if (fseek(infile, 0, SEEK_END) == -1)
2980 return got_error_from_errno("fseek");
2981 len = ftell(infile) + 1;
2982 if (ferror(infile))
2983 return got_error_from_errno("ftell");
2984 if (fseek(infile, 0, SEEK_SET) == -1)
2985 return got_error_from_errno("fseek");
2987 if (len == 0)
2988 return NULL;
2989 if ((buf = calloc(len, sizeof(char *))) == NULL)
2990 return got_error_from_errno("calloc");
2992 fread(buf, 1, len, infile);
2993 if (ferror(infile))
2994 return got_error_from_errno("fread");
2996 i = 0;
2997 if (line_offsets && nlines) {
2998 if (*line_offsets == NULL) {
2999 /* Have some data but perhaps no '\n'. */
3000 noffsets = 1;
3001 *nlines = 1;
3002 *line_offsets = calloc(1, sizeof(**line_offsets));
3003 if (*line_offsets == NULL)
3004 return got_error_from_errno("calloc");
3005 /* Skip forward over end of first line. */
3006 while (i < len) {
3007 if (buf[i] == '\n')
3008 break;
3009 i++;
3012 /* Scan '\n' offsets in remaining chunk of data. */
3013 while (i < len) {
3014 if (buf[i] != '\n') {
3015 i++;
3016 continue;
3018 (*nlines)++;
3019 if (noffsets < *nlines) {
3020 off_t *o = recallocarray(*line_offsets,
3021 noffsets, *nlines,
3022 sizeof(**line_offsets));
3023 if (o == NULL) {
3024 free(*line_offsets);
3025 *line_offsets = NULL;
3026 return got_error_from_errno(
3027 "recallocarray");
3029 *line_offsets = o;
3030 noffsets = *nlines;
3032 off = i + 1;
3033 (*line_offsets)[*nlines - 1] = off;
3034 i++;
3038 if (fflush(infile) != 0)
3039 return got_error_from_errno("fflush");
3040 rewind(infile);
3042 if (filesize)
3043 *filesize = len;
3045 return NULL;
3048 static const struct got_error *
3049 create_diff(struct tog_diff_view_state *s)
3051 const struct got_error *err = NULL;
3052 FILE *f = NULL;
3053 int obj_type;
3055 f = got_opentemp();
3056 if (f == NULL) {
3057 err = got_error_from_errno("got_opentemp");
3058 goto done;
3060 if (s->f && fclose(s->f) != 0) {
3061 err = got_error_from_errno("fclose");
3062 goto done;
3064 s->f = f;
3066 if (s->id1)
3067 err = got_object_get_type(&obj_type, s->repo, s->id1);
3068 else
3069 err = got_object_get_type(&obj_type, s->repo, s->id2);
3070 if (err)
3071 goto done;
3073 switch (obj_type) {
3074 case GOT_OBJ_TYPE_BLOB:
3075 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3076 s->diff_context, 0, s->repo, s->f);
3077 break;
3078 case GOT_OBJ_TYPE_TREE:
3079 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3080 s->diff_context, 0, s->repo, s->f);
3081 break;
3082 case GOT_OBJ_TYPE_COMMIT: {
3083 const struct got_object_id_queue *parent_ids;
3084 struct got_object_qid *pid;
3085 struct got_commit_object *commit2;
3087 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3088 if (err)
3089 goto done;
3090 /* Show commit info if we're diffing to a parent/root commit. */
3091 if (s->id1 == NULL) {
3092 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3093 if (err)
3094 goto done;
3095 } else {
3096 parent_ids = got_object_commit_get_parent_ids(commit2);
3097 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3098 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3099 err = write_commit_info(s->id2, s->refs,
3100 s->repo, s->f);
3101 if (err)
3102 goto done;
3103 break;
3107 got_object_commit_close(commit2);
3109 err = got_diff_objects_as_commits(s->id1, s->id2,
3110 s->diff_context, 0, s->repo, s->f);
3111 break;
3113 default:
3114 err = got_error(GOT_ERR_OBJ_TYPE);
3115 break;
3117 if (err)
3118 goto done;
3119 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3120 s->f);
3121 done:
3122 if (s->f && fflush(s->f) != 0 && err == NULL)
3123 err = got_error_from_errno("fflush");
3124 return err;
3127 static void
3128 diff_view_indicate_progress(struct tog_view *view)
3130 mvwaddstr(view->window, 0, 0, "diffing...");
3131 update_panels();
3132 doupdate();
3135 static const struct got_error *
3136 search_start_diff_view(struct tog_view *view)
3138 struct tog_diff_view_state *s = &view->state.diff;
3140 s->matched_line = 0;
3141 return NULL;
3144 static const struct got_error *
3145 search_next_diff_view(struct tog_view *view)
3147 struct tog_diff_view_state *s = &view->state.diff;
3148 int lineno;
3150 if (!view->searching) {
3151 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3152 return NULL;
3155 if (s->matched_line) {
3156 if (view->searching == TOG_SEARCH_FORWARD)
3157 lineno = s->matched_line + 1;
3158 else
3159 lineno = s->matched_line - 1;
3160 } else {
3161 if (view->searching == TOG_SEARCH_FORWARD)
3162 lineno = 1;
3163 else
3164 lineno = s->nlines;
3167 while (1) {
3168 char *line = NULL;
3169 off_t offset;
3170 size_t len;
3172 if (lineno <= 0 || lineno > s->nlines) {
3173 if (s->matched_line == 0) {
3174 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3175 free(line);
3176 break;
3179 if (view->searching == TOG_SEARCH_FORWARD)
3180 lineno = 1;
3181 else
3182 lineno = s->nlines;
3185 offset = s->line_offsets[lineno - 1];
3186 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3187 free(line);
3188 return got_error_from_errno("fseeko");
3190 free(line);
3191 line = parse_next_line(s->f, &len);
3192 if (line && match_line(line, &view->regex)) {
3193 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3194 s->matched_line = lineno;
3195 free(line);
3196 break;
3198 free(line);
3199 if (view->searching == TOG_SEARCH_FORWARD)
3200 lineno++;
3201 else
3202 lineno--;
3205 if (s->matched_line) {
3206 s->first_displayed_line = s->matched_line;
3207 s->selected_line = 1;
3210 return NULL;
3213 static const struct got_error *
3214 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3215 struct got_object_id *id2, struct tog_view *log_view,
3216 struct got_reflist_head *refs, struct got_repository *repo)
3218 const struct got_error *err;
3219 struct tog_diff_view_state *s = &view->state.diff;
3221 if (id1 != NULL && id2 != NULL) {
3222 int type1, type2;
3223 err = got_object_get_type(&type1, repo, id1);
3224 if (err)
3225 return err;
3226 err = got_object_get_type(&type2, repo, id2);
3227 if (err)
3228 return err;
3230 if (type1 != type2)
3231 return got_error(GOT_ERR_OBJ_TYPE);
3233 s->first_displayed_line = 1;
3234 s->last_displayed_line = view->nlines;
3235 s->selected_line = 1;
3236 s->repo = repo;
3237 s->refs = refs;
3238 s->id1 = id1;
3239 s->id2 = id2;
3241 if (id1) {
3242 s->id1 = got_object_id_dup(id1);
3243 if (s->id1 == NULL)
3244 return got_error_from_errno("got_object_id_dup");
3245 } else
3246 s->id1 = NULL;
3248 s->id2 = got_object_id_dup(id2);
3249 if (s->id2 == NULL) {
3250 free(s->id1);
3251 s->id1 = NULL;
3252 return got_error_from_errno("got_object_id_dup");
3254 s->f = NULL;
3255 s->first_displayed_line = 1;
3256 s->last_displayed_line = view->nlines;
3257 s->diff_context = 3;
3258 s->log_view = log_view;
3259 s->repo = repo;
3260 s->refs = refs;
3262 SIMPLEQ_INIT(&s->colors);
3263 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3264 err = add_color(&s->colors,
3265 "^-", TOG_COLOR_DIFF_MINUS,
3266 get_color_value("TOG_COLOR_DIFF_MINUS"));
3267 if (err)
3268 return err;
3269 err = add_color(&s->colors, "^\\+",
3270 TOG_COLOR_DIFF_PLUS,
3271 get_color_value("TOG_COLOR_DIFF_PLUS"));
3272 if (err) {
3273 free_colors(&s->colors);
3274 return err;
3276 err = add_color(&s->colors,
3277 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3278 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3279 if (err) {
3280 free_colors(&s->colors);
3281 return err;
3284 err = add_color(&s->colors,
3285 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3286 get_color_value("TOG_COLOR_DIFF_META"));
3287 if (err) {
3288 free_colors(&s->colors);
3289 return err;
3292 err = add_color(&s->colors,
3293 "^(from|via): ", TOG_COLOR_AUTHOR,
3294 get_color_value("TOG_COLOR_AUTHOR"));
3295 if (err) {
3296 free_colors(&s->colors);
3297 return err;
3300 err = add_color(&s->colors,
3301 "^date: ", TOG_COLOR_DATE,
3302 get_color_value("TOG_COLOR_DATE"));
3303 if (err) {
3304 free_colors(&s->colors);
3305 return err;
3309 if (log_view && view_is_splitscreen(view))
3310 show_log_view(log_view); /* draw vborder */
3311 diff_view_indicate_progress(view);
3313 err = create_diff(s);
3314 if (err) {
3315 free(s->id1);
3316 s->id1 = NULL;
3317 free(s->id2);
3318 s->id2 = NULL;
3319 return err;
3322 view->show = show_diff_view;
3323 view->input = input_diff_view;
3324 view->close = close_diff_view;
3325 view->search_start = search_start_diff_view;
3326 view->search_next = search_next_diff_view;
3328 return NULL;
3331 static const struct got_error *
3332 close_diff_view(struct tog_view *view)
3334 const struct got_error *err = NULL;
3335 struct tog_diff_view_state *s = &view->state.diff;
3337 free(s->id1);
3338 s->id1 = NULL;
3339 free(s->id2);
3340 s->id2 = NULL;
3341 if (s->f && fclose(s->f) == EOF)
3342 err = got_error_from_errno("fclose");
3343 free_colors(&s->colors);
3344 free(s->line_offsets);
3345 return err;
3348 static const struct got_error *
3349 show_diff_view(struct tog_view *view)
3351 const struct got_error *err;
3352 struct tog_diff_view_state *s = &view->state.diff;
3353 char *id_str1 = NULL, *id_str2, *header;
3355 if (s->id1) {
3356 err = got_object_id_str(&id_str1, s->id1);
3357 if (err)
3358 return err;
3360 err = got_object_id_str(&id_str2, s->id2);
3361 if (err)
3362 return err;
3364 if (asprintf(&header, "diff %s %s",
3365 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3366 err = got_error_from_errno("asprintf");
3367 free(id_str1);
3368 free(id_str2);
3369 return err;
3371 free(id_str1);
3372 free(id_str2);
3374 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3375 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3376 header, &s->colors);
3379 static const struct got_error *
3380 set_selected_commit(struct tog_diff_view_state *s,
3381 struct commit_queue_entry *entry)
3383 const struct got_error *err;
3384 const struct got_object_id_queue *parent_ids;
3385 struct got_commit_object *selected_commit;
3386 struct got_object_qid *pid;
3388 free(s->id2);
3389 s->id2 = got_object_id_dup(entry->id);
3390 if (s->id2 == NULL)
3391 return got_error_from_errno("got_object_id_dup");
3393 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3394 if (err)
3395 return err;
3396 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3397 free(s->id1);
3398 pid = SIMPLEQ_FIRST(parent_ids);
3399 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3400 got_object_commit_close(selected_commit);
3401 return NULL;
3404 static const struct got_error *
3405 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3406 struct tog_view **focus_view, struct tog_view *view, int ch)
3408 const struct got_error *err = NULL;
3409 struct tog_diff_view_state *s = &view->state.diff;
3410 struct tog_log_view_state *ls;
3411 struct commit_queue_entry *entry;
3412 int i;
3414 switch (ch) {
3415 case 'k':
3416 case KEY_UP:
3417 if (s->first_displayed_line > 1)
3418 s->first_displayed_line--;
3419 break;
3420 case KEY_PPAGE:
3421 case CTRL('b'):
3422 if (s->first_displayed_line == 1)
3423 break;
3424 i = 0;
3425 while (i++ < view->nlines - 1 &&
3426 s->first_displayed_line > 1)
3427 s->first_displayed_line--;
3428 break;
3429 case 'j':
3430 case KEY_DOWN:
3431 if (!s->eof)
3432 s->first_displayed_line++;
3433 break;
3434 case KEY_NPAGE:
3435 case CTRL('f'):
3436 case ' ':
3437 if (s->eof)
3438 break;
3439 i = 0;
3440 while (!s->eof && i++ < view->nlines - 1) {
3441 char *line;
3442 line = parse_next_line(s->f, NULL);
3443 s->first_displayed_line++;
3444 if (line == NULL)
3445 break;
3447 break;
3448 case '[':
3449 if (s->diff_context > 0) {
3450 s->diff_context--;
3451 diff_view_indicate_progress(view);
3452 err = create_diff(s);
3454 break;
3455 case ']':
3456 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3457 s->diff_context++;
3458 diff_view_indicate_progress(view);
3459 err = create_diff(s);
3461 break;
3462 case '<':
3463 case ',':
3464 if (s->log_view == NULL)
3465 break;
3466 ls = &s->log_view->state.log;
3467 entry = TAILQ_PREV(ls->selected_entry,
3468 commit_queue_head, entry);
3469 if (entry == NULL)
3470 break;
3472 err = input_log_view(NULL, NULL, NULL, s->log_view,
3473 KEY_UP);
3474 if (err)
3475 break;
3477 err = set_selected_commit(s, entry);
3478 if (err)
3479 break;
3481 s->first_displayed_line = 1;
3482 s->last_displayed_line = view->nlines;
3484 diff_view_indicate_progress(view);
3485 err = create_diff(s);
3486 break;
3487 case '>':
3488 case '.':
3489 if (s->log_view == NULL)
3490 break;
3491 ls = &s->log_view->state.log;
3493 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3494 ls->thread_args.commits_needed++;
3495 err = trigger_log_thread(s->log_view, 1,
3496 &ls->thread_args.commits_needed,
3497 &ls->thread_args.log_complete,
3498 &ls->thread_args.need_commits,
3499 &ls->thread_args.commit_loaded);
3500 if (err)
3501 break;
3503 err = input_log_view(NULL, NULL, NULL, s->log_view,
3504 KEY_DOWN);
3505 if (err)
3506 break;
3508 entry = TAILQ_NEXT(ls->selected_entry, entry);
3509 if (entry == NULL)
3510 break;
3512 err = set_selected_commit(s, entry);
3513 if (err)
3514 break;
3516 s->first_displayed_line = 1;
3517 s->last_displayed_line = view->nlines;
3519 diff_view_indicate_progress(view);
3520 err = create_diff(s);
3521 break;
3522 default:
3523 break;
3526 return err;
3529 static const struct got_error *
3530 cmd_diff(int argc, char *argv[])
3532 const struct got_error *error = NULL;
3533 struct got_repository *repo = NULL;
3534 struct got_worktree *worktree = NULL;
3535 struct got_reflist_head refs;
3536 struct got_object_id *id1 = NULL, *id2 = NULL;
3537 char *repo_path = NULL, *cwd = NULL;
3538 char *id_str1 = NULL, *id_str2 = NULL;
3539 int ch;
3540 struct tog_view *view;
3542 SIMPLEQ_INIT(&refs);
3544 #ifndef PROFILE
3545 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3546 NULL) == -1)
3547 err(1, "pledge");
3548 #endif
3550 while ((ch = getopt(argc, argv, "r:")) != -1) {
3551 switch (ch) {
3552 case 'r':
3553 repo_path = realpath(optarg, NULL);
3554 if (repo_path == NULL)
3555 return got_error_from_errno2("realpath",
3556 optarg);
3557 break;
3558 default:
3559 usage_diff();
3560 /* NOTREACHED */
3564 argc -= optind;
3565 argv += optind;
3567 if (argc == 0) {
3568 usage_diff(); /* TODO show local worktree changes */
3569 } else if (argc == 2) {
3570 id_str1 = argv[0];
3571 id_str2 = argv[1];
3572 } else
3573 usage_diff();
3575 cwd = getcwd(NULL, 0);
3576 if (cwd == NULL)
3577 return got_error_from_errno("getcwd");
3579 error = got_worktree_open(&worktree, cwd);
3580 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3581 goto done;
3583 if (repo_path == NULL) {
3584 if (worktree)
3585 repo_path =
3586 strdup(got_worktree_get_repo_path(worktree));
3587 else
3588 repo_path = strdup(cwd);
3590 if (repo_path == NULL) {
3591 error = got_error_from_errno("strdup");
3592 goto done;
3595 error = got_repo_open(&repo, repo_path, NULL);
3596 if (error)
3597 goto done;
3599 init_curses();
3601 error = apply_unveil(got_repo_get_path(repo), NULL);
3602 if (error)
3603 goto done;
3605 error = got_repo_match_object_id_prefix(&id1, id_str1,
3606 GOT_OBJ_TYPE_ANY, repo);
3607 if (error)
3608 goto done;
3610 error = got_repo_match_object_id_prefix(&id2, id_str2,
3611 GOT_OBJ_TYPE_ANY, repo);
3612 if (error)
3613 goto done;
3615 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3616 if (error)
3617 goto done;
3619 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3620 if (view == NULL) {
3621 error = got_error_from_errno("view_open");
3622 goto done;
3624 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3625 if (error)
3626 goto done;
3627 error = view_loop(view);
3628 done:
3629 free(repo_path);
3630 free(cwd);
3631 if (repo)
3632 got_repo_close(repo);
3633 if (worktree)
3634 got_worktree_close(worktree);
3635 got_ref_list_free(&refs);
3636 return error;
3639 __dead static void
3640 usage_blame(void)
3642 endwin();
3643 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3644 getprogname());
3645 exit(1);
3648 struct tog_blame_line {
3649 int annotated;
3650 struct got_object_id *id;
3653 static const struct got_error *
3654 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3655 const char *path, struct tog_blame_line *lines, int nlines,
3656 int blame_complete, int selected_line, int *first_displayed_line,
3657 int *last_displayed_line, int *eof, int max_lines,
3658 struct tog_colors *colors)
3660 const struct got_error *err;
3661 int lineno = 0, nprinted = 0;
3662 char *line;
3663 size_t len;
3664 wchar_t *wline;
3665 int width;
3666 struct tog_blame_line *blame_line;
3667 struct got_object_id *prev_id = NULL;
3668 char *id_str;
3669 struct tog_color *tc;
3671 err = got_object_id_str(&id_str, id);
3672 if (err)
3673 return err;
3675 rewind(f);
3676 werase(view->window);
3678 if (asprintf(&line, "commit %s", id_str) == -1) {
3679 err = got_error_from_errno("asprintf");
3680 free(id_str);
3681 return err;
3684 err = format_line(&wline, &width, line, view->ncols, 0);
3685 free(line);
3686 line = NULL;
3687 if (err)
3688 return err;
3689 if (view_needs_focus_indication(view))
3690 wstandout(view->window);
3691 tc = get_color(colors, TOG_COLOR_COMMIT);
3692 if (tc)
3693 wattr_on(view->window,
3694 COLOR_PAIR(tc->colorpair), NULL);
3695 waddwstr(view->window, wline);
3696 if (tc)
3697 wattr_off(view->window,
3698 COLOR_PAIR(tc->colorpair), NULL);
3699 if (view_needs_focus_indication(view))
3700 wstandend(view->window);
3701 free(wline);
3702 wline = NULL;
3703 if (width < view->ncols - 1)
3704 waddch(view->window, '\n');
3706 if (asprintf(&line, "[%d/%d] %s%s",
3707 *first_displayed_line - 1 + selected_line, nlines,
3708 blame_complete ? "" : "annotating... ", path) == -1) {
3709 free(id_str);
3710 return got_error_from_errno("asprintf");
3712 free(id_str);
3713 err = format_line(&wline, &width, line, view->ncols, 0);
3714 free(line);
3715 line = NULL;
3716 if (err)
3717 return err;
3718 waddwstr(view->window, wline);
3719 free(wline);
3720 wline = NULL;
3721 if (width < view->ncols - 1)
3722 waddch(view->window, '\n');
3724 *eof = 0;
3725 while (nprinted < max_lines - 2) {
3726 line = parse_next_line(f, &len);
3727 if (line == NULL) {
3728 *eof = 1;
3729 break;
3731 if (++lineno < *first_displayed_line) {
3732 free(line);
3733 continue;
3736 if (view->ncols <= 9) {
3737 width = 9;
3738 wline = wcsdup(L"");
3739 if (wline == NULL)
3740 err = got_error_from_errno("wcsdup");
3741 } else {
3742 err = format_line(&wline, &width, line,
3743 view->ncols - 9, 9);
3744 width += 9;
3746 if (err) {
3747 free(line);
3748 return err;
3751 if (view->focussed && nprinted == selected_line - 1)
3752 wstandout(view->window);
3754 if (nlines > 0) {
3755 blame_line = &lines[lineno - 1];
3756 if (blame_line->annotated && prev_id &&
3757 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3758 !(view->focussed &&
3759 nprinted == selected_line - 1)) {
3760 waddstr(view->window, " ");
3761 } else if (blame_line->annotated) {
3762 char *id_str;
3763 err = got_object_id_str(&id_str, blame_line->id);
3764 if (err) {
3765 free(line);
3766 free(wline);
3767 return err;
3769 tc = get_color(colors, TOG_COLOR_COMMIT);
3770 if (tc)
3771 wattr_on(view->window,
3772 COLOR_PAIR(tc->colorpair), NULL);
3773 wprintw(view->window, "%.8s", id_str);
3774 if (tc)
3775 wattr_off(view->window,
3776 COLOR_PAIR(tc->colorpair), NULL);
3777 free(id_str);
3778 prev_id = blame_line->id;
3779 } else {
3780 waddstr(view->window, "........");
3781 prev_id = NULL;
3783 } else {
3784 waddstr(view->window, "........");
3785 prev_id = NULL;
3788 if (view->focussed && nprinted == selected_line - 1)
3789 wstandend(view->window);
3790 waddstr(view->window, " ");
3792 waddwstr(view->window, wline);
3793 if (width <= view->ncols - 1)
3794 waddch(view->window, '\n');
3795 if (++nprinted == 1)
3796 *first_displayed_line = lineno;
3797 free(line);
3798 free(wline);
3799 wline = NULL;
3801 *last_displayed_line = lineno;
3803 view_vborder(view);
3805 return NULL;
3808 static const struct got_error *
3809 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3811 const struct got_error *err = NULL;
3812 struct tog_blame_cb_args *a = arg;
3813 struct tog_blame_line *line;
3814 int errcode;
3816 if (nlines != a->nlines ||
3817 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3818 return got_error(GOT_ERR_RANGE);
3820 errcode = pthread_mutex_lock(&tog_mutex);
3821 if (errcode)
3822 return got_error_set_errno(errcode, "pthread_mutex_lock");
3824 if (*a->quit) { /* user has quit the blame view */
3825 err = got_error(GOT_ERR_ITER_COMPLETED);
3826 goto done;
3829 if (lineno == -1)
3830 goto done; /* no change in this commit */
3832 line = &a->lines[lineno - 1];
3833 if (line->annotated)
3834 goto done;
3836 line->id = got_object_id_dup(id);
3837 if (line->id == NULL) {
3838 err = got_error_from_errno("got_object_id_dup");
3839 goto done;
3841 line->annotated = 1;
3842 done:
3843 errcode = pthread_mutex_unlock(&tog_mutex);
3844 if (errcode)
3845 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3846 return err;
3849 static void *
3850 blame_thread(void *arg)
3852 const struct got_error *err;
3853 struct tog_blame_thread_args *ta = arg;
3854 struct tog_blame_cb_args *a = ta->cb_args;
3855 int errcode;
3857 err = block_signals_used_by_main_thread();
3858 if (err)
3859 return (void *)err;
3861 err = got_blame(ta->path, a->commit_id, ta->repo,
3862 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3863 if (err && err->code == GOT_ERR_CANCELLED)
3864 err = NULL;
3866 errcode = pthread_mutex_lock(&tog_mutex);
3867 if (errcode)
3868 return (void *)got_error_set_errno(errcode,
3869 "pthread_mutex_lock");
3871 got_repo_close(ta->repo);
3872 ta->repo = NULL;
3873 *ta->complete = 1;
3875 errcode = pthread_mutex_unlock(&tog_mutex);
3876 if (errcode && err == NULL)
3877 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3879 return (void *)err;
3882 static struct got_object_id *
3883 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3884 int first_displayed_line, int selected_line)
3886 struct tog_blame_line *line;
3888 if (nlines <= 0)
3889 return NULL;
3891 line = &lines[first_displayed_line - 1 + selected_line - 1];
3892 if (!line->annotated)
3893 return NULL;
3895 return line->id;
3898 static const struct got_error *
3899 stop_blame(struct tog_blame *blame)
3901 const struct got_error *err = NULL;
3902 int i;
3904 if (blame->thread) {
3905 int errcode;
3906 errcode = pthread_mutex_unlock(&tog_mutex);
3907 if (errcode)
3908 return got_error_set_errno(errcode,
3909 "pthread_mutex_unlock");
3910 errcode = pthread_join(blame->thread, (void **)&err);
3911 if (errcode)
3912 return got_error_set_errno(errcode, "pthread_join");
3913 errcode = pthread_mutex_lock(&tog_mutex);
3914 if (errcode)
3915 return got_error_set_errno(errcode,
3916 "pthread_mutex_lock");
3917 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3918 err = NULL;
3919 blame->thread = NULL;
3921 if (blame->thread_args.repo) {
3922 got_repo_close(blame->thread_args.repo);
3923 blame->thread_args.repo = NULL;
3925 if (blame->f) {
3926 if (fclose(blame->f) != 0 && err == NULL)
3927 err = got_error_from_errno("fclose");
3928 blame->f = NULL;
3930 if (blame->lines) {
3931 for (i = 0; i < blame->nlines; i++)
3932 free(blame->lines[i].id);
3933 free(blame->lines);
3934 blame->lines = NULL;
3936 free(blame->cb_args.commit_id);
3937 blame->cb_args.commit_id = NULL;
3939 return err;
3942 static const struct got_error *
3943 cancel_blame_view(void *arg)
3945 const struct got_error *err = NULL;
3946 int *done = arg;
3947 int errcode;
3949 errcode = pthread_mutex_lock(&tog_mutex);
3950 if (errcode)
3951 return got_error_set_errno(errcode,
3952 "pthread_mutex_unlock");
3954 if (*done)
3955 err = got_error(GOT_ERR_CANCELLED);
3957 errcode = pthread_mutex_unlock(&tog_mutex);
3958 if (errcode)
3959 return got_error_set_errno(errcode,
3960 "pthread_mutex_lock");
3962 return err;
3965 static const struct got_error *
3966 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3967 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3968 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3969 struct got_repository *repo)
3971 const struct got_error *err = NULL;
3972 struct got_blob_object *blob = NULL;
3973 struct got_repository *thread_repo = NULL;
3974 struct got_object_id *obj_id = NULL;
3975 int obj_type;
3977 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3978 if (err)
3979 return err;
3981 err = got_object_get_type(&obj_type, repo, obj_id);
3982 if (err)
3983 goto done;
3985 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3986 err = got_error(GOT_ERR_OBJ_TYPE);
3987 goto done;
3990 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3991 if (err)
3992 goto done;
3993 blame->f = got_opentemp();
3994 if (blame->f == NULL) {
3995 err = got_error_from_errno("got_opentemp");
3996 goto done;
3998 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3999 &blame->line_offsets, blame->f, blob);
4000 if (err || blame->nlines == 0)
4001 goto done;
4003 /* Don't include \n at EOF in the blame line count. */
4004 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4005 blame->nlines--;
4007 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4008 if (blame->lines == NULL) {
4009 err = got_error_from_errno("calloc");
4010 goto done;
4013 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4014 if (err)
4015 goto done;
4017 blame->cb_args.view = view;
4018 blame->cb_args.lines = blame->lines;
4019 blame->cb_args.nlines = blame->nlines;
4020 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4021 if (blame->cb_args.commit_id == NULL) {
4022 err = got_error_from_errno("got_object_id_dup");
4023 goto done;
4025 blame->cb_args.quit = done;
4027 blame->thread_args.path = path;
4028 blame->thread_args.repo = thread_repo;
4029 blame->thread_args.cb_args = &blame->cb_args;
4030 blame->thread_args.complete = blame_complete;
4031 blame->thread_args.cancel_cb = cancel_blame_view;
4032 blame->thread_args.cancel_arg = done;
4033 *blame_complete = 0;
4035 done:
4036 if (blob)
4037 got_object_blob_close(blob);
4038 free(obj_id);
4039 if (err)
4040 stop_blame(blame);
4041 return err;
4044 static const struct got_error *
4045 open_blame_view(struct tog_view *view, char *path,
4046 struct got_object_id *commit_id, struct got_reflist_head *refs,
4047 struct got_repository *repo)
4049 const struct got_error *err = NULL;
4050 struct tog_blame_view_state *s = &view->state.blame;
4052 SIMPLEQ_INIT(&s->blamed_commits);
4054 s->path = strdup(path);
4055 if (s->path == NULL)
4056 return got_error_from_errno("strdup");
4058 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4059 if (err) {
4060 free(s->path);
4061 return err;
4064 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4065 s->first_displayed_line = 1;
4066 s->last_displayed_line = view->nlines;
4067 s->selected_line = 1;
4068 s->blame_complete = 0;
4069 s->repo = repo;
4070 s->refs = refs;
4071 s->commit_id = commit_id;
4072 memset(&s->blame, 0, sizeof(s->blame));
4074 SIMPLEQ_INIT(&s->colors);
4075 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4076 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4077 get_color_value("TOG_COLOR_COMMIT"));
4078 if (err)
4079 return err;
4082 view->show = show_blame_view;
4083 view->input = input_blame_view;
4084 view->close = close_blame_view;
4085 view->search_start = search_start_blame_view;
4086 view->search_next = search_next_blame_view;
4088 return run_blame(&s->blame, view, &s->blame_complete,
4089 &s->first_displayed_line, &s->last_displayed_line,
4090 &s->selected_line, &s->done, &s->eof, s->path,
4091 s->blamed_commit->id, s->repo);
4094 static const struct got_error *
4095 close_blame_view(struct tog_view *view)
4097 const struct got_error *err = NULL;
4098 struct tog_blame_view_state *s = &view->state.blame;
4100 if (s->blame.thread)
4101 err = stop_blame(&s->blame);
4103 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4104 struct got_object_qid *blamed_commit;
4105 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4106 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4107 got_object_qid_free(blamed_commit);
4110 free(s->path);
4111 free_colors(&s->colors);
4113 return err;
4116 static const struct got_error *
4117 search_start_blame_view(struct tog_view *view)
4119 struct tog_blame_view_state *s = &view->state.blame;
4121 s->matched_line = 0;
4122 return NULL;
4125 static const struct got_error *
4126 search_next_blame_view(struct tog_view *view)
4128 struct tog_blame_view_state *s = &view->state.blame;
4129 int lineno;
4131 if (!view->searching) {
4132 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4133 return NULL;
4136 if (s->matched_line) {
4137 if (view->searching == TOG_SEARCH_FORWARD)
4138 lineno = s->matched_line + 1;
4139 else
4140 lineno = s->matched_line - 1;
4141 } else {
4142 if (view->searching == TOG_SEARCH_FORWARD)
4143 lineno = 1;
4144 else
4145 lineno = s->blame.nlines;
4148 while (1) {
4149 char *line = NULL;
4150 off_t offset;
4151 size_t len;
4153 if (lineno <= 0 || lineno > s->blame.nlines) {
4154 if (s->matched_line == 0) {
4155 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4156 free(line);
4157 break;
4160 if (view->searching == TOG_SEARCH_FORWARD)
4161 lineno = 1;
4162 else
4163 lineno = s->blame.nlines;
4166 offset = s->blame.line_offsets[lineno - 1];
4167 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4168 free(line);
4169 return got_error_from_errno("fseeko");
4171 free(line);
4172 line = parse_next_line(s->blame.f, &len);
4173 if (line && match_line(line, &view->regex)) {
4174 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4175 s->matched_line = lineno;
4176 free(line);
4177 break;
4179 free(line);
4180 if (view->searching == TOG_SEARCH_FORWARD)
4181 lineno++;
4182 else
4183 lineno--;
4186 if (s->matched_line) {
4187 s->first_displayed_line = s->matched_line;
4188 s->selected_line = 1;
4191 return NULL;
4194 static const struct got_error *
4195 show_blame_view(struct tog_view *view)
4197 const struct got_error *err = NULL;
4198 struct tog_blame_view_state *s = &view->state.blame;
4199 int errcode;
4201 if (s->blame.thread == NULL) {
4202 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4203 &s->blame.thread_args);
4204 if (errcode)
4205 return got_error_set_errno(errcode, "pthread_create");
4207 halfdelay(1); /* fast refresh while annotating */
4210 if (s->blame_complete)
4211 halfdelay(10); /* disable fast refresh */
4213 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4214 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4215 s->selected_line, &s->first_displayed_line,
4216 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4218 view_vborder(view);
4219 return err;
4222 static const struct got_error *
4223 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4224 struct tog_view **focus_view, struct tog_view *view, int ch)
4226 const struct got_error *err = NULL, *thread_err = NULL;
4227 struct tog_view *diff_view;
4228 struct tog_blame_view_state *s = &view->state.blame;
4229 int begin_x = 0;
4231 switch (ch) {
4232 case 'q':
4233 s->done = 1;
4234 break;
4235 case 'k':
4236 case KEY_UP:
4237 if (s->selected_line > 1)
4238 s->selected_line--;
4239 else if (s->selected_line == 1 &&
4240 s->first_displayed_line > 1)
4241 s->first_displayed_line--;
4242 break;
4243 case KEY_PPAGE:
4244 case CTRL('b'):
4245 if (s->first_displayed_line == 1) {
4246 s->selected_line = 1;
4247 break;
4249 if (s->first_displayed_line > view->nlines - 2)
4250 s->first_displayed_line -=
4251 (view->nlines - 2);
4252 else
4253 s->first_displayed_line = 1;
4254 break;
4255 case 'j':
4256 case KEY_DOWN:
4257 if (s->selected_line < view->nlines - 2 &&
4258 s->first_displayed_line +
4259 s->selected_line <= s->blame.nlines)
4260 s->selected_line++;
4261 else if (s->last_displayed_line <
4262 s->blame.nlines)
4263 s->first_displayed_line++;
4264 break;
4265 case 'b':
4266 case 'p': {
4267 struct got_object_id *id = NULL;
4268 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4269 s->first_displayed_line, s->selected_line);
4270 if (id == NULL)
4271 break;
4272 if (ch == 'p') {
4273 struct got_commit_object *commit;
4274 struct got_object_qid *pid;
4275 struct got_object_id *blob_id = NULL;
4276 int obj_type;
4277 err = got_object_open_as_commit(&commit,
4278 s->repo, id);
4279 if (err)
4280 break;
4281 pid = SIMPLEQ_FIRST(
4282 got_object_commit_get_parent_ids(commit));
4283 if (pid == NULL) {
4284 got_object_commit_close(commit);
4285 break;
4287 /* Check if path history ends here. */
4288 err = got_object_id_by_path(&blob_id, s->repo,
4289 pid->id, s->path);
4290 if (err) {
4291 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4292 err = NULL;
4293 got_object_commit_close(commit);
4294 break;
4296 err = got_object_get_type(&obj_type, s->repo,
4297 blob_id);
4298 free(blob_id);
4299 /* Can't blame non-blob type objects. */
4300 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4301 got_object_commit_close(commit);
4302 break;
4304 err = got_object_qid_alloc(&s->blamed_commit,
4305 pid->id);
4306 got_object_commit_close(commit);
4307 } else {
4308 if (got_object_id_cmp(id,
4309 s->blamed_commit->id) == 0)
4310 break;
4311 err = got_object_qid_alloc(&s->blamed_commit,
4312 id);
4314 if (err)
4315 break;
4316 s->done = 1;
4317 thread_err = stop_blame(&s->blame);
4318 s->done = 0;
4319 if (thread_err)
4320 break;
4321 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4322 s->blamed_commit, entry);
4323 err = run_blame(&s->blame, view, &s->blame_complete,
4324 &s->first_displayed_line, &s->last_displayed_line,
4325 &s->selected_line, &s->done, &s->eof,
4326 s->path, s->blamed_commit->id, s->repo);
4327 if (err)
4328 break;
4329 break;
4331 case 'B': {
4332 struct got_object_qid *first;
4333 first = SIMPLEQ_FIRST(&s->blamed_commits);
4334 if (!got_object_id_cmp(first->id, s->commit_id))
4335 break;
4336 s->done = 1;
4337 thread_err = stop_blame(&s->blame);
4338 s->done = 0;
4339 if (thread_err)
4340 break;
4341 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4342 got_object_qid_free(s->blamed_commit);
4343 s->blamed_commit =
4344 SIMPLEQ_FIRST(&s->blamed_commits);
4345 err = run_blame(&s->blame, view, &s->blame_complete,
4346 &s->first_displayed_line, &s->last_displayed_line,
4347 &s->selected_line, &s->done, &s->eof, s->path,
4348 s->blamed_commit->id, s->repo);
4349 if (err)
4350 break;
4351 break;
4353 case KEY_ENTER:
4354 case '\r': {
4355 struct got_object_id *id = NULL;
4356 struct got_object_qid *pid;
4357 struct got_commit_object *commit = NULL;
4358 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4359 s->first_displayed_line, s->selected_line);
4360 if (id == NULL)
4361 break;
4362 err = got_object_open_as_commit(&commit, s->repo, id);
4363 if (err)
4364 break;
4365 pid = SIMPLEQ_FIRST(
4366 got_object_commit_get_parent_ids(commit));
4367 if (view_is_parent_view(view))
4368 begin_x = view_split_begin_x(view->begin_x);
4369 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4370 if (diff_view == NULL) {
4371 got_object_commit_close(commit);
4372 err = got_error_from_errno("view_open");
4373 break;
4375 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4376 id, NULL, s->refs, s->repo);
4377 got_object_commit_close(commit);
4378 if (err) {
4379 view_close(diff_view);
4380 break;
4382 if (view_is_parent_view(view)) {
4383 err = view_close_child(view);
4384 if (err)
4385 break;
4386 err = view_set_child(view, diff_view);
4387 if (err) {
4388 view_close(diff_view);
4389 break;
4391 *focus_view = diff_view;
4392 view->child_focussed = 1;
4393 } else
4394 *new_view = diff_view;
4395 if (err)
4396 break;
4397 break;
4399 case KEY_NPAGE:
4400 case CTRL('f'):
4401 case ' ':
4402 if (s->last_displayed_line >= s->blame.nlines &&
4403 s->selected_line >= MIN(s->blame.nlines,
4404 view->nlines - 2)) {
4405 break;
4407 if (s->last_displayed_line >= s->blame.nlines &&
4408 s->selected_line < view->nlines - 2) {
4409 s->selected_line = MIN(s->blame.nlines,
4410 view->nlines - 2);
4411 break;
4413 if (s->last_displayed_line + view->nlines - 2
4414 <= s->blame.nlines)
4415 s->first_displayed_line +=
4416 view->nlines - 2;
4417 else
4418 s->first_displayed_line =
4419 s->blame.nlines -
4420 (view->nlines - 3);
4421 break;
4422 case KEY_RESIZE:
4423 if (s->selected_line > view->nlines - 2) {
4424 s->selected_line = MIN(s->blame.nlines,
4425 view->nlines - 2);
4427 break;
4428 default:
4429 break;
4431 return thread_err ? thread_err : err;
4434 static const struct got_error *
4435 cmd_blame(int argc, char *argv[])
4437 const struct got_error *error;
4438 struct got_repository *repo = NULL;
4439 struct got_reflist_head refs;
4440 struct got_worktree *worktree = NULL;
4441 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4442 struct got_object_id *commit_id = NULL;
4443 char *commit_id_str = NULL;
4444 int ch;
4445 struct tog_view *view;
4447 SIMPLEQ_INIT(&refs);
4449 #ifndef PROFILE
4450 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4451 NULL) == -1)
4452 err(1, "pledge");
4453 #endif
4455 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4456 switch (ch) {
4457 case 'c':
4458 commit_id_str = optarg;
4459 break;
4460 case 'r':
4461 repo_path = realpath(optarg, NULL);
4462 if (repo_path == NULL)
4463 return got_error_from_errno2("realpath",
4464 optarg);
4465 break;
4466 default:
4467 usage_blame();
4468 /* NOTREACHED */
4472 argc -= optind;
4473 argv += optind;
4475 if (argc != 1)
4476 usage_blame();
4478 cwd = getcwd(NULL, 0);
4479 if (cwd == NULL)
4480 return got_error_from_errno("getcwd");
4482 error = got_worktree_open(&worktree, cwd);
4483 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4484 goto done;
4486 if (repo_path == NULL) {
4487 if (worktree)
4488 repo_path =
4489 strdup(got_worktree_get_repo_path(worktree));
4490 else
4491 repo_path = strdup(cwd);
4493 if (repo_path == NULL) {
4494 error = got_error_from_errno("strdup");
4495 goto done;
4498 error = got_repo_open(&repo, repo_path, NULL);
4499 if (error != NULL)
4500 goto done;
4502 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4503 worktree);
4504 if (error)
4505 goto done;
4507 init_curses();
4509 error = apply_unveil(got_repo_get_path(repo), NULL);
4510 if (error)
4511 goto done;
4513 if (commit_id_str == NULL) {
4514 struct got_reference *head_ref;
4515 error = got_ref_open(&head_ref, repo, worktree ?
4516 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4517 if (error != NULL)
4518 goto done;
4519 error = got_ref_resolve(&commit_id, repo, head_ref);
4520 got_ref_close(head_ref);
4521 } else {
4522 error = got_repo_match_object_id(&commit_id, NULL,
4523 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4525 if (error != NULL)
4526 goto done;
4528 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4529 if (error)
4530 goto done;
4532 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4533 if (view == NULL) {
4534 error = got_error_from_errno("view_open");
4535 goto done;
4537 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4538 if (error)
4539 goto done;
4540 if (worktree) {
4541 /* Release work tree lock. */
4542 got_worktree_close(worktree);
4543 worktree = NULL;
4545 error = view_loop(view);
4546 done:
4547 free(repo_path);
4548 free(in_repo_path);
4549 free(cwd);
4550 free(commit_id);
4551 if (worktree)
4552 got_worktree_close(worktree);
4553 if (repo)
4554 got_repo_close(repo);
4555 got_ref_list_free(&refs);
4556 return error;
4559 static const struct got_error *
4560 draw_tree_entries(struct tog_view *view,
4561 struct got_tree_entry **first_displayed_entry,
4562 struct got_tree_entry **last_displayed_entry,
4563 struct got_tree_entry **selected_entry, int *ndisplayed,
4564 const char *label, int show_ids, const char *parent_path,
4565 struct got_tree_object *tree, int selected, int limit,
4566 int isroot, struct tog_colors *colors)
4568 const struct got_error *err = NULL;
4569 struct got_tree_entry *te;
4570 wchar_t *wline;
4571 struct tog_color *tc;
4572 int width, n, i, nentries;
4574 *ndisplayed = 0;
4576 werase(view->window);
4578 if (limit == 0)
4579 return NULL;
4581 err = format_line(&wline, &width, label, view->ncols, 0);
4582 if (err)
4583 return err;
4584 if (view_needs_focus_indication(view))
4585 wstandout(view->window);
4586 tc = get_color(colors, TOG_COLOR_COMMIT);
4587 if (tc)
4588 wattr_on(view->window,
4589 COLOR_PAIR(tc->colorpair), NULL);
4590 waddwstr(view->window, wline);
4591 if (tc)
4592 wattr_off(view->window,
4593 COLOR_PAIR(tc->colorpair), NULL);
4594 if (view_needs_focus_indication(view))
4595 wstandend(view->window);
4596 free(wline);
4597 wline = NULL;
4598 if (width < view->ncols - 1)
4599 waddch(view->window, '\n');
4600 if (--limit <= 0)
4601 return NULL;
4602 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4603 if (err)
4604 return err;
4605 waddwstr(view->window, wline);
4606 free(wline);
4607 wline = NULL;
4608 if (width < view->ncols - 1)
4609 waddch(view->window, '\n');
4610 if (--limit <= 0)
4611 return NULL;
4612 waddch(view->window, '\n');
4613 if (--limit <= 0)
4614 return NULL;
4616 if (*first_displayed_entry == NULL) {
4617 te = got_object_tree_get_first_entry(tree);
4618 if (selected == 0) {
4619 if (view->focussed)
4620 wstandout(view->window);
4621 *selected_entry = NULL;
4623 waddstr(view->window, " ..\n"); /* parent directory */
4624 if (selected == 0 && view->focussed)
4625 wstandend(view->window);
4626 (*ndisplayed)++;
4627 if (--limit <= 0)
4628 return NULL;
4629 n = 1;
4630 } else {
4631 n = 0;
4632 te = *first_displayed_entry;
4635 nentries = got_object_tree_get_nentries(tree);
4636 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4637 char *line = NULL, *id_str = NULL;
4638 const char *modestr = "";
4639 mode_t mode;
4641 te = got_object_tree_get_entry(tree, i);
4642 mode = got_tree_entry_get_mode(te);
4644 if (show_ids) {
4645 err = got_object_id_str(&id_str,
4646 got_tree_entry_get_id(te));
4647 if (err)
4648 return got_error_from_errno(
4649 "got_object_id_str");
4651 if (got_object_tree_entry_is_submodule(te))
4652 modestr = "$";
4653 else if (S_ISLNK(mode))
4654 modestr = "@";
4655 else if (S_ISDIR(mode))
4656 modestr = "/";
4657 else if (mode & S_IXUSR)
4658 modestr = "*";
4659 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4660 got_tree_entry_get_name(te), modestr) == -1) {
4661 free(id_str);
4662 return got_error_from_errno("asprintf");
4664 free(id_str);
4665 err = format_line(&wline, &width, line, view->ncols, 0);
4666 if (err) {
4667 free(line);
4668 break;
4670 if (n == selected) {
4671 if (view->focussed)
4672 wstandout(view->window);
4673 *selected_entry = te;
4675 tc = match_color(colors, line);
4676 if (tc)
4677 wattr_on(view->window,
4678 COLOR_PAIR(tc->colorpair), NULL);
4679 waddwstr(view->window, wline);
4680 if (tc)
4681 wattr_off(view->window,
4682 COLOR_PAIR(tc->colorpair), NULL);
4683 if (width < view->ncols - 1)
4684 waddch(view->window, '\n');
4685 if (n == selected && view->focussed)
4686 wstandend(view->window);
4687 free(line);
4688 free(wline);
4689 wline = NULL;
4690 n++;
4691 (*ndisplayed)++;
4692 *last_displayed_entry = te;
4693 if (--limit <= 0)
4694 break;
4697 return err;
4700 static void
4701 tree_scroll_up(struct tog_view *view,
4702 struct got_tree_entry **first_displayed_entry, int maxscroll,
4703 struct got_tree_object *tree, int isroot)
4705 struct got_tree_entry *te;
4706 int i;
4708 if (*first_displayed_entry == NULL)
4709 return;
4711 te = got_object_tree_get_entry(tree, 0);
4712 if (*first_displayed_entry == te) {
4713 if (!isroot)
4714 *first_displayed_entry = NULL;
4715 return;
4718 i = 0;
4719 while (*first_displayed_entry && i < maxscroll) {
4720 *first_displayed_entry = got_tree_entry_get_prev(tree,
4721 *first_displayed_entry);
4722 i++;
4724 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4725 *first_displayed_entry = NULL;
4728 static int
4729 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4730 struct got_tree_entry *last_displayed_entry,
4731 struct got_tree_object *tree)
4733 struct got_tree_entry *next, *last;
4734 int n = 0;
4736 if (*first_displayed_entry)
4737 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4738 else
4739 next = got_object_tree_get_first_entry(tree);
4741 last = last_displayed_entry;
4742 while (next && last && n++ < maxscroll) {
4743 last = got_tree_entry_get_next(tree, last);
4744 if (last) {
4745 *first_displayed_entry = next;
4746 next = got_tree_entry_get_next(tree, next);
4749 return n;
4752 static const struct got_error *
4753 tree_entry_path(char **path, struct tog_parent_trees *parents,
4754 struct got_tree_entry *te)
4756 const struct got_error *err = NULL;
4757 struct tog_parent_tree *pt;
4758 size_t len = 2; /* for leading slash and NUL */
4760 TAILQ_FOREACH(pt, parents, entry)
4761 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4762 + 1 /* slash */;
4763 if (te)
4764 len += strlen(got_tree_entry_get_name(te));
4766 *path = calloc(1, len);
4767 if (path == NULL)
4768 return got_error_from_errno("calloc");
4770 (*path)[0] = '/';
4771 pt = TAILQ_LAST(parents, tog_parent_trees);
4772 while (pt) {
4773 const char *name = got_tree_entry_get_name(pt->selected_entry);
4774 if (strlcat(*path, name, len) >= len) {
4775 err = got_error(GOT_ERR_NO_SPACE);
4776 goto done;
4778 if (strlcat(*path, "/", len) >= len) {
4779 err = got_error(GOT_ERR_NO_SPACE);
4780 goto done;
4782 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4784 if (te) {
4785 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4786 err = got_error(GOT_ERR_NO_SPACE);
4787 goto done;
4790 done:
4791 if (err) {
4792 free(*path);
4793 *path = NULL;
4795 return err;
4798 static const struct got_error *
4799 blame_tree_entry(struct tog_view **new_view, int begin_x,
4800 struct got_tree_entry *te, struct tog_parent_trees *parents,
4801 struct got_object_id *commit_id, struct got_reflist_head *refs,
4802 struct got_repository *repo)
4804 const struct got_error *err = NULL;
4805 char *path;
4806 struct tog_view *blame_view;
4808 *new_view = NULL;
4810 err = tree_entry_path(&path, parents, te);
4811 if (err)
4812 return err;
4814 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4815 if (blame_view == NULL) {
4816 err = got_error_from_errno("view_open");
4817 goto done;
4820 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4821 if (err) {
4822 if (err->code == GOT_ERR_CANCELLED)
4823 err = NULL;
4824 view_close(blame_view);
4825 } else
4826 *new_view = blame_view;
4827 done:
4828 free(path);
4829 return err;
4832 static const struct got_error *
4833 log_tree_entry(struct tog_view **new_view, int begin_x,
4834 struct got_tree_entry *te, struct tog_parent_trees *parents,
4835 struct got_object_id *commit_id, struct got_reflist_head *refs,
4836 struct got_repository *repo)
4838 struct tog_view *log_view;
4839 const struct got_error *err = NULL;
4840 char *path;
4842 *new_view = NULL;
4844 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4845 if (log_view == NULL)
4846 return got_error_from_errno("view_open");
4848 err = tree_entry_path(&path, parents, te);
4849 if (err)
4850 return err;
4852 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4853 if (err)
4854 view_close(log_view);
4855 else
4856 *new_view = log_view;
4857 free(path);
4858 return err;
4861 static const struct got_error *
4862 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4863 struct got_object_id *commit_id, struct got_reflist_head *refs,
4864 struct got_repository *repo)
4866 const struct got_error *err = NULL;
4867 char *commit_id_str = NULL;
4868 struct tog_tree_view_state *s = &view->state.tree;
4870 TAILQ_INIT(&s->parents);
4872 err = got_object_id_str(&commit_id_str, commit_id);
4873 if (err != NULL)
4874 goto done;
4876 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4877 err = got_error_from_errno("asprintf");
4878 goto done;
4881 s->root = s->tree = root;
4882 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4883 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4884 s->commit_id = got_object_id_dup(commit_id);
4885 if (s->commit_id == NULL) {
4886 err = got_error_from_errno("got_object_id_dup");
4887 goto done;
4889 s->refs = refs;
4890 s->repo = repo;
4892 SIMPLEQ_INIT(&s->colors);
4894 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4895 err = add_color(&s->colors, "\\$$",
4896 TOG_COLOR_TREE_SUBMODULE,
4897 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4898 if (err)
4899 goto done;
4900 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4901 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4902 if (err) {
4903 free_colors(&s->colors);
4904 goto done;
4906 err = add_color(&s->colors, "/$",
4907 TOG_COLOR_TREE_DIRECTORY,
4908 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4909 if (err) {
4910 free_colors(&s->colors);
4911 goto done;
4914 err = add_color(&s->colors, "\\*$",
4915 TOG_COLOR_TREE_EXECUTABLE,
4916 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4917 if (err) {
4918 free_colors(&s->colors);
4919 goto done;
4922 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4923 get_color_value("TOG_COLOR_COMMIT"));
4924 if (err) {
4925 free_colors(&s->colors);
4926 goto done;
4930 view->show = show_tree_view;
4931 view->input = input_tree_view;
4932 view->close = close_tree_view;
4933 view->search_start = search_start_tree_view;
4934 view->search_next = search_next_tree_view;
4935 done:
4936 free(commit_id_str);
4937 if (err) {
4938 free(s->tree_label);
4939 s->tree_label = NULL;
4941 return err;
4944 static const struct got_error *
4945 close_tree_view(struct tog_view *view)
4947 struct tog_tree_view_state *s = &view->state.tree;
4949 free_colors(&s->colors);
4950 free(s->tree_label);
4951 s->tree_label = NULL;
4952 free(s->commit_id);
4953 s->commit_id = NULL;
4954 while (!TAILQ_EMPTY(&s->parents)) {
4955 struct tog_parent_tree *parent;
4956 parent = TAILQ_FIRST(&s->parents);
4957 TAILQ_REMOVE(&s->parents, parent, entry);
4958 free(parent);
4961 if (s->tree != s->root)
4962 got_object_tree_close(s->tree);
4963 got_object_tree_close(s->root);
4965 return NULL;
4968 static const struct got_error *
4969 search_start_tree_view(struct tog_view *view)
4971 struct tog_tree_view_state *s = &view->state.tree;
4973 s->matched_entry = NULL;
4974 return NULL;
4977 static int
4978 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4980 regmatch_t regmatch;
4982 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4983 0) == 0;
4986 static const struct got_error *
4987 search_next_tree_view(struct tog_view *view)
4989 struct tog_tree_view_state *s = &view->state.tree;
4990 struct got_tree_entry *te = NULL;
4992 if (!view->searching) {
4993 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4994 return NULL;
4997 if (s->matched_entry) {
4998 if (view->searching == TOG_SEARCH_FORWARD) {
4999 if (s->selected_entry)
5000 te = got_tree_entry_get_next(s->tree,
5001 s->selected_entry);
5002 else
5003 te = got_object_tree_get_first_entry(s->tree);
5004 } else {
5005 if (s->selected_entry == NULL)
5006 te = got_object_tree_get_last_entry(s->tree);
5007 else
5008 te = got_tree_entry_get_prev(s->tree,
5009 s->selected_entry);
5011 } else {
5012 if (view->searching == TOG_SEARCH_FORWARD)
5013 te = got_object_tree_get_first_entry(s->tree);
5014 else
5015 te = got_object_tree_get_last_entry(s->tree);
5018 while (1) {
5019 if (te == NULL) {
5020 if (s->matched_entry == NULL) {
5021 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5022 return NULL;
5024 if (view->searching == TOG_SEARCH_FORWARD)
5025 te = got_object_tree_get_first_entry(s->tree);
5026 else
5027 te = got_object_tree_get_last_entry(s->tree);
5030 if (match_tree_entry(te, &view->regex)) {
5031 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5032 s->matched_entry = te;
5033 break;
5036 if (view->searching == TOG_SEARCH_FORWARD)
5037 te = got_tree_entry_get_next(s->tree, te);
5038 else
5039 te = got_tree_entry_get_prev(s->tree, te);
5042 if (s->matched_entry) {
5043 s->first_displayed_entry = s->matched_entry;
5044 s->selected = 0;
5047 return NULL;
5050 static const struct got_error *
5051 show_tree_view(struct tog_view *view)
5053 const struct got_error *err = NULL;
5054 struct tog_tree_view_state *s = &view->state.tree;
5055 char *parent_path;
5057 err = tree_entry_path(&parent_path, &s->parents, NULL);
5058 if (err)
5059 return err;
5061 err = draw_tree_entries(view, &s->first_displayed_entry,
5062 &s->last_displayed_entry, &s->selected_entry,
5063 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5064 s->tree, s->selected, view->nlines, s->tree == s->root,
5065 &s->colors);
5066 free(parent_path);
5068 view_vborder(view);
5069 return err;
5072 static const struct got_error *
5073 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5074 struct tog_view **focus_view, struct tog_view *view, int ch)
5076 const struct got_error *err = NULL;
5077 struct tog_tree_view_state *s = &view->state.tree;
5078 struct tog_view *log_view;
5079 int begin_x = 0, nscrolled;
5081 switch (ch) {
5082 case 'i':
5083 s->show_ids = !s->show_ids;
5084 break;
5085 case 'l':
5086 if (!s->selected_entry)
5087 break;
5088 if (view_is_parent_view(view))
5089 begin_x = view_split_begin_x(view->begin_x);
5090 err = log_tree_entry(&log_view, begin_x,
5091 s->selected_entry, &s->parents,
5092 s->commit_id, s->refs, s->repo);
5093 if (view_is_parent_view(view)) {
5094 err = view_close_child(view);
5095 if (err)
5096 return err;
5097 err = view_set_child(view, log_view);
5098 if (err) {
5099 view_close(log_view);
5100 break;
5102 *focus_view = log_view;
5103 view->child_focussed = 1;
5104 } else
5105 *new_view = log_view;
5106 break;
5107 case 'k':
5108 case KEY_UP:
5109 if (s->selected > 0) {
5110 s->selected--;
5111 if (s->selected == 0)
5112 break;
5114 if (s->selected > 0)
5115 break;
5116 tree_scroll_up(view, &s->first_displayed_entry, 1,
5117 s->tree, s->tree == s->root);
5118 break;
5119 case KEY_PPAGE:
5120 case CTRL('b'):
5121 tree_scroll_up(view, &s->first_displayed_entry,
5122 MAX(0, view->nlines - 4 - s->selected), s->tree,
5123 s->tree == s->root);
5124 s->selected = 0;
5125 if (got_object_tree_get_first_entry(s->tree) ==
5126 s->first_displayed_entry && s->tree != s->root)
5127 s->first_displayed_entry = NULL;
5128 break;
5129 case 'j':
5130 case KEY_DOWN:
5131 if (s->selected < s->ndisplayed - 1) {
5132 s->selected++;
5133 break;
5135 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5136 == NULL)
5137 /* can't scroll any further */
5138 break;
5139 tree_scroll_down(&s->first_displayed_entry, 1,
5140 s->last_displayed_entry, s->tree);
5141 break;
5142 case KEY_NPAGE:
5143 case CTRL('f'):
5144 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5145 == NULL) {
5146 /* can't scroll any further; move cursor down */
5147 if (s->selected < s->ndisplayed - 1)
5148 s->selected = s->ndisplayed - 1;
5149 break;
5151 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5152 view->nlines, s->last_displayed_entry, s->tree);
5153 if (nscrolled < view->nlines) {
5154 int ndisplayed = 0;
5155 struct got_tree_entry *te;
5156 te = s->first_displayed_entry;
5157 do {
5158 ndisplayed++;
5159 te = got_tree_entry_get_next(s->tree, te);
5160 } while (te);
5161 s->selected = ndisplayed - 1;
5163 break;
5164 case KEY_ENTER:
5165 case '\r':
5166 case KEY_BACKSPACE:
5167 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5168 struct tog_parent_tree *parent;
5169 /* user selected '..' */
5170 if (s->tree == s->root)
5171 break;
5172 parent = TAILQ_FIRST(&s->parents);
5173 TAILQ_REMOVE(&s->parents, parent,
5174 entry);
5175 got_object_tree_close(s->tree);
5176 s->tree = parent->tree;
5177 s->first_displayed_entry =
5178 parent->first_displayed_entry;
5179 s->selected_entry =
5180 parent->selected_entry;
5181 s->selected = parent->selected;
5182 free(parent);
5183 } else if (S_ISDIR(got_tree_entry_get_mode(
5184 s->selected_entry))) {
5185 struct got_tree_object *subtree;
5186 err = got_object_open_as_tree(&subtree, s->repo,
5187 got_tree_entry_get_id(s->selected_entry));
5188 if (err)
5189 break;
5190 err = tree_view_visit_subtree(subtree, s);
5191 if (err) {
5192 got_object_tree_close(subtree);
5193 break;
5195 } else if (S_ISREG(got_tree_entry_get_mode(
5196 s->selected_entry))) {
5197 struct tog_view *blame_view;
5198 int begin_x = view_is_parent_view(view) ?
5199 view_split_begin_x(view->begin_x) : 0;
5201 err = blame_tree_entry(&blame_view, begin_x,
5202 s->selected_entry, &s->parents,
5203 s->commit_id, s->refs, s->repo);
5204 if (err)
5205 break;
5206 if (view_is_parent_view(view)) {
5207 err = view_close_child(view);
5208 if (err)
5209 return err;
5210 err = view_set_child(view, blame_view);
5211 if (err) {
5212 view_close(blame_view);
5213 break;
5215 *focus_view = blame_view;
5216 view->child_focussed = 1;
5217 } else
5218 *new_view = blame_view;
5220 break;
5221 case KEY_RESIZE:
5222 if (s->selected > view->nlines)
5223 s->selected = s->ndisplayed - 1;
5224 break;
5225 default:
5226 break;
5229 return err;
5232 __dead static void
5233 usage_tree(void)
5235 endwin();
5236 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5237 getprogname());
5238 exit(1);
5241 static const struct got_error *
5242 cmd_tree(int argc, char *argv[])
5244 const struct got_error *error;
5245 struct got_repository *repo = NULL;
5246 struct got_worktree *worktree = NULL;
5247 struct got_reflist_head refs;
5248 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5249 struct got_object_id *commit_id = NULL;
5250 char *commit_id_arg = NULL;
5251 struct got_commit_object *commit = NULL;
5252 struct got_tree_object *tree = NULL;
5253 int ch;
5254 struct tog_view *view;
5256 SIMPLEQ_INIT(&refs);
5258 #ifndef PROFILE
5259 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5260 NULL) == -1)
5261 err(1, "pledge");
5262 #endif
5264 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5265 switch (ch) {
5266 case 'c':
5267 commit_id_arg = optarg;
5268 break;
5269 case 'r':
5270 repo_path = realpath(optarg, NULL);
5271 if (repo_path == NULL)
5272 return got_error_from_errno2("realpath",
5273 optarg);
5274 break;
5275 default:
5276 usage_tree();
5277 /* NOTREACHED */
5281 argc -= optind;
5282 argv += optind;
5284 if (argc > 1)
5285 usage_tree();
5287 cwd = getcwd(NULL, 0);
5288 if (cwd == NULL)
5289 return got_error_from_errno("getcwd");
5291 error = got_worktree_open(&worktree, cwd);
5292 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5293 goto done;
5295 if (repo_path == NULL) {
5296 if (worktree)
5297 repo_path =
5298 strdup(got_worktree_get_repo_path(worktree));
5299 else
5300 repo_path = strdup(cwd);
5302 if (repo_path == NULL) {
5303 error = got_error_from_errno("strdup");
5304 goto done;
5307 error = got_repo_open(&repo, repo_path, NULL);
5308 if (error != NULL)
5309 goto done;
5311 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5312 repo, worktree);
5313 if (error)
5314 goto done;
5316 init_curses();
5318 error = apply_unveil(got_repo_get_path(repo), NULL);
5319 if (error)
5320 goto done;
5322 error = got_repo_match_object_id(&commit_id, NULL,
5323 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5324 GOT_OBJ_TYPE_COMMIT, 1, repo);
5325 if (error)
5326 goto done;
5328 error = got_object_open_as_commit(&commit, repo, commit_id);
5329 if (error)
5330 goto done;
5332 error = got_object_open_as_tree(&tree, repo,
5333 got_object_commit_get_tree_id(commit));
5334 if (error)
5335 goto done;
5337 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5338 if (error)
5339 goto done;
5341 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5342 if (view == NULL) {
5343 error = got_error_from_errno("view_open");
5344 goto done;
5346 error = open_tree_view(view, tree, commit_id, &refs, repo);
5347 if (error)
5348 goto done;
5349 if (!got_path_is_root_dir(in_repo_path)) {
5350 error = tree_view_walk_path(&view->state.tree, commit_id,
5351 in_repo_path, repo);
5352 if (error)
5353 goto done;
5356 if (worktree) {
5357 /* Release work tree lock. */
5358 got_worktree_close(worktree);
5359 worktree = NULL;
5361 error = view_loop(view);
5362 done:
5363 free(repo_path);
5364 free(cwd);
5365 free(commit_id);
5366 if (commit)
5367 got_object_commit_close(commit);
5368 if (tree)
5369 got_object_tree_close(tree);
5370 if (repo)
5371 got_repo_close(repo);
5372 got_ref_list_free(&refs);
5373 return error;
5376 static void
5377 list_commands(void)
5379 int i;
5381 fprintf(stderr, "commands:");
5382 for (i = 0; i < nitems(tog_commands); i++) {
5383 struct tog_cmd *cmd = &tog_commands[i];
5384 fprintf(stderr, " %s", cmd->name);
5386 fputc('\n', stderr);
5389 __dead static void
5390 usage(int hflag)
5392 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] "
5393 "[arg ...]\n", getprogname());
5394 if (hflag) {
5395 fprintf(stderr, "lazy usage: %s path\n", getprogname());
5396 list_commands();
5398 exit(1);
5401 static char **
5402 make_argv(int argc, ...)
5404 va_list ap;
5405 char **argv;
5406 int i;
5408 va_start(ap, argc);
5410 argv = calloc(argc, sizeof(char *));
5411 if (argv == NULL)
5412 err(1, "calloc");
5413 for (i = 0; i < argc; i++) {
5414 argv[i] = strdup(va_arg(ap, char *));
5415 if (argv[i] == NULL)
5416 err(1, "strdup");
5419 va_end(ap);
5420 return argv;
5424 * Try to convert 'tog path' into a 'tog log path' command.
5425 * The user could simply have mistyped the command rather than knowingly
5426 * provided a path. So check whether argv[0] can in fact be resolved
5427 * to a path in the HEAD commit and print a special error if not.
5428 * This hack is for mpi@ <3
5430 static const struct got_error *
5431 tog_log_with_path(int argc, char *argv[])
5433 const struct got_error *error = NULL;
5434 struct tog_cmd *cmd = NULL;
5435 struct got_repository *repo = NULL;
5436 struct got_worktree *worktree = NULL;
5437 struct got_object_id *commit_id = NULL, *id = NULL;
5438 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5439 char *commit_id_str = NULL, **cmd_argv = NULL;
5441 cwd = getcwd(NULL, 0);
5442 if (cwd == NULL)
5443 return got_error_from_errno("getcwd");
5445 error = got_worktree_open(&worktree, cwd);
5446 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5447 goto done;
5449 if (worktree)
5450 repo_path = strdup(got_worktree_get_repo_path(worktree));
5451 else
5452 repo_path = strdup(cwd);
5453 if (repo_path == NULL) {
5454 error = got_error_from_errno("strdup");
5455 goto done;
5458 error = got_repo_open(&repo, repo_path, NULL);
5459 if (error != NULL)
5460 goto done;
5462 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5463 repo, worktree);
5464 if (error)
5465 goto done;
5467 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5468 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5469 GOT_OBJ_TYPE_COMMIT, 1, repo);
5470 if (error)
5471 goto done;
5473 if (worktree) {
5474 got_worktree_close(worktree);
5475 worktree = NULL;
5478 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5479 if (error) {
5480 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5481 goto done;
5482 fprintf(stderr, "%s: '%s' is no known command or path\n",
5483 getprogname(), argv[0]);
5484 usage(1);
5485 /* not reached */
5488 got_repo_close(repo);
5489 repo = NULL;
5491 error = got_object_id_str(&commit_id_str, commit_id);
5492 if (error)
5493 goto done;
5495 cmd = &tog_commands[0]; /* log */
5496 argc = 4;
5497 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5498 error = cmd->cmd_main(argc, cmd_argv);
5499 done:
5500 if (repo)
5501 got_repo_close(repo);
5502 if (worktree)
5503 got_worktree_close(worktree);
5504 free(id);
5505 free(commit_id_str);
5506 free(commit_id);
5507 free(cwd);
5508 free(repo_path);
5509 free(in_repo_path);
5510 if (cmd_argv) {
5511 int i;
5512 for (i = 0; i < argc; i++)
5513 free(cmd_argv[i]);
5514 free(cmd_argv);
5516 return error;
5519 int
5520 main(int argc, char *argv[])
5522 const struct got_error *error = NULL;
5523 struct tog_cmd *cmd = NULL;
5524 int ch, hflag = 0, Vflag = 0;
5525 char **cmd_argv = NULL;
5526 static struct option longopts[] = {
5527 { "version", no_argument, NULL, 'V' },
5528 { NULL, 0, NULL, 0}
5531 setlocale(LC_CTYPE, "");
5533 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5534 switch (ch) {
5535 case 'h':
5536 hflag = 1;
5537 break;
5538 case 'V':
5539 Vflag = 1;
5540 break;
5541 default:
5542 usage(hflag);
5543 /* NOTREACHED */
5547 argc -= optind;
5548 argv += optind;
5549 optind = 0;
5550 optreset = 1;
5552 if (Vflag) {
5553 got_version_print_str();
5554 return 1;
5557 if (argc == 0) {
5558 if (hflag)
5559 usage(hflag);
5560 /* Build an argument vector which runs a default command. */
5561 cmd = &tog_commands[0];
5562 argc = 1;
5563 cmd_argv = make_argv(argc, cmd->name);
5564 } else {
5565 int i;
5567 /* Did the user specify a command? */
5568 for (i = 0; i < nitems(tog_commands); i++) {
5569 if (strncmp(tog_commands[i].name, argv[0],
5570 strlen(argv[0])) == 0) {
5571 cmd = &tog_commands[i];
5572 break;
5577 if (cmd == NULL) {
5578 if (argc != 1)
5579 usage(0);
5580 /* No command specified; try log with a path */
5581 error = tog_log_with_path(argc, argv);
5582 } else {
5583 if (hflag)
5584 cmd->cmd_usage();
5585 else
5586 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5589 endwin();
5590 if (cmd_argv) {
5591 int i;
5592 for (i = 0; i < argc; i++)
5593 free(cmd_argv[i]);
5594 free(cmd_argv);
5597 if (error && error->code != GOT_ERR_CANCELLED)
5598 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5599 return 0;