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