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