Blame


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