2 069bbb86 2022-03-07 thomas * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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.
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.
16 069bbb86 2022-03-07 thomas * Apply patches.
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?
22 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
24 069bbb86 2022-03-07 thomas #include <sys/types.h>
25 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
26 069bbb86 2022-03-07 thomas #include <sys/socket.h>
27 7dd42450 2022-03-13 thomas #include <sys/stat.h>
28 069bbb86 2022-03-07 thomas #include <sys/uio.h>
30 bb90ca7b 2022-07-03 thomas #include <ctype.h>
31 42d9d68e 2022-03-13 thomas #include <errno.h>
32 069bbb86 2022-03-07 thomas #include <limits.h>
33 069bbb86 2022-03-07 thomas #include <stdint.h>
34 069bbb86 2022-03-07 thomas #include <stdio.h>
35 069bbb86 2022-03-07 thomas #include <stdlib.h>
36 069bbb86 2022-03-07 thomas #include <string.h>
37 069bbb86 2022-03-07 thomas #include <unistd.h>
39 069bbb86 2022-03-07 thomas #include "got_error.h"
40 069bbb86 2022-03-07 thomas #include "got_object.h"
41 069bbb86 2022-03-07 thomas #include "got_path.h"
42 069bbb86 2022-03-07 thomas #include "got_reference.h"
43 069bbb86 2022-03-07 thomas #include "got_cancel.h"
44 069bbb86 2022-03-07 thomas #include "got_worktree.h"
45 0f76ab83 2022-06-23 thomas #include "got_repository.h"
46 069bbb86 2022-03-07 thomas #include "got_opentemp.h"
47 069bbb86 2022-03-07 thomas #include "got_patch.h"
48 25ec7006 2022-07-01 thomas #include "got_diff.h"
50 069bbb86 2022-03-07 thomas #include "got_lib_delta.h"
51 0f76ab83 2022-06-23 thomas #include "got_lib_diff.h"
52 069bbb86 2022-03-07 thomas #include "got_lib_object.h"
53 069bbb86 2022-03-07 thomas #include "got_lib_privsep.h"
54 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
56 5c23a559 2023-03-03 thomas #ifndef MIN
57 069bbb86 2022-03-07 thomas #define MIN(a, b) ((a) < (b) ? (a) : (b))
60 069bbb86 2022-03-07 thomas struct got_patch_hunk {
61 069bbb86 2022-03-07 thomas STAILQ_ENTRY(got_patch_hunk) entries;
62 49114f01 2022-03-22 thomas const struct got_error *err;
63 bb90ca7b 2022-07-03 thomas int ws_mangled;
64 8ebb3daa 2022-06-23 thomas int offset;
65 656c2baa 2022-04-23 thomas int old_nonl;
66 656c2baa 2022-04-23 thomas int new_nonl;
67 8ebb3daa 2022-06-23 thomas int old_from;
68 8ebb3daa 2022-06-23 thomas int old_lines;
69 8ebb3daa 2022-06-23 thomas int new_from;
70 8ebb3daa 2022-06-23 thomas int new_lines;
71 069bbb86 2022-03-07 thomas size_t len;
72 069bbb86 2022-03-07 thomas size_t cap;
73 069bbb86 2022-03-07 thomas char **lines;
76 49114f01 2022-03-22 thomas STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
77 069bbb86 2022-03-07 thomas struct got_patch {
79 069bbb86 2022-03-07 thomas char *old;
80 069bbb86 2022-03-07 thomas char *new;
81 016bfe4b 2022-06-23 thomas char cid[41];
82 0f76ab83 2022-06-23 thomas char blob[41];
83 49114f01 2022-03-22 thomas struct got_patch_hunk_head head;
86 c71da4f7 2022-03-22 thomas struct patch_args {
87 c71da4f7 2022-03-22 thomas got_patch_progress_cb progress_cb;
88 c71da4f7 2022-03-22 thomas void *progress_arg;
89 49114f01 2022-03-22 thomas struct got_patch_hunk_head *head;
92 a2c162eb 2022-10-30 thomas static mode_t
93 a2c162eb 2022-10-30 thomas apply_umask(mode_t mode)
95 a2c162eb 2022-10-30 thomas mode_t um;
97 a2c162eb 2022-10-30 thomas um = umask(000);
98 a2c162eb 2022-10-30 thomas umask(um);
99 a2c162eb 2022-10-30 thomas return mode & ~um;
102 069bbb86 2022-03-07 thomas static const struct got_error *
103 069bbb86 2022-03-07 thomas send_patch(struct imsgbuf *ibuf, int fd)
105 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
107 069bbb86 2022-03-07 thomas if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
108 069bbb86 2022-03-07 thomas NULL, 0) == -1) {
109 069bbb86 2022-03-07 thomas err = got_error_from_errno(
110 069bbb86 2022-03-07 thomas "imsg_compose GOT_IMSG_PATCH_FILE");
111 069bbb86 2022-03-07 thomas close(fd);
112 069bbb86 2022-03-07 thomas return err;
115 dab315a5 2022-07-07 thomas return got_privsep_flush_imsg(ibuf);
118 069bbb86 2022-03-07 thomas static void
119 069bbb86 2022-03-07 thomas patch_free(struct got_patch *p)
121 069bbb86 2022-03-07 thomas struct got_patch_hunk *h;
122 069bbb86 2022-03-07 thomas size_t i;
124 069bbb86 2022-03-07 thomas while (!STAILQ_EMPTY(&p->head)) {
125 069bbb86 2022-03-07 thomas h = STAILQ_FIRST(&p->head);
126 069bbb86 2022-03-07 thomas STAILQ_REMOVE_HEAD(&p->head, entries);
128 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i)
129 069bbb86 2022-03-07 thomas free(h->lines[i]);
130 069bbb86 2022-03-07 thomas free(h->lines);
134 069bbb86 2022-03-07 thomas free(p->new);
135 069bbb86 2022-03-07 thomas free(p->old);
137 9880a1dd 2022-06-23 thomas memset(p, 0, sizeof(*p));
138 9880a1dd 2022-06-23 thomas STAILQ_INIT(&p->head);
141 069bbb86 2022-03-07 thomas static const struct got_error *
142 069bbb86 2022-03-07 thomas pushline(struct got_patch_hunk *h, const char *line)
145 069bbb86 2022-03-07 thomas size_t newcap;
147 069bbb86 2022-03-07 thomas if (h->len == h->cap) {
148 069bbb86 2022-03-07 thomas if ((newcap = h->cap * 1.5) == 0)
149 069bbb86 2022-03-07 thomas newcap = 16;
150 069bbb86 2022-03-07 thomas t = recallocarray(h->lines, h->cap, newcap,
151 069bbb86 2022-03-07 thomas sizeof(h->lines[0]));
152 069bbb86 2022-03-07 thomas if (t == NULL)
153 069bbb86 2022-03-07 thomas return got_error_from_errno("recallocarray");
154 069bbb86 2022-03-07 thomas h->lines = t;
155 069bbb86 2022-03-07 thomas h->cap = newcap;
158 069bbb86 2022-03-07 thomas if ((t = strdup(line)) == NULL)
159 069bbb86 2022-03-07 thomas return got_error_from_errno("strdup");
161 069bbb86 2022-03-07 thomas h->lines[h->len++] = t;
162 069bbb86 2022-03-07 thomas return NULL;
165 069bbb86 2022-03-07 thomas static const struct got_error *
166 d9db2ff9 2022-04-16 thomas recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
168 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
169 069bbb86 2022-03-07 thomas struct imsg imsg;
170 069bbb86 2022-03-07 thomas struct got_imsg_patch_hunk hdr;
171 069bbb86 2022-03-07 thomas struct got_imsg_patch patch;
172 069bbb86 2022-03-07 thomas struct got_patch_hunk *h = NULL;
173 069bbb86 2022-03-07 thomas size_t datalen;
174 656c2baa 2022-04-23 thomas int lastmode = -1;
176 069bbb86 2022-03-07 thomas memset(p, 0, sizeof(*p));
177 069bbb86 2022-03-07 thomas STAILQ_INIT(&p->head);
179 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, ibuf, 0);
181 069bbb86 2022-03-07 thomas return err;
182 069bbb86 2022-03-07 thomas if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
183 069bbb86 2022-03-07 thomas *done = 1;
184 069bbb86 2022-03-07 thomas goto done;
186 069bbb86 2022-03-07 thomas if (imsg.hdr.type != GOT_IMSG_PATCH) {
187 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
188 069bbb86 2022-03-07 thomas goto done;
190 069bbb86 2022-03-07 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
191 069bbb86 2022-03-07 thomas if (datalen != sizeof(patch)) {
192 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
193 069bbb86 2022-03-07 thomas goto done;
195 069bbb86 2022-03-07 thomas memcpy(&patch, imsg.data, sizeof(patch));
197 ca357dd9 2022-06-23 thomas if (patch.old[sizeof(patch.old)-1] != '\0' ||
198 0f76ab83 2022-06-23 thomas patch.new[sizeof(patch.new)-1] != '\0' ||
199 016bfe4b 2022-06-23 thomas patch.cid[sizeof(patch.cid)-1] != '\0' ||
200 0f76ab83 2022-06-23 thomas patch.blob[sizeof(patch.blob)-1] != '\0') {
201 ca357dd9 2022-06-23 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
202 ca357dd9 2022-06-23 thomas goto done;
205 8afec5d5 2022-07-01 thomas if (*patch.cid != '\0')
206 016bfe4b 2022-06-23 thomas strlcpy(p->cid, patch.cid, sizeof(p->cid));
208 8afec5d5 2022-07-01 thomas if (*patch.blob != '\0')
209 016bfe4b 2022-06-23 thomas strlcpy(p->blob, patch.blob, sizeof(p->blob));
211 37e766f4 2022-09-21 thomas p->xbit = patch.xbit;
213 d9db2ff9 2022-04-16 thomas /* automatically set strip=1 for git-style diffs */
214 d9db2ff9 2022-04-16 thomas if (strip == -1 && patch.git &&
215 d9db2ff9 2022-04-16 thomas (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
216 d9db2ff9 2022-04-16 thomas (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
217 d9db2ff9 2022-04-16 thomas strip = 1;
219 d9db2ff9 2022-04-16 thomas /* prefer the new name if not /dev/null for not git-style diffs */
220 d9db2ff9 2022-04-16 thomas if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
221 d9db2ff9 2022-04-16 thomas err = got_path_strip(&p->old, patch.new, strip);
223 d9db2ff9 2022-04-16 thomas goto done;
224 d9db2ff9 2022-04-16 thomas } else if (*patch.old != '\0') {
225 d9db2ff9 2022-04-16 thomas err = got_path_strip(&p->old, patch.old, strip);
227 d9db2ff9 2022-04-16 thomas goto done;
230 d9db2ff9 2022-04-16 thomas if (*patch.new != '\0') {
231 d9db2ff9 2022-04-16 thomas err = got_path_strip(&p->new, patch.new, strip);
233 d9db2ff9 2022-04-16 thomas goto done;
236 64c9e565 2022-03-13 thomas if (p->old == NULL && p->new == NULL) {
237 64c9e565 2022-03-13 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
238 64c9e565 2022-03-13 thomas goto done;
241 069bbb86 2022-03-07 thomas imsg_free(&imsg);
243 069bbb86 2022-03-07 thomas for (;;) {
246 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, ibuf, 0);
247 9880a1dd 2022-06-23 thomas if (err) {
248 9880a1dd 2022-06-23 thomas patch_free(p);
249 069bbb86 2022-03-07 thomas return err;
252 047c926f 2022-06-23 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
253 069bbb86 2022-03-07 thomas switch (imsg.hdr.type) {
254 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_DONE:
255 88c260f4 2022-05-14 thomas if (h != NULL && h->len == 0)
256 88c260f4 2022-05-14 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
257 069bbb86 2022-03-07 thomas goto done;
258 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_HUNK:
259 88c260f4 2022-05-14 thomas if (h != NULL &&
260 88c260f4 2022-05-14 thomas (h->len == 0 || h->old_nonl || h->new_nonl)) {
261 ff7f34d3 2022-03-22 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
262 ff7f34d3 2022-03-22 thomas goto done;
264 656c2baa 2022-04-23 thomas lastmode = -1;
265 069bbb86 2022-03-07 thomas if (datalen != sizeof(hdr)) {
266 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
267 069bbb86 2022-03-07 thomas goto done;
269 069bbb86 2022-03-07 thomas memcpy(&hdr, imsg.data, sizeof(hdr));
270 06227823 2022-06-23 thomas if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
271 06227823 2022-06-23 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
272 eb35d814 2022-06-23 thomas goto done;
274 06227823 2022-06-23 thomas if ((h = calloc(1, sizeof(*h))) == NULL) {
275 06227823 2022-06-23 thomas err = got_error_from_errno("calloc");
276 069bbb86 2022-03-07 thomas goto done;
278 069bbb86 2022-03-07 thomas h->old_from = hdr.oldfrom;
279 069bbb86 2022-03-07 thomas h->old_lines = hdr.oldlines;
280 069bbb86 2022-03-07 thomas h->new_from = hdr.newfrom;
281 069bbb86 2022-03-07 thomas h->new_lines = hdr.newlines;
282 069bbb86 2022-03-07 thomas STAILQ_INSERT_TAIL(&p->head, h, entries);
284 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_LINE:
285 069bbb86 2022-03-07 thomas if (h == NULL) {
286 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
287 069bbb86 2022-03-07 thomas goto done;
289 069bbb86 2022-03-07 thomas t = imsg.data;
290 ff7f34d3 2022-03-22 thomas /* at least one char */
291 069bbb86 2022-03-07 thomas if (datalen < 2 || t[datalen-1] != '\0') {
292 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
293 069bbb86 2022-03-07 thomas goto done;
295 ff7f34d3 2022-03-22 thomas if (*t != ' ' && *t != '-' && *t != '+' &&
296 ff7f34d3 2022-03-22 thomas *t != '\\') {
297 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
298 069bbb86 2022-03-07 thomas goto done;
301 656c2baa 2022-04-23 thomas if (*t != '\\')
302 ff7f34d3 2022-03-22 thomas err = pushline(h, t);
303 656c2baa 2022-04-23 thomas else if (lastmode == '-')
304 656c2baa 2022-04-23 thomas h->old_nonl = 1;
305 656c2baa 2022-04-23 thomas else if (lastmode == '+')
306 656c2baa 2022-04-23 thomas h->new_nonl = 1;
308 656c2baa 2022-04-23 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
311 069bbb86 2022-03-07 thomas goto done;
313 656c2baa 2022-04-23 thomas lastmode = *t;
316 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
317 069bbb86 2022-03-07 thomas goto done;
320 069bbb86 2022-03-07 thomas imsg_free(&imsg);
325 9880a1dd 2022-06-23 thomas patch_free(p);
327 069bbb86 2022-03-07 thomas imsg_free(&imsg);
328 069bbb86 2022-03-07 thomas return err;
331 f4602cbd 2022-07-23 thomas static void
332 f4602cbd 2022-07-23 thomas reverse_patch(struct got_patch *p)
334 f4602cbd 2022-07-23 thomas struct got_patch_hunk *h;
335 f4602cbd 2022-07-23 thomas size_t i;
338 f4602cbd 2022-07-23 thomas STAILQ_FOREACH(h, &p->head, entries) {
339 f4602cbd 2022-07-23 thomas tmp = h->old_from;
340 f4602cbd 2022-07-23 thomas h->old_from = h->new_from;
341 f4602cbd 2022-07-23 thomas h->new_from = tmp;
343 f4602cbd 2022-07-23 thomas tmp = h->old_lines;
344 f4602cbd 2022-07-23 thomas h->old_lines = h->new_lines;
345 f4602cbd 2022-07-23 thomas h->new_lines = tmp;
347 f4602cbd 2022-07-23 thomas tmp = h->old_nonl;
348 f4602cbd 2022-07-23 thomas h->old_nonl = h->new_nonl;
349 f4602cbd 2022-07-23 thomas h->new_nonl = tmp;
351 f4602cbd 2022-07-23 thomas for (i = 0; i < h->len; ++i) {
352 f4602cbd 2022-07-23 thomas if (*h->lines[i] == '+')
353 f4602cbd 2022-07-23 thomas *h->lines[i] = '-';
354 f4602cbd 2022-07-23 thomas else if (*h->lines[i] == '-')
355 f4602cbd 2022-07-23 thomas *h->lines[i] = '+';
361 069bbb86 2022-03-07 thomas * Copy data from orig starting at copypos until pos into tmp.
362 069bbb86 2022-03-07 thomas * If pos is -1, copy until EOF.
364 069bbb86 2022-03-07 thomas static const struct got_error *
365 069bbb86 2022-03-07 thomas copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
367 069bbb86 2022-03-07 thomas char buf[BUFSIZ];
368 069bbb86 2022-03-07 thomas size_t len, r, w;
370 bafaf650 2022-05-14 thomas if (fseeko(orig, copypos, SEEK_SET) == -1)
371 bafaf650 2022-05-14 thomas return got_error_from_errno("fseeko");
373 069bbb86 2022-03-07 thomas while (pos == -1 || copypos < pos) {
374 069bbb86 2022-03-07 thomas len = sizeof(buf);
375 069bbb86 2022-03-07 thomas if (pos > 0)
376 069bbb86 2022-03-07 thomas len = MIN(len, (size_t)pos - copypos);
377 069bbb86 2022-03-07 thomas r = fread(buf, 1, len, orig);
378 069bbb86 2022-03-07 thomas if (r != len && ferror(orig))
379 069bbb86 2022-03-07 thomas return got_error_from_errno("fread");
380 069bbb86 2022-03-07 thomas w = fwrite(buf, 1, r, tmp);
381 069bbb86 2022-03-07 thomas if (w != r)
382 069bbb86 2022-03-07 thomas return got_error_from_errno("fwrite");
383 069bbb86 2022-03-07 thomas copypos += len;
384 069bbb86 2022-03-07 thomas if (r != len && feof(orig)) {
385 069bbb86 2022-03-07 thomas if (pos == -1)
386 069bbb86 2022-03-07 thomas return NULL;
387 49114f01 2022-03-22 thomas return got_error(GOT_ERR_HUNK_FAILED);
390 069bbb86 2022-03-07 thomas return NULL;
393 4f34ed97 2022-08-06 thomas static int linecmp(const char *, const char *, int *);
395 069bbb86 2022-03-07 thomas static const struct got_error *
396 8ebb3daa 2022-06-23 thomas locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
398 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
399 069bbb86 2022-03-07 thomas char *line = NULL;
400 069bbb86 2022-03-07 thomas char mode = *h->lines[0];
401 069bbb86 2022-03-07 thomas size_t linesize = 0;
402 069bbb86 2022-03-07 thomas ssize_t linelen;
403 069bbb86 2022-03-07 thomas off_t match = -1;
404 4f34ed97 2022-08-06 thomas int mangled = 0, match_lineno = -1;
406 069bbb86 2022-03-07 thomas for (;;) {
407 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
408 069bbb86 2022-03-07 thomas if (linelen == -1) {
409 069bbb86 2022-03-07 thomas if (ferror(orig))
410 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
411 069bbb86 2022-03-07 thomas else if (match == -1)
412 49114f01 2022-03-22 thomas err = got_error(GOT_ERR_HUNK_FAILED);
415 ff7f34d3 2022-03-22 thomas if (line[linelen - 1] == '\n')
416 ff7f34d3 2022-03-22 thomas line[linelen - 1] = '\0';
417 069bbb86 2022-03-07 thomas (*lineno)++;
419 4f34ed97 2022-08-06 thomas if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
420 4f34ed97 2022-08-06 thomas (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
421 069bbb86 2022-03-07 thomas (mode == '+' && *lineno == h->old_from)) {
422 069bbb86 2022-03-07 thomas match = ftello(orig);
423 069bbb86 2022-03-07 thomas if (match == -1) {
424 069bbb86 2022-03-07 thomas err = got_error_from_errno("ftello");
427 069bbb86 2022-03-07 thomas match -= linelen;
428 069bbb86 2022-03-07 thomas match_lineno = (*lineno)-1;
431 aaabf83f 2022-08-06 thomas if (*lineno >= h->old_from && match != -1) {
432 aaabf83f 2022-08-06 thomas if (mangled)
433 aaabf83f 2022-08-06 thomas h->ws_mangled = 1;
438 069bbb86 2022-03-07 thomas if (err == NULL) {
439 122e1611 2022-03-22 thomas *pos = match;
440 069bbb86 2022-03-07 thomas *lineno = match_lineno;
441 bafaf650 2022-05-14 thomas if (fseeko(orig, match, SEEK_SET) == -1)
442 bafaf650 2022-05-14 thomas err = got_error_from_errno("fseeko");
445 069bbb86 2022-03-07 thomas free(line);
446 069bbb86 2022-03-07 thomas return err;
449 bb90ca7b 2022-07-03 thomas static int
450 bb90ca7b 2022-07-03 thomas linecmp(const char *a, const char *b, int *mangled)
454 bb90ca7b 2022-07-03 thomas *mangled = 0;
455 bb90ca7b 2022-07-03 thomas c = strcmp(a, b);
456 bb90ca7b 2022-07-03 thomas if (c == 0)
457 bb90ca7b 2022-07-03 thomas return c;
459 bb90ca7b 2022-07-03 thomas *mangled = 1;
460 bb90ca7b 2022-07-03 thomas for (;;) {
461 2140c6ec 2022-07-04 thomas while (*a == '\t' || *a == ' ' || *a == '\f')
463 2140c6ec 2022-07-04 thomas while (*b == '\t' || *b == ' ' || *b == '\f')
465 2140c6ec 2022-07-04 thomas if (*a == '\0' || *a != *b)
467 bb90ca7b 2022-07-03 thomas a++, b++;
470 bb90ca7b 2022-07-03 thomas return *a - *b;
473 069bbb86 2022-03-07 thomas static const struct got_error *
474 069bbb86 2022-03-07 thomas test_hunk(FILE *orig, struct got_patch_hunk *h)
476 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
477 069bbb86 2022-03-07 thomas char *line = NULL;
478 069bbb86 2022-03-07 thomas size_t linesize = 0, i = 0;
479 069bbb86 2022-03-07 thomas ssize_t linelen;
480 bb90ca7b 2022-07-03 thomas int mangled;
482 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i) {
483 069bbb86 2022-03-07 thomas switch (*h->lines[i]) {
484 069bbb86 2022-03-07 thomas case '+':
485 069bbb86 2022-03-07 thomas continue;
486 069bbb86 2022-03-07 thomas case ' ':
487 069bbb86 2022-03-07 thomas case '-':
488 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
489 069bbb86 2022-03-07 thomas if (linelen == -1) {
490 069bbb86 2022-03-07 thomas if (ferror(orig))
491 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
493 069bbb86 2022-03-07 thomas err = got_error(
494 49114f01 2022-03-22 thomas GOT_ERR_HUNK_FAILED);
495 069bbb86 2022-03-07 thomas goto done;
497 ff7f34d3 2022-03-22 thomas if (line[linelen - 1] == '\n')
498 ff7f34d3 2022-03-22 thomas line[linelen - 1] = '\0';
499 bb90ca7b 2022-07-03 thomas if (linecmp(h->lines[i] + 1, line, &mangled)) {
500 49114f01 2022-03-22 thomas err = got_error(GOT_ERR_HUNK_FAILED);
501 069bbb86 2022-03-07 thomas goto done;
503 bb90ca7b 2022-07-03 thomas if (mangled)
504 bb90ca7b 2022-07-03 thomas h->ws_mangled = 1;
510 069bbb86 2022-03-07 thomas free(line);
511 069bbb86 2022-03-07 thomas return err;
514 069bbb86 2022-03-07 thomas static const struct got_error *
515 bb90ca7b 2022-07-03 thomas apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
516 bb90ca7b 2022-07-03 thomas off_t from)
518 bb90ca7b 2022-07-03 thomas const struct got_error *err = NULL;
519 bb90ca7b 2022-07-03 thomas const char *t;
520 bb90ca7b 2022-07-03 thomas size_t linesize = 0, i, new = 0;
521 bb90ca7b 2022-07-03 thomas char *line = NULL;
522 bb90ca7b 2022-07-03 thomas char mode;
523 bb90ca7b 2022-07-03 thomas ssize_t linelen;
525 bb90ca7b 2022-07-03 thomas if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
526 bb90ca7b 2022-07-03 thomas return got_error_from_errno("fseeko");
528 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i) {
529 bb90ca7b 2022-07-03 thomas switch (mode = *h->lines[i]) {
530 069bbb86 2022-03-07 thomas case '-':
531 bb90ca7b 2022-07-03 thomas case ' ':
532 069bbb86 2022-03-07 thomas (*lineno)++;
533 bb90ca7b 2022-07-03 thomas if (orig != NULL) {
534 bb90ca7b 2022-07-03 thomas linelen = getline(&line, &linesize, orig);
535 bb90ca7b 2022-07-03 thomas if (linelen == -1) {
536 bb90ca7b 2022-07-03 thomas err = got_error_from_errno("getline");
537 bb90ca7b 2022-07-03 thomas goto done;
539 bb90ca7b 2022-07-03 thomas if (line[linelen - 1] == '\n')
540 bb90ca7b 2022-07-03 thomas line[linelen - 1] = '\0';
541 bb90ca7b 2022-07-03 thomas t = line;
543 bb90ca7b 2022-07-03 thomas t = h->lines[i] + 1;
544 bb90ca7b 2022-07-03 thomas if (mode == '-')
545 bb90ca7b 2022-07-03 thomas continue;
546 bb90ca7b 2022-07-03 thomas if (fprintf(tmp, "%s\n", t) < 0) {
547 bb90ca7b 2022-07-03 thomas err = got_error_from_errno("fprintf");
548 bb90ca7b 2022-07-03 thomas goto done;
551 069bbb86 2022-03-07 thomas case '+':
553 bb90ca7b 2022-07-03 thomas if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
554 bb90ca7b 2022-07-03 thomas err = got_error_from_errno("fprintf");
555 bb90ca7b 2022-07-03 thomas goto done;
557 656c2baa 2022-04-23 thomas if (new != h->new_lines || !h->new_nonl) {
558 bb90ca7b 2022-07-03 thomas if (fprintf(tmp, "\n") < 0) {
559 bb90ca7b 2022-07-03 thomas err = got_error_from_errno("fprintf");
560 bb90ca7b 2022-07-03 thomas goto done;
568 bb90ca7b 2022-07-03 thomas free(line);
569 bb90ca7b 2022-07-03 thomas return err;
572 46e6bdd4 2022-03-22 thomas static const struct got_error *
573 25a880e1 2022-07-03 thomas patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
575 bb2ad8ff 2022-03-13 thomas const struct got_error *err = NULL;
576 bb2ad8ff 2022-03-13 thomas struct got_patch_hunk *h;
577 da09d8ed 2022-03-22 thomas struct stat sb;
578 8ebb3daa 2022-06-23 thomas int lineno = 0;
579 bb2ad8ff 2022-03-13 thomas off_t copypos, pos;
580 bb2ad8ff 2022-03-13 thomas char *line = NULL;
581 bb2ad8ff 2022-03-13 thomas size_t linesize = 0;
582 bb2ad8ff 2022-03-13 thomas ssize_t linelen;
584 069bbb86 2022-03-07 thomas if (p->old == NULL) { /* create */
585 069bbb86 2022-03-07 thomas h = STAILQ_FIRST(&p->head);
586 bb2ad8ff 2022-03-13 thomas if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
587 bb2ad8ff 2022-03-13 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
588 bb90ca7b 2022-07-03 thomas return apply_hunk(orig, tmp, h, &lineno, 0);
591 650a3405 2023-01-02 thomas /* When deleting binary files there are no hunks to apply. */
592 650a3405 2023-01-02 thomas if (p->new == NULL && STAILQ_EMPTY(&p->head))
593 650a3405 2023-01-02 thomas return NULL;
595 2249fadd 2022-06-23 thomas if (fstat(fileno(orig), &sb) == -1)
596 2249fadd 2022-06-23 thomas return got_error_from_errno("fstat");
598 069bbb86 2022-03-07 thomas copypos = 0;
599 069bbb86 2022-03-07 thomas STAILQ_FOREACH(h, &p->head, entries) {
600 069bbb86 2022-03-07 thomas tryagain:
601 122e1611 2022-03-22 thomas err = locate_hunk(orig, h, &pos, &lineno);
602 49114f01 2022-03-22 thomas if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
603 49114f01 2022-03-22 thomas h->err = err;
604 069bbb86 2022-03-07 thomas if (err != NULL)
605 2249fadd 2022-06-23 thomas return err;
606 2d9b6837 2022-06-23 thomas err = copy(tmp, orig, copypos, pos);
607 069bbb86 2022-03-07 thomas if (err != NULL)
608 2249fadd 2022-06-23 thomas return err;
609 069bbb86 2022-03-07 thomas copypos = pos;
611 069bbb86 2022-03-07 thomas err = test_hunk(orig, h);
612 49114f01 2022-03-22 thomas if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
614 069bbb86 2022-03-07 thomas * try to apply the hunk again starting the search
615 069bbb86 2022-03-07 thomas * after the previous partial match.
617 2249fadd 2022-06-23 thomas if (fseeko(orig, pos, SEEK_SET) == -1)
618 2249fadd 2022-06-23 thomas return got_error_from_errno("fseeko");
619 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
620 2249fadd 2022-06-23 thomas if (linelen == -1)
621 2249fadd 2022-06-23 thomas return got_error_from_errno("getline");
622 069bbb86 2022-03-07 thomas lineno++;
623 069bbb86 2022-03-07 thomas goto tryagain;
625 069bbb86 2022-03-07 thomas if (err != NULL)
626 2249fadd 2022-06-23 thomas return err;
628 49114f01 2022-03-22 thomas if (lineno + 1 != h->old_from)
629 49114f01 2022-03-22 thomas h->offset = lineno + 1 - h->old_from;
631 bb90ca7b 2022-07-03 thomas err = apply_hunk(orig, tmp, h, &lineno, pos);
632 069bbb86 2022-03-07 thomas if (err != NULL)
633 2249fadd 2022-06-23 thomas return err;
635 069bbb86 2022-03-07 thomas copypos = ftello(orig);
636 2249fadd 2022-06-23 thomas if (copypos == -1)
637 2249fadd 2022-06-23 thomas return got_error_from_errno("ftello");
640 49114f01 2022-03-22 thomas if (p->new == NULL && sb.st_size != copypos) {
641 49114f01 2022-03-22 thomas h = STAILQ_FIRST(&p->head);
642 49114f01 2022-03-22 thomas h->err = got_error(GOT_ERR_HUNK_FAILED);
643 49114f01 2022-03-22 thomas err = h->err;
644 2d9b6837 2022-06-23 thomas } else if (!feof(orig))
645 069bbb86 2022-03-07 thomas err = copy(tmp, orig, copypos, -1);
647 10e55613 2022-03-22 thomas return err;
650 10e55613 2022-03-22 thomas static const struct got_error *
651 49114f01 2022-03-22 thomas report_progress(struct patch_args *pa, const char *old, const char *new,
652 49114f01 2022-03-22 thomas unsigned char status, const struct got_error *orig_error)
654 49114f01 2022-03-22 thomas const struct got_error *err;
655 49114f01 2022-03-22 thomas struct got_patch_hunk *h;
657 49114f01 2022-03-22 thomas err = pa->progress_cb(pa->progress_arg, old, new, status,
658 bb90ca7b 2022-07-03 thomas orig_error, 0, 0, 0, 0, 0, 0, NULL);
660 49114f01 2022-03-22 thomas return err;
662 49114f01 2022-03-22 thomas STAILQ_FOREACH(h, pa->head, entries) {
663 bb90ca7b 2022-07-03 thomas if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
664 49114f01 2022-03-22 thomas continue;
666 49114f01 2022-03-22 thomas err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
667 49114f01 2022-03-22 thomas h->old_from, h->old_lines, h->new_from, h->new_lines,
668 bb90ca7b 2022-07-03 thomas h->offset, h->ws_mangled, h->err);
670 49114f01 2022-03-22 thomas return err;
673 49114f01 2022-03-22 thomas return NULL;
676 49114f01 2022-03-22 thomas static const struct got_error *
677 c71da4f7 2022-03-22 thomas patch_delete(void *arg, unsigned char status, unsigned char staged_status,
678 c71da4f7 2022-03-22 thomas const char *path)
680 49114f01 2022-03-22 thomas return report_progress(arg, path, NULL, status, NULL);
683 c71da4f7 2022-03-22 thomas static const struct got_error *
684 c71da4f7 2022-03-22 thomas patch_add(void *arg, unsigned char status, const char *path)
686 49114f01 2022-03-22 thomas return report_progress(arg, NULL, path, status, NULL);
689 c71da4f7 2022-03-22 thomas static const struct got_error *
690 0f76ab83 2022-06-23 thomas open_blob(char **path, FILE **fp, const char *blobid,
691 0f76ab83 2022-06-23 thomas struct got_repository *repo)
693 bb2ad8ff 2022-03-13 thomas const struct got_error *err = NULL;
694 0f76ab83 2022-06-23 thomas struct got_blob_object *blob = NULL;
695 19dd85cb 2022-07-01 thomas struct got_object_id id, *idptr, *matched_id = NULL;
696 f4ae6ddb 2022-07-01 thomas int fd = -1;
698 0f76ab83 2022-06-23 thomas *fp = NULL;
699 0f76ab83 2022-06-23 thomas *path = NULL;
701 19dd85cb 2022-07-01 thomas if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
702 19dd85cb 2022-07-01 thomas err = got_repo_match_object_id(&matched_id, NULL, blobid,
703 19dd85cb 2022-07-01 thomas GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
706 19dd85cb 2022-07-01 thomas return err;
707 19dd85cb 2022-07-01 thomas idptr = matched_id;
709 c8ae092d 2023-02-23 thomas if (!got_parse_object_id(&id, blobid, GOT_HASH_SHA1))
710 19dd85cb 2022-07-01 thomas return got_error(GOT_ERR_BAD_OBJ_ID_STR);
711 19dd85cb 2022-07-01 thomas idptr = &id;
714 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
715 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
716 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
717 f4ae6ddb 2022-07-01 thomas goto done;
720 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
722 0f76ab83 2022-06-23 thomas goto done;
724 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
727 0f76ab83 2022-06-23 thomas goto done;
729 0f76ab83 2022-06-23 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
731 0f76ab83 2022-06-23 thomas goto done;
734 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
735 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
736 0f76ab83 2022-06-23 thomas if (blob)
737 0f76ab83 2022-06-23 thomas got_object_blob_close(blob);
738 19dd85cb 2022-07-01 thomas if (matched_id != NULL)
739 19dd85cb 2022-07-01 thomas free(matched_id);
740 0f76ab83 2022-06-23 thomas if (err) {
741 0f76ab83 2022-06-23 thomas if (*fp != NULL)
742 0f76ab83 2022-06-23 thomas fclose(*fp);
743 0f76ab83 2022-06-23 thomas if (*path != NULL)
744 0f76ab83 2022-06-23 thomas unlink(*path);
745 0f76ab83 2022-06-23 thomas free(*path);
746 0f76ab83 2022-06-23 thomas *fp = NULL;
747 0f76ab83 2022-06-23 thomas *path = NULL;
749 0f76ab83 2022-06-23 thomas return err;
752 0f76ab83 2022-06-23 thomas static const struct got_error *
753 7d8bcb99 2022-07-28 thomas prepare_merge(int *do_merge, char **apath, FILE **afile,
754 7d8bcb99 2022-07-28 thomas struct got_worktree *worktree, struct got_repository *repo,
755 7d8bcb99 2022-07-28 thomas struct got_patch *p, struct got_object_id *commit_id,
756 7d8bcb99 2022-07-28 thomas struct got_tree_object *tree, const char *path)
758 7d8bcb99 2022-07-28 thomas const struct got_error *err = NULL;
760 7d8bcb99 2022-07-28 thomas *do_merge = 0;
761 7d8bcb99 2022-07-28 thomas *apath = NULL;
762 7d8bcb99 2022-07-28 thomas *afile = NULL;
764 7d8bcb99 2022-07-28 thomas /* don't run the diff3 merge on creations/deletions */
765 7d8bcb99 2022-07-28 thomas if (p->old == NULL || p->new == NULL)
766 7d8bcb99 2022-07-28 thomas return NULL;
768 7d8bcb99 2022-07-28 thomas if (commit_id) {
769 7d8bcb99 2022-07-28 thomas struct got_object_id *id;
771 7d8bcb99 2022-07-28 thomas err = got_object_tree_find_path(&id, NULL, repo, tree, path);
773 7d8bcb99 2022-07-28 thomas return err;
774 7d8bcb99 2022-07-28 thomas got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
775 7d8bcb99 2022-07-28 thomas got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
776 7d8bcb99 2022-07-28 thomas free(id);
777 7d8bcb99 2022-07-28 thomas err = open_blob(apath, afile, p->blob, repo);
778 7d8bcb99 2022-07-28 thomas *do_merge = err == NULL;
779 7d8bcb99 2022-07-28 thomas } else if (*p->blob != '\0') {
780 7d8bcb99 2022-07-28 thomas err = open_blob(apath, afile, p->blob, repo);
782 7d8bcb99 2022-07-28 thomas * ignore failures to open this blob, we might have
783 7d8bcb99 2022-07-28 thomas * parsed gibberish.
785 7d8bcb99 2022-07-28 thomas if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
786 7d8bcb99 2022-07-28 thomas err->code != GOT_ERR_NO_OBJ)
787 7d8bcb99 2022-07-28 thomas return err;
788 7d8bcb99 2022-07-28 thomas *do_merge = err == NULL;
789 7d8bcb99 2022-07-28 thomas err = NULL;
792 7d8bcb99 2022-07-28 thomas return err;
795 7d8bcb99 2022-07-28 thomas static const struct got_error *
796 0f76ab83 2022-06-23 thomas apply_patch(int *overlapcnt, struct got_worktree *worktree,
797 0f76ab83 2022-06-23 thomas struct got_repository *repo, struct got_fileindex *fileindex,
798 0f76ab83 2022-06-23 thomas const char *old, const char *new, struct got_patch *p, int nop,
799 7d8bcb99 2022-07-28 thomas int reverse, struct got_object_id *commit_id,
800 7d8bcb99 2022-07-28 thomas struct got_tree_object *tree, struct patch_args *pa,
801 f4602cbd 2022-07-23 thomas got_cancel_cb cancel_cb, void *cancel_arg)
803 0f76ab83 2022-06-23 thomas const struct got_error *err = NULL;
804 25a880e1 2022-07-03 thomas struct stat sb;
805 0f76ab83 2022-06-23 thomas int do_merge = 0, file_renamed = 0;
806 016bfe4b 2022-06-23 thomas char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
807 72f46891 2022-04-23 thomas char *oldpath = NULL, *newpath = NULL;
808 dd73a53d 2022-10-16 thomas char *tmppath = NULL, *template = NULL;
809 0f76ab83 2022-06-23 thomas char *apath = NULL, *mergepath = NULL;
810 0f76ab83 2022-06-23 thomas FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
811 0f76ab83 2022-06-23 thomas int outfd;
812 da09d8ed 2022-03-22 thomas mode_t mode = GOT_DEFAULT_FILE_MODE;
814 0f76ab83 2022-06-23 thomas *overlapcnt = 0;
816 7d8bcb99 2022-07-28 thomas err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
817 7d8bcb99 2022-07-28 thomas commit_id, tree, old);
819 7d8bcb99 2022-07-28 thomas return err;
821 21948212 2022-07-27 thomas if (reverse && !do_merge)
822 21948212 2022-07-27 thomas reverse_patch(p);
824 72f46891 2022-04-23 thomas if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
825 72f46891 2022-04-23 thomas old) == -1) {
826 72f46891 2022-04-23 thomas err = got_error_from_errno("asprintf");
827 814624e7 2022-03-22 thomas goto done;
830 72f46891 2022-04-23 thomas if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
831 72f46891 2022-04-23 thomas new) == -1) {
832 72f46891 2022-04-23 thomas err = got_error_from_errno("asprintf");
833 72f46891 2022-04-23 thomas goto done;
836 814624e7 2022-03-22 thomas file_renamed = strcmp(oldpath, newpath);
838 bb2ad8ff 2022-03-13 thomas if (asprintf(&template, "%s/got-patch",
839 bb2ad8ff 2022-03-13 thomas got_worktree_get_root_path(worktree)) == -1) {
840 bb2ad8ff 2022-03-13 thomas err = got_error_from_errno(template);
841 069bbb86 2022-03-07 thomas goto done;
844 25a880e1 2022-07-03 thomas if (p->old != NULL) {
845 25a880e1 2022-07-03 thomas if ((oldfile = fopen(oldpath, "r")) == NULL) {
846 25a880e1 2022-07-03 thomas err = got_error_from_errno2("open", oldpath);
847 25a880e1 2022-07-03 thomas goto done;
849 25a880e1 2022-07-03 thomas if (fstat(fileno(oldfile), &sb) == -1) {
850 25a880e1 2022-07-03 thomas err = got_error_from_errno2("fstat", oldpath);
851 25a880e1 2022-07-03 thomas goto done;
853 25a880e1 2022-07-03 thomas mode = sb.st_mode;
854 37e766f4 2022-09-21 thomas } else if (p->xbit)
855 37e766f4 2022-09-21 thomas mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
857 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpfile, template, "");
859 bb2ad8ff 2022-03-13 thomas goto done;
860 0f76ab83 2022-06-23 thomas outfd = fileno(tmpfile);
861 25a880e1 2022-07-03 thomas err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
863 bb2ad8ff 2022-03-13 thomas goto done;
865 0f76ab83 2022-06-23 thomas if (do_merge) {
866 8afec5d5 2022-07-01 thomas const char *type, *id;
868 0f76ab83 2022-06-23 thomas if (fseeko(afile, 0, SEEK_SET) == -1 ||
869 0f76ab83 2022-06-23 thomas fseeko(oldfile, 0, SEEK_SET) == -1 ||
870 0f76ab83 2022-06-23 thomas fseeko(tmpfile, 0, SEEK_SET) == -1) {
871 0f76ab83 2022-06-23 thomas err = got_error_from_errno("fseeko");
872 0f76ab83 2022-06-23 thomas goto done;
875 0f76ab83 2022-06-23 thomas if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
876 0f76ab83 2022-06-23 thomas err = got_error_from_errno("asprintf");
877 0f76ab83 2022-06-23 thomas oldlabel = NULL;
878 0f76ab83 2022-06-23 thomas goto done;
881 0f76ab83 2022-06-23 thomas if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
882 0f76ab83 2022-06-23 thomas err = got_error_from_errno("asprintf");
883 0f76ab83 2022-06-23 thomas newlabel = NULL;
884 0f76ab83 2022-06-23 thomas goto done;
887 8afec5d5 2022-07-01 thomas if (*p->cid != '\0') {
888 8afec5d5 2022-07-01 thomas type = "commit";
889 8afec5d5 2022-07-01 thomas id = p->cid;
891 8afec5d5 2022-07-01 thomas type = "blob";
892 8afec5d5 2022-07-01 thomas id = p->blob;
895 8afec5d5 2022-07-01 thomas if (asprintf(&anclabel, "%s %s", type, id) == -1) {
896 016bfe4b 2022-06-23 thomas err = got_error_from_errno("asprintf");
897 016bfe4b 2022-06-23 thomas anclabel = NULL;
898 016bfe4b 2022-06-23 thomas goto done;
901 f4602cbd 2022-07-23 thomas if (reverse) {
905 f4602cbd 2022-07-23 thomas s = anclabel;
906 f4602cbd 2022-07-23 thomas anclabel = newlabel;
907 f4602cbd 2022-07-23 thomas newlabel = s;
909 f4602cbd 2022-07-23 thomas t = afile;
910 f4602cbd 2022-07-23 thomas afile = tmpfile;
911 f4602cbd 2022-07-23 thomas tmpfile = t;
914 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&mergepath, &mergefile, template, "");
916 0f76ab83 2022-06-23 thomas goto done;
917 0f76ab83 2022-06-23 thomas outfd = fileno(mergefile);
919 0f76ab83 2022-06-23 thomas err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
920 016bfe4b 2022-06-23 thomas oldfile, oldlabel, anclabel, newlabel,
921 0f76ab83 2022-06-23 thomas GOT_DIFF_ALGORITHM_PATIENCE);
923 0f76ab83 2022-06-23 thomas goto done;
927 eaf99875 2022-03-22 thomas goto done;
929 eaf99875 2022-03-22 thomas if (p->old != NULL && p->new == NULL) {
930 5e22b737 2022-05-31 thomas err = got_worktree_patch_schedule_rm(old, repo, worktree,
931 5e22b737 2022-05-31 thomas fileindex, patch_delete, pa);
932 eaf99875 2022-03-22 thomas goto done;
935 a2c162eb 2022-10-30 thomas if (fchmod(outfd, apply_umask(mode)) == -1) {
936 8260acc8 2022-05-01 thomas err = got_error_from_errno2("chmod", tmppath);
937 da09d8ed 2022-03-22 thomas goto done;
940 dd73a53d 2022-10-16 thomas if (mergepath) {
941 dd73a53d 2022-10-16 thomas err = got_path_move_file(mergepath, newpath);
943 e0c1f81c 2022-03-22 thomas goto done;
944 dd73a53d 2022-10-16 thomas free(mergepath);
945 dd73a53d 2022-10-16 thomas mergepath = NULL;
947 dd73a53d 2022-10-16 thomas err = got_path_move_file(tmppath, newpath);
949 e0c1f81c 2022-03-22 thomas goto done;
950 dd73a53d 2022-10-16 thomas free(tmppath);
951 dd73a53d 2022-10-16 thomas tmppath = NULL;
954 bb2ad8ff 2022-03-13 thomas if (file_renamed) {
955 5e22b737 2022-05-31 thomas err = got_worktree_patch_schedule_rm(old, repo, worktree,
956 5e22b737 2022-05-31 thomas fileindex, patch_delete, pa);
957 bb2ad8ff 2022-03-13 thomas if (err == NULL)
958 5e22b737 2022-05-31 thomas err = got_worktree_patch_schedule_add(new, repo,
959 5e22b737 2022-05-31 thomas worktree, fileindex, patch_add,
962 814624e7 2022-03-22 thomas unlink(newpath);
963 814624e7 2022-03-22 thomas } else if (p->old == NULL) {
964 5e22b737 2022-05-31 thomas err = got_worktree_patch_schedule_add(new, repo, worktree,
965 5e22b737 2022-05-31 thomas fileindex, patch_add, pa);
967 814624e7 2022-03-22 thomas unlink(newpath);
968 0f76ab83 2022-06-23 thomas } else if (*overlapcnt != 0)
969 0f76ab83 2022-06-23 thomas err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
970 762b8e82 2022-06-23 thomas else if (do_merge)
971 762b8e82 2022-06-23 thomas err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
973 72f46891 2022-04-23 thomas err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
976 94af5a06 2022-03-08 thomas free(template);
978 dd73a53d 2022-10-16 thomas if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
979 dd73a53d 2022-10-16 thomas err = got_error_from_errno("unlink");
980 0f76ab83 2022-06-23 thomas if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
981 04cdf6ac 2022-06-23 thomas err = got_error_from_errno("fclose");
982 069bbb86 2022-03-07 thomas free(tmppath);
984 72f46891 2022-04-23 thomas free(oldpath);
985 2249fadd 2022-06-23 thomas if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
986 2249fadd 2022-06-23 thomas err = got_error_from_errno("fclose");
988 dd73a53d 2022-10-16 thomas if (apath != NULL && unlink(apath) == -1 && err == NULL)
989 dd73a53d 2022-10-16 thomas err = got_error_from_errno("unlink");
990 0f76ab83 2022-06-23 thomas if (afile != NULL && fclose(afile) == EOF && err == NULL)
991 0f76ab83 2022-06-23 thomas err = got_error_from_errno("fclose");
992 0f76ab83 2022-06-23 thomas free(apath);
994 dd73a53d 2022-10-16 thomas if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
995 dd73a53d 2022-10-16 thomas err = got_error_from_errno("unlink");
996 0f76ab83 2022-06-23 thomas if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
997 0f76ab83 2022-06-23 thomas err = got_error_from_errno("fclose");
998 0f76ab83 2022-06-23 thomas free(mergepath);
1000 72f46891 2022-04-23 thomas free(newpath);
1001 0f76ab83 2022-06-23 thomas free(oldlabel);
1002 0f76ab83 2022-06-23 thomas free(newlabel);
1003 016bfe4b 2022-06-23 thomas free(anclabel);
1004 069bbb86 2022-03-07 thomas return err;
1007 069bbb86 2022-03-07 thomas const struct got_error *
1008 069bbb86 2022-03-07 thomas got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1009 7d8bcb99 2022-07-28 thomas int nop, int strip, int reverse, struct got_object_id *commit_id,
1010 7d8bcb99 2022-07-28 thomas got_patch_progress_cb progress_cb, void *progress_arg,
1011 7d8bcb99 2022-07-28 thomas got_cancel_cb cancel_cb, void *cancel_arg)
1013 36832a8e 2022-05-31 thomas const struct got_error *err = NULL, *complete_err = NULL;
1014 814624e7 2022-03-22 thomas struct got_fileindex *fileindex = NULL;
1015 7d8bcb99 2022-07-28 thomas struct got_commit_object *commit = NULL;
1016 7d8bcb99 2022-07-28 thomas struct got_tree_object *tree = NULL;
1017 5e22b737 2022-05-31 thomas char *fileindex_path = NULL;
1018 49114f01 2022-03-22 thomas char *oldpath, *newpath;
1019 069bbb86 2022-03-07 thomas struct imsgbuf *ibuf;
1020 069bbb86 2022-03-07 thomas int imsg_fds[2] = {-1, -1};
1021 0f76ab83 2022-06-23 thomas int overlapcnt, done = 0, failed = 0;
1022 069bbb86 2022-03-07 thomas pid_t pid;
1024 069bbb86 2022-03-07 thomas ibuf = calloc(1, sizeof(*ibuf));
1025 069bbb86 2022-03-07 thomas if (ibuf == NULL) {
1026 069bbb86 2022-03-07 thomas err = got_error_from_errno("calloc");
1027 069bbb86 2022-03-07 thomas goto done;
1030 069bbb86 2022-03-07 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1031 069bbb86 2022-03-07 thomas err = got_error_from_errno("socketpair");
1032 069bbb86 2022-03-07 thomas goto done;
1035 069bbb86 2022-03-07 thomas pid = fork();
1036 069bbb86 2022-03-07 thomas if (pid == -1) {
1037 069bbb86 2022-03-07 thomas err = got_error_from_errno("fork");
1038 069bbb86 2022-03-07 thomas goto done;
1039 069bbb86 2022-03-07 thomas } else if (pid == 0) {
1040 069bbb86 2022-03-07 thomas got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1042 069bbb86 2022-03-07 thomas /* not reached */
1045 069bbb86 2022-03-07 thomas if (close(imsg_fds[1]) == -1) {
1046 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
1047 069bbb86 2022-03-07 thomas goto done;
1049 069bbb86 2022-03-07 thomas imsg_fds[1] = -1;
1050 069bbb86 2022-03-07 thomas imsg_init(ibuf, imsg_fds[0]);
1052 069bbb86 2022-03-07 thomas err = send_patch(ibuf, fd);
1053 069bbb86 2022-03-07 thomas fd = -1;
1054 069bbb86 2022-03-07 thomas if (err)
1055 069bbb86 2022-03-07 thomas goto done;
1057 5e22b737 2022-05-31 thomas err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1058 5e22b737 2022-05-31 thomas worktree);
1059 814624e7 2022-03-22 thomas if (err)
1060 814624e7 2022-03-22 thomas goto done;
1062 7d8bcb99 2022-07-28 thomas if (commit_id) {
1063 7d8bcb99 2022-07-28 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
1064 7d8bcb99 2022-07-28 thomas if (err)
1065 7d8bcb99 2022-07-28 thomas goto done;
1067 7d8bcb99 2022-07-28 thomas err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1068 7d8bcb99 2022-07-28 thomas if (err)
1069 7d8bcb99 2022-07-28 thomas goto done;
1072 069bbb86 2022-03-07 thomas while (!done && err == NULL) {
1073 069bbb86 2022-03-07 thomas struct got_patch p;
1074 49114f01 2022-03-22 thomas struct patch_args pa;
1076 49114f01 2022-03-22 thomas pa.progress_cb = progress_cb;
1077 49114f01 2022-03-22 thomas pa.progress_arg = progress_arg;
1078 49114f01 2022-03-22 thomas pa.head = &p.head;
1080 d9db2ff9 2022-04-16 thomas err = recv_patch(ibuf, &done, &p, strip);
1081 069bbb86 2022-03-07 thomas if (err || done)
1084 814624e7 2022-03-22 thomas err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1085 814624e7 2022-03-22 thomas &newpath, worktree, repo, fileindex);
1086 814624e7 2022-03-22 thomas if (err == NULL)
1087 0f76ab83 2022-06-23 thomas err = apply_patch(&overlapcnt, worktree, repo,
1088 f4602cbd 2022-07-23 thomas fileindex, oldpath, newpath, &p, nop, reverse,
1089 7d8bcb99 2022-07-28 thomas commit_id, tree, &pa, cancel_cb, cancel_arg);
1090 49114f01 2022-03-22 thomas if (err != NULL) {
1091 49114f01 2022-03-22 thomas failed = 1;
1092 49114f01 2022-03-22 thomas /* recoverable errors */
1093 49114f01 2022-03-22 thomas if (err->code == GOT_ERR_FILE_STATUS ||
1094 49114f01 2022-03-22 thomas (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1095 49114f01 2022-03-22 thomas err = report_progress(&pa, p.old, p.new,
1096 49114f01 2022-03-22 thomas GOT_STATUS_CANNOT_UPDATE, err);
1097 49114f01 2022-03-22 thomas else if (err->code == GOT_ERR_HUNK_FAILED)
1098 49114f01 2022-03-22 thomas err = report_progress(&pa, p.old, p.new,
1099 49114f01 2022-03-22 thomas GOT_STATUS_CANNOT_UPDATE, NULL);
1101 0f76ab83 2022-06-23 thomas if (overlapcnt != 0)
1102 0f76ab83 2022-06-23 thomas failed = 1;
1104 49114f01 2022-03-22 thomas free(oldpath);
1105 49114f01 2022-03-22 thomas free(newpath);
1106 069bbb86 2022-03-07 thomas patch_free(&p);
1108 069bbb86 2022-03-07 thomas if (err)
1113 36832a8e 2022-05-31 thomas if (fileindex != NULL)
1114 36832a8e 2022-05-31 thomas complete_err = got_worktree_patch_complete(fileindex,
1115 36832a8e 2022-05-31 thomas fileindex_path);
1116 5e22b737 2022-05-31 thomas if (complete_err && err == NULL)
1117 5e22b737 2022-05-31 thomas err = complete_err;
1118 36832a8e 2022-05-31 thomas free(fileindex_path);
1119 7d8bcb99 2022-07-28 thomas if (tree)
1120 7d8bcb99 2022-07-28 thomas got_object_tree_close(tree);
1121 7d8bcb99 2022-07-28 thomas if (commit)
1122 7d8bcb99 2022-07-28 thomas got_object_commit_close(commit);
1123 069bbb86 2022-03-07 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
1124 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
1125 069bbb86 2022-03-07 thomas if (ibuf != NULL)
1126 069bbb86 2022-03-07 thomas imsg_clear(ibuf);
1127 069bbb86 2022-03-07 thomas if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1128 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
1129 069bbb86 2022-03-07 thomas if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1130 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
1131 49114f01 2022-03-22 thomas if (err == NULL && failed)
1132 49114f01 2022-03-22 thomas err = got_error(GOT_ERR_PATCH_FAILED);
1133 069bbb86 2022-03-07 thomas return err;