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_view *view, int maxscroll)
1688 struct tog_log_view_state *s = &view->state.log;
1689 struct commit_queue_entry *entry;
1690 int nscrolled = 0;
1692 entry = TAILQ_FIRST(&s->commits.head);
1693 if (s->first_displayed_entry == entry)
1694 return;
1696 entry = s->first_displayed_entry;
1697 while (entry && nscrolled < maxscroll) {
1698 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1699 if (entry) {
1700 s->first_displayed_entry = entry;
1701 nscrolled++;
1706 static const struct got_error *
1707 trigger_log_thread(struct tog_view *view, int wait)
1709 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1710 int errcode;
1712 halfdelay(1); /* fast refresh while loading commits */
1714 while (ta->commits_needed > 0) {
1715 if (ta->log_complete)
1716 break;
1718 /* Wake the log thread. */
1719 errcode = pthread_cond_signal(&ta->need_commits);
1720 if (errcode)
1721 return got_error_set_errno(errcode,
1722 "pthread_cond_signal");
1725 * The mutex will be released while the view loop waits
1726 * in wgetch(), at which time the log thread will run.
1728 if (!wait)
1729 break;
1731 /* Display progress update in log view. */
1732 show_log_view(view);
1733 update_panels();
1734 doupdate();
1736 /* Wait right here while next commit is being loaded. */
1737 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1738 if (errcode)
1739 return got_error_set_errno(errcode,
1740 "pthread_cond_wait");
1742 /* Display progress update in log view. */
1743 show_log_view(view);
1744 update_panels();
1745 doupdate();
1748 return NULL;
1751 static const struct got_error *
1752 log_scroll_down(struct tog_view *view, int maxscroll)
1754 struct tog_log_view_state *s = &view->state.log;
1755 const struct got_error *err = NULL;
1756 struct commit_queue_entry *pentry;
1757 int nscrolled = 0, ncommits_needed;
1759 if (s->last_displayed_entry == NULL)
1760 return NULL;
1762 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1763 if (s->commits.ncommits < ncommits_needed &&
1764 !s->thread_args.log_complete) {
1766 * Ask the log thread for required amount of commits.
1768 s->thread_args.commits_needed += maxscroll;
1769 err = trigger_log_thread(view, 1);
1770 if (err)
1771 return err;
1774 do {
1775 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1776 if (pentry == NULL)
1777 break;
1779 s->last_displayed_entry = pentry;
1781 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1782 if (pentry == NULL)
1783 break;
1784 s->first_displayed_entry = pentry;
1785 } while (++nscrolled < maxscroll);
1787 return err;
1790 static const struct got_error *
1791 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1792 struct got_commit_object *commit, struct got_object_id *commit_id,
1793 struct tog_view *log_view, struct got_repository *repo)
1795 const struct got_error *err;
1796 struct got_object_qid *parent_id;
1797 struct tog_view *diff_view;
1799 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1800 if (diff_view == NULL)
1801 return got_error_from_errno("view_open");
1803 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1804 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1805 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1806 if (err == NULL)
1807 *new_view = diff_view;
1808 return err;
1811 static const struct got_error *
1812 tree_view_visit_subtree(struct got_tree_object *subtree,
1813 struct tog_tree_view_state *s)
1815 struct tog_parent_tree *parent;
1817 parent = calloc(1, sizeof(*parent));
1818 if (parent == NULL)
1819 return got_error_from_errno("calloc");
1821 parent->tree = s->tree;
1822 parent->first_displayed_entry = s->first_displayed_entry;
1823 parent->selected_entry = s->selected_entry;
1824 parent->selected = s->selected;
1825 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1826 s->tree = subtree;
1827 s->selected = 0;
1828 s->first_displayed_entry = NULL;
1829 return NULL;
1832 static const struct got_error *
1833 tree_view_walk_path(struct tog_tree_view_state *s,
1834 struct got_object_id *commit_id, const char *path)
1836 const struct got_error *err = NULL;
1837 struct got_tree_object *tree = NULL;
1838 const char *p;
1839 char *slash, *subpath = NULL;
1841 /* Walk the path and open corresponding tree objects. */
1842 p = path;
1843 while (*p) {
1844 struct got_tree_entry *te;
1845 struct got_object_id *tree_id;
1846 char *te_name;
1848 while (p[0] == '/')
1849 p++;
1851 /* Ensure the correct subtree entry is selected. */
1852 slash = strchr(p, '/');
1853 if (slash == NULL)
1854 te_name = strdup(p);
1855 else
1856 te_name = strndup(p, slash - p);
1857 if (te_name == NULL) {
1858 err = got_error_from_errno("strndup");
1859 break;
1861 te = got_object_tree_find_entry(s->tree, te_name);
1862 if (te == NULL) {
1863 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1864 free(te_name);
1865 break;
1867 free(te_name);
1868 s->first_displayed_entry = s->selected_entry = te;
1870 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1871 break; /* jump to this file's entry */
1873 slash = strchr(p, '/');
1874 if (slash)
1875 subpath = strndup(path, slash - path);
1876 else
1877 subpath = strdup(path);
1878 if (subpath == NULL) {
1879 err = got_error_from_errno("strdup");
1880 break;
1883 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1884 subpath);
1885 if (err)
1886 break;
1888 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1889 free(tree_id);
1890 if (err)
1891 break;
1893 err = tree_view_visit_subtree(tree, s);
1894 if (err) {
1895 got_object_tree_close(tree);
1896 break;
1898 if (slash == NULL)
1899 break;
1900 free(subpath);
1901 subpath = NULL;
1902 p = slash;
1905 free(subpath);
1906 return err;
1909 static const struct got_error *
1910 browse_commit_tree(struct tog_view **new_view, int begin_x,
1911 struct commit_queue_entry *entry, const char *path,
1912 struct got_repository *repo)
1914 const struct got_error *err = NULL;
1915 struct got_tree_object *tree;
1916 struct tog_tree_view_state *s;
1917 struct tog_view *tree_view;
1919 err = got_object_open_as_tree(&tree, repo,
1920 got_object_commit_get_tree_id(entry->commit));
1921 if (err)
1922 return err;
1924 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1925 if (tree_view == NULL)
1926 return got_error_from_errno("view_open");
1928 err = open_tree_view(tree_view, tree, entry->id, repo);
1929 if (err) {
1930 got_object_tree_close(tree);
1931 return err;
1933 s = &tree_view->state.tree;
1935 *new_view = tree_view;
1937 if (got_path_is_root_dir(path))
1938 return NULL;
1940 return tree_view_walk_path(s, entry->id, path);
1943 static const struct got_error *
1944 block_signals_used_by_main_thread(void)
1946 sigset_t sigset;
1947 int errcode;
1949 if (sigemptyset(&sigset) == -1)
1950 return got_error_from_errno("sigemptyset");
1952 /* tog handles SIGWINCH and SIGCONT */
1953 if (sigaddset(&sigset, SIGWINCH) == -1)
1954 return got_error_from_errno("sigaddset");
1955 if (sigaddset(&sigset, SIGCONT) == -1)
1956 return got_error_from_errno("sigaddset");
1958 /* ncurses handles SIGTSTP */
1959 if (sigaddset(&sigset, SIGTSTP) == -1)
1960 return got_error_from_errno("sigaddset");
1962 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1963 if (errcode)
1964 return got_error_set_errno(errcode, "pthread_sigmask");
1966 return NULL;
1969 static void *
1970 log_thread(void *arg)
1972 const struct got_error *err = NULL;
1973 int errcode = 0;
1974 struct tog_log_thread_args *a = arg;
1975 int done = 0;
1977 err = block_signals_used_by_main_thread();
1978 if (err)
1979 return (void *)err;
1981 while (!done && !err && !tog_sigpipe_received) {
1982 err = queue_commits(a->graph, a->commits, 1, a->repo,
1983 a->in_repo_path, a->searching, a->search_next_done,
1984 a->regex);
1985 if (err) {
1986 if (err->code != GOT_ERR_ITER_COMPLETED)
1987 return (void *)err;
1988 err = NULL;
1989 done = 1;
1990 } else if (a->commits_needed > 0)
1991 a->commits_needed--;
1993 errcode = pthread_mutex_lock(&tog_mutex);
1994 if (errcode) {
1995 err = got_error_set_errno(errcode,
1996 "pthread_mutex_lock");
1997 break;
1998 } else if (*a->quit)
1999 done = 1;
2000 else if (*a->first_displayed_entry == NULL) {
2001 *a->first_displayed_entry =
2002 TAILQ_FIRST(&a->commits->head);
2003 *a->selected_entry = *a->first_displayed_entry;
2006 errcode = pthread_cond_signal(&a->commit_loaded);
2007 if (errcode) {
2008 err = got_error_set_errno(errcode,
2009 "pthread_cond_signal");
2010 pthread_mutex_unlock(&tog_mutex);
2011 break;
2014 if (done)
2015 a->commits_needed = 0;
2016 else {
2017 if (a->commits_needed == 0) {
2018 errcode = pthread_cond_wait(&a->need_commits,
2019 &tog_mutex);
2020 if (errcode)
2021 err = got_error_set_errno(errcode,
2022 "pthread_cond_wait");
2026 errcode = pthread_mutex_unlock(&tog_mutex);
2027 if (errcode && err == NULL)
2028 err = got_error_set_errno(errcode,
2029 "pthread_mutex_unlock");
2031 a->log_complete = 1;
2032 return (void *)err;
2035 static const struct got_error *
2036 stop_log_thread(struct tog_log_view_state *s)
2038 const struct got_error *err = NULL;
2039 int errcode;
2041 if (s->thread) {
2042 s->quit = 1;
2043 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2044 if (errcode)
2045 return got_error_set_errno(errcode,
2046 "pthread_cond_signal");
2047 errcode = pthread_mutex_unlock(&tog_mutex);
2048 if (errcode)
2049 return got_error_set_errno(errcode,
2050 "pthread_mutex_unlock");
2051 errcode = pthread_join(s->thread, (void **)&err);
2052 if (errcode)
2053 return got_error_set_errno(errcode, "pthread_join");
2054 errcode = pthread_mutex_lock(&tog_mutex);
2055 if (errcode)
2056 return got_error_set_errno(errcode,
2057 "pthread_mutex_lock");
2058 s->thread = NULL;
2061 if (s->thread_args.repo) {
2062 got_repo_close(s->thread_args.repo);
2063 s->thread_args.repo = NULL;
2066 if (s->thread_args.graph) {
2067 got_commit_graph_close(s->thread_args.graph);
2068 s->thread_args.graph = NULL;
2071 return err;
2074 static const struct got_error *
2075 close_log_view(struct tog_view *view)
2077 const struct got_error *err = NULL;
2078 struct tog_log_view_state *s = &view->state.log;
2079 int errcode;
2081 err = stop_log_thread(s);
2083 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2084 if (errcode && err == NULL)
2085 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2087 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2088 if (errcode && err == NULL)
2089 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2091 free_commits(&s->commits);
2092 free(s->in_repo_path);
2093 s->in_repo_path = NULL;
2094 free(s->start_id);
2095 s->start_id = NULL;
2096 got_ref_list_free(&s->refs);
2097 return err;
2100 static const struct got_error *
2101 search_start_log_view(struct tog_view *view)
2103 struct tog_log_view_state *s = &view->state.log;
2105 s->matched_entry = NULL;
2106 s->search_entry = NULL;
2107 return NULL;
2110 static const struct got_error *
2111 search_next_log_view(struct tog_view *view)
2113 const struct got_error *err = NULL;
2114 struct tog_log_view_state *s = &view->state.log;
2115 struct commit_queue_entry *entry;
2117 /* Display progress update in log view. */
2118 show_log_view(view);
2119 update_panels();
2120 doupdate();
2122 if (s->search_entry) {
2123 int errcode, ch;
2124 errcode = pthread_mutex_unlock(&tog_mutex);
2125 if (errcode)
2126 return got_error_set_errno(errcode,
2127 "pthread_mutex_unlock");
2128 ch = wgetch(view->window);
2129 errcode = pthread_mutex_lock(&tog_mutex);
2130 if (errcode)
2131 return got_error_set_errno(errcode,
2132 "pthread_mutex_lock");
2133 if (ch == KEY_BACKSPACE) {
2134 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2135 return NULL;
2137 if (view->searching == TOG_SEARCH_FORWARD)
2138 entry = TAILQ_NEXT(s->search_entry, entry);
2139 else
2140 entry = TAILQ_PREV(s->search_entry,
2141 commit_queue_head, entry);
2142 } else if (s->matched_entry) {
2143 if (view->searching == TOG_SEARCH_FORWARD)
2144 entry = TAILQ_NEXT(s->matched_entry, entry);
2145 else
2146 entry = TAILQ_PREV(s->matched_entry,
2147 commit_queue_head, entry);
2148 } else {
2149 if (view->searching == TOG_SEARCH_FORWARD)
2150 entry = TAILQ_FIRST(&s->commits.head);
2151 else
2152 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2155 while (1) {
2156 int have_match = 0;
2158 if (entry == NULL) {
2159 if (s->thread_args.log_complete ||
2160 view->searching == TOG_SEARCH_BACKWARD) {
2161 view->search_next_done =
2162 (s->matched_entry == NULL ?
2163 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2164 s->search_entry = NULL;
2165 return NULL;
2168 * Poke the log thread for more commits and return,
2169 * allowing the main loop to make progress. Search
2170 * will resume at s->search_entry once we come back.
2172 s->thread_args.commits_needed++;
2173 return trigger_log_thread(view, 0);
2176 err = match_commit(&have_match, entry->id, entry->commit,
2177 &view->regex);
2178 if (err)
2179 break;
2180 if (have_match) {
2181 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2182 s->matched_entry = entry;
2183 break;
2186 s->search_entry = entry;
2187 if (view->searching == TOG_SEARCH_FORWARD)
2188 entry = TAILQ_NEXT(entry, entry);
2189 else
2190 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2193 if (s->matched_entry) {
2194 int cur = s->selected_entry->idx;
2195 while (cur < s->matched_entry->idx) {
2196 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2197 if (err)
2198 return err;
2199 cur++;
2201 while (cur > s->matched_entry->idx) {
2202 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2203 if (err)
2204 return err;
2205 cur--;
2209 s->search_entry = NULL;
2211 return NULL;
2214 static const struct got_error *
2215 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2216 struct got_repository *repo, const char *head_ref_name,
2217 const char *in_repo_path, int log_branches)
2219 const struct got_error *err = NULL;
2220 struct tog_log_view_state *s = &view->state.log;
2221 struct got_repository *thread_repo = NULL;
2222 struct got_commit_graph *thread_graph = NULL;
2223 int errcode;
2225 SIMPLEQ_INIT(&s->refs);
2227 if (in_repo_path != s->in_repo_path) {
2228 free(s->in_repo_path);
2229 s->in_repo_path = strdup(in_repo_path);
2230 if (s->in_repo_path == NULL)
2231 return got_error_from_errno("strdup");
2234 /* The commit queue only contains commits being displayed. */
2235 TAILQ_INIT(&s->commits.head);
2236 s->commits.ncommits = 0;
2238 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2239 if (err)
2240 goto done;
2242 s->repo = repo;
2243 s->head_ref_name = head_ref_name;
2244 s->start_id = got_object_id_dup(start_id);
2245 if (s->start_id == NULL) {
2246 err = got_error_from_errno("got_object_id_dup");
2247 goto done;
2249 s->log_branches = log_branches;
2251 SIMPLEQ_INIT(&s->colors);
2252 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2253 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2254 get_color_value("TOG_COLOR_COMMIT"));
2255 if (err)
2256 goto done;
2257 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2258 get_color_value("TOG_COLOR_AUTHOR"));
2259 if (err) {
2260 free_colors(&s->colors);
2261 goto done;
2263 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2264 get_color_value("TOG_COLOR_DATE"));
2265 if (err) {
2266 free_colors(&s->colors);
2267 goto done;
2271 view->show = show_log_view;
2272 view->input = input_log_view;
2273 view->close = close_log_view;
2274 view->search_start = search_start_log_view;
2275 view->search_next = search_next_log_view;
2277 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2278 if (err)
2279 goto done;
2280 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2281 !s->log_branches);
2282 if (err)
2283 goto done;
2284 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2285 s->repo, NULL, NULL);
2286 if (err)
2287 goto done;
2289 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2290 if (errcode) {
2291 err = got_error_set_errno(errcode, "pthread_cond_init");
2292 goto done;
2294 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2295 if (errcode) {
2296 err = got_error_set_errno(errcode, "pthread_cond_init");
2297 goto done;
2300 s->thread_args.commits_needed = view->nlines;
2301 s->thread_args.graph = thread_graph;
2302 s->thread_args.commits = &s->commits;
2303 s->thread_args.in_repo_path = s->in_repo_path;
2304 s->thread_args.start_id = s->start_id;
2305 s->thread_args.repo = thread_repo;
2306 s->thread_args.log_complete = 0;
2307 s->thread_args.quit = &s->quit;
2308 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2309 s->thread_args.selected_entry = &s->selected_entry;
2310 s->thread_args.searching = &view->searching;
2311 s->thread_args.search_next_done = &view->search_next_done;
2312 s->thread_args.regex = &view->regex;
2313 done:
2314 if (err)
2315 close_log_view(view);
2316 return err;
2319 static const struct got_error *
2320 show_log_view(struct tog_view *view)
2322 const struct got_error *err;
2323 struct tog_log_view_state *s = &view->state.log;
2325 if (s->thread == NULL) {
2326 int errcode = pthread_create(&s->thread, NULL, log_thread,
2327 &s->thread_args);
2328 if (errcode)
2329 return got_error_set_errno(errcode, "pthread_create");
2330 if (s->thread_args.commits_needed > 0) {
2331 err = trigger_log_thread(view, 1);
2332 if (err)
2333 return err;
2337 return draw_commits(view);
2340 static const struct got_error *
2341 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2342 struct tog_view **focus_view, struct tog_view *view, int ch)
2344 const struct got_error *err = NULL;
2345 struct tog_log_view_state *s = &view->state.log;
2346 char *parent_path, *in_repo_path = NULL;
2347 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2348 struct tog_view *ref_view = NULL;
2349 int begin_x = 0;
2350 struct got_object_id *start_id;
2352 switch (ch) {
2353 case 'q':
2354 s->quit = 1;
2355 break;
2356 case 'k':
2357 case KEY_UP:
2358 case '<':
2359 case ',':
2360 if (s->first_displayed_entry == NULL)
2361 break;
2362 if (s->selected > 0)
2363 s->selected--;
2364 else
2365 log_scroll_up(view, 1);
2366 break;
2367 case KEY_PPAGE:
2368 case CTRL('b'):
2369 if (s->first_displayed_entry == NULL)
2370 break;
2371 if (TAILQ_FIRST(&s->commits.head) ==
2372 s->first_displayed_entry) {
2373 s->selected = 0;
2374 break;
2376 log_scroll_up(view, view->nlines - 1);
2377 break;
2378 case 'j':
2379 case KEY_DOWN:
2380 case '>':
2381 case '.':
2382 if (s->first_displayed_entry == NULL)
2383 break;
2384 if (s->selected < MIN(view->nlines - 2,
2385 s->commits.ncommits - 1)) {
2386 s->selected++;
2387 break;
2389 err = log_scroll_down(view, 1);
2390 break;
2391 case KEY_NPAGE:
2392 case CTRL('f'): {
2393 struct commit_queue_entry *first;
2394 first = s->first_displayed_entry;
2395 if (first == NULL)
2396 break;
2397 err = log_scroll_down(view, view->nlines - 1);
2398 if (err)
2399 break;
2400 if (first == s->first_displayed_entry &&
2401 s->selected < MIN(view->nlines - 2,
2402 s->commits.ncommits - 1)) {
2403 /* can't scroll further down */
2404 s->selected = MIN(view->nlines - 2,
2405 s->commits.ncommits - 1);
2407 err = NULL;
2408 break;
2410 case KEY_RESIZE:
2411 if (s->selected > view->nlines - 2)
2412 s->selected = view->nlines - 2;
2413 if (s->selected > s->commits.ncommits - 1)
2414 s->selected = s->commits.ncommits - 1;
2415 break;
2416 case KEY_ENTER:
2417 case ' ':
2418 case '\r':
2419 if (s->selected_entry == NULL)
2420 break;
2421 if (view_is_parent_view(view))
2422 begin_x = view_split_begin_x(view->begin_x);
2423 err = open_diff_view_for_commit(&diff_view, begin_x,
2424 s->selected_entry->commit, s->selected_entry->id,
2425 view, s->repo);
2426 if (err)
2427 break;
2428 if (view_is_parent_view(view)) {
2429 err = view_close_child(view);
2430 if (err)
2431 return err;
2432 err = view_set_child(view, diff_view);
2433 if (err) {
2434 view_close(diff_view);
2435 break;
2437 *focus_view = diff_view;
2438 view->child_focussed = 1;
2439 } else
2440 *new_view = diff_view;
2441 break;
2442 case 't':
2443 if (s->selected_entry == NULL)
2444 break;
2445 if (view_is_parent_view(view))
2446 begin_x = view_split_begin_x(view->begin_x);
2447 err = browse_commit_tree(&tree_view, begin_x,
2448 s->selected_entry, s->in_repo_path, s->repo);
2449 if (err)
2450 break;
2451 if (view_is_parent_view(view)) {
2452 err = view_close_child(view);
2453 if (err)
2454 return err;
2455 err = view_set_child(view, tree_view);
2456 if (err) {
2457 view_close(tree_view);
2458 break;
2460 *focus_view = tree_view;
2461 view->child_focussed = 1;
2462 } else
2463 *new_view = tree_view;
2464 break;
2465 case KEY_BACKSPACE:
2466 if (got_path_cmp(s->in_repo_path, "/",
2467 strlen(s->in_repo_path), 1) == 0)
2468 break;
2469 err = got_path_dirname(&parent_path, s->in_repo_path);
2470 if (err)
2471 return err;
2472 err = stop_log_thread(s);
2473 if (err) {
2474 free(parent_path);
2475 return err;
2477 lv = view_open(view->nlines, view->ncols,
2478 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2479 if (lv == NULL) {
2480 free(parent_path);
2481 return got_error_from_errno("view_open");
2483 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2484 parent_path, s->log_branches);
2485 free(parent_path);
2486 if (err)
2487 return err;;
2488 if (view_is_parent_view(view))
2489 *new_view = lv;
2490 else {
2491 view_set_child(view->parent, lv);
2492 *focus_view = lv;
2494 break;
2495 case CTRL('l'):
2496 err = stop_log_thread(s);
2497 if (err)
2498 return err;
2499 lv = view_open(view->nlines, view->ncols,
2500 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2501 if (lv == NULL)
2502 return got_error_from_errno("view_open");
2503 err = got_repo_match_object_id(&start_id, NULL,
2504 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2505 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2506 if (err) {
2507 view_close(lv);
2508 return err;
2510 in_repo_path = strdup(s->in_repo_path);
2511 if (in_repo_path == NULL) {
2512 free(start_id);
2513 view_close(lv);
2514 return got_error_from_errno("strdup");
2516 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2517 in_repo_path, s->log_branches);
2518 if (err) {
2519 free(start_id);
2520 view_close(lv);
2521 return err;;
2523 *dead_view = view;
2524 *new_view = lv;
2525 break;
2526 case 'B':
2527 s->log_branches = !s->log_branches;
2528 err = stop_log_thread(s);
2529 if (err)
2530 return err;
2531 lv = view_open(view->nlines, view->ncols,
2532 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2533 if (lv == NULL)
2534 return got_error_from_errno("view_open");
2535 err = open_log_view(lv, s->start_id, s->repo,
2536 s->head_ref_name, s->in_repo_path, s->log_branches);
2537 if (err) {
2538 view_close(lv);
2539 return err;;
2541 *dead_view = view;
2542 *new_view = lv;
2543 break;
2544 case 'r':
2545 if (view_is_parent_view(view))
2546 begin_x = view_split_begin_x(view->begin_x);
2547 ref_view = view_open(view->nlines, view->ncols,
2548 view->begin_y, begin_x, TOG_VIEW_REF);
2549 if (ref_view == NULL)
2550 return got_error_from_errno("view_open");
2551 err = open_ref_view(ref_view, s->repo);
2552 if (err) {
2553 view_close(ref_view);
2554 return err;
2556 if (view_is_parent_view(view)) {
2557 err = view_close_child(view);
2558 if (err)
2559 return err;
2560 err = view_set_child(view, ref_view);
2561 if (err) {
2562 view_close(ref_view);
2563 break;
2565 *focus_view = ref_view;
2566 view->child_focussed = 1;
2567 } else
2568 *new_view = ref_view;
2569 break;
2570 default:
2571 break;
2574 return err;
2577 static const struct got_error *
2578 apply_unveil(const char *repo_path, const char *worktree_path)
2580 const struct got_error *error;
2582 #ifdef PROFILE
2583 if (unveil("gmon.out", "rwc") != 0)
2584 return got_error_from_errno2("unveil", "gmon.out");
2585 #endif
2586 if (repo_path && unveil(repo_path, "r") != 0)
2587 return got_error_from_errno2("unveil", repo_path);
2589 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2590 return got_error_from_errno2("unveil", worktree_path);
2592 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2593 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2595 error = got_privsep_unveil_exec_helpers();
2596 if (error != NULL)
2597 return error;
2599 if (unveil(NULL, NULL) != 0)
2600 return got_error_from_errno("unveil");
2602 return NULL;
2605 static void
2606 init_curses(void)
2608 initscr();
2609 cbreak();
2610 halfdelay(1); /* Do fast refresh while initial view is loading. */
2611 noecho();
2612 nonl();
2613 intrflush(stdscr, FALSE);
2614 keypad(stdscr, TRUE);
2615 curs_set(0);
2616 if (getenv("TOG_COLORS") != NULL) {
2617 start_color();
2618 use_default_colors();
2620 signal(SIGWINCH, tog_sigwinch);
2621 signal(SIGPIPE, tog_sigpipe);
2622 signal(SIGCONT, tog_sigcont);
2625 static const struct got_error *
2626 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2627 struct got_repository *repo, struct got_worktree *worktree)
2629 const struct got_error *err = NULL;
2631 if (argc == 0) {
2632 *in_repo_path = strdup("/");
2633 if (*in_repo_path == NULL)
2634 return got_error_from_errno("strdup");
2635 return NULL;
2638 if (worktree) {
2639 const char *prefix = got_worktree_get_path_prefix(worktree);
2640 char *p;
2642 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2643 if (err)
2644 return err;
2645 if (asprintf(in_repo_path, "%s%s%s", prefix,
2646 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2647 p) == -1) {
2648 err = got_error_from_errno("asprintf");
2649 *in_repo_path = NULL;
2651 free(p);
2652 } else
2653 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2655 return err;
2658 static const struct got_error *
2659 cmd_log(int argc, char *argv[])
2661 const struct got_error *error;
2662 struct got_repository *repo = NULL;
2663 struct got_worktree *worktree = NULL;
2664 struct got_object_id *start_id = NULL;
2665 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2666 char *start_commit = NULL, *head_ref_name = NULL;
2667 int ch, log_branches = 0;
2668 struct tog_view *view;
2670 #ifndef PROFILE
2671 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2672 NULL) == -1)
2673 err(1, "pledge");
2674 #endif
2676 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2677 switch (ch) {
2678 case 'b':
2679 log_branches = 1;
2680 break;
2681 case 'c':
2682 start_commit = optarg;
2683 break;
2684 case 'r':
2685 repo_path = realpath(optarg, NULL);
2686 if (repo_path == NULL)
2687 return got_error_from_errno2("realpath",
2688 optarg);
2689 break;
2690 default:
2691 usage_log();
2692 /* NOTREACHED */
2696 argc -= optind;
2697 argv += optind;
2699 if (argc > 1)
2700 usage_log();
2702 cwd = getcwd(NULL, 0);
2703 if (cwd == NULL)
2704 return got_error_from_errno("getcwd");
2706 error = got_worktree_open(&worktree, cwd);
2707 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2708 goto done;
2710 if (repo_path == NULL) {
2711 if (worktree)
2712 repo_path =
2713 strdup(got_worktree_get_repo_path(worktree));
2714 else
2715 repo_path = strdup(cwd);
2717 if (repo_path == NULL) {
2718 error = got_error_from_errno("strdup");
2719 goto done;
2722 error = got_repo_open(&repo, repo_path, NULL);
2723 if (error != NULL)
2724 goto done;
2726 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2727 repo, worktree);
2728 if (error)
2729 goto done;
2731 init_curses();
2733 error = apply_unveil(got_repo_get_path(repo),
2734 worktree ? got_worktree_get_root_path(worktree) : NULL);
2735 if (error)
2736 goto done;
2738 if (start_commit == NULL)
2739 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2740 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2741 GOT_OBJ_TYPE_COMMIT, 1, repo);
2742 else
2743 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2744 GOT_OBJ_TYPE_COMMIT, 1, repo);
2745 if (error != NULL)
2746 goto done;
2748 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2749 if (view == NULL) {
2750 error = got_error_from_errno("view_open");
2751 goto done;
2753 if (worktree) {
2754 head_ref_name = strdup(
2755 got_worktree_get_head_ref_name(worktree));
2756 if (head_ref_name == NULL) {
2757 error = got_error_from_errno("strdup");
2758 goto done;
2761 error = open_log_view(view, start_id, repo, head_ref_name,
2762 in_repo_path, log_branches);
2763 if (error)
2764 goto done;
2765 if (worktree) {
2766 /* Release work tree lock. */
2767 got_worktree_close(worktree);
2768 worktree = NULL;
2770 error = view_loop(view);
2771 done:
2772 free(in_repo_path);
2773 free(repo_path);
2774 free(cwd);
2775 free(start_id);
2776 free(head_ref_name);
2777 if (repo)
2778 got_repo_close(repo);
2779 if (worktree)
2780 got_worktree_close(worktree);
2781 return error;
2784 __dead static void
2785 usage_diff(void)
2787 endwin();
2788 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2789 "[-w] object1 object2\n", getprogname());
2790 exit(1);
2793 static char *
2794 parse_next_line(FILE *f, size_t *len)
2796 char *line;
2797 size_t linelen;
2798 size_t lineno;
2799 const char delim[3] = { '\0', '\0', '\0'};
2801 line = fparseln(f, &linelen, &lineno, delim, 0);
2802 if (len)
2803 *len = linelen;
2804 return line;
2807 static int
2808 match_line(const char *line, regex_t *regex, size_t nmatch,
2809 regmatch_t *regmatch)
2811 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2814 struct tog_color *
2815 match_color(struct tog_colors *colors, const char *line)
2817 struct tog_color *tc = NULL;
2819 SIMPLEQ_FOREACH(tc, colors, entry) {
2820 if (match_line(line, &tc->regex, 0, NULL))
2821 return tc;
2824 return NULL;
2827 static const struct got_error *
2828 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2829 WINDOW *window, regmatch_t *regmatch)
2831 const struct got_error *err = NULL;
2832 wchar_t *wline;
2833 int width;
2834 char *s;
2836 *wtotal = 0;
2838 s = strndup(line, regmatch->rm_so);
2839 if (s == NULL)
2840 return got_error_from_errno("strndup");
2842 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2843 if (err) {
2844 free(s);
2845 return err;
2847 waddwstr(window, wline);
2848 free(wline);
2849 free(s);
2850 wlimit -= width;
2851 *wtotal += width;
2853 if (wlimit > 0) {
2854 s = strndup(line + regmatch->rm_so,
2855 regmatch->rm_eo - regmatch->rm_so);
2856 if (s == NULL) {
2857 err = got_error_from_errno("strndup");
2858 free(s);
2859 return err;
2861 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2862 if (err) {
2863 free(s);
2864 return err;
2866 wattr_on(window, A_STANDOUT, NULL);
2867 waddwstr(window, wline);
2868 wattr_off(window, A_STANDOUT, NULL);
2869 free(wline);
2870 free(s);
2871 wlimit -= width;
2872 *wtotal += width;
2875 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2876 err = format_line(&wline, &width,
2877 line + regmatch->rm_eo, wlimit, col_tab_align);
2878 if (err)
2879 return err;
2880 waddwstr(window, wline);
2881 free(wline);
2882 *wtotal += width;
2885 return NULL;
2888 static const struct got_error *
2889 draw_file(struct tog_view *view, const char *header)
2891 struct tog_diff_view_state *s = &view->state.diff;
2892 regmatch_t *regmatch = &view->regmatch;
2893 const struct got_error *err;
2894 int nprinted = 0;
2895 char *line;
2896 struct tog_color *tc;
2897 size_t len;
2898 wchar_t *wline;
2899 int width;
2900 int max_lines = view->nlines;
2901 int nlines = s->nlines;
2902 off_t line_offset;
2904 line_offset = s->line_offsets[s->first_displayed_line - 1];
2905 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2906 return got_error_from_errno("fseek");
2908 werase(view->window);
2910 if (header) {
2911 if (asprintf(&line, "[%d/%d] %s",
2912 s->first_displayed_line - 1 + s->selected_line, nlines,
2913 header) == -1)
2914 return got_error_from_errno("asprintf");
2915 err = format_line(&wline, &width, line, view->ncols, 0);
2916 free(line);
2917 if (err)
2918 return err;
2920 if (view_needs_focus_indication(view))
2921 wstandout(view->window);
2922 waddwstr(view->window, wline);
2923 free(wline);
2924 wline = NULL;
2925 if (view_needs_focus_indication(view))
2926 wstandend(view->window);
2927 if (width <= view->ncols - 1)
2928 waddch(view->window, '\n');
2930 if (max_lines <= 1)
2931 return NULL;
2932 max_lines--;
2935 s->eof = 0;
2936 while (max_lines > 0 && nprinted < max_lines) {
2937 line = parse_next_line(s->f, &len);
2938 if (line == NULL) {
2939 s->eof = 1;
2940 break;
2943 tc = match_color(&s->colors, line);
2944 if (tc)
2945 wattr_on(view->window,
2946 COLOR_PAIR(tc->colorpair), NULL);
2947 if (s->first_displayed_line + nprinted == s->matched_line &&
2948 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2949 err = add_matched_line(&width, line, view->ncols, 0,
2950 view->window, regmatch);
2951 if (err) {
2952 free(line);
2953 return err;
2955 } else {
2956 err = format_line(&wline, &width, line, view->ncols, 0);
2957 if (err) {
2958 free(line);
2959 return err;
2961 waddwstr(view->window, wline);
2962 free(wline);
2963 wline = NULL;
2965 if (tc)
2966 wattr_off(view->window,
2967 COLOR_PAIR(tc->colorpair), NULL);
2968 if (width <= view->ncols - 1)
2969 waddch(view->window, '\n');
2970 nprinted++;
2971 free(line);
2973 if (nprinted >= 1)
2974 s->last_displayed_line = s->first_displayed_line +
2975 (nprinted - 1);
2976 else
2977 s->last_displayed_line = s->first_displayed_line;
2979 view_vborder(view);
2981 if (s->eof) {
2982 while (nprinted < view->nlines) {
2983 waddch(view->window, '\n');
2984 nprinted++;
2987 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2988 if (err) {
2989 return err;
2992 wstandout(view->window);
2993 waddwstr(view->window, wline);
2994 free(wline);
2995 wline = NULL;
2996 wstandend(view->window);
2999 return NULL;
3002 static char *
3003 get_datestr(time_t *time, char *datebuf)
3005 struct tm mytm, *tm;
3006 char *p, *s;
3008 tm = gmtime_r(time, &mytm);
3009 if (tm == NULL)
3010 return NULL;
3011 s = asctime_r(tm, datebuf);
3012 if (s == NULL)
3013 return NULL;
3014 p = strchr(s, '\n');
3015 if (p)
3016 *p = '\0';
3017 return s;
3020 static const struct got_error *
3021 get_changed_paths(struct got_pathlist_head *paths,
3022 struct got_commit_object *commit, struct got_repository *repo)
3024 const struct got_error *err = NULL;
3025 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3026 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3027 struct got_object_qid *qid;
3029 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3030 if (qid != NULL) {
3031 struct got_commit_object *pcommit;
3032 err = got_object_open_as_commit(&pcommit, repo,
3033 qid->id);
3034 if (err)
3035 return err;
3037 tree_id1 = got_object_commit_get_tree_id(pcommit);
3038 got_object_commit_close(pcommit);
3042 if (tree_id1) {
3043 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3044 if (err)
3045 goto done;
3048 tree_id2 = got_object_commit_get_tree_id(commit);
3049 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3050 if (err)
3051 goto done;
3053 err = got_diff_tree(tree1, tree2, "", "", repo,
3054 got_diff_tree_collect_changed_paths, paths, 0);
3055 done:
3056 if (tree1)
3057 got_object_tree_close(tree1);
3058 if (tree2)
3059 got_object_tree_close(tree2);
3060 return err;
3063 static const struct got_error *
3064 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3066 off_t *p;
3068 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3069 if (p == NULL)
3070 return got_error_from_errno("reallocarray");
3071 *line_offsets = p;
3072 (*line_offsets)[*nlines] = off;
3073 (*nlines)++;
3074 return NULL;
3077 static const struct got_error *
3078 write_commit_info(off_t **line_offsets, size_t *nlines,
3079 struct got_object_id *commit_id, struct got_reflist_head *refs,
3080 struct got_repository *repo, FILE *outfile)
3082 const struct got_error *err = NULL;
3083 char datebuf[26], *datestr;
3084 struct got_commit_object *commit;
3085 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3086 time_t committer_time;
3087 const char *author, *committer;
3088 char *refs_str = NULL;
3089 struct got_pathlist_head changed_paths;
3090 struct got_pathlist_entry *pe;
3091 off_t outoff = 0;
3092 int n;
3094 TAILQ_INIT(&changed_paths);
3096 if (refs) {
3097 err = build_refs_str(&refs_str, refs, commit_id, repo);
3098 if (err)
3099 return err;
3102 err = got_object_open_as_commit(&commit, repo, commit_id);
3103 if (err)
3104 return err;
3106 err = got_object_id_str(&id_str, commit_id);
3107 if (err) {
3108 err = got_error_from_errno("got_object_id_str");
3109 goto done;
3112 err = add_line_offset(line_offsets, nlines, 0);
3113 if (err)
3114 goto done;
3116 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3117 refs_str ? refs_str : "", refs_str ? ")" : "");
3118 if (n < 0) {
3119 err = got_error_from_errno("fprintf");
3120 goto done;
3122 outoff += n;
3123 err = add_line_offset(line_offsets, nlines, outoff);
3124 if (err)
3125 goto done;
3127 n = fprintf(outfile, "from: %s\n",
3128 got_object_commit_get_author(commit));
3129 if (n < 0) {
3130 err = got_error_from_errno("fprintf");
3131 goto done;
3133 outoff += n;
3134 err = add_line_offset(line_offsets, nlines, outoff);
3135 if (err)
3136 goto done;
3138 committer_time = got_object_commit_get_committer_time(commit);
3139 datestr = get_datestr(&committer_time, datebuf);
3140 if (datestr) {
3141 n = fprintf(outfile, "date: %s UTC\n", datestr);
3142 if (n < 0) {
3143 err = got_error_from_errno("fprintf");
3144 goto done;
3146 outoff += n;
3147 err = add_line_offset(line_offsets, nlines, outoff);
3148 if (err)
3149 goto done;
3151 author = got_object_commit_get_author(commit);
3152 committer = got_object_commit_get_committer(commit);
3153 if (strcmp(author, committer) != 0) {
3154 n = fprintf(outfile, "via: %s\n", committer);
3155 if (n < 0) {
3156 err = got_error_from_errno("fprintf");
3157 goto done;
3159 outoff += n;
3160 err = add_line_offset(line_offsets, nlines, outoff);
3161 if (err)
3162 goto done;
3164 err = got_object_commit_get_logmsg(&logmsg, commit);
3165 if (err)
3166 goto done;
3167 s = logmsg;
3168 while ((line = strsep(&s, "\n")) != NULL) {
3169 n = fprintf(outfile, "%s\n", line);
3170 if (n < 0) {
3171 err = got_error_from_errno("fprintf");
3172 goto done;
3174 outoff += n;
3175 err = add_line_offset(line_offsets, nlines, outoff);
3176 if (err)
3177 goto done;
3180 err = get_changed_paths(&changed_paths, commit, repo);
3181 if (err)
3182 goto done;
3183 TAILQ_FOREACH(pe, &changed_paths, entry) {
3184 struct got_diff_changed_path *cp = pe->data;
3185 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3186 if (n < 0) {
3187 err = got_error_from_errno("fprintf");
3188 goto done;
3190 outoff += n;
3191 err = add_line_offset(line_offsets, nlines, outoff);
3192 if (err)
3193 goto done;
3194 free((char *)pe->path);
3195 free(pe->data);
3198 fputc('\n', outfile);
3199 outoff++;
3200 err = add_line_offset(line_offsets, nlines, outoff);
3201 done:
3202 got_pathlist_free(&changed_paths);
3203 free(id_str);
3204 free(logmsg);
3205 free(refs_str);
3206 got_object_commit_close(commit);
3207 if (err) {
3208 free(*line_offsets);
3209 *line_offsets = NULL;
3210 *nlines = 0;
3212 return err;
3215 static const struct got_error *
3216 create_diff(struct tog_diff_view_state *s)
3218 const struct got_error *err = NULL;
3219 FILE *f = NULL;
3220 int obj_type;
3222 free(s->line_offsets);
3223 s->line_offsets = malloc(sizeof(off_t));
3224 if (s->line_offsets == NULL)
3225 return got_error_from_errno("malloc");
3226 s->nlines = 0;
3228 f = got_opentemp();
3229 if (f == NULL) {
3230 err = got_error_from_errno("got_opentemp");
3231 goto done;
3233 if (s->f && fclose(s->f) != 0) {
3234 err = got_error_from_errno("fclose");
3235 goto done;
3237 s->f = f;
3239 if (s->id1)
3240 err = got_object_get_type(&obj_type, s->repo, s->id1);
3241 else
3242 err = got_object_get_type(&obj_type, s->repo, s->id2);
3243 if (err)
3244 goto done;
3246 switch (obj_type) {
3247 case GOT_OBJ_TYPE_BLOB:
3248 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3249 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3250 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3251 break;
3252 case GOT_OBJ_TYPE_TREE:
3253 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3254 s->id1, s->id2, "", "", s->diff_context,
3255 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3256 break;
3257 case GOT_OBJ_TYPE_COMMIT: {
3258 const struct got_object_id_queue *parent_ids;
3259 struct got_object_qid *pid;
3260 struct got_commit_object *commit2;
3262 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3263 if (err)
3264 goto done;
3265 /* Show commit info if we're diffing to a parent/root commit. */
3266 if (s->id1 == NULL) {
3267 err = write_commit_info(&s->line_offsets, &s->nlines,
3268 s->id2, &s->refs, s->repo, s->f);
3269 if (err)
3270 goto done;
3271 } else {
3272 parent_ids = got_object_commit_get_parent_ids(commit2);
3273 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3274 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3275 err = write_commit_info(
3276 &s->line_offsets, &s->nlines,
3277 s->id2, &s->refs, s->repo, s->f);
3278 if (err)
3279 goto done;
3280 break;
3284 got_object_commit_close(commit2);
3286 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3287 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3288 s->force_text_diff, s->repo, s->f);
3289 break;
3291 default:
3292 err = got_error(GOT_ERR_OBJ_TYPE);
3293 break;
3295 if (err)
3296 goto done;
3297 done:
3298 if (s->f && fflush(s->f) != 0 && err == NULL)
3299 err = got_error_from_errno("fflush");
3300 return err;
3303 static void
3304 diff_view_indicate_progress(struct tog_view *view)
3306 mvwaddstr(view->window, 0, 0, "diffing...");
3307 update_panels();
3308 doupdate();
3311 static const struct got_error *
3312 search_start_diff_view(struct tog_view *view)
3314 struct tog_diff_view_state *s = &view->state.diff;
3316 s->matched_line = 0;
3317 return NULL;
3320 static const struct got_error *
3321 search_next_diff_view(struct tog_view *view)
3323 struct tog_diff_view_state *s = &view->state.diff;
3324 int lineno;
3326 if (!view->searching) {
3327 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3328 return NULL;
3331 if (s->matched_line) {
3332 if (view->searching == TOG_SEARCH_FORWARD)
3333 lineno = s->matched_line + 1;
3334 else
3335 lineno = s->matched_line - 1;
3336 } else {
3337 if (view->searching == TOG_SEARCH_FORWARD)
3338 lineno = 1;
3339 else
3340 lineno = s->nlines;
3343 while (1) {
3344 char *line = NULL;
3345 off_t offset;
3346 size_t len;
3348 if (lineno <= 0 || lineno > s->nlines) {
3349 if (s->matched_line == 0) {
3350 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3351 free(line);
3352 break;
3355 if (view->searching == TOG_SEARCH_FORWARD)
3356 lineno = 1;
3357 else
3358 lineno = s->nlines;
3361 offset = s->line_offsets[lineno - 1];
3362 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3363 free(line);
3364 return got_error_from_errno("fseeko");
3366 free(line);
3367 line = parse_next_line(s->f, &len);
3368 if (line &&
3369 match_line(line, &view->regex, 1, &view->regmatch)) {
3370 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3371 s->matched_line = lineno;
3372 free(line);
3373 break;
3375 free(line);
3376 if (view->searching == TOG_SEARCH_FORWARD)
3377 lineno++;
3378 else
3379 lineno--;
3382 if (s->matched_line) {
3383 s->first_displayed_line = s->matched_line;
3384 s->selected_line = 1;
3387 return NULL;
3390 static const struct got_error *
3391 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3392 struct got_object_id *id2, const char *label1, const char *label2,
3393 int diff_context, int ignore_whitespace, int force_text_diff,
3394 struct tog_view *log_view, struct got_repository *repo)
3396 const struct got_error *err;
3397 struct tog_diff_view_state *s = &view->state.diff;
3399 SIMPLEQ_INIT(&s->refs);
3401 if (id1 != NULL && id2 != NULL) {
3402 int type1, type2;
3403 err = got_object_get_type(&type1, repo, id1);
3404 if (err)
3405 return err;
3406 err = got_object_get_type(&type2, repo, id2);
3407 if (err)
3408 return err;
3410 if (type1 != type2)
3411 return got_error(GOT_ERR_OBJ_TYPE);
3413 s->first_displayed_line = 1;
3414 s->last_displayed_line = view->nlines;
3415 s->selected_line = 1;
3416 s->repo = repo;
3417 s->id1 = id1;
3418 s->id2 = id2;
3419 s->label1 = label1;
3420 s->label2 = label2;
3422 if (id1) {
3423 s->id1 = got_object_id_dup(id1);
3424 if (s->id1 == NULL)
3425 return got_error_from_errno("got_object_id_dup");
3426 } else
3427 s->id1 = NULL;
3429 s->id2 = got_object_id_dup(id2);
3430 if (s->id2 == NULL) {
3431 free(s->id1);
3432 s->id1 = NULL;
3433 return got_error_from_errno("got_object_id_dup");
3435 s->f = NULL;
3436 s->first_displayed_line = 1;
3437 s->last_displayed_line = view->nlines;
3438 s->diff_context = diff_context;
3439 s->ignore_whitespace = ignore_whitespace;
3440 s->force_text_diff = force_text_diff;
3441 s->log_view = log_view;
3442 s->repo = repo;
3444 SIMPLEQ_INIT(&s->colors);
3445 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3446 err = add_color(&s->colors,
3447 "^-", TOG_COLOR_DIFF_MINUS,
3448 get_color_value("TOG_COLOR_DIFF_MINUS"));
3449 if (err)
3450 return err;
3451 err = add_color(&s->colors, "^\\+",
3452 TOG_COLOR_DIFF_PLUS,
3453 get_color_value("TOG_COLOR_DIFF_PLUS"));
3454 if (err) {
3455 free_colors(&s->colors);
3456 return err;
3458 err = add_color(&s->colors,
3459 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3460 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3461 if (err) {
3462 free_colors(&s->colors);
3463 return err;
3466 err = add_color(&s->colors,
3467 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3468 TOG_COLOR_DIFF_META,
3469 get_color_value("TOG_COLOR_DIFF_META"));
3470 if (err) {
3471 free_colors(&s->colors);
3472 return err;
3475 err = add_color(&s->colors,
3476 "^(from|via): ", TOG_COLOR_AUTHOR,
3477 get_color_value("TOG_COLOR_AUTHOR"));
3478 if (err) {
3479 free_colors(&s->colors);
3480 return err;
3483 err = add_color(&s->colors,
3484 "^date: ", TOG_COLOR_DATE,
3485 get_color_value("TOG_COLOR_DATE"));
3486 if (err) {
3487 free_colors(&s->colors);
3488 return err;
3492 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3493 if (err) {
3494 free(s->id1);
3495 s->id1 = NULL;
3496 free(s->id2);
3497 s->id2 = NULL;
3498 free_colors(&s->colors);
3499 return err;
3502 if (log_view && view_is_splitscreen(view))
3503 show_log_view(log_view); /* draw vborder */
3504 diff_view_indicate_progress(view);
3506 s->line_offsets = NULL;
3507 s->nlines = 0;
3508 err = create_diff(s);
3509 if (err) {
3510 free(s->id1);
3511 s->id1 = NULL;
3512 free(s->id2);
3513 s->id2 = NULL;
3514 free_colors(&s->colors);
3515 got_ref_list_free(&s->refs);
3516 return err;
3519 view->show = show_diff_view;
3520 view->input = input_diff_view;
3521 view->close = close_diff_view;
3522 view->search_start = search_start_diff_view;
3523 view->search_next = search_next_diff_view;
3525 return NULL;
3528 static const struct got_error *
3529 close_diff_view(struct tog_view *view)
3531 const struct got_error *err = NULL;
3532 struct tog_diff_view_state *s = &view->state.diff;
3534 free(s->id1);
3535 s->id1 = NULL;
3536 free(s->id2);
3537 s->id2 = NULL;
3538 if (s->f && fclose(s->f) == EOF)
3539 err = got_error_from_errno("fclose");
3540 free_colors(&s->colors);
3541 free(s->line_offsets);
3542 s->line_offsets = NULL;
3543 s->nlines = 0;
3544 got_ref_list_free(&s->refs);
3545 return err;
3548 static const struct got_error *
3549 show_diff_view(struct tog_view *view)
3551 const struct got_error *err;
3552 struct tog_diff_view_state *s = &view->state.diff;
3553 char *id_str1 = NULL, *id_str2, *header;
3554 const char *label1, *label2;
3556 if (s->id1) {
3557 err = got_object_id_str(&id_str1, s->id1);
3558 if (err)
3559 return err;
3560 label1 = s->label1 ? : id_str1;
3561 } else
3562 label1 = "/dev/null";
3564 err = got_object_id_str(&id_str2, s->id2);
3565 if (err)
3566 return err;
3567 label2 = s->label2 ? : id_str2;
3569 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3570 err = got_error_from_errno("asprintf");
3571 free(id_str1);
3572 free(id_str2);
3573 return err;
3575 free(id_str1);
3576 free(id_str2);
3578 return draw_file(view, header);
3581 static const struct got_error *
3582 set_selected_commit(struct tog_diff_view_state *s,
3583 struct commit_queue_entry *entry)
3585 const struct got_error *err;
3586 const struct got_object_id_queue *parent_ids;
3587 struct got_commit_object *selected_commit;
3588 struct got_object_qid *pid;
3590 free(s->id2);
3591 s->id2 = got_object_id_dup(entry->id);
3592 if (s->id2 == NULL)
3593 return got_error_from_errno("got_object_id_dup");
3595 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3596 if (err)
3597 return err;
3598 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3599 free(s->id1);
3600 pid = SIMPLEQ_FIRST(parent_ids);
3601 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3602 got_object_commit_close(selected_commit);
3603 return NULL;
3606 static const struct got_error *
3607 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3608 struct tog_view **focus_view, struct tog_view *view, int ch)
3610 const struct got_error *err = NULL;
3611 struct tog_diff_view_state *s = &view->state.diff;
3612 struct tog_log_view_state *ls;
3613 struct commit_queue_entry *entry;
3614 int i;
3616 switch (ch) {
3617 case 'a':
3618 case 'w':
3619 if (ch == 'a')
3620 s->force_text_diff = !s->force_text_diff;
3621 if (ch == 'w')
3622 s->ignore_whitespace = !s->ignore_whitespace;
3623 wclear(view->window);
3624 s->first_displayed_line = 1;
3625 s->last_displayed_line = view->nlines;
3626 diff_view_indicate_progress(view);
3627 err = create_diff(s);
3628 break;
3629 case 'k':
3630 case KEY_UP:
3631 if (s->first_displayed_line > 1)
3632 s->first_displayed_line--;
3633 break;
3634 case KEY_PPAGE:
3635 case CTRL('b'):
3636 if (s->first_displayed_line == 1)
3637 break;
3638 i = 0;
3639 while (i++ < view->nlines - 1 &&
3640 s->first_displayed_line > 1)
3641 s->first_displayed_line--;
3642 break;
3643 case 'j':
3644 case KEY_DOWN:
3645 if (!s->eof)
3646 s->first_displayed_line++;
3647 break;
3648 case KEY_NPAGE:
3649 case CTRL('f'):
3650 case ' ':
3651 if (s->eof)
3652 break;
3653 i = 0;
3654 while (!s->eof && i++ < view->nlines - 1) {
3655 char *line;
3656 line = parse_next_line(s->f, NULL);
3657 s->first_displayed_line++;
3658 if (line == NULL)
3659 break;
3661 break;
3662 case '[':
3663 if (s->diff_context > 0) {
3664 s->diff_context--;
3665 diff_view_indicate_progress(view);
3666 err = create_diff(s);
3667 if (s->first_displayed_line + view->nlines - 1 >
3668 s->nlines) {
3669 s->first_displayed_line = 1;
3670 s->last_displayed_line = view->nlines;
3673 break;
3674 case ']':
3675 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3676 s->diff_context++;
3677 diff_view_indicate_progress(view);
3678 err = create_diff(s);
3680 break;
3681 case '<':
3682 case ',':
3683 if (s->log_view == NULL)
3684 break;
3685 ls = &s->log_view->state.log;
3686 entry = TAILQ_PREV(ls->selected_entry,
3687 commit_queue_head, entry);
3688 if (entry == NULL)
3689 break;
3691 err = input_log_view(NULL, NULL, NULL, s->log_view,
3692 KEY_UP);
3693 if (err)
3694 break;
3696 err = set_selected_commit(s, entry);
3697 if (err)
3698 break;
3700 s->first_displayed_line = 1;
3701 s->last_displayed_line = view->nlines;
3703 diff_view_indicate_progress(view);
3704 err = create_diff(s);
3705 break;
3706 case '>':
3707 case '.':
3708 if (s->log_view == NULL)
3709 break;
3710 ls = &s->log_view->state.log;
3712 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3713 ls->thread_args.commits_needed++;
3714 err = trigger_log_thread(s->log_view, 1);
3715 if (err)
3716 break;
3718 err = input_log_view(NULL, NULL, NULL, s->log_view,
3719 KEY_DOWN);
3720 if (err)
3721 break;
3723 entry = TAILQ_NEXT(ls->selected_entry, entry);
3724 if (entry == NULL)
3725 break;
3727 err = set_selected_commit(s, entry);
3728 if (err)
3729 break;
3731 s->first_displayed_line = 1;
3732 s->last_displayed_line = view->nlines;
3734 diff_view_indicate_progress(view);
3735 err = create_diff(s);
3736 break;
3737 default:
3738 break;
3741 return err;
3744 static const struct got_error *
3745 cmd_diff(int argc, char *argv[])
3747 const struct got_error *error = NULL;
3748 struct got_repository *repo = NULL;
3749 struct got_worktree *worktree = NULL;
3750 struct got_object_id *id1 = NULL, *id2 = NULL;
3751 char *repo_path = NULL, *cwd = NULL;
3752 char *id_str1 = NULL, *id_str2 = NULL;
3753 char *label1 = NULL, *label2 = NULL;
3754 int diff_context = 3, ignore_whitespace = 0;
3755 int ch, force_text_diff = 0;
3756 const char *errstr;
3757 struct tog_view *view;
3759 #ifndef PROFILE
3760 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3761 NULL) == -1)
3762 err(1, "pledge");
3763 #endif
3764 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3765 switch (ch) {
3766 case 'a':
3767 force_text_diff = 1;
3768 break;
3769 case 'C':
3770 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3771 &errstr);
3772 if (errstr != NULL)
3773 err(1, "-C option %s", errstr);
3774 break;
3775 case 'r':
3776 repo_path = realpath(optarg, NULL);
3777 if (repo_path == NULL)
3778 return got_error_from_errno2("realpath",
3779 optarg);
3780 got_path_strip_trailing_slashes(repo_path);
3781 break;
3782 case 'w':
3783 ignore_whitespace = 1;
3784 break;
3785 default:
3786 usage_diff();
3787 /* NOTREACHED */
3791 argc -= optind;
3792 argv += optind;
3794 if (argc == 0) {
3795 usage_diff(); /* TODO show local worktree changes */
3796 } else if (argc == 2) {
3797 id_str1 = argv[0];
3798 id_str2 = argv[1];
3799 } else
3800 usage_diff();
3802 cwd = getcwd(NULL, 0);
3803 if (cwd == NULL)
3804 return got_error_from_errno("getcwd");
3806 error = got_worktree_open(&worktree, cwd);
3807 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3808 goto done;
3810 if (repo_path == NULL) {
3811 if (worktree)
3812 repo_path =
3813 strdup(got_worktree_get_repo_path(worktree));
3814 else
3815 repo_path = strdup(cwd);
3817 if (repo_path == NULL) {
3818 error = got_error_from_errno("strdup");
3819 goto done;
3822 error = got_repo_open(&repo, repo_path, NULL);
3823 if (error)
3824 goto done;
3826 init_curses();
3828 error = apply_unveil(got_repo_get_path(repo), NULL);
3829 if (error)
3830 goto done;
3832 error = got_repo_match_object_id(&id1, &label1, id_str1,
3833 GOT_OBJ_TYPE_ANY, 1, repo);
3834 if (error)
3835 goto done;
3837 error = got_repo_match_object_id(&id2, &label2, id_str2,
3838 GOT_OBJ_TYPE_ANY, 1, repo);
3839 if (error)
3840 goto done;
3842 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3843 if (view == NULL) {
3844 error = got_error_from_errno("view_open");
3845 goto done;
3847 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3848 ignore_whitespace, force_text_diff, NULL, repo);
3849 if (error)
3850 goto done;
3851 error = view_loop(view);
3852 done:
3853 free(label1);
3854 free(label2);
3855 free(repo_path);
3856 free(cwd);
3857 if (repo)
3858 got_repo_close(repo);
3859 if (worktree)
3860 got_worktree_close(worktree);
3861 return error;
3864 __dead static void
3865 usage_blame(void)
3867 endwin();
3868 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3869 getprogname());
3870 exit(1);
3873 struct tog_blame_line {
3874 int annotated;
3875 struct got_object_id *id;
3878 static const struct got_error *
3879 draw_blame(struct tog_view *view)
3881 struct tog_blame_view_state *s = &view->state.blame;
3882 struct tog_blame *blame = &s->blame;
3883 regmatch_t *regmatch = &view->regmatch;
3884 const struct got_error *err;
3885 int lineno = 0, nprinted = 0;
3886 char *line;
3887 size_t len;
3888 wchar_t *wline;
3889 int width;
3890 struct tog_blame_line *blame_line;
3891 struct got_object_id *prev_id = NULL;
3892 char *id_str;
3893 struct tog_color *tc;
3895 err = got_object_id_str(&id_str, s->blamed_commit->id);
3896 if (err)
3897 return err;
3899 rewind(blame->f);
3900 werase(view->window);
3902 if (asprintf(&line, "commit %s", id_str) == -1) {
3903 err = got_error_from_errno("asprintf");
3904 free(id_str);
3905 return err;
3908 err = format_line(&wline, &width, line, view->ncols, 0);
3909 free(line);
3910 line = NULL;
3911 if (err)
3912 return err;
3913 if (view_needs_focus_indication(view))
3914 wstandout(view->window);
3915 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3916 if (tc)
3917 wattr_on(view->window,
3918 COLOR_PAIR(tc->colorpair), NULL);
3919 waddwstr(view->window, wline);
3920 if (tc)
3921 wattr_off(view->window,
3922 COLOR_PAIR(tc->colorpair), NULL);
3923 if (view_needs_focus_indication(view))
3924 wstandend(view->window);
3925 free(wline);
3926 wline = NULL;
3927 if (width < view->ncols - 1)
3928 waddch(view->window, '\n');
3930 if (asprintf(&line, "[%d/%d] %s%s",
3931 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3932 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3933 free(id_str);
3934 return got_error_from_errno("asprintf");
3936 free(id_str);
3937 err = format_line(&wline, &width, line, view->ncols, 0);
3938 free(line);
3939 line = NULL;
3940 if (err)
3941 return err;
3942 waddwstr(view->window, wline);
3943 free(wline);
3944 wline = NULL;
3945 if (width < view->ncols - 1)
3946 waddch(view->window, '\n');
3948 s->eof = 0;
3949 while (nprinted < view->nlines - 2) {
3950 line = parse_next_line(blame->f, &len);
3951 if (line == NULL) {
3952 s->eof = 1;
3953 break;
3955 if (++lineno < s->first_displayed_line) {
3956 free(line);
3957 continue;
3960 if (view->focussed && nprinted == s->selected_line - 1)
3961 wstandout(view->window);
3963 if (blame->nlines > 0) {
3964 blame_line = &blame->lines[lineno - 1];
3965 if (blame_line->annotated && prev_id &&
3966 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3967 !(view->focussed &&
3968 nprinted == s->selected_line - 1)) {
3969 waddstr(view->window, " ");
3970 } else if (blame_line->annotated) {
3971 char *id_str;
3972 err = got_object_id_str(&id_str, blame_line->id);
3973 if (err) {
3974 free(line);
3975 return err;
3977 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3978 if (tc)
3979 wattr_on(view->window,
3980 COLOR_PAIR(tc->colorpair), NULL);
3981 wprintw(view->window, "%.8s", id_str);
3982 if (tc)
3983 wattr_off(view->window,
3984 COLOR_PAIR(tc->colorpair), NULL);
3985 free(id_str);
3986 prev_id = blame_line->id;
3987 } else {
3988 waddstr(view->window, "........");
3989 prev_id = NULL;
3991 } else {
3992 waddstr(view->window, "........");
3993 prev_id = NULL;
3996 if (view->focussed && nprinted == s->selected_line - 1)
3997 wstandend(view->window);
3998 waddstr(view->window, " ");
4000 if (view->ncols <= 9) {
4001 width = 9;
4002 wline = wcsdup(L"");
4003 if (wline == NULL) {
4004 err = got_error_from_errno("wcsdup");
4005 free(line);
4006 return err;
4008 } else if (s->first_displayed_line + nprinted ==
4009 s->matched_line &&
4010 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4011 err = add_matched_line(&width, line, view->ncols - 9, 9,
4012 view->window, regmatch);
4013 if (err) {
4014 free(line);
4015 return err;
4017 width += 9;
4018 } else {
4019 err = format_line(&wline, &width, line,
4020 view->ncols - 9, 9);
4021 waddwstr(view->window, wline);
4022 free(wline);
4023 wline = NULL;
4024 width += 9;
4027 if (width <= view->ncols - 1)
4028 waddch(view->window, '\n');
4029 if (++nprinted == 1)
4030 s->first_displayed_line = lineno;
4031 free(line);
4033 s->last_displayed_line = lineno;
4035 view_vborder(view);
4037 return NULL;
4040 static const struct got_error *
4041 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4043 const struct got_error *err = NULL;
4044 struct tog_blame_cb_args *a = arg;
4045 struct tog_blame_line *line;
4046 int errcode;
4048 if (nlines != a->nlines ||
4049 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4050 return got_error(GOT_ERR_RANGE);
4052 errcode = pthread_mutex_lock(&tog_mutex);
4053 if (errcode)
4054 return got_error_set_errno(errcode, "pthread_mutex_lock");
4056 if (*a->quit) { /* user has quit the blame view */
4057 err = got_error(GOT_ERR_ITER_COMPLETED);
4058 goto done;
4061 if (lineno == -1)
4062 goto done; /* no change in this commit */
4064 line = &a->lines[lineno - 1];
4065 if (line->annotated)
4066 goto done;
4068 line->id = got_object_id_dup(id);
4069 if (line->id == NULL) {
4070 err = got_error_from_errno("got_object_id_dup");
4071 goto done;
4073 line->annotated = 1;
4074 done:
4075 errcode = pthread_mutex_unlock(&tog_mutex);
4076 if (errcode)
4077 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4078 return err;
4081 static void *
4082 blame_thread(void *arg)
4084 const struct got_error *err;
4085 struct tog_blame_thread_args *ta = arg;
4086 struct tog_blame_cb_args *a = ta->cb_args;
4087 int errcode;
4089 err = block_signals_used_by_main_thread();
4090 if (err)
4091 return (void *)err;
4093 err = got_blame(ta->path, a->commit_id, ta->repo,
4094 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4095 if (err && err->code == GOT_ERR_CANCELLED)
4096 err = NULL;
4098 errcode = pthread_mutex_lock(&tog_mutex);
4099 if (errcode)
4100 return (void *)got_error_set_errno(errcode,
4101 "pthread_mutex_lock");
4103 got_repo_close(ta->repo);
4104 ta->repo = NULL;
4105 *ta->complete = 1;
4107 errcode = pthread_mutex_unlock(&tog_mutex);
4108 if (errcode && err == NULL)
4109 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4111 return (void *)err;
4114 static struct got_object_id *
4115 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4116 int first_displayed_line, int selected_line)
4118 struct tog_blame_line *line;
4120 if (nlines <= 0)
4121 return NULL;
4123 line = &lines[first_displayed_line - 1 + selected_line - 1];
4124 if (!line->annotated)
4125 return NULL;
4127 return line->id;
4130 static const struct got_error *
4131 stop_blame(struct tog_blame *blame)
4133 const struct got_error *err = NULL;
4134 int i;
4136 if (blame->thread) {
4137 int errcode;
4138 errcode = pthread_mutex_unlock(&tog_mutex);
4139 if (errcode)
4140 return got_error_set_errno(errcode,
4141 "pthread_mutex_unlock");
4142 errcode = pthread_join(blame->thread, (void **)&err);
4143 if (errcode)
4144 return got_error_set_errno(errcode, "pthread_join");
4145 errcode = pthread_mutex_lock(&tog_mutex);
4146 if (errcode)
4147 return got_error_set_errno(errcode,
4148 "pthread_mutex_lock");
4149 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4150 err = NULL;
4151 blame->thread = NULL;
4153 if (blame->thread_args.repo) {
4154 got_repo_close(blame->thread_args.repo);
4155 blame->thread_args.repo = NULL;
4157 if (blame->f) {
4158 if (fclose(blame->f) != 0 && err == NULL)
4159 err = got_error_from_errno("fclose");
4160 blame->f = NULL;
4162 if (blame->lines) {
4163 for (i = 0; i < blame->nlines; i++)
4164 free(blame->lines[i].id);
4165 free(blame->lines);
4166 blame->lines = NULL;
4168 free(blame->cb_args.commit_id);
4169 blame->cb_args.commit_id = NULL;
4171 return err;
4174 static const struct got_error *
4175 cancel_blame_view(void *arg)
4177 const struct got_error *err = NULL;
4178 int *done = arg;
4179 int errcode;
4181 errcode = pthread_mutex_lock(&tog_mutex);
4182 if (errcode)
4183 return got_error_set_errno(errcode,
4184 "pthread_mutex_unlock");
4186 if (*done)
4187 err = got_error(GOT_ERR_CANCELLED);
4189 errcode = pthread_mutex_unlock(&tog_mutex);
4190 if (errcode)
4191 return got_error_set_errno(errcode,
4192 "pthread_mutex_lock");
4194 return err;
4197 static const struct got_error *
4198 run_blame(struct tog_view *view)
4200 struct tog_blame_view_state *s = &view->state.blame;
4201 struct tog_blame *blame = &s->blame;
4202 const struct got_error *err = NULL;
4203 struct got_blob_object *blob = NULL;
4204 struct got_repository *thread_repo = NULL;
4205 struct got_object_id *obj_id = NULL;
4206 int obj_type;
4208 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4209 s->path);
4210 if (err)
4211 return err;
4213 err = got_object_get_type(&obj_type, s->repo, obj_id);
4214 if (err)
4215 goto done;
4217 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4218 err = got_error(GOT_ERR_OBJ_TYPE);
4219 goto done;
4222 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4223 if (err)
4224 goto done;
4225 blame->f = got_opentemp();
4226 if (blame->f == NULL) {
4227 err = got_error_from_errno("got_opentemp");
4228 goto done;
4230 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4231 &blame->line_offsets, blame->f, blob);
4232 if (err || blame->nlines == 0)
4233 goto done;
4235 /* Don't include \n at EOF in the blame line count. */
4236 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4237 blame->nlines--;
4239 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4240 if (blame->lines == NULL) {
4241 err = got_error_from_errno("calloc");
4242 goto done;
4245 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4246 if (err)
4247 goto done;
4249 blame->cb_args.view = view;
4250 blame->cb_args.lines = blame->lines;
4251 blame->cb_args.nlines = blame->nlines;
4252 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4253 if (blame->cb_args.commit_id == NULL) {
4254 err = got_error_from_errno("got_object_id_dup");
4255 goto done;
4257 blame->cb_args.quit = &s->done;
4259 blame->thread_args.path = s->path;
4260 blame->thread_args.repo = thread_repo;
4261 blame->thread_args.cb_args = &blame->cb_args;
4262 blame->thread_args.complete = &s->blame_complete;
4263 blame->thread_args.cancel_cb = cancel_blame_view;
4264 blame->thread_args.cancel_arg = &s->done;
4265 s->blame_complete = 0;
4267 done:
4268 if (blob)
4269 got_object_blob_close(blob);
4270 free(obj_id);
4271 if (err)
4272 stop_blame(blame);
4273 return err;
4276 static const struct got_error *
4277 open_blame_view(struct tog_view *view, char *path,
4278 struct got_object_id *commit_id, struct got_repository *repo)
4280 const struct got_error *err = NULL;
4281 struct tog_blame_view_state *s = &view->state.blame;
4283 SIMPLEQ_INIT(&s->blamed_commits);
4285 s->path = strdup(path);
4286 if (s->path == NULL)
4287 return got_error_from_errno("strdup");
4289 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4290 if (err) {
4291 free(s->path);
4292 return err;
4295 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4296 s->first_displayed_line = 1;
4297 s->last_displayed_line = view->nlines;
4298 s->selected_line = 1;
4299 s->blame_complete = 0;
4300 s->repo = repo;
4301 s->commit_id = commit_id;
4302 memset(&s->blame, 0, sizeof(s->blame));
4304 SIMPLEQ_INIT(&s->colors);
4305 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4306 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4307 get_color_value("TOG_COLOR_COMMIT"));
4308 if (err)
4309 return err;
4312 view->show = show_blame_view;
4313 view->input = input_blame_view;
4314 view->close = close_blame_view;
4315 view->search_start = search_start_blame_view;
4316 view->search_next = search_next_blame_view;
4318 return run_blame(view);
4321 static const struct got_error *
4322 close_blame_view(struct tog_view *view)
4324 const struct got_error *err = NULL;
4325 struct tog_blame_view_state *s = &view->state.blame;
4327 if (s->blame.thread)
4328 err = stop_blame(&s->blame);
4330 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4331 struct got_object_qid *blamed_commit;
4332 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4333 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4334 got_object_qid_free(blamed_commit);
4337 free(s->path);
4338 free_colors(&s->colors);
4340 return err;
4343 static const struct got_error *
4344 search_start_blame_view(struct tog_view *view)
4346 struct tog_blame_view_state *s = &view->state.blame;
4348 s->matched_line = 0;
4349 return NULL;
4352 static const struct got_error *
4353 search_next_blame_view(struct tog_view *view)
4355 struct tog_blame_view_state *s = &view->state.blame;
4356 int lineno;
4358 if (!view->searching) {
4359 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4360 return NULL;
4363 if (s->matched_line) {
4364 if (view->searching == TOG_SEARCH_FORWARD)
4365 lineno = s->matched_line + 1;
4366 else
4367 lineno = s->matched_line - 1;
4368 } else {
4369 if (view->searching == TOG_SEARCH_FORWARD)
4370 lineno = 1;
4371 else
4372 lineno = s->blame.nlines;
4375 while (1) {
4376 char *line = NULL;
4377 off_t offset;
4378 size_t len;
4380 if (lineno <= 0 || lineno > s->blame.nlines) {
4381 if (s->matched_line == 0) {
4382 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4383 free(line);
4384 break;
4387 if (view->searching == TOG_SEARCH_FORWARD)
4388 lineno = 1;
4389 else
4390 lineno = s->blame.nlines;
4393 offset = s->blame.line_offsets[lineno - 1];
4394 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4395 free(line);
4396 return got_error_from_errno("fseeko");
4398 free(line);
4399 line = parse_next_line(s->blame.f, &len);
4400 if (line &&
4401 match_line(line, &view->regex, 1, &view->regmatch)) {
4402 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4403 s->matched_line = lineno;
4404 free(line);
4405 break;
4407 free(line);
4408 if (view->searching == TOG_SEARCH_FORWARD)
4409 lineno++;
4410 else
4411 lineno--;
4414 if (s->matched_line) {
4415 s->first_displayed_line = s->matched_line;
4416 s->selected_line = 1;
4419 return NULL;
4422 static const struct got_error *
4423 show_blame_view(struct tog_view *view)
4425 const struct got_error *err = NULL;
4426 struct tog_blame_view_state *s = &view->state.blame;
4427 int errcode;
4429 if (s->blame.thread == NULL) {
4430 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4431 &s->blame.thread_args);
4432 if (errcode)
4433 return got_error_set_errno(errcode, "pthread_create");
4435 halfdelay(1); /* fast refresh while annotating */
4438 if (s->blame_complete)
4439 halfdelay(10); /* disable fast refresh */
4441 err = draw_blame(view);
4443 view_vborder(view);
4444 return err;
4447 static const struct got_error *
4448 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4449 struct tog_view **focus_view, struct tog_view *view, int ch)
4451 const struct got_error *err = NULL, *thread_err = NULL;
4452 struct tog_view *diff_view;
4453 struct tog_blame_view_state *s = &view->state.blame;
4454 int begin_x = 0;
4456 switch (ch) {
4457 case 'q':
4458 s->done = 1;
4459 break;
4460 case 'k':
4461 case KEY_UP:
4462 if (s->selected_line > 1)
4463 s->selected_line--;
4464 else if (s->selected_line == 1 &&
4465 s->first_displayed_line > 1)
4466 s->first_displayed_line--;
4467 break;
4468 case KEY_PPAGE:
4469 case CTRL('b'):
4470 if (s->first_displayed_line == 1) {
4471 s->selected_line = 1;
4472 break;
4474 if (s->first_displayed_line > view->nlines - 2)
4475 s->first_displayed_line -=
4476 (view->nlines - 2);
4477 else
4478 s->first_displayed_line = 1;
4479 break;
4480 case 'j':
4481 case KEY_DOWN:
4482 if (s->selected_line < view->nlines - 2 &&
4483 s->first_displayed_line +
4484 s->selected_line <= s->blame.nlines)
4485 s->selected_line++;
4486 else if (s->last_displayed_line <
4487 s->blame.nlines)
4488 s->first_displayed_line++;
4489 break;
4490 case 'b':
4491 case 'p': {
4492 struct got_object_id *id = NULL;
4493 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4494 s->first_displayed_line, s->selected_line);
4495 if (id == NULL)
4496 break;
4497 if (ch == 'p') {
4498 struct got_commit_object *commit;
4499 struct got_object_qid *pid;
4500 struct got_object_id *blob_id = NULL;
4501 int obj_type;
4502 err = got_object_open_as_commit(&commit,
4503 s->repo, id);
4504 if (err)
4505 break;
4506 pid = SIMPLEQ_FIRST(
4507 got_object_commit_get_parent_ids(commit));
4508 if (pid == NULL) {
4509 got_object_commit_close(commit);
4510 break;
4512 /* Check if path history ends here. */
4513 err = got_object_id_by_path(&blob_id, s->repo,
4514 pid->id, s->path);
4515 if (err) {
4516 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4517 err = NULL;
4518 got_object_commit_close(commit);
4519 break;
4521 err = got_object_get_type(&obj_type, s->repo,
4522 blob_id);
4523 free(blob_id);
4524 /* Can't blame non-blob type objects. */
4525 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4526 got_object_commit_close(commit);
4527 break;
4529 err = got_object_qid_alloc(&s->blamed_commit,
4530 pid->id);
4531 got_object_commit_close(commit);
4532 } else {
4533 if (got_object_id_cmp(id,
4534 s->blamed_commit->id) == 0)
4535 break;
4536 err = got_object_qid_alloc(&s->blamed_commit,
4537 id);
4539 if (err)
4540 break;
4541 s->done = 1;
4542 thread_err = stop_blame(&s->blame);
4543 s->done = 0;
4544 if (thread_err)
4545 break;
4546 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4547 s->blamed_commit, entry);
4548 err = run_blame(view);
4549 if (err)
4550 break;
4551 break;
4553 case 'B': {
4554 struct got_object_qid *first;
4555 first = SIMPLEQ_FIRST(&s->blamed_commits);
4556 if (!got_object_id_cmp(first->id, s->commit_id))
4557 break;
4558 s->done = 1;
4559 thread_err = stop_blame(&s->blame);
4560 s->done = 0;
4561 if (thread_err)
4562 break;
4563 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4564 got_object_qid_free(s->blamed_commit);
4565 s->blamed_commit =
4566 SIMPLEQ_FIRST(&s->blamed_commits);
4567 err = run_blame(view);
4568 if (err)
4569 break;
4570 break;
4572 case KEY_ENTER:
4573 case '\r': {
4574 struct got_object_id *id = NULL;
4575 struct got_object_qid *pid;
4576 struct got_commit_object *commit = NULL;
4577 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4578 s->first_displayed_line, s->selected_line);
4579 if (id == NULL)
4580 break;
4581 err = got_object_open_as_commit(&commit, s->repo, id);
4582 if (err)
4583 break;
4584 pid = SIMPLEQ_FIRST(
4585 got_object_commit_get_parent_ids(commit));
4586 if (view_is_parent_view(view))
4587 begin_x = view_split_begin_x(view->begin_x);
4588 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4589 if (diff_view == NULL) {
4590 got_object_commit_close(commit);
4591 err = got_error_from_errno("view_open");
4592 break;
4594 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4595 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4596 got_object_commit_close(commit);
4597 if (err) {
4598 view_close(diff_view);
4599 break;
4601 if (view_is_parent_view(view)) {
4602 err = view_close_child(view);
4603 if (err)
4604 break;
4605 err = view_set_child(view, diff_view);
4606 if (err) {
4607 view_close(diff_view);
4608 break;
4610 *focus_view = diff_view;
4611 view->child_focussed = 1;
4612 } else
4613 *new_view = diff_view;
4614 if (err)
4615 break;
4616 break;
4618 case KEY_NPAGE:
4619 case CTRL('f'):
4620 case ' ':
4621 if (s->last_displayed_line >= s->blame.nlines &&
4622 s->selected_line >= MIN(s->blame.nlines,
4623 view->nlines - 2)) {
4624 break;
4626 if (s->last_displayed_line >= s->blame.nlines &&
4627 s->selected_line < view->nlines - 2) {
4628 s->selected_line = MIN(s->blame.nlines,
4629 view->nlines - 2);
4630 break;
4632 if (s->last_displayed_line + view->nlines - 2
4633 <= s->blame.nlines)
4634 s->first_displayed_line +=
4635 view->nlines - 2;
4636 else
4637 s->first_displayed_line =
4638 s->blame.nlines -
4639 (view->nlines - 3);
4640 break;
4641 case KEY_RESIZE:
4642 if (s->selected_line > view->nlines - 2) {
4643 s->selected_line = MIN(s->blame.nlines,
4644 view->nlines - 2);
4646 break;
4647 default:
4648 break;
4650 return thread_err ? thread_err : err;
4653 static const struct got_error *
4654 cmd_blame(int argc, char *argv[])
4656 const struct got_error *error;
4657 struct got_repository *repo = NULL;
4658 struct got_worktree *worktree = NULL;
4659 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4660 char *link_target = NULL;
4661 struct got_object_id *commit_id = NULL;
4662 char *commit_id_str = NULL;
4663 int ch;
4664 struct tog_view *view;
4666 #ifndef PROFILE
4667 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4668 NULL) == -1)
4669 err(1, "pledge");
4670 #endif
4672 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4673 switch (ch) {
4674 case 'c':
4675 commit_id_str = optarg;
4676 break;
4677 case 'r':
4678 repo_path = realpath(optarg, NULL);
4679 if (repo_path == NULL)
4680 return got_error_from_errno2("realpath",
4681 optarg);
4682 break;
4683 default:
4684 usage_blame();
4685 /* NOTREACHED */
4689 argc -= optind;
4690 argv += optind;
4692 if (argc != 1)
4693 usage_blame();
4695 cwd = getcwd(NULL, 0);
4696 if (cwd == NULL)
4697 return got_error_from_errno("getcwd");
4699 error = got_worktree_open(&worktree, cwd);
4700 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4701 goto done;
4703 if (repo_path == NULL) {
4704 if (worktree)
4705 repo_path =
4706 strdup(got_worktree_get_repo_path(worktree));
4707 else
4708 repo_path = strdup(cwd);
4710 if (repo_path == NULL) {
4711 error = got_error_from_errno("strdup");
4712 goto done;
4715 error = got_repo_open(&repo, repo_path, NULL);
4716 if (error != NULL)
4717 goto done;
4719 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4720 worktree);
4721 if (error)
4722 goto done;
4724 init_curses();
4726 error = apply_unveil(got_repo_get_path(repo), NULL);
4727 if (error)
4728 goto done;
4730 if (commit_id_str == NULL) {
4731 struct got_reference *head_ref;
4732 error = got_ref_open(&head_ref, repo, worktree ?
4733 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4734 if (error != NULL)
4735 goto done;
4736 error = got_ref_resolve(&commit_id, repo, head_ref);
4737 got_ref_close(head_ref);
4738 } else {
4739 error = got_repo_match_object_id(&commit_id, NULL,
4740 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4742 if (error != NULL)
4743 goto done;
4745 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4746 if (view == NULL) {
4747 error = got_error_from_errno("view_open");
4748 goto done;
4751 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4752 commit_id, repo);
4753 if (error)
4754 goto done;
4756 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4757 commit_id, repo);
4758 if (error)
4759 goto done;
4760 if (worktree) {
4761 /* Release work tree lock. */
4762 got_worktree_close(worktree);
4763 worktree = NULL;
4765 error = view_loop(view);
4766 done:
4767 free(repo_path);
4768 free(in_repo_path);
4769 free(link_target);
4770 free(cwd);
4771 free(commit_id);
4772 if (worktree)
4773 got_worktree_close(worktree);
4774 if (repo)
4775 got_repo_close(repo);
4776 return error;
4779 static const struct got_error *
4780 draw_tree_entries(struct tog_view *view, const char *parent_path)
4782 struct tog_tree_view_state *s = &view->state.tree;
4783 const struct got_error *err = NULL;
4784 struct got_tree_entry *te;
4785 wchar_t *wline;
4786 struct tog_color *tc;
4787 int width, n, i, nentries;
4788 int limit = view->nlines;
4790 s->ndisplayed = 0;
4792 werase(view->window);
4794 if (limit == 0)
4795 return NULL;
4797 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4798 if (err)
4799 return err;
4800 if (view_needs_focus_indication(view))
4801 wstandout(view->window);
4802 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4803 if (tc)
4804 wattr_on(view->window,
4805 COLOR_PAIR(tc->colorpair), NULL);
4806 waddwstr(view->window, wline);
4807 if (tc)
4808 wattr_off(view->window,
4809 COLOR_PAIR(tc->colorpair), NULL);
4810 if (view_needs_focus_indication(view))
4811 wstandend(view->window);
4812 free(wline);
4813 wline = NULL;
4814 if (width < view->ncols - 1)
4815 waddch(view->window, '\n');
4816 if (--limit <= 0)
4817 return NULL;
4818 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4819 if (err)
4820 return err;
4821 waddwstr(view->window, wline);
4822 free(wline);
4823 wline = NULL;
4824 if (width < view->ncols - 1)
4825 waddch(view->window, '\n');
4826 if (--limit <= 0)
4827 return NULL;
4828 waddch(view->window, '\n');
4829 if (--limit <= 0)
4830 return NULL;
4832 if (s->first_displayed_entry == NULL) {
4833 te = got_object_tree_get_first_entry(s->tree);
4834 if (s->selected == 0) {
4835 if (view->focussed)
4836 wstandout(view->window);
4837 s->selected_entry = NULL;
4839 waddstr(view->window, " ..\n"); /* parent directory */
4840 if (s->selected == 0 && view->focussed)
4841 wstandend(view->window);
4842 s->ndisplayed++;
4843 if (--limit <= 0)
4844 return NULL;
4845 n = 1;
4846 } else {
4847 n = 0;
4848 te = s->first_displayed_entry;
4851 nentries = got_object_tree_get_nentries(s->tree);
4852 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4853 char *line = NULL, *id_str = NULL, *link_target = NULL;
4854 const char *modestr = "";
4855 mode_t mode;
4857 te = got_object_tree_get_entry(s->tree, i);
4858 mode = got_tree_entry_get_mode(te);
4860 if (s->show_ids) {
4861 err = got_object_id_str(&id_str,
4862 got_tree_entry_get_id(te));
4863 if (err)
4864 return got_error_from_errno(
4865 "got_object_id_str");
4867 if (got_object_tree_entry_is_submodule(te))
4868 modestr = "$";
4869 else if (S_ISLNK(mode)) {
4870 int i;
4872 err = got_tree_entry_get_symlink_target(&link_target,
4873 te, s->repo);
4874 if (err) {
4875 free(id_str);
4876 return err;
4878 for (i = 0; i < strlen(link_target); i++) {
4879 if (!isprint((unsigned char)link_target[i]))
4880 link_target[i] = '?';
4882 modestr = "@";
4884 else if (S_ISDIR(mode))
4885 modestr = "/";
4886 else if (mode & S_IXUSR)
4887 modestr = "*";
4888 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4889 got_tree_entry_get_name(te), modestr,
4890 link_target ? " -> ": "",
4891 link_target ? link_target : "") == -1) {
4892 free(id_str);
4893 free(link_target);
4894 return got_error_from_errno("asprintf");
4896 free(id_str);
4897 free(link_target);
4898 err = format_line(&wline, &width, line, view->ncols, 0);
4899 if (err) {
4900 free(line);
4901 break;
4903 if (n == s->selected) {
4904 if (view->focussed)
4905 wstandout(view->window);
4906 s->selected_entry = te;
4908 tc = match_color(&s->colors, line);
4909 if (tc)
4910 wattr_on(view->window,
4911 COLOR_PAIR(tc->colorpair), NULL);
4912 waddwstr(view->window, wline);
4913 if (tc)
4914 wattr_off(view->window,
4915 COLOR_PAIR(tc->colorpair), NULL);
4916 if (width < view->ncols - 1)
4917 waddch(view->window, '\n');
4918 if (n == s->selected && view->focussed)
4919 wstandend(view->window);
4920 free(line);
4921 free(wline);
4922 wline = NULL;
4923 n++;
4924 s->ndisplayed++;
4925 s->last_displayed_entry = te;
4926 if (--limit <= 0)
4927 break;
4930 return err;
4933 static void
4934 tree_scroll_up(struct tog_view *view, int maxscroll)
4936 struct tog_tree_view_state *s = &view->state.tree;
4937 struct got_tree_entry *te;
4938 int isroot = s->tree == s->root;
4939 int i = 0;
4941 if (s->first_displayed_entry == NULL)
4942 return;
4944 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4945 while (i++ < maxscroll) {
4946 if (te == NULL) {
4947 if (!isroot)
4948 s->first_displayed_entry = NULL;
4949 break;
4951 s->first_displayed_entry = te;
4952 te = got_tree_entry_get_prev(s->tree, te);
4956 static void
4957 tree_scroll_down(struct tog_view *view, int maxscroll)
4959 struct tog_tree_view_state *s = &view->state.tree;
4960 struct got_tree_entry *next, *last;
4961 int n = 0;
4963 if (s->first_displayed_entry)
4964 next = got_tree_entry_get_next(s->tree,
4965 s->first_displayed_entry);
4966 else
4967 next = got_object_tree_get_first_entry(s->tree);
4969 last = s->last_displayed_entry;
4970 while (next && last && n++ < maxscroll) {
4971 last = got_tree_entry_get_next(s->tree, last);
4972 if (last) {
4973 s->first_displayed_entry = next;
4974 next = got_tree_entry_get_next(s->tree, next);
4979 static const struct got_error *
4980 tree_entry_path(char **path, struct tog_parent_trees *parents,
4981 struct got_tree_entry *te)
4983 const struct got_error *err = NULL;
4984 struct tog_parent_tree *pt;
4985 size_t len = 2; /* for leading slash and NUL */
4987 TAILQ_FOREACH(pt, parents, entry)
4988 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4989 + 1 /* slash */;
4990 if (te)
4991 len += strlen(got_tree_entry_get_name(te));
4993 *path = calloc(1, len);
4994 if (path == NULL)
4995 return got_error_from_errno("calloc");
4997 (*path)[0] = '/';
4998 pt = TAILQ_LAST(parents, tog_parent_trees);
4999 while (pt) {
5000 const char *name = got_tree_entry_get_name(pt->selected_entry);
5001 if (strlcat(*path, name, len) >= len) {
5002 err = got_error(GOT_ERR_NO_SPACE);
5003 goto done;
5005 if (strlcat(*path, "/", len) >= len) {
5006 err = got_error(GOT_ERR_NO_SPACE);
5007 goto done;
5009 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5011 if (te) {
5012 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5013 err = got_error(GOT_ERR_NO_SPACE);
5014 goto done;
5017 done:
5018 if (err) {
5019 free(*path);
5020 *path = NULL;
5022 return err;
5025 static const struct got_error *
5026 blame_tree_entry(struct tog_view **new_view, int begin_x,
5027 struct got_tree_entry *te, struct tog_parent_trees *parents,
5028 struct got_object_id *commit_id, struct got_repository *repo)
5030 const struct got_error *err = NULL;
5031 char *path;
5032 struct tog_view *blame_view;
5034 *new_view = NULL;
5036 err = tree_entry_path(&path, parents, te);
5037 if (err)
5038 return err;
5040 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5041 if (blame_view == NULL) {
5042 err = got_error_from_errno("view_open");
5043 goto done;
5046 err = open_blame_view(blame_view, path, commit_id, repo);
5047 if (err) {
5048 if (err->code == GOT_ERR_CANCELLED)
5049 err = NULL;
5050 view_close(blame_view);
5051 } else
5052 *new_view = blame_view;
5053 done:
5054 free(path);
5055 return err;
5058 static const struct got_error *
5059 log_tree_entry(struct tog_view **new_view, int begin_x,
5060 struct got_tree_entry *te, struct tog_parent_trees *parents,
5061 struct got_object_id *commit_id, struct got_repository *repo)
5063 struct tog_view *log_view;
5064 const struct got_error *err = NULL;
5065 char *path;
5067 *new_view = NULL;
5069 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5070 if (log_view == NULL)
5071 return got_error_from_errno("view_open");
5073 err = tree_entry_path(&path, parents, te);
5074 if (err)
5075 return err;
5077 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5078 if (err)
5079 view_close(log_view);
5080 else
5081 *new_view = log_view;
5082 free(path);
5083 return err;
5086 static const struct got_error *
5087 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5088 struct got_object_id *commit_id, struct got_repository *repo)
5090 const struct got_error *err = NULL;
5091 char *commit_id_str = NULL;
5092 struct tog_tree_view_state *s = &view->state.tree;
5094 TAILQ_INIT(&s->parents);
5096 err = got_object_id_str(&commit_id_str, commit_id);
5097 if (err != NULL)
5098 goto done;
5100 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5101 err = got_error_from_errno("asprintf");
5102 goto done;
5105 s->root = s->tree = root;
5106 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5107 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5108 s->commit_id = got_object_id_dup(commit_id);
5109 if (s->commit_id == NULL) {
5110 err = got_error_from_errno("got_object_id_dup");
5111 goto done;
5113 s->repo = repo;
5115 SIMPLEQ_INIT(&s->colors);
5117 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5118 err = add_color(&s->colors, "\\$$",
5119 TOG_COLOR_TREE_SUBMODULE,
5120 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5121 if (err)
5122 goto done;
5123 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5124 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5125 if (err) {
5126 free_colors(&s->colors);
5127 goto done;
5129 err = add_color(&s->colors, "/$",
5130 TOG_COLOR_TREE_DIRECTORY,
5131 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5132 if (err) {
5133 free_colors(&s->colors);
5134 goto done;
5137 err = add_color(&s->colors, "\\*$",
5138 TOG_COLOR_TREE_EXECUTABLE,
5139 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5140 if (err) {
5141 free_colors(&s->colors);
5142 goto done;
5145 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5146 get_color_value("TOG_COLOR_COMMIT"));
5147 if (err) {
5148 free_colors(&s->colors);
5149 goto done;
5153 view->show = show_tree_view;
5154 view->input = input_tree_view;
5155 view->close = close_tree_view;
5156 view->search_start = search_start_tree_view;
5157 view->search_next = search_next_tree_view;
5158 done:
5159 free(commit_id_str);
5160 if (err) {
5161 free(s->tree_label);
5162 s->tree_label = NULL;
5164 return err;
5167 static const struct got_error *
5168 close_tree_view(struct tog_view *view)
5170 struct tog_tree_view_state *s = &view->state.tree;
5172 free_colors(&s->colors);
5173 free(s->tree_label);
5174 s->tree_label = NULL;
5175 free(s->commit_id);
5176 s->commit_id = NULL;
5177 while (!TAILQ_EMPTY(&s->parents)) {
5178 struct tog_parent_tree *parent;
5179 parent = TAILQ_FIRST(&s->parents);
5180 TAILQ_REMOVE(&s->parents, parent, entry);
5181 free(parent);
5184 if (s->tree != s->root)
5185 got_object_tree_close(s->tree);
5186 got_object_tree_close(s->root);
5187 return NULL;
5190 static const struct got_error *
5191 search_start_tree_view(struct tog_view *view)
5193 struct tog_tree_view_state *s = &view->state.tree;
5195 s->matched_entry = NULL;
5196 return NULL;
5199 static int
5200 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5202 regmatch_t regmatch;
5204 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5205 0) == 0;
5208 static const struct got_error *
5209 search_next_tree_view(struct tog_view *view)
5211 struct tog_tree_view_state *s = &view->state.tree;
5212 struct got_tree_entry *te = NULL;
5214 if (!view->searching) {
5215 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5216 return NULL;
5219 if (s->matched_entry) {
5220 if (view->searching == TOG_SEARCH_FORWARD) {
5221 if (s->selected_entry)
5222 te = got_tree_entry_get_next(s->tree,
5223 s->selected_entry);
5224 else
5225 te = got_object_tree_get_first_entry(s->tree);
5226 } else {
5227 if (s->selected_entry == NULL)
5228 te = got_object_tree_get_last_entry(s->tree);
5229 else
5230 te = got_tree_entry_get_prev(s->tree,
5231 s->selected_entry);
5233 } else {
5234 if (view->searching == TOG_SEARCH_FORWARD)
5235 te = got_object_tree_get_first_entry(s->tree);
5236 else
5237 te = got_object_tree_get_last_entry(s->tree);
5240 while (1) {
5241 if (te == NULL) {
5242 if (s->matched_entry == NULL) {
5243 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5244 return NULL;
5246 if (view->searching == TOG_SEARCH_FORWARD)
5247 te = got_object_tree_get_first_entry(s->tree);
5248 else
5249 te = got_object_tree_get_last_entry(s->tree);
5252 if (match_tree_entry(te, &view->regex)) {
5253 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5254 s->matched_entry = te;
5255 break;
5258 if (view->searching == TOG_SEARCH_FORWARD)
5259 te = got_tree_entry_get_next(s->tree, te);
5260 else
5261 te = got_tree_entry_get_prev(s->tree, te);
5264 if (s->matched_entry) {
5265 s->first_displayed_entry = s->matched_entry;
5266 s->selected = 0;
5269 return NULL;
5272 static const struct got_error *
5273 show_tree_view(struct tog_view *view)
5275 const struct got_error *err = NULL;
5276 struct tog_tree_view_state *s = &view->state.tree;
5277 char *parent_path;
5279 err = tree_entry_path(&parent_path, &s->parents, NULL);
5280 if (err)
5281 return err;
5283 err = draw_tree_entries(view, parent_path);
5284 free(parent_path);
5286 view_vborder(view);
5287 return err;
5290 static const struct got_error *
5291 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5292 struct tog_view **focus_view, struct tog_view *view, int ch)
5294 const struct got_error *err = NULL;
5295 struct tog_tree_view_state *s = &view->state.tree;
5296 struct tog_view *log_view, *ref_view;
5297 int begin_x = 0;
5299 switch (ch) {
5300 case 'i':
5301 s->show_ids = !s->show_ids;
5302 break;
5303 case 'l':
5304 if (!s->selected_entry)
5305 break;
5306 if (view_is_parent_view(view))
5307 begin_x = view_split_begin_x(view->begin_x);
5308 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5309 &s->parents, s->commit_id, s->repo);
5310 if (view_is_parent_view(view)) {
5311 err = view_close_child(view);
5312 if (err)
5313 return err;
5314 err = view_set_child(view, log_view);
5315 if (err) {
5316 view_close(log_view);
5317 break;
5319 *focus_view = log_view;
5320 view->child_focussed = 1;
5321 } else
5322 *new_view = log_view;
5323 break;
5324 case 'r':
5325 if (view_is_parent_view(view))
5326 begin_x = view_split_begin_x(view->begin_x);
5327 ref_view = view_open(view->nlines, view->ncols,
5328 view->begin_y, begin_x, TOG_VIEW_REF);
5329 if (ref_view == NULL)
5330 return got_error_from_errno("view_open");
5331 err = open_ref_view(ref_view, s->repo);
5332 if (err) {
5333 view_close(ref_view);
5334 return err;
5336 if (view_is_parent_view(view)) {
5337 err = view_close_child(view);
5338 if (err)
5339 return err;
5340 err = view_set_child(view, ref_view);
5341 if (err) {
5342 view_close(ref_view);
5343 break;
5345 *focus_view = ref_view;
5346 view->child_focussed = 1;
5347 } else
5348 *new_view = ref_view;
5349 break;
5350 case 'k':
5351 case KEY_UP:
5352 if (s->selected > 0) {
5353 s->selected--;
5354 break;
5356 tree_scroll_up(view, 1);
5357 break;
5358 case KEY_PPAGE:
5359 case CTRL('b'):
5360 if (s->tree == s->root) {
5361 if (got_object_tree_get_first_entry(s->tree) ==
5362 s->first_displayed_entry)
5363 s->selected = 0;
5364 } else {
5365 if (s->first_displayed_entry == NULL)
5366 s->selected = 0;
5368 tree_scroll_up(view, MAX(0, view->nlines - 3));
5369 break;
5370 case 'j':
5371 case KEY_DOWN:
5372 if (s->selected < s->ndisplayed - 1) {
5373 s->selected++;
5374 break;
5376 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5377 == NULL)
5378 /* can't scroll any further */
5379 break;
5380 tree_scroll_down(view, 1);
5381 break;
5382 case KEY_NPAGE:
5383 case CTRL('f'):
5384 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5385 == NULL) {
5386 /* can't scroll any further; move cursor down */
5387 if (s->selected < s->ndisplayed - 1)
5388 s->selected = s->ndisplayed - 1;
5389 break;
5391 tree_scroll_down(view, view->nlines - 3);
5392 break;
5393 case KEY_ENTER:
5394 case '\r':
5395 case KEY_BACKSPACE:
5396 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5397 struct tog_parent_tree *parent;
5398 /* user selected '..' */
5399 if (s->tree == s->root)
5400 break;
5401 parent = TAILQ_FIRST(&s->parents);
5402 TAILQ_REMOVE(&s->parents, parent,
5403 entry);
5404 got_object_tree_close(s->tree);
5405 s->tree = parent->tree;
5406 s->first_displayed_entry =
5407 parent->first_displayed_entry;
5408 s->selected_entry =
5409 parent->selected_entry;
5410 s->selected = parent->selected;
5411 free(parent);
5412 } else if (S_ISDIR(got_tree_entry_get_mode(
5413 s->selected_entry))) {
5414 struct got_tree_object *subtree;
5415 err = got_object_open_as_tree(&subtree, s->repo,
5416 got_tree_entry_get_id(s->selected_entry));
5417 if (err)
5418 break;
5419 err = tree_view_visit_subtree(subtree, s);
5420 if (err) {
5421 got_object_tree_close(subtree);
5422 break;
5424 } else if (S_ISREG(got_tree_entry_get_mode(
5425 s->selected_entry))) {
5426 struct tog_view *blame_view;
5427 int begin_x = view_is_parent_view(view) ?
5428 view_split_begin_x(view->begin_x) : 0;
5430 err = blame_tree_entry(&blame_view, begin_x,
5431 s->selected_entry, &s->parents,
5432 s->commit_id, s->repo);
5433 if (err)
5434 break;
5435 if (view_is_parent_view(view)) {
5436 err = view_close_child(view);
5437 if (err)
5438 return err;
5439 err = view_set_child(view, blame_view);
5440 if (err) {
5441 view_close(blame_view);
5442 break;
5444 *focus_view = blame_view;
5445 view->child_focussed = 1;
5446 } else
5447 *new_view = blame_view;
5449 break;
5450 case KEY_RESIZE:
5451 if (s->selected > view->nlines)
5452 s->selected = s->ndisplayed - 1;
5453 break;
5454 default:
5455 break;
5458 return err;
5461 __dead static void
5462 usage_tree(void)
5464 endwin();
5465 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5466 getprogname());
5467 exit(1);
5470 static const struct got_error *
5471 cmd_tree(int argc, char *argv[])
5473 const struct got_error *error;
5474 struct got_repository *repo = NULL;
5475 struct got_worktree *worktree = NULL;
5476 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5477 struct got_object_id *commit_id = NULL;
5478 char *commit_id_arg = NULL;
5479 struct got_commit_object *commit = NULL;
5480 struct got_tree_object *tree = NULL;
5481 int ch;
5482 struct tog_view *view;
5484 #ifndef PROFILE
5485 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5486 NULL) == -1)
5487 err(1, "pledge");
5488 #endif
5490 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5491 switch (ch) {
5492 case 'c':
5493 commit_id_arg = optarg;
5494 break;
5495 case 'r':
5496 repo_path = realpath(optarg, NULL);
5497 if (repo_path == NULL)
5498 return got_error_from_errno2("realpath",
5499 optarg);
5500 break;
5501 default:
5502 usage_tree();
5503 /* NOTREACHED */
5507 argc -= optind;
5508 argv += optind;
5510 if (argc > 1)
5511 usage_tree();
5513 cwd = getcwd(NULL, 0);
5514 if (cwd == NULL)
5515 return got_error_from_errno("getcwd");
5517 error = got_worktree_open(&worktree, cwd);
5518 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5519 goto done;
5521 if (repo_path == NULL) {
5522 if (worktree)
5523 repo_path =
5524 strdup(got_worktree_get_repo_path(worktree));
5525 else
5526 repo_path = strdup(cwd);
5528 if (repo_path == NULL) {
5529 error = got_error_from_errno("strdup");
5530 goto done;
5533 error = got_repo_open(&repo, repo_path, NULL);
5534 if (error != NULL)
5535 goto done;
5537 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5538 repo, worktree);
5539 if (error)
5540 goto done;
5542 init_curses();
5544 error = apply_unveil(got_repo_get_path(repo), NULL);
5545 if (error)
5546 goto done;
5548 error = got_repo_match_object_id(&commit_id, NULL,
5549 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5550 GOT_OBJ_TYPE_COMMIT, 1, repo);
5551 if (error)
5552 goto done;
5554 error = got_object_open_as_commit(&commit, repo, commit_id);
5555 if (error)
5556 goto done;
5558 error = got_object_open_as_tree(&tree, repo,
5559 got_object_commit_get_tree_id(commit));
5560 if (error)
5561 goto done;
5563 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5564 if (view == NULL) {
5565 error = got_error_from_errno("view_open");
5566 goto done;
5568 error = open_tree_view(view, tree, commit_id, repo);
5569 if (error)
5570 goto done;
5571 if (!got_path_is_root_dir(in_repo_path)) {
5572 error = tree_view_walk_path(&view->state.tree, commit_id,
5573 in_repo_path);
5574 if (error)
5575 goto done;
5578 if (worktree) {
5579 /* Release work tree lock. */
5580 got_worktree_close(worktree);
5581 worktree = NULL;
5583 error = view_loop(view);
5584 done:
5585 free(repo_path);
5586 free(cwd);
5587 free(commit_id);
5588 if (commit)
5589 got_object_commit_close(commit);
5590 if (tree)
5591 got_object_tree_close(tree);
5592 if (repo)
5593 got_repo_close(repo);
5594 return error;
5597 static const struct got_error *
5598 ref_view_load_refs(struct tog_ref_view_state *s)
5600 const struct got_error *err;
5601 struct got_reflist_entry *sre;
5602 struct tog_reflist_entry *re;
5604 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5605 got_ref_cmp_by_name, NULL);
5606 if (err)
5607 return err;
5609 s->nrefs = 0;
5610 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5611 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5612 continue;
5614 re = malloc(sizeof(*re));
5615 if (re == NULL)
5616 return got_error_from_errno("malloc");
5618 re->ref = sre->ref;
5619 re->idx = s->nrefs++;
5620 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5623 return NULL;
5626 void
5627 ref_view_free_refs(struct tog_ref_view_state *s)
5629 struct tog_reflist_entry *re;
5631 while (!TAILQ_EMPTY(&s->refs)) {
5632 re = TAILQ_FIRST(&s->refs);
5633 TAILQ_REMOVE(&s->refs, re, entry);
5634 free(re);
5636 got_ref_list_free(&s->simplerefs);
5639 static const struct got_error *
5640 open_ref_view(struct tog_view *view, struct got_repository *repo)
5642 const struct got_error *err = NULL;
5643 struct tog_ref_view_state *s = &view->state.ref;
5645 s->selected_entry = 0;
5646 s->repo = repo;
5648 SIMPLEQ_INIT(&s->simplerefs);
5649 TAILQ_INIT(&s->refs);
5650 SIMPLEQ_INIT(&s->colors);
5652 err = ref_view_load_refs(s);
5653 if (err)
5654 return err;
5656 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5658 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5659 err = add_color(&s->colors, "^refs/heads/",
5660 TOG_COLOR_REFS_HEADS,
5661 get_color_value("TOG_COLOR_REFS_HEADS"));
5662 if (err)
5663 goto done;
5665 err = add_color(&s->colors, "^refs/tags/",
5666 TOG_COLOR_REFS_TAGS,
5667 get_color_value("TOG_COLOR_REFS_TAGS"));
5668 if (err)
5669 goto done;
5671 err = add_color(&s->colors, "^refs/remotes/",
5672 TOG_COLOR_REFS_REMOTES,
5673 get_color_value("TOG_COLOR_REFS_REMOTES"));
5674 if (err)
5675 goto done;
5678 view->show = show_ref_view;
5679 view->input = input_ref_view;
5680 view->close = close_ref_view;
5681 view->search_start = search_start_ref_view;
5682 view->search_next = search_next_ref_view;
5683 done:
5684 if (err)
5685 free_colors(&s->colors);
5686 return err;
5689 static const struct got_error *
5690 close_ref_view(struct tog_view *view)
5692 struct tog_ref_view_state *s = &view->state.ref;
5694 ref_view_free_refs(s);
5695 free_colors(&s->colors);
5697 return NULL;
5700 static const struct got_error *
5701 resolve_reflist_entry(struct got_object_id **commit_id,
5702 struct tog_reflist_entry *re, struct got_repository *repo)
5704 const struct got_error *err = NULL;
5705 struct got_object_id *obj_id;
5706 struct got_tag_object *tag = NULL;
5707 int obj_type;
5709 *commit_id = NULL;
5711 err = got_ref_resolve(&obj_id, repo, re->ref);
5712 if (err)
5713 return err;
5715 err = got_object_get_type(&obj_type, repo, obj_id);
5716 if (err)
5717 goto done;
5719 switch (obj_type) {
5720 case GOT_OBJ_TYPE_COMMIT:
5721 *commit_id = obj_id;
5722 break;
5723 case GOT_OBJ_TYPE_TAG:
5724 err = got_object_open_as_tag(&tag, repo, obj_id);
5725 if (err)
5726 goto done;
5727 free(obj_id);
5728 err = got_object_get_type(&obj_type, repo,
5729 got_object_tag_get_object_id(tag));
5730 if (err)
5731 goto done;
5732 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5733 err = got_error(GOT_ERR_OBJ_TYPE);
5734 goto done;
5736 *commit_id = got_object_id_dup(
5737 got_object_tag_get_object_id(tag));
5738 if (*commit_id == NULL) {
5739 err = got_error_from_errno("got_object_id_dup");
5740 goto done;
5742 break;
5743 default:
5744 err = got_error(GOT_ERR_OBJ_TYPE);
5745 break;
5748 done:
5749 if (tag)
5750 got_object_tag_close(tag);
5751 if (err) {
5752 free(*commit_id);
5753 *commit_id = NULL;
5755 return err;
5758 static const struct got_error *
5759 log_ref_entry(struct tog_view **new_view, int begin_x,
5760 struct tog_reflist_entry *re, struct got_repository *repo)
5762 struct tog_view *log_view;
5763 const struct got_error *err = NULL;
5764 struct got_object_id *commit_id = NULL;
5766 *new_view = NULL;
5768 err = resolve_reflist_entry(&commit_id, re, repo);
5769 if (err) {
5770 if (err->code != GOT_ERR_OBJ_TYPE)
5771 return err;
5772 else
5773 return NULL;
5776 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5777 if (log_view == NULL) {
5778 err = got_error_from_errno("view_open");
5779 goto done;
5782 err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5783 done:
5784 if (err)
5785 view_close(log_view);
5786 else
5787 *new_view = log_view;
5788 free(commit_id);
5789 return err;
5792 static void
5793 ref_scroll_up(struct tog_view *view, int maxscroll)
5795 struct tog_ref_view_state *s = &view->state.ref;
5796 struct tog_reflist_entry *re;
5797 int i = 0;
5799 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5800 return;
5802 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5803 while (i++ < maxscroll) {
5804 if (re == NULL)
5805 break;
5806 s->first_displayed_entry = re;
5807 re = TAILQ_PREV(re, tog_reflist_head, entry);
5811 static void
5812 ref_scroll_down(struct tog_view *view, int maxscroll)
5814 struct tog_ref_view_state *s = &view->state.ref;
5815 struct tog_reflist_entry *next, *last;
5816 int n = 0;
5818 if (s->first_displayed_entry)
5819 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5820 else
5821 next = TAILQ_FIRST(&s->refs);
5823 last = s->last_displayed_entry;
5824 while (next && last && n++ < maxscroll) {
5825 last = TAILQ_NEXT(last, entry);
5826 if (last) {
5827 s->first_displayed_entry = next;
5828 next = TAILQ_NEXT(next, entry);
5833 static const struct got_error *
5834 search_start_ref_view(struct tog_view *view)
5836 struct tog_ref_view_state *s = &view->state.ref;
5838 s->matched_entry = NULL;
5839 return NULL;
5842 static int
5843 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5845 regmatch_t regmatch;
5847 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5848 0) == 0;
5851 static const struct got_error *
5852 search_next_ref_view(struct tog_view *view)
5854 struct tog_ref_view_state *s = &view->state.ref;
5855 struct tog_reflist_entry *re = NULL;
5857 if (!view->searching) {
5858 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5859 return NULL;
5862 if (s->matched_entry) {
5863 if (view->searching == TOG_SEARCH_FORWARD) {
5864 if (s->selected_entry)
5865 re = TAILQ_NEXT(s->selected_entry, entry);
5866 else
5867 re = TAILQ_PREV(s->selected_entry,
5868 tog_reflist_head, entry);
5869 } else {
5870 if (s->selected_entry == NULL)
5871 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5872 else
5873 re = TAILQ_PREV(s->selected_entry,
5874 tog_reflist_head, entry);
5876 } else {
5877 if (view->searching == TOG_SEARCH_FORWARD)
5878 re = TAILQ_FIRST(&s->refs);
5879 else
5880 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5883 while (1) {
5884 if (re == NULL) {
5885 if (s->matched_entry == NULL) {
5886 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5887 return NULL;
5889 if (view->searching == TOG_SEARCH_FORWARD)
5890 re = TAILQ_FIRST(&s->refs);
5891 else
5892 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5895 if (match_reflist_entry(re, &view->regex)) {
5896 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5897 s->matched_entry = re;
5898 break;
5901 if (view->searching == TOG_SEARCH_FORWARD)
5902 re = TAILQ_NEXT(re, entry);
5903 else
5904 re = TAILQ_PREV(re, tog_reflist_head, entry);
5907 if (s->matched_entry) {
5908 s->first_displayed_entry = s->matched_entry;
5909 s->selected = 0;
5912 return NULL;
5915 static const struct got_error *
5916 show_ref_view(struct tog_view *view)
5918 const struct got_error *err = NULL;
5919 struct tog_ref_view_state *s = &view->state.ref;
5920 struct tog_reflist_entry *re;
5921 char *line = NULL;
5922 wchar_t *wline;
5923 struct tog_color *tc;
5924 int width, n;
5925 int limit = view->nlines;
5927 werase(view->window);
5929 s->ndisplayed = 0;
5931 if (limit == 0)
5932 return NULL;
5934 re = s->first_displayed_entry;
5936 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5937 s->nrefs) == -1)
5938 return got_error_from_errno("asprintf");
5940 err = format_line(&wline, &width, line, view->ncols, 0);
5941 if (err) {
5942 free(line);
5943 return err;
5945 if (view_needs_focus_indication(view))
5946 wstandout(view->window);
5947 waddwstr(view->window, wline);
5948 if (view_needs_focus_indication(view))
5949 wstandend(view->window);
5950 free(wline);
5951 wline = NULL;
5952 free(line);
5953 line = NULL;
5954 if (width < view->ncols - 1)
5955 waddch(view->window, '\n');
5956 if (--limit <= 0)
5957 return NULL;
5959 n = 0;
5960 while (re && limit > 0) {
5961 char *line = NULL;
5963 if (got_ref_is_symbolic(re->ref)) {
5964 if (asprintf(&line, "%s -> %s",
5965 got_ref_get_name(re->ref),
5966 got_ref_get_symref_target(re->ref)) == -1)
5967 return got_error_from_errno("asprintf");
5968 } else if (s->show_ids) {
5969 struct got_object_id *id;
5970 char *id_str;
5971 err = got_ref_resolve(&id, s->repo, re->ref);
5972 if (err)
5973 return err;
5974 err = got_object_id_str(&id_str, id);
5975 if (err) {
5976 free(id);
5977 return err;
5979 if (asprintf(&line, "%s: %s",
5980 got_ref_get_name(re->ref), id_str) == -1) {
5981 err = got_error_from_errno("asprintf");
5982 free(id);
5983 free(id_str);
5984 return err;
5986 free(id);
5987 free(id_str);
5988 } else {
5989 line = strdup(got_ref_get_name(re->ref));
5990 if (line == NULL)
5991 return got_error_from_errno("strdup");
5994 err = format_line(&wline, &width, line, view->ncols, 0);
5995 if (err) {
5996 free(line);
5997 return err;
5999 if (n == s->selected) {
6000 if (view->focussed)
6001 wstandout(view->window);
6002 s->selected_entry = re;
6004 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6005 if (tc)
6006 wattr_on(view->window,
6007 COLOR_PAIR(tc->colorpair), NULL);
6008 waddwstr(view->window, wline);
6009 if (tc)
6010 wattr_off(view->window,
6011 COLOR_PAIR(tc->colorpair), NULL);
6012 if (width < view->ncols - 1)
6013 waddch(view->window, '\n');
6014 if (n == s->selected && view->focussed)
6015 wstandend(view->window);
6016 free(line);
6017 free(wline);
6018 wline = NULL;
6019 n++;
6020 s->ndisplayed++;
6021 s->last_displayed_entry = re;
6023 limit--;
6024 re = TAILQ_NEXT(re, entry);
6027 view_vborder(view);
6028 return err;
6031 static const struct got_error *
6032 browse_ref_tree(struct tog_view **new_view, int begin_x,
6033 struct tog_reflist_entry *re, struct got_repository *repo)
6035 const struct got_error *err = NULL;
6036 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6037 struct got_tree_object *tree = NULL;
6038 struct tog_view *tree_view;
6040 *new_view = NULL;
6042 err = resolve_reflist_entry(&commit_id, re, repo);
6043 if (err) {
6044 if (err->code != GOT_ERR_OBJ_TYPE)
6045 return err;
6046 else
6047 return NULL;
6050 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6051 if (err)
6052 goto done;
6054 err = got_object_open_as_tree(&tree, repo, tree_id);
6055 if (err)
6056 goto done;
6058 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6059 if (tree_view == NULL) {
6060 err = got_error_from_errno("view_open");
6061 goto done;
6064 err = open_tree_view(tree_view, tree, commit_id, repo);
6065 if (err)
6066 goto done;
6068 *new_view = tree_view;
6069 done:
6070 free(commit_id);
6071 free(tree_id);
6072 if (err) {
6073 if (tree)
6074 got_object_tree_close(tree);
6076 return err;
6078 static const struct got_error *
6079 input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6080 struct tog_view **focus_view, struct tog_view *view, int ch)
6082 const struct got_error *err = NULL;
6083 struct tog_ref_view_state *s = &view->state.ref;
6084 struct tog_view *log_view, *tree_view;
6085 int begin_x = 0;
6087 switch (ch) {
6088 case 'i':
6089 s->show_ids = !s->show_ids;
6090 break;
6091 case KEY_ENTER:
6092 case '\r':
6093 if (!s->selected_entry)
6094 break;
6095 if (view_is_parent_view(view))
6096 begin_x = view_split_begin_x(view->begin_x);
6097 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6098 s->repo);
6099 if (view_is_parent_view(view)) {
6100 err = view_close_child(view);
6101 if (err)
6102 return err;
6103 err = view_set_child(view, log_view);
6104 if (err) {
6105 view_close(log_view);
6106 break;
6108 *focus_view = log_view;
6109 view->child_focussed = 1;
6110 } else
6111 *new_view = log_view;
6112 break;
6113 case 't':
6114 if (!s->selected_entry)
6115 break;
6116 if (view_is_parent_view(view))
6117 begin_x = view_split_begin_x(view->begin_x);
6118 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6119 s->repo);
6120 if (err || tree_view == NULL)
6121 break;
6122 if (view_is_parent_view(view)) {
6123 err = view_close_child(view);
6124 if (err)
6125 return err;
6126 err = view_set_child(view, tree_view);
6127 if (err) {
6128 view_close(tree_view);
6129 break;
6131 *focus_view = tree_view;
6132 view->child_focussed = 1;
6133 } else
6134 *new_view = tree_view;
6135 break;
6136 case 'k':
6137 case KEY_UP:
6138 if (s->selected > 0) {
6139 s->selected--;
6140 break;
6142 ref_scroll_up(view, 1);
6143 break;
6144 case KEY_PPAGE:
6145 case CTRL('b'):
6146 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6147 s->selected = 0;
6148 ref_scroll_up(view, MAX(0, view->nlines - 1));
6149 break;
6150 case 'j':
6151 case KEY_DOWN:
6152 if (s->selected < s->ndisplayed - 1) {
6153 s->selected++;
6154 break;
6156 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6157 /* can't scroll any further */
6158 break;
6159 ref_scroll_down(view, 1);
6160 break;
6161 case KEY_NPAGE:
6162 case CTRL('f'):
6163 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6164 /* can't scroll any further; move cursor down */
6165 if (s->selected < s->ndisplayed - 1)
6166 s->selected = s->ndisplayed - 1;
6167 break;
6169 ref_scroll_down(view, view->nlines - 1);
6170 break;
6171 case CTRL('l'):
6172 ref_view_free_refs(s);
6173 err = ref_view_load_refs(s);
6174 break;
6175 case KEY_RESIZE:
6176 if (s->selected > view->nlines)
6177 s->selected = s->ndisplayed - 1;
6178 break;
6179 default:
6180 break;
6183 return err;
6186 __dead static void
6187 usage_ref(void)
6189 endwin();
6190 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6191 getprogname());
6192 exit(1);
6195 static const struct got_error *
6196 cmd_ref(int argc, char *argv[])
6198 const struct got_error *error;
6199 struct got_repository *repo = NULL;
6200 struct got_worktree *worktree = NULL;
6201 char *cwd = NULL, *repo_path = NULL;
6202 int ch;
6203 struct tog_view *view;
6205 #ifndef PROFILE
6206 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6207 NULL) == -1)
6208 err(1, "pledge");
6209 #endif
6211 while ((ch = getopt(argc, argv, "r:")) != -1) {
6212 switch (ch) {
6213 case 'r':
6214 repo_path = realpath(optarg, NULL);
6215 if (repo_path == NULL)
6216 return got_error_from_errno2("realpath",
6217 optarg);
6218 break;
6219 default:
6220 usage_ref();
6221 /* NOTREACHED */
6225 argc -= optind;
6226 argv += optind;
6228 if (argc > 1)
6229 usage_ref();
6231 cwd = getcwd(NULL, 0);
6232 if (cwd == NULL)
6233 return got_error_from_errno("getcwd");
6235 error = got_worktree_open(&worktree, cwd);
6236 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6237 goto done;
6239 if (repo_path == NULL) {
6240 if (worktree)
6241 repo_path =
6242 strdup(got_worktree_get_repo_path(worktree));
6243 else
6244 repo_path = strdup(cwd);
6246 if (repo_path == NULL) {
6247 error = got_error_from_errno("strdup");
6248 goto done;
6251 error = got_repo_open(&repo, repo_path, NULL);
6252 if (error != NULL)
6253 goto done;
6255 init_curses();
6257 error = apply_unveil(got_repo_get_path(repo), NULL);
6258 if (error)
6259 goto done;
6261 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6262 if (view == NULL) {
6263 error = got_error_from_errno("view_open");
6264 goto done;
6267 error = open_ref_view(view, repo);
6268 if (error)
6269 goto done;
6271 if (worktree) {
6272 /* Release work tree lock. */
6273 got_worktree_close(worktree);
6274 worktree = NULL;
6276 error = view_loop(view);
6277 done:
6278 free(repo_path);
6279 free(cwd);
6280 if (repo)
6281 got_repo_close(repo);
6282 return error;
6285 static void
6286 list_commands(FILE *fp)
6288 int i;
6290 fprintf(fp, "commands:");
6291 for (i = 0; i < nitems(tog_commands); i++) {
6292 struct tog_cmd *cmd = &tog_commands[i];
6293 fprintf(fp, " %s", cmd->name);
6295 fputc('\n', fp);
6298 __dead static void
6299 usage(int hflag, int status)
6301 FILE *fp = (status == 0) ? stdout : stderr;
6303 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6304 getprogname());
6305 if (hflag) {
6306 fprintf(fp, "lazy usage: %s path\n", getprogname());
6307 list_commands(fp);
6309 exit(status);
6312 static char **
6313 make_argv(int argc, ...)
6315 va_list ap;
6316 char **argv;
6317 int i;
6319 va_start(ap, argc);
6321 argv = calloc(argc, sizeof(char *));
6322 if (argv == NULL)
6323 err(1, "calloc");
6324 for (i = 0; i < argc; i++) {
6325 argv[i] = strdup(va_arg(ap, char *));
6326 if (argv[i] == NULL)
6327 err(1, "strdup");
6330 va_end(ap);
6331 return argv;
6335 * Try to convert 'tog path' into a 'tog log path' command.
6336 * The user could simply have mistyped the command rather than knowingly
6337 * provided a path. So check whether argv[0] can in fact be resolved
6338 * to a path in the HEAD commit and print a special error if not.
6339 * This hack is for mpi@ <3
6341 static const struct got_error *
6342 tog_log_with_path(int argc, char *argv[])
6344 const struct got_error *error = NULL;
6345 struct tog_cmd *cmd = NULL;
6346 struct got_repository *repo = NULL;
6347 struct got_worktree *worktree = NULL;
6348 struct got_object_id *commit_id = NULL, *id = NULL;
6349 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6350 char *commit_id_str = NULL, **cmd_argv = NULL;
6352 cwd = getcwd(NULL, 0);
6353 if (cwd == NULL)
6354 return got_error_from_errno("getcwd");
6356 error = got_worktree_open(&worktree, cwd);
6357 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6358 goto done;
6360 if (worktree)
6361 repo_path = strdup(got_worktree_get_repo_path(worktree));
6362 else
6363 repo_path = strdup(cwd);
6364 if (repo_path == NULL) {
6365 error = got_error_from_errno("strdup");
6366 goto done;
6369 error = got_repo_open(&repo, repo_path, NULL);
6370 if (error != NULL)
6371 goto done;
6373 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6374 repo, worktree);
6375 if (error)
6376 goto done;
6378 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6379 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6380 GOT_OBJ_TYPE_COMMIT, 1, repo);
6381 if (error)
6382 goto done;
6384 if (worktree) {
6385 got_worktree_close(worktree);
6386 worktree = NULL;
6389 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6390 if (error) {
6391 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6392 goto done;
6393 fprintf(stderr, "%s: '%s' is no known command or path\n",
6394 getprogname(), argv[0]);
6395 usage(1, 1);
6396 /* not reached */
6399 got_repo_close(repo);
6400 repo = NULL;
6402 error = got_object_id_str(&commit_id_str, commit_id);
6403 if (error)
6404 goto done;
6406 cmd = &tog_commands[0]; /* log */
6407 argc = 4;
6408 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6409 error = cmd->cmd_main(argc, cmd_argv);
6410 done:
6411 if (repo)
6412 got_repo_close(repo);
6413 if (worktree)
6414 got_worktree_close(worktree);
6415 free(id);
6416 free(commit_id_str);
6417 free(commit_id);
6418 free(cwd);
6419 free(repo_path);
6420 free(in_repo_path);
6421 if (cmd_argv) {
6422 int i;
6423 for (i = 0; i < argc; i++)
6424 free(cmd_argv[i]);
6425 free(cmd_argv);
6427 return error;
6430 int
6431 main(int argc, char *argv[])
6433 const struct got_error *error = NULL;
6434 struct tog_cmd *cmd = NULL;
6435 int ch, hflag = 0, Vflag = 0;
6436 char **cmd_argv = NULL;
6437 static struct option longopts[] = {
6438 { "version", no_argument, NULL, 'V' },
6439 { NULL, 0, NULL, 0}
6442 setlocale(LC_CTYPE, "");
6444 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6445 switch (ch) {
6446 case 'h':
6447 hflag = 1;
6448 break;
6449 case 'V':
6450 Vflag = 1;
6451 break;
6452 default:
6453 usage(hflag, 1);
6454 /* NOTREACHED */
6458 argc -= optind;
6459 argv += optind;
6460 optind = 1;
6461 optreset = 1;
6463 if (Vflag) {
6464 got_version_print_str();
6465 return 0;
6468 if (argc == 0) {
6469 if (hflag)
6470 usage(hflag, 0);
6471 /* Build an argument vector which runs a default command. */
6472 cmd = &tog_commands[0];
6473 argc = 1;
6474 cmd_argv = make_argv(argc, cmd->name);
6475 } else {
6476 int i;
6478 /* Did the user specify a command? */
6479 for (i = 0; i < nitems(tog_commands); i++) {
6480 if (strncmp(tog_commands[i].name, argv[0],
6481 strlen(argv[0])) == 0) {
6482 cmd = &tog_commands[i];
6483 break;
6488 if (cmd == NULL) {
6489 if (argc != 1)
6490 usage(0, 1);
6491 /* No command specified; try log with a path */
6492 error = tog_log_with_path(argc, argv);
6493 } else {
6494 if (hflag)
6495 cmd->cmd_usage();
6496 else
6497 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6500 endwin();
6501 putchar('\n');
6502 if (cmd_argv) {
6503 int i;
6504 for (i = 0; i < argc; i++)
6505 free(cmd_argv[i]);
6506 free(cmd_argv);
6509 if (error && error->code != GOT_ERR_CANCELLED)
6510 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6511 return 0;