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