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