Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED
24 #include <curses.h>
25 #undef _XOPEN_SOURCE_EXTENDED
26 #include <panel.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <util.h>
37 #include <limits.h>
38 #include <wchar.h>
39 #include <time.h>
40 #include <pthread.h>
41 #include <libgen.h>
42 #include <regex.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_diff.h"
50 #include "got_opentemp.h"
51 #include "got_utf8.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_path.h"
57 #include "got_worktree.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #ifndef MAX
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #endif
67 #define CTRL(x) ((x) & 0x1f)
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 struct tog_cmd {
74 const char *name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 };
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
98 };
100 enum tog_view_type {
101 TOG_VIEW_DIFF,
102 TOG_VIEW_LOG,
103 TOG_VIEW_BLAME,
104 TOG_VIEW_TREE,
105 TOG_VIEW_REF,
106 };
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
114 int idx;
115 };
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
118 int ncommits;
119 struct commit_queue_head head;
120 };
122 struct tog_color {
123 SIMPLEQ_ENTRY(tog_color) entry;
124 regex_t regex;
125 short colorpair;
126 };
127 SIMPLEQ_HEAD(tog_colors, tog_color);
129 static const struct got_error *
130 add_color(struct tog_colors *colors, const char *pattern,
131 int idx, short color)
133 const struct got_error *err = NULL;
134 struct tog_color *tc;
135 int regerr = 0;
137 if (idx < 1 || idx > COLOR_PAIRS - 1)
138 return NULL;
140 init_pair(idx, color, -1);
142 tc = calloc(1, sizeof(*tc));
143 if (tc == NULL)
144 return got_error_from_errno("calloc");
145 regerr = regcomp(&tc->regex, pattern,
146 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
147 if (regerr) {
148 static char regerr_msg[512];
149 static char err_msg[512];
150 regerror(regerr, &tc->regex, regerr_msg,
151 sizeof(regerr_msg));
152 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
153 regerr_msg);
154 err = got_error_msg(GOT_ERR_REGEX, err_msg);
155 free(tc);
156 return err;
158 tc->colorpair = idx;
159 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
160 return NULL;
163 static void
164 free_colors(struct tog_colors *colors)
166 struct tog_color *tc;
168 while (!SIMPLEQ_EMPTY(colors)) {
169 tc = SIMPLEQ_FIRST(colors);
170 SIMPLEQ_REMOVE_HEAD(colors, entry);
171 regfree(&tc->regex);
172 free(tc);
176 struct tog_color *
177 get_color(struct tog_colors *colors, int colorpair)
179 struct tog_color *tc = NULL;
181 SIMPLEQ_FOREACH(tc, colors, entry) {
182 if (tc->colorpair == colorpair)
183 return tc;
186 return NULL;
189 static int
190 default_color_value(const char *envvar)
192 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
193 return COLOR_MAGENTA;
194 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
195 return COLOR_CYAN;
196 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
197 return COLOR_YELLOW;
198 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
199 return COLOR_GREEN;
200 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
201 return COLOR_MAGENTA;
202 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
203 return COLOR_MAGENTA;
204 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
205 return COLOR_CYAN;
206 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
207 return COLOR_GREEN;
208 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
209 return COLOR_GREEN;
210 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
211 return COLOR_CYAN;
212 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
213 return COLOR_YELLOW;
214 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
215 return COLOR_GREEN;
216 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
217 return COLOR_MAGENTA;
218 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
219 return COLOR_YELLOW;
221 return -1;
224 static int
225 get_color_value(const char *envvar)
227 const char *val = getenv(envvar);
229 if (val == NULL)
230 return default_color_value(envvar);
232 if (strcasecmp(val, "black") == 0)
233 return COLOR_BLACK;
234 if (strcasecmp(val, "red") == 0)
235 return COLOR_RED;
236 if (strcasecmp(val, "green") == 0)
237 return COLOR_GREEN;
238 if (strcasecmp(val, "yellow") == 0)
239 return COLOR_YELLOW;
240 if (strcasecmp(val, "blue") == 0)
241 return COLOR_BLUE;
242 if (strcasecmp(val, "magenta") == 0)
243 return COLOR_MAGENTA;
244 if (strcasecmp(val, "cyan") == 0)
245 return COLOR_CYAN;
246 if (strcasecmp(val, "white") == 0)
247 return COLOR_WHITE;
248 if (strcasecmp(val, "default") == 0)
249 return -1;
251 return default_color_value(envvar);
255 struct tog_diff_view_state {
256 struct got_object_id *id1, *id2;
257 const char *label1, *label2;
258 FILE *f;
259 int first_displayed_line;
260 int last_displayed_line;
261 int eof;
262 int diff_context;
263 int ignore_whitespace;
264 int force_text_diff;
265 struct got_repository *repo;
266 struct got_reflist_head refs;
267 struct tog_colors colors;
268 size_t nlines;
269 off_t *line_offsets;
270 int matched_line;
271 int selected_line;
273 /* passed from log view; may be NULL */
274 struct tog_view *log_view;
275 };
277 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
279 struct tog_log_thread_args {
280 pthread_cond_t need_commits;
281 pthread_cond_t commit_loaded;
282 int commits_needed;
283 struct got_commit_graph *graph;
284 struct commit_queue *commits;
285 const char *in_repo_path;
286 struct got_object_id *start_id;
287 struct got_repository *repo;
288 int log_complete;
289 sig_atomic_t *quit;
290 struct commit_queue_entry **first_displayed_entry;
291 struct commit_queue_entry **selected_entry;
292 int *searching;
293 int *search_next_done;
294 regex_t *regex;
295 };
297 struct tog_log_view_state {
298 struct commit_queue commits;
299 struct commit_queue_entry *first_displayed_entry;
300 struct commit_queue_entry *last_displayed_entry;
301 struct commit_queue_entry *selected_entry;
302 int selected;
303 char *in_repo_path;
304 const char *head_ref_name;
305 int log_branches;
306 struct got_repository *repo;
307 struct got_reflist_head refs;
308 struct got_object_id *start_id;
309 sig_atomic_t quit;
310 pthread_t thread;
311 struct tog_log_thread_args thread_args;
312 struct commit_queue_entry *matched_entry;
313 struct commit_queue_entry *search_entry;
314 struct tog_colors colors;
315 };
317 #define TOG_COLOR_DIFF_MINUS 1
318 #define TOG_COLOR_DIFF_PLUS 2
319 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
320 #define TOG_COLOR_DIFF_META 4
321 #define TOG_COLOR_TREE_SUBMODULE 5
322 #define TOG_COLOR_TREE_SYMLINK 6
323 #define TOG_COLOR_TREE_DIRECTORY 7
324 #define TOG_COLOR_TREE_EXECUTABLE 8
325 #define TOG_COLOR_COMMIT 9
326 #define TOG_COLOR_AUTHOR 10
327 #define TOG_COLOR_DATE 11
328 #define TOG_COLOR_REFS_HEADS 12
329 #define TOG_COLOR_REFS_TAGS 13
330 #define TOG_COLOR_REFS_REMOTES 14
332 struct tog_blame_cb_args {
333 struct tog_blame_line *lines; /* one per line */
334 int nlines;
336 struct tog_view *view;
337 struct got_object_id *commit_id;
338 int *quit;
339 };
341 struct tog_blame_thread_args {
342 const char *path;
343 struct got_repository *repo;
344 struct tog_blame_cb_args *cb_args;
345 int *complete;
346 got_cancel_cb cancel_cb;
347 void *cancel_arg;
348 };
350 struct tog_blame {
351 FILE *f;
352 off_t filesize;
353 struct tog_blame_line *lines;
354 int nlines;
355 off_t *line_offsets;
356 pthread_t thread;
357 struct tog_blame_thread_args thread_args;
358 struct tog_blame_cb_args cb_args;
359 const char *path;
360 };
362 struct tog_blame_view_state {
363 int first_displayed_line;
364 int last_displayed_line;
365 int selected_line;
366 int blame_complete;
367 int eof;
368 int done;
369 struct got_object_id_queue blamed_commits;
370 struct got_object_qid *blamed_commit;
371 char *path;
372 struct got_repository *repo;
373 struct got_object_id *commit_id;
374 struct tog_blame blame;
375 int matched_line;
376 struct tog_colors colors;
377 };
379 struct tog_parent_tree {
380 TAILQ_ENTRY(tog_parent_tree) entry;
381 struct got_tree_object *tree;
382 struct got_tree_entry *first_displayed_entry;
383 struct got_tree_entry *selected_entry;
384 int selected;
385 };
387 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
389 struct tog_tree_view_state {
390 char *tree_label;
391 struct got_tree_object *root;
392 struct got_tree_object *tree;
393 struct got_tree_entry *first_displayed_entry;
394 struct got_tree_entry *last_displayed_entry;
395 struct got_tree_entry *selected_entry;
396 int ndisplayed, selected, show_ids;
397 struct tog_parent_trees parents;
398 struct got_object_id *commit_id;
399 struct got_repository *repo;
400 struct got_tree_entry *matched_entry;
401 struct tog_colors colors;
402 };
404 struct tog_reflist_entry {
405 TAILQ_ENTRY(tog_reflist_entry) entry;
406 struct got_reference *ref;
407 int idx;
408 };
410 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
412 struct tog_ref_view_state {
413 struct got_reflist_head simplerefs; /* SIMPLEQ */
414 struct tog_reflist_head refs; /* TAILQ */
415 struct tog_reflist_entry *first_displayed_entry;
416 struct tog_reflist_entry *last_displayed_entry;
417 struct tog_reflist_entry *selected_entry;
418 int nrefs, ndisplayed, selected, show_ids;
419 struct got_repository *repo;
420 struct tog_reflist_entry *matched_entry;
421 struct tog_colors colors;
422 };
424 /*
425 * We implement two types of views: parent views and child views.
427 * The 'Tab' key switches between a parent view and its child view.
428 * Child views are shown side-by-side to their parent view, provided
429 * there is enough screen estate.
431 * When a new view is opened from within a parent view, this new view
432 * becomes a child view of the parent view, replacing any existing child.
434 * When a new view is opened from within a child view, this new view
435 * becomes a parent view which will obscure the views below until the
436 * user quits the new parent view by typing 'q'.
438 * This list of views contains parent views only.
439 * Child views are only pointed to by their parent view.
440 */
441 TAILQ_HEAD(tog_view_list_head, tog_view);
443 struct tog_view {
444 TAILQ_ENTRY(tog_view) entry;
445 WINDOW *window;
446 PANEL *panel;
447 int nlines, ncols, begin_y, begin_x;
448 int lines, cols; /* copies of LINES and COLS */
449 int focussed;
450 struct tog_view *parent;
451 struct tog_view *child;
452 int child_focussed;
454 /* type-specific state */
455 enum tog_view_type type;
456 union {
457 struct tog_diff_view_state diff;
458 struct tog_log_view_state log;
459 struct tog_blame_view_state blame;
460 struct tog_tree_view_state tree;
461 struct tog_ref_view_state ref;
462 } state;
464 const struct got_error *(*show)(struct tog_view *);
465 const struct got_error *(*input)(struct tog_view **,
466 struct tog_view **, struct tog_view**, struct tog_view *, int);
467 const struct got_error *(*close)(struct tog_view *);
469 const struct got_error *(*search_start)(struct tog_view *);
470 const struct got_error *(*search_next)(struct tog_view *);
471 int searching;
472 #define TOG_SEARCH_FORWARD 1
473 #define TOG_SEARCH_BACKWARD 2
474 int search_next_done;
475 #define TOG_SEARCH_HAVE_MORE 1
476 #define TOG_SEARCH_NO_MORE 2
477 #define TOG_SEARCH_HAVE_NONE 3
478 regex_t regex;
479 regmatch_t regmatch;
480 };
482 static const struct got_error *open_diff_view(struct tog_view *,
483 struct got_object_id *, struct got_object_id *,
484 const char *, const char *, int, int, int, struct tog_view *,
485 struct got_repository *);
486 static const struct got_error *show_diff_view(struct tog_view *);
487 static const struct got_error *input_diff_view(struct tog_view **,
488 struct tog_view **, struct tog_view **, struct tog_view *, int);
489 static const struct got_error* close_diff_view(struct tog_view *);
490 static const struct got_error *search_start_diff_view(struct tog_view *);
491 static const struct got_error *search_next_diff_view(struct tog_view *);
493 static const struct got_error *open_log_view(struct tog_view *,
494 struct got_object_id *, struct got_repository *,
495 const char *, const char *, int);
496 static const struct got_error * show_log_view(struct tog_view *);
497 static const struct got_error *input_log_view(struct tog_view **,
498 struct tog_view **, struct tog_view **, struct tog_view *, int);
499 static const struct got_error *close_log_view(struct tog_view *);
500 static const struct got_error *search_start_log_view(struct tog_view *);
501 static const struct got_error *search_next_log_view(struct tog_view *);
503 static const struct got_error *open_blame_view(struct tog_view *, char *,
504 struct got_object_id *, struct got_repository *);
505 static const struct got_error *show_blame_view(struct tog_view *);
506 static const struct got_error *input_blame_view(struct tog_view **,
507 struct tog_view **, struct tog_view **, struct tog_view *, int);
508 static const struct got_error *close_blame_view(struct tog_view *);
509 static const struct got_error *search_start_blame_view(struct tog_view *);
510 static const struct got_error *search_next_blame_view(struct tog_view *);
512 static const struct got_error *open_tree_view(struct tog_view *,
513 struct got_tree_object *, struct got_object_id *, struct got_repository *);
514 static const struct got_error *show_tree_view(struct tog_view *);
515 static const struct got_error *input_tree_view(struct tog_view **,
516 struct tog_view **, struct tog_view **, struct tog_view *, int);
517 static const struct got_error *close_tree_view(struct tog_view *);
518 static const struct got_error *search_start_tree_view(struct tog_view *);
519 static const struct got_error *search_next_tree_view(struct tog_view *);
521 static const struct got_error *open_ref_view(struct tog_view *,
522 struct got_repository *);
523 static const struct got_error *show_ref_view(struct tog_view *);
524 static const struct got_error *input_ref_view(struct tog_view **,
525 struct tog_view **, struct tog_view **, struct tog_view *, int);
526 static const struct got_error *close_ref_view(struct tog_view *);
527 static const struct got_error *search_start_ref_view(struct tog_view *);
528 static const struct got_error *search_next_ref_view(struct tog_view *);
530 static volatile sig_atomic_t tog_sigwinch_received;
531 static volatile sig_atomic_t tog_sigpipe_received;
532 static volatile sig_atomic_t tog_sigcont_received;
534 static void
535 tog_sigwinch(int signo)
537 tog_sigwinch_received = 1;
540 static void
541 tog_sigpipe(int signo)
543 tog_sigpipe_received = 1;
546 static void
547 tog_sigcont(int signo)
549 tog_sigcont_received = 1;
552 static const struct got_error *
553 view_close(struct tog_view *view)
555 const struct got_error *err = NULL;
557 if (view->child) {
558 view_close(view->child);
559 view->child = NULL;
561 if (view->close)
562 err = view->close(view);
563 if (view->panel)
564 del_panel(view->panel);
565 if (view->window)
566 delwin(view->window);
567 free(view);
568 return err;
571 static struct tog_view *
572 view_open(int nlines, int ncols, int begin_y, int begin_x,
573 enum tog_view_type type)
575 struct tog_view *view = calloc(1, sizeof(*view));
577 if (view == NULL)
578 return NULL;
580 view->type = type;
581 view->lines = LINES;
582 view->cols = COLS;
583 view->nlines = nlines ? nlines : LINES - begin_y;
584 view->ncols = ncols ? ncols : COLS - begin_x;
585 view->begin_y = begin_y;
586 view->begin_x = begin_x;
587 view->window = newwin(nlines, ncols, begin_y, begin_x);
588 if (view->window == NULL) {
589 view_close(view);
590 return NULL;
592 view->panel = new_panel(view->window);
593 if (view->panel == NULL ||
594 set_panel_userptr(view->panel, view) != OK) {
595 view_close(view);
596 return NULL;
599 keypad(view->window, TRUE);
600 return view;
603 static int
604 view_split_begin_x(int begin_x)
606 if (begin_x > 0 || COLS < 120)
607 return 0;
608 return (COLS - MAX(COLS / 2, 80));
611 static const struct got_error *view_resize(struct tog_view *);
613 static const struct got_error *
614 view_splitscreen(struct tog_view *view)
616 const struct got_error *err = NULL;
618 view->begin_y = 0;
619 view->begin_x = view_split_begin_x(0);
620 view->nlines = LINES;
621 view->ncols = COLS - view->begin_x;
622 view->lines = LINES;
623 view->cols = COLS;
624 err = view_resize(view);
625 if (err)
626 return err;
628 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
629 return got_error_from_errno("mvwin");
631 return NULL;
634 static const struct got_error *
635 view_fullscreen(struct tog_view *view)
637 const struct got_error *err = NULL;
639 view->begin_x = 0;
640 view->begin_y = 0;
641 view->nlines = LINES;
642 view->ncols = COLS;
643 view->lines = LINES;
644 view->cols = COLS;
645 err = view_resize(view);
646 if (err)
647 return err;
649 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
650 return got_error_from_errno("mvwin");
652 return NULL;
655 static int
656 view_is_parent_view(struct tog_view *view)
658 return view->parent == NULL;
661 static const struct got_error *
662 view_resize(struct tog_view *view)
664 int nlines, ncols;
666 if (view->lines > LINES)
667 nlines = view->nlines - (view->lines - LINES);
668 else
669 nlines = view->nlines + (LINES - view->lines);
671 if (view->cols > COLS)
672 ncols = view->ncols - (view->cols - COLS);
673 else
674 ncols = view->ncols + (COLS - view->cols);
676 if (wresize(view->window, nlines, ncols) == ERR)
677 return got_error_from_errno("wresize");
678 if (replace_panel(view->panel, view->window) == ERR)
679 return got_error_from_errno("replace_panel");
680 wclear(view->window);
682 view->nlines = nlines;
683 view->ncols = ncols;
684 view->lines = LINES;
685 view->cols = COLS;
687 if (view->child) {
688 view->child->begin_x = view_split_begin_x(view->begin_x);
689 if (view->child->begin_x == 0) {
690 view_fullscreen(view->child);
691 if (view->child->focussed)
692 show_panel(view->child->panel);
693 else
694 show_panel(view->panel);
695 } else {
696 view_splitscreen(view->child);
697 show_panel(view->child->panel);
701 return NULL;
704 static const struct got_error *
705 view_close_child(struct tog_view *view)
707 const struct got_error *err = NULL;
709 if (view->child == NULL)
710 return NULL;
712 err = view_close(view->child);
713 view->child = NULL;
714 return err;
717 static const struct got_error *
718 view_set_child(struct tog_view *view, struct tog_view *child)
720 const struct got_error *err = NULL;
722 view->child = child;
723 child->parent = view;
724 return err;
727 static int
728 view_is_splitscreen(struct tog_view *view)
730 return view->begin_x > 0;
733 static void
734 tog_resizeterm(void)
736 int cols, lines;
737 struct winsize size;
739 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
740 cols = 80; /* Default */
741 lines = 24;
742 } else {
743 cols = size.ws_col;
744 lines = size.ws_row;
746 resize_term(lines, cols);
749 static const struct got_error *
750 view_search_start(struct tog_view *view)
752 const struct got_error *err = NULL;
753 char pattern[1024];
754 int ret;
756 if (view->nlines < 1)
757 return NULL;
759 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
760 wclrtoeol(view->window);
762 nocbreak();
763 echo();
764 ret = wgetnstr(view->window, pattern, sizeof(pattern));
765 cbreak();
766 noecho();
767 if (ret == ERR)
768 return NULL;
770 if (view->searching) {
771 regfree(&view->regex);
772 view->searching = 0;
775 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
776 err = view->search_start(view);
777 if (err) {
778 regfree(&view->regex);
779 return err;
781 view->searching = TOG_SEARCH_FORWARD;
782 view->search_next_done = 0;
783 view->search_next(view);
786 return NULL;
789 static const struct got_error *
790 view_input(struct tog_view **new, struct tog_view **dead,
791 struct tog_view **focus, int *done, struct tog_view *view,
792 struct tog_view_list_head *views)
794 const struct got_error *err = NULL;
795 struct tog_view *v;
796 int ch, errcode;
798 *new = NULL;
799 *dead = NULL;
800 *focus = NULL;
802 /* Clear "no matches" indicator. */
803 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
804 view->search_next_done == TOG_SEARCH_HAVE_NONE)
805 view->search_next_done = TOG_SEARCH_HAVE_MORE;
807 if (view->searching && !view->search_next_done) {
808 errcode = pthread_mutex_unlock(&tog_mutex);
809 if (errcode)
810 return got_error_set_errno(errcode,
811 "pthread_mutex_unlock");
812 pthread_yield();
813 errcode = pthread_mutex_lock(&tog_mutex);
814 if (errcode)
815 return got_error_set_errno(errcode,
816 "pthread_mutex_lock");
817 view->search_next(view);
818 return NULL;
821 nodelay(stdscr, FALSE);
822 /* Allow threads to make progress while we are waiting for input. */
823 errcode = pthread_mutex_unlock(&tog_mutex);
824 if (errcode)
825 return got_error_set_errno(errcode, "pthread_mutex_unlock");
826 ch = wgetch(view->window);
827 errcode = pthread_mutex_lock(&tog_mutex);
828 if (errcode)
829 return got_error_set_errno(errcode, "pthread_mutex_lock");
830 nodelay(stdscr, TRUE);
832 if (tog_sigwinch_received || tog_sigcont_received) {
833 tog_resizeterm();
834 tog_sigwinch_received = 0;
835 tog_sigcont_received = 0;
836 TAILQ_FOREACH(v, views, entry) {
837 err = view_resize(v);
838 if (err)
839 return err;
840 err = v->input(new, dead, focus, v, KEY_RESIZE);
841 if (err)
842 return err;
846 switch (ch) {
847 case ERR:
848 break;
849 case '\t':
850 if (view->child) {
851 *focus = view->child;
852 view->child_focussed = 1;
853 } else if (view->parent) {
854 *focus = view->parent;
855 view->parent->child_focussed = 0;
857 break;
858 case 'q':
859 err = view->input(new, dead, focus, view, ch);
860 *dead = view;
861 break;
862 case 'Q':
863 *done = 1;
864 break;
865 case 'f':
866 if (view_is_parent_view(view)) {
867 if (view->child == NULL)
868 break;
869 if (view_is_splitscreen(view->child)) {
870 *focus = view->child;
871 view->child_focussed = 1;
872 err = view_fullscreen(view->child);
873 } else
874 err = view_splitscreen(view->child);
875 if (err)
876 break;
877 err = view->child->input(new, dead, focus,
878 view->child, KEY_RESIZE);
879 } else {
880 if (view_is_splitscreen(view)) {
881 *focus = view;
882 view->parent->child_focussed = 1;
883 err = view_fullscreen(view);
884 } else {
885 err = view_splitscreen(view);
887 if (err)
888 break;
889 err = view->input(new, dead, focus, view,
890 KEY_RESIZE);
892 break;
893 case KEY_RESIZE:
894 break;
895 case '/':
896 if (view->search_start)
897 view_search_start(view);
898 else
899 err = view->input(new, dead, focus, view, ch);
900 break;
901 case 'N':
902 case 'n':
903 if (view->search_next) {
904 view->searching = (ch == 'n' ?
905 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
906 view->search_next_done = 0;
907 view->search_next(view);
908 } else
909 err = view->input(new, dead, focus, view, ch);
910 break;
911 default:
912 err = view->input(new, dead, focus, view, ch);
913 break;
916 return err;
919 void
920 view_vborder(struct tog_view *view)
922 PANEL *panel;
923 struct tog_view *view_above;
925 if (view->parent)
926 return view_vborder(view->parent);
928 panel = panel_above(view->panel);
929 if (panel == NULL)
930 return;
932 view_above = panel_userptr(panel);
933 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
934 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
937 int
938 view_needs_focus_indication(struct tog_view *view)
940 if (view_is_parent_view(view)) {
941 if (view->child == NULL || view->child_focussed)
942 return 0;
943 if (!view_is_splitscreen(view->child))
944 return 0;
945 } else if (!view_is_splitscreen(view))
946 return 0;
948 return view->focussed;
951 static const struct got_error *
952 view_loop(struct tog_view *view)
954 const struct got_error *err = NULL;
955 struct tog_view_list_head views;
956 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
957 int fast_refresh = 10;
958 int done = 0, errcode;
960 errcode = pthread_mutex_lock(&tog_mutex);
961 if (errcode)
962 return got_error_set_errno(errcode, "pthread_mutex_lock");
964 TAILQ_INIT(&views);
965 TAILQ_INSERT_HEAD(&views, view, entry);
967 main_view = view;
968 view->focussed = 1;
969 err = view->show(view);
970 if (err)
971 return err;
972 update_panels();
973 doupdate();
974 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
975 /* Refresh fast during initialization, then become slower. */
976 if (fast_refresh && fast_refresh-- == 0)
977 halfdelay(10); /* switch to once per second */
979 err = view_input(&new_view, &dead_view, &focus_view, &done,
980 view, &views);
981 if (err)
982 break;
983 if (dead_view) {
984 struct tog_view *prev = NULL;
986 if (view_is_parent_view(dead_view))
987 prev = TAILQ_PREV(dead_view,
988 tog_view_list_head, entry);
989 else if (view->parent != dead_view)
990 prev = view->parent;
992 if (dead_view->parent)
993 dead_view->parent->child = NULL;
994 else
995 TAILQ_REMOVE(&views, dead_view, entry);
997 err = view_close(dead_view);
998 if (err || (dead_view == main_view && new_view == NULL))
999 goto done;
1001 if (view == dead_view) {
1002 if (focus_view)
1003 view = focus_view;
1004 else if (prev)
1005 view = prev;
1006 else if (!TAILQ_EMPTY(&views))
1007 view = TAILQ_LAST(&views,
1008 tog_view_list_head);
1009 else
1010 view = NULL;
1011 if (view) {
1012 if (view->child && view->child_focussed)
1013 focus_view = view->child;
1014 else
1015 focus_view = view;
1019 if (new_view) {
1020 struct tog_view *v, *t;
1021 /* Only allow one parent view per type. */
1022 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1023 if (v->type != new_view->type)
1024 continue;
1025 TAILQ_REMOVE(&views, v, entry);
1026 err = view_close(v);
1027 if (err)
1028 goto done;
1029 break;
1031 TAILQ_INSERT_TAIL(&views, new_view, entry);
1032 view = new_view;
1033 if (focus_view == NULL)
1034 focus_view = new_view;
1036 if (focus_view) {
1037 show_panel(focus_view->panel);
1038 if (view)
1039 view->focussed = 0;
1040 focus_view->focussed = 1;
1041 view = focus_view;
1042 if (new_view)
1043 show_panel(new_view->panel);
1044 if (view->child && view_is_splitscreen(view->child))
1045 show_panel(view->child->panel);
1047 if (view) {
1048 if (focus_view == NULL) {
1049 view->focussed = 1;
1050 show_panel(view->panel);
1051 if (view->child && view_is_splitscreen(view->child))
1052 show_panel(view->child->panel);
1053 focus_view = view;
1055 if (view->parent) {
1056 err = view->parent->show(view->parent);
1057 if (err)
1058 goto done;
1060 err = view->show(view);
1061 if (err)
1062 goto done;
1063 if (view->child) {
1064 err = view->child->show(view->child);
1065 if (err)
1066 goto done;
1068 update_panels();
1069 doupdate();
1072 done:
1073 while (!TAILQ_EMPTY(&views)) {
1074 view = TAILQ_FIRST(&views);
1075 TAILQ_REMOVE(&views, view, entry);
1076 view_close(view);
1079 errcode = pthread_mutex_unlock(&tog_mutex);
1080 if (errcode && err == NULL)
1081 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1083 return err;
1086 __dead static void
1087 usage_log(void)
1089 endwin();
1090 fprintf(stderr,
1091 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1092 getprogname());
1093 exit(1);
1096 /* Create newly allocated wide-character string equivalent to a byte string. */
1097 static const struct got_error *
1098 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1100 char *vis = NULL;
1101 const struct got_error *err = NULL;
1103 *ws = NULL;
1104 *wlen = mbstowcs(NULL, s, 0);
1105 if (*wlen == (size_t)-1) {
1106 int vislen;
1107 if (errno != EILSEQ)
1108 return got_error_from_errno("mbstowcs");
1110 /* byte string invalid in current encoding; try to "fix" it */
1111 err = got_mbsavis(&vis, &vislen, s);
1112 if (err)
1113 return err;
1114 *wlen = mbstowcs(NULL, vis, 0);
1115 if (*wlen == (size_t)-1) {
1116 err = got_error_from_errno("mbstowcs"); /* give up */
1117 goto done;
1121 *ws = calloc(*wlen + 1, sizeof(**ws));
1122 if (*ws == NULL) {
1123 err = got_error_from_errno("calloc");
1124 goto done;
1127 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1128 err = got_error_from_errno("mbstowcs");
1129 done:
1130 free(vis);
1131 if (err) {
1132 free(*ws);
1133 *ws = NULL;
1134 *wlen = 0;
1136 return err;
1139 /* Format a line for display, ensuring that it won't overflow a width limit. */
1140 static const struct got_error *
1141 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1142 int col_tab_align)
1144 const struct got_error *err = NULL;
1145 int cols = 0;
1146 wchar_t *wline = NULL;
1147 size_t wlen;
1148 int i;
1150 *wlinep = NULL;
1151 *widthp = 0;
1153 err = mbs2ws(&wline, &wlen, line);
1154 if (err)
1155 return err;
1157 i = 0;
1158 while (i < wlen) {
1159 int width = wcwidth(wline[i]);
1161 if (width == 0) {
1162 i++;
1163 continue;
1166 if (width == 1 || width == 2) {
1167 if (cols + width > wlimit)
1168 break;
1169 cols += width;
1170 i++;
1171 } else if (width == -1) {
1172 if (wline[i] == L'\t') {
1173 width = TABSIZE -
1174 ((cols + col_tab_align) % TABSIZE);
1175 if (cols + width > wlimit)
1176 break;
1177 cols += width;
1179 i++;
1180 } else {
1181 err = got_error_from_errno("wcwidth");
1182 goto done;
1185 wline[i] = L'\0';
1186 if (widthp)
1187 *widthp = cols;
1188 done:
1189 if (err)
1190 free(wline);
1191 else
1192 *wlinep = wline;
1193 return err;
1196 static const struct got_error*
1197 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1198 struct got_object_id *id, struct got_repository *repo)
1200 static const struct got_error *err = NULL;
1201 struct got_reflist_entry *re;
1202 char *s;
1203 const char *name;
1205 *refs_str = NULL;
1207 SIMPLEQ_FOREACH(re, refs, entry) {
1208 struct got_tag_object *tag = NULL;
1209 struct got_object_id *ref_id;
1210 int cmp;
1212 name = got_ref_get_name(re->ref);
1213 if (strcmp(name, GOT_REF_HEAD) == 0)
1214 continue;
1215 if (strncmp(name, "refs/", 5) == 0)
1216 name += 5;
1217 if (strncmp(name, "got/", 4) == 0)
1218 continue;
1219 if (strncmp(name, "heads/", 6) == 0)
1220 name += 6;
1221 if (strncmp(name, "remotes/", 8) == 0) {
1222 name += 8;
1223 s = strstr(name, "/" GOT_REF_HEAD);
1224 if (s != NULL && s[strlen(s)] == '\0')
1225 continue;
1227 err = got_ref_resolve(&ref_id, repo, re->ref);
1228 if (err)
1229 break;
1230 if (strncmp(name, "tags/", 5) == 0) {
1231 err = got_object_open_as_tag(&tag, repo, ref_id);
1232 if (err) {
1233 if (err->code != GOT_ERR_OBJ_TYPE) {
1234 free(ref_id);
1235 break;
1237 /* Ref points at something other than a tag. */
1238 err = NULL;
1239 tag = NULL;
1242 cmp = got_object_id_cmp(tag ?
1243 got_object_tag_get_object_id(tag) : ref_id, id);
1244 free(ref_id);
1245 if (tag)
1246 got_object_tag_close(tag);
1247 if (cmp != 0)
1248 continue;
1249 s = *refs_str;
1250 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1251 s ? ", " : "", name) == -1) {
1252 err = got_error_from_errno("asprintf");
1253 free(s);
1254 *refs_str = NULL;
1255 break;
1257 free(s);
1260 return err;
1263 static const struct got_error *
1264 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1265 int col_tab_align)
1267 char *smallerthan, *at;
1269 smallerthan = strchr(author, '<');
1270 if (smallerthan && smallerthan[1] != '\0')
1271 author = smallerthan + 1;
1272 at = strchr(author, '@');
1273 if (at)
1274 *at = '\0';
1275 return format_line(wauthor, author_width, author, limit, col_tab_align);
1278 static const struct got_error *
1279 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1280 struct got_object_id *id, const size_t date_display_cols,
1281 int author_display_cols)
1283 struct tog_log_view_state *s = &view->state.log;
1284 const struct got_error *err = NULL;
1285 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1286 char *logmsg0 = NULL, *logmsg = NULL;
1287 char *author = NULL;
1288 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1289 int author_width, logmsg_width;
1290 char *newline, *line = NULL;
1291 int col, limit;
1292 const int avail = view->ncols;
1293 struct tm tm;
1294 time_t committer_time;
1295 struct tog_color *tc;
1297 committer_time = got_object_commit_get_committer_time(commit);
1298 if (localtime_r(&committer_time, &tm) == NULL)
1299 return got_error_from_errno("localtime_r");
1300 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1301 >= sizeof(datebuf))
1302 return got_error(GOT_ERR_NO_SPACE);
1304 if (avail <= date_display_cols)
1305 limit = MIN(sizeof(datebuf) - 1, avail);
1306 else
1307 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1308 tc = get_color(&s->colors, TOG_COLOR_DATE);
1309 if (tc)
1310 wattr_on(view->window,
1311 COLOR_PAIR(tc->colorpair), NULL);
1312 waddnstr(view->window, datebuf, limit);
1313 if (tc)
1314 wattr_off(view->window,
1315 COLOR_PAIR(tc->colorpair), NULL);
1316 col = limit;
1317 if (col > avail)
1318 goto done;
1320 if (avail >= 120) {
1321 char *id_str;
1322 err = got_object_id_str(&id_str, id);
1323 if (err)
1324 goto done;
1325 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1326 if (tc)
1327 wattr_on(view->window,
1328 COLOR_PAIR(tc->colorpair), NULL);
1329 wprintw(view->window, "%.8s ", id_str);
1330 if (tc)
1331 wattr_off(view->window,
1332 COLOR_PAIR(tc->colorpair), NULL);
1333 free(id_str);
1334 col += 9;
1335 if (col > avail)
1336 goto done;
1339 author = strdup(got_object_commit_get_author(commit));
1340 if (author == NULL) {
1341 err = got_error_from_errno("strdup");
1342 goto done;
1344 err = format_author(&wauthor, &author_width, author, avail - col, col);
1345 if (err)
1346 goto done;
1347 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1348 if (tc)
1349 wattr_on(view->window,
1350 COLOR_PAIR(tc->colorpair), NULL);
1351 waddwstr(view->window, wauthor);
1352 if (tc)
1353 wattr_off(view->window,
1354 COLOR_PAIR(tc->colorpair), NULL);
1355 col += author_width;
1356 while (col < avail && author_width < author_display_cols + 2) {
1357 waddch(view->window, ' ');
1358 col++;
1359 author_width++;
1361 if (col > avail)
1362 goto done;
1364 err = got_object_commit_get_logmsg(&logmsg0, commit);
1365 if (err)
1366 goto done;
1367 logmsg = logmsg0;
1368 while (*logmsg == '\n')
1369 logmsg++;
1370 newline = strchr(logmsg, '\n');
1371 if (newline)
1372 *newline = '\0';
1373 limit = avail - col;
1374 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1375 if (err)
1376 goto done;
1377 waddwstr(view->window, wlogmsg);
1378 col += logmsg_width;
1379 while (col < avail) {
1380 waddch(view->window, ' ');
1381 col++;
1383 done:
1384 free(logmsg0);
1385 free(wlogmsg);
1386 free(author);
1387 free(wauthor);
1388 free(line);
1389 return err;
1392 static struct commit_queue_entry *
1393 alloc_commit_queue_entry(struct got_commit_object *commit,
1394 struct got_object_id *id)
1396 struct commit_queue_entry *entry;
1398 entry = calloc(1, sizeof(*entry));
1399 if (entry == NULL)
1400 return NULL;
1402 entry->id = id;
1403 entry->commit = commit;
1404 return entry;
1407 static void
1408 pop_commit(struct commit_queue *commits)
1410 struct commit_queue_entry *entry;
1412 entry = TAILQ_FIRST(&commits->head);
1413 TAILQ_REMOVE(&commits->head, entry, entry);
1414 got_object_commit_close(entry->commit);
1415 commits->ncommits--;
1416 /* Don't free entry->id! It is owned by the commit graph. */
1417 free(entry);
1420 static void
1421 free_commits(struct commit_queue *commits)
1423 while (!TAILQ_EMPTY(&commits->head))
1424 pop_commit(commits);
1427 static const struct got_error *
1428 match_commit(int *have_match, struct got_object_id *id,
1429 struct got_commit_object *commit, regex_t *regex)
1431 const struct got_error *err = NULL;
1432 regmatch_t regmatch;
1433 char *id_str = NULL, *logmsg = NULL;
1435 *have_match = 0;
1437 err = got_object_id_str(&id_str, id);
1438 if (err)
1439 return err;
1441 err = got_object_commit_get_logmsg(&logmsg, commit);
1442 if (err)
1443 goto done;
1445 if (regexec(regex, got_object_commit_get_author(commit), 1,
1446 &regmatch, 0) == 0 ||
1447 regexec(regex, got_object_commit_get_committer(commit), 1,
1448 &regmatch, 0) == 0 ||
1449 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1450 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1451 *have_match = 1;
1452 done:
1453 free(id_str);
1454 free(logmsg);
1455 return err;
1458 static const struct got_error *
1459 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1460 int minqueue, struct got_repository *repo, const char *path,
1461 int *searching, int *search_next_done, regex_t *regex)
1463 const struct got_error *err = NULL;
1464 int nqueued = 0;
1467 * We keep all commits open throughout the lifetime of the log
1468 * view in order to avoid having to re-fetch commits from disk
1469 * while updating the display.
1471 while (nqueued < minqueue ||
1472 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1473 struct got_object_id *id;
1474 struct got_commit_object *commit;
1475 struct commit_queue_entry *entry;
1476 int errcode;
1478 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1479 if (err || id == NULL)
1480 break;
1482 err = got_object_open_as_commit(&commit, repo, id);
1483 if (err)
1484 break;
1485 entry = alloc_commit_queue_entry(commit, id);
1486 if (entry == NULL) {
1487 err = got_error_from_errno("alloc_commit_queue_entry");
1488 break;
1491 errcode = pthread_mutex_lock(&tog_mutex);
1492 if (errcode) {
1493 err = got_error_set_errno(errcode,
1494 "pthread_mutex_lock");
1495 break;
1498 entry->idx = commits->ncommits;
1499 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1500 nqueued++;
1501 commits->ncommits++;
1503 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1504 int have_match;
1505 err = match_commit(&have_match, id, commit, regex);
1506 if (err)
1507 break;
1508 if (have_match)
1509 *search_next_done = TOG_SEARCH_HAVE_MORE;
1512 errcode = pthread_mutex_unlock(&tog_mutex);
1513 if (errcode && err == NULL)
1514 err = got_error_set_errno(errcode,
1515 "pthread_mutex_unlock");
1516 if (err)
1517 break;
1520 return err;
1523 static const struct got_error *
1524 draw_commits(struct tog_view *view)
1526 const struct got_error *err = NULL;
1527 struct tog_log_view_state *s = &view->state.log;
1528 struct commit_queue_entry *entry;
1529 const int limit = view->nlines;
1530 int width;
1531 int ncommits, author_cols = 4;
1532 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1533 char *refs_str = NULL;
1534 wchar_t *wline;
1535 struct tog_color *tc;
1536 static const size_t date_display_cols = 12;
1538 entry = s->first_displayed_entry;
1539 ncommits = 0;
1540 while (entry) {
1541 if (ncommits == s->selected) {
1542 s->selected_entry = entry;
1543 break;
1545 entry = TAILQ_NEXT(entry, entry);
1546 ncommits++;
1549 if (s->selected_entry &&
1550 !(view->searching && view->search_next_done == 0)) {
1551 err = got_object_id_str(&id_str, s->selected_entry->id);
1552 if (err)
1553 return err;
1554 err = build_refs_str(&refs_str, &s->refs,
1555 s->selected_entry->id, s->repo);
1556 if (err)
1557 goto done;
1560 if (s->thread_args.commits_needed == 0)
1561 halfdelay(10); /* disable fast refresh */
1563 if (s->thread_args.commits_needed > 0) {
1564 if (asprintf(&ncommits_str, " [%d/%d] %s",
1565 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1566 (view->searching && !view->search_next_done) ?
1567 "searching..." : "loading...") == -1) {
1568 err = got_error_from_errno("asprintf");
1569 goto done;
1571 } else {
1572 const char *search_str = NULL;
1574 if (view->searching) {
1575 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1576 search_str = "no more matches";
1577 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1578 search_str = "no matches found";
1579 else if (!view->search_next_done)
1580 search_str = "searching...";
1583 if (asprintf(&ncommits_str, " [%d/%d] %s",
1584 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1585 search_str ? search_str :
1586 (refs_str ? refs_str : "")) == -1) {
1587 err = got_error_from_errno("asprintf");
1588 goto done;
1592 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1593 if (asprintf(&header, "commit %s %s%s",
1594 id_str ? id_str : "........................................",
1595 s->in_repo_path, ncommits_str) == -1) {
1596 err = got_error_from_errno("asprintf");
1597 header = NULL;
1598 goto done;
1600 } else if (asprintf(&header, "commit %s%s",
1601 id_str ? id_str : "........................................",
1602 ncommits_str) == -1) {
1603 err = got_error_from_errno("asprintf");
1604 header = NULL;
1605 goto done;
1607 err = format_line(&wline, &width, header, view->ncols, 0);
1608 if (err)
1609 goto done;
1611 werase(view->window);
1613 if (view_needs_focus_indication(view))
1614 wstandout(view->window);
1615 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1616 if (tc)
1617 wattr_on(view->window,
1618 COLOR_PAIR(tc->colorpair), NULL);
1619 waddwstr(view->window, wline);
1620 if (tc)
1621 wattr_off(view->window,
1622 COLOR_PAIR(tc->colorpair), NULL);
1623 while (width < view->ncols) {
1624 waddch(view->window, ' ');
1625 width++;
1627 if (view_needs_focus_indication(view))
1628 wstandend(view->window);
1629 free(wline);
1630 if (limit <= 1)
1631 goto done;
1633 /* Grow author column size if necessary. */
1634 entry = s->first_displayed_entry;
1635 ncommits = 0;
1636 while (entry) {
1637 char *author;
1638 wchar_t *wauthor;
1639 int width;
1640 if (ncommits >= limit - 1)
1641 break;
1642 author = strdup(got_object_commit_get_author(entry->commit));
1643 if (author == NULL) {
1644 err = got_error_from_errno("strdup");
1645 goto done;
1647 err = format_author(&wauthor, &width, author, COLS,
1648 date_display_cols);
1649 if (author_cols < width)
1650 author_cols = width;
1651 free(wauthor);
1652 free(author);
1653 ncommits++;
1654 entry = TAILQ_NEXT(entry, entry);
1657 entry = s->first_displayed_entry;
1658 s->last_displayed_entry = s->first_displayed_entry;
1659 ncommits = 0;
1660 while (entry) {
1661 if (ncommits >= limit - 1)
1662 break;
1663 if (ncommits == s->selected)
1664 wstandout(view->window);
1665 err = draw_commit(view, entry->commit, entry->id,
1666 date_display_cols, author_cols);
1667 if (ncommits == s->selected)
1668 wstandend(view->window);
1669 if (err)
1670 goto done;
1671 ncommits++;
1672 s->last_displayed_entry = entry;
1673 entry = TAILQ_NEXT(entry, entry);
1676 view_vborder(view);
1677 done:
1678 free(id_str);
1679 free(refs_str);
1680 free(ncommits_str);
1681 free(header);
1682 return err;
1685 static void
1686 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1688 struct commit_queue_entry *entry;
1689 int nscrolled = 0;
1691 entry = TAILQ_FIRST(&s->commits.head);
1692 if (s->first_displayed_entry == entry)
1693 return;
1695 entry = s->first_displayed_entry;
1696 while (entry && nscrolled < maxscroll) {
1697 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1698 if (entry) {
1699 s->first_displayed_entry = entry;
1700 nscrolled++;
1705 static const struct got_error *
1706 trigger_log_thread(struct tog_view *view, int wait)
1708 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1709 int errcode;
1711 halfdelay(1); /* fast refresh while loading commits */
1713 while (ta->commits_needed > 0) {
1714 if (ta->log_complete)
1715 break;
1717 /* Wake the log thread. */
1718 errcode = pthread_cond_signal(&ta->need_commits);
1719 if (errcode)
1720 return got_error_set_errno(errcode,
1721 "pthread_cond_signal");
1724 * The mutex will be released while the view loop waits
1725 * in wgetch(), at which time the log thread will run.
1727 if (!wait)
1728 break;
1730 /* Display progress update in log view. */
1731 show_log_view(view);
1732 update_panels();
1733 doupdate();
1735 /* Wait right here while next commit is being loaded. */
1736 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1737 if (errcode)
1738 return got_error_set_errno(errcode,
1739 "pthread_cond_wait");
1741 /* Display progress update in log view. */
1742 show_log_view(view);
1743 update_panels();
1744 doupdate();
1747 return NULL;
1750 static const struct got_error *
1751 log_scroll_down(struct tog_view *view, int maxscroll)
1753 struct tog_log_view_state *s = &view->state.log;
1754 const struct got_error *err = NULL;
1755 struct commit_queue_entry *pentry;
1756 int nscrolled = 0, ncommits_needed;
1758 if (s->last_displayed_entry == NULL)
1759 return NULL;
1761 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1762 if (s->commits.ncommits < ncommits_needed &&
1763 !s->thread_args.log_complete) {
1765 * Ask the log thread for required amount of commits.
1767 s->thread_args.commits_needed += maxscroll;
1768 err = trigger_log_thread(view, 1);
1769 if (err)
1770 return err;
1773 do {
1774 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1775 if (pentry == NULL)
1776 break;
1778 s->last_displayed_entry = pentry;
1780 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1781 if (pentry == NULL)
1782 break;
1783 s->first_displayed_entry = pentry;
1784 } while (++nscrolled < maxscroll);
1786 return err;
1789 static const struct got_error *
1790 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1791 struct got_commit_object *commit, struct got_object_id *commit_id,
1792 struct tog_view *log_view, struct got_repository *repo)
1794 const struct got_error *err;
1795 struct got_object_qid *parent_id;
1796 struct tog_view *diff_view;
1798 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1799 if (diff_view == NULL)
1800 return got_error_from_errno("view_open");
1802 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1803 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1804 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1805 if (err == NULL)
1806 *new_view = diff_view;
1807 return err;
1810 static const struct got_error *
1811 tree_view_visit_subtree(struct tog_tree_view_state *s,
1812 struct got_tree_object *subtree)
1814 struct tog_parent_tree *parent;
1816 parent = calloc(1, sizeof(*parent));
1817 if (parent == NULL)
1818 return got_error_from_errno("calloc");
1820 parent->tree = s->tree;
1821 parent->first_displayed_entry = s->first_displayed_entry;
1822 parent->selected_entry = s->selected_entry;
1823 parent->selected = s->selected;
1824 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1825 s->tree = subtree;
1826 s->selected = 0;
1827 s->first_displayed_entry = NULL;
1828 return NULL;
1831 static const struct got_error *
1832 tree_view_walk_path(struct tog_tree_view_state *s,
1833 struct got_object_id *commit_id, const char *path)
1835 const struct got_error *err = NULL;
1836 struct got_tree_object *tree = NULL;
1837 const char *p;
1838 char *slash, *subpath = NULL;
1840 /* Walk the path and open corresponding tree objects. */
1841 p = path;
1842 while (*p) {
1843 struct got_tree_entry *te;
1844 struct got_object_id *tree_id;
1845 char *te_name;
1847 while (p[0] == '/')
1848 p++;
1850 /* Ensure the correct subtree entry is selected. */
1851 slash = strchr(p, '/');
1852 if (slash == NULL)
1853 te_name = strdup(p);
1854 else
1855 te_name = strndup(p, slash - p);
1856 if (te_name == NULL) {
1857 err = got_error_from_errno("strndup");
1858 break;
1860 te = got_object_tree_find_entry(s->tree, te_name);
1861 if (te == NULL) {
1862 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1863 free(te_name);
1864 break;
1866 free(te_name);
1867 s->first_displayed_entry = s->selected_entry = te;
1869 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1870 break; /* jump to this file's entry */
1872 slash = strchr(p, '/');
1873 if (slash)
1874 subpath = strndup(path, slash - path);
1875 else
1876 subpath = strdup(path);
1877 if (subpath == NULL) {
1878 err = got_error_from_errno("strdup");
1879 break;
1882 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1883 subpath);
1884 if (err)
1885 break;
1887 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1888 free(tree_id);
1889 if (err)
1890 break;
1892 err = tree_view_visit_subtree(s, tree);
1893 if (err) {
1894 got_object_tree_close(tree);
1895 break;
1897 if (slash == NULL)
1898 break;
1899 free(subpath);
1900 subpath = NULL;
1901 p = slash;
1904 free(subpath);
1905 return err;
1908 static const struct got_error *
1909 browse_commit_tree(struct tog_view **new_view, int begin_x,
1910 struct commit_queue_entry *entry, const char *path,
1911 struct got_repository *repo)
1913 const struct got_error *err = NULL;
1914 struct got_tree_object *tree;
1915 struct tog_tree_view_state *s;
1916 struct tog_view *tree_view;
1918 err = got_object_open_as_tree(&tree, repo,
1919 got_object_commit_get_tree_id(entry->commit));
1920 if (err)
1921 return err;
1923 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1924 if (tree_view == NULL)
1925 return got_error_from_errno("view_open");
1927 err = open_tree_view(tree_view, tree, entry->id, repo);
1928 if (err) {
1929 got_object_tree_close(tree);
1930 return err;
1932 s = &tree_view->state.tree;
1934 *new_view = tree_view;
1936 if (got_path_is_root_dir(path))
1937 return NULL;
1939 return tree_view_walk_path(s, entry->id, path);
1942 static const struct got_error *
1943 block_signals_used_by_main_thread(void)
1945 sigset_t sigset;
1946 int errcode;
1948 if (sigemptyset(&sigset) == -1)
1949 return got_error_from_errno("sigemptyset");
1951 /* tog handles SIGWINCH and SIGCONT */
1952 if (sigaddset(&sigset, SIGWINCH) == -1)
1953 return got_error_from_errno("sigaddset");
1954 if (sigaddset(&sigset, SIGCONT) == -1)
1955 return got_error_from_errno("sigaddset");
1957 /* ncurses handles SIGTSTP */
1958 if (sigaddset(&sigset, SIGTSTP) == -1)
1959 return got_error_from_errno("sigaddset");
1961 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1962 if (errcode)
1963 return got_error_set_errno(errcode, "pthread_sigmask");
1965 return NULL;
1968 static void *
1969 log_thread(void *arg)
1971 const struct got_error *err = NULL;
1972 int errcode = 0;
1973 struct tog_log_thread_args *a = arg;
1974 int done = 0;
1976 err = block_signals_used_by_main_thread();
1977 if (err)
1978 return (void *)err;
1980 while (!done && !err && !tog_sigpipe_received) {
1981 err = queue_commits(a->graph, a->commits, 1, a->repo,
1982 a->in_repo_path, a->searching, a->search_next_done,
1983 a->regex);
1984 if (err) {
1985 if (err->code != GOT_ERR_ITER_COMPLETED)
1986 return (void *)err;
1987 err = NULL;
1988 done = 1;
1989 } else if (a->commits_needed > 0)
1990 a->commits_needed--;
1992 errcode = pthread_mutex_lock(&tog_mutex);
1993 if (errcode) {
1994 err = got_error_set_errno(errcode,
1995 "pthread_mutex_lock");
1996 break;
1997 } else if (*a->quit)
1998 done = 1;
1999 else if (*a->first_displayed_entry == NULL) {
2000 *a->first_displayed_entry =
2001 TAILQ_FIRST(&a->commits->head);
2002 *a->selected_entry = *a->first_displayed_entry;
2005 errcode = pthread_cond_signal(&a->commit_loaded);
2006 if (errcode) {
2007 err = got_error_set_errno(errcode,
2008 "pthread_cond_signal");
2009 pthread_mutex_unlock(&tog_mutex);
2010 break;
2013 if (done)
2014 a->commits_needed = 0;
2015 else {
2016 if (a->commits_needed == 0) {
2017 errcode = pthread_cond_wait(&a->need_commits,
2018 &tog_mutex);
2019 if (errcode)
2020 err = got_error_set_errno(errcode,
2021 "pthread_cond_wait");
2025 errcode = pthread_mutex_unlock(&tog_mutex);
2026 if (errcode && err == NULL)
2027 err = got_error_set_errno(errcode,
2028 "pthread_mutex_unlock");
2030 a->log_complete = 1;
2031 return (void *)err;
2034 static const struct got_error *
2035 stop_log_thread(struct tog_log_view_state *s)
2037 const struct got_error *err = NULL;
2038 int errcode;
2040 if (s->thread) {
2041 s->quit = 1;
2042 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2043 if (errcode)
2044 return got_error_set_errno(errcode,
2045 "pthread_cond_signal");
2046 errcode = pthread_mutex_unlock(&tog_mutex);
2047 if (errcode)
2048 return got_error_set_errno(errcode,
2049 "pthread_mutex_unlock");
2050 errcode = pthread_join(s->thread, (void **)&err);
2051 if (errcode)
2052 return got_error_set_errno(errcode, "pthread_join");
2053 errcode = pthread_mutex_lock(&tog_mutex);
2054 if (errcode)
2055 return got_error_set_errno(errcode,
2056 "pthread_mutex_lock");
2057 s->thread = NULL;
2060 if (s->thread_args.repo) {
2061 got_repo_close(s->thread_args.repo);
2062 s->thread_args.repo = NULL;
2065 if (s->thread_args.graph) {
2066 got_commit_graph_close(s->thread_args.graph);
2067 s->thread_args.graph = NULL;
2070 return err;
2073 static const struct got_error *
2074 close_log_view(struct tog_view *view)
2076 const struct got_error *err = NULL;
2077 struct tog_log_view_state *s = &view->state.log;
2078 int errcode;
2080 err = stop_log_thread(s);
2082 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2083 if (errcode && err == NULL)
2084 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2086 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2087 if (errcode && err == NULL)
2088 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2090 free_commits(&s->commits);
2091 free(s->in_repo_path);
2092 s->in_repo_path = NULL;
2093 free(s->start_id);
2094 s->start_id = NULL;
2095 got_ref_list_free(&s->refs);
2096 return err;
2099 static const struct got_error *
2100 search_start_log_view(struct tog_view *view)
2102 struct tog_log_view_state *s = &view->state.log;
2104 s->matched_entry = NULL;
2105 s->search_entry = NULL;
2106 return NULL;
2109 static const struct got_error *
2110 search_next_log_view(struct tog_view *view)
2112 const struct got_error *err = NULL;
2113 struct tog_log_view_state *s = &view->state.log;
2114 struct commit_queue_entry *entry;
2116 /* Display progress update in log view. */
2117 show_log_view(view);
2118 update_panels();
2119 doupdate();
2121 if (s->search_entry) {
2122 int errcode, ch;
2123 errcode = pthread_mutex_unlock(&tog_mutex);
2124 if (errcode)
2125 return got_error_set_errno(errcode,
2126 "pthread_mutex_unlock");
2127 ch = wgetch(view->window);
2128 errcode = pthread_mutex_lock(&tog_mutex);
2129 if (errcode)
2130 return got_error_set_errno(errcode,
2131 "pthread_mutex_lock");
2132 if (ch == KEY_BACKSPACE) {
2133 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2134 return NULL;
2136 if (view->searching == TOG_SEARCH_FORWARD)
2137 entry = TAILQ_NEXT(s->search_entry, entry);
2138 else
2139 entry = TAILQ_PREV(s->search_entry,
2140 commit_queue_head, entry);
2141 } else if (s->matched_entry) {
2142 if (view->searching == TOG_SEARCH_FORWARD)
2143 entry = TAILQ_NEXT(s->matched_entry, entry);
2144 else
2145 entry = TAILQ_PREV(s->matched_entry,
2146 commit_queue_head, entry);
2147 } else {
2148 if (view->searching == TOG_SEARCH_FORWARD)
2149 entry = TAILQ_FIRST(&s->commits.head);
2150 else
2151 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2154 while (1) {
2155 int have_match = 0;
2157 if (entry == NULL) {
2158 if (s->thread_args.log_complete ||
2159 view->searching == TOG_SEARCH_BACKWARD) {
2160 view->search_next_done =
2161 (s->matched_entry == NULL ?
2162 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2163 s->search_entry = NULL;
2164 return NULL;
2167 * Poke the log thread for more commits and return,
2168 * allowing the main loop to make progress. Search
2169 * will resume at s->search_entry once we come back.
2171 s->thread_args.commits_needed++;
2172 return trigger_log_thread(view, 0);
2175 err = match_commit(&have_match, entry->id, entry->commit,
2176 &view->regex);
2177 if (err)
2178 break;
2179 if (have_match) {
2180 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2181 s->matched_entry = entry;
2182 break;
2185 s->search_entry = entry;
2186 if (view->searching == TOG_SEARCH_FORWARD)
2187 entry = TAILQ_NEXT(entry, entry);
2188 else
2189 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2192 if (s->matched_entry) {
2193 int cur = s->selected_entry->idx;
2194 while (cur < s->matched_entry->idx) {
2195 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2196 if (err)
2197 return err;
2198 cur++;
2200 while (cur > s->matched_entry->idx) {
2201 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2202 if (err)
2203 return err;
2204 cur--;
2208 s->search_entry = NULL;
2210 return NULL;
2213 static const struct got_error *
2214 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2215 struct got_repository *repo, const char *head_ref_name,
2216 const char *in_repo_path, int log_branches)
2218 const struct got_error *err = NULL;
2219 struct tog_log_view_state *s = &view->state.log;
2220 struct got_repository *thread_repo = NULL;
2221 struct got_commit_graph *thread_graph = NULL;
2222 int errcode;
2224 SIMPLEQ_INIT(&s->refs);
2226 if (in_repo_path != s->in_repo_path) {
2227 free(s->in_repo_path);
2228 s->in_repo_path = strdup(in_repo_path);
2229 if (s->in_repo_path == NULL)
2230 return got_error_from_errno("strdup");
2233 /* The commit queue only contains commits being displayed. */
2234 TAILQ_INIT(&s->commits.head);
2235 s->commits.ncommits = 0;
2237 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2238 if (err)
2239 goto done;
2241 s->repo = repo;
2242 s->head_ref_name = head_ref_name;
2243 s->start_id = got_object_id_dup(start_id);
2244 if (s->start_id == NULL) {
2245 err = got_error_from_errno("got_object_id_dup");
2246 goto done;
2248 s->log_branches = log_branches;
2250 SIMPLEQ_INIT(&s->colors);
2251 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2252 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2253 get_color_value("TOG_COLOR_COMMIT"));
2254 if (err)
2255 goto done;
2256 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2257 get_color_value("TOG_COLOR_AUTHOR"));
2258 if (err) {
2259 free_colors(&s->colors);
2260 goto done;
2262 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2263 get_color_value("TOG_COLOR_DATE"));
2264 if (err) {
2265 free_colors(&s->colors);
2266 goto done;
2270 view->show = show_log_view;
2271 view->input = input_log_view;
2272 view->close = close_log_view;
2273 view->search_start = search_start_log_view;
2274 view->search_next = search_next_log_view;
2276 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2277 if (err)
2278 goto done;
2279 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2280 !s->log_branches);
2281 if (err)
2282 goto done;
2283 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2284 s->repo, NULL, NULL);
2285 if (err)
2286 goto done;
2288 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2289 if (errcode) {
2290 err = got_error_set_errno(errcode, "pthread_cond_init");
2291 goto done;
2293 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2294 if (errcode) {
2295 err = got_error_set_errno(errcode, "pthread_cond_init");
2296 goto done;
2299 s->thread_args.commits_needed = view->nlines;
2300 s->thread_args.graph = thread_graph;
2301 s->thread_args.commits = &s->commits;
2302 s->thread_args.in_repo_path = s->in_repo_path;
2303 s->thread_args.start_id = s->start_id;
2304 s->thread_args.repo = thread_repo;
2305 s->thread_args.log_complete = 0;
2306 s->thread_args.quit = &s->quit;
2307 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2308 s->thread_args.selected_entry = &s->selected_entry;
2309 s->thread_args.searching = &view->searching;
2310 s->thread_args.search_next_done = &view->search_next_done;
2311 s->thread_args.regex = &view->regex;
2312 done:
2313 if (err)
2314 close_log_view(view);
2315 return err;
2318 static const struct got_error *
2319 show_log_view(struct tog_view *view)
2321 const struct got_error *err;
2322 struct tog_log_view_state *s = &view->state.log;
2324 if (s->thread == NULL) {
2325 int errcode = pthread_create(&s->thread, NULL, log_thread,
2326 &s->thread_args);
2327 if (errcode)
2328 return got_error_set_errno(errcode, "pthread_create");
2329 if (s->thread_args.commits_needed > 0) {
2330 err = trigger_log_thread(view, 1);
2331 if (err)
2332 return err;
2336 return draw_commits(view);
2339 static const struct got_error *
2340 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2341 struct tog_view **focus_view, struct tog_view *view, int ch)
2343 const struct got_error *err = NULL;
2344 struct tog_log_view_state *s = &view->state.log;
2345 char *parent_path, *in_repo_path = NULL;
2346 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2347 struct tog_view *ref_view = NULL;
2348 int begin_x = 0;
2349 struct got_object_id *start_id;
2351 switch (ch) {
2352 case 'q':
2353 s->quit = 1;
2354 break;
2355 case 'k':
2356 case KEY_UP:
2357 case '<':
2358 case ',':
2359 if (s->first_displayed_entry == NULL)
2360 break;
2361 if (s->selected > 0)
2362 s->selected--;
2363 else
2364 log_scroll_up(s, 1);
2365 break;
2366 case KEY_PPAGE:
2367 case CTRL('b'):
2368 if (s->first_displayed_entry == NULL)
2369 break;
2370 if (TAILQ_FIRST(&s->commits.head) ==
2371 s->first_displayed_entry) {
2372 s->selected = 0;
2373 break;
2375 log_scroll_up(s, view->nlines - 1);
2376 break;
2377 case 'j':
2378 case KEY_DOWN:
2379 case '>':
2380 case '.':
2381 if (s->first_displayed_entry == NULL)
2382 break;
2383 if (s->selected < MIN(view->nlines - 2,
2384 s->commits.ncommits - 1)) {
2385 s->selected++;
2386 break;
2388 err = log_scroll_down(view, 1);
2389 break;
2390 case KEY_NPAGE:
2391 case CTRL('f'): {
2392 struct commit_queue_entry *first;
2393 first = s->first_displayed_entry;
2394 if (first == NULL)
2395 break;
2396 err = log_scroll_down(view, view->nlines - 1);
2397 if (err)
2398 break;
2399 if (first == s->first_displayed_entry &&
2400 s->selected < MIN(view->nlines - 2,
2401 s->commits.ncommits - 1)) {
2402 /* can't scroll further down */
2403 s->selected = MIN(view->nlines - 2,
2404 s->commits.ncommits - 1);
2406 err = NULL;
2407 break;
2409 case KEY_RESIZE:
2410 if (s->selected > view->nlines - 2)
2411 s->selected = view->nlines - 2;
2412 if (s->selected > s->commits.ncommits - 1)
2413 s->selected = s->commits.ncommits - 1;
2414 if (s->commits.ncommits < view->nlines - 1 &&
2415 !s->thread_args.log_complete) {
2416 s->thread_args.commits_needed += (view->nlines - 1) -
2417 s->commits.ncommits;
2418 err = trigger_log_thread(view, 1);
2420 break;
2421 case KEY_ENTER:
2422 case ' ':
2423 case '\r':
2424 if (s->selected_entry == NULL)
2425 break;
2426 if (view_is_parent_view(view))
2427 begin_x = view_split_begin_x(view->begin_x);
2428 err = open_diff_view_for_commit(&diff_view, begin_x,
2429 s->selected_entry->commit, s->selected_entry->id,
2430 view, s->repo);
2431 if (err)
2432 break;
2433 if (view_is_parent_view(view)) {
2434 err = view_close_child(view);
2435 if (err)
2436 return err;
2437 err = view_set_child(view, diff_view);
2438 if (err) {
2439 view_close(diff_view);
2440 break;
2442 *focus_view = diff_view;
2443 view->child_focussed = 1;
2444 } else
2445 *new_view = diff_view;
2446 break;
2447 case 't':
2448 if (s->selected_entry == NULL)
2449 break;
2450 if (view_is_parent_view(view))
2451 begin_x = view_split_begin_x(view->begin_x);
2452 err = browse_commit_tree(&tree_view, begin_x,
2453 s->selected_entry, s->in_repo_path, s->repo);
2454 if (err)
2455 break;
2456 if (view_is_parent_view(view)) {
2457 err = view_close_child(view);
2458 if (err)
2459 return err;
2460 err = view_set_child(view, tree_view);
2461 if (err) {
2462 view_close(tree_view);
2463 break;
2465 *focus_view = tree_view;
2466 view->child_focussed = 1;
2467 } else
2468 *new_view = tree_view;
2469 break;
2470 case KEY_BACKSPACE:
2471 if (got_path_cmp(s->in_repo_path, "/",
2472 strlen(s->in_repo_path), 1) == 0)
2473 break;
2474 err = got_path_dirname(&parent_path, s->in_repo_path);
2475 if (err)
2476 return err;
2477 err = stop_log_thread(s);
2478 if (err) {
2479 free(parent_path);
2480 return err;
2482 lv = view_open(view->nlines, view->ncols,
2483 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2484 if (lv == NULL) {
2485 free(parent_path);
2486 return got_error_from_errno("view_open");
2488 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2489 parent_path, s->log_branches);
2490 free(parent_path);
2491 if (err)
2492 return err;;
2493 if (view_is_parent_view(view))
2494 *new_view = lv;
2495 else {
2496 view_set_child(view->parent, lv);
2497 *focus_view = lv;
2499 break;
2500 case CTRL('l'):
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 = got_repo_match_object_id(&start_id, NULL,
2509 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2510 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2511 if (err) {
2512 view_close(lv);
2513 return err;
2515 in_repo_path = strdup(s->in_repo_path);
2516 if (in_repo_path == NULL) {
2517 free(start_id);
2518 view_close(lv);
2519 return got_error_from_errno("strdup");
2521 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2522 in_repo_path, s->log_branches);
2523 if (err) {
2524 free(start_id);
2525 view_close(lv);
2526 return err;;
2528 *dead_view = view;
2529 *new_view = lv;
2530 break;
2531 case 'B':
2532 s->log_branches = !s->log_branches;
2533 err = stop_log_thread(s);
2534 if (err)
2535 return err;
2536 lv = view_open(view->nlines, view->ncols,
2537 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2538 if (lv == NULL)
2539 return got_error_from_errno("view_open");
2540 err = open_log_view(lv, s->start_id, s->repo,
2541 s->head_ref_name, s->in_repo_path, s->log_branches);
2542 if (err) {
2543 view_close(lv);
2544 return err;;
2546 *dead_view = view;
2547 *new_view = lv;
2548 break;
2549 case 'r':
2550 if (view_is_parent_view(view))
2551 begin_x = view_split_begin_x(view->begin_x);
2552 ref_view = view_open(view->nlines, view->ncols,
2553 view->begin_y, begin_x, TOG_VIEW_REF);
2554 if (ref_view == NULL)
2555 return got_error_from_errno("view_open");
2556 err = open_ref_view(ref_view, s->repo);
2557 if (err) {
2558 view_close(ref_view);
2559 return err;
2561 if (view_is_parent_view(view)) {
2562 err = view_close_child(view);
2563 if (err)
2564 return err;
2565 err = view_set_child(view, ref_view);
2566 if (err) {
2567 view_close(ref_view);
2568 break;
2570 *focus_view = ref_view;
2571 view->child_focussed = 1;
2572 } else
2573 *new_view = ref_view;
2574 break;
2575 default:
2576 break;
2579 return err;
2582 static const struct got_error *
2583 apply_unveil(const char *repo_path, const char *worktree_path)
2585 const struct got_error *error;
2587 #ifdef PROFILE
2588 if (unveil("gmon.out", "rwc") != 0)
2589 return got_error_from_errno2("unveil", "gmon.out");
2590 #endif
2591 if (repo_path && unveil(repo_path, "r") != 0)
2592 return got_error_from_errno2("unveil", repo_path);
2594 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2595 return got_error_from_errno2("unveil", worktree_path);
2597 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2598 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2600 error = got_privsep_unveil_exec_helpers();
2601 if (error != NULL)
2602 return error;
2604 if (unveil(NULL, NULL) != 0)
2605 return got_error_from_errno("unveil");
2607 return NULL;
2610 static void
2611 init_curses(void)
2613 initscr();
2614 cbreak();
2615 halfdelay(1); /* Do fast refresh while initial view is loading. */
2616 noecho();
2617 nonl();
2618 intrflush(stdscr, FALSE);
2619 keypad(stdscr, TRUE);
2620 curs_set(0);
2621 if (getenv("TOG_COLORS") != NULL) {
2622 start_color();
2623 use_default_colors();
2625 signal(SIGWINCH, tog_sigwinch);
2626 signal(SIGPIPE, tog_sigpipe);
2627 signal(SIGCONT, tog_sigcont);
2630 static const struct got_error *
2631 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2632 struct got_repository *repo, struct got_worktree *worktree)
2634 const struct got_error *err = NULL;
2636 if (argc == 0) {
2637 *in_repo_path = strdup("/");
2638 if (*in_repo_path == NULL)
2639 return got_error_from_errno("strdup");
2640 return NULL;
2643 if (worktree) {
2644 const char *prefix = got_worktree_get_path_prefix(worktree);
2645 char *p;
2647 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2648 if (err)
2649 return err;
2650 if (asprintf(in_repo_path, "%s%s%s", prefix,
2651 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2652 p) == -1) {
2653 err = got_error_from_errno("asprintf");
2654 *in_repo_path = NULL;
2656 free(p);
2657 } else
2658 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2660 return err;
2663 static const struct got_error *
2664 cmd_log(int argc, char *argv[])
2666 const struct got_error *error;
2667 struct got_repository *repo = NULL;
2668 struct got_worktree *worktree = NULL;
2669 struct got_object_id *start_id = NULL;
2670 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2671 char *start_commit = NULL, *head_ref_name = NULL;
2672 int ch, log_branches = 0;
2673 struct tog_view *view;
2675 #ifndef PROFILE
2676 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2677 NULL) == -1)
2678 err(1, "pledge");
2679 #endif
2681 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2682 switch (ch) {
2683 case 'b':
2684 log_branches = 1;
2685 break;
2686 case 'c':
2687 start_commit = optarg;
2688 break;
2689 case 'r':
2690 repo_path = realpath(optarg, NULL);
2691 if (repo_path == NULL)
2692 return got_error_from_errno2("realpath",
2693 optarg);
2694 break;
2695 default:
2696 usage_log();
2697 /* NOTREACHED */
2701 argc -= optind;
2702 argv += optind;
2704 if (argc > 1)
2705 usage_log();
2707 cwd = getcwd(NULL, 0);
2708 if (cwd == NULL)
2709 return got_error_from_errno("getcwd");
2711 error = got_worktree_open(&worktree, cwd);
2712 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2713 goto done;
2715 if (repo_path == NULL) {
2716 if (worktree)
2717 repo_path =
2718 strdup(got_worktree_get_repo_path(worktree));
2719 else
2720 repo_path = strdup(cwd);
2722 if (repo_path == NULL) {
2723 error = got_error_from_errno("strdup");
2724 goto done;
2727 error = got_repo_open(&repo, repo_path, NULL);
2728 if (error != NULL)
2729 goto done;
2731 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2732 repo, worktree);
2733 if (error)
2734 goto done;
2736 init_curses();
2738 error = apply_unveil(got_repo_get_path(repo),
2739 worktree ? got_worktree_get_root_path(worktree) : NULL);
2740 if (error)
2741 goto done;
2743 if (start_commit == NULL)
2744 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2745 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2746 GOT_OBJ_TYPE_COMMIT, 1, repo);
2747 else
2748 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2749 GOT_OBJ_TYPE_COMMIT, 1, repo);
2750 if (error != NULL)
2751 goto done;
2753 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2754 if (view == NULL) {
2755 error = got_error_from_errno("view_open");
2756 goto done;
2758 if (worktree) {
2759 head_ref_name = strdup(
2760 got_worktree_get_head_ref_name(worktree));
2761 if (head_ref_name == NULL) {
2762 error = got_error_from_errno("strdup");
2763 goto done;
2766 error = open_log_view(view, start_id, repo, head_ref_name,
2767 in_repo_path, log_branches);
2768 if (error)
2769 goto done;
2770 if (worktree) {
2771 /* Release work tree lock. */
2772 got_worktree_close(worktree);
2773 worktree = NULL;
2775 error = view_loop(view);
2776 done:
2777 free(in_repo_path);
2778 free(repo_path);
2779 free(cwd);
2780 free(start_id);
2781 free(head_ref_name);
2782 if (repo)
2783 got_repo_close(repo);
2784 if (worktree)
2785 got_worktree_close(worktree);
2786 return error;
2789 __dead static void
2790 usage_diff(void)
2792 endwin();
2793 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2794 "[-w] object1 object2\n", getprogname());
2795 exit(1);
2798 static char *
2799 parse_next_line(FILE *f, size_t *len)
2801 char *line;
2802 size_t linelen;
2803 size_t lineno;
2804 const char delim[3] = { '\0', '\0', '\0'};
2806 line = fparseln(f, &linelen, &lineno, delim, 0);
2807 if (len)
2808 *len = linelen;
2809 return line;
2812 static int
2813 match_line(const char *line, regex_t *regex, size_t nmatch,
2814 regmatch_t *regmatch)
2816 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2819 struct tog_color *
2820 match_color(struct tog_colors *colors, const char *line)
2822 struct tog_color *tc = NULL;
2824 SIMPLEQ_FOREACH(tc, colors, entry) {
2825 if (match_line(line, &tc->regex, 0, NULL))
2826 return tc;
2829 return NULL;
2832 static const struct got_error *
2833 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2834 WINDOW *window, regmatch_t *regmatch)
2836 const struct got_error *err = NULL;
2837 wchar_t *wline;
2838 int width;
2839 char *s;
2841 *wtotal = 0;
2843 s = strndup(line, regmatch->rm_so);
2844 if (s == NULL)
2845 return got_error_from_errno("strndup");
2847 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2848 if (err) {
2849 free(s);
2850 return err;
2852 waddwstr(window, wline);
2853 free(wline);
2854 free(s);
2855 wlimit -= width;
2856 *wtotal += width;
2858 if (wlimit > 0) {
2859 s = strndup(line + regmatch->rm_so,
2860 regmatch->rm_eo - regmatch->rm_so);
2861 if (s == NULL) {
2862 err = got_error_from_errno("strndup");
2863 free(s);
2864 return err;
2866 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2867 if (err) {
2868 free(s);
2869 return err;
2871 wattr_on(window, A_STANDOUT, NULL);
2872 waddwstr(window, wline);
2873 wattr_off(window, A_STANDOUT, NULL);
2874 free(wline);
2875 free(s);
2876 wlimit -= width;
2877 *wtotal += width;
2880 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2881 err = format_line(&wline, &width,
2882 line + regmatch->rm_eo, wlimit, col_tab_align);
2883 if (err)
2884 return err;
2885 waddwstr(window, wline);
2886 free(wline);
2887 *wtotal += width;
2890 return NULL;
2893 static const struct got_error *
2894 draw_file(struct tog_view *view, const char *header)
2896 struct tog_diff_view_state *s = &view->state.diff;
2897 regmatch_t *regmatch = &view->regmatch;
2898 const struct got_error *err;
2899 int nprinted = 0;
2900 char *line;
2901 struct tog_color *tc;
2902 size_t len;
2903 wchar_t *wline;
2904 int width;
2905 int max_lines = view->nlines;
2906 int nlines = s->nlines;
2907 off_t line_offset;
2909 line_offset = s->line_offsets[s->first_displayed_line - 1];
2910 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2911 return got_error_from_errno("fseek");
2913 werase(view->window);
2915 if (header) {
2916 if (asprintf(&line, "[%d/%d] %s",
2917 s->first_displayed_line - 1 + s->selected_line, nlines,
2918 header) == -1)
2919 return got_error_from_errno("asprintf");
2920 err = format_line(&wline, &width, line, view->ncols, 0);
2921 free(line);
2922 if (err)
2923 return err;
2925 if (view_needs_focus_indication(view))
2926 wstandout(view->window);
2927 waddwstr(view->window, wline);
2928 free(wline);
2929 wline = NULL;
2930 if (view_needs_focus_indication(view))
2931 wstandend(view->window);
2932 if (width <= view->ncols - 1)
2933 waddch(view->window, '\n');
2935 if (max_lines <= 1)
2936 return NULL;
2937 max_lines--;
2940 s->eof = 0;
2941 while (max_lines > 0 && nprinted < max_lines) {
2942 line = parse_next_line(s->f, &len);
2943 if (line == NULL) {
2944 s->eof = 1;
2945 break;
2948 tc = match_color(&s->colors, line);
2949 if (tc)
2950 wattr_on(view->window,
2951 COLOR_PAIR(tc->colorpair), NULL);
2952 if (s->first_displayed_line + nprinted == s->matched_line &&
2953 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2954 err = add_matched_line(&width, line, view->ncols, 0,
2955 view->window, regmatch);
2956 if (err) {
2957 free(line);
2958 return err;
2960 } else {
2961 err = format_line(&wline, &width, line, view->ncols, 0);
2962 if (err) {
2963 free(line);
2964 return err;
2966 waddwstr(view->window, wline);
2967 free(wline);
2968 wline = NULL;
2970 if (tc)
2971 wattr_off(view->window,
2972 COLOR_PAIR(tc->colorpair), NULL);
2973 if (width <= view->ncols - 1)
2974 waddch(view->window, '\n');
2975 nprinted++;
2976 free(line);
2978 if (nprinted >= 1)
2979 s->last_displayed_line = s->first_displayed_line +
2980 (nprinted - 1);
2981 else
2982 s->last_displayed_line = s->first_displayed_line;
2984 view_vborder(view);
2986 if (s->eof) {
2987 while (nprinted < view->nlines) {
2988 waddch(view->window, '\n');
2989 nprinted++;
2992 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2993 if (err) {
2994 return err;
2997 wstandout(view->window);
2998 waddwstr(view->window, wline);
2999 free(wline);
3000 wline = NULL;
3001 wstandend(view->window);
3004 return NULL;
3007 static char *
3008 get_datestr(time_t *time, char *datebuf)
3010 struct tm mytm, *tm;
3011 char *p, *s;
3013 tm = gmtime_r(time, &mytm);
3014 if (tm == NULL)
3015 return NULL;
3016 s = asctime_r(tm, datebuf);
3017 if (s == NULL)
3018 return NULL;
3019 p = strchr(s, '\n');
3020 if (p)
3021 *p = '\0';
3022 return s;
3025 static const struct got_error *
3026 get_changed_paths(struct got_pathlist_head *paths,
3027 struct got_commit_object *commit, struct got_repository *repo)
3029 const struct got_error *err = NULL;
3030 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3031 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3032 struct got_object_qid *qid;
3034 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3035 if (qid != NULL) {
3036 struct got_commit_object *pcommit;
3037 err = got_object_open_as_commit(&pcommit, repo,
3038 qid->id);
3039 if (err)
3040 return err;
3042 tree_id1 = got_object_commit_get_tree_id(pcommit);
3043 got_object_commit_close(pcommit);
3047 if (tree_id1) {
3048 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3049 if (err)
3050 goto done;
3053 tree_id2 = got_object_commit_get_tree_id(commit);
3054 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3055 if (err)
3056 goto done;
3058 err = got_diff_tree(tree1, tree2, "", "", repo,
3059 got_diff_tree_collect_changed_paths, paths, 0);
3060 done:
3061 if (tree1)
3062 got_object_tree_close(tree1);
3063 if (tree2)
3064 got_object_tree_close(tree2);
3065 return err;
3068 static const struct got_error *
3069 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3071 off_t *p;
3073 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3074 if (p == NULL)
3075 return got_error_from_errno("reallocarray");
3076 *line_offsets = p;
3077 (*line_offsets)[*nlines] = off;
3078 (*nlines)++;
3079 return NULL;
3082 static const struct got_error *
3083 write_commit_info(off_t **line_offsets, size_t *nlines,
3084 struct got_object_id *commit_id, struct got_reflist_head *refs,
3085 struct got_repository *repo, FILE *outfile)
3087 const struct got_error *err = NULL;
3088 char datebuf[26], *datestr;
3089 struct got_commit_object *commit;
3090 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3091 time_t committer_time;
3092 const char *author, *committer;
3093 char *refs_str = NULL;
3094 struct got_pathlist_head changed_paths;
3095 struct got_pathlist_entry *pe;
3096 off_t outoff = 0;
3097 int n;
3099 TAILQ_INIT(&changed_paths);
3101 if (refs) {
3102 err = build_refs_str(&refs_str, refs, commit_id, repo);
3103 if (err)
3104 return err;
3107 err = got_object_open_as_commit(&commit, repo, commit_id);
3108 if (err)
3109 return err;
3111 err = got_object_id_str(&id_str, commit_id);
3112 if (err) {
3113 err = got_error_from_errno("got_object_id_str");
3114 goto done;
3117 err = add_line_offset(line_offsets, nlines, 0);
3118 if (err)
3119 goto done;
3121 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3122 refs_str ? refs_str : "", refs_str ? ")" : "");
3123 if (n < 0) {
3124 err = got_error_from_errno("fprintf");
3125 goto done;
3127 outoff += n;
3128 err = add_line_offset(line_offsets, nlines, outoff);
3129 if (err)
3130 goto done;
3132 n = fprintf(outfile, "from: %s\n",
3133 got_object_commit_get_author(commit));
3134 if (n < 0) {
3135 err = got_error_from_errno("fprintf");
3136 goto done;
3138 outoff += n;
3139 err = add_line_offset(line_offsets, nlines, outoff);
3140 if (err)
3141 goto done;
3143 committer_time = got_object_commit_get_committer_time(commit);
3144 datestr = get_datestr(&committer_time, datebuf);
3145 if (datestr) {
3146 n = fprintf(outfile, "date: %s UTC\n", datestr);
3147 if (n < 0) {
3148 err = got_error_from_errno("fprintf");
3149 goto done;
3151 outoff += n;
3152 err = add_line_offset(line_offsets, nlines, outoff);
3153 if (err)
3154 goto done;
3156 author = got_object_commit_get_author(commit);
3157 committer = got_object_commit_get_committer(commit);
3158 if (strcmp(author, committer) != 0) {
3159 n = fprintf(outfile, "via: %s\n", committer);
3160 if (n < 0) {
3161 err = got_error_from_errno("fprintf");
3162 goto done;
3164 outoff += n;
3165 err = add_line_offset(line_offsets, nlines, outoff);
3166 if (err)
3167 goto done;
3169 err = got_object_commit_get_logmsg(&logmsg, commit);
3170 if (err)
3171 goto done;
3172 s = logmsg;
3173 while ((line = strsep(&s, "\n")) != NULL) {
3174 n = fprintf(outfile, "%s\n", line);
3175 if (n < 0) {
3176 err = got_error_from_errno("fprintf");
3177 goto done;
3179 outoff += n;
3180 err = add_line_offset(line_offsets, nlines, outoff);
3181 if (err)
3182 goto done;
3185 err = get_changed_paths(&changed_paths, commit, repo);
3186 if (err)
3187 goto done;
3188 TAILQ_FOREACH(pe, &changed_paths, entry) {
3189 struct got_diff_changed_path *cp = pe->data;
3190 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3191 if (n < 0) {
3192 err = got_error_from_errno("fprintf");
3193 goto done;
3195 outoff += n;
3196 err = add_line_offset(line_offsets, nlines, outoff);
3197 if (err)
3198 goto done;
3199 free((char *)pe->path);
3200 free(pe->data);
3203 fputc('\n', outfile);
3204 outoff++;
3205 err = add_line_offset(line_offsets, nlines, outoff);
3206 done:
3207 got_pathlist_free(&changed_paths);
3208 free(id_str);
3209 free(logmsg);
3210 free(refs_str);
3211 got_object_commit_close(commit);
3212 if (err) {
3213 free(*line_offsets);
3214 *line_offsets = NULL;
3215 *nlines = 0;
3217 return err;
3220 static const struct got_error *
3221 create_diff(struct tog_diff_view_state *s)
3223 const struct got_error *err = NULL;
3224 FILE *f = NULL;
3225 int obj_type;
3227 free(s->line_offsets);
3228 s->line_offsets = malloc(sizeof(off_t));
3229 if (s->line_offsets == NULL)
3230 return got_error_from_errno("malloc");
3231 s->nlines = 0;
3233 f = got_opentemp();
3234 if (f == NULL) {
3235 err = got_error_from_errno("got_opentemp");
3236 goto done;
3238 if (s->f && fclose(s->f) != 0) {
3239 err = got_error_from_errno("fclose");
3240 goto done;
3242 s->f = f;
3244 if (s->id1)
3245 err = got_object_get_type(&obj_type, s->repo, s->id1);
3246 else
3247 err = got_object_get_type(&obj_type, s->repo, s->id2);
3248 if (err)
3249 goto done;
3251 switch (obj_type) {
3252 case GOT_OBJ_TYPE_BLOB:
3253 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3254 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3255 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3256 break;
3257 case GOT_OBJ_TYPE_TREE:
3258 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3259 s->id1, s->id2, "", "", s->diff_context,
3260 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3261 break;
3262 case GOT_OBJ_TYPE_COMMIT: {
3263 const struct got_object_id_queue *parent_ids;
3264 struct got_object_qid *pid;
3265 struct got_commit_object *commit2;
3267 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3268 if (err)
3269 goto done;
3270 /* Show commit info if we're diffing to a parent/root commit. */
3271 if (s->id1 == NULL) {
3272 err = write_commit_info(&s->line_offsets, &s->nlines,
3273 s->id2, &s->refs, s->repo, s->f);
3274 if (err)
3275 goto done;
3276 } else {
3277 parent_ids = got_object_commit_get_parent_ids(commit2);
3278 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3279 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3280 err = write_commit_info(
3281 &s->line_offsets, &s->nlines,
3282 s->id2, &s->refs, s->repo, s->f);
3283 if (err)
3284 goto done;
3285 break;
3289 got_object_commit_close(commit2);
3291 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3292 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3293 s->force_text_diff, s->repo, s->f);
3294 break;
3296 default:
3297 err = got_error(GOT_ERR_OBJ_TYPE);
3298 break;
3300 if (err)
3301 goto done;
3302 done:
3303 if (s->f && fflush(s->f) != 0 && err == NULL)
3304 err = got_error_from_errno("fflush");
3305 return err;
3308 static void
3309 diff_view_indicate_progress(struct tog_view *view)
3311 mvwaddstr(view->window, 0, 0, "diffing...");
3312 update_panels();
3313 doupdate();
3316 static const struct got_error *
3317 search_start_diff_view(struct tog_view *view)
3319 struct tog_diff_view_state *s = &view->state.diff;
3321 s->matched_line = 0;
3322 return NULL;
3325 static const struct got_error *
3326 search_next_diff_view(struct tog_view *view)
3328 struct tog_diff_view_state *s = &view->state.diff;
3329 int lineno;
3331 if (!view->searching) {
3332 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3333 return NULL;
3336 if (s->matched_line) {
3337 if (view->searching == TOG_SEARCH_FORWARD)
3338 lineno = s->matched_line + 1;
3339 else
3340 lineno = s->matched_line - 1;
3341 } else {
3342 if (view->searching == TOG_SEARCH_FORWARD)
3343 lineno = 1;
3344 else
3345 lineno = s->nlines;
3348 while (1) {
3349 char *line = NULL;
3350 off_t offset;
3351 size_t len;
3353 if (lineno <= 0 || lineno > s->nlines) {
3354 if (s->matched_line == 0) {
3355 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3356 free(line);
3357 break;
3360 if (view->searching == TOG_SEARCH_FORWARD)
3361 lineno = 1;
3362 else
3363 lineno = s->nlines;
3366 offset = s->line_offsets[lineno - 1];
3367 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3368 free(line);
3369 return got_error_from_errno("fseeko");
3371 free(line);
3372 line = parse_next_line(s->f, &len);
3373 if (line &&
3374 match_line(line, &view->regex, 1, &view->regmatch)) {
3375 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3376 s->matched_line = lineno;
3377 free(line);
3378 break;
3380 free(line);
3381 if (view->searching == TOG_SEARCH_FORWARD)
3382 lineno++;
3383 else
3384 lineno--;
3387 if (s->matched_line) {
3388 s->first_displayed_line = s->matched_line;
3389 s->selected_line = 1;
3392 return NULL;
3395 static const struct got_error *
3396 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3397 struct got_object_id *id2, const char *label1, const char *label2,
3398 int diff_context, int ignore_whitespace, int force_text_diff,
3399 struct tog_view *log_view, struct got_repository *repo)
3401 const struct got_error *err;
3402 struct tog_diff_view_state *s = &view->state.diff;
3404 SIMPLEQ_INIT(&s->refs);
3406 if (id1 != NULL && id2 != NULL) {
3407 int type1, type2;
3408 err = got_object_get_type(&type1, repo, id1);
3409 if (err)
3410 return err;
3411 err = got_object_get_type(&type2, repo, id2);
3412 if (err)
3413 return err;
3415 if (type1 != type2)
3416 return got_error(GOT_ERR_OBJ_TYPE);
3418 s->first_displayed_line = 1;
3419 s->last_displayed_line = view->nlines;
3420 s->selected_line = 1;
3421 s->repo = repo;
3422 s->id1 = id1;
3423 s->id2 = id2;
3424 s->label1 = label1;
3425 s->label2 = label2;
3427 if (id1) {
3428 s->id1 = got_object_id_dup(id1);
3429 if (s->id1 == NULL)
3430 return got_error_from_errno("got_object_id_dup");
3431 } else
3432 s->id1 = NULL;
3434 s->id2 = got_object_id_dup(id2);
3435 if (s->id2 == NULL) {
3436 free(s->id1);
3437 s->id1 = NULL;
3438 return got_error_from_errno("got_object_id_dup");
3440 s->f = NULL;
3441 s->first_displayed_line = 1;
3442 s->last_displayed_line = view->nlines;
3443 s->diff_context = diff_context;
3444 s->ignore_whitespace = ignore_whitespace;
3445 s->force_text_diff = force_text_diff;
3446 s->log_view = log_view;
3447 s->repo = repo;
3449 SIMPLEQ_INIT(&s->colors);
3450 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3451 err = add_color(&s->colors,
3452 "^-", TOG_COLOR_DIFF_MINUS,
3453 get_color_value("TOG_COLOR_DIFF_MINUS"));
3454 if (err)
3455 return err;
3456 err = add_color(&s->colors, "^\\+",
3457 TOG_COLOR_DIFF_PLUS,
3458 get_color_value("TOG_COLOR_DIFF_PLUS"));
3459 if (err) {
3460 free_colors(&s->colors);
3461 return err;
3463 err = add_color(&s->colors,
3464 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3465 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3466 if (err) {
3467 free_colors(&s->colors);
3468 return err;
3471 err = add_color(&s->colors,
3472 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3473 TOG_COLOR_DIFF_META,
3474 get_color_value("TOG_COLOR_DIFF_META"));
3475 if (err) {
3476 free_colors(&s->colors);
3477 return err;
3480 err = add_color(&s->colors,
3481 "^(from|via): ", TOG_COLOR_AUTHOR,
3482 get_color_value("TOG_COLOR_AUTHOR"));
3483 if (err) {
3484 free_colors(&s->colors);
3485 return err;
3488 err = add_color(&s->colors,
3489 "^date: ", TOG_COLOR_DATE,
3490 get_color_value("TOG_COLOR_DATE"));
3491 if (err) {
3492 free_colors(&s->colors);
3493 return err;
3497 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3498 if (err) {
3499 free(s->id1);
3500 s->id1 = NULL;
3501 free(s->id2);
3502 s->id2 = NULL;
3503 free_colors(&s->colors);
3504 return err;
3507 if (log_view && view_is_splitscreen(view))
3508 show_log_view(log_view); /* draw vborder */
3509 diff_view_indicate_progress(view);
3511 s->line_offsets = NULL;
3512 s->nlines = 0;
3513 err = create_diff(s);
3514 if (err) {
3515 free(s->id1);
3516 s->id1 = NULL;
3517 free(s->id2);
3518 s->id2 = NULL;
3519 free_colors(&s->colors);
3520 got_ref_list_free(&s->refs);
3521 return err;
3524 view->show = show_diff_view;
3525 view->input = input_diff_view;
3526 view->close = close_diff_view;
3527 view->search_start = search_start_diff_view;
3528 view->search_next = search_next_diff_view;
3530 return NULL;
3533 static const struct got_error *
3534 close_diff_view(struct tog_view *view)
3536 const struct got_error *err = NULL;
3537 struct tog_diff_view_state *s = &view->state.diff;
3539 free(s->id1);
3540 s->id1 = NULL;
3541 free(s->id2);
3542 s->id2 = NULL;
3543 if (s->f && fclose(s->f) == EOF)
3544 err = got_error_from_errno("fclose");
3545 free_colors(&s->colors);
3546 free(s->line_offsets);
3547 s->line_offsets = NULL;
3548 s->nlines = 0;
3549 got_ref_list_free(&s->refs);
3550 return err;
3553 static const struct got_error *
3554 show_diff_view(struct tog_view *view)
3556 const struct got_error *err;
3557 struct tog_diff_view_state *s = &view->state.diff;
3558 char *id_str1 = NULL, *id_str2, *header;
3559 const char *label1, *label2;
3561 if (s->id1) {
3562 err = got_object_id_str(&id_str1, s->id1);
3563 if (err)
3564 return err;
3565 label1 = s->label1 ? : id_str1;
3566 } else
3567 label1 = "/dev/null";
3569 err = got_object_id_str(&id_str2, s->id2);
3570 if (err)
3571 return err;
3572 label2 = s->label2 ? : id_str2;
3574 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3575 err = got_error_from_errno("asprintf");
3576 free(id_str1);
3577 free(id_str2);
3578 return err;
3580 free(id_str1);
3581 free(id_str2);
3583 return draw_file(view, header);
3586 static const struct got_error *
3587 set_selected_commit(struct tog_diff_view_state *s,
3588 struct commit_queue_entry *entry)
3590 const struct got_error *err;
3591 const struct got_object_id_queue *parent_ids;
3592 struct got_commit_object *selected_commit;
3593 struct got_object_qid *pid;
3595 free(s->id2);
3596 s->id2 = got_object_id_dup(entry->id);
3597 if (s->id2 == NULL)
3598 return got_error_from_errno("got_object_id_dup");
3600 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3601 if (err)
3602 return err;
3603 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3604 free(s->id1);
3605 pid = SIMPLEQ_FIRST(parent_ids);
3606 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3607 got_object_commit_close(selected_commit);
3608 return NULL;
3611 static const struct got_error *
3612 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3613 struct tog_view **focus_view, struct tog_view *view, int ch)
3615 const struct got_error *err = NULL;
3616 struct tog_diff_view_state *s = &view->state.diff;
3617 struct tog_log_view_state *ls;
3618 struct commit_queue_entry *entry;
3619 int i;
3621 switch (ch) {
3622 case 'a':
3623 case 'w':
3624 if (ch == 'a')
3625 s->force_text_diff = !s->force_text_diff;
3626 if (ch == 'w')
3627 s->ignore_whitespace = !s->ignore_whitespace;
3628 wclear(view->window);
3629 s->first_displayed_line = 1;
3630 s->last_displayed_line = view->nlines;
3631 diff_view_indicate_progress(view);
3632 err = create_diff(s);
3633 break;
3634 case 'k':
3635 case KEY_UP:
3636 if (s->first_displayed_line > 1)
3637 s->first_displayed_line--;
3638 break;
3639 case KEY_PPAGE:
3640 case CTRL('b'):
3641 if (s->first_displayed_line == 1)
3642 break;
3643 i = 0;
3644 while (i++ < view->nlines - 1 &&
3645 s->first_displayed_line > 1)
3646 s->first_displayed_line--;
3647 break;
3648 case 'j':
3649 case KEY_DOWN:
3650 if (!s->eof)
3651 s->first_displayed_line++;
3652 break;
3653 case KEY_NPAGE:
3654 case CTRL('f'):
3655 case ' ':
3656 if (s->eof)
3657 break;
3658 i = 0;
3659 while (!s->eof && i++ < view->nlines - 1) {
3660 char *line;
3661 line = parse_next_line(s->f, NULL);
3662 s->first_displayed_line++;
3663 if (line == NULL)
3664 break;
3666 break;
3667 case '[':
3668 if (s->diff_context > 0) {
3669 s->diff_context--;
3670 diff_view_indicate_progress(view);
3671 err = create_diff(s);
3672 if (s->first_displayed_line + view->nlines - 1 >
3673 s->nlines) {
3674 s->first_displayed_line = 1;
3675 s->last_displayed_line = view->nlines;
3678 break;
3679 case ']':
3680 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3681 s->diff_context++;
3682 diff_view_indicate_progress(view);
3683 err = create_diff(s);
3685 break;
3686 case '<':
3687 case ',':
3688 if (s->log_view == NULL)
3689 break;
3690 ls = &s->log_view->state.log;
3691 entry = TAILQ_PREV(ls->selected_entry,
3692 commit_queue_head, entry);
3693 if (entry == NULL)
3694 break;
3696 err = input_log_view(NULL, NULL, NULL, s->log_view,
3697 KEY_UP);
3698 if (err)
3699 break;
3701 err = set_selected_commit(s, entry);
3702 if (err)
3703 break;
3705 s->first_displayed_line = 1;
3706 s->last_displayed_line = view->nlines;
3708 diff_view_indicate_progress(view);
3709 err = create_diff(s);
3710 break;
3711 case '>':
3712 case '.':
3713 if (s->log_view == NULL)
3714 break;
3715 ls = &s->log_view->state.log;
3717 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3718 ls->thread_args.commits_needed++;
3719 err = trigger_log_thread(s->log_view, 1);
3720 if (err)
3721 break;
3723 err = input_log_view(NULL, NULL, NULL, s->log_view,
3724 KEY_DOWN);
3725 if (err)
3726 break;
3728 entry = TAILQ_NEXT(ls->selected_entry, entry);
3729 if (entry == NULL)
3730 break;
3732 err = set_selected_commit(s, entry);
3733 if (err)
3734 break;
3736 s->first_displayed_line = 1;
3737 s->last_displayed_line = view->nlines;
3739 diff_view_indicate_progress(view);
3740 err = create_diff(s);
3741 break;
3742 default:
3743 break;
3746 return err;
3749 static const struct got_error *
3750 cmd_diff(int argc, char *argv[])
3752 const struct got_error *error = NULL;
3753 struct got_repository *repo = NULL;
3754 struct got_worktree *worktree = NULL;
3755 struct got_object_id *id1 = NULL, *id2 = NULL;
3756 char *repo_path = NULL, *cwd = NULL;
3757 char *id_str1 = NULL, *id_str2 = NULL;
3758 char *label1 = NULL, *label2 = NULL;
3759 int diff_context = 3, ignore_whitespace = 0;
3760 int ch, force_text_diff = 0;
3761 const char *errstr;
3762 struct tog_view *view;
3764 #ifndef PROFILE
3765 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3766 NULL) == -1)
3767 err(1, "pledge");
3768 #endif
3769 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3770 switch (ch) {
3771 case 'a':
3772 force_text_diff = 1;
3773 break;
3774 case 'C':
3775 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3776 &errstr);
3777 if (errstr != NULL)
3778 err(1, "-C option %s", errstr);
3779 break;
3780 case 'r':
3781 repo_path = realpath(optarg, NULL);
3782 if (repo_path == NULL)
3783 return got_error_from_errno2("realpath",
3784 optarg);
3785 got_path_strip_trailing_slashes(repo_path);
3786 break;
3787 case 'w':
3788 ignore_whitespace = 1;
3789 break;
3790 default:
3791 usage_diff();
3792 /* NOTREACHED */
3796 argc -= optind;
3797 argv += optind;
3799 if (argc == 0) {
3800 usage_diff(); /* TODO show local worktree changes */
3801 } else if (argc == 2) {
3802 id_str1 = argv[0];
3803 id_str2 = argv[1];
3804 } else
3805 usage_diff();
3807 cwd = getcwd(NULL, 0);
3808 if (cwd == NULL)
3809 return got_error_from_errno("getcwd");
3811 error = got_worktree_open(&worktree, cwd);
3812 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3813 goto done;
3815 if (repo_path == NULL) {
3816 if (worktree)
3817 repo_path =
3818 strdup(got_worktree_get_repo_path(worktree));
3819 else
3820 repo_path = strdup(cwd);
3822 if (repo_path == NULL) {
3823 error = got_error_from_errno("strdup");
3824 goto done;
3827 error = got_repo_open(&repo, repo_path, NULL);
3828 if (error)
3829 goto done;
3831 init_curses();
3833 error = apply_unveil(got_repo_get_path(repo), NULL);
3834 if (error)
3835 goto done;
3837 error = got_repo_match_object_id(&id1, &label1, id_str1,
3838 GOT_OBJ_TYPE_ANY, 1, repo);
3839 if (error)
3840 goto done;
3842 error = got_repo_match_object_id(&id2, &label2, id_str2,
3843 GOT_OBJ_TYPE_ANY, 1, repo);
3844 if (error)
3845 goto done;
3847 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3848 if (view == NULL) {
3849 error = got_error_from_errno("view_open");
3850 goto done;
3852 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3853 ignore_whitespace, force_text_diff, NULL, repo);
3854 if (error)
3855 goto done;
3856 error = view_loop(view);
3857 done:
3858 free(label1);
3859 free(label2);
3860 free(repo_path);
3861 free(cwd);
3862 if (repo)
3863 got_repo_close(repo);
3864 if (worktree)
3865 got_worktree_close(worktree);
3866 return error;
3869 __dead static void
3870 usage_blame(void)
3872 endwin();
3873 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3874 getprogname());
3875 exit(1);
3878 struct tog_blame_line {
3879 int annotated;
3880 struct got_object_id *id;
3883 static const struct got_error *
3884 draw_blame(struct tog_view *view)
3886 struct tog_blame_view_state *s = &view->state.blame;
3887 struct tog_blame *blame = &s->blame;
3888 regmatch_t *regmatch = &view->regmatch;
3889 const struct got_error *err;
3890 int lineno = 0, nprinted = 0;
3891 char *line;
3892 size_t len;
3893 wchar_t *wline;
3894 int width;
3895 struct tog_blame_line *blame_line;
3896 struct got_object_id *prev_id = NULL;
3897 char *id_str;
3898 struct tog_color *tc;
3900 err = got_object_id_str(&id_str, s->blamed_commit->id);
3901 if (err)
3902 return err;
3904 rewind(blame->f);
3905 werase(view->window);
3907 if (asprintf(&line, "commit %s", id_str) == -1) {
3908 err = got_error_from_errno("asprintf");
3909 free(id_str);
3910 return err;
3913 err = format_line(&wline, &width, line, view->ncols, 0);
3914 free(line);
3915 line = NULL;
3916 if (err)
3917 return err;
3918 if (view_needs_focus_indication(view))
3919 wstandout(view->window);
3920 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3921 if (tc)
3922 wattr_on(view->window,
3923 COLOR_PAIR(tc->colorpair), NULL);
3924 waddwstr(view->window, wline);
3925 if (tc)
3926 wattr_off(view->window,
3927 COLOR_PAIR(tc->colorpair), NULL);
3928 if (view_needs_focus_indication(view))
3929 wstandend(view->window);
3930 free(wline);
3931 wline = NULL;
3932 if (width < view->ncols - 1)
3933 waddch(view->window, '\n');
3935 if (asprintf(&line, "[%d/%d] %s%s",
3936 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3937 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3938 free(id_str);
3939 return got_error_from_errno("asprintf");
3941 free(id_str);
3942 err = format_line(&wline, &width, line, view->ncols, 0);
3943 free(line);
3944 line = NULL;
3945 if (err)
3946 return err;
3947 waddwstr(view->window, wline);
3948 free(wline);
3949 wline = NULL;
3950 if (width < view->ncols - 1)
3951 waddch(view->window, '\n');
3953 s->eof = 0;
3954 while (nprinted < view->nlines - 2) {
3955 line = parse_next_line(blame->f, &len);
3956 if (line == NULL) {
3957 s->eof = 1;
3958 break;
3960 if (++lineno < s->first_displayed_line) {
3961 free(line);
3962 continue;
3965 if (view->focussed && nprinted == s->selected_line - 1)
3966 wstandout(view->window);
3968 if (blame->nlines > 0) {
3969 blame_line = &blame->lines[lineno - 1];
3970 if (blame_line->annotated && prev_id &&
3971 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3972 !(view->focussed &&
3973 nprinted == s->selected_line - 1)) {
3974 waddstr(view->window, " ");
3975 } else if (blame_line->annotated) {
3976 char *id_str;
3977 err = got_object_id_str(&id_str, blame_line->id);
3978 if (err) {
3979 free(line);
3980 return err;
3982 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3983 if (tc)
3984 wattr_on(view->window,
3985 COLOR_PAIR(tc->colorpair), NULL);
3986 wprintw(view->window, "%.8s", id_str);
3987 if (tc)
3988 wattr_off(view->window,
3989 COLOR_PAIR(tc->colorpair), NULL);
3990 free(id_str);
3991 prev_id = blame_line->id;
3992 } else {
3993 waddstr(view->window, "........");
3994 prev_id = NULL;
3996 } else {
3997 waddstr(view->window, "........");
3998 prev_id = NULL;
4001 if (view->focussed && nprinted == s->selected_line - 1)
4002 wstandend(view->window);
4003 waddstr(view->window, " ");
4005 if (view->ncols <= 9) {
4006 width = 9;
4007 wline = wcsdup(L"");
4008 if (wline == NULL) {
4009 err = got_error_from_errno("wcsdup");
4010 free(line);
4011 return err;
4013 } else if (s->first_displayed_line + nprinted ==
4014 s->matched_line &&
4015 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4016 err = add_matched_line(&width, line, view->ncols - 9, 9,
4017 view->window, regmatch);
4018 if (err) {
4019 free(line);
4020 return err;
4022 width += 9;
4023 } else {
4024 err = format_line(&wline, &width, line,
4025 view->ncols - 9, 9);
4026 waddwstr(view->window, wline);
4027 free(wline);
4028 wline = NULL;
4029 width += 9;
4032 if (width <= view->ncols - 1)
4033 waddch(view->window, '\n');
4034 if (++nprinted == 1)
4035 s->first_displayed_line = lineno;
4036 free(line);
4038 s->last_displayed_line = lineno;
4040 view_vborder(view);
4042 return NULL;
4045 static const struct got_error *
4046 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4048 const struct got_error *err = NULL;
4049 struct tog_blame_cb_args *a = arg;
4050 struct tog_blame_line *line;
4051 int errcode;
4053 if (nlines != a->nlines ||
4054 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4055 return got_error(GOT_ERR_RANGE);
4057 errcode = pthread_mutex_lock(&tog_mutex);
4058 if (errcode)
4059 return got_error_set_errno(errcode, "pthread_mutex_lock");
4061 if (*a->quit) { /* user has quit the blame view */
4062 err = got_error(GOT_ERR_ITER_COMPLETED);
4063 goto done;
4066 if (lineno == -1)
4067 goto done; /* no change in this commit */
4069 line = &a->lines[lineno - 1];
4070 if (line->annotated)
4071 goto done;
4073 line->id = got_object_id_dup(id);
4074 if (line->id == NULL) {
4075 err = got_error_from_errno("got_object_id_dup");
4076 goto done;
4078 line->annotated = 1;
4079 done:
4080 errcode = pthread_mutex_unlock(&tog_mutex);
4081 if (errcode)
4082 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4083 return err;
4086 static void *
4087 blame_thread(void *arg)
4089 const struct got_error *err;
4090 struct tog_blame_thread_args *ta = arg;
4091 struct tog_blame_cb_args *a = ta->cb_args;
4092 int errcode;
4094 err = block_signals_used_by_main_thread();
4095 if (err)
4096 return (void *)err;
4098 err = got_blame(ta->path, a->commit_id, ta->repo,
4099 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4100 if (err && err->code == GOT_ERR_CANCELLED)
4101 err = NULL;
4103 errcode = pthread_mutex_lock(&tog_mutex);
4104 if (errcode)
4105 return (void *)got_error_set_errno(errcode,
4106 "pthread_mutex_lock");
4108 got_repo_close(ta->repo);
4109 ta->repo = NULL;
4110 *ta->complete = 1;
4112 errcode = pthread_mutex_unlock(&tog_mutex);
4113 if (errcode && err == NULL)
4114 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4116 return (void *)err;
4119 static struct got_object_id *
4120 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4121 int first_displayed_line, int selected_line)
4123 struct tog_blame_line *line;
4125 if (nlines <= 0)
4126 return NULL;
4128 line = &lines[first_displayed_line - 1 + selected_line - 1];
4129 if (!line->annotated)
4130 return NULL;
4132 return line->id;
4135 static const struct got_error *
4136 stop_blame(struct tog_blame *blame)
4138 const struct got_error *err = NULL;
4139 int i;
4141 if (blame->thread) {
4142 int errcode;
4143 errcode = pthread_mutex_unlock(&tog_mutex);
4144 if (errcode)
4145 return got_error_set_errno(errcode,
4146 "pthread_mutex_unlock");
4147 errcode = pthread_join(blame->thread, (void **)&err);
4148 if (errcode)
4149 return got_error_set_errno(errcode, "pthread_join");
4150 errcode = pthread_mutex_lock(&tog_mutex);
4151 if (errcode)
4152 return got_error_set_errno(errcode,
4153 "pthread_mutex_lock");
4154 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4155 err = NULL;
4156 blame->thread = NULL;
4158 if (blame->thread_args.repo) {
4159 got_repo_close(blame->thread_args.repo);
4160 blame->thread_args.repo = NULL;
4162 if (blame->f) {
4163 if (fclose(blame->f) != 0 && err == NULL)
4164 err = got_error_from_errno("fclose");
4165 blame->f = NULL;
4167 if (blame->lines) {
4168 for (i = 0; i < blame->nlines; i++)
4169 free(blame->lines[i].id);
4170 free(blame->lines);
4171 blame->lines = NULL;
4173 free(blame->cb_args.commit_id);
4174 blame->cb_args.commit_id = NULL;
4176 return err;
4179 static const struct got_error *
4180 cancel_blame_view(void *arg)
4182 const struct got_error *err = NULL;
4183 int *done = arg;
4184 int errcode;
4186 errcode = pthread_mutex_lock(&tog_mutex);
4187 if (errcode)
4188 return got_error_set_errno(errcode,
4189 "pthread_mutex_unlock");
4191 if (*done)
4192 err = got_error(GOT_ERR_CANCELLED);
4194 errcode = pthread_mutex_unlock(&tog_mutex);
4195 if (errcode)
4196 return got_error_set_errno(errcode,
4197 "pthread_mutex_lock");
4199 return err;
4202 static const struct got_error *
4203 run_blame(struct tog_view *view)
4205 struct tog_blame_view_state *s = &view->state.blame;
4206 struct tog_blame *blame = &s->blame;
4207 const struct got_error *err = NULL;
4208 struct got_blob_object *blob = NULL;
4209 struct got_repository *thread_repo = NULL;
4210 struct got_object_id *obj_id = NULL;
4211 int obj_type;
4213 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4214 s->path);
4215 if (err)
4216 return err;
4218 err = got_object_get_type(&obj_type, s->repo, obj_id);
4219 if (err)
4220 goto done;
4222 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4223 err = got_error(GOT_ERR_OBJ_TYPE);
4224 goto done;
4227 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4228 if (err)
4229 goto done;
4230 blame->f = got_opentemp();
4231 if (blame->f == NULL) {
4232 err = got_error_from_errno("got_opentemp");
4233 goto done;
4235 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4236 &blame->line_offsets, blame->f, blob);
4237 if (err || blame->nlines == 0)
4238 goto done;
4240 /* Don't include \n at EOF in the blame line count. */
4241 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4242 blame->nlines--;
4244 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4245 if (blame->lines == NULL) {
4246 err = got_error_from_errno("calloc");
4247 goto done;
4250 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4251 if (err)
4252 goto done;
4254 blame->cb_args.view = view;
4255 blame->cb_args.lines = blame->lines;
4256 blame->cb_args.nlines = blame->nlines;
4257 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4258 if (blame->cb_args.commit_id == NULL) {
4259 err = got_error_from_errno("got_object_id_dup");
4260 goto done;
4262 blame->cb_args.quit = &s->done;
4264 blame->thread_args.path = s->path;
4265 blame->thread_args.repo = thread_repo;
4266 blame->thread_args.cb_args = &blame->cb_args;
4267 blame->thread_args.complete = &s->blame_complete;
4268 blame->thread_args.cancel_cb = cancel_blame_view;
4269 blame->thread_args.cancel_arg = &s->done;
4270 s->blame_complete = 0;
4272 done:
4273 if (blob)
4274 got_object_blob_close(blob);
4275 free(obj_id);
4276 if (err)
4277 stop_blame(blame);
4278 return err;
4281 static const struct got_error *
4282 open_blame_view(struct tog_view *view, char *path,
4283 struct got_object_id *commit_id, struct got_repository *repo)
4285 const struct got_error *err = NULL;
4286 struct tog_blame_view_state *s = &view->state.blame;
4288 SIMPLEQ_INIT(&s->blamed_commits);
4290 s->path = strdup(path);
4291 if (s->path == NULL)
4292 return got_error_from_errno("strdup");
4294 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4295 if (err) {
4296 free(s->path);
4297 return err;
4300 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4301 s->first_displayed_line = 1;
4302 s->last_displayed_line = view->nlines;
4303 s->selected_line = 1;
4304 s->blame_complete = 0;
4305 s->repo = repo;
4306 s->commit_id = commit_id;
4307 memset(&s->blame, 0, sizeof(s->blame));
4309 SIMPLEQ_INIT(&s->colors);
4310 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4311 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4312 get_color_value("TOG_COLOR_COMMIT"));
4313 if (err)
4314 return err;
4317 view->show = show_blame_view;
4318 view->input = input_blame_view;
4319 view->close = close_blame_view;
4320 view->search_start = search_start_blame_view;
4321 view->search_next = search_next_blame_view;
4323 return run_blame(view);
4326 static const struct got_error *
4327 close_blame_view(struct tog_view *view)
4329 const struct got_error *err = NULL;
4330 struct tog_blame_view_state *s = &view->state.blame;
4332 if (s->blame.thread)
4333 err = stop_blame(&s->blame);
4335 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4336 struct got_object_qid *blamed_commit;
4337 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4338 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4339 got_object_qid_free(blamed_commit);
4342 free(s->path);
4343 free_colors(&s->colors);
4345 return err;
4348 static const struct got_error *
4349 search_start_blame_view(struct tog_view *view)
4351 struct tog_blame_view_state *s = &view->state.blame;
4353 s->matched_line = 0;
4354 return NULL;
4357 static const struct got_error *
4358 search_next_blame_view(struct tog_view *view)
4360 struct tog_blame_view_state *s = &view->state.blame;
4361 int lineno;
4363 if (!view->searching) {
4364 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4365 return NULL;
4368 if (s->matched_line) {
4369 if (view->searching == TOG_SEARCH_FORWARD)
4370 lineno = s->matched_line + 1;
4371 else
4372 lineno = s->matched_line - 1;
4373 } else {
4374 if (view->searching == TOG_SEARCH_FORWARD)
4375 lineno = 1;
4376 else
4377 lineno = s->blame.nlines;
4380 while (1) {
4381 char *line = NULL;
4382 off_t offset;
4383 size_t len;
4385 if (lineno <= 0 || lineno > s->blame.nlines) {
4386 if (s->matched_line == 0) {
4387 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4388 free(line);
4389 break;
4392 if (view->searching == TOG_SEARCH_FORWARD)
4393 lineno = 1;
4394 else
4395 lineno = s->blame.nlines;
4398 offset = s->blame.line_offsets[lineno - 1];
4399 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4400 free(line);
4401 return got_error_from_errno("fseeko");
4403 free(line);
4404 line = parse_next_line(s->blame.f, &len);
4405 if (line &&
4406 match_line(line, &view->regex, 1, &view->regmatch)) {
4407 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4408 s->matched_line = lineno;
4409 free(line);
4410 break;
4412 free(line);
4413 if (view->searching == TOG_SEARCH_FORWARD)
4414 lineno++;
4415 else
4416 lineno--;
4419 if (s->matched_line) {
4420 s->first_displayed_line = s->matched_line;
4421 s->selected_line = 1;
4424 return NULL;
4427 static const struct got_error *
4428 show_blame_view(struct tog_view *view)
4430 const struct got_error *err = NULL;
4431 struct tog_blame_view_state *s = &view->state.blame;
4432 int errcode;
4434 if (s->blame.thread == NULL) {
4435 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4436 &s->blame.thread_args);
4437 if (errcode)
4438 return got_error_set_errno(errcode, "pthread_create");
4440 halfdelay(1); /* fast refresh while annotating */
4443 if (s->blame_complete)
4444 halfdelay(10); /* disable fast refresh */
4446 err = draw_blame(view);
4448 view_vborder(view);
4449 return err;
4452 static const struct got_error *
4453 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4454 struct tog_view **focus_view, struct tog_view *view, int ch)
4456 const struct got_error *err = NULL, *thread_err = NULL;
4457 struct tog_view *diff_view;
4458 struct tog_blame_view_state *s = &view->state.blame;
4459 int begin_x = 0;
4461 switch (ch) {
4462 case 'q':
4463 s->done = 1;
4464 break;
4465 case 'k':
4466 case KEY_UP:
4467 if (s->selected_line > 1)
4468 s->selected_line--;
4469 else if (s->selected_line == 1 &&
4470 s->first_displayed_line > 1)
4471 s->first_displayed_line--;
4472 break;
4473 case KEY_PPAGE:
4474 case CTRL('b'):
4475 if (s->first_displayed_line == 1) {
4476 s->selected_line = 1;
4477 break;
4479 if (s->first_displayed_line > view->nlines - 2)
4480 s->first_displayed_line -=
4481 (view->nlines - 2);
4482 else
4483 s->first_displayed_line = 1;
4484 break;
4485 case 'j':
4486 case KEY_DOWN:
4487 if (s->selected_line < view->nlines - 2 &&
4488 s->first_displayed_line +
4489 s->selected_line <= s->blame.nlines)
4490 s->selected_line++;
4491 else if (s->last_displayed_line <
4492 s->blame.nlines)
4493 s->first_displayed_line++;
4494 break;
4495 case 'b':
4496 case 'p': {
4497 struct got_object_id *id = NULL;
4498 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4499 s->first_displayed_line, s->selected_line);
4500 if (id == NULL)
4501 break;
4502 if (ch == 'p') {
4503 struct got_commit_object *commit;
4504 struct got_object_qid *pid;
4505 struct got_object_id *blob_id = NULL;
4506 int obj_type;
4507 err = got_object_open_as_commit(&commit,
4508 s->repo, id);
4509 if (err)
4510 break;
4511 pid = SIMPLEQ_FIRST(
4512 got_object_commit_get_parent_ids(commit));
4513 if (pid == NULL) {
4514 got_object_commit_close(commit);
4515 break;
4517 /* Check if path history ends here. */
4518 err = got_object_id_by_path(&blob_id, s->repo,
4519 pid->id, s->path);
4520 if (err) {
4521 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4522 err = NULL;
4523 got_object_commit_close(commit);
4524 break;
4526 err = got_object_get_type(&obj_type, s->repo,
4527 blob_id);
4528 free(blob_id);
4529 /* Can't blame non-blob type objects. */
4530 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4531 got_object_commit_close(commit);
4532 break;
4534 err = got_object_qid_alloc(&s->blamed_commit,
4535 pid->id);
4536 got_object_commit_close(commit);
4537 } else {
4538 if (got_object_id_cmp(id,
4539 s->blamed_commit->id) == 0)
4540 break;
4541 err = got_object_qid_alloc(&s->blamed_commit,
4542 id);
4544 if (err)
4545 break;
4546 s->done = 1;
4547 thread_err = stop_blame(&s->blame);
4548 s->done = 0;
4549 if (thread_err)
4550 break;
4551 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4552 s->blamed_commit, entry);
4553 err = run_blame(view);
4554 if (err)
4555 break;
4556 break;
4558 case 'B': {
4559 struct got_object_qid *first;
4560 first = SIMPLEQ_FIRST(&s->blamed_commits);
4561 if (!got_object_id_cmp(first->id, s->commit_id))
4562 break;
4563 s->done = 1;
4564 thread_err = stop_blame(&s->blame);
4565 s->done = 0;
4566 if (thread_err)
4567 break;
4568 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4569 got_object_qid_free(s->blamed_commit);
4570 s->blamed_commit =
4571 SIMPLEQ_FIRST(&s->blamed_commits);
4572 err = run_blame(view);
4573 if (err)
4574 break;
4575 break;
4577 case KEY_ENTER:
4578 case '\r': {
4579 struct got_object_id *id = NULL;
4580 struct got_object_qid *pid;
4581 struct got_commit_object *commit = NULL;
4582 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4583 s->first_displayed_line, s->selected_line);
4584 if (id == NULL)
4585 break;
4586 err = got_object_open_as_commit(&commit, s->repo, id);
4587 if (err)
4588 break;
4589 pid = SIMPLEQ_FIRST(
4590 got_object_commit_get_parent_ids(commit));
4591 if (view_is_parent_view(view))
4592 begin_x = view_split_begin_x(view->begin_x);
4593 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4594 if (diff_view == NULL) {
4595 got_object_commit_close(commit);
4596 err = got_error_from_errno("view_open");
4597 break;
4599 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4600 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4601 got_object_commit_close(commit);
4602 if (err) {
4603 view_close(diff_view);
4604 break;
4606 if (view_is_parent_view(view)) {
4607 err = view_close_child(view);
4608 if (err)
4609 break;
4610 err = view_set_child(view, diff_view);
4611 if (err) {
4612 view_close(diff_view);
4613 break;
4615 *focus_view = diff_view;
4616 view->child_focussed = 1;
4617 } else
4618 *new_view = diff_view;
4619 if (err)
4620 break;
4621 break;
4623 case KEY_NPAGE:
4624 case CTRL('f'):
4625 case ' ':
4626 if (s->last_displayed_line >= s->blame.nlines &&
4627 s->selected_line >= MIN(s->blame.nlines,
4628 view->nlines - 2)) {
4629 break;
4631 if (s->last_displayed_line >= s->blame.nlines &&
4632 s->selected_line < view->nlines - 2) {
4633 s->selected_line = MIN(s->blame.nlines,
4634 view->nlines - 2);
4635 break;
4637 if (s->last_displayed_line + view->nlines - 2
4638 <= s->blame.nlines)
4639 s->first_displayed_line +=
4640 view->nlines - 2;
4641 else
4642 s->first_displayed_line =
4643 s->blame.nlines -
4644 (view->nlines - 3);
4645 break;
4646 case KEY_RESIZE:
4647 if (s->selected_line > view->nlines - 2) {
4648 s->selected_line = MIN(s->blame.nlines,
4649 view->nlines - 2);
4651 break;
4652 default:
4653 break;
4655 return thread_err ? thread_err : err;
4658 static const struct got_error *
4659 cmd_blame(int argc, char *argv[])
4661 const struct got_error *error;
4662 struct got_repository *repo = NULL;
4663 struct got_worktree *worktree = NULL;
4664 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4665 char *link_target = NULL;
4666 struct got_object_id *commit_id = NULL;
4667 char *commit_id_str = NULL;
4668 int ch;
4669 struct tog_view *view;
4671 #ifndef PROFILE
4672 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4673 NULL) == -1)
4674 err(1, "pledge");
4675 #endif
4677 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4678 switch (ch) {
4679 case 'c':
4680 commit_id_str = optarg;
4681 break;
4682 case 'r':
4683 repo_path = realpath(optarg, NULL);
4684 if (repo_path == NULL)
4685 return got_error_from_errno2("realpath",
4686 optarg);
4687 break;
4688 default:
4689 usage_blame();
4690 /* NOTREACHED */
4694 argc -= optind;
4695 argv += optind;
4697 if (argc != 1)
4698 usage_blame();
4700 cwd = getcwd(NULL, 0);
4701 if (cwd == NULL)
4702 return got_error_from_errno("getcwd");
4704 error = got_worktree_open(&worktree, cwd);
4705 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4706 goto done;
4708 if (repo_path == NULL) {
4709 if (worktree)
4710 repo_path =
4711 strdup(got_worktree_get_repo_path(worktree));
4712 else
4713 repo_path = strdup(cwd);
4715 if (repo_path == NULL) {
4716 error = got_error_from_errno("strdup");
4717 goto done;
4720 error = got_repo_open(&repo, repo_path, NULL);
4721 if (error != NULL)
4722 goto done;
4724 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4725 worktree);
4726 if (error)
4727 goto done;
4729 init_curses();
4731 error = apply_unveil(got_repo_get_path(repo), NULL);
4732 if (error)
4733 goto done;
4735 if (commit_id_str == NULL) {
4736 struct got_reference *head_ref;
4737 error = got_ref_open(&head_ref, repo, worktree ?
4738 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4739 if (error != NULL)
4740 goto done;
4741 error = got_ref_resolve(&commit_id, repo, head_ref);
4742 got_ref_close(head_ref);
4743 } else {
4744 error = got_repo_match_object_id(&commit_id, NULL,
4745 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4747 if (error != NULL)
4748 goto done;
4750 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4751 if (view == NULL) {
4752 error = got_error_from_errno("view_open");
4753 goto done;
4756 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4757 commit_id, repo);
4758 if (error)
4759 goto done;
4761 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4762 commit_id, repo);
4763 if (error)
4764 goto done;
4765 if (worktree) {
4766 /* Release work tree lock. */
4767 got_worktree_close(worktree);
4768 worktree = NULL;
4770 error = view_loop(view);
4771 done:
4772 free(repo_path);
4773 free(in_repo_path);
4774 free(link_target);
4775 free(cwd);
4776 free(commit_id);
4777 if (worktree)
4778 got_worktree_close(worktree);
4779 if (repo)
4780 got_repo_close(repo);
4781 return error;
4784 static const struct got_error *
4785 draw_tree_entries(struct tog_view *view, const char *parent_path)
4787 struct tog_tree_view_state *s = &view->state.tree;
4788 const struct got_error *err = NULL;
4789 struct got_tree_entry *te;
4790 wchar_t *wline;
4791 struct tog_color *tc;
4792 int width, n, i, nentries;
4793 int limit = view->nlines;
4795 s->ndisplayed = 0;
4797 werase(view->window);
4799 if (limit == 0)
4800 return NULL;
4802 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4803 if (err)
4804 return err;
4805 if (view_needs_focus_indication(view))
4806 wstandout(view->window);
4807 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4808 if (tc)
4809 wattr_on(view->window,
4810 COLOR_PAIR(tc->colorpair), NULL);
4811 waddwstr(view->window, wline);
4812 if (tc)
4813 wattr_off(view->window,
4814 COLOR_PAIR(tc->colorpair), NULL);
4815 if (view_needs_focus_indication(view))
4816 wstandend(view->window);
4817 free(wline);
4818 wline = NULL;
4819 if (width < view->ncols - 1)
4820 waddch(view->window, '\n');
4821 if (--limit <= 0)
4822 return NULL;
4823 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4824 if (err)
4825 return err;
4826 waddwstr(view->window, wline);
4827 free(wline);
4828 wline = NULL;
4829 if (width < view->ncols - 1)
4830 waddch(view->window, '\n');
4831 if (--limit <= 0)
4832 return NULL;
4833 waddch(view->window, '\n');
4834 if (--limit <= 0)
4835 return NULL;
4837 if (s->first_displayed_entry == NULL) {
4838 te = got_object_tree_get_first_entry(s->tree);
4839 if (s->selected == 0) {
4840 if (view->focussed)
4841 wstandout(view->window);
4842 s->selected_entry = NULL;
4844 waddstr(view->window, " ..\n"); /* parent directory */
4845 if (s->selected == 0 && view->focussed)
4846 wstandend(view->window);
4847 s->ndisplayed++;
4848 if (--limit <= 0)
4849 return NULL;
4850 n = 1;
4851 } else {
4852 n = 0;
4853 te = s->first_displayed_entry;
4856 nentries = got_object_tree_get_nentries(s->tree);
4857 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4858 char *line = NULL, *id_str = NULL, *link_target = NULL;
4859 const char *modestr = "";
4860 mode_t mode;
4862 te = got_object_tree_get_entry(s->tree, i);
4863 mode = got_tree_entry_get_mode(te);
4865 if (s->show_ids) {
4866 err = got_object_id_str(&id_str,
4867 got_tree_entry_get_id(te));
4868 if (err)
4869 return got_error_from_errno(
4870 "got_object_id_str");
4872 if (got_object_tree_entry_is_submodule(te))
4873 modestr = "$";
4874 else if (S_ISLNK(mode)) {
4875 int i;
4877 err = got_tree_entry_get_symlink_target(&link_target,
4878 te, s->repo);
4879 if (err) {
4880 free(id_str);
4881 return err;
4883 for (i = 0; i < strlen(link_target); i++) {
4884 if (!isprint((unsigned char)link_target[i]))
4885 link_target[i] = '?';
4887 modestr = "@";
4889 else if (S_ISDIR(mode))
4890 modestr = "/";
4891 else if (mode & S_IXUSR)
4892 modestr = "*";
4893 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4894 got_tree_entry_get_name(te), modestr,
4895 link_target ? " -> ": "",
4896 link_target ? link_target : "") == -1) {
4897 free(id_str);
4898 free(link_target);
4899 return got_error_from_errno("asprintf");
4901 free(id_str);
4902 free(link_target);
4903 err = format_line(&wline, &width, line, view->ncols, 0);
4904 if (err) {
4905 free(line);
4906 break;
4908 if (n == s->selected) {
4909 if (view->focussed)
4910 wstandout(view->window);
4911 s->selected_entry = te;
4913 tc = match_color(&s->colors, line);
4914 if (tc)
4915 wattr_on(view->window,
4916 COLOR_PAIR(tc->colorpair), NULL);
4917 waddwstr(view->window, wline);
4918 if (tc)
4919 wattr_off(view->window,
4920 COLOR_PAIR(tc->colorpair), NULL);
4921 if (width < view->ncols - 1)
4922 waddch(view->window, '\n');
4923 if (n == s->selected && view->focussed)
4924 wstandend(view->window);
4925 free(line);
4926 free(wline);
4927 wline = NULL;
4928 n++;
4929 s->ndisplayed++;
4930 s->last_displayed_entry = te;
4931 if (--limit <= 0)
4932 break;
4935 return err;
4938 static void
4939 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4941 struct got_tree_entry *te;
4942 int isroot = s->tree == s->root;
4943 int i = 0;
4945 if (s->first_displayed_entry == NULL)
4946 return;
4948 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4949 while (i++ < maxscroll) {
4950 if (te == NULL) {
4951 if (!isroot)
4952 s->first_displayed_entry = NULL;
4953 break;
4955 s->first_displayed_entry = te;
4956 te = got_tree_entry_get_prev(s->tree, te);
4960 static void
4961 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4963 struct got_tree_entry *next, *last;
4964 int n = 0;
4966 if (s->first_displayed_entry)
4967 next = got_tree_entry_get_next(s->tree,
4968 s->first_displayed_entry);
4969 else
4970 next = got_object_tree_get_first_entry(s->tree);
4972 last = s->last_displayed_entry;
4973 while (next && last && n++ < maxscroll) {
4974 last = got_tree_entry_get_next(s->tree, last);
4975 if (last) {
4976 s->first_displayed_entry = next;
4977 next = got_tree_entry_get_next(s->tree, next);
4982 static const struct got_error *
4983 tree_entry_path(char **path, struct tog_parent_trees *parents,
4984 struct got_tree_entry *te)
4986 const struct got_error *err = NULL;
4987 struct tog_parent_tree *pt;
4988 size_t len = 2; /* for leading slash and NUL */
4990 TAILQ_FOREACH(pt, parents, entry)
4991 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4992 + 1 /* slash */;
4993 if (te)
4994 len += strlen(got_tree_entry_get_name(te));
4996 *path = calloc(1, len);
4997 if (path == NULL)
4998 return got_error_from_errno("calloc");
5000 (*path)[0] = '/';
5001 pt = TAILQ_LAST(parents, tog_parent_trees);
5002 while (pt) {
5003 const char *name = got_tree_entry_get_name(pt->selected_entry);
5004 if (strlcat(*path, name, len) >= len) {
5005 err = got_error(GOT_ERR_NO_SPACE);
5006 goto done;
5008 if (strlcat(*path, "/", len) >= len) {
5009 err = got_error(GOT_ERR_NO_SPACE);
5010 goto done;
5012 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5014 if (te) {
5015 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5016 err = got_error(GOT_ERR_NO_SPACE);
5017 goto done;
5020 done:
5021 if (err) {
5022 free(*path);
5023 *path = NULL;
5025 return err;
5028 static const struct got_error *
5029 blame_tree_entry(struct tog_view **new_view, int begin_x,
5030 struct got_tree_entry *te, struct tog_parent_trees *parents,
5031 struct got_object_id *commit_id, struct got_repository *repo)
5033 const struct got_error *err = NULL;
5034 char *path;
5035 struct tog_view *blame_view;
5037 *new_view = NULL;
5039 err = tree_entry_path(&path, parents, te);
5040 if (err)
5041 return err;
5043 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5044 if (blame_view == NULL) {
5045 err = got_error_from_errno("view_open");
5046 goto done;
5049 err = open_blame_view(blame_view, path, commit_id, repo);
5050 if (err) {
5051 if (err->code == GOT_ERR_CANCELLED)
5052 err = NULL;
5053 view_close(blame_view);
5054 } else
5055 *new_view = blame_view;
5056 done:
5057 free(path);
5058 return err;
5061 static const struct got_error *
5062 log_tree_entry(struct tog_view **new_view, int begin_x,
5063 struct got_tree_entry *te, struct tog_parent_trees *parents,
5064 struct got_object_id *commit_id, struct got_repository *repo)
5066 struct tog_view *log_view;
5067 const struct got_error *err = NULL;
5068 char *path;
5070 *new_view = NULL;
5072 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5073 if (log_view == NULL)
5074 return got_error_from_errno("view_open");
5076 err = tree_entry_path(&path, parents, te);
5077 if (err)
5078 return err;
5080 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5081 if (err)
5082 view_close(log_view);
5083 else
5084 *new_view = log_view;
5085 free(path);
5086 return err;
5089 static const struct got_error *
5090 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5091 struct got_object_id *commit_id, struct got_repository *repo)
5093 const struct got_error *err = NULL;
5094 char *commit_id_str = NULL;
5095 struct tog_tree_view_state *s = &view->state.tree;
5097 TAILQ_INIT(&s->parents);
5099 err = got_object_id_str(&commit_id_str, commit_id);
5100 if (err != NULL)
5101 goto done;
5103 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5104 err = got_error_from_errno("asprintf");
5105 goto done;
5108 s->root = s->tree = root;
5109 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5110 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5111 s->commit_id = got_object_id_dup(commit_id);
5112 if (s->commit_id == NULL) {
5113 err = got_error_from_errno("got_object_id_dup");
5114 goto done;
5116 s->repo = repo;
5118 SIMPLEQ_INIT(&s->colors);
5120 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5121 err = add_color(&s->colors, "\\$$",
5122 TOG_COLOR_TREE_SUBMODULE,
5123 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5124 if (err)
5125 goto done;
5126 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5127 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5128 if (err) {
5129 free_colors(&s->colors);
5130 goto done;
5132 err = add_color(&s->colors, "/$",
5133 TOG_COLOR_TREE_DIRECTORY,
5134 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5135 if (err) {
5136 free_colors(&s->colors);
5137 goto done;
5140 err = add_color(&s->colors, "\\*$",
5141 TOG_COLOR_TREE_EXECUTABLE,
5142 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5143 if (err) {
5144 free_colors(&s->colors);
5145 goto done;
5148 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5149 get_color_value("TOG_COLOR_COMMIT"));
5150 if (err) {
5151 free_colors(&s->colors);
5152 goto done;
5156 view->show = show_tree_view;
5157 view->input = input_tree_view;
5158 view->close = close_tree_view;
5159 view->search_start = search_start_tree_view;
5160 view->search_next = search_next_tree_view;
5161 done:
5162 free(commit_id_str);
5163 if (err) {
5164 free(s->tree_label);
5165 s->tree_label = NULL;
5167 return err;
5170 static const struct got_error *
5171 close_tree_view(struct tog_view *view)
5173 struct tog_tree_view_state *s = &view->state.tree;
5175 free_colors(&s->colors);
5176 free(s->tree_label);
5177 s->tree_label = NULL;
5178 free(s->commit_id);
5179 s->commit_id = NULL;
5180 while (!TAILQ_EMPTY(&s->parents)) {
5181 struct tog_parent_tree *parent;
5182 parent = TAILQ_FIRST(&s->parents);
5183 TAILQ_REMOVE(&s->parents, parent, entry);
5184 free(parent);
5187 if (s->tree != s->root)
5188 got_object_tree_close(s->tree);
5189 got_object_tree_close(s->root);
5190 return NULL;
5193 static const struct got_error *
5194 search_start_tree_view(struct tog_view *view)
5196 struct tog_tree_view_state *s = &view->state.tree;
5198 s->matched_entry = NULL;
5199 return NULL;
5202 static int
5203 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5205 regmatch_t regmatch;
5207 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5208 0) == 0;
5211 static const struct got_error *
5212 search_next_tree_view(struct tog_view *view)
5214 struct tog_tree_view_state *s = &view->state.tree;
5215 struct got_tree_entry *te = NULL;
5217 if (!view->searching) {
5218 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5219 return NULL;
5222 if (s->matched_entry) {
5223 if (view->searching == TOG_SEARCH_FORWARD) {
5224 if (s->selected_entry)
5225 te = got_tree_entry_get_next(s->tree,
5226 s->selected_entry);
5227 else
5228 te = got_object_tree_get_first_entry(s->tree);
5229 } else {
5230 if (s->selected_entry == NULL)
5231 te = got_object_tree_get_last_entry(s->tree);
5232 else
5233 te = got_tree_entry_get_prev(s->tree,
5234 s->selected_entry);
5236 } else {
5237 if (view->searching == TOG_SEARCH_FORWARD)
5238 te = got_object_tree_get_first_entry(s->tree);
5239 else
5240 te = got_object_tree_get_last_entry(s->tree);
5243 while (1) {
5244 if (te == NULL) {
5245 if (s->matched_entry == NULL) {
5246 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5247 return NULL;
5249 if (view->searching == TOG_SEARCH_FORWARD)
5250 te = got_object_tree_get_first_entry(s->tree);
5251 else
5252 te = got_object_tree_get_last_entry(s->tree);
5255 if (match_tree_entry(te, &view->regex)) {
5256 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5257 s->matched_entry = te;
5258 break;
5261 if (view->searching == TOG_SEARCH_FORWARD)
5262 te = got_tree_entry_get_next(s->tree, te);
5263 else
5264 te = got_tree_entry_get_prev(s->tree, te);
5267 if (s->matched_entry) {
5268 s->first_displayed_entry = s->matched_entry;
5269 s->selected = 0;
5272 return NULL;
5275 static const struct got_error *
5276 show_tree_view(struct tog_view *view)
5278 const struct got_error *err = NULL;
5279 struct tog_tree_view_state *s = &view->state.tree;
5280 char *parent_path;
5282 err = tree_entry_path(&parent_path, &s->parents, NULL);
5283 if (err)
5284 return err;
5286 err = draw_tree_entries(view, parent_path);
5287 free(parent_path);
5289 view_vborder(view);
5290 return err;
5293 static const struct got_error *
5294 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5295 struct tog_view **focus_view, struct tog_view *view, int ch)
5297 const struct got_error *err = NULL;
5298 struct tog_tree_view_state *s = &view->state.tree;
5299 struct tog_view *log_view, *ref_view;
5300 int begin_x = 0;
5302 switch (ch) {
5303 case 'i':
5304 s->show_ids = !s->show_ids;
5305 break;
5306 case 'l':
5307 if (!s->selected_entry)
5308 break;
5309 if (view_is_parent_view(view))
5310 begin_x = view_split_begin_x(view->begin_x);
5311 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5312 &s->parents, s->commit_id, s->repo);
5313 if (view_is_parent_view(view)) {
5314 err = view_close_child(view);
5315 if (err)
5316 return err;
5317 err = view_set_child(view, log_view);
5318 if (err) {
5319 view_close(log_view);
5320 break;
5322 *focus_view = log_view;
5323 view->child_focussed = 1;
5324 } else
5325 *new_view = log_view;
5326 break;
5327 case 'r':
5328 if (view_is_parent_view(view))
5329 begin_x = view_split_begin_x(view->begin_x);
5330 ref_view = view_open(view->nlines, view->ncols,
5331 view->begin_y, begin_x, TOG_VIEW_REF);
5332 if (ref_view == NULL)
5333 return got_error_from_errno("view_open");
5334 err = open_ref_view(ref_view, s->repo);
5335 if (err) {
5336 view_close(ref_view);
5337 return err;
5339 if (view_is_parent_view(view)) {
5340 err = view_close_child(view);
5341 if (err)
5342 return err;
5343 err = view_set_child(view, ref_view);
5344 if (err) {
5345 view_close(ref_view);
5346 break;
5348 *focus_view = ref_view;
5349 view->child_focussed = 1;
5350 } else
5351 *new_view = ref_view;
5352 break;
5353 case 'k':
5354 case KEY_UP:
5355 if (s->selected > 0) {
5356 s->selected--;
5357 break;
5359 tree_scroll_up(s, 1);
5360 break;
5361 case KEY_PPAGE:
5362 case CTRL('b'):
5363 if (s->tree == s->root) {
5364 if (got_object_tree_get_first_entry(s->tree) ==
5365 s->first_displayed_entry)
5366 s->selected = 0;
5367 } else {
5368 if (s->first_displayed_entry == NULL)
5369 s->selected = 0;
5371 tree_scroll_up(s, MAX(0, view->nlines - 3));
5372 break;
5373 case 'j':
5374 case KEY_DOWN:
5375 if (s->selected < s->ndisplayed - 1) {
5376 s->selected++;
5377 break;
5379 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5380 == NULL)
5381 /* can't scroll any further */
5382 break;
5383 tree_scroll_down(s, 1);
5384 break;
5385 case KEY_NPAGE:
5386 case CTRL('f'):
5387 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5388 == NULL) {
5389 /* can't scroll any further; move cursor down */
5390 if (s->selected < s->ndisplayed - 1)
5391 s->selected = s->ndisplayed - 1;
5392 break;
5394 tree_scroll_down(s, view->nlines - 3);
5395 break;
5396 case KEY_ENTER:
5397 case '\r':
5398 case KEY_BACKSPACE:
5399 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5400 struct tog_parent_tree *parent;
5401 /* user selected '..' */
5402 if (s->tree == s->root)
5403 break;
5404 parent = TAILQ_FIRST(&s->parents);
5405 TAILQ_REMOVE(&s->parents, parent,
5406 entry);
5407 got_object_tree_close(s->tree);
5408 s->tree = parent->tree;
5409 s->first_displayed_entry =
5410 parent->first_displayed_entry;
5411 s->selected_entry =
5412 parent->selected_entry;
5413 s->selected = parent->selected;
5414 free(parent);
5415 } else if (S_ISDIR(got_tree_entry_get_mode(
5416 s->selected_entry))) {
5417 struct got_tree_object *subtree;
5418 err = got_object_open_as_tree(&subtree, s->repo,
5419 got_tree_entry_get_id(s->selected_entry));
5420 if (err)
5421 break;
5422 err = tree_view_visit_subtree(s, subtree);
5423 if (err) {
5424 got_object_tree_close(subtree);
5425 break;
5427 } else if (S_ISREG(got_tree_entry_get_mode(
5428 s->selected_entry))) {
5429 struct tog_view *blame_view;
5430 int begin_x = view_is_parent_view(view) ?
5431 view_split_begin_x(view->begin_x) : 0;
5433 err = blame_tree_entry(&blame_view, begin_x,
5434 s->selected_entry, &s->parents,
5435 s->commit_id, s->repo);
5436 if (err)
5437 break;
5438 if (view_is_parent_view(view)) {
5439 err = view_close_child(view);
5440 if (err)
5441 return err;
5442 err = view_set_child(view, blame_view);
5443 if (err) {
5444 view_close(blame_view);
5445 break;
5447 *focus_view = blame_view;
5448 view->child_focussed = 1;
5449 } else
5450 *new_view = blame_view;
5452 break;
5453 case KEY_RESIZE:
5454 if (s->selected > view->nlines)
5455 s->selected = s->ndisplayed - 1;
5456 break;
5457 default:
5458 break;
5461 return err;
5464 __dead static void
5465 usage_tree(void)
5467 endwin();
5468 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5469 getprogname());
5470 exit(1);
5473 static const struct got_error *
5474 cmd_tree(int argc, char *argv[])
5476 const struct got_error *error;
5477 struct got_repository *repo = NULL;
5478 struct got_worktree *worktree = NULL;
5479 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5480 struct got_object_id *commit_id = NULL;
5481 char *commit_id_arg = NULL;
5482 struct got_commit_object *commit = NULL;
5483 struct got_tree_object *tree = NULL;
5484 int ch;
5485 struct tog_view *view;
5487 #ifndef PROFILE
5488 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5489 NULL) == -1)
5490 err(1, "pledge");
5491 #endif
5493 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5494 switch (ch) {
5495 case 'c':
5496 commit_id_arg = optarg;
5497 break;
5498 case 'r':
5499 repo_path = realpath(optarg, NULL);
5500 if (repo_path == NULL)
5501 return got_error_from_errno2("realpath",
5502 optarg);
5503 break;
5504 default:
5505 usage_tree();
5506 /* NOTREACHED */
5510 argc -= optind;
5511 argv += optind;
5513 if (argc > 1)
5514 usage_tree();
5516 cwd = getcwd(NULL, 0);
5517 if (cwd == NULL)
5518 return got_error_from_errno("getcwd");
5520 error = got_worktree_open(&worktree, cwd);
5521 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5522 goto done;
5524 if (repo_path == NULL) {
5525 if (worktree)
5526 repo_path =
5527 strdup(got_worktree_get_repo_path(worktree));
5528 else
5529 repo_path = strdup(cwd);
5531 if (repo_path == NULL) {
5532 error = got_error_from_errno("strdup");
5533 goto done;
5536 error = got_repo_open(&repo, repo_path, NULL);
5537 if (error != NULL)
5538 goto done;
5540 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5541 repo, worktree);
5542 if (error)
5543 goto done;
5545 init_curses();
5547 error = apply_unveil(got_repo_get_path(repo), NULL);
5548 if (error)
5549 goto done;
5551 error = got_repo_match_object_id(&commit_id, NULL,
5552 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5553 GOT_OBJ_TYPE_COMMIT, 1, repo);
5554 if (error)
5555 goto done;
5557 error = got_object_open_as_commit(&commit, repo, commit_id);
5558 if (error)
5559 goto done;
5561 error = got_object_open_as_tree(&tree, repo,
5562 got_object_commit_get_tree_id(commit));
5563 if (error)
5564 goto done;
5566 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5567 if (view == NULL) {
5568 error = got_error_from_errno("view_open");
5569 goto done;
5571 error = open_tree_view(view, tree, commit_id, repo);
5572 if (error)
5573 goto done;
5574 if (!got_path_is_root_dir(in_repo_path)) {
5575 error = tree_view_walk_path(&view->state.tree, commit_id,
5576 in_repo_path);
5577 if (error)
5578 goto done;
5581 if (worktree) {
5582 /* Release work tree lock. */
5583 got_worktree_close(worktree);
5584 worktree = NULL;
5586 error = view_loop(view);
5587 done:
5588 free(repo_path);
5589 free(cwd);
5590 free(commit_id);
5591 if (commit)
5592 got_object_commit_close(commit);
5593 if (tree)
5594 got_object_tree_close(tree);
5595 if (repo)
5596 got_repo_close(repo);
5597 return error;
5600 static const struct got_error *
5601 ref_view_load_refs(struct tog_ref_view_state *s)
5603 const struct got_error *err;
5604 struct got_reflist_entry *sre;
5605 struct tog_reflist_entry *re;
5607 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5608 got_ref_cmp_by_name, NULL);
5609 if (err)
5610 return err;
5612 s->nrefs = 0;
5613 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5614 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5615 continue;
5617 re = malloc(sizeof(*re));
5618 if (re == NULL)
5619 return got_error_from_errno("malloc");
5621 re->ref = sre->ref;
5622 re->idx = s->nrefs++;
5623 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5626 return NULL;
5629 void
5630 ref_view_free_refs(struct tog_ref_view_state *s)
5632 struct tog_reflist_entry *re;
5634 while (!TAILQ_EMPTY(&s->refs)) {
5635 re = TAILQ_FIRST(&s->refs);
5636 TAILQ_REMOVE(&s->refs, re, entry);
5637 free(re);
5639 got_ref_list_free(&s->simplerefs);
5642 static const struct got_error *
5643 open_ref_view(struct tog_view *view, struct got_repository *repo)
5645 const struct got_error *err = NULL;
5646 struct tog_ref_view_state *s = &view->state.ref;
5648 s->selected_entry = 0;
5649 s->repo = repo;
5651 SIMPLEQ_INIT(&s->simplerefs);
5652 TAILQ_INIT(&s->refs);
5653 SIMPLEQ_INIT(&s->colors);
5655 err = ref_view_load_refs(s);
5656 if (err)
5657 return err;
5659 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5661 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5662 err = add_color(&s->colors, "^refs/heads/",
5663 TOG_COLOR_REFS_HEADS,
5664 get_color_value("TOG_COLOR_REFS_HEADS"));
5665 if (err)
5666 goto done;
5668 err = add_color(&s->colors, "^refs/tags/",
5669 TOG_COLOR_REFS_TAGS,
5670 get_color_value("TOG_COLOR_REFS_TAGS"));
5671 if (err)
5672 goto done;
5674 err = add_color(&s->colors, "^refs/remotes/",
5675 TOG_COLOR_REFS_REMOTES,
5676 get_color_value("TOG_COLOR_REFS_REMOTES"));
5677 if (err)
5678 goto done;
5681 view->show = show_ref_view;
5682 view->input = input_ref_view;
5683 view->close = close_ref_view;
5684 view->search_start = search_start_ref_view;
5685 view->search_next = search_next_ref_view;
5686 done:
5687 if (err)
5688 free_colors(&s->colors);
5689 return err;
5692 static const struct got_error *
5693 close_ref_view(struct tog_view *view)
5695 struct tog_ref_view_state *s = &view->state.ref;
5697 ref_view_free_refs(s);
5698 free_colors(&s->colors);
5700 return NULL;
5703 static const struct got_error *
5704 resolve_reflist_entry(struct got_object_id **commit_id,
5705 struct tog_reflist_entry *re, struct got_repository *repo)
5707 const struct got_error *err = NULL;
5708 struct got_object_id *obj_id;
5709 struct got_tag_object *tag = NULL;
5710 int obj_type;
5712 *commit_id = NULL;
5714 err = got_ref_resolve(&obj_id, repo, re->ref);
5715 if (err)
5716 return err;
5718 err = got_object_get_type(&obj_type, repo, obj_id);
5719 if (err)
5720 goto done;
5722 switch (obj_type) {
5723 case GOT_OBJ_TYPE_COMMIT:
5724 *commit_id = obj_id;
5725 break;
5726 case GOT_OBJ_TYPE_TAG:
5727 err = got_object_open_as_tag(&tag, repo, obj_id);
5728 if (err)
5729 goto done;
5730 free(obj_id);
5731 err = got_object_get_type(&obj_type, repo,
5732 got_object_tag_get_object_id(tag));
5733 if (err)
5734 goto done;
5735 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5736 err = got_error(GOT_ERR_OBJ_TYPE);
5737 goto done;
5739 *commit_id = got_object_id_dup(
5740 got_object_tag_get_object_id(tag));
5741 if (*commit_id == NULL) {
5742 err = got_error_from_errno("got_object_id_dup");
5743 goto done;
5745 break;
5746 default:
5747 err = got_error(GOT_ERR_OBJ_TYPE);
5748 break;
5751 done:
5752 if (tag)
5753 got_object_tag_close(tag);
5754 if (err) {
5755 free(*commit_id);
5756 *commit_id = NULL;
5758 return err;
5761 static const struct got_error *
5762 log_ref_entry(struct tog_view **new_view, int begin_x,
5763 struct tog_reflist_entry *re, struct got_repository *repo)
5765 struct tog_view *log_view;
5766 const struct got_error *err = NULL;
5767 struct got_object_id *commit_id = NULL;
5769 *new_view = NULL;
5771 err = resolve_reflist_entry(&commit_id, re, repo);
5772 if (err) {
5773 if (err->code != GOT_ERR_OBJ_TYPE)
5774 return err;
5775 else
5776 return NULL;
5779 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5780 if (log_view == NULL) {
5781 err = got_error_from_errno("view_open");
5782 goto done;
5785 err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5786 done:
5787 if (err)
5788 view_close(log_view);
5789 else
5790 *new_view = log_view;
5791 free(commit_id);
5792 return err;
5795 static void
5796 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5798 struct tog_reflist_entry *re;
5799 int i = 0;
5801 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5802 return;
5804 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5805 while (i++ < maxscroll) {
5806 if (re == NULL)
5807 break;
5808 s->first_displayed_entry = re;
5809 re = TAILQ_PREV(re, tog_reflist_head, entry);
5813 static void
5814 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5816 struct tog_reflist_entry *next, *last;
5817 int n = 0;
5819 if (s->first_displayed_entry)
5820 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5821 else
5822 next = TAILQ_FIRST(&s->refs);
5824 last = s->last_displayed_entry;
5825 while (next && last && n++ < maxscroll) {
5826 last = TAILQ_NEXT(last, entry);
5827 if (last) {
5828 s->first_displayed_entry = next;
5829 next = TAILQ_NEXT(next, entry);
5834 static const struct got_error *
5835 search_start_ref_view(struct tog_view *view)
5837 struct tog_ref_view_state *s = &view->state.ref;
5839 s->matched_entry = NULL;
5840 return NULL;
5843 static int
5844 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5846 regmatch_t regmatch;
5848 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5849 0) == 0;
5852 static const struct got_error *
5853 search_next_ref_view(struct tog_view *view)
5855 struct tog_ref_view_state *s = &view->state.ref;
5856 struct tog_reflist_entry *re = NULL;
5858 if (!view->searching) {
5859 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5860 return NULL;
5863 if (s->matched_entry) {
5864 if (view->searching == TOG_SEARCH_FORWARD) {
5865 if (s->selected_entry)
5866 re = TAILQ_NEXT(s->selected_entry, entry);
5867 else
5868 re = TAILQ_PREV(s->selected_entry,
5869 tog_reflist_head, entry);
5870 } else {
5871 if (s->selected_entry == NULL)
5872 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5873 else
5874 re = TAILQ_PREV(s->selected_entry,
5875 tog_reflist_head, entry);
5877 } else {
5878 if (view->searching == TOG_SEARCH_FORWARD)
5879 re = TAILQ_FIRST(&s->refs);
5880 else
5881 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5884 while (1) {
5885 if (re == NULL) {
5886 if (s->matched_entry == NULL) {
5887 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5888 return NULL;
5890 if (view->searching == TOG_SEARCH_FORWARD)
5891 re = TAILQ_FIRST(&s->refs);
5892 else
5893 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5896 if (match_reflist_entry(re, &view->regex)) {
5897 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5898 s->matched_entry = re;
5899 break;
5902 if (view->searching == TOG_SEARCH_FORWARD)
5903 re = TAILQ_NEXT(re, entry);
5904 else
5905 re = TAILQ_PREV(re, tog_reflist_head, entry);
5908 if (s->matched_entry) {
5909 s->first_displayed_entry = s->matched_entry;
5910 s->selected = 0;
5913 return NULL;
5916 static const struct got_error *
5917 show_ref_view(struct tog_view *view)
5919 const struct got_error *err = NULL;
5920 struct tog_ref_view_state *s = &view->state.ref;
5921 struct tog_reflist_entry *re;
5922 char *line = NULL;
5923 wchar_t *wline;
5924 struct tog_color *tc;
5925 int width, n;
5926 int limit = view->nlines;
5928 werase(view->window);
5930 s->ndisplayed = 0;
5932 if (limit == 0)
5933 return NULL;
5935 re = s->first_displayed_entry;
5937 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5938 s->nrefs) == -1)
5939 return got_error_from_errno("asprintf");
5941 err = format_line(&wline, &width, line, view->ncols, 0);
5942 if (err) {
5943 free(line);
5944 return err;
5946 if (view_needs_focus_indication(view))
5947 wstandout(view->window);
5948 waddwstr(view->window, wline);
5949 if (view_needs_focus_indication(view))
5950 wstandend(view->window);
5951 free(wline);
5952 wline = NULL;
5953 free(line);
5954 line = NULL;
5955 if (width < view->ncols - 1)
5956 waddch(view->window, '\n');
5957 if (--limit <= 0)
5958 return NULL;
5960 n = 0;
5961 while (re && limit > 0) {
5962 char *line = NULL;
5964 if (got_ref_is_symbolic(re->ref)) {
5965 if (asprintf(&line, "%s -> %s",
5966 got_ref_get_name(re->ref),
5967 got_ref_get_symref_target(re->ref)) == -1)
5968 return got_error_from_errno("asprintf");
5969 } else if (s->show_ids) {
5970 struct got_object_id *id;
5971 char *id_str;
5972 err = got_ref_resolve(&id, s->repo, re->ref);
5973 if (err)
5974 return err;
5975 err = got_object_id_str(&id_str, id);
5976 if (err) {
5977 free(id);
5978 return err;
5980 if (asprintf(&line, "%s: %s",
5981 got_ref_get_name(re->ref), id_str) == -1) {
5982 err = got_error_from_errno("asprintf");
5983 free(id);
5984 free(id_str);
5985 return err;
5987 free(id);
5988 free(id_str);
5989 } else {
5990 line = strdup(got_ref_get_name(re->ref));
5991 if (line == NULL)
5992 return got_error_from_errno("strdup");
5995 err = format_line(&wline, &width, line, view->ncols, 0);
5996 if (err) {
5997 free(line);
5998 return err;
6000 if (n == s->selected) {
6001 if (view->focussed)
6002 wstandout(view->window);
6003 s->selected_entry = re;
6005 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6006 if (tc)
6007 wattr_on(view->window,
6008 COLOR_PAIR(tc->colorpair), NULL);
6009 waddwstr(view->window, wline);
6010 if (tc)
6011 wattr_off(view->window,
6012 COLOR_PAIR(tc->colorpair), NULL);
6013 if (width < view->ncols - 1)
6014 waddch(view->window, '\n');
6015 if (n == s->selected && view->focussed)
6016 wstandend(view->window);
6017 free(line);
6018 free(wline);
6019 wline = NULL;
6020 n++;
6021 s->ndisplayed++;
6022 s->last_displayed_entry = re;
6024 limit--;
6025 re = TAILQ_NEXT(re, entry);
6028 view_vborder(view);
6029 return err;
6032 static const struct got_error *
6033 browse_ref_tree(struct tog_view **new_view, int begin_x,
6034 struct tog_reflist_entry *re, struct got_repository *repo)
6036 const struct got_error *err = NULL;
6037 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6038 struct got_tree_object *tree = NULL;
6039 struct tog_view *tree_view;
6041 *new_view = NULL;
6043 err = resolve_reflist_entry(&commit_id, re, repo);
6044 if (err) {
6045 if (err->code != GOT_ERR_OBJ_TYPE)
6046 return err;
6047 else
6048 return NULL;
6051 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6052 if (err)
6053 goto done;
6055 err = got_object_open_as_tree(&tree, repo, tree_id);
6056 if (err)
6057 goto done;
6059 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6060 if (tree_view == NULL) {
6061 err = got_error_from_errno("view_open");
6062 goto done;
6065 err = open_tree_view(tree_view, tree, commit_id, repo);
6066 if (err)
6067 goto done;
6069 *new_view = tree_view;
6070 done:
6071 free(commit_id);
6072 free(tree_id);
6073 if (err) {
6074 if (tree)
6075 got_object_tree_close(tree);
6077 return err;
6079 static const struct got_error *
6080 input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6081 struct tog_view **focus_view, struct tog_view *view, int ch)
6083 const struct got_error *err = NULL;
6084 struct tog_ref_view_state *s = &view->state.ref;
6085 struct tog_view *log_view, *tree_view;
6086 int begin_x = 0;
6088 switch (ch) {
6089 case 'i':
6090 s->show_ids = !s->show_ids;
6091 break;
6092 case KEY_ENTER:
6093 case '\r':
6094 if (!s->selected_entry)
6095 break;
6096 if (view_is_parent_view(view))
6097 begin_x = view_split_begin_x(view->begin_x);
6098 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6099 s->repo);
6100 if (view_is_parent_view(view)) {
6101 err = view_close_child(view);
6102 if (err)
6103 return err;
6104 err = view_set_child(view, log_view);
6105 if (err) {
6106 view_close(log_view);
6107 break;
6109 *focus_view = log_view;
6110 view->child_focussed = 1;
6111 } else
6112 *new_view = log_view;
6113 break;
6114 case 't':
6115 if (!s->selected_entry)
6116 break;
6117 if (view_is_parent_view(view))
6118 begin_x = view_split_begin_x(view->begin_x);
6119 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6120 s->repo);
6121 if (err || tree_view == NULL)
6122 break;
6123 if (view_is_parent_view(view)) {
6124 err = view_close_child(view);
6125 if (err)
6126 return err;
6127 err = view_set_child(view, tree_view);
6128 if (err) {
6129 view_close(tree_view);
6130 break;
6132 *focus_view = tree_view;
6133 view->child_focussed = 1;
6134 } else
6135 *new_view = tree_view;
6136 break;
6137 case 'k':
6138 case KEY_UP:
6139 if (s->selected > 0) {
6140 s->selected--;
6141 break;
6143 ref_scroll_up(s, 1);
6144 break;
6145 case KEY_PPAGE:
6146 case CTRL('b'):
6147 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6148 s->selected = 0;
6149 ref_scroll_up(s, MAX(0, view->nlines - 1));
6150 break;
6151 case 'j':
6152 case KEY_DOWN:
6153 if (s->selected < s->ndisplayed - 1) {
6154 s->selected++;
6155 break;
6157 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6158 /* can't scroll any further */
6159 break;
6160 ref_scroll_down(s, 1);
6161 break;
6162 case KEY_NPAGE:
6163 case CTRL('f'):
6164 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6165 /* can't scroll any further; move cursor down */
6166 if (s->selected < s->ndisplayed - 1)
6167 s->selected = s->ndisplayed - 1;
6168 break;
6170 ref_scroll_down(s, view->nlines - 1);
6171 break;
6172 case CTRL('l'):
6173 ref_view_free_refs(s);
6174 err = ref_view_load_refs(s);
6175 break;
6176 case KEY_RESIZE:
6177 if (s->selected > view->nlines)
6178 s->selected = s->ndisplayed - 1;
6179 break;
6180 default:
6181 break;
6184 return err;
6187 __dead static void
6188 usage_ref(void)
6190 endwin();
6191 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6192 getprogname());
6193 exit(1);
6196 static const struct got_error *
6197 cmd_ref(int argc, char *argv[])
6199 const struct got_error *error;
6200 struct got_repository *repo = NULL;
6201 struct got_worktree *worktree = NULL;
6202 char *cwd = NULL, *repo_path = NULL;
6203 int ch;
6204 struct tog_view *view;
6206 #ifndef PROFILE
6207 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6208 NULL) == -1)
6209 err(1, "pledge");
6210 #endif
6212 while ((ch = getopt(argc, argv, "r:")) != -1) {
6213 switch (ch) {
6214 case 'r':
6215 repo_path = realpath(optarg, NULL);
6216 if (repo_path == NULL)
6217 return got_error_from_errno2("realpath",
6218 optarg);
6219 break;
6220 default:
6221 usage_ref();
6222 /* NOTREACHED */
6226 argc -= optind;
6227 argv += optind;
6229 if (argc > 1)
6230 usage_ref();
6232 cwd = getcwd(NULL, 0);
6233 if (cwd == NULL)
6234 return got_error_from_errno("getcwd");
6236 error = got_worktree_open(&worktree, cwd);
6237 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6238 goto done;
6240 if (repo_path == NULL) {
6241 if (worktree)
6242 repo_path =
6243 strdup(got_worktree_get_repo_path(worktree));
6244 else
6245 repo_path = strdup(cwd);
6247 if (repo_path == NULL) {
6248 error = got_error_from_errno("strdup");
6249 goto done;
6252 error = got_repo_open(&repo, repo_path, NULL);
6253 if (error != NULL)
6254 goto done;
6256 init_curses();
6258 error = apply_unveil(got_repo_get_path(repo), NULL);
6259 if (error)
6260 goto done;
6262 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6263 if (view == NULL) {
6264 error = got_error_from_errno("view_open");
6265 goto done;
6268 error = open_ref_view(view, repo);
6269 if (error)
6270 goto done;
6272 if (worktree) {
6273 /* Release work tree lock. */
6274 got_worktree_close(worktree);
6275 worktree = NULL;
6277 error = view_loop(view);
6278 done:
6279 free(repo_path);
6280 free(cwd);
6281 if (repo)
6282 got_repo_close(repo);
6283 return error;
6286 static void
6287 list_commands(FILE *fp)
6289 int i;
6291 fprintf(fp, "commands:");
6292 for (i = 0; i < nitems(tog_commands); i++) {
6293 struct tog_cmd *cmd = &tog_commands[i];
6294 fprintf(fp, " %s", cmd->name);
6296 fputc('\n', fp);
6299 __dead static void
6300 usage(int hflag, int status)
6302 FILE *fp = (status == 0) ? stdout : stderr;
6304 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6305 getprogname());
6306 if (hflag) {
6307 fprintf(fp, "lazy usage: %s path\n", getprogname());
6308 list_commands(fp);
6310 exit(status);
6313 static char **
6314 make_argv(int argc, ...)
6316 va_list ap;
6317 char **argv;
6318 int i;
6320 va_start(ap, argc);
6322 argv = calloc(argc, sizeof(char *));
6323 if (argv == NULL)
6324 err(1, "calloc");
6325 for (i = 0; i < argc; i++) {
6326 argv[i] = strdup(va_arg(ap, char *));
6327 if (argv[i] == NULL)
6328 err(1, "strdup");
6331 va_end(ap);
6332 return argv;
6336 * Try to convert 'tog path' into a 'tog log path' command.
6337 * The user could simply have mistyped the command rather than knowingly
6338 * provided a path. So check whether argv[0] can in fact be resolved
6339 * to a path in the HEAD commit and print a special error if not.
6340 * This hack is for mpi@ <3
6342 static const struct got_error *
6343 tog_log_with_path(int argc, char *argv[])
6345 const struct got_error *error = NULL;
6346 struct tog_cmd *cmd = NULL;
6347 struct got_repository *repo = NULL;
6348 struct got_worktree *worktree = NULL;
6349 struct got_object_id *commit_id = NULL, *id = NULL;
6350 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6351 char *commit_id_str = NULL, **cmd_argv = NULL;
6353 cwd = getcwd(NULL, 0);
6354 if (cwd == NULL)
6355 return got_error_from_errno("getcwd");
6357 error = got_worktree_open(&worktree, cwd);
6358 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6359 goto done;
6361 if (worktree)
6362 repo_path = strdup(got_worktree_get_repo_path(worktree));
6363 else
6364 repo_path = strdup(cwd);
6365 if (repo_path == NULL) {
6366 error = got_error_from_errno("strdup");
6367 goto done;
6370 error = got_repo_open(&repo, repo_path, NULL);
6371 if (error != NULL)
6372 goto done;
6374 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6375 repo, worktree);
6376 if (error)
6377 goto done;
6379 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6380 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6381 GOT_OBJ_TYPE_COMMIT, 1, repo);
6382 if (error)
6383 goto done;
6385 if (worktree) {
6386 got_worktree_close(worktree);
6387 worktree = NULL;
6390 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6391 if (error) {
6392 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6393 goto done;
6394 fprintf(stderr, "%s: '%s' is no known command or path\n",
6395 getprogname(), argv[0]);
6396 usage(1, 1);
6397 /* not reached */
6400 got_repo_close(repo);
6401 repo = NULL;
6403 error = got_object_id_str(&commit_id_str, commit_id);
6404 if (error)
6405 goto done;
6407 cmd = &tog_commands[0]; /* log */
6408 argc = 4;
6409 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6410 error = cmd->cmd_main(argc, cmd_argv);
6411 done:
6412 if (repo)
6413 got_repo_close(repo);
6414 if (worktree)
6415 got_worktree_close(worktree);
6416 free(id);
6417 free(commit_id_str);
6418 free(commit_id);
6419 free(cwd);
6420 free(repo_path);
6421 free(in_repo_path);
6422 if (cmd_argv) {
6423 int i;
6424 for (i = 0; i < argc; i++)
6425 free(cmd_argv[i]);
6426 free(cmd_argv);
6428 return error;
6431 int
6432 main(int argc, char *argv[])
6434 const struct got_error *error = NULL;
6435 struct tog_cmd *cmd = NULL;
6436 int ch, hflag = 0, Vflag = 0;
6437 char **cmd_argv = NULL;
6438 static struct option longopts[] = {
6439 { "version", no_argument, NULL, 'V' },
6440 { NULL, 0, NULL, 0}
6443 setlocale(LC_CTYPE, "");
6445 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6446 switch (ch) {
6447 case 'h':
6448 hflag = 1;
6449 break;
6450 case 'V':
6451 Vflag = 1;
6452 break;
6453 default:
6454 usage(hflag, 1);
6455 /* NOTREACHED */
6459 argc -= optind;
6460 argv += optind;
6461 optind = 1;
6462 optreset = 1;
6464 if (Vflag) {
6465 got_version_print_str();
6466 return 0;
6469 if (argc == 0) {
6470 if (hflag)
6471 usage(hflag, 0);
6472 /* Build an argument vector which runs a default command. */
6473 cmd = &tog_commands[0];
6474 argc = 1;
6475 cmd_argv = make_argv(argc, cmd->name);
6476 } else {
6477 int i;
6479 /* Did the user specify a command? */
6480 for (i = 0; i < nitems(tog_commands); i++) {
6481 if (strncmp(tog_commands[i].name, argv[0],
6482 strlen(argv[0])) == 0) {
6483 cmd = &tog_commands[i];
6484 break;
6489 if (cmd == NULL) {
6490 if (argc != 1)
6491 usage(0, 1);
6492 /* No command specified; try log with a path */
6493 error = tog_log_with_path(argc, argv);
6494 } else {
6495 if (hflag)
6496 cmd->cmd_usage();
6497 else
6498 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6501 endwin();
6502 putchar('\n');
6503 if (cmd_argv) {
6504 int i;
6505 for (i = 0; i < argc; i++)
6506 free(cmd_argv[i]);
6507 free(cmd_argv);
6510 if (error && error->code != GOT_ERR_CANCELLED)
6511 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6512 return 0;