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,
1835 const char *path, struct got_repository *repo)
1837 const struct got_error *err = NULL;
1838 struct got_tree_object *tree = NULL;
1839 const char *p;
1840 char *slash, *subpath = NULL;
1842 /* Walk the path and open corresponding tree objects. */
1843 p = path;
1844 while (*p) {
1845 struct got_tree_entry *te;
1846 struct got_object_id *tree_id;
1847 char *te_name;
1849 while (p[0] == '/')
1850 p++;
1852 /* Ensure the correct subtree entry is selected. */
1853 slash = strchr(p, '/');
1854 if (slash == NULL)
1855 te_name = strdup(p);
1856 else
1857 te_name = strndup(p, slash - p);
1858 if (te_name == NULL) {
1859 err = got_error_from_errno("strndup");
1860 break;
1862 te = got_object_tree_find_entry(s->tree, te_name);
1863 if (te == NULL) {
1864 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1865 free(te_name);
1866 break;
1868 free(te_name);
1869 s->first_displayed_entry = s->selected_entry = te;
1871 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1872 break; /* jump to this file's entry */
1874 slash = strchr(p, '/');
1875 if (slash)
1876 subpath = strndup(path, slash - path);
1877 else
1878 subpath = strdup(path);
1879 if (subpath == NULL) {
1880 err = got_error_from_errno("strdup");
1881 break;
1884 err = got_object_id_by_path(&tree_id, repo, commit_id,
1885 subpath);
1886 if (err)
1887 break;
1889 err = got_object_open_as_tree(&tree, repo, tree_id);
1890 free(tree_id);
1891 if (err)
1892 break;
1894 err = tree_view_visit_subtree(tree, s);
1895 if (err) {
1896 got_object_tree_close(tree);
1897 break;
1899 if (slash == NULL)
1900 break;
1901 free(subpath);
1902 subpath = NULL;
1903 p = slash;
1906 free(subpath);
1907 return err;
1910 static const struct got_error *
1911 browse_commit_tree(struct tog_view **new_view, int begin_x,
1912 struct commit_queue_entry *entry, const char *path,
1913 struct got_repository *repo)
1915 const struct got_error *err = NULL;
1916 struct got_tree_object *tree;
1917 struct tog_tree_view_state *s;
1918 struct tog_view *tree_view;
1920 err = got_object_open_as_tree(&tree, repo,
1921 got_object_commit_get_tree_id(entry->commit));
1922 if (err)
1923 return err;
1925 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1926 if (tree_view == NULL)
1927 return got_error_from_errno("view_open");
1929 err = open_tree_view(tree_view, tree, entry->id, repo);
1930 if (err) {
1931 got_object_tree_close(tree);
1932 return err;
1934 s = &tree_view->state.tree;
1936 *new_view = tree_view;
1938 if (got_path_is_root_dir(path))
1939 return NULL;
1941 return tree_view_walk_path(s, entry->id, path, repo);
1944 static const struct got_error *
1945 block_signals_used_by_main_thread(void)
1947 sigset_t sigset;
1948 int errcode;
1950 if (sigemptyset(&sigset) == -1)
1951 return got_error_from_errno("sigemptyset");
1953 /* tog handles SIGWINCH and SIGCONT */
1954 if (sigaddset(&sigset, SIGWINCH) == -1)
1955 return got_error_from_errno("sigaddset");
1956 if (sigaddset(&sigset, SIGCONT) == -1)
1957 return got_error_from_errno("sigaddset");
1959 /* ncurses handles SIGTSTP */
1960 if (sigaddset(&sigset, SIGTSTP) == -1)
1961 return got_error_from_errno("sigaddset");
1963 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1964 if (errcode)
1965 return got_error_set_errno(errcode, "pthread_sigmask");
1967 return NULL;
1970 static void *
1971 log_thread(void *arg)
1973 const struct got_error *err = NULL;
1974 int errcode = 0;
1975 struct tog_log_thread_args *a = arg;
1976 int done = 0;
1978 err = block_signals_used_by_main_thread();
1979 if (err)
1980 return (void *)err;
1982 while (!done && !err && !tog_sigpipe_received) {
1983 err = queue_commits(a->graph, a->commits, 1, a->repo,
1984 a->in_repo_path, a->searching, a->search_next_done,
1985 a->regex);
1986 if (err) {
1987 if (err->code != GOT_ERR_ITER_COMPLETED)
1988 return (void *)err;
1989 err = NULL;
1990 done = 1;
1991 } else if (a->commits_needed > 0)
1992 a->commits_needed--;
1994 errcode = pthread_mutex_lock(&tog_mutex);
1995 if (errcode) {
1996 err = got_error_set_errno(errcode,
1997 "pthread_mutex_lock");
1998 break;
1999 } else if (*a->quit)
2000 done = 1;
2001 else if (*a->first_displayed_entry == NULL) {
2002 *a->first_displayed_entry =
2003 TAILQ_FIRST(&a->commits->head);
2004 *a->selected_entry = *a->first_displayed_entry;
2007 errcode = pthread_cond_signal(&a->commit_loaded);
2008 if (errcode) {
2009 err = got_error_set_errno(errcode,
2010 "pthread_cond_signal");
2011 pthread_mutex_unlock(&tog_mutex);
2012 break;
2015 if (done)
2016 a->commits_needed = 0;
2017 else {
2018 if (a->commits_needed == 0) {
2019 errcode = pthread_cond_wait(&a->need_commits,
2020 &tog_mutex);
2021 if (errcode)
2022 err = got_error_set_errno(errcode,
2023 "pthread_cond_wait");
2027 errcode = pthread_mutex_unlock(&tog_mutex);
2028 if (errcode && err == NULL)
2029 err = got_error_set_errno(errcode,
2030 "pthread_mutex_unlock");
2032 a->log_complete = 1;
2033 return (void *)err;
2036 static const struct got_error *
2037 stop_log_thread(struct tog_log_view_state *s)
2039 const struct got_error *err = NULL;
2040 int errcode;
2042 if (s->thread) {
2043 s->quit = 1;
2044 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2045 if (errcode)
2046 return got_error_set_errno(errcode,
2047 "pthread_cond_signal");
2048 errcode = pthread_mutex_unlock(&tog_mutex);
2049 if (errcode)
2050 return got_error_set_errno(errcode,
2051 "pthread_mutex_unlock");
2052 errcode = pthread_join(s->thread, (void **)&err);
2053 if (errcode)
2054 return got_error_set_errno(errcode, "pthread_join");
2055 errcode = pthread_mutex_lock(&tog_mutex);
2056 if (errcode)
2057 return got_error_set_errno(errcode,
2058 "pthread_mutex_lock");
2059 s->thread = NULL;
2062 if (s->thread_args.repo) {
2063 got_repo_close(s->thread_args.repo);
2064 s->thread_args.repo = NULL;
2067 if (s->thread_args.graph) {
2068 got_commit_graph_close(s->thread_args.graph);
2069 s->thread_args.graph = NULL;
2072 return err;
2075 static const struct got_error *
2076 close_log_view(struct tog_view *view)
2078 const struct got_error *err = NULL;
2079 struct tog_log_view_state *s = &view->state.log;
2080 int errcode;
2082 err = stop_log_thread(s);
2084 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2085 if (errcode && err == NULL)
2086 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2088 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2089 if (errcode && err == NULL)
2090 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2092 free_commits(&s->commits);
2093 free(s->in_repo_path);
2094 s->in_repo_path = NULL;
2095 free(s->start_id);
2096 s->start_id = NULL;
2097 got_ref_list_free(&s->refs);
2098 return err;
2101 static const struct got_error *
2102 search_start_log_view(struct tog_view *view)
2104 struct tog_log_view_state *s = &view->state.log;
2106 s->matched_entry = NULL;
2107 s->search_entry = NULL;
2108 return NULL;
2111 static const struct got_error *
2112 search_next_log_view(struct tog_view *view)
2114 const struct got_error *err = NULL;
2115 struct tog_log_view_state *s = &view->state.log;
2116 struct commit_queue_entry *entry;
2118 /* Display progress update in log view. */
2119 show_log_view(view);
2120 update_panels();
2121 doupdate();
2123 if (s->search_entry) {
2124 int errcode, ch;
2125 errcode = pthread_mutex_unlock(&tog_mutex);
2126 if (errcode)
2127 return got_error_set_errno(errcode,
2128 "pthread_mutex_unlock");
2129 ch = wgetch(view->window);
2130 errcode = pthread_mutex_lock(&tog_mutex);
2131 if (errcode)
2132 return got_error_set_errno(errcode,
2133 "pthread_mutex_lock");
2134 if (ch == KEY_BACKSPACE) {
2135 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2136 return NULL;
2138 if (view->searching == TOG_SEARCH_FORWARD)
2139 entry = TAILQ_NEXT(s->search_entry, entry);
2140 else
2141 entry = TAILQ_PREV(s->search_entry,
2142 commit_queue_head, entry);
2143 } else if (s->matched_entry) {
2144 if (view->searching == TOG_SEARCH_FORWARD)
2145 entry = TAILQ_NEXT(s->matched_entry, entry);
2146 else
2147 entry = TAILQ_PREV(s->matched_entry,
2148 commit_queue_head, entry);
2149 } else {
2150 if (view->searching == TOG_SEARCH_FORWARD)
2151 entry = TAILQ_FIRST(&s->commits.head);
2152 else
2153 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2156 while (1) {
2157 int have_match = 0;
2159 if (entry == NULL) {
2160 if (s->thread_args.log_complete ||
2161 view->searching == TOG_SEARCH_BACKWARD) {
2162 view->search_next_done =
2163 (s->matched_entry == NULL ?
2164 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2165 s->search_entry = NULL;
2166 return NULL;
2169 * Poke the log thread for more commits and return,
2170 * allowing the main loop to make progress. Search
2171 * will resume at s->search_entry once we come back.
2173 s->thread_args.commits_needed++;
2174 return trigger_log_thread(view, 0);
2177 err = match_commit(&have_match, entry->id, entry->commit,
2178 &view->regex);
2179 if (err)
2180 break;
2181 if (have_match) {
2182 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2183 s->matched_entry = entry;
2184 break;
2187 s->search_entry = entry;
2188 if (view->searching == TOG_SEARCH_FORWARD)
2189 entry = TAILQ_NEXT(entry, entry);
2190 else
2191 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2194 if (s->matched_entry) {
2195 int cur = s->selected_entry->idx;
2196 while (cur < s->matched_entry->idx) {
2197 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2198 if (err)
2199 return err;
2200 cur++;
2202 while (cur > s->matched_entry->idx) {
2203 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2204 if (err)
2205 return err;
2206 cur--;
2210 s->search_entry = NULL;
2212 return NULL;
2215 static const struct got_error *
2216 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2217 struct got_repository *repo, const char *head_ref_name,
2218 const char *in_repo_path, int log_branches)
2220 const struct got_error *err = NULL;
2221 struct tog_log_view_state *s = &view->state.log;
2222 struct got_repository *thread_repo = NULL;
2223 struct got_commit_graph *thread_graph = NULL;
2224 int errcode;
2226 SIMPLEQ_INIT(&s->refs);
2228 if (in_repo_path != s->in_repo_path) {
2229 free(s->in_repo_path);
2230 s->in_repo_path = strdup(in_repo_path);
2231 if (s->in_repo_path == NULL)
2232 return got_error_from_errno("strdup");
2235 /* The commit queue only contains commits being displayed. */
2236 TAILQ_INIT(&s->commits.head);
2237 s->commits.ncommits = 0;
2239 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2240 if (err)
2241 goto done;
2243 s->repo = repo;
2244 s->head_ref_name = head_ref_name;
2245 s->start_id = got_object_id_dup(start_id);
2246 if (s->start_id == NULL) {
2247 err = got_error_from_errno("got_object_id_dup");
2248 goto done;
2250 s->log_branches = log_branches;
2252 SIMPLEQ_INIT(&s->colors);
2253 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2254 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2255 get_color_value("TOG_COLOR_COMMIT"));
2256 if (err)
2257 goto done;
2258 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2259 get_color_value("TOG_COLOR_AUTHOR"));
2260 if (err) {
2261 free_colors(&s->colors);
2262 goto done;
2264 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2265 get_color_value("TOG_COLOR_DATE"));
2266 if (err) {
2267 free_colors(&s->colors);
2268 goto done;
2272 view->show = show_log_view;
2273 view->input = input_log_view;
2274 view->close = close_log_view;
2275 view->search_start = search_start_log_view;
2276 view->search_next = search_next_log_view;
2278 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2279 if (err)
2280 goto done;
2281 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2282 !s->log_branches);
2283 if (err)
2284 goto done;
2285 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2286 s->repo, NULL, NULL);
2287 if (err)
2288 goto done;
2290 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2291 if (errcode) {
2292 err = got_error_set_errno(errcode, "pthread_cond_init");
2293 goto done;
2295 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2296 if (errcode) {
2297 err = got_error_set_errno(errcode, "pthread_cond_init");
2298 goto done;
2301 s->thread_args.commits_needed = view->nlines;
2302 s->thread_args.graph = thread_graph;
2303 s->thread_args.commits = &s->commits;
2304 s->thread_args.in_repo_path = s->in_repo_path;
2305 s->thread_args.start_id = s->start_id;
2306 s->thread_args.repo = thread_repo;
2307 s->thread_args.log_complete = 0;
2308 s->thread_args.quit = &s->quit;
2309 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2310 s->thread_args.selected_entry = &s->selected_entry;
2311 s->thread_args.searching = &view->searching;
2312 s->thread_args.search_next_done = &view->search_next_done;
2313 s->thread_args.regex = &view->regex;
2314 done:
2315 if (err)
2316 close_log_view(view);
2317 return err;
2320 static const struct got_error *
2321 show_log_view(struct tog_view *view)
2323 const struct got_error *err;
2324 struct tog_log_view_state *s = &view->state.log;
2326 if (s->thread == NULL) {
2327 int errcode = pthread_create(&s->thread, NULL, log_thread,
2328 &s->thread_args);
2329 if (errcode)
2330 return got_error_set_errno(errcode, "pthread_create");
2331 if (s->thread_args.commits_needed > 0) {
2332 err = trigger_log_thread(view, 1);
2333 if (err)
2334 return err;
2338 return draw_commits(view);
2341 static const struct got_error *
2342 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2343 struct tog_view **focus_view, struct tog_view *view, int ch)
2345 const struct got_error *err = NULL;
2346 struct tog_log_view_state *s = &view->state.log;
2347 char *parent_path, *in_repo_path = NULL;
2348 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2349 struct tog_view *ref_view = NULL;
2350 int begin_x = 0;
2351 struct got_object_id *start_id;
2353 switch (ch) {
2354 case 'q':
2355 s->quit = 1;
2356 break;
2357 case 'k':
2358 case KEY_UP:
2359 case '<':
2360 case ',':
2361 if (s->first_displayed_entry == NULL)
2362 break;
2363 if (s->selected > 0)
2364 s->selected--;
2365 else
2366 log_scroll_up(view, 1);
2367 break;
2368 case KEY_PPAGE:
2369 case CTRL('b'):
2370 if (s->first_displayed_entry == NULL)
2371 break;
2372 if (TAILQ_FIRST(&s->commits.head) ==
2373 s->first_displayed_entry) {
2374 s->selected = 0;
2375 break;
2377 log_scroll_up(view, view->nlines - 1);
2378 break;
2379 case 'j':
2380 case KEY_DOWN:
2381 case '>':
2382 case '.':
2383 if (s->first_displayed_entry == NULL)
2384 break;
2385 if (s->selected < MIN(view->nlines - 2,
2386 s->commits.ncommits - 1)) {
2387 s->selected++;
2388 break;
2390 err = log_scroll_down(view, 1);
2391 break;
2392 case KEY_NPAGE:
2393 case CTRL('f'): {
2394 struct commit_queue_entry *first;
2395 first = s->first_displayed_entry;
2396 if (first == NULL)
2397 break;
2398 err = log_scroll_down(view, view->nlines - 1);
2399 if (err)
2400 break;
2401 if (first == s->first_displayed_entry &&
2402 s->selected < MIN(view->nlines - 2,
2403 s->commits.ncommits - 1)) {
2404 /* can't scroll further down */
2405 s->selected = MIN(view->nlines - 2,
2406 s->commits.ncommits - 1);
2408 err = NULL;
2409 break;
2411 case KEY_RESIZE:
2412 if (s->selected > view->nlines - 2)
2413 s->selected = view->nlines - 2;
2414 if (s->selected > s->commits.ncommits - 1)
2415 s->selected = s->commits.ncommits - 1;
2416 break;
2417 case KEY_ENTER:
2418 case ' ':
2419 case '\r':
2420 if (s->selected_entry == NULL)
2421 break;
2422 if (view_is_parent_view(view))
2423 begin_x = view_split_begin_x(view->begin_x);
2424 err = open_diff_view_for_commit(&diff_view, begin_x,
2425 s->selected_entry->commit, s->selected_entry->id,
2426 view, s->repo);
2427 if (err)
2428 break;
2429 if (view_is_parent_view(view)) {
2430 err = view_close_child(view);
2431 if (err)
2432 return err;
2433 err = view_set_child(view, diff_view);
2434 if (err) {
2435 view_close(diff_view);
2436 break;
2438 *focus_view = diff_view;
2439 view->child_focussed = 1;
2440 } else
2441 *new_view = diff_view;
2442 break;
2443 case 't':
2444 if (s->selected_entry == NULL)
2445 break;
2446 if (view_is_parent_view(view))
2447 begin_x = view_split_begin_x(view->begin_x);
2448 err = browse_commit_tree(&tree_view, begin_x,
2449 s->selected_entry, s->in_repo_path, s->repo);
2450 if (err)
2451 break;
2452 if (view_is_parent_view(view)) {
2453 err = view_close_child(view);
2454 if (err)
2455 return err;
2456 err = view_set_child(view, tree_view);
2457 if (err) {
2458 view_close(tree_view);
2459 break;
2461 *focus_view = tree_view;
2462 view->child_focussed = 1;
2463 } else
2464 *new_view = tree_view;
2465 break;
2466 case KEY_BACKSPACE:
2467 if (got_path_cmp(s->in_repo_path, "/",
2468 strlen(s->in_repo_path), 1) == 0)
2469 break;
2470 err = got_path_dirname(&parent_path, s->in_repo_path);
2471 if (err)
2472 return err;
2473 err = stop_log_thread(s);
2474 if (err) {
2475 free(parent_path);
2476 return err;
2478 lv = view_open(view->nlines, view->ncols,
2479 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2480 if (lv == NULL) {
2481 free(parent_path);
2482 return got_error_from_errno("view_open");
2484 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2485 parent_path, s->log_branches);
2486 free(parent_path);
2487 if (err)
2488 return err;;
2489 if (view_is_parent_view(view))
2490 *new_view = lv;
2491 else {
2492 view_set_child(view->parent, lv);
2493 *focus_view = lv;
2495 break;
2496 case CTRL('l'):
2497 err = stop_log_thread(s);
2498 if (err)
2499 return err;
2500 lv = view_open(view->nlines, view->ncols,
2501 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2502 if (lv == NULL)
2503 return got_error_from_errno("view_open");
2504 err = got_repo_match_object_id(&start_id, NULL,
2505 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2506 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2507 if (err) {
2508 view_close(lv);
2509 return err;
2511 in_repo_path = strdup(s->in_repo_path);
2512 if (in_repo_path == NULL) {
2513 free(start_id);
2514 view_close(lv);
2515 return got_error_from_errno("strdup");
2517 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2518 in_repo_path, s->log_branches);
2519 if (err) {
2520 free(start_id);
2521 view_close(lv);
2522 return err;;
2524 *dead_view = view;
2525 *new_view = lv;
2526 break;
2527 case 'B':
2528 s->log_branches = !s->log_branches;
2529 err = stop_log_thread(s);
2530 if (err)
2531 return err;
2532 lv = view_open(view->nlines, view->ncols,
2533 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2534 if (lv == NULL)
2535 return got_error_from_errno("view_open");
2536 err = open_log_view(lv, s->start_id, s->repo,
2537 s->head_ref_name, s->in_repo_path, s->log_branches);
2538 if (err) {
2539 view_close(lv);
2540 return err;;
2542 *dead_view = view;
2543 *new_view = lv;
2544 break;
2545 case 'r':
2546 if (view_is_parent_view(view))
2547 begin_x = view_split_begin_x(view->begin_x);
2548 ref_view = view_open(view->nlines, view->ncols,
2549 view->begin_y, begin_x, TOG_VIEW_REF);
2550 if (ref_view == NULL)
2551 return got_error_from_errno("view_open");
2552 err = open_ref_view(ref_view, s->repo);
2553 if (err) {
2554 view_close(ref_view);
2555 return err;
2557 if (view_is_parent_view(view)) {
2558 err = view_close_child(view);
2559 if (err)
2560 return err;
2561 err = view_set_child(view, ref_view);
2562 if (err) {
2563 view_close(ref_view);
2564 break;
2566 *focus_view = ref_view;
2567 view->child_focussed = 1;
2568 } else
2569 *new_view = ref_view;
2570 break;
2571 default:
2572 break;
2575 return err;
2578 static const struct got_error *
2579 apply_unveil(const char *repo_path, const char *worktree_path)
2581 const struct got_error *error;
2583 #ifdef PROFILE
2584 if (unveil("gmon.out", "rwc") != 0)
2585 return got_error_from_errno2("unveil", "gmon.out");
2586 #endif
2587 if (repo_path && unveil(repo_path, "r") != 0)
2588 return got_error_from_errno2("unveil", repo_path);
2590 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2591 return got_error_from_errno2("unveil", worktree_path);
2593 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2594 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2596 error = got_privsep_unveil_exec_helpers();
2597 if (error != NULL)
2598 return error;
2600 if (unveil(NULL, NULL) != 0)
2601 return got_error_from_errno("unveil");
2603 return NULL;
2606 static void
2607 init_curses(void)
2609 initscr();
2610 cbreak();
2611 halfdelay(1); /* Do fast refresh while initial view is loading. */
2612 noecho();
2613 nonl();
2614 intrflush(stdscr, FALSE);
2615 keypad(stdscr, TRUE);
2616 curs_set(0);
2617 if (getenv("TOG_COLORS") != NULL) {
2618 start_color();
2619 use_default_colors();
2621 signal(SIGWINCH, tog_sigwinch);
2622 signal(SIGPIPE, tog_sigpipe);
2623 signal(SIGCONT, tog_sigcont);
2626 static const struct got_error *
2627 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2628 struct got_repository *repo, struct got_worktree *worktree)
2630 const struct got_error *err = NULL;
2632 if (argc == 0) {
2633 *in_repo_path = strdup("/");
2634 if (*in_repo_path == NULL)
2635 return got_error_from_errno("strdup");
2636 return NULL;
2639 if (worktree) {
2640 const char *prefix = got_worktree_get_path_prefix(worktree);
2641 char *p;
2643 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2644 if (err)
2645 return err;
2646 if (asprintf(in_repo_path, "%s%s%s", prefix,
2647 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2648 p) == -1) {
2649 err = got_error_from_errno("asprintf");
2650 *in_repo_path = NULL;
2652 free(p);
2653 } else
2654 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2656 return err;
2659 static const struct got_error *
2660 cmd_log(int argc, char *argv[])
2662 const struct got_error *error;
2663 struct got_repository *repo = NULL;
2664 struct got_worktree *worktree = NULL;
2665 struct got_object_id *start_id = NULL;
2666 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2667 char *start_commit = NULL, *head_ref_name = NULL;
2668 int ch, log_branches = 0;
2669 struct tog_view *view;
2671 #ifndef PROFILE
2672 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2673 NULL) == -1)
2674 err(1, "pledge");
2675 #endif
2677 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2678 switch (ch) {
2679 case 'b':
2680 log_branches = 1;
2681 break;
2682 case 'c':
2683 start_commit = optarg;
2684 break;
2685 case 'r':
2686 repo_path = realpath(optarg, NULL);
2687 if (repo_path == NULL)
2688 return got_error_from_errno2("realpath",
2689 optarg);
2690 break;
2691 default:
2692 usage_log();
2693 /* NOTREACHED */
2697 argc -= optind;
2698 argv += optind;
2700 if (argc > 1)
2701 usage_log();
2703 cwd = getcwd(NULL, 0);
2704 if (cwd == NULL)
2705 return got_error_from_errno("getcwd");
2707 error = got_worktree_open(&worktree, cwd);
2708 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2709 goto done;
2711 if (repo_path == NULL) {
2712 if (worktree)
2713 repo_path =
2714 strdup(got_worktree_get_repo_path(worktree));
2715 else
2716 repo_path = strdup(cwd);
2718 if (repo_path == NULL) {
2719 error = got_error_from_errno("strdup");
2720 goto done;
2723 error = got_repo_open(&repo, repo_path, NULL);
2724 if (error != NULL)
2725 goto done;
2727 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2728 repo, worktree);
2729 if (error)
2730 goto done;
2732 init_curses();
2734 error = apply_unveil(got_repo_get_path(repo),
2735 worktree ? got_worktree_get_root_path(worktree) : NULL);
2736 if (error)
2737 goto done;
2739 if (start_commit == NULL)
2740 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2741 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2742 GOT_OBJ_TYPE_COMMIT, 1, repo);
2743 else
2744 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2745 GOT_OBJ_TYPE_COMMIT, 1, repo);
2746 if (error != NULL)
2747 goto done;
2749 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2750 if (view == NULL) {
2751 error = got_error_from_errno("view_open");
2752 goto done;
2754 if (worktree) {
2755 head_ref_name = strdup(
2756 got_worktree_get_head_ref_name(worktree));
2757 if (head_ref_name == NULL) {
2758 error = got_error_from_errno("strdup");
2759 goto done;
2762 error = open_log_view(view, start_id, repo, head_ref_name,
2763 in_repo_path, log_branches);
2764 if (error)
2765 goto done;
2766 if (worktree) {
2767 /* Release work tree lock. */
2768 got_worktree_close(worktree);
2769 worktree = NULL;
2771 error = view_loop(view);
2772 done:
2773 free(in_repo_path);
2774 free(repo_path);
2775 free(cwd);
2776 free(start_id);
2777 free(head_ref_name);
2778 if (repo)
2779 got_repo_close(repo);
2780 if (worktree)
2781 got_worktree_close(worktree);
2782 return error;
2785 __dead static void
2786 usage_diff(void)
2788 endwin();
2789 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2790 "[-w] object1 object2\n", getprogname());
2791 exit(1);
2794 static char *
2795 parse_next_line(FILE *f, size_t *len)
2797 char *line;
2798 size_t linelen;
2799 size_t lineno;
2800 const char delim[3] = { '\0', '\0', '\0'};
2802 line = fparseln(f, &linelen, &lineno, delim, 0);
2803 if (len)
2804 *len = linelen;
2805 return line;
2808 static int
2809 match_line(const char *line, regex_t *regex, size_t nmatch,
2810 regmatch_t *regmatch)
2812 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2815 struct tog_color *
2816 match_color(struct tog_colors *colors, const char *line)
2818 struct tog_color *tc = NULL;
2820 SIMPLEQ_FOREACH(tc, colors, entry) {
2821 if (match_line(line, &tc->regex, 0, NULL))
2822 return tc;
2825 return NULL;
2828 static const struct got_error *
2829 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2830 WINDOW *window, regmatch_t *regmatch)
2832 const struct got_error *err = NULL;
2833 wchar_t *wline;
2834 int width;
2835 char *s;
2837 *wtotal = 0;
2839 s = strndup(line, regmatch->rm_so);
2840 if (s == NULL)
2841 return got_error_from_errno("strndup");
2843 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2844 if (err) {
2845 free(s);
2846 return err;
2848 waddwstr(window, wline);
2849 free(wline);
2850 free(s);
2851 wlimit -= width;
2852 *wtotal += width;
2854 if (wlimit > 0) {
2855 s = strndup(line + regmatch->rm_so,
2856 regmatch->rm_eo - regmatch->rm_so);
2857 if (s == NULL) {
2858 err = got_error_from_errno("strndup");
2859 free(s);
2860 return err;
2862 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2863 if (err) {
2864 free(s);
2865 return err;
2867 wattr_on(window, A_STANDOUT, NULL);
2868 waddwstr(window, wline);
2869 wattr_off(window, A_STANDOUT, NULL);
2870 free(wline);
2871 free(s);
2872 wlimit -= width;
2873 *wtotal += width;
2876 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2877 err = format_line(&wline, &width,
2878 line + regmatch->rm_eo, wlimit, col_tab_align);
2879 if (err)
2880 return err;
2881 waddwstr(window, wline);
2882 free(wline);
2883 *wtotal += width;
2886 return NULL;
2889 static const struct got_error *
2890 draw_file(struct tog_view *view, const char *header)
2892 struct tog_diff_view_state *s = &view->state.diff;
2893 regmatch_t *regmatch = &view->regmatch;
2894 const struct got_error *err;
2895 int nprinted = 0;
2896 char *line;
2897 struct tog_color *tc;
2898 size_t len;
2899 wchar_t *wline;
2900 int width;
2901 int max_lines = view->nlines;
2902 int nlines = s->nlines;
2903 off_t line_offset;
2905 line_offset = s->line_offsets[s->first_displayed_line - 1];
2906 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2907 return got_error_from_errno("fseek");
2909 werase(view->window);
2911 if (header) {
2912 if (asprintf(&line, "[%d/%d] %s",
2913 s->first_displayed_line - 1 + s->selected_line, nlines,
2914 header) == -1)
2915 return got_error_from_errno("asprintf");
2916 err = format_line(&wline, &width, line, view->ncols, 0);
2917 free(line);
2918 if (err)
2919 return err;
2921 if (view_needs_focus_indication(view))
2922 wstandout(view->window);
2923 waddwstr(view->window, wline);
2924 free(wline);
2925 wline = NULL;
2926 if (view_needs_focus_indication(view))
2927 wstandend(view->window);
2928 if (width <= view->ncols - 1)
2929 waddch(view->window, '\n');
2931 if (max_lines <= 1)
2932 return NULL;
2933 max_lines--;
2936 s->eof = 0;
2937 while (max_lines > 0 && nprinted < max_lines) {
2938 line = parse_next_line(s->f, &len);
2939 if (line == NULL) {
2940 s->eof = 1;
2941 break;
2944 tc = match_color(&s->colors, line);
2945 if (tc)
2946 wattr_on(view->window,
2947 COLOR_PAIR(tc->colorpair), NULL);
2948 if (s->first_displayed_line + nprinted == s->matched_line &&
2949 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2950 err = add_matched_line(&width, line, view->ncols, 0,
2951 view->window, regmatch);
2952 if (err) {
2953 free(line);
2954 return err;
2956 } else {
2957 err = format_line(&wline, &width, line, view->ncols, 0);
2958 if (err) {
2959 free(line);
2960 return err;
2962 waddwstr(view->window, wline);
2963 free(wline);
2964 wline = NULL;
2966 if (tc)
2967 wattr_off(view->window,
2968 COLOR_PAIR(tc->colorpair), NULL);
2969 if (width <= view->ncols - 1)
2970 waddch(view->window, '\n');
2971 nprinted++;
2972 free(line);
2974 if (nprinted >= 1)
2975 s->last_displayed_line = s->first_displayed_line +
2976 (nprinted - 1);
2977 else
2978 s->last_displayed_line = s->first_displayed_line;
2980 view_vborder(view);
2982 if (s->eof) {
2983 while (nprinted < view->nlines) {
2984 waddch(view->window, '\n');
2985 nprinted++;
2988 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2989 if (err) {
2990 return err;
2993 wstandout(view->window);
2994 waddwstr(view->window, wline);
2995 free(wline);
2996 wline = NULL;
2997 wstandend(view->window);
3000 return NULL;
3003 static char *
3004 get_datestr(time_t *time, char *datebuf)
3006 struct tm mytm, *tm;
3007 char *p, *s;
3009 tm = gmtime_r(time, &mytm);
3010 if (tm == NULL)
3011 return NULL;
3012 s = asctime_r(tm, datebuf);
3013 if (s == NULL)
3014 return NULL;
3015 p = strchr(s, '\n');
3016 if (p)
3017 *p = '\0';
3018 return s;
3021 static const struct got_error *
3022 get_changed_paths(struct got_pathlist_head *paths,
3023 struct got_commit_object *commit, struct got_repository *repo)
3025 const struct got_error *err = NULL;
3026 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3027 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3028 struct got_object_qid *qid;
3030 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3031 if (qid != NULL) {
3032 struct got_commit_object *pcommit;
3033 err = got_object_open_as_commit(&pcommit, repo,
3034 qid->id);
3035 if (err)
3036 return err;
3038 tree_id1 = got_object_commit_get_tree_id(pcommit);
3039 got_object_commit_close(pcommit);
3043 if (tree_id1) {
3044 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3045 if (err)
3046 goto done;
3049 tree_id2 = got_object_commit_get_tree_id(commit);
3050 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3051 if (err)
3052 goto done;
3054 err = got_diff_tree(tree1, tree2, "", "", repo,
3055 got_diff_tree_collect_changed_paths, paths, 0);
3056 done:
3057 if (tree1)
3058 got_object_tree_close(tree1);
3059 if (tree2)
3060 got_object_tree_close(tree2);
3061 return err;
3064 static const struct got_error *
3065 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3067 off_t *p;
3069 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3070 if (p == NULL)
3071 return got_error_from_errno("reallocarray");
3072 *line_offsets = p;
3073 (*line_offsets)[*nlines] = off;
3074 (*nlines)++;
3075 return NULL;
3078 static const struct got_error *
3079 write_commit_info(off_t **line_offsets, size_t *nlines,
3080 struct got_object_id *commit_id, struct got_reflist_head *refs,
3081 struct got_repository *repo, FILE *outfile)
3083 const struct got_error *err = NULL;
3084 char datebuf[26], *datestr;
3085 struct got_commit_object *commit;
3086 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3087 time_t committer_time;
3088 const char *author, *committer;
3089 char *refs_str = NULL;
3090 struct got_pathlist_head changed_paths;
3091 struct got_pathlist_entry *pe;
3092 off_t outoff = 0;
3093 int n;
3095 TAILQ_INIT(&changed_paths);
3097 if (refs) {
3098 err = build_refs_str(&refs_str, refs, commit_id, repo);
3099 if (err)
3100 return err;
3103 err = got_object_open_as_commit(&commit, repo, commit_id);
3104 if (err)
3105 return err;
3107 err = got_object_id_str(&id_str, commit_id);
3108 if (err) {
3109 err = got_error_from_errno("got_object_id_str");
3110 goto done;
3113 err = add_line_offset(line_offsets, nlines, 0);
3114 if (err)
3115 goto done;
3117 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3118 refs_str ? refs_str : "", refs_str ? ")" : "");
3119 if (n < 0) {
3120 err = got_error_from_errno("fprintf");
3121 goto done;
3123 outoff += n;
3124 err = add_line_offset(line_offsets, nlines, outoff);
3125 if (err)
3126 goto done;
3128 n = fprintf(outfile, "from: %s\n",
3129 got_object_commit_get_author(commit));
3130 if (n < 0) {
3131 err = got_error_from_errno("fprintf");
3132 goto done;
3134 outoff += n;
3135 err = add_line_offset(line_offsets, nlines, outoff);
3136 if (err)
3137 goto done;
3139 committer_time = got_object_commit_get_committer_time(commit);
3140 datestr = get_datestr(&committer_time, datebuf);
3141 if (datestr) {
3142 n = fprintf(outfile, "date: %s UTC\n", datestr);
3143 if (n < 0) {
3144 err = got_error_from_errno("fprintf");
3145 goto done;
3147 outoff += n;
3148 err = add_line_offset(line_offsets, nlines, outoff);
3149 if (err)
3150 goto done;
3152 author = got_object_commit_get_author(commit);
3153 committer = got_object_commit_get_committer(commit);
3154 if (strcmp(author, committer) != 0) {
3155 n = fprintf(outfile, "via: %s\n", committer);
3156 if (n < 0) {
3157 err = got_error_from_errno("fprintf");
3158 goto done;
3160 outoff += n;
3161 err = add_line_offset(line_offsets, nlines, outoff);
3162 if (err)
3163 goto done;
3165 err = got_object_commit_get_logmsg(&logmsg, commit);
3166 if (err)
3167 goto done;
3168 s = logmsg;
3169 while ((line = strsep(&s, "\n")) != NULL) {
3170 n = fprintf(outfile, "%s\n", line);
3171 if (n < 0) {
3172 err = got_error_from_errno("fprintf");
3173 goto done;
3175 outoff += n;
3176 err = add_line_offset(line_offsets, nlines, outoff);
3177 if (err)
3178 goto done;
3181 err = get_changed_paths(&changed_paths, commit, repo);
3182 if (err)
3183 goto done;
3184 TAILQ_FOREACH(pe, &changed_paths, entry) {
3185 struct got_diff_changed_path *cp = pe->data;
3186 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3187 if (n < 0) {
3188 err = got_error_from_errno("fprintf");
3189 goto done;
3191 outoff += n;
3192 err = add_line_offset(line_offsets, nlines, outoff);
3193 if (err)
3194 goto done;
3195 free((char *)pe->path);
3196 free(pe->data);
3199 fputc('\n', outfile);
3200 outoff++;
3201 err = add_line_offset(line_offsets, nlines, outoff);
3202 done:
3203 got_pathlist_free(&changed_paths);
3204 free(id_str);
3205 free(logmsg);
3206 free(refs_str);
3207 got_object_commit_close(commit);
3208 if (err) {
3209 free(*line_offsets);
3210 *line_offsets = NULL;
3211 *nlines = 0;
3213 return err;
3216 static const struct got_error *
3217 create_diff(struct tog_diff_view_state *s)
3219 const struct got_error *err = NULL;
3220 FILE *f = NULL;
3221 int obj_type;
3223 free(s->line_offsets);
3224 s->line_offsets = malloc(sizeof(off_t));
3225 if (s->line_offsets == NULL)
3226 return got_error_from_errno("malloc");
3227 s->nlines = 0;
3229 f = got_opentemp();
3230 if (f == NULL) {
3231 err = got_error_from_errno("got_opentemp");
3232 goto done;
3234 if (s->f && fclose(s->f) != 0) {
3235 err = got_error_from_errno("fclose");
3236 goto done;
3238 s->f = f;
3240 if (s->id1)
3241 err = got_object_get_type(&obj_type, s->repo, s->id1);
3242 else
3243 err = got_object_get_type(&obj_type, s->repo, s->id2);
3244 if (err)
3245 goto done;
3247 switch (obj_type) {
3248 case GOT_OBJ_TYPE_BLOB:
3249 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3250 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3251 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3252 break;
3253 case GOT_OBJ_TYPE_TREE:
3254 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3255 s->id1, s->id2, "", "", s->diff_context,
3256 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3257 break;
3258 case GOT_OBJ_TYPE_COMMIT: {
3259 const struct got_object_id_queue *parent_ids;
3260 struct got_object_qid *pid;
3261 struct got_commit_object *commit2;
3263 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3264 if (err)
3265 goto done;
3266 /* Show commit info if we're diffing to a parent/root commit. */
3267 if (s->id1 == NULL) {
3268 err = write_commit_info(&s->line_offsets, &s->nlines,
3269 s->id2, &s->refs, s->repo, s->f);
3270 if (err)
3271 goto done;
3272 } else {
3273 parent_ids = got_object_commit_get_parent_ids(commit2);
3274 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3275 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3276 err = write_commit_info(
3277 &s->line_offsets, &s->nlines,
3278 s->id2, &s->refs, s->repo, s->f);
3279 if (err)
3280 goto done;
3281 break;
3285 got_object_commit_close(commit2);
3287 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3288 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3289 s->force_text_diff, s->repo, s->f);
3290 break;
3292 default:
3293 err = got_error(GOT_ERR_OBJ_TYPE);
3294 break;
3296 if (err)
3297 goto done;
3298 done:
3299 if (s->f && fflush(s->f) != 0 && err == NULL)
3300 err = got_error_from_errno("fflush");
3301 return err;
3304 static void
3305 diff_view_indicate_progress(struct tog_view *view)
3307 mvwaddstr(view->window, 0, 0, "diffing...");
3308 update_panels();
3309 doupdate();
3312 static const struct got_error *
3313 search_start_diff_view(struct tog_view *view)
3315 struct tog_diff_view_state *s = &view->state.diff;
3317 s->matched_line = 0;
3318 return NULL;
3321 static const struct got_error *
3322 search_next_diff_view(struct tog_view *view)
3324 struct tog_diff_view_state *s = &view->state.diff;
3325 int lineno;
3327 if (!view->searching) {
3328 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3329 return NULL;
3332 if (s->matched_line) {
3333 if (view->searching == TOG_SEARCH_FORWARD)
3334 lineno = s->matched_line + 1;
3335 else
3336 lineno = s->matched_line - 1;
3337 } else {
3338 if (view->searching == TOG_SEARCH_FORWARD)
3339 lineno = 1;
3340 else
3341 lineno = s->nlines;
3344 while (1) {
3345 char *line = NULL;
3346 off_t offset;
3347 size_t len;
3349 if (lineno <= 0 || lineno > s->nlines) {
3350 if (s->matched_line == 0) {
3351 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3352 free(line);
3353 break;
3356 if (view->searching == TOG_SEARCH_FORWARD)
3357 lineno = 1;
3358 else
3359 lineno = s->nlines;
3362 offset = s->line_offsets[lineno - 1];
3363 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3364 free(line);
3365 return got_error_from_errno("fseeko");
3367 free(line);
3368 line = parse_next_line(s->f, &len);
3369 if (line &&
3370 match_line(line, &view->regex, 1, &view->regmatch)) {
3371 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3372 s->matched_line = lineno;
3373 free(line);
3374 break;
3376 free(line);
3377 if (view->searching == TOG_SEARCH_FORWARD)
3378 lineno++;
3379 else
3380 lineno--;
3383 if (s->matched_line) {
3384 s->first_displayed_line = s->matched_line;
3385 s->selected_line = 1;
3388 return NULL;
3391 static const struct got_error *
3392 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3393 struct got_object_id *id2, const char *label1, const char *label2,
3394 int diff_context, int ignore_whitespace, int force_text_diff,
3395 struct tog_view *log_view, struct got_repository *repo)
3397 const struct got_error *err;
3398 struct tog_diff_view_state *s = &view->state.diff;
3400 SIMPLEQ_INIT(&s->refs);
3402 if (id1 != NULL && id2 != NULL) {
3403 int type1, type2;
3404 err = got_object_get_type(&type1, repo, id1);
3405 if (err)
3406 return err;
3407 err = got_object_get_type(&type2, repo, id2);
3408 if (err)
3409 return err;
3411 if (type1 != type2)
3412 return got_error(GOT_ERR_OBJ_TYPE);
3414 s->first_displayed_line = 1;
3415 s->last_displayed_line = view->nlines;
3416 s->selected_line = 1;
3417 s->repo = repo;
3418 s->id1 = id1;
3419 s->id2 = id2;
3420 s->label1 = label1;
3421 s->label2 = label2;
3423 if (id1) {
3424 s->id1 = got_object_id_dup(id1);
3425 if (s->id1 == NULL)
3426 return got_error_from_errno("got_object_id_dup");
3427 } else
3428 s->id1 = NULL;
3430 s->id2 = got_object_id_dup(id2);
3431 if (s->id2 == NULL) {
3432 free(s->id1);
3433 s->id1 = NULL;
3434 return got_error_from_errno("got_object_id_dup");
3436 s->f = NULL;
3437 s->first_displayed_line = 1;
3438 s->last_displayed_line = view->nlines;
3439 s->diff_context = diff_context;
3440 s->ignore_whitespace = ignore_whitespace;
3441 s->force_text_diff = force_text_diff;
3442 s->log_view = log_view;
3443 s->repo = repo;
3445 SIMPLEQ_INIT(&s->colors);
3446 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3447 err = add_color(&s->colors,
3448 "^-", TOG_COLOR_DIFF_MINUS,
3449 get_color_value("TOG_COLOR_DIFF_MINUS"));
3450 if (err)
3451 return err;
3452 err = add_color(&s->colors, "^\\+",
3453 TOG_COLOR_DIFF_PLUS,
3454 get_color_value("TOG_COLOR_DIFF_PLUS"));
3455 if (err) {
3456 free_colors(&s->colors);
3457 return err;
3459 err = add_color(&s->colors,
3460 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3461 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3462 if (err) {
3463 free_colors(&s->colors);
3464 return err;
3467 err = add_color(&s->colors,
3468 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3469 TOG_COLOR_DIFF_META,
3470 get_color_value("TOG_COLOR_DIFF_META"));
3471 if (err) {
3472 free_colors(&s->colors);
3473 return err;
3476 err = add_color(&s->colors,
3477 "^(from|via): ", TOG_COLOR_AUTHOR,
3478 get_color_value("TOG_COLOR_AUTHOR"));
3479 if (err) {
3480 free_colors(&s->colors);
3481 return err;
3484 err = add_color(&s->colors,
3485 "^date: ", TOG_COLOR_DATE,
3486 get_color_value("TOG_COLOR_DATE"));
3487 if (err) {
3488 free_colors(&s->colors);
3489 return err;
3493 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3494 if (err) {
3495 free(s->id1);
3496 s->id1 = NULL;
3497 free(s->id2);
3498 s->id2 = NULL;
3499 free_colors(&s->colors);
3500 return err;
3503 if (log_view && view_is_splitscreen(view))
3504 show_log_view(log_view); /* draw vborder */
3505 diff_view_indicate_progress(view);
3507 s->line_offsets = NULL;
3508 s->nlines = 0;
3509 err = create_diff(s);
3510 if (err) {
3511 free(s->id1);
3512 s->id1 = NULL;
3513 free(s->id2);
3514 s->id2 = NULL;
3515 free_colors(&s->colors);
3516 got_ref_list_free(&s->refs);
3517 return err;
3520 view->show = show_diff_view;
3521 view->input = input_diff_view;
3522 view->close = close_diff_view;
3523 view->search_start = search_start_diff_view;
3524 view->search_next = search_next_diff_view;
3526 return NULL;
3529 static const struct got_error *
3530 close_diff_view(struct tog_view *view)
3532 const struct got_error *err = NULL;
3533 struct tog_diff_view_state *s = &view->state.diff;
3535 free(s->id1);
3536 s->id1 = NULL;
3537 free(s->id2);
3538 s->id2 = NULL;
3539 if (s->f && fclose(s->f) == EOF)
3540 err = got_error_from_errno("fclose");
3541 free_colors(&s->colors);
3542 free(s->line_offsets);
3543 s->line_offsets = NULL;
3544 s->nlines = 0;
3545 got_ref_list_free(&s->refs);
3546 return err;
3549 static const struct got_error *
3550 show_diff_view(struct tog_view *view)
3552 const struct got_error *err;
3553 struct tog_diff_view_state *s = &view->state.diff;
3554 char *id_str1 = NULL, *id_str2, *header;
3555 const char *label1, *label2;
3557 if (s->id1) {
3558 err = got_object_id_str(&id_str1, s->id1);
3559 if (err)
3560 return err;
3561 label1 = s->label1 ? : id_str1;
3562 } else
3563 label1 = "/dev/null";
3565 err = got_object_id_str(&id_str2, s->id2);
3566 if (err)
3567 return err;
3568 label2 = s->label2 ? : id_str2;
3570 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3571 err = got_error_from_errno("asprintf");
3572 free(id_str1);
3573 free(id_str2);
3574 return err;
3576 free(id_str1);
3577 free(id_str2);
3579 return draw_file(view, header);
3582 static const struct got_error *
3583 set_selected_commit(struct tog_diff_view_state *s,
3584 struct commit_queue_entry *entry)
3586 const struct got_error *err;
3587 const struct got_object_id_queue *parent_ids;
3588 struct got_commit_object *selected_commit;
3589 struct got_object_qid *pid;
3591 free(s->id2);
3592 s->id2 = got_object_id_dup(entry->id);
3593 if (s->id2 == NULL)
3594 return got_error_from_errno("got_object_id_dup");
3596 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3597 if (err)
3598 return err;
3599 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3600 free(s->id1);
3601 pid = SIMPLEQ_FIRST(parent_ids);
3602 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3603 got_object_commit_close(selected_commit);
3604 return NULL;
3607 static const struct got_error *
3608 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3609 struct tog_view **focus_view, struct tog_view *view, int ch)
3611 const struct got_error *err = NULL;
3612 struct tog_diff_view_state *s = &view->state.diff;
3613 struct tog_log_view_state *ls;
3614 struct commit_queue_entry *entry;
3615 int i;
3617 switch (ch) {
3618 case 'a':
3619 case 'w':
3620 if (ch == 'a')
3621 s->force_text_diff = !s->force_text_diff;
3622 if (ch == 'w')
3623 s->ignore_whitespace = !s->ignore_whitespace;
3624 wclear(view->window);
3625 s->first_displayed_line = 1;
3626 s->last_displayed_line = view->nlines;
3627 diff_view_indicate_progress(view);
3628 err = create_diff(s);
3629 break;
3630 case 'k':
3631 case KEY_UP:
3632 if (s->first_displayed_line > 1)
3633 s->first_displayed_line--;
3634 break;
3635 case KEY_PPAGE:
3636 case CTRL('b'):
3637 if (s->first_displayed_line == 1)
3638 break;
3639 i = 0;
3640 while (i++ < view->nlines - 1 &&
3641 s->first_displayed_line > 1)
3642 s->first_displayed_line--;
3643 break;
3644 case 'j':
3645 case KEY_DOWN:
3646 if (!s->eof)
3647 s->first_displayed_line++;
3648 break;
3649 case KEY_NPAGE:
3650 case CTRL('f'):
3651 case ' ':
3652 if (s->eof)
3653 break;
3654 i = 0;
3655 while (!s->eof && i++ < view->nlines - 1) {
3656 char *line;
3657 line = parse_next_line(s->f, NULL);
3658 s->first_displayed_line++;
3659 if (line == NULL)
3660 break;
3662 break;
3663 case '[':
3664 if (s->diff_context > 0) {
3665 s->diff_context--;
3666 diff_view_indicate_progress(view);
3667 err = create_diff(s);
3668 if (s->first_displayed_line + view->nlines - 1 >
3669 s->nlines) {
3670 s->first_displayed_line = 1;
3671 s->last_displayed_line = view->nlines;
3674 break;
3675 case ']':
3676 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3677 s->diff_context++;
3678 diff_view_indicate_progress(view);
3679 err = create_diff(s);
3681 break;
3682 case '<':
3683 case ',':
3684 if (s->log_view == NULL)
3685 break;
3686 ls = &s->log_view->state.log;
3687 entry = TAILQ_PREV(ls->selected_entry,
3688 commit_queue_head, entry);
3689 if (entry == NULL)
3690 break;
3692 err = input_log_view(NULL, NULL, NULL, s->log_view,
3693 KEY_UP);
3694 if (err)
3695 break;
3697 err = set_selected_commit(s, entry);
3698 if (err)
3699 break;
3701 s->first_displayed_line = 1;
3702 s->last_displayed_line = view->nlines;
3704 diff_view_indicate_progress(view);
3705 err = create_diff(s);
3706 break;
3707 case '>':
3708 case '.':
3709 if (s->log_view == NULL)
3710 break;
3711 ls = &s->log_view->state.log;
3713 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3714 ls->thread_args.commits_needed++;
3715 err = trigger_log_thread(s->log_view, 1);
3716 if (err)
3717 break;
3719 err = input_log_view(NULL, NULL, NULL, s->log_view,
3720 KEY_DOWN);
3721 if (err)
3722 break;
3724 entry = TAILQ_NEXT(ls->selected_entry, entry);
3725 if (entry == NULL)
3726 break;
3728 err = set_selected_commit(s, entry);
3729 if (err)
3730 break;
3732 s->first_displayed_line = 1;
3733 s->last_displayed_line = view->nlines;
3735 diff_view_indicate_progress(view);
3736 err = create_diff(s);
3737 break;
3738 default:
3739 break;
3742 return err;
3745 static const struct got_error *
3746 cmd_diff(int argc, char *argv[])
3748 const struct got_error *error = NULL;
3749 struct got_repository *repo = NULL;
3750 struct got_worktree *worktree = NULL;
3751 struct got_object_id *id1 = NULL, *id2 = NULL;
3752 char *repo_path = NULL, *cwd = NULL;
3753 char *id_str1 = NULL, *id_str2 = NULL;
3754 char *label1 = NULL, *label2 = NULL;
3755 int diff_context = 3, ignore_whitespace = 0;
3756 int ch, force_text_diff = 0;
3757 const char *errstr;
3758 struct tog_view *view;
3760 #ifndef PROFILE
3761 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3762 NULL) == -1)
3763 err(1, "pledge");
3764 #endif
3765 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3766 switch (ch) {
3767 case 'a':
3768 force_text_diff = 1;
3769 break;
3770 case 'C':
3771 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3772 &errstr);
3773 if (errstr != NULL)
3774 err(1, "-C option %s", errstr);
3775 break;
3776 case 'r':
3777 repo_path = realpath(optarg, NULL);
3778 if (repo_path == NULL)
3779 return got_error_from_errno2("realpath",
3780 optarg);
3781 got_path_strip_trailing_slashes(repo_path);
3782 break;
3783 case 'w':
3784 ignore_whitespace = 1;
3785 break;
3786 default:
3787 usage_diff();
3788 /* NOTREACHED */
3792 argc -= optind;
3793 argv += optind;
3795 if (argc == 0) {
3796 usage_diff(); /* TODO show local worktree changes */
3797 } else if (argc == 2) {
3798 id_str1 = argv[0];
3799 id_str2 = argv[1];
3800 } else
3801 usage_diff();
3803 cwd = getcwd(NULL, 0);
3804 if (cwd == NULL)
3805 return got_error_from_errno("getcwd");
3807 error = got_worktree_open(&worktree, cwd);
3808 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3809 goto done;
3811 if (repo_path == NULL) {
3812 if (worktree)
3813 repo_path =
3814 strdup(got_worktree_get_repo_path(worktree));
3815 else
3816 repo_path = strdup(cwd);
3818 if (repo_path == NULL) {
3819 error = got_error_from_errno("strdup");
3820 goto done;
3823 error = got_repo_open(&repo, repo_path, NULL);
3824 if (error)
3825 goto done;
3827 init_curses();
3829 error = apply_unveil(got_repo_get_path(repo), NULL);
3830 if (error)
3831 goto done;
3833 error = got_repo_match_object_id(&id1, &label1, id_str1,
3834 GOT_OBJ_TYPE_ANY, 1, repo);
3835 if (error)
3836 goto done;
3838 error = got_repo_match_object_id(&id2, &label2, id_str2,
3839 GOT_OBJ_TYPE_ANY, 1, repo);
3840 if (error)
3841 goto done;
3843 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3844 if (view == NULL) {
3845 error = got_error_from_errno("view_open");
3846 goto done;
3848 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3849 ignore_whitespace, force_text_diff, NULL, repo);
3850 if (error)
3851 goto done;
3852 error = view_loop(view);
3853 done:
3854 free(label1);
3855 free(label2);
3856 free(repo_path);
3857 free(cwd);
3858 if (repo)
3859 got_repo_close(repo);
3860 if (worktree)
3861 got_worktree_close(worktree);
3862 return error;
3865 __dead static void
3866 usage_blame(void)
3868 endwin();
3869 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3870 getprogname());
3871 exit(1);
3874 struct tog_blame_line {
3875 int annotated;
3876 struct got_object_id *id;
3879 static const struct got_error *
3880 draw_blame(struct tog_view *view)
3882 struct tog_blame_view_state *s = &view->state.blame;
3883 struct tog_blame *blame = &s->blame;
3884 regmatch_t *regmatch = &view->regmatch;
3885 const struct got_error *err;
3886 int lineno = 0, nprinted = 0;
3887 char *line;
3888 size_t len;
3889 wchar_t *wline;
3890 int width;
3891 struct tog_blame_line *blame_line;
3892 struct got_object_id *prev_id = NULL;
3893 char *id_str;
3894 struct tog_color *tc;
3896 err = got_object_id_str(&id_str, s->blamed_commit->id);
3897 if (err)
3898 return err;
3900 rewind(blame->f);
3901 werase(view->window);
3903 if (asprintf(&line, "commit %s", id_str) == -1) {
3904 err = got_error_from_errno("asprintf");
3905 free(id_str);
3906 return err;
3909 err = format_line(&wline, &width, line, view->ncols, 0);
3910 free(line);
3911 line = NULL;
3912 if (err)
3913 return err;
3914 if (view_needs_focus_indication(view))
3915 wstandout(view->window);
3916 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3917 if (tc)
3918 wattr_on(view->window,
3919 COLOR_PAIR(tc->colorpair), NULL);
3920 waddwstr(view->window, wline);
3921 if (tc)
3922 wattr_off(view->window,
3923 COLOR_PAIR(tc->colorpair), NULL);
3924 if (view_needs_focus_indication(view))
3925 wstandend(view->window);
3926 free(wline);
3927 wline = NULL;
3928 if (width < view->ncols - 1)
3929 waddch(view->window, '\n');
3931 if (asprintf(&line, "[%d/%d] %s%s",
3932 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3933 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3934 free(id_str);
3935 return got_error_from_errno("asprintf");
3937 free(id_str);
3938 err = format_line(&wline, &width, line, view->ncols, 0);
3939 free(line);
3940 line = NULL;
3941 if (err)
3942 return err;
3943 waddwstr(view->window, wline);
3944 free(wline);
3945 wline = NULL;
3946 if (width < view->ncols - 1)
3947 waddch(view->window, '\n');
3949 s->eof = 0;
3950 while (nprinted < view->nlines - 2) {
3951 line = parse_next_line(blame->f, &len);
3952 if (line == NULL) {
3953 s->eof = 1;
3954 break;
3956 if (++lineno < s->first_displayed_line) {
3957 free(line);
3958 continue;
3961 if (view->focussed && nprinted == s->selected_line - 1)
3962 wstandout(view->window);
3964 if (blame->nlines > 0) {
3965 blame_line = &blame->lines[lineno - 1];
3966 if (blame_line->annotated && prev_id &&
3967 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3968 !(view->focussed &&
3969 nprinted == s->selected_line - 1)) {
3970 waddstr(view->window, " ");
3971 } else if (blame_line->annotated) {
3972 char *id_str;
3973 err = got_object_id_str(&id_str, blame_line->id);
3974 if (err) {
3975 free(line);
3976 return err;
3978 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3979 if (tc)
3980 wattr_on(view->window,
3981 COLOR_PAIR(tc->colorpair), NULL);
3982 wprintw(view->window, "%.8s", id_str);
3983 if (tc)
3984 wattr_off(view->window,
3985 COLOR_PAIR(tc->colorpair), NULL);
3986 free(id_str);
3987 prev_id = blame_line->id;
3988 } else {
3989 waddstr(view->window, "........");
3990 prev_id = NULL;
3992 } else {
3993 waddstr(view->window, "........");
3994 prev_id = NULL;
3997 if (view->focussed && nprinted == s->selected_line - 1)
3998 wstandend(view->window);
3999 waddstr(view->window, " ");
4001 if (view->ncols <= 9) {
4002 width = 9;
4003 wline = wcsdup(L"");
4004 if (wline == NULL) {
4005 err = got_error_from_errno("wcsdup");
4006 free(line);
4007 return err;
4009 } else if (s->first_displayed_line + nprinted ==
4010 s->matched_line &&
4011 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4012 err = add_matched_line(&width, line, view->ncols - 9, 9,
4013 view->window, regmatch);
4014 if (err) {
4015 free(line);
4016 return err;
4018 width += 9;
4019 } else {
4020 err = format_line(&wline, &width, line,
4021 view->ncols - 9, 9);
4022 waddwstr(view->window, wline);
4023 free(wline);
4024 wline = NULL;
4025 width += 9;
4028 if (width <= view->ncols - 1)
4029 waddch(view->window, '\n');
4030 if (++nprinted == 1)
4031 s->first_displayed_line = lineno;
4032 free(line);
4034 s->last_displayed_line = lineno;
4036 view_vborder(view);
4038 return NULL;
4041 static const struct got_error *
4042 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4044 const struct got_error *err = NULL;
4045 struct tog_blame_cb_args *a = arg;
4046 struct tog_blame_line *line;
4047 int errcode;
4049 if (nlines != a->nlines ||
4050 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4051 return got_error(GOT_ERR_RANGE);
4053 errcode = pthread_mutex_lock(&tog_mutex);
4054 if (errcode)
4055 return got_error_set_errno(errcode, "pthread_mutex_lock");
4057 if (*a->quit) { /* user has quit the blame view */
4058 err = got_error(GOT_ERR_ITER_COMPLETED);
4059 goto done;
4062 if (lineno == -1)
4063 goto done; /* no change in this commit */
4065 line = &a->lines[lineno - 1];
4066 if (line->annotated)
4067 goto done;
4069 line->id = got_object_id_dup(id);
4070 if (line->id == NULL) {
4071 err = got_error_from_errno("got_object_id_dup");
4072 goto done;
4074 line->annotated = 1;
4075 done:
4076 errcode = pthread_mutex_unlock(&tog_mutex);
4077 if (errcode)
4078 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4079 return err;
4082 static void *
4083 blame_thread(void *arg)
4085 const struct got_error *err;
4086 struct tog_blame_thread_args *ta = arg;
4087 struct tog_blame_cb_args *a = ta->cb_args;
4088 int errcode;
4090 err = block_signals_used_by_main_thread();
4091 if (err)
4092 return (void *)err;
4094 err = got_blame(ta->path, a->commit_id, ta->repo,
4095 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4096 if (err && err->code == GOT_ERR_CANCELLED)
4097 err = NULL;
4099 errcode = pthread_mutex_lock(&tog_mutex);
4100 if (errcode)
4101 return (void *)got_error_set_errno(errcode,
4102 "pthread_mutex_lock");
4104 got_repo_close(ta->repo);
4105 ta->repo = NULL;
4106 *ta->complete = 1;
4108 errcode = pthread_mutex_unlock(&tog_mutex);
4109 if (errcode && err == NULL)
4110 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4112 return (void *)err;
4115 static struct got_object_id *
4116 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4117 int first_displayed_line, int selected_line)
4119 struct tog_blame_line *line;
4121 if (nlines <= 0)
4122 return NULL;
4124 line = &lines[first_displayed_line - 1 + selected_line - 1];
4125 if (!line->annotated)
4126 return NULL;
4128 return line->id;
4131 static const struct got_error *
4132 stop_blame(struct tog_blame *blame)
4134 const struct got_error *err = NULL;
4135 int i;
4137 if (blame->thread) {
4138 int errcode;
4139 errcode = pthread_mutex_unlock(&tog_mutex);
4140 if (errcode)
4141 return got_error_set_errno(errcode,
4142 "pthread_mutex_unlock");
4143 errcode = pthread_join(blame->thread, (void **)&err);
4144 if (errcode)
4145 return got_error_set_errno(errcode, "pthread_join");
4146 errcode = pthread_mutex_lock(&tog_mutex);
4147 if (errcode)
4148 return got_error_set_errno(errcode,
4149 "pthread_mutex_lock");
4150 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4151 err = NULL;
4152 blame->thread = NULL;
4154 if (blame->thread_args.repo) {
4155 got_repo_close(blame->thread_args.repo);
4156 blame->thread_args.repo = NULL;
4158 if (blame->f) {
4159 if (fclose(blame->f) != 0 && err == NULL)
4160 err = got_error_from_errno("fclose");
4161 blame->f = NULL;
4163 if (blame->lines) {
4164 for (i = 0; i < blame->nlines; i++)
4165 free(blame->lines[i].id);
4166 free(blame->lines);
4167 blame->lines = NULL;
4169 free(blame->cb_args.commit_id);
4170 blame->cb_args.commit_id = NULL;
4172 return err;
4175 static const struct got_error *
4176 cancel_blame_view(void *arg)
4178 const struct got_error *err = NULL;
4179 int *done = arg;
4180 int errcode;
4182 errcode = pthread_mutex_lock(&tog_mutex);
4183 if (errcode)
4184 return got_error_set_errno(errcode,
4185 "pthread_mutex_unlock");
4187 if (*done)
4188 err = got_error(GOT_ERR_CANCELLED);
4190 errcode = pthread_mutex_unlock(&tog_mutex);
4191 if (errcode)
4192 return got_error_set_errno(errcode,
4193 "pthread_mutex_lock");
4195 return err;
4198 static const struct got_error *
4199 run_blame(struct tog_view *view)
4201 struct tog_blame_view_state *s = &view->state.blame;
4202 struct tog_blame *blame = &s->blame;
4203 const struct got_error *err = NULL;
4204 struct got_blob_object *blob = NULL;
4205 struct got_repository *thread_repo = NULL;
4206 struct got_object_id *obj_id = NULL;
4207 int obj_type;
4209 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4210 s->path);
4211 if (err)
4212 return err;
4214 err = got_object_get_type(&obj_type, s->repo, obj_id);
4215 if (err)
4216 goto done;
4218 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4219 err = got_error(GOT_ERR_OBJ_TYPE);
4220 goto done;
4223 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4224 if (err)
4225 goto done;
4226 blame->f = got_opentemp();
4227 if (blame->f == NULL) {
4228 err = got_error_from_errno("got_opentemp");
4229 goto done;
4231 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4232 &blame->line_offsets, blame->f, blob);
4233 if (err || blame->nlines == 0)
4234 goto done;
4236 /* Don't include \n at EOF in the blame line count. */
4237 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4238 blame->nlines--;
4240 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4241 if (blame->lines == NULL) {
4242 err = got_error_from_errno("calloc");
4243 goto done;
4246 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4247 if (err)
4248 goto done;
4250 blame->cb_args.view = view;
4251 blame->cb_args.lines = blame->lines;
4252 blame->cb_args.nlines = blame->nlines;
4253 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4254 if (blame->cb_args.commit_id == NULL) {
4255 err = got_error_from_errno("got_object_id_dup");
4256 goto done;
4258 blame->cb_args.quit = &s->done;
4260 blame->thread_args.path = s->path;
4261 blame->thread_args.repo = thread_repo;
4262 blame->thread_args.cb_args = &blame->cb_args;
4263 blame->thread_args.complete = &s->blame_complete;
4264 blame->thread_args.cancel_cb = cancel_blame_view;
4265 blame->thread_args.cancel_arg = &s->done;
4266 s->blame_complete = 0;
4268 done:
4269 if (blob)
4270 got_object_blob_close(blob);
4271 free(obj_id);
4272 if (err)
4273 stop_blame(blame);
4274 return err;
4277 static const struct got_error *
4278 open_blame_view(struct tog_view *view, char *path,
4279 struct got_object_id *commit_id, struct got_repository *repo)
4281 const struct got_error *err = NULL;
4282 struct tog_blame_view_state *s = &view->state.blame;
4284 SIMPLEQ_INIT(&s->blamed_commits);
4286 s->path = strdup(path);
4287 if (s->path == NULL)
4288 return got_error_from_errno("strdup");
4290 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4291 if (err) {
4292 free(s->path);
4293 return err;
4296 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4297 s->first_displayed_line = 1;
4298 s->last_displayed_line = view->nlines;
4299 s->selected_line = 1;
4300 s->blame_complete = 0;
4301 s->repo = repo;
4302 s->commit_id = commit_id;
4303 memset(&s->blame, 0, sizeof(s->blame));
4305 SIMPLEQ_INIT(&s->colors);
4306 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4307 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4308 get_color_value("TOG_COLOR_COMMIT"));
4309 if (err)
4310 return err;
4313 view->show = show_blame_view;
4314 view->input = input_blame_view;
4315 view->close = close_blame_view;
4316 view->search_start = search_start_blame_view;
4317 view->search_next = search_next_blame_view;
4319 return run_blame(view);
4322 static const struct got_error *
4323 close_blame_view(struct tog_view *view)
4325 const struct got_error *err = NULL;
4326 struct tog_blame_view_state *s = &view->state.blame;
4328 if (s->blame.thread)
4329 err = stop_blame(&s->blame);
4331 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4332 struct got_object_qid *blamed_commit;
4333 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4334 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4335 got_object_qid_free(blamed_commit);
4338 free(s->path);
4339 free_colors(&s->colors);
4341 return err;
4344 static const struct got_error *
4345 search_start_blame_view(struct tog_view *view)
4347 struct tog_blame_view_state *s = &view->state.blame;
4349 s->matched_line = 0;
4350 return NULL;
4353 static const struct got_error *
4354 search_next_blame_view(struct tog_view *view)
4356 struct tog_blame_view_state *s = &view->state.blame;
4357 int lineno;
4359 if (!view->searching) {
4360 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4361 return NULL;
4364 if (s->matched_line) {
4365 if (view->searching == TOG_SEARCH_FORWARD)
4366 lineno = s->matched_line + 1;
4367 else
4368 lineno = s->matched_line - 1;
4369 } else {
4370 if (view->searching == TOG_SEARCH_FORWARD)
4371 lineno = 1;
4372 else
4373 lineno = s->blame.nlines;
4376 while (1) {
4377 char *line = NULL;
4378 off_t offset;
4379 size_t len;
4381 if (lineno <= 0 || lineno > s->blame.nlines) {
4382 if (s->matched_line == 0) {
4383 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4384 free(line);
4385 break;
4388 if (view->searching == TOG_SEARCH_FORWARD)
4389 lineno = 1;
4390 else
4391 lineno = s->blame.nlines;
4394 offset = s->blame.line_offsets[lineno - 1];
4395 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4396 free(line);
4397 return got_error_from_errno("fseeko");
4399 free(line);
4400 line = parse_next_line(s->blame.f, &len);
4401 if (line &&
4402 match_line(line, &view->regex, 1, &view->regmatch)) {
4403 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4404 s->matched_line = lineno;
4405 free(line);
4406 break;
4408 free(line);
4409 if (view->searching == TOG_SEARCH_FORWARD)
4410 lineno++;
4411 else
4412 lineno--;
4415 if (s->matched_line) {
4416 s->first_displayed_line = s->matched_line;
4417 s->selected_line = 1;
4420 return NULL;
4423 static const struct got_error *
4424 show_blame_view(struct tog_view *view)
4426 const struct got_error *err = NULL;
4427 struct tog_blame_view_state *s = &view->state.blame;
4428 int errcode;
4430 if (s->blame.thread == NULL) {
4431 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4432 &s->blame.thread_args);
4433 if (errcode)
4434 return got_error_set_errno(errcode, "pthread_create");
4436 halfdelay(1); /* fast refresh while annotating */
4439 if (s->blame_complete)
4440 halfdelay(10); /* disable fast refresh */
4442 err = draw_blame(view);
4444 view_vborder(view);
4445 return err;
4448 static const struct got_error *
4449 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4450 struct tog_view **focus_view, struct tog_view *view, int ch)
4452 const struct got_error *err = NULL, *thread_err = NULL;
4453 struct tog_view *diff_view;
4454 struct tog_blame_view_state *s = &view->state.blame;
4455 int begin_x = 0;
4457 switch (ch) {
4458 case 'q':
4459 s->done = 1;
4460 break;
4461 case 'k':
4462 case KEY_UP:
4463 if (s->selected_line > 1)
4464 s->selected_line--;
4465 else if (s->selected_line == 1 &&
4466 s->first_displayed_line > 1)
4467 s->first_displayed_line--;
4468 break;
4469 case KEY_PPAGE:
4470 case CTRL('b'):
4471 if (s->first_displayed_line == 1) {
4472 s->selected_line = 1;
4473 break;
4475 if (s->first_displayed_line > view->nlines - 2)
4476 s->first_displayed_line -=
4477 (view->nlines - 2);
4478 else
4479 s->first_displayed_line = 1;
4480 break;
4481 case 'j':
4482 case KEY_DOWN:
4483 if (s->selected_line < view->nlines - 2 &&
4484 s->first_displayed_line +
4485 s->selected_line <= s->blame.nlines)
4486 s->selected_line++;
4487 else if (s->last_displayed_line <
4488 s->blame.nlines)
4489 s->first_displayed_line++;
4490 break;
4491 case 'b':
4492 case 'p': {
4493 struct got_object_id *id = NULL;
4494 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4495 s->first_displayed_line, s->selected_line);
4496 if (id == NULL)
4497 break;
4498 if (ch == 'p') {
4499 struct got_commit_object *commit;
4500 struct got_object_qid *pid;
4501 struct got_object_id *blob_id = NULL;
4502 int obj_type;
4503 err = got_object_open_as_commit(&commit,
4504 s->repo, id);
4505 if (err)
4506 break;
4507 pid = SIMPLEQ_FIRST(
4508 got_object_commit_get_parent_ids(commit));
4509 if (pid == NULL) {
4510 got_object_commit_close(commit);
4511 break;
4513 /* Check if path history ends here. */
4514 err = got_object_id_by_path(&blob_id, s->repo,
4515 pid->id, s->path);
4516 if (err) {
4517 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4518 err = NULL;
4519 got_object_commit_close(commit);
4520 break;
4522 err = got_object_get_type(&obj_type, s->repo,
4523 blob_id);
4524 free(blob_id);
4525 /* Can't blame non-blob type objects. */
4526 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4527 got_object_commit_close(commit);
4528 break;
4530 err = got_object_qid_alloc(&s->blamed_commit,
4531 pid->id);
4532 got_object_commit_close(commit);
4533 } else {
4534 if (got_object_id_cmp(id,
4535 s->blamed_commit->id) == 0)
4536 break;
4537 err = got_object_qid_alloc(&s->blamed_commit,
4538 id);
4540 if (err)
4541 break;
4542 s->done = 1;
4543 thread_err = stop_blame(&s->blame);
4544 s->done = 0;
4545 if (thread_err)
4546 break;
4547 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4548 s->blamed_commit, entry);
4549 err = run_blame(view);
4550 if (err)
4551 break;
4552 break;
4554 case 'B': {
4555 struct got_object_qid *first;
4556 first = SIMPLEQ_FIRST(&s->blamed_commits);
4557 if (!got_object_id_cmp(first->id, s->commit_id))
4558 break;
4559 s->done = 1;
4560 thread_err = stop_blame(&s->blame);
4561 s->done = 0;
4562 if (thread_err)
4563 break;
4564 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4565 got_object_qid_free(s->blamed_commit);
4566 s->blamed_commit =
4567 SIMPLEQ_FIRST(&s->blamed_commits);
4568 err = run_blame(view);
4569 if (err)
4570 break;
4571 break;
4573 case KEY_ENTER:
4574 case '\r': {
4575 struct got_object_id *id = NULL;
4576 struct got_object_qid *pid;
4577 struct got_commit_object *commit = NULL;
4578 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4579 s->first_displayed_line, s->selected_line);
4580 if (id == NULL)
4581 break;
4582 err = got_object_open_as_commit(&commit, s->repo, id);
4583 if (err)
4584 break;
4585 pid = SIMPLEQ_FIRST(
4586 got_object_commit_get_parent_ids(commit));
4587 if (view_is_parent_view(view))
4588 begin_x = view_split_begin_x(view->begin_x);
4589 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4590 if (diff_view == NULL) {
4591 got_object_commit_close(commit);
4592 err = got_error_from_errno("view_open");
4593 break;
4595 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4596 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4597 got_object_commit_close(commit);
4598 if (err) {
4599 view_close(diff_view);
4600 break;
4602 if (view_is_parent_view(view)) {
4603 err = view_close_child(view);
4604 if (err)
4605 break;
4606 err = view_set_child(view, diff_view);
4607 if (err) {
4608 view_close(diff_view);
4609 break;
4611 *focus_view = diff_view;
4612 view->child_focussed = 1;
4613 } else
4614 *new_view = diff_view;
4615 if (err)
4616 break;
4617 break;
4619 case KEY_NPAGE:
4620 case CTRL('f'):
4621 case ' ':
4622 if (s->last_displayed_line >= s->blame.nlines &&
4623 s->selected_line >= MIN(s->blame.nlines,
4624 view->nlines - 2)) {
4625 break;
4627 if (s->last_displayed_line >= s->blame.nlines &&
4628 s->selected_line < view->nlines - 2) {
4629 s->selected_line = MIN(s->blame.nlines,
4630 view->nlines - 2);
4631 break;
4633 if (s->last_displayed_line + view->nlines - 2
4634 <= s->blame.nlines)
4635 s->first_displayed_line +=
4636 view->nlines - 2;
4637 else
4638 s->first_displayed_line =
4639 s->blame.nlines -
4640 (view->nlines - 3);
4641 break;
4642 case KEY_RESIZE:
4643 if (s->selected_line > view->nlines - 2) {
4644 s->selected_line = MIN(s->blame.nlines,
4645 view->nlines - 2);
4647 break;
4648 default:
4649 break;
4651 return thread_err ? thread_err : err;
4654 static const struct got_error *
4655 cmd_blame(int argc, char *argv[])
4657 const struct got_error *error;
4658 struct got_repository *repo = NULL;
4659 struct got_worktree *worktree = NULL;
4660 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4661 char *link_target = NULL;
4662 struct got_object_id *commit_id = NULL;
4663 char *commit_id_str = NULL;
4664 int ch;
4665 struct tog_view *view;
4667 #ifndef PROFILE
4668 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4669 NULL) == -1)
4670 err(1, "pledge");
4671 #endif
4673 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4674 switch (ch) {
4675 case 'c':
4676 commit_id_str = optarg;
4677 break;
4678 case 'r':
4679 repo_path = realpath(optarg, NULL);
4680 if (repo_path == NULL)
4681 return got_error_from_errno2("realpath",
4682 optarg);
4683 break;
4684 default:
4685 usage_blame();
4686 /* NOTREACHED */
4690 argc -= optind;
4691 argv += optind;
4693 if (argc != 1)
4694 usage_blame();
4696 cwd = getcwd(NULL, 0);
4697 if (cwd == NULL)
4698 return got_error_from_errno("getcwd");
4700 error = got_worktree_open(&worktree, cwd);
4701 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4702 goto done;
4704 if (repo_path == NULL) {
4705 if (worktree)
4706 repo_path =
4707 strdup(got_worktree_get_repo_path(worktree));
4708 else
4709 repo_path = strdup(cwd);
4711 if (repo_path == NULL) {
4712 error = got_error_from_errno("strdup");
4713 goto done;
4716 error = got_repo_open(&repo, repo_path, NULL);
4717 if (error != NULL)
4718 goto done;
4720 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4721 worktree);
4722 if (error)
4723 goto done;
4725 init_curses();
4727 error = apply_unveil(got_repo_get_path(repo), NULL);
4728 if (error)
4729 goto done;
4731 if (commit_id_str == NULL) {
4732 struct got_reference *head_ref;
4733 error = got_ref_open(&head_ref, repo, worktree ?
4734 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4735 if (error != NULL)
4736 goto done;
4737 error = got_ref_resolve(&commit_id, repo, head_ref);
4738 got_ref_close(head_ref);
4739 } else {
4740 error = got_repo_match_object_id(&commit_id, NULL,
4741 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4743 if (error != NULL)
4744 goto done;
4746 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4747 if (view == NULL) {
4748 error = got_error_from_errno("view_open");
4749 goto done;
4752 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4753 commit_id, repo);
4754 if (error)
4755 goto done;
4757 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4758 commit_id, repo);
4759 if (error)
4760 goto done;
4761 if (worktree) {
4762 /* Release work tree lock. */
4763 got_worktree_close(worktree);
4764 worktree = NULL;
4766 error = view_loop(view);
4767 done:
4768 free(repo_path);
4769 free(in_repo_path);
4770 free(link_target);
4771 free(cwd);
4772 free(commit_id);
4773 if (worktree)
4774 got_worktree_close(worktree);
4775 if (repo)
4776 got_repo_close(repo);
4777 return error;
4780 static const struct got_error *
4781 draw_tree_entries(struct tog_view *view, const char *parent_path)
4783 struct tog_tree_view_state *s = &view->state.tree;
4784 const struct got_error *err = NULL;
4785 struct got_tree_entry *te;
4786 wchar_t *wline;
4787 struct tog_color *tc;
4788 int width, n, i, nentries;
4789 int limit = view->nlines;
4791 s->ndisplayed = 0;
4793 werase(view->window);
4795 if (limit == 0)
4796 return NULL;
4798 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4799 if (err)
4800 return err;
4801 if (view_needs_focus_indication(view))
4802 wstandout(view->window);
4803 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4804 if (tc)
4805 wattr_on(view->window,
4806 COLOR_PAIR(tc->colorpair), NULL);
4807 waddwstr(view->window, wline);
4808 if (tc)
4809 wattr_off(view->window,
4810 COLOR_PAIR(tc->colorpair), NULL);
4811 if (view_needs_focus_indication(view))
4812 wstandend(view->window);
4813 free(wline);
4814 wline = NULL;
4815 if (width < view->ncols - 1)
4816 waddch(view->window, '\n');
4817 if (--limit <= 0)
4818 return NULL;
4819 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4820 if (err)
4821 return err;
4822 waddwstr(view->window, wline);
4823 free(wline);
4824 wline = NULL;
4825 if (width < view->ncols - 1)
4826 waddch(view->window, '\n');
4827 if (--limit <= 0)
4828 return NULL;
4829 waddch(view->window, '\n');
4830 if (--limit <= 0)
4831 return NULL;
4833 if (s->first_displayed_entry == NULL) {
4834 te = got_object_tree_get_first_entry(s->tree);
4835 if (s->selected == 0) {
4836 if (view->focussed)
4837 wstandout(view->window);
4838 s->selected_entry = NULL;
4840 waddstr(view->window, " ..\n"); /* parent directory */
4841 if (s->selected == 0 && view->focussed)
4842 wstandend(view->window);
4843 s->ndisplayed++;
4844 if (--limit <= 0)
4845 return NULL;
4846 n = 1;
4847 } else {
4848 n = 0;
4849 te = s->first_displayed_entry;
4852 nentries = got_object_tree_get_nentries(s->tree);
4853 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4854 char *line = NULL, *id_str = NULL, *link_target = NULL;
4855 const char *modestr = "";
4856 mode_t mode;
4858 te = got_object_tree_get_entry(s->tree, i);
4859 mode = got_tree_entry_get_mode(te);
4861 if (s->show_ids) {
4862 err = got_object_id_str(&id_str,
4863 got_tree_entry_get_id(te));
4864 if (err)
4865 return got_error_from_errno(
4866 "got_object_id_str");
4868 if (got_object_tree_entry_is_submodule(te))
4869 modestr = "$";
4870 else if (S_ISLNK(mode)) {
4871 int i;
4873 err = got_tree_entry_get_symlink_target(&link_target,
4874 te, s->repo);
4875 if (err) {
4876 free(id_str);
4877 return err;
4879 for (i = 0; i < strlen(link_target); i++) {
4880 if (!isprint((unsigned char)link_target[i]))
4881 link_target[i] = '?';
4883 modestr = "@";
4885 else if (S_ISDIR(mode))
4886 modestr = "/";
4887 else if (mode & S_IXUSR)
4888 modestr = "*";
4889 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4890 got_tree_entry_get_name(te), modestr,
4891 link_target ? " -> ": "",
4892 link_target ? link_target : "") == -1) {
4893 free(id_str);
4894 free(link_target);
4895 return got_error_from_errno("asprintf");
4897 free(id_str);
4898 free(link_target);
4899 err = format_line(&wline, &width, line, view->ncols, 0);
4900 if (err) {
4901 free(line);
4902 break;
4904 if (n == s->selected) {
4905 if (view->focussed)
4906 wstandout(view->window);
4907 s->selected_entry = te;
4909 tc = match_color(&s->colors, line);
4910 if (tc)
4911 wattr_on(view->window,
4912 COLOR_PAIR(tc->colorpair), NULL);
4913 waddwstr(view->window, wline);
4914 if (tc)
4915 wattr_off(view->window,
4916 COLOR_PAIR(tc->colorpair), NULL);
4917 if (width < view->ncols - 1)
4918 waddch(view->window, '\n');
4919 if (n == s->selected && view->focussed)
4920 wstandend(view->window);
4921 free(line);
4922 free(wline);
4923 wline = NULL;
4924 n++;
4925 s->ndisplayed++;
4926 s->last_displayed_entry = te;
4927 if (--limit <= 0)
4928 break;
4931 return err;
4934 static void
4935 tree_scroll_up(struct tog_view *view, int maxscroll)
4937 struct tog_tree_view_state *s = &view->state.tree;
4938 struct got_tree_entry *te;
4939 int isroot = s->tree == s->root;
4940 int i = 0;
4942 if (s->first_displayed_entry == NULL)
4943 return;
4945 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4946 while (i++ < maxscroll) {
4947 if (te == NULL) {
4948 if (!isroot)
4949 s->first_displayed_entry = NULL;
4950 break;
4952 s->first_displayed_entry = te;
4953 te = got_tree_entry_get_prev(s->tree, te);
4957 static void
4958 tree_scroll_down(struct tog_view *view, int maxscroll)
4960 struct tog_tree_view_state *s = &view->state.tree;
4961 struct got_tree_entry *next, *last;
4962 int n = 0;
4964 if (s->first_displayed_entry)
4965 next = got_tree_entry_get_next(s->tree,
4966 s->first_displayed_entry);
4967 else
4968 next = got_object_tree_get_first_entry(s->tree);
4970 last = s->last_displayed_entry;
4971 while (next && last && n++ < maxscroll) {
4972 last = got_tree_entry_get_next(s->tree, last);
4973 if (last) {
4974 s->first_displayed_entry = next;
4975 next = got_tree_entry_get_next(s->tree, next);
4980 static const struct got_error *
4981 tree_entry_path(char **path, struct tog_parent_trees *parents,
4982 struct got_tree_entry *te)
4984 const struct got_error *err = NULL;
4985 struct tog_parent_tree *pt;
4986 size_t len = 2; /* for leading slash and NUL */
4988 TAILQ_FOREACH(pt, parents, entry)
4989 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4990 + 1 /* slash */;
4991 if (te)
4992 len += strlen(got_tree_entry_get_name(te));
4994 *path = calloc(1, len);
4995 if (path == NULL)
4996 return got_error_from_errno("calloc");
4998 (*path)[0] = '/';
4999 pt = TAILQ_LAST(parents, tog_parent_trees);
5000 while (pt) {
5001 const char *name = got_tree_entry_get_name(pt->selected_entry);
5002 if (strlcat(*path, name, len) >= len) {
5003 err = got_error(GOT_ERR_NO_SPACE);
5004 goto done;
5006 if (strlcat(*path, "/", len) >= len) {
5007 err = got_error(GOT_ERR_NO_SPACE);
5008 goto done;
5010 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5012 if (te) {
5013 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5014 err = got_error(GOT_ERR_NO_SPACE);
5015 goto done;
5018 done:
5019 if (err) {
5020 free(*path);
5021 *path = NULL;
5023 return err;
5026 static const struct got_error *
5027 blame_tree_entry(struct tog_view **new_view, int begin_x,
5028 struct got_tree_entry *te, struct tog_parent_trees *parents,
5029 struct got_object_id *commit_id, struct got_repository *repo)
5031 const struct got_error *err = NULL;
5032 char *path;
5033 struct tog_view *blame_view;
5035 *new_view = NULL;
5037 err = tree_entry_path(&path, parents, te);
5038 if (err)
5039 return err;
5041 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5042 if (blame_view == NULL) {
5043 err = got_error_from_errno("view_open");
5044 goto done;
5047 err = open_blame_view(blame_view, path, commit_id, repo);
5048 if (err) {
5049 if (err->code == GOT_ERR_CANCELLED)
5050 err = NULL;
5051 view_close(blame_view);
5052 } else
5053 *new_view = blame_view;
5054 done:
5055 free(path);
5056 return err;
5059 static const struct got_error *
5060 log_tree_entry(struct tog_view **new_view, int begin_x,
5061 struct got_tree_entry *te, struct tog_parent_trees *parents,
5062 struct got_object_id *commit_id, struct got_repository *repo)
5064 struct tog_view *log_view;
5065 const struct got_error *err = NULL;
5066 char *path;
5068 *new_view = NULL;
5070 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5071 if (log_view == NULL)
5072 return got_error_from_errno("view_open");
5074 err = tree_entry_path(&path, parents, te);
5075 if (err)
5076 return err;
5078 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5079 if (err)
5080 view_close(log_view);
5081 else
5082 *new_view = log_view;
5083 free(path);
5084 return err;
5087 static const struct got_error *
5088 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5089 struct got_object_id *commit_id, struct got_repository *repo)
5091 const struct got_error *err = NULL;
5092 char *commit_id_str = NULL;
5093 struct tog_tree_view_state *s = &view->state.tree;
5095 TAILQ_INIT(&s->parents);
5097 err = got_object_id_str(&commit_id_str, commit_id);
5098 if (err != NULL)
5099 goto done;
5101 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5102 err = got_error_from_errno("asprintf");
5103 goto done;
5106 s->root = s->tree = root;
5107 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5108 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5109 s->commit_id = got_object_id_dup(commit_id);
5110 if (s->commit_id == NULL) {
5111 err = got_error_from_errno("got_object_id_dup");
5112 goto done;
5114 s->repo = repo;
5116 SIMPLEQ_INIT(&s->colors);
5118 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5119 err = add_color(&s->colors, "\\$$",
5120 TOG_COLOR_TREE_SUBMODULE,
5121 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5122 if (err)
5123 goto done;
5124 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5125 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5126 if (err) {
5127 free_colors(&s->colors);
5128 goto done;
5130 err = add_color(&s->colors, "/$",
5131 TOG_COLOR_TREE_DIRECTORY,
5132 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5133 if (err) {
5134 free_colors(&s->colors);
5135 goto done;
5138 err = add_color(&s->colors, "\\*$",
5139 TOG_COLOR_TREE_EXECUTABLE,
5140 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5141 if (err) {
5142 free_colors(&s->colors);
5143 goto done;
5146 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5147 get_color_value("TOG_COLOR_COMMIT"));
5148 if (err) {
5149 free_colors(&s->colors);
5150 goto done;
5154 view->show = show_tree_view;
5155 view->input = input_tree_view;
5156 view->close = close_tree_view;
5157 view->search_start = search_start_tree_view;
5158 view->search_next = search_next_tree_view;
5159 done:
5160 free(commit_id_str);
5161 if (err) {
5162 free(s->tree_label);
5163 s->tree_label = NULL;
5165 return err;
5168 static const struct got_error *
5169 close_tree_view(struct tog_view *view)
5171 struct tog_tree_view_state *s = &view->state.tree;
5173 free_colors(&s->colors);
5174 free(s->tree_label);
5175 s->tree_label = NULL;
5176 free(s->commit_id);
5177 s->commit_id = NULL;
5178 while (!TAILQ_EMPTY(&s->parents)) {
5179 struct tog_parent_tree *parent;
5180 parent = TAILQ_FIRST(&s->parents);
5181 TAILQ_REMOVE(&s->parents, parent, entry);
5182 free(parent);
5185 if (s->tree != s->root)
5186 got_object_tree_close(s->tree);
5187 got_object_tree_close(s->root);
5188 return NULL;
5191 static const struct got_error *
5192 search_start_tree_view(struct tog_view *view)
5194 struct tog_tree_view_state *s = &view->state.tree;
5196 s->matched_entry = NULL;
5197 return NULL;
5200 static int
5201 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5203 regmatch_t regmatch;
5205 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5206 0) == 0;
5209 static const struct got_error *
5210 search_next_tree_view(struct tog_view *view)
5212 struct tog_tree_view_state *s = &view->state.tree;
5213 struct got_tree_entry *te = NULL;
5215 if (!view->searching) {
5216 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5217 return NULL;
5220 if (s->matched_entry) {
5221 if (view->searching == TOG_SEARCH_FORWARD) {
5222 if (s->selected_entry)
5223 te = got_tree_entry_get_next(s->tree,
5224 s->selected_entry);
5225 else
5226 te = got_object_tree_get_first_entry(s->tree);
5227 } else {
5228 if (s->selected_entry == NULL)
5229 te = got_object_tree_get_last_entry(s->tree);
5230 else
5231 te = got_tree_entry_get_prev(s->tree,
5232 s->selected_entry);
5234 } else {
5235 if (view->searching == TOG_SEARCH_FORWARD)
5236 te = got_object_tree_get_first_entry(s->tree);
5237 else
5238 te = got_object_tree_get_last_entry(s->tree);
5241 while (1) {
5242 if (te == NULL) {
5243 if (s->matched_entry == NULL) {
5244 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5245 return NULL;
5247 if (view->searching == TOG_SEARCH_FORWARD)
5248 te = got_object_tree_get_first_entry(s->tree);
5249 else
5250 te = got_object_tree_get_last_entry(s->tree);
5253 if (match_tree_entry(te, &view->regex)) {
5254 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5255 s->matched_entry = te;
5256 break;
5259 if (view->searching == TOG_SEARCH_FORWARD)
5260 te = got_tree_entry_get_next(s->tree, te);
5261 else
5262 te = got_tree_entry_get_prev(s->tree, te);
5265 if (s->matched_entry) {
5266 s->first_displayed_entry = s->matched_entry;
5267 s->selected = 0;
5270 return NULL;
5273 static const struct got_error *
5274 show_tree_view(struct tog_view *view)
5276 const struct got_error *err = NULL;
5277 struct tog_tree_view_state *s = &view->state.tree;
5278 char *parent_path;
5280 err = tree_entry_path(&parent_path, &s->parents, NULL);
5281 if (err)
5282 return err;
5284 err = draw_tree_entries(view, parent_path);
5285 free(parent_path);
5287 view_vborder(view);
5288 return err;
5291 static const struct got_error *
5292 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5293 struct tog_view **focus_view, struct tog_view *view, int ch)
5295 const struct got_error *err = NULL;
5296 struct tog_tree_view_state *s = &view->state.tree;
5297 struct tog_view *log_view, *ref_view;
5298 int begin_x = 0;
5300 switch (ch) {
5301 case 'i':
5302 s->show_ids = !s->show_ids;
5303 break;
5304 case 'l':
5305 if (!s->selected_entry)
5306 break;
5307 if (view_is_parent_view(view))
5308 begin_x = view_split_begin_x(view->begin_x);
5309 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5310 &s->parents, s->commit_id, s->repo);
5311 if (view_is_parent_view(view)) {
5312 err = view_close_child(view);
5313 if (err)
5314 return err;
5315 err = view_set_child(view, log_view);
5316 if (err) {
5317 view_close(log_view);
5318 break;
5320 *focus_view = log_view;
5321 view->child_focussed = 1;
5322 } else
5323 *new_view = log_view;
5324 break;
5325 case 'r':
5326 if (view_is_parent_view(view))
5327 begin_x = view_split_begin_x(view->begin_x);
5328 ref_view = view_open(view->nlines, view->ncols,
5329 view->begin_y, begin_x, TOG_VIEW_REF);
5330 if (ref_view == NULL)
5331 return got_error_from_errno("view_open");
5332 err = open_ref_view(ref_view, s->repo);
5333 if (err) {
5334 view_close(ref_view);
5335 return err;
5337 if (view_is_parent_view(view)) {
5338 err = view_close_child(view);
5339 if (err)
5340 return err;
5341 err = view_set_child(view, ref_view);
5342 if (err) {
5343 view_close(ref_view);
5344 break;
5346 *focus_view = ref_view;
5347 view->child_focussed = 1;
5348 } else
5349 *new_view = ref_view;
5350 break;
5351 case 'k':
5352 case KEY_UP:
5353 if (s->selected > 0) {
5354 s->selected--;
5355 break;
5357 tree_scroll_up(view, 1);
5358 break;
5359 case KEY_PPAGE:
5360 case CTRL('b'):
5361 if (s->tree == s->root) {
5362 if (got_object_tree_get_first_entry(s->tree) ==
5363 s->first_displayed_entry)
5364 s->selected = 0;
5365 } else {
5366 if (s->first_displayed_entry == NULL)
5367 s->selected = 0;
5369 tree_scroll_up(view, MAX(0, view->nlines - 3));
5370 break;
5371 case 'j':
5372 case KEY_DOWN:
5373 if (s->selected < s->ndisplayed - 1) {
5374 s->selected++;
5375 break;
5377 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5378 == NULL)
5379 /* can't scroll any further */
5380 break;
5381 tree_scroll_down(view, 1);
5382 break;
5383 case KEY_NPAGE:
5384 case CTRL('f'):
5385 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5386 == NULL) {
5387 /* can't scroll any further; move cursor down */
5388 if (s->selected < s->ndisplayed - 1)
5389 s->selected = s->ndisplayed - 1;
5390 break;
5392 tree_scroll_down(view, view->nlines - 3);
5393 break;
5394 case KEY_ENTER:
5395 case '\r':
5396 case KEY_BACKSPACE:
5397 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5398 struct tog_parent_tree *parent;
5399 /* user selected '..' */
5400 if (s->tree == s->root)
5401 break;
5402 parent = TAILQ_FIRST(&s->parents);
5403 TAILQ_REMOVE(&s->parents, parent,
5404 entry);
5405 got_object_tree_close(s->tree);
5406 s->tree = parent->tree;
5407 s->first_displayed_entry =
5408 parent->first_displayed_entry;
5409 s->selected_entry =
5410 parent->selected_entry;
5411 s->selected = parent->selected;
5412 free(parent);
5413 } else if (S_ISDIR(got_tree_entry_get_mode(
5414 s->selected_entry))) {
5415 struct got_tree_object *subtree;
5416 err = got_object_open_as_tree(&subtree, s->repo,
5417 got_tree_entry_get_id(s->selected_entry));
5418 if (err)
5419 break;
5420 err = tree_view_visit_subtree(subtree, s);
5421 if (err) {
5422 got_object_tree_close(subtree);
5423 break;
5425 } else if (S_ISREG(got_tree_entry_get_mode(
5426 s->selected_entry))) {
5427 struct tog_view *blame_view;
5428 int begin_x = view_is_parent_view(view) ?
5429 view_split_begin_x(view->begin_x) : 0;
5431 err = blame_tree_entry(&blame_view, begin_x,
5432 s->selected_entry, &s->parents,
5433 s->commit_id, s->repo);
5434 if (err)
5435 break;
5436 if (view_is_parent_view(view)) {
5437 err = view_close_child(view);
5438 if (err)
5439 return err;
5440 err = view_set_child(view, blame_view);
5441 if (err) {
5442 view_close(blame_view);
5443 break;
5445 *focus_view = blame_view;
5446 view->child_focussed = 1;
5447 } else
5448 *new_view = blame_view;
5450 break;
5451 case KEY_RESIZE:
5452 if (s->selected > view->nlines)
5453 s->selected = s->ndisplayed - 1;
5454 break;
5455 default:
5456 break;
5459 return err;
5462 __dead static void
5463 usage_tree(void)
5465 endwin();
5466 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5467 getprogname());
5468 exit(1);
5471 static const struct got_error *
5472 cmd_tree(int argc, char *argv[])
5474 const struct got_error *error;
5475 struct got_repository *repo = NULL;
5476 struct got_worktree *worktree = NULL;
5477 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5478 struct got_object_id *commit_id = NULL;
5479 char *commit_id_arg = NULL;
5480 struct got_commit_object *commit = NULL;
5481 struct got_tree_object *tree = NULL;
5482 int ch;
5483 struct tog_view *view;
5485 #ifndef PROFILE
5486 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5487 NULL) == -1)
5488 err(1, "pledge");
5489 #endif
5491 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5492 switch (ch) {
5493 case 'c':
5494 commit_id_arg = optarg;
5495 break;
5496 case 'r':
5497 repo_path = realpath(optarg, NULL);
5498 if (repo_path == NULL)
5499 return got_error_from_errno2("realpath",
5500 optarg);
5501 break;
5502 default:
5503 usage_tree();
5504 /* NOTREACHED */
5508 argc -= optind;
5509 argv += optind;
5511 if (argc > 1)
5512 usage_tree();
5514 cwd = getcwd(NULL, 0);
5515 if (cwd == NULL)
5516 return got_error_from_errno("getcwd");
5518 error = got_worktree_open(&worktree, cwd);
5519 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5520 goto done;
5522 if (repo_path == NULL) {
5523 if (worktree)
5524 repo_path =
5525 strdup(got_worktree_get_repo_path(worktree));
5526 else
5527 repo_path = strdup(cwd);
5529 if (repo_path == NULL) {
5530 error = got_error_from_errno("strdup");
5531 goto done;
5534 error = got_repo_open(&repo, repo_path, NULL);
5535 if (error != NULL)
5536 goto done;
5538 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5539 repo, worktree);
5540 if (error)
5541 goto done;
5543 init_curses();
5545 error = apply_unveil(got_repo_get_path(repo), NULL);
5546 if (error)
5547 goto done;
5549 error = got_repo_match_object_id(&commit_id, NULL,
5550 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5551 GOT_OBJ_TYPE_COMMIT, 1, repo);
5552 if (error)
5553 goto done;
5555 error = got_object_open_as_commit(&commit, repo, commit_id);
5556 if (error)
5557 goto done;
5559 error = got_object_open_as_tree(&tree, repo,
5560 got_object_commit_get_tree_id(commit));
5561 if (error)
5562 goto done;
5564 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5565 if (view == NULL) {
5566 error = got_error_from_errno("view_open");
5567 goto done;
5569 error = open_tree_view(view, tree, commit_id, repo);
5570 if (error)
5571 goto done;
5572 if (!got_path_is_root_dir(in_repo_path)) {
5573 error = tree_view_walk_path(&view->state.tree, commit_id,
5574 in_repo_path, repo);
5575 if (error)
5576 goto done;
5579 if (worktree) {
5580 /* Release work tree lock. */
5581 got_worktree_close(worktree);
5582 worktree = NULL;
5584 error = view_loop(view);
5585 done:
5586 free(repo_path);
5587 free(cwd);
5588 free(commit_id);
5589 if (commit)
5590 got_object_commit_close(commit);
5591 if (tree)
5592 got_object_tree_close(tree);
5593 if (repo)
5594 got_repo_close(repo);
5595 return error;
5598 static const struct got_error *
5599 ref_view_load_refs(struct tog_ref_view_state *s)
5601 const struct got_error *err;
5602 struct got_reflist_entry *sre;
5603 struct tog_reflist_entry *re;
5605 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5606 got_ref_cmp_by_name, NULL);
5607 if (err)
5608 return err;
5610 s->nrefs = 0;
5611 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5612 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5613 continue;
5615 re = malloc(sizeof(*re));
5616 if (re == NULL)
5617 return got_error_from_errno("malloc");
5619 re->ref = sre->ref;
5620 re->idx = s->nrefs++;
5621 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5624 return NULL;
5627 void
5628 ref_view_free_refs(struct tog_ref_view_state *s)
5630 struct tog_reflist_entry *re;
5632 while (!TAILQ_EMPTY(&s->refs)) {
5633 re = TAILQ_FIRST(&s->refs);
5634 TAILQ_REMOVE(&s->refs, re, entry);
5635 free(re);
5637 got_ref_list_free(&s->simplerefs);
5640 static const struct got_error *
5641 open_ref_view(struct tog_view *view, struct got_repository *repo)
5643 const struct got_error *err = NULL;
5644 struct tog_ref_view_state *s = &view->state.ref;
5646 s->selected_entry = 0;
5647 s->repo = repo;
5649 SIMPLEQ_INIT(&s->simplerefs);
5650 TAILQ_INIT(&s->refs);
5651 SIMPLEQ_INIT(&s->colors);
5653 err = ref_view_load_refs(s);
5654 if (err)
5655 return err;
5657 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5659 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5660 err = add_color(&s->colors, "^refs/heads/",
5661 TOG_COLOR_REFS_HEADS,
5662 get_color_value("TOG_COLOR_REFS_HEADS"));
5663 if (err)
5664 goto done;
5666 err = add_color(&s->colors, "^refs/tags/",
5667 TOG_COLOR_REFS_TAGS,
5668 get_color_value("TOG_COLOR_REFS_TAGS"));
5669 if (err)
5670 goto done;
5672 err = add_color(&s->colors, "^refs/remotes/",
5673 TOG_COLOR_REFS_REMOTES,
5674 get_color_value("TOG_COLOR_REFS_REMOTES"));
5675 if (err)
5676 goto done;
5679 view->show = show_ref_view;
5680 view->input = input_ref_view;
5681 view->close = close_ref_view;
5682 view->search_start = search_start_ref_view;
5683 view->search_next = search_next_ref_view;
5684 done:
5685 if (err)
5686 free_colors(&s->colors);
5687 return err;
5690 static const struct got_error *
5691 close_ref_view(struct tog_view *view)
5693 struct tog_ref_view_state *s = &view->state.ref;
5695 ref_view_free_refs(s);
5696 free_colors(&s->colors);
5698 return NULL;
5701 static const struct got_error *
5702 resolve_reflist_entry(struct got_object_id **commit_id,
5703 struct tog_reflist_entry *re, struct got_repository *repo)
5705 const struct got_error *err = NULL;
5706 struct got_object_id *obj_id;
5707 struct got_tag_object *tag = NULL;
5708 int obj_type;
5710 *commit_id = NULL;
5712 err = got_ref_resolve(&obj_id, repo, re->ref);
5713 if (err)
5714 return err;
5716 err = got_object_get_type(&obj_type, repo, obj_id);
5717 if (err)
5718 goto done;
5720 switch (obj_type) {
5721 case GOT_OBJ_TYPE_COMMIT:
5722 *commit_id = obj_id;
5723 break;
5724 case GOT_OBJ_TYPE_TAG:
5725 err = got_object_open_as_tag(&tag, repo, obj_id);
5726 if (err)
5727 goto done;
5728 free(obj_id);
5729 err = got_object_get_type(&obj_type, repo,
5730 got_object_tag_get_object_id(tag));
5731 if (err)
5732 goto done;
5733 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5734 err = got_error(GOT_ERR_OBJ_TYPE);
5735 goto done;
5737 *commit_id = got_object_id_dup(
5738 got_object_tag_get_object_id(tag));
5739 if (*commit_id == NULL) {
5740 err = got_error_from_errno("got_object_id_dup");
5741 goto done;
5743 break;
5744 default:
5745 err = got_error(GOT_ERR_OBJ_TYPE);
5746 break;
5749 done:
5750 if (tag)
5751 got_object_tag_close(tag);
5752 if (err) {
5753 free(*commit_id);
5754 *commit_id = NULL;
5756 return err;
5759 static const struct got_error *
5760 log_ref_entry(struct tog_view **new_view, int begin_x,
5761 struct tog_reflist_entry *re, struct got_repository *repo)
5763 struct tog_view *log_view;
5764 const struct got_error *err = NULL;
5765 struct got_object_id *commit_id = NULL;
5767 *new_view = NULL;
5769 err = resolve_reflist_entry(&commit_id, re, repo);
5770 if (err) {
5771 if (err->code != GOT_ERR_OBJ_TYPE)
5772 return err;
5773 else
5774 return NULL;
5777 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5778 if (log_view == NULL) {
5779 err = got_error_from_errno("view_open");
5780 goto done;
5783 err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5784 done:
5785 if (err)
5786 view_close(log_view);
5787 else
5788 *new_view = log_view;
5789 free(commit_id);
5790 return err;
5793 static void
5794 ref_scroll_up(struct tog_view *view, int maxscroll)
5796 struct tog_ref_view_state *s = &view->state.ref;
5797 struct tog_reflist_entry *re;
5798 int i = 0;
5800 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5801 return;
5803 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5804 while (i++ < maxscroll) {
5805 if (re == NULL)
5806 break;
5807 s->first_displayed_entry = re;
5808 re = TAILQ_PREV(re, tog_reflist_head, entry);
5812 static void
5813 ref_scroll_down(struct tog_view *view, int maxscroll)
5815 struct tog_ref_view_state *s = &view->state.ref;
5816 struct tog_reflist_entry *next, *last;
5817 int n = 0;
5819 if (s->first_displayed_entry)
5820 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5821 else
5822 next = TAILQ_FIRST(&s->refs);
5824 last = s->last_displayed_entry;
5825 while (next && last && n++ < maxscroll) {
5826 last = TAILQ_NEXT(last, entry);
5827 if (last) {
5828 s->first_displayed_entry = next;
5829 next = TAILQ_NEXT(next, entry);
5834 static const struct got_error *
5835 search_start_ref_view(struct tog_view *view)
5837 struct tog_ref_view_state *s = &view->state.ref;
5839 s->matched_entry = NULL;
5840 return NULL;
5843 static int
5844 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5846 regmatch_t regmatch;
5848 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5849 0) == 0;
5852 static const struct got_error *
5853 search_next_ref_view(struct tog_view *view)
5855 struct tog_ref_view_state *s = &view->state.ref;
5856 struct tog_reflist_entry *re = NULL;
5858 if (!view->searching) {
5859 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5860 return NULL;
5863 if (s->matched_entry) {
5864 if (view->searching == TOG_SEARCH_FORWARD) {
5865 if (s->selected_entry)
5866 re = TAILQ_NEXT(s->selected_entry, entry);
5867 else
5868 re = TAILQ_PREV(s->selected_entry,
5869 tog_reflist_head, entry);
5870 } else {
5871 if (s->selected_entry == NULL)
5872 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5873 else
5874 re = TAILQ_PREV(s->selected_entry,
5875 tog_reflist_head, entry);
5877 } else {
5878 if (view->searching == TOG_SEARCH_FORWARD)
5879 re = TAILQ_FIRST(&s->refs);
5880 else
5881 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5884 while (1) {
5885 if (re == NULL) {
5886 if (s->matched_entry == NULL) {
5887 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5888 return NULL;
5890 if (view->searching == TOG_SEARCH_FORWARD)
5891 re = TAILQ_FIRST(&s->refs);
5892 else
5893 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5896 if (match_reflist_entry(re, &view->regex)) {
5897 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5898 s->matched_entry = re;
5899 break;
5902 if (view->searching == TOG_SEARCH_FORWARD)
5903 re = TAILQ_NEXT(re, entry);
5904 else
5905 re = TAILQ_PREV(re, tog_reflist_head, entry);
5908 if (s->matched_entry) {
5909 s->first_displayed_entry = s->matched_entry;
5910 s->selected = 0;
5913 return NULL;
5916 static const struct got_error *
5917 show_ref_view(struct tog_view *view)
5919 const struct got_error *err = NULL;
5920 struct tog_ref_view_state *s = &view->state.ref;
5921 struct tog_reflist_entry *re;
5922 char *line = NULL;
5923 wchar_t *wline;
5924 struct tog_color *tc;
5925 int width, n;
5926 int limit = view->nlines;
5928 werase(view->window);
5930 s->ndisplayed = 0;
5932 if (limit == 0)
5933 return NULL;
5935 re = s->first_displayed_entry;
5937 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5938 s->nrefs) == -1)
5939 return got_error_from_errno("asprintf");
5941 err = format_line(&wline, &width, line, view->ncols, 0);
5942 if (err) {
5943 free(line);
5944 return err;
5946 if (view_needs_focus_indication(view))
5947 wstandout(view->window);
5948 waddwstr(view->window, wline);
5949 if (view_needs_focus_indication(view))
5950 wstandend(view->window);
5951 free(wline);
5952 wline = NULL;
5953 free(line);
5954 line = NULL;
5955 if (width < view->ncols - 1)
5956 waddch(view->window, '\n');
5957 if (--limit <= 0)
5958 return NULL;
5960 n = 0;
5961 while (re && limit > 0) {
5962 char *line = NULL;
5964 if (got_ref_is_symbolic(re->ref)) {
5965 if (asprintf(&line, "%s -> %s",
5966 got_ref_get_name(re->ref),
5967 got_ref_get_symref_target(re->ref)) == -1)
5968 return got_error_from_errno("asprintf");
5969 } else if (s->show_ids) {
5970 struct got_object_id *id;
5971 char *id_str;
5972 err = got_ref_resolve(&id, s->repo, re->ref);
5973 if (err)
5974 return err;
5975 err = got_object_id_str(&id_str, id);
5976 if (err) {
5977 free(id);
5978 return err;
5980 if (asprintf(&line, "%s: %s",
5981 got_ref_get_name(re->ref), id_str) == -1) {
5982 err = got_error_from_errno("asprintf");
5983 free(id);
5984 free(id_str);
5985 return err;
5987 free(id);
5988 free(id_str);
5989 } else {
5990 line = strdup(got_ref_get_name(re->ref));
5991 if (line == NULL)
5992 return got_error_from_errno("strdup");
5995 err = format_line(&wline, &width, line, view->ncols, 0);
5996 if (err) {
5997 free(line);
5998 return err;
6000 if (n == s->selected) {
6001 if (view->focussed)
6002 wstandout(view->window);
6003 s->selected_entry = re;
6005 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6006 if (tc)
6007 wattr_on(view->window,
6008 COLOR_PAIR(tc->colorpair), NULL);
6009 waddwstr(view->window, wline);
6010 if (tc)
6011 wattr_off(view->window,
6012 COLOR_PAIR(tc->colorpair), NULL);
6013 if (width < view->ncols - 1)
6014 waddch(view->window, '\n');
6015 if (n == s->selected && view->focussed)
6016 wstandend(view->window);
6017 free(line);
6018 free(wline);
6019 wline = NULL;
6020 n++;
6021 s->ndisplayed++;
6022 s->last_displayed_entry = re;
6024 limit--;
6025 re = TAILQ_NEXT(re, entry);
6028 view_vborder(view);
6029 return err;
6032 static const struct got_error *
6033 browse_ref_tree(struct tog_view **new_view, int begin_x,
6034 struct tog_reflist_entry *re, struct got_repository *repo)
6036 const struct got_error *err = NULL;
6037 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6038 struct got_tree_object *tree = NULL;
6039 struct tog_view *tree_view;
6041 *new_view = NULL;
6043 err = resolve_reflist_entry(&commit_id, re, repo);
6044 if (err) {
6045 if (err->code != GOT_ERR_OBJ_TYPE)
6046 return err;
6047 else
6048 return NULL;
6051 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6052 if (err)
6053 goto done;
6055 err = got_object_open_as_tree(&tree, repo, tree_id);
6056 if (err)
6057 goto done;
6059 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6060 if (tree_view == NULL) {
6061 err = got_error_from_errno("view_open");
6062 goto done;
6065 err = open_tree_view(tree_view, tree, commit_id, repo);
6066 if (err)
6067 goto done;
6069 *new_view = tree_view;
6070 done:
6071 free(commit_id);
6072 free(tree_id);
6073 if (err) {
6074 if (tree)
6075 got_object_tree_close(tree);
6077 return err;
6079 static const struct got_error *
6080 input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6081 struct tog_view **focus_view, struct tog_view *view, int ch)
6083 const struct got_error *err = NULL;
6084 struct tog_ref_view_state *s = &view->state.ref;
6085 struct tog_view *log_view, *tree_view;
6086 int begin_x = 0;
6088 switch (ch) {
6089 case 'i':
6090 s->show_ids = !s->show_ids;
6091 break;
6092 case KEY_ENTER:
6093 case '\r':
6094 if (!s->selected_entry)
6095 break;
6096 if (view_is_parent_view(view))
6097 begin_x = view_split_begin_x(view->begin_x);
6098 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6099 s->repo);
6100 if (view_is_parent_view(view)) {
6101 err = view_close_child(view);
6102 if (err)
6103 return err;
6104 err = view_set_child(view, log_view);
6105 if (err) {
6106 view_close(log_view);
6107 break;
6109 *focus_view = log_view;
6110 view->child_focussed = 1;
6111 } else
6112 *new_view = log_view;
6113 break;
6114 case 't':
6115 if (!s->selected_entry)
6116 break;
6117 if (view_is_parent_view(view))
6118 begin_x = view_split_begin_x(view->begin_x);
6119 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6120 s->repo);
6121 if (err || tree_view == NULL)
6122 break;
6123 if (view_is_parent_view(view)) {
6124 err = view_close_child(view);
6125 if (err)
6126 return err;
6127 err = view_set_child(view, tree_view);
6128 if (err) {
6129 view_close(tree_view);
6130 break;
6132 *focus_view = tree_view;
6133 view->child_focussed = 1;
6134 } else
6135 *new_view = tree_view;
6136 break;
6137 case 'k':
6138 case KEY_UP:
6139 if (s->selected > 0) {
6140 s->selected--;
6141 break;
6143 ref_scroll_up(view, 1);
6144 break;
6145 case KEY_PPAGE:
6146 case CTRL('b'):
6147 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6148 s->selected = 0;
6149 ref_scroll_up(view, MAX(0, view->nlines - 1));
6150 break;
6151 case 'j':
6152 case KEY_DOWN:
6153 if (s->selected < s->ndisplayed - 1) {
6154 s->selected++;
6155 break;
6157 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6158 /* can't scroll any further */
6159 break;
6160 ref_scroll_down(view, 1);
6161 break;
6162 case KEY_NPAGE:
6163 case CTRL('f'):
6164 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6165 /* can't scroll any further; move cursor down */
6166 if (s->selected < s->ndisplayed - 1)
6167 s->selected = s->ndisplayed - 1;
6168 break;
6170 ref_scroll_down(view, view->nlines - 1);
6171 break;
6172 case CTRL('l'):
6173 ref_view_free_refs(s);
6174 err = ref_view_load_refs(s);
6175 break;
6176 case KEY_RESIZE:
6177 if (s->selected > view->nlines)
6178 s->selected = s->ndisplayed - 1;
6179 break;
6180 default:
6181 break;
6184 return err;
6187 __dead static void
6188 usage_ref(void)
6190 endwin();
6191 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6192 getprogname());
6193 exit(1);
6196 static const struct got_error *
6197 cmd_ref(int argc, char *argv[])
6199 const struct got_error *error;
6200 struct got_repository *repo = NULL;
6201 struct got_worktree *worktree = NULL;
6202 char *cwd = NULL, *repo_path = NULL;
6203 int ch;
6204 struct tog_view *view;
6206 #ifndef PROFILE
6207 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6208 NULL) == -1)
6209 err(1, "pledge");
6210 #endif
6212 while ((ch = getopt(argc, argv, "r:")) != -1) {
6213 switch (ch) {
6214 case 'r':
6215 repo_path = realpath(optarg, NULL);
6216 if (repo_path == NULL)
6217 return got_error_from_errno2("realpath",
6218 optarg);
6219 break;
6220 default:
6221 usage_ref();
6222 /* NOTREACHED */
6226 argc -= optind;
6227 argv += optind;
6229 if (argc > 1)
6230 usage_ref();
6232 cwd = getcwd(NULL, 0);
6233 if (cwd == NULL)
6234 return got_error_from_errno("getcwd");
6236 error = got_worktree_open(&worktree, cwd);
6237 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6238 goto done;
6240 if (repo_path == NULL) {
6241 if (worktree)
6242 repo_path =
6243 strdup(got_worktree_get_repo_path(worktree));
6244 else
6245 repo_path = strdup(cwd);
6247 if (repo_path == NULL) {
6248 error = got_error_from_errno("strdup");
6249 goto done;
6252 error = got_repo_open(&repo, repo_path, NULL);
6253 if (error != NULL)
6254 goto done;
6256 init_curses();
6258 error = apply_unveil(got_repo_get_path(repo), NULL);
6259 if (error)
6260 goto done;
6262 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6263 if (view == NULL) {
6264 error = got_error_from_errno("view_open");
6265 goto done;
6268 error = open_ref_view(view, repo);
6269 if (error)
6270 goto done;
6272 if (worktree) {
6273 /* Release work tree lock. */
6274 got_worktree_close(worktree);
6275 worktree = NULL;
6277 error = view_loop(view);
6278 done:
6279 free(repo_path);
6280 free(cwd);
6281 if (repo)
6282 got_repo_close(repo);
6283 return error;
6286 static void
6287 list_commands(FILE *fp)
6289 int i;
6291 fprintf(fp, "commands:");
6292 for (i = 0; i < nitems(tog_commands); i++) {
6293 struct tog_cmd *cmd = &tog_commands[i];
6294 fprintf(fp, " %s", cmd->name);
6296 fputc('\n', fp);
6299 __dead static void
6300 usage(int hflag, int status)
6302 FILE *fp = (status == 0) ? stdout : stderr;
6304 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6305 getprogname());
6306 if (hflag) {
6307 fprintf(fp, "lazy usage: %s path\n", getprogname());
6308 list_commands(fp);
6310 exit(status);
6313 static char **
6314 make_argv(int argc, ...)
6316 va_list ap;
6317 char **argv;
6318 int i;
6320 va_start(ap, argc);
6322 argv = calloc(argc, sizeof(char *));
6323 if (argv == NULL)
6324 err(1, "calloc");
6325 for (i = 0; i < argc; i++) {
6326 argv[i] = strdup(va_arg(ap, char *));
6327 if (argv[i] == NULL)
6328 err(1, "strdup");
6331 va_end(ap);
6332 return argv;
6336 * Try to convert 'tog path' into a 'tog log path' command.
6337 * The user could simply have mistyped the command rather than knowingly
6338 * provided a path. So check whether argv[0] can in fact be resolved
6339 * to a path in the HEAD commit and print a special error if not.
6340 * This hack is for mpi@ <3
6342 static const struct got_error *
6343 tog_log_with_path(int argc, char *argv[])
6345 const struct got_error *error = NULL;
6346 struct tog_cmd *cmd = NULL;
6347 struct got_repository *repo = NULL;
6348 struct got_worktree *worktree = NULL;
6349 struct got_object_id *commit_id = NULL, *id = NULL;
6350 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6351 char *commit_id_str = NULL, **cmd_argv = NULL;
6353 cwd = getcwd(NULL, 0);
6354 if (cwd == NULL)
6355 return got_error_from_errno("getcwd");
6357 error = got_worktree_open(&worktree, cwd);
6358 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6359 goto done;
6361 if (worktree)
6362 repo_path = strdup(got_worktree_get_repo_path(worktree));
6363 else
6364 repo_path = strdup(cwd);
6365 if (repo_path == NULL) {
6366 error = got_error_from_errno("strdup");
6367 goto done;
6370 error = got_repo_open(&repo, repo_path, NULL);
6371 if (error != NULL)
6372 goto done;
6374 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6375 repo, worktree);
6376 if (error)
6377 goto done;
6379 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6380 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6381 GOT_OBJ_TYPE_COMMIT, 1, repo);
6382 if (error)
6383 goto done;
6385 if (worktree) {
6386 got_worktree_close(worktree);
6387 worktree = NULL;
6390 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6391 if (error) {
6392 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6393 goto done;
6394 fprintf(stderr, "%s: '%s' is no known command or path\n",
6395 getprogname(), argv[0]);
6396 usage(1, 1);
6397 /* not reached */
6400 got_repo_close(repo);
6401 repo = NULL;
6403 error = got_object_id_str(&commit_id_str, commit_id);
6404 if (error)
6405 goto done;
6407 cmd = &tog_commands[0]; /* log */
6408 argc = 4;
6409 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6410 error = cmd->cmd_main(argc, cmd_argv);
6411 done:
6412 if (repo)
6413 got_repo_close(repo);
6414 if (worktree)
6415 got_worktree_close(worktree);
6416 free(id);
6417 free(commit_id_str);
6418 free(commit_id);
6419 free(cwd);
6420 free(repo_path);
6421 free(in_repo_path);
6422 if (cmd_argv) {
6423 int i;
6424 for (i = 0; i < argc; i++)
6425 free(cmd_argv[i]);
6426 free(cmd_argv);
6428 return error;
6431 int
6432 main(int argc, char *argv[])
6434 const struct got_error *error = NULL;
6435 struct tog_cmd *cmd = NULL;
6436 int ch, hflag = 0, Vflag = 0;
6437 char **cmd_argv = NULL;
6438 static struct option longopts[] = {
6439 { "version", no_argument, NULL, 'V' },
6440 { NULL, 0, NULL, 0}
6443 setlocale(LC_CTYPE, "");
6445 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6446 switch (ch) {
6447 case 'h':
6448 hflag = 1;
6449 break;
6450 case 'V':
6451 Vflag = 1;
6452 break;
6453 default:
6454 usage(hflag, 1);
6455 /* NOTREACHED */
6459 argc -= optind;
6460 argv += optind;
6461 optind = 1;
6462 optreset = 1;
6464 if (Vflag) {
6465 got_version_print_str();
6466 return 0;
6469 if (argc == 0) {
6470 if (hflag)
6471 usage(hflag, 0);
6472 /* Build an argument vector which runs a default command. */
6473 cmd = &tog_commands[0];
6474 argc = 1;
6475 cmd_argv = make_argv(argc, cmd->name);
6476 } else {
6477 int i;
6479 /* Did the user specify a command? */
6480 for (i = 0; i < nitems(tog_commands); i++) {
6481 if (strncmp(tog_commands[i].name, argv[0],
6482 strlen(argv[0])) == 0) {
6483 cmd = &tog_commands[i];
6484 break;
6489 if (cmd == NULL) {
6490 if (argc != 1)
6491 usage(0, 1);
6492 /* No command specified; try log with a path */
6493 error = tog_log_with_path(argc, argv);
6494 } else {
6495 if (hflag)
6496 cmd->cmd_usage();
6497 else
6498 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6501 endwin();
6502 putchar('\n');
6503 if (cmd_argv) {
6504 int i;
6505 for (i = 0; i < argc; i++)
6506 free(cmd_argv[i]);
6507 free(cmd_argv);
6510 if (error && error->code != GOT_ERR_CANCELLED)
6511 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6512 return 0;