2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 6062e8ea 2021-09-21 stsp #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 d7b5a0e8 2022-04-20 stsp #include <sha1.h>
28 5822e79e 2023-02-23 op #include <sha2.h>
29 61266923 2020-01-14 stsp #include <signal.h>
30 9f7d7167 2018-04-29 stsp #include <stdlib.h>
31 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
32 26ed57b2 2018-05-19 stsp #include <stdio.h>
33 9f7d7167 2018-04-29 stsp #include <getopt.h>
34 9f7d7167 2018-04-29 stsp #include <string.h>
35 9f7d7167 2018-04-29 stsp #include <err.h>
36 80ddbec8 2018-04-29 stsp #include <unistd.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 3da8ef85 2021-09-21 stsp #include <sched.h>
45 53ccebc2 2019-07-30 stsp #include "got_version.h"
46 9f7d7167 2018-04-29 stsp #include "got_error.h"
47 80ddbec8 2018-04-29 stsp #include "got_object.h"
48 80ddbec8 2018-04-29 stsp #include "got_reference.h"
49 80ddbec8 2018-04-29 stsp #include "got_repository.h"
50 80ddbec8 2018-04-29 stsp #include "got_diff.h"
51 511a516b 2018-05-19 stsp #include "got_opentemp.h"
52 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
53 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
54 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
55 a70480e0 2018-06-23 stsp #include "got_blame.h"
56 c2db6724 2019-01-04 stsp #include "got_privsep.h"
57 1dd54920 2019-05-11 stsp #include "got_path.h"
58 b7165be3 2019-02-05 stsp #include "got_worktree.h"
61 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
70 9f7d7167 2018-04-29 stsp #ifndef nitems
71 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 9f7d7167 2018-04-29 stsp struct tog_cmd {
75 c2301be8 2018-04-30 stsp const char *name;
76 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
77 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
80 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
83 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
84 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
85 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
89 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
90 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
91 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
93 3e166534 2022-02-16 naddy static const struct tog_cmd tog_commands[] = {
94 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
95 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
96 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
97 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
98 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
101 d6b05b5b 2018-08-04 stsp enum tog_view_type {
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
103 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
104 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
105 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
106 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
107 ec2a9698 2022-09-15 mark TOG_VIEW_HELP
110 ec2a9698 2022-09-15 mark /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
111 ec2a9698 2022-09-15 mark enum tog_keymap_type {
112 ec2a9698 2022-09-15 mark TOG_KEYMAP_KEYS = -2,
113 ec2a9698 2022-09-15 mark TOG_KEYMAP_GLOBAL,
114 ec2a9698 2022-09-15 mark TOG_KEYMAP_DIFF,
115 ec2a9698 2022-09-15 mark TOG_KEYMAP_LOG,
116 ec2a9698 2022-09-15 mark TOG_KEYMAP_BLAME,
117 ec2a9698 2022-09-15 mark TOG_KEYMAP_TREE,
118 ec2a9698 2022-09-15 mark TOG_KEYMAP_REF,
119 ec2a9698 2022-09-15 mark TOG_KEYMAP_HELP
122 9b058f45 2022-06-30 mark enum tog_view_mode {
123 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_NONE,
124 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_VERT,
125 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_HRZN
128 9b058f45 2022-06-30 mark #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
130 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
132 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
133 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
134 ba4f502b 2018-08-04 stsp struct got_object_id *id;
135 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
138 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
139 ba4f502b 2018-08-04 stsp struct commit_queue {
140 ba4f502b 2018-08-04 stsp int ncommits;
141 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
144 f26dddb7 2019-11-08 stsp struct tog_color {
145 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
146 6d17833f 2019-11-08 stsp regex_t regex;
147 6d17833f 2019-11-08 stsp short colorpair;
149 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
151 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
152 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
153 917d79a7 2022-07-01 stsp static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
155 cc488aa7 2022-01-23 stsp static const struct got_error *
156 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
157 cc488aa7 2022-01-23 stsp struct got_reference* re2)
159 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
160 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
161 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
163 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
164 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
165 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
166 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
168 cc488aa7 2022-01-23 stsp return NULL;
169 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
171 cc488aa7 2022-01-23 stsp return NULL;
174 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
175 cc488aa7 2022-01-23 stsp return NULL;
178 11b20872 2019-11-08 stsp static const struct got_error *
179 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
181 51a10b52 2020-12-26 stsp const struct got_error *err;
183 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
184 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
187 51a10b52 2020-12-26 stsp return err;
189 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
193 51a10b52 2020-12-26 stsp static void
194 51a10b52 2020-12-26 stsp tog_free_refs(void)
196 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
197 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
198 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
200 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
203 51a10b52 2020-12-26 stsp static const struct got_error *
204 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
205 11b20872 2019-11-08 stsp int idx, short color)
207 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
208 11b20872 2019-11-08 stsp struct tog_color *tc;
209 11b20872 2019-11-08 stsp int regerr = 0;
211 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
212 11b20872 2019-11-08 stsp return NULL;
214 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
216 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
217 11b20872 2019-11-08 stsp if (tc == NULL)
218 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
219 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
220 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
221 11b20872 2019-11-08 stsp if (regerr) {
222 11b20872 2019-11-08 stsp static char regerr_msg[512];
223 11b20872 2019-11-08 stsp static char err_msg[512];
224 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
225 11b20872 2019-11-08 stsp sizeof(regerr_msg));
226 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
227 11b20872 2019-11-08 stsp regerr_msg);
228 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
230 11b20872 2019-11-08 stsp return err;
232 11b20872 2019-11-08 stsp tc->colorpair = idx;
233 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
234 11b20872 2019-11-08 stsp return NULL;
237 11b20872 2019-11-08 stsp static void
238 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
240 11b20872 2019-11-08 stsp struct tog_color *tc;
242 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
243 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
244 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
245 11b20872 2019-11-08 stsp regfree(&tc->regex);
250 336075a4 2022-06-25 op static struct tog_color *
251 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
253 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
255 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
256 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
260 11b20872 2019-11-08 stsp return NULL;
264 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
267 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
269 11b20872 2019-11-08 stsp return COLOR_CYAN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
271 11b20872 2019-11-08 stsp return COLOR_YELLOW;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
273 11b20872 2019-11-08 stsp return COLOR_GREEN;
274 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
275 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
276 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
277 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
278 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
279 91b8c405 2020-01-25 stsp return COLOR_CYAN;
280 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
281 11b20872 2019-11-08 stsp return COLOR_GREEN;
282 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
283 11b20872 2019-11-08 stsp return COLOR_GREEN;
284 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
285 11b20872 2019-11-08 stsp return COLOR_CYAN;
286 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
287 11b20872 2019-11-08 stsp return COLOR_YELLOW;
288 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
289 6458efa5 2020-11-24 stsp return COLOR_GREEN;
290 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
291 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
292 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
293 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
294 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
295 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
301 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
303 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
305 11b20872 2019-11-08 stsp if (val == NULL)
306 11b20872 2019-11-08 stsp return default_color_value(envvar);
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
309 11b20872 2019-11-08 stsp return COLOR_BLACK;
310 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
311 11b20872 2019-11-08 stsp return COLOR_RED;
312 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
313 11b20872 2019-11-08 stsp return COLOR_GREEN;
314 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
315 11b20872 2019-11-08 stsp return COLOR_YELLOW;
316 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
317 11b20872 2019-11-08 stsp return COLOR_BLUE;
318 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
319 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
320 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
321 11b20872 2019-11-08 stsp return COLOR_CYAN;
322 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
323 11b20872 2019-11-08 stsp return COLOR_WHITE;
324 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
327 11b20872 2019-11-08 stsp return default_color_value(envvar);
330 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
331 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
332 3dbaef42 2020-11-24 stsp const char *label1, *label2;
333 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
334 f9d37699 2022-06-28 stsp int fd1, fd2;
335 c7d5c43c 2022-08-04 mark int lineno;
336 15a087fe 2019-02-21 stsp int first_displayed_line;
337 15a087fe 2019-02-21 stsp int last_displayed_line;
339 15a087fe 2019-02-21 stsp int diff_context;
340 3dbaef42 2020-11-24 stsp int ignore_whitespace;
341 64453f7e 2020-11-21 stsp int force_text_diff;
342 15a087fe 2019-02-21 stsp struct got_repository *repo;
343 c7d5c43c 2022-08-04 mark struct got_diff_line *lines;
344 fe621944 2020-11-10 stsp size_t nlines;
345 f44b1f58 2020-02-02 tracey int matched_line;
346 f44b1f58 2020-02-02 tracey int selected_line;
348 c0f61fa4 2022-07-11 mark /* passed from log or blame view; may be NULL */
349 c0f61fa4 2022-07-11 mark struct tog_view *parent_view;
352 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
353 5629093a 2022-07-11 stsp static volatile sig_atomic_t tog_thread_error;
355 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
356 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
357 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
358 1a76625f 2018-10-22 stsp int commits_needed;
359 fb280deb 2021-08-30 stsp int load_all;
360 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
361 568eae95 2022-09-11 mark struct commit_queue *real_commits;
362 1a76625f 2018-10-22 stsp const char *in_repo_path;
363 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
364 1a76625f 2018-10-22 stsp struct got_repository *repo;
365 74467cc8 2022-06-15 stsp int *pack_fds;
366 1a76625f 2018-10-22 stsp int log_complete;
367 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
368 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
369 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
370 13add988 2019-10-15 stsp int *searching;
371 13add988 2019-10-15 stsp int *search_next_done;
372 13add988 2019-10-15 stsp regex_t *regex;
373 568eae95 2022-09-11 mark int *limiting;
374 568eae95 2022-09-11 mark int limit_match;
375 568eae95 2022-09-11 mark regex_t *limit_regex;
376 568eae95 2022-09-11 mark struct commit_queue *limit_commits;
379 1a76625f 2018-10-22 stsp struct tog_log_view_state {
380 568eae95 2022-09-11 mark struct commit_queue *commits;
381 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
382 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
383 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
384 568eae95 2022-09-11 mark struct commit_queue real_commits;
385 b01e7d3b 2018-08-04 stsp int selected;
386 b01e7d3b 2018-08-04 stsp char *in_repo_path;
387 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
388 b672a97a 2020-01-27 stsp int log_branches;
389 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
390 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
391 1a76625f 2018-10-22 stsp sig_atomic_t quit;
392 1a76625f 2018-10-22 stsp pthread_t thread;
393 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
394 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
395 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
396 11b20872 2019-11-08 stsp struct tog_colors colors;
397 10aab77f 2022-07-19 op int use_committer;
398 568eae95 2022-09-11 mark int limit_view;
399 568eae95 2022-09-11 mark regex_t limit_regex;
400 568eae95 2022-09-11 mark struct commit_queue limit_commits;
403 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
404 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
405 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
406 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
407 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
408 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
409 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
410 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
411 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
412 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
413 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
414 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
415 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
416 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
417 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
419 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
420 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
421 e9424729 2018-08-04 stsp int nlines;
423 e9424729 2018-08-04 stsp struct tog_view *view;
424 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
428 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
429 e9424729 2018-08-04 stsp const char *path;
430 e9424729 2018-08-04 stsp struct got_repository *repo;
431 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
432 e9424729 2018-08-04 stsp int *complete;
433 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
434 fc06ba56 2019-08-22 stsp void *cancel_arg;
437 e9424729 2018-08-04 stsp struct tog_blame {
439 be659d10 2020-11-18 stsp off_t filesize;
440 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
441 6fcac457 2018-11-19 stsp int nlines;
442 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
443 e9424729 2018-08-04 stsp pthread_t thread;
444 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
445 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
446 e9424729 2018-08-04 stsp const char *path;
447 0ae84acc 2022-06-15 tracey int *pack_fds;
450 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
451 7cbe629d 2018-08-04 stsp int first_displayed_line;
452 7cbe629d 2018-08-04 stsp int last_displayed_line;
453 7cbe629d 2018-08-04 stsp int selected_line;
454 c0f61fa4 2022-07-11 mark int last_diffed_line;
455 7cbe629d 2018-08-04 stsp int blame_complete;
458 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
459 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
460 e5a0f69f 2018-08-18 stsp char *path;
461 7cbe629d 2018-08-04 stsp struct got_repository *repo;
462 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
463 136e2bd4 2022-07-23 mark struct got_object_id *id_to_log;
464 e9424729 2018-08-04 stsp struct tog_blame blame;
465 6c4c42e0 2019-06-24 stsp int matched_line;
466 11b20872 2019-11-08 stsp struct tog_colors colors;
469 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
470 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
471 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
472 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
473 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
474 ad80ab7b 2018-08-04 stsp int selected;
477 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
479 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
480 ad80ab7b 2018-08-04 stsp char *tree_label;
481 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
482 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
483 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
484 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
485 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
486 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
487 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
488 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
489 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
490 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
491 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
492 6458efa5 2020-11-24 stsp struct tog_colors colors;
495 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
496 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
497 6458efa5 2020-11-24 stsp struct got_reference *ref;
501 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
503 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
504 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
505 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
506 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
507 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
508 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
509 6458efa5 2020-11-24 stsp struct got_repository *repo;
510 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
511 bddb1296 2019-11-08 stsp struct tog_colors colors;
514 ec2a9698 2022-09-15 mark struct tog_help_view_state {
516 ec2a9698 2022-09-15 mark off_t *line_offsets;
517 ec2a9698 2022-09-15 mark size_t nlines;
518 ec2a9698 2022-09-15 mark int lineno;
519 ec2a9698 2022-09-15 mark int first_displayed_line;
520 ec2a9698 2022-09-15 mark int last_displayed_line;
522 ec2a9698 2022-09-15 mark int matched_line;
523 ec2a9698 2022-09-15 mark int selected_line;
525 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
528 ec2a9698 2022-09-15 mark #define GENERATE_HELP \
529 ec2a9698 2022-09-15 mark KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
530 ec2a9698 2022-09-15 mark KEY_("H F1", "Open view-specific help (double tap for all help)"), \
531 ec2a9698 2022-09-15 mark KEY_("k C-p Up", "Move cursor or page up one line"), \
532 ec2a9698 2022-09-15 mark KEY_("j C-n Down", "Move cursor or page down one line"), \
533 ec2a9698 2022-09-15 mark KEY_("C-b b PgUp", "Scroll the view up one page"), \
534 ec2a9698 2022-09-15 mark KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
535 ec2a9698 2022-09-15 mark KEY_("C-u u", "Scroll the view up one half page"), \
536 ec2a9698 2022-09-15 mark KEY_("C-d d", "Scroll the view down one half page"), \
537 0b3f028d 2023-01-09 mark KEY_("g", "Go to line N (default: first line)"), \
538 0b3f028d 2023-01-09 mark KEY_("Home =", "Go to the first line"), \
539 0b3f028d 2023-01-09 mark KEY_("G", "Go to line N (default: last line)"), \
540 0b3f028d 2023-01-09 mark KEY_("End *", "Go to the last line"), \
541 ec2a9698 2022-09-15 mark KEY_("l Right", "Scroll the view right"), \
542 ec2a9698 2022-09-15 mark KEY_("h Left", "Scroll the view left"), \
543 ec2a9698 2022-09-15 mark KEY_("$", "Scroll view to the rightmost position"), \
544 ec2a9698 2022-09-15 mark KEY_("0", "Scroll view to the leftmost position"), \
545 ec2a9698 2022-09-15 mark KEY_("-", "Decrease size of the focussed split"), \
546 ec2a9698 2022-09-15 mark KEY_("+", "Increase size of the focussed split"), \
547 ec2a9698 2022-09-15 mark KEY_("Tab", "Switch focus between views"), \
548 ec2a9698 2022-09-15 mark KEY_("F", "Toggle fullscreen mode"), \
549 132d5247 2023-04-14 stsp KEY_("S", "Switch split-screen layout"), \
550 ec2a9698 2022-09-15 mark KEY_("/", "Open prompt to enter search term"), \
551 ec2a9698 2022-09-15 mark KEY_("n", "Find next line/token matching the current search term"), \
552 ec2a9698 2022-09-15 mark KEY_("N", "Find previous line/token matching the current search term"),\
553 16a2d4f0 2022-09-19 stsp KEY_("q", "Quit the focussed view; Quit help screen"), \
554 ec2a9698 2022-09-15 mark KEY_("Q", "Quit tog"), \
556 fd6badaa 2022-09-23 stsp KEYMAP_("Log view", TOG_KEYMAP_LOG), \
557 ec2a9698 2022-09-15 mark KEY_("< ,", "Move cursor up one commit"), \
558 ec2a9698 2022-09-15 mark KEY_("> .", "Move cursor down one commit"), \
559 ec2a9698 2022-09-15 mark KEY_("Enter", "Open diff view of the selected commit"), \
560 ec2a9698 2022-09-15 mark KEY_("B", "Reload the log view and toggle display of merged commits"), \
561 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
562 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the repository from the selected" \
563 ec2a9698 2022-09-15 mark " commit"), \
564 ec2a9698 2022-09-15 mark KEY_("@", "Toggle between displaying author and committer name"), \
565 ec2a9698 2022-09-15 mark KEY_("&", "Open prompt to enter term to limit commits displayed"), \
566 ec2a9698 2022-09-15 mark KEY_("C-g Backspace", "Cancel current search or log operation"), \
567 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload the log view with new commits in the repository"), \
569 fd6badaa 2022-09-23 stsp KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
570 ec2a9698 2022-09-15 mark KEY_("K < ,", "Display diff of next line in the file/log entry"), \
571 ec2a9698 2022-09-15 mark KEY_("J > .", "Display diff of previous line in the file/log entry"), \
572 ec2a9698 2022-09-15 mark KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
573 ec2a9698 2022-09-15 mark KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
574 ec2a9698 2022-09-15 mark " data"), \
575 ec2a9698 2022-09-15 mark KEY_("(", "Go to the previous file in the diff"), \
576 ec2a9698 2022-09-15 mark KEY_(")", "Go to the next file in the diff"), \
577 ec2a9698 2022-09-15 mark KEY_("{", "Go to the previous hunk in the diff"), \
578 ec2a9698 2022-09-15 mark KEY_("}", "Go to the next hunk in the diff"), \
579 ec2a9698 2022-09-15 mark KEY_("[", "Decrease the number of context lines"), \
580 ec2a9698 2022-09-15 mark KEY_("]", "Increase the number of context lines"), \
581 ec2a9698 2022-09-15 mark KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
583 fd6badaa 2022-09-23 stsp KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
584 ec2a9698 2022-09-15 mark KEY_("Enter", "Display diff view of the selected line's commit"), \
585 ec2a9698 2022-09-15 mark KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
586 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the currently selected annotated line"), \
587 ec2a9698 2022-09-15 mark KEY_("C", "Reload view with the previously blamed commit"), \
588 ec2a9698 2022-09-15 mark KEY_("c", "Reload view with the version of the file found in the" \
589 ec2a9698 2022-09-15 mark " selected line's commit"), \
590 ec2a9698 2022-09-15 mark KEY_("p", "Reload view with the version of the file found in the" \
591 ec2a9698 2022-09-15 mark " selected line's parent commit"), \
593 fd6badaa 2022-09-23 stsp KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
594 ec2a9698 2022-09-15 mark KEY_("Enter", "Enter selected directory or open blame view of the" \
595 ec2a9698 2022-09-15 mark " selected file"), \
596 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the selected entry"), \
597 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
598 ec2a9698 2022-09-15 mark KEY_("i", "Show object IDs for all tree entries"), \
599 ec2a9698 2022-09-15 mark KEY_("Backspace", "Return to the parent directory"), \
601 fd6badaa 2022-09-23 stsp KEYMAP_("Ref view", TOG_KEYMAP_REF), \
602 ec2a9698 2022-09-15 mark KEY_("Enter", "Display log view of the selected reference"), \
603 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the selected reference"), \
604 ec2a9698 2022-09-15 mark KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
605 ec2a9698 2022-09-15 mark KEY_("m", "Toggle display of last modified date for each reference"), \
606 ec2a9698 2022-09-15 mark KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
607 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload view with all repository references")
609 ec2a9698 2022-09-15 mark struct tog_key_map {
610 ec2a9698 2022-09-15 mark const char *keys;
611 ec2a9698 2022-09-15 mark const char *info;
612 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
615 af21bb7e 2023-04-12 mark /* curses io for tog regress */
616 af21bb7e 2023-04-12 mark struct tog_io {
618 af21bb7e 2023-04-12 mark FILE *cout;
621 098596c5 2023-04-14 stsp static int using_mock_io;
623 af21bb7e 2023-04-12 mark #define TOG_SCREEN_DUMP "SCREENDUMP"
624 af21bb7e 2023-04-12 mark #define TOG_SCREEN_DUMP_LEN (sizeof(TOG_SCREEN_DUMP) - 1)
625 af21bb7e 2023-04-12 mark #define TOG_KEY_SCRDUMP SHRT_MIN
628 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
630 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
631 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
632 669b5ffa 2018-10-07 stsp * there is enough screen estate.
634 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
635 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
637 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
638 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
639 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
641 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
642 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
644 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
646 cc3c9aac 2018-08-01 stsp struct tog_view {
647 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
648 26ed57b2 2018-05-19 stsp WINDOW *window;
649 26ed57b2 2018-05-19 stsp PANEL *panel;
650 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
651 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
652 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
653 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
654 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
655 94b80cfa 2022-08-01 mark int gline, hiline; /* navigate to and highlight this nG line */
656 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
657 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
658 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
660 669b5ffa 2018-10-07 stsp struct tog_view *parent;
661 669b5ffa 2018-10-07 stsp struct tog_view *child;
664 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
665 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
666 e78dc838 2020-12-04 stsp * between parent and child.
667 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
668 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
669 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
670 e78dc838 2020-12-04 stsp * situations.
672 e78dc838 2020-12-04 stsp int focus_child;
674 9b058f45 2022-06-30 mark enum tog_view_mode mode;
675 5dc9f4bc 2018-08-04 stsp /* type-specific state */
676 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
678 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
679 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
680 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
681 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
682 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
683 ec2a9698 2022-09-15 mark struct tog_help_view_state help;
686 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
687 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
688 e78dc838 2020-12-04 stsp struct tog_view *, int);
689 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
690 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
691 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
693 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
694 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
695 eaf2c13e 2022-09-17 mark void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
696 eaf2c13e 2022-09-17 mark int **, int **, int **, int **);
697 c0c4acc8 2021-01-24 stsp int search_started;
698 60493ae3 2019-06-20 stsp int searching;
699 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
700 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
701 60493ae3 2019-06-20 stsp int search_next_done;
702 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
703 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
704 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
705 1803e47f 2019-06-22 stsp regex_t regex;
706 41605754 2020-11-12 stsp regmatch_t regmatch;
707 3f6c6614 2023-01-23 mark const char *action;
710 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
711 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
712 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
713 78756c87 2020-11-24 stsp struct got_repository *);
714 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
715 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
716 e78dc838 2020-12-04 stsp struct tog_view *, int);
717 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
718 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
719 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
720 eaf2c13e 2022-09-17 mark static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
721 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
722 ec2a9698 2022-09-15 mark static const struct got_error *search_next_view_match(struct tog_view *);
724 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
725 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
726 78756c87 2020-11-24 stsp const char *, const char *, int);
727 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
728 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
729 e78dc838 2020-12-04 stsp struct tog_view *, int);
730 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
731 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
732 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
733 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
735 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
736 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
737 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
738 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
739 e78dc838 2020-12-04 stsp struct tog_view *, int);
740 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
741 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
742 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
743 eaf2c13e 2022-09-17 mark static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
744 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
746 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
747 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
748 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
749 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
750 e78dc838 2020-12-04 stsp struct tog_view *, int);
751 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
752 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
753 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
755 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
756 6458efa5 2020-11-24 stsp struct got_repository *);
757 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
758 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
759 e78dc838 2020-12-04 stsp struct tog_view *, int);
760 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
761 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
762 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
764 eaf2c13e 2022-09-17 mark static const struct got_error *open_help_view(struct tog_view *,
765 eaf2c13e 2022-09-17 mark struct tog_view *);
766 eaf2c13e 2022-09-17 mark static const struct got_error *show_help_view(struct tog_view *);
767 eaf2c13e 2022-09-17 mark static const struct got_error *input_help_view(struct tog_view **,
768 eaf2c13e 2022-09-17 mark struct tog_view *, int);
769 eaf2c13e 2022-09-17 mark static const struct got_error *reset_help_view(struct tog_view *);
770 eaf2c13e 2022-09-17 mark static const struct got_error* close_help_view(struct tog_view *);
771 eaf2c13e 2022-09-17 mark static const struct got_error *search_start_help_view(struct tog_view *);
772 eaf2c13e 2022-09-17 mark static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
773 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
775 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
776 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
777 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
778 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
779 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
781 25791caa 2018-10-24 stsp static void
782 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
784 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
787 83baff54 2019-08-12 stsp static void
788 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
790 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
793 61266923 2020-01-14 stsp static void
794 61266923 2020-01-14 stsp tog_sigcont(int signo)
796 61266923 2020-01-14 stsp tog_sigcont_received = 1;
799 2497f032 2022-05-31 stsp static void
800 2497f032 2022-05-31 stsp tog_sigint(int signo)
802 2497f032 2022-05-31 stsp tog_sigint_received = 1;
805 2497f032 2022-05-31 stsp static void
806 2497f032 2022-05-31 stsp tog_sigterm(int signo)
808 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
812 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
814 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
815 f044c841 2022-10-24 stsp tog_sigint_received || tog_sigterm_received);
818 e5a0f69f 2018-08-18 stsp static const struct got_error *
819 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
821 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
823 669b5ffa 2018-10-07 stsp if (view->child) {
824 5629093a 2022-07-11 stsp child_err = view_close(view->child);
825 669b5ffa 2018-10-07 stsp view->child = NULL;
827 e5a0f69f 2018-08-18 stsp if (view->close)
828 e5a0f69f 2018-08-18 stsp err = view->close(view);
829 ea5e7bb5 2018-08-01 stsp if (view->panel)
830 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
831 ea5e7bb5 2018-08-01 stsp if (view->window)
832 ea5e7bb5 2018-08-01 stsp delwin(view->window);
833 ea5e7bb5 2018-08-01 stsp free(view);
834 5629093a 2022-07-11 stsp return err ? err : child_err;
837 ea5e7bb5 2018-08-01 stsp static struct tog_view *
838 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
839 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
841 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
843 ea5e7bb5 2018-08-01 stsp if (view == NULL)
844 ea5e7bb5 2018-08-01 stsp return NULL;
846 d6b05b5b 2018-08-04 stsp view->type = type;
847 f7d12f7e 2018-08-01 stsp view->lines = LINES;
848 f7d12f7e 2018-08-01 stsp view->cols = COLS;
849 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
850 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
851 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
852 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
853 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
854 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
855 96a765a8 2018-08-04 stsp view_close(view);
856 ea5e7bb5 2018-08-01 stsp return NULL;
858 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
859 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
860 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
861 96a765a8 2018-08-04 stsp view_close(view);
862 ea5e7bb5 2018-08-01 stsp return NULL;
865 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
866 ea5e7bb5 2018-08-01 stsp return view;
870 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
872 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
874 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
877 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
879 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
881 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
884 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
886 5c60c32a 2018-10-18 stsp static const struct got_error *
887 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
889 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
891 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
892 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
893 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
895 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
896 9b058f45 2022-06-30 mark view->begin_x = 0;
897 571ccd73 2022-07-19 mark } else if (!view->resized) {
898 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
899 3c1dfe12 2022-07-08 mark view->cols > 119)
900 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
902 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
903 9b058f45 2022-06-30 mark view->begin_y = 0;
905 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
906 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
907 5c60c32a 2018-10-18 stsp view->lines = LINES;
908 5c60c32a 2018-10-18 stsp view->cols = COLS;
909 5c60c32a 2018-10-18 stsp err = view_resize(view);
911 5c60c32a 2018-10-18 stsp return err;
913 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
914 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
916 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
917 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
919 5c60c32a 2018-10-18 stsp return NULL;
922 5c60c32a 2018-10-18 stsp static const struct got_error *
923 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
925 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
927 5c60c32a 2018-10-18 stsp view->begin_x = 0;
928 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
929 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
930 5c60c32a 2018-10-18 stsp view->ncols = COLS;
931 5c60c32a 2018-10-18 stsp view->lines = LINES;
932 5c60c32a 2018-10-18 stsp view->cols = COLS;
933 5c60c32a 2018-10-18 stsp err = view_resize(view);
935 5c60c32a 2018-10-18 stsp return err;
937 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
938 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
940 5c60c32a 2018-10-18 stsp return NULL;
944 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
946 5c60c32a 2018-10-18 stsp return view->parent == NULL;
950 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
952 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
956 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
958 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
962 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
964 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
965 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
968 9b058f45 2022-06-30 mark static void
969 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
971 9b058f45 2022-06-30 mark PANEL *panel;
972 9b058f45 2022-06-30 mark const struct tog_view *view_above;
974 9b058f45 2022-06-30 mark if (view->parent)
975 9b058f45 2022-06-30 mark return view_border(view->parent);
977 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
978 9b058f45 2022-06-30 mark if (panel == NULL)
981 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
982 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
983 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
984 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
985 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
987 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
988 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
991 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
992 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
993 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
994 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
995 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
997 4d8c2215 2018-08-19 stsp static const struct got_error *
998 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
1000 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
1001 9b058f45 2022-06-30 mark int dif, nlines, ncols;
1003 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
1005 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
1006 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
1008 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
1009 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
1010 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
1012 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1014 4dd27a72 2022-06-29 stsp if (view->child) {
1015 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
1017 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
1018 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
1019 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1020 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
1021 0dbbbe90 2022-06-17 op ncols = COLS;
1023 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1024 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1025 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1027 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1029 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
1031 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1032 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1035 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
1036 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
1037 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
1040 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
1042 9b058f45 2022-06-30 mark return err;
1043 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
1044 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1046 9b058f45 2022-06-30 mark return err;
1048 9b058f45 2022-06-30 mark view_border(view);
1049 9b058f45 2022-06-30 mark update_panels();
1050 9b058f45 2022-06-30 mark doupdate();
1051 9b058f45 2022-06-30 mark show_panel(view->child->panel);
1052 9b058f45 2022-06-30 mark nlines = view->nlines;
1054 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
1055 0dbbbe90 2022-06-17 op ncols = COLS;
1057 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
1058 571ccd73 2022-07-19 mark err = view->resize(view, dif);
1060 571ccd73 2022-07-19 mark return err;
1063 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
1064 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
1065 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
1066 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
1067 0dbbbe90 2022-06-17 op wclear(view->window);
1069 0dbbbe90 2022-06-17 op view->nlines = nlines;
1070 0dbbbe90 2022-06-17 op view->ncols = ncols;
1071 0dbbbe90 2022-06-17 op view->lines = LINES;
1072 0dbbbe90 2022-06-17 op view->cols = COLS;
1074 5c60c32a 2018-10-18 stsp return NULL;
1077 571ccd73 2022-07-19 mark static const struct got_error *
1078 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
1080 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
1081 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
1082 6fe51fee 2022-07-22 mark int n = 0;
1084 6fe51fee 2022-07-22 mark if (s->selected_entry)
1085 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
1088 571ccd73 2022-07-19 mark * Request commits to account for the increased
1089 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
1091 568eae95 2022-09-11 mark if (s->commits->ncommits < n) {
1092 568eae95 2022-09-11 mark view->nscrolled = n - s->commits->ncommits + increase + 1;
1093 571ccd73 2022-07-19 mark err = request_log_commits(view);
1096 571ccd73 2022-07-19 mark return err;
1099 d9a7ab53 2022-07-11 mark static void
1100 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
1102 d9a7ab53 2022-07-11 mark if (n == 0)
1105 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
1106 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
1107 d9a7ab53 2022-07-11 mark view->parent->offset += n;
1109 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
1110 d9a7ab53 2022-07-11 mark } else if (view->offset) {
1111 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
1112 d9a7ab53 2022-07-11 mark view->offset -= n;
1114 d9a7ab53 2022-07-11 mark view->offset = 0;
1118 3c1dfe12 2022-07-08 mark static const struct got_error *
1119 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
1121 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1122 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
1124 3c1dfe12 2022-07-08 mark if (view->parent)
1125 3c1dfe12 2022-07-08 mark v = view->parent;
1129 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
1130 3c1dfe12 2022-07-08 mark return NULL;
1132 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
1134 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1135 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
1136 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1137 3c1dfe12 2022-07-08 mark if (view->parent)
1138 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
1140 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
1141 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
1142 3c1dfe12 2022-07-08 mark view->count = 0;
1143 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
1144 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
1145 3c1dfe12 2022-07-08 mark view->count = 0;
1146 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
1148 3c1dfe12 2022-07-08 mark v->ncols = COLS;
1149 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
1150 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
1151 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
1153 3c1dfe12 2022-07-08 mark return err;
1154 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1156 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1157 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1158 3c1dfe12 2022-07-08 mark if (view->parent)
1159 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1161 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1162 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1163 3c1dfe12 2022-07-08 mark view->count = 0;
1164 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1165 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1166 3c1dfe12 2022-07-08 mark view->count = 0;
1167 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1169 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1172 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1173 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1174 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1175 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1177 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1179 3c1dfe12 2022-07-08 mark return err;
1180 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1182 3c1dfe12 2022-07-08 mark return err;
1184 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1185 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1187 3c1dfe12 2022-07-08 mark return err;
1190 6fe51fee 2022-07-22 mark if (v->resize)
1191 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1192 6fe51fee 2022-07-22 mark else if (v->child->resize)
1193 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1195 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1197 3c1dfe12 2022-07-08 mark return err;
1200 3c1dfe12 2022-07-08 mark static void
1201 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1203 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1205 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1206 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1209 669b5ffa 2018-10-07 stsp static const struct got_error *
1210 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1212 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1214 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1215 669b5ffa 2018-10-07 stsp return NULL;
1217 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1218 669b5ffa 2018-10-07 stsp view->child = NULL;
1219 669b5ffa 2018-10-07 stsp return err;
1222 0dbbbe90 2022-06-17 op static const struct got_error *
1223 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1225 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1227 669b5ffa 2018-10-07 stsp view->child = child;
1228 669b5ffa 2018-10-07 stsp child->parent = view;
1230 3c1dfe12 2022-07-08 mark err = view_resize(view);
1232 3c1dfe12 2022-07-08 mark return err;
1234 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1235 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1237 3c1dfe12 2022-07-08 mark return err;
1240 136e2bd4 2022-07-23 mark static const struct got_error *view_dispatch_request(struct tog_view **,
1241 136e2bd4 2022-07-23 mark struct tog_view *, enum tog_view_type, int, int);
1243 136e2bd4 2022-07-23 mark static const struct got_error *
1244 136e2bd4 2022-07-23 mark view_request_new(struct tog_view **requested, struct tog_view *view,
1245 136e2bd4 2022-07-23 mark enum tog_view_type request)
1247 136e2bd4 2022-07-23 mark struct tog_view *new_view = NULL;
1248 136e2bd4 2022-07-23 mark const struct got_error *err;
1249 136e2bd4 2022-07-23 mark int y = 0, x = 0;
1251 136e2bd4 2022-07-23 mark *requested = NULL;
1253 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1254 136e2bd4 2022-07-23 mark view_get_split(view, &y, &x);
1256 136e2bd4 2022-07-23 mark err = view_dispatch_request(&new_view, view, request, y, x);
1258 136e2bd4 2022-07-23 mark return err;
1260 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1261 94274a8f 2022-09-19 mark request != TOG_VIEW_HELP) {
1262 136e2bd4 2022-07-23 mark err = view_init_hsplit(view, y);
1264 136e2bd4 2022-07-23 mark return err;
1267 136e2bd4 2022-07-23 mark view->focussed = 0;
1268 136e2bd4 2022-07-23 mark new_view->focussed = 1;
1269 136e2bd4 2022-07-23 mark new_view->mode = view->mode;
1270 94274a8f 2022-09-19 mark new_view->nlines = request == TOG_VIEW_HELP ?
1271 94274a8f 2022-09-19 mark view->lines : view->lines - y;
1273 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1274 136e2bd4 2022-07-23 mark view_transfer_size(new_view, view);
1275 136e2bd4 2022-07-23 mark err = view_close_child(view);
1277 136e2bd4 2022-07-23 mark return err;
1278 136e2bd4 2022-07-23 mark err = view_set_child(view, new_view);
1280 136e2bd4 2022-07-23 mark return err;
1281 136e2bd4 2022-07-23 mark view->focus_child = 1;
1283 136e2bd4 2022-07-23 mark *requested = new_view;
1285 136e2bd4 2022-07-23 mark return NULL;
1288 34bc9ec9 2019-02-22 stsp static void
1289 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1291 25791caa 2018-10-24 stsp int cols, lines;
1292 25791caa 2018-10-24 stsp struct winsize size;
1294 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1295 25791caa 2018-10-24 stsp cols = 80; /* Default */
1296 25791caa 2018-10-24 stsp lines = 24;
1298 25791caa 2018-10-24 stsp cols = size.ws_col;
1299 25791caa 2018-10-24 stsp lines = size.ws_row;
1301 25791caa 2018-10-24 stsp resize_term(lines, cols);
1304 2b49a8ae 2019-06-22 stsp static const struct got_error *
1305 602eda79 2023-02-09 mark view_search_start(struct tog_view *view, int fast_refresh)
1307 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1308 9b058f45 2022-06-30 mark struct tog_view *v = view;
1309 2b49a8ae 2019-06-22 stsp char pattern[1024];
1312 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1313 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1314 c0c4acc8 2021-01-24 stsp view->searching = 0;
1315 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1317 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1319 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1320 2b49a8ae 2019-06-22 stsp return NULL;
1322 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1323 9b058f45 2022-06-30 mark v = view->child;
1324 9d0feb8b 2022-12-27 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1325 9d0feb8b 2022-12-27 mark v = view->parent;
1327 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1328 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1330 092a9f9c 2022-12-27 mark nodelay(v->window, FALSE); /* block for search term input */
1331 2b49a8ae 2019-06-22 stsp nocbreak();
1333 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1334 9b058f45 2022-06-30 mark wrefresh(v->window);
1337 092a9f9c 2022-12-27 mark nodelay(v->window, TRUE);
1338 098596c5 2023-04-14 stsp if (!fast_refresh && !using_mock_io)
1339 602eda79 2023-02-09 mark halfdelay(10);
1340 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1341 2b49a8ae 2019-06-22 stsp return NULL;
1343 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1344 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1345 7c32bd05 2019-06-22 stsp if (err) {
1346 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1347 7c32bd05 2019-06-22 stsp return err;
1349 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1350 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1351 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1352 2b49a8ae 2019-06-22 stsp view->search_next(view);
1355 2b49a8ae 2019-06-22 stsp return NULL;
1358 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1359 d2366e29 2022-07-07 mark static const struct got_error *
1360 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1362 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1363 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1365 d2366e29 2022-07-07 mark if (view->parent)
1366 d2366e29 2022-07-07 mark v = view->parent;
1370 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1371 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1373 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1375 7532ccda 2022-07-11 mark if (!v->child)
1376 7532ccda 2022-07-11 mark return NULL;
1377 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1378 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1380 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1381 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1382 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1383 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1384 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1387 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1388 d2366e29 2022-07-07 mark v->ncols = COLS;
1389 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1390 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1392 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1394 d2366e29 2022-07-07 mark return err;
1396 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1397 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1398 d2366e29 2022-07-07 mark v->focus_child = 1;
1400 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1402 d2366e29 2022-07-07 mark return err;
1403 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1405 d2366e29 2022-07-07 mark return err;
1407 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1408 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1409 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1410 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1412 279d2047 2022-07-29 stsp return err;
1413 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1415 279d2047 2022-07-29 stsp return err;
1417 7532ccda 2022-07-11 mark offset_selection_up(v);
1418 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1420 dff91825 2022-07-22 mark if (v->resize)
1421 dff91825 2022-07-22 mark err = v->resize(v, 0);
1422 dff91825 2022-07-22 mark else if (v->child->resize)
1423 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1425 d2366e29 2022-07-07 mark return err;
1429 af21bb7e 2023-04-12 mark * Strip trailing whitespace from str starting at byte *n;
1430 af21bb7e 2023-04-12 mark * if *n < 0, use strlen(str). Return new str length in *n.
1432 af21bb7e 2023-04-12 mark static void
1433 af21bb7e 2023-04-12 mark strip_trailing_ws(char *str, int *n)
1435 af21bb7e 2023-04-12 mark size_t x = *n;
1437 af21bb7e 2023-04-12 mark if (str == NULL || *str == '\0')
1440 af21bb7e 2023-04-12 mark if (x < 0)
1441 af21bb7e 2023-04-12 mark x = strlen(str);
1443 af21bb7e 2023-04-12 mark while (x-- > 0 && isspace((unsigned char)str[x]))
1444 af21bb7e 2023-04-12 mark str[x] = '\0';
1446 af21bb7e 2023-04-12 mark *n = x + 1;
1450 af21bb7e 2023-04-12 mark * Extract visible substring of line y from the curses screen
1451 af21bb7e 2023-04-12 mark * and strip trailing whitespace. If vline is set and locale is
1452 af21bb7e 2023-04-12 mark * UTF-8, overwrite line[vline] with '|' because the ACS_VLINE
1453 af21bb7e 2023-04-12 mark * character is written out as 'x'. Write the line to file f.
1455 af21bb7e 2023-04-12 mark static const struct got_error *
1456 af21bb7e 2023-04-12 mark view_write_line(FILE *f, int y, int vline)
1458 af21bb7e 2023-04-12 mark char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1461 af21bb7e 2023-04-12 mark r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1462 af21bb7e 2023-04-12 mark if (r == ERR)
1463 af21bb7e 2023-04-12 mark return got_error_fmt(GOT_ERR_RANGE,
1464 af21bb7e 2023-04-12 mark "failed to extract line %d", y);
1467 af21bb7e 2023-04-12 mark * In some views, lines are padded with blanks to COLS width.
1468 af21bb7e 2023-04-12 mark * Strip them so we can diff without the -b flag when testing.
1470 af21bb7e 2023-04-12 mark strip_trailing_ws(line, &r);
1472 af21bb7e 2023-04-12 mark if (vline > 0 && got_locale_is_utf8())
1473 af21bb7e 2023-04-12 mark line[vline] = '|';
1475 af21bb7e 2023-04-12 mark w = fprintf(f, "%s\n", line);
1476 af21bb7e 2023-04-12 mark if (w != r + 1) /* \n */
1477 af21bb7e 2023-04-12 mark return got_ferror(f, GOT_ERR_IO);
1479 af21bb7e 2023-04-12 mark return NULL;
1483 af21bb7e 2023-04-12 mark * Capture the visible curses screen by writing each line to the
1484 af21bb7e 2023-04-12 mark * file at the path set via the TOG_SCR_DUMP environment variable.
1486 af21bb7e 2023-04-12 mark static const struct got_error *
1487 af21bb7e 2023-04-12 mark screendump(struct tog_view *view)
1489 af21bb7e 2023-04-12 mark const struct got_error *err;
1490 af21bb7e 2023-04-12 mark FILE *f = NULL;
1491 af21bb7e 2023-04-12 mark const char *path;
1494 af21bb7e 2023-04-12 mark path = getenv("TOG_SCR_DUMP");
1495 af21bb7e 2023-04-12 mark if (path == NULL || *path == '\0')
1496 af21bb7e 2023-04-12 mark return got_error_msg(GOT_ERR_BAD_PATH,
1497 af21bb7e 2023-04-12 mark "TOG_SCR_DUMP path not set to capture screen dump");
1498 af21bb7e 2023-04-12 mark f = fopen(path, "wex");
1499 af21bb7e 2023-04-12 mark if (f == NULL)
1500 af21bb7e 2023-04-12 mark return got_error_from_errno_fmt("fopen: %s", path);
1502 af21bb7e 2023-04-12 mark if ((view->child && view->child->begin_x) ||
1503 af21bb7e 2023-04-12 mark (view->parent && view->begin_x)) {
1504 af21bb7e 2023-04-12 mark int ncols = view->child ? view->ncols : view->parent->ncols;
1506 af21bb7e 2023-04-12 mark /* vertical splitscreen */
1507 af21bb7e 2023-04-12 mark for (i = 0; i < view->nlines; ++i) {
1508 af21bb7e 2023-04-12 mark err = view_write_line(f, i, ncols - 1);
1510 af21bb7e 2023-04-12 mark goto done;
1513 af21bb7e 2023-04-12 mark int hline = 0;
1515 af21bb7e 2023-04-12 mark /* fullscreen or horizontal splitscreen */
1516 af21bb7e 2023-04-12 mark if ((view->child && view->child->begin_y) ||
1517 af21bb7e 2023-04-12 mark (view->parent && view->begin_y)) /* hsplit */
1518 af21bb7e 2023-04-12 mark hline = view->child ?
1519 af21bb7e 2023-04-12 mark view->child->begin_y : view->begin_y;
1521 af21bb7e 2023-04-12 mark for (i = 0; i < view->lines; i++) {
1522 af21bb7e 2023-04-12 mark if (hline && got_locale_is_utf8() && i == hline - 1) {
1525 af21bb7e 2023-04-12 mark /* ACS_HLINE writes out as 'q', overwrite it */
1526 af21bb7e 2023-04-12 mark for (c = 0; c < view->cols; ++c)
1527 af21bb7e 2023-04-12 mark fputc('-', f);
1528 af21bb7e 2023-04-12 mark fputc('\n', f);
1532 af21bb7e 2023-04-12 mark err = view_write_line(f, i, 0);
1534 af21bb7e 2023-04-12 mark goto done;
1539 af21bb7e 2023-04-12 mark if (f && fclose(f) == EOF && err == NULL)
1540 af21bb7e 2023-04-12 mark err = got_ferror(f, GOT_ERR_IO);
1541 af21bb7e 2023-04-12 mark return err;
1545 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1546 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1548 640cd7ff 2022-06-22 mark static int
1549 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1551 9b058f45 2022-06-30 mark struct tog_view *v = view;
1552 9b058f45 2022-06-30 mark int x, n = 0;
1554 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1555 9b058f45 2022-06-30 mark v = view->child;
1556 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1557 9b058f45 2022-06-30 mark v = view->parent;
1559 640cd7ff 2022-06-22 mark view->count = 0;
1560 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1561 94b80cfa 2022-08-01 mark nodelay(view->window, FALSE);
1562 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1563 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1564 9b058f45 2022-06-30 mark waddch(v->window, ':');
1567 9b058f45 2022-06-30 mark x = getcurx(v->window);
1568 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1569 9b058f45 2022-06-30 mark waddch(v->window, c);
1570 9b058f45 2022-06-30 mark wrefresh(v->window);
1574 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1575 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1577 640cd7ff 2022-06-22 mark if (n >= 9999999)
1578 640cd7ff 2022-06-22 mark n = 9999999;
1580 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1581 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1583 94b80cfa 2022-08-01 mark if (c == 'G' || c == 'g') { /* nG key map */
1584 94b80cfa 2022-08-01 mark view->gline = view->hiline = n;
1589 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1590 640cd7ff 2022-06-22 mark view->count = n;
1595 3f6c6614 2023-01-23 mark static void
1596 3f6c6614 2023-01-23 mark action_report(struct tog_view *view)
1598 3f6c6614 2023-01-23 mark struct tog_view *v = view;
1600 3f6c6614 2023-01-23 mark if (view_is_hsplit_top(view))
1601 3f6c6614 2023-01-23 mark v = view->child;
1602 3f6c6614 2023-01-23 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1603 3f6c6614 2023-01-23 mark v = view->parent;
1605 3f6c6614 2023-01-23 mark wmove(v->window, v->nlines - 1, 0);
1606 3f6c6614 2023-01-23 mark wclrtoeol(v->window);
1607 3f6c6614 2023-01-23 mark wprintw(v->window, ":%s", view->action);
1608 3f6c6614 2023-01-23 mark wrefresh(v->window);
1611 3f6c6614 2023-01-23 mark * Clear action status report. Only clear in blame view
1612 3f6c6614 2023-01-23 mark * once annotating is complete, otherwise it's too fast.
1614 3f6c6614 2023-01-23 mark if (view->type == TOG_VIEW_BLAME) {
1615 3f6c6614 2023-01-23 mark if (view->state.blame.blame_complete)
1616 3f6c6614 2023-01-23 mark view->action = NULL;
1618 3f6c6614 2023-01-23 mark view->action = NULL;
1622 af21bb7e 2023-04-12 mark * Read the next line from the test script and assign
1623 af21bb7e 2023-04-12 mark * key instruction to *ch. If at EOF, set the *done flag.
1625 0cf4efb1 2018-09-29 stsp static const struct got_error *
1626 af21bb7e 2023-04-12 mark tog_read_script_key(FILE *script, int *ch, int *done)
1628 af21bb7e 2023-04-12 mark const struct got_error *err = NULL;
1629 af21bb7e 2023-04-12 mark char *line = NULL;
1630 af21bb7e 2023-04-12 mark size_t linesz = 0;
1632 af21bb7e 2023-04-12 mark if (getline(&line, &linesz, script) == -1) {
1633 af21bb7e 2023-04-12 mark if (feof(script)) {
1634 af21bb7e 2023-04-12 mark *done = 1;
1635 af21bb7e 2023-04-12 mark goto done;
1637 af21bb7e 2023-04-12 mark err = got_ferror(script, GOT_ERR_IO);
1638 af21bb7e 2023-04-12 mark goto done;
1640 af21bb7e 2023-04-12 mark } else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1641 af21bb7e 2023-04-12 mark *ch = KEY_ENTER;
1642 af21bb7e 2023-04-12 mark else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1643 af21bb7e 2023-04-12 mark *ch = KEY_RIGHT;
1644 af21bb7e 2023-04-12 mark else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1645 af21bb7e 2023-04-12 mark *ch = KEY_LEFT;
1646 af21bb7e 2023-04-12 mark else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1647 af21bb7e 2023-04-12 mark *ch = KEY_DOWN;
1648 af21bb7e 2023-04-12 mark else if (strncasecmp(line, "KEY_UP", 6) == 0)
1649 af21bb7e 2023-04-12 mark *ch = KEY_UP;
1650 af21bb7e 2023-04-12 mark else if (strncasecmp(line, TOG_SCREEN_DUMP, TOG_SCREEN_DUMP_LEN) == 0)
1651 af21bb7e 2023-04-12 mark *ch = TOG_KEY_SCRDUMP;
1653 af21bb7e 2023-04-12 mark *ch = *line;
1656 af21bb7e 2023-04-12 mark free(line);
1657 af21bb7e 2023-04-12 mark return err;
1660 af21bb7e 2023-04-12 mark static const struct got_error *
1661 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1662 098596c5 2023-04-14 stsp struct tog_view_list_head *views, int fast_refresh)
1664 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1665 669b5ffa 2018-10-07 stsp struct tog_view *v;
1666 1a76625f 2018-10-22 stsp int ch, errcode;
1668 e5a0f69f 2018-08-18 stsp *new = NULL;
1670 3f6c6614 2023-01-23 mark if (view->action)
1671 3f6c6614 2023-01-23 mark action_report(view);
1673 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1674 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1675 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1676 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1677 640cd7ff 2022-06-22 mark view->count = 0;
1680 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1681 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1682 82954512 2020-02-03 stsp if (errcode)
1683 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1684 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1685 3da8ef85 2021-09-21 stsp sched_yield();
1686 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1687 82954512 2020-02-03 stsp if (errcode)
1688 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1689 82954512 2020-02-03 stsp "pthread_mutex_lock");
1690 60493ae3 2019-06-20 stsp view->search_next(view);
1691 60493ae3 2019-06-20 stsp return NULL;
1694 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1695 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1696 1a76625f 2018-10-22 stsp if (errcode)
1697 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1699 098596c5 2023-04-14 stsp if (using_mock_io) {
1700 098596c5 2023-04-14 stsp err = tog_read_script_key(tog_io.f, &ch, done);
1702 af21bb7e 2023-04-12 mark return err;
1703 af21bb7e 2023-04-12 mark } else if (view->count && --view->count) {
1705 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1706 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1707 af21bb7e 2023-04-12 mark /* let C-g or backspace abort unfinished count */
1708 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1709 a6d37fac 2022-07-03 mark view->count = 0;
1711 a6d37fac 2022-07-03 mark ch = view->ch;
1713 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1714 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1715 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1717 94b80cfa 2022-08-01 mark if (view->hiline && ch != ERR && ch != 0)
1718 94b80cfa 2022-08-01 mark view->hiline = 0; /* key pressed, clear line highlight */
1719 94b80cfa 2022-08-01 mark nodelay(view->window, TRUE);
1720 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1721 1a76625f 2018-10-22 stsp if (errcode)
1722 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1724 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1725 25791caa 2018-10-24 stsp tog_resizeterm();
1726 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1727 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1728 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1729 25791caa 2018-10-24 stsp err = view_resize(v);
1731 25791caa 2018-10-24 stsp return err;
1732 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1734 25791caa 2018-10-24 stsp return err;
1735 cdfcfb03 2020-12-06 stsp if (v->child) {
1736 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1738 cdfcfb03 2020-12-06 stsp return err;
1739 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1740 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1742 cdfcfb03 2020-12-06 stsp return err;
1743 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1744 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1746 3c1dfe12 2022-07-08 mark return err;
1752 e5a0f69f 2018-08-18 stsp switch (ch) {
1755 ec2a9698 2022-09-15 mark case KEY_F(1):
1756 ec2a9698 2022-09-15 mark if (view->type == TOG_VIEW_HELP)
1757 ec2a9698 2022-09-15 mark err = view->reset(view);
1759 ec2a9698 2022-09-15 mark err = view_request_new(new, view, TOG_VIEW_HELP);
1762 640cd7ff 2022-06-22 mark view->count = 0;
1763 1e37a5c2 2019-05-12 jcs if (view->child) {
1764 e78dc838 2020-12-04 stsp view->focussed = 0;
1765 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1766 e78dc838 2020-12-04 stsp view->focus_child = 1;
1767 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1768 e78dc838 2020-12-04 stsp view->focussed = 0;
1769 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1770 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1771 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1772 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1773 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1776 9b058f45 2022-06-30 mark return err;
1778 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1779 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1781 9b058f45 2022-06-30 mark return err;
1786 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1787 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1788 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1789 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1793 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1795 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1796 9970f7fc 2020-12-03 stsp view->dying = 1;
1802 640cd7ff 2022-06-22 mark view->count = 0;
1803 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1804 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1806 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1807 e78dc838 2020-12-04 stsp view->focussed = 0;
1808 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1809 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1811 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1813 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1817 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1818 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1820 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1821 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1822 e78dc838 2020-12-04 stsp view->focussed = 1;
1823 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1825 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1826 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1827 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1829 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1833 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1837 6fe51fee 2022-07-22 mark if (view->resize) {
1838 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1842 9b058f45 2022-06-30 mark if (view->parent)
1843 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1845 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1848 3c1dfe12 2022-07-08 mark view->count = 0;
1849 d2366e29 2022-07-07 mark err = switch_split(view);
1852 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1855 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1857 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1860 640cd7ff 2022-06-22 mark view->count = 0;
1861 60493ae3 2019-06-20 stsp if (view->search_start)
1862 602eda79 2023-02-09 mark view_search_start(view, fast_refresh);
1864 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1868 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1869 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1870 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1871 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1872 60493ae3 2019-06-20 stsp view->search_next(view);
1874 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1877 3f6c6614 2023-01-23 mark if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1878 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1879 3f6c6614 2023-01-23 mark view->action = "Patience diff algorithm";
1881 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1882 3f6c6614 2023-01-23 mark view->action = "Myers diff algorithm";
1884 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1885 917d79a7 2022-07-01 stsp if (v->reset) {
1886 917d79a7 2022-07-01 stsp err = v->reset(v);
1888 917d79a7 2022-07-01 stsp return err;
1890 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1891 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1893 917d79a7 2022-07-01 stsp return err;
1897 af21bb7e 2023-04-12 mark case TOG_KEY_SCRDUMP:
1898 af21bb7e 2023-04-12 mark err = screendump(view);
1901 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1905 e5a0f69f 2018-08-18 stsp return err;
1909 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1911 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1912 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1914 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1916 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1919 669b5ffa 2018-10-07 stsp return view->focussed;
1922 bcbd79e2 2018-08-19 stsp static const struct got_error *
1923 098596c5 2023-04-14 stsp tog_io_close(void)
1925 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1927 098596c5 2023-04-14 stsp if (tog_io.cin && fclose(tog_io.cin) == EOF)
1928 098596c5 2023-04-14 stsp err = got_ferror(tog_io.cin, GOT_ERR_IO);
1929 098596c5 2023-04-14 stsp if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1930 098596c5 2023-04-14 stsp err = got_ferror(tog_io.cout, GOT_ERR_IO);
1931 098596c5 2023-04-14 stsp if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1932 098596c5 2023-04-14 stsp err = got_ferror(tog_io.f, GOT_ERR_IO);
1934 af21bb7e 2023-04-12 mark return err;
1937 af21bb7e 2023-04-12 mark static const struct got_error *
1938 098596c5 2023-04-14 stsp view_loop(struct tog_view *view)
1940 af21bb7e 2023-04-12 mark const struct got_error *err = NULL;
1941 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1942 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1943 d2366e29 2022-07-07 mark char *mode;
1944 fd823528 2018-10-22 stsp int fast_refresh = 10;
1945 1a76625f 2018-10-22 stsp int done = 0, errcode;
1947 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1948 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1949 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1951 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1953 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1954 1a76625f 2018-10-22 stsp if (errcode)
1955 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1957 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1958 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1960 1004088d 2018-09-29 stsp view->focussed = 1;
1961 878940b7 2018-09-29 stsp err = view->show(view);
1963 0cf4efb1 2018-09-29 stsp return err;
1964 0cf4efb1 2018-09-29 stsp update_panels();
1965 0cf4efb1 2018-09-29 stsp doupdate();
1966 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1967 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1968 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1969 098596c5 2023-04-14 stsp if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
1970 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1972 098596c5 2023-04-14 stsp err = view_input(&new_view, &done, view, &views, fast_refresh);
1976 dc2c3344 2023-01-23 mark if (view->dying && view == TAILQ_FIRST(&views) &&
1977 dc2c3344 2023-01-23 mark TAILQ_NEXT(view, entry) == NULL)
1979 dc2c3344 2023-01-23 mark if (done) {
1980 dc2c3344 2023-01-23 mark struct tog_view *v;
1983 dc2c3344 2023-01-23 mark * When we quit, scroll the screen up a single line
1984 dc2c3344 2023-01-23 mark * so we don't lose any information.
1986 dc2c3344 2023-01-23 mark TAILQ_FOREACH(v, &views, entry) {
1987 dc2c3344 2023-01-23 mark wmove(v->window, 0, 0);
1988 dc2c3344 2023-01-23 mark wdeleteln(v->window);
1989 dc2c3344 2023-01-23 mark wnoutrefresh(v->window);
1990 dc2c3344 2023-01-23 mark if (v->child && !view_is_fullscreen(v)) {
1991 dc2c3344 2023-01-23 mark wmove(v->child->window, 0, 0);
1992 dc2c3344 2023-01-23 mark wdeleteln(v->child->window);
1993 dc2c3344 2023-01-23 mark wnoutrefresh(v->child->window);
1996 dc2c3344 2023-01-23 mark doupdate();
1999 9970f7fc 2020-12-03 stsp if (view->dying) {
2000 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
2002 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
2003 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
2005 e78dc838 2020-12-04 stsp else if (view->parent)
2006 669b5ffa 2018-10-07 stsp prev = view->parent;
2008 e78dc838 2020-12-04 stsp if (view->parent) {
2009 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
2010 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
2011 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
2012 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
2013 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
2016 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
2017 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
2019 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
2021 9970f7fc 2020-12-03 stsp err = view_close(view);
2023 e5a0f69f 2018-08-18 stsp goto done;
2025 e78dc838 2020-12-04 stsp view = NULL;
2026 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
2027 e78dc838 2020-12-04 stsp if (v->focussed)
2030 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
2031 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
2033 e78dc838 2020-12-04 stsp view = prev;
2034 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
2035 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
2036 e78dc838 2020-12-04 stsp tog_view_list_head);
2038 e78dc838 2020-12-04 stsp if (view) {
2039 e78dc838 2020-12-04 stsp if (view->focus_child) {
2040 e78dc838 2020-12-04 stsp view->child->focussed = 1;
2041 e78dc838 2020-12-04 stsp view = view->child;
2043 e78dc838 2020-12-04 stsp view->focussed = 1;
2047 bcbd79e2 2018-08-19 stsp if (new_view) {
2048 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
2049 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
2050 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2051 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
2053 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
2054 86c66b02 2018-10-18 stsp err = view_close(v);
2056 86c66b02 2018-10-18 stsp goto done;
2059 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
2060 fed7eaa8 2018-10-24 stsp view = new_view;
2062 dc2c3344 2023-01-23 mark if (view && !done) {
2063 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
2064 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
2065 e78dc838 2020-12-04 stsp view = view->child;
2067 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
2068 e78dc838 2020-12-04 stsp view = view->parent;
2070 e78dc838 2020-12-04 stsp show_panel(view->panel);
2071 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
2072 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
2073 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
2074 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
2076 1a76625f 2018-10-22 stsp goto done;
2078 669b5ffa 2018-10-07 stsp err = view->show(view);
2080 1a76625f 2018-10-22 stsp goto done;
2081 669b5ffa 2018-10-07 stsp if (view->child) {
2082 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
2084 1a76625f 2018-10-22 stsp goto done;
2086 1a76625f 2018-10-22 stsp update_panels();
2087 1a76625f 2018-10-22 stsp doupdate();
2091 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
2092 5629093a 2022-07-11 stsp const struct got_error *close_err;
2093 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
2094 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
2095 5629093a 2022-07-11 stsp close_err = view_close(view);
2096 5629093a 2022-07-11 stsp if (close_err && err == NULL)
2097 5629093a 2022-07-11 stsp err = close_err;
2100 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2101 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
2102 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2104 e5a0f69f 2018-08-18 stsp return err;
2107 4ed7e80c 2018-05-20 stsp __dead static void
2108 9f7d7167 2018-04-29 stsp usage_log(void)
2111 c70c5802 2018-08-01 stsp fprintf(stderr,
2112 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2113 9f7d7167 2018-04-29 stsp getprogname());
2117 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
2118 80ddbec8 2018-04-29 stsp static const struct got_error *
2119 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2121 00dfcb92 2018-06-11 stsp char *vis = NULL;
2122 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2124 963b370f 2018-05-20 stsp *ws = NULL;
2125 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
2126 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
2127 00dfcb92 2018-06-11 stsp int vislen;
2128 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
2129 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
2131 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
2132 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
2134 00dfcb92 2018-06-11 stsp return err;
2135 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
2136 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
2137 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
2138 a7f50699 2018-06-11 stsp goto done;
2142 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
2143 a7f50699 2018-06-11 stsp if (*ws == NULL) {
2144 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2145 a7f50699 2018-06-11 stsp goto done;
2148 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2149 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
2151 00dfcb92 2018-06-11 stsp free(vis);
2152 963b370f 2018-05-20 stsp if (err) {
2153 963b370f 2018-05-20 stsp free(*ws);
2154 963b370f 2018-05-20 stsp *ws = NULL;
2155 963b370f 2018-05-20 stsp *wlen = 0;
2157 963b370f 2018-05-20 stsp return err;
2160 145b6838 2022-06-16 stsp static const struct got_error *
2161 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
2163 145b6838 2022-06-16 stsp char *dst;
2164 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
2166 145b6838 2022-06-16 stsp *ptr = NULL;
2167 145b6838 2022-06-16 stsp n = len = strlen(src);
2168 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
2169 145b6838 2022-06-16 stsp if (dst == NULL)
2170 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
2172 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
2173 145b6838 2022-06-16 stsp const char c = src[idx];
2175 145b6838 2022-06-16 stsp if (c == '\t') {
2176 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
2179 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
2180 6e1c41ad 2022-06-16 mark if (p == NULL) {
2181 6e1c41ad 2022-06-16 mark free(dst);
2182 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
2187 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
2190 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
2194 145b6838 2022-06-16 stsp dst[sz] = '\0';
2195 145b6838 2022-06-16 stsp *ptr = dst;
2196 145b6838 2022-06-16 stsp return NULL;
2200 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
2201 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
2202 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
2206 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2208 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
2210 4e4a9ac8 2022-06-17 op if (n == 0) {
2211 4e4a9ac8 2022-06-17 op *rcol = cols;
2215 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
2216 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
2217 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2219 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
2221 4e4a9ac8 2022-06-17 op if (width == -1) {
2223 4e4a9ac8 2022-06-17 op wline[i] = L'.';
2226 4e4a9ac8 2022-06-17 op if (cols + width > n)
2228 4e4a9ac8 2022-06-17 op cols += width;
2231 4e4a9ac8 2022-06-17 op *rcol = cols;
2236 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
2237 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
2238 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2240 ccda2f4d 2022-06-16 stsp static const struct got_error *
2241 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2242 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2244 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2246 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2247 145b6838 2022-06-16 stsp char *exstr = NULL;
2248 963b370f 2018-05-20 stsp size_t wlen;
2249 4e4a9ac8 2022-06-17 op int i, scrollx;
2251 963b370f 2018-05-20 stsp *wlinep = NULL;
2252 b700b5d6 2018-07-10 stsp *widthp = 0;
2254 145b6838 2022-06-16 stsp if (expand) {
2255 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
2257 145b6838 2022-06-16 stsp return err;
2260 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2261 145b6838 2022-06-16 stsp free(exstr);
2263 963b370f 2018-05-20 stsp return err;
2265 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2267 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2268 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2271 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2272 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2276 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2277 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
2279 b700b5d6 2018-07-10 stsp if (widthp)
2280 b700b5d6 2018-07-10 stsp *widthp = cols;
2281 ccda2f4d 2022-06-16 stsp if (scrollxp)
2282 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
2284 963b370f 2018-05-20 stsp free(wline);
2286 963b370f 2018-05-20 stsp *wlinep = wline;
2287 963b370f 2018-05-20 stsp return err;
2290 8b473291 2019-02-21 stsp static const struct got_error*
2291 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2292 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2294 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2295 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2297 8b473291 2019-02-21 stsp const char *name;
2299 8b473291 2019-02-21 stsp *refs_str = NULL;
2301 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2302 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2303 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2306 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2307 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2309 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2310 8b473291 2019-02-21 stsp name += 5;
2311 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
2312 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
2314 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2315 8b473291 2019-02-21 stsp name += 6;
2316 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2317 8b473291 2019-02-21 stsp name += 8;
2318 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2319 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2322 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2325 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2326 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2327 5d844a1e 2019-08-13 stsp if (err) {
2328 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2329 48cae60d 2020-09-22 stsp free(ref_id);
2332 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2333 5d844a1e 2019-08-13 stsp err = NULL;
2334 5d844a1e 2019-08-13 stsp tag = NULL;
2337 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2338 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2339 48cae60d 2020-09-22 stsp free(ref_id);
2341 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2342 52b5abe1 2019-08-13 stsp if (cmp != 0)
2344 8b473291 2019-02-21 stsp s = *refs_str;
2345 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2346 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2347 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2349 8b473291 2019-02-21 stsp *refs_str = NULL;
2355 8b473291 2019-02-21 stsp return err;
2358 963b370f 2018-05-20 stsp static const struct got_error *
2359 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2360 27a741e5 2019-09-11 stsp int col_tab_align)
2362 e6b8b890 2020-12-29 naddy char *smallerthan;
2364 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2365 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2366 5813d178 2019-03-09 stsp author = smallerthan + 1;
2367 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2368 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
2369 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
2372 5813d178 2019-03-09 stsp static const struct got_error *
2373 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2374 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2375 8fdc79fe 2020-12-01 naddy int author_display_cols)
2377 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2378 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2379 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2380 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2381 5813d178 2019-03-09 stsp char *author = NULL;
2382 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
2383 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2384 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2385 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
2386 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2387 ccb26ccd 2018-11-05 stsp struct tm tm;
2388 45d799e2 2018-12-23 stsp time_t committer_time;
2389 11b20872 2019-11-08 stsp struct tog_color *tc;
2391 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2392 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2393 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2394 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2395 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2397 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2398 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2400 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2401 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2403 11b20872 2019-11-08 stsp wattr_on(view->window,
2404 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2405 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2407 11b20872 2019-11-08 stsp wattr_off(view->window,
2408 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2409 27a741e5 2019-09-11 stsp col = limit;
2410 b39d25c7 2018-07-10 stsp if (col > avail)
2411 b39d25c7 2018-07-10 stsp goto done;
2413 6570a66d 2019-11-08 stsp if (avail >= 120) {
2414 6570a66d 2019-11-08 stsp char *id_str;
2415 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2417 6570a66d 2019-11-08 stsp goto done;
2418 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2420 11b20872 2019-11-08 stsp wattr_on(view->window,
2421 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2422 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2424 11b20872 2019-11-08 stsp wattr_off(view->window,
2425 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2426 6570a66d 2019-11-08 stsp free(id_str);
2428 6570a66d 2019-11-08 stsp if (col > avail)
2429 6570a66d 2019-11-08 stsp goto done;
2432 10aab77f 2022-07-19 op if (s->use_committer)
2433 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2435 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2436 5813d178 2019-03-09 stsp if (author == NULL) {
2437 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2438 80ddbec8 2018-04-29 stsp goto done;
2440 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2442 bb737323 2018-05-20 stsp goto done;
2443 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2445 11b20872 2019-11-08 stsp wattr_on(view->window,
2446 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2447 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2448 bb737323 2018-05-20 stsp col += author_width;
2449 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2450 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2452 bb737323 2018-05-20 stsp author_width++;
2455 f0f62654 2022-09-08 mark wattr_off(view->window,
2456 f0f62654 2022-09-08 mark COLOR_PAIR(tc->colorpair), NULL);
2457 9c2eaf34 2018-05-20 stsp if (col > avail)
2458 9c2eaf34 2018-05-20 stsp goto done;
2460 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2462 6d9fbc00 2018-04-29 stsp goto done;
2463 bb737323 2018-05-20 stsp logmsg = logmsg0;
2464 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2466 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2467 bb737323 2018-05-20 stsp if (newline)
2468 bb737323 2018-05-20 stsp *newline = '\0';
2469 ccda2f4d 2022-06-16 stsp limit = avail - col;
2470 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2471 4d1f6af3 2022-06-17 op limit--; /* for the border */
2472 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2473 ccda2f4d 2022-06-16 stsp limit, col, 1);
2475 29688b02 2022-06-16 stsp goto done;
2476 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2477 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2478 27a741e5 2019-09-11 stsp while (col < avail) {
2479 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2483 80ddbec8 2018-04-29 stsp free(logmsg0);
2484 bb737323 2018-05-20 stsp free(wlogmsg);
2485 5813d178 2019-03-09 stsp free(author);
2486 bb737323 2018-05-20 stsp free(wauthor);
2487 80ddbec8 2018-04-29 stsp free(line);
2488 80ddbec8 2018-04-29 stsp return err;
2491 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2492 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2493 899d86c2 2018-05-10 stsp struct got_object_id *id)
2495 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2496 e15c42de 2022-09-05 op struct got_object_id *dup;
2498 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2499 80ddbec8 2018-04-29 stsp if (entry == NULL)
2500 899d86c2 2018-05-10 stsp return NULL;
2502 e15c42de 2022-09-05 op dup = got_object_id_dup(id);
2503 e15c42de 2022-09-05 op if (dup == NULL) {
2504 e15c42de 2022-09-05 op free(entry);
2505 e15c42de 2022-09-05 op return NULL;
2508 e15c42de 2022-09-05 op entry->id = dup;
2509 99db9666 2018-05-07 stsp entry->commit = commit;
2510 899d86c2 2018-05-10 stsp return entry;
2513 99db9666 2018-05-07 stsp static void
2514 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2516 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2518 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2519 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2520 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2521 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2522 e15c42de 2022-09-05 op free(entry->id);
2523 99db9666 2018-05-07 stsp free(entry);
2526 99db9666 2018-05-07 stsp static void
2527 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2529 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2530 99db9666 2018-05-07 stsp pop_commit(commits);
2533 c4972b91 2018-05-07 stsp static const struct got_error *
2534 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2535 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2537 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2538 13add988 2019-10-15 stsp regmatch_t regmatch;
2539 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2541 13add988 2019-10-15 stsp *have_match = 0;
2543 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2545 13add988 2019-10-15 stsp return err;
2547 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2549 13add988 2019-10-15 stsp goto done;
2551 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2552 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2553 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2554 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2555 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2556 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2557 13add988 2019-10-15 stsp *have_match = 1;
2559 13add988 2019-10-15 stsp free(id_str);
2560 13add988 2019-10-15 stsp free(logmsg);
2561 13add988 2019-10-15 stsp return err;
2564 13add988 2019-10-15 stsp static const struct got_error *
2565 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2567 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2570 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2571 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2572 1a76625f 2018-10-22 stsp * while updating the display.
2575 d9787ed8 2022-09-10 op struct got_object_id id;
2576 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2577 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2578 568eae95 2022-09-11 mark int limit_match = 0;
2579 1a76625f 2018-10-22 stsp int errcode;
2581 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2582 4e0d2870 2020-12-07 naddy NULL, NULL);
2586 d9787ed8 2022-09-10 op err = got_object_open_as_commit(&commit, a->repo, &id);
2589 d9787ed8 2022-09-10 op entry = alloc_commit_queue_entry(commit, &id);
2590 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2591 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2595 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2596 1a76625f 2018-10-22 stsp if (errcode) {
2597 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2598 13add988 2019-10-15 stsp "pthread_mutex_lock");
2602 568eae95 2022-09-11 mark entry->idx = a->real_commits->ncommits;
2603 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2604 568eae95 2022-09-11 mark a->real_commits->ncommits++;
2606 568eae95 2022-09-11 mark if (*a->limiting) {
2607 568eae95 2022-09-11 mark err = match_commit(&limit_match, &id, commit,
2608 568eae95 2022-09-11 mark a->limit_regex);
2612 568eae95 2022-09-11 mark if (limit_match) {
2613 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
2615 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(
2616 568eae95 2022-09-11 mark entry->commit, entry->id);
2617 568eae95 2022-09-11 mark if (matched == NULL) {
2618 568eae95 2022-09-11 mark err = got_error_from_errno(
2619 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
2622 34a842a4 2022-09-18 mark matched->commit = entry->commit;
2623 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
2625 568eae95 2022-09-11 mark matched->idx = a->limit_commits->ncommits;
2626 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->limit_commits->head,
2627 568eae95 2022-09-11 mark matched, entry);
2628 568eae95 2022-09-11 mark a->limit_commits->ncommits++;
2632 568eae95 2022-09-11 mark * This is how we signal log_thread() that we
2633 568eae95 2022-09-11 mark * have found a match, and that it should be
2634 568eae95 2022-09-11 mark * counted as a new entry for the view.
2636 568eae95 2022-09-11 mark a->limit_match = limit_match;
2639 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2640 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2641 7c1452c1 2020-03-26 stsp int have_match;
2642 d9787ed8 2022-09-10 op err = match_commit(&have_match, &id, commit, a->regex);
2646 568eae95 2022-09-11 mark if (*a->limiting) {
2647 568eae95 2022-09-11 mark if (limit_match && have_match)
2648 568eae95 2022-09-11 mark *a->search_next_done =
2649 568eae95 2022-09-11 mark TOG_SEARCH_HAVE_MORE;
2650 568eae95 2022-09-11 mark } else if (have_match)
2651 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2654 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2655 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2656 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2657 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2660 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2662 9ba79e04 2018-06-11 stsp return err;
2665 2b779855 2020-12-05 naddy static void
2666 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2668 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2669 2b779855 2020-12-05 naddy int ncommits = 0;
2671 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2672 2b779855 2020-12-05 naddy while (entry) {
2673 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2674 2b779855 2020-12-05 naddy s->selected_entry = entry;
2677 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2678 2b779855 2020-12-05 naddy ncommits++;
2682 0553a4e3 2018-04-30 stsp static const struct got_error *
2683 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2685 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2686 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2687 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2688 cbb0c8d7 2022-08-12 mark int limit = view->nlines;
2689 60493ae3 2019-06-20 stsp int width;
2690 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2691 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2692 8b473291 2019-02-21 stsp char *refs_str = NULL;
2693 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2694 11b20872 2019-11-08 stsp struct tog_color *tc;
2695 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2697 cbb0c8d7 2022-08-12 mark if (view_is_hsplit_top(view))
2698 cbb0c8d7 2022-08-12 mark --limit; /* account for border */
2700 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2701 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2702 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2703 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2705 ecb28ae0 2018-07-16 stsp return err;
2706 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2707 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2708 d2075bf3 2020-12-25 stsp if (refs) {
2709 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2710 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2712 d2075bf3 2020-12-25 stsp goto done;
2716 098596c5 2023-04-14 stsp if (s->thread_args.commits_needed == 0 && !using_mock_io)
2717 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2719 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2720 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2721 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2722 38d166d8 2022-09-23 stsp (view->searching && !view->search_next_done) ?
2723 38d166d8 2022-09-23 stsp "searching..." : "loading...") == -1) {
2724 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2725 8f4ed634 2020-03-26 stsp goto done;
2728 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2729 568eae95 2022-09-11 mark const char *limit_str = NULL;
2731 f9686aa5 2020-03-27 stsp if (view->searching) {
2732 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2733 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2734 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2735 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2736 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2737 f9686aa5 2020-03-27 stsp search_str = "searching...";
2740 568eae95 2022-09-11 mark if (s->limit_view && s->commits->ncommits == 0)
2741 568eae95 2022-09-11 mark limit_str = "no matches found";
2743 568eae95 2022-09-11 mark if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2744 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2745 568eae95 2022-09-11 mark search_str ? search_str : (refs_str ? refs_str : ""),
2746 568eae95 2022-09-11 mark limit_str ? limit_str : "") == -1) {
2747 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2748 8f4ed634 2020-03-26 stsp goto done;
2752 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2753 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2754 87411fa9 2022-06-16 stsp "........................................",
2755 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2756 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2757 1a76625f 2018-10-22 stsp header = NULL;
2758 1a76625f 2018-10-22 stsp goto done;
2760 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2761 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2762 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2763 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2764 1a76625f 2018-10-22 stsp header = NULL;
2765 1a76625f 2018-10-22 stsp goto done;
2767 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2769 1a76625f 2018-10-22 stsp goto done;
2771 2814baeb 2018-08-01 stsp werase(view->window);
2773 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2774 a3404814 2018-09-02 stsp wstandout(view->window);
2775 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2777 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2778 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2779 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2780 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2784 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2785 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2786 a3404814 2018-09-02 stsp wstandend(view->window);
2787 ecb28ae0 2018-07-16 stsp free(wline);
2788 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2789 1a76625f 2018-10-22 stsp goto done;
2791 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2792 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2793 5813d178 2019-03-09 stsp ncommits = 0;
2794 145b6838 2022-06-16 stsp view->maxx = 0;
2795 5813d178 2019-03-09 stsp while (entry) {
2796 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2797 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2798 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2799 5813d178 2019-03-09 stsp int width;
2800 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2802 10aab77f 2022-07-19 op if (s->use_committer)
2803 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2805 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2806 5813d178 2019-03-09 stsp if (author == NULL) {
2807 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2808 5813d178 2019-03-09 stsp goto done;
2810 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2811 27a741e5 2019-09-11 stsp date_display_cols);
2812 5813d178 2019-03-09 stsp if (author_cols < width)
2813 5813d178 2019-03-09 stsp author_cols = width;
2814 5813d178 2019-03-09 stsp free(wauthor);
2815 5813d178 2019-03-09 stsp free(author);
2816 a310d9c3 2022-07-21 florian if (err)
2817 a310d9c3 2022-07-21 florian goto done;
2818 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2820 145b6838 2022-06-16 stsp goto done;
2821 145b6838 2022-06-16 stsp msg = msg0;
2822 145b6838 2022-06-16 stsp while (*msg == '\n')
2824 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2825 29688b02 2022-06-16 stsp *eol = '\0';
2826 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2827 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2829 29688b02 2022-06-16 stsp goto done;
2830 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2831 145b6838 2022-06-16 stsp free(msg0);
2832 29688b02 2022-06-16 stsp free(wmsg);
2833 7ca04879 2019-10-19 stsp ncommits++;
2834 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2837 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2838 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2839 867c6645 2018-07-10 stsp ncommits = 0;
2840 899d86c2 2018-05-10 stsp while (entry) {
2841 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2843 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2844 2814baeb 2018-08-01 stsp wstandout(view->window);
2845 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2846 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2847 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2848 2814baeb 2018-08-01 stsp wstandend(view->window);
2850 60493ae3 2019-06-20 stsp goto done;
2851 0553a4e3 2018-04-30 stsp ncommits++;
2852 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2853 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2856 9b058f45 2022-06-30 mark view_border(view);
2858 1a76625f 2018-10-22 stsp free(id_str);
2859 8b473291 2019-02-21 stsp free(refs_str);
2860 1a76625f 2018-10-22 stsp free(ncommits_str);
2861 1a76625f 2018-10-22 stsp free(header);
2862 80ddbec8 2018-04-29 stsp return err;
2865 07b55e75 2018-05-10 stsp static void
2866 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2868 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2869 07b55e75 2018-05-10 stsp int nscrolled = 0;
2871 568eae95 2022-09-11 mark entry = TAILQ_FIRST(&s->commits->head);
2872 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2875 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2876 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2877 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2878 07b55e75 2018-05-10 stsp if (entry) {
2879 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2880 07b55e75 2018-05-10 stsp nscrolled++;
2885 aa075928 2018-05-10 stsp static const struct got_error *
2886 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2888 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2889 5e224a3e 2019-02-22 stsp int errcode;
2891 098596c5 2023-04-14 stsp if (!using_mock_io)
2892 098596c5 2023-04-14 stsp halfdelay(1); /* fast refresh while loading commits */
2894 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2895 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2896 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2897 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2898 7aafa0d1 2019-02-22 stsp if (errcode)
2899 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2900 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2903 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2904 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2906 7c1452c1 2020-03-26 stsp if (!wait)
2909 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2910 ffe38506 2020-12-01 naddy show_log_view(view);
2911 7c1452c1 2020-03-26 stsp update_panels();
2912 7c1452c1 2020-03-26 stsp doupdate();
2914 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2915 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2916 82954512 2020-02-03 stsp if (errcode)
2917 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2918 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2920 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2921 ffe38506 2020-12-01 naddy show_log_view(view);
2922 7c1452c1 2020-03-26 stsp update_panels();
2923 7c1452c1 2020-03-26 stsp doupdate();
2926 5e224a3e 2019-02-22 stsp return NULL;
2929 5e224a3e 2019-02-22 stsp static const struct got_error *
2930 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2932 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2933 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2935 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2936 2525dccb 2022-07-13 mark return NULL;
2938 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2939 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2940 9b058f45 2022-06-30 mark view->nscrolled = 0;
2942 9b058f45 2022-06-30 mark return err;
2945 9b058f45 2022-06-30 mark static const struct got_error *
2946 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2948 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2949 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2950 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2951 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2953 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2954 5e224a3e 2019-02-22 stsp return NULL;
2956 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2957 568eae95 2022-09-11 mark if (s->commits->ncommits < ncommits_needed &&
2958 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2960 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2962 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2963 568eae95 2022-09-11 mark ncommits_needed - s->commits->ncommits;
2964 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2966 5e224a3e 2019-02-22 stsp return err;
2970 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2971 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2974 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2975 d0407b86 2023-01-10 mark pentry : s->last_displayed_entry;
2977 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2978 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2980 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2981 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2983 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2984 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2986 9b058f45 2022-06-30 mark view->nscrolled = 0;
2988 dd0a52c1 2018-05-20 stsp return err;
2991 cd0acaa7 2018-05-20 stsp static const struct got_error *
2992 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2993 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2994 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2996 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2997 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2998 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
3000 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3001 15a94983 2018-12-23 stsp if (diff_view == NULL)
3002 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3004 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3005 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3006 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3007 e5a0f69f 2018-08-18 stsp if (err == NULL)
3008 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
3009 cd0acaa7 2018-05-20 stsp return err;
3012 80ddbec8 2018-04-29 stsp static const struct got_error *
3013 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
3014 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
3016 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
3018 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
3019 941e9f74 2019-05-21 stsp if (parent == NULL)
3020 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
3022 941e9f74 2019-05-21 stsp parent->tree = s->tree;
3023 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
3024 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
3025 941e9f74 2019-05-21 stsp parent->selected = s->selected;
3026 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3027 941e9f74 2019-05-21 stsp s->tree = subtree;
3028 941e9f74 2019-05-21 stsp s->selected = 0;
3029 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
3030 941e9f74 2019-05-21 stsp return NULL;
3033 941e9f74 2019-05-21 stsp static const struct got_error *
3034 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
3035 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
3037 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
3038 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
3039 941e9f74 2019-05-21 stsp const char *p;
3040 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
3042 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
3044 941e9f74 2019-05-21 stsp while (*p) {
3045 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3046 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
3047 56e0773d 2019-11-28 stsp char *te_name;
3049 33cbf02b 2020-01-12 stsp while (p[0] == '/')
3052 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
3053 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3054 941e9f74 2019-05-21 stsp if (slash == NULL)
3055 33cbf02b 2020-01-12 stsp te_name = strdup(p);
3057 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
3058 56e0773d 2019-11-28 stsp if (te_name == NULL) {
3059 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
3062 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
3063 56e0773d 2019-11-28 stsp if (te == NULL) {
3064 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3065 56e0773d 2019-11-28 stsp free(te_name);
3068 56e0773d 2019-11-28 stsp free(te_name);
3069 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
3071 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3072 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
3074 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3075 941e9f74 2019-05-21 stsp if (slash)
3076 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
3078 941e9f74 2019-05-21 stsp subpath = strdup(path);
3079 941e9f74 2019-05-21 stsp if (subpath == NULL) {
3080 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
3084 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
3089 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
3090 941e9f74 2019-05-21 stsp free(tree_id);
3094 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
3095 941e9f74 2019-05-21 stsp if (err) {
3096 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
3099 941e9f74 2019-05-21 stsp if (slash == NULL)
3101 941e9f74 2019-05-21 stsp free(subpath);
3102 941e9f74 2019-05-21 stsp subpath = NULL;
3103 941e9f74 2019-05-21 stsp p = slash;
3106 941e9f74 2019-05-21 stsp free(subpath);
3107 1a76625f 2018-10-22 stsp return err;
3110 61266923 2020-01-14 stsp static const struct got_error *
3111 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3112 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
3113 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
3115 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
3116 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
3117 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
3119 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3120 55cccc34 2020-02-20 stsp if (tree_view == NULL)
3121 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
3123 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3125 55cccc34 2020-02-20 stsp return err;
3126 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
3128 55cccc34 2020-02-20 stsp *new_view = tree_view;
3130 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
3131 55cccc34 2020-02-20 stsp return NULL;
3133 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
3136 55cccc34 2020-02-20 stsp static const struct got_error *
3137 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
3139 61266923 2020-01-14 stsp sigset_t sigset;
3140 61266923 2020-01-14 stsp int errcode;
3142 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
3143 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
3145 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3146 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
3147 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3148 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
3149 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
3150 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
3151 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3152 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
3153 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
3155 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
3156 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
3157 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3159 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3160 61266923 2020-01-14 stsp if (errcode)
3161 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
3163 61266923 2020-01-14 stsp return NULL;
3166 1a76625f 2018-10-22 stsp static void *
3167 1a76625f 2018-10-22 stsp log_thread(void *arg)
3169 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3170 1a76625f 2018-10-22 stsp int errcode = 0;
3171 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
3172 1a76625f 2018-10-22 stsp int done = 0;
3175 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
3176 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
3178 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
3179 5629093a 2022-07-11 stsp if (errcode) {
3180 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
3181 61266923 2020-01-14 stsp return (void *)err;
3184 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
3185 5629093a 2022-07-11 stsp if (err) {
3186 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
3187 5629093a 2022-07-11 stsp goto done;
3190 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
3191 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3192 5629093a 2022-07-11 stsp if (errcode) {
3193 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
3194 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
3195 5629093a 2022-07-11 stsp goto done;
3197 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3198 1a76625f 2018-10-22 stsp if (err) {
3199 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3200 5629093a 2022-07-11 stsp goto done;
3201 1a76625f 2018-10-22 stsp err = NULL;
3203 568eae95 2022-09-11 mark } else if (a->commits_needed > 0 && !a->load_all) {
3204 568eae95 2022-09-11 mark if (*a->limiting) {
3205 568eae95 2022-09-11 mark if (a->limit_match)
3206 568eae95 2022-09-11 mark a->commits_needed--;
3208 568eae95 2022-09-11 mark a->commits_needed--;
3211 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3212 3abe8080 2019-04-10 stsp if (errcode) {
3213 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3214 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3215 5629093a 2022-07-11 stsp goto done;
3216 3abe8080 2019-04-10 stsp } else if (*a->quit)
3218 568eae95 2022-09-11 mark else if (*a->limiting && *a->first_displayed_entry == NULL) {
3219 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3220 568eae95 2022-09-11 mark TAILQ_FIRST(&a->limit_commits->head);
3221 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3222 568eae95 2022-09-11 mark } else if (*a->first_displayed_entry == NULL) {
3223 568eae95 2022-09-11 mark *a->first_displayed_entry =
3224 568eae95 2022-09-11 mark TAILQ_FIRST(&a->real_commits->head);
3225 568eae95 2022-09-11 mark *a->selected_entry = *a->first_displayed_entry;
3228 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3229 7c1452c1 2020-03-26 stsp if (errcode) {
3230 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3231 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3232 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3233 5629093a 2022-07-11 stsp goto done;
3237 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3239 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3240 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3241 7c1452c1 2020-03-26 stsp &tog_mutex);
3242 5629093a 2022-07-11 stsp if (errcode) {
3243 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3244 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3245 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
3246 5629093a 2022-07-11 stsp goto done;
3248 21355643 2020-12-06 stsp if (*a->quit)
3253 3abe8080 2019-04-10 stsp a->log_complete = 1;
3254 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3255 5629093a 2022-07-11 stsp if (errcode)
3256 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3258 5629093a 2022-07-11 stsp if (err) {
3259 5629093a 2022-07-11 stsp tog_thread_error = 1;
3260 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
3262 1a76625f 2018-10-22 stsp return (void *)err;
3265 1a76625f 2018-10-22 stsp static const struct got_error *
3266 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3268 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
3269 1a76625f 2018-10-22 stsp int errcode;
3271 1a76625f 2018-10-22 stsp if (s->thread) {
3272 1a76625f 2018-10-22 stsp s->quit = 1;
3273 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3274 1a76625f 2018-10-22 stsp if (errcode)
3275 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3276 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3277 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3278 1a76625f 2018-10-22 stsp if (errcode)
3279 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3280 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3281 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
3282 1a76625f 2018-10-22 stsp if (errcode)
3283 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3284 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3285 1a76625f 2018-10-22 stsp if (errcode)
3286 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3287 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3288 1a76625f 2018-10-22 stsp s->thread = NULL;
3291 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3292 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3293 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3296 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
3297 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
3298 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
3299 74467cc8 2022-06-15 stsp if (err == NULL)
3300 74467cc8 2022-06-15 stsp err = pack_err;
3301 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
3304 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3305 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3306 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3309 5629093a 2022-07-11 stsp return err ? err : thread_err;
3312 9343a5fb 2018-06-23 stsp static const struct got_error *
3313 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3315 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3316 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3317 276b94a1 2020-11-13 naddy int errcode;
3319 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3321 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3322 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3323 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3325 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3326 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3327 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3329 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3330 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3331 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3332 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3333 1a76625f 2018-10-22 stsp free(s->start_id);
3334 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3335 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3336 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3337 1a76625f 2018-10-22 stsp return err;
3341 568eae95 2022-09-11 mark * We use two queues to implement the limit feature: first consists of
3342 568eae95 2022-09-11 mark * commits matching the current limit_regex; second is the real queue
3343 568eae95 2022-09-11 mark * of all known commits (real_commits). When the user starts limiting,
3344 568eae95 2022-09-11 mark * we swap queues such that all movement and displaying functionality
3345 568eae95 2022-09-11 mark * works with very slight change.
3347 1a76625f 2018-10-22 stsp static const struct got_error *
3348 568eae95 2022-09-11 mark limit_log_view(struct tog_view *view)
3350 568eae95 2022-09-11 mark struct tog_log_view_state *s = &view->state.log;
3351 568eae95 2022-09-11 mark struct commit_queue_entry *entry;
3352 568eae95 2022-09-11 mark struct tog_view *v = view;
3353 568eae95 2022-09-11 mark const struct got_error *err = NULL;
3354 568eae95 2022-09-11 mark char pattern[1024];
3357 568eae95 2022-09-11 mark if (view_is_hsplit_top(view))
3358 568eae95 2022-09-11 mark v = view->child;
3359 568eae95 2022-09-11 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3360 568eae95 2022-09-11 mark v = view->parent;
3362 568eae95 2022-09-11 mark /* Get the pattern */
3363 568eae95 2022-09-11 mark wmove(v->window, v->nlines - 1, 0);
3364 568eae95 2022-09-11 mark wclrtoeol(v->window);
3365 568eae95 2022-09-11 mark mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3366 568eae95 2022-09-11 mark nodelay(v->window, FALSE);
3367 568eae95 2022-09-11 mark nocbreak();
3369 568eae95 2022-09-11 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
3372 568eae95 2022-09-11 mark nodelay(v->window, TRUE);
3373 568eae95 2022-09-11 mark if (ret == ERR)
3374 568eae95 2022-09-11 mark return NULL;
3376 568eae95 2022-09-11 mark if (*pattern == '\0') {
3378 568eae95 2022-09-11 mark * Safety measure for the situation where the user
3379 568eae95 2022-09-11 mark * resets limit without previously limiting anything.
3381 568eae95 2022-09-11 mark if (!s->limit_view)
3382 568eae95 2022-09-11 mark return NULL;
3385 568eae95 2022-09-11 mark * User could have pressed Ctrl+L, which refreshed the
3386 568eae95 2022-09-11 mark * commit queues, it means we can't save previously
3387 568eae95 2022-09-11 mark * (before limit took place) displayed entries,
3388 568eae95 2022-09-11 mark * because they would point to already free'ed memory,
3389 568eae95 2022-09-11 mark * so we are forced to always select first entry of
3390 568eae95 2022-09-11 mark * the queue.
3392 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3393 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3394 568eae95 2022-09-11 mark s->selected_entry = s->first_displayed_entry;
3395 568eae95 2022-09-11 mark s->selected = 0;
3396 568eae95 2022-09-11 mark s->limit_view = 0;
3398 568eae95 2022-09-11 mark return NULL;
3401 568eae95 2022-09-11 mark if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3402 568eae95 2022-09-11 mark return NULL;
3404 568eae95 2022-09-11 mark s->limit_view = 1;
3406 568eae95 2022-09-11 mark /* Clear the screen while loading limit view */
3407 568eae95 2022-09-11 mark s->first_displayed_entry = NULL;
3408 568eae95 2022-09-11 mark s->last_displayed_entry = NULL;
3409 568eae95 2022-09-11 mark s->selected_entry = NULL;
3410 568eae95 2022-09-11 mark s->commits = &s->limit_commits;
3412 568eae95 2022-09-11 mark /* Prepare limit queue for new search */
3413 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3414 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3416 568eae95 2022-09-11 mark /* First process commits, which are in queue already */
3417 568eae95 2022-09-11 mark TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3418 568eae95 2022-09-11 mark int have_match = 0;
3420 568eae95 2022-09-11 mark err = match_commit(&have_match, entry->id,
3421 568eae95 2022-09-11 mark entry->commit, &s->limit_regex);
3423 568eae95 2022-09-11 mark return err;
3425 568eae95 2022-09-11 mark if (have_match) {
3426 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
3428 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(entry->commit,
3429 568eae95 2022-09-11 mark entry->id);
3430 568eae95 2022-09-11 mark if (matched == NULL) {
3431 568eae95 2022-09-11 mark err = got_error_from_errno(
3432 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
3435 34a842a4 2022-09-18 mark matched->commit = entry->commit;
3436 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
3438 568eae95 2022-09-11 mark matched->idx = s->limit_commits.ncommits;
3439 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&s->limit_commits.head,
3440 568eae95 2022-09-11 mark matched, entry);
3441 568eae95 2022-09-11 mark s->limit_commits.ncommits++;
3445 568eae95 2022-09-11 mark /* Second process all the commits, until we fill the screen */
3446 568eae95 2022-09-11 mark if (s->limit_commits.ncommits < view->nlines - 1 &&
3447 568eae95 2022-09-11 mark !s->thread_args.log_complete) {
3448 568eae95 2022-09-11 mark s->thread_args.commits_needed +=
3449 568eae95 2022-09-11 mark view->nlines - s->limit_commits.ncommits - 1;
3450 568eae95 2022-09-11 mark err = trigger_log_thread(view, 1);
3452 568eae95 2022-09-11 mark return err;
3455 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3456 568eae95 2022-09-11 mark s->selected_entry = TAILQ_FIRST(&s->commits->head);
3457 568eae95 2022-09-11 mark s->selected = 0;
3459 568eae95 2022-09-11 mark return NULL;
3462 568eae95 2022-09-11 mark static const struct got_error *
3463 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3465 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3467 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3468 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3469 60493ae3 2019-06-20 stsp return NULL;
3472 60493ae3 2019-06-20 stsp static const struct got_error *
3473 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3475 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3476 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3477 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3479 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3480 f9686aa5 2020-03-27 stsp show_log_view(view);
3481 f9686aa5 2020-03-27 stsp update_panels();
3482 f9686aa5 2020-03-27 stsp doupdate();
3484 96e2b566 2019-07-08 stsp if (s->search_entry) {
3485 13add988 2019-10-15 stsp int errcode, ch;
3486 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3487 13add988 2019-10-15 stsp if (errcode)
3488 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3489 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3490 13add988 2019-10-15 stsp ch = wgetch(view->window);
3491 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3492 13add988 2019-10-15 stsp if (errcode)
3493 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3494 13add988 2019-10-15 stsp "pthread_mutex_lock");
3495 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3496 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3497 678cbce5 2019-07-28 stsp return NULL;
3499 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3500 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3502 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3503 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3504 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3506 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
3507 f704b35c 2022-06-18 stsp * the position from where we should continue searching
3508 f704b35c 2022-06-18 stsp * might have changed.
3510 f83ada34 2022-09-13 mark if (view->searching == TOG_SEARCH_FORWARD)
3511 f83ada34 2022-09-13 mark entry = TAILQ_NEXT(s->selected_entry, entry);
3513 f83ada34 2022-09-13 mark entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3516 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
3519 60493ae3 2019-06-20 stsp while (1) {
3520 13add988 2019-10-15 stsp int have_match = 0;
3522 60493ae3 2019-06-20 stsp if (entry == NULL) {
3523 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3524 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3525 f9967bca 2020-03-27 stsp view->search_next_done =
3526 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3527 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3528 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3529 f801134a 2019-06-25 stsp return NULL;
3532 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3533 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3534 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3536 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3537 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3540 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3541 13add988 2019-10-15 stsp &view->regex);
3544 13add988 2019-10-15 stsp if (have_match) {
3545 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3546 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3550 96e2b566 2019-07-08 stsp s->search_entry = entry;
3551 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3552 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3554 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3557 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3558 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3559 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3560 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3562 60493ae3 2019-06-20 stsp return err;
3565 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3566 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3568 60493ae3 2019-06-20 stsp return err;
3573 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3575 60493ae3 2019-06-20 stsp return NULL;
3578 60493ae3 2019-06-20 stsp static const struct got_error *
3579 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3580 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3581 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3583 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3584 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3585 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3586 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3587 1a76625f 2018-10-22 stsp int errcode;
3589 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3590 f135c941 2020-02-20 stsp free(s->in_repo_path);
3591 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3592 2ca2f982 2023-04-14 mark if (s->in_repo_path == NULL) {
3593 2ca2f982 2023-04-14 mark err = got_error_from_errno("strdup");
3594 2ca2f982 2023-04-14 mark goto done;
3598 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3599 568eae95 2022-09-11 mark TAILQ_INIT(&s->real_commits.head);
3600 568eae95 2022-09-11 mark s->real_commits.ncommits = 0;
3601 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3603 568eae95 2022-09-11 mark TAILQ_INIT(&s->limit_commits.head);
3604 568eae95 2022-09-11 mark s->limit_view = 0;
3605 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3607 fb2756b9 2018-08-04 stsp s->repo = repo;
3608 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3609 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3610 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3611 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3612 9cd7cbd1 2020-12-07 stsp goto done;
3615 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3616 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3617 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3618 5036bf37 2018-09-24 stsp goto done;
3620 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3621 3bf00f25 2023-01-02 stsp s->use_committer = 1;
3623 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3624 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3625 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3626 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3628 11b20872 2019-11-08 stsp goto done;
3629 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3630 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3631 11b20872 2019-11-08 stsp if (err) {
3632 11b20872 2019-11-08 stsp free_colors(&s->colors);
3633 11b20872 2019-11-08 stsp goto done;
3635 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3636 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3637 11b20872 2019-11-08 stsp if (err) {
3638 11b20872 2019-11-08 stsp free_colors(&s->colors);
3639 11b20872 2019-11-08 stsp goto done;
3643 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3644 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3645 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3646 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3647 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3648 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3650 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3651 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3653 74467cc8 2022-06-15 stsp goto done;
3655 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3656 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3657 0ae84acc 2022-06-15 tracey if (err)
3658 0ae84acc 2022-06-15 tracey goto done;
3659 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3660 b672a97a 2020-01-27 stsp !s->log_branches);
3662 1a76625f 2018-10-22 stsp goto done;
3663 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3664 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3666 c5b78334 2020-01-12 stsp goto done;
3668 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3669 1a76625f 2018-10-22 stsp if (errcode) {
3670 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3671 1a76625f 2018-10-22 stsp goto done;
3673 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3674 7c1452c1 2020-03-26 stsp if (errcode) {
3675 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3676 7c1452c1 2020-03-26 stsp goto done;
3679 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3680 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3681 568eae95 2022-09-11 mark s->thread_args.real_commits = &s->real_commits;
3682 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3683 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3684 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3685 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3686 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3687 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3688 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3689 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3690 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3691 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3692 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3693 568eae95 2022-09-11 mark s->thread_args.limiting = &s->limit_view;
3694 568eae95 2022-09-11 mark s->thread_args.limit_regex = &s->limit_regex;
3695 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3697 2ca2f982 2023-04-14 mark if (err) {
3698 2ca2f982 2023-04-14 mark if (view->close == NULL)
3699 2ca2f982 2023-04-14 mark close_log_view(view);
3700 2ca2f982 2023-04-14 mark view_close(view);
3702 ba4f502b 2018-08-04 stsp return err;
3705 e5a0f69f 2018-08-18 stsp static const struct got_error *
3706 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3708 f2f6d207 2020-11-24 stsp const struct got_error *err;
3709 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3711 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3712 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3713 2b380cc8 2018-10-24 stsp &s->thread_args);
3714 2b380cc8 2018-10-24 stsp if (errcode)
3715 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3716 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3717 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3719 f2f6d207 2020-11-24 stsp return err;
3723 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3726 b880cc75 2022-06-30 mark static void
3727 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3729 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3731 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3733 568eae95 2022-09-11 mark if (s->selected_entry->idx == 0)
3734 568eae95 2022-09-11 mark view->count = 0;
3736 568eae95 2022-09-11 mark if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3738 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3740 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3741 b880cc75 2022-06-30 mark --s->selected;
3743 568eae95 2022-09-11 mark log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3745 b880cc75 2022-06-30 mark select_commit(s);
3749 b880cc75 2022-06-30 mark static const struct got_error *
3750 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3752 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3753 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3754 631e7531 2022-08-12 mark int eos = view->nlines - 2;
3756 568eae95 2022-09-11 mark if (s->first_displayed_entry == NULL)
3757 568eae95 2022-09-11 mark return NULL;
3759 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3760 568eae95 2022-09-11 mark s->selected_entry->idx >= s->commits->ncommits - 1)
3761 b880cc75 2022-06-30 mark return NULL;
3763 631e7531 2022-08-12 mark if (view_is_hsplit_top(view))
3764 631e7531 2022-08-12 mark --eos; /* border consumes the last line */
3766 631e7531 2022-08-12 mark if (!page) {
3767 568eae95 2022-09-11 mark if (s->selected < MIN(eos, s->commits->ncommits - 1))
3768 b880cc75 2022-06-30 mark ++s->selected;
3770 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3771 11edf34c 2022-08-12 mark } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3772 631e7531 2022-08-12 mark struct commit_queue_entry *entry;
3775 631e7531 2022-08-12 mark s->selected = 0;
3776 568eae95 2022-09-11 mark entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3777 631e7531 2022-08-12 mark s->last_displayed_entry = entry;
3778 631e7531 2022-08-12 mark for (n = 0; n <= eos; n++) {
3779 631e7531 2022-08-12 mark if (entry == NULL)
3781 631e7531 2022-08-12 mark s->first_displayed_entry = entry;
3782 631e7531 2022-08-12 mark entry = TAILQ_PREV(entry, commit_queue_head, entry);
3784 631e7531 2022-08-12 mark if (n > 0)
3785 631e7531 2022-08-12 mark s->selected = n - 1;
3787 568eae95 2022-09-11 mark if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3788 cbb0c8d7 2022-08-12 mark s->thread_args.log_complete)
3789 cbb0c8d7 2022-08-12 mark s->selected += MIN(page,
3790 568eae95 2022-09-11 mark s->commits->ncommits - s->selected_entry->idx - 1);
3792 cbb0c8d7 2022-08-12 mark err = log_scroll_down(view, page);
3795 b880cc75 2022-06-30 mark return err;
3798 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3799 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3801 374f69dd 2022-08-13 stsp if (s->first_displayed_entry && s->last_displayed_entry) {
3802 374f69dd 2022-08-13 stsp s->selected = MIN(s->selected,
3803 374f69dd 2022-08-13 stsp s->last_displayed_entry->idx -
3804 374f69dd 2022-08-13 stsp s->first_displayed_entry->idx);
3807 b880cc75 2022-06-30 mark select_commit(s);
3809 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3810 568eae95 2022-09-11 mark s->selected_entry->idx == s->commits->ncommits - 1)
3811 b880cc75 2022-06-30 mark view->count = 0;
3813 b880cc75 2022-06-30 mark return NULL;
3816 9b058f45 2022-06-30 mark static void
3817 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3822 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3823 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3824 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3825 7532ccda 2022-07-11 mark else if (view->resized_y)
3826 7532ccda 2022-07-11 mark *y = view->resized_y;
3828 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3829 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3830 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3831 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3832 7532ccda 2022-07-11 mark else if (view->resized_x)
3833 7532ccda 2022-07-11 mark *x = view->resized_x;
3835 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3839 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3840 e5a0f69f 2018-08-18 stsp static const struct got_error *
3841 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3843 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3845 9b058f45 2022-06-30 mark view->nlines = y;
3846 d2366e29 2022-07-07 mark view->ncols = COLS;
3847 9b058f45 2022-06-30 mark err = view_resize(view);
3849 9b058f45 2022-06-30 mark return err;
3851 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3853 9b058f45 2022-06-30 mark return err;
3856 94b80cfa 2022-08-01 mark static const struct got_error *
3857 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3859 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3860 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3861 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3863 374f69dd 2022-08-13 stsp if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3864 374f69dd 2022-08-13 stsp return NULL;
3866 94b80cfa 2022-08-01 mark g = view->gline;
3867 94b80cfa 2022-08-01 mark view->gline = 0;
3869 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3870 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3871 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3872 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3873 94b80cfa 2022-08-01 mark select_commit(s);
3874 94b80cfa 2022-08-01 mark return NULL;
3877 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3878 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3879 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3880 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3881 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3883 94b80cfa 2022-08-01 mark return err;
3884 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3885 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3887 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3888 94b80cfa 2022-08-01 mark s->selected = g - 1;
3890 94b80cfa 2022-08-01 mark select_commit(s);
3891 94b80cfa 2022-08-01 mark return NULL;
3895 d627976f 2023-02-01 mark static void
3896 d627976f 2023-02-01 mark horizontal_scroll_input(struct tog_view *view, int ch)
3899 d627976f 2023-02-01 mark switch (ch) {
3900 d627976f 2023-02-01 mark case KEY_LEFT:
3902 d627976f 2023-02-01 mark view->x -= MIN(view->x, 2);
3903 d627976f 2023-02-01 mark if (view->x <= 0)
3904 d627976f 2023-02-01 mark view->count = 0;
3906 d627976f 2023-02-01 mark case KEY_RIGHT:
3908 d627976f 2023-02-01 mark if (view->x + view->ncols / 2 < view->maxx)
3909 d627976f 2023-02-01 mark view->x += 2;
3911 d627976f 2023-02-01 mark view->count = 0;
3914 d627976f 2023-02-01 mark view->x = 0;
3917 d627976f 2023-02-01 mark view->x = MAX(view->maxx - view->ncols / 2, 0);
3918 d627976f 2023-02-01 mark view->count = 0;
3925 9b058f45 2022-06-30 mark static const struct got_error *
3926 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3928 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3929 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3930 631e7531 2022-08-12 mark int eos, nscroll;
3932 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3933 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3934 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3935 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3936 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3937 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3940 11edf34c 2022-08-12 mark return err;
3943 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3944 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3945 0dca135e 2022-06-30 mark --eos; /* border */
3947 94b80cfa 2022-08-01 mark if (view->gline)
3948 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3950 528dedf3 2021-08-30 stsp switch (ch) {
3952 568eae95 2022-09-11 mark err = limit_log_view(view);
3955 1e37a5c2 2019-05-12 jcs s->quit = 1;
3959 145b6838 2022-06-16 stsp case KEY_RIGHT:
3961 145b6838 2022-06-16 stsp case KEY_LEFT:
3963 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
3966 1e37a5c2 2019-05-12 jcs case KEY_UP:
3969 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3970 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3974 912a3f79 2021-08-30 j case KEY_HOME:
3975 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3976 640cd7ff 2022-06-22 mark view->count = 0;
3978 83cc4199 2022-06-13 stsp case CTRL('u'):
3980 83cc4199 2022-06-13 stsp nscroll /= 2;
3981 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3982 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3983 a4292ac5 2019-05-12 jcs case CTRL('b'):
3985 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3988 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3991 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3992 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3995 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3996 3f6c6614 2023-01-23 mark view->action = s->use_committer ?
3997 3f6c6614 2023-01-23 mark "show committer" : "show commit author";
4001 912a3f79 2021-08-30 j case KEY_END: {
4002 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
4003 912a3f79 2021-08-30 j * traverse them all. */
4004 640cd7ff 2022-06-22 mark view->count = 0;
4005 631e7531 2022-08-12 mark s->thread_args.load_all = 1;
4006 631e7531 2022-08-12 mark if (!s->thread_args.log_complete)
4007 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
4008 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
4009 631e7531 2022-08-12 mark s->thread_args.load_all = 0;
4012 80b7e8da 2022-06-11 stsp case CTRL('d'):
4014 83cc4199 2022-06-13 stsp nscroll /= 2;
4015 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4016 83cc4199 2022-06-13 stsp case KEY_NPAGE:
4017 61417565 2022-06-20 mark case CTRL('f'):
4018 48bb96f0 2022-06-20 naddy case 'f':
4020 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
4022 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4023 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
4024 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
4025 568eae95 2022-09-11 mark if (s->selected > s->commits->ncommits - 1)
4026 568eae95 2022-09-11 mark s->selected = s->commits->ncommits - 1;
4027 2b779855 2020-12-05 naddy select_commit(s);
4028 568eae95 2022-09-11 mark if (s->commits->ncommits < view->nlines - 1 &&
4029 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
4030 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
4031 568eae95 2022-09-11 mark s->commits->ncommits;
4032 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
4035 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4036 49b24ee5 2022-07-03 mark case '\r':
4037 640cd7ff 2022-06-22 mark view->count = 0;
4038 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4040 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4043 640cd7ff 2022-06-22 mark view->count = 0;
4044 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4046 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
4048 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4049 21355643 2020-12-06 stsp case CTRL('l'):
4051 640cd7ff 2022-06-22 mark view->count = 0;
4052 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
4053 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
4055 21355643 2020-12-06 stsp err = stop_log_thread(s);
4057 74cfe85e 2020-10-20 stsp return err;
4058 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
4059 21355643 2020-12-06 stsp char *parent_path;
4060 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
4062 21355643 2020-12-06 stsp return err;
4063 21355643 2020-12-06 stsp free(s->in_repo_path);
4064 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
4065 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
4066 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
4067 21355643 2020-12-06 stsp struct got_object_id *start_id;
4068 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
4069 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4070 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4071 ea371198 2022-11-17 stsp if (err) {
4072 ea371198 2022-11-17 stsp if (s->head_ref_name == NULL ||
4073 ea371198 2022-11-17 stsp err->code != GOT_ERR_NOT_REF)
4074 ea371198 2022-11-17 stsp return err;
4075 ea371198 2022-11-17 stsp /* Try to cope with deleted references. */
4076 ea371198 2022-11-17 stsp free(s->head_ref_name);
4077 ea371198 2022-11-17 stsp s->head_ref_name = NULL;
4078 ea371198 2022-11-17 stsp err = got_repo_match_object_id(&start_id,
4079 ea371198 2022-11-17 stsp NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4080 ea371198 2022-11-17 stsp &tog_refs, s->repo);
4082 ea371198 2022-11-17 stsp return err;
4084 21355643 2020-12-06 stsp free(s->start_id);
4085 21355643 2020-12-06 stsp s->start_id = start_id;
4086 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
4087 21355643 2020-12-06 stsp } else /* 'B' */
4088 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
4090 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
4091 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4093 b0dd8d27 2022-06-16 stsp return err;
4095 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
4096 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
4097 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
4099 21355643 2020-12-06 stsp return err;
4100 51a10b52 2020-12-26 stsp tog_free_refs();
4101 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
4103 ca51c541 2020-12-07 stsp return err;
4104 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
4105 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
4107 d01904d4 2019-06-25 stsp return err;
4108 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
4109 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
4111 b672a97a 2020-01-27 stsp return err;
4112 568eae95 2022-09-11 mark free_commits(&s->real_commits);
4113 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
4114 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
4115 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
4116 21355643 2020-12-06 stsp s->selected_entry = NULL;
4117 21355643 2020-12-06 stsp s->selected = 0;
4118 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
4119 21355643 2020-12-06 stsp s->quit = 0;
4120 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
4121 dfee752e 2022-06-18 op s->matched_entry = NULL;
4122 dfee752e 2022-06-18 op s->search_entry = NULL;
4123 01a7bcaf 2022-07-22 mark view->offset = 0;
4126 640cd7ff 2022-06-22 mark view->count = 0;
4127 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
4130 640cd7ff 2022-06-22 mark view->count = 0;
4134 80ddbec8 2018-04-29 stsp return err;
4137 4ed7e80c 2018-05-20 stsp static const struct got_error *
4138 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
4140 c2db6724 2019-01-04 stsp const struct got_error *error;
4142 37c06ea4 2019-07-15 stsp #ifdef PROFILE
4143 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
4144 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
4146 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
4147 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
4149 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
4150 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
4152 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4153 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4155 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
4156 c2db6724 2019-01-04 stsp if (error != NULL)
4157 c2db6724 2019-01-04 stsp return error;
4159 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
4160 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
4162 c2db6724 2019-01-04 stsp return NULL;
4165 af21bb7e 2023-04-12 mark static const struct got_error *
4166 098596c5 2023-04-14 stsp init_mock_term(const char *test_script_path)
4168 af21bb7e 2023-04-12 mark const struct got_error *err = NULL;
4170 af21bb7e 2023-04-12 mark if (test_script_path == NULL || *test_script_path == '\0')
4171 af21bb7e 2023-04-12 mark return got_error_msg(GOT_ERR_IO, "GOT_TOG_TEST not defined");
4173 098596c5 2023-04-14 stsp tog_io.f = fopen(test_script_path, "re");
4174 098596c5 2023-04-14 stsp if (tog_io.f == NULL) {
4175 af21bb7e 2023-04-12 mark err = got_error_from_errno_fmt("fopen: %s",
4176 af21bb7e 2023-04-12 mark test_script_path);
4177 af21bb7e 2023-04-12 mark goto done;
4180 af21bb7e 2023-04-12 mark /* test mode, we don't want any output */
4181 098596c5 2023-04-14 stsp tog_io.cout = fopen("/dev/null", "w+");
4182 098596c5 2023-04-14 stsp if (tog_io.cout == NULL) {
4183 af21bb7e 2023-04-12 mark err = got_error_from_errno("fopen: /dev/null");
4184 af21bb7e 2023-04-12 mark goto done;
4187 098596c5 2023-04-14 stsp tog_io.cin = fopen("/dev/tty", "r+");
4188 098596c5 2023-04-14 stsp if (tog_io.cin == NULL) {
4189 af21bb7e 2023-04-12 mark err = got_error_from_errno("fopen: /dev/tty");
4190 af21bb7e 2023-04-12 mark goto done;
4193 098596c5 2023-04-14 stsp if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4194 af21bb7e 2023-04-12 mark err = got_error_from_errno("fseeko");
4195 af21bb7e 2023-04-12 mark goto done;
4198 7483826e 2023-04-14 mark /* use local TERM so we test in different environments */
4199 098596c5 2023-04-14 stsp if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4200 af21bb7e 2023-04-12 mark err = got_error_msg(GOT_ERR_IO,
4201 af21bb7e 2023-04-12 mark "newterm: failed to initialise curses");
4203 098596c5 2023-04-14 stsp using_mock_io = 1;
4206 098596c5 2023-04-14 stsp tog_io_close();
4207 af21bb7e 2023-04-12 mark return err;
4210 098596c5 2023-04-14 stsp static void
4211 098596c5 2023-04-14 stsp init_curses(void)
4214 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
4215 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
4216 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
4218 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
4219 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
4220 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
4221 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
4222 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
4224 098596c5 2023-04-14 stsp if (using_mock_io) /* In test mode we use a fake terminal */
4227 098596c5 2023-04-14 stsp initscr();
4230 098596c5 2023-04-14 stsp halfdelay(1); /* Fast refresh while initial view is loading. */
4233 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
4234 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
4235 a915003a 2019-02-05 stsp curs_set(0);
4236 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
4237 6d17833f 2019-11-08 stsp start_color();
4238 6d17833f 2019-11-08 stsp use_default_colors();
4244 c2db6724 2019-01-04 stsp static const struct got_error *
4245 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4246 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4248 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4250 f135c941 2020-02-20 stsp if (argc == 0) {
4251 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4252 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4253 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4254 f135c941 2020-02-20 stsp return NULL;
4257 f135c941 2020-02-20 stsp if (worktree) {
4258 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4261 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4263 f135c941 2020-02-20 stsp return err;
4264 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4265 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4266 bfd61697 2020-11-14 stsp p) == -1) {
4267 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4268 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4272 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4274 f135c941 2020-02-20 stsp return err;
4277 f135c941 2020-02-20 stsp static const struct got_error *
4278 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4280 7483826e 2023-04-14 mark const struct got_error *error;
4281 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4282 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4283 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4284 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4285 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
4286 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4287 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4288 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4289 04cc582a 2018-08-01 stsp struct tog_view *view;
4290 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4292 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4293 80ddbec8 2018-04-29 stsp switch (ch) {
4295 b672a97a 2020-01-27 stsp log_branches = 1;
4298 80ddbec8 2018-04-29 stsp start_commit = optarg;
4301 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4302 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4303 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4307 17020d27 2019-03-07 stsp usage_log();
4308 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4312 80ddbec8 2018-04-29 stsp argc -= optind;
4313 80ddbec8 2018-04-29 stsp argv += optind;
4315 f135c941 2020-02-20 stsp if (argc > 1)
4316 f135c941 2020-02-20 stsp usage_log();
4318 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4319 0ae84acc 2022-06-15 tracey if (error != NULL)
4320 0ae84acc 2022-06-15 tracey goto done;
4322 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4323 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4324 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4325 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4326 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4327 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4328 c156c7a4 2020-12-18 stsp goto done;
4329 a1fbf39a 2019-08-11 stsp if (worktree)
4330 6962eb72 2020-02-20 stsp repo_path =
4331 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4333 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4334 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4335 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4336 c156c7a4 2020-12-18 stsp goto done;
4340 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4341 80ddbec8 2018-04-29 stsp if (error != NULL)
4342 ecb28ae0 2018-07-16 stsp goto done;
4344 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4345 f135c941 2020-02-20 stsp repo, worktree);
4346 f135c941 2020-02-20 stsp if (error)
4347 f135c941 2020-02-20 stsp goto done;
4349 098596c5 2023-04-14 stsp init_curses();
4351 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4352 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4353 c02c541e 2019-03-29 stsp if (error)
4354 c02c541e 2019-03-29 stsp goto done;
4356 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4357 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4358 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4359 87670572 2020-12-26 naddy if (error)
4360 87670572 2020-12-26 naddy goto done;
4363 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4364 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4365 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4366 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4367 d8f38dc4 2020-12-05 stsp if (error)
4368 d8f38dc4 2020-12-05 stsp goto done;
4369 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4371 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4372 d8f38dc4 2020-12-05 stsp if (error == NULL)
4373 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4374 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4375 d8f38dc4 2020-12-05 stsp goto done;
4376 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4377 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4378 d8f38dc4 2020-12-05 stsp if (error)
4379 d8f38dc4 2020-12-05 stsp goto done;
4382 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4383 04cc582a 2018-08-01 stsp if (view == NULL) {
4384 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4385 04cc582a 2018-08-01 stsp goto done;
4387 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4388 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4389 ba4f502b 2018-08-04 stsp if (error)
4390 ba4f502b 2018-08-04 stsp goto done;
4391 2fc00ff4 2019-08-31 stsp if (worktree) {
4392 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4393 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4394 2fc00ff4 2019-08-31 stsp worktree = NULL;
4396 098596c5 2023-04-14 stsp error = view_loop(view);
4398 f135c941 2020-02-20 stsp free(in_repo_path);
4399 ecb28ae0 2018-07-16 stsp free(repo_path);
4400 ecb28ae0 2018-07-16 stsp free(cwd);
4401 899d86c2 2018-05-10 stsp free(start_id);
4402 d8f38dc4 2020-12-05 stsp free(label);
4404 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4405 1d0f4054 2021-06-17 stsp if (repo) {
4406 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4407 1d0f4054 2021-06-17 stsp if (error == NULL)
4408 1d0f4054 2021-06-17 stsp error = close_err;
4410 ec142235 2019-03-07 stsp if (worktree)
4411 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4412 0ae84acc 2022-06-15 tracey if (pack_fds) {
4413 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4414 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4415 0ae84acc 2022-06-15 tracey if (error == NULL)
4416 0ae84acc 2022-06-15 tracey error = pack_err;
4418 51a10b52 2020-12-26 stsp tog_free_refs();
4419 80ddbec8 2018-04-29 stsp return error;
4422 4ed7e80c 2018-05-20 stsp __dead static void
4423 9f7d7167 2018-04-29 stsp usage_diff(void)
4426 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4427 827a167b 2022-08-16 stsp "object1 object2\n", getprogname());
4431 6d17833f 2019-11-08 stsp static int
4432 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4433 41605754 2020-11-12 stsp regmatch_t *regmatch)
4435 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4438 336075a4 2022-06-25 op static struct tog_color *
4439 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4441 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4443 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4444 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4445 6d17833f 2019-11-08 stsp return tc;
4448 6d17833f 2019-11-08 stsp return NULL;
4451 4ed7e80c 2018-05-20 stsp static const struct got_error *
4452 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4453 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
4455 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4456 44a87665 2022-06-16 stsp char *exstr = NULL;
4457 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
4458 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
4459 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
4460 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4462 41605754 2020-11-12 stsp *wtotal = 0;
4464 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
4465 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
4467 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
4469 44a87665 2022-06-16 stsp return err;
4471 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
4472 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
4473 44a87665 2022-06-16 stsp if (seg0 == NULL) {
4474 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
4475 44a87665 2022-06-16 stsp goto done;
4477 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
4478 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
4479 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4480 1853e0f4 2022-06-16 stsp goto done;
4482 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
4483 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
4484 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4485 1853e0f4 2022-06-16 stsp goto done;
4488 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
4489 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4490 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4492 1853e0f4 2022-06-16 stsp goto done;
4493 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
4495 1853e0f4 2022-06-16 stsp free(wline);
4496 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4497 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4499 1853e0f4 2022-06-16 stsp goto done;
4500 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4501 1853e0f4 2022-06-16 stsp wlimit -= width;
4502 1853e0f4 2022-06-16 stsp *wtotal += width;
4505 41605754 2020-11-12 stsp if (wlimit > 0) {
4506 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
4507 1853e0f4 2022-06-16 stsp size_t wlen;
4509 1853e0f4 2022-06-16 stsp free(wline);
4510 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4511 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4513 1853e0f4 2022-06-16 stsp goto done;
4514 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
4515 1853e0f4 2022-06-16 stsp while (i < wlen) {
4516 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
4517 1853e0f4 2022-06-16 stsp if (width == -1) {
4518 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
4519 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
4520 1853e0f4 2022-06-16 stsp goto done;
4522 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
4524 61417565 2022-06-20 mark w += width;
4527 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
4528 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
4529 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
4530 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
4531 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
4532 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
4533 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
4537 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
4538 1853e0f4 2022-06-16 stsp free(wline);
4539 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
4540 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
4541 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
4542 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4544 1853e0f4 2022-06-16 stsp goto done;
4545 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4547 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
4548 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4550 1853e0f4 2022-06-16 stsp goto done;
4551 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
4553 1853e0f4 2022-06-16 stsp *wtotal += width2;
4556 145b6838 2022-06-16 stsp free(wline);
4557 44a87665 2022-06-16 stsp free(exstr);
4558 1853e0f4 2022-06-16 stsp free(seg0);
4559 1853e0f4 2022-06-16 stsp free(seg1);
4560 1853e0f4 2022-06-16 stsp free(seg2);
4561 1853e0f4 2022-06-16 stsp return err;
4564 94b80cfa 2022-08-01 mark static int
4565 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
4567 94b80cfa 2022-08-01 mark FILE *f = NULL;
4568 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
4570 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
4571 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
4573 ec2a9698 2022-09-15 mark first = &s->first_displayed_line;
4574 ec2a9698 2022-09-15 mark selected = first;
4575 ec2a9698 2022-09-15 mark eof = &s->eof;
4577 ec2a9698 2022-09-15 mark } else if (view->type == TOG_VIEW_HELP) {
4578 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
4580 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4581 94b80cfa 2022-08-01 mark selected = first;
4582 94b80cfa 2022-08-01 mark eof = &s->eof;
4584 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
4585 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
4587 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4588 94b80cfa 2022-08-01 mark selected = &s->selected_line;
4589 94b80cfa 2022-08-01 mark eof = &s->eof;
4590 94b80cfa 2022-08-01 mark f = s->blame.f;
4594 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
4595 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
4597 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4598 94b80cfa 2022-08-01 mark rewind(f);
4600 94b80cfa 2022-08-01 mark *first = 1;
4601 94b80cfa 2022-08-01 mark *lineno = 0;
4602 94b80cfa 2022-08-01 mark *nprinted = 0;
4606 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
4607 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
4608 94b80cfa 2022-08-01 mark view->gline = 0;
4613 41605754 2020-11-12 stsp static const struct got_error *
4614 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4616 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4617 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4618 61e69b96 2018-05-20 stsp const struct got_error *err;
4619 c7d5c43c 2022-08-04 mark int nprinted = 0;
4620 b304db33 2018-05-20 stsp char *line;
4621 826082fe 2020-12-10 stsp size_t linesize = 0;
4622 826082fe 2020-12-10 stsp ssize_t linelen;
4623 61e69b96 2018-05-20 stsp wchar_t *wline;
4624 e0b650dd 2018-05-20 stsp int width;
4625 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4626 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4627 fe621944 2020-11-10 stsp off_t line_offset;
4629 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1;
4630 c7d5c43c 2022-08-04 mark line_offset = s->lines[s->first_displayed_line - 1].offset;
4631 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4632 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4634 f7d12f7e 2018-08-01 stsp werase(view->window);
4636 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
4637 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
4639 a3404814 2018-09-02 stsp if (header) {
4640 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4641 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
4642 c7d5c43c 2022-08-04 mark s->lineno + s->selected_line;
4644 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4645 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4646 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4648 135a2da0 2020-11-11 stsp free(line);
4650 a3404814 2018-09-02 stsp return err;
4652 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4653 a3404814 2018-09-02 stsp wstandout(view->window);
4654 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4655 e54cc94a 2020-11-11 stsp free(wline);
4656 e54cc94a 2020-11-11 stsp wline = NULL;
4657 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
4658 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
4659 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4660 a3404814 2018-09-02 stsp wstandend(view->window);
4662 a3404814 2018-09-02 stsp if (max_lines <= 1)
4663 a3404814 2018-09-02 stsp return NULL;
4664 a3404814 2018-09-02 stsp max_lines--;
4667 89f1a395 2020-12-01 naddy s->eof = 0;
4668 145b6838 2022-06-16 stsp view->maxx = 0;
4669 826082fe 2020-12-10 stsp line = NULL;
4670 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4671 6a5ff2d4 2022-08-06 mark enum got_diff_line_type linetype;
4672 6a5ff2d4 2022-08-06 mark attr_t attr = 0;
4674 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4675 826082fe 2020-12-10 stsp if (linelen == -1) {
4676 826082fe 2020-12-10 stsp if (feof(s->f)) {
4677 826082fe 2020-12-10 stsp s->eof = 1;
4680 826082fe 2020-12-10 stsp free(line);
4681 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4684 c7d5c43c 2022-08-04 mark if (++s->lineno < s->first_displayed_line)
4686 c7d5c43c 2022-08-04 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4688 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline)
4689 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4691 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4692 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4693 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4694 1853e0f4 2022-06-16 stsp if (err) {
4695 1853e0f4 2022-06-16 stsp free(line);
4696 1853e0f4 2022-06-16 stsp return err;
4698 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4699 1853e0f4 2022-06-16 stsp free(wline);
4700 1853e0f4 2022-06-16 stsp wline = NULL;
4702 6a5ff2d4 2022-08-06 mark linetype = s->lines[s->lineno].type;
4703 6a5ff2d4 2022-08-06 mark if (linetype > GOT_DIFF_LINE_LOGMSG &&
4704 6a5ff2d4 2022-08-06 mark linetype < GOT_DIFF_LINE_CONTEXT)
4705 6a5ff2d4 2022-08-06 mark attr |= COLOR_PAIR(linetype);
4707 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4708 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4709 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4710 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4711 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4712 41605754 2020-11-12 stsp if (err) {
4713 41605754 2020-11-12 stsp free(line);
4714 41605754 2020-11-12 stsp return err;
4718 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4719 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4720 969c159c 2022-06-16 stsp if (err) {
4721 969c159c 2022-06-16 stsp free(line);
4722 969c159c 2022-06-16 stsp return err;
4724 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4725 969c159c 2022-06-16 stsp free(wline);
4726 41605754 2020-11-12 stsp wline = NULL;
4728 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline) {
4729 94b80cfa 2022-08-01 mark /* highlight full gline length */
4730 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4731 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4733 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4734 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4737 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4738 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4739 c7d5c43c 2022-08-04 mark s->first_displayed_line = s->lineno;
4741 826082fe 2020-12-10 stsp free(line);
4742 fe621944 2020-11-10 stsp if (nprinted >= 1)
4743 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4744 89f1a395 2020-12-01 naddy (nprinted - 1);
4746 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4748 9b058f45 2022-06-30 mark view_border(view);
4750 89f1a395 2020-12-01 naddy if (s->eof) {
4751 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4752 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4753 c3e9aa98 2019-05-13 jcs nprinted++;
4756 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4757 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4759 c3e9aa98 2019-05-13 jcs return err;
4762 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4763 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4764 e54cc94a 2020-11-11 stsp free(wline);
4765 e54cc94a 2020-11-11 stsp wline = NULL;
4766 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4769 26ed57b2 2018-05-19 stsp return NULL;
4772 abd2672a 2018-12-23 stsp static char *
4773 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4775 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4776 09867e48 2019-08-13 stsp char *p, *s;
4778 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4779 09867e48 2019-08-13 stsp if (tm == NULL)
4780 09867e48 2019-08-13 stsp return NULL;
4781 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4782 09867e48 2019-08-13 stsp if (s == NULL)
4783 09867e48 2019-08-13 stsp return NULL;
4784 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4786 abd2672a 2018-12-23 stsp *p = '\0';
4790 4ed7e80c 2018-05-20 stsp static const struct got_error *
4791 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4792 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4794 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4796 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4797 fe621944 2020-11-10 stsp if (p == NULL)
4798 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4799 c7d5c43c 2022-08-04 mark *lines = p;
4800 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4801 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4802 fe621944 2020-11-10 stsp (*nlines)++;
4804 fe621944 2020-11-10 stsp return NULL;
4807 fe621944 2020-11-10 stsp static const struct got_error *
4808 1f3405c9 2023-01-17 mark cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4809 1f3405c9 2023-01-17 mark struct got_diff_line *s_lines, size_t s_nlines)
4811 1f3405c9 2023-01-17 mark struct got_diff_line *p;
4812 1f3405c9 2023-01-17 mark char buf[BUFSIZ];
4813 1f3405c9 2023-01-17 mark size_t i, r;
4815 1f3405c9 2023-01-17 mark if (fseeko(src, 0L, SEEK_SET) == -1)
4816 1f3405c9 2023-01-17 mark return got_error_from_errno("fseeko");
4818 1f3405c9 2023-01-17 mark for (;;) {
4819 1f3405c9 2023-01-17 mark r = fread(buf, 1, sizeof(buf), src);
4820 1f3405c9 2023-01-17 mark if (r == 0) {
4821 1f3405c9 2023-01-17 mark if (ferror(src))
4822 1f3405c9 2023-01-17 mark return got_error_from_errno("fread");
4823 1f3405c9 2023-01-17 mark if (feof(src))
4826 1f3405c9 2023-01-17 mark if (fwrite(buf, 1, r, dst) != r)
4827 1f3405c9 2023-01-17 mark return got_ferror(dst, GOT_ERR_IO);
4830 5afebbd1 2023-02-22 mark if (s_nlines == 0 && *d_nlines == 0)
4831 5afebbd1 2023-02-22 mark return NULL;
4834 5afebbd1 2023-02-22 mark * If commit info was in dst, increment line offsets
4835 5afebbd1 2023-02-22 mark * of the appended diff content, but skip s_lines[0]
4836 5afebbd1 2023-02-22 mark * because offset zero is already in *d_lines.
4838 5afebbd1 2023-02-22 mark if (*d_nlines > 0) {
4839 5afebbd1 2023-02-22 mark for (i = 1; i < s_nlines; ++i)
4840 5afebbd1 2023-02-22 mark s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4842 5afebbd1 2023-02-22 mark if (s_nlines > 0) {
4843 5afebbd1 2023-02-22 mark --s_nlines;
4844 5afebbd1 2023-02-22 mark ++s_lines;
4848 1f3405c9 2023-01-17 mark p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4849 1f3405c9 2023-01-17 mark if (p == NULL) {
4850 1f3405c9 2023-01-17 mark /* d_lines is freed in close_diff_view() */
4851 1f3405c9 2023-01-17 mark return got_error_from_errno("reallocarray");
4854 1f3405c9 2023-01-17 mark *d_lines = p;
4856 5afebbd1 2023-02-22 mark memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4857 1f3405c9 2023-01-17 mark *d_nlines += s_nlines;
4859 1f3405c9 2023-01-17 mark return NULL;
4862 1f3405c9 2023-01-17 mark static const struct got_error *
4863 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4864 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4865 5191b70b 2023-01-07 mark struct got_repository *repo, int ignore_ws, int force_text_diff,
4866 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *dsa, FILE *outfile)
4868 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4869 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4870 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4871 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4872 45d799e2 2018-12-23 stsp time_t committer_time;
4873 45d799e2 2018-12-23 stsp const char *author, *committer;
4874 8b473291 2019-02-21 stsp char *refs_str = NULL;
4875 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4876 fe621944 2020-11-10 stsp off_t outoff = 0;
4879 8b473291 2019-02-21 stsp if (refs) {
4880 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4882 8b473291 2019-02-21 stsp return err;
4885 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4887 abd2672a 2018-12-23 stsp return err;
4889 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4890 15a94983 2018-12-23 stsp if (err) {
4891 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4892 15a94983 2018-12-23 stsp goto done;
4895 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4897 fe621944 2020-11-10 stsp goto done;
4899 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4900 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4901 fe621944 2020-11-10 stsp if (n < 0) {
4902 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4903 abd2672a 2018-12-23 stsp goto done;
4905 fe621944 2020-11-10 stsp outoff += n;
4906 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4908 fe621944 2020-11-10 stsp goto done;
4910 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4911 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4912 fe621944 2020-11-10 stsp if (n < 0) {
4913 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4914 abd2672a 2018-12-23 stsp goto done;
4916 fe621944 2020-11-10 stsp outoff += n;
4917 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4919 fe621944 2020-11-10 stsp goto done;
4921 1a99e0b4 2023-01-07 stsp author = got_object_commit_get_author(commit);
4922 1a99e0b4 2023-01-07 stsp committer = got_object_commit_get_committer(commit);
4923 1a99e0b4 2023-01-07 stsp if (strcmp(author, committer) != 0) {
4924 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "via: %s\n", committer);
4925 fe621944 2020-11-10 stsp if (n < 0) {
4926 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4927 fe621944 2020-11-10 stsp goto done;
4929 fe621944 2020-11-10 stsp outoff += n;
4930 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4931 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_AUTHOR);
4933 fe621944 2020-11-10 stsp goto done;
4935 1a99e0b4 2023-01-07 stsp committer_time = got_object_commit_get_committer_time(commit);
4936 1a99e0b4 2023-01-07 stsp datestr = get_datestr(&committer_time, datebuf);
4937 1a99e0b4 2023-01-07 stsp if (datestr) {
4938 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4939 fe621944 2020-11-10 stsp if (n < 0) {
4940 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4941 fe621944 2020-11-10 stsp goto done;
4943 fe621944 2020-11-10 stsp outoff += n;
4944 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4945 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_DATE);
4947 fe621944 2020-11-10 stsp goto done;
4949 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4950 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4951 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4952 9f98ca05 2021-09-24 stsp int pn = 1;
4953 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4954 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4955 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4957 9f98ca05 2021-09-24 stsp goto done;
4958 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4959 9f98ca05 2021-09-24 stsp if (n < 0) {
4960 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4961 9f98ca05 2021-09-24 stsp goto done;
4963 9f98ca05 2021-09-24 stsp outoff += n;
4964 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4965 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
4967 9f98ca05 2021-09-24 stsp goto done;
4968 9f98ca05 2021-09-24 stsp free(id_str);
4969 9f98ca05 2021-09-24 stsp id_str = NULL;
4973 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4975 5943eee2 2019-08-13 stsp goto done;
4976 fe621944 2020-11-10 stsp s = logmsg;
4977 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4978 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4979 fe621944 2020-11-10 stsp if (n < 0) {
4980 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4981 fe621944 2020-11-10 stsp goto done;
4983 fe621944 2020-11-10 stsp outoff += n;
4984 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4985 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
4987 fe621944 2020-11-10 stsp goto done;
4990 1f3405c9 2023-01-17 mark TAILQ_FOREACH(pe, dsa->paths, entry) {
4991 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4992 1f3405c9 2023-01-17 mark int pad = dsa->max_path_len - pe->path_len + 1;
4994 5191b70b 2023-01-07 mark n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4995 1f3405c9 2023-01-17 mark pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4996 1f3405c9 2023-01-17 mark dsa->rm_cols + 1, cp->rm);
4997 fe621944 2020-11-10 stsp if (n < 0) {
4998 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4999 fe621944 2020-11-10 stsp goto done;
5001 fe621944 2020-11-10 stsp outoff += n;
5002 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
5003 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
5005 fe621944 2020-11-10 stsp goto done;
5008 0208f208 2020-05-05 stsp fputc('\n', outfile);
5010 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5012 5191b70b 2023-01-07 mark goto done;
5014 5191b70b 2023-01-07 mark n = fprintf(outfile,
5015 65dedee0 2023-01-15 mark "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5016 1f3405c9 2023-01-17 mark dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5017 1f3405c9 2023-01-17 mark dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5018 5191b70b 2023-01-07 mark if (n < 0) {
5019 5191b70b 2023-01-07 mark err = got_error_from_errno("fprintf");
5020 5191b70b 2023-01-07 mark goto done;
5022 5191b70b 2023-01-07 mark outoff += n;
5023 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5025 5191b70b 2023-01-07 mark goto done;
5027 5191b70b 2023-01-07 mark fputc('\n', outfile);
5029 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5031 abd2672a 2018-12-23 stsp free(id_str);
5032 5943eee2 2019-08-13 stsp free(logmsg);
5033 8b473291 2019-02-21 stsp free(refs_str);
5034 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5035 7510f233 2020-08-09 stsp if (err) {
5036 c7d5c43c 2022-08-04 mark free(*lines);
5037 c7d5c43c 2022-08-04 mark *lines = NULL;
5038 ae6a6978 2020-08-09 stsp *nlines = 0;
5040 fe621944 2020-11-10 stsp return err;
5043 abd2672a 2018-12-23 stsp static const struct got_error *
5044 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
5046 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
5047 1f3405c9 2023-01-17 mark FILE *f = NULL, *tmp_diff_file = NULL;
5048 15a94983 2018-12-23 stsp int obj_type;
5049 1f3405c9 2023-01-17 mark struct got_diff_line *lines = NULL;
5050 1f3405c9 2023-01-17 mark struct got_pathlist_head changed_paths;
5052 1f3405c9 2023-01-17 mark TAILQ_INIT(&changed_paths);
5054 c7d5c43c 2022-08-04 mark free(s->lines);
5055 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
5056 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
5057 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
5058 fe621944 2020-11-10 stsp s->nlines = 0;
5060 511a516b 2018-05-19 stsp f = got_opentemp();
5061 48ae06ee 2018-10-18 stsp if (f == NULL) {
5062 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5063 48ae06ee 2018-10-18 stsp goto done;
5065 1f3405c9 2023-01-17 mark tmp_diff_file = got_opentemp();
5066 1f3405c9 2023-01-17 mark if (tmp_diff_file == NULL) {
5067 1f3405c9 2023-01-17 mark err = got_error_from_errno("got_opentemp");
5068 1f3405c9 2023-01-17 mark goto done;
5070 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
5071 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5072 fb43ecf1 2019-02-11 stsp goto done;
5076 15a94983 2018-12-23 stsp if (s->id1)
5077 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
5079 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
5081 15a94983 2018-12-23 stsp goto done;
5083 15a94983 2018-12-23 stsp switch (obj_type) {
5084 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
5085 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5086 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5087 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
5088 1f3405c9 2023-01-17 mark s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5091 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
5092 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5093 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5094 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
5095 1f3405c9 2023-01-17 mark s->force_text_diff, NULL, s->repo, s->f);
5097 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
5098 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
5099 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
5100 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
5101 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
5102 1f3405c9 2023-01-17 mark size_t nlines = 0;
5103 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg dsa = {
5104 1f3405c9 2023-01-17 mark 0, 0, 0, 0, 0, 0,
5105 1f3405c9 2023-01-17 mark &changed_paths,
5106 1f3405c9 2023-01-17 mark s->ignore_whitespace,
5107 1f3405c9 2023-01-17 mark s->force_text_diff,
5108 1f3405c9 2023-01-17 mark tog_diff_algo
5111 1f3405c9 2023-01-17 mark lines = malloc(sizeof(*lines));
5112 1f3405c9 2023-01-17 mark if (lines == NULL) {
5113 1f3405c9 2023-01-17 mark err = got_error_from_errno("malloc");
5114 1f3405c9 2023-01-17 mark goto done;
5117 1f3405c9 2023-01-17 mark /* build diff first in tmp file then append to commit info */
5118 1f3405c9 2023-01-17 mark err = got_diff_objects_as_commits(&lines, &nlines,
5119 1f3405c9 2023-01-17 mark s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5120 1f3405c9 2023-01-17 mark tog_diff_algo, s->diff_context, s->ignore_whitespace,
5121 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5125 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5127 3ffacbe1 2020-02-02 tracey goto done;
5128 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5129 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
5130 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
5131 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
5132 5191b70b 2023-01-07 mark refs, s->repo, s->ignore_whitespace,
5133 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
5134 f44b1f58 2020-02-02 tracey if (err)
5135 f44b1f58 2020-02-02 tracey goto done;
5136 f44b1f58 2020-02-02 tracey } else {
5137 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
5138 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
5139 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5140 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
5141 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
5142 5191b70b 2023-01-07 mark s->ignore_whitespace,
5143 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
5144 f44b1f58 2020-02-02 tracey if (err)
5145 f44b1f58 2020-02-02 tracey goto done;
5150 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
5152 1f3405c9 2023-01-17 mark err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5153 1f3405c9 2023-01-17 mark lines, nlines);
5157 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5161 1f3405c9 2023-01-17 mark free(lines);
5162 1f3405c9 2023-01-17 mark got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5163 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
5164 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
5165 1f3405c9 2023-01-17 mark if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5166 1f3405c9 2023-01-17 mark err = got_error_from_errno("fclose");
5167 48ae06ee 2018-10-18 stsp return err;
5170 f5215bb9 2019-02-22 stsp static void
5171 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
5173 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
5174 f5215bb9 2019-02-22 stsp update_panels();
5175 f5215bb9 2019-02-22 stsp doupdate();
5178 f44b1f58 2020-02-02 tracey static const struct got_error *
5179 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
5181 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
5183 f44b1f58 2020-02-02 tracey s->matched_line = 0;
5184 f44b1f58 2020-02-02 tracey return NULL;
5187 eaf2c13e 2022-09-17 mark static void
5188 eaf2c13e 2022-09-17 mark search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5189 ec2a9698 2022-09-15 mark size_t *nlines, int **first, int **last, int **match, int **selected)
5191 eaf2c13e 2022-09-17 mark struct tog_diff_view_state *s = &view->state.diff;
5193 eaf2c13e 2022-09-17 mark *f = s->f;
5194 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
5195 eaf2c13e 2022-09-17 mark *line_offsets = NULL;
5196 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
5197 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
5198 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
5199 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
5202 ec2a9698 2022-09-15 mark static const struct got_error *
5203 ec2a9698 2022-09-15 mark search_next_view_match(struct tog_view *view)
5205 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5207 f44b1f58 2020-02-02 tracey int lineno;
5208 cb713507 2022-06-16 stsp char *line = NULL;
5209 826082fe 2020-12-10 stsp size_t linesize = 0;
5210 826082fe 2020-12-10 stsp ssize_t linelen;
5211 ec2a9698 2022-09-15 mark off_t *line_offsets;
5212 ec2a9698 2022-09-15 mark size_t nlines = 0;
5213 ec2a9698 2022-09-15 mark int *first, *last, *match, *selected;
5215 eaf2c13e 2022-09-17 mark if (!view->search_setup)
5216 eaf2c13e 2022-09-17 mark return got_error_msg(GOT_ERR_NOT_IMPL,
5217 eaf2c13e 2022-09-17 mark "view search not supported");
5218 eaf2c13e 2022-09-17 mark view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5219 ec2a9698 2022-09-15 mark &match, &selected);
5221 f44b1f58 2020-02-02 tracey if (!view->searching) {
5222 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5223 f44b1f58 2020-02-02 tracey return NULL;
5226 ec2a9698 2022-09-15 mark if (*match) {
5227 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5228 8c28a2ab 2023-04-02 mark lineno = *first + 1;
5230 8c28a2ab 2023-04-02 mark lineno = *first - 1;
5232 ec2a9698 2022-09-15 mark lineno = *first - 1 + *selected;
5234 f44b1f58 2020-02-02 tracey while (1) {
5235 f44b1f58 2020-02-02 tracey off_t offset;
5237 ec2a9698 2022-09-15 mark if (lineno <= 0 || lineno > nlines) {
5238 ec2a9698 2022-09-15 mark if (*match == 0) {
5239 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5243 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5244 f44b1f58 2020-02-02 tracey lineno = 1;
5246 ec2a9698 2022-09-15 mark lineno = nlines;
5249 ec2a9698 2022-09-15 mark offset = view->type == TOG_VIEW_DIFF ?
5250 ec2a9698 2022-09-15 mark view->state.diff.lines[lineno - 1].offset :
5251 ec2a9698 2022-09-15 mark line_offsets[lineno - 1];
5252 ec2a9698 2022-09-15 mark if (fseeko(f, offset, SEEK_SET) != 0) {
5253 f44b1f58 2020-02-02 tracey free(line);
5254 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5256 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesize, f);
5257 cb713507 2022-06-16 stsp if (linelen != -1) {
5258 cb713507 2022-06-16 stsp char *exstr;
5259 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5262 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5263 cb713507 2022-06-16 stsp &view->regmatch)) {
5264 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5265 ec2a9698 2022-09-15 mark *match = lineno;
5266 cb713507 2022-06-16 stsp free(exstr);
5269 cb713507 2022-06-16 stsp free(exstr);
5271 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5272 f44b1f58 2020-02-02 tracey lineno++;
5274 f44b1f58 2020-02-02 tracey lineno--;
5276 826082fe 2020-12-10 stsp free(line);
5278 ec2a9698 2022-09-15 mark if (*match) {
5279 ec2a9698 2022-09-15 mark *first = *match;
5280 ec2a9698 2022-09-15 mark *selected = 1;
5283 145b6838 2022-06-16 stsp return err;
5286 48ae06ee 2018-10-18 stsp static const struct got_error *
5287 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
5289 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
5290 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5292 b72706c3 2022-06-01 stsp free(s->id1);
5293 b72706c3 2022-06-01 stsp s->id1 = NULL;
5294 b72706c3 2022-06-01 stsp free(s->id2);
5295 b72706c3 2022-06-01 stsp s->id2 = NULL;
5296 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
5297 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5298 b72706c3 2022-06-01 stsp s->f = NULL;
5299 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5300 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5301 b72706c3 2022-06-01 stsp s->f1 = NULL;
5302 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5303 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5304 b72706c3 2022-06-01 stsp s->f2 = NULL;
5305 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5306 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5307 f9d37699 2022-06-28 stsp s->fd1 = -1;
5308 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5309 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5310 f9d37699 2022-06-28 stsp s->fd2 = -1;
5311 c7d5c43c 2022-08-04 mark free(s->lines);
5312 c7d5c43c 2022-08-04 mark s->lines = NULL;
5313 b72706c3 2022-06-01 stsp s->nlines = 0;
5314 b72706c3 2022-06-01 stsp return err;
5317 b72706c3 2022-06-01 stsp static const struct got_error *
5318 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5319 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5320 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5321 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
5323 48ae06ee 2018-10-18 stsp const struct got_error *err;
5324 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5326 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
5327 f9d37699 2022-06-28 stsp s->fd1 = -1;
5328 f9d37699 2022-06-28 stsp s->fd2 = -1;
5330 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5331 2ca2f982 2023-04-14 mark int type1, type2;
5333 2ca2f982 2023-04-14 mark err = got_object_get_type(&type1, repo, id1);
5335 2ca2f982 2023-04-14 mark goto done;
5336 2ca2f982 2023-04-14 mark err = got_object_get_type(&type2, repo, id2);
5338 2ca2f982 2023-04-14 mark goto done;
5340 2ca2f982 2023-04-14 mark if (type1 != type2) {
5341 2ca2f982 2023-04-14 mark err = got_error(GOT_ERR_OBJ_TYPE);
5342 2ca2f982 2023-04-14 mark goto done;
5345 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5346 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5347 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5348 f44b1f58 2020-02-02 tracey s->repo = repo;
5349 f44b1f58 2020-02-02 tracey s->id1 = id1;
5350 f44b1f58 2020-02-02 tracey s->id2 = id2;
5351 3dbaef42 2020-11-24 stsp s->label1 = label1;
5352 3dbaef42 2020-11-24 stsp s->label2 = label2;
5354 15a94983 2018-12-23 stsp if (id1) {
5355 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5356 2ca2f982 2023-04-14 mark if (s->id1 == NULL) {
5357 2ca2f982 2023-04-14 mark err = got_error_from_errno("got_object_id_dup");
5358 2ca2f982 2023-04-14 mark goto done;
5361 5465d566 2020-02-01 tracey s->id1 = NULL;
5363 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5364 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5365 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
5366 b72706c3 2022-06-01 stsp goto done;
5369 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
5370 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
5371 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
5372 a00719e9 2022-06-17 stsp goto done;
5375 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
5376 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
5377 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
5378 b72706c3 2022-06-01 stsp goto done;
5381 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
5382 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
5383 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5384 f9d37699 2022-06-28 stsp goto done;
5387 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
5388 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
5389 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5390 f9d37699 2022-06-28 stsp goto done;
5393 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5394 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5395 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5396 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
5397 5465d566 2020-02-01 tracey s->repo = repo;
5399 098596c5 2023-04-14 stsp if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5402 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
5403 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5404 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5405 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
5406 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5407 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5408 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
5409 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5410 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5411 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
5412 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5413 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5414 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
5415 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5416 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5417 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5418 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5419 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5420 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5421 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5422 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5423 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5424 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
5425 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5426 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
5427 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
5428 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
5429 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
5430 b72706c3 2022-06-01 stsp goto done;
5434 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5435 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
5436 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
5437 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5439 5465d566 2020-02-01 tracey err = create_diff(s);
5441 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5442 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5443 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
5444 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5445 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5446 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_diff_view;
5447 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
5449 2ca2f982 2023-04-14 mark if (err) {
5450 2ca2f982 2023-04-14 mark if (view->close == NULL)
5451 2ca2f982 2023-04-14 mark close_diff_view(view);
5452 2ca2f982 2023-04-14 mark view_close(view);
5454 e5a0f69f 2018-08-18 stsp return err;
5457 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5458 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5460 a3404814 2018-09-02 stsp const struct got_error *err;
5461 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5462 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5463 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5465 a3404814 2018-09-02 stsp if (s->id1) {
5466 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5468 a3404814 2018-09-02 stsp return err;
5469 4924afe1 2022-08-31 mark label1 = s->label1 ? s->label1 : id_str1;
5471 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5473 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5475 a3404814 2018-09-02 stsp return err;
5476 4924afe1 2022-08-31 mark label2 = s->label2 ? s->label2 : id_str2;
5478 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5479 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5480 a3404814 2018-09-02 stsp free(id_str1);
5481 a3404814 2018-09-02 stsp free(id_str2);
5482 a3404814 2018-09-02 stsp return err;
5484 a3404814 2018-09-02 stsp free(id_str1);
5485 a3404814 2018-09-02 stsp free(id_str2);
5487 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5488 267bb3b8 2021-08-01 stsp free(header);
5489 267bb3b8 2021-08-01 stsp return err;
5492 15a087fe 2019-02-21 stsp static const struct got_error *
5493 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5494 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5496 d7a04538 2019-02-21 stsp const struct got_error *err;
5497 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5498 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5499 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5501 15a087fe 2019-02-21 stsp free(s->id2);
5502 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5503 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5504 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5506 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5508 d7a04538 2019-02-21 stsp return err;
5509 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5510 15a087fe 2019-02-21 stsp free(s->id1);
5511 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5512 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5513 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5514 15a087fe 2019-02-21 stsp return NULL;
5517 0cf4efb1 2018-09-29 stsp static const struct got_error *
5518 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
5520 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5522 917d79a7 2022-07-01 stsp view->count = 0;
5523 917d79a7 2022-07-01 stsp wclear(view->window);
5524 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
5525 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
5526 917d79a7 2022-07-01 stsp s->matched_line = 0;
5527 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
5528 917d79a7 2022-07-01 stsp return create_diff(s);
5531 c7d5c43c 2022-08-04 mark static void
5532 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5534 c7d5c43c 2022-08-04 mark int start, i;
5536 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
5538 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5539 c7d5c43c 2022-08-04 mark if (i == 0)
5540 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
5541 c7d5c43c 2022-08-04 mark if (--i == start)
5542 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5545 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5546 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5549 c7d5c43c 2022-08-04 mark static void
5550 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5552 c7d5c43c 2022-08-04 mark int start, i;
5554 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
5556 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5557 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
5559 c7d5c43c 2022-08-04 mark if (++i == start)
5560 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5563 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5564 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5567 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5568 c0f61fa4 2022-07-11 mark int, int, int);
5569 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5570 c0f61fa4 2022-07-11 mark int, int);
5572 917d79a7 2022-07-01 stsp static const struct got_error *
5573 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5575 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5576 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5577 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5578 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5579 826082fe 2020-12-10 stsp char *line = NULL;
5580 826082fe 2020-12-10 stsp size_t linesize = 0;
5581 826082fe 2020-12-10 stsp ssize_t linelen;
5582 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
5584 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
5586 e5a0f69f 2018-08-18 stsp switch (ch) {
5589 145b6838 2022-06-16 stsp case KEY_RIGHT:
5591 145b6838 2022-06-16 stsp case KEY_LEFT:
5593 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
5597 3f6c6614 2023-01-23 mark if (ch == 'a') {
5598 3f6c6614 2023-01-23 mark s->force_text_diff = !s->force_text_diff;
5599 3f6c6614 2023-01-23 mark view->action = s->force_text_diff ?
5600 3f6c6614 2023-01-23 mark "force ASCII text enabled" :
5601 3f6c6614 2023-01-23 mark "force ASCII text disabled";
5603 3f6c6614 2023-01-23 mark else if (ch == 'w') {
5604 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5605 3f6c6614 2023-01-23 mark view->action = s->ignore_whitespace ?
5606 3f6c6614 2023-01-23 mark "ignore whitespace enabled" :
5607 3f6c6614 2023-01-23 mark "ignore whitespace disabled";
5609 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
5612 912a3f79 2021-08-30 j case KEY_HOME:
5613 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5614 640cd7ff 2022-06-22 mark view->count = 0;
5617 912a3f79 2021-08-30 j case KEY_END:
5618 640cd7ff 2022-06-22 mark view->count = 0;
5622 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5626 1e37a5c2 2019-05-12 jcs case KEY_UP:
5627 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5628 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5629 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5631 640cd7ff 2022-06-22 mark view->count = 0;
5633 83cc4199 2022-06-13 stsp case CTRL('u'):
5635 83cc4199 2022-06-13 stsp nscroll /= 2;
5636 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5637 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5638 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5640 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
5641 640cd7ff 2022-06-22 mark view->count = 0;
5645 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
5646 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5649 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5650 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5651 1e37a5c2 2019-05-12 jcs if (!s->eof)
5652 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5654 640cd7ff 2022-06-22 mark view->count = 0;
5656 83cc4199 2022-06-13 stsp case CTRL('d'):
5658 83cc4199 2022-06-13 stsp nscroll /= 2;
5659 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5660 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5661 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5664 640cd7ff 2022-06-22 mark if (s->eof) {
5665 640cd7ff 2022-06-22 mark view->count = 0;
5669 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
5670 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5671 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5672 826082fe 2020-12-10 stsp if (linelen == -1) {
5673 826082fe 2020-12-10 stsp if (feof(s->f)) {
5674 826082fe 2020-12-10 stsp s->eof = 1;
5676 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5680 826082fe 2020-12-10 stsp free(line);
5683 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5686 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5689 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5692 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
5695 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5696 1e37a5c2 2019-05-12 jcs s->diff_context--;
5697 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5698 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5699 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5700 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5701 27829c9e 2020-11-21 stsp s->nlines) {
5702 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5703 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5706 640cd7ff 2022-06-22 mark view->count = 0;
5709 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5710 1e37a5c2 2019-05-12 jcs s->diff_context++;
5711 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5712 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5713 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5715 640cd7ff 2022-06-22 mark view->count = 0;
5721 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
5725 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
5726 640cd7ff 2022-06-22 mark view->count = 0;
5729 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
5731 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
5732 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
5733 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
5735 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
5736 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5739 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5741 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
5744 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
5747 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5748 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
5749 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
5751 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
5752 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
5753 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
5755 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
5756 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5759 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5761 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
5763 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
5764 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
5765 c0f61fa4 2022-07-11 mark bs->selected_line);
5766 c0f61fa4 2022-07-11 mark if (id == NULL)
5769 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5772 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5776 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5777 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5778 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5779 145b6838 2022-06-16 stsp view->x = 0;
5781 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5782 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5785 640cd7ff 2022-06-22 mark view->count = 0;
5789 bcbd79e2 2018-08-19 stsp return err;
5792 4ed7e80c 2018-05-20 stsp static const struct got_error *
5793 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5795 7483826e 2023-04-14 mark const struct got_error *error;
5796 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5797 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5798 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5799 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5800 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5801 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5802 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5803 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5804 3dbaef42 2020-11-24 stsp const char *errstr;
5805 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5806 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5808 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5809 26ed57b2 2018-05-19 stsp switch (ch) {
5811 64453f7e 2020-11-21 stsp force_text_diff = 1;
5814 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5816 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5817 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5818 5a20d08d 2022-02-09 op errstr, errstr);
5820 09b5bff8 2020-02-23 naddy case 'r':
5821 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5822 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5823 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5825 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5828 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5831 17020d27 2019-03-07 stsp usage_diff();
5832 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5836 26ed57b2 2018-05-19 stsp argc -= optind;
5837 26ed57b2 2018-05-19 stsp argv += optind;
5839 26ed57b2 2018-05-19 stsp if (argc == 0) {
5840 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5841 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5842 15a94983 2018-12-23 stsp id_str1 = argv[0];
5843 15a94983 2018-12-23 stsp id_str2 = argv[1];
5845 26ed57b2 2018-05-19 stsp usage_diff();
5847 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5848 0ae84acc 2022-06-15 tracey if (error)
5849 0ae84acc 2022-06-15 tracey goto done;
5851 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5852 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5853 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5854 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5855 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5856 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5857 c156c7a4 2020-12-18 stsp goto done;
5858 a273ac94 2020-02-23 naddy if (worktree)
5859 a273ac94 2020-02-23 naddy repo_path =
5860 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5862 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5863 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5864 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5865 c156c7a4 2020-12-18 stsp goto done;
5869 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5870 eb6600df 2019-01-04 stsp if (error)
5871 eb6600df 2019-01-04 stsp goto done;
5873 098596c5 2023-04-14 stsp init_curses();
5875 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5876 51a10b52 2020-12-26 stsp if (error)
5877 51a10b52 2020-12-26 stsp goto done;
5879 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5880 26ed57b2 2018-05-19 stsp if (error)
5881 26ed57b2 2018-05-19 stsp goto done;
5883 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5884 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5885 26ed57b2 2018-05-19 stsp if (error)
5886 26ed57b2 2018-05-19 stsp goto done;
5888 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5889 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5890 26ed57b2 2018-05-19 stsp if (error)
5891 26ed57b2 2018-05-19 stsp goto done;
5893 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5894 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5895 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5896 ea5e7bb5 2018-08-01 stsp goto done;
5898 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5899 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5900 5dc9f4bc 2018-08-04 stsp if (error)
5901 5dc9f4bc 2018-08-04 stsp goto done;
5902 098596c5 2023-04-14 stsp error = view_loop(view);
5904 3dbaef42 2020-11-24 stsp free(label1);
5905 3dbaef42 2020-11-24 stsp free(label2);
5906 c02c541e 2019-03-29 stsp free(repo_path);
5907 a273ac94 2020-02-23 naddy free(cwd);
5908 1d0f4054 2021-06-17 stsp if (repo) {
5909 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5910 1d0f4054 2021-06-17 stsp if (error == NULL)
5911 1d0f4054 2021-06-17 stsp error = close_err;
5913 a273ac94 2020-02-23 naddy if (worktree)
5914 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5915 0ae84acc 2022-06-15 tracey if (pack_fds) {
5916 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5917 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5918 0ae84acc 2022-06-15 tracey if (error == NULL)
5919 0ae84acc 2022-06-15 tracey error = pack_err;
5921 51a10b52 2020-12-26 stsp tog_free_refs();
5922 26ed57b2 2018-05-19 stsp return error;
5925 4ed7e80c 2018-05-20 stsp __dead static void
5926 9f7d7167 2018-04-29 stsp usage_blame(void)
5929 87411fa9 2022-06-16 stsp fprintf(stderr,
5930 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5931 9f7d7167 2018-04-29 stsp getprogname());
5935 84451b3e 2018-07-10 stsp struct tog_blame_line {
5936 84451b3e 2018-07-10 stsp int annotated;
5937 84451b3e 2018-07-10 stsp struct got_object_id *id;
5940 4ed7e80c 2018-05-20 stsp static const struct got_error *
5941 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5943 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5944 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5945 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5946 84451b3e 2018-07-10 stsp const struct got_error *err;
5947 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5948 826082fe 2020-12-10 stsp char *line = NULL;
5949 826082fe 2020-12-10 stsp size_t linesize = 0;
5950 826082fe 2020-12-10 stsp ssize_t linelen;
5951 84451b3e 2018-07-10 stsp wchar_t *wline;
5952 27a741e5 2019-09-11 stsp int width;
5953 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5954 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5955 ab089a2a 2018-07-12 stsp char *id_str;
5956 11b20872 2019-11-08 stsp struct tog_color *tc;
5958 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5960 ab089a2a 2018-07-12 stsp return err;
5962 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5963 f7d12f7e 2018-08-01 stsp werase(view->window);
5965 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5966 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5967 ab089a2a 2018-07-12 stsp free(id_str);
5968 ab089a2a 2018-07-12 stsp return err;
5971 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5972 ab089a2a 2018-07-12 stsp free(line);
5973 2550e4c3 2018-07-13 stsp line = NULL;
5975 1cae65b4 2019-09-22 stsp return err;
5976 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5977 a3404814 2018-09-02 stsp wstandout(view->window);
5978 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5980 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5981 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5982 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
5983 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
5985 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5986 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5987 a3404814 2018-09-02 stsp wstandend(view->window);
5988 2550e4c3 2018-07-13 stsp free(wline);
5989 2550e4c3 2018-07-13 stsp wline = NULL;
5991 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
5992 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
5994 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5995 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5996 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5997 ab089a2a 2018-07-12 stsp free(id_str);
5998 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6000 ab089a2a 2018-07-12 stsp free(id_str);
6001 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6002 3f60a8ef 2018-07-10 stsp free(line);
6003 2550e4c3 2018-07-13 stsp line = NULL;
6005 3f60a8ef 2018-07-10 stsp return err;
6006 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6007 2550e4c3 2018-07-13 stsp free(wline);
6008 2550e4c3 2018-07-13 stsp wline = NULL;
6009 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6010 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6012 4f7c3e5e 2020-12-01 naddy s->eof = 0;
6013 145b6838 2022-06-16 stsp view->maxx = 0;
6014 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
6015 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
6016 826082fe 2020-12-10 stsp if (linelen == -1) {
6017 826082fe 2020-12-10 stsp if (feof(blame->f)) {
6018 826082fe 2020-12-10 stsp s->eof = 1;
6021 84451b3e 2018-07-10 stsp free(line);
6022 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
6024 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
6026 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
6029 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
6030 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6031 1853e0f4 2022-06-16 stsp if (err) {
6032 1853e0f4 2022-06-16 stsp free(line);
6033 1853e0f4 2022-06-16 stsp return err;
6035 1853e0f4 2022-06-16 stsp free(wline);
6036 1853e0f4 2022-06-16 stsp wline = NULL;
6037 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
6039 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
6040 f7d12f7e 2018-08-01 stsp wstandout(view->window);
6042 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
6043 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
6044 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
6045 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6046 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
6047 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6048 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
6049 8d0fe45a 2019-08-12 stsp char *id_str;
6050 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
6051 87411fa9 2022-06-16 stsp blame_line->id);
6052 8d0fe45a 2019-08-12 stsp if (err) {
6053 8d0fe45a 2019-08-12 stsp free(line);
6054 8d0fe45a 2019-08-12 stsp return err;
6056 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6058 11b20872 2019-11-08 stsp wattr_on(view->window,
6059 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6060 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
6062 11b20872 2019-11-08 stsp wattr_off(view->window,
6063 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6064 8d0fe45a 2019-08-12 stsp free(id_str);
6065 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
6067 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6068 8d0fe45a 2019-08-12 stsp prev_id = NULL;
6071 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6072 ee41ec32 2018-07-10 stsp prev_id = NULL;
6075 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
6076 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6077 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6079 41605754 2020-11-12 stsp if (view->ncols <= 9) {
6080 41605754 2020-11-12 stsp width = 9;
6081 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
6082 4f7c3e5e 2020-12-01 naddy s->matched_line &&
6083 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6084 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
6085 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
6086 41605754 2020-11-12 stsp if (err) {
6087 41605754 2020-11-12 stsp free(line);
6088 41605754 2020-11-12 stsp return err;
6090 41605754 2020-11-12 stsp width += 9;
6093 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
6094 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
6095 4cb9d869 2022-06-16 stsp if (err) {
6096 4cb9d869 2022-06-16 stsp free(line);
6097 4cb9d869 2022-06-16 stsp return err;
6099 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
6100 a75b3e2d 2022-06-16 stsp width += 9;
6101 41605754 2020-11-12 stsp free(wline);
6102 41605754 2020-11-12 stsp wline = NULL;
6105 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
6106 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
6107 84451b3e 2018-07-10 stsp if (++nprinted == 1)
6108 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
6110 826082fe 2020-12-10 stsp free(line);
6111 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
6113 9b058f45 2022-06-30 mark view_border(view);
6115 84451b3e 2018-07-10 stsp return NULL;
6118 84451b3e 2018-07-10 stsp static const struct got_error *
6119 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
6120 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
6122 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
6123 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
6124 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
6125 1a76625f 2018-10-22 stsp int errcode;
6127 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
6128 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
6129 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
6131 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6132 1a76625f 2018-10-22 stsp if (errcode)
6133 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
6135 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
6136 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
6137 d68a0a7d 2018-07-10 stsp goto done;
6140 d68a0a7d 2018-07-10 stsp if (lineno == -1)
6141 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
6143 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
6144 d68a0a7d 2018-07-10 stsp if (line->annotated)
6145 d68a0a7d 2018-07-10 stsp goto done;
6147 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
6148 84451b3e 2018-07-10 stsp if (line->id == NULL) {
6149 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6150 84451b3e 2018-07-10 stsp goto done;
6152 84451b3e 2018-07-10 stsp line->annotated = 1;
6154 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6155 1a76625f 2018-10-22 stsp if (errcode)
6156 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6157 84451b3e 2018-07-10 stsp return err;
6160 84451b3e 2018-07-10 stsp static void *
6161 84451b3e 2018-07-10 stsp blame_thread(void *arg)
6163 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
6164 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
6165 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
6166 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
6167 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
6169 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
6170 e6e73e55 2022-06-30 tracey if (fd1 == -1)
6171 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
6173 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
6174 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
6175 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
6176 e6e73e55 2022-06-30 tracey goto done;
6179 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
6180 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
6181 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
6182 e6e73e55 2022-06-30 tracey goto done;
6184 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
6185 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
6186 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
6187 e6e73e55 2022-06-30 tracey goto done;
6190 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
6192 e6e73e55 2022-06-30 tracey goto done;
6194 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
6195 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
6196 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6197 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
6198 fc06ba56 2019-08-22 stsp err = NULL;
6200 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6201 e6e73e55 2022-06-30 tracey if (errcode) {
6202 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
6203 e6e73e55 2022-06-30 tracey goto done;
6206 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
6207 1d0f4054 2021-06-17 stsp if (err == NULL)
6208 1d0f4054 2021-06-17 stsp err = close_err;
6209 c9beca56 2018-07-22 stsp ta->repo = NULL;
6210 c9beca56 2018-07-22 stsp *ta->complete = 1;
6212 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6213 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
6214 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6217 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6218 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
6219 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6220 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
6221 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
6222 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
6223 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
6224 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
6226 18430de3 2018-07-10 stsp return (void *)err;
6229 245d91c1 2018-07-12 stsp static struct got_object_id *
6230 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6231 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
6233 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
6235 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
6236 8d0fe45a 2019-08-12 stsp return NULL;
6238 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
6239 c0f61fa4 2022-07-11 mark if (!line->annotated)
6240 c0f61fa4 2022-07-11 mark return NULL;
6242 c0f61fa4 2022-07-11 mark return line->id;
6245 c0f61fa4 2022-07-11 mark static struct got_object_id *
6246 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6247 c0f61fa4 2022-07-11 mark int lineno)
6249 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
6251 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
6252 c0f61fa4 2022-07-11 mark return NULL;
6254 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
6255 245d91c1 2018-07-12 stsp if (!line->annotated)
6256 245d91c1 2018-07-12 stsp return NULL;
6258 245d91c1 2018-07-12 stsp return line->id;
6261 b880a918 2018-07-10 stsp static const struct got_error *
6262 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6264 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6267 245d91c1 2018-07-12 stsp if (blame->thread) {
6268 1a76625f 2018-10-22 stsp int errcode;
6269 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6270 1a76625f 2018-10-22 stsp if (errcode)
6271 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6272 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6273 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6274 1a76625f 2018-10-22 stsp if (errcode)
6275 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6276 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6277 1a76625f 2018-10-22 stsp if (errcode)
6278 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6279 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6280 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6281 245d91c1 2018-07-12 stsp err = NULL;
6282 245d91c1 2018-07-12 stsp blame->thread = NULL;
6284 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6285 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6286 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6287 1d0f4054 2021-06-17 stsp if (err == NULL)
6288 1d0f4054 2021-06-17 stsp err = close_err;
6289 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6291 245d91c1 2018-07-12 stsp if (blame->f) {
6292 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6293 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6294 245d91c1 2018-07-12 stsp blame->f = NULL;
6296 57670559 2018-12-23 stsp if (blame->lines) {
6297 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6298 57670559 2018-12-23 stsp free(blame->lines[i].id);
6299 57670559 2018-12-23 stsp free(blame->lines);
6300 57670559 2018-12-23 stsp blame->lines = NULL;
6302 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6303 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6304 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
6305 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6306 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
6307 0ae84acc 2022-06-15 tracey if (err == NULL)
6308 0ae84acc 2022-06-15 tracey err = pack_err;
6309 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
6311 245d91c1 2018-07-12 stsp return err;
6314 245d91c1 2018-07-12 stsp static const struct got_error *
6315 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6317 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6318 fc06ba56 2019-08-22 stsp int *done = arg;
6319 fc06ba56 2019-08-22 stsp int errcode;
6321 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6322 fc06ba56 2019-08-22 stsp if (errcode)
6323 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6324 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6326 fc06ba56 2019-08-22 stsp if (*done)
6327 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6329 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6330 fc06ba56 2019-08-22 stsp if (errcode)
6331 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6332 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6334 fc06ba56 2019-08-22 stsp return err;
6337 fc06ba56 2019-08-22 stsp static const struct got_error *
6338 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6340 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6341 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6342 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6343 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6344 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6345 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6346 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6347 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
6348 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6350 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
6351 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
6353 15a94983 2018-12-23 stsp return err;
6355 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
6356 eb81bc23 2022-06-28 tracey if (fd == -1) {
6357 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
6358 eb81bc23 2022-06-28 tracey goto done;
6361 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6363 a44927cc 2022-04-07 stsp goto done;
6365 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6367 84451b3e 2018-07-10 stsp goto done;
6369 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6370 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6371 84451b3e 2018-07-10 stsp goto done;
6374 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6376 a70480e0 2018-06-23 stsp goto done;
6377 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6378 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6379 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6380 84451b3e 2018-07-10 stsp goto done;
6382 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6383 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6385 1fddf795 2021-01-20 stsp goto done;
6386 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6387 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6388 84451b3e 2018-07-10 stsp goto done;
6391 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6392 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6393 b02560ec 2019-08-19 stsp blame->nlines--;
6395 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6396 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6397 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6398 84451b3e 2018-07-10 stsp goto done;
6401 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
6403 bd24772e 2018-07-11 stsp goto done;
6404 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6405 0ae84acc 2022-06-15 tracey pack_fds);
6406 0ae84acc 2022-06-15 tracey if (err)
6407 0ae84acc 2022-06-15 tracey goto done;
6409 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
6410 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6411 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6412 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6413 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6414 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6415 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6416 245d91c1 2018-07-12 stsp goto done;
6418 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6420 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6421 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6422 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6423 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6424 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6425 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6426 a5388363 2020-12-01 naddy s->blame_complete = 0;
6428 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6429 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6430 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6431 f5a09613 2020-12-13 naddy s->selected_line = 1;
6433 67b5eae1 2021-12-27 naddy s->matched_line = 0;
6436 a44927cc 2022-04-07 stsp if (commit)
6437 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6438 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
6439 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
6441 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6442 27d434c2 2018-09-15 stsp free(obj_id);
6444 245d91c1 2018-07-12 stsp stop_blame(blame);
6445 245d91c1 2018-07-12 stsp return err;
6448 245d91c1 2018-07-12 stsp static const struct got_error *
6449 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6450 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6452 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6453 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6455 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6457 c4843652 2019-08-12 stsp s->path = strdup(path);
6458 c4843652 2019-08-12 stsp if (s->path == NULL)
6459 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6461 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6462 c4843652 2019-08-12 stsp if (err) {
6463 c4843652 2019-08-12 stsp free(s->path);
6464 7cbe629d 2018-08-04 stsp return err;
6467 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6468 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6469 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6470 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6471 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6472 fb2756b9 2018-08-04 stsp s->repo = repo;
6473 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6474 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6476 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6477 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6478 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6479 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6481 11b20872 2019-11-08 stsp return err;
6484 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6485 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6486 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
6487 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6488 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6489 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_blame_view;
6490 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
6492 a5388363 2020-12-01 naddy return run_blame(view);
6495 e5a0f69f 2018-08-18 stsp static const struct got_error *
6496 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6498 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6499 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6501 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6502 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6504 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6505 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6506 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6507 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6508 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6511 e5a0f69f 2018-08-18 stsp free(s->path);
6512 11b20872 2019-11-08 stsp free_colors(&s->colors);
6513 e5a0f69f 2018-08-18 stsp return err;
6516 7cbe629d 2018-08-04 stsp static const struct got_error *
6517 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6519 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6521 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6522 6c4c42e0 2019-06-24 stsp return NULL;
6525 eaf2c13e 2022-09-17 mark static void
6526 eaf2c13e 2022-09-17 mark search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6527 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
6529 eaf2c13e 2022-09-17 mark struct tog_blame_view_state *s = &view->state.blame;
6531 eaf2c13e 2022-09-17 mark *f = s->blame.f;
6532 eaf2c13e 2022-09-17 mark *nlines = s->blame.nlines;
6533 eaf2c13e 2022-09-17 mark *line_offsets = s->blame.line_offsets;
6534 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
6535 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
6536 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
6537 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
6540 6c4c42e0 2019-06-24 stsp static const struct got_error *
6541 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6543 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6544 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6545 2b380cc8 2018-10-24 stsp int errcode;
6547 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
6548 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6549 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6550 2b380cc8 2018-10-24 stsp if (errcode)
6551 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6553 098596c5 2023-04-14 stsp if (!using_mock_io)
6554 098596c5 2023-04-14 stsp halfdelay(1); /* fast refresh while annotating */
6557 098596c5 2023-04-14 stsp if (s->blame_complete && !using_mock_io)
6558 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6560 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6562 9b058f45 2022-06-30 mark view_border(view);
6563 e5a0f69f 2018-08-18 stsp return err;
6566 e5a0f69f 2018-08-18 stsp static const struct got_error *
6567 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6568 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
6570 05f04cdf 2022-07-20 mark struct tog_view *log_view;
6571 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
6573 05f04cdf 2022-07-20 mark *new_view = NULL;
6575 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6576 05f04cdf 2022-07-20 mark if (log_view == NULL)
6577 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
6579 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6581 05f04cdf 2022-07-20 mark view_close(log_view);
6583 05f04cdf 2022-07-20 mark *new_view = log_view;
6585 05f04cdf 2022-07-20 mark return err;
6588 05f04cdf 2022-07-20 mark static const struct got_error *
6589 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6591 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6592 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
6593 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6594 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
6596 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
6597 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6598 9b058f45 2022-06-30 mark --eos; /* border */
6600 e5a0f69f 2018-08-18 stsp switch (ch) {
6603 145b6838 2022-06-16 stsp case KEY_RIGHT:
6605 145b6838 2022-06-16 stsp case KEY_LEFT:
6607 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
6610 1e37a5c2 2019-05-12 jcs s->done = 1;
6612 4deef56f 2021-09-02 naddy case 'g':
6613 4deef56f 2021-09-02 naddy case KEY_HOME:
6614 4deef56f 2021-09-02 naddy s->selected_line = 1;
6615 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6616 640cd7ff 2022-06-22 mark view->count = 0;
6618 4deef56f 2021-09-02 naddy case 'G':
6619 4deef56f 2021-09-02 naddy case KEY_END:
6620 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
6621 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6622 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6624 9b058f45 2022-06-30 mark s->selected_line = eos;
6625 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
6627 640cd7ff 2022-06-22 mark view->count = 0;
6630 1e37a5c2 2019-05-12 jcs case KEY_UP:
6631 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6632 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6633 1e37a5c2 2019-05-12 jcs s->selected_line--;
6634 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6635 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6636 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6638 640cd7ff 2022-06-22 mark view->count = 0;
6640 83cc4199 2022-06-13 stsp case CTRL('u'):
6642 83cc4199 2022-06-13 stsp nscroll /= 2;
6643 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6644 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6645 ea025d1d 2020-02-22 naddy case CTRL('b'):
6647 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6648 640cd7ff 2022-06-22 mark if (view->count > 1)
6649 640cd7ff 2022-06-22 mark nscroll += nscroll;
6650 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
6651 640cd7ff 2022-06-22 mark view->count = 0;
6654 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
6655 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
6657 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6660 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6661 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6662 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
6663 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6664 1e37a5c2 2019-05-12 jcs s->selected_line++;
6665 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6666 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6668 640cd7ff 2022-06-22 mark view->count = 0;
6671 1e37a5c2 2019-05-12 jcs case 'p': {
6672 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6674 640cd7ff 2022-06-22 mark view->count = 0;
6675 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6676 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6677 1e37a5c2 2019-05-12 jcs if (id == NULL)
6679 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6680 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
6681 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6682 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6683 1e37a5c2 2019-05-12 jcs int obj_type;
6684 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6685 1e37a5c2 2019-05-12 jcs s->repo, id);
6688 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6689 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6690 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6691 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6694 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6695 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
6696 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
6699 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6700 a44927cc 2022-04-07 stsp pcommit, s->path);
6701 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6702 e5a0f69f 2018-08-18 stsp if (err) {
6703 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6704 1e37a5c2 2019-05-12 jcs err = NULL;
6705 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6708 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6710 1e37a5c2 2019-05-12 jcs free(blob_id);
6711 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6712 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6713 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6716 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6717 d7b5a0e8 2022-04-20 stsp &pid->id);
6718 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6720 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6721 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6723 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6728 1e37a5c2 2019-05-12 jcs s->done = 1;
6729 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6730 1e37a5c2 2019-05-12 jcs s->done = 0;
6731 1e37a5c2 2019-05-12 jcs if (thread_err)
6733 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6734 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6735 a5388363 2020-12-01 naddy err = run_blame(view);
6740 61417565 2022-06-20 mark case 'C': {
6741 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6743 640cd7ff 2022-06-22 mark view->count = 0;
6744 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6745 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6747 1e37a5c2 2019-05-12 jcs s->done = 1;
6748 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6749 1e37a5c2 2019-05-12 jcs s->done = 0;
6750 1e37a5c2 2019-05-12 jcs if (thread_err)
6752 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6753 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6754 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6755 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6756 a5388363 2020-12-01 naddy err = run_blame(view);
6762 05f04cdf 2022-07-20 mark view->count = 0;
6763 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6764 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6765 136e2bd4 2022-07-23 mark if (s->id_to_log)
6766 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6768 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6769 1e37a5c2 2019-05-12 jcs case '\r': {
6770 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6771 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6772 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6774 640cd7ff 2022-06-22 mark view->count = 0;
6775 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6776 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6777 1e37a5c2 2019-05-12 jcs if (id == NULL)
6779 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6782 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6783 c0f61fa4 2022-07-11 mark if (*new_view) {
6784 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6785 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6788 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6790 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6791 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6793 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6794 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6795 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6796 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6797 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6801 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6802 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6803 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6805 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6808 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6809 c0f61fa4 2022-07-11 mark s->selected_line;
6810 c0f61fa4 2022-07-11 mark if (*new_view)
6811 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6812 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6813 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6814 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6819 e78dc838 2020-12-04 stsp view->focussed = 0;
6820 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6821 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6822 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6823 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6824 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6825 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6828 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6831 e78dc838 2020-12-04 stsp view->focus_child = 1;
6833 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6838 83cc4199 2022-06-13 stsp case CTRL('d'):
6840 83cc4199 2022-06-13 stsp nscroll /= 2;
6841 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6842 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6843 ea025d1d 2020-02-22 naddy case CTRL('f'):
6846 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6847 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6848 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6849 640cd7ff 2022-06-22 mark view->count = 0;
6852 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6853 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6854 83cc4199 2022-06-13 stsp s->selected_line +=
6855 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6856 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6858 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6859 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6861 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6862 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6864 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6865 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6866 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6867 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6871 640cd7ff 2022-06-22 mark view->count = 0;
6874 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6877 917d79a7 2022-07-01 stsp static const struct got_error *
6878 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6880 917d79a7 2022-07-01 stsp const struct got_error *err;
6881 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6883 917d79a7 2022-07-01 stsp view->count = 0;
6884 917d79a7 2022-07-01 stsp s->done = 1;
6885 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6886 917d79a7 2022-07-01 stsp s->done = 0;
6888 917d79a7 2022-07-01 stsp return err;
6889 917d79a7 2022-07-01 stsp return run_blame(view);
6892 a70480e0 2018-06-23 stsp static const struct got_error *
6893 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6895 7483826e 2023-04-14 mark const struct got_error *error;
6896 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6897 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6898 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6899 0587e10c 2020-07-23 stsp char *link_target = NULL;
6900 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6901 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6902 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6904 2ca2f982 2023-04-14 mark struct tog_view *view = NULL;
6905 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6907 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6908 a70480e0 2018-06-23 stsp switch (ch) {
6910 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6913 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6914 69069811 2018-08-02 stsp if (repo_path == NULL)
6915 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6919 17020d27 2019-03-07 stsp usage_blame();
6920 a70480e0 2018-06-23 stsp /* NOTREACHED */
6924 a70480e0 2018-06-23 stsp argc -= optind;
6925 a70480e0 2018-06-23 stsp argv += optind;
6927 f135c941 2020-02-20 stsp if (argc != 1)
6928 a70480e0 2018-06-23 stsp usage_blame();
6930 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6931 0ae84acc 2022-06-15 tracey if (error != NULL)
6932 0ae84acc 2022-06-15 tracey goto done;
6934 69069811 2018-08-02 stsp if (repo_path == NULL) {
6935 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6936 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6937 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6938 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6939 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6940 c156c7a4 2020-12-18 stsp goto done;
6941 f135c941 2020-02-20 stsp if (worktree)
6942 eb41ed75 2019-02-05 stsp repo_path =
6943 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6945 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6946 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6947 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6948 c156c7a4 2020-12-18 stsp goto done;
6952 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6953 c02c541e 2019-03-29 stsp if (error != NULL)
6954 8e94dd5b 2019-01-04 stsp goto done;
6956 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6957 f135c941 2020-02-20 stsp worktree);
6958 c02c541e 2019-03-29 stsp if (error)
6959 92205607 2019-01-04 stsp goto done;
6961 098596c5 2023-04-14 stsp init_curses();
6963 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6964 51a10b52 2020-12-26 stsp if (error)
6965 51a10b52 2020-12-26 stsp goto done;
6967 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6968 eb41ed75 2019-02-05 stsp if (error)
6969 69069811 2018-08-02 stsp goto done;
6971 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6972 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6973 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6974 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6975 a70480e0 2018-06-23 stsp if (error != NULL)
6976 66b4983c 2018-06-23 stsp goto done;
6977 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6978 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6980 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6981 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6983 a19e88aa 2018-06-23 stsp if (error != NULL)
6984 8b473291 2019-02-21 stsp goto done;
6986 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6987 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6988 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6989 e1cd8fed 2018-08-01 stsp goto done;
6992 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6993 a44927cc 2022-04-07 stsp if (error)
6994 a44927cc 2022-04-07 stsp goto done;
6996 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6997 a44927cc 2022-04-07 stsp commit, repo);
6998 7cbe629d 2018-08-04 stsp if (error)
6999 7cbe629d 2018-08-04 stsp goto done;
7001 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
7002 78756c87 2020-11-24 stsp commit_id, repo);
7003 0587e10c 2020-07-23 stsp if (error)
7004 0587e10c 2020-07-23 stsp goto done;
7005 12314ad4 2019-08-31 stsp if (worktree) {
7006 12314ad4 2019-08-31 stsp /* Release work tree lock. */
7007 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
7008 12314ad4 2019-08-31 stsp worktree = NULL;
7010 098596c5 2023-04-14 stsp error = view_loop(view);
7012 69069811 2018-08-02 stsp free(repo_path);
7013 f135c941 2020-02-20 stsp free(in_repo_path);
7014 0587e10c 2020-07-23 stsp free(link_target);
7015 69069811 2018-08-02 stsp free(cwd);
7016 a70480e0 2018-06-23 stsp free(commit_id);
7017 2ca2f982 2023-04-14 mark if (error != NULL && view != NULL) {
7018 2ca2f982 2023-04-14 mark if (view->close == NULL)
7019 2ca2f982 2023-04-14 mark close_blame_view(view);
7020 2ca2f982 2023-04-14 mark view_close(view);
7022 a44927cc 2022-04-07 stsp if (commit)
7023 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7024 eb41ed75 2019-02-05 stsp if (worktree)
7025 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
7026 1d0f4054 2021-06-17 stsp if (repo) {
7027 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7028 1d0f4054 2021-06-17 stsp if (error == NULL)
7029 1d0f4054 2021-06-17 stsp error = close_err;
7031 0ae84acc 2022-06-15 tracey if (pack_fds) {
7032 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7033 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7034 0ae84acc 2022-06-15 tracey if (error == NULL)
7035 0ae84acc 2022-06-15 tracey error = pack_err;
7037 51a10b52 2020-12-26 stsp tog_free_refs();
7038 a70480e0 2018-06-23 stsp return error;
7041 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7042 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
7044 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
7045 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7046 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
7047 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
7048 0c6ad1bc 2022-09-09 mark char *index = NULL;
7049 f26dddb7 2019-11-08 stsp struct tog_color *tc;
7050 d627976f 2023-02-01 mark int width, n, nentries, scrollx, i = 1;
7051 d86d3b18 2020-12-01 naddy int limit = view->nlines;
7053 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
7054 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7055 9b058f45 2022-06-30 mark --limit; /* border */
7057 f7d12f7e 2018-08-01 stsp werase(view->window);
7059 ffd1d5e5 2018-06-23 stsp if (limit == 0)
7060 ffd1d5e5 2018-06-23 stsp return NULL;
7062 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7065 ffd1d5e5 2018-06-23 stsp return err;
7066 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7067 a3404814 2018-09-02 stsp wstandout(view->window);
7068 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7070 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7071 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7072 0c6ad1bc 2022-09-09 mark free(wline);
7073 0c6ad1bc 2022-09-09 mark wline = NULL;
7074 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
7075 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
7077 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7078 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7079 a3404814 2018-09-02 stsp wstandend(view->window);
7080 0c6ad1bc 2022-09-09 mark if (--limit <= 0)
7081 0c6ad1bc 2022-09-09 mark return NULL;
7083 df68a56b 2022-08-12 mark i += s->selected;
7084 df68a56b 2022-08-12 mark if (s->first_displayed_entry) {
7085 df68a56b 2022-08-12 mark i += got_tree_entry_get_index(s->first_displayed_entry);
7086 df68a56b 2022-08-12 mark if (s->tree != s->root)
7087 df68a56b 2022-08-12 mark ++i; /* account for ".." entry */
7089 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
7090 0c6ad1bc 2022-09-09 mark if (asprintf(&index, "[%d/%d] %s",
7091 0c6ad1bc 2022-09-09 mark i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7092 0c6ad1bc 2022-09-09 mark return got_error_from_errno("asprintf");
7093 0c6ad1bc 2022-09-09 mark err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7094 0c6ad1bc 2022-09-09 mark free(index);
7096 ce52c690 2018-06-23 stsp return err;
7097 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7098 2550e4c3 2018-07-13 stsp free(wline);
7099 2550e4c3 2018-07-13 stsp wline = NULL;
7100 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
7101 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7102 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7103 ffd1d5e5 2018-06-23 stsp return NULL;
7104 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7105 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
7106 a1eca9bb 2018-06-23 stsp return NULL;
7108 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
7109 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
7110 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
7111 0cf4efb1 2018-09-29 stsp if (view->focussed)
7112 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7113 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
7115 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
7116 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
7117 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7118 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7119 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7120 ffd1d5e5 2018-06-23 stsp return NULL;
7124 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
7127 d627976f 2023-02-01 mark view->maxx = 0;
7128 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7129 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
7130 848d6979 2019-08-12 stsp const char *modestr = "";
7131 56e0773d 2019-11-28 stsp mode_t mode;
7133 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
7134 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
7136 d86d3b18 2020-12-01 naddy if (s->show_ids) {
7137 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
7138 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
7140 638f9024 2019-05-13 stsp return got_error_from_errno(
7141 230a42bd 2019-05-11 jcs "got_object_id_str");
7143 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
7144 63c5ca5d 2019-08-24 stsp modestr = "$";
7145 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
7148 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
7149 d86d3b18 2020-12-01 naddy te, s->repo);
7150 0d6c6ee3 2020-05-20 stsp if (err) {
7151 0d6c6ee3 2020-05-20 stsp free(id_str);
7152 0d6c6ee3 2020-05-20 stsp return err;
7154 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
7155 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
7156 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
7158 848d6979 2019-08-12 stsp modestr = "@";
7160 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
7161 848d6979 2019-08-12 stsp modestr = "/";
7162 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
7163 848d6979 2019-08-12 stsp modestr = "*";
7164 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7165 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
7166 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
7167 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
7168 1d13200f 2018-07-12 stsp free(id_str);
7169 0d6c6ee3 2020-05-20 stsp free(link_target);
7170 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
7172 1d13200f 2018-07-12 stsp free(id_str);
7173 0d6c6ee3 2020-05-20 stsp free(link_target);
7175 d627976f 2023-02-01 mark /* use full line width to determine view->maxx */
7176 d627976f 2023-02-01 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7177 ffd1d5e5 2018-06-23 stsp if (err) {
7178 ffd1d5e5 2018-06-23 stsp free(line);
7181 d627976f 2023-02-01 mark view->maxx = MAX(view->maxx, width);
7182 d627976f 2023-02-01 mark free(wline);
7183 d627976f 2023-02-01 mark wline = NULL;
7185 d627976f 2023-02-01 mark err = format_line(&wline, &width, &scrollx, line, view->x,
7186 d627976f 2023-02-01 mark view->ncols, 0, 0);
7187 d627976f 2023-02-01 mark if (err) {
7188 d627976f 2023-02-01 mark free(line);
7191 d86d3b18 2020-12-01 naddy if (n == s->selected) {
7192 0cf4efb1 2018-09-29 stsp if (view->focussed)
7193 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7194 d86d3b18 2020-12-01 naddy s->selected_entry = te;
7196 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
7198 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
7199 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7200 d627976f 2023-02-01 mark waddwstr(view->window, &wline[scrollx]);
7202 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
7203 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7204 b637f33f 2023-02-01 mark if (width < view->ncols)
7205 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7206 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
7207 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7208 ffd1d5e5 2018-06-23 stsp free(line);
7209 2550e4c3 2018-07-13 stsp free(wline);
7210 2550e4c3 2018-07-13 stsp wline = NULL;
7212 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7213 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
7214 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7218 ffd1d5e5 2018-06-23 stsp return err;
7221 ffd1d5e5 2018-06-23 stsp static void
7222 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7224 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7225 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
7226 fa86c4bf 2020-11-29 stsp int i = 0;
7228 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
7231 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7232 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
7233 fa86c4bf 2020-11-29 stsp if (te == NULL) {
7234 fa86c4bf 2020-11-29 stsp if (!isroot)
7235 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
7238 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
7239 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
7243 9b058f45 2022-06-30 mark static const struct got_error *
7244 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
7246 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
7247 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
7248 ffd1d5e5 2018-06-23 stsp int n = 0;
7250 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7251 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
7252 694d3271 2020-12-01 naddy s->first_displayed_entry);
7254 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
7256 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7257 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7258 94b80cfa 2022-08-01 mark if (last) {
7259 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7260 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
7262 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7263 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7264 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
7268 9b058f45 2022-06-30 mark return NULL;
7271 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7272 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7273 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7275 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7276 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7277 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7279 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7280 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7281 56e0773d 2019-11-28 stsp + 1 /* slash */;
7283 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7285 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7286 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7287 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7289 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7290 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7291 d9765a41 2018-06-23 stsp while (pt) {
7292 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7293 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7294 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7295 cb2ebc8a 2018-06-23 stsp goto done;
7297 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7298 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7299 cb2ebc8a 2018-06-23 stsp goto done;
7301 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7304 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7305 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7306 ce52c690 2018-06-23 stsp goto done;
7310 ce52c690 2018-06-23 stsp if (err) {
7311 ce52c690 2018-06-23 stsp free(*path);
7312 ce52c690 2018-06-23 stsp *path = NULL;
7314 ce52c690 2018-06-23 stsp return err;
7317 ce52c690 2018-06-23 stsp static const struct got_error *
7318 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7319 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7320 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7322 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7323 ce52c690 2018-06-23 stsp char *path;
7324 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7326 a0de39f3 2019-08-09 stsp *new_view = NULL;
7328 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7330 ce52c690 2018-06-23 stsp return err;
7332 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7333 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7334 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7335 83ce39e3 2019-08-12 stsp goto done;
7338 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7339 e5a0f69f 2018-08-18 stsp if (err) {
7340 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7341 fc06ba56 2019-08-22 stsp err = NULL;
7342 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7344 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7346 83ce39e3 2019-08-12 stsp free(path);
7347 69efd4c4 2018-07-18 stsp return err;
7350 69efd4c4 2018-07-18 stsp static const struct got_error *
7351 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7352 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7354 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7355 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7356 69efd4c4 2018-07-18 stsp char *path;
7358 a0de39f3 2019-08-09 stsp *new_view = NULL;
7360 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7361 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7362 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7364 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7366 69efd4c4 2018-07-18 stsp return err;
7368 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7371 e5a0f69f 2018-08-18 stsp view_close(log_view);
7373 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7374 cb2ebc8a 2018-06-23 stsp free(path);
7375 cb2ebc8a 2018-06-23 stsp return err;
7378 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7379 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7380 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7382 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7383 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7384 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7385 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7387 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7388 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7390 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7391 2ca2f982 2023-04-14 mark if (s->commit_id == NULL) {
7392 2ca2f982 2023-04-14 mark err = got_error_from_errno("got_object_id_dup");
7393 2ca2f982 2023-04-14 mark goto done;
7396 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7398 bc573f3b 2021-07-10 stsp goto done;
7401 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7402 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7403 bc573f3b 2021-07-10 stsp * closed on demand.
7405 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7406 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7408 bc573f3b 2021-07-10 stsp goto done;
7409 bc573f3b 2021-07-10 stsp s->tree = s->root;
7411 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7412 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7413 ffd1d5e5 2018-06-23 stsp goto done;
7415 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7416 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7417 ffd1d5e5 2018-06-23 stsp goto done;
7420 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7421 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7422 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7423 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7424 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7425 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7426 9cd7cbd1 2020-12-07 stsp goto done;
7429 fb2756b9 2018-08-04 stsp s->repo = repo;
7431 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7432 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7433 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7434 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7436 c0b01bdb 2019-11-08 stsp goto done;
7437 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7438 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7440 c0b01bdb 2019-11-08 stsp goto done;
7441 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7442 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7443 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7445 c0b01bdb 2019-11-08 stsp goto done;
7447 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7448 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7449 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7451 11b20872 2019-11-08 stsp goto done;
7453 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7454 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7456 c0b01bdb 2019-11-08 stsp goto done;
7459 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7460 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7461 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7462 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7463 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7465 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7466 bc573f3b 2021-07-10 stsp if (commit)
7467 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7468 2ca2f982 2023-04-14 mark if (err) {
7469 2ca2f982 2023-04-14 mark if (view->close == NULL)
7470 2ca2f982 2023-04-14 mark close_tree_view(view);
7471 2ca2f982 2023-04-14 mark view_close(view);
7473 ad80ab7b 2018-08-04 stsp return err;
7476 e5a0f69f 2018-08-18 stsp static const struct got_error *
7477 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7479 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7481 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7482 fb2756b9 2018-08-04 stsp free(s->tree_label);
7483 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7484 6484ec90 2018-09-29 stsp free(s->commit_id);
7485 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7486 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7487 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7488 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7489 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7490 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7491 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7492 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7493 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7494 ad80ab7b 2018-08-04 stsp free(parent);
7497 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7498 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7499 bc573f3b 2021-07-10 stsp if (s->root)
7500 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7501 7c32bd05 2019-06-22 stsp return NULL;
7504 7c32bd05 2019-06-22 stsp static const struct got_error *
7505 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7507 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7509 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7510 7c32bd05 2019-06-22 stsp return NULL;
7513 7c32bd05 2019-06-22 stsp static int
7514 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7516 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7518 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7522 7c32bd05 2019-06-22 stsp static const struct got_error *
7523 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7525 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7526 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7528 7c32bd05 2019-06-22 stsp if (!view->searching) {
7529 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7530 7c32bd05 2019-06-22 stsp return NULL;
7533 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7534 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7535 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7536 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7537 56e0773d 2019-11-28 stsp s->selected_entry);
7539 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7541 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7542 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7544 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7545 56e0773d 2019-11-28 stsp s->selected_entry);
7548 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7549 487cd7d2 2021-12-17 stsp te = s->selected_entry;
7550 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7551 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7553 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7556 7c32bd05 2019-06-22 stsp while (1) {
7557 56e0773d 2019-11-28 stsp if (te == NULL) {
7558 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7559 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7560 ac66afb8 2019-06-24 stsp return NULL;
7562 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7563 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7565 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7568 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7569 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7570 56e0773d 2019-11-28 stsp s->matched_entry = te;
7574 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7575 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7577 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7580 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7581 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7582 7c32bd05 2019-06-22 stsp s->selected = 0;
7585 e5a0f69f 2018-08-18 stsp return NULL;
7588 ad80ab7b 2018-08-04 stsp static const struct got_error *
7589 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7591 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7592 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7593 e5a0f69f 2018-08-18 stsp char *parent_path;
7595 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7597 e5a0f69f 2018-08-18 stsp return err;
7599 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7600 e5a0f69f 2018-08-18 stsp free(parent_path);
7602 9b058f45 2022-06-30 mark view_border(view);
7603 e5a0f69f 2018-08-18 stsp return err;
7606 94b80cfa 2022-08-01 mark static const struct got_error *
7607 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
7609 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7610 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
7611 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
7612 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
7613 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
7614 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
7616 94b80cfa 2022-08-01 mark g = view->gline;
7617 94b80cfa 2022-08-01 mark view->gline = 0;
7619 94b80cfa 2022-08-01 mark if (g == 0)
7621 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
7622 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7624 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
7625 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
7626 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
7628 94b80cfa 2022-08-01 mark if (*fte != NULL) {
7629 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7630 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
7632 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
7633 94b80cfa 2022-08-01 mark last += off;
7635 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
7636 94b80cfa 2022-08-01 mark s->selected = g - first;
7637 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
7640 94b80cfa 2022-08-01 mark if (*ste != NULL) {
7641 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
7645 94b80cfa 2022-08-01 mark if (i < g) {
7646 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
7648 94b80cfa 2022-08-01 mark return err;
7649 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
7650 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
7651 94b80cfa 2022-08-01 mark first + s->selected < g &&
7652 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
7653 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7654 94b80cfa 2022-08-01 mark first += off;
7655 94b80cfa 2022-08-01 mark s->selected = g - first;
7657 94b80cfa 2022-08-01 mark } else if (i > g)
7658 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
7660 94b80cfa 2022-08-01 mark if (g < nlines &&
7661 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7662 94b80cfa 2022-08-01 mark s->selected = g - 1;
7664 94b80cfa 2022-08-01 mark return NULL;
7667 e5a0f69f 2018-08-18 stsp static const struct got_error *
7668 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7670 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7671 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7672 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7673 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
7675 94b80cfa 2022-08-01 mark if (view->gline)
7676 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
7678 e5a0f69f 2018-08-18 stsp switch (ch) {
7681 d627976f 2023-02-01 mark case KEY_RIGHT:
7683 d627976f 2023-02-01 mark case KEY_LEFT:
7685 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
7688 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7689 640cd7ff 2022-06-22 mark view->count = 0;
7692 640cd7ff 2022-06-22 mark view->count = 0;
7693 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7695 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7698 640cd7ff 2022-06-22 mark view->count = 0;
7699 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
7701 e4526bf5 2021-09-03 naddy case 'g':
7703 e4526bf5 2021-09-03 naddy case KEY_HOME:
7704 e4526bf5 2021-09-03 naddy s->selected = 0;
7705 640cd7ff 2022-06-22 mark view->count = 0;
7706 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7707 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7708 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7710 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7712 e4526bf5 2021-09-03 naddy case 'G':
7714 9b058f45 2022-06-30 mark case KEY_END: {
7715 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
7717 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7718 9b058f45 2022-06-30 mark --eos; /* border */
7719 e4526bf5 2021-09-03 naddy s->selected = 0;
7720 640cd7ff 2022-06-22 mark view->count = 0;
7721 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7722 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7723 e4526bf5 2021-09-03 naddy if (te == NULL) {
7724 ad055527 2022-07-16 mark if (s->tree != s->root) {
7725 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7730 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7731 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7733 e4526bf5 2021-09-03 naddy if (n > 0)
7734 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7738 1e37a5c2 2019-05-12 jcs case KEY_UP:
7739 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7740 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7741 1e37a5c2 2019-05-12 jcs s->selected--;
7744 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7745 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7746 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7747 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7748 640cd7ff 2022-06-22 mark view->count = 0;
7750 83cc4199 2022-06-13 stsp case CTRL('u'):
7752 83cc4199 2022-06-13 stsp nscroll /= 2;
7753 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7754 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7755 ea025d1d 2020-02-22 naddy case CTRL('b'):
7757 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7758 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7759 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7760 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7762 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7763 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7765 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7766 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7767 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7768 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7769 640cd7ff 2022-06-22 mark view->count = 0;
7772 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7773 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7774 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7775 1e37a5c2 2019-05-12 jcs s->selected++;
7778 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7779 640cd7ff 2022-06-22 mark == NULL) {
7780 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7781 640cd7ff 2022-06-22 mark view->count = 0;
7784 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7786 83cc4199 2022-06-13 stsp case CTRL('d'):
7788 83cc4199 2022-06-13 stsp nscroll /= 2;
7789 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7790 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7791 ea025d1d 2020-02-22 naddy case CTRL('f'):
7793 48bb96f0 2022-06-20 naddy case ' ':
7794 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7796 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7797 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7798 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7799 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7801 640cd7ff 2022-06-22 mark view->count = 0;
7804 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7806 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7808 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7809 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7810 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7811 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7812 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7813 640cd7ff 2022-06-22 mark view->count = 0;
7816 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7817 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7819 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7820 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7821 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7822 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7823 1e37a5c2 2019-05-12 jcs s->selected_entry =
7824 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7825 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7826 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7827 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7831 1e37a5c2 2019-05-12 jcs free(parent);
7832 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7833 56e0773d 2019-11-28 stsp s->selected_entry))) {
7834 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7835 640cd7ff 2022-06-22 mark view->count = 0;
7836 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7837 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7840 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7841 941e9f74 2019-05-21 stsp if (err) {
7842 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7845 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7846 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7848 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7849 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7850 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7851 640cd7ff 2022-06-22 mark view->count = 0;
7854 640cd7ff 2022-06-22 mark view->count = 0;
7858 ffd1d5e5 2018-06-23 stsp return err;
7861 ffd1d5e5 2018-06-23 stsp __dead static void
7862 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7865 87411fa9 2022-06-16 stsp fprintf(stderr,
7866 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7867 ffd1d5e5 2018-06-23 stsp getprogname());
7871 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7872 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7874 7483826e 2023-04-14 mark const struct got_error *error;
7875 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7876 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7877 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7878 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7879 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7880 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7881 4e97c21c 2020-12-06 stsp char *label = NULL;
7882 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7883 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7885 5221c383 2018-08-01 stsp struct tog_view *view;
7886 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7888 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7889 ffd1d5e5 2018-06-23 stsp switch (ch) {
7891 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7894 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7895 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7896 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7900 e99e2d15 2020-11-24 naddy usage_tree();
7901 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7905 ffd1d5e5 2018-06-23 stsp argc -= optind;
7906 ffd1d5e5 2018-06-23 stsp argv += optind;
7908 55cccc34 2020-02-20 stsp if (argc > 1)
7909 e99e2d15 2020-11-24 naddy usage_tree();
7911 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7912 0ae84acc 2022-06-15 tracey if (error != NULL)
7913 0ae84acc 2022-06-15 tracey goto done;
7915 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7916 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7917 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7918 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7919 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7920 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7921 c156c7a4 2020-12-18 stsp goto done;
7922 55cccc34 2020-02-20 stsp if (worktree)
7923 52185f70 2019-02-05 stsp repo_path =
7924 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7926 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7927 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7928 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7929 c156c7a4 2020-12-18 stsp goto done;
7933 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7934 c02c541e 2019-03-29 stsp if (error != NULL)
7935 52185f70 2019-02-05 stsp goto done;
7937 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7938 55cccc34 2020-02-20 stsp repo, worktree);
7939 55cccc34 2020-02-20 stsp if (error)
7940 55cccc34 2020-02-20 stsp goto done;
7942 098596c5 2023-04-14 stsp init_curses();
7944 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7945 c02c541e 2019-03-29 stsp if (error)
7946 52185f70 2019-02-05 stsp goto done;
7948 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7949 51a10b52 2020-12-26 stsp if (error)
7950 51a10b52 2020-12-26 stsp goto done;
7952 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7953 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7954 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7955 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7956 4e97c21c 2020-12-06 stsp if (error)
7957 4e97c21c 2020-12-06 stsp goto done;
7958 4e97c21c 2020-12-06 stsp head_ref_name = label;
7960 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7961 4e97c21c 2020-12-06 stsp if (error == NULL)
7962 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7963 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7964 4e97c21c 2020-12-06 stsp goto done;
7965 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7966 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7967 4e97c21c 2020-12-06 stsp if (error)
7968 4e97c21c 2020-12-06 stsp goto done;
7971 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7972 a44927cc 2022-04-07 stsp if (error)
7973 a44927cc 2022-04-07 stsp goto done;
7975 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7976 5221c383 2018-08-01 stsp if (view == NULL) {
7977 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7978 5221c383 2018-08-01 stsp goto done;
7980 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7981 ad80ab7b 2018-08-04 stsp if (error)
7982 ad80ab7b 2018-08-04 stsp goto done;
7983 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7984 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7985 d91faf3b 2020-12-01 naddy in_repo_path);
7986 55cccc34 2020-02-20 stsp if (error)
7987 55cccc34 2020-02-20 stsp goto done;
7990 55cccc34 2020-02-20 stsp if (worktree) {
7991 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7992 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7993 55cccc34 2020-02-20 stsp worktree = NULL;
7995 098596c5 2023-04-14 stsp error = view_loop(view);
7997 52185f70 2019-02-05 stsp free(repo_path);
7998 e4a0e26d 2020-02-20 stsp free(cwd);
7999 ffd1d5e5 2018-06-23 stsp free(commit_id);
8000 4e97c21c 2020-12-06 stsp free(label);
8002 486cd271 2020-12-06 stsp got_ref_close(ref);
8003 1d0f4054 2021-06-17 stsp if (repo) {
8004 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8005 1d0f4054 2021-06-17 stsp if (error == NULL)
8006 1d0f4054 2021-06-17 stsp error = close_err;
8008 0ae84acc 2022-06-15 tracey if (pack_fds) {
8009 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8010 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8011 0ae84acc 2022-06-15 tracey if (error == NULL)
8012 0ae84acc 2022-06-15 tracey error = pack_err;
8014 51a10b52 2020-12-26 stsp tog_free_refs();
8015 ffd1d5e5 2018-06-23 stsp return error;
8018 6458efa5 2020-11-24 stsp static const struct got_error *
8019 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
8021 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
8022 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8024 6458efa5 2020-11-24 stsp s->nrefs = 0;
8025 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
8026 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
8027 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
8028 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
8029 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
8032 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
8033 6458efa5 2020-11-24 stsp if (re == NULL)
8034 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
8036 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
8037 8924d611 2020-12-26 stsp if (re->ref == NULL)
8038 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
8039 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
8040 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
8043 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8044 6458efa5 2020-11-24 stsp return NULL;
8048 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
8050 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8052 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
8053 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8054 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
8055 8924d611 2020-12-26 stsp got_ref_close(re->ref);
8060 6458efa5 2020-11-24 stsp static const struct got_error *
8061 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
8063 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8064 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8066 6458efa5 2020-11-24 stsp s->selected_entry = 0;
8067 6458efa5 2020-11-24 stsp s->repo = repo;
8069 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
8070 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
8072 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8074 2ca2f982 2023-04-14 mark goto done;
8076 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
8077 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
8078 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
8079 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
8081 6458efa5 2020-11-24 stsp goto done;
8083 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
8084 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
8085 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
8087 6458efa5 2020-11-24 stsp goto done;
8089 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
8090 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
8091 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
8093 cc488aa7 2022-01-23 stsp goto done;
8095 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
8096 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
8097 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
8099 6458efa5 2020-11-24 stsp goto done;
8102 6458efa5 2020-11-24 stsp view->show = show_ref_view;
8103 6458efa5 2020-11-24 stsp view->input = input_ref_view;
8104 6458efa5 2020-11-24 stsp view->close = close_ref_view;
8105 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
8106 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
8108 2ca2f982 2023-04-14 mark if (err) {
8109 2ca2f982 2023-04-14 mark if (view->close == NULL)
8110 2ca2f982 2023-04-14 mark close_ref_view(view);
8111 2ca2f982 2023-04-14 mark view_close(view);
8113 6458efa5 2020-11-24 stsp return err;
8116 6458efa5 2020-11-24 stsp static const struct got_error *
8117 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
8119 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8121 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8122 6458efa5 2020-11-24 stsp free_colors(&s->colors);
8124 6458efa5 2020-11-24 stsp return NULL;
8127 6458efa5 2020-11-24 stsp static const struct got_error *
8128 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
8129 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8131 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8132 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
8133 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
8134 6458efa5 2020-11-24 stsp int obj_type;
8136 c42c9805 2020-11-24 stsp *commit_id = NULL;
8138 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
8140 6458efa5 2020-11-24 stsp return err;
8142 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
8144 6458efa5 2020-11-24 stsp goto done;
8146 6458efa5 2020-11-24 stsp switch (obj_type) {
8147 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
8148 c42c9805 2020-11-24 stsp *commit_id = obj_id;
8150 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
8151 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
8153 6458efa5 2020-11-24 stsp goto done;
8154 c42c9805 2020-11-24 stsp free(obj_id);
8155 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
8156 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8158 6458efa5 2020-11-24 stsp goto done;
8159 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8160 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8161 c42c9805 2020-11-24 stsp goto done;
8163 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
8164 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8165 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
8166 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
8167 c42c9805 2020-11-24 stsp goto done;
8171 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8177 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
8178 c42c9805 2020-11-24 stsp if (err) {
8179 c42c9805 2020-11-24 stsp free(*commit_id);
8180 c42c9805 2020-11-24 stsp *commit_id = NULL;
8182 c42c9805 2020-11-24 stsp return err;
8185 c42c9805 2020-11-24 stsp static const struct got_error *
8186 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8187 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8189 c42c9805 2020-11-24 stsp struct tog_view *log_view;
8190 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8191 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
8193 c42c9805 2020-11-24 stsp *new_view = NULL;
8195 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8196 c42c9805 2020-11-24 stsp if (err) {
8197 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8198 c42c9805 2020-11-24 stsp return err;
8200 c42c9805 2020-11-24 stsp return NULL;
8203 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8204 6458efa5 2020-11-24 stsp if (log_view == NULL) {
8205 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
8206 6458efa5 2020-11-24 stsp goto done;
8209 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
8210 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
8213 6458efa5 2020-11-24 stsp view_close(log_view);
8215 6458efa5 2020-11-24 stsp *new_view = log_view;
8216 c42c9805 2020-11-24 stsp free(commit_id);
8217 6458efa5 2020-11-24 stsp return err;
8220 ce5b7c56 2019-07-09 stsp static void
8221 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8223 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
8224 34ba6917 2020-11-30 stsp int i = 0;
8226 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8229 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8230 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
8231 34ba6917 2020-11-30 stsp if (re == NULL)
8233 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
8234 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8238 9b058f45 2022-06-30 mark static const struct got_error *
8239 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
8241 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
8242 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
8243 6458efa5 2020-11-24 stsp int n = 0;
8245 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
8246 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
8248 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
8250 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
8251 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
8252 94b80cfa 2022-08-01 mark if (last) {
8253 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
8254 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
8256 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8257 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
8258 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
8262 9b058f45 2022-06-30 mark return NULL;
8265 6458efa5 2020-11-24 stsp static const struct got_error *
8266 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
8268 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8270 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
8271 6458efa5 2020-11-24 stsp return NULL;
8274 6458efa5 2020-11-24 stsp static int
8275 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8277 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8279 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8283 6458efa5 2020-11-24 stsp static const struct got_error *
8284 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8286 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8287 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8289 6458efa5 2020-11-24 stsp if (!view->searching) {
8290 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8291 6458efa5 2020-11-24 stsp return NULL;
8294 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8295 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8296 6458efa5 2020-11-24 stsp if (s->selected_entry)
8297 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8299 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8300 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8302 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8303 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8305 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8306 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8309 487cd7d2 2021-12-17 stsp if (s->selected_entry)
8310 487cd7d2 2021-12-17 stsp re = s->selected_entry;
8311 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
8312 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8314 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8317 6458efa5 2020-11-24 stsp while (1) {
8318 6458efa5 2020-11-24 stsp if (re == NULL) {
8319 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8320 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8321 6458efa5 2020-11-24 stsp return NULL;
8323 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8324 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8326 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8329 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8330 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8331 6458efa5 2020-11-24 stsp s->matched_entry = re;
8335 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8336 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8338 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8341 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8342 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8343 6458efa5 2020-11-24 stsp s->selected = 0;
8346 6458efa5 2020-11-24 stsp return NULL;
8349 6458efa5 2020-11-24 stsp static const struct got_error *
8350 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8352 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8353 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8354 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8355 6458efa5 2020-11-24 stsp char *line = NULL;
8356 6458efa5 2020-11-24 stsp wchar_t *wline;
8357 6458efa5 2020-11-24 stsp struct tog_color *tc;
8358 0ed6bc90 2023-02-01 mark int width, n, scrollx;
8359 6458efa5 2020-11-24 stsp int limit = view->nlines;
8361 6458efa5 2020-11-24 stsp werase(view->window);
8363 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8364 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
8365 9b058f45 2022-06-30 mark --limit; /* border */
8367 6458efa5 2020-11-24 stsp if (limit == 0)
8368 6458efa5 2020-11-24 stsp return NULL;
8370 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8372 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8373 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8374 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8376 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8377 6458efa5 2020-11-24 stsp if (err) {
8378 6458efa5 2020-11-24 stsp free(line);
8379 6458efa5 2020-11-24 stsp return err;
8381 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8382 6458efa5 2020-11-24 stsp wstandout(view->window);
8383 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8384 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
8385 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
8386 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8387 6458efa5 2020-11-24 stsp wstandend(view->window);
8388 6458efa5 2020-11-24 stsp free(wline);
8389 6458efa5 2020-11-24 stsp wline = NULL;
8390 6458efa5 2020-11-24 stsp free(line);
8391 6458efa5 2020-11-24 stsp line = NULL;
8392 6458efa5 2020-11-24 stsp if (--limit <= 0)
8393 6458efa5 2020-11-24 stsp return NULL;
8396 0ed6bc90 2023-02-01 mark view->maxx = 0;
8397 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8398 6458efa5 2020-11-24 stsp char *line = NULL;
8399 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8401 b4996bee 2022-06-16 stsp if (s->show_date) {
8402 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
8403 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
8404 b4996bee 2022-06-16 stsp struct got_object_id *id;
8405 b4996bee 2022-06-16 stsp struct tm tm;
8408 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8410 b4996bee 2022-06-16 stsp return err;
8411 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
8412 b4996bee 2022-06-16 stsp if (err) {
8413 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
8415 b4996bee 2022-06-16 stsp return err;
8417 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
8419 b4996bee 2022-06-16 stsp if (err) {
8421 b4996bee 2022-06-16 stsp return err;
8423 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
8424 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
8426 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
8427 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
8430 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
8431 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
8432 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8433 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
8435 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8436 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
8437 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
8438 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8439 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8440 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8441 6458efa5 2020-11-24 stsp struct got_object_id *id;
8442 6458efa5 2020-11-24 stsp char *id_str;
8443 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8445 6458efa5 2020-11-24 stsp return err;
8446 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8447 6458efa5 2020-11-24 stsp if (err) {
8449 6458efa5 2020-11-24 stsp return err;
8451 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8452 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8453 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8455 6458efa5 2020-11-24 stsp free(id_str);
8456 6458efa5 2020-11-24 stsp return err;
8459 6458efa5 2020-11-24 stsp free(id_str);
8460 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8461 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
8462 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
8464 0ed6bc90 2023-02-01 mark /* use full line width to determine view->maxx */
8465 0ed6bc90 2023-02-01 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8466 6458efa5 2020-11-24 stsp if (err) {
8467 6458efa5 2020-11-24 stsp free(line);
8468 6458efa5 2020-11-24 stsp return err;
8470 0ed6bc90 2023-02-01 mark view->maxx = MAX(view->maxx, width);
8471 0ed6bc90 2023-02-01 mark free(wline);
8472 0ed6bc90 2023-02-01 mark wline = NULL;
8474 0ed6bc90 2023-02-01 mark err = format_line(&wline, &width, &scrollx, line, view->x,
8475 0ed6bc90 2023-02-01 mark view->ncols, 0, 0);
8476 0ed6bc90 2023-02-01 mark if (err) {
8477 0ed6bc90 2023-02-01 mark free(line);
8478 0ed6bc90 2023-02-01 mark return err;
8480 6458efa5 2020-11-24 stsp if (n == s->selected) {
8481 6458efa5 2020-11-24 stsp if (view->focussed)
8482 6458efa5 2020-11-24 stsp wstandout(view->window);
8483 6458efa5 2020-11-24 stsp s->selected_entry = re;
8485 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8487 6458efa5 2020-11-24 stsp wattr_on(view->window,
8488 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8489 0ed6bc90 2023-02-01 mark waddwstr(view->window, &wline[scrollx]);
8491 6458efa5 2020-11-24 stsp wattr_off(view->window,
8492 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8493 b637f33f 2023-02-01 mark if (width < view->ncols)
8494 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8495 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8496 6458efa5 2020-11-24 stsp wstandend(view->window);
8497 6458efa5 2020-11-24 stsp free(line);
8498 6458efa5 2020-11-24 stsp free(wline);
8499 6458efa5 2020-11-24 stsp wline = NULL;
8501 6458efa5 2020-11-24 stsp s->ndisplayed++;
8502 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8505 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8508 9b058f45 2022-06-30 mark view_border(view);
8509 6458efa5 2020-11-24 stsp return err;
8512 6458efa5 2020-11-24 stsp static const struct got_error *
8513 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8514 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8516 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8517 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8518 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8520 c42c9805 2020-11-24 stsp *new_view = NULL;
8522 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8523 c42c9805 2020-11-24 stsp if (err) {
8524 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8525 c42c9805 2020-11-24 stsp return err;
8527 c42c9805 2020-11-24 stsp return NULL;
8531 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8532 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8533 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8534 c42c9805 2020-11-24 stsp goto done;
8537 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8538 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8540 c42c9805 2020-11-24 stsp goto done;
8542 c42c9805 2020-11-24 stsp *new_view = tree_view;
8544 c42c9805 2020-11-24 stsp free(commit_id);
8545 c42c9805 2020-11-24 stsp return err;
8548 94b80cfa 2022-08-01 mark static const struct got_error *
8549 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
8551 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
8552 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
8553 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
8555 94b80cfa 2022-08-01 mark g = view->gline;
8556 94b80cfa 2022-08-01 mark view->gline = 0;
8558 94b80cfa 2022-08-01 mark if (g == 0)
8560 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
8561 94b80cfa 2022-08-01 mark g = s->nrefs;
8563 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
8564 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
8565 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
8566 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8567 94b80cfa 2022-08-01 mark return NULL;
8570 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
8571 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
8573 94b80cfa 2022-08-01 mark return err;
8574 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8575 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
8576 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
8577 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8578 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
8579 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
8581 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
8582 94b80cfa 2022-08-01 mark s->selected = g - 1;
8584 94b80cfa 2022-08-01 mark return NULL;
8588 c42c9805 2020-11-24 stsp static const struct got_error *
8589 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8591 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8592 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8593 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8594 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
8596 94b80cfa 2022-08-01 mark if (view->gline)
8597 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
8599 6458efa5 2020-11-24 stsp switch (ch) {
8602 0ed6bc90 2023-02-01 mark case KEY_RIGHT:
8604 0ed6bc90 2023-02-01 mark case KEY_LEFT:
8606 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
8609 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8610 640cd7ff 2022-06-22 mark view->count = 0;
8613 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
8614 640cd7ff 2022-06-22 mark view->count = 0;
8617 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
8618 3f6c6614 2023-01-23 mark view->action = s->sort_by_date ? "sort by date" : "sort by name";
8619 640cd7ff 2022-06-22 mark view->count = 0;
8620 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8621 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
8622 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
8625 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
8626 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
8627 50617b77 2021-11-20 stsp &tog_refs, s->repo);
8630 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
8631 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
8633 6458efa5 2020-11-24 stsp case KEY_ENTER:
8634 6458efa5 2020-11-24 stsp case '\r':
8635 640cd7ff 2022-06-22 mark view->count = 0;
8636 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8638 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
8641 640cd7ff 2022-06-22 mark view->count = 0;
8642 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8644 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
8646 e4526bf5 2021-09-03 naddy case 'g':
8648 e4526bf5 2021-09-03 naddy case KEY_HOME:
8649 e4526bf5 2021-09-03 naddy s->selected = 0;
8650 640cd7ff 2022-06-22 mark view->count = 0;
8651 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8653 e4526bf5 2021-09-03 naddy case 'G':
8655 9b058f45 2022-06-30 mark case KEY_END: {
8656 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
8658 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
8659 9b058f45 2022-06-30 mark --eos; /* border */
8660 e4526bf5 2021-09-03 naddy s->selected = 0;
8661 640cd7ff 2022-06-22 mark view->count = 0;
8662 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8663 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
8664 e4526bf5 2021-09-03 naddy if (re == NULL)
8666 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8667 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8669 e4526bf5 2021-09-03 naddy if (n > 0)
8670 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8674 6458efa5 2020-11-24 stsp case KEY_UP:
8675 02ffd0d5 2021-10-17 stsp case CTRL('p'):
8676 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8677 6458efa5 2020-11-24 stsp s->selected--;
8680 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8681 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8682 640cd7ff 2022-06-22 mark view->count = 0;
8684 83cc4199 2022-06-13 stsp case CTRL('u'):
8686 83cc4199 2022-06-13 stsp nscroll /= 2;
8687 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8688 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8689 6458efa5 2020-11-24 stsp case CTRL('b'):
8691 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8692 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
8693 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
8694 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8695 640cd7ff 2022-06-22 mark view->count = 0;
8698 6458efa5 2020-11-24 stsp case KEY_DOWN:
8699 02ffd0d5 2021-10-17 stsp case CTRL('n'):
8700 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8701 6458efa5 2020-11-24 stsp s->selected++;
8704 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8705 6458efa5 2020-11-24 stsp /* can't scroll any further */
8706 640cd7ff 2022-06-22 mark view->count = 0;
8709 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
8711 83cc4199 2022-06-13 stsp case CTRL('d'):
8713 83cc4199 2022-06-13 stsp nscroll /= 2;
8714 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8715 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8716 6458efa5 2020-11-24 stsp case CTRL('f'):
8718 48bb96f0 2022-06-20 naddy case ' ':
8719 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8720 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8721 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8722 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
8723 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
8724 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
8725 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
8726 640cd7ff 2022-06-22 mark view->count = 0;
8729 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
8731 6458efa5 2020-11-24 stsp case CTRL('l'):
8732 640cd7ff 2022-06-22 mark view->count = 0;
8733 8924d611 2020-12-26 stsp tog_free_refs();
8734 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
8737 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8738 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8740 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8741 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8742 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8745 640cd7ff 2022-06-22 mark view->count = 0;
8749 6458efa5 2020-11-24 stsp return err;
8752 6458efa5 2020-11-24 stsp __dead static void
8753 6458efa5 2020-11-24 stsp usage_ref(void)
8756 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8757 6458efa5 2020-11-24 stsp getprogname());
8761 6458efa5 2020-11-24 stsp static const struct got_error *
8762 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8764 7483826e 2023-04-14 mark const struct got_error *error;
8765 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8766 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8767 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8769 6458efa5 2020-11-24 stsp struct tog_view *view;
8770 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8772 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8773 6458efa5 2020-11-24 stsp switch (ch) {
8775 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8776 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8777 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8781 e99e2d15 2020-11-24 naddy usage_ref();
8782 6458efa5 2020-11-24 stsp /* NOTREACHED */
8786 6458efa5 2020-11-24 stsp argc -= optind;
8787 6458efa5 2020-11-24 stsp argv += optind;
8789 6458efa5 2020-11-24 stsp if (argc > 1)
8790 e99e2d15 2020-11-24 naddy usage_ref();
8792 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8793 0ae84acc 2022-06-15 tracey if (error != NULL)
8794 0ae84acc 2022-06-15 tracey goto done;
8796 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8797 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8798 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8799 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8800 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8801 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8802 c156c7a4 2020-12-18 stsp goto done;
8803 6458efa5 2020-11-24 stsp if (worktree)
8804 6458efa5 2020-11-24 stsp repo_path =
8805 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8807 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8808 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8809 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8810 c156c7a4 2020-12-18 stsp goto done;
8814 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8815 6458efa5 2020-11-24 stsp if (error != NULL)
8816 6458efa5 2020-11-24 stsp goto done;
8818 098596c5 2023-04-14 stsp init_curses();
8820 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8821 51a10b52 2020-12-26 stsp if (error)
8822 51a10b52 2020-12-26 stsp goto done;
8824 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8825 6458efa5 2020-11-24 stsp if (error)
8826 6458efa5 2020-11-24 stsp goto done;
8828 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8829 6458efa5 2020-11-24 stsp if (view == NULL) {
8830 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8831 6458efa5 2020-11-24 stsp goto done;
8834 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8835 6458efa5 2020-11-24 stsp if (error)
8836 6458efa5 2020-11-24 stsp goto done;
8838 6458efa5 2020-11-24 stsp if (worktree) {
8839 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8840 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8841 6458efa5 2020-11-24 stsp worktree = NULL;
8843 098596c5 2023-04-14 stsp error = view_loop(view);
8845 6458efa5 2020-11-24 stsp free(repo_path);
8846 6458efa5 2020-11-24 stsp free(cwd);
8847 1d0f4054 2021-06-17 stsp if (repo) {
8848 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8849 1d0f4054 2021-06-17 stsp if (close_err)
8850 1d0f4054 2021-06-17 stsp error = close_err;
8852 0ae84acc 2022-06-15 tracey if (pack_fds) {
8853 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8854 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8855 0ae84acc 2022-06-15 tracey if (error == NULL)
8856 0ae84acc 2022-06-15 tracey error = pack_err;
8858 51a10b52 2020-12-26 stsp tog_free_refs();
8859 6458efa5 2020-11-24 stsp return error;
8862 ec2a9698 2022-09-15 mark static const struct got_error*
8863 ec2a9698 2022-09-15 mark win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8864 ec2a9698 2022-09-15 mark const char *str)
8866 ec2a9698 2022-09-15 mark size_t len;
8868 ec2a9698 2022-09-15 mark if (win == NULL)
8869 ec2a9698 2022-09-15 mark win = stdscr;
8871 ec2a9698 2022-09-15 mark len = strlen(str);
8872 ec2a9698 2022-09-15 mark x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8874 ec2a9698 2022-09-15 mark if (focus)
8875 ec2a9698 2022-09-15 mark wstandout(win);
8876 ec2a9698 2022-09-15 mark if (mvwprintw(win, y, x, "%s", str) == ERR)
8877 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8878 ec2a9698 2022-09-15 mark if (focus)
8879 ec2a9698 2022-09-15 mark wstandend(win);
8881 ec2a9698 2022-09-15 mark return NULL;
8884 136e2bd4 2022-07-23 mark static const struct got_error *
8885 ec2a9698 2022-09-15 mark add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8889 ec2a9698 2022-09-15 mark p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8890 ec2a9698 2022-09-15 mark if (p == NULL) {
8891 ec2a9698 2022-09-15 mark free(*line_offsets);
8892 ec2a9698 2022-09-15 mark *line_offsets = NULL;
8893 ec2a9698 2022-09-15 mark return got_error_from_errno("reallocarray");
8896 ec2a9698 2022-09-15 mark *line_offsets = p;
8897 ec2a9698 2022-09-15 mark (*line_offsets)[*nlines] = off;
8898 ec2a9698 2022-09-15 mark ++(*nlines);
8899 ec2a9698 2022-09-15 mark return NULL;
8902 ec2a9698 2022-09-15 mark static const struct got_error *
8903 ec2a9698 2022-09-15 mark max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8907 ec2a9698 2022-09-15 mark for (;n > 0; --n, ++km) {
8908 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8909 ec2a9698 2022-09-15 mark size_t len = 1;
8911 ec2a9698 2022-09-15 mark if (km->keys == NULL)
8914 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8915 ec2a9698 2022-09-15 mark if (t0 == NULL)
8916 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8918 ec2a9698 2022-09-15 mark len += strlen(t);
8919 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL)
8920 ec2a9698 2022-09-15 mark len += strlen(k) > 1 ? 2 : 0;
8922 ec2a9698 2022-09-15 mark *ret = MAX(*ret, len);
8925 ec2a9698 2022-09-15 mark return NULL;
8929 ec2a9698 2022-09-15 mark * Write keymap section headers, keys, and key info in km to f.
8930 ec2a9698 2022-09-15 mark * Save line offset to *off. If terminal has UTF8 encoding enabled,
8931 ec2a9698 2022-09-15 mark * wrap control and symbolic keys in guillemets, else use <>.
8933 ec2a9698 2022-09-15 mark static const struct got_error *
8934 ec2a9698 2022-09-15 mark format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8936 ec2a9698 2022-09-15 mark int n, len = width;
8938 ec2a9698 2022-09-15 mark if (km->keys) {
8939 1681ba87 2022-09-18 mark static const char *u8_glyph[] = {
8940 1681ba87 2022-09-18 mark "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8941 1681ba87 2022-09-18 mark "\xe2\x80\xba" /* U+203A (utf8 >) */
8943 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8944 ec2a9698 2022-09-15 mark int cs, s, first = 1;
8946 ec2a9698 2022-09-15 mark cs = got_locale_is_utf8();
8948 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8949 ec2a9698 2022-09-15 mark if (t0 == NULL)
8950 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8952 ec2a9698 2022-09-15 mark len = strlen(km->keys);
8953 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL) {
8954 ec2a9698 2022-09-15 mark s = strlen(k) > 1; /* control or symbolic key */
8955 ec2a9698 2022-09-15 mark n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8956 1681ba87 2022-09-18 mark cs && s ? u8_glyph[0] : s ? "<" : "", k,
8957 1681ba87 2022-09-18 mark cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8958 ec2a9698 2022-09-15 mark if (n < 0) {
8960 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8962 ec2a9698 2022-09-15 mark first = 0;
8963 ec2a9698 2022-09-15 mark len += s ? 2 : 0;
8964 ec2a9698 2022-09-15 mark *off += n;
8968 ec2a9698 2022-09-15 mark n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8969 ec2a9698 2022-09-15 mark if (n < 0)
8970 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8971 ec2a9698 2022-09-15 mark *off += n;
8973 ec2a9698 2022-09-15 mark return NULL;
8976 ec2a9698 2022-09-15 mark static const struct got_error *
8977 ec2a9698 2022-09-15 mark format_help(struct tog_help_view_state *s)
8979 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8980 ec2a9698 2022-09-15 mark off_t off = 0;
8981 ec2a9698 2022-09-15 mark int i, max, n, show = s->all;
8982 ec2a9698 2022-09-15 mark static const struct tog_key_map km[] = {
8983 ec2a9698 2022-09-15 mark #define KEYMAP_(info, type) { NULL, (info), type }
8984 ec2a9698 2022-09-15 mark #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8985 ec2a9698 2022-09-15 mark GENERATE_HELP
8986 ec2a9698 2022-09-15 mark #undef KEYMAP_
8987 ec2a9698 2022-09-15 mark #undef KEY_
8990 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8992 ec2a9698 2022-09-15 mark return err;
8994 ec2a9698 2022-09-15 mark n = nitems(km);
8995 ec2a9698 2022-09-15 mark err = max_key_str(&max, km, n);
8997 ec2a9698 2022-09-15 mark return err;
8999 ec2a9698 2022-09-15 mark for (i = 0; i < n; ++i) {
9000 ec2a9698 2022-09-15 mark if (km[i].keys == NULL) {
9001 ec2a9698 2022-09-15 mark show = s->all;
9002 ec2a9698 2022-09-15 mark if (km[i].type == TOG_KEYMAP_GLOBAL ||
9003 ec2a9698 2022-09-15 mark km[i].type == s->type || s->all)
9006 ec2a9698 2022-09-15 mark if (show) {
9007 ec2a9698 2022-09-15 mark err = format_help_line(&off, s->f, &km[i], max);
9009 ec2a9698 2022-09-15 mark return err;
9010 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
9012 ec2a9698 2022-09-15 mark return err;
9015 ec2a9698 2022-09-15 mark fputc('\n', s->f);
9017 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
9018 ec2a9698 2022-09-15 mark return err;
9021 ec2a9698 2022-09-15 mark static const struct got_error *
9022 ec2a9698 2022-09-15 mark create_help(struct tog_help_view_state *s)
9025 ec2a9698 2022-09-15 mark const struct got_error *err;
9027 ec2a9698 2022-09-15 mark free(s->line_offsets);
9028 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
9029 ec2a9698 2022-09-15 mark s->nlines = 0;
9031 ec2a9698 2022-09-15 mark f = got_opentemp();
9032 ec2a9698 2022-09-15 mark if (f == NULL)
9033 ec2a9698 2022-09-15 mark return got_error_from_errno("got_opentemp");
9036 ec2a9698 2022-09-15 mark err = format_help(s);
9038 ec2a9698 2022-09-15 mark return err;
9040 ec2a9698 2022-09-15 mark if (s->f && fflush(s->f) != 0)
9041 ec2a9698 2022-09-15 mark return got_error_from_errno("fflush");
9043 ec2a9698 2022-09-15 mark return NULL;
9046 ec2a9698 2022-09-15 mark static const struct got_error *
9047 ec2a9698 2022-09-15 mark search_start_help_view(struct tog_view *view)
9049 ec2a9698 2022-09-15 mark view->state.help.matched_line = 0;
9050 ec2a9698 2022-09-15 mark return NULL;
9053 eaf2c13e 2022-09-17 mark static void
9054 eaf2c13e 2022-09-17 mark search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9055 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
9057 eaf2c13e 2022-09-17 mark struct tog_help_view_state *s = &view->state.help;
9059 eaf2c13e 2022-09-17 mark *f = s->f;
9060 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
9061 eaf2c13e 2022-09-17 mark *line_offsets = s->line_offsets;
9062 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
9063 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
9064 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
9065 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
9068 ec2a9698 2022-09-15 mark static const struct got_error *
9069 ec2a9698 2022-09-15 mark show_help_view(struct tog_view *view)
9071 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9072 ec2a9698 2022-09-15 mark const struct got_error *err;
9073 ec2a9698 2022-09-15 mark regmatch_t *regmatch = &view->regmatch;
9074 ec2a9698 2022-09-15 mark wchar_t *wline;
9075 ec2a9698 2022-09-15 mark char *line;
9076 ec2a9698 2022-09-15 mark ssize_t linelen;
9077 ec2a9698 2022-09-15 mark size_t linesz = 0;
9078 ec2a9698 2022-09-15 mark int width, nprinted = 0, rc = 0;
9079 ec2a9698 2022-09-15 mark int eos = view->nlines;
9081 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
9082 ec2a9698 2022-09-15 mark --eos; /* account for border */
9084 ec2a9698 2022-09-15 mark s->lineno = 0;
9085 ec2a9698 2022-09-15 mark rewind(s->f);
9086 ec2a9698 2022-09-15 mark werase(view->window);
9088 ec2a9698 2022-09-15 mark if (view->gline > s->nlines - 1)
9089 ec2a9698 2022-09-15 mark view->gline = s->nlines - 1;
9091 ec2a9698 2022-09-15 mark err = win_draw_center(view->window, 0, 0, view->ncols,
9092 16a2d4f0 2022-09-19 stsp view_needs_focus_indication(view),
9093 12c07a25 2022-09-19 stsp "tog help (press q to return to tog)");
9095 ec2a9698 2022-09-15 mark return err;
9096 ec2a9698 2022-09-15 mark if (eos <= 1)
9097 ec2a9698 2022-09-15 mark return NULL;
9098 ec2a9698 2022-09-15 mark waddstr(view->window, "\n\n");
9101 ec2a9698 2022-09-15 mark s->eof = 0;
9102 ec2a9698 2022-09-15 mark view->maxx = 0;
9103 ec2a9698 2022-09-15 mark line = NULL;
9104 ec2a9698 2022-09-15 mark while (eos > 0 && nprinted < eos) {
9105 ec2a9698 2022-09-15 mark attr_t attr = 0;
9107 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
9108 ec2a9698 2022-09-15 mark if (linelen == -1) {
9109 ec2a9698 2022-09-15 mark if (!feof(s->f)) {
9110 ec2a9698 2022-09-15 mark free(line);
9111 ec2a9698 2022-09-15 mark return got_ferror(s->f, GOT_ERR_IO);
9113 ec2a9698 2022-09-15 mark s->eof = 1;
9116 ec2a9698 2022-09-15 mark if (++s->lineno < s->first_displayed_line)
9118 ec2a9698 2022-09-15 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9120 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline)
9121 ec2a9698 2022-09-15 mark attr = A_STANDOUT;
9123 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9124 ec2a9698 2022-09-15 mark view->x ? 1 : 0);
9125 ec2a9698 2022-09-15 mark if (err) {
9126 ec2a9698 2022-09-15 mark free(line);
9127 ec2a9698 2022-09-15 mark return err;
9129 ec2a9698 2022-09-15 mark view->maxx = MAX(view->maxx, width);
9130 ec2a9698 2022-09-15 mark free(wline);
9131 ec2a9698 2022-09-15 mark wline = NULL;
9134 ec2a9698 2022-09-15 mark wattron(view->window, attr);
9135 ec2a9698 2022-09-15 mark if (s->first_displayed_line + nprinted == s->matched_line &&
9136 ec2a9698 2022-09-15 mark regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9137 ec2a9698 2022-09-15 mark err = add_matched_line(&width, line, view->ncols - 1, 0,
9138 ec2a9698 2022-09-15 mark view->window, view->x, regmatch);
9139 ec2a9698 2022-09-15 mark if (err) {
9140 ec2a9698 2022-09-15 mark free(line);
9141 ec2a9698 2022-09-15 mark return err;
9146 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, &skip, line,
9147 b637f33f 2023-02-01 mark view->x, view->ncols, 0, view->x ? 1 : 0);
9148 ec2a9698 2022-09-15 mark if (err) {
9149 ec2a9698 2022-09-15 mark free(line);
9150 ec2a9698 2022-09-15 mark return err;
9152 b637f33f 2023-02-01 mark waddwstr(view->window, &wline[skip]);
9153 ec2a9698 2022-09-15 mark free(wline);
9154 ec2a9698 2022-09-15 mark wline = NULL;
9156 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline) {
9157 ec2a9698 2022-09-15 mark while (width++ < view->ncols)
9158 ec2a9698 2022-09-15 mark waddch(view->window, ' ');
9160 b637f33f 2023-02-01 mark if (width < view->ncols)
9161 ec2a9698 2022-09-15 mark waddch(view->window, '\n');
9164 ec2a9698 2022-09-15 mark wattroff(view->window, attr);
9165 ec2a9698 2022-09-15 mark if (++nprinted == 1)
9166 ec2a9698 2022-09-15 mark s->first_displayed_line = s->lineno;
9168 ec2a9698 2022-09-15 mark free(line);
9169 ec2a9698 2022-09-15 mark if (nprinted > 0)
9170 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9172 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line;
9174 ec2a9698 2022-09-15 mark view_border(view);
9176 ec2a9698 2022-09-15 mark if (s->eof) {
9177 ec2a9698 2022-09-15 mark rc = waddnstr(view->window,
9178 ec2a9698 2022-09-15 mark "See the tog(1) manual page for full documentation",
9179 ec2a9698 2022-09-15 mark view->ncols - 1);
9180 ec2a9698 2022-09-15 mark if (rc == ERR)
9181 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9183 ec2a9698 2022-09-15 mark wmove(view->window, view->nlines - 1, 0);
9184 ec2a9698 2022-09-15 mark wclrtoeol(view->window);
9185 ec2a9698 2022-09-15 mark wstandout(view->window);
9186 ec2a9698 2022-09-15 mark rc = waddnstr(view->window, "scroll down for more...",
9187 ec2a9698 2022-09-15 mark view->ncols - 1);
9188 ec2a9698 2022-09-15 mark if (rc == ERR)
9189 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9190 ec2a9698 2022-09-15 mark if (getcurx(view->window) < view->ncols - 6) {
9191 ec2a9698 2022-09-15 mark rc = wprintw(view->window, "[%.0f%%]",
9192 ec2a9698 2022-09-15 mark 100.00 * s->last_displayed_line / s->nlines);
9193 ec2a9698 2022-09-15 mark if (rc == ERR)
9194 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "wprintw");
9196 ec2a9698 2022-09-15 mark wstandend(view->window);
9199 ec2a9698 2022-09-15 mark return NULL;
9202 ec2a9698 2022-09-15 mark static const struct got_error *
9203 ec2a9698 2022-09-15 mark input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9205 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9206 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
9207 ec2a9698 2022-09-15 mark char *line = NULL;
9208 ec2a9698 2022-09-15 mark ssize_t linelen;
9209 ec2a9698 2022-09-15 mark size_t linesz = 0;
9210 ec2a9698 2022-09-15 mark int eos, nscroll;
9212 ec2a9698 2022-09-15 mark eos = nscroll = view->nlines;
9213 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
9214 ec2a9698 2022-09-15 mark --eos; /* border */
9216 ec2a9698 2022-09-15 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
9218 ec2a9698 2022-09-15 mark switch (ch) {
9221 ec2a9698 2022-09-15 mark case KEY_RIGHT:
9223 ec2a9698 2022-09-15 mark case KEY_LEFT:
9225 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
9228 ec2a9698 2022-09-15 mark case KEY_HOME:
9229 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9230 ec2a9698 2022-09-15 mark view->count = 0;
9233 ec2a9698 2022-09-15 mark case KEY_END:
9234 ec2a9698 2022-09-15 mark view->count = 0;
9235 ec2a9698 2022-09-15 mark if (s->eof)
9237 ec2a9698 2022-09-15 mark s->first_displayed_line = (s->nlines - eos) + 3;
9238 ec2a9698 2022-09-15 mark s->eof = 1;
9241 ec2a9698 2022-09-15 mark case KEY_UP:
9242 ec2a9698 2022-09-15 mark if (s->first_displayed_line > 1)
9243 ec2a9698 2022-09-15 mark --s->first_displayed_line;
9245 ec2a9698 2022-09-15 mark view->count = 0;
9247 ec2a9698 2022-09-15 mark case CTRL('u'):
9249 ec2a9698 2022-09-15 mark nscroll /= 2;
9250 ec2a9698 2022-09-15 mark /* FALL THROUGH */
9251 ec2a9698 2022-09-15 mark case KEY_PPAGE:
9252 ec2a9698 2022-09-15 mark case CTRL('b'):
9254 ec2a9698 2022-09-15 mark if (s->first_displayed_line == 1) {
9255 ec2a9698 2022-09-15 mark view->count = 0;
9258 ec2a9698 2022-09-15 mark while (--nscroll > 0 && s->first_displayed_line > 1)
9259 ec2a9698 2022-09-15 mark s->first_displayed_line--;
9262 ec2a9698 2022-09-15 mark case KEY_DOWN:
9263 ec2a9698 2022-09-15 mark case CTRL('n'):
9264 ec2a9698 2022-09-15 mark if (!s->eof)
9265 ec2a9698 2022-09-15 mark ++s->first_displayed_line;
9267 ec2a9698 2022-09-15 mark view->count = 0;
9269 ec2a9698 2022-09-15 mark case CTRL('d'):
9271 ec2a9698 2022-09-15 mark nscroll /= 2;
9272 ec2a9698 2022-09-15 mark /* FALL THROUGH */
9273 ec2a9698 2022-09-15 mark case KEY_NPAGE:
9274 ec2a9698 2022-09-15 mark case CTRL('f'):
9277 ec2a9698 2022-09-15 mark if (s->eof) {
9278 ec2a9698 2022-09-15 mark view->count = 0;
9281 ec2a9698 2022-09-15 mark while (!s->eof && --nscroll > 0) {
9282 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
9283 ec2a9698 2022-09-15 mark s->first_displayed_line++;
9284 ec2a9698 2022-09-15 mark if (linelen == -1) {
9285 ec2a9698 2022-09-15 mark if (feof(s->f))
9286 ec2a9698 2022-09-15 mark s->eof = 1;
9288 ec2a9698 2022-09-15 mark err = got_ferror(s->f, GOT_ERR_IO);
9292 ec2a9698 2022-09-15 mark free(line);
9295 ec2a9698 2022-09-15 mark view->count = 0;
9299 ec2a9698 2022-09-15 mark return err;
9302 ec2a9698 2022-09-15 mark static const struct got_error *
9303 ec2a9698 2022-09-15 mark close_help_view(struct tog_view *view)
9305 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9307 ec2a9698 2022-09-15 mark free(s->line_offsets);
9308 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
9309 ec2a9698 2022-09-15 mark if (fclose(s->f) == EOF)
9310 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
9312 ec2a9698 2022-09-15 mark return NULL;
9315 ec2a9698 2022-09-15 mark static const struct got_error *
9316 ec2a9698 2022-09-15 mark reset_help_view(struct tog_view *view)
9318 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9321 ec2a9698 2022-09-15 mark if (s->f && fclose(s->f) == EOF)
9322 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
9324 ec2a9698 2022-09-15 mark wclear(view->window);
9325 ec2a9698 2022-09-15 mark view->count = 0;
9326 ec2a9698 2022-09-15 mark view->x = 0;
9327 ec2a9698 2022-09-15 mark s->all = !s->all;
9328 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9329 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9330 ec2a9698 2022-09-15 mark s->matched_line = 0;
9332 ec2a9698 2022-09-15 mark return create_help(s);
9335 ec2a9698 2022-09-15 mark static const struct got_error *
9336 ec2a9698 2022-09-15 mark open_help_view(struct tog_view *view, struct tog_view *parent)
9338 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
9339 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9341 ec2a9698 2022-09-15 mark s->type = (enum tog_keymap_type)parent->type;
9342 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9343 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9344 ec2a9698 2022-09-15 mark s->selected_line = 1;
9346 ec2a9698 2022-09-15 mark view->show = show_help_view;
9347 ec2a9698 2022-09-15 mark view->input = input_help_view;
9348 ec2a9698 2022-09-15 mark view->reset = reset_help_view;
9349 ec2a9698 2022-09-15 mark view->close = close_help_view;
9350 ec2a9698 2022-09-15 mark view->search_start = search_start_help_view;
9351 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_help_view;
9352 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
9354 ec2a9698 2022-09-15 mark err = create_help(s);
9355 ec2a9698 2022-09-15 mark return err;
9358 ec2a9698 2022-09-15 mark static const struct got_error *
9359 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9360 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
9362 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
9364 136e2bd4 2022-07-23 mark *new_view = NULL;
9366 136e2bd4 2022-07-23 mark switch (request) {
9367 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
9368 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
9369 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
9371 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
9372 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
9373 136e2bd4 2022-07-23 mark view, s->repo);
9375 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9376 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9378 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
9379 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
9380 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
9382 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
9383 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
9386 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9387 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9389 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
9390 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
9391 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
9392 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
9393 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9394 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
9395 136e2bd4 2022-07-23 mark &view->state.tree);
9396 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9397 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
9398 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9399 136e2bd4 2022-07-23 mark view->state.ref.repo);
9401 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9402 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9404 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
9405 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9406 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
9407 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
9408 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
9409 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
9410 136e2bd4 2022-07-23 mark view->state.log.repo);
9411 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9412 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
9413 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9414 136e2bd4 2022-07-23 mark view->state.ref.repo);
9416 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9417 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9419 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
9420 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9421 136e2bd4 2022-07-23 mark if (*new_view == NULL)
9422 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
9423 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9424 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
9425 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9426 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
9428 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
9429 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9431 136e2bd4 2022-07-23 mark view_close(*new_view);
9433 ec2a9698 2022-09-15 mark case TOG_VIEW_HELP:
9434 94274a8f 2022-09-19 mark *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9435 ec2a9698 2022-09-15 mark if (*new_view == NULL)
9436 ec2a9698 2022-09-15 mark return got_error_from_errno("view_open");
9437 ec2a9698 2022-09-15 mark err = open_help_view(*new_view, view);
9439 ec2a9698 2022-09-15 mark view_close(*new_view);
9442 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9445 136e2bd4 2022-07-23 mark return err;
9449 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
9450 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
9452 6458efa5 2020-11-24 stsp static void
9453 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
9455 9b058f45 2022-06-30 mark switch (view->type) {
9456 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9457 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9458 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
9459 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
9463 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
9464 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
9466 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
9467 9b058f45 2022-06-30 mark s->selected_line += view->offset;
9470 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
9471 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
9472 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
9474 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
9475 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
9476 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
9478 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
9479 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
9480 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
9486 9b058f45 2022-06-30 mark view->offset = 0;
9490 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
9491 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
9493 9b058f45 2022-06-30 mark static const struct got_error *
9494 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
9496 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
9497 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
9498 9b058f45 2022-06-30 mark int *selected = NULL;
9499 9b058f45 2022-06-30 mark int header, offset;
9501 9b058f45 2022-06-30 mark switch (view->type) {
9502 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9503 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9504 9b058f45 2022-06-30 mark header = 3;
9505 9b058f45 2022-06-30 mark scrolld = NULL;
9506 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
9507 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
9508 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
9509 9b058f45 2022-06-30 mark s->selected_line -= offset;
9510 9b058f45 2022-06-30 mark view->offset = offset;
9514 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
9515 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
9516 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
9517 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
9518 9b058f45 2022-06-30 mark selected = &s->selected;
9521 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
9522 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
9523 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
9524 9b058f45 2022-06-30 mark header = 3;
9525 9b058f45 2022-06-30 mark selected = &s->selected;
9528 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
9529 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
9530 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
9531 9b058f45 2022-06-30 mark header = 5;
9532 9b058f45 2022-06-30 mark selected = &s->selected;
9536 9b058f45 2022-06-30 mark selected = NULL;
9537 9b058f45 2022-06-30 mark scrolld = NULL;
9538 9b058f45 2022-06-30 mark header = 0;
9542 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
9543 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
9544 9b058f45 2022-06-30 mark view->offset = offset;
9545 9b058f45 2022-06-30 mark if (scrolld && offset) {
9546 9b058f45 2022-06-30 mark err = scrolld(view, offset);
9547 9b058f45 2022-06-30 mark *selected -= offset;
9551 9b058f45 2022-06-30 mark return err;
9554 9b058f45 2022-06-30 mark static void
9555 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9559 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9560 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9561 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
9562 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9564 6879ba42 2020-10-01 naddy fputc('\n', fp);
9567 4ed7e80c 2018-05-20 stsp __dead static void
9568 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9570 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9572 6a0a1bd4 2022-10-04 op fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9573 6879ba42 2020-10-01 naddy getprogname());
9574 6879ba42 2020-10-01 naddy if (hflag) {
9575 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9576 6879ba42 2020-10-01 naddy list_commands(fp);
9578 6879ba42 2020-10-01 naddy exit(status);
9581 c2301be8 2018-04-30 stsp static char **
9582 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9584 ee85c5e8 2020-02-29 stsp va_list ap;
9585 c2301be8 2018-04-30 stsp char **argv;
9588 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9590 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9591 c2301be8 2018-04-30 stsp if (argv == NULL)
9592 c2301be8 2018-04-30 stsp err(1, "calloc");
9593 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9594 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9595 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9596 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9599 ee85c5e8 2020-02-29 stsp va_end(ap);
9600 c2301be8 2018-04-30 stsp return argv;
9604 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9605 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9606 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9607 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9608 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9610 ee85c5e8 2020-02-29 stsp static const struct got_error *
9611 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9613 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9614 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9615 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9616 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9617 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9618 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
9619 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9620 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9621 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
9623 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9624 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9625 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9627 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
9628 0ae84acc 2022-06-15 tracey if (error != NULL)
9629 0ae84acc 2022-06-15 tracey goto done;
9631 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9632 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9633 ee85c5e8 2020-02-29 stsp goto done;
9635 ee85c5e8 2020-02-29 stsp if (worktree)
9636 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9638 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9639 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9640 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9641 ee85c5e8 2020-02-29 stsp goto done;
9644 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9645 ee85c5e8 2020-02-29 stsp if (error != NULL)
9646 ee85c5e8 2020-02-29 stsp goto done;
9648 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9649 ee85c5e8 2020-02-29 stsp repo, worktree);
9650 ee85c5e8 2020-02-29 stsp if (error)
9651 ee85c5e8 2020-02-29 stsp goto done;
9653 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
9654 84de9106 2020-12-26 stsp if (error)
9655 84de9106 2020-12-26 stsp goto done;
9656 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9657 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9658 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9659 ee85c5e8 2020-02-29 stsp if (error)
9660 ee85c5e8 2020-02-29 stsp goto done;
9662 ee85c5e8 2020-02-29 stsp if (worktree) {
9663 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9664 ee85c5e8 2020-02-29 stsp worktree = NULL;
9667 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9668 a44927cc 2022-04-07 stsp if (error)
9669 a44927cc 2022-04-07 stsp goto done;
9671 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9672 ee85c5e8 2020-02-29 stsp if (error) {
9673 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9674 ee85c5e8 2020-02-29 stsp goto done;
9675 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9676 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9677 6879ba42 2020-10-01 naddy usage(1, 1);
9678 ee85c5e8 2020-02-29 stsp /* not reached */
9681 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9682 ee85c5e8 2020-02-29 stsp if (error)
9683 ee85c5e8 2020-02-29 stsp goto done;
9685 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9687 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9688 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9690 1d0f4054 2021-06-17 stsp if (repo) {
9691 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9692 1d0f4054 2021-06-17 stsp if (error == NULL)
9693 1d0f4054 2021-06-17 stsp error = close_err;
9695 a44927cc 2022-04-07 stsp if (commit)
9696 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
9697 ee85c5e8 2020-02-29 stsp if (worktree)
9698 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9699 0ae84acc 2022-06-15 tracey if (pack_fds) {
9700 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
9701 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
9702 0ae84acc 2022-06-15 tracey if (error == NULL)
9703 0ae84acc 2022-06-15 tracey error = pack_err;
9706 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9707 ee85c5e8 2020-02-29 stsp free(commit_id);
9708 ee85c5e8 2020-02-29 stsp free(cwd);
9709 ee85c5e8 2020-02-29 stsp free(repo_path);
9710 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9711 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9713 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9714 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9715 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9717 87670572 2020-12-26 naddy tog_free_refs();
9718 ee85c5e8 2020-02-29 stsp return error;
9722 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9724 7483826e 2023-04-14 mark const struct got_error *io_err, *error = NULL;
9725 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9726 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9727 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9728 3e166534 2022-02-16 naddy static const struct option longopts[] = {
9729 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9730 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9732 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
9733 098596c5 2023-04-14 stsp const char *test_script_path;
9735 1fa0d17d 2023-02-13 op setlocale(LC_CTYPE, "");
9738 098596c5 2023-04-14 stsp * Test mode init must happen before pledge() because "tty" will
9739 098596c5 2023-04-14 stsp * not allow TTY-related ioctls to occur via regular files.
9741 098596c5 2023-04-14 stsp test_script_path = getenv("GOT_TOG_TEST");
9742 098596c5 2023-04-14 stsp if (test_script_path != NULL) {
9743 098596c5 2023-04-14 stsp error = init_mock_term(test_script_path);
9744 098596c5 2023-04-14 stsp if (error) {
9745 098596c5 2023-04-14 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9750 098596c5 2023-04-14 stsp #if !defined(PROFILE)
9751 1fa0d17d 2023-02-13 op if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9752 1fa0d17d 2023-02-13 op NULL) == -1)
9753 1fa0d17d 2023-02-13 op err(1, "pledge");
9756 3264c09c 2022-09-06 mark if (!isatty(STDIN_FILENO))
9757 3264c09c 2022-09-06 mark errx(1, "standard input is not a tty");
9759 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9760 9f7d7167 2018-04-29 stsp switch (ch) {
9762 9f7d7167 2018-04-29 stsp hflag = 1;
9765 53ccebc2 2019-07-30 stsp Vflag = 1;
9768 6879ba42 2020-10-01 naddy usage(hflag, 1);
9769 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9773 9f7d7167 2018-04-29 stsp argc -= optind;
9774 9f7d7167 2018-04-29 stsp argv += optind;
9775 9814e6a3 2020-09-27 naddy optind = 1;
9776 c2301be8 2018-04-30 stsp optreset = 1;
9778 53ccebc2 2019-07-30 stsp if (Vflag) {
9779 53ccebc2 2019-07-30 stsp got_version_print_str();
9780 6879ba42 2020-10-01 naddy return 0;
9783 c2301be8 2018-04-30 stsp if (argc == 0) {
9784 f29d3e89 2018-06-23 stsp if (hflag)
9785 6879ba42 2020-10-01 naddy usage(hflag, 0);
9786 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9787 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9789 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9793 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9794 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9795 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9796 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9797 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9803 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9804 917d79a7 2022-07-01 stsp if (diff_algo_str) {
9805 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
9806 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9807 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
9808 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9811 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9812 ee85c5e8 2020-02-29 stsp if (argc != 1)
9813 6879ba42 2020-10-01 naddy usage(0, 1);
9814 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9815 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9817 ee85c5e8 2020-02-29 stsp if (hflag)
9818 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9820 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9823 7483826e 2023-04-14 mark if (using_mock_io) {
9824 7483826e 2023-04-14 mark io_err = tog_io_close();
9825 7483826e 2023-04-14 mark if (error == NULL)
9826 7483826e 2023-04-14 mark error = io_err;
9829 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9831 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9832 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9833 a2f4a359 2020-02-28 stsp free(cmd_argv);
9836 e45c294c 2022-10-24 stsp if (error && error->code != GOT_ERR_CANCELLED &&
9837 e45c294c 2022-10-24 stsp error->code != GOT_ERR_EOF &&
9838 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
9839 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_PIPE &&
9840 e45c294c 2022-10-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9841 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);