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/stat.h>
18 #include <sys/ioctl.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #if defined(__FreeBSD__) || defined(__APPLE__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #endif
25 #include <curses.h>
26 #include <panel.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <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_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
51 #include "got_diff.h"
52 #include "got_opentemp.h"
53 #include "got_utf8.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_path.h"
59 #include "got_worktree.h"
61 //#define update_panels() (0)
62 //#define doupdate() (0)
64 #ifndef MIN
65 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #endif
68 #ifndef MAX
69 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 #endif
72 #ifndef CTRL
73 #define CTRL(x) ((x) & 0x1f)
74 #endif
76 #ifndef nitems
77 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
78 #endif
80 struct tog_cmd {
81 const char *name;
82 const struct got_error *(*cmd_main)(int, char *[]);
83 void (*cmd_usage)(void);
84 };
86 __dead static void usage(int, int);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_ref(void);
93 static const struct got_error* cmd_log(int, char *[]);
94 static const struct got_error* cmd_diff(int, char *[]);
95 static const struct got_error* cmd_blame(int, char *[]);
96 static const struct got_error* cmd_tree(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
99 static const struct tog_cmd tog_commands[] = {
100 { "log", cmd_log, usage_log },
101 { "diff", cmd_diff, usage_diff },
102 { "blame", cmd_blame, usage_blame },
103 { "tree", cmd_tree, usage_tree },
104 { "ref", cmd_ref, usage_ref },
105 };
107 enum tog_view_type {
108 TOG_VIEW_DIFF,
109 TOG_VIEW_LOG,
110 TOG_VIEW_BLAME,
111 TOG_VIEW_TREE,
112 TOG_VIEW_REF,
113 };
115 #define TOG_EOF_STRING "(END)"
117 struct commit_queue_entry {
118 TAILQ_ENTRY(commit_queue_entry) entry;
119 struct got_object_id *id;
120 struct got_commit_object *commit;
121 int idx;
122 };
123 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 struct commit_queue {
125 int ncommits;
126 struct commit_queue_head head;
127 };
129 struct tog_color {
130 STAILQ_ENTRY(tog_color) entry;
131 regex_t regex;
132 short colorpair;
133 };
134 STAILQ_HEAD(tog_colors, tog_color);
136 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static const struct got_error *
140 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
141 struct got_reference* re2)
143 const char *name1 = got_ref_get_name(re1);
144 const char *name2 = got_ref_get_name(re2);
145 int isbackup1, isbackup2;
147 /* Sort backup refs towards the bottom of the list. */
148 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
149 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
150 if (!isbackup1 && isbackup2) {
151 *cmp = -1;
152 return NULL;
153 } else if (isbackup1 && !isbackup2) {
154 *cmp = 1;
155 return NULL;
158 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
159 return NULL;
162 static const struct got_error *
163 tog_load_refs(struct got_repository *repo, int sort_by_date)
165 const struct got_error *err;
167 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
168 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
169 repo);
170 if (err)
171 return err;
173 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
174 repo);
177 static void
178 tog_free_refs(void)
180 if (tog_refs_idmap) {
181 got_reflist_object_id_map_free(tog_refs_idmap);
182 tog_refs_idmap = NULL;
184 got_ref_list_free(&tog_refs);
187 static const struct got_error *
188 add_color(struct tog_colors *colors, const char *pattern,
189 int idx, short color)
191 const struct got_error *err = NULL;
192 struct tog_color *tc;
193 int regerr = 0;
195 if (idx < 1 || idx > COLOR_PAIRS - 1)
196 return NULL;
198 init_pair(idx, color, -1);
200 tc = calloc(1, sizeof(*tc));
201 if (tc == NULL)
202 return got_error_from_errno("calloc");
203 regerr = regcomp(&tc->regex, pattern,
204 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
205 if (regerr) {
206 static char regerr_msg[512];
207 static char err_msg[512];
208 regerror(regerr, &tc->regex, regerr_msg,
209 sizeof(regerr_msg));
210 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
211 regerr_msg);
212 err = got_error_msg(GOT_ERR_REGEX, err_msg);
213 free(tc);
214 return err;
216 tc->colorpair = idx;
217 STAILQ_INSERT_HEAD(colors, tc, entry);
218 return NULL;
221 static void
222 free_colors(struct tog_colors *colors)
224 struct tog_color *tc;
226 while (!STAILQ_EMPTY(colors)) {
227 tc = STAILQ_FIRST(colors);
228 STAILQ_REMOVE_HEAD(colors, entry);
229 regfree(&tc->regex);
230 free(tc);
234 struct tog_color *
235 get_color(struct tog_colors *colors, int colorpair)
237 struct tog_color *tc = NULL;
239 STAILQ_FOREACH(tc, colors, entry) {
240 if (tc->colorpair == colorpair)
241 return tc;
244 return NULL;
247 static int
248 default_color_value(const char *envvar)
250 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
251 return COLOR_MAGENTA;
252 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
253 return COLOR_CYAN;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
255 return COLOR_YELLOW;
256 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
257 return COLOR_GREEN;
258 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
259 return COLOR_MAGENTA;
260 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
263 return COLOR_CYAN;
264 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
265 return COLOR_GREEN;
266 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
267 return COLOR_GREEN;
268 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
269 return COLOR_CYAN;
270 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
271 return COLOR_YELLOW;
272 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
273 return COLOR_GREEN;
274 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
275 return COLOR_MAGENTA;
276 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
277 return COLOR_YELLOW;
278 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
279 return COLOR_CYAN;
281 return -1;
284 static int
285 get_color_value(const char *envvar)
287 const char *val = getenv(envvar);
289 if (val == NULL)
290 return default_color_value(envvar);
292 if (strcasecmp(val, "black") == 0)
293 return COLOR_BLACK;
294 if (strcasecmp(val, "red") == 0)
295 return COLOR_RED;
296 if (strcasecmp(val, "green") == 0)
297 return COLOR_GREEN;
298 if (strcasecmp(val, "yellow") == 0)
299 return COLOR_YELLOW;
300 if (strcasecmp(val, "blue") == 0)
301 return COLOR_BLUE;
302 if (strcasecmp(val, "magenta") == 0)
303 return COLOR_MAGENTA;
304 if (strcasecmp(val, "cyan") == 0)
305 return COLOR_CYAN;
306 if (strcasecmp(val, "white") == 0)
307 return COLOR_WHITE;
308 if (strcasecmp(val, "default") == 0)
309 return -1;
311 return default_color_value(envvar);
315 struct tog_diff_view_state {
316 struct got_object_id *id1, *id2;
317 const char *label1, *label2;
318 FILE *f, *f1, *f2;
319 int first_displayed_line;
320 int last_displayed_line;
321 int eof;
322 int diff_context;
323 int ignore_whitespace;
324 int force_text_diff;
325 struct got_repository *repo;
326 struct tog_colors colors;
327 size_t nlines;
328 off_t *line_offsets;
329 int matched_line;
330 int selected_line;
332 /* passed from log view; may be NULL */
333 struct tog_view *log_view;
334 };
336 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
338 struct tog_log_thread_args {
339 pthread_cond_t need_commits;
340 pthread_cond_t commit_loaded;
341 int commits_needed;
342 int load_all;
343 struct got_commit_graph *graph;
344 struct commit_queue *commits;
345 const char *in_repo_path;
346 struct got_object_id *start_id;
347 struct got_repository *repo;
348 int *pack_fds;
349 int log_complete;
350 sig_atomic_t *quit;
351 struct commit_queue_entry **first_displayed_entry;
352 struct commit_queue_entry **selected_entry;
353 int *searching;
354 int *search_next_done;
355 regex_t *regex;
356 };
358 struct tog_log_view_state {
359 struct commit_queue commits;
360 struct commit_queue_entry *first_displayed_entry;
361 struct commit_queue_entry *last_displayed_entry;
362 struct commit_queue_entry *selected_entry;
363 int selected;
364 char *in_repo_path;
365 char *head_ref_name;
366 int log_branches;
367 struct got_repository *repo;
368 struct got_object_id *start_id;
369 sig_atomic_t quit;
370 pthread_t thread;
371 struct tog_log_thread_args thread_args;
372 struct commit_queue_entry *matched_entry;
373 struct commit_queue_entry *search_entry;
374 struct tog_colors colors;
375 };
377 #define TOG_COLOR_DIFF_MINUS 1
378 #define TOG_COLOR_DIFF_PLUS 2
379 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
380 #define TOG_COLOR_DIFF_META 4
381 #define TOG_COLOR_TREE_SUBMODULE 5
382 #define TOG_COLOR_TREE_SYMLINK 6
383 #define TOG_COLOR_TREE_DIRECTORY 7
384 #define TOG_COLOR_TREE_EXECUTABLE 8
385 #define TOG_COLOR_COMMIT 9
386 #define TOG_COLOR_AUTHOR 10
387 #define TOG_COLOR_DATE 11
388 #define TOG_COLOR_REFS_HEADS 12
389 #define TOG_COLOR_REFS_TAGS 13
390 #define TOG_COLOR_REFS_REMOTES 14
391 #define TOG_COLOR_REFS_BACKUP 15
393 struct tog_blame_cb_args {
394 struct tog_blame_line *lines; /* one per line */
395 int nlines;
397 struct tog_view *view;
398 struct got_object_id *commit_id;
399 int *quit;
400 };
402 struct tog_blame_thread_args {
403 const char *path;
404 struct got_repository *repo;
405 struct tog_blame_cb_args *cb_args;
406 int *complete;
407 got_cancel_cb cancel_cb;
408 void *cancel_arg;
409 };
411 struct tog_blame {
412 FILE *f;
413 off_t filesize;
414 struct tog_blame_line *lines;
415 int nlines;
416 off_t *line_offsets;
417 pthread_t thread;
418 struct tog_blame_thread_args thread_args;
419 struct tog_blame_cb_args cb_args;
420 const char *path;
421 int *pack_fds;
422 };
424 struct tog_blame_view_state {
425 int first_displayed_line;
426 int last_displayed_line;
427 int selected_line;
428 int blame_complete;
429 int eof;
430 int done;
431 struct got_object_id_queue blamed_commits;
432 struct got_object_qid *blamed_commit;
433 char *path;
434 struct got_repository *repo;
435 struct got_object_id *commit_id;
436 struct tog_blame blame;
437 int matched_line;
438 struct tog_colors colors;
439 };
441 struct tog_parent_tree {
442 TAILQ_ENTRY(tog_parent_tree) entry;
443 struct got_tree_object *tree;
444 struct got_tree_entry *first_displayed_entry;
445 struct got_tree_entry *selected_entry;
446 int selected;
447 };
449 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
451 struct tog_tree_view_state {
452 char *tree_label;
453 struct got_object_id *commit_id;/* commit which this tree belongs to */
454 struct got_tree_object *root; /* the commit's root tree entry */
455 struct got_tree_object *tree; /* currently displayed (sub-)tree */
456 struct got_tree_entry *first_displayed_entry;
457 struct got_tree_entry *last_displayed_entry;
458 struct got_tree_entry *selected_entry;
459 int ndisplayed, selected, show_ids;
460 struct tog_parent_trees parents; /* parent trees of current sub-tree */
461 char *head_ref_name;
462 struct got_repository *repo;
463 struct got_tree_entry *matched_entry;
464 struct tog_colors colors;
465 };
467 struct tog_reflist_entry {
468 TAILQ_ENTRY(tog_reflist_entry) entry;
469 struct got_reference *ref;
470 int idx;
471 };
473 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
475 struct tog_ref_view_state {
476 struct tog_reflist_head refs;
477 struct tog_reflist_entry *first_displayed_entry;
478 struct tog_reflist_entry *last_displayed_entry;
479 struct tog_reflist_entry *selected_entry;
480 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
481 struct got_repository *repo;
482 struct tog_reflist_entry *matched_entry;
483 struct tog_colors colors;
484 };
486 /*
487 * We implement two types of views: parent views and child views.
489 * The 'Tab' key switches focus between a parent view and its child view.
490 * Child views are shown side-by-side to their parent view, provided
491 * there is enough screen estate.
493 * When a new view is opened from within a parent view, this new view
494 * becomes a child view of the parent view, replacing any existing child.
496 * When a new view is opened from within a child view, this new view
497 * becomes a parent view which will obscure the views below until the
498 * user quits the new parent view by typing 'q'.
500 * This list of views contains parent views only.
501 * Child views are only pointed to by their parent view.
502 */
503 TAILQ_HEAD(tog_view_list_head, tog_view);
505 struct tog_view {
506 TAILQ_ENTRY(tog_view) entry;
507 WINDOW *window;
508 PANEL *panel;
509 int nlines, ncols, begin_y, begin_x;
510 int maxx, x; /* max column and current start column */
511 int lines, cols; /* copies of LINES and COLS */
512 int focussed; /* Only set on one parent or child view at a time. */
513 int dying;
514 struct tog_view *parent;
515 struct tog_view *child;
517 /*
518 * This flag is initially set on parent views when a new child view
519 * is created. It gets toggled when the 'Tab' key switches focus
520 * between parent and child.
521 * The flag indicates whether focus should be passed on to our child
522 * view if this parent view gets picked for focus after another parent
523 * view was closed. This prevents child views from losing focus in such
524 * situations.
525 */
526 int focus_child;
528 /* type-specific state */
529 enum tog_view_type type;
530 union {
531 struct tog_diff_view_state diff;
532 struct tog_log_view_state log;
533 struct tog_blame_view_state blame;
534 struct tog_tree_view_state tree;
535 struct tog_ref_view_state ref;
536 } state;
538 const struct got_error *(*show)(struct tog_view *);
539 const struct got_error *(*input)(struct tog_view **,
540 struct tog_view *, int);
541 const struct got_error *(*close)(struct tog_view *);
543 const struct got_error *(*search_start)(struct tog_view *);
544 const struct got_error *(*search_next)(struct tog_view *);
545 int search_started;
546 int searching;
547 #define TOG_SEARCH_FORWARD 1
548 #define TOG_SEARCH_BACKWARD 2
549 int search_next_done;
550 #define TOG_SEARCH_HAVE_MORE 1
551 #define TOG_SEARCH_NO_MORE 2
552 #define TOG_SEARCH_HAVE_NONE 3
553 regex_t regex;
554 regmatch_t regmatch;
555 };
557 static const struct got_error *open_diff_view(struct tog_view *,
558 struct got_object_id *, struct got_object_id *,
559 const char *, const char *, int, int, int, struct tog_view *,
560 struct got_repository *);
561 static const struct got_error *show_diff_view(struct tog_view *);
562 static const struct got_error *input_diff_view(struct tog_view **,
563 struct tog_view *, int);
564 static const struct got_error* close_diff_view(struct tog_view *);
565 static const struct got_error *search_start_diff_view(struct tog_view *);
566 static const struct got_error *search_next_diff_view(struct tog_view *);
568 static const struct got_error *open_log_view(struct tog_view *,
569 struct got_object_id *, struct got_repository *,
570 const char *, const char *, int);
571 static const struct got_error * show_log_view(struct tog_view *);
572 static const struct got_error *input_log_view(struct tog_view **,
573 struct tog_view *, int);
574 static const struct got_error *close_log_view(struct tog_view *);
575 static const struct got_error *search_start_log_view(struct tog_view *);
576 static const struct got_error *search_next_log_view(struct tog_view *);
578 static const struct got_error *open_blame_view(struct tog_view *, char *,
579 struct got_object_id *, struct got_repository *);
580 static const struct got_error *show_blame_view(struct tog_view *);
581 static const struct got_error *input_blame_view(struct tog_view **,
582 struct tog_view *, int);
583 static const struct got_error *close_blame_view(struct tog_view *);
584 static const struct got_error *search_start_blame_view(struct tog_view *);
585 static const struct got_error *search_next_blame_view(struct tog_view *);
587 static const struct got_error *open_tree_view(struct tog_view *,
588 struct got_object_id *, const char *, struct got_repository *);
589 static const struct got_error *show_tree_view(struct tog_view *);
590 static const struct got_error *input_tree_view(struct tog_view **,
591 struct tog_view *, int);
592 static const struct got_error *close_tree_view(struct tog_view *);
593 static const struct got_error *search_start_tree_view(struct tog_view *);
594 static const struct got_error *search_next_tree_view(struct tog_view *);
596 static const struct got_error *open_ref_view(struct tog_view *,
597 struct got_repository *);
598 static const struct got_error *show_ref_view(struct tog_view *);
599 static const struct got_error *input_ref_view(struct tog_view **,
600 struct tog_view *, int);
601 static const struct got_error *close_ref_view(struct tog_view *);
602 static const struct got_error *search_start_ref_view(struct tog_view *);
603 static const struct got_error *search_next_ref_view(struct tog_view *);
605 static volatile sig_atomic_t tog_sigwinch_received;
606 static volatile sig_atomic_t tog_sigpipe_received;
607 static volatile sig_atomic_t tog_sigcont_received;
608 static volatile sig_atomic_t tog_sigint_received;
609 static volatile sig_atomic_t tog_sigterm_received;
611 static void
612 tog_sigwinch(int signo)
614 tog_sigwinch_received = 1;
617 static void
618 tog_sigpipe(int signo)
620 tog_sigpipe_received = 1;
623 static void
624 tog_sigcont(int signo)
626 tog_sigcont_received = 1;
629 static void
630 tog_sigint(int signo)
632 tog_sigint_received = 1;
635 static void
636 tog_sigterm(int signo)
638 tog_sigterm_received = 1;
641 static int
642 tog_fatal_signal_received()
644 return (tog_sigpipe_received ||
645 tog_sigint_received || tog_sigint_received);
649 static const struct got_error *
650 view_close(struct tog_view *view)
652 const struct got_error *err = NULL;
654 if (view->child) {
655 view_close(view->child);
656 view->child = NULL;
658 if (view->close)
659 err = view->close(view);
660 if (view->panel)
661 del_panel(view->panel);
662 if (view->window)
663 delwin(view->window);
664 free(view);
665 return err;
668 static struct tog_view *
669 view_open(int nlines, int ncols, int begin_y, int begin_x,
670 enum tog_view_type type)
672 struct tog_view *view = calloc(1, sizeof(*view));
674 if (view == NULL)
675 return NULL;
677 view->type = type;
678 view->lines = LINES;
679 view->cols = COLS;
680 view->nlines = nlines ? nlines : LINES - begin_y;
681 view->ncols = ncols ? ncols : COLS - begin_x;
682 view->begin_y = begin_y;
683 view->begin_x = begin_x;
684 view->window = newwin(nlines, ncols, begin_y, begin_x);
685 if (view->window == NULL) {
686 view_close(view);
687 return NULL;
689 view->panel = new_panel(view->window);
690 if (view->panel == NULL ||
691 set_panel_userptr(view->panel, view) != OK) {
692 view_close(view);
693 return NULL;
696 keypad(view->window, TRUE);
697 return view;
700 static int
701 view_split_begin_x(int begin_x)
703 if (begin_x > 0 || COLS < 120)
704 return 0;
705 return (COLS - MAX(COLS / 2, 80));
708 static const struct got_error *view_resize(struct tog_view *);
710 static const struct got_error *
711 view_splitscreen(struct tog_view *view)
713 const struct got_error *err = NULL;
715 view->begin_y = 0;
716 view->begin_x = view_split_begin_x(0);
717 view->nlines = LINES;
718 view->ncols = COLS - view->begin_x;
719 view->lines = LINES;
720 view->cols = COLS;
721 err = view_resize(view);
722 if (err)
723 return err;
725 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
726 return got_error_from_errno("mvwin");
728 return NULL;
731 static const struct got_error *
732 view_fullscreen(struct tog_view *view)
734 const struct got_error *err = NULL;
736 view->begin_x = 0;
737 view->begin_y = 0;
738 view->nlines = LINES;
739 view->ncols = COLS;
740 view->lines = LINES;
741 view->cols = COLS;
742 err = view_resize(view);
743 if (err)
744 return err;
746 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
747 return got_error_from_errno("mvwin");
749 return NULL;
752 static int
753 view_is_parent_view(struct tog_view *view)
755 return view->parent == NULL;
758 static const struct got_error *
759 view_resize(struct tog_view *view)
761 int nlines, ncols;
763 if (view->lines > LINES)
764 nlines = view->nlines - (view->lines - LINES);
765 else
766 nlines = view->nlines + (LINES - view->lines);
768 if (view->cols > COLS)
769 ncols = view->ncols - (view->cols - COLS);
770 else
771 ncols = view->ncols + (COLS - view->cols);
773 if (wresize(view->window, nlines, ncols) == ERR)
774 return got_error_from_errno("wresize");
775 if (replace_panel(view->panel, view->window) == ERR)
776 return got_error_from_errno("replace_panel");
777 wclear(view->window);
779 view->nlines = nlines;
780 view->ncols = ncols;
781 view->lines = LINES;
782 view->cols = COLS;
784 if (view->child) {
785 view->child->begin_x = view_split_begin_x(view->begin_x);
786 if (view->child->begin_x == 0) {
787 view_fullscreen(view->child);
788 if (view->child->focussed)
789 show_panel(view->child->panel);
790 else
791 show_panel(view->panel);
792 } else {
793 view_splitscreen(view->child);
794 show_panel(view->child->panel);
798 return NULL;
801 static const struct got_error *
802 view_close_child(struct tog_view *view)
804 const struct got_error *err = NULL;
806 if (view->child == NULL)
807 return NULL;
809 err = view_close(view->child);
810 view->child = NULL;
811 return err;
814 static void
815 view_set_child(struct tog_view *view, struct tog_view *child)
817 view->child = child;
818 child->parent = view;
821 static int
822 view_is_splitscreen(struct tog_view *view)
824 return view->begin_x > 0;
827 static void
828 tog_resizeterm(void)
830 int cols, lines;
831 struct winsize size;
833 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
834 cols = 80; /* Default */
835 lines = 24;
836 } else {
837 cols = size.ws_col;
838 lines = size.ws_row;
840 resize_term(lines, cols);
843 static const struct got_error *
844 view_search_start(struct tog_view *view)
846 const struct got_error *err = NULL;
847 char pattern[1024];
848 int ret;
850 if (view->search_started) {
851 regfree(&view->regex);
852 view->searching = 0;
853 memset(&view->regmatch, 0, sizeof(view->regmatch));
855 view->search_started = 0;
857 if (view->nlines < 1)
858 return NULL;
860 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
861 wclrtoeol(view->window);
863 nocbreak();
864 echo();
865 ret = wgetnstr(view->window, pattern, sizeof(pattern));
866 cbreak();
867 noecho();
868 if (ret == ERR)
869 return NULL;
871 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
872 err = view->search_start(view);
873 if (err) {
874 regfree(&view->regex);
875 return err;
877 view->search_started = 1;
878 view->searching = TOG_SEARCH_FORWARD;
879 view->search_next_done = 0;
880 view->search_next(view);
883 return NULL;
886 static const struct got_error *
887 view_input(struct tog_view **new, int *done, struct tog_view *view,
888 struct tog_view_list_head *views)
890 const struct got_error *err = NULL;
891 struct tog_view *v;
892 int ch, errcode;
894 *new = NULL;
896 /* Clear "no matches" indicator. */
897 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
898 view->search_next_done == TOG_SEARCH_HAVE_NONE)
899 view->search_next_done = TOG_SEARCH_HAVE_MORE;
901 if (view->searching && !view->search_next_done) {
902 errcode = pthread_mutex_unlock(&tog_mutex);
903 if (errcode)
904 return got_error_set_errno(errcode,
905 "pthread_mutex_unlock");
906 sched_yield();
907 errcode = pthread_mutex_lock(&tog_mutex);
908 if (errcode)
909 return got_error_set_errno(errcode,
910 "pthread_mutex_lock");
911 view->search_next(view);
912 return NULL;
915 nodelay(stdscr, FALSE);
916 /* Allow threads to make progress while we are waiting for input. */
917 errcode = pthread_mutex_unlock(&tog_mutex);
918 if (errcode)
919 return got_error_set_errno(errcode, "pthread_mutex_unlock");
920 ch = wgetch(view->window);
921 errcode = pthread_mutex_lock(&tog_mutex);
922 if (errcode)
923 return got_error_set_errno(errcode, "pthread_mutex_lock");
924 nodelay(stdscr, TRUE);
926 if (tog_sigwinch_received || tog_sigcont_received) {
927 tog_resizeterm();
928 tog_sigwinch_received = 0;
929 tog_sigcont_received = 0;
930 TAILQ_FOREACH(v, views, entry) {
931 err = view_resize(v);
932 if (err)
933 return err;
934 err = v->input(new, v, KEY_RESIZE);
935 if (err)
936 return err;
937 if (v->child) {
938 err = view_resize(v->child);
939 if (err)
940 return err;
941 err = v->child->input(new, v->child,
942 KEY_RESIZE);
943 if (err)
944 return err;
949 switch (ch) {
950 case '\t':
951 if (view->child) {
952 view->focussed = 0;
953 view->child->focussed = 1;
954 view->focus_child = 1;
955 } else if (view->parent) {
956 view->focussed = 0;
957 view->parent->focussed = 1;
958 view->parent->focus_child = 0;
960 break;
961 case 'q':
962 err = view->input(new, view, ch);
963 view->dying = 1;
964 break;
965 case 'Q':
966 *done = 1;
967 break;
968 case 'f':
969 if (view_is_parent_view(view)) {
970 if (view->child == NULL)
971 break;
972 if (view_is_splitscreen(view->child)) {
973 view->focussed = 0;
974 view->child->focussed = 1;
975 err = view_fullscreen(view->child);
976 } else
977 err = view_splitscreen(view->child);
978 if (err)
979 break;
980 err = view->child->input(new, view->child,
981 KEY_RESIZE);
982 } else {
983 if (view_is_splitscreen(view)) {
984 view->parent->focussed = 0;
985 view->focussed = 1;
986 err = view_fullscreen(view);
987 } else {
988 err = view_splitscreen(view);
990 if (err)
991 break;
992 err = view->input(new, view, KEY_RESIZE);
994 break;
995 case KEY_RESIZE:
996 break;
997 case '/':
998 if (view->search_start)
999 view_search_start(view);
1000 else
1001 err = view->input(new, view, ch);
1002 break;
1003 case 'N':
1004 case 'n':
1005 if (view->search_started && view->search_next) {
1006 view->searching = (ch == 'n' ?
1007 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1008 view->search_next_done = 0;
1009 view->search_next(view);
1010 } else
1011 err = view->input(new, view, ch);
1012 break;
1013 default:
1014 err = view->input(new, view, ch);
1015 break;
1018 return err;
1021 void
1022 view_vborder(struct tog_view *view)
1024 PANEL *panel;
1025 const struct tog_view *view_above;
1027 if (view->parent)
1028 return view_vborder(view->parent);
1030 panel = panel_above(view->panel);
1031 if (panel == NULL)
1032 return;
1034 view_above = panel_userptr(panel);
1035 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1036 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1039 int
1040 view_needs_focus_indication(struct tog_view *view)
1042 if (view_is_parent_view(view)) {
1043 if (view->child == NULL || view->child->focussed)
1044 return 0;
1045 if (!view_is_splitscreen(view->child))
1046 return 0;
1047 } else if (!view_is_splitscreen(view))
1048 return 0;
1050 return view->focussed;
1053 static const struct got_error *
1054 view_loop(struct tog_view *view)
1056 const struct got_error *err = NULL;
1057 struct tog_view_list_head views;
1058 struct tog_view *new_view;
1059 int fast_refresh = 10;
1060 int done = 0, errcode;
1062 errcode = pthread_mutex_lock(&tog_mutex);
1063 if (errcode)
1064 return got_error_set_errno(errcode, "pthread_mutex_lock");
1066 TAILQ_INIT(&views);
1067 TAILQ_INSERT_HEAD(&views, view, entry);
1069 view->focussed = 1;
1070 err = view->show(view);
1071 if (err)
1072 return err;
1073 update_panels();
1074 doupdate();
1075 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1076 /* Refresh fast during initialization, then become slower. */
1077 if (fast_refresh && fast_refresh-- == 0)
1078 halfdelay(10); /* switch to once per second */
1080 err = view_input(&new_view, &done, view, &views);
1081 if (err)
1082 break;
1083 if (view->dying) {
1084 struct tog_view *v, *prev = NULL;
1086 if (view_is_parent_view(view))
1087 prev = TAILQ_PREV(view, tog_view_list_head,
1088 entry);
1089 else if (view->parent)
1090 prev = view->parent;
1092 if (view->parent) {
1093 view->parent->child = NULL;
1094 view->parent->focus_child = 0;
1095 } else
1096 TAILQ_REMOVE(&views, view, entry);
1098 err = view_close(view);
1099 if (err)
1100 goto done;
1102 view = NULL;
1103 TAILQ_FOREACH(v, &views, entry) {
1104 if (v->focussed)
1105 break;
1107 if (view == NULL && new_view == NULL) {
1108 /* No view has focus. Try to pick one. */
1109 if (prev)
1110 view = prev;
1111 else if (!TAILQ_EMPTY(&views)) {
1112 view = TAILQ_LAST(&views,
1113 tog_view_list_head);
1115 if (view) {
1116 if (view->focus_child) {
1117 view->child->focussed = 1;
1118 view = view->child;
1119 } else
1120 view->focussed = 1;
1124 if (new_view) {
1125 struct tog_view *v, *t;
1126 /* Only allow one parent view per type. */
1127 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1128 if (v->type != new_view->type)
1129 continue;
1130 TAILQ_REMOVE(&views, v, entry);
1131 err = view_close(v);
1132 if (err)
1133 goto done;
1134 break;
1136 TAILQ_INSERT_TAIL(&views, new_view, entry);
1137 view = new_view;
1139 if (view) {
1140 if (view_is_parent_view(view)) {
1141 if (view->child && view->child->focussed)
1142 view = view->child;
1143 } else {
1144 if (view->parent && view->parent->focussed)
1145 view = view->parent;
1147 show_panel(view->panel);
1148 if (view->child && view_is_splitscreen(view->child))
1149 show_panel(view->child->panel);
1150 if (view->parent && view_is_splitscreen(view)) {
1151 err = view->parent->show(view->parent);
1152 if (err)
1153 goto done;
1155 err = view->show(view);
1156 if (err)
1157 goto done;
1158 if (view->child) {
1159 err = view->child->show(view->child);
1160 if (err)
1161 goto done;
1163 update_panels();
1164 doupdate();
1167 done:
1168 while (!TAILQ_EMPTY(&views)) {
1169 view = TAILQ_FIRST(&views);
1170 TAILQ_REMOVE(&views, view, entry);
1171 view_close(view);
1174 errcode = pthread_mutex_unlock(&tog_mutex);
1175 if (errcode && err == NULL)
1176 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1178 return err;
1181 __dead static void
1182 usage_log(void)
1184 endwin();
1185 fprintf(stderr,
1186 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1187 getprogname());
1188 exit(1);
1191 /* Create newly allocated wide-character string equivalent to a byte string. */
1192 static const struct got_error *
1193 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1195 char *vis = NULL;
1196 const struct got_error *err = NULL;
1198 *ws = NULL;
1199 *wlen = mbstowcs(NULL, s, 0);
1200 if (*wlen == (size_t)-1) {
1201 int vislen;
1202 if (errno != EILSEQ)
1203 return got_error_from_errno("mbstowcs");
1205 /* byte string invalid in current encoding; try to "fix" it */
1206 err = got_mbsavis(&vis, &vislen, s);
1207 if (err)
1208 return err;
1209 *wlen = mbstowcs(NULL, vis, 0);
1210 if (*wlen == (size_t)-1) {
1211 err = got_error_from_errno("mbstowcs"); /* give up */
1212 goto done;
1216 *ws = calloc(*wlen + 1, sizeof(**ws));
1217 if (*ws == NULL) {
1218 err = got_error_from_errno("calloc");
1219 goto done;
1222 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1223 err = got_error_from_errno("mbstowcs");
1224 done:
1225 free(vis);
1226 if (err) {
1227 free(*ws);
1228 *ws = NULL;
1229 *wlen = 0;
1231 return err;
1234 static const struct got_error *
1235 expand_tab(char **ptr, const char *src)
1237 char *dst;
1238 size_t len, n, idx = 0, sz = 0;
1240 *ptr = NULL;
1241 n = len = strlen(src);
1242 dst = malloc((n + 1) * sizeof(char));
1243 if (dst == NULL)
1244 return got_error_from_errno("malloc");
1246 while (idx < len && src[idx]) {
1247 const char c = src[idx];
1249 if (c == '\t') {
1250 size_t nb = TABSIZE - sz % TABSIZE;
1251 n += nb;
1252 dst = reallocarray(dst, n, sizeof(char));
1253 if (dst == NULL)
1254 return got_error_from_errno("reallocarray");
1255 memcpy(dst + sz, " ", nb);
1256 sz += nb;
1257 } else
1258 dst[sz++] = src[idx];
1259 ++idx;
1262 dst[sz] = '\0';
1263 *ptr = dst;
1264 return NULL;
1267 /* Format a line for display, ensuring that it won't overflow a width limit. */
1268 static const struct got_error *
1269 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1270 int col_tab_align, int expand)
1272 const struct got_error *err = NULL;
1273 int cols = 0;
1274 wchar_t *wline = NULL;
1275 char *exstr = NULL;
1276 size_t wlen;
1277 int i;
1279 *wlinep = NULL;
1280 *widthp = 0;
1282 if (expand) {
1283 err = expand_tab(&exstr, line);
1284 if (err)
1285 return err;
1288 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1289 free(exstr);
1290 if (err)
1291 return err;
1293 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1294 wline[wlen - 1] = L'\0';
1295 wlen--;
1297 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1298 wline[wlen - 1] = L'\0';
1299 wlen--;
1302 i = 0;
1303 while (i < wlen) {
1304 int width = wcwidth(wline[i]);
1306 if (width == 0) {
1307 i++;
1308 continue;
1311 if (width == 1 || width == 2) {
1312 if (cols + width > wlimit)
1313 break;
1314 cols += width;
1315 i++;
1316 } else if (width == -1) {
1317 if (wline[i] == L'\t') {
1318 width = TABSIZE -
1319 ((cols + col_tab_align) % TABSIZE);
1320 } else {
1321 width = 1;
1322 wline[i] = L'.';
1324 if (cols + width > wlimit)
1325 break;
1326 cols += width;
1327 i++;
1328 } else {
1329 err = got_error_from_errno("wcwidth");
1330 goto done;
1333 wline[i] = L'\0';
1334 if (widthp)
1335 *widthp = cols;
1336 done:
1337 if (err)
1338 free(wline);
1339 else
1340 *wlinep = wline;
1341 return err;
1344 /* Skip the leading nscroll columns of a wide character string. */
1345 const struct got_error *
1346 scroll_wline(wchar_t **wlinep, wchar_t *wline, int nscroll,
1347 int col_tab_align)
1349 int cols = 0;
1350 size_t wlen = wcslen(wline);
1351 int i = 0, j = 0;
1353 *wlinep = wline;
1355 while (i < wlen && cols < nscroll) {
1356 int width = wcwidth(wline[i]);
1358 if (width == 0) {
1359 i++;
1360 continue;
1363 if (width == 1 || width == 2) {
1364 if (cols + width > nscroll)
1365 break;
1366 cols += width;
1367 i++;
1368 } else if (width == -1) {
1369 if (wline[i] == L'\t') {
1370 width = TABSIZE -
1371 ((cols + col_tab_align) % TABSIZE);
1372 } else {
1373 width = 1;
1374 wline[i] = L'.';
1376 if (cols + width > nscroll)
1377 break;
1378 cols += width;
1379 i++;
1380 } else
1381 return got_error_from_errno("wcwidth");
1382 j++;
1385 *wlinep = &wline[j];
1386 return NULL;
1389 static const struct got_error*
1390 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1391 struct got_object_id *id, struct got_repository *repo)
1393 static const struct got_error *err = NULL;
1394 struct got_reflist_entry *re;
1395 char *s;
1396 const char *name;
1398 *refs_str = NULL;
1400 TAILQ_FOREACH(re, refs, entry) {
1401 struct got_tag_object *tag = NULL;
1402 struct got_object_id *ref_id;
1403 int cmp;
1405 name = got_ref_get_name(re->ref);
1406 if (strcmp(name, GOT_REF_HEAD) == 0)
1407 continue;
1408 if (strncmp(name, "refs/", 5) == 0)
1409 name += 5;
1410 if (strncmp(name, "got/", 4) == 0 &&
1411 strncmp(name, "got/backup/", 11) != 0)
1412 continue;
1413 if (strncmp(name, "heads/", 6) == 0)
1414 name += 6;
1415 if (strncmp(name, "remotes/", 8) == 0) {
1416 name += 8;
1417 s = strstr(name, "/" GOT_REF_HEAD);
1418 if (s != NULL && s[strlen(s)] == '\0')
1419 continue;
1421 err = got_ref_resolve(&ref_id, repo, re->ref);
1422 if (err)
1423 break;
1424 if (strncmp(name, "tags/", 5) == 0) {
1425 err = got_object_open_as_tag(&tag, repo, ref_id);
1426 if (err) {
1427 if (err->code != GOT_ERR_OBJ_TYPE) {
1428 free(ref_id);
1429 break;
1431 /* Ref points at something other than a tag. */
1432 err = NULL;
1433 tag = NULL;
1436 cmp = got_object_id_cmp(tag ?
1437 got_object_tag_get_object_id(tag) : ref_id, id);
1438 free(ref_id);
1439 if (tag)
1440 got_object_tag_close(tag);
1441 if (cmp != 0)
1442 continue;
1443 s = *refs_str;
1444 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1445 s ? ", " : "", name) == -1) {
1446 err = got_error_from_errno("asprintf");
1447 free(s);
1448 *refs_str = NULL;
1449 break;
1451 free(s);
1454 return err;
1457 static const struct got_error *
1458 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1459 int col_tab_align)
1461 char *smallerthan;
1463 smallerthan = strchr(author, '<');
1464 if (smallerthan && smallerthan[1] != '\0')
1465 author = smallerthan + 1;
1466 author[strcspn(author, "@>")] = '\0';
1467 return format_line(wauthor, author_width, author, limit, col_tab_align,
1468 0);
1471 static const struct got_error *
1472 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1473 struct got_object_id *id, const size_t date_display_cols,
1474 int author_display_cols)
1476 struct tog_log_view_state *s = &view->state.log;
1477 const struct got_error *err = NULL;
1478 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1479 char *logmsg0 = NULL, *logmsg = NULL;
1480 char *author = NULL;
1481 wchar_t *wlogmsg = NULL, *wauthor = NULL, *scrolled_wline;
1482 int author_width, logmsg_width;
1483 char *newline, *line = NULL;
1484 int col, limit;
1485 const int avail = view->ncols;
1486 struct tm tm;
1487 time_t committer_time;
1488 struct tog_color *tc;
1490 committer_time = got_object_commit_get_committer_time(commit);
1491 if (gmtime_r(&committer_time, &tm) == NULL)
1492 return got_error_from_errno("gmtime_r");
1493 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1494 return got_error(GOT_ERR_NO_SPACE);
1496 if (avail <= date_display_cols)
1497 limit = MIN(sizeof(datebuf) - 1, avail);
1498 else
1499 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1500 tc = get_color(&s->colors, TOG_COLOR_DATE);
1501 if (tc)
1502 wattr_on(view->window,
1503 COLOR_PAIR(tc->colorpair), NULL);
1504 waddnstr(view->window, datebuf, limit);
1505 if (tc)
1506 wattr_off(view->window,
1507 COLOR_PAIR(tc->colorpair), NULL);
1508 col = limit;
1509 if (col > avail)
1510 goto done;
1512 if (avail >= 120) {
1513 char *id_str;
1514 err = got_object_id_str(&id_str, id);
1515 if (err)
1516 goto done;
1517 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1518 if (tc)
1519 wattr_on(view->window,
1520 COLOR_PAIR(tc->colorpair), NULL);
1521 wprintw(view->window, "%.8s ", id_str);
1522 if (tc)
1523 wattr_off(view->window,
1524 COLOR_PAIR(tc->colorpair), NULL);
1525 free(id_str);
1526 col += 9;
1527 if (col > avail)
1528 goto done;
1531 author = strdup(got_object_commit_get_author(commit));
1532 if (author == NULL) {
1533 err = got_error_from_errno("strdup");
1534 goto done;
1536 err = format_author(&wauthor, &author_width, author, avail - col, col);
1537 if (err)
1538 goto done;
1539 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1540 if (tc)
1541 wattr_on(view->window,
1542 COLOR_PAIR(tc->colorpair), NULL);
1543 waddwstr(view->window, wauthor);
1544 if (tc)
1545 wattr_off(view->window,
1546 COLOR_PAIR(tc->colorpair), NULL);
1547 col += author_width;
1548 while (col < avail && author_width < author_display_cols + 2) {
1549 waddch(view->window, ' ');
1550 col++;
1551 author_width++;
1553 if (col > avail)
1554 goto done;
1556 err = got_object_commit_get_logmsg(&logmsg0, commit);
1557 if (err)
1558 goto done;
1559 logmsg = logmsg0;
1560 while (*logmsg == '\n')
1561 logmsg++;
1562 newline = strchr(logmsg, '\n');
1563 if (newline)
1564 *newline = '\0';
1565 limit = view->x + avail - col;
1566 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col, 1);
1567 if (err)
1568 goto done;
1569 err = scroll_wline(&scrolled_wline, wlogmsg, view->x, col);
1570 if (err)
1571 goto done;
1572 waddwstr(view->window, scrolled_wline);
1573 logmsg_width = wcswidth(scrolled_wline, wcslen(scrolled_wline));
1574 col += MAX(logmsg_width, 0);
1575 while (col < avail) {
1576 waddch(view->window, ' ');
1577 col++;
1579 done:
1580 free(logmsg0);
1581 free(wlogmsg);
1582 free(author);
1583 free(wauthor);
1584 free(line);
1585 return err;
1588 static struct commit_queue_entry *
1589 alloc_commit_queue_entry(struct got_commit_object *commit,
1590 struct got_object_id *id)
1592 struct commit_queue_entry *entry;
1594 entry = calloc(1, sizeof(*entry));
1595 if (entry == NULL)
1596 return NULL;
1598 entry->id = id;
1599 entry->commit = commit;
1600 return entry;
1603 static void
1604 pop_commit(struct commit_queue *commits)
1606 struct commit_queue_entry *entry;
1608 entry = TAILQ_FIRST(&commits->head);
1609 TAILQ_REMOVE(&commits->head, entry, entry);
1610 got_object_commit_close(entry->commit);
1611 commits->ncommits--;
1612 /* Don't free entry->id! It is owned by the commit graph. */
1613 free(entry);
1616 static void
1617 free_commits(struct commit_queue *commits)
1619 while (!TAILQ_EMPTY(&commits->head))
1620 pop_commit(commits);
1623 static const struct got_error *
1624 match_commit(int *have_match, struct got_object_id *id,
1625 struct got_commit_object *commit, regex_t *regex)
1627 const struct got_error *err = NULL;
1628 regmatch_t regmatch;
1629 char *id_str = NULL, *logmsg = NULL;
1631 *have_match = 0;
1633 err = got_object_id_str(&id_str, id);
1634 if (err)
1635 return err;
1637 err = got_object_commit_get_logmsg(&logmsg, commit);
1638 if (err)
1639 goto done;
1641 if (regexec(regex, got_object_commit_get_author(commit), 1,
1642 &regmatch, 0) == 0 ||
1643 regexec(regex, got_object_commit_get_committer(commit), 1,
1644 &regmatch, 0) == 0 ||
1645 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1646 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1647 *have_match = 1;
1648 done:
1649 free(id_str);
1650 free(logmsg);
1651 return err;
1654 static const struct got_error *
1655 queue_commits(struct tog_log_thread_args *a)
1657 const struct got_error *err = NULL;
1660 * We keep all commits open throughout the lifetime of the log
1661 * view in order to avoid having to re-fetch commits from disk
1662 * while updating the display.
1664 do {
1665 struct got_object_id *id;
1666 struct got_commit_object *commit;
1667 struct commit_queue_entry *entry;
1668 int errcode;
1670 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1671 NULL, NULL);
1672 if (err || id == NULL)
1673 break;
1675 err = got_object_open_as_commit(&commit, a->repo, id);
1676 if (err)
1677 break;
1678 entry = alloc_commit_queue_entry(commit, id);
1679 if (entry == NULL) {
1680 err = got_error_from_errno("alloc_commit_queue_entry");
1681 break;
1684 errcode = pthread_mutex_lock(&tog_mutex);
1685 if (errcode) {
1686 err = got_error_set_errno(errcode,
1687 "pthread_mutex_lock");
1688 break;
1691 entry->idx = a->commits->ncommits;
1692 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1693 a->commits->ncommits++;
1695 if (*a->searching == TOG_SEARCH_FORWARD &&
1696 !*a->search_next_done) {
1697 int have_match;
1698 err = match_commit(&have_match, id, commit, a->regex);
1699 if (err)
1700 break;
1701 if (have_match)
1702 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1705 errcode = pthread_mutex_unlock(&tog_mutex);
1706 if (errcode && err == NULL)
1707 err = got_error_set_errno(errcode,
1708 "pthread_mutex_unlock");
1709 if (err)
1710 break;
1711 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1713 return err;
1716 static void
1717 select_commit(struct tog_log_view_state *s)
1719 struct commit_queue_entry *entry;
1720 int ncommits = 0;
1722 entry = s->first_displayed_entry;
1723 while (entry) {
1724 if (ncommits == s->selected) {
1725 s->selected_entry = entry;
1726 break;
1728 entry = TAILQ_NEXT(entry, entry);
1729 ncommits++;
1733 static const struct got_error *
1734 draw_commits(struct tog_view *view)
1736 const struct got_error *err = NULL;
1737 struct tog_log_view_state *s = &view->state.log;
1738 struct commit_queue_entry *entry = s->selected_entry;
1739 const int limit = view->nlines;
1740 int width;
1741 int ncommits, author_cols = 4;
1742 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1743 char *refs_str = NULL;
1744 wchar_t *wline;
1745 struct tog_color *tc;
1746 static const size_t date_display_cols = 12;
1748 if (s->selected_entry &&
1749 !(view->searching && view->search_next_done == 0)) {
1750 struct got_reflist_head *refs;
1751 err = got_object_id_str(&id_str, s->selected_entry->id);
1752 if (err)
1753 return err;
1754 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1755 s->selected_entry->id);
1756 if (refs) {
1757 err = build_refs_str(&refs_str, refs,
1758 s->selected_entry->id, s->repo);
1759 if (err)
1760 goto done;
1764 if (s->thread_args.commits_needed == 0)
1765 halfdelay(10); /* disable fast refresh */
1767 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1768 if (asprintf(&ncommits_str, " [%d/%d] %s",
1769 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1770 (view->searching && !view->search_next_done) ?
1771 "searching..." : "loading...") == -1) {
1772 err = got_error_from_errno("asprintf");
1773 goto done;
1775 } else {
1776 const char *search_str = NULL;
1778 if (view->searching) {
1779 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1780 search_str = "no more matches";
1781 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1782 search_str = "no matches found";
1783 else if (!view->search_next_done)
1784 search_str = "searching...";
1787 if (asprintf(&ncommits_str, " [%d/%d] %s",
1788 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1789 search_str ? search_str :
1790 (refs_str ? refs_str : "")) == -1) {
1791 err = got_error_from_errno("asprintf");
1792 goto done;
1796 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1797 if (asprintf(&header, "commit %s %s%s",
1798 id_str ? id_str : "........................................",
1799 s->in_repo_path, ncommits_str) == -1) {
1800 err = got_error_from_errno("asprintf");
1801 header = NULL;
1802 goto done;
1804 } else if (asprintf(&header, "commit %s%s",
1805 id_str ? id_str : "........................................",
1806 ncommits_str) == -1) {
1807 err = got_error_from_errno("asprintf");
1808 header = NULL;
1809 goto done;
1811 err = format_line(&wline, &width, header, view->ncols, 0, 0);
1812 if (err)
1813 goto done;
1815 werase(view->window);
1817 if (view_needs_focus_indication(view))
1818 wstandout(view->window);
1819 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1820 if (tc)
1821 wattr_on(view->window,
1822 COLOR_PAIR(tc->colorpair), NULL);
1823 waddwstr(view->window, wline);
1824 if (tc)
1825 wattr_off(view->window,
1826 COLOR_PAIR(tc->colorpair), NULL);
1827 while (width < view->ncols) {
1828 waddch(view->window, ' ');
1829 width++;
1831 if (view_needs_focus_indication(view))
1832 wstandend(view->window);
1833 free(wline);
1834 if (limit <= 1)
1835 goto done;
1837 /* Grow author column size if necessary, and set view->maxx. */
1838 entry = s->first_displayed_entry;
1839 ncommits = 0;
1840 view->maxx = 0;
1841 while (entry) {
1842 char *author, *eol, *msg, *msg0;
1843 wchar_t *wauthor, *wmsg;
1844 int width;
1845 if (ncommits >= limit - 1)
1846 break;
1847 author = strdup(got_object_commit_get_author(entry->commit));
1848 if (author == NULL) {
1849 err = got_error_from_errno("strdup");
1850 goto done;
1852 err = format_author(&wauthor, &width, author, COLS,
1853 date_display_cols);
1854 if (author_cols < width)
1855 author_cols = width;
1856 free(wauthor);
1857 free(author);
1858 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1859 if (err)
1860 goto done;
1861 msg = msg0;
1862 while (*msg == '\n')
1863 ++msg;
1864 if ((eol = strchr(msg, '\n')))
1865 *eol = '\0';
1866 err = format_line(&wmsg, &width, msg, INT_MAX,
1867 date_display_cols + author_cols, 0);
1868 if (err)
1869 goto done;
1870 view->maxx = MAX(view->maxx, width);
1871 free(msg0);
1872 free(wmsg);
1873 ncommits++;
1874 entry = TAILQ_NEXT(entry, entry);
1877 entry = s->first_displayed_entry;
1878 s->last_displayed_entry = s->first_displayed_entry;
1879 ncommits = 0;
1880 while (entry) {
1881 if (ncommits >= limit - 1)
1882 break;
1883 if (ncommits == s->selected)
1884 wstandout(view->window);
1885 err = draw_commit(view, entry->commit, entry->id,
1886 date_display_cols, author_cols);
1887 if (ncommits == s->selected)
1888 wstandend(view->window);
1889 if (err)
1890 goto done;
1891 ncommits++;
1892 s->last_displayed_entry = entry;
1893 entry = TAILQ_NEXT(entry, entry);
1896 view_vborder(view);
1897 update_panels();
1898 doupdate();
1899 done:
1900 free(id_str);
1901 free(refs_str);
1902 free(ncommits_str);
1903 free(header);
1904 return err;
1907 static void
1908 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1910 struct commit_queue_entry *entry;
1911 int nscrolled = 0;
1913 entry = TAILQ_FIRST(&s->commits.head);
1914 if (s->first_displayed_entry == entry)
1915 return;
1917 entry = s->first_displayed_entry;
1918 while (entry && nscrolled < maxscroll) {
1919 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1920 if (entry) {
1921 s->first_displayed_entry = entry;
1922 nscrolled++;
1927 static const struct got_error *
1928 trigger_log_thread(struct tog_view *view, int wait)
1930 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1931 int errcode;
1933 halfdelay(1); /* fast refresh while loading commits */
1935 while (ta->commits_needed > 0 || ta->load_all) {
1936 if (ta->log_complete)
1937 break;
1939 /* Wake the log thread. */
1940 errcode = pthread_cond_signal(&ta->need_commits);
1941 if (errcode)
1942 return got_error_set_errno(errcode,
1943 "pthread_cond_signal");
1946 * The mutex will be released while the view loop waits
1947 * in wgetch(), at which time the log thread will run.
1949 if (!wait)
1950 break;
1952 /* Display progress update in log view. */
1953 show_log_view(view);
1954 update_panels();
1955 doupdate();
1957 /* Wait right here while next commit is being loaded. */
1958 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1959 if (errcode)
1960 return got_error_set_errno(errcode,
1961 "pthread_cond_wait");
1963 /* Display progress update in log view. */
1964 show_log_view(view);
1965 update_panels();
1966 doupdate();
1969 return NULL;
1972 static const struct got_error *
1973 log_scroll_down(struct tog_view *view, int maxscroll)
1975 struct tog_log_view_state *s = &view->state.log;
1976 const struct got_error *err = NULL;
1977 struct commit_queue_entry *pentry;
1978 int nscrolled = 0, ncommits_needed;
1980 if (s->last_displayed_entry == NULL)
1981 return NULL;
1983 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1984 if (s->commits.ncommits < ncommits_needed &&
1985 !s->thread_args.log_complete) {
1987 * Ask the log thread for required amount of commits.
1989 s->thread_args.commits_needed += maxscroll;
1990 err = trigger_log_thread(view, 1);
1991 if (err)
1992 return err;
1995 do {
1996 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1997 if (pentry == NULL)
1998 break;
2000 s->last_displayed_entry = pentry;
2002 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2003 if (pentry == NULL)
2004 break;
2005 s->first_displayed_entry = pentry;
2006 } while (++nscrolled < maxscroll);
2008 return err;
2011 static const struct got_error *
2012 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2013 struct got_commit_object *commit, struct got_object_id *commit_id,
2014 struct tog_view *log_view, struct got_repository *repo)
2016 const struct got_error *err;
2017 struct got_object_qid *parent_id;
2018 struct tog_view *diff_view;
2020 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2021 if (diff_view == NULL)
2022 return got_error_from_errno("view_open");
2024 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2025 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2026 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2027 if (err == NULL)
2028 *new_view = diff_view;
2029 return err;
2032 static const struct got_error *
2033 tree_view_visit_subtree(struct tog_tree_view_state *s,
2034 struct got_tree_object *subtree)
2036 struct tog_parent_tree *parent;
2038 parent = calloc(1, sizeof(*parent));
2039 if (parent == NULL)
2040 return got_error_from_errno("calloc");
2042 parent->tree = s->tree;
2043 parent->first_displayed_entry = s->first_displayed_entry;
2044 parent->selected_entry = s->selected_entry;
2045 parent->selected = s->selected;
2046 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2047 s->tree = subtree;
2048 s->selected = 0;
2049 s->first_displayed_entry = NULL;
2050 return NULL;
2053 static const struct got_error *
2054 tree_view_walk_path(struct tog_tree_view_state *s,
2055 struct got_commit_object *commit, const char *path)
2057 const struct got_error *err = NULL;
2058 struct got_tree_object *tree = NULL;
2059 const char *p;
2060 char *slash, *subpath = NULL;
2062 /* Walk the path and open corresponding tree objects. */
2063 p = path;
2064 while (*p) {
2065 struct got_tree_entry *te;
2066 struct got_object_id *tree_id;
2067 char *te_name;
2069 while (p[0] == '/')
2070 p++;
2072 /* Ensure the correct subtree entry is selected. */
2073 slash = strchr(p, '/');
2074 if (slash == NULL)
2075 te_name = strdup(p);
2076 else
2077 te_name = strndup(p, slash - p);
2078 if (te_name == NULL) {
2079 err = got_error_from_errno("strndup");
2080 break;
2082 te = got_object_tree_find_entry(s->tree, te_name);
2083 if (te == NULL) {
2084 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2085 free(te_name);
2086 break;
2088 free(te_name);
2089 s->first_displayed_entry = s->selected_entry = te;
2091 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2092 break; /* jump to this file's entry */
2094 slash = strchr(p, '/');
2095 if (slash)
2096 subpath = strndup(path, slash - path);
2097 else
2098 subpath = strdup(path);
2099 if (subpath == NULL) {
2100 err = got_error_from_errno("strdup");
2101 break;
2104 err = got_object_id_by_path(&tree_id, s->repo, commit,
2105 subpath);
2106 if (err)
2107 break;
2109 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2110 free(tree_id);
2111 if (err)
2112 break;
2114 err = tree_view_visit_subtree(s, tree);
2115 if (err) {
2116 got_object_tree_close(tree);
2117 break;
2119 if (slash == NULL)
2120 break;
2121 free(subpath);
2122 subpath = NULL;
2123 p = slash;
2126 free(subpath);
2127 return err;
2130 static const struct got_error *
2131 browse_commit_tree(struct tog_view **new_view, int begin_x,
2132 struct commit_queue_entry *entry, const char *path,
2133 const char *head_ref_name, struct got_repository *repo)
2135 const struct got_error *err = NULL;
2136 struct tog_tree_view_state *s;
2137 struct tog_view *tree_view;
2139 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2140 if (tree_view == NULL)
2141 return got_error_from_errno("view_open");
2143 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2144 if (err)
2145 return err;
2146 s = &tree_view->state.tree;
2148 *new_view = tree_view;
2150 if (got_path_is_root_dir(path))
2151 return NULL;
2153 return tree_view_walk_path(s, entry->commit, path);
2156 static const struct got_error *
2157 block_signals_used_by_main_thread(void)
2159 sigset_t sigset;
2160 int errcode;
2162 if (sigemptyset(&sigset) == -1)
2163 return got_error_from_errno("sigemptyset");
2165 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2166 if (sigaddset(&sigset, SIGWINCH) == -1)
2167 return got_error_from_errno("sigaddset");
2168 if (sigaddset(&sigset, SIGCONT) == -1)
2169 return got_error_from_errno("sigaddset");
2170 if (sigaddset(&sigset, SIGINT) == -1)
2171 return got_error_from_errno("sigaddset");
2172 if (sigaddset(&sigset, SIGTERM) == -1)
2173 return got_error_from_errno("sigaddset");
2175 /* ncurses handles SIGTSTP */
2176 if (sigaddset(&sigset, SIGTSTP) == -1)
2177 return got_error_from_errno("sigaddset");
2179 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2180 if (errcode)
2181 return got_error_set_errno(errcode, "pthread_sigmask");
2183 return NULL;
2186 static void *
2187 log_thread(void *arg)
2189 const struct got_error *err = NULL;
2190 int errcode = 0;
2191 struct tog_log_thread_args *a = arg;
2192 int done = 0;
2194 err = block_signals_used_by_main_thread();
2195 if (err)
2196 return (void *)err;
2198 while (!done && !err && !tog_fatal_signal_received()) {
2199 err = queue_commits(a);
2200 if (err) {
2201 if (err->code != GOT_ERR_ITER_COMPLETED)
2202 return (void *)err;
2203 err = NULL;
2204 done = 1;
2205 } else if (a->commits_needed > 0 && !a->load_all)
2206 a->commits_needed--;
2208 errcode = pthread_mutex_lock(&tog_mutex);
2209 if (errcode) {
2210 err = got_error_set_errno(errcode,
2211 "pthread_mutex_lock");
2212 break;
2213 } else if (*a->quit)
2214 done = 1;
2215 else if (*a->first_displayed_entry == NULL) {
2216 *a->first_displayed_entry =
2217 TAILQ_FIRST(&a->commits->head);
2218 *a->selected_entry = *a->first_displayed_entry;
2221 errcode = pthread_cond_signal(&a->commit_loaded);
2222 if (errcode) {
2223 err = got_error_set_errno(errcode,
2224 "pthread_cond_signal");
2225 pthread_mutex_unlock(&tog_mutex);
2226 break;
2229 if (done)
2230 a->commits_needed = 0;
2231 else {
2232 if (a->commits_needed == 0 && !a->load_all) {
2233 errcode = pthread_cond_wait(&a->need_commits,
2234 &tog_mutex);
2235 if (errcode)
2236 err = got_error_set_errno(errcode,
2237 "pthread_cond_wait");
2238 if (*a->quit)
2239 done = 1;
2243 errcode = pthread_mutex_unlock(&tog_mutex);
2244 if (errcode && err == NULL)
2245 err = got_error_set_errno(errcode,
2246 "pthread_mutex_unlock");
2248 a->log_complete = 1;
2249 return (void *)err;
2252 static const struct got_error *
2253 stop_log_thread(struct tog_log_view_state *s)
2255 const struct got_error *err = NULL;
2256 int errcode;
2258 if (s->thread) {
2259 s->quit = 1;
2260 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2261 if (errcode)
2262 return got_error_set_errno(errcode,
2263 "pthread_cond_signal");
2264 errcode = pthread_mutex_unlock(&tog_mutex);
2265 if (errcode)
2266 return got_error_set_errno(errcode,
2267 "pthread_mutex_unlock");
2268 errcode = pthread_join(s->thread, (void **)&err);
2269 if (errcode)
2270 return got_error_set_errno(errcode, "pthread_join");
2271 errcode = pthread_mutex_lock(&tog_mutex);
2272 if (errcode)
2273 return got_error_set_errno(errcode,
2274 "pthread_mutex_lock");
2275 s->thread = 0; //NULL;
2278 if (s->thread_args.repo) {
2279 err = got_repo_close(s->thread_args.repo);
2280 s->thread_args.repo = NULL;
2283 if (s->thread_args.pack_fds) {
2284 const struct got_error *pack_err =
2285 got_repo_pack_fds_close(s->thread_args.pack_fds);
2286 if (err == NULL)
2287 err = pack_err;
2288 s->thread_args.pack_fds = NULL;
2291 if (s->thread_args.graph) {
2292 got_commit_graph_close(s->thread_args.graph);
2293 s->thread_args.graph = NULL;
2296 return err;
2299 static const struct got_error *
2300 close_log_view(struct tog_view *view)
2302 const struct got_error *err = NULL;
2303 struct tog_log_view_state *s = &view->state.log;
2304 int errcode;
2306 err = stop_log_thread(s);
2308 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2309 if (errcode && err == NULL)
2310 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2312 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2313 if (errcode && err == NULL)
2314 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2316 free_commits(&s->commits);
2317 free(s->in_repo_path);
2318 s->in_repo_path = NULL;
2319 free(s->start_id);
2320 s->start_id = NULL;
2321 free(s->head_ref_name);
2322 s->head_ref_name = NULL;
2323 return err;
2326 static const struct got_error *
2327 search_start_log_view(struct tog_view *view)
2329 struct tog_log_view_state *s = &view->state.log;
2331 s->matched_entry = NULL;
2332 s->search_entry = NULL;
2333 return NULL;
2336 static const struct got_error *
2337 search_next_log_view(struct tog_view *view)
2339 const struct got_error *err = NULL;
2340 struct tog_log_view_state *s = &view->state.log;
2341 struct commit_queue_entry *entry;
2343 /* Display progress update in log view. */
2344 show_log_view(view);
2345 update_panels();
2346 doupdate();
2348 if (s->search_entry) {
2349 int errcode, ch;
2350 errcode = pthread_mutex_unlock(&tog_mutex);
2351 if (errcode)
2352 return got_error_set_errno(errcode,
2353 "pthread_mutex_unlock");
2354 ch = wgetch(view->window);
2355 errcode = pthread_mutex_lock(&tog_mutex);
2356 if (errcode)
2357 return got_error_set_errno(errcode,
2358 "pthread_mutex_lock");
2359 if (ch == KEY_BACKSPACE) {
2360 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2361 return NULL;
2363 if (view->searching == TOG_SEARCH_FORWARD)
2364 entry = TAILQ_NEXT(s->search_entry, entry);
2365 else
2366 entry = TAILQ_PREV(s->search_entry,
2367 commit_queue_head, entry);
2368 } else if (s->matched_entry) {
2369 if (view->searching == TOG_SEARCH_FORWARD)
2370 entry = TAILQ_NEXT(s->matched_entry, entry);
2371 else
2372 entry = TAILQ_PREV(s->matched_entry,
2373 commit_queue_head, entry);
2374 } else {
2375 entry = s->selected_entry;
2378 while (1) {
2379 int have_match = 0;
2381 if (entry == NULL) {
2382 if (s->thread_args.log_complete ||
2383 view->searching == TOG_SEARCH_BACKWARD) {
2384 view->search_next_done =
2385 (s->matched_entry == NULL ?
2386 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2387 s->search_entry = NULL;
2388 return NULL;
2391 * Poke the log thread for more commits and return,
2392 * allowing the main loop to make progress. Search
2393 * will resume at s->search_entry once we come back.
2395 s->thread_args.commits_needed++;
2396 return trigger_log_thread(view, 0);
2399 err = match_commit(&have_match, entry->id, entry->commit,
2400 &view->regex);
2401 if (err)
2402 break;
2403 if (have_match) {
2404 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2405 s->matched_entry = entry;
2406 break;
2409 s->search_entry = entry;
2410 if (view->searching == TOG_SEARCH_FORWARD)
2411 entry = TAILQ_NEXT(entry, entry);
2412 else
2413 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2416 if (s->matched_entry) {
2417 int cur = s->selected_entry->idx;
2418 while (cur < s->matched_entry->idx) {
2419 err = input_log_view(NULL, view, KEY_DOWN);
2420 if (err)
2421 return err;
2422 cur++;
2424 while (cur > s->matched_entry->idx) {
2425 err = input_log_view(NULL, view, KEY_UP);
2426 if (err)
2427 return err;
2428 cur--;
2432 s->search_entry = NULL;
2434 return NULL;
2437 static const struct got_error *
2438 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2439 struct got_repository *repo, const char *head_ref_name,
2440 const char *in_repo_path, int log_branches)
2442 const struct got_error *err = NULL;
2443 struct tog_log_view_state *s = &view->state.log;
2444 struct got_repository *thread_repo = NULL;
2445 struct got_commit_graph *thread_graph = NULL;
2446 int errcode;
2448 if (in_repo_path != s->in_repo_path) {
2449 free(s->in_repo_path);
2450 s->in_repo_path = strdup(in_repo_path);
2451 if (s->in_repo_path == NULL)
2452 return got_error_from_errno("strdup");
2455 /* The commit queue only contains commits being displayed. */
2456 TAILQ_INIT(&s->commits.head);
2457 s->commits.ncommits = 0;
2459 s->repo = repo;
2460 if (head_ref_name) {
2461 s->head_ref_name = strdup(head_ref_name);
2462 if (s->head_ref_name == NULL) {
2463 err = got_error_from_errno("strdup");
2464 goto done;
2467 s->start_id = got_object_id_dup(start_id);
2468 if (s->start_id == NULL) {
2469 err = got_error_from_errno("got_object_id_dup");
2470 goto done;
2472 s->log_branches = log_branches;
2474 STAILQ_INIT(&s->colors);
2475 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2476 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2477 get_color_value("TOG_COLOR_COMMIT"));
2478 if (err)
2479 goto done;
2480 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2481 get_color_value("TOG_COLOR_AUTHOR"));
2482 if (err) {
2483 free_colors(&s->colors);
2484 goto done;
2486 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2487 get_color_value("TOG_COLOR_DATE"));
2488 if (err) {
2489 free_colors(&s->colors);
2490 goto done;
2494 view->show = show_log_view;
2495 view->input = input_log_view;
2496 view->close = close_log_view;
2497 view->search_start = search_start_log_view;
2498 view->search_next = search_next_log_view;
2500 if (s->thread_args.pack_fds == NULL) {
2501 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2502 if (err)
2503 goto done;
2505 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2506 s->thread_args.pack_fds);
2507 if (err)
2508 goto done;
2509 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2510 !s->log_branches);
2511 if (err)
2512 goto done;
2513 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2514 s->repo, NULL, NULL);
2515 if (err)
2516 goto done;
2518 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2519 if (errcode) {
2520 err = got_error_set_errno(errcode, "pthread_cond_init");
2521 goto done;
2523 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2524 if (errcode) {
2525 err = got_error_set_errno(errcode, "pthread_cond_init");
2526 goto done;
2529 s->thread_args.commits_needed = view->nlines;
2530 s->thread_args.graph = thread_graph;
2531 s->thread_args.commits = &s->commits;
2532 s->thread_args.in_repo_path = s->in_repo_path;
2533 s->thread_args.start_id = s->start_id;
2534 s->thread_args.repo = thread_repo;
2535 s->thread_args.log_complete = 0;
2536 s->thread_args.quit = &s->quit;
2537 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2538 s->thread_args.selected_entry = &s->selected_entry;
2539 s->thread_args.searching = &view->searching;
2540 s->thread_args.search_next_done = &view->search_next_done;
2541 s->thread_args.regex = &view->regex;
2542 done:
2543 if (err)
2544 close_log_view(view);
2545 return err;
2548 static const struct got_error *
2549 show_log_view(struct tog_view *view)
2551 const struct got_error *err;
2552 struct tog_log_view_state *s = &view->state.log;
2554 if (s->thread == 0) { //NULL) {
2555 int errcode = pthread_create(&s->thread, NULL, log_thread,
2556 &s->thread_args);
2557 if (errcode)
2558 return got_error_set_errno(errcode, "pthread_create");
2559 if (s->thread_args.commits_needed > 0) {
2560 err = trigger_log_thread(view, 1);
2561 if (err)
2562 return err;
2566 return draw_commits(view);
2569 static const struct got_error *
2570 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2572 const struct got_error *err = NULL;
2573 struct tog_log_view_state *s = &view->state.log;
2574 struct tog_view *diff_view = NULL, *tree_view = NULL;
2575 struct tog_view *ref_view = NULL;
2576 struct commit_queue_entry *entry;
2577 int begin_x = 0, n, nscroll = view->nlines - 1;
2579 if (s->thread_args.load_all) {
2580 if (ch == KEY_BACKSPACE)
2581 s->thread_args.load_all = 0;
2582 else if (s->thread_args.log_complete) {
2583 s->thread_args.load_all = 0;
2584 log_scroll_down(view, s->commits.ncommits);
2585 s->selected = MIN(view->nlines - 2,
2586 s->commits.ncommits - 1);
2587 select_commit(s);
2589 return NULL;
2592 switch (ch) {
2593 case 'q':
2594 s->quit = 1;
2595 break;
2596 case '0':
2597 view->x = 0;
2598 break;
2599 case '$':
2600 view->x = MAX(view->maxx - view->ncols / 2, 0);
2601 break;
2602 case KEY_RIGHT:
2603 case 'l':
2604 if (view->x + view->ncols / 2 < view->maxx)
2605 view->x += 2; /* move two columns right */
2606 break;
2607 case KEY_LEFT:
2608 case 'h':
2609 view->x -= MIN(view->x, 2); /* move two columns back */
2610 break;
2611 case 'k':
2612 case KEY_UP:
2613 case '<':
2614 case ',':
2615 case CTRL('p'):
2616 if (s->first_displayed_entry == NULL)
2617 break;
2618 if (s->selected > 0)
2619 s->selected--;
2620 else
2621 log_scroll_up(s, 1);
2622 select_commit(s);
2623 break;
2624 case 'g':
2625 case KEY_HOME:
2626 s->selected = 0;
2627 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2628 select_commit(s);
2629 break;
2630 case CTRL('u'):
2631 case 'u':
2632 nscroll /= 2;
2633 /* FALL THROUGH */
2634 case KEY_PPAGE:
2635 case CTRL('b'):
2636 if (s->first_displayed_entry == NULL)
2637 break;
2638 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2639 s->selected = MAX(0, s->selected - nscroll - 1);
2640 else
2641 log_scroll_up(s, nscroll);
2642 select_commit(s);
2643 break;
2644 case 'j':
2645 case KEY_DOWN:
2646 case '>':
2647 case '.':
2648 case CTRL('n'):
2649 if (s->first_displayed_entry == NULL)
2650 break;
2651 if (s->selected < MIN(view->nlines - 2,
2652 s->commits.ncommits - 1))
2653 s->selected++;
2654 else {
2655 err = log_scroll_down(view, 1);
2656 if (err)
2657 break;
2659 select_commit(s);
2660 break;
2661 case 'G':
2662 case KEY_END: {
2663 /* We don't know yet how many commits, so we're forced to
2664 * traverse them all. */
2665 if (!s->thread_args.log_complete) {
2666 s->thread_args.load_all = 1;
2667 return trigger_log_thread(view, 0);
2670 s->selected = 0;
2671 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2672 for (n = 0; n < view->nlines - 1; n++) {
2673 if (entry == NULL)
2674 break;
2675 s->first_displayed_entry = entry;
2676 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2678 if (n > 0)
2679 s->selected = n - 1;
2680 select_commit(s);
2681 break;
2683 case CTRL('d'):
2684 case 'd':
2685 nscroll /= 2;
2686 /* FALL THROUGH */
2687 case KEY_NPAGE:
2688 case CTRL('f'): {
2689 struct commit_queue_entry *first;
2690 first = s->first_displayed_entry;
2691 if (first == NULL)
2692 break;
2693 err = log_scroll_down(view, nscroll);
2694 if (err)
2695 break;
2696 if (first == s->first_displayed_entry &&
2697 s->selected < MIN(view->nlines - 2,
2698 s->commits.ncommits - 1)) {
2699 /* can't scroll further down */
2700 s->selected += MIN(s->last_displayed_entry->idx -
2701 s->selected_entry->idx, nscroll + 1);
2703 select_commit(s);
2704 break;
2706 case KEY_RESIZE:
2707 if (s->selected > view->nlines - 2)
2708 s->selected = view->nlines - 2;
2709 if (s->selected > s->commits.ncommits - 1)
2710 s->selected = s->commits.ncommits - 1;
2711 select_commit(s);
2712 if (s->commits.ncommits < view->nlines - 1 &&
2713 !s->thread_args.log_complete) {
2714 s->thread_args.commits_needed += (view->nlines - 1) -
2715 s->commits.ncommits;
2716 err = trigger_log_thread(view, 1);
2718 break;
2719 case KEY_ENTER:
2720 case ' ':
2721 case '\r':
2722 if (s->selected_entry == NULL)
2723 break;
2724 if (view_is_parent_view(view))
2725 begin_x = view_split_begin_x(view->begin_x);
2726 err = open_diff_view_for_commit(&diff_view, begin_x,
2727 s->selected_entry->commit, s->selected_entry->id,
2728 view, s->repo);
2729 if (err)
2730 break;
2731 view->focussed = 0;
2732 diff_view->focussed = 1;
2733 if (view_is_parent_view(view)) {
2734 err = view_close_child(view);
2735 if (err)
2736 return err;
2737 view_set_child(view, diff_view);
2738 view->focus_child = 1;
2739 } else
2740 *new_view = diff_view;
2741 break;
2742 case 't':
2743 if (s->selected_entry == NULL)
2744 break;
2745 if (view_is_parent_view(view))
2746 begin_x = view_split_begin_x(view->begin_x);
2747 err = browse_commit_tree(&tree_view, begin_x,
2748 s->selected_entry, s->in_repo_path, s->head_ref_name,
2749 s->repo);
2750 if (err)
2751 break;
2752 view->focussed = 0;
2753 tree_view->focussed = 1;
2754 if (view_is_parent_view(view)) {
2755 err = view_close_child(view);
2756 if (err)
2757 return err;
2758 view_set_child(view, tree_view);
2759 view->focus_child = 1;
2760 } else
2761 *new_view = tree_view;
2762 break;
2763 case KEY_BACKSPACE:
2764 case CTRL('l'):
2765 case 'B':
2766 if (ch == KEY_BACKSPACE &&
2767 got_path_is_root_dir(s->in_repo_path))
2768 break;
2769 err = stop_log_thread(s);
2770 if (err)
2771 return err;
2772 if (ch == KEY_BACKSPACE) {
2773 char *parent_path;
2774 err = got_path_dirname(&parent_path, s->in_repo_path);
2775 if (err)
2776 return err;
2777 free(s->in_repo_path);
2778 s->in_repo_path = parent_path;
2779 s->thread_args.in_repo_path = s->in_repo_path;
2780 } else if (ch == CTRL('l')) {
2781 struct got_object_id *start_id;
2782 err = got_repo_match_object_id(&start_id, NULL,
2783 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2784 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2785 if (err)
2786 return err;
2787 free(s->start_id);
2788 s->start_id = start_id;
2789 s->thread_args.start_id = s->start_id;
2790 } else /* 'B' */
2791 s->log_branches = !s->log_branches;
2793 err = got_repo_open(&s->thread_args.repo,
2794 got_repo_get_path(s->repo), NULL,
2795 s->thread_args.pack_fds);
2796 if (err)
2797 return err;
2798 tog_free_refs();
2799 err = tog_load_refs(s->repo, 0);
2800 if (err)
2801 return err;
2802 err = got_commit_graph_open(&s->thread_args.graph,
2803 s->in_repo_path, !s->log_branches);
2804 if (err)
2805 return err;
2806 err = got_commit_graph_iter_start(s->thread_args.graph,
2807 s->start_id, s->repo, NULL, NULL);
2808 if (err)
2809 return err;
2810 free_commits(&s->commits);
2811 s->first_displayed_entry = NULL;
2812 s->last_displayed_entry = NULL;
2813 s->selected_entry = NULL;
2814 s->selected = 0;
2815 s->thread_args.log_complete = 0;
2816 s->quit = 0;
2817 s->thread_args.commits_needed = view->nlines;
2818 break;
2819 case 'r':
2820 if (view_is_parent_view(view))
2821 begin_x = view_split_begin_x(view->begin_x);
2822 ref_view = view_open(view->nlines, view->ncols,
2823 view->begin_y, begin_x, TOG_VIEW_REF);
2824 if (ref_view == NULL)
2825 return got_error_from_errno("view_open");
2826 err = open_ref_view(ref_view, s->repo);
2827 if (err) {
2828 view_close(ref_view);
2829 return err;
2831 view->focussed = 0;
2832 ref_view->focussed = 1;
2833 if (view_is_parent_view(view)) {
2834 err = view_close_child(view);
2835 if (err)
2836 return err;
2837 view_set_child(view, ref_view);
2838 view->focus_child = 1;
2839 } else
2840 *new_view = ref_view;
2841 break;
2842 default:
2843 break;
2846 return err;
2849 static const struct got_error *
2850 apply_unveil(const char *repo_path, const char *worktree_path)
2852 const struct got_error *error;
2854 #ifdef PROFILE
2855 if (unveil("gmon.out", "rwc") != 0)
2856 return got_error_from_errno2("unveil", "gmon.out");
2857 #endif
2858 if (repo_path && unveil(repo_path, "r") != 0)
2859 return got_error_from_errno2("unveil", repo_path);
2861 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2862 return got_error_from_errno2("unveil", worktree_path);
2864 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2865 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2867 error = got_privsep_unveil_exec_helpers();
2868 if (error != NULL)
2869 return error;
2871 if (unveil(NULL, NULL) != 0)
2872 return got_error_from_errno("unveil");
2874 return NULL;
2877 static void
2878 init_curses(void)
2881 * Override default signal handlers before starting ncurses.
2882 * This should prevent ncurses from installing its own
2883 * broken cleanup() signal handler.
2885 signal(SIGWINCH, tog_sigwinch);
2886 signal(SIGPIPE, tog_sigpipe);
2887 signal(SIGCONT, tog_sigcont);
2888 signal(SIGINT, tog_sigint);
2889 signal(SIGTERM, tog_sigterm);
2891 initscr();
2892 cbreak();
2893 halfdelay(1); /* Do fast refresh while initial view is loading. */
2894 noecho();
2895 nonl();
2896 intrflush(stdscr, FALSE);
2897 keypad(stdscr, TRUE);
2898 curs_set(0);
2899 if (getenv("TOG_COLORS") != NULL) {
2900 start_color();
2901 use_default_colors();
2905 static const struct got_error *
2906 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2907 struct got_repository *repo, struct got_worktree *worktree)
2909 const struct got_error *err = NULL;
2911 if (argc == 0) {
2912 *in_repo_path = strdup("/");
2913 if (*in_repo_path == NULL)
2914 return got_error_from_errno("strdup");
2915 return NULL;
2918 if (worktree) {
2919 const char *prefix = got_worktree_get_path_prefix(worktree);
2920 char *p;
2922 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2923 if (err)
2924 return err;
2925 if (asprintf(in_repo_path, "%s%s%s", prefix,
2926 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2927 p) == -1) {
2928 err = got_error_from_errno("asprintf");
2929 *in_repo_path = NULL;
2931 free(p);
2932 } else
2933 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2935 return err;
2938 static const struct got_error *
2939 cmd_log(int argc, char *argv[])
2941 const struct got_error *error;
2942 struct got_repository *repo = NULL;
2943 struct got_worktree *worktree = NULL;
2944 struct got_object_id *start_id = NULL;
2945 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2946 char *start_commit = NULL, *label = NULL;
2947 struct got_reference *ref = NULL;
2948 const char *head_ref_name = NULL;
2949 int ch, log_branches = 0;
2950 struct tog_view *view;
2951 int *pack_fds = NULL;
2953 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2954 switch (ch) {
2955 case 'b':
2956 log_branches = 1;
2957 break;
2958 case 'c':
2959 start_commit = optarg;
2960 break;
2961 case 'r':
2962 repo_path = realpath(optarg, NULL);
2963 if (repo_path == NULL)
2964 return got_error_from_errno2("realpath",
2965 optarg);
2966 break;
2967 default:
2968 usage_log();
2969 /* NOTREACHED */
2973 argc -= optind;
2974 argv += optind;
2976 if (argc > 1)
2977 usage_log();
2979 error = got_repo_pack_fds_open(&pack_fds);
2980 if (error != NULL)
2981 goto done;
2983 if (repo_path == NULL) {
2984 cwd = getcwd(NULL, 0);
2985 if (cwd == NULL)
2986 return got_error_from_errno("getcwd");
2987 error = got_worktree_open(&worktree, cwd);
2988 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2989 goto done;
2990 if (worktree)
2991 repo_path =
2992 strdup(got_worktree_get_repo_path(worktree));
2993 else
2994 repo_path = strdup(cwd);
2995 if (repo_path == NULL) {
2996 error = got_error_from_errno("strdup");
2997 goto done;
3001 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3002 if (error != NULL)
3003 goto done;
3005 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3006 repo, worktree);
3007 if (error)
3008 goto done;
3010 init_curses();
3012 error = apply_unveil(got_repo_get_path(repo),
3013 worktree ? got_worktree_get_root_path(worktree) : NULL);
3014 if (error)
3015 goto done;
3017 /* already loaded by tog_log_with_path()? */
3018 if (TAILQ_EMPTY(&tog_refs)) {
3019 error = tog_load_refs(repo, 0);
3020 if (error)
3021 goto done;
3024 if (start_commit == NULL) {
3025 error = got_repo_match_object_id(&start_id, &label,
3026 worktree ? got_worktree_get_head_ref_name(worktree) :
3027 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3028 if (error)
3029 goto done;
3030 head_ref_name = label;
3031 } else {
3032 error = got_ref_open(&ref, repo, start_commit, 0);
3033 if (error == NULL)
3034 head_ref_name = got_ref_get_name(ref);
3035 else if (error->code != GOT_ERR_NOT_REF)
3036 goto done;
3037 error = got_repo_match_object_id(&start_id, NULL,
3038 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3039 if (error)
3040 goto done;
3043 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3044 if (view == NULL) {
3045 error = got_error_from_errno("view_open");
3046 goto done;
3048 error = open_log_view(view, start_id, repo, head_ref_name,
3049 in_repo_path, log_branches);
3050 if (error)
3051 goto done;
3052 if (worktree) {
3053 /* Release work tree lock. */
3054 got_worktree_close(worktree);
3055 worktree = NULL;
3057 error = view_loop(view);
3058 done:
3059 free(in_repo_path);
3060 free(repo_path);
3061 free(cwd);
3062 free(start_id);
3063 free(label);
3064 if (ref)
3065 got_ref_close(ref);
3066 if (repo) {
3067 const struct got_error *close_err = got_repo_close(repo);
3068 if (error == NULL)
3069 error = close_err;
3071 if (worktree)
3072 got_worktree_close(worktree);
3073 if (pack_fds) {
3074 const struct got_error *pack_err =
3075 got_repo_pack_fds_close(pack_fds);
3076 if (error == NULL)
3077 error = pack_err;
3079 tog_free_refs();
3080 return error;
3083 __dead static void
3084 usage_diff(void)
3086 endwin();
3087 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3088 "[-w] object1 object2\n", getprogname());
3089 exit(1);
3092 static int
3093 match_line(const char *line, regex_t *regex, size_t nmatch,
3094 regmatch_t *regmatch)
3096 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3099 struct tog_color *
3100 match_color(struct tog_colors *colors, const char *line)
3102 struct tog_color *tc = NULL;
3104 STAILQ_FOREACH(tc, colors, entry) {
3105 if (match_line(line, &tc->regex, 0, NULL))
3106 return tc;
3109 return NULL;
3112 static const struct got_error *
3113 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3114 WINDOW *window, int skip, regmatch_t *regmatch)
3116 const struct got_error *err = NULL;
3117 wchar_t *wline;
3118 int rme, rms, n, width;
3120 *wtotal = 0;
3121 rms = regmatch->rm_so;
3122 rme = regmatch->rm_eo;
3124 err = format_line(&wline, &width, line, wlimit + skip,
3125 col_tab_align, 1);
3126 if (err)
3127 return err;
3129 /* draw up to matched token if we haven't scrolled past it */
3130 n = MAX(rms - skip, 0);
3131 if (n) {
3132 waddnwstr(window, wline + skip, n);
3133 wlimit -= n;
3134 *wtotal += n;
3137 if (wlimit > 0) {
3138 int len = rme - rms;
3139 n = 0;
3140 if (skip > rms) {
3141 n = skip - rms;
3142 len = MAX(len - n, 0);
3144 /* draw (visible part of) matched token (if scrolled into it) */
3145 if (len) {
3146 wattron(window, A_STANDOUT);
3147 waddnwstr(window, wline + rms + n, len);
3148 wattroff(window, A_STANDOUT);
3149 wlimit -= len;
3150 *wtotal += len;
3154 if (wlimit > 0 && skip < width) { /* draw rest of line */
3155 n = 0;
3156 if (skip > rme)
3157 n = MIN(skip - rme, width - rme);
3158 waddnwstr(window, wline + rme + n, wlimit);
3161 *wtotal = width;
3162 free(wline);
3163 return NULL;
3166 static const struct got_error *
3167 draw_file(struct tog_view *view, const char *header)
3169 struct tog_diff_view_state *s = &view->state.diff;
3170 regmatch_t *regmatch = &view->regmatch;
3171 const struct got_error *err;
3172 int nprinted = 0;
3173 char *line;
3174 size_t linesize = 0;
3175 ssize_t linelen;
3176 struct tog_color *tc;
3177 wchar_t *wline;
3178 int width;
3179 int max_lines = view->nlines;
3180 int nlines = s->nlines;
3181 off_t line_offset;
3183 line_offset = s->line_offsets[s->first_displayed_line - 1];
3184 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3185 return got_error_from_errno("fseek");
3187 werase(view->window);
3189 if (header) {
3190 if (asprintf(&line, "[%d/%d] %s",
3191 s->first_displayed_line - 1 + s->selected_line, nlines,
3192 header) == -1)
3193 return got_error_from_errno("asprintf");
3194 err = format_line(&wline, &width, line, view->ncols, 0, 0);
3195 free(line);
3196 if (err)
3197 return err;
3199 if (view_needs_focus_indication(view))
3200 wstandout(view->window);
3201 waddwstr(view->window, wline);
3202 free(wline);
3203 wline = NULL;
3204 if (view_needs_focus_indication(view))
3205 wstandend(view->window);
3206 if (width <= view->ncols - 1)
3207 waddch(view->window, '\n');
3209 if (max_lines <= 1)
3210 return NULL;
3211 max_lines--;
3214 s->eof = 0;
3215 view->maxx = 0;
3216 line = NULL;
3217 while (max_lines > 0 && nprinted < max_lines) {
3218 linelen = getline(&line, &linesize, s->f);
3219 if (linelen == -1) {
3220 if (feof(s->f)) {
3221 s->eof = 1;
3222 break;
3224 free(line);
3225 return got_ferror(s->f, GOT_ERR_IO);
3228 view->maxx = MAX(view->maxx, linelen);
3230 tc = match_color(&s->colors, line);
3231 if (tc)
3232 wattr_on(view->window,
3233 COLOR_PAIR(tc->colorpair), NULL);
3234 if (s->first_displayed_line + nprinted == s->matched_line &&
3235 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3236 err = add_matched_line(&width, line, view->ncols, 0,
3237 view->window, view->x, regmatch);
3238 if (err) {
3239 free(line);
3240 return err;
3242 } else {
3243 err = format_line(&wline, &width, line,
3244 view->x + view->ncols, 0, view->x ? 1 : 0);
3245 if (err) {
3246 free(line);
3247 return err;
3249 if (view->x < width - 1)
3250 waddwstr(view->window, wline + view->x);
3251 free(wline);
3252 wline = NULL;
3254 if (tc)
3255 wattr_off(view->window,
3256 COLOR_PAIR(tc->colorpair), NULL);
3257 if (width - view->x <= view->ncols - 1)
3258 waddch(view->window, '\n');
3259 nprinted++;
3261 free(line);
3262 if (nprinted >= 1)
3263 s->last_displayed_line = s->first_displayed_line +
3264 (nprinted - 1);
3265 else
3266 s->last_displayed_line = s->first_displayed_line;
3268 view_vborder(view);
3270 if (s->eof) {
3271 while (nprinted < view->nlines) {
3272 waddch(view->window, '\n');
3273 nprinted++;
3276 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols,
3277 0, 0);
3278 if (err) {
3279 return err;
3282 wstandout(view->window);
3283 waddwstr(view->window, wline);
3284 free(wline);
3285 wline = NULL;
3286 wstandend(view->window);
3289 return NULL;
3292 static char *
3293 get_datestr(time_t *time, char *datebuf)
3295 struct tm mytm, *tm;
3296 char *p, *s;
3298 tm = gmtime_r(time, &mytm);
3299 if (tm == NULL)
3300 return NULL;
3301 s = asctime_r(tm, datebuf);
3302 if (s == NULL)
3303 return NULL;
3304 p = strchr(s, '\n');
3305 if (p)
3306 *p = '\0';
3307 return s;
3310 static const struct got_error *
3311 get_changed_paths(struct got_pathlist_head *paths,
3312 struct got_commit_object *commit, struct got_repository *repo)
3314 const struct got_error *err = NULL;
3315 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3316 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3317 struct got_object_qid *qid;
3319 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3320 if (qid != NULL) {
3321 struct got_commit_object *pcommit;
3322 err = got_object_open_as_commit(&pcommit, repo,
3323 &qid->id);
3324 if (err)
3325 return err;
3327 tree_id1 = got_object_id_dup(
3328 got_object_commit_get_tree_id(pcommit));
3329 if (tree_id1 == NULL) {
3330 got_object_commit_close(pcommit);
3331 return got_error_from_errno("got_object_id_dup");
3333 got_object_commit_close(pcommit);
3337 if (tree_id1) {
3338 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3339 if (err)
3340 goto done;
3343 tree_id2 = got_object_commit_get_tree_id(commit);
3344 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3345 if (err)
3346 goto done;
3348 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3349 got_diff_tree_collect_changed_paths, paths, 0);
3350 done:
3351 if (tree1)
3352 got_object_tree_close(tree1);
3353 if (tree2)
3354 got_object_tree_close(tree2);
3355 free(tree_id1);
3356 return err;
3359 static const struct got_error *
3360 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3362 off_t *p;
3364 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3365 if (p == NULL)
3366 return got_error_from_errno("reallocarray");
3367 *line_offsets = p;
3368 (*line_offsets)[*nlines] = off;
3369 (*nlines)++;
3370 return NULL;
3373 static const struct got_error *
3374 write_commit_info(off_t **line_offsets, size_t *nlines,
3375 struct got_object_id *commit_id, struct got_reflist_head *refs,
3376 struct got_repository *repo, FILE *outfile)
3378 const struct got_error *err = NULL;
3379 char datebuf[26], *datestr;
3380 struct got_commit_object *commit;
3381 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3382 time_t committer_time;
3383 const char *author, *committer;
3384 char *refs_str = NULL;
3385 struct got_pathlist_head changed_paths;
3386 struct got_pathlist_entry *pe;
3387 off_t outoff = 0;
3388 int n;
3390 TAILQ_INIT(&changed_paths);
3392 if (refs) {
3393 err = build_refs_str(&refs_str, refs, commit_id, repo);
3394 if (err)
3395 return err;
3398 err = got_object_open_as_commit(&commit, repo, commit_id);
3399 if (err)
3400 return err;
3402 err = got_object_id_str(&id_str, commit_id);
3403 if (err) {
3404 err = got_error_from_errno("got_object_id_str");
3405 goto done;
3408 err = add_line_offset(line_offsets, nlines, 0);
3409 if (err)
3410 goto done;
3412 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3413 refs_str ? refs_str : "", refs_str ? ")" : "");
3414 if (n < 0) {
3415 err = got_error_from_errno("fprintf");
3416 goto done;
3418 outoff += n;
3419 err = add_line_offset(line_offsets, nlines, outoff);
3420 if (err)
3421 goto done;
3423 n = fprintf(outfile, "from: %s\n",
3424 got_object_commit_get_author(commit));
3425 if (n < 0) {
3426 err = got_error_from_errno("fprintf");
3427 goto done;
3429 outoff += n;
3430 err = add_line_offset(line_offsets, nlines, outoff);
3431 if (err)
3432 goto done;
3434 committer_time = got_object_commit_get_committer_time(commit);
3435 datestr = get_datestr(&committer_time, datebuf);
3436 if (datestr) {
3437 n = fprintf(outfile, "date: %s UTC\n", datestr);
3438 if (n < 0) {
3439 err = got_error_from_errno("fprintf");
3440 goto done;
3442 outoff += n;
3443 err = add_line_offset(line_offsets, nlines, outoff);
3444 if (err)
3445 goto done;
3447 author = got_object_commit_get_author(commit);
3448 committer = got_object_commit_get_committer(commit);
3449 if (strcmp(author, committer) != 0) {
3450 n = fprintf(outfile, "via: %s\n", committer);
3451 if (n < 0) {
3452 err = got_error_from_errno("fprintf");
3453 goto done;
3455 outoff += n;
3456 err = add_line_offset(line_offsets, nlines, outoff);
3457 if (err)
3458 goto done;
3460 if (got_object_commit_get_nparents(commit) > 1) {
3461 const struct got_object_id_queue *parent_ids;
3462 struct got_object_qid *qid;
3463 int pn = 1;
3464 parent_ids = got_object_commit_get_parent_ids(commit);
3465 STAILQ_FOREACH(qid, parent_ids, entry) {
3466 err = got_object_id_str(&id_str, &qid->id);
3467 if (err)
3468 goto done;
3469 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3470 if (n < 0) {
3471 err = got_error_from_errno("fprintf");
3472 goto done;
3474 outoff += n;
3475 err = add_line_offset(line_offsets, nlines, outoff);
3476 if (err)
3477 goto done;
3478 free(id_str);
3479 id_str = NULL;
3483 err = got_object_commit_get_logmsg(&logmsg, commit);
3484 if (err)
3485 goto done;
3486 s = logmsg;
3487 while ((line = strsep(&s, "\n")) != NULL) {
3488 n = fprintf(outfile, "%s\n", line);
3489 if (n < 0) {
3490 err = got_error_from_errno("fprintf");
3491 goto done;
3493 outoff += n;
3494 err = add_line_offset(line_offsets, nlines, outoff);
3495 if (err)
3496 goto done;
3499 err = get_changed_paths(&changed_paths, commit, repo);
3500 if (err)
3501 goto done;
3502 TAILQ_FOREACH(pe, &changed_paths, entry) {
3503 struct got_diff_changed_path *cp = pe->data;
3504 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3505 if (n < 0) {
3506 err = got_error_from_errno("fprintf");
3507 goto done;
3509 outoff += n;
3510 err = add_line_offset(line_offsets, nlines, outoff);
3511 if (err)
3512 goto done;
3513 free((char *)pe->path);
3514 free(pe->data);
3517 fputc('\n', outfile);
3518 outoff++;
3519 err = add_line_offset(line_offsets, nlines, outoff);
3520 done:
3521 got_pathlist_free(&changed_paths);
3522 free(id_str);
3523 free(logmsg);
3524 free(refs_str);
3525 got_object_commit_close(commit);
3526 if (err) {
3527 free(*line_offsets);
3528 *line_offsets = NULL;
3529 *nlines = 0;
3531 return err;
3534 static const struct got_error *
3535 create_diff(struct tog_diff_view_state *s)
3537 const struct got_error *err = NULL;
3538 FILE *f = NULL;
3539 int obj_type;
3541 free(s->line_offsets);
3542 s->line_offsets = malloc(sizeof(off_t));
3543 if (s->line_offsets == NULL)
3544 return got_error_from_errno("malloc");
3545 s->nlines = 0;
3547 f = got_opentemp();
3548 if (f == NULL) {
3549 err = got_error_from_errno("got_opentemp");
3550 goto done;
3552 if (s->f && fclose(s->f) == EOF) {
3553 err = got_error_from_errno("fclose");
3554 goto done;
3556 s->f = f;
3558 if (s->id1)
3559 err = got_object_get_type(&obj_type, s->repo, s->id1);
3560 else
3561 err = got_object_get_type(&obj_type, s->repo, s->id2);
3562 if (err)
3563 goto done;
3565 switch (obj_type) {
3566 case GOT_OBJ_TYPE_BLOB:
3567 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3568 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3569 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3570 s->repo, s->f);
3571 break;
3572 case GOT_OBJ_TYPE_TREE:
3573 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3574 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3575 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3576 break;
3577 case GOT_OBJ_TYPE_COMMIT: {
3578 const struct got_object_id_queue *parent_ids;
3579 struct got_object_qid *pid;
3580 struct got_commit_object *commit2;
3581 struct got_reflist_head *refs;
3583 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3584 if (err)
3585 goto done;
3586 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3587 /* Show commit info if we're diffing to a parent/root commit. */
3588 if (s->id1 == NULL) {
3589 err = write_commit_info(&s->line_offsets, &s->nlines,
3590 s->id2, refs, s->repo, s->f);
3591 if (err)
3592 goto done;
3593 } else {
3594 parent_ids = got_object_commit_get_parent_ids(commit2);
3595 STAILQ_FOREACH(pid, parent_ids, entry) {
3596 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3597 err = write_commit_info(
3598 &s->line_offsets, &s->nlines,
3599 s->id2, refs, s->repo, s->f);
3600 if (err)
3601 goto done;
3602 break;
3606 got_object_commit_close(commit2);
3608 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3609 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3610 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3611 break;
3613 default:
3614 err = got_error(GOT_ERR_OBJ_TYPE);
3615 break;
3617 if (err)
3618 goto done;
3619 done:
3620 if (s->f && fflush(s->f) != 0 && err == NULL)
3621 err = got_error_from_errno("fflush");
3622 return err;
3625 static void
3626 diff_view_indicate_progress(struct tog_view *view)
3628 mvwaddstr(view->window, 0, 0, "diffing...");
3629 update_panels();
3630 doupdate();
3633 static const struct got_error *
3634 search_start_diff_view(struct tog_view *view)
3636 struct tog_diff_view_state *s = &view->state.diff;
3638 s->matched_line = 0;
3639 return NULL;
3642 static const struct got_error *
3643 search_next_diff_view(struct tog_view *view)
3645 struct tog_diff_view_state *s = &view->state.diff;
3646 const struct got_error *err = NULL;
3647 int lineno;
3648 char *exstr = NULL, *line = NULL;
3649 size_t linesize = 0;
3650 ssize_t linelen;
3652 if (!view->searching) {
3653 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3654 return NULL;
3657 if (s->matched_line) {
3658 if (view->searching == TOG_SEARCH_FORWARD)
3659 lineno = s->matched_line + 1;
3660 else
3661 lineno = s->matched_line - 1;
3662 } else
3663 lineno = s->first_displayed_line;
3665 while (1) {
3666 off_t offset;
3668 if (lineno <= 0 || lineno > s->nlines) {
3669 if (s->matched_line == 0) {
3670 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3671 break;
3674 if (view->searching == TOG_SEARCH_FORWARD)
3675 lineno = 1;
3676 else
3677 lineno = s->nlines;
3680 offset = s->line_offsets[lineno - 1];
3681 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3682 free(line);
3683 return got_error_from_errno("fseeko");
3685 linelen = getline(&line, &linesize, s->f);
3686 err = expand_tab(&exstr, line);
3687 if (err)
3688 break;
3689 if (linelen != -1 &&
3690 match_line(exstr, &view->regex, 1, &view->regmatch)) {
3691 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3692 s->matched_line = lineno;
3693 break;
3695 free(exstr);
3696 exstr = NULL;
3697 if (view->searching == TOG_SEARCH_FORWARD)
3698 lineno++;
3699 else
3700 lineno--;
3702 free(line);
3703 free(exstr);
3705 if (s->matched_line) {
3706 s->first_displayed_line = s->matched_line;
3707 s->selected_line = 1;
3710 return err;
3713 static const struct got_error *
3714 close_diff_view(struct tog_view *view)
3716 const struct got_error *err = NULL;
3717 struct tog_diff_view_state *s = &view->state.diff;
3719 free(s->id1);
3720 s->id1 = NULL;
3721 free(s->id2);
3722 s->id2 = NULL;
3723 if (s->f && fclose(s->f) == EOF)
3724 err = got_error_from_errno("fclose");
3725 s->f = NULL;
3726 if (s->f1 && fclose(s->f1) == EOF)
3727 err = got_error_from_errno("fclose");
3728 s->f1 = NULL;
3729 if (s->f2 && fclose(s->f2) == EOF)
3730 err = got_error_from_errno("fclose");
3731 s->f2 = NULL;
3732 free_colors(&s->colors);
3733 free(s->line_offsets);
3734 s->line_offsets = NULL;
3735 s->nlines = 0;
3736 return err;
3739 static const struct got_error *
3740 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3741 struct got_object_id *id2, const char *label1, const char *label2,
3742 int diff_context, int ignore_whitespace, int force_text_diff,
3743 struct tog_view *log_view, struct got_repository *repo)
3745 const struct got_error *err;
3746 struct tog_diff_view_state *s = &view->state.diff;
3748 memset(s, 0, sizeof(*s));
3750 if (id1 != NULL && id2 != NULL) {
3751 int type1, type2;
3752 err = got_object_get_type(&type1, repo, id1);
3753 if (err)
3754 return err;
3755 err = got_object_get_type(&type2, repo, id2);
3756 if (err)
3757 return err;
3759 if (type1 != type2)
3760 return got_error(GOT_ERR_OBJ_TYPE);
3762 s->first_displayed_line = 1;
3763 s->last_displayed_line = view->nlines;
3764 s->selected_line = 1;
3765 s->repo = repo;
3766 s->id1 = id1;
3767 s->id2 = id2;
3768 s->label1 = label1;
3769 s->label2 = label2;
3771 if (id1) {
3772 s->id1 = got_object_id_dup(id1);
3773 if (s->id1 == NULL)
3774 return got_error_from_errno("got_object_id_dup");
3775 s->f1 = got_opentemp();
3776 if (s->f1 == NULL) {
3777 err = got_error_from_errno("got_opentemp");
3778 goto done;
3780 } else
3781 s->id1 = NULL;
3783 s->id2 = got_object_id_dup(id2);
3784 if (s->id2 == NULL) {
3785 err = got_error_from_errno("got_object_id_dup");
3786 goto done;
3789 s->f2 = got_opentemp();
3790 if (s->f2 == NULL) {
3791 err = got_error_from_errno("got_opentemp");
3792 goto done;
3795 s->first_displayed_line = 1;
3796 s->last_displayed_line = view->nlines;
3797 s->diff_context = diff_context;
3798 s->ignore_whitespace = ignore_whitespace;
3799 s->force_text_diff = force_text_diff;
3800 s->log_view = log_view;
3801 s->repo = repo;
3803 STAILQ_INIT(&s->colors);
3804 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3805 err = add_color(&s->colors,
3806 "^-", TOG_COLOR_DIFF_MINUS,
3807 get_color_value("TOG_COLOR_DIFF_MINUS"));
3808 if (err)
3809 goto done;
3810 err = add_color(&s->colors, "^\\+",
3811 TOG_COLOR_DIFF_PLUS,
3812 get_color_value("TOG_COLOR_DIFF_PLUS"));
3813 if (err)
3814 goto done;
3815 err = add_color(&s->colors,
3816 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3817 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3818 if (err)
3819 goto done;
3821 err = add_color(&s->colors,
3822 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3823 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3824 get_color_value("TOG_COLOR_DIFF_META"));
3825 if (err)
3826 goto done;
3828 err = add_color(&s->colors,
3829 "^(from|via): ", TOG_COLOR_AUTHOR,
3830 get_color_value("TOG_COLOR_AUTHOR"));
3831 if (err)
3832 goto done;
3834 err = add_color(&s->colors,
3835 "^date: ", TOG_COLOR_DATE,
3836 get_color_value("TOG_COLOR_DATE"));
3837 if (err)
3838 goto done;
3841 if (log_view && view_is_splitscreen(view))
3842 show_log_view(log_view); /* draw vborder */
3843 diff_view_indicate_progress(view);
3845 err = create_diff(s);
3847 view->show = show_diff_view;
3848 view->input = input_diff_view;
3849 view->close = close_diff_view;
3850 view->search_start = search_start_diff_view;
3851 view->search_next = search_next_diff_view;
3852 done:
3853 if (err)
3854 close_diff_view(view);
3855 return err;
3858 static const struct got_error *
3859 show_diff_view(struct tog_view *view)
3861 const struct got_error *err;
3862 struct tog_diff_view_state *s = &view->state.diff;
3863 char *id_str1 = NULL, *id_str2, *header;
3864 const char *label1, *label2;
3866 if (s->id1) {
3867 err = got_object_id_str(&id_str1, s->id1);
3868 if (err)
3869 return err;
3870 label1 = s->label1 ? : id_str1;
3871 } else
3872 label1 = "/dev/null";
3874 err = got_object_id_str(&id_str2, s->id2);
3875 if (err)
3876 return err;
3877 label2 = s->label2 ? : id_str2;
3879 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3880 err = got_error_from_errno("asprintf");
3881 free(id_str1);
3882 free(id_str2);
3883 return err;
3885 free(id_str1);
3886 free(id_str2);
3888 err = draw_file(view, header);
3889 free(header);
3890 return err;
3893 static const struct got_error *
3894 set_selected_commit(struct tog_diff_view_state *s,
3895 struct commit_queue_entry *entry)
3897 const struct got_error *err;
3898 const struct got_object_id_queue *parent_ids;
3899 struct got_commit_object *selected_commit;
3900 struct got_object_qid *pid;
3902 free(s->id2);
3903 s->id2 = got_object_id_dup(entry->id);
3904 if (s->id2 == NULL)
3905 return got_error_from_errno("got_object_id_dup");
3907 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3908 if (err)
3909 return err;
3910 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3911 free(s->id1);
3912 pid = STAILQ_FIRST(parent_ids);
3913 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3914 got_object_commit_close(selected_commit);
3915 return NULL;
3918 static const struct got_error *
3919 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3921 const struct got_error *err = NULL;
3922 struct tog_diff_view_state *s = &view->state.diff;
3923 struct tog_log_view_state *ls;
3924 struct commit_queue_entry *old_selected_entry;
3925 char *line = NULL;
3926 size_t linesize = 0;
3927 ssize_t linelen;
3928 int i, nscroll = view->nlines - 1;
3930 switch (ch) {
3931 case '0':
3932 view->x = 0;
3933 break;
3934 case '$':
3935 view->x = MAX(view->maxx - view->ncols / 3, 0);
3936 break;
3937 case KEY_RIGHT:
3938 case 'l':
3939 if (view->x + view->ncols / 3 < view->maxx)
3940 view->x += 2; /* move two columns right */
3941 break;
3942 case KEY_LEFT:
3943 case 'h':
3944 view->x -= MIN(view->x, 2); /* move two columns back */
3945 break;
3946 case 'a':
3947 case 'w':
3948 if (ch == 'a')
3949 s->force_text_diff = !s->force_text_diff;
3950 if (ch == 'w')
3951 s->ignore_whitespace = !s->ignore_whitespace;
3952 wclear(view->window);
3953 s->first_displayed_line = 1;
3954 s->last_displayed_line = view->nlines;
3955 s->matched_line = 0;
3956 diff_view_indicate_progress(view);
3957 err = create_diff(s);
3958 break;
3959 case 'g':
3960 case KEY_HOME:
3961 s->first_displayed_line = 1;
3962 break;
3963 case 'G':
3964 case KEY_END:
3965 if (s->eof)
3966 break;
3968 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3969 s->eof = 1;
3970 break;
3971 case 'k':
3972 case KEY_UP:
3973 case CTRL('p'):
3974 if (s->first_displayed_line > 1)
3975 s->first_displayed_line--;
3976 break;
3977 case CTRL('u'):
3978 case 'u':
3979 nscroll /= 2;
3980 /* FALL THROUGH */
3981 case KEY_PPAGE:
3982 case CTRL('b'):
3983 if (s->first_displayed_line == 1)
3984 break;
3985 i = 0;
3986 while (i++ < nscroll && s->first_displayed_line > 1)
3987 s->first_displayed_line--;
3988 break;
3989 case 'j':
3990 case KEY_DOWN:
3991 case CTRL('n'):
3992 if (!s->eof)
3993 s->first_displayed_line++;
3994 break;
3995 case CTRL('d'):
3996 case 'd':
3997 nscroll /= 2;
3998 /* FALL THROUGH */
3999 case KEY_NPAGE:
4000 case CTRL('f'):
4001 case ' ':
4002 if (s->eof)
4003 break;
4004 i = 0;
4005 while (!s->eof && i++ < nscroll) {
4006 linelen = getline(&line, &linesize, s->f);
4007 s->first_displayed_line++;
4008 if (linelen == -1) {
4009 if (feof(s->f)) {
4010 s->eof = 1;
4011 } else
4012 err = got_ferror(s->f, GOT_ERR_IO);
4013 break;
4016 free(line);
4017 break;
4018 case '[':
4019 if (s->diff_context > 0) {
4020 s->diff_context--;
4021 s->matched_line = 0;
4022 diff_view_indicate_progress(view);
4023 err = create_diff(s);
4024 if (s->first_displayed_line + view->nlines - 1 >
4025 s->nlines) {
4026 s->first_displayed_line = 1;
4027 s->last_displayed_line = view->nlines;
4030 break;
4031 case ']':
4032 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4033 s->diff_context++;
4034 s->matched_line = 0;
4035 diff_view_indicate_progress(view);
4036 err = create_diff(s);
4038 break;
4039 case '<':
4040 case ',':
4041 if (s->log_view == NULL)
4042 break;
4043 ls = &s->log_view->state.log;
4044 old_selected_entry = ls->selected_entry;
4046 err = input_log_view(NULL, s->log_view, KEY_UP);
4047 if (err)
4048 break;
4050 if (old_selected_entry == ls->selected_entry)
4051 break;
4053 err = set_selected_commit(s, ls->selected_entry);
4054 if (err)
4055 break;
4057 s->first_displayed_line = 1;
4058 s->last_displayed_line = view->nlines;
4059 s->matched_line = 0;
4060 view->x = 0;
4062 diff_view_indicate_progress(view);
4063 err = create_diff(s);
4064 break;
4065 case '>':
4066 case '.':
4067 if (s->log_view == NULL)
4068 break;
4069 ls = &s->log_view->state.log;
4070 old_selected_entry = ls->selected_entry;
4072 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4073 if (err)
4074 break;
4076 if (old_selected_entry == ls->selected_entry)
4077 break;
4079 err = set_selected_commit(s, ls->selected_entry);
4080 if (err)
4081 break;
4083 s->first_displayed_line = 1;
4084 s->last_displayed_line = view->nlines;
4085 s->matched_line = 0;
4086 view->x = 0;
4088 diff_view_indicate_progress(view);
4089 err = create_diff(s);
4090 break;
4091 default:
4092 break;
4095 return err;
4098 static const struct got_error *
4099 cmd_diff(int argc, char *argv[])
4101 const struct got_error *error = NULL;
4102 struct got_repository *repo = NULL;
4103 struct got_worktree *worktree = NULL;
4104 struct got_object_id *id1 = NULL, *id2 = NULL;
4105 char *repo_path = NULL, *cwd = NULL;
4106 char *id_str1 = NULL, *id_str2 = NULL;
4107 char *label1 = NULL, *label2 = NULL;
4108 int diff_context = 3, ignore_whitespace = 0;
4109 int ch, force_text_diff = 0;
4110 const char *errstr;
4111 struct tog_view *view;
4112 int *pack_fds = NULL;
4114 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4115 switch (ch) {
4116 case 'a':
4117 force_text_diff = 1;
4118 break;
4119 case 'C':
4120 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4121 &errstr);
4122 if (errstr != NULL)
4123 errx(1, "number of context lines is %s: %s",
4124 errstr, errstr);
4125 break;
4126 case 'r':
4127 repo_path = realpath(optarg, NULL);
4128 if (repo_path == NULL)
4129 return got_error_from_errno2("realpath",
4130 optarg);
4131 got_path_strip_trailing_slashes(repo_path);
4132 break;
4133 case 'w':
4134 ignore_whitespace = 1;
4135 break;
4136 default:
4137 usage_diff();
4138 /* NOTREACHED */
4142 argc -= optind;
4143 argv += optind;
4145 if (argc == 0) {
4146 usage_diff(); /* TODO show local worktree changes */
4147 } else if (argc == 2) {
4148 id_str1 = argv[0];
4149 id_str2 = argv[1];
4150 } else
4151 usage_diff();
4153 error = got_repo_pack_fds_open(&pack_fds);
4154 if (error)
4155 goto done;
4157 if (repo_path == NULL) {
4158 cwd = getcwd(NULL, 0);
4159 if (cwd == NULL)
4160 return got_error_from_errno("getcwd");
4161 error = got_worktree_open(&worktree, cwd);
4162 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4163 goto done;
4164 if (worktree)
4165 repo_path =
4166 strdup(got_worktree_get_repo_path(worktree));
4167 else
4168 repo_path = strdup(cwd);
4169 if (repo_path == NULL) {
4170 error = got_error_from_errno("strdup");
4171 goto done;
4175 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4176 if (error)
4177 goto done;
4179 init_curses();
4181 error = apply_unveil(got_repo_get_path(repo), NULL);
4182 if (error)
4183 goto done;
4185 error = tog_load_refs(repo, 0);
4186 if (error)
4187 goto done;
4189 error = got_repo_match_object_id(&id1, &label1, id_str1,
4190 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4191 if (error)
4192 goto done;
4194 error = got_repo_match_object_id(&id2, &label2, id_str2,
4195 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4196 if (error)
4197 goto done;
4199 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4200 if (view == NULL) {
4201 error = got_error_from_errno("view_open");
4202 goto done;
4204 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4205 ignore_whitespace, force_text_diff, NULL, repo);
4206 if (error)
4207 goto done;
4208 error = view_loop(view);
4209 done:
4210 free(label1);
4211 free(label2);
4212 free(repo_path);
4213 free(cwd);
4214 if (repo) {
4215 const struct got_error *close_err = got_repo_close(repo);
4216 if (error == NULL)
4217 error = close_err;
4219 if (worktree)
4220 got_worktree_close(worktree);
4221 if (pack_fds) {
4222 const struct got_error *pack_err =
4223 got_repo_pack_fds_close(pack_fds);
4224 if (error == NULL)
4225 error = pack_err;
4227 tog_free_refs();
4228 return error;
4231 __dead static void
4232 usage_blame(void)
4234 endwin();
4235 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4236 getprogname());
4237 exit(1);
4240 struct tog_blame_line {
4241 int annotated;
4242 struct got_object_id *id;
4245 static const struct got_error *
4246 draw_blame(struct tog_view *view)
4248 struct tog_blame_view_state *s = &view->state.blame;
4249 struct tog_blame *blame = &s->blame;
4250 regmatch_t *regmatch = &view->regmatch;
4251 const struct got_error *err;
4252 int lineno = 0, nprinted = 0, i;
4253 char *line = NULL;
4254 size_t linesize = 0;
4255 ssize_t linelen;
4256 wchar_t *wline;
4257 int width;
4258 struct tog_blame_line *blame_line;
4259 struct got_object_id *prev_id = NULL;
4260 char *id_str;
4261 struct tog_color *tc;
4263 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4264 if (err)
4265 return err;
4267 rewind(blame->f);
4268 werase(view->window);
4270 if (asprintf(&line, "commit %s", id_str) == -1) {
4271 err = got_error_from_errno("asprintf");
4272 free(id_str);
4273 return err;
4276 err = format_line(&wline, &width, line, view->ncols, 0, 0);
4277 free(line);
4278 line = NULL;
4279 if (err)
4280 return err;
4281 if (view_needs_focus_indication(view))
4282 wstandout(view->window);
4283 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4284 if (tc)
4285 wattr_on(view->window,
4286 COLOR_PAIR(tc->colorpair), NULL);
4287 waddwstr(view->window, wline);
4288 if (tc)
4289 wattr_off(view->window,
4290 COLOR_PAIR(tc->colorpair), NULL);
4291 if (view_needs_focus_indication(view))
4292 wstandend(view->window);
4293 free(wline);
4294 wline = NULL;
4295 if (width < view->ncols - 1)
4296 waddch(view->window, '\n');
4298 if (asprintf(&line, "[%d/%d] %s%s",
4299 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4300 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4301 free(id_str);
4302 return got_error_from_errno("asprintf");
4304 free(id_str);
4305 err = format_line(&wline, &width, line, view->ncols, 0, 0);
4306 free(line);
4307 line = NULL;
4308 if (err)
4309 return err;
4310 waddwstr(view->window, wline);
4311 free(wline);
4312 wline = NULL;
4313 if (width < view->ncols - 1)
4314 waddch(view->window, '\n');
4316 s->eof = 0;
4317 view->maxx = 0;
4318 while (nprinted < view->nlines - 2) {
4319 linelen = getline(&line, &linesize, blame->f);
4320 if (linelen == -1) {
4321 if (feof(blame->f)) {
4322 s->eof = 1;
4323 break;
4325 free(line);
4326 return got_ferror(blame->f, GOT_ERR_IO);
4328 if (++lineno < s->first_displayed_line)
4329 continue;
4331 view->maxx = MAX(view->maxx, linelen);
4333 if (view->focussed && nprinted == s->selected_line - 1)
4334 wstandout(view->window);
4336 if (blame->nlines > 0) {
4337 blame_line = &blame->lines[lineno - 1];
4338 if (blame_line->annotated && prev_id &&
4339 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4340 !(view->focussed &&
4341 nprinted == s->selected_line - 1)) {
4342 waddstr(view->window, " ");
4343 } else if (blame_line->annotated) {
4344 char *id_str;
4345 err = got_object_id_str(&id_str, blame_line->id);
4346 if (err) {
4347 free(line);
4348 return err;
4350 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4351 if (tc)
4352 wattr_on(view->window,
4353 COLOR_PAIR(tc->colorpair), NULL);
4354 wprintw(view->window, "%.8s", id_str);
4355 if (tc)
4356 wattr_off(view->window,
4357 COLOR_PAIR(tc->colorpair), NULL);
4358 free(id_str);
4359 prev_id = blame_line->id;
4360 } else {
4361 waddstr(view->window, "........");
4362 prev_id = NULL;
4364 } else {
4365 waddstr(view->window, "........");
4366 prev_id = NULL;
4369 if (view->focussed && nprinted == s->selected_line - 1)
4370 wstandend(view->window);
4371 waddstr(view->window, " ");
4373 if (view->ncols <= 9) {
4374 width = 9;
4375 wline = wcsdup(L"");
4376 if (wline == NULL) {
4377 err = got_error_from_errno("wcsdup");
4378 free(line);
4379 return err;
4381 } else if (s->first_displayed_line + nprinted ==
4382 s->matched_line &&
4383 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4384 err = add_matched_line(&width, line, view->ncols - 9, 9,
4385 view->window, view->x, regmatch);
4386 if (err) {
4387 free(line);
4388 return err;
4390 width += 9;
4391 } else {
4392 err = format_line(&wline, &width, line,
4393 view->x + view->ncols - 9, 9, 1);
4394 if (err) {
4395 free(line);
4396 return err;
4398 if (view->x < width) {
4399 waddwstr(view->window, wline + view->x);
4400 for (i = 0; i < view->x; i++)
4401 width -= wcwidth(wline[i]);
4403 width += 9;
4404 free(wline);
4405 wline = NULL;
4408 if (width <= view->ncols - 1)
4409 waddch(view->window, '\n');
4410 if (++nprinted == 1)
4411 s->first_displayed_line = lineno;
4413 free(line);
4414 s->last_displayed_line = lineno;
4416 view_vborder(view);
4418 return NULL;
4421 static const struct got_error *
4422 blame_cb(void *arg, int nlines, int lineno,
4423 struct got_commit_object *commit, struct got_object_id *id)
4425 const struct got_error *err = NULL;
4426 struct tog_blame_cb_args *a = arg;
4427 struct tog_blame_line *line;
4428 int errcode;
4430 if (nlines != a->nlines ||
4431 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4432 return got_error(GOT_ERR_RANGE);
4434 errcode = pthread_mutex_lock(&tog_mutex);
4435 if (errcode)
4436 return got_error_set_errno(errcode, "pthread_mutex_lock");
4438 if (*a->quit) { /* user has quit the blame view */
4439 err = got_error(GOT_ERR_ITER_COMPLETED);
4440 goto done;
4443 if (lineno == -1)
4444 goto done; /* no change in this commit */
4446 line = &a->lines[lineno - 1];
4447 if (line->annotated)
4448 goto done;
4450 line->id = got_object_id_dup(id);
4451 if (line->id == NULL) {
4452 err = got_error_from_errno("got_object_id_dup");
4453 goto done;
4455 line->annotated = 1;
4456 done:
4457 errcode = pthread_mutex_unlock(&tog_mutex);
4458 if (errcode)
4459 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4460 return err;
4463 static void *
4464 blame_thread(void *arg)
4466 const struct got_error *err, *close_err;
4467 struct tog_blame_thread_args *ta = arg;
4468 struct tog_blame_cb_args *a = ta->cb_args;
4469 int errcode;
4471 err = block_signals_used_by_main_thread();
4472 if (err)
4473 return (void *)err;
4475 err = got_blame(ta->path, a->commit_id, ta->repo,
4476 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4477 if (err && err->code == GOT_ERR_CANCELLED)
4478 err = NULL;
4480 errcode = pthread_mutex_lock(&tog_mutex);
4481 if (errcode)
4482 return (void *)got_error_set_errno(errcode,
4483 "pthread_mutex_lock");
4485 close_err = got_repo_close(ta->repo);
4486 if (err == NULL)
4487 err = close_err;
4488 ta->repo = NULL;
4489 *ta->complete = 1;
4491 errcode = pthread_mutex_unlock(&tog_mutex);
4492 if (errcode && err == NULL)
4493 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4495 return (void *)err;
4498 static struct got_object_id *
4499 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4500 int first_displayed_line, int selected_line)
4502 struct tog_blame_line *line;
4504 if (nlines <= 0)
4505 return NULL;
4507 line = &lines[first_displayed_line - 1 + selected_line - 1];
4508 if (!line->annotated)
4509 return NULL;
4511 return line->id;
4514 static const struct got_error *
4515 stop_blame(struct tog_blame *blame)
4517 const struct got_error *err = NULL;
4518 int i;
4520 if (blame->thread) {
4521 int errcode;
4522 errcode = pthread_mutex_unlock(&tog_mutex);
4523 if (errcode)
4524 return got_error_set_errno(errcode,
4525 "pthread_mutex_unlock");
4526 errcode = pthread_join(blame->thread, (void **)&err);
4527 if (errcode)
4528 return got_error_set_errno(errcode, "pthread_join");
4529 errcode = pthread_mutex_lock(&tog_mutex);
4530 if (errcode)
4531 return got_error_set_errno(errcode,
4532 "pthread_mutex_lock");
4533 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4534 err = NULL;
4535 blame->thread = 0; //NULL;
4537 if (blame->thread_args.repo) {
4538 const struct got_error *close_err;
4539 close_err = got_repo_close(blame->thread_args.repo);
4540 if (err == NULL)
4541 err = close_err;
4542 blame->thread_args.repo = NULL;
4544 if (blame->f) {
4545 if (fclose(blame->f) == EOF && err == NULL)
4546 err = got_error_from_errno("fclose");
4547 blame->f = NULL;
4549 if (blame->lines) {
4550 for (i = 0; i < blame->nlines; i++)
4551 free(blame->lines[i].id);
4552 free(blame->lines);
4553 blame->lines = NULL;
4555 free(blame->cb_args.commit_id);
4556 blame->cb_args.commit_id = NULL;
4557 if (blame->pack_fds) {
4558 const struct got_error *pack_err =
4559 got_repo_pack_fds_close(blame->pack_fds);
4560 if (err == NULL)
4561 err = pack_err;
4562 blame->pack_fds = NULL;
4564 return err;
4567 static const struct got_error *
4568 cancel_blame_view(void *arg)
4570 const struct got_error *err = NULL;
4571 int *done = arg;
4572 int errcode;
4574 errcode = pthread_mutex_lock(&tog_mutex);
4575 if (errcode)
4576 return got_error_set_errno(errcode,
4577 "pthread_mutex_unlock");
4579 if (*done)
4580 err = got_error(GOT_ERR_CANCELLED);
4582 errcode = pthread_mutex_unlock(&tog_mutex);
4583 if (errcode)
4584 return got_error_set_errno(errcode,
4585 "pthread_mutex_lock");
4587 return err;
4590 static const struct got_error *
4591 run_blame(struct tog_view *view)
4593 struct tog_blame_view_state *s = &view->state.blame;
4594 struct tog_blame *blame = &s->blame;
4595 const struct got_error *err = NULL;
4596 struct got_commit_object *commit = NULL;
4597 struct got_blob_object *blob = NULL;
4598 struct got_repository *thread_repo = NULL;
4599 struct got_object_id *obj_id = NULL;
4600 int obj_type;
4601 int *pack_fds = NULL;
4603 err = got_object_open_as_commit(&commit, s->repo,
4604 &s->blamed_commit->id);
4605 if (err)
4606 return err;
4608 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4609 if (err)
4610 goto done;
4612 err = got_object_get_type(&obj_type, s->repo, obj_id);
4613 if (err)
4614 goto done;
4616 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4617 err = got_error(GOT_ERR_OBJ_TYPE);
4618 goto done;
4621 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4622 if (err)
4623 goto done;
4624 blame->f = got_opentemp();
4625 if (blame->f == NULL) {
4626 err = got_error_from_errno("got_opentemp");
4627 goto done;
4629 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4630 &blame->line_offsets, blame->f, blob);
4631 if (err)
4632 goto done;
4633 if (blame->nlines == 0) {
4634 s->blame_complete = 1;
4635 goto done;
4638 /* Don't include \n at EOF in the blame line count. */
4639 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4640 blame->nlines--;
4642 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4643 if (blame->lines == NULL) {
4644 err = got_error_from_errno("calloc");
4645 goto done;
4648 err = got_repo_pack_fds_open(&pack_fds);
4649 if (err)
4650 goto done;
4651 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4652 pack_fds);
4653 if (err)
4654 goto done;
4656 blame->pack_fds = pack_fds;
4657 blame->cb_args.view = view;
4658 blame->cb_args.lines = blame->lines;
4659 blame->cb_args.nlines = blame->nlines;
4660 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4661 if (blame->cb_args.commit_id == NULL) {
4662 err = got_error_from_errno("got_object_id_dup");
4663 goto done;
4665 blame->cb_args.quit = &s->done;
4667 blame->thread_args.path = s->path;
4668 blame->thread_args.repo = thread_repo;
4669 blame->thread_args.cb_args = &blame->cb_args;
4670 blame->thread_args.complete = &s->blame_complete;
4671 blame->thread_args.cancel_cb = cancel_blame_view;
4672 blame->thread_args.cancel_arg = &s->done;
4673 s->blame_complete = 0;
4675 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4676 s->first_displayed_line = 1;
4677 s->last_displayed_line = view->nlines;
4678 s->selected_line = 1;
4680 s->matched_line = 0;
4682 done:
4683 if (commit)
4684 got_object_commit_close(commit);
4685 if (blob)
4686 got_object_blob_close(blob);
4687 free(obj_id);
4688 if (err)
4689 stop_blame(blame);
4690 return err;
4693 static const struct got_error *
4694 open_blame_view(struct tog_view *view, char *path,
4695 struct got_object_id *commit_id, struct got_repository *repo)
4697 const struct got_error *err = NULL;
4698 struct tog_blame_view_state *s = &view->state.blame;
4700 STAILQ_INIT(&s->blamed_commits);
4702 s->path = strdup(path);
4703 if (s->path == NULL)
4704 return got_error_from_errno("strdup");
4706 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4707 if (err) {
4708 free(s->path);
4709 return err;
4712 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4713 s->first_displayed_line = 1;
4714 s->last_displayed_line = view->nlines;
4715 s->selected_line = 1;
4716 s->blame_complete = 0;
4717 s->repo = repo;
4718 s->commit_id = commit_id;
4719 memset(&s->blame, 0, sizeof(s->blame));
4721 STAILQ_INIT(&s->colors);
4722 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4723 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4724 get_color_value("TOG_COLOR_COMMIT"));
4725 if (err)
4726 return err;
4729 view->show = show_blame_view;
4730 view->input = input_blame_view;
4731 view->close = close_blame_view;
4732 view->search_start = search_start_blame_view;
4733 view->search_next = search_next_blame_view;
4735 return run_blame(view);
4738 static const struct got_error *
4739 close_blame_view(struct tog_view *view)
4741 const struct got_error *err = NULL;
4742 struct tog_blame_view_state *s = &view->state.blame;
4744 if (s->blame.thread)
4745 err = stop_blame(&s->blame);
4747 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4748 struct got_object_qid *blamed_commit;
4749 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4750 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4751 got_object_qid_free(blamed_commit);
4754 free(s->path);
4755 free_colors(&s->colors);
4756 return err;
4759 static const struct got_error *
4760 search_start_blame_view(struct tog_view *view)
4762 struct tog_blame_view_state *s = &view->state.blame;
4764 s->matched_line = 0;
4765 return NULL;
4768 static const struct got_error *
4769 search_next_blame_view(struct tog_view *view)
4771 struct tog_blame_view_state *s = &view->state.blame;
4772 const struct got_error *err = NULL;
4773 int lineno;
4774 char *exstr = NULL, *line = NULL;
4775 size_t linesize = 0;
4776 ssize_t linelen;
4778 if (!view->searching) {
4779 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4780 return NULL;
4783 if (s->matched_line) {
4784 if (view->searching == TOG_SEARCH_FORWARD)
4785 lineno = s->matched_line + 1;
4786 else
4787 lineno = s->matched_line - 1;
4788 } else
4789 lineno = s->first_displayed_line - 1 + s->selected_line;
4791 while (1) {
4792 off_t offset;
4794 if (lineno <= 0 || lineno > s->blame.nlines) {
4795 if (s->matched_line == 0) {
4796 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4797 break;
4800 if (view->searching == TOG_SEARCH_FORWARD)
4801 lineno = 1;
4802 else
4803 lineno = s->blame.nlines;
4806 offset = s->blame.line_offsets[lineno - 1];
4807 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4808 free(line);
4809 return got_error_from_errno("fseeko");
4811 linelen = getline(&line, &linesize, s->blame.f);
4812 err = expand_tab(&exstr, line);
4813 if (err)
4814 break;
4815 if (linelen != -1 &&
4816 match_line(exstr, &view->regex, 1, &view->regmatch)) {
4817 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4818 s->matched_line = lineno;
4819 break;
4821 free(exstr);
4822 exstr = NULL;
4823 if (view->searching == TOG_SEARCH_FORWARD)
4824 lineno++;
4825 else
4826 lineno--;
4828 free(line);
4829 free(exstr);
4831 if (s->matched_line) {
4832 s->first_displayed_line = s->matched_line;
4833 s->selected_line = 1;
4836 return err;
4839 static const struct got_error *
4840 show_blame_view(struct tog_view *view)
4842 const struct got_error *err = NULL;
4843 struct tog_blame_view_state *s = &view->state.blame;
4844 int errcode;
4846 if (s->blame.thread == 0 && !s->blame_complete) {
4847 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4848 &s->blame.thread_args);
4849 if (errcode)
4850 return got_error_set_errno(errcode, "pthread_create");
4852 halfdelay(1); /* fast refresh while annotating */
4855 if (s->blame_complete)
4856 halfdelay(10); /* disable fast refresh */
4858 err = draw_blame(view);
4860 view_vborder(view);
4861 return err;
4864 static const struct got_error *
4865 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4867 const struct got_error *err = NULL, *thread_err = NULL;
4868 struct tog_view *diff_view;
4869 struct tog_blame_view_state *s = &view->state.blame;
4870 int begin_x = 0, nscroll = view->nlines - 2;
4872 switch (ch) {
4873 case '0':
4874 view->x = 0;
4875 break;
4876 case '$':
4877 view->x = MAX(view->maxx - view->ncols / 3, 0);
4878 break;
4879 case KEY_RIGHT:
4880 case 'l':
4881 if (view->x + view->ncols / 3 < view->maxx)
4882 view->x += 2; /* move two columns right */
4883 break;
4884 case KEY_LEFT:
4885 case 'h':
4886 view->x -= MIN(view->x, 2); /* move two columns back */
4887 break;
4888 case 'q':
4889 s->done = 1;
4890 break;
4891 case 'g':
4892 case KEY_HOME:
4893 s->selected_line = 1;
4894 s->first_displayed_line = 1;
4895 break;
4896 case 'G':
4897 case KEY_END:
4898 if (s->blame.nlines < view->nlines - 2) {
4899 s->selected_line = s->blame.nlines;
4900 s->first_displayed_line = 1;
4901 } else {
4902 s->selected_line = view->nlines - 2;
4903 s->first_displayed_line = s->blame.nlines -
4904 (view->nlines - 3);
4906 break;
4907 case 'k':
4908 case KEY_UP:
4909 case CTRL('p'):
4910 if (s->selected_line > 1)
4911 s->selected_line--;
4912 else if (s->selected_line == 1 &&
4913 s->first_displayed_line > 1)
4914 s->first_displayed_line--;
4915 break;
4916 case CTRL('u'):
4917 case 'u':
4918 nscroll /= 2;
4919 /* FALL THROUGH */
4920 case KEY_PPAGE:
4921 case CTRL('b'):
4922 if (s->first_displayed_line == 1) {
4923 s->selected_line = MAX(1, s->selected_line - nscroll);
4924 break;
4926 if (s->first_displayed_line > nscroll)
4927 s->first_displayed_line -= nscroll;
4928 else
4929 s->first_displayed_line = 1;
4930 break;
4931 case 'j':
4932 case KEY_DOWN:
4933 case CTRL('n'):
4934 if (s->selected_line < view->nlines - 2 &&
4935 s->first_displayed_line +
4936 s->selected_line <= s->blame.nlines)
4937 s->selected_line++;
4938 else if (s->last_displayed_line <
4939 s->blame.nlines)
4940 s->first_displayed_line++;
4941 break;
4942 case 'b':
4943 case 'p': {
4944 struct got_object_id *id = NULL;
4945 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4946 s->first_displayed_line, s->selected_line);
4947 if (id == NULL)
4948 break;
4949 if (ch == 'p') {
4950 struct got_commit_object *commit, *pcommit;
4951 struct got_object_qid *pid;
4952 struct got_object_id *blob_id = NULL;
4953 int obj_type;
4954 err = got_object_open_as_commit(&commit,
4955 s->repo, id);
4956 if (err)
4957 break;
4958 pid = STAILQ_FIRST(
4959 got_object_commit_get_parent_ids(commit));
4960 if (pid == NULL) {
4961 got_object_commit_close(commit);
4962 break;
4964 /* Check if path history ends here. */
4965 err = got_object_open_as_commit(&pcommit,
4966 s->repo, &pid->id);
4967 if (err)
4968 break;
4969 err = got_object_id_by_path(&blob_id, s->repo,
4970 pcommit, s->path);
4971 got_object_commit_close(pcommit);
4972 if (err) {
4973 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4974 err = NULL;
4975 got_object_commit_close(commit);
4976 break;
4978 err = got_object_get_type(&obj_type, s->repo,
4979 blob_id);
4980 free(blob_id);
4981 /* Can't blame non-blob type objects. */
4982 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4983 got_object_commit_close(commit);
4984 break;
4986 err = got_object_qid_alloc(&s->blamed_commit,
4987 &pid->id);
4988 got_object_commit_close(commit);
4989 } else {
4990 if (got_object_id_cmp(id,
4991 &s->blamed_commit->id) == 0)
4992 break;
4993 err = got_object_qid_alloc(&s->blamed_commit,
4994 id);
4996 if (err)
4997 break;
4998 s->done = 1;
4999 thread_err = stop_blame(&s->blame);
5000 s->done = 0;
5001 if (thread_err)
5002 break;
5003 STAILQ_INSERT_HEAD(&s->blamed_commits,
5004 s->blamed_commit, entry);
5005 err = run_blame(view);
5006 if (err)
5007 break;
5008 break;
5010 case 'B': {
5011 struct got_object_qid *first;
5012 first = STAILQ_FIRST(&s->blamed_commits);
5013 if (!got_object_id_cmp(&first->id, s->commit_id))
5014 break;
5015 s->done = 1;
5016 thread_err = stop_blame(&s->blame);
5017 s->done = 0;
5018 if (thread_err)
5019 break;
5020 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5021 got_object_qid_free(s->blamed_commit);
5022 s->blamed_commit =
5023 STAILQ_FIRST(&s->blamed_commits);
5024 err = run_blame(view);
5025 if (err)
5026 break;
5027 break;
5029 case KEY_ENTER:
5030 case '\r': {
5031 struct got_object_id *id = NULL;
5032 struct got_object_qid *pid;
5033 struct got_commit_object *commit = NULL;
5034 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5035 s->first_displayed_line, s->selected_line);
5036 if (id == NULL)
5037 break;
5038 err = got_object_open_as_commit(&commit, s->repo, id);
5039 if (err)
5040 break;
5041 pid = STAILQ_FIRST(
5042 got_object_commit_get_parent_ids(commit));
5043 if (view_is_parent_view(view))
5044 begin_x = view_split_begin_x(view->begin_x);
5045 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5046 if (diff_view == NULL) {
5047 got_object_commit_close(commit);
5048 err = got_error_from_errno("view_open");
5049 break;
5051 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5052 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5053 got_object_commit_close(commit);
5054 if (err) {
5055 view_close(diff_view);
5056 break;
5058 view->focussed = 0;
5059 diff_view->focussed = 1;
5060 if (view_is_parent_view(view)) {
5061 err = view_close_child(view);
5062 if (err)
5063 break;
5064 view_set_child(view, diff_view);
5065 view->focus_child = 1;
5066 } else
5067 *new_view = diff_view;
5068 if (err)
5069 break;
5070 break;
5072 case CTRL('d'):
5073 case 'd':
5074 nscroll /= 2;
5075 /* FALL THROUGH */
5076 case KEY_NPAGE:
5077 case CTRL('f'):
5078 case ' ':
5079 if (s->last_displayed_line >= s->blame.nlines &&
5080 s->selected_line >= MIN(s->blame.nlines,
5081 view->nlines - 2)) {
5082 break;
5084 if (s->last_displayed_line >= s->blame.nlines &&
5085 s->selected_line < view->nlines - 2) {
5086 s->selected_line +=
5087 MIN(nscroll, s->last_displayed_line -
5088 s->first_displayed_line - s->selected_line + 1);
5090 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5091 s->first_displayed_line += nscroll;
5092 else
5093 s->first_displayed_line =
5094 s->blame.nlines - (view->nlines - 3);
5095 break;
5096 case KEY_RESIZE:
5097 if (s->selected_line > view->nlines - 2) {
5098 s->selected_line = MIN(s->blame.nlines,
5099 view->nlines - 2);
5101 break;
5102 default:
5103 break;
5105 return thread_err ? thread_err : err;
5108 static const struct got_error *
5109 cmd_blame(int argc, char *argv[])
5111 const struct got_error *error;
5112 struct got_repository *repo = NULL;
5113 struct got_worktree *worktree = NULL;
5114 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5115 char *link_target = NULL;
5116 struct got_object_id *commit_id = NULL;
5117 struct got_commit_object *commit = NULL;
5118 char *commit_id_str = NULL;
5119 int ch;
5120 struct tog_view *view;
5121 int *pack_fds = NULL;
5123 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5124 switch (ch) {
5125 case 'c':
5126 commit_id_str = optarg;
5127 break;
5128 case 'r':
5129 repo_path = realpath(optarg, NULL);
5130 if (repo_path == NULL)
5131 return got_error_from_errno2("realpath",
5132 optarg);
5133 break;
5134 default:
5135 usage_blame();
5136 /* NOTREACHED */
5140 argc -= optind;
5141 argv += optind;
5143 if (argc != 1)
5144 usage_blame();
5146 error = got_repo_pack_fds_open(&pack_fds);
5147 if (error != NULL)
5148 goto done;
5150 if (repo_path == NULL) {
5151 cwd = getcwd(NULL, 0);
5152 if (cwd == NULL)
5153 return got_error_from_errno("getcwd");
5154 error = got_worktree_open(&worktree, cwd);
5155 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5156 goto done;
5157 if (worktree)
5158 repo_path =
5159 strdup(got_worktree_get_repo_path(worktree));
5160 else
5161 repo_path = strdup(cwd);
5162 if (repo_path == NULL) {
5163 error = got_error_from_errno("strdup");
5164 goto done;
5168 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5169 if (error != NULL)
5170 goto done;
5172 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5173 worktree);
5174 if (error)
5175 goto done;
5177 init_curses();
5179 error = apply_unveil(got_repo_get_path(repo), NULL);
5180 if (error)
5181 goto done;
5183 error = tog_load_refs(repo, 0);
5184 if (error)
5185 goto done;
5187 if (commit_id_str == NULL) {
5188 struct got_reference *head_ref;
5189 error = got_ref_open(&head_ref, repo, worktree ?
5190 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5191 if (error != NULL)
5192 goto done;
5193 error = got_ref_resolve(&commit_id, repo, head_ref);
5194 got_ref_close(head_ref);
5195 } else {
5196 error = got_repo_match_object_id(&commit_id, NULL,
5197 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5199 if (error != NULL)
5200 goto done;
5202 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5203 if (view == NULL) {
5204 error = got_error_from_errno("view_open");
5205 goto done;
5208 error = got_object_open_as_commit(&commit, repo, commit_id);
5209 if (error)
5210 goto done;
5212 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5213 commit, repo);
5214 if (error)
5215 goto done;
5217 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5218 commit_id, repo);
5219 if (error)
5220 goto done;
5221 if (worktree) {
5222 /* Release work tree lock. */
5223 got_worktree_close(worktree);
5224 worktree = NULL;
5226 error = view_loop(view);
5227 done:
5228 free(repo_path);
5229 free(in_repo_path);
5230 free(link_target);
5231 free(cwd);
5232 free(commit_id);
5233 if (commit)
5234 got_object_commit_close(commit);
5235 if (worktree)
5236 got_worktree_close(worktree);
5237 if (repo) {
5238 const struct got_error *close_err = got_repo_close(repo);
5239 if (error == NULL)
5240 error = close_err;
5242 if (pack_fds) {
5243 const struct got_error *pack_err =
5244 got_repo_pack_fds_close(pack_fds);
5245 if (error == NULL)
5246 error = pack_err;
5248 tog_free_refs();
5249 return error;
5252 static const struct got_error *
5253 draw_tree_entries(struct tog_view *view, const char *parent_path)
5255 struct tog_tree_view_state *s = &view->state.tree;
5256 const struct got_error *err = NULL;
5257 struct got_tree_entry *te;
5258 wchar_t *wline;
5259 struct tog_color *tc;
5260 int width, n, i, nentries;
5261 int limit = view->nlines;
5263 s->ndisplayed = 0;
5265 werase(view->window);
5267 if (limit == 0)
5268 return NULL;
5270 err = format_line(&wline, &width, s->tree_label, view->ncols, 0, 0);
5271 if (err)
5272 return err;
5273 if (view_needs_focus_indication(view))
5274 wstandout(view->window);
5275 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5276 if (tc)
5277 wattr_on(view->window,
5278 COLOR_PAIR(tc->colorpair), NULL);
5279 waddwstr(view->window, wline);
5280 if (tc)
5281 wattr_off(view->window,
5282 COLOR_PAIR(tc->colorpair), NULL);
5283 if (view_needs_focus_indication(view))
5284 wstandend(view->window);
5285 free(wline);
5286 wline = NULL;
5287 if (width < view->ncols - 1)
5288 waddch(view->window, '\n');
5289 if (--limit <= 0)
5290 return NULL;
5291 err = format_line(&wline, &width, parent_path, view->ncols, 0, 0);
5292 if (err)
5293 return err;
5294 waddwstr(view->window, wline);
5295 free(wline);
5296 wline = NULL;
5297 if (width < view->ncols - 1)
5298 waddch(view->window, '\n');
5299 if (--limit <= 0)
5300 return NULL;
5301 waddch(view->window, '\n');
5302 if (--limit <= 0)
5303 return NULL;
5305 if (s->first_displayed_entry == NULL) {
5306 te = got_object_tree_get_first_entry(s->tree);
5307 if (s->selected == 0) {
5308 if (view->focussed)
5309 wstandout(view->window);
5310 s->selected_entry = NULL;
5312 waddstr(view->window, " ..\n"); /* parent directory */
5313 if (s->selected == 0 && view->focussed)
5314 wstandend(view->window);
5315 s->ndisplayed++;
5316 if (--limit <= 0)
5317 return NULL;
5318 n = 1;
5319 } else {
5320 n = 0;
5321 te = s->first_displayed_entry;
5324 nentries = got_object_tree_get_nentries(s->tree);
5325 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5326 char *line = NULL, *id_str = NULL, *link_target = NULL;
5327 const char *modestr = "";
5328 mode_t mode;
5330 te = got_object_tree_get_entry(s->tree, i);
5331 mode = got_tree_entry_get_mode(te);
5333 if (s->show_ids) {
5334 err = got_object_id_str(&id_str,
5335 got_tree_entry_get_id(te));
5336 if (err)
5337 return got_error_from_errno(
5338 "got_object_id_str");
5340 if (got_object_tree_entry_is_submodule(te))
5341 modestr = "$";
5342 else if (S_ISLNK(mode)) {
5343 int i;
5345 err = got_tree_entry_get_symlink_target(&link_target,
5346 te, s->repo);
5347 if (err) {
5348 free(id_str);
5349 return err;
5351 for (i = 0; i < strlen(link_target); i++) {
5352 if (!isprint((unsigned char)link_target[i]))
5353 link_target[i] = '?';
5355 modestr = "@";
5357 else if (S_ISDIR(mode))
5358 modestr = "/";
5359 else if (mode & S_IXUSR)
5360 modestr = "*";
5361 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5362 got_tree_entry_get_name(te), modestr,
5363 link_target ? " -> ": "",
5364 link_target ? link_target : "") == -1) {
5365 free(id_str);
5366 free(link_target);
5367 return got_error_from_errno("asprintf");
5369 free(id_str);
5370 free(link_target);
5371 err = format_line(&wline, &width, line, view->ncols, 0, 0);
5372 if (err) {
5373 free(line);
5374 break;
5376 if (n == s->selected) {
5377 if (view->focussed)
5378 wstandout(view->window);
5379 s->selected_entry = te;
5381 tc = match_color(&s->colors, line);
5382 if (tc)
5383 wattr_on(view->window,
5384 COLOR_PAIR(tc->colorpair), NULL);
5385 waddwstr(view->window, wline);
5386 if (tc)
5387 wattr_off(view->window,
5388 COLOR_PAIR(tc->colorpair), NULL);
5389 if (width < view->ncols - 1)
5390 waddch(view->window, '\n');
5391 if (n == s->selected && view->focussed)
5392 wstandend(view->window);
5393 free(line);
5394 free(wline);
5395 wline = NULL;
5396 n++;
5397 s->ndisplayed++;
5398 s->last_displayed_entry = te;
5399 if (--limit <= 0)
5400 break;
5403 return err;
5406 static void
5407 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5409 struct got_tree_entry *te;
5410 int isroot = s->tree == s->root;
5411 int i = 0;
5413 if (s->first_displayed_entry == NULL)
5414 return;
5416 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5417 while (i++ < maxscroll) {
5418 if (te == NULL) {
5419 if (!isroot)
5420 s->first_displayed_entry = NULL;
5421 break;
5423 s->first_displayed_entry = te;
5424 te = got_tree_entry_get_prev(s->tree, te);
5428 static void
5429 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5431 struct got_tree_entry *next, *last;
5432 int n = 0;
5434 if (s->first_displayed_entry)
5435 next = got_tree_entry_get_next(s->tree,
5436 s->first_displayed_entry);
5437 else
5438 next = got_object_tree_get_first_entry(s->tree);
5440 last = s->last_displayed_entry;
5441 while (next && last && n++ < maxscroll) {
5442 last = got_tree_entry_get_next(s->tree, last);
5443 if (last) {
5444 s->first_displayed_entry = next;
5445 next = got_tree_entry_get_next(s->tree, next);
5450 static const struct got_error *
5451 tree_entry_path(char **path, struct tog_parent_trees *parents,
5452 struct got_tree_entry *te)
5454 const struct got_error *err = NULL;
5455 struct tog_parent_tree *pt;
5456 size_t len = 2; /* for leading slash and NUL */
5458 TAILQ_FOREACH(pt, parents, entry)
5459 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5460 + 1 /* slash */;
5461 if (te)
5462 len += strlen(got_tree_entry_get_name(te));
5464 *path = calloc(1, len);
5465 if (path == NULL)
5466 return got_error_from_errno("calloc");
5468 (*path)[0] = '/';
5469 pt = TAILQ_LAST(parents, tog_parent_trees);
5470 while (pt) {
5471 const char *name = got_tree_entry_get_name(pt->selected_entry);
5472 if (strlcat(*path, name, len) >= len) {
5473 err = got_error(GOT_ERR_NO_SPACE);
5474 goto done;
5476 if (strlcat(*path, "/", len) >= len) {
5477 err = got_error(GOT_ERR_NO_SPACE);
5478 goto done;
5480 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5482 if (te) {
5483 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5484 err = got_error(GOT_ERR_NO_SPACE);
5485 goto done;
5488 done:
5489 if (err) {
5490 free(*path);
5491 *path = NULL;
5493 return err;
5496 static const struct got_error *
5497 blame_tree_entry(struct tog_view **new_view, int begin_x,
5498 struct got_tree_entry *te, struct tog_parent_trees *parents,
5499 struct got_object_id *commit_id, struct got_repository *repo)
5501 const struct got_error *err = NULL;
5502 char *path;
5503 struct tog_view *blame_view;
5505 *new_view = NULL;
5507 err = tree_entry_path(&path, parents, te);
5508 if (err)
5509 return err;
5511 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5512 if (blame_view == NULL) {
5513 err = got_error_from_errno("view_open");
5514 goto done;
5517 err = open_blame_view(blame_view, path, commit_id, repo);
5518 if (err) {
5519 if (err->code == GOT_ERR_CANCELLED)
5520 err = NULL;
5521 view_close(blame_view);
5522 } else
5523 *new_view = blame_view;
5524 done:
5525 free(path);
5526 return err;
5529 static const struct got_error *
5530 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5531 struct tog_tree_view_state *s)
5533 struct tog_view *log_view;
5534 const struct got_error *err = NULL;
5535 char *path;
5537 *new_view = NULL;
5539 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5540 if (log_view == NULL)
5541 return got_error_from_errno("view_open");
5543 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5544 if (err)
5545 return err;
5547 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5548 path, 0);
5549 if (err)
5550 view_close(log_view);
5551 else
5552 *new_view = log_view;
5553 free(path);
5554 return err;
5557 static const struct got_error *
5558 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5559 const char *head_ref_name, struct got_repository *repo)
5561 const struct got_error *err = NULL;
5562 char *commit_id_str = NULL;
5563 struct tog_tree_view_state *s = &view->state.tree;
5564 struct got_commit_object *commit = NULL;
5566 TAILQ_INIT(&s->parents);
5567 STAILQ_INIT(&s->colors);
5569 s->commit_id = got_object_id_dup(commit_id);
5570 if (s->commit_id == NULL)
5571 return got_error_from_errno("got_object_id_dup");
5573 err = got_object_open_as_commit(&commit, repo, commit_id);
5574 if (err)
5575 goto done;
5578 * The root is opened here and will be closed when the view is closed.
5579 * Any visited subtrees and their path-wise parents are opened and
5580 * closed on demand.
5582 err = got_object_open_as_tree(&s->root, repo,
5583 got_object_commit_get_tree_id(commit));
5584 if (err)
5585 goto done;
5586 s->tree = s->root;
5588 err = got_object_id_str(&commit_id_str, commit_id);
5589 if (err != NULL)
5590 goto done;
5592 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5593 err = got_error_from_errno("asprintf");
5594 goto done;
5597 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5598 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5599 if (head_ref_name) {
5600 s->head_ref_name = strdup(head_ref_name);
5601 if (s->head_ref_name == NULL) {
5602 err = got_error_from_errno("strdup");
5603 goto done;
5606 s->repo = repo;
5608 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5609 err = add_color(&s->colors, "\\$$",
5610 TOG_COLOR_TREE_SUBMODULE,
5611 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5612 if (err)
5613 goto done;
5614 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5615 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5616 if (err)
5617 goto done;
5618 err = add_color(&s->colors, "/$",
5619 TOG_COLOR_TREE_DIRECTORY,
5620 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5621 if (err)
5622 goto done;
5624 err = add_color(&s->colors, "\\*$",
5625 TOG_COLOR_TREE_EXECUTABLE,
5626 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5627 if (err)
5628 goto done;
5630 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5631 get_color_value("TOG_COLOR_COMMIT"));
5632 if (err)
5633 goto done;
5636 view->show = show_tree_view;
5637 view->input = input_tree_view;
5638 view->close = close_tree_view;
5639 view->search_start = search_start_tree_view;
5640 view->search_next = search_next_tree_view;
5641 done:
5642 free(commit_id_str);
5643 if (commit)
5644 got_object_commit_close(commit);
5645 if (err)
5646 close_tree_view(view);
5647 return err;
5650 static const struct got_error *
5651 close_tree_view(struct tog_view *view)
5653 struct tog_tree_view_state *s = &view->state.tree;
5655 free_colors(&s->colors);
5656 free(s->tree_label);
5657 s->tree_label = NULL;
5658 free(s->commit_id);
5659 s->commit_id = NULL;
5660 free(s->head_ref_name);
5661 s->head_ref_name = NULL;
5662 while (!TAILQ_EMPTY(&s->parents)) {
5663 struct tog_parent_tree *parent;
5664 parent = TAILQ_FIRST(&s->parents);
5665 TAILQ_REMOVE(&s->parents, parent, entry);
5666 if (parent->tree != s->root)
5667 got_object_tree_close(parent->tree);
5668 free(parent);
5671 if (s->tree != NULL && s->tree != s->root)
5672 got_object_tree_close(s->tree);
5673 if (s->root)
5674 got_object_tree_close(s->root);
5675 return NULL;
5678 static const struct got_error *
5679 search_start_tree_view(struct tog_view *view)
5681 struct tog_tree_view_state *s = &view->state.tree;
5683 s->matched_entry = NULL;
5684 return NULL;
5687 static int
5688 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5690 regmatch_t regmatch;
5692 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5693 0) == 0;
5696 static const struct got_error *
5697 search_next_tree_view(struct tog_view *view)
5699 struct tog_tree_view_state *s = &view->state.tree;
5700 struct got_tree_entry *te = NULL;
5702 if (!view->searching) {
5703 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5704 return NULL;
5707 if (s->matched_entry) {
5708 if (view->searching == TOG_SEARCH_FORWARD) {
5709 if (s->selected_entry)
5710 te = got_tree_entry_get_next(s->tree,
5711 s->selected_entry);
5712 else
5713 te = got_object_tree_get_first_entry(s->tree);
5714 } else {
5715 if (s->selected_entry == NULL)
5716 te = got_object_tree_get_last_entry(s->tree);
5717 else
5718 te = got_tree_entry_get_prev(s->tree,
5719 s->selected_entry);
5721 } else {
5722 if (s->selected_entry)
5723 te = s->selected_entry;
5724 else if (view->searching == TOG_SEARCH_FORWARD)
5725 te = got_object_tree_get_first_entry(s->tree);
5726 else
5727 te = got_object_tree_get_last_entry(s->tree);
5730 while (1) {
5731 if (te == NULL) {
5732 if (s->matched_entry == NULL) {
5733 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5734 return NULL;
5736 if (view->searching == TOG_SEARCH_FORWARD)
5737 te = got_object_tree_get_first_entry(s->tree);
5738 else
5739 te = got_object_tree_get_last_entry(s->tree);
5742 if (match_tree_entry(te, &view->regex)) {
5743 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5744 s->matched_entry = te;
5745 break;
5748 if (view->searching == TOG_SEARCH_FORWARD)
5749 te = got_tree_entry_get_next(s->tree, te);
5750 else
5751 te = got_tree_entry_get_prev(s->tree, te);
5754 if (s->matched_entry) {
5755 s->first_displayed_entry = s->matched_entry;
5756 s->selected = 0;
5759 return NULL;
5762 static const struct got_error *
5763 show_tree_view(struct tog_view *view)
5765 const struct got_error *err = NULL;
5766 struct tog_tree_view_state *s = &view->state.tree;
5767 char *parent_path;
5769 err = tree_entry_path(&parent_path, &s->parents, NULL);
5770 if (err)
5771 return err;
5773 err = draw_tree_entries(view, parent_path);
5774 free(parent_path);
5776 view_vborder(view);
5777 return err;
5780 static const struct got_error *
5781 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5783 const struct got_error *err = NULL;
5784 struct tog_tree_view_state *s = &view->state.tree;
5785 struct tog_view *log_view, *ref_view;
5786 struct got_tree_entry *te;
5787 int begin_x = 0, n, nscroll = view->nlines - 3;
5789 switch (ch) {
5790 case 'i':
5791 s->show_ids = !s->show_ids;
5792 break;
5793 case 'l':
5794 if (!s->selected_entry)
5795 break;
5796 if (view_is_parent_view(view))
5797 begin_x = view_split_begin_x(view->begin_x);
5798 err = log_selected_tree_entry(&log_view, begin_x, s);
5799 view->focussed = 0;
5800 log_view->focussed = 1;
5801 if (view_is_parent_view(view)) {
5802 err = view_close_child(view);
5803 if (err)
5804 return err;
5805 view_set_child(view, log_view);
5806 view->focus_child = 1;
5807 } else
5808 *new_view = log_view;
5809 break;
5810 case 'r':
5811 if (view_is_parent_view(view))
5812 begin_x = view_split_begin_x(view->begin_x);
5813 ref_view = view_open(view->nlines, view->ncols,
5814 view->begin_y, begin_x, TOG_VIEW_REF);
5815 if (ref_view == NULL)
5816 return got_error_from_errno("view_open");
5817 err = open_ref_view(ref_view, s->repo);
5818 if (err) {
5819 view_close(ref_view);
5820 return err;
5822 view->focussed = 0;
5823 ref_view->focussed = 1;
5824 if (view_is_parent_view(view)) {
5825 err = view_close_child(view);
5826 if (err)
5827 return err;
5828 view_set_child(view, ref_view);
5829 view->focus_child = 1;
5830 } else
5831 *new_view = ref_view;
5832 break;
5833 case 'g':
5834 case KEY_HOME:
5835 s->selected = 0;
5836 if (s->tree == s->root)
5837 s->first_displayed_entry =
5838 got_object_tree_get_first_entry(s->tree);
5839 else
5840 s->first_displayed_entry = NULL;
5841 break;
5842 case 'G':
5843 case KEY_END:
5844 s->selected = 0;
5845 te = got_object_tree_get_last_entry(s->tree);
5846 for (n = 0; n < view->nlines - 3; n++) {
5847 if (te == NULL) {
5848 if(s->tree != s->root) {
5849 s->first_displayed_entry = NULL;
5850 n++;
5852 break;
5854 s->first_displayed_entry = te;
5855 te = got_tree_entry_get_prev(s->tree, te);
5857 if (n > 0)
5858 s->selected = n - 1;
5859 break;
5860 case 'k':
5861 case KEY_UP:
5862 case CTRL('p'):
5863 if (s->selected > 0) {
5864 s->selected--;
5865 break;
5867 tree_scroll_up(s, 1);
5868 break;
5869 case CTRL('u'):
5870 case 'u':
5871 nscroll /= 2;
5872 /* FALL THROUGH */
5873 case KEY_PPAGE:
5874 case CTRL('b'):
5875 if (s->tree == s->root) {
5876 if (got_object_tree_get_first_entry(s->tree) ==
5877 s->first_displayed_entry)
5878 s->selected -= MIN(s->selected, nscroll);
5879 } else {
5880 if (s->first_displayed_entry == NULL)
5881 s->selected -= MIN(s->selected, nscroll);
5883 tree_scroll_up(s, MAX(0, nscroll));
5884 break;
5885 case 'j':
5886 case KEY_DOWN:
5887 case CTRL('n'):
5888 if (s->selected < s->ndisplayed - 1) {
5889 s->selected++;
5890 break;
5892 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5893 == NULL)
5894 /* can't scroll any further */
5895 break;
5896 tree_scroll_down(s, 1);
5897 break;
5898 case CTRL('d'):
5899 case 'd':
5900 nscroll /= 2;
5901 /* FALL THROUGH */
5902 case KEY_NPAGE:
5903 case CTRL('f'):
5904 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5905 == NULL) {
5906 /* can't scroll any further; move cursor down */
5907 if (s->selected < s->ndisplayed - 1)
5908 s->selected += MIN(nscroll,
5909 s->ndisplayed - s->selected - 1);
5910 break;
5912 tree_scroll_down(s, nscroll);
5913 break;
5914 case KEY_ENTER:
5915 case '\r':
5916 case KEY_BACKSPACE:
5917 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5918 struct tog_parent_tree *parent;
5919 /* user selected '..' */
5920 if (s->tree == s->root)
5921 break;
5922 parent = TAILQ_FIRST(&s->parents);
5923 TAILQ_REMOVE(&s->parents, parent,
5924 entry);
5925 got_object_tree_close(s->tree);
5926 s->tree = parent->tree;
5927 s->first_displayed_entry =
5928 parent->first_displayed_entry;
5929 s->selected_entry =
5930 parent->selected_entry;
5931 s->selected = parent->selected;
5932 free(parent);
5933 } else if (S_ISDIR(got_tree_entry_get_mode(
5934 s->selected_entry))) {
5935 struct got_tree_object *subtree;
5936 err = got_object_open_as_tree(&subtree, s->repo,
5937 got_tree_entry_get_id(s->selected_entry));
5938 if (err)
5939 break;
5940 err = tree_view_visit_subtree(s, subtree);
5941 if (err) {
5942 got_object_tree_close(subtree);
5943 break;
5945 } else if (S_ISREG(got_tree_entry_get_mode(
5946 s->selected_entry))) {
5947 struct tog_view *blame_view;
5948 int begin_x = view_is_parent_view(view) ?
5949 view_split_begin_x(view->begin_x) : 0;
5951 err = blame_tree_entry(&blame_view, begin_x,
5952 s->selected_entry, &s->parents,
5953 s->commit_id, s->repo);
5954 if (err)
5955 break;
5956 view->focussed = 0;
5957 blame_view->focussed = 1;
5958 if (view_is_parent_view(view)) {
5959 err = view_close_child(view);
5960 if (err)
5961 return err;
5962 view_set_child(view, blame_view);
5963 view->focus_child = 1;
5964 } else
5965 *new_view = blame_view;
5967 break;
5968 case KEY_RESIZE:
5969 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5970 s->selected = view->nlines - 4;
5971 break;
5972 default:
5973 break;
5976 return err;
5979 __dead static void
5980 usage_tree(void)
5982 endwin();
5983 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5984 getprogname());
5985 exit(1);
5988 static const struct got_error *
5989 cmd_tree(int argc, char *argv[])
5991 const struct got_error *error;
5992 struct got_repository *repo = NULL;
5993 struct got_worktree *worktree = NULL;
5994 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5995 struct got_object_id *commit_id = NULL;
5996 struct got_commit_object *commit = NULL;
5997 const char *commit_id_arg = NULL;
5998 char *label = NULL;
5999 struct got_reference *ref = NULL;
6000 const char *head_ref_name = NULL;
6001 int ch;
6002 struct tog_view *view;
6003 int *pack_fds = NULL;
6005 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6006 switch (ch) {
6007 case 'c':
6008 commit_id_arg = optarg;
6009 break;
6010 case 'r':
6011 repo_path = realpath(optarg, NULL);
6012 if (repo_path == NULL)
6013 return got_error_from_errno2("realpath",
6014 optarg);
6015 break;
6016 default:
6017 usage_tree();
6018 /* NOTREACHED */
6022 argc -= optind;
6023 argv += optind;
6025 if (argc > 1)
6026 usage_tree();
6028 error = got_repo_pack_fds_open(&pack_fds);
6029 if (error != NULL)
6030 goto done;
6032 if (repo_path == NULL) {
6033 cwd = getcwd(NULL, 0);
6034 if (cwd == NULL)
6035 return got_error_from_errno("getcwd");
6036 error = got_worktree_open(&worktree, cwd);
6037 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6038 goto done;
6039 if (worktree)
6040 repo_path =
6041 strdup(got_worktree_get_repo_path(worktree));
6042 else
6043 repo_path = strdup(cwd);
6044 if (repo_path == NULL) {
6045 error = got_error_from_errno("strdup");
6046 goto done;
6050 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6051 if (error != NULL)
6052 goto done;
6054 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6055 repo, worktree);
6056 if (error)
6057 goto done;
6059 init_curses();
6061 error = apply_unveil(got_repo_get_path(repo), NULL);
6062 if (error)
6063 goto done;
6065 error = tog_load_refs(repo, 0);
6066 if (error)
6067 goto done;
6069 if (commit_id_arg == NULL) {
6070 error = got_repo_match_object_id(&commit_id, &label,
6071 worktree ? got_worktree_get_head_ref_name(worktree) :
6072 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6073 if (error)
6074 goto done;
6075 head_ref_name = label;
6076 } else {
6077 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6078 if (error == NULL)
6079 head_ref_name = got_ref_get_name(ref);
6080 else if (error->code != GOT_ERR_NOT_REF)
6081 goto done;
6082 error = got_repo_match_object_id(&commit_id, NULL,
6083 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6084 if (error)
6085 goto done;
6088 error = got_object_open_as_commit(&commit, repo, commit_id);
6089 if (error)
6090 goto done;
6092 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6093 if (view == NULL) {
6094 error = got_error_from_errno("view_open");
6095 goto done;
6097 error = open_tree_view(view, commit_id, head_ref_name, repo);
6098 if (error)
6099 goto done;
6100 if (!got_path_is_root_dir(in_repo_path)) {
6101 error = tree_view_walk_path(&view->state.tree, commit,
6102 in_repo_path);
6103 if (error)
6104 goto done;
6107 if (worktree) {
6108 /* Release work tree lock. */
6109 got_worktree_close(worktree);
6110 worktree = NULL;
6112 error = view_loop(view);
6113 done:
6114 free(repo_path);
6115 free(cwd);
6116 free(commit_id);
6117 free(label);
6118 if (ref)
6119 got_ref_close(ref);
6120 if (repo) {
6121 const struct got_error *close_err = got_repo_close(repo);
6122 if (error == NULL)
6123 error = close_err;
6125 if (pack_fds) {
6126 const struct got_error *pack_err =
6127 got_repo_pack_fds_close(pack_fds);
6128 if (error == NULL)
6129 error = pack_err;
6131 tog_free_refs();
6132 return error;
6135 static const struct got_error *
6136 ref_view_load_refs(struct tog_ref_view_state *s)
6138 struct got_reflist_entry *sre;
6139 struct tog_reflist_entry *re;
6141 s->nrefs = 0;
6142 TAILQ_FOREACH(sre, &tog_refs, entry) {
6143 if (strncmp(got_ref_get_name(sre->ref),
6144 "refs/got/", 9) == 0 &&
6145 strncmp(got_ref_get_name(sre->ref),
6146 "refs/got/backup/", 16) != 0)
6147 continue;
6149 re = malloc(sizeof(*re));
6150 if (re == NULL)
6151 return got_error_from_errno("malloc");
6153 re->ref = got_ref_dup(sre->ref);
6154 if (re->ref == NULL)
6155 return got_error_from_errno("got_ref_dup");
6156 re->idx = s->nrefs++;
6157 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6160 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6161 return NULL;
6164 void
6165 ref_view_free_refs(struct tog_ref_view_state *s)
6167 struct tog_reflist_entry *re;
6169 while (!TAILQ_EMPTY(&s->refs)) {
6170 re = TAILQ_FIRST(&s->refs);
6171 TAILQ_REMOVE(&s->refs, re, entry);
6172 got_ref_close(re->ref);
6173 free(re);
6177 static const struct got_error *
6178 open_ref_view(struct tog_view *view, struct got_repository *repo)
6180 const struct got_error *err = NULL;
6181 struct tog_ref_view_state *s = &view->state.ref;
6183 s->selected_entry = 0;
6184 s->repo = repo;
6186 TAILQ_INIT(&s->refs);
6187 STAILQ_INIT(&s->colors);
6189 err = ref_view_load_refs(s);
6190 if (err)
6191 return err;
6193 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6194 err = add_color(&s->colors, "^refs/heads/",
6195 TOG_COLOR_REFS_HEADS,
6196 get_color_value("TOG_COLOR_REFS_HEADS"));
6197 if (err)
6198 goto done;
6200 err = add_color(&s->colors, "^refs/tags/",
6201 TOG_COLOR_REFS_TAGS,
6202 get_color_value("TOG_COLOR_REFS_TAGS"));
6203 if (err)
6204 goto done;
6206 err = add_color(&s->colors, "^refs/remotes/",
6207 TOG_COLOR_REFS_REMOTES,
6208 get_color_value("TOG_COLOR_REFS_REMOTES"));
6209 if (err)
6210 goto done;
6212 err = add_color(&s->colors, "^refs/got/backup/",
6213 TOG_COLOR_REFS_BACKUP,
6214 get_color_value("TOG_COLOR_REFS_BACKUP"));
6215 if (err)
6216 goto done;
6219 view->show = show_ref_view;
6220 view->input = input_ref_view;
6221 view->close = close_ref_view;
6222 view->search_start = search_start_ref_view;
6223 view->search_next = search_next_ref_view;
6224 done:
6225 if (err)
6226 free_colors(&s->colors);
6227 return err;
6230 static const struct got_error *
6231 close_ref_view(struct tog_view *view)
6233 struct tog_ref_view_state *s = &view->state.ref;
6235 ref_view_free_refs(s);
6236 free_colors(&s->colors);
6238 return NULL;
6241 static const struct got_error *
6242 resolve_reflist_entry(struct got_object_id **commit_id,
6243 struct tog_reflist_entry *re, struct got_repository *repo)
6245 const struct got_error *err = NULL;
6246 struct got_object_id *obj_id;
6247 struct got_tag_object *tag = NULL;
6248 int obj_type;
6250 *commit_id = NULL;
6252 err = got_ref_resolve(&obj_id, repo, re->ref);
6253 if (err)
6254 return err;
6256 err = got_object_get_type(&obj_type, repo, obj_id);
6257 if (err)
6258 goto done;
6260 switch (obj_type) {
6261 case GOT_OBJ_TYPE_COMMIT:
6262 *commit_id = obj_id;
6263 break;
6264 case GOT_OBJ_TYPE_TAG:
6265 err = got_object_open_as_tag(&tag, repo, obj_id);
6266 if (err)
6267 goto done;
6268 free(obj_id);
6269 err = got_object_get_type(&obj_type, repo,
6270 got_object_tag_get_object_id(tag));
6271 if (err)
6272 goto done;
6273 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6274 err = got_error(GOT_ERR_OBJ_TYPE);
6275 goto done;
6277 *commit_id = got_object_id_dup(
6278 got_object_tag_get_object_id(tag));
6279 if (*commit_id == NULL) {
6280 err = got_error_from_errno("got_object_id_dup");
6281 goto done;
6283 break;
6284 default:
6285 err = got_error(GOT_ERR_OBJ_TYPE);
6286 break;
6289 done:
6290 if (tag)
6291 got_object_tag_close(tag);
6292 if (err) {
6293 free(*commit_id);
6294 *commit_id = NULL;
6296 return err;
6299 static const struct got_error *
6300 log_ref_entry(struct tog_view **new_view, int begin_x,
6301 struct tog_reflist_entry *re, struct got_repository *repo)
6303 struct tog_view *log_view;
6304 const struct got_error *err = NULL;
6305 struct got_object_id *commit_id = NULL;
6307 *new_view = NULL;
6309 err = resolve_reflist_entry(&commit_id, re, repo);
6310 if (err) {
6311 if (err->code != GOT_ERR_OBJ_TYPE)
6312 return err;
6313 else
6314 return NULL;
6317 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6318 if (log_view == NULL) {
6319 err = got_error_from_errno("view_open");
6320 goto done;
6323 err = open_log_view(log_view, commit_id, repo,
6324 got_ref_get_name(re->ref), "", 0);
6325 done:
6326 if (err)
6327 view_close(log_view);
6328 else
6329 *new_view = log_view;
6330 free(commit_id);
6331 return err;
6334 static void
6335 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6337 struct tog_reflist_entry *re;
6338 int i = 0;
6340 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6341 return;
6343 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6344 while (i++ < maxscroll) {
6345 if (re == NULL)
6346 break;
6347 s->first_displayed_entry = re;
6348 re = TAILQ_PREV(re, tog_reflist_head, entry);
6352 static void
6353 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6355 struct tog_reflist_entry *next, *last;
6356 int n = 0;
6358 if (s->first_displayed_entry)
6359 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6360 else
6361 next = TAILQ_FIRST(&s->refs);
6363 last = s->last_displayed_entry;
6364 while (next && last && n++ < maxscroll) {
6365 last = TAILQ_NEXT(last, entry);
6366 if (last) {
6367 s->first_displayed_entry = next;
6368 next = TAILQ_NEXT(next, entry);
6373 static const struct got_error *
6374 search_start_ref_view(struct tog_view *view)
6376 struct tog_ref_view_state *s = &view->state.ref;
6378 s->matched_entry = NULL;
6379 return NULL;
6382 static int
6383 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6385 regmatch_t regmatch;
6387 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6388 0) == 0;
6391 static const struct got_error *
6392 search_next_ref_view(struct tog_view *view)
6394 struct tog_ref_view_state *s = &view->state.ref;
6395 struct tog_reflist_entry *re = NULL;
6397 if (!view->searching) {
6398 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6399 return NULL;
6402 if (s->matched_entry) {
6403 if (view->searching == TOG_SEARCH_FORWARD) {
6404 if (s->selected_entry)
6405 re = TAILQ_NEXT(s->selected_entry, entry);
6406 else
6407 re = TAILQ_PREV(s->selected_entry,
6408 tog_reflist_head, entry);
6409 } else {
6410 if (s->selected_entry == NULL)
6411 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6412 else
6413 re = TAILQ_PREV(s->selected_entry,
6414 tog_reflist_head, entry);
6416 } else {
6417 if (s->selected_entry)
6418 re = s->selected_entry;
6419 else if (view->searching == TOG_SEARCH_FORWARD)
6420 re = TAILQ_FIRST(&s->refs);
6421 else
6422 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6425 while (1) {
6426 if (re == NULL) {
6427 if (s->matched_entry == NULL) {
6428 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6429 return NULL;
6431 if (view->searching == TOG_SEARCH_FORWARD)
6432 re = TAILQ_FIRST(&s->refs);
6433 else
6434 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6437 if (match_reflist_entry(re, &view->regex)) {
6438 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6439 s->matched_entry = re;
6440 break;
6443 if (view->searching == TOG_SEARCH_FORWARD)
6444 re = TAILQ_NEXT(re, entry);
6445 else
6446 re = TAILQ_PREV(re, tog_reflist_head, entry);
6449 if (s->matched_entry) {
6450 s->first_displayed_entry = s->matched_entry;
6451 s->selected = 0;
6454 return NULL;
6457 static const struct got_error *
6458 show_ref_view(struct tog_view *view)
6460 const struct got_error *err = NULL;
6461 struct tog_ref_view_state *s = &view->state.ref;
6462 struct tog_reflist_entry *re;
6463 char *line = NULL;
6464 wchar_t *wline;
6465 struct tog_color *tc;
6466 int width, n;
6467 int limit = view->nlines;
6469 werase(view->window);
6471 s->ndisplayed = 0;
6473 if (limit == 0)
6474 return NULL;
6476 re = s->first_displayed_entry;
6478 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6479 s->nrefs) == -1)
6480 return got_error_from_errno("asprintf");
6482 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6483 if (err) {
6484 free(line);
6485 return err;
6487 if (view_needs_focus_indication(view))
6488 wstandout(view->window);
6489 waddwstr(view->window, wline);
6490 if (view_needs_focus_indication(view))
6491 wstandend(view->window);
6492 free(wline);
6493 wline = NULL;
6494 free(line);
6495 line = NULL;
6496 if (width < view->ncols - 1)
6497 waddch(view->window, '\n');
6498 if (--limit <= 0)
6499 return NULL;
6501 n = 0;
6502 while (re && limit > 0) {
6503 char *line = NULL;
6504 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6506 if (s->show_date) {
6507 struct got_commit_object *ci;
6508 struct got_tag_object *tag;
6509 struct got_object_id *id;
6510 struct tm tm;
6511 time_t t;
6513 err = got_ref_resolve(&id, s->repo, re->ref);
6514 if (err)
6515 return err;
6516 err = got_object_open_as_tag(&tag, s->repo, id);
6517 if (err) {
6518 if (err->code != GOT_ERR_OBJ_TYPE) {
6519 free(id);
6520 return err;
6522 err = got_object_open_as_commit(&ci, s->repo,
6523 id);
6524 if (err) {
6525 free(id);
6526 return err;
6528 t = got_object_commit_get_committer_time(ci);
6529 got_object_commit_close(ci);
6530 } else {
6531 t = got_object_tag_get_tagger_time(tag);
6532 got_object_tag_close(tag);
6534 free(id);
6535 if (gmtime_r(&t, &tm) == NULL)
6536 return got_error_from_errno("gmtime_r");
6537 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6538 return got_error(GOT_ERR_NO_SPACE);
6540 if (got_ref_is_symbolic(re->ref)) {
6541 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6542 ymd : "", got_ref_get_name(re->ref),
6543 got_ref_get_symref_target(re->ref)) == -1)
6544 return got_error_from_errno("asprintf");
6545 } else if (s->show_ids) {
6546 struct got_object_id *id;
6547 char *id_str;
6548 err = got_ref_resolve(&id, s->repo, re->ref);
6549 if (err)
6550 return err;
6551 err = got_object_id_str(&id_str, id);
6552 if (err) {
6553 free(id);
6554 return err;
6556 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6557 got_ref_get_name(re->ref), id_str) == -1) {
6558 err = got_error_from_errno("asprintf");
6559 free(id);
6560 free(id_str);
6561 return err;
6563 free(id);
6564 free(id_str);
6565 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6566 got_ref_get_name(re->ref)) == -1)
6567 return got_error_from_errno("asprintf");
6569 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6570 if (err) {
6571 free(line);
6572 return err;
6574 if (n == s->selected) {
6575 if (view->focussed)
6576 wstandout(view->window);
6577 s->selected_entry = re;
6579 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6580 if (tc)
6581 wattr_on(view->window,
6582 COLOR_PAIR(tc->colorpair), NULL);
6583 waddwstr(view->window, wline);
6584 if (tc)
6585 wattr_off(view->window,
6586 COLOR_PAIR(tc->colorpair), NULL);
6587 if (width < view->ncols - 1)
6588 waddch(view->window, '\n');
6589 if (n == s->selected && view->focussed)
6590 wstandend(view->window);
6591 free(line);
6592 free(wline);
6593 wline = NULL;
6594 n++;
6595 s->ndisplayed++;
6596 s->last_displayed_entry = re;
6598 limit--;
6599 re = TAILQ_NEXT(re, entry);
6602 view_vborder(view);
6603 return err;
6606 static const struct got_error *
6607 browse_ref_tree(struct tog_view **new_view, int begin_x,
6608 struct tog_reflist_entry *re, struct got_repository *repo)
6610 const struct got_error *err = NULL;
6611 struct got_object_id *commit_id = NULL;
6612 struct tog_view *tree_view;
6614 *new_view = NULL;
6616 err = resolve_reflist_entry(&commit_id, re, repo);
6617 if (err) {
6618 if (err->code != GOT_ERR_OBJ_TYPE)
6619 return err;
6620 else
6621 return NULL;
6625 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6626 if (tree_view == NULL) {
6627 err = got_error_from_errno("view_open");
6628 goto done;
6631 err = open_tree_view(tree_view, commit_id,
6632 got_ref_get_name(re->ref), repo);
6633 if (err)
6634 goto done;
6636 *new_view = tree_view;
6637 done:
6638 free(commit_id);
6639 return err;
6641 static const struct got_error *
6642 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6644 const struct got_error *err = NULL;
6645 struct tog_ref_view_state *s = &view->state.ref;
6646 struct tog_view *log_view, *tree_view;
6647 struct tog_reflist_entry *re;
6648 int begin_x = 0, n, nscroll = view->nlines - 1;
6650 switch (ch) {
6651 case 'i':
6652 s->show_ids = !s->show_ids;
6653 break;
6654 case 'm':
6655 s->show_date = !s->show_date;
6656 break;
6657 case 'o':
6658 s->sort_by_date = !s->sort_by_date;
6659 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6660 got_ref_cmp_by_commit_timestamp_descending :
6661 tog_ref_cmp_by_name, s->repo);
6662 if (err)
6663 break;
6664 got_reflist_object_id_map_free(tog_refs_idmap);
6665 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6666 &tog_refs, s->repo);
6667 if (err)
6668 break;
6669 ref_view_free_refs(s);
6670 err = ref_view_load_refs(s);
6671 break;
6672 case KEY_ENTER:
6673 case '\r':
6674 if (!s->selected_entry)
6675 break;
6676 if (view_is_parent_view(view))
6677 begin_x = view_split_begin_x(view->begin_x);
6678 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6679 s->repo);
6680 view->focussed = 0;
6681 log_view->focussed = 1;
6682 if (view_is_parent_view(view)) {
6683 err = view_close_child(view);
6684 if (err)
6685 return err;
6686 view_set_child(view, log_view);
6687 view->focus_child = 1;
6688 } else
6689 *new_view = log_view;
6690 break;
6691 case 't':
6692 if (!s->selected_entry)
6693 break;
6694 if (view_is_parent_view(view))
6695 begin_x = view_split_begin_x(view->begin_x);
6696 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6697 s->repo);
6698 if (err || tree_view == NULL)
6699 break;
6700 view->focussed = 0;
6701 tree_view->focussed = 1;
6702 if (view_is_parent_view(view)) {
6703 err = view_close_child(view);
6704 if (err)
6705 return err;
6706 view_set_child(view, tree_view);
6707 view->focus_child = 1;
6708 } else
6709 *new_view = tree_view;
6710 break;
6711 case 'g':
6712 case KEY_HOME:
6713 s->selected = 0;
6714 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6715 break;
6716 case 'G':
6717 case KEY_END:
6718 s->selected = 0;
6719 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6720 for (n = 0; n < view->nlines - 1; n++) {
6721 if (re == NULL)
6722 break;
6723 s->first_displayed_entry = re;
6724 re = TAILQ_PREV(re, tog_reflist_head, entry);
6726 if (n > 0)
6727 s->selected = n - 1;
6728 break;
6729 case 'k':
6730 case KEY_UP:
6731 case CTRL('p'):
6732 if (s->selected > 0) {
6733 s->selected--;
6734 break;
6736 ref_scroll_up(s, 1);
6737 break;
6738 case CTRL('u'):
6739 case 'u':
6740 nscroll /= 2;
6741 /* FALL THROUGH */
6742 case KEY_PPAGE:
6743 case CTRL('b'):
6744 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6745 s->selected -= MIN(nscroll, s->selected);
6746 ref_scroll_up(s, MAX(0, nscroll));
6747 break;
6748 case 'j':
6749 case KEY_DOWN:
6750 case CTRL('n'):
6751 if (s->selected < s->ndisplayed - 1) {
6752 s->selected++;
6753 break;
6755 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6756 /* can't scroll any further */
6757 break;
6758 ref_scroll_down(s, 1);
6759 break;
6760 case CTRL('d'):
6761 case 'd':
6762 nscroll /= 2;
6763 /* FALL THROUGH */
6764 case KEY_NPAGE:
6765 case CTRL('f'):
6766 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6767 /* can't scroll any further; move cursor down */
6768 if (s->selected < s->ndisplayed - 1)
6769 s->selected += MIN(nscroll,
6770 s->ndisplayed - s->selected - 1);
6771 break;
6773 ref_scroll_down(s, nscroll);
6774 break;
6775 case CTRL('l'):
6776 tog_free_refs();
6777 err = tog_load_refs(s->repo, s->sort_by_date);
6778 if (err)
6779 break;
6780 ref_view_free_refs(s);
6781 err = ref_view_load_refs(s);
6782 break;
6783 case KEY_RESIZE:
6784 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6785 s->selected = view->nlines - 2;
6786 break;
6787 default:
6788 break;
6791 return err;
6794 __dead static void
6795 usage_ref(void)
6797 endwin();
6798 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6799 getprogname());
6800 exit(1);
6803 static const struct got_error *
6804 cmd_ref(int argc, char *argv[])
6806 const struct got_error *error;
6807 struct got_repository *repo = NULL;
6808 struct got_worktree *worktree = NULL;
6809 char *cwd = NULL, *repo_path = NULL;
6810 int ch;
6811 struct tog_view *view;
6812 int *pack_fds = NULL;
6814 while ((ch = getopt(argc, argv, "r:")) != -1) {
6815 switch (ch) {
6816 case 'r':
6817 repo_path = realpath(optarg, NULL);
6818 if (repo_path == NULL)
6819 return got_error_from_errno2("realpath",
6820 optarg);
6821 break;
6822 default:
6823 usage_ref();
6824 /* NOTREACHED */
6828 argc -= optind;
6829 argv += optind;
6831 if (argc > 1)
6832 usage_ref();
6834 error = got_repo_pack_fds_open(&pack_fds);
6835 if (error != NULL)
6836 goto done;
6838 if (repo_path == NULL) {
6839 cwd = getcwd(NULL, 0);
6840 if (cwd == NULL)
6841 return got_error_from_errno("getcwd");
6842 error = got_worktree_open(&worktree, cwd);
6843 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6844 goto done;
6845 if (worktree)
6846 repo_path =
6847 strdup(got_worktree_get_repo_path(worktree));
6848 else
6849 repo_path = strdup(cwd);
6850 if (repo_path == NULL) {
6851 error = got_error_from_errno("strdup");
6852 goto done;
6856 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6857 if (error != NULL)
6858 goto done;
6860 init_curses();
6862 error = apply_unveil(got_repo_get_path(repo), NULL);
6863 if (error)
6864 goto done;
6866 error = tog_load_refs(repo, 0);
6867 if (error)
6868 goto done;
6870 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6871 if (view == NULL) {
6872 error = got_error_from_errno("view_open");
6873 goto done;
6876 error = open_ref_view(view, repo);
6877 if (error)
6878 goto done;
6880 if (worktree) {
6881 /* Release work tree lock. */
6882 got_worktree_close(worktree);
6883 worktree = NULL;
6885 error = view_loop(view);
6886 done:
6887 free(repo_path);
6888 free(cwd);
6889 if (repo) {
6890 const struct got_error *close_err = got_repo_close(repo);
6891 if (close_err)
6892 error = close_err;
6894 if (pack_fds) {
6895 const struct got_error *pack_err =
6896 got_repo_pack_fds_close(pack_fds);
6897 if (error == NULL)
6898 error = pack_err;
6900 tog_free_refs();
6901 return error;
6904 static void
6905 list_commands(FILE *fp)
6907 size_t i;
6909 fprintf(fp, "commands:");
6910 for (i = 0; i < nitems(tog_commands); i++) {
6911 const struct tog_cmd *cmd = &tog_commands[i];
6912 fprintf(fp, " %s", cmd->name);
6914 fputc('\n', fp);
6917 __dead static void
6918 usage(int hflag, int status)
6920 FILE *fp = (status == 0) ? stdout : stderr;
6922 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6923 getprogname());
6924 if (hflag) {
6925 fprintf(fp, "lazy usage: %s path\n", getprogname());
6926 list_commands(fp);
6928 exit(status);
6931 static char **
6932 make_argv(int argc, ...)
6934 va_list ap;
6935 char **argv;
6936 int i;
6938 va_start(ap, argc);
6940 argv = calloc(argc, sizeof(char *));
6941 if (argv == NULL)
6942 err(1, "calloc");
6943 for (i = 0; i < argc; i++) {
6944 argv[i] = strdup(va_arg(ap, char *));
6945 if (argv[i] == NULL)
6946 err(1, "strdup");
6949 va_end(ap);
6950 return argv;
6954 * Try to convert 'tog path' into a 'tog log path' command.
6955 * The user could simply have mistyped the command rather than knowingly
6956 * provided a path. So check whether argv[0] can in fact be resolved
6957 * to a path in the HEAD commit and print a special error if not.
6958 * This hack is for mpi@ <3
6960 static const struct got_error *
6961 tog_log_with_path(int argc, char *argv[])
6963 const struct got_error *error = NULL, *close_err;
6964 const struct tog_cmd *cmd = NULL;
6965 struct got_repository *repo = NULL;
6966 struct got_worktree *worktree = NULL;
6967 struct got_object_id *commit_id = NULL, *id = NULL;
6968 struct got_commit_object *commit = NULL;
6969 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6970 char *commit_id_str = NULL, **cmd_argv = NULL;
6971 int *pack_fds = NULL;
6973 cwd = getcwd(NULL, 0);
6974 if (cwd == NULL)
6975 return got_error_from_errno("getcwd");
6977 error = got_repo_pack_fds_open(&pack_fds);
6978 if (error != NULL)
6979 goto done;
6981 error = got_worktree_open(&worktree, cwd);
6982 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6983 goto done;
6985 if (worktree)
6986 repo_path = strdup(got_worktree_get_repo_path(worktree));
6987 else
6988 repo_path = strdup(cwd);
6989 if (repo_path == NULL) {
6990 error = got_error_from_errno("strdup");
6991 goto done;
6994 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6995 if (error != NULL)
6996 goto done;
6998 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6999 repo, worktree);
7000 if (error)
7001 goto done;
7003 error = tog_load_refs(repo, 0);
7004 if (error)
7005 goto done;
7006 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7007 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7008 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7009 if (error)
7010 goto done;
7012 if (worktree) {
7013 got_worktree_close(worktree);
7014 worktree = NULL;
7017 error = got_object_open_as_commit(&commit, repo, commit_id);
7018 if (error)
7019 goto done;
7021 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7022 if (error) {
7023 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7024 goto done;
7025 fprintf(stderr, "%s: '%s' is no known command or path\n",
7026 getprogname(), argv[0]);
7027 usage(1, 1);
7028 /* not reached */
7031 close_err = got_repo_close(repo);
7032 if (error == NULL)
7033 error = close_err;
7034 repo = NULL;
7036 error = got_object_id_str(&commit_id_str, commit_id);
7037 if (error)
7038 goto done;
7040 cmd = &tog_commands[0]; /* log */
7041 argc = 4;
7042 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7043 error = cmd->cmd_main(argc, cmd_argv);
7044 done:
7045 if (repo) {
7046 close_err = got_repo_close(repo);
7047 if (error == NULL)
7048 error = close_err;
7050 if (commit)
7051 got_object_commit_close(commit);
7052 if (worktree)
7053 got_worktree_close(worktree);
7054 if (pack_fds) {
7055 const struct got_error *pack_err =
7056 got_repo_pack_fds_close(pack_fds);
7057 if (error == NULL)
7058 error = pack_err;
7060 free(id);
7061 free(commit_id_str);
7062 free(commit_id);
7063 free(cwd);
7064 free(repo_path);
7065 free(in_repo_path);
7066 if (cmd_argv) {
7067 int i;
7068 for (i = 0; i < argc; i++)
7069 free(cmd_argv[i]);
7070 free(cmd_argv);
7072 tog_free_refs();
7073 return error;
7076 int
7077 main(int argc, char *argv[])
7079 const struct got_error *error = NULL;
7080 const struct tog_cmd *cmd = NULL;
7081 int ch, hflag = 0, Vflag = 0;
7082 char **cmd_argv = NULL;
7083 static const struct option longopts[] = {
7084 { "version", no_argument, NULL, 'V' },
7085 { NULL, 0, NULL, 0}
7088 setlocale(LC_CTYPE, "");
7090 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7091 switch (ch) {
7092 case 'h':
7093 hflag = 1;
7094 break;
7095 case 'V':
7096 Vflag = 1;
7097 break;
7098 default:
7099 usage(hflag, 1);
7100 /* NOTREACHED */
7104 argc -= optind;
7105 argv += optind;
7106 optind = 1;
7107 optreset = 1;
7109 if (Vflag) {
7110 got_version_print_str();
7111 return 0;
7114 #ifndef PROFILE
7115 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7116 NULL) == -1)
7117 err(1, "pledge");
7118 #endif
7120 if (argc == 0) {
7121 if (hflag)
7122 usage(hflag, 0);
7123 /* Build an argument vector which runs a default command. */
7124 cmd = &tog_commands[0];
7125 argc = 1;
7126 cmd_argv = make_argv(argc, cmd->name);
7127 } else {
7128 size_t i;
7130 /* Did the user specify a command? */
7131 for (i = 0; i < nitems(tog_commands); i++) {
7132 if (strncmp(tog_commands[i].name, argv[0],
7133 strlen(argv[0])) == 0) {
7134 cmd = &tog_commands[i];
7135 break;
7140 if (cmd == NULL) {
7141 if (argc != 1)
7142 usage(0, 1);
7143 /* No command specified; try log with a path */
7144 error = tog_log_with_path(argc, argv);
7145 } else {
7146 if (hflag)
7147 cmd->cmd_usage();
7148 else
7149 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7152 endwin();
7153 putchar('\n');
7154 if (cmd_argv) {
7155 int i;
7156 for (i = 0; i < argc; i++)
7157 free(cmd_argv[i]);
7158 free(cmd_argv);
7161 if (error && error->code != GOT_ERR_CANCELLED)
7162 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7163 return 0;