Blame


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