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