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