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 input. Assign total to view->count and
1001 * return first non-numeric key entered.
1003 static int
1004 get_compound_key(struct tog_view *view, int c)
1006 struct tog_view *v = view;
1007 int x, n = 0;
1009 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
1010 view_is_splitscreen(view->child))
1011 v = view->child;
1012 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1013 v = view->parent;
1015 view->count = 0;
1016 cbreak(); /* block for input */
1017 wmove(v->window, v->nlines - 1, 0);
1018 wclrtoeol(v->window);
1019 waddch(v->window, ':');
1021 do {
1022 x = getcurx(v->window);
1023 if (x != ERR && x < view->ncols) {
1024 waddch(v->window, c);
1025 wrefresh(v->window);
1029 * Don't overflow. Max valid request should be the greatest
1030 * between the longest and total lines; cap at 10 million.
1032 if (n >= 9999999)
1033 n = 9999999;
1034 else
1035 n = n * 10 + (c - '0');
1036 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1038 /* Massage excessive or inapplicable values at the input handler. */
1039 view->count = n;
1041 return c;
1044 static const struct got_error *
1045 view_input(struct tog_view **new, int *done, struct tog_view *view,
1046 struct tog_view_list_head *views)
1048 const struct got_error *err = NULL;
1049 struct tog_view *v;
1050 int ch, errcode;
1052 *new = NULL;
1054 /* Clear "no matches" indicator. */
1055 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1056 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1057 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1058 view->count = 0;
1061 if (view->searching && !view->search_next_done) {
1062 errcode = pthread_mutex_unlock(&tog_mutex);
1063 if (errcode)
1064 return got_error_set_errno(errcode,
1065 "pthread_mutex_unlock");
1066 sched_yield();
1067 errcode = pthread_mutex_lock(&tog_mutex);
1068 if (errcode)
1069 return got_error_set_errno(errcode,
1070 "pthread_mutex_lock");
1071 view->search_next(view);
1072 return NULL;
1075 nodelay(stdscr, FALSE);
1076 /* Allow threads to make progress while we are waiting for input. */
1077 errcode = pthread_mutex_unlock(&tog_mutex);
1078 if (errcode)
1079 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1080 /* If we have an unfinished count, don't get a new key map. */
1081 ch = view->ch;
1082 if ((view->count && --view->count == 0) || !view->count) {
1083 ch = wgetch(view->window);
1084 if (ch >= '1' && ch <= '9')
1085 view->ch = ch = get_compound_key(view, ch);
1087 errcode = pthread_mutex_lock(&tog_mutex);
1088 if (errcode)
1089 return got_error_set_errno(errcode, "pthread_mutex_lock");
1090 nodelay(stdscr, TRUE);
1092 if (tog_sigwinch_received || tog_sigcont_received) {
1093 tog_resizeterm();
1094 tog_sigwinch_received = 0;
1095 tog_sigcont_received = 0;
1096 TAILQ_FOREACH(v, views, entry) {
1097 err = view_resize(v);
1098 if (err)
1099 return err;
1100 err = v->input(new, v, KEY_RESIZE);
1101 if (err)
1102 return err;
1103 if (v->child) {
1104 err = view_resize(v->child);
1105 if (err)
1106 return err;
1107 err = v->child->input(new, v->child,
1108 KEY_RESIZE);
1109 if (err)
1110 return err;
1115 switch (ch) {
1116 case '\t':
1117 view->count = 0;
1118 if (view->child) {
1119 view->focussed = 0;
1120 view->child->focussed = 1;
1121 view->focus_child = 1;
1122 } else if (view->parent) {
1123 view->focussed = 0;
1124 view->parent->focussed = 1;
1125 view->parent->focus_child = 0;
1126 if (!view_is_splitscreen(view)) {
1127 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1128 view->parent->type == TOG_VIEW_LOG) {
1129 err = request_log_commits(view->parent);
1130 if (err)
1131 return err;
1133 offset_selection_up(view->parent);
1134 err = view_fullscreen(view->parent);
1135 if (err)
1136 return err;
1139 break;
1140 case 'q':
1141 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1142 if (view->parent->type == TOG_VIEW_LOG) {
1143 /* might need more commits to fill fullscreen */
1144 err = request_log_commits(view->parent);
1145 if (err)
1146 break;
1148 offset_selection_up(view->parent);
1149 view->parent->mode = TOG_VIEW_SPLIT_NONE;
1151 err = view->input(new, view, ch);
1152 view->dying = 1;
1153 break;
1154 case 'Q':
1155 *done = 1;
1156 break;
1157 case 'F':
1158 view->count = 0;
1159 if (view_is_parent_view(view)) {
1160 if (view->child == NULL)
1161 break;
1162 if (view_is_splitscreen(view->child)) {
1163 view->focussed = 0;
1164 view->child->focussed = 1;
1165 err = view_fullscreen(view->child);
1166 } else
1167 err = view_splitscreen(view->child);
1168 if (err)
1169 break;
1170 err = view->child->input(new, view->child,
1171 KEY_RESIZE);
1172 } else {
1173 if (view_is_splitscreen(view)) {
1174 view->parent->focussed = 0;
1175 view->focussed = 1;
1176 err = view_fullscreen(view);
1177 } else {
1178 err = view_splitscreen(view);
1179 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1180 err = view_resize(view->parent);
1182 if (err)
1183 break;
1184 err = view->input(new, view, KEY_RESIZE);
1186 if (err)
1187 break;
1188 if (view->type == TOG_VIEW_LOG) {
1189 err = request_log_commits(view);
1190 if (err)
1191 break;
1193 if (view->parent)
1194 err = offset_selection_down(view->parent);
1195 if (!err)
1196 err = offset_selection_down(view);
1197 break;
1198 case KEY_RESIZE:
1199 break;
1200 case '/':
1201 view->count = 0;
1202 if (view->search_start)
1203 view_search_start(view);
1204 else
1205 err = view->input(new, view, ch);
1206 break;
1207 case 'N':
1208 case 'n':
1209 if (view->search_started && view->search_next) {
1210 view->searching = (ch == 'n' ?
1211 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1212 view->search_next_done = 0;
1213 view->search_next(view);
1214 } else
1215 err = view->input(new, view, ch);
1216 break;
1217 case 'A':
1218 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1219 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1220 else
1221 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1222 TAILQ_FOREACH(v, views, entry) {
1223 if (v->reset) {
1224 err = v->reset(v);
1225 if (err)
1226 return err;
1228 if (v->child && v->child->reset) {
1229 err = v->child->reset(v->child);
1230 if (err)
1231 return err;
1234 break;
1235 default:
1236 err = view->input(new, view, ch);
1237 break;
1240 return err;
1243 static int
1244 view_needs_focus_indication(struct tog_view *view)
1246 if (view_is_parent_view(view)) {
1247 if (view->child == NULL || view->child->focussed)
1248 return 0;
1249 if (!view_is_splitscreen(view->child))
1250 return 0;
1251 } else if (!view_is_splitscreen(view))
1252 return 0;
1254 return view->focussed;
1257 static const struct got_error *
1258 view_loop(struct tog_view *view)
1260 const struct got_error *err = NULL;
1261 struct tog_view_list_head views;
1262 struct tog_view *new_view;
1263 int fast_refresh = 10;
1264 int done = 0, errcode;
1266 errcode = pthread_mutex_lock(&tog_mutex);
1267 if (errcode)
1268 return got_error_set_errno(errcode, "pthread_mutex_lock");
1270 TAILQ_INIT(&views);
1271 TAILQ_INSERT_HEAD(&views, view, entry);
1273 view->focussed = 1;
1274 err = view->show(view);
1275 if (err)
1276 return err;
1277 update_panels();
1278 doupdate();
1279 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1280 /* Refresh fast during initialization, then become slower. */
1281 if (fast_refresh && fast_refresh-- == 0)
1282 halfdelay(10); /* switch to once per second */
1284 err = view_input(&new_view, &done, view, &views);
1285 if (err)
1286 break;
1287 if (view->dying) {
1288 struct tog_view *v, *prev = NULL;
1290 if (view_is_parent_view(view))
1291 prev = TAILQ_PREV(view, tog_view_list_head,
1292 entry);
1293 else if (view->parent)
1294 prev = view->parent;
1296 if (view->parent) {
1297 view->parent->child = NULL;
1298 view->parent->focus_child = 0;
1299 /* Restore fullscreen line height. */
1300 view->parent->nlines = view->parent->lines;
1301 err = view_resize(view->parent);
1302 if (err)
1303 break;
1304 } else
1305 TAILQ_REMOVE(&views, view, entry);
1307 err = view_close(view);
1308 if (err)
1309 goto done;
1311 view = NULL;
1312 TAILQ_FOREACH(v, &views, entry) {
1313 if (v->focussed)
1314 break;
1316 if (view == NULL && new_view == NULL) {
1317 /* No view has focus. Try to pick one. */
1318 if (prev)
1319 view = prev;
1320 else if (!TAILQ_EMPTY(&views)) {
1321 view = TAILQ_LAST(&views,
1322 tog_view_list_head);
1324 if (view) {
1325 if (view->focus_child) {
1326 view->child->focussed = 1;
1327 view = view->child;
1328 } else
1329 view->focussed = 1;
1333 if (new_view) {
1334 struct tog_view *v, *t;
1335 /* Only allow one parent view per type. */
1336 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1337 if (v->type != new_view->type)
1338 continue;
1339 TAILQ_REMOVE(&views, v, entry);
1340 err = view_close(v);
1341 if (err)
1342 goto done;
1343 break;
1345 TAILQ_INSERT_TAIL(&views, new_view, entry);
1346 view = new_view;
1348 if (view) {
1349 if (view_is_parent_view(view)) {
1350 if (view->child && view->child->focussed)
1351 view = view->child;
1352 } else {
1353 if (view->parent && view->parent->focussed)
1354 view = view->parent;
1356 show_panel(view->panel);
1357 if (view->child && view_is_splitscreen(view->child))
1358 show_panel(view->child->panel);
1359 if (view->parent && view_is_splitscreen(view)) {
1360 err = view->parent->show(view->parent);
1361 if (err)
1362 goto done;
1364 err = view->show(view);
1365 if (err)
1366 goto done;
1367 if (view->child) {
1368 err = view->child->show(view->child);
1369 if (err)
1370 goto done;
1372 update_panels();
1373 doupdate();
1376 done:
1377 while (!TAILQ_EMPTY(&views)) {
1378 view = TAILQ_FIRST(&views);
1379 TAILQ_REMOVE(&views, view, entry);
1380 view_close(view);
1383 errcode = pthread_mutex_unlock(&tog_mutex);
1384 if (errcode && err == NULL)
1385 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1387 return err;
1390 __dead static void
1391 usage_log(void)
1393 endwin();
1394 fprintf(stderr,
1395 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1396 getprogname());
1397 exit(1);
1400 /* Create newly allocated wide-character string equivalent to a byte string. */
1401 static const struct got_error *
1402 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1404 char *vis = NULL;
1405 const struct got_error *err = NULL;
1407 *ws = NULL;
1408 *wlen = mbstowcs(NULL, s, 0);
1409 if (*wlen == (size_t)-1) {
1410 int vislen;
1411 if (errno != EILSEQ)
1412 return got_error_from_errno("mbstowcs");
1414 /* byte string invalid in current encoding; try to "fix" it */
1415 err = got_mbsavis(&vis, &vislen, s);
1416 if (err)
1417 return err;
1418 *wlen = mbstowcs(NULL, vis, 0);
1419 if (*wlen == (size_t)-1) {
1420 err = got_error_from_errno("mbstowcs"); /* give up */
1421 goto done;
1425 *ws = calloc(*wlen + 1, sizeof(**ws));
1426 if (*ws == NULL) {
1427 err = got_error_from_errno("calloc");
1428 goto done;
1431 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1432 err = got_error_from_errno("mbstowcs");
1433 done:
1434 free(vis);
1435 if (err) {
1436 free(*ws);
1437 *ws = NULL;
1438 *wlen = 0;
1440 return err;
1443 static const struct got_error *
1444 expand_tab(char **ptr, const char *src)
1446 char *dst;
1447 size_t len, n, idx = 0, sz = 0;
1449 *ptr = NULL;
1450 n = len = strlen(src);
1451 dst = malloc(n + 1);
1452 if (dst == NULL)
1453 return got_error_from_errno("malloc");
1455 while (idx < len && src[idx]) {
1456 const char c = src[idx];
1458 if (c == '\t') {
1459 size_t nb = TABSIZE - sz % TABSIZE;
1460 char *p;
1462 p = realloc(dst, n + nb);
1463 if (p == NULL) {
1464 free(dst);
1465 return got_error_from_errno("realloc");
1468 dst = p;
1469 n += nb;
1470 memset(dst + sz, ' ', nb);
1471 sz += nb;
1472 } else
1473 dst[sz++] = src[idx];
1474 ++idx;
1477 dst[sz] = '\0';
1478 *ptr = dst;
1479 return NULL;
1483 * Advance at most n columns from wline starting at offset off.
1484 * Return the index to the first character after the span operation.
1485 * Return the combined column width of all spanned wide character in
1486 * *rcol.
1488 static int
1489 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1491 int width, i, cols = 0;
1493 if (n == 0) {
1494 *rcol = cols;
1495 return off;
1498 for (i = off; wline[i] != L'\0'; ++i) {
1499 if (wline[i] == L'\t')
1500 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1501 else
1502 width = wcwidth(wline[i]);
1504 if (width == -1) {
1505 width = 1;
1506 wline[i] = L'.';
1509 if (cols + width > n)
1510 break;
1511 cols += width;
1514 *rcol = cols;
1515 return i;
1519 * Format a line for display, ensuring that it won't overflow a width limit.
1520 * With scrolling, the width returned refers to the scrolled version of the
1521 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1523 static const struct got_error *
1524 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1525 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1527 const struct got_error *err = NULL;
1528 int cols;
1529 wchar_t *wline = NULL;
1530 char *exstr = NULL;
1531 size_t wlen;
1532 int i, scrollx;
1534 *wlinep = NULL;
1535 *widthp = 0;
1537 if (expand) {
1538 err = expand_tab(&exstr, line);
1539 if (err)
1540 return err;
1543 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1544 free(exstr);
1545 if (err)
1546 return err;
1548 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1550 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1551 wline[wlen - 1] = L'\0';
1552 wlen--;
1554 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1555 wline[wlen - 1] = L'\0';
1556 wlen--;
1559 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1560 wline[i] = L'\0';
1562 if (widthp)
1563 *widthp = cols;
1564 if (scrollxp)
1565 *scrollxp = scrollx;
1566 if (err)
1567 free(wline);
1568 else
1569 *wlinep = wline;
1570 return err;
1573 static const struct got_error*
1574 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1575 struct got_object_id *id, struct got_repository *repo)
1577 static const struct got_error *err = NULL;
1578 struct got_reflist_entry *re;
1579 char *s;
1580 const char *name;
1582 *refs_str = NULL;
1584 TAILQ_FOREACH(re, refs, entry) {
1585 struct got_tag_object *tag = NULL;
1586 struct got_object_id *ref_id;
1587 int cmp;
1589 name = got_ref_get_name(re->ref);
1590 if (strcmp(name, GOT_REF_HEAD) == 0)
1591 continue;
1592 if (strncmp(name, "refs/", 5) == 0)
1593 name += 5;
1594 if (strncmp(name, "got/", 4) == 0 &&
1595 strncmp(name, "got/backup/", 11) != 0)
1596 continue;
1597 if (strncmp(name, "heads/", 6) == 0)
1598 name += 6;
1599 if (strncmp(name, "remotes/", 8) == 0) {
1600 name += 8;
1601 s = strstr(name, "/" GOT_REF_HEAD);
1602 if (s != NULL && s[strlen(s)] == '\0')
1603 continue;
1605 err = got_ref_resolve(&ref_id, repo, re->ref);
1606 if (err)
1607 break;
1608 if (strncmp(name, "tags/", 5) == 0) {
1609 err = got_object_open_as_tag(&tag, repo, ref_id);
1610 if (err) {
1611 if (err->code != GOT_ERR_OBJ_TYPE) {
1612 free(ref_id);
1613 break;
1615 /* Ref points at something other than a tag. */
1616 err = NULL;
1617 tag = NULL;
1620 cmp = got_object_id_cmp(tag ?
1621 got_object_tag_get_object_id(tag) : ref_id, id);
1622 free(ref_id);
1623 if (tag)
1624 got_object_tag_close(tag);
1625 if (cmp != 0)
1626 continue;
1627 s = *refs_str;
1628 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1629 s ? ", " : "", name) == -1) {
1630 err = got_error_from_errno("asprintf");
1631 free(s);
1632 *refs_str = NULL;
1633 break;
1635 free(s);
1638 return err;
1641 static const struct got_error *
1642 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1643 int col_tab_align)
1645 char *smallerthan;
1647 smallerthan = strchr(author, '<');
1648 if (smallerthan && smallerthan[1] != '\0')
1649 author = smallerthan + 1;
1650 author[strcspn(author, "@>")] = '\0';
1651 return format_line(wauthor, author_width, NULL, author, 0, limit,
1652 col_tab_align, 0);
1655 static const struct got_error *
1656 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1657 struct got_object_id *id, const size_t date_display_cols,
1658 int author_display_cols)
1660 struct tog_log_view_state *s = &view->state.log;
1661 const struct got_error *err = NULL;
1662 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1663 char *logmsg0 = NULL, *logmsg = NULL;
1664 char *author = NULL;
1665 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1666 int author_width, logmsg_width;
1667 char *newline, *line = NULL;
1668 int col, limit, scrollx;
1669 const int avail = view->ncols;
1670 struct tm tm;
1671 time_t committer_time;
1672 struct tog_color *tc;
1674 committer_time = got_object_commit_get_committer_time(commit);
1675 if (gmtime_r(&committer_time, &tm) == NULL)
1676 return got_error_from_errno("gmtime_r");
1677 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1678 return got_error(GOT_ERR_NO_SPACE);
1680 if (avail <= date_display_cols)
1681 limit = MIN(sizeof(datebuf) - 1, avail);
1682 else
1683 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1684 tc = get_color(&s->colors, TOG_COLOR_DATE);
1685 if (tc)
1686 wattr_on(view->window,
1687 COLOR_PAIR(tc->colorpair), NULL);
1688 waddnstr(view->window, datebuf, limit);
1689 if (tc)
1690 wattr_off(view->window,
1691 COLOR_PAIR(tc->colorpair), NULL);
1692 col = limit;
1693 if (col > avail)
1694 goto done;
1696 if (avail >= 120) {
1697 char *id_str;
1698 err = got_object_id_str(&id_str, id);
1699 if (err)
1700 goto done;
1701 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1702 if (tc)
1703 wattr_on(view->window,
1704 COLOR_PAIR(tc->colorpair), NULL);
1705 wprintw(view->window, "%.8s ", id_str);
1706 if (tc)
1707 wattr_off(view->window,
1708 COLOR_PAIR(tc->colorpair), NULL);
1709 free(id_str);
1710 col += 9;
1711 if (col > avail)
1712 goto done;
1715 author = strdup(got_object_commit_get_author(commit));
1716 if (author == NULL) {
1717 err = got_error_from_errno("strdup");
1718 goto done;
1720 err = format_author(&wauthor, &author_width, author, avail - col, col);
1721 if (err)
1722 goto done;
1723 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1724 if (tc)
1725 wattr_on(view->window,
1726 COLOR_PAIR(tc->colorpair), NULL);
1727 waddwstr(view->window, wauthor);
1728 if (tc)
1729 wattr_off(view->window,
1730 COLOR_PAIR(tc->colorpair), NULL);
1731 col += author_width;
1732 while (col < avail && author_width < author_display_cols + 2) {
1733 waddch(view->window, ' ');
1734 col++;
1735 author_width++;
1737 if (col > avail)
1738 goto done;
1740 err = got_object_commit_get_logmsg(&logmsg0, commit);
1741 if (err)
1742 goto done;
1743 logmsg = logmsg0;
1744 while (*logmsg == '\n')
1745 logmsg++;
1746 newline = strchr(logmsg, '\n');
1747 if (newline)
1748 *newline = '\0';
1749 limit = avail - col;
1750 if (view->child && view_is_splitscreen(view->child) && limit > 0)
1751 limit--; /* for the border */
1752 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1753 limit, col, 1);
1754 if (err)
1755 goto done;
1756 waddwstr(view->window, &wlogmsg[scrollx]);
1757 col += MAX(logmsg_width, 0);
1758 while (col < avail) {
1759 waddch(view->window, ' ');
1760 col++;
1762 done:
1763 free(logmsg0);
1764 free(wlogmsg);
1765 free(author);
1766 free(wauthor);
1767 free(line);
1768 return err;
1771 static struct commit_queue_entry *
1772 alloc_commit_queue_entry(struct got_commit_object *commit,
1773 struct got_object_id *id)
1775 struct commit_queue_entry *entry;
1777 entry = calloc(1, sizeof(*entry));
1778 if (entry == NULL)
1779 return NULL;
1781 entry->id = id;
1782 entry->commit = commit;
1783 return entry;
1786 static void
1787 pop_commit(struct commit_queue *commits)
1789 struct commit_queue_entry *entry;
1791 entry = TAILQ_FIRST(&commits->head);
1792 TAILQ_REMOVE(&commits->head, entry, entry);
1793 got_object_commit_close(entry->commit);
1794 commits->ncommits--;
1795 /* Don't free entry->id! It is owned by the commit graph. */
1796 free(entry);
1799 static void
1800 free_commits(struct commit_queue *commits)
1802 while (!TAILQ_EMPTY(&commits->head))
1803 pop_commit(commits);
1806 static const struct got_error *
1807 match_commit(int *have_match, struct got_object_id *id,
1808 struct got_commit_object *commit, regex_t *regex)
1810 const struct got_error *err = NULL;
1811 regmatch_t regmatch;
1812 char *id_str = NULL, *logmsg = NULL;
1814 *have_match = 0;
1816 err = got_object_id_str(&id_str, id);
1817 if (err)
1818 return err;
1820 err = got_object_commit_get_logmsg(&logmsg, commit);
1821 if (err)
1822 goto done;
1824 if (regexec(regex, got_object_commit_get_author(commit), 1,
1825 &regmatch, 0) == 0 ||
1826 regexec(regex, got_object_commit_get_committer(commit), 1,
1827 &regmatch, 0) == 0 ||
1828 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1829 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1830 *have_match = 1;
1831 done:
1832 free(id_str);
1833 free(logmsg);
1834 return err;
1837 static const struct got_error *
1838 queue_commits(struct tog_log_thread_args *a)
1840 const struct got_error *err = NULL;
1843 * We keep all commits open throughout the lifetime of the log
1844 * view in order to avoid having to re-fetch commits from disk
1845 * while updating the display.
1847 do {
1848 struct got_object_id *id;
1849 struct got_commit_object *commit;
1850 struct commit_queue_entry *entry;
1851 int errcode;
1853 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1854 NULL, NULL);
1855 if (err || id == NULL)
1856 break;
1858 err = got_object_open_as_commit(&commit, a->repo, id);
1859 if (err)
1860 break;
1861 entry = alloc_commit_queue_entry(commit, id);
1862 if (entry == NULL) {
1863 err = got_error_from_errno("alloc_commit_queue_entry");
1864 break;
1867 errcode = pthread_mutex_lock(&tog_mutex);
1868 if (errcode) {
1869 err = got_error_set_errno(errcode,
1870 "pthread_mutex_lock");
1871 break;
1874 entry->idx = a->commits->ncommits;
1875 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1876 a->commits->ncommits++;
1878 if (*a->searching == TOG_SEARCH_FORWARD &&
1879 !*a->search_next_done) {
1880 int have_match;
1881 err = match_commit(&have_match, id, commit, a->regex);
1882 if (err)
1883 break;
1884 if (have_match)
1885 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1888 errcode = pthread_mutex_unlock(&tog_mutex);
1889 if (errcode && err == NULL)
1890 err = got_error_set_errno(errcode,
1891 "pthread_mutex_unlock");
1892 if (err)
1893 break;
1894 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1896 return err;
1899 static void
1900 select_commit(struct tog_log_view_state *s)
1902 struct commit_queue_entry *entry;
1903 int ncommits = 0;
1905 entry = s->first_displayed_entry;
1906 while (entry) {
1907 if (ncommits == s->selected) {
1908 s->selected_entry = entry;
1909 break;
1911 entry = TAILQ_NEXT(entry, entry);
1912 ncommits++;
1916 static const struct got_error *
1917 draw_commits(struct tog_view *view)
1919 const struct got_error *err = NULL;
1920 struct tog_log_view_state *s = &view->state.log;
1921 struct commit_queue_entry *entry = s->selected_entry;
1922 const int limit = view->nlines;
1923 int width;
1924 int ncommits, author_cols = 4;
1925 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1926 char *refs_str = NULL;
1927 wchar_t *wline;
1928 struct tog_color *tc;
1929 static const size_t date_display_cols = 12;
1931 if (s->selected_entry &&
1932 !(view->searching && view->search_next_done == 0)) {
1933 struct got_reflist_head *refs;
1934 err = got_object_id_str(&id_str, s->selected_entry->id);
1935 if (err)
1936 return err;
1937 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1938 s->selected_entry->id);
1939 if (refs) {
1940 err = build_refs_str(&refs_str, refs,
1941 s->selected_entry->id, s->repo);
1942 if (err)
1943 goto done;
1947 if (s->thread_args.commits_needed == 0)
1948 halfdelay(10); /* disable fast refresh */
1950 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1951 if (asprintf(&ncommits_str, " [%d/%d] %s",
1952 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1953 (view->searching && !view->search_next_done) ?
1954 "searching..." : "loading...") == -1) {
1955 err = got_error_from_errno("asprintf");
1956 goto done;
1958 } else {
1959 const char *search_str = NULL;
1961 if (view->searching) {
1962 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1963 search_str = "no more matches";
1964 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1965 search_str = "no matches found";
1966 else if (!view->search_next_done)
1967 search_str = "searching...";
1970 if (asprintf(&ncommits_str, " [%d/%d] %s",
1971 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1972 search_str ? search_str :
1973 (refs_str ? refs_str : "")) == -1) {
1974 err = got_error_from_errno("asprintf");
1975 goto done;
1979 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1980 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1981 "........................................",
1982 s->in_repo_path, ncommits_str) == -1) {
1983 err = got_error_from_errno("asprintf");
1984 header = NULL;
1985 goto done;
1987 } else if (asprintf(&header, "commit %s%s",
1988 id_str ? id_str : "........................................",
1989 ncommits_str) == -1) {
1990 err = got_error_from_errno("asprintf");
1991 header = NULL;
1992 goto done;
1994 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1995 if (err)
1996 goto done;
1998 werase(view->window);
2000 if (view_needs_focus_indication(view))
2001 wstandout(view->window);
2002 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2003 if (tc)
2004 wattr_on(view->window,
2005 COLOR_PAIR(tc->colorpair), NULL);
2006 waddwstr(view->window, wline);
2007 if (tc)
2008 wattr_off(view->window,
2009 COLOR_PAIR(tc->colorpair), NULL);
2010 while (width < view->ncols) {
2011 waddch(view->window, ' ');
2012 width++;
2014 if (view_needs_focus_indication(view))
2015 wstandend(view->window);
2016 free(wline);
2017 if (limit <= 1)
2018 goto done;
2020 /* Grow author column size if necessary, and set view->maxx. */
2021 entry = s->first_displayed_entry;
2022 ncommits = 0;
2023 view->maxx = 0;
2024 while (entry) {
2025 char *author, *eol, *msg, *msg0;
2026 wchar_t *wauthor, *wmsg;
2027 int width;
2028 if (ncommits >= limit - 1)
2029 break;
2030 author = strdup(got_object_commit_get_author(entry->commit));
2031 if (author == NULL) {
2032 err = got_error_from_errno("strdup");
2033 goto done;
2035 err = format_author(&wauthor, &width, author, COLS,
2036 date_display_cols);
2037 if (author_cols < width)
2038 author_cols = width;
2039 free(wauthor);
2040 free(author);
2041 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2042 if (err)
2043 goto done;
2044 msg = msg0;
2045 while (*msg == '\n')
2046 ++msg;
2047 if ((eol = strchr(msg, '\n')))
2048 *eol = '\0';
2049 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2050 date_display_cols + author_cols, 0);
2051 if (err)
2052 goto done;
2053 view->maxx = MAX(view->maxx, width);
2054 free(msg0);
2055 free(wmsg);
2056 ncommits++;
2057 entry = TAILQ_NEXT(entry, entry);
2060 entry = s->first_displayed_entry;
2061 s->last_displayed_entry = s->first_displayed_entry;
2062 ncommits = 0;
2063 while (entry) {
2064 if (ncommits >= limit - 1)
2065 break;
2066 if (ncommits == s->selected)
2067 wstandout(view->window);
2068 err = draw_commit(view, entry->commit, entry->id,
2069 date_display_cols, author_cols);
2070 if (ncommits == s->selected)
2071 wstandend(view->window);
2072 if (err)
2073 goto done;
2074 ncommits++;
2075 s->last_displayed_entry = entry;
2076 entry = TAILQ_NEXT(entry, entry);
2079 view_border(view);
2080 done:
2081 free(id_str);
2082 free(refs_str);
2083 free(ncommits_str);
2084 free(header);
2085 return err;
2088 static void
2089 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2091 struct commit_queue_entry *entry;
2092 int nscrolled = 0;
2094 entry = TAILQ_FIRST(&s->commits.head);
2095 if (s->first_displayed_entry == entry)
2096 return;
2098 entry = s->first_displayed_entry;
2099 while (entry && nscrolled < maxscroll) {
2100 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2101 if (entry) {
2102 s->first_displayed_entry = entry;
2103 nscrolled++;
2108 static const struct got_error *
2109 trigger_log_thread(struct tog_view *view, int wait)
2111 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2112 int errcode;
2114 halfdelay(1); /* fast refresh while loading commits */
2116 while (ta->commits_needed > 0 || ta->load_all) {
2117 if (ta->log_complete)
2118 break;
2120 /* Wake the log thread. */
2121 errcode = pthread_cond_signal(&ta->need_commits);
2122 if (errcode)
2123 return got_error_set_errno(errcode,
2124 "pthread_cond_signal");
2127 * The mutex will be released while the view loop waits
2128 * in wgetch(), at which time the log thread will run.
2130 if (!wait)
2131 break;
2133 /* Display progress update in log view. */
2134 show_log_view(view);
2135 update_panels();
2136 doupdate();
2138 /* Wait right here while next commit is being loaded. */
2139 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2140 if (errcode)
2141 return got_error_set_errno(errcode,
2142 "pthread_cond_wait");
2144 /* Display progress update in log view. */
2145 show_log_view(view);
2146 update_panels();
2147 doupdate();
2150 return NULL;
2153 static const struct got_error *
2154 request_log_commits(struct tog_view *view)
2156 struct tog_log_view_state *state = &view->state.log;
2157 const struct got_error *err = NULL;
2159 state->thread_args.commits_needed = view->nscrolled;
2160 err = trigger_log_thread(view, 1);
2161 view->nscrolled = 0;
2163 return err;
2166 static const struct got_error *
2167 log_scroll_down(struct tog_view *view, int maxscroll)
2169 struct tog_log_view_state *s = &view->state.log;
2170 const struct got_error *err = NULL;
2171 struct commit_queue_entry *pentry;
2172 int nscrolled = 0, ncommits_needed;
2174 if (s->last_displayed_entry == NULL)
2175 return NULL;
2177 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2178 if (s->commits.ncommits < ncommits_needed &&
2179 !s->thread_args.log_complete) {
2181 * Ask the log thread for required amount of commits.
2183 s->thread_args.commits_needed += maxscroll;
2184 err = trigger_log_thread(view, 1);
2185 if (err)
2186 return err;
2189 do {
2190 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2191 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2192 break;
2194 s->last_displayed_entry = pentry ?
2195 pentry : s->last_displayed_entry;;
2197 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2198 if (pentry == NULL)
2199 break;
2200 s->first_displayed_entry = pentry;
2201 } while (++nscrolled < maxscroll);
2203 if (view->mode == TOG_VIEW_SPLIT_HRZN)
2204 view->nscrolled += nscrolled;
2205 else
2206 view->nscrolled = 0;
2208 return err;
2211 static const struct got_error *
2212 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2213 struct got_commit_object *commit, struct got_object_id *commit_id,
2214 struct tog_view *log_view, struct got_repository *repo)
2216 const struct got_error *err;
2217 struct got_object_qid *parent_id;
2218 struct tog_view *diff_view;
2220 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2221 if (diff_view == NULL)
2222 return got_error_from_errno("view_open");
2224 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2225 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2226 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2227 if (err == NULL)
2228 *new_view = diff_view;
2229 return err;
2232 static const struct got_error *
2233 tree_view_visit_subtree(struct tog_tree_view_state *s,
2234 struct got_tree_object *subtree)
2236 struct tog_parent_tree *parent;
2238 parent = calloc(1, sizeof(*parent));
2239 if (parent == NULL)
2240 return got_error_from_errno("calloc");
2242 parent->tree = s->tree;
2243 parent->first_displayed_entry = s->first_displayed_entry;
2244 parent->selected_entry = s->selected_entry;
2245 parent->selected = s->selected;
2246 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2247 s->tree = subtree;
2248 s->selected = 0;
2249 s->first_displayed_entry = NULL;
2250 return NULL;
2253 static const struct got_error *
2254 tree_view_walk_path(struct tog_tree_view_state *s,
2255 struct got_commit_object *commit, const char *path)
2257 const struct got_error *err = NULL;
2258 struct got_tree_object *tree = NULL;
2259 const char *p;
2260 char *slash, *subpath = NULL;
2262 /* Walk the path and open corresponding tree objects. */
2263 p = path;
2264 while (*p) {
2265 struct got_tree_entry *te;
2266 struct got_object_id *tree_id;
2267 char *te_name;
2269 while (p[0] == '/')
2270 p++;
2272 /* Ensure the correct subtree entry is selected. */
2273 slash = strchr(p, '/');
2274 if (slash == NULL)
2275 te_name = strdup(p);
2276 else
2277 te_name = strndup(p, slash - p);
2278 if (te_name == NULL) {
2279 err = got_error_from_errno("strndup");
2280 break;
2282 te = got_object_tree_find_entry(s->tree, te_name);
2283 if (te == NULL) {
2284 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2285 free(te_name);
2286 break;
2288 free(te_name);
2289 s->first_displayed_entry = s->selected_entry = te;
2291 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2292 break; /* jump to this file's entry */
2294 slash = strchr(p, '/');
2295 if (slash)
2296 subpath = strndup(path, slash - path);
2297 else
2298 subpath = strdup(path);
2299 if (subpath == NULL) {
2300 err = got_error_from_errno("strdup");
2301 break;
2304 err = got_object_id_by_path(&tree_id, s->repo, commit,
2305 subpath);
2306 if (err)
2307 break;
2309 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2310 free(tree_id);
2311 if (err)
2312 break;
2314 err = tree_view_visit_subtree(s, tree);
2315 if (err) {
2316 got_object_tree_close(tree);
2317 break;
2319 if (slash == NULL)
2320 break;
2321 free(subpath);
2322 subpath = NULL;
2323 p = slash;
2326 free(subpath);
2327 return err;
2330 static const struct got_error *
2331 browse_commit_tree(struct tog_view **new_view, int begin_x,
2332 struct commit_queue_entry *entry, const char *path,
2333 const char *head_ref_name, struct got_repository *repo)
2335 const struct got_error *err = NULL;
2336 struct tog_tree_view_state *s;
2337 struct tog_view *tree_view;
2339 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2340 if (tree_view == NULL)
2341 return got_error_from_errno("view_open");
2343 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2344 if (err)
2345 return err;
2346 s = &tree_view->state.tree;
2348 *new_view = tree_view;
2350 if (got_path_is_root_dir(path))
2351 return NULL;
2353 return tree_view_walk_path(s, entry->commit, path);
2356 static const struct got_error *
2357 block_signals_used_by_main_thread(void)
2359 sigset_t sigset;
2360 int errcode;
2362 if (sigemptyset(&sigset) == -1)
2363 return got_error_from_errno("sigemptyset");
2365 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2366 if (sigaddset(&sigset, SIGWINCH) == -1)
2367 return got_error_from_errno("sigaddset");
2368 if (sigaddset(&sigset, SIGCONT) == -1)
2369 return got_error_from_errno("sigaddset");
2370 if (sigaddset(&sigset, SIGINT) == -1)
2371 return got_error_from_errno("sigaddset");
2372 if (sigaddset(&sigset, SIGTERM) == -1)
2373 return got_error_from_errno("sigaddset");
2375 /* ncurses handles SIGTSTP */
2376 if (sigaddset(&sigset, SIGTSTP) == -1)
2377 return got_error_from_errno("sigaddset");
2379 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2380 if (errcode)
2381 return got_error_set_errno(errcode, "pthread_sigmask");
2383 return NULL;
2386 static void *
2387 log_thread(void *arg)
2389 const struct got_error *err = NULL;
2390 int errcode = 0;
2391 struct tog_log_thread_args *a = arg;
2392 int done = 0;
2394 err = block_signals_used_by_main_thread();
2395 if (err)
2396 return (void *)err;
2398 while (!done && !err && !tog_fatal_signal_received()) {
2399 err = queue_commits(a);
2400 if (err) {
2401 if (err->code != GOT_ERR_ITER_COMPLETED)
2402 return (void *)err;
2403 err = NULL;
2404 done = 1;
2405 } else if (a->commits_needed > 0 && !a->load_all)
2406 a->commits_needed--;
2408 errcode = pthread_mutex_lock(&tog_mutex);
2409 if (errcode) {
2410 err = got_error_set_errno(errcode,
2411 "pthread_mutex_lock");
2412 break;
2413 } else if (*a->quit)
2414 done = 1;
2415 else if (*a->first_displayed_entry == NULL) {
2416 *a->first_displayed_entry =
2417 TAILQ_FIRST(&a->commits->head);
2418 *a->selected_entry = *a->first_displayed_entry;
2421 errcode = pthread_cond_signal(&a->commit_loaded);
2422 if (errcode) {
2423 err = got_error_set_errno(errcode,
2424 "pthread_cond_signal");
2425 pthread_mutex_unlock(&tog_mutex);
2426 break;
2429 if (done)
2430 a->commits_needed = 0;
2431 else {
2432 if (a->commits_needed == 0 && !a->load_all) {
2433 errcode = pthread_cond_wait(&a->need_commits,
2434 &tog_mutex);
2435 if (errcode)
2436 err = got_error_set_errno(errcode,
2437 "pthread_cond_wait");
2438 if (*a->quit)
2439 done = 1;
2443 errcode = pthread_mutex_unlock(&tog_mutex);
2444 if (errcode && err == NULL)
2445 err = got_error_set_errno(errcode,
2446 "pthread_mutex_unlock");
2448 a->log_complete = 1;
2449 return (void *)err;
2452 static const struct got_error *
2453 stop_log_thread(struct tog_log_view_state *s)
2455 const struct got_error *err = NULL;
2456 int errcode;
2458 if (s->thread) {
2459 s->quit = 1;
2460 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2461 if (errcode)
2462 return got_error_set_errno(errcode,
2463 "pthread_cond_signal");
2464 errcode = pthread_mutex_unlock(&tog_mutex);
2465 if (errcode)
2466 return got_error_set_errno(errcode,
2467 "pthread_mutex_unlock");
2468 errcode = pthread_join(s->thread, (void **)&err);
2469 if (errcode)
2470 return got_error_set_errno(errcode, "pthread_join");
2471 errcode = pthread_mutex_lock(&tog_mutex);
2472 if (errcode)
2473 return got_error_set_errno(errcode,
2474 "pthread_mutex_lock");
2475 s->thread = NULL;
2478 if (s->thread_args.repo) {
2479 err = got_repo_close(s->thread_args.repo);
2480 s->thread_args.repo = NULL;
2483 if (s->thread_args.pack_fds) {
2484 const struct got_error *pack_err =
2485 got_repo_pack_fds_close(s->thread_args.pack_fds);
2486 if (err == NULL)
2487 err = pack_err;
2488 s->thread_args.pack_fds = NULL;
2491 if (s->thread_args.graph) {
2492 got_commit_graph_close(s->thread_args.graph);
2493 s->thread_args.graph = NULL;
2496 return err;
2499 static const struct got_error *
2500 close_log_view(struct tog_view *view)
2502 const struct got_error *err = NULL;
2503 struct tog_log_view_state *s = &view->state.log;
2504 int errcode;
2506 err = stop_log_thread(s);
2508 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2509 if (errcode && err == NULL)
2510 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2512 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2513 if (errcode && err == NULL)
2514 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2516 free_commits(&s->commits);
2517 free(s->in_repo_path);
2518 s->in_repo_path = NULL;
2519 free(s->start_id);
2520 s->start_id = NULL;
2521 free(s->head_ref_name);
2522 s->head_ref_name = NULL;
2523 return err;
2526 static const struct got_error *
2527 search_start_log_view(struct tog_view *view)
2529 struct tog_log_view_state *s = &view->state.log;
2531 s->matched_entry = NULL;
2532 s->search_entry = NULL;
2533 return NULL;
2536 static const struct got_error *
2537 search_next_log_view(struct tog_view *view)
2539 const struct got_error *err = NULL;
2540 struct tog_log_view_state *s = &view->state.log;
2541 struct commit_queue_entry *entry;
2543 /* Display progress update in log view. */
2544 show_log_view(view);
2545 update_panels();
2546 doupdate();
2548 if (s->search_entry) {
2549 int errcode, ch;
2550 errcode = pthread_mutex_unlock(&tog_mutex);
2551 if (errcode)
2552 return got_error_set_errno(errcode,
2553 "pthread_mutex_unlock");
2554 ch = wgetch(view->window);
2555 errcode = pthread_mutex_lock(&tog_mutex);
2556 if (errcode)
2557 return got_error_set_errno(errcode,
2558 "pthread_mutex_lock");
2559 if (ch == KEY_BACKSPACE) {
2560 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2561 return NULL;
2563 if (view->searching == TOG_SEARCH_FORWARD)
2564 entry = TAILQ_NEXT(s->search_entry, entry);
2565 else
2566 entry = TAILQ_PREV(s->search_entry,
2567 commit_queue_head, entry);
2568 } else if (s->matched_entry) {
2569 int matched_idx = s->matched_entry->idx;
2570 int selected_idx = s->selected_entry->idx;
2573 * If the user has moved the cursor after we hit a match,
2574 * the position from where we should continue searching
2575 * might have changed.
2577 if (view->searching == TOG_SEARCH_FORWARD) {
2578 if (matched_idx > selected_idx)
2579 entry = TAILQ_NEXT(s->selected_entry, entry);
2580 else
2581 entry = TAILQ_NEXT(s->matched_entry, entry);
2582 } else {
2583 if (matched_idx < selected_idx)
2584 entry = TAILQ_PREV(s->selected_entry,
2585 commit_queue_head, entry);
2586 else
2587 entry = TAILQ_PREV(s->matched_entry,
2588 commit_queue_head, entry);
2590 } else {
2591 entry = s->selected_entry;
2594 while (1) {
2595 int have_match = 0;
2597 if (entry == NULL) {
2598 if (s->thread_args.log_complete ||
2599 view->searching == TOG_SEARCH_BACKWARD) {
2600 view->search_next_done =
2601 (s->matched_entry == NULL ?
2602 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2603 s->search_entry = NULL;
2604 return NULL;
2607 * Poke the log thread for more commits and return,
2608 * allowing the main loop to make progress. Search
2609 * will resume at s->search_entry once we come back.
2611 s->thread_args.commits_needed++;
2612 return trigger_log_thread(view, 0);
2615 err = match_commit(&have_match, entry->id, entry->commit,
2616 &view->regex);
2617 if (err)
2618 break;
2619 if (have_match) {
2620 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2621 s->matched_entry = entry;
2622 break;
2625 s->search_entry = entry;
2626 if (view->searching == TOG_SEARCH_FORWARD)
2627 entry = TAILQ_NEXT(entry, entry);
2628 else
2629 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2632 if (s->matched_entry) {
2633 int cur = s->selected_entry->idx;
2634 while (cur < s->matched_entry->idx) {
2635 err = input_log_view(NULL, view, KEY_DOWN);
2636 if (err)
2637 return err;
2638 cur++;
2640 while (cur > s->matched_entry->idx) {
2641 err = input_log_view(NULL, view, KEY_UP);
2642 if (err)
2643 return err;
2644 cur--;
2648 s->search_entry = NULL;
2650 return NULL;
2653 static const struct got_error *
2654 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2655 struct got_repository *repo, const char *head_ref_name,
2656 const char *in_repo_path, int log_branches)
2658 const struct got_error *err = NULL;
2659 struct tog_log_view_state *s = &view->state.log;
2660 struct got_repository *thread_repo = NULL;
2661 struct got_commit_graph *thread_graph = NULL;
2662 int errcode;
2664 if (in_repo_path != s->in_repo_path) {
2665 free(s->in_repo_path);
2666 s->in_repo_path = strdup(in_repo_path);
2667 if (s->in_repo_path == NULL)
2668 return got_error_from_errno("strdup");
2671 /* The commit queue only contains commits being displayed. */
2672 TAILQ_INIT(&s->commits.head);
2673 s->commits.ncommits = 0;
2675 s->repo = repo;
2676 if (head_ref_name) {
2677 s->head_ref_name = strdup(head_ref_name);
2678 if (s->head_ref_name == NULL) {
2679 err = got_error_from_errno("strdup");
2680 goto done;
2683 s->start_id = got_object_id_dup(start_id);
2684 if (s->start_id == NULL) {
2685 err = got_error_from_errno("got_object_id_dup");
2686 goto done;
2688 s->log_branches = log_branches;
2690 STAILQ_INIT(&s->colors);
2691 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2692 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2693 get_color_value("TOG_COLOR_COMMIT"));
2694 if (err)
2695 goto done;
2696 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2697 get_color_value("TOG_COLOR_AUTHOR"));
2698 if (err) {
2699 free_colors(&s->colors);
2700 goto done;
2702 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2703 get_color_value("TOG_COLOR_DATE"));
2704 if (err) {
2705 free_colors(&s->colors);
2706 goto done;
2710 view->show = show_log_view;
2711 view->input = input_log_view;
2712 view->close = close_log_view;
2713 view->search_start = search_start_log_view;
2714 view->search_next = search_next_log_view;
2716 if (s->thread_args.pack_fds == NULL) {
2717 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2718 if (err)
2719 goto done;
2721 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2722 s->thread_args.pack_fds);
2723 if (err)
2724 goto done;
2725 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2726 !s->log_branches);
2727 if (err)
2728 goto done;
2729 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2730 s->repo, NULL, NULL);
2731 if (err)
2732 goto done;
2734 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2735 if (errcode) {
2736 err = got_error_set_errno(errcode, "pthread_cond_init");
2737 goto done;
2739 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2740 if (errcode) {
2741 err = got_error_set_errno(errcode, "pthread_cond_init");
2742 goto done;
2745 s->thread_args.commits_needed = view->nlines;
2746 s->thread_args.graph = thread_graph;
2747 s->thread_args.commits = &s->commits;
2748 s->thread_args.in_repo_path = s->in_repo_path;
2749 s->thread_args.start_id = s->start_id;
2750 s->thread_args.repo = thread_repo;
2751 s->thread_args.log_complete = 0;
2752 s->thread_args.quit = &s->quit;
2753 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2754 s->thread_args.selected_entry = &s->selected_entry;
2755 s->thread_args.searching = &view->searching;
2756 s->thread_args.search_next_done = &view->search_next_done;
2757 s->thread_args.regex = &view->regex;
2758 done:
2759 if (err)
2760 close_log_view(view);
2761 return err;
2764 static const struct got_error *
2765 show_log_view(struct tog_view *view)
2767 const struct got_error *err;
2768 struct tog_log_view_state *s = &view->state.log;
2770 if (s->thread == NULL) {
2771 int errcode = pthread_create(&s->thread, NULL, log_thread,
2772 &s->thread_args);
2773 if (errcode)
2774 return got_error_set_errno(errcode, "pthread_create");
2775 if (s->thread_args.commits_needed > 0) {
2776 err = trigger_log_thread(view, 1);
2777 if (err)
2778 return err;
2782 return draw_commits(view);
2785 static void
2786 log_move_cursor_up(struct tog_view *view, int page, int home)
2788 struct tog_log_view_state *s = &view->state.log;
2790 if (s->selected_entry->idx == 0)
2791 view->count = 0;
2792 if (s->first_displayed_entry == NULL)
2793 return;
2795 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2796 || home)
2797 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2799 if (!page && !home && s->selected > 0)
2800 --s->selected;
2801 else
2802 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2804 select_commit(s);
2805 return;
2808 static const struct got_error *
2809 log_move_cursor_down(struct tog_view *view, int page)
2811 struct tog_log_view_state *s = &view->state.log;
2812 struct commit_queue_entry *first;
2813 const struct got_error *err = NULL;
2815 first = s->first_displayed_entry;
2816 if (first == NULL) {
2817 view->count = 0;
2818 return NULL;
2821 if (s->thread_args.log_complete &&
2822 s->selected_entry->idx >= s->commits.ncommits - 1)
2823 return NULL;
2825 if (!page) {
2826 int eos = view->nlines - 2;
2828 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2829 view_is_splitscreen(view->child))
2830 --eos; /* border consumes the last line */
2831 if (s->selected < MIN(eos, s->commits.ncommits - 1))
2832 ++s->selected;
2833 else
2834 err = log_scroll_down(view, 1);
2835 } else if (s->thread_args.load_all) {
2836 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2837 s->selected += MIN(s->last_displayed_entry->idx -
2838 s->selected_entry->idx, page + 1);
2839 else
2840 err = log_scroll_down(view, MIN(page,
2841 s->commits.ncommits - s->selected_entry->idx - 1));
2842 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2843 } else {
2844 err = log_scroll_down(view, page);
2845 if (err)
2846 return err;
2847 if (first == s->first_displayed_entry && s->selected <
2848 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2849 s->selected = MIN(s->commits.ncommits - 1, page);
2852 if (err)
2853 return err;
2856 * We might necessarily overshoot in horizontal
2857 * splits; if so, select the last displayed commit.
2859 s->selected = MIN(s->selected,
2860 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2862 select_commit(s);
2864 if (s->thread_args.log_complete &&
2865 s->selected_entry->idx == s->commits.ncommits - 1)
2866 view->count = 0;
2868 return NULL;
2872 * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2873 * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2874 * TOG_VIEW_SPLIT_HRZN horizontal split
2875 * Assign start column and line of the new split to *x and *y, respectively,
2876 * and assign view mode to view->mode.
2878 static void
2879 view_get_split(struct tog_view *view, int *y, int *x)
2881 char *mode;
2883 *x = 0;
2884 *y = 0;
2886 mode = getenv("TOG_VIEW_SPLIT_MODE");
2888 if (!mode || mode[0] != 'h') {
2889 view->mode = TOG_VIEW_SPLIT_VERT;
2890 *x = view_split_begin_x(view->begin_x);
2891 } else if (mode && mode[0] == 'h') {
2892 view->mode = TOG_VIEW_SPLIT_HRZN;
2893 *y = view_split_begin_y(view->lines);
2897 /* Split view horizontally at y and offset view->state->selected line. */
2898 static const struct got_error *
2899 view_init_hsplit(struct tog_view *view, int y)
2901 const struct got_error *err = NULL;
2903 view->nlines = y;
2904 err = view_resize(view);
2905 if (err)
2906 return err;
2908 err = offset_selection_down(view);
2910 return err;
2913 static const struct got_error *
2914 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2916 const struct got_error *err = NULL;
2917 struct tog_log_view_state *s = &view->state.log;
2918 struct tog_view *diff_view = NULL, *tree_view = NULL;
2919 struct tog_view *ref_view = NULL;
2920 struct commit_queue_entry *entry;
2921 int begin_x = 0, begin_y = 0, eos, n, nscroll;
2923 if (s->thread_args.load_all) {
2924 if (ch == KEY_BACKSPACE)
2925 s->thread_args.load_all = 0;
2926 else if (s->thread_args.log_complete) {
2927 err = log_move_cursor_down(view, s->commits.ncommits);
2928 s->thread_args.load_all = 0;
2930 return err;
2933 eos = nscroll = view->nlines - 1;
2934 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2935 view_is_splitscreen(view->child))
2936 --eos; /* border */
2939 switch (ch) {
2940 case 'q':
2941 s->quit = 1;
2942 break;
2943 case '0':
2944 view->x = 0;
2945 break;
2946 case '$':
2947 view->x = MAX(view->maxx - view->ncols / 2, 0);
2948 view->count = 0;
2949 break;
2950 case KEY_RIGHT:
2951 case 'l':
2952 if (view->x + view->ncols / 2 < view->maxx)
2953 view->x += 2; /* move two columns right */
2954 else
2955 view->count = 0;
2956 break;
2957 case KEY_LEFT:
2958 case 'h':
2959 view->x -= MIN(view->x, 2); /* move two columns back */
2960 if (view->x <= 0)
2961 view->count = 0;
2962 break;
2963 case 'k':
2964 case KEY_UP:
2965 case '<':
2966 case ',':
2967 case CTRL('p'):
2968 log_move_cursor_up(view, 0, 0);
2969 break;
2970 case 'g':
2971 case KEY_HOME:
2972 log_move_cursor_up(view, 0, 1);
2973 view->count = 0;
2974 break;
2975 case CTRL('u'):
2976 case 'u':
2977 nscroll /= 2;
2978 /* FALL THROUGH */
2979 case KEY_PPAGE:
2980 case CTRL('b'):
2981 case 'b':
2982 log_move_cursor_up(view, nscroll, 0);
2983 break;
2984 case 'j':
2985 case KEY_DOWN:
2986 case '>':
2987 case '.':
2988 case CTRL('n'):
2989 err = log_move_cursor_down(view, 0);
2990 break;
2991 case 'G':
2992 case KEY_END: {
2993 /* We don't know yet how many commits, so we're forced to
2994 * traverse them all. */
2995 view->count = 0;
2996 if (!s->thread_args.log_complete) {
2997 s->thread_args.load_all = 1;
2998 return trigger_log_thread(view, 0);
3001 s->selected = 0;
3002 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3003 for (n = 0; n < eos; n++) {
3004 if (entry == NULL)
3005 break;
3006 s->first_displayed_entry = entry;
3007 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3009 if (n > 0)
3010 s->selected = n - 1;
3011 select_commit(s);
3012 break;
3014 case CTRL('d'):
3015 case 'd':
3016 nscroll /= 2;
3017 /* FALL THROUGH */
3018 case KEY_NPAGE:
3019 case CTRL('f'):
3020 case 'f':
3021 case ' ':
3022 err = log_move_cursor_down(view, nscroll);
3023 break;
3024 case KEY_RESIZE:
3025 if (s->selected > view->nlines - 2)
3026 s->selected = view->nlines - 2;
3027 if (s->selected > s->commits.ncommits - 1)
3028 s->selected = s->commits.ncommits - 1;
3029 select_commit(s);
3030 if (s->commits.ncommits < view->nlines - 1 &&
3031 !s->thread_args.log_complete) {
3032 s->thread_args.commits_needed += (view->nlines - 1) -
3033 s->commits.ncommits;
3034 err = trigger_log_thread(view, 1);
3036 break;
3037 case KEY_ENTER:
3038 case '\r': {
3039 view->count = 0;
3040 if (s->selected_entry == NULL)
3041 break;
3043 /* get dimensions--don't split till initialisation succeeds */
3044 if (view_is_parent_view(view))
3045 view_get_split(view, &begin_y, &begin_x);
3047 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3048 s->selected_entry->commit, s->selected_entry->id,
3049 view, s->repo);
3050 if (err)
3051 break;
3053 if (view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3054 err = view_init_hsplit(view, begin_y);
3055 if (err)
3056 break;
3059 view->focussed = 0;
3060 diff_view->focussed = 1;
3061 diff_view->mode = view->mode;
3062 diff_view->nlines = view->lines - begin_y;
3064 if (view_is_parent_view(view)) {
3065 err = view_close_child(view);
3066 if (err)
3067 return err;
3068 err = view_set_child(view, diff_view);
3069 if (err)
3070 return err;
3071 view->focus_child = 1;
3072 } else
3073 *new_view = diff_view;
3074 break;
3076 case 't':
3077 view->count = 0;
3078 if (s->selected_entry == NULL)
3079 break;
3080 if (view_is_parent_view(view))
3081 begin_x = view_split_begin_x(view->begin_x);
3082 err = browse_commit_tree(&tree_view, begin_x,
3083 s->selected_entry, s->in_repo_path, s->head_ref_name,
3084 s->repo);
3085 if (err)
3086 break;
3087 view->focussed = 0;
3088 tree_view->focussed = 1;
3089 if (view_is_parent_view(view)) {
3090 err = view_close_child(view);
3091 if (err)
3092 return err;
3093 err = view_set_child(view, tree_view);
3094 if (err)
3095 return err;
3096 view->focus_child = 1;
3097 } else
3098 *new_view = tree_view;
3099 break;
3100 case KEY_BACKSPACE:
3101 case CTRL('l'):
3102 case 'B':
3103 view->count = 0;
3104 if (ch == KEY_BACKSPACE &&
3105 got_path_is_root_dir(s->in_repo_path))
3106 break;
3107 err = stop_log_thread(s);
3108 if (err)
3109 return err;
3110 if (ch == KEY_BACKSPACE) {
3111 char *parent_path;
3112 err = got_path_dirname(&parent_path, s->in_repo_path);
3113 if (err)
3114 return err;
3115 free(s->in_repo_path);
3116 s->in_repo_path = parent_path;
3117 s->thread_args.in_repo_path = s->in_repo_path;
3118 } else if (ch == CTRL('l')) {
3119 struct got_object_id *start_id;
3120 err = got_repo_match_object_id(&start_id, NULL,
3121 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3122 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3123 if (err)
3124 return err;
3125 free(s->start_id);
3126 s->start_id = start_id;
3127 s->thread_args.start_id = s->start_id;
3128 } else /* 'B' */
3129 s->log_branches = !s->log_branches;
3131 if (s->thread_args.pack_fds == NULL) {
3132 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3133 if (err)
3134 return err;
3136 err = got_repo_open(&s->thread_args.repo,
3137 got_repo_get_path(s->repo), NULL,
3138 s->thread_args.pack_fds);
3139 if (err)
3140 return err;
3141 tog_free_refs();
3142 err = tog_load_refs(s->repo, 0);
3143 if (err)
3144 return err;
3145 err = got_commit_graph_open(&s->thread_args.graph,
3146 s->in_repo_path, !s->log_branches);
3147 if (err)
3148 return err;
3149 err = got_commit_graph_iter_start(s->thread_args.graph,
3150 s->start_id, s->repo, NULL, NULL);
3151 if (err)
3152 return err;
3153 free_commits(&s->commits);
3154 s->first_displayed_entry = NULL;
3155 s->last_displayed_entry = NULL;
3156 s->selected_entry = NULL;
3157 s->selected = 0;
3158 s->thread_args.log_complete = 0;
3159 s->quit = 0;
3160 s->thread_args.commits_needed = view->lines;
3161 s->matched_entry = NULL;
3162 s->search_entry = NULL;
3163 break;
3164 case 'r':
3165 view->count = 0;
3166 if (view_is_parent_view(view))
3167 begin_x = view_split_begin_x(view->begin_x);
3168 ref_view = view_open(view->nlines, view->ncols,
3169 view->begin_y, begin_x, TOG_VIEW_REF);
3170 if (ref_view == NULL)
3171 return got_error_from_errno("view_open");
3172 err = open_ref_view(ref_view, s->repo);
3173 if (err) {
3174 view_close(ref_view);
3175 return err;
3177 view->focussed = 0;
3178 ref_view->focussed = 1;
3179 if (view_is_parent_view(view)) {
3180 err = view_close_child(view);
3181 if (err)
3182 return err;
3183 err = view_set_child(view, ref_view);
3184 if (err)
3185 return err;
3186 view->focus_child = 1;
3187 } else
3188 *new_view = ref_view;
3189 break;
3190 default:
3191 view->count = 0;
3192 break;
3195 return err;
3198 static const struct got_error *
3199 apply_unveil(const char *repo_path, const char *worktree_path)
3201 const struct got_error *error;
3203 #ifdef PROFILE
3204 if (unveil("gmon.out", "rwc") != 0)
3205 return got_error_from_errno2("unveil", "gmon.out");
3206 #endif
3207 if (repo_path && unveil(repo_path, "r") != 0)
3208 return got_error_from_errno2("unveil", repo_path);
3210 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3211 return got_error_from_errno2("unveil", worktree_path);
3213 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3214 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3216 error = got_privsep_unveil_exec_helpers();
3217 if (error != NULL)
3218 return error;
3220 if (unveil(NULL, NULL) != 0)
3221 return got_error_from_errno("unveil");
3223 return NULL;
3226 static void
3227 init_curses(void)
3230 * Override default signal handlers before starting ncurses.
3231 * This should prevent ncurses from installing its own
3232 * broken cleanup() signal handler.
3234 signal(SIGWINCH, tog_sigwinch);
3235 signal(SIGPIPE, tog_sigpipe);
3236 signal(SIGCONT, tog_sigcont);
3237 signal(SIGINT, tog_sigint);
3238 signal(SIGTERM, tog_sigterm);
3240 initscr();
3241 cbreak();
3242 halfdelay(1); /* Do fast refresh while initial view is loading. */
3243 noecho();
3244 nonl();
3245 intrflush(stdscr, FALSE);
3246 keypad(stdscr, TRUE);
3247 curs_set(0);
3248 if (getenv("TOG_COLORS") != NULL) {
3249 start_color();
3250 use_default_colors();
3254 static const struct got_error *
3255 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3256 struct got_repository *repo, struct got_worktree *worktree)
3258 const struct got_error *err = NULL;
3260 if (argc == 0) {
3261 *in_repo_path = strdup("/");
3262 if (*in_repo_path == NULL)
3263 return got_error_from_errno("strdup");
3264 return NULL;
3267 if (worktree) {
3268 const char *prefix = got_worktree_get_path_prefix(worktree);
3269 char *p;
3271 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3272 if (err)
3273 return err;
3274 if (asprintf(in_repo_path, "%s%s%s", prefix,
3275 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3276 p) == -1) {
3277 err = got_error_from_errno("asprintf");
3278 *in_repo_path = NULL;
3280 free(p);
3281 } else
3282 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3284 return err;
3287 static const struct got_error *
3288 cmd_log(int argc, char *argv[])
3290 const struct got_error *error;
3291 struct got_repository *repo = NULL;
3292 struct got_worktree *worktree = NULL;
3293 struct got_object_id *start_id = NULL;
3294 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3295 char *start_commit = NULL, *label = NULL;
3296 struct got_reference *ref = NULL;
3297 const char *head_ref_name = NULL;
3298 int ch, log_branches = 0;
3299 struct tog_view *view;
3300 int *pack_fds = NULL;
3302 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3303 switch (ch) {
3304 case 'b':
3305 log_branches = 1;
3306 break;
3307 case 'c':
3308 start_commit = optarg;
3309 break;
3310 case 'r':
3311 repo_path = realpath(optarg, NULL);
3312 if (repo_path == NULL)
3313 return got_error_from_errno2("realpath",
3314 optarg);
3315 break;
3316 default:
3317 usage_log();
3318 /* NOTREACHED */
3322 argc -= optind;
3323 argv += optind;
3325 if (argc > 1)
3326 usage_log();
3328 error = got_repo_pack_fds_open(&pack_fds);
3329 if (error != NULL)
3330 goto done;
3332 if (repo_path == NULL) {
3333 cwd = getcwd(NULL, 0);
3334 if (cwd == NULL)
3335 return got_error_from_errno("getcwd");
3336 error = got_worktree_open(&worktree, cwd);
3337 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3338 goto done;
3339 if (worktree)
3340 repo_path =
3341 strdup(got_worktree_get_repo_path(worktree));
3342 else
3343 repo_path = strdup(cwd);
3344 if (repo_path == NULL) {
3345 error = got_error_from_errno("strdup");
3346 goto done;
3350 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3351 if (error != NULL)
3352 goto done;
3354 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3355 repo, worktree);
3356 if (error)
3357 goto done;
3359 init_curses();
3361 error = apply_unveil(got_repo_get_path(repo),
3362 worktree ? got_worktree_get_root_path(worktree) : NULL);
3363 if (error)
3364 goto done;
3366 /* already loaded by tog_log_with_path()? */
3367 if (TAILQ_EMPTY(&tog_refs)) {
3368 error = tog_load_refs(repo, 0);
3369 if (error)
3370 goto done;
3373 if (start_commit == NULL) {
3374 error = got_repo_match_object_id(&start_id, &label,
3375 worktree ? got_worktree_get_head_ref_name(worktree) :
3376 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3377 if (error)
3378 goto done;
3379 head_ref_name = label;
3380 } else {
3381 error = got_ref_open(&ref, repo, start_commit, 0);
3382 if (error == NULL)
3383 head_ref_name = got_ref_get_name(ref);
3384 else if (error->code != GOT_ERR_NOT_REF)
3385 goto done;
3386 error = got_repo_match_object_id(&start_id, NULL,
3387 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3388 if (error)
3389 goto done;
3392 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3393 if (view == NULL) {
3394 error = got_error_from_errno("view_open");
3395 goto done;
3397 error = open_log_view(view, start_id, repo, head_ref_name,
3398 in_repo_path, log_branches);
3399 if (error)
3400 goto done;
3401 if (worktree) {
3402 /* Release work tree lock. */
3403 got_worktree_close(worktree);
3404 worktree = NULL;
3406 error = view_loop(view);
3407 done:
3408 free(in_repo_path);
3409 free(repo_path);
3410 free(cwd);
3411 free(start_id);
3412 free(label);
3413 if (ref)
3414 got_ref_close(ref);
3415 if (repo) {
3416 const struct got_error *close_err = got_repo_close(repo);
3417 if (error == NULL)
3418 error = close_err;
3420 if (worktree)
3421 got_worktree_close(worktree);
3422 if (pack_fds) {
3423 const struct got_error *pack_err =
3424 got_repo_pack_fds_close(pack_fds);
3425 if (error == NULL)
3426 error = pack_err;
3428 tog_free_refs();
3429 return error;
3432 __dead static void
3433 usage_diff(void)
3435 endwin();
3436 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3437 "[-w] object1 object2\n", getprogname());
3438 exit(1);
3441 static int
3442 match_line(const char *line, regex_t *regex, size_t nmatch,
3443 regmatch_t *regmatch)
3445 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3448 static struct tog_color *
3449 match_color(struct tog_colors *colors, const char *line)
3451 struct tog_color *tc = NULL;
3453 STAILQ_FOREACH(tc, colors, entry) {
3454 if (match_line(line, &tc->regex, 0, NULL))
3455 return tc;
3458 return NULL;
3461 static const struct got_error *
3462 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3463 WINDOW *window, int skipcol, regmatch_t *regmatch)
3465 const struct got_error *err = NULL;
3466 char *exstr = NULL;
3467 wchar_t *wline = NULL;
3468 int rme, rms, n, width, scrollx;
3469 int width0 = 0, width1 = 0, width2 = 0;
3470 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3472 *wtotal = 0;
3474 rms = regmatch->rm_so;
3475 rme = regmatch->rm_eo;
3477 err = expand_tab(&exstr, line);
3478 if (err)
3479 return err;
3481 /* Split the line into 3 segments, according to match offsets. */
3482 seg0 = strndup(exstr, rms);
3483 if (seg0 == NULL) {
3484 err = got_error_from_errno("strndup");
3485 goto done;
3487 seg1 = strndup(exstr + rms, rme - rms);
3488 if (seg1 == NULL) {
3489 err = got_error_from_errno("strndup");
3490 goto done;
3492 seg2 = strdup(exstr + rme);
3493 if (seg2 == NULL) {
3494 err = got_error_from_errno("strndup");
3495 goto done;
3498 /* draw up to matched token if we haven't scrolled past it */
3499 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3500 col_tab_align, 1);
3501 if (err)
3502 goto done;
3503 n = MAX(width0 - skipcol, 0);
3504 if (n) {
3505 free(wline);
3506 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3507 wlimit, col_tab_align, 1);
3508 if (err)
3509 goto done;
3510 waddwstr(window, &wline[scrollx]);
3511 wlimit -= width;
3512 *wtotal += width;
3515 if (wlimit > 0) {
3516 int i = 0, w = 0;
3517 size_t wlen;
3519 free(wline);
3520 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3521 col_tab_align, 1);
3522 if (err)
3523 goto done;
3524 wlen = wcslen(wline);
3525 while (i < wlen) {
3526 width = wcwidth(wline[i]);
3527 if (width == -1) {
3528 /* should not happen, tabs are expanded */
3529 err = got_error(GOT_ERR_RANGE);
3530 goto done;
3532 if (width0 + w + width > skipcol)
3533 break;
3534 w += width;
3535 i++;
3537 /* draw (visible part of) matched token (if scrolled into it) */
3538 if (width1 - w > 0) {
3539 wattron(window, A_STANDOUT);
3540 waddwstr(window, &wline[i]);
3541 wattroff(window, A_STANDOUT);
3542 wlimit -= (width1 - w);
3543 *wtotal += (width1 - w);
3547 if (wlimit > 0) { /* draw rest of line */
3548 free(wline);
3549 if (skipcol > width0 + width1) {
3550 err = format_line(&wline, &width2, &scrollx, seg2,
3551 skipcol - (width0 + width1), wlimit,
3552 col_tab_align, 1);
3553 if (err)
3554 goto done;
3555 waddwstr(window, &wline[scrollx]);
3556 } else {
3557 err = format_line(&wline, &width2, NULL, seg2, 0,
3558 wlimit, col_tab_align, 1);
3559 if (err)
3560 goto done;
3561 waddwstr(window, wline);
3563 *wtotal += width2;
3565 done:
3566 free(wline);
3567 free(exstr);
3568 free(seg0);
3569 free(seg1);
3570 free(seg2);
3571 return err;
3574 static const struct got_error *
3575 draw_file(struct tog_view *view, const char *header)
3577 struct tog_diff_view_state *s = &view->state.diff;
3578 regmatch_t *regmatch = &view->regmatch;
3579 const struct got_error *err;
3580 int nprinted = 0;
3581 char *line;
3582 size_t linesize = 0;
3583 ssize_t linelen;
3584 struct tog_color *tc;
3585 wchar_t *wline;
3586 int width;
3587 int max_lines = view->nlines;
3588 int nlines = s->nlines;
3589 off_t line_offset;
3591 line_offset = s->line_offsets[s->first_displayed_line - 1];
3592 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3593 return got_error_from_errno("fseek");
3595 werase(view->window);
3597 if (header) {
3598 if (asprintf(&line, "[%d/%d] %s",
3599 s->first_displayed_line - 1 + s->selected_line, nlines,
3600 header) == -1)
3601 return got_error_from_errno("asprintf");
3602 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3603 0, 0);
3604 free(line);
3605 if (err)
3606 return err;
3608 if (view_needs_focus_indication(view))
3609 wstandout(view->window);
3610 waddwstr(view->window, wline);
3611 free(wline);
3612 wline = NULL;
3613 if (view_needs_focus_indication(view))
3614 wstandend(view->window);
3615 if (width <= view->ncols - 1)
3616 waddch(view->window, '\n');
3618 if (max_lines <= 1)
3619 return NULL;
3620 max_lines--;
3623 s->eof = 0;
3624 view->maxx = 0;
3625 line = NULL;
3626 while (max_lines > 0 && nprinted < max_lines) {
3627 linelen = getline(&line, &linesize, s->f);
3628 if (linelen == -1) {
3629 if (feof(s->f)) {
3630 s->eof = 1;
3631 break;
3633 free(line);
3634 return got_ferror(s->f, GOT_ERR_IO);
3637 /* Set view->maxx based on full line length. */
3638 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3639 view->x ? 1 : 0);
3640 if (err) {
3641 free(line);
3642 return err;
3644 view->maxx = MAX(view->maxx, width);
3645 free(wline);
3646 wline = NULL;
3648 tc = match_color(&s->colors, line);
3649 if (tc)
3650 wattr_on(view->window,
3651 COLOR_PAIR(tc->colorpair), NULL);
3652 if (s->first_displayed_line + nprinted == s->matched_line &&
3653 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3654 err = add_matched_line(&width, line, view->ncols, 0,
3655 view->window, view->x, regmatch);
3656 if (err) {
3657 free(line);
3658 return err;
3660 } else {
3661 int skip;
3662 err = format_line(&wline, &width, &skip, line,
3663 view->x, view->ncols, 0, view->x ? 1 : 0);
3664 if (err) {
3665 free(line);
3666 return err;
3668 waddwstr(view->window, &wline[skip]);
3669 free(wline);
3670 wline = NULL;
3672 if (tc)
3673 wattr_off(view->window,
3674 COLOR_PAIR(tc->colorpair), NULL);
3675 if (width <= view->ncols - 1)
3676 waddch(view->window, '\n');
3677 nprinted++;
3679 free(line);
3680 if (nprinted >= 1)
3681 s->last_displayed_line = s->first_displayed_line +
3682 (nprinted - 1);
3683 else
3684 s->last_displayed_line = s->first_displayed_line;
3686 view_border(view);
3688 if (s->eof) {
3689 while (nprinted < view->nlines) {
3690 waddch(view->window, '\n');
3691 nprinted++;
3694 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3695 view->ncols, 0, 0);
3696 if (err) {
3697 return err;
3700 wstandout(view->window);
3701 waddwstr(view->window, wline);
3702 free(wline);
3703 wline = NULL;
3704 wstandend(view->window);
3707 return NULL;
3710 static char *
3711 get_datestr(time_t *time, char *datebuf)
3713 struct tm mytm, *tm;
3714 char *p, *s;
3716 tm = gmtime_r(time, &mytm);
3717 if (tm == NULL)
3718 return NULL;
3719 s = asctime_r(tm, datebuf);
3720 if (s == NULL)
3721 return NULL;
3722 p = strchr(s, '\n');
3723 if (p)
3724 *p = '\0';
3725 return s;
3728 static const struct got_error *
3729 get_changed_paths(struct got_pathlist_head *paths,
3730 struct got_commit_object *commit, struct got_repository *repo)
3732 const struct got_error *err = NULL;
3733 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3734 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3735 struct got_object_qid *qid;
3737 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3738 if (qid != NULL) {
3739 struct got_commit_object *pcommit;
3740 err = got_object_open_as_commit(&pcommit, repo,
3741 &qid->id);
3742 if (err)
3743 return err;
3745 tree_id1 = got_object_id_dup(
3746 got_object_commit_get_tree_id(pcommit));
3747 if (tree_id1 == NULL) {
3748 got_object_commit_close(pcommit);
3749 return got_error_from_errno("got_object_id_dup");
3751 got_object_commit_close(pcommit);
3755 if (tree_id1) {
3756 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3757 if (err)
3758 goto done;
3761 tree_id2 = got_object_commit_get_tree_id(commit);
3762 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3763 if (err)
3764 goto done;
3766 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3767 got_diff_tree_collect_changed_paths, paths, 0);
3768 done:
3769 if (tree1)
3770 got_object_tree_close(tree1);
3771 if (tree2)
3772 got_object_tree_close(tree2);
3773 free(tree_id1);
3774 return err;
3777 static const struct got_error *
3778 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3780 off_t *p;
3782 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3783 if (p == NULL)
3784 return got_error_from_errno("reallocarray");
3785 *line_offsets = p;
3786 (*line_offsets)[*nlines] = off;
3787 (*nlines)++;
3788 return NULL;
3791 static const struct got_error *
3792 write_commit_info(off_t **line_offsets, size_t *nlines,
3793 struct got_object_id *commit_id, struct got_reflist_head *refs,
3794 struct got_repository *repo, FILE *outfile)
3796 const struct got_error *err = NULL;
3797 char datebuf[26], *datestr;
3798 struct got_commit_object *commit;
3799 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3800 time_t committer_time;
3801 const char *author, *committer;
3802 char *refs_str = NULL;
3803 struct got_pathlist_head changed_paths;
3804 struct got_pathlist_entry *pe;
3805 off_t outoff = 0;
3806 int n;
3808 TAILQ_INIT(&changed_paths);
3810 if (refs) {
3811 err = build_refs_str(&refs_str, refs, commit_id, repo);
3812 if (err)
3813 return err;
3816 err = got_object_open_as_commit(&commit, repo, commit_id);
3817 if (err)
3818 return err;
3820 err = got_object_id_str(&id_str, commit_id);
3821 if (err) {
3822 err = got_error_from_errno("got_object_id_str");
3823 goto done;
3826 err = add_line_offset(line_offsets, nlines, 0);
3827 if (err)
3828 goto done;
3830 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3831 refs_str ? refs_str : "", refs_str ? ")" : "");
3832 if (n < 0) {
3833 err = got_error_from_errno("fprintf");
3834 goto done;
3836 outoff += n;
3837 err = add_line_offset(line_offsets, nlines, outoff);
3838 if (err)
3839 goto done;
3841 n = fprintf(outfile, "from: %s\n",
3842 got_object_commit_get_author(commit));
3843 if (n < 0) {
3844 err = got_error_from_errno("fprintf");
3845 goto done;
3847 outoff += n;
3848 err = add_line_offset(line_offsets, nlines, outoff);
3849 if (err)
3850 goto done;
3852 committer_time = got_object_commit_get_committer_time(commit);
3853 datestr = get_datestr(&committer_time, datebuf);
3854 if (datestr) {
3855 n = fprintf(outfile, "date: %s UTC\n", datestr);
3856 if (n < 0) {
3857 err = got_error_from_errno("fprintf");
3858 goto done;
3860 outoff += n;
3861 err = add_line_offset(line_offsets, nlines, outoff);
3862 if (err)
3863 goto done;
3865 author = got_object_commit_get_author(commit);
3866 committer = got_object_commit_get_committer(commit);
3867 if (strcmp(author, committer) != 0) {
3868 n = fprintf(outfile, "via: %s\n", committer);
3869 if (n < 0) {
3870 err = got_error_from_errno("fprintf");
3871 goto done;
3873 outoff += n;
3874 err = add_line_offset(line_offsets, nlines, outoff);
3875 if (err)
3876 goto done;
3878 if (got_object_commit_get_nparents(commit) > 1) {
3879 const struct got_object_id_queue *parent_ids;
3880 struct got_object_qid *qid;
3881 int pn = 1;
3882 parent_ids = got_object_commit_get_parent_ids(commit);
3883 STAILQ_FOREACH(qid, parent_ids, entry) {
3884 err = got_object_id_str(&id_str, &qid->id);
3885 if (err)
3886 goto done;
3887 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3888 if (n < 0) {
3889 err = got_error_from_errno("fprintf");
3890 goto done;
3892 outoff += n;
3893 err = add_line_offset(line_offsets, nlines, outoff);
3894 if (err)
3895 goto done;
3896 free(id_str);
3897 id_str = NULL;
3901 err = got_object_commit_get_logmsg(&logmsg, commit);
3902 if (err)
3903 goto done;
3904 s = logmsg;
3905 while ((line = strsep(&s, "\n")) != NULL) {
3906 n = fprintf(outfile, "%s\n", line);
3907 if (n < 0) {
3908 err = got_error_from_errno("fprintf");
3909 goto done;
3911 outoff += n;
3912 err = add_line_offset(line_offsets, nlines, outoff);
3913 if (err)
3914 goto done;
3917 err = get_changed_paths(&changed_paths, commit, repo);
3918 if (err)
3919 goto done;
3920 TAILQ_FOREACH(pe, &changed_paths, entry) {
3921 struct got_diff_changed_path *cp = pe->data;
3922 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3923 if (n < 0) {
3924 err = got_error_from_errno("fprintf");
3925 goto done;
3927 outoff += n;
3928 err = add_line_offset(line_offsets, nlines, outoff);
3929 if (err)
3930 goto done;
3931 free((char *)pe->path);
3932 free(pe->data);
3935 fputc('\n', outfile);
3936 outoff++;
3937 err = add_line_offset(line_offsets, nlines, outoff);
3938 done:
3939 got_pathlist_free(&changed_paths);
3940 free(id_str);
3941 free(logmsg);
3942 free(refs_str);
3943 got_object_commit_close(commit);
3944 if (err) {
3945 free(*line_offsets);
3946 *line_offsets = NULL;
3947 *nlines = 0;
3949 return err;
3952 static const struct got_error *
3953 create_diff(struct tog_diff_view_state *s)
3955 const struct got_error *err = NULL;
3956 FILE *f = NULL;
3957 int obj_type;
3959 free(s->line_offsets);
3960 s->line_offsets = malloc(sizeof(off_t));
3961 if (s->line_offsets == NULL)
3962 return got_error_from_errno("malloc");
3963 s->nlines = 0;
3965 f = got_opentemp();
3966 if (f == NULL) {
3967 err = got_error_from_errno("got_opentemp");
3968 goto done;
3970 if (s->f && fclose(s->f) == EOF) {
3971 err = got_error_from_errno("fclose");
3972 goto done;
3974 s->f = f;
3976 if (s->id1)
3977 err = got_object_get_type(&obj_type, s->repo, s->id1);
3978 else
3979 err = got_object_get_type(&obj_type, s->repo, s->id2);
3980 if (err)
3981 goto done;
3983 switch (obj_type) {
3984 case GOT_OBJ_TYPE_BLOB:
3985 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3986 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
3987 s->label1, s->label2, tog_diff_algo, s->diff_context,
3988 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3989 break;
3990 case GOT_OBJ_TYPE_TREE:
3991 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3992 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
3993 tog_diff_algo, s->diff_context, s->ignore_whitespace,
3994 s->force_text_diff, s->repo, s->f);
3995 break;
3996 case GOT_OBJ_TYPE_COMMIT: {
3997 const struct got_object_id_queue *parent_ids;
3998 struct got_object_qid *pid;
3999 struct got_commit_object *commit2;
4000 struct got_reflist_head *refs;
4002 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4003 if (err)
4004 goto done;
4005 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4006 /* Show commit info if we're diffing to a parent/root commit. */
4007 if (s->id1 == NULL) {
4008 err = write_commit_info(&s->line_offsets, &s->nlines,
4009 s->id2, refs, s->repo, s->f);
4010 if (err)
4011 goto done;
4012 } else {
4013 parent_ids = got_object_commit_get_parent_ids(commit2);
4014 STAILQ_FOREACH(pid, parent_ids, entry) {
4015 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4016 err = write_commit_info(
4017 &s->line_offsets, &s->nlines,
4018 s->id2, refs, s->repo, s->f);
4019 if (err)
4020 goto done;
4021 break;
4025 got_object_commit_close(commit2);
4027 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4028 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4029 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4030 s->force_text_diff, s->repo, s->f);
4031 break;
4033 default:
4034 err = got_error(GOT_ERR_OBJ_TYPE);
4035 break;
4037 if (err)
4038 goto done;
4039 done:
4040 if (s->f && fflush(s->f) != 0 && err == NULL)
4041 err = got_error_from_errno("fflush");
4042 return err;
4045 static void
4046 diff_view_indicate_progress(struct tog_view *view)
4048 mvwaddstr(view->window, 0, 0, "diffing...");
4049 update_panels();
4050 doupdate();
4053 static const struct got_error *
4054 search_start_diff_view(struct tog_view *view)
4056 struct tog_diff_view_state *s = &view->state.diff;
4058 s->matched_line = 0;
4059 return NULL;
4062 static const struct got_error *
4063 search_next_diff_view(struct tog_view *view)
4065 struct tog_diff_view_state *s = &view->state.diff;
4066 const struct got_error *err = NULL;
4067 int lineno;
4068 char *line = NULL;
4069 size_t linesize = 0;
4070 ssize_t linelen;
4072 if (!view->searching) {
4073 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4074 return NULL;
4077 if (s->matched_line) {
4078 if (view->searching == TOG_SEARCH_FORWARD)
4079 lineno = s->matched_line + 1;
4080 else
4081 lineno = s->matched_line - 1;
4082 } else
4083 lineno = s->first_displayed_line;
4085 while (1) {
4086 off_t offset;
4088 if (lineno <= 0 || lineno > s->nlines) {
4089 if (s->matched_line == 0) {
4090 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4091 break;
4094 if (view->searching == TOG_SEARCH_FORWARD)
4095 lineno = 1;
4096 else
4097 lineno = s->nlines;
4100 offset = s->line_offsets[lineno - 1];
4101 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4102 free(line);
4103 return got_error_from_errno("fseeko");
4105 linelen = getline(&line, &linesize, s->f);
4106 if (linelen != -1) {
4107 char *exstr;
4108 err = expand_tab(&exstr, line);
4109 if (err)
4110 break;
4111 if (match_line(exstr, &view->regex, 1,
4112 &view->regmatch)) {
4113 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4114 s->matched_line = lineno;
4115 free(exstr);
4116 break;
4118 free(exstr);
4120 if (view->searching == TOG_SEARCH_FORWARD)
4121 lineno++;
4122 else
4123 lineno--;
4125 free(line);
4127 if (s->matched_line) {
4128 s->first_displayed_line = s->matched_line;
4129 s->selected_line = 1;
4132 return err;
4135 static const struct got_error *
4136 close_diff_view(struct tog_view *view)
4138 const struct got_error *err = NULL;
4139 struct tog_diff_view_state *s = &view->state.diff;
4141 free(s->id1);
4142 s->id1 = NULL;
4143 free(s->id2);
4144 s->id2 = NULL;
4145 if (s->f && fclose(s->f) == EOF)
4146 err = got_error_from_errno("fclose");
4147 s->f = NULL;
4148 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4149 err = got_error_from_errno("fclose");
4150 s->f1 = NULL;
4151 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4152 err = got_error_from_errno("fclose");
4153 s->f2 = NULL;
4154 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4155 err = got_error_from_errno("close");
4156 s->fd1 = -1;
4157 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4158 err = got_error_from_errno("close");
4159 s->fd2 = -1;
4160 free_colors(&s->colors);
4161 free(s->line_offsets);
4162 s->line_offsets = NULL;
4163 s->nlines = 0;
4164 return err;
4167 static const struct got_error *
4168 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4169 struct got_object_id *id2, const char *label1, const char *label2,
4170 int diff_context, int ignore_whitespace, int force_text_diff,
4171 struct tog_view *log_view, struct got_repository *repo)
4173 const struct got_error *err;
4174 struct tog_diff_view_state *s = &view->state.diff;
4176 memset(s, 0, sizeof(*s));
4177 s->fd1 = -1;
4178 s->fd2 = -1;
4180 if (id1 != NULL && id2 != NULL) {
4181 int type1, type2;
4182 err = got_object_get_type(&type1, repo, id1);
4183 if (err)
4184 return err;
4185 err = got_object_get_type(&type2, repo, id2);
4186 if (err)
4187 return err;
4189 if (type1 != type2)
4190 return got_error(GOT_ERR_OBJ_TYPE);
4192 s->first_displayed_line = 1;
4193 s->last_displayed_line = view->nlines;
4194 s->selected_line = 1;
4195 s->repo = repo;
4196 s->id1 = id1;
4197 s->id2 = id2;
4198 s->label1 = label1;
4199 s->label2 = label2;
4201 if (id1) {
4202 s->id1 = got_object_id_dup(id1);
4203 if (s->id1 == NULL)
4204 return got_error_from_errno("got_object_id_dup");
4205 } else
4206 s->id1 = NULL;
4208 s->id2 = got_object_id_dup(id2);
4209 if (s->id2 == NULL) {
4210 err = got_error_from_errno("got_object_id_dup");
4211 goto done;
4214 s->f1 = got_opentemp();
4215 if (s->f1 == NULL) {
4216 err = got_error_from_errno("got_opentemp");
4217 goto done;
4220 s->f2 = got_opentemp();
4221 if (s->f2 == NULL) {
4222 err = got_error_from_errno("got_opentemp");
4223 goto done;
4226 s->fd1 = got_opentempfd();
4227 if (s->fd1 == -1) {
4228 err = got_error_from_errno("got_opentempfd");
4229 goto done;
4232 s->fd2 = got_opentempfd();
4233 if (s->fd2 == -1) {
4234 err = got_error_from_errno("got_opentempfd");
4235 goto done;
4238 s->first_displayed_line = 1;
4239 s->last_displayed_line = view->nlines;
4240 s->diff_context = diff_context;
4241 s->ignore_whitespace = ignore_whitespace;
4242 s->force_text_diff = force_text_diff;
4243 s->log_view = log_view;
4244 s->repo = repo;
4246 STAILQ_INIT(&s->colors);
4247 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4248 err = add_color(&s->colors,
4249 "^-", TOG_COLOR_DIFF_MINUS,
4250 get_color_value("TOG_COLOR_DIFF_MINUS"));
4251 if (err)
4252 goto done;
4253 err = add_color(&s->colors, "^\\+",
4254 TOG_COLOR_DIFF_PLUS,
4255 get_color_value("TOG_COLOR_DIFF_PLUS"));
4256 if (err)
4257 goto done;
4258 err = add_color(&s->colors,
4259 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4260 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4261 if (err)
4262 goto done;
4264 err = add_color(&s->colors,
4265 "^(commit [0-9a-f]|parent [0-9]|"
4266 "(blob|file|tree|commit) [-+] |"
4267 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4268 get_color_value("TOG_COLOR_DIFF_META"));
4269 if (err)
4270 goto done;
4272 err = add_color(&s->colors,
4273 "^(from|via): ", TOG_COLOR_AUTHOR,
4274 get_color_value("TOG_COLOR_AUTHOR"));
4275 if (err)
4276 goto done;
4278 err = add_color(&s->colors,
4279 "^date: ", TOG_COLOR_DATE,
4280 get_color_value("TOG_COLOR_DATE"));
4281 if (err)
4282 goto done;
4285 if (log_view && view_is_splitscreen(view))
4286 show_log_view(log_view); /* draw vborder */
4287 diff_view_indicate_progress(view);
4289 err = create_diff(s);
4291 view->show = show_diff_view;
4292 view->input = input_diff_view;
4293 view->reset = reset_diff_view;
4294 view->close = close_diff_view;
4295 view->search_start = search_start_diff_view;
4296 view->search_next = search_next_diff_view;
4297 done:
4298 if (err)
4299 close_diff_view(view);
4300 return err;
4303 static const struct got_error *
4304 show_diff_view(struct tog_view *view)
4306 const struct got_error *err;
4307 struct tog_diff_view_state *s = &view->state.diff;
4308 char *id_str1 = NULL, *id_str2, *header;
4309 const char *label1, *label2;
4311 if (s->id1) {
4312 err = got_object_id_str(&id_str1, s->id1);
4313 if (err)
4314 return err;
4315 label1 = s->label1 ? : id_str1;
4316 } else
4317 label1 = "/dev/null";
4319 err = got_object_id_str(&id_str2, s->id2);
4320 if (err)
4321 return err;
4322 label2 = s->label2 ? : id_str2;
4324 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4325 err = got_error_from_errno("asprintf");
4326 free(id_str1);
4327 free(id_str2);
4328 return err;
4330 free(id_str1);
4331 free(id_str2);
4333 err = draw_file(view, header);
4334 free(header);
4335 return err;
4338 static const struct got_error *
4339 set_selected_commit(struct tog_diff_view_state *s,
4340 struct commit_queue_entry *entry)
4342 const struct got_error *err;
4343 const struct got_object_id_queue *parent_ids;
4344 struct got_commit_object *selected_commit;
4345 struct got_object_qid *pid;
4347 free(s->id2);
4348 s->id2 = got_object_id_dup(entry->id);
4349 if (s->id2 == NULL)
4350 return got_error_from_errno("got_object_id_dup");
4352 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4353 if (err)
4354 return err;
4355 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4356 free(s->id1);
4357 pid = STAILQ_FIRST(parent_ids);
4358 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4359 got_object_commit_close(selected_commit);
4360 return NULL;
4363 static const struct got_error *
4364 reset_diff_view(struct tog_view *view)
4366 struct tog_diff_view_state *s = &view->state.diff;
4368 view->count = 0;
4369 wclear(view->window);
4370 s->first_displayed_line = 1;
4371 s->last_displayed_line = view->nlines;
4372 s->matched_line = 0;
4373 diff_view_indicate_progress(view);
4374 return create_diff(s);
4377 static const struct got_error *
4378 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4380 const struct got_error *err = NULL;
4381 struct tog_diff_view_state *s = &view->state.diff;
4382 struct tog_log_view_state *ls;
4383 struct commit_queue_entry *old_selected_entry;
4384 char *line = NULL;
4385 size_t linesize = 0;
4386 ssize_t linelen;
4387 int i, nscroll = view->nlines - 1;
4389 switch (ch) {
4390 case '0':
4391 view->x = 0;
4392 break;
4393 case '$':
4394 view->x = MAX(view->maxx - view->ncols / 3, 0);
4395 view->count = 0;
4396 break;
4397 case KEY_RIGHT:
4398 case 'l':
4399 if (view->x + view->ncols / 3 < view->maxx)
4400 view->x += 2; /* move two columns right */
4401 else
4402 view->count = 0;
4403 break;
4404 case KEY_LEFT:
4405 case 'h':
4406 view->x -= MIN(view->x, 2); /* move two columns back */
4407 if (view->x <= 0)
4408 view->count = 0;
4409 break;
4410 case 'a':
4411 case 'w':
4412 if (ch == 'a')
4413 s->force_text_diff = !s->force_text_diff;
4414 if (ch == 'w')
4415 s->ignore_whitespace = !s->ignore_whitespace;
4416 err = reset_diff_view(view);
4417 break;
4418 case 'g':
4419 case KEY_HOME:
4420 s->first_displayed_line = 1;
4421 view->count = 0;
4422 break;
4423 case 'G':
4424 case KEY_END:
4425 view->count = 0;
4426 if (s->eof)
4427 break;
4429 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4430 s->eof = 1;
4431 break;
4432 case 'k':
4433 case KEY_UP:
4434 case CTRL('p'):
4435 if (s->first_displayed_line > 1)
4436 s->first_displayed_line--;
4437 else
4438 view->count = 0;
4439 break;
4440 case CTRL('u'):
4441 case 'u':
4442 nscroll /= 2;
4443 /* FALL THROUGH */
4444 case KEY_PPAGE:
4445 case CTRL('b'):
4446 case 'b':
4447 if (s->first_displayed_line == 1) {
4448 view->count = 0;
4449 break;
4451 i = 0;
4452 while (i++ < nscroll && s->first_displayed_line > 1)
4453 s->first_displayed_line--;
4454 break;
4455 case 'j':
4456 case KEY_DOWN:
4457 case CTRL('n'):
4458 if (!s->eof)
4459 s->first_displayed_line++;
4460 else
4461 view->count = 0;
4462 break;
4463 case CTRL('d'):
4464 case 'd':
4465 nscroll /= 2;
4466 /* FALL THROUGH */
4467 case KEY_NPAGE:
4468 case CTRL('f'):
4469 case 'f':
4470 case ' ':
4471 if (s->eof) {
4472 view->count = 0;
4473 break;
4475 i = 0;
4476 while (!s->eof && i++ < nscroll) {
4477 linelen = getline(&line, &linesize, s->f);
4478 s->first_displayed_line++;
4479 if (linelen == -1) {
4480 if (feof(s->f)) {
4481 s->eof = 1;
4482 } else
4483 err = got_ferror(s->f, GOT_ERR_IO);
4484 break;
4487 free(line);
4488 break;
4489 case '[':
4490 if (s->diff_context > 0) {
4491 s->diff_context--;
4492 s->matched_line = 0;
4493 diff_view_indicate_progress(view);
4494 err = create_diff(s);
4495 if (s->first_displayed_line + view->nlines - 1 >
4496 s->nlines) {
4497 s->first_displayed_line = 1;
4498 s->last_displayed_line = view->nlines;
4500 } else
4501 view->count = 0;
4502 break;
4503 case ']':
4504 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4505 s->diff_context++;
4506 s->matched_line = 0;
4507 diff_view_indicate_progress(view);
4508 err = create_diff(s);
4509 } else
4510 view->count = 0;
4511 break;
4512 case '<':
4513 case ',':
4514 if (s->log_view == NULL) {
4515 view->count = 0;
4516 break;
4518 ls = &s->log_view->state.log;
4519 old_selected_entry = ls->selected_entry;
4521 /* view->count handled in input_log_view() */
4522 err = input_log_view(NULL, s->log_view, KEY_UP);
4523 if (err)
4524 break;
4526 if (old_selected_entry == ls->selected_entry)
4527 break;
4529 err = set_selected_commit(s, ls->selected_entry);
4530 if (err)
4531 break;
4533 s->first_displayed_line = 1;
4534 s->last_displayed_line = view->nlines;
4535 s->matched_line = 0;
4536 view->x = 0;
4538 diff_view_indicate_progress(view);
4539 err = create_diff(s);
4540 break;
4541 case '>':
4542 case '.':
4543 if (s->log_view == NULL) {
4544 view->count = 0;
4545 break;
4547 ls = &s->log_view->state.log;
4548 old_selected_entry = ls->selected_entry;
4550 /* view->count handled in input_log_view() */
4551 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4552 if (err)
4553 break;
4555 if (old_selected_entry == ls->selected_entry)
4556 break;
4558 err = set_selected_commit(s, ls->selected_entry);
4559 if (err)
4560 break;
4562 s->first_displayed_line = 1;
4563 s->last_displayed_line = view->nlines;
4564 s->matched_line = 0;
4565 view->x = 0;
4567 diff_view_indicate_progress(view);
4568 err = create_diff(s);
4569 break;
4570 default:
4571 view->count = 0;
4572 break;
4575 return err;
4578 static const struct got_error *
4579 cmd_diff(int argc, char *argv[])
4581 const struct got_error *error = NULL;
4582 struct got_repository *repo = NULL;
4583 struct got_worktree *worktree = NULL;
4584 struct got_object_id *id1 = NULL, *id2 = NULL;
4585 char *repo_path = NULL, *cwd = NULL;
4586 char *id_str1 = NULL, *id_str2 = NULL;
4587 char *label1 = NULL, *label2 = NULL;
4588 int diff_context = 3, ignore_whitespace = 0;
4589 int ch, force_text_diff = 0;
4590 const char *errstr;
4591 struct tog_view *view;
4592 int *pack_fds = NULL;
4594 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4595 switch (ch) {
4596 case 'a':
4597 force_text_diff = 1;
4598 break;
4599 case 'C':
4600 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4601 &errstr);
4602 if (errstr != NULL)
4603 errx(1, "number of context lines is %s: %s",
4604 errstr, errstr);
4605 break;
4606 case 'r':
4607 repo_path = realpath(optarg, NULL);
4608 if (repo_path == NULL)
4609 return got_error_from_errno2("realpath",
4610 optarg);
4611 got_path_strip_trailing_slashes(repo_path);
4612 break;
4613 case 'w':
4614 ignore_whitespace = 1;
4615 break;
4616 default:
4617 usage_diff();
4618 /* NOTREACHED */
4622 argc -= optind;
4623 argv += optind;
4625 if (argc == 0) {
4626 usage_diff(); /* TODO show local worktree changes */
4627 } else if (argc == 2) {
4628 id_str1 = argv[0];
4629 id_str2 = argv[1];
4630 } else
4631 usage_diff();
4633 error = got_repo_pack_fds_open(&pack_fds);
4634 if (error)
4635 goto done;
4637 if (repo_path == NULL) {
4638 cwd = getcwd(NULL, 0);
4639 if (cwd == NULL)
4640 return got_error_from_errno("getcwd");
4641 error = got_worktree_open(&worktree, cwd);
4642 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4643 goto done;
4644 if (worktree)
4645 repo_path =
4646 strdup(got_worktree_get_repo_path(worktree));
4647 else
4648 repo_path = strdup(cwd);
4649 if (repo_path == NULL) {
4650 error = got_error_from_errno("strdup");
4651 goto done;
4655 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4656 if (error)
4657 goto done;
4659 init_curses();
4661 error = apply_unveil(got_repo_get_path(repo), NULL);
4662 if (error)
4663 goto done;
4665 error = tog_load_refs(repo, 0);
4666 if (error)
4667 goto done;
4669 error = got_repo_match_object_id(&id1, &label1, id_str1,
4670 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4671 if (error)
4672 goto done;
4674 error = got_repo_match_object_id(&id2, &label2, id_str2,
4675 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4676 if (error)
4677 goto done;
4679 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4680 if (view == NULL) {
4681 error = got_error_from_errno("view_open");
4682 goto done;
4684 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4685 ignore_whitespace, force_text_diff, NULL, repo);
4686 if (error)
4687 goto done;
4688 error = view_loop(view);
4689 done:
4690 free(label1);
4691 free(label2);
4692 free(repo_path);
4693 free(cwd);
4694 if (repo) {
4695 const struct got_error *close_err = got_repo_close(repo);
4696 if (error == NULL)
4697 error = close_err;
4699 if (worktree)
4700 got_worktree_close(worktree);
4701 if (pack_fds) {
4702 const struct got_error *pack_err =
4703 got_repo_pack_fds_close(pack_fds);
4704 if (error == NULL)
4705 error = pack_err;
4707 tog_free_refs();
4708 return error;
4711 __dead static void
4712 usage_blame(void)
4714 endwin();
4715 fprintf(stderr,
4716 "usage: %s blame [-c commit] [-r repository-path] path\n",
4717 getprogname());
4718 exit(1);
4721 struct tog_blame_line {
4722 int annotated;
4723 struct got_object_id *id;
4726 static const struct got_error *
4727 draw_blame(struct tog_view *view)
4729 struct tog_blame_view_state *s = &view->state.blame;
4730 struct tog_blame *blame = &s->blame;
4731 regmatch_t *regmatch = &view->regmatch;
4732 const struct got_error *err;
4733 int lineno = 0, nprinted = 0;
4734 char *line = NULL;
4735 size_t linesize = 0;
4736 ssize_t linelen;
4737 wchar_t *wline;
4738 int width;
4739 struct tog_blame_line *blame_line;
4740 struct got_object_id *prev_id = NULL;
4741 char *id_str;
4742 struct tog_color *tc;
4744 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4745 if (err)
4746 return err;
4748 rewind(blame->f);
4749 werase(view->window);
4751 if (asprintf(&line, "commit %s", id_str) == -1) {
4752 err = got_error_from_errno("asprintf");
4753 free(id_str);
4754 return err;
4757 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4758 free(line);
4759 line = NULL;
4760 if (err)
4761 return err;
4762 if (view_needs_focus_indication(view))
4763 wstandout(view->window);
4764 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4765 if (tc)
4766 wattr_on(view->window,
4767 COLOR_PAIR(tc->colorpair), NULL);
4768 waddwstr(view->window, wline);
4769 if (tc)
4770 wattr_off(view->window,
4771 COLOR_PAIR(tc->colorpair), NULL);
4772 if (view_needs_focus_indication(view))
4773 wstandend(view->window);
4774 free(wline);
4775 wline = NULL;
4776 if (width < view->ncols - 1)
4777 waddch(view->window, '\n');
4779 if (asprintf(&line, "[%d/%d] %s%s",
4780 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4781 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4782 free(id_str);
4783 return got_error_from_errno("asprintf");
4785 free(id_str);
4786 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4787 free(line);
4788 line = NULL;
4789 if (err)
4790 return err;
4791 waddwstr(view->window, wline);
4792 free(wline);
4793 wline = NULL;
4794 if (width < view->ncols - 1)
4795 waddch(view->window, '\n');
4797 s->eof = 0;
4798 view->maxx = 0;
4799 while (nprinted < view->nlines - 2) {
4800 linelen = getline(&line, &linesize, blame->f);
4801 if (linelen == -1) {
4802 if (feof(blame->f)) {
4803 s->eof = 1;
4804 break;
4806 free(line);
4807 return got_ferror(blame->f, GOT_ERR_IO);
4809 if (++lineno < s->first_displayed_line)
4810 continue;
4812 /* Set view->maxx based on full line length. */
4813 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4814 if (err) {
4815 free(line);
4816 return err;
4818 free(wline);
4819 wline = NULL;
4820 view->maxx = MAX(view->maxx, width);
4822 if (view->focussed && nprinted == s->selected_line - 1)
4823 wstandout(view->window);
4825 if (blame->nlines > 0) {
4826 blame_line = &blame->lines[lineno - 1];
4827 if (blame_line->annotated && prev_id &&
4828 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4829 !(view->focussed &&
4830 nprinted == s->selected_line - 1)) {
4831 waddstr(view->window, " ");
4832 } else if (blame_line->annotated) {
4833 char *id_str;
4834 err = got_object_id_str(&id_str,
4835 blame_line->id);
4836 if (err) {
4837 free(line);
4838 return err;
4840 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4841 if (tc)
4842 wattr_on(view->window,
4843 COLOR_PAIR(tc->colorpair), NULL);
4844 wprintw(view->window, "%.8s", id_str);
4845 if (tc)
4846 wattr_off(view->window,
4847 COLOR_PAIR(tc->colorpair), NULL);
4848 free(id_str);
4849 prev_id = blame_line->id;
4850 } else {
4851 waddstr(view->window, "........");
4852 prev_id = NULL;
4854 } else {
4855 waddstr(view->window, "........");
4856 prev_id = NULL;
4859 if (view->focussed && nprinted == s->selected_line - 1)
4860 wstandend(view->window);
4861 waddstr(view->window, " ");
4863 if (view->ncols <= 9) {
4864 width = 9;
4865 } else if (s->first_displayed_line + nprinted ==
4866 s->matched_line &&
4867 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4868 err = add_matched_line(&width, line, view->ncols - 9, 9,
4869 view->window, view->x, regmatch);
4870 if (err) {
4871 free(line);
4872 return err;
4874 width += 9;
4875 } else {
4876 int skip;
4877 err = format_line(&wline, &width, &skip, line,
4878 view->x, view->ncols - 9, 9, 1);
4879 if (err) {
4880 free(line);
4881 return err;
4883 waddwstr(view->window, &wline[skip]);
4884 width += 9;
4885 free(wline);
4886 wline = NULL;
4889 if (width <= view->ncols - 1)
4890 waddch(view->window, '\n');
4891 if (++nprinted == 1)
4892 s->first_displayed_line = lineno;
4894 free(line);
4895 s->last_displayed_line = lineno;
4897 view_border(view);
4899 return NULL;
4902 static const struct got_error *
4903 blame_cb(void *arg, int nlines, int lineno,
4904 struct got_commit_object *commit, struct got_object_id *id)
4906 const struct got_error *err = NULL;
4907 struct tog_blame_cb_args *a = arg;
4908 struct tog_blame_line *line;
4909 int errcode;
4911 if (nlines != a->nlines ||
4912 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4913 return got_error(GOT_ERR_RANGE);
4915 errcode = pthread_mutex_lock(&tog_mutex);
4916 if (errcode)
4917 return got_error_set_errno(errcode, "pthread_mutex_lock");
4919 if (*a->quit) { /* user has quit the blame view */
4920 err = got_error(GOT_ERR_ITER_COMPLETED);
4921 goto done;
4924 if (lineno == -1)
4925 goto done; /* no change in this commit */
4927 line = &a->lines[lineno - 1];
4928 if (line->annotated)
4929 goto done;
4931 line->id = got_object_id_dup(id);
4932 if (line->id == NULL) {
4933 err = got_error_from_errno("got_object_id_dup");
4934 goto done;
4936 line->annotated = 1;
4937 done:
4938 errcode = pthread_mutex_unlock(&tog_mutex);
4939 if (errcode)
4940 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4941 return err;
4944 static void *
4945 blame_thread(void *arg)
4947 const struct got_error *err, *close_err;
4948 struct tog_blame_thread_args *ta = arg;
4949 struct tog_blame_cb_args *a = ta->cb_args;
4950 int errcode, fd1 = -1, fd2 = -1;
4951 FILE *f1 = NULL, *f2 = NULL;
4953 fd1 = got_opentempfd();
4954 if (fd1 == -1)
4955 return (void *)got_error_from_errno("got_opentempfd");
4957 fd2 = got_opentempfd();
4958 if (fd2 == -1) {
4959 err = got_error_from_errno("got_opentempfd");
4960 goto done;
4963 f1 = got_opentemp();
4964 if (f1 == NULL) {
4965 err = (void *)got_error_from_errno("got_opentemp");
4966 goto done;
4968 f2 = got_opentemp();
4969 if (f2 == NULL) {
4970 err = (void *)got_error_from_errno("got_opentemp");
4971 goto done;
4974 err = block_signals_used_by_main_thread();
4975 if (err)
4976 goto done;
4978 err = got_blame(ta->path, a->commit_id, ta->repo,
4979 tog_diff_algo, blame_cb, ta->cb_args,
4980 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
4981 if (err && err->code == GOT_ERR_CANCELLED)
4982 err = NULL;
4984 errcode = pthread_mutex_lock(&tog_mutex);
4985 if (errcode) {
4986 err = got_error_set_errno(errcode, "pthread_mutex_lock");
4987 goto done;
4990 close_err = got_repo_close(ta->repo);
4991 if (err == NULL)
4992 err = close_err;
4993 ta->repo = NULL;
4994 *ta->complete = 1;
4996 errcode = pthread_mutex_unlock(&tog_mutex);
4997 if (errcode && err == NULL)
4998 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5000 done:
5001 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5002 err = got_error_from_errno("close");
5003 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5004 err = got_error_from_errno("close");
5005 if (f1 && fclose(f1) == EOF && err == NULL)
5006 err = got_error_from_errno("fclose");
5007 if (f2 && fclose(f2) == EOF && err == NULL)
5008 err = got_error_from_errno("fclose");
5010 return (void *)err;
5013 static struct got_object_id *
5014 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5015 int first_displayed_line, int selected_line)
5017 struct tog_blame_line *line;
5019 if (nlines <= 0)
5020 return NULL;
5022 line = &lines[first_displayed_line - 1 + selected_line - 1];
5023 if (!line->annotated)
5024 return NULL;
5026 return line->id;
5029 static const struct got_error *
5030 stop_blame(struct tog_blame *blame)
5032 const struct got_error *err = NULL;
5033 int i;
5035 if (blame->thread) {
5036 int errcode;
5037 errcode = pthread_mutex_unlock(&tog_mutex);
5038 if (errcode)
5039 return got_error_set_errno(errcode,
5040 "pthread_mutex_unlock");
5041 errcode = pthread_join(blame->thread, (void **)&err);
5042 if (errcode)
5043 return got_error_set_errno(errcode, "pthread_join");
5044 errcode = pthread_mutex_lock(&tog_mutex);
5045 if (errcode)
5046 return got_error_set_errno(errcode,
5047 "pthread_mutex_lock");
5048 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5049 err = NULL;
5050 blame->thread = NULL;
5052 if (blame->thread_args.repo) {
5053 const struct got_error *close_err;
5054 close_err = got_repo_close(blame->thread_args.repo);
5055 if (err == NULL)
5056 err = close_err;
5057 blame->thread_args.repo = NULL;
5059 if (blame->f) {
5060 if (fclose(blame->f) == EOF && err == NULL)
5061 err = got_error_from_errno("fclose");
5062 blame->f = NULL;
5064 if (blame->lines) {
5065 for (i = 0; i < blame->nlines; i++)
5066 free(blame->lines[i].id);
5067 free(blame->lines);
5068 blame->lines = NULL;
5070 free(blame->cb_args.commit_id);
5071 blame->cb_args.commit_id = NULL;
5072 if (blame->pack_fds) {
5073 const struct got_error *pack_err =
5074 got_repo_pack_fds_close(blame->pack_fds);
5075 if (err == NULL)
5076 err = pack_err;
5077 blame->pack_fds = NULL;
5079 return err;
5082 static const struct got_error *
5083 cancel_blame_view(void *arg)
5085 const struct got_error *err = NULL;
5086 int *done = arg;
5087 int errcode;
5089 errcode = pthread_mutex_lock(&tog_mutex);
5090 if (errcode)
5091 return got_error_set_errno(errcode,
5092 "pthread_mutex_unlock");
5094 if (*done)
5095 err = got_error(GOT_ERR_CANCELLED);
5097 errcode = pthread_mutex_unlock(&tog_mutex);
5098 if (errcode)
5099 return got_error_set_errno(errcode,
5100 "pthread_mutex_lock");
5102 return err;
5105 static const struct got_error *
5106 run_blame(struct tog_view *view)
5108 struct tog_blame_view_state *s = &view->state.blame;
5109 struct tog_blame *blame = &s->blame;
5110 const struct got_error *err = NULL;
5111 struct got_commit_object *commit = NULL;
5112 struct got_blob_object *blob = NULL;
5113 struct got_repository *thread_repo = NULL;
5114 struct got_object_id *obj_id = NULL;
5115 int obj_type, fd = -1;
5116 int *pack_fds = NULL;
5118 err = got_object_open_as_commit(&commit, s->repo,
5119 &s->blamed_commit->id);
5120 if (err)
5121 return err;
5123 fd = got_opentempfd();
5124 if (fd == -1) {
5125 err = got_error_from_errno("got_opentempfd");
5126 goto done;
5129 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5130 if (err)
5131 goto done;
5133 err = got_object_get_type(&obj_type, s->repo, obj_id);
5134 if (err)
5135 goto done;
5137 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5138 err = got_error(GOT_ERR_OBJ_TYPE);
5139 goto done;
5142 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5143 if (err)
5144 goto done;
5145 blame->f = got_opentemp();
5146 if (blame->f == NULL) {
5147 err = got_error_from_errno("got_opentemp");
5148 goto done;
5150 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5151 &blame->line_offsets, blame->f, blob);
5152 if (err)
5153 goto done;
5154 if (blame->nlines == 0) {
5155 s->blame_complete = 1;
5156 goto done;
5159 /* Don't include \n at EOF in the blame line count. */
5160 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5161 blame->nlines--;
5163 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5164 if (blame->lines == NULL) {
5165 err = got_error_from_errno("calloc");
5166 goto done;
5169 err = got_repo_pack_fds_open(&pack_fds);
5170 if (err)
5171 goto done;
5172 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5173 pack_fds);
5174 if (err)
5175 goto done;
5177 blame->pack_fds = pack_fds;
5178 blame->cb_args.view = view;
5179 blame->cb_args.lines = blame->lines;
5180 blame->cb_args.nlines = blame->nlines;
5181 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5182 if (blame->cb_args.commit_id == NULL) {
5183 err = got_error_from_errno("got_object_id_dup");
5184 goto done;
5186 blame->cb_args.quit = &s->done;
5188 blame->thread_args.path = s->path;
5189 blame->thread_args.repo = thread_repo;
5190 blame->thread_args.cb_args = &blame->cb_args;
5191 blame->thread_args.complete = &s->blame_complete;
5192 blame->thread_args.cancel_cb = cancel_blame_view;
5193 blame->thread_args.cancel_arg = &s->done;
5194 s->blame_complete = 0;
5196 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5197 s->first_displayed_line = 1;
5198 s->last_displayed_line = view->nlines;
5199 s->selected_line = 1;
5201 s->matched_line = 0;
5203 done:
5204 if (commit)
5205 got_object_commit_close(commit);
5206 if (fd != -1 && close(fd) == -1 && err == NULL)
5207 err = got_error_from_errno("close");
5208 if (blob)
5209 got_object_blob_close(blob);
5210 free(obj_id);
5211 if (err)
5212 stop_blame(blame);
5213 return err;
5216 static const struct got_error *
5217 open_blame_view(struct tog_view *view, char *path,
5218 struct got_object_id *commit_id, struct got_repository *repo)
5220 const struct got_error *err = NULL;
5221 struct tog_blame_view_state *s = &view->state.blame;
5223 STAILQ_INIT(&s->blamed_commits);
5225 s->path = strdup(path);
5226 if (s->path == NULL)
5227 return got_error_from_errno("strdup");
5229 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5230 if (err) {
5231 free(s->path);
5232 return err;
5235 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5236 s->first_displayed_line = 1;
5237 s->last_displayed_line = view->nlines;
5238 s->selected_line = 1;
5239 s->blame_complete = 0;
5240 s->repo = repo;
5241 s->commit_id = commit_id;
5242 memset(&s->blame, 0, sizeof(s->blame));
5244 STAILQ_INIT(&s->colors);
5245 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5246 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5247 get_color_value("TOG_COLOR_COMMIT"));
5248 if (err)
5249 return err;
5252 view->show = show_blame_view;
5253 view->input = input_blame_view;
5254 view->reset = reset_blame_view;
5255 view->close = close_blame_view;
5256 view->search_start = search_start_blame_view;
5257 view->search_next = search_next_blame_view;
5259 return run_blame(view);
5262 static const struct got_error *
5263 close_blame_view(struct tog_view *view)
5265 const struct got_error *err = NULL;
5266 struct tog_blame_view_state *s = &view->state.blame;
5268 if (s->blame.thread)
5269 err = stop_blame(&s->blame);
5271 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5272 struct got_object_qid *blamed_commit;
5273 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5274 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5275 got_object_qid_free(blamed_commit);
5278 free(s->path);
5279 free_colors(&s->colors);
5280 return err;
5283 static const struct got_error *
5284 search_start_blame_view(struct tog_view *view)
5286 struct tog_blame_view_state *s = &view->state.blame;
5288 s->matched_line = 0;
5289 return NULL;
5292 static const struct got_error *
5293 search_next_blame_view(struct tog_view *view)
5295 struct tog_blame_view_state *s = &view->state.blame;
5296 const struct got_error *err = NULL;
5297 int lineno;
5298 char *line = NULL;
5299 size_t linesize = 0;
5300 ssize_t linelen;
5302 if (!view->searching) {
5303 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5304 return NULL;
5307 if (s->matched_line) {
5308 if (view->searching == TOG_SEARCH_FORWARD)
5309 lineno = s->matched_line + 1;
5310 else
5311 lineno = s->matched_line - 1;
5312 } else
5313 lineno = s->first_displayed_line - 1 + s->selected_line;
5315 while (1) {
5316 off_t offset;
5318 if (lineno <= 0 || lineno > s->blame.nlines) {
5319 if (s->matched_line == 0) {
5320 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5321 break;
5324 if (view->searching == TOG_SEARCH_FORWARD)
5325 lineno = 1;
5326 else
5327 lineno = s->blame.nlines;
5330 offset = s->blame.line_offsets[lineno - 1];
5331 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5332 free(line);
5333 return got_error_from_errno("fseeko");
5335 linelen = getline(&line, &linesize, s->blame.f);
5336 if (linelen != -1) {
5337 char *exstr;
5338 err = expand_tab(&exstr, line);
5339 if (err)
5340 break;
5341 if (match_line(exstr, &view->regex, 1,
5342 &view->regmatch)) {
5343 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5344 s->matched_line = lineno;
5345 free(exstr);
5346 break;
5348 free(exstr);
5350 if (view->searching == TOG_SEARCH_FORWARD)
5351 lineno++;
5352 else
5353 lineno--;
5355 free(line);
5357 if (s->matched_line) {
5358 s->first_displayed_line = s->matched_line;
5359 s->selected_line = 1;
5362 return err;
5365 static const struct got_error *
5366 show_blame_view(struct tog_view *view)
5368 const struct got_error *err = NULL;
5369 struct tog_blame_view_state *s = &view->state.blame;
5370 int errcode;
5372 if (s->blame.thread == NULL && !s->blame_complete) {
5373 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5374 &s->blame.thread_args);
5375 if (errcode)
5376 return got_error_set_errno(errcode, "pthread_create");
5378 halfdelay(1); /* fast refresh while annotating */
5381 if (s->blame_complete)
5382 halfdelay(10); /* disable fast refresh */
5384 err = draw_blame(view);
5386 view_border(view);
5387 return err;
5390 static const struct got_error *
5391 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5393 const struct got_error *err = NULL, *thread_err = NULL;
5394 struct tog_view *diff_view;
5395 struct tog_blame_view_state *s = &view->state.blame;
5396 int eos, nscroll, begin_y = 0, begin_x = 0;
5398 eos = nscroll = view->nlines - 2;
5399 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5400 view_is_splitscreen(view->child))
5401 --eos; /* border */
5403 switch (ch) {
5404 case '0':
5405 view->x = 0;
5406 break;
5407 case '$':
5408 view->x = MAX(view->maxx - view->ncols / 3, 0);
5409 view->count = 0;
5410 break;
5411 case KEY_RIGHT:
5412 case 'l':
5413 if (view->x + view->ncols / 3 < view->maxx)
5414 view->x += 2; /* move two columns right */
5415 else
5416 view->count = 0;
5417 break;
5418 case KEY_LEFT:
5419 case 'h':
5420 view->x -= MIN(view->x, 2); /* move two columns back */
5421 if (view->x <= 0)
5422 view->count = 0;
5423 break;
5424 case 'q':
5425 s->done = 1;
5426 break;
5427 case 'g':
5428 case KEY_HOME:
5429 s->selected_line = 1;
5430 s->first_displayed_line = 1;
5431 view->count = 0;
5432 break;
5433 case 'G':
5434 case KEY_END:
5435 if (s->blame.nlines < eos) {
5436 s->selected_line = s->blame.nlines;
5437 s->first_displayed_line = 1;
5438 } else {
5439 s->selected_line = eos;
5440 s->first_displayed_line = s->blame.nlines - (eos - 1);
5442 view->count = 0;
5443 break;
5444 case 'k':
5445 case KEY_UP:
5446 case CTRL('p'):
5447 if (s->selected_line > 1)
5448 s->selected_line--;
5449 else if (s->selected_line == 1 &&
5450 s->first_displayed_line > 1)
5451 s->first_displayed_line--;
5452 else
5453 view->count = 0;
5454 break;
5455 case CTRL('u'):
5456 case 'u':
5457 nscroll /= 2;
5458 /* FALL THROUGH */
5459 case KEY_PPAGE:
5460 case CTRL('b'):
5461 case 'b':
5462 if (s->first_displayed_line == 1) {
5463 if (view->count > 1)
5464 nscroll += nscroll;
5465 s->selected_line = MAX(1, s->selected_line - nscroll);
5466 view->count = 0;
5467 break;
5469 if (s->first_displayed_line > nscroll)
5470 s->first_displayed_line -= nscroll;
5471 else
5472 s->first_displayed_line = 1;
5473 break;
5474 case 'j':
5475 case KEY_DOWN:
5476 case CTRL('n'):
5477 if (s->selected_line < eos && s->first_displayed_line +
5478 s->selected_line <= s->blame.nlines)
5479 s->selected_line++;
5480 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5481 s->first_displayed_line++;
5482 else
5483 view->count = 0;
5484 break;
5485 case 'c':
5486 case 'p': {
5487 struct got_object_id *id = NULL;
5489 view->count = 0;
5490 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5491 s->first_displayed_line, s->selected_line);
5492 if (id == NULL)
5493 break;
5494 if (ch == 'p') {
5495 struct got_commit_object *commit, *pcommit;
5496 struct got_object_qid *pid;
5497 struct got_object_id *blob_id = NULL;
5498 int obj_type;
5499 err = got_object_open_as_commit(&commit,
5500 s->repo, id);
5501 if (err)
5502 break;
5503 pid = STAILQ_FIRST(
5504 got_object_commit_get_parent_ids(commit));
5505 if (pid == NULL) {
5506 got_object_commit_close(commit);
5507 break;
5509 /* Check if path history ends here. */
5510 err = got_object_open_as_commit(&pcommit,
5511 s->repo, &pid->id);
5512 if (err)
5513 break;
5514 err = got_object_id_by_path(&blob_id, s->repo,
5515 pcommit, s->path);
5516 got_object_commit_close(pcommit);
5517 if (err) {
5518 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5519 err = NULL;
5520 got_object_commit_close(commit);
5521 break;
5523 err = got_object_get_type(&obj_type, s->repo,
5524 blob_id);
5525 free(blob_id);
5526 /* Can't blame non-blob type objects. */
5527 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5528 got_object_commit_close(commit);
5529 break;
5531 err = got_object_qid_alloc(&s->blamed_commit,
5532 &pid->id);
5533 got_object_commit_close(commit);
5534 } else {
5535 if (got_object_id_cmp(id,
5536 &s->blamed_commit->id) == 0)
5537 break;
5538 err = got_object_qid_alloc(&s->blamed_commit,
5539 id);
5541 if (err)
5542 break;
5543 s->done = 1;
5544 thread_err = stop_blame(&s->blame);
5545 s->done = 0;
5546 if (thread_err)
5547 break;
5548 STAILQ_INSERT_HEAD(&s->blamed_commits,
5549 s->blamed_commit, entry);
5550 err = run_blame(view);
5551 if (err)
5552 break;
5553 break;
5555 case 'C': {
5556 struct got_object_qid *first;
5558 view->count = 0;
5559 first = STAILQ_FIRST(&s->blamed_commits);
5560 if (!got_object_id_cmp(&first->id, s->commit_id))
5561 break;
5562 s->done = 1;
5563 thread_err = stop_blame(&s->blame);
5564 s->done = 0;
5565 if (thread_err)
5566 break;
5567 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5568 got_object_qid_free(s->blamed_commit);
5569 s->blamed_commit =
5570 STAILQ_FIRST(&s->blamed_commits);
5571 err = run_blame(view);
5572 if (err)
5573 break;
5574 break;
5576 case KEY_ENTER:
5577 case '\r': {
5578 struct got_object_id *id = NULL;
5579 struct got_object_qid *pid;
5580 struct got_commit_object *commit = NULL;
5582 view->count = 0;
5583 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5584 s->first_displayed_line, s->selected_line);
5585 if (id == NULL)
5586 break;
5587 err = got_object_open_as_commit(&commit, s->repo, id);
5588 if (err)
5589 break;
5590 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5591 if (view_is_parent_view(view))
5592 view_get_split(view, &begin_y, &begin_x);
5594 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5595 if (diff_view == NULL) {
5596 got_object_commit_close(commit);
5597 err = got_error_from_errno("view_open");
5598 break;
5600 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5601 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5602 got_object_commit_close(commit);
5603 if (err) {
5604 view_close(diff_view);
5605 break;
5607 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
5608 err = view_init_hsplit(view, begin_y);
5609 if (err)
5610 break;
5613 view->focussed = 0;
5614 diff_view->focussed = 1;
5615 diff_view->mode = view->mode;
5616 diff_view->nlines = view->lines - begin_y;
5617 if (view_is_parent_view(view)) {
5618 err = view_close_child(view);
5619 if (err)
5620 break;
5621 err = view_set_child(view, diff_view);
5622 if (err)
5623 break;
5624 view->focus_child = 1;
5625 } else
5626 *new_view = diff_view;
5627 if (err)
5628 break;
5629 break;
5631 case CTRL('d'):
5632 case 'd':
5633 nscroll /= 2;
5634 /* FALL THROUGH */
5635 case KEY_NPAGE:
5636 case CTRL('f'):
5637 case 'f':
5638 case ' ':
5639 if (s->last_displayed_line >= s->blame.nlines &&
5640 s->selected_line >= MIN(s->blame.nlines,
5641 view->nlines - 2)) {
5642 view->count = 0;
5643 break;
5645 if (s->last_displayed_line >= s->blame.nlines &&
5646 s->selected_line < view->nlines - 2) {
5647 s->selected_line +=
5648 MIN(nscroll, s->last_displayed_line -
5649 s->first_displayed_line - s->selected_line + 1);
5651 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5652 s->first_displayed_line += nscroll;
5653 else
5654 s->first_displayed_line =
5655 s->blame.nlines - (view->nlines - 3);
5656 break;
5657 case KEY_RESIZE:
5658 if (s->selected_line > view->nlines - 2) {
5659 s->selected_line = MIN(s->blame.nlines,
5660 view->nlines - 2);
5662 break;
5663 default:
5664 view->count = 0;
5665 break;
5667 return thread_err ? thread_err : err;
5670 static const struct got_error *
5671 reset_blame_view(struct tog_view *view)
5673 const struct got_error *err;
5674 struct tog_blame_view_state *s = &view->state.blame;
5676 view->count = 0;
5677 s->done = 1;
5678 err = stop_blame(&s->blame);
5679 s->done = 0;
5680 if (err)
5681 return err;
5682 return run_blame(view);
5685 static const struct got_error *
5686 cmd_blame(int argc, char *argv[])
5688 const struct got_error *error;
5689 struct got_repository *repo = NULL;
5690 struct got_worktree *worktree = NULL;
5691 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5692 char *link_target = NULL;
5693 struct got_object_id *commit_id = NULL;
5694 struct got_commit_object *commit = NULL;
5695 char *commit_id_str = NULL;
5696 int ch;
5697 struct tog_view *view;
5698 int *pack_fds = NULL;
5700 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5701 switch (ch) {
5702 case 'c':
5703 commit_id_str = optarg;
5704 break;
5705 case 'r':
5706 repo_path = realpath(optarg, NULL);
5707 if (repo_path == NULL)
5708 return got_error_from_errno2("realpath",
5709 optarg);
5710 break;
5711 default:
5712 usage_blame();
5713 /* NOTREACHED */
5717 argc -= optind;
5718 argv += optind;
5720 if (argc != 1)
5721 usage_blame();
5723 error = got_repo_pack_fds_open(&pack_fds);
5724 if (error != NULL)
5725 goto done;
5727 if (repo_path == NULL) {
5728 cwd = getcwd(NULL, 0);
5729 if (cwd == NULL)
5730 return got_error_from_errno("getcwd");
5731 error = got_worktree_open(&worktree, cwd);
5732 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5733 goto done;
5734 if (worktree)
5735 repo_path =
5736 strdup(got_worktree_get_repo_path(worktree));
5737 else
5738 repo_path = strdup(cwd);
5739 if (repo_path == NULL) {
5740 error = got_error_from_errno("strdup");
5741 goto done;
5745 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5746 if (error != NULL)
5747 goto done;
5749 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5750 worktree);
5751 if (error)
5752 goto done;
5754 init_curses();
5756 error = apply_unveil(got_repo_get_path(repo), NULL);
5757 if (error)
5758 goto done;
5760 error = tog_load_refs(repo, 0);
5761 if (error)
5762 goto done;
5764 if (commit_id_str == NULL) {
5765 struct got_reference *head_ref;
5766 error = got_ref_open(&head_ref, repo, worktree ?
5767 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5768 if (error != NULL)
5769 goto done;
5770 error = got_ref_resolve(&commit_id, repo, head_ref);
5771 got_ref_close(head_ref);
5772 } else {
5773 error = got_repo_match_object_id(&commit_id, NULL,
5774 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5776 if (error != NULL)
5777 goto done;
5779 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5780 if (view == NULL) {
5781 error = got_error_from_errno("view_open");
5782 goto done;
5785 error = got_object_open_as_commit(&commit, repo, commit_id);
5786 if (error)
5787 goto done;
5789 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5790 commit, repo);
5791 if (error)
5792 goto done;
5794 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5795 commit_id, repo);
5796 if (error)
5797 goto done;
5798 if (worktree) {
5799 /* Release work tree lock. */
5800 got_worktree_close(worktree);
5801 worktree = NULL;
5803 error = view_loop(view);
5804 done:
5805 free(repo_path);
5806 free(in_repo_path);
5807 free(link_target);
5808 free(cwd);
5809 free(commit_id);
5810 if (commit)
5811 got_object_commit_close(commit);
5812 if (worktree)
5813 got_worktree_close(worktree);
5814 if (repo) {
5815 const struct got_error *close_err = got_repo_close(repo);
5816 if (error == NULL)
5817 error = close_err;
5819 if (pack_fds) {
5820 const struct got_error *pack_err =
5821 got_repo_pack_fds_close(pack_fds);
5822 if (error == NULL)
5823 error = pack_err;
5825 tog_free_refs();
5826 return error;
5829 static const struct got_error *
5830 draw_tree_entries(struct tog_view *view, const char *parent_path)
5832 struct tog_tree_view_state *s = &view->state.tree;
5833 const struct got_error *err = NULL;
5834 struct got_tree_entry *te;
5835 wchar_t *wline;
5836 struct tog_color *tc;
5837 int width, n, i, nentries;
5838 int limit = view->nlines;
5840 s->ndisplayed = 0;
5841 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5842 view_is_splitscreen(view->child))
5843 --limit; /* border */
5845 werase(view->window);
5847 if (limit == 0)
5848 return NULL;
5850 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5851 0, 0);
5852 if (err)
5853 return err;
5854 if (view_needs_focus_indication(view))
5855 wstandout(view->window);
5856 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5857 if (tc)
5858 wattr_on(view->window,
5859 COLOR_PAIR(tc->colorpair), NULL);
5860 waddwstr(view->window, wline);
5861 if (tc)
5862 wattr_off(view->window,
5863 COLOR_PAIR(tc->colorpair), NULL);
5864 if (view_needs_focus_indication(view))
5865 wstandend(view->window);
5866 free(wline);
5867 wline = NULL;
5868 if (width < view->ncols - 1)
5869 waddch(view->window, '\n');
5870 if (--limit <= 0)
5871 return NULL;
5872 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5873 0, 0);
5874 if (err)
5875 return err;
5876 waddwstr(view->window, wline);
5877 free(wline);
5878 wline = NULL;
5879 if (width < view->ncols - 1)
5880 waddch(view->window, '\n');
5881 if (--limit <= 0)
5882 return NULL;
5883 waddch(view->window, '\n');
5884 if (--limit <= 0)
5885 return NULL;
5887 if (s->first_displayed_entry == NULL) {
5888 te = got_object_tree_get_first_entry(s->tree);
5889 if (s->selected == 0) {
5890 if (view->focussed)
5891 wstandout(view->window);
5892 s->selected_entry = NULL;
5894 waddstr(view->window, " ..\n"); /* parent directory */
5895 if (s->selected == 0 && view->focussed)
5896 wstandend(view->window);
5897 s->ndisplayed++;
5898 if (--limit <= 0)
5899 return NULL;
5900 n = 1;
5901 } else {
5902 n = 0;
5903 te = s->first_displayed_entry;
5906 nentries = got_object_tree_get_nentries(s->tree);
5907 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5908 char *line = NULL, *id_str = NULL, *link_target = NULL;
5909 const char *modestr = "";
5910 mode_t mode;
5912 te = got_object_tree_get_entry(s->tree, i);
5913 mode = got_tree_entry_get_mode(te);
5915 if (s->show_ids) {
5916 err = got_object_id_str(&id_str,
5917 got_tree_entry_get_id(te));
5918 if (err)
5919 return got_error_from_errno(
5920 "got_object_id_str");
5922 if (got_object_tree_entry_is_submodule(te))
5923 modestr = "$";
5924 else if (S_ISLNK(mode)) {
5925 int i;
5927 err = got_tree_entry_get_symlink_target(&link_target,
5928 te, s->repo);
5929 if (err) {
5930 free(id_str);
5931 return err;
5933 for (i = 0; i < strlen(link_target); i++) {
5934 if (!isprint((unsigned char)link_target[i]))
5935 link_target[i] = '?';
5937 modestr = "@";
5939 else if (S_ISDIR(mode))
5940 modestr = "/";
5941 else if (mode & S_IXUSR)
5942 modestr = "*";
5943 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5944 got_tree_entry_get_name(te), modestr,
5945 link_target ? " -> ": "",
5946 link_target ? link_target : "") == -1) {
5947 free(id_str);
5948 free(link_target);
5949 return got_error_from_errno("asprintf");
5951 free(id_str);
5952 free(link_target);
5953 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5954 0, 0);
5955 if (err) {
5956 free(line);
5957 break;
5959 if (n == s->selected) {
5960 if (view->focussed)
5961 wstandout(view->window);
5962 s->selected_entry = te;
5964 tc = match_color(&s->colors, line);
5965 if (tc)
5966 wattr_on(view->window,
5967 COLOR_PAIR(tc->colorpair), NULL);
5968 waddwstr(view->window, wline);
5969 if (tc)
5970 wattr_off(view->window,
5971 COLOR_PAIR(tc->colorpair), NULL);
5972 if (width < view->ncols - 1)
5973 waddch(view->window, '\n');
5974 if (n == s->selected && view->focussed)
5975 wstandend(view->window);
5976 free(line);
5977 free(wline);
5978 wline = NULL;
5979 n++;
5980 s->ndisplayed++;
5981 s->last_displayed_entry = te;
5982 if (--limit <= 0)
5983 break;
5986 return err;
5989 static void
5990 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5992 struct got_tree_entry *te;
5993 int isroot = s->tree == s->root;
5994 int i = 0;
5996 if (s->first_displayed_entry == NULL)
5997 return;
5999 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6000 while (i++ < maxscroll) {
6001 if (te == NULL) {
6002 if (!isroot)
6003 s->first_displayed_entry = NULL;
6004 break;
6006 s->first_displayed_entry = te;
6007 te = got_tree_entry_get_prev(s->tree, te);
6011 static const struct got_error *
6012 tree_scroll_down(struct tog_view *view, int maxscroll)
6014 struct tog_tree_view_state *s = &view->state.tree;
6015 struct got_tree_entry *next, *last;
6016 int n = 0;
6018 if (s->first_displayed_entry)
6019 next = got_tree_entry_get_next(s->tree,
6020 s->first_displayed_entry);
6021 else
6022 next = got_object_tree_get_first_entry(s->tree);
6024 last = s->last_displayed_entry;
6025 while (next && n++ < maxscroll) {
6026 if (last)
6027 last = got_tree_entry_get_next(s->tree, last);
6028 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6029 s->first_displayed_entry = next;
6030 next = got_tree_entry_get_next(s->tree, next);
6034 return NULL;
6037 static const struct got_error *
6038 tree_entry_path(char **path, struct tog_parent_trees *parents,
6039 struct got_tree_entry *te)
6041 const struct got_error *err = NULL;
6042 struct tog_parent_tree *pt;
6043 size_t len = 2; /* for leading slash and NUL */
6045 TAILQ_FOREACH(pt, parents, entry)
6046 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6047 + 1 /* slash */;
6048 if (te)
6049 len += strlen(got_tree_entry_get_name(te));
6051 *path = calloc(1, len);
6052 if (path == NULL)
6053 return got_error_from_errno("calloc");
6055 (*path)[0] = '/';
6056 pt = TAILQ_LAST(parents, tog_parent_trees);
6057 while (pt) {
6058 const char *name = got_tree_entry_get_name(pt->selected_entry);
6059 if (strlcat(*path, name, len) >= len) {
6060 err = got_error(GOT_ERR_NO_SPACE);
6061 goto done;
6063 if (strlcat(*path, "/", len) >= len) {
6064 err = got_error(GOT_ERR_NO_SPACE);
6065 goto done;
6067 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6069 if (te) {
6070 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6071 err = got_error(GOT_ERR_NO_SPACE);
6072 goto done;
6075 done:
6076 if (err) {
6077 free(*path);
6078 *path = NULL;
6080 return err;
6083 static const struct got_error *
6084 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6085 struct got_tree_entry *te, struct tog_parent_trees *parents,
6086 struct got_object_id *commit_id, struct got_repository *repo)
6088 const struct got_error *err = NULL;
6089 char *path;
6090 struct tog_view *blame_view;
6092 *new_view = NULL;
6094 err = tree_entry_path(&path, parents, te);
6095 if (err)
6096 return err;
6098 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6099 if (blame_view == NULL) {
6100 err = got_error_from_errno("view_open");
6101 goto done;
6104 err = open_blame_view(blame_view, path, commit_id, repo);
6105 if (err) {
6106 if (err->code == GOT_ERR_CANCELLED)
6107 err = NULL;
6108 view_close(blame_view);
6109 } else
6110 *new_view = blame_view;
6111 done:
6112 free(path);
6113 return err;
6116 static const struct got_error *
6117 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
6118 struct tog_tree_view_state *s)
6120 struct tog_view *log_view;
6121 const struct got_error *err = NULL;
6122 char *path;
6124 *new_view = NULL;
6126 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6127 if (log_view == NULL)
6128 return got_error_from_errno("view_open");
6130 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6131 if (err)
6132 return err;
6134 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6135 path, 0);
6136 if (err)
6137 view_close(log_view);
6138 else
6139 *new_view = log_view;
6140 free(path);
6141 return err;
6144 static const struct got_error *
6145 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6146 const char *head_ref_name, struct got_repository *repo)
6148 const struct got_error *err = NULL;
6149 char *commit_id_str = NULL;
6150 struct tog_tree_view_state *s = &view->state.tree;
6151 struct got_commit_object *commit = NULL;
6153 TAILQ_INIT(&s->parents);
6154 STAILQ_INIT(&s->colors);
6156 s->commit_id = got_object_id_dup(commit_id);
6157 if (s->commit_id == NULL)
6158 return got_error_from_errno("got_object_id_dup");
6160 err = got_object_open_as_commit(&commit, repo, commit_id);
6161 if (err)
6162 goto done;
6165 * The root is opened here and will be closed when the view is closed.
6166 * Any visited subtrees and their path-wise parents are opened and
6167 * closed on demand.
6169 err = got_object_open_as_tree(&s->root, repo,
6170 got_object_commit_get_tree_id(commit));
6171 if (err)
6172 goto done;
6173 s->tree = s->root;
6175 err = got_object_id_str(&commit_id_str, commit_id);
6176 if (err != NULL)
6177 goto done;
6179 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6180 err = got_error_from_errno("asprintf");
6181 goto done;
6184 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6185 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6186 if (head_ref_name) {
6187 s->head_ref_name = strdup(head_ref_name);
6188 if (s->head_ref_name == NULL) {
6189 err = got_error_from_errno("strdup");
6190 goto done;
6193 s->repo = repo;
6195 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6196 err = add_color(&s->colors, "\\$$",
6197 TOG_COLOR_TREE_SUBMODULE,
6198 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6199 if (err)
6200 goto done;
6201 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6202 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6203 if (err)
6204 goto done;
6205 err = add_color(&s->colors, "/$",
6206 TOG_COLOR_TREE_DIRECTORY,
6207 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6208 if (err)
6209 goto done;
6211 err = add_color(&s->colors, "\\*$",
6212 TOG_COLOR_TREE_EXECUTABLE,
6213 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6214 if (err)
6215 goto done;
6217 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6218 get_color_value("TOG_COLOR_COMMIT"));
6219 if (err)
6220 goto done;
6223 view->show = show_tree_view;
6224 view->input = input_tree_view;
6225 view->close = close_tree_view;
6226 view->search_start = search_start_tree_view;
6227 view->search_next = search_next_tree_view;
6228 done:
6229 free(commit_id_str);
6230 if (commit)
6231 got_object_commit_close(commit);
6232 if (err)
6233 close_tree_view(view);
6234 return err;
6237 static const struct got_error *
6238 close_tree_view(struct tog_view *view)
6240 struct tog_tree_view_state *s = &view->state.tree;
6242 free_colors(&s->colors);
6243 free(s->tree_label);
6244 s->tree_label = NULL;
6245 free(s->commit_id);
6246 s->commit_id = NULL;
6247 free(s->head_ref_name);
6248 s->head_ref_name = NULL;
6249 while (!TAILQ_EMPTY(&s->parents)) {
6250 struct tog_parent_tree *parent;
6251 parent = TAILQ_FIRST(&s->parents);
6252 TAILQ_REMOVE(&s->parents, parent, entry);
6253 if (parent->tree != s->root)
6254 got_object_tree_close(parent->tree);
6255 free(parent);
6258 if (s->tree != NULL && s->tree != s->root)
6259 got_object_tree_close(s->tree);
6260 if (s->root)
6261 got_object_tree_close(s->root);
6262 return NULL;
6265 static const struct got_error *
6266 search_start_tree_view(struct tog_view *view)
6268 struct tog_tree_view_state *s = &view->state.tree;
6270 s->matched_entry = NULL;
6271 return NULL;
6274 static int
6275 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6277 regmatch_t regmatch;
6279 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6280 0) == 0;
6283 static const struct got_error *
6284 search_next_tree_view(struct tog_view *view)
6286 struct tog_tree_view_state *s = &view->state.tree;
6287 struct got_tree_entry *te = NULL;
6289 if (!view->searching) {
6290 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6291 return NULL;
6294 if (s->matched_entry) {
6295 if (view->searching == TOG_SEARCH_FORWARD) {
6296 if (s->selected_entry)
6297 te = got_tree_entry_get_next(s->tree,
6298 s->selected_entry);
6299 else
6300 te = got_object_tree_get_first_entry(s->tree);
6301 } else {
6302 if (s->selected_entry == NULL)
6303 te = got_object_tree_get_last_entry(s->tree);
6304 else
6305 te = got_tree_entry_get_prev(s->tree,
6306 s->selected_entry);
6308 } else {
6309 if (s->selected_entry)
6310 te = s->selected_entry;
6311 else if (view->searching == TOG_SEARCH_FORWARD)
6312 te = got_object_tree_get_first_entry(s->tree);
6313 else
6314 te = got_object_tree_get_last_entry(s->tree);
6317 while (1) {
6318 if (te == NULL) {
6319 if (s->matched_entry == NULL) {
6320 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6321 return NULL;
6323 if (view->searching == TOG_SEARCH_FORWARD)
6324 te = got_object_tree_get_first_entry(s->tree);
6325 else
6326 te = got_object_tree_get_last_entry(s->tree);
6329 if (match_tree_entry(te, &view->regex)) {
6330 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6331 s->matched_entry = te;
6332 break;
6335 if (view->searching == TOG_SEARCH_FORWARD)
6336 te = got_tree_entry_get_next(s->tree, te);
6337 else
6338 te = got_tree_entry_get_prev(s->tree, te);
6341 if (s->matched_entry) {
6342 s->first_displayed_entry = s->matched_entry;
6343 s->selected = 0;
6346 return NULL;
6349 static const struct got_error *
6350 show_tree_view(struct tog_view *view)
6352 const struct got_error *err = NULL;
6353 struct tog_tree_view_state *s = &view->state.tree;
6354 char *parent_path;
6356 err = tree_entry_path(&parent_path, &s->parents, NULL);
6357 if (err)
6358 return err;
6360 err = draw_tree_entries(view, parent_path);
6361 free(parent_path);
6363 view_border(view);
6364 return err;
6367 static const struct got_error *
6368 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6370 const struct got_error *err = NULL;
6371 struct tog_tree_view_state *s = &view->state.tree;
6372 struct tog_view *log_view, *ref_view;
6373 struct got_tree_entry *te;
6374 int begin_x = 0, n, nscroll = view->nlines - 3;
6376 switch (ch) {
6377 case 'i':
6378 s->show_ids = !s->show_ids;
6379 view->count = 0;
6380 break;
6381 case 'l':
6382 view->count = 0;
6383 if (!s->selected_entry)
6384 break;
6385 if (view_is_parent_view(view))
6386 begin_x = view_split_begin_x(view->begin_x);
6387 err = log_selected_tree_entry(&log_view, begin_x, s);
6388 view->focussed = 0;
6389 log_view->focussed = 1;
6390 if (view_is_parent_view(view)) {
6391 err = view_close_child(view);
6392 if (err)
6393 return err;
6394 err = view_set_child(view, log_view);
6395 if (err)
6396 return err;
6397 view->focus_child = 1;
6398 } else
6399 *new_view = log_view;
6400 break;
6401 case 'r':
6402 view->count = 0;
6403 if (view_is_parent_view(view))
6404 begin_x = view_split_begin_x(view->begin_x);
6405 ref_view = view_open(view->nlines, view->ncols,
6406 view->begin_y, begin_x, TOG_VIEW_REF);
6407 if (ref_view == NULL)
6408 return got_error_from_errno("view_open");
6409 err = open_ref_view(ref_view, s->repo);
6410 if (err) {
6411 view_close(ref_view);
6412 return err;
6414 view->focussed = 0;
6415 ref_view->focussed = 1;
6416 if (view_is_parent_view(view)) {
6417 err = view_close_child(view);
6418 if (err)
6419 return err;
6420 err = view_set_child(view, ref_view);
6421 if (err)
6422 return err;
6423 view->focus_child = 1;
6424 } else
6425 *new_view = ref_view;
6426 break;
6427 case 'g':
6428 case KEY_HOME:
6429 s->selected = 0;
6430 view->count = 0;
6431 if (s->tree == s->root)
6432 s->first_displayed_entry =
6433 got_object_tree_get_first_entry(s->tree);
6434 else
6435 s->first_displayed_entry = NULL;
6436 break;
6437 case 'G':
6438 case KEY_END: {
6439 int eos = view->nlines - 3;
6441 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6442 --eos; /* border */
6443 s->selected = 0;
6444 view->count = 0;
6445 te = got_object_tree_get_last_entry(s->tree);
6446 for (n = 0; n < eos; n++) {
6447 if (te == NULL) {
6448 if(s->tree != s->root) {
6449 s->first_displayed_entry = NULL;
6450 n++;
6452 break;
6454 s->first_displayed_entry = te;
6455 te = got_tree_entry_get_prev(s->tree, te);
6457 if (n > 0)
6458 s->selected = n - 1;
6459 break;
6461 case 'k':
6462 case KEY_UP:
6463 case CTRL('p'):
6464 if (s->selected > 0) {
6465 s->selected--;
6466 break;
6468 tree_scroll_up(s, 1);
6469 if (s->selected_entry == NULL ||
6470 (s->tree == s->root && s->selected_entry ==
6471 got_object_tree_get_first_entry(s->tree)))
6472 view->count = 0;
6473 break;
6474 case CTRL('u'):
6475 case 'u':
6476 nscroll /= 2;
6477 /* FALL THROUGH */
6478 case KEY_PPAGE:
6479 case CTRL('b'):
6480 case 'b':
6481 if (s->tree == s->root) {
6482 if (got_object_tree_get_first_entry(s->tree) ==
6483 s->first_displayed_entry)
6484 s->selected -= MIN(s->selected, nscroll);
6485 } else {
6486 if (s->first_displayed_entry == NULL)
6487 s->selected -= MIN(s->selected, nscroll);
6489 tree_scroll_up(s, MAX(0, nscroll));
6490 if (s->selected_entry == NULL ||
6491 (s->tree == s->root && s->selected_entry ==
6492 got_object_tree_get_first_entry(s->tree)))
6493 view->count = 0;
6494 break;
6495 case 'j':
6496 case KEY_DOWN:
6497 case CTRL('n'):
6498 if (s->selected < s->ndisplayed - 1) {
6499 s->selected++;
6500 break;
6502 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6503 == NULL) {
6504 /* can't scroll any further */
6505 view->count = 0;
6506 break;
6508 tree_scroll_down(view, 1);
6509 break;
6510 case CTRL('d'):
6511 case 'd':
6512 nscroll /= 2;
6513 /* FALL THROUGH */
6514 case KEY_NPAGE:
6515 case CTRL('f'):
6516 case 'f':
6517 case ' ':
6518 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6519 == NULL) {
6520 /* can't scroll any further; move cursor down */
6521 if (s->selected < s->ndisplayed - 1)
6522 s->selected += MIN(nscroll,
6523 s->ndisplayed - s->selected - 1);
6524 else
6525 view->count = 0;
6526 break;
6528 tree_scroll_down(view, nscroll);
6529 break;
6530 case KEY_ENTER:
6531 case '\r':
6532 case KEY_BACKSPACE:
6533 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6534 struct tog_parent_tree *parent;
6535 /* user selected '..' */
6536 if (s->tree == s->root) {
6537 view->count = 0;
6538 break;
6540 parent = TAILQ_FIRST(&s->parents);
6541 TAILQ_REMOVE(&s->parents, parent,
6542 entry);
6543 got_object_tree_close(s->tree);
6544 s->tree = parent->tree;
6545 s->first_displayed_entry =
6546 parent->first_displayed_entry;
6547 s->selected_entry =
6548 parent->selected_entry;
6549 s->selected = parent->selected;
6550 if (s->selected > view->nlines - 3) {
6551 err = offset_selection_down(view);
6552 if (err)
6553 break;
6555 free(parent);
6556 } else if (S_ISDIR(got_tree_entry_get_mode(
6557 s->selected_entry))) {
6558 struct got_tree_object *subtree;
6559 view->count = 0;
6560 err = got_object_open_as_tree(&subtree, s->repo,
6561 got_tree_entry_get_id(s->selected_entry));
6562 if (err)
6563 break;
6564 err = tree_view_visit_subtree(s, subtree);
6565 if (err) {
6566 got_object_tree_close(subtree);
6567 break;
6569 } else if (S_ISREG(got_tree_entry_get_mode(
6570 s->selected_entry))) {
6571 struct tog_view *blame_view;
6572 int begin_x = 0, begin_y = 0;
6574 if (view_is_parent_view(view))
6575 view_get_split(view, &begin_y, &begin_x);
6577 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6578 s->selected_entry, &s->parents,
6579 s->commit_id, s->repo);
6580 if (err)
6581 break;
6583 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
6584 err = view_init_hsplit(view, begin_y);
6585 if (err)
6586 break;
6589 view->count = 0;
6590 view->focussed = 0;
6591 blame_view->focussed = 1;
6592 blame_view->mode = view->mode;
6593 blame_view->nlines = view->lines - begin_y;
6594 if (view_is_parent_view(view)) {
6595 err = view_close_child(view);
6596 if (err)
6597 return err;
6598 err = view_set_child(view, blame_view);
6599 if (err)
6600 return err;
6601 view->focus_child = 1;
6602 } else
6603 *new_view = blame_view;
6605 break;
6606 case KEY_RESIZE:
6607 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6608 s->selected = view->nlines - 4;
6609 view->count = 0;
6610 break;
6611 default:
6612 view->count = 0;
6613 break;
6616 return err;
6619 __dead static void
6620 usage_tree(void)
6622 endwin();
6623 fprintf(stderr,
6624 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6625 getprogname());
6626 exit(1);
6629 static const struct got_error *
6630 cmd_tree(int argc, char *argv[])
6632 const struct got_error *error;
6633 struct got_repository *repo = NULL;
6634 struct got_worktree *worktree = NULL;
6635 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6636 struct got_object_id *commit_id = NULL;
6637 struct got_commit_object *commit = NULL;
6638 const char *commit_id_arg = NULL;
6639 char *label = NULL;
6640 struct got_reference *ref = NULL;
6641 const char *head_ref_name = NULL;
6642 int ch;
6643 struct tog_view *view;
6644 int *pack_fds = NULL;
6646 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6647 switch (ch) {
6648 case 'c':
6649 commit_id_arg = optarg;
6650 break;
6651 case 'r':
6652 repo_path = realpath(optarg, NULL);
6653 if (repo_path == NULL)
6654 return got_error_from_errno2("realpath",
6655 optarg);
6656 break;
6657 default:
6658 usage_tree();
6659 /* NOTREACHED */
6663 argc -= optind;
6664 argv += optind;
6666 if (argc > 1)
6667 usage_tree();
6669 error = got_repo_pack_fds_open(&pack_fds);
6670 if (error != NULL)
6671 goto done;
6673 if (repo_path == NULL) {
6674 cwd = getcwd(NULL, 0);
6675 if (cwd == NULL)
6676 return got_error_from_errno("getcwd");
6677 error = got_worktree_open(&worktree, cwd);
6678 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6679 goto done;
6680 if (worktree)
6681 repo_path =
6682 strdup(got_worktree_get_repo_path(worktree));
6683 else
6684 repo_path = strdup(cwd);
6685 if (repo_path == NULL) {
6686 error = got_error_from_errno("strdup");
6687 goto done;
6691 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6692 if (error != NULL)
6693 goto done;
6695 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6696 repo, worktree);
6697 if (error)
6698 goto done;
6700 init_curses();
6702 error = apply_unveil(got_repo_get_path(repo), NULL);
6703 if (error)
6704 goto done;
6706 error = tog_load_refs(repo, 0);
6707 if (error)
6708 goto done;
6710 if (commit_id_arg == NULL) {
6711 error = got_repo_match_object_id(&commit_id, &label,
6712 worktree ? got_worktree_get_head_ref_name(worktree) :
6713 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6714 if (error)
6715 goto done;
6716 head_ref_name = label;
6717 } else {
6718 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6719 if (error == NULL)
6720 head_ref_name = got_ref_get_name(ref);
6721 else if (error->code != GOT_ERR_NOT_REF)
6722 goto done;
6723 error = got_repo_match_object_id(&commit_id, NULL,
6724 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6725 if (error)
6726 goto done;
6729 error = got_object_open_as_commit(&commit, repo, commit_id);
6730 if (error)
6731 goto done;
6733 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6734 if (view == NULL) {
6735 error = got_error_from_errno("view_open");
6736 goto done;
6738 error = open_tree_view(view, commit_id, head_ref_name, repo);
6739 if (error)
6740 goto done;
6741 if (!got_path_is_root_dir(in_repo_path)) {
6742 error = tree_view_walk_path(&view->state.tree, commit,
6743 in_repo_path);
6744 if (error)
6745 goto done;
6748 if (worktree) {
6749 /* Release work tree lock. */
6750 got_worktree_close(worktree);
6751 worktree = NULL;
6753 error = view_loop(view);
6754 done:
6755 free(repo_path);
6756 free(cwd);
6757 free(commit_id);
6758 free(label);
6759 if (ref)
6760 got_ref_close(ref);
6761 if (repo) {
6762 const struct got_error *close_err = got_repo_close(repo);
6763 if (error == NULL)
6764 error = close_err;
6766 if (pack_fds) {
6767 const struct got_error *pack_err =
6768 got_repo_pack_fds_close(pack_fds);
6769 if (error == NULL)
6770 error = pack_err;
6772 tog_free_refs();
6773 return error;
6776 static const struct got_error *
6777 ref_view_load_refs(struct tog_ref_view_state *s)
6779 struct got_reflist_entry *sre;
6780 struct tog_reflist_entry *re;
6782 s->nrefs = 0;
6783 TAILQ_FOREACH(sre, &tog_refs, entry) {
6784 if (strncmp(got_ref_get_name(sre->ref),
6785 "refs/got/", 9) == 0 &&
6786 strncmp(got_ref_get_name(sre->ref),
6787 "refs/got/backup/", 16) != 0)
6788 continue;
6790 re = malloc(sizeof(*re));
6791 if (re == NULL)
6792 return got_error_from_errno("malloc");
6794 re->ref = got_ref_dup(sre->ref);
6795 if (re->ref == NULL)
6796 return got_error_from_errno("got_ref_dup");
6797 re->idx = s->nrefs++;
6798 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6801 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6802 return NULL;
6805 static void
6806 ref_view_free_refs(struct tog_ref_view_state *s)
6808 struct tog_reflist_entry *re;
6810 while (!TAILQ_EMPTY(&s->refs)) {
6811 re = TAILQ_FIRST(&s->refs);
6812 TAILQ_REMOVE(&s->refs, re, entry);
6813 got_ref_close(re->ref);
6814 free(re);
6818 static const struct got_error *
6819 open_ref_view(struct tog_view *view, struct got_repository *repo)
6821 const struct got_error *err = NULL;
6822 struct tog_ref_view_state *s = &view->state.ref;
6824 s->selected_entry = 0;
6825 s->repo = repo;
6827 TAILQ_INIT(&s->refs);
6828 STAILQ_INIT(&s->colors);
6830 err = ref_view_load_refs(s);
6831 if (err)
6832 return err;
6834 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6835 err = add_color(&s->colors, "^refs/heads/",
6836 TOG_COLOR_REFS_HEADS,
6837 get_color_value("TOG_COLOR_REFS_HEADS"));
6838 if (err)
6839 goto done;
6841 err = add_color(&s->colors, "^refs/tags/",
6842 TOG_COLOR_REFS_TAGS,
6843 get_color_value("TOG_COLOR_REFS_TAGS"));
6844 if (err)
6845 goto done;
6847 err = add_color(&s->colors, "^refs/remotes/",
6848 TOG_COLOR_REFS_REMOTES,
6849 get_color_value("TOG_COLOR_REFS_REMOTES"));
6850 if (err)
6851 goto done;
6853 err = add_color(&s->colors, "^refs/got/backup/",
6854 TOG_COLOR_REFS_BACKUP,
6855 get_color_value("TOG_COLOR_REFS_BACKUP"));
6856 if (err)
6857 goto done;
6860 view->show = show_ref_view;
6861 view->input = input_ref_view;
6862 view->close = close_ref_view;
6863 view->search_start = search_start_ref_view;
6864 view->search_next = search_next_ref_view;
6865 done:
6866 if (err)
6867 free_colors(&s->colors);
6868 return err;
6871 static const struct got_error *
6872 close_ref_view(struct tog_view *view)
6874 struct tog_ref_view_state *s = &view->state.ref;
6876 ref_view_free_refs(s);
6877 free_colors(&s->colors);
6879 return NULL;
6882 static const struct got_error *
6883 resolve_reflist_entry(struct got_object_id **commit_id,
6884 struct tog_reflist_entry *re, struct got_repository *repo)
6886 const struct got_error *err = NULL;
6887 struct got_object_id *obj_id;
6888 struct got_tag_object *tag = NULL;
6889 int obj_type;
6891 *commit_id = NULL;
6893 err = got_ref_resolve(&obj_id, repo, re->ref);
6894 if (err)
6895 return err;
6897 err = got_object_get_type(&obj_type, repo, obj_id);
6898 if (err)
6899 goto done;
6901 switch (obj_type) {
6902 case GOT_OBJ_TYPE_COMMIT:
6903 *commit_id = obj_id;
6904 break;
6905 case GOT_OBJ_TYPE_TAG:
6906 err = got_object_open_as_tag(&tag, repo, obj_id);
6907 if (err)
6908 goto done;
6909 free(obj_id);
6910 err = got_object_get_type(&obj_type, repo,
6911 got_object_tag_get_object_id(tag));
6912 if (err)
6913 goto done;
6914 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6915 err = got_error(GOT_ERR_OBJ_TYPE);
6916 goto done;
6918 *commit_id = got_object_id_dup(
6919 got_object_tag_get_object_id(tag));
6920 if (*commit_id == NULL) {
6921 err = got_error_from_errno("got_object_id_dup");
6922 goto done;
6924 break;
6925 default:
6926 err = got_error(GOT_ERR_OBJ_TYPE);
6927 break;
6930 done:
6931 if (tag)
6932 got_object_tag_close(tag);
6933 if (err) {
6934 free(*commit_id);
6935 *commit_id = NULL;
6937 return err;
6940 static const struct got_error *
6941 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6942 struct tog_reflist_entry *re, struct got_repository *repo)
6944 struct tog_view *log_view;
6945 const struct got_error *err = NULL;
6946 struct got_object_id *commit_id = NULL;
6948 *new_view = NULL;
6950 err = resolve_reflist_entry(&commit_id, re, repo);
6951 if (err) {
6952 if (err->code != GOT_ERR_OBJ_TYPE)
6953 return err;
6954 else
6955 return NULL;
6958 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6959 if (log_view == NULL) {
6960 err = got_error_from_errno("view_open");
6961 goto done;
6964 err = open_log_view(log_view, commit_id, repo,
6965 got_ref_get_name(re->ref), "", 0);
6966 done:
6967 if (err)
6968 view_close(log_view);
6969 else
6970 *new_view = log_view;
6971 free(commit_id);
6972 return err;
6975 static void
6976 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6978 struct tog_reflist_entry *re;
6979 int i = 0;
6981 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6982 return;
6984 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6985 while (i++ < maxscroll) {
6986 if (re == NULL)
6987 break;
6988 s->first_displayed_entry = re;
6989 re = TAILQ_PREV(re, tog_reflist_head, entry);
6993 static const struct got_error *
6994 ref_scroll_down(struct tog_view *view, int maxscroll)
6996 struct tog_ref_view_state *s = &view->state.ref;
6997 struct tog_reflist_entry *next, *last;
6998 int n = 0;
7000 if (s->first_displayed_entry)
7001 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7002 else
7003 next = TAILQ_FIRST(&s->refs);
7005 last = s->last_displayed_entry;
7006 while (next && n++ < maxscroll) {
7007 if (last)
7008 last = TAILQ_NEXT(last, entry);
7009 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7010 s->first_displayed_entry = next;
7011 next = TAILQ_NEXT(next, entry);
7015 return NULL;
7018 static const struct got_error *
7019 search_start_ref_view(struct tog_view *view)
7021 struct tog_ref_view_state *s = &view->state.ref;
7023 s->matched_entry = NULL;
7024 return NULL;
7027 static int
7028 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7030 regmatch_t regmatch;
7032 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7033 0) == 0;
7036 static const struct got_error *
7037 search_next_ref_view(struct tog_view *view)
7039 struct tog_ref_view_state *s = &view->state.ref;
7040 struct tog_reflist_entry *re = NULL;
7042 if (!view->searching) {
7043 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7044 return NULL;
7047 if (s->matched_entry) {
7048 if (view->searching == TOG_SEARCH_FORWARD) {
7049 if (s->selected_entry)
7050 re = TAILQ_NEXT(s->selected_entry, entry);
7051 else
7052 re = TAILQ_PREV(s->selected_entry,
7053 tog_reflist_head, entry);
7054 } else {
7055 if (s->selected_entry == NULL)
7056 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7057 else
7058 re = TAILQ_PREV(s->selected_entry,
7059 tog_reflist_head, entry);
7061 } else {
7062 if (s->selected_entry)
7063 re = s->selected_entry;
7064 else if (view->searching == TOG_SEARCH_FORWARD)
7065 re = TAILQ_FIRST(&s->refs);
7066 else
7067 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7070 while (1) {
7071 if (re == NULL) {
7072 if (s->matched_entry == NULL) {
7073 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7074 return NULL;
7076 if (view->searching == TOG_SEARCH_FORWARD)
7077 re = TAILQ_FIRST(&s->refs);
7078 else
7079 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7082 if (match_reflist_entry(re, &view->regex)) {
7083 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7084 s->matched_entry = re;
7085 break;
7088 if (view->searching == TOG_SEARCH_FORWARD)
7089 re = TAILQ_NEXT(re, entry);
7090 else
7091 re = TAILQ_PREV(re, tog_reflist_head, entry);
7094 if (s->matched_entry) {
7095 s->first_displayed_entry = s->matched_entry;
7096 s->selected = 0;
7099 return NULL;
7102 static const struct got_error *
7103 show_ref_view(struct tog_view *view)
7105 const struct got_error *err = NULL;
7106 struct tog_ref_view_state *s = &view->state.ref;
7107 struct tog_reflist_entry *re;
7108 char *line = NULL;
7109 wchar_t *wline;
7110 struct tog_color *tc;
7111 int width, n;
7112 int limit = view->nlines;
7114 werase(view->window);
7116 s->ndisplayed = 0;
7117 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
7118 view_is_splitscreen(view->child))
7119 --limit; /* border */
7121 if (limit == 0)
7122 return NULL;
7124 re = s->first_displayed_entry;
7126 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7127 s->nrefs) == -1)
7128 return got_error_from_errno("asprintf");
7130 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7131 if (err) {
7132 free(line);
7133 return err;
7135 if (view_needs_focus_indication(view))
7136 wstandout(view->window);
7137 waddwstr(view->window, wline);
7138 if (view_needs_focus_indication(view))
7139 wstandend(view->window);
7140 free(wline);
7141 wline = NULL;
7142 free(line);
7143 line = NULL;
7144 if (width < view->ncols - 1)
7145 waddch(view->window, '\n');
7146 if (--limit <= 0)
7147 return NULL;
7149 n = 0;
7150 while (re && limit > 0) {
7151 char *line = NULL;
7152 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7154 if (s->show_date) {
7155 struct got_commit_object *ci;
7156 struct got_tag_object *tag;
7157 struct got_object_id *id;
7158 struct tm tm;
7159 time_t t;
7161 err = got_ref_resolve(&id, s->repo, re->ref);
7162 if (err)
7163 return err;
7164 err = got_object_open_as_tag(&tag, s->repo, id);
7165 if (err) {
7166 if (err->code != GOT_ERR_OBJ_TYPE) {
7167 free(id);
7168 return err;
7170 err = got_object_open_as_commit(&ci, s->repo,
7171 id);
7172 if (err) {
7173 free(id);
7174 return err;
7176 t = got_object_commit_get_committer_time(ci);
7177 got_object_commit_close(ci);
7178 } else {
7179 t = got_object_tag_get_tagger_time(tag);
7180 got_object_tag_close(tag);
7182 free(id);
7183 if (gmtime_r(&t, &tm) == NULL)
7184 return got_error_from_errno("gmtime_r");
7185 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7186 return got_error(GOT_ERR_NO_SPACE);
7188 if (got_ref_is_symbolic(re->ref)) {
7189 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7190 ymd : "", got_ref_get_name(re->ref),
7191 got_ref_get_symref_target(re->ref)) == -1)
7192 return got_error_from_errno("asprintf");
7193 } else if (s->show_ids) {
7194 struct got_object_id *id;
7195 char *id_str;
7196 err = got_ref_resolve(&id, s->repo, re->ref);
7197 if (err)
7198 return err;
7199 err = got_object_id_str(&id_str, id);
7200 if (err) {
7201 free(id);
7202 return err;
7204 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7205 got_ref_get_name(re->ref), id_str) == -1) {
7206 err = got_error_from_errno("asprintf");
7207 free(id);
7208 free(id_str);
7209 return err;
7211 free(id);
7212 free(id_str);
7213 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7214 got_ref_get_name(re->ref)) == -1)
7215 return got_error_from_errno("asprintf");
7217 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7218 0, 0);
7219 if (err) {
7220 free(line);
7221 return err;
7223 if (n == s->selected) {
7224 if (view->focussed)
7225 wstandout(view->window);
7226 s->selected_entry = re;
7228 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7229 if (tc)
7230 wattr_on(view->window,
7231 COLOR_PAIR(tc->colorpair), NULL);
7232 waddwstr(view->window, wline);
7233 if (tc)
7234 wattr_off(view->window,
7235 COLOR_PAIR(tc->colorpair), NULL);
7236 if (width < view->ncols - 1)
7237 waddch(view->window, '\n');
7238 if (n == s->selected && view->focussed)
7239 wstandend(view->window);
7240 free(line);
7241 free(wline);
7242 wline = NULL;
7243 n++;
7244 s->ndisplayed++;
7245 s->last_displayed_entry = re;
7247 limit--;
7248 re = TAILQ_NEXT(re, entry);
7251 view_border(view);
7252 return err;
7255 static const struct got_error *
7256 browse_ref_tree(struct tog_view **new_view, int begin_x,
7257 struct tog_reflist_entry *re, struct got_repository *repo)
7259 const struct got_error *err = NULL;
7260 struct got_object_id *commit_id = NULL;
7261 struct tog_view *tree_view;
7263 *new_view = NULL;
7265 err = resolve_reflist_entry(&commit_id, re, repo);
7266 if (err) {
7267 if (err->code != GOT_ERR_OBJ_TYPE)
7268 return err;
7269 else
7270 return NULL;
7274 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
7275 if (tree_view == NULL) {
7276 err = got_error_from_errno("view_open");
7277 goto done;
7280 err = open_tree_view(tree_view, commit_id,
7281 got_ref_get_name(re->ref), repo);
7282 if (err)
7283 goto done;
7285 *new_view = tree_view;
7286 done:
7287 free(commit_id);
7288 return err;
7290 static const struct got_error *
7291 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7293 const struct got_error *err = NULL;
7294 struct tog_ref_view_state *s = &view->state.ref;
7295 struct tog_view *log_view, *tree_view;
7296 struct tog_reflist_entry *re;
7297 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7299 switch (ch) {
7300 case 'i':
7301 s->show_ids = !s->show_ids;
7302 view->count = 0;
7303 break;
7304 case 'm':
7305 s->show_date = !s->show_date;
7306 view->count = 0;
7307 break;
7308 case 'o':
7309 s->sort_by_date = !s->sort_by_date;
7310 view->count = 0;
7311 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7312 got_ref_cmp_by_commit_timestamp_descending :
7313 tog_ref_cmp_by_name, s->repo);
7314 if (err)
7315 break;
7316 got_reflist_object_id_map_free(tog_refs_idmap);
7317 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7318 &tog_refs, s->repo);
7319 if (err)
7320 break;
7321 ref_view_free_refs(s);
7322 err = ref_view_load_refs(s);
7323 break;
7324 case KEY_ENTER:
7325 case '\r':
7326 view->count = 0;
7327 if (!s->selected_entry)
7328 break;
7329 if (view_is_parent_view(view))
7330 view_get_split(view, &begin_y, &begin_x);
7332 err = log_ref_entry(&log_view, begin_y, begin_x,
7333 s->selected_entry, s->repo);
7334 if (err)
7335 break;
7337 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
7338 err = view_init_hsplit(view, begin_y);
7339 if (err)
7340 break;
7343 view->focussed = 0;
7344 log_view->focussed = 1;
7345 log_view->mode = view->mode;
7346 log_view->nlines = view->lines - begin_y;
7347 if (view_is_parent_view(view)) {
7348 err = view_close_child(view);
7349 if (err)
7350 return err;
7351 err = view_set_child(view, log_view);
7352 if (err)
7353 return err;
7354 view->focus_child = 1;
7355 } else
7356 *new_view = log_view;
7357 break;
7358 case 't':
7359 view->count = 0;
7360 if (!s->selected_entry)
7361 break;
7362 if (view_is_parent_view(view))
7363 begin_x = view_split_begin_x(view->begin_x);
7364 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
7365 s->repo);
7366 if (err || tree_view == NULL)
7367 break;
7368 view->focussed = 0;
7369 tree_view->focussed = 1;
7370 if (view_is_parent_view(view)) {
7371 err = view_close_child(view);
7372 if (err)
7373 return err;
7374 err = view_set_child(view, tree_view);
7375 if (err)
7376 return err;
7377 view->focus_child = 1;
7378 } else
7379 *new_view = tree_view;
7380 break;
7381 case 'g':
7382 case KEY_HOME:
7383 s->selected = 0;
7384 view->count = 0;
7385 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7386 break;
7387 case 'G':
7388 case KEY_END: {
7389 int eos = view->nlines - 1;
7391 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7392 --eos; /* border */
7393 s->selected = 0;
7394 view->count = 0;
7395 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7396 for (n = 0; n < eos; n++) {
7397 if (re == NULL)
7398 break;
7399 s->first_displayed_entry = re;
7400 re = TAILQ_PREV(re, tog_reflist_head, entry);
7402 if (n > 0)
7403 s->selected = n - 1;
7404 break;
7406 case 'k':
7407 case KEY_UP:
7408 case CTRL('p'):
7409 if (s->selected > 0) {
7410 s->selected--;
7411 break;
7413 ref_scroll_up(s, 1);
7414 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7415 view->count = 0;
7416 break;
7417 case CTRL('u'):
7418 case 'u':
7419 nscroll /= 2;
7420 /* FALL THROUGH */
7421 case KEY_PPAGE:
7422 case CTRL('b'):
7423 case 'b':
7424 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7425 s->selected -= MIN(nscroll, s->selected);
7426 ref_scroll_up(s, MAX(0, nscroll));
7427 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7428 view->count = 0;
7429 break;
7430 case 'j':
7431 case KEY_DOWN:
7432 case CTRL('n'):
7433 if (s->selected < s->ndisplayed - 1) {
7434 s->selected++;
7435 break;
7437 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7438 /* can't scroll any further */
7439 view->count = 0;
7440 break;
7442 ref_scroll_down(view, 1);
7443 break;
7444 case CTRL('d'):
7445 case 'd':
7446 nscroll /= 2;
7447 /* FALL THROUGH */
7448 case KEY_NPAGE:
7449 case CTRL('f'):
7450 case 'f':
7451 case ' ':
7452 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7453 /* can't scroll any further; move cursor down */
7454 if (s->selected < s->ndisplayed - 1)
7455 s->selected += MIN(nscroll,
7456 s->ndisplayed - s->selected - 1);
7457 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7458 s->selected += s->ndisplayed - s->selected - 1;
7459 view->count = 0;
7460 break;
7462 ref_scroll_down(view, nscroll);
7463 break;
7464 case CTRL('l'):
7465 view->count = 0;
7466 tog_free_refs();
7467 err = tog_load_refs(s->repo, s->sort_by_date);
7468 if (err)
7469 break;
7470 ref_view_free_refs(s);
7471 err = ref_view_load_refs(s);
7472 break;
7473 case KEY_RESIZE:
7474 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7475 s->selected = view->nlines - 2;
7476 break;
7477 default:
7478 view->count = 0;
7479 break;
7482 return err;
7485 __dead static void
7486 usage_ref(void)
7488 endwin();
7489 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7490 getprogname());
7491 exit(1);
7494 static const struct got_error *
7495 cmd_ref(int argc, char *argv[])
7497 const struct got_error *error;
7498 struct got_repository *repo = NULL;
7499 struct got_worktree *worktree = NULL;
7500 char *cwd = NULL, *repo_path = NULL;
7501 int ch;
7502 struct tog_view *view;
7503 int *pack_fds = NULL;
7505 while ((ch = getopt(argc, argv, "r:")) != -1) {
7506 switch (ch) {
7507 case 'r':
7508 repo_path = realpath(optarg, NULL);
7509 if (repo_path == NULL)
7510 return got_error_from_errno2("realpath",
7511 optarg);
7512 break;
7513 default:
7514 usage_ref();
7515 /* NOTREACHED */
7519 argc -= optind;
7520 argv += optind;
7522 if (argc > 1)
7523 usage_ref();
7525 error = got_repo_pack_fds_open(&pack_fds);
7526 if (error != NULL)
7527 goto done;
7529 if (repo_path == NULL) {
7530 cwd = getcwd(NULL, 0);
7531 if (cwd == NULL)
7532 return got_error_from_errno("getcwd");
7533 error = got_worktree_open(&worktree, cwd);
7534 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7535 goto done;
7536 if (worktree)
7537 repo_path =
7538 strdup(got_worktree_get_repo_path(worktree));
7539 else
7540 repo_path = strdup(cwd);
7541 if (repo_path == NULL) {
7542 error = got_error_from_errno("strdup");
7543 goto done;
7547 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7548 if (error != NULL)
7549 goto done;
7551 init_curses();
7553 error = apply_unveil(got_repo_get_path(repo), NULL);
7554 if (error)
7555 goto done;
7557 error = tog_load_refs(repo, 0);
7558 if (error)
7559 goto done;
7561 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7562 if (view == NULL) {
7563 error = got_error_from_errno("view_open");
7564 goto done;
7567 error = open_ref_view(view, repo);
7568 if (error)
7569 goto done;
7571 if (worktree) {
7572 /* Release work tree lock. */
7573 got_worktree_close(worktree);
7574 worktree = NULL;
7576 error = view_loop(view);
7577 done:
7578 free(repo_path);
7579 free(cwd);
7580 if (repo) {
7581 const struct got_error *close_err = got_repo_close(repo);
7582 if (close_err)
7583 error = close_err;
7585 if (pack_fds) {
7586 const struct got_error *pack_err =
7587 got_repo_pack_fds_close(pack_fds);
7588 if (error == NULL)
7589 error = pack_err;
7591 tog_free_refs();
7592 return error;
7596 * If view was scrolled down to move the selected line into view when opening a
7597 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7599 static void
7600 offset_selection_up(struct tog_view *view)
7602 switch (view->type) {
7603 case TOG_VIEW_BLAME: {
7604 struct tog_blame_view_state *s = &view->state.blame;
7605 if (s->first_displayed_line == 1) {
7606 s->selected_line = MAX(s->selected_line - view->offset,
7607 1);
7608 break;
7610 if (s->first_displayed_line > view->offset)
7611 s->first_displayed_line -= view->offset;
7612 else
7613 s->first_displayed_line = 1;
7614 s->selected_line += view->offset;
7615 break;
7617 case TOG_VIEW_LOG:
7618 log_scroll_up(&view->state.log, view->offset);
7619 view->state.log.selected += view->offset;
7620 break;
7621 case TOG_VIEW_REF:
7622 ref_scroll_up(&view->state.ref, view->offset);
7623 view->state.ref.selected += view->offset;
7624 break;
7625 case TOG_VIEW_TREE:
7626 tree_scroll_up(&view->state.tree, view->offset);
7627 view->state.tree.selected += view->offset;
7628 break;
7629 default:
7630 break;
7633 view->offset = 0;
7637 * If the selected line is in the section of screen covered by the bottom split,
7638 * scroll down offset lines to move it into view and index its new position.
7640 static const struct got_error *
7641 offset_selection_down(struct tog_view *view)
7643 const struct got_error *err = NULL;
7644 const struct got_error *(*scrolld)(struct tog_view *, int);
7645 int *selected = NULL;
7646 int header, offset;
7648 switch (view->type) {
7649 case TOG_VIEW_BLAME: {
7650 struct tog_blame_view_state *s = &view->state.blame;
7651 header = 3;
7652 scrolld = NULL;
7653 if (s->selected_line > view->nlines - header) {
7654 offset = abs(view->nlines - s->selected_line - header);
7655 s->first_displayed_line += offset;
7656 s->selected_line -= offset;
7657 view->offset = offset;
7659 break;
7661 case TOG_VIEW_LOG: {
7662 struct tog_log_view_state *s = &view->state.log;
7663 scrolld = &log_scroll_down;
7664 header = 3;
7665 selected = &s->selected;
7666 break;
7668 case TOG_VIEW_REF: {
7669 struct tog_ref_view_state *s = &view->state.ref;
7670 scrolld = &ref_scroll_down;
7671 header = 3;
7672 selected = &s->selected;
7673 break;
7675 case TOG_VIEW_TREE: {
7676 struct tog_tree_view_state *s = &view->state.tree;
7677 scrolld = &tree_scroll_down;
7678 header = 5;
7679 selected = &s->selected;
7680 break;
7682 default:
7683 selected = NULL;
7684 scrolld = NULL;
7685 header = 0;
7686 break;
7689 if (selected && *selected > view->nlines - header) {
7690 offset = abs(view->nlines - *selected - header);
7691 view->offset = offset;
7692 if (scrolld && offset) {
7693 err = scrolld(view, offset);
7694 *selected -= offset;
7698 return err;
7701 static void
7702 list_commands(FILE *fp)
7704 size_t i;
7706 fprintf(fp, "commands:");
7707 for (i = 0; i < nitems(tog_commands); i++) {
7708 const struct tog_cmd *cmd = &tog_commands[i];
7709 fprintf(fp, " %s", cmd->name);
7711 fputc('\n', fp);
7714 __dead static void
7715 usage(int hflag, int status)
7717 FILE *fp = (status == 0) ? stdout : stderr;
7719 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7720 getprogname());
7721 if (hflag) {
7722 fprintf(fp, "lazy usage: %s path\n", getprogname());
7723 list_commands(fp);
7725 exit(status);
7728 static char **
7729 make_argv(int argc, ...)
7731 va_list ap;
7732 char **argv;
7733 int i;
7735 va_start(ap, argc);
7737 argv = calloc(argc, sizeof(char *));
7738 if (argv == NULL)
7739 err(1, "calloc");
7740 for (i = 0; i < argc; i++) {
7741 argv[i] = strdup(va_arg(ap, char *));
7742 if (argv[i] == NULL)
7743 err(1, "strdup");
7746 va_end(ap);
7747 return argv;
7751 * Try to convert 'tog path' into a 'tog log path' command.
7752 * The user could simply have mistyped the command rather than knowingly
7753 * provided a path. So check whether argv[0] can in fact be resolved
7754 * to a path in the HEAD commit and print a special error if not.
7755 * This hack is for mpi@ <3
7757 static const struct got_error *
7758 tog_log_with_path(int argc, char *argv[])
7760 const struct got_error *error = NULL, *close_err;
7761 const struct tog_cmd *cmd = NULL;
7762 struct got_repository *repo = NULL;
7763 struct got_worktree *worktree = NULL;
7764 struct got_object_id *commit_id = NULL, *id = NULL;
7765 struct got_commit_object *commit = NULL;
7766 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7767 char *commit_id_str = NULL, **cmd_argv = NULL;
7768 int *pack_fds = NULL;
7770 cwd = getcwd(NULL, 0);
7771 if (cwd == NULL)
7772 return got_error_from_errno("getcwd");
7774 error = got_repo_pack_fds_open(&pack_fds);
7775 if (error != NULL)
7776 goto done;
7778 error = got_worktree_open(&worktree, cwd);
7779 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7780 goto done;
7782 if (worktree)
7783 repo_path = strdup(got_worktree_get_repo_path(worktree));
7784 else
7785 repo_path = strdup(cwd);
7786 if (repo_path == NULL) {
7787 error = got_error_from_errno("strdup");
7788 goto done;
7791 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7792 if (error != NULL)
7793 goto done;
7795 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7796 repo, worktree);
7797 if (error)
7798 goto done;
7800 error = tog_load_refs(repo, 0);
7801 if (error)
7802 goto done;
7803 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7804 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7805 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7806 if (error)
7807 goto done;
7809 if (worktree) {
7810 got_worktree_close(worktree);
7811 worktree = NULL;
7814 error = got_object_open_as_commit(&commit, repo, commit_id);
7815 if (error)
7816 goto done;
7818 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7819 if (error) {
7820 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7821 goto done;
7822 fprintf(stderr, "%s: '%s' is no known command or path\n",
7823 getprogname(), argv[0]);
7824 usage(1, 1);
7825 /* not reached */
7828 close_err = got_repo_close(repo);
7829 if (error == NULL)
7830 error = close_err;
7831 repo = NULL;
7833 error = got_object_id_str(&commit_id_str, commit_id);
7834 if (error)
7835 goto done;
7837 cmd = &tog_commands[0]; /* log */
7838 argc = 4;
7839 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7840 error = cmd->cmd_main(argc, cmd_argv);
7841 done:
7842 if (repo) {
7843 close_err = got_repo_close(repo);
7844 if (error == NULL)
7845 error = close_err;
7847 if (commit)
7848 got_object_commit_close(commit);
7849 if (worktree)
7850 got_worktree_close(worktree);
7851 if (pack_fds) {
7852 const struct got_error *pack_err =
7853 got_repo_pack_fds_close(pack_fds);
7854 if (error == NULL)
7855 error = pack_err;
7857 free(id);
7858 free(commit_id_str);
7859 free(commit_id);
7860 free(cwd);
7861 free(repo_path);
7862 free(in_repo_path);
7863 if (cmd_argv) {
7864 int i;
7865 for (i = 0; i < argc; i++)
7866 free(cmd_argv[i]);
7867 free(cmd_argv);
7869 tog_free_refs();
7870 return error;
7873 int
7874 main(int argc, char *argv[])
7876 const struct got_error *error = NULL;
7877 const struct tog_cmd *cmd = NULL;
7878 int ch, hflag = 0, Vflag = 0;
7879 char **cmd_argv = NULL;
7880 static const struct option longopts[] = {
7881 { "version", no_argument, NULL, 'V' },
7882 { NULL, 0, NULL, 0}
7884 char *diff_algo_str = NULL;
7886 setlocale(LC_CTYPE, "");
7888 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7889 switch (ch) {
7890 case 'h':
7891 hflag = 1;
7892 break;
7893 case 'V':
7894 Vflag = 1;
7895 break;
7896 default:
7897 usage(hflag, 1);
7898 /* NOTREACHED */
7902 argc -= optind;
7903 argv += optind;
7904 optind = 1;
7905 optreset = 1;
7907 if (Vflag) {
7908 got_version_print_str();
7909 return 0;
7912 #ifndef PROFILE
7913 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7914 NULL) == -1)
7915 err(1, "pledge");
7916 #endif
7918 if (argc == 0) {
7919 if (hflag)
7920 usage(hflag, 0);
7921 /* Build an argument vector which runs a default command. */
7922 cmd = &tog_commands[0];
7923 argc = 1;
7924 cmd_argv = make_argv(argc, cmd->name);
7925 } else {
7926 size_t i;
7928 /* Did the user specify a command? */
7929 for (i = 0; i < nitems(tog_commands); i++) {
7930 if (strncmp(tog_commands[i].name, argv[0],
7931 strlen(argv[0])) == 0) {
7932 cmd = &tog_commands[i];
7933 break;
7938 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
7939 if (diff_algo_str) {
7940 if (strcasecmp(diff_algo_str, "patience") == 0)
7941 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
7942 if (strcasecmp(diff_algo_str, "myers") == 0)
7943 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
7946 if (cmd == NULL) {
7947 if (argc != 1)
7948 usage(0, 1);
7949 /* No command specified; try log with a path */
7950 error = tog_log_with_path(argc, argv);
7951 } else {
7952 if (hflag)
7953 cmd->cmd_usage();
7954 else
7955 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7958 endwin();
7959 putchar('\n');
7960 if (cmd_argv) {
7961 int i;
7962 for (i = 0; i < argc; i++)
7963 free(cmd_argv[i]);
7964 free(cmd_argv);
7967 if (error && error->code != GOT_ERR_CANCELLED)
7968 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7969 return 0;