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