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