Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/ioctl.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #if defined(__FreeBSD__) || defined(__APPLE__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #endif
25 #include <curses.h>
26 #include <panel.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
51 #include "got_diff.h"
52 #include "got_opentemp.h"
53 #include "got_utf8.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_path.h"
59 #include "got_worktree.h"
61 //#define update_panels() (0)
62 //#define doupdate() (0)
64 #ifndef MIN
65 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #endif
68 #ifndef MAX
69 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 #endif
72 #ifndef CTRL
73 #define CTRL(x) ((x) & 0x1f)
74 #endif
76 #ifndef nitems
77 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
78 #endif
80 struct tog_cmd {
81 const char *name;
82 const struct got_error *(*cmd_main)(int, char *[]);
83 void (*cmd_usage)(void);
84 };
86 __dead static void usage(int, int);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_ref(void);
93 static const struct got_error* cmd_log(int, char *[]);
94 static const struct got_error* cmd_diff(int, char *[]);
95 static const struct got_error* cmd_blame(int, char *[]);
96 static const struct got_error* cmd_tree(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
99 static const struct tog_cmd tog_commands[] = {
100 { "log", cmd_log, usage_log },
101 { "diff", cmd_diff, usage_diff },
102 { "blame", cmd_blame, usage_blame },
103 { "tree", cmd_tree, usage_tree },
104 { "ref", cmd_ref, usage_ref },
105 };
107 enum tog_view_type {
108 TOG_VIEW_DIFF,
109 TOG_VIEW_LOG,
110 TOG_VIEW_BLAME,
111 TOG_VIEW_TREE,
112 TOG_VIEW_REF,
113 };
115 #define TOG_EOF_STRING "(END)"
117 struct commit_queue_entry {
118 TAILQ_ENTRY(commit_queue_entry) entry;
119 struct got_object_id *id;
120 struct got_commit_object *commit;
121 int idx;
122 };
123 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 struct commit_queue {
125 int ncommits;
126 struct commit_queue_head head;
127 };
129 struct tog_color {
130 STAILQ_ENTRY(tog_color) entry;
131 regex_t regex;
132 short colorpair;
133 };
134 STAILQ_HEAD(tog_colors, tog_color);
136 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static const struct got_error *
140 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
141 struct got_reference* re2)
143 const char *name1 = got_ref_get_name(re1);
144 const char *name2 = got_ref_get_name(re2);
145 int isbackup1, isbackup2;
147 /* Sort backup refs towards the bottom of the list. */
148 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
149 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
150 if (!isbackup1 && isbackup2) {
151 *cmp = -1;
152 return NULL;
153 } else if (isbackup1 && !isbackup2) {
154 *cmp = 1;
155 return NULL;
158 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
159 return NULL;
162 static const struct got_error *
163 tog_load_refs(struct got_repository *repo, int sort_by_date)
165 const struct got_error *err;
167 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
168 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
169 repo);
170 if (err)
171 return err;
173 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
174 repo);
177 static void
178 tog_free_refs(void)
180 if (tog_refs_idmap) {
181 got_reflist_object_id_map_free(tog_refs_idmap);
182 tog_refs_idmap = NULL;
184 got_ref_list_free(&tog_refs);
187 static const struct got_error *
188 add_color(struct tog_colors *colors, const char *pattern,
189 int idx, short color)
191 const struct got_error *err = NULL;
192 struct tog_color *tc;
193 int regerr = 0;
195 if (idx < 1 || idx > COLOR_PAIRS - 1)
196 return NULL;
198 init_pair(idx, color, -1);
200 tc = calloc(1, sizeof(*tc));
201 if (tc == NULL)
202 return got_error_from_errno("calloc");
203 regerr = regcomp(&tc->regex, pattern,
204 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
205 if (regerr) {
206 static char regerr_msg[512];
207 static char err_msg[512];
208 regerror(regerr, &tc->regex, regerr_msg,
209 sizeof(regerr_msg));
210 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
211 regerr_msg);
212 err = got_error_msg(GOT_ERR_REGEX, err_msg);
213 free(tc);
214 return err;
216 tc->colorpair = idx;
217 STAILQ_INSERT_HEAD(colors, tc, entry);
218 return NULL;
221 static void
222 free_colors(struct tog_colors *colors)
224 struct tog_color *tc;
226 while (!STAILQ_EMPTY(colors)) {
227 tc = STAILQ_FIRST(colors);
228 STAILQ_REMOVE_HEAD(colors, entry);
229 regfree(&tc->regex);
230 free(tc);
234 struct tog_color *
235 get_color(struct tog_colors *colors, int colorpair)
237 struct tog_color *tc = NULL;
239 STAILQ_FOREACH(tc, colors, entry) {
240 if (tc->colorpair == colorpair)
241 return tc;
244 return NULL;
247 static int
248 default_color_value(const char *envvar)
250 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
251 return COLOR_MAGENTA;
252 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
253 return COLOR_CYAN;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
255 return COLOR_YELLOW;
256 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
257 return COLOR_GREEN;
258 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
259 return COLOR_MAGENTA;
260 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
263 return COLOR_CYAN;
264 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
265 return COLOR_GREEN;
266 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
267 return COLOR_GREEN;
268 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
269 return COLOR_CYAN;
270 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
271 return COLOR_YELLOW;
272 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
273 return COLOR_GREEN;
274 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
275 return COLOR_MAGENTA;
276 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
277 return COLOR_YELLOW;
278 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
279 return COLOR_CYAN;
281 return -1;
284 static int
285 get_color_value(const char *envvar)
287 const char *val = getenv(envvar);
289 if (val == NULL)
290 return default_color_value(envvar);
292 if (strcasecmp(val, "black") == 0)
293 return COLOR_BLACK;
294 if (strcasecmp(val, "red") == 0)
295 return COLOR_RED;
296 if (strcasecmp(val, "green") == 0)
297 return COLOR_GREEN;
298 if (strcasecmp(val, "yellow") == 0)
299 return COLOR_YELLOW;
300 if (strcasecmp(val, "blue") == 0)
301 return COLOR_BLUE;
302 if (strcasecmp(val, "magenta") == 0)
303 return COLOR_MAGENTA;
304 if (strcasecmp(val, "cyan") == 0)
305 return COLOR_CYAN;
306 if (strcasecmp(val, "white") == 0)
307 return COLOR_WHITE;
308 if (strcasecmp(val, "default") == 0)
309 return -1;
311 return default_color_value(envvar);
315 struct tog_diff_view_state {
316 struct got_object_id *id1, *id2;
317 const char *label1, *label2;
318 FILE *f, *f1, *f2;
319 int first_displayed_line;
320 int last_displayed_line;
321 int eof;
322 int diff_context;
323 int ignore_whitespace;
324 int force_text_diff;
325 struct got_repository *repo;
326 struct tog_colors colors;
327 size_t nlines;
328 off_t *line_offsets;
329 int matched_line;
330 int selected_line;
332 /* passed from log view; may be NULL */
333 struct tog_view *log_view;
334 };
336 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
338 struct tog_log_thread_args {
339 pthread_cond_t need_commits;
340 pthread_cond_t commit_loaded;
341 int commits_needed;
342 int load_all;
343 struct got_commit_graph *graph;
344 struct commit_queue *commits;
345 const char *in_repo_path;
346 struct got_object_id *start_id;
347 struct got_repository *repo;
348 int *pack_fds;
349 int log_complete;
350 sig_atomic_t *quit;
351 struct commit_queue_entry **first_displayed_entry;
352 struct commit_queue_entry **selected_entry;
353 int *searching;
354 int *search_next_done;
355 regex_t *regex;
356 };
358 struct tog_log_view_state {
359 struct commit_queue commits;
360 struct commit_queue_entry *first_displayed_entry;
361 struct commit_queue_entry *last_displayed_entry;
362 struct commit_queue_entry *selected_entry;
363 int selected;
364 char *in_repo_path;
365 char *head_ref_name;
366 int log_branches;
367 struct got_repository *repo;
368 struct got_object_id *start_id;
369 sig_atomic_t quit;
370 pthread_t thread;
371 struct tog_log_thread_args thread_args;
372 struct commit_queue_entry *matched_entry;
373 struct commit_queue_entry *search_entry;
374 struct tog_colors colors;
375 };
377 #define TOG_COLOR_DIFF_MINUS 1
378 #define TOG_COLOR_DIFF_PLUS 2
379 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
380 #define TOG_COLOR_DIFF_META 4
381 #define TOG_COLOR_TREE_SUBMODULE 5
382 #define TOG_COLOR_TREE_SYMLINK 6
383 #define TOG_COLOR_TREE_DIRECTORY 7
384 #define TOG_COLOR_TREE_EXECUTABLE 8
385 #define TOG_COLOR_COMMIT 9
386 #define TOG_COLOR_AUTHOR 10
387 #define TOG_COLOR_DATE 11
388 #define TOG_COLOR_REFS_HEADS 12
389 #define TOG_COLOR_REFS_TAGS 13
390 #define TOG_COLOR_REFS_REMOTES 14
391 #define TOG_COLOR_REFS_BACKUP 15
393 struct tog_blame_cb_args {
394 struct tog_blame_line *lines; /* one per line */
395 int nlines;
397 struct tog_view *view;
398 struct got_object_id *commit_id;
399 int *quit;
400 };
402 struct tog_blame_thread_args {
403 const char *path;
404 struct got_repository *repo;
405 struct tog_blame_cb_args *cb_args;
406 int *complete;
407 got_cancel_cb cancel_cb;
408 void *cancel_arg;
409 };
411 struct tog_blame {
412 FILE *f;
413 off_t filesize;
414 struct tog_blame_line *lines;
415 int nlines;
416 off_t *line_offsets;
417 pthread_t thread;
418 struct tog_blame_thread_args thread_args;
419 struct tog_blame_cb_args cb_args;
420 const char *path;
421 int *pack_fds;
422 };
424 struct tog_blame_view_state {
425 int first_displayed_line;
426 int last_displayed_line;
427 int selected_line;
428 int blame_complete;
429 int eof;
430 int done;
431 struct got_object_id_queue blamed_commits;
432 struct got_object_qid *blamed_commit;
433 char *path;
434 struct got_repository *repo;
435 struct got_object_id *commit_id;
436 struct tog_blame blame;
437 int matched_line;
438 struct tog_colors colors;
439 };
441 struct tog_parent_tree {
442 TAILQ_ENTRY(tog_parent_tree) entry;
443 struct got_tree_object *tree;
444 struct got_tree_entry *first_displayed_entry;
445 struct got_tree_entry *selected_entry;
446 int selected;
447 };
449 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
451 struct tog_tree_view_state {
452 char *tree_label;
453 struct got_object_id *commit_id;/* commit which this tree belongs to */
454 struct got_tree_object *root; /* the commit's root tree entry */
455 struct got_tree_object *tree; /* currently displayed (sub-)tree */
456 struct got_tree_entry *first_displayed_entry;
457 struct got_tree_entry *last_displayed_entry;
458 struct got_tree_entry *selected_entry;
459 int ndisplayed, selected, show_ids;
460 struct tog_parent_trees parents; /* parent trees of current sub-tree */
461 char *head_ref_name;
462 struct got_repository *repo;
463 struct got_tree_entry *matched_entry;
464 struct tog_colors colors;
465 };
467 struct tog_reflist_entry {
468 TAILQ_ENTRY(tog_reflist_entry) entry;
469 struct got_reference *ref;
470 int idx;
471 };
473 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
475 struct tog_ref_view_state {
476 struct tog_reflist_head refs;
477 struct tog_reflist_entry *first_displayed_entry;
478 struct tog_reflist_entry *last_displayed_entry;
479 struct tog_reflist_entry *selected_entry;
480 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
481 struct got_repository *repo;
482 struct tog_reflist_entry *matched_entry;
483 struct tog_colors colors;
484 };
486 /*
487 * We implement two types of views: parent views and child views.
489 * The 'Tab' key switches focus between a parent view and its child view.
490 * Child views are shown side-by-side to their parent view, provided
491 * there is enough screen estate.
493 * When a new view is opened from within a parent view, this new view
494 * becomes a child view of the parent view, replacing any existing child.
496 * When a new view is opened from within a child view, this new view
497 * becomes a parent view which will obscure the views below until the
498 * user quits the new parent view by typing 'q'.
500 * This list of views contains parent views only.
501 * Child views are only pointed to by their parent view.
502 */
503 TAILQ_HEAD(tog_view_list_head, tog_view);
505 struct tog_view {
506 TAILQ_ENTRY(tog_view) entry;
507 WINDOW *window;
508 PANEL *panel;
509 int nlines, ncols, begin_y, begin_x;
510 int maxx, x; /* max column and current start column */
511 int lines, cols; /* copies of LINES and COLS */
512 int focussed; /* Only set on one parent or child view at a time. */
513 int dying;
514 struct tog_view *parent;
515 struct tog_view *child;
517 /*
518 * This flag is initially set on parent views when a new child view
519 * is created. It gets toggled when the 'Tab' key switches focus
520 * between parent and child.
521 * The flag indicates whether focus should be passed on to our child
522 * view if this parent view gets picked for focus after another parent
523 * view was closed. This prevents child views from losing focus in such
524 * situations.
525 */
526 int focus_child;
528 /* type-specific state */
529 enum tog_view_type type;
530 union {
531 struct tog_diff_view_state diff;
532 struct tog_log_view_state log;
533 struct tog_blame_view_state blame;
534 struct tog_tree_view_state tree;
535 struct tog_ref_view_state ref;
536 } state;
538 const struct got_error *(*show)(struct tog_view *);
539 const struct got_error *(*input)(struct tog_view **,
540 struct tog_view *, int);
541 const struct got_error *(*close)(struct tog_view *);
543 const struct got_error *(*search_start)(struct tog_view *);
544 const struct got_error *(*search_next)(struct tog_view *);
545 int search_started;
546 int searching;
547 #define TOG_SEARCH_FORWARD 1
548 #define TOG_SEARCH_BACKWARD 2
549 int search_next_done;
550 #define TOG_SEARCH_HAVE_MORE 1
551 #define TOG_SEARCH_NO_MORE 2
552 #define TOG_SEARCH_HAVE_NONE 3
553 regex_t regex;
554 regmatch_t regmatch;
555 };
557 static const struct got_error *open_diff_view(struct tog_view *,
558 struct got_object_id *, struct got_object_id *,
559 const char *, const char *, int, int, int, struct tog_view *,
560 struct got_repository *);
561 static const struct got_error *show_diff_view(struct tog_view *);
562 static const struct got_error *input_diff_view(struct tog_view **,
563 struct tog_view *, int);
564 static const struct got_error* close_diff_view(struct tog_view *);
565 static const struct got_error *search_start_diff_view(struct tog_view *);
566 static const struct got_error *search_next_diff_view(struct tog_view *);
568 static const struct got_error *open_log_view(struct tog_view *,
569 struct got_object_id *, struct got_repository *,
570 const char *, const char *, int);
571 static const struct got_error * show_log_view(struct tog_view *);
572 static const struct got_error *input_log_view(struct tog_view **,
573 struct tog_view *, int);
574 static const struct got_error *close_log_view(struct tog_view *);
575 static const struct got_error *search_start_log_view(struct tog_view *);
576 static const struct got_error *search_next_log_view(struct tog_view *);
578 static const struct got_error *open_blame_view(struct tog_view *, char *,
579 struct got_object_id *, struct got_repository *);
580 static const struct got_error *show_blame_view(struct tog_view *);
581 static const struct got_error *input_blame_view(struct tog_view **,
582 struct tog_view *, int);
583 static const struct got_error *close_blame_view(struct tog_view *);
584 static const struct got_error *search_start_blame_view(struct tog_view *);
585 static const struct got_error *search_next_blame_view(struct tog_view *);
587 static const struct got_error *open_tree_view(struct tog_view *,
588 struct got_object_id *, const char *, struct got_repository *);
589 static const struct got_error *show_tree_view(struct tog_view *);
590 static const struct got_error *input_tree_view(struct tog_view **,
591 struct tog_view *, int);
592 static const struct got_error *close_tree_view(struct tog_view *);
593 static const struct got_error *search_start_tree_view(struct tog_view *);
594 static const struct got_error *search_next_tree_view(struct tog_view *);
596 static const struct got_error *open_ref_view(struct tog_view *,
597 struct got_repository *);
598 static const struct got_error *show_ref_view(struct tog_view *);
599 static const struct got_error *input_ref_view(struct tog_view **,
600 struct tog_view *, int);
601 static const struct got_error *close_ref_view(struct tog_view *);
602 static const struct got_error *search_start_ref_view(struct tog_view *);
603 static const struct got_error *search_next_ref_view(struct tog_view *);
605 static volatile sig_atomic_t tog_sigwinch_received;
606 static volatile sig_atomic_t tog_sigpipe_received;
607 static volatile sig_atomic_t tog_sigcont_received;
608 static volatile sig_atomic_t tog_sigint_received;
609 static volatile sig_atomic_t tog_sigterm_received;
611 static void
612 tog_sigwinch(int signo)
614 tog_sigwinch_received = 1;
617 static void
618 tog_sigpipe(int signo)
620 tog_sigpipe_received = 1;
623 static void
624 tog_sigcont(int signo)
626 tog_sigcont_received = 1;
629 static void
630 tog_sigint(int signo)
632 tog_sigint_received = 1;
635 static void
636 tog_sigterm(int signo)
638 tog_sigterm_received = 1;
641 static int
642 tog_fatal_signal_received()
644 return (tog_sigpipe_received ||
645 tog_sigint_received || tog_sigint_received);
649 static const struct got_error *
650 view_close(struct tog_view *view)
652 const struct got_error *err = NULL;
654 if (view->child) {
655 view_close(view->child);
656 view->child = NULL;
658 if (view->close)
659 err = view->close(view);
660 if (view->panel)
661 del_panel(view->panel);
662 if (view->window)
663 delwin(view->window);
664 free(view);
665 return err;
668 static struct tog_view *
669 view_open(int nlines, int ncols, int begin_y, int begin_x,
670 enum tog_view_type type)
672 struct tog_view *view = calloc(1, sizeof(*view));
674 if (view == NULL)
675 return NULL;
677 view->type = type;
678 view->lines = LINES;
679 view->cols = COLS;
680 view->nlines = nlines ? nlines : LINES - begin_y;
681 view->ncols = ncols ? ncols : COLS - begin_x;
682 view->begin_y = begin_y;
683 view->begin_x = begin_x;
684 view->window = newwin(nlines, ncols, begin_y, begin_x);
685 if (view->window == NULL) {
686 view_close(view);
687 return NULL;
689 view->panel = new_panel(view->window);
690 if (view->panel == NULL ||
691 set_panel_userptr(view->panel, view) != OK) {
692 view_close(view);
693 return NULL;
696 keypad(view->window, TRUE);
697 return view;
700 static int
701 view_split_begin_x(int begin_x)
703 if (begin_x > 0 || COLS < 120)
704 return 0;
705 return (COLS - MAX(COLS / 2, 80));
708 static const struct got_error *view_resize(struct tog_view *);
710 static const struct got_error *
711 view_splitscreen(struct tog_view *view)
713 const struct got_error *err = NULL;
715 view->begin_y = 0;
716 view->begin_x = view_split_begin_x(0);
717 view->nlines = LINES;
718 view->ncols = COLS - view->begin_x;
719 view->lines = LINES;
720 view->cols = COLS;
721 err = view_resize(view);
722 if (err)
723 return err;
725 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
726 return got_error_from_errno("mvwin");
728 return NULL;
731 static const struct got_error *
732 view_fullscreen(struct tog_view *view)
734 const struct got_error *err = NULL;
736 view->begin_x = 0;
737 view->begin_y = 0;
738 view->nlines = LINES;
739 view->ncols = COLS;
740 view->lines = LINES;
741 view->cols = COLS;
742 err = view_resize(view);
743 if (err)
744 return err;
746 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
747 return got_error_from_errno("mvwin");
749 return NULL;
752 static int
753 view_is_parent_view(struct tog_view *view)
755 return view->parent == NULL;
758 static const struct got_error *
759 view_resize(struct tog_view *view)
761 int nlines, ncols;
763 if (view->lines > LINES)
764 nlines = view->nlines - (view->lines - LINES);
765 else
766 nlines = view->nlines + (LINES - view->lines);
768 if (view->cols > COLS)
769 ncols = view->ncols - (view->cols - COLS);
770 else
771 ncols = view->ncols + (COLS - view->cols);
773 if (wresize(view->window, nlines, ncols) == ERR)
774 return got_error_from_errno("wresize");
775 if (replace_panel(view->panel, view->window) == ERR)
776 return got_error_from_errno("replace_panel");
777 wclear(view->window);
779 view->nlines = nlines;
780 view->ncols = ncols;
781 view->lines = LINES;
782 view->cols = COLS;
784 if (view->child) {
785 view->child->begin_x = view_split_begin_x(view->begin_x);
786 if (view->child->begin_x == 0) {
787 view_fullscreen(view->child);
788 if (view->child->focussed)
789 show_panel(view->child->panel);
790 else
791 show_panel(view->panel);
792 } else {
793 view_splitscreen(view->child);
794 show_panel(view->child->panel);
798 return NULL;
801 static const struct got_error *
802 view_close_child(struct tog_view *view)
804 const struct got_error *err = NULL;
806 if (view->child == NULL)
807 return NULL;
809 err = view_close(view->child);
810 view->child = NULL;
811 return err;
814 static void
815 view_set_child(struct tog_view *view, struct tog_view *child)
817 view->child = child;
818 child->parent = view;
821 static int
822 view_is_splitscreen(struct tog_view *view)
824 return view->begin_x > 0;
827 static void
828 tog_resizeterm(void)
830 int cols, lines;
831 struct winsize size;
833 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
834 cols = 80; /* Default */
835 lines = 24;
836 } else {
837 cols = size.ws_col;
838 lines = size.ws_row;
840 resize_term(lines, cols);
843 static const struct got_error *
844 view_search_start(struct tog_view *view)
846 const struct got_error *err = NULL;
847 char pattern[1024];
848 int ret;
850 if (view->search_started) {
851 regfree(&view->regex);
852 view->searching = 0;
853 memset(&view->regmatch, 0, sizeof(view->regmatch));
855 view->search_started = 0;
857 if (view->nlines < 1)
858 return NULL;
860 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
861 wclrtoeol(view->window);
863 nocbreak();
864 echo();
865 ret = wgetnstr(view->window, pattern, sizeof(pattern));
866 cbreak();
867 noecho();
868 if (ret == ERR)
869 return NULL;
871 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
872 err = view->search_start(view);
873 if (err) {
874 regfree(&view->regex);
875 return err;
877 view->search_started = 1;
878 view->searching = TOG_SEARCH_FORWARD;
879 view->search_next_done = 0;
880 view->search_next(view);
883 return NULL;
886 static const struct got_error *
887 view_input(struct tog_view **new, int *done, struct tog_view *view,
888 struct tog_view_list_head *views)
890 const struct got_error *err = NULL;
891 struct tog_view *v;
892 int ch, errcode;
894 *new = NULL;
896 /* Clear "no matches" indicator. */
897 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
898 view->search_next_done == TOG_SEARCH_HAVE_NONE)
899 view->search_next_done = TOG_SEARCH_HAVE_MORE;
901 if (view->searching && !view->search_next_done) {
902 errcode = pthread_mutex_unlock(&tog_mutex);
903 if (errcode)
904 return got_error_set_errno(errcode,
905 "pthread_mutex_unlock");
906 sched_yield();
907 errcode = pthread_mutex_lock(&tog_mutex);
908 if (errcode)
909 return got_error_set_errno(errcode,
910 "pthread_mutex_lock");
911 view->search_next(view);
912 return NULL;
915 nodelay(stdscr, FALSE);
916 /* Allow threads to make progress while we are waiting for input. */
917 errcode = pthread_mutex_unlock(&tog_mutex);
918 if (errcode)
919 return got_error_set_errno(errcode, "pthread_mutex_unlock");
920 ch = wgetch(view->window);
921 errcode = pthread_mutex_lock(&tog_mutex);
922 if (errcode)
923 return got_error_set_errno(errcode, "pthread_mutex_lock");
924 nodelay(stdscr, TRUE);
926 if (tog_sigwinch_received || tog_sigcont_received) {
927 tog_resizeterm();
928 tog_sigwinch_received = 0;
929 tog_sigcont_received = 0;
930 TAILQ_FOREACH(v, views, entry) {
931 err = view_resize(v);
932 if (err)
933 return err;
934 err = v->input(new, v, KEY_RESIZE);
935 if (err)
936 return err;
937 if (v->child) {
938 err = view_resize(v->child);
939 if (err)
940 return err;
941 err = v->child->input(new, v->child,
942 KEY_RESIZE);
943 if (err)
944 return err;
949 switch (ch) {
950 case '\t':
951 if (view->child) {
952 view->focussed = 0;
953 view->child->focussed = 1;
954 view->focus_child = 1;
955 } else if (view->parent) {
956 view->focussed = 0;
957 view->parent->focussed = 1;
958 view->parent->focus_child = 0;
960 break;
961 case 'q':
962 err = view->input(new, view, ch);
963 view->dying = 1;
964 break;
965 case 'Q':
966 *done = 1;
967 break;
968 case 'f':
969 if (view_is_parent_view(view)) {
970 if (view->child == NULL)
971 break;
972 if (view_is_splitscreen(view->child)) {
973 view->focussed = 0;
974 view->child->focussed = 1;
975 err = view_fullscreen(view->child);
976 } else
977 err = view_splitscreen(view->child);
978 if (err)
979 break;
980 err = view->child->input(new, view->child,
981 KEY_RESIZE);
982 } else {
983 if (view_is_splitscreen(view)) {
984 view->parent->focussed = 0;
985 view->focussed = 1;
986 err = view_fullscreen(view);
987 } else {
988 err = view_splitscreen(view);
990 if (err)
991 break;
992 err = view->input(new, view, KEY_RESIZE);
994 break;
995 case KEY_RESIZE:
996 break;
997 case '/':
998 if (view->search_start)
999 view_search_start(view);
1000 else
1001 err = view->input(new, view, ch);
1002 break;
1003 case 'N':
1004 case 'n':
1005 if (view->search_started && view->search_next) {
1006 view->searching = (ch == 'n' ?
1007 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1008 view->search_next_done = 0;
1009 view->search_next(view);
1010 } else
1011 err = view->input(new, view, ch);
1012 break;
1013 default:
1014 err = view->input(new, view, ch);
1015 break;
1018 return err;
1021 void
1022 view_vborder(struct tog_view *view)
1024 PANEL *panel;
1025 const struct tog_view *view_above;
1027 if (view->parent)
1028 return view_vborder(view->parent);
1030 panel = panel_above(view->panel);
1031 if (panel == NULL)
1032 return;
1034 view_above = panel_userptr(panel);
1035 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1036 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1039 int
1040 view_needs_focus_indication(struct tog_view *view)
1042 if (view_is_parent_view(view)) {
1043 if (view->child == NULL || view->child->focussed)
1044 return 0;
1045 if (!view_is_splitscreen(view->child))
1046 return 0;
1047 } else if (!view_is_splitscreen(view))
1048 return 0;
1050 return view->focussed;
1053 static const struct got_error *
1054 view_loop(struct tog_view *view)
1056 const struct got_error *err = NULL;
1057 struct tog_view_list_head views;
1058 struct tog_view *new_view;
1059 int fast_refresh = 10;
1060 int done = 0, errcode;
1062 errcode = pthread_mutex_lock(&tog_mutex);
1063 if (errcode)
1064 return got_error_set_errno(errcode, "pthread_mutex_lock");
1066 TAILQ_INIT(&views);
1067 TAILQ_INSERT_HEAD(&views, view, entry);
1069 view->focussed = 1;
1070 err = view->show(view);
1071 if (err)
1072 return err;
1073 update_panels();
1074 doupdate();
1075 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1076 /* Refresh fast during initialization, then become slower. */
1077 if (fast_refresh && fast_refresh-- == 0)
1078 halfdelay(10); /* switch to once per second */
1080 err = view_input(&new_view, &done, view, &views);
1081 if (err)
1082 break;
1083 if (view->dying) {
1084 struct tog_view *v, *prev = NULL;
1086 if (view_is_parent_view(view))
1087 prev = TAILQ_PREV(view, tog_view_list_head,
1088 entry);
1089 else if (view->parent)
1090 prev = view->parent;
1092 if (view->parent) {
1093 view->parent->child = NULL;
1094 view->parent->focus_child = 0;
1095 } else
1096 TAILQ_REMOVE(&views, view, entry);
1098 err = view_close(view);
1099 if (err)
1100 goto done;
1102 view = NULL;
1103 TAILQ_FOREACH(v, &views, entry) {
1104 if (v->focussed)
1105 break;
1107 if (view == NULL && new_view == NULL) {
1108 /* No view has focus. Try to pick one. */
1109 if (prev)
1110 view = prev;
1111 else if (!TAILQ_EMPTY(&views)) {
1112 view = TAILQ_LAST(&views,
1113 tog_view_list_head);
1115 if (view) {
1116 if (view->focus_child) {
1117 view->child->focussed = 1;
1118 view = view->child;
1119 } else
1120 view->focussed = 1;
1124 if (new_view) {
1125 struct tog_view *v, *t;
1126 /* Only allow one parent view per type. */
1127 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1128 if (v->type != new_view->type)
1129 continue;
1130 TAILQ_REMOVE(&views, v, entry);
1131 err = view_close(v);
1132 if (err)
1133 goto done;
1134 break;
1136 TAILQ_INSERT_TAIL(&views, new_view, entry);
1137 view = new_view;
1139 if (view) {
1140 if (view_is_parent_view(view)) {
1141 if (view->child && view->child->focussed)
1142 view = view->child;
1143 } else {
1144 if (view->parent && view->parent->focussed)
1145 view = view->parent;
1147 show_panel(view->panel);
1148 if (view->child && view_is_splitscreen(view->child))
1149 show_panel(view->child->panel);
1150 if (view->parent && view_is_splitscreen(view)) {
1151 err = view->parent->show(view->parent);
1152 if (err)
1153 goto done;
1155 err = view->show(view);
1156 if (err)
1157 goto done;
1158 if (view->child) {
1159 err = view->child->show(view->child);
1160 if (err)
1161 goto done;
1163 update_panels();
1164 doupdate();
1167 done:
1168 while (!TAILQ_EMPTY(&views)) {
1169 view = TAILQ_FIRST(&views);
1170 TAILQ_REMOVE(&views, view, entry);
1171 view_close(view);
1174 errcode = pthread_mutex_unlock(&tog_mutex);
1175 if (errcode && err == NULL)
1176 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1178 return err;
1181 __dead static void
1182 usage_log(void)
1184 endwin();
1185 fprintf(stderr,
1186 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1187 getprogname());
1188 exit(1);
1191 /* Create newly allocated wide-character string equivalent to a byte string. */
1192 static const struct got_error *
1193 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1195 char *vis = NULL;
1196 const struct got_error *err = NULL;
1198 *ws = NULL;
1199 *wlen = mbstowcs(NULL, s, 0);
1200 if (*wlen == (size_t)-1) {
1201 int vislen;
1202 if (errno != EILSEQ)
1203 return got_error_from_errno("mbstowcs");
1205 /* byte string invalid in current encoding; try to "fix" it */
1206 err = got_mbsavis(&vis, &vislen, s);
1207 if (err)
1208 return err;
1209 *wlen = mbstowcs(NULL, vis, 0);
1210 if (*wlen == (size_t)-1) {
1211 err = got_error_from_errno("mbstowcs"); /* give up */
1212 goto done;
1216 *ws = calloc(*wlen + 1, sizeof(**ws));
1217 if (*ws == NULL) {
1218 err = got_error_from_errno("calloc");
1219 goto done;
1222 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1223 err = got_error_from_errno("mbstowcs");
1224 done:
1225 free(vis);
1226 if (err) {
1227 free(*ws);
1228 *ws = NULL;
1229 *wlen = 0;
1231 return err;
1234 static const struct got_error *
1235 expand_tab(char **ptr, const char *src)
1237 char *dst;
1238 size_t len, n, idx = 0, sz = 0;
1240 *ptr = NULL;
1241 n = len = strlen(src);
1242 dst = malloc((n + 1) * sizeof(char));
1243 if (dst == NULL)
1244 return got_error_from_errno("malloc");
1246 while (idx < len && src[idx]) {
1247 const char c = src[idx];
1249 if (c == '\t') {
1250 size_t nb = TABSIZE - sz % TABSIZE;
1251 n += nb;
1252 dst = reallocarray(dst, n, sizeof(char));
1253 if (dst == NULL)
1254 return got_error_from_errno("reallocarray");
1255 memcpy(dst + sz, " ", nb);
1256 sz += nb;
1257 } else
1258 dst[sz++] = src[idx];
1259 ++idx;
1262 dst[sz] = '\0';
1263 *ptr = dst;
1264 return NULL;
1268 * Skip leading nscroll columns of a wide character string.
1269 * Returns the index to the first character of the scrolled string.
1271 static const struct got_error *
1272 scroll_wline(int *scrollx , wchar_t *wline, int nscroll,
1273 int col_tab_align)
1275 int cols = 0;
1276 size_t wlen = wcslen(wline);
1277 int i = 0;
1279 *scrollx = 0;
1281 while (i < wlen && cols < nscroll) {
1282 int width = wcwidth(wline[i]);
1284 if (width == 0) {
1285 i++;
1286 continue;
1289 if (width == 1 || width == 2) {
1290 if (cols + width > nscroll)
1291 break;
1292 cols += width;
1293 i++;
1294 } else if (width == -1) {
1295 if (wline[i] == L'\t') {
1296 width = TABSIZE -
1297 ((cols + col_tab_align) % TABSIZE);
1298 } else {
1299 width = 1;
1300 wline[i] = L'.';
1302 if (cols + width > nscroll)
1303 break;
1304 cols += width;
1305 i++;
1306 } else
1307 return got_error_from_errno("wcwidth");
1310 *scrollx = i;
1311 return NULL;
1315 * Format a line for display, ensuring that it won't overflow a width limit.
1316 * With scrolling, the width returned refers to the scrolled version of the
1317 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1319 static const struct got_error *
1320 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1321 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1323 const struct got_error *err = NULL;
1324 int cols = 0;
1325 wchar_t *wline = NULL;
1326 char *exstr = NULL;
1327 size_t wlen;
1328 int i, scrollx = 0;
1330 *wlinep = NULL;
1331 *widthp = 0;
1333 if (expand) {
1334 err = expand_tab(&exstr, line);
1335 if (err)
1336 return err;
1339 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1340 free(exstr);
1341 if (err)
1342 return err;
1344 err = scroll_wline(&scrollx, wline, nscroll, col_tab_align);
1345 if (err)
1346 goto done;
1348 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1349 wline[wlen - 1] = L'\0';
1350 wlen--;
1352 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1353 wline[wlen - 1] = L'\0';
1354 wlen--;
1357 i = scrollx;
1358 while (i < wlen) {
1359 int width = wcwidth(wline[i]);
1361 if (width == 0) {
1362 i++;
1363 continue;
1366 if (width == 1 || width == 2) {
1367 if (cols + width > wlimit)
1368 break;
1369 cols += width;
1370 i++;
1371 } else if (width == -1) {
1372 if (wline[i] == L'\t') {
1373 width = TABSIZE -
1374 ((cols + col_tab_align) % TABSIZE);
1375 } else {
1376 width = 1;
1377 wline[i] = L'.';
1379 if (cols + width > wlimit)
1380 break;
1381 cols += width;
1382 i++;
1383 } else {
1384 err = got_error_from_errno("wcwidth");
1385 goto done;
1388 wline[i] = L'\0';
1389 if (widthp)
1390 *widthp = cols;
1391 if (scrollxp)
1392 *scrollxp = scrollx;
1393 done:
1394 if (err)
1395 free(wline);
1396 else
1397 *wlinep = wline;
1398 return err;
1401 static const struct got_error*
1402 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1403 struct got_object_id *id, struct got_repository *repo)
1405 static const struct got_error *err = NULL;
1406 struct got_reflist_entry *re;
1407 char *s;
1408 const char *name;
1410 *refs_str = NULL;
1412 TAILQ_FOREACH(re, refs, entry) {
1413 struct got_tag_object *tag = NULL;
1414 struct got_object_id *ref_id;
1415 int cmp;
1417 name = got_ref_get_name(re->ref);
1418 if (strcmp(name, GOT_REF_HEAD) == 0)
1419 continue;
1420 if (strncmp(name, "refs/", 5) == 0)
1421 name += 5;
1422 if (strncmp(name, "got/", 4) == 0 &&
1423 strncmp(name, "got/backup/", 11) != 0)
1424 continue;
1425 if (strncmp(name, "heads/", 6) == 0)
1426 name += 6;
1427 if (strncmp(name, "remotes/", 8) == 0) {
1428 name += 8;
1429 s = strstr(name, "/" GOT_REF_HEAD);
1430 if (s != NULL && s[strlen(s)] == '\0')
1431 continue;
1433 err = got_ref_resolve(&ref_id, repo, re->ref);
1434 if (err)
1435 break;
1436 if (strncmp(name, "tags/", 5) == 0) {
1437 err = got_object_open_as_tag(&tag, repo, ref_id);
1438 if (err) {
1439 if (err->code != GOT_ERR_OBJ_TYPE) {
1440 free(ref_id);
1441 break;
1443 /* Ref points at something other than a tag. */
1444 err = NULL;
1445 tag = NULL;
1448 cmp = got_object_id_cmp(tag ?
1449 got_object_tag_get_object_id(tag) : ref_id, id);
1450 free(ref_id);
1451 if (tag)
1452 got_object_tag_close(tag);
1453 if (cmp != 0)
1454 continue;
1455 s = *refs_str;
1456 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1457 s ? ", " : "", name) == -1) {
1458 err = got_error_from_errno("asprintf");
1459 free(s);
1460 *refs_str = NULL;
1461 break;
1463 free(s);
1466 return err;
1469 static const struct got_error *
1470 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1471 int col_tab_align)
1473 char *smallerthan;
1475 smallerthan = strchr(author, '<');
1476 if (smallerthan && smallerthan[1] != '\0')
1477 author = smallerthan + 1;
1478 author[strcspn(author, "@>")] = '\0';
1479 return format_line(wauthor, author_width, NULL, author, 0, limit,
1480 col_tab_align, 0);
1483 static const struct got_error *
1484 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1485 struct got_object_id *id, const size_t date_display_cols,
1486 int author_display_cols)
1488 struct tog_log_view_state *s = &view->state.log;
1489 const struct got_error *err = NULL;
1490 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1491 char *logmsg0 = NULL, *logmsg = NULL;
1492 char *author = NULL;
1493 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1494 int author_width, logmsg_width;
1495 char *newline, *line = NULL;
1496 int col, limit, scrollx;
1497 const int avail = view->ncols;
1498 struct tm tm;
1499 time_t committer_time;
1500 struct tog_color *tc;
1502 committer_time = got_object_commit_get_committer_time(commit);
1503 if (gmtime_r(&committer_time, &tm) == NULL)
1504 return got_error_from_errno("gmtime_r");
1505 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1506 return got_error(GOT_ERR_NO_SPACE);
1508 if (avail <= date_display_cols)
1509 limit = MIN(sizeof(datebuf) - 1, avail);
1510 else
1511 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1512 tc = get_color(&s->colors, TOG_COLOR_DATE);
1513 if (tc)
1514 wattr_on(view->window,
1515 COLOR_PAIR(tc->colorpair), NULL);
1516 waddnstr(view->window, datebuf, limit);
1517 if (tc)
1518 wattr_off(view->window,
1519 COLOR_PAIR(tc->colorpair), NULL);
1520 col = limit;
1521 if (col > avail)
1522 goto done;
1524 if (avail >= 120) {
1525 char *id_str;
1526 err = got_object_id_str(&id_str, id);
1527 if (err)
1528 goto done;
1529 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1530 if (tc)
1531 wattr_on(view->window,
1532 COLOR_PAIR(tc->colorpair), NULL);
1533 wprintw(view->window, "%.8s ", id_str);
1534 if (tc)
1535 wattr_off(view->window,
1536 COLOR_PAIR(tc->colorpair), NULL);
1537 free(id_str);
1538 col += 9;
1539 if (col > avail)
1540 goto done;
1543 author = strdup(got_object_commit_get_author(commit));
1544 if (author == NULL) {
1545 err = got_error_from_errno("strdup");
1546 goto done;
1548 err = format_author(&wauthor, &author_width, author, avail - col, col);
1549 if (err)
1550 goto done;
1551 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1552 if (tc)
1553 wattr_on(view->window,
1554 COLOR_PAIR(tc->colorpair), NULL);
1555 waddwstr(view->window, wauthor);
1556 if (tc)
1557 wattr_off(view->window,
1558 COLOR_PAIR(tc->colorpair), NULL);
1559 col += author_width;
1560 while (col < avail && author_width < author_display_cols + 2) {
1561 waddch(view->window, ' ');
1562 col++;
1563 author_width++;
1565 if (col > avail)
1566 goto done;
1568 err = got_object_commit_get_logmsg(&logmsg0, commit);
1569 if (err)
1570 goto done;
1571 logmsg = logmsg0;
1572 while (*logmsg == '\n')
1573 logmsg++;
1574 newline = strchr(logmsg, '\n');
1575 if (newline)
1576 *newline = '\0';
1577 limit = avail - col;
1578 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1579 limit, col, 1);
1580 if (err)
1581 goto done;
1582 waddwstr(view->window, &wlogmsg[scrollx]);
1583 col += MAX(logmsg_width, 0);
1584 while (col < avail) {
1585 waddch(view->window, ' ');
1586 col++;
1588 done:
1589 free(logmsg0);
1590 free(wlogmsg);
1591 free(author);
1592 free(wauthor);
1593 free(line);
1594 return err;
1597 static struct commit_queue_entry *
1598 alloc_commit_queue_entry(struct got_commit_object *commit,
1599 struct got_object_id *id)
1601 struct commit_queue_entry *entry;
1603 entry = calloc(1, sizeof(*entry));
1604 if (entry == NULL)
1605 return NULL;
1607 entry->id = id;
1608 entry->commit = commit;
1609 return entry;
1612 static void
1613 pop_commit(struct commit_queue *commits)
1615 struct commit_queue_entry *entry;
1617 entry = TAILQ_FIRST(&commits->head);
1618 TAILQ_REMOVE(&commits->head, entry, entry);
1619 got_object_commit_close(entry->commit);
1620 commits->ncommits--;
1621 /* Don't free entry->id! It is owned by the commit graph. */
1622 free(entry);
1625 static void
1626 free_commits(struct commit_queue *commits)
1628 while (!TAILQ_EMPTY(&commits->head))
1629 pop_commit(commits);
1632 static const struct got_error *
1633 match_commit(int *have_match, struct got_object_id *id,
1634 struct got_commit_object *commit, regex_t *regex)
1636 const struct got_error *err = NULL;
1637 regmatch_t regmatch;
1638 char *id_str = NULL, *logmsg = NULL;
1640 *have_match = 0;
1642 err = got_object_id_str(&id_str, id);
1643 if (err)
1644 return err;
1646 err = got_object_commit_get_logmsg(&logmsg, commit);
1647 if (err)
1648 goto done;
1650 if (regexec(regex, got_object_commit_get_author(commit), 1,
1651 &regmatch, 0) == 0 ||
1652 regexec(regex, got_object_commit_get_committer(commit), 1,
1653 &regmatch, 0) == 0 ||
1654 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1655 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1656 *have_match = 1;
1657 done:
1658 free(id_str);
1659 free(logmsg);
1660 return err;
1663 static const struct got_error *
1664 queue_commits(struct tog_log_thread_args *a)
1666 const struct got_error *err = NULL;
1669 * We keep all commits open throughout the lifetime of the log
1670 * view in order to avoid having to re-fetch commits from disk
1671 * while updating the display.
1673 do {
1674 struct got_object_id *id;
1675 struct got_commit_object *commit;
1676 struct commit_queue_entry *entry;
1677 int errcode;
1679 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1680 NULL, NULL);
1681 if (err || id == NULL)
1682 break;
1684 err = got_object_open_as_commit(&commit, a->repo, id);
1685 if (err)
1686 break;
1687 entry = alloc_commit_queue_entry(commit, id);
1688 if (entry == NULL) {
1689 err = got_error_from_errno("alloc_commit_queue_entry");
1690 break;
1693 errcode = pthread_mutex_lock(&tog_mutex);
1694 if (errcode) {
1695 err = got_error_set_errno(errcode,
1696 "pthread_mutex_lock");
1697 break;
1700 entry->idx = a->commits->ncommits;
1701 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1702 a->commits->ncommits++;
1704 if (*a->searching == TOG_SEARCH_FORWARD &&
1705 !*a->search_next_done) {
1706 int have_match;
1707 err = match_commit(&have_match, id, commit, a->regex);
1708 if (err)
1709 break;
1710 if (have_match)
1711 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1714 errcode = pthread_mutex_unlock(&tog_mutex);
1715 if (errcode && err == NULL)
1716 err = got_error_set_errno(errcode,
1717 "pthread_mutex_unlock");
1718 if (err)
1719 break;
1720 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1722 return err;
1725 static void
1726 select_commit(struct tog_log_view_state *s)
1728 struct commit_queue_entry *entry;
1729 int ncommits = 0;
1731 entry = s->first_displayed_entry;
1732 while (entry) {
1733 if (ncommits == s->selected) {
1734 s->selected_entry = entry;
1735 break;
1737 entry = TAILQ_NEXT(entry, entry);
1738 ncommits++;
1742 static const struct got_error *
1743 draw_commits(struct tog_view *view)
1745 const struct got_error *err = NULL;
1746 struct tog_log_view_state *s = &view->state.log;
1747 struct commit_queue_entry *entry = s->selected_entry;
1748 const int limit = view->nlines;
1749 int width;
1750 int ncommits, author_cols = 4;
1751 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1752 char *refs_str = NULL;
1753 wchar_t *wline;
1754 struct tog_color *tc;
1755 static const size_t date_display_cols = 12;
1757 if (s->selected_entry &&
1758 !(view->searching && view->search_next_done == 0)) {
1759 struct got_reflist_head *refs;
1760 err = got_object_id_str(&id_str, s->selected_entry->id);
1761 if (err)
1762 return err;
1763 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1764 s->selected_entry->id);
1765 if (refs) {
1766 err = build_refs_str(&refs_str, refs,
1767 s->selected_entry->id, s->repo);
1768 if (err)
1769 goto done;
1773 if (s->thread_args.commits_needed == 0)
1774 halfdelay(10); /* disable fast refresh */
1776 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1777 if (asprintf(&ncommits_str, " [%d/%d] %s",
1778 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1779 (view->searching && !view->search_next_done) ?
1780 "searching..." : "loading...") == -1) {
1781 err = got_error_from_errno("asprintf");
1782 goto done;
1784 } else {
1785 const char *search_str = NULL;
1787 if (view->searching) {
1788 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1789 search_str = "no more matches";
1790 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1791 search_str = "no matches found";
1792 else if (!view->search_next_done)
1793 search_str = "searching...";
1796 if (asprintf(&ncommits_str, " [%d/%d] %s",
1797 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1798 search_str ? search_str :
1799 (refs_str ? refs_str : "")) == -1) {
1800 err = got_error_from_errno("asprintf");
1801 goto done;
1805 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1806 if (asprintf(&header, "commit %s %s%s",
1807 id_str ? id_str : "........................................",
1808 s->in_repo_path, ncommits_str) == -1) {
1809 err = got_error_from_errno("asprintf");
1810 header = NULL;
1811 goto done;
1813 } else if (asprintf(&header, "commit %s%s",
1814 id_str ? id_str : "........................................",
1815 ncommits_str) == -1) {
1816 err = got_error_from_errno("asprintf");
1817 header = NULL;
1818 goto done;
1820 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1821 if (err)
1822 goto done;
1824 werase(view->window);
1826 if (view_needs_focus_indication(view))
1827 wstandout(view->window);
1828 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1829 if (tc)
1830 wattr_on(view->window,
1831 COLOR_PAIR(tc->colorpair), NULL);
1832 waddwstr(view->window, wline);
1833 if (tc)
1834 wattr_off(view->window,
1835 COLOR_PAIR(tc->colorpair), NULL);
1836 while (width < view->ncols) {
1837 waddch(view->window, ' ');
1838 width++;
1840 if (view_needs_focus_indication(view))
1841 wstandend(view->window);
1842 free(wline);
1843 if (limit <= 1)
1844 goto done;
1846 /* Grow author column size if necessary, and set view->maxx. */
1847 entry = s->first_displayed_entry;
1848 ncommits = 0;
1849 view->maxx = 0;
1850 while (entry) {
1851 char *author, *eol, *msg, *msg0;
1852 wchar_t *wauthor, *wmsg;
1853 int width;
1854 if (ncommits >= limit - 1)
1855 break;
1856 author = strdup(got_object_commit_get_author(entry->commit));
1857 if (author == NULL) {
1858 err = got_error_from_errno("strdup");
1859 goto done;
1861 err = format_author(&wauthor, &width, author, COLS,
1862 date_display_cols);
1863 if (author_cols < width)
1864 author_cols = width;
1865 free(wauthor);
1866 free(author);
1867 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1868 if (err)
1869 goto done;
1870 msg = msg0;
1871 while (*msg == '\n')
1872 ++msg;
1873 if ((eol = strchr(msg, '\n')))
1874 *eol = '\0';
1875 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1876 date_display_cols + author_cols, 0);
1877 if (err)
1878 goto done;
1879 view->maxx = MAX(view->maxx, width);
1880 free(msg0);
1881 free(wmsg);
1882 ncommits++;
1883 entry = TAILQ_NEXT(entry, entry);
1886 entry = s->first_displayed_entry;
1887 s->last_displayed_entry = s->first_displayed_entry;
1888 ncommits = 0;
1889 while (entry) {
1890 if (ncommits >= limit - 1)
1891 break;
1892 if (ncommits == s->selected)
1893 wstandout(view->window);
1894 err = draw_commit(view, entry->commit, entry->id,
1895 date_display_cols, author_cols);
1896 if (ncommits == s->selected)
1897 wstandend(view->window);
1898 if (err)
1899 goto done;
1900 ncommits++;
1901 s->last_displayed_entry = entry;
1902 entry = TAILQ_NEXT(entry, entry);
1905 view_vborder(view);
1906 update_panels();
1907 doupdate();
1908 done:
1909 free(id_str);
1910 free(refs_str);
1911 free(ncommits_str);
1912 free(header);
1913 return err;
1916 static void
1917 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1919 struct commit_queue_entry *entry;
1920 int nscrolled = 0;
1922 entry = TAILQ_FIRST(&s->commits.head);
1923 if (s->first_displayed_entry == entry)
1924 return;
1926 entry = s->first_displayed_entry;
1927 while (entry && nscrolled < maxscroll) {
1928 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1929 if (entry) {
1930 s->first_displayed_entry = entry;
1931 nscrolled++;
1936 static const struct got_error *
1937 trigger_log_thread(struct tog_view *view, int wait)
1939 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1940 int errcode;
1942 halfdelay(1); /* fast refresh while loading commits */
1944 while (ta->commits_needed > 0 || ta->load_all) {
1945 if (ta->log_complete)
1946 break;
1948 /* Wake the log thread. */
1949 errcode = pthread_cond_signal(&ta->need_commits);
1950 if (errcode)
1951 return got_error_set_errno(errcode,
1952 "pthread_cond_signal");
1955 * The mutex will be released while the view loop waits
1956 * in wgetch(), at which time the log thread will run.
1958 if (!wait)
1959 break;
1961 /* Display progress update in log view. */
1962 show_log_view(view);
1963 update_panels();
1964 doupdate();
1966 /* Wait right here while next commit is being loaded. */
1967 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1968 if (errcode)
1969 return got_error_set_errno(errcode,
1970 "pthread_cond_wait");
1972 /* Display progress update in log view. */
1973 show_log_view(view);
1974 update_panels();
1975 doupdate();
1978 return NULL;
1981 static const struct got_error *
1982 log_scroll_down(struct tog_view *view, int maxscroll)
1984 struct tog_log_view_state *s = &view->state.log;
1985 const struct got_error *err = NULL;
1986 struct commit_queue_entry *pentry;
1987 int nscrolled = 0, ncommits_needed;
1989 if (s->last_displayed_entry == NULL)
1990 return NULL;
1992 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1993 if (s->commits.ncommits < ncommits_needed &&
1994 !s->thread_args.log_complete) {
1996 * Ask the log thread for required amount of commits.
1998 s->thread_args.commits_needed += maxscroll;
1999 err = trigger_log_thread(view, 1);
2000 if (err)
2001 return err;
2004 do {
2005 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2006 if (pentry == NULL)
2007 break;
2009 s->last_displayed_entry = pentry;
2011 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2012 if (pentry == NULL)
2013 break;
2014 s->first_displayed_entry = pentry;
2015 } while (++nscrolled < maxscroll);
2017 return err;
2020 static const struct got_error *
2021 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2022 struct got_commit_object *commit, struct got_object_id *commit_id,
2023 struct tog_view *log_view, struct got_repository *repo)
2025 const struct got_error *err;
2026 struct got_object_qid *parent_id;
2027 struct tog_view *diff_view;
2029 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2030 if (diff_view == NULL)
2031 return got_error_from_errno("view_open");
2033 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2034 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2035 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2036 if (err == NULL)
2037 *new_view = diff_view;
2038 return err;
2041 static const struct got_error *
2042 tree_view_visit_subtree(struct tog_tree_view_state *s,
2043 struct got_tree_object *subtree)
2045 struct tog_parent_tree *parent;
2047 parent = calloc(1, sizeof(*parent));
2048 if (parent == NULL)
2049 return got_error_from_errno("calloc");
2051 parent->tree = s->tree;
2052 parent->first_displayed_entry = s->first_displayed_entry;
2053 parent->selected_entry = s->selected_entry;
2054 parent->selected = s->selected;
2055 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2056 s->tree = subtree;
2057 s->selected = 0;
2058 s->first_displayed_entry = NULL;
2059 return NULL;
2062 static const struct got_error *
2063 tree_view_walk_path(struct tog_tree_view_state *s,
2064 struct got_commit_object *commit, const char *path)
2066 const struct got_error *err = NULL;
2067 struct got_tree_object *tree = NULL;
2068 const char *p;
2069 char *slash, *subpath = NULL;
2071 /* Walk the path and open corresponding tree objects. */
2072 p = path;
2073 while (*p) {
2074 struct got_tree_entry *te;
2075 struct got_object_id *tree_id;
2076 char *te_name;
2078 while (p[0] == '/')
2079 p++;
2081 /* Ensure the correct subtree entry is selected. */
2082 slash = strchr(p, '/');
2083 if (slash == NULL)
2084 te_name = strdup(p);
2085 else
2086 te_name = strndup(p, slash - p);
2087 if (te_name == NULL) {
2088 err = got_error_from_errno("strndup");
2089 break;
2091 te = got_object_tree_find_entry(s->tree, te_name);
2092 if (te == NULL) {
2093 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2094 free(te_name);
2095 break;
2097 free(te_name);
2098 s->first_displayed_entry = s->selected_entry = te;
2100 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2101 break; /* jump to this file's entry */
2103 slash = strchr(p, '/');
2104 if (slash)
2105 subpath = strndup(path, slash - path);
2106 else
2107 subpath = strdup(path);
2108 if (subpath == NULL) {
2109 err = got_error_from_errno("strdup");
2110 break;
2113 err = got_object_id_by_path(&tree_id, s->repo, commit,
2114 subpath);
2115 if (err)
2116 break;
2118 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2119 free(tree_id);
2120 if (err)
2121 break;
2123 err = tree_view_visit_subtree(s, tree);
2124 if (err) {
2125 got_object_tree_close(tree);
2126 break;
2128 if (slash == NULL)
2129 break;
2130 free(subpath);
2131 subpath = NULL;
2132 p = slash;
2135 free(subpath);
2136 return err;
2139 static const struct got_error *
2140 browse_commit_tree(struct tog_view **new_view, int begin_x,
2141 struct commit_queue_entry *entry, const char *path,
2142 const char *head_ref_name, struct got_repository *repo)
2144 const struct got_error *err = NULL;
2145 struct tog_tree_view_state *s;
2146 struct tog_view *tree_view;
2148 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2149 if (tree_view == NULL)
2150 return got_error_from_errno("view_open");
2152 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2153 if (err)
2154 return err;
2155 s = &tree_view->state.tree;
2157 *new_view = tree_view;
2159 if (got_path_is_root_dir(path))
2160 return NULL;
2162 return tree_view_walk_path(s, entry->commit, path);
2165 static const struct got_error *
2166 block_signals_used_by_main_thread(void)
2168 sigset_t sigset;
2169 int errcode;
2171 if (sigemptyset(&sigset) == -1)
2172 return got_error_from_errno("sigemptyset");
2174 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2175 if (sigaddset(&sigset, SIGWINCH) == -1)
2176 return got_error_from_errno("sigaddset");
2177 if (sigaddset(&sigset, SIGCONT) == -1)
2178 return got_error_from_errno("sigaddset");
2179 if (sigaddset(&sigset, SIGINT) == -1)
2180 return got_error_from_errno("sigaddset");
2181 if (sigaddset(&sigset, SIGTERM) == -1)
2182 return got_error_from_errno("sigaddset");
2184 /* ncurses handles SIGTSTP */
2185 if (sigaddset(&sigset, SIGTSTP) == -1)
2186 return got_error_from_errno("sigaddset");
2188 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2189 if (errcode)
2190 return got_error_set_errno(errcode, "pthread_sigmask");
2192 return NULL;
2195 static void *
2196 log_thread(void *arg)
2198 const struct got_error *err = NULL;
2199 int errcode = 0;
2200 struct tog_log_thread_args *a = arg;
2201 int done = 0;
2203 err = block_signals_used_by_main_thread();
2204 if (err)
2205 return (void *)err;
2207 while (!done && !err && !tog_fatal_signal_received()) {
2208 err = queue_commits(a);
2209 if (err) {
2210 if (err->code != GOT_ERR_ITER_COMPLETED)
2211 return (void *)err;
2212 err = NULL;
2213 done = 1;
2214 } else if (a->commits_needed > 0 && !a->load_all)
2215 a->commits_needed--;
2217 errcode = pthread_mutex_lock(&tog_mutex);
2218 if (errcode) {
2219 err = got_error_set_errno(errcode,
2220 "pthread_mutex_lock");
2221 break;
2222 } else if (*a->quit)
2223 done = 1;
2224 else if (*a->first_displayed_entry == NULL) {
2225 *a->first_displayed_entry =
2226 TAILQ_FIRST(&a->commits->head);
2227 *a->selected_entry = *a->first_displayed_entry;
2230 errcode = pthread_cond_signal(&a->commit_loaded);
2231 if (errcode) {
2232 err = got_error_set_errno(errcode,
2233 "pthread_cond_signal");
2234 pthread_mutex_unlock(&tog_mutex);
2235 break;
2238 if (done)
2239 a->commits_needed = 0;
2240 else {
2241 if (a->commits_needed == 0 && !a->load_all) {
2242 errcode = pthread_cond_wait(&a->need_commits,
2243 &tog_mutex);
2244 if (errcode)
2245 err = got_error_set_errno(errcode,
2246 "pthread_cond_wait");
2247 if (*a->quit)
2248 done = 1;
2252 errcode = pthread_mutex_unlock(&tog_mutex);
2253 if (errcode && err == NULL)
2254 err = got_error_set_errno(errcode,
2255 "pthread_mutex_unlock");
2257 a->log_complete = 1;
2258 return (void *)err;
2261 static const struct got_error *
2262 stop_log_thread(struct tog_log_view_state *s)
2264 const struct got_error *err = NULL;
2265 int errcode;
2267 if (s->thread) {
2268 s->quit = 1;
2269 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2270 if (errcode)
2271 return got_error_set_errno(errcode,
2272 "pthread_cond_signal");
2273 errcode = pthread_mutex_unlock(&tog_mutex);
2274 if (errcode)
2275 return got_error_set_errno(errcode,
2276 "pthread_mutex_unlock");
2277 errcode = pthread_join(s->thread, (void **)&err);
2278 if (errcode)
2279 return got_error_set_errno(errcode, "pthread_join");
2280 errcode = pthread_mutex_lock(&tog_mutex);
2281 if (errcode)
2282 return got_error_set_errno(errcode,
2283 "pthread_mutex_lock");
2284 s->thread = 0; //NULL;
2287 if (s->thread_args.repo) {
2288 err = got_repo_close(s->thread_args.repo);
2289 s->thread_args.repo = NULL;
2292 if (s->thread_args.pack_fds) {
2293 const struct got_error *pack_err =
2294 got_repo_pack_fds_close(s->thread_args.pack_fds);
2295 if (err == NULL)
2296 err = pack_err;
2297 s->thread_args.pack_fds = NULL;
2300 if (s->thread_args.graph) {
2301 got_commit_graph_close(s->thread_args.graph);
2302 s->thread_args.graph = NULL;
2305 return err;
2308 static const struct got_error *
2309 close_log_view(struct tog_view *view)
2311 const struct got_error *err = NULL;
2312 struct tog_log_view_state *s = &view->state.log;
2313 int errcode;
2315 err = stop_log_thread(s);
2317 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2318 if (errcode && err == NULL)
2319 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2321 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2322 if (errcode && err == NULL)
2323 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2325 free_commits(&s->commits);
2326 free(s->in_repo_path);
2327 s->in_repo_path = NULL;
2328 free(s->start_id);
2329 s->start_id = NULL;
2330 free(s->head_ref_name);
2331 s->head_ref_name = NULL;
2332 return err;
2335 static const struct got_error *
2336 search_start_log_view(struct tog_view *view)
2338 struct tog_log_view_state *s = &view->state.log;
2340 s->matched_entry = NULL;
2341 s->search_entry = NULL;
2342 return NULL;
2345 static const struct got_error *
2346 search_next_log_view(struct tog_view *view)
2348 const struct got_error *err = NULL;
2349 struct tog_log_view_state *s = &view->state.log;
2350 struct commit_queue_entry *entry;
2352 /* Display progress update in log view. */
2353 show_log_view(view);
2354 update_panels();
2355 doupdate();
2357 if (s->search_entry) {
2358 int errcode, ch;
2359 errcode = pthread_mutex_unlock(&tog_mutex);
2360 if (errcode)
2361 return got_error_set_errno(errcode,
2362 "pthread_mutex_unlock");
2363 ch = wgetch(view->window);
2364 errcode = pthread_mutex_lock(&tog_mutex);
2365 if (errcode)
2366 return got_error_set_errno(errcode,
2367 "pthread_mutex_lock");
2368 if (ch == KEY_BACKSPACE) {
2369 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2370 return NULL;
2372 if (view->searching == TOG_SEARCH_FORWARD)
2373 entry = TAILQ_NEXT(s->search_entry, entry);
2374 else
2375 entry = TAILQ_PREV(s->search_entry,
2376 commit_queue_head, entry);
2377 } else if (s->matched_entry) {
2378 if (view->searching == TOG_SEARCH_FORWARD)
2379 entry = TAILQ_NEXT(s->matched_entry, entry);
2380 else
2381 entry = TAILQ_PREV(s->matched_entry,
2382 commit_queue_head, entry);
2383 } else {
2384 entry = s->selected_entry;
2387 while (1) {
2388 int have_match = 0;
2390 if (entry == NULL) {
2391 if (s->thread_args.log_complete ||
2392 view->searching == TOG_SEARCH_BACKWARD) {
2393 view->search_next_done =
2394 (s->matched_entry == NULL ?
2395 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2396 s->search_entry = NULL;
2397 return NULL;
2400 * Poke the log thread for more commits and return,
2401 * allowing the main loop to make progress. Search
2402 * will resume at s->search_entry once we come back.
2404 s->thread_args.commits_needed++;
2405 return trigger_log_thread(view, 0);
2408 err = match_commit(&have_match, entry->id, entry->commit,
2409 &view->regex);
2410 if (err)
2411 break;
2412 if (have_match) {
2413 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2414 s->matched_entry = entry;
2415 break;
2418 s->search_entry = entry;
2419 if (view->searching == TOG_SEARCH_FORWARD)
2420 entry = TAILQ_NEXT(entry, entry);
2421 else
2422 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2425 if (s->matched_entry) {
2426 int cur = s->selected_entry->idx;
2427 while (cur < s->matched_entry->idx) {
2428 err = input_log_view(NULL, view, KEY_DOWN);
2429 if (err)
2430 return err;
2431 cur++;
2433 while (cur > s->matched_entry->idx) {
2434 err = input_log_view(NULL, view, KEY_UP);
2435 if (err)
2436 return err;
2437 cur--;
2441 s->search_entry = NULL;
2443 return NULL;
2446 static const struct got_error *
2447 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2448 struct got_repository *repo, const char *head_ref_name,
2449 const char *in_repo_path, int log_branches)
2451 const struct got_error *err = NULL;
2452 struct tog_log_view_state *s = &view->state.log;
2453 struct got_repository *thread_repo = NULL;
2454 struct got_commit_graph *thread_graph = NULL;
2455 int errcode;
2457 if (in_repo_path != s->in_repo_path) {
2458 free(s->in_repo_path);
2459 s->in_repo_path = strdup(in_repo_path);
2460 if (s->in_repo_path == NULL)
2461 return got_error_from_errno("strdup");
2464 /* The commit queue only contains commits being displayed. */
2465 TAILQ_INIT(&s->commits.head);
2466 s->commits.ncommits = 0;
2468 s->repo = repo;
2469 if (head_ref_name) {
2470 s->head_ref_name = strdup(head_ref_name);
2471 if (s->head_ref_name == NULL) {
2472 err = got_error_from_errno("strdup");
2473 goto done;
2476 s->start_id = got_object_id_dup(start_id);
2477 if (s->start_id == NULL) {
2478 err = got_error_from_errno("got_object_id_dup");
2479 goto done;
2481 s->log_branches = log_branches;
2483 STAILQ_INIT(&s->colors);
2484 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2485 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2486 get_color_value("TOG_COLOR_COMMIT"));
2487 if (err)
2488 goto done;
2489 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2490 get_color_value("TOG_COLOR_AUTHOR"));
2491 if (err) {
2492 free_colors(&s->colors);
2493 goto done;
2495 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2496 get_color_value("TOG_COLOR_DATE"));
2497 if (err) {
2498 free_colors(&s->colors);
2499 goto done;
2503 view->show = show_log_view;
2504 view->input = input_log_view;
2505 view->close = close_log_view;
2506 view->search_start = search_start_log_view;
2507 view->search_next = search_next_log_view;
2509 if (s->thread_args.pack_fds == NULL) {
2510 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2511 if (err)
2512 goto done;
2514 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2515 s->thread_args.pack_fds);
2516 if (err)
2517 goto done;
2518 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2519 !s->log_branches);
2520 if (err)
2521 goto done;
2522 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2523 s->repo, NULL, NULL);
2524 if (err)
2525 goto done;
2527 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2528 if (errcode) {
2529 err = got_error_set_errno(errcode, "pthread_cond_init");
2530 goto done;
2532 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2533 if (errcode) {
2534 err = got_error_set_errno(errcode, "pthread_cond_init");
2535 goto done;
2538 s->thread_args.commits_needed = view->nlines;
2539 s->thread_args.graph = thread_graph;
2540 s->thread_args.commits = &s->commits;
2541 s->thread_args.in_repo_path = s->in_repo_path;
2542 s->thread_args.start_id = s->start_id;
2543 s->thread_args.repo = thread_repo;
2544 s->thread_args.log_complete = 0;
2545 s->thread_args.quit = &s->quit;
2546 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2547 s->thread_args.selected_entry = &s->selected_entry;
2548 s->thread_args.searching = &view->searching;
2549 s->thread_args.search_next_done = &view->search_next_done;
2550 s->thread_args.regex = &view->regex;
2551 done:
2552 if (err)
2553 close_log_view(view);
2554 return err;
2557 static const struct got_error *
2558 show_log_view(struct tog_view *view)
2560 const struct got_error *err;
2561 struct tog_log_view_state *s = &view->state.log;
2563 if (s->thread == 0) { //NULL) {
2564 int errcode = pthread_create(&s->thread, NULL, log_thread,
2565 &s->thread_args);
2566 if (errcode)
2567 return got_error_set_errno(errcode, "pthread_create");
2568 if (s->thread_args.commits_needed > 0) {
2569 err = trigger_log_thread(view, 1);
2570 if (err)
2571 return err;
2575 return draw_commits(view);
2578 static const struct got_error *
2579 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2581 const struct got_error *err = NULL;
2582 struct tog_log_view_state *s = &view->state.log;
2583 struct tog_view *diff_view = NULL, *tree_view = NULL;
2584 struct tog_view *ref_view = NULL;
2585 struct commit_queue_entry *entry;
2586 int begin_x = 0, n, nscroll = view->nlines - 1;
2588 if (s->thread_args.load_all) {
2589 if (ch == KEY_BACKSPACE)
2590 s->thread_args.load_all = 0;
2591 else if (s->thread_args.log_complete) {
2592 s->thread_args.load_all = 0;
2593 log_scroll_down(view, s->commits.ncommits);
2594 s->selected = MIN(view->nlines - 2,
2595 s->commits.ncommits - 1);
2596 select_commit(s);
2598 return NULL;
2601 switch (ch) {
2602 case 'q':
2603 s->quit = 1;
2604 break;
2605 case '0':
2606 view->x = 0;
2607 break;
2608 case '$':
2609 view->x = MAX(view->maxx - view->ncols / 2, 0);
2610 break;
2611 case KEY_RIGHT:
2612 case 'l':
2613 if (view->x + view->ncols / 2 < view->maxx)
2614 view->x += 2; /* move two columns right */
2615 break;
2616 case KEY_LEFT:
2617 case 'h':
2618 view->x -= MIN(view->x, 2); /* move two columns back */
2619 break;
2620 case 'k':
2621 case KEY_UP:
2622 case '<':
2623 case ',':
2624 case CTRL('p'):
2625 if (s->first_displayed_entry == NULL)
2626 break;
2627 if (s->selected > 0)
2628 s->selected--;
2629 else
2630 log_scroll_up(s, 1);
2631 select_commit(s);
2632 break;
2633 case 'g':
2634 case KEY_HOME:
2635 s->selected = 0;
2636 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2637 select_commit(s);
2638 break;
2639 case CTRL('u'):
2640 case 'u':
2641 nscroll /= 2;
2642 /* FALL THROUGH */
2643 case KEY_PPAGE:
2644 case CTRL('b'):
2645 if (s->first_displayed_entry == NULL)
2646 break;
2647 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2648 s->selected = MAX(0, s->selected - nscroll - 1);
2649 else
2650 log_scroll_up(s, nscroll);
2651 select_commit(s);
2652 break;
2653 case 'j':
2654 case KEY_DOWN:
2655 case '>':
2656 case '.':
2657 case CTRL('n'):
2658 if (s->first_displayed_entry == NULL)
2659 break;
2660 if (s->selected < MIN(view->nlines - 2,
2661 s->commits.ncommits - 1))
2662 s->selected++;
2663 else {
2664 err = log_scroll_down(view, 1);
2665 if (err)
2666 break;
2668 select_commit(s);
2669 break;
2670 case 'G':
2671 case KEY_END: {
2672 /* We don't know yet how many commits, so we're forced to
2673 * traverse them all. */
2674 if (!s->thread_args.log_complete) {
2675 s->thread_args.load_all = 1;
2676 return trigger_log_thread(view, 0);
2679 s->selected = 0;
2680 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2681 for (n = 0; n < view->nlines - 1; n++) {
2682 if (entry == NULL)
2683 break;
2684 s->first_displayed_entry = entry;
2685 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2687 if (n > 0)
2688 s->selected = n - 1;
2689 select_commit(s);
2690 break;
2692 case CTRL('d'):
2693 case 'd':
2694 nscroll /= 2;
2695 /* FALL THROUGH */
2696 case KEY_NPAGE:
2697 case CTRL('f'): {
2698 struct commit_queue_entry *first;
2699 first = s->first_displayed_entry;
2700 if (first == NULL)
2701 break;
2702 err = log_scroll_down(view, nscroll);
2703 if (err)
2704 break;
2705 if (first == s->first_displayed_entry &&
2706 s->selected < MIN(view->nlines - 2,
2707 s->commits.ncommits - 1)) {
2708 /* can't scroll further down */
2709 s->selected += MIN(s->last_displayed_entry->idx -
2710 s->selected_entry->idx, nscroll + 1);
2712 select_commit(s);
2713 break;
2715 case KEY_RESIZE:
2716 if (s->selected > view->nlines - 2)
2717 s->selected = view->nlines - 2;
2718 if (s->selected > s->commits.ncommits - 1)
2719 s->selected = s->commits.ncommits - 1;
2720 select_commit(s);
2721 if (s->commits.ncommits < view->nlines - 1 &&
2722 !s->thread_args.log_complete) {
2723 s->thread_args.commits_needed += (view->nlines - 1) -
2724 s->commits.ncommits;
2725 err = trigger_log_thread(view, 1);
2727 break;
2728 case KEY_ENTER:
2729 case ' ':
2730 case '\r':
2731 if (s->selected_entry == NULL)
2732 break;
2733 if (view_is_parent_view(view))
2734 begin_x = view_split_begin_x(view->begin_x);
2735 err = open_diff_view_for_commit(&diff_view, begin_x,
2736 s->selected_entry->commit, s->selected_entry->id,
2737 view, s->repo);
2738 if (err)
2739 break;
2740 view->focussed = 0;
2741 diff_view->focussed = 1;
2742 if (view_is_parent_view(view)) {
2743 err = view_close_child(view);
2744 if (err)
2745 return err;
2746 view_set_child(view, diff_view);
2747 view->focus_child = 1;
2748 } else
2749 *new_view = diff_view;
2750 break;
2751 case 't':
2752 if (s->selected_entry == NULL)
2753 break;
2754 if (view_is_parent_view(view))
2755 begin_x = view_split_begin_x(view->begin_x);
2756 err = browse_commit_tree(&tree_view, begin_x,
2757 s->selected_entry, s->in_repo_path, s->head_ref_name,
2758 s->repo);
2759 if (err)
2760 break;
2761 view->focussed = 0;
2762 tree_view->focussed = 1;
2763 if (view_is_parent_view(view)) {
2764 err = view_close_child(view);
2765 if (err)
2766 return err;
2767 view_set_child(view, tree_view);
2768 view->focus_child = 1;
2769 } else
2770 *new_view = tree_view;
2771 break;
2772 case KEY_BACKSPACE:
2773 case CTRL('l'):
2774 case 'B':
2775 if (ch == KEY_BACKSPACE &&
2776 got_path_is_root_dir(s->in_repo_path))
2777 break;
2778 err = stop_log_thread(s);
2779 if (err)
2780 return err;
2781 if (ch == KEY_BACKSPACE) {
2782 char *parent_path;
2783 err = got_path_dirname(&parent_path, s->in_repo_path);
2784 if (err)
2785 return err;
2786 free(s->in_repo_path);
2787 s->in_repo_path = parent_path;
2788 s->thread_args.in_repo_path = s->in_repo_path;
2789 } else if (ch == CTRL('l')) {
2790 struct got_object_id *start_id;
2791 err = got_repo_match_object_id(&start_id, NULL,
2792 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2793 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2794 if (err)
2795 return err;
2796 free(s->start_id);
2797 s->start_id = start_id;
2798 s->thread_args.start_id = s->start_id;
2799 } else /* 'B' */
2800 s->log_branches = !s->log_branches;
2802 if (s->thread_args.pack_fds == NULL) {
2803 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2804 if (err)
2805 return err;
2807 err = got_repo_open(&s->thread_args.repo,
2808 got_repo_get_path(s->repo), NULL,
2809 s->thread_args.pack_fds);
2810 if (err)
2811 return err;
2812 tog_free_refs();
2813 err = tog_load_refs(s->repo, 0);
2814 if (err)
2815 return err;
2816 err = got_commit_graph_open(&s->thread_args.graph,
2817 s->in_repo_path, !s->log_branches);
2818 if (err)
2819 return err;
2820 err = got_commit_graph_iter_start(s->thread_args.graph,
2821 s->start_id, s->repo, NULL, NULL);
2822 if (err)
2823 return err;
2824 free_commits(&s->commits);
2825 s->first_displayed_entry = NULL;
2826 s->last_displayed_entry = NULL;
2827 s->selected_entry = NULL;
2828 s->selected = 0;
2829 s->thread_args.log_complete = 0;
2830 s->quit = 0;
2831 s->thread_args.commits_needed = view->nlines;
2832 break;
2833 case 'r':
2834 if (view_is_parent_view(view))
2835 begin_x = view_split_begin_x(view->begin_x);
2836 ref_view = view_open(view->nlines, view->ncols,
2837 view->begin_y, begin_x, TOG_VIEW_REF);
2838 if (ref_view == NULL)
2839 return got_error_from_errno("view_open");
2840 err = open_ref_view(ref_view, s->repo);
2841 if (err) {
2842 view_close(ref_view);
2843 return err;
2845 view->focussed = 0;
2846 ref_view->focussed = 1;
2847 if (view_is_parent_view(view)) {
2848 err = view_close_child(view);
2849 if (err)
2850 return err;
2851 view_set_child(view, ref_view);
2852 view->focus_child = 1;
2853 } else
2854 *new_view = ref_view;
2855 break;
2856 default:
2857 break;
2860 return err;
2863 static const struct got_error *
2864 apply_unveil(const char *repo_path, const char *worktree_path)
2866 const struct got_error *error;
2868 #ifdef PROFILE
2869 if (unveil("gmon.out", "rwc") != 0)
2870 return got_error_from_errno2("unveil", "gmon.out");
2871 #endif
2872 if (repo_path && unveil(repo_path, "r") != 0)
2873 return got_error_from_errno2("unveil", repo_path);
2875 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2876 return got_error_from_errno2("unveil", worktree_path);
2878 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2879 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2881 error = got_privsep_unveil_exec_helpers();
2882 if (error != NULL)
2883 return error;
2885 if (unveil(NULL, NULL) != 0)
2886 return got_error_from_errno("unveil");
2888 return NULL;
2891 static void
2892 init_curses(void)
2895 * Override default signal handlers before starting ncurses.
2896 * This should prevent ncurses from installing its own
2897 * broken cleanup() signal handler.
2899 signal(SIGWINCH, tog_sigwinch);
2900 signal(SIGPIPE, tog_sigpipe);
2901 signal(SIGCONT, tog_sigcont);
2902 signal(SIGINT, tog_sigint);
2903 signal(SIGTERM, tog_sigterm);
2905 initscr();
2906 cbreak();
2907 halfdelay(1); /* Do fast refresh while initial view is loading. */
2908 noecho();
2909 nonl();
2910 intrflush(stdscr, FALSE);
2911 keypad(stdscr, TRUE);
2912 curs_set(0);
2913 if (getenv("TOG_COLORS") != NULL) {
2914 start_color();
2915 use_default_colors();
2919 static const struct got_error *
2920 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2921 struct got_repository *repo, struct got_worktree *worktree)
2923 const struct got_error *err = NULL;
2925 if (argc == 0) {
2926 *in_repo_path = strdup("/");
2927 if (*in_repo_path == NULL)
2928 return got_error_from_errno("strdup");
2929 return NULL;
2932 if (worktree) {
2933 const char *prefix = got_worktree_get_path_prefix(worktree);
2934 char *p;
2936 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2937 if (err)
2938 return err;
2939 if (asprintf(in_repo_path, "%s%s%s", prefix,
2940 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2941 p) == -1) {
2942 err = got_error_from_errno("asprintf");
2943 *in_repo_path = NULL;
2945 free(p);
2946 } else
2947 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2949 return err;
2952 static const struct got_error *
2953 cmd_log(int argc, char *argv[])
2955 const struct got_error *error;
2956 struct got_repository *repo = NULL;
2957 struct got_worktree *worktree = NULL;
2958 struct got_object_id *start_id = NULL;
2959 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2960 char *start_commit = NULL, *label = NULL;
2961 struct got_reference *ref = NULL;
2962 const char *head_ref_name = NULL;
2963 int ch, log_branches = 0;
2964 struct tog_view *view;
2965 int *pack_fds = NULL;
2967 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2968 switch (ch) {
2969 case 'b':
2970 log_branches = 1;
2971 break;
2972 case 'c':
2973 start_commit = optarg;
2974 break;
2975 case 'r':
2976 repo_path = realpath(optarg, NULL);
2977 if (repo_path == NULL)
2978 return got_error_from_errno2("realpath",
2979 optarg);
2980 break;
2981 default:
2982 usage_log();
2983 /* NOTREACHED */
2987 argc -= optind;
2988 argv += optind;
2990 if (argc > 1)
2991 usage_log();
2993 error = got_repo_pack_fds_open(&pack_fds);
2994 if (error != NULL)
2995 goto done;
2997 if (repo_path == NULL) {
2998 cwd = getcwd(NULL, 0);
2999 if (cwd == NULL)
3000 return got_error_from_errno("getcwd");
3001 error = got_worktree_open(&worktree, cwd);
3002 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3003 goto done;
3004 if (worktree)
3005 repo_path =
3006 strdup(got_worktree_get_repo_path(worktree));
3007 else
3008 repo_path = strdup(cwd);
3009 if (repo_path == NULL) {
3010 error = got_error_from_errno("strdup");
3011 goto done;
3015 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3016 if (error != NULL)
3017 goto done;
3019 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3020 repo, worktree);
3021 if (error)
3022 goto done;
3024 init_curses();
3026 error = apply_unveil(got_repo_get_path(repo),
3027 worktree ? got_worktree_get_root_path(worktree) : NULL);
3028 if (error)
3029 goto done;
3031 /* already loaded by tog_log_with_path()? */
3032 if (TAILQ_EMPTY(&tog_refs)) {
3033 error = tog_load_refs(repo, 0);
3034 if (error)
3035 goto done;
3038 if (start_commit == NULL) {
3039 error = got_repo_match_object_id(&start_id, &label,
3040 worktree ? got_worktree_get_head_ref_name(worktree) :
3041 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3042 if (error)
3043 goto done;
3044 head_ref_name = label;
3045 } else {
3046 error = got_ref_open(&ref, repo, start_commit, 0);
3047 if (error == NULL)
3048 head_ref_name = got_ref_get_name(ref);
3049 else if (error->code != GOT_ERR_NOT_REF)
3050 goto done;
3051 error = got_repo_match_object_id(&start_id, NULL,
3052 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3053 if (error)
3054 goto done;
3057 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3058 if (view == NULL) {
3059 error = got_error_from_errno("view_open");
3060 goto done;
3062 error = open_log_view(view, start_id, repo, head_ref_name,
3063 in_repo_path, log_branches);
3064 if (error)
3065 goto done;
3066 if (worktree) {
3067 /* Release work tree lock. */
3068 got_worktree_close(worktree);
3069 worktree = NULL;
3071 error = view_loop(view);
3072 done:
3073 free(in_repo_path);
3074 free(repo_path);
3075 free(cwd);
3076 free(start_id);
3077 free(label);
3078 if (ref)
3079 got_ref_close(ref);
3080 if (repo) {
3081 const struct got_error *close_err = got_repo_close(repo);
3082 if (error == NULL)
3083 error = close_err;
3085 if (worktree)
3086 got_worktree_close(worktree);
3087 if (pack_fds) {
3088 const struct got_error *pack_err =
3089 got_repo_pack_fds_close(pack_fds);
3090 if (error == NULL)
3091 error = pack_err;
3093 tog_free_refs();
3094 return error;
3097 __dead static void
3098 usage_diff(void)
3100 endwin();
3101 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3102 "[-w] object1 object2\n", getprogname());
3103 exit(1);
3106 static int
3107 match_line(const char *line, regex_t *regex, size_t nmatch,
3108 regmatch_t *regmatch)
3110 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3113 struct tog_color *
3114 match_color(struct tog_colors *colors, const char *line)
3116 struct tog_color *tc = NULL;
3118 STAILQ_FOREACH(tc, colors, entry) {
3119 if (match_line(line, &tc->regex, 0, NULL))
3120 return tc;
3123 return NULL;
3126 static const struct got_error *
3127 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3128 WINDOW *window, int skip, regmatch_t *regmatch)
3130 const struct got_error *err = NULL;
3131 wchar_t *wline;
3132 int rme, rms, n, width;
3134 *wtotal = 0;
3135 rms = regmatch->rm_so;
3136 rme = regmatch->rm_eo;
3138 err = format_line(&wline, &width, NULL, line, 0, wlimit + skip,
3139 col_tab_align, 1);
3140 if (err)
3141 return err;
3143 /* draw up to matched token if we haven't scrolled past it */
3144 n = MAX(rms - skip, 0);
3145 if (n) {
3146 waddnwstr(window, wline + skip, n);
3147 wlimit -= n;
3148 *wtotal += n;
3151 if (wlimit > 0) {
3152 int len = rme - rms;
3153 n = 0;
3154 if (skip > rms) {
3155 n = skip - rms;
3156 len = MAX(len - n, 0);
3158 /* draw (visible part of) matched token (if scrolled into it) */
3159 if (len) {
3160 wattron(window, A_STANDOUT);
3161 waddnwstr(window, wline + rms + n, len);
3162 wattroff(window, A_STANDOUT);
3163 wlimit -= len;
3164 *wtotal += len;
3168 if (wlimit > 0 && skip < width) { /* draw rest of line */
3169 n = 0;
3170 if (skip > rme)
3171 n = MIN(skip - rme, width - rme);
3172 waddnwstr(window, wline + rme + n, wlimit);
3175 *wtotal = width;
3176 free(wline);
3177 return NULL;
3180 static const struct got_error *
3181 draw_file(struct tog_view *view, const char *header)
3183 struct tog_diff_view_state *s = &view->state.diff;
3184 regmatch_t *regmatch = &view->regmatch;
3185 const struct got_error *err;
3186 int nprinted = 0;
3187 char *line;
3188 size_t linesize = 0;
3189 ssize_t linelen;
3190 struct tog_color *tc;
3191 wchar_t *wline;
3192 int width;
3193 int max_lines = view->nlines;
3194 int nlines = s->nlines;
3195 off_t line_offset;
3197 line_offset = s->line_offsets[s->first_displayed_line - 1];
3198 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3199 return got_error_from_errno("fseek");
3201 werase(view->window);
3203 if (header) {
3204 if (asprintf(&line, "[%d/%d] %s",
3205 s->first_displayed_line - 1 + s->selected_line, nlines,
3206 header) == -1)
3207 return got_error_from_errno("asprintf");
3208 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3209 0, 0);
3210 free(line);
3211 if (err)
3212 return err;
3214 if (view_needs_focus_indication(view))
3215 wstandout(view->window);
3216 waddwstr(view->window, wline);
3217 free(wline);
3218 wline = NULL;
3219 if (view_needs_focus_indication(view))
3220 wstandend(view->window);
3221 if (width <= view->ncols - 1)
3222 waddch(view->window, '\n');
3224 if (max_lines <= 1)
3225 return NULL;
3226 max_lines--;
3229 s->eof = 0;
3230 view->maxx = 0;
3231 line = NULL;
3232 while (max_lines > 0 && nprinted < max_lines) {
3233 linelen = getline(&line, &linesize, s->f);
3234 if (linelen == -1) {
3235 if (feof(s->f)) {
3236 s->eof = 1;
3237 break;
3239 free(line);
3240 return got_ferror(s->f, GOT_ERR_IO);
3243 view->maxx = MAX(view->maxx, linelen);
3245 tc = match_color(&s->colors, line);
3246 if (tc)
3247 wattr_on(view->window,
3248 COLOR_PAIR(tc->colorpair), NULL);
3249 if (s->first_displayed_line + nprinted == s->matched_line &&
3250 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3251 err = add_matched_line(&width, line, view->ncols, 0,
3252 view->window, view->x, regmatch);
3253 if (err) {
3254 free(line);
3255 return err;
3257 } else {
3258 err = format_line(&wline, &width, NULL, line, 0,
3259 view->x + view->ncols, 0, view->x ? 1 : 0);
3260 if (err) {
3261 free(line);
3262 return err;
3264 if (view->x < width)
3265 waddwstr(view->window, wline + view->x);
3266 free(wline);
3267 wline = NULL;
3269 if (tc)
3270 wattr_off(view->window,
3271 COLOR_PAIR(tc->colorpair), NULL);
3272 if (width - view->x <= view->ncols - 1)
3273 waddch(view->window, '\n');
3274 nprinted++;
3276 free(line);
3277 if (nprinted >= 1)
3278 s->last_displayed_line = s->first_displayed_line +
3279 (nprinted - 1);
3280 else
3281 s->last_displayed_line = s->first_displayed_line;
3283 view_vborder(view);
3285 if (s->eof) {
3286 while (nprinted < view->nlines) {
3287 waddch(view->window, '\n');
3288 nprinted++;
3291 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3292 view->ncols, 0, 0);
3293 if (err) {
3294 return err;
3297 wstandout(view->window);
3298 waddwstr(view->window, wline);
3299 free(wline);
3300 wline = NULL;
3301 wstandend(view->window);
3304 return NULL;
3307 static char *
3308 get_datestr(time_t *time, char *datebuf)
3310 struct tm mytm, *tm;
3311 char *p, *s;
3313 tm = gmtime_r(time, &mytm);
3314 if (tm == NULL)
3315 return NULL;
3316 s = asctime_r(tm, datebuf);
3317 if (s == NULL)
3318 return NULL;
3319 p = strchr(s, '\n');
3320 if (p)
3321 *p = '\0';
3322 return s;
3325 static const struct got_error *
3326 get_changed_paths(struct got_pathlist_head *paths,
3327 struct got_commit_object *commit, struct got_repository *repo)
3329 const struct got_error *err = NULL;
3330 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3331 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3332 struct got_object_qid *qid;
3334 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3335 if (qid != NULL) {
3336 struct got_commit_object *pcommit;
3337 err = got_object_open_as_commit(&pcommit, repo,
3338 &qid->id);
3339 if (err)
3340 return err;
3342 tree_id1 = got_object_id_dup(
3343 got_object_commit_get_tree_id(pcommit));
3344 if (tree_id1 == NULL) {
3345 got_object_commit_close(pcommit);
3346 return got_error_from_errno("got_object_id_dup");
3348 got_object_commit_close(pcommit);
3352 if (tree_id1) {
3353 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3354 if (err)
3355 goto done;
3358 tree_id2 = got_object_commit_get_tree_id(commit);
3359 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3360 if (err)
3361 goto done;
3363 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3364 got_diff_tree_collect_changed_paths, paths, 0);
3365 done:
3366 if (tree1)
3367 got_object_tree_close(tree1);
3368 if (tree2)
3369 got_object_tree_close(tree2);
3370 free(tree_id1);
3371 return err;
3374 static const struct got_error *
3375 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3377 off_t *p;
3379 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3380 if (p == NULL)
3381 return got_error_from_errno("reallocarray");
3382 *line_offsets = p;
3383 (*line_offsets)[*nlines] = off;
3384 (*nlines)++;
3385 return NULL;
3388 static const struct got_error *
3389 write_commit_info(off_t **line_offsets, size_t *nlines,
3390 struct got_object_id *commit_id, struct got_reflist_head *refs,
3391 struct got_repository *repo, FILE *outfile)
3393 const struct got_error *err = NULL;
3394 char datebuf[26], *datestr;
3395 struct got_commit_object *commit;
3396 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3397 time_t committer_time;
3398 const char *author, *committer;
3399 char *refs_str = NULL;
3400 struct got_pathlist_head changed_paths;
3401 struct got_pathlist_entry *pe;
3402 off_t outoff = 0;
3403 int n;
3405 TAILQ_INIT(&changed_paths);
3407 if (refs) {
3408 err = build_refs_str(&refs_str, refs, commit_id, repo);
3409 if (err)
3410 return err;
3413 err = got_object_open_as_commit(&commit, repo, commit_id);
3414 if (err)
3415 return err;
3417 err = got_object_id_str(&id_str, commit_id);
3418 if (err) {
3419 err = got_error_from_errno("got_object_id_str");
3420 goto done;
3423 err = add_line_offset(line_offsets, nlines, 0);
3424 if (err)
3425 goto done;
3427 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3428 refs_str ? refs_str : "", refs_str ? ")" : "");
3429 if (n < 0) {
3430 err = got_error_from_errno("fprintf");
3431 goto done;
3433 outoff += n;
3434 err = add_line_offset(line_offsets, nlines, outoff);
3435 if (err)
3436 goto done;
3438 n = fprintf(outfile, "from: %s\n",
3439 got_object_commit_get_author(commit));
3440 if (n < 0) {
3441 err = got_error_from_errno("fprintf");
3442 goto done;
3444 outoff += n;
3445 err = add_line_offset(line_offsets, nlines, outoff);
3446 if (err)
3447 goto done;
3449 committer_time = got_object_commit_get_committer_time(commit);
3450 datestr = get_datestr(&committer_time, datebuf);
3451 if (datestr) {
3452 n = fprintf(outfile, "date: %s UTC\n", datestr);
3453 if (n < 0) {
3454 err = got_error_from_errno("fprintf");
3455 goto done;
3457 outoff += n;
3458 err = add_line_offset(line_offsets, nlines, outoff);
3459 if (err)
3460 goto done;
3462 author = got_object_commit_get_author(commit);
3463 committer = got_object_commit_get_committer(commit);
3464 if (strcmp(author, committer) != 0) {
3465 n = fprintf(outfile, "via: %s\n", committer);
3466 if (n < 0) {
3467 err = got_error_from_errno("fprintf");
3468 goto done;
3470 outoff += n;
3471 err = add_line_offset(line_offsets, nlines, outoff);
3472 if (err)
3473 goto done;
3475 if (got_object_commit_get_nparents(commit) > 1) {
3476 const struct got_object_id_queue *parent_ids;
3477 struct got_object_qid *qid;
3478 int pn = 1;
3479 parent_ids = got_object_commit_get_parent_ids(commit);
3480 STAILQ_FOREACH(qid, parent_ids, entry) {
3481 err = got_object_id_str(&id_str, &qid->id);
3482 if (err)
3483 goto done;
3484 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3485 if (n < 0) {
3486 err = got_error_from_errno("fprintf");
3487 goto done;
3489 outoff += n;
3490 err = add_line_offset(line_offsets, nlines, outoff);
3491 if (err)
3492 goto done;
3493 free(id_str);
3494 id_str = NULL;
3498 err = got_object_commit_get_logmsg(&logmsg, commit);
3499 if (err)
3500 goto done;
3501 s = logmsg;
3502 while ((line = strsep(&s, "\n")) != NULL) {
3503 n = fprintf(outfile, "%s\n", line);
3504 if (n < 0) {
3505 err = got_error_from_errno("fprintf");
3506 goto done;
3508 outoff += n;
3509 err = add_line_offset(line_offsets, nlines, outoff);
3510 if (err)
3511 goto done;
3514 err = get_changed_paths(&changed_paths, commit, repo);
3515 if (err)
3516 goto done;
3517 TAILQ_FOREACH(pe, &changed_paths, entry) {
3518 struct got_diff_changed_path *cp = pe->data;
3519 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3520 if (n < 0) {
3521 err = got_error_from_errno("fprintf");
3522 goto done;
3524 outoff += n;
3525 err = add_line_offset(line_offsets, nlines, outoff);
3526 if (err)
3527 goto done;
3528 free((char *)pe->path);
3529 free(pe->data);
3532 fputc('\n', outfile);
3533 outoff++;
3534 err = add_line_offset(line_offsets, nlines, outoff);
3535 done:
3536 got_pathlist_free(&changed_paths);
3537 free(id_str);
3538 free(logmsg);
3539 free(refs_str);
3540 got_object_commit_close(commit);
3541 if (err) {
3542 free(*line_offsets);
3543 *line_offsets = NULL;
3544 *nlines = 0;
3546 return err;
3549 static const struct got_error *
3550 create_diff(struct tog_diff_view_state *s)
3552 const struct got_error *err = NULL;
3553 FILE *f = NULL;
3554 int obj_type;
3556 free(s->line_offsets);
3557 s->line_offsets = malloc(sizeof(off_t));
3558 if (s->line_offsets == NULL)
3559 return got_error_from_errno("malloc");
3560 s->nlines = 0;
3562 f = got_opentemp();
3563 if (f == NULL) {
3564 err = got_error_from_errno("got_opentemp");
3565 goto done;
3567 if (s->f && fclose(s->f) == EOF) {
3568 err = got_error_from_errno("fclose");
3569 goto done;
3571 s->f = f;
3573 if (s->id1)
3574 err = got_object_get_type(&obj_type, s->repo, s->id1);
3575 else
3576 err = got_object_get_type(&obj_type, s->repo, s->id2);
3577 if (err)
3578 goto done;
3580 switch (obj_type) {
3581 case GOT_OBJ_TYPE_BLOB:
3582 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3583 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3584 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3585 s->repo, s->f);
3586 break;
3587 case GOT_OBJ_TYPE_TREE:
3588 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3589 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3590 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3591 break;
3592 case GOT_OBJ_TYPE_COMMIT: {
3593 const struct got_object_id_queue *parent_ids;
3594 struct got_object_qid *pid;
3595 struct got_commit_object *commit2;
3596 struct got_reflist_head *refs;
3598 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3599 if (err)
3600 goto done;
3601 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3602 /* Show commit info if we're diffing to a parent/root commit. */
3603 if (s->id1 == NULL) {
3604 err = write_commit_info(&s->line_offsets, &s->nlines,
3605 s->id2, refs, s->repo, s->f);
3606 if (err)
3607 goto done;
3608 } else {
3609 parent_ids = got_object_commit_get_parent_ids(commit2);
3610 STAILQ_FOREACH(pid, parent_ids, entry) {
3611 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3612 err = write_commit_info(
3613 &s->line_offsets, &s->nlines,
3614 s->id2, refs, s->repo, s->f);
3615 if (err)
3616 goto done;
3617 break;
3621 got_object_commit_close(commit2);
3623 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3624 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3625 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3626 break;
3628 default:
3629 err = got_error(GOT_ERR_OBJ_TYPE);
3630 break;
3632 if (err)
3633 goto done;
3634 done:
3635 if (s->f && fflush(s->f) != 0 && err == NULL)
3636 err = got_error_from_errno("fflush");
3637 return err;
3640 static void
3641 diff_view_indicate_progress(struct tog_view *view)
3643 mvwaddstr(view->window, 0, 0, "diffing...");
3644 update_panels();
3645 doupdate();
3648 static const struct got_error *
3649 search_start_diff_view(struct tog_view *view)
3651 struct tog_diff_view_state *s = &view->state.diff;
3653 s->matched_line = 0;
3654 return NULL;
3657 static const struct got_error *
3658 search_next_diff_view(struct tog_view *view)
3660 struct tog_diff_view_state *s = &view->state.diff;
3661 const struct got_error *err = NULL;
3662 int lineno;
3663 char *exstr = NULL, *line = NULL;
3664 size_t linesize = 0;
3665 ssize_t linelen;
3667 if (!view->searching) {
3668 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3669 return NULL;
3672 if (s->matched_line) {
3673 if (view->searching == TOG_SEARCH_FORWARD)
3674 lineno = s->matched_line + 1;
3675 else
3676 lineno = s->matched_line - 1;
3677 } else
3678 lineno = s->first_displayed_line;
3680 while (1) {
3681 off_t offset;
3683 if (lineno <= 0 || lineno > s->nlines) {
3684 if (s->matched_line == 0) {
3685 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3686 break;
3689 if (view->searching == TOG_SEARCH_FORWARD)
3690 lineno = 1;
3691 else
3692 lineno = s->nlines;
3695 offset = s->line_offsets[lineno - 1];
3696 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3697 free(line);
3698 return got_error_from_errno("fseeko");
3700 linelen = getline(&line, &linesize, s->f);
3701 err = expand_tab(&exstr, line);
3702 if (err)
3703 break;
3704 if (linelen != -1 &&
3705 match_line(exstr, &view->regex, 1, &view->regmatch)) {
3706 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3707 s->matched_line = lineno;
3708 break;
3710 free(exstr);
3711 exstr = NULL;
3712 if (view->searching == TOG_SEARCH_FORWARD)
3713 lineno++;
3714 else
3715 lineno--;
3717 free(line);
3718 free(exstr);
3720 if (s->matched_line) {
3721 s->first_displayed_line = s->matched_line;
3722 s->selected_line = 1;
3725 return err;
3728 static const struct got_error *
3729 close_diff_view(struct tog_view *view)
3731 const struct got_error *err = NULL;
3732 struct tog_diff_view_state *s = &view->state.diff;
3734 free(s->id1);
3735 s->id1 = NULL;
3736 free(s->id2);
3737 s->id2 = NULL;
3738 if (s->f && fclose(s->f) == EOF)
3739 err = got_error_from_errno("fclose");
3740 s->f = NULL;
3741 if (s->f1 && fclose(s->f1) == EOF)
3742 err = got_error_from_errno("fclose");
3743 s->f1 = NULL;
3744 if (s->f2 && fclose(s->f2) == EOF)
3745 err = got_error_from_errno("fclose");
3746 s->f2 = NULL;
3747 free_colors(&s->colors);
3748 free(s->line_offsets);
3749 s->line_offsets = NULL;
3750 s->nlines = 0;
3751 return err;
3754 static const struct got_error *
3755 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3756 struct got_object_id *id2, const char *label1, const char *label2,
3757 int diff_context, int ignore_whitespace, int force_text_diff,
3758 struct tog_view *log_view, struct got_repository *repo)
3760 const struct got_error *err;
3761 struct tog_diff_view_state *s = &view->state.diff;
3763 memset(s, 0, sizeof(*s));
3765 if (id1 != NULL && id2 != NULL) {
3766 int type1, type2;
3767 err = got_object_get_type(&type1, repo, id1);
3768 if (err)
3769 return err;
3770 err = got_object_get_type(&type2, repo, id2);
3771 if (err)
3772 return err;
3774 if (type1 != type2)
3775 return got_error(GOT_ERR_OBJ_TYPE);
3777 s->first_displayed_line = 1;
3778 s->last_displayed_line = view->nlines;
3779 s->selected_line = 1;
3780 s->repo = repo;
3781 s->id1 = id1;
3782 s->id2 = id2;
3783 s->label1 = label1;
3784 s->label2 = label2;
3786 if (id1) {
3787 s->id1 = got_object_id_dup(id1);
3788 if (s->id1 == NULL)
3789 return got_error_from_errno("got_object_id_dup");
3790 s->f1 = got_opentemp();
3791 if (s->f1 == NULL) {
3792 err = got_error_from_errno("got_opentemp");
3793 goto done;
3795 } else
3796 s->id1 = NULL;
3798 s->id2 = got_object_id_dup(id2);
3799 if (s->id2 == NULL) {
3800 err = got_error_from_errno("got_object_id_dup");
3801 goto done;
3804 s->f2 = got_opentemp();
3805 if (s->f2 == NULL) {
3806 err = got_error_from_errno("got_opentemp");
3807 goto done;
3810 s->first_displayed_line = 1;
3811 s->last_displayed_line = view->nlines;
3812 s->diff_context = diff_context;
3813 s->ignore_whitespace = ignore_whitespace;
3814 s->force_text_diff = force_text_diff;
3815 s->log_view = log_view;
3816 s->repo = repo;
3818 STAILQ_INIT(&s->colors);
3819 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3820 err = add_color(&s->colors,
3821 "^-", TOG_COLOR_DIFF_MINUS,
3822 get_color_value("TOG_COLOR_DIFF_MINUS"));
3823 if (err)
3824 goto done;
3825 err = add_color(&s->colors, "^\\+",
3826 TOG_COLOR_DIFF_PLUS,
3827 get_color_value("TOG_COLOR_DIFF_PLUS"));
3828 if (err)
3829 goto done;
3830 err = add_color(&s->colors,
3831 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3832 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3833 if (err)
3834 goto done;
3836 err = add_color(&s->colors,
3837 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3838 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3839 get_color_value("TOG_COLOR_DIFF_META"));
3840 if (err)
3841 goto done;
3843 err = add_color(&s->colors,
3844 "^(from|via): ", TOG_COLOR_AUTHOR,
3845 get_color_value("TOG_COLOR_AUTHOR"));
3846 if (err)
3847 goto done;
3849 err = add_color(&s->colors,
3850 "^date: ", TOG_COLOR_DATE,
3851 get_color_value("TOG_COLOR_DATE"));
3852 if (err)
3853 goto done;
3856 if (log_view && view_is_splitscreen(view))
3857 show_log_view(log_view); /* draw vborder */
3858 diff_view_indicate_progress(view);
3860 err = create_diff(s);
3862 view->show = show_diff_view;
3863 view->input = input_diff_view;
3864 view->close = close_diff_view;
3865 view->search_start = search_start_diff_view;
3866 view->search_next = search_next_diff_view;
3867 done:
3868 if (err)
3869 close_diff_view(view);
3870 return err;
3873 static const struct got_error *
3874 show_diff_view(struct tog_view *view)
3876 const struct got_error *err;
3877 struct tog_diff_view_state *s = &view->state.diff;
3878 char *id_str1 = NULL, *id_str2, *header;
3879 const char *label1, *label2;
3881 if (s->id1) {
3882 err = got_object_id_str(&id_str1, s->id1);
3883 if (err)
3884 return err;
3885 label1 = s->label1 ? : id_str1;
3886 } else
3887 label1 = "/dev/null";
3889 err = got_object_id_str(&id_str2, s->id2);
3890 if (err)
3891 return err;
3892 label2 = s->label2 ? : id_str2;
3894 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3895 err = got_error_from_errno("asprintf");
3896 free(id_str1);
3897 free(id_str2);
3898 return err;
3900 free(id_str1);
3901 free(id_str2);
3903 err = draw_file(view, header);
3904 free(header);
3905 return err;
3908 static const struct got_error *
3909 set_selected_commit(struct tog_diff_view_state *s,
3910 struct commit_queue_entry *entry)
3912 const struct got_error *err;
3913 const struct got_object_id_queue *parent_ids;
3914 struct got_commit_object *selected_commit;
3915 struct got_object_qid *pid;
3917 free(s->id2);
3918 s->id2 = got_object_id_dup(entry->id);
3919 if (s->id2 == NULL)
3920 return got_error_from_errno("got_object_id_dup");
3922 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3923 if (err)
3924 return err;
3925 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3926 free(s->id1);
3927 pid = STAILQ_FIRST(parent_ids);
3928 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3929 got_object_commit_close(selected_commit);
3930 return NULL;
3933 static const struct got_error *
3934 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3936 const struct got_error *err = NULL;
3937 struct tog_diff_view_state *s = &view->state.diff;
3938 struct tog_log_view_state *ls;
3939 struct commit_queue_entry *old_selected_entry;
3940 char *line = NULL;
3941 size_t linesize = 0;
3942 ssize_t linelen;
3943 int i, nscroll = view->nlines - 1;
3945 switch (ch) {
3946 case '0':
3947 view->x = 0;
3948 break;
3949 case '$':
3950 view->x = MAX(view->maxx - view->ncols / 3, 0);
3951 break;
3952 case KEY_RIGHT:
3953 case 'l':
3954 if (view->x + view->ncols / 3 < view->maxx)
3955 view->x += 2; /* move two columns right */
3956 break;
3957 case KEY_LEFT:
3958 case 'h':
3959 view->x -= MIN(view->x, 2); /* move two columns back */
3960 break;
3961 case 'a':
3962 case 'w':
3963 if (ch == 'a')
3964 s->force_text_diff = !s->force_text_diff;
3965 if (ch == 'w')
3966 s->ignore_whitespace = !s->ignore_whitespace;
3967 wclear(view->window);
3968 s->first_displayed_line = 1;
3969 s->last_displayed_line = view->nlines;
3970 s->matched_line = 0;
3971 diff_view_indicate_progress(view);
3972 err = create_diff(s);
3973 break;
3974 case 'g':
3975 case KEY_HOME:
3976 s->first_displayed_line = 1;
3977 break;
3978 case 'G':
3979 case KEY_END:
3980 if (s->eof)
3981 break;
3983 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3984 s->eof = 1;
3985 break;
3986 case 'k':
3987 case KEY_UP:
3988 case CTRL('p'):
3989 if (s->first_displayed_line > 1)
3990 s->first_displayed_line--;
3991 break;
3992 case CTRL('u'):
3993 case 'u':
3994 nscroll /= 2;
3995 /* FALL THROUGH */
3996 case KEY_PPAGE:
3997 case CTRL('b'):
3998 if (s->first_displayed_line == 1)
3999 break;
4000 i = 0;
4001 while (i++ < nscroll && s->first_displayed_line > 1)
4002 s->first_displayed_line--;
4003 break;
4004 case 'j':
4005 case KEY_DOWN:
4006 case CTRL('n'):
4007 if (!s->eof)
4008 s->first_displayed_line++;
4009 break;
4010 case CTRL('d'):
4011 case 'd':
4012 nscroll /= 2;
4013 /* FALL THROUGH */
4014 case KEY_NPAGE:
4015 case CTRL('f'):
4016 case ' ':
4017 if (s->eof)
4018 break;
4019 i = 0;
4020 while (!s->eof && i++ < nscroll) {
4021 linelen = getline(&line, &linesize, s->f);
4022 s->first_displayed_line++;
4023 if (linelen == -1) {
4024 if (feof(s->f)) {
4025 s->eof = 1;
4026 } else
4027 err = got_ferror(s->f, GOT_ERR_IO);
4028 break;
4031 free(line);
4032 break;
4033 case '[':
4034 if (s->diff_context > 0) {
4035 s->diff_context--;
4036 s->matched_line = 0;
4037 diff_view_indicate_progress(view);
4038 err = create_diff(s);
4039 if (s->first_displayed_line + view->nlines - 1 >
4040 s->nlines) {
4041 s->first_displayed_line = 1;
4042 s->last_displayed_line = view->nlines;
4045 break;
4046 case ']':
4047 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4048 s->diff_context++;
4049 s->matched_line = 0;
4050 diff_view_indicate_progress(view);
4051 err = create_diff(s);
4053 break;
4054 case '<':
4055 case ',':
4056 if (s->log_view == NULL)
4057 break;
4058 ls = &s->log_view->state.log;
4059 old_selected_entry = ls->selected_entry;
4061 err = input_log_view(NULL, s->log_view, KEY_UP);
4062 if (err)
4063 break;
4065 if (old_selected_entry == ls->selected_entry)
4066 break;
4068 err = set_selected_commit(s, ls->selected_entry);
4069 if (err)
4070 break;
4072 s->first_displayed_line = 1;
4073 s->last_displayed_line = view->nlines;
4074 s->matched_line = 0;
4075 view->x = 0;
4077 diff_view_indicate_progress(view);
4078 err = create_diff(s);
4079 break;
4080 case '>':
4081 case '.':
4082 if (s->log_view == NULL)
4083 break;
4084 ls = &s->log_view->state.log;
4085 old_selected_entry = ls->selected_entry;
4087 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4088 if (err)
4089 break;
4091 if (old_selected_entry == ls->selected_entry)
4092 break;
4094 err = set_selected_commit(s, ls->selected_entry);
4095 if (err)
4096 break;
4098 s->first_displayed_line = 1;
4099 s->last_displayed_line = view->nlines;
4100 s->matched_line = 0;
4101 view->x = 0;
4103 diff_view_indicate_progress(view);
4104 err = create_diff(s);
4105 break;
4106 default:
4107 break;
4110 return err;
4113 static const struct got_error *
4114 cmd_diff(int argc, char *argv[])
4116 const struct got_error *error = NULL;
4117 struct got_repository *repo = NULL;
4118 struct got_worktree *worktree = NULL;
4119 struct got_object_id *id1 = NULL, *id2 = NULL;
4120 char *repo_path = NULL, *cwd = NULL;
4121 char *id_str1 = NULL, *id_str2 = NULL;
4122 char *label1 = NULL, *label2 = NULL;
4123 int diff_context = 3, ignore_whitespace = 0;
4124 int ch, force_text_diff = 0;
4125 const char *errstr;
4126 struct tog_view *view;
4127 int *pack_fds = NULL;
4129 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4130 switch (ch) {
4131 case 'a':
4132 force_text_diff = 1;
4133 break;
4134 case 'C':
4135 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4136 &errstr);
4137 if (errstr != NULL)
4138 errx(1, "number of context lines is %s: %s",
4139 errstr, errstr);
4140 break;
4141 case 'r':
4142 repo_path = realpath(optarg, NULL);
4143 if (repo_path == NULL)
4144 return got_error_from_errno2("realpath",
4145 optarg);
4146 got_path_strip_trailing_slashes(repo_path);
4147 break;
4148 case 'w':
4149 ignore_whitespace = 1;
4150 break;
4151 default:
4152 usage_diff();
4153 /* NOTREACHED */
4157 argc -= optind;
4158 argv += optind;
4160 if (argc == 0) {
4161 usage_diff(); /* TODO show local worktree changes */
4162 } else if (argc == 2) {
4163 id_str1 = argv[0];
4164 id_str2 = argv[1];
4165 } else
4166 usage_diff();
4168 error = got_repo_pack_fds_open(&pack_fds);
4169 if (error)
4170 goto done;
4172 if (repo_path == NULL) {
4173 cwd = getcwd(NULL, 0);
4174 if (cwd == NULL)
4175 return got_error_from_errno("getcwd");
4176 error = got_worktree_open(&worktree, cwd);
4177 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4178 goto done;
4179 if (worktree)
4180 repo_path =
4181 strdup(got_worktree_get_repo_path(worktree));
4182 else
4183 repo_path = strdup(cwd);
4184 if (repo_path == NULL) {
4185 error = got_error_from_errno("strdup");
4186 goto done;
4190 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4191 if (error)
4192 goto done;
4194 init_curses();
4196 error = apply_unveil(got_repo_get_path(repo), NULL);
4197 if (error)
4198 goto done;
4200 error = tog_load_refs(repo, 0);
4201 if (error)
4202 goto done;
4204 error = got_repo_match_object_id(&id1, &label1, id_str1,
4205 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4206 if (error)
4207 goto done;
4209 error = got_repo_match_object_id(&id2, &label2, id_str2,
4210 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4211 if (error)
4212 goto done;
4214 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4215 if (view == NULL) {
4216 error = got_error_from_errno("view_open");
4217 goto done;
4219 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4220 ignore_whitespace, force_text_diff, NULL, repo);
4221 if (error)
4222 goto done;
4223 error = view_loop(view);
4224 done:
4225 free(label1);
4226 free(label2);
4227 free(repo_path);
4228 free(cwd);
4229 if (repo) {
4230 const struct got_error *close_err = got_repo_close(repo);
4231 if (error == NULL)
4232 error = close_err;
4234 if (worktree)
4235 got_worktree_close(worktree);
4236 if (pack_fds) {
4237 const struct got_error *pack_err =
4238 got_repo_pack_fds_close(pack_fds);
4239 if (error == NULL)
4240 error = pack_err;
4242 tog_free_refs();
4243 return error;
4246 __dead static void
4247 usage_blame(void)
4249 endwin();
4250 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4251 getprogname());
4252 exit(1);
4255 struct tog_blame_line {
4256 int annotated;
4257 struct got_object_id *id;
4260 static const struct got_error *
4261 draw_blame(struct tog_view *view)
4263 struct tog_blame_view_state *s = &view->state.blame;
4264 struct tog_blame *blame = &s->blame;
4265 regmatch_t *regmatch = &view->regmatch;
4266 const struct got_error *err;
4267 int lineno = 0, nprinted = 0, i;
4268 char *line = NULL;
4269 size_t linesize = 0;
4270 ssize_t linelen;
4271 wchar_t *wline;
4272 int width;
4273 struct tog_blame_line *blame_line;
4274 struct got_object_id *prev_id = NULL;
4275 char *id_str;
4276 struct tog_color *tc;
4278 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4279 if (err)
4280 return err;
4282 rewind(blame->f);
4283 werase(view->window);
4285 if (asprintf(&line, "commit %s", id_str) == -1) {
4286 err = got_error_from_errno("asprintf");
4287 free(id_str);
4288 return err;
4291 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4292 free(line);
4293 line = NULL;
4294 if (err)
4295 return err;
4296 if (view_needs_focus_indication(view))
4297 wstandout(view->window);
4298 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4299 if (tc)
4300 wattr_on(view->window,
4301 COLOR_PAIR(tc->colorpair), NULL);
4302 waddwstr(view->window, wline);
4303 if (tc)
4304 wattr_off(view->window,
4305 COLOR_PAIR(tc->colorpair), NULL);
4306 if (view_needs_focus_indication(view))
4307 wstandend(view->window);
4308 free(wline);
4309 wline = NULL;
4310 if (width < view->ncols - 1)
4311 waddch(view->window, '\n');
4313 if (asprintf(&line, "[%d/%d] %s%s",
4314 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4315 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4316 free(id_str);
4317 return got_error_from_errno("asprintf");
4319 free(id_str);
4320 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4321 free(line);
4322 line = NULL;
4323 if (err)
4324 return err;
4325 waddwstr(view->window, wline);
4326 free(wline);
4327 wline = NULL;
4328 if (width < view->ncols - 1)
4329 waddch(view->window, '\n');
4331 s->eof = 0;
4332 view->maxx = 0;
4333 while (nprinted < view->nlines - 2) {
4334 linelen = getline(&line, &linesize, blame->f);
4335 if (linelen == -1) {
4336 if (feof(blame->f)) {
4337 s->eof = 1;
4338 break;
4340 free(line);
4341 return got_ferror(blame->f, GOT_ERR_IO);
4343 if (++lineno < s->first_displayed_line)
4344 continue;
4346 view->maxx = MAX(view->maxx, linelen);
4348 if (view->focussed && nprinted == s->selected_line - 1)
4349 wstandout(view->window);
4351 if (blame->nlines > 0) {
4352 blame_line = &blame->lines[lineno - 1];
4353 if (blame_line->annotated && prev_id &&
4354 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4355 !(view->focussed &&
4356 nprinted == s->selected_line - 1)) {
4357 waddstr(view->window, " ");
4358 } else if (blame_line->annotated) {
4359 char *id_str;
4360 err = got_object_id_str(&id_str, blame_line->id);
4361 if (err) {
4362 free(line);
4363 return err;
4365 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4366 if (tc)
4367 wattr_on(view->window,
4368 COLOR_PAIR(tc->colorpair), NULL);
4369 wprintw(view->window, "%.8s", id_str);
4370 if (tc)
4371 wattr_off(view->window,
4372 COLOR_PAIR(tc->colorpair), NULL);
4373 free(id_str);
4374 prev_id = blame_line->id;
4375 } else {
4376 waddstr(view->window, "........");
4377 prev_id = NULL;
4379 } else {
4380 waddstr(view->window, "........");
4381 prev_id = NULL;
4384 if (view->focussed && nprinted == s->selected_line - 1)
4385 wstandend(view->window);
4386 waddstr(view->window, " ");
4388 if (view->ncols <= 9) {
4389 width = 9;
4390 wline = wcsdup(L"");
4391 if (wline == NULL) {
4392 err = got_error_from_errno("wcsdup");
4393 free(line);
4394 return err;
4396 } else if (s->first_displayed_line + nprinted ==
4397 s->matched_line &&
4398 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4399 err = add_matched_line(&width, line, view->ncols - 9, 9,
4400 view->window, view->x, regmatch);
4401 if (err) {
4402 free(line);
4403 return err;
4405 width += 9;
4406 } else {
4407 err = format_line(&wline, &width, NULL, line, 0,
4408 view->x + view->ncols - 9, 9, 1);
4409 if (err) {
4410 free(line);
4411 return err;
4413 if (view->x < width) {
4414 waddwstr(view->window, wline + view->x);
4415 for (i = 0; i < view->x; i++)
4416 width -= wcwidth(wline[i]);
4418 width += 9;
4419 free(wline);
4420 wline = NULL;
4423 if (width <= view->ncols - 1)
4424 waddch(view->window, '\n');
4425 if (++nprinted == 1)
4426 s->first_displayed_line = lineno;
4428 free(line);
4429 s->last_displayed_line = lineno;
4431 view_vborder(view);
4433 return NULL;
4436 static const struct got_error *
4437 blame_cb(void *arg, int nlines, int lineno,
4438 struct got_commit_object *commit, struct got_object_id *id)
4440 const struct got_error *err = NULL;
4441 struct tog_blame_cb_args *a = arg;
4442 struct tog_blame_line *line;
4443 int errcode;
4445 if (nlines != a->nlines ||
4446 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4447 return got_error(GOT_ERR_RANGE);
4449 errcode = pthread_mutex_lock(&tog_mutex);
4450 if (errcode)
4451 return got_error_set_errno(errcode, "pthread_mutex_lock");
4453 if (*a->quit) { /* user has quit the blame view */
4454 err = got_error(GOT_ERR_ITER_COMPLETED);
4455 goto done;
4458 if (lineno == -1)
4459 goto done; /* no change in this commit */
4461 line = &a->lines[lineno - 1];
4462 if (line->annotated)
4463 goto done;
4465 line->id = got_object_id_dup(id);
4466 if (line->id == NULL) {
4467 err = got_error_from_errno("got_object_id_dup");
4468 goto done;
4470 line->annotated = 1;
4471 done:
4472 errcode = pthread_mutex_unlock(&tog_mutex);
4473 if (errcode)
4474 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4475 return err;
4478 static void *
4479 blame_thread(void *arg)
4481 const struct got_error *err, *close_err;
4482 struct tog_blame_thread_args *ta = arg;
4483 struct tog_blame_cb_args *a = ta->cb_args;
4484 int errcode;
4486 err = block_signals_used_by_main_thread();
4487 if (err)
4488 return (void *)err;
4490 err = got_blame(ta->path, a->commit_id, ta->repo,
4491 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4492 if (err && err->code == GOT_ERR_CANCELLED)
4493 err = NULL;
4495 errcode = pthread_mutex_lock(&tog_mutex);
4496 if (errcode)
4497 return (void *)got_error_set_errno(errcode,
4498 "pthread_mutex_lock");
4500 close_err = got_repo_close(ta->repo);
4501 if (err == NULL)
4502 err = close_err;
4503 ta->repo = NULL;
4504 *ta->complete = 1;
4506 errcode = pthread_mutex_unlock(&tog_mutex);
4507 if (errcode && err == NULL)
4508 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4510 return (void *)err;
4513 static struct got_object_id *
4514 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4515 int first_displayed_line, int selected_line)
4517 struct tog_blame_line *line;
4519 if (nlines <= 0)
4520 return NULL;
4522 line = &lines[first_displayed_line - 1 + selected_line - 1];
4523 if (!line->annotated)
4524 return NULL;
4526 return line->id;
4529 static const struct got_error *
4530 stop_blame(struct tog_blame *blame)
4532 const struct got_error *err = NULL;
4533 int i;
4535 if (blame->thread) {
4536 int errcode;
4537 errcode = pthread_mutex_unlock(&tog_mutex);
4538 if (errcode)
4539 return got_error_set_errno(errcode,
4540 "pthread_mutex_unlock");
4541 errcode = pthread_join(blame->thread, (void **)&err);
4542 if (errcode)
4543 return got_error_set_errno(errcode, "pthread_join");
4544 errcode = pthread_mutex_lock(&tog_mutex);
4545 if (errcode)
4546 return got_error_set_errno(errcode,
4547 "pthread_mutex_lock");
4548 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4549 err = NULL;
4550 blame->thread = 0; //NULL;
4552 if (blame->thread_args.repo) {
4553 const struct got_error *close_err;
4554 close_err = got_repo_close(blame->thread_args.repo);
4555 if (err == NULL)
4556 err = close_err;
4557 blame->thread_args.repo = NULL;
4559 if (blame->f) {
4560 if (fclose(blame->f) == EOF && err == NULL)
4561 err = got_error_from_errno("fclose");
4562 blame->f = NULL;
4564 if (blame->lines) {
4565 for (i = 0; i < blame->nlines; i++)
4566 free(blame->lines[i].id);
4567 free(blame->lines);
4568 blame->lines = NULL;
4570 free(blame->cb_args.commit_id);
4571 blame->cb_args.commit_id = NULL;
4572 if (blame->pack_fds) {
4573 const struct got_error *pack_err =
4574 got_repo_pack_fds_close(blame->pack_fds);
4575 if (err == NULL)
4576 err = pack_err;
4577 blame->pack_fds = NULL;
4579 return err;
4582 static const struct got_error *
4583 cancel_blame_view(void *arg)
4585 const struct got_error *err = NULL;
4586 int *done = arg;
4587 int errcode;
4589 errcode = pthread_mutex_lock(&tog_mutex);
4590 if (errcode)
4591 return got_error_set_errno(errcode,
4592 "pthread_mutex_unlock");
4594 if (*done)
4595 err = got_error(GOT_ERR_CANCELLED);
4597 errcode = pthread_mutex_unlock(&tog_mutex);
4598 if (errcode)
4599 return got_error_set_errno(errcode,
4600 "pthread_mutex_lock");
4602 return err;
4605 static const struct got_error *
4606 run_blame(struct tog_view *view)
4608 struct tog_blame_view_state *s = &view->state.blame;
4609 struct tog_blame *blame = &s->blame;
4610 const struct got_error *err = NULL;
4611 struct got_commit_object *commit = NULL;
4612 struct got_blob_object *blob = NULL;
4613 struct got_repository *thread_repo = NULL;
4614 struct got_object_id *obj_id = NULL;
4615 int obj_type;
4616 int *pack_fds = NULL;
4618 err = got_object_open_as_commit(&commit, s->repo,
4619 &s->blamed_commit->id);
4620 if (err)
4621 return err;
4623 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4624 if (err)
4625 goto done;
4627 err = got_object_get_type(&obj_type, s->repo, obj_id);
4628 if (err)
4629 goto done;
4631 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4632 err = got_error(GOT_ERR_OBJ_TYPE);
4633 goto done;
4636 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4637 if (err)
4638 goto done;
4639 blame->f = got_opentemp();
4640 if (blame->f == NULL) {
4641 err = got_error_from_errno("got_opentemp");
4642 goto done;
4644 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4645 &blame->line_offsets, blame->f, blob);
4646 if (err)
4647 goto done;
4648 if (blame->nlines == 0) {
4649 s->blame_complete = 1;
4650 goto done;
4653 /* Don't include \n at EOF in the blame line count. */
4654 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4655 blame->nlines--;
4657 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4658 if (blame->lines == NULL) {
4659 err = got_error_from_errno("calloc");
4660 goto done;
4663 err = got_repo_pack_fds_open(&pack_fds);
4664 if (err)
4665 goto done;
4666 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4667 pack_fds);
4668 if (err)
4669 goto done;
4671 blame->pack_fds = pack_fds;
4672 blame->cb_args.view = view;
4673 blame->cb_args.lines = blame->lines;
4674 blame->cb_args.nlines = blame->nlines;
4675 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4676 if (blame->cb_args.commit_id == NULL) {
4677 err = got_error_from_errno("got_object_id_dup");
4678 goto done;
4680 blame->cb_args.quit = &s->done;
4682 blame->thread_args.path = s->path;
4683 blame->thread_args.repo = thread_repo;
4684 blame->thread_args.cb_args = &blame->cb_args;
4685 blame->thread_args.complete = &s->blame_complete;
4686 blame->thread_args.cancel_cb = cancel_blame_view;
4687 blame->thread_args.cancel_arg = &s->done;
4688 s->blame_complete = 0;
4690 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4691 s->first_displayed_line = 1;
4692 s->last_displayed_line = view->nlines;
4693 s->selected_line = 1;
4695 s->matched_line = 0;
4697 done:
4698 if (commit)
4699 got_object_commit_close(commit);
4700 if (blob)
4701 got_object_blob_close(blob);
4702 free(obj_id);
4703 if (err)
4704 stop_blame(blame);
4705 return err;
4708 static const struct got_error *
4709 open_blame_view(struct tog_view *view, char *path,
4710 struct got_object_id *commit_id, struct got_repository *repo)
4712 const struct got_error *err = NULL;
4713 struct tog_blame_view_state *s = &view->state.blame;
4715 STAILQ_INIT(&s->blamed_commits);
4717 s->path = strdup(path);
4718 if (s->path == NULL)
4719 return got_error_from_errno("strdup");
4721 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4722 if (err) {
4723 free(s->path);
4724 return err;
4727 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4728 s->first_displayed_line = 1;
4729 s->last_displayed_line = view->nlines;
4730 s->selected_line = 1;
4731 s->blame_complete = 0;
4732 s->repo = repo;
4733 s->commit_id = commit_id;
4734 memset(&s->blame, 0, sizeof(s->blame));
4736 STAILQ_INIT(&s->colors);
4737 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4738 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4739 get_color_value("TOG_COLOR_COMMIT"));
4740 if (err)
4741 return err;
4744 view->show = show_blame_view;
4745 view->input = input_blame_view;
4746 view->close = close_blame_view;
4747 view->search_start = search_start_blame_view;
4748 view->search_next = search_next_blame_view;
4750 return run_blame(view);
4753 static const struct got_error *
4754 close_blame_view(struct tog_view *view)
4756 const struct got_error *err = NULL;
4757 struct tog_blame_view_state *s = &view->state.blame;
4759 if (s->blame.thread)
4760 err = stop_blame(&s->blame);
4762 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4763 struct got_object_qid *blamed_commit;
4764 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4765 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4766 got_object_qid_free(blamed_commit);
4769 free(s->path);
4770 free_colors(&s->colors);
4771 return err;
4774 static const struct got_error *
4775 search_start_blame_view(struct tog_view *view)
4777 struct tog_blame_view_state *s = &view->state.blame;
4779 s->matched_line = 0;
4780 return NULL;
4783 static const struct got_error *
4784 search_next_blame_view(struct tog_view *view)
4786 struct tog_blame_view_state *s = &view->state.blame;
4787 const struct got_error *err = NULL;
4788 int lineno;
4789 char *exstr = NULL, *line = NULL;
4790 size_t linesize = 0;
4791 ssize_t linelen;
4793 if (!view->searching) {
4794 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4795 return NULL;
4798 if (s->matched_line) {
4799 if (view->searching == TOG_SEARCH_FORWARD)
4800 lineno = s->matched_line + 1;
4801 else
4802 lineno = s->matched_line - 1;
4803 } else
4804 lineno = s->first_displayed_line - 1 + s->selected_line;
4806 while (1) {
4807 off_t offset;
4809 if (lineno <= 0 || lineno > s->blame.nlines) {
4810 if (s->matched_line == 0) {
4811 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4812 break;
4815 if (view->searching == TOG_SEARCH_FORWARD)
4816 lineno = 1;
4817 else
4818 lineno = s->blame.nlines;
4821 offset = s->blame.line_offsets[lineno - 1];
4822 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4823 free(line);
4824 return got_error_from_errno("fseeko");
4826 linelen = getline(&line, &linesize, s->blame.f);
4827 err = expand_tab(&exstr, line);
4828 if (err)
4829 break;
4830 if (linelen != -1 &&
4831 match_line(exstr, &view->regex, 1, &view->regmatch)) {
4832 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4833 s->matched_line = lineno;
4834 break;
4836 free(exstr);
4837 exstr = NULL;
4838 if (view->searching == TOG_SEARCH_FORWARD)
4839 lineno++;
4840 else
4841 lineno--;
4843 free(line);
4844 free(exstr);
4846 if (s->matched_line) {
4847 s->first_displayed_line = s->matched_line;
4848 s->selected_line = 1;
4851 return err;
4854 static const struct got_error *
4855 show_blame_view(struct tog_view *view)
4857 const struct got_error *err = NULL;
4858 struct tog_blame_view_state *s = &view->state.blame;
4859 int errcode;
4861 if (s->blame.thread == 0 && !s->blame_complete) {
4862 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4863 &s->blame.thread_args);
4864 if (errcode)
4865 return got_error_set_errno(errcode, "pthread_create");
4867 halfdelay(1); /* fast refresh while annotating */
4870 if (s->blame_complete)
4871 halfdelay(10); /* disable fast refresh */
4873 err = draw_blame(view);
4875 view_vborder(view);
4876 return err;
4879 static const struct got_error *
4880 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4882 const struct got_error *err = NULL, *thread_err = NULL;
4883 struct tog_view *diff_view;
4884 struct tog_blame_view_state *s = &view->state.blame;
4885 int begin_x = 0, nscroll = view->nlines - 2;
4887 switch (ch) {
4888 case '0':
4889 view->x = 0;
4890 break;
4891 case '$':
4892 view->x = MAX(view->maxx - view->ncols / 3, 0);
4893 break;
4894 case KEY_RIGHT:
4895 case 'l':
4896 if (view->x + view->ncols / 3 < view->maxx)
4897 view->x += 2; /* move two columns right */
4898 break;
4899 case KEY_LEFT:
4900 case 'h':
4901 view->x -= MIN(view->x, 2); /* move two columns back */
4902 break;
4903 case 'q':
4904 s->done = 1;
4905 break;
4906 case 'g':
4907 case KEY_HOME:
4908 s->selected_line = 1;
4909 s->first_displayed_line = 1;
4910 break;
4911 case 'G':
4912 case KEY_END:
4913 if (s->blame.nlines < view->nlines - 2) {
4914 s->selected_line = s->blame.nlines;
4915 s->first_displayed_line = 1;
4916 } else {
4917 s->selected_line = view->nlines - 2;
4918 s->first_displayed_line = s->blame.nlines -
4919 (view->nlines - 3);
4921 break;
4922 case 'k':
4923 case KEY_UP:
4924 case CTRL('p'):
4925 if (s->selected_line > 1)
4926 s->selected_line--;
4927 else if (s->selected_line == 1 &&
4928 s->first_displayed_line > 1)
4929 s->first_displayed_line--;
4930 break;
4931 case CTRL('u'):
4932 case 'u':
4933 nscroll /= 2;
4934 /* FALL THROUGH */
4935 case KEY_PPAGE:
4936 case CTRL('b'):
4937 if (s->first_displayed_line == 1) {
4938 s->selected_line = MAX(1, s->selected_line - nscroll);
4939 break;
4941 if (s->first_displayed_line > nscroll)
4942 s->first_displayed_line -= nscroll;
4943 else
4944 s->first_displayed_line = 1;
4945 break;
4946 case 'j':
4947 case KEY_DOWN:
4948 case CTRL('n'):
4949 if (s->selected_line < view->nlines - 2 &&
4950 s->first_displayed_line +
4951 s->selected_line <= s->blame.nlines)
4952 s->selected_line++;
4953 else if (s->last_displayed_line <
4954 s->blame.nlines)
4955 s->first_displayed_line++;
4956 break;
4957 case 'b':
4958 case 'p': {
4959 struct got_object_id *id = NULL;
4960 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4961 s->first_displayed_line, s->selected_line);
4962 if (id == NULL)
4963 break;
4964 if (ch == 'p') {
4965 struct got_commit_object *commit, *pcommit;
4966 struct got_object_qid *pid;
4967 struct got_object_id *blob_id = NULL;
4968 int obj_type;
4969 err = got_object_open_as_commit(&commit,
4970 s->repo, id);
4971 if (err)
4972 break;
4973 pid = STAILQ_FIRST(
4974 got_object_commit_get_parent_ids(commit));
4975 if (pid == NULL) {
4976 got_object_commit_close(commit);
4977 break;
4979 /* Check if path history ends here. */
4980 err = got_object_open_as_commit(&pcommit,
4981 s->repo, &pid->id);
4982 if (err)
4983 break;
4984 err = got_object_id_by_path(&blob_id, s->repo,
4985 pcommit, s->path);
4986 got_object_commit_close(pcommit);
4987 if (err) {
4988 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4989 err = NULL;
4990 got_object_commit_close(commit);
4991 break;
4993 err = got_object_get_type(&obj_type, s->repo,
4994 blob_id);
4995 free(blob_id);
4996 /* Can't blame non-blob type objects. */
4997 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4998 got_object_commit_close(commit);
4999 break;
5001 err = got_object_qid_alloc(&s->blamed_commit,
5002 &pid->id);
5003 got_object_commit_close(commit);
5004 } else {
5005 if (got_object_id_cmp(id,
5006 &s->blamed_commit->id) == 0)
5007 break;
5008 err = got_object_qid_alloc(&s->blamed_commit,
5009 id);
5011 if (err)
5012 break;
5013 s->done = 1;
5014 thread_err = stop_blame(&s->blame);
5015 s->done = 0;
5016 if (thread_err)
5017 break;
5018 STAILQ_INSERT_HEAD(&s->blamed_commits,
5019 s->blamed_commit, entry);
5020 err = run_blame(view);
5021 if (err)
5022 break;
5023 break;
5025 case 'B': {
5026 struct got_object_qid *first;
5027 first = STAILQ_FIRST(&s->blamed_commits);
5028 if (!got_object_id_cmp(&first->id, s->commit_id))
5029 break;
5030 s->done = 1;
5031 thread_err = stop_blame(&s->blame);
5032 s->done = 0;
5033 if (thread_err)
5034 break;
5035 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5036 got_object_qid_free(s->blamed_commit);
5037 s->blamed_commit =
5038 STAILQ_FIRST(&s->blamed_commits);
5039 err = run_blame(view);
5040 if (err)
5041 break;
5042 break;
5044 case KEY_ENTER:
5045 case '\r': {
5046 struct got_object_id *id = NULL;
5047 struct got_object_qid *pid;
5048 struct got_commit_object *commit = NULL;
5049 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5050 s->first_displayed_line, s->selected_line);
5051 if (id == NULL)
5052 break;
5053 err = got_object_open_as_commit(&commit, s->repo, id);
5054 if (err)
5055 break;
5056 pid = STAILQ_FIRST(
5057 got_object_commit_get_parent_ids(commit));
5058 if (view_is_parent_view(view))
5059 begin_x = view_split_begin_x(view->begin_x);
5060 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5061 if (diff_view == NULL) {
5062 got_object_commit_close(commit);
5063 err = got_error_from_errno("view_open");
5064 break;
5066 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5067 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5068 got_object_commit_close(commit);
5069 if (err) {
5070 view_close(diff_view);
5071 break;
5073 view->focussed = 0;
5074 diff_view->focussed = 1;
5075 if (view_is_parent_view(view)) {
5076 err = view_close_child(view);
5077 if (err)
5078 break;
5079 view_set_child(view, diff_view);
5080 view->focus_child = 1;
5081 } else
5082 *new_view = diff_view;
5083 if (err)
5084 break;
5085 break;
5087 case CTRL('d'):
5088 case 'd':
5089 nscroll /= 2;
5090 /* FALL THROUGH */
5091 case KEY_NPAGE:
5092 case CTRL('f'):
5093 case ' ':
5094 if (s->last_displayed_line >= s->blame.nlines &&
5095 s->selected_line >= MIN(s->blame.nlines,
5096 view->nlines - 2)) {
5097 break;
5099 if (s->last_displayed_line >= s->blame.nlines &&
5100 s->selected_line < view->nlines - 2) {
5101 s->selected_line +=
5102 MIN(nscroll, s->last_displayed_line -
5103 s->first_displayed_line - s->selected_line + 1);
5105 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5106 s->first_displayed_line += nscroll;
5107 else
5108 s->first_displayed_line =
5109 s->blame.nlines - (view->nlines - 3);
5110 break;
5111 case KEY_RESIZE:
5112 if (s->selected_line > view->nlines - 2) {
5113 s->selected_line = MIN(s->blame.nlines,
5114 view->nlines - 2);
5116 break;
5117 default:
5118 break;
5120 return thread_err ? thread_err : err;
5123 static const struct got_error *
5124 cmd_blame(int argc, char *argv[])
5126 const struct got_error *error;
5127 struct got_repository *repo = NULL;
5128 struct got_worktree *worktree = NULL;
5129 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5130 char *link_target = NULL;
5131 struct got_object_id *commit_id = NULL;
5132 struct got_commit_object *commit = NULL;
5133 char *commit_id_str = NULL;
5134 int ch;
5135 struct tog_view *view;
5136 int *pack_fds = NULL;
5138 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5139 switch (ch) {
5140 case 'c':
5141 commit_id_str = optarg;
5142 break;
5143 case 'r':
5144 repo_path = realpath(optarg, NULL);
5145 if (repo_path == NULL)
5146 return got_error_from_errno2("realpath",
5147 optarg);
5148 break;
5149 default:
5150 usage_blame();
5151 /* NOTREACHED */
5155 argc -= optind;
5156 argv += optind;
5158 if (argc != 1)
5159 usage_blame();
5161 error = got_repo_pack_fds_open(&pack_fds);
5162 if (error != NULL)
5163 goto done;
5165 if (repo_path == NULL) {
5166 cwd = getcwd(NULL, 0);
5167 if (cwd == NULL)
5168 return got_error_from_errno("getcwd");
5169 error = got_worktree_open(&worktree, cwd);
5170 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5171 goto done;
5172 if (worktree)
5173 repo_path =
5174 strdup(got_worktree_get_repo_path(worktree));
5175 else
5176 repo_path = strdup(cwd);
5177 if (repo_path == NULL) {
5178 error = got_error_from_errno("strdup");
5179 goto done;
5183 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5184 if (error != NULL)
5185 goto done;
5187 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5188 worktree);
5189 if (error)
5190 goto done;
5192 init_curses();
5194 error = apply_unveil(got_repo_get_path(repo), NULL);
5195 if (error)
5196 goto done;
5198 error = tog_load_refs(repo, 0);
5199 if (error)
5200 goto done;
5202 if (commit_id_str == NULL) {
5203 struct got_reference *head_ref;
5204 error = got_ref_open(&head_ref, repo, worktree ?
5205 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5206 if (error != NULL)
5207 goto done;
5208 error = got_ref_resolve(&commit_id, repo, head_ref);
5209 got_ref_close(head_ref);
5210 } else {
5211 error = got_repo_match_object_id(&commit_id, NULL,
5212 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5214 if (error != NULL)
5215 goto done;
5217 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5218 if (view == NULL) {
5219 error = got_error_from_errno("view_open");
5220 goto done;
5223 error = got_object_open_as_commit(&commit, repo, commit_id);
5224 if (error)
5225 goto done;
5227 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5228 commit, repo);
5229 if (error)
5230 goto done;
5232 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5233 commit_id, repo);
5234 if (error)
5235 goto done;
5236 if (worktree) {
5237 /* Release work tree lock. */
5238 got_worktree_close(worktree);
5239 worktree = NULL;
5241 error = view_loop(view);
5242 done:
5243 free(repo_path);
5244 free(in_repo_path);
5245 free(link_target);
5246 free(cwd);
5247 free(commit_id);
5248 if (commit)
5249 got_object_commit_close(commit);
5250 if (worktree)
5251 got_worktree_close(worktree);
5252 if (repo) {
5253 const struct got_error *close_err = got_repo_close(repo);
5254 if (error == NULL)
5255 error = close_err;
5257 if (pack_fds) {
5258 const struct got_error *pack_err =
5259 got_repo_pack_fds_close(pack_fds);
5260 if (error == NULL)
5261 error = pack_err;
5263 tog_free_refs();
5264 return error;
5267 static const struct got_error *
5268 draw_tree_entries(struct tog_view *view, const char *parent_path)
5270 struct tog_tree_view_state *s = &view->state.tree;
5271 const struct got_error *err = NULL;
5272 struct got_tree_entry *te;
5273 wchar_t *wline;
5274 struct tog_color *tc;
5275 int width, n, i, nentries;
5276 int limit = view->nlines;
5278 s->ndisplayed = 0;
5280 werase(view->window);
5282 if (limit == 0)
5283 return NULL;
5285 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5286 0, 0);
5287 if (err)
5288 return err;
5289 if (view_needs_focus_indication(view))
5290 wstandout(view->window);
5291 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5292 if (tc)
5293 wattr_on(view->window,
5294 COLOR_PAIR(tc->colorpair), NULL);
5295 waddwstr(view->window, wline);
5296 if (tc)
5297 wattr_off(view->window,
5298 COLOR_PAIR(tc->colorpair), NULL);
5299 if (view_needs_focus_indication(view))
5300 wstandend(view->window);
5301 free(wline);
5302 wline = NULL;
5303 if (width < view->ncols - 1)
5304 waddch(view->window, '\n');
5305 if (--limit <= 0)
5306 return NULL;
5307 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5308 0, 0);
5309 if (err)
5310 return err;
5311 waddwstr(view->window, wline);
5312 free(wline);
5313 wline = NULL;
5314 if (width < view->ncols - 1)
5315 waddch(view->window, '\n');
5316 if (--limit <= 0)
5317 return NULL;
5318 waddch(view->window, '\n');
5319 if (--limit <= 0)
5320 return NULL;
5322 if (s->first_displayed_entry == NULL) {
5323 te = got_object_tree_get_first_entry(s->tree);
5324 if (s->selected == 0) {
5325 if (view->focussed)
5326 wstandout(view->window);
5327 s->selected_entry = NULL;
5329 waddstr(view->window, " ..\n"); /* parent directory */
5330 if (s->selected == 0 && view->focussed)
5331 wstandend(view->window);
5332 s->ndisplayed++;
5333 if (--limit <= 0)
5334 return NULL;
5335 n = 1;
5336 } else {
5337 n = 0;
5338 te = s->first_displayed_entry;
5341 nentries = got_object_tree_get_nentries(s->tree);
5342 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5343 char *line = NULL, *id_str = NULL, *link_target = NULL;
5344 const char *modestr = "";
5345 mode_t mode;
5347 te = got_object_tree_get_entry(s->tree, i);
5348 mode = got_tree_entry_get_mode(te);
5350 if (s->show_ids) {
5351 err = got_object_id_str(&id_str,
5352 got_tree_entry_get_id(te));
5353 if (err)
5354 return got_error_from_errno(
5355 "got_object_id_str");
5357 if (got_object_tree_entry_is_submodule(te))
5358 modestr = "$";
5359 else if (S_ISLNK(mode)) {
5360 int i;
5362 err = got_tree_entry_get_symlink_target(&link_target,
5363 te, s->repo);
5364 if (err) {
5365 free(id_str);
5366 return err;
5368 for (i = 0; i < strlen(link_target); i++) {
5369 if (!isprint((unsigned char)link_target[i]))
5370 link_target[i] = '?';
5372 modestr = "@";
5374 else if (S_ISDIR(mode))
5375 modestr = "/";
5376 else if (mode & S_IXUSR)
5377 modestr = "*";
5378 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5379 got_tree_entry_get_name(te), modestr,
5380 link_target ? " -> ": "",
5381 link_target ? link_target : "") == -1) {
5382 free(id_str);
5383 free(link_target);
5384 return got_error_from_errno("asprintf");
5386 free(id_str);
5387 free(link_target);
5388 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5389 0, 0);
5390 if (err) {
5391 free(line);
5392 break;
5394 if (n == s->selected) {
5395 if (view->focussed)
5396 wstandout(view->window);
5397 s->selected_entry = te;
5399 tc = match_color(&s->colors, line);
5400 if (tc)
5401 wattr_on(view->window,
5402 COLOR_PAIR(tc->colorpair), NULL);
5403 waddwstr(view->window, wline);
5404 if (tc)
5405 wattr_off(view->window,
5406 COLOR_PAIR(tc->colorpair), NULL);
5407 if (width < view->ncols - 1)
5408 waddch(view->window, '\n');
5409 if (n == s->selected && view->focussed)
5410 wstandend(view->window);
5411 free(line);
5412 free(wline);
5413 wline = NULL;
5414 n++;
5415 s->ndisplayed++;
5416 s->last_displayed_entry = te;
5417 if (--limit <= 0)
5418 break;
5421 return err;
5424 static void
5425 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5427 struct got_tree_entry *te;
5428 int isroot = s->tree == s->root;
5429 int i = 0;
5431 if (s->first_displayed_entry == NULL)
5432 return;
5434 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5435 while (i++ < maxscroll) {
5436 if (te == NULL) {
5437 if (!isroot)
5438 s->first_displayed_entry = NULL;
5439 break;
5441 s->first_displayed_entry = te;
5442 te = got_tree_entry_get_prev(s->tree, te);
5446 static void
5447 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5449 struct got_tree_entry *next, *last;
5450 int n = 0;
5452 if (s->first_displayed_entry)
5453 next = got_tree_entry_get_next(s->tree,
5454 s->first_displayed_entry);
5455 else
5456 next = got_object_tree_get_first_entry(s->tree);
5458 last = s->last_displayed_entry;
5459 while (next && last && n++ < maxscroll) {
5460 last = got_tree_entry_get_next(s->tree, last);
5461 if (last) {
5462 s->first_displayed_entry = next;
5463 next = got_tree_entry_get_next(s->tree, next);
5468 static const struct got_error *
5469 tree_entry_path(char **path, struct tog_parent_trees *parents,
5470 struct got_tree_entry *te)
5472 const struct got_error *err = NULL;
5473 struct tog_parent_tree *pt;
5474 size_t len = 2; /* for leading slash and NUL */
5476 TAILQ_FOREACH(pt, parents, entry)
5477 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5478 + 1 /* slash */;
5479 if (te)
5480 len += strlen(got_tree_entry_get_name(te));
5482 *path = calloc(1, len);
5483 if (path == NULL)
5484 return got_error_from_errno("calloc");
5486 (*path)[0] = '/';
5487 pt = TAILQ_LAST(parents, tog_parent_trees);
5488 while (pt) {
5489 const char *name = got_tree_entry_get_name(pt->selected_entry);
5490 if (strlcat(*path, name, len) >= len) {
5491 err = got_error(GOT_ERR_NO_SPACE);
5492 goto done;
5494 if (strlcat(*path, "/", len) >= len) {
5495 err = got_error(GOT_ERR_NO_SPACE);
5496 goto done;
5498 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5500 if (te) {
5501 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5502 err = got_error(GOT_ERR_NO_SPACE);
5503 goto done;
5506 done:
5507 if (err) {
5508 free(*path);
5509 *path = NULL;
5511 return err;
5514 static const struct got_error *
5515 blame_tree_entry(struct tog_view **new_view, int begin_x,
5516 struct got_tree_entry *te, struct tog_parent_trees *parents,
5517 struct got_object_id *commit_id, struct got_repository *repo)
5519 const struct got_error *err = NULL;
5520 char *path;
5521 struct tog_view *blame_view;
5523 *new_view = NULL;
5525 err = tree_entry_path(&path, parents, te);
5526 if (err)
5527 return err;
5529 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5530 if (blame_view == NULL) {
5531 err = got_error_from_errno("view_open");
5532 goto done;
5535 err = open_blame_view(blame_view, path, commit_id, repo);
5536 if (err) {
5537 if (err->code == GOT_ERR_CANCELLED)
5538 err = NULL;
5539 view_close(blame_view);
5540 } else
5541 *new_view = blame_view;
5542 done:
5543 free(path);
5544 return err;
5547 static const struct got_error *
5548 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5549 struct tog_tree_view_state *s)
5551 struct tog_view *log_view;
5552 const struct got_error *err = NULL;
5553 char *path;
5555 *new_view = NULL;
5557 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5558 if (log_view == NULL)
5559 return got_error_from_errno("view_open");
5561 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5562 if (err)
5563 return err;
5565 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5566 path, 0);
5567 if (err)
5568 view_close(log_view);
5569 else
5570 *new_view = log_view;
5571 free(path);
5572 return err;
5575 static const struct got_error *
5576 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5577 const char *head_ref_name, struct got_repository *repo)
5579 const struct got_error *err = NULL;
5580 char *commit_id_str = NULL;
5581 struct tog_tree_view_state *s = &view->state.tree;
5582 struct got_commit_object *commit = NULL;
5584 TAILQ_INIT(&s->parents);
5585 STAILQ_INIT(&s->colors);
5587 s->commit_id = got_object_id_dup(commit_id);
5588 if (s->commit_id == NULL)
5589 return got_error_from_errno("got_object_id_dup");
5591 err = got_object_open_as_commit(&commit, repo, commit_id);
5592 if (err)
5593 goto done;
5596 * The root is opened here and will be closed when the view is closed.
5597 * Any visited subtrees and their path-wise parents are opened and
5598 * closed on demand.
5600 err = got_object_open_as_tree(&s->root, repo,
5601 got_object_commit_get_tree_id(commit));
5602 if (err)
5603 goto done;
5604 s->tree = s->root;
5606 err = got_object_id_str(&commit_id_str, commit_id);
5607 if (err != NULL)
5608 goto done;
5610 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5611 err = got_error_from_errno("asprintf");
5612 goto done;
5615 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5616 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5617 if (head_ref_name) {
5618 s->head_ref_name = strdup(head_ref_name);
5619 if (s->head_ref_name == NULL) {
5620 err = got_error_from_errno("strdup");
5621 goto done;
5624 s->repo = repo;
5626 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5627 err = add_color(&s->colors, "\\$$",
5628 TOG_COLOR_TREE_SUBMODULE,
5629 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5630 if (err)
5631 goto done;
5632 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5633 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5634 if (err)
5635 goto done;
5636 err = add_color(&s->colors, "/$",
5637 TOG_COLOR_TREE_DIRECTORY,
5638 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5639 if (err)
5640 goto done;
5642 err = add_color(&s->colors, "\\*$",
5643 TOG_COLOR_TREE_EXECUTABLE,
5644 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5645 if (err)
5646 goto done;
5648 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5649 get_color_value("TOG_COLOR_COMMIT"));
5650 if (err)
5651 goto done;
5654 view->show = show_tree_view;
5655 view->input = input_tree_view;
5656 view->close = close_tree_view;
5657 view->search_start = search_start_tree_view;
5658 view->search_next = search_next_tree_view;
5659 done:
5660 free(commit_id_str);
5661 if (commit)
5662 got_object_commit_close(commit);
5663 if (err)
5664 close_tree_view(view);
5665 return err;
5668 static const struct got_error *
5669 close_tree_view(struct tog_view *view)
5671 struct tog_tree_view_state *s = &view->state.tree;
5673 free_colors(&s->colors);
5674 free(s->tree_label);
5675 s->tree_label = NULL;
5676 free(s->commit_id);
5677 s->commit_id = NULL;
5678 free(s->head_ref_name);
5679 s->head_ref_name = NULL;
5680 while (!TAILQ_EMPTY(&s->parents)) {
5681 struct tog_parent_tree *parent;
5682 parent = TAILQ_FIRST(&s->parents);
5683 TAILQ_REMOVE(&s->parents, parent, entry);
5684 if (parent->tree != s->root)
5685 got_object_tree_close(parent->tree);
5686 free(parent);
5689 if (s->tree != NULL && s->tree != s->root)
5690 got_object_tree_close(s->tree);
5691 if (s->root)
5692 got_object_tree_close(s->root);
5693 return NULL;
5696 static const struct got_error *
5697 search_start_tree_view(struct tog_view *view)
5699 struct tog_tree_view_state *s = &view->state.tree;
5701 s->matched_entry = NULL;
5702 return NULL;
5705 static int
5706 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5708 regmatch_t regmatch;
5710 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5711 0) == 0;
5714 static const struct got_error *
5715 search_next_tree_view(struct tog_view *view)
5717 struct tog_tree_view_state *s = &view->state.tree;
5718 struct got_tree_entry *te = NULL;
5720 if (!view->searching) {
5721 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5722 return NULL;
5725 if (s->matched_entry) {
5726 if (view->searching == TOG_SEARCH_FORWARD) {
5727 if (s->selected_entry)
5728 te = got_tree_entry_get_next(s->tree,
5729 s->selected_entry);
5730 else
5731 te = got_object_tree_get_first_entry(s->tree);
5732 } else {
5733 if (s->selected_entry == NULL)
5734 te = got_object_tree_get_last_entry(s->tree);
5735 else
5736 te = got_tree_entry_get_prev(s->tree,
5737 s->selected_entry);
5739 } else {
5740 if (s->selected_entry)
5741 te = s->selected_entry;
5742 else if (view->searching == TOG_SEARCH_FORWARD)
5743 te = got_object_tree_get_first_entry(s->tree);
5744 else
5745 te = got_object_tree_get_last_entry(s->tree);
5748 while (1) {
5749 if (te == NULL) {
5750 if (s->matched_entry == NULL) {
5751 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5752 return NULL;
5754 if (view->searching == TOG_SEARCH_FORWARD)
5755 te = got_object_tree_get_first_entry(s->tree);
5756 else
5757 te = got_object_tree_get_last_entry(s->tree);
5760 if (match_tree_entry(te, &view->regex)) {
5761 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5762 s->matched_entry = te;
5763 break;
5766 if (view->searching == TOG_SEARCH_FORWARD)
5767 te = got_tree_entry_get_next(s->tree, te);
5768 else
5769 te = got_tree_entry_get_prev(s->tree, te);
5772 if (s->matched_entry) {
5773 s->first_displayed_entry = s->matched_entry;
5774 s->selected = 0;
5777 return NULL;
5780 static const struct got_error *
5781 show_tree_view(struct tog_view *view)
5783 const struct got_error *err = NULL;
5784 struct tog_tree_view_state *s = &view->state.tree;
5785 char *parent_path;
5787 err = tree_entry_path(&parent_path, &s->parents, NULL);
5788 if (err)
5789 return err;
5791 err = draw_tree_entries(view, parent_path);
5792 free(parent_path);
5794 view_vborder(view);
5795 return err;
5798 static const struct got_error *
5799 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5801 const struct got_error *err = NULL;
5802 struct tog_tree_view_state *s = &view->state.tree;
5803 struct tog_view *log_view, *ref_view;
5804 struct got_tree_entry *te;
5805 int begin_x = 0, n, nscroll = view->nlines - 3;
5807 switch (ch) {
5808 case 'i':
5809 s->show_ids = !s->show_ids;
5810 break;
5811 case 'l':
5812 if (!s->selected_entry)
5813 break;
5814 if (view_is_parent_view(view))
5815 begin_x = view_split_begin_x(view->begin_x);
5816 err = log_selected_tree_entry(&log_view, begin_x, s);
5817 view->focussed = 0;
5818 log_view->focussed = 1;
5819 if (view_is_parent_view(view)) {
5820 err = view_close_child(view);
5821 if (err)
5822 return err;
5823 view_set_child(view, log_view);
5824 view->focus_child = 1;
5825 } else
5826 *new_view = log_view;
5827 break;
5828 case 'r':
5829 if (view_is_parent_view(view))
5830 begin_x = view_split_begin_x(view->begin_x);
5831 ref_view = view_open(view->nlines, view->ncols,
5832 view->begin_y, begin_x, TOG_VIEW_REF);
5833 if (ref_view == NULL)
5834 return got_error_from_errno("view_open");
5835 err = open_ref_view(ref_view, s->repo);
5836 if (err) {
5837 view_close(ref_view);
5838 return err;
5840 view->focussed = 0;
5841 ref_view->focussed = 1;
5842 if (view_is_parent_view(view)) {
5843 err = view_close_child(view);
5844 if (err)
5845 return err;
5846 view_set_child(view, ref_view);
5847 view->focus_child = 1;
5848 } else
5849 *new_view = ref_view;
5850 break;
5851 case 'g':
5852 case KEY_HOME:
5853 s->selected = 0;
5854 if (s->tree == s->root)
5855 s->first_displayed_entry =
5856 got_object_tree_get_first_entry(s->tree);
5857 else
5858 s->first_displayed_entry = NULL;
5859 break;
5860 case 'G':
5861 case KEY_END:
5862 s->selected = 0;
5863 te = got_object_tree_get_last_entry(s->tree);
5864 for (n = 0; n < view->nlines - 3; n++) {
5865 if (te == NULL) {
5866 if(s->tree != s->root) {
5867 s->first_displayed_entry = NULL;
5868 n++;
5870 break;
5872 s->first_displayed_entry = te;
5873 te = got_tree_entry_get_prev(s->tree, te);
5875 if (n > 0)
5876 s->selected = n - 1;
5877 break;
5878 case 'k':
5879 case KEY_UP:
5880 case CTRL('p'):
5881 if (s->selected > 0) {
5882 s->selected--;
5883 break;
5885 tree_scroll_up(s, 1);
5886 break;
5887 case CTRL('u'):
5888 case 'u':
5889 nscroll /= 2;
5890 /* FALL THROUGH */
5891 case KEY_PPAGE:
5892 case CTRL('b'):
5893 if (s->tree == s->root) {
5894 if (got_object_tree_get_first_entry(s->tree) ==
5895 s->first_displayed_entry)
5896 s->selected -= MIN(s->selected, nscroll);
5897 } else {
5898 if (s->first_displayed_entry == NULL)
5899 s->selected -= MIN(s->selected, nscroll);
5901 tree_scroll_up(s, MAX(0, nscroll));
5902 break;
5903 case 'j':
5904 case KEY_DOWN:
5905 case CTRL('n'):
5906 if (s->selected < s->ndisplayed - 1) {
5907 s->selected++;
5908 break;
5910 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5911 == NULL)
5912 /* can't scroll any further */
5913 break;
5914 tree_scroll_down(s, 1);
5915 break;
5916 case CTRL('d'):
5917 case 'd':
5918 nscroll /= 2;
5919 /* FALL THROUGH */
5920 case KEY_NPAGE:
5921 case CTRL('f'):
5922 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5923 == NULL) {
5924 /* can't scroll any further; move cursor down */
5925 if (s->selected < s->ndisplayed - 1)
5926 s->selected += MIN(nscroll,
5927 s->ndisplayed - s->selected - 1);
5928 break;
5930 tree_scroll_down(s, nscroll);
5931 break;
5932 case KEY_ENTER:
5933 case '\r':
5934 case KEY_BACKSPACE:
5935 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5936 struct tog_parent_tree *parent;
5937 /* user selected '..' */
5938 if (s->tree == s->root)
5939 break;
5940 parent = TAILQ_FIRST(&s->parents);
5941 TAILQ_REMOVE(&s->parents, parent,
5942 entry);
5943 got_object_tree_close(s->tree);
5944 s->tree = parent->tree;
5945 s->first_displayed_entry =
5946 parent->first_displayed_entry;
5947 s->selected_entry =
5948 parent->selected_entry;
5949 s->selected = parent->selected;
5950 free(parent);
5951 } else if (S_ISDIR(got_tree_entry_get_mode(
5952 s->selected_entry))) {
5953 struct got_tree_object *subtree;
5954 err = got_object_open_as_tree(&subtree, s->repo,
5955 got_tree_entry_get_id(s->selected_entry));
5956 if (err)
5957 break;
5958 err = tree_view_visit_subtree(s, subtree);
5959 if (err) {
5960 got_object_tree_close(subtree);
5961 break;
5963 } else if (S_ISREG(got_tree_entry_get_mode(
5964 s->selected_entry))) {
5965 struct tog_view *blame_view;
5966 int begin_x = view_is_parent_view(view) ?
5967 view_split_begin_x(view->begin_x) : 0;
5969 err = blame_tree_entry(&blame_view, begin_x,
5970 s->selected_entry, &s->parents,
5971 s->commit_id, s->repo);
5972 if (err)
5973 break;
5974 view->focussed = 0;
5975 blame_view->focussed = 1;
5976 if (view_is_parent_view(view)) {
5977 err = view_close_child(view);
5978 if (err)
5979 return err;
5980 view_set_child(view, blame_view);
5981 view->focus_child = 1;
5982 } else
5983 *new_view = blame_view;
5985 break;
5986 case KEY_RESIZE:
5987 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5988 s->selected = view->nlines - 4;
5989 break;
5990 default:
5991 break;
5994 return err;
5997 __dead static void
5998 usage_tree(void)
6000 endwin();
6001 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6002 getprogname());
6003 exit(1);
6006 static const struct got_error *
6007 cmd_tree(int argc, char *argv[])
6009 const struct got_error *error;
6010 struct got_repository *repo = NULL;
6011 struct got_worktree *worktree = NULL;
6012 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6013 struct got_object_id *commit_id = NULL;
6014 struct got_commit_object *commit = NULL;
6015 const char *commit_id_arg = NULL;
6016 char *label = NULL;
6017 struct got_reference *ref = NULL;
6018 const char *head_ref_name = NULL;
6019 int ch;
6020 struct tog_view *view;
6021 int *pack_fds = NULL;
6023 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6024 switch (ch) {
6025 case 'c':
6026 commit_id_arg = optarg;
6027 break;
6028 case 'r':
6029 repo_path = realpath(optarg, NULL);
6030 if (repo_path == NULL)
6031 return got_error_from_errno2("realpath",
6032 optarg);
6033 break;
6034 default:
6035 usage_tree();
6036 /* NOTREACHED */
6040 argc -= optind;
6041 argv += optind;
6043 if (argc > 1)
6044 usage_tree();
6046 error = got_repo_pack_fds_open(&pack_fds);
6047 if (error != NULL)
6048 goto done;
6050 if (repo_path == NULL) {
6051 cwd = getcwd(NULL, 0);
6052 if (cwd == NULL)
6053 return got_error_from_errno("getcwd");
6054 error = got_worktree_open(&worktree, cwd);
6055 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6056 goto done;
6057 if (worktree)
6058 repo_path =
6059 strdup(got_worktree_get_repo_path(worktree));
6060 else
6061 repo_path = strdup(cwd);
6062 if (repo_path == NULL) {
6063 error = got_error_from_errno("strdup");
6064 goto done;
6068 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6069 if (error != NULL)
6070 goto done;
6072 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6073 repo, worktree);
6074 if (error)
6075 goto done;
6077 init_curses();
6079 error = apply_unveil(got_repo_get_path(repo), NULL);
6080 if (error)
6081 goto done;
6083 error = tog_load_refs(repo, 0);
6084 if (error)
6085 goto done;
6087 if (commit_id_arg == NULL) {
6088 error = got_repo_match_object_id(&commit_id, &label,
6089 worktree ? got_worktree_get_head_ref_name(worktree) :
6090 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6091 if (error)
6092 goto done;
6093 head_ref_name = label;
6094 } else {
6095 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6096 if (error == NULL)
6097 head_ref_name = got_ref_get_name(ref);
6098 else if (error->code != GOT_ERR_NOT_REF)
6099 goto done;
6100 error = got_repo_match_object_id(&commit_id, NULL,
6101 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6102 if (error)
6103 goto done;
6106 error = got_object_open_as_commit(&commit, repo, commit_id);
6107 if (error)
6108 goto done;
6110 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6111 if (view == NULL) {
6112 error = got_error_from_errno("view_open");
6113 goto done;
6115 error = open_tree_view(view, commit_id, head_ref_name, repo);
6116 if (error)
6117 goto done;
6118 if (!got_path_is_root_dir(in_repo_path)) {
6119 error = tree_view_walk_path(&view->state.tree, commit,
6120 in_repo_path);
6121 if (error)
6122 goto done;
6125 if (worktree) {
6126 /* Release work tree lock. */
6127 got_worktree_close(worktree);
6128 worktree = NULL;
6130 error = view_loop(view);
6131 done:
6132 free(repo_path);
6133 free(cwd);
6134 free(commit_id);
6135 free(label);
6136 if (ref)
6137 got_ref_close(ref);
6138 if (repo) {
6139 const struct got_error *close_err = got_repo_close(repo);
6140 if (error == NULL)
6141 error = close_err;
6143 if (pack_fds) {
6144 const struct got_error *pack_err =
6145 got_repo_pack_fds_close(pack_fds);
6146 if (error == NULL)
6147 error = pack_err;
6149 tog_free_refs();
6150 return error;
6153 static const struct got_error *
6154 ref_view_load_refs(struct tog_ref_view_state *s)
6156 struct got_reflist_entry *sre;
6157 struct tog_reflist_entry *re;
6159 s->nrefs = 0;
6160 TAILQ_FOREACH(sre, &tog_refs, entry) {
6161 if (strncmp(got_ref_get_name(sre->ref),
6162 "refs/got/", 9) == 0 &&
6163 strncmp(got_ref_get_name(sre->ref),
6164 "refs/got/backup/", 16) != 0)
6165 continue;
6167 re = malloc(sizeof(*re));
6168 if (re == NULL)
6169 return got_error_from_errno("malloc");
6171 re->ref = got_ref_dup(sre->ref);
6172 if (re->ref == NULL)
6173 return got_error_from_errno("got_ref_dup");
6174 re->idx = s->nrefs++;
6175 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6178 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6179 return NULL;
6182 void
6183 ref_view_free_refs(struct tog_ref_view_state *s)
6185 struct tog_reflist_entry *re;
6187 while (!TAILQ_EMPTY(&s->refs)) {
6188 re = TAILQ_FIRST(&s->refs);
6189 TAILQ_REMOVE(&s->refs, re, entry);
6190 got_ref_close(re->ref);
6191 free(re);
6195 static const struct got_error *
6196 open_ref_view(struct tog_view *view, struct got_repository *repo)
6198 const struct got_error *err = NULL;
6199 struct tog_ref_view_state *s = &view->state.ref;
6201 s->selected_entry = 0;
6202 s->repo = repo;
6204 TAILQ_INIT(&s->refs);
6205 STAILQ_INIT(&s->colors);
6207 err = ref_view_load_refs(s);
6208 if (err)
6209 return err;
6211 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6212 err = add_color(&s->colors, "^refs/heads/",
6213 TOG_COLOR_REFS_HEADS,
6214 get_color_value("TOG_COLOR_REFS_HEADS"));
6215 if (err)
6216 goto done;
6218 err = add_color(&s->colors, "^refs/tags/",
6219 TOG_COLOR_REFS_TAGS,
6220 get_color_value("TOG_COLOR_REFS_TAGS"));
6221 if (err)
6222 goto done;
6224 err = add_color(&s->colors, "^refs/remotes/",
6225 TOG_COLOR_REFS_REMOTES,
6226 get_color_value("TOG_COLOR_REFS_REMOTES"));
6227 if (err)
6228 goto done;
6230 err = add_color(&s->colors, "^refs/got/backup/",
6231 TOG_COLOR_REFS_BACKUP,
6232 get_color_value("TOG_COLOR_REFS_BACKUP"));
6233 if (err)
6234 goto done;
6237 view->show = show_ref_view;
6238 view->input = input_ref_view;
6239 view->close = close_ref_view;
6240 view->search_start = search_start_ref_view;
6241 view->search_next = search_next_ref_view;
6242 done:
6243 if (err)
6244 free_colors(&s->colors);
6245 return err;
6248 static const struct got_error *
6249 close_ref_view(struct tog_view *view)
6251 struct tog_ref_view_state *s = &view->state.ref;
6253 ref_view_free_refs(s);
6254 free_colors(&s->colors);
6256 return NULL;
6259 static const struct got_error *
6260 resolve_reflist_entry(struct got_object_id **commit_id,
6261 struct tog_reflist_entry *re, struct got_repository *repo)
6263 const struct got_error *err = NULL;
6264 struct got_object_id *obj_id;
6265 struct got_tag_object *tag = NULL;
6266 int obj_type;
6268 *commit_id = NULL;
6270 err = got_ref_resolve(&obj_id, repo, re->ref);
6271 if (err)
6272 return err;
6274 err = got_object_get_type(&obj_type, repo, obj_id);
6275 if (err)
6276 goto done;
6278 switch (obj_type) {
6279 case GOT_OBJ_TYPE_COMMIT:
6280 *commit_id = obj_id;
6281 break;
6282 case GOT_OBJ_TYPE_TAG:
6283 err = got_object_open_as_tag(&tag, repo, obj_id);
6284 if (err)
6285 goto done;
6286 free(obj_id);
6287 err = got_object_get_type(&obj_type, repo,
6288 got_object_tag_get_object_id(tag));
6289 if (err)
6290 goto done;
6291 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6292 err = got_error(GOT_ERR_OBJ_TYPE);
6293 goto done;
6295 *commit_id = got_object_id_dup(
6296 got_object_tag_get_object_id(tag));
6297 if (*commit_id == NULL) {
6298 err = got_error_from_errno("got_object_id_dup");
6299 goto done;
6301 break;
6302 default:
6303 err = got_error(GOT_ERR_OBJ_TYPE);
6304 break;
6307 done:
6308 if (tag)
6309 got_object_tag_close(tag);
6310 if (err) {
6311 free(*commit_id);
6312 *commit_id = NULL;
6314 return err;
6317 static const struct got_error *
6318 log_ref_entry(struct tog_view **new_view, int begin_x,
6319 struct tog_reflist_entry *re, struct got_repository *repo)
6321 struct tog_view *log_view;
6322 const struct got_error *err = NULL;
6323 struct got_object_id *commit_id = NULL;
6325 *new_view = NULL;
6327 err = resolve_reflist_entry(&commit_id, re, repo);
6328 if (err) {
6329 if (err->code != GOT_ERR_OBJ_TYPE)
6330 return err;
6331 else
6332 return NULL;
6335 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6336 if (log_view == NULL) {
6337 err = got_error_from_errno("view_open");
6338 goto done;
6341 err = open_log_view(log_view, commit_id, repo,
6342 got_ref_get_name(re->ref), "", 0);
6343 done:
6344 if (err)
6345 view_close(log_view);
6346 else
6347 *new_view = log_view;
6348 free(commit_id);
6349 return err;
6352 static void
6353 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6355 struct tog_reflist_entry *re;
6356 int i = 0;
6358 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6359 return;
6361 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6362 while (i++ < maxscroll) {
6363 if (re == NULL)
6364 break;
6365 s->first_displayed_entry = re;
6366 re = TAILQ_PREV(re, tog_reflist_head, entry);
6370 static void
6371 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6373 struct tog_reflist_entry *next, *last;
6374 int n = 0;
6376 if (s->first_displayed_entry)
6377 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6378 else
6379 next = TAILQ_FIRST(&s->refs);
6381 last = s->last_displayed_entry;
6382 while (next && last && n++ < maxscroll) {
6383 last = TAILQ_NEXT(last, entry);
6384 if (last) {
6385 s->first_displayed_entry = next;
6386 next = TAILQ_NEXT(next, entry);
6391 static const struct got_error *
6392 search_start_ref_view(struct tog_view *view)
6394 struct tog_ref_view_state *s = &view->state.ref;
6396 s->matched_entry = NULL;
6397 return NULL;
6400 static int
6401 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6403 regmatch_t regmatch;
6405 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6406 0) == 0;
6409 static const struct got_error *
6410 search_next_ref_view(struct tog_view *view)
6412 struct tog_ref_view_state *s = &view->state.ref;
6413 struct tog_reflist_entry *re = NULL;
6415 if (!view->searching) {
6416 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6417 return NULL;
6420 if (s->matched_entry) {
6421 if (view->searching == TOG_SEARCH_FORWARD) {
6422 if (s->selected_entry)
6423 re = TAILQ_NEXT(s->selected_entry, entry);
6424 else
6425 re = TAILQ_PREV(s->selected_entry,
6426 tog_reflist_head, entry);
6427 } else {
6428 if (s->selected_entry == NULL)
6429 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6430 else
6431 re = TAILQ_PREV(s->selected_entry,
6432 tog_reflist_head, entry);
6434 } else {
6435 if (s->selected_entry)
6436 re = s->selected_entry;
6437 else if (view->searching == TOG_SEARCH_FORWARD)
6438 re = TAILQ_FIRST(&s->refs);
6439 else
6440 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6443 while (1) {
6444 if (re == NULL) {
6445 if (s->matched_entry == NULL) {
6446 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6447 return NULL;
6449 if (view->searching == TOG_SEARCH_FORWARD)
6450 re = TAILQ_FIRST(&s->refs);
6451 else
6452 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6455 if (match_reflist_entry(re, &view->regex)) {
6456 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6457 s->matched_entry = re;
6458 break;
6461 if (view->searching == TOG_SEARCH_FORWARD)
6462 re = TAILQ_NEXT(re, entry);
6463 else
6464 re = TAILQ_PREV(re, tog_reflist_head, entry);
6467 if (s->matched_entry) {
6468 s->first_displayed_entry = s->matched_entry;
6469 s->selected = 0;
6472 return NULL;
6475 static const struct got_error *
6476 show_ref_view(struct tog_view *view)
6478 const struct got_error *err = NULL;
6479 struct tog_ref_view_state *s = &view->state.ref;
6480 struct tog_reflist_entry *re;
6481 char *line = NULL;
6482 wchar_t *wline;
6483 struct tog_color *tc;
6484 int width, n;
6485 int limit = view->nlines;
6487 werase(view->window);
6489 s->ndisplayed = 0;
6491 if (limit == 0)
6492 return NULL;
6494 re = s->first_displayed_entry;
6496 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6497 s->nrefs) == -1)
6498 return got_error_from_errno("asprintf");
6500 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6501 if (err) {
6502 free(line);
6503 return err;
6505 if (view_needs_focus_indication(view))
6506 wstandout(view->window);
6507 waddwstr(view->window, wline);
6508 if (view_needs_focus_indication(view))
6509 wstandend(view->window);
6510 free(wline);
6511 wline = NULL;
6512 free(line);
6513 line = NULL;
6514 if (width < view->ncols - 1)
6515 waddch(view->window, '\n');
6516 if (--limit <= 0)
6517 return NULL;
6519 n = 0;
6520 while (re && limit > 0) {
6521 char *line = NULL;
6522 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6524 if (s->show_date) {
6525 struct got_commit_object *ci;
6526 struct got_tag_object *tag;
6527 struct got_object_id *id;
6528 struct tm tm;
6529 time_t t;
6531 err = got_ref_resolve(&id, s->repo, re->ref);
6532 if (err)
6533 return err;
6534 err = got_object_open_as_tag(&tag, s->repo, id);
6535 if (err) {
6536 if (err->code != GOT_ERR_OBJ_TYPE) {
6537 free(id);
6538 return err;
6540 err = got_object_open_as_commit(&ci, s->repo,
6541 id);
6542 if (err) {
6543 free(id);
6544 return err;
6546 t = got_object_commit_get_committer_time(ci);
6547 got_object_commit_close(ci);
6548 } else {
6549 t = got_object_tag_get_tagger_time(tag);
6550 got_object_tag_close(tag);
6552 free(id);
6553 if (gmtime_r(&t, &tm) == NULL)
6554 return got_error_from_errno("gmtime_r");
6555 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6556 return got_error(GOT_ERR_NO_SPACE);
6558 if (got_ref_is_symbolic(re->ref)) {
6559 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6560 ymd : "", got_ref_get_name(re->ref),
6561 got_ref_get_symref_target(re->ref)) == -1)
6562 return got_error_from_errno("asprintf");
6563 } else if (s->show_ids) {
6564 struct got_object_id *id;
6565 char *id_str;
6566 err = got_ref_resolve(&id, s->repo, re->ref);
6567 if (err)
6568 return err;
6569 err = got_object_id_str(&id_str, id);
6570 if (err) {
6571 free(id);
6572 return err;
6574 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6575 got_ref_get_name(re->ref), id_str) == -1) {
6576 err = got_error_from_errno("asprintf");
6577 free(id);
6578 free(id_str);
6579 return err;
6581 free(id);
6582 free(id_str);
6583 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6584 got_ref_get_name(re->ref)) == -1)
6585 return got_error_from_errno("asprintf");
6587 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6588 0, 0);
6589 if (err) {
6590 free(line);
6591 return err;
6593 if (n == s->selected) {
6594 if (view->focussed)
6595 wstandout(view->window);
6596 s->selected_entry = re;
6598 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6599 if (tc)
6600 wattr_on(view->window,
6601 COLOR_PAIR(tc->colorpair), NULL);
6602 waddwstr(view->window, wline);
6603 if (tc)
6604 wattr_off(view->window,
6605 COLOR_PAIR(tc->colorpair), NULL);
6606 if (width < view->ncols - 1)
6607 waddch(view->window, '\n');
6608 if (n == s->selected && view->focussed)
6609 wstandend(view->window);
6610 free(line);
6611 free(wline);
6612 wline = NULL;
6613 n++;
6614 s->ndisplayed++;
6615 s->last_displayed_entry = re;
6617 limit--;
6618 re = TAILQ_NEXT(re, entry);
6621 view_vborder(view);
6622 return err;
6625 static const struct got_error *
6626 browse_ref_tree(struct tog_view **new_view, int begin_x,
6627 struct tog_reflist_entry *re, struct got_repository *repo)
6629 const struct got_error *err = NULL;
6630 struct got_object_id *commit_id = NULL;
6631 struct tog_view *tree_view;
6633 *new_view = NULL;
6635 err = resolve_reflist_entry(&commit_id, re, repo);
6636 if (err) {
6637 if (err->code != GOT_ERR_OBJ_TYPE)
6638 return err;
6639 else
6640 return NULL;
6644 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6645 if (tree_view == NULL) {
6646 err = got_error_from_errno("view_open");
6647 goto done;
6650 err = open_tree_view(tree_view, commit_id,
6651 got_ref_get_name(re->ref), repo);
6652 if (err)
6653 goto done;
6655 *new_view = tree_view;
6656 done:
6657 free(commit_id);
6658 return err;
6660 static const struct got_error *
6661 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6663 const struct got_error *err = NULL;
6664 struct tog_ref_view_state *s = &view->state.ref;
6665 struct tog_view *log_view, *tree_view;
6666 struct tog_reflist_entry *re;
6667 int begin_x = 0, n, nscroll = view->nlines - 1;
6669 switch (ch) {
6670 case 'i':
6671 s->show_ids = !s->show_ids;
6672 break;
6673 case 'm':
6674 s->show_date = !s->show_date;
6675 break;
6676 case 'o':
6677 s->sort_by_date = !s->sort_by_date;
6678 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6679 got_ref_cmp_by_commit_timestamp_descending :
6680 tog_ref_cmp_by_name, s->repo);
6681 if (err)
6682 break;
6683 got_reflist_object_id_map_free(tog_refs_idmap);
6684 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6685 &tog_refs, s->repo);
6686 if (err)
6687 break;
6688 ref_view_free_refs(s);
6689 err = ref_view_load_refs(s);
6690 break;
6691 case KEY_ENTER:
6692 case '\r':
6693 if (!s->selected_entry)
6694 break;
6695 if (view_is_parent_view(view))
6696 begin_x = view_split_begin_x(view->begin_x);
6697 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6698 s->repo);
6699 view->focussed = 0;
6700 log_view->focussed = 1;
6701 if (view_is_parent_view(view)) {
6702 err = view_close_child(view);
6703 if (err)
6704 return err;
6705 view_set_child(view, log_view);
6706 view->focus_child = 1;
6707 } else
6708 *new_view = log_view;
6709 break;
6710 case 't':
6711 if (!s->selected_entry)
6712 break;
6713 if (view_is_parent_view(view))
6714 begin_x = view_split_begin_x(view->begin_x);
6715 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6716 s->repo);
6717 if (err || tree_view == NULL)
6718 break;
6719 view->focussed = 0;
6720 tree_view->focussed = 1;
6721 if (view_is_parent_view(view)) {
6722 err = view_close_child(view);
6723 if (err)
6724 return err;
6725 view_set_child(view, tree_view);
6726 view->focus_child = 1;
6727 } else
6728 *new_view = tree_view;
6729 break;
6730 case 'g':
6731 case KEY_HOME:
6732 s->selected = 0;
6733 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6734 break;
6735 case 'G':
6736 case KEY_END:
6737 s->selected = 0;
6738 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6739 for (n = 0; n < view->nlines - 1; n++) {
6740 if (re == NULL)
6741 break;
6742 s->first_displayed_entry = re;
6743 re = TAILQ_PREV(re, tog_reflist_head, entry);
6745 if (n > 0)
6746 s->selected = n - 1;
6747 break;
6748 case 'k':
6749 case KEY_UP:
6750 case CTRL('p'):
6751 if (s->selected > 0) {
6752 s->selected--;
6753 break;
6755 ref_scroll_up(s, 1);
6756 break;
6757 case CTRL('u'):
6758 case 'u':
6759 nscroll /= 2;
6760 /* FALL THROUGH */
6761 case KEY_PPAGE:
6762 case CTRL('b'):
6763 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6764 s->selected -= MIN(nscroll, s->selected);
6765 ref_scroll_up(s, MAX(0, nscroll));
6766 break;
6767 case 'j':
6768 case KEY_DOWN:
6769 case CTRL('n'):
6770 if (s->selected < s->ndisplayed - 1) {
6771 s->selected++;
6772 break;
6774 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6775 /* can't scroll any further */
6776 break;
6777 ref_scroll_down(s, 1);
6778 break;
6779 case CTRL('d'):
6780 case 'd':
6781 nscroll /= 2;
6782 /* FALL THROUGH */
6783 case KEY_NPAGE:
6784 case CTRL('f'):
6785 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6786 /* can't scroll any further; move cursor down */
6787 if (s->selected < s->ndisplayed - 1)
6788 s->selected += MIN(nscroll,
6789 s->ndisplayed - s->selected - 1);
6790 break;
6792 ref_scroll_down(s, nscroll);
6793 break;
6794 case CTRL('l'):
6795 tog_free_refs();
6796 err = tog_load_refs(s->repo, s->sort_by_date);
6797 if (err)
6798 break;
6799 ref_view_free_refs(s);
6800 err = ref_view_load_refs(s);
6801 break;
6802 case KEY_RESIZE:
6803 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6804 s->selected = view->nlines - 2;
6805 break;
6806 default:
6807 break;
6810 return err;
6813 __dead static void
6814 usage_ref(void)
6816 endwin();
6817 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6818 getprogname());
6819 exit(1);
6822 static const struct got_error *
6823 cmd_ref(int argc, char *argv[])
6825 const struct got_error *error;
6826 struct got_repository *repo = NULL;
6827 struct got_worktree *worktree = NULL;
6828 char *cwd = NULL, *repo_path = NULL;
6829 int ch;
6830 struct tog_view *view;
6831 int *pack_fds = NULL;
6833 while ((ch = getopt(argc, argv, "r:")) != -1) {
6834 switch (ch) {
6835 case 'r':
6836 repo_path = realpath(optarg, NULL);
6837 if (repo_path == NULL)
6838 return got_error_from_errno2("realpath",
6839 optarg);
6840 break;
6841 default:
6842 usage_ref();
6843 /* NOTREACHED */
6847 argc -= optind;
6848 argv += optind;
6850 if (argc > 1)
6851 usage_ref();
6853 error = got_repo_pack_fds_open(&pack_fds);
6854 if (error != NULL)
6855 goto done;
6857 if (repo_path == NULL) {
6858 cwd = getcwd(NULL, 0);
6859 if (cwd == NULL)
6860 return got_error_from_errno("getcwd");
6861 error = got_worktree_open(&worktree, cwd);
6862 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6863 goto done;
6864 if (worktree)
6865 repo_path =
6866 strdup(got_worktree_get_repo_path(worktree));
6867 else
6868 repo_path = strdup(cwd);
6869 if (repo_path == NULL) {
6870 error = got_error_from_errno("strdup");
6871 goto done;
6875 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6876 if (error != NULL)
6877 goto done;
6879 init_curses();
6881 error = apply_unveil(got_repo_get_path(repo), NULL);
6882 if (error)
6883 goto done;
6885 error = tog_load_refs(repo, 0);
6886 if (error)
6887 goto done;
6889 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6890 if (view == NULL) {
6891 error = got_error_from_errno("view_open");
6892 goto done;
6895 error = open_ref_view(view, repo);
6896 if (error)
6897 goto done;
6899 if (worktree) {
6900 /* Release work tree lock. */
6901 got_worktree_close(worktree);
6902 worktree = NULL;
6904 error = view_loop(view);
6905 done:
6906 free(repo_path);
6907 free(cwd);
6908 if (repo) {
6909 const struct got_error *close_err = got_repo_close(repo);
6910 if (close_err)
6911 error = close_err;
6913 if (pack_fds) {
6914 const struct got_error *pack_err =
6915 got_repo_pack_fds_close(pack_fds);
6916 if (error == NULL)
6917 error = pack_err;
6919 tog_free_refs();
6920 return error;
6923 static void
6924 list_commands(FILE *fp)
6926 size_t i;
6928 fprintf(fp, "commands:");
6929 for (i = 0; i < nitems(tog_commands); i++) {
6930 const struct tog_cmd *cmd = &tog_commands[i];
6931 fprintf(fp, " %s", cmd->name);
6933 fputc('\n', fp);
6936 __dead static void
6937 usage(int hflag, int status)
6939 FILE *fp = (status == 0) ? stdout : stderr;
6941 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6942 getprogname());
6943 if (hflag) {
6944 fprintf(fp, "lazy usage: %s path\n", getprogname());
6945 list_commands(fp);
6947 exit(status);
6950 static char **
6951 make_argv(int argc, ...)
6953 va_list ap;
6954 char **argv;
6955 int i;
6957 va_start(ap, argc);
6959 argv = calloc(argc, sizeof(char *));
6960 if (argv == NULL)
6961 err(1, "calloc");
6962 for (i = 0; i < argc; i++) {
6963 argv[i] = strdup(va_arg(ap, char *));
6964 if (argv[i] == NULL)
6965 err(1, "strdup");
6968 va_end(ap);
6969 return argv;
6973 * Try to convert 'tog path' into a 'tog log path' command.
6974 * The user could simply have mistyped the command rather than knowingly
6975 * provided a path. So check whether argv[0] can in fact be resolved
6976 * to a path in the HEAD commit and print a special error if not.
6977 * This hack is for mpi@ <3
6979 static const struct got_error *
6980 tog_log_with_path(int argc, char *argv[])
6982 const struct got_error *error = NULL, *close_err;
6983 const struct tog_cmd *cmd = NULL;
6984 struct got_repository *repo = NULL;
6985 struct got_worktree *worktree = NULL;
6986 struct got_object_id *commit_id = NULL, *id = NULL;
6987 struct got_commit_object *commit = NULL;
6988 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6989 char *commit_id_str = NULL, **cmd_argv = NULL;
6990 int *pack_fds = NULL;
6992 cwd = getcwd(NULL, 0);
6993 if (cwd == NULL)
6994 return got_error_from_errno("getcwd");
6996 error = got_repo_pack_fds_open(&pack_fds);
6997 if (error != NULL)
6998 goto done;
7000 error = got_worktree_open(&worktree, cwd);
7001 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7002 goto done;
7004 if (worktree)
7005 repo_path = strdup(got_worktree_get_repo_path(worktree));
7006 else
7007 repo_path = strdup(cwd);
7008 if (repo_path == NULL) {
7009 error = got_error_from_errno("strdup");
7010 goto done;
7013 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7014 if (error != NULL)
7015 goto done;
7017 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7018 repo, worktree);
7019 if (error)
7020 goto done;
7022 error = tog_load_refs(repo, 0);
7023 if (error)
7024 goto done;
7025 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7026 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7027 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7028 if (error)
7029 goto done;
7031 if (worktree) {
7032 got_worktree_close(worktree);
7033 worktree = NULL;
7036 error = got_object_open_as_commit(&commit, repo, commit_id);
7037 if (error)
7038 goto done;
7040 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7041 if (error) {
7042 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7043 goto done;
7044 fprintf(stderr, "%s: '%s' is no known command or path\n",
7045 getprogname(), argv[0]);
7046 usage(1, 1);
7047 /* not reached */
7050 close_err = got_repo_close(repo);
7051 if (error == NULL)
7052 error = close_err;
7053 repo = NULL;
7055 error = got_object_id_str(&commit_id_str, commit_id);
7056 if (error)
7057 goto done;
7059 cmd = &tog_commands[0]; /* log */
7060 argc = 4;
7061 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7062 error = cmd->cmd_main(argc, cmd_argv);
7063 done:
7064 if (repo) {
7065 close_err = got_repo_close(repo);
7066 if (error == NULL)
7067 error = close_err;
7069 if (commit)
7070 got_object_commit_close(commit);
7071 if (worktree)
7072 got_worktree_close(worktree);
7073 if (pack_fds) {
7074 const struct got_error *pack_err =
7075 got_repo_pack_fds_close(pack_fds);
7076 if (error == NULL)
7077 error = pack_err;
7079 free(id);
7080 free(commit_id_str);
7081 free(commit_id);
7082 free(cwd);
7083 free(repo_path);
7084 free(in_repo_path);
7085 if (cmd_argv) {
7086 int i;
7087 for (i = 0; i < argc; i++)
7088 free(cmd_argv[i]);
7089 free(cmd_argv);
7091 tog_free_refs();
7092 return error;
7095 int
7096 main(int argc, char *argv[])
7098 const struct got_error *error = NULL;
7099 const struct tog_cmd *cmd = NULL;
7100 int ch, hflag = 0, Vflag = 0;
7101 char **cmd_argv = NULL;
7102 static const struct option longopts[] = {
7103 { "version", no_argument, NULL, 'V' },
7104 { NULL, 0, NULL, 0}
7107 setlocale(LC_CTYPE, "");
7109 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7110 switch (ch) {
7111 case 'h':
7112 hflag = 1;
7113 break;
7114 case 'V':
7115 Vflag = 1;
7116 break;
7117 default:
7118 usage(hflag, 1);
7119 /* NOTREACHED */
7123 argc -= optind;
7124 argv += optind;
7125 optind = 1;
7126 optreset = 1;
7128 if (Vflag) {
7129 got_version_print_str();
7130 return 0;
7133 #ifndef PROFILE
7134 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7135 NULL) == -1)
7136 err(1, "pledge");
7137 #endif
7139 if (argc == 0) {
7140 if (hflag)
7141 usage(hflag, 0);
7142 /* Build an argument vector which runs a default command. */
7143 cmd = &tog_commands[0];
7144 argc = 1;
7145 cmd_argv = make_argv(argc, cmd->name);
7146 } else {
7147 size_t i;
7149 /* Did the user specify a command? */
7150 for (i = 0; i < nitems(tog_commands); i++) {
7151 if (strncmp(tog_commands[i].name, argv[0],
7152 strlen(argv[0])) == 0) {
7153 cmd = &tog_commands[i];
7154 break;
7159 if (cmd == NULL) {
7160 if (argc != 1)
7161 usage(0, 1);
7162 /* No command specified; try log with a path */
7163 error = tog_log_with_path(argc, argv);
7164 } else {
7165 if (hflag)
7166 cmd->cmd_usage();
7167 else
7168 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7171 endwin();
7172 putchar('\n');
7173 if (cmd_argv) {
7174 int i;
7175 for (i = 0; i < argc; i++)
7176 free(cmd_argv[i]);
7177 free(cmd_argv);
7180 if (error && error->code != GOT_ERR_CANCELLED)
7181 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7182 return 0;