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