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 mode = getenv("TOG_VIEW_SPLIT_MODE");
2890 if (!mode || mode[0] != 'h') {
2891 view->mode = TOG_VIEW_SPLIT_VERT;
2892 *x = view_split_begin_x(view->begin_x);
2893 } else if (mode && mode[0] == 'h') {
2894 view->mode = TOG_VIEW_SPLIT_HRZN;
2895 *y = view_split_begin_y(view->lines);
2899 /* Split view horizontally at y and offset view->state->selected line. */
2900 static const struct got_error *
2901 view_init_hsplit(struct tog_view *view, int y)
2903 const struct got_error *err = NULL;
2905 view->nlines = y;
2906 err = view_resize(view);
2907 if (err)
2908 return err;
2910 err = offset_selection_down(view);
2912 return err;
2915 static const struct got_error *
2916 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2918 const struct got_error *err = NULL;
2919 struct tog_log_view_state *s = &view->state.log;
2920 struct tog_view *diff_view = NULL, *tree_view = NULL;
2921 struct tog_view *ref_view = NULL;
2922 struct commit_queue_entry *entry;
2923 int begin_x = 0, begin_y = 0, eos, n, nscroll;
2925 if (s->thread_args.load_all) {
2926 if (ch == KEY_BACKSPACE)
2927 s->thread_args.load_all = 0;
2928 else if (s->thread_args.log_complete) {
2929 err = log_move_cursor_down(view, s->commits.ncommits);
2930 s->thread_args.load_all = 0;
2932 return err;
2935 eos = nscroll = view->nlines - 1;
2936 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2937 view_is_splitscreen(view->child))
2938 --eos; /* border */
2941 switch (ch) {
2942 case 'q':
2943 s->quit = 1;
2944 break;
2945 case '0':
2946 view->x = 0;
2947 break;
2948 case '$':
2949 view->x = MAX(view->maxx - view->ncols / 2, 0);
2950 view->count = 0;
2951 break;
2952 case KEY_RIGHT:
2953 case 'l':
2954 if (view->x + view->ncols / 2 < view->maxx)
2955 view->x += 2; /* move two columns right */
2956 else
2957 view->count = 0;
2958 break;
2959 case KEY_LEFT:
2960 case 'h':
2961 view->x -= MIN(view->x, 2); /* move two columns back */
2962 if (view->x <= 0)
2963 view->count = 0;
2964 break;
2965 case 'k':
2966 case KEY_UP:
2967 case '<':
2968 case ',':
2969 case CTRL('p'):
2970 log_move_cursor_up(view, 0, 0);
2971 break;
2972 case 'g':
2973 case KEY_HOME:
2974 log_move_cursor_up(view, 0, 1);
2975 view->count = 0;
2976 break;
2977 case CTRL('u'):
2978 case 'u':
2979 nscroll /= 2;
2980 /* FALL THROUGH */
2981 case KEY_PPAGE:
2982 case CTRL('b'):
2983 case 'b':
2984 log_move_cursor_up(view, nscroll, 0);
2985 break;
2986 case 'j':
2987 case KEY_DOWN:
2988 case '>':
2989 case '.':
2990 case CTRL('n'):
2991 err = log_move_cursor_down(view, 0);
2992 break;
2993 case 'G':
2994 case KEY_END: {
2995 /* We don't know yet how many commits, so we're forced to
2996 * traverse them all. */
2997 view->count = 0;
2998 if (!s->thread_args.log_complete) {
2999 s->thread_args.load_all = 1;
3000 return trigger_log_thread(view, 0);
3003 s->selected = 0;
3004 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3005 for (n = 0; n < eos; n++) {
3006 if (entry == NULL)
3007 break;
3008 s->first_displayed_entry = entry;
3009 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3011 if (n > 0)
3012 s->selected = n - 1;
3013 select_commit(s);
3014 break;
3016 case CTRL('d'):
3017 case 'd':
3018 nscroll /= 2;
3019 /* FALL THROUGH */
3020 case KEY_NPAGE:
3021 case CTRL('f'):
3022 case 'f':
3023 case ' ':
3024 err = log_move_cursor_down(view, nscroll);
3025 break;
3026 case KEY_RESIZE:
3027 if (s->selected > view->nlines - 2)
3028 s->selected = view->nlines - 2;
3029 if (s->selected > s->commits.ncommits - 1)
3030 s->selected = s->commits.ncommits - 1;
3031 select_commit(s);
3032 if (s->commits.ncommits < view->nlines - 1 &&
3033 !s->thread_args.log_complete) {
3034 s->thread_args.commits_needed += (view->nlines - 1) -
3035 s->commits.ncommits;
3036 err = trigger_log_thread(view, 1);
3038 break;
3039 case KEY_ENTER:
3040 case '\r': {
3041 view->count = 0;
3042 if (s->selected_entry == NULL)
3043 break;
3045 /* get dimensions--don't split till initialisation succeeds */
3046 if (view_is_parent_view(view))
3047 view_get_split(view, &begin_y, &begin_x);
3049 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3050 s->selected_entry->commit, s->selected_entry->id,
3051 view, s->repo);
3052 if (err)
3053 break;
3055 if (view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3056 err = view_init_hsplit(view, begin_y);
3057 if (err)
3058 break;
3061 view->focussed = 0;
3062 diff_view->focussed = 1;
3063 diff_view->mode = view->mode;
3064 diff_view->nlines = view->lines - begin_y;
3066 if (view_is_parent_view(view)) {
3067 err = view_close_child(view);
3068 if (err)
3069 return err;
3070 err = view_set_child(view, diff_view);
3071 if (err)
3072 return err;
3073 view->focus_child = 1;
3074 } else
3075 *new_view = diff_view;
3076 break;
3078 case 't':
3079 view->count = 0;
3080 if (s->selected_entry == NULL)
3081 break;
3082 if (view_is_parent_view(view))
3083 begin_x = view_split_begin_x(view->begin_x);
3084 err = browse_commit_tree(&tree_view, begin_x,
3085 s->selected_entry, s->in_repo_path, s->head_ref_name,
3086 s->repo);
3087 if (err)
3088 break;
3089 view->focussed = 0;
3090 tree_view->focussed = 1;
3091 if (view_is_parent_view(view)) {
3092 err = view_close_child(view);
3093 if (err)
3094 return err;
3095 err = view_set_child(view, tree_view);
3096 if (err)
3097 return err;
3098 view->focus_child = 1;
3099 } else
3100 *new_view = tree_view;
3101 break;
3102 case KEY_BACKSPACE:
3103 case CTRL('l'):
3104 case 'B':
3105 view->count = 0;
3106 if (ch == KEY_BACKSPACE &&
3107 got_path_is_root_dir(s->in_repo_path))
3108 break;
3109 err = stop_log_thread(s);
3110 if (err)
3111 return err;
3112 if (ch == KEY_BACKSPACE) {
3113 char *parent_path;
3114 err = got_path_dirname(&parent_path, s->in_repo_path);
3115 if (err)
3116 return err;
3117 free(s->in_repo_path);
3118 s->in_repo_path = parent_path;
3119 s->thread_args.in_repo_path = s->in_repo_path;
3120 } else if (ch == CTRL('l')) {
3121 struct got_object_id *start_id;
3122 err = got_repo_match_object_id(&start_id, NULL,
3123 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3124 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3125 if (err)
3126 return err;
3127 free(s->start_id);
3128 s->start_id = start_id;
3129 s->thread_args.start_id = s->start_id;
3130 } else /* 'B' */
3131 s->log_branches = !s->log_branches;
3133 if (s->thread_args.pack_fds == NULL) {
3134 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3135 if (err)
3136 return err;
3138 err = got_repo_open(&s->thread_args.repo,
3139 got_repo_get_path(s->repo), NULL,
3140 s->thread_args.pack_fds);
3141 if (err)
3142 return err;
3143 tog_free_refs();
3144 err = tog_load_refs(s->repo, 0);
3145 if (err)
3146 return err;
3147 err = got_commit_graph_open(&s->thread_args.graph,
3148 s->in_repo_path, !s->log_branches);
3149 if (err)
3150 return err;
3151 err = got_commit_graph_iter_start(s->thread_args.graph,
3152 s->start_id, s->repo, NULL, NULL);
3153 if (err)
3154 return err;
3155 free_commits(&s->commits);
3156 s->first_displayed_entry = NULL;
3157 s->last_displayed_entry = NULL;
3158 s->selected_entry = NULL;
3159 s->selected = 0;
3160 s->thread_args.log_complete = 0;
3161 s->quit = 0;
3162 s->thread_args.commits_needed = view->lines;
3163 s->matched_entry = NULL;
3164 s->search_entry = NULL;
3165 break;
3166 case 'r':
3167 view->count = 0;
3168 if (view_is_parent_view(view))
3169 begin_x = view_split_begin_x(view->begin_x);
3170 ref_view = view_open(view->nlines, view->ncols,
3171 view->begin_y, begin_x, TOG_VIEW_REF);
3172 if (ref_view == NULL)
3173 return got_error_from_errno("view_open");
3174 err = open_ref_view(ref_view, s->repo);
3175 if (err) {
3176 view_close(ref_view);
3177 return err;
3179 view->focussed = 0;
3180 ref_view->focussed = 1;
3181 if (view_is_parent_view(view)) {
3182 err = view_close_child(view);
3183 if (err)
3184 return err;
3185 err = view_set_child(view, ref_view);
3186 if (err)
3187 return err;
3188 view->focus_child = 1;
3189 } else
3190 *new_view = ref_view;
3191 break;
3192 default:
3193 view->count = 0;
3194 break;
3197 return err;
3200 static const struct got_error *
3201 apply_unveil(const char *repo_path, const char *worktree_path)
3203 const struct got_error *error;
3205 #ifdef PROFILE
3206 if (unveil("gmon.out", "rwc") != 0)
3207 return got_error_from_errno2("unveil", "gmon.out");
3208 #endif
3209 if (repo_path && unveil(repo_path, "r") != 0)
3210 return got_error_from_errno2("unveil", repo_path);
3212 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3213 return got_error_from_errno2("unveil", worktree_path);
3215 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3216 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3218 error = got_privsep_unveil_exec_helpers();
3219 if (error != NULL)
3220 return error;
3222 if (unveil(NULL, NULL) != 0)
3223 return got_error_from_errno("unveil");
3225 return NULL;
3228 static void
3229 init_curses(void)
3232 * Override default signal handlers before starting ncurses.
3233 * This should prevent ncurses from installing its own
3234 * broken cleanup() signal handler.
3236 signal(SIGWINCH, tog_sigwinch);
3237 signal(SIGPIPE, tog_sigpipe);
3238 signal(SIGCONT, tog_sigcont);
3239 signal(SIGINT, tog_sigint);
3240 signal(SIGTERM, tog_sigterm);
3242 initscr();
3243 cbreak();
3244 halfdelay(1); /* Do fast refresh while initial view is loading. */
3245 noecho();
3246 nonl();
3247 intrflush(stdscr, FALSE);
3248 keypad(stdscr, TRUE);
3249 curs_set(0);
3250 if (getenv("TOG_COLORS") != NULL) {
3251 start_color();
3252 use_default_colors();
3256 static const struct got_error *
3257 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3258 struct got_repository *repo, struct got_worktree *worktree)
3260 const struct got_error *err = NULL;
3262 if (argc == 0) {
3263 *in_repo_path = strdup("/");
3264 if (*in_repo_path == NULL)
3265 return got_error_from_errno("strdup");
3266 return NULL;
3269 if (worktree) {
3270 const char *prefix = got_worktree_get_path_prefix(worktree);
3271 char *p;
3273 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3274 if (err)
3275 return err;
3276 if (asprintf(in_repo_path, "%s%s%s", prefix,
3277 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3278 p) == -1) {
3279 err = got_error_from_errno("asprintf");
3280 *in_repo_path = NULL;
3282 free(p);
3283 } else
3284 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3286 return err;
3289 static const struct got_error *
3290 cmd_log(int argc, char *argv[])
3292 const struct got_error *error;
3293 struct got_repository *repo = NULL;
3294 struct got_worktree *worktree = NULL;
3295 struct got_object_id *start_id = NULL;
3296 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3297 char *start_commit = NULL, *label = NULL;
3298 struct got_reference *ref = NULL;
3299 const char *head_ref_name = NULL;
3300 int ch, log_branches = 0;
3301 struct tog_view *view;
3302 int *pack_fds = NULL;
3304 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3305 switch (ch) {
3306 case 'b':
3307 log_branches = 1;
3308 break;
3309 case 'c':
3310 start_commit = optarg;
3311 break;
3312 case 'r':
3313 repo_path = realpath(optarg, NULL);
3314 if (repo_path == NULL)
3315 return got_error_from_errno2("realpath",
3316 optarg);
3317 break;
3318 default:
3319 usage_log();
3320 /* NOTREACHED */
3324 argc -= optind;
3325 argv += optind;
3327 if (argc > 1)
3328 usage_log();
3330 error = got_repo_pack_fds_open(&pack_fds);
3331 if (error != NULL)
3332 goto done;
3334 if (repo_path == NULL) {
3335 cwd = getcwd(NULL, 0);
3336 if (cwd == NULL)
3337 return got_error_from_errno("getcwd");
3338 error = got_worktree_open(&worktree, cwd);
3339 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3340 goto done;
3341 if (worktree)
3342 repo_path =
3343 strdup(got_worktree_get_repo_path(worktree));
3344 else
3345 repo_path = strdup(cwd);
3346 if (repo_path == NULL) {
3347 error = got_error_from_errno("strdup");
3348 goto done;
3352 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3353 if (error != NULL)
3354 goto done;
3356 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3357 repo, worktree);
3358 if (error)
3359 goto done;
3361 init_curses();
3363 error = apply_unveil(got_repo_get_path(repo),
3364 worktree ? got_worktree_get_root_path(worktree) : NULL);
3365 if (error)
3366 goto done;
3368 /* already loaded by tog_log_with_path()? */
3369 if (TAILQ_EMPTY(&tog_refs)) {
3370 error = tog_load_refs(repo, 0);
3371 if (error)
3372 goto done;
3375 if (start_commit == NULL) {
3376 error = got_repo_match_object_id(&start_id, &label,
3377 worktree ? got_worktree_get_head_ref_name(worktree) :
3378 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3379 if (error)
3380 goto done;
3381 head_ref_name = label;
3382 } else {
3383 error = got_ref_open(&ref, repo, start_commit, 0);
3384 if (error == NULL)
3385 head_ref_name = got_ref_get_name(ref);
3386 else if (error->code != GOT_ERR_NOT_REF)
3387 goto done;
3388 error = got_repo_match_object_id(&start_id, NULL,
3389 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3390 if (error)
3391 goto done;
3394 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3395 if (view == NULL) {
3396 error = got_error_from_errno("view_open");
3397 goto done;
3399 error = open_log_view(view, start_id, repo, head_ref_name,
3400 in_repo_path, log_branches);
3401 if (error)
3402 goto done;
3403 if (worktree) {
3404 /* Release work tree lock. */
3405 got_worktree_close(worktree);
3406 worktree = NULL;
3408 error = view_loop(view);
3409 done:
3410 free(in_repo_path);
3411 free(repo_path);
3412 free(cwd);
3413 free(start_id);
3414 free(label);
3415 if (ref)
3416 got_ref_close(ref);
3417 if (repo) {
3418 const struct got_error *close_err = got_repo_close(repo);
3419 if (error == NULL)
3420 error = close_err;
3422 if (worktree)
3423 got_worktree_close(worktree);
3424 if (pack_fds) {
3425 const struct got_error *pack_err =
3426 got_repo_pack_fds_close(pack_fds);
3427 if (error == NULL)
3428 error = pack_err;
3430 tog_free_refs();
3431 return error;
3434 __dead static void
3435 usage_diff(void)
3437 endwin();
3438 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3439 "[-w] object1 object2\n", getprogname());
3440 exit(1);
3443 static int
3444 match_line(const char *line, regex_t *regex, size_t nmatch,
3445 regmatch_t *regmatch)
3447 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3450 static struct tog_color *
3451 match_color(struct tog_colors *colors, const char *line)
3453 struct tog_color *tc = NULL;
3455 STAILQ_FOREACH(tc, colors, entry) {
3456 if (match_line(line, &tc->regex, 0, NULL))
3457 return tc;
3460 return NULL;
3463 static const struct got_error *
3464 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3465 WINDOW *window, int skipcol, regmatch_t *regmatch)
3467 const struct got_error *err = NULL;
3468 char *exstr = NULL;
3469 wchar_t *wline = NULL;
3470 int rme, rms, n, width, scrollx;
3471 int width0 = 0, width1 = 0, width2 = 0;
3472 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3474 *wtotal = 0;
3476 rms = regmatch->rm_so;
3477 rme = regmatch->rm_eo;
3479 err = expand_tab(&exstr, line);
3480 if (err)
3481 return err;
3483 /* Split the line into 3 segments, according to match offsets. */
3484 seg0 = strndup(exstr, rms);
3485 if (seg0 == NULL) {
3486 err = got_error_from_errno("strndup");
3487 goto done;
3489 seg1 = strndup(exstr + rms, rme - rms);
3490 if (seg1 == NULL) {
3491 err = got_error_from_errno("strndup");
3492 goto done;
3494 seg2 = strdup(exstr + rme);
3495 if (seg2 == NULL) {
3496 err = got_error_from_errno("strndup");
3497 goto done;
3500 /* draw up to matched token if we haven't scrolled past it */
3501 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3502 col_tab_align, 1);
3503 if (err)
3504 goto done;
3505 n = MAX(width0 - skipcol, 0);
3506 if (n) {
3507 free(wline);
3508 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3509 wlimit, col_tab_align, 1);
3510 if (err)
3511 goto done;
3512 waddwstr(window, &wline[scrollx]);
3513 wlimit -= width;
3514 *wtotal += width;
3517 if (wlimit > 0) {
3518 int i = 0, w = 0;
3519 size_t wlen;
3521 free(wline);
3522 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3523 col_tab_align, 1);
3524 if (err)
3525 goto done;
3526 wlen = wcslen(wline);
3527 while (i < wlen) {
3528 width = wcwidth(wline[i]);
3529 if (width == -1) {
3530 /* should not happen, tabs are expanded */
3531 err = got_error(GOT_ERR_RANGE);
3532 goto done;
3534 if (width0 + w + width > skipcol)
3535 break;
3536 w += width;
3537 i++;
3539 /* draw (visible part of) matched token (if scrolled into it) */
3540 if (width1 - w > 0) {
3541 wattron(window, A_STANDOUT);
3542 waddwstr(window, &wline[i]);
3543 wattroff(window, A_STANDOUT);
3544 wlimit -= (width1 - w);
3545 *wtotal += (width1 - w);
3549 if (wlimit > 0) { /* draw rest of line */
3550 free(wline);
3551 if (skipcol > width0 + width1) {
3552 err = format_line(&wline, &width2, &scrollx, seg2,
3553 skipcol - (width0 + width1), wlimit,
3554 col_tab_align, 1);
3555 if (err)
3556 goto done;
3557 waddwstr(window, &wline[scrollx]);
3558 } else {
3559 err = format_line(&wline, &width2, NULL, seg2, 0,
3560 wlimit, col_tab_align, 1);
3561 if (err)
3562 goto done;
3563 waddwstr(window, wline);
3565 *wtotal += width2;
3567 done:
3568 free(wline);
3569 free(exstr);
3570 free(seg0);
3571 free(seg1);
3572 free(seg2);
3573 return err;
3576 static const struct got_error *
3577 draw_file(struct tog_view *view, const char *header)
3579 struct tog_diff_view_state *s = &view->state.diff;
3580 regmatch_t *regmatch = &view->regmatch;
3581 const struct got_error *err;
3582 int nprinted = 0;
3583 char *line;
3584 size_t linesize = 0;
3585 ssize_t linelen;
3586 struct tog_color *tc;
3587 wchar_t *wline;
3588 int width;
3589 int max_lines = view->nlines;
3590 int nlines = s->nlines;
3591 off_t line_offset;
3593 line_offset = s->line_offsets[s->first_displayed_line - 1];
3594 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3595 return got_error_from_errno("fseek");
3597 werase(view->window);
3599 if (header) {
3600 if (asprintf(&line, "[%d/%d] %s",
3601 s->first_displayed_line - 1 + s->selected_line, nlines,
3602 header) == -1)
3603 return got_error_from_errno("asprintf");
3604 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3605 0, 0);
3606 free(line);
3607 if (err)
3608 return err;
3610 if (view_needs_focus_indication(view))
3611 wstandout(view->window);
3612 waddwstr(view->window, wline);
3613 free(wline);
3614 wline = NULL;
3615 if (view_needs_focus_indication(view))
3616 wstandend(view->window);
3617 if (width <= view->ncols - 1)
3618 waddch(view->window, '\n');
3620 if (max_lines <= 1)
3621 return NULL;
3622 max_lines--;
3625 s->eof = 0;
3626 view->maxx = 0;
3627 line = NULL;
3628 while (max_lines > 0 && nprinted < max_lines) {
3629 linelen = getline(&line, &linesize, s->f);
3630 if (linelen == -1) {
3631 if (feof(s->f)) {
3632 s->eof = 1;
3633 break;
3635 free(line);
3636 return got_ferror(s->f, GOT_ERR_IO);
3639 /* Set view->maxx based on full line length. */
3640 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3641 view->x ? 1 : 0);
3642 if (err) {
3643 free(line);
3644 return err;
3646 view->maxx = MAX(view->maxx, width);
3647 free(wline);
3648 wline = NULL;
3650 tc = match_color(&s->colors, line);
3651 if (tc)
3652 wattr_on(view->window,
3653 COLOR_PAIR(tc->colorpair), NULL);
3654 if (s->first_displayed_line + nprinted == s->matched_line &&
3655 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3656 err = add_matched_line(&width, line, view->ncols, 0,
3657 view->window, view->x, regmatch);
3658 if (err) {
3659 free(line);
3660 return err;
3662 } else {
3663 int skip;
3664 err = format_line(&wline, &width, &skip, line,
3665 view->x, view->ncols, 0, view->x ? 1 : 0);
3666 if (err) {
3667 free(line);
3668 return err;
3670 waddwstr(view->window, &wline[skip]);
3671 free(wline);
3672 wline = NULL;
3674 if (tc)
3675 wattr_off(view->window,
3676 COLOR_PAIR(tc->colorpair), NULL);
3677 if (width <= view->ncols - 1)
3678 waddch(view->window, '\n');
3679 nprinted++;
3681 free(line);
3682 if (nprinted >= 1)
3683 s->last_displayed_line = s->first_displayed_line +
3684 (nprinted - 1);
3685 else
3686 s->last_displayed_line = s->first_displayed_line;
3688 view_border(view);
3690 if (s->eof) {
3691 while (nprinted < view->nlines) {
3692 waddch(view->window, '\n');
3693 nprinted++;
3696 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3697 view->ncols, 0, 0);
3698 if (err) {
3699 return err;
3702 wstandout(view->window);
3703 waddwstr(view->window, wline);
3704 free(wline);
3705 wline = NULL;
3706 wstandend(view->window);
3709 return NULL;
3712 static char *
3713 get_datestr(time_t *time, char *datebuf)
3715 struct tm mytm, *tm;
3716 char *p, *s;
3718 tm = gmtime_r(time, &mytm);
3719 if (tm == NULL)
3720 return NULL;
3721 s = asctime_r(tm, datebuf);
3722 if (s == NULL)
3723 return NULL;
3724 p = strchr(s, '\n');
3725 if (p)
3726 *p = '\0';
3727 return s;
3730 static const struct got_error *
3731 get_changed_paths(struct got_pathlist_head *paths,
3732 struct got_commit_object *commit, struct got_repository *repo)
3734 const struct got_error *err = NULL;
3735 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3736 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3737 struct got_object_qid *qid;
3739 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3740 if (qid != NULL) {
3741 struct got_commit_object *pcommit;
3742 err = got_object_open_as_commit(&pcommit, repo,
3743 &qid->id);
3744 if (err)
3745 return err;
3747 tree_id1 = got_object_id_dup(
3748 got_object_commit_get_tree_id(pcommit));
3749 if (tree_id1 == NULL) {
3750 got_object_commit_close(pcommit);
3751 return got_error_from_errno("got_object_id_dup");
3753 got_object_commit_close(pcommit);
3757 if (tree_id1) {
3758 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3759 if (err)
3760 goto done;
3763 tree_id2 = got_object_commit_get_tree_id(commit);
3764 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3765 if (err)
3766 goto done;
3768 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3769 got_diff_tree_collect_changed_paths, paths, 0);
3770 done:
3771 if (tree1)
3772 got_object_tree_close(tree1);
3773 if (tree2)
3774 got_object_tree_close(tree2);
3775 free(tree_id1);
3776 return err;
3779 static const struct got_error *
3780 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3782 off_t *p;
3784 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3785 if (p == NULL)
3786 return got_error_from_errno("reallocarray");
3787 *line_offsets = p;
3788 (*line_offsets)[*nlines] = off;
3789 (*nlines)++;
3790 return NULL;
3793 static const struct got_error *
3794 write_commit_info(off_t **line_offsets, size_t *nlines,
3795 struct got_object_id *commit_id, struct got_reflist_head *refs,
3796 struct got_repository *repo, FILE *outfile)
3798 const struct got_error *err = NULL;
3799 char datebuf[26], *datestr;
3800 struct got_commit_object *commit;
3801 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3802 time_t committer_time;
3803 const char *author, *committer;
3804 char *refs_str = NULL;
3805 struct got_pathlist_head changed_paths;
3806 struct got_pathlist_entry *pe;
3807 off_t outoff = 0;
3808 int n;
3810 TAILQ_INIT(&changed_paths);
3812 if (refs) {
3813 err = build_refs_str(&refs_str, refs, commit_id, repo);
3814 if (err)
3815 return err;
3818 err = got_object_open_as_commit(&commit, repo, commit_id);
3819 if (err)
3820 return err;
3822 err = got_object_id_str(&id_str, commit_id);
3823 if (err) {
3824 err = got_error_from_errno("got_object_id_str");
3825 goto done;
3828 err = add_line_offset(line_offsets, nlines, 0);
3829 if (err)
3830 goto done;
3832 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3833 refs_str ? refs_str : "", refs_str ? ")" : "");
3834 if (n < 0) {
3835 err = got_error_from_errno("fprintf");
3836 goto done;
3838 outoff += n;
3839 err = add_line_offset(line_offsets, nlines, outoff);
3840 if (err)
3841 goto done;
3843 n = fprintf(outfile, "from: %s\n",
3844 got_object_commit_get_author(commit));
3845 if (n < 0) {
3846 err = got_error_from_errno("fprintf");
3847 goto done;
3849 outoff += n;
3850 err = add_line_offset(line_offsets, nlines, outoff);
3851 if (err)
3852 goto done;
3854 committer_time = got_object_commit_get_committer_time(commit);
3855 datestr = get_datestr(&committer_time, datebuf);
3856 if (datestr) {
3857 n = fprintf(outfile, "date: %s UTC\n", datestr);
3858 if (n < 0) {
3859 err = got_error_from_errno("fprintf");
3860 goto done;
3862 outoff += n;
3863 err = add_line_offset(line_offsets, nlines, outoff);
3864 if (err)
3865 goto done;
3867 author = got_object_commit_get_author(commit);
3868 committer = got_object_commit_get_committer(commit);
3869 if (strcmp(author, committer) != 0) {
3870 n = fprintf(outfile, "via: %s\n", committer);
3871 if (n < 0) {
3872 err = got_error_from_errno("fprintf");
3873 goto done;
3875 outoff += n;
3876 err = add_line_offset(line_offsets, nlines, outoff);
3877 if (err)
3878 goto done;
3880 if (got_object_commit_get_nparents(commit) > 1) {
3881 const struct got_object_id_queue *parent_ids;
3882 struct got_object_qid *qid;
3883 int pn = 1;
3884 parent_ids = got_object_commit_get_parent_ids(commit);
3885 STAILQ_FOREACH(qid, parent_ids, entry) {
3886 err = got_object_id_str(&id_str, &qid->id);
3887 if (err)
3888 goto done;
3889 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3890 if (n < 0) {
3891 err = got_error_from_errno("fprintf");
3892 goto done;
3894 outoff += n;
3895 err = add_line_offset(line_offsets, nlines, outoff);
3896 if (err)
3897 goto done;
3898 free(id_str);
3899 id_str = NULL;
3903 err = got_object_commit_get_logmsg(&logmsg, commit);
3904 if (err)
3905 goto done;
3906 s = logmsg;
3907 while ((line = strsep(&s, "\n")) != NULL) {
3908 n = fprintf(outfile, "%s\n", line);
3909 if (n < 0) {
3910 err = got_error_from_errno("fprintf");
3911 goto done;
3913 outoff += n;
3914 err = add_line_offset(line_offsets, nlines, outoff);
3915 if (err)
3916 goto done;
3919 err = get_changed_paths(&changed_paths, commit, repo);
3920 if (err)
3921 goto done;
3922 TAILQ_FOREACH(pe, &changed_paths, entry) {
3923 struct got_diff_changed_path *cp = pe->data;
3924 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3925 if (n < 0) {
3926 err = got_error_from_errno("fprintf");
3927 goto done;
3929 outoff += n;
3930 err = add_line_offset(line_offsets, nlines, outoff);
3931 if (err)
3932 goto done;
3933 free((char *)pe->path);
3934 free(pe->data);
3937 fputc('\n', outfile);
3938 outoff++;
3939 err = add_line_offset(line_offsets, nlines, outoff);
3940 done:
3941 got_pathlist_free(&changed_paths);
3942 free(id_str);
3943 free(logmsg);
3944 free(refs_str);
3945 got_object_commit_close(commit);
3946 if (err) {
3947 free(*line_offsets);
3948 *line_offsets = NULL;
3949 *nlines = 0;
3951 return err;
3954 static const struct got_error *
3955 create_diff(struct tog_diff_view_state *s)
3957 const struct got_error *err = NULL;
3958 FILE *f = NULL;
3959 int obj_type;
3961 free(s->line_offsets);
3962 s->line_offsets = malloc(sizeof(off_t));
3963 if (s->line_offsets == NULL)
3964 return got_error_from_errno("malloc");
3965 s->nlines = 0;
3967 f = got_opentemp();
3968 if (f == NULL) {
3969 err = got_error_from_errno("got_opentemp");
3970 goto done;
3972 if (s->f && fclose(s->f) == EOF) {
3973 err = got_error_from_errno("fclose");
3974 goto done;
3976 s->f = f;
3978 if (s->id1)
3979 err = got_object_get_type(&obj_type, s->repo, s->id1);
3980 else
3981 err = got_object_get_type(&obj_type, s->repo, s->id2);
3982 if (err)
3983 goto done;
3985 switch (obj_type) {
3986 case GOT_OBJ_TYPE_BLOB:
3987 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3988 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
3989 s->label1, s->label2, tog_diff_algo, s->diff_context,
3990 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3991 break;
3992 case GOT_OBJ_TYPE_TREE:
3993 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3994 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
3995 tog_diff_algo, s->diff_context, s->ignore_whitespace,
3996 s->force_text_diff, s->repo, s->f);
3997 break;
3998 case GOT_OBJ_TYPE_COMMIT: {
3999 const struct got_object_id_queue *parent_ids;
4000 struct got_object_qid *pid;
4001 struct got_commit_object *commit2;
4002 struct got_reflist_head *refs;
4004 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4005 if (err)
4006 goto done;
4007 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4008 /* Show commit info if we're diffing to a parent/root commit. */
4009 if (s->id1 == NULL) {
4010 err = write_commit_info(&s->line_offsets, &s->nlines,
4011 s->id2, refs, s->repo, s->f);
4012 if (err)
4013 goto done;
4014 } else {
4015 parent_ids = got_object_commit_get_parent_ids(commit2);
4016 STAILQ_FOREACH(pid, parent_ids, entry) {
4017 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4018 err = write_commit_info(
4019 &s->line_offsets, &s->nlines,
4020 s->id2, refs, s->repo, s->f);
4021 if (err)
4022 goto done;
4023 break;
4027 got_object_commit_close(commit2);
4029 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4030 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4031 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4032 s->force_text_diff, s->repo, s->f);
4033 break;
4035 default:
4036 err = got_error(GOT_ERR_OBJ_TYPE);
4037 break;
4039 if (err)
4040 goto done;
4041 done:
4042 if (s->f && fflush(s->f) != 0 && err == NULL)
4043 err = got_error_from_errno("fflush");
4044 return err;
4047 static void
4048 diff_view_indicate_progress(struct tog_view *view)
4050 mvwaddstr(view->window, 0, 0, "diffing...");
4051 update_panels();
4052 doupdate();
4055 static const struct got_error *
4056 search_start_diff_view(struct tog_view *view)
4058 struct tog_diff_view_state *s = &view->state.diff;
4060 s->matched_line = 0;
4061 return NULL;
4064 static const struct got_error *
4065 search_next_diff_view(struct tog_view *view)
4067 struct tog_diff_view_state *s = &view->state.diff;
4068 const struct got_error *err = NULL;
4069 int lineno;
4070 char *line = NULL;
4071 size_t linesize = 0;
4072 ssize_t linelen;
4074 if (!view->searching) {
4075 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4076 return NULL;
4079 if (s->matched_line) {
4080 if (view->searching == TOG_SEARCH_FORWARD)
4081 lineno = s->matched_line + 1;
4082 else
4083 lineno = s->matched_line - 1;
4084 } else
4085 lineno = s->first_displayed_line;
4087 while (1) {
4088 off_t offset;
4090 if (lineno <= 0 || lineno > s->nlines) {
4091 if (s->matched_line == 0) {
4092 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4093 break;
4096 if (view->searching == TOG_SEARCH_FORWARD)
4097 lineno = 1;
4098 else
4099 lineno = s->nlines;
4102 offset = s->line_offsets[lineno - 1];
4103 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4104 free(line);
4105 return got_error_from_errno("fseeko");
4107 linelen = getline(&line, &linesize, s->f);
4108 if (linelen != -1) {
4109 char *exstr;
4110 err = expand_tab(&exstr, line);
4111 if (err)
4112 break;
4113 if (match_line(exstr, &view->regex, 1,
4114 &view->regmatch)) {
4115 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4116 s->matched_line = lineno;
4117 free(exstr);
4118 break;
4120 free(exstr);
4122 if (view->searching == TOG_SEARCH_FORWARD)
4123 lineno++;
4124 else
4125 lineno--;
4127 free(line);
4129 if (s->matched_line) {
4130 s->first_displayed_line = s->matched_line;
4131 s->selected_line = 1;
4134 return err;
4137 static const struct got_error *
4138 close_diff_view(struct tog_view *view)
4140 const struct got_error *err = NULL;
4141 struct tog_diff_view_state *s = &view->state.diff;
4143 free(s->id1);
4144 s->id1 = NULL;
4145 free(s->id2);
4146 s->id2 = NULL;
4147 if (s->f && fclose(s->f) == EOF)
4148 err = got_error_from_errno("fclose");
4149 s->f = NULL;
4150 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4151 err = got_error_from_errno("fclose");
4152 s->f1 = NULL;
4153 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4154 err = got_error_from_errno("fclose");
4155 s->f2 = NULL;
4156 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4157 err = got_error_from_errno("close");
4158 s->fd1 = -1;
4159 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4160 err = got_error_from_errno("close");
4161 s->fd2 = -1;
4162 free_colors(&s->colors);
4163 free(s->line_offsets);
4164 s->line_offsets = NULL;
4165 s->nlines = 0;
4166 return err;
4169 static const struct got_error *
4170 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4171 struct got_object_id *id2, const char *label1, const char *label2,
4172 int diff_context, int ignore_whitespace, int force_text_diff,
4173 struct tog_view *log_view, struct got_repository *repo)
4175 const struct got_error *err;
4176 struct tog_diff_view_state *s = &view->state.diff;
4178 memset(s, 0, sizeof(*s));
4179 s->fd1 = -1;
4180 s->fd2 = -1;
4182 if (id1 != NULL && id2 != NULL) {
4183 int type1, type2;
4184 err = got_object_get_type(&type1, repo, id1);
4185 if (err)
4186 return err;
4187 err = got_object_get_type(&type2, repo, id2);
4188 if (err)
4189 return err;
4191 if (type1 != type2)
4192 return got_error(GOT_ERR_OBJ_TYPE);
4194 s->first_displayed_line = 1;
4195 s->last_displayed_line = view->nlines;
4196 s->selected_line = 1;
4197 s->repo = repo;
4198 s->id1 = id1;
4199 s->id2 = id2;
4200 s->label1 = label1;
4201 s->label2 = label2;
4203 if (id1) {
4204 s->id1 = got_object_id_dup(id1);
4205 if (s->id1 == NULL)
4206 return got_error_from_errno("got_object_id_dup");
4207 } else
4208 s->id1 = NULL;
4210 s->id2 = got_object_id_dup(id2);
4211 if (s->id2 == NULL) {
4212 err = got_error_from_errno("got_object_id_dup");
4213 goto done;
4216 s->f1 = got_opentemp();
4217 if (s->f1 == NULL) {
4218 err = got_error_from_errno("got_opentemp");
4219 goto done;
4222 s->f2 = got_opentemp();
4223 if (s->f2 == NULL) {
4224 err = got_error_from_errno("got_opentemp");
4225 goto done;
4228 s->fd1 = got_opentempfd();
4229 if (s->fd1 == -1) {
4230 err = got_error_from_errno("got_opentempfd");
4231 goto done;
4234 s->fd2 = got_opentempfd();
4235 if (s->fd2 == -1) {
4236 err = got_error_from_errno("got_opentempfd");
4237 goto done;
4240 s->first_displayed_line = 1;
4241 s->last_displayed_line = view->nlines;
4242 s->diff_context = diff_context;
4243 s->ignore_whitespace = ignore_whitespace;
4244 s->force_text_diff = force_text_diff;
4245 s->log_view = log_view;
4246 s->repo = repo;
4248 STAILQ_INIT(&s->colors);
4249 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4250 err = add_color(&s->colors,
4251 "^-", TOG_COLOR_DIFF_MINUS,
4252 get_color_value("TOG_COLOR_DIFF_MINUS"));
4253 if (err)
4254 goto done;
4255 err = add_color(&s->colors, "^\\+",
4256 TOG_COLOR_DIFF_PLUS,
4257 get_color_value("TOG_COLOR_DIFF_PLUS"));
4258 if (err)
4259 goto done;
4260 err = add_color(&s->colors,
4261 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4262 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4263 if (err)
4264 goto done;
4266 err = add_color(&s->colors,
4267 "^(commit [0-9a-f]|parent [0-9]|"
4268 "(blob|file|tree|commit) [-+] |"
4269 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4270 get_color_value("TOG_COLOR_DIFF_META"));
4271 if (err)
4272 goto done;
4274 err = add_color(&s->colors,
4275 "^(from|via): ", TOG_COLOR_AUTHOR,
4276 get_color_value("TOG_COLOR_AUTHOR"));
4277 if (err)
4278 goto done;
4280 err = add_color(&s->colors,
4281 "^date: ", TOG_COLOR_DATE,
4282 get_color_value("TOG_COLOR_DATE"));
4283 if (err)
4284 goto done;
4287 if (log_view && view_is_splitscreen(view))
4288 show_log_view(log_view); /* draw vborder */
4289 diff_view_indicate_progress(view);
4291 err = create_diff(s);
4293 view->show = show_diff_view;
4294 view->input = input_diff_view;
4295 view->reset = reset_diff_view;
4296 view->close = close_diff_view;
4297 view->search_start = search_start_diff_view;
4298 view->search_next = search_next_diff_view;
4299 done:
4300 if (err)
4301 close_diff_view(view);
4302 return err;
4305 static const struct got_error *
4306 show_diff_view(struct tog_view *view)
4308 const struct got_error *err;
4309 struct tog_diff_view_state *s = &view->state.diff;
4310 char *id_str1 = NULL, *id_str2, *header;
4311 const char *label1, *label2;
4313 if (s->id1) {
4314 err = got_object_id_str(&id_str1, s->id1);
4315 if (err)
4316 return err;
4317 label1 = s->label1 ? : id_str1;
4318 } else
4319 label1 = "/dev/null";
4321 err = got_object_id_str(&id_str2, s->id2);
4322 if (err)
4323 return err;
4324 label2 = s->label2 ? : id_str2;
4326 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4327 err = got_error_from_errno("asprintf");
4328 free(id_str1);
4329 free(id_str2);
4330 return err;
4332 free(id_str1);
4333 free(id_str2);
4335 err = draw_file(view, header);
4336 free(header);
4337 return err;
4340 static const struct got_error *
4341 set_selected_commit(struct tog_diff_view_state *s,
4342 struct commit_queue_entry *entry)
4344 const struct got_error *err;
4345 const struct got_object_id_queue *parent_ids;
4346 struct got_commit_object *selected_commit;
4347 struct got_object_qid *pid;
4349 free(s->id2);
4350 s->id2 = got_object_id_dup(entry->id);
4351 if (s->id2 == NULL)
4352 return got_error_from_errno("got_object_id_dup");
4354 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4355 if (err)
4356 return err;
4357 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4358 free(s->id1);
4359 pid = STAILQ_FIRST(parent_ids);
4360 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4361 got_object_commit_close(selected_commit);
4362 return NULL;
4365 static const struct got_error *
4366 reset_diff_view(struct tog_view *view)
4368 struct tog_diff_view_state *s = &view->state.diff;
4370 view->count = 0;
4371 wclear(view->window);
4372 s->first_displayed_line = 1;
4373 s->last_displayed_line = view->nlines;
4374 s->matched_line = 0;
4375 diff_view_indicate_progress(view);
4376 return create_diff(s);
4379 static const struct got_error *
4380 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4382 const struct got_error *err = NULL;
4383 struct tog_diff_view_state *s = &view->state.diff;
4384 struct tog_log_view_state *ls;
4385 struct commit_queue_entry *old_selected_entry;
4386 char *line = NULL;
4387 size_t linesize = 0;
4388 ssize_t linelen;
4389 int i, nscroll = view->nlines - 1;
4391 switch (ch) {
4392 case '0':
4393 view->x = 0;
4394 break;
4395 case '$':
4396 view->x = MAX(view->maxx - view->ncols / 3, 0);
4397 view->count = 0;
4398 break;
4399 case KEY_RIGHT:
4400 case 'l':
4401 if (view->x + view->ncols / 3 < view->maxx)
4402 view->x += 2; /* move two columns right */
4403 else
4404 view->count = 0;
4405 break;
4406 case KEY_LEFT:
4407 case 'h':
4408 view->x -= MIN(view->x, 2); /* move two columns back */
4409 if (view->x <= 0)
4410 view->count = 0;
4411 break;
4412 case 'a':
4413 case 'w':
4414 if (ch == 'a')
4415 s->force_text_diff = !s->force_text_diff;
4416 if (ch == 'w')
4417 s->ignore_whitespace = !s->ignore_whitespace;
4418 err = reset_diff_view(view);
4419 break;
4420 case 'g':
4421 case KEY_HOME:
4422 s->first_displayed_line = 1;
4423 view->count = 0;
4424 break;
4425 case 'G':
4426 case KEY_END:
4427 view->count = 0;
4428 if (s->eof)
4429 break;
4431 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4432 s->eof = 1;
4433 break;
4434 case 'k':
4435 case KEY_UP:
4436 case CTRL('p'):
4437 if (s->first_displayed_line > 1)
4438 s->first_displayed_line--;
4439 else
4440 view->count = 0;
4441 break;
4442 case CTRL('u'):
4443 case 'u':
4444 nscroll /= 2;
4445 /* FALL THROUGH */
4446 case KEY_PPAGE:
4447 case CTRL('b'):
4448 case 'b':
4449 if (s->first_displayed_line == 1) {
4450 view->count = 0;
4451 break;
4453 i = 0;
4454 while (i++ < nscroll && s->first_displayed_line > 1)
4455 s->first_displayed_line--;
4456 break;
4457 case 'j':
4458 case KEY_DOWN:
4459 case CTRL('n'):
4460 if (!s->eof)
4461 s->first_displayed_line++;
4462 else
4463 view->count = 0;
4464 break;
4465 case CTRL('d'):
4466 case 'd':
4467 nscroll /= 2;
4468 /* FALL THROUGH */
4469 case KEY_NPAGE:
4470 case CTRL('f'):
4471 case 'f':
4472 case ' ':
4473 if (s->eof) {
4474 view->count = 0;
4475 break;
4477 i = 0;
4478 while (!s->eof && i++ < nscroll) {
4479 linelen = getline(&line, &linesize, s->f);
4480 s->first_displayed_line++;
4481 if (linelen == -1) {
4482 if (feof(s->f)) {
4483 s->eof = 1;
4484 } else
4485 err = got_ferror(s->f, GOT_ERR_IO);
4486 break;
4489 free(line);
4490 break;
4491 case '[':
4492 if (s->diff_context > 0) {
4493 s->diff_context--;
4494 s->matched_line = 0;
4495 diff_view_indicate_progress(view);
4496 err = create_diff(s);
4497 if (s->first_displayed_line + view->nlines - 1 >
4498 s->nlines) {
4499 s->first_displayed_line = 1;
4500 s->last_displayed_line = view->nlines;
4502 } else
4503 view->count = 0;
4504 break;
4505 case ']':
4506 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4507 s->diff_context++;
4508 s->matched_line = 0;
4509 diff_view_indicate_progress(view);
4510 err = create_diff(s);
4511 } else
4512 view->count = 0;
4513 break;
4514 case '<':
4515 case ',':
4516 if (s->log_view == NULL) {
4517 view->count = 0;
4518 break;
4520 ls = &s->log_view->state.log;
4521 old_selected_entry = ls->selected_entry;
4523 /* view->count handled in input_log_view() */
4524 err = input_log_view(NULL, s->log_view, KEY_UP);
4525 if (err)
4526 break;
4528 if (old_selected_entry == ls->selected_entry)
4529 break;
4531 err = set_selected_commit(s, ls->selected_entry);
4532 if (err)
4533 break;
4535 s->first_displayed_line = 1;
4536 s->last_displayed_line = view->nlines;
4537 s->matched_line = 0;
4538 view->x = 0;
4540 diff_view_indicate_progress(view);
4541 err = create_diff(s);
4542 break;
4543 case '>':
4544 case '.':
4545 if (s->log_view == NULL) {
4546 view->count = 0;
4547 break;
4549 ls = &s->log_view->state.log;
4550 old_selected_entry = ls->selected_entry;
4552 /* view->count handled in input_log_view() */
4553 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4554 if (err)
4555 break;
4557 if (old_selected_entry == ls->selected_entry)
4558 break;
4560 err = set_selected_commit(s, ls->selected_entry);
4561 if (err)
4562 break;
4564 s->first_displayed_line = 1;
4565 s->last_displayed_line = view->nlines;
4566 s->matched_line = 0;
4567 view->x = 0;
4569 diff_view_indicate_progress(view);
4570 err = create_diff(s);
4571 break;
4572 default:
4573 view->count = 0;
4574 break;
4577 return err;
4580 static const struct got_error *
4581 cmd_diff(int argc, char *argv[])
4583 const struct got_error *error = NULL;
4584 struct got_repository *repo = NULL;
4585 struct got_worktree *worktree = NULL;
4586 struct got_object_id *id1 = NULL, *id2 = NULL;
4587 char *repo_path = NULL, *cwd = NULL;
4588 char *id_str1 = NULL, *id_str2 = NULL;
4589 char *label1 = NULL, *label2 = NULL;
4590 int diff_context = 3, ignore_whitespace = 0;
4591 int ch, force_text_diff = 0;
4592 const char *errstr;
4593 struct tog_view *view;
4594 int *pack_fds = NULL;
4596 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4597 switch (ch) {
4598 case 'a':
4599 force_text_diff = 1;
4600 break;
4601 case 'C':
4602 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4603 &errstr);
4604 if (errstr != NULL)
4605 errx(1, "number of context lines is %s: %s",
4606 errstr, errstr);
4607 break;
4608 case 'r':
4609 repo_path = realpath(optarg, NULL);
4610 if (repo_path == NULL)
4611 return got_error_from_errno2("realpath",
4612 optarg);
4613 got_path_strip_trailing_slashes(repo_path);
4614 break;
4615 case 'w':
4616 ignore_whitespace = 1;
4617 break;
4618 default:
4619 usage_diff();
4620 /* NOTREACHED */
4624 argc -= optind;
4625 argv += optind;
4627 if (argc == 0) {
4628 usage_diff(); /* TODO show local worktree changes */
4629 } else if (argc == 2) {
4630 id_str1 = argv[0];
4631 id_str2 = argv[1];
4632 } else
4633 usage_diff();
4635 error = got_repo_pack_fds_open(&pack_fds);
4636 if (error)
4637 goto done;
4639 if (repo_path == NULL) {
4640 cwd = getcwd(NULL, 0);
4641 if (cwd == NULL)
4642 return got_error_from_errno("getcwd");
4643 error = got_worktree_open(&worktree, cwd);
4644 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4645 goto done;
4646 if (worktree)
4647 repo_path =
4648 strdup(got_worktree_get_repo_path(worktree));
4649 else
4650 repo_path = strdup(cwd);
4651 if (repo_path == NULL) {
4652 error = got_error_from_errno("strdup");
4653 goto done;
4657 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4658 if (error)
4659 goto done;
4661 init_curses();
4663 error = apply_unveil(got_repo_get_path(repo), NULL);
4664 if (error)
4665 goto done;
4667 error = tog_load_refs(repo, 0);
4668 if (error)
4669 goto done;
4671 error = got_repo_match_object_id(&id1, &label1, id_str1,
4672 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4673 if (error)
4674 goto done;
4676 error = got_repo_match_object_id(&id2, &label2, id_str2,
4677 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4678 if (error)
4679 goto done;
4681 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4682 if (view == NULL) {
4683 error = got_error_from_errno("view_open");
4684 goto done;
4686 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4687 ignore_whitespace, force_text_diff, NULL, repo);
4688 if (error)
4689 goto done;
4690 error = view_loop(view);
4691 done:
4692 free(label1);
4693 free(label2);
4694 free(repo_path);
4695 free(cwd);
4696 if (repo) {
4697 const struct got_error *close_err = got_repo_close(repo);
4698 if (error == NULL)
4699 error = close_err;
4701 if (worktree)
4702 got_worktree_close(worktree);
4703 if (pack_fds) {
4704 const struct got_error *pack_err =
4705 got_repo_pack_fds_close(pack_fds);
4706 if (error == NULL)
4707 error = pack_err;
4709 tog_free_refs();
4710 return error;
4713 __dead static void
4714 usage_blame(void)
4716 endwin();
4717 fprintf(stderr,
4718 "usage: %s blame [-c commit] [-r repository-path] path\n",
4719 getprogname());
4720 exit(1);
4723 struct tog_blame_line {
4724 int annotated;
4725 struct got_object_id *id;
4728 static const struct got_error *
4729 draw_blame(struct tog_view *view)
4731 struct tog_blame_view_state *s = &view->state.blame;
4732 struct tog_blame *blame = &s->blame;
4733 regmatch_t *regmatch = &view->regmatch;
4734 const struct got_error *err;
4735 int lineno = 0, nprinted = 0;
4736 char *line = NULL;
4737 size_t linesize = 0;
4738 ssize_t linelen;
4739 wchar_t *wline;
4740 int width;
4741 struct tog_blame_line *blame_line;
4742 struct got_object_id *prev_id = NULL;
4743 char *id_str;
4744 struct tog_color *tc;
4746 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4747 if (err)
4748 return err;
4750 rewind(blame->f);
4751 werase(view->window);
4753 if (asprintf(&line, "commit %s", id_str) == -1) {
4754 err = got_error_from_errno("asprintf");
4755 free(id_str);
4756 return err;
4759 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4760 free(line);
4761 line = NULL;
4762 if (err)
4763 return err;
4764 if (view_needs_focus_indication(view))
4765 wstandout(view->window);
4766 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4767 if (tc)
4768 wattr_on(view->window,
4769 COLOR_PAIR(tc->colorpair), NULL);
4770 waddwstr(view->window, wline);
4771 if (tc)
4772 wattr_off(view->window,
4773 COLOR_PAIR(tc->colorpair), NULL);
4774 if (view_needs_focus_indication(view))
4775 wstandend(view->window);
4776 free(wline);
4777 wline = NULL;
4778 if (width < view->ncols - 1)
4779 waddch(view->window, '\n');
4781 if (asprintf(&line, "[%d/%d] %s%s",
4782 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4783 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4784 free(id_str);
4785 return got_error_from_errno("asprintf");
4787 free(id_str);
4788 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4789 free(line);
4790 line = NULL;
4791 if (err)
4792 return err;
4793 waddwstr(view->window, wline);
4794 free(wline);
4795 wline = NULL;
4796 if (width < view->ncols - 1)
4797 waddch(view->window, '\n');
4799 s->eof = 0;
4800 view->maxx = 0;
4801 while (nprinted < view->nlines - 2) {
4802 linelen = getline(&line, &linesize, blame->f);
4803 if (linelen == -1) {
4804 if (feof(blame->f)) {
4805 s->eof = 1;
4806 break;
4808 free(line);
4809 return got_ferror(blame->f, GOT_ERR_IO);
4811 if (++lineno < s->first_displayed_line)
4812 continue;
4814 /* Set view->maxx based on full line length. */
4815 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4816 if (err) {
4817 free(line);
4818 return err;
4820 free(wline);
4821 wline = NULL;
4822 view->maxx = MAX(view->maxx, width);
4824 if (view->focussed && nprinted == s->selected_line - 1)
4825 wstandout(view->window);
4827 if (blame->nlines > 0) {
4828 blame_line = &blame->lines[lineno - 1];
4829 if (blame_line->annotated && prev_id &&
4830 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4831 !(view->focussed &&
4832 nprinted == s->selected_line - 1)) {
4833 waddstr(view->window, " ");
4834 } else if (blame_line->annotated) {
4835 char *id_str;
4836 err = got_object_id_str(&id_str,
4837 blame_line->id);
4838 if (err) {
4839 free(line);
4840 return err;
4842 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4843 if (tc)
4844 wattr_on(view->window,
4845 COLOR_PAIR(tc->colorpair), NULL);
4846 wprintw(view->window, "%.8s", id_str);
4847 if (tc)
4848 wattr_off(view->window,
4849 COLOR_PAIR(tc->colorpair), NULL);
4850 free(id_str);
4851 prev_id = blame_line->id;
4852 } else {
4853 waddstr(view->window, "........");
4854 prev_id = NULL;
4856 } else {
4857 waddstr(view->window, "........");
4858 prev_id = NULL;
4861 if (view->focussed && nprinted == s->selected_line - 1)
4862 wstandend(view->window);
4863 waddstr(view->window, " ");
4865 if (view->ncols <= 9) {
4866 width = 9;
4867 } else if (s->first_displayed_line + nprinted ==
4868 s->matched_line &&
4869 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4870 err = add_matched_line(&width, line, view->ncols - 9, 9,
4871 view->window, view->x, regmatch);
4872 if (err) {
4873 free(line);
4874 return err;
4876 width += 9;
4877 } else {
4878 int skip;
4879 err = format_line(&wline, &width, &skip, line,
4880 view->x, view->ncols - 9, 9, 1);
4881 if (err) {
4882 free(line);
4883 return err;
4885 waddwstr(view->window, &wline[skip]);
4886 width += 9;
4887 free(wline);
4888 wline = NULL;
4891 if (width <= view->ncols - 1)
4892 waddch(view->window, '\n');
4893 if (++nprinted == 1)
4894 s->first_displayed_line = lineno;
4896 free(line);
4897 s->last_displayed_line = lineno;
4899 view_border(view);
4901 return NULL;
4904 static const struct got_error *
4905 blame_cb(void *arg, int nlines, int lineno,
4906 struct got_commit_object *commit, struct got_object_id *id)
4908 const struct got_error *err = NULL;
4909 struct tog_blame_cb_args *a = arg;
4910 struct tog_blame_line *line;
4911 int errcode;
4913 if (nlines != a->nlines ||
4914 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4915 return got_error(GOT_ERR_RANGE);
4917 errcode = pthread_mutex_lock(&tog_mutex);
4918 if (errcode)
4919 return got_error_set_errno(errcode, "pthread_mutex_lock");
4921 if (*a->quit) { /* user has quit the blame view */
4922 err = got_error(GOT_ERR_ITER_COMPLETED);
4923 goto done;
4926 if (lineno == -1)
4927 goto done; /* no change in this commit */
4929 line = &a->lines[lineno - 1];
4930 if (line->annotated)
4931 goto done;
4933 line->id = got_object_id_dup(id);
4934 if (line->id == NULL) {
4935 err = got_error_from_errno("got_object_id_dup");
4936 goto done;
4938 line->annotated = 1;
4939 done:
4940 errcode = pthread_mutex_unlock(&tog_mutex);
4941 if (errcode)
4942 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4943 return err;
4946 static void *
4947 blame_thread(void *arg)
4949 const struct got_error *err, *close_err;
4950 struct tog_blame_thread_args *ta = arg;
4951 struct tog_blame_cb_args *a = ta->cb_args;
4952 int errcode, fd1 = -1, fd2 = -1;
4953 FILE *f1 = NULL, *f2 = NULL;
4955 fd1 = got_opentempfd();
4956 if (fd1 == -1)
4957 return (void *)got_error_from_errno("got_opentempfd");
4959 fd2 = got_opentempfd();
4960 if (fd2 == -1) {
4961 err = got_error_from_errno("got_opentempfd");
4962 goto done;
4965 f1 = got_opentemp();
4966 if (f1 == NULL) {
4967 err = (void *)got_error_from_errno("got_opentemp");
4968 goto done;
4970 f2 = got_opentemp();
4971 if (f2 == NULL) {
4972 err = (void *)got_error_from_errno("got_opentemp");
4973 goto done;
4976 err = block_signals_used_by_main_thread();
4977 if (err)
4978 goto done;
4980 err = got_blame(ta->path, a->commit_id, ta->repo,
4981 tog_diff_algo, blame_cb, ta->cb_args,
4982 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
4983 if (err && err->code == GOT_ERR_CANCELLED)
4984 err = NULL;
4986 errcode = pthread_mutex_lock(&tog_mutex);
4987 if (errcode) {
4988 err = got_error_set_errno(errcode, "pthread_mutex_lock");
4989 goto done;
4992 close_err = got_repo_close(ta->repo);
4993 if (err == NULL)
4994 err = close_err;
4995 ta->repo = NULL;
4996 *ta->complete = 1;
4998 errcode = pthread_mutex_unlock(&tog_mutex);
4999 if (errcode && err == NULL)
5000 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5002 done:
5003 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5004 err = got_error_from_errno("close");
5005 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5006 err = got_error_from_errno("close");
5007 if (f1 && fclose(f1) == EOF && err == NULL)
5008 err = got_error_from_errno("fclose");
5009 if (f2 && fclose(f2) == EOF && err == NULL)
5010 err = got_error_from_errno("fclose");
5012 return (void *)err;
5015 static struct got_object_id *
5016 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5017 int first_displayed_line, int selected_line)
5019 struct tog_blame_line *line;
5021 if (nlines <= 0)
5022 return NULL;
5024 line = &lines[first_displayed_line - 1 + selected_line - 1];
5025 if (!line->annotated)
5026 return NULL;
5028 return line->id;
5031 static const struct got_error *
5032 stop_blame(struct tog_blame *blame)
5034 const struct got_error *err = NULL;
5035 int i;
5037 if (blame->thread) {
5038 int errcode;
5039 errcode = pthread_mutex_unlock(&tog_mutex);
5040 if (errcode)
5041 return got_error_set_errno(errcode,
5042 "pthread_mutex_unlock");
5043 errcode = pthread_join(blame->thread, (void **)&err);
5044 if (errcode)
5045 return got_error_set_errno(errcode, "pthread_join");
5046 errcode = pthread_mutex_lock(&tog_mutex);
5047 if (errcode)
5048 return got_error_set_errno(errcode,
5049 "pthread_mutex_lock");
5050 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5051 err = NULL;
5052 blame->thread = NULL;
5054 if (blame->thread_args.repo) {
5055 const struct got_error *close_err;
5056 close_err = got_repo_close(blame->thread_args.repo);
5057 if (err == NULL)
5058 err = close_err;
5059 blame->thread_args.repo = NULL;
5061 if (blame->f) {
5062 if (fclose(blame->f) == EOF && err == NULL)
5063 err = got_error_from_errno("fclose");
5064 blame->f = NULL;
5066 if (blame->lines) {
5067 for (i = 0; i < blame->nlines; i++)
5068 free(blame->lines[i].id);
5069 free(blame->lines);
5070 blame->lines = NULL;
5072 free(blame->cb_args.commit_id);
5073 blame->cb_args.commit_id = NULL;
5074 if (blame->pack_fds) {
5075 const struct got_error *pack_err =
5076 got_repo_pack_fds_close(blame->pack_fds);
5077 if (err == NULL)
5078 err = pack_err;
5079 blame->pack_fds = NULL;
5081 return err;
5084 static const struct got_error *
5085 cancel_blame_view(void *arg)
5087 const struct got_error *err = NULL;
5088 int *done = arg;
5089 int errcode;
5091 errcode = pthread_mutex_lock(&tog_mutex);
5092 if (errcode)
5093 return got_error_set_errno(errcode,
5094 "pthread_mutex_unlock");
5096 if (*done)
5097 err = got_error(GOT_ERR_CANCELLED);
5099 errcode = pthread_mutex_unlock(&tog_mutex);
5100 if (errcode)
5101 return got_error_set_errno(errcode,
5102 "pthread_mutex_lock");
5104 return err;
5107 static const struct got_error *
5108 run_blame(struct tog_view *view)
5110 struct tog_blame_view_state *s = &view->state.blame;
5111 struct tog_blame *blame = &s->blame;
5112 const struct got_error *err = NULL;
5113 struct got_commit_object *commit = NULL;
5114 struct got_blob_object *blob = NULL;
5115 struct got_repository *thread_repo = NULL;
5116 struct got_object_id *obj_id = NULL;
5117 int obj_type, fd = -1;
5118 int *pack_fds = NULL;
5120 err = got_object_open_as_commit(&commit, s->repo,
5121 &s->blamed_commit->id);
5122 if (err)
5123 return err;
5125 fd = got_opentempfd();
5126 if (fd == -1) {
5127 err = got_error_from_errno("got_opentempfd");
5128 goto done;
5131 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5132 if (err)
5133 goto done;
5135 err = got_object_get_type(&obj_type, s->repo, obj_id);
5136 if (err)
5137 goto done;
5139 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5140 err = got_error(GOT_ERR_OBJ_TYPE);
5141 goto done;
5144 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5145 if (err)
5146 goto done;
5147 blame->f = got_opentemp();
5148 if (blame->f == NULL) {
5149 err = got_error_from_errno("got_opentemp");
5150 goto done;
5152 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5153 &blame->line_offsets, blame->f, blob);
5154 if (err)
5155 goto done;
5156 if (blame->nlines == 0) {
5157 s->blame_complete = 1;
5158 goto done;
5161 /* Don't include \n at EOF in the blame line count. */
5162 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5163 blame->nlines--;
5165 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5166 if (blame->lines == NULL) {
5167 err = got_error_from_errno("calloc");
5168 goto done;
5171 err = got_repo_pack_fds_open(&pack_fds);
5172 if (err)
5173 goto done;
5174 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5175 pack_fds);
5176 if (err)
5177 goto done;
5179 blame->pack_fds = pack_fds;
5180 blame->cb_args.view = view;
5181 blame->cb_args.lines = blame->lines;
5182 blame->cb_args.nlines = blame->nlines;
5183 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5184 if (blame->cb_args.commit_id == NULL) {
5185 err = got_error_from_errno("got_object_id_dup");
5186 goto done;
5188 blame->cb_args.quit = &s->done;
5190 blame->thread_args.path = s->path;
5191 blame->thread_args.repo = thread_repo;
5192 blame->thread_args.cb_args = &blame->cb_args;
5193 blame->thread_args.complete = &s->blame_complete;
5194 blame->thread_args.cancel_cb = cancel_blame_view;
5195 blame->thread_args.cancel_arg = &s->done;
5196 s->blame_complete = 0;
5198 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5199 s->first_displayed_line = 1;
5200 s->last_displayed_line = view->nlines;
5201 s->selected_line = 1;
5203 s->matched_line = 0;
5205 done:
5206 if (commit)
5207 got_object_commit_close(commit);
5208 if (fd != -1 && close(fd) == -1 && err == NULL)
5209 err = got_error_from_errno("close");
5210 if (blob)
5211 got_object_blob_close(blob);
5212 free(obj_id);
5213 if (err)
5214 stop_blame(blame);
5215 return err;
5218 static const struct got_error *
5219 open_blame_view(struct tog_view *view, char *path,
5220 struct got_object_id *commit_id, struct got_repository *repo)
5222 const struct got_error *err = NULL;
5223 struct tog_blame_view_state *s = &view->state.blame;
5225 STAILQ_INIT(&s->blamed_commits);
5227 s->path = strdup(path);
5228 if (s->path == NULL)
5229 return got_error_from_errno("strdup");
5231 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5232 if (err) {
5233 free(s->path);
5234 return err;
5237 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5238 s->first_displayed_line = 1;
5239 s->last_displayed_line = view->nlines;
5240 s->selected_line = 1;
5241 s->blame_complete = 0;
5242 s->repo = repo;
5243 s->commit_id = commit_id;
5244 memset(&s->blame, 0, sizeof(s->blame));
5246 STAILQ_INIT(&s->colors);
5247 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5248 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5249 get_color_value("TOG_COLOR_COMMIT"));
5250 if (err)
5251 return err;
5254 view->show = show_blame_view;
5255 view->input = input_blame_view;
5256 view->reset = reset_blame_view;
5257 view->close = close_blame_view;
5258 view->search_start = search_start_blame_view;
5259 view->search_next = search_next_blame_view;
5261 return run_blame(view);
5264 static const struct got_error *
5265 close_blame_view(struct tog_view *view)
5267 const struct got_error *err = NULL;
5268 struct tog_blame_view_state *s = &view->state.blame;
5270 if (s->blame.thread)
5271 err = stop_blame(&s->blame);
5273 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5274 struct got_object_qid *blamed_commit;
5275 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5276 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5277 got_object_qid_free(blamed_commit);
5280 free(s->path);
5281 free_colors(&s->colors);
5282 return err;
5285 static const struct got_error *
5286 search_start_blame_view(struct tog_view *view)
5288 struct tog_blame_view_state *s = &view->state.blame;
5290 s->matched_line = 0;
5291 return NULL;
5294 static const struct got_error *
5295 search_next_blame_view(struct tog_view *view)
5297 struct tog_blame_view_state *s = &view->state.blame;
5298 const struct got_error *err = NULL;
5299 int lineno;
5300 char *line = NULL;
5301 size_t linesize = 0;
5302 ssize_t linelen;
5304 if (!view->searching) {
5305 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5306 return NULL;
5309 if (s->matched_line) {
5310 if (view->searching == TOG_SEARCH_FORWARD)
5311 lineno = s->matched_line + 1;
5312 else
5313 lineno = s->matched_line - 1;
5314 } else
5315 lineno = s->first_displayed_line - 1 + s->selected_line;
5317 while (1) {
5318 off_t offset;
5320 if (lineno <= 0 || lineno > s->blame.nlines) {
5321 if (s->matched_line == 0) {
5322 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5323 break;
5326 if (view->searching == TOG_SEARCH_FORWARD)
5327 lineno = 1;
5328 else
5329 lineno = s->blame.nlines;
5332 offset = s->blame.line_offsets[lineno - 1];
5333 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5334 free(line);
5335 return got_error_from_errno("fseeko");
5337 linelen = getline(&line, &linesize, s->blame.f);
5338 if (linelen != -1) {
5339 char *exstr;
5340 err = expand_tab(&exstr, line);
5341 if (err)
5342 break;
5343 if (match_line(exstr, &view->regex, 1,
5344 &view->regmatch)) {
5345 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5346 s->matched_line = lineno;
5347 free(exstr);
5348 break;
5350 free(exstr);
5352 if (view->searching == TOG_SEARCH_FORWARD)
5353 lineno++;
5354 else
5355 lineno--;
5357 free(line);
5359 if (s->matched_line) {
5360 s->first_displayed_line = s->matched_line;
5361 s->selected_line = 1;
5364 return err;
5367 static const struct got_error *
5368 show_blame_view(struct tog_view *view)
5370 const struct got_error *err = NULL;
5371 struct tog_blame_view_state *s = &view->state.blame;
5372 int errcode;
5374 if (s->blame.thread == NULL && !s->blame_complete) {
5375 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5376 &s->blame.thread_args);
5377 if (errcode)
5378 return got_error_set_errno(errcode, "pthread_create");
5380 halfdelay(1); /* fast refresh while annotating */
5383 if (s->blame_complete)
5384 halfdelay(10); /* disable fast refresh */
5386 err = draw_blame(view);
5388 view_border(view);
5389 return err;
5392 static const struct got_error *
5393 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5395 const struct got_error *err = NULL, *thread_err = NULL;
5396 struct tog_view *diff_view;
5397 struct tog_blame_view_state *s = &view->state.blame;
5398 int eos, nscroll, begin_y = 0, begin_x = 0;
5400 eos = nscroll = view->nlines - 2;
5401 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5402 view_is_splitscreen(view->child))
5403 --eos; /* border */
5405 switch (ch) {
5406 case '0':
5407 view->x = 0;
5408 break;
5409 case '$':
5410 view->x = MAX(view->maxx - view->ncols / 3, 0);
5411 view->count = 0;
5412 break;
5413 case KEY_RIGHT:
5414 case 'l':
5415 if (view->x + view->ncols / 3 < view->maxx)
5416 view->x += 2; /* move two columns right */
5417 else
5418 view->count = 0;
5419 break;
5420 case KEY_LEFT:
5421 case 'h':
5422 view->x -= MIN(view->x, 2); /* move two columns back */
5423 if (view->x <= 0)
5424 view->count = 0;
5425 break;
5426 case 'q':
5427 s->done = 1;
5428 break;
5429 case 'g':
5430 case KEY_HOME:
5431 s->selected_line = 1;
5432 s->first_displayed_line = 1;
5433 view->count = 0;
5434 break;
5435 case 'G':
5436 case KEY_END:
5437 if (s->blame.nlines < eos) {
5438 s->selected_line = s->blame.nlines;
5439 s->first_displayed_line = 1;
5440 } else {
5441 s->selected_line = eos;
5442 s->first_displayed_line = s->blame.nlines - (eos - 1);
5444 view->count = 0;
5445 break;
5446 case 'k':
5447 case KEY_UP:
5448 case CTRL('p'):
5449 if (s->selected_line > 1)
5450 s->selected_line--;
5451 else if (s->selected_line == 1 &&
5452 s->first_displayed_line > 1)
5453 s->first_displayed_line--;
5454 else
5455 view->count = 0;
5456 break;
5457 case CTRL('u'):
5458 case 'u':
5459 nscroll /= 2;
5460 /* FALL THROUGH */
5461 case KEY_PPAGE:
5462 case CTRL('b'):
5463 case 'b':
5464 if (s->first_displayed_line == 1) {
5465 if (view->count > 1)
5466 nscroll += nscroll;
5467 s->selected_line = MAX(1, s->selected_line - nscroll);
5468 view->count = 0;
5469 break;
5471 if (s->first_displayed_line > nscroll)
5472 s->first_displayed_line -= nscroll;
5473 else
5474 s->first_displayed_line = 1;
5475 break;
5476 case 'j':
5477 case KEY_DOWN:
5478 case CTRL('n'):
5479 if (s->selected_line < eos && s->first_displayed_line +
5480 s->selected_line <= s->blame.nlines)
5481 s->selected_line++;
5482 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5483 s->first_displayed_line++;
5484 else
5485 view->count = 0;
5486 break;
5487 case 'c':
5488 case 'p': {
5489 struct got_object_id *id = NULL;
5491 view->count = 0;
5492 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5493 s->first_displayed_line, s->selected_line);
5494 if (id == NULL)
5495 break;
5496 if (ch == 'p') {
5497 struct got_commit_object *commit, *pcommit;
5498 struct got_object_qid *pid;
5499 struct got_object_id *blob_id = NULL;
5500 int obj_type;
5501 err = got_object_open_as_commit(&commit,
5502 s->repo, id);
5503 if (err)
5504 break;
5505 pid = STAILQ_FIRST(
5506 got_object_commit_get_parent_ids(commit));
5507 if (pid == NULL) {
5508 got_object_commit_close(commit);
5509 break;
5511 /* Check if path history ends here. */
5512 err = got_object_open_as_commit(&pcommit,
5513 s->repo, &pid->id);
5514 if (err)
5515 break;
5516 err = got_object_id_by_path(&blob_id, s->repo,
5517 pcommit, s->path);
5518 got_object_commit_close(pcommit);
5519 if (err) {
5520 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5521 err = NULL;
5522 got_object_commit_close(commit);
5523 break;
5525 err = got_object_get_type(&obj_type, s->repo,
5526 blob_id);
5527 free(blob_id);
5528 /* Can't blame non-blob type objects. */
5529 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5530 got_object_commit_close(commit);
5531 break;
5533 err = got_object_qid_alloc(&s->blamed_commit,
5534 &pid->id);
5535 got_object_commit_close(commit);
5536 } else {
5537 if (got_object_id_cmp(id,
5538 &s->blamed_commit->id) == 0)
5539 break;
5540 err = got_object_qid_alloc(&s->blamed_commit,
5541 id);
5543 if (err)
5544 break;
5545 s->done = 1;
5546 thread_err = stop_blame(&s->blame);
5547 s->done = 0;
5548 if (thread_err)
5549 break;
5550 STAILQ_INSERT_HEAD(&s->blamed_commits,
5551 s->blamed_commit, entry);
5552 err = run_blame(view);
5553 if (err)
5554 break;
5555 break;
5557 case 'C': {
5558 struct got_object_qid *first;
5560 view->count = 0;
5561 first = STAILQ_FIRST(&s->blamed_commits);
5562 if (!got_object_id_cmp(&first->id, s->commit_id))
5563 break;
5564 s->done = 1;
5565 thread_err = stop_blame(&s->blame);
5566 s->done = 0;
5567 if (thread_err)
5568 break;
5569 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5570 got_object_qid_free(s->blamed_commit);
5571 s->blamed_commit =
5572 STAILQ_FIRST(&s->blamed_commits);
5573 err = run_blame(view);
5574 if (err)
5575 break;
5576 break;
5578 case KEY_ENTER:
5579 case '\r': {
5580 struct got_object_id *id = NULL;
5581 struct got_object_qid *pid;
5582 struct got_commit_object *commit = NULL;
5584 view->count = 0;
5585 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5586 s->first_displayed_line, s->selected_line);
5587 if (id == NULL)
5588 break;
5589 err = got_object_open_as_commit(&commit, s->repo, id);
5590 if (err)
5591 break;
5592 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5593 if (view_is_parent_view(view))
5594 view_get_split(view, &begin_y, &begin_x);
5596 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5597 if (diff_view == NULL) {
5598 got_object_commit_close(commit);
5599 err = got_error_from_errno("view_open");
5600 break;
5602 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5603 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5604 got_object_commit_close(commit);
5605 if (err) {
5606 view_close(diff_view);
5607 break;
5609 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
5610 err = view_init_hsplit(view, begin_y);
5611 if (err)
5612 break;
5615 view->focussed = 0;
5616 diff_view->focussed = 1;
5617 diff_view->mode = view->mode;
5618 diff_view->nlines = view->lines - begin_y;
5619 if (view_is_parent_view(view)) {
5620 err = view_close_child(view);
5621 if (err)
5622 break;
5623 err = view_set_child(view, diff_view);
5624 if (err)
5625 break;
5626 view->focus_child = 1;
5627 } else
5628 *new_view = diff_view;
5629 if (err)
5630 break;
5631 break;
5633 case CTRL('d'):
5634 case 'd':
5635 nscroll /= 2;
5636 /* FALL THROUGH */
5637 case KEY_NPAGE:
5638 case CTRL('f'):
5639 case 'f':
5640 case ' ':
5641 if (s->last_displayed_line >= s->blame.nlines &&
5642 s->selected_line >= MIN(s->blame.nlines,
5643 view->nlines - 2)) {
5644 view->count = 0;
5645 break;
5647 if (s->last_displayed_line >= s->blame.nlines &&
5648 s->selected_line < view->nlines - 2) {
5649 s->selected_line +=
5650 MIN(nscroll, s->last_displayed_line -
5651 s->first_displayed_line - s->selected_line + 1);
5653 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5654 s->first_displayed_line += nscroll;
5655 else
5656 s->first_displayed_line =
5657 s->blame.nlines - (view->nlines - 3);
5658 break;
5659 case KEY_RESIZE:
5660 if (s->selected_line > view->nlines - 2) {
5661 s->selected_line = MIN(s->blame.nlines,
5662 view->nlines - 2);
5664 break;
5665 default:
5666 view->count = 0;
5667 break;
5669 return thread_err ? thread_err : err;
5672 static const struct got_error *
5673 reset_blame_view(struct tog_view *view)
5675 const struct got_error *err;
5676 struct tog_blame_view_state *s = &view->state.blame;
5678 view->count = 0;
5679 s->done = 1;
5680 err = stop_blame(&s->blame);
5681 s->done = 0;
5682 if (err)
5683 return err;
5684 return run_blame(view);
5687 static const struct got_error *
5688 cmd_blame(int argc, char *argv[])
5690 const struct got_error *error;
5691 struct got_repository *repo = NULL;
5692 struct got_worktree *worktree = NULL;
5693 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5694 char *link_target = NULL;
5695 struct got_object_id *commit_id = NULL;
5696 struct got_commit_object *commit = NULL;
5697 char *commit_id_str = NULL;
5698 int ch;
5699 struct tog_view *view;
5700 int *pack_fds = NULL;
5702 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5703 switch (ch) {
5704 case 'c':
5705 commit_id_str = optarg;
5706 break;
5707 case 'r':
5708 repo_path = realpath(optarg, NULL);
5709 if (repo_path == NULL)
5710 return got_error_from_errno2("realpath",
5711 optarg);
5712 break;
5713 default:
5714 usage_blame();
5715 /* NOTREACHED */
5719 argc -= optind;
5720 argv += optind;
5722 if (argc != 1)
5723 usage_blame();
5725 error = got_repo_pack_fds_open(&pack_fds);
5726 if (error != NULL)
5727 goto done;
5729 if (repo_path == NULL) {
5730 cwd = getcwd(NULL, 0);
5731 if (cwd == NULL)
5732 return got_error_from_errno("getcwd");
5733 error = got_worktree_open(&worktree, cwd);
5734 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5735 goto done;
5736 if (worktree)
5737 repo_path =
5738 strdup(got_worktree_get_repo_path(worktree));
5739 else
5740 repo_path = strdup(cwd);
5741 if (repo_path == NULL) {
5742 error = got_error_from_errno("strdup");
5743 goto done;
5747 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5748 if (error != NULL)
5749 goto done;
5751 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5752 worktree);
5753 if (error)
5754 goto done;
5756 init_curses();
5758 error = apply_unveil(got_repo_get_path(repo), NULL);
5759 if (error)
5760 goto done;
5762 error = tog_load_refs(repo, 0);
5763 if (error)
5764 goto done;
5766 if (commit_id_str == NULL) {
5767 struct got_reference *head_ref;
5768 error = got_ref_open(&head_ref, repo, worktree ?
5769 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5770 if (error != NULL)
5771 goto done;
5772 error = got_ref_resolve(&commit_id, repo, head_ref);
5773 got_ref_close(head_ref);
5774 } else {
5775 error = got_repo_match_object_id(&commit_id, NULL,
5776 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5778 if (error != NULL)
5779 goto done;
5781 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5782 if (view == NULL) {
5783 error = got_error_from_errno("view_open");
5784 goto done;
5787 error = got_object_open_as_commit(&commit, repo, commit_id);
5788 if (error)
5789 goto done;
5791 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5792 commit, repo);
5793 if (error)
5794 goto done;
5796 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5797 commit_id, repo);
5798 if (error)
5799 goto done;
5800 if (worktree) {
5801 /* Release work tree lock. */
5802 got_worktree_close(worktree);
5803 worktree = NULL;
5805 error = view_loop(view);
5806 done:
5807 free(repo_path);
5808 free(in_repo_path);
5809 free(link_target);
5810 free(cwd);
5811 free(commit_id);
5812 if (commit)
5813 got_object_commit_close(commit);
5814 if (worktree)
5815 got_worktree_close(worktree);
5816 if (repo) {
5817 const struct got_error *close_err = got_repo_close(repo);
5818 if (error == NULL)
5819 error = close_err;
5821 if (pack_fds) {
5822 const struct got_error *pack_err =
5823 got_repo_pack_fds_close(pack_fds);
5824 if (error == NULL)
5825 error = pack_err;
5827 tog_free_refs();
5828 return error;
5831 static const struct got_error *
5832 draw_tree_entries(struct tog_view *view, const char *parent_path)
5834 struct tog_tree_view_state *s = &view->state.tree;
5835 const struct got_error *err = NULL;
5836 struct got_tree_entry *te;
5837 wchar_t *wline;
5838 struct tog_color *tc;
5839 int width, n, i, nentries;
5840 int limit = view->nlines;
5842 s->ndisplayed = 0;
5843 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5844 view_is_splitscreen(view->child))
5845 --limit; /* border */
5847 werase(view->window);
5849 if (limit == 0)
5850 return NULL;
5852 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5853 0, 0);
5854 if (err)
5855 return err;
5856 if (view_needs_focus_indication(view))
5857 wstandout(view->window);
5858 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5859 if (tc)
5860 wattr_on(view->window,
5861 COLOR_PAIR(tc->colorpair), NULL);
5862 waddwstr(view->window, wline);
5863 if (tc)
5864 wattr_off(view->window,
5865 COLOR_PAIR(tc->colorpair), NULL);
5866 if (view_needs_focus_indication(view))
5867 wstandend(view->window);
5868 free(wline);
5869 wline = NULL;
5870 if (width < view->ncols - 1)
5871 waddch(view->window, '\n');
5872 if (--limit <= 0)
5873 return NULL;
5874 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5875 0, 0);
5876 if (err)
5877 return err;
5878 waddwstr(view->window, wline);
5879 free(wline);
5880 wline = NULL;
5881 if (width < view->ncols - 1)
5882 waddch(view->window, '\n');
5883 if (--limit <= 0)
5884 return NULL;
5885 waddch(view->window, '\n');
5886 if (--limit <= 0)
5887 return NULL;
5889 if (s->first_displayed_entry == NULL) {
5890 te = got_object_tree_get_first_entry(s->tree);
5891 if (s->selected == 0) {
5892 if (view->focussed)
5893 wstandout(view->window);
5894 s->selected_entry = NULL;
5896 waddstr(view->window, " ..\n"); /* parent directory */
5897 if (s->selected == 0 && view->focussed)
5898 wstandend(view->window);
5899 s->ndisplayed++;
5900 if (--limit <= 0)
5901 return NULL;
5902 n = 1;
5903 } else {
5904 n = 0;
5905 te = s->first_displayed_entry;
5908 nentries = got_object_tree_get_nentries(s->tree);
5909 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5910 char *line = NULL, *id_str = NULL, *link_target = NULL;
5911 const char *modestr = "";
5912 mode_t mode;
5914 te = got_object_tree_get_entry(s->tree, i);
5915 mode = got_tree_entry_get_mode(te);
5917 if (s->show_ids) {
5918 err = got_object_id_str(&id_str,
5919 got_tree_entry_get_id(te));
5920 if (err)
5921 return got_error_from_errno(
5922 "got_object_id_str");
5924 if (got_object_tree_entry_is_submodule(te))
5925 modestr = "$";
5926 else if (S_ISLNK(mode)) {
5927 int i;
5929 err = got_tree_entry_get_symlink_target(&link_target,
5930 te, s->repo);
5931 if (err) {
5932 free(id_str);
5933 return err;
5935 for (i = 0; i < strlen(link_target); i++) {
5936 if (!isprint((unsigned char)link_target[i]))
5937 link_target[i] = '?';
5939 modestr = "@";
5941 else if (S_ISDIR(mode))
5942 modestr = "/";
5943 else if (mode & S_IXUSR)
5944 modestr = "*";
5945 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5946 got_tree_entry_get_name(te), modestr,
5947 link_target ? " -> ": "",
5948 link_target ? link_target : "") == -1) {
5949 free(id_str);
5950 free(link_target);
5951 return got_error_from_errno("asprintf");
5953 free(id_str);
5954 free(link_target);
5955 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5956 0, 0);
5957 if (err) {
5958 free(line);
5959 break;
5961 if (n == s->selected) {
5962 if (view->focussed)
5963 wstandout(view->window);
5964 s->selected_entry = te;
5966 tc = match_color(&s->colors, line);
5967 if (tc)
5968 wattr_on(view->window,
5969 COLOR_PAIR(tc->colorpair), NULL);
5970 waddwstr(view->window, wline);
5971 if (tc)
5972 wattr_off(view->window,
5973 COLOR_PAIR(tc->colorpair), NULL);
5974 if (width < view->ncols - 1)
5975 waddch(view->window, '\n');
5976 if (n == s->selected && view->focussed)
5977 wstandend(view->window);
5978 free(line);
5979 free(wline);
5980 wline = NULL;
5981 n++;
5982 s->ndisplayed++;
5983 s->last_displayed_entry = te;
5984 if (--limit <= 0)
5985 break;
5988 return err;
5991 static void
5992 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5994 struct got_tree_entry *te;
5995 int isroot = s->tree == s->root;
5996 int i = 0;
5998 if (s->first_displayed_entry == NULL)
5999 return;
6001 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6002 while (i++ < maxscroll) {
6003 if (te == NULL) {
6004 if (!isroot)
6005 s->first_displayed_entry = NULL;
6006 break;
6008 s->first_displayed_entry = te;
6009 te = got_tree_entry_get_prev(s->tree, te);
6013 static const struct got_error *
6014 tree_scroll_down(struct tog_view *view, int maxscroll)
6016 struct tog_tree_view_state *s = &view->state.tree;
6017 struct got_tree_entry *next, *last;
6018 int n = 0;
6020 if (s->first_displayed_entry)
6021 next = got_tree_entry_get_next(s->tree,
6022 s->first_displayed_entry);
6023 else
6024 next = got_object_tree_get_first_entry(s->tree);
6026 last = s->last_displayed_entry;
6027 while (next && n++ < maxscroll) {
6028 if (last)
6029 last = got_tree_entry_get_next(s->tree, last);
6030 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6031 s->first_displayed_entry = next;
6032 next = got_tree_entry_get_next(s->tree, next);
6036 return NULL;
6039 static const struct got_error *
6040 tree_entry_path(char **path, struct tog_parent_trees *parents,
6041 struct got_tree_entry *te)
6043 const struct got_error *err = NULL;
6044 struct tog_parent_tree *pt;
6045 size_t len = 2; /* for leading slash and NUL */
6047 TAILQ_FOREACH(pt, parents, entry)
6048 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6049 + 1 /* slash */;
6050 if (te)
6051 len += strlen(got_tree_entry_get_name(te));
6053 *path = calloc(1, len);
6054 if (path == NULL)
6055 return got_error_from_errno("calloc");
6057 (*path)[0] = '/';
6058 pt = TAILQ_LAST(parents, tog_parent_trees);
6059 while (pt) {
6060 const char *name = got_tree_entry_get_name(pt->selected_entry);
6061 if (strlcat(*path, name, len) >= len) {
6062 err = got_error(GOT_ERR_NO_SPACE);
6063 goto done;
6065 if (strlcat(*path, "/", len) >= len) {
6066 err = got_error(GOT_ERR_NO_SPACE);
6067 goto done;
6069 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6071 if (te) {
6072 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6073 err = got_error(GOT_ERR_NO_SPACE);
6074 goto done;
6077 done:
6078 if (err) {
6079 free(*path);
6080 *path = NULL;
6082 return err;
6085 static const struct got_error *
6086 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6087 struct got_tree_entry *te, struct tog_parent_trees *parents,
6088 struct got_object_id *commit_id, struct got_repository *repo)
6090 const struct got_error *err = NULL;
6091 char *path;
6092 struct tog_view *blame_view;
6094 *new_view = NULL;
6096 err = tree_entry_path(&path, parents, te);
6097 if (err)
6098 return err;
6100 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6101 if (blame_view == NULL) {
6102 err = got_error_from_errno("view_open");
6103 goto done;
6106 err = open_blame_view(blame_view, path, commit_id, repo);
6107 if (err) {
6108 if (err->code == GOT_ERR_CANCELLED)
6109 err = NULL;
6110 view_close(blame_view);
6111 } else
6112 *new_view = blame_view;
6113 done:
6114 free(path);
6115 return err;
6118 static const struct got_error *
6119 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
6120 struct tog_tree_view_state *s)
6122 struct tog_view *log_view;
6123 const struct got_error *err = NULL;
6124 char *path;
6126 *new_view = NULL;
6128 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6129 if (log_view == NULL)
6130 return got_error_from_errno("view_open");
6132 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6133 if (err)
6134 return err;
6136 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6137 path, 0);
6138 if (err)
6139 view_close(log_view);
6140 else
6141 *new_view = log_view;
6142 free(path);
6143 return err;
6146 static const struct got_error *
6147 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6148 const char *head_ref_name, struct got_repository *repo)
6150 const struct got_error *err = NULL;
6151 char *commit_id_str = NULL;
6152 struct tog_tree_view_state *s = &view->state.tree;
6153 struct got_commit_object *commit = NULL;
6155 TAILQ_INIT(&s->parents);
6156 STAILQ_INIT(&s->colors);
6158 s->commit_id = got_object_id_dup(commit_id);
6159 if (s->commit_id == NULL)
6160 return got_error_from_errno("got_object_id_dup");
6162 err = got_object_open_as_commit(&commit, repo, commit_id);
6163 if (err)
6164 goto done;
6167 * The root is opened here and will be closed when the view is closed.
6168 * Any visited subtrees and their path-wise parents are opened and
6169 * closed on demand.
6171 err = got_object_open_as_tree(&s->root, repo,
6172 got_object_commit_get_tree_id(commit));
6173 if (err)
6174 goto done;
6175 s->tree = s->root;
6177 err = got_object_id_str(&commit_id_str, commit_id);
6178 if (err != NULL)
6179 goto done;
6181 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6182 err = got_error_from_errno("asprintf");
6183 goto done;
6186 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6187 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6188 if (head_ref_name) {
6189 s->head_ref_name = strdup(head_ref_name);
6190 if (s->head_ref_name == NULL) {
6191 err = got_error_from_errno("strdup");
6192 goto done;
6195 s->repo = repo;
6197 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6198 err = add_color(&s->colors, "\\$$",
6199 TOG_COLOR_TREE_SUBMODULE,
6200 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6201 if (err)
6202 goto done;
6203 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6204 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6205 if (err)
6206 goto done;
6207 err = add_color(&s->colors, "/$",
6208 TOG_COLOR_TREE_DIRECTORY,
6209 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6210 if (err)
6211 goto done;
6213 err = add_color(&s->colors, "\\*$",
6214 TOG_COLOR_TREE_EXECUTABLE,
6215 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6216 if (err)
6217 goto done;
6219 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6220 get_color_value("TOG_COLOR_COMMIT"));
6221 if (err)
6222 goto done;
6225 view->show = show_tree_view;
6226 view->input = input_tree_view;
6227 view->close = close_tree_view;
6228 view->search_start = search_start_tree_view;
6229 view->search_next = search_next_tree_view;
6230 done:
6231 free(commit_id_str);
6232 if (commit)
6233 got_object_commit_close(commit);
6234 if (err)
6235 close_tree_view(view);
6236 return err;
6239 static const struct got_error *
6240 close_tree_view(struct tog_view *view)
6242 struct tog_tree_view_state *s = &view->state.tree;
6244 free_colors(&s->colors);
6245 free(s->tree_label);
6246 s->tree_label = NULL;
6247 free(s->commit_id);
6248 s->commit_id = NULL;
6249 free(s->head_ref_name);
6250 s->head_ref_name = NULL;
6251 while (!TAILQ_EMPTY(&s->parents)) {
6252 struct tog_parent_tree *parent;
6253 parent = TAILQ_FIRST(&s->parents);
6254 TAILQ_REMOVE(&s->parents, parent, entry);
6255 if (parent->tree != s->root)
6256 got_object_tree_close(parent->tree);
6257 free(parent);
6260 if (s->tree != NULL && s->tree != s->root)
6261 got_object_tree_close(s->tree);
6262 if (s->root)
6263 got_object_tree_close(s->root);
6264 return NULL;
6267 static const struct got_error *
6268 search_start_tree_view(struct tog_view *view)
6270 struct tog_tree_view_state *s = &view->state.tree;
6272 s->matched_entry = NULL;
6273 return NULL;
6276 static int
6277 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6279 regmatch_t regmatch;
6281 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6282 0) == 0;
6285 static const struct got_error *
6286 search_next_tree_view(struct tog_view *view)
6288 struct tog_tree_view_state *s = &view->state.tree;
6289 struct got_tree_entry *te = NULL;
6291 if (!view->searching) {
6292 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6293 return NULL;
6296 if (s->matched_entry) {
6297 if (view->searching == TOG_SEARCH_FORWARD) {
6298 if (s->selected_entry)
6299 te = got_tree_entry_get_next(s->tree,
6300 s->selected_entry);
6301 else
6302 te = got_object_tree_get_first_entry(s->tree);
6303 } else {
6304 if (s->selected_entry == NULL)
6305 te = got_object_tree_get_last_entry(s->tree);
6306 else
6307 te = got_tree_entry_get_prev(s->tree,
6308 s->selected_entry);
6310 } else {
6311 if (s->selected_entry)
6312 te = s->selected_entry;
6313 else if (view->searching == TOG_SEARCH_FORWARD)
6314 te = got_object_tree_get_first_entry(s->tree);
6315 else
6316 te = got_object_tree_get_last_entry(s->tree);
6319 while (1) {
6320 if (te == NULL) {
6321 if (s->matched_entry == NULL) {
6322 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6323 return NULL;
6325 if (view->searching == TOG_SEARCH_FORWARD)
6326 te = got_object_tree_get_first_entry(s->tree);
6327 else
6328 te = got_object_tree_get_last_entry(s->tree);
6331 if (match_tree_entry(te, &view->regex)) {
6332 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6333 s->matched_entry = te;
6334 break;
6337 if (view->searching == TOG_SEARCH_FORWARD)
6338 te = got_tree_entry_get_next(s->tree, te);
6339 else
6340 te = got_tree_entry_get_prev(s->tree, te);
6343 if (s->matched_entry) {
6344 s->first_displayed_entry = s->matched_entry;
6345 s->selected = 0;
6348 return NULL;
6351 static const struct got_error *
6352 show_tree_view(struct tog_view *view)
6354 const struct got_error *err = NULL;
6355 struct tog_tree_view_state *s = &view->state.tree;
6356 char *parent_path;
6358 err = tree_entry_path(&parent_path, &s->parents, NULL);
6359 if (err)
6360 return err;
6362 err = draw_tree_entries(view, parent_path);
6363 free(parent_path);
6365 view_border(view);
6366 return err;
6369 static const struct got_error *
6370 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6372 const struct got_error *err = NULL;
6373 struct tog_tree_view_state *s = &view->state.tree;
6374 struct tog_view *log_view, *ref_view;
6375 struct got_tree_entry *te;
6376 int begin_x = 0, n, nscroll = view->nlines - 3;
6378 switch (ch) {
6379 case 'i':
6380 s->show_ids = !s->show_ids;
6381 view->count = 0;
6382 break;
6383 case 'l':
6384 view->count = 0;
6385 if (!s->selected_entry)
6386 break;
6387 if (view_is_parent_view(view))
6388 begin_x = view_split_begin_x(view->begin_x);
6389 err = log_selected_tree_entry(&log_view, begin_x, s);
6390 view->focussed = 0;
6391 log_view->focussed = 1;
6392 if (view_is_parent_view(view)) {
6393 err = view_close_child(view);
6394 if (err)
6395 return err;
6396 err = view_set_child(view, log_view);
6397 if (err)
6398 return err;
6399 view->focus_child = 1;
6400 } else
6401 *new_view = log_view;
6402 break;
6403 case 'r':
6404 view->count = 0;
6405 if (view_is_parent_view(view))
6406 begin_x = view_split_begin_x(view->begin_x);
6407 ref_view = view_open(view->nlines, view->ncols,
6408 view->begin_y, begin_x, TOG_VIEW_REF);
6409 if (ref_view == NULL)
6410 return got_error_from_errno("view_open");
6411 err = open_ref_view(ref_view, s->repo);
6412 if (err) {
6413 view_close(ref_view);
6414 return err;
6416 view->focussed = 0;
6417 ref_view->focussed = 1;
6418 if (view_is_parent_view(view)) {
6419 err = view_close_child(view);
6420 if (err)
6421 return err;
6422 err = view_set_child(view, ref_view);
6423 if (err)
6424 return err;
6425 view->focus_child = 1;
6426 } else
6427 *new_view = ref_view;
6428 break;
6429 case 'g':
6430 case KEY_HOME:
6431 s->selected = 0;
6432 view->count = 0;
6433 if (s->tree == s->root)
6434 s->first_displayed_entry =
6435 got_object_tree_get_first_entry(s->tree);
6436 else
6437 s->first_displayed_entry = NULL;
6438 break;
6439 case 'G':
6440 case KEY_END: {
6441 int eos = view->nlines - 3;
6443 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6444 --eos; /* border */
6445 s->selected = 0;
6446 view->count = 0;
6447 te = got_object_tree_get_last_entry(s->tree);
6448 for (n = 0; n < eos; n++) {
6449 if (te == NULL) {
6450 if(s->tree != s->root) {
6451 s->first_displayed_entry = NULL;
6452 n++;
6454 break;
6456 s->first_displayed_entry = te;
6457 te = got_tree_entry_get_prev(s->tree, te);
6459 if (n > 0)
6460 s->selected = n - 1;
6461 break;
6463 case 'k':
6464 case KEY_UP:
6465 case CTRL('p'):
6466 if (s->selected > 0) {
6467 s->selected--;
6468 break;
6470 tree_scroll_up(s, 1);
6471 if (s->selected_entry == NULL ||
6472 (s->tree == s->root && s->selected_entry ==
6473 got_object_tree_get_first_entry(s->tree)))
6474 view->count = 0;
6475 break;
6476 case CTRL('u'):
6477 case 'u':
6478 nscroll /= 2;
6479 /* FALL THROUGH */
6480 case KEY_PPAGE:
6481 case CTRL('b'):
6482 case 'b':
6483 if (s->tree == s->root) {
6484 if (got_object_tree_get_first_entry(s->tree) ==
6485 s->first_displayed_entry)
6486 s->selected -= MIN(s->selected, nscroll);
6487 } else {
6488 if (s->first_displayed_entry == NULL)
6489 s->selected -= MIN(s->selected, nscroll);
6491 tree_scroll_up(s, MAX(0, nscroll));
6492 if (s->selected_entry == NULL ||
6493 (s->tree == s->root && s->selected_entry ==
6494 got_object_tree_get_first_entry(s->tree)))
6495 view->count = 0;
6496 break;
6497 case 'j':
6498 case KEY_DOWN:
6499 case CTRL('n'):
6500 if (s->selected < s->ndisplayed - 1) {
6501 s->selected++;
6502 break;
6504 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6505 == NULL) {
6506 /* can't scroll any further */
6507 view->count = 0;
6508 break;
6510 tree_scroll_down(view, 1);
6511 break;
6512 case CTRL('d'):
6513 case 'd':
6514 nscroll /= 2;
6515 /* FALL THROUGH */
6516 case KEY_NPAGE:
6517 case CTRL('f'):
6518 case 'f':
6519 case ' ':
6520 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6521 == NULL) {
6522 /* can't scroll any further; move cursor down */
6523 if (s->selected < s->ndisplayed - 1)
6524 s->selected += MIN(nscroll,
6525 s->ndisplayed - s->selected - 1);
6526 else
6527 view->count = 0;
6528 break;
6530 tree_scroll_down(view, nscroll);
6531 break;
6532 case KEY_ENTER:
6533 case '\r':
6534 case KEY_BACKSPACE:
6535 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6536 struct tog_parent_tree *parent;
6537 /* user selected '..' */
6538 if (s->tree == s->root) {
6539 view->count = 0;
6540 break;
6542 parent = TAILQ_FIRST(&s->parents);
6543 TAILQ_REMOVE(&s->parents, parent,
6544 entry);
6545 got_object_tree_close(s->tree);
6546 s->tree = parent->tree;
6547 s->first_displayed_entry =
6548 parent->first_displayed_entry;
6549 s->selected_entry =
6550 parent->selected_entry;
6551 s->selected = parent->selected;
6552 if (s->selected > view->nlines - 3) {
6553 err = offset_selection_down(view);
6554 if (err)
6555 break;
6557 free(parent);
6558 } else if (S_ISDIR(got_tree_entry_get_mode(
6559 s->selected_entry))) {
6560 struct got_tree_object *subtree;
6561 view->count = 0;
6562 err = got_object_open_as_tree(&subtree, s->repo,
6563 got_tree_entry_get_id(s->selected_entry));
6564 if (err)
6565 break;
6566 err = tree_view_visit_subtree(s, subtree);
6567 if (err) {
6568 got_object_tree_close(subtree);
6569 break;
6571 } else if (S_ISREG(got_tree_entry_get_mode(
6572 s->selected_entry))) {
6573 struct tog_view *blame_view;
6574 int begin_x = 0, begin_y = 0;
6576 if (view_is_parent_view(view))
6577 view_get_split(view, &begin_y, &begin_x);
6579 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6580 s->selected_entry, &s->parents,
6581 s->commit_id, s->repo);
6582 if (err)
6583 break;
6585 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
6586 err = view_init_hsplit(view, begin_y);
6587 if (err)
6588 break;
6591 view->count = 0;
6592 view->focussed = 0;
6593 blame_view->focussed = 1;
6594 blame_view->mode = view->mode;
6595 blame_view->nlines = view->lines - begin_y;
6596 if (view_is_parent_view(view)) {
6597 err = view_close_child(view);
6598 if (err)
6599 return err;
6600 err = view_set_child(view, blame_view);
6601 if (err)
6602 return err;
6603 view->focus_child = 1;
6604 } else
6605 *new_view = blame_view;
6607 break;
6608 case KEY_RESIZE:
6609 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6610 s->selected = view->nlines - 4;
6611 view->count = 0;
6612 break;
6613 default:
6614 view->count = 0;
6615 break;
6618 return err;
6621 __dead static void
6622 usage_tree(void)
6624 endwin();
6625 fprintf(stderr,
6626 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6627 getprogname());
6628 exit(1);
6631 static const struct got_error *
6632 cmd_tree(int argc, char *argv[])
6634 const struct got_error *error;
6635 struct got_repository *repo = NULL;
6636 struct got_worktree *worktree = NULL;
6637 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6638 struct got_object_id *commit_id = NULL;
6639 struct got_commit_object *commit = NULL;
6640 const char *commit_id_arg = NULL;
6641 char *label = NULL;
6642 struct got_reference *ref = NULL;
6643 const char *head_ref_name = NULL;
6644 int ch;
6645 struct tog_view *view;
6646 int *pack_fds = NULL;
6648 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6649 switch (ch) {
6650 case 'c':
6651 commit_id_arg = optarg;
6652 break;
6653 case 'r':
6654 repo_path = realpath(optarg, NULL);
6655 if (repo_path == NULL)
6656 return got_error_from_errno2("realpath",
6657 optarg);
6658 break;
6659 default:
6660 usage_tree();
6661 /* NOTREACHED */
6665 argc -= optind;
6666 argv += optind;
6668 if (argc > 1)
6669 usage_tree();
6671 error = got_repo_pack_fds_open(&pack_fds);
6672 if (error != NULL)
6673 goto done;
6675 if (repo_path == NULL) {
6676 cwd = getcwd(NULL, 0);
6677 if (cwd == NULL)
6678 return got_error_from_errno("getcwd");
6679 error = got_worktree_open(&worktree, cwd);
6680 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6681 goto done;
6682 if (worktree)
6683 repo_path =
6684 strdup(got_worktree_get_repo_path(worktree));
6685 else
6686 repo_path = strdup(cwd);
6687 if (repo_path == NULL) {
6688 error = got_error_from_errno("strdup");
6689 goto done;
6693 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6694 if (error != NULL)
6695 goto done;
6697 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6698 repo, worktree);
6699 if (error)
6700 goto done;
6702 init_curses();
6704 error = apply_unveil(got_repo_get_path(repo), NULL);
6705 if (error)
6706 goto done;
6708 error = tog_load_refs(repo, 0);
6709 if (error)
6710 goto done;
6712 if (commit_id_arg == NULL) {
6713 error = got_repo_match_object_id(&commit_id, &label,
6714 worktree ? got_worktree_get_head_ref_name(worktree) :
6715 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6716 if (error)
6717 goto done;
6718 head_ref_name = label;
6719 } else {
6720 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6721 if (error == NULL)
6722 head_ref_name = got_ref_get_name(ref);
6723 else if (error->code != GOT_ERR_NOT_REF)
6724 goto done;
6725 error = got_repo_match_object_id(&commit_id, NULL,
6726 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6727 if (error)
6728 goto done;
6731 error = got_object_open_as_commit(&commit, repo, commit_id);
6732 if (error)
6733 goto done;
6735 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6736 if (view == NULL) {
6737 error = got_error_from_errno("view_open");
6738 goto done;
6740 error = open_tree_view(view, commit_id, head_ref_name, repo);
6741 if (error)
6742 goto done;
6743 if (!got_path_is_root_dir(in_repo_path)) {
6744 error = tree_view_walk_path(&view->state.tree, commit,
6745 in_repo_path);
6746 if (error)
6747 goto done;
6750 if (worktree) {
6751 /* Release work tree lock. */
6752 got_worktree_close(worktree);
6753 worktree = NULL;
6755 error = view_loop(view);
6756 done:
6757 free(repo_path);
6758 free(cwd);
6759 free(commit_id);
6760 free(label);
6761 if (ref)
6762 got_ref_close(ref);
6763 if (repo) {
6764 const struct got_error *close_err = got_repo_close(repo);
6765 if (error == NULL)
6766 error = close_err;
6768 if (pack_fds) {
6769 const struct got_error *pack_err =
6770 got_repo_pack_fds_close(pack_fds);
6771 if (error == NULL)
6772 error = pack_err;
6774 tog_free_refs();
6775 return error;
6778 static const struct got_error *
6779 ref_view_load_refs(struct tog_ref_view_state *s)
6781 struct got_reflist_entry *sre;
6782 struct tog_reflist_entry *re;
6784 s->nrefs = 0;
6785 TAILQ_FOREACH(sre, &tog_refs, entry) {
6786 if (strncmp(got_ref_get_name(sre->ref),
6787 "refs/got/", 9) == 0 &&
6788 strncmp(got_ref_get_name(sre->ref),
6789 "refs/got/backup/", 16) != 0)
6790 continue;
6792 re = malloc(sizeof(*re));
6793 if (re == NULL)
6794 return got_error_from_errno("malloc");
6796 re->ref = got_ref_dup(sre->ref);
6797 if (re->ref == NULL)
6798 return got_error_from_errno("got_ref_dup");
6799 re->idx = s->nrefs++;
6800 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6803 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6804 return NULL;
6807 static void
6808 ref_view_free_refs(struct tog_ref_view_state *s)
6810 struct tog_reflist_entry *re;
6812 while (!TAILQ_EMPTY(&s->refs)) {
6813 re = TAILQ_FIRST(&s->refs);
6814 TAILQ_REMOVE(&s->refs, re, entry);
6815 got_ref_close(re->ref);
6816 free(re);
6820 static const struct got_error *
6821 open_ref_view(struct tog_view *view, struct got_repository *repo)
6823 const struct got_error *err = NULL;
6824 struct tog_ref_view_state *s = &view->state.ref;
6826 s->selected_entry = 0;
6827 s->repo = repo;
6829 TAILQ_INIT(&s->refs);
6830 STAILQ_INIT(&s->colors);
6832 err = ref_view_load_refs(s);
6833 if (err)
6834 return err;
6836 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6837 err = add_color(&s->colors, "^refs/heads/",
6838 TOG_COLOR_REFS_HEADS,
6839 get_color_value("TOG_COLOR_REFS_HEADS"));
6840 if (err)
6841 goto done;
6843 err = add_color(&s->colors, "^refs/tags/",
6844 TOG_COLOR_REFS_TAGS,
6845 get_color_value("TOG_COLOR_REFS_TAGS"));
6846 if (err)
6847 goto done;
6849 err = add_color(&s->colors, "^refs/remotes/",
6850 TOG_COLOR_REFS_REMOTES,
6851 get_color_value("TOG_COLOR_REFS_REMOTES"));
6852 if (err)
6853 goto done;
6855 err = add_color(&s->colors, "^refs/got/backup/",
6856 TOG_COLOR_REFS_BACKUP,
6857 get_color_value("TOG_COLOR_REFS_BACKUP"));
6858 if (err)
6859 goto done;
6862 view->show = show_ref_view;
6863 view->input = input_ref_view;
6864 view->close = close_ref_view;
6865 view->search_start = search_start_ref_view;
6866 view->search_next = search_next_ref_view;
6867 done:
6868 if (err)
6869 free_colors(&s->colors);
6870 return err;
6873 static const struct got_error *
6874 close_ref_view(struct tog_view *view)
6876 struct tog_ref_view_state *s = &view->state.ref;
6878 ref_view_free_refs(s);
6879 free_colors(&s->colors);
6881 return NULL;
6884 static const struct got_error *
6885 resolve_reflist_entry(struct got_object_id **commit_id,
6886 struct tog_reflist_entry *re, struct got_repository *repo)
6888 const struct got_error *err = NULL;
6889 struct got_object_id *obj_id;
6890 struct got_tag_object *tag = NULL;
6891 int obj_type;
6893 *commit_id = NULL;
6895 err = got_ref_resolve(&obj_id, repo, re->ref);
6896 if (err)
6897 return err;
6899 err = got_object_get_type(&obj_type, repo, obj_id);
6900 if (err)
6901 goto done;
6903 switch (obj_type) {
6904 case GOT_OBJ_TYPE_COMMIT:
6905 *commit_id = obj_id;
6906 break;
6907 case GOT_OBJ_TYPE_TAG:
6908 err = got_object_open_as_tag(&tag, repo, obj_id);
6909 if (err)
6910 goto done;
6911 free(obj_id);
6912 err = got_object_get_type(&obj_type, repo,
6913 got_object_tag_get_object_id(tag));
6914 if (err)
6915 goto done;
6916 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6917 err = got_error(GOT_ERR_OBJ_TYPE);
6918 goto done;
6920 *commit_id = got_object_id_dup(
6921 got_object_tag_get_object_id(tag));
6922 if (*commit_id == NULL) {
6923 err = got_error_from_errno("got_object_id_dup");
6924 goto done;
6926 break;
6927 default:
6928 err = got_error(GOT_ERR_OBJ_TYPE);
6929 break;
6932 done:
6933 if (tag)
6934 got_object_tag_close(tag);
6935 if (err) {
6936 free(*commit_id);
6937 *commit_id = NULL;
6939 return err;
6942 static const struct got_error *
6943 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6944 struct tog_reflist_entry *re, struct got_repository *repo)
6946 struct tog_view *log_view;
6947 const struct got_error *err = NULL;
6948 struct got_object_id *commit_id = NULL;
6950 *new_view = NULL;
6952 err = resolve_reflist_entry(&commit_id, re, repo);
6953 if (err) {
6954 if (err->code != GOT_ERR_OBJ_TYPE)
6955 return err;
6956 else
6957 return NULL;
6960 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6961 if (log_view == NULL) {
6962 err = got_error_from_errno("view_open");
6963 goto done;
6966 err = open_log_view(log_view, commit_id, repo,
6967 got_ref_get_name(re->ref), "", 0);
6968 done:
6969 if (err)
6970 view_close(log_view);
6971 else
6972 *new_view = log_view;
6973 free(commit_id);
6974 return err;
6977 static void
6978 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6980 struct tog_reflist_entry *re;
6981 int i = 0;
6983 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6984 return;
6986 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6987 while (i++ < maxscroll) {
6988 if (re == NULL)
6989 break;
6990 s->first_displayed_entry = re;
6991 re = TAILQ_PREV(re, tog_reflist_head, entry);
6995 static const struct got_error *
6996 ref_scroll_down(struct tog_view *view, int maxscroll)
6998 struct tog_ref_view_state *s = &view->state.ref;
6999 struct tog_reflist_entry *next, *last;
7000 int n = 0;
7002 if (s->first_displayed_entry)
7003 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7004 else
7005 next = TAILQ_FIRST(&s->refs);
7007 last = s->last_displayed_entry;
7008 while (next && n++ < maxscroll) {
7009 if (last)
7010 last = TAILQ_NEXT(last, entry);
7011 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7012 s->first_displayed_entry = next;
7013 next = TAILQ_NEXT(next, entry);
7017 return NULL;
7020 static const struct got_error *
7021 search_start_ref_view(struct tog_view *view)
7023 struct tog_ref_view_state *s = &view->state.ref;
7025 s->matched_entry = NULL;
7026 return NULL;
7029 static int
7030 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7032 regmatch_t regmatch;
7034 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7035 0) == 0;
7038 static const struct got_error *
7039 search_next_ref_view(struct tog_view *view)
7041 struct tog_ref_view_state *s = &view->state.ref;
7042 struct tog_reflist_entry *re = NULL;
7044 if (!view->searching) {
7045 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7046 return NULL;
7049 if (s->matched_entry) {
7050 if (view->searching == TOG_SEARCH_FORWARD) {
7051 if (s->selected_entry)
7052 re = TAILQ_NEXT(s->selected_entry, entry);
7053 else
7054 re = TAILQ_PREV(s->selected_entry,
7055 tog_reflist_head, entry);
7056 } else {
7057 if (s->selected_entry == NULL)
7058 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7059 else
7060 re = TAILQ_PREV(s->selected_entry,
7061 tog_reflist_head, entry);
7063 } else {
7064 if (s->selected_entry)
7065 re = s->selected_entry;
7066 else if (view->searching == TOG_SEARCH_FORWARD)
7067 re = TAILQ_FIRST(&s->refs);
7068 else
7069 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7072 while (1) {
7073 if (re == NULL) {
7074 if (s->matched_entry == NULL) {
7075 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7076 return NULL;
7078 if (view->searching == TOG_SEARCH_FORWARD)
7079 re = TAILQ_FIRST(&s->refs);
7080 else
7081 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7084 if (match_reflist_entry(re, &view->regex)) {
7085 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7086 s->matched_entry = re;
7087 break;
7090 if (view->searching == TOG_SEARCH_FORWARD)
7091 re = TAILQ_NEXT(re, entry);
7092 else
7093 re = TAILQ_PREV(re, tog_reflist_head, entry);
7096 if (s->matched_entry) {
7097 s->first_displayed_entry = s->matched_entry;
7098 s->selected = 0;
7101 return NULL;
7104 static const struct got_error *
7105 show_ref_view(struct tog_view *view)
7107 const struct got_error *err = NULL;
7108 struct tog_ref_view_state *s = &view->state.ref;
7109 struct tog_reflist_entry *re;
7110 char *line = NULL;
7111 wchar_t *wline;
7112 struct tog_color *tc;
7113 int width, n;
7114 int limit = view->nlines;
7116 werase(view->window);
7118 s->ndisplayed = 0;
7119 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
7120 view_is_splitscreen(view->child))
7121 --limit; /* border */
7123 if (limit == 0)
7124 return NULL;
7126 re = s->first_displayed_entry;
7128 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7129 s->nrefs) == -1)
7130 return got_error_from_errno("asprintf");
7132 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7133 if (err) {
7134 free(line);
7135 return err;
7137 if (view_needs_focus_indication(view))
7138 wstandout(view->window);
7139 waddwstr(view->window, wline);
7140 if (view_needs_focus_indication(view))
7141 wstandend(view->window);
7142 free(wline);
7143 wline = NULL;
7144 free(line);
7145 line = NULL;
7146 if (width < view->ncols - 1)
7147 waddch(view->window, '\n');
7148 if (--limit <= 0)
7149 return NULL;
7151 n = 0;
7152 while (re && limit > 0) {
7153 char *line = NULL;
7154 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7156 if (s->show_date) {
7157 struct got_commit_object *ci;
7158 struct got_tag_object *tag;
7159 struct got_object_id *id;
7160 struct tm tm;
7161 time_t t;
7163 err = got_ref_resolve(&id, s->repo, re->ref);
7164 if (err)
7165 return err;
7166 err = got_object_open_as_tag(&tag, s->repo, id);
7167 if (err) {
7168 if (err->code != GOT_ERR_OBJ_TYPE) {
7169 free(id);
7170 return err;
7172 err = got_object_open_as_commit(&ci, s->repo,
7173 id);
7174 if (err) {
7175 free(id);
7176 return err;
7178 t = got_object_commit_get_committer_time(ci);
7179 got_object_commit_close(ci);
7180 } else {
7181 t = got_object_tag_get_tagger_time(tag);
7182 got_object_tag_close(tag);
7184 free(id);
7185 if (gmtime_r(&t, &tm) == NULL)
7186 return got_error_from_errno("gmtime_r");
7187 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7188 return got_error(GOT_ERR_NO_SPACE);
7190 if (got_ref_is_symbolic(re->ref)) {
7191 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7192 ymd : "", got_ref_get_name(re->ref),
7193 got_ref_get_symref_target(re->ref)) == -1)
7194 return got_error_from_errno("asprintf");
7195 } else if (s->show_ids) {
7196 struct got_object_id *id;
7197 char *id_str;
7198 err = got_ref_resolve(&id, s->repo, re->ref);
7199 if (err)
7200 return err;
7201 err = got_object_id_str(&id_str, id);
7202 if (err) {
7203 free(id);
7204 return err;
7206 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7207 got_ref_get_name(re->ref), id_str) == -1) {
7208 err = got_error_from_errno("asprintf");
7209 free(id);
7210 free(id_str);
7211 return err;
7213 free(id);
7214 free(id_str);
7215 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7216 got_ref_get_name(re->ref)) == -1)
7217 return got_error_from_errno("asprintf");
7219 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7220 0, 0);
7221 if (err) {
7222 free(line);
7223 return err;
7225 if (n == s->selected) {
7226 if (view->focussed)
7227 wstandout(view->window);
7228 s->selected_entry = re;
7230 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7231 if (tc)
7232 wattr_on(view->window,
7233 COLOR_PAIR(tc->colorpair), NULL);
7234 waddwstr(view->window, wline);
7235 if (tc)
7236 wattr_off(view->window,
7237 COLOR_PAIR(tc->colorpair), NULL);
7238 if (width < view->ncols - 1)
7239 waddch(view->window, '\n');
7240 if (n == s->selected && view->focussed)
7241 wstandend(view->window);
7242 free(line);
7243 free(wline);
7244 wline = NULL;
7245 n++;
7246 s->ndisplayed++;
7247 s->last_displayed_entry = re;
7249 limit--;
7250 re = TAILQ_NEXT(re, entry);
7253 view_border(view);
7254 return err;
7257 static const struct got_error *
7258 browse_ref_tree(struct tog_view **new_view, int begin_x,
7259 struct tog_reflist_entry *re, struct got_repository *repo)
7261 const struct got_error *err = NULL;
7262 struct got_object_id *commit_id = NULL;
7263 struct tog_view *tree_view;
7265 *new_view = NULL;
7267 err = resolve_reflist_entry(&commit_id, re, repo);
7268 if (err) {
7269 if (err->code != GOT_ERR_OBJ_TYPE)
7270 return err;
7271 else
7272 return NULL;
7276 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
7277 if (tree_view == NULL) {
7278 err = got_error_from_errno("view_open");
7279 goto done;
7282 err = open_tree_view(tree_view, commit_id,
7283 got_ref_get_name(re->ref), repo);
7284 if (err)
7285 goto done;
7287 *new_view = tree_view;
7288 done:
7289 free(commit_id);
7290 return err;
7292 static const struct got_error *
7293 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7295 const struct got_error *err = NULL;
7296 struct tog_ref_view_state *s = &view->state.ref;
7297 struct tog_view *log_view, *tree_view;
7298 struct tog_reflist_entry *re;
7299 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7301 switch (ch) {
7302 case 'i':
7303 s->show_ids = !s->show_ids;
7304 view->count = 0;
7305 break;
7306 case 'm':
7307 s->show_date = !s->show_date;
7308 view->count = 0;
7309 break;
7310 case 'o':
7311 s->sort_by_date = !s->sort_by_date;
7312 view->count = 0;
7313 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7314 got_ref_cmp_by_commit_timestamp_descending :
7315 tog_ref_cmp_by_name, s->repo);
7316 if (err)
7317 break;
7318 got_reflist_object_id_map_free(tog_refs_idmap);
7319 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7320 &tog_refs, s->repo);
7321 if (err)
7322 break;
7323 ref_view_free_refs(s);
7324 err = ref_view_load_refs(s);
7325 break;
7326 case KEY_ENTER:
7327 case '\r':
7328 view->count = 0;
7329 if (!s->selected_entry)
7330 break;
7331 if (view_is_parent_view(view))
7332 view_get_split(view, &begin_y, &begin_x);
7334 err = log_ref_entry(&log_view, begin_y, begin_x,
7335 s->selected_entry, s->repo);
7336 if (err)
7337 break;
7339 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
7340 err = view_init_hsplit(view, begin_y);
7341 if (err)
7342 break;
7345 view->focussed = 0;
7346 log_view->focussed = 1;
7347 log_view->mode = view->mode;
7348 log_view->nlines = view->lines - begin_y;
7349 if (view_is_parent_view(view)) {
7350 err = view_close_child(view);
7351 if (err)
7352 return err;
7353 err = view_set_child(view, log_view);
7354 if (err)
7355 return err;
7356 view->focus_child = 1;
7357 } else
7358 *new_view = log_view;
7359 break;
7360 case 't':
7361 view->count = 0;
7362 if (!s->selected_entry)
7363 break;
7364 if (view_is_parent_view(view))
7365 begin_x = view_split_begin_x(view->begin_x);
7366 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
7367 s->repo);
7368 if (err || tree_view == NULL)
7369 break;
7370 view->focussed = 0;
7371 tree_view->focussed = 1;
7372 if (view_is_parent_view(view)) {
7373 err = view_close_child(view);
7374 if (err)
7375 return err;
7376 err = view_set_child(view, tree_view);
7377 if (err)
7378 return err;
7379 view->focus_child = 1;
7380 } else
7381 *new_view = tree_view;
7382 break;
7383 case 'g':
7384 case KEY_HOME:
7385 s->selected = 0;
7386 view->count = 0;
7387 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7388 break;
7389 case 'G':
7390 case KEY_END: {
7391 int eos = view->nlines - 1;
7393 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7394 --eos; /* border */
7395 s->selected = 0;
7396 view->count = 0;
7397 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7398 for (n = 0; n < eos; n++) {
7399 if (re == NULL)
7400 break;
7401 s->first_displayed_entry = re;
7402 re = TAILQ_PREV(re, tog_reflist_head, entry);
7404 if (n > 0)
7405 s->selected = n - 1;
7406 break;
7408 case 'k':
7409 case KEY_UP:
7410 case CTRL('p'):
7411 if (s->selected > 0) {
7412 s->selected--;
7413 break;
7415 ref_scroll_up(s, 1);
7416 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7417 view->count = 0;
7418 break;
7419 case CTRL('u'):
7420 case 'u':
7421 nscroll /= 2;
7422 /* FALL THROUGH */
7423 case KEY_PPAGE:
7424 case CTRL('b'):
7425 case 'b':
7426 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7427 s->selected -= MIN(nscroll, s->selected);
7428 ref_scroll_up(s, MAX(0, nscroll));
7429 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7430 view->count = 0;
7431 break;
7432 case 'j':
7433 case KEY_DOWN:
7434 case CTRL('n'):
7435 if (s->selected < s->ndisplayed - 1) {
7436 s->selected++;
7437 break;
7439 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7440 /* can't scroll any further */
7441 view->count = 0;
7442 break;
7444 ref_scroll_down(view, 1);
7445 break;
7446 case CTRL('d'):
7447 case 'd':
7448 nscroll /= 2;
7449 /* FALL THROUGH */
7450 case KEY_NPAGE:
7451 case CTRL('f'):
7452 case 'f':
7453 case ' ':
7454 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7455 /* can't scroll any further; move cursor down */
7456 if (s->selected < s->ndisplayed - 1)
7457 s->selected += MIN(nscroll,
7458 s->ndisplayed - s->selected - 1);
7459 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7460 s->selected += s->ndisplayed - s->selected - 1;
7461 view->count = 0;
7462 break;
7464 ref_scroll_down(view, nscroll);
7465 break;
7466 case CTRL('l'):
7467 view->count = 0;
7468 tog_free_refs();
7469 err = tog_load_refs(s->repo, s->sort_by_date);
7470 if (err)
7471 break;
7472 ref_view_free_refs(s);
7473 err = ref_view_load_refs(s);
7474 break;
7475 case KEY_RESIZE:
7476 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7477 s->selected = view->nlines - 2;
7478 break;
7479 default:
7480 view->count = 0;
7481 break;
7484 return err;
7487 __dead static void
7488 usage_ref(void)
7490 endwin();
7491 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7492 getprogname());
7493 exit(1);
7496 static const struct got_error *
7497 cmd_ref(int argc, char *argv[])
7499 const struct got_error *error;
7500 struct got_repository *repo = NULL;
7501 struct got_worktree *worktree = NULL;
7502 char *cwd = NULL, *repo_path = NULL;
7503 int ch;
7504 struct tog_view *view;
7505 int *pack_fds = NULL;
7507 while ((ch = getopt(argc, argv, "r:")) != -1) {
7508 switch (ch) {
7509 case 'r':
7510 repo_path = realpath(optarg, NULL);
7511 if (repo_path == NULL)
7512 return got_error_from_errno2("realpath",
7513 optarg);
7514 break;
7515 default:
7516 usage_ref();
7517 /* NOTREACHED */
7521 argc -= optind;
7522 argv += optind;
7524 if (argc > 1)
7525 usage_ref();
7527 error = got_repo_pack_fds_open(&pack_fds);
7528 if (error != NULL)
7529 goto done;
7531 if (repo_path == NULL) {
7532 cwd = getcwd(NULL, 0);
7533 if (cwd == NULL)
7534 return got_error_from_errno("getcwd");
7535 error = got_worktree_open(&worktree, cwd);
7536 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7537 goto done;
7538 if (worktree)
7539 repo_path =
7540 strdup(got_worktree_get_repo_path(worktree));
7541 else
7542 repo_path = strdup(cwd);
7543 if (repo_path == NULL) {
7544 error = got_error_from_errno("strdup");
7545 goto done;
7549 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7550 if (error != NULL)
7551 goto done;
7553 init_curses();
7555 error = apply_unveil(got_repo_get_path(repo), NULL);
7556 if (error)
7557 goto done;
7559 error = tog_load_refs(repo, 0);
7560 if (error)
7561 goto done;
7563 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7564 if (view == NULL) {
7565 error = got_error_from_errno("view_open");
7566 goto done;
7569 error = open_ref_view(view, repo);
7570 if (error)
7571 goto done;
7573 if (worktree) {
7574 /* Release work tree lock. */
7575 got_worktree_close(worktree);
7576 worktree = NULL;
7578 error = view_loop(view);
7579 done:
7580 free(repo_path);
7581 free(cwd);
7582 if (repo) {
7583 const struct got_error *close_err = got_repo_close(repo);
7584 if (close_err)
7585 error = close_err;
7587 if (pack_fds) {
7588 const struct got_error *pack_err =
7589 got_repo_pack_fds_close(pack_fds);
7590 if (error == NULL)
7591 error = pack_err;
7593 tog_free_refs();
7594 return error;
7598 * If view was scrolled down to move the selected line into view when opening a
7599 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7601 static void
7602 offset_selection_up(struct tog_view *view)
7604 switch (view->type) {
7605 case TOG_VIEW_BLAME: {
7606 struct tog_blame_view_state *s = &view->state.blame;
7607 if (s->first_displayed_line == 1) {
7608 s->selected_line = MAX(s->selected_line - view->offset,
7609 1);
7610 break;
7612 if (s->first_displayed_line > view->offset)
7613 s->first_displayed_line -= view->offset;
7614 else
7615 s->first_displayed_line = 1;
7616 s->selected_line += view->offset;
7617 break;
7619 case TOG_VIEW_LOG:
7620 log_scroll_up(&view->state.log, view->offset);
7621 view->state.log.selected += view->offset;
7622 break;
7623 case TOG_VIEW_REF:
7624 ref_scroll_up(&view->state.ref, view->offset);
7625 view->state.ref.selected += view->offset;
7626 break;
7627 case TOG_VIEW_TREE:
7628 tree_scroll_up(&view->state.tree, view->offset);
7629 view->state.tree.selected += view->offset;
7630 break;
7631 default:
7632 break;
7635 view->offset = 0;
7639 * If the selected line is in the section of screen covered by the bottom split,
7640 * scroll down offset lines to move it into view and index its new position.
7642 static const struct got_error *
7643 offset_selection_down(struct tog_view *view)
7645 const struct got_error *err = NULL;
7646 const struct got_error *(*scrolld)(struct tog_view *, int);
7647 int *selected = NULL;
7648 int header, offset;
7650 switch (view->type) {
7651 case TOG_VIEW_BLAME: {
7652 struct tog_blame_view_state *s = &view->state.blame;
7653 header = 3;
7654 scrolld = NULL;
7655 if (s->selected_line > view->nlines - header) {
7656 offset = abs(view->nlines - s->selected_line - header);
7657 s->first_displayed_line += offset;
7658 s->selected_line -= offset;
7659 view->offset = offset;
7661 break;
7663 case TOG_VIEW_LOG: {
7664 struct tog_log_view_state *s = &view->state.log;
7665 scrolld = &log_scroll_down;
7666 header = 3;
7667 selected = &s->selected;
7668 break;
7670 case TOG_VIEW_REF: {
7671 struct tog_ref_view_state *s = &view->state.ref;
7672 scrolld = &ref_scroll_down;
7673 header = 3;
7674 selected = &s->selected;
7675 break;
7677 case TOG_VIEW_TREE: {
7678 struct tog_tree_view_state *s = &view->state.tree;
7679 scrolld = &tree_scroll_down;
7680 header = 5;
7681 selected = &s->selected;
7682 break;
7684 default:
7685 selected = NULL;
7686 scrolld = NULL;
7687 header = 0;
7688 break;
7691 if (selected && *selected > view->nlines - header) {
7692 offset = abs(view->nlines - *selected - header);
7693 view->offset = offset;
7694 if (scrolld && offset) {
7695 err = scrolld(view, offset);
7696 *selected -= offset;
7700 return err;
7703 static void
7704 list_commands(FILE *fp)
7706 size_t i;
7708 fprintf(fp, "commands:");
7709 for (i = 0; i < nitems(tog_commands); i++) {
7710 const struct tog_cmd *cmd = &tog_commands[i];
7711 fprintf(fp, " %s", cmd->name);
7713 fputc('\n', fp);
7716 __dead static void
7717 usage(int hflag, int status)
7719 FILE *fp = (status == 0) ? stdout : stderr;
7721 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7722 getprogname());
7723 if (hflag) {
7724 fprintf(fp, "lazy usage: %s path\n", getprogname());
7725 list_commands(fp);
7727 exit(status);
7730 static char **
7731 make_argv(int argc, ...)
7733 va_list ap;
7734 char **argv;
7735 int i;
7737 va_start(ap, argc);
7739 argv = calloc(argc, sizeof(char *));
7740 if (argv == NULL)
7741 err(1, "calloc");
7742 for (i = 0; i < argc; i++) {
7743 argv[i] = strdup(va_arg(ap, char *));
7744 if (argv[i] == NULL)
7745 err(1, "strdup");
7748 va_end(ap);
7749 return argv;
7753 * Try to convert 'tog path' into a 'tog log path' command.
7754 * The user could simply have mistyped the command rather than knowingly
7755 * provided a path. So check whether argv[0] can in fact be resolved
7756 * to a path in the HEAD commit and print a special error if not.
7757 * This hack is for mpi@ <3
7759 static const struct got_error *
7760 tog_log_with_path(int argc, char *argv[])
7762 const struct got_error *error = NULL, *close_err;
7763 const struct tog_cmd *cmd = NULL;
7764 struct got_repository *repo = NULL;
7765 struct got_worktree *worktree = NULL;
7766 struct got_object_id *commit_id = NULL, *id = NULL;
7767 struct got_commit_object *commit = NULL;
7768 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7769 char *commit_id_str = NULL, **cmd_argv = NULL;
7770 int *pack_fds = NULL;
7772 cwd = getcwd(NULL, 0);
7773 if (cwd == NULL)
7774 return got_error_from_errno("getcwd");
7776 error = got_repo_pack_fds_open(&pack_fds);
7777 if (error != NULL)
7778 goto done;
7780 error = got_worktree_open(&worktree, cwd);
7781 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7782 goto done;
7784 if (worktree)
7785 repo_path = strdup(got_worktree_get_repo_path(worktree));
7786 else
7787 repo_path = strdup(cwd);
7788 if (repo_path == NULL) {
7789 error = got_error_from_errno("strdup");
7790 goto done;
7793 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7794 if (error != NULL)
7795 goto done;
7797 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7798 repo, worktree);
7799 if (error)
7800 goto done;
7802 error = tog_load_refs(repo, 0);
7803 if (error)
7804 goto done;
7805 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7806 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7807 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7808 if (error)
7809 goto done;
7811 if (worktree) {
7812 got_worktree_close(worktree);
7813 worktree = NULL;
7816 error = got_object_open_as_commit(&commit, repo, commit_id);
7817 if (error)
7818 goto done;
7820 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7821 if (error) {
7822 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7823 goto done;
7824 fprintf(stderr, "%s: '%s' is no known command or path\n",
7825 getprogname(), argv[0]);
7826 usage(1, 1);
7827 /* not reached */
7830 close_err = got_repo_close(repo);
7831 if (error == NULL)
7832 error = close_err;
7833 repo = NULL;
7835 error = got_object_id_str(&commit_id_str, commit_id);
7836 if (error)
7837 goto done;
7839 cmd = &tog_commands[0]; /* log */
7840 argc = 4;
7841 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7842 error = cmd->cmd_main(argc, cmd_argv);
7843 done:
7844 if (repo) {
7845 close_err = got_repo_close(repo);
7846 if (error == NULL)
7847 error = close_err;
7849 if (commit)
7850 got_object_commit_close(commit);
7851 if (worktree)
7852 got_worktree_close(worktree);
7853 if (pack_fds) {
7854 const struct got_error *pack_err =
7855 got_repo_pack_fds_close(pack_fds);
7856 if (error == NULL)
7857 error = pack_err;
7859 free(id);
7860 free(commit_id_str);
7861 free(commit_id);
7862 free(cwd);
7863 free(repo_path);
7864 free(in_repo_path);
7865 if (cmd_argv) {
7866 int i;
7867 for (i = 0; i < argc; i++)
7868 free(cmd_argv[i]);
7869 free(cmd_argv);
7871 tog_free_refs();
7872 return error;
7875 int
7876 main(int argc, char *argv[])
7878 const struct got_error *error = NULL;
7879 const struct tog_cmd *cmd = NULL;
7880 int ch, hflag = 0, Vflag = 0;
7881 char **cmd_argv = NULL;
7882 static const struct option longopts[] = {
7883 { "version", no_argument, NULL, 'V' },
7884 { NULL, 0, NULL, 0}
7886 char *diff_algo_str = NULL;
7888 setlocale(LC_CTYPE, "");
7890 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7891 switch (ch) {
7892 case 'h':
7893 hflag = 1;
7894 break;
7895 case 'V':
7896 Vflag = 1;
7897 break;
7898 default:
7899 usage(hflag, 1);
7900 /* NOTREACHED */
7904 argc -= optind;
7905 argv += optind;
7906 optind = 1;
7907 optreset = 1;
7909 if (Vflag) {
7910 got_version_print_str();
7911 return 0;
7914 #ifndef PROFILE
7915 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7916 NULL) == -1)
7917 err(1, "pledge");
7918 #endif
7920 if (argc == 0) {
7921 if (hflag)
7922 usage(hflag, 0);
7923 /* Build an argument vector which runs a default command. */
7924 cmd = &tog_commands[0];
7925 argc = 1;
7926 cmd_argv = make_argv(argc, cmd->name);
7927 } else {
7928 size_t i;
7930 /* Did the user specify a command? */
7931 for (i = 0; i < nitems(tog_commands); i++) {
7932 if (strncmp(tog_commands[i].name, argv[0],
7933 strlen(argv[0])) == 0) {
7934 cmd = &tog_commands[i];
7935 break;
7940 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
7941 if (diff_algo_str) {
7942 if (strcasecmp(diff_algo_str, "patience") == 0)
7943 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
7944 if (strcasecmp(diff_algo_str, "myers") == 0)
7945 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
7948 if (cmd == NULL) {
7949 if (argc != 1)
7950 usage(0, 1);
7951 /* No command specified; try log with a path */
7952 error = tog_log_with_path(argc, argv);
7953 } else {
7954 if (hflag)
7955 cmd->cmd_usage();
7956 else
7957 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7960 endwin();
7961 putchar('\n');
7962 if (cmd_argv) {
7963 int i;
7964 for (i = 0; i < argc; i++)
7965 free(cmd_argv[i]);
7966 free(cmd_argv);
7969 if (error && error->code != GOT_ERR_CANCELLED)
7970 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7971 return 0;