Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #include <curses.h>
25 #include <panel.h>
26 #include <locale.h>
27 #include <sha1.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_diff.h"
50 #include "got_opentemp.h"
51 #include "got_utf8.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_path.h"
57 #include "got_worktree.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #ifndef MAX
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #endif
67 #define CTRL(x) ((x) & 0x1f)
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 struct tog_cmd {
74 const char *name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 };
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
98 };
100 enum tog_view_type {
101 TOG_VIEW_DIFF,
102 TOG_VIEW_LOG,
103 TOG_VIEW_BLAME,
104 TOG_VIEW_TREE,
105 TOG_VIEW_REF,
106 };
108 enum tog_view_mode {
109 TOG_VIEW_SPLIT_NONE,
110 TOG_VIEW_SPLIT_VERT,
111 TOG_VIEW_SPLIT_HRZN
112 };
114 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 #define TOG_EOF_STRING "(END)"
118 struct commit_queue_entry {
119 TAILQ_ENTRY(commit_queue_entry) entry;
120 struct got_object_id *id;
121 struct got_commit_object *commit;
122 int idx;
123 };
124 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 struct commit_queue {
126 int ncommits;
127 struct commit_queue_head head;
128 };
130 struct tog_color {
131 STAILQ_ENTRY(tog_color) entry;
132 regex_t regex;
133 short colorpair;
134 };
135 STAILQ_HEAD(tog_colors, tog_color);
137 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 static const struct got_error *
142 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 struct got_reference* re2)
145 const char *name1 = got_ref_get_name(re1);
146 const char *name2 = got_ref_get_name(re2);
147 int isbackup1, isbackup2;
149 /* Sort backup refs towards the bottom of the list. */
150 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 if (!isbackup1 && isbackup2) {
153 *cmp = -1;
154 return NULL;
155 } else if (isbackup1 && !isbackup2) {
156 *cmp = 1;
157 return NULL;
160 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
161 return NULL;
164 static const struct got_error *
165 tog_load_refs(struct got_repository *repo, int sort_by_date)
167 const struct got_error *err;
169 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
171 repo);
172 if (err)
173 return err;
175 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
176 repo);
179 static void
180 tog_free_refs(void)
182 if (tog_refs_idmap) {
183 got_reflist_object_id_map_free(tog_refs_idmap);
184 tog_refs_idmap = NULL;
186 got_ref_list_free(&tog_refs);
189 static const struct got_error *
190 add_color(struct tog_colors *colors, const char *pattern,
191 int idx, short color)
193 const struct got_error *err = NULL;
194 struct tog_color *tc;
195 int regerr = 0;
197 if (idx < 1 || idx > COLOR_PAIRS - 1)
198 return NULL;
200 init_pair(idx, color, -1);
202 tc = calloc(1, sizeof(*tc));
203 if (tc == NULL)
204 return got_error_from_errno("calloc");
205 regerr = regcomp(&tc->regex, pattern,
206 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
207 if (regerr) {
208 static char regerr_msg[512];
209 static char err_msg[512];
210 regerror(regerr, &tc->regex, regerr_msg,
211 sizeof(regerr_msg));
212 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
213 regerr_msg);
214 err = got_error_msg(GOT_ERR_REGEX, err_msg);
215 free(tc);
216 return err;
218 tc->colorpair = idx;
219 STAILQ_INSERT_HEAD(colors, tc, entry);
220 return NULL;
223 static void
224 free_colors(struct tog_colors *colors)
226 struct tog_color *tc;
228 while (!STAILQ_EMPTY(colors)) {
229 tc = STAILQ_FIRST(colors);
230 STAILQ_REMOVE_HEAD(colors, entry);
231 regfree(&tc->regex);
232 free(tc);
236 static struct tog_color *
237 get_color(struct tog_colors *colors, int colorpair)
239 struct tog_color *tc = NULL;
241 STAILQ_FOREACH(tc, colors, entry) {
242 if (tc->colorpair == colorpair)
243 return tc;
246 return NULL;
249 static int
250 default_color_value(const char *envvar)
252 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 return COLOR_MAGENTA;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
255 return COLOR_CYAN;
256 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
257 return COLOR_YELLOW;
258 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
259 return COLOR_GREEN;
260 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 return COLOR_MAGENTA;
264 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
265 return COLOR_CYAN;
266 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
267 return COLOR_GREEN;
268 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
269 return COLOR_GREEN;
270 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
271 return COLOR_CYAN;
272 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
273 return COLOR_YELLOW;
274 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
275 return COLOR_GREEN;
276 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
279 return COLOR_YELLOW;
280 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
281 return COLOR_CYAN;
283 return -1;
286 static int
287 get_color_value(const char *envvar)
289 const char *val = getenv(envvar);
291 if (val == NULL)
292 return default_color_value(envvar);
294 if (strcasecmp(val, "black") == 0)
295 return COLOR_BLACK;
296 if (strcasecmp(val, "red") == 0)
297 return COLOR_RED;
298 if (strcasecmp(val, "green") == 0)
299 return COLOR_GREEN;
300 if (strcasecmp(val, "yellow") == 0)
301 return COLOR_YELLOW;
302 if (strcasecmp(val, "blue") == 0)
303 return COLOR_BLUE;
304 if (strcasecmp(val, "magenta") == 0)
305 return COLOR_MAGENTA;
306 if (strcasecmp(val, "cyan") == 0)
307 return COLOR_CYAN;
308 if (strcasecmp(val, "white") == 0)
309 return COLOR_WHITE;
310 if (strcasecmp(val, "default") == 0)
311 return -1;
313 return default_color_value(envvar);
317 struct tog_diff_view_state {
318 struct got_object_id *id1, *id2;
319 const char *label1, *label2;
320 FILE *f, *f1, *f2;
321 int fd1, fd2;
322 int first_displayed_line;
323 int last_displayed_line;
324 int eof;
325 int diff_context;
326 int ignore_whitespace;
327 int force_text_diff;
328 struct got_repository *repo;
329 struct tog_colors colors;
330 size_t nlines;
331 off_t *line_offsets;
332 int matched_line;
333 int selected_line;
335 /* passed from log view; may be NULL */
336 struct tog_view *log_view;
337 };
339 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
341 struct tog_log_thread_args {
342 pthread_cond_t need_commits;
343 pthread_cond_t commit_loaded;
344 int commits_needed;
345 int load_all;
346 struct got_commit_graph *graph;
347 struct commit_queue *commits;
348 const char *in_repo_path;
349 struct got_object_id *start_id;
350 struct got_repository *repo;
351 int *pack_fds;
352 int log_complete;
353 sig_atomic_t *quit;
354 struct commit_queue_entry **first_displayed_entry;
355 struct commit_queue_entry **selected_entry;
356 int *searching;
357 int *search_next_done;
358 regex_t *regex;
359 };
361 struct tog_log_view_state {
362 struct commit_queue commits;
363 struct commit_queue_entry *first_displayed_entry;
364 struct commit_queue_entry *last_displayed_entry;
365 struct commit_queue_entry *selected_entry;
366 int selected;
367 char *in_repo_path;
368 char *head_ref_name;
369 int log_branches;
370 struct got_repository *repo;
371 struct got_object_id *start_id;
372 sig_atomic_t quit;
373 pthread_t thread;
374 struct tog_log_thread_args thread_args;
375 struct commit_queue_entry *matched_entry;
376 struct commit_queue_entry *search_entry;
377 struct tog_colors colors;
378 };
380 #define TOG_COLOR_DIFF_MINUS 1
381 #define TOG_COLOR_DIFF_PLUS 2
382 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
383 #define TOG_COLOR_DIFF_META 4
384 #define TOG_COLOR_TREE_SUBMODULE 5
385 #define TOG_COLOR_TREE_SYMLINK 6
386 #define TOG_COLOR_TREE_DIRECTORY 7
387 #define TOG_COLOR_TREE_EXECUTABLE 8
388 #define TOG_COLOR_COMMIT 9
389 #define TOG_COLOR_AUTHOR 10
390 #define TOG_COLOR_DATE 11
391 #define TOG_COLOR_REFS_HEADS 12
392 #define TOG_COLOR_REFS_TAGS 13
393 #define TOG_COLOR_REFS_REMOTES 14
394 #define TOG_COLOR_REFS_BACKUP 15
396 struct tog_blame_cb_args {
397 struct tog_blame_line *lines; /* one per line */
398 int nlines;
400 struct tog_view *view;
401 struct got_object_id *commit_id;
402 int *quit;
403 };
405 struct tog_blame_thread_args {
406 const char *path;
407 struct got_repository *repo;
408 struct tog_blame_cb_args *cb_args;
409 int *complete;
410 got_cancel_cb cancel_cb;
411 void *cancel_arg;
412 };
414 struct tog_blame {
415 FILE *f;
416 off_t filesize;
417 struct tog_blame_line *lines;
418 int nlines;
419 off_t *line_offsets;
420 pthread_t thread;
421 struct tog_blame_thread_args thread_args;
422 struct tog_blame_cb_args cb_args;
423 const char *path;
424 int *pack_fds;
425 };
427 struct tog_blame_view_state {
428 int first_displayed_line;
429 int last_displayed_line;
430 int selected_line;
431 int blame_complete;
432 int eof;
433 int done;
434 struct got_object_id_queue blamed_commits;
435 struct got_object_qid *blamed_commit;
436 char *path;
437 struct got_repository *repo;
438 struct got_object_id *commit_id;
439 struct tog_blame blame;
440 int matched_line;
441 struct tog_colors colors;
442 };
444 struct tog_parent_tree {
445 TAILQ_ENTRY(tog_parent_tree) entry;
446 struct got_tree_object *tree;
447 struct got_tree_entry *first_displayed_entry;
448 struct got_tree_entry *selected_entry;
449 int selected;
450 };
452 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
454 struct tog_tree_view_state {
455 char *tree_label;
456 struct got_object_id *commit_id;/* commit which this tree belongs to */
457 struct got_tree_object *root; /* the commit's root tree entry */
458 struct got_tree_object *tree; /* currently displayed (sub-)tree */
459 struct got_tree_entry *first_displayed_entry;
460 struct got_tree_entry *last_displayed_entry;
461 struct got_tree_entry *selected_entry;
462 int ndisplayed, selected, show_ids;
463 struct tog_parent_trees parents; /* parent trees of current sub-tree */
464 char *head_ref_name;
465 struct got_repository *repo;
466 struct got_tree_entry *matched_entry;
467 struct tog_colors colors;
468 };
470 struct tog_reflist_entry {
471 TAILQ_ENTRY(tog_reflist_entry) entry;
472 struct got_reference *ref;
473 int idx;
474 };
476 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
478 struct tog_ref_view_state {
479 struct tog_reflist_head refs;
480 struct tog_reflist_entry *first_displayed_entry;
481 struct tog_reflist_entry *last_displayed_entry;
482 struct tog_reflist_entry *selected_entry;
483 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
484 struct got_repository *repo;
485 struct tog_reflist_entry *matched_entry;
486 struct tog_colors colors;
487 };
489 /*
490 * We implement two types of views: parent views and child views.
492 * The 'Tab' key switches focus between a parent view and its child view.
493 * Child views are shown side-by-side to their parent view, provided
494 * there is enough screen estate.
496 * When a new view is opened from within a parent view, this new view
497 * becomes a child view of the parent view, replacing any existing child.
499 * When a new view is opened from within a child view, this new view
500 * becomes a parent view which will obscure the views below until the
501 * user quits the new parent view by typing 'q'.
503 * This list of views contains parent views only.
504 * Child views are only pointed to by their parent view.
505 */
506 TAILQ_HEAD(tog_view_list_head, tog_view);
508 struct tog_view {
509 TAILQ_ENTRY(tog_view) entry;
510 WINDOW *window;
511 PANEL *panel;
512 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
513 int maxx, x; /* max column and current start column */
514 int lines, cols; /* copies of LINES and COLS */
515 int nscrolled, offset; /* lines scrolled and hsplit line offset */
516 int ch, count; /* current keymap and count prefix */
517 int focussed; /* Only set on one parent or child view at a time. */
518 int dying;
519 struct tog_view *parent;
520 struct tog_view *child;
522 /*
523 * This flag is initially set on parent views when a new child view
524 * is created. It gets toggled when the 'Tab' key switches focus
525 * between parent and child.
526 * The flag indicates whether focus should be passed on to our child
527 * view if this parent view gets picked for focus after another parent
528 * view was closed. This prevents child views from losing focus in such
529 * situations.
530 */
531 int focus_child;
533 enum tog_view_mode mode;
534 /* type-specific state */
535 enum tog_view_type type;
536 union {
537 struct tog_diff_view_state diff;
538 struct tog_log_view_state log;
539 struct tog_blame_view_state blame;
540 struct tog_tree_view_state tree;
541 struct tog_ref_view_state ref;
542 } state;
544 const struct got_error *(*show)(struct tog_view *);
545 const struct got_error *(*input)(struct tog_view **,
546 struct tog_view *, int);
547 const struct got_error *(*reset)(struct tog_view *);
548 const struct got_error *(*close)(struct tog_view *);
550 const struct got_error *(*search_start)(struct tog_view *);
551 const struct got_error *(*search_next)(struct tog_view *);
552 int search_started;
553 int searching;
554 #define TOG_SEARCH_FORWARD 1
555 #define TOG_SEARCH_BACKWARD 2
556 int search_next_done;
557 #define TOG_SEARCH_HAVE_MORE 1
558 #define TOG_SEARCH_NO_MORE 2
559 #define TOG_SEARCH_HAVE_NONE 3
560 regex_t regex;
561 regmatch_t regmatch;
562 };
564 static const struct got_error *open_diff_view(struct tog_view *,
565 struct got_object_id *, struct got_object_id *,
566 const char *, const char *, int, int, int, struct tog_view *,
567 struct got_repository *);
568 static const struct got_error *show_diff_view(struct tog_view *);
569 static const struct got_error *input_diff_view(struct tog_view **,
570 struct tog_view *, int);
571 static const struct got_error *reset_diff_view(struct tog_view *);
572 static const struct got_error* close_diff_view(struct tog_view *);
573 static const struct got_error *search_start_diff_view(struct tog_view *);
574 static const struct got_error *search_next_diff_view(struct tog_view *);
576 static const struct got_error *open_log_view(struct tog_view *,
577 struct got_object_id *, struct got_repository *,
578 const char *, const char *, int);
579 static const struct got_error * show_log_view(struct tog_view *);
580 static const struct got_error *input_log_view(struct tog_view **,
581 struct tog_view *, int);
582 static const struct got_error *close_log_view(struct tog_view *);
583 static const struct got_error *search_start_log_view(struct tog_view *);
584 static const struct got_error *search_next_log_view(struct tog_view *);
586 static const struct got_error *open_blame_view(struct tog_view *, char *,
587 struct got_object_id *, struct got_repository *);
588 static const struct got_error *show_blame_view(struct tog_view *);
589 static const struct got_error *input_blame_view(struct tog_view **,
590 struct tog_view *, int);
591 static const struct got_error *reset_blame_view(struct tog_view *);
592 static const struct got_error *close_blame_view(struct tog_view *);
593 static const struct got_error *search_start_blame_view(struct tog_view *);
594 static const struct got_error *search_next_blame_view(struct tog_view *);
596 static const struct got_error *open_tree_view(struct tog_view *,
597 struct got_object_id *, const char *, struct got_repository *);
598 static const struct got_error *show_tree_view(struct tog_view *);
599 static const struct got_error *input_tree_view(struct tog_view **,
600 struct tog_view *, int);
601 static const struct got_error *close_tree_view(struct tog_view *);
602 static const struct got_error *search_start_tree_view(struct tog_view *);
603 static const struct got_error *search_next_tree_view(struct tog_view *);
605 static const struct got_error *open_ref_view(struct tog_view *,
606 struct got_repository *);
607 static const struct got_error *show_ref_view(struct tog_view *);
608 static const struct got_error *input_ref_view(struct tog_view **,
609 struct tog_view *, int);
610 static const struct got_error *close_ref_view(struct tog_view *);
611 static const struct got_error *search_start_ref_view(struct tog_view *);
612 static const struct got_error *search_next_ref_view(struct tog_view *);
614 static volatile sig_atomic_t tog_sigwinch_received;
615 static volatile sig_atomic_t tog_sigpipe_received;
616 static volatile sig_atomic_t tog_sigcont_received;
617 static volatile sig_atomic_t tog_sigint_received;
618 static volatile sig_atomic_t tog_sigterm_received;
620 static void
621 tog_sigwinch(int signo)
623 tog_sigwinch_received = 1;
626 static void
627 tog_sigpipe(int signo)
629 tog_sigpipe_received = 1;
632 static void
633 tog_sigcont(int signo)
635 tog_sigcont_received = 1;
638 static void
639 tog_sigint(int signo)
641 tog_sigint_received = 1;
644 static void
645 tog_sigterm(int signo)
647 tog_sigterm_received = 1;
650 static int
651 tog_fatal_signal_received(void)
653 return (tog_sigpipe_received ||
654 tog_sigint_received || tog_sigint_received);
657 static const struct got_error *
658 view_close(struct tog_view *view)
660 const struct got_error *err = NULL;
662 if (view->child) {
663 view_close(view->child);
664 view->child = NULL;
666 if (view->close)
667 err = view->close(view);
668 if (view->panel)
669 del_panel(view->panel);
670 if (view->window)
671 delwin(view->window);
672 free(view);
673 return err;
676 static struct tog_view *
677 view_open(int nlines, int ncols, int begin_y, int begin_x,
678 enum tog_view_type type)
680 struct tog_view *view = calloc(1, sizeof(*view));
682 if (view == NULL)
683 return NULL;
685 view->type = type;
686 view->lines = LINES;
687 view->cols = COLS;
688 view->nlines = nlines ? nlines : LINES - begin_y;
689 view->ncols = ncols ? ncols : COLS - begin_x;
690 view->begin_y = begin_y;
691 view->begin_x = begin_x;
692 view->window = newwin(nlines, ncols, begin_y, begin_x);
693 if (view->window == NULL) {
694 view_close(view);
695 return NULL;
697 view->panel = new_panel(view->window);
698 if (view->panel == NULL ||
699 set_panel_userptr(view->panel, view) != OK) {
700 view_close(view);
701 return NULL;
704 keypad(view->window, TRUE);
705 return view;
708 static int
709 view_split_begin_x(int begin_x)
711 if (begin_x > 0 || COLS < 120)
712 return 0;
713 return (COLS - MAX(COLS / 2, 80));
716 /* XXX Stub till we decide what to do. */
717 static int
718 view_split_begin_y(int lines)
720 return lines * HSPLIT_SCALE;
723 static const struct got_error *view_resize(struct tog_view *);
725 static const struct got_error *
726 view_splitscreen(struct tog_view *view)
728 const struct got_error *err = NULL;
730 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
731 view->begin_y = view_split_begin_y(view->nlines);
732 view->begin_x = 0;
733 } else {
734 view->begin_x = view_split_begin_x(0);
735 view->begin_y = 0;
737 view->nlines = LINES - view->begin_y;
738 view->ncols = COLS - view->begin_x;
739 view->lines = LINES;
740 view->cols = COLS;
741 err = view_resize(view);
742 if (err)
743 return err;
745 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
746 view->parent->nlines = view->begin_y;
748 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
749 return got_error_from_errno("mvwin");
751 return NULL;
754 static const struct got_error *
755 view_fullscreen(struct tog_view *view)
757 const struct got_error *err = NULL;
759 view->begin_x = 0;
760 view->begin_y = 0;
761 view->nlines = LINES;
762 view->ncols = COLS;
763 view->lines = LINES;
764 view->cols = COLS;
765 err = view_resize(view);
766 if (err)
767 return err;
769 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
770 return got_error_from_errno("mvwin");
772 return NULL;
775 static int
776 view_is_parent_view(struct tog_view *view)
778 return view->parent == NULL;
781 static int
782 view_is_splitscreen(struct tog_view *view)
784 return view->begin_x > 0 || view->begin_y > 0;
787 static int
788 view_is_fullscreen(struct tog_view *view)
790 return view->nlines == LINES && view->ncols == COLS;
793 static void
794 view_border(struct tog_view *view)
796 PANEL *panel;
797 const struct tog_view *view_above;
799 if (view->parent)
800 return view_border(view->parent);
802 panel = panel_above(view->panel);
803 if (panel == NULL)
804 return;
806 view_above = panel_userptr(panel);
807 if (view->mode == TOG_VIEW_SPLIT_HRZN)
808 mvwhline(view->window, view_above->begin_y - 1,
809 view->begin_x, got_locale_is_utf8() ?
810 ACS_HLINE : '-', view->ncols);
811 else
812 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
813 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
816 static const struct got_error *request_log_commits(struct tog_view *);
817 static const struct got_error *offset_selection_down(struct tog_view *);
818 static void offset_selection_up(struct tog_view *);
820 static const struct got_error *
821 view_resize(struct tog_view *view)
823 const struct got_error *err = NULL;
824 int dif, nlines, ncols;
826 dif = LINES - view->lines; /* line difference */
828 if (view->lines > LINES)
829 nlines = view->nlines - (view->lines - LINES);
830 else
831 nlines = view->nlines + (LINES - view->lines);
832 if (view->cols > COLS)
833 ncols = view->ncols - (view->cols - COLS);
834 else
835 ncols = view->ncols + (COLS - view->cols);
837 if (view->child) {
838 int hs = view->child->begin_y;
840 if (!view_is_fullscreen(view))
841 view->child->begin_x = view_split_begin_x(view->begin_x);
842 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
843 view->child->begin_x == 0) {
844 ncols = COLS;
846 view_fullscreen(view->child);
847 if (view->child->focussed)
848 show_panel(view->child->panel);
849 else
850 show_panel(view->panel);
851 } else {
852 ncols = view->child->begin_x;
854 view_splitscreen(view->child);
855 show_panel(view->child->panel);
857 /*
858 * Request commits if terminal height was increased in a log
859 * view so we have enough commits loaded to populate the view.
860 */
861 if (view->type == TOG_VIEW_LOG && dif > 0) {
862 struct tog_log_view_state *ts = &view->state.log;
864 if (ts->commits.ncommits < ts->selected_entry->idx +
865 view->lines - ts->selected) {
866 view->nscrolled = ts->selected_entry->idx +
867 view->lines - ts->selected -
868 ts->commits.ncommits + dif;
869 err = request_log_commits(view);
870 if (err)
871 return err;
875 /*
876 * XXX This is ugly and needs to be moved into the above
877 * logic but "works" for now and my attempts at moving it
878 * break either 'tab' or 'F' key maps in horizontal splits.
879 */
880 if (hs) {
881 err = view_splitscreen(view->child);
882 if (err)
883 return err;
884 if (dif < 0) { /* top split decreased */
885 err = offset_selection_down(view);
886 if (err)
887 return err;
889 view_border(view);
890 update_panels();
891 doupdate();
892 show_panel(view->child->panel);
893 nlines = view->nlines;
895 } else if (view->parent == NULL)
896 ncols = COLS;
898 if (wresize(view->window, nlines, ncols) == ERR)
899 return got_error_from_errno("wresize");
900 if (replace_panel(view->panel, view->window) == ERR)
901 return got_error_from_errno("replace_panel");
902 wclear(view->window);
904 view->nlines = nlines;
905 view->ncols = ncols;
906 view->lines = LINES;
907 view->cols = COLS;
909 return NULL;
912 static const struct got_error *
913 view_close_child(struct tog_view *view)
915 const struct got_error *err = NULL;
917 if (view->child == NULL)
918 return NULL;
920 err = view_close(view->child);
921 view->child = NULL;
922 return err;
925 static const struct got_error *
926 view_set_child(struct tog_view *view, struct tog_view *child)
928 view->child = child;
929 child->parent = view;
931 return view_resize(view);
934 static void
935 tog_resizeterm(void)
937 int cols, lines;
938 struct winsize size;
940 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
941 cols = 80; /* Default */
942 lines = 24;
943 } else {
944 cols = size.ws_col;
945 lines = size.ws_row;
947 resize_term(lines, cols);
950 static const struct got_error *
951 view_search_start(struct tog_view *view)
953 const struct got_error *err = NULL;
954 struct tog_view *v = view;
955 char pattern[1024];
956 int ret;
958 if (view->search_started) {
959 regfree(&view->regex);
960 view->searching = 0;
961 memset(&view->regmatch, 0, sizeof(view->regmatch));
963 view->search_started = 0;
965 if (view->nlines < 1)
966 return NULL;
968 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
969 view_is_splitscreen(view->child))
970 v = view->child;
972 mvwaddstr(v->window, v->nlines - 1, 0, "/");
973 wclrtoeol(v->window);
975 nocbreak();
976 echo();
977 ret = wgetnstr(v->window, pattern, sizeof(pattern));
978 wrefresh(v->window);
979 cbreak();
980 noecho();
981 if (ret == ERR)
982 return NULL;
984 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
985 err = view->search_start(view);
986 if (err) {
987 regfree(&view->regex);
988 return err;
990 view->search_started = 1;
991 view->searching = TOG_SEARCH_FORWARD;
992 view->search_next_done = 0;
993 view->search_next(view);
996 return NULL;
999 /*
1000 * Compute view->count from numeric user input. User has five-tenths of a
1001 * second to follow each numeric keypress with another number to form count.
1002 * Return first non-numeric input or ERR and assign total to view->count.
1003 * XXX Should we add support for user-defined timeout?
1005 static int
1006 get_compound_key(struct tog_view *view, int c)
1008 struct tog_view *v = view;
1009 int x, n = 0;
1011 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
1012 view_is_splitscreen(view->child))
1013 v = view->child;
1014 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1015 v = view->parent;
1017 view->count = 0;
1018 halfdelay(5); /* block for half a second */
1019 wattron(v->window, A_BOLD);
1020 wmove(v->window, v->nlines - 1, 0);
1021 wclrtoeol(v->window);
1022 waddch(v->window, ':');
1024 do {
1025 x = getcurx(v->window);
1026 if (x != ERR && x < view->ncols) {
1027 waddch(v->window, c);
1028 wrefresh(v->window);
1032 * Don't overflow. Max valid request should be the greatest
1033 * between the longest and total lines; cap at 10 million.
1035 if (n >= 9999999)
1036 n = 9999999;
1037 else
1038 n = n * 10 + (c - '0');
1039 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1041 /* Massage excessive or inapplicable values at the input handler. */
1042 view->count = n;
1044 wattroff(v->window, A_BOLD);
1045 cbreak(); /* return to blocking */
1046 return c;
1049 static const struct got_error *
1050 view_input(struct tog_view **new, int *done, struct tog_view *view,
1051 struct tog_view_list_head *views)
1053 const struct got_error *err = NULL;
1054 struct tog_view *v;
1055 int ch, errcode;
1057 *new = NULL;
1059 /* Clear "no matches" indicator. */
1060 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1061 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1062 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1063 view->count = 0;
1066 if (view->searching && !view->search_next_done) {
1067 errcode = pthread_mutex_unlock(&tog_mutex);
1068 if (errcode)
1069 return got_error_set_errno(errcode,
1070 "pthread_mutex_unlock");
1071 sched_yield();
1072 errcode = pthread_mutex_lock(&tog_mutex);
1073 if (errcode)
1074 return got_error_set_errno(errcode,
1075 "pthread_mutex_lock");
1076 view->search_next(view);
1077 return NULL;
1080 nodelay(stdscr, FALSE);
1081 /* Allow threads to make progress while we are waiting for input. */
1082 errcode = pthread_mutex_unlock(&tog_mutex);
1083 if (errcode)
1084 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1085 /* If we have an unfinished count, don't get a new key map. */
1086 ch = view->ch;
1087 if ((view->count && --view->count == 0) || !view->count) {
1088 ch = wgetch(view->window);
1089 if (ch >= '1' && ch <= '9')
1090 view->ch = ch = get_compound_key(view, ch);
1092 errcode = pthread_mutex_lock(&tog_mutex);
1093 if (errcode)
1094 return got_error_set_errno(errcode, "pthread_mutex_lock");
1095 nodelay(stdscr, TRUE);
1097 if (tog_sigwinch_received || tog_sigcont_received) {
1098 tog_resizeterm();
1099 tog_sigwinch_received = 0;
1100 tog_sigcont_received = 0;
1101 TAILQ_FOREACH(v, views, entry) {
1102 err = view_resize(v);
1103 if (err)
1104 return err;
1105 err = v->input(new, v, KEY_RESIZE);
1106 if (err)
1107 return err;
1108 if (v->child) {
1109 err = view_resize(v->child);
1110 if (err)
1111 return err;
1112 err = v->child->input(new, v->child,
1113 KEY_RESIZE);
1114 if (err)
1115 return err;
1120 switch (ch) {
1121 case '\t':
1122 view->count = 0;
1123 if (view->child) {
1124 view->focussed = 0;
1125 view->child->focussed = 1;
1126 view->focus_child = 1;
1127 } else if (view->parent) {
1128 view->focussed = 0;
1129 view->parent->focussed = 1;
1130 view->parent->focus_child = 0;
1131 if (!view_is_splitscreen(view)) {
1132 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1133 view->parent->type == TOG_VIEW_LOG) {
1134 err = request_log_commits(view->parent);
1135 if (err)
1136 return err;
1138 offset_selection_up(view->parent);
1139 err = view_fullscreen(view->parent);
1140 if (err)
1141 return err;
1144 break;
1145 case 'q':
1146 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1147 if (view->parent->type == TOG_VIEW_LOG) {
1148 /* might need more commits to fill fullscreen */
1149 err = request_log_commits(view->parent);
1150 if (err)
1151 break;
1153 offset_selection_up(view->parent);
1154 view->parent->mode = TOG_VIEW_SPLIT_NONE;
1156 err = view->input(new, view, ch);
1157 view->dying = 1;
1158 break;
1159 case 'Q':
1160 *done = 1;
1161 break;
1162 case 'F':
1163 view->count = 0;
1164 if (view_is_parent_view(view)) {
1165 if (view->child == NULL)
1166 break;
1167 if (view_is_splitscreen(view->child)) {
1168 view->focussed = 0;
1169 view->child->focussed = 1;
1170 err = view_fullscreen(view->child);
1171 } else
1172 err = view_splitscreen(view->child);
1173 if (err)
1174 break;
1175 err = view->child->input(new, view->child,
1176 KEY_RESIZE);
1177 } else {
1178 if (view_is_splitscreen(view)) {
1179 view->parent->focussed = 0;
1180 view->focussed = 1;
1181 err = view_fullscreen(view);
1182 } else {
1183 err = view_splitscreen(view);
1184 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1185 err = view_resize(view->parent);
1187 if (err)
1188 break;
1189 err = view->input(new, view, KEY_RESIZE);
1191 if (err)
1192 break;
1193 if (view->type == TOG_VIEW_LOG) {
1194 err = request_log_commits(view);
1195 if (err)
1196 break;
1198 if (view->parent)
1199 err = offset_selection_down(view->parent);
1200 if (!err)
1201 err = offset_selection_down(view);
1202 break;
1203 case KEY_RESIZE:
1204 break;
1205 case '/':
1206 view->count = 0;
1207 if (view->search_start)
1208 view_search_start(view);
1209 else
1210 err = view->input(new, view, ch);
1211 break;
1212 case 'N':
1213 case 'n':
1214 if (view->search_started && view->search_next) {
1215 view->searching = (ch == 'n' ?
1216 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1217 view->search_next_done = 0;
1218 view->search_next(view);
1219 } else
1220 err = view->input(new, view, ch);
1221 break;
1222 case 'A':
1223 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1224 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1225 else
1226 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1227 TAILQ_FOREACH(v, views, entry) {
1228 if (v->reset) {
1229 err = v->reset(v);
1230 if (err)
1231 return err;
1233 if (v->child && v->child->reset) {
1234 err = v->child->reset(v->child);
1235 if (err)
1236 return err;
1239 break;
1240 default:
1241 err = view->input(new, view, ch);
1242 break;
1245 return err;
1248 static int
1249 view_needs_focus_indication(struct tog_view *view)
1251 if (view_is_parent_view(view)) {
1252 if (view->child == NULL || view->child->focussed)
1253 return 0;
1254 if (!view_is_splitscreen(view->child))
1255 return 0;
1256 } else if (!view_is_splitscreen(view))
1257 return 0;
1259 return view->focussed;
1262 static const struct got_error *
1263 view_loop(struct tog_view *view)
1265 const struct got_error *err = NULL;
1266 struct tog_view_list_head views;
1267 struct tog_view *new_view;
1268 int fast_refresh = 10;
1269 int done = 0, errcode;
1271 errcode = pthread_mutex_lock(&tog_mutex);
1272 if (errcode)
1273 return got_error_set_errno(errcode, "pthread_mutex_lock");
1275 TAILQ_INIT(&views);
1276 TAILQ_INSERT_HEAD(&views, view, entry);
1278 view->focussed = 1;
1279 err = view->show(view);
1280 if (err)
1281 return err;
1282 update_panels();
1283 doupdate();
1284 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1285 /* Refresh fast during initialization, then become slower. */
1286 if (fast_refresh && fast_refresh-- == 0)
1287 halfdelay(10); /* switch to once per second */
1289 err = view_input(&new_view, &done, view, &views);
1290 if (err)
1291 break;
1292 if (view->dying) {
1293 struct tog_view *v, *prev = NULL;
1295 if (view_is_parent_view(view))
1296 prev = TAILQ_PREV(view, tog_view_list_head,
1297 entry);
1298 else if (view->parent)
1299 prev = view->parent;
1301 if (view->parent) {
1302 view->parent->child = NULL;
1303 view->parent->focus_child = 0;
1304 /* Restore fullscreen line height. */
1305 view->parent->nlines = view->parent->lines;
1306 err = view_resize(view->parent);
1307 if (err)
1308 break;
1309 } else
1310 TAILQ_REMOVE(&views, view, entry);
1312 err = view_close(view);
1313 if (err)
1314 goto done;
1316 view = NULL;
1317 TAILQ_FOREACH(v, &views, entry) {
1318 if (v->focussed)
1319 break;
1321 if (view == NULL && new_view == NULL) {
1322 /* No view has focus. Try to pick one. */
1323 if (prev)
1324 view = prev;
1325 else if (!TAILQ_EMPTY(&views)) {
1326 view = TAILQ_LAST(&views,
1327 tog_view_list_head);
1329 if (view) {
1330 if (view->focus_child) {
1331 view->child->focussed = 1;
1332 view = view->child;
1333 } else
1334 view->focussed = 1;
1338 if (new_view) {
1339 struct tog_view *v, *t;
1340 /* Only allow one parent view per type. */
1341 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1342 if (v->type != new_view->type)
1343 continue;
1344 TAILQ_REMOVE(&views, v, entry);
1345 err = view_close(v);
1346 if (err)
1347 goto done;
1348 break;
1350 TAILQ_INSERT_TAIL(&views, new_view, entry);
1351 view = new_view;
1353 if (view) {
1354 if (view_is_parent_view(view)) {
1355 if (view->child && view->child->focussed)
1356 view = view->child;
1357 } else {
1358 if (view->parent && view->parent->focussed)
1359 view = view->parent;
1361 show_panel(view->panel);
1362 if (view->child && view_is_splitscreen(view->child))
1363 show_panel(view->child->panel);
1364 if (view->parent && view_is_splitscreen(view)) {
1365 err = view->parent->show(view->parent);
1366 if (err)
1367 goto done;
1369 err = view->show(view);
1370 if (err)
1371 goto done;
1372 if (view->child) {
1373 err = view->child->show(view->child);
1374 if (err)
1375 goto done;
1377 update_panels();
1378 doupdate();
1381 done:
1382 while (!TAILQ_EMPTY(&views)) {
1383 view = TAILQ_FIRST(&views);
1384 TAILQ_REMOVE(&views, view, entry);
1385 view_close(view);
1388 errcode = pthread_mutex_unlock(&tog_mutex);
1389 if (errcode && err == NULL)
1390 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1392 return err;
1395 __dead static void
1396 usage_log(void)
1398 endwin();
1399 fprintf(stderr,
1400 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1401 getprogname());
1402 exit(1);
1405 /* Create newly allocated wide-character string equivalent to a byte string. */
1406 static const struct got_error *
1407 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1409 char *vis = NULL;
1410 const struct got_error *err = NULL;
1412 *ws = NULL;
1413 *wlen = mbstowcs(NULL, s, 0);
1414 if (*wlen == (size_t)-1) {
1415 int vislen;
1416 if (errno != EILSEQ)
1417 return got_error_from_errno("mbstowcs");
1419 /* byte string invalid in current encoding; try to "fix" it */
1420 err = got_mbsavis(&vis, &vislen, s);
1421 if (err)
1422 return err;
1423 *wlen = mbstowcs(NULL, vis, 0);
1424 if (*wlen == (size_t)-1) {
1425 err = got_error_from_errno("mbstowcs"); /* give up */
1426 goto done;
1430 *ws = calloc(*wlen + 1, sizeof(**ws));
1431 if (*ws == NULL) {
1432 err = got_error_from_errno("calloc");
1433 goto done;
1436 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1437 err = got_error_from_errno("mbstowcs");
1438 done:
1439 free(vis);
1440 if (err) {
1441 free(*ws);
1442 *ws = NULL;
1443 *wlen = 0;
1445 return err;
1448 static const struct got_error *
1449 expand_tab(char **ptr, const char *src)
1451 char *dst;
1452 size_t len, n, idx = 0, sz = 0;
1454 *ptr = NULL;
1455 n = len = strlen(src);
1456 dst = malloc(n + 1);
1457 if (dst == NULL)
1458 return got_error_from_errno("malloc");
1460 while (idx < len && src[idx]) {
1461 const char c = src[idx];
1463 if (c == '\t') {
1464 size_t nb = TABSIZE - sz % TABSIZE;
1465 char *p;
1467 p = realloc(dst, n + nb);
1468 if (p == NULL) {
1469 free(dst);
1470 return got_error_from_errno("realloc");
1473 dst = p;
1474 n += nb;
1475 memset(dst + sz, ' ', nb);
1476 sz += nb;
1477 } else
1478 dst[sz++] = src[idx];
1479 ++idx;
1482 dst[sz] = '\0';
1483 *ptr = dst;
1484 return NULL;
1488 * Advance at most n columns from wline starting at offset off.
1489 * Return the index to the first character after the span operation.
1490 * Return the combined column width of all spanned wide character in
1491 * *rcol.
1493 static int
1494 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1496 int width, i, cols = 0;
1498 if (n == 0) {
1499 *rcol = cols;
1500 return off;
1503 for (i = off; wline[i] != L'\0'; ++i) {
1504 if (wline[i] == L'\t')
1505 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1506 else
1507 width = wcwidth(wline[i]);
1509 if (width == -1) {
1510 width = 1;
1511 wline[i] = L'.';
1514 if (cols + width > n)
1515 break;
1516 cols += width;
1519 *rcol = cols;
1520 return i;
1524 * Format a line for display, ensuring that it won't overflow a width limit.
1525 * With scrolling, the width returned refers to the scrolled version of the
1526 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1528 static const struct got_error *
1529 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1530 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1532 const struct got_error *err = NULL;
1533 int cols;
1534 wchar_t *wline = NULL;
1535 char *exstr = NULL;
1536 size_t wlen;
1537 int i, scrollx;
1539 *wlinep = NULL;
1540 *widthp = 0;
1542 if (expand) {
1543 err = expand_tab(&exstr, line);
1544 if (err)
1545 return err;
1548 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1549 free(exstr);
1550 if (err)
1551 return err;
1553 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1555 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1556 wline[wlen - 1] = L'\0';
1557 wlen--;
1559 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1560 wline[wlen - 1] = L'\0';
1561 wlen--;
1564 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1565 wline[i] = L'\0';
1567 if (widthp)
1568 *widthp = cols;
1569 if (scrollxp)
1570 *scrollxp = scrollx;
1571 if (err)
1572 free(wline);
1573 else
1574 *wlinep = wline;
1575 return err;
1578 static const struct got_error*
1579 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1580 struct got_object_id *id, struct got_repository *repo)
1582 static const struct got_error *err = NULL;
1583 struct got_reflist_entry *re;
1584 char *s;
1585 const char *name;
1587 *refs_str = NULL;
1589 TAILQ_FOREACH(re, refs, entry) {
1590 struct got_tag_object *tag = NULL;
1591 struct got_object_id *ref_id;
1592 int cmp;
1594 name = got_ref_get_name(re->ref);
1595 if (strcmp(name, GOT_REF_HEAD) == 0)
1596 continue;
1597 if (strncmp(name, "refs/", 5) == 0)
1598 name += 5;
1599 if (strncmp(name, "got/", 4) == 0 &&
1600 strncmp(name, "got/backup/", 11) != 0)
1601 continue;
1602 if (strncmp(name, "heads/", 6) == 0)
1603 name += 6;
1604 if (strncmp(name, "remotes/", 8) == 0) {
1605 name += 8;
1606 s = strstr(name, "/" GOT_REF_HEAD);
1607 if (s != NULL && s[strlen(s)] == '\0')
1608 continue;
1610 err = got_ref_resolve(&ref_id, repo, re->ref);
1611 if (err)
1612 break;
1613 if (strncmp(name, "tags/", 5) == 0) {
1614 err = got_object_open_as_tag(&tag, repo, ref_id);
1615 if (err) {
1616 if (err->code != GOT_ERR_OBJ_TYPE) {
1617 free(ref_id);
1618 break;
1620 /* Ref points at something other than a tag. */
1621 err = NULL;
1622 tag = NULL;
1625 cmp = got_object_id_cmp(tag ?
1626 got_object_tag_get_object_id(tag) : ref_id, id);
1627 free(ref_id);
1628 if (tag)
1629 got_object_tag_close(tag);
1630 if (cmp != 0)
1631 continue;
1632 s = *refs_str;
1633 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1634 s ? ", " : "", name) == -1) {
1635 err = got_error_from_errno("asprintf");
1636 free(s);
1637 *refs_str = NULL;
1638 break;
1640 free(s);
1643 return err;
1646 static const struct got_error *
1647 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1648 int col_tab_align)
1650 char *smallerthan;
1652 smallerthan = strchr(author, '<');
1653 if (smallerthan && smallerthan[1] != '\0')
1654 author = smallerthan + 1;
1655 author[strcspn(author, "@>")] = '\0';
1656 return format_line(wauthor, author_width, NULL, author, 0, limit,
1657 col_tab_align, 0);
1660 static const struct got_error *
1661 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1662 struct got_object_id *id, const size_t date_display_cols,
1663 int author_display_cols)
1665 struct tog_log_view_state *s = &view->state.log;
1666 const struct got_error *err = NULL;
1667 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1668 char *logmsg0 = NULL, *logmsg = NULL;
1669 char *author = NULL;
1670 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1671 int author_width, logmsg_width;
1672 char *newline, *line = NULL;
1673 int col, limit, scrollx;
1674 const int avail = view->ncols;
1675 struct tm tm;
1676 time_t committer_time;
1677 struct tog_color *tc;
1679 committer_time = got_object_commit_get_committer_time(commit);
1680 if (gmtime_r(&committer_time, &tm) == NULL)
1681 return got_error_from_errno("gmtime_r");
1682 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1683 return got_error(GOT_ERR_NO_SPACE);
1685 if (avail <= date_display_cols)
1686 limit = MIN(sizeof(datebuf) - 1, avail);
1687 else
1688 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1689 tc = get_color(&s->colors, TOG_COLOR_DATE);
1690 if (tc)
1691 wattr_on(view->window,
1692 COLOR_PAIR(tc->colorpair), NULL);
1693 waddnstr(view->window, datebuf, limit);
1694 if (tc)
1695 wattr_off(view->window,
1696 COLOR_PAIR(tc->colorpair), NULL);
1697 col = limit;
1698 if (col > avail)
1699 goto done;
1701 if (avail >= 120) {
1702 char *id_str;
1703 err = got_object_id_str(&id_str, id);
1704 if (err)
1705 goto done;
1706 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1707 if (tc)
1708 wattr_on(view->window,
1709 COLOR_PAIR(tc->colorpair), NULL);
1710 wprintw(view->window, "%.8s ", id_str);
1711 if (tc)
1712 wattr_off(view->window,
1713 COLOR_PAIR(tc->colorpair), NULL);
1714 free(id_str);
1715 col += 9;
1716 if (col > avail)
1717 goto done;
1720 author = strdup(got_object_commit_get_author(commit));
1721 if (author == NULL) {
1722 err = got_error_from_errno("strdup");
1723 goto done;
1725 err = format_author(&wauthor, &author_width, author, avail - col, col);
1726 if (err)
1727 goto done;
1728 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1729 if (tc)
1730 wattr_on(view->window,
1731 COLOR_PAIR(tc->colorpair), NULL);
1732 waddwstr(view->window, wauthor);
1733 if (tc)
1734 wattr_off(view->window,
1735 COLOR_PAIR(tc->colorpair), NULL);
1736 col += author_width;
1737 while (col < avail && author_width < author_display_cols + 2) {
1738 waddch(view->window, ' ');
1739 col++;
1740 author_width++;
1742 if (col > avail)
1743 goto done;
1745 err = got_object_commit_get_logmsg(&logmsg0, commit);
1746 if (err)
1747 goto done;
1748 logmsg = logmsg0;
1749 while (*logmsg == '\n')
1750 logmsg++;
1751 newline = strchr(logmsg, '\n');
1752 if (newline)
1753 *newline = '\0';
1754 limit = avail - col;
1755 if (view->child && view_is_splitscreen(view->child) && limit > 0)
1756 limit--; /* for the border */
1757 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1758 limit, col, 1);
1759 if (err)
1760 goto done;
1761 waddwstr(view->window, &wlogmsg[scrollx]);
1762 col += MAX(logmsg_width, 0);
1763 while (col < avail) {
1764 waddch(view->window, ' ');
1765 col++;
1767 done:
1768 free(logmsg0);
1769 free(wlogmsg);
1770 free(author);
1771 free(wauthor);
1772 free(line);
1773 return err;
1776 static struct commit_queue_entry *
1777 alloc_commit_queue_entry(struct got_commit_object *commit,
1778 struct got_object_id *id)
1780 struct commit_queue_entry *entry;
1782 entry = calloc(1, sizeof(*entry));
1783 if (entry == NULL)
1784 return NULL;
1786 entry->id = id;
1787 entry->commit = commit;
1788 return entry;
1791 static void
1792 pop_commit(struct commit_queue *commits)
1794 struct commit_queue_entry *entry;
1796 entry = TAILQ_FIRST(&commits->head);
1797 TAILQ_REMOVE(&commits->head, entry, entry);
1798 got_object_commit_close(entry->commit);
1799 commits->ncommits--;
1800 /* Don't free entry->id! It is owned by the commit graph. */
1801 free(entry);
1804 static void
1805 free_commits(struct commit_queue *commits)
1807 while (!TAILQ_EMPTY(&commits->head))
1808 pop_commit(commits);
1811 static const struct got_error *
1812 match_commit(int *have_match, struct got_object_id *id,
1813 struct got_commit_object *commit, regex_t *regex)
1815 const struct got_error *err = NULL;
1816 regmatch_t regmatch;
1817 char *id_str = NULL, *logmsg = NULL;
1819 *have_match = 0;
1821 err = got_object_id_str(&id_str, id);
1822 if (err)
1823 return err;
1825 err = got_object_commit_get_logmsg(&logmsg, commit);
1826 if (err)
1827 goto done;
1829 if (regexec(regex, got_object_commit_get_author(commit), 1,
1830 &regmatch, 0) == 0 ||
1831 regexec(regex, got_object_commit_get_committer(commit), 1,
1832 &regmatch, 0) == 0 ||
1833 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1834 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1835 *have_match = 1;
1836 done:
1837 free(id_str);
1838 free(logmsg);
1839 return err;
1842 static const struct got_error *
1843 queue_commits(struct tog_log_thread_args *a)
1845 const struct got_error *err = NULL;
1848 * We keep all commits open throughout the lifetime of the log
1849 * view in order to avoid having to re-fetch commits from disk
1850 * while updating the display.
1852 do {
1853 struct got_object_id *id;
1854 struct got_commit_object *commit;
1855 struct commit_queue_entry *entry;
1856 int errcode;
1858 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1859 NULL, NULL);
1860 if (err || id == NULL)
1861 break;
1863 err = got_object_open_as_commit(&commit, a->repo, id);
1864 if (err)
1865 break;
1866 entry = alloc_commit_queue_entry(commit, id);
1867 if (entry == NULL) {
1868 err = got_error_from_errno("alloc_commit_queue_entry");
1869 break;
1872 errcode = pthread_mutex_lock(&tog_mutex);
1873 if (errcode) {
1874 err = got_error_set_errno(errcode,
1875 "pthread_mutex_lock");
1876 break;
1879 entry->idx = a->commits->ncommits;
1880 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1881 a->commits->ncommits++;
1883 if (*a->searching == TOG_SEARCH_FORWARD &&
1884 !*a->search_next_done) {
1885 int have_match;
1886 err = match_commit(&have_match, id, commit, a->regex);
1887 if (err)
1888 break;
1889 if (have_match)
1890 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1893 errcode = pthread_mutex_unlock(&tog_mutex);
1894 if (errcode && err == NULL)
1895 err = got_error_set_errno(errcode,
1896 "pthread_mutex_unlock");
1897 if (err)
1898 break;
1899 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1901 return err;
1904 static void
1905 select_commit(struct tog_log_view_state *s)
1907 struct commit_queue_entry *entry;
1908 int ncommits = 0;
1910 entry = s->first_displayed_entry;
1911 while (entry) {
1912 if (ncommits == s->selected) {
1913 s->selected_entry = entry;
1914 break;
1916 entry = TAILQ_NEXT(entry, entry);
1917 ncommits++;
1921 static const struct got_error *
1922 draw_commits(struct tog_view *view)
1924 const struct got_error *err = NULL;
1925 struct tog_log_view_state *s = &view->state.log;
1926 struct commit_queue_entry *entry = s->selected_entry;
1927 const int limit = view->nlines;
1928 int width;
1929 int ncommits, author_cols = 4;
1930 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1931 char *refs_str = NULL;
1932 wchar_t *wline;
1933 struct tog_color *tc;
1934 static const size_t date_display_cols = 12;
1936 if (s->selected_entry &&
1937 !(view->searching && view->search_next_done == 0)) {
1938 struct got_reflist_head *refs;
1939 err = got_object_id_str(&id_str, s->selected_entry->id);
1940 if (err)
1941 return err;
1942 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1943 s->selected_entry->id);
1944 if (refs) {
1945 err = build_refs_str(&refs_str, refs,
1946 s->selected_entry->id, s->repo);
1947 if (err)
1948 goto done;
1952 if (s->thread_args.commits_needed == 0)
1953 halfdelay(10); /* disable fast refresh */
1955 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1956 if (asprintf(&ncommits_str, " [%d/%d] %s",
1957 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1958 (view->searching && !view->search_next_done) ?
1959 "searching..." : "loading...") == -1) {
1960 err = got_error_from_errno("asprintf");
1961 goto done;
1963 } else {
1964 const char *search_str = NULL;
1966 if (view->searching) {
1967 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1968 search_str = "no more matches";
1969 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1970 search_str = "no matches found";
1971 else if (!view->search_next_done)
1972 search_str = "searching...";
1975 if (asprintf(&ncommits_str, " [%d/%d] %s",
1976 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1977 search_str ? search_str :
1978 (refs_str ? refs_str : "")) == -1) {
1979 err = got_error_from_errno("asprintf");
1980 goto done;
1984 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1985 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1986 "........................................",
1987 s->in_repo_path, ncommits_str) == -1) {
1988 err = got_error_from_errno("asprintf");
1989 header = NULL;
1990 goto done;
1992 } else if (asprintf(&header, "commit %s%s",
1993 id_str ? id_str : "........................................",
1994 ncommits_str) == -1) {
1995 err = got_error_from_errno("asprintf");
1996 header = NULL;
1997 goto done;
1999 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2000 if (err)
2001 goto done;
2003 werase(view->window);
2005 if (view_needs_focus_indication(view))
2006 wstandout(view->window);
2007 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2008 if (tc)
2009 wattr_on(view->window,
2010 COLOR_PAIR(tc->colorpair), NULL);
2011 waddwstr(view->window, wline);
2012 if (tc)
2013 wattr_off(view->window,
2014 COLOR_PAIR(tc->colorpair), NULL);
2015 while (width < view->ncols) {
2016 waddch(view->window, ' ');
2017 width++;
2019 if (view_needs_focus_indication(view))
2020 wstandend(view->window);
2021 free(wline);
2022 if (limit <= 1)
2023 goto done;
2025 /* Grow author column size if necessary, and set view->maxx. */
2026 entry = s->first_displayed_entry;
2027 ncommits = 0;
2028 view->maxx = 0;
2029 while (entry) {
2030 char *author, *eol, *msg, *msg0;
2031 wchar_t *wauthor, *wmsg;
2032 int width;
2033 if (ncommits >= limit - 1)
2034 break;
2035 author = strdup(got_object_commit_get_author(entry->commit));
2036 if (author == NULL) {
2037 err = got_error_from_errno("strdup");
2038 goto done;
2040 err = format_author(&wauthor, &width, author, COLS,
2041 date_display_cols);
2042 if (author_cols < width)
2043 author_cols = width;
2044 free(wauthor);
2045 free(author);
2046 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2047 if (err)
2048 goto done;
2049 msg = msg0;
2050 while (*msg == '\n')
2051 ++msg;
2052 if ((eol = strchr(msg, '\n')))
2053 *eol = '\0';
2054 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2055 date_display_cols + author_cols, 0);
2056 if (err)
2057 goto done;
2058 view->maxx = MAX(view->maxx, width);
2059 free(msg0);
2060 free(wmsg);
2061 ncommits++;
2062 entry = TAILQ_NEXT(entry, entry);
2065 entry = s->first_displayed_entry;
2066 s->last_displayed_entry = s->first_displayed_entry;
2067 ncommits = 0;
2068 while (entry) {
2069 if (ncommits >= limit - 1)
2070 break;
2071 if (ncommits == s->selected)
2072 wstandout(view->window);
2073 err = draw_commit(view, entry->commit, entry->id,
2074 date_display_cols, author_cols);
2075 if (ncommits == s->selected)
2076 wstandend(view->window);
2077 if (err)
2078 goto done;
2079 ncommits++;
2080 s->last_displayed_entry = entry;
2081 entry = TAILQ_NEXT(entry, entry);
2084 view_border(view);
2085 done:
2086 free(id_str);
2087 free(refs_str);
2088 free(ncommits_str);
2089 free(header);
2090 return err;
2093 static void
2094 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2096 struct commit_queue_entry *entry;
2097 int nscrolled = 0;
2099 entry = TAILQ_FIRST(&s->commits.head);
2100 if (s->first_displayed_entry == entry)
2101 return;
2103 entry = s->first_displayed_entry;
2104 while (entry && nscrolled < maxscroll) {
2105 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2106 if (entry) {
2107 s->first_displayed_entry = entry;
2108 nscrolled++;
2113 static const struct got_error *
2114 trigger_log_thread(struct tog_view *view, int wait)
2116 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2117 int errcode;
2119 halfdelay(1); /* fast refresh while loading commits */
2121 while (ta->commits_needed > 0 || ta->load_all) {
2122 if (ta->log_complete)
2123 break;
2125 /* Wake the log thread. */
2126 errcode = pthread_cond_signal(&ta->need_commits);
2127 if (errcode)
2128 return got_error_set_errno(errcode,
2129 "pthread_cond_signal");
2132 * The mutex will be released while the view loop waits
2133 * in wgetch(), at which time the log thread will run.
2135 if (!wait)
2136 break;
2138 /* Display progress update in log view. */
2139 show_log_view(view);
2140 update_panels();
2141 doupdate();
2143 /* Wait right here while next commit is being loaded. */
2144 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2145 if (errcode)
2146 return got_error_set_errno(errcode,
2147 "pthread_cond_wait");
2149 /* Display progress update in log view. */
2150 show_log_view(view);
2151 update_panels();
2152 doupdate();
2155 return NULL;
2158 static const struct got_error *
2159 request_log_commits(struct tog_view *view)
2161 struct tog_log_view_state *state = &view->state.log;
2162 const struct got_error *err = NULL;
2164 state->thread_args.commits_needed = view->nscrolled;
2165 err = trigger_log_thread(view, 1);
2166 view->nscrolled = 0;
2168 return err;
2171 static const struct got_error *
2172 log_scroll_down(struct tog_view *view, int maxscroll)
2174 struct tog_log_view_state *s = &view->state.log;
2175 const struct got_error *err = NULL;
2176 struct commit_queue_entry *pentry;
2177 int nscrolled = 0, ncommits_needed;
2179 if (s->last_displayed_entry == NULL)
2180 return NULL;
2182 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2183 if (s->commits.ncommits < ncommits_needed &&
2184 !s->thread_args.log_complete) {
2186 * Ask the log thread for required amount of commits.
2188 s->thread_args.commits_needed += maxscroll;
2189 err = trigger_log_thread(view, 1);
2190 if (err)
2191 return err;
2194 do {
2195 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2196 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2197 break;
2199 s->last_displayed_entry = pentry ?
2200 pentry : s->last_displayed_entry;;
2202 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2203 if (pentry == NULL)
2204 break;
2205 s->first_displayed_entry = pentry;
2206 } while (++nscrolled < maxscroll);
2208 if (view->mode == TOG_VIEW_SPLIT_HRZN)
2209 view->nscrolled += nscrolled;
2210 else
2211 view->nscrolled = 0;
2213 return err;
2216 static const struct got_error *
2217 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2218 struct got_commit_object *commit, struct got_object_id *commit_id,
2219 struct tog_view *log_view, struct got_repository *repo)
2221 const struct got_error *err;
2222 struct got_object_qid *parent_id;
2223 struct tog_view *diff_view;
2225 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2226 if (diff_view == NULL)
2227 return got_error_from_errno("view_open");
2229 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2230 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2231 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2232 if (err == NULL)
2233 *new_view = diff_view;
2234 return err;
2237 static const struct got_error *
2238 tree_view_visit_subtree(struct tog_tree_view_state *s,
2239 struct got_tree_object *subtree)
2241 struct tog_parent_tree *parent;
2243 parent = calloc(1, sizeof(*parent));
2244 if (parent == NULL)
2245 return got_error_from_errno("calloc");
2247 parent->tree = s->tree;
2248 parent->first_displayed_entry = s->first_displayed_entry;
2249 parent->selected_entry = s->selected_entry;
2250 parent->selected = s->selected;
2251 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2252 s->tree = subtree;
2253 s->selected = 0;
2254 s->first_displayed_entry = NULL;
2255 return NULL;
2258 static const struct got_error *
2259 tree_view_walk_path(struct tog_tree_view_state *s,
2260 struct got_commit_object *commit, const char *path)
2262 const struct got_error *err = NULL;
2263 struct got_tree_object *tree = NULL;
2264 const char *p;
2265 char *slash, *subpath = NULL;
2267 /* Walk the path and open corresponding tree objects. */
2268 p = path;
2269 while (*p) {
2270 struct got_tree_entry *te;
2271 struct got_object_id *tree_id;
2272 char *te_name;
2274 while (p[0] == '/')
2275 p++;
2277 /* Ensure the correct subtree entry is selected. */
2278 slash = strchr(p, '/');
2279 if (slash == NULL)
2280 te_name = strdup(p);
2281 else
2282 te_name = strndup(p, slash - p);
2283 if (te_name == NULL) {
2284 err = got_error_from_errno("strndup");
2285 break;
2287 te = got_object_tree_find_entry(s->tree, te_name);
2288 if (te == NULL) {
2289 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2290 free(te_name);
2291 break;
2293 free(te_name);
2294 s->first_displayed_entry = s->selected_entry = te;
2296 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2297 break; /* jump to this file's entry */
2299 slash = strchr(p, '/');
2300 if (slash)
2301 subpath = strndup(path, slash - path);
2302 else
2303 subpath = strdup(path);
2304 if (subpath == NULL) {
2305 err = got_error_from_errno("strdup");
2306 break;
2309 err = got_object_id_by_path(&tree_id, s->repo, commit,
2310 subpath);
2311 if (err)
2312 break;
2314 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2315 free(tree_id);
2316 if (err)
2317 break;
2319 err = tree_view_visit_subtree(s, tree);
2320 if (err) {
2321 got_object_tree_close(tree);
2322 break;
2324 if (slash == NULL)
2325 break;
2326 free(subpath);
2327 subpath = NULL;
2328 p = slash;
2331 free(subpath);
2332 return err;
2335 static const struct got_error *
2336 browse_commit_tree(struct tog_view **new_view, int begin_x,
2337 struct commit_queue_entry *entry, const char *path,
2338 const char *head_ref_name, struct got_repository *repo)
2340 const struct got_error *err = NULL;
2341 struct tog_tree_view_state *s;
2342 struct tog_view *tree_view;
2344 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2345 if (tree_view == NULL)
2346 return got_error_from_errno("view_open");
2348 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2349 if (err)
2350 return err;
2351 s = &tree_view->state.tree;
2353 *new_view = tree_view;
2355 if (got_path_is_root_dir(path))
2356 return NULL;
2358 return tree_view_walk_path(s, entry->commit, path);
2361 static const struct got_error *
2362 block_signals_used_by_main_thread(void)
2364 sigset_t sigset;
2365 int errcode;
2367 if (sigemptyset(&sigset) == -1)
2368 return got_error_from_errno("sigemptyset");
2370 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2371 if (sigaddset(&sigset, SIGWINCH) == -1)
2372 return got_error_from_errno("sigaddset");
2373 if (sigaddset(&sigset, SIGCONT) == -1)
2374 return got_error_from_errno("sigaddset");
2375 if (sigaddset(&sigset, SIGINT) == -1)
2376 return got_error_from_errno("sigaddset");
2377 if (sigaddset(&sigset, SIGTERM) == -1)
2378 return got_error_from_errno("sigaddset");
2380 /* ncurses handles SIGTSTP */
2381 if (sigaddset(&sigset, SIGTSTP) == -1)
2382 return got_error_from_errno("sigaddset");
2384 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2385 if (errcode)
2386 return got_error_set_errno(errcode, "pthread_sigmask");
2388 return NULL;
2391 static void *
2392 log_thread(void *arg)
2394 const struct got_error *err = NULL;
2395 int errcode = 0;
2396 struct tog_log_thread_args *a = arg;
2397 int done = 0;
2399 err = block_signals_used_by_main_thread();
2400 if (err)
2401 return (void *)err;
2403 while (!done && !err && !tog_fatal_signal_received()) {
2404 err = queue_commits(a);
2405 if (err) {
2406 if (err->code != GOT_ERR_ITER_COMPLETED)
2407 return (void *)err;
2408 err = NULL;
2409 done = 1;
2410 } else if (a->commits_needed > 0 && !a->load_all)
2411 a->commits_needed--;
2413 errcode = pthread_mutex_lock(&tog_mutex);
2414 if (errcode) {
2415 err = got_error_set_errno(errcode,
2416 "pthread_mutex_lock");
2417 break;
2418 } else if (*a->quit)
2419 done = 1;
2420 else if (*a->first_displayed_entry == NULL) {
2421 *a->first_displayed_entry =
2422 TAILQ_FIRST(&a->commits->head);
2423 *a->selected_entry = *a->first_displayed_entry;
2426 errcode = pthread_cond_signal(&a->commit_loaded);
2427 if (errcode) {
2428 err = got_error_set_errno(errcode,
2429 "pthread_cond_signal");
2430 pthread_mutex_unlock(&tog_mutex);
2431 break;
2434 if (done)
2435 a->commits_needed = 0;
2436 else {
2437 if (a->commits_needed == 0 && !a->load_all) {
2438 errcode = pthread_cond_wait(&a->need_commits,
2439 &tog_mutex);
2440 if (errcode)
2441 err = got_error_set_errno(errcode,
2442 "pthread_cond_wait");
2443 if (*a->quit)
2444 done = 1;
2448 errcode = pthread_mutex_unlock(&tog_mutex);
2449 if (errcode && err == NULL)
2450 err = got_error_set_errno(errcode,
2451 "pthread_mutex_unlock");
2453 a->log_complete = 1;
2454 return (void *)err;
2457 static const struct got_error *
2458 stop_log_thread(struct tog_log_view_state *s)
2460 const struct got_error *err = NULL;
2461 int errcode;
2463 if (s->thread) {
2464 s->quit = 1;
2465 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2466 if (errcode)
2467 return got_error_set_errno(errcode,
2468 "pthread_cond_signal");
2469 errcode = pthread_mutex_unlock(&tog_mutex);
2470 if (errcode)
2471 return got_error_set_errno(errcode,
2472 "pthread_mutex_unlock");
2473 errcode = pthread_join(s->thread, (void **)&err);
2474 if (errcode)
2475 return got_error_set_errno(errcode, "pthread_join");
2476 errcode = pthread_mutex_lock(&tog_mutex);
2477 if (errcode)
2478 return got_error_set_errno(errcode,
2479 "pthread_mutex_lock");
2480 s->thread = NULL;
2483 if (s->thread_args.repo) {
2484 err = got_repo_close(s->thread_args.repo);
2485 s->thread_args.repo = NULL;
2488 if (s->thread_args.pack_fds) {
2489 const struct got_error *pack_err =
2490 got_repo_pack_fds_close(s->thread_args.pack_fds);
2491 if (err == NULL)
2492 err = pack_err;
2493 s->thread_args.pack_fds = NULL;
2496 if (s->thread_args.graph) {
2497 got_commit_graph_close(s->thread_args.graph);
2498 s->thread_args.graph = NULL;
2501 return err;
2504 static const struct got_error *
2505 close_log_view(struct tog_view *view)
2507 const struct got_error *err = NULL;
2508 struct tog_log_view_state *s = &view->state.log;
2509 int errcode;
2511 err = stop_log_thread(s);
2513 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2514 if (errcode && err == NULL)
2515 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2517 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2518 if (errcode && err == NULL)
2519 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2521 free_commits(&s->commits);
2522 free(s->in_repo_path);
2523 s->in_repo_path = NULL;
2524 free(s->start_id);
2525 s->start_id = NULL;
2526 free(s->head_ref_name);
2527 s->head_ref_name = NULL;
2528 return err;
2531 static const struct got_error *
2532 search_start_log_view(struct tog_view *view)
2534 struct tog_log_view_state *s = &view->state.log;
2536 s->matched_entry = NULL;
2537 s->search_entry = NULL;
2538 return NULL;
2541 static const struct got_error *
2542 search_next_log_view(struct tog_view *view)
2544 const struct got_error *err = NULL;
2545 struct tog_log_view_state *s = &view->state.log;
2546 struct commit_queue_entry *entry;
2548 /* Display progress update in log view. */
2549 show_log_view(view);
2550 update_panels();
2551 doupdate();
2553 if (s->search_entry) {
2554 int errcode, ch;
2555 errcode = pthread_mutex_unlock(&tog_mutex);
2556 if (errcode)
2557 return got_error_set_errno(errcode,
2558 "pthread_mutex_unlock");
2559 ch = wgetch(view->window);
2560 errcode = pthread_mutex_lock(&tog_mutex);
2561 if (errcode)
2562 return got_error_set_errno(errcode,
2563 "pthread_mutex_lock");
2564 if (ch == KEY_BACKSPACE) {
2565 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2566 return NULL;
2568 if (view->searching == TOG_SEARCH_FORWARD)
2569 entry = TAILQ_NEXT(s->search_entry, entry);
2570 else
2571 entry = TAILQ_PREV(s->search_entry,
2572 commit_queue_head, entry);
2573 } else if (s->matched_entry) {
2574 int matched_idx = s->matched_entry->idx;
2575 int selected_idx = s->selected_entry->idx;
2578 * If the user has moved the cursor after we hit a match,
2579 * the position from where we should continue searching
2580 * might have changed.
2582 if (view->searching == TOG_SEARCH_FORWARD) {
2583 if (matched_idx > selected_idx)
2584 entry = TAILQ_NEXT(s->selected_entry, entry);
2585 else
2586 entry = TAILQ_NEXT(s->matched_entry, entry);
2587 } else {
2588 if (matched_idx < selected_idx)
2589 entry = TAILQ_PREV(s->selected_entry,
2590 commit_queue_head, entry);
2591 else
2592 entry = TAILQ_PREV(s->matched_entry,
2593 commit_queue_head, entry);
2595 } else {
2596 entry = s->selected_entry;
2599 while (1) {
2600 int have_match = 0;
2602 if (entry == NULL) {
2603 if (s->thread_args.log_complete ||
2604 view->searching == TOG_SEARCH_BACKWARD) {
2605 view->search_next_done =
2606 (s->matched_entry == NULL ?
2607 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2608 s->search_entry = NULL;
2609 return NULL;
2612 * Poke the log thread for more commits and return,
2613 * allowing the main loop to make progress. Search
2614 * will resume at s->search_entry once we come back.
2616 s->thread_args.commits_needed++;
2617 return trigger_log_thread(view, 0);
2620 err = match_commit(&have_match, entry->id, entry->commit,
2621 &view->regex);
2622 if (err)
2623 break;
2624 if (have_match) {
2625 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2626 s->matched_entry = entry;
2627 break;
2630 s->search_entry = entry;
2631 if (view->searching == TOG_SEARCH_FORWARD)
2632 entry = TAILQ_NEXT(entry, entry);
2633 else
2634 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2637 if (s->matched_entry) {
2638 int cur = s->selected_entry->idx;
2639 while (cur < s->matched_entry->idx) {
2640 err = input_log_view(NULL, view, KEY_DOWN);
2641 if (err)
2642 return err;
2643 cur++;
2645 while (cur > s->matched_entry->idx) {
2646 err = input_log_view(NULL, view, KEY_UP);
2647 if (err)
2648 return err;
2649 cur--;
2653 s->search_entry = NULL;
2655 return NULL;
2658 static const struct got_error *
2659 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2660 struct got_repository *repo, const char *head_ref_name,
2661 const char *in_repo_path, int log_branches)
2663 const struct got_error *err = NULL;
2664 struct tog_log_view_state *s = &view->state.log;
2665 struct got_repository *thread_repo = NULL;
2666 struct got_commit_graph *thread_graph = NULL;
2667 int errcode;
2669 if (in_repo_path != s->in_repo_path) {
2670 free(s->in_repo_path);
2671 s->in_repo_path = strdup(in_repo_path);
2672 if (s->in_repo_path == NULL)
2673 return got_error_from_errno("strdup");
2676 /* The commit queue only contains commits being displayed. */
2677 TAILQ_INIT(&s->commits.head);
2678 s->commits.ncommits = 0;
2680 s->repo = repo;
2681 if (head_ref_name) {
2682 s->head_ref_name = strdup(head_ref_name);
2683 if (s->head_ref_name == NULL) {
2684 err = got_error_from_errno("strdup");
2685 goto done;
2688 s->start_id = got_object_id_dup(start_id);
2689 if (s->start_id == NULL) {
2690 err = got_error_from_errno("got_object_id_dup");
2691 goto done;
2693 s->log_branches = log_branches;
2695 STAILQ_INIT(&s->colors);
2696 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2697 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2698 get_color_value("TOG_COLOR_COMMIT"));
2699 if (err)
2700 goto done;
2701 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2702 get_color_value("TOG_COLOR_AUTHOR"));
2703 if (err) {
2704 free_colors(&s->colors);
2705 goto done;
2707 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2708 get_color_value("TOG_COLOR_DATE"));
2709 if (err) {
2710 free_colors(&s->colors);
2711 goto done;
2715 view->show = show_log_view;
2716 view->input = input_log_view;
2717 view->close = close_log_view;
2718 view->search_start = search_start_log_view;
2719 view->search_next = search_next_log_view;
2721 if (s->thread_args.pack_fds == NULL) {
2722 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2723 if (err)
2724 goto done;
2726 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2727 s->thread_args.pack_fds);
2728 if (err)
2729 goto done;
2730 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2731 !s->log_branches);
2732 if (err)
2733 goto done;
2734 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2735 s->repo, NULL, NULL);
2736 if (err)
2737 goto done;
2739 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2740 if (errcode) {
2741 err = got_error_set_errno(errcode, "pthread_cond_init");
2742 goto done;
2744 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2745 if (errcode) {
2746 err = got_error_set_errno(errcode, "pthread_cond_init");
2747 goto done;
2750 s->thread_args.commits_needed = view->nlines;
2751 s->thread_args.graph = thread_graph;
2752 s->thread_args.commits = &s->commits;
2753 s->thread_args.in_repo_path = s->in_repo_path;
2754 s->thread_args.start_id = s->start_id;
2755 s->thread_args.repo = thread_repo;
2756 s->thread_args.log_complete = 0;
2757 s->thread_args.quit = &s->quit;
2758 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2759 s->thread_args.selected_entry = &s->selected_entry;
2760 s->thread_args.searching = &view->searching;
2761 s->thread_args.search_next_done = &view->search_next_done;
2762 s->thread_args.regex = &view->regex;
2763 done:
2764 if (err)
2765 close_log_view(view);
2766 return err;
2769 static const struct got_error *
2770 show_log_view(struct tog_view *view)
2772 const struct got_error *err;
2773 struct tog_log_view_state *s = &view->state.log;
2775 if (s->thread == NULL) {
2776 int errcode = pthread_create(&s->thread, NULL, log_thread,
2777 &s->thread_args);
2778 if (errcode)
2779 return got_error_set_errno(errcode, "pthread_create");
2780 if (s->thread_args.commits_needed > 0) {
2781 err = trigger_log_thread(view, 1);
2782 if (err)
2783 return err;
2787 return draw_commits(view);
2790 static void
2791 log_move_cursor_up(struct tog_view *view, int page, int home)
2793 struct tog_log_view_state *s = &view->state.log;
2795 if (s->selected_entry->idx == 0)
2796 view->count = 0;
2797 if (s->first_displayed_entry == NULL)
2798 return;
2800 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2801 || home)
2802 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2804 if (!page && !home && s->selected > 0)
2805 --s->selected;
2806 else
2807 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2809 select_commit(s);
2810 return;
2813 static const struct got_error *
2814 log_move_cursor_down(struct tog_view *view, int page)
2816 struct tog_log_view_state *s = &view->state.log;
2817 struct commit_queue_entry *first;
2818 const struct got_error *err = NULL;
2820 first = s->first_displayed_entry;
2821 if (first == NULL) {
2822 view->count = 0;
2823 return NULL;
2826 if (s->thread_args.log_complete &&
2827 s->selected_entry->idx >= s->commits.ncommits - 1)
2828 return NULL;
2830 if (!page) {
2831 int eos = view->nlines - 2;
2833 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2834 view_is_splitscreen(view->child))
2835 --eos; /* border consumes the last line */
2836 if (s->selected < MIN(eos, s->commits.ncommits - 1))
2837 ++s->selected;
2838 else
2839 err = log_scroll_down(view, 1);
2840 } else if (s->thread_args.load_all) {
2841 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2842 s->selected += MIN(s->last_displayed_entry->idx -
2843 s->selected_entry->idx, page + 1);
2844 else
2845 err = log_scroll_down(view, MIN(page,
2846 s->commits.ncommits - s->selected_entry->idx - 1));
2847 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2848 } else {
2849 err = log_scroll_down(view, page);
2850 if (err)
2851 return err;
2852 if (first == s->first_displayed_entry && s->selected <
2853 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2854 s->selected = MIN(s->commits.ncommits - 1, page);
2857 if (err)
2858 return err;
2861 * We might necessarily overshoot in horizontal
2862 * splits; if so, select the last displayed commit.
2864 s->selected = MIN(s->selected,
2865 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2867 select_commit(s);
2869 if (s->thread_args.log_complete &&
2870 s->selected_entry->idx == s->commits.ncommits - 1)
2871 view->count = 0;
2873 return NULL;
2877 * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2878 * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2879 * TOG_VIEW_SPLIT_HRZN horizontal split
2880 * Assign start column and line of the new split to *x and *y, respectively,
2881 * and assign view mode to view->mode.
2883 static void
2884 view_get_split(struct tog_view *view, int *y, int *x)
2886 char *mode;
2888 *x = 0;
2889 *y = 0;
2891 mode = getenv("TOG_VIEW_SPLIT_MODE");
2893 if (!mode || mode[0] != 'h') {
2894 view->mode = TOG_VIEW_SPLIT_VERT;
2895 *x = view_split_begin_x(view->begin_x);
2896 } else if (mode && mode[0] == 'h') {
2897 view->mode = TOG_VIEW_SPLIT_HRZN;
2898 *y = view_split_begin_y(view->lines);
2902 /* Split view horizontally at y and offset view->state->selected line. */
2903 static const struct got_error *
2904 view_init_hsplit(struct tog_view *view, int y)
2906 const struct got_error *err = NULL;
2908 view->nlines = y;
2909 err = view_resize(view);
2910 if (err)
2911 return err;
2913 err = offset_selection_down(view);
2915 return err;
2918 static const struct got_error *
2919 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2921 const struct got_error *err = NULL;
2922 struct tog_log_view_state *s = &view->state.log;
2923 struct tog_view *diff_view = NULL, *tree_view = NULL;
2924 struct tog_view *ref_view = NULL;
2925 struct commit_queue_entry *entry;
2926 int begin_x = 0, begin_y = 0, eos, n, nscroll;
2928 if (s->thread_args.load_all) {
2929 if (ch == KEY_BACKSPACE)
2930 s->thread_args.load_all = 0;
2931 else if (s->thread_args.log_complete) {
2932 err = log_move_cursor_down(view, s->commits.ncommits);
2933 s->thread_args.load_all = 0;
2935 return err;
2938 eos = nscroll = view->nlines - 1;
2939 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2940 view_is_splitscreen(view->child))
2941 --eos; /* border */
2944 switch (ch) {
2945 case 'q':
2946 s->quit = 1;
2947 break;
2948 case '0':
2949 view->x = 0;
2950 break;
2951 case '$':
2952 view->x = MAX(view->maxx - view->ncols / 2, 0);
2953 view->count = 0;
2954 break;
2955 case KEY_RIGHT:
2956 case 'l':
2957 if (view->x + view->ncols / 2 < view->maxx)
2958 view->x += 2; /* move two columns right */
2959 else
2960 view->count = 0;
2961 break;
2962 case KEY_LEFT:
2963 case 'h':
2964 view->x -= MIN(view->x, 2); /* move two columns back */
2965 if (view->x <= 0)
2966 view->count = 0;
2967 break;
2968 case 'k':
2969 case KEY_UP:
2970 case '<':
2971 case ',':
2972 case CTRL('p'):
2973 log_move_cursor_up(view, 0, 0);
2974 break;
2975 case 'g':
2976 case KEY_HOME:
2977 log_move_cursor_up(view, 0, 1);
2978 view->count = 0;
2979 break;
2980 case CTRL('u'):
2981 case 'u':
2982 nscroll /= 2;
2983 /* FALL THROUGH */
2984 case KEY_PPAGE:
2985 case CTRL('b'):
2986 case 'b':
2987 log_move_cursor_up(view, nscroll, 0);
2988 break;
2989 case 'j':
2990 case KEY_DOWN:
2991 case '>':
2992 case '.':
2993 case CTRL('n'):
2994 err = log_move_cursor_down(view, 0);
2995 break;
2996 case 'G':
2997 case KEY_END: {
2998 /* We don't know yet how many commits, so we're forced to
2999 * traverse them all. */
3000 view->count = 0;
3001 if (!s->thread_args.log_complete) {
3002 s->thread_args.load_all = 1;
3003 return trigger_log_thread(view, 0);
3006 s->selected = 0;
3007 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3008 for (n = 0; n < eos; n++) {
3009 if (entry == NULL)
3010 break;
3011 s->first_displayed_entry = entry;
3012 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3014 if (n > 0)
3015 s->selected = n - 1;
3016 select_commit(s);
3017 break;
3019 case CTRL('d'):
3020 case 'd':
3021 nscroll /= 2;
3022 /* FALL THROUGH */
3023 case KEY_NPAGE:
3024 case CTRL('f'):
3025 case 'f':
3026 case ' ':
3027 err = log_move_cursor_down(view, nscroll);
3028 break;
3029 case KEY_RESIZE:
3030 if (s->selected > view->nlines - 2)
3031 s->selected = view->nlines - 2;
3032 if (s->selected > s->commits.ncommits - 1)
3033 s->selected = s->commits.ncommits - 1;
3034 select_commit(s);
3035 if (s->commits.ncommits < view->nlines - 1 &&
3036 !s->thread_args.log_complete) {
3037 s->thread_args.commits_needed += (view->nlines - 1) -
3038 s->commits.ncommits;
3039 err = trigger_log_thread(view, 1);
3041 break;
3042 case KEY_ENTER:
3043 case '\r': {
3044 view->count = 0;
3045 if (s->selected_entry == NULL)
3046 break;
3048 /* get dimensions--don't split till initialisation succeeds */
3049 if (view_is_parent_view(view))
3050 view_get_split(view, &begin_y, &begin_x);
3052 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3053 s->selected_entry->commit, s->selected_entry->id,
3054 view, s->repo);
3055 if (err)
3056 break;
3058 if (view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3059 err = view_init_hsplit(view, begin_y);
3060 if (err)
3061 break;
3064 view->focussed = 0;
3065 diff_view->focussed = 1;
3066 diff_view->mode = view->mode;
3067 diff_view->nlines = view->lines - begin_y;
3069 if (view_is_parent_view(view)) {
3070 err = view_close_child(view);
3071 if (err)
3072 return err;
3073 err = view_set_child(view, diff_view);
3074 if (err)
3075 return err;
3076 view->focus_child = 1;
3077 } else
3078 *new_view = diff_view;
3079 break;
3081 case 't':
3082 view->count = 0;
3083 if (s->selected_entry == NULL)
3084 break;
3085 if (view_is_parent_view(view))
3086 begin_x = view_split_begin_x(view->begin_x);
3087 err = browse_commit_tree(&tree_view, begin_x,
3088 s->selected_entry, s->in_repo_path, s->head_ref_name,
3089 s->repo);
3090 if (err)
3091 break;
3092 view->focussed = 0;
3093 tree_view->focussed = 1;
3094 if (view_is_parent_view(view)) {
3095 err = view_close_child(view);
3096 if (err)
3097 return err;
3098 err = view_set_child(view, tree_view);
3099 if (err)
3100 return err;
3101 view->focus_child = 1;
3102 } else
3103 *new_view = tree_view;
3104 break;
3105 case KEY_BACKSPACE:
3106 case CTRL('l'):
3107 case 'B':
3108 view->count = 0;
3109 if (ch == KEY_BACKSPACE &&
3110 got_path_is_root_dir(s->in_repo_path))
3111 break;
3112 err = stop_log_thread(s);
3113 if (err)
3114 return err;
3115 if (ch == KEY_BACKSPACE) {
3116 char *parent_path;
3117 err = got_path_dirname(&parent_path, s->in_repo_path);
3118 if (err)
3119 return err;
3120 free(s->in_repo_path);
3121 s->in_repo_path = parent_path;
3122 s->thread_args.in_repo_path = s->in_repo_path;
3123 } else if (ch == CTRL('l')) {
3124 struct got_object_id *start_id;
3125 err = got_repo_match_object_id(&start_id, NULL,
3126 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3127 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3128 if (err)
3129 return err;
3130 free(s->start_id);
3131 s->start_id = start_id;
3132 s->thread_args.start_id = s->start_id;
3133 } else /* 'B' */
3134 s->log_branches = !s->log_branches;
3136 if (s->thread_args.pack_fds == NULL) {
3137 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3138 if (err)
3139 return err;
3141 err = got_repo_open(&s->thread_args.repo,
3142 got_repo_get_path(s->repo), NULL,
3143 s->thread_args.pack_fds);
3144 if (err)
3145 return err;
3146 tog_free_refs();
3147 err = tog_load_refs(s->repo, 0);
3148 if (err)
3149 return err;
3150 err = got_commit_graph_open(&s->thread_args.graph,
3151 s->in_repo_path, !s->log_branches);
3152 if (err)
3153 return err;
3154 err = got_commit_graph_iter_start(s->thread_args.graph,
3155 s->start_id, s->repo, NULL, NULL);
3156 if (err)
3157 return err;
3158 free_commits(&s->commits);
3159 s->first_displayed_entry = NULL;
3160 s->last_displayed_entry = NULL;
3161 s->selected_entry = NULL;
3162 s->selected = 0;
3163 s->thread_args.log_complete = 0;
3164 s->quit = 0;
3165 s->thread_args.commits_needed = view->lines;
3166 s->matched_entry = NULL;
3167 s->search_entry = NULL;
3168 break;
3169 case 'r':
3170 view->count = 0;
3171 if (view_is_parent_view(view))
3172 begin_x = view_split_begin_x(view->begin_x);
3173 ref_view = view_open(view->nlines, view->ncols,
3174 view->begin_y, begin_x, TOG_VIEW_REF);
3175 if (ref_view == NULL)
3176 return got_error_from_errno("view_open");
3177 err = open_ref_view(ref_view, s->repo);
3178 if (err) {
3179 view_close(ref_view);
3180 return err;
3182 view->focussed = 0;
3183 ref_view->focussed = 1;
3184 if (view_is_parent_view(view)) {
3185 err = view_close_child(view);
3186 if (err)
3187 return err;
3188 err = view_set_child(view, ref_view);
3189 if (err)
3190 return err;
3191 view->focus_child = 1;
3192 } else
3193 *new_view = ref_view;
3194 break;
3195 default:
3196 view->count = 0;
3197 break;
3200 return err;
3203 static const struct got_error *
3204 apply_unveil(const char *repo_path, const char *worktree_path)
3206 const struct got_error *error;
3208 #ifdef PROFILE
3209 if (unveil("gmon.out", "rwc") != 0)
3210 return got_error_from_errno2("unveil", "gmon.out");
3211 #endif
3212 if (repo_path && unveil(repo_path, "r") != 0)
3213 return got_error_from_errno2("unveil", repo_path);
3215 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3216 return got_error_from_errno2("unveil", worktree_path);
3218 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3219 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3221 error = got_privsep_unveil_exec_helpers();
3222 if (error != NULL)
3223 return error;
3225 if (unveil(NULL, NULL) != 0)
3226 return got_error_from_errno("unveil");
3228 return NULL;
3231 static void
3232 init_curses(void)
3235 * Override default signal handlers before starting ncurses.
3236 * This should prevent ncurses from installing its own
3237 * broken cleanup() signal handler.
3239 signal(SIGWINCH, tog_sigwinch);
3240 signal(SIGPIPE, tog_sigpipe);
3241 signal(SIGCONT, tog_sigcont);
3242 signal(SIGINT, tog_sigint);
3243 signal(SIGTERM, tog_sigterm);
3245 initscr();
3246 cbreak();
3247 halfdelay(1); /* Do fast refresh while initial view is loading. */
3248 noecho();
3249 nonl();
3250 intrflush(stdscr, FALSE);
3251 keypad(stdscr, TRUE);
3252 curs_set(0);
3253 if (getenv("TOG_COLORS") != NULL) {
3254 start_color();
3255 use_default_colors();
3259 static const struct got_error *
3260 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3261 struct got_repository *repo, struct got_worktree *worktree)
3263 const struct got_error *err = NULL;
3265 if (argc == 0) {
3266 *in_repo_path = strdup("/");
3267 if (*in_repo_path == NULL)
3268 return got_error_from_errno("strdup");
3269 return NULL;
3272 if (worktree) {
3273 const char *prefix = got_worktree_get_path_prefix(worktree);
3274 char *p;
3276 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3277 if (err)
3278 return err;
3279 if (asprintf(in_repo_path, "%s%s%s", prefix,
3280 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3281 p) == -1) {
3282 err = got_error_from_errno("asprintf");
3283 *in_repo_path = NULL;
3285 free(p);
3286 } else
3287 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3289 return err;
3292 static const struct got_error *
3293 cmd_log(int argc, char *argv[])
3295 const struct got_error *error;
3296 struct got_repository *repo = NULL;
3297 struct got_worktree *worktree = NULL;
3298 struct got_object_id *start_id = NULL;
3299 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3300 char *start_commit = NULL, *label = NULL;
3301 struct got_reference *ref = NULL;
3302 const char *head_ref_name = NULL;
3303 int ch, log_branches = 0;
3304 struct tog_view *view;
3305 int *pack_fds = NULL;
3307 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3308 switch (ch) {
3309 case 'b':
3310 log_branches = 1;
3311 break;
3312 case 'c':
3313 start_commit = optarg;
3314 break;
3315 case 'r':
3316 repo_path = realpath(optarg, NULL);
3317 if (repo_path == NULL)
3318 return got_error_from_errno2("realpath",
3319 optarg);
3320 break;
3321 default:
3322 usage_log();
3323 /* NOTREACHED */
3327 argc -= optind;
3328 argv += optind;
3330 if (argc > 1)
3331 usage_log();
3333 error = got_repo_pack_fds_open(&pack_fds);
3334 if (error != NULL)
3335 goto done;
3337 if (repo_path == NULL) {
3338 cwd = getcwd(NULL, 0);
3339 if (cwd == NULL)
3340 return got_error_from_errno("getcwd");
3341 error = got_worktree_open(&worktree, cwd);
3342 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3343 goto done;
3344 if (worktree)
3345 repo_path =
3346 strdup(got_worktree_get_repo_path(worktree));
3347 else
3348 repo_path = strdup(cwd);
3349 if (repo_path == NULL) {
3350 error = got_error_from_errno("strdup");
3351 goto done;
3355 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3356 if (error != NULL)
3357 goto done;
3359 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3360 repo, worktree);
3361 if (error)
3362 goto done;
3364 init_curses();
3366 error = apply_unveil(got_repo_get_path(repo),
3367 worktree ? got_worktree_get_root_path(worktree) : NULL);
3368 if (error)
3369 goto done;
3371 /* already loaded by tog_log_with_path()? */
3372 if (TAILQ_EMPTY(&tog_refs)) {
3373 error = tog_load_refs(repo, 0);
3374 if (error)
3375 goto done;
3378 if (start_commit == NULL) {
3379 error = got_repo_match_object_id(&start_id, &label,
3380 worktree ? got_worktree_get_head_ref_name(worktree) :
3381 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3382 if (error)
3383 goto done;
3384 head_ref_name = label;
3385 } else {
3386 error = got_ref_open(&ref, repo, start_commit, 0);
3387 if (error == NULL)
3388 head_ref_name = got_ref_get_name(ref);
3389 else if (error->code != GOT_ERR_NOT_REF)
3390 goto done;
3391 error = got_repo_match_object_id(&start_id, NULL,
3392 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3393 if (error)
3394 goto done;
3397 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3398 if (view == NULL) {
3399 error = got_error_from_errno("view_open");
3400 goto done;
3402 error = open_log_view(view, start_id, repo, head_ref_name,
3403 in_repo_path, log_branches);
3404 if (error)
3405 goto done;
3406 if (worktree) {
3407 /* Release work tree lock. */
3408 got_worktree_close(worktree);
3409 worktree = NULL;
3411 error = view_loop(view);
3412 done:
3413 free(in_repo_path);
3414 free(repo_path);
3415 free(cwd);
3416 free(start_id);
3417 free(label);
3418 if (ref)
3419 got_ref_close(ref);
3420 if (repo) {
3421 const struct got_error *close_err = got_repo_close(repo);
3422 if (error == NULL)
3423 error = close_err;
3425 if (worktree)
3426 got_worktree_close(worktree);
3427 if (pack_fds) {
3428 const struct got_error *pack_err =
3429 got_repo_pack_fds_close(pack_fds);
3430 if (error == NULL)
3431 error = pack_err;
3433 tog_free_refs();
3434 return error;
3437 __dead static void
3438 usage_diff(void)
3440 endwin();
3441 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3442 "[-w] object1 object2\n", getprogname());
3443 exit(1);
3446 static int
3447 match_line(const char *line, regex_t *regex, size_t nmatch,
3448 regmatch_t *regmatch)
3450 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3453 static struct tog_color *
3454 match_color(struct tog_colors *colors, const char *line)
3456 struct tog_color *tc = NULL;
3458 STAILQ_FOREACH(tc, colors, entry) {
3459 if (match_line(line, &tc->regex, 0, NULL))
3460 return tc;
3463 return NULL;
3466 static const struct got_error *
3467 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3468 WINDOW *window, int skipcol, regmatch_t *regmatch)
3470 const struct got_error *err = NULL;
3471 char *exstr = NULL;
3472 wchar_t *wline = NULL;
3473 int rme, rms, n, width, scrollx;
3474 int width0 = 0, width1 = 0, width2 = 0;
3475 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3477 *wtotal = 0;
3479 rms = regmatch->rm_so;
3480 rme = regmatch->rm_eo;
3482 err = expand_tab(&exstr, line);
3483 if (err)
3484 return err;
3486 /* Split the line into 3 segments, according to match offsets. */
3487 seg0 = strndup(exstr, rms);
3488 if (seg0 == NULL) {
3489 err = got_error_from_errno("strndup");
3490 goto done;
3492 seg1 = strndup(exstr + rms, rme - rms);
3493 if (seg1 == NULL) {
3494 err = got_error_from_errno("strndup");
3495 goto done;
3497 seg2 = strdup(exstr + rme);
3498 if (seg2 == NULL) {
3499 err = got_error_from_errno("strndup");
3500 goto done;
3503 /* draw up to matched token if we haven't scrolled past it */
3504 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3505 col_tab_align, 1);
3506 if (err)
3507 goto done;
3508 n = MAX(width0 - skipcol, 0);
3509 if (n) {
3510 free(wline);
3511 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3512 wlimit, col_tab_align, 1);
3513 if (err)
3514 goto done;
3515 waddwstr(window, &wline[scrollx]);
3516 wlimit -= width;
3517 *wtotal += width;
3520 if (wlimit > 0) {
3521 int i = 0, w = 0;
3522 size_t wlen;
3524 free(wline);
3525 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3526 col_tab_align, 1);
3527 if (err)
3528 goto done;
3529 wlen = wcslen(wline);
3530 while (i < wlen) {
3531 width = wcwidth(wline[i]);
3532 if (width == -1) {
3533 /* should not happen, tabs are expanded */
3534 err = got_error(GOT_ERR_RANGE);
3535 goto done;
3537 if (width0 + w + width > skipcol)
3538 break;
3539 w += width;
3540 i++;
3542 /* draw (visible part of) matched token (if scrolled into it) */
3543 if (width1 - w > 0) {
3544 wattron(window, A_STANDOUT);
3545 waddwstr(window, &wline[i]);
3546 wattroff(window, A_STANDOUT);
3547 wlimit -= (width1 - w);
3548 *wtotal += (width1 - w);
3552 if (wlimit > 0) { /* draw rest of line */
3553 free(wline);
3554 if (skipcol > width0 + width1) {
3555 err = format_line(&wline, &width2, &scrollx, seg2,
3556 skipcol - (width0 + width1), wlimit,
3557 col_tab_align, 1);
3558 if (err)
3559 goto done;
3560 waddwstr(window, &wline[scrollx]);
3561 } else {
3562 err = format_line(&wline, &width2, NULL, seg2, 0,
3563 wlimit, col_tab_align, 1);
3564 if (err)
3565 goto done;
3566 waddwstr(window, wline);
3568 *wtotal += width2;
3570 done:
3571 free(wline);
3572 free(exstr);
3573 free(seg0);
3574 free(seg1);
3575 free(seg2);
3576 return err;
3579 static const struct got_error *
3580 draw_file(struct tog_view *view, const char *header)
3582 struct tog_diff_view_state *s = &view->state.diff;
3583 regmatch_t *regmatch = &view->regmatch;
3584 const struct got_error *err;
3585 int nprinted = 0;
3586 char *line;
3587 size_t linesize = 0;
3588 ssize_t linelen;
3589 struct tog_color *tc;
3590 wchar_t *wline;
3591 int width;
3592 int max_lines = view->nlines;
3593 int nlines = s->nlines;
3594 off_t line_offset;
3596 line_offset = s->line_offsets[s->first_displayed_line - 1];
3597 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3598 return got_error_from_errno("fseek");
3600 werase(view->window);
3602 if (header) {
3603 if (asprintf(&line, "[%d/%d] %s",
3604 s->first_displayed_line - 1 + s->selected_line, nlines,
3605 header) == -1)
3606 return got_error_from_errno("asprintf");
3607 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3608 0, 0);
3609 free(line);
3610 if (err)
3611 return err;
3613 if (view_needs_focus_indication(view))
3614 wstandout(view->window);
3615 waddwstr(view->window, wline);
3616 free(wline);
3617 wline = NULL;
3618 if (view_needs_focus_indication(view))
3619 wstandend(view->window);
3620 if (width <= view->ncols - 1)
3621 waddch(view->window, '\n');
3623 if (max_lines <= 1)
3624 return NULL;
3625 max_lines--;
3628 s->eof = 0;
3629 view->maxx = 0;
3630 line = NULL;
3631 while (max_lines > 0 && nprinted < max_lines) {
3632 linelen = getline(&line, &linesize, s->f);
3633 if (linelen == -1) {
3634 if (feof(s->f)) {
3635 s->eof = 1;
3636 break;
3638 free(line);
3639 return got_ferror(s->f, GOT_ERR_IO);
3642 /* Set view->maxx based on full line length. */
3643 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3644 view->x ? 1 : 0);
3645 if (err) {
3646 free(line);
3647 return err;
3649 view->maxx = MAX(view->maxx, width);
3650 free(wline);
3651 wline = NULL;
3653 tc = match_color(&s->colors, line);
3654 if (tc)
3655 wattr_on(view->window,
3656 COLOR_PAIR(tc->colorpair), NULL);
3657 if (s->first_displayed_line + nprinted == s->matched_line &&
3658 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3659 err = add_matched_line(&width, line, view->ncols, 0,
3660 view->window, view->x, regmatch);
3661 if (err) {
3662 free(line);
3663 return err;
3665 } else {
3666 int skip;
3667 err = format_line(&wline, &width, &skip, line,
3668 view->x, view->ncols, 0, view->x ? 1 : 0);
3669 if (err) {
3670 free(line);
3671 return err;
3673 waddwstr(view->window, &wline[skip]);
3674 free(wline);
3675 wline = NULL;
3677 if (tc)
3678 wattr_off(view->window,
3679 COLOR_PAIR(tc->colorpair), NULL);
3680 if (width <= view->ncols - 1)
3681 waddch(view->window, '\n');
3682 nprinted++;
3684 free(line);
3685 if (nprinted >= 1)
3686 s->last_displayed_line = s->first_displayed_line +
3687 (nprinted - 1);
3688 else
3689 s->last_displayed_line = s->first_displayed_line;
3691 view_border(view);
3693 if (s->eof) {
3694 while (nprinted < view->nlines) {
3695 waddch(view->window, '\n');
3696 nprinted++;
3699 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3700 view->ncols, 0, 0);
3701 if (err) {
3702 return err;
3705 wstandout(view->window);
3706 waddwstr(view->window, wline);
3707 free(wline);
3708 wline = NULL;
3709 wstandend(view->window);
3712 return NULL;
3715 static char *
3716 get_datestr(time_t *time, char *datebuf)
3718 struct tm mytm, *tm;
3719 char *p, *s;
3721 tm = gmtime_r(time, &mytm);
3722 if (tm == NULL)
3723 return NULL;
3724 s = asctime_r(tm, datebuf);
3725 if (s == NULL)
3726 return NULL;
3727 p = strchr(s, '\n');
3728 if (p)
3729 *p = '\0';
3730 return s;
3733 static const struct got_error *
3734 get_changed_paths(struct got_pathlist_head *paths,
3735 struct got_commit_object *commit, struct got_repository *repo)
3737 const struct got_error *err = NULL;
3738 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3739 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3740 struct got_object_qid *qid;
3742 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3743 if (qid != NULL) {
3744 struct got_commit_object *pcommit;
3745 err = got_object_open_as_commit(&pcommit, repo,
3746 &qid->id);
3747 if (err)
3748 return err;
3750 tree_id1 = got_object_id_dup(
3751 got_object_commit_get_tree_id(pcommit));
3752 if (tree_id1 == NULL) {
3753 got_object_commit_close(pcommit);
3754 return got_error_from_errno("got_object_id_dup");
3756 got_object_commit_close(pcommit);
3760 if (tree_id1) {
3761 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3762 if (err)
3763 goto done;
3766 tree_id2 = got_object_commit_get_tree_id(commit);
3767 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3768 if (err)
3769 goto done;
3771 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3772 got_diff_tree_collect_changed_paths, paths, 0);
3773 done:
3774 if (tree1)
3775 got_object_tree_close(tree1);
3776 if (tree2)
3777 got_object_tree_close(tree2);
3778 free(tree_id1);
3779 return err;
3782 static const struct got_error *
3783 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3785 off_t *p;
3787 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3788 if (p == NULL)
3789 return got_error_from_errno("reallocarray");
3790 *line_offsets = p;
3791 (*line_offsets)[*nlines] = off;
3792 (*nlines)++;
3793 return NULL;
3796 static const struct got_error *
3797 write_commit_info(off_t **line_offsets, size_t *nlines,
3798 struct got_object_id *commit_id, struct got_reflist_head *refs,
3799 struct got_repository *repo, FILE *outfile)
3801 const struct got_error *err = NULL;
3802 char datebuf[26], *datestr;
3803 struct got_commit_object *commit;
3804 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3805 time_t committer_time;
3806 const char *author, *committer;
3807 char *refs_str = NULL;
3808 struct got_pathlist_head changed_paths;
3809 struct got_pathlist_entry *pe;
3810 off_t outoff = 0;
3811 int n;
3813 TAILQ_INIT(&changed_paths);
3815 if (refs) {
3816 err = build_refs_str(&refs_str, refs, commit_id, repo);
3817 if (err)
3818 return err;
3821 err = got_object_open_as_commit(&commit, repo, commit_id);
3822 if (err)
3823 return err;
3825 err = got_object_id_str(&id_str, commit_id);
3826 if (err) {
3827 err = got_error_from_errno("got_object_id_str");
3828 goto done;
3831 err = add_line_offset(line_offsets, nlines, 0);
3832 if (err)
3833 goto done;
3835 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3836 refs_str ? refs_str : "", refs_str ? ")" : "");
3837 if (n < 0) {
3838 err = got_error_from_errno("fprintf");
3839 goto done;
3841 outoff += n;
3842 err = add_line_offset(line_offsets, nlines, outoff);
3843 if (err)
3844 goto done;
3846 n = fprintf(outfile, "from: %s\n",
3847 got_object_commit_get_author(commit));
3848 if (n < 0) {
3849 err = got_error_from_errno("fprintf");
3850 goto done;
3852 outoff += n;
3853 err = add_line_offset(line_offsets, nlines, outoff);
3854 if (err)
3855 goto done;
3857 committer_time = got_object_commit_get_committer_time(commit);
3858 datestr = get_datestr(&committer_time, datebuf);
3859 if (datestr) {
3860 n = fprintf(outfile, "date: %s UTC\n", datestr);
3861 if (n < 0) {
3862 err = got_error_from_errno("fprintf");
3863 goto done;
3865 outoff += n;
3866 err = add_line_offset(line_offsets, nlines, outoff);
3867 if (err)
3868 goto done;
3870 author = got_object_commit_get_author(commit);
3871 committer = got_object_commit_get_committer(commit);
3872 if (strcmp(author, committer) != 0) {
3873 n = fprintf(outfile, "via: %s\n", committer);
3874 if (n < 0) {
3875 err = got_error_from_errno("fprintf");
3876 goto done;
3878 outoff += n;
3879 err = add_line_offset(line_offsets, nlines, outoff);
3880 if (err)
3881 goto done;
3883 if (got_object_commit_get_nparents(commit) > 1) {
3884 const struct got_object_id_queue *parent_ids;
3885 struct got_object_qid *qid;
3886 int pn = 1;
3887 parent_ids = got_object_commit_get_parent_ids(commit);
3888 STAILQ_FOREACH(qid, parent_ids, entry) {
3889 err = got_object_id_str(&id_str, &qid->id);
3890 if (err)
3891 goto done;
3892 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3893 if (n < 0) {
3894 err = got_error_from_errno("fprintf");
3895 goto done;
3897 outoff += n;
3898 err = add_line_offset(line_offsets, nlines, outoff);
3899 if (err)
3900 goto done;
3901 free(id_str);
3902 id_str = NULL;
3906 err = got_object_commit_get_logmsg(&logmsg, commit);
3907 if (err)
3908 goto done;
3909 s = logmsg;
3910 while ((line = strsep(&s, "\n")) != NULL) {
3911 n = fprintf(outfile, "%s\n", line);
3912 if (n < 0) {
3913 err = got_error_from_errno("fprintf");
3914 goto done;
3916 outoff += n;
3917 err = add_line_offset(line_offsets, nlines, outoff);
3918 if (err)
3919 goto done;
3922 err = get_changed_paths(&changed_paths, commit, repo);
3923 if (err)
3924 goto done;
3925 TAILQ_FOREACH(pe, &changed_paths, entry) {
3926 struct got_diff_changed_path *cp = pe->data;
3927 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3928 if (n < 0) {
3929 err = got_error_from_errno("fprintf");
3930 goto done;
3932 outoff += n;
3933 err = add_line_offset(line_offsets, nlines, outoff);
3934 if (err)
3935 goto done;
3936 free((char *)pe->path);
3937 free(pe->data);
3940 fputc('\n', outfile);
3941 outoff++;
3942 err = add_line_offset(line_offsets, nlines, outoff);
3943 done:
3944 got_pathlist_free(&changed_paths);
3945 free(id_str);
3946 free(logmsg);
3947 free(refs_str);
3948 got_object_commit_close(commit);
3949 if (err) {
3950 free(*line_offsets);
3951 *line_offsets = NULL;
3952 *nlines = 0;
3954 return err;
3957 static const struct got_error *
3958 create_diff(struct tog_diff_view_state *s)
3960 const struct got_error *err = NULL;
3961 FILE *f = NULL;
3962 int obj_type;
3964 free(s->line_offsets);
3965 s->line_offsets = malloc(sizeof(off_t));
3966 if (s->line_offsets == NULL)
3967 return got_error_from_errno("malloc");
3968 s->nlines = 0;
3970 f = got_opentemp();
3971 if (f == NULL) {
3972 err = got_error_from_errno("got_opentemp");
3973 goto done;
3975 if (s->f && fclose(s->f) == EOF) {
3976 err = got_error_from_errno("fclose");
3977 goto done;
3979 s->f = f;
3981 if (s->id1)
3982 err = got_object_get_type(&obj_type, s->repo, s->id1);
3983 else
3984 err = got_object_get_type(&obj_type, s->repo, s->id2);
3985 if (err)
3986 goto done;
3988 switch (obj_type) {
3989 case GOT_OBJ_TYPE_BLOB:
3990 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3991 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
3992 s->label1, s->label2, tog_diff_algo, s->diff_context,
3993 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3994 break;
3995 case GOT_OBJ_TYPE_TREE:
3996 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3997 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
3998 tog_diff_algo, s->diff_context, s->ignore_whitespace,
3999 s->force_text_diff, s->repo, s->f);
4000 break;
4001 case GOT_OBJ_TYPE_COMMIT: {
4002 const struct got_object_id_queue *parent_ids;
4003 struct got_object_qid *pid;
4004 struct got_commit_object *commit2;
4005 struct got_reflist_head *refs;
4007 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4008 if (err)
4009 goto done;
4010 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4011 /* Show commit info if we're diffing to a parent/root commit. */
4012 if (s->id1 == NULL) {
4013 err = write_commit_info(&s->line_offsets, &s->nlines,
4014 s->id2, refs, s->repo, s->f);
4015 if (err)
4016 goto done;
4017 } else {
4018 parent_ids = got_object_commit_get_parent_ids(commit2);
4019 STAILQ_FOREACH(pid, parent_ids, entry) {
4020 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4021 err = write_commit_info(
4022 &s->line_offsets, &s->nlines,
4023 s->id2, refs, s->repo, s->f);
4024 if (err)
4025 goto done;
4026 break;
4030 got_object_commit_close(commit2);
4032 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4033 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4034 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4035 s->force_text_diff, s->repo, s->f);
4036 break;
4038 default:
4039 err = got_error(GOT_ERR_OBJ_TYPE);
4040 break;
4042 if (err)
4043 goto done;
4044 done:
4045 if (s->f && fflush(s->f) != 0 && err == NULL)
4046 err = got_error_from_errno("fflush");
4047 return err;
4050 static void
4051 diff_view_indicate_progress(struct tog_view *view)
4053 mvwaddstr(view->window, 0, 0, "diffing...");
4054 update_panels();
4055 doupdate();
4058 static const struct got_error *
4059 search_start_diff_view(struct tog_view *view)
4061 struct tog_diff_view_state *s = &view->state.diff;
4063 s->matched_line = 0;
4064 return NULL;
4067 static const struct got_error *
4068 search_next_diff_view(struct tog_view *view)
4070 struct tog_diff_view_state *s = &view->state.diff;
4071 const struct got_error *err = NULL;
4072 int lineno;
4073 char *line = NULL;
4074 size_t linesize = 0;
4075 ssize_t linelen;
4077 if (!view->searching) {
4078 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4079 return NULL;
4082 if (s->matched_line) {
4083 if (view->searching == TOG_SEARCH_FORWARD)
4084 lineno = s->matched_line + 1;
4085 else
4086 lineno = s->matched_line - 1;
4087 } else
4088 lineno = s->first_displayed_line;
4090 while (1) {
4091 off_t offset;
4093 if (lineno <= 0 || lineno > s->nlines) {
4094 if (s->matched_line == 0) {
4095 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4096 break;
4099 if (view->searching == TOG_SEARCH_FORWARD)
4100 lineno = 1;
4101 else
4102 lineno = s->nlines;
4105 offset = s->line_offsets[lineno - 1];
4106 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4107 free(line);
4108 return got_error_from_errno("fseeko");
4110 linelen = getline(&line, &linesize, s->f);
4111 if (linelen != -1) {
4112 char *exstr;
4113 err = expand_tab(&exstr, line);
4114 if (err)
4115 break;
4116 if (match_line(exstr, &view->regex, 1,
4117 &view->regmatch)) {
4118 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4119 s->matched_line = lineno;
4120 free(exstr);
4121 break;
4123 free(exstr);
4125 if (view->searching == TOG_SEARCH_FORWARD)
4126 lineno++;
4127 else
4128 lineno--;
4130 free(line);
4132 if (s->matched_line) {
4133 s->first_displayed_line = s->matched_line;
4134 s->selected_line = 1;
4137 return err;
4140 static const struct got_error *
4141 close_diff_view(struct tog_view *view)
4143 const struct got_error *err = NULL;
4144 struct tog_diff_view_state *s = &view->state.diff;
4146 free(s->id1);
4147 s->id1 = NULL;
4148 free(s->id2);
4149 s->id2 = NULL;
4150 if (s->f && fclose(s->f) == EOF)
4151 err = got_error_from_errno("fclose");
4152 s->f = NULL;
4153 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4154 err = got_error_from_errno("fclose");
4155 s->f1 = NULL;
4156 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4157 err = got_error_from_errno("fclose");
4158 s->f2 = NULL;
4159 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4160 err = got_error_from_errno("close");
4161 s->fd1 = -1;
4162 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4163 err = got_error_from_errno("close");
4164 s->fd2 = -1;
4165 free_colors(&s->colors);
4166 free(s->line_offsets);
4167 s->line_offsets = NULL;
4168 s->nlines = 0;
4169 return err;
4172 static const struct got_error *
4173 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4174 struct got_object_id *id2, const char *label1, const char *label2,
4175 int diff_context, int ignore_whitespace, int force_text_diff,
4176 struct tog_view *log_view, struct got_repository *repo)
4178 const struct got_error *err;
4179 struct tog_diff_view_state *s = &view->state.diff;
4181 memset(s, 0, sizeof(*s));
4182 s->fd1 = -1;
4183 s->fd2 = -1;
4185 if (id1 != NULL && id2 != NULL) {
4186 int type1, type2;
4187 err = got_object_get_type(&type1, repo, id1);
4188 if (err)
4189 return err;
4190 err = got_object_get_type(&type2, repo, id2);
4191 if (err)
4192 return err;
4194 if (type1 != type2)
4195 return got_error(GOT_ERR_OBJ_TYPE);
4197 s->first_displayed_line = 1;
4198 s->last_displayed_line = view->nlines;
4199 s->selected_line = 1;
4200 s->repo = repo;
4201 s->id1 = id1;
4202 s->id2 = id2;
4203 s->label1 = label1;
4204 s->label2 = label2;
4206 if (id1) {
4207 s->id1 = got_object_id_dup(id1);
4208 if (s->id1 == NULL)
4209 return got_error_from_errno("got_object_id_dup");
4210 } else
4211 s->id1 = NULL;
4213 s->id2 = got_object_id_dup(id2);
4214 if (s->id2 == NULL) {
4215 err = got_error_from_errno("got_object_id_dup");
4216 goto done;
4219 s->f1 = got_opentemp();
4220 if (s->f1 == NULL) {
4221 err = got_error_from_errno("got_opentemp");
4222 goto done;
4225 s->f2 = got_opentemp();
4226 if (s->f2 == NULL) {
4227 err = got_error_from_errno("got_opentemp");
4228 goto done;
4231 s->fd1 = got_opentempfd();
4232 if (s->fd1 == -1) {
4233 err = got_error_from_errno("got_opentempfd");
4234 goto done;
4237 s->fd2 = got_opentempfd();
4238 if (s->fd2 == -1) {
4239 err = got_error_from_errno("got_opentempfd");
4240 goto done;
4243 s->first_displayed_line = 1;
4244 s->last_displayed_line = view->nlines;
4245 s->diff_context = diff_context;
4246 s->ignore_whitespace = ignore_whitespace;
4247 s->force_text_diff = force_text_diff;
4248 s->log_view = log_view;
4249 s->repo = repo;
4251 STAILQ_INIT(&s->colors);
4252 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4253 err = add_color(&s->colors,
4254 "^-", TOG_COLOR_DIFF_MINUS,
4255 get_color_value("TOG_COLOR_DIFF_MINUS"));
4256 if (err)
4257 goto done;
4258 err = add_color(&s->colors, "^\\+",
4259 TOG_COLOR_DIFF_PLUS,
4260 get_color_value("TOG_COLOR_DIFF_PLUS"));
4261 if (err)
4262 goto done;
4263 err = add_color(&s->colors,
4264 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4265 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4266 if (err)
4267 goto done;
4269 err = add_color(&s->colors,
4270 "^(commit [0-9a-f]|parent [0-9]|"
4271 "(blob|file|tree|commit) [-+] |"
4272 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4273 get_color_value("TOG_COLOR_DIFF_META"));
4274 if (err)
4275 goto done;
4277 err = add_color(&s->colors,
4278 "^(from|via): ", TOG_COLOR_AUTHOR,
4279 get_color_value("TOG_COLOR_AUTHOR"));
4280 if (err)
4281 goto done;
4283 err = add_color(&s->colors,
4284 "^date: ", TOG_COLOR_DATE,
4285 get_color_value("TOG_COLOR_DATE"));
4286 if (err)
4287 goto done;
4290 if (log_view && view_is_splitscreen(view))
4291 show_log_view(log_view); /* draw vborder */
4292 diff_view_indicate_progress(view);
4294 err = create_diff(s);
4296 view->show = show_diff_view;
4297 view->input = input_diff_view;
4298 view->reset = reset_diff_view;
4299 view->close = close_diff_view;
4300 view->search_start = search_start_diff_view;
4301 view->search_next = search_next_diff_view;
4302 done:
4303 if (err)
4304 close_diff_view(view);
4305 return err;
4308 static const struct got_error *
4309 show_diff_view(struct tog_view *view)
4311 const struct got_error *err;
4312 struct tog_diff_view_state *s = &view->state.diff;
4313 char *id_str1 = NULL, *id_str2, *header;
4314 const char *label1, *label2;
4316 if (s->id1) {
4317 err = got_object_id_str(&id_str1, s->id1);
4318 if (err)
4319 return err;
4320 label1 = s->label1 ? : id_str1;
4321 } else
4322 label1 = "/dev/null";
4324 err = got_object_id_str(&id_str2, s->id2);
4325 if (err)
4326 return err;
4327 label2 = s->label2 ? : id_str2;
4329 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4330 err = got_error_from_errno("asprintf");
4331 free(id_str1);
4332 free(id_str2);
4333 return err;
4335 free(id_str1);
4336 free(id_str2);
4338 err = draw_file(view, header);
4339 free(header);
4340 return err;
4343 static const struct got_error *
4344 set_selected_commit(struct tog_diff_view_state *s,
4345 struct commit_queue_entry *entry)
4347 const struct got_error *err;
4348 const struct got_object_id_queue *parent_ids;
4349 struct got_commit_object *selected_commit;
4350 struct got_object_qid *pid;
4352 free(s->id2);
4353 s->id2 = got_object_id_dup(entry->id);
4354 if (s->id2 == NULL)
4355 return got_error_from_errno("got_object_id_dup");
4357 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4358 if (err)
4359 return err;
4360 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4361 free(s->id1);
4362 pid = STAILQ_FIRST(parent_ids);
4363 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4364 got_object_commit_close(selected_commit);
4365 return NULL;
4368 static const struct got_error *
4369 reset_diff_view(struct tog_view *view)
4371 struct tog_diff_view_state *s = &view->state.diff;
4373 view->count = 0;
4374 wclear(view->window);
4375 s->first_displayed_line = 1;
4376 s->last_displayed_line = view->nlines;
4377 s->matched_line = 0;
4378 diff_view_indicate_progress(view);
4379 return create_diff(s);
4382 static const struct got_error *
4383 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4385 const struct got_error *err = NULL;
4386 struct tog_diff_view_state *s = &view->state.diff;
4387 struct tog_log_view_state *ls;
4388 struct commit_queue_entry *old_selected_entry;
4389 char *line = NULL;
4390 size_t linesize = 0;
4391 ssize_t linelen;
4392 int i, nscroll = view->nlines - 1;
4394 switch (ch) {
4395 case '0':
4396 view->x = 0;
4397 break;
4398 case '$':
4399 view->x = MAX(view->maxx - view->ncols / 3, 0);
4400 view->count = 0;
4401 break;
4402 case KEY_RIGHT:
4403 case 'l':
4404 if (view->x + view->ncols / 3 < view->maxx)
4405 view->x += 2; /* move two columns right */
4406 else
4407 view->count = 0;
4408 break;
4409 case KEY_LEFT:
4410 case 'h':
4411 view->x -= MIN(view->x, 2); /* move two columns back */
4412 if (view->x <= 0)
4413 view->count = 0;
4414 break;
4415 case 'a':
4416 case 'w':
4417 if (ch == 'a')
4418 s->force_text_diff = !s->force_text_diff;
4419 if (ch == 'w')
4420 s->ignore_whitespace = !s->ignore_whitespace;
4421 err = reset_diff_view(view);
4422 break;
4423 case 'g':
4424 case KEY_HOME:
4425 s->first_displayed_line = 1;
4426 view->count = 0;
4427 break;
4428 case 'G':
4429 case KEY_END:
4430 view->count = 0;
4431 if (s->eof)
4432 break;
4434 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4435 s->eof = 1;
4436 break;
4437 case 'k':
4438 case KEY_UP:
4439 case CTRL('p'):
4440 if (s->first_displayed_line > 1)
4441 s->first_displayed_line--;
4442 else
4443 view->count = 0;
4444 break;
4445 case CTRL('u'):
4446 case 'u':
4447 nscroll /= 2;
4448 /* FALL THROUGH */
4449 case KEY_PPAGE:
4450 case CTRL('b'):
4451 case 'b':
4452 if (s->first_displayed_line == 1) {
4453 view->count = 0;
4454 break;
4456 i = 0;
4457 while (i++ < nscroll && s->first_displayed_line > 1)
4458 s->first_displayed_line--;
4459 break;
4460 case 'j':
4461 case KEY_DOWN:
4462 case CTRL('n'):
4463 if (!s->eof)
4464 s->first_displayed_line++;
4465 else
4466 view->count = 0;
4467 break;
4468 case CTRL('d'):
4469 case 'd':
4470 nscroll /= 2;
4471 /* FALL THROUGH */
4472 case KEY_NPAGE:
4473 case CTRL('f'):
4474 case 'f':
4475 case ' ':
4476 if (s->eof) {
4477 view->count = 0;
4478 break;
4480 i = 0;
4481 while (!s->eof && i++ < nscroll) {
4482 linelen = getline(&line, &linesize, s->f);
4483 s->first_displayed_line++;
4484 if (linelen == -1) {
4485 if (feof(s->f)) {
4486 s->eof = 1;
4487 } else
4488 err = got_ferror(s->f, GOT_ERR_IO);
4489 break;
4492 free(line);
4493 break;
4494 case '[':
4495 if (s->diff_context > 0) {
4496 s->diff_context--;
4497 s->matched_line = 0;
4498 diff_view_indicate_progress(view);
4499 err = create_diff(s);
4500 if (s->first_displayed_line + view->nlines - 1 >
4501 s->nlines) {
4502 s->first_displayed_line = 1;
4503 s->last_displayed_line = view->nlines;
4505 } else
4506 view->count = 0;
4507 break;
4508 case ']':
4509 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4510 s->diff_context++;
4511 s->matched_line = 0;
4512 diff_view_indicate_progress(view);
4513 err = create_diff(s);
4514 } else
4515 view->count = 0;
4516 break;
4517 case '<':
4518 case ',':
4519 if (s->log_view == NULL) {
4520 view->count = 0;
4521 break;
4523 ls = &s->log_view->state.log;
4524 old_selected_entry = ls->selected_entry;
4526 /* view->count handled in input_log_view() */
4527 err = input_log_view(NULL, s->log_view, KEY_UP);
4528 if (err)
4529 break;
4531 if (old_selected_entry == ls->selected_entry)
4532 break;
4534 err = set_selected_commit(s, ls->selected_entry);
4535 if (err)
4536 break;
4538 s->first_displayed_line = 1;
4539 s->last_displayed_line = view->nlines;
4540 s->matched_line = 0;
4541 view->x = 0;
4543 diff_view_indicate_progress(view);
4544 err = create_diff(s);
4545 break;
4546 case '>':
4547 case '.':
4548 if (s->log_view == NULL) {
4549 view->count = 0;
4550 break;
4552 ls = &s->log_view->state.log;
4553 old_selected_entry = ls->selected_entry;
4555 /* view->count handled in input_log_view() */
4556 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4557 if (err)
4558 break;
4560 if (old_selected_entry == ls->selected_entry)
4561 break;
4563 err = set_selected_commit(s, ls->selected_entry);
4564 if (err)
4565 break;
4567 s->first_displayed_line = 1;
4568 s->last_displayed_line = view->nlines;
4569 s->matched_line = 0;
4570 view->x = 0;
4572 diff_view_indicate_progress(view);
4573 err = create_diff(s);
4574 break;
4575 default:
4576 view->count = 0;
4577 break;
4580 return err;
4583 static const struct got_error *
4584 cmd_diff(int argc, char *argv[])
4586 const struct got_error *error = NULL;
4587 struct got_repository *repo = NULL;
4588 struct got_worktree *worktree = NULL;
4589 struct got_object_id *id1 = NULL, *id2 = NULL;
4590 char *repo_path = NULL, *cwd = NULL;
4591 char *id_str1 = NULL, *id_str2 = NULL;
4592 char *label1 = NULL, *label2 = NULL;
4593 int diff_context = 3, ignore_whitespace = 0;
4594 int ch, force_text_diff = 0;
4595 const char *errstr;
4596 struct tog_view *view;
4597 int *pack_fds = NULL;
4599 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4600 switch (ch) {
4601 case 'a':
4602 force_text_diff = 1;
4603 break;
4604 case 'C':
4605 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4606 &errstr);
4607 if (errstr != NULL)
4608 errx(1, "number of context lines is %s: %s",
4609 errstr, errstr);
4610 break;
4611 case 'r':
4612 repo_path = realpath(optarg, NULL);
4613 if (repo_path == NULL)
4614 return got_error_from_errno2("realpath",
4615 optarg);
4616 got_path_strip_trailing_slashes(repo_path);
4617 break;
4618 case 'w':
4619 ignore_whitespace = 1;
4620 break;
4621 default:
4622 usage_diff();
4623 /* NOTREACHED */
4627 argc -= optind;
4628 argv += optind;
4630 if (argc == 0) {
4631 usage_diff(); /* TODO show local worktree changes */
4632 } else if (argc == 2) {
4633 id_str1 = argv[0];
4634 id_str2 = argv[1];
4635 } else
4636 usage_diff();
4638 error = got_repo_pack_fds_open(&pack_fds);
4639 if (error)
4640 goto done;
4642 if (repo_path == NULL) {
4643 cwd = getcwd(NULL, 0);
4644 if (cwd == NULL)
4645 return got_error_from_errno("getcwd");
4646 error = got_worktree_open(&worktree, cwd);
4647 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4648 goto done;
4649 if (worktree)
4650 repo_path =
4651 strdup(got_worktree_get_repo_path(worktree));
4652 else
4653 repo_path = strdup(cwd);
4654 if (repo_path == NULL) {
4655 error = got_error_from_errno("strdup");
4656 goto done;
4660 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4661 if (error)
4662 goto done;
4664 init_curses();
4666 error = apply_unveil(got_repo_get_path(repo), NULL);
4667 if (error)
4668 goto done;
4670 error = tog_load_refs(repo, 0);
4671 if (error)
4672 goto done;
4674 error = got_repo_match_object_id(&id1, &label1, id_str1,
4675 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4676 if (error)
4677 goto done;
4679 error = got_repo_match_object_id(&id2, &label2, id_str2,
4680 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4681 if (error)
4682 goto done;
4684 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4685 if (view == NULL) {
4686 error = got_error_from_errno("view_open");
4687 goto done;
4689 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4690 ignore_whitespace, force_text_diff, NULL, repo);
4691 if (error)
4692 goto done;
4693 error = view_loop(view);
4694 done:
4695 free(label1);
4696 free(label2);
4697 free(repo_path);
4698 free(cwd);
4699 if (repo) {
4700 const struct got_error *close_err = got_repo_close(repo);
4701 if (error == NULL)
4702 error = close_err;
4704 if (worktree)
4705 got_worktree_close(worktree);
4706 if (pack_fds) {
4707 const struct got_error *pack_err =
4708 got_repo_pack_fds_close(pack_fds);
4709 if (error == NULL)
4710 error = pack_err;
4712 tog_free_refs();
4713 return error;
4716 __dead static void
4717 usage_blame(void)
4719 endwin();
4720 fprintf(stderr,
4721 "usage: %s blame [-c commit] [-r repository-path] path\n",
4722 getprogname());
4723 exit(1);
4726 struct tog_blame_line {
4727 int annotated;
4728 struct got_object_id *id;
4731 static const struct got_error *
4732 draw_blame(struct tog_view *view)
4734 struct tog_blame_view_state *s = &view->state.blame;
4735 struct tog_blame *blame = &s->blame;
4736 regmatch_t *regmatch = &view->regmatch;
4737 const struct got_error *err;
4738 int lineno = 0, nprinted = 0;
4739 char *line = NULL;
4740 size_t linesize = 0;
4741 ssize_t linelen;
4742 wchar_t *wline;
4743 int width;
4744 struct tog_blame_line *blame_line;
4745 struct got_object_id *prev_id = NULL;
4746 char *id_str;
4747 struct tog_color *tc;
4749 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4750 if (err)
4751 return err;
4753 rewind(blame->f);
4754 werase(view->window);
4756 if (asprintf(&line, "commit %s", id_str) == -1) {
4757 err = got_error_from_errno("asprintf");
4758 free(id_str);
4759 return err;
4762 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4763 free(line);
4764 line = NULL;
4765 if (err)
4766 return err;
4767 if (view_needs_focus_indication(view))
4768 wstandout(view->window);
4769 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4770 if (tc)
4771 wattr_on(view->window,
4772 COLOR_PAIR(tc->colorpair), NULL);
4773 waddwstr(view->window, wline);
4774 if (tc)
4775 wattr_off(view->window,
4776 COLOR_PAIR(tc->colorpair), NULL);
4777 if (view_needs_focus_indication(view))
4778 wstandend(view->window);
4779 free(wline);
4780 wline = NULL;
4781 if (width < view->ncols - 1)
4782 waddch(view->window, '\n');
4784 if (asprintf(&line, "[%d/%d] %s%s",
4785 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4786 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4787 free(id_str);
4788 return got_error_from_errno("asprintf");
4790 free(id_str);
4791 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4792 free(line);
4793 line = NULL;
4794 if (err)
4795 return err;
4796 waddwstr(view->window, wline);
4797 free(wline);
4798 wline = NULL;
4799 if (width < view->ncols - 1)
4800 waddch(view->window, '\n');
4802 s->eof = 0;
4803 view->maxx = 0;
4804 while (nprinted < view->nlines - 2) {
4805 linelen = getline(&line, &linesize, blame->f);
4806 if (linelen == -1) {
4807 if (feof(blame->f)) {
4808 s->eof = 1;
4809 break;
4811 free(line);
4812 return got_ferror(blame->f, GOT_ERR_IO);
4814 if (++lineno < s->first_displayed_line)
4815 continue;
4817 /* Set view->maxx based on full line length. */
4818 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4819 if (err) {
4820 free(line);
4821 return err;
4823 free(wline);
4824 wline = NULL;
4825 view->maxx = MAX(view->maxx, width);
4827 if (view->focussed && nprinted == s->selected_line - 1)
4828 wstandout(view->window);
4830 if (blame->nlines > 0) {
4831 blame_line = &blame->lines[lineno - 1];
4832 if (blame_line->annotated && prev_id &&
4833 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4834 !(view->focussed &&
4835 nprinted == s->selected_line - 1)) {
4836 waddstr(view->window, " ");
4837 } else if (blame_line->annotated) {
4838 char *id_str;
4839 err = got_object_id_str(&id_str,
4840 blame_line->id);
4841 if (err) {
4842 free(line);
4843 return err;
4845 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4846 if (tc)
4847 wattr_on(view->window,
4848 COLOR_PAIR(tc->colorpair), NULL);
4849 wprintw(view->window, "%.8s", id_str);
4850 if (tc)
4851 wattr_off(view->window,
4852 COLOR_PAIR(tc->colorpair), NULL);
4853 free(id_str);
4854 prev_id = blame_line->id;
4855 } else {
4856 waddstr(view->window, "........");
4857 prev_id = NULL;
4859 } else {
4860 waddstr(view->window, "........");
4861 prev_id = NULL;
4864 if (view->focussed && nprinted == s->selected_line - 1)
4865 wstandend(view->window);
4866 waddstr(view->window, " ");
4868 if (view->ncols <= 9) {
4869 width = 9;
4870 } else if (s->first_displayed_line + nprinted ==
4871 s->matched_line &&
4872 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4873 err = add_matched_line(&width, line, view->ncols - 9, 9,
4874 view->window, view->x, regmatch);
4875 if (err) {
4876 free(line);
4877 return err;
4879 width += 9;
4880 } else {
4881 int skip;
4882 err = format_line(&wline, &width, &skip, line,
4883 view->x, view->ncols - 9, 9, 1);
4884 if (err) {
4885 free(line);
4886 return err;
4888 waddwstr(view->window, &wline[skip]);
4889 width += 9;
4890 free(wline);
4891 wline = NULL;
4894 if (width <= view->ncols - 1)
4895 waddch(view->window, '\n');
4896 if (++nprinted == 1)
4897 s->first_displayed_line = lineno;
4899 free(line);
4900 s->last_displayed_line = lineno;
4902 view_border(view);
4904 return NULL;
4907 static const struct got_error *
4908 blame_cb(void *arg, int nlines, int lineno,
4909 struct got_commit_object *commit, struct got_object_id *id)
4911 const struct got_error *err = NULL;
4912 struct tog_blame_cb_args *a = arg;
4913 struct tog_blame_line *line;
4914 int errcode;
4916 if (nlines != a->nlines ||
4917 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4918 return got_error(GOT_ERR_RANGE);
4920 errcode = pthread_mutex_lock(&tog_mutex);
4921 if (errcode)
4922 return got_error_set_errno(errcode, "pthread_mutex_lock");
4924 if (*a->quit) { /* user has quit the blame view */
4925 err = got_error(GOT_ERR_ITER_COMPLETED);
4926 goto done;
4929 if (lineno == -1)
4930 goto done; /* no change in this commit */
4932 line = &a->lines[lineno - 1];
4933 if (line->annotated)
4934 goto done;
4936 line->id = got_object_id_dup(id);
4937 if (line->id == NULL) {
4938 err = got_error_from_errno("got_object_id_dup");
4939 goto done;
4941 line->annotated = 1;
4942 done:
4943 errcode = pthread_mutex_unlock(&tog_mutex);
4944 if (errcode)
4945 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4946 return err;
4949 static void *
4950 blame_thread(void *arg)
4952 const struct got_error *err, *close_err;
4953 struct tog_blame_thread_args *ta = arg;
4954 struct tog_blame_cb_args *a = ta->cb_args;
4955 int errcode, fd1 = -1, fd2 = -1;
4956 FILE *f1 = NULL, *f2 = NULL;
4958 fd1 = got_opentempfd();
4959 if (fd1 == -1)
4960 return (void *)got_error_from_errno("got_opentempfd");
4962 fd2 = got_opentempfd();
4963 if (fd2 == -1) {
4964 err = got_error_from_errno("got_opentempfd");
4965 goto done;
4968 f1 = got_opentemp();
4969 if (f1 == NULL) {
4970 err = (void *)got_error_from_errno("got_opentemp");
4971 goto done;
4973 f2 = got_opentemp();
4974 if (f2 == NULL) {
4975 err = (void *)got_error_from_errno("got_opentemp");
4976 goto done;
4979 err = block_signals_used_by_main_thread();
4980 if (err)
4981 goto done;
4983 err = got_blame(ta->path, a->commit_id, ta->repo,
4984 tog_diff_algo, blame_cb, ta->cb_args,
4985 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
4986 if (err && err->code == GOT_ERR_CANCELLED)
4987 err = NULL;
4989 errcode = pthread_mutex_lock(&tog_mutex);
4990 if (errcode) {
4991 err = got_error_set_errno(errcode, "pthread_mutex_lock");
4992 goto done;
4995 close_err = got_repo_close(ta->repo);
4996 if (err == NULL)
4997 err = close_err;
4998 ta->repo = NULL;
4999 *ta->complete = 1;
5001 errcode = pthread_mutex_unlock(&tog_mutex);
5002 if (errcode && err == NULL)
5003 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5005 done:
5006 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5007 err = got_error_from_errno("close");
5008 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5009 err = got_error_from_errno("close");
5010 if (f1 && fclose(f1) == EOF && err == NULL)
5011 err = got_error_from_errno("fclose");
5012 if (f2 && fclose(f2) == EOF && err == NULL)
5013 err = got_error_from_errno("fclose");
5015 return (void *)err;
5018 static struct got_object_id *
5019 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5020 int first_displayed_line, int selected_line)
5022 struct tog_blame_line *line;
5024 if (nlines <= 0)
5025 return NULL;
5027 line = &lines[first_displayed_line - 1 + selected_line - 1];
5028 if (!line->annotated)
5029 return NULL;
5031 return line->id;
5034 static const struct got_error *
5035 stop_blame(struct tog_blame *blame)
5037 const struct got_error *err = NULL;
5038 int i;
5040 if (blame->thread) {
5041 int errcode;
5042 errcode = pthread_mutex_unlock(&tog_mutex);
5043 if (errcode)
5044 return got_error_set_errno(errcode,
5045 "pthread_mutex_unlock");
5046 errcode = pthread_join(blame->thread, (void **)&err);
5047 if (errcode)
5048 return got_error_set_errno(errcode, "pthread_join");
5049 errcode = pthread_mutex_lock(&tog_mutex);
5050 if (errcode)
5051 return got_error_set_errno(errcode,
5052 "pthread_mutex_lock");
5053 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5054 err = NULL;
5055 blame->thread = NULL;
5057 if (blame->thread_args.repo) {
5058 const struct got_error *close_err;
5059 close_err = got_repo_close(blame->thread_args.repo);
5060 if (err == NULL)
5061 err = close_err;
5062 blame->thread_args.repo = NULL;
5064 if (blame->f) {
5065 if (fclose(blame->f) == EOF && err == NULL)
5066 err = got_error_from_errno("fclose");
5067 blame->f = NULL;
5069 if (blame->lines) {
5070 for (i = 0; i < blame->nlines; i++)
5071 free(blame->lines[i].id);
5072 free(blame->lines);
5073 blame->lines = NULL;
5075 free(blame->cb_args.commit_id);
5076 blame->cb_args.commit_id = NULL;
5077 if (blame->pack_fds) {
5078 const struct got_error *pack_err =
5079 got_repo_pack_fds_close(blame->pack_fds);
5080 if (err == NULL)
5081 err = pack_err;
5082 blame->pack_fds = NULL;
5084 return err;
5087 static const struct got_error *
5088 cancel_blame_view(void *arg)
5090 const struct got_error *err = NULL;
5091 int *done = arg;
5092 int errcode;
5094 errcode = pthread_mutex_lock(&tog_mutex);
5095 if (errcode)
5096 return got_error_set_errno(errcode,
5097 "pthread_mutex_unlock");
5099 if (*done)
5100 err = got_error(GOT_ERR_CANCELLED);
5102 errcode = pthread_mutex_unlock(&tog_mutex);
5103 if (errcode)
5104 return got_error_set_errno(errcode,
5105 "pthread_mutex_lock");
5107 return err;
5110 static const struct got_error *
5111 run_blame(struct tog_view *view)
5113 struct tog_blame_view_state *s = &view->state.blame;
5114 struct tog_blame *blame = &s->blame;
5115 const struct got_error *err = NULL;
5116 struct got_commit_object *commit = NULL;
5117 struct got_blob_object *blob = NULL;
5118 struct got_repository *thread_repo = NULL;
5119 struct got_object_id *obj_id = NULL;
5120 int obj_type, fd = -1;
5121 int *pack_fds = NULL;
5123 err = got_object_open_as_commit(&commit, s->repo,
5124 &s->blamed_commit->id);
5125 if (err)
5126 return err;
5128 fd = got_opentempfd();
5129 if (fd == -1) {
5130 err = got_error_from_errno("got_opentempfd");
5131 goto done;
5134 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5135 if (err)
5136 goto done;
5138 err = got_object_get_type(&obj_type, s->repo, obj_id);
5139 if (err)
5140 goto done;
5142 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5143 err = got_error(GOT_ERR_OBJ_TYPE);
5144 goto done;
5147 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5148 if (err)
5149 goto done;
5150 blame->f = got_opentemp();
5151 if (blame->f == NULL) {
5152 err = got_error_from_errno("got_opentemp");
5153 goto done;
5155 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5156 &blame->line_offsets, blame->f, blob);
5157 if (err)
5158 goto done;
5159 if (blame->nlines == 0) {
5160 s->blame_complete = 1;
5161 goto done;
5164 /* Don't include \n at EOF in the blame line count. */
5165 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5166 blame->nlines--;
5168 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5169 if (blame->lines == NULL) {
5170 err = got_error_from_errno("calloc");
5171 goto done;
5174 err = got_repo_pack_fds_open(&pack_fds);
5175 if (err)
5176 goto done;
5177 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5178 pack_fds);
5179 if (err)
5180 goto done;
5182 blame->pack_fds = pack_fds;
5183 blame->cb_args.view = view;
5184 blame->cb_args.lines = blame->lines;
5185 blame->cb_args.nlines = blame->nlines;
5186 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5187 if (blame->cb_args.commit_id == NULL) {
5188 err = got_error_from_errno("got_object_id_dup");
5189 goto done;
5191 blame->cb_args.quit = &s->done;
5193 blame->thread_args.path = s->path;
5194 blame->thread_args.repo = thread_repo;
5195 blame->thread_args.cb_args = &blame->cb_args;
5196 blame->thread_args.complete = &s->blame_complete;
5197 blame->thread_args.cancel_cb = cancel_blame_view;
5198 blame->thread_args.cancel_arg = &s->done;
5199 s->blame_complete = 0;
5201 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5202 s->first_displayed_line = 1;
5203 s->last_displayed_line = view->nlines;
5204 s->selected_line = 1;
5206 s->matched_line = 0;
5208 done:
5209 if (commit)
5210 got_object_commit_close(commit);
5211 if (fd != -1 && close(fd) == -1 && err == NULL)
5212 err = got_error_from_errno("close");
5213 if (blob)
5214 got_object_blob_close(blob);
5215 free(obj_id);
5216 if (err)
5217 stop_blame(blame);
5218 return err;
5221 static const struct got_error *
5222 open_blame_view(struct tog_view *view, char *path,
5223 struct got_object_id *commit_id, struct got_repository *repo)
5225 const struct got_error *err = NULL;
5226 struct tog_blame_view_state *s = &view->state.blame;
5228 STAILQ_INIT(&s->blamed_commits);
5230 s->path = strdup(path);
5231 if (s->path == NULL)
5232 return got_error_from_errno("strdup");
5234 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5235 if (err) {
5236 free(s->path);
5237 return err;
5240 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5241 s->first_displayed_line = 1;
5242 s->last_displayed_line = view->nlines;
5243 s->selected_line = 1;
5244 s->blame_complete = 0;
5245 s->repo = repo;
5246 s->commit_id = commit_id;
5247 memset(&s->blame, 0, sizeof(s->blame));
5249 STAILQ_INIT(&s->colors);
5250 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5251 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5252 get_color_value("TOG_COLOR_COMMIT"));
5253 if (err)
5254 return err;
5257 view->show = show_blame_view;
5258 view->input = input_blame_view;
5259 view->reset = reset_blame_view;
5260 view->close = close_blame_view;
5261 view->search_start = search_start_blame_view;
5262 view->search_next = search_next_blame_view;
5264 return run_blame(view);
5267 static const struct got_error *
5268 close_blame_view(struct tog_view *view)
5270 const struct got_error *err = NULL;
5271 struct tog_blame_view_state *s = &view->state.blame;
5273 if (s->blame.thread)
5274 err = stop_blame(&s->blame);
5276 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5277 struct got_object_qid *blamed_commit;
5278 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5279 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5280 got_object_qid_free(blamed_commit);
5283 free(s->path);
5284 free_colors(&s->colors);
5285 return err;
5288 static const struct got_error *
5289 search_start_blame_view(struct tog_view *view)
5291 struct tog_blame_view_state *s = &view->state.blame;
5293 s->matched_line = 0;
5294 return NULL;
5297 static const struct got_error *
5298 search_next_blame_view(struct tog_view *view)
5300 struct tog_blame_view_state *s = &view->state.blame;
5301 const struct got_error *err = NULL;
5302 int lineno;
5303 char *line = NULL;
5304 size_t linesize = 0;
5305 ssize_t linelen;
5307 if (!view->searching) {
5308 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5309 return NULL;
5312 if (s->matched_line) {
5313 if (view->searching == TOG_SEARCH_FORWARD)
5314 lineno = s->matched_line + 1;
5315 else
5316 lineno = s->matched_line - 1;
5317 } else
5318 lineno = s->first_displayed_line - 1 + s->selected_line;
5320 while (1) {
5321 off_t offset;
5323 if (lineno <= 0 || lineno > s->blame.nlines) {
5324 if (s->matched_line == 0) {
5325 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5326 break;
5329 if (view->searching == TOG_SEARCH_FORWARD)
5330 lineno = 1;
5331 else
5332 lineno = s->blame.nlines;
5335 offset = s->blame.line_offsets[lineno - 1];
5336 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5337 free(line);
5338 return got_error_from_errno("fseeko");
5340 linelen = getline(&line, &linesize, s->blame.f);
5341 if (linelen != -1) {
5342 char *exstr;
5343 err = expand_tab(&exstr, line);
5344 if (err)
5345 break;
5346 if (match_line(exstr, &view->regex, 1,
5347 &view->regmatch)) {
5348 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5349 s->matched_line = lineno;
5350 free(exstr);
5351 break;
5353 free(exstr);
5355 if (view->searching == TOG_SEARCH_FORWARD)
5356 lineno++;
5357 else
5358 lineno--;
5360 free(line);
5362 if (s->matched_line) {
5363 s->first_displayed_line = s->matched_line;
5364 s->selected_line = 1;
5367 return err;
5370 static const struct got_error *
5371 show_blame_view(struct tog_view *view)
5373 const struct got_error *err = NULL;
5374 struct tog_blame_view_state *s = &view->state.blame;
5375 int errcode;
5377 if (s->blame.thread == NULL && !s->blame_complete) {
5378 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5379 &s->blame.thread_args);
5380 if (errcode)
5381 return got_error_set_errno(errcode, "pthread_create");
5383 halfdelay(1); /* fast refresh while annotating */
5386 if (s->blame_complete)
5387 halfdelay(10); /* disable fast refresh */
5389 err = draw_blame(view);
5391 view_border(view);
5392 return err;
5395 static const struct got_error *
5396 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5398 const struct got_error *err = NULL, *thread_err = NULL;
5399 struct tog_view *diff_view;
5400 struct tog_blame_view_state *s = &view->state.blame;
5401 int eos, nscroll, begin_y = 0, begin_x = 0;
5403 eos = nscroll = view->nlines - 2;
5404 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5405 view_is_splitscreen(view->child))
5406 --eos; /* border */
5408 switch (ch) {
5409 case '0':
5410 view->x = 0;
5411 break;
5412 case '$':
5413 view->x = MAX(view->maxx - view->ncols / 3, 0);
5414 view->count = 0;
5415 break;
5416 case KEY_RIGHT:
5417 case 'l':
5418 if (view->x + view->ncols / 3 < view->maxx)
5419 view->x += 2; /* move two columns right */
5420 else
5421 view->count = 0;
5422 break;
5423 case KEY_LEFT:
5424 case 'h':
5425 view->x -= MIN(view->x, 2); /* move two columns back */
5426 if (view->x <= 0)
5427 view->count = 0;
5428 break;
5429 case 'q':
5430 s->done = 1;
5431 break;
5432 case 'g':
5433 case KEY_HOME:
5434 s->selected_line = 1;
5435 s->first_displayed_line = 1;
5436 view->count = 0;
5437 break;
5438 case 'G':
5439 case KEY_END:
5440 if (s->blame.nlines < eos) {
5441 s->selected_line = s->blame.nlines;
5442 s->first_displayed_line = 1;
5443 } else {
5444 s->selected_line = eos;
5445 s->first_displayed_line = s->blame.nlines - (eos - 1);
5447 view->count = 0;
5448 break;
5449 case 'k':
5450 case KEY_UP:
5451 case CTRL('p'):
5452 if (s->selected_line > 1)
5453 s->selected_line--;
5454 else if (s->selected_line == 1 &&
5455 s->first_displayed_line > 1)
5456 s->first_displayed_line--;
5457 else
5458 view->count = 0;
5459 break;
5460 case CTRL('u'):
5461 case 'u':
5462 nscroll /= 2;
5463 /* FALL THROUGH */
5464 case KEY_PPAGE:
5465 case CTRL('b'):
5466 case 'b':
5467 if (s->first_displayed_line == 1) {
5468 if (view->count > 1)
5469 nscroll += nscroll;
5470 s->selected_line = MAX(1, s->selected_line - nscroll);
5471 view->count = 0;
5472 break;
5474 if (s->first_displayed_line > nscroll)
5475 s->first_displayed_line -= nscroll;
5476 else
5477 s->first_displayed_line = 1;
5478 break;
5479 case 'j':
5480 case KEY_DOWN:
5481 case CTRL('n'):
5482 if (s->selected_line < eos && s->first_displayed_line +
5483 s->selected_line <= s->blame.nlines)
5484 s->selected_line++;
5485 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5486 s->first_displayed_line++;
5487 else
5488 view->count = 0;
5489 break;
5490 case 'c':
5491 case 'p': {
5492 struct got_object_id *id = NULL;
5494 view->count = 0;
5495 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5496 s->first_displayed_line, s->selected_line);
5497 if (id == NULL)
5498 break;
5499 if (ch == 'p') {
5500 struct got_commit_object *commit, *pcommit;
5501 struct got_object_qid *pid;
5502 struct got_object_id *blob_id = NULL;
5503 int obj_type;
5504 err = got_object_open_as_commit(&commit,
5505 s->repo, id);
5506 if (err)
5507 break;
5508 pid = STAILQ_FIRST(
5509 got_object_commit_get_parent_ids(commit));
5510 if (pid == NULL) {
5511 got_object_commit_close(commit);
5512 break;
5514 /* Check if path history ends here. */
5515 err = got_object_open_as_commit(&pcommit,
5516 s->repo, &pid->id);
5517 if (err)
5518 break;
5519 err = got_object_id_by_path(&blob_id, s->repo,
5520 pcommit, s->path);
5521 got_object_commit_close(pcommit);
5522 if (err) {
5523 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5524 err = NULL;
5525 got_object_commit_close(commit);
5526 break;
5528 err = got_object_get_type(&obj_type, s->repo,
5529 blob_id);
5530 free(blob_id);
5531 /* Can't blame non-blob type objects. */
5532 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5533 got_object_commit_close(commit);
5534 break;
5536 err = got_object_qid_alloc(&s->blamed_commit,
5537 &pid->id);
5538 got_object_commit_close(commit);
5539 } else {
5540 if (got_object_id_cmp(id,
5541 &s->blamed_commit->id) == 0)
5542 break;
5543 err = got_object_qid_alloc(&s->blamed_commit,
5544 id);
5546 if (err)
5547 break;
5548 s->done = 1;
5549 thread_err = stop_blame(&s->blame);
5550 s->done = 0;
5551 if (thread_err)
5552 break;
5553 STAILQ_INSERT_HEAD(&s->blamed_commits,
5554 s->blamed_commit, entry);
5555 err = run_blame(view);
5556 if (err)
5557 break;
5558 break;
5560 case 'C': {
5561 struct got_object_qid *first;
5563 view->count = 0;
5564 first = STAILQ_FIRST(&s->blamed_commits);
5565 if (!got_object_id_cmp(&first->id, s->commit_id))
5566 break;
5567 s->done = 1;
5568 thread_err = stop_blame(&s->blame);
5569 s->done = 0;
5570 if (thread_err)
5571 break;
5572 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5573 got_object_qid_free(s->blamed_commit);
5574 s->blamed_commit =
5575 STAILQ_FIRST(&s->blamed_commits);
5576 err = run_blame(view);
5577 if (err)
5578 break;
5579 break;
5581 case KEY_ENTER:
5582 case '\r': {
5583 struct got_object_id *id = NULL;
5584 struct got_object_qid *pid;
5585 struct got_commit_object *commit = NULL;
5587 view->count = 0;
5588 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5589 s->first_displayed_line, s->selected_line);
5590 if (id == NULL)
5591 break;
5592 err = got_object_open_as_commit(&commit, s->repo, id);
5593 if (err)
5594 break;
5595 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5596 if (view_is_parent_view(view))
5597 view_get_split(view, &begin_y, &begin_x);
5599 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5600 if (diff_view == NULL) {
5601 got_object_commit_close(commit);
5602 err = got_error_from_errno("view_open");
5603 break;
5605 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5606 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5607 got_object_commit_close(commit);
5608 if (err) {
5609 view_close(diff_view);
5610 break;
5612 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
5613 err = view_init_hsplit(view, begin_y);
5614 if (err)
5615 break;
5618 view->focussed = 0;
5619 diff_view->focussed = 1;
5620 diff_view->mode = view->mode;
5621 diff_view->nlines = view->lines - begin_y;
5622 if (view_is_parent_view(view)) {
5623 err = view_close_child(view);
5624 if (err)
5625 break;
5626 err = view_set_child(view, diff_view);
5627 if (err)
5628 break;
5629 view->focus_child = 1;
5630 } else
5631 *new_view = diff_view;
5632 if (err)
5633 break;
5634 break;
5636 case CTRL('d'):
5637 case 'd':
5638 nscroll /= 2;
5639 /* FALL THROUGH */
5640 case KEY_NPAGE:
5641 case CTRL('f'):
5642 case 'f':
5643 case ' ':
5644 if (s->last_displayed_line >= s->blame.nlines &&
5645 s->selected_line >= MIN(s->blame.nlines,
5646 view->nlines - 2)) {
5647 view->count = 0;
5648 break;
5650 if (s->last_displayed_line >= s->blame.nlines &&
5651 s->selected_line < view->nlines - 2) {
5652 s->selected_line +=
5653 MIN(nscroll, s->last_displayed_line -
5654 s->first_displayed_line - s->selected_line + 1);
5656 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5657 s->first_displayed_line += nscroll;
5658 else
5659 s->first_displayed_line =
5660 s->blame.nlines - (view->nlines - 3);
5661 break;
5662 case KEY_RESIZE:
5663 if (s->selected_line > view->nlines - 2) {
5664 s->selected_line = MIN(s->blame.nlines,
5665 view->nlines - 2);
5667 break;
5668 default:
5669 view->count = 0;
5670 break;
5672 return thread_err ? thread_err : err;
5675 static const struct got_error *
5676 reset_blame_view(struct tog_view *view)
5678 const struct got_error *err;
5679 struct tog_blame_view_state *s = &view->state.blame;
5681 view->count = 0;
5682 s->done = 1;
5683 err = stop_blame(&s->blame);
5684 s->done = 0;
5685 if (err)
5686 return err;
5687 return run_blame(view);
5690 static const struct got_error *
5691 cmd_blame(int argc, char *argv[])
5693 const struct got_error *error;
5694 struct got_repository *repo = NULL;
5695 struct got_worktree *worktree = NULL;
5696 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5697 char *link_target = NULL;
5698 struct got_object_id *commit_id = NULL;
5699 struct got_commit_object *commit = NULL;
5700 char *commit_id_str = NULL;
5701 int ch;
5702 struct tog_view *view;
5703 int *pack_fds = NULL;
5705 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5706 switch (ch) {
5707 case 'c':
5708 commit_id_str = optarg;
5709 break;
5710 case 'r':
5711 repo_path = realpath(optarg, NULL);
5712 if (repo_path == NULL)
5713 return got_error_from_errno2("realpath",
5714 optarg);
5715 break;
5716 default:
5717 usage_blame();
5718 /* NOTREACHED */
5722 argc -= optind;
5723 argv += optind;
5725 if (argc != 1)
5726 usage_blame();
5728 error = got_repo_pack_fds_open(&pack_fds);
5729 if (error != NULL)
5730 goto done;
5732 if (repo_path == NULL) {
5733 cwd = getcwd(NULL, 0);
5734 if (cwd == NULL)
5735 return got_error_from_errno("getcwd");
5736 error = got_worktree_open(&worktree, cwd);
5737 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5738 goto done;
5739 if (worktree)
5740 repo_path =
5741 strdup(got_worktree_get_repo_path(worktree));
5742 else
5743 repo_path = strdup(cwd);
5744 if (repo_path == NULL) {
5745 error = got_error_from_errno("strdup");
5746 goto done;
5750 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5751 if (error != NULL)
5752 goto done;
5754 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5755 worktree);
5756 if (error)
5757 goto done;
5759 init_curses();
5761 error = apply_unveil(got_repo_get_path(repo), NULL);
5762 if (error)
5763 goto done;
5765 error = tog_load_refs(repo, 0);
5766 if (error)
5767 goto done;
5769 if (commit_id_str == NULL) {
5770 struct got_reference *head_ref;
5771 error = got_ref_open(&head_ref, repo, worktree ?
5772 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5773 if (error != NULL)
5774 goto done;
5775 error = got_ref_resolve(&commit_id, repo, head_ref);
5776 got_ref_close(head_ref);
5777 } else {
5778 error = got_repo_match_object_id(&commit_id, NULL,
5779 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5781 if (error != NULL)
5782 goto done;
5784 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5785 if (view == NULL) {
5786 error = got_error_from_errno("view_open");
5787 goto done;
5790 error = got_object_open_as_commit(&commit, repo, commit_id);
5791 if (error)
5792 goto done;
5794 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5795 commit, repo);
5796 if (error)
5797 goto done;
5799 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5800 commit_id, repo);
5801 if (error)
5802 goto done;
5803 if (worktree) {
5804 /* Release work tree lock. */
5805 got_worktree_close(worktree);
5806 worktree = NULL;
5808 error = view_loop(view);
5809 done:
5810 free(repo_path);
5811 free(in_repo_path);
5812 free(link_target);
5813 free(cwd);
5814 free(commit_id);
5815 if (commit)
5816 got_object_commit_close(commit);
5817 if (worktree)
5818 got_worktree_close(worktree);
5819 if (repo) {
5820 const struct got_error *close_err = got_repo_close(repo);
5821 if (error == NULL)
5822 error = close_err;
5824 if (pack_fds) {
5825 const struct got_error *pack_err =
5826 got_repo_pack_fds_close(pack_fds);
5827 if (error == NULL)
5828 error = pack_err;
5830 tog_free_refs();
5831 return error;
5834 static const struct got_error *
5835 draw_tree_entries(struct tog_view *view, const char *parent_path)
5837 struct tog_tree_view_state *s = &view->state.tree;
5838 const struct got_error *err = NULL;
5839 struct got_tree_entry *te;
5840 wchar_t *wline;
5841 struct tog_color *tc;
5842 int width, n, i, nentries;
5843 int limit = view->nlines;
5845 s->ndisplayed = 0;
5846 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5847 view_is_splitscreen(view->child))
5848 --limit; /* border */
5850 werase(view->window);
5852 if (limit == 0)
5853 return NULL;
5855 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5856 0, 0);
5857 if (err)
5858 return err;
5859 if (view_needs_focus_indication(view))
5860 wstandout(view->window);
5861 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5862 if (tc)
5863 wattr_on(view->window,
5864 COLOR_PAIR(tc->colorpair), NULL);
5865 waddwstr(view->window, wline);
5866 if (tc)
5867 wattr_off(view->window,
5868 COLOR_PAIR(tc->colorpair), NULL);
5869 if (view_needs_focus_indication(view))
5870 wstandend(view->window);
5871 free(wline);
5872 wline = NULL;
5873 if (width < view->ncols - 1)
5874 waddch(view->window, '\n');
5875 if (--limit <= 0)
5876 return NULL;
5877 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5878 0, 0);
5879 if (err)
5880 return err;
5881 waddwstr(view->window, wline);
5882 free(wline);
5883 wline = NULL;
5884 if (width < view->ncols - 1)
5885 waddch(view->window, '\n');
5886 if (--limit <= 0)
5887 return NULL;
5888 waddch(view->window, '\n');
5889 if (--limit <= 0)
5890 return NULL;
5892 if (s->first_displayed_entry == NULL) {
5893 te = got_object_tree_get_first_entry(s->tree);
5894 if (s->selected == 0) {
5895 if (view->focussed)
5896 wstandout(view->window);
5897 s->selected_entry = NULL;
5899 waddstr(view->window, " ..\n"); /* parent directory */
5900 if (s->selected == 0 && view->focussed)
5901 wstandend(view->window);
5902 s->ndisplayed++;
5903 if (--limit <= 0)
5904 return NULL;
5905 n = 1;
5906 } else {
5907 n = 0;
5908 te = s->first_displayed_entry;
5911 nentries = got_object_tree_get_nentries(s->tree);
5912 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5913 char *line = NULL, *id_str = NULL, *link_target = NULL;
5914 const char *modestr = "";
5915 mode_t mode;
5917 te = got_object_tree_get_entry(s->tree, i);
5918 mode = got_tree_entry_get_mode(te);
5920 if (s->show_ids) {
5921 err = got_object_id_str(&id_str,
5922 got_tree_entry_get_id(te));
5923 if (err)
5924 return got_error_from_errno(
5925 "got_object_id_str");
5927 if (got_object_tree_entry_is_submodule(te))
5928 modestr = "$";
5929 else if (S_ISLNK(mode)) {
5930 int i;
5932 err = got_tree_entry_get_symlink_target(&link_target,
5933 te, s->repo);
5934 if (err) {
5935 free(id_str);
5936 return err;
5938 for (i = 0; i < strlen(link_target); i++) {
5939 if (!isprint((unsigned char)link_target[i]))
5940 link_target[i] = '?';
5942 modestr = "@";
5944 else if (S_ISDIR(mode))
5945 modestr = "/";
5946 else if (mode & S_IXUSR)
5947 modestr = "*";
5948 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5949 got_tree_entry_get_name(te), modestr,
5950 link_target ? " -> ": "",
5951 link_target ? link_target : "") == -1) {
5952 free(id_str);
5953 free(link_target);
5954 return got_error_from_errno("asprintf");
5956 free(id_str);
5957 free(link_target);
5958 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5959 0, 0);
5960 if (err) {
5961 free(line);
5962 break;
5964 if (n == s->selected) {
5965 if (view->focussed)
5966 wstandout(view->window);
5967 s->selected_entry = te;
5969 tc = match_color(&s->colors, line);
5970 if (tc)
5971 wattr_on(view->window,
5972 COLOR_PAIR(tc->colorpair), NULL);
5973 waddwstr(view->window, wline);
5974 if (tc)
5975 wattr_off(view->window,
5976 COLOR_PAIR(tc->colorpair), NULL);
5977 if (width < view->ncols - 1)
5978 waddch(view->window, '\n');
5979 if (n == s->selected && view->focussed)
5980 wstandend(view->window);
5981 free(line);
5982 free(wline);
5983 wline = NULL;
5984 n++;
5985 s->ndisplayed++;
5986 s->last_displayed_entry = te;
5987 if (--limit <= 0)
5988 break;
5991 return err;
5994 static void
5995 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5997 struct got_tree_entry *te;
5998 int isroot = s->tree == s->root;
5999 int i = 0;
6001 if (s->first_displayed_entry == NULL)
6002 return;
6004 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6005 while (i++ < maxscroll) {
6006 if (te == NULL) {
6007 if (!isroot)
6008 s->first_displayed_entry = NULL;
6009 break;
6011 s->first_displayed_entry = te;
6012 te = got_tree_entry_get_prev(s->tree, te);
6016 static const struct got_error *
6017 tree_scroll_down(struct tog_view *view, int maxscroll)
6019 struct tog_tree_view_state *s = &view->state.tree;
6020 struct got_tree_entry *next, *last;
6021 int n = 0;
6023 if (s->first_displayed_entry)
6024 next = got_tree_entry_get_next(s->tree,
6025 s->first_displayed_entry);
6026 else
6027 next = got_object_tree_get_first_entry(s->tree);
6029 last = s->last_displayed_entry;
6030 while (next && n++ < maxscroll) {
6031 if (last)
6032 last = got_tree_entry_get_next(s->tree, last);
6033 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6034 s->first_displayed_entry = next;
6035 next = got_tree_entry_get_next(s->tree, next);
6039 return NULL;
6042 static const struct got_error *
6043 tree_entry_path(char **path, struct tog_parent_trees *parents,
6044 struct got_tree_entry *te)
6046 const struct got_error *err = NULL;
6047 struct tog_parent_tree *pt;
6048 size_t len = 2; /* for leading slash and NUL */
6050 TAILQ_FOREACH(pt, parents, entry)
6051 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6052 + 1 /* slash */;
6053 if (te)
6054 len += strlen(got_tree_entry_get_name(te));
6056 *path = calloc(1, len);
6057 if (path == NULL)
6058 return got_error_from_errno("calloc");
6060 (*path)[0] = '/';
6061 pt = TAILQ_LAST(parents, tog_parent_trees);
6062 while (pt) {
6063 const char *name = got_tree_entry_get_name(pt->selected_entry);
6064 if (strlcat(*path, name, len) >= len) {
6065 err = got_error(GOT_ERR_NO_SPACE);
6066 goto done;
6068 if (strlcat(*path, "/", len) >= len) {
6069 err = got_error(GOT_ERR_NO_SPACE);
6070 goto done;
6072 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6074 if (te) {
6075 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6076 err = got_error(GOT_ERR_NO_SPACE);
6077 goto done;
6080 done:
6081 if (err) {
6082 free(*path);
6083 *path = NULL;
6085 return err;
6088 static const struct got_error *
6089 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6090 struct got_tree_entry *te, struct tog_parent_trees *parents,
6091 struct got_object_id *commit_id, struct got_repository *repo)
6093 const struct got_error *err = NULL;
6094 char *path;
6095 struct tog_view *blame_view;
6097 *new_view = NULL;
6099 err = tree_entry_path(&path, parents, te);
6100 if (err)
6101 return err;
6103 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6104 if (blame_view == NULL) {
6105 err = got_error_from_errno("view_open");
6106 goto done;
6109 err = open_blame_view(blame_view, path, commit_id, repo);
6110 if (err) {
6111 if (err->code == GOT_ERR_CANCELLED)
6112 err = NULL;
6113 view_close(blame_view);
6114 } else
6115 *new_view = blame_view;
6116 done:
6117 free(path);
6118 return err;
6121 static const struct got_error *
6122 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
6123 struct tog_tree_view_state *s)
6125 struct tog_view *log_view;
6126 const struct got_error *err = NULL;
6127 char *path;
6129 *new_view = NULL;
6131 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6132 if (log_view == NULL)
6133 return got_error_from_errno("view_open");
6135 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6136 if (err)
6137 return err;
6139 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6140 path, 0);
6141 if (err)
6142 view_close(log_view);
6143 else
6144 *new_view = log_view;
6145 free(path);
6146 return err;
6149 static const struct got_error *
6150 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6151 const char *head_ref_name, struct got_repository *repo)
6153 const struct got_error *err = NULL;
6154 char *commit_id_str = NULL;
6155 struct tog_tree_view_state *s = &view->state.tree;
6156 struct got_commit_object *commit = NULL;
6158 TAILQ_INIT(&s->parents);
6159 STAILQ_INIT(&s->colors);
6161 s->commit_id = got_object_id_dup(commit_id);
6162 if (s->commit_id == NULL)
6163 return got_error_from_errno("got_object_id_dup");
6165 err = got_object_open_as_commit(&commit, repo, commit_id);
6166 if (err)
6167 goto done;
6170 * The root is opened here and will be closed when the view is closed.
6171 * Any visited subtrees and their path-wise parents are opened and
6172 * closed on demand.
6174 err = got_object_open_as_tree(&s->root, repo,
6175 got_object_commit_get_tree_id(commit));
6176 if (err)
6177 goto done;
6178 s->tree = s->root;
6180 err = got_object_id_str(&commit_id_str, commit_id);
6181 if (err != NULL)
6182 goto done;
6184 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6185 err = got_error_from_errno("asprintf");
6186 goto done;
6189 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6190 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6191 if (head_ref_name) {
6192 s->head_ref_name = strdup(head_ref_name);
6193 if (s->head_ref_name == NULL) {
6194 err = got_error_from_errno("strdup");
6195 goto done;
6198 s->repo = repo;
6200 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6201 err = add_color(&s->colors, "\\$$",
6202 TOG_COLOR_TREE_SUBMODULE,
6203 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6204 if (err)
6205 goto done;
6206 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6207 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6208 if (err)
6209 goto done;
6210 err = add_color(&s->colors, "/$",
6211 TOG_COLOR_TREE_DIRECTORY,
6212 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6213 if (err)
6214 goto done;
6216 err = add_color(&s->colors, "\\*$",
6217 TOG_COLOR_TREE_EXECUTABLE,
6218 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6219 if (err)
6220 goto done;
6222 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6223 get_color_value("TOG_COLOR_COMMIT"));
6224 if (err)
6225 goto done;
6228 view->show = show_tree_view;
6229 view->input = input_tree_view;
6230 view->close = close_tree_view;
6231 view->search_start = search_start_tree_view;
6232 view->search_next = search_next_tree_view;
6233 done:
6234 free(commit_id_str);
6235 if (commit)
6236 got_object_commit_close(commit);
6237 if (err)
6238 close_tree_view(view);
6239 return err;
6242 static const struct got_error *
6243 close_tree_view(struct tog_view *view)
6245 struct tog_tree_view_state *s = &view->state.tree;
6247 free_colors(&s->colors);
6248 free(s->tree_label);
6249 s->tree_label = NULL;
6250 free(s->commit_id);
6251 s->commit_id = NULL;
6252 free(s->head_ref_name);
6253 s->head_ref_name = NULL;
6254 while (!TAILQ_EMPTY(&s->parents)) {
6255 struct tog_parent_tree *parent;
6256 parent = TAILQ_FIRST(&s->parents);
6257 TAILQ_REMOVE(&s->parents, parent, entry);
6258 if (parent->tree != s->root)
6259 got_object_tree_close(parent->tree);
6260 free(parent);
6263 if (s->tree != NULL && s->tree != s->root)
6264 got_object_tree_close(s->tree);
6265 if (s->root)
6266 got_object_tree_close(s->root);
6267 return NULL;
6270 static const struct got_error *
6271 search_start_tree_view(struct tog_view *view)
6273 struct tog_tree_view_state *s = &view->state.tree;
6275 s->matched_entry = NULL;
6276 return NULL;
6279 static int
6280 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6282 regmatch_t regmatch;
6284 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6285 0) == 0;
6288 static const struct got_error *
6289 search_next_tree_view(struct tog_view *view)
6291 struct tog_tree_view_state *s = &view->state.tree;
6292 struct got_tree_entry *te = NULL;
6294 if (!view->searching) {
6295 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6296 return NULL;
6299 if (s->matched_entry) {
6300 if (view->searching == TOG_SEARCH_FORWARD) {
6301 if (s->selected_entry)
6302 te = got_tree_entry_get_next(s->tree,
6303 s->selected_entry);
6304 else
6305 te = got_object_tree_get_first_entry(s->tree);
6306 } else {
6307 if (s->selected_entry == NULL)
6308 te = got_object_tree_get_last_entry(s->tree);
6309 else
6310 te = got_tree_entry_get_prev(s->tree,
6311 s->selected_entry);
6313 } else {
6314 if (s->selected_entry)
6315 te = s->selected_entry;
6316 else if (view->searching == TOG_SEARCH_FORWARD)
6317 te = got_object_tree_get_first_entry(s->tree);
6318 else
6319 te = got_object_tree_get_last_entry(s->tree);
6322 while (1) {
6323 if (te == NULL) {
6324 if (s->matched_entry == NULL) {
6325 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6326 return NULL;
6328 if (view->searching == TOG_SEARCH_FORWARD)
6329 te = got_object_tree_get_first_entry(s->tree);
6330 else
6331 te = got_object_tree_get_last_entry(s->tree);
6334 if (match_tree_entry(te, &view->regex)) {
6335 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6336 s->matched_entry = te;
6337 break;
6340 if (view->searching == TOG_SEARCH_FORWARD)
6341 te = got_tree_entry_get_next(s->tree, te);
6342 else
6343 te = got_tree_entry_get_prev(s->tree, te);
6346 if (s->matched_entry) {
6347 s->first_displayed_entry = s->matched_entry;
6348 s->selected = 0;
6351 return NULL;
6354 static const struct got_error *
6355 show_tree_view(struct tog_view *view)
6357 const struct got_error *err = NULL;
6358 struct tog_tree_view_state *s = &view->state.tree;
6359 char *parent_path;
6361 err = tree_entry_path(&parent_path, &s->parents, NULL);
6362 if (err)
6363 return err;
6365 err = draw_tree_entries(view, parent_path);
6366 free(parent_path);
6368 view_border(view);
6369 return err;
6372 static const struct got_error *
6373 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6375 const struct got_error *err = NULL;
6376 struct tog_tree_view_state *s = &view->state.tree;
6377 struct tog_view *log_view, *ref_view;
6378 struct got_tree_entry *te;
6379 int begin_x = 0, n, nscroll = view->nlines - 3;
6381 switch (ch) {
6382 case 'i':
6383 s->show_ids = !s->show_ids;
6384 view->count = 0;
6385 break;
6386 case 'l':
6387 view->count = 0;
6388 if (!s->selected_entry)
6389 break;
6390 if (view_is_parent_view(view))
6391 begin_x = view_split_begin_x(view->begin_x);
6392 err = log_selected_tree_entry(&log_view, begin_x, s);
6393 view->focussed = 0;
6394 log_view->focussed = 1;
6395 if (view_is_parent_view(view)) {
6396 err = view_close_child(view);
6397 if (err)
6398 return err;
6399 err = view_set_child(view, log_view);
6400 if (err)
6401 return err;
6402 view->focus_child = 1;
6403 } else
6404 *new_view = log_view;
6405 break;
6406 case 'r':
6407 view->count = 0;
6408 if (view_is_parent_view(view))
6409 begin_x = view_split_begin_x(view->begin_x);
6410 ref_view = view_open(view->nlines, view->ncols,
6411 view->begin_y, begin_x, TOG_VIEW_REF);
6412 if (ref_view == NULL)
6413 return got_error_from_errno("view_open");
6414 err = open_ref_view(ref_view, s->repo);
6415 if (err) {
6416 view_close(ref_view);
6417 return err;
6419 view->focussed = 0;
6420 ref_view->focussed = 1;
6421 if (view_is_parent_view(view)) {
6422 err = view_close_child(view);
6423 if (err)
6424 return err;
6425 err = view_set_child(view, ref_view);
6426 if (err)
6427 return err;
6428 view->focus_child = 1;
6429 } else
6430 *new_view = ref_view;
6431 break;
6432 case 'g':
6433 case KEY_HOME:
6434 s->selected = 0;
6435 view->count = 0;
6436 if (s->tree == s->root)
6437 s->first_displayed_entry =
6438 got_object_tree_get_first_entry(s->tree);
6439 else
6440 s->first_displayed_entry = NULL;
6441 break;
6442 case 'G':
6443 case KEY_END: {
6444 int eos = view->nlines - 3;
6446 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6447 --eos; /* border */
6448 s->selected = 0;
6449 view->count = 0;
6450 te = got_object_tree_get_last_entry(s->tree);
6451 for (n = 0; n < eos; n++) {
6452 if (te == NULL) {
6453 if(s->tree != s->root) {
6454 s->first_displayed_entry = NULL;
6455 n++;
6457 break;
6459 s->first_displayed_entry = te;
6460 te = got_tree_entry_get_prev(s->tree, te);
6462 if (n > 0)
6463 s->selected = n - 1;
6464 break;
6466 case 'k':
6467 case KEY_UP:
6468 case CTRL('p'):
6469 if (s->selected > 0) {
6470 s->selected--;
6471 break;
6473 tree_scroll_up(s, 1);
6474 if (s->selected_entry == NULL ||
6475 (s->tree == s->root && s->selected_entry ==
6476 got_object_tree_get_first_entry(s->tree)))
6477 view->count = 0;
6478 break;
6479 case CTRL('u'):
6480 case 'u':
6481 nscroll /= 2;
6482 /* FALL THROUGH */
6483 case KEY_PPAGE:
6484 case CTRL('b'):
6485 case 'b':
6486 if (s->tree == s->root) {
6487 if (got_object_tree_get_first_entry(s->tree) ==
6488 s->first_displayed_entry)
6489 s->selected -= MIN(s->selected, nscroll);
6490 } else {
6491 if (s->first_displayed_entry == NULL)
6492 s->selected -= MIN(s->selected, nscroll);
6494 tree_scroll_up(s, MAX(0, nscroll));
6495 if (s->selected_entry == NULL ||
6496 (s->tree == s->root && s->selected_entry ==
6497 got_object_tree_get_first_entry(s->tree)))
6498 view->count = 0;
6499 break;
6500 case 'j':
6501 case KEY_DOWN:
6502 case CTRL('n'):
6503 if (s->selected < s->ndisplayed - 1) {
6504 s->selected++;
6505 break;
6507 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6508 == NULL) {
6509 /* can't scroll any further */
6510 view->count = 0;
6511 break;
6513 tree_scroll_down(view, 1);
6514 break;
6515 case CTRL('d'):
6516 case 'd':
6517 nscroll /= 2;
6518 /* FALL THROUGH */
6519 case KEY_NPAGE:
6520 case CTRL('f'):
6521 case 'f':
6522 case ' ':
6523 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6524 == NULL) {
6525 /* can't scroll any further; move cursor down */
6526 if (s->selected < s->ndisplayed - 1)
6527 s->selected += MIN(nscroll,
6528 s->ndisplayed - s->selected - 1);
6529 else
6530 view->count = 0;
6531 break;
6533 tree_scroll_down(view, nscroll);
6534 break;
6535 case KEY_ENTER:
6536 case '\r':
6537 case KEY_BACKSPACE:
6538 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6539 struct tog_parent_tree *parent;
6540 /* user selected '..' */
6541 if (s->tree == s->root) {
6542 view->count = 0;
6543 break;
6545 parent = TAILQ_FIRST(&s->parents);
6546 TAILQ_REMOVE(&s->parents, parent,
6547 entry);
6548 got_object_tree_close(s->tree);
6549 s->tree = parent->tree;
6550 s->first_displayed_entry =
6551 parent->first_displayed_entry;
6552 s->selected_entry =
6553 parent->selected_entry;
6554 s->selected = parent->selected;
6555 if (s->selected > view->nlines - 3) {
6556 err = offset_selection_down(view);
6557 if (err)
6558 break;
6560 free(parent);
6561 } else if (S_ISDIR(got_tree_entry_get_mode(
6562 s->selected_entry))) {
6563 struct got_tree_object *subtree;
6564 view->count = 0;
6565 err = got_object_open_as_tree(&subtree, s->repo,
6566 got_tree_entry_get_id(s->selected_entry));
6567 if (err)
6568 break;
6569 err = tree_view_visit_subtree(s, subtree);
6570 if (err) {
6571 got_object_tree_close(subtree);
6572 break;
6574 } else if (S_ISREG(got_tree_entry_get_mode(
6575 s->selected_entry))) {
6576 struct tog_view *blame_view;
6577 int begin_x = 0, begin_y = 0;
6579 if (view_is_parent_view(view))
6580 view_get_split(view, &begin_y, &begin_x);
6582 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6583 s->selected_entry, &s->parents,
6584 s->commit_id, s->repo);
6585 if (err)
6586 break;
6588 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
6589 err = view_init_hsplit(view, begin_y);
6590 if (err)
6591 break;
6594 view->count = 0;
6595 view->focussed = 0;
6596 blame_view->focussed = 1;
6597 blame_view->mode = view->mode;
6598 blame_view->nlines = view->lines - begin_y;
6599 if (view_is_parent_view(view)) {
6600 err = view_close_child(view);
6601 if (err)
6602 return err;
6603 err = view_set_child(view, blame_view);
6604 if (err)
6605 return err;
6606 view->focus_child = 1;
6607 } else
6608 *new_view = blame_view;
6610 break;
6611 case KEY_RESIZE:
6612 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6613 s->selected = view->nlines - 4;
6614 view->count = 0;
6615 break;
6616 default:
6617 view->count = 0;
6618 break;
6621 return err;
6624 __dead static void
6625 usage_tree(void)
6627 endwin();
6628 fprintf(stderr,
6629 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6630 getprogname());
6631 exit(1);
6634 static const struct got_error *
6635 cmd_tree(int argc, char *argv[])
6637 const struct got_error *error;
6638 struct got_repository *repo = NULL;
6639 struct got_worktree *worktree = NULL;
6640 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6641 struct got_object_id *commit_id = NULL;
6642 struct got_commit_object *commit = NULL;
6643 const char *commit_id_arg = NULL;
6644 char *label = NULL;
6645 struct got_reference *ref = NULL;
6646 const char *head_ref_name = NULL;
6647 int ch;
6648 struct tog_view *view;
6649 int *pack_fds = NULL;
6651 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6652 switch (ch) {
6653 case 'c':
6654 commit_id_arg = optarg;
6655 break;
6656 case 'r':
6657 repo_path = realpath(optarg, NULL);
6658 if (repo_path == NULL)
6659 return got_error_from_errno2("realpath",
6660 optarg);
6661 break;
6662 default:
6663 usage_tree();
6664 /* NOTREACHED */
6668 argc -= optind;
6669 argv += optind;
6671 if (argc > 1)
6672 usage_tree();
6674 error = got_repo_pack_fds_open(&pack_fds);
6675 if (error != NULL)
6676 goto done;
6678 if (repo_path == NULL) {
6679 cwd = getcwd(NULL, 0);
6680 if (cwd == NULL)
6681 return got_error_from_errno("getcwd");
6682 error = got_worktree_open(&worktree, cwd);
6683 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6684 goto done;
6685 if (worktree)
6686 repo_path =
6687 strdup(got_worktree_get_repo_path(worktree));
6688 else
6689 repo_path = strdup(cwd);
6690 if (repo_path == NULL) {
6691 error = got_error_from_errno("strdup");
6692 goto done;
6696 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6697 if (error != NULL)
6698 goto done;
6700 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6701 repo, worktree);
6702 if (error)
6703 goto done;
6705 init_curses();
6707 error = apply_unveil(got_repo_get_path(repo), NULL);
6708 if (error)
6709 goto done;
6711 error = tog_load_refs(repo, 0);
6712 if (error)
6713 goto done;
6715 if (commit_id_arg == NULL) {
6716 error = got_repo_match_object_id(&commit_id, &label,
6717 worktree ? got_worktree_get_head_ref_name(worktree) :
6718 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6719 if (error)
6720 goto done;
6721 head_ref_name = label;
6722 } else {
6723 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6724 if (error == NULL)
6725 head_ref_name = got_ref_get_name(ref);
6726 else if (error->code != GOT_ERR_NOT_REF)
6727 goto done;
6728 error = got_repo_match_object_id(&commit_id, NULL,
6729 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6730 if (error)
6731 goto done;
6734 error = got_object_open_as_commit(&commit, repo, commit_id);
6735 if (error)
6736 goto done;
6738 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6739 if (view == NULL) {
6740 error = got_error_from_errno("view_open");
6741 goto done;
6743 error = open_tree_view(view, commit_id, head_ref_name, repo);
6744 if (error)
6745 goto done;
6746 if (!got_path_is_root_dir(in_repo_path)) {
6747 error = tree_view_walk_path(&view->state.tree, commit,
6748 in_repo_path);
6749 if (error)
6750 goto done;
6753 if (worktree) {
6754 /* Release work tree lock. */
6755 got_worktree_close(worktree);
6756 worktree = NULL;
6758 error = view_loop(view);
6759 done:
6760 free(repo_path);
6761 free(cwd);
6762 free(commit_id);
6763 free(label);
6764 if (ref)
6765 got_ref_close(ref);
6766 if (repo) {
6767 const struct got_error *close_err = got_repo_close(repo);
6768 if (error == NULL)
6769 error = close_err;
6771 if (pack_fds) {
6772 const struct got_error *pack_err =
6773 got_repo_pack_fds_close(pack_fds);
6774 if (error == NULL)
6775 error = pack_err;
6777 tog_free_refs();
6778 return error;
6781 static const struct got_error *
6782 ref_view_load_refs(struct tog_ref_view_state *s)
6784 struct got_reflist_entry *sre;
6785 struct tog_reflist_entry *re;
6787 s->nrefs = 0;
6788 TAILQ_FOREACH(sre, &tog_refs, entry) {
6789 if (strncmp(got_ref_get_name(sre->ref),
6790 "refs/got/", 9) == 0 &&
6791 strncmp(got_ref_get_name(sre->ref),
6792 "refs/got/backup/", 16) != 0)
6793 continue;
6795 re = malloc(sizeof(*re));
6796 if (re == NULL)
6797 return got_error_from_errno("malloc");
6799 re->ref = got_ref_dup(sre->ref);
6800 if (re->ref == NULL)
6801 return got_error_from_errno("got_ref_dup");
6802 re->idx = s->nrefs++;
6803 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6806 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6807 return NULL;
6810 static void
6811 ref_view_free_refs(struct tog_ref_view_state *s)
6813 struct tog_reflist_entry *re;
6815 while (!TAILQ_EMPTY(&s->refs)) {
6816 re = TAILQ_FIRST(&s->refs);
6817 TAILQ_REMOVE(&s->refs, re, entry);
6818 got_ref_close(re->ref);
6819 free(re);
6823 static const struct got_error *
6824 open_ref_view(struct tog_view *view, struct got_repository *repo)
6826 const struct got_error *err = NULL;
6827 struct tog_ref_view_state *s = &view->state.ref;
6829 s->selected_entry = 0;
6830 s->repo = repo;
6832 TAILQ_INIT(&s->refs);
6833 STAILQ_INIT(&s->colors);
6835 err = ref_view_load_refs(s);
6836 if (err)
6837 return err;
6839 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6840 err = add_color(&s->colors, "^refs/heads/",
6841 TOG_COLOR_REFS_HEADS,
6842 get_color_value("TOG_COLOR_REFS_HEADS"));
6843 if (err)
6844 goto done;
6846 err = add_color(&s->colors, "^refs/tags/",
6847 TOG_COLOR_REFS_TAGS,
6848 get_color_value("TOG_COLOR_REFS_TAGS"));
6849 if (err)
6850 goto done;
6852 err = add_color(&s->colors, "^refs/remotes/",
6853 TOG_COLOR_REFS_REMOTES,
6854 get_color_value("TOG_COLOR_REFS_REMOTES"));
6855 if (err)
6856 goto done;
6858 err = add_color(&s->colors, "^refs/got/backup/",
6859 TOG_COLOR_REFS_BACKUP,
6860 get_color_value("TOG_COLOR_REFS_BACKUP"));
6861 if (err)
6862 goto done;
6865 view->show = show_ref_view;
6866 view->input = input_ref_view;
6867 view->close = close_ref_view;
6868 view->search_start = search_start_ref_view;
6869 view->search_next = search_next_ref_view;
6870 done:
6871 if (err)
6872 free_colors(&s->colors);
6873 return err;
6876 static const struct got_error *
6877 close_ref_view(struct tog_view *view)
6879 struct tog_ref_view_state *s = &view->state.ref;
6881 ref_view_free_refs(s);
6882 free_colors(&s->colors);
6884 return NULL;
6887 static const struct got_error *
6888 resolve_reflist_entry(struct got_object_id **commit_id,
6889 struct tog_reflist_entry *re, struct got_repository *repo)
6891 const struct got_error *err = NULL;
6892 struct got_object_id *obj_id;
6893 struct got_tag_object *tag = NULL;
6894 int obj_type;
6896 *commit_id = NULL;
6898 err = got_ref_resolve(&obj_id, repo, re->ref);
6899 if (err)
6900 return err;
6902 err = got_object_get_type(&obj_type, repo, obj_id);
6903 if (err)
6904 goto done;
6906 switch (obj_type) {
6907 case GOT_OBJ_TYPE_COMMIT:
6908 *commit_id = obj_id;
6909 break;
6910 case GOT_OBJ_TYPE_TAG:
6911 err = got_object_open_as_tag(&tag, repo, obj_id);
6912 if (err)
6913 goto done;
6914 free(obj_id);
6915 err = got_object_get_type(&obj_type, repo,
6916 got_object_tag_get_object_id(tag));
6917 if (err)
6918 goto done;
6919 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6920 err = got_error(GOT_ERR_OBJ_TYPE);
6921 goto done;
6923 *commit_id = got_object_id_dup(
6924 got_object_tag_get_object_id(tag));
6925 if (*commit_id == NULL) {
6926 err = got_error_from_errno("got_object_id_dup");
6927 goto done;
6929 break;
6930 default:
6931 err = got_error(GOT_ERR_OBJ_TYPE);
6932 break;
6935 done:
6936 if (tag)
6937 got_object_tag_close(tag);
6938 if (err) {
6939 free(*commit_id);
6940 *commit_id = NULL;
6942 return err;
6945 static const struct got_error *
6946 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6947 struct tog_reflist_entry *re, struct got_repository *repo)
6949 struct tog_view *log_view;
6950 const struct got_error *err = NULL;
6951 struct got_object_id *commit_id = NULL;
6953 *new_view = NULL;
6955 err = resolve_reflist_entry(&commit_id, re, repo);
6956 if (err) {
6957 if (err->code != GOT_ERR_OBJ_TYPE)
6958 return err;
6959 else
6960 return NULL;
6963 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6964 if (log_view == NULL) {
6965 err = got_error_from_errno("view_open");
6966 goto done;
6969 err = open_log_view(log_view, commit_id, repo,
6970 got_ref_get_name(re->ref), "", 0);
6971 done:
6972 if (err)
6973 view_close(log_view);
6974 else
6975 *new_view = log_view;
6976 free(commit_id);
6977 return err;
6980 static void
6981 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6983 struct tog_reflist_entry *re;
6984 int i = 0;
6986 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6987 return;
6989 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6990 while (i++ < maxscroll) {
6991 if (re == NULL)
6992 break;
6993 s->first_displayed_entry = re;
6994 re = TAILQ_PREV(re, tog_reflist_head, entry);
6998 static const struct got_error *
6999 ref_scroll_down(struct tog_view *view, int maxscroll)
7001 struct tog_ref_view_state *s = &view->state.ref;
7002 struct tog_reflist_entry *next, *last;
7003 int n = 0;
7005 if (s->first_displayed_entry)
7006 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7007 else
7008 next = TAILQ_FIRST(&s->refs);
7010 last = s->last_displayed_entry;
7011 while (next && n++ < maxscroll) {
7012 if (last)
7013 last = TAILQ_NEXT(last, entry);
7014 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7015 s->first_displayed_entry = next;
7016 next = TAILQ_NEXT(next, entry);
7020 return NULL;
7023 static const struct got_error *
7024 search_start_ref_view(struct tog_view *view)
7026 struct tog_ref_view_state *s = &view->state.ref;
7028 s->matched_entry = NULL;
7029 return NULL;
7032 static int
7033 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7035 regmatch_t regmatch;
7037 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7038 0) == 0;
7041 static const struct got_error *
7042 search_next_ref_view(struct tog_view *view)
7044 struct tog_ref_view_state *s = &view->state.ref;
7045 struct tog_reflist_entry *re = NULL;
7047 if (!view->searching) {
7048 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7049 return NULL;
7052 if (s->matched_entry) {
7053 if (view->searching == TOG_SEARCH_FORWARD) {
7054 if (s->selected_entry)
7055 re = TAILQ_NEXT(s->selected_entry, entry);
7056 else
7057 re = TAILQ_PREV(s->selected_entry,
7058 tog_reflist_head, entry);
7059 } else {
7060 if (s->selected_entry == NULL)
7061 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7062 else
7063 re = TAILQ_PREV(s->selected_entry,
7064 tog_reflist_head, entry);
7066 } else {
7067 if (s->selected_entry)
7068 re = s->selected_entry;
7069 else if (view->searching == TOG_SEARCH_FORWARD)
7070 re = TAILQ_FIRST(&s->refs);
7071 else
7072 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7075 while (1) {
7076 if (re == NULL) {
7077 if (s->matched_entry == NULL) {
7078 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7079 return NULL;
7081 if (view->searching == TOG_SEARCH_FORWARD)
7082 re = TAILQ_FIRST(&s->refs);
7083 else
7084 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7087 if (match_reflist_entry(re, &view->regex)) {
7088 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7089 s->matched_entry = re;
7090 break;
7093 if (view->searching == TOG_SEARCH_FORWARD)
7094 re = TAILQ_NEXT(re, entry);
7095 else
7096 re = TAILQ_PREV(re, tog_reflist_head, entry);
7099 if (s->matched_entry) {
7100 s->first_displayed_entry = s->matched_entry;
7101 s->selected = 0;
7104 return NULL;
7107 static const struct got_error *
7108 show_ref_view(struct tog_view *view)
7110 const struct got_error *err = NULL;
7111 struct tog_ref_view_state *s = &view->state.ref;
7112 struct tog_reflist_entry *re;
7113 char *line = NULL;
7114 wchar_t *wline;
7115 struct tog_color *tc;
7116 int width, n;
7117 int limit = view->nlines;
7119 werase(view->window);
7121 s->ndisplayed = 0;
7122 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
7123 view_is_splitscreen(view->child))
7124 --limit; /* border */
7126 if (limit == 0)
7127 return NULL;
7129 re = s->first_displayed_entry;
7131 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7132 s->nrefs) == -1)
7133 return got_error_from_errno("asprintf");
7135 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7136 if (err) {
7137 free(line);
7138 return err;
7140 if (view_needs_focus_indication(view))
7141 wstandout(view->window);
7142 waddwstr(view->window, wline);
7143 if (view_needs_focus_indication(view))
7144 wstandend(view->window);
7145 free(wline);
7146 wline = NULL;
7147 free(line);
7148 line = NULL;
7149 if (width < view->ncols - 1)
7150 waddch(view->window, '\n');
7151 if (--limit <= 0)
7152 return NULL;
7154 n = 0;
7155 while (re && limit > 0) {
7156 char *line = NULL;
7157 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7159 if (s->show_date) {
7160 struct got_commit_object *ci;
7161 struct got_tag_object *tag;
7162 struct got_object_id *id;
7163 struct tm tm;
7164 time_t t;
7166 err = got_ref_resolve(&id, s->repo, re->ref);
7167 if (err)
7168 return err;
7169 err = got_object_open_as_tag(&tag, s->repo, id);
7170 if (err) {
7171 if (err->code != GOT_ERR_OBJ_TYPE) {
7172 free(id);
7173 return err;
7175 err = got_object_open_as_commit(&ci, s->repo,
7176 id);
7177 if (err) {
7178 free(id);
7179 return err;
7181 t = got_object_commit_get_committer_time(ci);
7182 got_object_commit_close(ci);
7183 } else {
7184 t = got_object_tag_get_tagger_time(tag);
7185 got_object_tag_close(tag);
7187 free(id);
7188 if (gmtime_r(&t, &tm) == NULL)
7189 return got_error_from_errno("gmtime_r");
7190 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7191 return got_error(GOT_ERR_NO_SPACE);
7193 if (got_ref_is_symbolic(re->ref)) {
7194 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7195 ymd : "", got_ref_get_name(re->ref),
7196 got_ref_get_symref_target(re->ref)) == -1)
7197 return got_error_from_errno("asprintf");
7198 } else if (s->show_ids) {
7199 struct got_object_id *id;
7200 char *id_str;
7201 err = got_ref_resolve(&id, s->repo, re->ref);
7202 if (err)
7203 return err;
7204 err = got_object_id_str(&id_str, id);
7205 if (err) {
7206 free(id);
7207 return err;
7209 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7210 got_ref_get_name(re->ref), id_str) == -1) {
7211 err = got_error_from_errno("asprintf");
7212 free(id);
7213 free(id_str);
7214 return err;
7216 free(id);
7217 free(id_str);
7218 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7219 got_ref_get_name(re->ref)) == -1)
7220 return got_error_from_errno("asprintf");
7222 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7223 0, 0);
7224 if (err) {
7225 free(line);
7226 return err;
7228 if (n == s->selected) {
7229 if (view->focussed)
7230 wstandout(view->window);
7231 s->selected_entry = re;
7233 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7234 if (tc)
7235 wattr_on(view->window,
7236 COLOR_PAIR(tc->colorpair), NULL);
7237 waddwstr(view->window, wline);
7238 if (tc)
7239 wattr_off(view->window,
7240 COLOR_PAIR(tc->colorpair), NULL);
7241 if (width < view->ncols - 1)
7242 waddch(view->window, '\n');
7243 if (n == s->selected && view->focussed)
7244 wstandend(view->window);
7245 free(line);
7246 free(wline);
7247 wline = NULL;
7248 n++;
7249 s->ndisplayed++;
7250 s->last_displayed_entry = re;
7252 limit--;
7253 re = TAILQ_NEXT(re, entry);
7256 view_border(view);
7257 return err;
7260 static const struct got_error *
7261 browse_ref_tree(struct tog_view **new_view, int begin_x,
7262 struct tog_reflist_entry *re, struct got_repository *repo)
7264 const struct got_error *err = NULL;
7265 struct got_object_id *commit_id = NULL;
7266 struct tog_view *tree_view;
7268 *new_view = NULL;
7270 err = resolve_reflist_entry(&commit_id, re, repo);
7271 if (err) {
7272 if (err->code != GOT_ERR_OBJ_TYPE)
7273 return err;
7274 else
7275 return NULL;
7279 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
7280 if (tree_view == NULL) {
7281 err = got_error_from_errno("view_open");
7282 goto done;
7285 err = open_tree_view(tree_view, commit_id,
7286 got_ref_get_name(re->ref), repo);
7287 if (err)
7288 goto done;
7290 *new_view = tree_view;
7291 done:
7292 free(commit_id);
7293 return err;
7295 static const struct got_error *
7296 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7298 const struct got_error *err = NULL;
7299 struct tog_ref_view_state *s = &view->state.ref;
7300 struct tog_view *log_view, *tree_view;
7301 struct tog_reflist_entry *re;
7302 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7304 switch (ch) {
7305 case 'i':
7306 s->show_ids = !s->show_ids;
7307 view->count = 0;
7308 break;
7309 case 'm':
7310 s->show_date = !s->show_date;
7311 view->count = 0;
7312 break;
7313 case 'o':
7314 s->sort_by_date = !s->sort_by_date;
7315 view->count = 0;
7316 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7317 got_ref_cmp_by_commit_timestamp_descending :
7318 tog_ref_cmp_by_name, s->repo);
7319 if (err)
7320 break;
7321 got_reflist_object_id_map_free(tog_refs_idmap);
7322 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7323 &tog_refs, s->repo);
7324 if (err)
7325 break;
7326 ref_view_free_refs(s);
7327 err = ref_view_load_refs(s);
7328 break;
7329 case KEY_ENTER:
7330 case '\r':
7331 view->count = 0;
7332 if (!s->selected_entry)
7333 break;
7334 if (view_is_parent_view(view))
7335 view_get_split(view, &begin_y, &begin_x);
7337 err = log_ref_entry(&log_view, begin_y, begin_x,
7338 s->selected_entry, s->repo);
7339 if (err)
7340 break;
7342 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
7343 err = view_init_hsplit(view, begin_y);
7344 if (err)
7345 break;
7348 view->focussed = 0;
7349 log_view->focussed = 1;
7350 log_view->mode = view->mode;
7351 log_view->nlines = view->lines - begin_y;
7352 if (view_is_parent_view(view)) {
7353 err = view_close_child(view);
7354 if (err)
7355 return err;
7356 err = view_set_child(view, log_view);
7357 if (err)
7358 return err;
7359 view->focus_child = 1;
7360 } else
7361 *new_view = log_view;
7362 break;
7363 case 't':
7364 view->count = 0;
7365 if (!s->selected_entry)
7366 break;
7367 if (view_is_parent_view(view))
7368 begin_x = view_split_begin_x(view->begin_x);
7369 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
7370 s->repo);
7371 if (err || tree_view == NULL)
7372 break;
7373 view->focussed = 0;
7374 tree_view->focussed = 1;
7375 if (view_is_parent_view(view)) {
7376 err = view_close_child(view);
7377 if (err)
7378 return err;
7379 err = view_set_child(view, tree_view);
7380 if (err)
7381 return err;
7382 view->focus_child = 1;
7383 } else
7384 *new_view = tree_view;
7385 break;
7386 case 'g':
7387 case KEY_HOME:
7388 s->selected = 0;
7389 view->count = 0;
7390 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7391 break;
7392 case 'G':
7393 case KEY_END: {
7394 int eos = view->nlines - 1;
7396 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7397 --eos; /* border */
7398 s->selected = 0;
7399 view->count = 0;
7400 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7401 for (n = 0; n < eos; n++) {
7402 if (re == NULL)
7403 break;
7404 s->first_displayed_entry = re;
7405 re = TAILQ_PREV(re, tog_reflist_head, entry);
7407 if (n > 0)
7408 s->selected = n - 1;
7409 break;
7411 case 'k':
7412 case KEY_UP:
7413 case CTRL('p'):
7414 if (s->selected > 0) {
7415 s->selected--;
7416 break;
7418 ref_scroll_up(s, 1);
7419 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7420 view->count = 0;
7421 break;
7422 case CTRL('u'):
7423 case 'u':
7424 nscroll /= 2;
7425 /* FALL THROUGH */
7426 case KEY_PPAGE:
7427 case CTRL('b'):
7428 case 'b':
7429 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7430 s->selected -= MIN(nscroll, s->selected);
7431 ref_scroll_up(s, MAX(0, nscroll));
7432 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7433 view->count = 0;
7434 break;
7435 case 'j':
7436 case KEY_DOWN:
7437 case CTRL('n'):
7438 if (s->selected < s->ndisplayed - 1) {
7439 s->selected++;
7440 break;
7442 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7443 /* can't scroll any further */
7444 view->count = 0;
7445 break;
7447 ref_scroll_down(view, 1);
7448 break;
7449 case CTRL('d'):
7450 case 'd':
7451 nscroll /= 2;
7452 /* FALL THROUGH */
7453 case KEY_NPAGE:
7454 case CTRL('f'):
7455 case 'f':
7456 case ' ':
7457 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7458 /* can't scroll any further; move cursor down */
7459 if (s->selected < s->ndisplayed - 1)
7460 s->selected += MIN(nscroll,
7461 s->ndisplayed - s->selected - 1);
7462 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7463 s->selected += s->ndisplayed - s->selected - 1;
7464 view->count = 0;
7465 break;
7467 ref_scroll_down(view, nscroll);
7468 break;
7469 case CTRL('l'):
7470 view->count = 0;
7471 tog_free_refs();
7472 err = tog_load_refs(s->repo, s->sort_by_date);
7473 if (err)
7474 break;
7475 ref_view_free_refs(s);
7476 err = ref_view_load_refs(s);
7477 break;
7478 case KEY_RESIZE:
7479 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7480 s->selected = view->nlines - 2;
7481 break;
7482 default:
7483 view->count = 0;
7484 break;
7487 return err;
7490 __dead static void
7491 usage_ref(void)
7493 endwin();
7494 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7495 getprogname());
7496 exit(1);
7499 static const struct got_error *
7500 cmd_ref(int argc, char *argv[])
7502 const struct got_error *error;
7503 struct got_repository *repo = NULL;
7504 struct got_worktree *worktree = NULL;
7505 char *cwd = NULL, *repo_path = NULL;
7506 int ch;
7507 struct tog_view *view;
7508 int *pack_fds = NULL;
7510 while ((ch = getopt(argc, argv, "r:")) != -1) {
7511 switch (ch) {
7512 case 'r':
7513 repo_path = realpath(optarg, NULL);
7514 if (repo_path == NULL)
7515 return got_error_from_errno2("realpath",
7516 optarg);
7517 break;
7518 default:
7519 usage_ref();
7520 /* NOTREACHED */
7524 argc -= optind;
7525 argv += optind;
7527 if (argc > 1)
7528 usage_ref();
7530 error = got_repo_pack_fds_open(&pack_fds);
7531 if (error != NULL)
7532 goto done;
7534 if (repo_path == NULL) {
7535 cwd = getcwd(NULL, 0);
7536 if (cwd == NULL)
7537 return got_error_from_errno("getcwd");
7538 error = got_worktree_open(&worktree, cwd);
7539 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7540 goto done;
7541 if (worktree)
7542 repo_path =
7543 strdup(got_worktree_get_repo_path(worktree));
7544 else
7545 repo_path = strdup(cwd);
7546 if (repo_path == NULL) {
7547 error = got_error_from_errno("strdup");
7548 goto done;
7552 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7553 if (error != NULL)
7554 goto done;
7556 init_curses();
7558 error = apply_unveil(got_repo_get_path(repo), NULL);
7559 if (error)
7560 goto done;
7562 error = tog_load_refs(repo, 0);
7563 if (error)
7564 goto done;
7566 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7567 if (view == NULL) {
7568 error = got_error_from_errno("view_open");
7569 goto done;
7572 error = open_ref_view(view, repo);
7573 if (error)
7574 goto done;
7576 if (worktree) {
7577 /* Release work tree lock. */
7578 got_worktree_close(worktree);
7579 worktree = NULL;
7581 error = view_loop(view);
7582 done:
7583 free(repo_path);
7584 free(cwd);
7585 if (repo) {
7586 const struct got_error *close_err = got_repo_close(repo);
7587 if (close_err)
7588 error = close_err;
7590 if (pack_fds) {
7591 const struct got_error *pack_err =
7592 got_repo_pack_fds_close(pack_fds);
7593 if (error == NULL)
7594 error = pack_err;
7596 tog_free_refs();
7597 return error;
7601 * If view was scrolled down to move the selected line into view when opening a
7602 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7604 static void
7605 offset_selection_up(struct tog_view *view)
7607 switch (view->type) {
7608 case TOG_VIEW_BLAME: {
7609 struct tog_blame_view_state *s = &view->state.blame;
7610 if (s->first_displayed_line == 1) {
7611 s->selected_line = MAX(s->selected_line - view->offset,
7612 1);
7613 break;
7615 if (s->first_displayed_line > view->offset)
7616 s->first_displayed_line -= view->offset;
7617 else
7618 s->first_displayed_line = 1;
7619 s->selected_line += view->offset;
7620 break;
7622 case TOG_VIEW_LOG:
7623 log_scroll_up(&view->state.log, view->offset);
7624 view->state.log.selected += view->offset;
7625 break;
7626 case TOG_VIEW_REF:
7627 ref_scroll_up(&view->state.ref, view->offset);
7628 view->state.ref.selected += view->offset;
7629 break;
7630 case TOG_VIEW_TREE:
7631 tree_scroll_up(&view->state.tree, view->offset);
7632 view->state.tree.selected += view->offset;
7633 break;
7634 default:
7635 break;
7638 view->offset = 0;
7642 * If the selected line is in the section of screen covered by the bottom split,
7643 * scroll down offset lines to move it into view and index its new position.
7645 static const struct got_error *
7646 offset_selection_down(struct tog_view *view)
7648 const struct got_error *err = NULL;
7649 const struct got_error *(*scrolld)(struct tog_view *, int);
7650 int *selected = NULL;
7651 int header, offset;
7653 switch (view->type) {
7654 case TOG_VIEW_BLAME: {
7655 struct tog_blame_view_state *s = &view->state.blame;
7656 header = 3;
7657 scrolld = NULL;
7658 if (s->selected_line > view->nlines - header) {
7659 offset = abs(view->nlines - s->selected_line - header);
7660 s->first_displayed_line += offset;
7661 s->selected_line -= offset;
7662 view->offset = offset;
7664 break;
7666 case TOG_VIEW_LOG: {
7667 struct tog_log_view_state *s = &view->state.log;
7668 scrolld = &log_scroll_down;
7669 header = 3;
7670 selected = &s->selected;
7671 break;
7673 case TOG_VIEW_REF: {
7674 struct tog_ref_view_state *s = &view->state.ref;
7675 scrolld = &ref_scroll_down;
7676 header = 3;
7677 selected = &s->selected;
7678 break;
7680 case TOG_VIEW_TREE: {
7681 struct tog_tree_view_state *s = &view->state.tree;
7682 scrolld = &tree_scroll_down;
7683 header = 5;
7684 selected = &s->selected;
7685 break;
7687 default:
7688 selected = NULL;
7689 scrolld = NULL;
7690 header = 0;
7691 break;
7694 if (selected && *selected > view->nlines - header) {
7695 offset = abs(view->nlines - *selected - header);
7696 view->offset = offset;
7697 if (scrolld && offset) {
7698 err = scrolld(view, offset);
7699 *selected -= offset;
7703 return err;
7706 static void
7707 list_commands(FILE *fp)
7709 size_t i;
7711 fprintf(fp, "commands:");
7712 for (i = 0; i < nitems(tog_commands); i++) {
7713 const struct tog_cmd *cmd = &tog_commands[i];
7714 fprintf(fp, " %s", cmd->name);
7716 fputc('\n', fp);
7719 __dead static void
7720 usage(int hflag, int status)
7722 FILE *fp = (status == 0) ? stdout : stderr;
7724 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7725 getprogname());
7726 if (hflag) {
7727 fprintf(fp, "lazy usage: %s path\n", getprogname());
7728 list_commands(fp);
7730 exit(status);
7733 static char **
7734 make_argv(int argc, ...)
7736 va_list ap;
7737 char **argv;
7738 int i;
7740 va_start(ap, argc);
7742 argv = calloc(argc, sizeof(char *));
7743 if (argv == NULL)
7744 err(1, "calloc");
7745 for (i = 0; i < argc; i++) {
7746 argv[i] = strdup(va_arg(ap, char *));
7747 if (argv[i] == NULL)
7748 err(1, "strdup");
7751 va_end(ap);
7752 return argv;
7756 * Try to convert 'tog path' into a 'tog log path' command.
7757 * The user could simply have mistyped the command rather than knowingly
7758 * provided a path. So check whether argv[0] can in fact be resolved
7759 * to a path in the HEAD commit and print a special error if not.
7760 * This hack is for mpi@ <3
7762 static const struct got_error *
7763 tog_log_with_path(int argc, char *argv[])
7765 const struct got_error *error = NULL, *close_err;
7766 const struct tog_cmd *cmd = NULL;
7767 struct got_repository *repo = NULL;
7768 struct got_worktree *worktree = NULL;
7769 struct got_object_id *commit_id = NULL, *id = NULL;
7770 struct got_commit_object *commit = NULL;
7771 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7772 char *commit_id_str = NULL, **cmd_argv = NULL;
7773 int *pack_fds = NULL;
7775 cwd = getcwd(NULL, 0);
7776 if (cwd == NULL)
7777 return got_error_from_errno("getcwd");
7779 error = got_repo_pack_fds_open(&pack_fds);
7780 if (error != NULL)
7781 goto done;
7783 error = got_worktree_open(&worktree, cwd);
7784 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7785 goto done;
7787 if (worktree)
7788 repo_path = strdup(got_worktree_get_repo_path(worktree));
7789 else
7790 repo_path = strdup(cwd);
7791 if (repo_path == NULL) {
7792 error = got_error_from_errno("strdup");
7793 goto done;
7796 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7797 if (error != NULL)
7798 goto done;
7800 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7801 repo, worktree);
7802 if (error)
7803 goto done;
7805 error = tog_load_refs(repo, 0);
7806 if (error)
7807 goto done;
7808 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7809 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7810 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7811 if (error)
7812 goto done;
7814 if (worktree) {
7815 got_worktree_close(worktree);
7816 worktree = NULL;
7819 error = got_object_open_as_commit(&commit, repo, commit_id);
7820 if (error)
7821 goto done;
7823 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7824 if (error) {
7825 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7826 goto done;
7827 fprintf(stderr, "%s: '%s' is no known command or path\n",
7828 getprogname(), argv[0]);
7829 usage(1, 1);
7830 /* not reached */
7833 close_err = got_repo_close(repo);
7834 if (error == NULL)
7835 error = close_err;
7836 repo = NULL;
7838 error = got_object_id_str(&commit_id_str, commit_id);
7839 if (error)
7840 goto done;
7842 cmd = &tog_commands[0]; /* log */
7843 argc = 4;
7844 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7845 error = cmd->cmd_main(argc, cmd_argv);
7846 done:
7847 if (repo) {
7848 close_err = got_repo_close(repo);
7849 if (error == NULL)
7850 error = close_err;
7852 if (commit)
7853 got_object_commit_close(commit);
7854 if (worktree)
7855 got_worktree_close(worktree);
7856 if (pack_fds) {
7857 const struct got_error *pack_err =
7858 got_repo_pack_fds_close(pack_fds);
7859 if (error == NULL)
7860 error = pack_err;
7862 free(id);
7863 free(commit_id_str);
7864 free(commit_id);
7865 free(cwd);
7866 free(repo_path);
7867 free(in_repo_path);
7868 if (cmd_argv) {
7869 int i;
7870 for (i = 0; i < argc; i++)
7871 free(cmd_argv[i]);
7872 free(cmd_argv);
7874 tog_free_refs();
7875 return error;
7878 int
7879 main(int argc, char *argv[])
7881 const struct got_error *error = NULL;
7882 const struct tog_cmd *cmd = NULL;
7883 int ch, hflag = 0, Vflag = 0;
7884 char **cmd_argv = NULL;
7885 static const struct option longopts[] = {
7886 { "version", no_argument, NULL, 'V' },
7887 { NULL, 0, NULL, 0}
7889 char *diff_algo_str = NULL;
7891 setlocale(LC_CTYPE, "");
7893 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7894 switch (ch) {
7895 case 'h':
7896 hflag = 1;
7897 break;
7898 case 'V':
7899 Vflag = 1;
7900 break;
7901 default:
7902 usage(hflag, 1);
7903 /* NOTREACHED */
7907 argc -= optind;
7908 argv += optind;
7909 optind = 1;
7910 optreset = 1;
7912 if (Vflag) {
7913 got_version_print_str();
7914 return 0;
7917 #ifndef PROFILE
7918 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7919 NULL) == -1)
7920 err(1, "pledge");
7921 #endif
7923 if (argc == 0) {
7924 if (hflag)
7925 usage(hflag, 0);
7926 /* Build an argument vector which runs a default command. */
7927 cmd = &tog_commands[0];
7928 argc = 1;
7929 cmd_argv = make_argv(argc, cmd->name);
7930 } else {
7931 size_t i;
7933 /* Did the user specify a command? */
7934 for (i = 0; i < nitems(tog_commands); i++) {
7935 if (strncmp(tog_commands[i].name, argv[0],
7936 strlen(argv[0])) == 0) {
7937 cmd = &tog_commands[i];
7938 break;
7943 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
7944 if (diff_algo_str) {
7945 if (strcasecmp(diff_algo_str, "patience") == 0)
7946 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
7947 if (strcasecmp(diff_algo_str, "myers") == 0)
7948 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
7951 if (cmd == NULL) {
7952 if (argc != 1)
7953 usage(0, 1);
7954 /* No command specified; try log with a path */
7955 error = tog_log_with_path(argc, argv);
7956 } else {
7957 if (hflag)
7958 cmd->cmd_usage();
7959 else
7960 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7963 endwin();
7964 putchar('\n');
7965 if (cmd_argv) {
7966 int i;
7967 for (i = 0; i < argc; i++)
7968 free(cmd_argv[i]);
7969 free(cmd_argv);
7972 if (error && error->code != GOT_ERR_CANCELLED)
7973 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7974 return 0;