Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
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.
7 9f7d7167 2018-04-29 stsp *
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.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
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>
20 80ddbec8 2018-04-29 stsp
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 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
42 3da8ef85 2021-09-21 stsp #include <sched.h>
43 9f7d7167 2018-04-29 stsp
44 53ccebc2 2019-07-30 stsp #include "got_version.h"
45 9f7d7167 2018-04-29 stsp #include "got_error.h"
46 80ddbec8 2018-04-29 stsp #include "got_object.h"
47 80ddbec8 2018-04-29 stsp #include "got_reference.h"
48 80ddbec8 2018-04-29 stsp #include "got_repository.h"
49 80ddbec8 2018-04-29 stsp #include "got_diff.h"
50 511a516b 2018-05-19 stsp #include "got_opentemp.h"
51 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
52 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
53 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
54 a70480e0 2018-06-23 stsp #include "got_blame.h"
55 c2db6724 2019-01-04 stsp #include "got_privsep.h"
56 1dd54920 2019-05-11 stsp #include "got_path.h"
57 b7165be3 2019-02-05 stsp #include "got_worktree.h"
58 9f7d7167 2018-04-29 stsp
59 881b2d3e 2018-04-30 stsp #ifndef MIN
60 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 881b2d3e 2018-04-30 stsp #endif
62 881b2d3e 2018-04-30 stsp
63 2bd27830 2018-10-22 stsp #ifndef MAX
64 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 2bd27830 2018-10-22 stsp #endif
66 2bd27830 2018-10-22 stsp
67 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
68 2bd27830 2018-10-22 stsp
69 9f7d7167 2018-04-29 stsp #ifndef nitems
70 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 9f7d7167 2018-04-29 stsp #endif
72 9f7d7167 2018-04-29 stsp
73 9f7d7167 2018-04-29 stsp struct tog_cmd {
74 c2301be8 2018-04-30 stsp const char *name;
75 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
77 9f7d7167 2018-04-29 stsp };
78 9f7d7167 2018-04-29 stsp
79 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
83 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
84 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
85 9f7d7167 2018-04-29 stsp
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
89 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
90 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
91 9f7d7167 2018-04-29 stsp
92 3e166534 2022-02-16 naddy static const struct tog_cmd tog_commands[] = {
93 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
94 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
95 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
96 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
97 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
98 9f7d7167 2018-04-29 stsp };
99 9f7d7167 2018-04-29 stsp
100 d6b05b5b 2018-08-04 stsp enum tog_view_type {
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
103 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
104 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
105 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
106 ec2a9698 2022-09-15 mark TOG_VIEW_HELP
107 ec2a9698 2022-09-15 mark };
108 ec2a9698 2022-09-15 mark
109 ec2a9698 2022-09-15 mark /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
110 ec2a9698 2022-09-15 mark enum tog_keymap_type {
111 ec2a9698 2022-09-15 mark TOG_KEYMAP_KEYS = -2,
112 ec2a9698 2022-09-15 mark TOG_KEYMAP_GLOBAL,
113 ec2a9698 2022-09-15 mark TOG_KEYMAP_DIFF,
114 ec2a9698 2022-09-15 mark TOG_KEYMAP_LOG,
115 ec2a9698 2022-09-15 mark TOG_KEYMAP_BLAME,
116 ec2a9698 2022-09-15 mark TOG_KEYMAP_TREE,
117 ec2a9698 2022-09-15 mark TOG_KEYMAP_REF,
118 ec2a9698 2022-09-15 mark TOG_KEYMAP_HELP
119 d6b05b5b 2018-08-04 stsp };
120 c3e9aa98 2019-05-13 jcs
121 9b058f45 2022-06-30 mark enum tog_view_mode {
122 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_NONE,
123 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_VERT,
124 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_HRZN
125 9b058f45 2022-06-30 mark };
126 9b058f45 2022-06-30 mark
127 9b058f45 2022-06-30 mark #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
128 9b058f45 2022-06-30 mark
129 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
130 d6b05b5b 2018-08-04 stsp
131 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
132 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
133 ba4f502b 2018-08-04 stsp struct got_object_id *id;
134 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
135 1a76625f 2018-10-22 stsp int idx;
136 ba4f502b 2018-08-04 stsp };
137 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
138 ba4f502b 2018-08-04 stsp struct commit_queue {
139 ba4f502b 2018-08-04 stsp int ncommits;
140 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
141 6d17833f 2019-11-08 stsp };
142 6d17833f 2019-11-08 stsp
143 f26dddb7 2019-11-08 stsp struct tog_color {
144 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
145 6d17833f 2019-11-08 stsp regex_t regex;
146 6d17833f 2019-11-08 stsp short colorpair;
147 15a087fe 2019-02-21 stsp };
148 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
149 11b20872 2019-11-08 stsp
150 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
151 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
152 917d79a7 2022-07-01 stsp static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
153 cc488aa7 2022-01-23 stsp
154 cc488aa7 2022-01-23 stsp static const struct got_error *
155 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
156 cc488aa7 2022-01-23 stsp struct got_reference* re2)
157 cc488aa7 2022-01-23 stsp {
158 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
159 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
160 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
161 cc488aa7 2022-01-23 stsp
162 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
163 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
164 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
165 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
166 cc488aa7 2022-01-23 stsp *cmp = -1;
167 cc488aa7 2022-01-23 stsp return NULL;
168 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
169 cc488aa7 2022-01-23 stsp *cmp = 1;
170 cc488aa7 2022-01-23 stsp return NULL;
171 cc488aa7 2022-01-23 stsp }
172 cc488aa7 2022-01-23 stsp
173 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
174 cc488aa7 2022-01-23 stsp return NULL;
175 cc488aa7 2022-01-23 stsp }
176 51a10b52 2020-12-26 stsp
177 11b20872 2019-11-08 stsp static const struct got_error *
178 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
179 51a10b52 2020-12-26 stsp {
180 51a10b52 2020-12-26 stsp const struct got_error *err;
181 51a10b52 2020-12-26 stsp
182 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
183 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
184 7f66531d 2021-11-16 stsp repo);
185 51a10b52 2020-12-26 stsp if (err)
186 51a10b52 2020-12-26 stsp return err;
187 51a10b52 2020-12-26 stsp
188 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
189 51a10b52 2020-12-26 stsp repo);
190 51a10b52 2020-12-26 stsp }
191 51a10b52 2020-12-26 stsp
192 51a10b52 2020-12-26 stsp static void
193 51a10b52 2020-12-26 stsp tog_free_refs(void)
194 51a10b52 2020-12-26 stsp {
195 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
196 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
197 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
198 51a10b52 2020-12-26 stsp }
199 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
200 51a10b52 2020-12-26 stsp }
201 51a10b52 2020-12-26 stsp
202 51a10b52 2020-12-26 stsp static const struct got_error *
203 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
204 11b20872 2019-11-08 stsp int idx, short color)
205 11b20872 2019-11-08 stsp {
206 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
207 11b20872 2019-11-08 stsp struct tog_color *tc;
208 11b20872 2019-11-08 stsp int regerr = 0;
209 11b20872 2019-11-08 stsp
210 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
211 11b20872 2019-11-08 stsp return NULL;
212 11b20872 2019-11-08 stsp
213 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
214 11b20872 2019-11-08 stsp
215 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
216 11b20872 2019-11-08 stsp if (tc == NULL)
217 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
218 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
219 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
220 11b20872 2019-11-08 stsp if (regerr) {
221 11b20872 2019-11-08 stsp static char regerr_msg[512];
222 11b20872 2019-11-08 stsp static char err_msg[512];
223 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
224 11b20872 2019-11-08 stsp sizeof(regerr_msg));
225 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
226 11b20872 2019-11-08 stsp regerr_msg);
227 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
228 11b20872 2019-11-08 stsp free(tc);
229 11b20872 2019-11-08 stsp return err;
230 11b20872 2019-11-08 stsp }
231 11b20872 2019-11-08 stsp tc->colorpair = idx;
232 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
233 11b20872 2019-11-08 stsp return NULL;
234 11b20872 2019-11-08 stsp }
235 11b20872 2019-11-08 stsp
236 11b20872 2019-11-08 stsp static void
237 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
238 11b20872 2019-11-08 stsp {
239 11b20872 2019-11-08 stsp struct tog_color *tc;
240 11b20872 2019-11-08 stsp
241 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
242 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
243 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
244 11b20872 2019-11-08 stsp regfree(&tc->regex);
245 11b20872 2019-11-08 stsp free(tc);
246 11b20872 2019-11-08 stsp }
247 11b20872 2019-11-08 stsp }
248 11b20872 2019-11-08 stsp
249 336075a4 2022-06-25 op static struct tog_color *
250 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
251 11b20872 2019-11-08 stsp {
252 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
253 11b20872 2019-11-08 stsp
254 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
255 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
256 11b20872 2019-11-08 stsp return tc;
257 11b20872 2019-11-08 stsp }
258 11b20872 2019-11-08 stsp
259 11b20872 2019-11-08 stsp return NULL;
260 11b20872 2019-11-08 stsp }
261 11b20872 2019-11-08 stsp
262 11b20872 2019-11-08 stsp static int
263 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
264 11b20872 2019-11-08 stsp {
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
266 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
268 11b20872 2019-11-08 stsp return COLOR_CYAN;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
270 11b20872 2019-11-08 stsp return COLOR_YELLOW;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
272 11b20872 2019-11-08 stsp return COLOR_GREEN;
273 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
274 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
275 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
276 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
277 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
278 91b8c405 2020-01-25 stsp return COLOR_CYAN;
279 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
280 11b20872 2019-11-08 stsp return COLOR_GREEN;
281 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
282 11b20872 2019-11-08 stsp return COLOR_GREEN;
283 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
284 11b20872 2019-11-08 stsp return COLOR_CYAN;
285 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
286 11b20872 2019-11-08 stsp return COLOR_YELLOW;
287 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
288 6458efa5 2020-11-24 stsp return COLOR_GREEN;
289 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
290 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
291 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
292 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
293 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
294 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
295 11b20872 2019-11-08 stsp
296 11b20872 2019-11-08 stsp return -1;
297 11b20872 2019-11-08 stsp }
298 11b20872 2019-11-08 stsp
299 11b20872 2019-11-08 stsp static int
300 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
301 11b20872 2019-11-08 stsp {
302 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
303 11b20872 2019-11-08 stsp
304 11b20872 2019-11-08 stsp if (val == NULL)
305 11b20872 2019-11-08 stsp return default_color_value(envvar);
306 15a087fe 2019-02-21 stsp
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
308 11b20872 2019-11-08 stsp return COLOR_BLACK;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
310 11b20872 2019-11-08 stsp return COLOR_RED;
311 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
312 11b20872 2019-11-08 stsp return COLOR_GREEN;
313 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
314 11b20872 2019-11-08 stsp return COLOR_YELLOW;
315 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
316 11b20872 2019-11-08 stsp return COLOR_BLUE;
317 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
318 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
319 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
320 11b20872 2019-11-08 stsp return COLOR_CYAN;
321 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
322 11b20872 2019-11-08 stsp return COLOR_WHITE;
323 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
324 11b20872 2019-11-08 stsp return -1;
325 11b20872 2019-11-08 stsp
326 11b20872 2019-11-08 stsp return default_color_value(envvar);
327 11b20872 2019-11-08 stsp }
328 11b20872 2019-11-08 stsp
329 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
330 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
331 3dbaef42 2020-11-24 stsp const char *label1, *label2;
332 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
333 f9d37699 2022-06-28 stsp int fd1, fd2;
334 c7d5c43c 2022-08-04 mark int lineno;
335 15a087fe 2019-02-21 stsp int first_displayed_line;
336 15a087fe 2019-02-21 stsp int last_displayed_line;
337 15a087fe 2019-02-21 stsp int eof;
338 15a087fe 2019-02-21 stsp int diff_context;
339 3dbaef42 2020-11-24 stsp int ignore_whitespace;
340 64453f7e 2020-11-21 stsp int force_text_diff;
341 15a087fe 2019-02-21 stsp struct got_repository *repo;
342 c7d5c43c 2022-08-04 mark struct got_diff_line *lines;
343 fe621944 2020-11-10 stsp size_t nlines;
344 f44b1f58 2020-02-02 tracey int matched_line;
345 f44b1f58 2020-02-02 tracey int selected_line;
346 15a087fe 2019-02-21 stsp
347 c0f61fa4 2022-07-11 mark /* passed from log or blame view; may be NULL */
348 c0f61fa4 2022-07-11 mark struct tog_view *parent_view;
349 b01e7d3b 2018-08-04 stsp };
350 b01e7d3b 2018-08-04 stsp
351 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
352 5629093a 2022-07-11 stsp static volatile sig_atomic_t tog_thread_error;
353 1a76625f 2018-10-22 stsp
354 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
355 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
356 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
357 1a76625f 2018-10-22 stsp int commits_needed;
358 fb280deb 2021-08-30 stsp int load_all;
359 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
360 568eae95 2022-09-11 mark struct commit_queue *real_commits;
361 1a76625f 2018-10-22 stsp const char *in_repo_path;
362 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
363 1a76625f 2018-10-22 stsp struct got_repository *repo;
364 74467cc8 2022-06-15 stsp int *pack_fds;
365 1a76625f 2018-10-22 stsp int log_complete;
366 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
367 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
368 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
369 13add988 2019-10-15 stsp int *searching;
370 13add988 2019-10-15 stsp int *search_next_done;
371 13add988 2019-10-15 stsp regex_t *regex;
372 568eae95 2022-09-11 mark int *limiting;
373 568eae95 2022-09-11 mark int limit_match;
374 568eae95 2022-09-11 mark regex_t *limit_regex;
375 568eae95 2022-09-11 mark struct commit_queue *limit_commits;
376 1a76625f 2018-10-22 stsp };
377 1a76625f 2018-10-22 stsp
378 1a76625f 2018-10-22 stsp struct tog_log_view_state {
379 568eae95 2022-09-11 mark struct commit_queue *commits;
380 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
381 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
382 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
383 568eae95 2022-09-11 mark struct commit_queue real_commits;
384 b01e7d3b 2018-08-04 stsp int selected;
385 b01e7d3b 2018-08-04 stsp char *in_repo_path;
386 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
387 b672a97a 2020-01-27 stsp int log_branches;
388 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
389 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
390 1a76625f 2018-10-22 stsp sig_atomic_t quit;
391 1a76625f 2018-10-22 stsp pthread_t thread;
392 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
393 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
394 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
395 11b20872 2019-11-08 stsp struct tog_colors colors;
396 10aab77f 2022-07-19 op int use_committer;
397 568eae95 2022-09-11 mark int limit_view;
398 568eae95 2022-09-11 mark regex_t limit_regex;
399 568eae95 2022-09-11 mark struct commit_queue limit_commits;
400 ba4f502b 2018-08-04 stsp };
401 11b20872 2019-11-08 stsp
402 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
403 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
404 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
405 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
406 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
407 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
408 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
409 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
410 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
411 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
412 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
413 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
414 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
415 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
416 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
417 ba4f502b 2018-08-04 stsp
418 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
419 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
420 e9424729 2018-08-04 stsp int nlines;
421 e9424729 2018-08-04 stsp
422 e9424729 2018-08-04 stsp struct tog_view *view;
423 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
424 e9424729 2018-08-04 stsp int *quit;
425 e9424729 2018-08-04 stsp };
426 e9424729 2018-08-04 stsp
427 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
428 e9424729 2018-08-04 stsp const char *path;
429 e9424729 2018-08-04 stsp struct got_repository *repo;
430 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
431 e9424729 2018-08-04 stsp int *complete;
432 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
433 fc06ba56 2019-08-22 stsp void *cancel_arg;
434 e9424729 2018-08-04 stsp };
435 e9424729 2018-08-04 stsp
436 e9424729 2018-08-04 stsp struct tog_blame {
437 e9424729 2018-08-04 stsp FILE *f;
438 be659d10 2020-11-18 stsp off_t filesize;
439 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
440 6fcac457 2018-11-19 stsp int nlines;
441 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
442 e9424729 2018-08-04 stsp pthread_t thread;
443 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
444 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
445 e9424729 2018-08-04 stsp const char *path;
446 0ae84acc 2022-06-15 tracey int *pack_fds;
447 e9424729 2018-08-04 stsp };
448 e9424729 2018-08-04 stsp
449 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
450 7cbe629d 2018-08-04 stsp int first_displayed_line;
451 7cbe629d 2018-08-04 stsp int last_displayed_line;
452 7cbe629d 2018-08-04 stsp int selected_line;
453 c0f61fa4 2022-07-11 mark int last_diffed_line;
454 7cbe629d 2018-08-04 stsp int blame_complete;
455 e5a0f69f 2018-08-18 stsp int eof;
456 e5a0f69f 2018-08-18 stsp int done;
457 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
458 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
459 e5a0f69f 2018-08-18 stsp char *path;
460 7cbe629d 2018-08-04 stsp struct got_repository *repo;
461 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
462 136e2bd4 2022-07-23 mark struct got_object_id *id_to_log;
463 e9424729 2018-08-04 stsp struct tog_blame blame;
464 6c4c42e0 2019-06-24 stsp int matched_line;
465 11b20872 2019-11-08 stsp struct tog_colors colors;
466 ad80ab7b 2018-08-04 stsp };
467 ad80ab7b 2018-08-04 stsp
468 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
469 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
470 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
471 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
472 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
473 ad80ab7b 2018-08-04 stsp int selected;
474 ad80ab7b 2018-08-04 stsp };
475 ad80ab7b 2018-08-04 stsp
476 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
477 ad80ab7b 2018-08-04 stsp
478 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
479 ad80ab7b 2018-08-04 stsp char *tree_label;
480 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
481 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
482 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
483 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
484 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
485 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
486 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
487 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
488 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
489 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
490 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
491 6458efa5 2020-11-24 stsp struct tog_colors colors;
492 6458efa5 2020-11-24 stsp };
493 6458efa5 2020-11-24 stsp
494 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
495 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
496 6458efa5 2020-11-24 stsp struct got_reference *ref;
497 6458efa5 2020-11-24 stsp int idx;
498 6458efa5 2020-11-24 stsp };
499 6458efa5 2020-11-24 stsp
500 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
501 6458efa5 2020-11-24 stsp
502 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
503 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
504 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
505 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
506 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
507 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
508 6458efa5 2020-11-24 stsp struct got_repository *repo;
509 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
510 bddb1296 2019-11-08 stsp struct tog_colors colors;
511 ec2a9698 2022-09-15 mark };
512 ec2a9698 2022-09-15 mark
513 ec2a9698 2022-09-15 mark struct tog_help_view_state {
514 ec2a9698 2022-09-15 mark FILE *f;
515 ec2a9698 2022-09-15 mark off_t *line_offsets;
516 ec2a9698 2022-09-15 mark size_t nlines;
517 ec2a9698 2022-09-15 mark int lineno;
518 ec2a9698 2022-09-15 mark int first_displayed_line;
519 ec2a9698 2022-09-15 mark int last_displayed_line;
520 ec2a9698 2022-09-15 mark int eof;
521 ec2a9698 2022-09-15 mark int matched_line;
522 ec2a9698 2022-09-15 mark int selected_line;
523 ec2a9698 2022-09-15 mark int all;
524 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
525 ec2a9698 2022-09-15 mark };
526 ec2a9698 2022-09-15 mark
527 ec2a9698 2022-09-15 mark #define GENERATE_HELP \
528 ec2a9698 2022-09-15 mark KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
529 ec2a9698 2022-09-15 mark KEY_("H F1", "Open view-specific help (double tap for all help)"), \
530 ec2a9698 2022-09-15 mark KEY_("k C-p Up", "Move cursor or page up one line"), \
531 ec2a9698 2022-09-15 mark KEY_("j C-n Down", "Move cursor or page down one line"), \
532 ec2a9698 2022-09-15 mark KEY_("C-b b PgUp", "Scroll the view up one page"), \
533 ec2a9698 2022-09-15 mark KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
534 ec2a9698 2022-09-15 mark KEY_("C-u u", "Scroll the view up one half page"), \
535 ec2a9698 2022-09-15 mark KEY_("C-d d", "Scroll the view down one half page"), \
536 ec2a9698 2022-09-15 mark KEY_("g Home", "Go to line N (default: first line)"), \
537 ec2a9698 2022-09-15 mark KEY_("G End", "Go to line N (default: last line)"), \
538 ec2a9698 2022-09-15 mark KEY_("l Right", "Scroll the view right"), \
539 ec2a9698 2022-09-15 mark KEY_("h Left", "Scroll the view left"), \
540 ec2a9698 2022-09-15 mark KEY_("$", "Scroll view to the rightmost position"), \
541 ec2a9698 2022-09-15 mark KEY_("0", "Scroll view to the leftmost position"), \
542 ec2a9698 2022-09-15 mark KEY_("-", "Decrease size of the focussed split"), \
543 ec2a9698 2022-09-15 mark KEY_("+", "Increase size of the focussed split"), \
544 ec2a9698 2022-09-15 mark KEY_("Tab", "Switch focus between views"), \
545 ec2a9698 2022-09-15 mark KEY_("F", "Toggle fullscreen mode"), \
546 ec2a9698 2022-09-15 mark KEY_("/", "Open prompt to enter search term"), \
547 ec2a9698 2022-09-15 mark KEY_("n", "Find next line/token matching the current search term"), \
548 ec2a9698 2022-09-15 mark KEY_("N", "Find previous line/token matching the current search term"),\
549 16a2d4f0 2022-09-19 stsp KEY_("q", "Quit the focussed view; Quit help screen"), \
550 ec2a9698 2022-09-15 mark KEY_("Q", "Quit tog"), \
551 ec2a9698 2022-09-15 mark \
552 fd6badaa 2022-09-23 stsp KEYMAP_("Log view", TOG_KEYMAP_LOG), \
553 ec2a9698 2022-09-15 mark KEY_("< ,", "Move cursor up one commit"), \
554 ec2a9698 2022-09-15 mark KEY_("> .", "Move cursor down one commit"), \
555 ec2a9698 2022-09-15 mark KEY_("Enter", "Open diff view of the selected commit"), \
556 ec2a9698 2022-09-15 mark KEY_("B", "Reload the log view and toggle display of merged commits"), \
557 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
558 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the repository from the selected" \
559 ec2a9698 2022-09-15 mark " commit"), \
560 ec2a9698 2022-09-15 mark KEY_("@", "Toggle between displaying author and committer name"), \
561 ec2a9698 2022-09-15 mark KEY_("&", "Open prompt to enter term to limit commits displayed"), \
562 ec2a9698 2022-09-15 mark KEY_("C-g Backspace", "Cancel current search or log operation"), \
563 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload the log view with new commits in the repository"), \
564 ec2a9698 2022-09-15 mark \
565 fd6badaa 2022-09-23 stsp KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
566 ec2a9698 2022-09-15 mark KEY_("K < ,", "Display diff of next line in the file/log entry"), \
567 ec2a9698 2022-09-15 mark KEY_("J > .", "Display diff of previous line in the file/log entry"), \
568 ec2a9698 2022-09-15 mark KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
569 ec2a9698 2022-09-15 mark KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
570 ec2a9698 2022-09-15 mark " data"), \
571 ec2a9698 2022-09-15 mark KEY_("(", "Go to the previous file in the diff"), \
572 ec2a9698 2022-09-15 mark KEY_(")", "Go to the next file in the diff"), \
573 ec2a9698 2022-09-15 mark KEY_("{", "Go to the previous hunk in the diff"), \
574 ec2a9698 2022-09-15 mark KEY_("}", "Go to the next hunk in the diff"), \
575 ec2a9698 2022-09-15 mark KEY_("[", "Decrease the number of context lines"), \
576 ec2a9698 2022-09-15 mark KEY_("]", "Increase the number of context lines"), \
577 ec2a9698 2022-09-15 mark KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
578 ec2a9698 2022-09-15 mark \
579 fd6badaa 2022-09-23 stsp KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
580 ec2a9698 2022-09-15 mark KEY_("Enter", "Display diff view of the selected line's commit"), \
581 ec2a9698 2022-09-15 mark KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
582 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the currently selected annotated line"), \
583 ec2a9698 2022-09-15 mark KEY_("C", "Reload view with the previously blamed commit"), \
584 ec2a9698 2022-09-15 mark KEY_("c", "Reload view with the version of the file found in the" \
585 ec2a9698 2022-09-15 mark " selected line's commit"), \
586 ec2a9698 2022-09-15 mark KEY_("p", "Reload view with the version of the file found in the" \
587 ec2a9698 2022-09-15 mark " selected line's parent commit"), \
588 ec2a9698 2022-09-15 mark \
589 fd6badaa 2022-09-23 stsp KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
590 ec2a9698 2022-09-15 mark KEY_("Enter", "Enter selected directory or open blame view of the" \
591 ec2a9698 2022-09-15 mark " selected file"), \
592 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the selected entry"), \
593 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
594 ec2a9698 2022-09-15 mark KEY_("i", "Show object IDs for all tree entries"), \
595 ec2a9698 2022-09-15 mark KEY_("Backspace", "Return to the parent directory"), \
596 ec2a9698 2022-09-15 mark \
597 fd6badaa 2022-09-23 stsp KEYMAP_("Ref view", TOG_KEYMAP_REF), \
598 ec2a9698 2022-09-15 mark KEY_("Enter", "Display log view of the selected reference"), \
599 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the selected reference"), \
600 ec2a9698 2022-09-15 mark KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
601 ec2a9698 2022-09-15 mark KEY_("m", "Toggle display of last modified date for each reference"), \
602 ec2a9698 2022-09-15 mark KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
603 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload view with all repository references")
604 ec2a9698 2022-09-15 mark
605 ec2a9698 2022-09-15 mark struct tog_key_map {
606 ec2a9698 2022-09-15 mark const char *keys;
607 ec2a9698 2022-09-15 mark const char *info;
608 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
609 7cbe629d 2018-08-04 stsp };
610 7cbe629d 2018-08-04 stsp
611 669b5ffa 2018-10-07 stsp /*
612 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
613 669b5ffa 2018-10-07 stsp *
614 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
615 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
616 669b5ffa 2018-10-07 stsp * there is enough screen estate.
617 669b5ffa 2018-10-07 stsp *
618 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
619 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
620 669b5ffa 2018-10-07 stsp *
621 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
622 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
623 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
624 669b5ffa 2018-10-07 stsp *
625 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
626 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
627 669b5ffa 2018-10-07 stsp */
628 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
629 669b5ffa 2018-10-07 stsp
630 cc3c9aac 2018-08-01 stsp struct tog_view {
631 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
632 26ed57b2 2018-05-19 stsp WINDOW *window;
633 26ed57b2 2018-05-19 stsp PANEL *panel;
634 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
635 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
636 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
637 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
638 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
639 94b80cfa 2022-08-01 mark int gline, hiline; /* navigate to and highlight this nG line */
640 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
641 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
642 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
643 9970f7fc 2020-12-03 stsp int dying;
644 669b5ffa 2018-10-07 stsp struct tog_view *parent;
645 669b5ffa 2018-10-07 stsp struct tog_view *child;
646 5dc9f4bc 2018-08-04 stsp
647 e78dc838 2020-12-04 stsp /*
648 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
649 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
650 e78dc838 2020-12-04 stsp * between parent and child.
651 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
652 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
653 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
654 e78dc838 2020-12-04 stsp * situations.
655 e78dc838 2020-12-04 stsp */
656 e78dc838 2020-12-04 stsp int focus_child;
657 e78dc838 2020-12-04 stsp
658 9b058f45 2022-06-30 mark enum tog_view_mode mode;
659 5dc9f4bc 2018-08-04 stsp /* type-specific state */
660 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
661 5dc9f4bc 2018-08-04 stsp union {
662 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
663 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
664 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
665 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
666 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
667 ec2a9698 2022-09-15 mark struct tog_help_view_state help;
668 5dc9f4bc 2018-08-04 stsp } state;
669 e5a0f69f 2018-08-18 stsp
670 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
671 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
672 e78dc838 2020-12-04 stsp struct tog_view *, int);
673 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
674 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
675 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
676 60493ae3 2019-06-20 stsp
677 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
678 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
679 eaf2c13e 2022-09-17 mark void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
680 eaf2c13e 2022-09-17 mark int **, int **, int **, int **);
681 c0c4acc8 2021-01-24 stsp int search_started;
682 60493ae3 2019-06-20 stsp int searching;
683 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
684 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
685 60493ae3 2019-06-20 stsp int search_next_done;
686 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
687 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
688 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
689 1803e47f 2019-06-22 stsp regex_t regex;
690 41605754 2020-11-12 stsp regmatch_t regmatch;
691 cc3c9aac 2018-08-01 stsp };
692 cd0acaa7 2018-05-20 stsp
693 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
694 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
695 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
696 78756c87 2020-11-24 stsp struct got_repository *);
697 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
698 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
699 e78dc838 2020-12-04 stsp struct tog_view *, int);
700 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
701 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
702 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
703 eaf2c13e 2022-09-17 mark static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
704 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
705 ec2a9698 2022-09-15 mark static const struct got_error *search_next_view_match(struct tog_view *);
706 e5a0f69f 2018-08-18 stsp
707 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
708 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
709 78756c87 2020-11-24 stsp const char *, const char *, int);
710 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
711 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
712 e78dc838 2020-12-04 stsp struct tog_view *, int);
713 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
714 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
715 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
716 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
717 e5a0f69f 2018-08-18 stsp
718 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
719 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
720 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
721 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
722 e78dc838 2020-12-04 stsp struct tog_view *, int);
723 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
724 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
725 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
726 eaf2c13e 2022-09-17 mark static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
727 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
728 e5a0f69f 2018-08-18 stsp
729 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
730 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
731 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
732 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
733 e78dc838 2020-12-04 stsp struct tog_view *, int);
734 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
735 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
736 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
737 6458efa5 2020-11-24 stsp
738 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
739 6458efa5 2020-11-24 stsp struct got_repository *);
740 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
741 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
742 e78dc838 2020-12-04 stsp struct tog_view *, int);
743 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
744 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
745 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
746 eaf2c13e 2022-09-17 mark
747 eaf2c13e 2022-09-17 mark static const struct got_error *open_help_view(struct tog_view *,
748 eaf2c13e 2022-09-17 mark struct tog_view *);
749 eaf2c13e 2022-09-17 mark static const struct got_error *show_help_view(struct tog_view *);
750 eaf2c13e 2022-09-17 mark static const struct got_error *input_help_view(struct tog_view **,
751 eaf2c13e 2022-09-17 mark struct tog_view *, int);
752 eaf2c13e 2022-09-17 mark static const struct got_error *reset_help_view(struct tog_view *);
753 eaf2c13e 2022-09-17 mark static const struct got_error* close_help_view(struct tog_view *);
754 eaf2c13e 2022-09-17 mark static const struct got_error *search_start_help_view(struct tog_view *);
755 eaf2c13e 2022-09-17 mark static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
756 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
757 25791caa 2018-10-24 stsp
758 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
759 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
760 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
761 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
762 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
763 25791caa 2018-10-24 stsp
764 25791caa 2018-10-24 stsp static void
765 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
766 25791caa 2018-10-24 stsp {
767 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
768 83baff54 2019-08-12 stsp }
769 83baff54 2019-08-12 stsp
770 83baff54 2019-08-12 stsp static void
771 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
772 83baff54 2019-08-12 stsp {
773 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
774 25791caa 2018-10-24 stsp }
775 26ed57b2 2018-05-19 stsp
776 61266923 2020-01-14 stsp static void
777 61266923 2020-01-14 stsp tog_sigcont(int signo)
778 61266923 2020-01-14 stsp {
779 61266923 2020-01-14 stsp tog_sigcont_received = 1;
780 61266923 2020-01-14 stsp }
781 61266923 2020-01-14 stsp
782 2497f032 2022-05-31 stsp static void
783 2497f032 2022-05-31 stsp tog_sigint(int signo)
784 2497f032 2022-05-31 stsp {
785 2497f032 2022-05-31 stsp tog_sigint_received = 1;
786 2497f032 2022-05-31 stsp }
787 2497f032 2022-05-31 stsp
788 2497f032 2022-05-31 stsp static void
789 2497f032 2022-05-31 stsp tog_sigterm(int signo)
790 2497f032 2022-05-31 stsp {
791 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
792 2497f032 2022-05-31 stsp }
793 2497f032 2022-05-31 stsp
794 2497f032 2022-05-31 stsp static int
795 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
796 2497f032 2022-05-31 stsp {
797 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
798 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
799 2497f032 2022-05-31 stsp }
800 2497f032 2022-05-31 stsp
801 e5a0f69f 2018-08-18 stsp static const struct got_error *
802 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
803 ea5e7bb5 2018-08-01 stsp {
804 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
805 e5a0f69f 2018-08-18 stsp
806 669b5ffa 2018-10-07 stsp if (view->child) {
807 5629093a 2022-07-11 stsp child_err = view_close(view->child);
808 669b5ffa 2018-10-07 stsp view->child = NULL;
809 669b5ffa 2018-10-07 stsp }
810 e5a0f69f 2018-08-18 stsp if (view->close)
811 e5a0f69f 2018-08-18 stsp err = view->close(view);
812 ea5e7bb5 2018-08-01 stsp if (view->panel)
813 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
814 ea5e7bb5 2018-08-01 stsp if (view->window)
815 ea5e7bb5 2018-08-01 stsp delwin(view->window);
816 ea5e7bb5 2018-08-01 stsp free(view);
817 5629093a 2022-07-11 stsp return err ? err : child_err;
818 ea5e7bb5 2018-08-01 stsp }
819 ea5e7bb5 2018-08-01 stsp
820 ea5e7bb5 2018-08-01 stsp static struct tog_view *
821 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
822 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
823 ea5e7bb5 2018-08-01 stsp {
824 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
825 ea5e7bb5 2018-08-01 stsp
826 ea5e7bb5 2018-08-01 stsp if (view == NULL)
827 ea5e7bb5 2018-08-01 stsp return NULL;
828 ea5e7bb5 2018-08-01 stsp
829 d6b05b5b 2018-08-04 stsp view->type = type;
830 f7d12f7e 2018-08-01 stsp view->lines = LINES;
831 f7d12f7e 2018-08-01 stsp view->cols = COLS;
832 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
833 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
834 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
835 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
836 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
837 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
838 96a765a8 2018-08-04 stsp view_close(view);
839 ea5e7bb5 2018-08-01 stsp return NULL;
840 ea5e7bb5 2018-08-01 stsp }
841 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
842 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
843 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
844 96a765a8 2018-08-04 stsp view_close(view);
845 ea5e7bb5 2018-08-01 stsp return NULL;
846 ea5e7bb5 2018-08-01 stsp }
847 ea5e7bb5 2018-08-01 stsp
848 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
849 ea5e7bb5 2018-08-01 stsp return view;
850 cdf1ee82 2018-08-01 stsp }
851 cdf1ee82 2018-08-01 stsp
852 0cf4efb1 2018-09-29 stsp static int
853 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
854 0cf4efb1 2018-09-29 stsp {
855 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
856 0cf4efb1 2018-09-29 stsp return 0;
857 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
858 5c60c32a 2018-10-18 stsp }
859 5c60c32a 2018-10-18 stsp
860 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
861 9b058f45 2022-06-30 mark static int
862 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
863 9b058f45 2022-06-30 mark {
864 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
865 9b058f45 2022-06-30 mark }
866 9b058f45 2022-06-30 mark
867 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
868 5c60c32a 2018-10-18 stsp
869 5c60c32a 2018-10-18 stsp static const struct got_error *
870 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
871 5c60c32a 2018-10-18 stsp {
872 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
873 5c60c32a 2018-10-18 stsp
874 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
875 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
876 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
877 3c1dfe12 2022-07-08 mark else
878 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
879 9b058f45 2022-06-30 mark view->begin_x = 0;
880 571ccd73 2022-07-19 mark } else if (!view->resized) {
881 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
882 3c1dfe12 2022-07-08 mark view->cols > 119)
883 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
884 3c1dfe12 2022-07-08 mark else
885 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
886 9b058f45 2022-06-30 mark view->begin_y = 0;
887 9b058f45 2022-06-30 mark }
888 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
889 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
890 5c60c32a 2018-10-18 stsp view->lines = LINES;
891 5c60c32a 2018-10-18 stsp view->cols = COLS;
892 5c60c32a 2018-10-18 stsp err = view_resize(view);
893 5c60c32a 2018-10-18 stsp if (err)
894 5c60c32a 2018-10-18 stsp return err;
895 5c60c32a 2018-10-18 stsp
896 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
897 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
898 9b058f45 2022-06-30 mark
899 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
900 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
901 5c60c32a 2018-10-18 stsp
902 5c60c32a 2018-10-18 stsp return NULL;
903 5c60c32a 2018-10-18 stsp }
904 5c60c32a 2018-10-18 stsp
905 5c60c32a 2018-10-18 stsp static const struct got_error *
906 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
907 5c60c32a 2018-10-18 stsp {
908 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
909 5c60c32a 2018-10-18 stsp
910 5c60c32a 2018-10-18 stsp view->begin_x = 0;
911 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
912 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
913 5c60c32a 2018-10-18 stsp view->ncols = COLS;
914 5c60c32a 2018-10-18 stsp view->lines = LINES;
915 5c60c32a 2018-10-18 stsp view->cols = COLS;
916 5c60c32a 2018-10-18 stsp err = view_resize(view);
917 5c60c32a 2018-10-18 stsp if (err)
918 5c60c32a 2018-10-18 stsp return err;
919 5c60c32a 2018-10-18 stsp
920 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
921 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
922 5c60c32a 2018-10-18 stsp
923 5c60c32a 2018-10-18 stsp return NULL;
924 0cf4efb1 2018-09-29 stsp }
925 0cf4efb1 2018-09-29 stsp
926 5c60c32a 2018-10-18 stsp static int
927 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
928 5c60c32a 2018-10-18 stsp {
929 5c60c32a 2018-10-18 stsp return view->parent == NULL;
930 5c60c32a 2018-10-18 stsp }
931 5c60c32a 2018-10-18 stsp
932 6131ff18 2022-06-20 mark static int
933 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
934 6131ff18 2022-06-20 mark {
935 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
936 24b9cfdc 2022-06-30 stsp }
937 24b9cfdc 2022-06-30 stsp
938 24b9cfdc 2022-06-30 stsp static int
939 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
940 24b9cfdc 2022-06-30 stsp {
941 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
942 6131ff18 2022-06-20 mark }
943 6131ff18 2022-06-20 mark
944 49b24ee5 2022-07-03 mark static int
945 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
946 49b24ee5 2022-07-03 mark {
947 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
948 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
949 49b24ee5 2022-07-03 mark }
950 49b24ee5 2022-07-03 mark
951 9b058f45 2022-06-30 mark static void
952 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
953 9b058f45 2022-06-30 mark {
954 9b058f45 2022-06-30 mark PANEL *panel;
955 9b058f45 2022-06-30 mark const struct tog_view *view_above;
956 6131ff18 2022-06-20 mark
957 9b058f45 2022-06-30 mark if (view->parent)
958 9b058f45 2022-06-30 mark return view_border(view->parent);
959 9b058f45 2022-06-30 mark
960 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
961 9b058f45 2022-06-30 mark if (panel == NULL)
962 9b058f45 2022-06-30 mark return;
963 9b058f45 2022-06-30 mark
964 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
965 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
966 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
967 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
968 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
969 9b058f45 2022-06-30 mark else
970 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
971 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
972 9b058f45 2022-06-30 mark }
973 9b058f45 2022-06-30 mark
974 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
975 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
976 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
977 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
978 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
979 9b058f45 2022-06-30 mark
980 4d8c2215 2018-08-19 stsp static const struct got_error *
981 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
982 f7d12f7e 2018-08-01 stsp {
983 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
984 9b058f45 2022-06-30 mark int dif, nlines, ncols;
985 f7d12f7e 2018-08-01 stsp
986 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
987 9b058f45 2022-06-30 mark
988 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
989 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
990 0cf4efb1 2018-09-29 stsp else
991 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
992 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
993 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
994 0cf4efb1 2018-09-29 stsp else
995 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
996 6d0fee91 2018-08-01 stsp
997 4dd27a72 2022-06-29 stsp if (view->child) {
998 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
999 9b058f45 2022-06-30 mark
1000 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
1001 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
1002 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1003 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
1004 0dbbbe90 2022-06-17 op ncols = COLS;
1005 0dbbbe90 2022-06-17 op
1006 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1007 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1008 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1009 5c60c32a 2018-10-18 stsp else
1010 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1011 5c60c32a 2018-10-18 stsp } else {
1012 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
1013 0dbbbe90 2022-06-17 op
1014 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1015 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1016 9b058f45 2022-06-30 mark }
1017 9b058f45 2022-06-30 mark /*
1018 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
1019 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
1020 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
1021 9b058f45 2022-06-30 mark */
1022 9b058f45 2022-06-30 mark if (hs) {
1023 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
1024 9b058f45 2022-06-30 mark if (err)
1025 9b058f45 2022-06-30 mark return err;
1026 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
1027 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1028 9b058f45 2022-06-30 mark if (err)
1029 9b058f45 2022-06-30 mark return err;
1030 9b058f45 2022-06-30 mark }
1031 9b058f45 2022-06-30 mark view_border(view);
1032 9b058f45 2022-06-30 mark update_panels();
1033 9b058f45 2022-06-30 mark doupdate();
1034 9b058f45 2022-06-30 mark show_panel(view->child->panel);
1035 9b058f45 2022-06-30 mark nlines = view->nlines;
1036 9b058f45 2022-06-30 mark }
1037 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
1038 0dbbbe90 2022-06-17 op ncols = COLS;
1039 669b5ffa 2018-10-07 stsp
1040 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
1041 571ccd73 2022-07-19 mark err = view->resize(view, dif);
1042 571ccd73 2022-07-19 mark if (err)
1043 571ccd73 2022-07-19 mark return err;
1044 571ccd73 2022-07-19 mark }
1045 571ccd73 2022-07-19 mark
1046 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
1047 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
1048 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
1049 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
1050 0dbbbe90 2022-06-17 op wclear(view->window);
1051 0dbbbe90 2022-06-17 op
1052 0dbbbe90 2022-06-17 op view->nlines = nlines;
1053 0dbbbe90 2022-06-17 op view->ncols = ncols;
1054 0dbbbe90 2022-06-17 op view->lines = LINES;
1055 0dbbbe90 2022-06-17 op view->cols = COLS;
1056 0dbbbe90 2022-06-17 op
1057 5c60c32a 2018-10-18 stsp return NULL;
1058 571ccd73 2022-07-19 mark }
1059 571ccd73 2022-07-19 mark
1060 571ccd73 2022-07-19 mark static const struct got_error *
1061 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
1062 571ccd73 2022-07-19 mark {
1063 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
1064 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
1065 6fe51fee 2022-07-22 mark int n = 0;
1066 571ccd73 2022-07-19 mark
1067 6fe51fee 2022-07-22 mark if (s->selected_entry)
1068 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
1069 6fe51fee 2022-07-22 mark
1070 571ccd73 2022-07-19 mark /*
1071 571ccd73 2022-07-19 mark * Request commits to account for the increased
1072 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
1073 571ccd73 2022-07-19 mark */
1074 568eae95 2022-09-11 mark if (s->commits->ncommits < n) {
1075 568eae95 2022-09-11 mark view->nscrolled = n - s->commits->ncommits + increase + 1;
1076 571ccd73 2022-07-19 mark err = request_log_commits(view);
1077 571ccd73 2022-07-19 mark }
1078 571ccd73 2022-07-19 mark
1079 571ccd73 2022-07-19 mark return err;
1080 d9a7ab53 2022-07-11 mark }
1081 d9a7ab53 2022-07-11 mark
1082 d9a7ab53 2022-07-11 mark static void
1083 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
1084 d9a7ab53 2022-07-11 mark {
1085 d9a7ab53 2022-07-11 mark if (n == 0)
1086 d9a7ab53 2022-07-11 mark return;
1087 d9a7ab53 2022-07-11 mark
1088 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
1089 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
1090 d9a7ab53 2022-07-11 mark view->parent->offset += n;
1091 d9a7ab53 2022-07-11 mark else
1092 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
1093 d9a7ab53 2022-07-11 mark } else if (view->offset) {
1094 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
1095 d9a7ab53 2022-07-11 mark view->offset -= n;
1096 d9a7ab53 2022-07-11 mark else
1097 d9a7ab53 2022-07-11 mark view->offset = 0;
1098 d9a7ab53 2022-07-11 mark }
1099 3c1dfe12 2022-07-08 mark }
1100 3c1dfe12 2022-07-08 mark
1101 3c1dfe12 2022-07-08 mark static const struct got_error *
1102 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
1103 3c1dfe12 2022-07-08 mark {
1104 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1105 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
1106 3c1dfe12 2022-07-08 mark
1107 3c1dfe12 2022-07-08 mark if (view->parent)
1108 3c1dfe12 2022-07-08 mark v = view->parent;
1109 3c1dfe12 2022-07-08 mark else
1110 3c1dfe12 2022-07-08 mark v = view;
1111 3c1dfe12 2022-07-08 mark
1112 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
1113 3c1dfe12 2022-07-08 mark return NULL;
1114 3c1dfe12 2022-07-08 mark
1115 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
1116 3c1dfe12 2022-07-08 mark
1117 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1118 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
1119 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1120 3c1dfe12 2022-07-08 mark if (view->parent)
1121 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
1122 3c1dfe12 2022-07-08 mark else
1123 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
1124 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
1125 3c1dfe12 2022-07-08 mark view->count = 0;
1126 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
1127 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
1128 3c1dfe12 2022-07-08 mark view->count = 0;
1129 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
1130 3c1dfe12 2022-07-08 mark }
1131 3c1dfe12 2022-07-08 mark v->ncols = COLS;
1132 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
1133 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
1134 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
1135 3c1dfe12 2022-07-08 mark if (err)
1136 3c1dfe12 2022-07-08 mark return err;
1137 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1138 3c1dfe12 2022-07-08 mark } else {
1139 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1140 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1141 3c1dfe12 2022-07-08 mark if (view->parent)
1142 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1143 3c1dfe12 2022-07-08 mark else
1144 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1145 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1146 3c1dfe12 2022-07-08 mark view->count = 0;
1147 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1148 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1149 3c1dfe12 2022-07-08 mark view->count = 0;
1150 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1151 3c1dfe12 2022-07-08 mark }
1152 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1153 3c1dfe12 2022-07-08 mark }
1154 3c1dfe12 2022-07-08 mark
1155 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1156 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1157 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1158 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1159 3c1dfe12 2022-07-08 mark
1160 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1161 3c1dfe12 2022-07-08 mark if (err)
1162 3c1dfe12 2022-07-08 mark return err;
1163 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1164 3c1dfe12 2022-07-08 mark if (err)
1165 3c1dfe12 2022-07-08 mark return err;
1166 3c1dfe12 2022-07-08 mark
1167 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1168 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1169 3c1dfe12 2022-07-08 mark if (err)
1170 3c1dfe12 2022-07-08 mark return err;
1171 3c1dfe12 2022-07-08 mark }
1172 3c1dfe12 2022-07-08 mark
1173 6fe51fee 2022-07-22 mark if (v->resize)
1174 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1175 6fe51fee 2022-07-22 mark else if (v->child->resize)
1176 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1177 3c1dfe12 2022-07-08 mark
1178 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1179 3c1dfe12 2022-07-08 mark
1180 3c1dfe12 2022-07-08 mark return err;
1181 3c1dfe12 2022-07-08 mark }
1182 3c1dfe12 2022-07-08 mark
1183 3c1dfe12 2022-07-08 mark static void
1184 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1185 3c1dfe12 2022-07-08 mark {
1186 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1187 3c1dfe12 2022-07-08 mark
1188 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1189 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1190 669b5ffa 2018-10-07 stsp }
1191 669b5ffa 2018-10-07 stsp
1192 669b5ffa 2018-10-07 stsp static const struct got_error *
1193 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1194 669b5ffa 2018-10-07 stsp {
1195 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1196 669b5ffa 2018-10-07 stsp
1197 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1198 669b5ffa 2018-10-07 stsp return NULL;
1199 669b5ffa 2018-10-07 stsp
1200 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1201 669b5ffa 2018-10-07 stsp view->child = NULL;
1202 669b5ffa 2018-10-07 stsp return err;
1203 669b5ffa 2018-10-07 stsp }
1204 669b5ffa 2018-10-07 stsp
1205 0dbbbe90 2022-06-17 op static const struct got_error *
1206 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1207 669b5ffa 2018-10-07 stsp {
1208 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1209 3c1dfe12 2022-07-08 mark
1210 669b5ffa 2018-10-07 stsp view->child = child;
1211 669b5ffa 2018-10-07 stsp child->parent = view;
1212 0dbbbe90 2022-06-17 op
1213 3c1dfe12 2022-07-08 mark err = view_resize(view);
1214 3c1dfe12 2022-07-08 mark if (err)
1215 3c1dfe12 2022-07-08 mark return err;
1216 3c1dfe12 2022-07-08 mark
1217 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1218 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1219 3c1dfe12 2022-07-08 mark
1220 3c1dfe12 2022-07-08 mark return err;
1221 bfddd0d9 2018-09-29 stsp }
1222 136e2bd4 2022-07-23 mark
1223 136e2bd4 2022-07-23 mark static const struct got_error *view_dispatch_request(struct tog_view **,
1224 136e2bd4 2022-07-23 mark struct tog_view *, enum tog_view_type, int, int);
1225 136e2bd4 2022-07-23 mark
1226 136e2bd4 2022-07-23 mark static const struct got_error *
1227 136e2bd4 2022-07-23 mark view_request_new(struct tog_view **requested, struct tog_view *view,
1228 136e2bd4 2022-07-23 mark enum tog_view_type request)
1229 136e2bd4 2022-07-23 mark {
1230 136e2bd4 2022-07-23 mark struct tog_view *new_view = NULL;
1231 136e2bd4 2022-07-23 mark const struct got_error *err;
1232 136e2bd4 2022-07-23 mark int y = 0, x = 0;
1233 136e2bd4 2022-07-23 mark
1234 136e2bd4 2022-07-23 mark *requested = NULL;
1235 136e2bd4 2022-07-23 mark
1236 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1237 136e2bd4 2022-07-23 mark view_get_split(view, &y, &x);
1238 bfddd0d9 2018-09-29 stsp
1239 136e2bd4 2022-07-23 mark err = view_dispatch_request(&new_view, view, request, y, x);
1240 136e2bd4 2022-07-23 mark if (err)
1241 136e2bd4 2022-07-23 mark return err;
1242 136e2bd4 2022-07-23 mark
1243 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1244 94274a8f 2022-09-19 mark request != TOG_VIEW_HELP) {
1245 136e2bd4 2022-07-23 mark err = view_init_hsplit(view, y);
1246 136e2bd4 2022-07-23 mark if (err)
1247 136e2bd4 2022-07-23 mark return err;
1248 136e2bd4 2022-07-23 mark }
1249 136e2bd4 2022-07-23 mark
1250 136e2bd4 2022-07-23 mark view->focussed = 0;
1251 136e2bd4 2022-07-23 mark new_view->focussed = 1;
1252 136e2bd4 2022-07-23 mark new_view->mode = view->mode;
1253 94274a8f 2022-09-19 mark new_view->nlines = request == TOG_VIEW_HELP ?
1254 94274a8f 2022-09-19 mark view->lines : view->lines - y;
1255 136e2bd4 2022-07-23 mark
1256 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1257 136e2bd4 2022-07-23 mark view_transfer_size(new_view, view);
1258 136e2bd4 2022-07-23 mark err = view_close_child(view);
1259 136e2bd4 2022-07-23 mark if (err)
1260 136e2bd4 2022-07-23 mark return err;
1261 136e2bd4 2022-07-23 mark err = view_set_child(view, new_view);
1262 136e2bd4 2022-07-23 mark if (err)
1263 136e2bd4 2022-07-23 mark return err;
1264 136e2bd4 2022-07-23 mark view->focus_child = 1;
1265 136e2bd4 2022-07-23 mark } else
1266 136e2bd4 2022-07-23 mark *requested = new_view;
1267 136e2bd4 2022-07-23 mark
1268 136e2bd4 2022-07-23 mark return NULL;
1269 136e2bd4 2022-07-23 mark }
1270 136e2bd4 2022-07-23 mark
1271 34bc9ec9 2019-02-22 stsp static void
1272 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1273 25791caa 2018-10-24 stsp {
1274 25791caa 2018-10-24 stsp int cols, lines;
1275 25791caa 2018-10-24 stsp struct winsize size;
1276 25791caa 2018-10-24 stsp
1277 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1278 25791caa 2018-10-24 stsp cols = 80; /* Default */
1279 25791caa 2018-10-24 stsp lines = 24;
1280 25791caa 2018-10-24 stsp } else {
1281 25791caa 2018-10-24 stsp cols = size.ws_col;
1282 25791caa 2018-10-24 stsp lines = size.ws_row;
1283 25791caa 2018-10-24 stsp }
1284 25791caa 2018-10-24 stsp resize_term(lines, cols);
1285 2b49a8ae 2019-06-22 stsp }
1286 2b49a8ae 2019-06-22 stsp
1287 2b49a8ae 2019-06-22 stsp static const struct got_error *
1288 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1289 2b49a8ae 2019-06-22 stsp {
1290 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1291 9b058f45 2022-06-30 mark struct tog_view *v = view;
1292 2b49a8ae 2019-06-22 stsp char pattern[1024];
1293 2b49a8ae 2019-06-22 stsp int ret;
1294 c0c4acc8 2021-01-24 stsp
1295 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1296 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1297 c0c4acc8 2021-01-24 stsp view->searching = 0;
1298 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1299 c0c4acc8 2021-01-24 stsp }
1300 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1301 2b49a8ae 2019-06-22 stsp
1302 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1303 2b49a8ae 2019-06-22 stsp return NULL;
1304 2b49a8ae 2019-06-22 stsp
1305 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1306 9b058f45 2022-06-30 mark v = view->child;
1307 2b49a8ae 2019-06-22 stsp
1308 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1309 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1310 9b058f45 2022-06-30 mark
1311 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE); /* block for search term input */
1312 2b49a8ae 2019-06-22 stsp nocbreak();
1313 2b49a8ae 2019-06-22 stsp echo();
1314 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1315 9b058f45 2022-06-30 mark wrefresh(v->window);
1316 2b49a8ae 2019-06-22 stsp cbreak();
1317 2b49a8ae 2019-06-22 stsp noecho();
1318 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1319 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1320 2b49a8ae 2019-06-22 stsp return NULL;
1321 2b49a8ae 2019-06-22 stsp
1322 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1323 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1324 7c32bd05 2019-06-22 stsp if (err) {
1325 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1326 7c32bd05 2019-06-22 stsp return err;
1327 7c32bd05 2019-06-22 stsp }
1328 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1329 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1330 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1331 2b49a8ae 2019-06-22 stsp view->search_next(view);
1332 2b49a8ae 2019-06-22 stsp }
1333 2b49a8ae 2019-06-22 stsp
1334 2b49a8ae 2019-06-22 stsp return NULL;
1335 d2366e29 2022-07-07 mark }
1336 d2366e29 2022-07-07 mark
1337 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1338 d2366e29 2022-07-07 mark static const struct got_error *
1339 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1340 d2366e29 2022-07-07 mark {
1341 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1342 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1343 d2366e29 2022-07-07 mark
1344 d2366e29 2022-07-07 mark if (view->parent)
1345 d2366e29 2022-07-07 mark v = view->parent;
1346 d2366e29 2022-07-07 mark else
1347 d2366e29 2022-07-07 mark v = view;
1348 d2366e29 2022-07-07 mark
1349 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1350 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1351 7532ccda 2022-07-11 mark else
1352 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1353 d2366e29 2022-07-07 mark
1354 7532ccda 2022-07-11 mark if (!v->child)
1355 7532ccda 2022-07-11 mark return NULL;
1356 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1357 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1358 7532ccda 2022-07-11 mark
1359 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1360 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1361 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1362 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1363 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1364 d2366e29 2022-07-07 mark
1365 7532ccda 2022-07-11 mark
1366 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1367 d2366e29 2022-07-07 mark v->ncols = COLS;
1368 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1369 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1370 d2366e29 2022-07-07 mark
1371 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1372 d2366e29 2022-07-07 mark if (err)
1373 d2366e29 2022-07-07 mark return err;
1374 d2366e29 2022-07-07 mark }
1375 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1376 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1377 d2366e29 2022-07-07 mark v->focus_child = 1;
1378 d2366e29 2022-07-07 mark
1379 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1380 d2366e29 2022-07-07 mark if (err)
1381 d2366e29 2022-07-07 mark return err;
1382 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1383 d2366e29 2022-07-07 mark if (err)
1384 d2366e29 2022-07-07 mark return err;
1385 d2366e29 2022-07-07 mark
1386 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1387 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1388 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1389 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1390 279d2047 2022-07-29 stsp if (err)
1391 279d2047 2022-07-29 stsp return err;
1392 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1393 279d2047 2022-07-29 stsp if (err)
1394 279d2047 2022-07-29 stsp return err;
1395 7532ccda 2022-07-11 mark } else {
1396 7532ccda 2022-07-11 mark offset_selection_up(v);
1397 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1398 d2366e29 2022-07-07 mark }
1399 dff91825 2022-07-22 mark if (v->resize)
1400 dff91825 2022-07-22 mark err = v->resize(v, 0);
1401 dff91825 2022-07-22 mark else if (v->child->resize)
1402 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1403 d2366e29 2022-07-07 mark
1404 d2366e29 2022-07-07 mark return err;
1405 0cf4efb1 2018-09-29 stsp }
1406 6d0fee91 2018-08-01 stsp
1407 640cd7ff 2022-06-22 mark /*
1408 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1409 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1410 640cd7ff 2022-06-22 mark */
1411 640cd7ff 2022-06-22 mark static int
1412 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1413 640cd7ff 2022-06-22 mark {
1414 9b058f45 2022-06-30 mark struct tog_view *v = view;
1415 9b058f45 2022-06-30 mark int x, n = 0;
1416 640cd7ff 2022-06-22 mark
1417 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1418 9b058f45 2022-06-30 mark v = view->child;
1419 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1420 9b058f45 2022-06-30 mark v = view->parent;
1421 9b058f45 2022-06-30 mark
1422 640cd7ff 2022-06-22 mark view->count = 0;
1423 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1424 94b80cfa 2022-08-01 mark nodelay(view->window, FALSE);
1425 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1426 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1427 9b058f45 2022-06-30 mark waddch(v->window, ':');
1428 640cd7ff 2022-06-22 mark
1429 640cd7ff 2022-06-22 mark do {
1430 9b058f45 2022-06-30 mark x = getcurx(v->window);
1431 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1432 9b058f45 2022-06-30 mark waddch(v->window, c);
1433 9b058f45 2022-06-30 mark wrefresh(v->window);
1434 9b058f45 2022-06-30 mark }
1435 9b058f45 2022-06-30 mark
1436 640cd7ff 2022-06-22 mark /*
1437 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1438 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1439 640cd7ff 2022-06-22 mark */
1440 640cd7ff 2022-06-22 mark if (n >= 9999999)
1441 640cd7ff 2022-06-22 mark n = 9999999;
1442 640cd7ff 2022-06-22 mark else
1443 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1444 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1445 640cd7ff 2022-06-22 mark
1446 94b80cfa 2022-08-01 mark if (c == 'G' || c == 'g') { /* nG key map */
1447 94b80cfa 2022-08-01 mark view->gline = view->hiline = n;
1448 94b80cfa 2022-08-01 mark n = 0;
1449 94b80cfa 2022-08-01 mark c = 0;
1450 94b80cfa 2022-08-01 mark }
1451 94b80cfa 2022-08-01 mark
1452 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1453 640cd7ff 2022-06-22 mark view->count = n;
1454 640cd7ff 2022-06-22 mark
1455 640cd7ff 2022-06-22 mark return c;
1456 640cd7ff 2022-06-22 mark }
1457 640cd7ff 2022-06-22 mark
1458 0cf4efb1 2018-09-29 stsp static const struct got_error *
1459 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1460 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1461 e5a0f69f 2018-08-18 stsp {
1462 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1463 669b5ffa 2018-10-07 stsp struct tog_view *v;
1464 1a76625f 2018-10-22 stsp int ch, errcode;
1465 e5a0f69f 2018-08-18 stsp
1466 e5a0f69f 2018-08-18 stsp *new = NULL;
1467 8f4ed634 2020-03-26 stsp
1468 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1469 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1470 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1471 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1472 640cd7ff 2022-06-22 mark view->count = 0;
1473 640cd7ff 2022-06-22 mark }
1474 e5a0f69f 2018-08-18 stsp
1475 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1476 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1477 82954512 2020-02-03 stsp if (errcode)
1478 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1479 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1480 3da8ef85 2021-09-21 stsp sched_yield();
1481 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1482 82954512 2020-02-03 stsp if (errcode)
1483 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1484 82954512 2020-02-03 stsp "pthread_mutex_lock");
1485 60493ae3 2019-06-20 stsp view->search_next(view);
1486 60493ae3 2019-06-20 stsp return NULL;
1487 60493ae3 2019-06-20 stsp }
1488 60493ae3 2019-06-20 stsp
1489 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1490 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1491 1a76625f 2018-10-22 stsp if (errcode)
1492 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1493 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1494 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1495 a6d37fac 2022-07-03 mark cbreak();
1496 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1497 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1498 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1499 a6d37fac 2022-07-03 mark view->count = 0;
1500 a6d37fac 2022-07-03 mark else
1501 a6d37fac 2022-07-03 mark ch = view->ch;
1502 a6d37fac 2022-07-03 mark } else {
1503 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1504 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1505 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1506 640cd7ff 2022-06-22 mark }
1507 94b80cfa 2022-08-01 mark if (view->hiline && ch != ERR && ch != 0)
1508 94b80cfa 2022-08-01 mark view->hiline = 0; /* key pressed, clear line highlight */
1509 94b80cfa 2022-08-01 mark nodelay(view->window, TRUE);
1510 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1511 1a76625f 2018-10-22 stsp if (errcode)
1512 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1513 25791caa 2018-10-24 stsp
1514 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1515 25791caa 2018-10-24 stsp tog_resizeterm();
1516 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1517 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1518 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1519 25791caa 2018-10-24 stsp err = view_resize(v);
1520 25791caa 2018-10-24 stsp if (err)
1521 25791caa 2018-10-24 stsp return err;
1522 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1523 25791caa 2018-10-24 stsp if (err)
1524 25791caa 2018-10-24 stsp return err;
1525 cdfcfb03 2020-12-06 stsp if (v->child) {
1526 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1527 cdfcfb03 2020-12-06 stsp if (err)
1528 cdfcfb03 2020-12-06 stsp return err;
1529 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1530 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1531 cdfcfb03 2020-12-06 stsp if (err)
1532 cdfcfb03 2020-12-06 stsp return err;
1533 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1534 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1535 3c1dfe12 2022-07-08 mark if (err)
1536 3c1dfe12 2022-07-08 mark return err;
1537 3c1dfe12 2022-07-08 mark }
1538 cdfcfb03 2020-12-06 stsp }
1539 25791caa 2018-10-24 stsp }
1540 25791caa 2018-10-24 stsp }
1541 25791caa 2018-10-24 stsp
1542 e5a0f69f 2018-08-18 stsp switch (ch) {
1543 ec2a9698 2022-09-15 mark case '?':
1544 ec2a9698 2022-09-15 mark case 'H':
1545 ec2a9698 2022-09-15 mark case KEY_F(1):
1546 ec2a9698 2022-09-15 mark if (view->type == TOG_VIEW_HELP)
1547 ec2a9698 2022-09-15 mark err = view->reset(view);
1548 ec2a9698 2022-09-15 mark else
1549 ec2a9698 2022-09-15 mark err = view_request_new(new, view, TOG_VIEW_HELP);
1550 ec2a9698 2022-09-15 mark break;
1551 1e37a5c2 2019-05-12 jcs case '\t':
1552 640cd7ff 2022-06-22 mark view->count = 0;
1553 1e37a5c2 2019-05-12 jcs if (view->child) {
1554 e78dc838 2020-12-04 stsp view->focussed = 0;
1555 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1556 e78dc838 2020-12-04 stsp view->focus_child = 1;
1557 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1558 e78dc838 2020-12-04 stsp view->focussed = 0;
1559 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1560 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1561 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1562 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1563 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1564 6fe51fee 2022-07-22 mark 0);
1565 9b058f45 2022-06-30 mark if (err)
1566 9b058f45 2022-06-30 mark return err;
1567 9b058f45 2022-06-30 mark }
1568 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1569 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1570 9b058f45 2022-06-30 mark if (err)
1571 9b058f45 2022-06-30 mark return err;
1572 9b058f45 2022-06-30 mark }
1573 1e37a5c2 2019-05-12 jcs }
1574 1e37a5c2 2019-05-12 jcs break;
1575 1e37a5c2 2019-05-12 jcs case 'q':
1576 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1577 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1578 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1579 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1580 9b058f45 2022-06-30 mark if (err)
1581 9b058f45 2022-06-30 mark break;
1582 9b058f45 2022-06-30 mark }
1583 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1584 9b058f45 2022-06-30 mark }
1585 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1586 9970f7fc 2020-12-03 stsp view->dying = 1;
1587 1e37a5c2 2019-05-12 jcs break;
1588 1e37a5c2 2019-05-12 jcs case 'Q':
1589 1e37a5c2 2019-05-12 jcs *done = 1;
1590 1e37a5c2 2019-05-12 jcs break;
1591 61417565 2022-06-20 mark case 'F':
1592 640cd7ff 2022-06-22 mark view->count = 0;
1593 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1594 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1595 1e37a5c2 2019-05-12 jcs break;
1596 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1597 e78dc838 2020-12-04 stsp view->focussed = 0;
1598 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1599 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1600 3c1dfe12 2022-07-08 mark } else {
1601 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1602 3c1dfe12 2022-07-08 mark if (!err)
1603 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1604 3c1dfe12 2022-07-08 mark }
1605 1e37a5c2 2019-05-12 jcs if (err)
1606 1e37a5c2 2019-05-12 jcs break;
1607 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1608 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1609 1e37a5c2 2019-05-12 jcs } else {
1610 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1611 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1612 e78dc838 2020-12-04 stsp view->focussed = 1;
1613 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1614 1e37a5c2 2019-05-12 jcs } else {
1615 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1616 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1617 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1618 3c1dfe12 2022-07-08 mark if (!err)
1619 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1620 669b5ffa 2018-10-07 stsp }
1621 1e37a5c2 2019-05-12 jcs if (err)
1622 1e37a5c2 2019-05-12 jcs break;
1623 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1624 1e37a5c2 2019-05-12 jcs }
1625 9b058f45 2022-06-30 mark if (err)
1626 9b058f45 2022-06-30 mark break;
1627 6fe51fee 2022-07-22 mark if (view->resize) {
1628 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1629 9b058f45 2022-06-30 mark if (err)
1630 9b058f45 2022-06-30 mark break;
1631 9b058f45 2022-06-30 mark }
1632 9b058f45 2022-06-30 mark if (view->parent)
1633 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1634 9b058f45 2022-06-30 mark if (!err)
1635 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1636 1e37a5c2 2019-05-12 jcs break;
1637 d2366e29 2022-07-07 mark case 'S':
1638 3c1dfe12 2022-07-08 mark view->count = 0;
1639 d2366e29 2022-07-07 mark err = switch_split(view);
1640 3c1dfe12 2022-07-08 mark break;
1641 3c1dfe12 2022-07-08 mark case '-':
1642 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1643 d2366e29 2022-07-07 mark break;
1644 3c1dfe12 2022-07-08 mark case '+':
1645 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1646 3c1dfe12 2022-07-08 mark break;
1647 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1648 60493ae3 2019-06-20 stsp break;
1649 60493ae3 2019-06-20 stsp case '/':
1650 640cd7ff 2022-06-22 mark view->count = 0;
1651 60493ae3 2019-06-20 stsp if (view->search_start)
1652 2b49a8ae 2019-06-22 stsp view_search_start(view);
1653 60493ae3 2019-06-20 stsp else
1654 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1655 1e37a5c2 2019-05-12 jcs break;
1656 b1bf1435 2019-06-21 stsp case 'N':
1657 60493ae3 2019-06-20 stsp case 'n':
1658 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1659 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1660 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1661 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1662 60493ae3 2019-06-20 stsp view->search_next(view);
1663 60493ae3 2019-06-20 stsp } else
1664 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1665 917d79a7 2022-07-01 stsp break;
1666 917d79a7 2022-07-01 stsp case 'A':
1667 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1668 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1669 917d79a7 2022-07-01 stsp else
1670 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1671 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1672 917d79a7 2022-07-01 stsp if (v->reset) {
1673 917d79a7 2022-07-01 stsp err = v->reset(v);
1674 917d79a7 2022-07-01 stsp if (err)
1675 917d79a7 2022-07-01 stsp return err;
1676 917d79a7 2022-07-01 stsp }
1677 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1678 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1679 917d79a7 2022-07-01 stsp if (err)
1680 917d79a7 2022-07-01 stsp return err;
1681 917d79a7 2022-07-01 stsp }
1682 917d79a7 2022-07-01 stsp }
1683 60493ae3 2019-06-20 stsp break;
1684 1e37a5c2 2019-05-12 jcs default:
1685 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1686 1e37a5c2 2019-05-12 jcs break;
1687 e5a0f69f 2018-08-18 stsp }
1688 e5a0f69f 2018-08-18 stsp
1689 e5a0f69f 2018-08-18 stsp return err;
1690 e5a0f69f 2018-08-18 stsp }
1691 e5a0f69f 2018-08-18 stsp
1692 336075a4 2022-06-25 op static int
1693 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1694 a3404814 2018-09-02 stsp {
1695 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1696 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1697 669b5ffa 2018-10-07 stsp return 0;
1698 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1699 669b5ffa 2018-10-07 stsp return 0;
1700 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1701 a3404814 2018-09-02 stsp return 0;
1702 a3404814 2018-09-02 stsp
1703 669b5ffa 2018-10-07 stsp return view->focussed;
1704 a3404814 2018-09-02 stsp }
1705 a3404814 2018-09-02 stsp
1706 bcbd79e2 2018-08-19 stsp static const struct got_error *
1707 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1708 e5a0f69f 2018-08-18 stsp {
1709 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1710 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1711 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1712 d2366e29 2022-07-07 mark char *mode;
1713 fd823528 2018-10-22 stsp int fast_refresh = 10;
1714 1a76625f 2018-10-22 stsp int done = 0, errcode;
1715 e5a0f69f 2018-08-18 stsp
1716 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1717 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1718 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1719 d2366e29 2022-07-07 mark else
1720 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1721 d2366e29 2022-07-07 mark
1722 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1723 1a76625f 2018-10-22 stsp if (errcode)
1724 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1725 1a76625f 2018-10-22 stsp
1726 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1727 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1728 e5a0f69f 2018-08-18 stsp
1729 1004088d 2018-09-29 stsp view->focussed = 1;
1730 878940b7 2018-09-29 stsp err = view->show(view);
1731 0cf4efb1 2018-09-29 stsp if (err)
1732 0cf4efb1 2018-09-29 stsp return err;
1733 0cf4efb1 2018-09-29 stsp update_panels();
1734 0cf4efb1 2018-09-29 stsp doupdate();
1735 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1736 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1737 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1738 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1739 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1740 fd823528 2018-10-22 stsp
1741 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1742 e5a0f69f 2018-08-18 stsp if (err)
1743 e5a0f69f 2018-08-18 stsp break;
1744 9970f7fc 2020-12-03 stsp if (view->dying) {
1745 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1746 669b5ffa 2018-10-07 stsp
1747 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1748 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1749 9970f7fc 2020-12-03 stsp entry);
1750 e78dc838 2020-12-04 stsp else if (view->parent)
1751 669b5ffa 2018-10-07 stsp prev = view->parent;
1752 669b5ffa 2018-10-07 stsp
1753 e78dc838 2020-12-04 stsp if (view->parent) {
1754 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1755 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1756 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1757 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1758 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1759 0dbbbe90 2022-06-17 op if (err)
1760 0dbbbe90 2022-06-17 op break;
1761 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1762 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1763 e78dc838 2020-12-04 stsp } else
1764 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1765 669b5ffa 2018-10-07 stsp
1766 9970f7fc 2020-12-03 stsp err = view_close(view);
1767 fb59748f 2020-12-05 stsp if (err)
1768 e5a0f69f 2018-08-18 stsp goto done;
1769 669b5ffa 2018-10-07 stsp
1770 e78dc838 2020-12-04 stsp view = NULL;
1771 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1772 e78dc838 2020-12-04 stsp if (v->focussed)
1773 e78dc838 2020-12-04 stsp break;
1774 0cf4efb1 2018-09-29 stsp }
1775 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1776 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1777 e78dc838 2020-12-04 stsp if (prev)
1778 e78dc838 2020-12-04 stsp view = prev;
1779 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1780 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1781 e78dc838 2020-12-04 stsp tog_view_list_head);
1782 e78dc838 2020-12-04 stsp }
1783 e78dc838 2020-12-04 stsp if (view) {
1784 e78dc838 2020-12-04 stsp if (view->focus_child) {
1785 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1786 e78dc838 2020-12-04 stsp view = view->child;
1787 e78dc838 2020-12-04 stsp } else
1788 e78dc838 2020-12-04 stsp view->focussed = 1;
1789 e78dc838 2020-12-04 stsp }
1790 e78dc838 2020-12-04 stsp }
1791 e5a0f69f 2018-08-18 stsp }
1792 bcbd79e2 2018-08-19 stsp if (new_view) {
1793 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1794 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1795 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1796 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1797 86c66b02 2018-10-18 stsp continue;
1798 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1799 86c66b02 2018-10-18 stsp err = view_close(v);
1800 86c66b02 2018-10-18 stsp if (err)
1801 86c66b02 2018-10-18 stsp goto done;
1802 86c66b02 2018-10-18 stsp break;
1803 86c66b02 2018-10-18 stsp }
1804 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1805 fed7eaa8 2018-10-24 stsp view = new_view;
1806 0ae84acc 2022-06-15 tracey }
1807 669b5ffa 2018-10-07 stsp if (view) {
1808 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1809 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1810 e78dc838 2020-12-04 stsp view = view->child;
1811 e78dc838 2020-12-04 stsp } else {
1812 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1813 e78dc838 2020-12-04 stsp view = view->parent;
1814 1a76625f 2018-10-22 stsp }
1815 e78dc838 2020-12-04 stsp show_panel(view->panel);
1816 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1817 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1818 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1819 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1820 669b5ffa 2018-10-07 stsp if (err)
1821 1a76625f 2018-10-22 stsp goto done;
1822 669b5ffa 2018-10-07 stsp }
1823 669b5ffa 2018-10-07 stsp err = view->show(view);
1824 0cf4efb1 2018-09-29 stsp if (err)
1825 1a76625f 2018-10-22 stsp goto done;
1826 669b5ffa 2018-10-07 stsp if (view->child) {
1827 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1828 669b5ffa 2018-10-07 stsp if (err)
1829 1a76625f 2018-10-22 stsp goto done;
1830 669b5ffa 2018-10-07 stsp }
1831 1a76625f 2018-10-22 stsp update_panels();
1832 1a76625f 2018-10-22 stsp doupdate();
1833 0cf4efb1 2018-09-29 stsp }
1834 e5a0f69f 2018-08-18 stsp }
1835 e5a0f69f 2018-08-18 stsp done:
1836 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1837 5629093a 2022-07-11 stsp const struct got_error *close_err;
1838 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1839 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1840 5629093a 2022-07-11 stsp close_err = view_close(view);
1841 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1842 5629093a 2022-07-11 stsp err = close_err;
1843 e5a0f69f 2018-08-18 stsp }
1844 1a76625f 2018-10-22 stsp
1845 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1846 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1847 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1848 1a76625f 2018-10-22 stsp
1849 e5a0f69f 2018-08-18 stsp return err;
1850 ea5e7bb5 2018-08-01 stsp }
1851 ea5e7bb5 2018-08-01 stsp
1852 4ed7e80c 2018-05-20 stsp __dead static void
1853 9f7d7167 2018-04-29 stsp usage_log(void)
1854 9f7d7167 2018-04-29 stsp {
1855 80ddbec8 2018-04-29 stsp endwin();
1856 c70c5802 2018-08-01 stsp fprintf(stderr,
1857 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1858 9f7d7167 2018-04-29 stsp getprogname());
1859 9f7d7167 2018-04-29 stsp exit(1);
1860 80ddbec8 2018-04-29 stsp }
1861 80ddbec8 2018-04-29 stsp
1862 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1863 80ddbec8 2018-04-29 stsp static const struct got_error *
1864 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1865 963b370f 2018-05-20 stsp {
1866 00dfcb92 2018-06-11 stsp char *vis = NULL;
1867 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1868 963b370f 2018-05-20 stsp
1869 963b370f 2018-05-20 stsp *ws = NULL;
1870 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1871 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1872 00dfcb92 2018-06-11 stsp int vislen;
1873 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1874 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1875 00dfcb92 2018-06-11 stsp
1876 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1877 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1878 00dfcb92 2018-06-11 stsp if (err)
1879 00dfcb92 2018-06-11 stsp return err;
1880 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1881 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1882 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1883 a7f50699 2018-06-11 stsp goto done;
1884 a7f50699 2018-06-11 stsp }
1885 00dfcb92 2018-06-11 stsp }
1886 963b370f 2018-05-20 stsp
1887 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1888 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1889 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1890 a7f50699 2018-06-11 stsp goto done;
1891 a7f50699 2018-06-11 stsp }
1892 963b370f 2018-05-20 stsp
1893 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1894 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1895 a7f50699 2018-06-11 stsp done:
1896 00dfcb92 2018-06-11 stsp free(vis);
1897 963b370f 2018-05-20 stsp if (err) {
1898 963b370f 2018-05-20 stsp free(*ws);
1899 963b370f 2018-05-20 stsp *ws = NULL;
1900 963b370f 2018-05-20 stsp *wlen = 0;
1901 963b370f 2018-05-20 stsp }
1902 963b370f 2018-05-20 stsp return err;
1903 145b6838 2022-06-16 stsp }
1904 145b6838 2022-06-16 stsp
1905 145b6838 2022-06-16 stsp static const struct got_error *
1906 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1907 145b6838 2022-06-16 stsp {
1908 145b6838 2022-06-16 stsp char *dst;
1909 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1910 145b6838 2022-06-16 stsp
1911 145b6838 2022-06-16 stsp *ptr = NULL;
1912 145b6838 2022-06-16 stsp n = len = strlen(src);
1913 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1914 145b6838 2022-06-16 stsp if (dst == NULL)
1915 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1916 145b6838 2022-06-16 stsp
1917 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1918 145b6838 2022-06-16 stsp const char c = src[idx];
1919 145b6838 2022-06-16 stsp
1920 145b6838 2022-06-16 stsp if (c == '\t') {
1921 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1922 367ddf28 2022-06-16 mark char *p;
1923 367ddf28 2022-06-16 mark
1924 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1925 6e1c41ad 2022-06-16 mark if (p == NULL) {
1926 6e1c41ad 2022-06-16 mark free(dst);
1927 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1928 6e1c41ad 2022-06-16 mark
1929 6e1c41ad 2022-06-16 mark }
1930 6e1c41ad 2022-06-16 mark dst = p;
1931 145b6838 2022-06-16 stsp n += nb;
1932 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1933 145b6838 2022-06-16 stsp sz += nb;
1934 145b6838 2022-06-16 stsp } else
1935 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1936 145b6838 2022-06-16 stsp ++idx;
1937 145b6838 2022-06-16 stsp }
1938 145b6838 2022-06-16 stsp
1939 145b6838 2022-06-16 stsp dst[sz] = '\0';
1940 145b6838 2022-06-16 stsp *ptr = dst;
1941 145b6838 2022-06-16 stsp return NULL;
1942 963b370f 2018-05-20 stsp }
1943 963b370f 2018-05-20 stsp
1944 4e4a9ac8 2022-06-17 op /*
1945 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1946 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1947 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1948 4e4a9ac8 2022-06-17 op * *rcol.
1949 ccda2f4d 2022-06-16 stsp */
1950 4e4a9ac8 2022-06-17 op static int
1951 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1952 4e4a9ac8 2022-06-17 op {
1953 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1954 ccda2f4d 2022-06-16 stsp
1955 4e4a9ac8 2022-06-17 op if (n == 0) {
1956 4e4a9ac8 2022-06-17 op *rcol = cols;
1957 4e4a9ac8 2022-06-17 op return off;
1958 4e4a9ac8 2022-06-17 op }
1959 ccda2f4d 2022-06-16 stsp
1960 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1961 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1962 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1963 4e4a9ac8 2022-06-17 op else
1964 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1965 ccda2f4d 2022-06-16 stsp
1966 4e4a9ac8 2022-06-17 op if (width == -1) {
1967 4e4a9ac8 2022-06-17 op width = 1;
1968 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1969 ccda2f4d 2022-06-16 stsp }
1970 ccda2f4d 2022-06-16 stsp
1971 4e4a9ac8 2022-06-17 op if (cols + width > n)
1972 4e4a9ac8 2022-06-17 op break;
1973 4e4a9ac8 2022-06-17 op cols += width;
1974 ccda2f4d 2022-06-16 stsp }
1975 ccda2f4d 2022-06-16 stsp
1976 4e4a9ac8 2022-06-17 op *rcol = cols;
1977 4e4a9ac8 2022-06-17 op return i;
1978 ccda2f4d 2022-06-16 stsp }
1979 ccda2f4d 2022-06-16 stsp
1980 ccda2f4d 2022-06-16 stsp /*
1981 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1982 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1983 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1984 ccda2f4d 2022-06-16 stsp */
1985 ccda2f4d 2022-06-16 stsp static const struct got_error *
1986 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1987 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1988 ccda2f4d 2022-06-16 stsp {
1989 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1990 4e4a9ac8 2022-06-17 op int cols;
1991 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1992 145b6838 2022-06-16 stsp char *exstr = NULL;
1993 963b370f 2018-05-20 stsp size_t wlen;
1994 4e4a9ac8 2022-06-17 op int i, scrollx;
1995 963b370f 2018-05-20 stsp
1996 963b370f 2018-05-20 stsp *wlinep = NULL;
1997 b700b5d6 2018-07-10 stsp *widthp = 0;
1998 963b370f 2018-05-20 stsp
1999 145b6838 2022-06-16 stsp if (expand) {
2000 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
2001 145b6838 2022-06-16 stsp if (err)
2002 145b6838 2022-06-16 stsp return err;
2003 145b6838 2022-06-16 stsp }
2004 145b6838 2022-06-16 stsp
2005 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2006 145b6838 2022-06-16 stsp free(exstr);
2007 963b370f 2018-05-20 stsp if (err)
2008 963b370f 2018-05-20 stsp return err;
2009 963b370f 2018-05-20 stsp
2010 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2011 ccda2f4d 2022-06-16 stsp
2012 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2013 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2014 3f670bfb 2020-12-10 stsp wlen--;
2015 3f670bfb 2020-12-10 stsp }
2016 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2017 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2018 3f670bfb 2020-12-10 stsp wlen--;
2019 3f670bfb 2020-12-10 stsp }
2020 3f670bfb 2020-12-10 stsp
2021 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2022 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
2023 27a741e5 2019-09-11 stsp
2024 b700b5d6 2018-07-10 stsp if (widthp)
2025 b700b5d6 2018-07-10 stsp *widthp = cols;
2026 ccda2f4d 2022-06-16 stsp if (scrollxp)
2027 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
2028 963b370f 2018-05-20 stsp if (err)
2029 963b370f 2018-05-20 stsp free(wline);
2030 963b370f 2018-05-20 stsp else
2031 963b370f 2018-05-20 stsp *wlinep = wline;
2032 963b370f 2018-05-20 stsp return err;
2033 963b370f 2018-05-20 stsp }
2034 963b370f 2018-05-20 stsp
2035 8b473291 2019-02-21 stsp static const struct got_error*
2036 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2037 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2038 8b473291 2019-02-21 stsp {
2039 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2040 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2041 8b473291 2019-02-21 stsp char *s;
2042 8b473291 2019-02-21 stsp const char *name;
2043 8b473291 2019-02-21 stsp
2044 8b473291 2019-02-21 stsp *refs_str = NULL;
2045 8b473291 2019-02-21 stsp
2046 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2047 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2048 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2049 52b5abe1 2019-08-13 stsp int cmp;
2050 52b5abe1 2019-08-13 stsp
2051 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2052 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2053 8b473291 2019-02-21 stsp continue;
2054 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2055 8b473291 2019-02-21 stsp name += 5;
2056 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
2057 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
2058 7143d404 2019-03-12 stsp continue;
2059 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2060 8b473291 2019-02-21 stsp name += 6;
2061 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2062 8b473291 2019-02-21 stsp name += 8;
2063 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2064 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2065 79cc719f 2020-04-24 stsp continue;
2066 79cc719f 2020-04-24 stsp }
2067 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2068 48cae60d 2020-09-22 stsp if (err)
2069 48cae60d 2020-09-22 stsp break;
2070 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2071 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2072 5d844a1e 2019-08-13 stsp if (err) {
2073 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2074 48cae60d 2020-09-22 stsp free(ref_id);
2075 5d844a1e 2019-08-13 stsp break;
2076 48cae60d 2020-09-22 stsp }
2077 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2078 5d844a1e 2019-08-13 stsp err = NULL;
2079 5d844a1e 2019-08-13 stsp tag = NULL;
2080 5d844a1e 2019-08-13 stsp }
2081 52b5abe1 2019-08-13 stsp }
2082 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2083 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2084 48cae60d 2020-09-22 stsp free(ref_id);
2085 52b5abe1 2019-08-13 stsp if (tag)
2086 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2087 52b5abe1 2019-08-13 stsp if (cmp != 0)
2088 52b5abe1 2019-08-13 stsp continue;
2089 8b473291 2019-02-21 stsp s = *refs_str;
2090 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2091 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2092 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2093 8b473291 2019-02-21 stsp free(s);
2094 8b473291 2019-02-21 stsp *refs_str = NULL;
2095 8b473291 2019-02-21 stsp break;
2096 8b473291 2019-02-21 stsp }
2097 8b473291 2019-02-21 stsp free(s);
2098 8b473291 2019-02-21 stsp }
2099 8b473291 2019-02-21 stsp
2100 8b473291 2019-02-21 stsp return err;
2101 8b473291 2019-02-21 stsp }
2102 8b473291 2019-02-21 stsp
2103 963b370f 2018-05-20 stsp static const struct got_error *
2104 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2105 27a741e5 2019-09-11 stsp int col_tab_align)
2106 5813d178 2019-03-09 stsp {
2107 e6b8b890 2020-12-29 naddy char *smallerthan;
2108 5813d178 2019-03-09 stsp
2109 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2110 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2111 5813d178 2019-03-09 stsp author = smallerthan + 1;
2112 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2113 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
2114 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
2115 5813d178 2019-03-09 stsp }
2116 5813d178 2019-03-09 stsp
2117 5813d178 2019-03-09 stsp static const struct got_error *
2118 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2119 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2120 8fdc79fe 2020-12-01 naddy int author_display_cols)
2121 80ddbec8 2018-04-29 stsp {
2122 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2123 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2124 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2125 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2126 5813d178 2019-03-09 stsp char *author = NULL;
2127 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
2128 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2129 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2130 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
2131 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2132 ccb26ccd 2018-11-05 stsp struct tm tm;
2133 45d799e2 2018-12-23 stsp time_t committer_time;
2134 11b20872 2019-11-08 stsp struct tog_color *tc;
2135 80ddbec8 2018-04-29 stsp
2136 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2137 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2138 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2139 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2140 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2141 b39d25c7 2018-07-10 stsp
2142 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2143 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2144 b39d25c7 2018-07-10 stsp else
2145 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2146 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2147 11b20872 2019-11-08 stsp if (tc)
2148 11b20872 2019-11-08 stsp wattr_on(view->window,
2149 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2150 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2151 11b20872 2019-11-08 stsp if (tc)
2152 11b20872 2019-11-08 stsp wattr_off(view->window,
2153 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2154 27a741e5 2019-09-11 stsp col = limit;
2155 b39d25c7 2018-07-10 stsp if (col > avail)
2156 b39d25c7 2018-07-10 stsp goto done;
2157 6570a66d 2019-11-08 stsp
2158 6570a66d 2019-11-08 stsp if (avail >= 120) {
2159 6570a66d 2019-11-08 stsp char *id_str;
2160 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2161 6570a66d 2019-11-08 stsp if (err)
2162 6570a66d 2019-11-08 stsp goto done;
2163 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2164 11b20872 2019-11-08 stsp if (tc)
2165 11b20872 2019-11-08 stsp wattr_on(view->window,
2166 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2167 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2168 11b20872 2019-11-08 stsp if (tc)
2169 11b20872 2019-11-08 stsp wattr_off(view->window,
2170 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2171 6570a66d 2019-11-08 stsp free(id_str);
2172 6570a66d 2019-11-08 stsp col += 9;
2173 6570a66d 2019-11-08 stsp if (col > avail)
2174 6570a66d 2019-11-08 stsp goto done;
2175 6570a66d 2019-11-08 stsp }
2176 b39d25c7 2018-07-10 stsp
2177 10aab77f 2022-07-19 op if (s->use_committer)
2178 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2179 10aab77f 2022-07-19 op else
2180 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2181 5813d178 2019-03-09 stsp if (author == NULL) {
2182 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2183 80ddbec8 2018-04-29 stsp goto done;
2184 80ddbec8 2018-04-29 stsp }
2185 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2186 bb737323 2018-05-20 stsp if (err)
2187 bb737323 2018-05-20 stsp goto done;
2188 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2189 11b20872 2019-11-08 stsp if (tc)
2190 11b20872 2019-11-08 stsp wattr_on(view->window,
2191 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2192 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2193 bb737323 2018-05-20 stsp col += author_width;
2194 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2195 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2196 bb737323 2018-05-20 stsp col++;
2197 bb737323 2018-05-20 stsp author_width++;
2198 bb737323 2018-05-20 stsp }
2199 f0f62654 2022-09-08 mark if (tc)
2200 f0f62654 2022-09-08 mark wattr_off(view->window,
2201 f0f62654 2022-09-08 mark COLOR_PAIR(tc->colorpair), NULL);
2202 9c2eaf34 2018-05-20 stsp if (col > avail)
2203 9c2eaf34 2018-05-20 stsp goto done;
2204 80ddbec8 2018-04-29 stsp
2205 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2206 5943eee2 2019-08-13 stsp if (err)
2207 6d9fbc00 2018-04-29 stsp goto done;
2208 bb737323 2018-05-20 stsp logmsg = logmsg0;
2209 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2210 bb737323 2018-05-20 stsp logmsg++;
2211 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2212 bb737323 2018-05-20 stsp if (newline)
2213 bb737323 2018-05-20 stsp *newline = '\0';
2214 ccda2f4d 2022-06-16 stsp limit = avail - col;
2215 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2216 4d1f6af3 2022-06-17 op limit--; /* for the border */
2217 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2218 ccda2f4d 2022-06-16 stsp limit, col, 1);
2219 29688b02 2022-06-16 stsp if (err)
2220 29688b02 2022-06-16 stsp goto done;
2221 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2222 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2223 27a741e5 2019-09-11 stsp while (col < avail) {
2224 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2225 bb737323 2018-05-20 stsp col++;
2226 881b2d3e 2018-04-30 stsp }
2227 80ddbec8 2018-04-29 stsp done:
2228 80ddbec8 2018-04-29 stsp free(logmsg0);
2229 bb737323 2018-05-20 stsp free(wlogmsg);
2230 5813d178 2019-03-09 stsp free(author);
2231 bb737323 2018-05-20 stsp free(wauthor);
2232 80ddbec8 2018-04-29 stsp free(line);
2233 80ddbec8 2018-04-29 stsp return err;
2234 80ddbec8 2018-04-29 stsp }
2235 26ed57b2 2018-05-19 stsp
2236 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2237 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2238 899d86c2 2018-05-10 stsp struct got_object_id *id)
2239 80ddbec8 2018-04-29 stsp {
2240 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2241 e15c42de 2022-09-05 op struct got_object_id *dup;
2242 80ddbec8 2018-04-29 stsp
2243 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2244 80ddbec8 2018-04-29 stsp if (entry == NULL)
2245 899d86c2 2018-05-10 stsp return NULL;
2246 99db9666 2018-05-07 stsp
2247 e15c42de 2022-09-05 op dup = got_object_id_dup(id);
2248 e15c42de 2022-09-05 op if (dup == NULL) {
2249 e15c42de 2022-09-05 op free(entry);
2250 e15c42de 2022-09-05 op return NULL;
2251 e15c42de 2022-09-05 op }
2252 e15c42de 2022-09-05 op
2253 e15c42de 2022-09-05 op entry->id = dup;
2254 99db9666 2018-05-07 stsp entry->commit = commit;
2255 899d86c2 2018-05-10 stsp return entry;
2256 99db9666 2018-05-07 stsp }
2257 80ddbec8 2018-04-29 stsp
2258 99db9666 2018-05-07 stsp static void
2259 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2260 99db9666 2018-05-07 stsp {
2261 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2262 99db9666 2018-05-07 stsp
2263 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2264 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2265 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2266 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2267 e15c42de 2022-09-05 op free(entry->id);
2268 99db9666 2018-05-07 stsp free(entry);
2269 99db9666 2018-05-07 stsp }
2270 99db9666 2018-05-07 stsp
2271 99db9666 2018-05-07 stsp static void
2272 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2273 99db9666 2018-05-07 stsp {
2274 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2275 99db9666 2018-05-07 stsp pop_commit(commits);
2276 c4972b91 2018-05-07 stsp }
2277 c4972b91 2018-05-07 stsp
2278 c4972b91 2018-05-07 stsp static const struct got_error *
2279 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2280 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2281 13add988 2019-10-15 stsp {
2282 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2283 13add988 2019-10-15 stsp regmatch_t regmatch;
2284 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2285 13add988 2019-10-15 stsp
2286 13add988 2019-10-15 stsp *have_match = 0;
2287 13add988 2019-10-15 stsp
2288 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2289 13add988 2019-10-15 stsp if (err)
2290 13add988 2019-10-15 stsp return err;
2291 13add988 2019-10-15 stsp
2292 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2293 13add988 2019-10-15 stsp if (err)
2294 13add988 2019-10-15 stsp goto done;
2295 13add988 2019-10-15 stsp
2296 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2297 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2298 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2299 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2300 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2301 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2302 13add988 2019-10-15 stsp *have_match = 1;
2303 13add988 2019-10-15 stsp done:
2304 13add988 2019-10-15 stsp free(id_str);
2305 13add988 2019-10-15 stsp free(logmsg);
2306 13add988 2019-10-15 stsp return err;
2307 13add988 2019-10-15 stsp }
2308 13add988 2019-10-15 stsp
2309 13add988 2019-10-15 stsp static const struct got_error *
2310 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2311 c4972b91 2018-05-07 stsp {
2312 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2313 9ba79e04 2018-06-11 stsp
2314 1a76625f 2018-10-22 stsp /*
2315 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2316 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2317 1a76625f 2018-10-22 stsp * while updating the display.
2318 1a76625f 2018-10-22 stsp */
2319 4e0d2870 2020-12-07 naddy do {
2320 d9787ed8 2022-09-10 op struct got_object_id id;
2321 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2322 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2323 568eae95 2022-09-11 mark int limit_match = 0;
2324 1a76625f 2018-10-22 stsp int errcode;
2325 899d86c2 2018-05-10 stsp
2326 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2327 4e0d2870 2020-12-07 naddy NULL, NULL);
2328 d9787ed8 2022-09-10 op if (err)
2329 ecb28ae0 2018-07-16 stsp break;
2330 899d86c2 2018-05-10 stsp
2331 d9787ed8 2022-09-10 op err = got_object_open_as_commit(&commit, a->repo, &id);
2332 9ba79e04 2018-06-11 stsp if (err)
2333 9ba79e04 2018-06-11 stsp break;
2334 d9787ed8 2022-09-10 op entry = alloc_commit_queue_entry(commit, &id);
2335 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2336 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2337 9ba79e04 2018-06-11 stsp break;
2338 9ba79e04 2018-06-11 stsp }
2339 93e45b7c 2018-09-24 stsp
2340 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2341 1a76625f 2018-10-22 stsp if (errcode) {
2342 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2343 13add988 2019-10-15 stsp "pthread_mutex_lock");
2344 1a76625f 2018-10-22 stsp break;
2345 1a76625f 2018-10-22 stsp }
2346 1a76625f 2018-10-22 stsp
2347 568eae95 2022-09-11 mark entry->idx = a->real_commits->ncommits;
2348 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2349 568eae95 2022-09-11 mark a->real_commits->ncommits++;
2350 1a76625f 2018-10-22 stsp
2351 568eae95 2022-09-11 mark if (*a->limiting) {
2352 568eae95 2022-09-11 mark err = match_commit(&limit_match, &id, commit,
2353 568eae95 2022-09-11 mark a->limit_regex);
2354 568eae95 2022-09-11 mark if (err)
2355 568eae95 2022-09-11 mark break;
2356 568eae95 2022-09-11 mark
2357 568eae95 2022-09-11 mark if (limit_match) {
2358 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
2359 568eae95 2022-09-11 mark
2360 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(
2361 568eae95 2022-09-11 mark entry->commit, entry->id);
2362 568eae95 2022-09-11 mark if (matched == NULL) {
2363 568eae95 2022-09-11 mark err = got_error_from_errno(
2364 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
2365 568eae95 2022-09-11 mark break;
2366 568eae95 2022-09-11 mark }
2367 34a842a4 2022-09-18 mark matched->commit = entry->commit;
2368 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
2369 568eae95 2022-09-11 mark
2370 568eae95 2022-09-11 mark matched->idx = a->limit_commits->ncommits;
2371 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->limit_commits->head,
2372 568eae95 2022-09-11 mark matched, entry);
2373 568eae95 2022-09-11 mark a->limit_commits->ncommits++;
2374 568eae95 2022-09-11 mark }
2375 568eae95 2022-09-11 mark
2376 568eae95 2022-09-11 mark /*
2377 568eae95 2022-09-11 mark * This is how we signal log_thread() that we
2378 568eae95 2022-09-11 mark * have found a match, and that it should be
2379 568eae95 2022-09-11 mark * counted as a new entry for the view.
2380 568eae95 2022-09-11 mark */
2381 568eae95 2022-09-11 mark a->limit_match = limit_match;
2382 568eae95 2022-09-11 mark }
2383 568eae95 2022-09-11 mark
2384 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2385 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2386 7c1452c1 2020-03-26 stsp int have_match;
2387 d9787ed8 2022-09-10 op err = match_commit(&have_match, &id, commit, a->regex);
2388 7c1452c1 2020-03-26 stsp if (err)
2389 7c1452c1 2020-03-26 stsp break;
2390 568eae95 2022-09-11 mark
2391 568eae95 2022-09-11 mark if (*a->limiting) {
2392 568eae95 2022-09-11 mark if (limit_match && have_match)
2393 568eae95 2022-09-11 mark *a->search_next_done =
2394 568eae95 2022-09-11 mark TOG_SEARCH_HAVE_MORE;
2395 568eae95 2022-09-11 mark } else if (have_match)
2396 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2397 13add988 2019-10-15 stsp }
2398 13add988 2019-10-15 stsp
2399 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2400 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2401 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2402 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2403 7c1452c1 2020-03-26 stsp if (err)
2404 13add988 2019-10-15 stsp break;
2405 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2406 899d86c2 2018-05-10 stsp
2407 9ba79e04 2018-06-11 stsp return err;
2408 0553a4e3 2018-04-30 stsp }
2409 0553a4e3 2018-04-30 stsp
2410 2b779855 2020-12-05 naddy static void
2411 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2412 2b779855 2020-12-05 naddy {
2413 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2414 2b779855 2020-12-05 naddy int ncommits = 0;
2415 2b779855 2020-12-05 naddy
2416 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2417 2b779855 2020-12-05 naddy while (entry) {
2418 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2419 2b779855 2020-12-05 naddy s->selected_entry = entry;
2420 2b779855 2020-12-05 naddy break;
2421 2b779855 2020-12-05 naddy }
2422 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2423 2b779855 2020-12-05 naddy ncommits++;
2424 2b779855 2020-12-05 naddy }
2425 2b779855 2020-12-05 naddy }
2426 2b779855 2020-12-05 naddy
2427 0553a4e3 2018-04-30 stsp static const struct got_error *
2428 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2429 0553a4e3 2018-04-30 stsp {
2430 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2431 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2432 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2433 cbb0c8d7 2022-08-12 mark int limit = view->nlines;
2434 60493ae3 2019-06-20 stsp int width;
2435 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2436 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2437 8b473291 2019-02-21 stsp char *refs_str = NULL;
2438 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2439 11b20872 2019-11-08 stsp struct tog_color *tc;
2440 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2441 cbb0c8d7 2022-08-12 mark
2442 cbb0c8d7 2022-08-12 mark if (view_is_hsplit_top(view))
2443 cbb0c8d7 2022-08-12 mark --limit; /* account for border */
2444 0553a4e3 2018-04-30 stsp
2445 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2446 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2447 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2448 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2449 1a76625f 2018-10-22 stsp if (err)
2450 ecb28ae0 2018-07-16 stsp return err;
2451 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2452 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2453 d2075bf3 2020-12-25 stsp if (refs) {
2454 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2455 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2456 d2075bf3 2020-12-25 stsp if (err)
2457 d2075bf3 2020-12-25 stsp goto done;
2458 d2075bf3 2020-12-25 stsp }
2459 867c6645 2018-07-10 stsp }
2460 359bfafd 2019-02-22 stsp
2461 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2462 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2463 1a76625f 2018-10-22 stsp
2464 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2465 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2466 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2467 38d166d8 2022-09-23 stsp (view->searching && !view->search_next_done) ?
2468 38d166d8 2022-09-23 stsp "searching..." : "loading...") == -1) {
2469 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2470 8f4ed634 2020-03-26 stsp goto done;
2471 8f4ed634 2020-03-26 stsp }
2472 8f4ed634 2020-03-26 stsp } else {
2473 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2474 568eae95 2022-09-11 mark const char *limit_str = NULL;
2475 f9686aa5 2020-03-27 stsp
2476 f9686aa5 2020-03-27 stsp if (view->searching) {
2477 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2478 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2479 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2480 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2481 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2482 f9686aa5 2020-03-27 stsp search_str = "searching...";
2483 f9686aa5 2020-03-27 stsp }
2484 f9686aa5 2020-03-27 stsp
2485 568eae95 2022-09-11 mark if (s->limit_view && s->commits->ncommits == 0)
2486 568eae95 2022-09-11 mark limit_str = "no matches found";
2487 568eae95 2022-09-11 mark
2488 568eae95 2022-09-11 mark if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2489 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2490 568eae95 2022-09-11 mark search_str ? search_str : (refs_str ? refs_str : ""),
2491 568eae95 2022-09-11 mark limit_str ? limit_str : "") == -1) {
2492 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2493 8f4ed634 2020-03-26 stsp goto done;
2494 8f4ed634 2020-03-26 stsp }
2495 8b473291 2019-02-21 stsp }
2496 1a76625f 2018-10-22 stsp
2497 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2498 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2499 87411fa9 2022-06-16 stsp "........................................",
2500 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2501 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2502 1a76625f 2018-10-22 stsp header = NULL;
2503 1a76625f 2018-10-22 stsp goto done;
2504 1a76625f 2018-10-22 stsp }
2505 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2506 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2507 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2508 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2509 1a76625f 2018-10-22 stsp header = NULL;
2510 1a76625f 2018-10-22 stsp goto done;
2511 ecb28ae0 2018-07-16 stsp }
2512 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2513 1a76625f 2018-10-22 stsp if (err)
2514 1a76625f 2018-10-22 stsp goto done;
2515 867c6645 2018-07-10 stsp
2516 2814baeb 2018-08-01 stsp werase(view->window);
2517 867c6645 2018-07-10 stsp
2518 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2519 a3404814 2018-09-02 stsp wstandout(view->window);
2520 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2521 11b20872 2019-11-08 stsp if (tc)
2522 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2523 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2524 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2525 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2526 1a76625f 2018-10-22 stsp width++;
2527 1a76625f 2018-10-22 stsp }
2528 0c6ad1bc 2022-09-09 mark if (tc)
2529 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2530 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2531 a3404814 2018-09-02 stsp wstandend(view->window);
2532 ecb28ae0 2018-07-16 stsp free(wline);
2533 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2534 1a76625f 2018-10-22 stsp goto done;
2535 0553a4e3 2018-04-30 stsp
2536 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2537 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2538 5813d178 2019-03-09 stsp ncommits = 0;
2539 145b6838 2022-06-16 stsp view->maxx = 0;
2540 5813d178 2019-03-09 stsp while (entry) {
2541 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2542 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2543 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2544 5813d178 2019-03-09 stsp int width;
2545 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2546 5813d178 2019-03-09 stsp break;
2547 10aab77f 2022-07-19 op if (s->use_committer)
2548 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2549 10aab77f 2022-07-19 op else
2550 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2551 5813d178 2019-03-09 stsp if (author == NULL) {
2552 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2553 5813d178 2019-03-09 stsp goto done;
2554 5813d178 2019-03-09 stsp }
2555 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2556 27a741e5 2019-09-11 stsp date_display_cols);
2557 5813d178 2019-03-09 stsp if (author_cols < width)
2558 5813d178 2019-03-09 stsp author_cols = width;
2559 5813d178 2019-03-09 stsp free(wauthor);
2560 5813d178 2019-03-09 stsp free(author);
2561 a310d9c3 2022-07-21 florian if (err)
2562 a310d9c3 2022-07-21 florian goto done;
2563 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2564 145b6838 2022-06-16 stsp if (err)
2565 145b6838 2022-06-16 stsp goto done;
2566 145b6838 2022-06-16 stsp msg = msg0;
2567 145b6838 2022-06-16 stsp while (*msg == '\n')
2568 145b6838 2022-06-16 stsp ++msg;
2569 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2570 29688b02 2022-06-16 stsp *eol = '\0';
2571 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2572 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2573 29688b02 2022-06-16 stsp if (err)
2574 29688b02 2022-06-16 stsp goto done;
2575 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2576 145b6838 2022-06-16 stsp free(msg0);
2577 29688b02 2022-06-16 stsp free(wmsg);
2578 7ca04879 2019-10-19 stsp ncommits++;
2579 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2580 5813d178 2019-03-09 stsp }
2581 5813d178 2019-03-09 stsp
2582 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2583 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2584 867c6645 2018-07-10 stsp ncommits = 0;
2585 899d86c2 2018-05-10 stsp while (entry) {
2586 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2587 899d86c2 2018-05-10 stsp break;
2588 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2589 2814baeb 2018-08-01 stsp wstandout(view->window);
2590 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2591 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2592 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2593 2814baeb 2018-08-01 stsp wstandend(view->window);
2594 0553a4e3 2018-04-30 stsp if (err)
2595 60493ae3 2019-06-20 stsp goto done;
2596 0553a4e3 2018-04-30 stsp ncommits++;
2597 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2598 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2599 80ddbec8 2018-04-29 stsp }
2600 80ddbec8 2018-04-29 stsp
2601 9b058f45 2022-06-30 mark view_border(view);
2602 1a76625f 2018-10-22 stsp done:
2603 1a76625f 2018-10-22 stsp free(id_str);
2604 8b473291 2019-02-21 stsp free(refs_str);
2605 1a76625f 2018-10-22 stsp free(ncommits_str);
2606 1a76625f 2018-10-22 stsp free(header);
2607 80ddbec8 2018-04-29 stsp return err;
2608 9f7d7167 2018-04-29 stsp }
2609 07b55e75 2018-05-10 stsp
2610 07b55e75 2018-05-10 stsp static void
2611 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2612 07b55e75 2018-05-10 stsp {
2613 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2614 07b55e75 2018-05-10 stsp int nscrolled = 0;
2615 07b55e75 2018-05-10 stsp
2616 568eae95 2022-09-11 mark entry = TAILQ_FIRST(&s->commits->head);
2617 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2618 07b55e75 2018-05-10 stsp return;
2619 9f7d7167 2018-04-29 stsp
2620 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2621 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2622 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2623 07b55e75 2018-05-10 stsp if (entry) {
2624 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2625 07b55e75 2018-05-10 stsp nscrolled++;
2626 07b55e75 2018-05-10 stsp }
2627 07b55e75 2018-05-10 stsp }
2628 aa075928 2018-05-10 stsp }
2629 aa075928 2018-05-10 stsp
2630 aa075928 2018-05-10 stsp static const struct got_error *
2631 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2632 aa075928 2018-05-10 stsp {
2633 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2634 5e224a3e 2019-02-22 stsp int errcode;
2635 8a42fca8 2019-02-22 stsp
2636 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2637 aa075928 2018-05-10 stsp
2638 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2639 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2640 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2641 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2642 7aafa0d1 2019-02-22 stsp if (errcode)
2643 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2644 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2645 7c1452c1 2020-03-26 stsp
2646 7c1452c1 2020-03-26 stsp /*
2647 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2648 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2649 7c1452c1 2020-03-26 stsp */
2650 7c1452c1 2020-03-26 stsp if (!wait)
2651 7c1452c1 2020-03-26 stsp break;
2652 7c1452c1 2020-03-26 stsp
2653 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2654 ffe38506 2020-12-01 naddy show_log_view(view);
2655 7c1452c1 2020-03-26 stsp update_panels();
2656 7c1452c1 2020-03-26 stsp doupdate();
2657 7c1452c1 2020-03-26 stsp
2658 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2659 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2660 82954512 2020-02-03 stsp if (errcode)
2661 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2662 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2663 82954512 2020-02-03 stsp
2664 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2665 ffe38506 2020-12-01 naddy show_log_view(view);
2666 7c1452c1 2020-03-26 stsp update_panels();
2667 7c1452c1 2020-03-26 stsp doupdate();
2668 5e224a3e 2019-02-22 stsp }
2669 5e224a3e 2019-02-22 stsp
2670 5e224a3e 2019-02-22 stsp return NULL;
2671 5e224a3e 2019-02-22 stsp }
2672 5e224a3e 2019-02-22 stsp
2673 5e224a3e 2019-02-22 stsp static const struct got_error *
2674 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2675 9b058f45 2022-06-30 mark {
2676 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2677 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2678 2525dccb 2022-07-13 mark
2679 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2680 2525dccb 2022-07-13 mark return NULL;
2681 9b058f45 2022-06-30 mark
2682 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2683 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2684 9b058f45 2022-06-30 mark view->nscrolled = 0;
2685 9b058f45 2022-06-30 mark
2686 9b058f45 2022-06-30 mark return err;
2687 9b058f45 2022-06-30 mark }
2688 9b058f45 2022-06-30 mark
2689 9b058f45 2022-06-30 mark static const struct got_error *
2690 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2691 5e224a3e 2019-02-22 stsp {
2692 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2693 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2694 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2695 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2696 5e224a3e 2019-02-22 stsp
2697 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2698 5e224a3e 2019-02-22 stsp return NULL;
2699 5e224a3e 2019-02-22 stsp
2700 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2701 568eae95 2022-09-11 mark if (s->commits->ncommits < ncommits_needed &&
2702 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2703 08ebd0a9 2019-02-22 stsp /*
2704 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2705 08ebd0a9 2019-02-22 stsp */
2706 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2707 568eae95 2022-09-11 mark ncommits_needed - s->commits->ncommits;
2708 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2709 5e224a3e 2019-02-22 stsp if (err)
2710 5e224a3e 2019-02-22 stsp return err;
2711 7aafa0d1 2019-02-22 stsp }
2712 b295e71b 2019-02-22 stsp
2713 7aafa0d1 2019-02-22 stsp do {
2714 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2715 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2716 88048b54 2019-02-21 stsp break;
2717 88048b54 2019-02-21 stsp
2718 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2719 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2720 aa075928 2018-05-10 stsp
2721 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2722 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2723 dd0a52c1 2018-05-20 stsp break;
2724 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2725 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2726 aa075928 2018-05-10 stsp
2727 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2728 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2729 9b058f45 2022-06-30 mark else
2730 9b058f45 2022-06-30 mark view->nscrolled = 0;
2731 9b058f45 2022-06-30 mark
2732 dd0a52c1 2018-05-20 stsp return err;
2733 07b55e75 2018-05-10 stsp }
2734 4a7f7875 2018-05-10 stsp
2735 cd0acaa7 2018-05-20 stsp static const struct got_error *
2736 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2737 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2738 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2739 cd0acaa7 2018-05-20 stsp {
2740 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2741 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2742 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2743 cd0acaa7 2018-05-20 stsp
2744 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2745 15a94983 2018-12-23 stsp if (diff_view == NULL)
2746 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2747 ea5e7bb5 2018-08-01 stsp
2748 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2749 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2750 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2751 e5a0f69f 2018-08-18 stsp if (err == NULL)
2752 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2753 cd0acaa7 2018-05-20 stsp return err;
2754 4a7f7875 2018-05-10 stsp }
2755 4a7f7875 2018-05-10 stsp
2756 80ddbec8 2018-04-29 stsp static const struct got_error *
2757 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2758 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2759 9343a5fb 2018-06-23 stsp {
2760 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2761 941e9f74 2019-05-21 stsp
2762 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2763 941e9f74 2019-05-21 stsp if (parent == NULL)
2764 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2765 941e9f74 2019-05-21 stsp
2766 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2767 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2768 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2769 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2770 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2771 941e9f74 2019-05-21 stsp s->tree = subtree;
2772 941e9f74 2019-05-21 stsp s->selected = 0;
2773 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2774 941e9f74 2019-05-21 stsp return NULL;
2775 941e9f74 2019-05-21 stsp }
2776 941e9f74 2019-05-21 stsp
2777 941e9f74 2019-05-21 stsp static const struct got_error *
2778 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2779 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2780 941e9f74 2019-05-21 stsp {
2781 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2782 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2783 941e9f74 2019-05-21 stsp const char *p;
2784 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2785 9343a5fb 2018-06-23 stsp
2786 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2787 941e9f74 2019-05-21 stsp p = path;
2788 941e9f74 2019-05-21 stsp while (*p) {
2789 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2790 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2791 56e0773d 2019-11-28 stsp char *te_name;
2792 33cbf02b 2020-01-12 stsp
2793 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2794 33cbf02b 2020-01-12 stsp p++;
2795 941e9f74 2019-05-21 stsp
2796 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2797 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2798 941e9f74 2019-05-21 stsp if (slash == NULL)
2799 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2800 33cbf02b 2020-01-12 stsp else
2801 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2802 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2803 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2804 56e0773d 2019-11-28 stsp break;
2805 941e9f74 2019-05-21 stsp }
2806 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2807 56e0773d 2019-11-28 stsp if (te == NULL) {
2808 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2809 56e0773d 2019-11-28 stsp free(te_name);
2810 941e9f74 2019-05-21 stsp break;
2811 941e9f74 2019-05-21 stsp }
2812 56e0773d 2019-11-28 stsp free(te_name);
2813 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2814 941e9f74 2019-05-21 stsp
2815 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2816 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2817 b03c880f 2019-05-21 stsp
2818 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2819 941e9f74 2019-05-21 stsp if (slash)
2820 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2821 941e9f74 2019-05-21 stsp else
2822 941e9f74 2019-05-21 stsp subpath = strdup(path);
2823 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2824 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2825 941e9f74 2019-05-21 stsp break;
2826 941e9f74 2019-05-21 stsp }
2827 941e9f74 2019-05-21 stsp
2828 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2829 941e9f74 2019-05-21 stsp subpath);
2830 941e9f74 2019-05-21 stsp if (err)
2831 941e9f74 2019-05-21 stsp break;
2832 941e9f74 2019-05-21 stsp
2833 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2834 941e9f74 2019-05-21 stsp free(tree_id);
2835 941e9f74 2019-05-21 stsp if (err)
2836 941e9f74 2019-05-21 stsp break;
2837 941e9f74 2019-05-21 stsp
2838 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2839 941e9f74 2019-05-21 stsp if (err) {
2840 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2841 941e9f74 2019-05-21 stsp break;
2842 941e9f74 2019-05-21 stsp }
2843 941e9f74 2019-05-21 stsp if (slash == NULL)
2844 941e9f74 2019-05-21 stsp break;
2845 941e9f74 2019-05-21 stsp free(subpath);
2846 941e9f74 2019-05-21 stsp subpath = NULL;
2847 941e9f74 2019-05-21 stsp p = slash;
2848 941e9f74 2019-05-21 stsp }
2849 941e9f74 2019-05-21 stsp
2850 941e9f74 2019-05-21 stsp free(subpath);
2851 1a76625f 2018-10-22 stsp return err;
2852 61266923 2020-01-14 stsp }
2853 61266923 2020-01-14 stsp
2854 61266923 2020-01-14 stsp static const struct got_error *
2855 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2856 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2857 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2858 55cccc34 2020-02-20 stsp {
2859 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2860 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2861 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2862 55cccc34 2020-02-20 stsp
2863 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2864 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2865 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2866 55cccc34 2020-02-20 stsp
2867 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2868 bc573f3b 2021-07-10 stsp if (err)
2869 55cccc34 2020-02-20 stsp return err;
2870 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2871 55cccc34 2020-02-20 stsp
2872 55cccc34 2020-02-20 stsp *new_view = tree_view;
2873 55cccc34 2020-02-20 stsp
2874 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2875 55cccc34 2020-02-20 stsp return NULL;
2876 55cccc34 2020-02-20 stsp
2877 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2878 55cccc34 2020-02-20 stsp }
2879 55cccc34 2020-02-20 stsp
2880 55cccc34 2020-02-20 stsp static const struct got_error *
2881 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2882 61266923 2020-01-14 stsp {
2883 61266923 2020-01-14 stsp sigset_t sigset;
2884 61266923 2020-01-14 stsp int errcode;
2885 61266923 2020-01-14 stsp
2886 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2887 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2888 61266923 2020-01-14 stsp
2889 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2890 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2891 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2892 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2893 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2894 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2895 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2896 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2897 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2898 61266923 2020-01-14 stsp
2899 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2900 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2901 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2902 61266923 2020-01-14 stsp
2903 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2904 61266923 2020-01-14 stsp if (errcode)
2905 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2906 61266923 2020-01-14 stsp
2907 61266923 2020-01-14 stsp return NULL;
2908 1a76625f 2018-10-22 stsp }
2909 1a76625f 2018-10-22 stsp
2910 1a76625f 2018-10-22 stsp static void *
2911 1a76625f 2018-10-22 stsp log_thread(void *arg)
2912 1a76625f 2018-10-22 stsp {
2913 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2914 1a76625f 2018-10-22 stsp int errcode = 0;
2915 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2916 1a76625f 2018-10-22 stsp int done = 0;
2917 61266923 2020-01-14 stsp
2918 5629093a 2022-07-11 stsp /*
2919 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2920 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2921 5629093a 2022-07-11 stsp */
2922 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2923 5629093a 2022-07-11 stsp if (errcode) {
2924 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2925 61266923 2020-01-14 stsp return (void *)err;
2926 5629093a 2022-07-11 stsp }
2927 1a76625f 2018-10-22 stsp
2928 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2929 5629093a 2022-07-11 stsp if (err) {
2930 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2931 5629093a 2022-07-11 stsp goto done;
2932 5629093a 2022-07-11 stsp }
2933 5629093a 2022-07-11 stsp
2934 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2935 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2936 5629093a 2022-07-11 stsp if (errcode) {
2937 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2938 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2939 5629093a 2022-07-11 stsp goto done;
2940 5629093a 2022-07-11 stsp }
2941 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2942 1a76625f 2018-10-22 stsp if (err) {
2943 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2944 5629093a 2022-07-11 stsp goto done;
2945 1a76625f 2018-10-22 stsp err = NULL;
2946 1a76625f 2018-10-22 stsp done = 1;
2947 568eae95 2022-09-11 mark } else if (a->commits_needed > 0 && !a->load_all) {
2948 568eae95 2022-09-11 mark if (*a->limiting) {
2949 568eae95 2022-09-11 mark if (a->limit_match)
2950 568eae95 2022-09-11 mark a->commits_needed--;
2951 568eae95 2022-09-11 mark } else
2952 568eae95 2022-09-11 mark a->commits_needed--;
2953 568eae95 2022-09-11 mark }
2954 1a76625f 2018-10-22 stsp
2955 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2956 3abe8080 2019-04-10 stsp if (errcode) {
2957 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2958 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2959 5629093a 2022-07-11 stsp goto done;
2960 3abe8080 2019-04-10 stsp } else if (*a->quit)
2961 1a76625f 2018-10-22 stsp done = 1;
2962 568eae95 2022-09-11 mark else if (*a->limiting && *a->first_displayed_entry == NULL) {
2963 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2964 568eae95 2022-09-11 mark TAILQ_FIRST(&a->limit_commits->head);
2965 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2966 568eae95 2022-09-11 mark } else if (*a->first_displayed_entry == NULL) {
2967 568eae95 2022-09-11 mark *a->first_displayed_entry =
2968 568eae95 2022-09-11 mark TAILQ_FIRST(&a->real_commits->head);
2969 568eae95 2022-09-11 mark *a->selected_entry = *a->first_displayed_entry;
2970 1a76625f 2018-10-22 stsp }
2971 1a76625f 2018-10-22 stsp
2972 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2973 7c1452c1 2020-03-26 stsp if (errcode) {
2974 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2975 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2976 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2977 5629093a 2022-07-11 stsp goto done;
2978 7c1452c1 2020-03-26 stsp }
2979 7c1452c1 2020-03-26 stsp
2980 1a76625f 2018-10-22 stsp if (done)
2981 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2982 7c1452c1 2020-03-26 stsp else {
2983 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2984 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2985 7c1452c1 2020-03-26 stsp &tog_mutex);
2986 5629093a 2022-07-11 stsp if (errcode) {
2987 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2988 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2989 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2990 5629093a 2022-07-11 stsp goto done;
2991 5629093a 2022-07-11 stsp }
2992 21355643 2020-12-06 stsp if (*a->quit)
2993 21355643 2020-12-06 stsp done = 1;
2994 7c1452c1 2020-03-26 stsp }
2995 1a76625f 2018-10-22 stsp }
2996 1a76625f 2018-10-22 stsp }
2997 3abe8080 2019-04-10 stsp a->log_complete = 1;
2998 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2999 5629093a 2022-07-11 stsp if (errcode)
3000 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3001 5629093a 2022-07-11 stsp done:
3002 5629093a 2022-07-11 stsp if (err) {
3003 5629093a 2022-07-11 stsp tog_thread_error = 1;
3004 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
3005 5629093a 2022-07-11 stsp }
3006 1a76625f 2018-10-22 stsp return (void *)err;
3007 1a76625f 2018-10-22 stsp }
3008 1a76625f 2018-10-22 stsp
3009 1a76625f 2018-10-22 stsp static const struct got_error *
3010 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3011 1a76625f 2018-10-22 stsp {
3012 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
3013 1a76625f 2018-10-22 stsp int errcode;
3014 1a76625f 2018-10-22 stsp
3015 1a76625f 2018-10-22 stsp if (s->thread) {
3016 1a76625f 2018-10-22 stsp s->quit = 1;
3017 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3018 1a76625f 2018-10-22 stsp if (errcode)
3019 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3020 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3021 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3022 1a76625f 2018-10-22 stsp if (errcode)
3023 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3024 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3025 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
3026 1a76625f 2018-10-22 stsp if (errcode)
3027 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3028 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3029 1a76625f 2018-10-22 stsp if (errcode)
3030 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3031 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3032 1a76625f 2018-10-22 stsp s->thread = NULL;
3033 1a76625f 2018-10-22 stsp }
3034 1a76625f 2018-10-22 stsp
3035 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3036 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3037 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3038 74467cc8 2022-06-15 stsp }
3039 74467cc8 2022-06-15 stsp
3040 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
3041 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
3042 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
3043 74467cc8 2022-06-15 stsp if (err == NULL)
3044 74467cc8 2022-06-15 stsp err = pack_err;
3045 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
3046 1a76625f 2018-10-22 stsp }
3047 1a76625f 2018-10-22 stsp
3048 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3049 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3050 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3051 1a76625f 2018-10-22 stsp }
3052 1a76625f 2018-10-22 stsp
3053 5629093a 2022-07-11 stsp return err ? err : thread_err;
3054 9343a5fb 2018-06-23 stsp }
3055 9343a5fb 2018-06-23 stsp
3056 9343a5fb 2018-06-23 stsp static const struct got_error *
3057 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3058 1a76625f 2018-10-22 stsp {
3059 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3060 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3061 276b94a1 2020-11-13 naddy int errcode;
3062 1a76625f 2018-10-22 stsp
3063 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3064 276b94a1 2020-11-13 naddy
3065 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3066 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3067 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3068 276b94a1 2020-11-13 naddy
3069 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3070 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3071 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3072 276b94a1 2020-11-13 naddy
3073 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3074 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3075 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3076 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3077 1a76625f 2018-10-22 stsp free(s->start_id);
3078 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3079 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3080 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3081 1a76625f 2018-10-22 stsp return err;
3082 1a76625f 2018-10-22 stsp }
3083 1a76625f 2018-10-22 stsp
3084 568eae95 2022-09-11 mark /*
3085 568eae95 2022-09-11 mark * We use two queues to implement the limit feature: first consists of
3086 568eae95 2022-09-11 mark * commits matching the current limit_regex; second is the real queue
3087 568eae95 2022-09-11 mark * of all known commits (real_commits). When the user starts limiting,
3088 568eae95 2022-09-11 mark * we swap queues such that all movement and displaying functionality
3089 568eae95 2022-09-11 mark * works with very slight change.
3090 568eae95 2022-09-11 mark */
3091 1a76625f 2018-10-22 stsp static const struct got_error *
3092 568eae95 2022-09-11 mark limit_log_view(struct tog_view *view)
3093 568eae95 2022-09-11 mark {
3094 568eae95 2022-09-11 mark struct tog_log_view_state *s = &view->state.log;
3095 568eae95 2022-09-11 mark struct commit_queue_entry *entry;
3096 568eae95 2022-09-11 mark struct tog_view *v = view;
3097 568eae95 2022-09-11 mark const struct got_error *err = NULL;
3098 568eae95 2022-09-11 mark char pattern[1024];
3099 568eae95 2022-09-11 mark int ret;
3100 568eae95 2022-09-11 mark
3101 568eae95 2022-09-11 mark if (view_is_hsplit_top(view))
3102 568eae95 2022-09-11 mark v = view->child;
3103 568eae95 2022-09-11 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3104 568eae95 2022-09-11 mark v = view->parent;
3105 568eae95 2022-09-11 mark
3106 568eae95 2022-09-11 mark /* Get the pattern */
3107 568eae95 2022-09-11 mark wmove(v->window, v->nlines - 1, 0);
3108 568eae95 2022-09-11 mark wclrtoeol(v->window);
3109 568eae95 2022-09-11 mark mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3110 568eae95 2022-09-11 mark nodelay(v->window, FALSE);
3111 568eae95 2022-09-11 mark nocbreak();
3112 568eae95 2022-09-11 mark echo();
3113 568eae95 2022-09-11 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
3114 568eae95 2022-09-11 mark cbreak();
3115 568eae95 2022-09-11 mark noecho();
3116 568eae95 2022-09-11 mark nodelay(v->window, TRUE);
3117 568eae95 2022-09-11 mark if (ret == ERR)
3118 568eae95 2022-09-11 mark return NULL;
3119 568eae95 2022-09-11 mark
3120 568eae95 2022-09-11 mark if (*pattern == '\0') {
3121 568eae95 2022-09-11 mark /*
3122 568eae95 2022-09-11 mark * Safety measure for the situation where the user
3123 568eae95 2022-09-11 mark * resets limit without previously limiting anything.
3124 568eae95 2022-09-11 mark */
3125 568eae95 2022-09-11 mark if (!s->limit_view)
3126 568eae95 2022-09-11 mark return NULL;
3127 568eae95 2022-09-11 mark
3128 568eae95 2022-09-11 mark /*
3129 568eae95 2022-09-11 mark * User could have pressed Ctrl+L, which refreshed the
3130 568eae95 2022-09-11 mark * commit queues, it means we can't save previously
3131 568eae95 2022-09-11 mark * (before limit took place) displayed entries,
3132 568eae95 2022-09-11 mark * because they would point to already free'ed memory,
3133 568eae95 2022-09-11 mark * so we are forced to always select first entry of
3134 568eae95 2022-09-11 mark * the queue.
3135 568eae95 2022-09-11 mark */
3136 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3137 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3138 568eae95 2022-09-11 mark s->selected_entry = s->first_displayed_entry;
3139 568eae95 2022-09-11 mark s->selected = 0;
3140 568eae95 2022-09-11 mark s->limit_view = 0;
3141 568eae95 2022-09-11 mark
3142 568eae95 2022-09-11 mark return NULL;
3143 568eae95 2022-09-11 mark }
3144 568eae95 2022-09-11 mark
3145 568eae95 2022-09-11 mark if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3146 568eae95 2022-09-11 mark return NULL;
3147 568eae95 2022-09-11 mark
3148 568eae95 2022-09-11 mark s->limit_view = 1;
3149 568eae95 2022-09-11 mark
3150 568eae95 2022-09-11 mark /* Clear the screen while loading limit view */
3151 568eae95 2022-09-11 mark s->first_displayed_entry = NULL;
3152 568eae95 2022-09-11 mark s->last_displayed_entry = NULL;
3153 568eae95 2022-09-11 mark s->selected_entry = NULL;
3154 568eae95 2022-09-11 mark s->commits = &s->limit_commits;
3155 568eae95 2022-09-11 mark
3156 568eae95 2022-09-11 mark /* Prepare limit queue for new search */
3157 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3158 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3159 568eae95 2022-09-11 mark
3160 568eae95 2022-09-11 mark /* First process commits, which are in queue already */
3161 568eae95 2022-09-11 mark TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3162 568eae95 2022-09-11 mark int have_match = 0;
3163 568eae95 2022-09-11 mark
3164 568eae95 2022-09-11 mark err = match_commit(&have_match, entry->id,
3165 568eae95 2022-09-11 mark entry->commit, &s->limit_regex);
3166 568eae95 2022-09-11 mark if (err)
3167 568eae95 2022-09-11 mark return err;
3168 568eae95 2022-09-11 mark
3169 568eae95 2022-09-11 mark if (have_match) {
3170 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
3171 568eae95 2022-09-11 mark
3172 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(entry->commit,
3173 568eae95 2022-09-11 mark entry->id);
3174 568eae95 2022-09-11 mark if (matched == NULL) {
3175 568eae95 2022-09-11 mark err = got_error_from_errno(
3176 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
3177 568eae95 2022-09-11 mark break;
3178 568eae95 2022-09-11 mark }
3179 34a842a4 2022-09-18 mark matched->commit = entry->commit;
3180 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
3181 568eae95 2022-09-11 mark
3182 568eae95 2022-09-11 mark matched->idx = s->limit_commits.ncommits;
3183 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&s->limit_commits.head,
3184 568eae95 2022-09-11 mark matched, entry);
3185 568eae95 2022-09-11 mark s->limit_commits.ncommits++;
3186 568eae95 2022-09-11 mark }
3187 568eae95 2022-09-11 mark }
3188 568eae95 2022-09-11 mark
3189 568eae95 2022-09-11 mark /* Second process all the commits, until we fill the screen */
3190 568eae95 2022-09-11 mark if (s->limit_commits.ncommits < view->nlines - 1 &&
3191 568eae95 2022-09-11 mark !s->thread_args.log_complete) {
3192 568eae95 2022-09-11 mark s->thread_args.commits_needed +=
3193 568eae95 2022-09-11 mark view->nlines - s->limit_commits.ncommits - 1;
3194 568eae95 2022-09-11 mark err = trigger_log_thread(view, 1);
3195 568eae95 2022-09-11 mark if (err)
3196 568eae95 2022-09-11 mark return err;
3197 568eae95 2022-09-11 mark }
3198 568eae95 2022-09-11 mark
3199 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3200 568eae95 2022-09-11 mark s->selected_entry = TAILQ_FIRST(&s->commits->head);
3201 568eae95 2022-09-11 mark s->selected = 0;
3202 568eae95 2022-09-11 mark
3203 568eae95 2022-09-11 mark return NULL;
3204 568eae95 2022-09-11 mark }
3205 568eae95 2022-09-11 mark
3206 568eae95 2022-09-11 mark static const struct got_error *
3207 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3208 60493ae3 2019-06-20 stsp {
3209 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3210 60493ae3 2019-06-20 stsp
3211 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3212 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3213 60493ae3 2019-06-20 stsp return NULL;
3214 60493ae3 2019-06-20 stsp }
3215 60493ae3 2019-06-20 stsp
3216 60493ae3 2019-06-20 stsp static const struct got_error *
3217 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3218 60493ae3 2019-06-20 stsp {
3219 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3220 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3221 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3222 60493ae3 2019-06-20 stsp
3223 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3224 f9686aa5 2020-03-27 stsp show_log_view(view);
3225 f9686aa5 2020-03-27 stsp update_panels();
3226 f9686aa5 2020-03-27 stsp doupdate();
3227 f9686aa5 2020-03-27 stsp
3228 96e2b566 2019-07-08 stsp if (s->search_entry) {
3229 13add988 2019-10-15 stsp int errcode, ch;
3230 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3231 13add988 2019-10-15 stsp if (errcode)
3232 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3233 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3234 13add988 2019-10-15 stsp ch = wgetch(view->window);
3235 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3236 13add988 2019-10-15 stsp if (errcode)
3237 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3238 13add988 2019-10-15 stsp "pthread_mutex_lock");
3239 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3240 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3241 678cbce5 2019-07-28 stsp return NULL;
3242 678cbce5 2019-07-28 stsp }
3243 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3244 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3245 96e2b566 2019-07-08 stsp else
3246 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3247 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3248 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3249 364ac6fd 2022-06-18 stsp /*
3250 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
3251 f704b35c 2022-06-18 stsp * the position from where we should continue searching
3252 f704b35c 2022-06-18 stsp * might have changed.
3253 364ac6fd 2022-06-18 stsp */
3254 f83ada34 2022-09-13 mark if (view->searching == TOG_SEARCH_FORWARD)
3255 f83ada34 2022-09-13 mark entry = TAILQ_NEXT(s->selected_entry, entry);
3256 f83ada34 2022-09-13 mark else
3257 f83ada34 2022-09-13 mark entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3258 f83ada34 2022-09-13 mark entry);
3259 20be8d96 2019-06-21 stsp } else {
3260 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
3261 20be8d96 2019-06-21 stsp }
3262 60493ae3 2019-06-20 stsp
3263 60493ae3 2019-06-20 stsp while (1) {
3264 13add988 2019-10-15 stsp int have_match = 0;
3265 13add988 2019-10-15 stsp
3266 60493ae3 2019-06-20 stsp if (entry == NULL) {
3267 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3268 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3269 f9967bca 2020-03-27 stsp view->search_next_done =
3270 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3271 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3272 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3273 f801134a 2019-06-25 stsp return NULL;
3274 60493ae3 2019-06-20 stsp }
3275 96e2b566 2019-07-08 stsp /*
3276 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3277 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3278 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3279 96e2b566 2019-07-08 stsp */
3280 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3281 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3282 60493ae3 2019-06-20 stsp }
3283 60493ae3 2019-06-20 stsp
3284 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3285 13add988 2019-10-15 stsp &view->regex);
3286 5943eee2 2019-08-13 stsp if (err)
3287 13add988 2019-10-15 stsp break;
3288 13add988 2019-10-15 stsp if (have_match) {
3289 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3290 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3291 60493ae3 2019-06-20 stsp break;
3292 60493ae3 2019-06-20 stsp }
3293 13add988 2019-10-15 stsp
3294 96e2b566 2019-07-08 stsp s->search_entry = entry;
3295 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3296 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3297 b1bf1435 2019-06-21 stsp else
3298 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3299 60493ae3 2019-06-20 stsp }
3300 60493ae3 2019-06-20 stsp
3301 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3302 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3303 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3304 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3305 60493ae3 2019-06-20 stsp if (err)
3306 60493ae3 2019-06-20 stsp return err;
3307 ead14cbe 2019-06-21 stsp cur++;
3308 ead14cbe 2019-06-21 stsp }
3309 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3310 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3311 60493ae3 2019-06-20 stsp if (err)
3312 60493ae3 2019-06-20 stsp return err;
3313 ead14cbe 2019-06-21 stsp cur--;
3314 60493ae3 2019-06-20 stsp }
3315 60493ae3 2019-06-20 stsp }
3316 60493ae3 2019-06-20 stsp
3317 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3318 96e2b566 2019-07-08 stsp
3319 60493ae3 2019-06-20 stsp return NULL;
3320 60493ae3 2019-06-20 stsp }
3321 60493ae3 2019-06-20 stsp
3322 60493ae3 2019-06-20 stsp static const struct got_error *
3323 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3324 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3325 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3326 80ddbec8 2018-04-29 stsp {
3327 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3328 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3329 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3330 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3331 1a76625f 2018-10-22 stsp int errcode;
3332 80ddbec8 2018-04-29 stsp
3333 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3334 f135c941 2020-02-20 stsp free(s->in_repo_path);
3335 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3336 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3337 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3338 f135c941 2020-02-20 stsp }
3339 ecb28ae0 2018-07-16 stsp
3340 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3341 568eae95 2022-09-11 mark TAILQ_INIT(&s->real_commits.head);
3342 568eae95 2022-09-11 mark s->real_commits.ncommits = 0;
3343 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3344 78756c87 2020-11-24 stsp
3345 568eae95 2022-09-11 mark TAILQ_INIT(&s->limit_commits.head);
3346 568eae95 2022-09-11 mark s->limit_view = 0;
3347 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3348 568eae95 2022-09-11 mark
3349 fb2756b9 2018-08-04 stsp s->repo = repo;
3350 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3351 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3352 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3353 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3354 9cd7cbd1 2020-12-07 stsp goto done;
3355 9cd7cbd1 2020-12-07 stsp }
3356 9cd7cbd1 2020-12-07 stsp }
3357 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3358 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3359 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3360 5036bf37 2018-09-24 stsp goto done;
3361 5036bf37 2018-09-24 stsp }
3362 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3363 e5a0f69f 2018-08-18 stsp
3364 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3365 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3366 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3367 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3368 11b20872 2019-11-08 stsp if (err)
3369 11b20872 2019-11-08 stsp goto done;
3370 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3371 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3372 11b20872 2019-11-08 stsp if (err) {
3373 11b20872 2019-11-08 stsp free_colors(&s->colors);
3374 11b20872 2019-11-08 stsp goto done;
3375 11b20872 2019-11-08 stsp }
3376 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3377 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3378 11b20872 2019-11-08 stsp if (err) {
3379 11b20872 2019-11-08 stsp free_colors(&s->colors);
3380 11b20872 2019-11-08 stsp goto done;
3381 11b20872 2019-11-08 stsp }
3382 11b20872 2019-11-08 stsp }
3383 11b20872 2019-11-08 stsp
3384 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3385 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3386 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3387 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3388 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3389 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3390 1a76625f 2018-10-22 stsp
3391 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3392 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3393 74467cc8 2022-06-15 stsp if (err)
3394 74467cc8 2022-06-15 stsp goto done;
3395 74467cc8 2022-06-15 stsp }
3396 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3397 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3398 0ae84acc 2022-06-15 tracey if (err)
3399 0ae84acc 2022-06-15 tracey goto done;
3400 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3401 b672a97a 2020-01-27 stsp !s->log_branches);
3402 1a76625f 2018-10-22 stsp if (err)
3403 1a76625f 2018-10-22 stsp goto done;
3404 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3405 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3406 c5b78334 2020-01-12 stsp if (err)
3407 c5b78334 2020-01-12 stsp goto done;
3408 1a76625f 2018-10-22 stsp
3409 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3410 1a76625f 2018-10-22 stsp if (errcode) {
3411 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3412 1a76625f 2018-10-22 stsp goto done;
3413 1a76625f 2018-10-22 stsp }
3414 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3415 7c1452c1 2020-03-26 stsp if (errcode) {
3416 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3417 7c1452c1 2020-03-26 stsp goto done;
3418 7c1452c1 2020-03-26 stsp }
3419 1a76625f 2018-10-22 stsp
3420 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3421 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3422 568eae95 2022-09-11 mark s->thread_args.real_commits = &s->real_commits;
3423 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3424 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3425 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3426 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3427 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3428 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3429 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3430 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3431 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3432 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3433 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3434 568eae95 2022-09-11 mark s->thread_args.limiting = &s->limit_view;
3435 568eae95 2022-09-11 mark s->thread_args.limit_regex = &s->limit_regex;
3436 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3437 ba4f502b 2018-08-04 stsp done:
3438 1a76625f 2018-10-22 stsp if (err)
3439 1a76625f 2018-10-22 stsp close_log_view(view);
3440 ba4f502b 2018-08-04 stsp return err;
3441 ba4f502b 2018-08-04 stsp }
3442 ba4f502b 2018-08-04 stsp
3443 e5a0f69f 2018-08-18 stsp static const struct got_error *
3444 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3445 ba4f502b 2018-08-04 stsp {
3446 f2f6d207 2020-11-24 stsp const struct got_error *err;
3447 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3448 ba4f502b 2018-08-04 stsp
3449 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3450 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3451 2b380cc8 2018-10-24 stsp &s->thread_args);
3452 2b380cc8 2018-10-24 stsp if (errcode)
3453 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3454 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3455 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3456 f2f6d207 2020-11-24 stsp if (err)
3457 f2f6d207 2020-11-24 stsp return err;
3458 f2f6d207 2020-11-24 stsp }
3459 2b380cc8 2018-10-24 stsp }
3460 2b380cc8 2018-10-24 stsp
3461 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3462 b880cc75 2022-06-30 mark }
3463 b880cc75 2022-06-30 mark
3464 b880cc75 2022-06-30 mark static void
3465 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3466 b880cc75 2022-06-30 mark {
3467 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3468 b880cc75 2022-06-30 mark
3469 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3470 b880cc75 2022-06-30 mark return;
3471 568eae95 2022-09-11 mark if (s->selected_entry->idx == 0)
3472 568eae95 2022-09-11 mark view->count = 0;
3473 b880cc75 2022-06-30 mark
3474 568eae95 2022-09-11 mark if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3475 b880cc75 2022-06-30 mark || home)
3476 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3477 b880cc75 2022-06-30 mark
3478 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3479 b880cc75 2022-06-30 mark --s->selected;
3480 b880cc75 2022-06-30 mark else
3481 568eae95 2022-09-11 mark log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3482 b880cc75 2022-06-30 mark
3483 b880cc75 2022-06-30 mark select_commit(s);
3484 b880cc75 2022-06-30 mark return;
3485 b880cc75 2022-06-30 mark }
3486 b880cc75 2022-06-30 mark
3487 b880cc75 2022-06-30 mark static const struct got_error *
3488 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3489 b880cc75 2022-06-30 mark {
3490 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3491 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3492 631e7531 2022-08-12 mark int eos = view->nlines - 2;
3493 b880cc75 2022-06-30 mark
3494 568eae95 2022-09-11 mark if (s->first_displayed_entry == NULL)
3495 568eae95 2022-09-11 mark return NULL;
3496 568eae95 2022-09-11 mark
3497 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3498 568eae95 2022-09-11 mark s->selected_entry->idx >= s->commits->ncommits - 1)
3499 b880cc75 2022-06-30 mark return NULL;
3500 b880cc75 2022-06-30 mark
3501 631e7531 2022-08-12 mark if (view_is_hsplit_top(view))
3502 631e7531 2022-08-12 mark --eos; /* border consumes the last line */
3503 b880cc75 2022-06-30 mark
3504 631e7531 2022-08-12 mark if (!page) {
3505 568eae95 2022-09-11 mark if (s->selected < MIN(eos, s->commits->ncommits - 1))
3506 b880cc75 2022-06-30 mark ++s->selected;
3507 b880cc75 2022-06-30 mark else
3508 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3509 11edf34c 2022-08-12 mark } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3510 631e7531 2022-08-12 mark struct commit_queue_entry *entry;
3511 631e7531 2022-08-12 mark int n;
3512 631e7531 2022-08-12 mark
3513 631e7531 2022-08-12 mark s->selected = 0;
3514 568eae95 2022-09-11 mark entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3515 631e7531 2022-08-12 mark s->last_displayed_entry = entry;
3516 631e7531 2022-08-12 mark for (n = 0; n <= eos; n++) {
3517 631e7531 2022-08-12 mark if (entry == NULL)
3518 631e7531 2022-08-12 mark break;
3519 631e7531 2022-08-12 mark s->first_displayed_entry = entry;
3520 631e7531 2022-08-12 mark entry = TAILQ_PREV(entry, commit_queue_head, entry);
3521 631e7531 2022-08-12 mark }
3522 631e7531 2022-08-12 mark if (n > 0)
3523 631e7531 2022-08-12 mark s->selected = n - 1;
3524 b880cc75 2022-06-30 mark } else {
3525 568eae95 2022-09-11 mark if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3526 cbb0c8d7 2022-08-12 mark s->thread_args.log_complete)
3527 cbb0c8d7 2022-08-12 mark s->selected += MIN(page,
3528 568eae95 2022-09-11 mark s->commits->ncommits - s->selected_entry->idx - 1);
3529 cbb0c8d7 2022-08-12 mark else
3530 cbb0c8d7 2022-08-12 mark err = log_scroll_down(view, page);
3531 b880cc75 2022-06-30 mark }
3532 b880cc75 2022-06-30 mark if (err)
3533 b880cc75 2022-06-30 mark return err;
3534 b880cc75 2022-06-30 mark
3535 9b058f45 2022-06-30 mark /*
3536 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3537 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3538 9b058f45 2022-06-30 mark */
3539 374f69dd 2022-08-13 stsp if (s->first_displayed_entry && s->last_displayed_entry) {
3540 374f69dd 2022-08-13 stsp s->selected = MIN(s->selected,
3541 374f69dd 2022-08-13 stsp s->last_displayed_entry->idx -
3542 374f69dd 2022-08-13 stsp s->first_displayed_entry->idx);
3543 374f69dd 2022-08-13 stsp }
3544 9b058f45 2022-06-30 mark
3545 b880cc75 2022-06-30 mark select_commit(s);
3546 b880cc75 2022-06-30 mark
3547 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3548 568eae95 2022-09-11 mark s->selected_entry->idx == s->commits->ncommits - 1)
3549 b880cc75 2022-06-30 mark view->count = 0;
3550 b880cc75 2022-06-30 mark
3551 b880cc75 2022-06-30 mark return NULL;
3552 e5a0f69f 2018-08-18 stsp }
3553 04cc582a 2018-08-01 stsp
3554 9b058f45 2022-06-30 mark static void
3555 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3556 9b058f45 2022-06-30 mark {
3557 76364b2d 2022-07-02 mark *x = 0;
3558 76364b2d 2022-07-02 mark *y = 0;
3559 76364b2d 2022-07-02 mark
3560 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3561 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3562 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3563 7532ccda 2022-07-11 mark else if (view->resized_y)
3564 7532ccda 2022-07-11 mark *y = view->resized_y;
3565 3c1dfe12 2022-07-08 mark else
3566 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3567 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3568 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3569 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3570 7532ccda 2022-07-11 mark else if (view->resized_x)
3571 7532ccda 2022-07-11 mark *x = view->resized_x;
3572 3c1dfe12 2022-07-08 mark else
3573 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3574 3c1dfe12 2022-07-08 mark }
3575 9b058f45 2022-06-30 mark }
3576 9b058f45 2022-06-30 mark
3577 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3578 e5a0f69f 2018-08-18 stsp static const struct got_error *
3579 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3580 9b058f45 2022-06-30 mark {
3581 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3582 9b058f45 2022-06-30 mark
3583 9b058f45 2022-06-30 mark view->nlines = y;
3584 d2366e29 2022-07-07 mark view->ncols = COLS;
3585 9b058f45 2022-06-30 mark err = view_resize(view);
3586 9b058f45 2022-06-30 mark if (err)
3587 9b058f45 2022-06-30 mark return err;
3588 9b058f45 2022-06-30 mark
3589 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3590 9b058f45 2022-06-30 mark
3591 9b058f45 2022-06-30 mark return err;
3592 94b80cfa 2022-08-01 mark }
3593 94b80cfa 2022-08-01 mark
3594 94b80cfa 2022-08-01 mark static const struct got_error *
3595 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3596 94b80cfa 2022-08-01 mark {
3597 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3598 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3599 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3600 374f69dd 2022-08-13 stsp
3601 374f69dd 2022-08-13 stsp if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3602 374f69dd 2022-08-13 stsp return NULL;
3603 94b80cfa 2022-08-01 mark
3604 94b80cfa 2022-08-01 mark g = view->gline;
3605 94b80cfa 2022-08-01 mark view->gline = 0;
3606 94b80cfa 2022-08-01 mark
3607 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3608 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3609 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3610 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3611 94b80cfa 2022-08-01 mark select_commit(s);
3612 94b80cfa 2022-08-01 mark return NULL;
3613 94b80cfa 2022-08-01 mark }
3614 94b80cfa 2022-08-01 mark
3615 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3616 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3617 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3618 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3619 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3620 94b80cfa 2022-08-01 mark if (err)
3621 94b80cfa 2022-08-01 mark return err;
3622 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3623 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3624 94b80cfa 2022-08-01 mark
3625 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3626 94b80cfa 2022-08-01 mark s->selected = g - 1;
3627 94b80cfa 2022-08-01 mark
3628 94b80cfa 2022-08-01 mark select_commit(s);
3629 94b80cfa 2022-08-01 mark return NULL;
3630 94b80cfa 2022-08-01 mark
3631 9b058f45 2022-06-30 mark }
3632 9b058f45 2022-06-30 mark
3633 9b058f45 2022-06-30 mark static const struct got_error *
3634 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3635 e5a0f69f 2018-08-18 stsp {
3636 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3637 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3638 631e7531 2022-08-12 mark int eos, nscroll;
3639 80ddbec8 2018-04-29 stsp
3640 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3641 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3642 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3643 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3644 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3645 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3646 fb280deb 2021-08-30 stsp }
3647 11edf34c 2022-08-12 mark if (err)
3648 11edf34c 2022-08-12 mark return err;
3649 528dedf3 2021-08-30 stsp }
3650 0dca135e 2022-06-30 mark
3651 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3652 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3653 0dca135e 2022-06-30 mark --eos; /* border */
3654 94b80cfa 2022-08-01 mark
3655 94b80cfa 2022-08-01 mark if (view->gline)
3656 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3657 0dca135e 2022-06-30 mark
3658 528dedf3 2021-08-30 stsp switch (ch) {
3659 568eae95 2022-09-11 mark case '&':
3660 568eae95 2022-09-11 mark err = limit_log_view(view);
3661 568eae95 2022-09-11 mark break;
3662 1e37a5c2 2019-05-12 jcs case 'q':
3663 1e37a5c2 2019-05-12 jcs s->quit = 1;
3664 145b6838 2022-06-16 stsp break;
3665 145b6838 2022-06-16 stsp case '0':
3666 145b6838 2022-06-16 stsp view->x = 0;
3667 145b6838 2022-06-16 stsp break;
3668 145b6838 2022-06-16 stsp case '$':
3669 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3670 640cd7ff 2022-06-22 mark view->count = 0;
3671 145b6838 2022-06-16 stsp break;
3672 145b6838 2022-06-16 stsp case KEY_RIGHT:
3673 145b6838 2022-06-16 stsp case 'l':
3674 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3675 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3676 640cd7ff 2022-06-22 mark else
3677 640cd7ff 2022-06-22 mark view->count = 0;
3678 1e37a5c2 2019-05-12 jcs break;
3679 145b6838 2022-06-16 stsp case KEY_LEFT:
3680 145b6838 2022-06-16 stsp case 'h':
3681 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3682 640cd7ff 2022-06-22 mark if (view->x <= 0)
3683 640cd7ff 2022-06-22 mark view->count = 0;
3684 145b6838 2022-06-16 stsp break;
3685 1e37a5c2 2019-05-12 jcs case 'k':
3686 1e37a5c2 2019-05-12 jcs case KEY_UP:
3687 1e37a5c2 2019-05-12 jcs case '<':
3688 1e37a5c2 2019-05-12 jcs case ',':
3689 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3690 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3691 912a3f79 2021-08-30 j break;
3692 912a3f79 2021-08-30 j case 'g':
3693 912a3f79 2021-08-30 j case KEY_HOME:
3694 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3695 640cd7ff 2022-06-22 mark view->count = 0;
3696 1e37a5c2 2019-05-12 jcs break;
3697 83cc4199 2022-06-13 stsp case CTRL('u'):
3698 33c3719a 2022-06-15 stsp case 'u':
3699 83cc4199 2022-06-13 stsp nscroll /= 2;
3700 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3701 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3702 a4292ac5 2019-05-12 jcs case CTRL('b'):
3703 61417565 2022-06-20 mark case 'b':
3704 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3705 1e37a5c2 2019-05-12 jcs break;
3706 1e37a5c2 2019-05-12 jcs case 'j':
3707 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3708 1e37a5c2 2019-05-12 jcs case '>':
3709 1e37a5c2 2019-05-12 jcs case '.':
3710 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3711 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3712 912a3f79 2021-08-30 j break;
3713 10aab77f 2022-07-19 op case '@':
3714 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3715 10aab77f 2022-07-19 op break;
3716 912a3f79 2021-08-30 j case 'G':
3717 912a3f79 2021-08-30 j case KEY_END: {
3718 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3719 912a3f79 2021-08-30 j * traverse them all. */
3720 640cd7ff 2022-06-22 mark view->count = 0;
3721 631e7531 2022-08-12 mark s->thread_args.load_all = 1;
3722 631e7531 2022-08-12 mark if (!s->thread_args.log_complete)
3723 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3724 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3725 631e7531 2022-08-12 mark s->thread_args.load_all = 0;
3726 1e37a5c2 2019-05-12 jcs break;
3727 912a3f79 2021-08-30 j }
3728 80b7e8da 2022-06-11 stsp case CTRL('d'):
3729 33c3719a 2022-06-15 stsp case 'd':
3730 83cc4199 2022-06-13 stsp nscroll /= 2;
3731 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3732 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3733 61417565 2022-06-20 mark case CTRL('f'):
3734 48bb96f0 2022-06-20 naddy case 'f':
3735 b880cc75 2022-06-30 mark case ' ':
3736 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3737 1e37a5c2 2019-05-12 jcs break;
3738 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3739 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3740 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3741 568eae95 2022-09-11 mark if (s->selected > s->commits->ncommits - 1)
3742 568eae95 2022-09-11 mark s->selected = s->commits->ncommits - 1;
3743 2b779855 2020-12-05 naddy select_commit(s);
3744 568eae95 2022-09-11 mark if (s->commits->ncommits < view->nlines - 1 &&
3745 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3746 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3747 568eae95 2022-09-11 mark s->commits->ncommits;
3748 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3749 0bf7f153 2020-12-02 naddy }
3750 1e37a5c2 2019-05-12 jcs break;
3751 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3752 49b24ee5 2022-07-03 mark case '\r':
3753 640cd7ff 2022-06-22 mark view->count = 0;
3754 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3755 e5a0f69f 2018-08-18 stsp break;
3756 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3757 1e37a5c2 2019-05-12 jcs break;
3758 5e98fb33 2022-07-22 mark case 'T':
3759 640cd7ff 2022-06-22 mark view->count = 0;
3760 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3761 5036bf37 2018-09-24 stsp break;
3762 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
3763 1e37a5c2 2019-05-12 jcs break;
3764 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3765 21355643 2020-12-06 stsp case CTRL('l'):
3766 21355643 2020-12-06 stsp case 'B':
3767 640cd7ff 2022-06-22 mark view->count = 0;
3768 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3769 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3770 1e37a5c2 2019-05-12 jcs break;
3771 21355643 2020-12-06 stsp err = stop_log_thread(s);
3772 74cfe85e 2020-10-20 stsp if (err)
3773 74cfe85e 2020-10-20 stsp return err;
3774 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3775 21355643 2020-12-06 stsp char *parent_path;
3776 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3777 21355643 2020-12-06 stsp if (err)
3778 21355643 2020-12-06 stsp return err;
3779 21355643 2020-12-06 stsp free(s->in_repo_path);
3780 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3781 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3782 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3783 21355643 2020-12-06 stsp struct got_object_id *start_id;
3784 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3785 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3786 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3787 21355643 2020-12-06 stsp if (err)
3788 21355643 2020-12-06 stsp return err;
3789 21355643 2020-12-06 stsp free(s->start_id);
3790 21355643 2020-12-06 stsp s->start_id = start_id;
3791 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3792 21355643 2020-12-06 stsp } else /* 'B' */
3793 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3794 21355643 2020-12-06 stsp
3795 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3796 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3797 b0dd8d27 2022-06-16 stsp if (err)
3798 b0dd8d27 2022-06-16 stsp return err;
3799 b0dd8d27 2022-06-16 stsp }
3800 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3801 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3802 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3803 74cfe85e 2020-10-20 stsp if (err)
3804 21355643 2020-12-06 stsp return err;
3805 51a10b52 2020-12-26 stsp tog_free_refs();
3806 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3807 ca51c541 2020-12-07 stsp if (err)
3808 ca51c541 2020-12-07 stsp return err;
3809 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3810 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3811 d01904d4 2019-06-25 stsp if (err)
3812 d01904d4 2019-06-25 stsp return err;
3813 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3814 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3815 b672a97a 2020-01-27 stsp if (err)
3816 b672a97a 2020-01-27 stsp return err;
3817 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3818 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3819 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3820 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3821 21355643 2020-12-06 stsp s->selected_entry = NULL;
3822 21355643 2020-12-06 stsp s->selected = 0;
3823 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3824 21355643 2020-12-06 stsp s->quit = 0;
3825 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3826 dfee752e 2022-06-18 op s->matched_entry = NULL;
3827 dfee752e 2022-06-18 op s->search_entry = NULL;
3828 01a7bcaf 2022-07-22 mark view->offset = 0;
3829 d01904d4 2019-06-25 stsp break;
3830 5e98fb33 2022-07-22 mark case 'R':
3831 640cd7ff 2022-06-22 mark view->count = 0;
3832 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
3833 6458efa5 2020-11-24 stsp break;
3834 1e37a5c2 2019-05-12 jcs default:
3835 640cd7ff 2022-06-22 mark view->count = 0;
3836 1e37a5c2 2019-05-12 jcs break;
3837 899d86c2 2018-05-10 stsp }
3838 e5a0f69f 2018-08-18 stsp
3839 80ddbec8 2018-04-29 stsp return err;
3840 80ddbec8 2018-04-29 stsp }
3841 80ddbec8 2018-04-29 stsp
3842 4ed7e80c 2018-05-20 stsp static const struct got_error *
3843 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3844 c2db6724 2019-01-04 stsp {
3845 c2db6724 2019-01-04 stsp const struct got_error *error;
3846 c2db6724 2019-01-04 stsp
3847 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3848 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3849 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3850 37c06ea4 2019-07-15 stsp #endif
3851 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3852 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3853 c2db6724 2019-01-04 stsp
3854 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3855 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3856 c2db6724 2019-01-04 stsp
3857 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3858 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3859 c2db6724 2019-01-04 stsp
3860 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3861 c2db6724 2019-01-04 stsp if (error != NULL)
3862 c2db6724 2019-01-04 stsp return error;
3863 c2db6724 2019-01-04 stsp
3864 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3865 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3866 c2db6724 2019-01-04 stsp
3867 c2db6724 2019-01-04 stsp return NULL;
3868 c2db6724 2019-01-04 stsp }
3869 c2db6724 2019-01-04 stsp
3870 a915003a 2019-02-05 stsp static void
3871 a915003a 2019-02-05 stsp init_curses(void)
3872 a915003a 2019-02-05 stsp {
3873 2497f032 2022-05-31 stsp /*
3874 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3875 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3876 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3877 2497f032 2022-05-31 stsp */
3878 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3879 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3880 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3881 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3882 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3883 2497f032 2022-05-31 stsp
3884 a915003a 2019-02-05 stsp initscr();
3885 a915003a 2019-02-05 stsp cbreak();
3886 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3887 a915003a 2019-02-05 stsp noecho();
3888 a915003a 2019-02-05 stsp nonl();
3889 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3890 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3891 a915003a 2019-02-05 stsp curs_set(0);
3892 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3893 6d17833f 2019-11-08 stsp start_color();
3894 6d17833f 2019-11-08 stsp use_default_colors();
3895 6d17833f 2019-11-08 stsp }
3896 a915003a 2019-02-05 stsp }
3897 a915003a 2019-02-05 stsp
3898 c2db6724 2019-01-04 stsp static const struct got_error *
3899 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3900 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3901 f135c941 2020-02-20 stsp {
3902 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3903 f135c941 2020-02-20 stsp
3904 f135c941 2020-02-20 stsp if (argc == 0) {
3905 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3906 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3907 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3908 f135c941 2020-02-20 stsp return NULL;
3909 f135c941 2020-02-20 stsp }
3910 f135c941 2020-02-20 stsp
3911 f135c941 2020-02-20 stsp if (worktree) {
3912 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3913 bfd61697 2020-11-14 stsp char *p;
3914 f135c941 2020-02-20 stsp
3915 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3916 f135c941 2020-02-20 stsp if (err)
3917 f135c941 2020-02-20 stsp return err;
3918 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3919 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3920 bfd61697 2020-11-14 stsp p) == -1) {
3921 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3922 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3923 f135c941 2020-02-20 stsp }
3924 f135c941 2020-02-20 stsp free(p);
3925 f135c941 2020-02-20 stsp } else
3926 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3927 f135c941 2020-02-20 stsp
3928 f135c941 2020-02-20 stsp return err;
3929 f135c941 2020-02-20 stsp }
3930 f135c941 2020-02-20 stsp
3931 f135c941 2020-02-20 stsp static const struct got_error *
3932 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3933 9f7d7167 2018-04-29 stsp {
3934 80ddbec8 2018-04-29 stsp const struct got_error *error;
3935 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3936 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3937 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3938 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3939 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3940 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3941 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3942 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3943 04cc582a 2018-08-01 stsp struct tog_view *view;
3944 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3945 80ddbec8 2018-04-29 stsp
3946 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3947 80ddbec8 2018-04-29 stsp switch (ch) {
3948 b672a97a 2020-01-27 stsp case 'b':
3949 b672a97a 2020-01-27 stsp log_branches = 1;
3950 b672a97a 2020-01-27 stsp break;
3951 80ddbec8 2018-04-29 stsp case 'c':
3952 80ddbec8 2018-04-29 stsp start_commit = optarg;
3953 80ddbec8 2018-04-29 stsp break;
3954 ecb28ae0 2018-07-16 stsp case 'r':
3955 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3956 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3957 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3958 9ba1d308 2019-10-21 stsp optarg);
3959 ecb28ae0 2018-07-16 stsp break;
3960 80ddbec8 2018-04-29 stsp default:
3961 17020d27 2019-03-07 stsp usage_log();
3962 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3963 80ddbec8 2018-04-29 stsp }
3964 80ddbec8 2018-04-29 stsp }
3965 80ddbec8 2018-04-29 stsp
3966 80ddbec8 2018-04-29 stsp argc -= optind;
3967 80ddbec8 2018-04-29 stsp argv += optind;
3968 80ddbec8 2018-04-29 stsp
3969 f135c941 2020-02-20 stsp if (argc > 1)
3970 f135c941 2020-02-20 stsp usage_log();
3971 963f97a1 2019-03-18 stsp
3972 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3973 0ae84acc 2022-06-15 tracey if (error != NULL)
3974 0ae84acc 2022-06-15 tracey goto done;
3975 0ae84acc 2022-06-15 tracey
3976 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3977 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3978 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3979 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3980 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3981 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3982 c156c7a4 2020-12-18 stsp goto done;
3983 a1fbf39a 2019-08-11 stsp if (worktree)
3984 6962eb72 2020-02-20 stsp repo_path =
3985 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3986 a1fbf39a 2019-08-11 stsp else
3987 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3988 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3989 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3990 c156c7a4 2020-12-18 stsp goto done;
3991 c156c7a4 2020-12-18 stsp }
3992 ecb28ae0 2018-07-16 stsp }
3993 ecb28ae0 2018-07-16 stsp
3994 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3995 80ddbec8 2018-04-29 stsp if (error != NULL)
3996 ecb28ae0 2018-07-16 stsp goto done;
3997 80ddbec8 2018-04-29 stsp
3998 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3999 f135c941 2020-02-20 stsp repo, worktree);
4000 f135c941 2020-02-20 stsp if (error)
4001 f135c941 2020-02-20 stsp goto done;
4002 f135c941 2020-02-20 stsp
4003 f135c941 2020-02-20 stsp init_curses();
4004 f135c941 2020-02-20 stsp
4005 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4006 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4007 c02c541e 2019-03-29 stsp if (error)
4008 c02c541e 2019-03-29 stsp goto done;
4009 c02c541e 2019-03-29 stsp
4010 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4011 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4012 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4013 87670572 2020-12-26 naddy if (error)
4014 87670572 2020-12-26 naddy goto done;
4015 87670572 2020-12-26 naddy }
4016 51a10b52 2020-12-26 stsp
4017 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4018 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4019 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4020 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4021 d8f38dc4 2020-12-05 stsp if (error)
4022 d8f38dc4 2020-12-05 stsp goto done;
4023 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4024 d8f38dc4 2020-12-05 stsp } else {
4025 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4026 d8f38dc4 2020-12-05 stsp if (error == NULL)
4027 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4028 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4029 d8f38dc4 2020-12-05 stsp goto done;
4030 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4031 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4032 d8f38dc4 2020-12-05 stsp if (error)
4033 d8f38dc4 2020-12-05 stsp goto done;
4034 d8f38dc4 2020-12-05 stsp }
4035 8b473291 2019-02-21 stsp
4036 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4037 04cc582a 2018-08-01 stsp if (view == NULL) {
4038 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4039 04cc582a 2018-08-01 stsp goto done;
4040 04cc582a 2018-08-01 stsp }
4041 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4042 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4043 ba4f502b 2018-08-04 stsp if (error)
4044 ba4f502b 2018-08-04 stsp goto done;
4045 2fc00ff4 2019-08-31 stsp if (worktree) {
4046 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4047 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4048 2fc00ff4 2019-08-31 stsp worktree = NULL;
4049 2fc00ff4 2019-08-31 stsp }
4050 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4051 ecb28ae0 2018-07-16 stsp done:
4052 f135c941 2020-02-20 stsp free(in_repo_path);
4053 ecb28ae0 2018-07-16 stsp free(repo_path);
4054 ecb28ae0 2018-07-16 stsp free(cwd);
4055 899d86c2 2018-05-10 stsp free(start_id);
4056 d8f38dc4 2020-12-05 stsp free(label);
4057 d8f38dc4 2020-12-05 stsp if (ref)
4058 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4059 1d0f4054 2021-06-17 stsp if (repo) {
4060 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4061 1d0f4054 2021-06-17 stsp if (error == NULL)
4062 1d0f4054 2021-06-17 stsp error = close_err;
4063 1d0f4054 2021-06-17 stsp }
4064 ec142235 2019-03-07 stsp if (worktree)
4065 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4066 0ae84acc 2022-06-15 tracey if (pack_fds) {
4067 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4068 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4069 0ae84acc 2022-06-15 tracey if (error == NULL)
4070 0ae84acc 2022-06-15 tracey error = pack_err;
4071 0ae84acc 2022-06-15 tracey }
4072 51a10b52 2020-12-26 stsp tog_free_refs();
4073 80ddbec8 2018-04-29 stsp return error;
4074 9f7d7167 2018-04-29 stsp }
4075 9f7d7167 2018-04-29 stsp
4076 4ed7e80c 2018-05-20 stsp __dead static void
4077 9f7d7167 2018-04-29 stsp usage_diff(void)
4078 9f7d7167 2018-04-29 stsp {
4079 80ddbec8 2018-04-29 stsp endwin();
4080 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4081 827a167b 2022-08-16 stsp "object1 object2\n", getprogname());
4082 9f7d7167 2018-04-29 stsp exit(1);
4083 b304db33 2018-05-20 stsp }
4084 b304db33 2018-05-20 stsp
4085 6d17833f 2019-11-08 stsp static int
4086 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4087 41605754 2020-11-12 stsp regmatch_t *regmatch)
4088 6d17833f 2019-11-08 stsp {
4089 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4090 6d17833f 2019-11-08 stsp }
4091 6d17833f 2019-11-08 stsp
4092 336075a4 2022-06-25 op static struct tog_color *
4093 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4094 6d17833f 2019-11-08 stsp {
4095 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4096 6d17833f 2019-11-08 stsp
4097 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4098 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4099 6d17833f 2019-11-08 stsp return tc;
4100 6d17833f 2019-11-08 stsp }
4101 6d17833f 2019-11-08 stsp
4102 6d17833f 2019-11-08 stsp return NULL;
4103 6d17833f 2019-11-08 stsp }
4104 6d17833f 2019-11-08 stsp
4105 4ed7e80c 2018-05-20 stsp static const struct got_error *
4106 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4107 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
4108 41605754 2020-11-12 stsp {
4109 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4110 44a87665 2022-06-16 stsp char *exstr = NULL;
4111 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
4112 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
4113 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
4114 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4115 41605754 2020-11-12 stsp
4116 41605754 2020-11-12 stsp *wtotal = 0;
4117 1853e0f4 2022-06-16 stsp
4118 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
4119 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
4120 41605754 2020-11-12 stsp
4121 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
4122 44a87665 2022-06-16 stsp if (err)
4123 44a87665 2022-06-16 stsp return err;
4124 44a87665 2022-06-16 stsp
4125 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
4126 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
4127 44a87665 2022-06-16 stsp if (seg0 == NULL) {
4128 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
4129 44a87665 2022-06-16 stsp goto done;
4130 44a87665 2022-06-16 stsp }
4131 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
4132 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
4133 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4134 1853e0f4 2022-06-16 stsp goto done;
4135 1853e0f4 2022-06-16 stsp }
4136 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
4137 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
4138 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4139 1853e0f4 2022-06-16 stsp goto done;
4140 1853e0f4 2022-06-16 stsp }
4141 145b6838 2022-06-16 stsp
4142 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
4143 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4144 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4145 1853e0f4 2022-06-16 stsp if (err)
4146 1853e0f4 2022-06-16 stsp goto done;
4147 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
4148 145b6838 2022-06-16 stsp if (n) {
4149 1853e0f4 2022-06-16 stsp free(wline);
4150 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4151 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4152 1853e0f4 2022-06-16 stsp if (err)
4153 1853e0f4 2022-06-16 stsp goto done;
4154 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4155 1853e0f4 2022-06-16 stsp wlimit -= width;
4156 1853e0f4 2022-06-16 stsp *wtotal += width;
4157 41605754 2020-11-12 stsp }
4158 41605754 2020-11-12 stsp
4159 41605754 2020-11-12 stsp if (wlimit > 0) {
4160 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
4161 1853e0f4 2022-06-16 stsp size_t wlen;
4162 1853e0f4 2022-06-16 stsp
4163 1853e0f4 2022-06-16 stsp free(wline);
4164 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4165 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4166 1853e0f4 2022-06-16 stsp if (err)
4167 1853e0f4 2022-06-16 stsp goto done;
4168 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
4169 1853e0f4 2022-06-16 stsp while (i < wlen) {
4170 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
4171 1853e0f4 2022-06-16 stsp if (width == -1) {
4172 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
4173 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
4174 1853e0f4 2022-06-16 stsp goto done;
4175 1853e0f4 2022-06-16 stsp }
4176 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
4177 1853e0f4 2022-06-16 stsp break;
4178 61417565 2022-06-20 mark w += width;
4179 1853e0f4 2022-06-16 stsp i++;
4180 41605754 2020-11-12 stsp }
4181 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
4182 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
4183 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
4184 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
4185 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
4186 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
4187 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
4188 41605754 2020-11-12 stsp }
4189 41605754 2020-11-12 stsp }
4190 41605754 2020-11-12 stsp
4191 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
4192 1853e0f4 2022-06-16 stsp free(wline);
4193 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
4194 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
4195 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
4196 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4197 1853e0f4 2022-06-16 stsp if (err)
4198 1853e0f4 2022-06-16 stsp goto done;
4199 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4200 1853e0f4 2022-06-16 stsp } else {
4201 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
4202 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4203 1853e0f4 2022-06-16 stsp if (err)
4204 1853e0f4 2022-06-16 stsp goto done;
4205 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
4206 1853e0f4 2022-06-16 stsp }
4207 1853e0f4 2022-06-16 stsp *wtotal += width2;
4208 41605754 2020-11-12 stsp }
4209 1853e0f4 2022-06-16 stsp done:
4210 145b6838 2022-06-16 stsp free(wline);
4211 44a87665 2022-06-16 stsp free(exstr);
4212 1853e0f4 2022-06-16 stsp free(seg0);
4213 1853e0f4 2022-06-16 stsp free(seg1);
4214 1853e0f4 2022-06-16 stsp free(seg2);
4215 1853e0f4 2022-06-16 stsp return err;
4216 41605754 2020-11-12 stsp }
4217 94b80cfa 2022-08-01 mark
4218 94b80cfa 2022-08-01 mark static int
4219 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
4220 94b80cfa 2022-08-01 mark {
4221 94b80cfa 2022-08-01 mark FILE *f = NULL;
4222 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
4223 94b80cfa 2022-08-01 mark
4224 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
4225 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
4226 ec2a9698 2022-09-15 mark
4227 ec2a9698 2022-09-15 mark first = &s->first_displayed_line;
4228 ec2a9698 2022-09-15 mark selected = first;
4229 ec2a9698 2022-09-15 mark eof = &s->eof;
4230 ec2a9698 2022-09-15 mark f = s->f;
4231 ec2a9698 2022-09-15 mark } else if (view->type == TOG_VIEW_HELP) {
4232 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
4233 41605754 2020-11-12 stsp
4234 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4235 94b80cfa 2022-08-01 mark selected = first;
4236 94b80cfa 2022-08-01 mark eof = &s->eof;
4237 94b80cfa 2022-08-01 mark f = s->f;
4238 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
4239 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
4240 94b80cfa 2022-08-01 mark
4241 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4242 94b80cfa 2022-08-01 mark selected = &s->selected_line;
4243 94b80cfa 2022-08-01 mark eof = &s->eof;
4244 94b80cfa 2022-08-01 mark f = s->blame.f;
4245 94b80cfa 2022-08-01 mark } else
4246 94b80cfa 2022-08-01 mark return 0;
4247 94b80cfa 2022-08-01 mark
4248 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
4249 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
4250 94b80cfa 2022-08-01 mark return 0;
4251 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4252 94b80cfa 2022-08-01 mark rewind(f);
4253 94b80cfa 2022-08-01 mark *eof = 0;
4254 94b80cfa 2022-08-01 mark *first = 1;
4255 94b80cfa 2022-08-01 mark *lineno = 0;
4256 94b80cfa 2022-08-01 mark *nprinted = 0;
4257 94b80cfa 2022-08-01 mark return 0;
4258 94b80cfa 2022-08-01 mark }
4259 94b80cfa 2022-08-01 mark
4260 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
4261 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
4262 94b80cfa 2022-08-01 mark view->gline = 0;
4263 94b80cfa 2022-08-01 mark
4264 94b80cfa 2022-08-01 mark return 1;
4265 94b80cfa 2022-08-01 mark }
4266 94b80cfa 2022-08-01 mark
4267 41605754 2020-11-12 stsp static const struct got_error *
4268 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4269 26ed57b2 2018-05-19 stsp {
4270 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4271 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4272 61e69b96 2018-05-20 stsp const struct got_error *err;
4273 c7d5c43c 2022-08-04 mark int nprinted = 0;
4274 b304db33 2018-05-20 stsp char *line;
4275 826082fe 2020-12-10 stsp size_t linesize = 0;
4276 826082fe 2020-12-10 stsp ssize_t linelen;
4277 61e69b96 2018-05-20 stsp wchar_t *wline;
4278 e0b650dd 2018-05-20 stsp int width;
4279 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4280 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4281 fe621944 2020-11-10 stsp off_t line_offset;
4282 26ed57b2 2018-05-19 stsp
4283 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1;
4284 c7d5c43c 2022-08-04 mark line_offset = s->lines[s->first_displayed_line - 1].offset;
4285 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4286 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4287 fe621944 2020-11-10 stsp
4288 f7d12f7e 2018-08-01 stsp werase(view->window);
4289 a3404814 2018-09-02 stsp
4290 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
4291 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
4292 94b80cfa 2022-08-01 mark
4293 a3404814 2018-09-02 stsp if (header) {
4294 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4295 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
4296 c7d5c43c 2022-08-04 mark s->lineno + s->selected_line;
4297 94b80cfa 2022-08-01 mark
4298 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4299 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4300 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4301 ccda2f4d 2022-06-16 stsp 0, 0);
4302 135a2da0 2020-11-11 stsp free(line);
4303 135a2da0 2020-11-11 stsp if (err)
4304 a3404814 2018-09-02 stsp return err;
4305 a3404814 2018-09-02 stsp
4306 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4307 a3404814 2018-09-02 stsp wstandout(view->window);
4308 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4309 e54cc94a 2020-11-11 stsp free(wline);
4310 e54cc94a 2020-11-11 stsp wline = NULL;
4311 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
4312 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
4313 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4314 a3404814 2018-09-02 stsp wstandend(view->window);
4315 26ed57b2 2018-05-19 stsp
4316 a3404814 2018-09-02 stsp if (max_lines <= 1)
4317 a3404814 2018-09-02 stsp return NULL;
4318 a3404814 2018-09-02 stsp max_lines--;
4319 a3404814 2018-09-02 stsp }
4320 a3404814 2018-09-02 stsp
4321 89f1a395 2020-12-01 naddy s->eof = 0;
4322 145b6838 2022-06-16 stsp view->maxx = 0;
4323 826082fe 2020-12-10 stsp line = NULL;
4324 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4325 6a5ff2d4 2022-08-06 mark enum got_diff_line_type linetype;
4326 6a5ff2d4 2022-08-06 mark attr_t attr = 0;
4327 6a5ff2d4 2022-08-06 mark
4328 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4329 826082fe 2020-12-10 stsp if (linelen == -1) {
4330 826082fe 2020-12-10 stsp if (feof(s->f)) {
4331 826082fe 2020-12-10 stsp s->eof = 1;
4332 826082fe 2020-12-10 stsp break;
4333 826082fe 2020-12-10 stsp }
4334 826082fe 2020-12-10 stsp free(line);
4335 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4336 61e69b96 2018-05-20 stsp }
4337 145b6838 2022-06-16 stsp
4338 c7d5c43c 2022-08-04 mark if (++s->lineno < s->first_displayed_line)
4339 94b80cfa 2022-08-01 mark continue;
4340 c7d5c43c 2022-08-04 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4341 94b80cfa 2022-08-01 mark continue;
4342 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline)
4343 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4344 94b80cfa 2022-08-01 mark
4345 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4346 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4347 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4348 1853e0f4 2022-06-16 stsp if (err) {
4349 1853e0f4 2022-06-16 stsp free(line);
4350 1853e0f4 2022-06-16 stsp return err;
4351 1853e0f4 2022-06-16 stsp }
4352 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4353 1853e0f4 2022-06-16 stsp free(wline);
4354 1853e0f4 2022-06-16 stsp wline = NULL;
4355 1853e0f4 2022-06-16 stsp
4356 6a5ff2d4 2022-08-06 mark linetype = s->lines[s->lineno].type;
4357 6a5ff2d4 2022-08-06 mark if (linetype > GOT_DIFF_LINE_LOGMSG &&
4358 6a5ff2d4 2022-08-06 mark linetype < GOT_DIFF_LINE_CONTEXT)
4359 6a5ff2d4 2022-08-06 mark attr |= COLOR_PAIR(linetype);
4360 94b80cfa 2022-08-01 mark if (attr)
4361 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4362 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4363 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4364 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4365 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4366 41605754 2020-11-12 stsp if (err) {
4367 41605754 2020-11-12 stsp free(line);
4368 41605754 2020-11-12 stsp return err;
4369 41605754 2020-11-12 stsp }
4370 41605754 2020-11-12 stsp } else {
4371 969c159c 2022-06-16 stsp int skip;
4372 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4373 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4374 969c159c 2022-06-16 stsp if (err) {
4375 969c159c 2022-06-16 stsp free(line);
4376 969c159c 2022-06-16 stsp return err;
4377 969c159c 2022-06-16 stsp }
4378 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4379 969c159c 2022-06-16 stsp free(wline);
4380 41605754 2020-11-12 stsp wline = NULL;
4381 41605754 2020-11-12 stsp }
4382 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline) {
4383 94b80cfa 2022-08-01 mark /* highlight full gline length */
4384 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4385 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4386 94b80cfa 2022-08-01 mark } else {
4387 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4388 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4389 94b80cfa 2022-08-01 mark }
4390 94b80cfa 2022-08-01 mark if (attr)
4391 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4392 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4393 c7d5c43c 2022-08-04 mark s->first_displayed_line = s->lineno;
4394 826082fe 2020-12-10 stsp }
4395 826082fe 2020-12-10 stsp free(line);
4396 fe621944 2020-11-10 stsp if (nprinted >= 1)
4397 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4398 89f1a395 2020-12-01 naddy (nprinted - 1);
4399 fe621944 2020-11-10 stsp else
4400 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4401 26ed57b2 2018-05-19 stsp
4402 9b058f45 2022-06-30 mark view_border(view);
4403 c3e9aa98 2019-05-13 jcs
4404 89f1a395 2020-12-01 naddy if (s->eof) {
4405 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4406 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4407 c3e9aa98 2019-05-13 jcs nprinted++;
4408 c3e9aa98 2019-05-13 jcs }
4409 c3e9aa98 2019-05-13 jcs
4410 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4411 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4412 c3e9aa98 2019-05-13 jcs if (err) {
4413 c3e9aa98 2019-05-13 jcs return err;
4414 c3e9aa98 2019-05-13 jcs }
4415 26ed57b2 2018-05-19 stsp
4416 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4417 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4418 e54cc94a 2020-11-11 stsp free(wline);
4419 e54cc94a 2020-11-11 stsp wline = NULL;
4420 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4421 c3e9aa98 2019-05-13 jcs }
4422 c3e9aa98 2019-05-13 jcs
4423 26ed57b2 2018-05-19 stsp return NULL;
4424 abd2672a 2018-12-23 stsp }
4425 abd2672a 2018-12-23 stsp
4426 abd2672a 2018-12-23 stsp static char *
4427 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4428 abd2672a 2018-12-23 stsp {
4429 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4430 09867e48 2019-08-13 stsp char *p, *s;
4431 09867e48 2019-08-13 stsp
4432 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4433 09867e48 2019-08-13 stsp if (tm == NULL)
4434 09867e48 2019-08-13 stsp return NULL;
4435 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4436 09867e48 2019-08-13 stsp if (s == NULL)
4437 09867e48 2019-08-13 stsp return NULL;
4438 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4439 abd2672a 2018-12-23 stsp if (p)
4440 abd2672a 2018-12-23 stsp *p = '\0';
4441 abd2672a 2018-12-23 stsp return s;
4442 9f7d7167 2018-04-29 stsp }
4443 9f7d7167 2018-04-29 stsp
4444 4ed7e80c 2018-05-20 stsp static const struct got_error *
4445 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4446 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4447 0208f208 2020-05-05 stsp {
4448 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4449 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4450 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4451 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4452 0208f208 2020-05-05 stsp
4453 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4454 0208f208 2020-05-05 stsp if (qid != NULL) {
4455 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4456 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4457 d7b5a0e8 2022-04-20 stsp &qid->id);
4458 0208f208 2020-05-05 stsp if (err)
4459 0208f208 2020-05-05 stsp return err;
4460 0208f208 2020-05-05 stsp
4461 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4462 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4463 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4464 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4465 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4466 aa8b5dd0 2021-08-01 stsp }
4467 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4468 0208f208 2020-05-05 stsp
4469 0208f208 2020-05-05 stsp }
4470 0208f208 2020-05-05 stsp
4471 0208f208 2020-05-05 stsp if (tree_id1) {
4472 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4473 0208f208 2020-05-05 stsp if (err)
4474 0208f208 2020-05-05 stsp goto done;
4475 0208f208 2020-05-05 stsp }
4476 0208f208 2020-05-05 stsp
4477 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4478 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4479 0208f208 2020-05-05 stsp if (err)
4480 0208f208 2020-05-05 stsp goto done;
4481 0208f208 2020-05-05 stsp
4482 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4483 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4484 0208f208 2020-05-05 stsp done:
4485 0208f208 2020-05-05 stsp if (tree1)
4486 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4487 0208f208 2020-05-05 stsp if (tree2)
4488 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4489 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4490 0208f208 2020-05-05 stsp return err;
4491 0208f208 2020-05-05 stsp }
4492 0208f208 2020-05-05 stsp
4493 0208f208 2020-05-05 stsp static const struct got_error *
4494 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4495 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4496 abd2672a 2018-12-23 stsp {
4497 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4498 fe621944 2020-11-10 stsp
4499 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4500 fe621944 2020-11-10 stsp if (p == NULL)
4501 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4502 c7d5c43c 2022-08-04 mark *lines = p;
4503 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4504 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4505 fe621944 2020-11-10 stsp (*nlines)++;
4506 c7d5c43c 2022-08-04 mark
4507 fe621944 2020-11-10 stsp return NULL;
4508 fe621944 2020-11-10 stsp }
4509 fe621944 2020-11-10 stsp
4510 fe621944 2020-11-10 stsp static const struct got_error *
4511 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4512 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4513 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4514 fe621944 2020-11-10 stsp {
4515 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4516 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4517 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4518 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4519 45d799e2 2018-12-23 stsp time_t committer_time;
4520 45d799e2 2018-12-23 stsp const char *author, *committer;
4521 8b473291 2019-02-21 stsp char *refs_str = NULL;
4522 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4523 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4524 fe621944 2020-11-10 stsp off_t outoff = 0;
4525 fe621944 2020-11-10 stsp int n;
4526 abd2672a 2018-12-23 stsp
4527 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4528 0208f208 2020-05-05 stsp
4529 8b473291 2019-02-21 stsp if (refs) {
4530 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4531 8b473291 2019-02-21 stsp if (err)
4532 8b473291 2019-02-21 stsp return err;
4533 8b473291 2019-02-21 stsp }
4534 8b473291 2019-02-21 stsp
4535 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4536 abd2672a 2018-12-23 stsp if (err)
4537 abd2672a 2018-12-23 stsp return err;
4538 abd2672a 2018-12-23 stsp
4539 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4540 15a94983 2018-12-23 stsp if (err) {
4541 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4542 15a94983 2018-12-23 stsp goto done;
4543 15a94983 2018-12-23 stsp }
4544 abd2672a 2018-12-23 stsp
4545 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4546 fe621944 2020-11-10 stsp if (err)
4547 fe621944 2020-11-10 stsp goto done;
4548 fe621944 2020-11-10 stsp
4549 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4550 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4551 fe621944 2020-11-10 stsp if (n < 0) {
4552 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4553 abd2672a 2018-12-23 stsp goto done;
4554 abd2672a 2018-12-23 stsp }
4555 fe621944 2020-11-10 stsp outoff += n;
4556 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4557 fe621944 2020-11-10 stsp if (err)
4558 fe621944 2020-11-10 stsp goto done;
4559 fe621944 2020-11-10 stsp
4560 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4561 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4562 fe621944 2020-11-10 stsp if (n < 0) {
4563 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4564 abd2672a 2018-12-23 stsp goto done;
4565 abd2672a 2018-12-23 stsp }
4566 fe621944 2020-11-10 stsp outoff += n;
4567 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4568 fe621944 2020-11-10 stsp if (err)
4569 fe621944 2020-11-10 stsp goto done;
4570 fe621944 2020-11-10 stsp
4571 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4572 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4573 fe621944 2020-11-10 stsp if (datestr) {
4574 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4575 fe621944 2020-11-10 stsp if (n < 0) {
4576 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4577 fe621944 2020-11-10 stsp goto done;
4578 fe621944 2020-11-10 stsp }
4579 fe621944 2020-11-10 stsp outoff += n;
4580 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4581 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_DATE);
4582 fe621944 2020-11-10 stsp if (err)
4583 fe621944 2020-11-10 stsp goto done;
4584 abd2672a 2018-12-23 stsp }
4585 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4586 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4587 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4588 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4589 fe621944 2020-11-10 stsp if (n < 0) {
4590 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4591 fe621944 2020-11-10 stsp goto done;
4592 fe621944 2020-11-10 stsp }
4593 fe621944 2020-11-10 stsp outoff += n;
4594 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4595 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_AUTHOR);
4596 fe621944 2020-11-10 stsp if (err)
4597 fe621944 2020-11-10 stsp goto done;
4598 abd2672a 2018-12-23 stsp }
4599 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4600 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4601 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4602 9f98ca05 2021-09-24 stsp int pn = 1;
4603 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4604 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4605 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4606 9f98ca05 2021-09-24 stsp if (err)
4607 9f98ca05 2021-09-24 stsp goto done;
4608 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4609 9f98ca05 2021-09-24 stsp if (n < 0) {
4610 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4611 9f98ca05 2021-09-24 stsp goto done;
4612 9f98ca05 2021-09-24 stsp }
4613 9f98ca05 2021-09-24 stsp outoff += n;
4614 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4615 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
4616 9f98ca05 2021-09-24 stsp if (err)
4617 9f98ca05 2021-09-24 stsp goto done;
4618 9f98ca05 2021-09-24 stsp free(id_str);
4619 9f98ca05 2021-09-24 stsp id_str = NULL;
4620 9f98ca05 2021-09-24 stsp }
4621 9f98ca05 2021-09-24 stsp }
4622 9f98ca05 2021-09-24 stsp
4623 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4624 5943eee2 2019-08-13 stsp if (err)
4625 5943eee2 2019-08-13 stsp goto done;
4626 fe621944 2020-11-10 stsp s = logmsg;
4627 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4628 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4629 fe621944 2020-11-10 stsp if (n < 0) {
4630 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4631 fe621944 2020-11-10 stsp goto done;
4632 fe621944 2020-11-10 stsp }
4633 fe621944 2020-11-10 stsp outoff += n;
4634 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4635 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
4636 fe621944 2020-11-10 stsp if (err)
4637 fe621944 2020-11-10 stsp goto done;
4638 abd2672a 2018-12-23 stsp }
4639 fe621944 2020-11-10 stsp
4640 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4641 0208f208 2020-05-05 stsp if (err)
4642 0208f208 2020-05-05 stsp goto done;
4643 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4644 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4645 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4646 fe621944 2020-11-10 stsp if (n < 0) {
4647 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4648 fe621944 2020-11-10 stsp goto done;
4649 fe621944 2020-11-10 stsp }
4650 fe621944 2020-11-10 stsp outoff += n;
4651 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4652 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
4653 fe621944 2020-11-10 stsp if (err)
4654 fe621944 2020-11-10 stsp goto done;
4655 0208f208 2020-05-05 stsp free((char *)pe->path);
4656 0208f208 2020-05-05 stsp free(pe->data);
4657 0208f208 2020-05-05 stsp }
4658 fe621944 2020-11-10 stsp
4659 0208f208 2020-05-05 stsp fputc('\n', outfile);
4660 fe621944 2020-11-10 stsp outoff++;
4661 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4662 abd2672a 2018-12-23 stsp done:
4663 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4664 abd2672a 2018-12-23 stsp free(id_str);
4665 5943eee2 2019-08-13 stsp free(logmsg);
4666 8b473291 2019-02-21 stsp free(refs_str);
4667 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4668 7510f233 2020-08-09 stsp if (err) {
4669 c7d5c43c 2022-08-04 mark free(*lines);
4670 c7d5c43c 2022-08-04 mark *lines = NULL;
4671 ae6a6978 2020-08-09 stsp *nlines = 0;
4672 7510f233 2020-08-09 stsp }
4673 fe621944 2020-11-10 stsp return err;
4674 abd2672a 2018-12-23 stsp }
4675 abd2672a 2018-12-23 stsp
4676 abd2672a 2018-12-23 stsp static const struct got_error *
4677 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4678 26ed57b2 2018-05-19 stsp {
4679 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4680 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4681 15a94983 2018-12-23 stsp int obj_type;
4682 fe621944 2020-11-10 stsp
4683 c7d5c43c 2022-08-04 mark free(s->lines);
4684 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
4685 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
4686 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4687 fe621944 2020-11-10 stsp s->nlines = 0;
4688 26ed57b2 2018-05-19 stsp
4689 511a516b 2018-05-19 stsp f = got_opentemp();
4690 48ae06ee 2018-10-18 stsp if (f == NULL) {
4691 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4692 48ae06ee 2018-10-18 stsp goto done;
4693 48ae06ee 2018-10-18 stsp }
4694 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4695 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4696 fb43ecf1 2019-02-11 stsp goto done;
4697 fb43ecf1 2019-02-11 stsp }
4698 48ae06ee 2018-10-18 stsp s->f = f;
4699 26ed57b2 2018-05-19 stsp
4700 15a94983 2018-12-23 stsp if (s->id1)
4701 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4702 15a94983 2018-12-23 stsp else
4703 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4704 15a94983 2018-12-23 stsp if (err)
4705 15a94983 2018-12-23 stsp goto done;
4706 15a94983 2018-12-23 stsp
4707 15a94983 2018-12-23 stsp switch (obj_type) {
4708 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4709 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4710 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4711 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4712 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4713 26ed57b2 2018-05-19 stsp break;
4714 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4715 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4716 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4717 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4718 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4719 26ed57b2 2018-05-19 stsp break;
4720 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4721 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4722 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4723 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4724 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4725 abd2672a 2018-12-23 stsp
4726 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4727 abd2672a 2018-12-23 stsp if (err)
4728 3ffacbe1 2020-02-02 tracey goto done;
4729 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4730 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4731 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4732 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
4733 c7d5c43c 2022-08-04 mark refs, s->repo, s->f);
4734 f44b1f58 2020-02-02 tracey if (err)
4735 f44b1f58 2020-02-02 tracey goto done;
4736 f44b1f58 2020-02-02 tracey } else {
4737 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4738 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4739 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4740 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
4741 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
4742 c7d5c43c 2022-08-04 mark s->f);
4743 f44b1f58 2020-02-02 tracey if (err)
4744 f44b1f58 2020-02-02 tracey goto done;
4745 f5404e4e 2020-02-02 tracey break;
4746 15a087fe 2019-02-21 stsp }
4747 abd2672a 2018-12-23 stsp }
4748 abd2672a 2018-12-23 stsp }
4749 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4750 abd2672a 2018-12-23 stsp
4751 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4752 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4753 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4754 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4755 26ed57b2 2018-05-19 stsp break;
4756 abd2672a 2018-12-23 stsp }
4757 26ed57b2 2018-05-19 stsp default:
4758 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4759 48ae06ee 2018-10-18 stsp break;
4760 26ed57b2 2018-05-19 stsp }
4761 48ae06ee 2018-10-18 stsp done:
4762 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4763 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4764 48ae06ee 2018-10-18 stsp return err;
4765 48ae06ee 2018-10-18 stsp }
4766 26ed57b2 2018-05-19 stsp
4767 f5215bb9 2019-02-22 stsp static void
4768 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4769 f5215bb9 2019-02-22 stsp {
4770 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4771 f5215bb9 2019-02-22 stsp update_panels();
4772 f5215bb9 2019-02-22 stsp doupdate();
4773 f44b1f58 2020-02-02 tracey }
4774 f44b1f58 2020-02-02 tracey
4775 f44b1f58 2020-02-02 tracey static const struct got_error *
4776 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4777 f44b1f58 2020-02-02 tracey {
4778 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4779 f44b1f58 2020-02-02 tracey
4780 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4781 f44b1f58 2020-02-02 tracey return NULL;
4782 f44b1f58 2020-02-02 tracey }
4783 f44b1f58 2020-02-02 tracey
4784 eaf2c13e 2022-09-17 mark static void
4785 eaf2c13e 2022-09-17 mark search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4786 ec2a9698 2022-09-15 mark size_t *nlines, int **first, int **last, int **match, int **selected)
4787 f44b1f58 2020-02-02 tracey {
4788 eaf2c13e 2022-09-17 mark struct tog_diff_view_state *s = &view->state.diff;
4789 ec2a9698 2022-09-15 mark
4790 eaf2c13e 2022-09-17 mark *f = s->f;
4791 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
4792 eaf2c13e 2022-09-17 mark *line_offsets = NULL;
4793 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
4794 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
4795 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
4796 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
4797 ec2a9698 2022-09-15 mark }
4798 ec2a9698 2022-09-15 mark
4799 ec2a9698 2022-09-15 mark static const struct got_error *
4800 ec2a9698 2022-09-15 mark search_next_view_match(struct tog_view *view)
4801 ec2a9698 2022-09-15 mark {
4802 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4803 ec2a9698 2022-09-15 mark FILE *f;
4804 f44b1f58 2020-02-02 tracey int lineno;
4805 cb713507 2022-06-16 stsp char *line = NULL;
4806 826082fe 2020-12-10 stsp size_t linesize = 0;
4807 826082fe 2020-12-10 stsp ssize_t linelen;
4808 ec2a9698 2022-09-15 mark off_t *line_offsets;
4809 ec2a9698 2022-09-15 mark size_t nlines = 0;
4810 ec2a9698 2022-09-15 mark int *first, *last, *match, *selected;
4811 f44b1f58 2020-02-02 tracey
4812 eaf2c13e 2022-09-17 mark if (!view->search_setup)
4813 eaf2c13e 2022-09-17 mark return got_error_msg(GOT_ERR_NOT_IMPL,
4814 eaf2c13e 2022-09-17 mark "view search not supported");
4815 eaf2c13e 2022-09-17 mark view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4816 ec2a9698 2022-09-15 mark &match, &selected);
4817 ec2a9698 2022-09-15 mark
4818 f44b1f58 2020-02-02 tracey if (!view->searching) {
4819 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4820 f44b1f58 2020-02-02 tracey return NULL;
4821 f44b1f58 2020-02-02 tracey }
4822 f44b1f58 2020-02-02 tracey
4823 ec2a9698 2022-09-15 mark if (*match) {
4824 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4825 ec2a9698 2022-09-15 mark lineno = *match + 1;
4826 f44b1f58 2020-02-02 tracey else
4827 ec2a9698 2022-09-15 mark lineno = *match - 1;
4828 487cd7d2 2021-12-17 stsp } else
4829 ec2a9698 2022-09-15 mark lineno = *first - 1 + *selected;
4830 f44b1f58 2020-02-02 tracey
4831 f44b1f58 2020-02-02 tracey while (1) {
4832 f44b1f58 2020-02-02 tracey off_t offset;
4833 f44b1f58 2020-02-02 tracey
4834 ec2a9698 2022-09-15 mark if (lineno <= 0 || lineno > nlines) {
4835 ec2a9698 2022-09-15 mark if (*match == 0) {
4836 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4837 f44b1f58 2020-02-02 tracey break;
4838 f44b1f58 2020-02-02 tracey }
4839 f44b1f58 2020-02-02 tracey
4840 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4841 f44b1f58 2020-02-02 tracey lineno = 1;
4842 f44b1f58 2020-02-02 tracey else
4843 ec2a9698 2022-09-15 mark lineno = nlines;
4844 f44b1f58 2020-02-02 tracey }
4845 f44b1f58 2020-02-02 tracey
4846 ec2a9698 2022-09-15 mark offset = view->type == TOG_VIEW_DIFF ?
4847 ec2a9698 2022-09-15 mark view->state.diff.lines[lineno - 1].offset :
4848 ec2a9698 2022-09-15 mark line_offsets[lineno - 1];
4849 ec2a9698 2022-09-15 mark if (fseeko(f, offset, SEEK_SET) != 0) {
4850 f44b1f58 2020-02-02 tracey free(line);
4851 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4852 f44b1f58 2020-02-02 tracey }
4853 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesize, f);
4854 cb713507 2022-06-16 stsp if (linelen != -1) {
4855 cb713507 2022-06-16 stsp char *exstr;
4856 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4857 cb713507 2022-06-16 stsp if (err)
4858 cb713507 2022-06-16 stsp break;
4859 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4860 cb713507 2022-06-16 stsp &view->regmatch)) {
4861 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4862 ec2a9698 2022-09-15 mark *match = lineno;
4863 cb713507 2022-06-16 stsp free(exstr);
4864 cb713507 2022-06-16 stsp break;
4865 cb713507 2022-06-16 stsp }
4866 cb713507 2022-06-16 stsp free(exstr);
4867 f44b1f58 2020-02-02 tracey }
4868 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4869 f44b1f58 2020-02-02 tracey lineno++;
4870 f44b1f58 2020-02-02 tracey else
4871 f44b1f58 2020-02-02 tracey lineno--;
4872 f44b1f58 2020-02-02 tracey }
4873 826082fe 2020-12-10 stsp free(line);
4874 f44b1f58 2020-02-02 tracey
4875 ec2a9698 2022-09-15 mark if (*match) {
4876 ec2a9698 2022-09-15 mark *first = *match;
4877 ec2a9698 2022-09-15 mark *selected = 1;
4878 f44b1f58 2020-02-02 tracey }
4879 f44b1f58 2020-02-02 tracey
4880 145b6838 2022-06-16 stsp return err;
4881 f5215bb9 2019-02-22 stsp }
4882 f5215bb9 2019-02-22 stsp
4883 48ae06ee 2018-10-18 stsp static const struct got_error *
4884 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4885 b72706c3 2022-06-01 stsp {
4886 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4887 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4888 b72706c3 2022-06-01 stsp
4889 b72706c3 2022-06-01 stsp free(s->id1);
4890 b72706c3 2022-06-01 stsp s->id1 = NULL;
4891 b72706c3 2022-06-01 stsp free(s->id2);
4892 b72706c3 2022-06-01 stsp s->id2 = NULL;
4893 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4894 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4895 b72706c3 2022-06-01 stsp s->f = NULL;
4896 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4897 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4898 b72706c3 2022-06-01 stsp s->f1 = NULL;
4899 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4900 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4901 b72706c3 2022-06-01 stsp s->f2 = NULL;
4902 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4903 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4904 f9d37699 2022-06-28 stsp s->fd1 = -1;
4905 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4906 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4907 f9d37699 2022-06-28 stsp s->fd2 = -1;
4908 c7d5c43c 2022-08-04 mark free(s->lines);
4909 c7d5c43c 2022-08-04 mark s->lines = NULL;
4910 b72706c3 2022-06-01 stsp s->nlines = 0;
4911 b72706c3 2022-06-01 stsp return err;
4912 b72706c3 2022-06-01 stsp }
4913 b72706c3 2022-06-01 stsp
4914 b72706c3 2022-06-01 stsp static const struct got_error *
4915 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4916 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4917 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4918 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4919 48ae06ee 2018-10-18 stsp {
4920 48ae06ee 2018-10-18 stsp const struct got_error *err;
4921 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4922 5dc9f4bc 2018-08-04 stsp
4923 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4924 f9d37699 2022-06-28 stsp s->fd1 = -1;
4925 f9d37699 2022-06-28 stsp s->fd2 = -1;
4926 b72706c3 2022-06-01 stsp
4927 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4928 15a94983 2018-12-23 stsp int type1, type2;
4929 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4930 15a94983 2018-12-23 stsp if (err)
4931 15a94983 2018-12-23 stsp return err;
4932 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4933 15a94983 2018-12-23 stsp if (err)
4934 15a94983 2018-12-23 stsp return err;
4935 15a94983 2018-12-23 stsp
4936 15a94983 2018-12-23 stsp if (type1 != type2)
4937 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4938 15a94983 2018-12-23 stsp }
4939 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4940 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4941 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4942 f44b1f58 2020-02-02 tracey s->repo = repo;
4943 f44b1f58 2020-02-02 tracey s->id1 = id1;
4944 f44b1f58 2020-02-02 tracey s->id2 = id2;
4945 3dbaef42 2020-11-24 stsp s->label1 = label1;
4946 3dbaef42 2020-11-24 stsp s->label2 = label2;
4947 48ae06ee 2018-10-18 stsp
4948 15a94983 2018-12-23 stsp if (id1) {
4949 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4950 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4951 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4952 48ae06ee 2018-10-18 stsp } else
4953 5465d566 2020-02-01 tracey s->id1 = NULL;
4954 48ae06ee 2018-10-18 stsp
4955 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4956 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4957 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4958 b72706c3 2022-06-01 stsp goto done;
4959 48ae06ee 2018-10-18 stsp }
4960 b72706c3 2022-06-01 stsp
4961 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4962 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4963 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4964 a00719e9 2022-06-17 stsp goto done;
4965 a00719e9 2022-06-17 stsp }
4966 a00719e9 2022-06-17 stsp
4967 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4968 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4969 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4970 b72706c3 2022-06-01 stsp goto done;
4971 b72706c3 2022-06-01 stsp }
4972 b72706c3 2022-06-01 stsp
4973 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4974 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4975 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4976 f9d37699 2022-06-28 stsp goto done;
4977 f9d37699 2022-06-28 stsp }
4978 f9d37699 2022-06-28 stsp
4979 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4980 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4981 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4982 f9d37699 2022-06-28 stsp goto done;
4983 f9d37699 2022-06-28 stsp }
4984 f9d37699 2022-06-28 stsp
4985 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4986 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4987 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4988 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4989 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4990 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
4991 5465d566 2020-02-01 tracey s->repo = repo;
4992 6d17833f 2019-11-08 stsp
4993 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4994 6a5ff2d4 2022-08-06 mark int rc;
4995 6d17833f 2019-11-08 stsp
4996 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
4997 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4998 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4999 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
5000 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5001 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5002 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
5003 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5004 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5005 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
5006 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5007 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5008 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
5009 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5010 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5011 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5012 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5013 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5014 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5015 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5016 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5017 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5018 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
5019 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5020 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
5021 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
5022 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
5023 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
5024 b72706c3 2022-06-01 stsp goto done;
5025 6a5ff2d4 2022-08-06 mark }
5026 6d17833f 2019-11-08 stsp }
5027 5dc9f4bc 2018-08-04 stsp
5028 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5029 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
5030 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
5031 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5032 f5215bb9 2019-02-22 stsp
5033 5465d566 2020-02-01 tracey err = create_diff(s);
5034 48ae06ee 2018-10-18 stsp
5035 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5036 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5037 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
5038 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5039 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5040 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_diff_view;
5041 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
5042 b72706c3 2022-06-01 stsp done:
5043 b72706c3 2022-06-01 stsp if (err)
5044 b72706c3 2022-06-01 stsp close_diff_view(view);
5045 e5a0f69f 2018-08-18 stsp return err;
5046 5dc9f4bc 2018-08-04 stsp }
5047 5dc9f4bc 2018-08-04 stsp
5048 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5049 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5050 5dc9f4bc 2018-08-04 stsp {
5051 a3404814 2018-09-02 stsp const struct got_error *err;
5052 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5053 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5054 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5055 a3404814 2018-09-02 stsp
5056 a3404814 2018-09-02 stsp if (s->id1) {
5057 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5058 a3404814 2018-09-02 stsp if (err)
5059 a3404814 2018-09-02 stsp return err;
5060 4924afe1 2022-08-31 mark label1 = s->label1 ? s->label1 : id_str1;
5061 3dbaef42 2020-11-24 stsp } else
5062 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5063 3dbaef42 2020-11-24 stsp
5064 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5065 a3404814 2018-09-02 stsp if (err)
5066 a3404814 2018-09-02 stsp return err;
5067 4924afe1 2022-08-31 mark label2 = s->label2 ? s->label2 : id_str2;
5068 26ed57b2 2018-05-19 stsp
5069 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5070 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5071 a3404814 2018-09-02 stsp free(id_str1);
5072 a3404814 2018-09-02 stsp free(id_str2);
5073 a3404814 2018-09-02 stsp return err;
5074 a3404814 2018-09-02 stsp }
5075 a3404814 2018-09-02 stsp free(id_str1);
5076 a3404814 2018-09-02 stsp free(id_str2);
5077 a3404814 2018-09-02 stsp
5078 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5079 267bb3b8 2021-08-01 stsp free(header);
5080 267bb3b8 2021-08-01 stsp return err;
5081 15a087fe 2019-02-21 stsp }
5082 15a087fe 2019-02-21 stsp
5083 15a087fe 2019-02-21 stsp static const struct got_error *
5084 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5085 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5086 15a087fe 2019-02-21 stsp {
5087 d7a04538 2019-02-21 stsp const struct got_error *err;
5088 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5089 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5090 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5091 15a087fe 2019-02-21 stsp
5092 15a087fe 2019-02-21 stsp free(s->id2);
5093 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5094 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5095 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5096 15a087fe 2019-02-21 stsp
5097 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5098 d7a04538 2019-02-21 stsp if (err)
5099 d7a04538 2019-02-21 stsp return err;
5100 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5101 15a087fe 2019-02-21 stsp free(s->id1);
5102 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5103 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5104 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5105 15a087fe 2019-02-21 stsp return NULL;
5106 0cf4efb1 2018-09-29 stsp }
5107 0cf4efb1 2018-09-29 stsp
5108 0cf4efb1 2018-09-29 stsp static const struct got_error *
5109 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
5110 917d79a7 2022-07-01 stsp {
5111 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5112 917d79a7 2022-07-01 stsp
5113 917d79a7 2022-07-01 stsp view->count = 0;
5114 917d79a7 2022-07-01 stsp wclear(view->window);
5115 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
5116 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
5117 917d79a7 2022-07-01 stsp s->matched_line = 0;
5118 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
5119 917d79a7 2022-07-01 stsp return create_diff(s);
5120 c7d5c43c 2022-08-04 mark }
5121 c7d5c43c 2022-08-04 mark
5122 c7d5c43c 2022-08-04 mark static void
5123 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5124 c7d5c43c 2022-08-04 mark {
5125 c7d5c43c 2022-08-04 mark int start, i;
5126 c7d5c43c 2022-08-04 mark
5127 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
5128 c7d5c43c 2022-08-04 mark
5129 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5130 c7d5c43c 2022-08-04 mark if (i == 0)
5131 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
5132 c7d5c43c 2022-08-04 mark if (--i == start)
5133 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5134 c7d5c43c 2022-08-04 mark }
5135 c7d5c43c 2022-08-04 mark
5136 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5137 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5138 917d79a7 2022-07-01 stsp }
5139 917d79a7 2022-07-01 stsp
5140 c7d5c43c 2022-08-04 mark static void
5141 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5142 c7d5c43c 2022-08-04 mark {
5143 c7d5c43c 2022-08-04 mark int start, i;
5144 c7d5c43c 2022-08-04 mark
5145 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
5146 c7d5c43c 2022-08-04 mark
5147 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5148 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
5149 c7d5c43c 2022-08-04 mark i = 0;
5150 c7d5c43c 2022-08-04 mark if (++i == start)
5151 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5152 c7d5c43c 2022-08-04 mark }
5153 c7d5c43c 2022-08-04 mark
5154 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5155 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5156 c7d5c43c 2022-08-04 mark }
5157 c7d5c43c 2022-08-04 mark
5158 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5159 c0f61fa4 2022-07-11 mark int, int, int);
5160 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5161 c0f61fa4 2022-07-11 mark int, int);
5162 c0f61fa4 2022-07-11 mark
5163 917d79a7 2022-07-01 stsp static const struct got_error *
5164 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5165 e5a0f69f 2018-08-18 stsp {
5166 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5167 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5168 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5169 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5170 826082fe 2020-12-10 stsp char *line = NULL;
5171 826082fe 2020-12-10 stsp size_t linesize = 0;
5172 826082fe 2020-12-10 stsp ssize_t linelen;
5173 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
5174 e5a0f69f 2018-08-18 stsp
5175 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
5176 c7d5c43c 2022-08-04 mark
5177 e5a0f69f 2018-08-18 stsp switch (ch) {
5178 145b6838 2022-06-16 stsp case '0':
5179 145b6838 2022-06-16 stsp view->x = 0;
5180 145b6838 2022-06-16 stsp break;
5181 145b6838 2022-06-16 stsp case '$':
5182 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5183 640cd7ff 2022-06-22 mark view->count = 0;
5184 145b6838 2022-06-16 stsp break;
5185 145b6838 2022-06-16 stsp case KEY_RIGHT:
5186 145b6838 2022-06-16 stsp case 'l':
5187 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5188 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5189 640cd7ff 2022-06-22 mark else
5190 640cd7ff 2022-06-22 mark view->count = 0;
5191 145b6838 2022-06-16 stsp break;
5192 145b6838 2022-06-16 stsp case KEY_LEFT:
5193 145b6838 2022-06-16 stsp case 'h':
5194 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5195 640cd7ff 2022-06-22 mark if (view->x <= 0)
5196 640cd7ff 2022-06-22 mark view->count = 0;
5197 145b6838 2022-06-16 stsp break;
5198 64453f7e 2020-11-21 stsp case 'a':
5199 3dbaef42 2020-11-24 stsp case 'w':
5200 3dbaef42 2020-11-24 stsp if (ch == 'a')
5201 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
5202 3dbaef42 2020-11-24 stsp if (ch == 'w')
5203 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5204 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
5205 912a3f79 2021-08-30 j break;
5206 912a3f79 2021-08-30 j case 'g':
5207 912a3f79 2021-08-30 j case KEY_HOME:
5208 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5209 640cd7ff 2022-06-22 mark view->count = 0;
5210 64453f7e 2020-11-21 stsp break;
5211 912a3f79 2021-08-30 j case 'G':
5212 912a3f79 2021-08-30 j case KEY_END:
5213 640cd7ff 2022-06-22 mark view->count = 0;
5214 912a3f79 2021-08-30 j if (s->eof)
5215 912a3f79 2021-08-30 j break;
5216 912a3f79 2021-08-30 j
5217 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5218 912a3f79 2021-08-30 j s->eof = 1;
5219 912a3f79 2021-08-30 j break;
5220 1e37a5c2 2019-05-12 jcs case 'k':
5221 1e37a5c2 2019-05-12 jcs case KEY_UP:
5222 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5223 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5224 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5225 640cd7ff 2022-06-22 mark else
5226 640cd7ff 2022-06-22 mark view->count = 0;
5227 1e37a5c2 2019-05-12 jcs break;
5228 83cc4199 2022-06-13 stsp case CTRL('u'):
5229 33c3719a 2022-06-15 stsp case 'u':
5230 83cc4199 2022-06-13 stsp nscroll /= 2;
5231 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5232 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5233 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5234 61417565 2022-06-20 mark case 'b':
5235 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
5236 640cd7ff 2022-06-22 mark view->count = 0;
5237 26ed57b2 2018-05-19 stsp break;
5238 640cd7ff 2022-06-22 mark }
5239 1e37a5c2 2019-05-12 jcs i = 0;
5240 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
5241 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5242 1e37a5c2 2019-05-12 jcs break;
5243 1e37a5c2 2019-05-12 jcs case 'j':
5244 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5245 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5246 1e37a5c2 2019-05-12 jcs if (!s->eof)
5247 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5248 640cd7ff 2022-06-22 mark else
5249 640cd7ff 2022-06-22 mark view->count = 0;
5250 1e37a5c2 2019-05-12 jcs break;
5251 83cc4199 2022-06-13 stsp case CTRL('d'):
5252 33c3719a 2022-06-15 stsp case 'd':
5253 83cc4199 2022-06-13 stsp nscroll /= 2;
5254 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5255 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5256 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5257 61417565 2022-06-20 mark case 'f':
5258 1e37a5c2 2019-05-12 jcs case ' ':
5259 640cd7ff 2022-06-22 mark if (s->eof) {
5260 640cd7ff 2022-06-22 mark view->count = 0;
5261 1e37a5c2 2019-05-12 jcs break;
5262 640cd7ff 2022-06-22 mark }
5263 1e37a5c2 2019-05-12 jcs i = 0;
5264 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
5265 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5266 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5267 826082fe 2020-12-10 stsp if (linelen == -1) {
5268 826082fe 2020-12-10 stsp if (feof(s->f)) {
5269 826082fe 2020-12-10 stsp s->eof = 1;
5270 826082fe 2020-12-10 stsp } else
5271 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5272 34bc9ec9 2019-02-22 stsp break;
5273 826082fe 2020-12-10 stsp }
5274 1e37a5c2 2019-05-12 jcs }
5275 826082fe 2020-12-10 stsp free(line);
5276 1e37a5c2 2019-05-12 jcs break;
5277 c7d5c43c 2022-08-04 mark case '(':
5278 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5279 c7d5c43c 2022-08-04 mark break;
5280 c7d5c43c 2022-08-04 mark case ')':
5281 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5282 c7d5c43c 2022-08-04 mark break;
5283 c7d5c43c 2022-08-04 mark case '{':
5284 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5285 c7d5c43c 2022-08-04 mark break;
5286 c7d5c43c 2022-08-04 mark case '}':
5287 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
5288 c7d5c43c 2022-08-04 mark break;
5289 1e37a5c2 2019-05-12 jcs case '[':
5290 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5291 1e37a5c2 2019-05-12 jcs s->diff_context--;
5292 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5293 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5294 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5295 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5296 27829c9e 2020-11-21 stsp s->nlines) {
5297 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5298 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5299 27829c9e 2020-11-21 stsp }
5300 640cd7ff 2022-06-22 mark } else
5301 640cd7ff 2022-06-22 mark view->count = 0;
5302 1e37a5c2 2019-05-12 jcs break;
5303 1e37a5c2 2019-05-12 jcs case ']':
5304 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5305 1e37a5c2 2019-05-12 jcs s->diff_context++;
5306 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5307 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5308 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5309 640cd7ff 2022-06-22 mark } else
5310 640cd7ff 2022-06-22 mark view->count = 0;
5311 1e37a5c2 2019-05-12 jcs break;
5312 1e37a5c2 2019-05-12 jcs case '<':
5313 1e37a5c2 2019-05-12 jcs case ',':
5314 2b3e6702 2022-07-20 mark case 'K':
5315 c0f61fa4 2022-07-11 mark up = 1;
5316 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
5317 c0f61fa4 2022-07-11 mark case '>':
5318 c0f61fa4 2022-07-11 mark case '.':
5319 2b3e6702 2022-07-20 mark case 'J':
5320 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
5321 640cd7ff 2022-06-22 mark view->count = 0;
5322 48ae06ee 2018-10-18 stsp break;
5323 640cd7ff 2022-06-22 mark }
5324 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
5325 6524637e 2019-02-21 stsp
5326 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
5327 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
5328 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
5329 15a087fe 2019-02-21 stsp
5330 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
5331 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5332 c0f61fa4 2022-07-11 mark if (err)
5333 c0f61fa4 2022-07-11 mark break;
5334 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5335 15a087fe 2019-02-21 stsp
5336 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
5337 c0f61fa4 2022-07-11 mark break;
5338 15a087fe 2019-02-21 stsp
5339 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
5340 c0f61fa4 2022-07-11 mark if (err)
5341 c0f61fa4 2022-07-11 mark break;
5342 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5343 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
5344 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
5345 5e224a3e 2019-02-22 stsp
5346 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
5347 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
5348 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
5349 c0f61fa4 2022-07-11 mark
5350 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
5351 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5352 c0f61fa4 2022-07-11 mark if (err)
5353 c0f61fa4 2022-07-11 mark break;
5354 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5355 5a8b5076 2020-12-05 stsp
5356 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
5357 c0f61fa4 2022-07-11 mark break;
5358 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
5359 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
5360 c0f61fa4 2022-07-11 mark bs->selected_line);
5361 c0f61fa4 2022-07-11 mark if (id == NULL)
5362 c0f61fa4 2022-07-11 mark break;
5363 15a087fe 2019-02-21 stsp
5364 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5365 c0f61fa4 2022-07-11 mark break;
5366 15a087fe 2019-02-21 stsp
5367 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5368 c0f61fa4 2022-07-11 mark if (err)
5369 c0f61fa4 2022-07-11 mark break;
5370 c0f61fa4 2022-07-11 mark }
5371 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5372 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5373 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5374 145b6838 2022-06-16 stsp view->x = 0;
5375 1e37a5c2 2019-05-12 jcs
5376 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5377 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5378 1e37a5c2 2019-05-12 jcs break;
5379 1e37a5c2 2019-05-12 jcs default:
5380 640cd7ff 2022-06-22 mark view->count = 0;
5381 1e37a5c2 2019-05-12 jcs break;
5382 26ed57b2 2018-05-19 stsp }
5383 e5a0f69f 2018-08-18 stsp
5384 bcbd79e2 2018-08-19 stsp return err;
5385 26ed57b2 2018-05-19 stsp }
5386 26ed57b2 2018-05-19 stsp
5387 4ed7e80c 2018-05-20 stsp static const struct got_error *
5388 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5389 9f7d7167 2018-04-29 stsp {
5390 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5391 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5392 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5393 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5394 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5395 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5396 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5397 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5398 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5399 3dbaef42 2020-11-24 stsp const char *errstr;
5400 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5401 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5402 70ac5f84 2019-03-28 stsp
5403 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5404 26ed57b2 2018-05-19 stsp switch (ch) {
5405 64453f7e 2020-11-21 stsp case 'a':
5406 64453f7e 2020-11-21 stsp force_text_diff = 1;
5407 3dbaef42 2020-11-24 stsp break;
5408 3dbaef42 2020-11-24 stsp case 'C':
5409 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5410 3dbaef42 2020-11-24 stsp &errstr);
5411 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5412 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5413 5a20d08d 2022-02-09 op errstr, errstr);
5414 64453f7e 2020-11-21 stsp break;
5415 09b5bff8 2020-02-23 naddy case 'r':
5416 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5417 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5418 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5419 09b5bff8 2020-02-23 naddy optarg);
5420 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5421 09b5bff8 2020-02-23 naddy break;
5422 3dbaef42 2020-11-24 stsp case 'w':
5423 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5424 3dbaef42 2020-11-24 stsp break;
5425 26ed57b2 2018-05-19 stsp default:
5426 17020d27 2019-03-07 stsp usage_diff();
5427 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5428 26ed57b2 2018-05-19 stsp }
5429 26ed57b2 2018-05-19 stsp }
5430 26ed57b2 2018-05-19 stsp
5431 26ed57b2 2018-05-19 stsp argc -= optind;
5432 26ed57b2 2018-05-19 stsp argv += optind;
5433 26ed57b2 2018-05-19 stsp
5434 26ed57b2 2018-05-19 stsp if (argc == 0) {
5435 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5436 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5437 15a94983 2018-12-23 stsp id_str1 = argv[0];
5438 15a94983 2018-12-23 stsp id_str2 = argv[1];
5439 26ed57b2 2018-05-19 stsp } else
5440 26ed57b2 2018-05-19 stsp usage_diff();
5441 eb6600df 2019-01-04 stsp
5442 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5443 0ae84acc 2022-06-15 tracey if (error)
5444 0ae84acc 2022-06-15 tracey goto done;
5445 0ae84acc 2022-06-15 tracey
5446 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5447 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5448 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5449 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5450 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5451 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5452 c156c7a4 2020-12-18 stsp goto done;
5453 a273ac94 2020-02-23 naddy if (worktree)
5454 a273ac94 2020-02-23 naddy repo_path =
5455 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5456 a273ac94 2020-02-23 naddy else
5457 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5458 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5459 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5460 c156c7a4 2020-12-18 stsp goto done;
5461 c156c7a4 2020-12-18 stsp }
5462 a273ac94 2020-02-23 naddy }
5463 a273ac94 2020-02-23 naddy
5464 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5465 eb6600df 2019-01-04 stsp if (error)
5466 eb6600df 2019-01-04 stsp goto done;
5467 26ed57b2 2018-05-19 stsp
5468 a273ac94 2020-02-23 naddy init_curses();
5469 a273ac94 2020-02-23 naddy
5470 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5471 51a10b52 2020-12-26 stsp if (error)
5472 51a10b52 2020-12-26 stsp goto done;
5473 51a10b52 2020-12-26 stsp
5474 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5475 26ed57b2 2018-05-19 stsp if (error)
5476 26ed57b2 2018-05-19 stsp goto done;
5477 26ed57b2 2018-05-19 stsp
5478 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5479 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5480 26ed57b2 2018-05-19 stsp if (error)
5481 26ed57b2 2018-05-19 stsp goto done;
5482 26ed57b2 2018-05-19 stsp
5483 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5484 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5485 26ed57b2 2018-05-19 stsp if (error)
5486 26ed57b2 2018-05-19 stsp goto done;
5487 26ed57b2 2018-05-19 stsp
5488 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5489 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5490 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5491 ea5e7bb5 2018-08-01 stsp goto done;
5492 ea5e7bb5 2018-08-01 stsp }
5493 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5494 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5495 5dc9f4bc 2018-08-04 stsp if (error)
5496 5dc9f4bc 2018-08-04 stsp goto done;
5497 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5498 26ed57b2 2018-05-19 stsp done:
5499 3dbaef42 2020-11-24 stsp free(label1);
5500 3dbaef42 2020-11-24 stsp free(label2);
5501 c02c541e 2019-03-29 stsp free(repo_path);
5502 a273ac94 2020-02-23 naddy free(cwd);
5503 1d0f4054 2021-06-17 stsp if (repo) {
5504 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5505 1d0f4054 2021-06-17 stsp if (error == NULL)
5506 1d0f4054 2021-06-17 stsp error = close_err;
5507 1d0f4054 2021-06-17 stsp }
5508 a273ac94 2020-02-23 naddy if (worktree)
5509 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5510 0ae84acc 2022-06-15 tracey if (pack_fds) {
5511 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5512 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5513 0ae84acc 2022-06-15 tracey if (error == NULL)
5514 0ae84acc 2022-06-15 tracey error = pack_err;
5515 0ae84acc 2022-06-15 tracey }
5516 51a10b52 2020-12-26 stsp tog_free_refs();
5517 26ed57b2 2018-05-19 stsp return error;
5518 9f7d7167 2018-04-29 stsp }
5519 9f7d7167 2018-04-29 stsp
5520 4ed7e80c 2018-05-20 stsp __dead static void
5521 9f7d7167 2018-04-29 stsp usage_blame(void)
5522 9f7d7167 2018-04-29 stsp {
5523 80ddbec8 2018-04-29 stsp endwin();
5524 87411fa9 2022-06-16 stsp fprintf(stderr,
5525 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5526 9f7d7167 2018-04-29 stsp getprogname());
5527 9f7d7167 2018-04-29 stsp exit(1);
5528 9f7d7167 2018-04-29 stsp }
5529 84451b3e 2018-07-10 stsp
5530 84451b3e 2018-07-10 stsp struct tog_blame_line {
5531 84451b3e 2018-07-10 stsp int annotated;
5532 84451b3e 2018-07-10 stsp struct got_object_id *id;
5533 84451b3e 2018-07-10 stsp };
5534 9f7d7167 2018-04-29 stsp
5535 4ed7e80c 2018-05-20 stsp static const struct got_error *
5536 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5537 84451b3e 2018-07-10 stsp {
5538 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5539 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5540 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5541 84451b3e 2018-07-10 stsp const struct got_error *err;
5542 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5543 826082fe 2020-12-10 stsp char *line = NULL;
5544 826082fe 2020-12-10 stsp size_t linesize = 0;
5545 826082fe 2020-12-10 stsp ssize_t linelen;
5546 84451b3e 2018-07-10 stsp wchar_t *wline;
5547 27a741e5 2019-09-11 stsp int width;
5548 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5549 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5550 ab089a2a 2018-07-12 stsp char *id_str;
5551 11b20872 2019-11-08 stsp struct tog_color *tc;
5552 ab089a2a 2018-07-12 stsp
5553 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5554 ab089a2a 2018-07-12 stsp if (err)
5555 ab089a2a 2018-07-12 stsp return err;
5556 84451b3e 2018-07-10 stsp
5557 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5558 f7d12f7e 2018-08-01 stsp werase(view->window);
5559 84451b3e 2018-07-10 stsp
5560 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5561 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5562 ab089a2a 2018-07-12 stsp free(id_str);
5563 ab089a2a 2018-07-12 stsp return err;
5564 ab089a2a 2018-07-12 stsp }
5565 ab089a2a 2018-07-12 stsp
5566 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5567 ab089a2a 2018-07-12 stsp free(line);
5568 2550e4c3 2018-07-13 stsp line = NULL;
5569 1cae65b4 2019-09-22 stsp if (err)
5570 1cae65b4 2019-09-22 stsp return err;
5571 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5572 a3404814 2018-09-02 stsp wstandout(view->window);
5573 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5574 11b20872 2019-11-08 stsp if (tc)
5575 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5576 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5577 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
5578 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
5579 11b20872 2019-11-08 stsp if (tc)
5580 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5581 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5582 a3404814 2018-09-02 stsp wstandend(view->window);
5583 2550e4c3 2018-07-13 stsp free(wline);
5584 2550e4c3 2018-07-13 stsp wline = NULL;
5585 ab089a2a 2018-07-12 stsp
5586 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
5587 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
5588 94b80cfa 2022-08-01 mark
5589 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5590 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5591 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5592 ab089a2a 2018-07-12 stsp free(id_str);
5593 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5594 ab089a2a 2018-07-12 stsp }
5595 ab089a2a 2018-07-12 stsp free(id_str);
5596 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5597 3f60a8ef 2018-07-10 stsp free(line);
5598 2550e4c3 2018-07-13 stsp line = NULL;
5599 3f60a8ef 2018-07-10 stsp if (err)
5600 3f60a8ef 2018-07-10 stsp return err;
5601 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5602 2550e4c3 2018-07-13 stsp free(wline);
5603 2550e4c3 2018-07-13 stsp wline = NULL;
5604 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5605 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5606 3f60a8ef 2018-07-10 stsp
5607 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5608 145b6838 2022-06-16 stsp view->maxx = 0;
5609 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5610 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5611 826082fe 2020-12-10 stsp if (linelen == -1) {
5612 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5613 826082fe 2020-12-10 stsp s->eof = 1;
5614 826082fe 2020-12-10 stsp break;
5615 826082fe 2020-12-10 stsp }
5616 84451b3e 2018-07-10 stsp free(line);
5617 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5618 84451b3e 2018-07-10 stsp }
5619 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5620 94b80cfa 2022-08-01 mark continue;
5621 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
5622 826082fe 2020-12-10 stsp continue;
5623 1853e0f4 2022-06-16 stsp
5624 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5625 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5626 1853e0f4 2022-06-16 stsp if (err) {
5627 1853e0f4 2022-06-16 stsp free(line);
5628 1853e0f4 2022-06-16 stsp return err;
5629 1853e0f4 2022-06-16 stsp }
5630 1853e0f4 2022-06-16 stsp free(wline);
5631 1853e0f4 2022-06-16 stsp wline = NULL;
5632 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5633 84451b3e 2018-07-10 stsp
5634 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5635 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5636 b700b5d6 2018-07-10 stsp
5637 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5638 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5639 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5640 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5641 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5642 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5643 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5644 8d0fe45a 2019-08-12 stsp char *id_str;
5645 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5646 87411fa9 2022-06-16 stsp blame_line->id);
5647 8d0fe45a 2019-08-12 stsp if (err) {
5648 8d0fe45a 2019-08-12 stsp free(line);
5649 8d0fe45a 2019-08-12 stsp return err;
5650 8d0fe45a 2019-08-12 stsp }
5651 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5652 11b20872 2019-11-08 stsp if (tc)
5653 11b20872 2019-11-08 stsp wattr_on(view->window,
5654 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5655 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5656 11b20872 2019-11-08 stsp if (tc)
5657 11b20872 2019-11-08 stsp wattr_off(view->window,
5658 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5659 8d0fe45a 2019-08-12 stsp free(id_str);
5660 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5661 8d0fe45a 2019-08-12 stsp } else {
5662 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5663 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5664 84451b3e 2018-07-10 stsp }
5665 ee41ec32 2018-07-10 stsp } else {
5666 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5667 ee41ec32 2018-07-10 stsp prev_id = NULL;
5668 ee41ec32 2018-07-10 stsp }
5669 84451b3e 2018-07-10 stsp
5670 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5671 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5672 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5673 27a741e5 2019-09-11 stsp
5674 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5675 41605754 2020-11-12 stsp width = 9;
5676 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5677 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5678 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5679 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5680 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5681 41605754 2020-11-12 stsp if (err) {
5682 41605754 2020-11-12 stsp free(line);
5683 41605754 2020-11-12 stsp return err;
5684 41605754 2020-11-12 stsp }
5685 41605754 2020-11-12 stsp width += 9;
5686 41605754 2020-11-12 stsp } else {
5687 4cb9d869 2022-06-16 stsp int skip;
5688 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5689 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5690 4cb9d869 2022-06-16 stsp if (err) {
5691 4cb9d869 2022-06-16 stsp free(line);
5692 4cb9d869 2022-06-16 stsp return err;
5693 145b6838 2022-06-16 stsp }
5694 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5695 a75b3e2d 2022-06-16 stsp width += 9;
5696 41605754 2020-11-12 stsp free(wline);
5697 41605754 2020-11-12 stsp wline = NULL;
5698 41605754 2020-11-12 stsp }
5699 41605754 2020-11-12 stsp
5700 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5701 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5702 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5703 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5704 84451b3e 2018-07-10 stsp }
5705 826082fe 2020-12-10 stsp free(line);
5706 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5707 84451b3e 2018-07-10 stsp
5708 9b058f45 2022-06-30 mark view_border(view);
5709 84451b3e 2018-07-10 stsp
5710 84451b3e 2018-07-10 stsp return NULL;
5711 84451b3e 2018-07-10 stsp }
5712 84451b3e 2018-07-10 stsp
5713 84451b3e 2018-07-10 stsp static const struct got_error *
5714 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5715 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5716 84451b3e 2018-07-10 stsp {
5717 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5718 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5719 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5720 1a76625f 2018-10-22 stsp int errcode;
5721 84451b3e 2018-07-10 stsp
5722 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5723 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5724 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5725 84451b3e 2018-07-10 stsp
5726 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5727 1a76625f 2018-10-22 stsp if (errcode)
5728 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5729 84451b3e 2018-07-10 stsp
5730 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5731 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5732 d68a0a7d 2018-07-10 stsp goto done;
5733 d68a0a7d 2018-07-10 stsp }
5734 d68a0a7d 2018-07-10 stsp
5735 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5736 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5737 d68a0a7d 2018-07-10 stsp
5738 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5739 d68a0a7d 2018-07-10 stsp if (line->annotated)
5740 d68a0a7d 2018-07-10 stsp goto done;
5741 d68a0a7d 2018-07-10 stsp
5742 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5743 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5744 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5745 84451b3e 2018-07-10 stsp goto done;
5746 84451b3e 2018-07-10 stsp }
5747 84451b3e 2018-07-10 stsp line->annotated = 1;
5748 84451b3e 2018-07-10 stsp done:
5749 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5750 1a76625f 2018-10-22 stsp if (errcode)
5751 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5752 84451b3e 2018-07-10 stsp return err;
5753 84451b3e 2018-07-10 stsp }
5754 84451b3e 2018-07-10 stsp
5755 84451b3e 2018-07-10 stsp static void *
5756 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5757 84451b3e 2018-07-10 stsp {
5758 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5759 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5760 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5761 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5762 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5763 1b484788 2022-06-28 tracey
5764 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5765 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5766 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5767 e6e73e55 2022-06-30 tracey
5768 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5769 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5770 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5771 e6e73e55 2022-06-30 tracey goto done;
5772 e6e73e55 2022-06-30 tracey }
5773 18430de3 2018-07-10 stsp
5774 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5775 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5776 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5777 e6e73e55 2022-06-30 tracey goto done;
5778 e6e73e55 2022-06-30 tracey }
5779 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5780 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5781 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5782 e6e73e55 2022-06-30 tracey goto done;
5783 e6e73e55 2022-06-30 tracey }
5784 e6e73e55 2022-06-30 tracey
5785 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5786 61266923 2020-01-14 stsp if (err)
5787 e6e73e55 2022-06-30 tracey goto done;
5788 61266923 2020-01-14 stsp
5789 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5790 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5791 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5792 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5793 fc06ba56 2019-08-22 stsp err = NULL;
5794 18430de3 2018-07-10 stsp
5795 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5796 e6e73e55 2022-06-30 tracey if (errcode) {
5797 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5798 e6e73e55 2022-06-30 tracey goto done;
5799 e6e73e55 2022-06-30 tracey }
5800 18430de3 2018-07-10 stsp
5801 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5802 1d0f4054 2021-06-17 stsp if (err == NULL)
5803 1d0f4054 2021-06-17 stsp err = close_err;
5804 c9beca56 2018-07-22 stsp ta->repo = NULL;
5805 c9beca56 2018-07-22 stsp *ta->complete = 1;
5806 18430de3 2018-07-10 stsp
5807 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5808 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5809 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5810 18430de3 2018-07-10 stsp
5811 e6e73e55 2022-06-30 tracey done:
5812 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5813 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5814 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5815 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5816 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5817 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5818 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5819 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5820 1b484788 2022-06-28 tracey
5821 18430de3 2018-07-10 stsp return (void *)err;
5822 84451b3e 2018-07-10 stsp }
5823 84451b3e 2018-07-10 stsp
5824 245d91c1 2018-07-12 stsp static struct got_object_id *
5825 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5826 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5827 245d91c1 2018-07-12 stsp {
5828 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5829 8d0fe45a 2019-08-12 stsp
5830 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5831 8d0fe45a 2019-08-12 stsp return NULL;
5832 b880a918 2018-07-10 stsp
5833 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5834 c0f61fa4 2022-07-11 mark if (!line->annotated)
5835 c0f61fa4 2022-07-11 mark return NULL;
5836 c0f61fa4 2022-07-11 mark
5837 c0f61fa4 2022-07-11 mark return line->id;
5838 c0f61fa4 2022-07-11 mark }
5839 c0f61fa4 2022-07-11 mark
5840 c0f61fa4 2022-07-11 mark static struct got_object_id *
5841 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5842 c0f61fa4 2022-07-11 mark int lineno)
5843 c0f61fa4 2022-07-11 mark {
5844 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5845 c0f61fa4 2022-07-11 mark
5846 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5847 c0f61fa4 2022-07-11 mark return NULL;
5848 c0f61fa4 2022-07-11 mark
5849 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5850 245d91c1 2018-07-12 stsp if (!line->annotated)
5851 245d91c1 2018-07-12 stsp return NULL;
5852 245d91c1 2018-07-12 stsp
5853 245d91c1 2018-07-12 stsp return line->id;
5854 b880a918 2018-07-10 stsp }
5855 245d91c1 2018-07-12 stsp
5856 b880a918 2018-07-10 stsp static const struct got_error *
5857 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5858 a70480e0 2018-06-23 stsp {
5859 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5860 245d91c1 2018-07-12 stsp int i;
5861 245d91c1 2018-07-12 stsp
5862 245d91c1 2018-07-12 stsp if (blame->thread) {
5863 1a76625f 2018-10-22 stsp int errcode;
5864 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5865 1a76625f 2018-10-22 stsp if (errcode)
5866 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5867 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5868 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5869 1a76625f 2018-10-22 stsp if (errcode)
5870 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5871 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5872 1a76625f 2018-10-22 stsp if (errcode)
5873 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5874 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5875 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5876 245d91c1 2018-07-12 stsp err = NULL;
5877 245d91c1 2018-07-12 stsp blame->thread = NULL;
5878 245d91c1 2018-07-12 stsp }
5879 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5880 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5881 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5882 1d0f4054 2021-06-17 stsp if (err == NULL)
5883 1d0f4054 2021-06-17 stsp err = close_err;
5884 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5885 245d91c1 2018-07-12 stsp }
5886 245d91c1 2018-07-12 stsp if (blame->f) {
5887 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5888 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5889 245d91c1 2018-07-12 stsp blame->f = NULL;
5890 245d91c1 2018-07-12 stsp }
5891 57670559 2018-12-23 stsp if (blame->lines) {
5892 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5893 57670559 2018-12-23 stsp free(blame->lines[i].id);
5894 57670559 2018-12-23 stsp free(blame->lines);
5895 57670559 2018-12-23 stsp blame->lines = NULL;
5896 57670559 2018-12-23 stsp }
5897 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5898 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5899 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5900 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5901 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5902 0ae84acc 2022-06-15 tracey if (err == NULL)
5903 0ae84acc 2022-06-15 tracey err = pack_err;
5904 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5905 0ae84acc 2022-06-15 tracey }
5906 245d91c1 2018-07-12 stsp return err;
5907 245d91c1 2018-07-12 stsp }
5908 245d91c1 2018-07-12 stsp
5909 245d91c1 2018-07-12 stsp static const struct got_error *
5910 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5911 fc06ba56 2019-08-22 stsp {
5912 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5913 fc06ba56 2019-08-22 stsp int *done = arg;
5914 fc06ba56 2019-08-22 stsp int errcode;
5915 fc06ba56 2019-08-22 stsp
5916 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5917 fc06ba56 2019-08-22 stsp if (errcode)
5918 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5919 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5920 fc06ba56 2019-08-22 stsp
5921 fc06ba56 2019-08-22 stsp if (*done)
5922 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5923 fc06ba56 2019-08-22 stsp
5924 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5925 fc06ba56 2019-08-22 stsp if (errcode)
5926 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5927 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5928 fc06ba56 2019-08-22 stsp
5929 fc06ba56 2019-08-22 stsp return err;
5930 fc06ba56 2019-08-22 stsp }
5931 fc06ba56 2019-08-22 stsp
5932 fc06ba56 2019-08-22 stsp static const struct got_error *
5933 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5934 245d91c1 2018-07-12 stsp {
5935 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5936 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5937 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5938 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5939 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5940 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5941 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5942 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5943 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5944 a70480e0 2018-06-23 stsp
5945 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5946 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5947 27d434c2 2018-09-15 stsp if (err)
5948 15a94983 2018-12-23 stsp return err;
5949 eb81bc23 2022-06-28 tracey
5950 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5951 eb81bc23 2022-06-28 tracey if (fd == -1) {
5952 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5953 eb81bc23 2022-06-28 tracey goto done;
5954 eb81bc23 2022-06-28 tracey }
5955 a44927cc 2022-04-07 stsp
5956 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5957 a44927cc 2022-04-07 stsp if (err)
5958 a44927cc 2022-04-07 stsp goto done;
5959 27d434c2 2018-09-15 stsp
5960 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5961 84451b3e 2018-07-10 stsp if (err)
5962 84451b3e 2018-07-10 stsp goto done;
5963 27d434c2 2018-09-15 stsp
5964 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5965 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5966 84451b3e 2018-07-10 stsp goto done;
5967 84451b3e 2018-07-10 stsp }
5968 a70480e0 2018-06-23 stsp
5969 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5970 a70480e0 2018-06-23 stsp if (err)
5971 a70480e0 2018-06-23 stsp goto done;
5972 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5973 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5974 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5975 84451b3e 2018-07-10 stsp goto done;
5976 84451b3e 2018-07-10 stsp }
5977 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5978 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5979 1fddf795 2021-01-20 stsp if (err)
5980 1fddf795 2021-01-20 stsp goto done;
5981 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5982 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5983 84451b3e 2018-07-10 stsp goto done;
5984 1fddf795 2021-01-20 stsp }
5985 b02560ec 2019-08-19 stsp
5986 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5987 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5988 b02560ec 2019-08-19 stsp blame->nlines--;
5989 a70480e0 2018-06-23 stsp
5990 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5991 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5992 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5993 84451b3e 2018-07-10 stsp goto done;
5994 84451b3e 2018-07-10 stsp }
5995 a70480e0 2018-06-23 stsp
5996 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5997 bd24772e 2018-07-11 stsp if (err)
5998 bd24772e 2018-07-11 stsp goto done;
5999 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6000 0ae84acc 2022-06-15 tracey pack_fds);
6001 0ae84acc 2022-06-15 tracey if (err)
6002 0ae84acc 2022-06-15 tracey goto done;
6003 bd24772e 2018-07-11 stsp
6004 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
6005 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6006 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6007 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6008 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6009 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6010 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6011 245d91c1 2018-07-12 stsp goto done;
6012 245d91c1 2018-07-12 stsp }
6013 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6014 245d91c1 2018-07-12 stsp
6015 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6016 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6017 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6018 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6019 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6020 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6021 a5388363 2020-12-01 naddy s->blame_complete = 0;
6022 f5a09613 2020-12-13 naddy
6023 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6024 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6025 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6026 f5a09613 2020-12-13 naddy s->selected_line = 1;
6027 f5a09613 2020-12-13 naddy }
6028 67b5eae1 2021-12-27 naddy s->matched_line = 0;
6029 245d91c1 2018-07-12 stsp
6030 245d91c1 2018-07-12 stsp done:
6031 a44927cc 2022-04-07 stsp if (commit)
6032 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6033 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
6034 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
6035 245d91c1 2018-07-12 stsp if (blob)
6036 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6037 27d434c2 2018-09-15 stsp free(obj_id);
6038 245d91c1 2018-07-12 stsp if (err)
6039 245d91c1 2018-07-12 stsp stop_blame(blame);
6040 245d91c1 2018-07-12 stsp return err;
6041 245d91c1 2018-07-12 stsp }
6042 245d91c1 2018-07-12 stsp
6043 245d91c1 2018-07-12 stsp static const struct got_error *
6044 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6045 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6046 245d91c1 2018-07-12 stsp {
6047 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6048 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6049 dbc6a6b6 2018-07-12 stsp
6050 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6051 245d91c1 2018-07-12 stsp
6052 c4843652 2019-08-12 stsp s->path = strdup(path);
6053 c4843652 2019-08-12 stsp if (s->path == NULL)
6054 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6055 c4843652 2019-08-12 stsp
6056 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6057 c4843652 2019-08-12 stsp if (err) {
6058 c4843652 2019-08-12 stsp free(s->path);
6059 7cbe629d 2018-08-04 stsp return err;
6060 c4843652 2019-08-12 stsp }
6061 245d91c1 2018-07-12 stsp
6062 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6063 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6064 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6065 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6066 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6067 fb2756b9 2018-08-04 stsp s->repo = repo;
6068 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6069 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6070 7cbe629d 2018-08-04 stsp
6071 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6072 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6073 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6074 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6075 11b20872 2019-11-08 stsp if (err)
6076 11b20872 2019-11-08 stsp return err;
6077 11b20872 2019-11-08 stsp }
6078 11b20872 2019-11-08 stsp
6079 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6080 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6081 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
6082 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6083 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6084 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_blame_view;
6085 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
6086 e5a0f69f 2018-08-18 stsp
6087 a5388363 2020-12-01 naddy return run_blame(view);
6088 7cbe629d 2018-08-04 stsp }
6089 7cbe629d 2018-08-04 stsp
6090 e5a0f69f 2018-08-18 stsp static const struct got_error *
6091 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6092 7cbe629d 2018-08-04 stsp {
6093 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6094 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6095 7cbe629d 2018-08-04 stsp
6096 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6097 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6098 e5a0f69f 2018-08-18 stsp
6099 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6100 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6101 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6102 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6103 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6104 7cbe629d 2018-08-04 stsp }
6105 e5a0f69f 2018-08-18 stsp
6106 e5a0f69f 2018-08-18 stsp free(s->path);
6107 11b20872 2019-11-08 stsp free_colors(&s->colors);
6108 e5a0f69f 2018-08-18 stsp return err;
6109 7cbe629d 2018-08-04 stsp }
6110 7cbe629d 2018-08-04 stsp
6111 7cbe629d 2018-08-04 stsp static const struct got_error *
6112 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6113 6c4c42e0 2019-06-24 stsp {
6114 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6115 6c4c42e0 2019-06-24 stsp
6116 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6117 6c4c42e0 2019-06-24 stsp return NULL;
6118 eaf2c13e 2022-09-17 mark }
6119 eaf2c13e 2022-09-17 mark
6120 eaf2c13e 2022-09-17 mark static void
6121 eaf2c13e 2022-09-17 mark search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6122 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
6123 eaf2c13e 2022-09-17 mark {
6124 eaf2c13e 2022-09-17 mark struct tog_blame_view_state *s = &view->state.blame;
6125 eaf2c13e 2022-09-17 mark
6126 eaf2c13e 2022-09-17 mark *f = s->blame.f;
6127 eaf2c13e 2022-09-17 mark *nlines = s->blame.nlines;
6128 eaf2c13e 2022-09-17 mark *line_offsets = s->blame.line_offsets;
6129 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
6130 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
6131 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
6132 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
6133 6c4c42e0 2019-06-24 stsp }
6134 6c4c42e0 2019-06-24 stsp
6135 6c4c42e0 2019-06-24 stsp static const struct got_error *
6136 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6137 7cbe629d 2018-08-04 stsp {
6138 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6139 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6140 2b380cc8 2018-10-24 stsp int errcode;
6141 2b380cc8 2018-10-24 stsp
6142 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
6143 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6144 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6145 2b380cc8 2018-10-24 stsp if (errcode)
6146 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6147 51fe7530 2019-08-19 stsp
6148 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6149 2b380cc8 2018-10-24 stsp }
6150 e5a0f69f 2018-08-18 stsp
6151 51fe7530 2019-08-19 stsp if (s->blame_complete)
6152 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6153 51fe7530 2019-08-19 stsp
6154 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6155 e5a0f69f 2018-08-18 stsp
6156 9b058f45 2022-06-30 mark view_border(view);
6157 e5a0f69f 2018-08-18 stsp return err;
6158 e5a0f69f 2018-08-18 stsp }
6159 e5a0f69f 2018-08-18 stsp
6160 e5a0f69f 2018-08-18 stsp static const struct got_error *
6161 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6162 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
6163 05f04cdf 2022-07-20 mark {
6164 05f04cdf 2022-07-20 mark struct tog_view *log_view;
6165 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
6166 05f04cdf 2022-07-20 mark
6167 05f04cdf 2022-07-20 mark *new_view = NULL;
6168 05f04cdf 2022-07-20 mark
6169 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6170 05f04cdf 2022-07-20 mark if (log_view == NULL)
6171 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
6172 05f04cdf 2022-07-20 mark
6173 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6174 05f04cdf 2022-07-20 mark if (err)
6175 05f04cdf 2022-07-20 mark view_close(log_view);
6176 05f04cdf 2022-07-20 mark else
6177 05f04cdf 2022-07-20 mark *new_view = log_view;
6178 05f04cdf 2022-07-20 mark
6179 05f04cdf 2022-07-20 mark return err;
6180 05f04cdf 2022-07-20 mark }
6181 05f04cdf 2022-07-20 mark
6182 05f04cdf 2022-07-20 mark static const struct got_error *
6183 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6184 e5a0f69f 2018-08-18 stsp {
6185 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6186 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
6187 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6188 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
6189 9b058f45 2022-06-30 mark
6190 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
6191 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6192 9b058f45 2022-06-30 mark --eos; /* border */
6193 7cbe629d 2018-08-04 stsp
6194 e5a0f69f 2018-08-18 stsp switch (ch) {
6195 145b6838 2022-06-16 stsp case '0':
6196 145b6838 2022-06-16 stsp view->x = 0;
6197 145b6838 2022-06-16 stsp break;
6198 145b6838 2022-06-16 stsp case '$':
6199 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
6200 640cd7ff 2022-06-22 mark view->count = 0;
6201 145b6838 2022-06-16 stsp break;
6202 145b6838 2022-06-16 stsp case KEY_RIGHT:
6203 145b6838 2022-06-16 stsp case 'l':
6204 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
6205 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
6206 640cd7ff 2022-06-22 mark else
6207 640cd7ff 2022-06-22 mark view->count = 0;
6208 145b6838 2022-06-16 stsp break;
6209 145b6838 2022-06-16 stsp case KEY_LEFT:
6210 145b6838 2022-06-16 stsp case 'h':
6211 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
6212 640cd7ff 2022-06-22 mark if (view->x <= 0)
6213 640cd7ff 2022-06-22 mark view->count = 0;
6214 145b6838 2022-06-16 stsp break;
6215 1e37a5c2 2019-05-12 jcs case 'q':
6216 1e37a5c2 2019-05-12 jcs s->done = 1;
6217 4deef56f 2021-09-02 naddy break;
6218 4deef56f 2021-09-02 naddy case 'g':
6219 4deef56f 2021-09-02 naddy case KEY_HOME:
6220 4deef56f 2021-09-02 naddy s->selected_line = 1;
6221 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6222 640cd7ff 2022-06-22 mark view->count = 0;
6223 4deef56f 2021-09-02 naddy break;
6224 4deef56f 2021-09-02 naddy case 'G':
6225 4deef56f 2021-09-02 naddy case KEY_END:
6226 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
6227 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6228 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6229 4deef56f 2021-09-02 naddy } else {
6230 9b058f45 2022-06-30 mark s->selected_line = eos;
6231 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
6232 4deef56f 2021-09-02 naddy }
6233 640cd7ff 2022-06-22 mark view->count = 0;
6234 1e37a5c2 2019-05-12 jcs break;
6235 1e37a5c2 2019-05-12 jcs case 'k':
6236 1e37a5c2 2019-05-12 jcs case KEY_UP:
6237 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6238 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6239 1e37a5c2 2019-05-12 jcs s->selected_line--;
6240 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6241 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6242 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6243 640cd7ff 2022-06-22 mark else
6244 640cd7ff 2022-06-22 mark view->count = 0;
6245 1e37a5c2 2019-05-12 jcs break;
6246 83cc4199 2022-06-13 stsp case CTRL('u'):
6247 33c3719a 2022-06-15 stsp case 'u':
6248 83cc4199 2022-06-13 stsp nscroll /= 2;
6249 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6250 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6251 ea025d1d 2020-02-22 naddy case CTRL('b'):
6252 61417565 2022-06-20 mark case 'b':
6253 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6254 640cd7ff 2022-06-22 mark if (view->count > 1)
6255 640cd7ff 2022-06-22 mark nscroll += nscroll;
6256 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
6257 640cd7ff 2022-06-22 mark view->count = 0;
6258 e5a0f69f 2018-08-18 stsp break;
6259 1e37a5c2 2019-05-12 jcs }
6260 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
6261 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
6262 1e37a5c2 2019-05-12 jcs else
6263 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6264 1e37a5c2 2019-05-12 jcs break;
6265 1e37a5c2 2019-05-12 jcs case 'j':
6266 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6267 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6268 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
6269 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6270 1e37a5c2 2019-05-12 jcs s->selected_line++;
6271 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6272 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6273 640cd7ff 2022-06-22 mark else
6274 640cd7ff 2022-06-22 mark view->count = 0;
6275 1e37a5c2 2019-05-12 jcs break;
6276 61417565 2022-06-20 mark case 'c':
6277 1e37a5c2 2019-05-12 jcs case 'p': {
6278 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6279 640cd7ff 2022-06-22 mark
6280 640cd7ff 2022-06-22 mark view->count = 0;
6281 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6282 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6283 1e37a5c2 2019-05-12 jcs if (id == NULL)
6284 e5a0f69f 2018-08-18 stsp break;
6285 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6286 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
6287 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6288 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6289 1e37a5c2 2019-05-12 jcs int obj_type;
6290 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6291 1e37a5c2 2019-05-12 jcs s->repo, id);
6292 e5a0f69f 2018-08-18 stsp if (err)
6293 e5a0f69f 2018-08-18 stsp break;
6294 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6295 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6296 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6297 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6298 e5a0f69f 2018-08-18 stsp break;
6299 e5a0f69f 2018-08-18 stsp }
6300 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6301 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
6302 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
6303 a44927cc 2022-04-07 stsp if (err)
6304 a44927cc 2022-04-07 stsp break;
6305 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6306 a44927cc 2022-04-07 stsp pcommit, s->path);
6307 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6308 e5a0f69f 2018-08-18 stsp if (err) {
6309 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6310 1e37a5c2 2019-05-12 jcs err = NULL;
6311 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6312 e5a0f69f 2018-08-18 stsp break;
6313 e5a0f69f 2018-08-18 stsp }
6314 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6315 1e37a5c2 2019-05-12 jcs blob_id);
6316 1e37a5c2 2019-05-12 jcs free(blob_id);
6317 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6318 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6319 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6320 e5a0f69f 2018-08-18 stsp break;
6321 1e37a5c2 2019-05-12 jcs }
6322 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6323 d7b5a0e8 2022-04-20 stsp &pid->id);
6324 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6325 1e37a5c2 2019-05-12 jcs } else {
6326 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6327 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6328 1e37a5c2 2019-05-12 jcs break;
6329 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6330 1e37a5c2 2019-05-12 jcs id);
6331 1e37a5c2 2019-05-12 jcs }
6332 1e37a5c2 2019-05-12 jcs if (err)
6333 e5a0f69f 2018-08-18 stsp break;
6334 1e37a5c2 2019-05-12 jcs s->done = 1;
6335 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6336 1e37a5c2 2019-05-12 jcs s->done = 0;
6337 1e37a5c2 2019-05-12 jcs if (thread_err)
6338 1e37a5c2 2019-05-12 jcs break;
6339 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6340 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6341 a5388363 2020-12-01 naddy err = run_blame(view);
6342 1e37a5c2 2019-05-12 jcs if (err)
6343 1e37a5c2 2019-05-12 jcs break;
6344 1e37a5c2 2019-05-12 jcs break;
6345 1e37a5c2 2019-05-12 jcs }
6346 61417565 2022-06-20 mark case 'C': {
6347 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6348 640cd7ff 2022-06-22 mark
6349 640cd7ff 2022-06-22 mark view->count = 0;
6350 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6351 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6352 1e37a5c2 2019-05-12 jcs break;
6353 1e37a5c2 2019-05-12 jcs s->done = 1;
6354 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6355 1e37a5c2 2019-05-12 jcs s->done = 0;
6356 1e37a5c2 2019-05-12 jcs if (thread_err)
6357 1e37a5c2 2019-05-12 jcs break;
6358 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6359 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6360 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6361 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6362 a5388363 2020-12-01 naddy err = run_blame(view);
6363 1e37a5c2 2019-05-12 jcs if (err)
6364 1e37a5c2 2019-05-12 jcs break;
6365 1e37a5c2 2019-05-12 jcs break;
6366 1e37a5c2 2019-05-12 jcs }
6367 136e2bd4 2022-07-23 mark case 'L':
6368 05f04cdf 2022-07-20 mark view->count = 0;
6369 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6370 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6371 136e2bd4 2022-07-23 mark if (s->id_to_log)
6372 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6373 05f04cdf 2022-07-20 mark break;
6374 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6375 1e37a5c2 2019-05-12 jcs case '\r': {
6376 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6377 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6378 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6379 640cd7ff 2022-06-22 mark
6380 640cd7ff 2022-06-22 mark view->count = 0;
6381 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6382 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6383 1e37a5c2 2019-05-12 jcs if (id == NULL)
6384 1e37a5c2 2019-05-12 jcs break;
6385 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6386 1e37a5c2 2019-05-12 jcs if (err)
6387 1e37a5c2 2019-05-12 jcs break;
6388 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6389 c0f61fa4 2022-07-11 mark if (*new_view) {
6390 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6391 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6392 c0f61fa4 2022-07-11 mark if (err)
6393 c0f61fa4 2022-07-11 mark break;
6394 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6395 c0f61fa4 2022-07-11 mark } else {
6396 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6397 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6398 9b058f45 2022-06-30 mark
6399 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6400 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6401 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6402 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6403 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6404 c0f61fa4 2022-07-11 mark break;
6405 c0f61fa4 2022-07-11 mark }
6406 15a94983 2018-12-23 stsp }
6407 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6408 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6409 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6410 1e37a5c2 2019-05-12 jcs if (err) {
6411 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6412 1e37a5c2 2019-05-12 jcs break;
6413 1e37a5c2 2019-05-12 jcs }
6414 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6415 c0f61fa4 2022-07-11 mark s->selected_line;
6416 c0f61fa4 2022-07-11 mark if (*new_view)
6417 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6418 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6419 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6420 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6421 9b058f45 2022-06-30 mark if (err)
6422 9b058f45 2022-06-30 mark break;
6423 9b058f45 2022-06-30 mark }
6424 9b058f45 2022-06-30 mark
6425 e78dc838 2020-12-04 stsp view->focussed = 0;
6426 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6427 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6428 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6429 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6430 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6431 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6432 1e37a5c2 2019-05-12 jcs if (err)
6433 34bc9ec9 2019-02-22 stsp break;
6434 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6435 0dbbbe90 2022-06-17 op if (err)
6436 0dbbbe90 2022-06-17 op break;
6437 e78dc838 2020-12-04 stsp view->focus_child = 1;
6438 1e37a5c2 2019-05-12 jcs } else
6439 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6440 1e37a5c2 2019-05-12 jcs if (err)
6441 e5a0f69f 2018-08-18 stsp break;
6442 1e37a5c2 2019-05-12 jcs break;
6443 1e37a5c2 2019-05-12 jcs }
6444 83cc4199 2022-06-13 stsp case CTRL('d'):
6445 33c3719a 2022-06-15 stsp case 'd':
6446 83cc4199 2022-06-13 stsp nscroll /= 2;
6447 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6448 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6449 ea025d1d 2020-02-22 naddy case CTRL('f'):
6450 61417565 2022-06-20 mark case 'f':
6451 1e37a5c2 2019-05-12 jcs case ' ':
6452 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6453 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6454 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6455 640cd7ff 2022-06-22 mark view->count = 0;
6456 e5a0f69f 2018-08-18 stsp break;
6457 1e37a5c2 2019-05-12 jcs }
6458 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6459 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6460 83cc4199 2022-06-13 stsp s->selected_line +=
6461 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6462 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6463 1e37a5c2 2019-05-12 jcs }
6464 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6465 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6466 1e37a5c2 2019-05-12 jcs else
6467 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6468 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6469 1e37a5c2 2019-05-12 jcs break;
6470 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6471 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6472 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6473 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6474 1e37a5c2 2019-05-12 jcs }
6475 1e37a5c2 2019-05-12 jcs break;
6476 1e37a5c2 2019-05-12 jcs default:
6477 640cd7ff 2022-06-22 mark view->count = 0;
6478 1e37a5c2 2019-05-12 jcs break;
6479 a70480e0 2018-06-23 stsp }
6480 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6481 917d79a7 2022-07-01 stsp }
6482 917d79a7 2022-07-01 stsp
6483 917d79a7 2022-07-01 stsp static const struct got_error *
6484 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6485 917d79a7 2022-07-01 stsp {
6486 917d79a7 2022-07-01 stsp const struct got_error *err;
6487 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6488 917d79a7 2022-07-01 stsp
6489 917d79a7 2022-07-01 stsp view->count = 0;
6490 917d79a7 2022-07-01 stsp s->done = 1;
6491 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6492 917d79a7 2022-07-01 stsp s->done = 0;
6493 917d79a7 2022-07-01 stsp if (err)
6494 917d79a7 2022-07-01 stsp return err;
6495 917d79a7 2022-07-01 stsp return run_blame(view);
6496 a70480e0 2018-06-23 stsp }
6497 a70480e0 2018-06-23 stsp
6498 a70480e0 2018-06-23 stsp static const struct got_error *
6499 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6500 9f7d7167 2018-04-29 stsp {
6501 a70480e0 2018-06-23 stsp const struct got_error *error;
6502 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6503 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6504 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6505 0587e10c 2020-07-23 stsp char *link_target = NULL;
6506 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6507 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6508 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6509 a70480e0 2018-06-23 stsp int ch;
6510 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6511 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6512 a70480e0 2018-06-23 stsp
6513 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6514 a70480e0 2018-06-23 stsp switch (ch) {
6515 a70480e0 2018-06-23 stsp case 'c':
6516 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6517 a70480e0 2018-06-23 stsp break;
6518 69069811 2018-08-02 stsp case 'r':
6519 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6520 69069811 2018-08-02 stsp if (repo_path == NULL)
6521 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6522 9ba1d308 2019-10-21 stsp optarg);
6523 69069811 2018-08-02 stsp break;
6524 a70480e0 2018-06-23 stsp default:
6525 17020d27 2019-03-07 stsp usage_blame();
6526 a70480e0 2018-06-23 stsp /* NOTREACHED */
6527 a70480e0 2018-06-23 stsp }
6528 a70480e0 2018-06-23 stsp }
6529 a70480e0 2018-06-23 stsp
6530 a70480e0 2018-06-23 stsp argc -= optind;
6531 a70480e0 2018-06-23 stsp argv += optind;
6532 a70480e0 2018-06-23 stsp
6533 f135c941 2020-02-20 stsp if (argc != 1)
6534 a70480e0 2018-06-23 stsp usage_blame();
6535 6962eb72 2020-02-20 stsp
6536 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6537 0ae84acc 2022-06-15 tracey if (error != NULL)
6538 0ae84acc 2022-06-15 tracey goto done;
6539 0ae84acc 2022-06-15 tracey
6540 69069811 2018-08-02 stsp if (repo_path == NULL) {
6541 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6542 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6543 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6544 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6545 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6546 c156c7a4 2020-12-18 stsp goto done;
6547 f135c941 2020-02-20 stsp if (worktree)
6548 eb41ed75 2019-02-05 stsp repo_path =
6549 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6550 f135c941 2020-02-20 stsp else
6551 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6552 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6553 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6554 c156c7a4 2020-12-18 stsp goto done;
6555 c156c7a4 2020-12-18 stsp }
6556 f135c941 2020-02-20 stsp }
6557 a915003a 2019-02-05 stsp
6558 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6559 c02c541e 2019-03-29 stsp if (error != NULL)
6560 8e94dd5b 2019-01-04 stsp goto done;
6561 69069811 2018-08-02 stsp
6562 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6563 f135c941 2020-02-20 stsp worktree);
6564 c02c541e 2019-03-29 stsp if (error)
6565 92205607 2019-01-04 stsp goto done;
6566 69069811 2018-08-02 stsp
6567 f135c941 2020-02-20 stsp init_curses();
6568 f135c941 2020-02-20 stsp
6569 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6570 51a10b52 2020-12-26 stsp if (error)
6571 51a10b52 2020-12-26 stsp goto done;
6572 51a10b52 2020-12-26 stsp
6573 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6574 eb41ed75 2019-02-05 stsp if (error)
6575 69069811 2018-08-02 stsp goto done;
6576 a70480e0 2018-06-23 stsp
6577 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6578 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6579 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6580 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6581 a70480e0 2018-06-23 stsp if (error != NULL)
6582 66b4983c 2018-06-23 stsp goto done;
6583 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6584 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6585 a70480e0 2018-06-23 stsp } else {
6586 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6587 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6588 a70480e0 2018-06-23 stsp }
6589 a19e88aa 2018-06-23 stsp if (error != NULL)
6590 8b473291 2019-02-21 stsp goto done;
6591 8b473291 2019-02-21 stsp
6592 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6593 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6594 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6595 e1cd8fed 2018-08-01 stsp goto done;
6596 e1cd8fed 2018-08-01 stsp }
6597 0587e10c 2020-07-23 stsp
6598 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6599 a44927cc 2022-04-07 stsp if (error)
6600 a44927cc 2022-04-07 stsp goto done;
6601 a44927cc 2022-04-07 stsp
6602 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6603 a44927cc 2022-04-07 stsp commit, repo);
6604 7cbe629d 2018-08-04 stsp if (error)
6605 7cbe629d 2018-08-04 stsp goto done;
6606 0587e10c 2020-07-23 stsp
6607 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6608 78756c87 2020-11-24 stsp commit_id, repo);
6609 0587e10c 2020-07-23 stsp if (error)
6610 0587e10c 2020-07-23 stsp goto done;
6611 12314ad4 2019-08-31 stsp if (worktree) {
6612 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6613 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6614 12314ad4 2019-08-31 stsp worktree = NULL;
6615 12314ad4 2019-08-31 stsp }
6616 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6617 a70480e0 2018-06-23 stsp done:
6618 69069811 2018-08-02 stsp free(repo_path);
6619 f135c941 2020-02-20 stsp free(in_repo_path);
6620 0587e10c 2020-07-23 stsp free(link_target);
6621 69069811 2018-08-02 stsp free(cwd);
6622 a70480e0 2018-06-23 stsp free(commit_id);
6623 a44927cc 2022-04-07 stsp if (commit)
6624 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6625 eb41ed75 2019-02-05 stsp if (worktree)
6626 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6627 1d0f4054 2021-06-17 stsp if (repo) {
6628 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6629 1d0f4054 2021-06-17 stsp if (error == NULL)
6630 1d0f4054 2021-06-17 stsp error = close_err;
6631 1d0f4054 2021-06-17 stsp }
6632 0ae84acc 2022-06-15 tracey if (pack_fds) {
6633 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6634 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6635 0ae84acc 2022-06-15 tracey if (error == NULL)
6636 0ae84acc 2022-06-15 tracey error = pack_err;
6637 0ae84acc 2022-06-15 tracey }
6638 51a10b52 2020-12-26 stsp tog_free_refs();
6639 a70480e0 2018-06-23 stsp return error;
6640 ffd1d5e5 2018-06-23 stsp }
6641 ffd1d5e5 2018-06-23 stsp
6642 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6643 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6644 ffd1d5e5 2018-06-23 stsp {
6645 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6646 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6647 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6648 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6649 0c6ad1bc 2022-09-09 mark char *index = NULL;
6650 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6651 94b80cfa 2022-08-01 mark int width, n, nentries, i = 1;
6652 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6653 ffd1d5e5 2018-06-23 stsp
6654 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6655 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6656 9b058f45 2022-06-30 mark --limit; /* border */
6657 ffd1d5e5 2018-06-23 stsp
6658 f7d12f7e 2018-08-01 stsp werase(view->window);
6659 ffd1d5e5 2018-06-23 stsp
6660 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6661 ffd1d5e5 2018-06-23 stsp return NULL;
6662 ffd1d5e5 2018-06-23 stsp
6663 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6664 ccda2f4d 2022-06-16 stsp 0, 0);
6665 ffd1d5e5 2018-06-23 stsp if (err)
6666 ffd1d5e5 2018-06-23 stsp return err;
6667 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6668 a3404814 2018-09-02 stsp wstandout(view->window);
6669 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6670 11b20872 2019-11-08 stsp if (tc)
6671 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6672 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6673 0c6ad1bc 2022-09-09 mark free(wline);
6674 0c6ad1bc 2022-09-09 mark wline = NULL;
6675 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
6676 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
6677 11b20872 2019-11-08 stsp if (tc)
6678 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6679 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6680 a3404814 2018-09-02 stsp wstandend(view->window);
6681 0c6ad1bc 2022-09-09 mark if (--limit <= 0)
6682 0c6ad1bc 2022-09-09 mark return NULL;
6683 94b80cfa 2022-08-01 mark
6684 df68a56b 2022-08-12 mark i += s->selected;
6685 df68a56b 2022-08-12 mark if (s->first_displayed_entry) {
6686 df68a56b 2022-08-12 mark i += got_tree_entry_get_index(s->first_displayed_entry);
6687 df68a56b 2022-08-12 mark if (s->tree != s->root)
6688 df68a56b 2022-08-12 mark ++i; /* account for ".." entry */
6689 94b80cfa 2022-08-01 mark }
6690 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
6691 0c6ad1bc 2022-09-09 mark if (asprintf(&index, "[%d/%d] %s",
6692 0c6ad1bc 2022-09-09 mark i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6693 0c6ad1bc 2022-09-09 mark return got_error_from_errno("asprintf");
6694 0c6ad1bc 2022-09-09 mark err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6695 0c6ad1bc 2022-09-09 mark free(index);
6696 ce52c690 2018-06-23 stsp if (err)
6697 ce52c690 2018-06-23 stsp return err;
6698 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6699 2550e4c3 2018-07-13 stsp free(wline);
6700 2550e4c3 2018-07-13 stsp wline = NULL;
6701 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6702 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6703 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6704 ffd1d5e5 2018-06-23 stsp return NULL;
6705 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6706 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6707 a1eca9bb 2018-06-23 stsp return NULL;
6708 ffd1d5e5 2018-06-23 stsp
6709 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6710 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6711 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6712 0cf4efb1 2018-09-29 stsp if (view->focussed)
6713 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6714 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6715 ffd1d5e5 2018-06-23 stsp }
6716 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6717 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6718 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6719 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6720 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6721 ffd1d5e5 2018-06-23 stsp return NULL;
6722 ffd1d5e5 2018-06-23 stsp n = 1;
6723 ffd1d5e5 2018-06-23 stsp } else {
6724 ffd1d5e5 2018-06-23 stsp n = 0;
6725 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6726 ffd1d5e5 2018-06-23 stsp }
6727 ffd1d5e5 2018-06-23 stsp
6728 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6729 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6730 848d6979 2019-08-12 stsp const char *modestr = "";
6731 56e0773d 2019-11-28 stsp mode_t mode;
6732 1d13200f 2018-07-12 stsp
6733 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6734 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6735 56e0773d 2019-11-28 stsp
6736 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6737 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6738 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6739 1d13200f 2018-07-12 stsp if (err)
6740 638f9024 2019-05-13 stsp return got_error_from_errno(
6741 230a42bd 2019-05-11 jcs "got_object_id_str");
6742 1d13200f 2018-07-12 stsp }
6743 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6744 63c5ca5d 2019-08-24 stsp modestr = "$";
6745 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6746 0d6c6ee3 2020-05-20 stsp int i;
6747 0d6c6ee3 2020-05-20 stsp
6748 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6749 d86d3b18 2020-12-01 naddy te, s->repo);
6750 0d6c6ee3 2020-05-20 stsp if (err) {
6751 0d6c6ee3 2020-05-20 stsp free(id_str);
6752 0d6c6ee3 2020-05-20 stsp return err;
6753 0d6c6ee3 2020-05-20 stsp }
6754 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6755 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6756 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6757 0d6c6ee3 2020-05-20 stsp }
6758 848d6979 2019-08-12 stsp modestr = "@";
6759 0d6c6ee3 2020-05-20 stsp }
6760 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6761 848d6979 2019-08-12 stsp modestr = "/";
6762 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6763 848d6979 2019-08-12 stsp modestr = "*";
6764 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6765 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6766 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6767 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6768 1d13200f 2018-07-12 stsp free(id_str);
6769 0d6c6ee3 2020-05-20 stsp free(link_target);
6770 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6771 1d13200f 2018-07-12 stsp }
6772 1d13200f 2018-07-12 stsp free(id_str);
6773 0d6c6ee3 2020-05-20 stsp free(link_target);
6774 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6775 ccda2f4d 2022-06-16 stsp 0, 0);
6776 ffd1d5e5 2018-06-23 stsp if (err) {
6777 ffd1d5e5 2018-06-23 stsp free(line);
6778 ffd1d5e5 2018-06-23 stsp break;
6779 ffd1d5e5 2018-06-23 stsp }
6780 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6781 0cf4efb1 2018-09-29 stsp if (view->focussed)
6782 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6783 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6784 ffd1d5e5 2018-06-23 stsp }
6785 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6786 f26dddb7 2019-11-08 stsp if (tc)
6787 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6788 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6789 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6790 f26dddb7 2019-11-08 stsp if (tc)
6791 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6792 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6793 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6794 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6795 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6796 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6797 ffd1d5e5 2018-06-23 stsp free(line);
6798 2550e4c3 2018-07-13 stsp free(wline);
6799 2550e4c3 2018-07-13 stsp wline = NULL;
6800 ffd1d5e5 2018-06-23 stsp n++;
6801 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6802 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6803 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6804 ffd1d5e5 2018-06-23 stsp break;
6805 ffd1d5e5 2018-06-23 stsp }
6806 ffd1d5e5 2018-06-23 stsp
6807 ffd1d5e5 2018-06-23 stsp return err;
6808 ffd1d5e5 2018-06-23 stsp }
6809 ffd1d5e5 2018-06-23 stsp
6810 ffd1d5e5 2018-06-23 stsp static void
6811 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6812 ffd1d5e5 2018-06-23 stsp {
6813 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6814 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6815 fa86c4bf 2020-11-29 stsp int i = 0;
6816 ffd1d5e5 2018-06-23 stsp
6817 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6818 ffd1d5e5 2018-06-23 stsp return;
6819 ffd1d5e5 2018-06-23 stsp
6820 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6821 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6822 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6823 fa86c4bf 2020-11-29 stsp if (!isroot)
6824 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6825 fa86c4bf 2020-11-29 stsp break;
6826 fa86c4bf 2020-11-29 stsp }
6827 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6828 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6829 ffd1d5e5 2018-06-23 stsp }
6830 ffd1d5e5 2018-06-23 stsp }
6831 ffd1d5e5 2018-06-23 stsp
6832 9b058f45 2022-06-30 mark static const struct got_error *
6833 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6834 ffd1d5e5 2018-06-23 stsp {
6835 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6836 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6837 ffd1d5e5 2018-06-23 stsp int n = 0;
6838 ffd1d5e5 2018-06-23 stsp
6839 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6840 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6841 694d3271 2020-12-01 naddy s->first_displayed_entry);
6842 694d3271 2020-12-01 naddy else
6843 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6844 56e0773d 2019-11-28 stsp
6845 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6846 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6847 94b80cfa 2022-08-01 mark if (last) {
6848 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
6849 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6850 94b80cfa 2022-08-01 mark }
6851 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6852 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6853 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6854 768394f3 2019-01-24 stsp }
6855 ffd1d5e5 2018-06-23 stsp }
6856 9b058f45 2022-06-30 mark
6857 9b058f45 2022-06-30 mark return NULL;
6858 ffd1d5e5 2018-06-23 stsp }
6859 ffd1d5e5 2018-06-23 stsp
6860 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6861 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6862 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6863 ffd1d5e5 2018-06-23 stsp {
6864 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6865 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6866 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6867 ffd1d5e5 2018-06-23 stsp
6868 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6869 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6870 56e0773d 2019-11-28 stsp + 1 /* slash */;
6871 ce52c690 2018-06-23 stsp if (te)
6872 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6873 ce52c690 2018-06-23 stsp
6874 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6875 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6876 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6877 ffd1d5e5 2018-06-23 stsp
6878 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6879 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6880 d9765a41 2018-06-23 stsp while (pt) {
6881 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6882 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6883 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6884 cb2ebc8a 2018-06-23 stsp goto done;
6885 cb2ebc8a 2018-06-23 stsp }
6886 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6887 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6888 cb2ebc8a 2018-06-23 stsp goto done;
6889 cb2ebc8a 2018-06-23 stsp }
6890 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6891 ffd1d5e5 2018-06-23 stsp }
6892 ce52c690 2018-06-23 stsp if (te) {
6893 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6894 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6895 ce52c690 2018-06-23 stsp goto done;
6896 ce52c690 2018-06-23 stsp }
6897 cb2ebc8a 2018-06-23 stsp }
6898 ce52c690 2018-06-23 stsp done:
6899 ce52c690 2018-06-23 stsp if (err) {
6900 ce52c690 2018-06-23 stsp free(*path);
6901 ce52c690 2018-06-23 stsp *path = NULL;
6902 ce52c690 2018-06-23 stsp }
6903 ce52c690 2018-06-23 stsp return err;
6904 ce52c690 2018-06-23 stsp }
6905 ce52c690 2018-06-23 stsp
6906 ce52c690 2018-06-23 stsp static const struct got_error *
6907 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6908 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6909 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6910 ce52c690 2018-06-23 stsp {
6911 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6912 ce52c690 2018-06-23 stsp char *path;
6913 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6914 a0de39f3 2019-08-09 stsp
6915 a0de39f3 2019-08-09 stsp *new_view = NULL;
6916 69efd4c4 2018-07-18 stsp
6917 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6918 ce52c690 2018-06-23 stsp if (err)
6919 ce52c690 2018-06-23 stsp return err;
6920 ffd1d5e5 2018-06-23 stsp
6921 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6922 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6923 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6924 83ce39e3 2019-08-12 stsp goto done;
6925 83ce39e3 2019-08-12 stsp }
6926 cdf1ee82 2018-08-01 stsp
6927 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6928 e5a0f69f 2018-08-18 stsp if (err) {
6929 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6930 fc06ba56 2019-08-22 stsp err = NULL;
6931 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6932 e5a0f69f 2018-08-18 stsp } else
6933 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6934 83ce39e3 2019-08-12 stsp done:
6935 83ce39e3 2019-08-12 stsp free(path);
6936 69efd4c4 2018-07-18 stsp return err;
6937 69efd4c4 2018-07-18 stsp }
6938 69efd4c4 2018-07-18 stsp
6939 69efd4c4 2018-07-18 stsp static const struct got_error *
6940 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6941 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6942 69efd4c4 2018-07-18 stsp {
6943 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6944 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6945 69efd4c4 2018-07-18 stsp char *path;
6946 a0de39f3 2019-08-09 stsp
6947 a0de39f3 2019-08-09 stsp *new_view = NULL;
6948 69efd4c4 2018-07-18 stsp
6949 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6950 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6951 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6952 e5a0f69f 2018-08-18 stsp
6953 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6954 69efd4c4 2018-07-18 stsp if (err)
6955 69efd4c4 2018-07-18 stsp return err;
6956 69efd4c4 2018-07-18 stsp
6957 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6958 4e97c21c 2020-12-06 stsp path, 0);
6959 ba4f502b 2018-08-04 stsp if (err)
6960 e5a0f69f 2018-08-18 stsp view_close(log_view);
6961 e5a0f69f 2018-08-18 stsp else
6962 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6963 cb2ebc8a 2018-06-23 stsp free(path);
6964 cb2ebc8a 2018-06-23 stsp return err;
6965 ffd1d5e5 2018-06-23 stsp }
6966 ffd1d5e5 2018-06-23 stsp
6967 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6968 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6969 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6970 ffd1d5e5 2018-06-23 stsp {
6971 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6972 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6973 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6974 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6975 ffd1d5e5 2018-06-23 stsp
6976 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6977 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6978 bc573f3b 2021-07-10 stsp
6979 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6980 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6981 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6982 ffd1d5e5 2018-06-23 stsp
6983 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6984 bc573f3b 2021-07-10 stsp if (err)
6985 bc573f3b 2021-07-10 stsp goto done;
6986 bc573f3b 2021-07-10 stsp
6987 bc573f3b 2021-07-10 stsp /*
6988 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6989 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6990 bc573f3b 2021-07-10 stsp * closed on demand.
6991 bc573f3b 2021-07-10 stsp */
6992 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6993 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6994 bc573f3b 2021-07-10 stsp if (err)
6995 bc573f3b 2021-07-10 stsp goto done;
6996 bc573f3b 2021-07-10 stsp s->tree = s->root;
6997 bc573f3b 2021-07-10 stsp
6998 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6999 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7000 ffd1d5e5 2018-06-23 stsp goto done;
7001 ffd1d5e5 2018-06-23 stsp
7002 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7003 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7004 ffd1d5e5 2018-06-23 stsp goto done;
7005 ffd1d5e5 2018-06-23 stsp }
7006 ffd1d5e5 2018-06-23 stsp
7007 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7008 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7009 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7010 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7011 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7012 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7013 9cd7cbd1 2020-12-07 stsp goto done;
7014 9cd7cbd1 2020-12-07 stsp }
7015 9cd7cbd1 2020-12-07 stsp }
7016 fb2756b9 2018-08-04 stsp s->repo = repo;
7017 c0b01bdb 2019-11-08 stsp
7018 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7019 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7020 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7021 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7022 c0b01bdb 2019-11-08 stsp if (err)
7023 c0b01bdb 2019-11-08 stsp goto done;
7024 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7025 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7026 bc573f3b 2021-07-10 stsp if (err)
7027 c0b01bdb 2019-11-08 stsp goto done;
7028 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7029 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7030 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7031 bc573f3b 2021-07-10 stsp if (err)
7032 c0b01bdb 2019-11-08 stsp goto done;
7033 e5a0f69f 2018-08-18 stsp
7034 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7035 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7036 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7037 bc573f3b 2021-07-10 stsp if (err)
7038 11b20872 2019-11-08 stsp goto done;
7039 11b20872 2019-11-08 stsp
7040 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7041 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7042 bc573f3b 2021-07-10 stsp if (err)
7043 c0b01bdb 2019-11-08 stsp goto done;
7044 c0b01bdb 2019-11-08 stsp }
7045 c0b01bdb 2019-11-08 stsp
7046 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7047 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7048 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7049 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7050 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7051 ad80ab7b 2018-08-04 stsp done:
7052 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7053 bc573f3b 2021-07-10 stsp if (commit)
7054 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7055 bc573f3b 2021-07-10 stsp if (err)
7056 bc573f3b 2021-07-10 stsp close_tree_view(view);
7057 ad80ab7b 2018-08-04 stsp return err;
7058 ad80ab7b 2018-08-04 stsp }
7059 ad80ab7b 2018-08-04 stsp
7060 e5a0f69f 2018-08-18 stsp static const struct got_error *
7061 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7062 ad80ab7b 2018-08-04 stsp {
7063 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7064 ad80ab7b 2018-08-04 stsp
7065 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7066 fb2756b9 2018-08-04 stsp free(s->tree_label);
7067 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7068 6484ec90 2018-09-29 stsp free(s->commit_id);
7069 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7070 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7071 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7072 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7073 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7074 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7075 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7076 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7077 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7078 ad80ab7b 2018-08-04 stsp free(parent);
7079 ad80ab7b 2018-08-04 stsp
7080 ad80ab7b 2018-08-04 stsp }
7081 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7082 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7083 bc573f3b 2021-07-10 stsp if (s->root)
7084 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7085 7c32bd05 2019-06-22 stsp return NULL;
7086 7c32bd05 2019-06-22 stsp }
7087 7c32bd05 2019-06-22 stsp
7088 7c32bd05 2019-06-22 stsp static const struct got_error *
7089 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7090 7c32bd05 2019-06-22 stsp {
7091 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7092 7c32bd05 2019-06-22 stsp
7093 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7094 7c32bd05 2019-06-22 stsp return NULL;
7095 7c32bd05 2019-06-22 stsp }
7096 7c32bd05 2019-06-22 stsp
7097 7c32bd05 2019-06-22 stsp static int
7098 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7099 7c32bd05 2019-06-22 stsp {
7100 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7101 7c32bd05 2019-06-22 stsp
7102 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7103 56e0773d 2019-11-28 stsp 0) == 0;
7104 7c32bd05 2019-06-22 stsp }
7105 7c32bd05 2019-06-22 stsp
7106 7c32bd05 2019-06-22 stsp static const struct got_error *
7107 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7108 7c32bd05 2019-06-22 stsp {
7109 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7110 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7111 7c32bd05 2019-06-22 stsp
7112 7c32bd05 2019-06-22 stsp if (!view->searching) {
7113 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7114 7c32bd05 2019-06-22 stsp return NULL;
7115 7c32bd05 2019-06-22 stsp }
7116 7c32bd05 2019-06-22 stsp
7117 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7118 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7119 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7120 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7121 56e0773d 2019-11-28 stsp s->selected_entry);
7122 7c32bd05 2019-06-22 stsp else
7123 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7124 56e0773d 2019-11-28 stsp } else {
7125 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7126 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7127 56e0773d 2019-11-28 stsp else
7128 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7129 56e0773d 2019-11-28 stsp s->selected_entry);
7130 7c32bd05 2019-06-22 stsp }
7131 7c32bd05 2019-06-22 stsp } else {
7132 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7133 487cd7d2 2021-12-17 stsp te = s->selected_entry;
7134 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7135 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7136 56e0773d 2019-11-28 stsp else
7137 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7138 7c32bd05 2019-06-22 stsp }
7139 7c32bd05 2019-06-22 stsp
7140 7c32bd05 2019-06-22 stsp while (1) {
7141 56e0773d 2019-11-28 stsp if (te == NULL) {
7142 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7143 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7144 ac66afb8 2019-06-24 stsp return NULL;
7145 ac66afb8 2019-06-24 stsp }
7146 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7147 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7148 56e0773d 2019-11-28 stsp else
7149 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7150 7c32bd05 2019-06-22 stsp }
7151 7c32bd05 2019-06-22 stsp
7152 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7153 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7154 56e0773d 2019-11-28 stsp s->matched_entry = te;
7155 7c32bd05 2019-06-22 stsp break;
7156 7c32bd05 2019-06-22 stsp }
7157 7c32bd05 2019-06-22 stsp
7158 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7159 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7160 56e0773d 2019-11-28 stsp else
7161 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7162 7c32bd05 2019-06-22 stsp }
7163 e5a0f69f 2018-08-18 stsp
7164 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7165 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7166 7c32bd05 2019-06-22 stsp s->selected = 0;
7167 7c32bd05 2019-06-22 stsp }
7168 7c32bd05 2019-06-22 stsp
7169 e5a0f69f 2018-08-18 stsp return NULL;
7170 ad80ab7b 2018-08-04 stsp }
7171 ad80ab7b 2018-08-04 stsp
7172 ad80ab7b 2018-08-04 stsp static const struct got_error *
7173 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7174 ad80ab7b 2018-08-04 stsp {
7175 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7176 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7177 e5a0f69f 2018-08-18 stsp char *parent_path;
7178 ad80ab7b 2018-08-04 stsp
7179 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7180 e5a0f69f 2018-08-18 stsp if (err)
7181 e5a0f69f 2018-08-18 stsp return err;
7182 ffd1d5e5 2018-06-23 stsp
7183 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7184 e5a0f69f 2018-08-18 stsp free(parent_path);
7185 669b5ffa 2018-10-07 stsp
7186 9b058f45 2022-06-30 mark view_border(view);
7187 e5a0f69f 2018-08-18 stsp return err;
7188 94b80cfa 2022-08-01 mark }
7189 94b80cfa 2022-08-01 mark
7190 94b80cfa 2022-08-01 mark static const struct got_error *
7191 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
7192 94b80cfa 2022-08-01 mark {
7193 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7194 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
7195 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
7196 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
7197 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
7198 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
7199 94b80cfa 2022-08-01 mark
7200 94b80cfa 2022-08-01 mark g = view->gline;
7201 94b80cfa 2022-08-01 mark view->gline = 0;
7202 94b80cfa 2022-08-01 mark
7203 94b80cfa 2022-08-01 mark if (g == 0)
7204 94b80cfa 2022-08-01 mark g = 1;
7205 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
7206 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7207 94b80cfa 2022-08-01 mark
7208 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
7209 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
7210 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
7211 94b80cfa 2022-08-01 mark
7212 94b80cfa 2022-08-01 mark if (*fte != NULL) {
7213 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7214 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
7215 94b80cfa 2022-08-01 mark }
7216 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
7217 94b80cfa 2022-08-01 mark last += off;
7218 94b80cfa 2022-08-01 mark
7219 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
7220 94b80cfa 2022-08-01 mark s->selected = g - first;
7221 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
7222 94b80cfa 2022-08-01 mark }
7223 94b80cfa 2022-08-01 mark
7224 94b80cfa 2022-08-01 mark if (*ste != NULL) {
7225 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
7226 94b80cfa 2022-08-01 mark i += off;
7227 94b80cfa 2022-08-01 mark }
7228 94b80cfa 2022-08-01 mark
7229 94b80cfa 2022-08-01 mark if (i < g) {
7230 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
7231 94b80cfa 2022-08-01 mark if (err)
7232 94b80cfa 2022-08-01 mark return err;
7233 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
7234 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
7235 94b80cfa 2022-08-01 mark first + s->selected < g &&
7236 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
7237 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7238 94b80cfa 2022-08-01 mark first += off;
7239 94b80cfa 2022-08-01 mark s->selected = g - first;
7240 94b80cfa 2022-08-01 mark }
7241 94b80cfa 2022-08-01 mark } else if (i > g)
7242 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
7243 94b80cfa 2022-08-01 mark
7244 94b80cfa 2022-08-01 mark if (g < nlines &&
7245 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7246 94b80cfa 2022-08-01 mark s->selected = g - 1;
7247 94b80cfa 2022-08-01 mark
7248 94b80cfa 2022-08-01 mark return NULL;
7249 e5a0f69f 2018-08-18 stsp }
7250 ce52c690 2018-06-23 stsp
7251 e5a0f69f 2018-08-18 stsp static const struct got_error *
7252 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7253 e5a0f69f 2018-08-18 stsp {
7254 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7255 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7256 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7257 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
7258 ffd1d5e5 2018-06-23 stsp
7259 94b80cfa 2022-08-01 mark if (view->gline)
7260 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
7261 94b80cfa 2022-08-01 mark
7262 e5a0f69f 2018-08-18 stsp switch (ch) {
7263 1e37a5c2 2019-05-12 jcs case 'i':
7264 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7265 640cd7ff 2022-06-22 mark view->count = 0;
7266 1e37a5c2 2019-05-12 jcs break;
7267 5e98fb33 2022-07-22 mark case 'L':
7268 640cd7ff 2022-06-22 mark view->count = 0;
7269 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7270 ffd1d5e5 2018-06-23 stsp break;
7271 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7272 152c1c93 2020-11-29 stsp break;
7273 5e98fb33 2022-07-22 mark case 'R':
7274 640cd7ff 2022-06-22 mark view->count = 0;
7275 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
7276 e4526bf5 2021-09-03 naddy break;
7277 e4526bf5 2021-09-03 naddy case 'g':
7278 e4526bf5 2021-09-03 naddy case KEY_HOME:
7279 e4526bf5 2021-09-03 naddy s->selected = 0;
7280 640cd7ff 2022-06-22 mark view->count = 0;
7281 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7282 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7283 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7284 e4526bf5 2021-09-03 naddy else
7285 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7286 1e37a5c2 2019-05-12 jcs break;
7287 e4526bf5 2021-09-03 naddy case 'G':
7288 9b058f45 2022-06-30 mark case KEY_END: {
7289 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
7290 9b058f45 2022-06-30 mark
7291 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7292 9b058f45 2022-06-30 mark --eos; /* border */
7293 e4526bf5 2021-09-03 naddy s->selected = 0;
7294 640cd7ff 2022-06-22 mark view->count = 0;
7295 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7296 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7297 e4526bf5 2021-09-03 naddy if (te == NULL) {
7298 ad055527 2022-07-16 mark if (s->tree != s->root) {
7299 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7300 e4526bf5 2021-09-03 naddy n++;
7301 e4526bf5 2021-09-03 naddy }
7302 e4526bf5 2021-09-03 naddy break;
7303 e4526bf5 2021-09-03 naddy }
7304 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7305 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7306 e4526bf5 2021-09-03 naddy }
7307 e4526bf5 2021-09-03 naddy if (n > 0)
7308 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7309 e4526bf5 2021-09-03 naddy break;
7310 9b058f45 2022-06-30 mark }
7311 1e37a5c2 2019-05-12 jcs case 'k':
7312 1e37a5c2 2019-05-12 jcs case KEY_UP:
7313 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7314 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7315 1e37a5c2 2019-05-12 jcs s->selected--;
7316 fa86c4bf 2020-11-29 stsp break;
7317 1e37a5c2 2019-05-12 jcs }
7318 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7319 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7320 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7321 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7322 640cd7ff 2022-06-22 mark view->count = 0;
7323 1e37a5c2 2019-05-12 jcs break;
7324 83cc4199 2022-06-13 stsp case CTRL('u'):
7325 33c3719a 2022-06-15 stsp case 'u':
7326 83cc4199 2022-06-13 stsp nscroll /= 2;
7327 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7328 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7329 ea025d1d 2020-02-22 naddy case CTRL('b'):
7330 61417565 2022-06-20 mark case 'b':
7331 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7332 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7333 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7334 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7335 fa86c4bf 2020-11-29 stsp } else {
7336 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7337 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7338 fa86c4bf 2020-11-29 stsp }
7339 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7340 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7341 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7342 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7343 640cd7ff 2022-06-22 mark view->count = 0;
7344 1e37a5c2 2019-05-12 jcs break;
7345 1e37a5c2 2019-05-12 jcs case 'j':
7346 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7347 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7348 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7349 1e37a5c2 2019-05-12 jcs s->selected++;
7350 1e37a5c2 2019-05-12 jcs break;
7351 1e37a5c2 2019-05-12 jcs }
7352 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7353 640cd7ff 2022-06-22 mark == NULL) {
7354 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7355 640cd7ff 2022-06-22 mark view->count = 0;
7356 1e37a5c2 2019-05-12 jcs break;
7357 640cd7ff 2022-06-22 mark }
7358 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7359 1e37a5c2 2019-05-12 jcs break;
7360 83cc4199 2022-06-13 stsp case CTRL('d'):
7361 33c3719a 2022-06-15 stsp case 'd':
7362 83cc4199 2022-06-13 stsp nscroll /= 2;
7363 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7364 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7365 ea025d1d 2020-02-22 naddy case CTRL('f'):
7366 61417565 2022-06-20 mark case 'f':
7367 48bb96f0 2022-06-20 naddy case ' ':
7368 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7369 1e37a5c2 2019-05-12 jcs == NULL) {
7370 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7371 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7372 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7373 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7374 640cd7ff 2022-06-22 mark else
7375 640cd7ff 2022-06-22 mark view->count = 0;
7376 1e37a5c2 2019-05-12 jcs break;
7377 1e37a5c2 2019-05-12 jcs }
7378 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7379 1e37a5c2 2019-05-12 jcs break;
7380 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7381 1e37a5c2 2019-05-12 jcs case '\r':
7382 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7383 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7384 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7385 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7386 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7387 640cd7ff 2022-06-22 mark view->count = 0;
7388 1e37a5c2 2019-05-12 jcs break;
7389 640cd7ff 2022-06-22 mark }
7390 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7391 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7392 1e37a5c2 2019-05-12 jcs entry);
7393 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7394 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7395 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7396 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7397 1e37a5c2 2019-05-12 jcs s->selected_entry =
7398 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7399 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7400 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7401 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7402 9b058f45 2022-06-30 mark if (err)
7403 9b058f45 2022-06-30 mark break;
7404 9b058f45 2022-06-30 mark }
7405 1e37a5c2 2019-05-12 jcs free(parent);
7406 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7407 56e0773d 2019-11-28 stsp s->selected_entry))) {
7408 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7409 640cd7ff 2022-06-22 mark view->count = 0;
7410 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7411 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7412 1e37a5c2 2019-05-12 jcs if (err)
7413 1e37a5c2 2019-05-12 jcs break;
7414 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7415 941e9f74 2019-05-21 stsp if (err) {
7416 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7417 1e37a5c2 2019-05-12 jcs break;
7418 1e37a5c2 2019-05-12 jcs }
7419 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7420 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7421 1e37a5c2 2019-05-12 jcs break;
7422 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7423 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7424 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7425 640cd7ff 2022-06-22 mark view->count = 0;
7426 1e37a5c2 2019-05-12 jcs break;
7427 1e37a5c2 2019-05-12 jcs default:
7428 640cd7ff 2022-06-22 mark view->count = 0;
7429 1e37a5c2 2019-05-12 jcs break;
7430 ffd1d5e5 2018-06-23 stsp }
7431 e5a0f69f 2018-08-18 stsp
7432 ffd1d5e5 2018-06-23 stsp return err;
7433 9f7d7167 2018-04-29 stsp }
7434 9f7d7167 2018-04-29 stsp
7435 ffd1d5e5 2018-06-23 stsp __dead static void
7436 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7437 ffd1d5e5 2018-06-23 stsp {
7438 ffd1d5e5 2018-06-23 stsp endwin();
7439 87411fa9 2022-06-16 stsp fprintf(stderr,
7440 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7441 ffd1d5e5 2018-06-23 stsp getprogname());
7442 ffd1d5e5 2018-06-23 stsp exit(1);
7443 ffd1d5e5 2018-06-23 stsp }
7444 ffd1d5e5 2018-06-23 stsp
7445 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7446 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7447 ffd1d5e5 2018-06-23 stsp {
7448 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7449 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7450 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7451 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7452 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7453 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7454 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7455 4e97c21c 2020-12-06 stsp char *label = NULL;
7456 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7457 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7458 ffd1d5e5 2018-06-23 stsp int ch;
7459 5221c383 2018-08-01 stsp struct tog_view *view;
7460 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7461 70ac5f84 2019-03-28 stsp
7462 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7463 ffd1d5e5 2018-06-23 stsp switch (ch) {
7464 ffd1d5e5 2018-06-23 stsp case 'c':
7465 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7466 74283ab8 2020-02-07 stsp break;
7467 74283ab8 2020-02-07 stsp case 'r':
7468 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7469 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7470 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7471 74283ab8 2020-02-07 stsp optarg);
7472 ffd1d5e5 2018-06-23 stsp break;
7473 ffd1d5e5 2018-06-23 stsp default:
7474 e99e2d15 2020-11-24 naddy usage_tree();
7475 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7476 ffd1d5e5 2018-06-23 stsp }
7477 ffd1d5e5 2018-06-23 stsp }
7478 ffd1d5e5 2018-06-23 stsp
7479 ffd1d5e5 2018-06-23 stsp argc -= optind;
7480 ffd1d5e5 2018-06-23 stsp argv += optind;
7481 ffd1d5e5 2018-06-23 stsp
7482 55cccc34 2020-02-20 stsp if (argc > 1)
7483 e99e2d15 2020-11-24 naddy usage_tree();
7484 0ae84acc 2022-06-15 tracey
7485 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7486 0ae84acc 2022-06-15 tracey if (error != NULL)
7487 0ae84acc 2022-06-15 tracey goto done;
7488 74283ab8 2020-02-07 stsp
7489 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7490 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7491 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7492 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7493 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7494 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7495 c156c7a4 2020-12-18 stsp goto done;
7496 55cccc34 2020-02-20 stsp if (worktree)
7497 52185f70 2019-02-05 stsp repo_path =
7498 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7499 55cccc34 2020-02-20 stsp else
7500 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7501 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7502 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7503 c156c7a4 2020-12-18 stsp goto done;
7504 c156c7a4 2020-12-18 stsp }
7505 55cccc34 2020-02-20 stsp }
7506 a915003a 2019-02-05 stsp
7507 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7508 c02c541e 2019-03-29 stsp if (error != NULL)
7509 52185f70 2019-02-05 stsp goto done;
7510 d188b9a6 2019-01-04 stsp
7511 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7512 55cccc34 2020-02-20 stsp repo, worktree);
7513 55cccc34 2020-02-20 stsp if (error)
7514 55cccc34 2020-02-20 stsp goto done;
7515 55cccc34 2020-02-20 stsp
7516 55cccc34 2020-02-20 stsp init_curses();
7517 55cccc34 2020-02-20 stsp
7518 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7519 c02c541e 2019-03-29 stsp if (error)
7520 52185f70 2019-02-05 stsp goto done;
7521 ffd1d5e5 2018-06-23 stsp
7522 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7523 51a10b52 2020-12-26 stsp if (error)
7524 51a10b52 2020-12-26 stsp goto done;
7525 51a10b52 2020-12-26 stsp
7526 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7527 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7528 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7529 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7530 4e97c21c 2020-12-06 stsp if (error)
7531 4e97c21c 2020-12-06 stsp goto done;
7532 4e97c21c 2020-12-06 stsp head_ref_name = label;
7533 4e97c21c 2020-12-06 stsp } else {
7534 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7535 4e97c21c 2020-12-06 stsp if (error == NULL)
7536 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7537 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7538 4e97c21c 2020-12-06 stsp goto done;
7539 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7540 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7541 4e97c21c 2020-12-06 stsp if (error)
7542 4e97c21c 2020-12-06 stsp goto done;
7543 4e97c21c 2020-12-06 stsp }
7544 ffd1d5e5 2018-06-23 stsp
7545 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7546 a44927cc 2022-04-07 stsp if (error)
7547 a44927cc 2022-04-07 stsp goto done;
7548 a44927cc 2022-04-07 stsp
7549 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7550 5221c383 2018-08-01 stsp if (view == NULL) {
7551 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7552 5221c383 2018-08-01 stsp goto done;
7553 5221c383 2018-08-01 stsp }
7554 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7555 ad80ab7b 2018-08-04 stsp if (error)
7556 ad80ab7b 2018-08-04 stsp goto done;
7557 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7558 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7559 d91faf3b 2020-12-01 naddy in_repo_path);
7560 55cccc34 2020-02-20 stsp if (error)
7561 55cccc34 2020-02-20 stsp goto done;
7562 55cccc34 2020-02-20 stsp }
7563 55cccc34 2020-02-20 stsp
7564 55cccc34 2020-02-20 stsp if (worktree) {
7565 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7566 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7567 55cccc34 2020-02-20 stsp worktree = NULL;
7568 55cccc34 2020-02-20 stsp }
7569 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7570 ffd1d5e5 2018-06-23 stsp done:
7571 52185f70 2019-02-05 stsp free(repo_path);
7572 e4a0e26d 2020-02-20 stsp free(cwd);
7573 ffd1d5e5 2018-06-23 stsp free(commit_id);
7574 4e97c21c 2020-12-06 stsp free(label);
7575 486cd271 2020-12-06 stsp if (ref)
7576 486cd271 2020-12-06 stsp got_ref_close(ref);
7577 1d0f4054 2021-06-17 stsp if (repo) {
7578 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7579 1d0f4054 2021-06-17 stsp if (error == NULL)
7580 1d0f4054 2021-06-17 stsp error = close_err;
7581 1d0f4054 2021-06-17 stsp }
7582 0ae84acc 2022-06-15 tracey if (pack_fds) {
7583 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7584 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7585 0ae84acc 2022-06-15 tracey if (error == NULL)
7586 0ae84acc 2022-06-15 tracey error = pack_err;
7587 0ae84acc 2022-06-15 tracey }
7588 51a10b52 2020-12-26 stsp tog_free_refs();
7589 ffd1d5e5 2018-06-23 stsp return error;
7590 6458efa5 2020-11-24 stsp }
7591 6458efa5 2020-11-24 stsp
7592 6458efa5 2020-11-24 stsp static const struct got_error *
7593 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7594 6458efa5 2020-11-24 stsp {
7595 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7596 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7597 6458efa5 2020-11-24 stsp
7598 6458efa5 2020-11-24 stsp s->nrefs = 0;
7599 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7600 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7601 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7602 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7603 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7604 6458efa5 2020-11-24 stsp continue;
7605 6458efa5 2020-11-24 stsp
7606 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7607 6458efa5 2020-11-24 stsp if (re == NULL)
7608 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7609 6458efa5 2020-11-24 stsp
7610 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7611 8924d611 2020-12-26 stsp if (re->ref == NULL)
7612 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7613 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7614 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7615 6458efa5 2020-11-24 stsp }
7616 6458efa5 2020-11-24 stsp
7617 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7618 6458efa5 2020-11-24 stsp return NULL;
7619 6458efa5 2020-11-24 stsp }
7620 6458efa5 2020-11-24 stsp
7621 336075a4 2022-06-25 op static void
7622 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7623 6458efa5 2020-11-24 stsp {
7624 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7625 6458efa5 2020-11-24 stsp
7626 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7627 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7628 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7629 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7630 6458efa5 2020-11-24 stsp free(re);
7631 6458efa5 2020-11-24 stsp }
7632 6458efa5 2020-11-24 stsp }
7633 6458efa5 2020-11-24 stsp
7634 6458efa5 2020-11-24 stsp static const struct got_error *
7635 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7636 6458efa5 2020-11-24 stsp {
7637 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7638 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7639 6458efa5 2020-11-24 stsp
7640 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7641 6458efa5 2020-11-24 stsp s->repo = repo;
7642 6458efa5 2020-11-24 stsp
7643 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7644 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7645 6458efa5 2020-11-24 stsp
7646 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7647 6458efa5 2020-11-24 stsp if (err)
7648 6458efa5 2020-11-24 stsp return err;
7649 34ba6917 2020-11-30 stsp
7650 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7651 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7652 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7653 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7654 6458efa5 2020-11-24 stsp if (err)
7655 6458efa5 2020-11-24 stsp goto done;
7656 6458efa5 2020-11-24 stsp
7657 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7658 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7659 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7660 6458efa5 2020-11-24 stsp if (err)
7661 6458efa5 2020-11-24 stsp goto done;
7662 6458efa5 2020-11-24 stsp
7663 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7664 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7665 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7666 cc488aa7 2022-01-23 stsp if (err)
7667 cc488aa7 2022-01-23 stsp goto done;
7668 cc488aa7 2022-01-23 stsp
7669 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7670 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7671 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7672 6458efa5 2020-11-24 stsp if (err)
7673 6458efa5 2020-11-24 stsp goto done;
7674 6458efa5 2020-11-24 stsp }
7675 6458efa5 2020-11-24 stsp
7676 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7677 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7678 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7679 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7680 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7681 6458efa5 2020-11-24 stsp done:
7682 6458efa5 2020-11-24 stsp if (err)
7683 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7684 6458efa5 2020-11-24 stsp return err;
7685 6458efa5 2020-11-24 stsp }
7686 6458efa5 2020-11-24 stsp
7687 6458efa5 2020-11-24 stsp static const struct got_error *
7688 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7689 6458efa5 2020-11-24 stsp {
7690 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7691 6458efa5 2020-11-24 stsp
7692 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7693 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7694 6458efa5 2020-11-24 stsp
7695 6458efa5 2020-11-24 stsp return NULL;
7696 9f7d7167 2018-04-29 stsp }
7697 ce5b7c56 2019-07-09 stsp
7698 6458efa5 2020-11-24 stsp static const struct got_error *
7699 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7700 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7701 6458efa5 2020-11-24 stsp {
7702 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7703 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7704 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7705 6458efa5 2020-11-24 stsp int obj_type;
7706 6458efa5 2020-11-24 stsp
7707 c42c9805 2020-11-24 stsp *commit_id = NULL;
7708 6458efa5 2020-11-24 stsp
7709 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7710 6458efa5 2020-11-24 stsp if (err)
7711 6458efa5 2020-11-24 stsp return err;
7712 6458efa5 2020-11-24 stsp
7713 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7714 6458efa5 2020-11-24 stsp if (err)
7715 6458efa5 2020-11-24 stsp goto done;
7716 6458efa5 2020-11-24 stsp
7717 6458efa5 2020-11-24 stsp switch (obj_type) {
7718 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7719 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7720 6458efa5 2020-11-24 stsp break;
7721 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7722 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7723 6458efa5 2020-11-24 stsp if (err)
7724 6458efa5 2020-11-24 stsp goto done;
7725 c42c9805 2020-11-24 stsp free(obj_id);
7726 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7727 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7728 c42c9805 2020-11-24 stsp if (err)
7729 6458efa5 2020-11-24 stsp goto done;
7730 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7731 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7732 c42c9805 2020-11-24 stsp goto done;
7733 c42c9805 2020-11-24 stsp }
7734 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7735 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7736 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7737 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7738 c42c9805 2020-11-24 stsp goto done;
7739 c42c9805 2020-11-24 stsp }
7740 6458efa5 2020-11-24 stsp break;
7741 6458efa5 2020-11-24 stsp default:
7742 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7743 c42c9805 2020-11-24 stsp break;
7744 6458efa5 2020-11-24 stsp }
7745 6458efa5 2020-11-24 stsp
7746 c42c9805 2020-11-24 stsp done:
7747 c42c9805 2020-11-24 stsp if (tag)
7748 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7749 c42c9805 2020-11-24 stsp if (err) {
7750 c42c9805 2020-11-24 stsp free(*commit_id);
7751 c42c9805 2020-11-24 stsp *commit_id = NULL;
7752 c42c9805 2020-11-24 stsp }
7753 c42c9805 2020-11-24 stsp return err;
7754 c42c9805 2020-11-24 stsp }
7755 c42c9805 2020-11-24 stsp
7756 c42c9805 2020-11-24 stsp static const struct got_error *
7757 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7758 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7759 c42c9805 2020-11-24 stsp {
7760 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7761 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7762 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7763 c42c9805 2020-11-24 stsp
7764 c42c9805 2020-11-24 stsp *new_view = NULL;
7765 c42c9805 2020-11-24 stsp
7766 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7767 c42c9805 2020-11-24 stsp if (err) {
7768 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7769 c42c9805 2020-11-24 stsp return err;
7770 c42c9805 2020-11-24 stsp else
7771 c42c9805 2020-11-24 stsp return NULL;
7772 c42c9805 2020-11-24 stsp }
7773 c42c9805 2020-11-24 stsp
7774 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7775 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7776 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7777 6458efa5 2020-11-24 stsp goto done;
7778 6458efa5 2020-11-24 stsp }
7779 6458efa5 2020-11-24 stsp
7780 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7781 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7782 6458efa5 2020-11-24 stsp done:
7783 6458efa5 2020-11-24 stsp if (err)
7784 6458efa5 2020-11-24 stsp view_close(log_view);
7785 6458efa5 2020-11-24 stsp else
7786 6458efa5 2020-11-24 stsp *new_view = log_view;
7787 c42c9805 2020-11-24 stsp free(commit_id);
7788 6458efa5 2020-11-24 stsp return err;
7789 6458efa5 2020-11-24 stsp }
7790 6458efa5 2020-11-24 stsp
7791 ce5b7c56 2019-07-09 stsp static void
7792 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7793 6458efa5 2020-11-24 stsp {
7794 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7795 34ba6917 2020-11-30 stsp int i = 0;
7796 6458efa5 2020-11-24 stsp
7797 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7798 6458efa5 2020-11-24 stsp return;
7799 6458efa5 2020-11-24 stsp
7800 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7801 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7802 34ba6917 2020-11-30 stsp if (re == NULL)
7803 34ba6917 2020-11-30 stsp break;
7804 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7805 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7806 6458efa5 2020-11-24 stsp }
7807 6458efa5 2020-11-24 stsp }
7808 6458efa5 2020-11-24 stsp
7809 9b058f45 2022-06-30 mark static const struct got_error *
7810 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7811 6458efa5 2020-11-24 stsp {
7812 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7813 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7814 6458efa5 2020-11-24 stsp int n = 0;
7815 6458efa5 2020-11-24 stsp
7816 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7817 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7818 6458efa5 2020-11-24 stsp else
7819 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7820 6458efa5 2020-11-24 stsp
7821 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7822 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7823 94b80cfa 2022-08-01 mark if (last) {
7824 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7825 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7826 94b80cfa 2022-08-01 mark }
7827 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7828 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7829 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7830 6458efa5 2020-11-24 stsp }
7831 6458efa5 2020-11-24 stsp }
7832 9b058f45 2022-06-30 mark
7833 9b058f45 2022-06-30 mark return NULL;
7834 6458efa5 2020-11-24 stsp }
7835 6458efa5 2020-11-24 stsp
7836 6458efa5 2020-11-24 stsp static const struct got_error *
7837 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7838 6458efa5 2020-11-24 stsp {
7839 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7840 6458efa5 2020-11-24 stsp
7841 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7842 6458efa5 2020-11-24 stsp return NULL;
7843 6458efa5 2020-11-24 stsp }
7844 6458efa5 2020-11-24 stsp
7845 6458efa5 2020-11-24 stsp static int
7846 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7847 6458efa5 2020-11-24 stsp {
7848 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7849 6458efa5 2020-11-24 stsp
7850 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7851 6458efa5 2020-11-24 stsp 0) == 0;
7852 6458efa5 2020-11-24 stsp }
7853 6458efa5 2020-11-24 stsp
7854 6458efa5 2020-11-24 stsp static const struct got_error *
7855 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7856 6458efa5 2020-11-24 stsp {
7857 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7858 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7859 6458efa5 2020-11-24 stsp
7860 6458efa5 2020-11-24 stsp if (!view->searching) {
7861 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7862 6458efa5 2020-11-24 stsp return NULL;
7863 6458efa5 2020-11-24 stsp }
7864 6458efa5 2020-11-24 stsp
7865 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7866 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7867 6458efa5 2020-11-24 stsp if (s->selected_entry)
7868 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7869 6458efa5 2020-11-24 stsp else
7870 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7871 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7872 6458efa5 2020-11-24 stsp } else {
7873 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7874 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7875 6458efa5 2020-11-24 stsp else
7876 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7877 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7878 6458efa5 2020-11-24 stsp }
7879 6458efa5 2020-11-24 stsp } else {
7880 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7881 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7882 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7883 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7884 6458efa5 2020-11-24 stsp else
7885 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7886 6458efa5 2020-11-24 stsp }
7887 6458efa5 2020-11-24 stsp
7888 6458efa5 2020-11-24 stsp while (1) {
7889 6458efa5 2020-11-24 stsp if (re == NULL) {
7890 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7891 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7892 6458efa5 2020-11-24 stsp return NULL;
7893 6458efa5 2020-11-24 stsp }
7894 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7895 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7896 6458efa5 2020-11-24 stsp else
7897 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7898 6458efa5 2020-11-24 stsp }
7899 6458efa5 2020-11-24 stsp
7900 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7901 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7902 6458efa5 2020-11-24 stsp s->matched_entry = re;
7903 6458efa5 2020-11-24 stsp break;
7904 6458efa5 2020-11-24 stsp }
7905 6458efa5 2020-11-24 stsp
7906 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7907 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7908 6458efa5 2020-11-24 stsp else
7909 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7910 6458efa5 2020-11-24 stsp }
7911 6458efa5 2020-11-24 stsp
7912 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7913 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7914 6458efa5 2020-11-24 stsp s->selected = 0;
7915 6458efa5 2020-11-24 stsp }
7916 6458efa5 2020-11-24 stsp
7917 6458efa5 2020-11-24 stsp return NULL;
7918 6458efa5 2020-11-24 stsp }
7919 6458efa5 2020-11-24 stsp
7920 6458efa5 2020-11-24 stsp static const struct got_error *
7921 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7922 6458efa5 2020-11-24 stsp {
7923 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7924 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7925 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7926 6458efa5 2020-11-24 stsp char *line = NULL;
7927 6458efa5 2020-11-24 stsp wchar_t *wline;
7928 6458efa5 2020-11-24 stsp struct tog_color *tc;
7929 6458efa5 2020-11-24 stsp int width, n;
7930 6458efa5 2020-11-24 stsp int limit = view->nlines;
7931 6458efa5 2020-11-24 stsp
7932 6458efa5 2020-11-24 stsp werase(view->window);
7933 6458efa5 2020-11-24 stsp
7934 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7935 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7936 9b058f45 2022-06-30 mark --limit; /* border */
7937 6458efa5 2020-11-24 stsp
7938 6458efa5 2020-11-24 stsp if (limit == 0)
7939 6458efa5 2020-11-24 stsp return NULL;
7940 6458efa5 2020-11-24 stsp
7941 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7942 6458efa5 2020-11-24 stsp
7943 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7944 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7945 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7946 6458efa5 2020-11-24 stsp
7947 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7948 6458efa5 2020-11-24 stsp if (err) {
7949 6458efa5 2020-11-24 stsp free(line);
7950 6458efa5 2020-11-24 stsp return err;
7951 6458efa5 2020-11-24 stsp }
7952 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7953 6458efa5 2020-11-24 stsp wstandout(view->window);
7954 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7955 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
7956 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
7957 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7958 6458efa5 2020-11-24 stsp wstandend(view->window);
7959 6458efa5 2020-11-24 stsp free(wline);
7960 6458efa5 2020-11-24 stsp wline = NULL;
7961 6458efa5 2020-11-24 stsp free(line);
7962 6458efa5 2020-11-24 stsp line = NULL;
7963 6458efa5 2020-11-24 stsp if (--limit <= 0)
7964 6458efa5 2020-11-24 stsp return NULL;
7965 6458efa5 2020-11-24 stsp
7966 6458efa5 2020-11-24 stsp n = 0;
7967 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7968 6458efa5 2020-11-24 stsp char *line = NULL;
7969 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7970 6458efa5 2020-11-24 stsp
7971 b4996bee 2022-06-16 stsp if (s->show_date) {
7972 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7973 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7974 b4996bee 2022-06-16 stsp struct got_object_id *id;
7975 b4996bee 2022-06-16 stsp struct tm tm;
7976 b4996bee 2022-06-16 stsp time_t t;
7977 b4996bee 2022-06-16 stsp
7978 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7979 b4996bee 2022-06-16 stsp if (err)
7980 b4996bee 2022-06-16 stsp return err;
7981 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7982 b4996bee 2022-06-16 stsp if (err) {
7983 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7984 b4996bee 2022-06-16 stsp free(id);
7985 b4996bee 2022-06-16 stsp return err;
7986 b4996bee 2022-06-16 stsp }
7987 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7988 b4996bee 2022-06-16 stsp id);
7989 b4996bee 2022-06-16 stsp if (err) {
7990 b4996bee 2022-06-16 stsp free(id);
7991 b4996bee 2022-06-16 stsp return err;
7992 b4996bee 2022-06-16 stsp }
7993 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7994 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7995 b4996bee 2022-06-16 stsp } else {
7996 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7997 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7998 b4996bee 2022-06-16 stsp }
7999 b4996bee 2022-06-16 stsp free(id);
8000 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
8001 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
8002 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8003 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
8004 b4996bee 2022-06-16 stsp }
8005 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8006 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
8007 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
8008 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8009 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8010 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8011 6458efa5 2020-11-24 stsp struct got_object_id *id;
8012 6458efa5 2020-11-24 stsp char *id_str;
8013 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8014 6458efa5 2020-11-24 stsp if (err)
8015 6458efa5 2020-11-24 stsp return err;
8016 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8017 6458efa5 2020-11-24 stsp if (err) {
8018 6458efa5 2020-11-24 stsp free(id);
8019 6458efa5 2020-11-24 stsp return err;
8020 6458efa5 2020-11-24 stsp }
8021 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8022 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8023 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8024 6458efa5 2020-11-24 stsp free(id);
8025 6458efa5 2020-11-24 stsp free(id_str);
8026 6458efa5 2020-11-24 stsp return err;
8027 6458efa5 2020-11-24 stsp }
8028 6458efa5 2020-11-24 stsp free(id);
8029 6458efa5 2020-11-24 stsp free(id_str);
8030 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8031 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
8032 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
8033 6458efa5 2020-11-24 stsp
8034 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8035 ccda2f4d 2022-06-16 stsp 0, 0);
8036 6458efa5 2020-11-24 stsp if (err) {
8037 6458efa5 2020-11-24 stsp free(line);
8038 6458efa5 2020-11-24 stsp return err;
8039 6458efa5 2020-11-24 stsp }
8040 6458efa5 2020-11-24 stsp if (n == s->selected) {
8041 6458efa5 2020-11-24 stsp if (view->focussed)
8042 6458efa5 2020-11-24 stsp wstandout(view->window);
8043 6458efa5 2020-11-24 stsp s->selected_entry = re;
8044 6458efa5 2020-11-24 stsp }
8045 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8046 6458efa5 2020-11-24 stsp if (tc)
8047 6458efa5 2020-11-24 stsp wattr_on(view->window,
8048 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8049 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8050 6458efa5 2020-11-24 stsp if (tc)
8051 6458efa5 2020-11-24 stsp wattr_off(view->window,
8052 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8053 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
8054 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8055 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8056 6458efa5 2020-11-24 stsp wstandend(view->window);
8057 6458efa5 2020-11-24 stsp free(line);
8058 6458efa5 2020-11-24 stsp free(wline);
8059 6458efa5 2020-11-24 stsp wline = NULL;
8060 6458efa5 2020-11-24 stsp n++;
8061 6458efa5 2020-11-24 stsp s->ndisplayed++;
8062 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8063 6458efa5 2020-11-24 stsp
8064 6458efa5 2020-11-24 stsp limit--;
8065 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8066 6458efa5 2020-11-24 stsp }
8067 6458efa5 2020-11-24 stsp
8068 9b058f45 2022-06-30 mark view_border(view);
8069 6458efa5 2020-11-24 stsp return err;
8070 6458efa5 2020-11-24 stsp }
8071 6458efa5 2020-11-24 stsp
8072 6458efa5 2020-11-24 stsp static const struct got_error *
8073 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8074 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8075 c42c9805 2020-11-24 stsp {
8076 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8077 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8078 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8079 c42c9805 2020-11-24 stsp
8080 c42c9805 2020-11-24 stsp *new_view = NULL;
8081 c42c9805 2020-11-24 stsp
8082 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8083 c42c9805 2020-11-24 stsp if (err) {
8084 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8085 c42c9805 2020-11-24 stsp return err;
8086 c42c9805 2020-11-24 stsp else
8087 c42c9805 2020-11-24 stsp return NULL;
8088 c42c9805 2020-11-24 stsp }
8089 c42c9805 2020-11-24 stsp
8090 c42c9805 2020-11-24 stsp
8091 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8092 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8093 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8094 c42c9805 2020-11-24 stsp goto done;
8095 c42c9805 2020-11-24 stsp }
8096 c42c9805 2020-11-24 stsp
8097 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8098 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8099 c42c9805 2020-11-24 stsp if (err)
8100 c42c9805 2020-11-24 stsp goto done;
8101 c42c9805 2020-11-24 stsp
8102 c42c9805 2020-11-24 stsp *new_view = tree_view;
8103 c42c9805 2020-11-24 stsp done:
8104 c42c9805 2020-11-24 stsp free(commit_id);
8105 c42c9805 2020-11-24 stsp return err;
8106 94b80cfa 2022-08-01 mark }
8107 94b80cfa 2022-08-01 mark
8108 94b80cfa 2022-08-01 mark static const struct got_error *
8109 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
8110 94b80cfa 2022-08-01 mark {
8111 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
8112 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
8113 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
8114 94b80cfa 2022-08-01 mark
8115 94b80cfa 2022-08-01 mark g = view->gline;
8116 94b80cfa 2022-08-01 mark view->gline = 0;
8117 94b80cfa 2022-08-01 mark
8118 94b80cfa 2022-08-01 mark if (g == 0)
8119 94b80cfa 2022-08-01 mark g = 1;
8120 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
8121 94b80cfa 2022-08-01 mark g = s->nrefs;
8122 94b80cfa 2022-08-01 mark
8123 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
8124 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
8125 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
8126 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8127 94b80cfa 2022-08-01 mark return NULL;
8128 94b80cfa 2022-08-01 mark }
8129 94b80cfa 2022-08-01 mark
8130 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
8131 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
8132 94b80cfa 2022-08-01 mark if (err)
8133 94b80cfa 2022-08-01 mark return err;
8134 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8135 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
8136 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
8137 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8138 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
8139 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
8140 94b80cfa 2022-08-01 mark
8141 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
8142 94b80cfa 2022-08-01 mark s->selected = g - 1;
8143 94b80cfa 2022-08-01 mark
8144 94b80cfa 2022-08-01 mark return NULL;
8145 94b80cfa 2022-08-01 mark
8146 c42c9805 2020-11-24 stsp }
8147 94b80cfa 2022-08-01 mark
8148 c42c9805 2020-11-24 stsp static const struct got_error *
8149 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8150 6458efa5 2020-11-24 stsp {
8151 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8152 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8153 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8154 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
8155 6458efa5 2020-11-24 stsp
8156 94b80cfa 2022-08-01 mark if (view->gline)
8157 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
8158 94b80cfa 2022-08-01 mark
8159 6458efa5 2020-11-24 stsp switch (ch) {
8160 6458efa5 2020-11-24 stsp case 'i':
8161 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8162 640cd7ff 2022-06-22 mark view->count = 0;
8163 7f66531d 2021-11-16 stsp break;
8164 b4996bee 2022-06-16 stsp case 'm':
8165 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
8166 640cd7ff 2022-06-22 mark view->count = 0;
8167 b4996bee 2022-06-16 stsp break;
8168 07a065fe 2021-11-20 stsp case 'o':
8169 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
8170 640cd7ff 2022-06-22 mark view->count = 0;
8171 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8172 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
8173 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
8174 50617b77 2021-11-20 stsp if (err)
8175 50617b77 2021-11-20 stsp break;
8176 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
8177 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
8178 50617b77 2021-11-20 stsp &tog_refs, s->repo);
8179 7f66531d 2021-11-16 stsp if (err)
8180 7f66531d 2021-11-16 stsp break;
8181 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
8182 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
8183 6458efa5 2020-11-24 stsp break;
8184 6458efa5 2020-11-24 stsp case KEY_ENTER:
8185 6458efa5 2020-11-24 stsp case '\r':
8186 640cd7ff 2022-06-22 mark view->count = 0;
8187 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8188 9b058f45 2022-06-30 mark break;
8189 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
8190 6458efa5 2020-11-24 stsp break;
8191 5e98fb33 2022-07-22 mark case 'T':
8192 640cd7ff 2022-06-22 mark view->count = 0;
8193 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8194 c42c9805 2020-11-24 stsp break;
8195 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
8196 c42c9805 2020-11-24 stsp break;
8197 e4526bf5 2021-09-03 naddy case 'g':
8198 e4526bf5 2021-09-03 naddy case KEY_HOME:
8199 e4526bf5 2021-09-03 naddy s->selected = 0;
8200 640cd7ff 2022-06-22 mark view->count = 0;
8201 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8202 e4526bf5 2021-09-03 naddy break;
8203 e4526bf5 2021-09-03 naddy case 'G':
8204 9b058f45 2022-06-30 mark case KEY_END: {
8205 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
8206 9b058f45 2022-06-30 mark
8207 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
8208 9b058f45 2022-06-30 mark --eos; /* border */
8209 e4526bf5 2021-09-03 naddy s->selected = 0;
8210 640cd7ff 2022-06-22 mark view->count = 0;
8211 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8212 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
8213 e4526bf5 2021-09-03 naddy if (re == NULL)
8214 e4526bf5 2021-09-03 naddy break;
8215 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8216 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8217 e4526bf5 2021-09-03 naddy }
8218 e4526bf5 2021-09-03 naddy if (n > 0)
8219 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8220 e4526bf5 2021-09-03 naddy break;
8221 9b058f45 2022-06-30 mark }
8222 6458efa5 2020-11-24 stsp case 'k':
8223 6458efa5 2020-11-24 stsp case KEY_UP:
8224 02ffd0d5 2021-10-17 stsp case CTRL('p'):
8225 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8226 6458efa5 2020-11-24 stsp s->selected--;
8227 6458efa5 2020-11-24 stsp break;
8228 34ba6917 2020-11-30 stsp }
8229 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8230 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8231 640cd7ff 2022-06-22 mark view->count = 0;
8232 6458efa5 2020-11-24 stsp break;
8233 83cc4199 2022-06-13 stsp case CTRL('u'):
8234 33c3719a 2022-06-15 stsp case 'u':
8235 83cc4199 2022-06-13 stsp nscroll /= 2;
8236 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8237 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8238 6458efa5 2020-11-24 stsp case CTRL('b'):
8239 61417565 2022-06-20 mark case 'b':
8240 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8241 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
8242 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
8243 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8244 640cd7ff 2022-06-22 mark view->count = 0;
8245 6458efa5 2020-11-24 stsp break;
8246 6458efa5 2020-11-24 stsp case 'j':
8247 6458efa5 2020-11-24 stsp case KEY_DOWN:
8248 02ffd0d5 2021-10-17 stsp case CTRL('n'):
8249 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8250 6458efa5 2020-11-24 stsp s->selected++;
8251 6458efa5 2020-11-24 stsp break;
8252 6458efa5 2020-11-24 stsp }
8253 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8254 6458efa5 2020-11-24 stsp /* can't scroll any further */
8255 640cd7ff 2022-06-22 mark view->count = 0;
8256 6458efa5 2020-11-24 stsp break;
8257 640cd7ff 2022-06-22 mark }
8258 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
8259 6458efa5 2020-11-24 stsp break;
8260 83cc4199 2022-06-13 stsp case CTRL('d'):
8261 33c3719a 2022-06-15 stsp case 'd':
8262 83cc4199 2022-06-13 stsp nscroll /= 2;
8263 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8264 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8265 6458efa5 2020-11-24 stsp case CTRL('f'):
8266 61417565 2022-06-20 mark case 'f':
8267 48bb96f0 2022-06-20 naddy case ' ':
8268 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8269 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8270 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8271 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
8272 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
8273 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
8274 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
8275 640cd7ff 2022-06-22 mark view->count = 0;
8276 6458efa5 2020-11-24 stsp break;
8277 6458efa5 2020-11-24 stsp }
8278 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
8279 6458efa5 2020-11-24 stsp break;
8280 6458efa5 2020-11-24 stsp case CTRL('l'):
8281 640cd7ff 2022-06-22 mark view->count = 0;
8282 8924d611 2020-12-26 stsp tog_free_refs();
8283 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
8284 8924d611 2020-12-26 stsp if (err)
8285 8924d611 2020-12-26 stsp break;
8286 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8287 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8288 6458efa5 2020-11-24 stsp break;
8289 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8290 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8291 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8292 6458efa5 2020-11-24 stsp break;
8293 6458efa5 2020-11-24 stsp default:
8294 640cd7ff 2022-06-22 mark view->count = 0;
8295 6458efa5 2020-11-24 stsp break;
8296 6458efa5 2020-11-24 stsp }
8297 6458efa5 2020-11-24 stsp
8298 6458efa5 2020-11-24 stsp return err;
8299 6458efa5 2020-11-24 stsp }
8300 6458efa5 2020-11-24 stsp
8301 6458efa5 2020-11-24 stsp __dead static void
8302 6458efa5 2020-11-24 stsp usage_ref(void)
8303 6458efa5 2020-11-24 stsp {
8304 6458efa5 2020-11-24 stsp endwin();
8305 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8306 6458efa5 2020-11-24 stsp getprogname());
8307 6458efa5 2020-11-24 stsp exit(1);
8308 6458efa5 2020-11-24 stsp }
8309 6458efa5 2020-11-24 stsp
8310 6458efa5 2020-11-24 stsp static const struct got_error *
8311 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8312 6458efa5 2020-11-24 stsp {
8313 6458efa5 2020-11-24 stsp const struct got_error *error;
8314 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8315 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8316 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8317 6458efa5 2020-11-24 stsp int ch;
8318 6458efa5 2020-11-24 stsp struct tog_view *view;
8319 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8320 6458efa5 2020-11-24 stsp
8321 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8322 6458efa5 2020-11-24 stsp switch (ch) {
8323 6458efa5 2020-11-24 stsp case 'r':
8324 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8325 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8326 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8327 6458efa5 2020-11-24 stsp optarg);
8328 6458efa5 2020-11-24 stsp break;
8329 6458efa5 2020-11-24 stsp default:
8330 e99e2d15 2020-11-24 naddy usage_ref();
8331 6458efa5 2020-11-24 stsp /* NOTREACHED */
8332 6458efa5 2020-11-24 stsp }
8333 6458efa5 2020-11-24 stsp }
8334 6458efa5 2020-11-24 stsp
8335 6458efa5 2020-11-24 stsp argc -= optind;
8336 6458efa5 2020-11-24 stsp argv += optind;
8337 6458efa5 2020-11-24 stsp
8338 6458efa5 2020-11-24 stsp if (argc > 1)
8339 e99e2d15 2020-11-24 naddy usage_ref();
8340 0ae84acc 2022-06-15 tracey
8341 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8342 0ae84acc 2022-06-15 tracey if (error != NULL)
8343 0ae84acc 2022-06-15 tracey goto done;
8344 6458efa5 2020-11-24 stsp
8345 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8346 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8347 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8348 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8349 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8350 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8351 c156c7a4 2020-12-18 stsp goto done;
8352 6458efa5 2020-11-24 stsp if (worktree)
8353 6458efa5 2020-11-24 stsp repo_path =
8354 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8355 6458efa5 2020-11-24 stsp else
8356 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8357 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8358 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8359 c156c7a4 2020-12-18 stsp goto done;
8360 c156c7a4 2020-12-18 stsp }
8361 6458efa5 2020-11-24 stsp }
8362 6458efa5 2020-11-24 stsp
8363 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8364 6458efa5 2020-11-24 stsp if (error != NULL)
8365 6458efa5 2020-11-24 stsp goto done;
8366 6458efa5 2020-11-24 stsp
8367 6458efa5 2020-11-24 stsp init_curses();
8368 6458efa5 2020-11-24 stsp
8369 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8370 51a10b52 2020-12-26 stsp if (error)
8371 51a10b52 2020-12-26 stsp goto done;
8372 51a10b52 2020-12-26 stsp
8373 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8374 6458efa5 2020-11-24 stsp if (error)
8375 6458efa5 2020-11-24 stsp goto done;
8376 6458efa5 2020-11-24 stsp
8377 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8378 6458efa5 2020-11-24 stsp if (view == NULL) {
8379 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8380 6458efa5 2020-11-24 stsp goto done;
8381 6458efa5 2020-11-24 stsp }
8382 6458efa5 2020-11-24 stsp
8383 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8384 6458efa5 2020-11-24 stsp if (error)
8385 6458efa5 2020-11-24 stsp goto done;
8386 6458efa5 2020-11-24 stsp
8387 6458efa5 2020-11-24 stsp if (worktree) {
8388 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8389 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8390 6458efa5 2020-11-24 stsp worktree = NULL;
8391 6458efa5 2020-11-24 stsp }
8392 6458efa5 2020-11-24 stsp error = view_loop(view);
8393 6458efa5 2020-11-24 stsp done:
8394 6458efa5 2020-11-24 stsp free(repo_path);
8395 6458efa5 2020-11-24 stsp free(cwd);
8396 1d0f4054 2021-06-17 stsp if (repo) {
8397 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8398 1d0f4054 2021-06-17 stsp if (close_err)
8399 1d0f4054 2021-06-17 stsp error = close_err;
8400 1d0f4054 2021-06-17 stsp }
8401 0ae84acc 2022-06-15 tracey if (pack_fds) {
8402 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8403 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8404 0ae84acc 2022-06-15 tracey if (error == NULL)
8405 0ae84acc 2022-06-15 tracey error = pack_err;
8406 0ae84acc 2022-06-15 tracey }
8407 51a10b52 2020-12-26 stsp tog_free_refs();
8408 6458efa5 2020-11-24 stsp return error;
8409 6458efa5 2020-11-24 stsp }
8410 6458efa5 2020-11-24 stsp
8411 ec2a9698 2022-09-15 mark static const struct got_error*
8412 ec2a9698 2022-09-15 mark win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8413 ec2a9698 2022-09-15 mark const char *str)
8414 ec2a9698 2022-09-15 mark {
8415 ec2a9698 2022-09-15 mark size_t len;
8416 ec2a9698 2022-09-15 mark
8417 ec2a9698 2022-09-15 mark if (win == NULL)
8418 ec2a9698 2022-09-15 mark win = stdscr;
8419 ec2a9698 2022-09-15 mark
8420 ec2a9698 2022-09-15 mark len = strlen(str);
8421 ec2a9698 2022-09-15 mark x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8422 ec2a9698 2022-09-15 mark
8423 ec2a9698 2022-09-15 mark if (focus)
8424 ec2a9698 2022-09-15 mark wstandout(win);
8425 ec2a9698 2022-09-15 mark if (mvwprintw(win, y, x, "%s", str) == ERR)
8426 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8427 ec2a9698 2022-09-15 mark if (focus)
8428 ec2a9698 2022-09-15 mark wstandend(win);
8429 ec2a9698 2022-09-15 mark
8430 ec2a9698 2022-09-15 mark return NULL;
8431 ec2a9698 2022-09-15 mark }
8432 ec2a9698 2022-09-15 mark
8433 136e2bd4 2022-07-23 mark static const struct got_error *
8434 ec2a9698 2022-09-15 mark add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8435 ec2a9698 2022-09-15 mark {
8436 ec2a9698 2022-09-15 mark off_t *p;
8437 ec2a9698 2022-09-15 mark
8438 ec2a9698 2022-09-15 mark p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8439 ec2a9698 2022-09-15 mark if (p == NULL) {
8440 ec2a9698 2022-09-15 mark free(*line_offsets);
8441 ec2a9698 2022-09-15 mark *line_offsets = NULL;
8442 ec2a9698 2022-09-15 mark return got_error_from_errno("reallocarray");
8443 ec2a9698 2022-09-15 mark }
8444 ec2a9698 2022-09-15 mark
8445 ec2a9698 2022-09-15 mark *line_offsets = p;
8446 ec2a9698 2022-09-15 mark (*line_offsets)[*nlines] = off;
8447 ec2a9698 2022-09-15 mark ++(*nlines);
8448 ec2a9698 2022-09-15 mark return NULL;
8449 ec2a9698 2022-09-15 mark }
8450 ec2a9698 2022-09-15 mark
8451 ec2a9698 2022-09-15 mark static const struct got_error *
8452 ec2a9698 2022-09-15 mark max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8453 ec2a9698 2022-09-15 mark {
8454 ec2a9698 2022-09-15 mark *ret = 0;
8455 ec2a9698 2022-09-15 mark
8456 ec2a9698 2022-09-15 mark for (;n > 0; --n, ++km) {
8457 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8458 ec2a9698 2022-09-15 mark size_t len = 1;
8459 ec2a9698 2022-09-15 mark
8460 ec2a9698 2022-09-15 mark if (km->keys == NULL)
8461 ec2a9698 2022-09-15 mark continue;
8462 ec2a9698 2022-09-15 mark
8463 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8464 ec2a9698 2022-09-15 mark if (t0 == NULL)
8465 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8466 ec2a9698 2022-09-15 mark
8467 ec2a9698 2022-09-15 mark len += strlen(t);
8468 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL)
8469 ec2a9698 2022-09-15 mark len += strlen(k) > 1 ? 2 : 0;
8470 ec2a9698 2022-09-15 mark free(t0);
8471 ec2a9698 2022-09-15 mark *ret = MAX(*ret, len);
8472 ec2a9698 2022-09-15 mark }
8473 ec2a9698 2022-09-15 mark
8474 ec2a9698 2022-09-15 mark return NULL;
8475 ec2a9698 2022-09-15 mark }
8476 ec2a9698 2022-09-15 mark
8477 ec2a9698 2022-09-15 mark /*
8478 ec2a9698 2022-09-15 mark * Write keymap section headers, keys, and key info in km to f.
8479 ec2a9698 2022-09-15 mark * Save line offset to *off. If terminal has UTF8 encoding enabled,
8480 ec2a9698 2022-09-15 mark * wrap control and symbolic keys in guillemets, else use <>.
8481 ec2a9698 2022-09-15 mark */
8482 ec2a9698 2022-09-15 mark static const struct got_error *
8483 ec2a9698 2022-09-15 mark format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8484 ec2a9698 2022-09-15 mark {
8485 ec2a9698 2022-09-15 mark int n, len = width;
8486 ec2a9698 2022-09-15 mark
8487 ec2a9698 2022-09-15 mark if (km->keys) {
8488 1681ba87 2022-09-18 mark static const char *u8_glyph[] = {
8489 1681ba87 2022-09-18 mark "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8490 1681ba87 2022-09-18 mark "\xe2\x80\xba" /* U+203A (utf8 >) */
8491 1681ba87 2022-09-18 mark };
8492 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8493 ec2a9698 2022-09-15 mark int cs, s, first = 1;
8494 ec2a9698 2022-09-15 mark
8495 ec2a9698 2022-09-15 mark cs = got_locale_is_utf8();
8496 ec2a9698 2022-09-15 mark
8497 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8498 ec2a9698 2022-09-15 mark if (t0 == NULL)
8499 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8500 ec2a9698 2022-09-15 mark
8501 ec2a9698 2022-09-15 mark len = strlen(km->keys);
8502 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL) {
8503 ec2a9698 2022-09-15 mark s = strlen(k) > 1; /* control or symbolic key */
8504 ec2a9698 2022-09-15 mark n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8505 1681ba87 2022-09-18 mark cs && s ? u8_glyph[0] : s ? "<" : "", k,
8506 1681ba87 2022-09-18 mark cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8507 ec2a9698 2022-09-15 mark if (n < 0) {
8508 ec2a9698 2022-09-15 mark free(t0);
8509 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8510 ec2a9698 2022-09-15 mark }
8511 ec2a9698 2022-09-15 mark first = 0;
8512 ec2a9698 2022-09-15 mark len += s ? 2 : 0;
8513 ec2a9698 2022-09-15 mark *off += n;
8514 ec2a9698 2022-09-15 mark }
8515 ec2a9698 2022-09-15 mark free(t0);
8516 ec2a9698 2022-09-15 mark }
8517 ec2a9698 2022-09-15 mark n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8518 ec2a9698 2022-09-15 mark if (n < 0)
8519 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8520 ec2a9698 2022-09-15 mark *off += n;
8521 ec2a9698 2022-09-15 mark
8522 ec2a9698 2022-09-15 mark return NULL;
8523 ec2a9698 2022-09-15 mark }
8524 ec2a9698 2022-09-15 mark
8525 ec2a9698 2022-09-15 mark static const struct got_error *
8526 ec2a9698 2022-09-15 mark format_help(struct tog_help_view_state *s)
8527 ec2a9698 2022-09-15 mark {
8528 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8529 ec2a9698 2022-09-15 mark off_t off = 0;
8530 ec2a9698 2022-09-15 mark int i, max, n, show = s->all;
8531 ec2a9698 2022-09-15 mark static const struct tog_key_map km[] = {
8532 ec2a9698 2022-09-15 mark #define KEYMAP_(info, type) { NULL, (info), type }
8533 ec2a9698 2022-09-15 mark #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8534 ec2a9698 2022-09-15 mark GENERATE_HELP
8535 ec2a9698 2022-09-15 mark #undef KEYMAP_
8536 ec2a9698 2022-09-15 mark #undef KEY_
8537 ec2a9698 2022-09-15 mark };
8538 ec2a9698 2022-09-15 mark
8539 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8540 ec2a9698 2022-09-15 mark if (err)
8541 ec2a9698 2022-09-15 mark return err;
8542 ec2a9698 2022-09-15 mark
8543 ec2a9698 2022-09-15 mark n = nitems(km);
8544 ec2a9698 2022-09-15 mark err = max_key_str(&max, km, n);
8545 ec2a9698 2022-09-15 mark if (err)
8546 ec2a9698 2022-09-15 mark return err;
8547 ec2a9698 2022-09-15 mark
8548 ec2a9698 2022-09-15 mark for (i = 0; i < n; ++i) {
8549 ec2a9698 2022-09-15 mark if (km[i].keys == NULL) {
8550 ec2a9698 2022-09-15 mark show = s->all;
8551 ec2a9698 2022-09-15 mark if (km[i].type == TOG_KEYMAP_GLOBAL ||
8552 ec2a9698 2022-09-15 mark km[i].type == s->type || s->all)
8553 ec2a9698 2022-09-15 mark show = 1;
8554 ec2a9698 2022-09-15 mark }
8555 ec2a9698 2022-09-15 mark if (show) {
8556 ec2a9698 2022-09-15 mark err = format_help_line(&off, s->f, &km[i], max);
8557 ec2a9698 2022-09-15 mark if (err)
8558 ec2a9698 2022-09-15 mark return err;
8559 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8560 ec2a9698 2022-09-15 mark if (err)
8561 ec2a9698 2022-09-15 mark return err;
8562 ec2a9698 2022-09-15 mark }
8563 ec2a9698 2022-09-15 mark }
8564 ec2a9698 2022-09-15 mark fputc('\n', s->f);
8565 ec2a9698 2022-09-15 mark ++off;
8566 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8567 ec2a9698 2022-09-15 mark return err;
8568 ec2a9698 2022-09-15 mark }
8569 ec2a9698 2022-09-15 mark
8570 ec2a9698 2022-09-15 mark static const struct got_error *
8571 ec2a9698 2022-09-15 mark create_help(struct tog_help_view_state *s)
8572 ec2a9698 2022-09-15 mark {
8573 ec2a9698 2022-09-15 mark FILE *f;
8574 ec2a9698 2022-09-15 mark const struct got_error *err;
8575 ec2a9698 2022-09-15 mark
8576 ec2a9698 2022-09-15 mark free(s->line_offsets);
8577 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8578 ec2a9698 2022-09-15 mark s->nlines = 0;
8579 ec2a9698 2022-09-15 mark
8580 ec2a9698 2022-09-15 mark f = got_opentemp();
8581 ec2a9698 2022-09-15 mark if (f == NULL)
8582 ec2a9698 2022-09-15 mark return got_error_from_errno("got_opentemp");
8583 ec2a9698 2022-09-15 mark s->f = f;
8584 ec2a9698 2022-09-15 mark
8585 ec2a9698 2022-09-15 mark err = format_help(s);
8586 ec2a9698 2022-09-15 mark if (err)
8587 ec2a9698 2022-09-15 mark return err;
8588 ec2a9698 2022-09-15 mark
8589 ec2a9698 2022-09-15 mark if (s->f && fflush(s->f) != 0)
8590 ec2a9698 2022-09-15 mark return got_error_from_errno("fflush");
8591 ec2a9698 2022-09-15 mark
8592 ec2a9698 2022-09-15 mark return NULL;
8593 ec2a9698 2022-09-15 mark }
8594 ec2a9698 2022-09-15 mark
8595 ec2a9698 2022-09-15 mark static const struct got_error *
8596 ec2a9698 2022-09-15 mark search_start_help_view(struct tog_view *view)
8597 ec2a9698 2022-09-15 mark {
8598 ec2a9698 2022-09-15 mark view->state.help.matched_line = 0;
8599 ec2a9698 2022-09-15 mark return NULL;
8600 ec2a9698 2022-09-15 mark }
8601 ec2a9698 2022-09-15 mark
8602 eaf2c13e 2022-09-17 mark static void
8603 eaf2c13e 2022-09-17 mark search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8604 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
8605 eaf2c13e 2022-09-17 mark {
8606 eaf2c13e 2022-09-17 mark struct tog_help_view_state *s = &view->state.help;
8607 eaf2c13e 2022-09-17 mark
8608 eaf2c13e 2022-09-17 mark *f = s->f;
8609 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
8610 eaf2c13e 2022-09-17 mark *line_offsets = s->line_offsets;
8611 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
8612 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
8613 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
8614 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
8615 eaf2c13e 2022-09-17 mark }
8616 eaf2c13e 2022-09-17 mark
8617 ec2a9698 2022-09-15 mark static const struct got_error *
8618 ec2a9698 2022-09-15 mark show_help_view(struct tog_view *view)
8619 ec2a9698 2022-09-15 mark {
8620 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8621 ec2a9698 2022-09-15 mark const struct got_error *err;
8622 ec2a9698 2022-09-15 mark regmatch_t *regmatch = &view->regmatch;
8623 ec2a9698 2022-09-15 mark wchar_t *wline;
8624 ec2a9698 2022-09-15 mark char *line;
8625 ec2a9698 2022-09-15 mark ssize_t linelen;
8626 ec2a9698 2022-09-15 mark size_t linesz = 0;
8627 ec2a9698 2022-09-15 mark int width, nprinted = 0, rc = 0;
8628 ec2a9698 2022-09-15 mark int eos = view->nlines;
8629 ec2a9698 2022-09-15 mark
8630 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8631 ec2a9698 2022-09-15 mark --eos; /* account for border */
8632 ec2a9698 2022-09-15 mark
8633 ec2a9698 2022-09-15 mark s->lineno = 0;
8634 ec2a9698 2022-09-15 mark rewind(s->f);
8635 ec2a9698 2022-09-15 mark werase(view->window);
8636 ec2a9698 2022-09-15 mark
8637 ec2a9698 2022-09-15 mark if (view->gline > s->nlines - 1)
8638 ec2a9698 2022-09-15 mark view->gline = s->nlines - 1;
8639 ec2a9698 2022-09-15 mark
8640 ec2a9698 2022-09-15 mark err = win_draw_center(view->window, 0, 0, view->ncols,
8641 16a2d4f0 2022-09-19 stsp view_needs_focus_indication(view),
8642 12c07a25 2022-09-19 stsp "tog help (press q to return to tog)");
8643 ec2a9698 2022-09-15 mark if (err)
8644 ec2a9698 2022-09-15 mark return err;
8645 ec2a9698 2022-09-15 mark if (eos <= 1)
8646 ec2a9698 2022-09-15 mark return NULL;
8647 ec2a9698 2022-09-15 mark waddstr(view->window, "\n\n");
8648 ec2a9698 2022-09-15 mark eos -= 2;
8649 ec2a9698 2022-09-15 mark
8650 ec2a9698 2022-09-15 mark s->eof = 0;
8651 ec2a9698 2022-09-15 mark view->maxx = 0;
8652 ec2a9698 2022-09-15 mark line = NULL;
8653 ec2a9698 2022-09-15 mark while (eos > 0 && nprinted < eos) {
8654 ec2a9698 2022-09-15 mark attr_t attr = 0;
8655 ec2a9698 2022-09-15 mark
8656 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8657 ec2a9698 2022-09-15 mark if (linelen == -1) {
8658 ec2a9698 2022-09-15 mark if (!feof(s->f)) {
8659 ec2a9698 2022-09-15 mark free(line);
8660 ec2a9698 2022-09-15 mark return got_ferror(s->f, GOT_ERR_IO);
8661 ec2a9698 2022-09-15 mark }
8662 ec2a9698 2022-09-15 mark s->eof = 1;
8663 ec2a9698 2022-09-15 mark break;
8664 ec2a9698 2022-09-15 mark }
8665 ec2a9698 2022-09-15 mark if (++s->lineno < s->first_displayed_line)
8666 ec2a9698 2022-09-15 mark continue;
8667 ec2a9698 2022-09-15 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8668 ec2a9698 2022-09-15 mark continue;
8669 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline)
8670 ec2a9698 2022-09-15 mark attr = A_STANDOUT;
8671 ec2a9698 2022-09-15 mark
8672 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8673 ec2a9698 2022-09-15 mark view->x ? 1 : 0);
8674 ec2a9698 2022-09-15 mark if (err) {
8675 ec2a9698 2022-09-15 mark free(line);
8676 ec2a9698 2022-09-15 mark return err;
8677 ec2a9698 2022-09-15 mark }
8678 ec2a9698 2022-09-15 mark view->maxx = MAX(view->maxx, width);
8679 ec2a9698 2022-09-15 mark free(wline);
8680 ec2a9698 2022-09-15 mark wline = NULL;
8681 ec2a9698 2022-09-15 mark
8682 ec2a9698 2022-09-15 mark if (attr)
8683 ec2a9698 2022-09-15 mark wattron(view->window, attr);
8684 ec2a9698 2022-09-15 mark if (s->first_displayed_line + nprinted == s->matched_line &&
8685 ec2a9698 2022-09-15 mark regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8686 ec2a9698 2022-09-15 mark err = add_matched_line(&width, line, view->ncols - 1, 0,
8687 ec2a9698 2022-09-15 mark view->window, view->x, regmatch);
8688 ec2a9698 2022-09-15 mark if (err) {
8689 ec2a9698 2022-09-15 mark free(line);
8690 ec2a9698 2022-09-15 mark return err;
8691 ec2a9698 2022-09-15 mark }
8692 ec2a9698 2022-09-15 mark } else {
8693 ec2a9698 2022-09-15 mark int skip;
8694 ec2a9698 2022-09-15 mark
8695 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, &skip, line,
8696 ec2a9698 2022-09-15 mark view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8697 ec2a9698 2022-09-15 mark if (err) {
8698 ec2a9698 2022-09-15 mark free(line);
8699 ec2a9698 2022-09-15 mark return err;
8700 ec2a9698 2022-09-15 mark }
8701 ec2a9698 2022-09-15 mark rc = waddwstr(view->window, &wline[skip]);
8702 ec2a9698 2022-09-15 mark free(wline);
8703 ec2a9698 2022-09-15 mark wline = NULL;
8704 ec2a9698 2022-09-15 mark if (rc == ERR)
8705 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "waddwstr");
8706 ec2a9698 2022-09-15 mark }
8707 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline) {
8708 ec2a9698 2022-09-15 mark while (width++ < view->ncols)
8709 ec2a9698 2022-09-15 mark waddch(view->window, ' ');
8710 ec2a9698 2022-09-15 mark } else {
8711 ec2a9698 2022-09-15 mark if (width <= view->ncols)
8712 ec2a9698 2022-09-15 mark waddch(view->window, '\n');
8713 ec2a9698 2022-09-15 mark }
8714 ec2a9698 2022-09-15 mark if (attr)
8715 ec2a9698 2022-09-15 mark wattroff(view->window, attr);
8716 ec2a9698 2022-09-15 mark if (++nprinted == 1)
8717 ec2a9698 2022-09-15 mark s->first_displayed_line = s->lineno;
8718 ec2a9698 2022-09-15 mark }
8719 ec2a9698 2022-09-15 mark free(line);
8720 ec2a9698 2022-09-15 mark if (nprinted > 0)
8721 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8722 ec2a9698 2022-09-15 mark else
8723 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line;
8724 ec2a9698 2022-09-15 mark
8725 ec2a9698 2022-09-15 mark view_border(view);
8726 ec2a9698 2022-09-15 mark
8727 ec2a9698 2022-09-15 mark if (s->eof) {
8728 ec2a9698 2022-09-15 mark rc = waddnstr(view->window,
8729 ec2a9698 2022-09-15 mark "See the tog(1) manual page for full documentation",
8730 ec2a9698 2022-09-15 mark view->ncols - 1);
8731 ec2a9698 2022-09-15 mark if (rc == ERR)
8732 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8733 ec2a9698 2022-09-15 mark } else {
8734 ec2a9698 2022-09-15 mark wmove(view->window, view->nlines - 1, 0);
8735 ec2a9698 2022-09-15 mark wclrtoeol(view->window);
8736 ec2a9698 2022-09-15 mark wstandout(view->window);
8737 ec2a9698 2022-09-15 mark rc = waddnstr(view->window, "scroll down for more...",
8738 ec2a9698 2022-09-15 mark view->ncols - 1);
8739 ec2a9698 2022-09-15 mark if (rc == ERR)
8740 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8741 ec2a9698 2022-09-15 mark if (getcurx(view->window) < view->ncols - 6) {
8742 ec2a9698 2022-09-15 mark rc = wprintw(view->window, "[%.0f%%]",
8743 ec2a9698 2022-09-15 mark 100.00 * s->last_displayed_line / s->nlines);
8744 ec2a9698 2022-09-15 mark if (rc == ERR)
8745 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "wprintw");
8746 ec2a9698 2022-09-15 mark }
8747 ec2a9698 2022-09-15 mark wstandend(view->window);
8748 ec2a9698 2022-09-15 mark }
8749 ec2a9698 2022-09-15 mark
8750 ec2a9698 2022-09-15 mark return NULL;
8751 ec2a9698 2022-09-15 mark }
8752 ec2a9698 2022-09-15 mark
8753 ec2a9698 2022-09-15 mark static const struct got_error *
8754 ec2a9698 2022-09-15 mark input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8755 ec2a9698 2022-09-15 mark {
8756 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8757 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8758 ec2a9698 2022-09-15 mark char *line = NULL;
8759 ec2a9698 2022-09-15 mark ssize_t linelen;
8760 ec2a9698 2022-09-15 mark size_t linesz = 0;
8761 ec2a9698 2022-09-15 mark int eos, nscroll;
8762 ec2a9698 2022-09-15 mark
8763 ec2a9698 2022-09-15 mark eos = nscroll = view->nlines;
8764 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8765 ec2a9698 2022-09-15 mark --eos; /* border */
8766 ec2a9698 2022-09-15 mark
8767 ec2a9698 2022-09-15 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
8768 ec2a9698 2022-09-15 mark
8769 ec2a9698 2022-09-15 mark switch (ch) {
8770 ec2a9698 2022-09-15 mark case '0':
8771 ec2a9698 2022-09-15 mark view->x = 0;
8772 ec2a9698 2022-09-15 mark break;
8773 ec2a9698 2022-09-15 mark case '$':
8774 ec2a9698 2022-09-15 mark view->x = MAX(view->maxx - view->ncols / 3, 0);
8775 ec2a9698 2022-09-15 mark view->count = 0;
8776 ec2a9698 2022-09-15 mark break;
8777 ec2a9698 2022-09-15 mark case KEY_RIGHT:
8778 ec2a9698 2022-09-15 mark case 'l':
8779 ec2a9698 2022-09-15 mark if (view->x + view->ncols / 3 < view->maxx)
8780 ec2a9698 2022-09-15 mark view->x += 2;
8781 ec2a9698 2022-09-15 mark else
8782 ec2a9698 2022-09-15 mark view->count = 0;
8783 ec2a9698 2022-09-15 mark break;
8784 ec2a9698 2022-09-15 mark case KEY_LEFT:
8785 ec2a9698 2022-09-15 mark case 'h':
8786 ec2a9698 2022-09-15 mark view->x -= MIN(view->x, 2);
8787 ec2a9698 2022-09-15 mark if (view->x <= 0)
8788 ec2a9698 2022-09-15 mark view->count = 0;
8789 ec2a9698 2022-09-15 mark break;
8790 ec2a9698 2022-09-15 mark case 'g':
8791 ec2a9698 2022-09-15 mark case KEY_HOME:
8792 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8793 ec2a9698 2022-09-15 mark view->count = 0;
8794 ec2a9698 2022-09-15 mark break;
8795 ec2a9698 2022-09-15 mark case 'G':
8796 ec2a9698 2022-09-15 mark case KEY_END:
8797 ec2a9698 2022-09-15 mark view->count = 0;
8798 ec2a9698 2022-09-15 mark if (s->eof)
8799 ec2a9698 2022-09-15 mark break;
8800 ec2a9698 2022-09-15 mark s->first_displayed_line = (s->nlines - eos) + 3;
8801 ec2a9698 2022-09-15 mark s->eof = 1;
8802 ec2a9698 2022-09-15 mark break;
8803 ec2a9698 2022-09-15 mark case 'k':
8804 ec2a9698 2022-09-15 mark case KEY_UP:
8805 ec2a9698 2022-09-15 mark if (s->first_displayed_line > 1)
8806 ec2a9698 2022-09-15 mark --s->first_displayed_line;
8807 ec2a9698 2022-09-15 mark else
8808 ec2a9698 2022-09-15 mark view->count = 0;
8809 ec2a9698 2022-09-15 mark break;
8810 ec2a9698 2022-09-15 mark case CTRL('u'):
8811 ec2a9698 2022-09-15 mark case 'u':
8812 ec2a9698 2022-09-15 mark nscroll /= 2;
8813 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8814 ec2a9698 2022-09-15 mark case KEY_PPAGE:
8815 ec2a9698 2022-09-15 mark case CTRL('b'):
8816 ec2a9698 2022-09-15 mark case 'b':
8817 ec2a9698 2022-09-15 mark if (s->first_displayed_line == 1) {
8818 ec2a9698 2022-09-15 mark view->count = 0;
8819 ec2a9698 2022-09-15 mark break;
8820 ec2a9698 2022-09-15 mark }
8821 ec2a9698 2022-09-15 mark while (--nscroll > 0 && s->first_displayed_line > 1)
8822 ec2a9698 2022-09-15 mark s->first_displayed_line--;
8823 ec2a9698 2022-09-15 mark break;
8824 ec2a9698 2022-09-15 mark case 'j':
8825 ec2a9698 2022-09-15 mark case KEY_DOWN:
8826 ec2a9698 2022-09-15 mark case CTRL('n'):
8827 ec2a9698 2022-09-15 mark if (!s->eof)
8828 ec2a9698 2022-09-15 mark ++s->first_displayed_line;
8829 ec2a9698 2022-09-15 mark else
8830 ec2a9698 2022-09-15 mark view->count = 0;
8831 ec2a9698 2022-09-15 mark break;
8832 ec2a9698 2022-09-15 mark case CTRL('d'):
8833 ec2a9698 2022-09-15 mark case 'd':
8834 ec2a9698 2022-09-15 mark nscroll /= 2;
8835 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8836 ec2a9698 2022-09-15 mark case KEY_NPAGE:
8837 ec2a9698 2022-09-15 mark case CTRL('f'):
8838 ec2a9698 2022-09-15 mark case 'f':
8839 ec2a9698 2022-09-15 mark case ' ':
8840 ec2a9698 2022-09-15 mark if (s->eof) {
8841 ec2a9698 2022-09-15 mark view->count = 0;
8842 ec2a9698 2022-09-15 mark break;
8843 ec2a9698 2022-09-15 mark }
8844 ec2a9698 2022-09-15 mark while (!s->eof && --nscroll > 0) {
8845 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8846 ec2a9698 2022-09-15 mark s->first_displayed_line++;
8847 ec2a9698 2022-09-15 mark if (linelen == -1) {
8848 ec2a9698 2022-09-15 mark if (feof(s->f))
8849 ec2a9698 2022-09-15 mark s->eof = 1;
8850 ec2a9698 2022-09-15 mark else
8851 ec2a9698 2022-09-15 mark err = got_ferror(s->f, GOT_ERR_IO);
8852 ec2a9698 2022-09-15 mark break;
8853 ec2a9698 2022-09-15 mark }
8854 ec2a9698 2022-09-15 mark }
8855 ec2a9698 2022-09-15 mark free(line);
8856 ec2a9698 2022-09-15 mark break;
8857 ec2a9698 2022-09-15 mark default:
8858 ec2a9698 2022-09-15 mark view->count = 0;
8859 ec2a9698 2022-09-15 mark break;
8860 ec2a9698 2022-09-15 mark }
8861 ec2a9698 2022-09-15 mark
8862 ec2a9698 2022-09-15 mark return err;
8863 ec2a9698 2022-09-15 mark }
8864 ec2a9698 2022-09-15 mark
8865 ec2a9698 2022-09-15 mark static const struct got_error *
8866 ec2a9698 2022-09-15 mark close_help_view(struct tog_view *view)
8867 ec2a9698 2022-09-15 mark {
8868 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8869 ec2a9698 2022-09-15 mark
8870 ec2a9698 2022-09-15 mark free(s->line_offsets);
8871 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8872 ec2a9698 2022-09-15 mark if (fclose(s->f) == EOF)
8873 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
8874 ec2a9698 2022-09-15 mark
8875 ec2a9698 2022-09-15 mark return NULL;
8876 ec2a9698 2022-09-15 mark }
8877 ec2a9698 2022-09-15 mark
8878 ec2a9698 2022-09-15 mark static const struct got_error *
8879 ec2a9698 2022-09-15 mark reset_help_view(struct tog_view *view)
8880 ec2a9698 2022-09-15 mark {
8881 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8882 ec2a9698 2022-09-15 mark
8883 ec2a9698 2022-09-15 mark
8884 ec2a9698 2022-09-15 mark if (s->f && fclose(s->f) == EOF)
8885 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
8886 ec2a9698 2022-09-15 mark
8887 ec2a9698 2022-09-15 mark wclear(view->window);
8888 ec2a9698 2022-09-15 mark view->count = 0;
8889 ec2a9698 2022-09-15 mark view->x = 0;
8890 ec2a9698 2022-09-15 mark s->all = !s->all;
8891 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8892 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
8893 ec2a9698 2022-09-15 mark s->matched_line = 0;
8894 ec2a9698 2022-09-15 mark
8895 ec2a9698 2022-09-15 mark return create_help(s);
8896 ec2a9698 2022-09-15 mark }
8897 ec2a9698 2022-09-15 mark
8898 ec2a9698 2022-09-15 mark static const struct got_error *
8899 ec2a9698 2022-09-15 mark open_help_view(struct tog_view *view, struct tog_view *parent)
8900 ec2a9698 2022-09-15 mark {
8901 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8902 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8903 ec2a9698 2022-09-15 mark
8904 ec2a9698 2022-09-15 mark s->type = (enum tog_keymap_type)parent->type;
8905 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8906 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
8907 ec2a9698 2022-09-15 mark s->selected_line = 1;
8908 ec2a9698 2022-09-15 mark
8909 ec2a9698 2022-09-15 mark view->show = show_help_view;
8910 ec2a9698 2022-09-15 mark view->input = input_help_view;
8911 ec2a9698 2022-09-15 mark view->reset = reset_help_view;
8912 ec2a9698 2022-09-15 mark view->close = close_help_view;
8913 ec2a9698 2022-09-15 mark view->search_start = search_start_help_view;
8914 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_help_view;
8915 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
8916 ec2a9698 2022-09-15 mark
8917 ec2a9698 2022-09-15 mark err = create_help(s);
8918 ec2a9698 2022-09-15 mark return err;
8919 ec2a9698 2022-09-15 mark }
8920 ec2a9698 2022-09-15 mark
8921 ec2a9698 2022-09-15 mark static const struct got_error *
8922 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8923 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
8924 136e2bd4 2022-07-23 mark {
8925 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
8926 136e2bd4 2022-07-23 mark
8927 136e2bd4 2022-07-23 mark *new_view = NULL;
8928 136e2bd4 2022-07-23 mark
8929 136e2bd4 2022-07-23 mark switch (request) {
8930 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
8931 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
8932 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
8933 136e2bd4 2022-07-23 mark
8934 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
8935 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
8936 136e2bd4 2022-07-23 mark view, s->repo);
8937 136e2bd4 2022-07-23 mark } else
8938 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8939 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8940 136e2bd4 2022-07-23 mark break;
8941 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
8942 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
8943 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
8944 136e2bd4 2022-07-23 mark
8945 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
8946 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
8947 136e2bd4 2022-07-23 mark s->repo);
8948 136e2bd4 2022-07-23 mark } else
8949 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8950 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8951 136e2bd4 2022-07-23 mark break;
8952 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
8953 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
8954 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
8955 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
8956 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
8957 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
8958 136e2bd4 2022-07-23 mark &view->state.tree);
8959 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
8960 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
8961 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
8962 136e2bd4 2022-07-23 mark view->state.ref.repo);
8963 136e2bd4 2022-07-23 mark else
8964 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8965 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8966 136e2bd4 2022-07-23 mark break;
8967 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
8968 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
8969 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
8970 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
8971 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
8972 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
8973 136e2bd4 2022-07-23 mark view->state.log.repo);
8974 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
8975 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
8976 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
8977 136e2bd4 2022-07-23 mark view->state.ref.repo);
8978 136e2bd4 2022-07-23 mark else
8979 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8980 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8981 136e2bd4 2022-07-23 mark break;
8982 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
8983 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8984 136e2bd4 2022-07-23 mark if (*new_view == NULL)
8985 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
8986 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
8987 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
8988 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
8989 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
8990 136e2bd4 2022-07-23 mark else
8991 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
8992 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8993 136e2bd4 2022-07-23 mark if (err)
8994 136e2bd4 2022-07-23 mark view_close(*new_view);
8995 136e2bd4 2022-07-23 mark break;
8996 ec2a9698 2022-09-15 mark case TOG_VIEW_HELP:
8997 94274a8f 2022-09-19 mark *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
8998 ec2a9698 2022-09-15 mark if (*new_view == NULL)
8999 ec2a9698 2022-09-15 mark return got_error_from_errno("view_open");
9000 ec2a9698 2022-09-15 mark err = open_help_view(*new_view, view);
9001 ec2a9698 2022-09-15 mark if (err)
9002 ec2a9698 2022-09-15 mark view_close(*new_view);
9003 ec2a9698 2022-09-15 mark break;
9004 136e2bd4 2022-07-23 mark default:
9005 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9006 136e2bd4 2022-07-23 mark }
9007 136e2bd4 2022-07-23 mark
9008 136e2bd4 2022-07-23 mark return err;
9009 136e2bd4 2022-07-23 mark }
9010 136e2bd4 2022-07-23 mark
9011 9b058f45 2022-06-30 mark /*
9012 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
9013 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
9014 9b058f45 2022-06-30 mark */
9015 6458efa5 2020-11-24 stsp static void
9016 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
9017 9b058f45 2022-06-30 mark {
9018 9b058f45 2022-06-30 mark switch (view->type) {
9019 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9020 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9021 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
9022 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
9023 9b058f45 2022-06-30 mark 1);
9024 9b058f45 2022-06-30 mark break;
9025 9b058f45 2022-06-30 mark }
9026 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
9027 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
9028 9b058f45 2022-06-30 mark else
9029 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
9030 9b058f45 2022-06-30 mark s->selected_line += view->offset;
9031 9b058f45 2022-06-30 mark break;
9032 9b058f45 2022-06-30 mark }
9033 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
9034 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
9035 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
9036 9b058f45 2022-06-30 mark break;
9037 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
9038 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
9039 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
9040 9b058f45 2022-06-30 mark break;
9041 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
9042 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
9043 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
9044 9b058f45 2022-06-30 mark break;
9045 9b058f45 2022-06-30 mark default:
9046 9b058f45 2022-06-30 mark break;
9047 9b058f45 2022-06-30 mark }
9048 9b058f45 2022-06-30 mark
9049 9b058f45 2022-06-30 mark view->offset = 0;
9050 9b058f45 2022-06-30 mark }
9051 9b058f45 2022-06-30 mark
9052 9b058f45 2022-06-30 mark /*
9053 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
9054 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
9055 9b058f45 2022-06-30 mark */
9056 9b058f45 2022-06-30 mark static const struct got_error *
9057 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
9058 9b058f45 2022-06-30 mark {
9059 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
9060 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
9061 9b058f45 2022-06-30 mark int *selected = NULL;
9062 9b058f45 2022-06-30 mark int header, offset;
9063 9b058f45 2022-06-30 mark
9064 9b058f45 2022-06-30 mark switch (view->type) {
9065 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9066 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9067 9b058f45 2022-06-30 mark header = 3;
9068 9b058f45 2022-06-30 mark scrolld = NULL;
9069 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
9070 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
9071 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
9072 9b058f45 2022-06-30 mark s->selected_line -= offset;
9073 9b058f45 2022-06-30 mark view->offset = offset;
9074 9b058f45 2022-06-30 mark }
9075 9b058f45 2022-06-30 mark break;
9076 9b058f45 2022-06-30 mark }
9077 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
9078 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
9079 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
9080 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
9081 9b058f45 2022-06-30 mark selected = &s->selected;
9082 9b058f45 2022-06-30 mark break;
9083 9b058f45 2022-06-30 mark }
9084 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
9085 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
9086 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
9087 9b058f45 2022-06-30 mark header = 3;
9088 9b058f45 2022-06-30 mark selected = &s->selected;
9089 9b058f45 2022-06-30 mark break;
9090 9b058f45 2022-06-30 mark }
9091 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
9092 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
9093 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
9094 9b058f45 2022-06-30 mark header = 5;
9095 9b058f45 2022-06-30 mark selected = &s->selected;
9096 9b058f45 2022-06-30 mark break;
9097 9b058f45 2022-06-30 mark }
9098 9b058f45 2022-06-30 mark default:
9099 9b058f45 2022-06-30 mark selected = NULL;
9100 9b058f45 2022-06-30 mark scrolld = NULL;
9101 9b058f45 2022-06-30 mark header = 0;
9102 9b058f45 2022-06-30 mark break;
9103 9b058f45 2022-06-30 mark }
9104 9b058f45 2022-06-30 mark
9105 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
9106 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
9107 9b058f45 2022-06-30 mark view->offset = offset;
9108 9b058f45 2022-06-30 mark if (scrolld && offset) {
9109 9b058f45 2022-06-30 mark err = scrolld(view, offset);
9110 9b058f45 2022-06-30 mark *selected -= offset;
9111 9b058f45 2022-06-30 mark }
9112 9b058f45 2022-06-30 mark }
9113 9b058f45 2022-06-30 mark
9114 9b058f45 2022-06-30 mark return err;
9115 9b058f45 2022-06-30 mark }
9116 9b058f45 2022-06-30 mark
9117 9b058f45 2022-06-30 mark static void
9118 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9119 ce5b7c56 2019-07-09 stsp {
9120 6059809a 2020-12-17 stsp size_t i;
9121 9f7d7167 2018-04-29 stsp
9122 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9123 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9124 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
9125 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9126 ce5b7c56 2019-07-09 stsp }
9127 6879ba42 2020-10-01 naddy fputc('\n', fp);
9128 ce5b7c56 2019-07-09 stsp }
9129 ce5b7c56 2019-07-09 stsp
9130 4ed7e80c 2018-05-20 stsp __dead static void
9131 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9132 9f7d7167 2018-04-29 stsp {
9133 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9134 6879ba42 2020-10-01 naddy
9135 6a0a1bd4 2022-10-04 op fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9136 6879ba42 2020-10-01 naddy getprogname());
9137 6879ba42 2020-10-01 naddy if (hflag) {
9138 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9139 6879ba42 2020-10-01 naddy list_commands(fp);
9140 ee85c5e8 2020-02-29 stsp }
9141 6879ba42 2020-10-01 naddy exit(status);
9142 9f7d7167 2018-04-29 stsp }
9143 9f7d7167 2018-04-29 stsp
9144 c2301be8 2018-04-30 stsp static char **
9145 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9146 c2301be8 2018-04-30 stsp {
9147 ee85c5e8 2020-02-29 stsp va_list ap;
9148 c2301be8 2018-04-30 stsp char **argv;
9149 ee85c5e8 2020-02-29 stsp int i;
9150 c2301be8 2018-04-30 stsp
9151 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9152 ee85c5e8 2020-02-29 stsp
9153 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9154 c2301be8 2018-04-30 stsp if (argv == NULL)
9155 c2301be8 2018-04-30 stsp err(1, "calloc");
9156 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9157 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9158 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9159 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9160 c2301be8 2018-04-30 stsp }
9161 c2301be8 2018-04-30 stsp
9162 ee85c5e8 2020-02-29 stsp va_end(ap);
9163 c2301be8 2018-04-30 stsp return argv;
9164 ee85c5e8 2020-02-29 stsp }
9165 ee85c5e8 2020-02-29 stsp
9166 ee85c5e8 2020-02-29 stsp /*
9167 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9168 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9169 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9170 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9171 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9172 ee85c5e8 2020-02-29 stsp */
9173 ee85c5e8 2020-02-29 stsp static const struct got_error *
9174 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9175 ee85c5e8 2020-02-29 stsp {
9176 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9177 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9178 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9179 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9180 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9181 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
9182 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9183 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9184 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
9185 84de9106 2020-12-26 stsp
9186 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9187 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9188 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9189 ee85c5e8 2020-02-29 stsp
9190 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
9191 0ae84acc 2022-06-15 tracey if (error != NULL)
9192 0ae84acc 2022-06-15 tracey goto done;
9193 0ae84acc 2022-06-15 tracey
9194 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9195 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9196 ee85c5e8 2020-02-29 stsp goto done;
9197 ee85c5e8 2020-02-29 stsp
9198 ee85c5e8 2020-02-29 stsp if (worktree)
9199 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9200 ee85c5e8 2020-02-29 stsp else
9201 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9202 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9203 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9204 ee85c5e8 2020-02-29 stsp goto done;
9205 ee85c5e8 2020-02-29 stsp }
9206 ee85c5e8 2020-02-29 stsp
9207 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9208 ee85c5e8 2020-02-29 stsp if (error != NULL)
9209 ee85c5e8 2020-02-29 stsp goto done;
9210 ee85c5e8 2020-02-29 stsp
9211 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9212 ee85c5e8 2020-02-29 stsp repo, worktree);
9213 ee85c5e8 2020-02-29 stsp if (error)
9214 ee85c5e8 2020-02-29 stsp goto done;
9215 ee85c5e8 2020-02-29 stsp
9216 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
9217 84de9106 2020-12-26 stsp if (error)
9218 84de9106 2020-12-26 stsp goto done;
9219 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9220 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9221 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9222 ee85c5e8 2020-02-29 stsp if (error)
9223 ee85c5e8 2020-02-29 stsp goto done;
9224 ee85c5e8 2020-02-29 stsp
9225 ee85c5e8 2020-02-29 stsp if (worktree) {
9226 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9227 ee85c5e8 2020-02-29 stsp worktree = NULL;
9228 ee85c5e8 2020-02-29 stsp }
9229 ee85c5e8 2020-02-29 stsp
9230 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9231 a44927cc 2022-04-07 stsp if (error)
9232 a44927cc 2022-04-07 stsp goto done;
9233 a44927cc 2022-04-07 stsp
9234 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9235 ee85c5e8 2020-02-29 stsp if (error) {
9236 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9237 ee85c5e8 2020-02-29 stsp goto done;
9238 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9239 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9240 6879ba42 2020-10-01 naddy usage(1, 1);
9241 ee85c5e8 2020-02-29 stsp /* not reached */
9242 ee85c5e8 2020-02-29 stsp }
9243 ee85c5e8 2020-02-29 stsp
9244 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9245 ee85c5e8 2020-02-29 stsp if (error)
9246 ee85c5e8 2020-02-29 stsp goto done;
9247 ee85c5e8 2020-02-29 stsp
9248 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9249 ee85c5e8 2020-02-29 stsp argc = 4;
9250 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9251 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9252 ee85c5e8 2020-02-29 stsp done:
9253 1d0f4054 2021-06-17 stsp if (repo) {
9254 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9255 1d0f4054 2021-06-17 stsp if (error == NULL)
9256 1d0f4054 2021-06-17 stsp error = close_err;
9257 1d0f4054 2021-06-17 stsp }
9258 a44927cc 2022-04-07 stsp if (commit)
9259 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
9260 ee85c5e8 2020-02-29 stsp if (worktree)
9261 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9262 0ae84acc 2022-06-15 tracey if (pack_fds) {
9263 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
9264 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
9265 0ae84acc 2022-06-15 tracey if (error == NULL)
9266 0ae84acc 2022-06-15 tracey error = pack_err;
9267 0ae84acc 2022-06-15 tracey }
9268 ee85c5e8 2020-02-29 stsp free(id);
9269 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9270 ee85c5e8 2020-02-29 stsp free(commit_id);
9271 ee85c5e8 2020-02-29 stsp free(cwd);
9272 ee85c5e8 2020-02-29 stsp free(repo_path);
9273 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9274 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9275 ee85c5e8 2020-02-29 stsp int i;
9276 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9277 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9278 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9279 ee85c5e8 2020-02-29 stsp }
9280 87670572 2020-12-26 naddy tog_free_refs();
9281 ee85c5e8 2020-02-29 stsp return error;
9282 c2301be8 2018-04-30 stsp }
9283 c2301be8 2018-04-30 stsp
9284 9f7d7167 2018-04-29 stsp int
9285 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9286 9f7d7167 2018-04-29 stsp {
9287 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9288 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9289 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9290 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9291 3e166534 2022-02-16 naddy static const struct option longopts[] = {
9292 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9293 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9294 83cd27f8 2020-01-13 stsp };
9295 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
9296 9f7d7167 2018-04-29 stsp
9297 3264c09c 2022-09-06 mark if (!isatty(STDIN_FILENO))
9298 3264c09c 2022-09-06 mark errx(1, "standard input is not a tty");
9299 3264c09c 2022-09-06 mark
9300 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
9301 9f7d7167 2018-04-29 stsp
9302 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9303 9f7d7167 2018-04-29 stsp switch (ch) {
9304 9f7d7167 2018-04-29 stsp case 'h':
9305 9f7d7167 2018-04-29 stsp hflag = 1;
9306 9f7d7167 2018-04-29 stsp break;
9307 53ccebc2 2019-07-30 stsp case 'V':
9308 53ccebc2 2019-07-30 stsp Vflag = 1;
9309 53ccebc2 2019-07-30 stsp break;
9310 9f7d7167 2018-04-29 stsp default:
9311 6879ba42 2020-10-01 naddy usage(hflag, 1);
9312 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9313 9f7d7167 2018-04-29 stsp }
9314 9f7d7167 2018-04-29 stsp }
9315 9f7d7167 2018-04-29 stsp
9316 9f7d7167 2018-04-29 stsp argc -= optind;
9317 9f7d7167 2018-04-29 stsp argv += optind;
9318 9814e6a3 2020-09-27 naddy optind = 1;
9319 c2301be8 2018-04-30 stsp optreset = 1;
9320 9f7d7167 2018-04-29 stsp
9321 53ccebc2 2019-07-30 stsp if (Vflag) {
9322 53ccebc2 2019-07-30 stsp got_version_print_str();
9323 6879ba42 2020-10-01 naddy return 0;
9324 53ccebc2 2019-07-30 stsp }
9325 4010e238 2020-12-04 stsp
9326 4010e238 2020-12-04 stsp #ifndef PROFILE
9327 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9328 4010e238 2020-12-04 stsp NULL) == -1)
9329 4010e238 2020-12-04 stsp err(1, "pledge");
9330 4010e238 2020-12-04 stsp #endif
9331 53ccebc2 2019-07-30 stsp
9332 c2301be8 2018-04-30 stsp if (argc == 0) {
9333 f29d3e89 2018-06-23 stsp if (hflag)
9334 6879ba42 2020-10-01 naddy usage(hflag, 0);
9335 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9336 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9337 c2301be8 2018-04-30 stsp argc = 1;
9338 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9339 c2301be8 2018-04-30 stsp } else {
9340 6059809a 2020-12-17 stsp size_t i;
9341 9f7d7167 2018-04-29 stsp
9342 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9343 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9344 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9345 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9346 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9347 9f7d7167 2018-04-29 stsp break;
9348 9f7d7167 2018-04-29 stsp }
9349 9f7d7167 2018-04-29 stsp }
9350 ee85c5e8 2020-02-29 stsp }
9351 3642c4c6 2019-07-09 stsp
9352 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9353 917d79a7 2022-07-01 stsp if (diff_algo_str) {
9354 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
9355 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9356 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
9357 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9358 917d79a7 2022-07-01 stsp }
9359 917d79a7 2022-07-01 stsp
9360 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9361 ee85c5e8 2020-02-29 stsp if (argc != 1)
9362 6879ba42 2020-10-01 naddy usage(0, 1);
9363 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9364 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9365 ee85c5e8 2020-02-29 stsp } else {
9366 ee85c5e8 2020-02-29 stsp if (hflag)
9367 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9368 ee85c5e8 2020-02-29 stsp else
9369 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9370 9f7d7167 2018-04-29 stsp }
9371 9f7d7167 2018-04-29 stsp
9372 9f7d7167 2018-04-29 stsp endwin();
9373 b46c1e04 2020-09-20 naddy putchar('\n');
9374 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9375 a2f4a359 2020-02-28 stsp int i;
9376 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9377 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9378 a2f4a359 2020-02-28 stsp free(cmd_argv);
9379 a2f4a359 2020-02-28 stsp }
9380 a2f4a359 2020-02-28 stsp
9381 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
9382 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9383 9f7d7167 2018-04-29 stsp return 0;
9384 9f7d7167 2018-04-29 stsp }