Blame


1 069bbb86 2022-03-07 thomas /*
2 069bbb86 2022-03-07 thomas * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 069bbb86 2022-03-07 thomas *
4 069bbb86 2022-03-07 thomas * Permission to use, copy, modify, and distribute this software for any
5 069bbb86 2022-03-07 thomas * purpose with or without fee is hereby granted, provided that the above
6 069bbb86 2022-03-07 thomas * copyright notice and this permission notice appear in all copies.
7 069bbb86 2022-03-07 thomas *
8 069bbb86 2022-03-07 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 069bbb86 2022-03-07 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 069bbb86 2022-03-07 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 069bbb86 2022-03-07 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 069bbb86 2022-03-07 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 069bbb86 2022-03-07 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 069bbb86 2022-03-07 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 069bbb86 2022-03-07 thomas *
16 069bbb86 2022-03-07 thomas * Apply patches.
17 069bbb86 2022-03-07 thomas *
18 069bbb86 2022-03-07 thomas * Things that we may want to support:
19 069bbb86 2022-03-07 thomas * + support indented patches?
20 069bbb86 2022-03-07 thomas * + support other kinds of patches?
21 069bbb86 2022-03-07 thomas */
22 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
23 069bbb86 2022-03-07 thomas
24 069bbb86 2022-03-07 thomas #include <sys/types.h>
25 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
26 069bbb86 2022-03-07 thomas #include <sys/socket.h>
27 7dd42450 2022-03-13 thomas #include <sys/stat.h>
28 069bbb86 2022-03-07 thomas #include <sys/uio.h>
29 069bbb86 2022-03-07 thomas
30 bb90ca7b 2022-07-03 thomas #include <ctype.h>
31 42d9d68e 2022-03-13 thomas #include <errno.h>
32 069bbb86 2022-03-07 thomas #include <limits.h>
33 069bbb86 2022-03-07 thomas #include <stdint.h>
34 069bbb86 2022-03-07 thomas #include <stdio.h>
35 069bbb86 2022-03-07 thomas #include <stdlib.h>
36 069bbb86 2022-03-07 thomas #include <string.h>
37 069bbb86 2022-03-07 thomas #include <unistd.h>
38 069bbb86 2022-03-07 thomas
39 069bbb86 2022-03-07 thomas #include "got_error.h"
40 069bbb86 2022-03-07 thomas #include "got_object.h"
41 069bbb86 2022-03-07 thomas #include "got_path.h"
42 069bbb86 2022-03-07 thomas #include "got_reference.h"
43 069bbb86 2022-03-07 thomas #include "got_cancel.h"
44 069bbb86 2022-03-07 thomas #include "got_worktree.h"
45 0f76ab83 2022-06-23 thomas #include "got_repository.h"
46 069bbb86 2022-03-07 thomas #include "got_opentemp.h"
47 069bbb86 2022-03-07 thomas #include "got_patch.h"
48 25ec7006 2022-07-01 thomas #include "got_diff.h"
49 069bbb86 2022-03-07 thomas
50 069bbb86 2022-03-07 thomas #include "got_lib_delta.h"
51 0f76ab83 2022-06-23 thomas #include "got_lib_diff.h"
52 069bbb86 2022-03-07 thomas #include "got_lib_object.h"
53 069bbb86 2022-03-07 thomas #include "got_lib_privsep.h"
54 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
55 069bbb86 2022-03-07 thomas
56 5c23a559 2023-03-03 thomas #ifndef MIN
57 069bbb86 2022-03-07 thomas #define MIN(a, b) ((a) < (b) ? (a) : (b))
58 5c23a559 2023-03-03 thomas #endif
59 069bbb86 2022-03-07 thomas
60 069bbb86 2022-03-07 thomas struct got_patch_hunk {
61 069bbb86 2022-03-07 thomas STAILQ_ENTRY(got_patch_hunk) entries;
62 49114f01 2022-03-22 thomas const struct got_error *err;
63 bb90ca7b 2022-07-03 thomas int ws_mangled;
64 8ebb3daa 2022-06-23 thomas int offset;
65 656c2baa 2022-04-23 thomas int old_nonl;
66 656c2baa 2022-04-23 thomas int new_nonl;
67 8ebb3daa 2022-06-23 thomas int old_from;
68 8ebb3daa 2022-06-23 thomas int old_lines;
69 8ebb3daa 2022-06-23 thomas int new_from;
70 8ebb3daa 2022-06-23 thomas int new_lines;
71 069bbb86 2022-03-07 thomas size_t len;
72 069bbb86 2022-03-07 thomas size_t cap;
73 069bbb86 2022-03-07 thomas char **lines;
74 069bbb86 2022-03-07 thomas };
75 069bbb86 2022-03-07 thomas
76 49114f01 2022-03-22 thomas STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
77 069bbb86 2022-03-07 thomas struct got_patch {
78 37e766f4 2022-09-21 thomas int xbit;
79 069bbb86 2022-03-07 thomas char *old;
80 069bbb86 2022-03-07 thomas char *new;
81 016bfe4b 2022-06-23 thomas char cid[41];
82 0f76ab83 2022-06-23 thomas char blob[41];
83 49114f01 2022-03-22 thomas struct got_patch_hunk_head head;
84 c71da4f7 2022-03-22 thomas };
85 c71da4f7 2022-03-22 thomas
86 c71da4f7 2022-03-22 thomas struct patch_args {
87 c71da4f7 2022-03-22 thomas got_patch_progress_cb progress_cb;
88 c71da4f7 2022-03-22 thomas void *progress_arg;
89 49114f01 2022-03-22 thomas struct got_patch_hunk_head *head;
90 069bbb86 2022-03-07 thomas };
91 a2c162eb 2022-10-30 thomas
92 a2c162eb 2022-10-30 thomas static mode_t
93 a2c162eb 2022-10-30 thomas apply_umask(mode_t mode)
94 a2c162eb 2022-10-30 thomas {
95 a2c162eb 2022-10-30 thomas mode_t um;
96 a2c162eb 2022-10-30 thomas
97 a2c162eb 2022-10-30 thomas um = umask(000);
98 a2c162eb 2022-10-30 thomas umask(um);
99 a2c162eb 2022-10-30 thomas return mode & ~um;
100 a2c162eb 2022-10-30 thomas }
101 069bbb86 2022-03-07 thomas
102 069bbb86 2022-03-07 thomas static const struct got_error *
103 069bbb86 2022-03-07 thomas send_patch(struct imsgbuf *ibuf, int fd)
104 069bbb86 2022-03-07 thomas {
105 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
106 069bbb86 2022-03-07 thomas
107 069bbb86 2022-03-07 thomas if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
108 069bbb86 2022-03-07 thomas NULL, 0) == -1) {
109 069bbb86 2022-03-07 thomas err = got_error_from_errno(
110 069bbb86 2022-03-07 thomas "imsg_compose GOT_IMSG_PATCH_FILE");
111 069bbb86 2022-03-07 thomas close(fd);
112 069bbb86 2022-03-07 thomas return err;
113 069bbb86 2022-03-07 thomas }
114 069bbb86 2022-03-07 thomas
115 dab315a5 2022-07-07 thomas return got_privsep_flush_imsg(ibuf);
116 069bbb86 2022-03-07 thomas }
117 069bbb86 2022-03-07 thomas
118 069bbb86 2022-03-07 thomas static void
119 069bbb86 2022-03-07 thomas patch_free(struct got_patch *p)
120 069bbb86 2022-03-07 thomas {
121 069bbb86 2022-03-07 thomas struct got_patch_hunk *h;
122 069bbb86 2022-03-07 thomas size_t i;
123 069bbb86 2022-03-07 thomas
124 069bbb86 2022-03-07 thomas while (!STAILQ_EMPTY(&p->head)) {
125 069bbb86 2022-03-07 thomas h = STAILQ_FIRST(&p->head);
126 069bbb86 2022-03-07 thomas STAILQ_REMOVE_HEAD(&p->head, entries);
127 069bbb86 2022-03-07 thomas
128 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i)
129 069bbb86 2022-03-07 thomas free(h->lines[i]);
130 069bbb86 2022-03-07 thomas free(h->lines);
131 069bbb86 2022-03-07 thomas free(h);
132 069bbb86 2022-03-07 thomas }
133 069bbb86 2022-03-07 thomas
134 069bbb86 2022-03-07 thomas free(p->new);
135 069bbb86 2022-03-07 thomas free(p->old);
136 9880a1dd 2022-06-23 thomas
137 9880a1dd 2022-06-23 thomas memset(p, 0, sizeof(*p));
138 9880a1dd 2022-06-23 thomas STAILQ_INIT(&p->head);
139 069bbb86 2022-03-07 thomas }
140 069bbb86 2022-03-07 thomas
141 069bbb86 2022-03-07 thomas static const struct got_error *
142 069bbb86 2022-03-07 thomas pushline(struct got_patch_hunk *h, const char *line)
143 069bbb86 2022-03-07 thomas {
144 069bbb86 2022-03-07 thomas void *t;
145 069bbb86 2022-03-07 thomas size_t newcap;
146 069bbb86 2022-03-07 thomas
147 069bbb86 2022-03-07 thomas if (h->len == h->cap) {
148 069bbb86 2022-03-07 thomas if ((newcap = h->cap * 1.5) == 0)
149 069bbb86 2022-03-07 thomas newcap = 16;
150 069bbb86 2022-03-07 thomas t = recallocarray(h->lines, h->cap, newcap,
151 069bbb86 2022-03-07 thomas sizeof(h->lines[0]));
152 069bbb86 2022-03-07 thomas if (t == NULL)
153 069bbb86 2022-03-07 thomas return got_error_from_errno("recallocarray");
154 069bbb86 2022-03-07 thomas h->lines = t;
155 069bbb86 2022-03-07 thomas h->cap = newcap;
156 069bbb86 2022-03-07 thomas }
157 069bbb86 2022-03-07 thomas
158 069bbb86 2022-03-07 thomas if ((t = strdup(line)) == NULL)
159 069bbb86 2022-03-07 thomas return got_error_from_errno("strdup");
160 069bbb86 2022-03-07 thomas
161 069bbb86 2022-03-07 thomas h->lines[h->len++] = t;
162 069bbb86 2022-03-07 thomas return NULL;
163 069bbb86 2022-03-07 thomas }
164 069bbb86 2022-03-07 thomas
165 069bbb86 2022-03-07 thomas static const struct got_error *
166 d9db2ff9 2022-04-16 thomas recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
167 069bbb86 2022-03-07 thomas {
168 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
169 069bbb86 2022-03-07 thomas struct imsg imsg;
170 069bbb86 2022-03-07 thomas struct got_imsg_patch_hunk hdr;
171 069bbb86 2022-03-07 thomas struct got_imsg_patch patch;
172 069bbb86 2022-03-07 thomas struct got_patch_hunk *h = NULL;
173 069bbb86 2022-03-07 thomas size_t datalen;
174 656c2baa 2022-04-23 thomas int lastmode = -1;
175 069bbb86 2022-03-07 thomas
176 069bbb86 2022-03-07 thomas memset(p, 0, sizeof(*p));
177 069bbb86 2022-03-07 thomas STAILQ_INIT(&p->head);
178 069bbb86 2022-03-07 thomas
179 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, ibuf, 0);
180 069bbb86 2022-03-07 thomas if (err)
181 069bbb86 2022-03-07 thomas return err;
182 069bbb86 2022-03-07 thomas if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
183 069bbb86 2022-03-07 thomas *done = 1;
184 069bbb86 2022-03-07 thomas goto done;
185 069bbb86 2022-03-07 thomas }
186 069bbb86 2022-03-07 thomas if (imsg.hdr.type != GOT_IMSG_PATCH) {
187 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
188 069bbb86 2022-03-07 thomas goto done;
189 069bbb86 2022-03-07 thomas }
190 069bbb86 2022-03-07 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
191 069bbb86 2022-03-07 thomas if (datalen != sizeof(patch)) {
192 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
193 069bbb86 2022-03-07 thomas goto done;
194 069bbb86 2022-03-07 thomas }
195 069bbb86 2022-03-07 thomas memcpy(&patch, imsg.data, sizeof(patch));
196 ca357dd9 2022-06-23 thomas
197 ca357dd9 2022-06-23 thomas if (patch.old[sizeof(patch.old)-1] != '\0' ||
198 0f76ab83 2022-06-23 thomas patch.new[sizeof(patch.new)-1] != '\0' ||
199 016bfe4b 2022-06-23 thomas patch.cid[sizeof(patch.cid)-1] != '\0' ||
200 0f76ab83 2022-06-23 thomas patch.blob[sizeof(patch.blob)-1] != '\0') {
201 ca357dd9 2022-06-23 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
202 ca357dd9 2022-06-23 thomas goto done;
203 ca357dd9 2022-06-23 thomas }
204 0f76ab83 2022-06-23 thomas
205 8afec5d5 2022-07-01 thomas if (*patch.cid != '\0')
206 016bfe4b 2022-06-23 thomas strlcpy(p->cid, patch.cid, sizeof(p->cid));
207 8afec5d5 2022-07-01 thomas
208 8afec5d5 2022-07-01 thomas if (*patch.blob != '\0')
209 016bfe4b 2022-06-23 thomas strlcpy(p->blob, patch.blob, sizeof(p->blob));
210 37e766f4 2022-09-21 thomas
211 37e766f4 2022-09-21 thomas p->xbit = patch.xbit;
212 d9db2ff9 2022-04-16 thomas
213 d9db2ff9 2022-04-16 thomas /* automatically set strip=1 for git-style diffs */
214 d9db2ff9 2022-04-16 thomas if (strip == -1 && patch.git &&
215 d9db2ff9 2022-04-16 thomas (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
216 d9db2ff9 2022-04-16 thomas (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
217 d9db2ff9 2022-04-16 thomas strip = 1;
218 d9db2ff9 2022-04-16 thomas
219 d9db2ff9 2022-04-16 thomas /* prefer the new name if not /dev/null for not git-style diffs */
220 d9db2ff9 2022-04-16 thomas if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
221 d9db2ff9 2022-04-16 thomas err = got_path_strip(&p->old, patch.new, strip);
222 d9db2ff9 2022-04-16 thomas if (err)
223 d9db2ff9 2022-04-16 thomas goto done;
224 d9db2ff9 2022-04-16 thomas } else if (*patch.old != '\0') {
225 d9db2ff9 2022-04-16 thomas err = got_path_strip(&p->old, patch.old, strip);
226 d9db2ff9 2022-04-16 thomas if (err)
227 d9db2ff9 2022-04-16 thomas goto done;
228 069bbb86 2022-03-07 thomas }
229 d9db2ff9 2022-04-16 thomas
230 d9db2ff9 2022-04-16 thomas if (*patch.new != '\0') {
231 d9db2ff9 2022-04-16 thomas err = got_path_strip(&p->new, patch.new, strip);
232 d9db2ff9 2022-04-16 thomas if (err)
233 d9db2ff9 2022-04-16 thomas goto done;
234 d9db2ff9 2022-04-16 thomas }
235 d9db2ff9 2022-04-16 thomas
236 64c9e565 2022-03-13 thomas if (p->old == NULL && p->new == NULL) {
237 64c9e565 2022-03-13 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
238 64c9e565 2022-03-13 thomas goto done;
239 64c9e565 2022-03-13 thomas }
240 069bbb86 2022-03-07 thomas
241 069bbb86 2022-03-07 thomas imsg_free(&imsg);
242 069bbb86 2022-03-07 thomas
243 069bbb86 2022-03-07 thomas for (;;) {
244 069bbb86 2022-03-07 thomas char *t;
245 069bbb86 2022-03-07 thomas
246 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, ibuf, 0);
247 9880a1dd 2022-06-23 thomas if (err) {
248 9880a1dd 2022-06-23 thomas patch_free(p);
249 069bbb86 2022-03-07 thomas return err;
250 9880a1dd 2022-06-23 thomas }
251 069bbb86 2022-03-07 thomas
252 047c926f 2022-06-23 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
253 069bbb86 2022-03-07 thomas switch (imsg.hdr.type) {
254 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_DONE:
255 88c260f4 2022-05-14 thomas if (h != NULL && h->len == 0)
256 88c260f4 2022-05-14 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
257 069bbb86 2022-03-07 thomas goto done;
258 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_HUNK:
259 88c260f4 2022-05-14 thomas if (h != NULL &&
260 88c260f4 2022-05-14 thomas (h->len == 0 || h->old_nonl || h->new_nonl)) {
261 ff7f34d3 2022-03-22 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
262 ff7f34d3 2022-03-22 thomas goto done;
263 ff7f34d3 2022-03-22 thomas }
264 656c2baa 2022-04-23 thomas lastmode = -1;
265 069bbb86 2022-03-07 thomas if (datalen != sizeof(hdr)) {
266 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
267 069bbb86 2022-03-07 thomas goto done;
268 069bbb86 2022-03-07 thomas }
269 069bbb86 2022-03-07 thomas memcpy(&hdr, imsg.data, sizeof(hdr));
270 06227823 2022-06-23 thomas if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
271 06227823 2022-06-23 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
272 eb35d814 2022-06-23 thomas goto done;
273 eb35d814 2022-06-23 thomas }
274 06227823 2022-06-23 thomas if ((h = calloc(1, sizeof(*h))) == NULL) {
275 06227823 2022-06-23 thomas err = got_error_from_errno("calloc");
276 069bbb86 2022-03-07 thomas goto done;
277 069bbb86 2022-03-07 thomas }
278 069bbb86 2022-03-07 thomas h->old_from = hdr.oldfrom;
279 069bbb86 2022-03-07 thomas h->old_lines = hdr.oldlines;
280 069bbb86 2022-03-07 thomas h->new_from = hdr.newfrom;
281 069bbb86 2022-03-07 thomas h->new_lines = hdr.newlines;
282 069bbb86 2022-03-07 thomas STAILQ_INSERT_TAIL(&p->head, h, entries);
283 069bbb86 2022-03-07 thomas break;
284 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_LINE:
285 069bbb86 2022-03-07 thomas if (h == NULL) {
286 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
287 069bbb86 2022-03-07 thomas goto done;
288 069bbb86 2022-03-07 thomas }
289 069bbb86 2022-03-07 thomas t = imsg.data;
290 ff7f34d3 2022-03-22 thomas /* at least one char */
291 069bbb86 2022-03-07 thomas if (datalen < 2 || t[datalen-1] != '\0') {
292 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
293 069bbb86 2022-03-07 thomas goto done;
294 069bbb86 2022-03-07 thomas }
295 ff7f34d3 2022-03-22 thomas if (*t != ' ' && *t != '-' && *t != '+' &&
296 ff7f34d3 2022-03-22 thomas *t != '\\') {
297 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
298 069bbb86 2022-03-07 thomas goto done;
299 069bbb86 2022-03-07 thomas }
300 656c2baa 2022-04-23 thomas
301 656c2baa 2022-04-23 thomas if (*t != '\\')
302 ff7f34d3 2022-03-22 thomas err = pushline(h, t);
303 656c2baa 2022-04-23 thomas else if (lastmode == '-')
304 656c2baa 2022-04-23 thomas h->old_nonl = 1;
305 656c2baa 2022-04-23 thomas else if (lastmode == '+')
306 656c2baa 2022-04-23 thomas h->new_nonl = 1;
307 656c2baa 2022-04-23 thomas else
308 656c2baa 2022-04-23 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
309 656c2baa 2022-04-23 thomas
310 069bbb86 2022-03-07 thomas if (err)
311 069bbb86 2022-03-07 thomas goto done;
312 656c2baa 2022-04-23 thomas
313 656c2baa 2022-04-23 thomas lastmode = *t;
314 069bbb86 2022-03-07 thomas break;
315 069bbb86 2022-03-07 thomas default:
316 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
317 069bbb86 2022-03-07 thomas goto done;
318 069bbb86 2022-03-07 thomas }
319 069bbb86 2022-03-07 thomas
320 069bbb86 2022-03-07 thomas imsg_free(&imsg);
321 069bbb86 2022-03-07 thomas }
322 069bbb86 2022-03-07 thomas
323 069bbb86 2022-03-07 thomas done:
324 9880a1dd 2022-06-23 thomas if (err)
325 9880a1dd 2022-06-23 thomas patch_free(p);
326 9880a1dd 2022-06-23 thomas
327 069bbb86 2022-03-07 thomas imsg_free(&imsg);
328 069bbb86 2022-03-07 thomas return err;
329 069bbb86 2022-03-07 thomas }
330 f4602cbd 2022-07-23 thomas
331 f4602cbd 2022-07-23 thomas static void
332 f4602cbd 2022-07-23 thomas reverse_patch(struct got_patch *p)
333 f4602cbd 2022-07-23 thomas {
334 f4602cbd 2022-07-23 thomas struct got_patch_hunk *h;
335 f4602cbd 2022-07-23 thomas size_t i;
336 f4602cbd 2022-07-23 thomas int tmp;
337 f4602cbd 2022-07-23 thomas
338 f4602cbd 2022-07-23 thomas STAILQ_FOREACH(h, &p->head, entries) {
339 f4602cbd 2022-07-23 thomas tmp = h->old_from;
340 f4602cbd 2022-07-23 thomas h->old_from = h->new_from;
341 f4602cbd 2022-07-23 thomas h->new_from = tmp;
342 069bbb86 2022-03-07 thomas
343 f4602cbd 2022-07-23 thomas tmp = h->old_lines;
344 f4602cbd 2022-07-23 thomas h->old_lines = h->new_lines;
345 f4602cbd 2022-07-23 thomas h->new_lines = tmp;
346 f4602cbd 2022-07-23 thomas
347 f4602cbd 2022-07-23 thomas tmp = h->old_nonl;
348 f4602cbd 2022-07-23 thomas h->old_nonl = h->new_nonl;
349 f4602cbd 2022-07-23 thomas h->new_nonl = tmp;
350 f4602cbd 2022-07-23 thomas
351 f4602cbd 2022-07-23 thomas for (i = 0; i < h->len; ++i) {
352 f4602cbd 2022-07-23 thomas if (*h->lines[i] == '+')
353 f4602cbd 2022-07-23 thomas *h->lines[i] = '-';
354 f4602cbd 2022-07-23 thomas else if (*h->lines[i] == '-')
355 f4602cbd 2022-07-23 thomas *h->lines[i] = '+';
356 f4602cbd 2022-07-23 thomas }
357 f4602cbd 2022-07-23 thomas }
358 f4602cbd 2022-07-23 thomas }
359 f4602cbd 2022-07-23 thomas
360 069bbb86 2022-03-07 thomas /*
361 069bbb86 2022-03-07 thomas * Copy data from orig starting at copypos until pos into tmp.
362 069bbb86 2022-03-07 thomas * If pos is -1, copy until EOF.
363 069bbb86 2022-03-07 thomas */
364 069bbb86 2022-03-07 thomas static const struct got_error *
365 069bbb86 2022-03-07 thomas copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
366 069bbb86 2022-03-07 thomas {
367 069bbb86 2022-03-07 thomas char buf[BUFSIZ];
368 069bbb86 2022-03-07 thomas size_t len, r, w;
369 069bbb86 2022-03-07 thomas
370 bafaf650 2022-05-14 thomas if (fseeko(orig, copypos, SEEK_SET) == -1)
371 bafaf650 2022-05-14 thomas return got_error_from_errno("fseeko");
372 069bbb86 2022-03-07 thomas
373 069bbb86 2022-03-07 thomas while (pos == -1 || copypos < pos) {
374 069bbb86 2022-03-07 thomas len = sizeof(buf);
375 069bbb86 2022-03-07 thomas if (pos > 0)
376 069bbb86 2022-03-07 thomas len = MIN(len, (size_t)pos - copypos);
377 069bbb86 2022-03-07 thomas r = fread(buf, 1, len, orig);
378 069bbb86 2022-03-07 thomas if (r != len && ferror(orig))
379 069bbb86 2022-03-07 thomas return got_error_from_errno("fread");
380 069bbb86 2022-03-07 thomas w = fwrite(buf, 1, r, tmp);
381 069bbb86 2022-03-07 thomas if (w != r)
382 069bbb86 2022-03-07 thomas return got_error_from_errno("fwrite");
383 069bbb86 2022-03-07 thomas copypos += len;
384 069bbb86 2022-03-07 thomas if (r != len && feof(orig)) {
385 069bbb86 2022-03-07 thomas if (pos == -1)
386 069bbb86 2022-03-07 thomas return NULL;
387 49114f01 2022-03-22 thomas return got_error(GOT_ERR_HUNK_FAILED);
388 069bbb86 2022-03-07 thomas }
389 069bbb86 2022-03-07 thomas }
390 069bbb86 2022-03-07 thomas return NULL;
391 069bbb86 2022-03-07 thomas }
392 4f34ed97 2022-08-06 thomas
393 4f34ed97 2022-08-06 thomas static int linecmp(const char *, const char *, int *);
394 069bbb86 2022-03-07 thomas
395 069bbb86 2022-03-07 thomas static const struct got_error *
396 8ebb3daa 2022-06-23 thomas locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
397 069bbb86 2022-03-07 thomas {
398 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
399 069bbb86 2022-03-07 thomas char *line = NULL;
400 069bbb86 2022-03-07 thomas char mode = *h->lines[0];
401 069bbb86 2022-03-07 thomas size_t linesize = 0;
402 069bbb86 2022-03-07 thomas ssize_t linelen;
403 069bbb86 2022-03-07 thomas off_t match = -1;
404 4f34ed97 2022-08-06 thomas int mangled = 0, match_lineno = -1;
405 069bbb86 2022-03-07 thomas
406 069bbb86 2022-03-07 thomas for (;;) {
407 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
408 069bbb86 2022-03-07 thomas if (linelen == -1) {
409 069bbb86 2022-03-07 thomas if (ferror(orig))
410 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
411 069bbb86 2022-03-07 thomas else if (match == -1)
412 49114f01 2022-03-22 thomas err = got_error(GOT_ERR_HUNK_FAILED);
413 069bbb86 2022-03-07 thomas break;
414 069bbb86 2022-03-07 thomas }
415 ff7f34d3 2022-03-22 thomas if (line[linelen - 1] == '\n')
416 ff7f34d3 2022-03-22 thomas line[linelen - 1] = '\0';
417 069bbb86 2022-03-07 thomas (*lineno)++;
418 069bbb86 2022-03-07 thomas
419 4f34ed97 2022-08-06 thomas if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
420 4f34ed97 2022-08-06 thomas (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
421 069bbb86 2022-03-07 thomas (mode == '+' && *lineno == h->old_from)) {
422 069bbb86 2022-03-07 thomas match = ftello(orig);
423 069bbb86 2022-03-07 thomas if (match == -1) {
424 069bbb86 2022-03-07 thomas err = got_error_from_errno("ftello");
425 069bbb86 2022-03-07 thomas break;
426 069bbb86 2022-03-07 thomas }
427 069bbb86 2022-03-07 thomas match -= linelen;
428 069bbb86 2022-03-07 thomas match_lineno = (*lineno)-1;
429 069bbb86 2022-03-07 thomas }
430 069bbb86 2022-03-07 thomas
431 aaabf83f 2022-08-06 thomas if (*lineno >= h->old_from && match != -1) {
432 aaabf83f 2022-08-06 thomas if (mangled)
433 aaabf83f 2022-08-06 thomas h->ws_mangled = 1;
434 069bbb86 2022-03-07 thomas break;
435 aaabf83f 2022-08-06 thomas }
436 069bbb86 2022-03-07 thomas }
437 4f34ed97 2022-08-06 thomas
438 069bbb86 2022-03-07 thomas if (err == NULL) {
439 122e1611 2022-03-22 thomas *pos = match;
440 069bbb86 2022-03-07 thomas *lineno = match_lineno;
441 bafaf650 2022-05-14 thomas if (fseeko(orig, match, SEEK_SET) == -1)
442 bafaf650 2022-05-14 thomas err = got_error_from_errno("fseeko");
443 069bbb86 2022-03-07 thomas }
444 069bbb86 2022-03-07 thomas
445 069bbb86 2022-03-07 thomas free(line);
446 069bbb86 2022-03-07 thomas return err;
447 bb90ca7b 2022-07-03 thomas }
448 bb90ca7b 2022-07-03 thomas
449 bb90ca7b 2022-07-03 thomas static int
450 bb90ca7b 2022-07-03 thomas linecmp(const char *a, const char *b, int *mangled)
451 bb90ca7b 2022-07-03 thomas {
452 bb90ca7b 2022-07-03 thomas int c;
453 bb90ca7b 2022-07-03 thomas
454 bb90ca7b 2022-07-03 thomas *mangled = 0;
455 bb90ca7b 2022-07-03 thomas c = strcmp(a, b);
456 bb90ca7b 2022-07-03 thomas if (c == 0)
457 bb90ca7b 2022-07-03 thomas return c;
458 bb90ca7b 2022-07-03 thomas
459 bb90ca7b 2022-07-03 thomas *mangled = 1;
460 bb90ca7b 2022-07-03 thomas for (;;) {
461 2140c6ec 2022-07-04 thomas while (*a == '\t' || *a == ' ' || *a == '\f')
462 bb90ca7b 2022-07-03 thomas a++;
463 2140c6ec 2022-07-04 thomas while (*b == '\t' || *b == ' ' || *b == '\f')
464 bb90ca7b 2022-07-03 thomas b++;
465 2140c6ec 2022-07-04 thomas if (*a == '\0' || *a != *b)
466 bb90ca7b 2022-07-03 thomas break;
467 bb90ca7b 2022-07-03 thomas a++, b++;
468 bb90ca7b 2022-07-03 thomas }
469 bb90ca7b 2022-07-03 thomas
470 bb90ca7b 2022-07-03 thomas return *a - *b;
471 069bbb86 2022-03-07 thomas }
472 069bbb86 2022-03-07 thomas
473 069bbb86 2022-03-07 thomas static const struct got_error *
474 069bbb86 2022-03-07 thomas test_hunk(FILE *orig, struct got_patch_hunk *h)
475 069bbb86 2022-03-07 thomas {
476 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
477 069bbb86 2022-03-07 thomas char *line = NULL;
478 069bbb86 2022-03-07 thomas size_t linesize = 0, i = 0;
479 069bbb86 2022-03-07 thomas ssize_t linelen;
480 bb90ca7b 2022-07-03 thomas int mangled;
481 069bbb86 2022-03-07 thomas
482 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i) {
483 069bbb86 2022-03-07 thomas switch (*h->lines[i]) {
484 069bbb86 2022-03-07 thomas case '+':
485 069bbb86 2022-03-07 thomas continue;
486 069bbb86 2022-03-07 thomas case ' ':
487 069bbb86 2022-03-07 thomas case '-':
488 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
489 069bbb86 2022-03-07 thomas if (linelen == -1) {
490 069bbb86 2022-03-07 thomas if (ferror(orig))
491 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
492 069bbb86 2022-03-07 thomas else
493 069bbb86 2022-03-07 thomas err = got_error(
494 49114f01 2022-03-22 thomas GOT_ERR_HUNK_FAILED);
495 069bbb86 2022-03-07 thomas goto done;
496 069bbb86 2022-03-07 thomas }
497 ff7f34d3 2022-03-22 thomas if (line[linelen - 1] == '\n')
498 ff7f34d3 2022-03-22 thomas line[linelen - 1] = '\0';
499 bb90ca7b 2022-07-03 thomas if (linecmp(h->lines[i] + 1, line, &mangled)) {
500 49114f01 2022-03-22 thomas err = got_error(GOT_ERR_HUNK_FAILED);
501 069bbb86 2022-03-07 thomas goto done;
502 069bbb86 2022-03-07 thomas }
503 bb90ca7b 2022-07-03 thomas if (mangled)
504 bb90ca7b 2022-07-03 thomas h->ws_mangled = 1;
505 069bbb86 2022-03-07 thomas break;
506 069bbb86 2022-03-07 thomas }
507 069bbb86 2022-03-07 thomas }
508 069bbb86 2022-03-07 thomas
509 069bbb86 2022-03-07 thomas done:
510 069bbb86 2022-03-07 thomas free(line);
511 069bbb86 2022-03-07 thomas return err;
512 069bbb86 2022-03-07 thomas }
513 069bbb86 2022-03-07 thomas
514 069bbb86 2022-03-07 thomas static const struct got_error *
515 bb90ca7b 2022-07-03 thomas apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
516 bb90ca7b 2022-07-03 thomas off_t from)
517 069bbb86 2022-03-07 thomas {
518 bb90ca7b 2022-07-03 thomas const struct got_error *err = NULL;
519 bb90ca7b 2022-07-03 thomas const char *t;
520 bb90ca7b 2022-07-03 thomas size_t linesize = 0, i, new = 0;
521 bb90ca7b 2022-07-03 thomas char *line = NULL;
522 bb90ca7b 2022-07-03 thomas char mode;
523 bb90ca7b 2022-07-03 thomas ssize_t linelen;
524 069bbb86 2022-03-07 thomas
525 bb90ca7b 2022-07-03 thomas if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
526 bb90ca7b 2022-07-03 thomas return got_error_from_errno("fseeko");
527 bb90ca7b 2022-07-03 thomas
528 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i) {
529 bb90ca7b 2022-07-03 thomas switch (mode = *h->lines[i]) {
530 069bbb86 2022-03-07 thomas case '-':
531 bb90ca7b 2022-07-03 thomas case ' ':
532 069bbb86 2022-03-07 thomas (*lineno)++;
533 bb90ca7b 2022-07-03 thomas if (orig != NULL) {
534 bb90ca7b 2022-07-03 thomas linelen = getline(&line, &linesize, orig);
535 bb90ca7b 2022-07-03 thomas if (linelen == -1) {
536 bb90ca7b 2022-07-03 thomas err = got_error_from_errno("getline");
537 bb90ca7b 2022-07-03 thomas goto done;
538 bb90ca7b 2022-07-03 thomas }
539 bb90ca7b 2022-07-03 thomas if (line[linelen - 1] == '\n')
540 bb90ca7b 2022-07-03 thomas line[linelen - 1] = '\0';
541 bb90ca7b 2022-07-03 thomas t = line;
542 bb90ca7b 2022-07-03 thomas } else
543 bb90ca7b 2022-07-03 thomas t = h->lines[i] + 1;
544 bb90ca7b 2022-07-03 thomas if (mode == '-')
545 bb90ca7b 2022-07-03 thomas continue;
546 bb90ca7b 2022-07-03 thomas if (fprintf(tmp, "%s\n", t) < 0) {
547 bb90ca7b 2022-07-03 thomas err = got_error_from_errno("fprintf");
548 bb90ca7b 2022-07-03 thomas goto done;
549 bb90ca7b 2022-07-03 thomas }
550 069bbb86 2022-03-07 thomas break;
551 069bbb86 2022-03-07 thomas case '+':
552 656c2baa 2022-04-23 thomas new++;
553 bb90ca7b 2022-07-03 thomas if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
554 bb90ca7b 2022-07-03 thomas err = got_error_from_errno("fprintf");
555 bb90ca7b 2022-07-03 thomas goto done;
556 bb90ca7b 2022-07-03 thomas }
557 656c2baa 2022-04-23 thomas if (new != h->new_lines || !h->new_nonl) {
558 bb90ca7b 2022-07-03 thomas if (fprintf(tmp, "\n") < 0) {
559 bb90ca7b 2022-07-03 thomas err = got_error_from_errno("fprintf");
560 bb90ca7b 2022-07-03 thomas goto done;
561 bb90ca7b 2022-07-03 thomas }
562 ff7f34d3 2022-03-22 thomas }
563 069bbb86 2022-03-07 thomas break;
564 069bbb86 2022-03-07 thomas }
565 069bbb86 2022-03-07 thomas }
566 bb90ca7b 2022-07-03 thomas
567 bb90ca7b 2022-07-03 thomas done:
568 bb90ca7b 2022-07-03 thomas free(line);
569 bb90ca7b 2022-07-03 thomas return err;
570 46e6bdd4 2022-03-22 thomas }
571 46e6bdd4 2022-03-22 thomas
572 46e6bdd4 2022-03-22 thomas static const struct got_error *
573 25a880e1 2022-07-03 thomas patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
574 bb2ad8ff 2022-03-13 thomas {
575 bb2ad8ff 2022-03-13 thomas const struct got_error *err = NULL;
576 bb2ad8ff 2022-03-13 thomas struct got_patch_hunk *h;
577 da09d8ed 2022-03-22 thomas struct stat sb;
578 8ebb3daa 2022-06-23 thomas int lineno = 0;
579 bb2ad8ff 2022-03-13 thomas off_t copypos, pos;
580 bb2ad8ff 2022-03-13 thomas char *line = NULL;
581 bb2ad8ff 2022-03-13 thomas size_t linesize = 0;
582 bb2ad8ff 2022-03-13 thomas ssize_t linelen;
583 94af5a06 2022-03-08 thomas
584 069bbb86 2022-03-07 thomas if (p->old == NULL) { /* create */
585 069bbb86 2022-03-07 thomas h = STAILQ_FIRST(&p->head);
586 bb2ad8ff 2022-03-13 thomas if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
587 bb2ad8ff 2022-03-13 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
588 bb90ca7b 2022-07-03 thomas return apply_hunk(orig, tmp, h, &lineno, 0);
589 069bbb86 2022-03-07 thomas }
590 650a3405 2023-01-02 thomas
591 650a3405 2023-01-02 thomas /* When deleting binary files there are no hunks to apply. */
592 650a3405 2023-01-02 thomas if (p->new == NULL && STAILQ_EMPTY(&p->head))
593 650a3405 2023-01-02 thomas return NULL;
594 069bbb86 2022-03-07 thomas
595 2249fadd 2022-06-23 thomas if (fstat(fileno(orig), &sb) == -1)
596 2249fadd 2022-06-23 thomas return got_error_from_errno("fstat");
597 da09d8ed 2022-03-22 thomas
598 069bbb86 2022-03-07 thomas copypos = 0;
599 069bbb86 2022-03-07 thomas STAILQ_FOREACH(h, &p->head, entries) {
600 069bbb86 2022-03-07 thomas tryagain:
601 122e1611 2022-03-22 thomas err = locate_hunk(orig, h, &pos, &lineno);
602 49114f01 2022-03-22 thomas if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
603 49114f01 2022-03-22 thomas h->err = err;
604 069bbb86 2022-03-07 thomas if (err != NULL)
605 2249fadd 2022-06-23 thomas return err;
606 2d9b6837 2022-06-23 thomas err = copy(tmp, orig, copypos, pos);
607 069bbb86 2022-03-07 thomas if (err != NULL)
608 2249fadd 2022-06-23 thomas return err;
609 069bbb86 2022-03-07 thomas copypos = pos;
610 069bbb86 2022-03-07 thomas
611 069bbb86 2022-03-07 thomas err = test_hunk(orig, h);
612 49114f01 2022-03-22 thomas if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
613 069bbb86 2022-03-07 thomas /*
614 069bbb86 2022-03-07 thomas * try to apply the hunk again starting the search
615 069bbb86 2022-03-07 thomas * after the previous partial match.
616 069bbb86 2022-03-07 thomas */
617 2249fadd 2022-06-23 thomas if (fseeko(orig, pos, SEEK_SET) == -1)
618 2249fadd 2022-06-23 thomas return got_error_from_errno("fseeko");
619 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
620 2249fadd 2022-06-23 thomas if (linelen == -1)
621 2249fadd 2022-06-23 thomas return got_error_from_errno("getline");
622 069bbb86 2022-03-07 thomas lineno++;
623 069bbb86 2022-03-07 thomas goto tryagain;
624 069bbb86 2022-03-07 thomas }
625 069bbb86 2022-03-07 thomas if (err != NULL)
626 2249fadd 2022-06-23 thomas return err;
627 069bbb86 2022-03-07 thomas
628 49114f01 2022-03-22 thomas if (lineno + 1 != h->old_from)
629 49114f01 2022-03-22 thomas h->offset = lineno + 1 - h->old_from;
630 49114f01 2022-03-22 thomas
631 bb90ca7b 2022-07-03 thomas err = apply_hunk(orig, tmp, h, &lineno, pos);
632 069bbb86 2022-03-07 thomas if (err != NULL)
633 2249fadd 2022-06-23 thomas return err;
634 f5f21873 2022-04-16 thomas
635 069bbb86 2022-03-07 thomas copypos = ftello(orig);
636 2249fadd 2022-06-23 thomas if (copypos == -1)
637 2249fadd 2022-06-23 thomas return got_error_from_errno("ftello");
638 069bbb86 2022-03-07 thomas }
639 069bbb86 2022-03-07 thomas
640 49114f01 2022-03-22 thomas if (p->new == NULL && sb.st_size != copypos) {
641 49114f01 2022-03-22 thomas h = STAILQ_FIRST(&p->head);
642 49114f01 2022-03-22 thomas h->err = got_error(GOT_ERR_HUNK_FAILED);
643 49114f01 2022-03-22 thomas err = h->err;
644 2d9b6837 2022-06-23 thomas } else if (!feof(orig))
645 069bbb86 2022-03-07 thomas err = copy(tmp, orig, copypos, -1);
646 bb2ad8ff 2022-03-13 thomas
647 10e55613 2022-03-22 thomas return err;
648 10e55613 2022-03-22 thomas }
649 10e55613 2022-03-22 thomas
650 10e55613 2022-03-22 thomas static const struct got_error *
651 49114f01 2022-03-22 thomas report_progress(struct patch_args *pa, const char *old, const char *new,
652 49114f01 2022-03-22 thomas unsigned char status, const struct got_error *orig_error)
653 49114f01 2022-03-22 thomas {
654 49114f01 2022-03-22 thomas const struct got_error *err;
655 49114f01 2022-03-22 thomas struct got_patch_hunk *h;
656 49114f01 2022-03-22 thomas
657 49114f01 2022-03-22 thomas err = pa->progress_cb(pa->progress_arg, old, new, status,
658 bb90ca7b 2022-07-03 thomas orig_error, 0, 0, 0, 0, 0, 0, NULL);
659 49114f01 2022-03-22 thomas if (err)
660 49114f01 2022-03-22 thomas return err;
661 49114f01 2022-03-22 thomas
662 49114f01 2022-03-22 thomas STAILQ_FOREACH(h, pa->head, entries) {
663 bb90ca7b 2022-07-03 thomas if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
664 49114f01 2022-03-22 thomas continue;
665 49114f01 2022-03-22 thomas
666 49114f01 2022-03-22 thomas err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
667 49114f01 2022-03-22 thomas h->old_from, h->old_lines, h->new_from, h->new_lines,
668 bb90ca7b 2022-07-03 thomas h->offset, h->ws_mangled, h->err);
669 49114f01 2022-03-22 thomas if (err)
670 49114f01 2022-03-22 thomas return err;
671 49114f01 2022-03-22 thomas }
672 49114f01 2022-03-22 thomas
673 49114f01 2022-03-22 thomas return NULL;
674 49114f01 2022-03-22 thomas }
675 49114f01 2022-03-22 thomas
676 49114f01 2022-03-22 thomas static const struct got_error *
677 c71da4f7 2022-03-22 thomas patch_delete(void *arg, unsigned char status, unsigned char staged_status,
678 c71da4f7 2022-03-22 thomas const char *path)
679 c71da4f7 2022-03-22 thomas {
680 49114f01 2022-03-22 thomas return report_progress(arg, path, NULL, status, NULL);
681 c71da4f7 2022-03-22 thomas }
682 c71da4f7 2022-03-22 thomas
683 c71da4f7 2022-03-22 thomas static const struct got_error *
684 c71da4f7 2022-03-22 thomas patch_add(void *arg, unsigned char status, const char *path)
685 c71da4f7 2022-03-22 thomas {
686 49114f01 2022-03-22 thomas return report_progress(arg, NULL, path, status, NULL);
687 c71da4f7 2022-03-22 thomas }
688 c71da4f7 2022-03-22 thomas
689 c71da4f7 2022-03-22 thomas static const struct got_error *
690 0f76ab83 2022-06-23 thomas open_blob(char **path, FILE **fp, const char *blobid,
691 0f76ab83 2022-06-23 thomas struct got_repository *repo)
692 bb2ad8ff 2022-03-13 thomas {
693 bb2ad8ff 2022-03-13 thomas const struct got_error *err = NULL;
694 0f76ab83 2022-06-23 thomas struct got_blob_object *blob = NULL;
695 19dd85cb 2022-07-01 thomas struct got_object_id id, *idptr, *matched_id = NULL;
696 f4ae6ddb 2022-07-01 thomas int fd = -1;
697 0f76ab83 2022-06-23 thomas
698 0f76ab83 2022-06-23 thomas *fp = NULL;
699 0f76ab83 2022-06-23 thomas *path = NULL;
700 0f76ab83 2022-06-23 thomas
701 19dd85cb 2022-07-01 thomas if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
702 19dd85cb 2022-07-01 thomas err = got_repo_match_object_id(&matched_id, NULL, blobid,
703 19dd85cb 2022-07-01 thomas GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
704 19dd85cb 2022-07-01 thomas repo);
705 19dd85cb 2022-07-01 thomas if (err)
706 19dd85cb 2022-07-01 thomas return err;
707 19dd85cb 2022-07-01 thomas idptr = matched_id;
708 19dd85cb 2022-07-01 thomas } else {
709 c8ae092d 2023-02-23 thomas if (!got_parse_object_id(&id, blobid, GOT_HASH_SHA1))
710 19dd85cb 2022-07-01 thomas return got_error(GOT_ERR_BAD_OBJ_ID_STR);
711 19dd85cb 2022-07-01 thomas idptr = &id;
712 19dd85cb 2022-07-01 thomas }
713 0f76ab83 2022-06-23 thomas
714 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
715 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
716 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
717 f4ae6ddb 2022-07-01 thomas goto done;
718 f4ae6ddb 2022-07-01 thomas }
719 f4ae6ddb 2022-07-01 thomas
720 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
721 0f76ab83 2022-06-23 thomas if (err)
722 0f76ab83 2022-06-23 thomas goto done;
723 0f76ab83 2022-06-23 thomas
724 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
725 fc2a50f2 2022-11-01 thomas "");
726 0f76ab83 2022-06-23 thomas if (err)
727 0f76ab83 2022-06-23 thomas goto done;
728 0f76ab83 2022-06-23 thomas
729 0f76ab83 2022-06-23 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
730 0f76ab83 2022-06-23 thomas if (err)
731 0f76ab83 2022-06-23 thomas goto done;
732 0f76ab83 2022-06-23 thomas
733 0f76ab83 2022-06-23 thomas done:
734 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
735 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
736 0f76ab83 2022-06-23 thomas if (blob)
737 0f76ab83 2022-06-23 thomas got_object_blob_close(blob);
738 19dd85cb 2022-07-01 thomas if (matched_id != NULL)
739 19dd85cb 2022-07-01 thomas free(matched_id);
740 0f76ab83 2022-06-23 thomas if (err) {
741 0f76ab83 2022-06-23 thomas if (*fp != NULL)
742 0f76ab83 2022-06-23 thomas fclose(*fp);
743 0f76ab83 2022-06-23 thomas if (*path != NULL)
744 0f76ab83 2022-06-23 thomas unlink(*path);
745 0f76ab83 2022-06-23 thomas free(*path);
746 0f76ab83 2022-06-23 thomas *fp = NULL;
747 0f76ab83 2022-06-23 thomas *path = NULL;
748 0f76ab83 2022-06-23 thomas }
749 0f76ab83 2022-06-23 thomas return err;
750 0f76ab83 2022-06-23 thomas }
751 0f76ab83 2022-06-23 thomas
752 0f76ab83 2022-06-23 thomas static const struct got_error *
753 7d8bcb99 2022-07-28 thomas prepare_merge(int *do_merge, char **apath, FILE **afile,
754 7d8bcb99 2022-07-28 thomas struct got_worktree *worktree, struct got_repository *repo,
755 7d8bcb99 2022-07-28 thomas struct got_patch *p, struct got_object_id *commit_id,
756 7d8bcb99 2022-07-28 thomas struct got_tree_object *tree, const char *path)
757 7d8bcb99 2022-07-28 thomas {
758 7d8bcb99 2022-07-28 thomas const struct got_error *err = NULL;
759 7d8bcb99 2022-07-28 thomas
760 7d8bcb99 2022-07-28 thomas *do_merge = 0;
761 7d8bcb99 2022-07-28 thomas *apath = NULL;
762 7d8bcb99 2022-07-28 thomas *afile = NULL;
763 7d8bcb99 2022-07-28 thomas
764 7d8bcb99 2022-07-28 thomas /* don't run the diff3 merge on creations/deletions */
765 7d8bcb99 2022-07-28 thomas if (p->old == NULL || p->new == NULL)
766 7d8bcb99 2022-07-28 thomas return NULL;
767 7d8bcb99 2022-07-28 thomas
768 7d8bcb99 2022-07-28 thomas if (commit_id) {
769 7d8bcb99 2022-07-28 thomas struct got_object_id *id;
770 7d8bcb99 2022-07-28 thomas
771 7d8bcb99 2022-07-28 thomas err = got_object_tree_find_path(&id, NULL, repo, tree, path);
772 7d8bcb99 2022-07-28 thomas if (err)
773 7d8bcb99 2022-07-28 thomas return err;
774 7d8bcb99 2022-07-28 thomas got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
775 7d8bcb99 2022-07-28 thomas got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
776 7d8bcb99 2022-07-28 thomas free(id);
777 7d8bcb99 2022-07-28 thomas err = open_blob(apath, afile, p->blob, repo);
778 7d8bcb99 2022-07-28 thomas *do_merge = err == NULL;
779 7d8bcb99 2022-07-28 thomas } else if (*p->blob != '\0') {
780 7d8bcb99 2022-07-28 thomas err = open_blob(apath, afile, p->blob, repo);
781 7d8bcb99 2022-07-28 thomas /*
782 7d8bcb99 2022-07-28 thomas * ignore failures to open this blob, we might have
783 7d8bcb99 2022-07-28 thomas * parsed gibberish.
784 7d8bcb99 2022-07-28 thomas */
785 7d8bcb99 2022-07-28 thomas if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
786 7d8bcb99 2022-07-28 thomas err->code != GOT_ERR_NO_OBJ)
787 7d8bcb99 2022-07-28 thomas return err;
788 7d8bcb99 2022-07-28 thomas *do_merge = err == NULL;
789 7d8bcb99 2022-07-28 thomas err = NULL;
790 7d8bcb99 2022-07-28 thomas }
791 7d8bcb99 2022-07-28 thomas
792 7d8bcb99 2022-07-28 thomas return err;
793 7d8bcb99 2022-07-28 thomas }
794 7d8bcb99 2022-07-28 thomas
795 7d8bcb99 2022-07-28 thomas static const struct got_error *
796 0f76ab83 2022-06-23 thomas apply_patch(int *overlapcnt, struct got_worktree *worktree,
797 0f76ab83 2022-06-23 thomas struct got_repository *repo, struct got_fileindex *fileindex,
798 0f76ab83 2022-06-23 thomas const char *old, const char *new, struct got_patch *p, int nop,
799 7d8bcb99 2022-07-28 thomas int reverse, struct got_object_id *commit_id,
800 7d8bcb99 2022-07-28 thomas struct got_tree_object *tree, struct patch_args *pa,
801 f4602cbd 2022-07-23 thomas got_cancel_cb cancel_cb, void *cancel_arg)
802 0f76ab83 2022-06-23 thomas {
803 0f76ab83 2022-06-23 thomas const struct got_error *err = NULL;
804 25a880e1 2022-07-03 thomas struct stat sb;
805 0f76ab83 2022-06-23 thomas int do_merge = 0, file_renamed = 0;
806 016bfe4b 2022-06-23 thomas char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
807 72f46891 2022-04-23 thomas char *oldpath = NULL, *newpath = NULL;
808 dd73a53d 2022-10-16 thomas char *tmppath = NULL, *template = NULL;
809 0f76ab83 2022-06-23 thomas char *apath = NULL, *mergepath = NULL;
810 0f76ab83 2022-06-23 thomas FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
811 0f76ab83 2022-06-23 thomas int outfd;
812 da09d8ed 2022-03-22 thomas mode_t mode = GOT_DEFAULT_FILE_MODE;
813 72f46891 2022-04-23 thomas
814 0f76ab83 2022-06-23 thomas *overlapcnt = 0;
815 0f76ab83 2022-06-23 thomas
816 7d8bcb99 2022-07-28 thomas err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
817 7d8bcb99 2022-07-28 thomas commit_id, tree, old);
818 7d8bcb99 2022-07-28 thomas if (err)
819 7d8bcb99 2022-07-28 thomas return err;
820 0f76ab83 2022-06-23 thomas
821 21948212 2022-07-27 thomas if (reverse && !do_merge)
822 21948212 2022-07-27 thomas reverse_patch(p);
823 21948212 2022-07-27 thomas
824 72f46891 2022-04-23 thomas if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
825 72f46891 2022-04-23 thomas old) == -1) {
826 72f46891 2022-04-23 thomas err = got_error_from_errno("asprintf");
827 814624e7 2022-03-22 thomas goto done;
828 72f46891 2022-04-23 thomas }
829 10e55613 2022-03-22 thomas
830 72f46891 2022-04-23 thomas if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
831 72f46891 2022-04-23 thomas new) == -1) {
832 72f46891 2022-04-23 thomas err = got_error_from_errno("asprintf");
833 72f46891 2022-04-23 thomas goto done;
834 72f46891 2022-04-23 thomas }
835 72f46891 2022-04-23 thomas
836 814624e7 2022-03-22 thomas file_renamed = strcmp(oldpath, newpath);
837 42d9d68e 2022-03-13 thomas
838 bb2ad8ff 2022-03-13 thomas if (asprintf(&template, "%s/got-patch",
839 bb2ad8ff 2022-03-13 thomas got_worktree_get_root_path(worktree)) == -1) {
840 bb2ad8ff 2022-03-13 thomas err = got_error_from_errno(template);
841 069bbb86 2022-03-07 thomas goto done;
842 069bbb86 2022-03-07 thomas }
843 069bbb86 2022-03-07 thomas
844 25a880e1 2022-07-03 thomas if (p->old != NULL) {
845 25a880e1 2022-07-03 thomas if ((oldfile = fopen(oldpath, "r")) == NULL) {
846 25a880e1 2022-07-03 thomas err = got_error_from_errno2("open", oldpath);
847 25a880e1 2022-07-03 thomas goto done;
848 25a880e1 2022-07-03 thomas }
849 25a880e1 2022-07-03 thomas if (fstat(fileno(oldfile), &sb) == -1) {
850 25a880e1 2022-07-03 thomas err = got_error_from_errno2("fstat", oldpath);
851 25a880e1 2022-07-03 thomas goto done;
852 25a880e1 2022-07-03 thomas }
853 25a880e1 2022-07-03 thomas mode = sb.st_mode;
854 37e766f4 2022-09-21 thomas } else if (p->xbit)
855 37e766f4 2022-09-21 thomas mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
856 2249fadd 2022-06-23 thomas
857 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpfile, template, "");
858 bb2ad8ff 2022-03-13 thomas if (err)
859 bb2ad8ff 2022-03-13 thomas goto done;
860 0f76ab83 2022-06-23 thomas outfd = fileno(tmpfile);
861 25a880e1 2022-07-03 thomas err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
862 bb2ad8ff 2022-03-13 thomas if (err)
863 bb2ad8ff 2022-03-13 thomas goto done;
864 bb2ad8ff 2022-03-13 thomas
865 0f76ab83 2022-06-23 thomas if (do_merge) {
866 8afec5d5 2022-07-01 thomas const char *type, *id;
867 8afec5d5 2022-07-01 thomas
868 0f76ab83 2022-06-23 thomas if (fseeko(afile, 0, SEEK_SET) == -1 ||
869 0f76ab83 2022-06-23 thomas fseeko(oldfile, 0, SEEK_SET) == -1 ||
870 0f76ab83 2022-06-23 thomas fseeko(tmpfile, 0, SEEK_SET) == -1) {
871 0f76ab83 2022-06-23 thomas err = got_error_from_errno("fseeko");
872 0f76ab83 2022-06-23 thomas goto done;
873 0f76ab83 2022-06-23 thomas }
874 0f76ab83 2022-06-23 thomas
875 0f76ab83 2022-06-23 thomas if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
876 0f76ab83 2022-06-23 thomas err = got_error_from_errno("asprintf");
877 0f76ab83 2022-06-23 thomas oldlabel = NULL;
878 0f76ab83 2022-06-23 thomas goto done;
879 0f76ab83 2022-06-23 thomas }
880 0f76ab83 2022-06-23 thomas
881 0f76ab83 2022-06-23 thomas if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
882 0f76ab83 2022-06-23 thomas err = got_error_from_errno("asprintf");
883 0f76ab83 2022-06-23 thomas newlabel = NULL;
884 0f76ab83 2022-06-23 thomas goto done;
885 0f76ab83 2022-06-23 thomas }
886 0f76ab83 2022-06-23 thomas
887 8afec5d5 2022-07-01 thomas if (*p->cid != '\0') {
888 8afec5d5 2022-07-01 thomas type = "commit";
889 8afec5d5 2022-07-01 thomas id = p->cid;
890 8afec5d5 2022-07-01 thomas } else {
891 8afec5d5 2022-07-01 thomas type = "blob";
892 8afec5d5 2022-07-01 thomas id = p->blob;
893 8afec5d5 2022-07-01 thomas }
894 8afec5d5 2022-07-01 thomas
895 8afec5d5 2022-07-01 thomas if (asprintf(&anclabel, "%s %s", type, id) == -1) {
896 016bfe4b 2022-06-23 thomas err = got_error_from_errno("asprintf");
897 016bfe4b 2022-06-23 thomas anclabel = NULL;
898 016bfe4b 2022-06-23 thomas goto done;
899 016bfe4b 2022-06-23 thomas }
900 016bfe4b 2022-06-23 thomas
901 f4602cbd 2022-07-23 thomas if (reverse) {
902 f4602cbd 2022-07-23 thomas char *s;
903 f4602cbd 2022-07-23 thomas FILE *t;
904 f4602cbd 2022-07-23 thomas
905 f4602cbd 2022-07-23 thomas s = anclabel;
906 f4602cbd 2022-07-23 thomas anclabel = newlabel;
907 f4602cbd 2022-07-23 thomas newlabel = s;
908 f4602cbd 2022-07-23 thomas
909 f4602cbd 2022-07-23 thomas t = afile;
910 f4602cbd 2022-07-23 thomas afile = tmpfile;
911 f4602cbd 2022-07-23 thomas tmpfile = t;
912 f4602cbd 2022-07-23 thomas }
913 f4602cbd 2022-07-23 thomas
914 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&mergepath, &mergefile, template, "");
915 0f76ab83 2022-06-23 thomas if (err)
916 0f76ab83 2022-06-23 thomas goto done;
917 0f76ab83 2022-06-23 thomas outfd = fileno(mergefile);
918 0f76ab83 2022-06-23 thomas
919 0f76ab83 2022-06-23 thomas err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
920 016bfe4b 2022-06-23 thomas oldfile, oldlabel, anclabel, newlabel,
921 0f76ab83 2022-06-23 thomas GOT_DIFF_ALGORITHM_PATIENCE);
922 0f76ab83 2022-06-23 thomas if (err)
923 0f76ab83 2022-06-23 thomas goto done;
924 0f76ab83 2022-06-23 thomas }
925 0f76ab83 2022-06-23 thomas
926 c71da4f7 2022-03-22 thomas if (nop)
927 eaf99875 2022-03-22 thomas goto done;
928 eaf99875 2022-03-22 thomas
929 eaf99875 2022-03-22 thomas if (p->old != NULL && p->new == NULL) {
930 5e22b737 2022-05-31 thomas err = got_worktree_patch_schedule_rm(old, repo, worktree,
931 5e22b737 2022-05-31 thomas fileindex, patch_delete, pa);
932 eaf99875 2022-03-22 thomas goto done;
933 eaf99875 2022-03-22 thomas }
934 eaf99875 2022-03-22 thomas
935 a2c162eb 2022-10-30 thomas if (fchmod(outfd, apply_umask(mode)) == -1) {
936 8260acc8 2022-05-01 thomas err = got_error_from_errno2("chmod", tmppath);
937 da09d8ed 2022-03-22 thomas goto done;
938 da09d8ed 2022-03-22 thomas }
939 da09d8ed 2022-03-22 thomas
940 dd73a53d 2022-10-16 thomas if (mergepath) {
941 dd73a53d 2022-10-16 thomas err = got_path_move_file(mergepath, newpath);
942 dd73a53d 2022-10-16 thomas if (err)
943 e0c1f81c 2022-03-22 thomas goto done;
944 dd73a53d 2022-10-16 thomas free(mergepath);
945 dd73a53d 2022-10-16 thomas mergepath = NULL;
946 dd73a53d 2022-10-16 thomas } else {
947 dd73a53d 2022-10-16 thomas err = got_path_move_file(tmppath, newpath);
948 dd73a53d 2022-10-16 thomas if (err)
949 e0c1f81c 2022-03-22 thomas goto done;
950 dd73a53d 2022-10-16 thomas free(tmppath);
951 dd73a53d 2022-10-16 thomas tmppath = NULL;
952 bb2ad8ff 2022-03-13 thomas }
953 bb2ad8ff 2022-03-13 thomas
954 bb2ad8ff 2022-03-13 thomas if (file_renamed) {
955 5e22b737 2022-05-31 thomas err = got_worktree_patch_schedule_rm(old, repo, worktree,
956 5e22b737 2022-05-31 thomas fileindex, patch_delete, pa);
957 bb2ad8ff 2022-03-13 thomas if (err == NULL)
958 5e22b737 2022-05-31 thomas err = got_worktree_patch_schedule_add(new, repo,
959 5e22b737 2022-05-31 thomas worktree, fileindex, patch_add,
960 5e22b737 2022-05-31 thomas pa);
961 814624e7 2022-03-22 thomas if (err)
962 814624e7 2022-03-22 thomas unlink(newpath);
963 814624e7 2022-03-22 thomas } else if (p->old == NULL) {
964 5e22b737 2022-05-31 thomas err = got_worktree_patch_schedule_add(new, repo, worktree,
965 5e22b737 2022-05-31 thomas fileindex, patch_add, pa);
966 814624e7 2022-03-22 thomas if (err)
967 814624e7 2022-03-22 thomas unlink(newpath);
968 0f76ab83 2022-06-23 thomas } else if (*overlapcnt != 0)
969 0f76ab83 2022-06-23 thomas err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
970 762b8e82 2022-06-23 thomas else if (do_merge)
971 762b8e82 2022-06-23 thomas err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
972 0f76ab83 2022-06-23 thomas else
973 72f46891 2022-04-23 thomas err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
974 bb2ad8ff 2022-03-13 thomas
975 069bbb86 2022-03-07 thomas done:
976 94af5a06 2022-03-08 thomas free(template);
977 0f76ab83 2022-06-23 thomas
978 dd73a53d 2022-10-16 thomas if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
979 dd73a53d 2022-10-16 thomas err = got_error_from_errno("unlink");
980 0f76ab83 2022-06-23 thomas if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
981 04cdf6ac 2022-06-23 thomas err = got_error_from_errno("fclose");
982 069bbb86 2022-03-07 thomas free(tmppath);
983 0f76ab83 2022-06-23 thomas
984 72f46891 2022-04-23 thomas free(oldpath);
985 2249fadd 2022-06-23 thomas if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
986 2249fadd 2022-06-23 thomas err = got_error_from_errno("fclose");
987 0f76ab83 2022-06-23 thomas
988 dd73a53d 2022-10-16 thomas if (apath != NULL && unlink(apath) == -1 && err == NULL)
989 dd73a53d 2022-10-16 thomas err = got_error_from_errno("unlink");
990 0f76ab83 2022-06-23 thomas if (afile != NULL && fclose(afile) == EOF && err == NULL)
991 0f76ab83 2022-06-23 thomas err = got_error_from_errno("fclose");
992 0f76ab83 2022-06-23 thomas free(apath);
993 0f76ab83 2022-06-23 thomas
994 dd73a53d 2022-10-16 thomas if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
995 dd73a53d 2022-10-16 thomas err = got_error_from_errno("unlink");
996 0f76ab83 2022-06-23 thomas if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
997 0f76ab83 2022-06-23 thomas err = got_error_from_errno("fclose");
998 0f76ab83 2022-06-23 thomas free(mergepath);
999 0f76ab83 2022-06-23 thomas
1000 72f46891 2022-04-23 thomas free(newpath);
1001 0f76ab83 2022-06-23 thomas free(oldlabel);
1002 0f76ab83 2022-06-23 thomas free(newlabel);
1003 016bfe4b 2022-06-23 thomas free(anclabel);
1004 069bbb86 2022-03-07 thomas return err;
1005 eaef698f 2022-04-23 thomas }
1006 eaef698f 2022-04-23 thomas
1007 069bbb86 2022-03-07 thomas const struct got_error *
1008 069bbb86 2022-03-07 thomas got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1009 7d8bcb99 2022-07-28 thomas int nop, int strip, int reverse, struct got_object_id *commit_id,
1010 7d8bcb99 2022-07-28 thomas got_patch_progress_cb progress_cb, void *progress_arg,
1011 7d8bcb99 2022-07-28 thomas got_cancel_cb cancel_cb, void *cancel_arg)
1012 069bbb86 2022-03-07 thomas {
1013 36832a8e 2022-05-31 thomas const struct got_error *err = NULL, *complete_err = NULL;
1014 814624e7 2022-03-22 thomas struct got_fileindex *fileindex = NULL;
1015 7d8bcb99 2022-07-28 thomas struct got_commit_object *commit = NULL;
1016 7d8bcb99 2022-07-28 thomas struct got_tree_object *tree = NULL;
1017 5e22b737 2022-05-31 thomas char *fileindex_path = NULL;
1018 49114f01 2022-03-22 thomas char *oldpath, *newpath;
1019 069bbb86 2022-03-07 thomas struct imsgbuf *ibuf;
1020 069bbb86 2022-03-07 thomas int imsg_fds[2] = {-1, -1};
1021 0f76ab83 2022-06-23 thomas int overlapcnt, done = 0, failed = 0;
1022 069bbb86 2022-03-07 thomas pid_t pid;
1023 069bbb86 2022-03-07 thomas
1024 069bbb86 2022-03-07 thomas ibuf = calloc(1, sizeof(*ibuf));
1025 069bbb86 2022-03-07 thomas if (ibuf == NULL) {
1026 069bbb86 2022-03-07 thomas err = got_error_from_errno("calloc");
1027 069bbb86 2022-03-07 thomas goto done;
1028 069bbb86 2022-03-07 thomas }
1029 069bbb86 2022-03-07 thomas
1030 069bbb86 2022-03-07 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1031 069bbb86 2022-03-07 thomas err = got_error_from_errno("socketpair");
1032 069bbb86 2022-03-07 thomas goto done;
1033 069bbb86 2022-03-07 thomas }
1034 069bbb86 2022-03-07 thomas
1035 069bbb86 2022-03-07 thomas pid = fork();
1036 069bbb86 2022-03-07 thomas if (pid == -1) {
1037 069bbb86 2022-03-07 thomas err = got_error_from_errno("fork");
1038 069bbb86 2022-03-07 thomas goto done;
1039 069bbb86 2022-03-07 thomas } else if (pid == 0) {
1040 069bbb86 2022-03-07 thomas got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1041 069bbb86 2022-03-07 thomas NULL);
1042 069bbb86 2022-03-07 thomas /* not reached */
1043 069bbb86 2022-03-07 thomas }
1044 069bbb86 2022-03-07 thomas
1045 069bbb86 2022-03-07 thomas if (close(imsg_fds[1]) == -1) {
1046 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
1047 069bbb86 2022-03-07 thomas goto done;
1048 069bbb86 2022-03-07 thomas }
1049 069bbb86 2022-03-07 thomas imsg_fds[1] = -1;
1050 069bbb86 2022-03-07 thomas imsg_init(ibuf, imsg_fds[0]);
1051 069bbb86 2022-03-07 thomas
1052 069bbb86 2022-03-07 thomas err = send_patch(ibuf, fd);
1053 069bbb86 2022-03-07 thomas fd = -1;
1054 069bbb86 2022-03-07 thomas if (err)
1055 069bbb86 2022-03-07 thomas goto done;
1056 069bbb86 2022-03-07 thomas
1057 5e22b737 2022-05-31 thomas err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1058 5e22b737 2022-05-31 thomas worktree);
1059 814624e7 2022-03-22 thomas if (err)
1060 814624e7 2022-03-22 thomas goto done;
1061 814624e7 2022-03-22 thomas
1062 7d8bcb99 2022-07-28 thomas if (commit_id) {
1063 7d8bcb99 2022-07-28 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
1064 7d8bcb99 2022-07-28 thomas if (err)
1065 7d8bcb99 2022-07-28 thomas goto done;
1066 7d8bcb99 2022-07-28 thomas
1067 7d8bcb99 2022-07-28 thomas err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1068 7d8bcb99 2022-07-28 thomas if (err)
1069 7d8bcb99 2022-07-28 thomas goto done;
1070 7d8bcb99 2022-07-28 thomas }
1071 7d8bcb99 2022-07-28 thomas
1072 069bbb86 2022-03-07 thomas while (!done && err == NULL) {
1073 069bbb86 2022-03-07 thomas struct got_patch p;
1074 49114f01 2022-03-22 thomas struct patch_args pa;
1075 069bbb86 2022-03-07 thomas
1076 49114f01 2022-03-22 thomas pa.progress_cb = progress_cb;
1077 49114f01 2022-03-22 thomas pa.progress_arg = progress_arg;
1078 49114f01 2022-03-22 thomas pa.head = &p.head;
1079 49114f01 2022-03-22 thomas
1080 d9db2ff9 2022-04-16 thomas err = recv_patch(ibuf, &done, &p, strip);
1081 069bbb86 2022-03-07 thomas if (err || done)
1082 069bbb86 2022-03-07 thomas break;
1083 069bbb86 2022-03-07 thomas
1084 814624e7 2022-03-22 thomas err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1085 814624e7 2022-03-22 thomas &newpath, worktree, repo, fileindex);
1086 814624e7 2022-03-22 thomas if (err == NULL)
1087 0f76ab83 2022-06-23 thomas err = apply_patch(&overlapcnt, worktree, repo,
1088 f4602cbd 2022-07-23 thomas fileindex, oldpath, newpath, &p, nop, reverse,
1089 7d8bcb99 2022-07-28 thomas commit_id, tree, &pa, cancel_cb, cancel_arg);
1090 49114f01 2022-03-22 thomas if (err != NULL) {
1091 49114f01 2022-03-22 thomas failed = 1;
1092 49114f01 2022-03-22 thomas /* recoverable errors */
1093 49114f01 2022-03-22 thomas if (err->code == GOT_ERR_FILE_STATUS ||
1094 49114f01 2022-03-22 thomas (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1095 49114f01 2022-03-22 thomas err = report_progress(&pa, p.old, p.new,
1096 49114f01 2022-03-22 thomas GOT_STATUS_CANNOT_UPDATE, err);
1097 49114f01 2022-03-22 thomas else if (err->code == GOT_ERR_HUNK_FAILED)
1098 49114f01 2022-03-22 thomas err = report_progress(&pa, p.old, p.new,
1099 49114f01 2022-03-22 thomas GOT_STATUS_CANNOT_UPDATE, NULL);
1100 49114f01 2022-03-22 thomas }
1101 0f76ab83 2022-06-23 thomas if (overlapcnt != 0)
1102 0f76ab83 2022-06-23 thomas failed = 1;
1103 49114f01 2022-03-22 thomas
1104 49114f01 2022-03-22 thomas free(oldpath);
1105 49114f01 2022-03-22 thomas free(newpath);
1106 069bbb86 2022-03-07 thomas patch_free(&p);
1107 49114f01 2022-03-22 thomas
1108 069bbb86 2022-03-07 thomas if (err)
1109 069bbb86 2022-03-07 thomas break;
1110 069bbb86 2022-03-07 thomas }
1111 069bbb86 2022-03-07 thomas
1112 069bbb86 2022-03-07 thomas done:
1113 36832a8e 2022-05-31 thomas if (fileindex != NULL)
1114 36832a8e 2022-05-31 thomas complete_err = got_worktree_patch_complete(fileindex,
1115 36832a8e 2022-05-31 thomas fileindex_path);
1116 5e22b737 2022-05-31 thomas if (complete_err && err == NULL)
1117 5e22b737 2022-05-31 thomas err = complete_err;
1118 36832a8e 2022-05-31 thomas free(fileindex_path);
1119 7d8bcb99 2022-07-28 thomas if (tree)
1120 7d8bcb99 2022-07-28 thomas got_object_tree_close(tree);
1121 7d8bcb99 2022-07-28 thomas if (commit)
1122 7d8bcb99 2022-07-28 thomas got_object_commit_close(commit);
1123 069bbb86 2022-03-07 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
1124 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
1125 069bbb86 2022-03-07 thomas if (ibuf != NULL)
1126 069bbb86 2022-03-07 thomas imsg_clear(ibuf);
1127 069bbb86 2022-03-07 thomas if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1128 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
1129 069bbb86 2022-03-07 thomas if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1130 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
1131 49114f01 2022-03-22 thomas if (err == NULL && failed)
1132 49114f01 2022-03-22 thomas err = got_error(GOT_ERR_PATCH_FAILED);
1133 069bbb86 2022-03-07 thomas return err;
1134 069bbb86 2022-03-07 thomas }