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