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;
1246 p = realloc(dst, n + nb);
1247 if (p == NULL) {
1248 free(dst);
1249 return got_error_from_errno("realloc");
1252 dst = p;
1253 n += nb;
1254 memset(dst + sz, ' ', nb);
1255 sz += nb;
1256 } else
1257 dst[sz++] = src[idx];
1258 ++idx;
1261 dst[sz] = '\0';
1262 *ptr = dst;
1263 return NULL;
1267 * Skip leading nscroll columns of a wide character string.
1268 * Returns the index to the first character of the scrolled string.
1270 static const struct got_error *
1271 scroll_wline(int *scrollx , wchar_t *wline, int nscroll,
1272 int col_tab_align)
1274 int cols = 0;
1275 size_t wlen = wcslen(wline);
1276 int i = 0;
1278 *scrollx = 0;
1280 while (i < wlen && cols < nscroll) {
1281 int width = wcwidth(wline[i]);
1283 if (width == 0) {
1284 i++;
1285 continue;
1288 if (width == 1 || width == 2) {
1289 if (cols + width > nscroll)
1290 break;
1291 cols += width;
1292 i++;
1293 } else if (width == -1) {
1294 if (wline[i] == L'\t') {
1295 width = TABSIZE -
1296 ((cols + col_tab_align) % TABSIZE);
1297 } else {
1298 width = 1;
1299 wline[i] = L'.';
1301 if (cols + width > nscroll)
1302 break;
1303 cols += width;
1304 i++;
1305 } else
1306 return got_error_from_errno("wcwidth");
1309 *scrollx = i;
1310 return NULL;
1314 * Format a line for display, ensuring that it won't overflow a width limit.
1315 * With scrolling, the width returned refers to the scrolled version of the
1316 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1318 static const struct got_error *
1319 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1320 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1322 const struct got_error *err = NULL;
1323 int cols = 0;
1324 wchar_t *wline = NULL;
1325 char *exstr = NULL;
1326 size_t wlen;
1327 int i, scrollx = 0;
1329 *wlinep = NULL;
1330 *widthp = 0;
1332 if (expand) {
1333 err = expand_tab(&exstr, line);
1334 if (err)
1335 return err;
1338 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1339 free(exstr);
1340 if (err)
1341 return err;
1343 err = scroll_wline(&scrollx, wline, nscroll, col_tab_align);
1344 if (err)
1345 goto done;
1347 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1348 wline[wlen - 1] = L'\0';
1349 wlen--;
1351 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1352 wline[wlen - 1] = L'\0';
1353 wlen--;
1356 i = scrollx;
1357 while (i < wlen) {
1358 int width = wcwidth(wline[i]);
1360 if (width == 0) {
1361 i++;
1362 continue;
1365 if (width == 1 || width == 2) {
1366 if (cols + width > wlimit)
1367 break;
1368 cols += width;
1369 i++;
1370 } else if (width == -1) {
1371 if (wline[i] == L'\t') {
1372 width = TABSIZE -
1373 ((cols + col_tab_align) % TABSIZE);
1374 } else {
1375 width = 1;
1376 wline[i] = L'.';
1378 if (cols + width > wlimit)
1379 break;
1380 cols += width;
1381 i++;
1382 } else {
1383 err = got_error_from_errno("wcwidth");
1384 goto done;
1387 wline[i] = L'\0';
1388 if (widthp)
1389 *widthp = cols;
1390 if (scrollxp)
1391 *scrollxp = scrollx;
1392 done:
1393 if (err)
1394 free(wline);
1395 else
1396 *wlinep = wline;
1397 return err;
1400 static const struct got_error*
1401 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1402 struct got_object_id *id, struct got_repository *repo)
1404 static const struct got_error *err = NULL;
1405 struct got_reflist_entry *re;
1406 char *s;
1407 const char *name;
1409 *refs_str = NULL;
1411 TAILQ_FOREACH(re, refs, entry) {
1412 struct got_tag_object *tag = NULL;
1413 struct got_object_id *ref_id;
1414 int cmp;
1416 name = got_ref_get_name(re->ref);
1417 if (strcmp(name, GOT_REF_HEAD) == 0)
1418 continue;
1419 if (strncmp(name, "refs/", 5) == 0)
1420 name += 5;
1421 if (strncmp(name, "got/", 4) == 0 &&
1422 strncmp(name, "got/backup/", 11) != 0)
1423 continue;
1424 if (strncmp(name, "heads/", 6) == 0)
1425 name += 6;
1426 if (strncmp(name, "remotes/", 8) == 0) {
1427 name += 8;
1428 s = strstr(name, "/" GOT_REF_HEAD);
1429 if (s != NULL && s[strlen(s)] == '\0')
1430 continue;
1432 err = got_ref_resolve(&ref_id, repo, re->ref);
1433 if (err)
1434 break;
1435 if (strncmp(name, "tags/", 5) == 0) {
1436 err = got_object_open_as_tag(&tag, repo, ref_id);
1437 if (err) {
1438 if (err->code != GOT_ERR_OBJ_TYPE) {
1439 free(ref_id);
1440 break;
1442 /* Ref points at something other than a tag. */
1443 err = NULL;
1444 tag = NULL;
1447 cmp = got_object_id_cmp(tag ?
1448 got_object_tag_get_object_id(tag) : ref_id, id);
1449 free(ref_id);
1450 if (tag)
1451 got_object_tag_close(tag);
1452 if (cmp != 0)
1453 continue;
1454 s = *refs_str;
1455 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1456 s ? ", " : "", name) == -1) {
1457 err = got_error_from_errno("asprintf");
1458 free(s);
1459 *refs_str = NULL;
1460 break;
1462 free(s);
1465 return err;
1468 static const struct got_error *
1469 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1470 int col_tab_align)
1472 char *smallerthan;
1474 smallerthan = strchr(author, '<');
1475 if (smallerthan && smallerthan[1] != '\0')
1476 author = smallerthan + 1;
1477 author[strcspn(author, "@>")] = '\0';
1478 return format_line(wauthor, author_width, NULL, author, 0, limit,
1479 col_tab_align, 0);
1482 static const struct got_error *
1483 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1484 struct got_object_id *id, const size_t date_display_cols,
1485 int author_display_cols)
1487 struct tog_log_view_state *s = &view->state.log;
1488 const struct got_error *err = NULL;
1489 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1490 char *logmsg0 = NULL, *logmsg = NULL;
1491 char *author = NULL;
1492 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1493 int author_width, logmsg_width;
1494 char *newline, *line = NULL;
1495 int col, limit, scrollx;
1496 const int avail = view->ncols;
1497 struct tm tm;
1498 time_t committer_time;
1499 struct tog_color *tc;
1501 committer_time = got_object_commit_get_committer_time(commit);
1502 if (gmtime_r(&committer_time, &tm) == NULL)
1503 return got_error_from_errno("gmtime_r");
1504 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1505 return got_error(GOT_ERR_NO_SPACE);
1507 if (avail <= date_display_cols)
1508 limit = MIN(sizeof(datebuf) - 1, avail);
1509 else
1510 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1511 tc = get_color(&s->colors, TOG_COLOR_DATE);
1512 if (tc)
1513 wattr_on(view->window,
1514 COLOR_PAIR(tc->colorpair), NULL);
1515 waddnstr(view->window, datebuf, limit);
1516 if (tc)
1517 wattr_off(view->window,
1518 COLOR_PAIR(tc->colorpair), NULL);
1519 col = limit;
1520 if (col > avail)
1521 goto done;
1523 if (avail >= 120) {
1524 char *id_str;
1525 err = got_object_id_str(&id_str, id);
1526 if (err)
1527 goto done;
1528 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1529 if (tc)
1530 wattr_on(view->window,
1531 COLOR_PAIR(tc->colorpair), NULL);
1532 wprintw(view->window, "%.8s ", id_str);
1533 if (tc)
1534 wattr_off(view->window,
1535 COLOR_PAIR(tc->colorpair), NULL);
1536 free(id_str);
1537 col += 9;
1538 if (col > avail)
1539 goto done;
1542 author = strdup(got_object_commit_get_author(commit));
1543 if (author == NULL) {
1544 err = got_error_from_errno("strdup");
1545 goto done;
1547 err = format_author(&wauthor, &author_width, author, avail - col, col);
1548 if (err)
1549 goto done;
1550 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1551 if (tc)
1552 wattr_on(view->window,
1553 COLOR_PAIR(tc->colorpair), NULL);
1554 waddwstr(view->window, wauthor);
1555 if (tc)
1556 wattr_off(view->window,
1557 COLOR_PAIR(tc->colorpair), NULL);
1558 col += author_width;
1559 while (col < avail && author_width < author_display_cols + 2) {
1560 waddch(view->window, ' ');
1561 col++;
1562 author_width++;
1564 if (col > avail)
1565 goto done;
1567 err = got_object_commit_get_logmsg(&logmsg0, commit);
1568 if (err)
1569 goto done;
1570 logmsg = logmsg0;
1571 while (*logmsg == '\n')
1572 logmsg++;
1573 newline = strchr(logmsg, '\n');
1574 if (newline)
1575 *newline = '\0';
1576 limit = avail - col;
1577 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1578 limit, col, 1);
1579 if (err)
1580 goto done;
1581 waddwstr(view->window, &wlogmsg[scrollx]);
1582 col += MAX(logmsg_width, 0);
1583 while (col < avail) {
1584 waddch(view->window, ' ');
1585 col++;
1587 done:
1588 free(logmsg0);
1589 free(wlogmsg);
1590 free(author);
1591 free(wauthor);
1592 free(line);
1593 return err;
1596 static struct commit_queue_entry *
1597 alloc_commit_queue_entry(struct got_commit_object *commit,
1598 struct got_object_id *id)
1600 struct commit_queue_entry *entry;
1602 entry = calloc(1, sizeof(*entry));
1603 if (entry == NULL)
1604 return NULL;
1606 entry->id = id;
1607 entry->commit = commit;
1608 return entry;
1611 static void
1612 pop_commit(struct commit_queue *commits)
1614 struct commit_queue_entry *entry;
1616 entry = TAILQ_FIRST(&commits->head);
1617 TAILQ_REMOVE(&commits->head, entry, entry);
1618 got_object_commit_close(entry->commit);
1619 commits->ncommits--;
1620 /* Don't free entry->id! It is owned by the commit graph. */
1621 free(entry);
1624 static void
1625 free_commits(struct commit_queue *commits)
1627 while (!TAILQ_EMPTY(&commits->head))
1628 pop_commit(commits);
1631 static const struct got_error *
1632 match_commit(int *have_match, struct got_object_id *id,
1633 struct got_commit_object *commit, regex_t *regex)
1635 const struct got_error *err = NULL;
1636 regmatch_t regmatch;
1637 char *id_str = NULL, *logmsg = NULL;
1639 *have_match = 0;
1641 err = got_object_id_str(&id_str, id);
1642 if (err)
1643 return err;
1645 err = got_object_commit_get_logmsg(&logmsg, commit);
1646 if (err)
1647 goto done;
1649 if (regexec(regex, got_object_commit_get_author(commit), 1,
1650 &regmatch, 0) == 0 ||
1651 regexec(regex, got_object_commit_get_committer(commit), 1,
1652 &regmatch, 0) == 0 ||
1653 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1654 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1655 *have_match = 1;
1656 done:
1657 free(id_str);
1658 free(logmsg);
1659 return err;
1662 static const struct got_error *
1663 queue_commits(struct tog_log_thread_args *a)
1665 const struct got_error *err = NULL;
1668 * We keep all commits open throughout the lifetime of the log
1669 * view in order to avoid having to re-fetch commits from disk
1670 * while updating the display.
1672 do {
1673 struct got_object_id *id;
1674 struct got_commit_object *commit;
1675 struct commit_queue_entry *entry;
1676 int errcode;
1678 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1679 NULL, NULL);
1680 if (err || id == NULL)
1681 break;
1683 err = got_object_open_as_commit(&commit, a->repo, id);
1684 if (err)
1685 break;
1686 entry = alloc_commit_queue_entry(commit, id);
1687 if (entry == NULL) {
1688 err = got_error_from_errno("alloc_commit_queue_entry");
1689 break;
1692 errcode = pthread_mutex_lock(&tog_mutex);
1693 if (errcode) {
1694 err = got_error_set_errno(errcode,
1695 "pthread_mutex_lock");
1696 break;
1699 entry->idx = a->commits->ncommits;
1700 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1701 a->commits->ncommits++;
1703 if (*a->searching == TOG_SEARCH_FORWARD &&
1704 !*a->search_next_done) {
1705 int have_match;
1706 err = match_commit(&have_match, id, commit, a->regex);
1707 if (err)
1708 break;
1709 if (have_match)
1710 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1713 errcode = pthread_mutex_unlock(&tog_mutex);
1714 if (errcode && err == NULL)
1715 err = got_error_set_errno(errcode,
1716 "pthread_mutex_unlock");
1717 if (err)
1718 break;
1719 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1721 return err;
1724 static void
1725 select_commit(struct tog_log_view_state *s)
1727 struct commit_queue_entry *entry;
1728 int ncommits = 0;
1730 entry = s->first_displayed_entry;
1731 while (entry) {
1732 if (ncommits == s->selected) {
1733 s->selected_entry = entry;
1734 break;
1736 entry = TAILQ_NEXT(entry, entry);
1737 ncommits++;
1741 static const struct got_error *
1742 draw_commits(struct tog_view *view)
1744 const struct got_error *err = NULL;
1745 struct tog_log_view_state *s = &view->state.log;
1746 struct commit_queue_entry *entry = s->selected_entry;
1747 const int limit = view->nlines;
1748 int width;
1749 int ncommits, author_cols = 4;
1750 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1751 char *refs_str = NULL;
1752 wchar_t *wline;
1753 struct tog_color *tc;
1754 static const size_t date_display_cols = 12;
1756 if (s->selected_entry &&
1757 !(view->searching && view->search_next_done == 0)) {
1758 struct got_reflist_head *refs;
1759 err = got_object_id_str(&id_str, s->selected_entry->id);
1760 if (err)
1761 return err;
1762 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1763 s->selected_entry->id);
1764 if (refs) {
1765 err = build_refs_str(&refs_str, refs,
1766 s->selected_entry->id, s->repo);
1767 if (err)
1768 goto done;
1772 if (s->thread_args.commits_needed == 0)
1773 halfdelay(10); /* disable fast refresh */
1775 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1776 if (asprintf(&ncommits_str, " [%d/%d] %s",
1777 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1778 (view->searching && !view->search_next_done) ?
1779 "searching..." : "loading...") == -1) {
1780 err = got_error_from_errno("asprintf");
1781 goto done;
1783 } else {
1784 const char *search_str = NULL;
1786 if (view->searching) {
1787 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1788 search_str = "no more matches";
1789 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1790 search_str = "no matches found";
1791 else if (!view->search_next_done)
1792 search_str = "searching...";
1795 if (asprintf(&ncommits_str, " [%d/%d] %s",
1796 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1797 search_str ? search_str :
1798 (refs_str ? refs_str : "")) == -1) {
1799 err = got_error_from_errno("asprintf");
1800 goto done;
1804 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1805 if (asprintf(&header, "commit %s %s%s",
1806 id_str ? id_str : "........................................",
1807 s->in_repo_path, ncommits_str) == -1) {
1808 err = got_error_from_errno("asprintf");
1809 header = NULL;
1810 goto done;
1812 } else if (asprintf(&header, "commit %s%s",
1813 id_str ? id_str : "........................................",
1814 ncommits_str) == -1) {
1815 err = got_error_from_errno("asprintf");
1816 header = NULL;
1817 goto done;
1819 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1820 if (err)
1821 goto done;
1823 werase(view->window);
1825 if (view_needs_focus_indication(view))
1826 wstandout(view->window);
1827 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1828 if (tc)
1829 wattr_on(view->window,
1830 COLOR_PAIR(tc->colorpair), NULL);
1831 waddwstr(view->window, wline);
1832 if (tc)
1833 wattr_off(view->window,
1834 COLOR_PAIR(tc->colorpair), NULL);
1835 while (width < view->ncols) {
1836 waddch(view->window, ' ');
1837 width++;
1839 if (view_needs_focus_indication(view))
1840 wstandend(view->window);
1841 free(wline);
1842 if (limit <= 1)
1843 goto done;
1845 /* Grow author column size if necessary, and set view->maxx. */
1846 entry = s->first_displayed_entry;
1847 ncommits = 0;
1848 view->maxx = 0;
1849 while (entry) {
1850 char *author, *eol, *msg, *msg0;
1851 wchar_t *wauthor, *wmsg;
1852 int width;
1853 if (ncommits >= limit - 1)
1854 break;
1855 author = strdup(got_object_commit_get_author(entry->commit));
1856 if (author == NULL) {
1857 err = got_error_from_errno("strdup");
1858 goto done;
1860 err = format_author(&wauthor, &width, author, COLS,
1861 date_display_cols);
1862 if (author_cols < width)
1863 author_cols = width;
1864 free(wauthor);
1865 free(author);
1866 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1867 if (err)
1868 goto done;
1869 msg = msg0;
1870 while (*msg == '\n')
1871 ++msg;
1872 if ((eol = strchr(msg, '\n')))
1873 *eol = '\0';
1874 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1875 date_display_cols + author_cols, 0);
1876 if (err)
1877 goto done;
1878 view->maxx = MAX(view->maxx, width);
1879 free(msg0);
1880 free(wmsg);
1881 ncommits++;
1882 entry = TAILQ_NEXT(entry, entry);
1885 entry = s->first_displayed_entry;
1886 s->last_displayed_entry = s->first_displayed_entry;
1887 ncommits = 0;
1888 while (entry) {
1889 if (ncommits >= limit - 1)
1890 break;
1891 if (ncommits == s->selected)
1892 wstandout(view->window);
1893 err = draw_commit(view, entry->commit, entry->id,
1894 date_display_cols, author_cols);
1895 if (ncommits == s->selected)
1896 wstandend(view->window);
1897 if (err)
1898 goto done;
1899 ncommits++;
1900 s->last_displayed_entry = entry;
1901 entry = TAILQ_NEXT(entry, entry);
1904 view_vborder(view);
1905 done:
1906 free(id_str);
1907 free(refs_str);
1908 free(ncommits_str);
1909 free(header);
1910 return err;
1913 static void
1914 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1916 struct commit_queue_entry *entry;
1917 int nscrolled = 0;
1919 entry = TAILQ_FIRST(&s->commits.head);
1920 if (s->first_displayed_entry == entry)
1921 return;
1923 entry = s->first_displayed_entry;
1924 while (entry && nscrolled < maxscroll) {
1925 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1926 if (entry) {
1927 s->first_displayed_entry = entry;
1928 nscrolled++;
1933 static const struct got_error *
1934 trigger_log_thread(struct tog_view *view, int wait)
1936 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1937 int errcode;
1939 halfdelay(1); /* fast refresh while loading commits */
1941 while (ta->commits_needed > 0 || ta->load_all) {
1942 if (ta->log_complete)
1943 break;
1945 /* Wake the log thread. */
1946 errcode = pthread_cond_signal(&ta->need_commits);
1947 if (errcode)
1948 return got_error_set_errno(errcode,
1949 "pthread_cond_signal");
1952 * The mutex will be released while the view loop waits
1953 * in wgetch(), at which time the log thread will run.
1955 if (!wait)
1956 break;
1958 /* Display progress update in log view. */
1959 show_log_view(view);
1960 update_panels();
1961 doupdate();
1963 /* Wait right here while next commit is being loaded. */
1964 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1965 if (errcode)
1966 return got_error_set_errno(errcode,
1967 "pthread_cond_wait");
1969 /* Display progress update in log view. */
1970 show_log_view(view);
1971 update_panels();
1972 doupdate();
1975 return NULL;
1978 static const struct got_error *
1979 log_scroll_down(struct tog_view *view, int maxscroll)
1981 struct tog_log_view_state *s = &view->state.log;
1982 const struct got_error *err = NULL;
1983 struct commit_queue_entry *pentry;
1984 int nscrolled = 0, ncommits_needed;
1986 if (s->last_displayed_entry == NULL)
1987 return NULL;
1989 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1990 if (s->commits.ncommits < ncommits_needed &&
1991 !s->thread_args.log_complete) {
1993 * Ask the log thread for required amount of commits.
1995 s->thread_args.commits_needed += maxscroll;
1996 err = trigger_log_thread(view, 1);
1997 if (err)
1998 return err;
2001 do {
2002 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2003 if (pentry == NULL)
2004 break;
2006 s->last_displayed_entry = pentry;
2008 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2009 if (pentry == NULL)
2010 break;
2011 s->first_displayed_entry = pentry;
2012 } while (++nscrolled < maxscroll);
2014 return err;
2017 static const struct got_error *
2018 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2019 struct got_commit_object *commit, struct got_object_id *commit_id,
2020 struct tog_view *log_view, struct got_repository *repo)
2022 const struct got_error *err;
2023 struct got_object_qid *parent_id;
2024 struct tog_view *diff_view;
2026 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2027 if (diff_view == NULL)
2028 return got_error_from_errno("view_open");
2030 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2031 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2032 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2033 if (err == NULL)
2034 *new_view = diff_view;
2035 return err;
2038 static const struct got_error *
2039 tree_view_visit_subtree(struct tog_tree_view_state *s,
2040 struct got_tree_object *subtree)
2042 struct tog_parent_tree *parent;
2044 parent = calloc(1, sizeof(*parent));
2045 if (parent == NULL)
2046 return got_error_from_errno("calloc");
2048 parent->tree = s->tree;
2049 parent->first_displayed_entry = s->first_displayed_entry;
2050 parent->selected_entry = s->selected_entry;
2051 parent->selected = s->selected;
2052 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2053 s->tree = subtree;
2054 s->selected = 0;
2055 s->first_displayed_entry = NULL;
2056 return NULL;
2059 static const struct got_error *
2060 tree_view_walk_path(struct tog_tree_view_state *s,
2061 struct got_commit_object *commit, const char *path)
2063 const struct got_error *err = NULL;
2064 struct got_tree_object *tree = NULL;
2065 const char *p;
2066 char *slash, *subpath = NULL;
2068 /* Walk the path and open corresponding tree objects. */
2069 p = path;
2070 while (*p) {
2071 struct got_tree_entry *te;
2072 struct got_object_id *tree_id;
2073 char *te_name;
2075 while (p[0] == '/')
2076 p++;
2078 /* Ensure the correct subtree entry is selected. */
2079 slash = strchr(p, '/');
2080 if (slash == NULL)
2081 te_name = strdup(p);
2082 else
2083 te_name = strndup(p, slash - p);
2084 if (te_name == NULL) {
2085 err = got_error_from_errno("strndup");
2086 break;
2088 te = got_object_tree_find_entry(s->tree, te_name);
2089 if (te == NULL) {
2090 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2091 free(te_name);
2092 break;
2094 free(te_name);
2095 s->first_displayed_entry = s->selected_entry = te;
2097 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2098 break; /* jump to this file's entry */
2100 slash = strchr(p, '/');
2101 if (slash)
2102 subpath = strndup(path, slash - path);
2103 else
2104 subpath = strdup(path);
2105 if (subpath == NULL) {
2106 err = got_error_from_errno("strdup");
2107 break;
2110 err = got_object_id_by_path(&tree_id, s->repo, commit,
2111 subpath);
2112 if (err)
2113 break;
2115 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2116 free(tree_id);
2117 if (err)
2118 break;
2120 err = tree_view_visit_subtree(s, tree);
2121 if (err) {
2122 got_object_tree_close(tree);
2123 break;
2125 if (slash == NULL)
2126 break;
2127 free(subpath);
2128 subpath = NULL;
2129 p = slash;
2132 free(subpath);
2133 return err;
2136 static const struct got_error *
2137 browse_commit_tree(struct tog_view **new_view, int begin_x,
2138 struct commit_queue_entry *entry, const char *path,
2139 const char *head_ref_name, struct got_repository *repo)
2141 const struct got_error *err = NULL;
2142 struct tog_tree_view_state *s;
2143 struct tog_view *tree_view;
2145 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2146 if (tree_view == NULL)
2147 return got_error_from_errno("view_open");
2149 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2150 if (err)
2151 return err;
2152 s = &tree_view->state.tree;
2154 *new_view = tree_view;
2156 if (got_path_is_root_dir(path))
2157 return NULL;
2159 return tree_view_walk_path(s, entry->commit, path);
2162 static const struct got_error *
2163 block_signals_used_by_main_thread(void)
2165 sigset_t sigset;
2166 int errcode;
2168 if (sigemptyset(&sigset) == -1)
2169 return got_error_from_errno("sigemptyset");
2171 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2172 if (sigaddset(&sigset, SIGWINCH) == -1)
2173 return got_error_from_errno("sigaddset");
2174 if (sigaddset(&sigset, SIGCONT) == -1)
2175 return got_error_from_errno("sigaddset");
2176 if (sigaddset(&sigset, SIGINT) == -1)
2177 return got_error_from_errno("sigaddset");
2178 if (sigaddset(&sigset, SIGTERM) == -1)
2179 return got_error_from_errno("sigaddset");
2181 /* ncurses handles SIGTSTP */
2182 if (sigaddset(&sigset, SIGTSTP) == -1)
2183 return got_error_from_errno("sigaddset");
2185 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2186 if (errcode)
2187 return got_error_set_errno(errcode, "pthread_sigmask");
2189 return NULL;
2192 static void *
2193 log_thread(void *arg)
2195 const struct got_error *err = NULL;
2196 int errcode = 0;
2197 struct tog_log_thread_args *a = arg;
2198 int done = 0;
2200 err = block_signals_used_by_main_thread();
2201 if (err)
2202 return (void *)err;
2204 while (!done && !err && !tog_fatal_signal_received()) {
2205 err = queue_commits(a);
2206 if (err) {
2207 if (err->code != GOT_ERR_ITER_COMPLETED)
2208 return (void *)err;
2209 err = NULL;
2210 done = 1;
2211 } else if (a->commits_needed > 0 && !a->load_all)
2212 a->commits_needed--;
2214 errcode = pthread_mutex_lock(&tog_mutex);
2215 if (errcode) {
2216 err = got_error_set_errno(errcode,
2217 "pthread_mutex_lock");
2218 break;
2219 } else if (*a->quit)
2220 done = 1;
2221 else if (*a->first_displayed_entry == NULL) {
2222 *a->first_displayed_entry =
2223 TAILQ_FIRST(&a->commits->head);
2224 *a->selected_entry = *a->first_displayed_entry;
2227 errcode = pthread_cond_signal(&a->commit_loaded);
2228 if (errcode) {
2229 err = got_error_set_errno(errcode,
2230 "pthread_cond_signal");
2231 pthread_mutex_unlock(&tog_mutex);
2232 break;
2235 if (done)
2236 a->commits_needed = 0;
2237 else {
2238 if (a->commits_needed == 0 && !a->load_all) {
2239 errcode = pthread_cond_wait(&a->need_commits,
2240 &tog_mutex);
2241 if (errcode)
2242 err = got_error_set_errno(errcode,
2243 "pthread_cond_wait");
2244 if (*a->quit)
2245 done = 1;
2249 errcode = pthread_mutex_unlock(&tog_mutex);
2250 if (errcode && err == NULL)
2251 err = got_error_set_errno(errcode,
2252 "pthread_mutex_unlock");
2254 a->log_complete = 1;
2255 return (void *)err;
2258 static const struct got_error *
2259 stop_log_thread(struct tog_log_view_state *s)
2261 const struct got_error *err = NULL;
2262 int errcode;
2264 if (s->thread) {
2265 s->quit = 1;
2266 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2267 if (errcode)
2268 return got_error_set_errno(errcode,
2269 "pthread_cond_signal");
2270 errcode = pthread_mutex_unlock(&tog_mutex);
2271 if (errcode)
2272 return got_error_set_errno(errcode,
2273 "pthread_mutex_unlock");
2274 errcode = pthread_join(s->thread, (void **)&err);
2275 if (errcode)
2276 return got_error_set_errno(errcode, "pthread_join");
2277 errcode = pthread_mutex_lock(&tog_mutex);
2278 if (errcode)
2279 return got_error_set_errno(errcode,
2280 "pthread_mutex_lock");
2281 s->thread = NULL;
2284 if (s->thread_args.repo) {
2285 err = got_repo_close(s->thread_args.repo);
2286 s->thread_args.repo = NULL;
2289 if (s->thread_args.pack_fds) {
2290 const struct got_error *pack_err =
2291 got_repo_pack_fds_close(s->thread_args.pack_fds);
2292 if (err == NULL)
2293 err = pack_err;
2294 s->thread_args.pack_fds = NULL;
2297 if (s->thread_args.graph) {
2298 got_commit_graph_close(s->thread_args.graph);
2299 s->thread_args.graph = NULL;
2302 return err;
2305 static const struct got_error *
2306 close_log_view(struct tog_view *view)
2308 const struct got_error *err = NULL;
2309 struct tog_log_view_state *s = &view->state.log;
2310 int errcode;
2312 err = stop_log_thread(s);
2314 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2315 if (errcode && err == NULL)
2316 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2318 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2319 if (errcode && err == NULL)
2320 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2322 free_commits(&s->commits);
2323 free(s->in_repo_path);
2324 s->in_repo_path = NULL;
2325 free(s->start_id);
2326 s->start_id = NULL;
2327 free(s->head_ref_name);
2328 s->head_ref_name = NULL;
2329 return err;
2332 static const struct got_error *
2333 search_start_log_view(struct tog_view *view)
2335 struct tog_log_view_state *s = &view->state.log;
2337 s->matched_entry = NULL;
2338 s->search_entry = NULL;
2339 return NULL;
2342 static const struct got_error *
2343 search_next_log_view(struct tog_view *view)
2345 const struct got_error *err = NULL;
2346 struct tog_log_view_state *s = &view->state.log;
2347 struct commit_queue_entry *entry;
2349 /* Display progress update in log view. */
2350 show_log_view(view);
2351 update_panels();
2352 doupdate();
2354 if (s->search_entry) {
2355 int errcode, ch;
2356 errcode = pthread_mutex_unlock(&tog_mutex);
2357 if (errcode)
2358 return got_error_set_errno(errcode,
2359 "pthread_mutex_unlock");
2360 ch = wgetch(view->window);
2361 errcode = pthread_mutex_lock(&tog_mutex);
2362 if (errcode)
2363 return got_error_set_errno(errcode,
2364 "pthread_mutex_lock");
2365 if (ch == KEY_BACKSPACE) {
2366 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2367 return NULL;
2369 if (view->searching == TOG_SEARCH_FORWARD)
2370 entry = TAILQ_NEXT(s->search_entry, entry);
2371 else
2372 entry = TAILQ_PREV(s->search_entry,
2373 commit_queue_head, entry);
2374 } else if (s->matched_entry) {
2375 if (view->searching == TOG_SEARCH_FORWARD)
2376 entry = TAILQ_NEXT(s->matched_entry, entry);
2377 else
2378 entry = TAILQ_PREV(s->matched_entry,
2379 commit_queue_head, entry);
2380 } else {
2381 entry = s->selected_entry;
2384 while (1) {
2385 int have_match = 0;
2387 if (entry == NULL) {
2388 if (s->thread_args.log_complete ||
2389 view->searching == TOG_SEARCH_BACKWARD) {
2390 view->search_next_done =
2391 (s->matched_entry == NULL ?
2392 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2393 s->search_entry = NULL;
2394 return NULL;
2397 * Poke the log thread for more commits and return,
2398 * allowing the main loop to make progress. Search
2399 * will resume at s->search_entry once we come back.
2401 s->thread_args.commits_needed++;
2402 return trigger_log_thread(view, 0);
2405 err = match_commit(&have_match, entry->id, entry->commit,
2406 &view->regex);
2407 if (err)
2408 break;
2409 if (have_match) {
2410 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2411 s->matched_entry = entry;
2412 break;
2415 s->search_entry = entry;
2416 if (view->searching == TOG_SEARCH_FORWARD)
2417 entry = TAILQ_NEXT(entry, entry);
2418 else
2419 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2422 if (s->matched_entry) {
2423 int cur = s->selected_entry->idx;
2424 while (cur < s->matched_entry->idx) {
2425 err = input_log_view(NULL, view, KEY_DOWN);
2426 if (err)
2427 return err;
2428 cur++;
2430 while (cur > s->matched_entry->idx) {
2431 err = input_log_view(NULL, view, KEY_UP);
2432 if (err)
2433 return err;
2434 cur--;
2438 s->search_entry = NULL;
2440 return NULL;
2443 static const struct got_error *
2444 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2445 struct got_repository *repo, const char *head_ref_name,
2446 const char *in_repo_path, int log_branches)
2448 const struct got_error *err = NULL;
2449 struct tog_log_view_state *s = &view->state.log;
2450 struct got_repository *thread_repo = NULL;
2451 struct got_commit_graph *thread_graph = NULL;
2452 int errcode;
2454 if (in_repo_path != s->in_repo_path) {
2455 free(s->in_repo_path);
2456 s->in_repo_path = strdup(in_repo_path);
2457 if (s->in_repo_path == NULL)
2458 return got_error_from_errno("strdup");
2461 /* The commit queue only contains commits being displayed. */
2462 TAILQ_INIT(&s->commits.head);
2463 s->commits.ncommits = 0;
2465 s->repo = repo;
2466 if (head_ref_name) {
2467 s->head_ref_name = strdup(head_ref_name);
2468 if (s->head_ref_name == NULL) {
2469 err = got_error_from_errno("strdup");
2470 goto done;
2473 s->start_id = got_object_id_dup(start_id);
2474 if (s->start_id == NULL) {
2475 err = got_error_from_errno("got_object_id_dup");
2476 goto done;
2478 s->log_branches = log_branches;
2480 STAILQ_INIT(&s->colors);
2481 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2482 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2483 get_color_value("TOG_COLOR_COMMIT"));
2484 if (err)
2485 goto done;
2486 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2487 get_color_value("TOG_COLOR_AUTHOR"));
2488 if (err) {
2489 free_colors(&s->colors);
2490 goto done;
2492 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2493 get_color_value("TOG_COLOR_DATE"));
2494 if (err) {
2495 free_colors(&s->colors);
2496 goto done;
2500 view->show = show_log_view;
2501 view->input = input_log_view;
2502 view->close = close_log_view;
2503 view->search_start = search_start_log_view;
2504 view->search_next = search_next_log_view;
2506 if (s->thread_args.pack_fds == NULL) {
2507 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2508 if (err)
2509 goto done;
2511 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2512 s->thread_args.pack_fds);
2513 if (err)
2514 goto done;
2515 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2516 !s->log_branches);
2517 if (err)
2518 goto done;
2519 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2520 s->repo, NULL, NULL);
2521 if (err)
2522 goto done;
2524 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2525 if (errcode) {
2526 err = got_error_set_errno(errcode, "pthread_cond_init");
2527 goto done;
2529 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2530 if (errcode) {
2531 err = got_error_set_errno(errcode, "pthread_cond_init");
2532 goto done;
2535 s->thread_args.commits_needed = view->nlines;
2536 s->thread_args.graph = thread_graph;
2537 s->thread_args.commits = &s->commits;
2538 s->thread_args.in_repo_path = s->in_repo_path;
2539 s->thread_args.start_id = s->start_id;
2540 s->thread_args.repo = thread_repo;
2541 s->thread_args.log_complete = 0;
2542 s->thread_args.quit = &s->quit;
2543 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2544 s->thread_args.selected_entry = &s->selected_entry;
2545 s->thread_args.searching = &view->searching;
2546 s->thread_args.search_next_done = &view->search_next_done;
2547 s->thread_args.regex = &view->regex;
2548 done:
2549 if (err)
2550 close_log_view(view);
2551 return err;
2554 static const struct got_error *
2555 show_log_view(struct tog_view *view)
2557 const struct got_error *err;
2558 struct tog_log_view_state *s = &view->state.log;
2560 if (s->thread == NULL) {
2561 int errcode = pthread_create(&s->thread, NULL, log_thread,
2562 &s->thread_args);
2563 if (errcode)
2564 return got_error_set_errno(errcode, "pthread_create");
2565 if (s->thread_args.commits_needed > 0) {
2566 err = trigger_log_thread(view, 1);
2567 if (err)
2568 return err;
2572 return draw_commits(view);
2575 static const struct got_error *
2576 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2578 const struct got_error *err = NULL;
2579 struct tog_log_view_state *s = &view->state.log;
2580 struct tog_view *diff_view = NULL, *tree_view = NULL;
2581 struct tog_view *ref_view = NULL;
2582 struct commit_queue_entry *entry;
2583 int begin_x = 0, n, nscroll = view->nlines - 1;
2585 if (s->thread_args.load_all) {
2586 if (ch == KEY_BACKSPACE)
2587 s->thread_args.load_all = 0;
2588 else if (s->thread_args.log_complete) {
2589 s->thread_args.load_all = 0;
2590 log_scroll_down(view, s->commits.ncommits);
2591 s->selected = MIN(view->nlines - 2,
2592 s->commits.ncommits - 1);
2593 select_commit(s);
2595 return NULL;
2598 switch (ch) {
2599 case 'q':
2600 s->quit = 1;
2601 break;
2602 case '0':
2603 view->x = 0;
2604 break;
2605 case '$':
2606 view->x = MAX(view->maxx - view->ncols / 2, 0);
2607 break;
2608 case KEY_RIGHT:
2609 case 'l':
2610 if (view->x + view->ncols / 2 < view->maxx)
2611 view->x += 2; /* move two columns right */
2612 break;
2613 case KEY_LEFT:
2614 case 'h':
2615 view->x -= MIN(view->x, 2); /* move two columns back */
2616 break;
2617 case 'k':
2618 case KEY_UP:
2619 case '<':
2620 case ',':
2621 case CTRL('p'):
2622 if (s->first_displayed_entry == NULL)
2623 break;
2624 if (s->selected > 0)
2625 s->selected--;
2626 else
2627 log_scroll_up(s, 1);
2628 select_commit(s);
2629 break;
2630 case 'g':
2631 case KEY_HOME:
2632 s->selected = 0;
2633 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2634 select_commit(s);
2635 break;
2636 case CTRL('u'):
2637 case 'u':
2638 nscroll /= 2;
2639 /* FALL THROUGH */
2640 case KEY_PPAGE:
2641 case CTRL('b'):
2642 if (s->first_displayed_entry == NULL)
2643 break;
2644 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2645 s->selected = MAX(0, s->selected - nscroll - 1);
2646 else
2647 log_scroll_up(s, nscroll);
2648 select_commit(s);
2649 break;
2650 case 'j':
2651 case KEY_DOWN:
2652 case '>':
2653 case '.':
2654 case CTRL('n'):
2655 if (s->first_displayed_entry == NULL)
2656 break;
2657 if (s->selected < MIN(view->nlines - 2,
2658 s->commits.ncommits - 1))
2659 s->selected++;
2660 else {
2661 err = log_scroll_down(view, 1);
2662 if (err)
2663 break;
2665 select_commit(s);
2666 break;
2667 case 'G':
2668 case KEY_END: {
2669 /* We don't know yet how many commits, so we're forced to
2670 * traverse them all. */
2671 if (!s->thread_args.log_complete) {
2672 s->thread_args.load_all = 1;
2673 return trigger_log_thread(view, 0);
2676 s->selected = 0;
2677 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2678 for (n = 0; n < view->nlines - 1; n++) {
2679 if (entry == NULL)
2680 break;
2681 s->first_displayed_entry = entry;
2682 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2684 if (n > 0)
2685 s->selected = n - 1;
2686 select_commit(s);
2687 break;
2689 case CTRL('d'):
2690 case 'd':
2691 nscroll /= 2;
2692 /* FALL THROUGH */
2693 case KEY_NPAGE:
2694 case CTRL('f'): {
2695 struct commit_queue_entry *first;
2696 first = s->first_displayed_entry;
2697 if (first == NULL)
2698 break;
2699 err = log_scroll_down(view, nscroll);
2700 if (err)
2701 break;
2702 if (first == s->first_displayed_entry &&
2703 s->selected < MIN(view->nlines - 2,
2704 s->commits.ncommits - 1)) {
2705 /* can't scroll further down */
2706 s->selected += MIN(s->last_displayed_entry->idx -
2707 s->selected_entry->idx, nscroll + 1);
2709 select_commit(s);
2710 break;
2712 case KEY_RESIZE:
2713 if (s->selected > view->nlines - 2)
2714 s->selected = view->nlines - 2;
2715 if (s->selected > s->commits.ncommits - 1)
2716 s->selected = s->commits.ncommits - 1;
2717 select_commit(s);
2718 if (s->commits.ncommits < view->nlines - 1 &&
2719 !s->thread_args.log_complete) {
2720 s->thread_args.commits_needed += (view->nlines - 1) -
2721 s->commits.ncommits;
2722 err = trigger_log_thread(view, 1);
2724 break;
2725 case KEY_ENTER:
2726 case ' ':
2727 case '\r':
2728 if (s->selected_entry == NULL)
2729 break;
2730 if (view_is_parent_view(view))
2731 begin_x = view_split_begin_x(view->begin_x);
2732 err = open_diff_view_for_commit(&diff_view, begin_x,
2733 s->selected_entry->commit, s->selected_entry->id,
2734 view, s->repo);
2735 if (err)
2736 break;
2737 view->focussed = 0;
2738 diff_view->focussed = 1;
2739 if (view_is_parent_view(view)) {
2740 err = view_close_child(view);
2741 if (err)
2742 return err;
2743 view_set_child(view, diff_view);
2744 view->focus_child = 1;
2745 } else
2746 *new_view = diff_view;
2747 break;
2748 case 't':
2749 if (s->selected_entry == NULL)
2750 break;
2751 if (view_is_parent_view(view))
2752 begin_x = view_split_begin_x(view->begin_x);
2753 err = browse_commit_tree(&tree_view, begin_x,
2754 s->selected_entry, s->in_repo_path, s->head_ref_name,
2755 s->repo);
2756 if (err)
2757 break;
2758 view->focussed = 0;
2759 tree_view->focussed = 1;
2760 if (view_is_parent_view(view)) {
2761 err = view_close_child(view);
2762 if (err)
2763 return err;
2764 view_set_child(view, tree_view);
2765 view->focus_child = 1;
2766 } else
2767 *new_view = tree_view;
2768 break;
2769 case KEY_BACKSPACE:
2770 case CTRL('l'):
2771 case 'B':
2772 if (ch == KEY_BACKSPACE &&
2773 got_path_is_root_dir(s->in_repo_path))
2774 break;
2775 err = stop_log_thread(s);
2776 if (err)
2777 return err;
2778 if (ch == KEY_BACKSPACE) {
2779 char *parent_path;
2780 err = got_path_dirname(&parent_path, s->in_repo_path);
2781 if (err)
2782 return err;
2783 free(s->in_repo_path);
2784 s->in_repo_path = parent_path;
2785 s->thread_args.in_repo_path = s->in_repo_path;
2786 } else if (ch == CTRL('l')) {
2787 struct got_object_id *start_id;
2788 err = got_repo_match_object_id(&start_id, NULL,
2789 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2790 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2791 if (err)
2792 return err;
2793 free(s->start_id);
2794 s->start_id = start_id;
2795 s->thread_args.start_id = s->start_id;
2796 } else /* 'B' */
2797 s->log_branches = !s->log_branches;
2799 if (s->thread_args.pack_fds == NULL) {
2800 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2801 if (err)
2802 return err;
2804 err = got_repo_open(&s->thread_args.repo,
2805 got_repo_get_path(s->repo), NULL,
2806 s->thread_args.pack_fds);
2807 if (err)
2808 return err;
2809 tog_free_refs();
2810 err = tog_load_refs(s->repo, 0);
2811 if (err)
2812 return err;
2813 err = got_commit_graph_open(&s->thread_args.graph,
2814 s->in_repo_path, !s->log_branches);
2815 if (err)
2816 return err;
2817 err = got_commit_graph_iter_start(s->thread_args.graph,
2818 s->start_id, s->repo, NULL, NULL);
2819 if (err)
2820 return err;
2821 free_commits(&s->commits);
2822 s->first_displayed_entry = NULL;
2823 s->last_displayed_entry = NULL;
2824 s->selected_entry = NULL;
2825 s->selected = 0;
2826 s->thread_args.log_complete = 0;
2827 s->quit = 0;
2828 s->thread_args.commits_needed = view->nlines;
2829 break;
2830 case 'r':
2831 if (view_is_parent_view(view))
2832 begin_x = view_split_begin_x(view->begin_x);
2833 ref_view = view_open(view->nlines, view->ncols,
2834 view->begin_y, begin_x, TOG_VIEW_REF);
2835 if (ref_view == NULL)
2836 return got_error_from_errno("view_open");
2837 err = open_ref_view(ref_view, s->repo);
2838 if (err) {
2839 view_close(ref_view);
2840 return err;
2842 view->focussed = 0;
2843 ref_view->focussed = 1;
2844 if (view_is_parent_view(view)) {
2845 err = view_close_child(view);
2846 if (err)
2847 return err;
2848 view_set_child(view, ref_view);
2849 view->focus_child = 1;
2850 } else
2851 *new_view = ref_view;
2852 break;
2853 default:
2854 break;
2857 return err;
2860 static const struct got_error *
2861 apply_unveil(const char *repo_path, const char *worktree_path)
2863 const struct got_error *error;
2865 #ifdef PROFILE
2866 if (unveil("gmon.out", "rwc") != 0)
2867 return got_error_from_errno2("unveil", "gmon.out");
2868 #endif
2869 if (repo_path && unveil(repo_path, "r") != 0)
2870 return got_error_from_errno2("unveil", repo_path);
2872 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2873 return got_error_from_errno2("unveil", worktree_path);
2875 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2876 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2878 error = got_privsep_unveil_exec_helpers();
2879 if (error != NULL)
2880 return error;
2882 if (unveil(NULL, NULL) != 0)
2883 return got_error_from_errno("unveil");
2885 return NULL;
2888 static void
2889 init_curses(void)
2892 * Override default signal handlers before starting ncurses.
2893 * This should prevent ncurses from installing its own
2894 * broken cleanup() signal handler.
2896 signal(SIGWINCH, tog_sigwinch);
2897 signal(SIGPIPE, tog_sigpipe);
2898 signal(SIGCONT, tog_sigcont);
2899 signal(SIGINT, tog_sigint);
2900 signal(SIGTERM, tog_sigterm);
2902 initscr();
2903 cbreak();
2904 halfdelay(1); /* Do fast refresh while initial view is loading. */
2905 noecho();
2906 nonl();
2907 intrflush(stdscr, FALSE);
2908 keypad(stdscr, TRUE);
2909 curs_set(0);
2910 if (getenv("TOG_COLORS") != NULL) {
2911 start_color();
2912 use_default_colors();
2916 static const struct got_error *
2917 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2918 struct got_repository *repo, struct got_worktree *worktree)
2920 const struct got_error *err = NULL;
2922 if (argc == 0) {
2923 *in_repo_path = strdup("/");
2924 if (*in_repo_path == NULL)
2925 return got_error_from_errno("strdup");
2926 return NULL;
2929 if (worktree) {
2930 const char *prefix = got_worktree_get_path_prefix(worktree);
2931 char *p;
2933 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2934 if (err)
2935 return err;
2936 if (asprintf(in_repo_path, "%s%s%s", prefix,
2937 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2938 p) == -1) {
2939 err = got_error_from_errno("asprintf");
2940 *in_repo_path = NULL;
2942 free(p);
2943 } else
2944 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2946 return err;
2949 static const struct got_error *
2950 cmd_log(int argc, char *argv[])
2952 const struct got_error *error;
2953 struct got_repository *repo = NULL;
2954 struct got_worktree *worktree = NULL;
2955 struct got_object_id *start_id = NULL;
2956 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2957 char *start_commit = NULL, *label = NULL;
2958 struct got_reference *ref = NULL;
2959 const char *head_ref_name = NULL;
2960 int ch, log_branches = 0;
2961 struct tog_view *view;
2962 int *pack_fds = NULL;
2964 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2965 switch (ch) {
2966 case 'b':
2967 log_branches = 1;
2968 break;
2969 case 'c':
2970 start_commit = optarg;
2971 break;
2972 case 'r':
2973 repo_path = realpath(optarg, NULL);
2974 if (repo_path == NULL)
2975 return got_error_from_errno2("realpath",
2976 optarg);
2977 break;
2978 default:
2979 usage_log();
2980 /* NOTREACHED */
2984 argc -= optind;
2985 argv += optind;
2987 if (argc > 1)
2988 usage_log();
2990 error = got_repo_pack_fds_open(&pack_fds);
2991 if (error != NULL)
2992 goto done;
2994 if (repo_path == NULL) {
2995 cwd = getcwd(NULL, 0);
2996 if (cwd == NULL)
2997 return got_error_from_errno("getcwd");
2998 error = got_worktree_open(&worktree, cwd);
2999 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3000 goto done;
3001 if (worktree)
3002 repo_path =
3003 strdup(got_worktree_get_repo_path(worktree));
3004 else
3005 repo_path = strdup(cwd);
3006 if (repo_path == NULL) {
3007 error = got_error_from_errno("strdup");
3008 goto done;
3012 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3013 if (error != NULL)
3014 goto done;
3016 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3017 repo, worktree);
3018 if (error)
3019 goto done;
3021 init_curses();
3023 error = apply_unveil(got_repo_get_path(repo),
3024 worktree ? got_worktree_get_root_path(worktree) : NULL);
3025 if (error)
3026 goto done;
3028 /* already loaded by tog_log_with_path()? */
3029 if (TAILQ_EMPTY(&tog_refs)) {
3030 error = tog_load_refs(repo, 0);
3031 if (error)
3032 goto done;
3035 if (start_commit == NULL) {
3036 error = got_repo_match_object_id(&start_id, &label,
3037 worktree ? got_worktree_get_head_ref_name(worktree) :
3038 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3039 if (error)
3040 goto done;
3041 head_ref_name = label;
3042 } else {
3043 error = got_ref_open(&ref, repo, start_commit, 0);
3044 if (error == NULL)
3045 head_ref_name = got_ref_get_name(ref);
3046 else if (error->code != GOT_ERR_NOT_REF)
3047 goto done;
3048 error = got_repo_match_object_id(&start_id, NULL,
3049 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3050 if (error)
3051 goto done;
3054 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3055 if (view == NULL) {
3056 error = got_error_from_errno("view_open");
3057 goto done;
3059 error = open_log_view(view, start_id, repo, head_ref_name,
3060 in_repo_path, log_branches);
3061 if (error)
3062 goto done;
3063 if (worktree) {
3064 /* Release work tree lock. */
3065 got_worktree_close(worktree);
3066 worktree = NULL;
3068 error = view_loop(view);
3069 done:
3070 free(in_repo_path);
3071 free(repo_path);
3072 free(cwd);
3073 free(start_id);
3074 free(label);
3075 if (ref)
3076 got_ref_close(ref);
3077 if (repo) {
3078 const struct got_error *close_err = got_repo_close(repo);
3079 if (error == NULL)
3080 error = close_err;
3082 if (worktree)
3083 got_worktree_close(worktree);
3084 if (pack_fds) {
3085 const struct got_error *pack_err =
3086 got_repo_pack_fds_close(pack_fds);
3087 if (error == NULL)
3088 error = pack_err;
3090 tog_free_refs();
3091 return error;
3094 __dead static void
3095 usage_diff(void)
3097 endwin();
3098 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3099 "[-w] object1 object2\n", getprogname());
3100 exit(1);
3103 static int
3104 match_line(const char *line, regex_t *regex, size_t nmatch,
3105 regmatch_t *regmatch)
3107 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3110 struct tog_color *
3111 match_color(struct tog_colors *colors, const char *line)
3113 struct tog_color *tc = NULL;
3115 STAILQ_FOREACH(tc, colors, entry) {
3116 if (match_line(line, &tc->regex, 0, NULL))
3117 return tc;
3120 return NULL;
3123 static const struct got_error *
3124 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3125 WINDOW *window, int skip, regmatch_t *regmatch)
3127 const struct got_error *err = NULL;
3128 wchar_t *wline;
3129 int rme, rms, n, width;
3131 *wtotal = 0;
3132 rms = regmatch->rm_so;
3133 rme = regmatch->rm_eo;
3135 err = format_line(&wline, &width, NULL, line, 0, wlimit + skip,
3136 col_tab_align, 1);
3137 if (err)
3138 return err;
3140 /* draw up to matched token if we haven't scrolled past it */
3141 n = MAX(rms - skip, 0);
3142 if (n) {
3143 waddnwstr(window, wline + skip, n);
3144 wlimit -= n;
3145 *wtotal += n;
3148 if (wlimit > 0) {
3149 int len = rme - rms;
3150 n = 0;
3151 if (skip > rms) {
3152 n = skip - rms;
3153 len = MAX(len - n, 0);
3155 /* draw (visible part of) matched token (if scrolled into it) */
3156 if (len) {
3157 wattron(window, A_STANDOUT);
3158 waddnwstr(window, wline + rms + n, len);
3159 wattroff(window, A_STANDOUT);
3160 wlimit -= len;
3161 *wtotal += len;
3165 if (wlimit > 0 && skip < width) { /* draw rest of line */
3166 n = 0;
3167 if (skip > rme)
3168 n = MIN(skip - rme, width - rme);
3169 waddnwstr(window, wline + rme + n, wlimit);
3172 *wtotal = width;
3173 free(wline);
3174 return NULL;
3177 static const struct got_error *
3178 draw_file(struct tog_view *view, const char *header)
3180 struct tog_diff_view_state *s = &view->state.diff;
3181 regmatch_t *regmatch = &view->regmatch;
3182 const struct got_error *err;
3183 int nprinted = 0;
3184 char *line;
3185 size_t linesize = 0;
3186 ssize_t linelen;
3187 struct tog_color *tc;
3188 wchar_t *wline;
3189 int width;
3190 int max_lines = view->nlines;
3191 int nlines = s->nlines;
3192 off_t line_offset;
3194 line_offset = s->line_offsets[s->first_displayed_line - 1];
3195 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3196 return got_error_from_errno("fseek");
3198 werase(view->window);
3200 if (header) {
3201 if (asprintf(&line, "[%d/%d] %s",
3202 s->first_displayed_line - 1 + s->selected_line, nlines,
3203 header) == -1)
3204 return got_error_from_errno("asprintf");
3205 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3206 0, 0);
3207 free(line);
3208 if (err)
3209 return err;
3211 if (view_needs_focus_indication(view))
3212 wstandout(view->window);
3213 waddwstr(view->window, wline);
3214 free(wline);
3215 wline = NULL;
3216 if (view_needs_focus_indication(view))
3217 wstandend(view->window);
3218 if (width <= view->ncols - 1)
3219 waddch(view->window, '\n');
3221 if (max_lines <= 1)
3222 return NULL;
3223 max_lines--;
3226 s->eof = 0;
3227 view->maxx = 0;
3228 line = NULL;
3229 while (max_lines > 0 && nprinted < max_lines) {
3230 linelen = getline(&line, &linesize, s->f);
3231 if (linelen == -1) {
3232 if (feof(s->f)) {
3233 s->eof = 1;
3234 break;
3236 free(line);
3237 return got_ferror(s->f, GOT_ERR_IO);
3240 view->maxx = MAX(view->maxx, linelen);
3242 tc = match_color(&s->colors, line);
3243 if (tc)
3244 wattr_on(view->window,
3245 COLOR_PAIR(tc->colorpair), NULL);
3246 if (s->first_displayed_line + nprinted == s->matched_line &&
3247 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3248 err = add_matched_line(&width, line, view->ncols, 0,
3249 view->window, view->x, regmatch);
3250 if (err) {
3251 free(line);
3252 return err;
3254 } else {
3255 err = format_line(&wline, &width, NULL, line, 0,
3256 view->x + view->ncols, 0, view->x ? 1 : 0);
3257 if (err) {
3258 free(line);
3259 return err;
3261 if (view->x < width)
3262 waddwstr(view->window, wline + view->x);
3263 free(wline);
3264 wline = NULL;
3266 if (tc)
3267 wattr_off(view->window,
3268 COLOR_PAIR(tc->colorpair), NULL);
3269 if (width - view->x <= view->ncols - 1)
3270 waddch(view->window, '\n');
3271 nprinted++;
3273 free(line);
3274 if (nprinted >= 1)
3275 s->last_displayed_line = s->first_displayed_line +
3276 (nprinted - 1);
3277 else
3278 s->last_displayed_line = s->first_displayed_line;
3280 view_vborder(view);
3282 if (s->eof) {
3283 while (nprinted < view->nlines) {
3284 waddch(view->window, '\n');
3285 nprinted++;
3288 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3289 view->ncols, 0, 0);
3290 if (err) {
3291 return err;
3294 wstandout(view->window);
3295 waddwstr(view->window, wline);
3296 free(wline);
3297 wline = NULL;
3298 wstandend(view->window);
3301 return NULL;
3304 static char *
3305 get_datestr(time_t *time, char *datebuf)
3307 struct tm mytm, *tm;
3308 char *p, *s;
3310 tm = gmtime_r(time, &mytm);
3311 if (tm == NULL)
3312 return NULL;
3313 s = asctime_r(tm, datebuf);
3314 if (s == NULL)
3315 return NULL;
3316 p = strchr(s, '\n');
3317 if (p)
3318 *p = '\0';
3319 return s;
3322 static const struct got_error *
3323 get_changed_paths(struct got_pathlist_head *paths,
3324 struct got_commit_object *commit, struct got_repository *repo)
3326 const struct got_error *err = NULL;
3327 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3328 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3329 struct got_object_qid *qid;
3331 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3332 if (qid != NULL) {
3333 struct got_commit_object *pcommit;
3334 err = got_object_open_as_commit(&pcommit, repo,
3335 &qid->id);
3336 if (err)
3337 return err;
3339 tree_id1 = got_object_id_dup(
3340 got_object_commit_get_tree_id(pcommit));
3341 if (tree_id1 == NULL) {
3342 got_object_commit_close(pcommit);
3343 return got_error_from_errno("got_object_id_dup");
3345 got_object_commit_close(pcommit);
3349 if (tree_id1) {
3350 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3351 if (err)
3352 goto done;
3355 tree_id2 = got_object_commit_get_tree_id(commit);
3356 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3357 if (err)
3358 goto done;
3360 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3361 got_diff_tree_collect_changed_paths, paths, 0);
3362 done:
3363 if (tree1)
3364 got_object_tree_close(tree1);
3365 if (tree2)
3366 got_object_tree_close(tree2);
3367 free(tree_id1);
3368 return err;
3371 static const struct got_error *
3372 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3374 off_t *p;
3376 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3377 if (p == NULL)
3378 return got_error_from_errno("reallocarray");
3379 *line_offsets = p;
3380 (*line_offsets)[*nlines] = off;
3381 (*nlines)++;
3382 return NULL;
3385 static const struct got_error *
3386 write_commit_info(off_t **line_offsets, size_t *nlines,
3387 struct got_object_id *commit_id, struct got_reflist_head *refs,
3388 struct got_repository *repo, FILE *outfile)
3390 const struct got_error *err = NULL;
3391 char datebuf[26], *datestr;
3392 struct got_commit_object *commit;
3393 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3394 time_t committer_time;
3395 const char *author, *committer;
3396 char *refs_str = NULL;
3397 struct got_pathlist_head changed_paths;
3398 struct got_pathlist_entry *pe;
3399 off_t outoff = 0;
3400 int n;
3402 TAILQ_INIT(&changed_paths);
3404 if (refs) {
3405 err = build_refs_str(&refs_str, refs, commit_id, repo);
3406 if (err)
3407 return err;
3410 err = got_object_open_as_commit(&commit, repo, commit_id);
3411 if (err)
3412 return err;
3414 err = got_object_id_str(&id_str, commit_id);
3415 if (err) {
3416 err = got_error_from_errno("got_object_id_str");
3417 goto done;
3420 err = add_line_offset(line_offsets, nlines, 0);
3421 if (err)
3422 goto done;
3424 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3425 refs_str ? refs_str : "", refs_str ? ")" : "");
3426 if (n < 0) {
3427 err = got_error_from_errno("fprintf");
3428 goto done;
3430 outoff += n;
3431 err = add_line_offset(line_offsets, nlines, outoff);
3432 if (err)
3433 goto done;
3435 n = fprintf(outfile, "from: %s\n",
3436 got_object_commit_get_author(commit));
3437 if (n < 0) {
3438 err = got_error_from_errno("fprintf");
3439 goto done;
3441 outoff += n;
3442 err = add_line_offset(line_offsets, nlines, outoff);
3443 if (err)
3444 goto done;
3446 committer_time = got_object_commit_get_committer_time(commit);
3447 datestr = get_datestr(&committer_time, datebuf);
3448 if (datestr) {
3449 n = fprintf(outfile, "date: %s UTC\n", datestr);
3450 if (n < 0) {
3451 err = got_error_from_errno("fprintf");
3452 goto done;
3454 outoff += n;
3455 err = add_line_offset(line_offsets, nlines, outoff);
3456 if (err)
3457 goto done;
3459 author = got_object_commit_get_author(commit);
3460 committer = got_object_commit_get_committer(commit);
3461 if (strcmp(author, committer) != 0) {
3462 n = fprintf(outfile, "via: %s\n", committer);
3463 if (n < 0) {
3464 err = got_error_from_errno("fprintf");
3465 goto done;
3467 outoff += n;
3468 err = add_line_offset(line_offsets, nlines, outoff);
3469 if (err)
3470 goto done;
3472 if (got_object_commit_get_nparents(commit) > 1) {
3473 const struct got_object_id_queue *parent_ids;
3474 struct got_object_qid *qid;
3475 int pn = 1;
3476 parent_ids = got_object_commit_get_parent_ids(commit);
3477 STAILQ_FOREACH(qid, parent_ids, entry) {
3478 err = got_object_id_str(&id_str, &qid->id);
3479 if (err)
3480 goto done;
3481 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3482 if (n < 0) {
3483 err = got_error_from_errno("fprintf");
3484 goto done;
3486 outoff += n;
3487 err = add_line_offset(line_offsets, nlines, outoff);
3488 if (err)
3489 goto done;
3490 free(id_str);
3491 id_str = NULL;
3495 err = got_object_commit_get_logmsg(&logmsg, commit);
3496 if (err)
3497 goto done;
3498 s = logmsg;
3499 while ((line = strsep(&s, "\n")) != NULL) {
3500 n = fprintf(outfile, "%s\n", line);
3501 if (n < 0) {
3502 err = got_error_from_errno("fprintf");
3503 goto done;
3505 outoff += n;
3506 err = add_line_offset(line_offsets, nlines, outoff);
3507 if (err)
3508 goto done;
3511 err = get_changed_paths(&changed_paths, commit, repo);
3512 if (err)
3513 goto done;
3514 TAILQ_FOREACH(pe, &changed_paths, entry) {
3515 struct got_diff_changed_path *cp = pe->data;
3516 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3517 if (n < 0) {
3518 err = got_error_from_errno("fprintf");
3519 goto done;
3521 outoff += n;
3522 err = add_line_offset(line_offsets, nlines, outoff);
3523 if (err)
3524 goto done;
3525 free((char *)pe->path);
3526 free(pe->data);
3529 fputc('\n', outfile);
3530 outoff++;
3531 err = add_line_offset(line_offsets, nlines, outoff);
3532 done:
3533 got_pathlist_free(&changed_paths);
3534 free(id_str);
3535 free(logmsg);
3536 free(refs_str);
3537 got_object_commit_close(commit);
3538 if (err) {
3539 free(*line_offsets);
3540 *line_offsets = NULL;
3541 *nlines = 0;
3543 return err;
3546 static const struct got_error *
3547 create_diff(struct tog_diff_view_state *s)
3549 const struct got_error *err = NULL;
3550 FILE *f = NULL;
3551 int obj_type;
3553 free(s->line_offsets);
3554 s->line_offsets = malloc(sizeof(off_t));
3555 if (s->line_offsets == NULL)
3556 return got_error_from_errno("malloc");
3557 s->nlines = 0;
3559 f = got_opentemp();
3560 if (f == NULL) {
3561 err = got_error_from_errno("got_opentemp");
3562 goto done;
3564 if (s->f && fclose(s->f) == EOF) {
3565 err = got_error_from_errno("fclose");
3566 goto done;
3568 s->f = f;
3570 if (s->id1)
3571 err = got_object_get_type(&obj_type, s->repo, s->id1);
3572 else
3573 err = got_object_get_type(&obj_type, s->repo, s->id2);
3574 if (err)
3575 goto done;
3577 switch (obj_type) {
3578 case GOT_OBJ_TYPE_BLOB:
3579 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3580 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3581 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3582 s->repo, s->f);
3583 break;
3584 case GOT_OBJ_TYPE_TREE:
3585 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3586 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3587 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3588 break;
3589 case GOT_OBJ_TYPE_COMMIT: {
3590 const struct got_object_id_queue *parent_ids;
3591 struct got_object_qid *pid;
3592 struct got_commit_object *commit2;
3593 struct got_reflist_head *refs;
3595 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3596 if (err)
3597 goto done;
3598 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3599 /* Show commit info if we're diffing to a parent/root commit. */
3600 if (s->id1 == NULL) {
3601 err = write_commit_info(&s->line_offsets, &s->nlines,
3602 s->id2, refs, s->repo, s->f);
3603 if (err)
3604 goto done;
3605 } else {
3606 parent_ids = got_object_commit_get_parent_ids(commit2);
3607 STAILQ_FOREACH(pid, parent_ids, entry) {
3608 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3609 err = write_commit_info(
3610 &s->line_offsets, &s->nlines,
3611 s->id2, refs, s->repo, s->f);
3612 if (err)
3613 goto done;
3614 break;
3618 got_object_commit_close(commit2);
3620 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3621 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3622 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3623 break;
3625 default:
3626 err = got_error(GOT_ERR_OBJ_TYPE);
3627 break;
3629 if (err)
3630 goto done;
3631 done:
3632 if (s->f && fflush(s->f) != 0 && err == NULL)
3633 err = got_error_from_errno("fflush");
3634 return err;
3637 static void
3638 diff_view_indicate_progress(struct tog_view *view)
3640 mvwaddstr(view->window, 0, 0, "diffing...");
3641 update_panels();
3642 doupdate();
3645 static const struct got_error *
3646 search_start_diff_view(struct tog_view *view)
3648 struct tog_diff_view_state *s = &view->state.diff;
3650 s->matched_line = 0;
3651 return NULL;
3654 static const struct got_error *
3655 search_next_diff_view(struct tog_view *view)
3657 struct tog_diff_view_state *s = &view->state.diff;
3658 const struct got_error *err = NULL;
3659 int lineno;
3660 char *exstr = NULL, *line = NULL;
3661 size_t linesize = 0;
3662 ssize_t linelen;
3664 if (!view->searching) {
3665 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3666 return NULL;
3669 if (s->matched_line) {
3670 if (view->searching == TOG_SEARCH_FORWARD)
3671 lineno = s->matched_line + 1;
3672 else
3673 lineno = s->matched_line - 1;
3674 } else
3675 lineno = s->first_displayed_line;
3677 while (1) {
3678 off_t offset;
3680 if (lineno <= 0 || lineno > s->nlines) {
3681 if (s->matched_line == 0) {
3682 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3683 break;
3686 if (view->searching == TOG_SEARCH_FORWARD)
3687 lineno = 1;
3688 else
3689 lineno = s->nlines;
3692 offset = s->line_offsets[lineno - 1];
3693 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3694 free(line);
3695 return got_error_from_errno("fseeko");
3697 linelen = getline(&line, &linesize, s->f);
3698 err = expand_tab(&exstr, line);
3699 if (err)
3700 break;
3701 if (linelen != -1 &&
3702 match_line(exstr, &view->regex, 1, &view->regmatch)) {
3703 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3704 s->matched_line = lineno;
3705 break;
3707 free(exstr);
3708 exstr = NULL;
3709 if (view->searching == TOG_SEARCH_FORWARD)
3710 lineno++;
3711 else
3712 lineno--;
3714 free(line);
3715 free(exstr);
3717 if (s->matched_line) {
3718 s->first_displayed_line = s->matched_line;
3719 s->selected_line = 1;
3722 return err;
3725 static const struct got_error *
3726 close_diff_view(struct tog_view *view)
3728 const struct got_error *err = NULL;
3729 struct tog_diff_view_state *s = &view->state.diff;
3731 free(s->id1);
3732 s->id1 = NULL;
3733 free(s->id2);
3734 s->id2 = NULL;
3735 if (s->f && fclose(s->f) == EOF)
3736 err = got_error_from_errno("fclose");
3737 s->f = NULL;
3738 if (s->f1 && fclose(s->f1) == EOF)
3739 err = got_error_from_errno("fclose");
3740 s->f1 = NULL;
3741 if (s->f2 && fclose(s->f2) == EOF)
3742 err = got_error_from_errno("fclose");
3743 s->f2 = NULL;
3744 free_colors(&s->colors);
3745 free(s->line_offsets);
3746 s->line_offsets = NULL;
3747 s->nlines = 0;
3748 return err;
3751 static const struct got_error *
3752 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3753 struct got_object_id *id2, const char *label1, const char *label2,
3754 int diff_context, int ignore_whitespace, int force_text_diff,
3755 struct tog_view *log_view, struct got_repository *repo)
3757 const struct got_error *err;
3758 struct tog_diff_view_state *s = &view->state.diff;
3760 memset(s, 0, sizeof(*s));
3762 if (id1 != NULL && id2 != NULL) {
3763 int type1, type2;
3764 err = got_object_get_type(&type1, repo, id1);
3765 if (err)
3766 return err;
3767 err = got_object_get_type(&type2, repo, id2);
3768 if (err)
3769 return err;
3771 if (type1 != type2)
3772 return got_error(GOT_ERR_OBJ_TYPE);
3774 s->first_displayed_line = 1;
3775 s->last_displayed_line = view->nlines;
3776 s->selected_line = 1;
3777 s->repo = repo;
3778 s->id1 = id1;
3779 s->id2 = id2;
3780 s->label1 = label1;
3781 s->label2 = label2;
3783 if (id1) {
3784 s->id1 = got_object_id_dup(id1);
3785 if (s->id1 == NULL)
3786 return got_error_from_errno("got_object_id_dup");
3787 s->f1 = got_opentemp();
3788 if (s->f1 == NULL) {
3789 err = got_error_from_errno("got_opentemp");
3790 goto done;
3792 } else
3793 s->id1 = NULL;
3795 s->id2 = got_object_id_dup(id2);
3796 if (s->id2 == NULL) {
3797 err = got_error_from_errno("got_object_id_dup");
3798 goto done;
3801 s->f2 = got_opentemp();
3802 if (s->f2 == NULL) {
3803 err = got_error_from_errno("got_opentemp");
3804 goto done;
3807 s->first_displayed_line = 1;
3808 s->last_displayed_line = view->nlines;
3809 s->diff_context = diff_context;
3810 s->ignore_whitespace = ignore_whitespace;
3811 s->force_text_diff = force_text_diff;
3812 s->log_view = log_view;
3813 s->repo = repo;
3815 STAILQ_INIT(&s->colors);
3816 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3817 err = add_color(&s->colors,
3818 "^-", TOG_COLOR_DIFF_MINUS,
3819 get_color_value("TOG_COLOR_DIFF_MINUS"));
3820 if (err)
3821 goto done;
3822 err = add_color(&s->colors, "^\\+",
3823 TOG_COLOR_DIFF_PLUS,
3824 get_color_value("TOG_COLOR_DIFF_PLUS"));
3825 if (err)
3826 goto done;
3827 err = add_color(&s->colors,
3828 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3829 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3830 if (err)
3831 goto done;
3833 err = add_color(&s->colors,
3834 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3835 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3836 get_color_value("TOG_COLOR_DIFF_META"));
3837 if (err)
3838 goto done;
3840 err = add_color(&s->colors,
3841 "^(from|via): ", TOG_COLOR_AUTHOR,
3842 get_color_value("TOG_COLOR_AUTHOR"));
3843 if (err)
3844 goto done;
3846 err = add_color(&s->colors,
3847 "^date: ", TOG_COLOR_DATE,
3848 get_color_value("TOG_COLOR_DATE"));
3849 if (err)
3850 goto done;
3853 if (log_view && view_is_splitscreen(view))
3854 show_log_view(log_view); /* draw vborder */
3855 diff_view_indicate_progress(view);
3857 err = create_diff(s);
3859 view->show = show_diff_view;
3860 view->input = input_diff_view;
3861 view->close = close_diff_view;
3862 view->search_start = search_start_diff_view;
3863 view->search_next = search_next_diff_view;
3864 done:
3865 if (err)
3866 close_diff_view(view);
3867 return err;
3870 static const struct got_error *
3871 show_diff_view(struct tog_view *view)
3873 const struct got_error *err;
3874 struct tog_diff_view_state *s = &view->state.diff;
3875 char *id_str1 = NULL, *id_str2, *header;
3876 const char *label1, *label2;
3878 if (s->id1) {
3879 err = got_object_id_str(&id_str1, s->id1);
3880 if (err)
3881 return err;
3882 label1 = s->label1 ? : id_str1;
3883 } else
3884 label1 = "/dev/null";
3886 err = got_object_id_str(&id_str2, s->id2);
3887 if (err)
3888 return err;
3889 label2 = s->label2 ? : id_str2;
3891 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3892 err = got_error_from_errno("asprintf");
3893 free(id_str1);
3894 free(id_str2);
3895 return err;
3897 free(id_str1);
3898 free(id_str2);
3900 err = draw_file(view, header);
3901 free(header);
3902 return err;
3905 static const struct got_error *
3906 set_selected_commit(struct tog_diff_view_state *s,
3907 struct commit_queue_entry *entry)
3909 const struct got_error *err;
3910 const struct got_object_id_queue *parent_ids;
3911 struct got_commit_object *selected_commit;
3912 struct got_object_qid *pid;
3914 free(s->id2);
3915 s->id2 = got_object_id_dup(entry->id);
3916 if (s->id2 == NULL)
3917 return got_error_from_errno("got_object_id_dup");
3919 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3920 if (err)
3921 return err;
3922 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3923 free(s->id1);
3924 pid = STAILQ_FIRST(parent_ids);
3925 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3926 got_object_commit_close(selected_commit);
3927 return NULL;
3930 static const struct got_error *
3931 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3933 const struct got_error *err = NULL;
3934 struct tog_diff_view_state *s = &view->state.diff;
3935 struct tog_log_view_state *ls;
3936 struct commit_queue_entry *old_selected_entry;
3937 char *line = NULL;
3938 size_t linesize = 0;
3939 ssize_t linelen;
3940 int i, nscroll = view->nlines - 1;
3942 switch (ch) {
3943 case '0':
3944 view->x = 0;
3945 break;
3946 case '$':
3947 view->x = MAX(view->maxx - view->ncols / 3, 0);
3948 break;
3949 case KEY_RIGHT:
3950 case 'l':
3951 if (view->x + view->ncols / 3 < view->maxx)
3952 view->x += 2; /* move two columns right */
3953 break;
3954 case KEY_LEFT:
3955 case 'h':
3956 view->x -= MIN(view->x, 2); /* move two columns back */
3957 break;
3958 case 'a':
3959 case 'w':
3960 if (ch == 'a')
3961 s->force_text_diff = !s->force_text_diff;
3962 if (ch == 'w')
3963 s->ignore_whitespace = !s->ignore_whitespace;
3964 wclear(view->window);
3965 s->first_displayed_line = 1;
3966 s->last_displayed_line = view->nlines;
3967 s->matched_line = 0;
3968 diff_view_indicate_progress(view);
3969 err = create_diff(s);
3970 break;
3971 case 'g':
3972 case KEY_HOME:
3973 s->first_displayed_line = 1;
3974 break;
3975 case 'G':
3976 case KEY_END:
3977 if (s->eof)
3978 break;
3980 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3981 s->eof = 1;
3982 break;
3983 case 'k':
3984 case KEY_UP:
3985 case CTRL('p'):
3986 if (s->first_displayed_line > 1)
3987 s->first_displayed_line--;
3988 break;
3989 case CTRL('u'):
3990 case 'u':
3991 nscroll /= 2;
3992 /* FALL THROUGH */
3993 case KEY_PPAGE:
3994 case CTRL('b'):
3995 if (s->first_displayed_line == 1)
3996 break;
3997 i = 0;
3998 while (i++ < nscroll && s->first_displayed_line > 1)
3999 s->first_displayed_line--;
4000 break;
4001 case 'j':
4002 case KEY_DOWN:
4003 case CTRL('n'):
4004 if (!s->eof)
4005 s->first_displayed_line++;
4006 break;
4007 case CTRL('d'):
4008 case 'd':
4009 nscroll /= 2;
4010 /* FALL THROUGH */
4011 case KEY_NPAGE:
4012 case CTRL('f'):
4013 case ' ':
4014 if (s->eof)
4015 break;
4016 i = 0;
4017 while (!s->eof && i++ < nscroll) {
4018 linelen = getline(&line, &linesize, s->f);
4019 s->first_displayed_line++;
4020 if (linelen == -1) {
4021 if (feof(s->f)) {
4022 s->eof = 1;
4023 } else
4024 err = got_ferror(s->f, GOT_ERR_IO);
4025 break;
4028 free(line);
4029 break;
4030 case '[':
4031 if (s->diff_context > 0) {
4032 s->diff_context--;
4033 s->matched_line = 0;
4034 diff_view_indicate_progress(view);
4035 err = create_diff(s);
4036 if (s->first_displayed_line + view->nlines - 1 >
4037 s->nlines) {
4038 s->first_displayed_line = 1;
4039 s->last_displayed_line = view->nlines;
4042 break;
4043 case ']':
4044 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4045 s->diff_context++;
4046 s->matched_line = 0;
4047 diff_view_indicate_progress(view);
4048 err = create_diff(s);
4050 break;
4051 case '<':
4052 case ',':
4053 if (s->log_view == NULL)
4054 break;
4055 ls = &s->log_view->state.log;
4056 old_selected_entry = ls->selected_entry;
4058 err = input_log_view(NULL, s->log_view, KEY_UP);
4059 if (err)
4060 break;
4062 if (old_selected_entry == ls->selected_entry)
4063 break;
4065 err = set_selected_commit(s, ls->selected_entry);
4066 if (err)
4067 break;
4069 s->first_displayed_line = 1;
4070 s->last_displayed_line = view->nlines;
4071 s->matched_line = 0;
4072 view->x = 0;
4074 diff_view_indicate_progress(view);
4075 err = create_diff(s);
4076 break;
4077 case '>':
4078 case '.':
4079 if (s->log_view == NULL)
4080 break;
4081 ls = &s->log_view->state.log;
4082 old_selected_entry = ls->selected_entry;
4084 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4085 if (err)
4086 break;
4088 if (old_selected_entry == ls->selected_entry)
4089 break;
4091 err = set_selected_commit(s, ls->selected_entry);
4092 if (err)
4093 break;
4095 s->first_displayed_line = 1;
4096 s->last_displayed_line = view->nlines;
4097 s->matched_line = 0;
4098 view->x = 0;
4100 diff_view_indicate_progress(view);
4101 err = create_diff(s);
4102 break;
4103 default:
4104 break;
4107 return err;
4110 static const struct got_error *
4111 cmd_diff(int argc, char *argv[])
4113 const struct got_error *error = NULL;
4114 struct got_repository *repo = NULL;
4115 struct got_worktree *worktree = NULL;
4116 struct got_object_id *id1 = NULL, *id2 = NULL;
4117 char *repo_path = NULL, *cwd = NULL;
4118 char *id_str1 = NULL, *id_str2 = NULL;
4119 char *label1 = NULL, *label2 = NULL;
4120 int diff_context = 3, ignore_whitespace = 0;
4121 int ch, force_text_diff = 0;
4122 const char *errstr;
4123 struct tog_view *view;
4124 int *pack_fds = NULL;
4126 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4127 switch (ch) {
4128 case 'a':
4129 force_text_diff = 1;
4130 break;
4131 case 'C':
4132 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4133 &errstr);
4134 if (errstr != NULL)
4135 errx(1, "number of context lines is %s: %s",
4136 errstr, errstr);
4137 break;
4138 case 'r':
4139 repo_path = realpath(optarg, NULL);
4140 if (repo_path == NULL)
4141 return got_error_from_errno2("realpath",
4142 optarg);
4143 got_path_strip_trailing_slashes(repo_path);
4144 break;
4145 case 'w':
4146 ignore_whitespace = 1;
4147 break;
4148 default:
4149 usage_diff();
4150 /* NOTREACHED */
4154 argc -= optind;
4155 argv += optind;
4157 if (argc == 0) {
4158 usage_diff(); /* TODO show local worktree changes */
4159 } else if (argc == 2) {
4160 id_str1 = argv[0];
4161 id_str2 = argv[1];
4162 } else
4163 usage_diff();
4165 error = got_repo_pack_fds_open(&pack_fds);
4166 if (error)
4167 goto done;
4169 if (repo_path == NULL) {
4170 cwd = getcwd(NULL, 0);
4171 if (cwd == NULL)
4172 return got_error_from_errno("getcwd");
4173 error = got_worktree_open(&worktree, cwd);
4174 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4175 goto done;
4176 if (worktree)
4177 repo_path =
4178 strdup(got_worktree_get_repo_path(worktree));
4179 else
4180 repo_path = strdup(cwd);
4181 if (repo_path == NULL) {
4182 error = got_error_from_errno("strdup");
4183 goto done;
4187 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4188 if (error)
4189 goto done;
4191 init_curses();
4193 error = apply_unveil(got_repo_get_path(repo), NULL);
4194 if (error)
4195 goto done;
4197 error = tog_load_refs(repo, 0);
4198 if (error)
4199 goto done;
4201 error = got_repo_match_object_id(&id1, &label1, id_str1,
4202 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4203 if (error)
4204 goto done;
4206 error = got_repo_match_object_id(&id2, &label2, id_str2,
4207 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4208 if (error)
4209 goto done;
4211 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4212 if (view == NULL) {
4213 error = got_error_from_errno("view_open");
4214 goto done;
4216 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4217 ignore_whitespace, force_text_diff, NULL, repo);
4218 if (error)
4219 goto done;
4220 error = view_loop(view);
4221 done:
4222 free(label1);
4223 free(label2);
4224 free(repo_path);
4225 free(cwd);
4226 if (repo) {
4227 const struct got_error *close_err = got_repo_close(repo);
4228 if (error == NULL)
4229 error = close_err;
4231 if (worktree)
4232 got_worktree_close(worktree);
4233 if (pack_fds) {
4234 const struct got_error *pack_err =
4235 got_repo_pack_fds_close(pack_fds);
4236 if (error == NULL)
4237 error = pack_err;
4239 tog_free_refs();
4240 return error;
4243 __dead static void
4244 usage_blame(void)
4246 endwin();
4247 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4248 getprogname());
4249 exit(1);
4252 struct tog_blame_line {
4253 int annotated;
4254 struct got_object_id *id;
4257 static const struct got_error *
4258 draw_blame(struct tog_view *view)
4260 struct tog_blame_view_state *s = &view->state.blame;
4261 struct tog_blame *blame = &s->blame;
4262 regmatch_t *regmatch = &view->regmatch;
4263 const struct got_error *err;
4264 int lineno = 0, nprinted = 0, i;
4265 char *line = NULL;
4266 size_t linesize = 0;
4267 ssize_t linelen;
4268 wchar_t *wline;
4269 int width;
4270 struct tog_blame_line *blame_line;
4271 struct got_object_id *prev_id = NULL;
4272 char *id_str;
4273 struct tog_color *tc;
4275 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4276 if (err)
4277 return err;
4279 rewind(blame->f);
4280 werase(view->window);
4282 if (asprintf(&line, "commit %s", id_str) == -1) {
4283 err = got_error_from_errno("asprintf");
4284 free(id_str);
4285 return err;
4288 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4289 free(line);
4290 line = NULL;
4291 if (err)
4292 return err;
4293 if (view_needs_focus_indication(view))
4294 wstandout(view->window);
4295 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4296 if (tc)
4297 wattr_on(view->window,
4298 COLOR_PAIR(tc->colorpair), NULL);
4299 waddwstr(view->window, wline);
4300 if (tc)
4301 wattr_off(view->window,
4302 COLOR_PAIR(tc->colorpair), NULL);
4303 if (view_needs_focus_indication(view))
4304 wstandend(view->window);
4305 free(wline);
4306 wline = NULL;
4307 if (width < view->ncols - 1)
4308 waddch(view->window, '\n');
4310 if (asprintf(&line, "[%d/%d] %s%s",
4311 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4312 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4313 free(id_str);
4314 return got_error_from_errno("asprintf");
4316 free(id_str);
4317 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4318 free(line);
4319 line = NULL;
4320 if (err)
4321 return err;
4322 waddwstr(view->window, wline);
4323 free(wline);
4324 wline = NULL;
4325 if (width < view->ncols - 1)
4326 waddch(view->window, '\n');
4328 s->eof = 0;
4329 view->maxx = 0;
4330 while (nprinted < view->nlines - 2) {
4331 linelen = getline(&line, &linesize, blame->f);
4332 if (linelen == -1) {
4333 if (feof(blame->f)) {
4334 s->eof = 1;
4335 break;
4337 free(line);
4338 return got_ferror(blame->f, GOT_ERR_IO);
4340 if (++lineno < s->first_displayed_line)
4341 continue;
4343 view->maxx = MAX(view->maxx, linelen);
4345 if (view->focussed && nprinted == s->selected_line - 1)
4346 wstandout(view->window);
4348 if (blame->nlines > 0) {
4349 blame_line = &blame->lines[lineno - 1];
4350 if (blame_line->annotated && prev_id &&
4351 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4352 !(view->focussed &&
4353 nprinted == s->selected_line - 1)) {
4354 waddstr(view->window, " ");
4355 } else if (blame_line->annotated) {
4356 char *id_str;
4357 err = got_object_id_str(&id_str, blame_line->id);
4358 if (err) {
4359 free(line);
4360 return err;
4362 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4363 if (tc)
4364 wattr_on(view->window,
4365 COLOR_PAIR(tc->colorpair), NULL);
4366 wprintw(view->window, "%.8s", id_str);
4367 if (tc)
4368 wattr_off(view->window,
4369 COLOR_PAIR(tc->colorpair), NULL);
4370 free(id_str);
4371 prev_id = blame_line->id;
4372 } else {
4373 waddstr(view->window, "........");
4374 prev_id = NULL;
4376 } else {
4377 waddstr(view->window, "........");
4378 prev_id = NULL;
4381 if (view->focussed && nprinted == s->selected_line - 1)
4382 wstandend(view->window);
4383 waddstr(view->window, " ");
4385 if (view->ncols <= 9) {
4386 width = 9;
4387 wline = wcsdup(L"");
4388 if (wline == NULL) {
4389 err = got_error_from_errno("wcsdup");
4390 free(line);
4391 return err;
4393 } else if (s->first_displayed_line + nprinted ==
4394 s->matched_line &&
4395 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4396 err = add_matched_line(&width, line, view->ncols - 9, 9,
4397 view->window, view->x, regmatch);
4398 if (err) {
4399 free(line);
4400 return err;
4402 width += 9;
4403 } else {
4404 err = format_line(&wline, &width, NULL, line, 0,
4405 view->x + view->ncols - 9, 9, 1);
4406 if (err) {
4407 free(line);
4408 return err;
4410 if (view->x < width) {
4411 waddwstr(view->window, wline + view->x);
4412 for (i = 0; i < view->x; i++)
4413 width -= wcwidth(wline[i]);
4415 width += 9;
4416 free(wline);
4417 wline = NULL;
4420 if (width <= view->ncols - 1)
4421 waddch(view->window, '\n');
4422 if (++nprinted == 1)
4423 s->first_displayed_line = lineno;
4425 free(line);
4426 s->last_displayed_line = lineno;
4428 view_vborder(view);
4430 return NULL;
4433 static const struct got_error *
4434 blame_cb(void *arg, int nlines, int lineno,
4435 struct got_commit_object *commit, struct got_object_id *id)
4437 const struct got_error *err = NULL;
4438 struct tog_blame_cb_args *a = arg;
4439 struct tog_blame_line *line;
4440 int errcode;
4442 if (nlines != a->nlines ||
4443 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4444 return got_error(GOT_ERR_RANGE);
4446 errcode = pthread_mutex_lock(&tog_mutex);
4447 if (errcode)
4448 return got_error_set_errno(errcode, "pthread_mutex_lock");
4450 if (*a->quit) { /* user has quit the blame view */
4451 err = got_error(GOT_ERR_ITER_COMPLETED);
4452 goto done;
4455 if (lineno == -1)
4456 goto done; /* no change in this commit */
4458 line = &a->lines[lineno - 1];
4459 if (line->annotated)
4460 goto done;
4462 line->id = got_object_id_dup(id);
4463 if (line->id == NULL) {
4464 err = got_error_from_errno("got_object_id_dup");
4465 goto done;
4467 line->annotated = 1;
4468 done:
4469 errcode = pthread_mutex_unlock(&tog_mutex);
4470 if (errcode)
4471 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4472 return err;
4475 static void *
4476 blame_thread(void *arg)
4478 const struct got_error *err, *close_err;
4479 struct tog_blame_thread_args *ta = arg;
4480 struct tog_blame_cb_args *a = ta->cb_args;
4481 int errcode;
4483 err = block_signals_used_by_main_thread();
4484 if (err)
4485 return (void *)err;
4487 err = got_blame(ta->path, a->commit_id, ta->repo,
4488 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4489 if (err && err->code == GOT_ERR_CANCELLED)
4490 err = NULL;
4492 errcode = pthread_mutex_lock(&tog_mutex);
4493 if (errcode)
4494 return (void *)got_error_set_errno(errcode,
4495 "pthread_mutex_lock");
4497 close_err = got_repo_close(ta->repo);
4498 if (err == NULL)
4499 err = close_err;
4500 ta->repo = NULL;
4501 *ta->complete = 1;
4503 errcode = pthread_mutex_unlock(&tog_mutex);
4504 if (errcode && err == NULL)
4505 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4507 return (void *)err;
4510 static struct got_object_id *
4511 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4512 int first_displayed_line, int selected_line)
4514 struct tog_blame_line *line;
4516 if (nlines <= 0)
4517 return NULL;
4519 line = &lines[first_displayed_line - 1 + selected_line - 1];
4520 if (!line->annotated)
4521 return NULL;
4523 return line->id;
4526 static const struct got_error *
4527 stop_blame(struct tog_blame *blame)
4529 const struct got_error *err = NULL;
4530 int i;
4532 if (blame->thread) {
4533 int errcode;
4534 errcode = pthread_mutex_unlock(&tog_mutex);
4535 if (errcode)
4536 return got_error_set_errno(errcode,
4537 "pthread_mutex_unlock");
4538 errcode = pthread_join(blame->thread, (void **)&err);
4539 if (errcode)
4540 return got_error_set_errno(errcode, "pthread_join");
4541 errcode = pthread_mutex_lock(&tog_mutex);
4542 if (errcode)
4543 return got_error_set_errno(errcode,
4544 "pthread_mutex_lock");
4545 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4546 err = NULL;
4547 blame->thread = NULL;
4549 if (blame->thread_args.repo) {
4550 const struct got_error *close_err;
4551 close_err = got_repo_close(blame->thread_args.repo);
4552 if (err == NULL)
4553 err = close_err;
4554 blame->thread_args.repo = NULL;
4556 if (blame->f) {
4557 if (fclose(blame->f) == EOF && err == NULL)
4558 err = got_error_from_errno("fclose");
4559 blame->f = NULL;
4561 if (blame->lines) {
4562 for (i = 0; i < blame->nlines; i++)
4563 free(blame->lines[i].id);
4564 free(blame->lines);
4565 blame->lines = NULL;
4567 free(blame->cb_args.commit_id);
4568 blame->cb_args.commit_id = NULL;
4569 if (blame->pack_fds) {
4570 const struct got_error *pack_err =
4571 got_repo_pack_fds_close(blame->pack_fds);
4572 if (err == NULL)
4573 err = pack_err;
4574 blame->pack_fds = NULL;
4576 return err;
4579 static const struct got_error *
4580 cancel_blame_view(void *arg)
4582 const struct got_error *err = NULL;
4583 int *done = arg;
4584 int errcode;
4586 errcode = pthread_mutex_lock(&tog_mutex);
4587 if (errcode)
4588 return got_error_set_errno(errcode,
4589 "pthread_mutex_unlock");
4591 if (*done)
4592 err = got_error(GOT_ERR_CANCELLED);
4594 errcode = pthread_mutex_unlock(&tog_mutex);
4595 if (errcode)
4596 return got_error_set_errno(errcode,
4597 "pthread_mutex_lock");
4599 return err;
4602 static const struct got_error *
4603 run_blame(struct tog_view *view)
4605 struct tog_blame_view_state *s = &view->state.blame;
4606 struct tog_blame *blame = &s->blame;
4607 const struct got_error *err = NULL;
4608 struct got_commit_object *commit = NULL;
4609 struct got_blob_object *blob = NULL;
4610 struct got_repository *thread_repo = NULL;
4611 struct got_object_id *obj_id = NULL;
4612 int obj_type;
4613 int *pack_fds = NULL;
4615 err = got_object_open_as_commit(&commit, s->repo,
4616 &s->blamed_commit->id);
4617 if (err)
4618 return err;
4620 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4621 if (err)
4622 goto done;
4624 err = got_object_get_type(&obj_type, s->repo, obj_id);
4625 if (err)
4626 goto done;
4628 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4629 err = got_error(GOT_ERR_OBJ_TYPE);
4630 goto done;
4633 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4634 if (err)
4635 goto done;
4636 blame->f = got_opentemp();
4637 if (blame->f == NULL) {
4638 err = got_error_from_errno("got_opentemp");
4639 goto done;
4641 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4642 &blame->line_offsets, blame->f, blob);
4643 if (err)
4644 goto done;
4645 if (blame->nlines == 0) {
4646 s->blame_complete = 1;
4647 goto done;
4650 /* Don't include \n at EOF in the blame line count. */
4651 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4652 blame->nlines--;
4654 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4655 if (blame->lines == NULL) {
4656 err = got_error_from_errno("calloc");
4657 goto done;
4660 err = got_repo_pack_fds_open(&pack_fds);
4661 if (err)
4662 goto done;
4663 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4664 pack_fds);
4665 if (err)
4666 goto done;
4668 blame->pack_fds = pack_fds;
4669 blame->cb_args.view = view;
4670 blame->cb_args.lines = blame->lines;
4671 blame->cb_args.nlines = blame->nlines;
4672 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4673 if (blame->cb_args.commit_id == NULL) {
4674 err = got_error_from_errno("got_object_id_dup");
4675 goto done;
4677 blame->cb_args.quit = &s->done;
4679 blame->thread_args.path = s->path;
4680 blame->thread_args.repo = thread_repo;
4681 blame->thread_args.cb_args = &blame->cb_args;
4682 blame->thread_args.complete = &s->blame_complete;
4683 blame->thread_args.cancel_cb = cancel_blame_view;
4684 blame->thread_args.cancel_arg = &s->done;
4685 s->blame_complete = 0;
4687 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4688 s->first_displayed_line = 1;
4689 s->last_displayed_line = view->nlines;
4690 s->selected_line = 1;
4692 s->matched_line = 0;
4694 done:
4695 if (commit)
4696 got_object_commit_close(commit);
4697 if (blob)
4698 got_object_blob_close(blob);
4699 free(obj_id);
4700 if (err)
4701 stop_blame(blame);
4702 return err;
4705 static const struct got_error *
4706 open_blame_view(struct tog_view *view, char *path,
4707 struct got_object_id *commit_id, struct got_repository *repo)
4709 const struct got_error *err = NULL;
4710 struct tog_blame_view_state *s = &view->state.blame;
4712 STAILQ_INIT(&s->blamed_commits);
4714 s->path = strdup(path);
4715 if (s->path == NULL)
4716 return got_error_from_errno("strdup");
4718 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4719 if (err) {
4720 free(s->path);
4721 return err;
4724 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4725 s->first_displayed_line = 1;
4726 s->last_displayed_line = view->nlines;
4727 s->selected_line = 1;
4728 s->blame_complete = 0;
4729 s->repo = repo;
4730 s->commit_id = commit_id;
4731 memset(&s->blame, 0, sizeof(s->blame));
4733 STAILQ_INIT(&s->colors);
4734 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4735 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4736 get_color_value("TOG_COLOR_COMMIT"));
4737 if (err)
4738 return err;
4741 view->show = show_blame_view;
4742 view->input = input_blame_view;
4743 view->close = close_blame_view;
4744 view->search_start = search_start_blame_view;
4745 view->search_next = search_next_blame_view;
4747 return run_blame(view);
4750 static const struct got_error *
4751 close_blame_view(struct tog_view *view)
4753 const struct got_error *err = NULL;
4754 struct tog_blame_view_state *s = &view->state.blame;
4756 if (s->blame.thread)
4757 err = stop_blame(&s->blame);
4759 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4760 struct got_object_qid *blamed_commit;
4761 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4762 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4763 got_object_qid_free(blamed_commit);
4766 free(s->path);
4767 free_colors(&s->colors);
4768 return err;
4771 static const struct got_error *
4772 search_start_blame_view(struct tog_view *view)
4774 struct tog_blame_view_state *s = &view->state.blame;
4776 s->matched_line = 0;
4777 return NULL;
4780 static const struct got_error *
4781 search_next_blame_view(struct tog_view *view)
4783 struct tog_blame_view_state *s = &view->state.blame;
4784 const struct got_error *err = NULL;
4785 int lineno;
4786 char *exstr = NULL, *line = NULL;
4787 size_t linesize = 0;
4788 ssize_t linelen;
4790 if (!view->searching) {
4791 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4792 return NULL;
4795 if (s->matched_line) {
4796 if (view->searching == TOG_SEARCH_FORWARD)
4797 lineno = s->matched_line + 1;
4798 else
4799 lineno = s->matched_line - 1;
4800 } else
4801 lineno = s->first_displayed_line - 1 + s->selected_line;
4803 while (1) {
4804 off_t offset;
4806 if (lineno <= 0 || lineno > s->blame.nlines) {
4807 if (s->matched_line == 0) {
4808 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4809 break;
4812 if (view->searching == TOG_SEARCH_FORWARD)
4813 lineno = 1;
4814 else
4815 lineno = s->blame.nlines;
4818 offset = s->blame.line_offsets[lineno - 1];
4819 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4820 free(line);
4821 return got_error_from_errno("fseeko");
4823 linelen = getline(&line, &linesize, s->blame.f);
4824 err = expand_tab(&exstr, line);
4825 if (err)
4826 break;
4827 if (linelen != -1 &&
4828 match_line(exstr, &view->regex, 1, &view->regmatch)) {
4829 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4830 s->matched_line = lineno;
4831 break;
4833 free(exstr);
4834 exstr = NULL;
4835 if (view->searching == TOG_SEARCH_FORWARD)
4836 lineno++;
4837 else
4838 lineno--;
4840 free(line);
4841 free(exstr);
4843 if (s->matched_line) {
4844 s->first_displayed_line = s->matched_line;
4845 s->selected_line = 1;
4848 return err;
4851 static const struct got_error *
4852 show_blame_view(struct tog_view *view)
4854 const struct got_error *err = NULL;
4855 struct tog_blame_view_state *s = &view->state.blame;
4856 int errcode;
4858 if (s->blame.thread == NULL && !s->blame_complete) {
4859 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4860 &s->blame.thread_args);
4861 if (errcode)
4862 return got_error_set_errno(errcode, "pthread_create");
4864 halfdelay(1); /* fast refresh while annotating */
4867 if (s->blame_complete)
4868 halfdelay(10); /* disable fast refresh */
4870 err = draw_blame(view);
4872 view_vborder(view);
4873 return err;
4876 static const struct got_error *
4877 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4879 const struct got_error *err = NULL, *thread_err = NULL;
4880 struct tog_view *diff_view;
4881 struct tog_blame_view_state *s = &view->state.blame;
4882 int begin_x = 0, nscroll = view->nlines - 2;
4884 switch (ch) {
4885 case '0':
4886 view->x = 0;
4887 break;
4888 case '$':
4889 view->x = MAX(view->maxx - view->ncols / 3, 0);
4890 break;
4891 case KEY_RIGHT:
4892 case 'l':
4893 if (view->x + view->ncols / 3 < view->maxx)
4894 view->x += 2; /* move two columns right */
4895 break;
4896 case KEY_LEFT:
4897 case 'h':
4898 view->x -= MIN(view->x, 2); /* move two columns back */
4899 break;
4900 case 'q':
4901 s->done = 1;
4902 break;
4903 case 'g':
4904 case KEY_HOME:
4905 s->selected_line = 1;
4906 s->first_displayed_line = 1;
4907 break;
4908 case 'G':
4909 case KEY_END:
4910 if (s->blame.nlines < view->nlines - 2) {
4911 s->selected_line = s->blame.nlines;
4912 s->first_displayed_line = 1;
4913 } else {
4914 s->selected_line = view->nlines - 2;
4915 s->first_displayed_line = s->blame.nlines -
4916 (view->nlines - 3);
4918 break;
4919 case 'k':
4920 case KEY_UP:
4921 case CTRL('p'):
4922 if (s->selected_line > 1)
4923 s->selected_line--;
4924 else if (s->selected_line == 1 &&
4925 s->first_displayed_line > 1)
4926 s->first_displayed_line--;
4927 break;
4928 case CTRL('u'):
4929 case 'u':
4930 nscroll /= 2;
4931 /* FALL THROUGH */
4932 case KEY_PPAGE:
4933 case CTRL('b'):
4934 if (s->first_displayed_line == 1) {
4935 s->selected_line = MAX(1, s->selected_line - nscroll);
4936 break;
4938 if (s->first_displayed_line > nscroll)
4939 s->first_displayed_line -= nscroll;
4940 else
4941 s->first_displayed_line = 1;
4942 break;
4943 case 'j':
4944 case KEY_DOWN:
4945 case CTRL('n'):
4946 if (s->selected_line < view->nlines - 2 &&
4947 s->first_displayed_line +
4948 s->selected_line <= s->blame.nlines)
4949 s->selected_line++;
4950 else if (s->last_displayed_line <
4951 s->blame.nlines)
4952 s->first_displayed_line++;
4953 break;
4954 case 'b':
4955 case 'p': {
4956 struct got_object_id *id = NULL;
4957 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4958 s->first_displayed_line, s->selected_line);
4959 if (id == NULL)
4960 break;
4961 if (ch == 'p') {
4962 struct got_commit_object *commit, *pcommit;
4963 struct got_object_qid *pid;
4964 struct got_object_id *blob_id = NULL;
4965 int obj_type;
4966 err = got_object_open_as_commit(&commit,
4967 s->repo, id);
4968 if (err)
4969 break;
4970 pid = STAILQ_FIRST(
4971 got_object_commit_get_parent_ids(commit));
4972 if (pid == NULL) {
4973 got_object_commit_close(commit);
4974 break;
4976 /* Check if path history ends here. */
4977 err = got_object_open_as_commit(&pcommit,
4978 s->repo, &pid->id);
4979 if (err)
4980 break;
4981 err = got_object_id_by_path(&blob_id, s->repo,
4982 pcommit, s->path);
4983 got_object_commit_close(pcommit);
4984 if (err) {
4985 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4986 err = NULL;
4987 got_object_commit_close(commit);
4988 break;
4990 err = got_object_get_type(&obj_type, s->repo,
4991 blob_id);
4992 free(blob_id);
4993 /* Can't blame non-blob type objects. */
4994 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4995 got_object_commit_close(commit);
4996 break;
4998 err = got_object_qid_alloc(&s->blamed_commit,
4999 &pid->id);
5000 got_object_commit_close(commit);
5001 } else {
5002 if (got_object_id_cmp(id,
5003 &s->blamed_commit->id) == 0)
5004 break;
5005 err = got_object_qid_alloc(&s->blamed_commit,
5006 id);
5008 if (err)
5009 break;
5010 s->done = 1;
5011 thread_err = stop_blame(&s->blame);
5012 s->done = 0;
5013 if (thread_err)
5014 break;
5015 STAILQ_INSERT_HEAD(&s->blamed_commits,
5016 s->blamed_commit, entry);
5017 err = run_blame(view);
5018 if (err)
5019 break;
5020 break;
5022 case 'B': {
5023 struct got_object_qid *first;
5024 first = STAILQ_FIRST(&s->blamed_commits);
5025 if (!got_object_id_cmp(&first->id, s->commit_id))
5026 break;
5027 s->done = 1;
5028 thread_err = stop_blame(&s->blame);
5029 s->done = 0;
5030 if (thread_err)
5031 break;
5032 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5033 got_object_qid_free(s->blamed_commit);
5034 s->blamed_commit =
5035 STAILQ_FIRST(&s->blamed_commits);
5036 err = run_blame(view);
5037 if (err)
5038 break;
5039 break;
5041 case KEY_ENTER:
5042 case '\r': {
5043 struct got_object_id *id = NULL;
5044 struct got_object_qid *pid;
5045 struct got_commit_object *commit = NULL;
5046 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5047 s->first_displayed_line, s->selected_line);
5048 if (id == NULL)
5049 break;
5050 err = got_object_open_as_commit(&commit, s->repo, id);
5051 if (err)
5052 break;
5053 pid = STAILQ_FIRST(
5054 got_object_commit_get_parent_ids(commit));
5055 if (view_is_parent_view(view))
5056 begin_x = view_split_begin_x(view->begin_x);
5057 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5058 if (diff_view == NULL) {
5059 got_object_commit_close(commit);
5060 err = got_error_from_errno("view_open");
5061 break;
5063 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5064 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5065 got_object_commit_close(commit);
5066 if (err) {
5067 view_close(diff_view);
5068 break;
5070 view->focussed = 0;
5071 diff_view->focussed = 1;
5072 if (view_is_parent_view(view)) {
5073 err = view_close_child(view);
5074 if (err)
5075 break;
5076 view_set_child(view, diff_view);
5077 view->focus_child = 1;
5078 } else
5079 *new_view = diff_view;
5080 if (err)
5081 break;
5082 break;
5084 case CTRL('d'):
5085 case 'd':
5086 nscroll /= 2;
5087 /* FALL THROUGH */
5088 case KEY_NPAGE:
5089 case CTRL('f'):
5090 case ' ':
5091 if (s->last_displayed_line >= s->blame.nlines &&
5092 s->selected_line >= MIN(s->blame.nlines,
5093 view->nlines - 2)) {
5094 break;
5096 if (s->last_displayed_line >= s->blame.nlines &&
5097 s->selected_line < view->nlines - 2) {
5098 s->selected_line +=
5099 MIN(nscroll, s->last_displayed_line -
5100 s->first_displayed_line - s->selected_line + 1);
5102 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5103 s->first_displayed_line += nscroll;
5104 else
5105 s->first_displayed_line =
5106 s->blame.nlines - (view->nlines - 3);
5107 break;
5108 case KEY_RESIZE:
5109 if (s->selected_line > view->nlines - 2) {
5110 s->selected_line = MIN(s->blame.nlines,
5111 view->nlines - 2);
5113 break;
5114 default:
5115 break;
5117 return thread_err ? thread_err : err;
5120 static const struct got_error *
5121 cmd_blame(int argc, char *argv[])
5123 const struct got_error *error;
5124 struct got_repository *repo = NULL;
5125 struct got_worktree *worktree = NULL;
5126 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5127 char *link_target = NULL;
5128 struct got_object_id *commit_id = NULL;
5129 struct got_commit_object *commit = NULL;
5130 char *commit_id_str = NULL;
5131 int ch;
5132 struct tog_view *view;
5133 int *pack_fds = NULL;
5135 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5136 switch (ch) {
5137 case 'c':
5138 commit_id_str = optarg;
5139 break;
5140 case 'r':
5141 repo_path = realpath(optarg, NULL);
5142 if (repo_path == NULL)
5143 return got_error_from_errno2("realpath",
5144 optarg);
5145 break;
5146 default:
5147 usage_blame();
5148 /* NOTREACHED */
5152 argc -= optind;
5153 argv += optind;
5155 if (argc != 1)
5156 usage_blame();
5158 error = got_repo_pack_fds_open(&pack_fds);
5159 if (error != NULL)
5160 goto done;
5162 if (repo_path == NULL) {
5163 cwd = getcwd(NULL, 0);
5164 if (cwd == NULL)
5165 return got_error_from_errno("getcwd");
5166 error = got_worktree_open(&worktree, cwd);
5167 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5168 goto done;
5169 if (worktree)
5170 repo_path =
5171 strdup(got_worktree_get_repo_path(worktree));
5172 else
5173 repo_path = strdup(cwd);
5174 if (repo_path == NULL) {
5175 error = got_error_from_errno("strdup");
5176 goto done;
5180 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5181 if (error != NULL)
5182 goto done;
5184 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5185 worktree);
5186 if (error)
5187 goto done;
5189 init_curses();
5191 error = apply_unveil(got_repo_get_path(repo), NULL);
5192 if (error)
5193 goto done;
5195 error = tog_load_refs(repo, 0);
5196 if (error)
5197 goto done;
5199 if (commit_id_str == NULL) {
5200 struct got_reference *head_ref;
5201 error = got_ref_open(&head_ref, repo, worktree ?
5202 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5203 if (error != NULL)
5204 goto done;
5205 error = got_ref_resolve(&commit_id, repo, head_ref);
5206 got_ref_close(head_ref);
5207 } else {
5208 error = got_repo_match_object_id(&commit_id, NULL,
5209 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5211 if (error != NULL)
5212 goto done;
5214 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5215 if (view == NULL) {
5216 error = got_error_from_errno("view_open");
5217 goto done;
5220 error = got_object_open_as_commit(&commit, repo, commit_id);
5221 if (error)
5222 goto done;
5224 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5225 commit, repo);
5226 if (error)
5227 goto done;
5229 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5230 commit_id, repo);
5231 if (error)
5232 goto done;
5233 if (worktree) {
5234 /* Release work tree lock. */
5235 got_worktree_close(worktree);
5236 worktree = NULL;
5238 error = view_loop(view);
5239 done:
5240 free(repo_path);
5241 free(in_repo_path);
5242 free(link_target);
5243 free(cwd);
5244 free(commit_id);
5245 if (commit)
5246 got_object_commit_close(commit);
5247 if (worktree)
5248 got_worktree_close(worktree);
5249 if (repo) {
5250 const struct got_error *close_err = got_repo_close(repo);
5251 if (error == NULL)
5252 error = close_err;
5254 if (pack_fds) {
5255 const struct got_error *pack_err =
5256 got_repo_pack_fds_close(pack_fds);
5257 if (error == NULL)
5258 error = pack_err;
5260 tog_free_refs();
5261 return error;
5264 static const struct got_error *
5265 draw_tree_entries(struct tog_view *view, const char *parent_path)
5267 struct tog_tree_view_state *s = &view->state.tree;
5268 const struct got_error *err = NULL;
5269 struct got_tree_entry *te;
5270 wchar_t *wline;
5271 struct tog_color *tc;
5272 int width, n, i, nentries;
5273 int limit = view->nlines;
5275 s->ndisplayed = 0;
5277 werase(view->window);
5279 if (limit == 0)
5280 return NULL;
5282 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5283 0, 0);
5284 if (err)
5285 return err;
5286 if (view_needs_focus_indication(view))
5287 wstandout(view->window);
5288 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5289 if (tc)
5290 wattr_on(view->window,
5291 COLOR_PAIR(tc->colorpair), NULL);
5292 waddwstr(view->window, wline);
5293 if (tc)
5294 wattr_off(view->window,
5295 COLOR_PAIR(tc->colorpair), NULL);
5296 if (view_needs_focus_indication(view))
5297 wstandend(view->window);
5298 free(wline);
5299 wline = NULL;
5300 if (width < view->ncols - 1)
5301 waddch(view->window, '\n');
5302 if (--limit <= 0)
5303 return NULL;
5304 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5305 0, 0);
5306 if (err)
5307 return err;
5308 waddwstr(view->window, wline);
5309 free(wline);
5310 wline = NULL;
5311 if (width < view->ncols - 1)
5312 waddch(view->window, '\n');
5313 if (--limit <= 0)
5314 return NULL;
5315 waddch(view->window, '\n');
5316 if (--limit <= 0)
5317 return NULL;
5319 if (s->first_displayed_entry == NULL) {
5320 te = got_object_tree_get_first_entry(s->tree);
5321 if (s->selected == 0) {
5322 if (view->focussed)
5323 wstandout(view->window);
5324 s->selected_entry = NULL;
5326 waddstr(view->window, " ..\n"); /* parent directory */
5327 if (s->selected == 0 && view->focussed)
5328 wstandend(view->window);
5329 s->ndisplayed++;
5330 if (--limit <= 0)
5331 return NULL;
5332 n = 1;
5333 } else {
5334 n = 0;
5335 te = s->first_displayed_entry;
5338 nentries = got_object_tree_get_nentries(s->tree);
5339 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5340 char *line = NULL, *id_str = NULL, *link_target = NULL;
5341 const char *modestr = "";
5342 mode_t mode;
5344 te = got_object_tree_get_entry(s->tree, i);
5345 mode = got_tree_entry_get_mode(te);
5347 if (s->show_ids) {
5348 err = got_object_id_str(&id_str,
5349 got_tree_entry_get_id(te));
5350 if (err)
5351 return got_error_from_errno(
5352 "got_object_id_str");
5354 if (got_object_tree_entry_is_submodule(te))
5355 modestr = "$";
5356 else if (S_ISLNK(mode)) {
5357 int i;
5359 err = got_tree_entry_get_symlink_target(&link_target,
5360 te, s->repo);
5361 if (err) {
5362 free(id_str);
5363 return err;
5365 for (i = 0; i < strlen(link_target); i++) {
5366 if (!isprint((unsigned char)link_target[i]))
5367 link_target[i] = '?';
5369 modestr = "@";
5371 else if (S_ISDIR(mode))
5372 modestr = "/";
5373 else if (mode & S_IXUSR)
5374 modestr = "*";
5375 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5376 got_tree_entry_get_name(te), modestr,
5377 link_target ? " -> ": "",
5378 link_target ? link_target : "") == -1) {
5379 free(id_str);
5380 free(link_target);
5381 return got_error_from_errno("asprintf");
5383 free(id_str);
5384 free(link_target);
5385 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5386 0, 0);
5387 if (err) {
5388 free(line);
5389 break;
5391 if (n == s->selected) {
5392 if (view->focussed)
5393 wstandout(view->window);
5394 s->selected_entry = te;
5396 tc = match_color(&s->colors, line);
5397 if (tc)
5398 wattr_on(view->window,
5399 COLOR_PAIR(tc->colorpair), NULL);
5400 waddwstr(view->window, wline);
5401 if (tc)
5402 wattr_off(view->window,
5403 COLOR_PAIR(tc->colorpair), NULL);
5404 if (width < view->ncols - 1)
5405 waddch(view->window, '\n');
5406 if (n == s->selected && view->focussed)
5407 wstandend(view->window);
5408 free(line);
5409 free(wline);
5410 wline = NULL;
5411 n++;
5412 s->ndisplayed++;
5413 s->last_displayed_entry = te;
5414 if (--limit <= 0)
5415 break;
5418 return err;
5421 static void
5422 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5424 struct got_tree_entry *te;
5425 int isroot = s->tree == s->root;
5426 int i = 0;
5428 if (s->first_displayed_entry == NULL)
5429 return;
5431 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5432 while (i++ < maxscroll) {
5433 if (te == NULL) {
5434 if (!isroot)
5435 s->first_displayed_entry = NULL;
5436 break;
5438 s->first_displayed_entry = te;
5439 te = got_tree_entry_get_prev(s->tree, te);
5443 static void
5444 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5446 struct got_tree_entry *next, *last;
5447 int n = 0;
5449 if (s->first_displayed_entry)
5450 next = got_tree_entry_get_next(s->tree,
5451 s->first_displayed_entry);
5452 else
5453 next = got_object_tree_get_first_entry(s->tree);
5455 last = s->last_displayed_entry;
5456 while (next && last && n++ < maxscroll) {
5457 last = got_tree_entry_get_next(s->tree, last);
5458 if (last) {
5459 s->first_displayed_entry = next;
5460 next = got_tree_entry_get_next(s->tree, next);
5465 static const struct got_error *
5466 tree_entry_path(char **path, struct tog_parent_trees *parents,
5467 struct got_tree_entry *te)
5469 const struct got_error *err = NULL;
5470 struct tog_parent_tree *pt;
5471 size_t len = 2; /* for leading slash and NUL */
5473 TAILQ_FOREACH(pt, parents, entry)
5474 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5475 + 1 /* slash */;
5476 if (te)
5477 len += strlen(got_tree_entry_get_name(te));
5479 *path = calloc(1, len);
5480 if (path == NULL)
5481 return got_error_from_errno("calloc");
5483 (*path)[0] = '/';
5484 pt = TAILQ_LAST(parents, tog_parent_trees);
5485 while (pt) {
5486 const char *name = got_tree_entry_get_name(pt->selected_entry);
5487 if (strlcat(*path, name, len) >= len) {
5488 err = got_error(GOT_ERR_NO_SPACE);
5489 goto done;
5491 if (strlcat(*path, "/", len) >= len) {
5492 err = got_error(GOT_ERR_NO_SPACE);
5493 goto done;
5495 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5497 if (te) {
5498 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5499 err = got_error(GOT_ERR_NO_SPACE);
5500 goto done;
5503 done:
5504 if (err) {
5505 free(*path);
5506 *path = NULL;
5508 return err;
5511 static const struct got_error *
5512 blame_tree_entry(struct tog_view **new_view, int begin_x,
5513 struct got_tree_entry *te, struct tog_parent_trees *parents,
5514 struct got_object_id *commit_id, struct got_repository *repo)
5516 const struct got_error *err = NULL;
5517 char *path;
5518 struct tog_view *blame_view;
5520 *new_view = NULL;
5522 err = tree_entry_path(&path, parents, te);
5523 if (err)
5524 return err;
5526 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5527 if (blame_view == NULL) {
5528 err = got_error_from_errno("view_open");
5529 goto done;
5532 err = open_blame_view(blame_view, path, commit_id, repo);
5533 if (err) {
5534 if (err->code == GOT_ERR_CANCELLED)
5535 err = NULL;
5536 view_close(blame_view);
5537 } else
5538 *new_view = blame_view;
5539 done:
5540 free(path);
5541 return err;
5544 static const struct got_error *
5545 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5546 struct tog_tree_view_state *s)
5548 struct tog_view *log_view;
5549 const struct got_error *err = NULL;
5550 char *path;
5552 *new_view = NULL;
5554 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5555 if (log_view == NULL)
5556 return got_error_from_errno("view_open");
5558 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5559 if (err)
5560 return err;
5562 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5563 path, 0);
5564 if (err)
5565 view_close(log_view);
5566 else
5567 *new_view = log_view;
5568 free(path);
5569 return err;
5572 static const struct got_error *
5573 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5574 const char *head_ref_name, struct got_repository *repo)
5576 const struct got_error *err = NULL;
5577 char *commit_id_str = NULL;
5578 struct tog_tree_view_state *s = &view->state.tree;
5579 struct got_commit_object *commit = NULL;
5581 TAILQ_INIT(&s->parents);
5582 STAILQ_INIT(&s->colors);
5584 s->commit_id = got_object_id_dup(commit_id);
5585 if (s->commit_id == NULL)
5586 return got_error_from_errno("got_object_id_dup");
5588 err = got_object_open_as_commit(&commit, repo, commit_id);
5589 if (err)
5590 goto done;
5593 * The root is opened here and will be closed when the view is closed.
5594 * Any visited subtrees and their path-wise parents are opened and
5595 * closed on demand.
5597 err = got_object_open_as_tree(&s->root, repo,
5598 got_object_commit_get_tree_id(commit));
5599 if (err)
5600 goto done;
5601 s->tree = s->root;
5603 err = got_object_id_str(&commit_id_str, commit_id);
5604 if (err != NULL)
5605 goto done;
5607 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5608 err = got_error_from_errno("asprintf");
5609 goto done;
5612 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5613 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5614 if (head_ref_name) {
5615 s->head_ref_name = strdup(head_ref_name);
5616 if (s->head_ref_name == NULL) {
5617 err = got_error_from_errno("strdup");
5618 goto done;
5621 s->repo = repo;
5623 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5624 err = add_color(&s->colors, "\\$$",
5625 TOG_COLOR_TREE_SUBMODULE,
5626 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5627 if (err)
5628 goto done;
5629 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5630 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5631 if (err)
5632 goto done;
5633 err = add_color(&s->colors, "/$",
5634 TOG_COLOR_TREE_DIRECTORY,
5635 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5636 if (err)
5637 goto done;
5639 err = add_color(&s->colors, "\\*$",
5640 TOG_COLOR_TREE_EXECUTABLE,
5641 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5642 if (err)
5643 goto done;
5645 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5646 get_color_value("TOG_COLOR_COMMIT"));
5647 if (err)
5648 goto done;
5651 view->show = show_tree_view;
5652 view->input = input_tree_view;
5653 view->close = close_tree_view;
5654 view->search_start = search_start_tree_view;
5655 view->search_next = search_next_tree_view;
5656 done:
5657 free(commit_id_str);
5658 if (commit)
5659 got_object_commit_close(commit);
5660 if (err)
5661 close_tree_view(view);
5662 return err;
5665 static const struct got_error *
5666 close_tree_view(struct tog_view *view)
5668 struct tog_tree_view_state *s = &view->state.tree;
5670 free_colors(&s->colors);
5671 free(s->tree_label);
5672 s->tree_label = NULL;
5673 free(s->commit_id);
5674 s->commit_id = NULL;
5675 free(s->head_ref_name);
5676 s->head_ref_name = NULL;
5677 while (!TAILQ_EMPTY(&s->parents)) {
5678 struct tog_parent_tree *parent;
5679 parent = TAILQ_FIRST(&s->parents);
5680 TAILQ_REMOVE(&s->parents, parent, entry);
5681 if (parent->tree != s->root)
5682 got_object_tree_close(parent->tree);
5683 free(parent);
5686 if (s->tree != NULL && s->tree != s->root)
5687 got_object_tree_close(s->tree);
5688 if (s->root)
5689 got_object_tree_close(s->root);
5690 return NULL;
5693 static const struct got_error *
5694 search_start_tree_view(struct tog_view *view)
5696 struct tog_tree_view_state *s = &view->state.tree;
5698 s->matched_entry = NULL;
5699 return NULL;
5702 static int
5703 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5705 regmatch_t regmatch;
5707 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5708 0) == 0;
5711 static const struct got_error *
5712 search_next_tree_view(struct tog_view *view)
5714 struct tog_tree_view_state *s = &view->state.tree;
5715 struct got_tree_entry *te = NULL;
5717 if (!view->searching) {
5718 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5719 return NULL;
5722 if (s->matched_entry) {
5723 if (view->searching == TOG_SEARCH_FORWARD) {
5724 if (s->selected_entry)
5725 te = got_tree_entry_get_next(s->tree,
5726 s->selected_entry);
5727 else
5728 te = got_object_tree_get_first_entry(s->tree);
5729 } else {
5730 if (s->selected_entry == NULL)
5731 te = got_object_tree_get_last_entry(s->tree);
5732 else
5733 te = got_tree_entry_get_prev(s->tree,
5734 s->selected_entry);
5736 } else {
5737 if (s->selected_entry)
5738 te = s->selected_entry;
5739 else if (view->searching == TOG_SEARCH_FORWARD)
5740 te = got_object_tree_get_first_entry(s->tree);
5741 else
5742 te = got_object_tree_get_last_entry(s->tree);
5745 while (1) {
5746 if (te == NULL) {
5747 if (s->matched_entry == NULL) {
5748 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5749 return NULL;
5751 if (view->searching == TOG_SEARCH_FORWARD)
5752 te = got_object_tree_get_first_entry(s->tree);
5753 else
5754 te = got_object_tree_get_last_entry(s->tree);
5757 if (match_tree_entry(te, &view->regex)) {
5758 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5759 s->matched_entry = te;
5760 break;
5763 if (view->searching == TOG_SEARCH_FORWARD)
5764 te = got_tree_entry_get_next(s->tree, te);
5765 else
5766 te = got_tree_entry_get_prev(s->tree, te);
5769 if (s->matched_entry) {
5770 s->first_displayed_entry = s->matched_entry;
5771 s->selected = 0;
5774 return NULL;
5777 static const struct got_error *
5778 show_tree_view(struct tog_view *view)
5780 const struct got_error *err = NULL;
5781 struct tog_tree_view_state *s = &view->state.tree;
5782 char *parent_path;
5784 err = tree_entry_path(&parent_path, &s->parents, NULL);
5785 if (err)
5786 return err;
5788 err = draw_tree_entries(view, parent_path);
5789 free(parent_path);
5791 view_vborder(view);
5792 return err;
5795 static const struct got_error *
5796 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5798 const struct got_error *err = NULL;
5799 struct tog_tree_view_state *s = &view->state.tree;
5800 struct tog_view *log_view, *ref_view;
5801 struct got_tree_entry *te;
5802 int begin_x = 0, n, nscroll = view->nlines - 3;
5804 switch (ch) {
5805 case 'i':
5806 s->show_ids = !s->show_ids;
5807 break;
5808 case 'l':
5809 if (!s->selected_entry)
5810 break;
5811 if (view_is_parent_view(view))
5812 begin_x = view_split_begin_x(view->begin_x);
5813 err = log_selected_tree_entry(&log_view, begin_x, s);
5814 view->focussed = 0;
5815 log_view->focussed = 1;
5816 if (view_is_parent_view(view)) {
5817 err = view_close_child(view);
5818 if (err)
5819 return err;
5820 view_set_child(view, log_view);
5821 view->focus_child = 1;
5822 } else
5823 *new_view = log_view;
5824 break;
5825 case 'r':
5826 if (view_is_parent_view(view))
5827 begin_x = view_split_begin_x(view->begin_x);
5828 ref_view = view_open(view->nlines, view->ncols,
5829 view->begin_y, begin_x, TOG_VIEW_REF);
5830 if (ref_view == NULL)
5831 return got_error_from_errno("view_open");
5832 err = open_ref_view(ref_view, s->repo);
5833 if (err) {
5834 view_close(ref_view);
5835 return err;
5837 view->focussed = 0;
5838 ref_view->focussed = 1;
5839 if (view_is_parent_view(view)) {
5840 err = view_close_child(view);
5841 if (err)
5842 return err;
5843 view_set_child(view, ref_view);
5844 view->focus_child = 1;
5845 } else
5846 *new_view = ref_view;
5847 break;
5848 case 'g':
5849 case KEY_HOME:
5850 s->selected = 0;
5851 if (s->tree == s->root)
5852 s->first_displayed_entry =
5853 got_object_tree_get_first_entry(s->tree);
5854 else
5855 s->first_displayed_entry = NULL;
5856 break;
5857 case 'G':
5858 case KEY_END:
5859 s->selected = 0;
5860 te = got_object_tree_get_last_entry(s->tree);
5861 for (n = 0; n < view->nlines - 3; n++) {
5862 if (te == NULL) {
5863 if(s->tree != s->root) {
5864 s->first_displayed_entry = NULL;
5865 n++;
5867 break;
5869 s->first_displayed_entry = te;
5870 te = got_tree_entry_get_prev(s->tree, te);
5872 if (n > 0)
5873 s->selected = n - 1;
5874 break;
5875 case 'k':
5876 case KEY_UP:
5877 case CTRL('p'):
5878 if (s->selected > 0) {
5879 s->selected--;
5880 break;
5882 tree_scroll_up(s, 1);
5883 break;
5884 case CTRL('u'):
5885 case 'u':
5886 nscroll /= 2;
5887 /* FALL THROUGH */
5888 case KEY_PPAGE:
5889 case CTRL('b'):
5890 if (s->tree == s->root) {
5891 if (got_object_tree_get_first_entry(s->tree) ==
5892 s->first_displayed_entry)
5893 s->selected -= MIN(s->selected, nscroll);
5894 } else {
5895 if (s->first_displayed_entry == NULL)
5896 s->selected -= MIN(s->selected, nscroll);
5898 tree_scroll_up(s, MAX(0, nscroll));
5899 break;
5900 case 'j':
5901 case KEY_DOWN:
5902 case CTRL('n'):
5903 if (s->selected < s->ndisplayed - 1) {
5904 s->selected++;
5905 break;
5907 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5908 == NULL)
5909 /* can't scroll any further */
5910 break;
5911 tree_scroll_down(s, 1);
5912 break;
5913 case CTRL('d'):
5914 case 'd':
5915 nscroll /= 2;
5916 /* FALL THROUGH */
5917 case KEY_NPAGE:
5918 case CTRL('f'):
5919 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5920 == NULL) {
5921 /* can't scroll any further; move cursor down */
5922 if (s->selected < s->ndisplayed - 1)
5923 s->selected += MIN(nscroll,
5924 s->ndisplayed - s->selected - 1);
5925 break;
5927 tree_scroll_down(s, nscroll);
5928 break;
5929 case KEY_ENTER:
5930 case '\r':
5931 case KEY_BACKSPACE:
5932 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5933 struct tog_parent_tree *parent;
5934 /* user selected '..' */
5935 if (s->tree == s->root)
5936 break;
5937 parent = TAILQ_FIRST(&s->parents);
5938 TAILQ_REMOVE(&s->parents, parent,
5939 entry);
5940 got_object_tree_close(s->tree);
5941 s->tree = parent->tree;
5942 s->first_displayed_entry =
5943 parent->first_displayed_entry;
5944 s->selected_entry =
5945 parent->selected_entry;
5946 s->selected = parent->selected;
5947 free(parent);
5948 } else if (S_ISDIR(got_tree_entry_get_mode(
5949 s->selected_entry))) {
5950 struct got_tree_object *subtree;
5951 err = got_object_open_as_tree(&subtree, s->repo,
5952 got_tree_entry_get_id(s->selected_entry));
5953 if (err)
5954 break;
5955 err = tree_view_visit_subtree(s, subtree);
5956 if (err) {
5957 got_object_tree_close(subtree);
5958 break;
5960 } else if (S_ISREG(got_tree_entry_get_mode(
5961 s->selected_entry))) {
5962 struct tog_view *blame_view;
5963 int begin_x = view_is_parent_view(view) ?
5964 view_split_begin_x(view->begin_x) : 0;
5966 err = blame_tree_entry(&blame_view, begin_x,
5967 s->selected_entry, &s->parents,
5968 s->commit_id, s->repo);
5969 if (err)
5970 break;
5971 view->focussed = 0;
5972 blame_view->focussed = 1;
5973 if (view_is_parent_view(view)) {
5974 err = view_close_child(view);
5975 if (err)
5976 return err;
5977 view_set_child(view, blame_view);
5978 view->focus_child = 1;
5979 } else
5980 *new_view = blame_view;
5982 break;
5983 case KEY_RESIZE:
5984 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5985 s->selected = view->nlines - 4;
5986 break;
5987 default:
5988 break;
5991 return err;
5994 __dead static void
5995 usage_tree(void)
5997 endwin();
5998 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5999 getprogname());
6000 exit(1);
6003 static const struct got_error *
6004 cmd_tree(int argc, char *argv[])
6006 const struct got_error *error;
6007 struct got_repository *repo = NULL;
6008 struct got_worktree *worktree = NULL;
6009 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6010 struct got_object_id *commit_id = NULL;
6011 struct got_commit_object *commit = NULL;
6012 const char *commit_id_arg = NULL;
6013 char *label = NULL;
6014 struct got_reference *ref = NULL;
6015 const char *head_ref_name = NULL;
6016 int ch;
6017 struct tog_view *view;
6018 int *pack_fds = NULL;
6020 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6021 switch (ch) {
6022 case 'c':
6023 commit_id_arg = optarg;
6024 break;
6025 case 'r':
6026 repo_path = realpath(optarg, NULL);
6027 if (repo_path == NULL)
6028 return got_error_from_errno2("realpath",
6029 optarg);
6030 break;
6031 default:
6032 usage_tree();
6033 /* NOTREACHED */
6037 argc -= optind;
6038 argv += optind;
6040 if (argc > 1)
6041 usage_tree();
6043 error = got_repo_pack_fds_open(&pack_fds);
6044 if (error != NULL)
6045 goto done;
6047 if (repo_path == NULL) {
6048 cwd = getcwd(NULL, 0);
6049 if (cwd == NULL)
6050 return got_error_from_errno("getcwd");
6051 error = got_worktree_open(&worktree, cwd);
6052 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6053 goto done;
6054 if (worktree)
6055 repo_path =
6056 strdup(got_worktree_get_repo_path(worktree));
6057 else
6058 repo_path = strdup(cwd);
6059 if (repo_path == NULL) {
6060 error = got_error_from_errno("strdup");
6061 goto done;
6065 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6066 if (error != NULL)
6067 goto done;
6069 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6070 repo, worktree);
6071 if (error)
6072 goto done;
6074 init_curses();
6076 error = apply_unveil(got_repo_get_path(repo), NULL);
6077 if (error)
6078 goto done;
6080 error = tog_load_refs(repo, 0);
6081 if (error)
6082 goto done;
6084 if (commit_id_arg == NULL) {
6085 error = got_repo_match_object_id(&commit_id, &label,
6086 worktree ? got_worktree_get_head_ref_name(worktree) :
6087 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6088 if (error)
6089 goto done;
6090 head_ref_name = label;
6091 } else {
6092 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6093 if (error == NULL)
6094 head_ref_name = got_ref_get_name(ref);
6095 else if (error->code != GOT_ERR_NOT_REF)
6096 goto done;
6097 error = got_repo_match_object_id(&commit_id, NULL,
6098 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6099 if (error)
6100 goto done;
6103 error = got_object_open_as_commit(&commit, repo, commit_id);
6104 if (error)
6105 goto done;
6107 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6108 if (view == NULL) {
6109 error = got_error_from_errno("view_open");
6110 goto done;
6112 error = open_tree_view(view, commit_id, head_ref_name, repo);
6113 if (error)
6114 goto done;
6115 if (!got_path_is_root_dir(in_repo_path)) {
6116 error = tree_view_walk_path(&view->state.tree, commit,
6117 in_repo_path);
6118 if (error)
6119 goto done;
6122 if (worktree) {
6123 /* Release work tree lock. */
6124 got_worktree_close(worktree);
6125 worktree = NULL;
6127 error = view_loop(view);
6128 done:
6129 free(repo_path);
6130 free(cwd);
6131 free(commit_id);
6132 free(label);
6133 if (ref)
6134 got_ref_close(ref);
6135 if (repo) {
6136 const struct got_error *close_err = got_repo_close(repo);
6137 if (error == NULL)
6138 error = close_err;
6140 if (pack_fds) {
6141 const struct got_error *pack_err =
6142 got_repo_pack_fds_close(pack_fds);
6143 if (error == NULL)
6144 error = pack_err;
6146 tog_free_refs();
6147 return error;
6150 static const struct got_error *
6151 ref_view_load_refs(struct tog_ref_view_state *s)
6153 struct got_reflist_entry *sre;
6154 struct tog_reflist_entry *re;
6156 s->nrefs = 0;
6157 TAILQ_FOREACH(sre, &tog_refs, entry) {
6158 if (strncmp(got_ref_get_name(sre->ref),
6159 "refs/got/", 9) == 0 &&
6160 strncmp(got_ref_get_name(sre->ref),
6161 "refs/got/backup/", 16) != 0)
6162 continue;
6164 re = malloc(sizeof(*re));
6165 if (re == NULL)
6166 return got_error_from_errno("malloc");
6168 re->ref = got_ref_dup(sre->ref);
6169 if (re->ref == NULL)
6170 return got_error_from_errno("got_ref_dup");
6171 re->idx = s->nrefs++;
6172 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6175 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6176 return NULL;
6179 void
6180 ref_view_free_refs(struct tog_ref_view_state *s)
6182 struct tog_reflist_entry *re;
6184 while (!TAILQ_EMPTY(&s->refs)) {
6185 re = TAILQ_FIRST(&s->refs);
6186 TAILQ_REMOVE(&s->refs, re, entry);
6187 got_ref_close(re->ref);
6188 free(re);
6192 static const struct got_error *
6193 open_ref_view(struct tog_view *view, struct got_repository *repo)
6195 const struct got_error *err = NULL;
6196 struct tog_ref_view_state *s = &view->state.ref;
6198 s->selected_entry = 0;
6199 s->repo = repo;
6201 TAILQ_INIT(&s->refs);
6202 STAILQ_INIT(&s->colors);
6204 err = ref_view_load_refs(s);
6205 if (err)
6206 return err;
6208 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6209 err = add_color(&s->colors, "^refs/heads/",
6210 TOG_COLOR_REFS_HEADS,
6211 get_color_value("TOG_COLOR_REFS_HEADS"));
6212 if (err)
6213 goto done;
6215 err = add_color(&s->colors, "^refs/tags/",
6216 TOG_COLOR_REFS_TAGS,
6217 get_color_value("TOG_COLOR_REFS_TAGS"));
6218 if (err)
6219 goto done;
6221 err = add_color(&s->colors, "^refs/remotes/",
6222 TOG_COLOR_REFS_REMOTES,
6223 get_color_value("TOG_COLOR_REFS_REMOTES"));
6224 if (err)
6225 goto done;
6227 err = add_color(&s->colors, "^refs/got/backup/",
6228 TOG_COLOR_REFS_BACKUP,
6229 get_color_value("TOG_COLOR_REFS_BACKUP"));
6230 if (err)
6231 goto done;
6234 view->show = show_ref_view;
6235 view->input = input_ref_view;
6236 view->close = close_ref_view;
6237 view->search_start = search_start_ref_view;
6238 view->search_next = search_next_ref_view;
6239 done:
6240 if (err)
6241 free_colors(&s->colors);
6242 return err;
6245 static const struct got_error *
6246 close_ref_view(struct tog_view *view)
6248 struct tog_ref_view_state *s = &view->state.ref;
6250 ref_view_free_refs(s);
6251 free_colors(&s->colors);
6253 return NULL;
6256 static const struct got_error *
6257 resolve_reflist_entry(struct got_object_id **commit_id,
6258 struct tog_reflist_entry *re, struct got_repository *repo)
6260 const struct got_error *err = NULL;
6261 struct got_object_id *obj_id;
6262 struct got_tag_object *tag = NULL;
6263 int obj_type;
6265 *commit_id = NULL;
6267 err = got_ref_resolve(&obj_id, repo, re->ref);
6268 if (err)
6269 return err;
6271 err = got_object_get_type(&obj_type, repo, obj_id);
6272 if (err)
6273 goto done;
6275 switch (obj_type) {
6276 case GOT_OBJ_TYPE_COMMIT:
6277 *commit_id = obj_id;
6278 break;
6279 case GOT_OBJ_TYPE_TAG:
6280 err = got_object_open_as_tag(&tag, repo, obj_id);
6281 if (err)
6282 goto done;
6283 free(obj_id);
6284 err = got_object_get_type(&obj_type, repo,
6285 got_object_tag_get_object_id(tag));
6286 if (err)
6287 goto done;
6288 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6289 err = got_error(GOT_ERR_OBJ_TYPE);
6290 goto done;
6292 *commit_id = got_object_id_dup(
6293 got_object_tag_get_object_id(tag));
6294 if (*commit_id == NULL) {
6295 err = got_error_from_errno("got_object_id_dup");
6296 goto done;
6298 break;
6299 default:
6300 err = got_error(GOT_ERR_OBJ_TYPE);
6301 break;
6304 done:
6305 if (tag)
6306 got_object_tag_close(tag);
6307 if (err) {
6308 free(*commit_id);
6309 *commit_id = NULL;
6311 return err;
6314 static const struct got_error *
6315 log_ref_entry(struct tog_view **new_view, int begin_x,
6316 struct tog_reflist_entry *re, struct got_repository *repo)
6318 struct tog_view *log_view;
6319 const struct got_error *err = NULL;
6320 struct got_object_id *commit_id = NULL;
6322 *new_view = NULL;
6324 err = resolve_reflist_entry(&commit_id, re, repo);
6325 if (err) {
6326 if (err->code != GOT_ERR_OBJ_TYPE)
6327 return err;
6328 else
6329 return NULL;
6332 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6333 if (log_view == NULL) {
6334 err = got_error_from_errno("view_open");
6335 goto done;
6338 err = open_log_view(log_view, commit_id, repo,
6339 got_ref_get_name(re->ref), "", 0);
6340 done:
6341 if (err)
6342 view_close(log_view);
6343 else
6344 *new_view = log_view;
6345 free(commit_id);
6346 return err;
6349 static void
6350 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6352 struct tog_reflist_entry *re;
6353 int i = 0;
6355 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6356 return;
6358 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6359 while (i++ < maxscroll) {
6360 if (re == NULL)
6361 break;
6362 s->first_displayed_entry = re;
6363 re = TAILQ_PREV(re, tog_reflist_head, entry);
6367 static void
6368 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6370 struct tog_reflist_entry *next, *last;
6371 int n = 0;
6373 if (s->first_displayed_entry)
6374 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6375 else
6376 next = TAILQ_FIRST(&s->refs);
6378 last = s->last_displayed_entry;
6379 while (next && last && n++ < maxscroll) {
6380 last = TAILQ_NEXT(last, entry);
6381 if (last) {
6382 s->first_displayed_entry = next;
6383 next = TAILQ_NEXT(next, entry);
6388 static const struct got_error *
6389 search_start_ref_view(struct tog_view *view)
6391 struct tog_ref_view_state *s = &view->state.ref;
6393 s->matched_entry = NULL;
6394 return NULL;
6397 static int
6398 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6400 regmatch_t regmatch;
6402 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6403 0) == 0;
6406 static const struct got_error *
6407 search_next_ref_view(struct tog_view *view)
6409 struct tog_ref_view_state *s = &view->state.ref;
6410 struct tog_reflist_entry *re = NULL;
6412 if (!view->searching) {
6413 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6414 return NULL;
6417 if (s->matched_entry) {
6418 if (view->searching == TOG_SEARCH_FORWARD) {
6419 if (s->selected_entry)
6420 re = TAILQ_NEXT(s->selected_entry, entry);
6421 else
6422 re = TAILQ_PREV(s->selected_entry,
6423 tog_reflist_head, entry);
6424 } else {
6425 if (s->selected_entry == NULL)
6426 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6427 else
6428 re = TAILQ_PREV(s->selected_entry,
6429 tog_reflist_head, entry);
6431 } else {
6432 if (s->selected_entry)
6433 re = s->selected_entry;
6434 else if (view->searching == TOG_SEARCH_FORWARD)
6435 re = TAILQ_FIRST(&s->refs);
6436 else
6437 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6440 while (1) {
6441 if (re == NULL) {
6442 if (s->matched_entry == NULL) {
6443 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6444 return NULL;
6446 if (view->searching == TOG_SEARCH_FORWARD)
6447 re = TAILQ_FIRST(&s->refs);
6448 else
6449 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6452 if (match_reflist_entry(re, &view->regex)) {
6453 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6454 s->matched_entry = re;
6455 break;
6458 if (view->searching == TOG_SEARCH_FORWARD)
6459 re = TAILQ_NEXT(re, entry);
6460 else
6461 re = TAILQ_PREV(re, tog_reflist_head, entry);
6464 if (s->matched_entry) {
6465 s->first_displayed_entry = s->matched_entry;
6466 s->selected = 0;
6469 return NULL;
6472 static const struct got_error *
6473 show_ref_view(struct tog_view *view)
6475 const struct got_error *err = NULL;
6476 struct tog_ref_view_state *s = &view->state.ref;
6477 struct tog_reflist_entry *re;
6478 char *line = NULL;
6479 wchar_t *wline;
6480 struct tog_color *tc;
6481 int width, n;
6482 int limit = view->nlines;
6484 werase(view->window);
6486 s->ndisplayed = 0;
6488 if (limit == 0)
6489 return NULL;
6491 re = s->first_displayed_entry;
6493 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6494 s->nrefs) == -1)
6495 return got_error_from_errno("asprintf");
6497 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6498 if (err) {
6499 free(line);
6500 return err;
6502 if (view_needs_focus_indication(view))
6503 wstandout(view->window);
6504 waddwstr(view->window, wline);
6505 if (view_needs_focus_indication(view))
6506 wstandend(view->window);
6507 free(wline);
6508 wline = NULL;
6509 free(line);
6510 line = NULL;
6511 if (width < view->ncols - 1)
6512 waddch(view->window, '\n');
6513 if (--limit <= 0)
6514 return NULL;
6516 n = 0;
6517 while (re && limit > 0) {
6518 char *line = NULL;
6519 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6521 if (s->show_date) {
6522 struct got_commit_object *ci;
6523 struct got_tag_object *tag;
6524 struct got_object_id *id;
6525 struct tm tm;
6526 time_t t;
6528 err = got_ref_resolve(&id, s->repo, re->ref);
6529 if (err)
6530 return err;
6531 err = got_object_open_as_tag(&tag, s->repo, id);
6532 if (err) {
6533 if (err->code != GOT_ERR_OBJ_TYPE) {
6534 free(id);
6535 return err;
6537 err = got_object_open_as_commit(&ci, s->repo,
6538 id);
6539 if (err) {
6540 free(id);
6541 return err;
6543 t = got_object_commit_get_committer_time(ci);
6544 got_object_commit_close(ci);
6545 } else {
6546 t = got_object_tag_get_tagger_time(tag);
6547 got_object_tag_close(tag);
6549 free(id);
6550 if (gmtime_r(&t, &tm) == NULL)
6551 return got_error_from_errno("gmtime_r");
6552 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6553 return got_error(GOT_ERR_NO_SPACE);
6555 if (got_ref_is_symbolic(re->ref)) {
6556 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6557 ymd : "", got_ref_get_name(re->ref),
6558 got_ref_get_symref_target(re->ref)) == -1)
6559 return got_error_from_errno("asprintf");
6560 } else if (s->show_ids) {
6561 struct got_object_id *id;
6562 char *id_str;
6563 err = got_ref_resolve(&id, s->repo, re->ref);
6564 if (err)
6565 return err;
6566 err = got_object_id_str(&id_str, id);
6567 if (err) {
6568 free(id);
6569 return err;
6571 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6572 got_ref_get_name(re->ref), id_str) == -1) {
6573 err = got_error_from_errno("asprintf");
6574 free(id);
6575 free(id_str);
6576 return err;
6578 free(id);
6579 free(id_str);
6580 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6581 got_ref_get_name(re->ref)) == -1)
6582 return got_error_from_errno("asprintf");
6584 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6585 0, 0);
6586 if (err) {
6587 free(line);
6588 return err;
6590 if (n == s->selected) {
6591 if (view->focussed)
6592 wstandout(view->window);
6593 s->selected_entry = re;
6595 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6596 if (tc)
6597 wattr_on(view->window,
6598 COLOR_PAIR(tc->colorpair), NULL);
6599 waddwstr(view->window, wline);
6600 if (tc)
6601 wattr_off(view->window,
6602 COLOR_PAIR(tc->colorpair), NULL);
6603 if (width < view->ncols - 1)
6604 waddch(view->window, '\n');
6605 if (n == s->selected && view->focussed)
6606 wstandend(view->window);
6607 free(line);
6608 free(wline);
6609 wline = NULL;
6610 n++;
6611 s->ndisplayed++;
6612 s->last_displayed_entry = re;
6614 limit--;
6615 re = TAILQ_NEXT(re, entry);
6618 view_vborder(view);
6619 return err;
6622 static const struct got_error *
6623 browse_ref_tree(struct tog_view **new_view, int begin_x,
6624 struct tog_reflist_entry *re, struct got_repository *repo)
6626 const struct got_error *err = NULL;
6627 struct got_object_id *commit_id = NULL;
6628 struct tog_view *tree_view;
6630 *new_view = NULL;
6632 err = resolve_reflist_entry(&commit_id, re, repo);
6633 if (err) {
6634 if (err->code != GOT_ERR_OBJ_TYPE)
6635 return err;
6636 else
6637 return NULL;
6641 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6642 if (tree_view == NULL) {
6643 err = got_error_from_errno("view_open");
6644 goto done;
6647 err = open_tree_view(tree_view, commit_id,
6648 got_ref_get_name(re->ref), repo);
6649 if (err)
6650 goto done;
6652 *new_view = tree_view;
6653 done:
6654 free(commit_id);
6655 return err;
6657 static const struct got_error *
6658 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6660 const struct got_error *err = NULL;
6661 struct tog_ref_view_state *s = &view->state.ref;
6662 struct tog_view *log_view, *tree_view;
6663 struct tog_reflist_entry *re;
6664 int begin_x = 0, n, nscroll = view->nlines - 1;
6666 switch (ch) {
6667 case 'i':
6668 s->show_ids = !s->show_ids;
6669 break;
6670 case 'm':
6671 s->show_date = !s->show_date;
6672 break;
6673 case 'o':
6674 s->sort_by_date = !s->sort_by_date;
6675 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6676 got_ref_cmp_by_commit_timestamp_descending :
6677 tog_ref_cmp_by_name, s->repo);
6678 if (err)
6679 break;
6680 got_reflist_object_id_map_free(tog_refs_idmap);
6681 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6682 &tog_refs, s->repo);
6683 if (err)
6684 break;
6685 ref_view_free_refs(s);
6686 err = ref_view_load_refs(s);
6687 break;
6688 case KEY_ENTER:
6689 case '\r':
6690 if (!s->selected_entry)
6691 break;
6692 if (view_is_parent_view(view))
6693 begin_x = view_split_begin_x(view->begin_x);
6694 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6695 s->repo);
6696 view->focussed = 0;
6697 log_view->focussed = 1;
6698 if (view_is_parent_view(view)) {
6699 err = view_close_child(view);
6700 if (err)
6701 return err;
6702 view_set_child(view, log_view);
6703 view->focus_child = 1;
6704 } else
6705 *new_view = log_view;
6706 break;
6707 case 't':
6708 if (!s->selected_entry)
6709 break;
6710 if (view_is_parent_view(view))
6711 begin_x = view_split_begin_x(view->begin_x);
6712 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6713 s->repo);
6714 if (err || tree_view == NULL)
6715 break;
6716 view->focussed = 0;
6717 tree_view->focussed = 1;
6718 if (view_is_parent_view(view)) {
6719 err = view_close_child(view);
6720 if (err)
6721 return err;
6722 view_set_child(view, tree_view);
6723 view->focus_child = 1;
6724 } else
6725 *new_view = tree_view;
6726 break;
6727 case 'g':
6728 case KEY_HOME:
6729 s->selected = 0;
6730 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6731 break;
6732 case 'G':
6733 case KEY_END:
6734 s->selected = 0;
6735 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6736 for (n = 0; n < view->nlines - 1; n++) {
6737 if (re == NULL)
6738 break;
6739 s->first_displayed_entry = re;
6740 re = TAILQ_PREV(re, tog_reflist_head, entry);
6742 if (n > 0)
6743 s->selected = n - 1;
6744 break;
6745 case 'k':
6746 case KEY_UP:
6747 case CTRL('p'):
6748 if (s->selected > 0) {
6749 s->selected--;
6750 break;
6752 ref_scroll_up(s, 1);
6753 break;
6754 case CTRL('u'):
6755 case 'u':
6756 nscroll /= 2;
6757 /* FALL THROUGH */
6758 case KEY_PPAGE:
6759 case CTRL('b'):
6760 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6761 s->selected -= MIN(nscroll, s->selected);
6762 ref_scroll_up(s, MAX(0, nscroll));
6763 break;
6764 case 'j':
6765 case KEY_DOWN:
6766 case CTRL('n'):
6767 if (s->selected < s->ndisplayed - 1) {
6768 s->selected++;
6769 break;
6771 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6772 /* can't scroll any further */
6773 break;
6774 ref_scroll_down(s, 1);
6775 break;
6776 case CTRL('d'):
6777 case 'd':
6778 nscroll /= 2;
6779 /* FALL THROUGH */
6780 case KEY_NPAGE:
6781 case CTRL('f'):
6782 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6783 /* can't scroll any further; move cursor down */
6784 if (s->selected < s->ndisplayed - 1)
6785 s->selected += MIN(nscroll,
6786 s->ndisplayed - s->selected - 1);
6787 break;
6789 ref_scroll_down(s, nscroll);
6790 break;
6791 case CTRL('l'):
6792 tog_free_refs();
6793 err = tog_load_refs(s->repo, s->sort_by_date);
6794 if (err)
6795 break;
6796 ref_view_free_refs(s);
6797 err = ref_view_load_refs(s);
6798 break;
6799 case KEY_RESIZE:
6800 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6801 s->selected = view->nlines - 2;
6802 break;
6803 default:
6804 break;
6807 return err;
6810 __dead static void
6811 usage_ref(void)
6813 endwin();
6814 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6815 getprogname());
6816 exit(1);
6819 static const struct got_error *
6820 cmd_ref(int argc, char *argv[])
6822 const struct got_error *error;
6823 struct got_repository *repo = NULL;
6824 struct got_worktree *worktree = NULL;
6825 char *cwd = NULL, *repo_path = NULL;
6826 int ch;
6827 struct tog_view *view;
6828 int *pack_fds = NULL;
6830 while ((ch = getopt(argc, argv, "r:")) != -1) {
6831 switch (ch) {
6832 case 'r':
6833 repo_path = realpath(optarg, NULL);
6834 if (repo_path == NULL)
6835 return got_error_from_errno2("realpath",
6836 optarg);
6837 break;
6838 default:
6839 usage_ref();
6840 /* NOTREACHED */
6844 argc -= optind;
6845 argv += optind;
6847 if (argc > 1)
6848 usage_ref();
6850 error = got_repo_pack_fds_open(&pack_fds);
6851 if (error != NULL)
6852 goto done;
6854 if (repo_path == NULL) {
6855 cwd = getcwd(NULL, 0);
6856 if (cwd == NULL)
6857 return got_error_from_errno("getcwd");
6858 error = got_worktree_open(&worktree, cwd);
6859 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6860 goto done;
6861 if (worktree)
6862 repo_path =
6863 strdup(got_worktree_get_repo_path(worktree));
6864 else
6865 repo_path = strdup(cwd);
6866 if (repo_path == NULL) {
6867 error = got_error_from_errno("strdup");
6868 goto done;
6872 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6873 if (error != NULL)
6874 goto done;
6876 init_curses();
6878 error = apply_unveil(got_repo_get_path(repo), NULL);
6879 if (error)
6880 goto done;
6882 error = tog_load_refs(repo, 0);
6883 if (error)
6884 goto done;
6886 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6887 if (view == NULL) {
6888 error = got_error_from_errno("view_open");
6889 goto done;
6892 error = open_ref_view(view, repo);
6893 if (error)
6894 goto done;
6896 if (worktree) {
6897 /* Release work tree lock. */
6898 got_worktree_close(worktree);
6899 worktree = NULL;
6901 error = view_loop(view);
6902 done:
6903 free(repo_path);
6904 free(cwd);
6905 if (repo) {
6906 const struct got_error *close_err = got_repo_close(repo);
6907 if (close_err)
6908 error = close_err;
6910 if (pack_fds) {
6911 const struct got_error *pack_err =
6912 got_repo_pack_fds_close(pack_fds);
6913 if (error == NULL)
6914 error = pack_err;
6916 tog_free_refs();
6917 return error;
6920 static void
6921 list_commands(FILE *fp)
6923 size_t i;
6925 fprintf(fp, "commands:");
6926 for (i = 0; i < nitems(tog_commands); i++) {
6927 const struct tog_cmd *cmd = &tog_commands[i];
6928 fprintf(fp, " %s", cmd->name);
6930 fputc('\n', fp);
6933 __dead static void
6934 usage(int hflag, int status)
6936 FILE *fp = (status == 0) ? stdout : stderr;
6938 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6939 getprogname());
6940 if (hflag) {
6941 fprintf(fp, "lazy usage: %s path\n", getprogname());
6942 list_commands(fp);
6944 exit(status);
6947 static char **
6948 make_argv(int argc, ...)
6950 va_list ap;
6951 char **argv;
6952 int i;
6954 va_start(ap, argc);
6956 argv = calloc(argc, sizeof(char *));
6957 if (argv == NULL)
6958 err(1, "calloc");
6959 for (i = 0; i < argc; i++) {
6960 argv[i] = strdup(va_arg(ap, char *));
6961 if (argv[i] == NULL)
6962 err(1, "strdup");
6965 va_end(ap);
6966 return argv;
6970 * Try to convert 'tog path' into a 'tog log path' command.
6971 * The user could simply have mistyped the command rather than knowingly
6972 * provided a path. So check whether argv[0] can in fact be resolved
6973 * to a path in the HEAD commit and print a special error if not.
6974 * This hack is for mpi@ <3
6976 static const struct got_error *
6977 tog_log_with_path(int argc, char *argv[])
6979 const struct got_error *error = NULL, *close_err;
6980 const struct tog_cmd *cmd = NULL;
6981 struct got_repository *repo = NULL;
6982 struct got_worktree *worktree = NULL;
6983 struct got_object_id *commit_id = NULL, *id = NULL;
6984 struct got_commit_object *commit = NULL;
6985 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6986 char *commit_id_str = NULL, **cmd_argv = NULL;
6987 int *pack_fds = NULL;
6989 cwd = getcwd(NULL, 0);
6990 if (cwd == NULL)
6991 return got_error_from_errno("getcwd");
6993 error = got_repo_pack_fds_open(&pack_fds);
6994 if (error != NULL)
6995 goto done;
6997 error = got_worktree_open(&worktree, cwd);
6998 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6999 goto done;
7001 if (worktree)
7002 repo_path = strdup(got_worktree_get_repo_path(worktree));
7003 else
7004 repo_path = strdup(cwd);
7005 if (repo_path == NULL) {
7006 error = got_error_from_errno("strdup");
7007 goto done;
7010 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7011 if (error != NULL)
7012 goto done;
7014 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7015 repo, worktree);
7016 if (error)
7017 goto done;
7019 error = tog_load_refs(repo, 0);
7020 if (error)
7021 goto done;
7022 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7023 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7024 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7025 if (error)
7026 goto done;
7028 if (worktree) {
7029 got_worktree_close(worktree);
7030 worktree = NULL;
7033 error = got_object_open_as_commit(&commit, repo, commit_id);
7034 if (error)
7035 goto done;
7037 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7038 if (error) {
7039 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7040 goto done;
7041 fprintf(stderr, "%s: '%s' is no known command or path\n",
7042 getprogname(), argv[0]);
7043 usage(1, 1);
7044 /* not reached */
7047 close_err = got_repo_close(repo);
7048 if (error == NULL)
7049 error = close_err;
7050 repo = NULL;
7052 error = got_object_id_str(&commit_id_str, commit_id);
7053 if (error)
7054 goto done;
7056 cmd = &tog_commands[0]; /* log */
7057 argc = 4;
7058 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7059 error = cmd->cmd_main(argc, cmd_argv);
7060 done:
7061 if (repo) {
7062 close_err = got_repo_close(repo);
7063 if (error == NULL)
7064 error = close_err;
7066 if (commit)
7067 got_object_commit_close(commit);
7068 if (worktree)
7069 got_worktree_close(worktree);
7070 if (pack_fds) {
7071 const struct got_error *pack_err =
7072 got_repo_pack_fds_close(pack_fds);
7073 if (error == NULL)
7074 error = pack_err;
7076 free(id);
7077 free(commit_id_str);
7078 free(commit_id);
7079 free(cwd);
7080 free(repo_path);
7081 free(in_repo_path);
7082 if (cmd_argv) {
7083 int i;
7084 for (i = 0; i < argc; i++)
7085 free(cmd_argv[i]);
7086 free(cmd_argv);
7088 tog_free_refs();
7089 return error;
7092 int
7093 main(int argc, char *argv[])
7095 const struct got_error *error = NULL;
7096 const struct tog_cmd *cmd = NULL;
7097 int ch, hflag = 0, Vflag = 0;
7098 char **cmd_argv = NULL;
7099 static const struct option longopts[] = {
7100 { "version", no_argument, NULL, 'V' },
7101 { NULL, 0, NULL, 0}
7104 setlocale(LC_CTYPE, "");
7106 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7107 switch (ch) {
7108 case 'h':
7109 hflag = 1;
7110 break;
7111 case 'V':
7112 Vflag = 1;
7113 break;
7114 default:
7115 usage(hflag, 1);
7116 /* NOTREACHED */
7120 argc -= optind;
7121 argv += optind;
7122 optind = 1;
7123 optreset = 1;
7125 if (Vflag) {
7126 got_version_print_str();
7127 return 0;
7130 #ifndef PROFILE
7131 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7132 NULL) == -1)
7133 err(1, "pledge");
7134 #endif
7136 if (argc == 0) {
7137 if (hflag)
7138 usage(hflag, 0);
7139 /* Build an argument vector which runs a default command. */
7140 cmd = &tog_commands[0];
7141 argc = 1;
7142 cmd_argv = make_argv(argc, cmd->name);
7143 } else {
7144 size_t i;
7146 /* Did the user specify a command? */
7147 for (i = 0; i < nitems(tog_commands); i++) {
7148 if (strncmp(tog_commands[i].name, argv[0],
7149 strlen(argv[0])) == 0) {
7150 cmd = &tog_commands[i];
7151 break;
7156 if (cmd == NULL) {
7157 if (argc != 1)
7158 usage(0, 1);
7159 /* No command specified; try log with a path */
7160 error = tog_log_with_path(argc, argv);
7161 } else {
7162 if (hflag)
7163 cmd->cmd_usage();
7164 else
7165 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7168 endwin();
7169 putchar('\n');
7170 if (cmd_argv) {
7171 int i;
7172 for (i = 0; i < argc; i++)
7173 free(cmd_argv[i]);
7174 free(cmd_argv);
7177 if (error && error->code != GOT_ERR_CANCELLED)
7178 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7179 return 0;