2 e9ce266e 2022-03-07 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 e9ce266e 2022-03-07 op * Permission to use, copy, modify, and distribute this software for any
5 e9ce266e 2022-03-07 op * purpose with or without fee is hereby granted, provided that the above
6 e9ce266e 2022-03-07 op * copyright notice and this permission notice appear in all copies.
8 e9ce266e 2022-03-07 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 e9ce266e 2022-03-07 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 e9ce266e 2022-03-07 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 e9ce266e 2022-03-07 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 e9ce266e 2022-03-07 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 e9ce266e 2022-03-07 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 e9ce266e 2022-03-07 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 e9ce266e 2022-03-07 op * Apply patches.
18 e9ce266e 2022-03-07 op * Things that we may want to support:
19 e9ce266e 2022-03-07 op * + support indented patches?
20 e9ce266e 2022-03-07 op * + support other kinds of patches?
23 e9ce266e 2022-03-07 op #include <sys/types.h>
24 e9ce266e 2022-03-07 op #include <sys/queue.h>
25 e9ce266e 2022-03-07 op #include <sys/socket.h>
26 5b67f96e 2022-03-13 op #include <sys/stat.h>
27 e9ce266e 2022-03-07 op #include <sys/uio.h>
29 a92a2042 2022-07-02 op #include <ctype.h>
30 dbda770b 2022-03-13 op #include <errno.h>
31 e9ce266e 2022-03-07 op #include <limits.h>
32 e9ce266e 2022-03-07 op #include <sha1.h>
33 5822e79e 2023-02-23 op #include <sha2.h>
34 e9ce266e 2022-03-07 op #include <stdint.h>
35 e9ce266e 2022-03-07 op #include <stdio.h>
36 e9ce266e 2022-03-07 op #include <stdlib.h>
37 e9ce266e 2022-03-07 op #include <string.h>
38 e9ce266e 2022-03-07 op #include <unistd.h>
39 e9ce266e 2022-03-07 op #include <imsg.h>
41 e9ce266e 2022-03-07 op #include "got_error.h"
42 e9ce266e 2022-03-07 op #include "got_object.h"
43 e9ce266e 2022-03-07 op #include "got_path.h"
44 e9ce266e 2022-03-07 op #include "got_reference.h"
45 e9ce266e 2022-03-07 op #include "got_cancel.h"
46 e9ce266e 2022-03-07 op #include "got_worktree.h"
47 55e9459f 2022-06-19 op #include "got_repository.h"
48 e9ce266e 2022-03-07 op #include "got_opentemp.h"
49 e9ce266e 2022-03-07 op #include "got_patch.h"
50 4b752015 2022-06-30 stsp #include "got_diff.h"
52 e9ce266e 2022-03-07 op #include "got_lib_delta.h"
53 55e9459f 2022-06-19 op #include "got_lib_diff.h"
54 e9ce266e 2022-03-07 op #include "got_lib_object.h"
55 e9ce266e 2022-03-07 op #include "got_lib_privsep.h"
56 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
58 038b5b12 2023-03-01 naddy #ifndef MIN
59 e9ce266e 2022-03-07 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
62 41208069 2023-10-23 op struct got_patch_line {
68 e9ce266e 2022-03-07 op struct got_patch_hunk {
69 e9ce266e 2022-03-07 op STAILQ_ENTRY(got_patch_hunk) entries;
70 60aa1fa0 2022-03-17 op const struct got_error *err;
71 a92a2042 2022-07-02 op int ws_mangled;
76 35095610 2022-06-14 op int old_lines;
78 35095610 2022-06-14 op int new_lines;
81 41208069 2023-10-23 op struct got_patch_line *lines;
84 60aa1fa0 2022-03-17 op STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
85 e9ce266e 2022-03-07 op struct got_patch {
90 55e9459f 2022-06-19 op char blob[41];
91 60aa1fa0 2022-03-17 op struct got_patch_hunk_head head;
94 b22138f5 2022-03-16 op struct patch_args {
95 b22138f5 2022-03-16 op got_patch_progress_cb progress_cb;
96 b22138f5 2022-03-16 op void *progress_arg;
97 60aa1fa0 2022-03-17 op struct got_patch_hunk_head *head;
100 b2b3fce1 2022-10-29 op static mode_t
101 b2b3fce1 2022-10-29 op apply_umask(mode_t mode)
105 b2b3fce1 2022-10-29 op um = umask(000);
107 b2b3fce1 2022-10-29 op return mode & ~um;
110 e9ce266e 2022-03-07 op static const struct got_error *
111 e9ce266e 2022-03-07 op send_patch(struct imsgbuf *ibuf, int fd)
113 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
115 e9ce266e 2022-03-07 op if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
116 e9ce266e 2022-03-07 op NULL, 0) == -1) {
117 e9ce266e 2022-03-07 op err = got_error_from_errno(
118 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_FILE");
123 08f44789 2022-07-07 op return got_privsep_flush_imsg(ibuf);
127 e9ce266e 2022-03-07 op patch_free(struct got_patch *p)
129 e9ce266e 2022-03-07 op struct got_patch_hunk *h;
132 e9ce266e 2022-03-07 op while (!STAILQ_EMPTY(&p->head)) {
133 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
134 e9ce266e 2022-03-07 op STAILQ_REMOVE_HEAD(&p->head, entries);
136 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i)
137 41208069 2023-10-23 op free(h->lines[i].line);
138 e9ce266e 2022-03-07 op free(h->lines);
142 e9ce266e 2022-03-07 op free(p->new);
143 e9ce266e 2022-03-07 op free(p->old);
145 0e07a2a1 2022-06-13 op memset(p, 0, sizeof(*p));
146 0e07a2a1 2022-06-13 op STAILQ_INIT(&p->head);
149 e9ce266e 2022-03-07 op static const struct got_error *
150 41208069 2023-10-23 op pushline(struct got_patch_hunk *h, const char *line, size_t len)
153 e9ce266e 2022-03-07 op size_t newcap;
155 e9ce266e 2022-03-07 op if (h->len == h->cap) {
156 e9ce266e 2022-03-07 op if ((newcap = h->cap * 1.5) == 0)
158 e9ce266e 2022-03-07 op t = recallocarray(h->lines, h->cap, newcap,
159 e9ce266e 2022-03-07 op sizeof(h->lines[0]));
160 e9ce266e 2022-03-07 op if (t == NULL)
161 e9ce266e 2022-03-07 op return got_error_from_errno("recallocarray");
162 e9ce266e 2022-03-07 op h->lines = t;
163 e9ce266e 2022-03-07 op h->cap = newcap;
166 41208069 2023-10-23 op if ((t = malloc(len - 1)) == NULL)
167 41208069 2023-10-23 op return got_error_from_errno("malloc");
168 41208069 2023-10-23 op memcpy(t, line + 1, len - 1); /* skip the line type */
170 41208069 2023-10-23 op h->lines[h->len].mode = *line;
171 41208069 2023-10-23 op h->lines[h->len].line = t;
172 41208069 2023-10-23 op h->lines[h->len].len = len - 2; /* line type and trailing NUL */
177 e9ce266e 2022-03-07 op static const struct got_error *
178 9d6cabd5 2022-04-07 op recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
180 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
181 e9ce266e 2022-03-07 op struct imsg imsg;
182 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
183 e9ce266e 2022-03-07 op struct got_imsg_patch patch;
184 e9ce266e 2022-03-07 op struct got_patch_hunk *h = NULL;
185 e9ce266e 2022-03-07 op size_t datalen;
186 b2832778 2022-04-23 op int lastmode = -1;
188 e9ce266e 2022-03-07 op memset(p, 0, sizeof(*p));
189 e9ce266e 2022-03-07 op STAILQ_INIT(&p->head);
191 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
194 e9ce266e 2022-03-07 op if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
198 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH) {
199 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
202 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
203 e9ce266e 2022-03-07 op if (datalen != sizeof(patch)) {
204 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
207 e9ce266e 2022-03-07 op memcpy(&patch, imsg.data, sizeof(patch));
209 99b94ad7 2022-06-13 op if (patch.old[sizeof(patch.old)-1] != '\0' ||
210 55e9459f 2022-06-19 op patch.new[sizeof(patch.new)-1] != '\0' ||
211 d8b5af43 2022-06-19 op patch.cid[sizeof(patch.cid)-1] != '\0' ||
212 55e9459f 2022-06-19 op patch.blob[sizeof(patch.blob)-1] != '\0') {
213 99b94ad7 2022-06-13 op err = got_error(GOT_ERR_PRIVSEP_LEN);
217 497a5915 2022-06-27 op if (*patch.cid != '\0')
218 d8b5af43 2022-06-19 op strlcpy(p->cid, patch.cid, sizeof(p->cid));
220 497a5915 2022-06-27 op if (*patch.blob != '\0')
221 d8b5af43 2022-06-19 op strlcpy(p->blob, patch.blob, sizeof(p->blob));
223 611e5fc2 2022-09-21 mark p->xbit = patch.xbit;
225 9d6cabd5 2022-04-07 op /* automatically set strip=1 for git-style diffs */
226 9d6cabd5 2022-04-07 op if (strip == -1 && patch.git &&
227 9d6cabd5 2022-04-07 op (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
228 9d6cabd5 2022-04-07 op (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
231 9d6cabd5 2022-04-07 op /* prefer the new name if not /dev/null for not git-style diffs */
232 9d6cabd5 2022-04-07 op if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
233 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.new, strip);
236 9d6cabd5 2022-04-07 op } else if (*patch.old != '\0') {
237 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.old, strip);
242 9d6cabd5 2022-04-07 op if (*patch.new != '\0') {
243 9d6cabd5 2022-04-07 op err = got_path_strip(&p->new, patch.new, strip);
248 b95c53df 2022-03-12 op if (p->old == NULL && p->new == NULL) {
249 b95c53df 2022-03-12 op err = got_error(GOT_ERR_PATCH_MALFORMED);
253 e9ce266e 2022-03-07 op imsg_free(&imsg);
258 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
260 0e07a2a1 2022-06-13 op patch_free(p);
264 2399b53d 2022-06-14 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
265 e9ce266e 2022-03-07 op switch (imsg.hdr.type) {
266 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_DONE:
267 be33dff7 2022-05-13 op if (h != NULL && h->len == 0)
268 be33dff7 2022-05-13 op err = got_error(GOT_ERR_PATCH_MALFORMED);
270 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_HUNK:
271 be33dff7 2022-05-13 op if (h != NULL &&
272 be33dff7 2022-05-13 op (h->len == 0 || h->old_nonl || h->new_nonl)) {
273 b3c57ab2 2022-03-22 op err = got_error(GOT_ERR_PATCH_MALFORMED);
276 b2832778 2022-04-23 op lastmode = -1;
277 e9ce266e 2022-03-07 op if (datalen != sizeof(hdr)) {
278 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
281 e9ce266e 2022-03-07 op memcpy(&hdr, imsg.data, sizeof(hdr));
282 5b7126c5 2022-06-14 op if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
283 5b7126c5 2022-06-14 op err = got_error(GOT_ERR_PRIVSEP_LEN);
286 5b7126c5 2022-06-14 op if ((h = calloc(1, sizeof(*h))) == NULL) {
287 5b7126c5 2022-06-14 op err = got_error_from_errno("calloc");
290 e9ce266e 2022-03-07 op h->old_from = hdr.oldfrom;
291 e9ce266e 2022-03-07 op h->old_lines = hdr.oldlines;
292 e9ce266e 2022-03-07 op h->new_from = hdr.newfrom;
293 e9ce266e 2022-03-07 op h->new_lines = hdr.newlines;
294 e9ce266e 2022-03-07 op STAILQ_INSERT_TAIL(&p->head, h, entries);
296 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_LINE:
297 e9ce266e 2022-03-07 op if (h == NULL) {
298 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
301 e9ce266e 2022-03-07 op t = imsg.data;
302 b3c57ab2 2022-03-22 op /* at least one char */
303 e9ce266e 2022-03-07 op if (datalen < 2 || t[datalen-1] != '\0') {
304 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
307 b3c57ab2 2022-03-22 op if (*t != ' ' && *t != '-' && *t != '+' &&
308 b3c57ab2 2022-03-22 op *t != '\\') {
309 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
313 b2832778 2022-04-23 op if (*t != '\\')
314 41208069 2023-10-23 op err = pushline(h, t, datalen);
315 b2832778 2022-04-23 op else if (lastmode == '-')
316 b2832778 2022-04-23 op h->old_nonl = 1;
317 b2832778 2022-04-23 op else if (lastmode == '+')
318 b2832778 2022-04-23 op h->new_nonl = 1;
320 b2832778 2022-04-23 op err = got_error(GOT_ERR_PATCH_MALFORMED);
325 b2832778 2022-04-23 op lastmode = *t;
328 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
332 e9ce266e 2022-03-07 op imsg_free(&imsg);
337 0e07a2a1 2022-06-13 op patch_free(p);
339 e9ce266e 2022-03-07 op imsg_free(&imsg);
344 38d61ead 2022-07-23 op reverse_patch(struct got_patch *p)
346 38d61ead 2022-07-23 op struct got_patch_hunk *h;
350 38d61ead 2022-07-23 op STAILQ_FOREACH(h, &p->head, entries) {
351 38d61ead 2022-07-23 op tmp = h->old_from;
352 38d61ead 2022-07-23 op h->old_from = h->new_from;
353 38d61ead 2022-07-23 op h->new_from = tmp;
355 38d61ead 2022-07-23 op tmp = h->old_lines;
356 38d61ead 2022-07-23 op h->old_lines = h->new_lines;
357 38d61ead 2022-07-23 op h->new_lines = tmp;
359 38d61ead 2022-07-23 op tmp = h->old_nonl;
360 38d61ead 2022-07-23 op h->old_nonl = h->new_nonl;
361 38d61ead 2022-07-23 op h->new_nonl = tmp;
363 38d61ead 2022-07-23 op for (i = 0; i < h->len; ++i) {
364 41208069 2023-10-23 op if (h->lines[i].mode == '+')
365 41208069 2023-10-23 op h->lines[i].mode = '-';
366 41208069 2023-10-23 op else if (h->lines[i].mode == '-')
367 41208069 2023-10-23 op h->lines[i].mode = '+';
373 e9ce266e 2022-03-07 op * Copy data from orig starting at copypos until pos into tmp.
374 e9ce266e 2022-03-07 op * If pos is -1, copy until EOF.
376 e9ce266e 2022-03-07 op static const struct got_error *
377 e9ce266e 2022-03-07 op copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
379 e9ce266e 2022-03-07 op char buf[BUFSIZ];
380 e9ce266e 2022-03-07 op size_t len, r, w;
382 e45f7eba 2022-05-14 naddy if (fseeko(orig, copypos, SEEK_SET) == -1)
383 e45f7eba 2022-05-14 naddy return got_error_from_errno("fseeko");
385 e9ce266e 2022-03-07 op while (pos == -1 || copypos < pos) {
386 e9ce266e 2022-03-07 op len = sizeof(buf);
388 e9ce266e 2022-03-07 op len = MIN(len, (size_t)pos - copypos);
389 e9ce266e 2022-03-07 op r = fread(buf, 1, len, orig);
390 e9ce266e 2022-03-07 op if (r != len && ferror(orig))
391 e9ce266e 2022-03-07 op return got_error_from_errno("fread");
392 e9ce266e 2022-03-07 op w = fwrite(buf, 1, r, tmp);
394 e9ce266e 2022-03-07 op return got_error_from_errno("fwrite");
395 e9ce266e 2022-03-07 op copypos += len;
396 e9ce266e 2022-03-07 op if (r != len && feof(orig)) {
397 e9ce266e 2022-03-07 op if (pos == -1)
399 60aa1fa0 2022-03-17 op return got_error(GOT_ERR_HUNK_FAILED);
405 41208069 2023-10-23 op static int lines_eq(struct got_patch_line *, const char *, size_t, int *);
407 e9ce266e 2022-03-07 op static const struct got_error *
408 35095610 2022-06-14 op locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
410 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
411 41208069 2023-10-23 op struct got_patch_line *l = &h->lines[0];
412 e9ce266e 2022-03-07 op char *line = NULL;
413 41208069 2023-10-23 op char mode = l->mode;
414 e9ce266e 2022-03-07 op size_t linesize = 0;
415 e9ce266e 2022-03-07 op ssize_t linelen;
416 e9ce266e 2022-03-07 op off_t match = -1;
417 445d38d7 2022-08-01 op int mangled = 0, match_lineno = -1;
421 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
422 e9ce266e 2022-03-07 op if (linelen == -1) {
423 e9ce266e 2022-03-07 op if (ferror(orig))
424 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
425 04a87797 2024-04-23 op /* An EOF is fine iff the target file is empty. */
426 04a87797 2024-04-23 op if (feof(orig) && match == -1 && h->old_lines != 0)
427 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
429 04a87797 2024-04-23 op match_lineno = (*lineno)-1;
433 41208069 2023-10-23 op if ((mode == ' ' && lines_eq(l, line, linelen, &mangled)) ||
434 41208069 2023-10-23 op (mode == '-' && lines_eq(l, line, linelen, &mangled)) ||
435 e9ce266e 2022-03-07 op (mode == '+' && *lineno == h->old_from)) {
436 e9ce266e 2022-03-07 op match = ftello(orig);
437 e9ce266e 2022-03-07 op if (match == -1) {
438 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
441 e9ce266e 2022-03-07 op match -= linelen;
442 e9ce266e 2022-03-07 op match_lineno = (*lineno)-1;
445 d7c808b7 2022-08-01 op if (*lineno >= h->old_from && match != -1) {
447 d7c808b7 2022-08-01 op h->ws_mangled = 1;
452 e9ce266e 2022-03-07 op if (err == NULL) {
453 33df9995 2022-03-11 op *pos = match;
454 e9ce266e 2022-03-07 op *lineno = match_lineno;
455 e45f7eba 2022-05-14 naddy if (fseeko(orig, match, SEEK_SET) == -1)
456 e45f7eba 2022-05-14 naddy err = got_error_from_errno("fseeko");
464 41208069 2023-10-23 op lines_eq(struct got_patch_line *l, const char *b, size_t len, int *mangled)
466 41208069 2023-10-23 op char *a = l->line;
469 41208069 2023-10-23 op if (len > 00 && b[len - 1] == '\n')
472 a92a2042 2022-07-02 op *mangled = 0;
473 41208069 2023-10-23 op if (l->len == len && !memcmp(a, b, len))
476 a92a2042 2022-07-02 op *mangled = 1;
480 41208069 2023-10-23 op while (i < l->len &&
481 41208069 2023-10-23 op (a[i] == '\t' || a[i] == ' ' || a[i] == '\f'))
483 41208069 2023-10-23 op while (j < len &&
484 41208069 2023-10-23 op (b[j] == '\t' || b[j] == ' ' || b[j] == '\f'))
486 41208069 2023-10-23 op if (i == l->len || j == len || a[i] != b[j])
491 41208069 2023-10-23 op return (i == l->len && j == len);
494 e9ce266e 2022-03-07 op static const struct got_error *
495 e9ce266e 2022-03-07 op test_hunk(FILE *orig, struct got_patch_hunk *h)
497 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
498 e9ce266e 2022-03-07 op char *line = NULL;
499 e9ce266e 2022-03-07 op size_t linesize = 0, i = 0;
500 e9ce266e 2022-03-07 op ssize_t linelen;
503 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
504 41208069 2023-10-23 op switch (h->lines[i].mode) {
509 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
510 e9ce266e 2022-03-07 op if (linelen == -1) {
511 e9ce266e 2022-03-07 op if (ferror(orig))
512 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
514 e9ce266e 2022-03-07 op err = got_error(
515 60aa1fa0 2022-03-17 op GOT_ERR_HUNK_FAILED);
518 41208069 2023-10-23 op if (!lines_eq(&h->lines[i], line, linelen, &mangled)) {
519 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
523 a92a2042 2022-07-02 op h->ws_mangled = 1;
533 e9ce266e 2022-03-07 op static const struct got_error *
534 a92a2042 2022-07-02 op apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
537 a92a2042 2022-07-02 op const struct got_error *err = NULL;
538 a92a2042 2022-07-02 op const char *t;
539 a92a2042 2022-07-02 op size_t linesize = 0, i, new = 0;
540 a92a2042 2022-07-02 op char *line = NULL;
543 a92a2042 2022-07-02 op ssize_t linelen;
545 a92a2042 2022-07-02 op if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
546 a92a2042 2022-07-02 op return got_error_from_errno("fseeko");
548 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
549 41208069 2023-10-23 op switch (mode = h->lines[i].mode) {
553 a92a2042 2022-07-02 op if (orig != NULL) {
554 a92a2042 2022-07-02 op linelen = getline(&line, &linesize, orig);
555 a92a2042 2022-07-02 op if (linelen == -1) {
556 a92a2042 2022-07-02 op err = got_error_from_errno("getline");
559 a92a2042 2022-07-02 op if (line[linelen - 1] == '\n')
560 a92a2042 2022-07-02 op line[linelen - 1] = '\0';
562 41208069 2023-10-23 op l = linelen - 1;
564 41208069 2023-10-23 op t = h->lines[i].line;
565 41208069 2023-10-23 op l = h->lines[i].len;
567 a92a2042 2022-07-02 op if (mode == '-')
569 41208069 2023-10-23 op if (fwrite(t, 1, l, tmp) != l ||
570 41208069 2023-10-23 op fputc('\n', tmp) == EOF) {
571 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
577 41208069 2023-10-23 op t = h->lines[i].line;
578 41208069 2023-10-23 op l = h->lines[i].len;
579 41208069 2023-10-23 op if (fwrite(t, 1, l, tmp) != l) {
580 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
583 b2832778 2022-04-23 op if (new != h->new_lines || !h->new_nonl) {
584 a92a2042 2022-07-02 op if (fprintf(tmp, "\n") < 0) {
585 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
598 6e96b326 2022-03-12 op static const struct got_error *
599 5dffb1a1 2022-07-02 op patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
601 6e96b326 2022-03-12 op const struct got_error *err = NULL;
602 6e96b326 2022-03-12 op struct got_patch_hunk *h;
603 2be5e1a2 2022-03-16 op struct stat sb;
604 35095610 2022-06-14 op int lineno = 0;
605 6e96b326 2022-03-12 op off_t copypos, pos;
606 6e96b326 2022-03-12 op char *line = NULL;
607 6e96b326 2022-03-12 op size_t linesize = 0;
608 6e96b326 2022-03-12 op ssize_t linelen;
610 e9ce266e 2022-03-07 op if (p->old == NULL) { /* create */
611 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
612 6e96b326 2022-03-12 op if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
613 6e96b326 2022-03-12 op return got_error(GOT_ERR_PATCH_MALFORMED);
614 a92a2042 2022-07-02 op return apply_hunk(orig, tmp, h, &lineno, 0);
617 684a9a6c 2022-12-31 op /* When deleting binary files there are no hunks to apply. */
618 684a9a6c 2022-12-31 op if (p->new == NULL && STAILQ_EMPTY(&p->head))
621 827bcd6c 2022-06-19 op if (fstat(fileno(orig), &sb) == -1)
622 827bcd6c 2022-06-19 op return got_error_from_errno("fstat");
625 e9ce266e 2022-03-07 op STAILQ_FOREACH(h, &p->head, entries) {
627 33df9995 2022-03-11 op err = locate_hunk(orig, h, &pos, &lineno);
628 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
629 60aa1fa0 2022-03-17 op h->err = err;
630 e9ce266e 2022-03-07 op if (err != NULL)
632 05737d49 2022-06-19 op err = copy(tmp, orig, copypos, pos);
633 e9ce266e 2022-03-07 op if (err != NULL)
635 e9ce266e 2022-03-07 op copypos = pos;
637 e9ce266e 2022-03-07 op err = test_hunk(orig, h);
638 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
640 e9ce266e 2022-03-07 op * try to apply the hunk again starting the search
641 e9ce266e 2022-03-07 op * after the previous partial match.
643 827bcd6c 2022-06-19 op if (fseeko(orig, pos, SEEK_SET) == -1)
644 827bcd6c 2022-06-19 op return got_error_from_errno("fseeko");
645 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
646 827bcd6c 2022-06-19 op if (linelen == -1)
647 827bcd6c 2022-06-19 op return got_error_from_errno("getline");
649 e9ce266e 2022-03-07 op goto tryagain;
651 e9ce266e 2022-03-07 op if (err != NULL)
654 60aa1fa0 2022-03-17 op if (lineno + 1 != h->old_from)
655 60aa1fa0 2022-03-17 op h->offset = lineno + 1 - h->old_from;
657 a92a2042 2022-07-02 op err = apply_hunk(orig, tmp, h, &lineno, pos);
658 e9ce266e 2022-03-07 op if (err != NULL)
661 e9ce266e 2022-03-07 op copypos = ftello(orig);
662 827bcd6c 2022-06-19 op if (copypos == -1)
663 827bcd6c 2022-06-19 op return got_error_from_errno("ftello");
666 60aa1fa0 2022-03-17 op if (p->new == NULL && sb.st_size != copypos) {
667 60aa1fa0 2022-03-17 op h = STAILQ_FIRST(&p->head);
668 60aa1fa0 2022-03-17 op h->err = got_error(GOT_ERR_HUNK_FAILED);
669 60aa1fa0 2022-03-17 op err = h->err;
670 05737d49 2022-06-19 op } else if (!feof(orig))
671 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, -1);
676 dbda770b 2022-03-13 op static const struct got_error *
677 60aa1fa0 2022-03-17 op report_progress(struct patch_args *pa, const char *old, const char *new,
678 60aa1fa0 2022-03-17 op unsigned char status, const struct got_error *orig_error)
680 60aa1fa0 2022-03-17 op const struct got_error *err;
681 60aa1fa0 2022-03-17 op struct got_patch_hunk *h;
683 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, status,
684 a92a2042 2022-07-02 op orig_error, 0, 0, 0, 0, 0, 0, NULL);
688 60aa1fa0 2022-03-17 op STAILQ_FOREACH(h, pa->head, entries) {
689 a92a2042 2022-07-02 op if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
692 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
693 60aa1fa0 2022-03-17 op h->old_from, h->old_lines, h->new_from, h->new_lines,
694 a92a2042 2022-07-02 op h->offset, h->ws_mangled, h->err);
702 60aa1fa0 2022-03-17 op static const struct got_error *
703 b22138f5 2022-03-16 op patch_delete(void *arg, unsigned char status, unsigned char staged_status,
704 b22138f5 2022-03-16 op const char *path)
706 60aa1fa0 2022-03-17 op return report_progress(arg, path, NULL, status, NULL);
709 b22138f5 2022-03-16 op static const struct got_error *
710 b22138f5 2022-03-16 op patch_add(void *arg, unsigned char status, const char *path)
712 60aa1fa0 2022-03-17 op return report_progress(arg, NULL, path, status, NULL);
715 b22138f5 2022-03-16 op static const struct got_error *
716 55e9459f 2022-06-19 op open_blob(char **path, FILE **fp, const char *blobid,
717 55e9459f 2022-06-19 op struct got_repository *repo)
719 6e96b326 2022-03-12 op const struct got_error *err = NULL;
720 55e9459f 2022-06-19 op struct got_blob_object *blob = NULL;
721 db0dfdd7 2022-06-27 op struct got_object_id id, *idptr, *matched_id = NULL;
722 eb81bc23 2022-06-28 tracey int fd = -1;
725 55e9459f 2022-06-19 op *path = NULL;
727 db0dfdd7 2022-06-27 op if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
728 db0dfdd7 2022-06-27 op err = got_repo_match_object_id(&matched_id, NULL, blobid,
729 db0dfdd7 2022-06-27 op GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
733 db0dfdd7 2022-06-27 op idptr = matched_id;
735 87a3ab84 2023-02-23 op if (!got_parse_object_id(&id, blobid, GOT_HASH_SHA1))
736 db0dfdd7 2022-06-27 op return got_error(GOT_ERR_BAD_OBJ_ID_STR);
740 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
741 eb81bc23 2022-06-28 tracey if (fd == -1) {
742 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
743 eb81bc23 2022-06-28 tracey goto done;
746 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
750 b90054ed 2022-11-01 stsp err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
755 55e9459f 2022-06-19 op err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
760 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
761 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
763 55e9459f 2022-06-19 op got_object_blob_close(blob);
764 db0dfdd7 2022-06-27 op if (matched_id != NULL)
765 db0dfdd7 2022-06-27 op free(matched_id);
767 55e9459f 2022-06-19 op if (*fp != NULL)
769 55e9459f 2022-06-19 op if (*path != NULL)
770 55e9459f 2022-06-19 op unlink(*path);
773 55e9459f 2022-06-19 op *path = NULL;
778 55e9459f 2022-06-19 op static const struct got_error *
779 5f56d41e 2022-07-28 op prepare_merge(int *do_merge, char **apath, FILE **afile,
780 5f56d41e 2022-07-28 op struct got_worktree *worktree, struct got_repository *repo,
781 5f56d41e 2022-07-28 op struct got_patch *p, struct got_object_id *commit_id,
782 5f56d41e 2022-07-28 op struct got_tree_object *tree, const char *path)
784 5f56d41e 2022-07-28 op const struct got_error *err = NULL;
786 5f56d41e 2022-07-28 op *do_merge = 0;
787 5f56d41e 2022-07-28 op *apath = NULL;
788 5f56d41e 2022-07-28 op *afile = NULL;
790 5f56d41e 2022-07-28 op /* don't run the diff3 merge on creations/deletions */
791 5f56d41e 2022-07-28 op if (p->old == NULL || p->new == NULL)
794 5f56d41e 2022-07-28 op if (commit_id) {
795 5f56d41e 2022-07-28 op struct got_object_id *id;
797 5f56d41e 2022-07-28 op err = got_object_tree_find_path(&id, NULL, repo, tree, path);
800 5f56d41e 2022-07-28 op got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
801 5f56d41e 2022-07-28 op got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
803 5f56d41e 2022-07-28 op err = open_blob(apath, afile, p->blob, repo);
804 5f56d41e 2022-07-28 op *do_merge = err == NULL;
805 5f56d41e 2022-07-28 op } else if (*p->blob != '\0') {
806 5f56d41e 2022-07-28 op err = open_blob(apath, afile, p->blob, repo);
808 5f56d41e 2022-07-28 op * ignore failures to open this blob, we might have
809 5f56d41e 2022-07-28 op * parsed gibberish.
811 5f56d41e 2022-07-28 op if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
812 5f56d41e 2022-07-28 op err->code != GOT_ERR_NO_OBJ)
814 5f56d41e 2022-07-28 op *do_merge = err == NULL;
821 5f56d41e 2022-07-28 op static const struct got_error *
822 55e9459f 2022-06-19 op apply_patch(int *overlapcnt, struct got_worktree *worktree,
823 55e9459f 2022-06-19 op struct got_repository *repo, struct got_fileindex *fileindex,
824 55e9459f 2022-06-19 op const char *old, const char *new, struct got_patch *p, int nop,
825 5f56d41e 2022-07-28 op int reverse, struct got_object_id *commit_id,
826 5f56d41e 2022-07-28 op struct got_tree_object *tree, struct patch_args *pa,
827 38d61ead 2022-07-23 op got_cancel_cb cancel_cb, void *cancel_arg)
829 55e9459f 2022-06-19 op const struct got_error *err = NULL;
830 5dffb1a1 2022-07-02 op struct stat sb;
831 55e9459f 2022-06-19 op int do_merge = 0, file_renamed = 0;
832 d8b5af43 2022-06-19 op char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
833 ed3bff83 2022-04-23 op char *oldpath = NULL, *newpath = NULL;
834 a6072ec9 2022-10-15 stsp char *tmppath = NULL, *template = NULL;
835 55e9459f 2022-06-19 op char *apath = NULL, *mergepath = NULL;
836 55e9459f 2022-06-19 op FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
838 2be5e1a2 2022-03-16 op mode_t mode = GOT_DEFAULT_FILE_MODE;
840 55e9459f 2022-06-19 op *overlapcnt = 0;
842 5f56d41e 2022-07-28 op err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
843 5f56d41e 2022-07-28 op commit_id, tree, old);
847 615e455c 2022-07-27 op if (reverse && !do_merge)
848 615e455c 2022-07-27 op reverse_patch(p);
850 ed3bff83 2022-04-23 op if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
851 ed3bff83 2022-04-23 op old) == -1) {
852 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
856 ed3bff83 2022-04-23 op if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
857 ed3bff83 2022-04-23 op new) == -1) {
858 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
862 78f5ac24 2022-03-19 op file_renamed = strcmp(oldpath, newpath);
864 6e96b326 2022-03-12 op if (asprintf(&template, "%s/got-patch",
865 6e96b326 2022-03-12 op got_worktree_get_root_path(worktree)) == -1) {
866 6e96b326 2022-03-12 op err = got_error_from_errno(template);
870 5dffb1a1 2022-07-02 op if (p->old != NULL) {
871 5dffb1a1 2022-07-02 op if ((oldfile = fopen(oldpath, "r")) == NULL) {
872 5dffb1a1 2022-07-02 op err = got_error_from_errno2("open", oldpath);
875 5dffb1a1 2022-07-02 op if (fstat(fileno(oldfile), &sb) == -1) {
876 5dffb1a1 2022-07-02 op err = got_error_from_errno2("fstat", oldpath);
879 5dffb1a1 2022-07-02 op mode = sb.st_mode;
880 611e5fc2 2022-09-21 mark } else if (p->xbit)
881 611e5fc2 2022-09-21 mark mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
883 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, template, "");
886 55e9459f 2022-06-19 op outfd = fileno(tmpfile);
887 5dffb1a1 2022-07-02 op err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
891 55e9459f 2022-06-19 op if (do_merge) {
892 497a5915 2022-06-27 op const char *type, *id;
894 55e9459f 2022-06-19 op if (fseeko(afile, 0, SEEK_SET) == -1 ||
895 55e9459f 2022-06-19 op fseeko(oldfile, 0, SEEK_SET) == -1 ||
896 55e9459f 2022-06-19 op fseeko(tmpfile, 0, SEEK_SET) == -1) {
897 55e9459f 2022-06-19 op err = got_error_from_errno("fseeko");
901 55e9459f 2022-06-19 op if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
902 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
903 55e9459f 2022-06-19 op oldlabel = NULL;
907 55e9459f 2022-06-19 op if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
908 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
909 55e9459f 2022-06-19 op newlabel = NULL;
913 497a5915 2022-06-27 op if (*p->cid != '\0') {
914 497a5915 2022-06-27 op type = "commit";
917 497a5915 2022-06-27 op type = "blob";
918 497a5915 2022-06-27 op id = p->blob;
921 497a5915 2022-06-27 op if (asprintf(&anclabel, "%s %s", type, id) == -1) {
922 d8b5af43 2022-06-19 op err = got_error_from_errno("asprintf");
923 d8b5af43 2022-06-19 op anclabel = NULL;
927 38d61ead 2022-07-23 op if (reverse) {
931 38d61ead 2022-07-23 op s = anclabel;
932 38d61ead 2022-07-23 op anclabel = newlabel;
933 38d61ead 2022-07-23 op newlabel = s;
936 38d61ead 2022-07-23 op afile = tmpfile;
940 b90054ed 2022-11-01 stsp err = got_opentemp_named(&mergepath, &mergefile, template, "");
943 55e9459f 2022-06-19 op outfd = fileno(mergefile);
945 55e9459f 2022-06-19 op err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
946 d8b5af43 2022-06-19 op oldfile, oldlabel, anclabel, newlabel,
947 55e9459f 2022-06-19 op GOT_DIFF_ALGORITHM_PATIENCE);
955 5b67f96e 2022-03-13 op if (p->old != NULL && p->new == NULL) {
956 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
957 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
961 b2b3fce1 2022-10-29 op if (fchmod(outfd, apply_umask(mode)) == -1) {
962 41a37a44 2022-04-23 op err = got_error_from_errno2("chmod", tmppath);
966 a6072ec9 2022-10-15 stsp if (mergepath) {
967 a6072ec9 2022-10-15 stsp err = got_path_move_file(mergepath, newpath);
970 a6072ec9 2022-10-15 stsp free(mergepath);
971 a6072ec9 2022-10-15 stsp mergepath = NULL;
973 a6072ec9 2022-10-15 stsp err = got_path_move_file(tmppath, newpath);
976 a6072ec9 2022-10-15 stsp free(tmppath);
977 a6072ec9 2022-10-15 stsp tmppath = NULL;
980 6e96b326 2022-03-12 op if (file_renamed) {
981 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
982 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
983 6e96b326 2022-03-12 op if (err == NULL)
984 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo,
985 f2dd7807 2022-05-17 op worktree, fileindex, patch_add,
988 78f5ac24 2022-03-19 op unlink(newpath);
989 78f5ac24 2022-03-19 op } else if (p->old == NULL) {
990 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo, worktree,
991 f2dd7807 2022-05-17 op fileindex, patch_add, pa);
993 78f5ac24 2022-03-19 op unlink(newpath);
994 55e9459f 2022-06-19 op } else if (*overlapcnt != 0)
995 55e9459f 2022-06-19 op err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
996 9802c41c 2022-06-21 op else if (do_merge)
997 9802c41c 2022-06-21 op err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
999 ed3bff83 2022-04-23 op err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
1002 6f5cb1bd 2022-03-08 op free(template);
1004 a6072ec9 2022-10-15 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1005 a6072ec9 2022-10-15 stsp err = got_error_from_errno("unlink");
1006 55e9459f 2022-06-19 op if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
1007 06c44edc 2022-06-15 stsp err = got_error_from_errno("fclose");
1008 e9ce266e 2022-03-07 op free(tmppath);
1010 ed3bff83 2022-04-23 op free(oldpath);
1011 827bcd6c 2022-06-19 op if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
1012 827bcd6c 2022-06-19 op err = got_error_from_errno("fclose");
1014 a6072ec9 2022-10-15 stsp if (apath != NULL && unlink(apath) == -1 && err == NULL)
1015 a6072ec9 2022-10-15 stsp err = got_error_from_errno("unlink");
1016 55e9459f 2022-06-19 op if (afile != NULL && fclose(afile) == EOF && err == NULL)
1017 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
1018 55e9459f 2022-06-19 op free(apath);
1020 a6072ec9 2022-10-15 stsp if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
1021 a6072ec9 2022-10-15 stsp err = got_error_from_errno("unlink");
1022 55e9459f 2022-06-19 op if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
1023 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
1024 55e9459f 2022-06-19 op free(mergepath);
1026 ed3bff83 2022-04-23 op free(newpath);
1027 55e9459f 2022-06-19 op free(oldlabel);
1028 55e9459f 2022-06-19 op free(newlabel);
1029 d8b5af43 2022-06-19 op free(anclabel);
1033 e9ce266e 2022-03-07 op const struct got_error *
1034 e9ce266e 2022-03-07 op got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1035 5f56d41e 2022-07-28 op int nop, int strip, int reverse, struct got_object_id *commit_id,
1036 5f56d41e 2022-07-28 op got_patch_progress_cb progress_cb, void *progress_arg,
1037 5f56d41e 2022-07-28 op got_cancel_cb cancel_cb, void *cancel_arg)
1039 4bcdc895 2022-05-17 op const struct got_error *err = NULL, *complete_err = NULL;
1040 78f5ac24 2022-03-19 op struct got_fileindex *fileindex = NULL;
1041 5f56d41e 2022-07-28 op struct got_commit_object *commit = NULL;
1042 5f56d41e 2022-07-28 op struct got_tree_object *tree = NULL;
1043 f2dd7807 2022-05-17 op char *fileindex_path = NULL;
1044 60aa1fa0 2022-03-17 op char *oldpath, *newpath;
1045 e9ce266e 2022-03-07 op struct imsgbuf *ibuf;
1046 e9ce266e 2022-03-07 op int imsg_fds[2] = {-1, -1};
1047 55e9459f 2022-06-19 op int overlapcnt, done = 0, failed = 0;
1050 e9ce266e 2022-03-07 op ibuf = calloc(1, sizeof(*ibuf));
1051 e9ce266e 2022-03-07 op if (ibuf == NULL) {
1052 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
1056 e9ce266e 2022-03-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1057 e9ce266e 2022-03-07 op err = got_error_from_errno("socketpair");
1061 e9ce266e 2022-03-07 op pid = fork();
1062 e9ce266e 2022-03-07 op if (pid == -1) {
1063 e9ce266e 2022-03-07 op err = got_error_from_errno("fork");
1065 e9ce266e 2022-03-07 op } else if (pid == 0) {
1066 e9ce266e 2022-03-07 op got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1068 e9ce266e 2022-03-07 op /* not reached */
1071 e9ce266e 2022-03-07 op if (close(imsg_fds[1]) == -1) {
1072 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1075 e9ce266e 2022-03-07 op imsg_fds[1] = -1;
1076 e9ce266e 2022-03-07 op imsg_init(ibuf, imsg_fds[0]);
1078 e9ce266e 2022-03-07 op err = send_patch(ibuf, fd);
1083 f2dd7807 2022-05-17 op err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1088 5f56d41e 2022-07-28 op if (commit_id) {
1089 5f56d41e 2022-07-28 op err = got_object_open_as_commit(&commit, repo, commit_id);
1093 5f56d41e 2022-07-28 op err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1098 e9ce266e 2022-03-07 op while (!done && err == NULL) {
1099 e9ce266e 2022-03-07 op struct got_patch p;
1100 60aa1fa0 2022-03-17 op struct patch_args pa;
1102 60aa1fa0 2022-03-17 op pa.progress_cb = progress_cb;
1103 60aa1fa0 2022-03-17 op pa.progress_arg = progress_arg;
1104 60aa1fa0 2022-03-17 op pa.head = &p.head;
1106 9d6cabd5 2022-04-07 op err = recv_patch(ibuf, &done, &p, strip);
1107 e9ce266e 2022-03-07 op if (err || done)
1110 78f5ac24 2022-03-19 op err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1111 78f5ac24 2022-03-19 op &newpath, worktree, repo, fileindex);
1112 78f5ac24 2022-03-19 op if (err == NULL)
1113 55e9459f 2022-06-19 op err = apply_patch(&overlapcnt, worktree, repo,
1114 38d61ead 2022-07-23 op fileindex, oldpath, newpath, &p, nop, reverse,
1115 5f56d41e 2022-07-28 op commit_id, tree, &pa, cancel_cb, cancel_arg);
1116 60aa1fa0 2022-03-17 op if (err != NULL) {
1118 60aa1fa0 2022-03-17 op /* recoverable errors */
1119 60aa1fa0 2022-03-17 op if (err->code == GOT_ERR_FILE_STATUS ||
1120 60aa1fa0 2022-03-17 op (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1121 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
1122 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, err);
1123 60aa1fa0 2022-03-17 op else if (err->code == GOT_ERR_HUNK_FAILED)
1124 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
1125 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, NULL);
1127 55e9459f 2022-06-19 op if (overlapcnt != 0)
1130 60aa1fa0 2022-03-17 op free(oldpath);
1131 60aa1fa0 2022-03-17 op free(newpath);
1132 e9ce266e 2022-03-07 op patch_free(&p);
1139 4bcdc895 2022-05-17 op if (fileindex != NULL)
1140 4bcdc895 2022-05-17 op complete_err = got_worktree_patch_complete(fileindex,
1141 4bcdc895 2022-05-17 op fileindex_path);
1142 f2dd7807 2022-05-17 op if (complete_err && err == NULL)
1143 f2dd7807 2022-05-17 op err = complete_err;
1144 4bcdc895 2022-05-17 op free(fileindex_path);
1146 5f56d41e 2022-07-28 op got_object_tree_close(tree);
1148 5f56d41e 2022-07-28 op got_object_commit_close(commit);
1149 e9ce266e 2022-03-07 op if (fd != -1 && close(fd) == -1 && err == NULL)
1150 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1151 e9ce266e 2022-03-07 op if (ibuf != NULL)
1152 e9ce266e 2022-03-07 op imsg_clear(ibuf);
1153 e9ce266e 2022-03-07 op if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1154 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1155 e9ce266e 2022-03-07 op if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1156 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1157 60aa1fa0 2022-03-17 op if (err == NULL && failed)
1158 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_PATCH_FAILED);