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);
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 char *p;
1253 p = realloc(dst, n + nb);
1254 if (p == NULL) {
1255 free(dst);
1256 return got_error_from_errno("realloc");
1259 dst = p;
1260 n += nb;
1261 memset(dst + sz, ' ', nb);
1262 sz += nb;
1263 } else
1264 dst[sz++] = src[idx];
1265 ++idx;
1268 dst[sz] = '\0';
1269 *ptr = dst;
1270 return NULL;
1274 * Skip leading nscroll columns of a wide character string.
1275 * Returns the index to the first character of the scrolled string.
1277 static const struct got_error *
1278 scroll_wline(int *scrollx , wchar_t *wline, int nscroll,
1279 int col_tab_align)
1281 int cols = 0;
1282 size_t wlen = wcslen(wline);
1283 int i = 0;
1285 *scrollx = 0;
1287 while (i < wlen && cols < nscroll) {
1288 int width = wcwidth(wline[i]);
1290 if (width == 0) {
1291 i++;
1292 continue;
1295 if (width == 1 || width == 2) {
1296 if (cols + width > nscroll)
1297 break;
1298 cols += width;
1299 i++;
1300 } else if (width == -1) {
1301 if (wline[i] == L'\t') {
1302 width = TABSIZE -
1303 ((cols + col_tab_align) % TABSIZE);
1304 } else {
1305 width = 1;
1306 wline[i] = L'.';
1308 if (cols + width > nscroll)
1309 break;
1310 cols += width;
1311 i++;
1312 } else
1313 return got_error_from_errno("wcwidth");
1316 *scrollx = i;
1317 return NULL;
1321 * Format a line for display, ensuring that it won't overflow a width limit.
1322 * With scrolling, the width returned refers to the scrolled version of the
1323 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1325 static const struct got_error *
1326 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1327 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1329 const struct got_error *err = NULL;
1330 int cols = 0;
1331 wchar_t *wline = NULL;
1332 char *exstr = NULL;
1333 size_t wlen;
1334 int i, scrollx = 0;
1336 *wlinep = NULL;
1337 *widthp = 0;
1339 if (expand) {
1340 err = expand_tab(&exstr, line);
1341 if (err)
1342 return err;
1345 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1346 free(exstr);
1347 if (err)
1348 return err;
1350 err = scroll_wline(&scrollx, wline, nscroll, col_tab_align);
1351 if (err)
1352 goto done;
1354 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1355 wline[wlen - 1] = L'\0';
1356 wlen--;
1358 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1359 wline[wlen - 1] = L'\0';
1360 wlen--;
1363 i = scrollx;
1364 while (i < wlen) {
1365 int width = wcwidth(wline[i]);
1367 if (width == 0) {
1368 i++;
1369 continue;
1372 if (width == 1 || width == 2) {
1373 if (cols + width > wlimit)
1374 break;
1375 cols += width;
1376 i++;
1377 } else if (width == -1) {
1378 if (wline[i] == L'\t') {
1379 width = TABSIZE -
1380 ((cols + col_tab_align) % TABSIZE);
1381 } else {
1382 width = 1;
1383 wline[i] = L'.';
1385 if (cols + width > wlimit)
1386 break;
1387 cols += width;
1388 i++;
1389 } else {
1390 err = got_error_from_errno("wcwidth");
1391 goto done;
1394 wline[i] = L'\0';
1395 if (widthp)
1396 *widthp = cols;
1397 if (scrollxp)
1398 *scrollxp = scrollx;
1399 done:
1400 if (err)
1401 free(wline);
1402 else
1403 *wlinep = wline;
1404 return err;
1407 static const struct got_error*
1408 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1409 struct got_object_id *id, struct got_repository *repo)
1411 static const struct got_error *err = NULL;
1412 struct got_reflist_entry *re;
1413 char *s;
1414 const char *name;
1416 *refs_str = NULL;
1418 TAILQ_FOREACH(re, refs, entry) {
1419 struct got_tag_object *tag = NULL;
1420 struct got_object_id *ref_id;
1421 int cmp;
1423 name = got_ref_get_name(re->ref);
1424 if (strcmp(name, GOT_REF_HEAD) == 0)
1425 continue;
1426 if (strncmp(name, "refs/", 5) == 0)
1427 name += 5;
1428 if (strncmp(name, "got/", 4) == 0 &&
1429 strncmp(name, "got/backup/", 11) != 0)
1430 continue;
1431 if (strncmp(name, "heads/", 6) == 0)
1432 name += 6;
1433 if (strncmp(name, "remotes/", 8) == 0) {
1434 name += 8;
1435 s = strstr(name, "/" GOT_REF_HEAD);
1436 if (s != NULL && s[strlen(s)] == '\0')
1437 continue;
1439 err = got_ref_resolve(&ref_id, repo, re->ref);
1440 if (err)
1441 break;
1442 if (strncmp(name, "tags/", 5) == 0) {
1443 err = got_object_open_as_tag(&tag, repo, ref_id);
1444 if (err) {
1445 if (err->code != GOT_ERR_OBJ_TYPE) {
1446 free(ref_id);
1447 break;
1449 /* Ref points at something other than a tag. */
1450 err = NULL;
1451 tag = NULL;
1454 cmp = got_object_id_cmp(tag ?
1455 got_object_tag_get_object_id(tag) : ref_id, id);
1456 free(ref_id);
1457 if (tag)
1458 got_object_tag_close(tag);
1459 if (cmp != 0)
1460 continue;
1461 s = *refs_str;
1462 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1463 s ? ", " : "", name) == -1) {
1464 err = got_error_from_errno("asprintf");
1465 free(s);
1466 *refs_str = NULL;
1467 break;
1469 free(s);
1472 return err;
1475 static const struct got_error *
1476 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1477 int col_tab_align)
1479 char *smallerthan;
1481 smallerthan = strchr(author, '<');
1482 if (smallerthan && smallerthan[1] != '\0')
1483 author = smallerthan + 1;
1484 author[strcspn(author, "@>")] = '\0';
1485 return format_line(wauthor, author_width, NULL, author, 0, limit,
1486 col_tab_align, 0);
1489 static const struct got_error *
1490 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1491 struct got_object_id *id, const size_t date_display_cols,
1492 int author_display_cols)
1494 struct tog_log_view_state *s = &view->state.log;
1495 const struct got_error *err = NULL;
1496 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1497 char *logmsg0 = NULL, *logmsg = NULL;
1498 char *author = NULL;
1499 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1500 int author_width, logmsg_width;
1501 char *newline, *line = NULL;
1502 int col, limit, scrollx;
1503 const int avail = view->ncols;
1504 struct tm tm;
1505 time_t committer_time;
1506 struct tog_color *tc;
1508 committer_time = got_object_commit_get_committer_time(commit);
1509 if (gmtime_r(&committer_time, &tm) == NULL)
1510 return got_error_from_errno("gmtime_r");
1511 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1512 return got_error(GOT_ERR_NO_SPACE);
1514 if (avail <= date_display_cols)
1515 limit = MIN(sizeof(datebuf) - 1, avail);
1516 else
1517 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1518 tc = get_color(&s->colors, TOG_COLOR_DATE);
1519 if (tc)
1520 wattr_on(view->window,
1521 COLOR_PAIR(tc->colorpair), NULL);
1522 waddnstr(view->window, datebuf, limit);
1523 if (tc)
1524 wattr_off(view->window,
1525 COLOR_PAIR(tc->colorpair), NULL);
1526 col = limit;
1527 if (col > avail)
1528 goto done;
1530 if (avail >= 120) {
1531 char *id_str;
1532 err = got_object_id_str(&id_str, id);
1533 if (err)
1534 goto done;
1535 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1536 if (tc)
1537 wattr_on(view->window,
1538 COLOR_PAIR(tc->colorpair), NULL);
1539 wprintw(view->window, "%.8s ", id_str);
1540 if (tc)
1541 wattr_off(view->window,
1542 COLOR_PAIR(tc->colorpair), NULL);
1543 free(id_str);
1544 col += 9;
1545 if (col > avail)
1546 goto done;
1549 author = strdup(got_object_commit_get_author(commit));
1550 if (author == NULL) {
1551 err = got_error_from_errno("strdup");
1552 goto done;
1554 err = format_author(&wauthor, &author_width, author, avail - col, col);
1555 if (err)
1556 goto done;
1557 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1558 if (tc)
1559 wattr_on(view->window,
1560 COLOR_PAIR(tc->colorpair), NULL);
1561 waddwstr(view->window, wauthor);
1562 if (tc)
1563 wattr_off(view->window,
1564 COLOR_PAIR(tc->colorpair), NULL);
1565 col += author_width;
1566 while (col < avail && author_width < author_display_cols + 2) {
1567 waddch(view->window, ' ');
1568 col++;
1569 author_width++;
1571 if (col > avail)
1572 goto done;
1574 err = got_object_commit_get_logmsg(&logmsg0, commit);
1575 if (err)
1576 goto done;
1577 logmsg = logmsg0;
1578 while (*logmsg == '\n')
1579 logmsg++;
1580 newline = strchr(logmsg, '\n');
1581 if (newline)
1582 *newline = '\0';
1583 limit = avail - col;
1584 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1585 limit, col, 1);
1586 if (err)
1587 goto done;
1588 waddwstr(view->window, &wlogmsg[scrollx]);
1589 col += MAX(logmsg_width, 0);
1590 while (col < avail) {
1591 waddch(view->window, ' ');
1592 col++;
1594 done:
1595 free(logmsg0);
1596 free(wlogmsg);
1597 free(author);
1598 free(wauthor);
1599 free(line);
1600 return err;
1603 static struct commit_queue_entry *
1604 alloc_commit_queue_entry(struct got_commit_object *commit,
1605 struct got_object_id *id)
1607 struct commit_queue_entry *entry;
1609 entry = calloc(1, sizeof(*entry));
1610 if (entry == NULL)
1611 return NULL;
1613 entry->id = id;
1614 entry->commit = commit;
1615 return entry;
1618 static void
1619 pop_commit(struct commit_queue *commits)
1621 struct commit_queue_entry *entry;
1623 entry = TAILQ_FIRST(&commits->head);
1624 TAILQ_REMOVE(&commits->head, entry, entry);
1625 got_object_commit_close(entry->commit);
1626 commits->ncommits--;
1627 /* Don't free entry->id! It is owned by the commit graph. */
1628 free(entry);
1631 static void
1632 free_commits(struct commit_queue *commits)
1634 while (!TAILQ_EMPTY(&commits->head))
1635 pop_commit(commits);
1638 static const struct got_error *
1639 match_commit(int *have_match, struct got_object_id *id,
1640 struct got_commit_object *commit, regex_t *regex)
1642 const struct got_error *err = NULL;
1643 regmatch_t regmatch;
1644 char *id_str = NULL, *logmsg = NULL;
1646 *have_match = 0;
1648 err = got_object_id_str(&id_str, id);
1649 if (err)
1650 return err;
1652 err = got_object_commit_get_logmsg(&logmsg, commit);
1653 if (err)
1654 goto done;
1656 if (regexec(regex, got_object_commit_get_author(commit), 1,
1657 &regmatch, 0) == 0 ||
1658 regexec(regex, got_object_commit_get_committer(commit), 1,
1659 &regmatch, 0) == 0 ||
1660 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1661 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1662 *have_match = 1;
1663 done:
1664 free(id_str);
1665 free(logmsg);
1666 return err;
1669 static const struct got_error *
1670 queue_commits(struct tog_log_thread_args *a)
1672 const struct got_error *err = NULL;
1675 * We keep all commits open throughout the lifetime of the log
1676 * view in order to avoid having to re-fetch commits from disk
1677 * while updating the display.
1679 do {
1680 struct got_object_id *id;
1681 struct got_commit_object *commit;
1682 struct commit_queue_entry *entry;
1683 int errcode;
1685 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1686 NULL, NULL);
1687 if (err || id == NULL)
1688 break;
1690 err = got_object_open_as_commit(&commit, a->repo, id);
1691 if (err)
1692 break;
1693 entry = alloc_commit_queue_entry(commit, id);
1694 if (entry == NULL) {
1695 err = got_error_from_errno("alloc_commit_queue_entry");
1696 break;
1699 errcode = pthread_mutex_lock(&tog_mutex);
1700 if (errcode) {
1701 err = got_error_set_errno(errcode,
1702 "pthread_mutex_lock");
1703 break;
1706 entry->idx = a->commits->ncommits;
1707 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1708 a->commits->ncommits++;
1710 if (*a->searching == TOG_SEARCH_FORWARD &&
1711 !*a->search_next_done) {
1712 int have_match;
1713 err = match_commit(&have_match, id, commit, a->regex);
1714 if (err)
1715 break;
1716 if (have_match)
1717 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1720 errcode = pthread_mutex_unlock(&tog_mutex);
1721 if (errcode && err == NULL)
1722 err = got_error_set_errno(errcode,
1723 "pthread_mutex_unlock");
1724 if (err)
1725 break;
1726 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1728 return err;
1731 static void
1732 select_commit(struct tog_log_view_state *s)
1734 struct commit_queue_entry *entry;
1735 int ncommits = 0;
1737 entry = s->first_displayed_entry;
1738 while (entry) {
1739 if (ncommits == s->selected) {
1740 s->selected_entry = entry;
1741 break;
1743 entry = TAILQ_NEXT(entry, entry);
1744 ncommits++;
1748 static const struct got_error *
1749 draw_commits(struct tog_view *view)
1751 const struct got_error *err = NULL;
1752 struct tog_log_view_state *s = &view->state.log;
1753 struct commit_queue_entry *entry = s->selected_entry;
1754 const int limit = view->nlines;
1755 int width;
1756 int ncommits, author_cols = 4;
1757 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1758 char *refs_str = NULL;
1759 wchar_t *wline;
1760 struct tog_color *tc;
1761 static const size_t date_display_cols = 12;
1763 if (s->selected_entry &&
1764 !(view->searching && view->search_next_done == 0)) {
1765 struct got_reflist_head *refs;
1766 err = got_object_id_str(&id_str, s->selected_entry->id);
1767 if (err)
1768 return err;
1769 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1770 s->selected_entry->id);
1771 if (refs) {
1772 err = build_refs_str(&refs_str, refs,
1773 s->selected_entry->id, s->repo);
1774 if (err)
1775 goto done;
1779 if (s->thread_args.commits_needed == 0)
1780 halfdelay(10); /* disable fast refresh */
1782 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1783 if (asprintf(&ncommits_str, " [%d/%d] %s",
1784 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1785 (view->searching && !view->search_next_done) ?
1786 "searching..." : "loading...") == -1) {
1787 err = got_error_from_errno("asprintf");
1788 goto done;
1790 } else {
1791 const char *search_str = NULL;
1793 if (view->searching) {
1794 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1795 search_str = "no more matches";
1796 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1797 search_str = "no matches found";
1798 else if (!view->search_next_done)
1799 search_str = "searching...";
1802 if (asprintf(&ncommits_str, " [%d/%d] %s",
1803 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1804 search_str ? search_str :
1805 (refs_str ? refs_str : "")) == -1) {
1806 err = got_error_from_errno("asprintf");
1807 goto done;
1811 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1812 if (asprintf(&header, "commit %s %s%s",
1813 id_str ? id_str : "........................................",
1814 s->in_repo_path, ncommits_str) == -1) {
1815 err = got_error_from_errno("asprintf");
1816 header = NULL;
1817 goto done;
1819 } else if (asprintf(&header, "commit %s%s",
1820 id_str ? id_str : "........................................",
1821 ncommits_str) == -1) {
1822 err = got_error_from_errno("asprintf");
1823 header = NULL;
1824 goto done;
1826 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1827 if (err)
1828 goto done;
1830 werase(view->window);
1832 if (view_needs_focus_indication(view))
1833 wstandout(view->window);
1834 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1835 if (tc)
1836 wattr_on(view->window,
1837 COLOR_PAIR(tc->colorpair), NULL);
1838 waddwstr(view->window, wline);
1839 if (tc)
1840 wattr_off(view->window,
1841 COLOR_PAIR(tc->colorpair), NULL);
1842 while (width < view->ncols) {
1843 waddch(view->window, ' ');
1844 width++;
1846 if (view_needs_focus_indication(view))
1847 wstandend(view->window);
1848 free(wline);
1849 if (limit <= 1)
1850 goto done;
1852 /* Grow author column size if necessary, and set view->maxx. */
1853 entry = s->first_displayed_entry;
1854 ncommits = 0;
1855 view->maxx = 0;
1856 while (entry) {
1857 char *author, *eol, *msg, *msg0;
1858 wchar_t *wauthor, *wmsg;
1859 int width;
1860 if (ncommits >= limit - 1)
1861 break;
1862 author = strdup(got_object_commit_get_author(entry->commit));
1863 if (author == NULL) {
1864 err = got_error_from_errno("strdup");
1865 goto done;
1867 err = format_author(&wauthor, &width, author, COLS,
1868 date_display_cols);
1869 if (author_cols < width)
1870 author_cols = width;
1871 free(wauthor);
1872 free(author);
1873 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1874 if (err)
1875 goto done;
1876 msg = msg0;
1877 while (*msg == '\n')
1878 ++msg;
1879 if ((eol = strchr(msg, '\n')))
1880 *eol = '\0';
1881 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1882 date_display_cols + author_cols, 0);
1883 if (err)
1884 goto done;
1885 view->maxx = MAX(view->maxx, width);
1886 free(msg0);
1887 free(wmsg);
1888 ncommits++;
1889 entry = TAILQ_NEXT(entry, entry);
1892 entry = s->first_displayed_entry;
1893 s->last_displayed_entry = s->first_displayed_entry;
1894 ncommits = 0;
1895 while (entry) {
1896 if (ncommits >= limit - 1)
1897 break;
1898 if (ncommits == s->selected)
1899 wstandout(view->window);
1900 err = draw_commit(view, entry->commit, entry->id,
1901 date_display_cols, author_cols);
1902 if (ncommits == s->selected)
1903 wstandend(view->window);
1904 if (err)
1905 goto done;
1906 ncommits++;
1907 s->last_displayed_entry = entry;
1908 entry = TAILQ_NEXT(entry, entry);
1911 view_vborder(view);
1912 update_panels();
1913 doupdate();
1914 done:
1915 free(id_str);
1916 free(refs_str);
1917 free(ncommits_str);
1918 free(header);
1919 return err;
1922 static void
1923 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1925 struct commit_queue_entry *entry;
1926 int nscrolled = 0;
1928 entry = TAILQ_FIRST(&s->commits.head);
1929 if (s->first_displayed_entry == entry)
1930 return;
1932 entry = s->first_displayed_entry;
1933 while (entry && nscrolled < maxscroll) {
1934 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1935 if (entry) {
1936 s->first_displayed_entry = entry;
1937 nscrolled++;
1942 static const struct got_error *
1943 trigger_log_thread(struct tog_view *view, int wait)
1945 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1946 int errcode;
1948 halfdelay(1); /* fast refresh while loading commits */
1950 while (ta->commits_needed > 0 || ta->load_all) {
1951 if (ta->log_complete)
1952 break;
1954 /* Wake the log thread. */
1955 errcode = pthread_cond_signal(&ta->need_commits);
1956 if (errcode)
1957 return got_error_set_errno(errcode,
1958 "pthread_cond_signal");
1961 * The mutex will be released while the view loop waits
1962 * in wgetch(), at which time the log thread will run.
1964 if (!wait)
1965 break;
1967 /* Display progress update in log view. */
1968 show_log_view(view);
1969 update_panels();
1970 doupdate();
1972 /* Wait right here while next commit is being loaded. */
1973 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1974 if (errcode)
1975 return got_error_set_errno(errcode,
1976 "pthread_cond_wait");
1978 /* Display progress update in log view. */
1979 show_log_view(view);
1980 update_panels();
1981 doupdate();
1984 return NULL;
1987 static const struct got_error *
1988 log_scroll_down(struct tog_view *view, int maxscroll)
1990 struct tog_log_view_state *s = &view->state.log;
1991 const struct got_error *err = NULL;
1992 struct commit_queue_entry *pentry;
1993 int nscrolled = 0, ncommits_needed;
1995 if (s->last_displayed_entry == NULL)
1996 return NULL;
1998 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1999 if (s->commits.ncommits < ncommits_needed &&
2000 !s->thread_args.log_complete) {
2002 * Ask the log thread for required amount of commits.
2004 s->thread_args.commits_needed += maxscroll;
2005 err = trigger_log_thread(view, 1);
2006 if (err)
2007 return err;
2010 do {
2011 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2012 if (pentry == NULL)
2013 break;
2015 s->last_displayed_entry = pentry;
2017 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2018 if (pentry == NULL)
2019 break;
2020 s->first_displayed_entry = pentry;
2021 } while (++nscrolled < maxscroll);
2023 return err;
2026 static const struct got_error *
2027 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2028 struct got_commit_object *commit, struct got_object_id *commit_id,
2029 struct tog_view *log_view, struct got_repository *repo)
2031 const struct got_error *err;
2032 struct got_object_qid *parent_id;
2033 struct tog_view *diff_view;
2035 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2036 if (diff_view == NULL)
2037 return got_error_from_errno("view_open");
2039 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2040 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2041 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2042 if (err == NULL)
2043 *new_view = diff_view;
2044 return err;
2047 static const struct got_error *
2048 tree_view_visit_subtree(struct tog_tree_view_state *s,
2049 struct got_tree_object *subtree)
2051 struct tog_parent_tree *parent;
2053 parent = calloc(1, sizeof(*parent));
2054 if (parent == NULL)
2055 return got_error_from_errno("calloc");
2057 parent->tree = s->tree;
2058 parent->first_displayed_entry = s->first_displayed_entry;
2059 parent->selected_entry = s->selected_entry;
2060 parent->selected = s->selected;
2061 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2062 s->tree = subtree;
2063 s->selected = 0;
2064 s->first_displayed_entry = NULL;
2065 return NULL;
2068 static const struct got_error *
2069 tree_view_walk_path(struct tog_tree_view_state *s,
2070 struct got_commit_object *commit, const char *path)
2072 const struct got_error *err = NULL;
2073 struct got_tree_object *tree = NULL;
2074 const char *p;
2075 char *slash, *subpath = NULL;
2077 /* Walk the path and open corresponding tree objects. */
2078 p = path;
2079 while (*p) {
2080 struct got_tree_entry *te;
2081 struct got_object_id *tree_id;
2082 char *te_name;
2084 while (p[0] == '/')
2085 p++;
2087 /* Ensure the correct subtree entry is selected. */
2088 slash = strchr(p, '/');
2089 if (slash == NULL)
2090 te_name = strdup(p);
2091 else
2092 te_name = strndup(p, slash - p);
2093 if (te_name == NULL) {
2094 err = got_error_from_errno("strndup");
2095 break;
2097 te = got_object_tree_find_entry(s->tree, te_name);
2098 if (te == NULL) {
2099 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2100 free(te_name);
2101 break;
2103 free(te_name);
2104 s->first_displayed_entry = s->selected_entry = te;
2106 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2107 break; /* jump to this file's entry */
2109 slash = strchr(p, '/');
2110 if (slash)
2111 subpath = strndup(path, slash - path);
2112 else
2113 subpath = strdup(path);
2114 if (subpath == NULL) {
2115 err = got_error_from_errno("strdup");
2116 break;
2119 err = got_object_id_by_path(&tree_id, s->repo, commit,
2120 subpath);
2121 if (err)
2122 break;
2124 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2125 free(tree_id);
2126 if (err)
2127 break;
2129 err = tree_view_visit_subtree(s, tree);
2130 if (err) {
2131 got_object_tree_close(tree);
2132 break;
2134 if (slash == NULL)
2135 break;
2136 free(subpath);
2137 subpath = NULL;
2138 p = slash;
2141 free(subpath);
2142 return err;
2145 static const struct got_error *
2146 browse_commit_tree(struct tog_view **new_view, int begin_x,
2147 struct commit_queue_entry *entry, const char *path,
2148 const char *head_ref_name, struct got_repository *repo)
2150 const struct got_error *err = NULL;
2151 struct tog_tree_view_state *s;
2152 struct tog_view *tree_view;
2154 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2155 if (tree_view == NULL)
2156 return got_error_from_errno("view_open");
2158 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2159 if (err)
2160 return err;
2161 s = &tree_view->state.tree;
2163 *new_view = tree_view;
2165 if (got_path_is_root_dir(path))
2166 return NULL;
2168 return tree_view_walk_path(s, entry->commit, path);
2171 static const struct got_error *
2172 block_signals_used_by_main_thread(void)
2174 sigset_t sigset;
2175 int errcode;
2177 if (sigemptyset(&sigset) == -1)
2178 return got_error_from_errno("sigemptyset");
2180 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2181 if (sigaddset(&sigset, SIGWINCH) == -1)
2182 return got_error_from_errno("sigaddset");
2183 if (sigaddset(&sigset, SIGCONT) == -1)
2184 return got_error_from_errno("sigaddset");
2185 if (sigaddset(&sigset, SIGINT) == -1)
2186 return got_error_from_errno("sigaddset");
2187 if (sigaddset(&sigset, SIGTERM) == -1)
2188 return got_error_from_errno("sigaddset");
2190 /* ncurses handles SIGTSTP */
2191 if (sigaddset(&sigset, SIGTSTP) == -1)
2192 return got_error_from_errno("sigaddset");
2194 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2195 if (errcode)
2196 return got_error_set_errno(errcode, "pthread_sigmask");
2198 return NULL;
2201 static void *
2202 log_thread(void *arg)
2204 const struct got_error *err = NULL;
2205 int errcode = 0;
2206 struct tog_log_thread_args *a = arg;
2207 int done = 0;
2209 err = block_signals_used_by_main_thread();
2210 if (err)
2211 return (void *)err;
2213 while (!done && !err && !tog_fatal_signal_received()) {
2214 err = queue_commits(a);
2215 if (err) {
2216 if (err->code != GOT_ERR_ITER_COMPLETED)
2217 return (void *)err;
2218 err = NULL;
2219 done = 1;
2220 } else if (a->commits_needed > 0 && !a->load_all)
2221 a->commits_needed--;
2223 errcode = pthread_mutex_lock(&tog_mutex);
2224 if (errcode) {
2225 err = got_error_set_errno(errcode,
2226 "pthread_mutex_lock");
2227 break;
2228 } else if (*a->quit)
2229 done = 1;
2230 else if (*a->first_displayed_entry == NULL) {
2231 *a->first_displayed_entry =
2232 TAILQ_FIRST(&a->commits->head);
2233 *a->selected_entry = *a->first_displayed_entry;
2236 errcode = pthread_cond_signal(&a->commit_loaded);
2237 if (errcode) {
2238 err = got_error_set_errno(errcode,
2239 "pthread_cond_signal");
2240 pthread_mutex_unlock(&tog_mutex);
2241 break;
2244 if (done)
2245 a->commits_needed = 0;
2246 else {
2247 if (a->commits_needed == 0 && !a->load_all) {
2248 errcode = pthread_cond_wait(&a->need_commits,
2249 &tog_mutex);
2250 if (errcode)
2251 err = got_error_set_errno(errcode,
2252 "pthread_cond_wait");
2253 if (*a->quit)
2254 done = 1;
2258 errcode = pthread_mutex_unlock(&tog_mutex);
2259 if (errcode && err == NULL)
2260 err = got_error_set_errno(errcode,
2261 "pthread_mutex_unlock");
2263 a->log_complete = 1;
2264 return (void *)err;
2267 static const struct got_error *
2268 stop_log_thread(struct tog_log_view_state *s)
2270 const struct got_error *err = NULL;
2271 int errcode;
2273 if (s->thread) {
2274 s->quit = 1;
2275 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2276 if (errcode)
2277 return got_error_set_errno(errcode,
2278 "pthread_cond_signal");
2279 errcode = pthread_mutex_unlock(&tog_mutex);
2280 if (errcode)
2281 return got_error_set_errno(errcode,
2282 "pthread_mutex_unlock");
2283 errcode = pthread_join(s->thread, (void **)&err);
2284 if (errcode)
2285 return got_error_set_errno(errcode, "pthread_join");
2286 errcode = pthread_mutex_lock(&tog_mutex);
2287 if (errcode)
2288 return got_error_set_errno(errcode,
2289 "pthread_mutex_lock");
2290 s->thread = 0; //NULL;
2293 if (s->thread_args.repo) {
2294 err = got_repo_close(s->thread_args.repo);
2295 s->thread_args.repo = NULL;
2298 if (s->thread_args.pack_fds) {
2299 const struct got_error *pack_err =
2300 got_repo_pack_fds_close(s->thread_args.pack_fds);
2301 if (err == NULL)
2302 err = pack_err;
2303 s->thread_args.pack_fds = NULL;
2306 if (s->thread_args.graph) {
2307 got_commit_graph_close(s->thread_args.graph);
2308 s->thread_args.graph = NULL;
2311 return err;
2314 static const struct got_error *
2315 close_log_view(struct tog_view *view)
2317 const struct got_error *err = NULL;
2318 struct tog_log_view_state *s = &view->state.log;
2319 int errcode;
2321 err = stop_log_thread(s);
2323 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2324 if (errcode && err == NULL)
2325 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2327 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2328 if (errcode && err == NULL)
2329 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2331 free_commits(&s->commits);
2332 free(s->in_repo_path);
2333 s->in_repo_path = NULL;
2334 free(s->start_id);
2335 s->start_id = NULL;
2336 free(s->head_ref_name);
2337 s->head_ref_name = NULL;
2338 return err;
2341 static const struct got_error *
2342 search_start_log_view(struct tog_view *view)
2344 struct tog_log_view_state *s = &view->state.log;
2346 s->matched_entry = NULL;
2347 s->search_entry = NULL;
2348 return NULL;
2351 static const struct got_error *
2352 search_next_log_view(struct tog_view *view)
2354 const struct got_error *err = NULL;
2355 struct tog_log_view_state *s = &view->state.log;
2356 struct commit_queue_entry *entry;
2358 /* Display progress update in log view. */
2359 show_log_view(view);
2360 update_panels();
2361 doupdate();
2363 if (s->search_entry) {
2364 int errcode, ch;
2365 errcode = pthread_mutex_unlock(&tog_mutex);
2366 if (errcode)
2367 return got_error_set_errno(errcode,
2368 "pthread_mutex_unlock");
2369 ch = wgetch(view->window);
2370 errcode = pthread_mutex_lock(&tog_mutex);
2371 if (errcode)
2372 return got_error_set_errno(errcode,
2373 "pthread_mutex_lock");
2374 if (ch == KEY_BACKSPACE) {
2375 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2376 return NULL;
2378 if (view->searching == TOG_SEARCH_FORWARD)
2379 entry = TAILQ_NEXT(s->search_entry, entry);
2380 else
2381 entry = TAILQ_PREV(s->search_entry,
2382 commit_queue_head, entry);
2383 } else if (s->matched_entry) {
2384 if (view->searching == TOG_SEARCH_FORWARD)
2385 entry = TAILQ_NEXT(s->matched_entry, entry);
2386 else
2387 entry = TAILQ_PREV(s->matched_entry,
2388 commit_queue_head, entry);
2389 } else {
2390 entry = s->selected_entry;
2393 while (1) {
2394 int have_match = 0;
2396 if (entry == NULL) {
2397 if (s->thread_args.log_complete ||
2398 view->searching == TOG_SEARCH_BACKWARD) {
2399 view->search_next_done =
2400 (s->matched_entry == NULL ?
2401 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2402 s->search_entry = NULL;
2403 return NULL;
2406 * Poke the log thread for more commits and return,
2407 * allowing the main loop to make progress. Search
2408 * will resume at s->search_entry once we come back.
2410 s->thread_args.commits_needed++;
2411 return trigger_log_thread(view, 0);
2414 err = match_commit(&have_match, entry->id, entry->commit,
2415 &view->regex);
2416 if (err)
2417 break;
2418 if (have_match) {
2419 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2420 s->matched_entry = entry;
2421 break;
2424 s->search_entry = entry;
2425 if (view->searching == TOG_SEARCH_FORWARD)
2426 entry = TAILQ_NEXT(entry, entry);
2427 else
2428 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2431 if (s->matched_entry) {
2432 int cur = s->selected_entry->idx;
2433 while (cur < s->matched_entry->idx) {
2434 err = input_log_view(NULL, view, KEY_DOWN);
2435 if (err)
2436 return err;
2437 cur++;
2439 while (cur > s->matched_entry->idx) {
2440 err = input_log_view(NULL, view, KEY_UP);
2441 if (err)
2442 return err;
2443 cur--;
2447 s->search_entry = NULL;
2449 return NULL;
2452 static const struct got_error *
2453 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2454 struct got_repository *repo, const char *head_ref_name,
2455 const char *in_repo_path, int log_branches)
2457 const struct got_error *err = NULL;
2458 struct tog_log_view_state *s = &view->state.log;
2459 struct got_repository *thread_repo = NULL;
2460 struct got_commit_graph *thread_graph = NULL;
2461 int errcode;
2463 if (in_repo_path != s->in_repo_path) {
2464 free(s->in_repo_path);
2465 s->in_repo_path = strdup(in_repo_path);
2466 if (s->in_repo_path == NULL)
2467 return got_error_from_errno("strdup");
2470 /* The commit queue only contains commits being displayed. */
2471 TAILQ_INIT(&s->commits.head);
2472 s->commits.ncommits = 0;
2474 s->repo = repo;
2475 if (head_ref_name) {
2476 s->head_ref_name = strdup(head_ref_name);
2477 if (s->head_ref_name == NULL) {
2478 err = got_error_from_errno("strdup");
2479 goto done;
2482 s->start_id = got_object_id_dup(start_id);
2483 if (s->start_id == NULL) {
2484 err = got_error_from_errno("got_object_id_dup");
2485 goto done;
2487 s->log_branches = log_branches;
2489 STAILQ_INIT(&s->colors);
2490 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2491 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2492 get_color_value("TOG_COLOR_COMMIT"));
2493 if (err)
2494 goto done;
2495 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2496 get_color_value("TOG_COLOR_AUTHOR"));
2497 if (err) {
2498 free_colors(&s->colors);
2499 goto done;
2501 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2502 get_color_value("TOG_COLOR_DATE"));
2503 if (err) {
2504 free_colors(&s->colors);
2505 goto done;
2509 view->show = show_log_view;
2510 view->input = input_log_view;
2511 view->close = close_log_view;
2512 view->search_start = search_start_log_view;
2513 view->search_next = search_next_log_view;
2515 if (s->thread_args.pack_fds == NULL) {
2516 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2517 if (err)
2518 goto done;
2520 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2521 s->thread_args.pack_fds);
2522 if (err)
2523 goto done;
2524 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2525 !s->log_branches);
2526 if (err)
2527 goto done;
2528 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2529 s->repo, NULL, NULL);
2530 if (err)
2531 goto done;
2533 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2534 if (errcode) {
2535 err = got_error_set_errno(errcode, "pthread_cond_init");
2536 goto done;
2538 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2539 if (errcode) {
2540 err = got_error_set_errno(errcode, "pthread_cond_init");
2541 goto done;
2544 s->thread_args.commits_needed = view->nlines;
2545 s->thread_args.graph = thread_graph;
2546 s->thread_args.commits = &s->commits;
2547 s->thread_args.in_repo_path = s->in_repo_path;
2548 s->thread_args.start_id = s->start_id;
2549 s->thread_args.repo = thread_repo;
2550 s->thread_args.log_complete = 0;
2551 s->thread_args.quit = &s->quit;
2552 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2553 s->thread_args.selected_entry = &s->selected_entry;
2554 s->thread_args.searching = &view->searching;
2555 s->thread_args.search_next_done = &view->search_next_done;
2556 s->thread_args.regex = &view->regex;
2557 done:
2558 if (err)
2559 close_log_view(view);
2560 return err;
2563 static const struct got_error *
2564 show_log_view(struct tog_view *view)
2566 const struct got_error *err;
2567 struct tog_log_view_state *s = &view->state.log;
2569 if (s->thread == 0) { //NULL) {
2570 int errcode = pthread_create(&s->thread, NULL, log_thread,
2571 &s->thread_args);
2572 if (errcode)
2573 return got_error_set_errno(errcode, "pthread_create");
2574 if (s->thread_args.commits_needed > 0) {
2575 err = trigger_log_thread(view, 1);
2576 if (err)
2577 return err;
2581 return draw_commits(view);
2584 static const struct got_error *
2585 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2587 const struct got_error *err = NULL;
2588 struct tog_log_view_state *s = &view->state.log;
2589 struct tog_view *diff_view = NULL, *tree_view = NULL;
2590 struct tog_view *ref_view = NULL;
2591 struct commit_queue_entry *entry;
2592 int begin_x = 0, n, nscroll = view->nlines - 1;
2594 if (s->thread_args.load_all) {
2595 if (ch == KEY_BACKSPACE)
2596 s->thread_args.load_all = 0;
2597 else if (s->thread_args.log_complete) {
2598 s->thread_args.load_all = 0;
2599 log_scroll_down(view, s->commits.ncommits);
2600 s->selected = MIN(view->nlines - 2,
2601 s->commits.ncommits - 1);
2602 select_commit(s);
2604 return NULL;
2607 switch (ch) {
2608 case 'q':
2609 s->quit = 1;
2610 break;
2611 case '0':
2612 view->x = 0;
2613 break;
2614 case '$':
2615 view->x = MAX(view->maxx - view->ncols / 2, 0);
2616 break;
2617 case KEY_RIGHT:
2618 case 'l':
2619 if (view->x + view->ncols / 2 < view->maxx)
2620 view->x += 2; /* move two columns right */
2621 break;
2622 case KEY_LEFT:
2623 case 'h':
2624 view->x -= MIN(view->x, 2); /* move two columns back */
2625 break;
2626 case 'k':
2627 case KEY_UP:
2628 case '<':
2629 case ',':
2630 case CTRL('p'):
2631 if (s->first_displayed_entry == NULL)
2632 break;
2633 if (s->selected > 0)
2634 s->selected--;
2635 else
2636 log_scroll_up(s, 1);
2637 select_commit(s);
2638 break;
2639 case 'g':
2640 case KEY_HOME:
2641 s->selected = 0;
2642 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2643 select_commit(s);
2644 break;
2645 case CTRL('u'):
2646 case 'u':
2647 nscroll /= 2;
2648 /* FALL THROUGH */
2649 case KEY_PPAGE:
2650 case CTRL('b'):
2651 if (s->first_displayed_entry == NULL)
2652 break;
2653 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2654 s->selected = MAX(0, s->selected - nscroll - 1);
2655 else
2656 log_scroll_up(s, nscroll);
2657 select_commit(s);
2658 break;
2659 case 'j':
2660 case KEY_DOWN:
2661 case '>':
2662 case '.':
2663 case CTRL('n'):
2664 if (s->first_displayed_entry == NULL)
2665 break;
2666 if (s->selected < MIN(view->nlines - 2,
2667 s->commits.ncommits - 1))
2668 s->selected++;
2669 else {
2670 err = log_scroll_down(view, 1);
2671 if (err)
2672 break;
2674 select_commit(s);
2675 break;
2676 case 'G':
2677 case KEY_END: {
2678 /* We don't know yet how many commits, so we're forced to
2679 * traverse them all. */
2680 if (!s->thread_args.log_complete) {
2681 s->thread_args.load_all = 1;
2682 return trigger_log_thread(view, 0);
2685 s->selected = 0;
2686 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2687 for (n = 0; n < view->nlines - 1; n++) {
2688 if (entry == NULL)
2689 break;
2690 s->first_displayed_entry = entry;
2691 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2693 if (n > 0)
2694 s->selected = n - 1;
2695 select_commit(s);
2696 break;
2698 case CTRL('d'):
2699 case 'd':
2700 nscroll /= 2;
2701 /* FALL THROUGH */
2702 case KEY_NPAGE:
2703 case CTRL('f'): {
2704 struct commit_queue_entry *first;
2705 first = s->first_displayed_entry;
2706 if (first == NULL)
2707 break;
2708 err = log_scroll_down(view, nscroll);
2709 if (err)
2710 break;
2711 if (first == s->first_displayed_entry &&
2712 s->selected < MIN(view->nlines - 2,
2713 s->commits.ncommits - 1)) {
2714 /* can't scroll further down */
2715 s->selected += MIN(s->last_displayed_entry->idx -
2716 s->selected_entry->idx, nscroll + 1);
2718 select_commit(s);
2719 break;
2721 case KEY_RESIZE:
2722 if (s->selected > view->nlines - 2)
2723 s->selected = view->nlines - 2;
2724 if (s->selected > s->commits.ncommits - 1)
2725 s->selected = s->commits.ncommits - 1;
2726 select_commit(s);
2727 if (s->commits.ncommits < view->nlines - 1 &&
2728 !s->thread_args.log_complete) {
2729 s->thread_args.commits_needed += (view->nlines - 1) -
2730 s->commits.ncommits;
2731 err = trigger_log_thread(view, 1);
2733 break;
2734 case KEY_ENTER:
2735 case ' ':
2736 case '\r':
2737 if (s->selected_entry == NULL)
2738 break;
2739 if (view_is_parent_view(view))
2740 begin_x = view_split_begin_x(view->begin_x);
2741 err = open_diff_view_for_commit(&diff_view, begin_x,
2742 s->selected_entry->commit, s->selected_entry->id,
2743 view, s->repo);
2744 if (err)
2745 break;
2746 view->focussed = 0;
2747 diff_view->focussed = 1;
2748 if (view_is_parent_view(view)) {
2749 err = view_close_child(view);
2750 if (err)
2751 return err;
2752 view_set_child(view, diff_view);
2753 view->focus_child = 1;
2754 } else
2755 *new_view = diff_view;
2756 break;
2757 case 't':
2758 if (s->selected_entry == NULL)
2759 break;
2760 if (view_is_parent_view(view))
2761 begin_x = view_split_begin_x(view->begin_x);
2762 err = browse_commit_tree(&tree_view, begin_x,
2763 s->selected_entry, s->in_repo_path, s->head_ref_name,
2764 s->repo);
2765 if (err)
2766 break;
2767 view->focussed = 0;
2768 tree_view->focussed = 1;
2769 if (view_is_parent_view(view)) {
2770 err = view_close_child(view);
2771 if (err)
2772 return err;
2773 view_set_child(view, tree_view);
2774 view->focus_child = 1;
2775 } else
2776 *new_view = tree_view;
2777 break;
2778 case KEY_BACKSPACE:
2779 case CTRL('l'):
2780 case 'B':
2781 if (ch == KEY_BACKSPACE &&
2782 got_path_is_root_dir(s->in_repo_path))
2783 break;
2784 err = stop_log_thread(s);
2785 if (err)
2786 return err;
2787 if (ch == KEY_BACKSPACE) {
2788 char *parent_path;
2789 err = got_path_dirname(&parent_path, s->in_repo_path);
2790 if (err)
2791 return err;
2792 free(s->in_repo_path);
2793 s->in_repo_path = parent_path;
2794 s->thread_args.in_repo_path = s->in_repo_path;
2795 } else if (ch == CTRL('l')) {
2796 struct got_object_id *start_id;
2797 err = got_repo_match_object_id(&start_id, NULL,
2798 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2799 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2800 if (err)
2801 return err;
2802 free(s->start_id);
2803 s->start_id = start_id;
2804 s->thread_args.start_id = s->start_id;
2805 } else /* 'B' */
2806 s->log_branches = !s->log_branches;
2808 if (s->thread_args.pack_fds == NULL) {
2809 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2810 if (err)
2811 return err;
2813 err = got_repo_open(&s->thread_args.repo,
2814 got_repo_get_path(s->repo), NULL,
2815 s->thread_args.pack_fds);
2816 if (err)
2817 return err;
2818 tog_free_refs();
2819 err = tog_load_refs(s->repo, 0);
2820 if (err)
2821 return err;
2822 err = got_commit_graph_open(&s->thread_args.graph,
2823 s->in_repo_path, !s->log_branches);
2824 if (err)
2825 return err;
2826 err = got_commit_graph_iter_start(s->thread_args.graph,
2827 s->start_id, s->repo, NULL, NULL);
2828 if (err)
2829 return err;
2830 free_commits(&s->commits);
2831 s->first_displayed_entry = NULL;
2832 s->last_displayed_entry = NULL;
2833 s->selected_entry = NULL;
2834 s->selected = 0;
2835 s->thread_args.log_complete = 0;
2836 s->quit = 0;
2837 s->thread_args.commits_needed = view->nlines;
2838 break;
2839 case 'r':
2840 if (view_is_parent_view(view))
2841 begin_x = view_split_begin_x(view->begin_x);
2842 ref_view = view_open(view->nlines, view->ncols,
2843 view->begin_y, begin_x, TOG_VIEW_REF);
2844 if (ref_view == NULL)
2845 return got_error_from_errno("view_open");
2846 err = open_ref_view(ref_view, s->repo);
2847 if (err) {
2848 view_close(ref_view);
2849 return err;
2851 view->focussed = 0;
2852 ref_view->focussed = 1;
2853 if (view_is_parent_view(view)) {
2854 err = view_close_child(view);
2855 if (err)
2856 return err;
2857 view_set_child(view, ref_view);
2858 view->focus_child = 1;
2859 } else
2860 *new_view = ref_view;
2861 break;
2862 default:
2863 break;
2866 return err;
2869 static const struct got_error *
2870 apply_unveil(const char *repo_path, const char *worktree_path)
2872 const struct got_error *error;
2874 #ifdef PROFILE
2875 if (unveil("gmon.out", "rwc") != 0)
2876 return got_error_from_errno2("unveil", "gmon.out");
2877 #endif
2878 if (repo_path && unveil(repo_path, "r") != 0)
2879 return got_error_from_errno2("unveil", repo_path);
2881 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2882 return got_error_from_errno2("unveil", worktree_path);
2884 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2885 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2887 error = got_privsep_unveil_exec_helpers();
2888 if (error != NULL)
2889 return error;
2891 if (unveil(NULL, NULL) != 0)
2892 return got_error_from_errno("unveil");
2894 return NULL;
2897 static void
2898 init_curses(void)
2901 * Override default signal handlers before starting ncurses.
2902 * This should prevent ncurses from installing its own
2903 * broken cleanup() signal handler.
2905 signal(SIGWINCH, tog_sigwinch);
2906 signal(SIGPIPE, tog_sigpipe);
2907 signal(SIGCONT, tog_sigcont);
2908 signal(SIGINT, tog_sigint);
2909 signal(SIGTERM, tog_sigterm);
2911 initscr();
2912 cbreak();
2913 halfdelay(1); /* Do fast refresh while initial view is loading. */
2914 noecho();
2915 nonl();
2916 intrflush(stdscr, FALSE);
2917 keypad(stdscr, TRUE);
2918 curs_set(0);
2919 if (getenv("TOG_COLORS") != NULL) {
2920 start_color();
2921 use_default_colors();
2925 static const struct got_error *
2926 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2927 struct got_repository *repo, struct got_worktree *worktree)
2929 const struct got_error *err = NULL;
2931 if (argc == 0) {
2932 *in_repo_path = strdup("/");
2933 if (*in_repo_path == NULL)
2934 return got_error_from_errno("strdup");
2935 return NULL;
2938 if (worktree) {
2939 const char *prefix = got_worktree_get_path_prefix(worktree);
2940 char *p;
2942 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2943 if (err)
2944 return err;
2945 if (asprintf(in_repo_path, "%s%s%s", prefix,
2946 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2947 p) == -1) {
2948 err = got_error_from_errno("asprintf");
2949 *in_repo_path = NULL;
2951 free(p);
2952 } else
2953 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2955 return err;
2958 static const struct got_error *
2959 cmd_log(int argc, char *argv[])
2961 const struct got_error *error;
2962 struct got_repository *repo = NULL;
2963 struct got_worktree *worktree = NULL;
2964 struct got_object_id *start_id = NULL;
2965 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2966 char *start_commit = NULL, *label = NULL;
2967 struct got_reference *ref = NULL;
2968 const char *head_ref_name = NULL;
2969 int ch, log_branches = 0;
2970 struct tog_view *view;
2971 int *pack_fds = NULL;
2973 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2974 switch (ch) {
2975 case 'b':
2976 log_branches = 1;
2977 break;
2978 case 'c':
2979 start_commit = optarg;
2980 break;
2981 case 'r':
2982 repo_path = realpath(optarg, NULL);
2983 if (repo_path == NULL)
2984 return got_error_from_errno2("realpath",
2985 optarg);
2986 break;
2987 default:
2988 usage_log();
2989 /* NOTREACHED */
2993 argc -= optind;
2994 argv += optind;
2996 if (argc > 1)
2997 usage_log();
2999 error = got_repo_pack_fds_open(&pack_fds);
3000 if (error != NULL)
3001 goto done;
3003 if (repo_path == NULL) {
3004 cwd = getcwd(NULL, 0);
3005 if (cwd == NULL)
3006 return got_error_from_errno("getcwd");
3007 error = got_worktree_open(&worktree, cwd);
3008 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3009 goto done;
3010 if (worktree)
3011 repo_path =
3012 strdup(got_worktree_get_repo_path(worktree));
3013 else
3014 repo_path = strdup(cwd);
3015 if (repo_path == NULL) {
3016 error = got_error_from_errno("strdup");
3017 goto done;
3021 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3022 if (error != NULL)
3023 goto done;
3025 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3026 repo, worktree);
3027 if (error)
3028 goto done;
3030 init_curses();
3032 error = apply_unveil(got_repo_get_path(repo),
3033 worktree ? got_worktree_get_root_path(worktree) : NULL);
3034 if (error)
3035 goto done;
3037 /* already loaded by tog_log_with_path()? */
3038 if (TAILQ_EMPTY(&tog_refs)) {
3039 error = tog_load_refs(repo, 0);
3040 if (error)
3041 goto done;
3044 if (start_commit == NULL) {
3045 error = got_repo_match_object_id(&start_id, &label,
3046 worktree ? got_worktree_get_head_ref_name(worktree) :
3047 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3048 if (error)
3049 goto done;
3050 head_ref_name = label;
3051 } else {
3052 error = got_ref_open(&ref, repo, start_commit, 0);
3053 if (error == NULL)
3054 head_ref_name = got_ref_get_name(ref);
3055 else if (error->code != GOT_ERR_NOT_REF)
3056 goto done;
3057 error = got_repo_match_object_id(&start_id, NULL,
3058 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3059 if (error)
3060 goto done;
3063 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3064 if (view == NULL) {
3065 error = got_error_from_errno("view_open");
3066 goto done;
3068 error = open_log_view(view, start_id, repo, head_ref_name,
3069 in_repo_path, log_branches);
3070 if (error)
3071 goto done;
3072 if (worktree) {
3073 /* Release work tree lock. */
3074 got_worktree_close(worktree);
3075 worktree = NULL;
3077 error = view_loop(view);
3078 done:
3079 free(in_repo_path);
3080 free(repo_path);
3081 free(cwd);
3082 free(start_id);
3083 free(label);
3084 if (ref)
3085 got_ref_close(ref);
3086 if (repo) {
3087 const struct got_error *close_err = got_repo_close(repo);
3088 if (error == NULL)
3089 error = close_err;
3091 if (worktree)
3092 got_worktree_close(worktree);
3093 if (pack_fds) {
3094 const struct got_error *pack_err =
3095 got_repo_pack_fds_close(pack_fds);
3096 if (error == NULL)
3097 error = pack_err;
3099 tog_free_refs();
3100 return error;
3103 __dead static void
3104 usage_diff(void)
3106 endwin();
3107 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3108 "[-w] object1 object2\n", getprogname());
3109 exit(1);
3112 static int
3113 match_line(const char *line, regex_t *regex, size_t nmatch,
3114 regmatch_t *regmatch)
3116 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3119 struct tog_color *
3120 match_color(struct tog_colors *colors, const char *line)
3122 struct tog_color *tc = NULL;
3124 STAILQ_FOREACH(tc, colors, entry) {
3125 if (match_line(line, &tc->regex, 0, NULL))
3126 return tc;
3129 return NULL;
3132 static const struct got_error *
3133 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3134 WINDOW *window, int skip, regmatch_t *regmatch)
3136 const struct got_error *err = NULL;
3137 wchar_t *wline;
3138 int rme, rms, n, width;
3140 *wtotal = 0;
3141 rms = regmatch->rm_so;
3142 rme = regmatch->rm_eo;
3144 err = format_line(&wline, &width, NULL, line, 0, wlimit + skip,
3145 col_tab_align, 1);
3146 if (err)
3147 return err;
3149 /* draw up to matched token if we haven't scrolled past it */
3150 n = MAX(rms - skip, 0);
3151 if (n) {
3152 waddnwstr(window, wline + skip, n);
3153 wlimit -= n;
3154 *wtotal += n;
3157 if (wlimit > 0) {
3158 int len = rme - rms;
3159 n = 0;
3160 if (skip > rms) {
3161 n = skip - rms;
3162 len = MAX(len - n, 0);
3164 /* draw (visible part of) matched token (if scrolled into it) */
3165 if (len) {
3166 wattron(window, A_STANDOUT);
3167 waddnwstr(window, wline + rms + n, len);
3168 wattroff(window, A_STANDOUT);
3169 wlimit -= len;
3170 *wtotal += len;
3174 if (wlimit > 0 && skip < width) { /* draw rest of line */
3175 n = 0;
3176 if (skip > rme)
3177 n = MIN(skip - rme, width - rme);
3178 waddnwstr(window, wline + rme + n, wlimit);
3181 *wtotal = width;
3182 free(wline);
3183 return NULL;
3186 static const struct got_error *
3187 draw_file(struct tog_view *view, const char *header)
3189 struct tog_diff_view_state *s = &view->state.diff;
3190 regmatch_t *regmatch = &view->regmatch;
3191 const struct got_error *err;
3192 int nprinted = 0;
3193 char *line;
3194 size_t linesize = 0;
3195 ssize_t linelen;
3196 struct tog_color *tc;
3197 wchar_t *wline;
3198 int width;
3199 int max_lines = view->nlines;
3200 int nlines = s->nlines;
3201 off_t line_offset;
3203 line_offset = s->line_offsets[s->first_displayed_line - 1];
3204 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3205 return got_error_from_errno("fseek");
3207 werase(view->window);
3209 if (header) {
3210 if (asprintf(&line, "[%d/%d] %s",
3211 s->first_displayed_line - 1 + s->selected_line, nlines,
3212 header) == -1)
3213 return got_error_from_errno("asprintf");
3214 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3215 0, 0);
3216 free(line);
3217 if (err)
3218 return err;
3220 if (view_needs_focus_indication(view))
3221 wstandout(view->window);
3222 waddwstr(view->window, wline);
3223 free(wline);
3224 wline = NULL;
3225 if (view_needs_focus_indication(view))
3226 wstandend(view->window);
3227 if (width <= view->ncols - 1)
3228 waddch(view->window, '\n');
3230 if (max_lines <= 1)
3231 return NULL;
3232 max_lines--;
3235 s->eof = 0;
3236 view->maxx = 0;
3237 line = NULL;
3238 while (max_lines > 0 && nprinted < max_lines) {
3239 linelen = getline(&line, &linesize, s->f);
3240 if (linelen == -1) {
3241 if (feof(s->f)) {
3242 s->eof = 1;
3243 break;
3245 free(line);
3246 return got_ferror(s->f, GOT_ERR_IO);
3249 view->maxx = MAX(view->maxx, linelen);
3251 tc = match_color(&s->colors, line);
3252 if (tc)
3253 wattr_on(view->window,
3254 COLOR_PAIR(tc->colorpair), NULL);
3255 if (s->first_displayed_line + nprinted == s->matched_line &&
3256 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3257 err = add_matched_line(&width, line, view->ncols, 0,
3258 view->window, view->x, regmatch);
3259 if (err) {
3260 free(line);
3261 return err;
3263 } else {
3264 err = format_line(&wline, &width, NULL, line, 0,
3265 view->x + view->ncols, 0, view->x ? 1 : 0);
3266 if (err) {
3267 free(line);
3268 return err;
3270 if (view->x < width)
3271 waddwstr(view->window, wline + view->x);
3272 free(wline);
3273 wline = NULL;
3275 if (tc)
3276 wattr_off(view->window,
3277 COLOR_PAIR(tc->colorpair), NULL);
3278 if (width - view->x <= view->ncols - 1)
3279 waddch(view->window, '\n');
3280 nprinted++;
3282 free(line);
3283 if (nprinted >= 1)
3284 s->last_displayed_line = s->first_displayed_line +
3285 (nprinted - 1);
3286 else
3287 s->last_displayed_line = s->first_displayed_line;
3289 view_vborder(view);
3291 if (s->eof) {
3292 while (nprinted < view->nlines) {
3293 waddch(view->window, '\n');
3294 nprinted++;
3297 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3298 view->ncols, 0, 0);
3299 if (err) {
3300 return err;
3303 wstandout(view->window);
3304 waddwstr(view->window, wline);
3305 free(wline);
3306 wline = NULL;
3307 wstandend(view->window);
3310 return NULL;
3313 static char *
3314 get_datestr(time_t *time, char *datebuf)
3316 struct tm mytm, *tm;
3317 char *p, *s;
3319 tm = gmtime_r(time, &mytm);
3320 if (tm == NULL)
3321 return NULL;
3322 s = asctime_r(tm, datebuf);
3323 if (s == NULL)
3324 return NULL;
3325 p = strchr(s, '\n');
3326 if (p)
3327 *p = '\0';
3328 return s;
3331 static const struct got_error *
3332 get_changed_paths(struct got_pathlist_head *paths,
3333 struct got_commit_object *commit, struct got_repository *repo)
3335 const struct got_error *err = NULL;
3336 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3337 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3338 struct got_object_qid *qid;
3340 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3341 if (qid != NULL) {
3342 struct got_commit_object *pcommit;
3343 err = got_object_open_as_commit(&pcommit, repo,
3344 &qid->id);
3345 if (err)
3346 return err;
3348 tree_id1 = got_object_id_dup(
3349 got_object_commit_get_tree_id(pcommit));
3350 if (tree_id1 == NULL) {
3351 got_object_commit_close(pcommit);
3352 return got_error_from_errno("got_object_id_dup");
3354 got_object_commit_close(pcommit);
3358 if (tree_id1) {
3359 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3360 if (err)
3361 goto done;
3364 tree_id2 = got_object_commit_get_tree_id(commit);
3365 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3366 if (err)
3367 goto done;
3369 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3370 got_diff_tree_collect_changed_paths, paths, 0);
3371 done:
3372 if (tree1)
3373 got_object_tree_close(tree1);
3374 if (tree2)
3375 got_object_tree_close(tree2);
3376 free(tree_id1);
3377 return err;
3380 static const struct got_error *
3381 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3383 off_t *p;
3385 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3386 if (p == NULL)
3387 return got_error_from_errno("reallocarray");
3388 *line_offsets = p;
3389 (*line_offsets)[*nlines] = off;
3390 (*nlines)++;
3391 return NULL;
3394 static const struct got_error *
3395 write_commit_info(off_t **line_offsets, size_t *nlines,
3396 struct got_object_id *commit_id, struct got_reflist_head *refs,
3397 struct got_repository *repo, FILE *outfile)
3399 const struct got_error *err = NULL;
3400 char datebuf[26], *datestr;
3401 struct got_commit_object *commit;
3402 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3403 time_t committer_time;
3404 const char *author, *committer;
3405 char *refs_str = NULL;
3406 struct got_pathlist_head changed_paths;
3407 struct got_pathlist_entry *pe;
3408 off_t outoff = 0;
3409 int n;
3411 TAILQ_INIT(&changed_paths);
3413 if (refs) {
3414 err = build_refs_str(&refs_str, refs, commit_id, repo);
3415 if (err)
3416 return err;
3419 err = got_object_open_as_commit(&commit, repo, commit_id);
3420 if (err)
3421 return err;
3423 err = got_object_id_str(&id_str, commit_id);
3424 if (err) {
3425 err = got_error_from_errno("got_object_id_str");
3426 goto done;
3429 err = add_line_offset(line_offsets, nlines, 0);
3430 if (err)
3431 goto done;
3433 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3434 refs_str ? refs_str : "", refs_str ? ")" : "");
3435 if (n < 0) {
3436 err = got_error_from_errno("fprintf");
3437 goto done;
3439 outoff += n;
3440 err = add_line_offset(line_offsets, nlines, outoff);
3441 if (err)
3442 goto done;
3444 n = fprintf(outfile, "from: %s\n",
3445 got_object_commit_get_author(commit));
3446 if (n < 0) {
3447 err = got_error_from_errno("fprintf");
3448 goto done;
3450 outoff += n;
3451 err = add_line_offset(line_offsets, nlines, outoff);
3452 if (err)
3453 goto done;
3455 committer_time = got_object_commit_get_committer_time(commit);
3456 datestr = get_datestr(&committer_time, datebuf);
3457 if (datestr) {
3458 n = fprintf(outfile, "date: %s UTC\n", datestr);
3459 if (n < 0) {
3460 err = got_error_from_errno("fprintf");
3461 goto done;
3463 outoff += n;
3464 err = add_line_offset(line_offsets, nlines, outoff);
3465 if (err)
3466 goto done;
3468 author = got_object_commit_get_author(commit);
3469 committer = got_object_commit_get_committer(commit);
3470 if (strcmp(author, committer) != 0) {
3471 n = fprintf(outfile, "via: %s\n", committer);
3472 if (n < 0) {
3473 err = got_error_from_errno("fprintf");
3474 goto done;
3476 outoff += n;
3477 err = add_line_offset(line_offsets, nlines, outoff);
3478 if (err)
3479 goto done;
3481 if (got_object_commit_get_nparents(commit) > 1) {
3482 const struct got_object_id_queue *parent_ids;
3483 struct got_object_qid *qid;
3484 int pn = 1;
3485 parent_ids = got_object_commit_get_parent_ids(commit);
3486 STAILQ_FOREACH(qid, parent_ids, entry) {
3487 err = got_object_id_str(&id_str, &qid->id);
3488 if (err)
3489 goto done;
3490 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3491 if (n < 0) {
3492 err = got_error_from_errno("fprintf");
3493 goto done;
3495 outoff += n;
3496 err = add_line_offset(line_offsets, nlines, outoff);
3497 if (err)
3498 goto done;
3499 free(id_str);
3500 id_str = NULL;
3504 err = got_object_commit_get_logmsg(&logmsg, commit);
3505 if (err)
3506 goto done;
3507 s = logmsg;
3508 while ((line = strsep(&s, "\n")) != NULL) {
3509 n = fprintf(outfile, "%s\n", line);
3510 if (n < 0) {
3511 err = got_error_from_errno("fprintf");
3512 goto done;
3514 outoff += n;
3515 err = add_line_offset(line_offsets, nlines, outoff);
3516 if (err)
3517 goto done;
3520 err = get_changed_paths(&changed_paths, commit, repo);
3521 if (err)
3522 goto done;
3523 TAILQ_FOREACH(pe, &changed_paths, entry) {
3524 struct got_diff_changed_path *cp = pe->data;
3525 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3526 if (n < 0) {
3527 err = got_error_from_errno("fprintf");
3528 goto done;
3530 outoff += n;
3531 err = add_line_offset(line_offsets, nlines, outoff);
3532 if (err)
3533 goto done;
3534 free((char *)pe->path);
3535 free(pe->data);
3538 fputc('\n', outfile);
3539 outoff++;
3540 err = add_line_offset(line_offsets, nlines, outoff);
3541 done:
3542 got_pathlist_free(&changed_paths);
3543 free(id_str);
3544 free(logmsg);
3545 free(refs_str);
3546 got_object_commit_close(commit);
3547 if (err) {
3548 free(*line_offsets);
3549 *line_offsets = NULL;
3550 *nlines = 0;
3552 return err;
3555 static const struct got_error *
3556 create_diff(struct tog_diff_view_state *s)
3558 const struct got_error *err = NULL;
3559 FILE *f = NULL;
3560 int obj_type;
3562 free(s->line_offsets);
3563 s->line_offsets = malloc(sizeof(off_t));
3564 if (s->line_offsets == NULL)
3565 return got_error_from_errno("malloc");
3566 s->nlines = 0;
3568 f = got_opentemp();
3569 if (f == NULL) {
3570 err = got_error_from_errno("got_opentemp");
3571 goto done;
3573 if (s->f && fclose(s->f) == EOF) {
3574 err = got_error_from_errno("fclose");
3575 goto done;
3577 s->f = f;
3579 if (s->id1)
3580 err = got_object_get_type(&obj_type, s->repo, s->id1);
3581 else
3582 err = got_object_get_type(&obj_type, s->repo, s->id2);
3583 if (err)
3584 goto done;
3586 switch (obj_type) {
3587 case GOT_OBJ_TYPE_BLOB:
3588 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3589 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3590 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3591 s->repo, s->f);
3592 break;
3593 case GOT_OBJ_TYPE_TREE:
3594 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3595 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3596 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3597 break;
3598 case GOT_OBJ_TYPE_COMMIT: {
3599 const struct got_object_id_queue *parent_ids;
3600 struct got_object_qid *pid;
3601 struct got_commit_object *commit2;
3602 struct got_reflist_head *refs;
3604 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3605 if (err)
3606 goto done;
3607 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3608 /* Show commit info if we're diffing to a parent/root commit. */
3609 if (s->id1 == NULL) {
3610 err = write_commit_info(&s->line_offsets, &s->nlines,
3611 s->id2, refs, s->repo, s->f);
3612 if (err)
3613 goto done;
3614 } else {
3615 parent_ids = got_object_commit_get_parent_ids(commit2);
3616 STAILQ_FOREACH(pid, parent_ids, entry) {
3617 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3618 err = write_commit_info(
3619 &s->line_offsets, &s->nlines,
3620 s->id2, refs, s->repo, s->f);
3621 if (err)
3622 goto done;
3623 break;
3627 got_object_commit_close(commit2);
3629 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3630 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3631 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3632 break;
3634 default:
3635 err = got_error(GOT_ERR_OBJ_TYPE);
3636 break;
3638 if (err)
3639 goto done;
3640 done:
3641 if (s->f && fflush(s->f) != 0 && err == NULL)
3642 err = got_error_from_errno("fflush");
3643 return err;
3646 static void
3647 diff_view_indicate_progress(struct tog_view *view)
3649 mvwaddstr(view->window, 0, 0, "diffing...");
3650 update_panels();
3651 doupdate();
3654 static const struct got_error *
3655 search_start_diff_view(struct tog_view *view)
3657 struct tog_diff_view_state *s = &view->state.diff;
3659 s->matched_line = 0;
3660 return NULL;
3663 static const struct got_error *
3664 search_next_diff_view(struct tog_view *view)
3666 struct tog_diff_view_state *s = &view->state.diff;
3667 const struct got_error *err = NULL;
3668 int lineno;
3669 char *exstr = NULL, *line = NULL;
3670 size_t linesize = 0;
3671 ssize_t linelen;
3673 if (!view->searching) {
3674 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3675 return NULL;
3678 if (s->matched_line) {
3679 if (view->searching == TOG_SEARCH_FORWARD)
3680 lineno = s->matched_line + 1;
3681 else
3682 lineno = s->matched_line - 1;
3683 } else
3684 lineno = s->first_displayed_line;
3686 while (1) {
3687 off_t offset;
3689 if (lineno <= 0 || lineno > s->nlines) {
3690 if (s->matched_line == 0) {
3691 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3692 break;
3695 if (view->searching == TOG_SEARCH_FORWARD)
3696 lineno = 1;
3697 else
3698 lineno = s->nlines;
3701 offset = s->line_offsets[lineno - 1];
3702 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3703 free(line);
3704 return got_error_from_errno("fseeko");
3706 linelen = getline(&line, &linesize, s->f);
3707 err = expand_tab(&exstr, line);
3708 if (err)
3709 break;
3710 if (linelen != -1 &&
3711 match_line(exstr, &view->regex, 1, &view->regmatch)) {
3712 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3713 s->matched_line = lineno;
3714 break;
3716 free(exstr);
3717 exstr = NULL;
3718 if (view->searching == TOG_SEARCH_FORWARD)
3719 lineno++;
3720 else
3721 lineno--;
3723 free(line);
3724 free(exstr);
3726 if (s->matched_line) {
3727 s->first_displayed_line = s->matched_line;
3728 s->selected_line = 1;
3731 return err;
3734 static const struct got_error *
3735 close_diff_view(struct tog_view *view)
3737 const struct got_error *err = NULL;
3738 struct tog_diff_view_state *s = &view->state.diff;
3740 free(s->id1);
3741 s->id1 = NULL;
3742 free(s->id2);
3743 s->id2 = NULL;
3744 if (s->f && fclose(s->f) == EOF)
3745 err = got_error_from_errno("fclose");
3746 s->f = NULL;
3747 if (s->f1 && fclose(s->f1) == EOF)
3748 err = got_error_from_errno("fclose");
3749 s->f1 = NULL;
3750 if (s->f2 && fclose(s->f2) == EOF)
3751 err = got_error_from_errno("fclose");
3752 s->f2 = NULL;
3753 free_colors(&s->colors);
3754 free(s->line_offsets);
3755 s->line_offsets = NULL;
3756 s->nlines = 0;
3757 return err;
3760 static const struct got_error *
3761 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3762 struct got_object_id *id2, const char *label1, const char *label2,
3763 int diff_context, int ignore_whitespace, int force_text_diff,
3764 struct tog_view *log_view, struct got_repository *repo)
3766 const struct got_error *err;
3767 struct tog_diff_view_state *s = &view->state.diff;
3769 memset(s, 0, sizeof(*s));
3771 if (id1 != NULL && id2 != NULL) {
3772 int type1, type2;
3773 err = got_object_get_type(&type1, repo, id1);
3774 if (err)
3775 return err;
3776 err = got_object_get_type(&type2, repo, id2);
3777 if (err)
3778 return err;
3780 if (type1 != type2)
3781 return got_error(GOT_ERR_OBJ_TYPE);
3783 s->first_displayed_line = 1;
3784 s->last_displayed_line = view->nlines;
3785 s->selected_line = 1;
3786 s->repo = repo;
3787 s->id1 = id1;
3788 s->id2 = id2;
3789 s->label1 = label1;
3790 s->label2 = label2;
3792 if (id1) {
3793 s->id1 = got_object_id_dup(id1);
3794 if (s->id1 == NULL)
3795 return got_error_from_errno("got_object_id_dup");
3796 s->f1 = got_opentemp();
3797 if (s->f1 == NULL) {
3798 err = got_error_from_errno("got_opentemp");
3799 goto done;
3801 } else
3802 s->id1 = NULL;
3804 s->id2 = got_object_id_dup(id2);
3805 if (s->id2 == NULL) {
3806 err = got_error_from_errno("got_object_id_dup");
3807 goto done;
3810 s->f2 = got_opentemp();
3811 if (s->f2 == NULL) {
3812 err = got_error_from_errno("got_opentemp");
3813 goto done;
3816 s->first_displayed_line = 1;
3817 s->last_displayed_line = view->nlines;
3818 s->diff_context = diff_context;
3819 s->ignore_whitespace = ignore_whitespace;
3820 s->force_text_diff = force_text_diff;
3821 s->log_view = log_view;
3822 s->repo = repo;
3824 STAILQ_INIT(&s->colors);
3825 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3826 err = add_color(&s->colors,
3827 "^-", TOG_COLOR_DIFF_MINUS,
3828 get_color_value("TOG_COLOR_DIFF_MINUS"));
3829 if (err)
3830 goto done;
3831 err = add_color(&s->colors, "^\\+",
3832 TOG_COLOR_DIFF_PLUS,
3833 get_color_value("TOG_COLOR_DIFF_PLUS"));
3834 if (err)
3835 goto done;
3836 err = add_color(&s->colors,
3837 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3838 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3839 if (err)
3840 goto done;
3842 err = add_color(&s->colors,
3843 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3844 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3845 get_color_value("TOG_COLOR_DIFF_META"));
3846 if (err)
3847 goto done;
3849 err = add_color(&s->colors,
3850 "^(from|via): ", TOG_COLOR_AUTHOR,
3851 get_color_value("TOG_COLOR_AUTHOR"));
3852 if (err)
3853 goto done;
3855 err = add_color(&s->colors,
3856 "^date: ", TOG_COLOR_DATE,
3857 get_color_value("TOG_COLOR_DATE"));
3858 if (err)
3859 goto done;
3862 if (log_view && view_is_splitscreen(view))
3863 show_log_view(log_view); /* draw vborder */
3864 diff_view_indicate_progress(view);
3866 err = create_diff(s);
3868 view->show = show_diff_view;
3869 view->input = input_diff_view;
3870 view->close = close_diff_view;
3871 view->search_start = search_start_diff_view;
3872 view->search_next = search_next_diff_view;
3873 done:
3874 if (err)
3875 close_diff_view(view);
3876 return err;
3879 static const struct got_error *
3880 show_diff_view(struct tog_view *view)
3882 const struct got_error *err;
3883 struct tog_diff_view_state *s = &view->state.diff;
3884 char *id_str1 = NULL, *id_str2, *header;
3885 const char *label1, *label2;
3887 if (s->id1) {
3888 err = got_object_id_str(&id_str1, s->id1);
3889 if (err)
3890 return err;
3891 label1 = s->label1 ? : id_str1;
3892 } else
3893 label1 = "/dev/null";
3895 err = got_object_id_str(&id_str2, s->id2);
3896 if (err)
3897 return err;
3898 label2 = s->label2 ? : id_str2;
3900 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3901 err = got_error_from_errno("asprintf");
3902 free(id_str1);
3903 free(id_str2);
3904 return err;
3906 free(id_str1);
3907 free(id_str2);
3909 err = draw_file(view, header);
3910 free(header);
3911 return err;
3914 static const struct got_error *
3915 set_selected_commit(struct tog_diff_view_state *s,
3916 struct commit_queue_entry *entry)
3918 const struct got_error *err;
3919 const struct got_object_id_queue *parent_ids;
3920 struct got_commit_object *selected_commit;
3921 struct got_object_qid *pid;
3923 free(s->id2);
3924 s->id2 = got_object_id_dup(entry->id);
3925 if (s->id2 == NULL)
3926 return got_error_from_errno("got_object_id_dup");
3928 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3929 if (err)
3930 return err;
3931 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3932 free(s->id1);
3933 pid = STAILQ_FIRST(parent_ids);
3934 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3935 got_object_commit_close(selected_commit);
3936 return NULL;
3939 static const struct got_error *
3940 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3942 const struct got_error *err = NULL;
3943 struct tog_diff_view_state *s = &view->state.diff;
3944 struct tog_log_view_state *ls;
3945 struct commit_queue_entry *old_selected_entry;
3946 char *line = NULL;
3947 size_t linesize = 0;
3948 ssize_t linelen;
3949 int i, nscroll = view->nlines - 1;
3951 switch (ch) {
3952 case '0':
3953 view->x = 0;
3954 break;
3955 case '$':
3956 view->x = MAX(view->maxx - view->ncols / 3, 0);
3957 break;
3958 case KEY_RIGHT:
3959 case 'l':
3960 if (view->x + view->ncols / 3 < view->maxx)
3961 view->x += 2; /* move two columns right */
3962 break;
3963 case KEY_LEFT:
3964 case 'h':
3965 view->x -= MIN(view->x, 2); /* move two columns back */
3966 break;
3967 case 'a':
3968 case 'w':
3969 if (ch == 'a')
3970 s->force_text_diff = !s->force_text_diff;
3971 if (ch == 'w')
3972 s->ignore_whitespace = !s->ignore_whitespace;
3973 wclear(view->window);
3974 s->first_displayed_line = 1;
3975 s->last_displayed_line = view->nlines;
3976 s->matched_line = 0;
3977 diff_view_indicate_progress(view);
3978 err = create_diff(s);
3979 break;
3980 case 'g':
3981 case KEY_HOME:
3982 s->first_displayed_line = 1;
3983 break;
3984 case 'G':
3985 case KEY_END:
3986 if (s->eof)
3987 break;
3989 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3990 s->eof = 1;
3991 break;
3992 case 'k':
3993 case KEY_UP:
3994 case CTRL('p'):
3995 if (s->first_displayed_line > 1)
3996 s->first_displayed_line--;
3997 break;
3998 case CTRL('u'):
3999 case 'u':
4000 nscroll /= 2;
4001 /* FALL THROUGH */
4002 case KEY_PPAGE:
4003 case CTRL('b'):
4004 if (s->first_displayed_line == 1)
4005 break;
4006 i = 0;
4007 while (i++ < nscroll && s->first_displayed_line > 1)
4008 s->first_displayed_line--;
4009 break;
4010 case 'j':
4011 case KEY_DOWN:
4012 case CTRL('n'):
4013 if (!s->eof)
4014 s->first_displayed_line++;
4015 break;
4016 case CTRL('d'):
4017 case 'd':
4018 nscroll /= 2;
4019 /* FALL THROUGH */
4020 case KEY_NPAGE:
4021 case CTRL('f'):
4022 case ' ':
4023 if (s->eof)
4024 break;
4025 i = 0;
4026 while (!s->eof && i++ < nscroll) {
4027 linelen = getline(&line, &linesize, s->f);
4028 s->first_displayed_line++;
4029 if (linelen == -1) {
4030 if (feof(s->f)) {
4031 s->eof = 1;
4032 } else
4033 err = got_ferror(s->f, GOT_ERR_IO);
4034 break;
4037 free(line);
4038 break;
4039 case '[':
4040 if (s->diff_context > 0) {
4041 s->diff_context--;
4042 s->matched_line = 0;
4043 diff_view_indicate_progress(view);
4044 err = create_diff(s);
4045 if (s->first_displayed_line + view->nlines - 1 >
4046 s->nlines) {
4047 s->first_displayed_line = 1;
4048 s->last_displayed_line = view->nlines;
4051 break;
4052 case ']':
4053 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4054 s->diff_context++;
4055 s->matched_line = 0;
4056 diff_view_indicate_progress(view);
4057 err = create_diff(s);
4059 break;
4060 case '<':
4061 case ',':
4062 if (s->log_view == NULL)
4063 break;
4064 ls = &s->log_view->state.log;
4065 old_selected_entry = ls->selected_entry;
4067 err = input_log_view(NULL, s->log_view, KEY_UP);
4068 if (err)
4069 break;
4071 if (old_selected_entry == ls->selected_entry)
4072 break;
4074 err = set_selected_commit(s, ls->selected_entry);
4075 if (err)
4076 break;
4078 s->first_displayed_line = 1;
4079 s->last_displayed_line = view->nlines;
4080 s->matched_line = 0;
4081 view->x = 0;
4083 diff_view_indicate_progress(view);
4084 err = create_diff(s);
4085 break;
4086 case '>':
4087 case '.':
4088 if (s->log_view == NULL)
4089 break;
4090 ls = &s->log_view->state.log;
4091 old_selected_entry = ls->selected_entry;
4093 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4094 if (err)
4095 break;
4097 if (old_selected_entry == ls->selected_entry)
4098 break;
4100 err = set_selected_commit(s, ls->selected_entry);
4101 if (err)
4102 break;
4104 s->first_displayed_line = 1;
4105 s->last_displayed_line = view->nlines;
4106 s->matched_line = 0;
4107 view->x = 0;
4109 diff_view_indicate_progress(view);
4110 err = create_diff(s);
4111 break;
4112 default:
4113 break;
4116 return err;
4119 static const struct got_error *
4120 cmd_diff(int argc, char *argv[])
4122 const struct got_error *error = NULL;
4123 struct got_repository *repo = NULL;
4124 struct got_worktree *worktree = NULL;
4125 struct got_object_id *id1 = NULL, *id2 = NULL;
4126 char *repo_path = NULL, *cwd = NULL;
4127 char *id_str1 = NULL, *id_str2 = NULL;
4128 char *label1 = NULL, *label2 = NULL;
4129 int diff_context = 3, ignore_whitespace = 0;
4130 int ch, force_text_diff = 0;
4131 const char *errstr;
4132 struct tog_view *view;
4133 int *pack_fds = NULL;
4135 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4136 switch (ch) {
4137 case 'a':
4138 force_text_diff = 1;
4139 break;
4140 case 'C':
4141 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4142 &errstr);
4143 if (errstr != NULL)
4144 errx(1, "number of context lines is %s: %s",
4145 errstr, errstr);
4146 break;
4147 case 'r':
4148 repo_path = realpath(optarg, NULL);
4149 if (repo_path == NULL)
4150 return got_error_from_errno2("realpath",
4151 optarg);
4152 got_path_strip_trailing_slashes(repo_path);
4153 break;
4154 case 'w':
4155 ignore_whitespace = 1;
4156 break;
4157 default:
4158 usage_diff();
4159 /* NOTREACHED */
4163 argc -= optind;
4164 argv += optind;
4166 if (argc == 0) {
4167 usage_diff(); /* TODO show local worktree changes */
4168 } else if (argc == 2) {
4169 id_str1 = argv[0];
4170 id_str2 = argv[1];
4171 } else
4172 usage_diff();
4174 error = got_repo_pack_fds_open(&pack_fds);
4175 if (error)
4176 goto done;
4178 if (repo_path == NULL) {
4179 cwd = getcwd(NULL, 0);
4180 if (cwd == NULL)
4181 return got_error_from_errno("getcwd");
4182 error = got_worktree_open(&worktree, cwd);
4183 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4184 goto done;
4185 if (worktree)
4186 repo_path =
4187 strdup(got_worktree_get_repo_path(worktree));
4188 else
4189 repo_path = strdup(cwd);
4190 if (repo_path == NULL) {
4191 error = got_error_from_errno("strdup");
4192 goto done;
4196 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4197 if (error)
4198 goto done;
4200 init_curses();
4202 error = apply_unveil(got_repo_get_path(repo), NULL);
4203 if (error)
4204 goto done;
4206 error = tog_load_refs(repo, 0);
4207 if (error)
4208 goto done;
4210 error = got_repo_match_object_id(&id1, &label1, id_str1,
4211 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4212 if (error)
4213 goto done;
4215 error = got_repo_match_object_id(&id2, &label2, id_str2,
4216 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4217 if (error)
4218 goto done;
4220 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4221 if (view == NULL) {
4222 error = got_error_from_errno("view_open");
4223 goto done;
4225 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4226 ignore_whitespace, force_text_diff, NULL, repo);
4227 if (error)
4228 goto done;
4229 error = view_loop(view);
4230 done:
4231 free(label1);
4232 free(label2);
4233 free(repo_path);
4234 free(cwd);
4235 if (repo) {
4236 const struct got_error *close_err = got_repo_close(repo);
4237 if (error == NULL)
4238 error = close_err;
4240 if (worktree)
4241 got_worktree_close(worktree);
4242 if (pack_fds) {
4243 const struct got_error *pack_err =
4244 got_repo_pack_fds_close(pack_fds);
4245 if (error == NULL)
4246 error = pack_err;
4248 tog_free_refs();
4249 return error;
4252 __dead static void
4253 usage_blame(void)
4255 endwin();
4256 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4257 getprogname());
4258 exit(1);
4261 struct tog_blame_line {
4262 int annotated;
4263 struct got_object_id *id;
4266 static const struct got_error *
4267 draw_blame(struct tog_view *view)
4269 struct tog_blame_view_state *s = &view->state.blame;
4270 struct tog_blame *blame = &s->blame;
4271 regmatch_t *regmatch = &view->regmatch;
4272 const struct got_error *err;
4273 int lineno = 0, nprinted = 0, i;
4274 char *line = NULL;
4275 size_t linesize = 0;
4276 ssize_t linelen;
4277 wchar_t *wline;
4278 int width;
4279 struct tog_blame_line *blame_line;
4280 struct got_object_id *prev_id = NULL;
4281 char *id_str;
4282 struct tog_color *tc;
4284 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4285 if (err)
4286 return err;
4288 rewind(blame->f);
4289 werase(view->window);
4291 if (asprintf(&line, "commit %s", id_str) == -1) {
4292 err = got_error_from_errno("asprintf");
4293 free(id_str);
4294 return err;
4297 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4298 free(line);
4299 line = NULL;
4300 if (err)
4301 return err;
4302 if (view_needs_focus_indication(view))
4303 wstandout(view->window);
4304 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4305 if (tc)
4306 wattr_on(view->window,
4307 COLOR_PAIR(tc->colorpair), NULL);
4308 waddwstr(view->window, wline);
4309 if (tc)
4310 wattr_off(view->window,
4311 COLOR_PAIR(tc->colorpair), NULL);
4312 if (view_needs_focus_indication(view))
4313 wstandend(view->window);
4314 free(wline);
4315 wline = NULL;
4316 if (width < view->ncols - 1)
4317 waddch(view->window, '\n');
4319 if (asprintf(&line, "[%d/%d] %s%s",
4320 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4321 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4322 free(id_str);
4323 return got_error_from_errno("asprintf");
4325 free(id_str);
4326 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4327 free(line);
4328 line = NULL;
4329 if (err)
4330 return err;
4331 waddwstr(view->window, wline);
4332 free(wline);
4333 wline = NULL;
4334 if (width < view->ncols - 1)
4335 waddch(view->window, '\n');
4337 s->eof = 0;
4338 view->maxx = 0;
4339 while (nprinted < view->nlines - 2) {
4340 linelen = getline(&line, &linesize, blame->f);
4341 if (linelen == -1) {
4342 if (feof(blame->f)) {
4343 s->eof = 1;
4344 break;
4346 free(line);
4347 return got_ferror(blame->f, GOT_ERR_IO);
4349 if (++lineno < s->first_displayed_line)
4350 continue;
4352 view->maxx = MAX(view->maxx, linelen);
4354 if (view->focussed && nprinted == s->selected_line - 1)
4355 wstandout(view->window);
4357 if (blame->nlines > 0) {
4358 blame_line = &blame->lines[lineno - 1];
4359 if (blame_line->annotated && prev_id &&
4360 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4361 !(view->focussed &&
4362 nprinted == s->selected_line - 1)) {
4363 waddstr(view->window, " ");
4364 } else if (blame_line->annotated) {
4365 char *id_str;
4366 err = got_object_id_str(&id_str, blame_line->id);
4367 if (err) {
4368 free(line);
4369 return err;
4371 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4372 if (tc)
4373 wattr_on(view->window,
4374 COLOR_PAIR(tc->colorpair), NULL);
4375 wprintw(view->window, "%.8s", id_str);
4376 if (tc)
4377 wattr_off(view->window,
4378 COLOR_PAIR(tc->colorpair), NULL);
4379 free(id_str);
4380 prev_id = blame_line->id;
4381 } else {
4382 waddstr(view->window, "........");
4383 prev_id = NULL;
4385 } else {
4386 waddstr(view->window, "........");
4387 prev_id = NULL;
4390 if (view->focussed && nprinted == s->selected_line - 1)
4391 wstandend(view->window);
4392 waddstr(view->window, " ");
4394 if (view->ncols <= 9) {
4395 width = 9;
4396 wline = wcsdup(L"");
4397 if (wline == NULL) {
4398 err = got_error_from_errno("wcsdup");
4399 free(line);
4400 return err;
4402 } else if (s->first_displayed_line + nprinted ==
4403 s->matched_line &&
4404 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4405 err = add_matched_line(&width, line, view->ncols - 9, 9,
4406 view->window, view->x, regmatch);
4407 if (err) {
4408 free(line);
4409 return err;
4411 width += 9;
4412 } else {
4413 err = format_line(&wline, &width, NULL, line, 0,
4414 view->x + view->ncols - 9, 9, 1);
4415 if (err) {
4416 free(line);
4417 return err;
4419 if (view->x < width) {
4420 waddwstr(view->window, wline + view->x);
4421 for (i = 0; i < view->x; i++)
4422 width -= wcwidth(wline[i]);
4424 width += 9;
4425 free(wline);
4426 wline = NULL;
4429 if (width <= view->ncols - 1)
4430 waddch(view->window, '\n');
4431 if (++nprinted == 1)
4432 s->first_displayed_line = lineno;
4434 free(line);
4435 s->last_displayed_line = lineno;
4437 view_vborder(view);
4439 return NULL;
4442 static const struct got_error *
4443 blame_cb(void *arg, int nlines, int lineno,
4444 struct got_commit_object *commit, struct got_object_id *id)
4446 const struct got_error *err = NULL;
4447 struct tog_blame_cb_args *a = arg;
4448 struct tog_blame_line *line;
4449 int errcode;
4451 if (nlines != a->nlines ||
4452 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4453 return got_error(GOT_ERR_RANGE);
4455 errcode = pthread_mutex_lock(&tog_mutex);
4456 if (errcode)
4457 return got_error_set_errno(errcode, "pthread_mutex_lock");
4459 if (*a->quit) { /* user has quit the blame view */
4460 err = got_error(GOT_ERR_ITER_COMPLETED);
4461 goto done;
4464 if (lineno == -1)
4465 goto done; /* no change in this commit */
4467 line = &a->lines[lineno - 1];
4468 if (line->annotated)
4469 goto done;
4471 line->id = got_object_id_dup(id);
4472 if (line->id == NULL) {
4473 err = got_error_from_errno("got_object_id_dup");
4474 goto done;
4476 line->annotated = 1;
4477 done:
4478 errcode = pthread_mutex_unlock(&tog_mutex);
4479 if (errcode)
4480 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4481 return err;
4484 static void *
4485 blame_thread(void *arg)
4487 const struct got_error *err, *close_err;
4488 struct tog_blame_thread_args *ta = arg;
4489 struct tog_blame_cb_args *a = ta->cb_args;
4490 int errcode;
4492 err = block_signals_used_by_main_thread();
4493 if (err)
4494 return (void *)err;
4496 err = got_blame(ta->path, a->commit_id, ta->repo,
4497 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4498 if (err && err->code == GOT_ERR_CANCELLED)
4499 err = NULL;
4501 errcode = pthread_mutex_lock(&tog_mutex);
4502 if (errcode)
4503 return (void *)got_error_set_errno(errcode,
4504 "pthread_mutex_lock");
4506 close_err = got_repo_close(ta->repo);
4507 if (err == NULL)
4508 err = close_err;
4509 ta->repo = NULL;
4510 *ta->complete = 1;
4512 errcode = pthread_mutex_unlock(&tog_mutex);
4513 if (errcode && err == NULL)
4514 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4516 return (void *)err;
4519 static struct got_object_id *
4520 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4521 int first_displayed_line, int selected_line)
4523 struct tog_blame_line *line;
4525 if (nlines <= 0)
4526 return NULL;
4528 line = &lines[first_displayed_line - 1 + selected_line - 1];
4529 if (!line->annotated)
4530 return NULL;
4532 return line->id;
4535 static const struct got_error *
4536 stop_blame(struct tog_blame *blame)
4538 const struct got_error *err = NULL;
4539 int i;
4541 if (blame->thread) {
4542 int errcode;
4543 errcode = pthread_mutex_unlock(&tog_mutex);
4544 if (errcode)
4545 return got_error_set_errno(errcode,
4546 "pthread_mutex_unlock");
4547 errcode = pthread_join(blame->thread, (void **)&err);
4548 if (errcode)
4549 return got_error_set_errno(errcode, "pthread_join");
4550 errcode = pthread_mutex_lock(&tog_mutex);
4551 if (errcode)
4552 return got_error_set_errno(errcode,
4553 "pthread_mutex_lock");
4554 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4555 err = NULL;
4556 blame->thread = 0; //NULL;
4558 if (blame->thread_args.repo) {
4559 const struct got_error *close_err;
4560 close_err = got_repo_close(blame->thread_args.repo);
4561 if (err == NULL)
4562 err = close_err;
4563 blame->thread_args.repo = NULL;
4565 if (blame->f) {
4566 if (fclose(blame->f) == EOF && err == NULL)
4567 err = got_error_from_errno("fclose");
4568 blame->f = NULL;
4570 if (blame->lines) {
4571 for (i = 0; i < blame->nlines; i++)
4572 free(blame->lines[i].id);
4573 free(blame->lines);
4574 blame->lines = NULL;
4576 free(blame->cb_args.commit_id);
4577 blame->cb_args.commit_id = NULL;
4578 if (blame->pack_fds) {
4579 const struct got_error *pack_err =
4580 got_repo_pack_fds_close(blame->pack_fds);
4581 if (err == NULL)
4582 err = pack_err;
4583 blame->pack_fds = NULL;
4585 return err;
4588 static const struct got_error *
4589 cancel_blame_view(void *arg)
4591 const struct got_error *err = NULL;
4592 int *done = arg;
4593 int errcode;
4595 errcode = pthread_mutex_lock(&tog_mutex);
4596 if (errcode)
4597 return got_error_set_errno(errcode,
4598 "pthread_mutex_unlock");
4600 if (*done)
4601 err = got_error(GOT_ERR_CANCELLED);
4603 errcode = pthread_mutex_unlock(&tog_mutex);
4604 if (errcode)
4605 return got_error_set_errno(errcode,
4606 "pthread_mutex_lock");
4608 return err;
4611 static const struct got_error *
4612 run_blame(struct tog_view *view)
4614 struct tog_blame_view_state *s = &view->state.blame;
4615 struct tog_blame *blame = &s->blame;
4616 const struct got_error *err = NULL;
4617 struct got_commit_object *commit = NULL;
4618 struct got_blob_object *blob = NULL;
4619 struct got_repository *thread_repo = NULL;
4620 struct got_object_id *obj_id = NULL;
4621 int obj_type;
4622 int *pack_fds = NULL;
4624 err = got_object_open_as_commit(&commit, s->repo,
4625 &s->blamed_commit->id);
4626 if (err)
4627 return err;
4629 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4630 if (err)
4631 goto done;
4633 err = got_object_get_type(&obj_type, s->repo, obj_id);
4634 if (err)
4635 goto done;
4637 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4638 err = got_error(GOT_ERR_OBJ_TYPE);
4639 goto done;
4642 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4643 if (err)
4644 goto done;
4645 blame->f = got_opentemp();
4646 if (blame->f == NULL) {
4647 err = got_error_from_errno("got_opentemp");
4648 goto done;
4650 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4651 &blame->line_offsets, blame->f, blob);
4652 if (err)
4653 goto done;
4654 if (blame->nlines == 0) {
4655 s->blame_complete = 1;
4656 goto done;
4659 /* Don't include \n at EOF in the blame line count. */
4660 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4661 blame->nlines--;
4663 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4664 if (blame->lines == NULL) {
4665 err = got_error_from_errno("calloc");
4666 goto done;
4669 err = got_repo_pack_fds_open(&pack_fds);
4670 if (err)
4671 goto done;
4672 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4673 pack_fds);
4674 if (err)
4675 goto done;
4677 blame->pack_fds = pack_fds;
4678 blame->cb_args.view = view;
4679 blame->cb_args.lines = blame->lines;
4680 blame->cb_args.nlines = blame->nlines;
4681 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4682 if (blame->cb_args.commit_id == NULL) {
4683 err = got_error_from_errno("got_object_id_dup");
4684 goto done;
4686 blame->cb_args.quit = &s->done;
4688 blame->thread_args.path = s->path;
4689 blame->thread_args.repo = thread_repo;
4690 blame->thread_args.cb_args = &blame->cb_args;
4691 blame->thread_args.complete = &s->blame_complete;
4692 blame->thread_args.cancel_cb = cancel_blame_view;
4693 blame->thread_args.cancel_arg = &s->done;
4694 s->blame_complete = 0;
4696 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4697 s->first_displayed_line = 1;
4698 s->last_displayed_line = view->nlines;
4699 s->selected_line = 1;
4701 s->matched_line = 0;
4703 done:
4704 if (commit)
4705 got_object_commit_close(commit);
4706 if (blob)
4707 got_object_blob_close(blob);
4708 free(obj_id);
4709 if (err)
4710 stop_blame(blame);
4711 return err;
4714 static const struct got_error *
4715 open_blame_view(struct tog_view *view, char *path,
4716 struct got_object_id *commit_id, struct got_repository *repo)
4718 const struct got_error *err = NULL;
4719 struct tog_blame_view_state *s = &view->state.blame;
4721 STAILQ_INIT(&s->blamed_commits);
4723 s->path = strdup(path);
4724 if (s->path == NULL)
4725 return got_error_from_errno("strdup");
4727 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4728 if (err) {
4729 free(s->path);
4730 return err;
4733 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4734 s->first_displayed_line = 1;
4735 s->last_displayed_line = view->nlines;
4736 s->selected_line = 1;
4737 s->blame_complete = 0;
4738 s->repo = repo;
4739 s->commit_id = commit_id;
4740 memset(&s->blame, 0, sizeof(s->blame));
4742 STAILQ_INIT(&s->colors);
4743 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4744 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4745 get_color_value("TOG_COLOR_COMMIT"));
4746 if (err)
4747 return err;
4750 view->show = show_blame_view;
4751 view->input = input_blame_view;
4752 view->close = close_blame_view;
4753 view->search_start = search_start_blame_view;
4754 view->search_next = search_next_blame_view;
4756 return run_blame(view);
4759 static const struct got_error *
4760 close_blame_view(struct tog_view *view)
4762 const struct got_error *err = NULL;
4763 struct tog_blame_view_state *s = &view->state.blame;
4765 if (s->blame.thread)
4766 err = stop_blame(&s->blame);
4768 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4769 struct got_object_qid *blamed_commit;
4770 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4771 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4772 got_object_qid_free(blamed_commit);
4775 free(s->path);
4776 free_colors(&s->colors);
4777 return err;
4780 static const struct got_error *
4781 search_start_blame_view(struct tog_view *view)
4783 struct tog_blame_view_state *s = &view->state.blame;
4785 s->matched_line = 0;
4786 return NULL;
4789 static const struct got_error *
4790 search_next_blame_view(struct tog_view *view)
4792 struct tog_blame_view_state *s = &view->state.blame;
4793 const struct got_error *err = NULL;
4794 int lineno;
4795 char *exstr = NULL, *line = NULL;
4796 size_t linesize = 0;
4797 ssize_t linelen;
4799 if (!view->searching) {
4800 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4801 return NULL;
4804 if (s->matched_line) {
4805 if (view->searching == TOG_SEARCH_FORWARD)
4806 lineno = s->matched_line + 1;
4807 else
4808 lineno = s->matched_line - 1;
4809 } else
4810 lineno = s->first_displayed_line - 1 + s->selected_line;
4812 while (1) {
4813 off_t offset;
4815 if (lineno <= 0 || lineno > s->blame.nlines) {
4816 if (s->matched_line == 0) {
4817 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4818 break;
4821 if (view->searching == TOG_SEARCH_FORWARD)
4822 lineno = 1;
4823 else
4824 lineno = s->blame.nlines;
4827 offset = s->blame.line_offsets[lineno - 1];
4828 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4829 free(line);
4830 return got_error_from_errno("fseeko");
4832 linelen = getline(&line, &linesize, s->blame.f);
4833 err = expand_tab(&exstr, line);
4834 if (err)
4835 break;
4836 if (linelen != -1 &&
4837 match_line(exstr, &view->regex, 1, &view->regmatch)) {
4838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4839 s->matched_line = lineno;
4840 break;
4842 free(exstr);
4843 exstr = NULL;
4844 if (view->searching == TOG_SEARCH_FORWARD)
4845 lineno++;
4846 else
4847 lineno--;
4849 free(line);
4850 free(exstr);
4852 if (s->matched_line) {
4853 s->first_displayed_line = s->matched_line;
4854 s->selected_line = 1;
4857 return err;
4860 static const struct got_error *
4861 show_blame_view(struct tog_view *view)
4863 const struct got_error *err = NULL;
4864 struct tog_blame_view_state *s = &view->state.blame;
4865 int errcode;
4867 if (s->blame.thread == 0 && !s->blame_complete) {
4868 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4869 &s->blame.thread_args);
4870 if (errcode)
4871 return got_error_set_errno(errcode, "pthread_create");
4873 halfdelay(1); /* fast refresh while annotating */
4876 if (s->blame_complete)
4877 halfdelay(10); /* disable fast refresh */
4879 err = draw_blame(view);
4881 view_vborder(view);
4882 return err;
4885 static const struct got_error *
4886 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4888 const struct got_error *err = NULL, *thread_err = NULL;
4889 struct tog_view *diff_view;
4890 struct tog_blame_view_state *s = &view->state.blame;
4891 int begin_x = 0, nscroll = view->nlines - 2;
4893 switch (ch) {
4894 case '0':
4895 view->x = 0;
4896 break;
4897 case '$':
4898 view->x = MAX(view->maxx - view->ncols / 3, 0);
4899 break;
4900 case KEY_RIGHT:
4901 case 'l':
4902 if (view->x + view->ncols / 3 < view->maxx)
4903 view->x += 2; /* move two columns right */
4904 break;
4905 case KEY_LEFT:
4906 case 'h':
4907 view->x -= MIN(view->x, 2); /* move two columns back */
4908 break;
4909 case 'q':
4910 s->done = 1;
4911 break;
4912 case 'g':
4913 case KEY_HOME:
4914 s->selected_line = 1;
4915 s->first_displayed_line = 1;
4916 break;
4917 case 'G':
4918 case KEY_END:
4919 if (s->blame.nlines < view->nlines - 2) {
4920 s->selected_line = s->blame.nlines;
4921 s->first_displayed_line = 1;
4922 } else {
4923 s->selected_line = view->nlines - 2;
4924 s->first_displayed_line = s->blame.nlines -
4925 (view->nlines - 3);
4927 break;
4928 case 'k':
4929 case KEY_UP:
4930 case CTRL('p'):
4931 if (s->selected_line > 1)
4932 s->selected_line--;
4933 else if (s->selected_line == 1 &&
4934 s->first_displayed_line > 1)
4935 s->first_displayed_line--;
4936 break;
4937 case CTRL('u'):
4938 case 'u':
4939 nscroll /= 2;
4940 /* FALL THROUGH */
4941 case KEY_PPAGE:
4942 case CTRL('b'):
4943 if (s->first_displayed_line == 1) {
4944 s->selected_line = MAX(1, s->selected_line - nscroll);
4945 break;
4947 if (s->first_displayed_line > nscroll)
4948 s->first_displayed_line -= nscroll;
4949 else
4950 s->first_displayed_line = 1;
4951 break;
4952 case 'j':
4953 case KEY_DOWN:
4954 case CTRL('n'):
4955 if (s->selected_line < view->nlines - 2 &&
4956 s->first_displayed_line +
4957 s->selected_line <= s->blame.nlines)
4958 s->selected_line++;
4959 else if (s->last_displayed_line <
4960 s->blame.nlines)
4961 s->first_displayed_line++;
4962 break;
4963 case 'b':
4964 case 'p': {
4965 struct got_object_id *id = NULL;
4966 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4967 s->first_displayed_line, s->selected_line);
4968 if (id == NULL)
4969 break;
4970 if (ch == 'p') {
4971 struct got_commit_object *commit, *pcommit;
4972 struct got_object_qid *pid;
4973 struct got_object_id *blob_id = NULL;
4974 int obj_type;
4975 err = got_object_open_as_commit(&commit,
4976 s->repo, id);
4977 if (err)
4978 break;
4979 pid = STAILQ_FIRST(
4980 got_object_commit_get_parent_ids(commit));
4981 if (pid == NULL) {
4982 got_object_commit_close(commit);
4983 break;
4985 /* Check if path history ends here. */
4986 err = got_object_open_as_commit(&pcommit,
4987 s->repo, &pid->id);
4988 if (err)
4989 break;
4990 err = got_object_id_by_path(&blob_id, s->repo,
4991 pcommit, s->path);
4992 got_object_commit_close(pcommit);
4993 if (err) {
4994 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4995 err = NULL;
4996 got_object_commit_close(commit);
4997 break;
4999 err = got_object_get_type(&obj_type, s->repo,
5000 blob_id);
5001 free(blob_id);
5002 /* Can't blame non-blob type objects. */
5003 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5004 got_object_commit_close(commit);
5005 break;
5007 err = got_object_qid_alloc(&s->blamed_commit,
5008 &pid->id);
5009 got_object_commit_close(commit);
5010 } else {
5011 if (got_object_id_cmp(id,
5012 &s->blamed_commit->id) == 0)
5013 break;
5014 err = got_object_qid_alloc(&s->blamed_commit,
5015 id);
5017 if (err)
5018 break;
5019 s->done = 1;
5020 thread_err = stop_blame(&s->blame);
5021 s->done = 0;
5022 if (thread_err)
5023 break;
5024 STAILQ_INSERT_HEAD(&s->blamed_commits,
5025 s->blamed_commit, entry);
5026 err = run_blame(view);
5027 if (err)
5028 break;
5029 break;
5031 case 'B': {
5032 struct got_object_qid *first;
5033 first = STAILQ_FIRST(&s->blamed_commits);
5034 if (!got_object_id_cmp(&first->id, s->commit_id))
5035 break;
5036 s->done = 1;
5037 thread_err = stop_blame(&s->blame);
5038 s->done = 0;
5039 if (thread_err)
5040 break;
5041 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5042 got_object_qid_free(s->blamed_commit);
5043 s->blamed_commit =
5044 STAILQ_FIRST(&s->blamed_commits);
5045 err = run_blame(view);
5046 if (err)
5047 break;
5048 break;
5050 case KEY_ENTER:
5051 case '\r': {
5052 struct got_object_id *id = NULL;
5053 struct got_object_qid *pid;
5054 struct got_commit_object *commit = NULL;
5055 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5056 s->first_displayed_line, s->selected_line);
5057 if (id == NULL)
5058 break;
5059 err = got_object_open_as_commit(&commit, s->repo, id);
5060 if (err)
5061 break;
5062 pid = STAILQ_FIRST(
5063 got_object_commit_get_parent_ids(commit));
5064 if (view_is_parent_view(view))
5065 begin_x = view_split_begin_x(view->begin_x);
5066 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5067 if (diff_view == NULL) {
5068 got_object_commit_close(commit);
5069 err = got_error_from_errno("view_open");
5070 break;
5072 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5073 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5074 got_object_commit_close(commit);
5075 if (err) {
5076 view_close(diff_view);
5077 break;
5079 view->focussed = 0;
5080 diff_view->focussed = 1;
5081 if (view_is_parent_view(view)) {
5082 err = view_close_child(view);
5083 if (err)
5084 break;
5085 view_set_child(view, diff_view);
5086 view->focus_child = 1;
5087 } else
5088 *new_view = diff_view;
5089 if (err)
5090 break;
5091 break;
5093 case CTRL('d'):
5094 case 'd':
5095 nscroll /= 2;
5096 /* FALL THROUGH */
5097 case KEY_NPAGE:
5098 case CTRL('f'):
5099 case ' ':
5100 if (s->last_displayed_line >= s->blame.nlines &&
5101 s->selected_line >= MIN(s->blame.nlines,
5102 view->nlines - 2)) {
5103 break;
5105 if (s->last_displayed_line >= s->blame.nlines &&
5106 s->selected_line < view->nlines - 2) {
5107 s->selected_line +=
5108 MIN(nscroll, s->last_displayed_line -
5109 s->first_displayed_line - s->selected_line + 1);
5111 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5112 s->first_displayed_line += nscroll;
5113 else
5114 s->first_displayed_line =
5115 s->blame.nlines - (view->nlines - 3);
5116 break;
5117 case KEY_RESIZE:
5118 if (s->selected_line > view->nlines - 2) {
5119 s->selected_line = MIN(s->blame.nlines,
5120 view->nlines - 2);
5122 break;
5123 default:
5124 break;
5126 return thread_err ? thread_err : err;
5129 static const struct got_error *
5130 cmd_blame(int argc, char *argv[])
5132 const struct got_error *error;
5133 struct got_repository *repo = NULL;
5134 struct got_worktree *worktree = NULL;
5135 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5136 char *link_target = NULL;
5137 struct got_object_id *commit_id = NULL;
5138 struct got_commit_object *commit = NULL;
5139 char *commit_id_str = NULL;
5140 int ch;
5141 struct tog_view *view;
5142 int *pack_fds = NULL;
5144 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5145 switch (ch) {
5146 case 'c':
5147 commit_id_str = optarg;
5148 break;
5149 case 'r':
5150 repo_path = realpath(optarg, NULL);
5151 if (repo_path == NULL)
5152 return got_error_from_errno2("realpath",
5153 optarg);
5154 break;
5155 default:
5156 usage_blame();
5157 /* NOTREACHED */
5161 argc -= optind;
5162 argv += optind;
5164 if (argc != 1)
5165 usage_blame();
5167 error = got_repo_pack_fds_open(&pack_fds);
5168 if (error != NULL)
5169 goto done;
5171 if (repo_path == NULL) {
5172 cwd = getcwd(NULL, 0);
5173 if (cwd == NULL)
5174 return got_error_from_errno("getcwd");
5175 error = got_worktree_open(&worktree, cwd);
5176 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5177 goto done;
5178 if (worktree)
5179 repo_path =
5180 strdup(got_worktree_get_repo_path(worktree));
5181 else
5182 repo_path = strdup(cwd);
5183 if (repo_path == NULL) {
5184 error = got_error_from_errno("strdup");
5185 goto done;
5189 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5190 if (error != NULL)
5191 goto done;
5193 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5194 worktree);
5195 if (error)
5196 goto done;
5198 init_curses();
5200 error = apply_unveil(got_repo_get_path(repo), NULL);
5201 if (error)
5202 goto done;
5204 error = tog_load_refs(repo, 0);
5205 if (error)
5206 goto done;
5208 if (commit_id_str == NULL) {
5209 struct got_reference *head_ref;
5210 error = got_ref_open(&head_ref, repo, worktree ?
5211 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5212 if (error != NULL)
5213 goto done;
5214 error = got_ref_resolve(&commit_id, repo, head_ref);
5215 got_ref_close(head_ref);
5216 } else {
5217 error = got_repo_match_object_id(&commit_id, NULL,
5218 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5220 if (error != NULL)
5221 goto done;
5223 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5224 if (view == NULL) {
5225 error = got_error_from_errno("view_open");
5226 goto done;
5229 error = got_object_open_as_commit(&commit, repo, commit_id);
5230 if (error)
5231 goto done;
5233 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5234 commit, repo);
5235 if (error)
5236 goto done;
5238 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5239 commit_id, repo);
5240 if (error)
5241 goto done;
5242 if (worktree) {
5243 /* Release work tree lock. */
5244 got_worktree_close(worktree);
5245 worktree = NULL;
5247 error = view_loop(view);
5248 done:
5249 free(repo_path);
5250 free(in_repo_path);
5251 free(link_target);
5252 free(cwd);
5253 free(commit_id);
5254 if (commit)
5255 got_object_commit_close(commit);
5256 if (worktree)
5257 got_worktree_close(worktree);
5258 if (repo) {
5259 const struct got_error *close_err = got_repo_close(repo);
5260 if (error == NULL)
5261 error = close_err;
5263 if (pack_fds) {
5264 const struct got_error *pack_err =
5265 got_repo_pack_fds_close(pack_fds);
5266 if (error == NULL)
5267 error = pack_err;
5269 tog_free_refs();
5270 return error;
5273 static const struct got_error *
5274 draw_tree_entries(struct tog_view *view, const char *parent_path)
5276 struct tog_tree_view_state *s = &view->state.tree;
5277 const struct got_error *err = NULL;
5278 struct got_tree_entry *te;
5279 wchar_t *wline;
5280 struct tog_color *tc;
5281 int width, n, i, nentries;
5282 int limit = view->nlines;
5284 s->ndisplayed = 0;
5286 werase(view->window);
5288 if (limit == 0)
5289 return NULL;
5291 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5292 0, 0);
5293 if (err)
5294 return err;
5295 if (view_needs_focus_indication(view))
5296 wstandout(view->window);
5297 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5298 if (tc)
5299 wattr_on(view->window,
5300 COLOR_PAIR(tc->colorpair), NULL);
5301 waddwstr(view->window, wline);
5302 if (tc)
5303 wattr_off(view->window,
5304 COLOR_PAIR(tc->colorpair), NULL);
5305 if (view_needs_focus_indication(view))
5306 wstandend(view->window);
5307 free(wline);
5308 wline = NULL;
5309 if (width < view->ncols - 1)
5310 waddch(view->window, '\n');
5311 if (--limit <= 0)
5312 return NULL;
5313 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5314 0, 0);
5315 if (err)
5316 return err;
5317 waddwstr(view->window, wline);
5318 free(wline);
5319 wline = NULL;
5320 if (width < view->ncols - 1)
5321 waddch(view->window, '\n');
5322 if (--limit <= 0)
5323 return NULL;
5324 waddch(view->window, '\n');
5325 if (--limit <= 0)
5326 return NULL;
5328 if (s->first_displayed_entry == NULL) {
5329 te = got_object_tree_get_first_entry(s->tree);
5330 if (s->selected == 0) {
5331 if (view->focussed)
5332 wstandout(view->window);
5333 s->selected_entry = NULL;
5335 waddstr(view->window, " ..\n"); /* parent directory */
5336 if (s->selected == 0 && view->focussed)
5337 wstandend(view->window);
5338 s->ndisplayed++;
5339 if (--limit <= 0)
5340 return NULL;
5341 n = 1;
5342 } else {
5343 n = 0;
5344 te = s->first_displayed_entry;
5347 nentries = got_object_tree_get_nentries(s->tree);
5348 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5349 char *line = NULL, *id_str = NULL, *link_target = NULL;
5350 const char *modestr = "";
5351 mode_t mode;
5353 te = got_object_tree_get_entry(s->tree, i);
5354 mode = got_tree_entry_get_mode(te);
5356 if (s->show_ids) {
5357 err = got_object_id_str(&id_str,
5358 got_tree_entry_get_id(te));
5359 if (err)
5360 return got_error_from_errno(
5361 "got_object_id_str");
5363 if (got_object_tree_entry_is_submodule(te))
5364 modestr = "$";
5365 else if (S_ISLNK(mode)) {
5366 int i;
5368 err = got_tree_entry_get_symlink_target(&link_target,
5369 te, s->repo);
5370 if (err) {
5371 free(id_str);
5372 return err;
5374 for (i = 0; i < strlen(link_target); i++) {
5375 if (!isprint((unsigned char)link_target[i]))
5376 link_target[i] = '?';
5378 modestr = "@";
5380 else if (S_ISDIR(mode))
5381 modestr = "/";
5382 else if (mode & S_IXUSR)
5383 modestr = "*";
5384 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5385 got_tree_entry_get_name(te), modestr,
5386 link_target ? " -> ": "",
5387 link_target ? link_target : "") == -1) {
5388 free(id_str);
5389 free(link_target);
5390 return got_error_from_errno("asprintf");
5392 free(id_str);
5393 free(link_target);
5394 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5395 0, 0);
5396 if (err) {
5397 free(line);
5398 break;
5400 if (n == s->selected) {
5401 if (view->focussed)
5402 wstandout(view->window);
5403 s->selected_entry = te;
5405 tc = match_color(&s->colors, line);
5406 if (tc)
5407 wattr_on(view->window,
5408 COLOR_PAIR(tc->colorpair), NULL);
5409 waddwstr(view->window, wline);
5410 if (tc)
5411 wattr_off(view->window,
5412 COLOR_PAIR(tc->colorpair), NULL);
5413 if (width < view->ncols - 1)
5414 waddch(view->window, '\n');
5415 if (n == s->selected && view->focussed)
5416 wstandend(view->window);
5417 free(line);
5418 free(wline);
5419 wline = NULL;
5420 n++;
5421 s->ndisplayed++;
5422 s->last_displayed_entry = te;
5423 if (--limit <= 0)
5424 break;
5427 return err;
5430 static void
5431 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5433 struct got_tree_entry *te;
5434 int isroot = s->tree == s->root;
5435 int i = 0;
5437 if (s->first_displayed_entry == NULL)
5438 return;
5440 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5441 while (i++ < maxscroll) {
5442 if (te == NULL) {
5443 if (!isroot)
5444 s->first_displayed_entry = NULL;
5445 break;
5447 s->first_displayed_entry = te;
5448 te = got_tree_entry_get_prev(s->tree, te);
5452 static void
5453 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5455 struct got_tree_entry *next, *last;
5456 int n = 0;
5458 if (s->first_displayed_entry)
5459 next = got_tree_entry_get_next(s->tree,
5460 s->first_displayed_entry);
5461 else
5462 next = got_object_tree_get_first_entry(s->tree);
5464 last = s->last_displayed_entry;
5465 while (next && last && n++ < maxscroll) {
5466 last = got_tree_entry_get_next(s->tree, last);
5467 if (last) {
5468 s->first_displayed_entry = next;
5469 next = got_tree_entry_get_next(s->tree, next);
5474 static const struct got_error *
5475 tree_entry_path(char **path, struct tog_parent_trees *parents,
5476 struct got_tree_entry *te)
5478 const struct got_error *err = NULL;
5479 struct tog_parent_tree *pt;
5480 size_t len = 2; /* for leading slash and NUL */
5482 TAILQ_FOREACH(pt, parents, entry)
5483 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5484 + 1 /* slash */;
5485 if (te)
5486 len += strlen(got_tree_entry_get_name(te));
5488 *path = calloc(1, len);
5489 if (path == NULL)
5490 return got_error_from_errno("calloc");
5492 (*path)[0] = '/';
5493 pt = TAILQ_LAST(parents, tog_parent_trees);
5494 while (pt) {
5495 const char *name = got_tree_entry_get_name(pt->selected_entry);
5496 if (strlcat(*path, name, len) >= len) {
5497 err = got_error(GOT_ERR_NO_SPACE);
5498 goto done;
5500 if (strlcat(*path, "/", len) >= len) {
5501 err = got_error(GOT_ERR_NO_SPACE);
5502 goto done;
5504 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5506 if (te) {
5507 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5508 err = got_error(GOT_ERR_NO_SPACE);
5509 goto done;
5512 done:
5513 if (err) {
5514 free(*path);
5515 *path = NULL;
5517 return err;
5520 static const struct got_error *
5521 blame_tree_entry(struct tog_view **new_view, int begin_x,
5522 struct got_tree_entry *te, struct tog_parent_trees *parents,
5523 struct got_object_id *commit_id, struct got_repository *repo)
5525 const struct got_error *err = NULL;
5526 char *path;
5527 struct tog_view *blame_view;
5529 *new_view = NULL;
5531 err = tree_entry_path(&path, parents, te);
5532 if (err)
5533 return err;
5535 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5536 if (blame_view == NULL) {
5537 err = got_error_from_errno("view_open");
5538 goto done;
5541 err = open_blame_view(blame_view, path, commit_id, repo);
5542 if (err) {
5543 if (err->code == GOT_ERR_CANCELLED)
5544 err = NULL;
5545 view_close(blame_view);
5546 } else
5547 *new_view = blame_view;
5548 done:
5549 free(path);
5550 return err;
5553 static const struct got_error *
5554 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5555 struct tog_tree_view_state *s)
5557 struct tog_view *log_view;
5558 const struct got_error *err = NULL;
5559 char *path;
5561 *new_view = NULL;
5563 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5564 if (log_view == NULL)
5565 return got_error_from_errno("view_open");
5567 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5568 if (err)
5569 return err;
5571 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5572 path, 0);
5573 if (err)
5574 view_close(log_view);
5575 else
5576 *new_view = log_view;
5577 free(path);
5578 return err;
5581 static const struct got_error *
5582 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5583 const char *head_ref_name, struct got_repository *repo)
5585 const struct got_error *err = NULL;
5586 char *commit_id_str = NULL;
5587 struct tog_tree_view_state *s = &view->state.tree;
5588 struct got_commit_object *commit = NULL;
5590 TAILQ_INIT(&s->parents);
5591 STAILQ_INIT(&s->colors);
5593 s->commit_id = got_object_id_dup(commit_id);
5594 if (s->commit_id == NULL)
5595 return got_error_from_errno("got_object_id_dup");
5597 err = got_object_open_as_commit(&commit, repo, commit_id);
5598 if (err)
5599 goto done;
5602 * The root is opened here and will be closed when the view is closed.
5603 * Any visited subtrees and their path-wise parents are opened and
5604 * closed on demand.
5606 err = got_object_open_as_tree(&s->root, repo,
5607 got_object_commit_get_tree_id(commit));
5608 if (err)
5609 goto done;
5610 s->tree = s->root;
5612 err = got_object_id_str(&commit_id_str, commit_id);
5613 if (err != NULL)
5614 goto done;
5616 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5617 err = got_error_from_errno("asprintf");
5618 goto done;
5621 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5622 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5623 if (head_ref_name) {
5624 s->head_ref_name = strdup(head_ref_name);
5625 if (s->head_ref_name == NULL) {
5626 err = got_error_from_errno("strdup");
5627 goto done;
5630 s->repo = repo;
5632 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5633 err = add_color(&s->colors, "\\$$",
5634 TOG_COLOR_TREE_SUBMODULE,
5635 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5636 if (err)
5637 goto done;
5638 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5639 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5640 if (err)
5641 goto done;
5642 err = add_color(&s->colors, "/$",
5643 TOG_COLOR_TREE_DIRECTORY,
5644 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5645 if (err)
5646 goto done;
5648 err = add_color(&s->colors, "\\*$",
5649 TOG_COLOR_TREE_EXECUTABLE,
5650 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5651 if (err)
5652 goto done;
5654 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5655 get_color_value("TOG_COLOR_COMMIT"));
5656 if (err)
5657 goto done;
5660 view->show = show_tree_view;
5661 view->input = input_tree_view;
5662 view->close = close_tree_view;
5663 view->search_start = search_start_tree_view;
5664 view->search_next = search_next_tree_view;
5665 done:
5666 free(commit_id_str);
5667 if (commit)
5668 got_object_commit_close(commit);
5669 if (err)
5670 close_tree_view(view);
5671 return err;
5674 static const struct got_error *
5675 close_tree_view(struct tog_view *view)
5677 struct tog_tree_view_state *s = &view->state.tree;
5679 free_colors(&s->colors);
5680 free(s->tree_label);
5681 s->tree_label = NULL;
5682 free(s->commit_id);
5683 s->commit_id = NULL;
5684 free(s->head_ref_name);
5685 s->head_ref_name = NULL;
5686 while (!TAILQ_EMPTY(&s->parents)) {
5687 struct tog_parent_tree *parent;
5688 parent = TAILQ_FIRST(&s->parents);
5689 TAILQ_REMOVE(&s->parents, parent, entry);
5690 if (parent->tree != s->root)
5691 got_object_tree_close(parent->tree);
5692 free(parent);
5695 if (s->tree != NULL && s->tree != s->root)
5696 got_object_tree_close(s->tree);
5697 if (s->root)
5698 got_object_tree_close(s->root);
5699 return NULL;
5702 static const struct got_error *
5703 search_start_tree_view(struct tog_view *view)
5705 struct tog_tree_view_state *s = &view->state.tree;
5707 s->matched_entry = NULL;
5708 return NULL;
5711 static int
5712 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5714 regmatch_t regmatch;
5716 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5717 0) == 0;
5720 static const struct got_error *
5721 search_next_tree_view(struct tog_view *view)
5723 struct tog_tree_view_state *s = &view->state.tree;
5724 struct got_tree_entry *te = NULL;
5726 if (!view->searching) {
5727 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5728 return NULL;
5731 if (s->matched_entry) {
5732 if (view->searching == TOG_SEARCH_FORWARD) {
5733 if (s->selected_entry)
5734 te = got_tree_entry_get_next(s->tree,
5735 s->selected_entry);
5736 else
5737 te = got_object_tree_get_first_entry(s->tree);
5738 } else {
5739 if (s->selected_entry == NULL)
5740 te = got_object_tree_get_last_entry(s->tree);
5741 else
5742 te = got_tree_entry_get_prev(s->tree,
5743 s->selected_entry);
5745 } else {
5746 if (s->selected_entry)
5747 te = s->selected_entry;
5748 else if (view->searching == TOG_SEARCH_FORWARD)
5749 te = got_object_tree_get_first_entry(s->tree);
5750 else
5751 te = got_object_tree_get_last_entry(s->tree);
5754 while (1) {
5755 if (te == NULL) {
5756 if (s->matched_entry == NULL) {
5757 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5758 return NULL;
5760 if (view->searching == TOG_SEARCH_FORWARD)
5761 te = got_object_tree_get_first_entry(s->tree);
5762 else
5763 te = got_object_tree_get_last_entry(s->tree);
5766 if (match_tree_entry(te, &view->regex)) {
5767 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5768 s->matched_entry = te;
5769 break;
5772 if (view->searching == TOG_SEARCH_FORWARD)
5773 te = got_tree_entry_get_next(s->tree, te);
5774 else
5775 te = got_tree_entry_get_prev(s->tree, te);
5778 if (s->matched_entry) {
5779 s->first_displayed_entry = s->matched_entry;
5780 s->selected = 0;
5783 return NULL;
5786 static const struct got_error *
5787 show_tree_view(struct tog_view *view)
5789 const struct got_error *err = NULL;
5790 struct tog_tree_view_state *s = &view->state.tree;
5791 char *parent_path;
5793 err = tree_entry_path(&parent_path, &s->parents, NULL);
5794 if (err)
5795 return err;
5797 err = draw_tree_entries(view, parent_path);
5798 free(parent_path);
5800 view_vborder(view);
5801 return err;
5804 static const struct got_error *
5805 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5807 const struct got_error *err = NULL;
5808 struct tog_tree_view_state *s = &view->state.tree;
5809 struct tog_view *log_view, *ref_view;
5810 struct got_tree_entry *te;
5811 int begin_x = 0, n, nscroll = view->nlines - 3;
5813 switch (ch) {
5814 case 'i':
5815 s->show_ids = !s->show_ids;
5816 break;
5817 case 'l':
5818 if (!s->selected_entry)
5819 break;
5820 if (view_is_parent_view(view))
5821 begin_x = view_split_begin_x(view->begin_x);
5822 err = log_selected_tree_entry(&log_view, begin_x, s);
5823 view->focussed = 0;
5824 log_view->focussed = 1;
5825 if (view_is_parent_view(view)) {
5826 err = view_close_child(view);
5827 if (err)
5828 return err;
5829 view_set_child(view, log_view);
5830 view->focus_child = 1;
5831 } else
5832 *new_view = log_view;
5833 break;
5834 case 'r':
5835 if (view_is_parent_view(view))
5836 begin_x = view_split_begin_x(view->begin_x);
5837 ref_view = view_open(view->nlines, view->ncols,
5838 view->begin_y, begin_x, TOG_VIEW_REF);
5839 if (ref_view == NULL)
5840 return got_error_from_errno("view_open");
5841 err = open_ref_view(ref_view, s->repo);
5842 if (err) {
5843 view_close(ref_view);
5844 return err;
5846 view->focussed = 0;
5847 ref_view->focussed = 1;
5848 if (view_is_parent_view(view)) {
5849 err = view_close_child(view);
5850 if (err)
5851 return err;
5852 view_set_child(view, ref_view);
5853 view->focus_child = 1;
5854 } else
5855 *new_view = ref_view;
5856 break;
5857 case 'g':
5858 case KEY_HOME:
5859 s->selected = 0;
5860 if (s->tree == s->root)
5861 s->first_displayed_entry =
5862 got_object_tree_get_first_entry(s->tree);
5863 else
5864 s->first_displayed_entry = NULL;
5865 break;
5866 case 'G':
5867 case KEY_END:
5868 s->selected = 0;
5869 te = got_object_tree_get_last_entry(s->tree);
5870 for (n = 0; n < view->nlines - 3; n++) {
5871 if (te == NULL) {
5872 if(s->tree != s->root) {
5873 s->first_displayed_entry = NULL;
5874 n++;
5876 break;
5878 s->first_displayed_entry = te;
5879 te = got_tree_entry_get_prev(s->tree, te);
5881 if (n > 0)
5882 s->selected = n - 1;
5883 break;
5884 case 'k':
5885 case KEY_UP:
5886 case CTRL('p'):
5887 if (s->selected > 0) {
5888 s->selected--;
5889 break;
5891 tree_scroll_up(s, 1);
5892 break;
5893 case CTRL('u'):
5894 case 'u':
5895 nscroll /= 2;
5896 /* FALL THROUGH */
5897 case KEY_PPAGE:
5898 case CTRL('b'):
5899 if (s->tree == s->root) {
5900 if (got_object_tree_get_first_entry(s->tree) ==
5901 s->first_displayed_entry)
5902 s->selected -= MIN(s->selected, nscroll);
5903 } else {
5904 if (s->first_displayed_entry == NULL)
5905 s->selected -= MIN(s->selected, nscroll);
5907 tree_scroll_up(s, MAX(0, nscroll));
5908 break;
5909 case 'j':
5910 case KEY_DOWN:
5911 case CTRL('n'):
5912 if (s->selected < s->ndisplayed - 1) {
5913 s->selected++;
5914 break;
5916 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5917 == NULL)
5918 /* can't scroll any further */
5919 break;
5920 tree_scroll_down(s, 1);
5921 break;
5922 case CTRL('d'):
5923 case 'd':
5924 nscroll /= 2;
5925 /* FALL THROUGH */
5926 case KEY_NPAGE:
5927 case CTRL('f'):
5928 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5929 == NULL) {
5930 /* can't scroll any further; move cursor down */
5931 if (s->selected < s->ndisplayed - 1)
5932 s->selected += MIN(nscroll,
5933 s->ndisplayed - s->selected - 1);
5934 break;
5936 tree_scroll_down(s, nscroll);
5937 break;
5938 case KEY_ENTER:
5939 case '\r':
5940 case KEY_BACKSPACE:
5941 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5942 struct tog_parent_tree *parent;
5943 /* user selected '..' */
5944 if (s->tree == s->root)
5945 break;
5946 parent = TAILQ_FIRST(&s->parents);
5947 TAILQ_REMOVE(&s->parents, parent,
5948 entry);
5949 got_object_tree_close(s->tree);
5950 s->tree = parent->tree;
5951 s->first_displayed_entry =
5952 parent->first_displayed_entry;
5953 s->selected_entry =
5954 parent->selected_entry;
5955 s->selected = parent->selected;
5956 free(parent);
5957 } else if (S_ISDIR(got_tree_entry_get_mode(
5958 s->selected_entry))) {
5959 struct got_tree_object *subtree;
5960 err = got_object_open_as_tree(&subtree, s->repo,
5961 got_tree_entry_get_id(s->selected_entry));
5962 if (err)
5963 break;
5964 err = tree_view_visit_subtree(s, subtree);
5965 if (err) {
5966 got_object_tree_close(subtree);
5967 break;
5969 } else if (S_ISREG(got_tree_entry_get_mode(
5970 s->selected_entry))) {
5971 struct tog_view *blame_view;
5972 int begin_x = view_is_parent_view(view) ?
5973 view_split_begin_x(view->begin_x) : 0;
5975 err = blame_tree_entry(&blame_view, begin_x,
5976 s->selected_entry, &s->parents,
5977 s->commit_id, s->repo);
5978 if (err)
5979 break;
5980 view->focussed = 0;
5981 blame_view->focussed = 1;
5982 if (view_is_parent_view(view)) {
5983 err = view_close_child(view);
5984 if (err)
5985 return err;
5986 view_set_child(view, blame_view);
5987 view->focus_child = 1;
5988 } else
5989 *new_view = blame_view;
5991 break;
5992 case KEY_RESIZE:
5993 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5994 s->selected = view->nlines - 4;
5995 break;
5996 default:
5997 break;
6000 return err;
6003 __dead static void
6004 usage_tree(void)
6006 endwin();
6007 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6008 getprogname());
6009 exit(1);
6012 static const struct got_error *
6013 cmd_tree(int argc, char *argv[])
6015 const struct got_error *error;
6016 struct got_repository *repo = NULL;
6017 struct got_worktree *worktree = NULL;
6018 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6019 struct got_object_id *commit_id = NULL;
6020 struct got_commit_object *commit = NULL;
6021 const char *commit_id_arg = NULL;
6022 char *label = NULL;
6023 struct got_reference *ref = NULL;
6024 const char *head_ref_name = NULL;
6025 int ch;
6026 struct tog_view *view;
6027 int *pack_fds = NULL;
6029 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6030 switch (ch) {
6031 case 'c':
6032 commit_id_arg = optarg;
6033 break;
6034 case 'r':
6035 repo_path = realpath(optarg, NULL);
6036 if (repo_path == NULL)
6037 return got_error_from_errno2("realpath",
6038 optarg);
6039 break;
6040 default:
6041 usage_tree();
6042 /* NOTREACHED */
6046 argc -= optind;
6047 argv += optind;
6049 if (argc > 1)
6050 usage_tree();
6052 error = got_repo_pack_fds_open(&pack_fds);
6053 if (error != NULL)
6054 goto done;
6056 if (repo_path == NULL) {
6057 cwd = getcwd(NULL, 0);
6058 if (cwd == NULL)
6059 return got_error_from_errno("getcwd");
6060 error = got_worktree_open(&worktree, cwd);
6061 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6062 goto done;
6063 if (worktree)
6064 repo_path =
6065 strdup(got_worktree_get_repo_path(worktree));
6066 else
6067 repo_path = strdup(cwd);
6068 if (repo_path == NULL) {
6069 error = got_error_from_errno("strdup");
6070 goto done;
6074 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6075 if (error != NULL)
6076 goto done;
6078 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6079 repo, worktree);
6080 if (error)
6081 goto done;
6083 init_curses();
6085 error = apply_unveil(got_repo_get_path(repo), NULL);
6086 if (error)
6087 goto done;
6089 error = tog_load_refs(repo, 0);
6090 if (error)
6091 goto done;
6093 if (commit_id_arg == NULL) {
6094 error = got_repo_match_object_id(&commit_id, &label,
6095 worktree ? got_worktree_get_head_ref_name(worktree) :
6096 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6097 if (error)
6098 goto done;
6099 head_ref_name = label;
6100 } else {
6101 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6102 if (error == NULL)
6103 head_ref_name = got_ref_get_name(ref);
6104 else if (error->code != GOT_ERR_NOT_REF)
6105 goto done;
6106 error = got_repo_match_object_id(&commit_id, NULL,
6107 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6108 if (error)
6109 goto done;
6112 error = got_object_open_as_commit(&commit, repo, commit_id);
6113 if (error)
6114 goto done;
6116 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6117 if (view == NULL) {
6118 error = got_error_from_errno("view_open");
6119 goto done;
6121 error = open_tree_view(view, commit_id, head_ref_name, repo);
6122 if (error)
6123 goto done;
6124 if (!got_path_is_root_dir(in_repo_path)) {
6125 error = tree_view_walk_path(&view->state.tree, commit,
6126 in_repo_path);
6127 if (error)
6128 goto done;
6131 if (worktree) {
6132 /* Release work tree lock. */
6133 got_worktree_close(worktree);
6134 worktree = NULL;
6136 error = view_loop(view);
6137 done:
6138 free(repo_path);
6139 free(cwd);
6140 free(commit_id);
6141 free(label);
6142 if (ref)
6143 got_ref_close(ref);
6144 if (repo) {
6145 const struct got_error *close_err = got_repo_close(repo);
6146 if (error == NULL)
6147 error = close_err;
6149 if (pack_fds) {
6150 const struct got_error *pack_err =
6151 got_repo_pack_fds_close(pack_fds);
6152 if (error == NULL)
6153 error = pack_err;
6155 tog_free_refs();
6156 return error;
6159 static const struct got_error *
6160 ref_view_load_refs(struct tog_ref_view_state *s)
6162 struct got_reflist_entry *sre;
6163 struct tog_reflist_entry *re;
6165 s->nrefs = 0;
6166 TAILQ_FOREACH(sre, &tog_refs, entry) {
6167 if (strncmp(got_ref_get_name(sre->ref),
6168 "refs/got/", 9) == 0 &&
6169 strncmp(got_ref_get_name(sre->ref),
6170 "refs/got/backup/", 16) != 0)
6171 continue;
6173 re = malloc(sizeof(*re));
6174 if (re == NULL)
6175 return got_error_from_errno("malloc");
6177 re->ref = got_ref_dup(sre->ref);
6178 if (re->ref == NULL)
6179 return got_error_from_errno("got_ref_dup");
6180 re->idx = s->nrefs++;
6181 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6184 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6185 return NULL;
6188 void
6189 ref_view_free_refs(struct tog_ref_view_state *s)
6191 struct tog_reflist_entry *re;
6193 while (!TAILQ_EMPTY(&s->refs)) {
6194 re = TAILQ_FIRST(&s->refs);
6195 TAILQ_REMOVE(&s->refs, re, entry);
6196 got_ref_close(re->ref);
6197 free(re);
6201 static const struct got_error *
6202 open_ref_view(struct tog_view *view, struct got_repository *repo)
6204 const struct got_error *err = NULL;
6205 struct tog_ref_view_state *s = &view->state.ref;
6207 s->selected_entry = 0;
6208 s->repo = repo;
6210 TAILQ_INIT(&s->refs);
6211 STAILQ_INIT(&s->colors);
6213 err = ref_view_load_refs(s);
6214 if (err)
6215 return err;
6217 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6218 err = add_color(&s->colors, "^refs/heads/",
6219 TOG_COLOR_REFS_HEADS,
6220 get_color_value("TOG_COLOR_REFS_HEADS"));
6221 if (err)
6222 goto done;
6224 err = add_color(&s->colors, "^refs/tags/",
6225 TOG_COLOR_REFS_TAGS,
6226 get_color_value("TOG_COLOR_REFS_TAGS"));
6227 if (err)
6228 goto done;
6230 err = add_color(&s->colors, "^refs/remotes/",
6231 TOG_COLOR_REFS_REMOTES,
6232 get_color_value("TOG_COLOR_REFS_REMOTES"));
6233 if (err)
6234 goto done;
6236 err = add_color(&s->colors, "^refs/got/backup/",
6237 TOG_COLOR_REFS_BACKUP,
6238 get_color_value("TOG_COLOR_REFS_BACKUP"));
6239 if (err)
6240 goto done;
6243 view->show = show_ref_view;
6244 view->input = input_ref_view;
6245 view->close = close_ref_view;
6246 view->search_start = search_start_ref_view;
6247 view->search_next = search_next_ref_view;
6248 done:
6249 if (err)
6250 free_colors(&s->colors);
6251 return err;
6254 static const struct got_error *
6255 close_ref_view(struct tog_view *view)
6257 struct tog_ref_view_state *s = &view->state.ref;
6259 ref_view_free_refs(s);
6260 free_colors(&s->colors);
6262 return NULL;
6265 static const struct got_error *
6266 resolve_reflist_entry(struct got_object_id **commit_id,
6267 struct tog_reflist_entry *re, struct got_repository *repo)
6269 const struct got_error *err = NULL;
6270 struct got_object_id *obj_id;
6271 struct got_tag_object *tag = NULL;
6272 int obj_type;
6274 *commit_id = NULL;
6276 err = got_ref_resolve(&obj_id, repo, re->ref);
6277 if (err)
6278 return err;
6280 err = got_object_get_type(&obj_type, repo, obj_id);
6281 if (err)
6282 goto done;
6284 switch (obj_type) {
6285 case GOT_OBJ_TYPE_COMMIT:
6286 *commit_id = obj_id;
6287 break;
6288 case GOT_OBJ_TYPE_TAG:
6289 err = got_object_open_as_tag(&tag, repo, obj_id);
6290 if (err)
6291 goto done;
6292 free(obj_id);
6293 err = got_object_get_type(&obj_type, repo,
6294 got_object_tag_get_object_id(tag));
6295 if (err)
6296 goto done;
6297 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6298 err = got_error(GOT_ERR_OBJ_TYPE);
6299 goto done;
6301 *commit_id = got_object_id_dup(
6302 got_object_tag_get_object_id(tag));
6303 if (*commit_id == NULL) {
6304 err = got_error_from_errno("got_object_id_dup");
6305 goto done;
6307 break;
6308 default:
6309 err = got_error(GOT_ERR_OBJ_TYPE);
6310 break;
6313 done:
6314 if (tag)
6315 got_object_tag_close(tag);
6316 if (err) {
6317 free(*commit_id);
6318 *commit_id = NULL;
6320 return err;
6323 static const struct got_error *
6324 log_ref_entry(struct tog_view **new_view, int begin_x,
6325 struct tog_reflist_entry *re, struct got_repository *repo)
6327 struct tog_view *log_view;
6328 const struct got_error *err = NULL;
6329 struct got_object_id *commit_id = NULL;
6331 *new_view = NULL;
6333 err = resolve_reflist_entry(&commit_id, re, repo);
6334 if (err) {
6335 if (err->code != GOT_ERR_OBJ_TYPE)
6336 return err;
6337 else
6338 return NULL;
6341 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6342 if (log_view == NULL) {
6343 err = got_error_from_errno("view_open");
6344 goto done;
6347 err = open_log_view(log_view, commit_id, repo,
6348 got_ref_get_name(re->ref), "", 0);
6349 done:
6350 if (err)
6351 view_close(log_view);
6352 else
6353 *new_view = log_view;
6354 free(commit_id);
6355 return err;
6358 static void
6359 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6361 struct tog_reflist_entry *re;
6362 int i = 0;
6364 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6365 return;
6367 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6368 while (i++ < maxscroll) {
6369 if (re == NULL)
6370 break;
6371 s->first_displayed_entry = re;
6372 re = TAILQ_PREV(re, tog_reflist_head, entry);
6376 static void
6377 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6379 struct tog_reflist_entry *next, *last;
6380 int n = 0;
6382 if (s->first_displayed_entry)
6383 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6384 else
6385 next = TAILQ_FIRST(&s->refs);
6387 last = s->last_displayed_entry;
6388 while (next && last && n++ < maxscroll) {
6389 last = TAILQ_NEXT(last, entry);
6390 if (last) {
6391 s->first_displayed_entry = next;
6392 next = TAILQ_NEXT(next, entry);
6397 static const struct got_error *
6398 search_start_ref_view(struct tog_view *view)
6400 struct tog_ref_view_state *s = &view->state.ref;
6402 s->matched_entry = NULL;
6403 return NULL;
6406 static int
6407 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6409 regmatch_t regmatch;
6411 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6412 0) == 0;
6415 static const struct got_error *
6416 search_next_ref_view(struct tog_view *view)
6418 struct tog_ref_view_state *s = &view->state.ref;
6419 struct tog_reflist_entry *re = NULL;
6421 if (!view->searching) {
6422 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6423 return NULL;
6426 if (s->matched_entry) {
6427 if (view->searching == TOG_SEARCH_FORWARD) {
6428 if (s->selected_entry)
6429 re = TAILQ_NEXT(s->selected_entry, entry);
6430 else
6431 re = TAILQ_PREV(s->selected_entry,
6432 tog_reflist_head, entry);
6433 } else {
6434 if (s->selected_entry == NULL)
6435 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6436 else
6437 re = TAILQ_PREV(s->selected_entry,
6438 tog_reflist_head, entry);
6440 } else {
6441 if (s->selected_entry)
6442 re = s->selected_entry;
6443 else if (view->searching == TOG_SEARCH_FORWARD)
6444 re = TAILQ_FIRST(&s->refs);
6445 else
6446 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6449 while (1) {
6450 if (re == NULL) {
6451 if (s->matched_entry == NULL) {
6452 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6453 return NULL;
6455 if (view->searching == TOG_SEARCH_FORWARD)
6456 re = TAILQ_FIRST(&s->refs);
6457 else
6458 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6461 if (match_reflist_entry(re, &view->regex)) {
6462 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6463 s->matched_entry = re;
6464 break;
6467 if (view->searching == TOG_SEARCH_FORWARD)
6468 re = TAILQ_NEXT(re, entry);
6469 else
6470 re = TAILQ_PREV(re, tog_reflist_head, entry);
6473 if (s->matched_entry) {
6474 s->first_displayed_entry = s->matched_entry;
6475 s->selected = 0;
6478 return NULL;
6481 static const struct got_error *
6482 show_ref_view(struct tog_view *view)
6484 const struct got_error *err = NULL;
6485 struct tog_ref_view_state *s = &view->state.ref;
6486 struct tog_reflist_entry *re;
6487 char *line = NULL;
6488 wchar_t *wline;
6489 struct tog_color *tc;
6490 int width, n;
6491 int limit = view->nlines;
6493 werase(view->window);
6495 s->ndisplayed = 0;
6497 if (limit == 0)
6498 return NULL;
6500 re = s->first_displayed_entry;
6502 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6503 s->nrefs) == -1)
6504 return got_error_from_errno("asprintf");
6506 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6507 if (err) {
6508 free(line);
6509 return err;
6511 if (view_needs_focus_indication(view))
6512 wstandout(view->window);
6513 waddwstr(view->window, wline);
6514 if (view_needs_focus_indication(view))
6515 wstandend(view->window);
6516 free(wline);
6517 wline = NULL;
6518 free(line);
6519 line = NULL;
6520 if (width < view->ncols - 1)
6521 waddch(view->window, '\n');
6522 if (--limit <= 0)
6523 return NULL;
6525 n = 0;
6526 while (re && limit > 0) {
6527 char *line = NULL;
6528 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6530 if (s->show_date) {
6531 struct got_commit_object *ci;
6532 struct got_tag_object *tag;
6533 struct got_object_id *id;
6534 struct tm tm;
6535 time_t t;
6537 err = got_ref_resolve(&id, s->repo, re->ref);
6538 if (err)
6539 return err;
6540 err = got_object_open_as_tag(&tag, s->repo, id);
6541 if (err) {
6542 if (err->code != GOT_ERR_OBJ_TYPE) {
6543 free(id);
6544 return err;
6546 err = got_object_open_as_commit(&ci, s->repo,
6547 id);
6548 if (err) {
6549 free(id);
6550 return err;
6552 t = got_object_commit_get_committer_time(ci);
6553 got_object_commit_close(ci);
6554 } else {
6555 t = got_object_tag_get_tagger_time(tag);
6556 got_object_tag_close(tag);
6558 free(id);
6559 if (gmtime_r(&t, &tm) == NULL)
6560 return got_error_from_errno("gmtime_r");
6561 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6562 return got_error(GOT_ERR_NO_SPACE);
6564 if (got_ref_is_symbolic(re->ref)) {
6565 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6566 ymd : "", got_ref_get_name(re->ref),
6567 got_ref_get_symref_target(re->ref)) == -1)
6568 return got_error_from_errno("asprintf");
6569 } else if (s->show_ids) {
6570 struct got_object_id *id;
6571 char *id_str;
6572 err = got_ref_resolve(&id, s->repo, re->ref);
6573 if (err)
6574 return err;
6575 err = got_object_id_str(&id_str, id);
6576 if (err) {
6577 free(id);
6578 return err;
6580 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6581 got_ref_get_name(re->ref), id_str) == -1) {
6582 err = got_error_from_errno("asprintf");
6583 free(id);
6584 free(id_str);
6585 return err;
6587 free(id);
6588 free(id_str);
6589 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6590 got_ref_get_name(re->ref)) == -1)
6591 return got_error_from_errno("asprintf");
6593 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6594 0, 0);
6595 if (err) {
6596 free(line);
6597 return err;
6599 if (n == s->selected) {
6600 if (view->focussed)
6601 wstandout(view->window);
6602 s->selected_entry = re;
6604 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6605 if (tc)
6606 wattr_on(view->window,
6607 COLOR_PAIR(tc->colorpair), NULL);
6608 waddwstr(view->window, wline);
6609 if (tc)
6610 wattr_off(view->window,
6611 COLOR_PAIR(tc->colorpair), NULL);
6612 if (width < view->ncols - 1)
6613 waddch(view->window, '\n');
6614 if (n == s->selected && view->focussed)
6615 wstandend(view->window);
6616 free(line);
6617 free(wline);
6618 wline = NULL;
6619 n++;
6620 s->ndisplayed++;
6621 s->last_displayed_entry = re;
6623 limit--;
6624 re = TAILQ_NEXT(re, entry);
6627 view_vborder(view);
6628 return err;
6631 static const struct got_error *
6632 browse_ref_tree(struct tog_view **new_view, int begin_x,
6633 struct tog_reflist_entry *re, struct got_repository *repo)
6635 const struct got_error *err = NULL;
6636 struct got_object_id *commit_id = NULL;
6637 struct tog_view *tree_view;
6639 *new_view = NULL;
6641 err = resolve_reflist_entry(&commit_id, re, repo);
6642 if (err) {
6643 if (err->code != GOT_ERR_OBJ_TYPE)
6644 return err;
6645 else
6646 return NULL;
6650 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6651 if (tree_view == NULL) {
6652 err = got_error_from_errno("view_open");
6653 goto done;
6656 err = open_tree_view(tree_view, commit_id,
6657 got_ref_get_name(re->ref), repo);
6658 if (err)
6659 goto done;
6661 *new_view = tree_view;
6662 done:
6663 free(commit_id);
6664 return err;
6666 static const struct got_error *
6667 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6669 const struct got_error *err = NULL;
6670 struct tog_ref_view_state *s = &view->state.ref;
6671 struct tog_view *log_view, *tree_view;
6672 struct tog_reflist_entry *re;
6673 int begin_x = 0, n, nscroll = view->nlines - 1;
6675 switch (ch) {
6676 case 'i':
6677 s->show_ids = !s->show_ids;
6678 break;
6679 case 'm':
6680 s->show_date = !s->show_date;
6681 break;
6682 case 'o':
6683 s->sort_by_date = !s->sort_by_date;
6684 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6685 got_ref_cmp_by_commit_timestamp_descending :
6686 tog_ref_cmp_by_name, s->repo);
6687 if (err)
6688 break;
6689 got_reflist_object_id_map_free(tog_refs_idmap);
6690 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6691 &tog_refs, s->repo);
6692 if (err)
6693 break;
6694 ref_view_free_refs(s);
6695 err = ref_view_load_refs(s);
6696 break;
6697 case KEY_ENTER:
6698 case '\r':
6699 if (!s->selected_entry)
6700 break;
6701 if (view_is_parent_view(view))
6702 begin_x = view_split_begin_x(view->begin_x);
6703 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6704 s->repo);
6705 view->focussed = 0;
6706 log_view->focussed = 1;
6707 if (view_is_parent_view(view)) {
6708 err = view_close_child(view);
6709 if (err)
6710 return err;
6711 view_set_child(view, log_view);
6712 view->focus_child = 1;
6713 } else
6714 *new_view = log_view;
6715 break;
6716 case 't':
6717 if (!s->selected_entry)
6718 break;
6719 if (view_is_parent_view(view))
6720 begin_x = view_split_begin_x(view->begin_x);
6721 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6722 s->repo);
6723 if (err || tree_view == NULL)
6724 break;
6725 view->focussed = 0;
6726 tree_view->focussed = 1;
6727 if (view_is_parent_view(view)) {
6728 err = view_close_child(view);
6729 if (err)
6730 return err;
6731 view_set_child(view, tree_view);
6732 view->focus_child = 1;
6733 } else
6734 *new_view = tree_view;
6735 break;
6736 case 'g':
6737 case KEY_HOME:
6738 s->selected = 0;
6739 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6740 break;
6741 case 'G':
6742 case KEY_END:
6743 s->selected = 0;
6744 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6745 for (n = 0; n < view->nlines - 1; n++) {
6746 if (re == NULL)
6747 break;
6748 s->first_displayed_entry = re;
6749 re = TAILQ_PREV(re, tog_reflist_head, entry);
6751 if (n > 0)
6752 s->selected = n - 1;
6753 break;
6754 case 'k':
6755 case KEY_UP:
6756 case CTRL('p'):
6757 if (s->selected > 0) {
6758 s->selected--;
6759 break;
6761 ref_scroll_up(s, 1);
6762 break;
6763 case CTRL('u'):
6764 case 'u':
6765 nscroll /= 2;
6766 /* FALL THROUGH */
6767 case KEY_PPAGE:
6768 case CTRL('b'):
6769 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6770 s->selected -= MIN(nscroll, s->selected);
6771 ref_scroll_up(s, MAX(0, nscroll));
6772 break;
6773 case 'j':
6774 case KEY_DOWN:
6775 case CTRL('n'):
6776 if (s->selected < s->ndisplayed - 1) {
6777 s->selected++;
6778 break;
6780 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6781 /* can't scroll any further */
6782 break;
6783 ref_scroll_down(s, 1);
6784 break;
6785 case CTRL('d'):
6786 case 'd':
6787 nscroll /= 2;
6788 /* FALL THROUGH */
6789 case KEY_NPAGE:
6790 case CTRL('f'):
6791 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6792 /* can't scroll any further; move cursor down */
6793 if (s->selected < s->ndisplayed - 1)
6794 s->selected += MIN(nscroll,
6795 s->ndisplayed - s->selected - 1);
6796 break;
6798 ref_scroll_down(s, nscroll);
6799 break;
6800 case CTRL('l'):
6801 tog_free_refs();
6802 err = tog_load_refs(s->repo, s->sort_by_date);
6803 if (err)
6804 break;
6805 ref_view_free_refs(s);
6806 err = ref_view_load_refs(s);
6807 break;
6808 case KEY_RESIZE:
6809 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6810 s->selected = view->nlines - 2;
6811 break;
6812 default:
6813 break;
6816 return err;
6819 __dead static void
6820 usage_ref(void)
6822 endwin();
6823 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6824 getprogname());
6825 exit(1);
6828 static const struct got_error *
6829 cmd_ref(int argc, char *argv[])
6831 const struct got_error *error;
6832 struct got_repository *repo = NULL;
6833 struct got_worktree *worktree = NULL;
6834 char *cwd = NULL, *repo_path = NULL;
6835 int ch;
6836 struct tog_view *view;
6837 int *pack_fds = NULL;
6839 while ((ch = getopt(argc, argv, "r:")) != -1) {
6840 switch (ch) {
6841 case 'r':
6842 repo_path = realpath(optarg, NULL);
6843 if (repo_path == NULL)
6844 return got_error_from_errno2("realpath",
6845 optarg);
6846 break;
6847 default:
6848 usage_ref();
6849 /* NOTREACHED */
6853 argc -= optind;
6854 argv += optind;
6856 if (argc > 1)
6857 usage_ref();
6859 error = got_repo_pack_fds_open(&pack_fds);
6860 if (error != NULL)
6861 goto done;
6863 if (repo_path == NULL) {
6864 cwd = getcwd(NULL, 0);
6865 if (cwd == NULL)
6866 return got_error_from_errno("getcwd");
6867 error = got_worktree_open(&worktree, cwd);
6868 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6869 goto done;
6870 if (worktree)
6871 repo_path =
6872 strdup(got_worktree_get_repo_path(worktree));
6873 else
6874 repo_path = strdup(cwd);
6875 if (repo_path == NULL) {
6876 error = got_error_from_errno("strdup");
6877 goto done;
6881 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6882 if (error != NULL)
6883 goto done;
6885 init_curses();
6887 error = apply_unveil(got_repo_get_path(repo), NULL);
6888 if (error)
6889 goto done;
6891 error = tog_load_refs(repo, 0);
6892 if (error)
6893 goto done;
6895 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6896 if (view == NULL) {
6897 error = got_error_from_errno("view_open");
6898 goto done;
6901 error = open_ref_view(view, repo);
6902 if (error)
6903 goto done;
6905 if (worktree) {
6906 /* Release work tree lock. */
6907 got_worktree_close(worktree);
6908 worktree = NULL;
6910 error = view_loop(view);
6911 done:
6912 free(repo_path);
6913 free(cwd);
6914 if (repo) {
6915 const struct got_error *close_err = got_repo_close(repo);
6916 if (close_err)
6917 error = close_err;
6919 if (pack_fds) {
6920 const struct got_error *pack_err =
6921 got_repo_pack_fds_close(pack_fds);
6922 if (error == NULL)
6923 error = pack_err;
6925 tog_free_refs();
6926 return error;
6929 static void
6930 list_commands(FILE *fp)
6932 size_t i;
6934 fprintf(fp, "commands:");
6935 for (i = 0; i < nitems(tog_commands); i++) {
6936 const struct tog_cmd *cmd = &tog_commands[i];
6937 fprintf(fp, " %s", cmd->name);
6939 fputc('\n', fp);
6942 __dead static void
6943 usage(int hflag, int status)
6945 FILE *fp = (status == 0) ? stdout : stderr;
6947 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6948 getprogname());
6949 if (hflag) {
6950 fprintf(fp, "lazy usage: %s path\n", getprogname());
6951 list_commands(fp);
6953 exit(status);
6956 static char **
6957 make_argv(int argc, ...)
6959 va_list ap;
6960 char **argv;
6961 int i;
6963 va_start(ap, argc);
6965 argv = calloc(argc, sizeof(char *));
6966 if (argv == NULL)
6967 err(1, "calloc");
6968 for (i = 0; i < argc; i++) {
6969 argv[i] = strdup(va_arg(ap, char *));
6970 if (argv[i] == NULL)
6971 err(1, "strdup");
6974 va_end(ap);
6975 return argv;
6979 * Try to convert 'tog path' into a 'tog log path' command.
6980 * The user could simply have mistyped the command rather than knowingly
6981 * provided a path. So check whether argv[0] can in fact be resolved
6982 * to a path in the HEAD commit and print a special error if not.
6983 * This hack is for mpi@ <3
6985 static const struct got_error *
6986 tog_log_with_path(int argc, char *argv[])
6988 const struct got_error *error = NULL, *close_err;
6989 const struct tog_cmd *cmd = NULL;
6990 struct got_repository *repo = NULL;
6991 struct got_worktree *worktree = NULL;
6992 struct got_object_id *commit_id = NULL, *id = NULL;
6993 struct got_commit_object *commit = NULL;
6994 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6995 char *commit_id_str = NULL, **cmd_argv = NULL;
6996 int *pack_fds = NULL;
6998 cwd = getcwd(NULL, 0);
6999 if (cwd == NULL)
7000 return got_error_from_errno("getcwd");
7002 error = got_repo_pack_fds_open(&pack_fds);
7003 if (error != NULL)
7004 goto done;
7006 error = got_worktree_open(&worktree, cwd);
7007 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7008 goto done;
7010 if (worktree)
7011 repo_path = strdup(got_worktree_get_repo_path(worktree));
7012 else
7013 repo_path = strdup(cwd);
7014 if (repo_path == NULL) {
7015 error = got_error_from_errno("strdup");
7016 goto done;
7019 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7020 if (error != NULL)
7021 goto done;
7023 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7024 repo, worktree);
7025 if (error)
7026 goto done;
7028 error = tog_load_refs(repo, 0);
7029 if (error)
7030 goto done;
7031 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7032 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7033 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7034 if (error)
7035 goto done;
7037 if (worktree) {
7038 got_worktree_close(worktree);
7039 worktree = NULL;
7042 error = got_object_open_as_commit(&commit, repo, commit_id);
7043 if (error)
7044 goto done;
7046 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7047 if (error) {
7048 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7049 goto done;
7050 fprintf(stderr, "%s: '%s' is no known command or path\n",
7051 getprogname(), argv[0]);
7052 usage(1, 1);
7053 /* not reached */
7056 close_err = got_repo_close(repo);
7057 if (error == NULL)
7058 error = close_err;
7059 repo = NULL;
7061 error = got_object_id_str(&commit_id_str, commit_id);
7062 if (error)
7063 goto done;
7065 cmd = &tog_commands[0]; /* log */
7066 argc = 4;
7067 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7068 error = cmd->cmd_main(argc, cmd_argv);
7069 done:
7070 if (repo) {
7071 close_err = got_repo_close(repo);
7072 if (error == NULL)
7073 error = close_err;
7075 if (commit)
7076 got_object_commit_close(commit);
7077 if (worktree)
7078 got_worktree_close(worktree);
7079 if (pack_fds) {
7080 const struct got_error *pack_err =
7081 got_repo_pack_fds_close(pack_fds);
7082 if (error == NULL)
7083 error = pack_err;
7085 free(id);
7086 free(commit_id_str);
7087 free(commit_id);
7088 free(cwd);
7089 free(repo_path);
7090 free(in_repo_path);
7091 if (cmd_argv) {
7092 int i;
7093 for (i = 0; i < argc; i++)
7094 free(cmd_argv[i]);
7095 free(cmd_argv);
7097 tog_free_refs();
7098 return error;
7101 int
7102 main(int argc, char *argv[])
7104 const struct got_error *error = NULL;
7105 const struct tog_cmd *cmd = NULL;
7106 int ch, hflag = 0, Vflag = 0;
7107 char **cmd_argv = NULL;
7108 static const struct option longopts[] = {
7109 { "version", no_argument, NULL, 'V' },
7110 { NULL, 0, NULL, 0}
7113 setlocale(LC_CTYPE, "");
7115 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7116 switch (ch) {
7117 case 'h':
7118 hflag = 1;
7119 break;
7120 case 'V':
7121 Vflag = 1;
7122 break;
7123 default:
7124 usage(hflag, 1);
7125 /* NOTREACHED */
7129 argc -= optind;
7130 argv += optind;
7131 optind = 1;
7132 optreset = 1;
7134 if (Vflag) {
7135 got_version_print_str();
7136 return 0;
7139 #ifndef PROFILE
7140 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7141 NULL) == -1)
7142 err(1, "pledge");
7143 #endif
7145 if (argc == 0) {
7146 if (hflag)
7147 usage(hflag, 0);
7148 /* Build an argument vector which runs a default command. */
7149 cmd = &tog_commands[0];
7150 argc = 1;
7151 cmd_argv = make_argv(argc, cmd->name);
7152 } else {
7153 size_t i;
7155 /* Did the user specify a command? */
7156 for (i = 0; i < nitems(tog_commands); i++) {
7157 if (strncmp(tog_commands[i].name, argv[0],
7158 strlen(argv[0])) == 0) {
7159 cmd = &tog_commands[i];
7160 break;
7165 if (cmd == NULL) {
7166 if (argc != 1)
7167 usage(0, 1);
7168 /* No command specified; try log with a path */
7169 error = tog_log_with_path(argc, argv);
7170 } else {
7171 if (hflag)
7172 cmd->cmd_usage();
7173 else
7174 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7177 endwin();
7178 putchar('\n');
7179 if (cmd_argv) {
7180 int i;
7181 for (i = 0; i < argc; i++)
7182 free(cmd_argv[i]);
7183 free(cmd_argv);
7186 if (error && error->code != GOT_ERR_CANCELLED)
7187 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7188 return 0;