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