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 /* for ncurses wide-character functions */
24 #include <curses.h>
25 #include <panel.h>
26 #include <locale.h>
27 #include <sha1.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 <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.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 const 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 STAILQ_ENTRY(tog_color) entry;
124 regex_t regex;
125 short colorpair;
126 };
127 STAILQ_HEAD(tog_colors, tog_color);
129 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
130 static struct got_reflist_object_id_map *tog_refs_idmap;
132 static const struct got_error *
133 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
134 struct got_reference* re2)
136 const char *name1 = got_ref_get_name(re1);
137 const char *name2 = got_ref_get_name(re2);
138 int isbackup1, isbackup2;
140 /* Sort backup refs towards the bottom of the list. */
141 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
142 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
143 if (!isbackup1 && isbackup2) {
144 *cmp = -1;
145 return NULL;
146 } else if (isbackup1 && !isbackup2) {
147 *cmp = 1;
148 return NULL;
151 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
152 return NULL;
155 static const struct got_error *
156 tog_load_refs(struct got_repository *repo, int sort_by_date)
158 const struct got_error *err;
160 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
161 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
162 repo);
163 if (err)
164 return err;
166 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
167 repo);
170 static void
171 tog_free_refs(void)
173 if (tog_refs_idmap) {
174 got_reflist_object_id_map_free(tog_refs_idmap);
175 tog_refs_idmap = NULL;
177 got_ref_list_free(&tog_refs);
180 static const struct got_error *
181 add_color(struct tog_colors *colors, const char *pattern,
182 int idx, short color)
184 const struct got_error *err = NULL;
185 struct tog_color *tc;
186 int regerr = 0;
188 if (idx < 1 || idx > COLOR_PAIRS - 1)
189 return NULL;
191 init_pair(idx, color, -1);
193 tc = calloc(1, sizeof(*tc));
194 if (tc == NULL)
195 return got_error_from_errno("calloc");
196 regerr = regcomp(&tc->regex, pattern,
197 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
198 if (regerr) {
199 static char regerr_msg[512];
200 static char err_msg[512];
201 regerror(regerr, &tc->regex, regerr_msg,
202 sizeof(regerr_msg));
203 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
204 regerr_msg);
205 err = got_error_msg(GOT_ERR_REGEX, err_msg);
206 free(tc);
207 return err;
209 tc->colorpair = idx;
210 STAILQ_INSERT_HEAD(colors, tc, entry);
211 return NULL;
214 static void
215 free_colors(struct tog_colors *colors)
217 struct tog_color *tc;
219 while (!STAILQ_EMPTY(colors)) {
220 tc = STAILQ_FIRST(colors);
221 STAILQ_REMOVE_HEAD(colors, entry);
222 regfree(&tc->regex);
223 free(tc);
227 struct tog_color *
228 get_color(struct tog_colors *colors, int colorpair)
230 struct tog_color *tc = NULL;
232 STAILQ_FOREACH(tc, colors, entry) {
233 if (tc->colorpair == colorpair)
234 return tc;
237 return NULL;
240 static int
241 default_color_value(const char *envvar)
243 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
244 return COLOR_MAGENTA;
245 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
246 return COLOR_CYAN;
247 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
248 return COLOR_YELLOW;
249 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
250 return COLOR_GREEN;
251 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
254 return COLOR_MAGENTA;
255 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
256 return COLOR_CYAN;
257 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
258 return COLOR_GREEN;
259 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
260 return COLOR_GREEN;
261 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
262 return COLOR_CYAN;
263 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
264 return COLOR_YELLOW;
265 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
266 return COLOR_GREEN;
267 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
270 return COLOR_YELLOW;
271 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
272 return COLOR_CYAN;
274 return -1;
277 static int
278 get_color_value(const char *envvar)
280 const char *val = getenv(envvar);
282 if (val == NULL)
283 return default_color_value(envvar);
285 if (strcasecmp(val, "black") == 0)
286 return COLOR_BLACK;
287 if (strcasecmp(val, "red") == 0)
288 return COLOR_RED;
289 if (strcasecmp(val, "green") == 0)
290 return COLOR_GREEN;
291 if (strcasecmp(val, "yellow") == 0)
292 return COLOR_YELLOW;
293 if (strcasecmp(val, "blue") == 0)
294 return COLOR_BLUE;
295 if (strcasecmp(val, "magenta") == 0)
296 return COLOR_MAGENTA;
297 if (strcasecmp(val, "cyan") == 0)
298 return COLOR_CYAN;
299 if (strcasecmp(val, "white") == 0)
300 return COLOR_WHITE;
301 if (strcasecmp(val, "default") == 0)
302 return -1;
304 return default_color_value(envvar);
308 struct tog_diff_view_state {
309 struct got_object_id *id1, *id2;
310 const char *label1, *label2;
311 FILE *f, *f1, *f2;
312 int first_displayed_line;
313 int last_displayed_line;
314 int eof;
315 int diff_context;
316 int ignore_whitespace;
317 int force_text_diff;
318 struct got_repository *repo;
319 struct tog_colors colors;
320 size_t nlines;
321 off_t *line_offsets;
322 int matched_line;
323 int selected_line;
325 /* passed from log view; may be NULL */
326 struct tog_view *log_view;
327 };
329 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
331 struct tog_log_thread_args {
332 pthread_cond_t need_commits;
333 pthread_cond_t commit_loaded;
334 int commits_needed;
335 int load_all;
336 struct got_commit_graph *graph;
337 struct commit_queue *commits;
338 const char *in_repo_path;
339 struct got_object_id *start_id;
340 struct got_repository *repo;
341 int *pack_fds;
342 int log_complete;
343 sig_atomic_t *quit;
344 struct commit_queue_entry **first_displayed_entry;
345 struct commit_queue_entry **selected_entry;
346 int *searching;
347 int *search_next_done;
348 regex_t *regex;
349 };
351 struct tog_log_view_state {
352 struct commit_queue commits;
353 struct commit_queue_entry *first_displayed_entry;
354 struct commit_queue_entry *last_displayed_entry;
355 struct commit_queue_entry *selected_entry;
356 int selected;
357 char *in_repo_path;
358 char *head_ref_name;
359 int log_branches;
360 struct got_repository *repo;
361 struct got_object_id *start_id;
362 sig_atomic_t quit;
363 pthread_t thread;
364 struct tog_log_thread_args thread_args;
365 struct commit_queue_entry *matched_entry;
366 struct commit_queue_entry *search_entry;
367 struct tog_colors colors;
368 };
370 #define TOG_COLOR_DIFF_MINUS 1
371 #define TOG_COLOR_DIFF_PLUS 2
372 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
373 #define TOG_COLOR_DIFF_META 4
374 #define TOG_COLOR_TREE_SUBMODULE 5
375 #define TOG_COLOR_TREE_SYMLINK 6
376 #define TOG_COLOR_TREE_DIRECTORY 7
377 #define TOG_COLOR_TREE_EXECUTABLE 8
378 #define TOG_COLOR_COMMIT 9
379 #define TOG_COLOR_AUTHOR 10
380 #define TOG_COLOR_DATE 11
381 #define TOG_COLOR_REFS_HEADS 12
382 #define TOG_COLOR_REFS_TAGS 13
383 #define TOG_COLOR_REFS_REMOTES 14
384 #define TOG_COLOR_REFS_BACKUP 15
386 struct tog_blame_cb_args {
387 struct tog_blame_line *lines; /* one per line */
388 int nlines;
390 struct tog_view *view;
391 struct got_object_id *commit_id;
392 int *quit;
393 };
395 struct tog_blame_thread_args {
396 const char *path;
397 struct got_repository *repo;
398 struct tog_blame_cb_args *cb_args;
399 int *complete;
400 got_cancel_cb cancel_cb;
401 void *cancel_arg;
402 };
404 struct tog_blame {
405 FILE *f;
406 off_t filesize;
407 struct tog_blame_line *lines;
408 int nlines;
409 off_t *line_offsets;
410 pthread_t thread;
411 struct tog_blame_thread_args thread_args;
412 struct tog_blame_cb_args cb_args;
413 const char *path;
414 int *pack_fds;
415 };
417 struct tog_blame_view_state {
418 int first_displayed_line;
419 int last_displayed_line;
420 int selected_line;
421 int blame_complete;
422 int eof;
423 int done;
424 struct got_object_id_queue blamed_commits;
425 struct got_object_qid *blamed_commit;
426 char *path;
427 struct got_repository *repo;
428 struct got_object_id *commit_id;
429 struct tog_blame blame;
430 int matched_line;
431 struct tog_colors colors;
432 };
434 struct tog_parent_tree {
435 TAILQ_ENTRY(tog_parent_tree) entry;
436 struct got_tree_object *tree;
437 struct got_tree_entry *first_displayed_entry;
438 struct got_tree_entry *selected_entry;
439 int selected;
440 };
442 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
444 struct tog_tree_view_state {
445 char *tree_label;
446 struct got_object_id *commit_id;/* commit which this tree belongs to */
447 struct got_tree_object *root; /* the commit's root tree entry */
448 struct got_tree_object *tree; /* currently displayed (sub-)tree */
449 struct got_tree_entry *first_displayed_entry;
450 struct got_tree_entry *last_displayed_entry;
451 struct got_tree_entry *selected_entry;
452 int ndisplayed, selected, show_ids;
453 struct tog_parent_trees parents; /* parent trees of current sub-tree */
454 char *head_ref_name;
455 struct got_repository *repo;
456 struct got_tree_entry *matched_entry;
457 struct tog_colors colors;
458 };
460 struct tog_reflist_entry {
461 TAILQ_ENTRY(tog_reflist_entry) entry;
462 struct got_reference *ref;
463 int idx;
464 };
466 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
468 struct tog_ref_view_state {
469 struct tog_reflist_head refs;
470 struct tog_reflist_entry *first_displayed_entry;
471 struct tog_reflist_entry *last_displayed_entry;
472 struct tog_reflist_entry *selected_entry;
473 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
474 struct got_repository *repo;
475 struct tog_reflist_entry *matched_entry;
476 struct tog_colors colors;
477 };
479 /*
480 * We implement two types of views: parent views and child views.
482 * The 'Tab' key switches focus between a parent view and its child view.
483 * Child views are shown side-by-side to their parent view, provided
484 * there is enough screen estate.
486 * When a new view is opened from within a parent view, this new view
487 * becomes a child view of the parent view, replacing any existing child.
489 * When a new view is opened from within a child view, this new view
490 * becomes a parent view which will obscure the views below until the
491 * user quits the new parent view by typing 'q'.
493 * This list of views contains parent views only.
494 * Child views are only pointed to by their parent view.
495 */
496 TAILQ_HEAD(tog_view_list_head, tog_view);
498 struct tog_view {
499 TAILQ_ENTRY(tog_view) entry;
500 WINDOW *window;
501 PANEL *panel;
502 int nlines, ncols, begin_y, begin_x;
503 int maxx, x; /* max column and current start column */
504 int lines, cols; /* copies of LINES and COLS */
505 int focussed; /* Only set on one parent or child view at a time. */
506 int dying;
507 struct tog_view *parent;
508 struct tog_view *child;
510 /*
511 * This flag is initially set on parent views when a new child view
512 * is created. It gets toggled when the 'Tab' key switches focus
513 * between parent and child.
514 * The flag indicates whether focus should be passed on to our child
515 * view if this parent view gets picked for focus after another parent
516 * view was closed. This prevents child views from losing focus in such
517 * situations.
518 */
519 int focus_child;
521 /* type-specific state */
522 enum tog_view_type type;
523 union {
524 struct tog_diff_view_state diff;
525 struct tog_log_view_state log;
526 struct tog_blame_view_state blame;
527 struct tog_tree_view_state tree;
528 struct tog_ref_view_state ref;
529 } state;
531 const struct got_error *(*show)(struct tog_view *);
532 const struct got_error *(*input)(struct tog_view **,
533 struct tog_view *, int);
534 const struct got_error *(*close)(struct tog_view *);
536 const struct got_error *(*search_start)(struct tog_view *);
537 const struct got_error *(*search_next)(struct tog_view *);
538 int search_started;
539 int searching;
540 #define TOG_SEARCH_FORWARD 1
541 #define TOG_SEARCH_BACKWARD 2
542 int search_next_done;
543 #define TOG_SEARCH_HAVE_MORE 1
544 #define TOG_SEARCH_NO_MORE 2
545 #define TOG_SEARCH_HAVE_NONE 3
546 regex_t regex;
547 regmatch_t regmatch;
548 };
550 static const struct got_error *open_diff_view(struct tog_view *,
551 struct got_object_id *, struct got_object_id *,
552 const char *, const char *, int, int, int, struct tog_view *,
553 struct got_repository *);
554 static const struct got_error *show_diff_view(struct tog_view *);
555 static const struct got_error *input_diff_view(struct tog_view **,
556 struct tog_view *, int);
557 static const struct got_error* close_diff_view(struct tog_view *);
558 static const struct got_error *search_start_diff_view(struct tog_view *);
559 static const struct got_error *search_next_diff_view(struct tog_view *);
561 static const struct got_error *open_log_view(struct tog_view *,
562 struct got_object_id *, struct got_repository *,
563 const char *, const char *, int);
564 static const struct got_error * show_log_view(struct tog_view *);
565 static const struct got_error *input_log_view(struct tog_view **,
566 struct tog_view *, int);
567 static const struct got_error *close_log_view(struct tog_view *);
568 static const struct got_error *search_start_log_view(struct tog_view *);
569 static const struct got_error *search_next_log_view(struct tog_view *);
571 static const struct got_error *open_blame_view(struct tog_view *, char *,
572 struct got_object_id *, struct got_repository *);
573 static const struct got_error *show_blame_view(struct tog_view *);
574 static const struct got_error *input_blame_view(struct tog_view **,
575 struct tog_view *, int);
576 static const struct got_error *close_blame_view(struct tog_view *);
577 static const struct got_error *search_start_blame_view(struct tog_view *);
578 static const struct got_error *search_next_blame_view(struct tog_view *);
580 static const struct got_error *open_tree_view(struct tog_view *,
581 struct got_object_id *, const char *, struct got_repository *);
582 static const struct got_error *show_tree_view(struct tog_view *);
583 static const struct got_error *input_tree_view(struct tog_view **,
584 struct tog_view *, int);
585 static const struct got_error *close_tree_view(struct tog_view *);
586 static const struct got_error *search_start_tree_view(struct tog_view *);
587 static const struct got_error *search_next_tree_view(struct tog_view *);
589 static const struct got_error *open_ref_view(struct tog_view *,
590 struct got_repository *);
591 static const struct got_error *show_ref_view(struct tog_view *);
592 static const struct got_error *input_ref_view(struct tog_view **,
593 struct tog_view *, int);
594 static const struct got_error *close_ref_view(struct tog_view *);
595 static const struct got_error *search_start_ref_view(struct tog_view *);
596 static const struct got_error *search_next_ref_view(struct tog_view *);
598 static volatile sig_atomic_t tog_sigwinch_received;
599 static volatile sig_atomic_t tog_sigpipe_received;
600 static volatile sig_atomic_t tog_sigcont_received;
601 static volatile sig_atomic_t tog_sigint_received;
602 static volatile sig_atomic_t tog_sigterm_received;
604 static void
605 tog_sigwinch(int signo)
607 tog_sigwinch_received = 1;
610 static void
611 tog_sigpipe(int signo)
613 tog_sigpipe_received = 1;
616 static void
617 tog_sigcont(int signo)
619 tog_sigcont_received = 1;
622 static void
623 tog_sigint(int signo)
625 tog_sigint_received = 1;
628 static void
629 tog_sigterm(int signo)
631 tog_sigterm_received = 1;
634 static int
635 tog_fatal_signal_received()
637 return (tog_sigpipe_received ||
638 tog_sigint_received || tog_sigint_received);
642 static const struct got_error *
643 view_close(struct tog_view *view)
645 const struct got_error *err = NULL;
647 if (view->child) {
648 view_close(view->child);
649 view->child = NULL;
651 if (view->close)
652 err = view->close(view);
653 if (view->panel)
654 del_panel(view->panel);
655 if (view->window)
656 delwin(view->window);
657 free(view);
658 return err;
661 static struct tog_view *
662 view_open(int nlines, int ncols, int begin_y, int begin_x,
663 enum tog_view_type type)
665 struct tog_view *view = calloc(1, sizeof(*view));
667 if (view == NULL)
668 return NULL;
670 view->type = type;
671 view->lines = LINES;
672 view->cols = COLS;
673 view->nlines = nlines ? nlines : LINES - begin_y;
674 view->ncols = ncols ? ncols : COLS - begin_x;
675 view->begin_y = begin_y;
676 view->begin_x = begin_x;
677 view->window = newwin(nlines, ncols, begin_y, begin_x);
678 if (view->window == NULL) {
679 view_close(view);
680 return NULL;
682 view->panel = new_panel(view->window);
683 if (view->panel == NULL ||
684 set_panel_userptr(view->panel, view) != OK) {
685 view_close(view);
686 return NULL;
689 keypad(view->window, TRUE);
690 return view;
693 static int
694 view_split_begin_x(int begin_x)
696 if (begin_x > 0 || COLS < 120)
697 return 0;
698 return (COLS - MAX(COLS / 2, 80));
701 static const struct got_error *view_resize(struct tog_view *);
703 static const struct got_error *
704 view_splitscreen(struct tog_view *view)
706 const struct got_error *err = NULL;
708 view->begin_y = 0;
709 view->begin_x = view_split_begin_x(0);
710 view->nlines = LINES;
711 view->ncols = COLS - view->begin_x;
712 view->lines = LINES;
713 view->cols = COLS;
714 err = view_resize(view);
715 if (err)
716 return err;
718 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
719 return got_error_from_errno("mvwin");
721 return NULL;
724 static const struct got_error *
725 view_fullscreen(struct tog_view *view)
727 const struct got_error *err = NULL;
729 view->begin_x = 0;
730 view->begin_y = 0;
731 view->nlines = LINES;
732 view->ncols = COLS;
733 view->lines = LINES;
734 view->cols = COLS;
735 err = view_resize(view);
736 if (err)
737 return err;
739 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
740 return got_error_from_errno("mvwin");
742 return NULL;
745 static int
746 view_is_parent_view(struct tog_view *view)
748 return view->parent == NULL;
751 static const struct got_error *
752 view_resize(struct tog_view *view)
754 int nlines, ncols;
756 if (view->lines > LINES)
757 nlines = view->nlines - (view->lines - LINES);
758 else
759 nlines = view->nlines + (LINES - view->lines);
761 if (view->cols > COLS)
762 ncols = view->ncols - (view->cols - COLS);
763 else
764 ncols = view->ncols + (COLS - view->cols);
766 if (wresize(view->window, nlines, ncols) == ERR)
767 return got_error_from_errno("wresize");
768 if (replace_panel(view->panel, view->window) == ERR)
769 return got_error_from_errno("replace_panel");
770 wclear(view->window);
772 view->nlines = nlines;
773 view->ncols = ncols;
774 view->lines = LINES;
775 view->cols = COLS;
777 if (view->child) {
778 view->child->begin_x = view_split_begin_x(view->begin_x);
779 if (view->child->begin_x == 0) {
780 view_fullscreen(view->child);
781 if (view->child->focussed)
782 show_panel(view->child->panel);
783 else
784 show_panel(view->panel);
785 } else {
786 view_splitscreen(view->child);
787 show_panel(view->child->panel);
791 return NULL;
794 static const struct got_error *
795 view_close_child(struct tog_view *view)
797 const struct got_error *err = NULL;
799 if (view->child == NULL)
800 return NULL;
802 err = view_close(view->child);
803 view->child = NULL;
804 return err;
807 static void
808 view_set_child(struct tog_view *view, struct tog_view *child)
810 view->child = child;
811 child->parent = view;
814 static int
815 view_is_splitscreen(struct tog_view *view)
817 return view->begin_x > 0;
820 static void
821 tog_resizeterm(void)
823 int cols, lines;
824 struct winsize size;
826 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
827 cols = 80; /* Default */
828 lines = 24;
829 } else {
830 cols = size.ws_col;
831 lines = size.ws_row;
833 resize_term(lines, cols);
836 static const struct got_error *
837 view_search_start(struct tog_view *view)
839 const struct got_error *err = NULL;
840 char pattern[1024];
841 int ret;
843 if (view->search_started) {
844 regfree(&view->regex);
845 view->searching = 0;
846 memset(&view->regmatch, 0, sizeof(view->regmatch));
848 view->search_started = 0;
850 if (view->nlines < 1)
851 return NULL;
853 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
854 wclrtoeol(view->window);
856 nocbreak();
857 echo();
858 ret = wgetnstr(view->window, pattern, sizeof(pattern));
859 cbreak();
860 noecho();
861 if (ret == ERR)
862 return NULL;
864 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
865 err = view->search_start(view);
866 if (err) {
867 regfree(&view->regex);
868 return err;
870 view->search_started = 1;
871 view->searching = TOG_SEARCH_FORWARD;
872 view->search_next_done = 0;
873 view->search_next(view);
876 return NULL;
879 static const struct got_error *
880 view_input(struct tog_view **new, int *done, struct tog_view *view,
881 struct tog_view_list_head *views)
883 const struct got_error *err = NULL;
884 struct tog_view *v;
885 int ch, errcode;
887 *new = NULL;
889 /* Clear "no matches" indicator. */
890 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
891 view->search_next_done == TOG_SEARCH_HAVE_NONE)
892 view->search_next_done = TOG_SEARCH_HAVE_MORE;
894 if (view->searching && !view->search_next_done) {
895 errcode = pthread_mutex_unlock(&tog_mutex);
896 if (errcode)
897 return got_error_set_errno(errcode,
898 "pthread_mutex_unlock");
899 sched_yield();
900 errcode = pthread_mutex_lock(&tog_mutex);
901 if (errcode)
902 return got_error_set_errno(errcode,
903 "pthread_mutex_lock");
904 view->search_next(view);
905 return NULL;
908 nodelay(stdscr, FALSE);
909 /* Allow threads to make progress while we are waiting for input. */
910 errcode = pthread_mutex_unlock(&tog_mutex);
911 if (errcode)
912 return got_error_set_errno(errcode, "pthread_mutex_unlock");
913 ch = wgetch(view->window);
914 errcode = pthread_mutex_lock(&tog_mutex);
915 if (errcode)
916 return got_error_set_errno(errcode, "pthread_mutex_lock");
917 nodelay(stdscr, TRUE);
919 if (tog_sigwinch_received || tog_sigcont_received) {
920 tog_resizeterm();
921 tog_sigwinch_received = 0;
922 tog_sigcont_received = 0;
923 TAILQ_FOREACH(v, views, entry) {
924 err = view_resize(v);
925 if (err)
926 return err;
927 err = v->input(new, v, KEY_RESIZE);
928 if (err)
929 return err;
930 if (v->child) {
931 err = view_resize(v->child);
932 if (err)
933 return err;
934 err = v->child->input(new, v->child,
935 KEY_RESIZE);
936 if (err)
937 return err;
942 switch (ch) {
943 case '\t':
944 if (view->child) {
945 view->focussed = 0;
946 view->child->focussed = 1;
947 view->focus_child = 1;
948 } else if (view->parent) {
949 view->focussed = 0;
950 view->parent->focussed = 1;
951 view->parent->focus_child = 0;
953 break;
954 case 'q':
955 err = view->input(new, view, ch);
956 view->dying = 1;
957 break;
958 case 'Q':
959 *done = 1;
960 break;
961 case 'f':
962 if (view_is_parent_view(view)) {
963 if (view->child == NULL)
964 break;
965 if (view_is_splitscreen(view->child)) {
966 view->focussed = 0;
967 view->child->focussed = 1;
968 err = view_fullscreen(view->child);
969 } else
970 err = view_splitscreen(view->child);
971 if (err)
972 break;
973 err = view->child->input(new, view->child,
974 KEY_RESIZE);
975 } else {
976 if (view_is_splitscreen(view)) {
977 view->parent->focussed = 0;
978 view->focussed = 1;
979 err = view_fullscreen(view);
980 } else {
981 err = view_splitscreen(view);
983 if (err)
984 break;
985 err = view->input(new, view, KEY_RESIZE);
987 break;
988 case KEY_RESIZE:
989 break;
990 case '/':
991 if (view->search_start)
992 view_search_start(view);
993 else
994 err = view->input(new, view, ch);
995 break;
996 case 'N':
997 case 'n':
998 if (view->search_started && view->search_next) {
999 view->searching = (ch == 'n' ?
1000 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1001 view->search_next_done = 0;
1002 view->search_next(view);
1003 } else
1004 err = view->input(new, view, ch);
1005 break;
1006 default:
1007 err = view->input(new, view, ch);
1008 break;
1011 return err;
1014 void
1015 view_vborder(struct tog_view *view)
1017 PANEL *panel;
1018 const struct tog_view *view_above;
1020 if (view->parent)
1021 return view_vborder(view->parent);
1023 panel = panel_above(view->panel);
1024 if (panel == NULL)
1025 return;
1027 view_above = panel_userptr(panel);
1028 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1029 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1032 int
1033 view_needs_focus_indication(struct tog_view *view)
1035 if (view_is_parent_view(view)) {
1036 if (view->child == NULL || view->child->focussed)
1037 return 0;
1038 if (!view_is_splitscreen(view->child))
1039 return 0;
1040 } else if (!view_is_splitscreen(view))
1041 return 0;
1043 return view->focussed;
1046 static const struct got_error *
1047 view_loop(struct tog_view *view)
1049 const struct got_error *err = NULL;
1050 struct tog_view_list_head views;
1051 struct tog_view *new_view;
1052 int fast_refresh = 10;
1053 int done = 0, errcode;
1055 errcode = pthread_mutex_lock(&tog_mutex);
1056 if (errcode)
1057 return got_error_set_errno(errcode, "pthread_mutex_lock");
1059 TAILQ_INIT(&views);
1060 TAILQ_INSERT_HEAD(&views, view, entry);
1062 view->focussed = 1;
1063 err = view->show(view);
1064 if (err)
1065 return err;
1066 update_panels();
1067 doupdate();
1068 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1069 /* Refresh fast during initialization, then become slower. */
1070 if (fast_refresh && fast_refresh-- == 0)
1071 halfdelay(10); /* switch to once per second */
1073 err = view_input(&new_view, &done, view, &views);
1074 if (err)
1075 break;
1076 if (view->dying) {
1077 struct tog_view *v, *prev = NULL;
1079 if (view_is_parent_view(view))
1080 prev = TAILQ_PREV(view, tog_view_list_head,
1081 entry);
1082 else if (view->parent)
1083 prev = view->parent;
1085 if (view->parent) {
1086 view->parent->child = NULL;
1087 view->parent->focus_child = 0;
1088 } else
1089 TAILQ_REMOVE(&views, view, entry);
1091 err = view_close(view);
1092 if (err)
1093 goto done;
1095 view = NULL;
1096 TAILQ_FOREACH(v, &views, entry) {
1097 if (v->focussed)
1098 break;
1100 if (view == NULL && new_view == NULL) {
1101 /* No view has focus. Try to pick one. */
1102 if (prev)
1103 view = prev;
1104 else if (!TAILQ_EMPTY(&views)) {
1105 view = TAILQ_LAST(&views,
1106 tog_view_list_head);
1108 if (view) {
1109 if (view->focus_child) {
1110 view->child->focussed = 1;
1111 view = view->child;
1112 } else
1113 view->focussed = 1;
1117 if (new_view) {
1118 struct tog_view *v, *t;
1119 /* Only allow one parent view per type. */
1120 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1121 if (v->type != new_view->type)
1122 continue;
1123 TAILQ_REMOVE(&views, v, entry);
1124 err = view_close(v);
1125 if (err)
1126 goto done;
1127 break;
1129 TAILQ_INSERT_TAIL(&views, new_view, entry);
1130 view = new_view;
1132 if (view) {
1133 if (view_is_parent_view(view)) {
1134 if (view->child && view->child->focussed)
1135 view = view->child;
1136 } else {
1137 if (view->parent && view->parent->focussed)
1138 view = view->parent;
1140 show_panel(view->panel);
1141 if (view->child && view_is_splitscreen(view->child))
1142 show_panel(view->child->panel);
1143 if (view->parent && view_is_splitscreen(view)) {
1144 err = view->parent->show(view->parent);
1145 if (err)
1146 goto done;
1148 err = view->show(view);
1149 if (err)
1150 goto done;
1151 if (view->child) {
1152 err = view->child->show(view->child);
1153 if (err)
1154 goto done;
1156 update_panels();
1157 doupdate();
1160 done:
1161 while (!TAILQ_EMPTY(&views)) {
1162 view = TAILQ_FIRST(&views);
1163 TAILQ_REMOVE(&views, view, entry);
1164 view_close(view);
1167 errcode = pthread_mutex_unlock(&tog_mutex);
1168 if (errcode && err == NULL)
1169 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1171 return err;
1174 __dead static void
1175 usage_log(void)
1177 endwin();
1178 fprintf(stderr,
1179 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1180 getprogname());
1181 exit(1);
1184 /* Create newly allocated wide-character string equivalent to a byte string. */
1185 static const struct got_error *
1186 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1188 char *vis = NULL;
1189 const struct got_error *err = NULL;
1191 *ws = NULL;
1192 *wlen = mbstowcs(NULL, s, 0);
1193 if (*wlen == (size_t)-1) {
1194 int vislen;
1195 if (errno != EILSEQ)
1196 return got_error_from_errno("mbstowcs");
1198 /* byte string invalid in current encoding; try to "fix" it */
1199 err = got_mbsavis(&vis, &vislen, s);
1200 if (err)
1201 return err;
1202 *wlen = mbstowcs(NULL, vis, 0);
1203 if (*wlen == (size_t)-1) {
1204 err = got_error_from_errno("mbstowcs"); /* give up */
1205 goto done;
1209 *ws = calloc(*wlen + 1, sizeof(**ws));
1210 if (*ws == NULL) {
1211 err = got_error_from_errno("calloc");
1212 goto done;
1215 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1216 err = got_error_from_errno("mbstowcs");
1217 done:
1218 free(vis);
1219 if (err) {
1220 free(*ws);
1221 *ws = NULL;
1222 *wlen = 0;
1224 return err;
1227 static const struct got_error *
1228 expand_tab(char **ptr, const char *src)
1230 char *dst;
1231 size_t len, n, idx = 0, sz = 0;
1233 *ptr = NULL;
1234 n = len = strlen(src);
1235 dst = malloc(n + 1);
1236 if (dst == NULL)
1237 return got_error_from_errno("malloc");
1239 while (idx < len && src[idx]) {
1240 const char c = src[idx];
1242 if (c == '\t') {
1243 size_t nb = TABSIZE - sz % TABSIZE;
1244 char *p = realloc(dst, n + nb);
1245 if (p == NULL) {
1246 free(dst);
1247 return got_error_from_errno("realloc");
1250 dst = p;
1251 n += nb;
1252 memset(dst + sz, ' ', nb);
1253 sz += nb;
1254 } else
1255 dst[sz++] = src[idx];
1256 ++idx;
1259 dst[sz] = '\0';
1260 *ptr = dst;
1261 return NULL;
1265 * Skip leading nscroll columns of a wide character string.
1266 * Returns the index to the first character of the scrolled string.
1268 static const struct got_error *
1269 scroll_wline(int *scrollx , wchar_t *wline, int nscroll,
1270 int col_tab_align)
1272 int cols = 0;
1273 size_t wlen = wcslen(wline);
1274 int i = 0;
1276 *scrollx = 0;
1278 while (i < wlen && cols < nscroll) {
1279 int width = wcwidth(wline[i]);
1281 if (width == 0) {
1282 i++;
1283 continue;
1286 if (width == 1 || width == 2) {
1287 if (cols + width > nscroll)
1288 break;
1289 cols += width;
1290 i++;
1291 } else if (width == -1) {
1292 if (wline[i] == L'\t') {
1293 width = TABSIZE -
1294 ((cols + col_tab_align) % TABSIZE);
1295 } else {
1296 width = 1;
1297 wline[i] = L'.';
1299 if (cols + width > nscroll)
1300 break;
1301 cols += width;
1302 i++;
1303 } else
1304 return got_error_from_errno("wcwidth");
1307 *scrollx = i;
1308 return NULL;
1312 * Format a line for display, ensuring that it won't overflow a width limit.
1313 * With scrolling, the width returned refers to the scrolled version of the
1314 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1316 static const struct got_error *
1317 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1318 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1320 const struct got_error *err = NULL;
1321 int cols = 0;
1322 wchar_t *wline = NULL;
1323 char *exstr = NULL;
1324 size_t wlen;
1325 int i, scrollx = 0;
1327 *wlinep = NULL;
1328 *widthp = 0;
1330 if (expand) {
1331 err = expand_tab(&exstr, line);
1332 if (err)
1333 return err;
1336 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1337 free(exstr);
1338 if (err)
1339 return err;
1341 err = scroll_wline(&scrollx, wline, nscroll, col_tab_align);
1342 if (err)
1343 goto done;
1345 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1346 wline[wlen - 1] = L'\0';
1347 wlen--;
1349 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1350 wline[wlen - 1] = L'\0';
1351 wlen--;
1354 i = scrollx;
1355 while (i < wlen) {
1356 int width = wcwidth(wline[i]);
1358 if (width == 0) {
1359 i++;
1360 continue;
1363 if (width == 1 || width == 2) {
1364 if (cols + width > wlimit)
1365 break;
1366 cols += width;
1367 i++;
1368 } else if (width == -1) {
1369 if (wline[i] == L'\t') {
1370 width = TABSIZE -
1371 ((cols + col_tab_align) % TABSIZE);
1372 } else {
1373 width = 1;
1374 wline[i] = L'.';
1376 if (cols + width > wlimit)
1377 break;
1378 cols += width;
1379 i++;
1380 } else {
1381 err = got_error_from_errno("wcwidth");
1382 goto done;
1385 wline[i] = L'\0';
1386 if (widthp)
1387 *widthp = cols;
1388 if (scrollxp)
1389 *scrollxp = scrollx;
1390 done:
1391 if (err)
1392 free(wline);
1393 else
1394 *wlinep = wline;
1395 return err;
1398 static const struct got_error*
1399 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1400 struct got_object_id *id, struct got_repository *repo)
1402 static const struct got_error *err = NULL;
1403 struct got_reflist_entry *re;
1404 char *s;
1405 const char *name;
1407 *refs_str = NULL;
1409 TAILQ_FOREACH(re, refs, entry) {
1410 struct got_tag_object *tag = NULL;
1411 struct got_object_id *ref_id;
1412 int cmp;
1414 name = got_ref_get_name(re->ref);
1415 if (strcmp(name, GOT_REF_HEAD) == 0)
1416 continue;
1417 if (strncmp(name, "refs/", 5) == 0)
1418 name += 5;
1419 if (strncmp(name, "got/", 4) == 0 &&
1420 strncmp(name, "got/backup/", 11) != 0)
1421 continue;
1422 if (strncmp(name, "heads/", 6) == 0)
1423 name += 6;
1424 if (strncmp(name, "remotes/", 8) == 0) {
1425 name += 8;
1426 s = strstr(name, "/" GOT_REF_HEAD);
1427 if (s != NULL && s[strlen(s)] == '\0')
1428 continue;
1430 err = got_ref_resolve(&ref_id, repo, re->ref);
1431 if (err)
1432 break;
1433 if (strncmp(name, "tags/", 5) == 0) {
1434 err = got_object_open_as_tag(&tag, repo, ref_id);
1435 if (err) {
1436 if (err->code != GOT_ERR_OBJ_TYPE) {
1437 free(ref_id);
1438 break;
1440 /* Ref points at something other than a tag. */
1441 err = NULL;
1442 tag = NULL;
1445 cmp = got_object_id_cmp(tag ?
1446 got_object_tag_get_object_id(tag) : ref_id, id);
1447 free(ref_id);
1448 if (tag)
1449 got_object_tag_close(tag);
1450 if (cmp != 0)
1451 continue;
1452 s = *refs_str;
1453 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1454 s ? ", " : "", name) == -1) {
1455 err = got_error_from_errno("asprintf");
1456 free(s);
1457 *refs_str = NULL;
1458 break;
1460 free(s);
1463 return err;
1466 static const struct got_error *
1467 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1468 int col_tab_align)
1470 char *smallerthan;
1472 smallerthan = strchr(author, '<');
1473 if (smallerthan && smallerthan[1] != '\0')
1474 author = smallerthan + 1;
1475 author[strcspn(author, "@>")] = '\0';
1476 return format_line(wauthor, author_width, NULL, author, 0, limit,
1477 col_tab_align, 0);
1480 static const struct got_error *
1481 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1482 struct got_object_id *id, const size_t date_display_cols,
1483 int author_display_cols)
1485 struct tog_log_view_state *s = &view->state.log;
1486 const struct got_error *err = NULL;
1487 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1488 char *logmsg0 = NULL, *logmsg = NULL;
1489 char *author = NULL;
1490 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1491 int author_width, logmsg_width;
1492 char *newline, *line = NULL;
1493 int col, limit, scrollx;
1494 const int avail = view->ncols;
1495 struct tm tm;
1496 time_t committer_time;
1497 struct tog_color *tc;
1499 committer_time = got_object_commit_get_committer_time(commit);
1500 if (gmtime_r(&committer_time, &tm) == NULL)
1501 return got_error_from_errno("gmtime_r");
1502 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1503 return got_error(GOT_ERR_NO_SPACE);
1505 if (avail <= date_display_cols)
1506 limit = MIN(sizeof(datebuf) - 1, avail);
1507 else
1508 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1509 tc = get_color(&s->colors, TOG_COLOR_DATE);
1510 if (tc)
1511 wattr_on(view->window,
1512 COLOR_PAIR(tc->colorpair), NULL);
1513 waddnstr(view->window, datebuf, limit);
1514 if (tc)
1515 wattr_off(view->window,
1516 COLOR_PAIR(tc->colorpair), NULL);
1517 col = limit;
1518 if (col > avail)
1519 goto done;
1521 if (avail >= 120) {
1522 char *id_str;
1523 err = got_object_id_str(&id_str, id);
1524 if (err)
1525 goto done;
1526 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1527 if (tc)
1528 wattr_on(view->window,
1529 COLOR_PAIR(tc->colorpair), NULL);
1530 wprintw(view->window, "%.8s ", id_str);
1531 if (tc)
1532 wattr_off(view->window,
1533 COLOR_PAIR(tc->colorpair), NULL);
1534 free(id_str);
1535 col += 9;
1536 if (col > avail)
1537 goto done;
1540 author = strdup(got_object_commit_get_author(commit));
1541 if (author == NULL) {
1542 err = got_error_from_errno("strdup");
1543 goto done;
1545 err = format_author(&wauthor, &author_width, author, avail - col, col);
1546 if (err)
1547 goto done;
1548 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1549 if (tc)
1550 wattr_on(view->window,
1551 COLOR_PAIR(tc->colorpair), NULL);
1552 waddwstr(view->window, wauthor);
1553 if (tc)
1554 wattr_off(view->window,
1555 COLOR_PAIR(tc->colorpair), NULL);
1556 col += author_width;
1557 while (col < avail && author_width < author_display_cols + 2) {
1558 waddch(view->window, ' ');
1559 col++;
1560 author_width++;
1562 if (col > avail)
1563 goto done;
1565 err = got_object_commit_get_logmsg(&logmsg0, commit);
1566 if (err)
1567 goto done;
1568 logmsg = logmsg0;
1569 while (*logmsg == '\n')
1570 logmsg++;
1571 newline = strchr(logmsg, '\n');
1572 if (newline)
1573 *newline = '\0';
1574 limit = avail - col;
1575 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1576 limit, col, 1);
1577 if (err)
1578 goto done;
1579 waddwstr(view->window, &wlogmsg[scrollx]);
1580 col += MAX(logmsg_width, 0);
1581 while (col < avail) {
1582 waddch(view->window, ' ');
1583 col++;
1585 done:
1586 free(logmsg0);
1587 free(wlogmsg);
1588 free(author);
1589 free(wauthor);
1590 free(line);
1591 return err;
1594 static struct commit_queue_entry *
1595 alloc_commit_queue_entry(struct got_commit_object *commit,
1596 struct got_object_id *id)
1598 struct commit_queue_entry *entry;
1600 entry = calloc(1, sizeof(*entry));
1601 if (entry == NULL)
1602 return NULL;
1604 entry->id = id;
1605 entry->commit = commit;
1606 return entry;
1609 static void
1610 pop_commit(struct commit_queue *commits)
1612 struct commit_queue_entry *entry;
1614 entry = TAILQ_FIRST(&commits->head);
1615 TAILQ_REMOVE(&commits->head, entry, entry);
1616 got_object_commit_close(entry->commit);
1617 commits->ncommits--;
1618 /* Don't free entry->id! It is owned by the commit graph. */
1619 free(entry);
1622 static void
1623 free_commits(struct commit_queue *commits)
1625 while (!TAILQ_EMPTY(&commits->head))
1626 pop_commit(commits);
1629 static const struct got_error *
1630 match_commit(int *have_match, struct got_object_id *id,
1631 struct got_commit_object *commit, regex_t *regex)
1633 const struct got_error *err = NULL;
1634 regmatch_t regmatch;
1635 char *id_str = NULL, *logmsg = NULL;
1637 *have_match = 0;
1639 err = got_object_id_str(&id_str, id);
1640 if (err)
1641 return err;
1643 err = got_object_commit_get_logmsg(&logmsg, commit);
1644 if (err)
1645 goto done;
1647 if (regexec(regex, got_object_commit_get_author(commit), 1,
1648 &regmatch, 0) == 0 ||
1649 regexec(regex, got_object_commit_get_committer(commit), 1,
1650 &regmatch, 0) == 0 ||
1651 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1652 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1653 *have_match = 1;
1654 done:
1655 free(id_str);
1656 free(logmsg);
1657 return err;
1660 static const struct got_error *
1661 queue_commits(struct tog_log_thread_args *a)
1663 const struct got_error *err = NULL;
1666 * We keep all commits open throughout the lifetime of the log
1667 * view in order to avoid having to re-fetch commits from disk
1668 * while updating the display.
1670 do {
1671 struct got_object_id *id;
1672 struct got_commit_object *commit;
1673 struct commit_queue_entry *entry;
1674 int errcode;
1676 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1677 NULL, NULL);
1678 if (err || id == NULL)
1679 break;
1681 err = got_object_open_as_commit(&commit, a->repo, id);
1682 if (err)
1683 break;
1684 entry = alloc_commit_queue_entry(commit, id);
1685 if (entry == NULL) {
1686 err = got_error_from_errno("alloc_commit_queue_entry");
1687 break;
1690 errcode = pthread_mutex_lock(&tog_mutex);
1691 if (errcode) {
1692 err = got_error_set_errno(errcode,
1693 "pthread_mutex_lock");
1694 break;
1697 entry->idx = a->commits->ncommits;
1698 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1699 a->commits->ncommits++;
1701 if (*a->searching == TOG_SEARCH_FORWARD &&
1702 !*a->search_next_done) {
1703 int have_match;
1704 err = match_commit(&have_match, id, commit, a->regex);
1705 if (err)
1706 break;
1707 if (have_match)
1708 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1711 errcode = pthread_mutex_unlock(&tog_mutex);
1712 if (errcode && err == NULL)
1713 err = got_error_set_errno(errcode,
1714 "pthread_mutex_unlock");
1715 if (err)
1716 break;
1717 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1719 return err;
1722 static void
1723 select_commit(struct tog_log_view_state *s)
1725 struct commit_queue_entry *entry;
1726 int ncommits = 0;
1728 entry = s->first_displayed_entry;
1729 while (entry) {
1730 if (ncommits == s->selected) {
1731 s->selected_entry = entry;
1732 break;
1734 entry = TAILQ_NEXT(entry, entry);
1735 ncommits++;
1739 static const struct got_error *
1740 draw_commits(struct tog_view *view)
1742 const struct got_error *err = NULL;
1743 struct tog_log_view_state *s = &view->state.log;
1744 struct commit_queue_entry *entry = s->selected_entry;
1745 const int limit = view->nlines;
1746 int width;
1747 int ncommits, author_cols = 4;
1748 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1749 char *refs_str = NULL;
1750 wchar_t *wline;
1751 struct tog_color *tc;
1752 static const size_t date_display_cols = 12;
1754 if (s->selected_entry &&
1755 !(view->searching && view->search_next_done == 0)) {
1756 struct got_reflist_head *refs;
1757 err = got_object_id_str(&id_str, s->selected_entry->id);
1758 if (err)
1759 return err;
1760 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1761 s->selected_entry->id);
1762 if (refs) {
1763 err = build_refs_str(&refs_str, refs,
1764 s->selected_entry->id, s->repo);
1765 if (err)
1766 goto done;
1770 if (s->thread_args.commits_needed == 0)
1771 halfdelay(10); /* disable fast refresh */
1773 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1774 if (asprintf(&ncommits_str, " [%d/%d] %s",
1775 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1776 (view->searching && !view->search_next_done) ?
1777 "searching..." : "loading...") == -1) {
1778 err = got_error_from_errno("asprintf");
1779 goto done;
1781 } else {
1782 const char *search_str = NULL;
1784 if (view->searching) {
1785 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1786 search_str = "no more matches";
1787 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1788 search_str = "no matches found";
1789 else if (!view->search_next_done)
1790 search_str = "searching...";
1793 if (asprintf(&ncommits_str, " [%d/%d] %s",
1794 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1795 search_str ? search_str :
1796 (refs_str ? refs_str : "")) == -1) {
1797 err = got_error_from_errno("asprintf");
1798 goto done;
1802 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1803 if (asprintf(&header, "commit %s %s%s",
1804 id_str ? id_str : "........................................",
1805 s->in_repo_path, ncommits_str) == -1) {
1806 err = got_error_from_errno("asprintf");
1807 header = NULL;
1808 goto done;
1810 } else if (asprintf(&header, "commit %s%s",
1811 id_str ? id_str : "........................................",
1812 ncommits_str) == -1) {
1813 err = got_error_from_errno("asprintf");
1814 header = NULL;
1815 goto done;
1817 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1818 if (err)
1819 goto done;
1821 werase(view->window);
1823 if (view_needs_focus_indication(view))
1824 wstandout(view->window);
1825 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1826 if (tc)
1827 wattr_on(view->window,
1828 COLOR_PAIR(tc->colorpair), NULL);
1829 waddwstr(view->window, wline);
1830 if (tc)
1831 wattr_off(view->window,
1832 COLOR_PAIR(tc->colorpair), NULL);
1833 while (width < view->ncols) {
1834 waddch(view->window, ' ');
1835 width++;
1837 if (view_needs_focus_indication(view))
1838 wstandend(view->window);
1839 free(wline);
1840 if (limit <= 1)
1841 goto done;
1843 /* Grow author column size if necessary, and set view->maxx. */
1844 entry = s->first_displayed_entry;
1845 ncommits = 0;
1846 view->maxx = 0;
1847 while (entry) {
1848 char *author, *eol, *msg, *msg0;
1849 wchar_t *wauthor, *wmsg;
1850 int width;
1851 if (ncommits >= limit - 1)
1852 break;
1853 author = strdup(got_object_commit_get_author(entry->commit));
1854 if (author == NULL) {
1855 err = got_error_from_errno("strdup");
1856 goto done;
1858 err = format_author(&wauthor, &width, author, COLS,
1859 date_display_cols);
1860 if (author_cols < width)
1861 author_cols = width;
1862 free(wauthor);
1863 free(author);
1864 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1865 if (err)
1866 goto done;
1867 msg = msg0;
1868 while (*msg == '\n')
1869 ++msg;
1870 if ((eol = strchr(msg, '\n')))
1871 *eol = '\0';
1872 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1873 date_display_cols + author_cols, 0);
1874 if (err)
1875 goto done;
1876 view->maxx = MAX(view->maxx, width);
1877 free(msg0);
1878 free(wmsg);
1879 ncommits++;
1880 entry = TAILQ_NEXT(entry, entry);
1883 entry = s->first_displayed_entry;
1884 s->last_displayed_entry = s->first_displayed_entry;
1885 ncommits = 0;
1886 while (entry) {
1887 if (ncommits >= limit - 1)
1888 break;
1889 if (ncommits == s->selected)
1890 wstandout(view->window);
1891 err = draw_commit(view, entry->commit, entry->id,
1892 date_display_cols, author_cols);
1893 if (ncommits == s->selected)
1894 wstandend(view->window);
1895 if (err)
1896 goto done;
1897 ncommits++;
1898 s->last_displayed_entry = entry;
1899 entry = TAILQ_NEXT(entry, entry);
1902 view_vborder(view);
1903 done:
1904 free(id_str);
1905 free(refs_str);
1906 free(ncommits_str);
1907 free(header);
1908 return err;
1911 static void
1912 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1914 struct commit_queue_entry *entry;
1915 int nscrolled = 0;
1917 entry = TAILQ_FIRST(&s->commits.head);
1918 if (s->first_displayed_entry == entry)
1919 return;
1921 entry = s->first_displayed_entry;
1922 while (entry && nscrolled < maxscroll) {
1923 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1924 if (entry) {
1925 s->first_displayed_entry = entry;
1926 nscrolled++;
1931 static const struct got_error *
1932 trigger_log_thread(struct tog_view *view, int wait)
1934 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1935 int errcode;
1937 halfdelay(1); /* fast refresh while loading commits */
1939 while (ta->commits_needed > 0 || ta->load_all) {
1940 if (ta->log_complete)
1941 break;
1943 /* Wake the log thread. */
1944 errcode = pthread_cond_signal(&ta->need_commits);
1945 if (errcode)
1946 return got_error_set_errno(errcode,
1947 "pthread_cond_signal");
1950 * The mutex will be released while the view loop waits
1951 * in wgetch(), at which time the log thread will run.
1953 if (!wait)
1954 break;
1956 /* Display progress update in log view. */
1957 show_log_view(view);
1958 update_panels();
1959 doupdate();
1961 /* Wait right here while next commit is being loaded. */
1962 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1963 if (errcode)
1964 return got_error_set_errno(errcode,
1965 "pthread_cond_wait");
1967 /* Display progress update in log view. */
1968 show_log_view(view);
1969 update_panels();
1970 doupdate();
1973 return NULL;
1976 static const struct got_error *
1977 log_scroll_down(struct tog_view *view, int maxscroll)
1979 struct tog_log_view_state *s = &view->state.log;
1980 const struct got_error *err = NULL;
1981 struct commit_queue_entry *pentry;
1982 int nscrolled = 0, ncommits_needed;
1984 if (s->last_displayed_entry == NULL)
1985 return NULL;
1987 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1988 if (s->commits.ncommits < ncommits_needed &&
1989 !s->thread_args.log_complete) {
1991 * Ask the log thread for required amount of commits.
1993 s->thread_args.commits_needed += maxscroll;
1994 err = trigger_log_thread(view, 1);
1995 if (err)
1996 return err;
1999 do {
2000 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2001 if (pentry == NULL)
2002 break;
2004 s->last_displayed_entry = pentry;
2006 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2007 if (pentry == NULL)
2008 break;
2009 s->first_displayed_entry = pentry;
2010 } while (++nscrolled < maxscroll);
2012 return err;
2015 static const struct got_error *
2016 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2017 struct got_commit_object *commit, struct got_object_id *commit_id,
2018 struct tog_view *log_view, struct got_repository *repo)
2020 const struct got_error *err;
2021 struct got_object_qid *parent_id;
2022 struct tog_view *diff_view;
2024 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2025 if (diff_view == NULL)
2026 return got_error_from_errno("view_open");
2028 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2029 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2030 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2031 if (err == NULL)
2032 *new_view = diff_view;
2033 return err;
2036 static const struct got_error *
2037 tree_view_visit_subtree(struct tog_tree_view_state *s,
2038 struct got_tree_object *subtree)
2040 struct tog_parent_tree *parent;
2042 parent = calloc(1, sizeof(*parent));
2043 if (parent == NULL)
2044 return got_error_from_errno("calloc");
2046 parent->tree = s->tree;
2047 parent->first_displayed_entry = s->first_displayed_entry;
2048 parent->selected_entry = s->selected_entry;
2049 parent->selected = s->selected;
2050 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2051 s->tree = subtree;
2052 s->selected = 0;
2053 s->first_displayed_entry = NULL;
2054 return NULL;
2057 static const struct got_error *
2058 tree_view_walk_path(struct tog_tree_view_state *s,
2059 struct got_commit_object *commit, const char *path)
2061 const struct got_error *err = NULL;
2062 struct got_tree_object *tree = NULL;
2063 const char *p;
2064 char *slash, *subpath = NULL;
2066 /* Walk the path and open corresponding tree objects. */
2067 p = path;
2068 while (*p) {
2069 struct got_tree_entry *te;
2070 struct got_object_id *tree_id;
2071 char *te_name;
2073 while (p[0] == '/')
2074 p++;
2076 /* Ensure the correct subtree entry is selected. */
2077 slash = strchr(p, '/');
2078 if (slash == NULL)
2079 te_name = strdup(p);
2080 else
2081 te_name = strndup(p, slash - p);
2082 if (te_name == NULL) {
2083 err = got_error_from_errno("strndup");
2084 break;
2086 te = got_object_tree_find_entry(s->tree, te_name);
2087 if (te == NULL) {
2088 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2089 free(te_name);
2090 break;
2092 free(te_name);
2093 s->first_displayed_entry = s->selected_entry = te;
2095 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2096 break; /* jump to this file's entry */
2098 slash = strchr(p, '/');
2099 if (slash)
2100 subpath = strndup(path, slash - path);
2101 else
2102 subpath = strdup(path);
2103 if (subpath == NULL) {
2104 err = got_error_from_errno("strdup");
2105 break;
2108 err = got_object_id_by_path(&tree_id, s->repo, commit,
2109 subpath);
2110 if (err)
2111 break;
2113 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2114 free(tree_id);
2115 if (err)
2116 break;
2118 err = tree_view_visit_subtree(s, tree);
2119 if (err) {
2120 got_object_tree_close(tree);
2121 break;
2123 if (slash == NULL)
2124 break;
2125 free(subpath);
2126 subpath = NULL;
2127 p = slash;
2130 free(subpath);
2131 return err;
2134 static const struct got_error *
2135 browse_commit_tree(struct tog_view **new_view, int begin_x,
2136 struct commit_queue_entry *entry, const char *path,
2137 const char *head_ref_name, struct got_repository *repo)
2139 const struct got_error *err = NULL;
2140 struct tog_tree_view_state *s;
2141 struct tog_view *tree_view;
2143 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2144 if (tree_view == NULL)
2145 return got_error_from_errno("view_open");
2147 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2148 if (err)
2149 return err;
2150 s = &tree_view->state.tree;
2152 *new_view = tree_view;
2154 if (got_path_is_root_dir(path))
2155 return NULL;
2157 return tree_view_walk_path(s, entry->commit, path);
2160 static const struct got_error *
2161 block_signals_used_by_main_thread(void)
2163 sigset_t sigset;
2164 int errcode;
2166 if (sigemptyset(&sigset) == -1)
2167 return got_error_from_errno("sigemptyset");
2169 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2170 if (sigaddset(&sigset, SIGWINCH) == -1)
2171 return got_error_from_errno("sigaddset");
2172 if (sigaddset(&sigset, SIGCONT) == -1)
2173 return got_error_from_errno("sigaddset");
2174 if (sigaddset(&sigset, SIGINT) == -1)
2175 return got_error_from_errno("sigaddset");
2176 if (sigaddset(&sigset, SIGTERM) == -1)
2177 return got_error_from_errno("sigaddset");
2179 /* ncurses handles SIGTSTP */
2180 if (sigaddset(&sigset, SIGTSTP) == -1)
2181 return got_error_from_errno("sigaddset");
2183 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2184 if (errcode)
2185 return got_error_set_errno(errcode, "pthread_sigmask");
2187 return NULL;
2190 static void *
2191 log_thread(void *arg)
2193 const struct got_error *err = NULL;
2194 int errcode = 0;
2195 struct tog_log_thread_args *a = arg;
2196 int done = 0;
2198 err = block_signals_used_by_main_thread();
2199 if (err)
2200 return (void *)err;
2202 while (!done && !err && !tog_fatal_signal_received()) {
2203 err = queue_commits(a);
2204 if (err) {
2205 if (err->code != GOT_ERR_ITER_COMPLETED)
2206 return (void *)err;
2207 err = NULL;
2208 done = 1;
2209 } else if (a->commits_needed > 0 && !a->load_all)
2210 a->commits_needed--;
2212 errcode = pthread_mutex_lock(&tog_mutex);
2213 if (errcode) {
2214 err = got_error_set_errno(errcode,
2215 "pthread_mutex_lock");
2216 break;
2217 } else if (*a->quit)
2218 done = 1;
2219 else if (*a->first_displayed_entry == NULL) {
2220 *a->first_displayed_entry =
2221 TAILQ_FIRST(&a->commits->head);
2222 *a->selected_entry = *a->first_displayed_entry;
2225 errcode = pthread_cond_signal(&a->commit_loaded);
2226 if (errcode) {
2227 err = got_error_set_errno(errcode,
2228 "pthread_cond_signal");
2229 pthread_mutex_unlock(&tog_mutex);
2230 break;
2233 if (done)
2234 a->commits_needed = 0;
2235 else {
2236 if (a->commits_needed == 0 && !a->load_all) {
2237 errcode = pthread_cond_wait(&a->need_commits,
2238 &tog_mutex);
2239 if (errcode)
2240 err = got_error_set_errno(errcode,
2241 "pthread_cond_wait");
2242 if (*a->quit)
2243 done = 1;
2247 errcode = pthread_mutex_unlock(&tog_mutex);
2248 if (errcode && err == NULL)
2249 err = got_error_set_errno(errcode,
2250 "pthread_mutex_unlock");
2252 a->log_complete = 1;
2253 return (void *)err;
2256 static const struct got_error *
2257 stop_log_thread(struct tog_log_view_state *s)
2259 const struct got_error *err = NULL;
2260 int errcode;
2262 if (s->thread) {
2263 s->quit = 1;
2264 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2265 if (errcode)
2266 return got_error_set_errno(errcode,
2267 "pthread_cond_signal");
2268 errcode = pthread_mutex_unlock(&tog_mutex);
2269 if (errcode)
2270 return got_error_set_errno(errcode,
2271 "pthread_mutex_unlock");
2272 errcode = pthread_join(s->thread, (void **)&err);
2273 if (errcode)
2274 return got_error_set_errno(errcode, "pthread_join");
2275 errcode = pthread_mutex_lock(&tog_mutex);
2276 if (errcode)
2277 return got_error_set_errno(errcode,
2278 "pthread_mutex_lock");
2279 s->thread = NULL;
2282 if (s->thread_args.repo) {
2283 err = got_repo_close(s->thread_args.repo);
2284 s->thread_args.repo = NULL;
2287 if (s->thread_args.pack_fds) {
2288 const struct got_error *pack_err =
2289 got_repo_pack_fds_close(s->thread_args.pack_fds);
2290 if (err == NULL)
2291 err = pack_err;
2292 s->thread_args.pack_fds = NULL;
2295 if (s->thread_args.graph) {
2296 got_commit_graph_close(s->thread_args.graph);
2297 s->thread_args.graph = NULL;
2300 return err;
2303 static const struct got_error *
2304 close_log_view(struct tog_view *view)
2306 const struct got_error *err = NULL;
2307 struct tog_log_view_state *s = &view->state.log;
2308 int errcode;
2310 err = stop_log_thread(s);
2312 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2313 if (errcode && err == NULL)
2314 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2316 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2317 if (errcode && err == NULL)
2318 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2320 free_commits(&s->commits);
2321 free(s->in_repo_path);
2322 s->in_repo_path = NULL;
2323 free(s->start_id);
2324 s->start_id = NULL;
2325 free(s->head_ref_name);
2326 s->head_ref_name = NULL;
2327 return err;
2330 static const struct got_error *
2331 search_start_log_view(struct tog_view *view)
2333 struct tog_log_view_state *s = &view->state.log;
2335 s->matched_entry = NULL;
2336 s->search_entry = NULL;
2337 return NULL;
2340 static const struct got_error *
2341 search_next_log_view(struct tog_view *view)
2343 const struct got_error *err = NULL;
2344 struct tog_log_view_state *s = &view->state.log;
2345 struct commit_queue_entry *entry;
2347 /* Display progress update in log view. */
2348 show_log_view(view);
2349 update_panels();
2350 doupdate();
2352 if (s->search_entry) {
2353 int errcode, ch;
2354 errcode = pthread_mutex_unlock(&tog_mutex);
2355 if (errcode)
2356 return got_error_set_errno(errcode,
2357 "pthread_mutex_unlock");
2358 ch = wgetch(view->window);
2359 errcode = pthread_mutex_lock(&tog_mutex);
2360 if (errcode)
2361 return got_error_set_errno(errcode,
2362 "pthread_mutex_lock");
2363 if (ch == KEY_BACKSPACE) {
2364 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2365 return NULL;
2367 if (view->searching == TOG_SEARCH_FORWARD)
2368 entry = TAILQ_NEXT(s->search_entry, entry);
2369 else
2370 entry = TAILQ_PREV(s->search_entry,
2371 commit_queue_head, entry);
2372 } else if (s->matched_entry) {
2373 if (view->searching == TOG_SEARCH_FORWARD)
2374 entry = TAILQ_NEXT(s->matched_entry, entry);
2375 else
2376 entry = TAILQ_PREV(s->matched_entry,
2377 commit_queue_head, entry);
2378 } else {
2379 entry = s->selected_entry;
2382 while (1) {
2383 int have_match = 0;
2385 if (entry == NULL) {
2386 if (s->thread_args.log_complete ||
2387 view->searching == TOG_SEARCH_BACKWARD) {
2388 view->search_next_done =
2389 (s->matched_entry == NULL ?
2390 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2391 s->search_entry = NULL;
2392 return NULL;
2395 * Poke the log thread for more commits and return,
2396 * allowing the main loop to make progress. Search
2397 * will resume at s->search_entry once we come back.
2399 s->thread_args.commits_needed++;
2400 return trigger_log_thread(view, 0);
2403 err = match_commit(&have_match, entry->id, entry->commit,
2404 &view->regex);
2405 if (err)
2406 break;
2407 if (have_match) {
2408 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2409 s->matched_entry = entry;
2410 break;
2413 s->search_entry = entry;
2414 if (view->searching == TOG_SEARCH_FORWARD)
2415 entry = TAILQ_NEXT(entry, entry);
2416 else
2417 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2420 if (s->matched_entry) {
2421 int cur = s->selected_entry->idx;
2422 while (cur < s->matched_entry->idx) {
2423 err = input_log_view(NULL, view, KEY_DOWN);
2424 if (err)
2425 return err;
2426 cur++;
2428 while (cur > s->matched_entry->idx) {
2429 err = input_log_view(NULL, view, KEY_UP);
2430 if (err)
2431 return err;
2432 cur--;
2436 s->search_entry = NULL;
2438 return NULL;
2441 static const struct got_error *
2442 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2443 struct got_repository *repo, const char *head_ref_name,
2444 const char *in_repo_path, int log_branches)
2446 const struct got_error *err = NULL;
2447 struct tog_log_view_state *s = &view->state.log;
2448 struct got_repository *thread_repo = NULL;
2449 struct got_commit_graph *thread_graph = NULL;
2450 int errcode;
2452 if (in_repo_path != s->in_repo_path) {
2453 free(s->in_repo_path);
2454 s->in_repo_path = strdup(in_repo_path);
2455 if (s->in_repo_path == NULL)
2456 return got_error_from_errno("strdup");
2459 /* The commit queue only contains commits being displayed. */
2460 TAILQ_INIT(&s->commits.head);
2461 s->commits.ncommits = 0;
2463 s->repo = repo;
2464 if (head_ref_name) {
2465 s->head_ref_name = strdup(head_ref_name);
2466 if (s->head_ref_name == NULL) {
2467 err = got_error_from_errno("strdup");
2468 goto done;
2471 s->start_id = got_object_id_dup(start_id);
2472 if (s->start_id == NULL) {
2473 err = got_error_from_errno("got_object_id_dup");
2474 goto done;
2476 s->log_branches = log_branches;
2478 STAILQ_INIT(&s->colors);
2479 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2480 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2481 get_color_value("TOG_COLOR_COMMIT"));
2482 if (err)
2483 goto done;
2484 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2485 get_color_value("TOG_COLOR_AUTHOR"));
2486 if (err) {
2487 free_colors(&s->colors);
2488 goto done;
2490 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2491 get_color_value("TOG_COLOR_DATE"));
2492 if (err) {
2493 free_colors(&s->colors);
2494 goto done;
2498 view->show = show_log_view;
2499 view->input = input_log_view;
2500 view->close = close_log_view;
2501 view->search_start = search_start_log_view;
2502 view->search_next = search_next_log_view;
2504 if (s->thread_args.pack_fds == NULL) {
2505 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2506 if (err)
2507 goto done;
2509 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2510 s->thread_args.pack_fds);
2511 if (err)
2512 goto done;
2513 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2514 !s->log_branches);
2515 if (err)
2516 goto done;
2517 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2518 s->repo, NULL, NULL);
2519 if (err)
2520 goto done;
2522 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2523 if (errcode) {
2524 err = got_error_set_errno(errcode, "pthread_cond_init");
2525 goto done;
2527 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2528 if (errcode) {
2529 err = got_error_set_errno(errcode, "pthread_cond_init");
2530 goto done;
2533 s->thread_args.commits_needed = view->nlines;
2534 s->thread_args.graph = thread_graph;
2535 s->thread_args.commits = &s->commits;
2536 s->thread_args.in_repo_path = s->in_repo_path;
2537 s->thread_args.start_id = s->start_id;
2538 s->thread_args.repo = thread_repo;
2539 s->thread_args.log_complete = 0;
2540 s->thread_args.quit = &s->quit;
2541 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2542 s->thread_args.selected_entry = &s->selected_entry;
2543 s->thread_args.searching = &view->searching;
2544 s->thread_args.search_next_done = &view->search_next_done;
2545 s->thread_args.regex = &view->regex;
2546 done:
2547 if (err)
2548 close_log_view(view);
2549 return err;
2552 static const struct got_error *
2553 show_log_view(struct tog_view *view)
2555 const struct got_error *err;
2556 struct tog_log_view_state *s = &view->state.log;
2558 if (s->thread == NULL) {
2559 int errcode = pthread_create(&s->thread, NULL, log_thread,
2560 &s->thread_args);
2561 if (errcode)
2562 return got_error_set_errno(errcode, "pthread_create");
2563 if (s->thread_args.commits_needed > 0) {
2564 err = trigger_log_thread(view, 1);
2565 if (err)
2566 return err;
2570 return draw_commits(view);
2573 static const struct got_error *
2574 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2576 const struct got_error *err = NULL;
2577 struct tog_log_view_state *s = &view->state.log;
2578 struct tog_view *diff_view = NULL, *tree_view = NULL;
2579 struct tog_view *ref_view = NULL;
2580 struct commit_queue_entry *entry;
2581 int begin_x = 0, n, nscroll = view->nlines - 1;
2583 if (s->thread_args.load_all) {
2584 if (ch == KEY_BACKSPACE)
2585 s->thread_args.load_all = 0;
2586 else if (s->thread_args.log_complete) {
2587 s->thread_args.load_all = 0;
2588 log_scroll_down(view, s->commits.ncommits);
2589 s->selected = MIN(view->nlines - 2,
2590 s->commits.ncommits - 1);
2591 select_commit(s);
2593 return NULL;
2596 switch (ch) {
2597 case 'q':
2598 s->quit = 1;
2599 break;
2600 case '0':
2601 view->x = 0;
2602 break;
2603 case '$':
2604 view->x = MAX(view->maxx - view->ncols / 2, 0);
2605 break;
2606 case KEY_RIGHT:
2607 case 'l':
2608 if (view->x + view->ncols / 2 < view->maxx)
2609 view->x += 2; /* move two columns right */
2610 break;
2611 case KEY_LEFT:
2612 case 'h':
2613 view->x -= MIN(view->x, 2); /* move two columns back */
2614 break;
2615 case 'k':
2616 case KEY_UP:
2617 case '<':
2618 case ',':
2619 case CTRL('p'):
2620 if (s->first_displayed_entry == NULL)
2621 break;
2622 if (s->selected > 0)
2623 s->selected--;
2624 else
2625 log_scroll_up(s, 1);
2626 select_commit(s);
2627 break;
2628 case 'g':
2629 case KEY_HOME:
2630 s->selected = 0;
2631 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2632 select_commit(s);
2633 break;
2634 case CTRL('u'):
2635 case 'u':
2636 nscroll /= 2;
2637 /* FALL THROUGH */
2638 case KEY_PPAGE:
2639 case CTRL('b'):
2640 if (s->first_displayed_entry == NULL)
2641 break;
2642 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2643 s->selected = MAX(0, s->selected - nscroll - 1);
2644 else
2645 log_scroll_up(s, nscroll);
2646 select_commit(s);
2647 break;
2648 case 'j':
2649 case KEY_DOWN:
2650 case '>':
2651 case '.':
2652 case CTRL('n'):
2653 if (s->first_displayed_entry == NULL)
2654 break;
2655 if (s->selected < MIN(view->nlines - 2,
2656 s->commits.ncommits - 1))
2657 s->selected++;
2658 else {
2659 err = log_scroll_down(view, 1);
2660 if (err)
2661 break;
2663 select_commit(s);
2664 break;
2665 case 'G':
2666 case KEY_END: {
2667 /* We don't know yet how many commits, so we're forced to
2668 * traverse them all. */
2669 if (!s->thread_args.log_complete) {
2670 s->thread_args.load_all = 1;
2671 return trigger_log_thread(view, 0);
2674 s->selected = 0;
2675 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2676 for (n = 0; n < view->nlines - 1; n++) {
2677 if (entry == NULL)
2678 break;
2679 s->first_displayed_entry = entry;
2680 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2682 if (n > 0)
2683 s->selected = n - 1;
2684 select_commit(s);
2685 break;
2687 case CTRL('d'):
2688 case 'd':
2689 nscroll /= 2;
2690 /* FALL THROUGH */
2691 case KEY_NPAGE:
2692 case CTRL('f'): {
2693 struct commit_queue_entry *first;
2694 first = s->first_displayed_entry;
2695 if (first == NULL)
2696 break;
2697 err = log_scroll_down(view, nscroll);
2698 if (err)
2699 break;
2700 if (first == s->first_displayed_entry &&
2701 s->selected < MIN(view->nlines - 2,
2702 s->commits.ncommits - 1)) {
2703 /* can't scroll further down */
2704 s->selected += MIN(s->last_displayed_entry->idx -
2705 s->selected_entry->idx, nscroll + 1);
2707 select_commit(s);
2708 break;
2710 case KEY_RESIZE:
2711 if (s->selected > view->nlines - 2)
2712 s->selected = view->nlines - 2;
2713 if (s->selected > s->commits.ncommits - 1)
2714 s->selected = s->commits.ncommits - 1;
2715 select_commit(s);
2716 if (s->commits.ncommits < view->nlines - 1 &&
2717 !s->thread_args.log_complete) {
2718 s->thread_args.commits_needed += (view->nlines - 1) -
2719 s->commits.ncommits;
2720 err = trigger_log_thread(view, 1);
2722 break;
2723 case KEY_ENTER:
2724 case ' ':
2725 case '\r':
2726 if (s->selected_entry == NULL)
2727 break;
2728 if (view_is_parent_view(view))
2729 begin_x = view_split_begin_x(view->begin_x);
2730 err = open_diff_view_for_commit(&diff_view, begin_x,
2731 s->selected_entry->commit, s->selected_entry->id,
2732 view, s->repo);
2733 if (err)
2734 break;
2735 view->focussed = 0;
2736 diff_view->focussed = 1;
2737 if (view_is_parent_view(view)) {
2738 err = view_close_child(view);
2739 if (err)
2740 return err;
2741 view_set_child(view, diff_view);
2742 view->focus_child = 1;
2743 } else
2744 *new_view = diff_view;
2745 break;
2746 case 't':
2747 if (s->selected_entry == NULL)
2748 break;
2749 if (view_is_parent_view(view))
2750 begin_x = view_split_begin_x(view->begin_x);
2751 err = browse_commit_tree(&tree_view, begin_x,
2752 s->selected_entry, s->in_repo_path, s->head_ref_name,
2753 s->repo);
2754 if (err)
2755 break;
2756 view->focussed = 0;
2757 tree_view->focussed = 1;
2758 if (view_is_parent_view(view)) {
2759 err = view_close_child(view);
2760 if (err)
2761 return err;
2762 view_set_child(view, tree_view);
2763 view->focus_child = 1;
2764 } else
2765 *new_view = tree_view;
2766 break;
2767 case KEY_BACKSPACE:
2768 case CTRL('l'):
2769 case 'B':
2770 if (ch == KEY_BACKSPACE &&
2771 got_path_is_root_dir(s->in_repo_path))
2772 break;
2773 err = stop_log_thread(s);
2774 if (err)
2775 return err;
2776 if (ch == KEY_BACKSPACE) {
2777 char *parent_path;
2778 err = got_path_dirname(&parent_path, s->in_repo_path);
2779 if (err)
2780 return err;
2781 free(s->in_repo_path);
2782 s->in_repo_path = parent_path;
2783 s->thread_args.in_repo_path = s->in_repo_path;
2784 } else if (ch == CTRL('l')) {
2785 struct got_object_id *start_id;
2786 err = got_repo_match_object_id(&start_id, NULL,
2787 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2788 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2789 if (err)
2790 return err;
2791 free(s->start_id);
2792 s->start_id = start_id;
2793 s->thread_args.start_id = s->start_id;
2794 } else /* 'B' */
2795 s->log_branches = !s->log_branches;
2797 if (s->thread_args.pack_fds == NULL) {
2798 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2799 if (err)
2800 return err;
2802 err = got_repo_open(&s->thread_args.repo,
2803 got_repo_get_path(s->repo), NULL,
2804 s->thread_args.pack_fds);
2805 if (err)
2806 return err;
2807 tog_free_refs();
2808 err = tog_load_refs(s->repo, 0);
2809 if (err)
2810 return err;
2811 err = got_commit_graph_open(&s->thread_args.graph,
2812 s->in_repo_path, !s->log_branches);
2813 if (err)
2814 return err;
2815 err = got_commit_graph_iter_start(s->thread_args.graph,
2816 s->start_id, s->repo, NULL, NULL);
2817 if (err)
2818 return err;
2819 free_commits(&s->commits);
2820 s->first_displayed_entry = NULL;
2821 s->last_displayed_entry = NULL;
2822 s->selected_entry = NULL;
2823 s->selected = 0;
2824 s->thread_args.log_complete = 0;
2825 s->quit = 0;
2826 s->thread_args.commits_needed = view->nlines;
2827 break;
2828 case 'r':
2829 if (view_is_parent_view(view))
2830 begin_x = view_split_begin_x(view->begin_x);
2831 ref_view = view_open(view->nlines, view->ncols,
2832 view->begin_y, begin_x, TOG_VIEW_REF);
2833 if (ref_view == NULL)
2834 return got_error_from_errno("view_open");
2835 err = open_ref_view(ref_view, s->repo);
2836 if (err) {
2837 view_close(ref_view);
2838 return err;
2840 view->focussed = 0;
2841 ref_view->focussed = 1;
2842 if (view_is_parent_view(view)) {
2843 err = view_close_child(view);
2844 if (err)
2845 return err;
2846 view_set_child(view, ref_view);
2847 view->focus_child = 1;
2848 } else
2849 *new_view = ref_view;
2850 break;
2851 default:
2852 break;
2855 return err;
2858 static const struct got_error *
2859 apply_unveil(const char *repo_path, const char *worktree_path)
2861 const struct got_error *error;
2863 #ifdef PROFILE
2864 if (unveil("gmon.out", "rwc") != 0)
2865 return got_error_from_errno2("unveil", "gmon.out");
2866 #endif
2867 if (repo_path && unveil(repo_path, "r") != 0)
2868 return got_error_from_errno2("unveil", repo_path);
2870 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2871 return got_error_from_errno2("unveil", worktree_path);
2873 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2874 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2876 error = got_privsep_unveil_exec_helpers();
2877 if (error != NULL)
2878 return error;
2880 if (unveil(NULL, NULL) != 0)
2881 return got_error_from_errno("unveil");
2883 return NULL;
2886 static void
2887 init_curses(void)
2890 * Override default signal handlers before starting ncurses.
2891 * This should prevent ncurses from installing its own
2892 * broken cleanup() signal handler.
2894 signal(SIGWINCH, tog_sigwinch);
2895 signal(SIGPIPE, tog_sigpipe);
2896 signal(SIGCONT, tog_sigcont);
2897 signal(SIGINT, tog_sigint);
2898 signal(SIGTERM, tog_sigterm);
2900 initscr();
2901 cbreak();
2902 halfdelay(1); /* Do fast refresh while initial view is loading. */
2903 noecho();
2904 nonl();
2905 intrflush(stdscr, FALSE);
2906 keypad(stdscr, TRUE);
2907 curs_set(0);
2908 if (getenv("TOG_COLORS") != NULL) {
2909 start_color();
2910 use_default_colors();
2914 static const struct got_error *
2915 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2916 struct got_repository *repo, struct got_worktree *worktree)
2918 const struct got_error *err = NULL;
2920 if (argc == 0) {
2921 *in_repo_path = strdup("/");
2922 if (*in_repo_path == NULL)
2923 return got_error_from_errno("strdup");
2924 return NULL;
2927 if (worktree) {
2928 const char *prefix = got_worktree_get_path_prefix(worktree);
2929 char *p;
2931 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2932 if (err)
2933 return err;
2934 if (asprintf(in_repo_path, "%s%s%s", prefix,
2935 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2936 p) == -1) {
2937 err = got_error_from_errno("asprintf");
2938 *in_repo_path = NULL;
2940 free(p);
2941 } else
2942 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2944 return err;
2947 static const struct got_error *
2948 cmd_log(int argc, char *argv[])
2950 const struct got_error *error;
2951 struct got_repository *repo = NULL;
2952 struct got_worktree *worktree = NULL;
2953 struct got_object_id *start_id = NULL;
2954 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2955 char *start_commit = NULL, *label = NULL;
2956 struct got_reference *ref = NULL;
2957 const char *head_ref_name = NULL;
2958 int ch, log_branches = 0;
2959 struct tog_view *view;
2960 int *pack_fds = NULL;
2962 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2963 switch (ch) {
2964 case 'b':
2965 log_branches = 1;
2966 break;
2967 case 'c':
2968 start_commit = optarg;
2969 break;
2970 case 'r':
2971 repo_path = realpath(optarg, NULL);
2972 if (repo_path == NULL)
2973 return got_error_from_errno2("realpath",
2974 optarg);
2975 break;
2976 default:
2977 usage_log();
2978 /* NOTREACHED */
2982 argc -= optind;
2983 argv += optind;
2985 if (argc > 1)
2986 usage_log();
2988 error = got_repo_pack_fds_open(&pack_fds);
2989 if (error != NULL)
2990 goto done;
2992 if (repo_path == NULL) {
2993 cwd = getcwd(NULL, 0);
2994 if (cwd == NULL)
2995 return got_error_from_errno("getcwd");
2996 error = got_worktree_open(&worktree, cwd);
2997 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2998 goto done;
2999 if (worktree)
3000 repo_path =
3001 strdup(got_worktree_get_repo_path(worktree));
3002 else
3003 repo_path = strdup(cwd);
3004 if (repo_path == NULL) {
3005 error = got_error_from_errno("strdup");
3006 goto done;
3010 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3011 if (error != NULL)
3012 goto done;
3014 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3015 repo, worktree);
3016 if (error)
3017 goto done;
3019 init_curses();
3021 error = apply_unveil(got_repo_get_path(repo),
3022 worktree ? got_worktree_get_root_path(worktree) : NULL);
3023 if (error)
3024 goto done;
3026 /* already loaded by tog_log_with_path()? */
3027 if (TAILQ_EMPTY(&tog_refs)) {
3028 error = tog_load_refs(repo, 0);
3029 if (error)
3030 goto done;
3033 if (start_commit == NULL) {
3034 error = got_repo_match_object_id(&start_id, &label,
3035 worktree ? got_worktree_get_head_ref_name(worktree) :
3036 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3037 if (error)
3038 goto done;
3039 head_ref_name = label;
3040 } else {
3041 error = got_ref_open(&ref, repo, start_commit, 0);
3042 if (error == NULL)
3043 head_ref_name = got_ref_get_name(ref);
3044 else if (error->code != GOT_ERR_NOT_REF)
3045 goto done;
3046 error = got_repo_match_object_id(&start_id, NULL,
3047 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3048 if (error)
3049 goto done;
3052 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3053 if (view == NULL) {
3054 error = got_error_from_errno("view_open");
3055 goto done;
3057 error = open_log_view(view, start_id, repo, head_ref_name,
3058 in_repo_path, log_branches);
3059 if (error)
3060 goto done;
3061 if (worktree) {
3062 /* Release work tree lock. */
3063 got_worktree_close(worktree);
3064 worktree = NULL;
3066 error = view_loop(view);
3067 done:
3068 free(in_repo_path);
3069 free(repo_path);
3070 free(cwd);
3071 free(start_id);
3072 free(label);
3073 if (ref)
3074 got_ref_close(ref);
3075 if (repo) {
3076 const struct got_error *close_err = got_repo_close(repo);
3077 if (error == NULL)
3078 error = close_err;
3080 if (worktree)
3081 got_worktree_close(worktree);
3082 if (pack_fds) {
3083 const struct got_error *pack_err =
3084 got_repo_pack_fds_close(pack_fds);
3085 if (error == NULL)
3086 error = pack_err;
3088 tog_free_refs();
3089 return error;
3092 __dead static void
3093 usage_diff(void)
3095 endwin();
3096 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3097 "[-w] object1 object2\n", getprogname());
3098 exit(1);
3101 static int
3102 match_line(const char *line, regex_t *regex, size_t nmatch,
3103 regmatch_t *regmatch)
3105 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3108 struct tog_color *
3109 match_color(struct tog_colors *colors, const char *line)
3111 struct tog_color *tc = NULL;
3113 STAILQ_FOREACH(tc, colors, entry) {
3114 if (match_line(line, &tc->regex, 0, NULL))
3115 return tc;
3118 return NULL;
3121 static const struct got_error *
3122 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3123 WINDOW *window, int skip, regmatch_t *regmatch)
3125 const struct got_error *err = NULL;
3126 wchar_t *wline;
3127 int rme, rms, n, width;
3129 *wtotal = 0;
3130 rms = regmatch->rm_so;
3131 rme = regmatch->rm_eo;
3133 err = format_line(&wline, &width, NULL, line, 0, wlimit + skip,
3134 col_tab_align, 1);
3135 if (err)
3136 return err;
3138 /* draw up to matched token if we haven't scrolled past it */
3139 n = MAX(rms - skip, 0);
3140 if (n) {
3141 waddnwstr(window, wline + skip, n);
3142 wlimit -= n;
3143 *wtotal += n;
3146 if (wlimit > 0) {
3147 int len = rme - rms;
3148 n = 0;
3149 if (skip > rms) {
3150 n = skip - rms;
3151 len = MAX(len - n, 0);
3153 /* draw (visible part of) matched token (if scrolled into it) */
3154 if (len) {
3155 wattron(window, A_STANDOUT);
3156 waddnwstr(window, wline + rms + n, len);
3157 wattroff(window, A_STANDOUT);
3158 wlimit -= len;
3159 *wtotal += len;
3163 if (wlimit > 0 && skip < width) { /* draw rest of line */
3164 n = 0;
3165 if (skip > rme)
3166 n = MIN(skip - rme, width - rme);
3167 waddnwstr(window, wline + rme + n, wlimit);
3170 *wtotal = width;
3171 free(wline);
3172 return NULL;
3175 static const struct got_error *
3176 draw_file(struct tog_view *view, const char *header)
3178 struct tog_diff_view_state *s = &view->state.diff;
3179 regmatch_t *regmatch = &view->regmatch;
3180 const struct got_error *err;
3181 int nprinted = 0;
3182 char *line;
3183 size_t linesize = 0;
3184 ssize_t linelen;
3185 struct tog_color *tc;
3186 wchar_t *wline;
3187 int width;
3188 int max_lines = view->nlines;
3189 int nlines = s->nlines;
3190 off_t line_offset;
3192 line_offset = s->line_offsets[s->first_displayed_line - 1];
3193 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3194 return got_error_from_errno("fseek");
3196 werase(view->window);
3198 if (header) {
3199 if (asprintf(&line, "[%d/%d] %s",
3200 s->first_displayed_line - 1 + s->selected_line, nlines,
3201 header) == -1)
3202 return got_error_from_errno("asprintf");
3203 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3204 0, 0);
3205 free(line);
3206 if (err)
3207 return err;
3209 if (view_needs_focus_indication(view))
3210 wstandout(view->window);
3211 waddwstr(view->window, wline);
3212 free(wline);
3213 wline = NULL;
3214 if (view_needs_focus_indication(view))
3215 wstandend(view->window);
3216 if (width <= view->ncols - 1)
3217 waddch(view->window, '\n');
3219 if (max_lines <= 1)
3220 return NULL;
3221 max_lines--;
3224 s->eof = 0;
3225 view->maxx = 0;
3226 line = NULL;
3227 while (max_lines > 0 && nprinted < max_lines) {
3228 linelen = getline(&line, &linesize, s->f);
3229 if (linelen == -1) {
3230 if (feof(s->f)) {
3231 s->eof = 1;
3232 break;
3234 free(line);
3235 return got_ferror(s->f, GOT_ERR_IO);
3238 view->maxx = MAX(view->maxx, linelen);
3240 tc = match_color(&s->colors, line);
3241 if (tc)
3242 wattr_on(view->window,
3243 COLOR_PAIR(tc->colorpair), NULL);
3244 if (s->first_displayed_line + nprinted == s->matched_line &&
3245 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3246 err = add_matched_line(&width, line, view->ncols, 0,
3247 view->window, view->x, regmatch);
3248 if (err) {
3249 free(line);
3250 return err;
3252 } else {
3253 err = format_line(&wline, &width, NULL, line, 0,
3254 view->x + view->ncols, 0, view->x ? 1 : 0);
3255 if (err) {
3256 free(line);
3257 return err;
3259 if (view->x < width)
3260 waddwstr(view->window, wline + view->x);
3261 free(wline);
3262 wline = NULL;
3264 if (tc)
3265 wattr_off(view->window,
3266 COLOR_PAIR(tc->colorpair), NULL);
3267 if (width - view->x <= view->ncols - 1)
3268 waddch(view->window, '\n');
3269 nprinted++;
3271 free(line);
3272 if (nprinted >= 1)
3273 s->last_displayed_line = s->first_displayed_line +
3274 (nprinted - 1);
3275 else
3276 s->last_displayed_line = s->first_displayed_line;
3278 view_vborder(view);
3280 if (s->eof) {
3281 while (nprinted < view->nlines) {
3282 waddch(view->window, '\n');
3283 nprinted++;
3286 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3287 view->ncols, 0, 0);
3288 if (err) {
3289 return err;
3292 wstandout(view->window);
3293 waddwstr(view->window, wline);
3294 free(wline);
3295 wline = NULL;
3296 wstandend(view->window);
3299 return NULL;
3302 static char *
3303 get_datestr(time_t *time, char *datebuf)
3305 struct tm mytm, *tm;
3306 char *p, *s;
3308 tm = gmtime_r(time, &mytm);
3309 if (tm == NULL)
3310 return NULL;
3311 s = asctime_r(tm, datebuf);
3312 if (s == NULL)
3313 return NULL;
3314 p = strchr(s, '\n');
3315 if (p)
3316 *p = '\0';
3317 return s;
3320 static const struct got_error *
3321 get_changed_paths(struct got_pathlist_head *paths,
3322 struct got_commit_object *commit, struct got_repository *repo)
3324 const struct got_error *err = NULL;
3325 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3326 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3327 struct got_object_qid *qid;
3329 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3330 if (qid != NULL) {
3331 struct got_commit_object *pcommit;
3332 err = got_object_open_as_commit(&pcommit, repo,
3333 &qid->id);
3334 if (err)
3335 return err;
3337 tree_id1 = got_object_id_dup(
3338 got_object_commit_get_tree_id(pcommit));
3339 if (tree_id1 == NULL) {
3340 got_object_commit_close(pcommit);
3341 return got_error_from_errno("got_object_id_dup");
3343 got_object_commit_close(pcommit);
3347 if (tree_id1) {
3348 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3349 if (err)
3350 goto done;
3353 tree_id2 = got_object_commit_get_tree_id(commit);
3354 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3355 if (err)
3356 goto done;
3358 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3359 got_diff_tree_collect_changed_paths, paths, 0);
3360 done:
3361 if (tree1)
3362 got_object_tree_close(tree1);
3363 if (tree2)
3364 got_object_tree_close(tree2);
3365 free(tree_id1);
3366 return err;
3369 static const struct got_error *
3370 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3372 off_t *p;
3374 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3375 if (p == NULL)
3376 return got_error_from_errno("reallocarray");
3377 *line_offsets = p;
3378 (*line_offsets)[*nlines] = off;
3379 (*nlines)++;
3380 return NULL;
3383 static const struct got_error *
3384 write_commit_info(off_t **line_offsets, size_t *nlines,
3385 struct got_object_id *commit_id, struct got_reflist_head *refs,
3386 struct got_repository *repo, FILE *outfile)
3388 const struct got_error *err = NULL;
3389 char datebuf[26], *datestr;
3390 struct got_commit_object *commit;
3391 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3392 time_t committer_time;
3393 const char *author, *committer;
3394 char *refs_str = NULL;
3395 struct got_pathlist_head changed_paths;
3396 struct got_pathlist_entry *pe;
3397 off_t outoff = 0;
3398 int n;
3400 TAILQ_INIT(&changed_paths);
3402 if (refs) {
3403 err = build_refs_str(&refs_str, refs, commit_id, repo);
3404 if (err)
3405 return err;
3408 err = got_object_open_as_commit(&commit, repo, commit_id);
3409 if (err)
3410 return err;
3412 err = got_object_id_str(&id_str, commit_id);
3413 if (err) {
3414 err = got_error_from_errno("got_object_id_str");
3415 goto done;
3418 err = add_line_offset(line_offsets, nlines, 0);
3419 if (err)
3420 goto done;
3422 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3423 refs_str ? refs_str : "", refs_str ? ")" : "");
3424 if (n < 0) {
3425 err = got_error_from_errno("fprintf");
3426 goto done;
3428 outoff += n;
3429 err = add_line_offset(line_offsets, nlines, outoff);
3430 if (err)
3431 goto done;
3433 n = fprintf(outfile, "from: %s\n",
3434 got_object_commit_get_author(commit));
3435 if (n < 0) {
3436 err = got_error_from_errno("fprintf");
3437 goto done;
3439 outoff += n;
3440 err = add_line_offset(line_offsets, nlines, outoff);
3441 if (err)
3442 goto done;
3444 committer_time = got_object_commit_get_committer_time(commit);
3445 datestr = get_datestr(&committer_time, datebuf);
3446 if (datestr) {
3447 n = fprintf(outfile, "date: %s UTC\n", datestr);
3448 if (n < 0) {
3449 err = got_error_from_errno("fprintf");
3450 goto done;
3452 outoff += n;
3453 err = add_line_offset(line_offsets, nlines, outoff);
3454 if (err)
3455 goto done;
3457 author = got_object_commit_get_author(commit);
3458 committer = got_object_commit_get_committer(commit);
3459 if (strcmp(author, committer) != 0) {
3460 n = fprintf(outfile, "via: %s\n", committer);
3461 if (n < 0) {
3462 err = got_error_from_errno("fprintf");
3463 goto done;
3465 outoff += n;
3466 err = add_line_offset(line_offsets, nlines, outoff);
3467 if (err)
3468 goto done;
3470 if (got_object_commit_get_nparents(commit) > 1) {
3471 const struct got_object_id_queue *parent_ids;
3472 struct got_object_qid *qid;
3473 int pn = 1;
3474 parent_ids = got_object_commit_get_parent_ids(commit);
3475 STAILQ_FOREACH(qid, parent_ids, entry) {
3476 err = got_object_id_str(&id_str, &qid->id);
3477 if (err)
3478 goto done;
3479 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3480 if (n < 0) {
3481 err = got_error_from_errno("fprintf");
3482 goto done;
3484 outoff += n;
3485 err = add_line_offset(line_offsets, nlines, outoff);
3486 if (err)
3487 goto done;
3488 free(id_str);
3489 id_str = NULL;
3493 err = got_object_commit_get_logmsg(&logmsg, commit);
3494 if (err)
3495 goto done;
3496 s = logmsg;
3497 while ((line = strsep(&s, "\n")) != NULL) {
3498 n = fprintf(outfile, "%s\n", line);
3499 if (n < 0) {
3500 err = got_error_from_errno("fprintf");
3501 goto done;
3503 outoff += n;
3504 err = add_line_offset(line_offsets, nlines, outoff);
3505 if (err)
3506 goto done;
3509 err = get_changed_paths(&changed_paths, commit, repo);
3510 if (err)
3511 goto done;
3512 TAILQ_FOREACH(pe, &changed_paths, entry) {
3513 struct got_diff_changed_path *cp = pe->data;
3514 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3515 if (n < 0) {
3516 err = got_error_from_errno("fprintf");
3517 goto done;
3519 outoff += n;
3520 err = add_line_offset(line_offsets, nlines, outoff);
3521 if (err)
3522 goto done;
3523 free((char *)pe->path);
3524 free(pe->data);
3527 fputc('\n', outfile);
3528 outoff++;
3529 err = add_line_offset(line_offsets, nlines, outoff);
3530 done:
3531 got_pathlist_free(&changed_paths);
3532 free(id_str);
3533 free(logmsg);
3534 free(refs_str);
3535 got_object_commit_close(commit);
3536 if (err) {
3537 free(*line_offsets);
3538 *line_offsets = NULL;
3539 *nlines = 0;
3541 return err;
3544 static const struct got_error *
3545 create_diff(struct tog_diff_view_state *s)
3547 const struct got_error *err = NULL;
3548 FILE *f = NULL;
3549 int obj_type;
3551 free(s->line_offsets);
3552 s->line_offsets = malloc(sizeof(off_t));
3553 if (s->line_offsets == NULL)
3554 return got_error_from_errno("malloc");
3555 s->nlines = 0;
3557 f = got_opentemp();
3558 if (f == NULL) {
3559 err = got_error_from_errno("got_opentemp");
3560 goto done;
3562 if (s->f && fclose(s->f) == EOF) {
3563 err = got_error_from_errno("fclose");
3564 goto done;
3566 s->f = f;
3568 if (s->id1)
3569 err = got_object_get_type(&obj_type, s->repo, s->id1);
3570 else
3571 err = got_object_get_type(&obj_type, s->repo, s->id2);
3572 if (err)
3573 goto done;
3575 switch (obj_type) {
3576 case GOT_OBJ_TYPE_BLOB:
3577 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3578 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3579 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3580 s->repo, s->f);
3581 break;
3582 case GOT_OBJ_TYPE_TREE:
3583 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3584 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3585 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3586 break;
3587 case GOT_OBJ_TYPE_COMMIT: {
3588 const struct got_object_id_queue *parent_ids;
3589 struct got_object_qid *pid;
3590 struct got_commit_object *commit2;
3591 struct got_reflist_head *refs;
3593 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3594 if (err)
3595 goto done;
3596 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3597 /* Show commit info if we're diffing to a parent/root commit. */
3598 if (s->id1 == NULL) {
3599 err = write_commit_info(&s->line_offsets, &s->nlines,
3600 s->id2, refs, s->repo, s->f);
3601 if (err)
3602 goto done;
3603 } else {
3604 parent_ids = got_object_commit_get_parent_ids(commit2);
3605 STAILQ_FOREACH(pid, parent_ids, entry) {
3606 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3607 err = write_commit_info(
3608 &s->line_offsets, &s->nlines,
3609 s->id2, refs, s->repo, s->f);
3610 if (err)
3611 goto done;
3612 break;
3616 got_object_commit_close(commit2);
3618 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3619 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3620 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3621 break;
3623 default:
3624 err = got_error(GOT_ERR_OBJ_TYPE);
3625 break;
3627 if (err)
3628 goto done;
3629 done:
3630 if (s->f && fflush(s->f) != 0 && err == NULL)
3631 err = got_error_from_errno("fflush");
3632 return err;
3635 static void
3636 diff_view_indicate_progress(struct tog_view *view)
3638 mvwaddstr(view->window, 0, 0, "diffing...");
3639 update_panels();
3640 doupdate();
3643 static const struct got_error *
3644 search_start_diff_view(struct tog_view *view)
3646 struct tog_diff_view_state *s = &view->state.diff;
3648 s->matched_line = 0;
3649 return NULL;
3652 static const struct got_error *
3653 search_next_diff_view(struct tog_view *view)
3655 struct tog_diff_view_state *s = &view->state.diff;
3656 const struct got_error *err = NULL;
3657 int lineno;
3658 char *exstr = NULL, *line = NULL;
3659 size_t linesize = 0;
3660 ssize_t linelen;
3662 if (!view->searching) {
3663 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3664 return NULL;
3667 if (s->matched_line) {
3668 if (view->searching == TOG_SEARCH_FORWARD)
3669 lineno = s->matched_line + 1;
3670 else
3671 lineno = s->matched_line - 1;
3672 } else
3673 lineno = s->first_displayed_line;
3675 while (1) {
3676 off_t offset;
3678 if (lineno <= 0 || lineno > s->nlines) {
3679 if (s->matched_line == 0) {
3680 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3681 break;
3684 if (view->searching == TOG_SEARCH_FORWARD)
3685 lineno = 1;
3686 else
3687 lineno = s->nlines;
3690 offset = s->line_offsets[lineno - 1];
3691 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3692 free(line);
3693 return got_error_from_errno("fseeko");
3695 linelen = getline(&line, &linesize, s->f);
3696 err = expand_tab(&exstr, line);
3697 if (err)
3698 break;
3699 if (linelen != -1 &&
3700 match_line(exstr, &view->regex, 1, &view->regmatch)) {
3701 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3702 s->matched_line = lineno;
3703 break;
3705 free(exstr);
3706 exstr = NULL;
3707 if (view->searching == TOG_SEARCH_FORWARD)
3708 lineno++;
3709 else
3710 lineno--;
3712 free(line);
3713 free(exstr);
3715 if (s->matched_line) {
3716 s->first_displayed_line = s->matched_line;
3717 s->selected_line = 1;
3720 return err;
3723 static const struct got_error *
3724 close_diff_view(struct tog_view *view)
3726 const struct got_error *err = NULL;
3727 struct tog_diff_view_state *s = &view->state.diff;
3729 free(s->id1);
3730 s->id1 = NULL;
3731 free(s->id2);
3732 s->id2 = NULL;
3733 if (s->f && fclose(s->f) == EOF)
3734 err = got_error_from_errno("fclose");
3735 s->f = NULL;
3736 if (s->f1 && fclose(s->f1) == EOF)
3737 err = got_error_from_errno("fclose");
3738 s->f1 = NULL;
3739 if (s->f2 && fclose(s->f2) == EOF)
3740 err = got_error_from_errno("fclose");
3741 s->f2 = NULL;
3742 free_colors(&s->colors);
3743 free(s->line_offsets);
3744 s->line_offsets = NULL;
3745 s->nlines = 0;
3746 return err;
3749 static const struct got_error *
3750 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3751 struct got_object_id *id2, const char *label1, const char *label2,
3752 int diff_context, int ignore_whitespace, int force_text_diff,
3753 struct tog_view *log_view, struct got_repository *repo)
3755 const struct got_error *err;
3756 struct tog_diff_view_state *s = &view->state.diff;
3758 memset(s, 0, sizeof(*s));
3760 if (id1 != NULL && id2 != NULL) {
3761 int type1, type2;
3762 err = got_object_get_type(&type1, repo, id1);
3763 if (err)
3764 return err;
3765 err = got_object_get_type(&type2, repo, id2);
3766 if (err)
3767 return err;
3769 if (type1 != type2)
3770 return got_error(GOT_ERR_OBJ_TYPE);
3772 s->first_displayed_line = 1;
3773 s->last_displayed_line = view->nlines;
3774 s->selected_line = 1;
3775 s->repo = repo;
3776 s->id1 = id1;
3777 s->id2 = id2;
3778 s->label1 = label1;
3779 s->label2 = label2;
3781 if (id1) {
3782 s->id1 = got_object_id_dup(id1);
3783 if (s->id1 == NULL)
3784 return got_error_from_errno("got_object_id_dup");
3785 s->f1 = got_opentemp();
3786 if (s->f1 == NULL) {
3787 err = got_error_from_errno("got_opentemp");
3788 goto done;
3790 } else
3791 s->id1 = NULL;
3793 s->id2 = got_object_id_dup(id2);
3794 if (s->id2 == NULL) {
3795 err = got_error_from_errno("got_object_id_dup");
3796 goto done;
3799 s->f2 = got_opentemp();
3800 if (s->f2 == NULL) {
3801 err = got_error_from_errno("got_opentemp");
3802 goto done;
3805 s->first_displayed_line = 1;
3806 s->last_displayed_line = view->nlines;
3807 s->diff_context = diff_context;
3808 s->ignore_whitespace = ignore_whitespace;
3809 s->force_text_diff = force_text_diff;
3810 s->log_view = log_view;
3811 s->repo = repo;
3813 STAILQ_INIT(&s->colors);
3814 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3815 err = add_color(&s->colors,
3816 "^-", TOG_COLOR_DIFF_MINUS,
3817 get_color_value("TOG_COLOR_DIFF_MINUS"));
3818 if (err)
3819 goto done;
3820 err = add_color(&s->colors, "^\\+",
3821 TOG_COLOR_DIFF_PLUS,
3822 get_color_value("TOG_COLOR_DIFF_PLUS"));
3823 if (err)
3824 goto done;
3825 err = add_color(&s->colors,
3826 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3827 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3828 if (err)
3829 goto done;
3831 err = add_color(&s->colors,
3832 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3833 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3834 get_color_value("TOG_COLOR_DIFF_META"));
3835 if (err)
3836 goto done;
3838 err = add_color(&s->colors,
3839 "^(from|via): ", TOG_COLOR_AUTHOR,
3840 get_color_value("TOG_COLOR_AUTHOR"));
3841 if (err)
3842 goto done;
3844 err = add_color(&s->colors,
3845 "^date: ", TOG_COLOR_DATE,
3846 get_color_value("TOG_COLOR_DATE"));
3847 if (err)
3848 goto done;
3851 if (log_view && view_is_splitscreen(view))
3852 show_log_view(log_view); /* draw vborder */
3853 diff_view_indicate_progress(view);
3855 err = create_diff(s);
3857 view->show = show_diff_view;
3858 view->input = input_diff_view;
3859 view->close = close_diff_view;
3860 view->search_start = search_start_diff_view;
3861 view->search_next = search_next_diff_view;
3862 done:
3863 if (err)
3864 close_diff_view(view);
3865 return err;
3868 static const struct got_error *
3869 show_diff_view(struct tog_view *view)
3871 const struct got_error *err;
3872 struct tog_diff_view_state *s = &view->state.diff;
3873 char *id_str1 = NULL, *id_str2, *header;
3874 const char *label1, *label2;
3876 if (s->id1) {
3877 err = got_object_id_str(&id_str1, s->id1);
3878 if (err)
3879 return err;
3880 label1 = s->label1 ? : id_str1;
3881 } else
3882 label1 = "/dev/null";
3884 err = got_object_id_str(&id_str2, s->id2);
3885 if (err)
3886 return err;
3887 label2 = s->label2 ? : id_str2;
3889 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3890 err = got_error_from_errno("asprintf");
3891 free(id_str1);
3892 free(id_str2);
3893 return err;
3895 free(id_str1);
3896 free(id_str2);
3898 err = draw_file(view, header);
3899 free(header);
3900 return err;
3903 static const struct got_error *
3904 set_selected_commit(struct tog_diff_view_state *s,
3905 struct commit_queue_entry *entry)
3907 const struct got_error *err;
3908 const struct got_object_id_queue *parent_ids;
3909 struct got_commit_object *selected_commit;
3910 struct got_object_qid *pid;
3912 free(s->id2);
3913 s->id2 = got_object_id_dup(entry->id);
3914 if (s->id2 == NULL)
3915 return got_error_from_errno("got_object_id_dup");
3917 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3918 if (err)
3919 return err;
3920 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3921 free(s->id1);
3922 pid = STAILQ_FIRST(parent_ids);
3923 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3924 got_object_commit_close(selected_commit);
3925 return NULL;
3928 static const struct got_error *
3929 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3931 const struct got_error *err = NULL;
3932 struct tog_diff_view_state *s = &view->state.diff;
3933 struct tog_log_view_state *ls;
3934 struct commit_queue_entry *old_selected_entry;
3935 char *line = NULL;
3936 size_t linesize = 0;
3937 ssize_t linelen;
3938 int i, nscroll = view->nlines - 1;
3940 switch (ch) {
3941 case '0':
3942 view->x = 0;
3943 break;
3944 case '$':
3945 view->x = MAX(view->maxx - view->ncols / 3, 0);
3946 break;
3947 case KEY_RIGHT:
3948 case 'l':
3949 if (view->x + view->ncols / 3 < view->maxx)
3950 view->x += 2; /* move two columns right */
3951 break;
3952 case KEY_LEFT:
3953 case 'h':
3954 view->x -= MIN(view->x, 2); /* move two columns back */
3955 break;
3956 case 'a':
3957 case 'w':
3958 if (ch == 'a')
3959 s->force_text_diff = !s->force_text_diff;
3960 if (ch == 'w')
3961 s->ignore_whitespace = !s->ignore_whitespace;
3962 wclear(view->window);
3963 s->first_displayed_line = 1;
3964 s->last_displayed_line = view->nlines;
3965 s->matched_line = 0;
3966 diff_view_indicate_progress(view);
3967 err = create_diff(s);
3968 break;
3969 case 'g':
3970 case KEY_HOME:
3971 s->first_displayed_line = 1;
3972 break;
3973 case 'G':
3974 case KEY_END:
3975 if (s->eof)
3976 break;
3978 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3979 s->eof = 1;
3980 break;
3981 case 'k':
3982 case KEY_UP:
3983 case CTRL('p'):
3984 if (s->first_displayed_line > 1)
3985 s->first_displayed_line--;
3986 break;
3987 case CTRL('u'):
3988 case 'u':
3989 nscroll /= 2;
3990 /* FALL THROUGH */
3991 case KEY_PPAGE:
3992 case CTRL('b'):
3993 if (s->first_displayed_line == 1)
3994 break;
3995 i = 0;
3996 while (i++ < nscroll && s->first_displayed_line > 1)
3997 s->first_displayed_line--;
3998 break;
3999 case 'j':
4000 case KEY_DOWN:
4001 case CTRL('n'):
4002 if (!s->eof)
4003 s->first_displayed_line++;
4004 break;
4005 case CTRL('d'):
4006 case 'd':
4007 nscroll /= 2;
4008 /* FALL THROUGH */
4009 case KEY_NPAGE:
4010 case CTRL('f'):
4011 case ' ':
4012 if (s->eof)
4013 break;
4014 i = 0;
4015 while (!s->eof && i++ < nscroll) {
4016 linelen = getline(&line, &linesize, s->f);
4017 s->first_displayed_line++;
4018 if (linelen == -1) {
4019 if (feof(s->f)) {
4020 s->eof = 1;
4021 } else
4022 err = got_ferror(s->f, GOT_ERR_IO);
4023 break;
4026 free(line);
4027 break;
4028 case '[':
4029 if (s->diff_context > 0) {
4030 s->diff_context--;
4031 s->matched_line = 0;
4032 diff_view_indicate_progress(view);
4033 err = create_diff(s);
4034 if (s->first_displayed_line + view->nlines - 1 >
4035 s->nlines) {
4036 s->first_displayed_line = 1;
4037 s->last_displayed_line = view->nlines;
4040 break;
4041 case ']':
4042 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4043 s->diff_context++;
4044 s->matched_line = 0;
4045 diff_view_indicate_progress(view);
4046 err = create_diff(s);
4048 break;
4049 case '<':
4050 case ',':
4051 if (s->log_view == NULL)
4052 break;
4053 ls = &s->log_view->state.log;
4054 old_selected_entry = ls->selected_entry;
4056 err = input_log_view(NULL, s->log_view, KEY_UP);
4057 if (err)
4058 break;
4060 if (old_selected_entry == ls->selected_entry)
4061 break;
4063 err = set_selected_commit(s, ls->selected_entry);
4064 if (err)
4065 break;
4067 s->first_displayed_line = 1;
4068 s->last_displayed_line = view->nlines;
4069 s->matched_line = 0;
4070 view->x = 0;
4072 diff_view_indicate_progress(view);
4073 err = create_diff(s);
4074 break;
4075 case '>':
4076 case '.':
4077 if (s->log_view == NULL)
4078 break;
4079 ls = &s->log_view->state.log;
4080 old_selected_entry = ls->selected_entry;
4082 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4083 if (err)
4084 break;
4086 if (old_selected_entry == ls->selected_entry)
4087 break;
4089 err = set_selected_commit(s, ls->selected_entry);
4090 if (err)
4091 break;
4093 s->first_displayed_line = 1;
4094 s->last_displayed_line = view->nlines;
4095 s->matched_line = 0;
4096 view->x = 0;
4098 diff_view_indicate_progress(view);
4099 err = create_diff(s);
4100 break;
4101 default:
4102 break;
4105 return err;
4108 static const struct got_error *
4109 cmd_diff(int argc, char *argv[])
4111 const struct got_error *error = NULL;
4112 struct got_repository *repo = NULL;
4113 struct got_worktree *worktree = NULL;
4114 struct got_object_id *id1 = NULL, *id2 = NULL;
4115 char *repo_path = NULL, *cwd = NULL;
4116 char *id_str1 = NULL, *id_str2 = NULL;
4117 char *label1 = NULL, *label2 = NULL;
4118 int diff_context = 3, ignore_whitespace = 0;
4119 int ch, force_text_diff = 0;
4120 const char *errstr;
4121 struct tog_view *view;
4122 int *pack_fds = NULL;
4124 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4125 switch (ch) {
4126 case 'a':
4127 force_text_diff = 1;
4128 break;
4129 case 'C':
4130 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4131 &errstr);
4132 if (errstr != NULL)
4133 errx(1, "number of context lines is %s: %s",
4134 errstr, errstr);
4135 break;
4136 case 'r':
4137 repo_path = realpath(optarg, NULL);
4138 if (repo_path == NULL)
4139 return got_error_from_errno2("realpath",
4140 optarg);
4141 got_path_strip_trailing_slashes(repo_path);
4142 break;
4143 case 'w':
4144 ignore_whitespace = 1;
4145 break;
4146 default:
4147 usage_diff();
4148 /* NOTREACHED */
4152 argc -= optind;
4153 argv += optind;
4155 if (argc == 0) {
4156 usage_diff(); /* TODO show local worktree changes */
4157 } else if (argc == 2) {
4158 id_str1 = argv[0];
4159 id_str2 = argv[1];
4160 } else
4161 usage_diff();
4163 error = got_repo_pack_fds_open(&pack_fds);
4164 if (error)
4165 goto done;
4167 if (repo_path == NULL) {
4168 cwd = getcwd(NULL, 0);
4169 if (cwd == NULL)
4170 return got_error_from_errno("getcwd");
4171 error = got_worktree_open(&worktree, cwd);
4172 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4173 goto done;
4174 if (worktree)
4175 repo_path =
4176 strdup(got_worktree_get_repo_path(worktree));
4177 else
4178 repo_path = strdup(cwd);
4179 if (repo_path == NULL) {
4180 error = got_error_from_errno("strdup");
4181 goto done;
4185 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4186 if (error)
4187 goto done;
4189 init_curses();
4191 error = apply_unveil(got_repo_get_path(repo), NULL);
4192 if (error)
4193 goto done;
4195 error = tog_load_refs(repo, 0);
4196 if (error)
4197 goto done;
4199 error = got_repo_match_object_id(&id1, &label1, id_str1,
4200 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4201 if (error)
4202 goto done;
4204 error = got_repo_match_object_id(&id2, &label2, id_str2,
4205 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4206 if (error)
4207 goto done;
4209 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4210 if (view == NULL) {
4211 error = got_error_from_errno("view_open");
4212 goto done;
4214 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4215 ignore_whitespace, force_text_diff, NULL, repo);
4216 if (error)
4217 goto done;
4218 error = view_loop(view);
4219 done:
4220 free(label1);
4221 free(label2);
4222 free(repo_path);
4223 free(cwd);
4224 if (repo) {
4225 const struct got_error *close_err = got_repo_close(repo);
4226 if (error == NULL)
4227 error = close_err;
4229 if (worktree)
4230 got_worktree_close(worktree);
4231 if (pack_fds) {
4232 const struct got_error *pack_err =
4233 got_repo_pack_fds_close(pack_fds);
4234 if (error == NULL)
4235 error = pack_err;
4237 tog_free_refs();
4238 return error;
4241 __dead static void
4242 usage_blame(void)
4244 endwin();
4245 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4246 getprogname());
4247 exit(1);
4250 struct tog_blame_line {
4251 int annotated;
4252 struct got_object_id *id;
4255 static const struct got_error *
4256 draw_blame(struct tog_view *view)
4258 struct tog_blame_view_state *s = &view->state.blame;
4259 struct tog_blame *blame = &s->blame;
4260 regmatch_t *regmatch = &view->regmatch;
4261 const struct got_error *err;
4262 int lineno = 0, nprinted = 0, i;
4263 char *line = NULL;
4264 size_t linesize = 0;
4265 ssize_t linelen;
4266 wchar_t *wline;
4267 int width;
4268 struct tog_blame_line *blame_line;
4269 struct got_object_id *prev_id = NULL;
4270 char *id_str;
4271 struct tog_color *tc;
4273 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4274 if (err)
4275 return err;
4277 rewind(blame->f);
4278 werase(view->window);
4280 if (asprintf(&line, "commit %s", id_str) == -1) {
4281 err = got_error_from_errno("asprintf");
4282 free(id_str);
4283 return err;
4286 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4287 free(line);
4288 line = NULL;
4289 if (err)
4290 return err;
4291 if (view_needs_focus_indication(view))
4292 wstandout(view->window);
4293 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4294 if (tc)
4295 wattr_on(view->window,
4296 COLOR_PAIR(tc->colorpair), NULL);
4297 waddwstr(view->window, wline);
4298 if (tc)
4299 wattr_off(view->window,
4300 COLOR_PAIR(tc->colorpair), NULL);
4301 if (view_needs_focus_indication(view))
4302 wstandend(view->window);
4303 free(wline);
4304 wline = NULL;
4305 if (width < view->ncols - 1)
4306 waddch(view->window, '\n');
4308 if (asprintf(&line, "[%d/%d] %s%s",
4309 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4310 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4311 free(id_str);
4312 return got_error_from_errno("asprintf");
4314 free(id_str);
4315 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4316 free(line);
4317 line = NULL;
4318 if (err)
4319 return err;
4320 waddwstr(view->window, wline);
4321 free(wline);
4322 wline = NULL;
4323 if (width < view->ncols - 1)
4324 waddch(view->window, '\n');
4326 s->eof = 0;
4327 view->maxx = 0;
4328 while (nprinted < view->nlines - 2) {
4329 linelen = getline(&line, &linesize, blame->f);
4330 if (linelen == -1) {
4331 if (feof(blame->f)) {
4332 s->eof = 1;
4333 break;
4335 free(line);
4336 return got_ferror(blame->f, GOT_ERR_IO);
4338 if (++lineno < s->first_displayed_line)
4339 continue;
4341 view->maxx = MAX(view->maxx, linelen);
4343 if (view->focussed && nprinted == s->selected_line - 1)
4344 wstandout(view->window);
4346 if (blame->nlines > 0) {
4347 blame_line = &blame->lines[lineno - 1];
4348 if (blame_line->annotated && prev_id &&
4349 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4350 !(view->focussed &&
4351 nprinted == s->selected_line - 1)) {
4352 waddstr(view->window, " ");
4353 } else if (blame_line->annotated) {
4354 char *id_str;
4355 err = got_object_id_str(&id_str, blame_line->id);
4356 if (err) {
4357 free(line);
4358 return err;
4360 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4361 if (tc)
4362 wattr_on(view->window,
4363 COLOR_PAIR(tc->colorpair), NULL);
4364 wprintw(view->window, "%.8s", id_str);
4365 if (tc)
4366 wattr_off(view->window,
4367 COLOR_PAIR(tc->colorpair), NULL);
4368 free(id_str);
4369 prev_id = blame_line->id;
4370 } else {
4371 waddstr(view->window, "........");
4372 prev_id = NULL;
4374 } else {
4375 waddstr(view->window, "........");
4376 prev_id = NULL;
4379 if (view->focussed && nprinted == s->selected_line - 1)
4380 wstandend(view->window);
4381 waddstr(view->window, " ");
4383 if (view->ncols <= 9) {
4384 width = 9;
4385 wline = wcsdup(L"");
4386 if (wline == NULL) {
4387 err = got_error_from_errno("wcsdup");
4388 free(line);
4389 return err;
4391 } else if (s->first_displayed_line + nprinted ==
4392 s->matched_line &&
4393 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4394 err = add_matched_line(&width, line, view->ncols - 9, 9,
4395 view->window, view->x, regmatch);
4396 if (err) {
4397 free(line);
4398 return err;
4400 width += 9;
4401 } else {
4402 err = format_line(&wline, &width, NULL, line, 0,
4403 view->x + view->ncols - 9, 9, 1);
4404 if (err) {
4405 free(line);
4406 return err;
4408 if (view->x < width) {
4409 waddwstr(view->window, wline + view->x);
4410 for (i = 0; i < view->x; i++)
4411 width -= wcwidth(wline[i]);
4413 width += 9;
4414 free(wline);
4415 wline = NULL;
4418 if (width <= view->ncols - 1)
4419 waddch(view->window, '\n');
4420 if (++nprinted == 1)
4421 s->first_displayed_line = lineno;
4423 free(line);
4424 s->last_displayed_line = lineno;
4426 view_vborder(view);
4428 return NULL;
4431 static const struct got_error *
4432 blame_cb(void *arg, int nlines, int lineno,
4433 struct got_commit_object *commit, struct got_object_id *id)
4435 const struct got_error *err = NULL;
4436 struct tog_blame_cb_args *a = arg;
4437 struct tog_blame_line *line;
4438 int errcode;
4440 if (nlines != a->nlines ||
4441 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4442 return got_error(GOT_ERR_RANGE);
4444 errcode = pthread_mutex_lock(&tog_mutex);
4445 if (errcode)
4446 return got_error_set_errno(errcode, "pthread_mutex_lock");
4448 if (*a->quit) { /* user has quit the blame view */
4449 err = got_error(GOT_ERR_ITER_COMPLETED);
4450 goto done;
4453 if (lineno == -1)
4454 goto done; /* no change in this commit */
4456 line = &a->lines[lineno - 1];
4457 if (line->annotated)
4458 goto done;
4460 line->id = got_object_id_dup(id);
4461 if (line->id == NULL) {
4462 err = got_error_from_errno("got_object_id_dup");
4463 goto done;
4465 line->annotated = 1;
4466 done:
4467 errcode = pthread_mutex_unlock(&tog_mutex);
4468 if (errcode)
4469 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4470 return err;
4473 static void *
4474 blame_thread(void *arg)
4476 const struct got_error *err, *close_err;
4477 struct tog_blame_thread_args *ta = arg;
4478 struct tog_blame_cb_args *a = ta->cb_args;
4479 int errcode;
4481 err = block_signals_used_by_main_thread();
4482 if (err)
4483 return (void *)err;
4485 err = got_blame(ta->path, a->commit_id, ta->repo,
4486 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4487 if (err && err->code == GOT_ERR_CANCELLED)
4488 err = NULL;
4490 errcode = pthread_mutex_lock(&tog_mutex);
4491 if (errcode)
4492 return (void *)got_error_set_errno(errcode,
4493 "pthread_mutex_lock");
4495 close_err = got_repo_close(ta->repo);
4496 if (err == NULL)
4497 err = close_err;
4498 ta->repo = NULL;
4499 *ta->complete = 1;
4501 errcode = pthread_mutex_unlock(&tog_mutex);
4502 if (errcode && err == NULL)
4503 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4505 return (void *)err;
4508 static struct got_object_id *
4509 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4510 int first_displayed_line, int selected_line)
4512 struct tog_blame_line *line;
4514 if (nlines <= 0)
4515 return NULL;
4517 line = &lines[first_displayed_line - 1 + selected_line - 1];
4518 if (!line->annotated)
4519 return NULL;
4521 return line->id;
4524 static const struct got_error *
4525 stop_blame(struct tog_blame *blame)
4527 const struct got_error *err = NULL;
4528 int i;
4530 if (blame->thread) {
4531 int errcode;
4532 errcode = pthread_mutex_unlock(&tog_mutex);
4533 if (errcode)
4534 return got_error_set_errno(errcode,
4535 "pthread_mutex_unlock");
4536 errcode = pthread_join(blame->thread, (void **)&err);
4537 if (errcode)
4538 return got_error_set_errno(errcode, "pthread_join");
4539 errcode = pthread_mutex_lock(&tog_mutex);
4540 if (errcode)
4541 return got_error_set_errno(errcode,
4542 "pthread_mutex_lock");
4543 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4544 err = NULL;
4545 blame->thread = NULL;
4547 if (blame->thread_args.repo) {
4548 const struct got_error *close_err;
4549 close_err = got_repo_close(blame->thread_args.repo);
4550 if (err == NULL)
4551 err = close_err;
4552 blame->thread_args.repo = NULL;
4554 if (blame->f) {
4555 if (fclose(blame->f) == EOF && err == NULL)
4556 err = got_error_from_errno("fclose");
4557 blame->f = NULL;
4559 if (blame->lines) {
4560 for (i = 0; i < blame->nlines; i++)
4561 free(blame->lines[i].id);
4562 free(blame->lines);
4563 blame->lines = NULL;
4565 free(blame->cb_args.commit_id);
4566 blame->cb_args.commit_id = NULL;
4567 if (blame->pack_fds) {
4568 const struct got_error *pack_err =
4569 got_repo_pack_fds_close(blame->pack_fds);
4570 if (err == NULL)
4571 err = pack_err;
4572 blame->pack_fds = NULL;
4574 return err;
4577 static const struct got_error *
4578 cancel_blame_view(void *arg)
4580 const struct got_error *err = NULL;
4581 int *done = arg;
4582 int errcode;
4584 errcode = pthread_mutex_lock(&tog_mutex);
4585 if (errcode)
4586 return got_error_set_errno(errcode,
4587 "pthread_mutex_unlock");
4589 if (*done)
4590 err = got_error(GOT_ERR_CANCELLED);
4592 errcode = pthread_mutex_unlock(&tog_mutex);
4593 if (errcode)
4594 return got_error_set_errno(errcode,
4595 "pthread_mutex_lock");
4597 return err;
4600 static const struct got_error *
4601 run_blame(struct tog_view *view)
4603 struct tog_blame_view_state *s = &view->state.blame;
4604 struct tog_blame *blame = &s->blame;
4605 const struct got_error *err = NULL;
4606 struct got_commit_object *commit = NULL;
4607 struct got_blob_object *blob = NULL;
4608 struct got_repository *thread_repo = NULL;
4609 struct got_object_id *obj_id = NULL;
4610 int obj_type;
4611 int *pack_fds = NULL;
4613 err = got_object_open_as_commit(&commit, s->repo,
4614 &s->blamed_commit->id);
4615 if (err)
4616 return err;
4618 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4619 if (err)
4620 goto done;
4622 err = got_object_get_type(&obj_type, s->repo, obj_id);
4623 if (err)
4624 goto done;
4626 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4627 err = got_error(GOT_ERR_OBJ_TYPE);
4628 goto done;
4631 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4632 if (err)
4633 goto done;
4634 blame->f = got_opentemp();
4635 if (blame->f == NULL) {
4636 err = got_error_from_errno("got_opentemp");
4637 goto done;
4639 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4640 &blame->line_offsets, blame->f, blob);
4641 if (err)
4642 goto done;
4643 if (blame->nlines == 0) {
4644 s->blame_complete = 1;
4645 goto done;
4648 /* Don't include \n at EOF in the blame line count. */
4649 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4650 blame->nlines--;
4652 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4653 if (blame->lines == NULL) {
4654 err = got_error_from_errno("calloc");
4655 goto done;
4658 err = got_repo_pack_fds_open(&pack_fds);
4659 if (err)
4660 goto done;
4661 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4662 pack_fds);
4663 if (err)
4664 goto done;
4666 blame->pack_fds = pack_fds;
4667 blame->cb_args.view = view;
4668 blame->cb_args.lines = blame->lines;
4669 blame->cb_args.nlines = blame->nlines;
4670 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4671 if (blame->cb_args.commit_id == NULL) {
4672 err = got_error_from_errno("got_object_id_dup");
4673 goto done;
4675 blame->cb_args.quit = &s->done;
4677 blame->thread_args.path = s->path;
4678 blame->thread_args.repo = thread_repo;
4679 blame->thread_args.cb_args = &blame->cb_args;
4680 blame->thread_args.complete = &s->blame_complete;
4681 blame->thread_args.cancel_cb = cancel_blame_view;
4682 blame->thread_args.cancel_arg = &s->done;
4683 s->blame_complete = 0;
4685 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4686 s->first_displayed_line = 1;
4687 s->last_displayed_line = view->nlines;
4688 s->selected_line = 1;
4690 s->matched_line = 0;
4692 done:
4693 if (commit)
4694 got_object_commit_close(commit);
4695 if (blob)
4696 got_object_blob_close(blob);
4697 free(obj_id);
4698 if (err)
4699 stop_blame(blame);
4700 return err;
4703 static const struct got_error *
4704 open_blame_view(struct tog_view *view, char *path,
4705 struct got_object_id *commit_id, struct got_repository *repo)
4707 const struct got_error *err = NULL;
4708 struct tog_blame_view_state *s = &view->state.blame;
4710 STAILQ_INIT(&s->blamed_commits);
4712 s->path = strdup(path);
4713 if (s->path == NULL)
4714 return got_error_from_errno("strdup");
4716 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4717 if (err) {
4718 free(s->path);
4719 return err;
4722 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4723 s->first_displayed_line = 1;
4724 s->last_displayed_line = view->nlines;
4725 s->selected_line = 1;
4726 s->blame_complete = 0;
4727 s->repo = repo;
4728 s->commit_id = commit_id;
4729 memset(&s->blame, 0, sizeof(s->blame));
4731 STAILQ_INIT(&s->colors);
4732 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4733 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4734 get_color_value("TOG_COLOR_COMMIT"));
4735 if (err)
4736 return err;
4739 view->show = show_blame_view;
4740 view->input = input_blame_view;
4741 view->close = close_blame_view;
4742 view->search_start = search_start_blame_view;
4743 view->search_next = search_next_blame_view;
4745 return run_blame(view);
4748 static const struct got_error *
4749 close_blame_view(struct tog_view *view)
4751 const struct got_error *err = NULL;
4752 struct tog_blame_view_state *s = &view->state.blame;
4754 if (s->blame.thread)
4755 err = stop_blame(&s->blame);
4757 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4758 struct got_object_qid *blamed_commit;
4759 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4760 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4761 got_object_qid_free(blamed_commit);
4764 free(s->path);
4765 free_colors(&s->colors);
4766 return err;
4769 static const struct got_error *
4770 search_start_blame_view(struct tog_view *view)
4772 struct tog_blame_view_state *s = &view->state.blame;
4774 s->matched_line = 0;
4775 return NULL;
4778 static const struct got_error *
4779 search_next_blame_view(struct tog_view *view)
4781 struct tog_blame_view_state *s = &view->state.blame;
4782 const struct got_error *err = NULL;
4783 int lineno;
4784 char *exstr = NULL, *line = NULL;
4785 size_t linesize = 0;
4786 ssize_t linelen;
4788 if (!view->searching) {
4789 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4790 return NULL;
4793 if (s->matched_line) {
4794 if (view->searching == TOG_SEARCH_FORWARD)
4795 lineno = s->matched_line + 1;
4796 else
4797 lineno = s->matched_line - 1;
4798 } else
4799 lineno = s->first_displayed_line - 1 + s->selected_line;
4801 while (1) {
4802 off_t offset;
4804 if (lineno <= 0 || lineno > s->blame.nlines) {
4805 if (s->matched_line == 0) {
4806 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4807 break;
4810 if (view->searching == TOG_SEARCH_FORWARD)
4811 lineno = 1;
4812 else
4813 lineno = s->blame.nlines;
4816 offset = s->blame.line_offsets[lineno - 1];
4817 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4818 free(line);
4819 return got_error_from_errno("fseeko");
4821 linelen = getline(&line, &linesize, s->blame.f);
4822 err = expand_tab(&exstr, line);
4823 if (err)
4824 break;
4825 if (linelen != -1 &&
4826 match_line(exstr, &view->regex, 1, &view->regmatch)) {
4827 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4828 s->matched_line = lineno;
4829 break;
4831 free(exstr);
4832 exstr = NULL;
4833 if (view->searching == TOG_SEARCH_FORWARD)
4834 lineno++;
4835 else
4836 lineno--;
4838 free(line);
4839 free(exstr);
4841 if (s->matched_line) {
4842 s->first_displayed_line = s->matched_line;
4843 s->selected_line = 1;
4846 return err;
4849 static const struct got_error *
4850 show_blame_view(struct tog_view *view)
4852 const struct got_error *err = NULL;
4853 struct tog_blame_view_state *s = &view->state.blame;
4854 int errcode;
4856 if (s->blame.thread == NULL && !s->blame_complete) {
4857 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4858 &s->blame.thread_args);
4859 if (errcode)
4860 return got_error_set_errno(errcode, "pthread_create");
4862 halfdelay(1); /* fast refresh while annotating */
4865 if (s->blame_complete)
4866 halfdelay(10); /* disable fast refresh */
4868 err = draw_blame(view);
4870 view_vborder(view);
4871 return err;
4874 static const struct got_error *
4875 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4877 const struct got_error *err = NULL, *thread_err = NULL;
4878 struct tog_view *diff_view;
4879 struct tog_blame_view_state *s = &view->state.blame;
4880 int begin_x = 0, nscroll = view->nlines - 2;
4882 switch (ch) {
4883 case '0':
4884 view->x = 0;
4885 break;
4886 case '$':
4887 view->x = MAX(view->maxx - view->ncols / 3, 0);
4888 break;
4889 case KEY_RIGHT:
4890 case 'l':
4891 if (view->x + view->ncols / 3 < view->maxx)
4892 view->x += 2; /* move two columns right */
4893 break;
4894 case KEY_LEFT:
4895 case 'h':
4896 view->x -= MIN(view->x, 2); /* move two columns back */
4897 break;
4898 case 'q':
4899 s->done = 1;
4900 break;
4901 case 'g':
4902 case KEY_HOME:
4903 s->selected_line = 1;
4904 s->first_displayed_line = 1;
4905 break;
4906 case 'G':
4907 case KEY_END:
4908 if (s->blame.nlines < view->nlines - 2) {
4909 s->selected_line = s->blame.nlines;
4910 s->first_displayed_line = 1;
4911 } else {
4912 s->selected_line = view->nlines - 2;
4913 s->first_displayed_line = s->blame.nlines -
4914 (view->nlines - 3);
4916 break;
4917 case 'k':
4918 case KEY_UP:
4919 case CTRL('p'):
4920 if (s->selected_line > 1)
4921 s->selected_line--;
4922 else if (s->selected_line == 1 &&
4923 s->first_displayed_line > 1)
4924 s->first_displayed_line--;
4925 break;
4926 case CTRL('u'):
4927 case 'u':
4928 nscroll /= 2;
4929 /* FALL THROUGH */
4930 case KEY_PPAGE:
4931 case CTRL('b'):
4932 if (s->first_displayed_line == 1) {
4933 s->selected_line = MAX(1, s->selected_line - nscroll);
4934 break;
4936 if (s->first_displayed_line > nscroll)
4937 s->first_displayed_line -= nscroll;
4938 else
4939 s->first_displayed_line = 1;
4940 break;
4941 case 'j':
4942 case KEY_DOWN:
4943 case CTRL('n'):
4944 if (s->selected_line < view->nlines - 2 &&
4945 s->first_displayed_line +
4946 s->selected_line <= s->blame.nlines)
4947 s->selected_line++;
4948 else if (s->last_displayed_line <
4949 s->blame.nlines)
4950 s->first_displayed_line++;
4951 break;
4952 case 'b':
4953 case 'p': {
4954 struct got_object_id *id = NULL;
4955 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4956 s->first_displayed_line, s->selected_line);
4957 if (id == NULL)
4958 break;
4959 if (ch == 'p') {
4960 struct got_commit_object *commit, *pcommit;
4961 struct got_object_qid *pid;
4962 struct got_object_id *blob_id = NULL;
4963 int obj_type;
4964 err = got_object_open_as_commit(&commit,
4965 s->repo, id);
4966 if (err)
4967 break;
4968 pid = STAILQ_FIRST(
4969 got_object_commit_get_parent_ids(commit));
4970 if (pid == NULL) {
4971 got_object_commit_close(commit);
4972 break;
4974 /* Check if path history ends here. */
4975 err = got_object_open_as_commit(&pcommit,
4976 s->repo, &pid->id);
4977 if (err)
4978 break;
4979 err = got_object_id_by_path(&blob_id, s->repo,
4980 pcommit, s->path);
4981 got_object_commit_close(pcommit);
4982 if (err) {
4983 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4984 err = NULL;
4985 got_object_commit_close(commit);
4986 break;
4988 err = got_object_get_type(&obj_type, s->repo,
4989 blob_id);
4990 free(blob_id);
4991 /* Can't blame non-blob type objects. */
4992 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4993 got_object_commit_close(commit);
4994 break;
4996 err = got_object_qid_alloc(&s->blamed_commit,
4997 &pid->id);
4998 got_object_commit_close(commit);
4999 } else {
5000 if (got_object_id_cmp(id,
5001 &s->blamed_commit->id) == 0)
5002 break;
5003 err = got_object_qid_alloc(&s->blamed_commit,
5004 id);
5006 if (err)
5007 break;
5008 s->done = 1;
5009 thread_err = stop_blame(&s->blame);
5010 s->done = 0;
5011 if (thread_err)
5012 break;
5013 STAILQ_INSERT_HEAD(&s->blamed_commits,
5014 s->blamed_commit, entry);
5015 err = run_blame(view);
5016 if (err)
5017 break;
5018 break;
5020 case 'B': {
5021 struct got_object_qid *first;
5022 first = STAILQ_FIRST(&s->blamed_commits);
5023 if (!got_object_id_cmp(&first->id, s->commit_id))
5024 break;
5025 s->done = 1;
5026 thread_err = stop_blame(&s->blame);
5027 s->done = 0;
5028 if (thread_err)
5029 break;
5030 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5031 got_object_qid_free(s->blamed_commit);
5032 s->blamed_commit =
5033 STAILQ_FIRST(&s->blamed_commits);
5034 err = run_blame(view);
5035 if (err)
5036 break;
5037 break;
5039 case KEY_ENTER:
5040 case '\r': {
5041 struct got_object_id *id = NULL;
5042 struct got_object_qid *pid;
5043 struct got_commit_object *commit = NULL;
5044 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5045 s->first_displayed_line, s->selected_line);
5046 if (id == NULL)
5047 break;
5048 err = got_object_open_as_commit(&commit, s->repo, id);
5049 if (err)
5050 break;
5051 pid = STAILQ_FIRST(
5052 got_object_commit_get_parent_ids(commit));
5053 if (view_is_parent_view(view))
5054 begin_x = view_split_begin_x(view->begin_x);
5055 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5056 if (diff_view == NULL) {
5057 got_object_commit_close(commit);
5058 err = got_error_from_errno("view_open");
5059 break;
5061 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5062 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5063 got_object_commit_close(commit);
5064 if (err) {
5065 view_close(diff_view);
5066 break;
5068 view->focussed = 0;
5069 diff_view->focussed = 1;
5070 if (view_is_parent_view(view)) {
5071 err = view_close_child(view);
5072 if (err)
5073 break;
5074 view_set_child(view, diff_view);
5075 view->focus_child = 1;
5076 } else
5077 *new_view = diff_view;
5078 if (err)
5079 break;
5080 break;
5082 case CTRL('d'):
5083 case 'd':
5084 nscroll /= 2;
5085 /* FALL THROUGH */
5086 case KEY_NPAGE:
5087 case CTRL('f'):
5088 case ' ':
5089 if (s->last_displayed_line >= s->blame.nlines &&
5090 s->selected_line >= MIN(s->blame.nlines,
5091 view->nlines - 2)) {
5092 break;
5094 if (s->last_displayed_line >= s->blame.nlines &&
5095 s->selected_line < view->nlines - 2) {
5096 s->selected_line +=
5097 MIN(nscroll, s->last_displayed_line -
5098 s->first_displayed_line - s->selected_line + 1);
5100 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5101 s->first_displayed_line += nscroll;
5102 else
5103 s->first_displayed_line =
5104 s->blame.nlines - (view->nlines - 3);
5105 break;
5106 case KEY_RESIZE:
5107 if (s->selected_line > view->nlines - 2) {
5108 s->selected_line = MIN(s->blame.nlines,
5109 view->nlines - 2);
5111 break;
5112 default:
5113 break;
5115 return thread_err ? thread_err : err;
5118 static const struct got_error *
5119 cmd_blame(int argc, char *argv[])
5121 const struct got_error *error;
5122 struct got_repository *repo = NULL;
5123 struct got_worktree *worktree = NULL;
5124 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5125 char *link_target = NULL;
5126 struct got_object_id *commit_id = NULL;
5127 struct got_commit_object *commit = NULL;
5128 char *commit_id_str = NULL;
5129 int ch;
5130 struct tog_view *view;
5131 int *pack_fds = NULL;
5133 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5134 switch (ch) {
5135 case 'c':
5136 commit_id_str = optarg;
5137 break;
5138 case 'r':
5139 repo_path = realpath(optarg, NULL);
5140 if (repo_path == NULL)
5141 return got_error_from_errno2("realpath",
5142 optarg);
5143 break;
5144 default:
5145 usage_blame();
5146 /* NOTREACHED */
5150 argc -= optind;
5151 argv += optind;
5153 if (argc != 1)
5154 usage_blame();
5156 error = got_repo_pack_fds_open(&pack_fds);
5157 if (error != NULL)
5158 goto done;
5160 if (repo_path == NULL) {
5161 cwd = getcwd(NULL, 0);
5162 if (cwd == NULL)
5163 return got_error_from_errno("getcwd");
5164 error = got_worktree_open(&worktree, cwd);
5165 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5166 goto done;
5167 if (worktree)
5168 repo_path =
5169 strdup(got_worktree_get_repo_path(worktree));
5170 else
5171 repo_path = strdup(cwd);
5172 if (repo_path == NULL) {
5173 error = got_error_from_errno("strdup");
5174 goto done;
5178 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5179 if (error != NULL)
5180 goto done;
5182 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5183 worktree);
5184 if (error)
5185 goto done;
5187 init_curses();
5189 error = apply_unveil(got_repo_get_path(repo), NULL);
5190 if (error)
5191 goto done;
5193 error = tog_load_refs(repo, 0);
5194 if (error)
5195 goto done;
5197 if (commit_id_str == NULL) {
5198 struct got_reference *head_ref;
5199 error = got_ref_open(&head_ref, repo, worktree ?
5200 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5201 if (error != NULL)
5202 goto done;
5203 error = got_ref_resolve(&commit_id, repo, head_ref);
5204 got_ref_close(head_ref);
5205 } else {
5206 error = got_repo_match_object_id(&commit_id, NULL,
5207 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5209 if (error != NULL)
5210 goto done;
5212 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5213 if (view == NULL) {
5214 error = got_error_from_errno("view_open");
5215 goto done;
5218 error = got_object_open_as_commit(&commit, repo, commit_id);
5219 if (error)
5220 goto done;
5222 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5223 commit, repo);
5224 if (error)
5225 goto done;
5227 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5228 commit_id, repo);
5229 if (error)
5230 goto done;
5231 if (worktree) {
5232 /* Release work tree lock. */
5233 got_worktree_close(worktree);
5234 worktree = NULL;
5236 error = view_loop(view);
5237 done:
5238 free(repo_path);
5239 free(in_repo_path);
5240 free(link_target);
5241 free(cwd);
5242 free(commit_id);
5243 if (commit)
5244 got_object_commit_close(commit);
5245 if (worktree)
5246 got_worktree_close(worktree);
5247 if (repo) {
5248 const struct got_error *close_err = got_repo_close(repo);
5249 if (error == NULL)
5250 error = close_err;
5252 if (pack_fds) {
5253 const struct got_error *pack_err =
5254 got_repo_pack_fds_close(pack_fds);
5255 if (error == NULL)
5256 error = pack_err;
5258 tog_free_refs();
5259 return error;
5262 static const struct got_error *
5263 draw_tree_entries(struct tog_view *view, const char *parent_path)
5265 struct tog_tree_view_state *s = &view->state.tree;
5266 const struct got_error *err = NULL;
5267 struct got_tree_entry *te;
5268 wchar_t *wline;
5269 struct tog_color *tc;
5270 int width, n, i, nentries;
5271 int limit = view->nlines;
5273 s->ndisplayed = 0;
5275 werase(view->window);
5277 if (limit == 0)
5278 return NULL;
5280 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5281 0, 0);
5282 if (err)
5283 return err;
5284 if (view_needs_focus_indication(view))
5285 wstandout(view->window);
5286 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5287 if (tc)
5288 wattr_on(view->window,
5289 COLOR_PAIR(tc->colorpair), NULL);
5290 waddwstr(view->window, wline);
5291 if (tc)
5292 wattr_off(view->window,
5293 COLOR_PAIR(tc->colorpair), NULL);
5294 if (view_needs_focus_indication(view))
5295 wstandend(view->window);
5296 free(wline);
5297 wline = NULL;
5298 if (width < view->ncols - 1)
5299 waddch(view->window, '\n');
5300 if (--limit <= 0)
5301 return NULL;
5302 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5303 0, 0);
5304 if (err)
5305 return err;
5306 waddwstr(view->window, wline);
5307 free(wline);
5308 wline = NULL;
5309 if (width < view->ncols - 1)
5310 waddch(view->window, '\n');
5311 if (--limit <= 0)
5312 return NULL;
5313 waddch(view->window, '\n');
5314 if (--limit <= 0)
5315 return NULL;
5317 if (s->first_displayed_entry == NULL) {
5318 te = got_object_tree_get_first_entry(s->tree);
5319 if (s->selected == 0) {
5320 if (view->focussed)
5321 wstandout(view->window);
5322 s->selected_entry = NULL;
5324 waddstr(view->window, " ..\n"); /* parent directory */
5325 if (s->selected == 0 && view->focussed)
5326 wstandend(view->window);
5327 s->ndisplayed++;
5328 if (--limit <= 0)
5329 return NULL;
5330 n = 1;
5331 } else {
5332 n = 0;
5333 te = s->first_displayed_entry;
5336 nentries = got_object_tree_get_nentries(s->tree);
5337 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5338 char *line = NULL, *id_str = NULL, *link_target = NULL;
5339 const char *modestr = "";
5340 mode_t mode;
5342 te = got_object_tree_get_entry(s->tree, i);
5343 mode = got_tree_entry_get_mode(te);
5345 if (s->show_ids) {
5346 err = got_object_id_str(&id_str,
5347 got_tree_entry_get_id(te));
5348 if (err)
5349 return got_error_from_errno(
5350 "got_object_id_str");
5352 if (got_object_tree_entry_is_submodule(te))
5353 modestr = "$";
5354 else if (S_ISLNK(mode)) {
5355 int i;
5357 err = got_tree_entry_get_symlink_target(&link_target,
5358 te, s->repo);
5359 if (err) {
5360 free(id_str);
5361 return err;
5363 for (i = 0; i < strlen(link_target); i++) {
5364 if (!isprint((unsigned char)link_target[i]))
5365 link_target[i] = '?';
5367 modestr = "@";
5369 else if (S_ISDIR(mode))
5370 modestr = "/";
5371 else if (mode & S_IXUSR)
5372 modestr = "*";
5373 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5374 got_tree_entry_get_name(te), modestr,
5375 link_target ? " -> ": "",
5376 link_target ? link_target : "") == -1) {
5377 free(id_str);
5378 free(link_target);
5379 return got_error_from_errno("asprintf");
5381 free(id_str);
5382 free(link_target);
5383 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5384 0, 0);
5385 if (err) {
5386 free(line);
5387 break;
5389 if (n == s->selected) {
5390 if (view->focussed)
5391 wstandout(view->window);
5392 s->selected_entry = te;
5394 tc = match_color(&s->colors, line);
5395 if (tc)
5396 wattr_on(view->window,
5397 COLOR_PAIR(tc->colorpair), NULL);
5398 waddwstr(view->window, wline);
5399 if (tc)
5400 wattr_off(view->window,
5401 COLOR_PAIR(tc->colorpair), NULL);
5402 if (width < view->ncols - 1)
5403 waddch(view->window, '\n');
5404 if (n == s->selected && view->focussed)
5405 wstandend(view->window);
5406 free(line);
5407 free(wline);
5408 wline = NULL;
5409 n++;
5410 s->ndisplayed++;
5411 s->last_displayed_entry = te;
5412 if (--limit <= 0)
5413 break;
5416 return err;
5419 static void
5420 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5422 struct got_tree_entry *te;
5423 int isroot = s->tree == s->root;
5424 int i = 0;
5426 if (s->first_displayed_entry == NULL)
5427 return;
5429 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5430 while (i++ < maxscroll) {
5431 if (te == NULL) {
5432 if (!isroot)
5433 s->first_displayed_entry = NULL;
5434 break;
5436 s->first_displayed_entry = te;
5437 te = got_tree_entry_get_prev(s->tree, te);
5441 static void
5442 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5444 struct got_tree_entry *next, *last;
5445 int n = 0;
5447 if (s->first_displayed_entry)
5448 next = got_tree_entry_get_next(s->tree,
5449 s->first_displayed_entry);
5450 else
5451 next = got_object_tree_get_first_entry(s->tree);
5453 last = s->last_displayed_entry;
5454 while (next && last && n++ < maxscroll) {
5455 last = got_tree_entry_get_next(s->tree, last);
5456 if (last) {
5457 s->first_displayed_entry = next;
5458 next = got_tree_entry_get_next(s->tree, next);
5463 static const struct got_error *
5464 tree_entry_path(char **path, struct tog_parent_trees *parents,
5465 struct got_tree_entry *te)
5467 const struct got_error *err = NULL;
5468 struct tog_parent_tree *pt;
5469 size_t len = 2; /* for leading slash and NUL */
5471 TAILQ_FOREACH(pt, parents, entry)
5472 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5473 + 1 /* slash */;
5474 if (te)
5475 len += strlen(got_tree_entry_get_name(te));
5477 *path = calloc(1, len);
5478 if (path == NULL)
5479 return got_error_from_errno("calloc");
5481 (*path)[0] = '/';
5482 pt = TAILQ_LAST(parents, tog_parent_trees);
5483 while (pt) {
5484 const char *name = got_tree_entry_get_name(pt->selected_entry);
5485 if (strlcat(*path, name, len) >= len) {
5486 err = got_error(GOT_ERR_NO_SPACE);
5487 goto done;
5489 if (strlcat(*path, "/", len) >= len) {
5490 err = got_error(GOT_ERR_NO_SPACE);
5491 goto done;
5493 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5495 if (te) {
5496 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5497 err = got_error(GOT_ERR_NO_SPACE);
5498 goto done;
5501 done:
5502 if (err) {
5503 free(*path);
5504 *path = NULL;
5506 return err;
5509 static const struct got_error *
5510 blame_tree_entry(struct tog_view **new_view, int begin_x,
5511 struct got_tree_entry *te, struct tog_parent_trees *parents,
5512 struct got_object_id *commit_id, struct got_repository *repo)
5514 const struct got_error *err = NULL;
5515 char *path;
5516 struct tog_view *blame_view;
5518 *new_view = NULL;
5520 err = tree_entry_path(&path, parents, te);
5521 if (err)
5522 return err;
5524 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5525 if (blame_view == NULL) {
5526 err = got_error_from_errno("view_open");
5527 goto done;
5530 err = open_blame_view(blame_view, path, commit_id, repo);
5531 if (err) {
5532 if (err->code == GOT_ERR_CANCELLED)
5533 err = NULL;
5534 view_close(blame_view);
5535 } else
5536 *new_view = blame_view;
5537 done:
5538 free(path);
5539 return err;
5542 static const struct got_error *
5543 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5544 struct tog_tree_view_state *s)
5546 struct tog_view *log_view;
5547 const struct got_error *err = NULL;
5548 char *path;
5550 *new_view = NULL;
5552 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5553 if (log_view == NULL)
5554 return got_error_from_errno("view_open");
5556 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5557 if (err)
5558 return err;
5560 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5561 path, 0);
5562 if (err)
5563 view_close(log_view);
5564 else
5565 *new_view = log_view;
5566 free(path);
5567 return err;
5570 static const struct got_error *
5571 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5572 const char *head_ref_name, struct got_repository *repo)
5574 const struct got_error *err = NULL;
5575 char *commit_id_str = NULL;
5576 struct tog_tree_view_state *s = &view->state.tree;
5577 struct got_commit_object *commit = NULL;
5579 TAILQ_INIT(&s->parents);
5580 STAILQ_INIT(&s->colors);
5582 s->commit_id = got_object_id_dup(commit_id);
5583 if (s->commit_id == NULL)
5584 return got_error_from_errno("got_object_id_dup");
5586 err = got_object_open_as_commit(&commit, repo, commit_id);
5587 if (err)
5588 goto done;
5591 * The root is opened here and will be closed when the view is closed.
5592 * Any visited subtrees and their path-wise parents are opened and
5593 * closed on demand.
5595 err = got_object_open_as_tree(&s->root, repo,
5596 got_object_commit_get_tree_id(commit));
5597 if (err)
5598 goto done;
5599 s->tree = s->root;
5601 err = got_object_id_str(&commit_id_str, commit_id);
5602 if (err != NULL)
5603 goto done;
5605 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5606 err = got_error_from_errno("asprintf");
5607 goto done;
5610 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5611 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5612 if (head_ref_name) {
5613 s->head_ref_name = strdup(head_ref_name);
5614 if (s->head_ref_name == NULL) {
5615 err = got_error_from_errno("strdup");
5616 goto done;
5619 s->repo = repo;
5621 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5622 err = add_color(&s->colors, "\\$$",
5623 TOG_COLOR_TREE_SUBMODULE,
5624 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5625 if (err)
5626 goto done;
5627 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5628 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5629 if (err)
5630 goto done;
5631 err = add_color(&s->colors, "/$",
5632 TOG_COLOR_TREE_DIRECTORY,
5633 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5634 if (err)
5635 goto done;
5637 err = add_color(&s->colors, "\\*$",
5638 TOG_COLOR_TREE_EXECUTABLE,
5639 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5640 if (err)
5641 goto done;
5643 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5644 get_color_value("TOG_COLOR_COMMIT"));
5645 if (err)
5646 goto done;
5649 view->show = show_tree_view;
5650 view->input = input_tree_view;
5651 view->close = close_tree_view;
5652 view->search_start = search_start_tree_view;
5653 view->search_next = search_next_tree_view;
5654 done:
5655 free(commit_id_str);
5656 if (commit)
5657 got_object_commit_close(commit);
5658 if (err)
5659 close_tree_view(view);
5660 return err;
5663 static const struct got_error *
5664 close_tree_view(struct tog_view *view)
5666 struct tog_tree_view_state *s = &view->state.tree;
5668 free_colors(&s->colors);
5669 free(s->tree_label);
5670 s->tree_label = NULL;
5671 free(s->commit_id);
5672 s->commit_id = NULL;
5673 free(s->head_ref_name);
5674 s->head_ref_name = NULL;
5675 while (!TAILQ_EMPTY(&s->parents)) {
5676 struct tog_parent_tree *parent;
5677 parent = TAILQ_FIRST(&s->parents);
5678 TAILQ_REMOVE(&s->parents, parent, entry);
5679 if (parent->tree != s->root)
5680 got_object_tree_close(parent->tree);
5681 free(parent);
5684 if (s->tree != NULL && s->tree != s->root)
5685 got_object_tree_close(s->tree);
5686 if (s->root)
5687 got_object_tree_close(s->root);
5688 return NULL;
5691 static const struct got_error *
5692 search_start_tree_view(struct tog_view *view)
5694 struct tog_tree_view_state *s = &view->state.tree;
5696 s->matched_entry = NULL;
5697 return NULL;
5700 static int
5701 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5703 regmatch_t regmatch;
5705 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5706 0) == 0;
5709 static const struct got_error *
5710 search_next_tree_view(struct tog_view *view)
5712 struct tog_tree_view_state *s = &view->state.tree;
5713 struct got_tree_entry *te = NULL;
5715 if (!view->searching) {
5716 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5717 return NULL;
5720 if (s->matched_entry) {
5721 if (view->searching == TOG_SEARCH_FORWARD) {
5722 if (s->selected_entry)
5723 te = got_tree_entry_get_next(s->tree,
5724 s->selected_entry);
5725 else
5726 te = got_object_tree_get_first_entry(s->tree);
5727 } else {
5728 if (s->selected_entry == NULL)
5729 te = got_object_tree_get_last_entry(s->tree);
5730 else
5731 te = got_tree_entry_get_prev(s->tree,
5732 s->selected_entry);
5734 } else {
5735 if (s->selected_entry)
5736 te = s->selected_entry;
5737 else if (view->searching == TOG_SEARCH_FORWARD)
5738 te = got_object_tree_get_first_entry(s->tree);
5739 else
5740 te = got_object_tree_get_last_entry(s->tree);
5743 while (1) {
5744 if (te == NULL) {
5745 if (s->matched_entry == NULL) {
5746 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5747 return NULL;
5749 if (view->searching == TOG_SEARCH_FORWARD)
5750 te = got_object_tree_get_first_entry(s->tree);
5751 else
5752 te = got_object_tree_get_last_entry(s->tree);
5755 if (match_tree_entry(te, &view->regex)) {
5756 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5757 s->matched_entry = te;
5758 break;
5761 if (view->searching == TOG_SEARCH_FORWARD)
5762 te = got_tree_entry_get_next(s->tree, te);
5763 else
5764 te = got_tree_entry_get_prev(s->tree, te);
5767 if (s->matched_entry) {
5768 s->first_displayed_entry = s->matched_entry;
5769 s->selected = 0;
5772 return NULL;
5775 static const struct got_error *
5776 show_tree_view(struct tog_view *view)
5778 const struct got_error *err = NULL;
5779 struct tog_tree_view_state *s = &view->state.tree;
5780 char *parent_path;
5782 err = tree_entry_path(&parent_path, &s->parents, NULL);
5783 if (err)
5784 return err;
5786 err = draw_tree_entries(view, parent_path);
5787 free(parent_path);
5789 view_vborder(view);
5790 return err;
5793 static const struct got_error *
5794 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5796 const struct got_error *err = NULL;
5797 struct tog_tree_view_state *s = &view->state.tree;
5798 struct tog_view *log_view, *ref_view;
5799 struct got_tree_entry *te;
5800 int begin_x = 0, n, nscroll = view->nlines - 3;
5802 switch (ch) {
5803 case 'i':
5804 s->show_ids = !s->show_ids;
5805 break;
5806 case 'l':
5807 if (!s->selected_entry)
5808 break;
5809 if (view_is_parent_view(view))
5810 begin_x = view_split_begin_x(view->begin_x);
5811 err = log_selected_tree_entry(&log_view, begin_x, s);
5812 view->focussed = 0;
5813 log_view->focussed = 1;
5814 if (view_is_parent_view(view)) {
5815 err = view_close_child(view);
5816 if (err)
5817 return err;
5818 view_set_child(view, log_view);
5819 view->focus_child = 1;
5820 } else
5821 *new_view = log_view;
5822 break;
5823 case 'r':
5824 if (view_is_parent_view(view))
5825 begin_x = view_split_begin_x(view->begin_x);
5826 ref_view = view_open(view->nlines, view->ncols,
5827 view->begin_y, begin_x, TOG_VIEW_REF);
5828 if (ref_view == NULL)
5829 return got_error_from_errno("view_open");
5830 err = open_ref_view(ref_view, s->repo);
5831 if (err) {
5832 view_close(ref_view);
5833 return err;
5835 view->focussed = 0;
5836 ref_view->focussed = 1;
5837 if (view_is_parent_view(view)) {
5838 err = view_close_child(view);
5839 if (err)
5840 return err;
5841 view_set_child(view, ref_view);
5842 view->focus_child = 1;
5843 } else
5844 *new_view = ref_view;
5845 break;
5846 case 'g':
5847 case KEY_HOME:
5848 s->selected = 0;
5849 if (s->tree == s->root)
5850 s->first_displayed_entry =
5851 got_object_tree_get_first_entry(s->tree);
5852 else
5853 s->first_displayed_entry = NULL;
5854 break;
5855 case 'G':
5856 case KEY_END:
5857 s->selected = 0;
5858 te = got_object_tree_get_last_entry(s->tree);
5859 for (n = 0; n < view->nlines - 3; n++) {
5860 if (te == NULL) {
5861 if(s->tree != s->root) {
5862 s->first_displayed_entry = NULL;
5863 n++;
5865 break;
5867 s->first_displayed_entry = te;
5868 te = got_tree_entry_get_prev(s->tree, te);
5870 if (n > 0)
5871 s->selected = n - 1;
5872 break;
5873 case 'k':
5874 case KEY_UP:
5875 case CTRL('p'):
5876 if (s->selected > 0) {
5877 s->selected--;
5878 break;
5880 tree_scroll_up(s, 1);
5881 break;
5882 case CTRL('u'):
5883 case 'u':
5884 nscroll /= 2;
5885 /* FALL THROUGH */
5886 case KEY_PPAGE:
5887 case CTRL('b'):
5888 if (s->tree == s->root) {
5889 if (got_object_tree_get_first_entry(s->tree) ==
5890 s->first_displayed_entry)
5891 s->selected -= MIN(s->selected, nscroll);
5892 } else {
5893 if (s->first_displayed_entry == NULL)
5894 s->selected -= MIN(s->selected, nscroll);
5896 tree_scroll_up(s, MAX(0, nscroll));
5897 break;
5898 case 'j':
5899 case KEY_DOWN:
5900 case CTRL('n'):
5901 if (s->selected < s->ndisplayed - 1) {
5902 s->selected++;
5903 break;
5905 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5906 == NULL)
5907 /* can't scroll any further */
5908 break;
5909 tree_scroll_down(s, 1);
5910 break;
5911 case CTRL('d'):
5912 case 'd':
5913 nscroll /= 2;
5914 /* FALL THROUGH */
5915 case KEY_NPAGE:
5916 case CTRL('f'):
5917 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5918 == NULL) {
5919 /* can't scroll any further; move cursor down */
5920 if (s->selected < s->ndisplayed - 1)
5921 s->selected += MIN(nscroll,
5922 s->ndisplayed - s->selected - 1);
5923 break;
5925 tree_scroll_down(s, nscroll);
5926 break;
5927 case KEY_ENTER:
5928 case '\r':
5929 case KEY_BACKSPACE:
5930 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5931 struct tog_parent_tree *parent;
5932 /* user selected '..' */
5933 if (s->tree == s->root)
5934 break;
5935 parent = TAILQ_FIRST(&s->parents);
5936 TAILQ_REMOVE(&s->parents, parent,
5937 entry);
5938 got_object_tree_close(s->tree);
5939 s->tree = parent->tree;
5940 s->first_displayed_entry =
5941 parent->first_displayed_entry;
5942 s->selected_entry =
5943 parent->selected_entry;
5944 s->selected = parent->selected;
5945 free(parent);
5946 } else if (S_ISDIR(got_tree_entry_get_mode(
5947 s->selected_entry))) {
5948 struct got_tree_object *subtree;
5949 err = got_object_open_as_tree(&subtree, s->repo,
5950 got_tree_entry_get_id(s->selected_entry));
5951 if (err)
5952 break;
5953 err = tree_view_visit_subtree(s, subtree);
5954 if (err) {
5955 got_object_tree_close(subtree);
5956 break;
5958 } else if (S_ISREG(got_tree_entry_get_mode(
5959 s->selected_entry))) {
5960 struct tog_view *blame_view;
5961 int begin_x = view_is_parent_view(view) ?
5962 view_split_begin_x(view->begin_x) : 0;
5964 err = blame_tree_entry(&blame_view, begin_x,
5965 s->selected_entry, &s->parents,
5966 s->commit_id, s->repo);
5967 if (err)
5968 break;
5969 view->focussed = 0;
5970 blame_view->focussed = 1;
5971 if (view_is_parent_view(view)) {
5972 err = view_close_child(view);
5973 if (err)
5974 return err;
5975 view_set_child(view, blame_view);
5976 view->focus_child = 1;
5977 } else
5978 *new_view = blame_view;
5980 break;
5981 case KEY_RESIZE:
5982 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5983 s->selected = view->nlines - 4;
5984 break;
5985 default:
5986 break;
5989 return err;
5992 __dead static void
5993 usage_tree(void)
5995 endwin();
5996 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5997 getprogname());
5998 exit(1);
6001 static const struct got_error *
6002 cmd_tree(int argc, char *argv[])
6004 const struct got_error *error;
6005 struct got_repository *repo = NULL;
6006 struct got_worktree *worktree = NULL;
6007 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6008 struct got_object_id *commit_id = NULL;
6009 struct got_commit_object *commit = NULL;
6010 const char *commit_id_arg = NULL;
6011 char *label = NULL;
6012 struct got_reference *ref = NULL;
6013 const char *head_ref_name = NULL;
6014 int ch;
6015 struct tog_view *view;
6016 int *pack_fds = NULL;
6018 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6019 switch (ch) {
6020 case 'c':
6021 commit_id_arg = optarg;
6022 break;
6023 case 'r':
6024 repo_path = realpath(optarg, NULL);
6025 if (repo_path == NULL)
6026 return got_error_from_errno2("realpath",
6027 optarg);
6028 break;
6029 default:
6030 usage_tree();
6031 /* NOTREACHED */
6035 argc -= optind;
6036 argv += optind;
6038 if (argc > 1)
6039 usage_tree();
6041 error = got_repo_pack_fds_open(&pack_fds);
6042 if (error != NULL)
6043 goto done;
6045 if (repo_path == NULL) {
6046 cwd = getcwd(NULL, 0);
6047 if (cwd == NULL)
6048 return got_error_from_errno("getcwd");
6049 error = got_worktree_open(&worktree, cwd);
6050 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6051 goto done;
6052 if (worktree)
6053 repo_path =
6054 strdup(got_worktree_get_repo_path(worktree));
6055 else
6056 repo_path = strdup(cwd);
6057 if (repo_path == NULL) {
6058 error = got_error_from_errno("strdup");
6059 goto done;
6063 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6064 if (error != NULL)
6065 goto done;
6067 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6068 repo, worktree);
6069 if (error)
6070 goto done;
6072 init_curses();
6074 error = apply_unveil(got_repo_get_path(repo), NULL);
6075 if (error)
6076 goto done;
6078 error = tog_load_refs(repo, 0);
6079 if (error)
6080 goto done;
6082 if (commit_id_arg == NULL) {
6083 error = got_repo_match_object_id(&commit_id, &label,
6084 worktree ? got_worktree_get_head_ref_name(worktree) :
6085 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6086 if (error)
6087 goto done;
6088 head_ref_name = label;
6089 } else {
6090 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6091 if (error == NULL)
6092 head_ref_name = got_ref_get_name(ref);
6093 else if (error->code != GOT_ERR_NOT_REF)
6094 goto done;
6095 error = got_repo_match_object_id(&commit_id, NULL,
6096 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6097 if (error)
6098 goto done;
6101 error = got_object_open_as_commit(&commit, repo, commit_id);
6102 if (error)
6103 goto done;
6105 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6106 if (view == NULL) {
6107 error = got_error_from_errno("view_open");
6108 goto done;
6110 error = open_tree_view(view, commit_id, head_ref_name, repo);
6111 if (error)
6112 goto done;
6113 if (!got_path_is_root_dir(in_repo_path)) {
6114 error = tree_view_walk_path(&view->state.tree, commit,
6115 in_repo_path);
6116 if (error)
6117 goto done;
6120 if (worktree) {
6121 /* Release work tree lock. */
6122 got_worktree_close(worktree);
6123 worktree = NULL;
6125 error = view_loop(view);
6126 done:
6127 free(repo_path);
6128 free(cwd);
6129 free(commit_id);
6130 free(label);
6131 if (ref)
6132 got_ref_close(ref);
6133 if (repo) {
6134 const struct got_error *close_err = got_repo_close(repo);
6135 if (error == NULL)
6136 error = close_err;
6138 if (pack_fds) {
6139 const struct got_error *pack_err =
6140 got_repo_pack_fds_close(pack_fds);
6141 if (error == NULL)
6142 error = pack_err;
6144 tog_free_refs();
6145 return error;
6148 static const struct got_error *
6149 ref_view_load_refs(struct tog_ref_view_state *s)
6151 struct got_reflist_entry *sre;
6152 struct tog_reflist_entry *re;
6154 s->nrefs = 0;
6155 TAILQ_FOREACH(sre, &tog_refs, entry) {
6156 if (strncmp(got_ref_get_name(sre->ref),
6157 "refs/got/", 9) == 0 &&
6158 strncmp(got_ref_get_name(sre->ref),
6159 "refs/got/backup/", 16) != 0)
6160 continue;
6162 re = malloc(sizeof(*re));
6163 if (re == NULL)
6164 return got_error_from_errno("malloc");
6166 re->ref = got_ref_dup(sre->ref);
6167 if (re->ref == NULL)
6168 return got_error_from_errno("got_ref_dup");
6169 re->idx = s->nrefs++;
6170 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6173 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6174 return NULL;
6177 void
6178 ref_view_free_refs(struct tog_ref_view_state *s)
6180 struct tog_reflist_entry *re;
6182 while (!TAILQ_EMPTY(&s->refs)) {
6183 re = TAILQ_FIRST(&s->refs);
6184 TAILQ_REMOVE(&s->refs, re, entry);
6185 got_ref_close(re->ref);
6186 free(re);
6190 static const struct got_error *
6191 open_ref_view(struct tog_view *view, struct got_repository *repo)
6193 const struct got_error *err = NULL;
6194 struct tog_ref_view_state *s = &view->state.ref;
6196 s->selected_entry = 0;
6197 s->repo = repo;
6199 TAILQ_INIT(&s->refs);
6200 STAILQ_INIT(&s->colors);
6202 err = ref_view_load_refs(s);
6203 if (err)
6204 return err;
6206 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6207 err = add_color(&s->colors, "^refs/heads/",
6208 TOG_COLOR_REFS_HEADS,
6209 get_color_value("TOG_COLOR_REFS_HEADS"));
6210 if (err)
6211 goto done;
6213 err = add_color(&s->colors, "^refs/tags/",
6214 TOG_COLOR_REFS_TAGS,
6215 get_color_value("TOG_COLOR_REFS_TAGS"));
6216 if (err)
6217 goto done;
6219 err = add_color(&s->colors, "^refs/remotes/",
6220 TOG_COLOR_REFS_REMOTES,
6221 get_color_value("TOG_COLOR_REFS_REMOTES"));
6222 if (err)
6223 goto done;
6225 err = add_color(&s->colors, "^refs/got/backup/",
6226 TOG_COLOR_REFS_BACKUP,
6227 get_color_value("TOG_COLOR_REFS_BACKUP"));
6228 if (err)
6229 goto done;
6232 view->show = show_ref_view;
6233 view->input = input_ref_view;
6234 view->close = close_ref_view;
6235 view->search_start = search_start_ref_view;
6236 view->search_next = search_next_ref_view;
6237 done:
6238 if (err)
6239 free_colors(&s->colors);
6240 return err;
6243 static const struct got_error *
6244 close_ref_view(struct tog_view *view)
6246 struct tog_ref_view_state *s = &view->state.ref;
6248 ref_view_free_refs(s);
6249 free_colors(&s->colors);
6251 return NULL;
6254 static const struct got_error *
6255 resolve_reflist_entry(struct got_object_id **commit_id,
6256 struct tog_reflist_entry *re, struct got_repository *repo)
6258 const struct got_error *err = NULL;
6259 struct got_object_id *obj_id;
6260 struct got_tag_object *tag = NULL;
6261 int obj_type;
6263 *commit_id = NULL;
6265 err = got_ref_resolve(&obj_id, repo, re->ref);
6266 if (err)
6267 return err;
6269 err = got_object_get_type(&obj_type, repo, obj_id);
6270 if (err)
6271 goto done;
6273 switch (obj_type) {
6274 case GOT_OBJ_TYPE_COMMIT:
6275 *commit_id = obj_id;
6276 break;
6277 case GOT_OBJ_TYPE_TAG:
6278 err = got_object_open_as_tag(&tag, repo, obj_id);
6279 if (err)
6280 goto done;
6281 free(obj_id);
6282 err = got_object_get_type(&obj_type, repo,
6283 got_object_tag_get_object_id(tag));
6284 if (err)
6285 goto done;
6286 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6287 err = got_error(GOT_ERR_OBJ_TYPE);
6288 goto done;
6290 *commit_id = got_object_id_dup(
6291 got_object_tag_get_object_id(tag));
6292 if (*commit_id == NULL) {
6293 err = got_error_from_errno("got_object_id_dup");
6294 goto done;
6296 break;
6297 default:
6298 err = got_error(GOT_ERR_OBJ_TYPE);
6299 break;
6302 done:
6303 if (tag)
6304 got_object_tag_close(tag);
6305 if (err) {
6306 free(*commit_id);
6307 *commit_id = NULL;
6309 return err;
6312 static const struct got_error *
6313 log_ref_entry(struct tog_view **new_view, int begin_x,
6314 struct tog_reflist_entry *re, struct got_repository *repo)
6316 struct tog_view *log_view;
6317 const struct got_error *err = NULL;
6318 struct got_object_id *commit_id = NULL;
6320 *new_view = NULL;
6322 err = resolve_reflist_entry(&commit_id, re, repo);
6323 if (err) {
6324 if (err->code != GOT_ERR_OBJ_TYPE)
6325 return err;
6326 else
6327 return NULL;
6330 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6331 if (log_view == NULL) {
6332 err = got_error_from_errno("view_open");
6333 goto done;
6336 err = open_log_view(log_view, commit_id, repo,
6337 got_ref_get_name(re->ref), "", 0);
6338 done:
6339 if (err)
6340 view_close(log_view);
6341 else
6342 *new_view = log_view;
6343 free(commit_id);
6344 return err;
6347 static void
6348 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6350 struct tog_reflist_entry *re;
6351 int i = 0;
6353 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6354 return;
6356 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6357 while (i++ < maxscroll) {
6358 if (re == NULL)
6359 break;
6360 s->first_displayed_entry = re;
6361 re = TAILQ_PREV(re, tog_reflist_head, entry);
6365 static void
6366 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6368 struct tog_reflist_entry *next, *last;
6369 int n = 0;
6371 if (s->first_displayed_entry)
6372 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6373 else
6374 next = TAILQ_FIRST(&s->refs);
6376 last = s->last_displayed_entry;
6377 while (next && last && n++ < maxscroll) {
6378 last = TAILQ_NEXT(last, entry);
6379 if (last) {
6380 s->first_displayed_entry = next;
6381 next = TAILQ_NEXT(next, entry);
6386 static const struct got_error *
6387 search_start_ref_view(struct tog_view *view)
6389 struct tog_ref_view_state *s = &view->state.ref;
6391 s->matched_entry = NULL;
6392 return NULL;
6395 static int
6396 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6398 regmatch_t regmatch;
6400 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6401 0) == 0;
6404 static const struct got_error *
6405 search_next_ref_view(struct tog_view *view)
6407 struct tog_ref_view_state *s = &view->state.ref;
6408 struct tog_reflist_entry *re = NULL;
6410 if (!view->searching) {
6411 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6412 return NULL;
6415 if (s->matched_entry) {
6416 if (view->searching == TOG_SEARCH_FORWARD) {
6417 if (s->selected_entry)
6418 re = TAILQ_NEXT(s->selected_entry, entry);
6419 else
6420 re = TAILQ_PREV(s->selected_entry,
6421 tog_reflist_head, entry);
6422 } else {
6423 if (s->selected_entry == NULL)
6424 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6425 else
6426 re = TAILQ_PREV(s->selected_entry,
6427 tog_reflist_head, entry);
6429 } else {
6430 if (s->selected_entry)
6431 re = s->selected_entry;
6432 else if (view->searching == TOG_SEARCH_FORWARD)
6433 re = TAILQ_FIRST(&s->refs);
6434 else
6435 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6438 while (1) {
6439 if (re == NULL) {
6440 if (s->matched_entry == NULL) {
6441 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6442 return NULL;
6444 if (view->searching == TOG_SEARCH_FORWARD)
6445 re = TAILQ_FIRST(&s->refs);
6446 else
6447 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6450 if (match_reflist_entry(re, &view->regex)) {
6451 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6452 s->matched_entry = re;
6453 break;
6456 if (view->searching == TOG_SEARCH_FORWARD)
6457 re = TAILQ_NEXT(re, entry);
6458 else
6459 re = TAILQ_PREV(re, tog_reflist_head, entry);
6462 if (s->matched_entry) {
6463 s->first_displayed_entry = s->matched_entry;
6464 s->selected = 0;
6467 return NULL;
6470 static const struct got_error *
6471 show_ref_view(struct tog_view *view)
6473 const struct got_error *err = NULL;
6474 struct tog_ref_view_state *s = &view->state.ref;
6475 struct tog_reflist_entry *re;
6476 char *line = NULL;
6477 wchar_t *wline;
6478 struct tog_color *tc;
6479 int width, n;
6480 int limit = view->nlines;
6482 werase(view->window);
6484 s->ndisplayed = 0;
6486 if (limit == 0)
6487 return NULL;
6489 re = s->first_displayed_entry;
6491 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6492 s->nrefs) == -1)
6493 return got_error_from_errno("asprintf");
6495 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6496 if (err) {
6497 free(line);
6498 return err;
6500 if (view_needs_focus_indication(view))
6501 wstandout(view->window);
6502 waddwstr(view->window, wline);
6503 if (view_needs_focus_indication(view))
6504 wstandend(view->window);
6505 free(wline);
6506 wline = NULL;
6507 free(line);
6508 line = NULL;
6509 if (width < view->ncols - 1)
6510 waddch(view->window, '\n');
6511 if (--limit <= 0)
6512 return NULL;
6514 n = 0;
6515 while (re && limit > 0) {
6516 char *line = NULL;
6517 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6519 if (s->show_date) {
6520 struct got_commit_object *ci;
6521 struct got_tag_object *tag;
6522 struct got_object_id *id;
6523 struct tm tm;
6524 time_t t;
6526 err = got_ref_resolve(&id, s->repo, re->ref);
6527 if (err)
6528 return err;
6529 err = got_object_open_as_tag(&tag, s->repo, id);
6530 if (err) {
6531 if (err->code != GOT_ERR_OBJ_TYPE) {
6532 free(id);
6533 return err;
6535 err = got_object_open_as_commit(&ci, s->repo,
6536 id);
6537 if (err) {
6538 free(id);
6539 return err;
6541 t = got_object_commit_get_committer_time(ci);
6542 got_object_commit_close(ci);
6543 } else {
6544 t = got_object_tag_get_tagger_time(tag);
6545 got_object_tag_close(tag);
6547 free(id);
6548 if (gmtime_r(&t, &tm) == NULL)
6549 return got_error_from_errno("gmtime_r");
6550 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6551 return got_error(GOT_ERR_NO_SPACE);
6553 if (got_ref_is_symbolic(re->ref)) {
6554 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6555 ymd : "", got_ref_get_name(re->ref),
6556 got_ref_get_symref_target(re->ref)) == -1)
6557 return got_error_from_errno("asprintf");
6558 } else if (s->show_ids) {
6559 struct got_object_id *id;
6560 char *id_str;
6561 err = got_ref_resolve(&id, s->repo, re->ref);
6562 if (err)
6563 return err;
6564 err = got_object_id_str(&id_str, id);
6565 if (err) {
6566 free(id);
6567 return err;
6569 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6570 got_ref_get_name(re->ref), id_str) == -1) {
6571 err = got_error_from_errno("asprintf");
6572 free(id);
6573 free(id_str);
6574 return err;
6576 free(id);
6577 free(id_str);
6578 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6579 got_ref_get_name(re->ref)) == -1)
6580 return got_error_from_errno("asprintf");
6582 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6583 0, 0);
6584 if (err) {
6585 free(line);
6586 return err;
6588 if (n == s->selected) {
6589 if (view->focussed)
6590 wstandout(view->window);
6591 s->selected_entry = re;
6593 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6594 if (tc)
6595 wattr_on(view->window,
6596 COLOR_PAIR(tc->colorpair), NULL);
6597 waddwstr(view->window, wline);
6598 if (tc)
6599 wattr_off(view->window,
6600 COLOR_PAIR(tc->colorpair), NULL);
6601 if (width < view->ncols - 1)
6602 waddch(view->window, '\n');
6603 if (n == s->selected && view->focussed)
6604 wstandend(view->window);
6605 free(line);
6606 free(wline);
6607 wline = NULL;
6608 n++;
6609 s->ndisplayed++;
6610 s->last_displayed_entry = re;
6612 limit--;
6613 re = TAILQ_NEXT(re, entry);
6616 view_vborder(view);
6617 return err;
6620 static const struct got_error *
6621 browse_ref_tree(struct tog_view **new_view, int begin_x,
6622 struct tog_reflist_entry *re, struct got_repository *repo)
6624 const struct got_error *err = NULL;
6625 struct got_object_id *commit_id = NULL;
6626 struct tog_view *tree_view;
6628 *new_view = NULL;
6630 err = resolve_reflist_entry(&commit_id, re, repo);
6631 if (err) {
6632 if (err->code != GOT_ERR_OBJ_TYPE)
6633 return err;
6634 else
6635 return NULL;
6639 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6640 if (tree_view == NULL) {
6641 err = got_error_from_errno("view_open");
6642 goto done;
6645 err = open_tree_view(tree_view, commit_id,
6646 got_ref_get_name(re->ref), repo);
6647 if (err)
6648 goto done;
6650 *new_view = tree_view;
6651 done:
6652 free(commit_id);
6653 return err;
6655 static const struct got_error *
6656 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6658 const struct got_error *err = NULL;
6659 struct tog_ref_view_state *s = &view->state.ref;
6660 struct tog_view *log_view, *tree_view;
6661 struct tog_reflist_entry *re;
6662 int begin_x = 0, n, nscroll = view->nlines - 1;
6664 switch (ch) {
6665 case 'i':
6666 s->show_ids = !s->show_ids;
6667 break;
6668 case 'm':
6669 s->show_date = !s->show_date;
6670 break;
6671 case 'o':
6672 s->sort_by_date = !s->sort_by_date;
6673 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6674 got_ref_cmp_by_commit_timestamp_descending :
6675 tog_ref_cmp_by_name, s->repo);
6676 if (err)
6677 break;
6678 got_reflist_object_id_map_free(tog_refs_idmap);
6679 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6680 &tog_refs, s->repo);
6681 if (err)
6682 break;
6683 ref_view_free_refs(s);
6684 err = ref_view_load_refs(s);
6685 break;
6686 case KEY_ENTER:
6687 case '\r':
6688 if (!s->selected_entry)
6689 break;
6690 if (view_is_parent_view(view))
6691 begin_x = view_split_begin_x(view->begin_x);
6692 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6693 s->repo);
6694 view->focussed = 0;
6695 log_view->focussed = 1;
6696 if (view_is_parent_view(view)) {
6697 err = view_close_child(view);
6698 if (err)
6699 return err;
6700 view_set_child(view, log_view);
6701 view->focus_child = 1;
6702 } else
6703 *new_view = log_view;
6704 break;
6705 case 't':
6706 if (!s->selected_entry)
6707 break;
6708 if (view_is_parent_view(view))
6709 begin_x = view_split_begin_x(view->begin_x);
6710 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6711 s->repo);
6712 if (err || tree_view == NULL)
6713 break;
6714 view->focussed = 0;
6715 tree_view->focussed = 1;
6716 if (view_is_parent_view(view)) {
6717 err = view_close_child(view);
6718 if (err)
6719 return err;
6720 view_set_child(view, tree_view);
6721 view->focus_child = 1;
6722 } else
6723 *new_view = tree_view;
6724 break;
6725 case 'g':
6726 case KEY_HOME:
6727 s->selected = 0;
6728 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6729 break;
6730 case 'G':
6731 case KEY_END:
6732 s->selected = 0;
6733 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6734 for (n = 0; n < view->nlines - 1; n++) {
6735 if (re == NULL)
6736 break;
6737 s->first_displayed_entry = re;
6738 re = TAILQ_PREV(re, tog_reflist_head, entry);
6740 if (n > 0)
6741 s->selected = n - 1;
6742 break;
6743 case 'k':
6744 case KEY_UP:
6745 case CTRL('p'):
6746 if (s->selected > 0) {
6747 s->selected--;
6748 break;
6750 ref_scroll_up(s, 1);
6751 break;
6752 case CTRL('u'):
6753 case 'u':
6754 nscroll /= 2;
6755 /* FALL THROUGH */
6756 case KEY_PPAGE:
6757 case CTRL('b'):
6758 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6759 s->selected -= MIN(nscroll, s->selected);
6760 ref_scroll_up(s, MAX(0, nscroll));
6761 break;
6762 case 'j':
6763 case KEY_DOWN:
6764 case CTRL('n'):
6765 if (s->selected < s->ndisplayed - 1) {
6766 s->selected++;
6767 break;
6769 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6770 /* can't scroll any further */
6771 break;
6772 ref_scroll_down(s, 1);
6773 break;
6774 case CTRL('d'):
6775 case 'd':
6776 nscroll /= 2;
6777 /* FALL THROUGH */
6778 case KEY_NPAGE:
6779 case CTRL('f'):
6780 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6781 /* can't scroll any further; move cursor down */
6782 if (s->selected < s->ndisplayed - 1)
6783 s->selected += MIN(nscroll,
6784 s->ndisplayed - s->selected - 1);
6785 break;
6787 ref_scroll_down(s, nscroll);
6788 break;
6789 case CTRL('l'):
6790 tog_free_refs();
6791 err = tog_load_refs(s->repo, s->sort_by_date);
6792 if (err)
6793 break;
6794 ref_view_free_refs(s);
6795 err = ref_view_load_refs(s);
6796 break;
6797 case KEY_RESIZE:
6798 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6799 s->selected = view->nlines - 2;
6800 break;
6801 default:
6802 break;
6805 return err;
6808 __dead static void
6809 usage_ref(void)
6811 endwin();
6812 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6813 getprogname());
6814 exit(1);
6817 static const struct got_error *
6818 cmd_ref(int argc, char *argv[])
6820 const struct got_error *error;
6821 struct got_repository *repo = NULL;
6822 struct got_worktree *worktree = NULL;
6823 char *cwd = NULL, *repo_path = NULL;
6824 int ch;
6825 struct tog_view *view;
6826 int *pack_fds = NULL;
6828 while ((ch = getopt(argc, argv, "r:")) != -1) {
6829 switch (ch) {
6830 case 'r':
6831 repo_path = realpath(optarg, NULL);
6832 if (repo_path == NULL)
6833 return got_error_from_errno2("realpath",
6834 optarg);
6835 break;
6836 default:
6837 usage_ref();
6838 /* NOTREACHED */
6842 argc -= optind;
6843 argv += optind;
6845 if (argc > 1)
6846 usage_ref();
6848 error = got_repo_pack_fds_open(&pack_fds);
6849 if (error != NULL)
6850 goto done;
6852 if (repo_path == NULL) {
6853 cwd = getcwd(NULL, 0);
6854 if (cwd == NULL)
6855 return got_error_from_errno("getcwd");
6856 error = got_worktree_open(&worktree, cwd);
6857 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6858 goto done;
6859 if (worktree)
6860 repo_path =
6861 strdup(got_worktree_get_repo_path(worktree));
6862 else
6863 repo_path = strdup(cwd);
6864 if (repo_path == NULL) {
6865 error = got_error_from_errno("strdup");
6866 goto done;
6870 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6871 if (error != NULL)
6872 goto done;
6874 init_curses();
6876 error = apply_unveil(got_repo_get_path(repo), NULL);
6877 if (error)
6878 goto done;
6880 error = tog_load_refs(repo, 0);
6881 if (error)
6882 goto done;
6884 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6885 if (view == NULL) {
6886 error = got_error_from_errno("view_open");
6887 goto done;
6890 error = open_ref_view(view, repo);
6891 if (error)
6892 goto done;
6894 if (worktree) {
6895 /* Release work tree lock. */
6896 got_worktree_close(worktree);
6897 worktree = NULL;
6899 error = view_loop(view);
6900 done:
6901 free(repo_path);
6902 free(cwd);
6903 if (repo) {
6904 const struct got_error *close_err = got_repo_close(repo);
6905 if (close_err)
6906 error = close_err;
6908 if (pack_fds) {
6909 const struct got_error *pack_err =
6910 got_repo_pack_fds_close(pack_fds);
6911 if (error == NULL)
6912 error = pack_err;
6914 tog_free_refs();
6915 return error;
6918 static void
6919 list_commands(FILE *fp)
6921 size_t i;
6923 fprintf(fp, "commands:");
6924 for (i = 0; i < nitems(tog_commands); i++) {
6925 const struct tog_cmd *cmd = &tog_commands[i];
6926 fprintf(fp, " %s", cmd->name);
6928 fputc('\n', fp);
6931 __dead static void
6932 usage(int hflag, int status)
6934 FILE *fp = (status == 0) ? stdout : stderr;
6936 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6937 getprogname());
6938 if (hflag) {
6939 fprintf(fp, "lazy usage: %s path\n", getprogname());
6940 list_commands(fp);
6942 exit(status);
6945 static char **
6946 make_argv(int argc, ...)
6948 va_list ap;
6949 char **argv;
6950 int i;
6952 va_start(ap, argc);
6954 argv = calloc(argc, sizeof(char *));
6955 if (argv == NULL)
6956 err(1, "calloc");
6957 for (i = 0; i < argc; i++) {
6958 argv[i] = strdup(va_arg(ap, char *));
6959 if (argv[i] == NULL)
6960 err(1, "strdup");
6963 va_end(ap);
6964 return argv;
6968 * Try to convert 'tog path' into a 'tog log path' command.
6969 * The user could simply have mistyped the command rather than knowingly
6970 * provided a path. So check whether argv[0] can in fact be resolved
6971 * to a path in the HEAD commit and print a special error if not.
6972 * This hack is for mpi@ <3
6974 static const struct got_error *
6975 tog_log_with_path(int argc, char *argv[])
6977 const struct got_error *error = NULL, *close_err;
6978 const struct tog_cmd *cmd = NULL;
6979 struct got_repository *repo = NULL;
6980 struct got_worktree *worktree = NULL;
6981 struct got_object_id *commit_id = NULL, *id = NULL;
6982 struct got_commit_object *commit = NULL;
6983 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6984 char *commit_id_str = NULL, **cmd_argv = NULL;
6985 int *pack_fds = NULL;
6987 cwd = getcwd(NULL, 0);
6988 if (cwd == NULL)
6989 return got_error_from_errno("getcwd");
6991 error = got_repo_pack_fds_open(&pack_fds);
6992 if (error != NULL)
6993 goto done;
6995 error = got_worktree_open(&worktree, cwd);
6996 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6997 goto done;
6999 if (worktree)
7000 repo_path = strdup(got_worktree_get_repo_path(worktree));
7001 else
7002 repo_path = strdup(cwd);
7003 if (repo_path == NULL) {
7004 error = got_error_from_errno("strdup");
7005 goto done;
7008 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7009 if (error != NULL)
7010 goto done;
7012 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7013 repo, worktree);
7014 if (error)
7015 goto done;
7017 error = tog_load_refs(repo, 0);
7018 if (error)
7019 goto done;
7020 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7021 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7022 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7023 if (error)
7024 goto done;
7026 if (worktree) {
7027 got_worktree_close(worktree);
7028 worktree = NULL;
7031 error = got_object_open_as_commit(&commit, repo, commit_id);
7032 if (error)
7033 goto done;
7035 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7036 if (error) {
7037 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7038 goto done;
7039 fprintf(stderr, "%s: '%s' is no known command or path\n",
7040 getprogname(), argv[0]);
7041 usage(1, 1);
7042 /* not reached */
7045 close_err = got_repo_close(repo);
7046 if (error == NULL)
7047 error = close_err;
7048 repo = NULL;
7050 error = got_object_id_str(&commit_id_str, commit_id);
7051 if (error)
7052 goto done;
7054 cmd = &tog_commands[0]; /* log */
7055 argc = 4;
7056 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7057 error = cmd->cmd_main(argc, cmd_argv);
7058 done:
7059 if (repo) {
7060 close_err = got_repo_close(repo);
7061 if (error == NULL)
7062 error = close_err;
7064 if (commit)
7065 got_object_commit_close(commit);
7066 if (worktree)
7067 got_worktree_close(worktree);
7068 if (pack_fds) {
7069 const struct got_error *pack_err =
7070 got_repo_pack_fds_close(pack_fds);
7071 if (error == NULL)
7072 error = pack_err;
7074 free(id);
7075 free(commit_id_str);
7076 free(commit_id);
7077 free(cwd);
7078 free(repo_path);
7079 free(in_repo_path);
7080 if (cmd_argv) {
7081 int i;
7082 for (i = 0; i < argc; i++)
7083 free(cmd_argv[i]);
7084 free(cmd_argv);
7086 tog_free_refs();
7087 return error;
7090 int
7091 main(int argc, char *argv[])
7093 const struct got_error *error = NULL;
7094 const struct tog_cmd *cmd = NULL;
7095 int ch, hflag = 0, Vflag = 0;
7096 char **cmd_argv = NULL;
7097 static const struct option longopts[] = {
7098 { "version", no_argument, NULL, 'V' },
7099 { NULL, 0, NULL, 0}
7102 setlocale(LC_CTYPE, "");
7104 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7105 switch (ch) {
7106 case 'h':
7107 hflag = 1;
7108 break;
7109 case 'V':
7110 Vflag = 1;
7111 break;
7112 default:
7113 usage(hflag, 1);
7114 /* NOTREACHED */
7118 argc -= optind;
7119 argv += optind;
7120 optind = 1;
7121 optreset = 1;
7123 if (Vflag) {
7124 got_version_print_str();
7125 return 0;
7128 #ifndef PROFILE
7129 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7130 NULL) == -1)
7131 err(1, "pledge");
7132 #endif
7134 if (argc == 0) {
7135 if (hflag)
7136 usage(hflag, 0);
7137 /* Build an argument vector which runs a default command. */
7138 cmd = &tog_commands[0];
7139 argc = 1;
7140 cmd_argv = make_argv(argc, cmd->name);
7141 } else {
7142 size_t i;
7144 /* Did the user specify a command? */
7145 for (i = 0; i < nitems(tog_commands); i++) {
7146 if (strncmp(tog_commands[i].name, argv[0],
7147 strlen(argv[0])) == 0) {
7148 cmd = &tog_commands[i];
7149 break;
7154 if (cmd == NULL) {
7155 if (argc != 1)
7156 usage(0, 1);
7157 /* No command specified; try log with a path */
7158 error = tog_log_with_path(argc, argv);
7159 } else {
7160 if (hflag)
7161 cmd->cmd_usage();
7162 else
7163 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7166 endwin();
7167 putchar('\n');
7168 if (cmd_argv) {
7169 int i;
7170 for (i = 0; i < argc; i++)
7171 free(cmd_argv[i]);
7172 free(cmd_argv);
7175 if (error && error->code != GOT_ERR_CANCELLED)
7176 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7177 return 0;